mirror of
https://github.com/hazemKrimi/jack-vm-translator.git
synced 2026-05-01 18:00:27 +00:00
Remove whitespace and comments
This commit is contained in:
+1
-3
@@ -3,13 +3,11 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
string path = argv[1];
|
||||
|
||||
Parser parser(path);
|
||||
|
||||
parser.printFile();
|
||||
|
||||
return 0;
|
||||
}
|
||||
+43
-3
@@ -1,6 +1,9 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <regex>
|
||||
#include <fstream>
|
||||
#include <cctype>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -8,20 +11,57 @@ class Parser
|
||||
{
|
||||
private:
|
||||
ifstream file;
|
||||
string vmCode;
|
||||
|
||||
bool isEmptyLine(string text)
|
||||
{
|
||||
for (char c : text)
|
||||
{
|
||||
if (!isspace(c))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void removeCommentsAndWhitespace()
|
||||
{
|
||||
string text;
|
||||
smatch matched;
|
||||
|
||||
while (getline(file, text))
|
||||
{
|
||||
if (regex_search(text, matched, regex("^(.*)?(\\/\\/.*)")) || isEmptyLine(text))
|
||||
{
|
||||
if (!isEmptyLine(matched[1])) {
|
||||
string command = matched[1];
|
||||
vmCode.append(command + '\n');
|
||||
}
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
vmCode.append(text + '\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
Parser(string path)
|
||||
{
|
||||
file = ifstream(path);
|
||||
|
||||
removeCommentsAndWhitespace();
|
||||
}
|
||||
|
||||
void printFile()
|
||||
{
|
||||
stringstream vmCodeStream(vmCode);
|
||||
string text;
|
||||
|
||||
while (getline(file, text))
|
||||
while (getline(vmCodeStream, text, '\n'))
|
||||
{
|
||||
cout << text;
|
||||
cout << text << endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user