From e488aad6da8bc6352bbf461b51f8c287ef6ac6f3 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Fri, 22 Mar 2024 23:02:00 +0100 Subject: [PATCH] Remove whitespace and comments --- src/main.cpp | 4 +--- src/parser.h | 46 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ea7d61a..6eb6ab0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; } \ No newline at end of file diff --git a/src/parser.h b/src/parser.h index 8898de9..2d979a9 100644 --- a/src/parser.h +++ b/src/parser.h @@ -1,6 +1,9 @@ -#include #include +#include +#include +#include #include +#include 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; } } };