Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/matklad/miniml


https://github.com/matklad/miniml

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        

# miniml

[miniml](http://andrej.com/plzoo/) implementation in [Rust](https://www.rust-lang.org/).

`cargo run` to run the repl, `cargo test` to run the tests. You can install Rust and Cargo
[here](https://www.rust-lang.org/downloads.html)

Some tests are [here](https://github.com/matklad/miniml/blob/master/src/tests.rs).

# Architecture

Miniml is a small statically typed functional language. This implementation uses
a virtual machine to execute miniml code. To get to the VM code, the following
phases are executed.

## Parsing/Lexing

There is no separate lexing phase.

There are two alternative parsers. One is a bottom up LR parser generated by
[LALRPOP](https://github.com/nikomatsakis/lalrpop). You can read the
[grammar](syntax/src/parser.lalrpop). Another one is a hand written top down LL
recursive descent/Pratt parser. It lives in [syntax_ll](syntax_ll/src/parser.rs)
crate. You can read about Pratt parsers
[here](http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/).
I bet it is more interesting and useful than your usual formal grammars class :)

The AST lives in [ast](ast/src/lib.rs) crate.

## Type checking

Miniml has a very simple type system (`int`, `bool` and arrow types) and no
polymorphism. All functions have annotated parameter and return types. There is
nothing fancy in [typechecking](src/typecheck.rs). It is executed on the AST
level.

## Deshugaring

Miniml VM has a native support for recursive functions. Local variable
declarations (`let`) and mutual recursion (`let rec`) are lowered to simple
recursive functions. This happens in the [ir](src/ir.rs) module. Alas, type
information is lost on the way: the IR is untyped. Also, string identifiers are
converted to numeric ones in the IR. Some new identifiers are synthesised while
desugaring.

## Compiling

IR is converted to VM instructions in the [compile](src/compile.rs) module.
Compilation is pretty simple because most of the interesting work happens in the
previous phase and also because the VM is pretty high level.

## VM

The machine lives in the [machine](src/machine/mod.rs) module. It is a stack
based [SECDish](https://en.wikipedia.org/wiki/SECD_machine) VM. It even has a
garbage collector (the `collect` function)!