# hlee131/pyjac architecture

> Architecture diagram of the GitHub repository hlee131/pyjac, 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/hlee131/pyjac
- Repository: https://github.com/hlee131/pyjac

## Overview

##### Main Processing Pipeline (Central Flow)
- Source Code Input
- Lexical Analysis (Lexer)
- Parsing
- Type Checking
- Code Generation (LLVM IR)
- Final Executable Output

##### Component Details

a. Lexer Component (src/lexer.c):
- Input handling for characters
- Special indentation handling subsystem
- Error recovery mechanism (panic mode)
- Output: Token stream

b. Parser Component (src/parser.c):
- Two parsing strategies:
- LL(1) predictive parser for statements
- Pratt parser for expressions
- Output: Abstract Syntax Tree (AST)

c. Type Checker Component (part of semantic analysis):
- Multi-pass system:
- Pass 1: Global symbol collection
- Pass 2: Statement/Expression checking
- Symbol table integration
- Error handling with 'error' subtype

d. Code Generator Component (src/codegen.c):
- LLVM IR generation process
- Link to optimization pipeline

##### Supporting Systems

a. Symbol Table (src/symtab.c):
- Hash table structure
- Scope management

b. AST Management (src/ast.c):
- Tree structure
- Link to both parser and type checker

##### Specific Implementation Details

Important architectural features:
- Indentation stack for Python-like syntax
- Error recovery mechanisms
- Symbol table hierarchy
- Type system with error subtype
- LLVM IR integration point

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Legend
        L1[Processing Component]
        L2[(Data Store)]
        L3[/"Input/Output"/]
        L4{{"Decision Point"}}
        style L1 fill:#a8d1f0
        style L2 fill:#90EE90
        style L3 fill:#FFE4B5
        style L4 fill:#FFB6C1
    end

    %% Input
    Input[/"Source Code (.pjc)"/]
    click Input "https://github.com/hlee131/pyjac/blob/master/examples/one.pjc"

    %% Main Pipeline Components
    Lexer[Lexical Analysis]
    click Lexer "https://github.com/hlee131/pyjac/blob/master/src/lexer.c"
    Parser[Parser]
    click Parser "https://github.com/hlee131/pyjac/blob/master/src/parser.c"
    TypeChecker[Type Checker]
    CodeGen[Code Generator]
    click CodeGen "https://github.com/hlee131/pyjac/blob/master/src/codegen.c"
    Output[/"Executable Output"/]

    %% Data Stores
    SymTab[(Symbol Table)]
    click SymTab "https://github.com/hlee131/pyjac/blob/master/src/symtab.c"
    AST[(Abstract Syntax Tree)]
    click AST "https://github.com/hlee131/pyjac/blob/master/src/ast.c"

    %% Supporting Components
    subgraph "Error Handling"
        ErrorRecovery{{"Error Recovery"}}
        PanicMode{{"Panic Mode"}}
    end

    subgraph "Parser Strategies"
        LL1["LL(1) Predictive"]
        PrattParser["Pratt Parser"]
    end

    subgraph "Type Checking Passes"
        Pass1["Pass 1: Global Symbols"]
        Pass2["Pass 2: Statement/Expression"]
    end

    subgraph "Code Generation"
        LLVMIR["LLVM IR Generation"]
        Optimize["Optimization Pipeline"]
    end

    %% Build System
    BuildSystem["Build System"]
    click BuildSystem "https://github.com/hlee131/pyjac/tree/master/Makefile"

    %% Connections
    Input --> Lexer
    Lexer --> Parser
    Parser --> AST
    AST --> TypeChecker
    TypeChecker --> CodeGen
    CodeGen --> Output
    
    %% Data Store Connections
    Parser <--> SymTab
    TypeChecker <--> SymTab
    Parser --> AST
    TypeChecker --> AST

    %% Error Handling
    Lexer -.-> ErrorRecovery
    Parser -.-> PanicMode
    TypeChecker -.-> ErrorRecovery

    %% Parser Strategy Connections
    Parser --> LL1
    Parser --> PrattParser

    %% Type Checking Pass Connections
    TypeChecker --> Pass1
    TypeChecker --> Pass2

    %% Code Generation Connections
    CodeGen --> LLVMIR
    LLVMIR --> Optimize

    %% Build System Connections
    BuildSystem --> Output

    %% Styling
    classDef component fill:#a8d1f0
    classDef datastore fill:#90EE90
    classDef io fill:#FFE4B5
    classDef error fill:#FFB6C1
    
    class Lexer,Parser,TypeChecker,CodeGen component
    class SymTab,AST datastore
    class Input,Output io
    class ErrorRecovery,PanicMode error
```
