Menu

1 / 5

rlox: the Lox language in Rust, as a tree-walker and a bytecode VM

DEV Community: rust·Abdeldjalil Chougui·8 days ago
#gJBTx4Yh
#devtorust#devto#dev#tree#rlox
Long article (1174 words) — no AI summary yet.
Reading 0:00
15s threshold

Every developer uses an interpreter or a compiler daily, and for most of us it stays a black box. We know there is a parser, something about a syntax tree, maybe "bytecode". But how does a closure keep a variable alive after its function returned? What does super.method() actually look up? Why is a bytecode VM faster than walking the tree?

Robert Nystrom's book Crafting Interpreters answers those questions by building one small language, Lox, twice: first as a tree-walk interpreter in Java ("jlox"), then as a bytecode virtual machine in C ("clox"). rlox is my implementation of both in Rust, in one repository, sharing one scanner and one test suite. This post is about what I built and what it teaches.

What I built

Lox is a small dynamic language with a C-like syntax. Both back ends in rlox support the full language: variables, blocks, if / while / for , functions, closures, classes, fields, methods, initializers, this , inheritance and super , plus the clock() native function.

A shared scanner.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.