Skip to content

dwayne/elm-monkey-interpreter

Folders and files

NameName
Last commit message
Last commit date
Jun 2, 2022
Jun 3, 2022
Jun 6, 2022
May 27, 2022
Jun 3, 2022
Apr 29, 2022
Jun 6, 2022
Jun 6, 2022
Apr 29, 2022
Jun 2, 2022
Jun 3, 2022
Jun 3, 2022

Repository files navigation

Elm Monkey Interpreter (Playground)

A Monkey interpreter written in Elm.

Monkey is a programming language designed by Thorsten Ball that is fully described in his interpreter book.

Syntax

The syntax of Monkey is scattered throughout the pages of the book. However, I extracted a context-free grammar that you can use to learn the syntax.

let filter = fn (pred, arr) {
  let iter = fn (arr, accumulated) {
    if (len(arr) == 0) {
      accumulated
    } else {
      if (pred(first(arr))) {
        iter(rest(arr), push(accumulated, first(arr)))
      } else {
        iter(rest(arr), accumulated)
      }
    }
  };
  iter(arr, [])
};

let mod = fn (a, b) {
  a - a / b * b
};

let isEven = fn (n) {
  mod(n, 2) == 0
};

filter(isEven, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

You can also use the parser tests as a guide.

Semantics

The semantics is defined by the interpreter. You can get a sense for it by reading through the extensive suite of interpreter tests.

Languages