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
+39
View File
@@ -0,0 +1,39 @@
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string translateLabel(string label)
{
stringstream output;
output << "(" << label << ")" << endl;
return output.str();
}
string translateGoto(string label)
{
stringstream output;
output << "@" << label << endl;
output << "0;JMP" << endl;
return output.str();
}
string translateIfGoto(string label)
{
stringstream output;
output << "@SP" << endl;
output << "M=M-1" << endl;
output << "A=M" << endl;
output << "D=M" << endl;
output << "@" << label << endl;
output << "D;JNE" << endl;
return output.str();
}
+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();
}
};
+14 -5
View File
@@ -4,7 +4,7 @@
#include <regex>
#include <fstream>
#include <cctype>
#include <vector>
#include "utils.h"
using namespace std;
@@ -37,17 +37,25 @@ private:
if (!isEmptyLine(matched[1]))
{
string command = matched[1];
trim(command);
vmCode.append(command + '\n');
}
continue;
}
else
{
trim(text);
vmCode.append(text + '\n');
}
}
}
void closeFile()
{
file.close();
}
public:
Parser(string path)
{
@@ -73,6 +81,11 @@ public:
matchedVector.push_back(matched[2]);
matchedVector.push_back(matched[3]);
}
else if (regex_search(text, matched, regex("^(.*) (.*)")))
{
matchedVector.push_back(matched[1]);
matchedVector.push_back(matched[2]);
}
else if (regex_search(text, matched, regex("^(.*)")))
{
matchedVector.push_back(matched[1]);
@@ -82,8 +95,4 @@ public:
return commands;
}
void closeFile() {
file.close();
}
};
+17
View File
@@ -5,6 +5,23 @@
using namespace std;
inline void ltrim(string &str) {
str.erase(str.begin(), find_if(str.begin(), str.end(), [](unsigned char ch) {
return !isspace(ch);
}));
}
inline void rtrim(string &str) {
str.erase(find_if(str.rbegin(), str.rend(), [](unsigned char ch) {
return !isspace(ch);
}).base(), str.end());
}
inline void trim(string &str) {
rtrim(str);
ltrim(str);
}
string generateRandomLabel() {
random_device rd;
mt19937 gen(rd());
-2
View File
@@ -35,7 +35,5 @@ int main(int argc, char* argv[])
code.translate();
parser.closeFile();
code.closeFile();
return 0;
}