An Overview of Forth
Forth is a computer programming language invented in 1970 by Charles Moore.
This version of Forth, called WebForth, is designed to teach you how to program in Forth.
Programming Languages
Computers are only useful when they have programs to run on them. Every
program must be crafted using a computer programming language.
Because the kinds of problems which people want to solve are so varied,
many programming languages have been developed to help do this.
There are several types of programming languages. The main types are:
-
Procedural
-
Functional
-
Logic
-
Object Oriented
Most languages, including Forth, are procedural languages.
Features of Forth
Forth is a compact language. The main philosophy of Forth is: keep things
simple by being flexible. Flexibility is achieved by encouraging the programmer
to extend the language until the problem solution is realized as a computer
program. The challenge of this approach is that the small details become
the concern of the programmer just as much as the large scale is.
Forth gives a unique opportunity for you to understand the underlying
hardware of the computer itself. Traditionally Forth gives direct access to all the memory
of the computer.
Words
Every procedure in Forth is called a word. Forth knows several hundred
words. You create new words using the core set to build your programs.
Every word that you build is added to the words already available.
The more you use Forth, the more flexible it becomes.
Data Handling
Forth uses a data structure called a stack. The majority of languages use
a data stack, but Forth does not hide it from you. The stack is a temporary
store for data which are needed by a word. Every word that expects
data looks automatically to the top of the stack to find the data it needs. If a word
returns data; the data is put onto the stack ready for the next word
to use. In consequence, Forth uses fewer variables than the majority of
programming languages.
Arithmetic
Another consequence of the direct use of a data stack is post-fix arithmetic.
This takes a bit of getting used to but is not difficult. The arithmetic
operators are Forth words and need their data just like any other
word. To add two numbers together, you make sure they are on the
stack before the operator is used. For example: to calculate 8+2 in Forth, you first put
the numbers on the stack:
8 2
You do this merely by typing the numbers and then pressing the [ENTER] key. Then use the operator
+
Again, you just type "+" and press [ENTER].
This word adds the two numbers and puts the answer back onto the stack
10
which you can see by using the print word, which is the full stop character:
.
A side effect of post-fix arithmetic is that brackets to indicate precedence
become unnecessary.