mirror of
https://github.com/hazemKrimi/jack-vm-translator.git
synced 2026-05-01 18:00:27 +00:00
chore: improve line cleanup from whitespace and comments
This commit is contained in:
+8
-7
@@ -1,3 +1,4 @@
|
|||||||
|
#include <cstdlib>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -13,21 +14,21 @@ int process(std::string inputPath, std::string outputPath,
|
|||||||
std::ios_base::openmode outputStreamMode) {
|
std::ios_base::openmode outputStreamMode) {
|
||||||
std::ifstream ifs(inputPath);
|
std::ifstream ifs(inputPath);
|
||||||
std::ofstream ofs(outputPath, outputStreamMode);
|
std::ofstream ofs(outputPath, outputStreamMode);
|
||||||
std::string fileName = getFileNameFromPath(inputPath);
|
std::string fileName = getFileNameFromFilePath(inputPath);
|
||||||
std::string line;
|
std::string line;
|
||||||
std::string output;
|
std::string output;
|
||||||
|
|
||||||
std::vector<Command> commands;
|
std::vector<Command> commands;
|
||||||
|
|
||||||
while (getline(ifs, line)) {
|
while (getline(ifs, line)) {
|
||||||
if (isComment(line) || isEmptyLine(line))
|
if (cleanupLine(line) != 0)
|
||||||
|
exit(1);
|
||||||
|
|
||||||
|
if (isEmptyLine(line))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
int parseResult;
|
if (parseCommand(commands, line) != 0)
|
||||||
|
exit(1);
|
||||||
if ((parseResult = parseCommand(commands, line)) != 0) {
|
|
||||||
return parseResult;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const Command &cmd : commands) {
|
for (const Command &cmd : commands) {
|
||||||
|
|||||||
+21
-3
@@ -10,11 +10,29 @@ bool isEmptyLine(const std::string &line) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isComment(const std::string &line) {
|
int cleanupLine(std::string &line) {
|
||||||
return line[0] == '/' && line[1] == '/';
|
size_t commentStartingPosition = line.find("//");
|
||||||
|
|
||||||
|
if (commentStartingPosition != std::string::npos) {
|
||||||
|
line = line.substr(0, commentStartingPosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t firstNonWhiteSpacePosition = line.find_first_not_of(" \t\n\r");
|
||||||
|
|
||||||
|
if (firstNonWhiteSpacePosition == std::string::npos) {
|
||||||
|
line = "";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t lastNonWhiteSpacePosition = line.find_last_not_of(" \t\n\r");
|
||||||
|
line =
|
||||||
|
line.substr(firstNonWhiteSpacePosition,
|
||||||
|
(lastNonWhiteSpacePosition - firstNonWhiteSpacePosition + 1));
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string getFileNameFromPath(const std::string &path) {
|
std::string getFileNameFromFilePath(const std::string &path) {
|
||||||
std::string fileName = path.substr(path.find_last_of("/\\") + 1);
|
std::string fileName = path.substr(path.find_last_of("/\\") + 1);
|
||||||
|
|
||||||
return fileName.substr(0, fileName.find_last_of('.'));
|
return fileName.substr(0, fileName.find_last_of('.'));
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
bool isEmptyLine(const std::string&);
|
bool isEmptyLine(const std::string&);
|
||||||
bool isComment(const std::string&);
|
int cleanupLine(std::string&);
|
||||||
std::string getFileNameFromPath(const std::string&);
|
std::string getFileNameFromFilePath(const std::string&);
|
||||||
std::string getOutputPath(const std::string&);
|
std::string getOutputPath(const std::string&);
|
||||||
|
|||||||
Reference in New Issue
Block a user