Remove whitespace and comments

This commit is contained in:
Hazem Krimi
2024-03-22 23:02:00 +01:00
parent 852904c40d
commit e488aad6da
2 changed files with 44 additions and 6 deletions
-2
View File
@@ -6,10 +6,8 @@ using namespace std;
int main(int argc, char* argv[])
{
string path = argv[1];
Parser parser(path);
parser.printFile();
return 0;
}
+43 -3
View File
@@ -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;
}
}
};