MonkeyLang Interpreter ๐ต โ
Repository: GitHub
Stack: Golang Pratt Parser
Description โ
๐ต The MonkeyLang Interpreter is a Go implementation of the Monkey programming language, following the book Writing An Interpreter In Go. Monkey is a simple and fun language designed for educational purposes.
โจ Interpreter Features โ
- Lexer and Parser for Monkey language
- ๐ณ Abstract Syntax Tree (AST) representation
- Interpreter for evaluating Monkey code
- ๐ Interactive REPL (Read-Eval-Print Loop)
๐ Monkey-Lang Features โ
๐ Monkey supports various features:
- Variable bindings: Use the
let
keyword to bind values to names, likelet x = 5;
. - Integers and booleans: Numbers and boolean literals are supported, e.g.,
10
ortrue
. - Arithmetic expressions: Perform arithmetic operations using operators like
+
,-
,*
,/
, e.g.,2 + 3 * 4
. - Built-in functions: Use predefined functions like
len("hello")
andputs("Hello, world!")
. - First-class and higher-order functions: Functions can be used as values, and you can define functions using the
fn
keyword, likelet add = fn(x, y) { x + y; };
. - Closures: Create functions that capture their environment, e.g.,
let newAdder = fn(x) { fn(y) { x + y }; };
. - String data structure: Create strings with double quotes and concatenate them with
+
. - Array data structure: Use square brackets for array literals, e.g.,
[1, 2, 3]
. - Hash data structure: Create hash literals with curly braces, and access values with the index operator.
In Monkey, the syntax is designed to be simple and easy to read. Here are some examples of Monkey's language features:
Variable Bindings โ
let age = 1;
let name = "Monkey";
let result = 10 * (20 / 2);
Integers and Booleans โ
let isMonkeyAwesome = true;
let numBananas = 42;
Arithmetic Expressions โ
let total = 2 + 3 * 4;
let divisionResult = 10 / 2;
Built-in Functions โ
let length = len("Hello, Monkey!"); // length = 13
puts("Hello, world!"); // Prints "Hello, world!"
Functions โ
You can create functions using the fn
keyword:
let add = fn(x, y) { return x + y; };
Implicit return values are also possible:
let subtract = fn(a, b) { a - b; };
Calling a function is straightforward:
add(1, 2); // Returns 3
Complex Functions โ
Here's a more complex function, a Fibonacci calculator:
let fibonacci = fn(x) {
if (x == 0) {
0
} else if (x == 1) {
1
} else {
fibonacci(x - 1) + fibonacci(x - 2);
}
};
Arrays โ
You can work with arrays like this:
let myArray = [1, 2, 3, 4, 5];
myArray[0]; // Accessing elements in an array, returns 1
Hashes โ
Hashes store values associated with keys:
let person = {"name": "Alice", "age": 25};
person["name"]; // Accessing values in a hash, returns "Alice"
Higher-Order Functions โ
Monkey supports higher-order functions that take other functions as arguments:
let twice = fn(f, x) {
return f(f(x));
};
let addTwo = fn(x) {
return x + 2;
};
twice(addTwo, 2); // Returns 6
In Monkey, functions are first-class citizens, just like integers or strings, and you can use them as such.
๐ License โ
This project is licensed under the MIT License
๐ Acknowledgments โ
- Thorsten Ball for the book "Writing an Interpreter in Go."
Happy coding! ๐๐ต