# 61131/echidna architecture

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

## Overview

##### Main Components

a. Compiler Frontend
- Lexer (src/lexer.l)
- Parser (src/grammar.y)
- Symbol Table (src/symbol.c)
- Abstract Syntax Tree (src/tree.c)

b. Compiler Backend
- Bytecode Generator (src/bytecode/)
- Code Optimization
- Binary Output Generation

c. Virtual Machine Runtime
- Task Scheduler (src/runtime/rt_task.c)
- Context Management (src/runtime/rt_context.c)
- Function Execution (src/runtime/rt_function.c)
- Error Handling (src/runtime/rt_error.c)

d. Standard Library
- Arithmetic Operations (src/standard/std_arithmetic.c)
- Bitwise Operations (src/standard/std_bitwise.c)
- Timer Functions (src/standard/std_timers.c)
- String Operations (src/standard/std_string.c)

e. Hardware Interface
- PiFace Digital I/O (src/hardware/piface.c)
- Hardware Abstraction Layer

##### Data Flow

Key interactions:

a. Compilation Phase:
- Source code → Lexer → Parser → AST → Bytecode
- Symbol table interactions
- Configuration file processing

b. Runtime Phase:
- Bytecode loading
- Task scheduling
- Function execution
- Hardware I/O operations

##### External Dependencies

Integration points for:
- libev (Event loop)
- libconfig (Configuration processing)
- protobuf-c (Data serialization)
- Hardware drivers

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Legend
        L1[Component]:::component
        L2([Process]):::process
        L3[(Storage)]:::storage
        L4{{Interface}}:::interface
        L5[/External Dependency/]:::external
    end

    subgraph CompilerFrontend
        SourceCode([Source Code]):::process
        Lexer([Lexer]):::process
        Parser([Parser]):::process
        SymbolTable[(Symbol Table)]:::storage
        AST([Abstract Syntax Tree]):::process
        
        SourceCode --> Lexer
        Lexer --> Parser
        Parser --> AST
        Parser <--> SymbolTable
    end

    subgraph CompilerBackend
        BytecodeGen([Bytecode Generator]):::process
        StackMgmt([Stack Management]):::process
        LabelMgmt([Label Management]):::process
        BinaryOut[(Binary Output)]:::storage
        
        AST --> BytecodeGen
        BytecodeGen <--> StackMgmt
        BytecodeGen <--> LabelMgmt
        BytecodeGen --> BinaryOut
    end

    subgraph VMRuntime
        TaskSched([Task Scheduler]):::process
        ContextMgmt([Context Management]):::process
        FuncExec([Function Execution]):::process
        ErrorHandler([Error Handler]):::process
        
        BinaryOut --> TaskSched
        TaskSched <--> ContextMgmt
        ContextMgmt <--> FuncExec
        FuncExec <--> ErrorHandler
    end

    subgraph StandardLib
        ArithOps[Arithmetic Ops]:::component
        BitwiseOps[Bitwise Ops]:::component
        TimerFuncs[Timer Functions]:::component
        StringOps[String Operations]:::component
        TimeFuncs[Time Functions]:::component
        CounterFuncs[Counter Functions]:::component
    end

    subgraph HardwareInterface
        PiFace{{PiFace Digital I/O}}:::interface
        HWCore{{Hardware Core}}:::interface
        HAL[Hardware Abstraction Layer]:::component
    end

    subgraph ExternalDeps
        LibEv[/libev/]:::external
        LibConfig[/libconfig/]:::external
        ProtoBuf[/protobuf-c/]:::external
    end

    FuncExec --> StandardLib
    StandardLib --> HAL
    HAL --> PiFace
    HAL --> HWCore
    TaskSched --> LibEv
    ContextMgmt --> LibConfig
    FuncExec --> ProtoBuf

    %% Component Mappings
    click Lexer "https://github.com/61131/echidna/blob/master/src/lexer.l"
    click Parser "https://github.com/61131/echidna/blob/master/src/grammar.y"
    click SymbolTable "https://github.com/61131/echidna/blob/master/src/symbol.c"
    click AST "https://github.com/61131/echidna/blob/master/src/tree.c"
    click BytecodeGen "https://github.com/61131/echidna/blob/master/src/bytecode/generate.c"
    click StackMgmt "https://github.com/61131/echidna/blob/master/src/bytecode/stack.c"
    click LabelMgmt "https://github.com/61131/echidna/blob/master/src/bytecode/label.c"
    click TaskSched "https://github.com/61131/echidna/blob/master/src/runtime/rt_task.c"
    click ContextMgmt "https://github.com/61131/echidna/blob/master/src/runtime/rt_context.c"
    click FuncExec "https://github.com/61131/echidna/blob/master/src/runtime/rt_function.c"
    click ErrorHandler "https://github.com/61131/echidna/blob/master/src/runtime/rt_error.c"
    click ArithOps "https://github.com/61131/echidna/blob/master/src/standard/std_arithmetic.c"
    click BitwiseOps "https://github.com/61131/echidna/blob/master/src/standard/std_bitwise.c"
    click TimerFuncs "https://github.com/61131/echidna/blob/master/src/standard/std_timers.c"
    click StringOps "https://github.com/61131/echidna/blob/master/src/standard/std_string.c"
    click TimeFuncs "https://github.com/61131/echidna/blob/master/src/standard/std_time.c"
    click CounterFuncs "https://github.com/61131/echidna/blob/master/src/standard/std_counters.c"
    click PiFace "https://github.com/61131/echidna/blob/master/src/hardware/piface.c"
    click HWCore "https://github.com/61131/echidna/blob/master/src/hardware.c"

    classDef component fill:#f9f,stroke:#333,stroke-width:2px
    classDef process fill:#bbf,stroke:#333,stroke-width:2px
    classDef storage fill:#ffd,stroke:#333,stroke-width:2px
    classDef interface fill:#bfb,stroke:#333,stroke-width:2px
    classDef external fill:#ddd,stroke:#333,stroke-width:2px
```
