Skip to content

Latest commit

 

History

History
107 lines (72 loc) · 2.46 KB

File metadata and controls

107 lines (72 loc) · 2.46 KB

Monkey Interpreter

An interpreter for the Monkey programming language, built by following Thorsten Ball's excellent book Writing an Interpreter in Go.

About Monkey

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.

Features

  • C-like syntax - Familiar and readable syntax structure
  • Variable bindings - Bind values to names using let statements
  • 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

Language Examples

Variable Bindings

Variables are declared using let statements:

let age = 1;
let name = "Monkey";
let result = 10 * (20 / 2);

Functions

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);  // => 3

Higher-Order Functions

Functions 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

Getting Started

Prerequisites

  • Go 1.16 or higher

Installation

Clone the repository:

git clone https://github.com/gyaneshwar01/monkey.git
cd monkey

Running the REPL

Start the interactive REPL:

go run main.go

Project Structure

.
├── 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

Acknowledgments

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.