How sheunl/phloxlang works
Based on your file tree and README, this repository is a “learning playground” for building simple compilers and interpreters, following ideas from the “Crafting Interpreters” book. In fact, you have two distinct implementations: one in PHP (named “phlox”) and another in Rust (named “rlox”). The PHP version appears to be a complete interpreter with components for scanning, parsing, building an abstract syntax tree (AST) through expression and statement classes, and handling runtime concerns (such as environments, errors, and function/class representation). Meanwhile, the Rust version is a more compact implementation dealing with chunks, memory, debugging, and the overall flow (as seen in main.rs).
• Overall Project – “Basic Compilers” or “Interpreters”
• PHP-based implementation (“phlox”)
• Rust-based implementation (“rlox”)
For the PHP implementation (phlox), highlight the following components
a. Lexical Analysis & Scanning:
– Module: Scanner (phlox/src/Phlox/Scanner.php)
– Role: Tokenizing the source code
b. Parsing & AST Construction:
– Modules: Parser (phlox/src/Phlox/Parser.php), and AST nodes under Expr (e.g., Binary, Literal, Grouping, etc.) and Stmt directories (e.g., Expression, If_.php, While_.php, etc.)
– Role: Converting tokens into an AST representation
c. Semantic Analysis:
– Module: Resolver (phlox/src/Phlox/Resolver.php)
– Role: Managing scope rules and resolving variable references
d. Interpreter Runtime:
– Modules: Interpreter (phlox/src/Phlox/Interpreter.php) and Environment (phlox/src/Phlox/Environment.php)
– Role: Executing the AST and managing runtime environments
e. Object Model & Data Structures:
– Core Types: LoxCallable, LoxClass, LoxFunction, LoxInstance, ClassType, FunctionType
– Data Structures: Custom implementations of Map and Stack (phlox/src/Phlox/DS)
f. Error Handling & Support:
– RuntimeError and Return_ modules to manage exceptions and returns from functions
g. Tooling & Testing:
– Tools directory for code generation or packaging (GenerateAst.php, PharBuilder.php)
– Extensive tests (unit, feature, and performance tests) under phlox/test
For the Rust implementation (rlox), include
a. Core Entry Point and Management:
– Main module (rlox/src/main.rs) as the central entry point
b. Code execution and memory:
– Chunk management (rlox/src/chunk.rs) representing instructions or bytecode sequences
– Memory management (rlox/src/memory.rs) to handle runtime allocations or garbage collection aspects
c. Debugging:
– Debug functionalities (rlox/src/debug.rs)
– Emphasize the separation of concerns, where the entry point orchestrates the lower-level processing modules
– Scanner → Parser → Resolver → Interpreter (depicting execution order in phlox)
– Main → Chunk/Memory/Debug in rlox flows.
• Dependencies like the Parser’s reliance on tokens from the Scanner, and the Interpreter’s dependency on the AST produced by the Parser.
• How testing modules interact with these core components, ensuring correctness and performance verification.
Architectural Patterns and Design Principles
• That the PHP implementation follows an interpreter pattern with clearly separated phases (lexing, parsing, semantic analysis, interpretation) and adheres to an AST visitor design pattern (seen in the Expr/Visitor.php and Stmt/Visitor.php).
• The modular design in both implementations reflects separation of concerns and makes it easier to extend or replace modules (for instance, swapping out the Scanner or adding new types of AST nodes).