-
Notifications
You must be signed in to change notification settings - Fork 39
/
interp_Cproxy.py
119 lines (111 loc) · 4.17 KB
/
interp_Cproxy.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
from ast import *
from interp_Ldyn import Tagged
from interp_Llambda import ClosureTuple
from interp_Cany import InterpCany
from utils import *
class InterpCproxy(InterpCany):
def apply_fun(self, fun, args, e):
match fun:
case ClosureTuple(elts, arity):
return self.apply_fun(elts[0], [elts] + args, e)
case _:
return super().apply_fun(fun, args, e)
def type_to_tag(self, typ):
match typ:
case ProxyOrTupleType(elt_types):
return 'tuple'
case _:
return super().type_to_tag(typ)
def interp_getitem(self, aggregate, index):
match aggregate:
case ProxiedTuple(tup, reads):
return self.apply_fun(reads[index],
[self.interp_getitem(tup, index)], None)
case ProxiedList(lst, read, write):
return self.apply_fun(read, [self.interp_getitem(lst, index)], None)
case _:
return super().interp_getitem(aggregate, index)
def interp_setitem(self, aggregate, index, value):
match aggregate:
case ProxiedList(lst, read, write):
value2 = self.apply_fun(write, [value], None)
self.interp_setitem(lst, index, value2)
case Tagged(agg, tag):
self.interp_setitem(agg, index, value)
case _:
super().interp_setitem(aggregate, index, value)
def interp_len(self, aggregate):
match aggregate:
case ProxiedTuple(tup, reads):
return self.interp_len(tup)
case ProxiedList(lst, read, write):
return self.interp_len(lst)
case _:
return super().interp_len(aggregate)
def interp_exp(self, e, env):
match e:
case TupleProxy(tup, reads, source, target):
reads_ = self.interp_exp(reads, env)
return ProxiedTuple(self.interp_exp(tup, env), reads__)
case ListProxy(lst, read, write, source, target):
read_ = self.interp_exp(read, env)
write_ = self.interp_exp(write, env)
return ProxiedList(self.interp_exp(lst, env), read_, write_)
case Subscript(tup, index, Load()):
t = self.interp_exp(tup, env)
n = self.interp_exp(index, env)
return self.interp_getitem(t, n)
case InjectTupleProxy(proxy, typ):
p = self.interp_exp(proxy, env)
return ProxiedTuple(p[0], p[1])
case InjectTuple(tup):
return self.interp_exp(tup, env)
case InjectListProxy(proxy, typ):
p = self.interp_exp(proxy, env)
return ProxiedList(p[0], p[1], p[2])
case InjectList(tup):
return self.interp_exp(tup, env)
case Call(Name('is_tuple_proxy'), [arg]):
p = self.interp_exp(arg, env)
match p:
case ProxiedTuple(tup, reads):
return True
case _:
return False
case Call(Name('is_array_proxy'), [arg]):
p = self.interp_exp(arg, env)
match p:
case ProxiedList(tup, reads, writes):
return True
case _:
return False
case Call(Name('proxy_array_get'), [arr, index]):
a = self.interp_exp(arr, env)
i = self.interp_exp(index, env)
return self.interp_getitem(a, i)
case Call(Name('proxy_tuple_load'), [tup, index]):
p = self.interp_exp(tup, env)
i = self.interp_exp(index, env)
return self.interp_getitem(p, i)
case Call(Name('proxy_array_load'), [tup, index]):
p = self.interp_exp(tup, env)
i = self.interp_exp(index, env)
return self.interp_getitem(p, i)
case Call(Name('proxy_array_store'), [tup, index, value]):
p = self.interp_exp(tup, env)
i = self.interp_exp(index, env)
v = self.interp_exp(value, env)
self.interp_setitem(p, i, v)
return None
case Call(Name('project_array'), [array]):
return self.interp_exp(array, env)
case Call(Name('proxy_array_len'), [tup]):
t = self.interp_exp(tup, env)
return self.interp_len(t)
case Call(Name('proxy_tuple_len'), [tup]):
t = self.interp_exp(tup, env)
return self.interp_len(t)
case Call(Name('project_tuple'), [tup]):
return self.interp_exp(tup, env)
case _:
return super().interp_exp(e, env)