From 934d2c8f08ce9af29f27353b526be0069964a80e Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Thu, 18 Apr 2024 20:10:29 +0100 Subject: [PATCH] Fix parsing non argument commands --- src/include/code.h | 21 ++++++++++----------- src/include/memory.h | 4 ++-- src/include/parser.h | 4 ++-- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/include/code.h b/src/include/code.h index d23352a..c07a3ef 100644 --- a/src/include/code.h +++ b/src/include/code.h @@ -31,9 +31,9 @@ public: void translate() { - for (const auto &vec : commands) - { - if (vec.size() > 1) + for (const vector &vec : commands) + { + if (vec.size() == 3) { if (vec[0] == "push") { @@ -44,17 +44,16 @@ public: file << translatePop(filename, determineSegment(vec[1]), stoi(vec[2])); } } - - if (vec.size() == 1) + else if (vec.size() == 1) { - if (vec[0] == "add") - cout << translateAdd(); + if (vec.at(0) == "add") + file << translateAdd(); - if (vec[0] == "sub") - cout << translateSub(); + if (vec.at(0) == "sub") + file << translateSub(); - if (vec[0] == "neg") - cout << translateNeg(); + if (vec.at(0) == "neg") + file << translateNeg(); } file << endl; diff --git a/src/include/memory.h b/src/include/memory.h index 106862f..6b21d4c 100644 --- a/src/include/memory.h +++ b/src/include/memory.h @@ -80,7 +80,7 @@ string translatePush(string filename, Segment segment, int index) output << "M=M+1" << endl; return output.str(); -}; +} string translatePop(string filename, Segment segment, int index) { @@ -146,4 +146,4 @@ string translatePop(string filename, Segment segment, int index) output << "M=D" << endl; return output.str(); -}; \ No newline at end of file +} diff --git a/src/include/parser.h b/src/include/parser.h index 58064bd..9029143 100644 --- a/src/include/parser.h +++ b/src/include/parser.h @@ -73,9 +73,9 @@ public: matchedVector.push_back(matched[2]); matchedVector.push_back(matched[3]); } - else + else if (regex_search(text, matched, regex("^(.*)"))) { - matchedVector.push_back(text); + matchedVector.push_back(matched[1]); } commands.push_back(matchedVector); }