Skip to content

Commit

Permalink
Improve FlowChart if/then/else
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurSonzogni committed Dec 10, 2022
1 parent ff01f72 commit 9f9db63
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 130 deletions.
1 change: 1 addition & 0 deletions src/fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/input_output_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
55 changes: 24 additions & 31 deletions src/translator/flowchart/Flowchart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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'_');
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions test/Flowchart/example/output
Original file line number Diff line number Diff line change
@@ -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└─────┘
Expand Down
9 changes: 4 additions & 5 deletions test/Flowchart/if-returned-0/output
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
┌▽┐ ┌▽┐ │
│e│ │d│ │
└┬┘ └┬┘ │
│ └───┬───┘
└──────┬──────┘
┌▽┐
│f│
└─┘
└────┬────┴───────┘
┌▽┐
│f│
└─┘
9 changes: 4 additions & 5 deletions test/Flowchart/if-returned-1/output
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
┌▽┐ ┌▽┐
│e│ │d│
└┬┘ └┬┘
│ │
└────┬────┘
┌▽┐
│f│
└─┘
└─┬───────┘
┌▽┐
│f│
└─┘
9 changes: 4 additions & 5 deletions test/Flowchart/if-returned-2/output
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
┌▽┐ ┌▽┐ │
│e│ │d│ │
└┬┘ └─┘ │
│ │
└────────┬────────┘
┌▽┐
│f│
└─┘
└────┬────────────┘
┌▽┐
│f│
└─┘
9 changes: 4 additions & 5 deletions test/Flowchart/if-returned-3/output
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
┌▽┐ ┌▽┐ │
│e│ │d│ │
└─┘ └┬┘ │
└───┬───┘
┌▽┐
│f│
└─┘
└─┬─────┘
┌▽┐
│f│
└─┘
2 changes: 0 additions & 2 deletions test/Flowchart/if-returned-4/output
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
┌▽┐ ┌▽┐
│e│ │d│
└─┘ └┬┘
┌▽┐
│f│
└─┘
2 changes: 0 additions & 2 deletions test/Flowchart/if-returned-5/output
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
┌▽┐ ┌▽┐ │
│e│ │d│ │
└─┘ └─┘ │
┌▽┐
│f│
└─┘
1 change: 0 additions & 1 deletion test/Flowchart/if-returned-6/output
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
┌▽┐ ┌▽┐
│e│ │d│
└┬┘ └─┘
┌▽┐
│f│
└─┘
11 changes: 6 additions & 5 deletions test/Flowchart/if_1/output
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
___
╱ ╲ ┌─┐
╱ ╲│b│
╲ a ╱└─┘
╲___╱
___
╱ ╲ ┌─┐
╱ ╲___│b│
╲ a ╱yes└─┘
╲___╱
no
11 changes: 6 additions & 5 deletions test/Flowchart/if_1_brace/output
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
___
╱ ╲ ┌─┐
╱ ╲│b│
╲ a ╱└─┘
╲___╱
___
╱ ╲ ┌─┐
╱ ╲___│b│
╲ a ╱yes└─┘
╲___╱
no
14 changes: 7 additions & 7 deletions test/Flowchart/if_2/output
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
___
╱ ╲ ┌─┐
╱ ╲│b│
╲ a ╱└┬┘
╲___╱ ┌▽┐
│c│
└─┘
___
╱ ╲ ┌─┐
╱ ╲___│b│
╲ a ╱yes└┬┘
╲___╱ ┌▽┐
no │c│
└─┘
19 changes: 10 additions & 9 deletions test/Flowchart/if_3/output
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
___
╱ ╲ ┌─┐
╱ ╲│b│
╲ a ╱└┬┘
╲_│_╱ │
├────┘
┌▽┐
│c│
└─┘
___
╱ ╲ ┌─┐
╱ ╲___│b│
╲ a ╱yes└┬┘
╲___╱ │
│no │
└─┬─────┘
┌▽┐
│c│
└─┘
11 changes: 6 additions & 5 deletions test/Flowchart/if_with_space_condition/output
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
___
╱ ╲ ┌───┐
╱ ╲│c d│
╲ a b ╱└───┘
╲___╱
___
╱ ╲ ┌───┐
╱ ╲___│c d│
╲ a b ╱yes└───┘
╲___╱
no
Loading

0 comments on commit 9f9db63

Please sign in to comment.