mirror of
https://github.com/hazemKrimi/jack-vm-translator.git
synced 2026-05-01 18:00:27 +00:00
Translate method refactoring
This commit is contained in:
+14
-13
@@ -1,7 +1,6 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <regex>
|
||||
#include <fstream>
|
||||
#include <cctype>
|
||||
#include "types.h"
|
||||
#include "utils.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
+12
-2
@@ -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
|
||||
};
|
||||
|
||||
+47
-1
@@ -33,4 +33,50 @@ string generateRandomLabel() {
|
||||
}
|
||||
|
||||
return label;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user