Giter VIP home page Giter VIP logo

monkey's Introduction

Monkey

Build

Yet another Monkey interpreter & compiler implementation in Rust, based on Thorsten Ball's Writing An Interpreter In Go and Writing A Compiler In Go books.

Features

  • AST
  • Tokenizer
  • Parser
  • Lexer
  • Evaluator
  • Compiler
  • SymbolTable
  • VirtualMachine
  • Builtin Functions
  • Functions & Closures
  • Read–Eval–Print Loop

Usage

1. Install rustup

2. Test

$ cargo test --all

3. Run

$ cargo run --bin monkey
Monkey Compiler v0.2.0
>>> "Hello, " + "World!"
Hello, World!
>>>

Examples

Functions

[1, 2 * 2, 3 + 3]

Function Call

let identity = fn(x) { x; }; identity(7); //7
let add = fn(a, b) { a + b }; 
let sub = fn(a, b) { a - b }; 
let applyFunc = fn(a, b, func) { 
    func(a, b) 
}; 
applyFunc(10, 2, sub); //8

If

if (x < y) { z } else { w }

Nested If

if (2 > 1) { if (3 > 1) { return 7; } return 0; }

HashMap

{"one": 0 + 1, "two": 10 - 8, "ten": 50 / 5}

Arrays

let arr = [1, 2 * 2, 3 + 3] //arr[1 + 1] => returns 6

Operator Precedence

3 + 4 * 5 == 3 * 1 + 4 * 5 //((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))

Error Handlers

if (10 > 1) { if (10 > 1) { return true + false; } return 1; } //unknown operator: BOOLEAN + BOOLEAN
let newAdder = fn(x) { fn(y) { x + y } }; let addTwo = newAdder(2); x //identifier not found: x

Complete Example

Builtin

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, []);
};
let a = [1, 2, 3, 4];
let double = fn(x) { x * 2 };
map(a, double);

HashMap

let people = [{"name": "Alice", "age": 24}, {"name": "Anna", "age": 28}];
let getAge = fn(person) { person["name"]; };
return getAge(people[0]) + getAge(people[1]);

Benchmark

$ cargo run --release --bin benchmark -- --vm
$ cargo run --release --bin benchmark -- --eval

TODO

  • Impl Compiler
  • Impl VM
  • Add Pipeline with Stages: fmt, check, clippy, test, build
  • Optimize overall performance
  • Fix clippy warnings

License

MIT License (LICENSE).

monkey's People

Contributors

dentrax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

kdeconinck

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.