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>
|
#include <sstream>
|
||||||
|
|
||||||
static int translateStackOperation(std::ostringstream &stream,
|
static int translateStackOperation(std::ostringstream &stream,
|
||||||
const std::string filename, Command cmd) {
|
const std::string programName, Command cmd) {
|
||||||
switch (cmd.segmentType) {
|
switch (cmd.segmentType) {
|
||||||
case SegmentType::LCL:
|
case SegmentType::LCL:
|
||||||
case SegmentType::ARG:
|
case SegmentType::ARG:
|
||||||
@@ -36,7 +36,7 @@ static int translateStackOperation(std::ostringstream &stream,
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case SegmentType::STATIC:
|
case SegmentType::STATIC:
|
||||||
stream << "@" << filename << "." << cmd.index << std::endl;
|
stream << "@" << programName << "." << cmd.index << std::endl;
|
||||||
|
|
||||||
if (cmd.commandType == CommandType::PUSH) {
|
if (cmd.commandType == CommandType::PUSH) {
|
||||||
stream << "D=M" << std::endl;
|
stream << "D=M" << std::endl;
|
||||||
@@ -238,7 +238,7 @@ int translateBistwiseUnaryOperation(std::ostringstream &stream) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int translateCommand(std::string &output, const std::string filename,
|
int translateCommand(std::string &output, const std::string programName,
|
||||||
Command cmd) {
|
Command cmd) {
|
||||||
std::ostringstream stream;
|
std::ostringstream stream;
|
||||||
|
|
||||||
@@ -248,7 +248,7 @@ int translateCommand(std::string &output, const std::string filename,
|
|||||||
switch (cmd.commandType) {
|
switch (cmd.commandType) {
|
||||||
case CommandType::PUSH:
|
case CommandType::PUSH:
|
||||||
case CommandType::POP:
|
case CommandType::POP:
|
||||||
translateStackOperation(stream, filename, cmd);
|
translateStackOperation(stream, programName, cmd);
|
||||||
break;
|
break;
|
||||||
case CommandType::ADD:
|
case CommandType::ADD:
|
||||||
case CommandType::SUB:
|
case CommandType::SUB:
|
||||||
|
|||||||
+7
-9
@@ -8,10 +8,10 @@
|
|||||||
#include "parser.hpp"
|
#include "parser.hpp"
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
|
||||||
int process(std::string source) {
|
int process(std::string path) {
|
||||||
std::ifstream ifs(source);
|
std::ifstream ifs(path);
|
||||||
std::string filename = source.substr(0, source.find_last_of('.'));
|
std::ofstream ofs(getOutputPath(path), std::ofstream::trunc);
|
||||||
std::ofstream ofs(filename + ".asm", std::ofstream::trunc);
|
std::string programName = getFileNameFromPath(path);
|
||||||
std::string line;
|
std::string line;
|
||||||
std::string output;
|
std::string output;
|
||||||
|
|
||||||
@@ -31,7 +31,7 @@ int process(std::string source) {
|
|||||||
for (const Command &cmd : commands) {
|
for (const Command &cmd : commands) {
|
||||||
int translateResult;
|
int translateResult;
|
||||||
|
|
||||||
if ((translateResult = translateCommand(output, filename, cmd)) != 0) {
|
if ((translateResult = translateCommand(output, programName, cmd)) != 0) {
|
||||||
return translateResult;
|
return translateResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,12 +50,10 @@ int main(int argc, char **argv) {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string source = argv[1];
|
if (!regex_match(argv[1], std::regex("^.+\\.vm"))) {
|
||||||
|
|
||||||
if (!regex_match(source, std::regex("^.+\\.vm"))) {
|
|
||||||
std::cerr << "Source file is not a vm file!" << std::endl;
|
std::cerr << "Source file is not a vm file!" << std::endl;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return process(source);
|
return process(argv[1]);
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-2
@@ -1,7 +1,8 @@
|
|||||||
#include <cctype>
|
#include <cctype>
|
||||||
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
bool isEmptyLine(std::string line) {
|
bool isEmptyLine(const std::string &line) {
|
||||||
for (char c : line) {
|
for (char c : line) {
|
||||||
if (!isspace(c))
|
if (!isspace(c))
|
||||||
return false;
|
return false;
|
||||||
@@ -10,4 +11,16 @@ bool isEmptyLine(std::string line) {
|
|||||||
return true;
|
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>
|
#include <string>
|
||||||
|
|
||||||
bool isEmptyLine(std::string);
|
bool isEmptyLine(const std::string&);
|
||||||
bool isComment(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