-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
202 lines (186 loc) · 4.9 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
#include <string>
#include <iostream>
#include "parser.h"
using namespace std;
ASTNode::ASTNode(NodeType tt) {
this->nodeType = tt;
}
Parser::Parser(Lexer l): lx(l) {
currToken = lx.getNextToken();
}
void Parser::eat(TokenType tokenType) {
if(this->currToken.tokenType == tokenType) {
this->currToken = lx.getNextToken();
}
else {
cerr << "Expected token " << tokenType << " but instead received " << this->currToken.tokenType << endl;
throw UNEXPECTED_TOKEN;
}
}
ASTNode* Parser::print() {
ASTNode* printRoot = new ASTNode(NODE_PRINT);
eat(PRINT);
eat(OPAREN);
ASTNode* printExpression = rvalue();
printRoot->children.push_back(printExpression);
eat(CPAREN);
return printRoot;
}
ASTNode* Parser::loop() {
eat(WHILE); eat(OPAREN);
ASTNode* condition = rvalue();
eat(CPAREN); eat(OBRACE);
ASTNode* loopBody = statementList();
eat(CBRACE);
ASTNode* loopRoot = new ASTNode(NODE_LOOP);
loopRoot->children.push_back(condition);
loopRoot->children.push_back(loopBody);
return loopRoot;
}
ASTNode* Parser::conditional() {
eat(IF); eat(OPAREN);
ASTNode* condition = rvalue();
eat(CPAREN); eat(OBRACE);
ASTNode* trueBlock = statementList();
eat(CBRACE); eat(ELSE); eat(OBRACE);
ASTNode* falseBlock = statementList();
eat(CBRACE);
ASTNode* conditionalRoot = new ASTNode(NODE_CONDITIONAL);
conditionalRoot->children.push_back(condition);
conditionalRoot->children.push_back(trueBlock);
conditionalRoot->children.push_back(falseBlock);
return conditionalRoot;
}
ASTNode* Parser::statementList() {
ASTNode* statementList = new ASTNode(NODE_COMPOUND);
statementList->children.push_back(statement());
while(this->currToken.tokenType == SEMI) {
eat(SEMI);
statementList->children.push_back(statement());
}
return statementList;
}
ASTNode* Parser::statement() {
if(this->currToken.tokenType == ID)
return assignment();
else if(this->currToken.tokenType == IF) {
return conditional();
} else if(this->currToken.tokenType == WHILE) {
return loop();
} else if(this->currToken.tokenType == PRINT) {
return print();
} else {
return new ASTNode(NODE_NOOP);
}
}
ASTNode* Parser::rvalue() {
ASTNode* expr = expression();
TokenType tt = this->currToken.tokenType;
if(tt == LESSTHAN || tt == GREATERTHAN || tt == EQUAL) {
ASTNode* leftExpr = expr;
NodeType nt = NODE_NOOP;
if(tt == LESSTHAN) {
eat(LESSTHAN);
nt = NODE_LESS_THAN;
}
else if(tt == GREATERTHAN) {
eat(GREATERTHAN);
nt = NODE_GREATER_THAN;
}
else {
eat(EQUAL);
eat(EQUAL);
nt = NODE_EQUAL_TO;
}
ASTNode* rightExpr = expression();
ASTNode* comparisonRoot = new ASTNode(nt);
comparisonRoot->children.push_back(leftExpr);
comparisonRoot->children.push_back(rightExpr);
return comparisonRoot;
} else {
return expr;
}
}
ASTNode* Parser::assignment() {
ASTNode* varRoot = variable();
eat(EQUAL);
ASTNode* rvalueRoot = rvalue();
ASTNode* assignRoot = new ASTNode(NODE_ASSIGN);
assignRoot->children.push_back(varRoot);
assignRoot->children.push_back(rvalueRoot);
return assignRoot;
}
ASTNode* Parser::expression() {
ASTNode* exprRoot = divmul();
TokenType tt = currToken.tokenType;
NodeType nt = NODE_NOOP;
while(tt==ADD || tt==SUB) {
if(tt==ADD) {
eat(ADD);
nt = NODE_ADD;
}
else if(tt==SUB) {
eat(SUB);
nt = NODE_SUB;
}
ASTNode* rightChild = divmul();
ASTNode* newExprRoot = new ASTNode(nt);
newExprRoot->children.push_back(exprRoot);
newExprRoot->children.push_back(rightChild);
exprRoot = newExprRoot;
tt = currToken.tokenType;
}
return exprRoot;
}
ASTNode* Parser::divmul() {
ASTNode* factorRoot = factor();
TokenType tt = currToken.tokenType;
NodeType nt = NODE_NOOP;
while(tt==MULT || tt==DIV) {
if(tt==MULT) {
eat(MULT);
nt = NODE_MULT;
}
else if(tt==DIV) {
eat(DIV);
nt = NODE_DIV;
}
ASTNode* rightChild = factor();
ASTNode* newFactorRoot = new ASTNode(nt);
newFactorRoot->children.push_back(factorRoot);
newFactorRoot->children.push_back(rightChild);
factorRoot = newFactorRoot;
tt = currToken.tokenType;
}
return factorRoot;
}
ASTNode* Parser::factor() {
Token t = currToken;
TokenType tt = currToken.tokenType;
if(tt == NUM) {
eat(NUM);
ASTNode* numLeaf = new ASTNode(NODE_NUM);
numLeaf->num = stoi(t.value);
return numLeaf;
} else if(tt == SUB) {
eat(SUB);
ASTNode* negRoot = new ASTNode(NODE_NEG);
negRoot->children.push_back(factor());
return negRoot;
} else if(tt == OPAREN){
eat(OPAREN);
ASTNode* subExprRoot = expression();
eat(CPAREN);
return subExprRoot;
} else {
ASTNode* varRoot = variable();
return varRoot;
}
}
ASTNode* Parser::variable() {
Token idToken = currToken;
eat(ID);
ASTNode* idLeaf = new ASTNode(NODE_ID);
idLeaf->id = idToken.value;
return idLeaf;
}