-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
372 lines (309 loc) · 12.1 KB
/
parse.py
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
from parse_result import *
from constants import *
from error import InvalidSyntaxError
from node import *
class Parser:
# tokens -> list of Tokens provided by lexer
# token_index -> int index tracker
# current_token -> currently referenced token
def __init__(self, tokens):
self.current_token = None
self.tokens = tokens
self.token_index = -1
self.advance()
# Traverse the tokens
def advance(self):
self.token_index += 1
if self.token_index < len(self.tokens):
self.current_token = self.tokens[self.token_index]
return self.current_token
# parse the expression
def parse(self):
res = self.expr()
if not res.error and (self.current_token.type != TT_EOF and self.current_token.type != TT_IDENTIFIER):
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
"Expected '+', '-', '*', '/', '^', '==', '!=', '<', '>', <=', '>=', 'AND' or 'OR'"
))
return res
# handle simple atomic parsing
def atom(self):
res = ParseResult()
token = self.current_token
# If integer or float return a NumberNode of that token
if token.type in (TT_INT, TT_FLOAT):
res.register_advancement()
self.advance()
return res.success(NumberNode(token))
# handle variable names
elif token.type == TT_IDENTIFIER:
res.register_advancement()
self.advance()
return res.success(VarAccessNode(token))
# handle expressions within parentheses
elif token.type == TT_LPAREN:
res.register_advancement()
self.advance()
expr = res.register(self.expr())
if res.error:
return res
if self.current_token.type == TT_RPAREN:
res.register_advancement()
self.advance()
return res.success(expr)
else:
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
"Expected ')'"
))
# handle conditions
elif token.matches(TT_KEYWORD, 'IF'):
if_expr = res.register(self.if_expr())
if res.error:
return res
return res.success(if_expr)
# handle for loops
elif token.matches(TT_KEYWORD, 'FOR'):
for_expr = res.register(self.for_expr())
if res.error:
return res
return res.success(for_expr)
# handle while loops
elif token.matches(TT_KEYWORD, 'WHILE'):
while_expr = res.register(self.while_expr())
if res.error:
return res
return res.success(while_expr)
return res.failure(InvalidSyntaxError(
token.pos_start, token.pos_end,
"Expected int, float, '+', '-' or '('"
))
# handle power operation parsing
def power(self):
return self.bin_op(self.atom, (TT_POW,), self.factor)
# handle factor parsing
def factor(self):
res = ParseResult()
token = self.current_token
if token.type in (TT_PLUS, TT_MINUS):
res.register_advancement()
self.advance()
factor = res.register(self.factor())
if res.error:
return res
return res.success(UnaryOpNode(token, factor))
return self.power()
# handle term parsing
def term(self):
return self.bin_op(self.factor, (TT_MUL, TT_DIV))
# handle arithmetic expression parsing
def arith_expr(self):
return self.bin_op(self.term, (TT_PLUS, TT_MINUS))
# handle conditional expression parsing
def if_expr(self):
res = ParseResult()
cases = []
else_case = None
if not self.current_token.matches(TT_KEYWORD, 'IF'):
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
f"Expected 'if' or 'IF'"
))
res.register_advancement()
self.advance()
condition = res.register(self.expr())
if res.error:
return res
if not self.current_token.matches(TT_KEYWORD, 'THEN'):
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
f"Expected 'then' or 'THEN'"
))
res.register_advancement()
self.advance()
expr = res.register(self.expr())
if res.error:
return res
cases.append((condition, expr))
while self.current_token.matches(TT_KEYWORD, 'ELIF'):
res.register_advancement()
self.advance()
condition = res.register(self.expr())
if res.error:
return res
if not self.current_token.matches(TT_KEYWORD, 'THEN'):
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
f"Expected 'then' or 'THEN'"
))
res.register_advancement()
self.advance()
expr = res.register(self.expr())
if res.error:
return res
# will hold conditions with their expressions (if CONDITION then EXPRESSION)
cases.append((condition, expr))
if self.current_token.matches(TT_KEYWORD, 'ELSE'):
res.register_advancement()
self.advance()
else_case = res.register(self.expr())
if res.error:
return res
return res.success(IfNode(cases, else_case))
# handle for loop parsing
def for_expr(self):
res = ParseResult()
if not self.current_token.matches(TT_KEYWORD, 'FOR'):
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
f"Expected 'FOR'"
))
res.register_advancement()
self.advance()
if self.current_token.type != TT_IDENTIFIER:
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
f"Expected identifier"
))
var_name = self.current_token
res.register_advancement()
self.advance()
if self.current_token.type != TT_EQ:
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
f"Expected '='"
))
res.register_advancement()
self.advance()
start_value = res.register(self.expr())
if res.error:
return res
if not self.current_token.matches(TT_KEYWORD, 'TO'):
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
f"Expected 'TO'"
))
res.register_advancement()
self.advance()
end_value = res.register(self.expr())
if res.error:
return res
if self.current_token.matches(TT_KEYWORD, 'STEP'):
res.register_advancement()
self.advance()
step_value = res.register(self.expr())
if res.error:
return res
else:
step_value = None
if not self.current_token.matches(TT_KEYWORD, 'THEN'):
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
f"Expected 'THEN'"
))
res.register_advancement()
self.advance()
body = res.register(self.expr())
if res.error:
return res
return res.success(ForNode(var_name, start_value, end_value, step_value, body))
# handle while loop parsing
def while_expr(self):
res = ParseResult()
if not self.current_token.matches(TT_KEYWORD, 'WHILE'):
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
f"Expected 'WHILE'"
))
res.register_advancement()
self.advance()
condition = res.register(self.expr())
if res.error:
return res
if not self.current_token.matches(TT_KEYWORD, 'THEN'):
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
f"Expected 'THEN'"
))
res.register_advancement()
self.advance()
body = res.register(self.expr())
if res.error:
return res
return res.success(WhileNode(condition, body))
# handle comparison expression parsing
def comp_expr(self):
res = ParseResult()
if self.current_token.matches(TT_KEYWORD, 'NOT'):
operator_token = self.current_token
res.register_advancement()
self.advance()
node = res.register(self.comp_expr())
if res.error:
return res
return res.success(UnaryOpNode(operator_token, node))
node = res.register(self.bin_op(self.arith_expr, (TT_EE, TT_NE, TT_LT, TT_GT, TT_LTE, TT_GTE)))
if res.error:
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
"Expected int, float, '+', '-' or '(', 'NOT"
))
return res.success(node)
# Handle overall expression parsing - create respective Nodes based on token type
def expr(self):
res = ParseResult()
# Variable assignment
if self.current_token.matches(TT_KEYWORD, 'SAVE'):
res.register_advancement()
self.advance()
# once the keyword is advanced only an identifier is accepted
if self.current_token.type != TT_IDENTIFIER:
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
'Expected identifier'
))
# valid identifier
var_name = self.current_token
res.register_advancement()
self.advance()
# after an identifier an expression is accepted
expr = res.register(self.expr())
if res.error:
return res
return res.success(VarAssignNode(var_name, expr))
# variable access
elif self.current_token.matches(TT_KEYWORD, 'SHOW'):
res.register_advancement()
self.advance()
# once the keyword is advanced only an identifier is accepted
if self.current_token.type != TT_IDENTIFIER:
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
'Expected identifier'
))
return res.success(VarAccessNode(self.current_token))
node = res.register(self.bin_op(self.comp_expr, ((TT_KEYWORD, 'AND'), (TT_KEYWORD, 'OR'))))
# invalid input
if res.error:
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
'Expected Keyword, \'+\', \'-\', \'(\', or \'NOT\''
))
return res.success(node)
# ops - operators | func - term/factor based on the grammar that's defined
def bin_op(self, func_a, ops, func_b=None):
if func_b is None:
func_b = func_a
res = ParseResult()
left = res.register(func_a())
if res.error:
return res
while self.current_token.type in ops or (self.current_token.type, self.current_token.value) in ops:
op_tok = self.current_token
res.register_advancement()
self.advance()
right = res.register(func_b())
if res.error:
return res
left = BinOpNode(left, op_tok, right)
return res.success(left)