-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.cpp
279 lines (218 loc) · 5.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
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
#include "Parser.h"
#include "Lexer.h"
#include "Common.h"
#include <map>
int curToken;
static map<char, int> binOperatorPrecedence;
int getNextToken()
{
curToken = getToken();
// printf("%i %s\n", curToken, identifierStr.c_str()); // For debug
return curToken;
}
unique_ptr<ExpressionAST> logError(const char *errorStr)
{
printf("Error: %s\n", errorStr);
return nullptr;
}
unique_ptr<PrototypeAST> logErrorProto(const char *errorStr)
{
logError(errorStr);
return nullptr;
}
unique_ptr<ExpressionAST> parseNumberExpr()
{
auto result = make_unique<NumberExpAST>(numVal);
getNextToken();
return move(result);
}
unique_ptr<ExpressionAST> parseParenthesesExpr()
{
getNextToken();
auto value = parseExpression();
if (!value)
return nullptr;
if (curToken != ')')
return logError("Expected ')'");
getNextToken();
return value;
}
unique_ptr<ExpressionAST> parseIdentifierExpr()
{
string nameId = identifierStr;
getNextToken();
if (curToken != '(')
return make_unique<VariableExpAST>(nameId);
getNextToken();
vector<unique_ptr<ExpressionAST>> args;
if (curToken != ')')
{
while (true)
{
if (auto arg = parseExpression())
args.push_back(move(arg));
else
return nullptr;
if (curToken == ')')
break;
if (curToken != ',')
return logError("Expected ','");
getNextToken();
}
}
getNextToken();
return make_unique<CallExpressionAST>(nameId, move(args));
}
unique_ptr<ExpressionAST> parsePrimary()
{
switch (curToken)
{
case tok_identifier:
return parseIdentifierExpr();
case tok_number:
return parseNumberExpr();
case '(':
return parseParenthesesExpr();
case tok_if:
return parseIfExpresion();
case tok_for:
return parseForExpresion();
default:
return logError("Unknown token");
}
}
int getTokPrecedence()
{
if (!isascii(curToken))
return -1;
int tokPrec = binOperatorPrecedence[curToken];
if (tokPrec < 0)
return -1;
return tokPrec;
}
void initialBinOpPrecs()
{
binOperatorPrecedence['<'] = 10;
binOperatorPrecedence['-'] = 20;
binOperatorPrecedence['+'] = 20;
binOperatorPrecedence['*'] = 40;
}
unique_ptr<ExpressionAST> parseBinaryOpRHS(int opCodePrec, unique_ptr<ExpressionAST> lhs)
{
while (true)
{
int tokPrec = getTokPrecedence();
if (tokPrec <= opCodePrec)
return lhs;
int binOp = curToken;
getNextToken();
auto rhs = parsePrimary();
if (!rhs)
return nullptr;
int nextPrec = getTokPrecedence();
if (tokPrec <= nextPrec)
{
rhs = parseBinaryOpRHS(tokPrec + 1, move(rhs));
if (!rhs)
return nullptr;
}
lhs = make_unique<BinaryExpAST>(binOp, move(lhs), move(rhs));
}
}
unique_ptr<ExpressionAST> parseExpression()
{
auto lhs = parsePrimary();
if (!lhs)
return nullptr;
return parseBinaryOpRHS(0, move(lhs));
}
unique_ptr<PrototypeAST> parsePrototype()
{
if (curToken != tok_identifier)
return logErrorProto("Expected Function name in ");
string funcName = identifierStr;
getNextToken();
if (curToken != '(')
return logErrorProto("Expected '(' in prototype");
vector<string> argNames;
while (getNextToken() == tok_identifier)
argNames.push_back(identifierStr);
if (curToken != ')')
return logErrorProto("Expected ')' in prototype");
getNextToken();
return make_unique<PrototypeAST>(funcName, move(argNames));
}
unique_ptr<FunctionExpressionAST> parseDefinition()
{
getNextToken();
auto prototype = parsePrototype();
if (!prototype)
return nullptr;
if (auto body = parseExpression())
return make_unique<FunctionExpressionAST>(move(prototype), move(body));
return nullptr;
}
unique_ptr<FunctionExpressionAST> parseTopLevelExpression()
{
if (auto exp = parseExpression())
{
auto prototype = make_unique<PrototypeAST>("__anon_expr", vector<string>());
return make_unique<FunctionExpressionAST>(move(prototype), move(exp));
}
return nullptr;
}
unique_ptr<ExpressionAST> parseIfExpresion()
{
getNextToken();
auto condition = parseExpression();
if (!condition)
return nullptr;
if (curToken != tok_then)
return logError("expected then");
getNextToken();
auto thenStmt = parseExpression();
if (!thenStmt)
return nullptr;
if (curToken != tok_else)
return logError("expected else");
getNextToken();
auto elseStmt = parseExpression();
if (!elseStmt)
return nullptr;
return make_unique<IfExpressionAST>(move(condition), move(thenStmt), move(elseStmt));
}
unique_ptr<ExpressionAST> parseForExpresion()
{
getNextToken();
if (curToken != tok_identifier)
return logError("expected identifier after for");
string idName = identifierStr;
getNextToken();
if (curToken != '=')
return logError("expected '='after identifier");
getNextToken();
auto start = parseExpression();
if (!start)
return nullptr;
if (curToken != ',')
return logError("expected ',' after initialization");
getNextToken();
auto end = parseExpression();
if (!end)
return nullptr;
unique_ptr<ExpressionAST> step;
if (curToken == ',')
{
getNextToken();
step = parseExpression();
if (!step)
return nullptr;
}
if (curToken != tok_in)
return logError("expected 'in' at the end of the loop");
getNextToken();
auto body = parseExpression();
if (!body)
return nullptr;
return make_unique<ForExpressionAST>(idName, move(start), move(end), move(step), move(body));
}