It is a simple interpreter written in Go, named Monkey.
It was built as a way to learn how interpreted languages are implemented.
Monkey is a dynamically typed, interpreted language with a C-like syntax. It supports variable bindings, integers, booleans, arithmetic expressions, built-in functions, first-class and higher-order functions, closures, strings, arrays, and hashes.
let age = 25;
let name = "John";
let sum = 5 + 5;
let product = 5 * 5;
let quotient = 10 / 2;
let difference = 10 - 5;
let isAdult = age > 18;
let isEqual = (10 == 10);
let isNotEqual = (10 != 9);
let add = fn(x, y) { x + y };
let result = add(5, 10);
let greeting = "Hello, " + name + "!";
let numbers = [1, 2, 3, 4, 5];
let firstNumber = numbers[0];
let person = {"name": "John", "age": 25};
let personName = person["name"];
puts("Hello, World!");
let map = fn(arr, f) {
let iter = fn(arr, accumulated) {
if (len(arr) == 0) {
accumulated
} else {
iter(rest(arr), push(accumulated, f(first(arr))));
}
};
iter(arr, []);
};
let double = fn(x) { x * 2 };
let result = map([1, 2, 3, 4], double);
puts(result); // [2, 4, 6, 8]
let factorial = fn(n) {
if (n == 0) {
1
} else {
n * factorial(n - 1);
}
};
puts(factorial(5)); // 120
let fibonacci = fn(n) {
if (n == 0) {
0
} else if (n == 1) {
1
} else {
fibonacci(n - 1) + fibonacci(n - 2);
}
};
puts(fibonacci(10)); // 55
Returns the length of a string or array.
len("hello"); // 5
len([1, 2, 3]); // 3
Returns the first element of an array.
first([1, 2, 3]); // 1
Returns the last element of an array.
last([1, 2, 3]); // 3
Returns the array without the first element.
rest([1, 2, 3]); // [2, 3]
Adds an element to the end of an array.
push([1, 2, 3], 4); // [1, 2, 3, 4]
To run it, you'll need Git and Go installed on your computer. From your command line:
# Clone this repository
$ git clone https://github.com/ekastn/monkey
# Go into the repository
$ cd monkey
# Build the project
$ go build -o bin/monkey cmd/main.go
# Run the REPL
$ ./bin/monkey