Core words - vroby65/MiniForthConsole GitHub Wiki
📘 Core Words Reference
This page documents all the built-in words available in the Mini Forth Console.
Each word is case-insensitive and operates on the global stack.
Words can be used in user-defined definitions.
🔢 Arithmetic
Word |
Description |
Example |
+ |
Adds top two numbers |
2 3 + . → → 5 OK |
- |
Subtracts second from top |
5 2 - . → → 3 OK |
* |
Multiplies top two numbers |
4 3 * . → → 12 OK |
/ |
Divides second by top |
10 2 / . → → 5 OK |
🌀 Stack Manipulation
Word |
Description |
Example |
dup |
Duplicates top item |
7 dup . . → → 7 7 OK |
drop |
Removes top item |
1 2 drop . → → 1 OK |
swap |
Swaps top two items |
1 2 swap . . → → 1 2 OK |
over |
Copies second item to top |
1 2 over . . . → → 1 2 1 OK |
clearstack |
Empties the stack |
1 2 3 clearstack .$ → → Stack is empty. OK |
🎯 Output
Word |
Description |
Example |
. |
Prints top item |
42 . → → 42 OK |
.$ |
Prints the full stack |
1 2 3 .$ → → [2: 3] [1: 2] [0: 1] OK |
cr |
Adds a line break |
cr → → OK |
clr |
Clears the console |
clr → (no output) |
🧠 Variables & Memory
Word |
Description |
Example |
variable |
Creates a variable |
variable x |
@ |
Reads value from variable |
x @ . |
! |
Writes value to variable |
42 x ! |
array |
Creates an array |
array a |
*@ |
Pushes all array items to stack |
a *@ .$ |
*! |
Sets array with entire stack |
1 2 3 a *! |
🔁 Control Flow
Word |
Description |
Example |
do ... loop |
Loop with index from start to limit |
0 5 do i . loop |
for ... next |
Loop from 0 to n-1 |
5 for i . next |
i |
Pushes current loop index |
See above |
leave |
Exits a loop early |
5 for i 2 = if leave then i . next |
if ... else ... then |
Conditional execution |
3 4 > if "yes" else "no" then . |
🧱 Word Definitions
Word |
Description |
Example |
: |
Starts a new word definition |
: square dup * ; |
; |
Ends word definition |
See above |
alias |
Creates alias for a word |
alias sqr square |
see |
Shows source of word |
see square |
edit |
Loads definition into input |
edit square |
forget |
Deletes a word or variable |
forget square |
📋 Introspection
Word |
Description |
Example |
words |
Lists all known words |
words |
.v |
Lists variables and values |
.v |
💾 Persistence
Word |
Description |
Example |
reset |
Clears words, variables, stack |
reset |
save |
Downloads current environment |
save |
load |
Loads from file |
load |
⚙️ Misc
Word |
Description |
Example |
$ |
Captures output into stack |
"3 4 +" $ . → → 7 OK |
.* |
Repeats block until stack is empty |
1 2 3 .* . → → 3 2 1 OK |
js |
Executes JavaScript |
"2+3" js . → → 5 OK |
Happy Forth-ing! 🤖