-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParser.cpp
More file actions
353 lines (280 loc) · 10.9 KB
/
Parser.cpp
File metadata and controls
353 lines (280 loc) · 10.9 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
#include "Parser.h"
std::unique_ptr<Expression> Parser::ParseExpression() {
auto LeftSide = ParseUnaryExpression();
if (!LeftSide)
return nullptr;
return ParseRightSideOfBinaryOperator(0, std::move(LeftSide));
}
std::unique_ptr<Expression> Parser::ParsePrimaryExpression() {
switch (Lexer.GetCurrentToken()) {
case t_id:
return ParseIdExpression();
case t_num:
return ParseNumExpression();
case '(':
return ParseParenthesisExpression();
case t_when:
return ParseConditionalExpression();
case t_while:
return ParseLoopExpression();
case t_let:
return ParseVariableDefinition();
default:
return LogError<Expression>("unknown token while parsing expression");
}
}
std::unique_ptr<Expression> Parser::ParseIdExpression() {
auto Name = Lexer.GetIdVal();
Lexer.GetNextToken(); // consume id
if (Lexer.GetCurrentToken() != '(') // if it's not a function call then it's just a variable
return std::make_unique<VariableExpression>(Name);
Lexer.GetNextToken(); // consume '(' of function call
std::vector<std::unique_ptr<Expression>> Arguments;
if (Lexer.GetCurrentToken() != ')') {
while (true) {
auto Argument = ParseExpression();
if (Argument) {
Arguments.push_back(std::move(Argument));
} else {
return nullptr;
}
if (Lexer.GetCurrentToken() == ')')
break;
if (Lexer.GetCurrentToken() != ',')
return LogError<Expression>("expected ')' or ','");
Lexer.GetNextToken(); // consume ','
}
}
Lexer.GetNextToken(); // consume ')'
return std::make_unique<FunctionCall>(Name, std::move(Arguments));
}
std::unique_ptr<Expression> Parser::ParseNumExpression() {
auto Num = std::make_unique<NumExpression>(Lexer.GetNumVal());
Lexer.GetNextToken(); // consume number
return std::move(Num);
}
// parse: '(' expr ')'
std::unique_ptr<Expression> Parser::ParseParenthesisExpression() {
Lexer.GetNextToken(); // consume '('
auto Value = ParseExpression();
if (!Value)
return nullptr;
if (Lexer.GetCurrentToken() != ')')
return LogError<Expression>("expected ')'");
Lexer.GetNextToken(); // consume ')'
return Value;
}
std::unique_ptr<Expression> Parser::ParseUnaryExpression() {
int CurrentToken = Lexer.GetCurrentToken();
if (CurrentToken == '(' || CurrentToken == ',' || !isascii(CurrentToken)) {
return ParsePrimaryExpression();
}
int Operator = CurrentToken;
Lexer.GetNextToken(); // consume operator
if (auto Operand = ParseUnaryExpression())
return std::make_unique<UnaryExpression>(Operator, std::move(Operand));
return nullptr;
}
/// parse: 'when' expr 'then' expr 'otherwise' expr
std::unique_ptr<Expression> Parser::ParseConditionalExpression() {
Lexer.GetNextToken(); // consume 'when'
auto Condition = ParseExpression();
if (!Condition) {
return nullptr;
}
if (Lexer.GetCurrentToken() != t_then)
return LogError<Expression>("expected 'then'");
Lexer.GetNextToken(); // consume 'then'
auto Then = ParseExpression();
if (!Then) {
return nullptr;
}
if (Lexer.GetCurrentToken() != t_otherwise)
return LogError<Expression>("expected 'otherwise'");
Lexer.GetNextToken(); // consume 'otherwise'
auto Otherwise = ParseExpression();
if (!Otherwise) {
return nullptr;
}
return std::make_unique<ConditionalExpression>(std::move(Condition), std::move(Then), std::move(Otherwise));
}
/// parse: 'while' expr 'let' id '=' expr ('step' expr)? 'do' expr
std::unique_ptr<Expression> Parser::ParseLoopExpression() {
Lexer.GetNextToken(); // consume 'while'
auto While = ParseExpression();
if (!While) {
return nullptr;
}
if (Lexer.GetCurrentToken() != t_let)
return LogError<Expression>("expected 'let'");
Lexer.GetNextToken(); // consume 'let'
if (Lexer.GetCurrentToken() != t_id)
return LogError<Expression>("expected id");
std::string Name = Lexer.GetIdVal();
Lexer.GetNextToken(); // consume id
if (Lexer.GetCurrentToken() != '=')
return LogError<Expression>("expected '='");
Lexer.GetNextToken(); // consume '='
auto Let = ParseExpression();
if (!Let) {
return nullptr;
}
// optional step
std::unique_ptr<Expression> Step;
if (Lexer.GetCurrentToken() == t_step) {
Lexer.GetNextToken(); // consume 'step'
Step = ParseExpression();
if (!Step) {
return nullptr;
}
}
if (Lexer.GetCurrentToken() != t_do)
return LogError<Expression>("expected 'do'");
Lexer.GetNextToken(); // consume 'do'
auto Body = ParseExpression();
if (!Body) {
return nullptr;
}
return std::make_unique<LoopExpression>(Name, std::move(Let), std::move(While), std::move(Step), std::move(Body));
}
/// parse: 'let' id ('=' expr)? (',' id ('=' expr)?)* 'in' expr
std::unique_ptr<Expression> Parser::ParseVariableDefinition() {
Lexer.GetNextToken(); // consume 'let'
if (Lexer.GetCurrentToken() != t_id)
return LogError<Expression>("expected id");
std::vector<std::pair<std::string, std::unique_ptr<Expression>>> Variables;
while (true) {
std::string Name = Lexer.GetIdVal();
Lexer.GetNextToken(); // consume id
// optional initializer
std::unique_ptr<Expression> Initializer;
if (Lexer.GetCurrentToken() == '=') {
Lexer.GetNextToken(); // consume '='
Initializer = ParseExpression();
if (!Initializer) {
return nullptr;
}
}
Variables.emplace_back(Name, std::move(Initializer));
if (Lexer.GetCurrentToken() != ',') {
break;
}
Lexer.GetNextToken(); // consume ','
if (Lexer.GetCurrentToken() != t_id)
return LogError<Expression>("expected list of ids");
}
if (Lexer.GetCurrentToken() != t_in)
return LogError<Expression>("expected 'in'");
Lexer.GetNextToken(); // consume 'in'
auto Body = ParseExpression();
if (!Body)
return nullptr;
return std::make_unique<VariableDefinition>(std::move(Variables), std::move(Body));
}
int Parser::GetTokenPrecedence() {
if (!isascii(Lexer.GetCurrentToken()))
return -1;
int Precedence = BinaryOperatorPrecedences[(char) Lexer.GetCurrentToken()];
if (Precedence <= 0)
return -1;
return Precedence;
}
std::unique_ptr<Expression> Parser::ParseRightSideOfBinaryOperator(int ExpressionPrecedence,
std::unique_ptr<Expression> LeftSide) {
while (true) {
int TokenPrecedence = GetTokenPrecedence();
if (TokenPrecedence < ExpressionPrecedence)
return LeftSide;
int BinaryOperator = Lexer.GetCurrentToken();
Lexer.GetNextToken(); // consume binary operator
auto RightSide = ParseUnaryExpression();
if (!RightSide)
return nullptr;
int NextPrecedence = GetTokenPrecedence();
if (TokenPrecedence < NextPrecedence) {
RightSide = ParseRightSideOfBinaryOperator(
TokenPrecedence + 1,
std::move(RightSide)
);
if (!RightSide)
return nullptr;
}
LeftSide = std::make_unique<BinaryExpression>(
BinaryOperator,
std::move(LeftSide),
std::move(RightSide)
);
}
}
std::unique_ptr<FunctionDeclaration> Parser::ParseFunctionDeclaration() {
std::string Name;
int Type; // 0 = id, 1 = unary, 2 = binary
switch (Lexer.GetCurrentToken()) {
default:
return LogError<FunctionDeclaration>("expected function name in declaration");
case t_id:
Name = Lexer.GetIdVal();
Type = 0;
Lexer.GetNextToken(); // consume id
break;
case t_unary:
Lexer.GetNextToken(); // consume 'unary'
if (!isascii(Lexer.GetCurrentToken()))
return LogError<FunctionDeclaration>("expected unary operator");
Name = std::string("unary") + ((char) Lexer.GetCurrentToken());
Type = 1;
Lexer.GetNextToken(); // consume operator symbol
break;
case t_binary:
Lexer.GetNextToken(); // consume 'binary'
if (!isascii(Lexer.GetCurrentToken()))
return LogError<FunctionDeclaration>("expected binary operator");
char Operator = (char) Lexer.GetCurrentToken();
Name = std::string("binary") + Operator;
Type = 2;
int Precedence = 30;
Lexer.GetNextToken(); // consume operator symbol
// optional operator precedence
if (Lexer.GetCurrentToken() == t_num) {
double Num = Lexer.GetNumVal();
if (Num > 100 || Num < 1)
return LogError<FunctionDeclaration>("Precedence must be in range 1..100");
Precedence = (int) Num;
Lexer.GetNextToken(); // consume operator precedence
}
BinaryOperatorPrecedences[Operator] = Precedence;
break;
}
if (Lexer.GetCurrentToken() != '(')
return LogError<FunctionDeclaration>("expected '('");
std::vector<std::string> ArgumentNames;
while (Lexer.GetNextToken() == t_id)
ArgumentNames.push_back(Lexer.GetIdVal());
if (Lexer.GetCurrentToken() != ')')
return LogError<FunctionDeclaration>("expected ')'");
Lexer.GetNextToken(); // consume ')'
if ((Type == 1 && ArgumentNames.size() != 1) || (Type == 2 && ArgumentNames.size() != 2))
return LogError<FunctionDeclaration>("invalid number of operands for unary/binary operator");
return std::make_unique<FunctionDeclaration>(Name, std::move(ArgumentNames));
}
std::unique_ptr<FunctionDefinition> Parser::ParseFunctionDefinition() {
Lexer.GetNextToken(); // consume 'func'/'operator'
auto Declaration = ParseFunctionDeclaration();
if (!Declaration)
return nullptr;
auto Body = ParseExpression();
if (!Body)
return nullptr;
return std::make_unique<FunctionDefinition>(std::move(Declaration), std::move(Body));
}
std::unique_ptr<FunctionDeclaration> Parser::ParseNative() {
Lexer.GetNextToken(); // consume 'native'
return ParseFunctionDeclaration();
}
std::unique_ptr<FunctionDefinition> Parser::ParseTopLevelExpression() {
auto Body = ParseExpression();
if (!Body)
return nullptr;
auto Declaration = std::make_unique<FunctionDeclaration>("__anonymous_top_level_expr", std::vector<std::string>());
return std::make_unique<FunctionDefinition>(std::move(Declaration), std::move(Body));
}