-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
executable file
·420 lines (344 loc) · 11 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import sly
from lex import Lexer
from obj_model import *
class Parser(sly.Parser):
tokens = Lexer.tokens
@_("expr LINE_TERM program")
def program(self, p):
return BlockNode(p.expr, p.program)
@_("expr LINE_TERM")
def program(self, p):
return p.expr
@_("IDENT EQ expr")
def expr(self, p):
"""Assignment expression"""
return AssignmentNode(p.IDENT, p.expr)
@_(
"IDENT PLUSEQ or_expr",
"IDENT MINUSEQ or_expr",
"IDENT MULEQ or_expr",
"IDENT DIVEQ or_expr",
"IDENT MODEQ or_expr",
"IDENT EXPEQ or_expr",
"IDENT OREQ or_expr",
"IDENT ANDEQ or_expr",
)
def expr(self, p):
"""In-place assignment expression"""
return InPlaceAssignmentNode(p[1].strip("="), p.IDENT, p.or_expr)
@_("EXIT")
def expr(self, p):
"""Exit-expression"""
return ExitNode()
@_("EXIT expr")
def expr(self, p):
"""Exit-expression"""
return ExitNode(p.expr)
@_("ASK expr")
def expr(self, p):
"""Ask-expression"""
return AskNode(p.expr)
@_("SAY expr")
def expr(self, p):
"""Say-expression"""
return SayNode(p.expr)
@_("or_expr LPAREN comma_sep RPAREN")
def expr(self, p):
"""Function call, with arguments"""
return (
CallFunctionNode(p.or_expr, p.comma_sep)
if isinstance(p.comma_sep, tuple)
else CallFunctionNode(p.or_expr, [p.comma_sep])
)
@_("or_expr LPAREN RPAREN")
def expr(self, p):
"""Function call, without arguments"""
return CallFunctionNode(p.or_expr, [])
@_("WHILE or_expr LPAREN expr RPAREN", "WHILE or_expr LPAREN program RPAREN")
def expr(self, p):
"""While-loop expression"""
return WhileNode(p.or_expr, p[-2])
@_("LPAREN expr WHILE or_expr RPAREN", "LPAREN program WHILE or_expr RPAREN")
def expr(self, p):
"""While-loop expression"""
return WhileNode(p.or_expr, p[1])
@_(
"LPAREN expr IF or_expr ELSE expr RPAREN",
"LPAREN program IF or_expr ELSE program RPAREN",
)
def expr(self, p):
"""If-expression"""
return IfNode(p.or_expr, p[1], p[-2])
@_(
"IF or_expr LPAREN expr RPAREN ELSE LPAREN expr RPAREN",
"IF or_expr LPAREN program RPAREN ELSE LPAREN program RPAREN",
)
def expr(self, p):
"""If-expression"""
return IfNode(p.or_expr, p[3], p[-2])
@_(
"LPAREN expr FOR IDENT IN or_expr RPAREN",
"LPAREN program FOR IDENT IN or_expr RPAREN",
)
def expr(self, p):
"""For-loop expression"""
return ForNode(p[-2], p.IDENT, p[1])
@_(
"FOR IDENT IN or_expr LPAREN expr RPAREN",
"FOR IDENT IN or_expr RPAREN program RPAREN",
)
def expr(self, p):
"""For-loop expression"""
return ForNode(p.or_expr, p.IDENT, p[-2])
@_("or_expr")
def expr(self, p):
"""Or-expression"""
return p.or_expr
@_("or_expr OR and_expr")
def or_expr(self, p):
"""Or-expression"""
return BinOp("||", p.or_expr, p.and_expr)
@_("and_expr")
def or_expr(self, p):
"""And-expression"""
return p.and_expr
@_("and_expr AND equals_expr")
def and_expr(self, p):
"""And-expression"""
return BinOp("&&", p.and_expr, p.equals_expr)
@_("equals_expr")
def and_expr(self, p):
"""Equals or not-equals expression"""
return p.equals_expr
@_("equals_expr EQEQ comp_expr")
def equals_expr(self, p):
"""Equals-expression"""
return BinOp("==", p.equals_expr, p.comp_expr)
@_("equals_expr NTEQ comp_expr")
def equals_expr(self, p):
"""Not-equals expression"""
return BinOp("!=", p.equals_expr, p.comp_expr)
@_("comp_expr")
def equals_expr(self, p):
"""Comparison expression"""
return p.comp_expr
@_("comp_expr LT plus_expr")
def comp_expr(self, p):
"""Less-than expression"""
return BinOp("<", p.comp_expr, p.plus_expr)
@_("comp_expr GT plus_expr")
def comp_expr(self, p):
"""Greater-than expression"""
return BinOp(">", p.comp_expr, p.plus_expr)
@_("comp_expr LTEQ plus_expr")
def comp_expr(self, p):
"""Less-than expression"""
return BinOp("<=", p.comp_expr, p.plus_expr)
@_("comp_expr GTEQ plus_expr")
def comp_expr(self, p):
"""Greater-than expression"""
return BinOp(">=", p.comp_expr, p.plus_expr)
@_("plus_expr")
def comp_expr(self, p):
"""Addition or subtraction expression"""
return p.plus_expr
@_("plus_expr PLUS mul_expr")
def plus_expr(self, p):
"""Addition expression"""
return BinOp("+", p.plus_expr, p.mul_expr)
@_("plus_expr MINUS mul_expr")
def plus_expr(self, p):
"""Subtraction expression"""
return BinOp("-", p.plus_expr, p.mul_expr)
@_("mul_expr")
def plus_expr(self, p):
"""Multiplication, division, modulus, or range expression"""
return p.mul_expr
@_("mul_expr MUL exp_expr")
def mul_expr(self, p):
"""Multiplication expression"""
return BinOp("*", p.mul_expr, p.exp_expr)
@_("mul_expr DIV exp_expr")
def mul_expr(self, p):
"""Division expression"""
return BinOp("/", p.mul_expr, p.exp_expr)
@_("mul_expr MOD exp_expr")
def mul_expr(self, p):
"""Modulus expression"""
return BinOp("%", p.mul_expr, p.exp_expr)
@_("exp_expr")
def mul_expr(self, p):
"""Exponent expression"""
return p.exp_expr
@_("exp_expr EXP rng_expr")
def exp_expr(self, p):
"""Exponent expression"""
return BinOp("**", p.exp_expr, p.rng_expr)
@_("rng_expr")
def exp_expr(self, p):
"""Range or containment expression"""
return p.rng_expr
@_("rng_expr TO atom")
def rng_expr(self, p):
"""Range definition"""
return RangeNode(p.rng_expr, p.atom)
@_("rng_expr IN atom")
def rng_expr(self, p):
"""Array-contains expression"""
return BinOp("in", p.atom, p.rng_expr)
@_("atom")
def rng_expr(self, p):
"""Atomic expression"""
return p.atom
@_("MINUS atom")
def atom(self, p):
"""Negated atom"""
return UnOp("-", p.atom)
@_("PLUS atom")
def atom(self, p):
"""Positive atom"""
return UnOp("+", p.atom)
@_("LPAREN program RPAREN")
def atom(self, p):
"""Parenthesized block"""
return p.program
@_("LPAREN expr RPAREN")
def atom(self, p):
"""Parenthesized expression"""
return p.expr
@_("NUMBER")
def atom(self, p):
"""Number"""
return Number(p.NUMBER)
@_("STRING")
def atom(self, p):
"""String"""
return String(p.STRING)
@_("expr DOT IDENT")
def atom(self, p):
"""Getattr expression"""
return BinOp(".", p.expr, p.IDENT)
@_("IDENT")
def atom(self, p):
"""Variable or constant reference"""
return ReferenceNode(p.IDENT)
@_("array")
def atom(self, p):
"""Array"""
return p.array
@_("FN LPAREN func_params RPAREN ARROW LPAREN expr RPAREN")
def atom(self, p):
"""Function definition"""
return (
Function(p.expr, list(p.func_params))
if isinstance(p.func_params, tuple)
else Function(p.expr, [p.func_params])
)
@_("FN LPAREN RPAREN ARROW LPAREN expr RPAREN")
def atom(self, p):
"""Function definition"""
return Function(p.expr, [])
@_("FN IDENT LPAREN func_params RPAREN ARROW LPAREN expr RPAREN")
def atom(self, p):
"""Function definition"""
return (
Function(p.expr, list(p.func_params), name=p.IDENT)
if isinstance(p.func_params, tuple)
else Function(p.expr, [p.func_params], name=p.IDENT)
)
@_("FN IDENT LPAREN RPAREN ARROW LPAREN expr RPAREN")
def atom(self, p):
"""Function definition"""
return Function(p.expr, [], name=p.IDENT)
@_("FN LPAREN func_params RPAREN ARROW LPAREN program RPAREN")
def atom(self, p):
"""Function definition"""
return (
Function(p.program, list(p.func_params))
if isinstance(p.func_params, tuple)
else Function(p.program, [p.func_params])
)
@_("FN LPAREN RPAREN ARROW LPAREN program RPAREN")
def atom(self, p):
"""Function definition"""
return Function(p.expr, [])
@_("FN IDENT LPAREN func_params RPAREN ARROW LPAREN program RPAREN")
def atom(self, p):
"""Function definition"""
return (
Function(p.program, list(p.func_params), name=p.IDENT)
if isinstance(p.func_params, tuple)
else Function(p.program, [p.func_params], name=p.IDENT)
)
@_("FN IDENT LPAREN RPAREN ARROW LPAREN program RPAREN")
def atom(self, p):
"""Function definition"""
return Function(p.program, [], name=p.IDENT)
@_("LBRACE key_val_pairs RBRACE")
def atom(self, p):
"""Object"""
return Object(p.key_val_pairs)
@_("LBRACE RBRACE")
def atom(self, p):
"""Empty object"""
return Object()
@_("FALSE")
def atom(self, p):
"""False"""
return Boolean(False)
@_("TRUE")
def atom(self, p):
"""True"""
return Boolean(True)
@_("IDENT COMMA func_params")
def func_params(self, p):
"""Function parameters"""
if isinstance(p.func_params, tuple):
return p.IDENT, *p.func_params
else:
return p.IDENT, p.func_params
@_("IDENT COMMA")
def func_params(self, p):
"""Function parameters"""
if isinstance(p.func_params, tuple):
return p.IDENT, *p.func_params
else:
return p.IDENT, p.func_params
@_("IDENT")
def func_params(self, p):
"""Function parameters"""
return p.IDENT
@_("LBRACK comma_sep RBRACK")
def array(self, p):
"""Array"""
if isinstance(p.comma_sep, Atom):
return Array([p.comma_sep])
return Array(p.comma_sep)
@_("LBRACK RBRACK")
def array(self, p):
"""Empty array"""
return Array()
@_("expr COMMA comma_sep")
def comma_sep(self, p):
"""Comma-separated list"""
if isinstance(p.comma_sep, tuple):
return p.expr, *p.comma_sep
else:
return p.expr, p.comma_sep
@_("expr COMMA")
def comma_sep(self, p):
"""Comma-separated list"""
return p.expr
@_("expr")
def comma_sep(self, p):
"""Comma-separated list"""
return p.expr
@_("key_val_pair COMMA key_val_pairs")
def key_val_pairs(self, p):
return {**p.key_val_pair, **p.key_val_pairs}
@_("key_val_pair", "key_val_pair COMMA")
def key_val_pairs(self, p):
return p.key_val_pair
@_("STRING COLON expr")
def key_val_pair(self, p):
return {p.STRING: p.expr}