Skip to content
Oeiuwq Faith Blog OpenSource Porfolio

coord-e/mlml

self-hosted compiler for a subset of OCaml

coord-e/mlml.json
{
"createdAt": "2019-04-04T07:24:20Z",
"defaultBranch": "develop",
"description": "self-hosted compiler for a subset of OCaml",
"fullName": "coord-e/mlml",
"homepage": "",
"language": "OCaml",
"name": "mlml",
"pushedAt": "2019-06-12T13:36:31Z",
"stargazersCount": 51,
"topics": [
"compiler",
"ocaml",
"self-hosted"
],
"updatedAt": "2025-03-13T21:48:37Z",
"url": "https://github.com/coord-e/mlml"
}

Build Status Coverage Status Docker Cloud Automated build Docker Cloud Build Status MicroBadger Image

mlml is a self-hosted toy compiler for a tiny subset of OCaml.

a detailed description can be found in my blog post (in Japanese): 自作OCamlコンパイラでセルフホストした - molecular coordinates

  • basic arithmetic
  • variables
  • if-then-else
  • functions
    • recursion
    • mutual recursion
    • closure
    • currying
  • tuples
  • variants
  • records
  • pattern matching
  • structual comparison
  • primitive types
    • string
    • list
    • bytes
    • array
  • formatted output with Printf
  • modules
    • definition
    • aliases
    • open
    • dune-like bundler
  • self-hosting!
  • exceptions
  • type checker & type inference

mlml is self-hosted. i.e. mlml can compile itself.

Terminal window
./dev/exec.sh ./dev/self_host.sh

To obtain build artifacts, pass a path to local directory as below. You will see compiled binaries under ./self_host.

Terminal window
mkdir self_host
./dev/exec.sh ./dev/self_host.sh ./self_host
  • external definitions are only available for functions
  • all modules and paths are statically-resolved
  • all custom oeprators are left-associative
  • function keyword does not make an expression

If you have docker installed in your system, simply run

Terminal window
./dev/start.sh

to start the development.

You can run tests manually by running the following command:

Terminal window
./dev/exec.sh dune runtest

The code and algorithm in parser and lexer is strongly inspired by ushitora-anqou/aqaml