diff --git a/src/main.cpp b/src/main.cpp index 6eb6ab0..3db314c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include +#include #include using namespace std; @@ -6,8 +7,15 @@ using namespace std; int main(int argc, char* argv[]) { string path = argv[1]; + + if (!regex_match(path, regex("^.+\\.vm"))) { + cout << "Wrong file extension!" << endl; + return 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 2d979a9..902055f 100644 --- a/src/parser.h +++ b/src/parser.h @@ -4,6 +4,7 @@ #include #include #include +#include using namespace std; @@ -33,7 +34,8 @@ private: { if (regex_search(text, matched, regex("^(.*)?(\\/\\/.*)")) || isEmptyLine(text)) { - if (!isEmptyLine(matched[1])) { + if (!isEmptyLine(matched[1])) + { string command = matched[1]; vmCode.append(command + '\n'); } @@ -54,14 +56,52 @@ public: removeCommentsAndWhitespace(); } - void printFile() + vector> getTokens() { stringstream vmCodeStream(vmCode); string text; + smatch matched; + vector> tokens; while (getline(vmCodeStream, text, '\n')) { - cout << text << endl; + vector matchedVector; + + if (regex_search(text, matched, regex("^(.*) (.*) (.*)"))) + { + matchedVector.push_back(matched[1]); + matchedVector.push_back(matched[2]); + matchedVector.push_back(matched[3]); + } + else + { + matchedVector.push_back(text); + } + tokens.push_back(matchedVector); + } + + return tokens; + } + + void printFile() + { + vector> tokens = getTokens(); + + for (const auto &vec : tokens) + { + if (vec.size() > 1) + { + cout << vec[0] << "-"; + if (vec.size() > 1) + cout << vec[1] << "-"; + if (vec.size() > 1) + cout << vec[2]; + cout << endl; + } + else + { + cout << vec[0] << endl; + } } } };