Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
VAL = ? (solved)
#1
f$ = "1+2"
PRINT VAL(f$)
gives 3

PRINT VAL("1+2")
gives 0
Reply
#2
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.
Reply
#3
(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?
Reply
#4
EvalBasic.

Here is an example:
https://github.com/boriel/zxbasic/blob/m...s/eval.bas
Reply
#5
(11-05-2023, 09:08 AM)boriel Wrote: EvalBasic.

Here is an example:
https://github.com/boriel/zxbasic/blob/m...s/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]
Reply
#6
It's a bug, try downloading this beta:
http://www.boriel.com/files/zxb/zxbasic-...ta4.tar.gz
http://www.boriel.com/files/zxb/zxbasic-...-beta4.zip
http://www.boriel.com/files/zxb/zxbasic-...-win32.zip
http://www.boriel.com/files/zxb/zxbasic-...x64.tar.gz
http://www.boriel.com/files/zxb/zxbasic-...cos.tar.gz

That routine only works in 48ik mode (not in +2 mode).
Reply
#7
EVAL works, thanks.
Reply
#8
This version uses a more powerful VAL implementation.
http://www.boriel.com/files/zxb/zxbasic-...ta2.tar.gz
http://www.boriel.com/files/zxb/zxbasic-...-beta2.zip
http://www.boriel.com/files/zxb/zxbasic-...-win32.zip
http://www.boriel.com/files/zxb/zxbasic-...x64.tar.gz
http://www.boriel.com/files/zxb/zxbasic-...cos.tar.gz

It will evaluate arithmetic expressions (parenthesis, numbers, and - / + *)
Reply
#9
Thanks
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)