/
ast.py
1019 lines (734 loc) · 31.6 KB
/
ast.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from src.environment import Environment
from src.rpythonized_object import RPythonizedObject, list_equals, dict_equals, nullable_equals, list_to_string, \
dict_to_string, nullable_to_string
# Begin RPython setup; catch import errors so this can still run in CPython...
try:
from rpython.rlib.jit import JitDriver, elidable, promote, unroll_safe, jit_debug, we_are_jitted
except ImportError:
class JitDriver(object):
def __init__(self, **kw): pass
def jit_merge_point(self, **kw): pass
def can_enter_jit(self, **kw): pass
def elidable(func):
return func
def promote(x):
return x
def unroll_safe(func):
return func
def jit_debug(string, arg1=0, arg2=0, arg3=0, arg4=0):
pass
def we_are_jitted():
return False
def get_location(code):
return "%s" % code.to_string()
jitdriver = JitDriver(greens=['code'], reds=['env', 'result', 'value'], get_printable_location=get_location)
function_jitdriver = JitDriver(greens=['code'], reds='auto', get_printable_location=get_location)
# jitdriver = JitDriver(greens=['code'], reds=['env', 'result', 'value'], virtualizables=['env'], get_printable_location=get_location)
def jitpolicy(driver):
try:
from rpython.jit.codewriter.policy import JitPolicy
return JitPolicy()
except ImportError:
raise NotImplemented("Abandon if we are unable to use RPython's JitPolicy")
try:
from rpython.rlib.objectmodel import import_from_mixin
except ImportError:
def import_from_mixin(M, special_methods=['__init__', '__del__']):
pass
# end of RPython setup
class InterpretationError(Exception):
def __init__(self, reason):
self.reason = reason
def to_string(self):
return self.reason
def __str__(self):
return self.to_string()
class Program(RPythonizedObject):
"""
Tiger programs have three types of AST nodes: expressions, declarations, and types
"""
_immutable_ = True
def evaluate(self, env):
pass
# this must be implemented in sub-classes
def equals(self, other):
return RPythonizedObject.equals(self, other)
# this should be implemented in sub-classes
class Exp(Program):
_immutable_ = True
pass
class Declaration(Program):
_immutable_ = True
def __init__(self, name, index=0):
self.name = name
self.index = index
def evaluate(self, env):
raise InterpretationError('Declaration evaluation must be overriden by subclasses')
class Type(Program):
_immutable_ = True
pass
# VALUES
class Value(Exp):
_immutable_ = True
def __init__(self):
pass
def value(self):
pass
def equals(self, other):
return RPythonizedObject.equals(self, other)
def evaluate(self, env):
return self
class NilValue(Value):
_immutable_ = True
def __init__(self):
Value.__init__(self)
def value(self):
return None
def to_string(self):
return '%s' % (self.__class__.__name__,)
class IntegerValue(Value):
_immutable_ = True
def __init__(self, value):
Value.__init__(self)
assert isinstance(value, int)
self.integer = value
def value(self):
return self.integer
@staticmethod
def from_string(number):
assert isinstance(number, str)
return IntegerValue(int(number))
def to_string(self):
return '%s(%d)' % (self.__class__.__name__, self.integer)
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.integer == other.integer
class StringValue(Value):
_immutable_ = True
def __init__(self, value):
Value.__init__(self)
self.string = value
def value(self):
return self.string
def to_string(self):
return '%s(%s)' % (self.__class__.__name__, self.string)
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.string == other.string
class ArrayValue(Value):
_immutable_ = True
def __init__(self, length=0, initial_value=None):
Value.__init__(self)
self.length = length
assert (isinstance(initial_value, Value) or initial_value is None)
self.array = [initial_value] * length
def to_string(self):
return '%s(length=%d, array=%s)' % (self.__class__.__name__, self.length, list_to_string(self.array))
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.length == other.length and list_equals(self.array,
other.array)
class RecordValue(Value):
_immutable_ = True
def __init__(self, type, values=None):
Value.__init__(self)
assert (isinstance(type, RecordType))
self.type = type
assert (isinstance(values, list))
self.values = values
def to_string(self):
return '%s(type=%s, values=%s)' % (self.__class__.__name__, self.type.to_string(), list_to_string(self.values))
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.type.equals(other.type) and list_equals(self.values,
other.values)
# EXPRESSIONS: LOCATORS AND STRUCTURE
class LValue(Exp):
_immutable_ = True
def __init__(self, name, next=None, level=0, index=0):
self.name = name
self.next = next
self.level = level
self.index = index
def to_string(self):
return '%s(name=%s, next=%s, level=%s, index=%s)' % (
self.__class__.__name__, self.name, nullable_to_string(self.next), self.level, self.index)
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.name == other.name \
and nullable_equals(self.next, other.next)
@unroll_safe
def evaluate(self, env):
lvalue = self
# extract normal lvalue from environment
assert (isinstance(lvalue, LValue))
value = env.get((self.level, self.index))
lvalue = lvalue.next
# iterate over records and arrays
while lvalue:
if isinstance(lvalue, ArrayLValue):
assert (isinstance(value, ArrayValue))
index = lvalue.exp.evaluate(env)
assert (isinstance(index, IntegerValue))
value = value.array[index.integer]
elif isinstance(lvalue, RecordLValue):
assert (isinstance(value, RecordValue))
index = value.type.field_positions[lvalue.name]
assert (isinstance(index, int))
value = value.values[index]
else:
raise InterpretationError('Incorrect AST; expected an array- or record-lvalue')
lvalue = lvalue.next
return value
class RecordLValue(LValue):
_immutable_ = True
pass
class ArrayLValue(LValue):
_immutable_ = True
def __init__(self, exp, next=None):
LValue.__init__(self, None, next)
self.exp = exp
def to_string(self):
return '%s(exp=%s, next=%s)' % (
self.__class__.__name__, self.exp.to_string(), nullable_to_string(self.next))
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.exp.equals(other.exp) \
and nullable_equals(self.next, other.next)
class ArrayCreation(Exp):
_immutable_ = True
def __init__(self, type, length, initial_value):
assert (isinstance(length, Exp))
self.length_expression = length
assert (isinstance(initial_value, Exp))
self.initial_value_expression = initial_value
assert (isinstance(type, TypeId))
self.type_id = type
def to_string(self):
return '%s(initial_value=%s, length=%s, type=%s)' % (
self.__class__.__name__, self.initial_value_expression.to_string(), self.length_expression.to_string(),
self.type_id.to_string())
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.initial_value_expression.equals(
other.initial_value_expression) and self.length_expression.equals(
other.length_expression) and self.type_id.equals(other.type_id)
@unroll_safe
def evaluate(self, env):
length = self.length_expression.evaluate(env)
assert (isinstance(length, IntegerValue))
initial_value = self.initial_value_expression.evaluate(env)
assert (isinstance(initial_value, Value))
# TODO type-check
# type = env.get(self.type_id.name, env.local_types)
return ArrayValue(length.integer, initial_value)
class RecordCreation(Exp):
_immutable_ = True
def __init__(self, type, fields):
assert (isinstance(type, TypeId))
self.type_id = type
# assert (isinstance(fields, dict))
self.fields = fields
def to_string(self):
return '%s(type=%s, fields=%s)' % (
self.__class__.__name__, self.type_id.to_string(), dict_to_string(self.fields))
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.type_id.equals(other.type_id) \
and dict_equals(self.fields, other.fields)
@unroll_safe
def evaluate(self, env):
type = env.get_type((self.type_id.level, self.type_id.index))
assert (isinstance(type, RecordType))
values = [None] * len(type.field_types)
index = 0
for field in type.field_types:
value = self.fields[field].evaluate(env)
values[index] = value
index += 1
assert (len(type.field_types) == len(values))
return RecordValue(type, values)
# EXPRESSIONS: STATEMENTS
class Assign(Exp):
_immutable_ = True
def __init__(self, lvalue, expression):
self.lvalue = lvalue
self.expression = expression
def to_string(self):
return '%s(lvalue=%s, expression=%s)' % (
self.__class__.__name__, self.lvalue.to_string(), self.expression.to_string())
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.lvalue.equals(other.lvalue) and self.expression.equals(
other.expression)
@unroll_safe
def evaluate(self, env):
value = self.expression.evaluate(env)
lvalue = self.lvalue
path = (lvalue.level, lvalue.index)
if not lvalue.next:
# assignment to a plain lvalue
env.set(path, value)
else:
# assignment to a sub-located destination
destination = env.get(path)
lvalue = lvalue.next
# traverse all locators except the last one
while lvalue and lvalue.next:
if isinstance(lvalue, ArrayLValue):
assert isinstance(destination, ArrayValue)
index = lvalue.exp.evaluate(env)
assert isinstance(index, IntegerValue)
destination = destination.array[index.integer]
elif isinstance(lvalue, RecordLValue):
assert isinstance(destination, RecordValue)
index = destination.type.field_positions[lvalue.name]
assert isinstance(index, int)
destination = destination.values[index]
else:
raise InterpretationError('Incorrect AST; expected an array- or record-value')
lvalue = lvalue.next
# assign to the last locator
if isinstance(lvalue, ArrayLValue):
assert isinstance(destination, ArrayValue)
index = lvalue.exp.evaluate(env)
assert isinstance(index, IntegerValue)
destination.array[index.integer] = value
elif isinstance(lvalue, RecordLValue):
assert isinstance(destination, RecordValue)
index = destination.type.field_positions[lvalue.name]
assert isinstance(index, int)
destination.values[index] = value
else:
raise InterpretationError('Incorrect AST; expected an array- or record-value')
class Sequence(Exp):
_immutable_ = True
def __init__(self, expressions):
self.expressions = expressions
def to_string(self):
return '%s(expressions=%s)' % (self.__class__.__name__, list_to_string(self.expressions))
def equals(self, other):
return RPythonizedObject.equals(self, other) and list_equals(self.expressions, other.expressions)
@unroll_safe
def evaluate(self, env):
value = None
for expression in self.expressions:
value = expression.evaluate(env)
return value
class Let(Exp):
_immutable_ = True
def __init__(self, declarations, expressions):
self.declarations = declarations
self.expressions = expressions
def to_string(self):
return '%s(declarations=%s, expressions=%s)' % (
self.__class__.__name__, list_to_string(self.declarations), list_to_string(self.expressions))
def equals(self, other):
return RPythonizedObject.equals(self, other) \
and list_equals(self.declarations, other.declarations) \
and list_equals(self.expressions, other.expressions)
@unroll_safe
def evaluate(self, env):
if not env: # not isinstance(env, Environment):
raise InterpretationError('No environment in %s' % self.to_string())
env = env.push(len(self.declarations))
for declaration in self.declarations:
assert isinstance(declaration, Declaration)
declaration.evaluate(env)
value = None
for expression in self.expressions:
value = expression.evaluate(env)
# env = env.pop() # unnecessary
return value
# EXPRESSIONS: CONTROL FLOW
class FunctionCall(Exp):
_immutable_ = True
def __init__(self, name, arguments, level=0, index=0):
self.name = name
assert (isinstance(arguments, list))
self.arguments = arguments
self.level = level
self.index = index
def to_string(self):
return '%s(name=%s, level=%d, index=%d, args=%s)' % (
self.__class__.__name__, self.name, self.level, self.index, list_to_string(self.arguments))
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.name == other.name \
and list_equals(self.arguments, other.arguments)
#@unroll_safe # TODO is this correct?
def evaluate(self, env):
function_jitdriver.jit_merge_point(code=self)
# find declaration
declaration = env.get((self.level, self.index))
if not declaration:
raise InterpretationError('Could not find function %s' % self.name)
#assert (isinstance(declaration, FunctionDeclaration) or isinstance(declaration, NativeFunctionDeclaration))
# check arguments
if len(self.arguments) != len(declaration.parameters):
raise InterpretationError('Incorrect number of arguments passed (%d); expected %d for function %s' % (
len(self.arguments), len(declaration.parameters), self.name))
# use declaration environment for function call (note: push() allows us to reuse the frame)
activation_environment = declaration.environment.clone()
activation_environment = activation_environment.push(len(declaration.parameters) + 1)
activation_environment.set((0, 0), declaration)
# evaluate body
result = None
if isinstance(declaration, FunctionDeclaration):
# evaluate arguments
for i in range(len(self.arguments)):
value = self.arguments[i].evaluate(env)
assert (isinstance(value, Value))
activation_environment.set((0, i + 1), value)
# call function
result = declaration.body.evaluate(activation_environment)
elif isinstance(declaration, NativeFunctionDeclaration):
# evaluate arguments (no need for an activation environment)
values = []
for i in range(len(self.arguments)):
value = self.arguments[i].evaluate(env)
assert (isinstance(value, Value))
values.append(value)
# call function
result = declaration.call(values)
else:
raise InterpretationError('Unknown function type: %s' % declaration.__class__.__name__)
assert isinstance(result, Value) if result is not None else True
# TODO type-check result
return result
class If(Exp):
_immutable_ = True
def __init__(self, condition, body_if_true, body_if_false=None):
self.condition = condition
self.body_if_true = body_if_true
self.body_if_false = body_if_false
def to_string(self):
return '%s(condition=%s, body_if_true=%s, body_if_false=%s)' % (
self.__class__.__name__, self.condition.to_string(), self.body_if_true.to_string(),
nullable_to_string(self.body_if_false))
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.condition.equals(other.condition) \
and self.body_if_true.equals(other.body_if_true) \
and nullable_equals(self.body_if_false, other.body_if_false)
@unroll_safe
def evaluate(self, env):
condition_value = self.condition.evaluate(env)
assert isinstance(condition_value, IntegerValue)
result = None
if condition_value.integer != 0:
result = self.body_if_true.evaluate(env)
elif self.body_if_false is not None:
result = self.body_if_false.evaluate(env)
return result
class While(Exp):
_immutable_ = True
def __init__(self, condition, body):
self.condition = condition
self.body = body
def to_string(self):
return '%s(condition=%s, body=%s)' % (
self.__class__.__name__, self.condition.to_string(), self.body.to_string())
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.condition.equals(other.condition) and self.body.equals(
other.body)
def evaluate(self, env):
condition_value = self.condition.evaluate(env)
assert isinstance(condition_value, IntegerValue)
result = None
while condition_value.integer != 0:
jitdriver.jit_merge_point(code=self, env=env, result=result, value=condition_value)
# attempted 'env = promote(env)' here but this let to incorrect number of inner loops in sumprimes
try:
result = self.body.evaluate(env)
except BreakException:
break
condition_value = self.condition.evaluate(env)
return result
class For(Exp):
_immutable_ = True
def __init__(self, var, start, end, body):
self.var = var
self.start = start
self.end = end
self.body = body
# transform this for-loop to a while-loop in order to use the merge point in the while-loop
self.while_expression = self.convert_to_while()
def to_string(self):
return '%s(var=%s, start=%s, end=%s, body=%s)' % (
self.__class__.__name__, self.var, self.start.to_string(), self.end.to_string(), self.body.to_string())
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.var == other.var and self.start.equals(
other.start) and self.end.equals(other.end) and self.body.equals(other.body)
def convert_to_while(self):
# var iterator := start
return Let([VariableDeclaration(self.var, None, self.start)], [
# while iterator <= end:
While(
LessThanOrEquals(LValue(self.var), self.end),
# do body; iterator = iterator + 1
Sequence([
self.body,
Assign(LValue(self.var), Add(LValue(self.var), IntegerValue(1)))
])
)
])
@unroll_safe
def evaluate(self, env):
self.while_expression.evaluate(env)
return None
class Break(Exp):
_immutable_ = True
@unroll_safe
def evaluate(self, env):
raise BreakException()
class BreakException(Exception):
pass
# EXPRESSIONS: LOGICAL AND ARITHMETIC OPERATORS
class BinaryOperation(Exp):
_immutable_ = True
def __init__(self, left, right):
self.left = left
self.right = right
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.left.equals(other.left) and self.right.equals(other.right)
def to_string(self):
return '%s(left=%s, right=%s)' % (self.__class__.__name__, self.left.to_string(), self.right.to_string())
# TODO inline
@unroll_safe
def evaluate_sides_to_value(self, env):
left_value = self.left.evaluate(env)
assert isinstance(left_value, Value)
right_value = self.right.evaluate(env)
assert isinstance(right_value, Value)
return left_value, right_value
# TODO inline
@unroll_safe
def evaluate_sides_to_int(self, env):
left_value = self.left.evaluate(env)
assert isinstance(left_value, IntegerValue)
right_value = self.right.evaluate(env)
assert isinstance(right_value, IntegerValue)
return left_value.integer, right_value.integer
class Multiply(BinaryOperation):
_immutable_ = True
def evaluate(self, env):
(left_int, right_int) = self.evaluate_sides_to_int(env)
return IntegerValue(left_int * right_int)
class Divide(BinaryOperation):
_immutable_ = True
def evaluate(self, env):
(left_int, right_int) = self.evaluate_sides_to_int(env)
return IntegerValue(left_int // right_int)
class Add(BinaryOperation):
_immutable_ = True
@unroll_safe
def evaluate(self, env):
(left_int, right_int) = self.evaluate_sides_to_int(env)
return IntegerValue(left_int + right_int)
class Subtract(BinaryOperation):
_immutable_ = True
def evaluate(self, env):
(left_int, right_int) = self.evaluate_sides_to_int(env)
return IntegerValue(left_int - right_int)
class GreaterThanOrEquals(BinaryOperation):
_immutable_ = True
def evaluate(self, env):
(left_int, right_int) = self.evaluate_sides_to_int(env)
return IntegerValue(1) if left_int >= right_int else IntegerValue(0)
class LessThanOrEquals(BinaryOperation):
_immutable_ = True
def evaluate(self, env):
(left_int, right_int) = self.evaluate_sides_to_int(env)
return IntegerValue(1) if left_int <= right_int else IntegerValue(0)
class Equals(BinaryOperation):
_immutable_ = True
def evaluate(self, env):
(left, right) = self.evaluate_sides_to_value(env)
return IntegerValue(1) if left.equals(right) else IntegerValue(0)
class NotEquals(BinaryOperation):
_immutable_ = True
def evaluate(self, env):
(left, right) = self.evaluate_sides_to_value(env)
return IntegerValue(1) if not left.equals(right) else IntegerValue(0)
class GreaterThan(BinaryOperation):
_immutable_ = True
def evaluate(self, env):
(left_int, right_int) = self.evaluate_sides_to_int(env)
return IntegerValue(1) if left_int > right_int else IntegerValue(0)
class LessThan(BinaryOperation):
_immutable_ = True
def evaluate(self, env):
(left_int, right_int) = self.evaluate_sides_to_int(env)
return IntegerValue(1) if left_int < right_int else IntegerValue(0)
class And(BinaryOperation):
_immutable_ = True
def evaluate(self, env):
(left_int, right_int) = self.evaluate_sides_to_int(env)
return IntegerValue(1) if left_int and right_int else IntegerValue(0)
class Or(BinaryOperation):
_immutable_ = True
def evaluate(self, env):
(left_int, right_int) = self.evaluate_sides_to_int(env)
return IntegerValue(1) if left_int or right_int else IntegerValue(0)
# DECLARATIONS
class TypeId(Declaration):
_immutable_ = True
def __init__(self, name, level=0, index=0):
Declaration.__init__(self, name)
self.level = level
self.index = index
def to_string(self):
return '%s(name=%s)' % (self.__class__.__name__, self.name)
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.name == other.name
class TypeDeclaration(Declaration):
_immutable_ = True
def __init__(self, name, type, index=0):
Declaration.__init__(self, name)
self.type = type
self.index = index
def to_string(self):
return '%s(name=%s, type=%s)' % (self.__class__.__name__, self.name, self.type.to_string())
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.name == other.name and self.type.equals(other.type)
@unroll_safe
def evaluate(self, env):
env.set_type((0, self.index), self.type)
class VariableDeclaration(Declaration):
_immutable_ = True
def __init__(self, name, type, exp, index=0):
Declaration.__init__(self, name)
self.type = type
self.exp = exp
self.index = 0
def to_string(self):
return '%s(name=%s, type=%s, exp=%s)' % (
self.__class__.__name__, self.name, nullable_to_string(self.type), self.exp.to_string())
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.name == other.name \
and nullable_equals(self.type, other.type) and self.exp.equals(other.exp)
@unroll_safe
def evaluate(self, env):
value = self.exp.evaluate(env)
# TODO type-check
env.set((0, self.index), value)
class FunctionParameter(Declaration):
_immutable_ = True
def __init__(self, name, type=None):
Declaration.__init__(self, name)
self.name = name
assert isinstance(type, TypeId) or type is None
self.type = type
def to_string(self):
return '%s(name=%s, type=%s)' % (self.__class__.__name__, self.name, nullable_to_string(self.type))
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.name == other.name \
and nullable_equals(self.type, other.type)
class FunctionDeclaration(Declaration):
_immutable_ = True
def __init__(self, name, parameters, return_type, body, environment=None, index=0):
Declaration.__init__(self, name)
assert isinstance(parameters, list)
self.parameters = parameters
assert isinstance(return_type, TypeId) or return_type is None
self.return_type = return_type
assert isinstance(body, Exp)
self.body = body
self.environment = Environment.empty() # to be reset when the function declaration is evaluated
assert index >= 0
self.index = index
def to_string(self):
return '%s(name=%s, parameters=%s, return_type=%s, body=%s)' % (
self.__class__.__name__, self.name, list_to_string(self.parameters), nullable_to_string(self.return_type),
self.body.to_string())
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.name == other.name \
and list_equals(self.parameters, other.parameters) \
and nullable_equals(self.return_type, other.return_type) \
and self.body.equals(other.body)
@unroll_safe
def evaluate(self, env):
assert (env is not None)
env.set((0, self.index), self)
self.environment = env.clone()
class NativeFunctionDeclaration(Declaration):
_immutable_ = True
def __init__(self, name, parameters=None, return_type=None):
Declaration.__init__(self, name)
self.parameters = parameters or []
assert isinstance(self.parameters, list)
assert isinstance(return_type, TypeId) or return_type is None
self.return_type = return_type
self.environment = Environment.empty() # native functions cannot touch the interpreter environment
def call(self, arguments):
raise InterpretationError('Use a subclass of NativeFunctionDeclaration that specifies the number of arguments')
def to_string(self):
return '%s(name=%s, parameters=%s, return_type=%s)' % (
self.__class__.__name__, self.name, list_to_string(self.parameters), nullable_to_string(self.return_type))
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.name == other.name \
and list_equals(self.parameters, other.parameters) \
and nullable_equals(self.return_type, other.return_type)
class NativeNoArgumentFunctionDeclaration(NativeFunctionDeclaration):
_immutable_ = True
def __init__(self, name, parameters, return_type, function):
NativeFunctionDeclaration.__init__(self, name, parameters, return_type)
self.function = function
def call(self, arguments):
assert len(arguments) == 0
return self.function()
class NativeOneArgumentFunctionDeclaration(NativeFunctionDeclaration):
_immutable_ = True
def __init__(self, name, parameters, return_type, function):
NativeFunctionDeclaration.__init__(self, name, parameters, return_type)
self.function = function
def call(self, arguments):
assert len(arguments) == 1
return self.function(arguments[0])
class NativeTwoArgumentFunctionDeclaration(NativeFunctionDeclaration):
_immutable_ = True
def __init__(self, name, parameters, return_type, function):
NativeFunctionDeclaration.__init__(self, name, parameters, return_type)
self.function = function
def call(self, arguments):
assert len(arguments) == 2
return self.function(arguments[0], arguments[1])
# TYPES
class ArrayType(Type):
_immutable_ = True
def __init__(self, element_type):
self.type_name = element_type
def to_string(self):
return '%s(type_name=%s)' % (self.__class__.__name__, self.type_name)
def equals(self, other):
return RPythonizedObject.equals(self, other) and self.type_name == other.type_name
class RecordType(Type):
_immutable_ = True
def __init__(self, field_types):
# assert (isinstance(field_types, dict))
self.field_types = field_types
self.field_positions = {}
index = 0
for field in field_types:
self.field_positions[field] = index
index += 1
def to_string(self):
_immutable_ = True
return '%s(field_types=%s)' % (self.__class__.__name__, dict_to_string(self.field_types))
def equals(self, other):
return RPythonizedObject.equals(self, other) and dict_equals(self.field_types, other.field_types)
def inject_logging_into_evaluate_methods():
"""
In order to avoid cluttering the AST implementation with logging calls, this function will:
1. examine all classes in this module
2. if the class has an 'evaluate' attribute, replace it with a wrapper to print the string representation of the
AST node
:return: nothing
"""
import sys
import inspect