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
/
chapter5.py
378 lines (322 loc) · 6.95 KB
/
chapter5.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
#!/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 == "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 == "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_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
def run_program():
global addr, crt_line, line, cursor
addr = sorted(program.keys())
crt_line = 0
while crt_line < len(addr):
line_num = addr[crt_line]
line = program[line_num]
crt_line += 1
cursor = 0
parse_block()
def parse_goto():
global crt_line
line_num = int(parse_expression())
if line_num in addr:
crt_line = addr.index(line_num)
else:
raise ValueError("Line not found")
stack = []
def parse_gosub():
global crt_line
line_num = int(parse_expression())
if line_num in addr:
stack.append(crt_line)
crt_line = addr.index(line_num)
else:
raise ValueError("Line not found")
def parse_return():
global crt_line
if len(stack) > 0:
crt_line = stack.pop()
else:
raise RuntimeError("Stack underflow")
def parse_end():
global crt_line
crt_line = len(addr)
test = ['10 print "main program"',
'20 gosub 100',
'25 print "and back"',
'30 end',
'100 print "sub #1"',
'110 gosub 200',
'120 return',
'200 print "sub #2"',
'210 return',
'1000 REM end program']
for i in test:
line = i
cursor = 0
parse_line()
list_program()
run_program()