Skip to content

thomas-forbes/monkey

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

104 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Monkey

A programming language built following the books Writing An Interpreter In Go and it's sequel Writing A Compiler In Go. The language is a simple and dynamically typed language with a syntax similar to JavaScript. I have personally extended it with additional features such as:

Example

let fib = fn(n) {
  if (n < 2) {
    n;
  } else {
    fib(n - 1) + fib(n - 2);
  }
};

puts(fib(10)); // 55

Usage

Running:

# Run the REPL
go run main.go
# Run a Monkey source file
go run main.go path/to/source.monkey

Testing:

# All tests
go test ./...
# Specific test
go test ./runner -run TestSpec/errors

Syntax

Variables

let x = 5; // immutable by default variable
let mut y = 10; // mutable variable
y = y + x;

Operations

>> 5 + 10
15
>> 5 - 10
-5
>> 5 * 10
50
>> 10 / 5
2
>> "Hello, " + "world!"
"Hello, world!"

Standard Library

built in functions:

>> puts("Hello, world!"); // prints to the console
Hello, world!
null
>> len("Hello");
5
>> append([1, 2], 3); 
[1, 2, 3] // new array

Functions

Functions are first class citizens in Monkey.

let add = fn(a, b) {
  a + b;
};

add(5, 10); // 15

// closure support
let getMultiplier = fn(x) {
  fn(y) {
    x * y;
  };
}
let doubler = getMultiplier(2);
doubler(5); // 10

Conditionals

if (x > 10) {
  puts("x is greater than 10");
} else if (x == 10) {
  puts("x is equal to 10");
} else {
  puts("x is less than 10");
}

Loops

for index, item in ["first", "second", "third"] {
  puts(index, item);
}

for key, value in {"a": 1, "b": 2} {
  puts(key + ": " + value);
}

for i in 0..5 {
  puts(i); // prints 0, 1, 2, 3, 4
}

for condition {
  puts("This will run until the condition is false");
}

Errors

About

an interpreter and compiler for the monkey programming language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages