Forth-Word is the term for a definition of any kind. Words are stored in the Forth-Dictionary where they can be looked up by the Forth system.
Having read a token Forth tries to look it up in the dictionary. Not the whole dictionary is search but only a subset defined by the active search order. The first occurence of the token in the search order of the dictionary is taken as the definition of the entered word.
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.
Each word is associated a meaning, i.e. an action it performs when executed, the word's execution semantics.
When the only syntax analysis a Forth system performs is seperating
space-delimited tokens then a word can contain any character with
the exception of white-space. For instance
+
is a good name for a word. The word
+
in fact exists and its execution semantics is to add the two
topmost items in the data stack and replace them by their result.
Let's put two numbers on the data stack and add them.
1234 8765 + okWhere 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 okLet's see if the operands are still on the stack:
. 0 Error: "." stack underflowObviously 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
.sok 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.
Programming an application in Forth means building bottom up a hierarchy of words until a single word or a small set of application specific words solves the problem.
When Forth is about build a new word it's in compiling
state. The usual way to build a new word and enter compiling
state is using the word
:
('colon')
like this:
: PRINT-SUM + . ;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:
1234 8765 + . 9999 ok 1234 8765 PRINT-SUM 9999 ok
bye
.
Using
quit
will reset the current interpreter. If that is not sufficient
try
abort
.