READY. LOADING FORTH-70 KERNEL FROM PAPER TAPE... ok

WHAT IS A STACK?

Forth (the language, ~1970, born on an IBM 1130 and later famous on the minicomputers that ran radio telescopes) has almost no syntax. Instead it has one big idea: everything happens on a stack. A stack is just a pile of numbers where you can only see the top one. You can push a number onto it, or pop the top number off. That's it. No variables, no expressions, no operator precedence — just a pile of numbers and a tiny vocabulary of instructions ("words") that rearrange the pile.

push 3        stack: ( 3 )
push 4        stack: ( 3 4 )
add           stack: ( 7 )        <-- add pops 4 and 3, pushes 7

WORDS: FORTH HAS NO FUNCTIONS, ONLY WORDS

In Forth every instruction, built-in or user-defined, is called a word. A word reads zero or more numbers off the top of the stack, does something, and pushes zero or more numbers back. There's no distinction between "operator" and "function" — + is a word, DUP is a word, and the word SQUARED you define yourself is exactly as first-class as the built-in ones.

: SQUARED  DUP * ;      \ define a new word "SQUARED"

5 SQUARED .             \ prints 25

\ trace of 5 SQUARED:
5           stack: ( 5 )
DUP         stack: ( 5 5 )     <-- duplicate the top item
*           stack: ( 25 )      <-- multiply pops 5 and 5, pushes 25
.           stack: ( )         <-- print pops 25 and prints it

The colon : starts a new word definition, its name comes next (SQUARED), then the sequence of existing words that make it up, and ; ends the definition. Once defined, SQUARED can be used exactly like DUP or + — Forth doesn't know or care that you just wrote it yourself five seconds ago. This is why Forth systems are described as "extensible": you're not writing programs in the language so much as growing the language until your problem is a single word.

WORD REFERENCE (CORE)

WordStack effectWhat it does
DUP( n -- n n )duplicate the top item
DROP( n -- )discard the top item
SWAP( a b -- b a )swap the top two items
OVER( a b -- a b a )copy the 2nd item to the top
+ - * /( a b -- n )pop two, push the result
.( n -- )pop and print the top item

That comment notation "( n -- n n )" is called a stack effect diagram: what the stack looks like before the "--", and what it looks like after. It's the closest thing Forth has to a type signature.


FORTH vs. REVERSE POLISH NOTATION (RPN)

Forth's surface syntax, numbers and words separated by spaces, evaluated left to right against a stack, is Reverse Polish Notation. Every Forth program is a stream of RPN. But RPN itself is older and narrower: RPN is just a notation for arithmetic expressions (made famous by HP calculators like the HP-35 and HP-12C), while Forth takes that same postfix idea and turns it into an entire general-purpose, extensible programming language.

Reverse Polish NotationForth
What it is a notation for writing arithmetic without parentheses a complete programming language built around a stack
Example 3 4 + means (3 + 4) 3 4 + . computes 3+4 and prints it
Vocabulary fixed: numbers and a handful of math operators open-ended: you define new words that extend the language itself
Data usually just numbers numbers, addresses, strings, and control-flow all ride the same stack(s)
Control flow none — it's just an expression grammar has it: IF...ELSE...THEN, DO...LOOP, recursion
Precedence rules none needed — that's the entire point of postfix notation none needed, for the same reason, applied to a whole language

Put another way: every RPN calculator expression is valid Forth, but Forth also lets you name a sequence of stack operations, save it as a new word, and use it forever after — something plain RPN, being just a notation and not a language, has no mechanism for. An HP calculator in RPN mode evaluates 3 ENTER 4 + and shows 7. Forth does the same underlying stack dance, but also lets you write : ADD-THEM + ; once and reuse ADD-THEM in every program you ever write again.


ok  

See also: 1959 LISP console (prefix / Polish notation) · 1985 dial-up BBS · 1993 Mosaic homepage · 1991 Gopher menu · return to the surface (WWW homepage)