PudgeWithMoML is a MiniML → RISC-V compiler written in OCaml. It implements type inference, closures, partial application and a small runtime with garbage collection.
source.ml
└─▶ parse ─▶ infer ─▶ α-rename ─▶ ANF ─▶ closure conversion ─▶ λ-lift ─▶ RISC-V
let, recursion, lambdas, tuples, lists and pattern matching- Hindley–Milner-style type inference and polymorphism
- first-class functions, closures and arbitrary arity
- a RISC-V runtime with partial application and copying GC
- expect and cram tests for individual compiler stages
- property-based parser tests with QCheck
The implementation is split into explicit compiler stages:
lib/frontend/— AST, Angstrom parser, type inference and typed treeslib/middle_end/— alpha conversion, ANF, closure conversion and lambda liftinglib/riscv/— RISC-V machine model and assembly generationlib/runtime/— closure application, primitive functions and copying garbage collectorbin/compiler.ml— command-line driver connecting all stagestest/— parser, type inference, ANF, code generation, runtime and QCheck tests
The frontend parses an ML-like source program, checks it and constructs the top-level type environment. The middle end gives every binding a unique name, converts expressions to ANF, eliminates free variables through closure conversion and lifts nested functions to the top level. The backend then emits RV64 assembly, which is linked with the C and assembly runtime.
Inline expect tests cover individual transformations and printers. Dune cram tests exercise the command-line compiler and generated programs, while QCheck verifies parser/pretty-printer round trips on generated ASTs.
You will need OCaml/opam, Dune, the RISC-V GNU toolchain and qemu-riscv64.
cd PudgeWithMoML
opam install . --deps-only --with-test
make test
make compile input=bin/tests/fact
qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 main.exeExpected output:
24
dune exec compiler -- -fromfile bin/tests/fact -dparsetree
dune exec compiler -- -fromfile bin/tests/fact -dtypes
dune exec compiler -- -fromfile bin/tests/fact -anf
dune exec compiler -- -fromfile bin/tests/fact -ccBuilt by Gleb Nasretdinov and Ilhom Kombaev. Licensed under LGPL-3.0-or-later.