Skip to content

dyscott/monkey-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unit Tests

monkey-rs

Rust Interpreter and Compiler for Monkey Programming Language

Based on the books Writing An Interpreter In Go and Writing A Compiler In Go by Thorsten Ball

Built as a learning exercise to better understand the Rust and Go programming languages and how interpreters and compilers work

Usage

Requires Rust to be installed

REPL

$ cargo run --release
>> let add = fn(x, y) { x + y };
>> add(1, 2);
3

File Loading

$ cargo run --release -- --file=examples/fibonacci.monkey
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...]

Use the -i or --interpreter flag to run in interpreter mode instead of compiler mode for REPL and File Loading

Benchmarking

Compare the performance of the interpreter and compiler modes

$ cargo run --release -- --benchmark --file=examples/fibonacci_benchmark.monkey
Parsing took: 18.791µs
Evaluation (interpreter) took: 42.488771428s
Compilation took: 7.754µs
Execution (VM) took: 5.539178683s

The compiler is almost 8x faster than the interpreter for this example!

Features

monkey-rs aims to be a fully featured interpreter and compiler for the Monkey Programming Language with additional features inspired by other languages such as Python.

Features progress is tracked below:

  • Language features from Monkey Programming Language
    • C-like syntax
    • Primitive Types (integers, booleans, strings, arrays, hash maps)
    • Arithmetic Expressions
    • Let and return statements
    • Conditionals
    • Functions (first-class, higher-order, closures)
    • Built-in functions (len, puts, push, etc.)
  • Additional language features
    • Better string parsing - character escaping and error handling
    • String indexing (ex: "hello"[4] -> 4)
    • Python-like string and array slicing (ex: [1, 2, 3, 4][1:-1] -> [2, 3])
  • REPL and File Loading
  • Interpreter
    • Lexer / Tokenizer
    • Pratt Parser
    • Abstract Syntax Tree
    • Evaluator
  • Bytecode Compiler
    • Conversion from AST to Bytecode
    • File Output
  • Virtual Machine
    • Stack-based VM
    • Bytecode Interpreter
  • Unit Tests

The only major missing feature is file output for the bytecode compiler - this is currently a work in progress. Perhaps new language features and performance improvements could be added in the future as well.

About

Rust Interpreter and Compiler for Monkey Programming Language

Resources

Stars

2 stars

Watchers

1 watching

Forks

Contributors

Languages