-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
174 lines (167 loc) · 5.14 KB
/
parser.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "Parser.hpp"
void Parser::nextToken() {
if (p_token.symbol().compare("EndOfFile") != 0) {
p_position++;
p_token = *p_position;
}
else {
eof_flag = true;
}
}
Parser::Parser(string file_path) {
Lexer lexer(file_path);
p_tokens = lexer.tokenize();
p_position = p_tokens.begin();
p_token = *p_position;
eof_flag = false;
}
unique_ptr<Statement> Parser::program() {
unique_ptr<Statement> statement;
while (eof_flag != true) {
statement = suite();
nextToken();
}
return move(statement);
}
unique_ptr<Statement> Parser::suite() {
auto statement_1 = statement();
while (p_token.symbol().compare("Identifier") == 0 ||
p_token.symbol().compare("IF") == 0 ||
p_token.symbol().compare("PRINT") == 0) {
auto statement_2 = statement();
statement_1 = make_unique<Statement>(move(statement_1), move(statement_2));
}
return statement_1;
}
unique_ptr<Statement> Parser::statement() {
unique_ptr<Statement> statement_1;
Expression* new_expression = nullptr;
switch (p_token.getSymbol()) {
case 0: { // Identifier
if (p_token.getToken().compare("declare") != 0) {
// error: declare missing'
cout << "Error: 'declare' keyword missing." << endl;
//exit(0);
}
else {
nextToken();
}
if (p_token.getToken().compare("a") != 0) {
// error: a missing
cout << "Error: 'a' keyword missing." << endl;
//exit(0);
}
else {
nextToken();
}
if (p_token.getToken().compare("variable") != 0) {
// error: variable missing
cout << "Error: 'variable' keyword missing." << endl;
//exit(0);
}
else {
nextToken();
}
if (p_token.getToken().compare("named") != 0) {
// error: named missing
cout << "Error: 'named' keyword missing." << endl;
//exit(0);
}
else {
nextToken();
}
Identifier new_id_1(p_token.getToken());
nextToken();
if (p_token.getToken().compare("that") != 0) {
// error: that missing
cout << "Error: 'that' keyword missing." << endl;
//exit(0);
}
else {
nextToken();
}
if (p_token.getToken().compare("equals") != 0) {
// error: equals missing
cout << "Error: 'equals' keyword missing." << endl;
//exit(0);
}
else {
nextToken();
}
if (p_token.symbol().compare("DoubleQuote") == 0) {
new_expression = handleString();
}
else if (p_token.symbol().compare("Integer") == 0) {
new_expression = new Integer(stoi(p_token.getToken()));
}
nextToken();
if (p_token.getToken().compare(";") != 0) {
// error: semicolon missing
throw runtime_error("Error: ';' missing at end of line.");
}
statement_1 = make_unique<Assignment>(new_id_1, new_expression);
nextToken();
break;
}
case 4: { // IF
nextToken();
Expression* new_id_2 = new Identifier(p_token.getToken());
nextToken();
if (p_token.getToken().compare("is") != 0) {
// error: is missing
}
string new_operator = p_token.getToken();
nextToken();
Expression* new_integer_1 = new Integer(stoi(p_token.getToken()));
Binary* new_binary_1 = new Binary(new_operator, new_id_2, new_integer_1);
nextToken();
if (p_token.getToken().compare(":") != 0) {
// error: colon missing
}
nextToken();
auto statement_2 = suite();
if (p_token.getToken().compare("else") != 0) {
statement_1 = make_unique<IfStatement>(new_binary_1, move(statement_2));
break;
}
else {
nextToken();
if (p_token.getToken().compare(":") != 0) {
//error: colon missing
}
nextToken();
auto statement_3 = suite();
statement_1 = make_unique<IfStatement>(new_binary_1, move(statement_2), move(statement_3));
break;
}
}
case 6: { // PRINT
nextToken();
if (p_token.symbol().compare("Integer") == 0) {
new_expression = new Integer(stoi(p_token.getToken()));
}
else if (p_token.symbol().compare("Identifier") == 0) {
new_expression = new Identifier(p_token.getToken());
}
statement_1 = make_unique<PrintStatement>(new_expression);
nextToken();
if (p_token.getToken().compare(";") != 0) {
// error: semicolon missing
}
nextToken();
break;
}
}
return statement_1;
}
Expression* Parser::handleString() {
nextToken();
string concat_string = p_token.getToken();
nextToken();
while (p_token.symbol().compare("DoubleQuote") != 0) {
concat_string = concat_string + " " + p_token.getToken();
nextToken();
}
Expression* string_1 = new String(concat_string);
return string_1;
}