# jkylander/codecrafters-interpreter-c architecture

> Architecture diagram of the GitHub repository jkylander/codecrafters-interpreter-c, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jkylander/codecrafters-interpreter-c
- Repository: https://github.com/jkylander/codecrafters-interpreter-c

## Overview

##### Main Components

a) Scanner/Lexer (scanner.c/h)
- Input: Source code text
- Output: Token stream
- Purpose: Breaks source code into tokens

b) Parser (parse.c/h)
- Input: Token stream
- Output: Abstract Syntax Tree (AST)
- Purpose: Creates syntactic structure

c) AST Components (ast.c/h, stmt.h)
- Purpose: Represents program structure
- Contains: Expression and Statement nodes

d) Interpreter (interpreter.c/h)
- Input: AST
- Output: Execution results
- Purpose: Evaluates the AST

**[Source Code] → [Scanner] → [Token Stream] → [Parser] → [AST] → [Interpreter] → [Program Output]**

##### Component Details

For each major component, include a sub-section showing:

Scanner:
- Character processing
- Token generation
- Error handling

Parser:
- Grammar rules
- AST node creation
- Error recovery

AST:
- Expression types
- Statement types
- Tree structure

Interpreter:
- Environment handling
- Expression evaluation
- Statement execution

##### Important Relationships

- How the AST (ast.h) is used by both Parser and Interpreter
- Error handling flows
- Relationship between statements (stmt.h) and the AST
- Demonstrate the single-pass nature of the interpreter

##### Technical Details

- That this is a tree-walk interpreter
- Main execution flow starting from main.c
- Error reporting paths
- Where ANSI handling occurs (ansi.h)

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Legend
        style Legend fill:#f5f5f5,stroke:#333
        L1[Component]:::component
        L2([Data Flow]):::data
        L3[/Input/Output/]:::io
        L4{{Process}}:::process
        L1 --> L2
        L2 --> L3
        L3 --> L4
    end

    subgraph "Source Input"
        SourceCode[/Source Code/]:::io
    end

    subgraph "Lexical Analysis"
        Scanner[Scanner/Lexer]:::component
        TokenStream([Token Stream]):::data
    end

    subgraph "Syntax Analysis"
        Parser[Parser]:::component
        AST([Abstract Syntax Tree]):::data
    end

    subgraph "AST Components"
        Expressions[Expression Nodes]:::component
        Statements[Statement Nodes]:::component
    end

    subgraph "Execution"
        Interpreter[Interpreter]:::component
        Environment([Environment]):::data
        Output[/Program Output/]:::io
    end

    subgraph "Error Handling"
        ErrorReporting{{Error Reporting}}:::process
        ANSIHandler[ANSI Handler]:::component
    end

    subgraph "Build System"
        CMake[CMake Configuration]:::build
        Make[Makefile]:::build
        RunScript[Run Script]:::build
        CompileScript[Compile Script]:::build
    end

    %% Main Flow
    SourceCode --> Scanner
    Scanner --> TokenStream
    TokenStream --> Parser
    Parser --> AST
    AST --> Interpreter
    Interpreter --> Output

    %% AST Relationships
    AST --- Expressions
    AST --- Statements
    
    %% Error Handling
    Scanner -.-> ErrorReporting
    Parser -.-> ErrorReporting
    Interpreter -.-> ErrorReporting
    ErrorReporting -.-> ANSIHandler

    %% Build System Relations
    CMake --> Make
    Make --> CompileScript
    CompileScript --> RunScript

    %% Component Mappings
    click Scanner "https://github.com/jkylander/codecrafters-interpreter-c/blob/master/src/scanner.c"
    click Parser "https://github.com/jkylander/codecrafters-interpreter-c/blob/master/src/parse.c"
    click Expressions "https://github.com/jkylander/codecrafters-interpreter-c/blob/master/src/ast.c"
    click Statements "https://github.com/jkylander/codecrafters-interpreter-c/blob/master/src/stmt.h"
    click Interpreter "https://github.com/jkylander/codecrafters-interpreter-c/blob/master/src/interpreter.c"
    click ANSIHandler "https://github.com/jkylander/codecrafters-interpreter-c/blob/master/src/ansi.h"
    click CMake "https://github.com/jkylander/codecrafters-interpreter-c/blob/master/CMakeLists.txt"
    click Make "https://github.com/jkylander/codecrafters-interpreter-c/tree/master/Makefile"
    click RunScript "https://github.com/jkylander/codecrafters-interpreter-c/blob/master/.codecrafters/run.sh"
    click CompileScript "https://github.com/jkylander/codecrafters-interpreter-c/blob/master/.codecrafters/compile.sh"

    %% Styles
    classDef component fill:#a8d1f0,stroke:#333
    classDef data fill:#ffe6a8,stroke:#333
    classDef io fill:#98fb98,stroke:#333
    classDef process fill:#ffa8a8,stroke:#333
    classDef build fill:#d3d3d3,stroke:#333
```
