Forum
VAL = ? (solved) - Printable Version

+- Forum (https://www.boriel.com/forum)
+-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11)
+---- Forum: Bug Reports (https://www.boriel.com/forum/forumdisplay.php?fid=15)
+---- Thread: VAL = ? (solved) (/showthread.php?tid=2486)



VAL = ? (solved) - zarsoft - 10-31-2023

f$ = "1+2"
PRINT VAL(f$)
gives 3

PRINT VAL("1+2")
gives 0


RE: VAL = ? - boriel - 11-02-2023

Will investigate this (it should work in both cases).

However, bear in mind that VAL in Sinclair BASIC actually calls the ROM interpreter, and that ZX BASIC calls the ROM too for maximum compatibility. However evaluating variables won't work, since "x" must be declared in the Sinclair BASIC variable space, and the compiler does another thing. The same applies for functions (i.e. SQR, SIN, COS...). If you evaluate VAL("SQR 2") it will work if you use the "SQR" token. But if you type "SQR(2)" letter by letter it will fail.

Most BASICs (other than Sinclair) will always return 0 in this case, because VAL only parse numbers, not expressions. Since this is *compiled* BASIC, using dynamic VAL evaluation is complex and costly. However if you need it, there's a function that even run entire BASIC code from within compiled BASIC.


RE: VAL = ? - zarsoft - 11-02-2023

(11-02-2023, 11:11 AM)boriel Wrote: However if you need it, there's a function that even run entire BASIC code from within compiled BASIC.



What function?


RE: VAL = ? - boriel - 11-05-2023

EvalBasic.

Here is an example:
https://github.com/boriel/zxbasic/blob/master/examples/eval.bas


RE: VAL = ? - zarsoft - 11-05-2023

(11-05-2023, 09:08 AM)boriel Wrote: EvalBasic.

Here is an example:
https://github.com/boriel/zxbasic/blob/master/examples/eval.bas





I got this error trying to compile the example:

src/arch/zx48k/library/basic.bas:122: error: Syntax error. Unexpected token 'f' [ID]


RE: VAL = ? - boriel - 11-07-2023

It's a bug, try downloading this beta:
http://www.boriel.com/files/zxb/zxbasic-1.17.2-beta4.tar.gz
http://www.boriel.com/files/zxb/zxbasic-1.17.2-beta4.zip
http://www.boriel.com/files/zxb/zxbasic-1.17.2-beta4-win32.zip
http://www.boriel.com/files/zxb/zxbasic-1.17.2-beta4-linux64.tar.gz
http://www.boriel.com/files/zxb/zxbasic-1.17.2-beta4-macos.tar.gz

That routine only works in 48ik mode (not in +2 mode).


RE: VAL = ? - zarsoft - 11-08-2023

EVAL works, thanks.


RE: VAL = ? - boriel - 01-03-2024

This version uses a more powerful VAL implementation.
http://www.boriel.com/files/zxb/zxbasic-v1.17.3-beta2.tar.gz
http://www.boriel.com/files/zxb/zxbasic-v1.17.3-beta2.zip
http://www.boriel.com/files/zxb/zxbasic-v1.17.3-beta2-win32.zip
http://www.boriel.com/files/zxb/zxbasic-v1.17.3-beta2-linux64.tar.gz
http://www.boriel.com/files/zxb/zxbasic-v1.17.3-beta2-macos.tar.gz

It will evaluate arithmetic expressions (parenthesis, numbers, and - / + *)


RE: VAL = ? - zarsoft - 01-03-2024

Thanks