-
Notifications
You must be signed in to change notification settings - Fork 0
/
expressions.py
442 lines (337 loc) · 10.3 KB
/
expressions.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
from expressions_base import Expr, Value, BinaryExpr
from bools import Bool
from context import Context, RootGameContext
from objects import Object
from numbers import Number
from strings import String
from undefineds import Undefined
class OpEqualsExpr(Expr):
'''A general expression for things like +=, -=, etc.
This class should be extended with a class field 'op' mapping to a binary
expression.'''
def __init__(self, op, reference, value_expression):
self.op = op
self.reference = reference
self.value_expression = value_expression
def Reset(self):
self.reference.Reset()
self.value_expression.Reset()
def Eval(self, context, sub_value):
old_value = RefAccess(self.reference)
new_value = self.op(old_value, self.value_expression)
return ("tailcall", context, AssignExpr(self.reference, new_value))
class InExpr(Value):
def __init__(self, key_expr, obj_expr):
self.key_expr = key_expr
self.obj_expr = obj_expr
self.state = 0
def Reset(self):
self.state = 0
self.key_expr.Reset()
self.obj_expr.Reset()
def Eval(self, context, sub_value):
self.state += 1
if self.state == 1:
return ("eval", context, self.key_expr)
elif self.state == 2:
self.evaled_key = sub_value
return ("eval", context, self.obj_expr)
else:
obj = sub_value
key = self.evaled_key
if (isinstance(obj, Object) and (isinstance(key, String) or
(isinstance(key, Number) and isinstance(key.num, int)))):
return ("result", None, Bool(obj.HasAttr(str(key))))
else:
raise TypeError('Invalid subscript types')
class AddExpr(BinaryExpr):
method_name = '__add__'
class PlusEqualsExpr(OpEqualsExpr):
op = AddExpr
class SubtractExpr(BinaryExpr):
method_name = '__sub__'
class MinusEqualsExpr(OpEqualsExpr):
op = SubtractExpr
class TimesExpr(BinaryExpr):
method_name = '__mul__'
class ModExpr(BinaryExpr):
method_name = '__mod__'
class TimesEqualsExpr(OpEqualsExpr):
op = TimesExpr
class DivExpr(BinaryExpr):
method_name = '__div__'
class DivEqualsExpr(OpEqualsExpr):
op = DivExpr
class ExptExpr(BinaryExpr):
method_name = '__pow__'
class NeExpr(BinaryExpr):
method_name = '__ne__'
class LtExpr(BinaryExpr):
method_name = '__lt__'
class GtExpr(BinaryExpr):
method_name = '__gt__'
class LeExpr(BinaryExpr):
method_name = '__le__'
class GeExpr(BinaryExpr):
method_name = '__ge__'
class AndExpr(Expr):
def __init__(self, x_expr, y_expr):
self.x_expr = x_expr
self.y_expr = y_expr
self.state = 0
def Reset(self):
self.state = 0
self.x_expr.Reset()
self.y_expr.Reset()
def Eval(self, context, sub_value):
self.state += 1
if self.state == 1:
return ("eval", context, self.x_expr)
else:
# short-circuiting
self.evaled_x = sub_value
if self.evaled_x.IsTruthy():
return ("tailcall", context, self.y_expr)
else:
return ("result", None, self.evaled_x)
class OrExpr(Expr):
def __init__(self, x_expr, y_expr):
self.x_expr = x_expr
self.y_expr = y_expr
self.state = 0
def Reset(self):
self.state = 0
self.x_expr.Reset()
self.y_expr.Reset()
def Eval(self, context, sub_value):
self.state += 1
if self.state == 1:
return ("eval", context, self.x_expr)
else:
self.evaled_x = sub_value
if self.evaled_x.IsTruthy():
return ("result", None, self.evaled_x)
else:
return ("tailcall", context, self.y_expr)
class XorExpr(Expr):
def __init__(self, x_expr, y_expr):
self.x_expr = x_expr
self.y_expr = y_expr
self.state = 0
def Reset(self):
self.state = 0
self.x_expr.Reset()
self.y_expr.Reset()
def Eval(self, context, sub_value):
self.state += 1
if self.state == 1:
return ("eval", context, self.x_expr)
elif self.state == 2:
self.evaled_x = sub_value
return ("eval", context, self.y_expr)
else:
self.evaled_y = sub_value
return ("result", None,
Bool(self.evaled_x.IsTruthy() ^ self.evaled_y.IsTruthy()))
class EqualsExpr(BinaryExpr):
method_name = '__eq__'
class ReturnExpr(Expr):
def __init__(self, expression):
self.expression = expression
def Reset(self):
self.expression.Reset()
def Eval(self, context, sub_value):
return ("return", context, self.expression)
class NotExpr(Expr):
def __init__(self, expression):
self.expression = expression
self.state = 0
def Reset(self):
self.state = 0
self.expression.Reset()
def Eval(self, context, sub_value):
self.state += 1
if self.state == 1:
return ("eval", context, self.expression)
else:
if sub_value.IsTruthy():
return ("result", None, Bool(False))
else:
return ("result", None, Bool(True))
class VarReference(object):
def __init__(self, var_name):
self.var_name = var_name
def Reset(self):
pass
def Get(self, context):
return context.GetVar(self.var_name)
def Set(self, val, context):
context.SetVar(self.var_name, val)
def Eval(self, context, sub_value):
# do nothing
return ("result", None, self)
def __str__(self):
return self.var_name
class AttributeReference(object):
def __init__(self, base_expression, index_expression):
self.base_expression = base_expression
self.index_expression = index_expression
self.state = 0
def Reset(self):
self.state = 0
self.base_expression.Reset()
self.index_expression.Reset()
def Eval(self, context, sub_value):
self.state += 1
if self.state == 1:
return ("eval", context, self.base_expression)
elif self.state == 2:
self.evaluated_base = sub_value
return ("eval", context, self.index_expression)
else:
self.evaluated_index = sub_value
if (isinstance(self.evaluated_index, Number) and
isinstance(self.evaluated_index.num, int)):
# attribute is an integer
self.attr_name = str(self.evaluated_index.num)
elif isinstance(self.evaluated_index, String):
self.attr_name = self.evaluated_index.elements
else:
raise TypeError('Invalid index type: must be int or string')
return ("result", None, self)
def Get(self, context):
return self.evaluated_base.GetAttr(self.attr_name)
def Set(self, val, context):
return self.evaluated_base.SetAttr(self.attr_name, val)
def GetParent(self):
'''Must be called after Evaluate()'''
return self.evaluated_base
def __str__(self):
return "%s.%s" % (self.base_expression, self.index_expression)
class AttributeExpr(Expr):
def __init__(self, attr_ref):
self.attr_ref = attr_ref
self.evaled_ref = None
self.state = 0
def Reset(self):
self.state = 0
self.attr_ref.Reset()
self.evaled_ref = None
def Eval(self, context, sub_value):
self.state += 1
if self.state == 1:
return ("eval", context, self.attr_ref)
else:
self.evaled_ref = sub_value
return ("result", None, sub_value.Get(context))
def GetParent(self):
'''Must be called after Eval()'''
return self.evaled_ref.GetParent()
def __str__(self):
return str(self.attr_ref)
class RefAccess(Expr):
def __init__(self, ref):
self.ref = ref
self.state = 0
def Reset(self):
self.state = 0
self.ref.Reset()
def Eval(self, context, sub_value):
self.state += 1
if self.state == 1:
return ("eval", context, self.ref)
else:
self.evaled_ref = sub_value
return ("result", None, self.evaled_ref.Get(context))
def __str__(self):
return str(self.ref)
class AssignExpr(Expr):
def __init__(self, var_reference, var_value):
self.ref = var_reference
self.val = var_value
self.state = 0
def Reset(self):
self.state = 0
self.ref.Reset()
self.val.Reset()
def Eval(self, context, sub_value):
self.state += 1
if self.state == 1:
return ("eval", context, self.val)
elif self.state == 2:
self.evaled_val = sub_value
return ("eval", context, self.ref)
else:
sub_value.Set(self.evaled_val, context)
return ("result", None, self.evaled_val)
class BlockExpr(Expr):
def __init__(self, statements):
self.statements = statements
self.current_index = 0
def Reset(self):
self.current_index = 0
for statement in self.statements:
statement.Reset()
def __str__(self):
return "{\n%s\n}" % ("\n".join([str(s) for s in self.statements]),)
def Eval(self, context, sub_value):
if len(self.statements) == 0:
return ("result", None, Undefined())
elif self.current_index == len(self.statements) - 1:
return ("tailcall", context, self.statements[self.current_index])
else:
self.current_index += 1
return ("eval", context, self.statements[self.current_index - 1])
class VarDeclaration(Expr):
def __init__(self, name, val_expr=None, const=False, local=True):
self.name = name
if not val_expr:
val_expr = Undefined()
self.val_expr = val_expr
self.const = const
self.local = local
self.state = 0
def Reset(self):
self.state = 0
if self.val_expr:
self.val_expr.Reset()
def Eval(self, context, sub_value):
self.state += 1
if self.state == 1:
return ("eval", context, self.val_expr)
else:
if self.local:
context.SetVar(self.name, sub_value,
const=self.const, local=True)
else:
context.SetVar(self.name, sub_value,
const=self.const)
return ("result", None, sub_value)
class DelStatement(Expr):
def __init__(self, reference):
self.reference = reference
self.state = 0
def Reset(self):
self.state = 0
self.reference.Reset()
def Eval(self, context, sub_value):
self.state += 1
if self.state == 1:
return ("eval", context, self.reference)
else:
base = sub_value.evaluated_base
index = str(sub_value.evaluated_index)
return ("result", None, Bool(base.DelAttr(index)))
class PrintStatement(Expr):
def __init__(self, expression):
self.expression = expression
self.state = 0
def Reset(self):
self.state = 0
self.expression.Reset()
def Eval(self, context, sub_value):
self.state += 1
if self.state == 1:
return ("eval", context, self.expression)
context.Output(str(sub_value))
return ("result", None, Undefined())