Forth is a small, interactive, stack-based language created by Charles H. "Chuck" Moore. Moore had been refining the ideas through the 1960s, and the language took recognisable shape around 1970 at the National Radio Astronomy Observatory, where he used it to control a radio telescope. Learning how Forth stack-based programming works feels strange for an hour and obvious after a day, and it explains why Forth can run on hardware too small for almost anything else.

Origins: Chuck Moore and the telescope

Moore built Forth as a personal toolkit during the 1960s, carrying it from machine to machine. At NRAO he wrote telescope control and data acquisition software with it, and astronomers at other observatories soon adopted it. The name comes from "fourth", as in fourth-generation software, shortened to five letters because the IBM 1130 he was using allowed only five-character file names.

In 1973, Moore and Elizabeth Rather co-founded FORTH, Inc. to sell Forth systems. In the late 1970s the Forth Interest Group published fig-Forth, a free public-domain implementation for many microprocessors, which spread Forth among hobbyists.

How Forth stack-based programming works

A Forth system is a dictionary of named routines called words, plus two stacks:

  • The data stack holds the values that words work on. Words take their inputs from the top of the stack and leave their results there.
  • The return stack holds return addresses, and is also used for loop counters and temporary storage.

Because arguments are already on the stack, Forth uses postfix notation, also known as reverse Polish notation. To add 2 and 3, you push both and then run +: 2 3 +. The interpreter simply reads space-separated tokens. If a token is a word in the dictionary, it executes it; if it is a number, it pushes it. There is almost no syntax.

A colon definition, : name ... ;, compiles a new word into the dictionary. Once defined, a new word is as much a part of the language as +. Programmers build a vocabulary for their problem and write the application in it. Stack comments such as ( n -- n*n ) document what a word consumes and produces.

A short Forth example

: SQUARE  ( n -- n*n )  DUP * ;

: SUM-SQUARES  ( n -- sum )
  0 SWAP 1+ 1 DO  I SQUARE +  LOOP ;

10 SUM-SQUARES .   \ prints 385

DUP duplicates the top value, SWAP exchanges the top two, DO ... LOOP counts from 1 up to but not including 11, and I pushes the current index. The final . prints and removes the top of the stack.

Design features that made Forth small

  • Threaded code. Compiled words are mostly lists of addresses of other words, which makes programs extremely compact.
  • Interpreter and compiler in one. You test each word interactively the moment you write it, even on the target hardware.
  • Extensibility. CREATE ... DOES> and immediate words let programmers add new data structures and even new control structures.
  • Tiny footprint. A complete system with editor, compiler and interpreter could fit in a few kilobytes.

How Forth was used

Forth found its niche wherever memory was scarce and hardware had to be controlled directly: observatory and laboratory instruments, industrial controllers, point-of-sale terminals, and instruments on spacecraft. The British Jupiter Ace home computer (1982) shipped with Forth instead of BASIC. Most famously, Open Firmware, standardised as IEEE 1275 in 1994, used Forth as its boot firmware language on Sun SPARC workstations, IBM POWER systems and Apple's PowerPC Macintosh computers.

YearStandard or milestone
Around 1970Forth in use at NRAO
1979Forth-79 standard
1983Forth-83 standard
1994ANS Forth (ANSI X3.215-1994); IEEE 1275 Open Firmware
2012Forth 2012 standard

Influence and legacy

Forth is the classic concatenative language, where programs are built by putting words one after another. That idea continued in Joy, Factor and Hewlett-Packard's RPL calculator language. Forth also influenced hardware: Moore and others designed stack processors that run Forth almost directly, and Moore later built colorForth and the GreenArrays multi-core chips.

Is Forth still used today?

Yes, quietly. Commercial systems such as SwiftForth from FORTH, Inc. and VFX Forth from MPE are still sold, the free Gforth runs on most platforms, and small Forths remain popular on microcontrollers because they give you an interactive prompt on the chip itself. It will never be mainstream, but once you understand how Forth stack-based programming works, you see why embedded engineers keep coming back to it.

Frequently asked questions

Why does Forth use reverse Polish notation?

Because every word takes its arguments from the stack, operands must be pushed before the operation runs. Postfix order matches that directly, so the interpreter needs no parser for operator precedence or parentheses. This is a big part of why Forth systems are so small.

Is Forth a compiled or interpreted language?

Both. The outer interpreter reads and executes words interactively, while colon definitions are compiled into the dictionary, often as threaded code. Many modern Forths also compile to native machine code for speed.

Is Forth good for microcontrollers?

It is one of the best-suited languages for them. A Forth system can fit in a few kilobytes of flash and give you an interactive command line on the device. You can test hardware registers and new words live, without a separate compile, flash and reboot cycle.