I would like to thank those of you who took the time to write and encourage the continuation of this column on the Forth computing environment. I cannot overstress how important reader involvement is to me as a writer and to the continuation of this column. Thanks. Please keep the cards and letters coming and let me know what your interests or needs are.
One of the problems encountered when introducing Forth to others, is explaining how a language that is so strong and rich can and must be extremely simple and elegant. These terms are not as contradictory as they seem on the surface.
I must postpone any meat and potatoes programming for one more issue. The idea and concept of Forth must be the first order or all else will be wasted effort. So, bear with me and really open your minds for this most important segment of your journey into Forth. You won’t regret the time taken to grasp the fundamentals.
Forth is, at its roots, very fundamental and not at all complicated. This is part of the reason that Forth is so compact. In essence, all computers operating with Forth as the operating system are virtual Forth terminals. Despite the fact that there are now three distinct standards (fig-Forth, Forth-79 and Forth-83), nearly all high level Forth is transportable from one terminal to the next.
How is this possible? Forth is basically a stack, on which we may push or pop numbers. Again, it is like a Hewlett Packard calculator. Forth also has a dictionary of words (functions) to provide us with tools to interpret and act on those numbers. When Forth sees a number, it places (pushes) that number on the stack. When it sees a word, it checks the validity of that word and then performs the manipulations to the numbers on the stack that are called for, the result of which is pushed back on the stack.
That’s it! That is all there is to a barebones Forth kernal, and there is very little else to an elaborate one. Because Forth is a Threaded Interpretive Language* (TIL), it is very easy to add words to your dictionary in order to do more with your data when necessary. This ability to expand the Forth language to fit an application instead of bending the application to fit some “cast in stone” language, is called “extensibility.” This is what gives Forth its great flexibility, and we will soon be exploring this virtue in detail.
For the moment, let’s look at some stack manipulation words that are standard to all kernals and used time and time again.
DUP(N->N N)
This word will duplicate the top value of the stack.
SWAP(N1, N2->N2, N1)
This exchanges the top and second value of the stack.
DROP(N-> )
Discard the top value of the stack.
OVER(N1, N2->N1, N2, N1)
This word will copy the second value to the top of the stack.
ROT(N1, N2, N3->N2, N3, N1)
Moves the third value on the stack to the first one.
Can you understand the “stack notation” (in parentheses)? Stack notation is the short hand prose of Forth. Remember the following:
1. The stack is a LIFO stack. This means that the last number pushed on will be the first number popped off, unless of course, we alter the order of the stack. Even then, the altered stack is a LIFO stack.
2. In stack notation, the right most number is the top of the stack, ALWAYS!
Now, can you understand the stack notation for these five very essential words?
Given the following stack:
5 4 2 3 1 1
Use the previous words to convert it as follows:
5 4 3 2 1
The solution will be in the next installment.
There is never one, and only one, correct solution in Forth. There are solutions that are the result of better programming practices, and there are solutions that better use the facilities available in your Forth kernal. Those solutions are without a doubt more elegant, but not necessarily more correct.