-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.cpp
More file actions
220 lines (196 loc) · 7.22 KB
/
parser.cpp
File metadata and controls
220 lines (196 loc) · 7.22 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
#include "parser.h"
#include "expressionNodes.h"
TreeNode* Parser::parseWhileStatement() {
TreeNode* condition = parseExpression();
expectNextToken(Loop);
TreeNode* body = parseBody();
return new WhileNode(condition, body);
}
TreeNode* Parser::parseIfStatement() {
TreeNode *condition = parseExpression();
expectNextToken(Then);
TreeNode *trueBody = parseBody();
if (checkCurrentToken(Else)) {
TreeNode *falseBody = parseBody();
expectCurrentToken(End);
return new IfNode(condition, trueBody, falseBody);
} else if (checkCurrentToken(End))
return new IfNode(condition, trueBody);
else
throwError("'end' or 'else' expected");
}
TreeNode* Parser::parseAssignmentStatement() {
std::string varName = (*currentToken)->getImage();
int token = getNextToken();
if (token == Dot) {
expectNextToken(Identifier);
std::string fieldName = (*currentToken)->getImage();
expectNextToken(Assignment);
TreeNode* initializer = parseExpression();
return new FieldAssignmentNode(varName, fieldName, initializer);
} else if (token == Assignment) {
TreeNode* initializer = parseExpression();
return new VarAssignmentNode(varName, initializer);
} else
throwError("':=' expected");
}
TreeNode* Parser::parseStatement() {
switch ((*currentToken)->getType()) {
case While:
return parseWhileStatement();
case If:
return parseIfStatement();
case Return:
return new ReturnNode(parseExpression());
case Identifier:
return parseAssignmentStatement();
default:
throwError("unknown expression");
}
}
TreeNode* Parser::parseIdentifier() {
std::string varName = (*currentToken)->getImage();
int token = getNextToken();
if(token == Dot) {
expectNextToken(Identifier);
std::string memberName = (*currentToken)->getImage();
if (!checkNextToken(LeftRoundBracket)) {
--currentToken;
return new FieldAccessNode(varName, memberName);
}
std::vector<TreeNode*> args;
if (getNextToken()==RightRoundBracket)
return new MethodInvokeNode(varName, memberName, args);
--currentToken;
do{
args.push_back(parseExpression());
} while (checkNextToken(Comma));
expectCurrentToken(RightRoundBracket);
return new MethodInvokeNode(varName, memberName, args);
}
else if (token == LeftRoundBracket){
std::vector<TreeNode*> args;
if (getNextToken()==RightRoundBracket)
return new ConstructorInvokeNode(varName, args);
--currentToken;
do{
args.push_back(parseExpression());
} while (checkNextToken(Comma));
expectCurrentToken(RightRoundBracket);
return new ConstructorInvokeNode(varName, args);
}{
--currentToken;
return new VariableNode(varName);
}
}
TreeNode* Parser::parseExpression(){
switch (getNextToken()) {
case RealLiteral:
return new RealLiteralNode((*currentToken)->getImage());
case IntegerLiteral:
return new IntegerLiteralNode((*currentToken)->getImage());
case BooleanLiteral:
return new BooleanLiteralNode((*currentToken)->getImage());
case Identifier:
return parseIdentifier();
default:
throwError("literal or identifier expected");
}
}
TreeNode* Parser::parseBody() {
std::unordered_map <std::string, std::string> symbols;
std::vector <TreeNode*> statements;
int token;
while ( (token = getNextToken()) != End && token != Else) {
if(token == Var) {
expectNextToken(Identifier);
std::string varName = (*currentToken)->getImage();
symbols[(*currentToken)->getImage()] = varName;
expectNextToken(Colon);
TreeNode* newValueExpression = parseExpression();
statements.push_back(new VarDeclarationNode(varName, newValueExpression));
}
else
statements.push_back(parseStatement());
}
return new BodyNode(symbols, statements);
}
ParamNode* Parser::parseParameter() {
expectNextToken(Identifier);
std::string paramName = (*currentToken)->getImage();
expectNextToken(Colon);
expectNextToken(Identifier);
std::string paramType = (*currentToken)->getImage();
return new ParamNode(paramName, paramType);
}
std::vector<ParamNode*> Parser::parseParameters() {
std::vector<ParamNode*> params;
expectNextToken(LeftRoundBracket);
if (getNextToken()==RightRoundBracket)
return params;
--currentToken;
do{
params.push_back(parseParameter());
} while (checkNextToken(Comma));
expectCurrentToken(RightRoundBracket);
return params;
}
ClassBodyNode* Parser::parseClassBody() {
std::unordered_map <std::string, std::string> symbols;
std::vector <TreeNode*> methods;
std::vector <FieldDeclarationNode*> varDeclarations;
TreeNode* constructor = nullptr;
while (!checkNextToken(End)) {
if(checkCurrentToken(Var)) {
expectNextToken(Identifier);
std::string varName = (*currentToken)->getImage();
symbols[(*currentToken)->getImage()] = varName;
expectNextToken(Colon);
TreeNode* newValueExpression = parseExpression();
varDeclarations.push_back(new FieldDeclarationNode(varName, newValueExpression));
}
else if (checkCurrentToken(Method)) {
expectNextToken(Identifier);
std::string methodName = (*currentToken)->getImage();
std::vector<ParamNode*> params = parseParameters();
std::string returnTypeName = "";
if (checkNextToken(Colon)) {
expectNextToken(Identifier);
returnTypeName = (*currentToken)->getImage();
}
expectNextToken(Is);
TreeNode* body = parseBody();
methods.push_back(new MethodDeclarationNode(methodName, returnTypeName, params, body));
} else if (checkCurrentToken(This)) {
std::vector<ParamNode*> params = parseParameters();
expectNextToken(Is);
TreeNode* body = parseBody();
constructor = new ConstructorDeclarationNode(params, body);
} else
throwError("'var', 'method', 'this' expected");
}
currentToken++;
return new ClassBodyNode(symbols, varDeclarations, methods, constructor);
}
TreeNode* Parser::parseClass(){
expectCurrentToken(Class);
expectNextToken(Identifier);
std::string className = (*currentToken)->getImage();
std::string classTemplate = "";
++currentToken;
if (checkCurrentToken(LeftRectBracket)) {
expectNextToken(Identifier);
classTemplate = (*currentToken)->getImage();
expectNextToken(RightRectBracket);
++currentToken;
}
std::string baseClass = "";
if (checkCurrentToken(Extends)) {
expectNextToken(Identifier);
baseClass = (*currentToken)->getImage();
++currentToken;
}
expectCurrentToken(Is);
ClassBodyNode* classBody = parseClassBody();
return new ClassNode(className, classTemplate, baseClass, classBody);
}