mirror of
https://github.com/hazemKrimi/jack-vm-translator.git
synced 2026-05-02 02:10:27 +00:00
Fixing functions logic wip
This commit is contained in:
+10
-2
@@ -23,7 +23,7 @@ private:
|
||||
}
|
||||
|
||||
public:
|
||||
Code(string path, vector<vector<string>> tokens)
|
||||
Code(string path, vector<vector<string>> tokens, bool isNew)
|
||||
{
|
||||
size_t slashIndex = path.find_last_of('/');
|
||||
size_t dotIndex = path.find_last_of('.');
|
||||
@@ -33,8 +33,16 @@ public:
|
||||
filename = path.substr(slashIndex + 1, dotIndex - slashIndex - 1);
|
||||
}
|
||||
|
||||
file = ofstream(path);
|
||||
file = ofstream(path, isNew ? ios_base::out : ios_base::app);
|
||||
commands = tokens;
|
||||
|
||||
if (isNew) {
|
||||
file << "@256" << endl;
|
||||
file << "D=A" << endl;
|
||||
file << "@SP" << endl;
|
||||
file << "M=D" << endl;
|
||||
file << translateCall("Sys.init", 0);
|
||||
}
|
||||
}
|
||||
|
||||
~Code()
|
||||
|
||||
+45
-20
@@ -4,12 +4,14 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
string translateFunction(string name, int args) {
|
||||
string translateFunction(string name, int args)
|
||||
{
|
||||
stringstream output;
|
||||
|
||||
output << "(" << name << ")" << endl;
|
||||
|
||||
for (int i = 0; i < args; ++i) {
|
||||
for (int i = 0; i < args; i++)
|
||||
{
|
||||
output << "@0" << endl;
|
||||
output << "D=A" << endl;
|
||||
output << "@SP" << endl;
|
||||
@@ -22,10 +24,10 @@ string translateFunction(string name, int args) {
|
||||
return output.str();
|
||||
}
|
||||
|
||||
string translateCall(string name, int args) {
|
||||
string translateCall(string name, int args)
|
||||
{
|
||||
stringstream output;
|
||||
string label = name + "$ret" + generateRandomLabel(3);
|
||||
vector<string> frame = { "LCL", "ARG", "THIS", "THAT" };
|
||||
|
||||
output << "@" << label << endl;
|
||||
output << "D=A" << endl;
|
||||
@@ -35,20 +37,38 @@ string translateCall(string name, int args) {
|
||||
output << "@SP" << endl;
|
||||
output << "M=M+1" << endl;
|
||||
|
||||
for (const string &segment : frame) {
|
||||
output << "@" << segment << endl;
|
||||
output << "D=M" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "A=M" << endl;
|
||||
output << "M=D" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "M=M+1" << endl;
|
||||
}
|
||||
|
||||
output << "@SP" << endl;
|
||||
output << "D=M" << endl;
|
||||
output << "@LCL" << endl;
|
||||
output << "D=M" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "A=M" << endl;
|
||||
output << "M=D" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "M=M+1" << endl;
|
||||
|
||||
output << "@ARG" << endl;
|
||||
output << "D=M" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "A=M" << endl;
|
||||
output << "M=D" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "M=M+1" << endl;
|
||||
|
||||
output << "@THIS" << endl;
|
||||
output << "D=M" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "A=M" << endl;
|
||||
output << "M=D" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "M=M+1" << endl;
|
||||
|
||||
output << "@THAT" << endl;
|
||||
output << "D=M" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "A=M" << endl;
|
||||
output << "M=D" << endl;
|
||||
output << "@SP" << endl;
|
||||
output << "M=M+1" << endl;
|
||||
|
||||
output << "@SP" << endl;
|
||||
output << "D=M" << endl;
|
||||
output << "@R" << endl;
|
||||
@@ -64,7 +84,11 @@ string translateCall(string name, int args) {
|
||||
output << "D=M" << endl;
|
||||
output << "@ARG" << endl;
|
||||
output << "M=D" << endl;
|
||||
output << "@R" << endl;
|
||||
|
||||
output << "@SP" << endl;
|
||||
output << "D=M" << endl;
|
||||
output << "@LCL" << endl;
|
||||
output << "M=D" << endl;
|
||||
|
||||
output << "@" << name << endl;
|
||||
output << "0;JMP" << endl;
|
||||
@@ -73,7 +97,8 @@ string translateCall(string name, int args) {
|
||||
return output.str();
|
||||
}
|
||||
|
||||
string translateReturn() {
|
||||
string translateReturn()
|
||||
{
|
||||
stringstream output;
|
||||
string cleanupLabel = "cleanup$ret" + generateRandomLabel(3);
|
||||
string endLabel = "end$ret" + generateRandomLabel(3);
|
||||
@@ -119,7 +144,7 @@ string translateReturn() {
|
||||
output << "D=M" << endl;
|
||||
output << "@THAT" << endl;
|
||||
output << "M=D" << endl;
|
||||
|
||||
|
||||
output << "@END_FRAME" << endl;
|
||||
output << "D=M" << endl;
|
||||
output << "@2" << endl;
|
||||
@@ -128,7 +153,7 @@ string translateReturn() {
|
||||
output << "D=M" << endl;
|
||||
output << "@THIS" << endl;
|
||||
output << "M=D" << endl;
|
||||
|
||||
|
||||
output << "@END_FRAME" << endl;
|
||||
output << "D=M" << endl;
|
||||
output << "@3" << endl;
|
||||
|
||||
Reference in New Issue
Block a user