Forth Language Reference

st-forth implements a subset of ANS Forth with a 4-tier vocabulary system.

Vocabulary Tiers

Each tier builds on the previous one. Use the tier selector to choose which words are available.

TierDescription
Kernel~35 Rust-native primitives only
Core+ ~20 derived stack/math words (NIP, TUCK, 2DUP, MAX, MIN...)
Standard+ boolean constants, formatting (TRUE, FALSE, AND, OR...)
Full+ utility words (SQUARE, CUBE, GCD, STARS...)

Stack Operations

WordStack EffectDescription
DUP( x -- x x )Duplicate top of stack
DROP( x -- )Discard top of stack
SWAP( a b -- b a )Swap top two items
OVER( a b -- a b a )Copy second item to top
ROT( a b c -- b c a )Rotate third item to top
?DUP( x -- x x | 0 )Duplicate if non-zero
DEPTH( -- n )Stack depth
.S( -- )Print entire stack
>R( x -- ) R:( -- x )Move to return stack
R>( -- x ) R:( x -- )Move from return stack
R@( -- x ) R:( x -- x )Copy from return stack

Arithmetic

WordStack EffectDescription
+( a b -- a+b )Add
-( a b -- a-b )Subtract
*( a b -- a*b )Multiply
/( a b -- a/b )Integer divide
MOD( a b -- a%b )Modulo
/MOD( a b -- rem quot )Division with remainder
NEGATE( n -- -n )Negate
ABS( n -- |n| )Absolute value

Comparison

Comparison words return -1 (true) or 0 (false).

WordStack EffectDescription
=( a b -- flag )Equal
<( a b -- flag )Less than
>( a b -- flag )Greater than
<>( a b -- flag )Not equal
0=( n -- flag )Equal to zero
0<( n -- flag )Less than zero
0>( n -- flag )Greater than zero

Memory

WordStack EffectDescription
@( addr -- n )Fetch from memory
!( n addr -- )Store to memory
+!( n addr -- )Add to memory
VARIABLEVARIABLE nameCreate a variable
CONSTANTn CONSTANT nameCreate a constant
ALLOT( n -- )Reserve n cells

Input/Output

WordStack EffectDescription
.( n -- )Print number
EMIT( c -- )Print character
CR( -- )Print newline
SPACE( -- )Print space
TYPE( str -- )Print string
." text"( -- )Print inline string

Control Flow

WordUsageDescription
IF...THENflag IF ... THENConditional
IF...ELSE...THENflag IF ... ELSE ... THENConditional with else
DO...LOOPlimit start DO ... LOOPCounted loop
DO...+LOOPlimit start DO ... step +LOOPCounted loop with step
I( -- index )Current loop index
J( -- index )Outer loop index
BEGIN...UNTILBEGIN ... flag UNTILLoop until true
BEGIN...WHILE...REPEATBEGIN ... flag WHILE ... REPEATLoop while true
RECURSERECURSECall current word recursively

Defining Words

WordUsageDescription
: ;: name ... ;Define a new word
VARIABLEVARIABLE nameCreate a variable (use @ and ! to access)
CONSTANTvalue CONSTANT nameCreate a constant
IMMEDIATEIMMEDIATEMark last word as immediate