From 675a9fe8e87450a91aab242ed53c949a31f2b701 Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Sat, 12 Aug 2023 22:54:17 +0200 Subject: [PATCH] Add dashed arrow. Bug:https://github.com/ArthurSonzogni/Diagon/issues/68 --- src/input_output_test.cpp | 33 ++++++++++--------- src/screen/Screen.cpp | 2 +- src/translator/sequence/Sequence.cpp | 31 ++++++++--------- src/translator/sequence/Sequence.g4 | 4 ++- src/translator/sequence/Sequence.hpp | 1 + test/Sequence/basic_dashed/input | 4 +++ test/Sequence/basic_dashed/output | 18 ++++++++++ .../Sequence/interleaved_complex_dashed/input | 8 +++++ .../interleaved_complex_dashed/output | 24 ++++++++++++++ test/Sequence/interleaved_dashed/input | 6 ++++ test/Sequence/interleaved_dashed/output | 16 +++++++++ 11 files changed, 115 insertions(+), 32 deletions(-) create mode 100644 test/Sequence/basic_dashed/input create mode 100644 test/Sequence/basic_dashed/output create mode 100644 test/Sequence/interleaved_complex_dashed/input create mode 100644 test/Sequence/interleaved_complex_dashed/output create mode 100644 test/Sequence/interleaved_dashed/input create mode 100644 test/Sequence/interleaved_dashed/output diff --git a/src/input_output_test.cpp b/src/input_output_test.cpp index f48cab6..ff86bbe 100644 --- a/src/input_output_test.cpp +++ b/src/input_output_test.cpp @@ -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; } } diff --git a/src/screen/Screen.cpp b/src/screen/Screen.cpp index c0ec366..1a6bdf6 100644 --- a/src/screen/Screen.cpp +++ b/src/screen/Screen.cpp @@ -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; } } diff --git a/src/translator/sequence/Sequence.cpp b/src/translator/sequence/Sequence.cpp index cda1cf1..3c3154d 100644 --- a/src/translator/sequence/Sequence.cpp +++ b/src/translator/sequence/Sequence.cpp @@ -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. @@ -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." @@ -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); } diff --git a/src/translator/sequence/Sequence.g4 b/src/translator/sequence/Sequence.g4 index 321033e..6a455ad 100644 --- a/src/translator/sequence/Sequence.g4 +++ b/src/translator/sequence/Sequence.g4 @@ -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: '>'; @@ -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 diff --git a/src/translator/sequence/Sequence.hpp b/src/translator/sequence/Sequence.hpp index ff5a364..b91286a 100644 --- a/src/translator/sequence/Sequence.hpp +++ b/src/translator/sequence/Sequence.hpp @@ -42,6 +42,7 @@ struct Message { std::wstring to; int id = -1; std::vector messages; + bool dashed = false; Direction direction = Direction::Right; diff --git a/test/Sequence/basic_dashed/input b/test/Sequence/basic_dashed/input new file mode 100644 index 0000000..779e667 --- /dev/null +++ b/test/Sequence/basic_dashed/input @@ -0,0 +1,4 @@ +A --> B: Make me a sandwitch +B --> A: No +A --> B: Sudo make me a sandwitch +B --> A: Okay diff --git a/test/Sequence/basic_dashed/output b/test/Sequence/basic_dashed/output new file mode 100644 index 0000000..b044e56 --- /dev/null +++ b/test/Sequence/basic_dashed/output @@ -0,0 +1,18 @@ +┌─┐ ┌─┐ +│A│ │B│ +└┬┘ └┬┘ + │ │ + │ Make me a sandwitch │ + │----------------------->│ + │ │ + │ No │ + │<-----------------------│ + │ │ + │Sudo make me a sandwitch│ + │----------------------->│ + │ │ + │ Okay │ + │<-----------------------│ +┌┴┐ ┌┴┐ +│A│ │B│ +└─┘ └─┘ diff --git a/test/Sequence/interleaved_complex_dashed/input b/test/Sequence/interleaved_complex_dashed/input new file mode 100644 index 0000000..7dbf1c1 --- /dev/null +++ b/test/Sequence/interleaved_complex_dashed/input @@ -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 diff --git a/test/Sequence/interleaved_complex_dashed/output b/test/Sequence/interleaved_complex_dashed/output new file mode 100644 index 0000000..94727ae --- /dev/null +++ b/test/Sequence/interleaved_complex_dashed/output @@ -0,0 +1,24 @@ +┌─┐ ┌─┐ +│A│ │B│ +└┬┘ └┬┘ + │ │ + │--. │ + │message 2│ + │-------->│ + │ | │ + │message 1│ + │ `----->│ + │ │ + │----. │ + │--. | │ + │message 5│ + │-------->│ + │ | | │ + │message 4│ + │ `----->│ + │ | │ + │message 3│ + │ `--->│ +┌┴┐ ┌┴┐ +│A│ │B│ +└─┘ └─┘ diff --git a/test/Sequence/interleaved_dashed/input b/test/Sequence/interleaved_dashed/input new file mode 100644 index 0000000..646cb7c --- /dev/null +++ b/test/Sequence/interleaved_dashed/input @@ -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 diff --git a/test/Sequence/interleaved_dashed/output b/test/Sequence/interleaved_dashed/output new file mode 100644 index 0000000..661d545 --- /dev/null +++ b/test/Sequence/interleaved_dashed/output @@ -0,0 +1,16 @@ +┌─┐ ┌─┐ +│A│ │B│ +└┬┘ └┬┘ + │ │ + │--. │ + │message 2│ + │-------->│ + │ | │ + │message 1│ + │ `----->│ + │ │ + │message 3│ + │<--------│ +┌┴┐ ┌┴┐ +│A│ │B│ +└─┘ └─┘