From 9f9db63fcaa388eea6e25db4853e3ba4528f9fc7 Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Sun, 4 Dec 2022 14:22:27 +0100 Subject: [PATCH] Improve FlowChart if/then/else --- src/fuzzer.cpp | 1 + src/input_output_test.cpp | 3 + src/translator/flowchart/Flowchart.cpp | 55 +++++++------- test/Flowchart/example/output | 12 ++-- test/Flowchart/if-returned-0/output | 9 ++- test/Flowchart/if-returned-1/output | 9 ++- test/Flowchart/if-returned-2/output | 9 ++- test/Flowchart/if-returned-3/output | 9 ++- test/Flowchart/if-returned-4/output | 2 - test/Flowchart/if-returned-5/output | 2 - test/Flowchart/if-returned-6/output | 1 - test/Flowchart/if_1/output | 11 +-- test/Flowchart/if_1_brace/output | 11 +-- test/Flowchart/if_2/output | 14 ++-- test/Flowchart/if_3/output | 19 ++--- test/Flowchart/if_with_space_condition/output | 11 +-- .../xkcd-do-you-understand-flowchart/output | 72 +++++++++---------- 17 files changed, 120 insertions(+), 130 deletions(-) diff --git a/src/fuzzer.cpp b/src/fuzzer.cpp index 1acaf77..e5aeba5 100644 --- a/src/fuzzer.cpp +++ b/src/fuzzer.cpp @@ -25,6 +25,7 @@ std::string GeneratorString(const char*& data, size_t& size) { extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) { auto& translators = TranslatorList(); + //auto* translator = FindTranslator("Flowchart"); auto& translator = translators[GeneratorInt(data, size) % translators.size()]; std::string input = GeneratorString(data, size); std::string options = GeneratorString(data, size); diff --git a/src/input_output_test.cpp b/src/input_output_test.cpp index a955705..02d36bd 100644 --- a/src/input_output_test.cpp +++ b/src/input_output_test.cpp @@ -71,6 +71,9 @@ int main(int, const char**) { std::cout << "---[Expected]----------------" << std::endl; std::cout << output << std::endl; std::cout << "---------------------" << std::endl; + + std::ofstream(test.path() / "output") << output_computed; + result = EXIT_FAILURE; } } diff --git a/src/translator/flowchart/Flowchart.cpp b/src/translator/flowchart/Flowchart.cpp index eedca5e..881f573 100644 --- a/src/translator/flowchart/Flowchart.cpp +++ b/src/translator/flowchart/Flowchart.cpp @@ -128,22 +128,20 @@ Draw ConnectVertically(Draw a, if (height == 0) return b; - if (a.returned) { - Draw out; - + if (a.bottom.size() == 0 || b.top.size() == 0) { a_shift = Point{0, 0}; - b_shift = Point{0, a.screen.height()}; + b_shift = Point{0, height}; + Draw out; out.screen.Append(a.screen, a_shift.x, a_shift.y); out.screen.Append(b.screen, b_shift.x, b_shift.y); - Shift(a, a_shift); - Shift(b, b_shift); out.left = Merge(a.left, b.left); out.right = Merge(a.right, b.right); out.top = a.top; out.bottom = b.bottom; out.returned = b.returned; + return out; } @@ -173,8 +171,14 @@ Draw ConnectVertically(Draw a, auto& top = out.screen.Pixel(a.bottom[0].x, a.bottom[0].y); if (top == L'─') top = L'┬'; + auto& bottom = out.screen.Pixel(b.top[0].x, b.top[0].y); - bottom = L'▽'; + if (bottom == L'─') + bottom = L'▽'; + if (bottom == L'-') + bottom = L'▽'; + if (bottom == L' ') + bottom = L'│'; out.returned = b.returned; return out; @@ -189,26 +193,14 @@ Draw ConnectHorizontally(Draw a, return b; } - if (a.returned) { - Draw out; - a_shift = Point{0, 0}; - b_shift = Point{a.screen.width(), 0}; - out.screen.Append(a.screen, 0, 0); - out.screen.Append(b.screen, a.screen.width(), 0); - Shift(a, a_shift); - Shift(b, b_shift); - - out.top = Merge(a.top, b.top); - out.left = a.left; - out.right = b.right; - out.bottom = Merge(a.bottom, b.bottom); - - out.returned = b.returned; - return out; - } - - a_shift = Point{0, 0}; - b_shift = Point{width, a_shift.y + a.right[0].y - b.left[0].y}; + a_shift = Point{ + 0, + 0, + }; + b_shift = Point{ + width, + a_shift.y + a.right[0].y - b.left[0].y, + }; int shifting = std::max(0, a_shift.y - b_shift.y); a_shift.y += shifting; @@ -226,7 +218,7 @@ Draw ConnectHorizontally(Draw a, out.right = b.right; out.bottom = Merge(a.bottom, b.bottom); - out.returned = b.returned; + out.returned = a.returned || b.returned; out.screen.DrawHorizontalLine(a.right[0].x + 1, b.left[0].x - 1, a.right[0].y, L'_'); @@ -277,11 +269,10 @@ Draw ParseUnmerged(FlowchartParser::ConditionContext* condition, bool is_final); Draw Noop() { Draw draw; - draw.screen.Resize(1,1); - // draw.screen.Pixel(0,0) = L'─'; + draw.screen.Resize(1, 1); draw.left = {{0, 0}}; draw.right = {{0, 0}}; - draw.bottom = {{0, 1}}; + draw.bottom = {{0, 0}}; draw.top = {{0, 0}}; return draw; } @@ -558,6 +549,8 @@ Draw Parse(FlowchartParser::ConditionContext* condition, bool is_final) { Draw Parse(FlowchartParser::ReturninstructionContext* instruction) { Draw draw = Parse(instruction->instruction(), true); + draw.bottom = {}; + draw.right = {}; draw.returned = true; return draw; } diff --git a/test/Flowchart/example/output b/test/Flowchart/example/output index 88216b7..5838b26 100644 --- a/test/Flowchart/example/output +++ b/test/Flowchart/example/output @@ -1,31 +1,31 @@ ┌─────┐ │start│ └──┬──┘ - __________▽__________ + _____________________ ╱ ╲ ┌──────────────┐ ╱ ╲________________________________________________________________________________________│Very expensive│ ╲ Does it cost > 1000 ? ╱yes └──────────────┘ ╲_____________________╱ │no - _________▽_________ ___________________ + ___________________ ___________________ ╱ ╲ ╱ ╲ ┌──────────────┐ - ╱ ╲__________________ ╱ ╲_______________________________│Very expensive│ + ╱ ╲___________________╱ ╲_______________________________│Very expensive│ ╲ Does it cost > 100? ╱yes ╲ Does it cost > 0.5? ╱yes └──────────────┘ ╲___________________╱ ╲___________________╱ │no │no - ________▽_________ _________▽_________ + __________________ ___________________ ╱ ╲ ┌───────┐ ╱ ╲ ┌─────────┐ ╱ ╲_________│Correct│ ╱ ╲____________________│expensive│ ╲ Does it cost > 10? ╱yes └───────┘ ╲ Does it cost > 0.1? ╱yes └─────────┘ ╲__________________╱ ╲___________________╱ │no │no - ________▽________ _________▽__________ + _________________ ____________________ ╱ ╲ ┌─────┐ ╱ ╲ ┌───────┐ ╱ ╲___│Cheap│ ╱ ╲__________│Correct│ ╲ Does it cost > 1? ╱yes└─────┘ ╲ Does it cost > 0.01? ╱yes └───────┘ ╲_________________╱ ╲____________________╱ │no │no - ┌───▽──┐ __________▽__________ + ┌───▽──┐ _____________________ │coucou│ ╱ ╲ ┌─────┐ └──────┘ ╱ ╲___│Cheap│ ╲ Does it cost > 0.001? ╱yes└─────┘ diff --git a/test/Flowchart/if-returned-0/output b/test/Flowchart/if-returned-0/output index e25e233..a8212c8 100644 --- a/test/Flowchart/if-returned-0/output +++ b/test/Flowchart/if-returned-0/output @@ -7,8 +7,7 @@ ┌▽┐ ┌▽┐ │ │e│ │d│ │ └┬┘ └┬┘ │ - │ └───┬───┘ - └──────┬──────┘ - ┌▽┐ - │f│ - └─┘ + └────┬────┴───────┘ + ┌▽┐ + │f│ + └─┘ diff --git a/test/Flowchart/if-returned-1/output b/test/Flowchart/if-returned-1/output index 9558b9b..62820ef 100644 --- a/test/Flowchart/if-returned-1/output +++ b/test/Flowchart/if-returned-1/output @@ -7,8 +7,7 @@ ┌▽┐ ┌▽┐ │e│ │d│ └┬┘ └┬┘ - │ │ - └────┬────┘ - ┌▽┐ - │f│ - └─┘ + └─┬───────┘ + ┌▽┐ + │f│ + └─┘ diff --git a/test/Flowchart/if-returned-2/output b/test/Flowchart/if-returned-2/output index b3ed917..a58f7fa 100644 --- a/test/Flowchart/if-returned-2/output +++ b/test/Flowchart/if-returned-2/output @@ -7,8 +7,7 @@ ┌▽┐ ┌▽┐ │ │e│ │d│ │ └┬┘ └─┘ │ - │ │ - └────────┬────────┘ - ┌▽┐ - │f│ - └─┘ + └────┬────────────┘ + ┌▽┐ + │f│ + └─┘ diff --git a/test/Flowchart/if-returned-3/output b/test/Flowchart/if-returned-3/output index bc993a6..dfb365c 100644 --- a/test/Flowchart/if-returned-3/output +++ b/test/Flowchart/if-returned-3/output @@ -7,8 +7,7 @@ ┌▽┐ ┌▽┐ │ │e│ │d│ │ └─┘ └┬┘ │ - └───┬───┘ - │ - ┌▽┐ - │f│ - └─┘ + └─┬─────┘ + ┌▽┐ + │f│ + └─┘ diff --git a/test/Flowchart/if-returned-4/output b/test/Flowchart/if-returned-4/output index 741f53a..14d14f8 100644 --- a/test/Flowchart/if-returned-4/output +++ b/test/Flowchart/if-returned-4/output @@ -7,8 +7,6 @@ ┌▽┐ ┌▽┐ │e│ │d│ └─┘ └┬┘ - │ - │ ┌▽┐ │f│ └─┘ diff --git a/test/Flowchart/if-returned-5/output b/test/Flowchart/if-returned-5/output index 34deee1..0ded722 100644 --- a/test/Flowchart/if-returned-5/output +++ b/test/Flowchart/if-returned-5/output @@ -7,8 +7,6 @@ ┌▽┐ ┌▽┐ │ │e│ │d│ │ └─┘ └─┘ │ - │ - │ ┌▽┐ │f│ └─┘ diff --git a/test/Flowchart/if-returned-6/output b/test/Flowchart/if-returned-6/output index 09d7aad..c0c62ca 100644 --- a/test/Flowchart/if-returned-6/output +++ b/test/Flowchart/if-returned-6/output @@ -7,7 +7,6 @@ ┌▽┐ ┌▽┐ │e│ │d│ └┬┘ └─┘ - │ ┌▽┐ │f│ └─┘ diff --git a/test/Flowchart/if_1/output b/test/Flowchart/if_1/output index 9b71fe4..430c492 100644 --- a/test/Flowchart/if_1/output +++ b/test/Flowchart/if_1/output @@ -1,5 +1,6 @@ - ___ - ╱ ╲ ┌─┐ -╱ ╲│b│ -╲ a ╱└─┘ - ╲___╱ + ___ + ╱ ╲ ┌─┐ +╱ ╲___│b│ +╲ a ╱yes└─┘ + ╲___╱ + no diff --git a/test/Flowchart/if_1_brace/output b/test/Flowchart/if_1_brace/output index 9b71fe4..430c492 100644 --- a/test/Flowchart/if_1_brace/output +++ b/test/Flowchart/if_1_brace/output @@ -1,5 +1,6 @@ - ___ - ╱ ╲ ┌─┐ -╱ ╲│b│ -╲ a ╱└─┘ - ╲___╱ + ___ + ╱ ╲ ┌─┐ +╱ ╲___│b│ +╲ a ╱yes└─┘ + ╲___╱ + no diff --git a/test/Flowchart/if_2/output b/test/Flowchart/if_2/output index 7da7683..140328d 100644 --- a/test/Flowchart/if_2/output +++ b/test/Flowchart/if_2/output @@ -1,7 +1,7 @@ - ___ - ╱ ╲ ┌─┐ -╱ ╲│b│ -╲ a ╱└┬┘ - ╲___╱ ┌▽┐ - │c│ - └─┘ + ___ + ╱ ╲ ┌─┐ +╱ ╲___│b│ +╲ a ╱yes└┬┘ + ╲___╱ ┌▽┐ + no │c│ + └─┘ diff --git a/test/Flowchart/if_3/output b/test/Flowchart/if_3/output index 0601022..6bf78a6 100644 --- a/test/Flowchart/if_3/output +++ b/test/Flowchart/if_3/output @@ -1,9 +1,10 @@ - ___ - ╱ ╲ ┌─┐ -╱ ╲│b│ -╲ a ╱└┬┘ - ╲_│_╱ │ - ├────┘ - ┌▽┐ - │c│ - └─┘ + ___ + ╱ ╲ ┌─┐ +╱ ╲___│b│ +╲ a ╱yes└┬┘ + ╲___╱ │ + │no │ + └─┬─────┘ + ┌▽┐ + │c│ + └─┘ diff --git a/test/Flowchart/if_with_space_condition/output b/test/Flowchart/if_with_space_condition/output index 30b5612..d1cdcdb 100644 --- a/test/Flowchart/if_with_space_condition/output +++ b/test/Flowchart/if_with_space_condition/output @@ -1,5 +1,6 @@ - ___ - ╱ ╲ ┌───┐ -╱ ╲│c d│ -╲ a b ╱└───┘ - ╲___╱ + ___ + ╱ ╲ ┌───┐ +╱ ╲___│c d│ +╲ a b ╱yes└───┘ + ╲___╱ + no diff --git a/test/Flowchart/xkcd-do-you-understand-flowchart/output b/test/Flowchart/xkcd-do-you-understand-flowchart/output index b2e1538..8ed36d5 100644 --- a/test/Flowchart/xkcd-do-you-understand-flowchart/output +++ b/test/Flowchart/xkcd-do-you-understand-flowchart/output @@ -1,37 +1,35 @@ - _________________ - ╱ ╲ ┌─────┐ - ╱ DO YOU UNDERSTAND ╲____________________________________________________│GOOD!│ - ╲ FLOW CHARTS? ╱yes └──┬──┘ - ╲_________________╱ │ - │no │ - _________▽_________ ______________________ │ - ╱ ╲ ╱ ╲ ┌────┐ │ -╱ OKAY, YOU SEE THE ╲________________╱ ... AND YOU CAN SEE ╲___│GOOD│ │ -╲ LINE LABELED "YES"? ╱yes ╲ THE ONES LABELED "NO"? ╱yes└──┬─┘ │ - ╲___________________╱ ╲______________________╱ │ │ - │no │no │ │ - ________▽_________ _________▽__________ │ │ - ╱ ╲ ┌───────────┐ ╱ ╲ │ │ - ╱ BUT YOU SEE THE ╲___│WAIT, WHAT?│ ╱ BUT YOU JUST ╲___ │ │ - ╲ ONES LABELED "NO"? ╱yes└───────────┘ ╲ FOLLOWED THEM TWICE? ╱yes│ │ │ - ╲__________________╱ ╲____________________╱ │ │ │ - │no │no │ │ │ - ┌───▽───┐ │ │ │ │ - │LISTEN.│ └───────┬───────┘ │ │ - └───┬───┘ ┌──────▽─────┐ │ │ - ┌─────▽────┐ │(THAT WASN'T│ │ │ - │I HATE YOU│ │A QUESTION) │ │ │ - └──────────┘ └──────┬─────┘ │ │ - ┌────▽───┐ │ │ - │SCREW IT│ │ │ - └────┬───┘ │ │ - └─────┬─────┘ │ - │ │ - └─────┬─────┘ - ┌───────▽──────┐ - │LET'S GO DRING│ - └───────┬──────┘ - ┌─────────▽─────────┐ - │HEY, I SHOULD TRY │ - │INSTALLING FREEBSD!│ - └───────────────────┘ + _________________ + ╱ ╲ ┌─────┐ + ╱ DO YOU UNDERSTAND ╲____________________________________________________│GOOD!│ + ╲ FLOW CHARTS? ╱yes └──┬──┘ + ╲_________________╱ │ + │no │ + ___________________ ______________________ │ + ╱ ╲ ╱ ╲ ┌────┐ │ +╱ OKAY, YOU SEE THE ╲________________╱ ... AND YOU CAN SEE ╲___│GOOD│ │ +╲ LINE LABELED "YES"? ╱yes ╲ THE ONES LABELED "NO"? ╱yes└──┬─┘ │ + ╲___________________╱ ╲______________________╱ │ │ + │no │no │ │ + __________________ ____________________ │ │ + ╱ ╲ ┌───────────┐ ╱ ╲ │ │ + ╱ BUT YOU SEE THE ╲___│WAIT, WHAT?│ ╱ BUT YOU JUST ╲___ │ │ + ╲ ONES LABELED "NO"? ╱yes└───────────┘ ╲ FOLLOWED THEM TWICE? ╱yes│ │ │ + ╲__________________╱ ╲____________________╱ │ │ │ + │no │no │ │ │ + ┌───▽───┐ │ │ │ │ + │LISTEN.│ └───┬───────────┘ │ │ + └───┬───┘ ┌──────▽─────┐ │ │ + ┌─────▽────┐ │(THAT WASN'T│ │ │ + │I HATE YOU│ │A QUESTION) │ │ │ + └──────────┘ └──────┬─────┘ │ │ + ┌────▽───┐ │ │ + │SCREW IT│ │ │ + └────┬───┘ │ │ + └─────┬─────────┴─────┘ + ┌───────▽──────┐ + │LET'S GO DRING│ + └───────┬──────┘ + ┌─────────▽─────────┐ + │HEY, I SHOULD TRY │ + │INSTALLING FREEBSD!│ + └───────────────────┘