How raphaelmeyer/crafting-interpreters works
Project Type and Purpose
• This repository contains two independent implementations of the Lox language from “Crafting Interpreters”:
a. clox: A bytecode‐based VM/interpreter written in C using CMake for build and simple unit specs.
b. hlox: A pure Haskell interpreter following the same language, structured around Cabal, with modules for scanning, parsing, resolution, and runtime.
• Purpose: Demonstrate language implementation techniques across two ecosystems.
File Structure Analysis
A. Top‐level
• .gitignore, LICENSE, README.md
• crafting‐interpreters.code‐workspace (IDE config)
B. clox/
• Build/config: CMakeLists.txt, CMakePresets.json, .clang-format
• src/:
– main.c (CLI entrypoint deciding REPL vs. file execution)
– debug.c/h (disassembler, logging)
– chunk.c/h (bytecode container, chunk specifications)
– memory.c/h (allocation, garbage collection stubs)
– value.c/h (value tagging, printing)
– *.spec.cc: unit tests for each module
• vendor/: third‐party code (if any)
C. hlox/
• TODO.md, crafting-interpreters.cabal, app/Main.hs (CLI entrypoint)
• src/Parser, src/Scanner, src/Resolver, src/Runtime, plus HLox.hs, Lox.hs glue modules
• test/: Hspec suites for scanner and integration
• Patterns: Both follow a classic compiler pipeline: Scanner → Parser → (for Haskell: Resolver) → Interpreter/VM → Output.
README Insights
• Minimal content; points back to the “Crafting Interpreters” book.
a. Main Components
• Source Input: CLI (main.c / Main.hs)
• Scanner (Lexer): clox/src/… (embedded in chunk/main) & hlox/src/Scanner
• Parser: clox (part of main or a submodule) & hlox/src/Parser
• Semantic Analysis / Resolver: Only in hlox/src/Resolver
• Bytecode Compiler / AST Evaluator: clox/src/chunk + value modules vs. hlox’s AST evaluator in Runtime modules
• Virtual Machine / Interpreter: clox VM in chunk + debug; hlox’s Interpreter in Runtime/Interpreter.hs
• Memory Management: clox/src/memory; in Haskell GC is implicit
• Tests: spec files under clox/src and hlox/test
• Build System: CMake for clox, Cabal for hlox
b. Relationships & Data Flow
1. CLI component reads source text → feeds Scanner → emits token stream
2. Parser consumes tokens → builds AST
3. (Hlox only) Resolver walks AST → annotates scoping information
4. clox: Compiler emits bytecode chunks; hlox: Interpreter walks AST directly
5. VM/Interpreter executes code → utilizes memory module (clox) or Haskell runtime
6. Debug module (clox) can hook into VM to disassemble or trace
7. Test suites import each module layer to verify correctness
c. Architectural Patterns & Principles
• Pipeline Architecture: Clear phase boundaries—lexing, parsing, analysis, execution
• Modularity: Each concern in its own directory/module, facilitating testing
• Single Responsibility: Scanner never parses, Parser never executes, VM never lexes
• Separation by Ecosystem: C implementation emphasizes manual memory, Haskell emphasizes pure functions and type systems
d. Key Technologies & Frameworks
• C/CMake, clang-format for clox
• Haskell/GHC, Cabal, Hspec for hlox
• Unit testing harness (spec.cc files, Hspec)
Detail
• That components unique to one implementation (Resolver in hlox, chunk in clox) are clearly delineated