forked from xyzqm/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (76 loc) · 2.16 KB
/
main.cpp
File metadata and controls
80 lines (76 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <fstream>
#include "Parser/parser.h"
#include "Lexer/Lexer.h"
#include "Interpreter/interpreter.h"
#include "Interpreter/Symbols.h"
// Tip: Don't use using namespace, see https://bit.ly/aaron_help_CPP_GUIDELINE_1
using namespace std;
using namespace interpreter;
std::string readFileToVector(const std::string& filename)
{
std::ifstream source;
source.open(filename);
std::string lines;
std::string line;
while (std::getline(source, line))
{
lines += line + '\n';
}
return lines;
}
int interpret(string input) {
Lexer lexer(input);
vector<Token> tokens = lexer.allTokens();
// tokens.size() is a long unsigned int, use to prevent -Wall or -Wextra warnings
/* // cout << "Lexer tokens:" << endl;
for (long unsigned int i = 0; i < tokens.size(); i++) {
// cout << tokens[i];
}
Parser parser(input);
Program program;
try {
program = parser.parseProgram();
}
catch (string error) {
// cout << error << "\n";
return 1;
}
// cout << "Parser blocks:" << endl;
for (int i = 0; i < block.size(); i++) {
// cout << block[i]->print() << endl;
}*/
Interpreter interpreter(input);
// // cout << "Interpreting...\n";
try {
interpreter.interpret();
}
catch (std::string error) {
cout << error << "\n";
return 1;
}
/*// cout << "Variables: " << endl;
ScopedSymbolTable symtab = interpreter.getSymTab();
// cout << symtab.print();
for (auto const& pair : interpreter.GLOBAL_SCOPE) {
// cout << pair.first << " ";
std::string type = symtab.lookup(pair.first)->type->name;
if (type == "int") {
// cout << std::get<int>(pair.second);
}
else if (type == "real") {
// cout << std::get<double>(pair.second);
}
else if (type == "string") {
// cout << std::get<std::string>(pair.second);
}
// cout << endl;
}*/
return 0;
}
int main (int argc, char** argv) {
std::string input(readFileToVector(argv[1]));
Interpreter interpreter(input);
interpreter.interpret();
return 0;
}