Words
A word is an identifier for an execution token. To define a new word, first define code inside{ }; then invoke word : followed by the identifier for the word. For instance,
square, which executes dup and * when invoked. Typing 5 square is equivalent to typing 5 dup *, and produces the same result:
3 **5:
: is already defined, it is redefined. However, all existing definitions of other words will continue to use the old definition of the redefined word. For instance, if square is redefined after the definition of **5 above, **5 will continue to use the original definition of square.
Constants
A constant is a word that pushes a predefined value when invoked. Constants can be defined using the wordconstant. For instance,
Gram equal to 10^9. In other words, 1000000000 will be pushed into the stack whenever Gram is invoked:
Integer. For instance, a string constant can be defined in the same way:
2constant. For instance:
pifrac, which will push 355 and 113, in that order, when invoked. The two components of a double constant can be of different types.
If a constant with a fixed name within a definition is needed, use =: and 2=:, instead of constant and 2constant.
The word =: <IDENTIFIER> takes the value at the top of the stack, creates constant <IDENTIFIER> and assigns the value to <IDENTIFIER>.
Similarly, word 2=: <IDENTIFIER> takes the two top-most values in the stack, creates constant <IDENTIFIER> and assigns the values to <IDENTIFIER>.
For instance, the following defines a word setxy, which sets constants x and y:
3 setxy x y +, which is equivalent to 3 dup =: x dup * =: y x y +, changes the stack as follows:
7 setxy x y + has a similar explanation.
To recover the execution-time value of a constant inside a definition, prefix the constant name with the word @'. For instance, using the definition of setxy as above, the following code defines a new word addxy which accesses the constants x and y and adds them:
3 setxy addxy has the same effect as the code 3 setxy x y +. The main difference between 3 setxy addxy and 3 setxy x y + is that in 3 setxy addxy, constants x and y are accessed inside a code definition, which require the use of word @' to access them; while in 3 setxy x y +, the constants are accessed outside a code definition, which does not require the use of word @' to access them.
The drawback of this approach is that @' has to look up the current definition of constants x and y in the dictionary each time addxy is executed. Variables provide a more efficient way to achieve similar results.
Variables
Variables are a much more efficient way to represent changeable values. To declare a variable, use the wordvariable followed by the identifier. Internally, the word variable creates an empty box, which can then be updated with word !, and read with word @.
For instance:
variable produces variables initialized to null. Instead, to create initialized variables to a specific value, use the phrase box constant:
@. This can be mitigated by defining a “getter” and a “setter” word for a variable, and use these words to write better-looking code:
variable-get-set, which creates a fresh variable and takes the two strings following variable-get-set to name the variable’s getter and setter, respectively. For example, variable-get-set x x! will create a variable with getter x and setter x!.
variable-get-set works as follows:
variable-get-set can be used as follows:
variable-get-set, the following implements a simple counter. The example uses auxiliary words reset-counter and incr-counter to reset the counter variable to 0 and increment the counter by one, respectively.
incr-counter works as follows: