Skip to content

Commit e120c40

Browse files
authored
Merge pull request #90 from krperry/list-negative-fix
Fixed the problem where negative numbers have space between '-' and n…
2 parents d4a5c5e + ca5cb3f commit e120c40

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

README.md

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ Program terminated
213213
### Statement structure
214214

215215
As per usual in old school BASIC, all program statements must be prefixed with a line number which indicates the order in which the
216-
statements may be executed. A statement may be modified or
216+
statements may be executed. There is no renumber command to allow all line numbers to be modified. A statement may be modified or
217217
replaced by re-entering a statement with the same line number:
218218

219219
```
@@ -389,6 +389,40 @@ Proper error handling is provided:
389389
- **OUT OF DATA**: When there are no more DATA items to read
390390
- Index validation occurs BEFORE consuming DATA items
391391

392+
#### Array Support in READ
393+
394+
The **READ** statement now supports reading data directly into array elements, using expressions for array indices:
395+
396+
```
397+
> 10 DIM A(5)
398+
> 20 DIM N$(3)
399+
> 30 DATA 10, 20, 30, "Hello", "World"
400+
> 40 READ A(1), A(2), A(3), N$(1), N$(2)
401+
> 50 PRINT A(1); A(2); A(3); N$(1); N$(2)
402+
> RUN
403+
102030HelloWorld
404+
>
405+
```
406+
407+
Array indices can be complex expressions including variables and arithmetic:
408+
409+
```
410+
> 10 DIM B(10)
411+
> 20 I = 2
412+
> 30 DATA 100, 200
413+
> 40 READ B(I*3), B(I+4)
414+
> 50 PRINT "B(6)="; B(6) REM Shows 200 (second value overwrites first)
415+
> RUN
416+
B(6)=200
417+
>
418+
```
419+
420+
Proper error handling is provided:
421+
- **SUBSCRIPT ERROR**: When array indices are out of bounds or negative
422+
- **OUT OF DATA**: When there are no more DATA items to read
423+
- Index validation occurs BEFORE consuming DATA items
424+
425+
392426
### Comments
393427

394428
The **REM** statement is used to indicate a comment, and occupies an entire statement. It has no effect on execution:
@@ -1230,11 +1264,8 @@ a subroutine call, or program termination. This paradigm of using the parser to
12301264
object to make control flow decisions and to track execution, and a signalling mechanism to allow the parser to signal
12311265
control flow changes to the Program object, is used consistently throughout the implementation.
12321266

1233-
## Open issues
1267+
## Python Basic feature
12341268

1235-
* It is not possible to renumber a program. This would require considerable extra functionality.
1236-
* Negative values are printed with a space (e.g. '- 5') in program listings because of tokenization. This does not affect functionality.
1237-
* User input values cannot be directly assigned to array variables in an **INPUT** or **READ** statement
12381269
* Strings representing numbers (e.g. "10") can actually be assigned to numeric variables in **INPUT** and **READ** statements without an
12391270
error, Python will silently convert them to integers.
12401271

program.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,31 @@ def str_statement(self, line_number):
175175
statement = self.__program[line_number]
176176
if statement[0].category == Token.DATA:
177177
statement = self.__data.getTokens(line_number)
178-
for token in statement:
178+
179+
# Track operators and contexts where minus might be unary
180+
operators_and_contexts = {
181+
Token.PLUS, Token.MINUS, Token.TIMES, Token.DIVIDE, Token.MODULO,
182+
Token.ASSIGNOP, Token.EQUAL, Token.NOTEQUAL, Token.GREATER,
183+
Token.LESSER, Token.LESSEQUAL, Token.GREATEQUAL,
184+
Token.LEFTPAREN, Token.COMMA, Token.AND, Token.OR
185+
}
186+
187+
for i, token in enumerate(statement):
179188
# Add in quotes for strings
180189
if token.category == Token.STRING:
181190
line_text += '"' + token.lexeme + '" '
182-
183191
else:
184-
line_text += token.lexeme + " "
192+
# Check if this is a minus sign that should be treated as unary
193+
if (token.category == Token.MINUS and
194+
i + 1 < len(statement) and
195+
statement[i + 1].category in {Token.UNSIGNEDINT, Token.UNSIGNEDFLOAT} and
196+
(i == 0 or statement[i - 1].category in operators_and_contexts)):
197+
# This is a unary minus, don't add space after it
198+
line_text += token.lexeme
199+
else:
200+
# Normal token, add space after
201+
line_text += token.lexeme + " "
202+
185203
line_text += "\n"
186204
return line_text
187205

0 commit comments

Comments
 (0)