Monkey Lang is a programming language built to help understand how an programming languages work under the hood. The compiler currently supports functions, higher-order functions, closures, strings, integers, arrays, hashes, and integer arithmetic.
This was written with the help of Writing an Intrepreter in Go by Thorsten Ball.
Currently the project only supports input from the command line repl.
-
Clone the repository:
git clone https://github.com/lukeomalley/go-intrepreter.git
-
Change into the root directory of the project:
cd go-intrepreter
-
Start the interactive REPL:
go run main.go
Declare a Variable:
let x = 5;
Define and Apply a Function:
let add = fn(x, y) {
return x + y;
}
add(5, 5); // => 10
Closures:
let newAdder = fn(x) {
fn(y) { x + y };
};
let addTwo = newAdder(2);
addTwo(2); // => 4
Nth Fibonacci Number:
let fib = fn(n) {
if (n < 1) {
return n;
}
return fib(n - 1) + fib(n - 2);
};
Array Map Function
let map = fn(arr, f) {
let iter = fn(arr, accumulated) {
if (len(arr) == 0) {
accumulated
} else {
iter(rest(arr), push(accumulated, f(first(arr))));
}
};
return iter(arr, []);
};
Array Reduce Function
let reduce = fn(arr, initial, f) {
let iter = fn(arr, result) {
if (len(arr) == 0) {
result
} else {
iter(rest(arr), f(result, first(arr)));
}
};
iter(arr, initial);
};