The VikiD Truth

Knowledge base / Computing / Smalltalk, Forth & others

Algorithms | Apple | AssemblyLanguage | BASH | Bash Cookbook | C-Programmming | CommonLisp | Computer Security | CuisSmalltalk | Databases | Emacs | Exploitation | Firewalls | Forth | Fortran | Functional Programming | Git | GNUSmalltalk | Haskell | Javascript | Julia | Linux | Machine Learning | Metasploit |Machine Learning | Networking | Operating Systems | Open Data Science | Odin | Picolisp | Pharo Smalltalk | Purescript | Python | Racket | Rust| Scheme | Social Engineering | Unity | vim | V | Web Development | Windows |Zig

Forth

Here is the truth about Forth: it is a language that barely exists until you write it. A tiny core of stack operations, and then every new word you define becomes part of the language. Few systems take “grow your own” this literally – PicoLisp is one; Forth is another.

What it is, in plain words

Forth was created by Charles Moore in the early 1970s. It is a stack-based, concatenative language: you push numbers onto a data stack, and words (functions) consume and produce stack values. Source looks like this:

3 4 + .          \ prints 7
: square ( n -- n^2 )  dup * ;
5 square .       \ prints 25

There is almost no syntax. Words are separated by spaces. : name … ; defines a new word. Everything else is convention. Implementations are famously tiny – a working Forth can fit in a few kilobytes – which is why it conquered embedded systems, bootloaders (Open Firmware), NASA instruments and hobby microcontrollers.

Modern free Forths include Gforth (GNU), SwapForth, Mecrisp (for ARM Cortex-M), and Factor (a richer concatenative cousin).

Why it matters

Forth is a masterclass in minimalism and interactivity. You build programs bottom-up, testing each word at the REPL as you go – the same joy as Lisp, with a completely different texture. If you care about small systems, language design, or how a REPL can be the system, Forth is worth a weekend. Moore’s aesthetic – extreme simplicity, direct hardware control – rhymes with why some of us prefer Void, runit and PicoLisp.

An everyday analogy: a RPN calculator that grows new buttons

A reverse-Polish calculator: type 3 4 +. Now imagine you can etch a new button labelled SQUARE that means dup *. Then a button that uses SQUARE. Keep etching. That’s Forth – a calculator whose panel you extend until the panel is your program.

flowchart LR
  I[input stream] --> Interp[outer interpreter]
  Interp --> S[data stack]
  Interp --> D[dictionary of words]
  D --> S

A hands-on example

sudo xbps-install -S gforth     # or: apt install gforth
gforth
\ stack effect comments: ( inputs -- outputs )
: square ( n -- n^2 )  dup * ;
: sum-of-squares ( a b -- n )  square swap square + ;

3 4 sum-of-squares . cr          \ 25

\ conditionals and loops
: fizzbuzz ( n -- )
  1+ 1 do
    i 15 mod 0= if ." FizzBuzz "
    else i 3 mod 0= if ." Fizz "
    else i 5 mod 0= if ." Buzz "
    else i . then then then
  loop cr ;
20 fizzbuzz

\ create a tiny "database" – a defining word
: cell: ( n "name" -- )  create ,  does> @ ;
100 cell: limit
limit . cr                       \ 100

The does> family – words that create other words with custom behaviour – is Forth’s meta-power, analogous to Lisp macros in spirit if not in mechanism.

Talking to hardware (conceptual – Mecrisp on a microcontroller):

: led-on   ( -- )  1 25 io! ;
: led-off  ( -- )  0 25 io! ;
: blink    ( -- )  begin led-on 200 ms led-off 200 ms key? until ;

Why the stack does not drive you mad

Beginners fear losing track of the stack. The discipline that prevents it:

  1. Keep words tiny – three to five operations.
  2. Always write the stack-effect comment ( in -- out ).
  3. Prefer named locals / value where your Forth supports them for longer words.
  4. Test every word at the prompt before building on it.

That last point is Forth culture: the REPL is not a luxury, it is the method.

Common misconceptions

Connections

Last edited · 3 revisions

Tags: #Computing