Forth is a programming language that uses two stacks and a dictionary of words.
A Forth environment combines the compiler with an interactive shell, where the user defines and runs subroutines called words. Words can be tested, redefined, and debugged as the source is entered without recompiling or restarting the whole program.
Forth programmers enjoy the immediacy of an interpreter while at the same time the performance and efficiency of a compiler. Forth is used in space applications such as the Philae spacecraft.
I use a custom version of the
lbforth.c REPL, modified to work on Plan9 ARM which can be downloaded here. To experiment with a subset of Forth in the terminal, have a look at Firth, for a graphical exploration try Postscript which also follows the Reverse Polish Notation.
Forth reads from left to right, spaces are separators, when you wish to quit, type
BYE. A stack is a way of managing data. With a stack, data is added to and taken from the "top", as with a stack of dishes. The acronym for this is LIFO: Last In First Out.
To inspect the stack, type
10 ; 20 ; 30 ; .S 30 20 10 OK DROP ; .S 20 10 OK BYE
Forth reads from left to right, there is no operator precedence.
2 3 4 + * \ (3 + 4) * 2 2 3 4 * + \ (3 * 4) + 2 2 3 * 4 + \ (2 * 3) + 4 5 6 + 7 * \ (5 + 6) * 7
The dictionary comes with the Forth system. The programmer writes a program by adding to the dictionary words defined in terms of words in the dictionary. As a rule, Forth finds a word by starting with the most recently defined word and working backwards. If two or more words in the dictionary have the same name, Forth will find the most recently defined and be satisfied.
A colon definition starts with the Forth word
: HELLO ." Hi! " ; HELLO Hi! OK
Because in Forth data is passed implicitly, it is considered insane to define a word without documenting what data it takes from the stack and what data it returns to the stack. The canonical way of doing this is to use the Forth word
( which tells the system to ignore what follows up to and including the next
). Expectations ("before") and results ("after") are separated by --. The resulting ( before -- after ) is a "stack-effect comment".
\ Return square of n : SQUARED DUP * ; 5 SQUARED . 25
You program in Forth by teaching a machine new actions that you and the machine know by name. Each new action is a novel arrangement of known actions, perhaps mixing in some data. By being added to the dictionary the new action can be used to teach still newer actions.
: SQUARED ( n -- n**2 ) DUP * ; : CUBED ( n -- n**3 ) DUP SQUARED * ; : 4TH ( n -- n**4 ) SQUARED SQUARED ;
To define TRUE/FALSE:
\ CONSTANT defines a word that always returns the same value 0 CONSTANT FALSE FALSE . \ = returns a true flag if the top two stack items are equal FALSE FALSE = CONSTANT TRUE TRUE . : EVEN? ( n -- ) 2 MOD IF ." odd" ELSE ." even" THEN ;
: STAR [CHAR] * EMIT ; : STARS 0 DO STAR LOOP CR ; 10 STARS **********
|SWAP||a b||b a||Reverses the top two stack items|
|DUP||a||a a||Duplicates the top stack item|
|OVER||a b||a b a||Copies second item to top|
|ROT||a b c||b c a||Rotates third item to top|
|DROP||a||Discards the top stack item|
- Forth Tutorial
- Original lbforth
- Reverse Polish notation
- Forth Methodology
- Forth Primitives
incoming(2): postscript reverse polish