From 2040a363e3f38b1f474c61f391458a441f0df5f5 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Thu, 2 May 2024 21:26:14 +0100 Subject: [PATCH] Implement branching --- include/branching.h | 39 +++++++++++++++++++++++++++++++++++++++ include/code.h | 32 ++++++++++++++++++++++++++------ include/parser.h | 19 ++++++++++++++----- include/utils.h | 17 +++++++++++++++++ main.cpp | 4 +--- 5 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 include/branching.h diff --git a/include/branching.h b/include/branching.h new file mode 100644 index 0000000..c283830 --- /dev/null +++ b/include/branching.h @@ -0,0 +1,39 @@ +#include +#include +#include + +using namespace std; + +string translateLabel(string label) +{ + stringstream output; + + output << "(" << label << ")" << endl; + + return output.str(); +} + +string translateGoto(string label) +{ + stringstream output; + + output << "@" << label << endl; + output << "0;JMP" << endl; + + return output.str(); +} + +string translateIfGoto(string label) +{ + stringstream output; + + output << "@SP" << endl; + output << "M=M-1" << endl; + output << "A=M" << endl; + output << "D=M" << endl; + + output << "@" << label << endl; + output << "D;JNE" << endl; + + return output.str(); +} \ No newline at end of file diff --git a/include/code.h b/include/code.h index 8bb605d..c9019b9 100644 --- a/include/code.h +++ b/include/code.h @@ -2,9 +2,9 @@ #include #include #include "types.h" -#include "utils.h" #include "operations.h" #include "memory.h" +#include "branching.h" using namespace std; @@ -15,6 +15,11 @@ private: string filename; vector> commands; + void closeFile() + { + file.close(); + } + public: Code(string path, vector> tokens) { @@ -30,6 +35,11 @@ public: commands = tokens; } + ~Code() + { + closeFile(); + } + void translate() { for (const vector &vec : commands) @@ -45,6 +55,21 @@ public: file << translatePop(filename, determineSegment(vec[1]), stoi(vec[2])); } } + else if (vec.size() == 2) + { + if (vec[0] == "label") + { + file << translateLabel(vec[1]); + } + if (vec[0] == "goto") + { + file << translateGoto(vec[1]); + } + if (vec[0] == "if-goto") + { + file << translateIfGoto(vec[1]); + } + } else if (vec.size() == 1) { switch (determineOperation(vec[0])) @@ -83,9 +108,4 @@ public: } } } - - void closeFile() - { - file.close(); - } }; diff --git a/include/parser.h b/include/parser.h index 9029143..75b0a19 100644 --- a/include/parser.h +++ b/include/parser.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include "utils.h" using namespace std; @@ -37,17 +37,25 @@ private: if (!isEmptyLine(matched[1])) { string command = matched[1]; + + trim(command); vmCode.append(command + '\n'); } continue; } else { + trim(text); vmCode.append(text + '\n'); } } } + void closeFile() + { + file.close(); + } + public: Parser(string path) { @@ -73,6 +81,11 @@ public: matchedVector.push_back(matched[2]); matchedVector.push_back(matched[3]); } + else if (regex_search(text, matched, regex("^(.*) (.*)"))) + { + matchedVector.push_back(matched[1]); + matchedVector.push_back(matched[2]); + } else if (regex_search(text, matched, regex("^(.*)"))) { matchedVector.push_back(matched[1]); @@ -82,8 +95,4 @@ public: return commands; } - - void closeFile() { - file.close(); - } }; diff --git a/include/utils.h b/include/utils.h index 0de1ae8..48bbcff 100644 --- a/include/utils.h +++ b/include/utils.h @@ -5,6 +5,23 @@ using namespace std; +inline void ltrim(string &str) { + str.erase(str.begin(), find_if(str.begin(), str.end(), [](unsigned char ch) { + return !isspace(ch); + })); +} + +inline void rtrim(string &str) { + str.erase(find_if(str.rbegin(), str.rend(), [](unsigned char ch) { + return !isspace(ch); + }).base(), str.end()); +} + +inline void trim(string &str) { + rtrim(str); + ltrim(str); +} + string generateRandomLabel() { random_device rd; mt19937 gen(rd()); diff --git a/main.cpp b/main.cpp index 43345b0..5b657a9 100644 --- a/main.cpp +++ b/main.cpp @@ -34,8 +34,6 @@ int main(int argc, char* argv[]) Code code(translatedPath, commands); code.translate(); - - parser.closeFile(); - code.closeFile(); + return 0; } \ No newline at end of file