Skip to content

Learn Forth with mforth

A from-zero introduction to Forth — the language — using mforth and its built-in Mindustry simulator. You do not need to know Forth, and you do not need to have played Mindustry. By the end you'll think in stacks, define your own words, and write small controllers that react to a simulated world.

This is the prequel to Writing mforth for Mindustry: that tutorial assumes you already think in stacks; this one teaches you how.

How the exercises work

Every chapter ends with a few short exercises, and each one checks itself. You write a .fs file, then run:

$ mforth check my-solution.fs
✓ forth-101/01-double — 3/3 cases pass

or, when something's off:

✗ forth-101/01-double — 1/3 cases pass
  case 1: 5 double . → printed "15", expected "10"
  hint: DUP makes a second copy, then + adds them.

Handy flags:

  • mforth check --list — see every bundled exercise.
  • mforth check --scaffold <id> — write a starter file for an exercise.
  • mforth check --solution <id> — reveal the reference answer when you're stuck.

Your solution carries one marker line so the checker knows which exercise it is — for example \ @exercise forth-101/01-double. The --scaffold command writes that line for you.

Part I — Thinking in Forth

No game knowledge needed. Each concept is small, and every exercise is checkable.

# Chapter You'll learn
1 The stack & postfix Numbers, + - * /, ., and why 3 4 +
2 Juggling the stack DUP DROP SWAP OVER ROT
3 Defining words : name … ; and naming things
4 Arithmetic & truth MOD, < > =, AND OR NOT
5 Branching IF / ELSE / THEN
6 Looping BEGIN/UNTIL, DO/LOOP, I
7 State & variables VARIABLE, @, !
8 Output S" …", PRINT, and .
9 Factoring Composing small, well-named words
10 Words that make words CREATE, ,, DOES>, and building CONSTANT yourself
11 Macros MACRO: name … ; — compile-time substitution

Part II — Forth in the simulator

Now we make it real: read and react to a simulated Mindustry world.

# Chapter You'll learn
12 Meet the simulator The world, blocks, the .world.toml sidecar
13 Reading the world SENSOR and @-properties
14 Acting on the world CONTROL-ENABLED & friends
15 A control loop Sense → decide → act → WAIT
16 Capstone: the sorter Build a real controller, milestone by milestone
17 Where next Compile to real mlog, optimize, and keep going

Ready? Start with Chapter 1 — The stack & postfix.