# hkey0/lox-interpreter architecture

> Architecture diagram of the GitHub repository hkey0/lox-interpreter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hkey0/lox-interpreter
- Repository: https://github.com/hkey0/lox-interpreter

## Overview

##### Project type and purpose
- This is a small compiler-style project: a Lox language interpreter written in Rust.
- It reads a “.lox” script, lexes it into tokens, builds an AST from the grammar, and then executes (interprets) it.

##### File structure
- Top-level:
- Cargo.toml – Rust dependencies/metadata
- first_program.lox – sample Lox source file
- src/
– scanner.rs – the lexer module
– generate_ast.rs – code that defines AST node types and visitor interfaces
– main.rs – orchestrates the pipeline (reads input, calls scanner, parser/interpreter)
- No external services or databases—this is a pure in-process compiler/interpreter.

##### README
- Usage is simply “cargo run -- first_program.lox,” confirming it’s a command-line compiler/interpreter.

##### Detail
- Break out subcomponents if needed (e.g., separate lexical rules vs. token buffer in Scanner).
- That generate_ast.rs actually generates AST node definitions and the visitor interface.
- Central role of main.rs as the orchestrator that ties modules together.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Data Artifacts
    SourceFile["Source File\n(first_program.lox)"]:::data
    ProjectMetadata["Cargo.toml"]:::data
    TokenStream["Token Stream"]:::data
    ASTStructure["AST Structure"]:::data

    %% Code Modules
    CLI["CLI Launcher"]:::codeMod
    Scanner["Scanner Module"]:::codeMod
    Parser["Parser/AST Generator"]:::codeMod
    Interpreter["Interpreter Module"]:::codeMod
    ErrorHandler["Error Handler"]:::error

    %% Data Flows
    SourceFile -->|"source text"| CLI
    CLI -->|"source text"| Scanner
    Scanner -->|"Token stream"| Parser
    Parser -->|"AST"| Interpreter
    Interpreter -->|"output/result"| CLI

    %% Error Flows
    Scanner -->|"scan error"| ErrorHandler
    Parser -->|"parse error"| ErrorHandler
    Interpreter -->|"runtime error"| ErrorHandler

    %% Click Events
    click CLI "https://github.com/hkey0/lox-interpreter/blob/main/src/main.rs"
    click Scanner "https://github.com/hkey0/lox-interpreter/blob/main/src/scanner.rs"
    click Parser "https://github.com/hkey0/lox-interpreter/blob/main/src/generate_ast.rs"
    click Interpreter "https://github.com/hkey0/lox-interpreter/blob/main/src/main.rs"
    click ErrorHandler "https://github.com/hkey0/lox-interpreter/blob/main/src/main.rs"
    click SourceFile "https://github.com/hkey0/lox-interpreter/blob/main/first_program.lox"
    click TokenStream "https://github.com/hkey0/lox-interpreter/blob/main/src/scanner.rs"
    click ASTStructure "https://github.com/hkey0/lox-interpreter/blob/main/src/generate_ast.rs"
    click ProjectMetadata "https://github.com/hkey0/lox-interpreter/blob/main/Cargo.toml"

    %% Styles
    classDef codeMod fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef data fill:#90EE90,stroke:#333,stroke-width:1px
    classDef error fill:#FFA500,stroke:#333,stroke-width:1px
```
