skx / gobasic

A BASIC interpreter written in golang.
https://blog.steve.fi/tags/basic/
GNU General Public License v2.0
328 stars 27 forks source link

Expected identifier after DATA #88

Closed udhos closed 5 years ago

udhos commented 5 years ago

Can anyone spot the problem causing the error below?

$ gobasic bubble.bas 
Error running program:
    Line 130 : Expected identifier after DATA - found Token{Type:( Value:(}
$ 
$ more bubble.bas 
100 DATA "HELLO", "WORLD", "BASIC", "BUBBLE", "SORT", "DEMO", "GO"
110 LET J=7
120 FOR N=1 TO J
130 READ A$(N)
140 NEXT
150 PRINT "SORTING ARRAY"
160 GOSUB 1000
170 PRINT "SORTING ARRAY - DONE"
180 PRINT
190 FOR N=1 TO J
200 PRINT A$(N)
210 NEXT
220 END
1000 REM BUBBLE SORT ARRAY
1010 REM INPUT: A$()=ARRAY J=ARRAY_SIZE
1020 REM OUTPUT: A$()=SORTED_ARRAY
1030 REM OTHER VARIABLES USED: FLIPS, N
1040 REM
1050 LET FLIPS=1
1060 WHILE FLIPS
1070 LET FLIPS=0
1080 FOR N=1 TO J-1
1090 IF A$(N)>A$(N+1) THEN SWAP A$(N), A$(N+1): LET FLIPS=1
1100 NEXT N
1110 WEND
1120 RETURN
$ 
skx commented 5 years ago

The following program demonstrates the same problem, more simply:

 10 DIM a(3)
 20 FOR I = 1 TO 3
 30  READ a[I]
 40 NEXT I
 50 DATA 3, 4, 1, 7 , 9

The problem is that I've always required "READ $VAR", and that case wasn't updated to cope with arrays.

Genuine bug:

That said your program is going to struggle, since you use A$(N) rather than what I expect A$[N]. Also you invoke wend and swap, neither of which I support - or have heard of!

skx commented 5 years ago

(I'll close this issue when READing from DATA into an array works.)

udhos commented 5 years ago

I found that sorting code here: http://www.antonis.de/qbebooks/gwbasman/whilewend.html

Then I added the missing parts to make it work. It works as is with PC-BASIC (https://github.com/robhagemans/pcbasic)

skx commented 5 years ago

We're hitting conflict because gwbasic is not what I've implemented, this BASIC is closest to ZX Spectrum BASIC - which was simple.

I expect users who wish to port programs will be forced to make changes.

udhos commented 5 years ago

No conflicts, man. I've been suggesting small improvements that would help running GW-BASIC dialects AND would NOT hurt ability to run ZX Spectrum dialect. Of course you would code those enhancements only if you enjoy them. No bad feelings. Cheers.