mirror of
https://github.com/hazemKrimi/jack-vm-translator.git
synced 2026-05-01 18:00:27 +00:00
feat: code translation scaffolding
This commit is contained in:
@@ -10,7 +10,7 @@ endif
|
|||||||
TARGET_NAME = vm-translator
|
TARGET_NAME = vm-translator
|
||||||
OUT_DIR = out
|
OUT_DIR = out
|
||||||
TARGET = $(OUT_DIR)/$(TARGET_NAME)
|
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)
|
OBJS = $(SRCS:src/%.cpp=$(OUT_DIR)/%.o)
|
||||||
|
|
||||||
$(TARGET): $(OBJS)
|
$(TARGET): $(OBJS)
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
#include "types.hpp"
|
||||||
|
|
||||||
|
int translateCommand(std::string&, Command);
|
||||||
+13
-1
@@ -4,12 +4,16 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "code.hpp"
|
||||||
#include "parser.hpp"
|
#include "parser.hpp"
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
|
||||||
int process(std::string source) {
|
int process(std::string source) {
|
||||||
std::ifstream ifs(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 line;
|
||||||
|
std::string output;
|
||||||
|
|
||||||
std::vector<Command> commands;
|
std::vector<Command> commands;
|
||||||
|
|
||||||
@@ -25,9 +29,17 @@ int process(std::string source) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const Command &cmd : commands) {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user