/
interp.py
140 lines (109 loc) · 3.89 KB
/
interp.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
# Copyright (c) 2012, Leonhard Vogt (name.surname@gmx.ch)
# This code is provided "as is" without any warranties.
# Redistribution of this code and use in derivative works are permitted.
def test():
for i in range(10):
print i
# define constants for the byte-codes
import dis
LOAD_CONST = dis.opmap['LOAD_CONST']
LOAD_FAST = dis.opmap['LOAD_FAST']
LOAD_GLOBAL = dis.opmap['LOAD_GLOBAL']
STORE_FAST = dis.opmap['STORE_FAST']
RETURN_VALUE = dis.opmap['RETURN_VALUE']
BINARY_ADD = dis.opmap['BINARY_ADD']
BINARY_SUBTRACT = dis.opmap['BINARY_SUBTRACT']
BINARY_MULTIPLY = dis.opmap['BINARY_MULTIPLY']
CALL_FUNCTION = dis.opmap['CALL_FUNCTION']
SETUP_LOOP = dis.opmap['SETUP_LOOP']
POP_BLOCK = dis.opmap['POP_BLOCK']
GET_ITER = dis.opmap['GET_ITER']
FOR_ITER = dis.opmap['FOR_ITER']
JUMP_ABSOLUTE = dis.opmap['JUMP_ABSOLUTE']
PRINT_ITEM = dis.opmap['PRINT_ITEM']
PRINT_NEWLINE = dis.opmap['PRINT_NEWLINE']
import types
class Frame(object):
def __init__(self, code, globals):
self.locals = [None] * code.co_nlocals
self.PC = 0
self.stack = []
self.globals = globals
self.code = code
def execute(code, globals):
f = Frame(code, globals)
while True:
bc = ord(f.code.co_code[f.PC])
if bc==LOAD_FAST:
arg = ord(f.code.co_code[f.PC+1]) + 256*ord(f.code.co_code[f.PC+2])
f.stack.append(f.locals[arg])
f.PC += 3
elif bc==LOAD_CONST:
arg = ord(f.code.co_code[f.PC+1]) + 256*ord(f.code.co_code[f.PC+2])
f.stack.append(f.code.co_consts[arg])
f.PC += 3
elif bc==LOAD_GLOBAL:
arg = ord(f.code.co_code[f.PC+1]) + 256*ord(f.code.co_code[f.PC+2])
f.stack.append(f.globals[f.code.co_names[arg]])
f.PC += 3
elif bc==BINARY_ADD:
b = f.stack.pop()
a = f.stack.pop()
f.stack.append(a+b)
f.PC += 1
elif bc==BINARY_SUBTRACT:
b = f.stack.pop()
a = f.stack.pop()
f.stack.append(a-b)
f.PC += 1
elif bc==BINARY_MULTIPLY:
b = f.stack.pop()
a = f.stack.pop()
f.stack.append(a*b)
f.PC += 1
elif bc==STORE_FAST:
arg = ord(f.code.co_code[f.PC+1]) + 256*ord(f.code.co_code[f.PC+2])
f.locals[arg] = f.stack.pop()
f.PC += 3
elif bc==RETURN_VALUE:
return f.stack.pop()
elif bc==SETUP_LOOP:
f.PC += 3
elif bc==POP_BLOCK:
f.PC += 1
elif bc==GET_ITER:
f.stack.append(iter(f.stack.pop()))
f.PC += 1
elif bc==JUMP_ABSOLUTE:
arg = ord(f.code.co_code[f.PC+1]) + 256*ord(f.code.co_code[f.PC+2])
f.PC = arg
elif bc==CALL_FUNCTION:
arg = ord(f.code.co_code[f.PC+1])
call_args = [None] * arg
for i in range(arg-1, -1, -1):
call_args[i] = f.stack.pop()
callee = f.stack.pop()
if type(callee) is types.BuiltinFunctionType:
f.stack.append(callee(*call_args))
f.PC += 3
elif bc==FOR_ITER:
arg = ord(f.code.co_code[f.PC+1]) + 256*ord(f.code.co_code[f.PC+2])
try:
nx = next(f.stack[-1])
except StopIteration:
f.stack.pop()
f.PC += arg + 3
else:
f.stack.append(nx)
f.PC += 3
elif bc==PRINT_ITEM:
print f.stack.pop(),
f.PC += 1
elif bc==PRINT_NEWLINE:
print
f.PC += 1
else:
raise Exception('Unknown Opcode %d (%s)' % (bc, dis.opname[bc]))
print 'normal Python call:', test()
my_globals = {'range': range}
print 'own execute function:', execute(test.func_code, my_globals)