Docs
About
Monkey is an educational programming language from the book Writing an Interpreter in Go by Thorston Ball
This implementation of Monkey is written in Rust instead of Go, and it also has some extra features beyond what's covered in the book. It has been compiled to WASM so that you can try it out here in the browser.
Source code: https://gitlab.com/findley/monkey-lang
Primitive Types
- Integers
- You can do all of the expected arithematic with integer numbers:
(1 + 2) * 3 / 4
- Strings
- You can concat them with the + operator:
"Hello" + " " + "World!"
- Booleans
- Just
true
orfalse
- Arrays
Lists of values
[1, 2, 3]
They can hold mixed types
[true, "false", []]
Index into them with the [] operator, they're 0 based
[1, 2, 3][0]
Use the builtin push to add elements to an array
push([1, 2], 3)
- Hashes
- These are just objects (or dictionaries). You can use integers, strings, and booleans as keys. Use the [] to get the value at a key.
{"name": "Marco", 1: true}["name"]
Declaring Variables
Use the let keyword to bind new variables
let x = 5;
Conditionals
If statements are expressions in Monkey, so you can assign them to variables.
let result = if (true) {
10
} else {
0
}
Functions
In Monkey, functions are first-class citizens. That is, they are just expressions which can be bound to variables like any other type.
let double = fn(x) { x * 2 };
double(2);
Functions capture the scope where they're defined as a closure.
let x = 5;
let foo = fn() {
puts(x);
};
foo();
Builtins
- puts
- Print out the given values:
puts("Hello World!")
- push
- Append an element to the end of an array:
append(myArray, 5)
Returns a new array without modifying the original. - len
- Gives the length of an array or a string:
len("hello"); len([1,2,3])
- first
- Gives the first element of an array, or null if the array is empty.
- last
- Gives the last element of an array, or null if the array is empty.
- rest
- Takes an array, and returns a new array with all of the elements except the first one. This can be useful for recursive functions.