mirror of
https://github.com/hazemKrimi/jack-vm-translator.git
synced 2026-05-01 18:00:27 +00:00
Remove src
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include "types.h"
|
||||
#include "utils.h"
|
||||
#include "operations.h"
|
||||
#include "memory.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Code
|
||||
{
|
||||
private:
|
||||
ofstream file;
|
||||
string filename;
|
||||
vector<vector<string>> commands;
|
||||
|
||||
public:
|
||||
Code(string path, vector<vector<string>> tokens)
|
||||
{
|
||||
size_t slashIndex = path.find_last_of('/');
|
||||
size_t dotIndex = path.find_last_of('.');
|
||||
|
||||
if (slashIndex != string::npos && dotIndex != string::npos)
|
||||
{
|
||||
filename = path.substr(slashIndex + 1, dotIndex - slashIndex - 1);
|
||||
}
|
||||
|
||||
file = ofstream(path);
|
||||
commands = tokens;
|
||||
}
|
||||
|
||||
void translate()
|
||||
{
|
||||
for (const vector<string> &vec : commands)
|
||||
{
|
||||
if (vec.size() == 3)
|
||||
{
|
||||
if (vec[0] == "push")
|
||||
{
|
||||
file << translatePush(filename, determineSegment(vec[1]), stoi(vec[2]));
|
||||
}
|
||||
if (vec[0] == "pop")
|
||||
{
|
||||
file << translatePop(filename, determineSegment(vec[1]), stoi(vec[2]));
|
||||
}
|
||||
}
|
||||
else if (vec.size() == 1)
|
||||
{
|
||||
switch (determineOperation(vec[0]))
|
||||
{
|
||||
case ADD:
|
||||
file << translateAdd();
|
||||
break;
|
||||
case SUB:
|
||||
file << translateSub();
|
||||
break;
|
||||
case NEG:
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void closeFile()
|
||||
{
|
||||
file.close();
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,161 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
Segment determineSegment(string segment)
|
||||
{
|
||||
if (segment == "local")
|
||||
return Segment::LCL;
|
||||
if (segment == "argument")
|
||||
return Segment::ARG;
|
||||
if (segment == "this")
|
||||
return Segment::THIS;
|
||||
if (segment == "that")
|
||||
return Segment::THAT;
|
||||
if (segment == "pointer")
|
||||
return Segment::POINTER;
|
||||
if (segment == "static")
|
||||
return Segment::STATIC;
|
||||
if (segment == "temp")
|
||||
return Segment::TEMP;
|
||||
if (segment == "constant")
|
||||
return Segment::CONSTANT;
|
||||
|
||||
return Segment::CONSTANT;
|
||||
}
|
||||
|
||||
string translatePush(string filename, Segment segment, int index)
|
||||
{
|
||||
stringstream output;
|
||||
|
||||
switch (segment)
|
||||
{
|
||||
case LCL:
|
||||
output << "@" << index << endl;
|
||||
output << "D=A" << endl;
|
||||
output << "@LCL" << endl;
|
||||
output << "A=D+M" << endl;
|
||||
output << "D=M" << endl;
|
||||
break;
|
||||
case ARG:
|
||||
output << "@" << index << endl;
|
||||
output << "D=A" << endl;
|
||||
output << "@ARG" << endl;
|
||||
output << "A=D+M" << endl;
|
||||
output << "D=M" << endl;
|
||||
break;
|
||||
case THIS:
|
||||
output << "@" << index << endl;
|
||||
output << "D=A" << endl;
|
||||
output << "@THIS" << endl;
|
||||
output << "A=D+M" << endl;
|
||||
output << "D=M" << endl;
|
||||
break;
|
||||
case THAT:
|
||||
output << "@" << index << endl;
|
||||
output << "D=A" << endl;
|
||||
output << "@THAT" << endl;
|
||||
output << "A=D+M" << endl;
|
||||
output << "D=M" << endl;
|
||||
break;
|
||||
case POINTER:
|
||||
output << (index == 0 ? "@THIS" : "@THAT") << endl;
|
||||
output << "D=M" << endl;
|
||||
break;
|
||||
case STATIC:
|
||||
output << "@" << filename << "." << index << endl;
|
||||
output << "D=M" << endl;
|
||||
break;
|
||||
case TEMP:
|
||||
output << "@" << index + 5 << endl;
|
||||
output << "D=M" << endl;
|
||||
break;
|
||||
case CONSTANT:
|
||||
default:
|
||||
output << '@' << index << endl;
|
||||
output << "D=A" << endl;
|
||||
break;
|
||||
}
|
||||
|
||||
output << "@SP" << endl;
|
||||
output << "A=M" << endl;
|
||||
output << "M=D" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "M=M+1" << endl;
|
||||
|
||||
return output.str();
|
||||
}
|
||||
|
||||
string translatePop(string filename, Segment segment, int index)
|
||||
{
|
||||
stringstream output;
|
||||
|
||||
switch (segment)
|
||||
{
|
||||
case LCL:
|
||||
output << "@" << index << endl;
|
||||
output << "D=A" << endl;
|
||||
output << "@LCL" << endl;
|
||||
output << "D=D+M" << endl;
|
||||
output << "@ADDR" << endl;
|
||||
output << "M=D" << endl;
|
||||
break;
|
||||
case ARG:
|
||||
output << "@" << index << endl;
|
||||
output << "D=A" << endl;
|
||||
output << "@ARG" << endl;
|
||||
output << "D=D+M" << endl;
|
||||
output << "@ADDR" << endl;
|
||||
output << "M=D" << endl;
|
||||
break;
|
||||
case THIS:
|
||||
output << "@" << index << endl;
|
||||
output << "D=A" << endl;
|
||||
output << "@THIS" << endl;
|
||||
output << "D=D+M" << endl;
|
||||
output << "@ADDR" << endl;
|
||||
output << "M=D" << endl;
|
||||
break;
|
||||
case THAT:
|
||||
output << "@" << index << endl;
|
||||
output << "D=A" << endl;
|
||||
output << "@THAT" << 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 << "M=D" << endl;
|
||||
break;
|
||||
case STATIC:
|
||||
output << "@" << filename << "." << index << endl;
|
||||
output << "D=A" << endl;
|
||||
output << "@ADDR" << endl;
|
||||
output << "M=D" << endl;
|
||||
break;
|
||||
case TEMP:
|
||||
output << "@" << index + 5 << endl;
|
||||
output << "D=A" << endl;
|
||||
output << "@ADDR" << endl;
|
||||
output << "M=D" << endl;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
output << "@SP" << endl;
|
||||
output << "M=M-1" << endl;
|
||||
output << "A=M" << endl;
|
||||
output << "D=M" << endl;
|
||||
output << "@ADDR" << endl;
|
||||
output << "A=M" << endl;
|
||||
output << "M=D" << endl;
|
||||
|
||||
return output.str();
|
||||
}
|
||||
@@ -0,0 +1,352 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <ctime>
|
||||
|
||||
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;
|
||||
|
||||
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 translateSub()
|
||||
{
|
||||
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 translateNeg()
|
||||
{
|
||||
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 << "M=M-D" << endl;
|
||||
output << "M=M-D" << 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 translateEq()
|
||||
{
|
||||
stringstream output;
|
||||
|
||||
srand(static_cast<unsigned int>(time(nullptr)));
|
||||
|
||||
string LABEL = generateRandomLabel();
|
||||
|
||||
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 << "@" << LABEL << "_TRUE" << endl;
|
||||
output << "D;JEQ" << endl;
|
||||
output << "@" << LABEL << "_FALSE" << endl;
|
||||
output << "D;JNE" << endl;
|
||||
|
||||
output << "(" << LABEL << "_TRUE)" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "A=M" << endl;
|
||||
output << "M=-1" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "M=M+1" << endl;
|
||||
output << "@" << LABEL << endl;
|
||||
output << "0;JMP" << endl;
|
||||
|
||||
output << "(" << LABEL << "_FALSE)" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "A=M" << endl;
|
||||
output << "M=0" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "M=M+1" << endl;
|
||||
output << "@" << LABEL << endl;
|
||||
output << "0;JMP" << endl;
|
||||
|
||||
output << "(" << LABEL << ")" << endl;
|
||||
|
||||
return output.str();
|
||||
}
|
||||
|
||||
string translateGt()
|
||||
{
|
||||
stringstream output;
|
||||
|
||||
srand(static_cast<unsigned int>(time(nullptr)));
|
||||
|
||||
string LABEL = generateRandomLabel();
|
||||
|
||||
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 << "@" << LABEL << "_TRUE" << endl;
|
||||
output << "D;JGT" << endl;
|
||||
output << "@" << LABEL << "_FALSE" << endl;
|
||||
output << "D;JLT" << endl;
|
||||
output << "@" << LABEL << "_FALSE" << endl;
|
||||
output << "D;JEQ" << endl;
|
||||
|
||||
output << "(" << LABEL << "_TRUE)" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "A=M" << endl;
|
||||
output << "M=-1" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "M=M+1" << endl;
|
||||
output << "@" << LABEL << endl;
|
||||
output << "0;JMP" << endl;
|
||||
|
||||
output << "(" << LABEL << "_FALSE)" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "A=M" << endl;
|
||||
output << "M=0" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "M=M+1" << endl;
|
||||
output << "@" << LABEL << endl;
|
||||
output << "0;JMP" << endl;
|
||||
|
||||
output << "(" << LABEL << ")" << endl;
|
||||
|
||||
return output.str();
|
||||
}
|
||||
|
||||
string translateLt()
|
||||
{
|
||||
stringstream output;
|
||||
|
||||
srand(static_cast<unsigned int>(time(nullptr)));
|
||||
|
||||
string LABEL = generateRandomLabel();
|
||||
|
||||
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 << "@" << LABEL << "_TRUE" << endl;
|
||||
output << "D;JLT" << endl;
|
||||
output << "@" << LABEL << "_FALSE" << endl;
|
||||
output << "D;JGT" << endl;
|
||||
output << "@" << LABEL << "_FALSE" << endl;
|
||||
output << "D;JEQ" << endl;
|
||||
|
||||
output << "(" << LABEL << "_TRUE)" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "A=M" << endl;
|
||||
output << "M=-1" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "M=M+1" << endl;
|
||||
output << "@" << LABEL << endl;
|
||||
output << "0;JMP" << endl;
|
||||
|
||||
output << "(" << LABEL << "_FALSE)" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "A=M" << endl;
|
||||
output << "M=0" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "M=M+1" << endl;
|
||||
output << "@" << LABEL << endl;
|
||||
output << "0;JMP" << endl;
|
||||
|
||||
output << "(" << LABEL << ")" << 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();
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <regex>
|
||||
#include <fstream>
|
||||
#include <cctype>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Parser
|
||||
{
|
||||
private:
|
||||
ifstream file;
|
||||
string vmCode;
|
||||
|
||||
bool isEmptyLine(string text)
|
||||
{
|
||||
for (char c : text)
|
||||
{
|
||||
if (!isspace(c))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void removeCommentsAndWhitespace()
|
||||
{
|
||||
string text;
|
||||
smatch matched;
|
||||
|
||||
while (getline(file, text))
|
||||
{
|
||||
if (regex_search(text, matched, regex("^(.*)?(\\/\\/.*)")) || isEmptyLine(text))
|
||||
{
|
||||
if (!isEmptyLine(matched[1]))
|
||||
{
|
||||
string command = matched[1];
|
||||
vmCode.append(command + '\n');
|
||||
}
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
vmCode.append(text + '\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
Parser(string path)
|
||||
{
|
||||
file = ifstream(path);
|
||||
|
||||
removeCommentsAndWhitespace();
|
||||
}
|
||||
|
||||
vector<vector<string>> getCommands()
|
||||
{
|
||||
stringstream vmCodeStream(vmCode);
|
||||
string text;
|
||||
smatch matched;
|
||||
vector<vector<string>> commands;
|
||||
|
||||
while (getline(vmCodeStream, text, '\n'))
|
||||
{
|
||||
vector<string> matchedVector;
|
||||
|
||||
if (regex_search(text, matched, regex("^(.*) (.*) (.*)")))
|
||||
{
|
||||
matchedVector.push_back(matched[1]);
|
||||
matchedVector.push_back(matched[2]);
|
||||
matchedVector.push_back(matched[3]);
|
||||
}
|
||||
else if (regex_search(text, matched, regex("^(.*)")))
|
||||
{
|
||||
matchedVector.push_back(matched[1]);
|
||||
}
|
||||
commands.push_back(matchedVector);
|
||||
}
|
||||
|
||||
return commands;
|
||||
}
|
||||
|
||||
void closeFile() {
|
||||
file.close();
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
enum Segment
|
||||
{
|
||||
LCL,
|
||||
ARG,
|
||||
THIS,
|
||||
THAT,
|
||||
STATIC,
|
||||
CONSTANT,
|
||||
TEMP,
|
||||
POINTER,
|
||||
};
|
||||
|
||||
enum Operation
|
||||
{
|
||||
ADD,
|
||||
SUB,
|
||||
NEG,
|
||||
EQ,
|
||||
GT,
|
||||
LT,
|
||||
AND,
|
||||
OR,
|
||||
NOT
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <ctime>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string generateRandomLabel() {
|
||||
random_device rd;
|
||||
mt19937 gen(rd());
|
||||
uniform_int_distribution<> dis('A', 'Z');
|
||||
string label;
|
||||
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
label += static_cast<char>(dis(gen));
|
||||
}
|
||||
|
||||
return label;
|
||||
}
|
||||
Reference in New Issue
Block a user