Using values. 4tH 4tH Forth compiler
Реклама
Реклама
CHAPTER 8. 4TH ARRAYS
When you access the first element of an array of constants you use this construction: sizes 0 th @c
79
The only difference is the word ’@C’, which is exclusively used to access arrays of constants.
8.4
Using values
A value is a cross-over between a variable and a constant. May be this example will give you an idea:
declaration:
variable a
1 constant b
2 b + value c
( No initial value)
( Literal expression assigned at compiletime)
( Expression assigned at runtime)
fetching:
a @ b c
( Variable throws address on stack)
( Constant throws value on stack)
( Value throws value on stack)
storing:
2 b + a !
2 b + to c
( Expression can be stored at runtime)
( Constant cannot be reassigned)
( Expression can be stored at runtime)
In many aspects, values behave like variables and can replace variables. The only thing you cannot do is make arrays of values. A value is not a literal expression either, so you can’t use them to size arrays. In fact, a value is a variable that behaves in certain aspects like a constant.
Note that although ’VALUE’ and ’TO’ are aliases, it is more portable and more readable to use ’VALUE’ for declaration and ’TO’ for reassignment.
8.5
Creating string variables
In 4tH you have to define the maximum length of the string, like Pascal:
10 string name
You cannot add the ’CHARS’ keyword, since ’STRING’ already implies that you are creating an array of characters. Note that the string variable includes the terminator. That is a special character that tells 4tH where the string ends (see section 8.14). You usually don’t have to add that yourself because 4tH will do that for you. But you will have to reserve space for it.
Изтегляне
Реклама