This project was completed as a part of Bradfield's Compilers class.
minimal-lox is a minimal implementation of Lox that supports Fibonacci number generation.
The Lox language was introduced in Crafting Interpreters, implemented first with a tree-walk interpreter (source code → tokens → AST → execution) written in Java, and second with a compiler and bytecode VM (source code → tokens → bytecode → execution) in C.
In an exercise similar to Lindsey Kuper's post, we implement Lox in Python through multiple iterations:
- v1 - compiler; up to conditionals.
- v2 - compiler; up to functions, with more guardrails and written in a functional style.
- v3 - interpreter; up to scope.
- v4 - interpreter + compiler; up to functions.
- v5 - interpreter + compiler; up to functions, simplified implementation.
The v4 implementation was completed as a part of Bradfield's Compiler class. The interpreter plus compiler design implements (1) source code → tokens → AST → execution, as well as (2) source code → tokens → AST → bytecode → execution. In particular, the implementation supports Fibonacci number generation as a use case, and can be run as a REPL (example here, sample source code here).
The v5 implementation / minimal-lox is a simplified version of v4, introduced to consolidate learning. More specifically, minimal-lox implements arithmetic operations, statements, variables, conditionals and functions.
- expr - abstract class for expressions
- statem - abstract class for statements
- scanner - converts raw source code into tokens
- parser - converts tokens into AST via recursive descent
- interpreter - execution by walking through the AST
- compiler - converts AST into bytecode
- vm - execution by interpreting the bytecode