A complete implementation of the Monkey programming language from the book "Writing An Interpreter In Go" by Thorsten Ball, with additional features and improvements.
Monkey is a programming language designed to learn about interpreter and compiler implementation. It features:
- C-like syntax
- Variable bindings
- Integer and boolean data types
- Arithmetic expressions
- Built-in functions
- First-class and higher-order functions
- Closures
- String data structure
- Array data structure
- Hash data structure
- Go 1.16 or higher
# Clone the repository
git clone https://github.com/yourusername/monkey-lang.git
# Navigate to the project directory
cd monkey-lang
# Build the project
go build -o monkey cmd/monkey/main.go
# Run the REPL
./monkey
$ ./monkey
This is the Monkey programming language!
Feel free to type in commands
>> let x = 5;
>> x + 10;
15
>>
$ ./monkey run examples/fibonacci.monkey
let age = 1;
let name = "Monkey";
let result = 10 * (20 / 2);
// Function definition
let add = fn(a, b) { return a + b; };
// Higher-order functions
let twice = fn(f, x) {
return f(f(x));
};
// Closures
let newAdder = fn(x) {
return fn(y) { return x + y };
};
let myArray = [1, 2, 3, 4, 5];
let first = myArray[0];
let myHash = {"name": "Monkey", "age": 1};
let name = myHash["name"];
monkey-lang/
├── ast/ # Abstract Syntax Tree definitions
├── evaluator/ # Expression evaluation logic
├── lexer/ # Lexical analysis
├── object/ # Object system
├── parser/ # Parsing logic
├── repl/ # Read-Eval-Print Loop
├── token/ # Token definitions
├── cmd/
│ └── monkey/ # Main executable
├── examples/ # Example Monkey programs
└── tests/ # Test suite
# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run specific package tests
go test ./lexer
go test ./parser
go test ./evaluator
Beyond the book's implementation, this version includes:
-
Standard Library
- Array methods (map, reduce, filter)
- String manipulation functions
- Math operations
-
Additional Data Types
- Float numbers
- Null type
- Regular expressions
-
Error Handling
- Try-catch mechanism
- Stack traces
- Better error messages
let fibonacci = fn(x) {
if (x < 2) { return x; }
return fibonacci(x - 1) + fibonacci(x - 2);
};
fibonacci(10);
let map = fn(arr, f) {
let iter = fn(arr, accumulated) {
if (len(arr) == 0) {
return accumulated;
}
return iter(rest(arr), push(accumulated, f(first(arr))));
};
return iter(arr, []);
};
let double = fn(x) { return x * 2 };
map([1, 2, 3, 4], double);
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
- Writing An Interpreter In Go - The original book
- Writing A Compiler In Go - The sequel
- Monkey Language Specification
- Contributing Guidelines
This project is licensed under the MIT License - see the LICENSE file for details.
- Thorsten Ball for writing the excellent books
- The Go team for creating a great language
- All contributors to this project
Made with ❤️ by Akshay