An interpreter for the Monkey programming language, built by following Thorsten Ball's excellent book Writing an Interpreter in Go.
Monkey is a programming language designed to learn about interpreter construction. It features a C-like syntax and supports modern programming language features in a minimal, easy-to-understand package.
- C-like syntax - Familiar and readable syntax structure
- Variable bindings - Bind values to names using
letstatements - Integers and booleans - Built-in primitive data types
- Arithmetic expressions - Standard mathematical operations
- Built-in functions - Utility functions available out of the box
- First-class and higher-order functions - Functions as values that can be passed as arguments
- Closures - Functions that capture their surrounding environment
Variables are declared using let statements:
let age = 1;
let name = "Monkey";
let result = 10 * (20 / 2);Functions are first-class values and can be bound to names:
let add = fn(a, b) { return a + b; };Monkey supports implicit returns, so you can omit the return keyword:
let add = fn(a, b) { a + b; };Calling functions is straightforward:
add(1, 2); // => 3Functions can take other functions as arguments:
let twice = fn(f, x) {
return f(f(x));
};
let addTwo = fn(x) {
return x + 2;
};
twice(addTwo, 2); // => 6- Go 1.16 or higher
Clone the repository:
git clone https://github.com/gyaneshwar01/monkey.git
cd monkeyStart the interactive REPL:
go run main.go.
├── ast/ # Abstract Syntax Tree definitions
├── lexer/ # Lexical analysis
├── token/ # Tokens used
├── parser/ # Syntax analysis
├── evaluator/ # Tree-walking interpreter
├── object/ # Object system and environment
├── repl/ # Read-Eval-Print Loop
└── main.go # Entry point
This interpreter was built by following Writing an Interpreter in Go by Thorsten Ball. The book provides an excellent, hands-on introduction to interpreter design and implementation.