# sharad420/lox architecture

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

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

## Overview

This repository contains two implementations of the Lox language: jLox, a tree-walk interpreter, and cLox, a bytecode compiler and virtual machine. In jLox, users provide a script or REPL input, which Lox scans, parses, resolves, and interprets; the interpreter supports functions, lambdas, classes, and instances. cLox compiles source to bytecode for VM execution. The graph preserves both runtimes and their major internal stages; some cLox relationships are based on the source index because those source excerpts were not provided.

Main entry points: [`cLox/vm.c`](https://github.com/sharad420/lox/blob/main/cLox/vm.c) (Virtual machine), [`jLox/src/main/java/com/craftinginterpreters/lox/Interpreter.java`](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Interpreter.java) (Tree-walk interpreter), [`jLox/src/main/java/com/craftinginterpreters/lox/Lox.java`](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Lox.java) (CLI and pipeline), [`jLox/src/main/java/com/craftinginterpreters/lox/Parser.java`](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Parser.java) (Parser)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_jfront["jLox Front End"]
  node_lox["CLI and pipeline<br/>[Lox.java]"]
  node_jscanner["Scanner<br/>[Scanner.java]"]
  node_tokens["Tokens<br/>[Token.java]"]
  node_parser["Parser<br/>[Parser.java]"]
  node_ast["AST nodes<br/>[Expr.java]"]
  node_statements["Statements<br/>[Stmt.java]"]
  node_resolver["Static resolver<br/>[Resolver.java]"]
  node_printer["AST printer<br/>[AstPrinter.java]"]
end

subgraph group_jruntime["jLox Runtime"]
  node_interpreter["Tree-walk interpreter<br/>[Interpreter.java]"]
  node_environment["Lexical environments<br/>[Environment.java]"]
  node_callables["Functions and lambdas<br/>[LoxFunction.java]"]
  node_classes["Classes and instances<br/>[LoxClass.java]"]
  node_nativeio["Native input / files<br/>[Interpreter.java]"]
end

subgraph group_ccompile["cLox Compilation"]
  node_cloxentry["CLI entry<br/>[main.c]"]
  node_cscanner["Scanner<br/>[scanner.c]"]
  node_compiler["Bytecode compiler<br/>[compiler.c]"]
  node_chunk["Bytecode chunks<br/>[chunk.h]"]
  node_debug["Bytecode disassembly<br/>[debug.c]"]
end

subgraph group_cruntime["cLox Runtime"]
  node_vm["Virtual machine<br/>[vm.c]"]
  node_objects["Runtime objects<br/>[object.c]"]
  node_values["Values<br/>[value.c]"]
  node_tables[("Hash tables<br/>[table.c]")]
  node_memory["Memory management<br/>[memory.c]"]
end

node_user(("Script / REPL user"))

node_user -->|"runs input"| node_lox
node_lox -->|"scans source"| node_jscanner
node_jscanner -->|"produces tokens"| node_tokens
node_lox -->|"parses tokens"| node_parser
node_parser -->|"builds expressions"| node_ast
node_parser -->|"builds statements"| node_statements
node_lox -->|"resolves scopes"| node_resolver
node_lox -->|"executes statements"| node_interpreter
node_resolver -->|"records scope depths"| node_interpreter
node_interpreter -->|"reads and assigns"| node_environment
node_interpreter -->|"creates functions"| node_callables
node_interpreter -->|"creates classes"| node_classes
node_interpreter -->|"provides built-ins"| node_nativeio
node_printer -->|"visits expressions"| node_ast
node_cloxentry -->|"invokes runtime"| node_vm
node_vm -->|"compiles source"| node_compiler
node_compiler -->|"scans source"| node_cscanner
node_compiler -->|"writes bytecode"| node_chunk
node_vm -->|"executes bytecode"| node_chunk
node_debug -->|"disassembles bytecode"| node_chunk
node_vm -->|"creates runtime objects"| node_objects
node_vm -->|"uses tables"| node_tables
node_vm -->|"operates on values"| node_values
node_memory -->|"allocates objects"| node_objects
node_memory -->|"manages VM memory"| node_vm
node_tables -->|"stores values"| node_values

click node_lox "https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Lox.java"
click node_jscanner "https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Scanner.java"
click node_tokens "https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Token.java"
click node_parser "https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Parser.java"
click node_ast "https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Expr.java"
click node_statements "https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Stmt.java"
click node_resolver "https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Resolver.java"
click node_printer "https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/AstPrinter.java"
click node_interpreter "https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Interpreter.java"
click node_environment "https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Environment.java"
click node_callables "https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/LoxFunction.java"
click node_classes "https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/LoxClass.java"
click node_nativeio "https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Interpreter.java"
click node_cloxentry "https://github.com/sharad420/lox/blob/main/cLox/main.c"
click node_cscanner "https://github.com/sharad420/lox/blob/main/cLox/scanner.c"
click node_compiler "https://github.com/sharad420/lox/blob/main/cLox/compiler.c"
click node_chunk "https://github.com/sharad420/lox/blob/main/cLox/chunk.h"
click node_debug "https://github.com/sharad420/lox/blob/main/cLox/debug.c"
click node_vm "https://github.com/sharad420/lox/blob/main/cLox/vm.c"
click node_objects "https://github.com/sharad420/lox/blob/main/cLox/object.c"
click node_values "https://github.com/sharad420/lox/blob/main/cLox/value.c"
click node_tables "https://github.com/sharad420/lox/blob/main/cLox/table.c"
click node_memory "https://github.com/sharad420/lox/blob/main/cLox/memory.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_lox,node_jscanner,node_tokens,node_parser,node_ast,node_statements,node_resolver,node_printer,node_user toneBlue
class node_interpreter,node_environment,node_callables,node_classes,node_nativeio toneAmber
class node_cloxentry,node_cscanner,node_compiler,node_chunk,node_debug toneMint
class node_vm,node_objects,node_values,node_tables,node_memory toneRose
```

## Components

### jLox Front End

- [CLI and pipeline](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Lox.java): `jLox/src/main/java/com/craftinginterpreters/lox/Lox.java`
- [Scanner](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Scanner.java): `jLox/src/main/java/com/craftinginterpreters/lox/Scanner.java`
- [Tokens](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Token.java): `jLox/src/main/java/com/craftinginterpreters/lox/Token.java`
- [Parser](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Parser.java): `jLox/src/main/java/com/craftinginterpreters/lox/Parser.java`
- [AST nodes](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Expr.java): `jLox/src/main/java/com/craftinginterpreters/lox/Expr.java`
- [Statements](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Stmt.java): `jLox/src/main/java/com/craftinginterpreters/lox/Stmt.java`
- [Static resolver](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Resolver.java): `jLox/src/main/java/com/craftinginterpreters/lox/Resolver.java`
- [AST printer](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/AstPrinter.java): `jLox/src/main/java/com/craftinginterpreters/lox/AstPrinter.java`

### jLox Runtime

- [Tree-walk interpreter](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Interpreter.java): `jLox/src/main/java/com/craftinginterpreters/lox/Interpreter.java`
- [Lexical environments](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Environment.java): `jLox/src/main/java/com/craftinginterpreters/lox/Environment.java`
- [Functions and lambdas](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/LoxFunction.java): `jLox/src/main/java/com/craftinginterpreters/lox/LoxFunction.java`
- [Classes and instances](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/LoxClass.java): `jLox/src/main/java/com/craftinginterpreters/lox/LoxClass.java`
- [Native input / files](https://github.com/sharad420/lox/blob/main/jLox/src/main/java/com/craftinginterpreters/lox/Interpreter.java): `jLox/src/main/java/com/craftinginterpreters/lox/Interpreter.java`

### cLox Compilation

- [CLI entry](https://github.com/sharad420/lox/blob/main/cLox/main.c): `cLox/main.c`
- [Scanner](https://github.com/sharad420/lox/blob/main/cLox/scanner.c): `cLox/scanner.c`
- [Bytecode compiler](https://github.com/sharad420/lox/blob/main/cLox/compiler.c): `cLox/compiler.c`
- [Bytecode chunks](https://github.com/sharad420/lox/blob/main/cLox/chunk.h): `cLox/chunk.h`
- [Bytecode disassembly](https://github.com/sharad420/lox/blob/main/cLox/debug.c): `cLox/debug.c`

### cLox Runtime

- [Virtual machine](https://github.com/sharad420/lox/blob/main/cLox/vm.c): `cLox/vm.c`
- [Runtime objects](https://github.com/sharad420/lox/blob/main/cLox/object.c): `cLox/object.c`
- [Values](https://github.com/sharad420/lox/blob/main/cLox/value.c): `cLox/value.c`
- [Hash tables](https://github.com/sharad420/lox/blob/main/cLox/table.c): `cLox/table.c`
- [Memory management](https://github.com/sharad420/lox/blob/main/cLox/memory.c): `cLox/memory.c`

### Other components

- **Script / REPL user**

## Connections

- Script / REPL user → CLI and pipeline: runs input (evidence: `jLox/src/main/java/com/craftinginterpreters/lox/Lox.java`)
- CLI and pipeline → Scanner: scans source (evidence: `jLox/src/main/java/com/craftinginterpreters/lox/Lox.java`)
- Scanner → Tokens: produces tokens (evidence: `jLox/src/main/java/com/craftinginterpreters/lox/Scanner.java`)
- CLI and pipeline → Parser: parses tokens (evidence: `jLox/src/main/java/com/craftinginterpreters/lox/Lox.java`)
- Parser → AST nodes: builds expressions (evidence: `jLox/src/main/java/com/craftinginterpreters/lox/Parser.java`)
- Parser → Statements: builds statements (evidence: `jLox/src/main/java/com/craftinginterpreters/lox/Parser.java`)
- CLI and pipeline → Static resolver: resolves scopes (evidence: `jLox/src/main/java/com/craftinginterpreters/lox/Lox.java`)
- CLI and pipeline → Tree-walk interpreter: executes statements (evidence: `jLox/src/main/java/com/craftinginterpreters/lox/Lox.java`)
- Static resolver → Tree-walk interpreter: records scope depths (evidence: `jLox/src/main/java/com/craftinginterpreters/lox/Resolver.java`)
- Tree-walk interpreter → Lexical environments: reads and assigns (evidence: `jLox/src/main/java/com/craftinginterpreters/lox/Interpreter.java`)
- Tree-walk interpreter → Functions and lambdas: creates functions (evidence: `jLox/src/main/java/com/craftinginterpreters/lox/Interpreter.java`)
- Tree-walk interpreter → Classes and instances: creates classes (evidence: `jLox/src/main/java/com/craftinginterpreters/lox/Interpreter.java`)
- Tree-walk interpreter → Native input / files: provides built-ins (evidence: `jLox/src/main/java/com/craftinginterpreters/lox/Interpreter.java`)
- AST printer → AST nodes: visits expressions (evidence: `jLox/src/main/java/com/craftinginterpreters/lox/AstPrinter.java`)
- CLI entry → Virtual machine: invokes runtime (evidence: `cLox/main.c`)
- Virtual machine → Bytecode compiler: compiles source (evidence: `cLox/vm.c`)
- Bytecode compiler → Scanner: scans source (evidence: `cLox/compiler.c`)
- Bytecode compiler → Bytecode chunks: writes bytecode (evidence: `cLox/compiler.c`)
- Virtual machine → Bytecode chunks: executes bytecode (evidence: `cLox/vm.c`)
- Bytecode disassembly → Bytecode chunks: disassembles bytecode (evidence: `cLox/debug.c`)
- Virtual machine → Runtime objects: creates runtime objects (evidence: `cLox/vm.c`)
- Virtual machine → Hash tables: uses tables (evidence: `cLox/vm.c`)
- Virtual machine → Values: operates on values (evidence: `cLox/vm.c`)
- Memory management → Runtime objects: allocates objects (evidence: `cLox/memory.c`)
- Memory management → Virtual machine: manages VM memory (evidence: `cLox/memory.c`)
- Hash tables → Values: stores values (evidence: `cLox/table.c`)
