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

Subtraction not processed #120

Closed eotect closed 1 year ago

eotect commented 1 year ago

gobasic version v1.8 under windows 7 x64 Code: LET a="Hello, World!" LET b=LEN a PRINT "b=",b,"\n" b=b-1 PRINT "b=",b,"\n"

Output: b= 13 b= 13

eotect commented 1 year ago

I figured it out, i need to put a space after -. changed b=b-1 to b=b- 1, and it works.

skx commented 1 year ago

Thanks for the bug-report, and the work-around.

It seems this is caused by bad parsing (as you can tell!) interestingly the other operations are working as expected:


LET a="Hello, World!"
LET b=LEN a
PRINT "b=",b,"\n"
b=b+1
PRINT "b=",b,"\n"
b=b*3
PRINT "b=",b,"\n"
b=b/6
PRINT "b=",b,"\n"
b=b-37
PRINT "b=",b,"\n"

Shows:

$ ./gobasic bug.bas 
b= 13 
b= 14 
b= 42 
b= 7 
b= 7 # WRONG 
skx commented 1 year ago

Looks like this is a consequence of the tokenizer.

Which explains why adding the space fixes it. Frustratingly this is a hard thing to fix, without losing support for negative numbers. I'll have to ponder whether it can be done, or whether it should be resolved by documenting it.

eotect commented 1 year ago

i found my issue title was totally wrong... glad you addressed the problem, i don't know about parsing tokens, but a variable followed by negative numbers make no senses. maybe there are some method for helping the parser make right decisions? thanks for this program i enjoy playing with it.