-
Notifications
You must be signed in to change notification settings - Fork 39
/
interp_Lany.py
124 lines (117 loc) · 4.08 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
122
123
124
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 ListType(elt_ty):
return 'tuple'
case t if t == int:
return 'int'
case t if t == bool:
return 'bool'
case IntType():
return 'int'
case BoolType():
return 'bool'
case _:
raise Exception('type_to_tag unexpected ' + repr(typ))
def arity(self, v):
match v:
case Function(name, params, body, env):
return len(params)
case ClosureTuple(args, arity):
return arity
case _:
raise Exception('Lany arity unexpected ' + repr(v))
# 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 Inject(value, typ):
v = self.interp_exp(value, env)
if isinstance(value, Tagged):
raise Exception('cannot inject a value that is already tagged')
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) + '\nin\n' + str(e))
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 Subscript(tup, index, Load()):
t = self.interp_exp(tup, env)
n = self.interp_exp(index, env)
return self.interp_getitem(t, n)
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 Call(Name('make_any'), [value, tag]):
v = self.interp_exp(value, env)
t = self.interp_exp(tag, env)
if isinstance(v, Tagged):
raise Exception('cannot inject a value that is already tagged')
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 AnnLambda(params, returns, body):
return Function('lambda', [x for (x,t) in params], [Return(body)], env)
case _:
return super().interp_exp(e, env)