Skip to content

Commit

Permalink
Add support for while loop in FlowChart
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurSonzogni committed Dec 10, 2022
1 parent 9f9db63 commit b23757b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/input_output_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ int main(int, const char**) {
int result = EXIT_SUCCESS;
std::string path = test_directory;
std::cout << "test_directory = " << test_directory << std::endl;

for (auto& dir : std::filesystem::directory_iterator(path)) {
std::string translator_name;
std::string options;
Expand Down Expand Up @@ -72,7 +73,7 @@ int main(int, const char**) {
std::cout << output << std::endl;
std::cout << "---------------------" << std::endl;

std::ofstream(test.path() / "output") << output_computed;
//std::ofstream(test.path() / "output") << output_computed;

result = EXIT_FAILURE;
}
Expand Down
74 changes: 71 additions & 3 deletions src/translator/flowchart/Flowchart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ Draw Parse(FlowchartParser::GroupContext* group, bool is_final);
Draw Parse(FlowchartParser::InstructionContext* instruction, bool is_final);
Draw Parse(FlowchartParser::ProgramContext* program, bool is_final);
Draw Parse(FlowchartParser::NoopContext* instruction, bool is_final);
// std::string Parse(FlowchartParser::WhileloopContext* whileloop);
Draw Parse(FlowchartParser::WhileloopContext* whileloop, bool is_final);
// std::string Parse(FlowchartParser::DoloopContext* doloop);

Draw ParseUnmerged(FlowchartParser::ConditionContext* condition, bool is_final);
Expand Down Expand Up @@ -562,8 +562,8 @@ Draw Parse(FlowchartParser::InstructionContext* instruction, bool is_final) {
if (instruction->condition())
return Parse(instruction->condition(), is_final);

// if (instruction->whileloop())
// return Parse(instruction->whileloop());
if (instruction->whileloop())
return Parse(instruction->whileloop(), is_final);

// if (instruction->doloop())
// return Parse(instruction->doloop());
Expand Down Expand Up @@ -620,6 +620,74 @@ std::string Flowchart::Translate(const std::string& input,
return Parse(context, true).screen.ToString();
}

Draw Parse(FlowchartParser::WhileloopContext* whileloop, bool is_final) {
Draw if_ = Diamond(Parse(whileloop->string()), /*is_final=*/false);
Screen if_screen;
std::swap(if_.screen, if_screen);
if_.screen.Append(std::move(if_screen), 4, 0);
Shift(if_, Point{4, 0});

AddLabel(if_.screen, if_.bottom[0], L"yes");

Point no_position = if_.left[0];
no_position.x -= 4;
AddLabel(if_.screen, no_position, L"no");

Draw instruction = Parse(whileloop->instruction(), is_final);
instruction = MergeBottoms(instruction);

Point if_left = if_.left[0];
Point if_right = if_.right[0];

Point if_shift;
Point instruction_shift;
Draw merged = ConnectVertically(std::move(if_), std::move(instruction),
if_shift, instruction_shift);
if_left += if_shift;
if_right += if_shift;

Draw out;
out.screen.Append(merged.screen, 1, 0);
out.screen.Resize(out.screen.width()+2, out.screen.height() + 1);


// --- if_left if_right ----
// |
// instruction_bottom |
// | |
// | |
// -----------------------


out.screen.DrawHorizontalLine(1, if_left.x, if_left.y, '_');

if (merged.bottom.size()) {
out.screen.DrawHorizontalLine(if_right.x + 2, out.screen.width() - 2,
if_right.y, '_');
out.screen.DrawHorizontalLine(merged.bottom[0].x + 2, out.screen.width() - 1,
out.screen.height() - 1, L'');
out.screen.DrawVerticalLineComplete(
merged.bottom[0].y + 1, out.screen.height() - 2, merged.bottom[0].x + 1);
out.screen.DrawVerticalLineComplete(if_right.y+1, out.screen.height() - 1,
out.screen.width() - 1);
out.screen.Pixel(merged.bottom[0].x + 1, out.screen.height() - 1) = L'';
out.screen.Pixel(out.screen.width()-1, out.screen.height() - 1) = L'';
auto& connector =
out.screen.Pixel(merged.bottom[0].x + 1, merged.bottom[0].y);
if (connector == L'')
connector = L'';
}

out.top = {merged.top[0] + if_shift};
out.left = {};
out.right = {};
out.bottom = {Point{0, if_left.y}};

out.returned = false;

return out;
}

} // namespace

std::unique_ptr<Translator> FlowchartTranslator() {
Expand Down
8 changes: 6 additions & 2 deletions test/Flowchart/whileloop/input
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
while("arthur")
"chocolat"
while("arthur") {
"chocolat 1"
"chocolat 2"
}

"then"
19 changes: 16 additions & 3 deletions test/Flowchart/whileloop/output
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
┌─────────────┐
│Unimplemented│
└─────────────┘
______
╱ ╲
____╱ ╲_
│ no ╲ arthur ╱ │
│ ╲______╱ │
│ │yes │
│ ┌─────▽────┐ │
│ │chocolat 1│ │
│ └─────┬────┘ │
│ ┌─────▽────┐ │
│ │chocolat 2│ │
│ └─────┬────┘ │
│ └──────┘
┌──▽─┐
│then│
└────┘

0 comments on commit b23757b

Please sign in to comment.