chore: add segment names to parsed commands for easier translations

This commit is contained in:
2026-04-01 17:02:36 +01:00
parent a4e4942069
commit 51c7327a65
2 changed files with 14 additions and 5 deletions
+5 -3
View File
@@ -12,9 +12,11 @@ int parseCommand(std::vector<Command> &commands, std::string line) {
Command cmd; Command cmd;
if (matched.ready()) { if (matched.ready()) {
std::string segment = matched[2];
cmd.line = line; cmd.line = line;
cmd.type = commandTypes.at(matched[1]); cmd.commandType = commandTypes.at(matched[1]);
cmd.segment = segmentTypes.at(matched[2]); cmd.segmentType = segmentTypes.at(segment);
cmd.segmentName = segmentNames.at(segment);
cmd.index = std::stoi(std::string(matched[3])); cmd.index = std::stoi(std::string(matched[3]));
} }
@@ -26,7 +28,7 @@ int parseCommand(std::vector<Command> &commands, std::string line) {
Command cmd; Command cmd;
cmd.line = line; cmd.line = line;
cmd.type = commandTypes.at(matched[1]); cmd.commandType = commandTypes.at(matched[1]);
commands.push_back(cmd); commands.push_back(cmd);
return 0; return 0;
+9 -2
View File
@@ -44,10 +44,17 @@ std::unordered_map<std::string, SegmentType> const segmentTypes = {
{"temp", SegmentType::TEMP}, {"pointer", SegmentType::POINTER}, {"temp", SegmentType::TEMP}, {"pointer", SegmentType::POINTER},
}; };
std::unordered_map<std::string, std::string> const segmentNames = {
{"local", "LCL"}, {"argument", "ARG"}, {"this", "THIS"},
{"that", "THAT"}, {"static", "NONE"}, {"constant", "NONE"},
{"temp", "NONE"}, {"pointer", "NONE"},
};
typedef struct { typedef struct {
std::string line; std::string line;
CommandType type; CommandType commandType;
SegmentType segment; SegmentType segmentType;
std::string segmentName;
int index; int index;
} Command; } Command;