-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
525 lines (445 loc) · 12.4 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include <sstream>
#include "parser.h"
#include "logger.h"
/**
* Parser class constructor
* @param filereader pointer to a FileReader object
* @param tokens list of tokens to parse
*/
Parser::Parser(Input *input, std::vector<Token> tokens) : input(input), tokens(tokens) {}
/**
* Check if parser has reached the final token
* @return true if parser has reached the final token, false otherwise
*/
bool Parser::is_at_end() {
return peek().type == Eof;
}
/**
* Advance the parser to the next token
* @return the next token
*/
Token Parser::advance() {
if (!is_at_end())
current++;
return peek();
}
/**
* Returns the next token without advancing the current position
* @return the next token
*/
Token Parser::peek() {
return tokens.at(current);
}
/**
* Returns the previous token without reducing the current position
* @return the previous token
*/
Token Parser::previous() {
return tokens.at(current - 1);
}
/**
* Consumes the current token and advances
* Throws an error if the current token does not match the expected type
* @param type expected token type
* @return the consumed token
*/
Token Parser::consume(TokenType type) {
auto curr = peek();
std::stringstream ss;
ss << "expected " << type << ", got " << peek().type;
return consume(type, ss.str());
}
/**
* Consumes the current token and advances
* Throws an error if the current token does not match the expected type
* @param type expected token type
* @param error_message custom error message
* @return the consumed token
*/
Token Parser::consume(TokenType type, const std::string &error_message) {
auto curr = peek();
if (check(type)) {
advance();
return curr;
}
Logger::error(input, curr.line, curr.column, curr.lexeme.length(), error_message);
// Should not be reached
// TODO: Maybe improve this
throw 0;
}
/**
* Check if the current token is of the given type
* @param type token type to match
* @return true if the current token matches the given type, false otherwise
*/
bool Parser::check(TokenType type) {
if (is_at_end())
return false;
return peek().type == type;
}
/**
* Advance if the the current token is of the given type
* @param type token type to match
* @return true if the current token matches the given type, false otherwise
*/
bool Parser::match(TokenType expected) {
if (peek().type != expected)
return false;
advance();
return true;
}
/**
* Program ::= { Declaration } EOF
*
* Parses the list of tokens given to the parser
* @param verbose should the abstract syntax tree be printed
* @return abstract syntax tree (AST) representing the given list of tokens
*/
AST *Parser::parse(bool verbose) {
auto ast = new AST("program");
while (!is_at_end()) {
auto child = decl();
ast->add_child(child);
}
if (verbose)
ast->print();
return ast;
}
/**
* Declaration ::= VarDecl | FuncDecl ";"
*/
AST *Parser::decl() {
AST *ast;
switch (peek().type) {
case Var:ast = var_decl(true);
break;
case Func:ast = func_decl();
break;
default:
Logger::error(input, peek().line, peek().column, peek().lexeme.length(),
"declarations must be begin with a \"var\" or \"func\" keyword");
}
consume(Semicolon, "declarations must be terminated by a semicolon");
return ast;
}
/**
* VarDecl ::= "var" identifier identifier
*/
AST *Parser::var_decl(bool global) {
auto token = consume(Var);
auto ast = new AST(global ? "globalvar" : "var", token.line, token.column);
// Variable name
auto id = consume(Identifier, "variable identifier must follow the \"var\" keyword");
ast->add_child(new AST("newid", id.lexeme, id.line, id.column));
// Variable type
auto type = consume(Identifier, "variable type must follow the identifier");
ast->add_child(new AST("typeid", type.lexeme, type.line, type.column));
return ast;
}
/**
* FuncDecl ::= "func" identifier Signature Block
*/
AST *Parser::func_decl() {
auto token = consume(Func);
auto ast = new AST("func", token.line, token.column);
// Function name
auto id = consume(Identifier, "function identifier must follow the \"func\" keyword");
ast->add_child(new AST("newid", id.lexeme, id.line, id.column));
// Function signature
auto signature = func_sig();
ast->add_child(signature);
// Function body
auto body = block();
ast->add_child(body);
return ast;
}
/**
* Signature ::= "(" { identifier identifier "," } ")" [ identifier ]
*/
AST *Parser::func_sig() {
auto ast = new AST("sig");
// Opening paren
consume(LeftParen, "function signature must open with \"(\"");
// Formals
auto formals = new AST("formals");
ast->add_child(formals);
while (check(Identifier)) {
auto formal = new AST("formal");
auto id = consume(Identifier, "signature formal must begin with an identifier");
formal->add_child(new AST("newid", id.lexeme, id.line, id.column));
auto type = consume(Identifier, "expected a type to follow the formal identifier");
formal->add_child(new AST("typeid", type.lexeme, type.line, type.column));
formals->add_child(formal);
if (!match(Comma))
break;
};
// Closing paren
consume(RightParen, "function signature must be closed with \")\"");
// Optional return type
auto has_type = check(Identifier);
if (has_type) {
auto type = consume(Identifier);
ast->add_child(new AST("typeid", type.lexeme, type.line, type.column));
} else {
ast->add_child(new AST("typeid", "$void"));
}
return ast;
}
/**
* Block ::= "{" { Statement } "}"
*/
AST *Parser::block() {
auto ast = new AST("block");
consume(LeftBracket, "block must begin with \"{\"");
// Block body
while (!check(RightBracket)) {
auto child = stmt();
ast->add_child(child);
}
consume(RightBracket, "block must close with \"}\"");
return ast;
}
/**
* Statement ::=
* | VarDecl
* | IfStmt
* | ForStmt
* | BreakStmt
* | ReturnStmt
* | Block
* | ExpressionStmt
* ";"
*/
AST *Parser::stmt() {
AST *ast;
switch (peek().type) {
case Var:ast = var_decl(false);
break;
case If:ast = if_stmt();
break;
case For:ast = for_stmt();
break;
case Break:ast = break_stmt();
break;
case Return:ast = return_stmt();
break;
case LeftBracket:ast = block();
break;
default:ast = expr_stmt();
break;
}
consume(Semicolon, "statement must be terminated by \";\"");
return ast;
}
/**
* IfStmt ::= "if" Expression block [ "else" IfStmt | block ]
*/
AST *Parser::if_stmt() {
auto token = consume(If);
auto ast = new AST("if", token.line, token.column);
// Condition
auto condition = expr();
ast->add_child(condition);
// If body
auto body = block();
ast->add_child(body);
// Optional else/else-if
if (match(Else)) {
// Else-if
if (check(If))
ast->add_child(if_stmt());
// Else
else
ast->add_child((new AST("else"))->add_child(block()));
}
return ast;
}
/**
* ForStmt ::= "for" Expression block
*/
AST *Parser::for_stmt() {
auto token = consume(For);
auto ast = new AST("for", token.line, token.column);
// Optional condition
AST *condition;
if (check(LeftBracket)) {
condition = new AST("id", "$true");
} else {
condition = expr();
}
ast->add_child(condition);
// For body
auto body = block();
ast->add_child(body);
return ast;
}
/**
* BreakStmt ::= "break"
*/
AST *Parser::break_stmt() {
auto token = consume(Break);
auto ast = new AST("break", token.line, token.column);
return ast;
}
/**
* ReturnStmt ::= "return" [ Expression ]
*/
AST *Parser::return_stmt() {
auto token = consume(Return);
auto ast = new AST("return", token.line, token.column);
// Optional return expression
if (!check(Semicolon)) {
auto child = expr();
ast->add_child(child);
}
return ast;
}
/**
* ExpressionStmt ::= Assignment
*/
AST *Parser::expr_stmt() {
auto ast = assignment();
return ast;
}
/**
* Assignment ::= Expr "=" Expr | Expr
*/
AST *Parser::assignment() {
auto l = expr();
if (match(Equal)) {
auto op = previous();
auto r = expr();
l = (new AST(op.lexeme, op.line, op.column))->add_child(l)->add_child(r);
}
return l;
}
/**
* Expression ::= OrExpr
*/
AST *Parser::expr() {
auto ast = or_expr();
return ast;
}
/**
* OrExpr ::= AndExpr { "||" AndExpr }
*/
AST *Parser::or_expr() {
auto l = and_expr();
while (match(Or)) {
auto op = previous();
auto r = and_expr();
l = (new AST(op.lexeme, op.line, op.column))->add_child(l)->add_child(r);
}
return l;
}
/**
* AndExpr ::= RelExpr { "&&" RelExpr }
*/
AST *Parser::and_expr() {
auto l = rel_expr();
while (match(And)) {
auto op = previous();
auto r = rel_expr();
l = (new AST(op.lexeme, op.line, op.column))->add_child(l)->add_child(r);
}
return l;
}
/**
* RelExpr ::= AddExpr { ("==" | "!=" | "<" | "<=" | ">" | ">=") AddExpr }
*/
AST *Parser::rel_expr() {
auto l = add_expr();
while (match(EqualEqual) || match(NotEqual) || match(Less) ||
match(LessEqual) || match(Greater) || match(GreaterEqual)) {
auto op = previous();
auto r = add_expr();
l = (new AST(op.lexeme, op.line, op.column))->add_child(l)->add_child(r);
}
return l;
}
/**
* AddExpr ::= MulExpr { ("+" | "-") MulExpr }
*/
AST *Parser::add_expr() {
auto l = mul_expr();
while (match(Add) || match(Subtract)) {
auto op = previous();
auto r = mul_expr();
l = (new AST(op.lexeme, op.line, op.column))->add_child(l)->add_child(r);
}
return l;
}
/**
* MulExpr ::= UnaryExpr { ("*" | "/" | "%") UnaryExpr }
*/
AST *Parser::mul_expr() {
auto l = unary_expr();
while (match(Multiply) || match(Divide) || match(Modulo)) {
auto op = previous();
auto r = unary_expr();
l = (new AST(op.lexeme, op.line, op.column))->add_child(l)->add_child(r);
}
return l;
}
/**
* UnaryExpr ::= ("!" | "-") UnaryExpr | FuncCall
*/
AST *Parser::unary_expr() {
// TODO: Refactor this to be more concise if you are out of fun things to do in life :)
if (match(Not)) {
auto op = previous();
auto r = unary_expr();
return (new AST(op.lexeme, op.line, op.column))->add_child(r);
}
if (match(Subtract)) {
// TODO: Hacky solution for negative integers
if(match(Integer))
return new AST("int", "-" + previous().lexeme, previous().line, previous().column - 1);
auto op = previous();
auto r = unary_expr();
return (new AST("u" + op.lexeme, op.line, op.column))->add_child(r);
}
return func_call();
}
/**
* FuncCall ::= Operand | Operand "(" [ Expression { "," Expression } ] ")"
*/
AST *Parser::func_call() {
auto ast = operand();
// Arguments
while (match(LeftParen)) {
ast = (new AST("funccall", previous().line, previous().column))->add_child(ast);
auto actuals = new AST("actuals");
ast->add_child(actuals);
while (!match(RightParen)) {
actuals->add_child(expr());
if (!match(Comma)) {
consume(RightParen, "function call must closed with \")\"");
break;
}
}
}
return ast;
}
/**
* Operand ::= int_lit | string_lit | identifier | ";" | "(" Expression ")"
*/
AST *Parser::operand() {
if (match(Integer))
return new AST("int", previous().lexeme, previous().line, previous().column);
if (match(String))
return new AST("string", previous().lexeme, previous().line, previous().column);
if (match(Identifier))
return new AST("id", previous().lexeme, previous().line, previous().column);
if (check(Semicolon))
return new AST("emptystmt");
if (match(LeftParen)) {
auto ast = expr();
consume(RightParen, "parenthesised operands must be closed with \")\"");
return ast;
}
Logger::error(input, peek().line, peek().column + 1, 1, "expected expression");
// Should not be reached
// TODO: Maybe improve this
throw 0;
}