From cfbf4b771e0166f0a5ffde561bf8d3ecf1225860 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Tue, 26 Mar 2024 15:39:11 +0100 Subject: [PATCH] Translate push and pop commands wip --- src/code.h | 102 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/main.cpp | 10 +++-- src/parser.h | 24 ++---------- 3 files changed, 106 insertions(+), 30 deletions(-) diff --git a/src/code.h b/src/code.h index 8a68821..a6e0573 100644 --- a/src/code.h +++ b/src/code.h @@ -4,17 +4,109 @@ using namespace std; +enum Segment +{ + LCL, + ARG, + THIS, + THAT, +}; + class Code { private: ofstream file; -public: - Code(string path) + vector> commands; + + string translatePush(int constant) { - file = ofstream(path); + stringstream output; + + output << '@' << constant << endl; + output << "D=A" << endl; + output << "@SP" << endl; + output << "D=M" << endl; + output << "@SP" << endl; + output << "M=M+1" << endl; + + return output.str(); + }; + + Segment determineSegment(string segment) + { + if (segment == "local") + return Segment::LCL; + if (segment == "argument") + return Segment::ARG; + if (segment == "this") + return Segment::THIS; + if (segment == "that") + return Segment::THAT; } - void writeToFile() { - file << "Hi!" << endl; + string translatePop(Segment segment, int index) + { + stringstream output; + + output << "@SP" << index << endl; + output << "A=M" << endl; + output << "D=M" << endl; + + switch (segment) + { + case LCL: + output << "@LCL" << endl; + break; + case ARG: + output << "@ARG" << endl; + break; + case THIS: + output << "@THIS" << endl; + break; + case THAT: + output << "@THAT" << endl; + break; + + default: + break; + } + + output << "M=D" << endl; + output << "@SP" << endl; + output << "M=M-1" << endl; + + return output.str(); + }; + +public: + Code(string path, vector> tokens) + { + file = ofstream(path); + commands = tokens; + } + + void translate() + { + for (const auto &vec : commands) + { + if (vec.size() > 1) + { + file << vec[0] << "-"; + if (vec.size() > 1) + file << vec[1] << "-"; + if (vec.size() > 1) + file << vec[2]; + file << endl; + } + else + { + file << vec[0] << endl; + } + } + } + + void closeFile() + { + file.close(); } }; diff --git a/src/main.cpp b/src/main.cpp index 63da0c6..5f7ea6d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,13 +22,15 @@ int main(int argc, char* argv[]) Parser parser(sourcePath); - parser.printFile(); + vector> commands = parser.getCommands(); string translatedPath = constructTranslatedPath(sourcePath); - Code code(translatedPath); + Code code(translatedPath, commands); - code.writeToFile(); - + code.translate(); + + parser.closeFile(); + code.closeFile(); return 0; } \ No newline at end of file diff --git a/src/parser.h b/src/parser.h index 902055f..70bfe68 100644 --- a/src/parser.h +++ b/src/parser.h @@ -56,7 +56,7 @@ public: removeCommentsAndWhitespace(); } - vector> getTokens() + vector> getCommands() { stringstream vmCodeStream(vmCode); string text; @@ -83,25 +83,7 @@ public: return tokens; } - void printFile() - { - vector> tokens = getTokens(); - - for (const auto &vec : tokens) - { - if (vec.size() > 1) - { - cout << vec[0] << "-"; - if (vec.size() > 1) - cout << vec[1] << "-"; - if (vec.size() > 1) - cout << vec[2]; - cout << endl; - } - else - { - cout << vec[0] << endl; - } - } + void closeFile() { + file.close(); } };