-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·143 lines (128 loc) · 2.96 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
#!/usr/bin/python3
# this file runs a .piASM program
def run(program, memory_string):
stack = []
# find pointers
pointers = {}
for i,ins in enumerate(program):
if ins[0] == "P":
pointers[int(ins[1:])] = i
MEM = []
for i in memory_string.split(","):
if i != "":
MEM.append(int(i))
curins = 0
while curins < len(program):
# print(stack, MEM)
ins = program[curins]
char = ins[0]
# general
if char == "P":
pass
elif char == "j":
a = stack.pop(0)
b = stack.pop(0)
if a == 1:
curins = pointers[b]
elif char == "p":
stack.insert(0, int(ins[1:]))
elif char == "g":
val = stack.pop(0)
stack.insert(0, MEM[val])
elif char == "s":
val = stack.pop(0)
MEM[val] = stack.pop(0)
# I/O
elif char == "i":
while True:
try:
stack.insert(0, int(input()))
break
except:
print("input failed, try again")
elif char == "o":
print(stack.pop(0), end="")
elif char == "I":
while True:
a=input()
if len(a)>0:break
inp = a[0]
stack.insert(0, ord(inp))
elif char == "O":
print(chr(stack.pop(0)), end="")
elif char == "R":
inp = input().replace("\n", "")
for i in inp:
stack.insert(0, ord(i))
stack.insert(0, len(inp))
# logic
elif char == "e":
a = stack.pop(0)
stack.insert(0, int(a==0))
elif char == "l":
a = stack.pop(0)
b = stack.pop(0)
stack.insert(0, a<b)
elif char == "a":
a = stack.pop(0)
b = stack.pop(0)
stack.insert(0, a&b)
elif char == "x":
a = stack.pop(0)
b = stack.pop(0)
stack.insert(0, a^b)
# maths
elif char == "A":
a = stack.pop(0)
b = stack.pop(0)
stack.insert(0, a+b)
elif char == "S":
a = stack.pop(0)
b = stack.pop(0)
stack.insert(0, a-b)
elif char == "M":
a = stack.pop(0)
b = stack.pop(0)
stack.insert(0, a*b)
elif char == "D":
a = stack.pop(0)
b = stack.pop(0)
stack.insert(0, a//b)
else:
print(f"unrecognized instruction around #{curins}: {ins}")
return
curins += 1
print("\nProgram executed successfully")
#################################
import re
##### get file to run
filenames = [
"examples/0_add",
"examples/1_helloworld",
"examples/2_exponentiation",
"examples/3_triangle"
"examples/4_choose",
"examples/5_recursion",
"examples/6_mazegame",
]
print("Input a number to choose which file to run:")
for i,val in enumerate(filenames):
print(f" {i}) {val}")
filename = filenames[int(input())]
##### parse file
raw = open(filename + ".piASM", "r").read().split("\n")
# remove comments + blank lines
parsed = [i.split("#")[0] for i in raw]
parsed = [i for i in parsed if i != ""]
# remove whitespace
parsed = re.sub("\s+", "", "".join(parsed))
# extract memory string
parsed = parsed.split("]")
memory_string = parsed[0][5:]
print("Loaded memory:", parsed[0]+"]")
print("Loaded program:", parsed[1])
# separate instructions
program = re.findall(r"[a-zA-Z][-\d]*", parsed[1])
##### run
print(f"Starting execution of {filename}")
run(program, memory_string)