forked from richpl/PyBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.py
More file actions
325 lines (244 loc) · 11.7 KB
/
Copy pathprogram.py
File metadata and controls
325 lines (244 loc) · 11.7 KB
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
#! /usr/bin/python
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Class representing a BASIC program.
This is a list of statements, ordered by
line number.
"""
from basictoken import BASICToken as Token
from basicparser import BASICParser
from flowsignal import FlowSignal
import pickle
class Program:
def __init__(self):
# Dictionary to represent program
# statements, keyed by line number
self.__program = {}
# Program counter
self.__next_stmt = 0
# Initialise return stack for subroutine returns
# and loop returns
self.__return_stack = []
def list(self):
"""Lists the program"""
line_numbers = self.line_numbers()
for line_number in line_numbers:
print(line_number, end=' ')
statement = self.__program[line_number]
for token in statement:
# Add in quotes for strings
if token.category == Token.STRING:
print('"' + token.lexeme + '"', end=' ')
else:
print(token.lexeme, end=' ')
print(flush=True)
def save(self, file):
"""Save the program
:param file: The name and path of the save file
"""
try:
with open(file, 'wb') as outfile:
pickle.dump(self.__program, outfile)
outfile.close()
except OSError:
raise OSError("Could not save to file")
def load(self, file):
"""Load the program
:param file: The name and path of the file to be loaded"""
try:
with open(file, 'rb') as infile:
self.__program = pickle.load(infile)
infile.close()
except OSError:
raise OSError("Could not read file")
def add_stmt(self, tokenlist):
"""
Adds the supplied token list
to the program. The first token should
be the line number. If a token list with the
same line number already exists, this is
replaced.
:param tokenlist: List of BTokens representing a
numbered program statement
"""
try:
line_number = int(tokenlist[0].lexeme)
self.__program[line_number] = tokenlist[1:]
except TypeError as err:
raise TypeError("Invalid line number: " +
str(err))
def line_numbers(self):
"""Returns a list of all the
line numbers for the program,
sorted
:return: A sorted list of
program line numbers
"""
line_numbers = list(self.__program.keys())
line_numbers.sort()
return line_numbers
def __execute(self, line_number):
"""Execute the statement with the
specified line number
:param line_number: The line number
:return: The FlowSignal to indicate to the program
how to branch if necessary, None otherwise
"""
if line_number not in self.__program.keys():
raise RuntimeError("Line number " + line_number +
" does not exist")
statement = self.__program[line_number]
try:
return self.__parser.parse(statement, line_number)
except RuntimeError as err:
raise RuntimeError(str(err))
def execute(self):
"""Execute the program"""
self.__parser = BASICParser()
line_numbers = self.line_numbers()
if len(line_numbers) > 0:
# Set up an index into the ordered list
# of line numbers that can be used for
# sequential statement execution. The index
# will be incremented by one, unless modified by
# a jump
index = 0
self.set_next_line_number(line_numbers[index])
# Run through the program until the
# has line number has been reached
while True:
flowsignal = self.__execute(self.get_next_line_number())
if flowsignal:
if flowsignal.ftype == FlowSignal.SIMPLE_JUMP:
# GOTO or conditional branch encountered
try:
index = line_numbers.index(flowsignal.ftarget)
except ValueError:
raise RuntimeError("Invalid line number supplied in GOTO or conditional branch: "
+ str(flowsignal.ftarget))
self.set_next_line_number(flowsignal.ftarget)
elif flowsignal.ftype == FlowSignal.GOSUB:
# Subroutine call encountered
# Add line number of next instruction to
# the return stack
if index + 1 < len(line_numbers):
self.__return_stack.append(line_numbers[index + 1])
else:
raise RuntimeError("GOSUB at end of program, nowhere to return")
# Set the index to be the subroutine start line
# number
try:
index = line_numbers.index(flowsignal.ftarget)
except ValueError:
raise RuntimeError("Invalid line number supplied in subroutine call: "
+ str(flowsignal.ftarget))
self.set_next_line_number(flowsignal.ftarget)
elif flowsignal.ftype == FlowSignal.RETURN:
# Subroutine return encountered
# Pop return address from the stack
try:
index = line_numbers.index(self.__return_stack.pop())
except ValueError:
raise RuntimeError("Invalid subroutine return in line " +
str(self.get_next_line_number()))
except IndexError:
raise RuntimeError("RETURN encountered without corresponding " +
"subroutine call in line " + str(self.get_next_line_number()))
self.set_next_line_number(line_numbers[index])
elif flowsignal.ftype == FlowSignal.STOP:
break
elif flowsignal.ftype == FlowSignal.LOOP_BEGIN:
# Loop start encountered
# Put loop line number on the stack so
# that it can be returned to when the loop
# repeats
self.__return_stack.append(self.get_next_line_number())
# Continue to the next statement in the loop
index = index + 1
if index < len(line_numbers):
self.set_next_line_number(line_numbers[index])
else:
# Reached end of program
raise RuntimeError("Program terminated within a loop")
elif flowsignal.ftype == FlowSignal.LOOP_SKIP:
# Loop variable has reached end value, so ignore
# all statements within loop and move past the corresponding
# NEXT statement
index = index + 1
while index < len(line_numbers):
next_line_number = line_numbers[index]
temp_tokenlist = self.__program[next_line_number]
if temp_tokenlist[0].category == Token.NEXT and \
len(temp_tokenlist) > 1:
# Check the loop variable to ensure we have not found
# the NEXT statement for a nested loop
if temp_tokenlist[1].lexeme == flowsignal.ftarget:
# Move the statement after this NEXT, if there
# is one
index = index + 1
if index < len(line_numbers):
next_line_number = line_numbers[index] # Statement after the NEXT
self.set_next_line_number(next_line_number)
break
index = index + 1
# Check we have not reached end of program
if index >= len(line_numbers):
# Terminate the program
break
elif flowsignal.ftype == FlowSignal.LOOP_REPEAT:
# Loop repeat encountered
# Pop the loop start address from the stack
try:
index = line_numbers.index(self.__return_stack.pop())
except ValueError:
raise RuntimeError("Invalid loop exit in line " +
str(self.get_next_line_number()))
except IndexError:
raise RuntimeError("NEXT encountered without corresponding " +
"FOR loop in line " + str(self.get_next_line_number()))
self.set_next_line_number(line_numbers[index])
else:
index = index + 1
if index < len(line_numbers):
self.set_next_line_number(line_numbers[index])
else:
# Reached end of program
break
else:
raise RuntimeError("No statements to execute")
def delete(self):
"""Deletes the program by emptying the dictionary"""
self.__program.clear()
def delete_statement(self, line_number):
"""Deletes a statement from the program with
the specified line number, if it exists
:param line_number: The line number to be deleted
"""
try:
del self.__program[line_number]
except KeyError:
raise KeyError("Line number does not exist")
def get_next_line_number(self):
"""Returns the line number of the next statement
to be executed
:return: The line number
"""
return self.__next_stmt
def set_next_line_number(self, line_number):
"""Sets the line number of the next
statement to be executed
:param line_number: The new line number
"""
self.__next_stmt = line_number