mirror of
https://github.com/hazemKrimi/jack-vm-translator.git
synced 2026-05-01 18:00:27 +00:00
chore: handle an argument consisting of a directory containing vm files
This commit is contained in:
+4
-4
@@ -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:
|
||||
|
||||
+24
-8
@@ -1,3 +1,4 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-2
@@ -1,5 +1,4 @@
|
||||
#include <cctype>
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user