mirror of
https://github.com/hazemKrimi/jack-vm-translator.git
synced 2026-05-01 18:00:27 +00:00
chore: improve input and output paths handling
This commit is contained in:
+4
-4
@@ -3,7 +3,7 @@
|
||||
#include <sstream>
|
||||
|
||||
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:
|
||||
|
||||
+7
-9
@@ -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]);
|
||||
}
|
||||
|
||||
+15
-2
@@ -1,7 +1,8 @@
|
||||
#include <cctype>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
+4
-2
@@ -1,4 +1,6 @@
|
||||
#include <string>
|
||||
|
||||
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&);
|
||||
|
||||
Reference in New Issue
Block a user