-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbasicdata.py
More file actions
130 lines (101 loc) · 4.35 KB
/
Copy pathbasicdata.py
File metadata and controls
130 lines (101 loc) · 4.35 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
#! /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 DATA Statements.
This is a list of DATA statements, ordered by
line number.
"""
from basictoken import BASICToken as Token
from lexer import Lexer
class BASICData:
def __init__(self):
# Dictionary to represent data
# statements, keyed by line number
self.__data = {}
# Data pointer
self.__next_data = 0
def delete(self):
self.__data.clear()
self.__next_data = 0
def delData(self,line_number):
if self.__data.get(line_number) != None:
del self.__data[line_number]
def addData(self,line_number,fIndex):
"""
Adds the supplied token list
to the program's DATA store. The first token should
be the line number. If a token list with the
same line number already exists, this is
replaced.
line_number: Basic program line number of DATA statement
fIndex: if >= 0: location in loaded program file of statment
if < 0: Location in temporary basic workfile
"""
try:
self.__data[line_number] = fIndex
except TypeError as err:
raise TypeError("Invalid line number: " + str(err))
def readData(self,read_line_number,infile,tmpfile):
if len(self.__data) == 0:
raise RuntimeError('No DATA statements available to READ ' +
'in line ' + str(read_line_number))
data_values = []
line_numbers = list(self.__data.keys())
line_numbers.sort()
#print("+++",self.__next_data,line_numbers)
if self.__next_data == 0:
self.__next_data = line_numbers[0]
elif line_numbers.index(self.__next_data) < len(line_numbers)-1:
self.__next_data = line_numbers[line_numbers.index(self.__next_data)+1]
else:
raise RuntimeError('No DATA statements available to READ ' +
'in line ' + str(read_line_number))
if self.__data[self.__next_data] >= 0:
infile.seek(self.__data[self.__next_data])
tokenlist = Lexer().tokenize((infile.readline().replace("\n","")).replace("\r",""))[1:]
else:
tmpfile.seek(-(self.__data[self.__next_data]+1))
tokenlist = Lexer().tokenize((tmpfile.readline().replace("\n","")).replace("\r",""))[1:]
sign = 1
for token in tokenlist[1:]:
if token.category != Token.COMMA:
if token.category == Token.STRING:
data_values.append(token.lexeme)
elif token.category == Token.UNSIGNEDINT:
data_values.append(sign*int(token.lexeme))
elif token.category == Token.UNSIGNEDFLOAT:
data_values.append(sign*eval(token.lexeme))
elif token.category == Token.MINUS:
sign = -1
#else:
#data_values.append(token.lexeme)
else:
sign = 1
return data_values
def restore(self,restoreLineNo):
if restoreLineNo == 0 or self.__data.get(restoreLineNo) != None:
if restoreLineNo == 0:
self.__next_data = restoreLineNo
else:
line_numbers = list(self.__data.keys())
line_numbers.sort()
indexln = line_numbers.index(restoreLineNo)
if indexln == 0:
self.__next_data = 0
else:
self.__next_data = line_numbers[indexln-1]
else:
raise RuntimeError('Attempt to RESTORE but no DATA ' +
'statement at line ' + str(restoreLineNo))