forked from ethanchewy/PythonBuddy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtl.py
More file actions
195 lines (152 loc) · 5.57 KB
/
tl.py
File metadata and controls
195 lines (152 loc) · 5.57 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
'''Toy Language'''
import py
from rpython.jit.tl.tlopcode import *
from rpython.rlib.jit import JitDriver, hint, dont_look_inside, promote
def char2int(c):
t = ord(c)
if t & 128:
t = -(-ord(c) & 0xff)
return t
class Stack(object):
_virtualizable_ = ['stackpos', 'stack[*]']
def __init__(self, size):
self = hint(self, access_directly=True, fresh_virtualizable=True)
self.stack = [0] * size
self.stackpos = 0 # always store a known-nonneg integer here
def append(self, elem):
self.stack[self.stackpos] = elem
self.stackpos += 1
def pop(self):
stackpos = self.stackpos - 1
if stackpos < 0:
raise IndexError
self.stackpos = stackpos # always store a known-nonneg integer here
return self.stack[stackpos]
def pick(self, i):
n = self.stackpos - i - 1
assert n >= 0
self.append(self.stack[n])
def put(self, i):
elem = self.pop()
n = self.stackpos - i - 1
assert n >= 0
self.stack[n] = elem
@dont_look_inside
def roll(self, r):
if r < -1:
i = self.stackpos + r
if i < 0:
raise IndexError
n = self.stackpos - 1
assert n >= 0
elem = self.stack[n]
for j in range(self.stackpos - 2, i - 1, -1):
assert j >= 0
self.stack[j + 1] = self.stack[j]
self.stack[i] = elem
elif r > 1:
i = self.stackpos - r
if i < 0:
raise IndexError
elem = self.stack[i]
for j in range(i, self.stackpos - 1):
self.stack[j] = self.stack[j + 1]
n = self.stackpos - 1
assert n >= 0
self.stack[n] = elem
def make_interp(supports_call):
myjitdriver = JitDriver(greens = ['pc', 'code'],
reds = ['inputarg', 'stack'],
virtualizables = ['stack'])
def interp(code='', pc=0, inputarg=0):
if not isinstance(code,str):
raise TypeError("code '%s' should be a string" % str(code))
stack = Stack(len(code))
stack = hint(stack, access_directly=True)
while True:
myjitdriver.jit_merge_point(pc=pc, code=code,
stack=stack, inputarg=inputarg)
if pc >= len(code):
break
opcode = ord(code[pc])
stack.stackpos = promote(stack.stackpos)
pc += 1
if opcode == NOP:
pass
elif opcode == PUSH:
stack.append( char2int(code[pc]) )
pc += 1
elif opcode == POP:
stack.pop()
elif opcode == SWAP:
a, b = stack.pop(), stack.pop()
stack.append(a)
stack.append(b)
elif opcode == ROLL: #rotate stack top to somewhere below
r = char2int(code[pc])
stack.roll(r)
pc += 1
elif opcode == PICK:
stack.pick(char2int(code[pc]))
pc += 1
elif opcode == PUT:
stack.put(char2int(code[pc]))
pc += 1
elif opcode == ADD:
a, b = stack.pop(), stack.pop()
stack.append( b + a )
elif opcode == SUB:
a, b = stack.pop(), stack.pop()
stack.append( b - a )
elif opcode == MUL:
a, b = stack.pop(), stack.pop()
stack.append( b * a )
elif opcode == DIV:
a, b = stack.pop(), stack.pop()
stack.append( b / a )
elif opcode == EQ:
a, b = stack.pop(), stack.pop()
stack.append( b == a )
elif opcode == NE:
a, b = stack.pop(), stack.pop()
stack.append( b != a )
elif opcode == LT:
a, b = stack.pop(), stack.pop()
stack.append( b < a )
elif opcode == LE:
a, b = stack.pop(), stack.pop()
stack.append( b <= a )
elif opcode == GT:
a, b = stack.pop(), stack.pop()
stack.append( b > a )
elif opcode == GE:
a, b = stack.pop(), stack.pop()
stack.append( b >= a )
elif opcode == BR_COND:
if stack.pop():
pc += char2int(code[pc]) + 1
myjitdriver.can_enter_jit(pc=pc, code=code,
stack=stack, inputarg=inputarg)
else:
pc += 1
elif opcode == BR_COND_STK:
offset = stack.pop()
if stack.pop():
pc += offset
myjitdriver.can_enter_jit(pc=pc, code=code,
stack=stack, inputarg=inputarg)
elif supports_call and opcode == CALL:
offset = char2int(code[pc])
pc += 1
res = interp(code, pc + offset)
stack.append( res )
elif opcode == RETURN:
break
elif opcode == PUSHARG:
stack.append( inputarg )
else:
raise RuntimeError("unknown opcode: " + str(opcode))
return stack.pop()
return interp
interp = make_interp(supports_call = True)
interp_without_call = make_interp(supports_call = False)