chore: handle an argument consisting of a directory containing vm files

This commit is contained in:
2026-04-07 14:32:47 +01:00
parent 27ade5ab88
commit 1ad35fcc26
3 changed files with 33 additions and 14 deletions
+4 -4
View File
@@ -22,7 +22,7 @@ static int popFromStackToDRegister(std::ostringstream &stream) {
} }
static int translateStackOperation(std::ostringstream &stream, static int translateStackOperation(std::ostringstream &stream,
const std::string programName, Command cmd) { const std::string fileName, Command cmd) {
switch (cmd.segmentType) { switch (cmd.segmentType) {
case SegmentType::LCL: case SegmentType::LCL:
case SegmentType::ARG: case SegmentType::ARG:
@@ -55,7 +55,7 @@ static int translateStackOperation(std::ostringstream &stream,
break; break;
case SegmentType::STATIC: case SegmentType::STATIC:
stream << "@" << programName << "." << cmd.index << std::endl; stream << "@" << fileName << "." << cmd.index << std::endl;
if (cmd.commandType == CommandType::PUSH) { if (cmd.commandType == CommandType::PUSH) {
stream << "D=M" << std::endl; stream << "D=M" << std::endl;
@@ -235,7 +235,7 @@ static int translateBistwiseUnaryOperation(std::ostringstream &stream) {
return 0; return 0;
} }
int translateCommand(std::string &output, const std::string programName, int translateCommand(std::string &output, const std::string fileName,
Command cmd) { Command cmd) {
std::ostringstream stream; std::ostringstream stream;
@@ -245,7 +245,7 @@ int translateCommand(std::string &output, const std::string programName,
switch (cmd.commandType) { switch (cmd.commandType) {
case CommandType::PUSH: case CommandType::PUSH:
case CommandType::POP: case CommandType::POP:
translateStackOperation(stream, programName, cmd); translateStackOperation(stream, fileName, cmd);
break; break;
case CommandType::ADD: case CommandType::ADD:
case CommandType::SUB: case CommandType::SUB:
+24 -8
View File
@@ -1,3 +1,4 @@
#include <filesystem>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <regex> #include <regex>
@@ -8,10 +9,11 @@
#include "parser.hpp" #include "parser.hpp"
#include "utils.hpp" #include "utils.hpp"
int process(std::string path) { int process(std::string inputPath, std::string outputPath,
std::ifstream ifs(path); std::ios_base::openmode outputStreamMode) {
std::ofstream ofs(getOutputPath(path), std::ofstream::trunc); std::ifstream ifs(inputPath);
std::string programName = getFileNameFromPath(path); std::ofstream ofs(outputPath, outputStreamMode);
std::string fileName = getFileNameFromPath(inputPath);
std::string line; std::string line;
std::string output; std::string output;
@@ -31,7 +33,7 @@ int process(std::string path) {
for (const Command &cmd : commands) { for (const Command &cmd : commands) {
int translateResult; int translateResult;
if ((translateResult = translateCommand(output, programName, cmd)) != 0) { if ((translateResult = translateCommand(output, fileName, cmd)) != 0) {
return translateResult; return translateResult;
} }
@@ -50,10 +52,24 @@ int main(int argc, char **argv) {
exit(1); exit(1);
} }
if (!regex_match(argv[1], std::regex("^.+\\.vm"))) { if (regex_match(argv[1], std::regex("^.+\\.vm")))
std::cerr << "Source file is not a vm file!" << std::endl; 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); 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);
}
}
} }
+5 -2
View File
@@ -1,5 +1,4 @@
#include <cctype> #include <filesystem>
#include <iostream>
#include <string> #include <string>
bool isEmptyLine(const std::string &line) { bool isEmptyLine(const std::string &line) {
@@ -22,5 +21,9 @@ std::string getFileNameFromPath(const std::string &path) {
} }
std::string getOutputPath(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"; return path.substr(0, path.find_last_of('.')) + ".asm";
} }