-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
308 lines (240 loc) · 8.69 KB
/
main.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
import random
import ConstLib
import sys
stack = []
temp_reg = 0
direction = ConstLib.RIGHT
original_direction = ConstLib.RIGHT
ip = (0, 0, 0)
push_mode = int
next_op = "+"
in_peak = False
peak_direction = ConstLib.PEAK_DIR.NONE
spot = (0, 0, 0)
in_comment = False
def gridify(code):
grid = []
temp_line = []
for line in code.split("\n"):
temp_line += line
grid.append(temp_line[::])
temp_line = []
return grid
def next_cmd(ip):
factor_x = 0
factor_y = 0
while is_empty(ip[0] + direction[0] + factor_x,
ip[1] + direction[1] + factor_y):
factor_x += direction[0]
factor_y += direction[1]
return (ip[0] + direction[0] + factor_x,
ip[1] + direction[1] + factor_y,
ip[2] + direction[2])
def is_empty(x, y):
try:
c = g[y][x]
return False
except IndexError:
return True
def run(grid):
global stack, temp_reg, direction, ip, push_mode, next_op
global peak_direction, original_direction, spot, in_comment
cmd = grid[ip[1]][ip[0]]
while cmd != ConstLib.END_PROG:
if in_comment and cmd != ConstLib.COMMENT_END:
ip = next_cmd(ip)
#print(stack, cmd)
cmd = grid[ip[1]][ip[0]]
continue
if cmd == ConstLib.COMMENT_END:
in_comment = False
elif cmd == ConstLib.COMMAND_LEFT:
direction = ConstLib.LEFT
elif cmd == ConstLib.COMMAND_RIGHT:
direction = ConstLib.RIGHT
elif cmd == ConstLib.COMMAND_DOWN:
direction = ConstLib.DOWN
elif cmd == ConstLib.COMMAND_UP:
direction = ConstLib.UP
elif cmd in ConstLib.LITERAL:
if next_op == "*":
temp_reg *= int(cmd, base=16)
elif next_op == "+":
temp_reg += int(cmd, base=16)
elif next_op == "-":
temp_reg -= int(cmd, base=16)
elif next_op == "/":
temp_reg /= int(cmd, base=16)
elif next_op == "%":
temp_reg %= int(cmd, base=16)
next_op = "+"
elif cmd == ConstLib.PUSH:
stack.append(push_mode(temp_reg))
temp_reg = 0
elif cmd == ConstLib.DELETE:
del stack[-1]
elif cmd == ConstLib.OUTPUT:
print(stack[-1], end="")
del stack[-1]
elif cmd == ConstLib.INPUT:
txt = input()
for char in txt:
stack.append(push_mode(char))
elif cmd == ConstLib.POP:
temp_reg = stack[-1]
del stack[-1]
elif cmd == ConstLib.REVERSE:
stack = stack[::-1]
elif cmd == ConstLib.DUPLICATE:
stack.append(stack[-1])
elif cmd == ConstLib.LENGTH:
stack.append(len(stack))
elif cmd == ConstLib.RETURN:
ip = (0, ip[1], ip[2])
cmd = grid[ip[1]][ip[0]]
continue
elif cmd == ConstLib.TRAMPOLINE:
ip = next_cmd(ip)
ip = next_cmd(ip)
cmd = grid[ip[1]][ip[0]]
continue
elif cmd == ConstLib.CONDITIONAL:
test = stack[-1]
del stack[-1]
if test == 0:
ip = next_cmd(ip)
ip = next_cmd(ip)
cmd = grid[ip[1]][ip[0]]
continue
elif cmd == ConstLib.PUSH_CHR:
push_mode = chr
elif cmd == ConstLib.PUSH_INT:
push_mode = int
elif cmd == ConstLib.PUSH_FLOAT:
push_mode = float
elif cmd == ConstLib.MULTIPLY:
if temp_reg == 0:
x, y = stack[-1], stack[-2]
if str not in [type(x), type(y)]:
del stack[-1]
del stack[-1]
stack.append(x * y)
else:
raise Exception("Types for multiplication are different")
else:
next_op = "*"
elif cmd == ConstLib.ADD:
if temp_reg == 0:
x, y = stack[-1], stack[-2]
if str not in [type(x), type(y)]:
del stack[-1]
del stack[-1]
stack.append(x + y)
else:
raise Exception("Types for addition are different")
else:
next_op = "+"
elif cmd == ConstLib.SUBTRACT:
if temp_reg == 0:
x, y = stack[-1], stack[-2]
if str not in [type(x), type(y)]:
del stack[-1]
del stack[-1]
stack.append(y - x)
else:
raise Exception("Types for subtraction are different")
else:
next_op = "-"
elif cmd == ConstLib.DIVIDE:
if temp_reg == 0:
x, y = stack[-1], stack[-2]
if str not in [type(x), type(y)]:
del stack[-1]
del stack[-1]
stack.append(y / x)
else:
raise Exception("Types for divison are different")
else:
next_op = "/"
elif cmd == ConstLib.MODULUS:
if temp_reg == 0:
x, y = stack[-1], stack[-2]
if str not in [type(x), type(y)]:
del stack[-1]
del stack[-1]
stack.append(y % x)
else:
raise Exception("Types for modulus are different")
else:
next_op = "%"
elif cmd == ConstLib.NEXT_LINE:
ip = (0, ip[1] + 1, ip[2])
cmd = grid[ip[1]][ip[0]]
continue
elif cmd == ConstLib.EQUALS:
if len(stack) >= 2:
left, right = stack[-1], stack[-2]
del stack[-1]; del stack[-1]
if left == right:
stack.append(1)
else:
stack.append(0)
else:
raise Exception("Not enough values for comparison for =")
elif cmd == ConstLib.LESS_THAN:
if len(stack) >= 2:
left, right = stack[-1], stack[-2]
del stack[-1]; del stack[-1]
if left < right:
stack.append(1)
else:
stack.append(0)
else:
raise Exception("Not enough values for comparison for <")
elif cmd == ConstLib.GREATER_THAN:
if len(stack) >= 2:
left, right = stack[-1], stack[-2]
del stack[-1]; del stack[-1]
if left > right:
stack.append(1)
else:
stack.append(0)
else:
raise Exception("Not enough values for comparison for >")
elif cmd == ConstLib.L_SHIFT:
val = stack[0]
del stack[0]
stack.append(val)
elif cmd == ConstLib.R_SHIFT:
val = stack[-1]
del stack[-1]
stack.insert(0, val)
elif cmd == ConstLib.SWAP_TOP:
stack[-1], stack[-2] = stack[-2], stack[-1]
elif cmd == ConstLib.COMMENT_START:
in_comment = True
elif cmd == ConstLib.PEAK_UP:
pass
elif cmd == ConstLib.PRINT:
#Not the single char output, but the smart function
if len(stack) == 0 or type(stack[-1]) == str or stack[-1] == "*":
print("".join(stack))
stack.clear()
else:
x = stack.pop()
print("".join(stack[:x]))
ip = next_cmd(ip)
#print(stack, cmd) #uncomment to enable debug mode
cmd = grid[ip[1]][ip[0]]
if __name__ == '__main__':
if len(sys.argv) > 1:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("file", help="The location of the Minefriff file to open")
args = parser.parse_args()
file_location = args.file
else:
file_location = input("Enter the file location of the Minefriff program: ")
code = open(file_location, encoding="utf-8").read().strip("\n")
g = gridify(code)
run(g)