A toy language parser, lexer and interpreter written in Golang

Monkey - A toy programming language

Monkey is a toy programming language used to learn how to write a lexer, parser and interpreter.

The language is introduced in the book Writing an Interpreter in Go by Thorsten Ball (https://interpreterbook.com/)

In this repo is my implementation of the Monkey language using Go primitives.

The Language

// Bind values to names with let-statements
let version = 1;
let name = "Monkey programming language";
let myArray = [1, 2, 3, 4, 5];
let coolBooleanLiteral = true;

// Use expressions to produce values
let awesomeValue = (10 / 2) * 5 + 30;
let arrayWithValues = [1 + 1, 2 * 2, 3];

Monkey also supports function literals and we can use them to bind a function to a name:

// Define a `fibonacci` function
let fibonacci = fn(x) {
    if (x == 0) {
        0                // Monkey supports implicit returning of values
    } else {
        if (x == 1) {
            return 1;      // ... and explicit return statements
        } else {
            fibonacci(x - 1) + fibonacci(x - 2); // Recursion! Yay!
        }
    }
};

The data types we're going to support in this book are booleans, strings, hashes, integers and arrays. We can combine them!

5 // We can also access hash elements with index expressions: let getName = fn(person) { person["name"]; }; // And here we access array elements and call a function with the element as // argument: getName(people[0]); // => "Anna" getName(people[1]); // => "Bob" ">
// Here is an array containing two hashes, that use strings as keys and integers
// and strings as values
let people = [{"name": "Anna", "age": 24}, {"name": "Bob", "age": 99}];

// Getting elements out of the data types is also supported.
// Here is how we can access array elements by using index expressions:
fibonacci(myArray[4]);
// => 5

// We can also access hash elements with index expressions:
let getName = fn(person) { person["name"]; };

// And here we access array elements and call a function with the element as
// argument:
getName(people[0]); // => "Anna"
getName(people[1]); // => "Bob"

That's not all though. Monkey has a few tricks up its sleeve. In Monkey functions are first-class citizens, they are treated like any other value. Thus we can use higher-order functions and pass functions around as values:

// Define the higher-order function `map`, that calls the given function `f`
// on each element in `arr` and returns an array of the produced values.
let map = fn(arr, f) {
    let iter = fn(arr, accumulated) {
        if (len(arr) == 0) {
            accumulated
        } else {
            iter(rest(arr), push(accumulated, f(first(arr))));
        }
    };
    iter(arr, []);
};

// Now let's take the `people` array and the `getName` function from above and
// use them with `map`.
map(people, getName); // => ["Anna", "Bob"]
Owner
Adam Petrovic
Software Engineer #golang #python #observability
Adam Petrovic
Similar Resources

Various Dungeons and Dragons Tools. Written in go as an aid to learning the language.

dnd_tools Various Dungeons and Dragons Tools. Written in go as an aid to learning the language. Some tools are generic, while others will target eithe

Jan 28, 2022

A Go parser for the /etc/passwd file

godoc github.com/willdonnelly/passwd PACKAGE package passwd import "github.com/willdonnelly/passwd" FUNCTIONS func Parse() (map[string]Entr

Oct 20, 2022

rsync wrapper (or output parser) that pushes metrics to prometheus

rsync-prom An rsync wrapper (or output parser) that pushes metrics to prometheus. This allows you to then build dashboards and alerting for your rsync

Dec 11, 2022

Cooklang parser

Cooklang parser

Aug 1, 2022

The new home of the CUE language! Validate and define text-based and dynamic configuration

The CUE Data Constraint Language Configure, Unify, Execute CUE is an open source data constraint language which aims to simplify tasks involving defin

Dec 31, 2022

Some utilities for Persian language in Go (Golang)

persian Some utilities for Persian language in Go (Golang). Installation go get github.com/mavihq/persian API .ToPersianDigits Converts all English d

Oct 22, 2022

Go-linq - A powerful language integrated query (LINQ) library for Golang

go-linq A powerful language integrated query (LINQ) library for Go. Written in v

Jan 7, 2023

Highly extensible, customizable application launcher and window switcher written in less than 300 lines of Golang and fyne

Highly extensible, customizable application launcher and window switcher written in less than 300 lines of Golang and fyne

golauncher A go application launcher A simple, highly extensible, customizable application launcher and window switcher written in less than 300 lines

Aug 21, 2022

CodePlayground is a playground tool for go and rust language.

CodePlayground CodePlayground is a playground tool for go and rust language. Installation Use homebrews to install code-playground. brew tap trendyol/

Mar 5, 2022
Related tags
Toy program for benchmarking safe and unsafe ways of saving a file

save-a-file benchmarks the many strategies an editor could use to save a file. Example output on a SSD: ext4: $ ./save-a-file ~/tmp/foo 29.195µs per s

Jan 4, 2023
Implement a toy in-memory store information service for a delivery company

Implement a toy in-memory store information service for a delivery company

Nov 22, 2021
A toy repo used to test the functionality of "go mod why".

Mod Why Test Discussion From a module perspective: The main module (github.com/ejweber/mod-why-test) has a single direct dependency (github.com/ejwebe

Dec 1, 2021
A toy project like cowsay or ponysay
A toy project like cowsay or ponysay

xkcdsay is a simple application just for fun. Once again, Just for fun. I like c

Dec 23, 2022
A Golang SSA Interpreter

A Golang SSA Interpreter

Nov 9, 2022
bf.go - A dead simple brainfuck interpreter Slow and simple

bf.go - A dead simple brainfuck interpreter Slow and simple. Can execute pretty much all tested Brainfuck scripts. Installation If you have Go install

Oct 15, 2021
Conventional Commits parser written in Go

Conventional Commit Parser This is a parser for Conventional Commits go get -u github.com/release-lab/conventional-commit-parser package main import

Feb 4, 2022
A modern programming language written in Golang.

MangoScript A modern programming language written in Golang. Here is what I want MangoScript to look like: struct Rectangle { width: number he

Nov 12, 2021
XSD (XML Schema Definition) parser and Go/C/Java/Rust/TypeScript code generator

xgen Introduction xgen is a library written in pure Go providing a set of functions that allow you to parse XSD (XML schema definition) files. This li

Jan 1, 2023
LDMud OBJ_DUMP Parser and Database Tool

DUMPDB: A LDMUD OBJ_DUMP -> SQLite3 DB Tool About The LDMUD MUD driver supports dumping a formatted text file with information about every object load

Dec 5, 2021