Implement branching

This commit is contained in:
Hazem Krimi
2024-05-02 21:26:14 +01:00
parent 8262f49d85
commit 2040a363e3
5 changed files with 97 additions and 14 deletions
+26 -6
View File
@@ -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();
}
};