When Forth is started in an interactive mode, you can talk to it.
You can enter definitions that can be used immediately after
they are entered completely.
If the token cannot be found in the active search order of the
dictionary then it is not considered a word. Next Forth tries to
convert it to a number. If the token can be converted to a number
then this number is placed on the data stack.
If the token is neither a word nor a number then it is an error and
reported as such.
Let's put two numbers on the data stack and add them.
1234 8765 + ok
Where is the result? On top of the data stack. To print the topmost
number from the data stack and remove it use the word
.
. 9999 ok
Let's see if the operands are still on the stack:
. 0
Error: "." stack underflow
Obviously not. Forth tried however to print and remove a number from
the stack (the 0 above) and ran into the error condition that
the stack contained -1 elements. After an error is reported the stack
is reset to empty.
A better way to see what's on the stack is the word
.S
.s ok
1 2 3 ok
.s
3 [00000003]
2 [00000002]
1 [00000001] ok
+ + . 6 ok
.s ok
.S nondestructively prints all numbers on the stack.
In the PFE numbers are printed both in decimal and in hex. The
topmost stack element is printed first.
In this example, the colon parses away the following white-space
delimited token and creates a new definition then named PRINT-SUM.
Then compilation state is entered. From now on each word is not
executed but compiled. This means not the execution semantics is
performed but an alternative compilation semantics.
The compilation semantics of + and . is to add
their respective execution semantics to the word in creation.
The final ;-semicolon
has the compilation semantics to finish compilation of the word
defined by the preceding colon.
The compilation semantics of most words is to add their execution
semantics to the compiled word. There must be some special words
too with other compilation semantics like ;-semicolon in
order to control the compilation.
Now we have new word named PRINT-SUM which has both the
execution semantics of + and .. Instead of the
sequence + . we can furtheron say PRINT-SUM: