Skip to content

TESTMECS/monkeyinterpreter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Interpreter and Repl for Monkey Based on Interpreter in Go by Thorsten Ball

Sample Code

// `newAdder` returns a closure that makes use of the free variables `a` and `b`:
let newAdder = fn(a, b) {
    fn(c) { a + b + c };
};
// This constructs a new `adder` function:
let adder = newAdder(1, 2);
adder(8); // => 11
// HashMaps + Builtin Functions
let h = {"name": "Alice", "age": 30};
keys(h); // => ["name", "age"]
values(h); // => ["Alice", 30]
// Array + Builtin Functions
let arr = [1, 2, 3, 4, 5];
first(arr); // => 1
last(arr); // => 5
rest(arr); // => [2, 3, 4, 5]
push(arr, 6); // => [1, 2, 3, 4, 5, 6]

Built-in Functions

Mathematical Operations

sqrt(16); // => 4.0
pow(2, 3); // => 8.0
sin(0); // => 0.0
cos(0); // => 1.0
log(1); // => 0.0
exp(1); // => 2.718281828459045

String Manipulation with Regex

match("\\d+", "abc123def"); // => true
find("\\d+", "abc123def"); // => "123"
findAll("\\d+", "a1b2c3"); // => ["1", "2", "3"]
replace("\\d+", "abc123def", "XXX"); // => "abcXXXdef"

Array Operations

let arr = [3, 1, 4, 1, 5];
sort(arr); // => [1, 1, 3, 4, 5]
unique(arr); // => [3, 1, 4, 5]
slice(arr, 1, 3); // => [1, 4]
indexOf(arr, 4); // => 2
sum(arr); // => 14
min(arr); // => 1
max(arr); // => 5

Random Numbers

rand(); // => 0.6046602879796196 (random float 0-1)
randInt(10); // => 7 (random int 0-9)

Hashing

hash("hello"); // => 2059638877649177446 (64-bit FNV hash)

Type Conversion and Utilities

str(42); // => "42"
int("123"); // => 123
type(42); // => "INTEGER"
abs(-5); // => 5
range(5); // => [0, 1, 2, 3, 4]

About

monkey-lang compiler & repl

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages