This is an interpreter for a 'Monkey' programming language written in Go
I've created this based on the Thorsten Ball's book on writing interpreters.
in order to use the interpreter, locate and move into the root folder and run:
go run .\main\main.go
let age = 1
let result = 10 * (20 / 2) + age
let found = true
Besides integers and booleans, in Monkey, you can also bind functions to names:
let add = fn(a, b) { return a + b }
Implicit return values are also possible which means we can leave out the return:
let add = fn(a, b) { a + b }
Function calling works as expected:
add(1, 2);
More complex functions can also be written:
let fibonacci = fn(x) {
if (x == 0) {
0
} else {
if (x == 1) {
1
} else {
fibonacci(x - 1) + fibonacci(x - 2);
}
}
};
let twice = fn(f, x) {
return f(f(x));
};
let addTwo = fn(x) {
return x + 2;
};
twice(addTwo, 2); // => 6
Monkey has first class functions. That means functions in Monkey are just values, like integers or booleans.
let newAdder = fn(x) {
fn(y) { x + y };
};
let addTwo = newAdder(3);
addTwo(2); // => 5