-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
344 lines (260 loc) · 9.07 KB
/
parser.cpp
File metadata and controls
344 lines (260 loc) · 9.07 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "parser.h"
TokenStore& Parser::getCurrentToken() {
if (currentToken >= tokens.size()) {
static TokenStore eofTok = { "", tok_eof };
return eofTok;
}
return tokens[currentToken];
}
TokenStore& Parser::getNextToken() {
if (currentToken < tokens.size())
currentToken++;
return getCurrentToken();
}
bool Parser::isOperator(Token tok) {
return tok == tok_plus || tok == tok_minus || tok == tok_multiply || tok == tok_divide ||
tok == tok_eq || tok == tok_ne || tok == tok_lt || tok == tok_gt || tok == tok_le || tok == tok_ge;
}
void Parser::parserError(const std::string& msg) {
const auto token = getCurrentToken();
const std::string currTokeInfo = " | Current token is => " + token.name;
const std::string error = "Line: " + std::to_string(token.line) + " | " + msg + currTokeInfo;
throw std::runtime_error(error);
}
// https://en.cppreference.com/w/cpp/language/operator_precedence.html
int Parser::op_precedence(const std::string& op) {
std::map<std::string, int> op_pre;
op_pre["*"] = 10;
op_pre["/"] = 10;
op_pre["%"] = 10;
op_pre["+"] = 8;
op_pre["-"] = 8;
op_pre["<"] = 7;
op_pre[">"] = 7;
return op_pre[op] > 0 ? op_pre[op] : -1;
}
ExprPtr Parser::parseNumber() {
auto res = std::make_unique<NumberExprAST>(std::stod(getCurrentToken().name));
getNextToken();
return res;
}
ExprPtr Parser::parseString() {
auto res = std::make_unique<StringExprAST>(getCurrentToken().name);
getNextToken();
return res;
}
ExprPtr Parser::parsePrint() {
getNextToken(); // skip "print"
if (getCurrentToken().token_type != tok_left_paren)
parserError("Expected '(' after 'print'");
getNextToken(); // skip '('
auto expr = parseExpression();
if (!expr)
std::cerr << "Invalid expression in print statement.\n";
if (getCurrentToken().token_type != tok_right_paren)
parserError("Expected ')' after print expression.");
getNextToken(); // skip ')'
return std::make_unique<PrintExprAST>(std::move(expr));
}
ExprPtr Parser::parsePrimary() {
switch (getCurrentToken().token_type) {
case tok_identifier: return parseIdentifier();
case tok_number: return parseNumber();
case tok_string: return parseString();
case tok_print: return parsePrint();
case tok_if: return parseIfElse();
default:
parserError("Unknown token at position: " + std::to_string(currentToken) + " -> " + getCurrentToken().name);
}
}
ExprPtr Parser::parseFunctionCall(const std::string& callee) {
getNextToken(); // skip '('
std::vector<ExprPtr> args;
while (getCurrentToken().token_type != tok_right_paren) {
args.push_back(parseExpression());
if (getCurrentToken().token_type == tok_comma)
getNextToken();
else if (getCurrentToken().token_type != tok_right_paren)
parserError("Expected ',' or ')' in function call.");
}
getNextToken(); // skip ')'
return std::make_unique<CallExprAST>(callee, std::move(args));
}
ExprPtr Parser::parseIdentifier() {
std::string id = getCurrentToken().name;
getNextToken();
if (getCurrentToken().token_type == tok_left_paren)
return parseFunctionCall(id);
return std::make_unique<VariableExprAST>(id);
}
// a = 3 + 4 * 5 || b = 3 * 4 + 11
ExprPtr Parser::parseBinaryOp(int min_prec) {
auto lhs = parsePrimary();
if (!lhs) return nullptr;
while (isOperator(getCurrentToken().token_type)) {
std::string op = getCurrentToken().name;
int precedence = op_precedence(op);
if (precedence < min_prec)
break;
getNextToken(); // skip binOperator
auto rhs = parseBinaryOp(precedence + 1); // recursive op_prec
if (!rhs) return nullptr;
lhs = std::make_unique<BinaryExprAST>(op, std::move(lhs), std::move(rhs));
}
return lhs;
}
ExprPtr Parser::parseExpression() {
if (getCurrentToken().token_type == tok_identifier &&
tokens.size() > currentToken + 1 &&
tokens[currentToken + 1].token_type == tok_equals)
return parseAssignment();
return parseBinaryOp(0);
}
ExprPtr Parser::parseBlock() {
std::vector<ExprPtr> expr;
while (getCurrentToken().token_type != tok_right_brace && getCurrentToken().token_type != tok_eof) {
if (getCurrentToken().token_type == tok_semicolon) {
getNextToken();
continue;
}
if (getCurrentToken().token_type == tok_if) {
expr.push_back(parseIfElse());
continue;
}
if (getCurrentToken().token_type == tok_for) {
expr.push_back(parseFor());
continue;
}
auto temp = parseExpression();
if (temp)
expr.push_back(std::move(temp));
}
return std::make_unique<BlockExprAST>(std::move(expr));
}
// Assign Parsing -> "a = 3*b;"
ExprPtr Parser::parseAssignment() {
std::string n = getCurrentToken().name; // get identifier name
getNextToken(); // skip identifier
if (getCurrentToken().token_type != tok_equals) {
parserError("Expected '=' after variable name.");
}
getNextToken(); // skip "="
auto val = parseExpression();
if (!val)
return nullptr;
if (getCurrentToken().token_type != tok_semicolon)
parserError("Expected ';' after assignment.");
getNextToken(); // skip ";"
return std::make_unique<AssignmentExprAST>(n, std::move(val));
}
// Prototype -> fn test( a , b )
std::unique_ptr<PrototypeAST> Parser::parsePrototype() {
if (getCurrentToken().token_type != tok_identifier)
parserError("Expected function name not available!");
std::string FuncName = getCurrentToken().name;
getNextToken(); // skip function name
if (getCurrentToken().token_type != tok_left_paren)
parserError("Expected '(' after function name.");
getNextToken(); // skip '('
std::vector<std::string> args;
while (getCurrentToken().token_type != tok_right_paren) {
if (getCurrentToken().token_type == tok_identifier)
args.push_back(getCurrentToken().name);
else
parserError("Expected identifier in function arguments.");
getNextToken();
if (getCurrentToken().token_type == tok_comma)
getNextToken();
else if (getCurrentToken().token_type != tok_right_paren)
parserError("Expected ',' or ')' in function arguments.");
}
getNextToken(); // skip ')'
return std::make_unique<PrototypeAST>(FuncName, std::move(args));
}
std::unique_ptr<FunctionAST> Parser::parseFunction() {
if (getCurrentToken().token_type != tok_fn)
parserError("Expected 'fn' keyword not available! Current Token -> " + getCurrentToken().name);
getNextToken(); // skip 'fn'
if (getCurrentToken().token_type != tok_identifier)
parserError("Expected function name not available!");
auto proto = parsePrototype();
if (getCurrentToken().token_type != tok_left_brace)
parserError("Expected '{' to start function body.");
getNextToken(); // skip '{'
auto body = parseBlock();
if (getCurrentToken().token_type != tok_right_brace)
parserError("Expected '}' to end function body.");
getNextToken(); // skip '}'
return std::make_unique<FunctionAST>(std::move(proto), std::move(body));
}
ExprPtr Parser::parseIfElse() {
getNextToken(); // skip 'if'
auto cond = parseExpression();
if (getCurrentToken().token_type != tok_left_brace)
parserError("Expected '{' to start 'then' block.");
getNextToken(); // skip '{'
auto thenExpr = parseBlock();
if (getCurrentToken().token_type != tok_right_brace)
parserError("Expected '}' to end 'then' block.");
getNextToken(); // skip '}'
auto elseExpr = parseElse();
return std::make_unique<ifExprAST>(std::move(cond), std::move(thenExpr), std::move(elseExpr));
}
// -> else { do_it_somethings } || else if { do_it_anything_else }
ExprPtr Parser::parseElse() {
if (getCurrentToken().token_type == tok_else) {
getNextToken(); // skip 'else'
// looks like "else if"
if (getCurrentToken().token_type == tok_if)
return parseIfElse();
else {
if (getCurrentToken().token_type != tok_left_brace)
parserError("Expected '{' to start 'else' block.");
getNextToken(); // skip "{"
auto elseExpr = parseBlock();
if (getCurrentToken().token_type != tok_right_brace)
parserError("Expected '}' to end 'else' block.");
getNextToken(); // skip '}'
return elseExpr;
}
}
// if "else" doesn't exist return empty BlockExpr!
return std::make_unique<BlockExprAST>(std::vector<ExprPtr>());
}
// for now broken!!!
// -> for x = 3, x < 50, 2 { Body }
ExprPtr Parser::parseFor() {
getNextToken(); // skip "for"
if (getCurrentToken().token_type != tok_identifier)
parserError("Expected identifier after 'for'");
// var shadowing
std::string varName = getCurrentToken().name;
getNextToken(); // skip "identifier"
getNextToken(); // skip "="
auto start = parseExpression();
if (!start)
return nullptr;
if (getCurrentToken().token_type != tok_comma)
parserError("Expected ',' after for start val");
getNextToken(); // skip ','
auto end = parseExpression();
if (!end)
return nullptr;
ExprPtr step = nullptr;
if (getCurrentToken().token_type == tok_comma) {
getNextToken();
step = parseExpression();
if (!step)
return nullptr;
}
if (getCurrentToken().token_type != tok_left_brace)
parserError("Expected '{' after for");
getNextToken(); // skip '{'
auto body = parseBlock();
if (!body)
return nullptr;
if (getCurrentToken().token_type != tok_right_brace)
parserError("Expected '}' after for body");
getNextToken(); // skip '}'
return std::make_unique<forExprAST>(varName, std::move(start), std::move(end), std::move(step), std::move(body));
}