mirror of
https://github.com/hazemKrimi/jack-vm-translator.git
synced 2026-05-01 18:00:27 +00:00
Implement branching
This commit is contained in:
+26
-6
@@ -2,9 +2,9 @@
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include "types.h"
|
||||
#include "utils.h"
|
||||
#include "operations.h"
|
||||
#include "memory.h"
|
||||
#include "branching.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -15,6 +15,11 @@ private:
|
||||
string filename;
|
||||
vector<vector<string>> commands;
|
||||
|
||||
void closeFile()
|
||||
{
|
||||
file.close();
|
||||
}
|
||||
|
||||
public:
|
||||
Code(string path, vector<vector<string>> tokens)
|
||||
{
|
||||
@@ -30,6 +35,11 @@ public:
|
||||
commands = tokens;
|
||||
}
|
||||
|
||||
~Code()
|
||||
{
|
||||
closeFile();
|
||||
}
|
||||
|
||||
void translate()
|
||||
{
|
||||
for (const vector<string> &vec : commands)
|
||||
@@ -45,6 +55,21 @@ public:
|
||||
file << translatePop(filename, determineSegment(vec[1]), stoi(vec[2]));
|
||||
}
|
||||
}
|
||||
else if (vec.size() == 2)
|
||||
{
|
||||
if (vec[0] == "label")
|
||||
{
|
||||
file << translateLabel(vec[1]);
|
||||
}
|
||||
if (vec[0] == "goto")
|
||||
{
|
||||
file << translateGoto(vec[1]);
|
||||
}
|
||||
if (vec[0] == "if-goto")
|
||||
{
|
||||
file << translateIfGoto(vec[1]);
|
||||
}
|
||||
}
|
||||
else if (vec.size() == 1)
|
||||
{
|
||||
switch (determineOperation(vec[0]))
|
||||
@@ -83,9 +108,4 @@ public:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void closeFile()
|
||||
{
|
||||
file.close();
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user