This repository has been archived by the owner on Jan 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Execute.py
334 lines (245 loc) · 7.02 KB
/
Execute.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from PIL import Image
stack = []
'''
single callstack so you really should have functions terminate in call order
or v bad
'''
callstack = []
pointers = [(0, 0)] # Point to the first pixel in the image
name = raw_input("What is the name of your program? ")
# name = 'imageCode/helloWorldComplex.png'
im = Image.open(name)
pix = im.load()
# No operation
def nop(operand):
getpointers()
# Pushes operand to the stack
def push(operand):
stack.append(operand)
getpointers()
'''
Pops and prints the first element of the stack, Prints as ascii char if
non-zero operand
'''
def pop(operand):
out = stack.pop()
if operand != 0:
try:
print (chr(out), end='')
getpointers()
except:
print ("Conversion error from int to char in pop")
exit(1)
else:
print (out, end='')
getpointers()
# Adds top elements of stack unless given non zero arg
def add(operand):
if operand == 0:
stack.append(stack.pop() + stack.pop())
getpointers()
else:
stack.append(stack.pop() + operand)
getpointers()
# Subtracts top elements of stack, unless given non zero arg
def sub(operand):
if operand == 0:
stack.append(stack.pop() - stack.pop())
getpointers()
else:
stack.append(stack.pop() - operand)
getpointers()
# Unconditional jump to (stack[0],stack[1]), destroys top 2 elements of stack
def jump(operand):
try:
coords = (stack.pop(), stack.pop())
pointers.append(coords)
callstack.append(coords)
getpointers()
except IndexError:
print ("Tried to pop empty stack")
exit(1)
# Jumps if stack.pop() == operand, destroys top 3 elements of stack
def condjump(operand):
try:
if stack.pop() == operand:
coords = (stack.pop(), stack.pop())
pointers.append(coords)
callstack.append(coords)
getpointers()
else:
stack.pop()
stack.pop()
getpointers()
except IndexError:
print ("Tried to pop empty stack")
exit(1)
# Functions like getpointers, But takes operand as B and top of call stack as location.
def ret(operand):
try:
location = callstack.pop()
if operand & 0b1:
pointers.append((location[0], location[1]-1))
if operand >> 1 & 0b1:
pointers.append((location[0]+1, location[1]))
if operand >> 2 & 0b1:
pointers.append((location[0], location[1]+1))
if operand >> 3 & 0b1:
pointers.append((location[0]-1, location[1]))
except IndexError:
print ("Tried to pop an empty call stack")
exit(1)
# Kills pointer if stack.pop() == operand, Destroys top of stack
def condkill(operand):
try:
if stack.pop() == operand:
pass
else:
getpointers()
except IndexError:
print ("Tried to pop empty stack")
exit(1)
# Transforms current coordinates to have opcode operand and operand stack.pop()
def transform(operand):
pix[(x, y)] = (operand, stack.pop(), pix[(x, y)][2])
getpointers()
# Reads input from user and pushes to stack
def inpchar(operand):
userin = ord(raw_input("? ")[0])
if type(userin) != int:
print ("INVALID INPUT! Please input an ascii character")
exit(1)
else:
stack.append(userin)
getpointers()
# Reads input from user as an integer and pushes to stack
def inpint(operand):
try:
userin = int(input("? "))
except:
print ("INVALID INPUT! Please input an integer")
exit(1)
stack.append(userin)
getpointers()
# Duplicates top element of stack
def dup(operand):
last = stack.pop()
stack.extend([last, last])
getpointers()
# Multiplies top elements of stack unless given a non-zero operand
def mul(operand):
if operand == 0:
stack.append(stack.pop() * stack.pop())
getpointers()
else:
stack.append(stack.pop() * operand)
getpointers()
# Removes outgoing pointers if the stack is empty
def emptykill(operand):
if stack == []:
pass
else:
getpointers()
# Reverses the stack
def reverse(operand):
stack.reverse()
getpointers()
# Rotates stack such that n1 n3
# n2 -> n1
# n3 n2
def rot(operand):
try:
n1 = stack.pop()
n2 = stack.pop()
n3 = stack.pop()
stack.extend([n2, n1, n3])
getpointers()
except:
print ("Rotate Error on stack {0}, at {1},{2}".format(stack, x, y))
exit(1)
# Makes a copy of the second item and pushes it to the top
def over(operand):
try:
n1 = stack.pop()
n2 = stack.pop()
stack.extend([n2, n1, n2])
getpointers()
except:
print ("Over Error")
exit(1)
# Swaps the top two elements of the stack
def swap(operand):
try:
n1 = stack.pop()
n2 = stack.pop()
stack.extend([n1, n2])
getpointers()
except:
print ("Swap Error")
exit(1)
# Removes the top of the stack
def drop(operand):
try:
stack.pop()
getpointers()
except IndexError:
print ("Tried to drop empty stack")
exit(1)
# Waits for stack to equal operand before proceeding
def waitfor(operand):
try:
top = stack.pop()
if top == operand:
getpointers()
else:
pointers.append((x, y))
stack.append(top)
except IndexError:
print ("Tried to wait on an empty stack")
exit(1)
# Kills if stack has one element
def singlekill(operand):
if len(stack) == 1:
pass
else:
getpointers()
# Runs if it is the last pointer
def runifonly(operand):
if len(pointers) == 0:
getpointers()
else:
pointers.append((x, y))
functions = {0: nop, 1: push, 2: pop, 3: add, 4: sub,
5: jump, 6: condjump, 7: ret, 8: condkill,
9: transform, 10: inpchar, 11: dup, 12: mul,
13: emptykill, 14: reverse, 15: rot, 16: over,
17: swap, 18: drop, 19: inpint, 20: waitfor,
21: singlekill, 22: runifonly}
def operate(opcode, operand):
try:
functions[opcode](operand)
except KeyError:
print ("R value {0} not valid! (instruction at {1},{2})".format(
opcode, x, y))
exit(1)
def getpointers():
# refresh current in case changed by kill
current = pix[x, y]
# Adds new pointers based of the B in RGB code.
# 1111 = WSEN
if current[2] & 0b1:
pointers.append((x, y-1))
if current[2] >> 1 & 0b1:
pointers.append((x+1, y))
if current[2] >> 2 & 0b1:
pointers.append((x, y+1))
if current[2] >> 3 & 0b1:
pointers.append((x-1, y))
while pointers != []:
x, y = pointers.pop(0)
try:
current = pix[x, y]
except IndexError:
print ("Coords out of range ({0},{1})".format(x,y))
exit(1)
operate(current[0], current[1])