-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterp_Lany.py
121 lines (115 loc) · 3.87 KB
/
interp_Lany.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
from ast import *
from interp_Lfun import Function
from interp_Llambda import InterpLlambda, ClosureTuple
from interp_Ldyn import Tagged
from utils import *
class InterpLany(InterpLlambda):
def type_to_tag(self, typ):
match typ:
case FunctionType(params, rt):
return 'function'
case TupleType(fields):
return 'tuple'
case t if t == int:
return 'int'
case t if t == bool:
return 'bool'
case IntType():
return 'int'
case BoolType():
return 'int'
case _:
raise Exception('type_to_tag unexpected ' + repr(typ))
def arity(self, v):
match v:
case Tagged(val, tag):
return super().arity(val)
case Function(name, params, body, env):
return len(params)
case ClosureTuple(args, arity):
return arity
case _:
raise Exception('Lany arity unexpected ' + repr(v))
def apply_fun(self, fun, args, e):
match fun:
case Function(name, xs, body, env):
new_env = {x: v for (x,v) in env.items()}
for (x,arg) in zip(xs, args):
new_env[x] = arg
return self.interp_stmts(body, new_env)
case Tagged(val, tag):
return super().apply_fun(val, args, e)
case _:
raise Exception('apply_fun: unexpected: ' + repr(fun))
def interp_exp(self, e, env):
match e:
case Inject(value, typ):
v = self.interp_exp(value, env)
return Tagged(v, self.type_to_tag(typ))
case Project(value, typ):
v = self.interp_exp(value, env)
match v:
case Tagged(val, tag) if self.type_to_tag(typ) == tag:
return val
case _:
raise Exception('interp project to ' + repr(typ) \
+ ' 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 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('arity'), [fun]):
f = self.interp_exp(fun, env)
return self.arity(f)
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 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 AnnLambda(params, returns, body):
return Function('lambda', [x for (x,t) in params], [Return(body)], env)
case _:
return super().interp_exp(e, env)