| 1 |
""" |
| 2 |
An interpreter for a strange word-based language: the program is a list |
| 3 |
of space-separated words. Most words push themselves on a stack; some |
| 4 |
words have another action. The result is the space-separated words |
| 5 |
from the stack. |
| 6 |
|
| 7 |
Hello World => 'Hello World' |
| 8 |
6 7 ADD => '13' 'ADD' is a special word |
| 9 |
7 * 5 = 7 5 MUL => '7 * 5 = 35' '*' and '=' are not special words |
| 10 |
|
| 11 |
Arithmetic on non-integers gives a 'symbolic' result: |
| 12 |
|
| 13 |
X 2 MUL => 'X*2' |
| 14 |
|
| 15 |
Input arguments can be passed on the command-line, and used as #1, #2, etc.: |
| 16 |
|
| 17 |
#1 1 ADD => one more than the argument on the command-line, |
| 18 |
or if it was not an integer, concatenates '+1' |
| 19 |
|
| 20 |
You can store back into an (existing) argument index with ->#N: |
| 21 |
|
| 22 |
#1 5 ADD ->#1 |
| 23 |
|
| 24 |
Braces { } delimitate a loop. Don't forget spaces around each one. |
| 25 |
The '}' pops an integer value off the stack and loops if it is not zero: |
| 26 |
|
| 27 |
{ #1 #1 1 SUB ->#1 #1 } => when called with 5, gives '5 4 3 2 1' |
| 28 |
|
| 29 |
""" |
| 30 |
from rpython.rlib.jit import hint, promote, dont_look_inside |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class Box: |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
pass |
| 44 |
|
| 45 |
class IntBox(Box): |
| 46 |
def __init__(self, intval): |
| 47 |
self.intval = intval |
| 48 |
def as_int(self): |
| 49 |
return self.intval |
| 50 |
def as_str(self): |
| 51 |
return str(self.intval) |
| 52 |
|
| 53 |
class StrBox(Box): |
| 54 |
def __init__(self, strval): |
| 55 |
self.strval = strval |
| 56 |
def as_int(self): |
| 57 |
return myint(self.strval) |
| 58 |
def as_str(self): |
| 59 |
return self.strval |
| 60 |
|
| 61 |
|
| 62 |
def func_add_int(ix, iy): return ix + iy |
| 63 |
def func_sub_int(ix, iy): return ix - iy |
| 64 |
def func_mul_int(ix, iy): return ix * iy |
| 65 |
|
| 66 |
def func_add_str(sx, sy): return sx + '+' + sy |
| 67 |
def func_sub_str(sx, sy): return sx + '-' + sy |
| 68 |
def func_mul_str(sx, sy): return sx + '*' + sy |
| 69 |
|
| 70 |
def op2(stack, func_int, func_str): |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
y = stack.pop() |
| 78 |
promote(y.__class__) |
| 79 |
x = stack.pop() |
| 80 |
promote(x.__class__) |
| 81 |
try: |
| 82 |
z = IntBox(func_int(x.as_int(), y.as_int())) |
| 83 |
except ValueError: |
| 84 |
z = StrBox(func_str(x.as_str(), y.as_str())) |
| 85 |
stack.append(z) |
| 86 |
|
| 87 |
|
| 88 |
def interpret(bytecode, args): |
| 89 |
"""The interpreter's entry point and portal function. |
| 90 |
""" |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
hint(None, global_merge_point=True) |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
bytecode = hint(bytecode, deepfreeze=True) |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
oldargs = args |
| 111 |
argcount = promote(len(oldargs)) |
| 112 |
args = [] |
| 113 |
n = 0 |
| 114 |
while n < argcount: |
| 115 |
hint(n, concrete=True) |
| 116 |
args.append(oldargs[n]) |
| 117 |
n += 1 |
| 118 |
|
| 119 |
|
| 120 |
loops = [] |
| 121 |
stack = [] |
| 122 |
pos = 0 |
| 123 |
while pos < len(bytecode): |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
hint(None, global_merge_point=True) |
| 131 |
|
| 132 |
opcode = bytecode[pos] |
| 133 |
hint(opcode, concrete=True) |
| 134 |
pos += 1 |
| 135 |
if opcode == 'ADD': op2(stack, func_add_int, func_add_str) |
| 136 |
elif opcode == 'SUB': op2(stack, func_sub_int, func_sub_str) |
| 137 |
elif opcode == 'MUL': op2(stack, func_mul_int, func_mul_str) |
| 138 |
elif opcode[0] == '#': |
| 139 |
n = myint(opcode, start=1) |
| 140 |
stack.append(args[n-1]) |
| 141 |
elif opcode.startswith('->#'): |
| 142 |
n = myint(opcode, start=3) |
| 143 |
if n > len(args): |
| 144 |
raise IndexError |
| 145 |
args[n-1] = stack.pop() |
| 146 |
elif opcode == '{': |
| 147 |
loops.append(pos) |
| 148 |
elif opcode == '}': |
| 149 |
if stack.pop().as_int() == 0: |
| 150 |
loops.pop() |
| 151 |
else: |
| 152 |
pos = loops[-1] |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
promote(pos) |
| 164 |
else: |
| 165 |
stack.append(StrBox(opcode)) |
| 166 |
return stack |
| 167 |
|
| 168 |
def repr(stack): |
| 169 |
|
| 170 |
|
| 171 |
return ' '.join([x.as_str() for x in stack]) |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
@dont_look_inside |
| 179 |
def myint_internal(s, start=0): |
| 180 |
if start >= len(s): |
| 181 |
return -1 |
| 182 |
res = 0 |
| 183 |
while start < len(s): |
| 184 |
c = s[start] |
| 185 |
n = ord(c) - ord('0') |
| 186 |
if not (0 <= n <= 9): |
| 187 |
return -1 |
| 188 |
res = res * 10 + n |
| 189 |
start += 1 |
| 190 |
return res |
| 191 |
def myint(s, start=0): |
| 192 |
n = myint_internal(s, start) |
| 193 |
if n < 0: |
| 194 |
raise ValueError |
| 195 |
return n |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
def test_main(): |
| 200 |
main = """#1 5 ADD""".split() |
| 201 |
res = interpret(main, [IntBox(20)]) |
| 202 |
assert repr(res) == '25' |
| 203 |
res = interpret(main, [StrBox('foo')]) |
| 204 |
assert repr(res) == 'foo+5' |
| 205 |
|
| 206 |
FACTORIAL = """The factorial of #1 is |
| 207 |
1 { #1 MUL #1 1 SUB ->#1 #1 }""".split() |
| 208 |
|
| 209 |
def test_factorial(): |
| 210 |
res = interpret(FACTORIAL, [IntBox(5)]) |
| 211 |
assert repr(res) == 'The factorial of 5 is 120' |
| 212 |
|
| 213 |
FIBONACCI = """Fibonacci numbers: |
| 214 |
{ #1 #2 #1 #2 ADD ->#2 ->#1 #3 1 SUB ->#3 #3 }""".split() |
| 215 |
|
| 216 |
def test_fibonacci(): |
| 217 |
res = interpret(FIBONACCI, [IntBox(1), IntBox(1), IntBox(10)]) |
| 218 |
assert repr(res) == "Fibonacci numbers: 1 1 2 3 5 8 13 21 34 55" |