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 <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "types.h"
|
|
||||||
#include "operations.h"
|
#include "operations.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "branching.h"
|
#include "branching.h"
|
||||||
@@ -46,33 +45,35 @@ public:
|
|||||||
{
|
{
|
||||||
if (vec.size() == 3)
|
if (vec.size() == 3)
|
||||||
{
|
{
|
||||||
if (vec[0] == "push")
|
switch (determineTwoArgumentCommand(vec[0]))
|
||||||
{
|
{
|
||||||
|
case PUSH:
|
||||||
file << translatePush(filename, determineSegment(vec[1]), stoi(vec[2]));
|
file << translatePush(filename, determineSegment(vec[1]), stoi(vec[2]));
|
||||||
}
|
break;
|
||||||
if (vec[0] == "pop")
|
case POP:
|
||||||
{
|
default:
|
||||||
file << translatePop(filename, determineSegment(vec[1]), stoi(vec[2]));
|
file << translatePop(filename, determineSegment(vec[1]), stoi(vec[2]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (vec.size() == 2)
|
else if (vec.size() == 2)
|
||||||
{
|
{
|
||||||
if (vec[0] == "label")
|
switch (determineOneArgumentCommand(vec[0]))
|
||||||
{
|
{
|
||||||
|
case LABEL:
|
||||||
file << translateLabel(vec[1]);
|
file << translateLabel(vec[1]);
|
||||||
}
|
break;
|
||||||
if (vec[0] == "goto")
|
case GOTO:
|
||||||
{
|
|
||||||
file << translateGoto(vec[1]);
|
file << translateGoto(vec[1]);
|
||||||
}
|
break;
|
||||||
if (vec[0] == "if-goto")
|
case IFGOTO:
|
||||||
{
|
default:
|
||||||
file << translateIfGoto(vec[1]);
|
file << translateIfGoto(vec[1]);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (vec.size() == 1)
|
else if (vec.size() == 1)
|
||||||
{
|
{
|
||||||
switch (determineOperation(vec[0]))
|
switch (determineNoArgumentCommand(vec[0]))
|
||||||
{
|
{
|
||||||
case ADD:
|
case ADD:
|
||||||
file << translateAdd();
|
file << translateAdd();
|
||||||
|
|||||||
@@ -5,30 +5,6 @@
|
|||||||
|
|
||||||
using namespace std;
|
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()
|
string translateAdd()
|
||||||
{
|
{
|
||||||
stringstream output;
|
stringstream output;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <regex>
|
#include <regex>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
|
#include "types.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|||||||
+12
-2
@@ -10,8 +10,7 @@ enum Segment
|
|||||||
POINTER,
|
POINTER,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum Operation
|
enum NoArgumentCommand {
|
||||||
{
|
|
||||||
ADD,
|
ADD,
|
||||||
SUB,
|
SUB,
|
||||||
NEG,
|
NEG,
|
||||||
@@ -22,3 +21,14 @@ enum Operation
|
|||||||
OR,
|
OR,
|
||||||
NOT
|
NOT
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum OneArgumentCommand {
|
||||||
|
LABEL,
|
||||||
|
GOTO,
|
||||||
|
IFGOTO,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum TwoArgumentCommand {
|
||||||
|
PUSH,
|
||||||
|
POP
|
||||||
|
};
|
||||||
|
|||||||
+47
-1
@@ -33,4 +33,50 @@ string generateRandomLabel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return label;
|
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