Forth Cheat Sheet
Core Forth stack-manipulation words, custom word definitions, conditionals, and loops for this minimalist stack-based language.
Stack Manipulation
Core words for manipulating the data stack.
5 3 + . \ push 5, push 3, add, print top -> 810 DUP * \ duplicate 10 then multiply -> 1001 2 SWAP \ stack becomes: 2 13 4 DROP \ removes top of stack -> 31 2 OVER \ copies 2nd item to top -> 1 2 1
Defining Words
Creating new words with : ... ; and stack-effect comments.
: SQUARE ( n -- n^2 ) DUP * ;5 SQUARE . \ prints 25: GREET ( -- ) ." Hello, World!" CR ;GREET
Conditionals
IF ELSE THEN branching.
: TEST ( n -- ) DUP 0> IF ." Positive" ELSE ." Not positive" THEN ;5 TEST \ prints Positive
Loops
DO LOOP for counted iteration, BEGIN UNTIL for conditional iteration.
: COUNT-UP ( n -- ) 1 DO I . LOOP ;5 COUNT-UP \ prints 1 2 3 4: COUNTDOWN ( n -- ) BEGIN DUP . 1- DUP 0= UNTIL DROP ;
Return Stack Manipulation
Move values between the data stack and return stack with >R, R>, and R@ — useful for preserving loop indices across nested word calls.
: SAVE-AND-USE ( a b -- a+b ) >R \ push b onto return stack, leaving only a DUP . \ operate on a R> \ pop b back onto data stack + ;\ R@ peeks the top of the return stack without popping: SHOW-LOOP-INDEX 5 0 DO R@ . \ inside a DO LOOP, R@ reads the loop index I LOOP ;\ CAUTION: unbalanced >R / R> across a word boundary corrupts\ the return address and crashes the interpreter.
Memory Access: VARIABLE, CONSTANT, @ !
Allocate named memory cells and read/write them directly with fetch (@) and store (!).
VARIABLE COUNTER0 COUNTER ! \ store 0 into COUNTERCOUNTER @ . \ fetch and print -> 01 COUNTER +! \ increment in place (+! adds to a cell)42 CONSTANT ANSWERANSWER . \ prints 42\ CREATE ... ALLOT reserves a block of memory (like an array)CREATE BUFFER 100 ALLOT65 BUFFER C! \ store a byteBUFFER C@ . \ fetch a byte -> 65
CREATE ... DOES> — Defining New Defining Words
DOES> attaches runtime behavior to words created by a custom defining word, the core metaprogramming idiom in Forth.
: CONSTANT2 ( n "name" -- ) CREATE , \ compile n into the new word's data field DOES> @ ; \ runtime action: fetch and push it5 CONSTANT2 FIVEFIVE . \ prints 5\ Build an array-defining word: ARRAY ( n "name" -- ) CREATE CELLS ALLOT DOES> SWAP CELLS + ; \ ( index -- addr )10 ARRAY SCORES3 SCORES ! \ error: DOES> word returns addr, not value3 5 SCORES ! \ scores[3] := 5 -- addr computed then stored
Compile-Time vs Interpret-Time: [ ] IMMEDIATE POSTPONE
Words tagged IMMEDIATE execute during compilation instead of being compiled — the mechanism behind Forth's control-flow words themselves.
: MY-IF ( flag -- ) POSTPONE IF ; IMMEDIATE\ [ ] drop into interpret mode inside a colon definition,\ useful for computing a literal at compile time: SCALE ( n -- n*100 ) [ 10 10 * ] LITERAL \ 100 is computed once, at compile time * ;\ IMMEDIATE words let you build your own structured control\ flow words that expand inline, just like IF/THEN/DO/LOOP do.
Advanced Stack Word Reference
Less common but essential stack shuffling words beyond DUP/SWAP/DROP/OVER.
- ROT- rotates the third item to the top: ( a b c -- b c a )
- -ROT- rotates the top item to third place: ( a b c -- c a b )
- 2DUP- duplicates the top pair: ( a b -- a b a b )
- 2DROP- drops the top pair: ( a b -- )
- 2SWAP- swaps two pairs: ( a b c d -- c d a b )
- TUCK- copies top item below the second: ( a b -- b a b )
- PICK- copies the nth item to the top: ( ... n -- ... x )
- ?DUP- duplicates only if the top of stack is non-zero
Always write the stack-effect comment `( before -- after )` immediately after a word's name — Forth has no type checking, so this comment is the only documentation of what a word expects and leaves on the stack.