Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dashed arrow. #69

Merged
merged 1 commit into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/input_output_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,29 @@ int main(int, const char**) {
continue;
}

std::cout << " [RUN ] " << test.path() << std::endl;
std::string input = ReadFile(test.path() / "input");
std::string output = ReadFile(test.path() / "output");

std::string output_computed = translator->Translate(input, options);

if (output_computed == output) {
// std::cout << " [PASS] " << test.path() << std::endl;
} else {
std::cout << " [FAIL] " << test.path() << std::endl;
std::cout << "---[Output]------------------" << std::endl;
std::cout << output_computed << std::endl;
std::cout << "---[Expected]----------------" << std::endl;
std::cout << output << std::endl;
std::cout << "---------------------" << std::endl;

//std::ofstream(test.path() / "output") << output_computed;
if (!std::filesystem::exists(test.path() / "output")) {
std::cout << " [RUN ] " << test.path() << std::endl;
std::cout << " [Create output] " << std::endl;
std::cout << output_computed;
std::ofstream(test.path() / "output") << output_computed;
continue;
}

result = EXIT_FAILURE;
std::string output = ReadFile(test.path() / "output");
if (output_computed == output) {
continue;
}

std::cout << " [FAIL] " << test.path() << std::endl;
std::cout << "---[Output]------------------" << std::endl;
std::cout << output_computed << std::endl;
std::cout << "---[Expected]----------------" << std::endl;
std::cout << output << std::endl;
std::cout << "---------------------" << std::endl;
result = EXIT_FAILURE;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/screen/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void Screen::DrawHorizontalLine(int left, int right, int y, wchar_t c) {

void Screen::DrawVerticalLine(int top, int bottom, int x, wchar_t c) {
for (int y = top; y <= bottom; ++y) {
lines_[y][x] = L'│';
lines_[y][x] = c;
}
}

Expand Down
31 changes: 16 additions & 15 deletions src/translator/sequence/Sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,19 @@ void Actor::Draw(Screen& screen, int height) {

void Message::Draw(Screen& screen) {
if (line_top == line_bottom) {
screen.DrawHorizontalLine(line_left, line_right, line_top);
screen.DrawHorizontalLine(line_left, line_right, line_top, dashed ? L'-' : L'─');
} else if (direction == Direction::Right) {
screen.DrawHorizontalLine(line_left, line_left + offset, line_top, dashed ? L'-' : L'─');
screen.DrawVerticalLine(line_top, line_bottom, line_left + offset, dashed ? L'|' : L'│');
screen.DrawHorizontalLine(line_left + offset, line_right, line_bottom, dashed ? L'-' : L'─');
screen.DrawPixel(line_left + offset, line_top, dashed ? L'.' : L'┐');
screen.DrawPixel(line_left + offset, line_bottom, dashed ? L'`' : L'└');
} else {
if (direction == Direction::Right) {
screen.DrawHorizontalLine(line_left, line_left + offset, line_top);
screen.DrawVerticalLine(line_top, line_bottom, line_left + offset);
screen.DrawHorizontalLine(line_left + offset, line_right, line_bottom);
screen.DrawPixel(line_left + offset, line_top, L'┐');
screen.DrawPixel(line_left + offset, line_bottom, L'└');
} else {
screen.DrawHorizontalLine(line_right - offset, line_right, line_top);
screen.DrawVerticalLine(line_top, line_bottom, line_right - offset);
screen.DrawHorizontalLine(line_left, line_right - offset, line_bottom);
screen.DrawPixel(line_right - offset, line_top, L'┌');
screen.DrawPixel(line_right - offset, line_bottom, L'┘');
}
screen.DrawHorizontalLine(line_right - offset, line_right, line_top, dashed ? L'-' : L'─');
screen.DrawVerticalLine(line_top, line_bottom, line_right - offset, dashed ? L'|' : L'│');
screen.DrawHorizontalLine(line_left, line_right - offset, line_bottom, dashed ? L'-' : L'─');
screen.DrawPixel(line_right - offset, line_top, L'.');
screen.DrawPixel(line_right - offset, line_bottom, L'`');
}

// Tip of the arrow.
Expand Down Expand Up @@ -464,6 +462,8 @@ void Sequence::AddMessageCommand(

message.from = GetText(message_command->text(0));
message.to = GetText(message_command->text(1));
message.dashed = message_command->arrow()->ARROW_LEFT_DASHED() != nullptr ||
message_command->arrow()->ARROW_RIGHT_DASHED() != nullptr;

if (message.from == message.to) {
std::cerr << "Self messages are not supported yet. It has been ignored."
Expand All @@ -473,7 +473,8 @@ void Sequence::AddMessageCommand(
return;
}

if (message_command->arrow()->ARROW_LEFT()) {
if (message_command->arrow()->ARROW_LEFT() ||
message_command->arrow()->ARROW_LEFT_DASHED()) {
std::swap(message.from, message.to);
}

Expand Down
4 changes: 3 additions & 1 deletion src/translator/sequence/Sequence.g4
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ COMMENT: '/' '*' .*? '*' '/' -> channel(HIDDEN);
LINE_COMMENT: '/' '/' (~('\n'))* -> channel(HIDDEN);

ARROW_RIGHT: '->';
ARROW_RIGHT_DASHED: '-->';
ARROW_LEFT: '<-';
ARROW_LEFT_DASHED: '<--';
COMMA: ':';
LOWER: '<';
GREATER: '>';
Expand Down Expand Up @@ -39,5 +41,5 @@ number: SPACE* NUMBER SPACE*;

comparison: LOWER | GREATER;

arrow: ARROW_LEFT | ARROW_RIGHT;
arrow: ARROW_LEFT | ARROW_RIGHT | ARROW_LEFT_DASHED | ARROW_RIGHT_DASHED;
// vim: filetype=ANTLR
1 change: 1 addition & 0 deletions src/translator/sequence/Sequence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct Message {
std::wstring to;
int id = -1;
std::vector<std::wstring> messages;
bool dashed = false;

Direction direction = Direction::Right;

Expand Down
4 changes: 4 additions & 0 deletions test/Sequence/basic_dashed/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
A --> B: Make me a sandwitch
B --> A: No
A --> B: Sudo make me a sandwitch
B --> A: Okay
18 changes: 18 additions & 0 deletions test/Sequence/basic_dashed/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
┌─┐ ┌─┐
│A│ │B│
└┬┘ └┬┘
│ │
│ Make me a sandwitch │
│----------------------->│
│ │
│ No │
│<-----------------------│
│ │
│Sudo make me a sandwitch│
│----------------------->│
│ │
│ Okay │
│<-----------------------│
┌┴┐ ┌┴┐
│A│ │B│
└─┘ └─┘
8 changes: 8 additions & 0 deletions test/Sequence/interleaved_complex_dashed/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1) A-->B: message 1
2) A-->B: message 2
3) A-->B: message 3
4) A-->B: message 4
5) A-->B: message 5

A: 1<2<3<4<5
B: 2<1<5<4<3
24 changes: 24 additions & 0 deletions test/Sequence/interleaved_complex_dashed/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
┌─┐ ┌─┐
│A│ │B│
└┬┘ └┬┘
│ │
│--. │
│message 2│
│-------->│
│ | │
│message 1│
│ `----->│
│ │
│----. │
│--. | │
│message 5│
│-------->│
│ | | │
│message 4│
│ `----->│
│ | │
│message 3│
│ `--->│
┌┴┐ ┌┴┐
│A│ │B│
└─┘ └─┘
6 changes: 6 additions & 0 deletions test/Sequence/interleaved_dashed/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1) A --> B : message 1
2) A --> B : message 2
3) A <-- B : message 3

A: 1<2
B: 1>2
16 changes: 16 additions & 0 deletions test/Sequence/interleaved_dashed/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
┌─┐ ┌─┐
│A│ │B│
└┬┘ └┬┘
│ │
│--. │
│message 2│
│-------->│
│ | │
│message 1│
│ `----->│
│ │
│message 3│
│<--------│
┌┴┐ ┌┴┐
│A│ │B│
└─┘ └─┘