I'm trying to store a string value into a variable. To define the variable, I use:
: define CREATE 0 , ;
define x
I can easily store an integer/float value to x
using
10 x !
or
10.0e x f!
And to access it I use either @
or f@
. Now I'm trying to store a string value:
s" hello world" x !
The problem with this is that it pushes two values to the stack (since it's a counted string) but x !
will only store the item on top, which is the length of the string. This is dangerous since the stack content might have been modified by the time x
is referenced such that the address is not directly below the length (bad!), so type
would fail. So my question is, is there a way to store both values (address and length) to x
? Or is there a different data type/operand that would let me achieve this?
Any help is appreciated.
A lot of the things that you need to make this work are quite conveniently similar to what you have already.
You need a different version of define
if you want to store two values in the things that you create with it;
: 2define create 0 , 0 , ;
Putting two at the start of a word is a convention that indicates it does the same thing as that word without the two but instead does it on double-cell things.
To use this you would write:
2define 2x
//Write something to 2x
s" Hello world!" 2x 2!
//Retrieve it and print
2x 2@ type
It is worth noting that the address that s"
returns is not guaranteed to last the duration of the program and may be overwritten by a later use of s"
, to see a way to make a string variable that is guaranteed to last have a look at this answer https://stackoverflow.com/a/8748192/547299 (it is a bit long winded but there is a definition of a word called string
which might be illustrative).