From 51c7327a657024e73e6c6eba1447946304f9a5e0 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Wed, 1 Apr 2026 17:02:36 +0100 Subject: [PATCH] chore: add segment names to parsed commands for easier translations --- src/parser.cpp | 8 +++++--- src/types.hpp | 11 +++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/parser.cpp b/src/parser.cpp index 5beb8b4..6c910e6 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -12,9 +12,11 @@ int parseCommand(std::vector &commands, std::string line) { Command cmd; if (matched.ready()) { + std::string segment = matched[2]; cmd.line = line; - cmd.type = commandTypes.at(matched[1]); - cmd.segment = segmentTypes.at(matched[2]); + cmd.commandType = commandTypes.at(matched[1]); + cmd.segmentType = segmentTypes.at(segment); + cmd.segmentName = segmentNames.at(segment); cmd.index = std::stoi(std::string(matched[3])); } @@ -26,7 +28,7 @@ int parseCommand(std::vector &commands, std::string line) { Command cmd; cmd.line = line; - cmd.type = commandTypes.at(matched[1]); + cmd.commandType = commandTypes.at(matched[1]); commands.push_back(cmd); return 0; diff --git a/src/types.hpp b/src/types.hpp index e1d97a5..faa8f63 100644 --- a/src/types.hpp +++ b/src/types.hpp @@ -44,10 +44,17 @@ std::unordered_map const segmentTypes = { {"temp", SegmentType::TEMP}, {"pointer", SegmentType::POINTER}, }; +std::unordered_map const segmentNames = { + {"local", "LCL"}, {"argument", "ARG"}, {"this", "THIS"}, + {"that", "THAT"}, {"static", "NONE"}, {"constant", "NONE"}, + {"temp", "NONE"}, {"pointer", "NONE"}, +}; + typedef struct { std::string line; - CommandType type; - SegmentType segment; + CommandType commandType; + SegmentType segmentType; + std::string segmentName; int index; } Command;