From 1ad35fcc2669b0a9e31ddb26bd198b5fcf8c9c7f Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Tue, 7 Apr 2026 14:32:47 +0100 Subject: [PATCH] chore: handle an argument consisting of a directory containing vm files --- src/code.cpp | 8 ++++---- src/main.cpp | 32 ++++++++++++++++++++++++-------- src/utils.cpp | 7 +++++-- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/code.cpp b/src/code.cpp index 3a27e34..de68809 100644 --- a/src/code.cpp +++ b/src/code.cpp @@ -22,7 +22,7 @@ static int popFromStackToDRegister(std::ostringstream &stream) { } static int translateStackOperation(std::ostringstream &stream, - const std::string programName, Command cmd) { + const std::string fileName, Command cmd) { switch (cmd.segmentType) { case SegmentType::LCL: case SegmentType::ARG: @@ -55,7 +55,7 @@ static int translateStackOperation(std::ostringstream &stream, break; case SegmentType::STATIC: - stream << "@" << programName << "." << cmd.index << std::endl; + stream << "@" << fileName << "." << cmd.index << std::endl; if (cmd.commandType == CommandType::PUSH) { stream << "D=M" << std::endl; @@ -235,7 +235,7 @@ static int translateBistwiseUnaryOperation(std::ostringstream &stream) { return 0; } -int translateCommand(std::string &output, const std::string programName, +int translateCommand(std::string &output, const std::string fileName, Command cmd) { std::ostringstream stream; @@ -245,7 +245,7 @@ int translateCommand(std::string &output, const std::string programName, switch (cmd.commandType) { case CommandType::PUSH: case CommandType::POP: - translateStackOperation(stream, programName, cmd); + translateStackOperation(stream, fileName, cmd); break; case CommandType::ADD: case CommandType::SUB: diff --git a/src/main.cpp b/src/main.cpp index 82f9c6d..b919ca3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -8,10 +9,11 @@ #include "parser.hpp" #include "utils.hpp" -int process(std::string path) { - std::ifstream ifs(path); - std::ofstream ofs(getOutputPath(path), std::ofstream::trunc); - std::string programName = getFileNameFromPath(path); +int process(std::string inputPath, std::string outputPath, + std::ios_base::openmode outputStreamMode) { + std::ifstream ifs(inputPath); + std::ofstream ofs(outputPath, outputStreamMode); + std::string fileName = getFileNameFromPath(inputPath); std::string line; std::string output; @@ -31,7 +33,7 @@ int process(std::string path) { for (const Command &cmd : commands) { int translateResult; - if ((translateResult = translateCommand(output, programName, cmd)) != 0) { + if ((translateResult = translateCommand(output, fileName, cmd)) != 0) { return translateResult; } @@ -50,10 +52,24 @@ int main(int argc, char **argv) { exit(1); } - if (!regex_match(argv[1], std::regex("^.+\\.vm"))) { - std::cerr << "Source file is not a vm file!" << std::endl; + if (regex_match(argv[1], std::regex("^.+\\.vm"))) + return process(argv[1], getOutputPath(argv[1]), std::ofstream::trunc); + + if (!std::filesystem::is_directory(argv[1])) { + std::cerr << "Argument is not a VM file!" << std::endl; exit(1); } - return process(argv[1]); + std::string outputPath = getOutputPath(argv[1]); + + if (std::filesystem::exists(outputPath)) { + std::filesystem::remove(outputPath); + } + + for (const auto &entry : std::filesystem::directory_iterator(argv[1])) { + if (entry.is_regular_file() && entry.path().extension() == ".vm") { + if (process(entry.path().string(), outputPath, std::ofstream::app) != 0) + exit(1); + } + } } diff --git a/src/utils.cpp b/src/utils.cpp index 14d1965..6b88acd 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,5 +1,4 @@ -#include -#include +#include #include bool isEmptyLine(const std::string &line) { @@ -22,5 +21,9 @@ std::string getFileNameFromPath(const std::string &path) { } std::string getOutputPath(const std::string &path) { + if (std::filesystem::is_directory(path)) { + return path + "/" + path.substr(path.find_last_of("/\\") + 1) + ".asm"; + } + return path.substr(0, path.find_last_of('.')) + ".asm"; }