From 4d70d05c27e1d3e7f86e896f955b6a06fe721705 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Thu, 2 May 2024 21:42:00 +0100 Subject: [PATCH] Translate method refactoring --- include/code.h | 27 +++++++++++++------------ include/operations.h | 24 ---------------------- include/parser.h | 1 + include/types.h | 14 +++++++++++-- include/utils.h | 48 +++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 74 insertions(+), 40 deletions(-) diff --git a/include/code.h b/include/code.h index c9019b9..f1bc6c8 100644 --- a/include/code.h +++ b/include/code.h @@ -1,7 +1,6 @@ #include #include #include -#include "types.h" #include "operations.h" #include "memory.h" #include "branching.h" @@ -46,33 +45,35 @@ public: { if (vec.size() == 3) { - if (vec[0] == "push") + switch (determineTwoArgumentCommand(vec[0])) { + case PUSH: file << translatePush(filename, determineSegment(vec[1]), stoi(vec[2])); - } - if (vec[0] == "pop") - { + break; + case POP: + default: file << translatePop(filename, determineSegment(vec[1]), stoi(vec[2])); } } else if (vec.size() == 2) { - if (vec[0] == "label") + switch (determineOneArgumentCommand(vec[0])) { + case LABEL: file << translateLabel(vec[1]); - } - if (vec[0] == "goto") - { + break; + case GOTO: file << translateGoto(vec[1]); - } - if (vec[0] == "if-goto") - { + break; + case IFGOTO: + default: file << translateIfGoto(vec[1]); + break; } } else if (vec.size() == 1) { - switch (determineOperation(vec[0])) + switch (determineNoArgumentCommand(vec[0])) { case ADD: file << translateAdd(); diff --git a/include/operations.h b/include/operations.h index 8b54abb..acd70e4 100644 --- a/include/operations.h +++ b/include/operations.h @@ -5,30 +5,6 @@ using namespace std; -Operation determineOperation(string operation) -{ - if (operation == "add") - return Operation::ADD; - if (operation == "sub") - return Operation::SUB; - if (operation == "neg") - return Operation::NEG; - if (operation == "eq") - return Operation::EQ; - if (operation == "gt") - return Operation::GT; - if (operation == "lt") - return Operation::LT; - if (operation == "and") - return Operation::AND; - if (operation == "or") - return Operation::OR; - if (operation == "not") - return Operation::NOT; - - return Operation::NEG; -} - string translateAdd() { stringstream output; diff --git a/include/parser.h b/include/parser.h index 75b0a19..4073a5f 100644 --- a/include/parser.h +++ b/include/parser.h @@ -4,6 +4,7 @@ #include #include #include +#include "types.h" #include "utils.h" using namespace std; diff --git a/include/types.h b/include/types.h index 192cf41..72931e3 100644 --- a/include/types.h +++ b/include/types.h @@ -10,8 +10,7 @@ enum Segment POINTER, }; -enum Operation -{ +enum NoArgumentCommand { ADD, SUB, NEG, @@ -22,3 +21,14 @@ enum Operation OR, NOT }; + +enum OneArgumentCommand { + LABEL, + GOTO, + IFGOTO, +}; + +enum TwoArgumentCommand { + PUSH, + POP +}; diff --git a/include/utils.h b/include/utils.h index 48bbcff..49e071e 100644 --- a/include/utils.h +++ b/include/utils.h @@ -33,4 +33,50 @@ string generateRandomLabel() { } return label; -} \ No newline at end of file +} + +NoArgumentCommand determineNoArgumentCommand(string command) +{ + if (command == "add") + return NoArgumentCommand::ADD; + if (command == "sub") + return NoArgumentCommand::SUB; + if (command == "neg") + return NoArgumentCommand::NEG; + if (command == "eq") + return NoArgumentCommand::EQ; + if (command == "gt") + return NoArgumentCommand::GT; + if (command == "lt") + return NoArgumentCommand::LT; + if (command == "and") + return NoArgumentCommand::AND; + if (command == "or") + return NoArgumentCommand::OR; + if (command == "not") + return NoArgumentCommand::NOT; + + return NoArgumentCommand::NEG; +} + +OneArgumentCommand determineOneArgumentCommand(string command) +{ + if (command == "label") + return OneArgumentCommand::LABEL; + if (command == "goto") + return OneArgumentCommand::GOTO; + if (command == "if-goto") + return OneArgumentCommand::IFGOTO; + + return OneArgumentCommand::IFGOTO; +} + +TwoArgumentCommand determineTwoArgumentCommand(string command) +{ + if (command == "push") + return TwoArgumentCommand::PUSH; + if (command == "pop") + return TwoArgumentCommand::POP; + + return TwoArgumentCommand::POP; +}