-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
668 lines (548 loc) · 19.1 KB
/
Copy pathparser.cpp
File metadata and controls
668 lines (548 loc) · 19.1 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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#include "util.h"
#include "parser.h"
#include "lexer.h"
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include "reversePolish.h"
#include "statement.h"
/*
* Will parse the token stream into a runnable programm
* @param tokens the tokens object tree that will be turned into a programm
* @return the programm object that will be runnable
*/
Program Parser::parse(Tokens tokens) {
std::vector<Expression*> expressions;
Token current = tokens.current();
while (current.get_type() != END){
Expression* exp = parseNextExpression(tokens);
expressions.push_back(exp);
current = tokens.current();
}
return Program(expressions);
}
/*
* Will parse a single expression in the token stream. This will consume the expression
* in the token stream. Note that this will consume any newlines at the end of a expression
* but not 'end of file' tokens. 'end of file' needs to be handle by the callee.
* @param tokens a reference to the token stream object
* @param remove_newline a optional value set to false if the newline at the end should not be removed (used for one line expressions) (default true)
* @return expression object e.g If(), FunctionCall(), etc.
*/
Expression* parseNextExpression(Tokens& tokens, bool remove_newline) {
Token current = tokens.current();
Expression* exp;
Token third;
// If the first thing in the line is a newline. Eat it and try again
while (current.get_type() == NL) {
tokens.eat(); // eat the newline
current = tokens.current(); // Get next token
}
// Get the next two values after the current token
Token next = tokens.next();
// This is also a dirty way of solving the problem of having only a single variable / statement in the programm
if (tokens.tokens.size()<=2) {
third = Token(END, "EOF");
}
else {
Token third = tokens.next(2);
}
if (current.get_type()==VAR && next.get_type() == EQUAL && third.get_type() != EQUAL) {
exp = parseDeclaration(tokens);
}
else if (current.get_type() == KEYWORD && current.value == "if"){
exp = parseIf(tokens);
}
else if (current.get_type() == KEYWORD && current.value == "while") {
exp = parseWhile(tokens);
}
else if (current.get_type() == KEYWORD && current.value == "for") {
exp = parseFor(tokens);
}
else if (current.get_type() == KEYWORD && current.get_value() == "def") {
exp = parseFunctionDefinition(tokens);
}
else if (current.get_type() == KEYWORD && current.get_value() == "return") {
exp = parseReturn(tokens);
}
else if (current.get_type() == KEYWORD && current.get_value() == "break") {
exp = parseBreak(tokens);
}
else if (current.get_type() == KEYWORD && current.get_value() == "debug") {
exp = parseDebug(tokens);
}
else {
exp = buildStatement(tokens);
}
// Eat the last thing at the end of the line. Either this will be at the end of a statement or at the end of if, while, etc. But it will always be a newline
// In case of if, while, etc it will eat the newline not at the end of the line with the if but after the end that ends the if (if not one liner if) otherwise
// it will eat the newline after the if.
current = tokens.current();
if (!(current.get_type() == NL || current.get_type() == END)) {
parse_error("newline or end of file", current.get_value(), tokens);
}
if (current.get_type() == NL && remove_newline) {
tokens.eat(); // eat newline
}
return exp;
}
/*
* Will parse the debug token if encountered.
* CAUTION: Should only be called if you are sure the current token
* is a debug token.
* @param tokens the tokens object (stream) that should be parsed
* @return a debug expression
*/
Expression* parseDebug(Tokens& tokens) {
tokens.eat(); // eat debug keyword
return new Debug();
}
/*
* Will parse the return token if encountered.
* @param tokens the tokens object (stream) that should be parsed
* @return a return expression containing the return value
*/
Expression* parseReturn(Tokens& tokens) {
Token current = tokens.current();
// We check if we really got a return expression
if (!(current.get_type() == KEYWORD && current.get_value() == "return")){
parse_error("return <statement>", current.get_value(), tokens);
}
tokens.eat(); // eat return keyword
Statement* return_value = buildStatement(tokens);
return new Return(return_value);
}
/*
* Will parse the break token if encountered.
* @param tokens the tokens object (stream) that should be parsed
* @return a break expression
*/
Expression* parseBreak(Tokens& tokens) {
Token current = tokens.current();
if (!(current.get_type() == KEYWORD && current.get_value() == "break")){
parse_error("break", current.get_value(), tokens);
}
tokens.eat(); // eat break keyword
return new Break();
}
/*
* Will parse a function definition expression. This will consume
* the def token, the function name, function header and function body.
* @param tokens the tokens object (stream) that should be parsed
* @return a function definition expression
*/
Expression* parseFunctionDefinition(Tokens& tokens) {
Token current = tokens.current();
Token next = tokens.next();
// We check if we get a function definition
if (!(current.get_type() == KEYWORD && current.get_value() == "def")){
parse_error("def <function name>(<params>): <body>", current.get_value(), tokens);
}
tokens.eat(); // eat def keyword
current = tokens.current();
// Check for the function name after def
if (current.get_type() != VAR) {
parse_error("<function name>", current.get_value(), tokens);
}
tokens.eat(); // eat function name
std::string function_name = current.get_value();
current = tokens.current();
// Check if we get a opening parenthesis
if (current.get_type() != LPAREN) {
parse_error("(", current.get_value(), tokens);
}
tokens.eat(); // eat left parenthesis
std::vector<std::string> param_names = parseFunctionDefinitionParams(tokens);
current = tokens.current();
// Check if we get a closing parenthesis
if (current.get_type() != RPAREN) {
parse_error(")", current.get_value(), tokens);
}
tokens.eat(); // eat right parenthesis
current = tokens.current();
// Check if we get a colon
if (current.get_type() != COLON) {
parse_error(":", current.get_value(), tokens);
}
tokens.eat(); // eat colon
current = tokens.current();
bool one_line = false;
if (current.get_type() != NL) {
one_line = true;
}
else {
tokens.eat(); // eat newline
}
// Parse body of function definiton
Block* body = parseBody(tokens, one_line);
body->setBlockType(FUNC);
return new FunctionDefinition(function_name, param_names, body);
}
/*
* Will parse the function header (params of function)
* @param tokens the tokens object (stream) that should be parsed
* @return a vector of strings containing the name of the parameters
* @see parseFunctionDefinition method
*/
std::vector<std::string> parseFunctionDefinitionParams(Tokens& tokens) {
Token current = tokens.current();
Token next = tokens.next();
std::vector<std::string> param_names;
while (true) {
if(current.get_type() != VAR) {
parse_error("<variable name>", current.get_value(), tokens);
}
param_names.push_back(current.get_value()); // push var name to param names
tokens.eat();
if (next.get_type() != COMMA){
break;
}
tokens.eat();
current = tokens.current();
next = tokens.next();
}
return param_names;
}
/*
* Will parse a while expression. This will consume the while keyword,
* the while header and the while body.
* @param tokens the tokens object (stream) that should be parsed
* @return a while expression
*/
Expression* parseWhile(Tokens& tokens){
Token current = tokens.current();
Token next = tokens.next();
// We check if we get a while loop
if (!(current.get_type() == KEYWORD && current.value == "while")){
parse_error("while <condition>: <body>", current.value, tokens);
}
tokens.eat(); // eat while keyword
// Get the conditional Statement of the while loop
Statement* condition = buildStatement(tokens);
current = tokens.current();
// Check that a colon follows after the while condition
if (current.get_type() != COLON){
parse_error(":", current.value, tokens);
}
tokens.eat(); // Eat :
current = tokens.current();
bool one_line = false;
// if there is no newline then this will be a oneline while loop
if (current.get_type() != NL){
one_line = true;
}
else {
tokens.eat(); // eat newline after the while loop
}
// Parse body for while loop
Block* body = parseBody(tokens, one_line);
body->setBlockType(WHILE);
return new While(condition, body);
}
/*
* Will parse an if expression. This will consume the if keyword,
* the if header and the if body.
* @param tokens the tokens object (stream) that should be parsed
* @return an if expression
*/
Expression* parseIf(Tokens& tokens){
Token current = tokens.current();
Token next = tokens.next();
// Check if it is really a if statement
if (!(current.get_type() == KEYWORD && current.value == "if")){
parse_error("if <condition>: <body>", current.value, tokens);
}
tokens.eat(); // eat if keyword
// Get the conditional Statement of the if
Statement* condition = buildStatement(tokens);
current = tokens.current();
// Check if a colon is after the if
if (current.get_type() != COLON){
parse_error(":", current.value, tokens);
}
tokens.eat(); // eat the :
current = tokens.current();
// This checks if the if statement is a oneline or mutliline (by checking if there is a \n after the if)
bool one_line = false;
if (current.get_type() != NL){
one_line = true;
}
else {
tokens.eat(); // eat newline after the if
}
// Start parsing the body of the if
Block* body = parseBody(tokens, one_line);
body->setBlockType(IF);
return new If(condition, body);
}
/*
* Will parse an body of expressions.
* @param tokens the tokens object (stream) that should be parsed
* @return block expression
* @see parseIf, parseWhile, parseFor ...
*/
Block* parseBody(Tokens& tokens, bool one_line){
Token current = tokens.current();
std::vector<Expression*> expressions;
// TODO: do this in seperate function
// The body of the object will be one line. E.g. if, while, for in oneline
if (one_line){
// parse the "line" after the if
expressions.push_back(parseNextExpression(tokens, false));
return new Block(expressions);
}
// If we get to here the body is not oneline and the newline after the if was eaten. Meaning we are in a line beneath the if
// Parse new lines until we reach the end keyword to end the if block
while(current.value != "end") {
if (current.get_type() == END) {
parse_error("end keyword", "EOF", tokens);
}
expressions.push_back(parseNextExpression(tokens));
current = tokens.current();
}
tokens.eat(); // eat end keyword
return new Block(expressions);
}
/*
* Will parse an variable declarations.
* @param tokens the tokens object (stream) that should be parsed
* @return variable declaration expression
*/
Expression* parseDeclaration(Tokens& tokens){
Token current = tokens.current();
Token next = tokens.next();
Token after_next;
if (tokens.tokens.size()<=2) {
after_next = Token(END, "EOF");
}
else {
after_next = tokens.next(2);
}
// Check if we get an assignment meaning we need to have a variable name followed by exactly one equals.
if (!(current.get_type()==VAR && next.get_type() == EQUAL && after_next.get_type() != EQUAL)) {
parse_error("<variable_name> = <statement>", current.value + next.value, tokens);
}
// if we have a if then we can eat the name and the equal sign
tokens.eat(); // eat variable name
tokens.eat(); // eat equal sign
// Now we build the statement that the variable will be assigned to
Statement* assigned_statement = buildStatement(tokens);
std::string variable_name = current.value; // This will be the variable name
Expression* var_decl = new VariableDeclaration(variable_name, assigned_statement);
return var_decl;
}
/*
* Will parse an variable declarations.
* @param tokens the tokens object (stream) that should be parsed
* @return variable declaration expression
* NOTE: This function is similar to the parseDeclaration function but with a little difference.
* @see parseDeclaration
*/
Expression* parseForChangerDeclaration(Tokens& tokens){
Token current = tokens.current();
Token next = tokens.next();
Token after_next = tokens.next(2);
// Check if we get an assignment meaning we need to have a variable name followed by exactly one equals.
if (!(current.get_type()==VAR && next.get_type() == EQUAL && after_next.get_type() != EQUAL)) {
parse_error("<variable_name> = <statement>", current.value + next.value, tokens);
}
// if we have a if then we can eat the name and the equal sign
tokens.eat(); // eat variable name
tokens.eat(); // eat equal sign
// Now we build the statement that the variable will be assigned to
// Here is where is the difference between normal declaration
Token* lparen = new Token(LPAREN, "(");
Statement* assigned_statement = buildStatement(tokens, lparen);
std::string variable_name = current.value; // This will be the variable name
Expression* var_decl = new VariableDeclaration(variable_name, assigned_statement);
return var_decl;
}
/*
* Will parse a for expression. This will consume the for keyword,
* the for header and the for body.
* @param tokens the tokens object (stream) that should be parsed
* @return a for expression
*/
Expression* parseFor(Tokens& tokens){
Token current = tokens.current();
Token next = tokens.next();
// Check if we get a for
if (!(current.get_type()==KEYWORD && current.value == "for")) {
parse_error("for (<init>;<condition>;<changer>;) <body>", current.value, tokens);
}
// Because for loop "params" can not be parsed as one statement we will split into 3 things. First it will start with a left parenthesis
if (next.get_type() != LPAREN) {
parse_error("(", next.value, tokens);
}
tokens.eat(); // eat for keyword
tokens.eat(); // eat left parenthesis
// We first get the variable declaration for the init "statement" in the for
VariableDeclaration* init = (VariableDeclaration*) parseDeclaration(tokens);
// The variable declaration will end with a semicolon
current = tokens.current();
if (current.get_type() != SEMICOLON) {
parse_error(";", current.value, tokens);
}
tokens.eat(); // eat semicolon
// Build the conditional statement for the for loop
Statement* condition = buildStatement(tokens);
// The conditional will end with a semicolon
current = tokens.current();
if (current.get_type() != SEMICOLON) {
parse_error(";", current.value, tokens);
}
tokens.eat(); // eat semicolon
// Build the changer statement
VariableDeclaration* changer = (VariableDeclaration*) parseForChangerDeclaration(tokens);
// The changer will end with ): but the ) is eaten in the variable declaration.
current = tokens.current();
if (current.get_type() != COLON) {
parse_error(":", current.value, tokens);
}
tokens.eat(); // eat the colon
current = tokens.current();
bool one_line = false;
if (current.get_type() != NL){
one_line = true;
}
else {
tokens.eat(); // eat newline
}
Block* body = parseBody(tokens, one_line);
body->setBlockType(FOR);
return new For(init, condition, changer, body);
}
/*
* Overwrite for the stream operator for the stack<Statement*> type.
* @param os needed for the overwrite of this function
* @param stm needed for the overwrite of this function
* @returna osstream reference
*/
std::ostream& operator<<(std::ostream& os, const std::stack<Statement*> stm) {
std::stack<Statement*> stm_copy = stm;
while(!stm_copy.empty()){
os << stm_copy.top()->toString() << std::endl;
stm_copy.pop();
}
return os;
}
/*
* Build a statement from a list of tokens. Statements are things that are
* combinations of logical and arithmetic terms. Also function calls are
* allowed. Examples: a+b, a*b, 4-x+2, functionCall(3), ...
* @param tokens the tokens object (stream) that should be parsed
* @param optional_beginning_token Only used once for the for loop because otherwise this will not work
* its a bad solution but works for now
* @return a statement
*/
Statement* buildStatement(Tokens& tokens, Token* optional_beginning_token){
ReversePolishNotation rpn = convertInfixToRPN(tokens, optional_beginning_token);
Statement* stm = parseRPN(rpn, tokens);
return stm;
}
// ------------------------------------------------ Class Definitions ---------------------------------------------
Program::Program(std::vector<Expression*> expressions){
this->expressions = expressions;
}
Parser::Parser() {
}
Expression::Expression(){};
Statement::Statement() {}
VariableDeclaration::VariableDeclaration(std::string var_name, Statement* assigned_statement) {
this->var_name = var_name;
this->assigned_stm = assigned_statement;
}
std::string VariableDeclaration::toTreeString() {
return "VariableDeclaration(" + this->var_name + ";" + this->assigned_stm->toTreeString() + ")";
}
Block::Block(std::vector<Expression*> expressions){
this->expressions = expressions;
}
std::string Block::toTreeString() {
std::string ret_string = "Block(";
for (auto exp: this->expressions){
ret_string += exp->toTreeString() + ";";
}
ret_string.pop_back();
ret_string += ")";
return ret_string;
}
void Block::setBlockType(BlockType type) {
this->type = type;
}
BlockType Block::getBlockType() {
return this->type;
}
If::If(Statement* condition, Block* body){
this->condition = condition;
this->body = body;
}
std::string If::toTreeString() {
return "If(" + this->condition->toTreeString() + ";" + this->body->toTreeString() + ")";
}
While::While(Statement* condition, Block* body) {
this->condition = condition;
this->body = body;
}
std::string While::toTreeString() {
return "While(" + this->condition->toTreeString() + ";" + this->body->toTreeString() + ")";
}
For::For(VariableDeclaration* init, Statement* condition, VariableDeclaration* changer, Block* body) {
this->init = init;
this->condition = condition;
this->changer = changer;
this->body = body;
}
std::string For::toTreeString() {
return "For(" + this->init->toTreeString() + ";" + this->condition->toTreeString() + ";" + this->changer->toTreeString() + ";" + this->body->toTreeString() + ")";
}
std::string Program::toTreeString() {
std::string ret = "Program(";
for (auto expression: this->expressions){
ret = ret + expression->toTreeString() + ";";
}
// Remove last semicolon
ret.pop_back();
ret += ")";
return ret;
}
FunctionDefinition::FunctionDefinition(std::string function_name, std::vector<std::string> param_names, Block* body) {
this->name = function_name;
this->param_names = param_names;
this->body = body;
}
std::string FunctionDefinition::toTreeString() {
std::string ret = "FunctionDefinition(" + this->name + ";Params(";
for (auto name: param_names) {
ret = ret + name + ";";
}
ret.pop_back();
ret += ");";
ret += this->body->toTreeString();
ret += ")";
return ret;
}
Return::Return(Statement* ret_value) {
this->return_value = ret_value;
}
std::string Return::toTreeString() {
return "Return(" + this->return_value->toTreeString() + ")";
}
Break::Break() {};
std::string Break::toTreeString() {
return "Break()";
}
std::vector<Expression*> Block::getExpressions() {
return this->expressions;
}
Debug::Debug() {};
std::string Debug::toTreeString() {
return "Debug()";
}
void Statement::setType(ValueType type) {
this->type = type;
}
ValueType Statement::getType() {
return this->type;
}