Working on the compiler
This section is for changing Moggi itself. To use the language, start at quickstart.md; a release carries every runtime it needs, and none of what follows is needed for that.
The compiler is PHP and needs 8.6.0 or newer (MINIMUM_PHP in
src/compiler.php); it compiles Moggi to PHP, the JVM and .NET. Everything here
assumes Linux or macOS with Nix; without Nix, php moggi.php … and
php test.php … run the same two entry points with whatever is installed, and
the jvm and .NET backends then need a JDK and the .NET 8 SDK on PATH.
Setup
curl -L https://nixos.org/nix/install | sh -s -- --daemon
# log out and back in, or: newgrp nix-users
nix develop
The shell pins PHP 8.6.0 — its libsodium XOF is what the crypto library’s
SHAKE binds, and its OpenSSL 3.5 what carries Keccak — together with the
.NET 8 SDK, GraalVM CE (Java 25 plus native-image), Composer, Maven
and ext-parallel with the upstream PHP 8.6 closure and packed-array fixes
(1.2.15 segfaults when a thread starts; the shell pins the commit that became
1.2.16). It also puts three wrappers on PATH:
| Command | Runs |
|---|---|
moggi |
php moggi.php |
runtest |
php test.php |
runalltest |
php test-all.php |
The shell points MOGGI_MICRO_SFX at
.dist-cache/runtimes/<target>/php-native/micro.sfx when a packaging run has
prepared one, which is what makes moggi compile … --native work from a
checkout. A fresh checkout has none — the micro runtime is a PHP source build —
so build it first if that path is absent:
php packaging/assemble.php --variants moggi-minimal
That runtime is built from PHP 8.6, and the shell, the bundled php runtime a
PHAR runs on and the micro runtime a native launcher is are all 8.6.0, so the
compiler’s floor is 8.6.0 too: a native launcher is the micro runtime, so the
floor cannot pass what that runtime can build. The micro runtime is compiled by
static-php-cli’s v3 branch, pinned by commit in dist/runtimes.json because 3.0
exists only as alpha nightlies — no release tag to pin an asset from — so
packaging/runtimes.php builds the tool from that source archive with the pinned
Composer. Nothing in that tree is patched: the Zig version probe that xz’s
-Werror trips over is fixed upstream at the pinned commit. The released
ext-parallel still segfaults on 8.6, so the shell builds the upstream develop
commit instead; the runtime itself carries it as spc builds it. The shell and
the crypto library’s PHP backend would be on 8.6 even without that, because
SHAKE’s libsodium XOF and Keccak’s OpenSSL 3.5 are only there.
With direnv, direnv allow loads the same shell from
nix/shell.nix (package list in nix/devshell.nix, shared with flake.nix).
Build and run
moggi compile examples/twice # -> Main.phar (entry file's name)
moggi compile examples/twice -o app.phar
moggi compile examples/twice -o out --unpacked # generated tree
moggi run examples/twice # compile + run
moggi run examples/twice --backend jvm
moggi compile examples/twice --native # standalone executable
moggi compile examples/twice --native --static # no dynamic loader or shared libs
moggi compile examples/twice --native --musl # link against musl libc
moggi compile examples/twice --native --package # executable + non-system libraries
Which flags each OS/backend supports, and how the result is verified, is the build-option matrix.
moggi compile accepts a file or a directory; a directory keeps its layout
under -o. Without -o the artifact is named after the entry source file —
hello.mog → hello.phar, a directory whose entry is Main.mog → Main.phar
(.jar on the JVM, .dll on .NET). Inspect stages with --tokens, --ast, --typed-ast, --ir,
--opt-ir; --no-opt compiles without the optimizer. The user-facing account
of those flags is pipeline.md.
Test
runtest # compiler suite, php backend
runtest --backend all # php, jvm, dotnet; one verdict
runtest --group semantics # one group or subject
runtest backend/codegen/Int-Arithmetic # one case
runtest --list --json # discover without running
runalltest --backend all # compiler + every library suite
runtest is test.php; runalltest is test-all.php, which runs the compiler
suite and then the base, json, containers, time and filepath library
suites. testing.md is the contract: goldens, backend selection,
adding a case, and the native smoke.
Debugging a hang
Sample the running process instead of bisecting. phpspy dumps PHP stacks of a live pid:
git clone https://github.com/adsr/phpspy.git /tmp/phpspy # outside the tree
cd /tmp/phpspy && make
./phpspy --limit=1000 --pid=$(pgrep -n php) > /tmp/php.traces
It needs ptrace permission on the target (sudo or a permissive
kernel.yama.ptrace_scope); without it the trace is empty, which is not
evidence of a bug. Reproduce with a direct php moggi.php compile first, then
sample that pid.
Layout
| Path | What lives there |
|---|---|
moggi.php |
CLI shim; all behaviour lives under src/ |
src/cli/ |
argv parsing, dispatch, one file per subcommand |
src/syntax/ |
lexer, parser, AST, desugaring |
src/semantics/ |
kinds, types, classes/evidence, IO boundary, intrinsics |
src/modules/ |
project graph, imports/exports, evidence plan, linking |
src/ir/ |
IR types, lowering, visitors, projection |
src/optimize/ |
IR passes and their driver |
src/backend/ |
PHP / JVM / .NET emitters, runtimes, packaging |
src/docs/, src/repl/, src/lsp/ |
mogdoc/moogle, interactive session, language server |
libraries/ |
the standard library, in Moggi |
tests/ |
suite, goldens, fixtures |
examples/ |
programs that are cases by existing |
packaging/, dist/ |
distribution assembly and pinned runtimes |
docs/ |
this documentation |
Where to read next
- architecture.md — phases, representation contracts, the invariants a change must preserve.
- testing.md — how a change is judged.
- optimizations.md — the IR model, pass order, adding a pass.
- packaging.md — distributions, releases, version identity.
A first change usually follows the same path: find the phase that owns the
concept in architecture.md, add a fixture under tests/,
run its group with runtest --group …, and inspect the produced stage with the
matching --ast / --ir / --opt-ir flag before touching a golden.