Skip to content

binoyjayan/monkey-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

92 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

monkey-rs

Rust

🦀 🦀 🦀 A Rust port of the Interpreter for the monkey programming language by Thorsten Ball 🦀 🦀 🦀

Features

  • REPL: A Read-Eval-Print-Loop (REPL)
  • Tokenizer and parser that parses code into an AST
  • An expression evaluator to evaluate the AST
  • A bytecode compiler that compiles the same AST into bytecode
  • A virtual machine to execute the bytecode
  • A byte code disassembler for debugging

Language features

  • Numeric, string, boolean, arrays, and maps
  • Global and local bindings
  • User defined and higher order functions
  • Closures and access to surrounding variables
  • Builtin functions to manipulate objects and strings

Build and test

Run tests

It has decent test coverage for all the modules, including tests for the scanner, parser, evaluator, opcode definitions, compiler, virtual machine, and built-ins.

cargo test

Run examples

cargo run --release examples/formatted-output.mky
cargo run --release examples/recursive-fibonacci.mky

Run the REPL

Run an interactive REPL loop to execute program statements on the terminal one at a time and see the output immediately.

cargo run --release
>> 1 + 2
3

Create a release build

Build release with default options

cargo build --release

This will create a release binary in the './target/release' directory.

Bytecode compiler vs expression evaluator

By default, the AST generated by the parser is compiled into a bytecode that is later executed by a bytecode virtual machine. To evaluate the AST directly without compilation, set the environment 'AST_EVAL' to true. Note that 'AST_EVAL' is a runtime option and not a compile time one.

AST_EVAL=true cargo run --release

Additional build options

debug_print_code

The option helps building source with source code disassembly enabled.

Build with the option:

cargo build --release --features  'debug_print_code'

Run the REPL:

cargo run --release --features  'debug_print_code'
...

>> 1 + 2
--------- Instructions [len: 8   ] -------------------
0000 OpConstant 0
0003 OpConstant 1
0006 OpAdd
0007 OpPop
------------------------------------------------------
----------- Constants [len: 2   ] --------------------
[0] 1
[1] 2
------------------------------------------------------
3

Run an example:

cargo run --release --features  'debug_print_code' examples/recursive-fibonacci.mky

debug_trace_execution

This option helps build the release for tracing program execution and examining the state of the stack.

cargo build --release --features  'debug_trace_execution'

Examples

  • Examples may be found in the examples directory
  • More examples may be found in VM Tests