Skip to content

martskins/monkers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MONKERS

A monkey language interpreter implementation, from the book Writing an interpreter in Go

An overview of what you'll find

Integers: 0; 42; 947;

Booleans: true | false

Strings "someString";

Functions

let add = fn(x, y) { return x + y; };
let three = add(1, 2);

Arrays:

let arr = [1, true, fn (x) { return x; }];
let one = arr[0];

Hashes:

let add = fn (x, y) = { return x + y; };
let some_hash = {"foo" + "bar": 2, true: add(40, 2)};
let two = some_hash["foobar"];
let meaning_of_life = some_hash[true];

Builtin functions

  • len(X): Returns the length of X, can be applied on string, array or hash.
  • first(X): Returns the first element of an array X, or null if it has no elements.
  • last(X): Returns the last element of an array of X, or null if it has no elements.
  • head(X): Returns a slice of an array X containing all but the last element, or an empty array if X had no elements.
  • tail(X): Returns a slice of an array X containing all but the first element, or an empty array if X had no elements.
  • push(X): Adds an element in the last position of an array X.
  • push(...X): Prints the value of any amount of arguments, each on a different line.

Infix Operators

  • *, /, +, -: Regular math operators that can be used on integers.

  • +: Other than it's previous use, it can also be used to concatenate string.

    let foobar = "foo" + "bar";
    
  • []: Index operator can be applied to arrays or hashes to get the element at the given index.

    let some_hash = {"foo": "bar"};
    let bar = some_hash["foo"];
    
    let numbers = [3, 2, 1];
    let one = numbers[2];
    

Prefix Operators

  • !: Bang operator negates the RHS operand.
    let not_true = !true;
    let unchanged = !!true;
    

The REPL

The project includes a REPL, which you can use to evaluate monkey code. You can launch said REPL by running cargo run.

> let some_hash = {"foo" + "bar": 2 * 2};
[foobar: 4]

> some_hash["foobar"]
4

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages