-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.py
161 lines (125 loc) · 3.52 KB
/
runtime.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import cStringIO
class Closure(object):
def __init__(self, proto, frame):
self.proto = proto
self.upvars = []
for kind, index, scope_index in proto.upvars:
if kind == "local":
upvar = Upvar(frame, index, scope_index)
self.upvars.append(upvar)
elif kind == "varg":
self.upvars.append(UpvarForVarg(frame))
elif kind == "up":
self.upvars.append(frame.upvars[index])
else:
assert 0
class UpvarForVarg(object):
def __init__(self, frame):
self.frame = frame
self.closed = False
self.scope_index = 0
def get(self):
if self.closed:
return self.value
else:
return self.frame.varargs
def set(self, value):
if self.closed:
self.value = value
else:
self.frame.varargs = value
def close(self):
if not self.closed:
self.value = self.frame.varargs
del self.frame
self.closed = True
class Upvar(object):
CLOSED = 0
OPEN = 1
def __init__(self, frame, index, scope_index):
self.frame = frame
self.index = index
self.stat = Upvar.OPEN
self.scope_index = scope_index
def get(self):
if self.stat == Upvar.CLOSED:
return self.value
else:
return self.frame.localvars[self.index]
def set(self, value):
if self.stat == Upvar.CLOSED:
self.value = value
else:
self.frame.localvars[self.index] = value
def close(self):
assert self.stat == Upvar.OPEN
self.value = self.frame.localvars[self.index]
del self.frame
self.stat == Upvar.CLOSED
def build_list(seq):
ret = None
for i in reversed(seq):
if isinstance(i, list):
i = build_list(i)
ret = cons(i, ret)
return ret
def sclist2pylist(l):
ret = []
while l:
head, l = l.car(), l.cdr()
if isinstance(head, LinkList):
head = sclist2pylist(head)
ret.append(head)
return ret
class LinkList(object):
def __init__(self, v, next=None):
self.v = v
self.next = next
def cons(self, v):
new = LinkList(v)
new.next = self
return new
def car(self):
return self.v
def cdr(self):
return self.next
def __str__(self):
n = self
sb = cStringIO.StringIO()
sb.write("(%s" % self.v)
try:
while n.next:
n = n.next
sb.write(" %s" % str(n.v))
except AttributeError:
sb.write(" . %s" % n)
sb.write(")")
return sb.getvalue()
def __eq__(self, other):
if type(other) != type(self):
return False
if self is other:
return True
return list_eq(self, other)
def list_eq(a, b):
while 1:
if a.car() != b.car():
return False
a, b = a.cdr(), b.cdr()
if a is None and b is None:
return True
elif a is None or b is None:
return False
def cons(x, y):
if not isinstance(y, LinkList):
return LinkList(x, next=y)
else:
return y.cons(x)
def car(x):
if not isinstance(x, LinkList):
raise ValueError("Unsupported operation car")
return x.car()
def cdr(x):
if not isinstance(x, LinkList):
raise ValueError("Unsupported operation cdr")
return x.cdr()