-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
233 lines (168 loc) · 5.42 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
from stack import Stack
import sys
stack = Stack('stack.txt')
labels = {}
def parseline(lines, idx, stack: Stack):
line = lines[idx].strip()
tokens = line.split()
if not tokens:
return
op = tokens[0].upper()
args = tokens[1:]
if op == "IF":
if not stack.stack():
raise ValueError('stack cannot be empty')
cond = stack.pop()
if cond.lower() == "true":
cond = True
elif cond.lower() == 'false':
cond = False
else:
raise ValueError('top item in stack has to be a boolean')
if cond:
return '', idx + 1
else:
while idx < len(lines):
idx += 1
next = lines[idx].strip().split()[0].upper()
if next == "ELSE" or next == "ENDIF":
break
return '', idx + 1
elif op == "ELSE":
while idx < len(lines):
idx += 1
next = lines[idx].strip().split()[0].upper()
if next == "ELSE" or next == "ENDIF":
break
return '', idx + 1
elif op == "ENDIF":
return '', idx + 1
elif op == 'PUSH':
if not args:
raise ValueError("you need to push something")
val = args[0].lower()
if val in {'true', 'false'}:
stack.push(val == 'true')
else:
try:
stack.push(int(val))
except:
try:
stack.push(float(val))
except:
stack.push(val)
return '', idx+1
elif op == 'POP':
if not stack.stack():
raise ValueError('stack cannot be empty')
stack.pop(int())
return '', idx+1
elif op == "PRINT":
return stack.print(args), idx+1
elif op == "DUMP":
return stack.dump(), idx+1
elif op == "STACK":
return stack.stack(), idx+1
elif op == "DUP":
return stack.dup(), idx+1
elif op == "SWAP":
return stack.swap(), idx+1
elif op == "DEPTH":
return stack.depth(), idx+1
elif op == "DROP":
return stack.drop(), idx+1
elif op == "ROT":
return stack.rot(), idx+1
elif op == 'ADD':
if not stack.stack():
raise ValueError('stack cannot be empty')
if len(stack.stack()) < 2:
raise ValueError('stack needs 2 elements')
# TODO: limit these to only ints and floats
lhs = float(stack.pop())
rhs = float(stack.pop())
stack.push(lhs + rhs)
return '', idx+1
elif op == 'SUB':
if not stack.stack():
raise ValueError('stack cannot be empty')
if len(stack.stack()) < 2:
raise ValueError('stack needs 2 elements')
lhs = float(stack.pop())
rhs = float(stack.pop())
stack.push(lhs-rhs)
return '', idx+1
elif op == "MUL":
if not stack.stack():
raise ValueError('stack cannot be empty')
if len(stack.stack()) < 2:
raise ValueError('stack needs 2 elements')
lhs = float(stack.pop())
rhs = float(stack.pop())
stack.push(lhs * rhs)
return '', idx+1
elif op == "DIV":
if not stack.stack():
raise ValueError('stack cannot be empty')
if len(stack.stack()) < 2:
raise ValueError('stack needs 2 elements')
lhs = float(stack.pop())
rhs = float(stack.pop())
if rhs == 0:
stack.push(0)
return '', idx+1
stack.push(lhs/rhs)
return '', idx+1
elif op == 'EQ':
if len(stack.stack()) < 2:
raise ValueError("stack needs 2 elements")
a = stack.pop()
b = stack.pop()
stack.push(a == b)
return '', idx+1
elif op == 'LT':
if len(stack.stack()) < 2:
raise ValueError("stack needs 2 elements")
a = stack.pop()
b = stack.pop()
stack.push(a < b)
return '', idx+1
elif op == 'GT':
if len(stack.stack()) < 2:
raise ValueError("stack needs 2 elements")
a = stack.pop()
b = stack.pop()
stack.push(a > b)
return '', idx+1
elif op == 'LABEL':
return '', idx+1
elif op == 'JUMP':
if not tokens[1:]:
return SyntaxError('jump needs an argument')
labelname = tokens[1]
if labelname not in set(labels.keys()):
raise ValueError(f'label {labelname} not found')
idx = labels[labelname]
return '', idx+1
else:
return '', idx+1
def marklabels(lines):
# TODO: debug
global labels
labels = {}
for idx in range(len(lines)):
if lines[idx].startswith('LABEL'):
labels[lines[idx].split()[1]] = idx
def interp(file, stack):
with open(file, 'r') as file:
lines = [line.strip() for line in file.readlines()]
marklabels(lines)
idx = 0
while idx < len(lines):
out, idx = parseline(lines, idx, stack)
if out:
print(out)
args = sys.argv[1:]
for file in args:
interp(file, stack)
print("===== END =====")