https://github.com/vincentlabelle/monkey
An interpreter for the Monkey programming language written in Go.
https://github.com/vincentlabelle/monkey
golang monkey-programming-language
Last synced: 10 months ago
JSON representation
An interpreter for the Monkey programming language written in Go.
- Host: GitHub
- URL: https://github.com/vincentlabelle/monkey
- Owner: vincentlabelle
- Created: 2024-10-28T00:32:20.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-15T01:38:44.000Z (about 1 year ago)
- Last Synced: 2025-02-13T12:19:37.889Z (12 months ago)
- Topics: golang, monkey-programming-language
- Language: Go
- Homepage:
- Size: 76.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Monkey
An interpreter and a compiler for the Monkey programming language written in Go
as described in [Ball, T. (2016). _Writing An Interpreter In Go._](https://interpreterbook.com/)
and [Ball, T. (2018). _Writing A Compiler In Go._](https://compilerbook.com/).
## Features
The Monkey programming language has the following features:
- Variable bindings
- Integers, booleans, strings, arrays, and hash maps
- Arithmetic operations
- First-class and higher-order functions
- Built-in functions
- Closures
## Example
Below is an excerpt of the language.
```text
// Integers & Booleans
let ispositive = fn(value) {
return value > -1;
};
let crement = fn(value, side) {
if (ispositive(side)) {
return value + 1;
}
return value - 1;
};
puts(crement(0, 1)); // 1
// Strings
let greeting = fn(first, last) {
let begin = "Hello, ";
let end = "!";
if (len(last) > 0) {
return begin + first + " " + last + end;
}
return begin + first + end;
};
puts(greeting("Monkey", "")); // Hello, Monkey!
// Arrays
let accumulate = fn(array, stop) {
if (len(array) == 0) {
return array;
}
if (last(array) == stop) {
return array;
}
let new = push(rest(array), last(array) + 1);
accumulate(new, stop);
};
puts(accumulate([1, 2, 3], 5)); // [3, 4, 5]
// Hash maps
let h = {"foo": 1, true: 2, 3: 3};
puts(h["foo"]); // 1
```
## Installation
Execute the below commands in your shell to install `monkey`.
```shell
git clone https://github.com/vincentlabelle/monkey.git
cd monkey
go install
```
## Usage
The REPL can be run by executing `monkey` in your shell (if `~/go/bin` is in
your `PATH`), and you can exit the REPL by typing `exit()`.