Core Concepts and Stack Manipulation
Stack-Based: Forth is a stack-based language where data is manipulated on a stack. Most operations involve pushing data onto the stack, performing operations on the top elements, and pushing the result back onto the stack.
Reverse Polish Notation (RPN): Forth uses RPN, also known as postfix notation, where operators follow their operands (e.g., 3 4 + instead of 3 + 4 ).
|
Words: Forth programs are built from ‘words’, which are essentially functions or commands. Words are executed in the order they appear.
|
|
Duplicates the top item on the stack.
Example:
10 DUP (Stack: 10 -> 10 10)
|
|
Removes the top item from the stack.
Example:
10 DROP (Stack: 10 -> )
|
|
Exchanges the top two items on the stack.
Example:
10 20 SWAP (Stack: 10 20 -> 20 10)
|
|
Duplicates the second item on the stack and places it on top.
Example:
10 20 OVER (Stack: 10 20 -> 10 20 10)
|
|
Rotates the top three items on the stack, bringing the third item to the top.
Example:
10 20 30 ROT (Stack: 10 20 30 -> 20 30 10)
|
|
Duplicates the top two items on the stack.
Example:
10 20 2DUP (Stack: 10 20 -> 10 20 10 20)
|
Arithmetic and Logical Operations
|
Adds the top two numbers on the stack.
Example:
3 4 + (Stack: -> 7)
|
|
Subtracts the top number from the second number on the stack.
Example:
7 3 - (Stack: -> 4)
|
|
Multiplies the top two numbers on the stack.
Example:
4 5 * (Stack: -> 20)
|
|
Divides the second number on the stack by the top number, returning the quotient.
Example:
20 4 \ (Stack: -> 5)
|
|
Divides the second number on the stack by the top number, returning the remainder.
Example:
22 5 MOD (Stack: -> 2)
|
|
Divides the second number by the top number, returning both the quotient and the remainder (remainder on top).
Example:
22 5 /MOD (Stack: -> 2 4)
|
|
Compares the top two numbers on the stack for equality. Returns TRUE (-1) if equal, FALSE (0) otherwise.
Example:
5 5 = (Stack: -> -1)
5 6 = (Stack: -> 0)
|
|
Compares the top two numbers for inequality. Returns TRUE (-1) if not equal, FALSE (0) otherwise.
Example:
5 6 <> (Stack: -> -1)
5 5 <> (Stack: -> 0)
|
|
Compares if the second number on the stack is less than the top number. Returns TRUE (-1) if true, FALSE (0) otherwise.
Example:
5 10 < (Stack: -> -1)
10 5 < (Stack: -> 0)
|
|
Compares if the second number on the stack is greater than the top number. Returns TRUE (-1) if true, FALSE (0) otherwise.
Example:
10 5 > (Stack: -> -1)
5 10 > (Stack: -> 0)
|
|
Performs a bitwise AND operation on the top two numbers.
Example:
3 6 AND (Stack: -> 2)
|
|
Performs a bitwise OR operation on the top two numbers.
Example:
3 6 OR (Stack: -> 7)
|
|
Performs a bitwise NOT operation on the top number.
Example:
0 NOT (Stack: -> -1)
|
Control Flow and Definitions
IF ... THEN : Conditional execution.
Example:
5 0 > IF ." Greater than zero " THEN
|
IF ... ELSE ... THEN : Conditional execution with alternative.
Example:
5 0 < IF ." Less than zero " ELSE ." Not less than zero " THEN
|
BEGIN ... UNTIL : Looping structure that executes until a condition is true.
Example:
: COUNTDOWN 10 BEGIN DUP . 1 - DUP 0 = UNTIL DROP ;
COUNTDOWN
|
BEGIN ... WHILE ... REPEAT : Looping structure that executes while a condition is true.
Example:
: STARS BEGIN DUP 0 > WHILE DUP . 1 - REPEAT DROP ;
5 STARS
|
: (colon): Starts the definition of a new word.
; (semicolon): Ends the definition of a new word.
Example:
: SQUARE DUP DUP * ;
This defines a new word SQUARE that duplicates the top of the stack and multiplies the two copies, effectively squaring the number.
|
Defining constants and variables:
CONSTANT : Defines a constant.
Example:
10 CONSTANT TEN
TEN . (Output: 10)
VARIABLE : Defines a variable (a memory location).
Example:
VARIABLE X
15 X !
X @ . (Output: 15)
! is used to store a value into the variable and @ is used to fetch the value from the variable.
|