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:
@@ -6,10 +6,8 @@ using namespace std;
|
|||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
string path = argv[1];
|
string path = argv[1];
|
||||||
|
|
||||||
Parser parser(path);
|
Parser parser(path);
|
||||||
|
|
||||||
parser.printFile();
|
parser.printFile();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
+43
-3
@@ -1,6 +1,9 @@
|
|||||||
#include <string>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
#include <regex>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -8,20 +11,57 @@ class Parser
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
ifstream file;
|
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:
|
public:
|
||||||
Parser(string path)
|
Parser(string path)
|
||||||
{
|
{
|
||||||
file = ifstream(path);
|
file = ifstream(path);
|
||||||
|
|
||||||
|
removeCommentsAndWhitespace();
|
||||||
}
|
}
|
||||||
|
|
||||||
void printFile()
|
void printFile()
|
||||||
{
|
{
|
||||||
|
stringstream vmCodeStream(vmCode);
|
||||||
string text;
|
string text;
|
||||||
|
|
||||||
while (getline(file, text))
|
while (getline(vmCodeStream, text, '\n'))
|
||||||
{
|
{
|
||||||
cout << text;
|
cout << text << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user