# noreplydev/ego architecture

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

## Overview

##### Main Components
- Command-Line Interface (CLI) Module:
- Located in src/commands (e.g. new.rs, run.rs, logo.rs).
- Acts as the entry point when executing “ego” commands.
- Lexer & Parser:
- Found in src/ast (lexer.rs, parser.rs, and the nodes directory under src/ast/nodes).
- The lexer processes the source code into tokens using defined lexer types.
- The parser then consumes these tokens to build an Abstract Syntax Tree (AST) from the different nodes (e.g. binary_expression, function_declaration, if_statement, etc.)
- AST and Semantic Analysis:
- The AST, represented through a variety of node types in src/ast/nodes, forms the structural foundation of the source code.
- Compiler:
- Resides in src/compiler (including bytecode.rs and associated handlers).
- Converts the AST into an intermediate representation (bytecode) and applies handlers for operations such as printing.
- Core Functionality:
- Located in src/core, this includes error handling (error module), runtime types (runtypes), logs, and type handling.
- The "runtypes" submodule (with types such as number, string, boolean, vector, nothing, and trait definitions) encapsulates the types used during execution.
- Runtime (Interpreter):
- Found in src/runtime, particularly in interpreter.rs and scope.rs.
- This part executes the code (or bytecode) using the dynamically built scope and runtime values.
- Optional WASM Integration:
- The presence of src/wasm.rs suggests an alternate runtime or compilation target (possibly for WebAssembly execution).
- Supporting Files & Build Tools:
- Cargo.toml, Cargo.lock are used for managing dependencies and builds.
- Documentation (docs folder) and examples (examples folder) assist with development and usage.

##### Relationships and Interactions
- User Interaction:
- The process begins with a developer using the CLI to create or run a project (e.g., “ego new” or “ego run”) which routes into the commands module.
- Source Code Processing:
- The CLI sends source code to the interpreter.
- The Lexer processes the code, converting it to a stream of tokens.
- The Parser takes these tokens and builds an AST using the nodes defined under src/ast/nodes.
- Code Transformation:
- From the AST, the Compiler module translates the structure into bytecode or another intermediate representation.
- The compilation process involves various handlers (like print.rs in the compiler/handlers directory) to process specific constructs.
- Execution:
- The Interpreter in the Runtime module executes the generated bytecode/intermediate form in a managed scope.
- The Core module plays a role here by managing types, errors, and logging.
- Alternative Execution Paths:
- When using the “-vm” flag, there might be alternative execution or a self-contained virtual machine path.
- WASM integration is available though likely experimental and optional.

##### Architectural Patterns and Key Technologies
- Pipeline Pattern for Language Processing:
- The processing pipeline is clearly split into multiple stages (Lexer → Parser → AST → Compiler → Interpreter).
- Modularity and Separation of Concerns:
- Directories like ast, compiler, core, and runtime show a clear separation between different functionalities.
- Error Handling and Logging:
- The core/error and core/logs modules indicate that runtime robustness and debugging are important.
- CLI-Driven Workflow:
- The commands module is designed to encapsulate various user actions, showing an architecture around command routing.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Main Pipeline
    DEV["Developer"]:::developer
    CLI["CLI Module"]:::cli
    LEXER["Lexer"]:::lexer

    subgraph "Parser & AST"
        PARSER["Parser"]:::parser
        AST["AST Nodes"]:::parser
    end

    subgraph "Compiler"
        COMPILER["Compiler Program"]:::compiler
        HANDLERS["Handlers (e.g., print)"]:::compiler
    end

    RUNTIME["Runtime Interpreter"]:::runtime
    WASM["WASM Integration"]:::wasm
    CORE["Core Functionality"]:::core

    %% Supporting Files & Build Tools
    subgraph "Supporting Files & Build Tools"
        CARGO_TOML["Cargo.toml"]:::support
        CARGO_LOCK["Cargo.lock"]:::support
        DOCS["Docs"]:::support
        EXAMPLES["Examples"]:::support
    end

    %% Connections for main processing pipeline
    DEV -->|"triggers"| CLI
    CLI -->|"initiates"| LEXER
    LEXER -->|"token stream"| PARSER
    PARSER -->|"builds"| AST
    AST -->|"generates IR"| COMPILER
    COMPILER -->|"compiles"| HANDLERS
    HANDLERS -->|"produces bytecode for"| RUNTIME
    COMPILER ---|"alternative target"| WASM
    WASM -->|"executes code"| RUNTIME

    %% Error handling feedback (dashed arrows)
    LEXER -.-> CORE
    PARSER -.-> CORE
    AST -.-> CORE
    COMPILER -.-> CORE
    HANDLERS -.-> CORE
    RUNTIME -.-> CORE
    WASM -.-> CORE

    %% Click Events for Component Mapping
    click CLI "https://github.com/noreplydev/ego/tree/main/src/commands"
    click LEXER "https://github.com/noreplydev/ego/blob/main/src/ast/lexer.rs"
    click PARSER "https://github.com/noreplydev/ego/blob/main/src/ast/parser.rs"
    click AST "https://github.com/noreplydev/ego/tree/main/src/ast/nodes"
    click COMPILER "https://github.com/noreplydev/ego/tree/main/src/compiler"
    click HANDLERS "https://github.com/noreplydev/ego/blob/main/src/compiler/handlers/print.rs"
    click CORE "https://github.com/noreplydev/ego/tree/main/src/core"
    click RUNTIME "https://github.com/noreplydev/ego/tree/main/src/runtime"
    click WASM "https://github.com/noreplydev/ego/blob/main/src/wasm.rs"
    click CARGO_TOML "https://github.com/noreplydev/ego/blob/main/Cargo.toml"
    click CARGO_LOCK "https://github.com/noreplydev/ego/blob/main/Cargo.lock"
    click DOCS "https://github.com/noreplydev/ego/tree/main/docs"
    click EXAMPLES "https://github.com/noreplydev/ego/tree/main/examples"

    %% Styles
    classDef developer fill:#D5F5E3,stroke:#27AE60,color:#1E8449,stroke-width:2px;
    classDef cli fill:#AED6F1,stroke:#1F618D,color:#1F618D,stroke-width:2px;
    classDef lexer fill:#ABEBC6,stroke:#229954,color:#1D8348,stroke-width:2px;
    classDef parser fill:#ABEBC6,stroke:#27AE60,color:#1E8449,stroke-width:2px;
    classDef compiler fill:#F9E79F,stroke:#B7950B,color:#7D6608,stroke-width:2px;
    classDef runtime fill:#D2B4DE,stroke:#6C3483,color:#512E5F,stroke-width:2px;
    classDef core fill:#F5B7B1,stroke:#C0392B,color:#641E16,stroke-width:2px;
    classDef wasm fill:#E8DAEF,stroke:#8E44AD,color:#5B2C6F,stroke-width:2px;
    classDef support fill:#FAD7A0,stroke:#D35400,color:#7D6608,stroke-width:2px;
```
