# tgw314/lion architecture

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

- Interactive diagram: https://gitdiagram.com/tgw314/lion
- Repository: https://github.com/tgw314/lion

## Overview

##### Main Components
- Lexical Analyzer: Represented by tokenize.c – this module handles scanning the source code input and converting it into tokens.
- Parser: Shown by parse.c – it takes the token stream and builds the syntactic structure (likely an abstract syntax tree).
- Type System/Semantic Analysis: Indicated by type.c – responsible for handling type checking and possibly other semantic validations.
- Code Generation: Carved out in codegen.c – generates target code from the processed AST.
- Core Entry Point: main.c – coordinates compiler execution and integrates the various phases.
- Additional Support/Test Components: Include test.c and test_common.c, which are used to validate the compiler's functionality.
- Utility/Supporting Files: lion.h for function prototypes and shared definitions, Makefile for build instructions, .clang-format for code style, and .gitignore to manage version control artifacts.
- Self-compilation or auxiliary functionality: The self.py file likely serves as a script to facilitate some self-hosting or testing behavior.

##### Relationships and Interactions
- Lexical analysis: main.c directs the input stream into the tokenizer (tokenize.c), where source text is split into tokens.
- Parsing: Tokens are then passed to the parser (parse.c) which creates the AST.
- Semantic Analysis: The AST is then processed by type.c for type checking and semantic validation.
- Code Generation: Once validated, the code generator (codegen.c) is invoked to produce executable or intermediate code.
- Testing: test.c and test_common.c should be depicted as auxiliary components that interact with the compiler modules to ensure correctness.
- Utility: lion.h is shared among most modules, acting as a common interface. The Makefile orchestrates the build process and integration of these components.
- Optionally, self.py represents a scripting tool whose role might be to run tests or even bootstrap some of the compiler’s operations.

##### Architectural Patterns
- Modular Compiler Phases: Clearly illustrate the separation of concerns by breaking down the compiler into its logical phases (tokenization, parsing, type checking, and code generation).
- Data flow between components is linear (or slightly iterative during test cycles), showing a “pipeline” of transformation from source code to output.
- Integration of Tests: Include how testing utilities plug into the overall workflow.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Compiler Pipeline
    subgraph "Compiler Pipeline"
        A("Source Input"):::core
        B("Main (main.c)"):::core
        C("Tokenize Module (tokenize.c)"):::core
        D("Parser (parse.c)"):::core
        E("Type Checker (type.c)"):::core
        F("Code Generator (codegen.c)"):::core
        G("Output Code"):::core
    end

    %% Testing Components
    subgraph "Testing"
        H("Test (test.c)"):::test
        I("Test Common (test_common.c)"):::test
    end

    %% Utilities & Configuration
    subgraph "Utilities & Configuration"
        J("Shared Definitions (lion.h)"):::utility
        K("Build (Makefile)"):::config
        L("Code Style (.clang-format)"):::config
        M("Version Control (.gitignore)"):::config
        N("Auxiliary Script (self.py)"):::aux
    end

    %% Compiler Pipeline Flow
    A --> B
    B --> C
    C --> D
    D --> E
    E --> F
    F --> G

    %% Shared Definitions Integration
    J --- B
    J --- C
    J --- D
    J --- E
    J --- F

    %% Build Process Integration
    K --- B

    %% Auxiliary Script Integration
    N --- B

    %% Testing Integration
    B --- H
    H --> I

    %% Class Definitions and Colors
    classDef core fill:#AADDFF,stroke:#333,stroke-width:2px;
    classDef test fill:#FFDDDD,stroke:#333,stroke-width:2px;
    classDef utility fill:#DDFFAA,stroke:#333,stroke-width:2px;
    classDef config fill:#FFDDEE,stroke:#333,stroke-width:2px;
    classDef aux fill:#EEDDFF,stroke:#333,stroke-width:2px;

    %% Click Events
    click B "https://github.com/tgw314/lion/blob/main/main.c"
    click C "https://github.com/tgw314/lion/blob/main/tokenize.c"
    click D "https://github.com/tgw314/lion/blob/main/parse.c"
    click E "https://github.com/tgw314/lion/blob/main/type.c"
    click F "https://github.com/tgw314/lion/blob/main/codegen.c"
    click H "https://github.com/tgw314/lion/blob/main/test.c"
    click I "https://github.com/tgw314/lion/blob/main/test_common.c"
    click J "https://github.com/tgw314/lion/blob/main/lion.h"
    click K "https://github.com/tgw314/lion/tree/main/Makefile"
    click L "https://github.com/tgw314/lion/blob/main/.clang-format"
    click M "https://github.com/tgw314/lion/blob/main/.gitignore"
    click N "https://github.com/tgw314/lion/blob/main/self.py"
```
