mirror of
https://github.com/hazemKrimi/jack-vm-translator.git
synced 2026-05-01 18:00:27 +00:00
Complete parser class
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <regex>
|
||||||
#include <parser.h>
|
#include <parser.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@@ -6,8 +7,15 @@ using namespace std;
|
|||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
string path = argv[1];
|
string path = argv[1];
|
||||||
|
|
||||||
|
if (!regex_match(path, regex("^.+\\.vm"))) {
|
||||||
|
cout << "Wrong file extension!" << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
Parser parser(path);
|
Parser parser(path);
|
||||||
|
|
||||||
parser.printFile();
|
parser.printFile();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
+43
-3
@@ -4,6 +4,7 @@
|
|||||||
#include <regex>
|
#include <regex>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -33,7 +34,8 @@ private:
|
|||||||
{
|
{
|
||||||
if (regex_search(text, matched, regex("^(.*)?(\\/\\/.*)")) || isEmptyLine(text))
|
if (regex_search(text, matched, regex("^(.*)?(\\/\\/.*)")) || isEmptyLine(text))
|
||||||
{
|
{
|
||||||
if (!isEmptyLine(matched[1])) {
|
if (!isEmptyLine(matched[1]))
|
||||||
|
{
|
||||||
string command = matched[1];
|
string command = matched[1];
|
||||||
vmCode.append(command + '\n');
|
vmCode.append(command + '\n');
|
||||||
}
|
}
|
||||||
@@ -54,14 +56,52 @@ public:
|
|||||||
removeCommentsAndWhitespace();
|
removeCommentsAndWhitespace();
|
||||||
}
|
}
|
||||||
|
|
||||||
void printFile()
|
vector<vector<string>> getTokens()
|
||||||
{
|
{
|
||||||
stringstream vmCodeStream(vmCode);
|
stringstream vmCodeStream(vmCode);
|
||||||
string text;
|
string text;
|
||||||
|
smatch matched;
|
||||||
|
vector<vector<string>> tokens;
|
||||||
|
|
||||||
while (getline(vmCodeStream, text, '\n'))
|
while (getline(vmCodeStream, text, '\n'))
|
||||||
{
|
{
|
||||||
cout << text << endl;
|
vector<string> 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<vector<string>> 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user