From 9de4219cef34cb5b373f2f0098a4dcfc88ca6813 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Thu, 2 Apr 2026 12:57:50 +0100 Subject: [PATCH] chore: improve input and output paths handling --- src/code.cpp | 8 ++++---- src/main.cpp | 16 +++++++--------- src/utils.cpp | 17 +++++++++++++++-- src/utils.hpp | 6 ++++-- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/code.cpp b/src/code.cpp index e50c8c4..0886edb 100644 --- a/src/code.cpp +++ b/src/code.cpp @@ -3,7 +3,7 @@ #include static int translateStackOperation(std::ostringstream &stream, - const std::string filename, Command cmd) { + const std::string programName, Command cmd) { switch (cmd.segmentType) { case SegmentType::LCL: case SegmentType::ARG: @@ -36,7 +36,7 @@ static int translateStackOperation(std::ostringstream &stream, break; case SegmentType::STATIC: - stream << "@" << filename << "." << cmd.index << std::endl; + stream << "@" << programName << "." << cmd.index << std::endl; if (cmd.commandType == CommandType::PUSH) { stream << "D=M" << std::endl; @@ -238,7 +238,7 @@ int translateBistwiseUnaryOperation(std::ostringstream &stream) { return 0; } -int translateCommand(std::string &output, const std::string filename, +int translateCommand(std::string &output, const std::string programName, Command cmd) { std::ostringstream stream; @@ -248,7 +248,7 @@ int translateCommand(std::string &output, const std::string filename, switch (cmd.commandType) { case CommandType::PUSH: case CommandType::POP: - translateStackOperation(stream, filename, cmd); + translateStackOperation(stream, programName, cmd); break; case CommandType::ADD: case CommandType::SUB: diff --git a/src/main.cpp b/src/main.cpp index aeb60d9..82f9c6d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,10 +8,10 @@ #include "parser.hpp" #include "utils.hpp" -int process(std::string source) { - std::ifstream ifs(source); - std::string filename = source.substr(0, source.find_last_of('.')); - std::ofstream ofs(filename + ".asm", std::ofstream::trunc); +int process(std::string path) { + std::ifstream ifs(path); + std::ofstream ofs(getOutputPath(path), std::ofstream::trunc); + std::string programName = getFileNameFromPath(path); std::string line; std::string output; @@ -31,7 +31,7 @@ int process(std::string source) { for (const Command &cmd : commands) { int translateResult; - if ((translateResult = translateCommand(output, filename, cmd)) != 0) { + if ((translateResult = translateCommand(output, programName, cmd)) != 0) { return translateResult; } @@ -50,12 +50,10 @@ int main(int argc, char **argv) { exit(1); } - std::string source = argv[1]; - - if (!regex_match(source, std::regex("^.+\\.vm"))) { + if (!regex_match(argv[1], std::regex("^.+\\.vm"))) { std::cerr << "Source file is not a vm file!" << std::endl; exit(1); } - return process(source); + return process(argv[1]); } diff --git a/src/utils.cpp b/src/utils.cpp index 4953966..14d1965 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,7 +1,8 @@ #include +#include #include -bool isEmptyLine(std::string line) { +bool isEmptyLine(const std::string &line) { for (char c : line) { if (!isspace(c)) return false; @@ -10,4 +11,16 @@ bool isEmptyLine(std::string line) { return true; } -bool isComment(std::string line) { return line[0] == '/' && line[1] == '/'; } +bool isComment(const std::string &line) { + return line[0] == '/' && line[1] == '/'; +} + +std::string getFileNameFromPath(const std::string &path) { + std::string fileName = path.substr(path.find_last_of("/\\") + 1); + + return fileName.substr(0, fileName.find_last_of('.')); +} + +std::string getOutputPath(const std::string &path) { + return path.substr(0, path.find_last_of('.')) + ".asm"; +} diff --git a/src/utils.hpp b/src/utils.hpp index 53cc254..b691e17 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -1,4 +1,6 @@ #include -bool isEmptyLine(std::string); -bool isComment(std::string); +bool isEmptyLine(const std::string&); +bool isComment(const std::string&); +std::string getFileNameFromPath(const std::string&); +std::string getOutputPath(const std::string&);