-
FORTH uses a data stack to perform operations.
-
Operands are pushed onto the stack, and operators consume values from the top of the stack.
Example:
3 4 + \ pushes 3 and 4, then adds them → stack contains 7
-
Expressions are written in postfix notation: operands first, then the operator.
Example:
2 5 * \ equivalent to 2 * 5 in infix notation
- FORTH is a concatenative language: programs are created by chaining words (functions or commands).
- Each word operates on the implicit stack.
-
The
:keyword defines a new word (function):Example:
: SQUARE ( n -- n² ) DUP * ;DUPduplicates the top of the stack.*multiplies the top two values on the stack.
- FORTH works in two modes:
- Interpretation mode: immediate execution.
- Compilation mode: defining new words.
- Enables direct and interactive control of the system.
- The core of FORTH is small and minimal.
- Users can define new words to build domain-specific languages.
- Offers low-level control over the system.
- FORTH is lightweight and fast.
- Popular in embedded systems, and used by organizations like NASA.
- Comments are enclosed in parentheses:
( This is a comment )
: CUBE ( n -- n³ ) DUP DUP * * ;
5 CUBE . \ Outputs 125