feat: code translation scaffolding

This commit is contained in:
2026-03-30 15:59:50 +01:00
parent 8888f7b97a
commit d01abac9e9
4 changed files with 28 additions and 2 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ endif
TARGET_NAME = vm-translator
OUT_DIR = out
TARGET = $(OUT_DIR)/$(TARGET_NAME)
SRCS = src/main.cpp src/utils.cpp src/parser.cpp
SRCS = src/main.cpp src/utils.cpp src/code.cpp src/parser.cpp
OBJS = $(SRCS:src/%.cpp=$(OUT_DIR)/%.o)
$(TARGET): $(OBJS)
+11
View File
@@ -0,0 +1,11 @@
#include "types.hpp"
#include <iostream>
#include <sstream>
int translateCommand(std::string &output, Command cmd) {
std::ostringstream stream;
stream << "// " << cmd.line << std::endl;
output = stream.str();
return 0;
}
+3
View File
@@ -0,0 +1,3 @@
#include "types.hpp"
int translateCommand(std::string&, Command);
+13 -1
View File
@@ -4,12 +4,16 @@
#include <string>
#include <vector>
#include "code.hpp"
#include "parser.hpp"
#include "utils.hpp"
int process(std::string source) {
std::ifstream ifs(source);
std::string out = source.substr(0, source.find_last_of('.')) + ".asm";
std::ofstream ofs(out);
std::string line;
std::string output;
std::vector<Command> commands;
@@ -25,9 +29,17 @@ int process(std::string source) {
}
for (const Command &cmd : commands) {
std::cout << int(cmd.type) << " " << int(cmd.segment) << " " << cmd.index << std::endl;
int translateResult;
if ((translateResult = translateCommand(output, cmd)) != 0) {
return translateResult;
}
ofs << output;
}
ifs.close();
ofs.close();
return 0;
}