A compiler written in Rust for the Lox programming language.
Implementation of clox (bytecode) in Robert Nystrom's Crafting Interpreters book.
I am writing it while learning Rust, so it is definitely not perfect/idiomatic.
Run by cargo run. Run with debug mode by cargo run --all-features.
Test with cargo test -- --nocapture (--nocapture means print statements will be shown).
- Op instruction is implemented with the
OpCodeenum (instead ofu8), which could be > 1 byte. A chunk has aVecofOpCode.- Different offset calculation
- Instead of reading 2 bytes,
OpCode::Constant,OpCode::GetGlobalandOpCode::DefineGlobalincludes au8as the extra byte
- Use
usizeindex instead of pointer+dereference to access element in array.- Though pointer+dereference should be faster?
- Tagged union replaced by Enum(T)
- No
Value::Objthat can save arbitary object - String Object (
Value::StringObj(u32)) is interned byHashMap<String, u32> - No printing for
Functionobject - Pointer operations are replaced by index lookup
- Following the same code structure of clox will mess up ownership in rust, so there are many tweaks about that (e.g.
compiler.enclosing, mutable and immutable ref toself.frameinvm.rs, etc.) - Save
Functionto a list in VM, while theValuestores the index
- Garbage Collection
- Classes and Instances
- Optimization