Rust implementation of the programming language Monkey that lives in books
- C-like Syntax
- Variable bindings
- Integer and Boolean
- Built-in data structures(String, Array, Hash)
- Arithmetic expressions
- If expressions
- Built-in functions
- First-class and higher-order functions
- Support closures
Integer
represents 2^-63(-9223372036854775808) to 2^63(9223372036854775807).
1;
-1;
101;
9223372036854775807;
Boolean
represents only two possible value, true or false.
true;
false;
let flag = !true;
String
represents the array of characters.Double-Quoted string is only available.
"Hello World"
"foo" + "bar" + "baz"
Array
represents a collection of elements.
let array = [1, 2, 3, 4, 5]
array[0] // 1
array[1+1] // 3
let array = [1, "foo", true, fn(a){a}]
array[4](2)
Hash
represents the data structure that holds a pair of key and value.
let hash = {"foo":1, "bar": 2, "baz": "hello", 100: "world"};
hash["foo"]
hash[99 + 1]
fn add(x, y){
return x + y;
}
let fibonacci = fn(x) {
if (x == 0) {
0;
} else {
if (x == 1) {
1;
} else {
fibonacci(x - 1) + fibonacci(x - 2);
}
}
};
fibonacci(5);
let twice = fn(f, x) { return f(f(x));};
let addTwo = fn(x) { return x + 2;};
twice(addTwo, 2);
puts("string");
puts(2);
len([0,1,2,3,4])
len("Hello World")
first([0,1,2,3]) // => 0
first(["string", 0, true]) // "string"
last([0,1,2,3,4]) // => 4
rest([0,1,2]) // => [1,2]
push([0,1,2,3,4], 5)