# open2b/scriggo architecture

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

- Interactive diagram: https://gitdiagram.com/open2b/scriggo
- Repository: https://github.com/open2b/scriggo

## Overview

##### Main Components

- Compiler Module (internal/compiler/*)
- Runtime Module (internal/runtime/*)
- AST Module (ast/*)
- Native Interface (native/*)
- Built-in Functions (builtin/*)

b. Supporting Components:
- Template Engine
- Program Interpreter
- File System Interface

##### Architecture Layers (top to bottom)

Layer 1 - Public API:
- Template API
- Program API
- Native Interface API

Layer 2 - Processing:
- Compiler
- Parser
- Type Checker
- Code Generator
- Runtime Environment
- VM
- Memory Management
- Error Handling

Layer 3 - Core Infrastructure:
- AST Implementation
- Built-in Functions
- File System Abstraction

a. Data Flow:
- Source Code → Parser → AST → Type Checker → Code Generator → Runtime
- Templates → Template Engine → Compiler → Runtime
- Native Code ↔ Runtime Environment

b. Dependencies:
- Template Engine → File System
- Compiler → AST
- Runtime → Built-in Functions

##### Special Elements

a. Security Boundaries:
- Isolation between user code and native code
- Package access controls

b. Extension Points:
- Native package integration
- Custom built-in functions
- File system implementation

##### Implementation Details

- Rectangles: Major components
- Rounded rectangles: Interfaces
- Hexagons: Security boundaries
- Diamonds: Decision points

- Blue: Core components
- Green: Public APIs
- Yellow: Security features
- Gray: Infrastructure
- Red: Critical paths

##### Additional Notes

Callouts for:
- Template inheritance system
- Markdown support
- Security features
- Performance optimization points

##### Runtime execution flow

Remember to include important interfaces like:
- scriggo.Build()
- scriggo.BuildTemplate()
- native.Declarations
- builtin package interfaces

This will create a comprehensive visualization of Scriggo's architecture while maintaining clarity and usefulness for technical discussions.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Public API Layer"
        TemplateAPI["Template API"]:::api
        ProgramAPI["Program API"]:::api
        NativeAPI["Native Interface API"]:::api
    end

    subgraph "Processing Layer"
        subgraph "Compiler"
            Parser["Parser"]:::compiler
            TypeChecker["Type Checker"]:::compiler
            CodeGen["Code Generator"]:::compiler
        end

        subgraph "Runtime Environment"
            VM["Virtual Machine"]:::runtime
            MemMgmt["Memory Management"]:::runtime
            ErrorHandler["Error Handling"]:::runtime
        end
    end

    subgraph "Core Infrastructure"
        AST["AST Implementation"]:::core
        BuiltIn["Built-in Functions"]:::core
        FS["File System Abstraction"]:::core
    end

    %% Template Flow
    TemplateAPI --> Parser
    Parser --> AST
    AST --> TypeChecker
    TypeChecker --> CodeGen
    CodeGen --> VM

    %% Program Flow
    ProgramAPI --> Parser
    NativeAPI <--> VM

    %% Dependencies
    TemplateAPI --> FS
    VM --> BuiltIn
    ErrorHandler --> BuiltIn
    MemMgmt --> VM

    %% Click Events
    click Parser "https://github.com/open2b/scriggo/blob/main//internal/compiler/parser.go"
    click TypeChecker "https://github.com/open2b/scriggo/blob/main//internal/compiler/checker.go"
    click CodeGen "https://github.com/open2b/scriggo/blob/main//internal/compiler/emitter.go"
    click VM "https://github.com/open2b/scriggo/blob/main//internal/runtime/vm.go"
    click ErrorHandler "https://github.com/open2b/scriggo/blob/main//internal/runtime/errors.go"
    click MemMgmt "https://github.com/open2b/scriggo/blob/main//internal/runtime/env.go"
    click AST "https://github.com/open2b/scriggo/blob/main//ast/ast.go"
    click NativeAPI "https://github.com/open2b/scriggo/blob/main//native/native.go"
    click BuiltIn "https://github.com/open2b/scriggo/blob/main//builtin/builtin.go"
    click FS "https://github.com/open2b/scriggo/blob/main//files.go"

    %% Styles
    classDef api fill:#90EE90,stroke:#333,stroke-width:2px
    classDef compiler fill:#ADD8E6,stroke:#333,stroke-width:2px
    classDef runtime fill:#FFB6C1,stroke:#333,stroke-width:2px
    classDef core fill:#DDA0DD,stroke:#333,stroke-width:2px

    %% Legend
    subgraph Legend
        API[Public APIs]:::api
        COMP[Compiler Components]:::compiler
        RT[Runtime Components]:::runtime
        CORE[Core Components]:::core
    end
```
