Skip to main content

mforth — a pragmatic Forth dialect that compiles to Mindustry mlog

License: MIT Python: 3.11+

mforth is a small Forth designed for a single, narrow target: the bytecode (mlog) that Mindustry's in-game logic processors execute. It ships two surfaces over one front end:

  • a host REPL (mforth repl / mforth run example.fs) that executes Forth against a Python simulation of the Mindustry world (a MockWorld + event stream), so you can author and debug without leaving your editor;
  • an AOT compiler (mforth compile example.fs -o example.mlog) that emits paste-ready mlog text you can drop straight into a Mindustry logic processor.

Both surfaces share one parser, one AST, one dictionary, and one static stack-checker. The headline correctness property is that the same .fs source produces the same observable events under the host REPL as it does when compiled to mlog and executed — the REPL is a teaching surface, and divergence between it and the compiler is the highest-severity regression class.

A pygls-based LSP server (mforth lsp), a tree-sitter grammar for syntax highlighting, and a tiny web visualizer for the host REPL's event stream all ship in the same package — they're just different subscribers to the same pipeline.

Quick start

pip install mforth

mforth repl                                          # interactive Forth REPL
mforth run examples/counter.fs                       # run a script under the host
mforth compile examples/counter.fs -o counter.mlog   # compile to mlog text
mforth lsp                                           # stdio LSP for editors
mforth version                                       # print version

mforth run <file> --no-loop exits after one pass instead of honoring mlog's auto-loop semantics — handy for unit-style smoke tests.

Example

examples/counter.fs is the minimal v1 demo: increment a variable and print it to a message block.

VARIABLE n

: tick ( -- )
  n @ 1 + n !       \ n := n + 1
  n @ PRINT         \ queue the value into the print buffer
  display PRINTFLUSH \ flush to the sidecar-bound display block
;

tick

Paired with a sidecar examples/counter.world.toml that binds the symbolic name display to the in-game message1 block:

[links]
display = { target = "message1" }

[clock]
ipt = 8

Compiled with mforth compile examples/counter.fs -o counter.mlog, this produces 7 mlog instructions ready to paste into a Mindustry logic processor. Running the same source via mforth run examples/counter.fs exercises it against a MockWorld and emits the same observable event stream.

A slightly larger sibling, examples/blink.fs, adds a string prefix and explicit 1 WAIT pacing — it exercises the full set of load-bearing v1 dialect features in one program (: / ;, VARIABLE / @ / !, integer arithmetic, string literals, PRINT / PRINTFLUSH / WAIT, sidecar-bound block references).

Status

This is v1 — the pragmatic-Forth dialect (POSTPONE / IMMEDIATE / DOES> / EXECUTE deliberately out of scope), static stack analysis mandatory, inline-everything codegen, no return stack, no memory cells. The full design and the optimization roadmap are captured in the MemPalace decision drawers drawer_mforth_decisions_3827fd238edc64f763e7b96b (v1 locked design) and drawer_mforth_decisions_d8910d0d11f2ce62c712c2ab (v2 optimization tiers, fast > small).

v1 substantive work is complete: lex → parse → resolve → stackcheck → host REPL → mlog backend → LSP → tree-sitter grammar → web viz, with 729+ tests pinning behavior and a property test that asserts REPL ↔ mlog event-stream equivalence on every shipped example.

Documentation

The full documentation is published at https://fkberthold.github.io/mforth/, organized per the Diataxis framework:

  • Tutorials — guided, learn-by-doing. Start with Getting started (5 min), then Writing mforth for Mindustry (the 6-part tutorial that ports a real mlog script from the Mindustry wiki).
  • How-to guides — task-focused recipes (wiring mforth into Helix or Neovim, pairing with a sidecar).
  • Reference — the dictionary word list, sidecar schema, CLI subcommands.
  • Explanation — design rationale, the REPL ↔ mlog equivalence property, dialect-choice trade-offs.

For source-level deep dives beyond what the docs site covers, the MemPalace decision drawers cited in Status above are the authoritative record.

Development

git clone https://github.com/fkberthold/mforth.git
cd mforth
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest -q                          # full test suite
pytest tests/unit -q               # fast unit tests
pytest --cov                       # with coverage (85% gate on compiler core)

License

MIT — see LICENSE.

Metadata

Release files for mforth 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mforth 0.1.2
File Size Uploaded Provenance Actions
mforth-0.1.2.tar.gz 244.7 kB Without provenance Details for mforth-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mforth 0.1.2
File Interpreter ABI Platform Actions
mforth-0.1.2-py3-none-any.whl Python 3 none any Details for mforth-0.1.2-py3-none-any.whl

Total release size: 370.9 kB

Release files / mforth-0.1.2.tar.gz

Download URL mforth-0.1.2.tar.gz
Size 244.7 kB
Tags Source
SHA-256 checksum
How to use checksums
819b019ac31293b75155b607afddf7470019a5a1b4d8f4202ff4e53eb84c0f5b
BLAKE2b-256 checksum
How to use checksums
15b02c83e4fc93d10ed1f4e1c5cdfbe2ba81873da54dfcc3254c5ed7009985f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / mforth-0.1.2-py3-none-any.whl

Download URL mforth-0.1.2-py3-none-any.whl
Size 126.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c54f80c46f5e885544f8a5792bf806d8e378c3a477e9a81f71cfef04ea8b9cd1
BLAKE2b-256 checksum
How to use checksums
6a864c345bb54cb969ef28771ecc17e64ff0322241e5617e1f1df55055a429d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for mforth 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page