Forth
Not an esolang: the working language that the stack words of other esolangs come from. You program Forth by growing its dictionary until the problem is one word.
Charles H. Moore, 1970 · Turing complete: yes · ●○○ easy · esolangs.org
Tested with 2 known programs · Test method
Run it
The idea
Chuck Moore grew it in the late 1960s as a personal toolkit for programming small computers, and it first appeared as Forth around
Read the story
- The name was meant to be FOURTH, for a fourth-generation language, but the IBM 1130 he used
allowed only five-letter names. It became known for running radio telescopes and other instruments with very little memory.
A Forth system is tiny: a stack, a dictionary of words, and a loop that reads the next word and either runs it or, if it is a number, pushes it. There is almost no syntax. A word is any run of characters between spaces, so 2DUP, . and ?DUP are all ordinary names.
The real idea is the colon definition. : SQUARE DUP * ; adds the word SQUARE to the dictionary, and from then on it is as much a part of the language as DUP. Good Forth style is relentless factoring: many short words, each one a line, until the program reads like a description of itself.
The machine
| Part | What it is |
|---|---|
| Data stack | Numbers. Every word takes its arguments from here and leaves its results here |
| Return stack | Where the system keeps return addresses (and loop counters) |
| Dictionary | A linked list of words, newest first. Looking a word up finds the newest definition |
: NAME … ; | Compile a new word |
Some core words:
| Word | Stack effect |
|---|---|
DUP DROP SWAP OVER ROT | Shuffle the stack |
+ - * / MOD | Arithmetic |
. | Pop and print a number |
EMIT CR ." | Print a character, a newline, a string |
IF … ELSE … THEN | Run a branch if the top is non-zero |
DO … LOOP I | Counted loop; I is the counter |
Example
Build words on words:
Build words on words:
: SQUARE DUP * ;
: CUBE DUP SQUARE * ;
3 CUBE . \ prints 27
A word keeps the meanings it was compiled with. Redefining a word later does not change older words that use it:
: GREET ." Hello" ;
: TWICE GREET GREET ;
: GREET ." Bye" ;
TWICE CR GREET \ prints HelloHello, then Bye
An unknown word stops everything with Forth's famously terse error: FOO ?.
This implementation is a toy. Real Forth compiles to threaded code, lets you add immediate words that run while compiling (which is how IF and DO are themselves defined), and gives you raw memory.
Patterns it teaches
- Concatenative: a definition is literally a sequence of words, and any run of them can be factored into a new word.
- Stack machines: the parameter stack replaces named arguments.
- Metacircular interpreter: much of a Forth system is written in Forth, on top of a few dozen primitives.
Try this
- Define
FOURTHthat raises a number to the fourth power usingSQUARE. - Write
COUNTDOWNthat prints5 4 3 2 1usingBEGIN … UNTIL. - Hard: define
GREETtwice as above, but makeTWICEpick up the new meaning. What would you need to change in Forth, and what doesDEFINEdo instead?
Further
- Forth on Wikipedia
- Leo Brodie, Starting Forth (1981), the classic introduction; and his Thinking Forth on factoring.
- Elizabeth Rather, Donald Colburn and Charles Moore, "The Evolution of Forth" (HOPL II, 1993).
- Joy, Forth's functional cousin; FALSE, a Forth-like esolang.
See also
Meta-languages
Stack-based
Write your own in Run Convert a program from Forth
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: forth.
Command line
shib run forth -e ': STARS 0 DO 42 EMIT LOOP ; 5 STARS'shib manual forthSource
- Module
langs/museum/ports.js- Interpreter
langs/museum/forth.js,langs/common.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org