-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterp_Cany.py
78 lines (74 loc) · 2.41 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
73
74
75
76
77
78
from ast import *
from interp_Ldyn import Tagged
from interp_Llambda import ClosureTuple
from interp_Clambda import InterpClambda
from utils import *
from interp_Lfun import Function
class InterpCany(InterpClambda):
def arity(self, v):
match v:
case Function(name, params, body, env):
return len(params)
case ClosureTuple(args, arity):
return arity
case Tagged(value):
return self.arity(value)
case _:
raise Exception('Cany arity unexpected ' + repr(v))
def interp_exp(self, e, env):
# trace(f"^^^^^^^^ {e=} ")
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('any_tuple_load'), [tup, index]):
tv = self.interp_exp(tup, env)
n = self.interp_exp(index, env)
match tv:
case Tagged(v, tag):
return v[n]
case _:
raise Exception('interp any_tuple_load unexpected ' + repr(tv))
case Call(Name('any_tuple_store'), [tup, index, value]):
tv = self.interp_exp(tup, env)
n = self.interp_exp(index, env)
val = self.interp_exp(value, env)
match tv:
case Tagged(v, tag):
v[n] = val
return None # ??
case _:
raise Exception('interp any_tuple_load unexpected ' + repr(tv))
case Call(Name('any_len'), [value]):
v = self.interp_exp(value, env)
match v:
case Tagged(value, tag):
return len(value)
case _:
raise Exception('interp any_len unexpected ' + repr(v))
case Subscript(tup, index, Load()):
t = self.interp_exp(tup, env)
n = self.interp_exp(index, env)
if isinstance(t, Tagged):
t = t.value
return t[n]
case _:
return super().interp_exp(e, env)