Posts: 98
Threads: 61
Joined: Jan 2014
Reputation:
2
06-24-2023, 11:54 AM
(This post was last modified: 06-24-2023, 11:55 AM by zarsoft.)
In ZX SPECTRUM:
LET x = 2
PRINT VAL "2*x"
Prints 4
But on the compiler gives 0
How about adding variables from "a" to "z" in VAL?
Posts: 1,766
Threads: 55
Joined: Aug 2019
Reputation:
24
This cannot be done in compiled BASIC. VAL "2*x" will need the ROM VAL, which calls the BASIC interpreter. This will need to store the variables in Sinclair BASIC Format which takes more space and is slow so the ROM interpreter will find the variable "x" in the BASIC Variables region.
Currently, a compiled variable like that of the example takes 1 byte (just the space needed to store the number). For Sinclair BASIC it will always be a Float (5 bytes, and slow) + the variable name "x" encoded in another region, etc.
So VAL here will behave as most BASICs, convert a STRING to a number or return 0 if it cannot decode it (indeed Sinclair BASIC VAL is very powerful).
There is a library function to call the BASIC interpreter, BTW, but it won't work in this case because it still needs the "x" variable.