-
Notifications
You must be signed in to change notification settings - Fork 2
/
operations.py
234 lines (171 loc) · 4.59 KB
/
operations.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
""" operations done by the virtual machine """
from stack import Stack
import opcodes as opc
stk = Stack()
def pop_top(cur):
""" pops the top of the stack """
stk.pop()
return cur+1
def unary_not(cur):
""" negates the top of the stack """
stk.push(not stk.pop())
return cur + 1
def binary_add(cur):
""" top1 + top """
tos = stk.pop()
tos1 = stk.pop()
stk.push(tos1 + tos)
return cur + 1
def binary_mul(cur):
""" top1 + top """
tos = stk.pop()
tos1 = stk.pop()
stk.push(tos1 * tos)
return cur + 1
def binary_sub(cur):
""" top1 - top """
tos = stk.pop()
tos1 = stk.pop()
stk.push(tos1 - tos)
return cur + 1
def binary_div(cur):
""" top1 - top """
tos = stk.pop()
tos1 = stk.pop()
stk.push(tos1 / tos)
return cur + 1
def binary_modulo(cur):
""" top1 - top """
tos = stk.pop()
tos1 = stk.pop()
stk.push(tos1 % tos)
return cur + 1
def print_item(cur):
""" prints the top of the stack """
print stk.get_top_n(0),
return cur + 1
def print_newline(cur):
""" prints the newline """
print
return cur + 1
def load_name(code_obj, cur):
""" loads co_names[oparg] to stack """
oparg = code_obj.get_oparg(cur)
name = code_obj.names[oparg]
if type(name) == int:
stk.push(name)
else:
stk.push(code_obj.names[oparg][0])
return cur + 3
def load_const(code_obj, cur):
""" loads co_consts[oparg] to stack """
oparg = code_obj.get_oparg(cur)
stk.push(code_obj.consts[oparg])
return cur + 3
def load_global(code_obj, cur):
""" loads co_consts[oparg] to stack """
oparg = code_obj.get_oparg(cur)
stk.push(code_obj.names[oparg][0])
return cur + 3
def load_fast(code_obj, cur):
""" loads co_consts[oparg] to stack """
oparg = code_obj.get_oparg(cur)
stk.push(code_obj.varnames[oparg])
return cur + 3
def store_name(code_obj, cur):
""" stores top to co_names[oparg] """
oparg = code_obj.get_oparg(cur)
code_obj.names[oparg] = stk.pop()
return cur + 3
def store_fast(code_obj, cur):
""" stores top to co_names[oparg] """
oparg = code_obj.get_oparg(cur)
code_obj.varnames[oparg] = stk.pop()
return cur + 3
def setup_loop(code_obj, cur):
""" setup loop """
return cur + 3
def pop_block(cur):
""" pop loop block """
return cur + 1
def pop_jump_if_false(code_obj, cur):
""" jumps if the top is false """
if not stk.pop():
return code_obj.get_oparg(cur)
else:
return cur + 3
def pop_jump_if_true(code_obj, cur):
""" jumps if the top is true """
if stk.pop():
return code_obj.get_oparg(cur)
else:
return cur + 3
def jump_forward(code_obj, cur):
""" jumps to cur + offset"""
return cur + 3 + code_obj.get_oparg(cur)
def jump_absolute(code_obj, cur):
""" jumps to target """
return code_obj.get_oparg(cur)
def make_function(code_obj, cur):
""" does nothing just updates the cur """
return cur + 9
def less_than(op1, op2):
""" a < b """
return op1 < op2
def less_equal(op1, op2):
""" a <= b """
return op1 <= op2
def equal(op1, op2):
""" a == b """
return op1 == op2
def not_equal(op1, op2):
""" a != b """
return op1 != op2
def greater_than(op1, op2):
""" a > b """
return op1 > op2
def grt_equal(op1, op2):
""" a >= b """
return op1 >= op2
""" dictionary compare_op_index: comparison """
comparisons = {
0: less_than,
1: less_equal,
2: equal,
3: not_equal,
4: greater_than,
5: grt_equal
}
def compare_op(code_obj, cur):
""" compares top1 and top """
top = stk.pop()
top1 = stk.pop()
oparg = code_obj.get_oparg(cur)
stk.push(comparisons[oparg](top1, top))
return cur + 3
""" dictionary opcode: operation """
operations = {
opc.BINARY_ADD: binary_add,
opc.BINARY_SUBTRACT: binary_sub,
opc.BINARY_MULTIPLY: binary_mul,
opc.BINARY_DIVIDE: binary_div,
opc.BINARY_MODULO: binary_modulo,
opc.LOAD_NAME: load_name,
opc.LOAD_FAST: load_fast,
opc.LOAD_CONSTANT: load_const,
opc.LOAD_GLOBAL: load_global,
opc.STORE_NAME: store_name,
opc.STORE_FAST: store_fast,
opc.PRINT_ITEM: print_item,
opc.PRINT_NEWLINE: print_newline,
opc.POP_JUMP_IF_FALSE: pop_jump_if_false,
opc.POP_JUMP_IF_TRUE: pop_jump_if_true,
opc.JUMP_FORWARD: jump_forward,
opc.JUMP_ABSOLUTE: jump_absolute,
opc.COMPARE_OP: compare_op,
opc.UNARY_NOT: unary_not,
opc.POP_TOP: pop_top,
opc.MAKE_FUNCTION: make_function,
opc.SETUP_LOOP: setup_loop,
opc.POP_BLOCK: pop_block
}