[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Forth is interactive, you can talk to it. You can enter definitions that can be used immediately after they are entered completely.
1.0.1 The "ok"-prompt | How Forth prompts for input. | |
1.0.2 Words and Numbers | What Forth looks for in your input. | |
1.0.3 Executing Words | How to do simple actions. | |
1.0.4 Compiling new words | How to build simple new definitions. |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
One peculiarity of Forth is that it doesn’t prompt you for input.
Instead it praises you for entering a good line of input with the
ok
prompt. An empty line is a good line. So if you want to check if your
Forth system is alive and well, simply type <<return>>. If it prints
– on the same line – one space and ok
then it is ready for
you. Your next input is expected on the next line.
<<return>> ok _
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When reading your input Forth looks for tokens separated by whitespace. Almost no further syntax analysis takes place. Basically such a token can be one of two things: A word or a number.
Word is the term for a definition of any kind. Words are stored in the 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 dictinary is searched but only a subset defined by the active search order. The first occurrence 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.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When numbers are put on the stack, what happens to the words found in the input stream? Unless you are about defining a new word (see below) they are executed.
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 systems performs is separating
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 + 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 <stacks empty> ok 1 2 3 ok .s 3 [00000003] 2 [00000002] 1 [00000001] ok + + . 6 ok .s <stacks empty> ok _
.S
nondestructively prints all numbers on the stack. In
pfe the numbers are printed both in decimal and in hex. The topmost
stack element is printed first.
Try the following:
9 8 7 6 5 4 3 2 1 ok * .S
reenter the second line several times.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Programming in Forth means adding words to the dictionary. When adding a new word every word already in the dictionary can be used by adding it’s execution semantics to the execution semantics of the newly created word.
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 building 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
:
reads the following white-space delimited token and creates a new
definition named PRINT-SUM
. Then enters compilation state. From
now on each word is not executed but compiled.
This means that 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 compilation semantics of ;
is to finish compilation of the
word defined by the preceeding :
.
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 ;
in order to control the
compilation.
Now we have a 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
[Top] | [Contents] | [Index] | [ ? ] |
This document was generated on November 5, 2024 using texi2html 5.0.
The buttons in the navigation panels have the following meaning:
Button | Name | Go to | From 1.2.3 go to |
---|---|---|---|
[ << ] | FastBack | Beginning of this chapter or previous chapter | 1 |
[ < ] | Back | Previous section in reading order | 1.2.2 |
[ Up ] | Up | Up section | 1.2 |
[ > ] | Forward | Next section in reading order | 1.2.4 |
[ >> ] | FastForward | Next chapter | 2 |
[Top] | Top | Cover (top) of document | |
[Contents] | Contents | Table of contents | |
[Index] | Index | Index | |
[ ? ] | About | About (help) |
where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:
This document was generated on November 5, 2024 using texi2html 5.0.