-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterp_Cany.py
72 lines (67 loc) · 2.29 KB
/
interp_Cany.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
from ast import *
from interp_Ldyn import Tagged
from interp_Llambda import ClosureTuple
from interp_Clambda import InterpClambda
from utils import *
class InterpCany(InterpClambda):
# hook to be overridden
def interp_getitem(self, aggregate, index):
return aggregate[index]
# hook to be overridden
def interp_setitem(self, aggregate, index, value):
aggregate[index] = value
# hook to be overridden
def interp_len(self, aggregate):
return len(aggregate)
def interp_exp(self, e, env):
match e:
case Call(Name('make_any'), [value, tag]):
v = self.interp_exp(value, env)
t = self.interp_exp(tag, env)
return Tagged(v, t)
case Call(Name('exit'), []):
trace('exiting!')
exit(0)
case TagOf(value):
v = self.interp_exp(value, env)
match v:
case Tagged(val, tag):
return tag
case _:
raise Exception('interp TagOf unexpected ' + repr(v))
case ValueOf(value, typ):
v = self.interp_exp(value, env)
match v:
case Tagged(val, tag):
return val
case _:
raise Exception('interp ValueOf unexpected ' + repr(v))
case Call(Name(atl), [tup, index]) \
if atl == 'any_load' or atl == 'any_load_unsafe':
tv = self.interp_exp(tup, env)
n = self.interp_exp(index, env)
match tv:
case Tagged(v, tag):
return self.interp_getitem(v, n)
case _:
raise Exception('interp any_load unexpected ' + repr(tv))
case Call(Name(ats), [tup, index, value]) \
if ats == 'any_store' or ats == 'any_store_unsafe':
tv = self.interp_exp(tup, env)
n = self.interp_exp(index, env)
val = self.interp_exp(value, env)
match tv:
case Tagged(v, tag):
self.interp_setitem(v, n, val)
return None
case _:
raise Exception('interp any_store unexpected ' + repr(tv))
case Call(Name('any_len'), [value]):
v = self.interp_exp(value, env)
match v:
case Tagged(value, tag):
return self.interp_len(value)
case _:
raise Exception('interp any_len unexpected ' + repr(v))
case _:
return super().interp_exp(e, env)