-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloopwhile.py
339 lines (266 loc) · 9.89 KB
/
loopwhile.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
335
336
337
338
339
#!/usr/bin/env python3
# Copied from CJ Quines
"""
this is a LOOP/WHILE interpreter, written for PROMYS 2020
the base implementation is on https://repl.it/@cjquines/LOOPWHILE
please edit this! i know it's long, but i tried to make it simple to read,
edit, and extend.
let me (CJ) know if there's a change you want to make but couldn't figure out
how to write. (hint: if you spend more than 15 minutes making a change, then
you should ask for help!)
07-01: fixed LOOP 0
"""
class ParseError(Exception):
# raised when the program can't be parsed
def __init__(self, line, message):
self.line = line
self.message = message
def __str__(self):
return f"in line {self.line + 1}: {self.message}"
class RuntimeError(Exception):
# raised when the program has a mistake during runtime
pass
def is_valid_name(name):
# is it a valid register name?
return name.isupper() and name.isalpha()
def split_but_return(string, sep):
# split the string by sep, but insert it in between instances
result = []
parts = string.split(sep)
tail = parts.pop()
for part in parts:
if part:
result.append(part)
result.append(sep)
if tail:
result.append(tail)
return result
class Instruction:
"""
a line in the program, like X = Y, LOOP X, or WHILE X != 0.
if you want to add a new instruction, you need to write three things.
1. edit the run function here. this runs the instruction.
2. edit the __init__ function here. this takes the input from the
parser and stores it as variables, like self.X.
3. edit the Parser's parse function. this should read the line, detect
if it's of the given instruction, and then call __init__.
see the existing instructions for examples.
"""
def __init__(self, *args):
# input from the parser, and set up the variables for this instruction.
raise Exception("Instruction not implemented!")
def run(self, registers, index):
# does the instruction, which is at the given index.
# change the registers in place. return index of the next line to run.
raise Exception("Instruction not implemented!")
class Equate(Instruction):
# X = Y
def __init__(self, X, Y):
self.X = X
self.Y = Y
def run(self, registers, index):
registers[self.X] = registers[self.Y]
return index + 1
class Addition(Instruction):
# X += Y
def __init__(self, X, Y):
self.X = X
self.Y = Y
def run(self, registers, index):
registers[self.X] = registers[self.X] + registers[self.Y]
return index + 1
class Add(Instruction):
# X = X + 1
# X++
def __init__(self, X):
self.X = X
def run(self, registers, index):
registers[self.X] = registers[self.X] + 1
return index + 1
class Set(Instruction):
# X = 0
def __init__(self, X):
self.X = X
def run(self, registers, index):
registers[self.X] = 0
return index + 1
class Loop(Instruction):
# LOOP X
def __init__(self, X):
self.X = X
self.loops_left = None
def set_end_index(self, end_index):
# we need this because we don't know the end index the first time we
# read the LOOP instruction.
self.end_index = end_index
def run(self, registers, index):
# the first time we run LOOP X
if self.loops_left is None:
self.loops_left = registers[self.X]
# looping
if self.loops_left > 0:
self.loops_left = self.loops_left - 1
return index + 1
# done looping
else:
self.loops_left = None # reset
return self.end_index
class While(Instruction):
# WHILE X != 0
def __init__(self, X):
self.X = X
def set_end_index(self, end_index):
# see the comment for LOOP's set_end_index.
self.end_index = end_index
def run(self, registers, index):
if registers[self.X] != 0:
return index + 1
else:
return self.end_index
class End(Instruction):
# END
def __init__(self):
pass
def set_start_index(self, start_index):
# see the comment for LOOP's set_end_index.
self.start_index = start_index
def run(self, registers, index):
return self.start_index
class LineParser:
"""
the line parser. converts a line into an instruction, and returns any
registers seen.
if you want to add a new instruction, you need to edit the parse_line
function here to make that new instruction. you might also have to edit
the parse function in Parser, if it depends on other lines.
"""
def __init__(self, index, line):
self.index = index
self.line = line
self.tokens = []
self.tokenize_line()
def tokenize_line(self):
self.line = self.line.split("//")[0]
# splits the line by whitespace, =, and +.
for partial in self.line.split():
for partial2 in split_but_return(partial, "="):
for token in split_but_return(partial2, "+"):
self.tokens.append(token)
def expect_length(self, length):
if not len(self.tokens) == length:
raise ParseError(self.index, "extra characters after instruction")
def expect_name(self, name):
if not is_valid_name(name):
raise ParseError(self.index, f"{name} not a valid register name")
def expect_token(self, token, expected):
if token != expected:
raise ParseError(self.index, f"expected {expected}, got {token}")
def parse_line(self):
# END
if self.tokens[0] == "END":
self.expect_length(1)
return End(), set()
# LOOP X
elif self.tokens[0] == "LOOP":
X = self.tokens[1]
self.expect_name(X)
self.expect_length(2)
return Loop(X), {X}
# X = 0
elif len(self.tokens) >= 3 and self.tokens[1] == "=" and self.tokens[2] == "0":
X = self.tokens[0]
self.expect_name(X)
self.expect_length(3)
return Set(X), {X}
# X = X + 1
elif len(self.tokens) >= 5 and self.tokens[1] == "=" and self.tokens[3] == "+":
X, X_, o = self.tokens[0], self.tokens[2], self.tokens[4]
self.expect_name(X)
self.expect_name(X_)
self.expect_token(X_, X)
self.expect_token(o, "1")
self.expect_length(5)
return Add(X), {X}
# X = Y
elif len(self.tokens) >= 3 and self.tokens[1] == "=":
X, Y = self.tokens[0], self.tokens[2]
self.expect_name(X)
self.expect_name(Y)
self.expect_length(3)
return Equate(X, Y), {X, Y}
# WHILE X != 0
elif len(self.tokens) >= 5 and self.tokens[0] == "WHILE":
X, n, e, z = self.tokens[1], self.tokens[2], self.tokens[3], self.tokens[4]
self.expect_name(X)
self.expect_token(n, "!")
self.expect_token(e, "=")
self.expect_token(z, "0")
self.expect_length(5)
return While(X), {X}
else:
raise ParseError(self.index, "don't know what instruction this is")
class Parser:
"""
the parser. converts the program to a list of instructions.
if you want to add a complicated instruction, you might also have to edit
the parse function here.
"""
def __init__(self, program):
self.lines = []
for line in program.splitlines():
stripped = line.strip()
if stripped:
self.lines.append(stripped)
def parse(self):
instructions = []
seen_registers = set()
# indices of LOOP/WHILEs that are unmatched:
unmatched_indices = []
for index in range(len(self.lines)):
line = self.lines[index]
line_parser = LineParser(index, line)
instruction, registers = line_parser.parse_line()
# handle LOOP, WHILE, END:
if isinstance(instruction, Loop) or isinstance(instruction, While):
unmatched_indices.append(index)
elif isinstance(instruction, End):
start_index = unmatched_indices.pop()
# the start index of END is start_index
instruction.set_start_index(start_index)
# the end index of this LOOP or WHILE is the index AFTER the
# the current index. (it's the index it goes to after the loop)
instructions[start_index].set_end_index(index + 1)
instructions.append(instruction)
seen_registers = seen_registers | registers
return instructions, seen_registers
class Program:
"""
runs the program itself.
"""
def __init__(self, program):
parser = Parser(program)
self.lines, self.seen_registers = parser.parse()
def check_inputs(self, inputs):
for name, value in inputs.items():
if not is_valid_name(name):
raise RuntimeError(f"input {name} not a valid register name")
if not isinstance(value, int) or value < 0:
raise RuntimeError(f"input {name}'s {value} is not in N")
def check_output(self, output):
if not is_valid_name(output):
raise RuntimeError(f"input {name} not a valid register")
def run(self, inputs, output):
self.check_inputs(inputs)
self.check_output(output)
# initialize registers
registers = {}
for name in self.seen_registers:
registers[name] = 0
for name, value in inputs.items():
registers[name] = value
# program loop
index = 0
while index != len(self.lines):
instruction = self.lines[index]
index = instruction.run(registers, index)
return registers