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[]) 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
View File
@@ -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;
} }
} }
}; };