This repository has been archived by the owner on Jul 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
chapter6.py
469 lines (407 loc) · 8.81 KB
/
chapter6.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/python3
from __future__ import division
from __future__ import print_function
line = ""
cursor = 0 # Index of current reading position
token = None # The last token matched, if any
def skip_whitespace():
global cursor
while cursor < len(line) and line[cursor].isspace():
cursor += 1
def match_keyword():
global cursor, token
skip_whitespace()
if cursor >= len(line) or not line[cursor].isalpha():
return False
mark = cursor
while cursor < len(line) and line[cursor].isalpha():
cursor += 1
token = line[mark:cursor]
return True
def match_number():
global cursor, token
skip_whitespace()
if cursor >= len(line) or not line[cursor].isdigit():
return False
mark = cursor
while cursor < len(line) and line[cursor].isdigit():
cursor += 1
if cursor < len(line) and line[cursor] == ".":
cursor += 1
while cursor < len(line) and line[cursor].isdigit():
cursor += 1
token = line[mark:cursor]
return True
def match_varname():
global cursor, token
skip_whitespace()
if cursor >= len(line) or not line[cursor].isalpha():
return False
mark = cursor
while cursor < len(line) and line[cursor].isalnum():
cursor += 1
token = line[mark:cursor]
return True
def match_string():
global cursor, token
skip_whitespace()
if cursor >= len(line) or line[cursor] != '"':
return False
mark = cursor
cursor += 1 # Skip the opening double quote.
while line[cursor] != '"':
cursor += 1
if cursor >= len(line):
raise IndexError("Unclosed string")
cursor += 1 # Skip the closing double quote.
# Save string value without the double quotes.
token = line[mark + 1:cursor - 1]
return True
def parse_statement():
global cursor
if not match_keyword():
raise SyntaxError("Statement expected")
stmt = token.lower()
if stmt == "let":
parse_let()
elif stmt == "print":
parse_print()
elif stmt == "input":
parse_input()
elif stmt == "if":
parse_if()
elif stmt == "goto":
parse_goto()
elif stmt == "gosub":
parse_gosub()
elif stmt == "return":
parse_return()
elif stmt == "end":
parse_end()
elif stmt == "stop":
parse_stop()
elif stmt == "rem":
cursor = len(line)
else:
raise SyntaxError("Unknown statement")
variables = {}
def parse_let():
if not match_varname():
raise SyntaxError("Variable expected")
var_name = token.lower()
if not match("="):
raise SyntaxError("'=' expected")
variables[var_name] = parse_disjunction()
def match(text):
global cursor
skip_whitespace()
if line.startswith(text, cursor):
cursor += len(text)
return True
else:
return False
def match_eol():
skip_whitespace()
return cursor >= len(line)
def parse_print():
if match_eol():
print()
return
value = str(parse_value())
while match(","):
value += str(parse_value())
print(value)
def parse_value():
global token
if match_string():
return token
else:
return parse_disjunction()
def parse_expression():
t1 = parse_term()
while match_add_sub():
op = token
t2 = parse_term()
if op == "+":
t1 += t2
elif op == "-":
t1 -= t2
else:
raise SyntaxError(op)
return t1
def parse_term():
t1 = parse_factor()
while match_mul_div():
op = token
t2 = parse_factor()
if op == "*":
t1 *= t2
elif op == "/":
t1 /= t2
else:
raise SyntaxError(op)
return t1
def parse_factor():
global token
if match("-"):
signum = -1
else:
signum = 1
if match_number():
return float(token) * signum
elif match_varname():
token = token.lower()
if token in variables:
return variables[token] * signum
else:
raise NameError("Var not found")
elif match("("):
value = parse_expression()
if match(")"):
return value * signum
else:
raise SyntaxError("Missing ')'")
else:
raise SyntaxError("Expression expected")
def match_add_sub():
global token
if match("+"):
token = "+"
return True
elif match("-"):
token = "-"
return True
else:
return False
def match_mul_div():
global token
if match("*"):
token = "*"
return True
elif match("/"):
token = "/"
return True
else:
return False
def parse_if():
global cursor
condition = parse_disjunction()
if match_nocase("then"):
if condition != 0:
parse_statement()
else:
cursor = len(line)
else:
raise SyntaxError("IF without THEN")
def parse_comparison():
lside = parse_expression()
if not match_relation():
return lside
else:
op = token
rside = parse_expression()
if op == "<=":
return -(lside <= rside)
elif op == "<":
return -(lside < rside)
elif op == "=":
return -(lside == rside)
elif op == "<>":
return -(lside != rside)
elif op == ">":
return -(lside > rside)
elif op == ">=":
return -(lside >= rside)
def match_relation():
global token
for op in ["=", "<>", "<=", ">=", "<", ">"]:
if match(op):
token = op
return True
return False
def parse_disjunction():
lside = parse_conjunction()
while match_nocase("or"):
rside = -(parse_conjunction() != 0)
lside = -(lside != 0 or rside != 0)
return lside
def parse_conjunction():
lside = parse_negation()
while match_nocase("and"):
rside = -(parse_negation() != 0)
lside = -(lside != 0 and rside != 0)
return lside
def parse_negation():
if match_nocase("not"):
return -(parse_comparison() == 0)
else:
# Leave purely arithmetic results intact
return parse_comparison()
def match_nocase(kw):
global cursor
mark = cursor
if not match_keyword():
cursor = mark
return False
elif token.lower() != kw.lower():
cursor = mark
return False
else:
return True
def parse_input():
if match_string():
prompt = token
if not match(","):
raise SyntaxError("Comma expected")
else:
prompt = ""
input_vars = parse_varlist()
data = input(prompt).split(",")
for i in range(len(input_vars)):
v = input_vars[i]
if i < len(data):
variables[v] = float(data[i])
else:
variables[v] = 0
def parse_varlist():
if not match_varname():
raise SyntaxError("Var expected")
varlist = [token]
while match(","):
if not match_varname():
raise SyntaxError("Var expected")
varlist.append(token)
return varlist
def parse_block():
parse_statement()
while match(":"):
parse_statement()
if not match_eol():
raise SyntaxError("End of line expected")
program = {}
def parse_line():
global cursor
skip_whitespace()
mark = cursor
while cursor < len(line) and line[cursor].isdigit():
cursor += 1
if cursor > mark:
linenum = int(line[mark:cursor])
skip_whitespace()
program[linenum] = line[cursor:]
else:
parse_block()
def list_program():
addr = sorted(program.keys())
for i in addr:
print(i, program[i], sep="\t")
addr = []
crt_line = -1
stop = False
def run_program():
global addr, crt_line
addr = sorted(program.keys())
crt_line = 0
continue_program()
def continue_program():
global crt_line, line, cursor, stop
stop = False
while crt_line < len(addr) and not stop:
line_num = addr[crt_line]
line = program[line_num]
crt_line += 1
cursor = 0
parse_block()
def parse_goto():
global crt_line, cursor
line_num = int(parse_expression())
if line_num in addr:
crt_line = addr.index(line_num)
cursor = len(line)
else:
raise ValueError("Line not found")
stack = []
def parse_gosub():
global crt_line, cursor
line_num = int(parse_expression())
if line_num in addr:
stack.append(crt_line)
crt_line = addr.index(line_num)
cursor = len(line)
else:
raise ValueError("Line not found")
def parse_return():
global crt_line, cursor
if len(stack) > 0:
crt_line = stack.pop()
cursor = len(line)
else:
raise RuntimeError("Stack underflow")
def parse_end():
global crt_line, cursor
crt_line = len(addr)
cursor = len(line)
def parse_stop():
global cursor, stop
stop = True
cursor = len(line)
def parse_delete():
line_num1 = parse_expression()
if match(","):
line_num2 = parse_expression()
addr = sorted(program.keys())
for i in addr:
if line_num1 <= i <= line_num2:
del program[i]
else:
del program[line_num1]
def save_program():
if not match_string():
raise SyntaxError("Filename expected")
addr = sorted(program.keys())
with open(token, "w") as f:
for i in addr:
print(
i,
program[i],
sep="\t",
file=f)
def load_program():
global line, cursor
if not match_string():
raise SyntaxError("Filename expected")
with open(token, "r") as f:
for i in f:
line = i.strip()
cursor = 0
parse_line()
print("Basic v0.5 READY\n")
done = False
while not done:
line = input("> ")
cursor = 0
if match_nocase("bye"):
done = True
elif match_nocase("list"):
list_program()
elif match_nocase("run"):
run_program()
elif match_nocase("continue"):
continue_program()
elif match_nocase("clear"):
variables.clear()
elif match_nocase("new"):
program.clear()
elif match_nocase("delete"):
parse_delete()
elif match_nocase("save"):
save_program()
elif match_nocase("load"):
load_program()
else:
try:
parse_line()
except Exception as e:
print(e,
"in column",
cursor)