Forth
A stack-based, extensible programming language known for its simplicity, interactivity, and use in embedded systems, firmware, and space exploration.
Created by Charles H. Moore
Forth is a unique programming language that challenges conventional programming paradigms. Its stack-based architecture, extreme simplicity, and interactive development style have made it a favorite for embedded systems, real-time applications, and situations where resources are limited.
History & Origins
In the late 1960s, Charles H. “Chuck” Moore was frustrated with the overhead of existing programming languages. While working at Mohasco Industries in Amsterdam, New York, he began developing a personal programming system on an IBM 1130 with just 8K of memory.
Moore called it “Fourth” because he considered it a fourth-generation language. However, the IBM 1130 only allowed five-character identifiers, so it became FORTH. The name stuck, though it’s now typically written as “Forth.”
The NRAO Years
In 1970, Moore brought Forth to the National Radio Astronomy Observatory (NRAO), where he used it to control radio telescopes. Elizabeth Rather learned the language and became its first user besides Moore. Together, they founded FORTH, Inc. in 1971, the first commercial Forth vendor.
What Made Forth Different
Forth was revolutionary in several ways:
- Interactive development - Write and test code immediately, no compilation cycle
- Extensibility - The language is built from the same primitives users employ
- Minimal overhead - The entire system could fit in kilobytes, not megabytes
- Direct hardware access - Perfect for embedded systems and real-time control
Why Forth Still Matters
1. The Stack-Based Model
Forth uses a data stack for passing parameters between words (functions). This eliminates named parameters and creates a very different mental model:
| |
This “reverse Polish” notation (like HP calculators) seems strange at first but becomes natural. Each word takes its input from the stack and leaves its output there.
2. Extensibility
In Forth, you define new words using existing words. There’s no distinction between built-in commands and user definitions:
| |
The entire language, including control structures, is built this way.
3. Interactive Development
Forth systems are interactive by nature. You type a word, and it executes immediately. This encourages incremental development and testing:
ok 2 3 + .
5 ok
ok : GREET ." Hello!" ;
ok GREET
Hello! ok
4. Minimal Resource Requirements
A complete Forth system can run in less than 8KB of memory. This made it invaluable for early microcomputers and continues to make it relevant for resource-constrained embedded systems.
Forth Dialects and Implementations
| Implementation | Description | Status |
|---|---|---|
| Gforth | GNU Forth, reference ANS implementation | Very active |
| SwiftForth | Commercial, FORTH Inc. | Active |
| VFX Forth | High-performance commercial | Active |
| pForth | Portable Forth in C | Maintained |
| Mecrisp | Forth for ARM Cortex-M | Active |
Modern Forth Implementations
Gforth (GNU Forth)
The most widely used open-source Forth:
| |
Online Interpreters
Several web-based Forth interpreters exist for quick experimentation without installation.
Key Concepts
The Stack
Forth uses two stacks:
- Data stack - for parameters and results
- Return stack - for return addresses (and temporary storage)
| |
Words
Everything in Forth is a “word” - a named operation. Defining new words extends the language:
| |
Stack Notation
Forth uses a standard notation for stack effects:
| |
Language Features
Defining Words
| |
Control Structures
| |
String Handling
| |
Getting Started
Forth source files typically use .fs, .fth, or .4th extensions. The basic structure is simply definitions followed by execution:
| |
Continue to the Hello World tutorial to write your first Forth program.
Timeline
Notable Uses & Legacy
Space Exploration
Forth powered systems on the Philae lander (Rosetta mission) that landed on a comet in 2014, and has been used in numerous NASA missions due to its reliability and small footprint.
Open Firmware
The Open Firmware boot standard used by Apple (PowerPC Macs), Sun Microsystems, and IBM uses Forth as its user interface and scripting language.
Embedded Systems
Forth's minimal memory requirements and interactive development make it ideal for microcontrollers, industrial automation, and real-time systems.
PostScript
Adobe's PostScript page description language was heavily influenced by Forth's stack-based architecture and extensibility model.
GreenArrays Chips
Charles Moore designed the GreenArrays GA144 - a chip containing 144 independent Forth processors, demonstrating Forth's suitability for parallel computing.
Language Influence
Influenced By
Influenced
Running Today
Run examples using the official Docker image:
docker pull forthy42/gforth:latestExample usage:
docker run --rm -v $(pwd):/app -w /app forthy42/gforth gforth hello.fth -e bye