# hussein-saad/tslox architecture

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

- Interactive diagram: https://gitdiagram.com/hussein-saad/tslox
- Repository: https://github.com/hussein-saad/tslox

## Overview

tslox is a TypeScript tree-walk interpreter. A user supplies a script or enters REPL input; the Lox entry point scans and parses it, resolves lexical scopes, then invokes the interpreter to evaluate statements and expressions. The interpreter uses environments for variables and closures, and callable functions for calls and returns. The README also documents short-circuit logic, loops, ternary expressions, native clock(), and error reporting. Relationships below are limited to the README and source excerpts or index references; unsampled implementation details are not assumed.

Main entry points: [`src/interpreter.ts`](https://github.com/hussein-saad/tslox/blob/main/src/interpreter.ts) (Tree-walk interpreter), [`src/lox.ts`](https://github.com/hussein-saad/tslox/blob/main/src/lox.ts) (CLI and REPL), [`src/parser.ts`](https://github.com/hussein-saad/tslox/blob/main/src/parser.ts) (Parser), [`src/scanner.ts`](https://github.com/hussein-saad/tslox/blob/main/src/scanner.ts) (Scanner)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_front["Language Front End"]
  node_scanner["Scanner<br/>[scanner.ts]"]
  node_tokens["Tokens and kinds<br/>[token.ts]"]
  node_token_types["Token kinds<br/>[token-type.ts]"]
  node_parser["Parser<br/>[parser.ts]"]
  node_ast_expr["Expression AST<br/>[expression.ts]"]
  node_ast_stmt["Statement AST<br/>[stmt.ts]"]
  node_printer["AST printer<br/>[printer.ts]"]
end

subgraph group_semantic["Scope and Runtime"]
  node_resolver["Scope resolver<br/>[resolver.ts]"]
  node_environment["Environments<br/>[environment.ts]"]
end

subgraph group_execution["Execution and I/O"]
  node_entry["CLI and REPL<br/>[lox.ts]"]
  node_interpreter["Tree-walk interpreter<br/>[interpreter.ts]"]
  node_callable["Callable protocol<br/>[callable.ts]"]
  node_functions["Lox functions<br/>[function.ts]"]
  node_return["Return signal<br/>[return.ts]"]
  node_runtime_error["Runtime errors<br/>[runtimeerror.ts]"]
end

node_user(("Lox user"))
node_output(("Terminal output"))

node_user -->|"provides input"| node_entry
node_entry -->|"scans source"| node_scanner
node_scanner -->|"emits tokens"| node_tokens
node_scanner -->|"tags tokens"| node_token_types
node_scanner -->|"reports scan errors"| node_entry
node_entry -->|"parses tokens"| node_parser
node_parser -->|"builds expressions"| node_ast_expr
node_parser -->|"builds statements"| node_ast_stmt
node_parser -->|"reads tokens"| node_tokens
node_parser -->|"matches kinds"| node_token_types
node_parser -->|"reports parse errors"| node_entry
node_entry -->|"resolves statements"| node_resolver
node_resolver -->|"records local depth"| node_interpreter
node_entry -->|"interprets statements"| node_interpreter
node_interpreter -->|"visits expressions"| node_ast_expr
node_interpreter -->|"executes statements"| node_ast_stmt
node_interpreter -->|"reads and updates"| node_environment
node_interpreter -->|"checks and invokes"| node_callable
node_interpreter -->|"creates functions"| node_functions
node_functions -->|"captures closure"| node_environment
node_functions -->|"executes function body"| node_interpreter
node_functions -->|"catches return"| node_return
node_interpreter -->|"throws return signal"| node_return
node_interpreter -->|"raises runtime errors"| node_runtime_error
node_interpreter -->|"reports runtime errors"| node_entry
node_entry -->|"prints results and errors"| node_output
node_printer -->|"prints expressions"| node_ast_expr

click node_entry "https://github.com/hussein-saad/tslox/blob/main/src/lox.ts"
click node_scanner "https://github.com/hussein-saad/tslox/blob/main/src/scanner.ts"
click node_tokens "https://github.com/hussein-saad/tslox/blob/main/src/token.ts"
click node_token_types "https://github.com/hussein-saad/tslox/blob/main/src/token-type.ts"
click node_parser "https://github.com/hussein-saad/tslox/blob/main/src/parser.ts"
click node_ast_expr "https://github.com/hussein-saad/tslox/blob/main/src/expression.ts"
click node_ast_stmt "https://github.com/hussein-saad/tslox/blob/main/src/stmt.ts"
click node_resolver "https://github.com/hussein-saad/tslox/blob/main/src/resolver.ts"
click node_interpreter "https://github.com/hussein-saad/tslox/blob/main/src/interpreter.ts"
click node_environment "https://github.com/hussein-saad/tslox/blob/main/src/environment.ts"
click node_callable "https://github.com/hussein-saad/tslox/blob/main/src/callable.ts"
click node_functions "https://github.com/hussein-saad/tslox/blob/main/src/function.ts"
click node_return "https://github.com/hussein-saad/tslox/blob/main/src/return.ts"
click node_runtime_error "https://github.com/hussein-saad/tslox/blob/main/src/runtimeerror.ts"
click node_printer "https://github.com/hussein-saad/tslox/blob/main/src/printer.ts"

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_scanner,node_tokens,node_token_types,node_parser,node_ast_expr,node_ast_stmt,node_printer,node_user toneBlue
class node_resolver,node_environment toneAmber
class node_entry,node_interpreter,node_callable,node_functions,node_return,node_runtime_error toneMint
class node_output toneIndigo
```

## Components

### Language Front End

- [Scanner](https://github.com/hussein-saad/tslox/blob/main/src/scanner.ts): `src/scanner.ts`
- [Tokens and kinds](https://github.com/hussein-saad/tslox/blob/main/src/token.ts): `src/token.ts`
- [Token kinds](https://github.com/hussein-saad/tslox/blob/main/src/token-type.ts): `src/token-type.ts`
- [Parser](https://github.com/hussein-saad/tslox/blob/main/src/parser.ts): `src/parser.ts`
- [Expression AST](https://github.com/hussein-saad/tslox/blob/main/src/expression.ts): `src/expression.ts`
- [Statement AST](https://github.com/hussein-saad/tslox/blob/main/src/stmt.ts): `src/stmt.ts`
- [AST printer](https://github.com/hussein-saad/tslox/blob/main/src/printer.ts): `src/printer.ts`

### Scope and Runtime

- [Scope resolver](https://github.com/hussein-saad/tslox/blob/main/src/resolver.ts): `src/resolver.ts`
- [Environments](https://github.com/hussein-saad/tslox/blob/main/src/environment.ts): `src/environment.ts`

### Execution and I/O

- [CLI and REPL](https://github.com/hussein-saad/tslox/blob/main/src/lox.ts): `src/lox.ts`
- [Tree-walk interpreter](https://github.com/hussein-saad/tslox/blob/main/src/interpreter.ts): `src/interpreter.ts`
- [Callable protocol](https://github.com/hussein-saad/tslox/blob/main/src/callable.ts): `src/callable.ts`
- [Lox functions](https://github.com/hussein-saad/tslox/blob/main/src/function.ts): `src/function.ts`
- [Return signal](https://github.com/hussein-saad/tslox/blob/main/src/return.ts): `src/return.ts`
- [Runtime errors](https://github.com/hussein-saad/tslox/blob/main/src/runtimeerror.ts): `src/runtimeerror.ts`

### Other components

- **Lox user**
- **Terminal output**

## Connections

- Lox user → CLI and REPL: provides input (evidence: `README.md`)
- CLI and REPL → Scanner: scans source (evidence: `src/lox.ts`)
- Scanner → Tokens and kinds: emits tokens (evidence: `src/scanner.ts`)
- Scanner → Token kinds: tags tokens (evidence: `src/scanner.ts`)
- Scanner → CLI and REPL: reports scan errors (evidence: `src/scanner.ts`)
- CLI and REPL → Parser: parses tokens (evidence: `src/lox.ts`)
- Parser → Expression AST: builds expressions (evidence: `src/parser.ts`)
- Parser → Statement AST: builds statements (evidence: `src/parser.ts`)
- Parser → Tokens and kinds: reads tokens (evidence: `src/parser.ts`)
- Parser → Token kinds: matches kinds (evidence: `src/parser.ts`)
- Parser → CLI and REPL: reports parse errors (evidence: `src/parser.ts`)
- CLI and REPL → Scope resolver: resolves statements (evidence: `src/lox.ts`)
- Scope resolver → Tree-walk interpreter: records local depth (evidence: `src/resolver.ts`)
- CLI and REPL → Tree-walk interpreter: interprets statements (evidence: `src/lox.ts`)
- Tree-walk interpreter → Expression AST: visits expressions (evidence: `src/interpreter.ts`)
- Tree-walk interpreter → Statement AST: executes statements (evidence: `src/interpreter.ts`)
- Tree-walk interpreter → Environments: reads and updates (evidence: `src/interpreter.ts`)
- Tree-walk interpreter → Callable protocol: checks and invokes (evidence: `src/interpreter.ts`)
- Tree-walk interpreter → Lox functions: creates functions (evidence: `src/interpreter.ts`)
- Lox functions → Environments: captures closure (evidence: `src/function.ts`)
- Lox functions → Tree-walk interpreter: executes function body (evidence: `src/function.ts`)
- Lox functions → Return signal: catches return (evidence: `src/function.ts`)
- Tree-walk interpreter → Return signal: throws return signal (evidence: `src/interpreter.ts`)
- Tree-walk interpreter → Runtime errors: raises runtime errors (evidence: `src/interpreter.ts`)
- Tree-walk interpreter → CLI and REPL: reports runtime errors (evidence: `src/interpreter.ts`)
- CLI and REPL → Terminal output: prints results and errors (evidence: `src/lox.ts`)
- AST printer → Expression AST: prints expressions (evidence: `src/printer.ts`)
