Skip to content

Commit 157a9aa

Browse files
committed
Added input, and read support for Arrays for the two issues
1 parent 6c8f5b5 commit 157a9aa

File tree

2 files changed

+283
-84
lines changed

2 files changed

+283
-84
lines changed

README.md

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ $ python interpreter.py
2727

2828
Although this started of as a personal project, it has been enhanced considerably by some other Github users. You can see them in the list of contributors! It's very much a group endeavour now.
2929

30+
## Recent Updates
31+
32+
### Array INPUT and READ Support
33+
The interpreter now supports reading data directly into array elements using both **INPUT** and **READ** statements:
34+
35+
- **Array Elements as Targets**: Use syntax like `INPUT A(1), B(I+2)` and `READ N$(1), N$(2)`
36+
- **Expression Indices**: Support for complex expressions in array indices like `A(I*2+1)`
37+
- **Mixed Data Types**: Read numeric and string data into appropriate array types
38+
- **Error Handling**: Proper SUBSCRIPT ERROR and OUT OF DATA error messages
39+
- **Specification Compliant**: Follows classic BASIC behavior for all edge cases
40+
41+
See the comprehensive test program `examples/array_input_read_tests.bas` for demonstrations of all functionality.
42+
3043
## Operators
3144

3245
A limited range of arithmetic expressions are provided. Addition and subtraction have the lowest precedence,
@@ -311,8 +324,38 @@ Hello Another Line of Data
311324
>
312325
```
313326

314-
It is a limitation of this BASIC dialect that it is not possible to assign constants directly to array variables
315-
within a **READ** statement, only simple variables.
327+
#### Array Support in READ
328+
329+
The **READ** statement now supports reading data directly into array elements, using expressions for array indices:
330+
331+
```
332+
> 10 DIM A(5)
333+
> 20 DIM N$(3)
334+
> 30 DATA 10, 20, 30, "Hello", "World"
335+
> 40 READ A(1), A(2), A(3), N$(1), N$(2)
336+
> 50 PRINT A(1); A(2); A(3); N$(1); N$(2)
337+
> RUN
338+
102030HelloWorld
339+
>
340+
```
341+
342+
Array indices can be complex expressions including variables and arithmetic:
343+
344+
```
345+
> 10 DIM B(10)
346+
> 20 I = 2
347+
> 30 DATA 100, 200
348+
> 40 READ B(I*3), B(I+4)
349+
> 50 PRINT "B(6)="; B(6) REM Shows 200 (second value overwrites first)
350+
> RUN
351+
B(6)=200
352+
>
353+
```
354+
355+
Proper error handling is provided:
356+
- **SUBSCRIPT ERROR**: When array indices are out of bounds or negative
357+
- **OUT OF DATA**: When there are no more DATA items to read
358+
- Index validation occurs BEFORE consuming DATA items
316359

317360
### Comments
318361

@@ -720,6 +763,41 @@ Num, Str, Num: 22, hello!, 33
720763
>
721764
```
722765

766+
#### Array Support in INPUT
767+
768+
The **INPUT** statement now supports inputting data directly into array elements:
769+
770+
```
771+
> 10 DIM A(3)
772+
> 20 DIM N$(2)
773+
> 30 INPUT "Enter 3 numbers: "; A(1), A(2), A(3)
774+
> 40 INPUT "Enter 2 names: "; N$(1), N$(2)
775+
> 50 PRINT A(1); A(2); A(3); N$(1); N$(2)
776+
> RUN
777+
Enter 3 numbers: 5, 10, 15
778+
Enter 2 names: Alice, Bob
779+
51015AliceBob
780+
>
781+
```
782+
783+
Array indices can use expressions with variables:
784+
785+
```
786+
> 10 DIM B(10)
787+
> 20 I = 5
788+
> 30 INPUT "Enter value for B(I): "; B(I)
789+
> 40 PRINT "B(5)="; B(5)
790+
> RUN
791+
Enter value for B(I): 42
792+
B(5)=42
793+
>
794+
```
795+
796+
Error handling includes:
797+
- **SUBSCRIPT ERROR**: When array indices are out of bounds
798+
- **"? REDO FROM START"**: When input types don't match array types
799+
- Index validation occurs BEFORE prompting for input
800+
723801
A mismatch between the input value and input variable type will trigger an error, and the user will be asked
724802
to re-input the values again.
725803

@@ -927,6 +1005,8 @@ and expanded by Don Woods.
9271005

9281006
* *Pneuma.bas* - A brief 21st century take on the venerable text adventure.
9291007

1008+
* *array_input_read_tests.bas* - A comprehensive test program that validates array INPUT and READ functionality, including expression indices, boundary conditions, error handling, and mixed data types. Demonstrates all the features of array element input and data reading.
1009+
9301010
## Informal grammar definition
9311011

9321012
**ABS**(*numerical-expression*) - Calculates the absolute value of the result of *numerical-expression*

0 commit comments

Comments
 (0)