Skip to content

madzarm/interpreter-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Monkey Interpreter

This is an interpreter for a 'Monkey' programming language written in Go
I've created this based on the Thorsten Ball's book on writing interpreters.

in order to use the interpreter, locate and move into the root folder and run:

go run .\main\main.go

Features

  • C-like syntax
  • variable bindings
  • integers and booleans
  • arithmetic expressions
  • first-class and higher-order functions
  • closures
  • Value binding

    let age = 1
    let result = 10 * (20 / 2) + age
    let found = true
    

    Besides integers and booleans, in Monkey, you can also bind functions to names:

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

    Implicit return values are also possible which means we can leave out the return:

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

    Function calling works as expected:

    add(1, 2);
    

    More complex functions can also be written:

    let fibonacci = fn(x) {
        if (x == 0) {
            0
        } else {
            if (x == 1) {
                1
            } else {
                fibonacci(x - 1) + fibonacci(x - 2);
            }
        }
    };
    

    Higher order functions

    Monkey also supports higher order functions. These are functions that 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
    

    Monkey has first class functions. That means functions in Monkey are just values, like integers or booleans.

    Closures

    Monkey also supports closures. Closure is a function that references variables from outside its body allowing it to encapsulate state and behaviour within functions:
    let newAdder = fn(x) {
        fn(y) { x + y };
    };
    let addTwo = newAdder(3);
    addTwo(2); // => 5
    

    About

    No description, website, or topics provided.

    Resources

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

    No packages published

    Languages