Skip to content

Commit

Permalink
Add support for do-loop
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurSonzogni committed Dec 10, 2022
1 parent b23757b commit e9c023c
Show file tree
Hide file tree
Showing 30 changed files with 294 additions and 158 deletions.
7 changes: 4 additions & 3 deletions src/fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ 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);
if (translator->Name())
if (std::string(translator->Identifier()) == "Grammar")
return 0;
std::cout << translator->Identifier() << std::endl;
std::cout << input << std::endl;
try {
translator->Translate(input, options);
std::cout << translator->Translate(input, options) << std::endl;
} catch (...) {
}
return 0; // Non-zero return values are reserved for future use.
Expand Down
4 changes: 2 additions & 2 deletions src/input_output_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void ParseDirectoryName(std::string name,
int main(int, const char**) {
int result = EXIT_SUCCESS;
std::string path = test_directory;
std::cout << "test_directory = " << test_directory << std::endl;
//std::cout << "test_directory = " << test_directory << std::endl;

for (auto& dir : std::filesystem::directory_iterator(path)) {
std::string translator_name;
Expand All @@ -64,7 +64,7 @@ int main(int, const char**) {
std::string output_computed = translator->Translate(input, options);

if (output_computed == output) {
std::cout << " [PASS] " << test.path() << std::endl;
//std::cout << " [PASS] " << test.path() << std::endl;
} else {
std::cout << " [FAIL] " << test.path() << std::endl;
std::cout << "---[Output]------------------" << std::endl;
Expand Down
163 changes: 117 additions & 46 deletions src/translator/flowchart/Flowchart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ std::vector<Translator::Example> Flowchart::Examples() {
return {
{
"xkcd - Flowchart explained by flowchart",
R"source(
R"source(
if ("DO YOU UNDERSTAND FLOW CHARTS?")
"GOOD!";
else if ("OKAY, YOU SEE THE LINE LABELED 'YES'?") {
Expand All @@ -61,6 +61,30 @@ else if ("OKAY, YOU SEE THE LINE LABELED 'YES'?") {
"LET'S GO DRING";
"HEY, I SHOULD TRY INSTALLING FREEBSD!"
)source",
},
{
"xkcd - local computer expert",
R"source(
"START";
do {
if ("FIND A MENU ITEM OR BUTTON WHICH LOOKS RELATED TO WHAT YOU WANT TO DO.") {
"CLICK IT.";
} else {
if ("PICK ON AT RANDOM.") {
"CLICK IT.";
} else {
"GOOGLE THE NAME OF THE PROGRAM PLUS A FEW WORDS RELATED TO WHAT YOU WANT TO DO. FOLLOW ANY INSTRUCTIONS.";
}
}
if ("DID IT WORK?")
return "YOU'RE DONE!"
} while("HAVE YOU BEEN TRYING THIS FOR LESS THAN AN HOUR?")
"ASK SOMEONE FOR HELP OR GIVE UP."
)source",
},
};
Expand Down Expand Up @@ -115,8 +139,6 @@ void Shift(Draw& out, Point shift) {
Shift(out.right, shift);
}


Point static_point;
Point static_point_1;
Point static_point_2;

Expand All @@ -141,7 +163,7 @@ Draw ConnectVertically(Draw a,
out.top = a.top;
out.bottom = b.bottom;
out.returned = b.returned;

return out;
}

Expand Down Expand Up @@ -177,6 +199,8 @@ Draw ConnectVertically(Draw a,
bottom = L'';
if (bottom == L'-')
bottom = L'';
if (bottom == L'_')
bottom = L'';
if (bottom == L' ')
bottom = L'';

Expand Down Expand Up @@ -235,7 +259,7 @@ Draw MergeBottoms(Draw draw) {
int right = draw.bottom.back().x;
draw.screen.DrawHorizontalLine(left, right, draw.screen.height() - 1, L'');

for(auto& it : draw.bottom) {
for (auto& it : draw.bottom) {
draw.screen.DrawVerticalLine(it.y + 1, draw.screen.height() - 2, it.x,
L'');
auto& top = draw.screen.Pixel(it.x, it.y);
Expand Down Expand Up @@ -263,7 +287,7 @@ Draw Parse(FlowchartParser::InstructionContext* instruction, bool is_final);
Draw Parse(FlowchartParser::ProgramContext* program, bool is_final);
Draw Parse(FlowchartParser::NoopContext* instruction, bool is_final);
Draw Parse(FlowchartParser::WhileloopContext* whileloop, bool is_final);
// std::string Parse(FlowchartParser::DoloopContext* doloop);
Draw Parse(FlowchartParser::DoloopContext* doloop, bool is_final);

Draw ParseUnmerged(FlowchartParser::ConditionContext* condition, bool is_final);

Expand Down Expand Up @@ -357,7 +381,7 @@ std::vector<std::wstring_view> Broke(std::wstring_view content) {

do {
right--;
} while (Broke(content, right).size() == lines_number);
} while (Broke(content, right).size() == lines_number && right >= 0);
right++;

return Broke(content, right);
Expand Down Expand Up @@ -395,7 +419,7 @@ Draw Diamond(std::string content, bool is_final) {
std::vector<std::wstring_view> lines = Broke(content_ws);
if (lines.size() % 2) {
lines.push_back(L"");
std::rotate(lines.rbegin(), lines.rbegin() + 1, lines.rend());
// std::rotate(lines.rbegin(), lines.rbegin() + 1, lines.rend());
}

int width = 0;
Expand All @@ -407,29 +431,29 @@ Draw Diamond(std::string content, bool is_final) {
width = width + height + 2;

Draw out;
out.screen.Resize(width, height+3);
out.screen.Resize(width, height + 3);

for (int x = height / 2+1; x < width - height / 2-1; ++x) {
for (int x = height / 2 + 1; x < width - height / 2 - 1; ++x) {
out.screen.Pixel(x, 0) = L'_';
out.screen.Pixel(x, height+2) = L'_';
out.screen.Pixel(x, height + 2) = L'_';
}

for (int i = 0; i < height/2+1; ++i) {
int I = width-i-1;
out.screen.Pixel(i,1+height/2-i+0) = L'';
out.screen.Pixel(i,1+height/2+i+1) = L'';
out.screen.Pixel(I,1+height/2-i+0) = L'';
out.screen.Pixel(I,1+height/2+i+1) = L'';
for (int i = 0; i < height / 2 + 1; ++i) {
int I = width - i - 1;
out.screen.Pixel(i, 1 + height / 2 - i + 0) = L'';
out.screen.Pixel(i, 1 + height / 2 + i + 1) = L'';
out.screen.Pixel(I, 1 + height / 2 - i + 0) = L'';
out.screen.Pixel(I, 1 + height / 2 + i + 1) = L'';
}

for(int i = 0; i<lines.size(); ++i)
out.screen.DrawText(height/2+1, i+2, lines[i]);
for (int i = 0; i < lines.size(); ++i)
out.screen.DrawText(height / 2 + 1, i + 2, lines[i]);

width = out.screen.width();
height = out.screen.height();

out.top = {{width / 2 - 1 + width % 2, 0}};
out.bottom = {{width / 2 - 1 + width%2, height - 1}};
out.bottom = {{width / 2 - 1 + width % 2, height - 1}};
out.left = {{0, height / 2}};
out.right = {{width - 1, height / 2}};
out.returned = is_final;
Expand All @@ -454,32 +478,32 @@ Draw Boxed(std::string content, bool is_final) {

draw.top = {{width / 2, 0}};
draw.bottom = {{width / 2, height - 1}};
draw.left = {{0,height / 2}};
draw.left = {{0, height / 2}};
draw.right = {{width - 1, height / 2}};

draw.returned = is_final;
return draw;
}

//Draw Text(std::string content, bool is_final) {
//Draw draw;
//draw.screen.Resize(content.size() + 5, 3);
//draw.screen.DrawText(2, 1, to_wstring(content));
// Draw Text(std::string content, bool is_final) {
// Draw draw;
// draw.screen.Resize(content.size() + 5, 3);
// draw.screen.DrawText(2, 1, to_wstring(content));

//draw.top.x = draw.screen.width() / 2;
//draw.top.y = 0;
// draw.top.x = draw.screen.width() / 2;
// draw.top.y = 0;

//draw.bottom.x = draw.screen.width() / 2;
//draw.bottom.y = draw.screen.height() - 1;
// draw.bottom.x = draw.screen.width() / 2;
// draw.bottom.y = draw.screen.height() - 1;

//draw.left.x = 0;
//draw.left.y = draw.screen.height() / 2;
// draw.left.x = 0;
// draw.left.y = draw.screen.height() / 2;

//draw.right.x = draw.screen.width() - 1;
//draw.right.y = draw.screen.height() / 2;
// draw.right.x = draw.screen.width() - 1;
// draw.right.y = draw.screen.height() / 2;

//draw.returned = is_final;
//return draw;
// draw.returned = is_final;
// return draw;
//}

Draw Unimplemented(bool is_final) {
Expand Down Expand Up @@ -515,7 +539,7 @@ void AddLabel(Screen& screen, Point point, std::wstring_view label) {
}

Draw ParseUnmerged(FlowchartParser::ConditionContext* condition,
bool is_final) {
bool is_final) {
Draw if_ = Diamond(Parse(condition->string()), /*is_final=*/false);
AddLabel(if_.screen, if_.bottom[0], L"no");
AddLabel(if_.screen, if_.right[0], L"yes");
Expand Down Expand Up @@ -565,8 +589,8 @@ Draw Parse(FlowchartParser::InstructionContext* instruction, bool is_final) {
if (instruction->whileloop())
return Parse(instruction->whileloop(), is_final);

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

if (instruction->group())
return Parse(instruction->group(), is_final);
Expand Down Expand Up @@ -648,8 +672,7 @@ Draw Parse(FlowchartParser::WhileloopContext* whileloop, bool is_final) {

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

out.screen.Resize(out.screen.width() + 2, out.screen.height() + 1);

// --- if_left if_right ----
// |
Expand All @@ -658,20 +681,21 @@ Draw Parse(FlowchartParser::WhileloopContext* whileloop, bool is_final) {
// | |
// -----------------------


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.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.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'';
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'')
Expand All @@ -688,6 +712,53 @@ Draw Parse(FlowchartParser::WhileloopContext* whileloop, bool is_final) {
return out;
}

Draw Parse(FlowchartParser::DoloopContext* doloop, bool is_final) {
Draw instruction = MergeBottoms(Parse(doloop->instruction(), is_final));
instruction.left = {};
instruction.right = {};
Draw if_ = Diamond(Parse(doloop->string()), /*is_final=*/false);
Point instruction_position;
Point if_position;
Draw merged = ConnectVertically(std::move(instruction), std::move(if_),
instruction_position, if_position);
// |_______________
// | |
//   [ ] |
// | |
// / \ if_right --
// \ /
// |
// |
merged.left = {};
merged.right = {};

AddLabel(merged.screen, merged.bottom[0], L"no");
AddLabel(merged.screen, merged.right[0], L"yes");

// |___
// |
// Add 2 empty line above
Screen merged_screen;
std::swap(merged.screen, merged_screen);
merged.screen.Resize(merged_screen.width() + 1, merged_screen.height() + 2);
merged.screen.Append(std::move(merged_screen), 0, 2);
Shift(merged, Point{0, 2});

merged.screen.DrawHorizontalLine(merged.right[0].x + 1,
merged.screen.width() - 1,
merged.right[0].y + 2, L'_');
merged.screen.DrawHorizontalLine(merged.top[0].x + 1,
merged.screen.width() - 1, 1, L'');
merged.screen.DrawVerticalLineComplete(1, merged.right[0].y + 2,
merged.screen.width() - 1);

merged.screen.DrawVerticalLineComplete(0, 2, merged.top[0].x);

merged.screen.Pixel(merged.screen.width() - 1, 1) = L'';
merged.screen.Pixel(merged.top[0].x + 1, 1) = L'';
return merged;
}

} // namespace

std::unique_ptr<Translator> FlowchartTranslator() {
Expand Down
15 changes: 12 additions & 3 deletions test/Flowchart/doloop/output
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
┌─────────────┐
│Unimplemented│
└─────────────┘
┌──────┐
│arthur│
└───┬──┘
┌───▽──┐
│andrea│
└───┬──┘
___▽____
╱ ╲
╱ chocolat ╲
╲ ╱yes
╲________╱
no
Loading

0 comments on commit e9c023c

Please sign in to comment.