A Beginners Guide to Forth
ISBN : -
Cover Design - A Beginners Guide to Forth
For your free electronic copy of this book please verify the numbers below.
(We need to do this to make sure you're a person and not a malicious script)
Sample Chapter From A Beginners Guide to Forth
Copyright © J.V. Noble
3. The structure of Forth
In the Win32Forth window, now type
The Win32Forth window immediately closes.
What just happened? Forth is an interactive programming language con-
sisting entirely of subroutines, called words in Forth jargon.
Interactive means you type things in at the keyboard and the machine
responds. We will see some details of how it does this below.
A word is executed (interactively) by naming it. We have just seen
this happen: BYE is a Forth subroutine meaning exit to the operating
system. So when we typed BYE <cr> BYE was executed, and the system re-
turned control to Windows.
Click on the Win32Forth icon again to re-start Forth.
Now we will try something a little more complicated. Enter
2 17 + . <cr> 19 ok
What happened? Forth is interpretive. A small program called the outer
interpreter continually loops, waiting for input from the keyboard or from
a mass storage device. The input is a sequence of text strings (words or numbers)
separated from each other by the standard Forth delimiter: one or more ASCII blank
(32decimal = 20hex) characters.
The text strings can be interpreted in only three ways: words (subroutine names),
numbers, or not defined.
The outer interpreter tries first to look for an incoming word in the dictionary
(a list of already-defined subroutine names). If it finds that word, the inter-
preter executes the corresponding code.
If no dictionary entry exists, the interpreter tries to read the input as a number.
If the string satisfies the rules defining a number, it is converted to a number
in the machine's internal representation, and stored in a special memory location,
called the top of the stack (TOS).
In the above example, Forth interpreted 2 and 17 as numbers, and
pushed them both onto the stack.
"+" is a pre-defined word as is ".", so they were looked up and exe-
"+" added 2 to 17 and left 19 on the stack.
The word "." (called "dot") removed 19 from the stack and displayed
it on the standard output device (in this case, CRT).
The diagram below is a flow chart representing the actions performed
by the Forth outer interpreter during interpretation.
We might also have said
HEX 0A 14 * . <cr> C8 ok
(Do you understand this? Hint: DECIMAL means switch to decimal arith-
metic, whereas HEX stands for switch to hexadecimal arithmetic.)
If the incoming text can neither be located in the dictionary nor in-
terpreted as a number, Forth issues an error message. Try it: type X <cr>
Error: X is undefined
or type THING <cr> and see
Error: THING is undefined
Finally, here is the obligatory "Hello, World!" program. Forth lets you
output text using the word ." as follows (we will explain in §4 below
what : and ; mean):
: hi ." Hello, World!" ; ok
Now type in hi and see what happens:
hi Hello, World! ok
This can be elaborated with words that tab, emit carriage returns,
display in colors, etc. but that would take us too far afield.
(The word ." means Display the string, following the obligatory blank space
and terminated by the close-quote " on the standard output device.)
Forth belongs to the class of Threaded Interpretive Languages. This
means it can interpret commands (subroutines or programs) typed in
at the console, as well as create (compile) new subroutines and pro-
grams. The compiler in a traditional language has the structure shown
To compile and test a program in a traditional language such
as Fortran, C or Pascal, one prepares an input file, submits it
to a black box that someone else created (the compiler) and then
runs the resulting executable file (which is generally in machine
language). This process can be so tedious that most program
development in traditional languages must be supported by
an elaborate set of programs called the environment,
consisting of integrated editors, debuggers, version control
catalogues and the like.
The outer interpreter/compiler of a Forth system looks like this:
Forth has little in common with the traditional compilation method.
Although the Forth interpreter/compiler diagrammed above looks
complicated, it is simplicity itself compared with the contents of
the oval blob representing a traditional black-box compiler (see the
A continuous loop waits for inputfrom the keyboard, a disk file or
whatever and acts on it according to its nature. Input consists
of a sequence of words and numbers. If a name is recognized it is
executed; if it is not in the dictionary (where else would you keep
a list of words?) Forth tries to convert it to a number and push it
on the stack. If this is impossible, Forth aborts execution, issues an
error message and waits for more input.
As we shall see below, what makes Forth a compiler as well as an
interpreter is the set of words (Forth subroutines) that, when they are
typed in and executed, create new Forth subroutines.