mirror of
https://github.com/hazemKrimi/jack-vm-translator.git
synced 2026-05-01 18:00:27 +00:00
Add comparison operations wip
This commit is contained in:
+30
-5
@@ -46,14 +46,39 @@ public:
|
|||||||
}
|
}
|
||||||
else if (vec.size() == 1)
|
else if (vec.size() == 1)
|
||||||
{
|
{
|
||||||
if (vec.at(0) == "add")
|
switch (determineOperation(vec[0]))
|
||||||
|
{
|
||||||
|
case ADD:
|
||||||
file << translateAdd();
|
file << translateAdd();
|
||||||
|
break;
|
||||||
if (vec.at(0) == "sub")
|
case SUB:
|
||||||
file << translateSub();
|
file << translateSub();
|
||||||
|
break;
|
||||||
if (vec.at(0) == "neg")
|
case NEG:
|
||||||
file << translateNeg();
|
file << translateNeg();
|
||||||
|
break;
|
||||||
|
case EQ:
|
||||||
|
file << translateEq();
|
||||||
|
break;
|
||||||
|
case GT:
|
||||||
|
file << translateGt();
|
||||||
|
break;
|
||||||
|
case LT:
|
||||||
|
file << translateLt();
|
||||||
|
break;
|
||||||
|
case AND:
|
||||||
|
file << translateAnd();
|
||||||
|
break;
|
||||||
|
case OR:
|
||||||
|
file << translateOr();
|
||||||
|
break;
|
||||||
|
case NOT:
|
||||||
|
file << translateNot();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
file << translateNeg();
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
file << endl;
|
file << endl;
|
||||||
|
|||||||
+20
-8
@@ -14,6 +14,8 @@ Segment determineSegment(string segment)
|
|||||||
return Segment::THIS;
|
return Segment::THIS;
|
||||||
if (segment == "that")
|
if (segment == "that")
|
||||||
return Segment::THAT;
|
return Segment::THAT;
|
||||||
|
if (segment == "pointer")
|
||||||
|
return Segment::POINTER;
|
||||||
if (segment == "static")
|
if (segment == "static")
|
||||||
return Segment::STATIC;
|
return Segment::STATIC;
|
||||||
if (segment == "temp")
|
if (segment == "temp")
|
||||||
@@ -34,28 +36,32 @@ string translatePush(string filename, Segment segment, int index)
|
|||||||
output << "@" << index << endl;
|
output << "@" << index << endl;
|
||||||
output << "D=A" << endl;
|
output << "D=A" << endl;
|
||||||
output << "@LCL" << endl;
|
output << "@LCL" << endl;
|
||||||
output << "A=D+A" << endl;
|
output << "A=D+M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
break;
|
break;
|
||||||
case ARG:
|
case ARG:
|
||||||
output << "@" << index << endl;
|
output << "@" << index << endl;
|
||||||
output << "D=A" << endl;
|
output << "D=A" << endl;
|
||||||
output << "@ARG" << endl;
|
output << "@ARG" << endl;
|
||||||
output << "A=D+A" << endl;
|
output << "A=D+M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
break;
|
break;
|
||||||
case THIS:
|
case THIS:
|
||||||
output << "@" << index << endl;
|
output << "@" << index << endl;
|
||||||
output << "D=A" << endl;
|
output << "D=A" << endl;
|
||||||
output << "@THIS" << endl;
|
output << "@THIS" << endl;
|
||||||
output << "A=D+A" << endl;
|
output << "A=D+M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
break;
|
break;
|
||||||
case THAT:
|
case THAT:
|
||||||
output << "@" << index << endl;
|
output << "@" << index << endl;
|
||||||
output << "D=A" << endl;
|
output << "D=A" << endl;
|
||||||
output << "@THAT" << endl;
|
output << "@THAT" << endl;
|
||||||
output << "A=D+A" << endl;
|
output << "A=D+M" << endl;
|
||||||
|
output << "D=M" << endl;
|
||||||
|
break;
|
||||||
|
case POINTER:
|
||||||
|
output << (index == 0 ? "@THIS" : "@THAT") << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
break;
|
break;
|
||||||
case STATIC:
|
case STATIC:
|
||||||
@@ -92,7 +98,7 @@ string translatePop(string filename, Segment segment, int index)
|
|||||||
output << "@" << index << endl;
|
output << "@" << index << endl;
|
||||||
output << "D=A" << endl;
|
output << "D=A" << endl;
|
||||||
output << "@LCL" << endl;
|
output << "@LCL" << endl;
|
||||||
output << "D=D+A" << endl;
|
output << "D=D+M" << endl;
|
||||||
output << "@ADDR" << endl;
|
output << "@ADDR" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
break;
|
break;
|
||||||
@@ -100,7 +106,7 @@ string translatePop(string filename, Segment segment, int index)
|
|||||||
output << "@" << index << endl;
|
output << "@" << index << endl;
|
||||||
output << "D=A" << endl;
|
output << "D=A" << endl;
|
||||||
output << "@ARG" << endl;
|
output << "@ARG" << endl;
|
||||||
output << "D=D+A" << endl;
|
output << "D=D+M" << endl;
|
||||||
output << "@ADDR" << endl;
|
output << "@ADDR" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
break;
|
break;
|
||||||
@@ -108,7 +114,7 @@ string translatePop(string filename, Segment segment, int index)
|
|||||||
output << "@" << index << endl;
|
output << "@" << index << endl;
|
||||||
output << "D=A" << endl;
|
output << "D=A" << endl;
|
||||||
output << "@THIS" << endl;
|
output << "@THIS" << endl;
|
||||||
output << "D=D+A" << endl;
|
output << "D=D+M" << endl;
|
||||||
output << "@ADDR" << endl;
|
output << "@ADDR" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
break;
|
break;
|
||||||
@@ -116,7 +122,13 @@ string translatePop(string filename, Segment segment, int index)
|
|||||||
output << "@" << index << endl;
|
output << "@" << index << endl;
|
||||||
output << "D=A" << endl;
|
output << "D=A" << endl;
|
||||||
output << "@THAT" << endl;
|
output << "@THAT" << endl;
|
||||||
output << "D=D+A" << endl;
|
output << "D=D+M" << endl;
|
||||||
|
output << "@ADDR" << endl;
|
||||||
|
output << "M=D" << endl;
|
||||||
|
break;
|
||||||
|
case POINTER:
|
||||||
|
output << (index == 0 ? "@THIS" : "@THAT") << endl;
|
||||||
|
output << "D=A" << endl;
|
||||||
output << "@ADDR" << endl;
|
output << "@ADDR" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
break;
|
break;
|
||||||
|
|||||||
+223
-10
@@ -4,17 +4,29 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// Operation determineOperation(string operation)
|
Operation determineOperation(string operation)
|
||||||
// {
|
{
|
||||||
// if (operation == "add")
|
if (operation == "add")
|
||||||
// return Operation::ADD;
|
return Operation::ADD;
|
||||||
// if (operation == "sub")
|
if (operation == "sub")
|
||||||
// return Operation::SUB;
|
return Operation::SUB;
|
||||||
// if (operation == "neg")
|
if (operation == "neg")
|
||||||
// return 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;
|
return Operation::NEG;
|
||||||
// }
|
}
|
||||||
|
|
||||||
string translateAdd()
|
string translateAdd()
|
||||||
{
|
{
|
||||||
@@ -99,3 +111,204 @@ string translateNeg()
|
|||||||
|
|
||||||
return output.str();
|
return output.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string translateEq()
|
||||||
|
{
|
||||||
|
stringstream output;
|
||||||
|
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M-1" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "D=M" << endl;
|
||||||
|
|
||||||
|
output << "@R" << endl;
|
||||||
|
output << "M=D" << endl;
|
||||||
|
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M-1" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "D=M" << endl;
|
||||||
|
|
||||||
|
output << "@R" << endl;
|
||||||
|
output << "M=M-D" << endl;
|
||||||
|
output << "@TRUE" << endl;
|
||||||
|
output << "M;JEQ" << endl;
|
||||||
|
output << "@FALSE" << endl;
|
||||||
|
output << "M;JNE" << endl;
|
||||||
|
|
||||||
|
output << "(TRUE)" << endl;
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "M=-1" << endl;
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M+1" << endl;
|
||||||
|
|
||||||
|
output << "(FALSE)" << endl;
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "M=-1" << endl;
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M+1" << endl;
|
||||||
|
|
||||||
|
return output.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
string translateGt()
|
||||||
|
{
|
||||||
|
stringstream output;
|
||||||
|
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M-1" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "D=M" << endl;
|
||||||
|
|
||||||
|
output << "@R" << endl;
|
||||||
|
output << "M=D" << endl;
|
||||||
|
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M-1" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "D=M" << endl;
|
||||||
|
|
||||||
|
output << "@R" << endl;
|
||||||
|
output << "M=M-D" << endl;
|
||||||
|
output << "@TRUE" << endl;
|
||||||
|
output << "M;JGT" << endl;
|
||||||
|
output << "@FALSE" << endl;
|
||||||
|
output << "M;JLT" << endl;
|
||||||
|
|
||||||
|
output << "(TRUE)" << endl;
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "M=-1" << endl;
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M+1" << endl;
|
||||||
|
|
||||||
|
output << "(FALSE)" << endl;
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "M=-1" << endl;
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M+1" << endl;
|
||||||
|
|
||||||
|
return output.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
string translateLt()
|
||||||
|
{
|
||||||
|
stringstream output;
|
||||||
|
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M-1" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "D=M" << endl;
|
||||||
|
|
||||||
|
output << "@R" << endl;
|
||||||
|
output << "M=D" << endl;
|
||||||
|
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M-1" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "D=M" << endl;
|
||||||
|
|
||||||
|
output << "@R" << endl;
|
||||||
|
output << "M=M-D" << endl;
|
||||||
|
output << "@TRUE" << endl;
|
||||||
|
output << "M;JLT" << endl;
|
||||||
|
output << "@FALSE" << endl;
|
||||||
|
output << "M;JGT" << endl;
|
||||||
|
|
||||||
|
output << "(TRUE)" << endl;
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "M=-1" << endl;
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M+1" << endl;
|
||||||
|
|
||||||
|
output << "(FALSE)" << endl;
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "M=-1" << endl;
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M+1" << endl;
|
||||||
|
|
||||||
|
return output.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
string translateAnd()
|
||||||
|
{
|
||||||
|
stringstream output;
|
||||||
|
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M-1" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "D=M" << endl;
|
||||||
|
|
||||||
|
output << "@R" << endl;
|
||||||
|
output << "M=D" << endl;
|
||||||
|
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M-1" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "D=M" << endl;
|
||||||
|
|
||||||
|
output << "@R" << endl;
|
||||||
|
output << "M=D&M" << endl;
|
||||||
|
output << "D=M" << endl;
|
||||||
|
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "M=D" << endl;
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M+1" << endl;
|
||||||
|
|
||||||
|
return output.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
string translateOr()
|
||||||
|
{
|
||||||
|
stringstream output;
|
||||||
|
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M-1" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "D=M" << endl;
|
||||||
|
|
||||||
|
output << "@R" << endl;
|
||||||
|
output << "M=D" << endl;
|
||||||
|
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M-1" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "D=M" << endl;
|
||||||
|
|
||||||
|
output << "@R" << endl;
|
||||||
|
output << "M=D|M" << endl;
|
||||||
|
output << "D=M" << endl;
|
||||||
|
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "M=D" << endl;
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M+1" << endl;
|
||||||
|
|
||||||
|
return output.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
string translateNot()
|
||||||
|
{
|
||||||
|
stringstream output;
|
||||||
|
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M-1" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "D=M" << endl;
|
||||||
|
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "A=M" << endl;
|
||||||
|
output << "M=!D" << endl;
|
||||||
|
output << "@SP" << endl;
|
||||||
|
output << "M=M+1" << endl;
|
||||||
|
|
||||||
|
return output.str();
|
||||||
|
}
|
||||||
|
|||||||
+13
-11
@@ -7,16 +7,18 @@ enum Segment
|
|||||||
STATIC,
|
STATIC,
|
||||||
CONSTANT,
|
CONSTANT,
|
||||||
TEMP,
|
TEMP,
|
||||||
|
POINTER,
|
||||||
};
|
};
|
||||||
|
|
||||||
// enum Operation {
|
enum Operation
|
||||||
// ADD,
|
{
|
||||||
// SUB,
|
ADD,
|
||||||
// NEG,
|
SUB,
|
||||||
// EQ,
|
NEG,
|
||||||
// GT,
|
EQ,
|
||||||
// LT,
|
GT,
|
||||||
// AND,
|
LT,
|
||||||
// OR,
|
AND,
|
||||||
// NOT
|
OR,
|
||||||
// };
|
NOT
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user