Operators wip

This commit is contained in:
Hazem Krimi
2024-04-16 19:22:09 +01:00
parent ed27ad8c3b
commit 9760124dc7
8 changed files with 363 additions and 215 deletions
+77
View File
@@ -0,0 +1,77 @@
#include <iostream>
#include <fstream>
#include <string>
#include "types.h"
#include "memory.h"
#include "arithmetic.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 auto &vec : commands)
{
if (vec.size() > 1)
{
if (vec[0] == "push")
{
file << translatePush(filename, determineSegment(vec[1]), stoi(vec[2]));
}
else
{
file << translatePop(filename, determineSegment(vec[1]), stoi(vec[2]));
}
}
if (vec.size() == 1)
{
int op = determineArithmeticOperator(vec[0]);
cout << op << " " << vec[0] << endl;
switch (determineArithmeticOperator(vec[0]))
{
case ArithmeticOperator::ADD:
file << translateAdd();
break;
case ArithmeticOperator::SUB:
file << translateSub();
break;
case ArithmeticOperator::NEG:
default:
file << translateNeg();
break;
}
}
file << endl;
}
}
void closeFile()
{
file.close();
}
};