Monkey language interpreter made in Rust

Monkey has a C-like syntax, supports variable bindings, prefix and infix operators, has first-class and higher-order functions, can handle closures with ease and has integers, booleans, arrays and hashes built-in.

There is a book about learning how to make an interpreter: Writing An Interpreter In Go. This is where the Monkey programming language come from.

Software model

The most important part of this project is that both the lexer and parser are made with the Nom library. Then everything is built up into an AST for execution. It has several unit tests to ensure it doesn't break when I update the libraries or add new features.

Examples of code you can run

let map = fn(f, arr) {
  if (len(arr) == 0) {
  } else {
    let h = head(arr);
    cons(f(h), map(f, tail(arr)));

let reduce = fn(f, init, arr) {
  if (len(arr) == 0) {
  } else {
    let newInit = f(init, head(arr));
    reduce(f, newInit, tail(arr));

let double = fn(x) {
  2 * x

let add = fn(x, y) {
  x + y

let mapped = map(double, [1, 2, 3, 4]);

let sum = fn(arr) {
  reduce(add, 0, arr);

let summed = sum([1, 2, 3, 4, 5]);

This is totally valid code and will run like so:

cargo run --release --bin monkey_exe -- --src examples/
    Finished release [optimized] target(s) in 0.02s
     Running `target/release/monkey_exe --src examples/`
[2, 4, 6, 8]

How to run

Build and test

$ cargo build
$ cargo test

Running the REPL

$ cargo run --release --bin monkey_repl

Running the Interpreter

$ cargo run --release --bin monkey_exe -- --src examples/

Source code

You can find it on my Github page related to the project: here.

Made in France in 2022 with NextJS, Typescript, Strapi, Tailwind etc.