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
OpCode
enum (instead ofu8
), which could be > 1 byte. A chunk has aVec
ofOpCode
.- Different offset calculation
- Instead of reading 2 bytes,
OpCode::Constant
,OpCode::GetGlobal
andOpCode::DefineGlobal
includes au8
as the extra byte
- Use
usize
index instead of pointer+dereference to access element in array.- Though pointer+dereference should be faster?
- Tagged union replaced by Enum(T)
- No
Value::Obj
that can save arbitary object - String Object (
Value::StringObj(u32)
) is interned byHashMap<String, u32>
- No printing for
Function
object - 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.frame
invm.rs
, etc.) - Save
Function
to a list in VM, while theValue
stores the index
- Garbage Collection
- Classes and Instances
- Optimization