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

PRINT doesn't handle expressions as expected: #41

Closed skx closed 6 years ago

skx commented 6 years ago

So this program prints the wrong result:

10 PRINT 3 + 4 * 5

However, ironically, because of the way we handle builtins this succeeds:

10 DUMP 3 + 4 * 5

As does the sum if you skip the use of print:

10 LET a = 3 + 4 * 5
20 PRINT a, "\n"

Report here:

skx commented 6 years ago

NOTE: The specific problem is that PRINT consumes tokens itself, and tries to guess their types.

If you force the expression to be parsed as an expression - not as a number/string (i.e. the leading 3 gets treated as a number) - then it works as it should:

 10 PRINT ( 3 + 4 * 5 ) , "\n"

Seems like I should just simplify the print-statement, by making it a built-in. That'd fix the problem by accident.