# killme2008/aviatorscript architecture

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

- Interactive diagram: https://gitdiagram.com/killme2008/aviatorscript
- Repository: https://github.com/killme2008/aviatorscript

## Overview

##### Project Type and Purpose
- AviatorScript is an open-source, JVM-hosted scripting language implementation (interpreter + compiler) written in Java.
- Its core purpose is to parse, compile (via ASM), interpret, and execute user scripts, expose a javax.script API, and offer a CLI for ad hoc evaluation.

##### Key Architectural Layers
a. CLI / Shell Layer
- `bin/aviator` shell script
- Downloads or locates the `aviator-{version}.jar` in `~/.aviatorscript`
- Invokes `java com.googlecode.aviator.Main [options]`
b. Core Engine (JAR)
1. Frontend (Lexing & Parsing)
- `lexer/ExpressionLexer`
- `parser/ExpressionParser` → produces AST / CompileType info
2. Intermediate Representation (IR)
- `code/interpreter/ir/*IR` classes model an instruction stream
3. Compiler / Code Generation
- ASM-based bytecode generator (`code/asm/ASMCodeGenerator` + `code/CodeGenerator` variants)
- Dynamic class definition via `ClassDefiner`
4. Interpreter (fallback)
- `InterpretExpression` + `InterpretCodeGenerator` + `InterpretContext`
5. Evaluator API
- `AviatorEvaluator` / `AviatorEvaluatorInstance` — entry points for evaluate/compile/execute
- Options: `EvalMode` toggles ASM vs. interpretation, timeout, sandbox settings
6. Runtime Environment
- `runtime` package: context, function dispatch, bindings, Java reflection fallback
- `runtime/function/*` and `runtime/function/math|seq|string|system/*` – built-in functions
- `runtime/type/*` – Aviator data types (AviatorNumber, AviatorString, AviatorObject…)
- `runtime/module/*` – module loaders (IoModule, etc.)
7. Annotation-driven Extension
- `annotation/@Function`, `FunctionLoader`, `ClassPathConfigFunctionLoader`, `SpringContextFunctionLoader`
8. Serialization & ScriptEngine
- `serialize/AviatorObjectInputStream` / `OutputStream` for script caching
- `script/AviatorScriptEngineFactory` / `AviatorScriptEngine` for JSR-223 integration
c. Testing & Examples
- `examples/*.av`, `tests/` – unit tests demonstrating features, quality gating via GitHub Actions/Maven.

##### Component Relationships & Data Flow
- CLI passes script or expression to `Main` → selects mode (compile/interpret) → calls `AviatorEvaluatorInstance`.
- EvaluatorInstance:
→ Lexer → Parser → AST
→ either pass AST to CodeGenerator (ASM) or to InterpretCodeGenerator
→ produced bytecode class is loaded, then invoked, returning AviatorObject
→ Runtime handles function dispatch, type conversion, error handling
- ScriptEngine API wraps Evaluator, exposes javax.script.* interfaces for host integration.
- Serialization layer can snapshot compiled bytecode for reuse.

##### Architectural Patterns & Principles
- Pluggable function loader via annotations (inversion of control)
- Strategy pattern for code generation (multiple `CodeGenerator` implementations)
- Interpreter pattern for IR execution
- Builder/factory for `AviatorEvaluatorInstance` configuration (EvalMode, sandbox)
- Separation of concerns: lexer, parser, IR, compiler, runtime, API layers.

##### Technologies & Frameworks
- Java 8+
- ASM bytecode framework (`com.googlecode.aviator.asm`)
- javax.script API (JSR-223)
- Maven build, GitHub Actions / Travis CI for CI
- Optional Spring integration via `spring/*` loaders.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients
    subgraph "Clients"
        direction TB
        CLI["CLI Shell\n(bin/aviator)"]:::clients
        JSR["JSR-223 Host\n(AviatorScriptEngine)"]:::clients
        CLI --> Main
        JSR --> Main
    end

    %% API Layer
    subgraph "API"
        direction TB
        Main["Main / AviatorEvaluatorInstance"]:::api
    end

    Main --> Frontend

    %% Frontend Layer
    subgraph "Frontend" 
        direction TB
        Lex["ExpressionLexer"]:::frontend
        Par["ExpressionParser"]:::frontend
        Lex --> Par
    end

    Frontend --> AST

    AST["IR / AST"]:::ir

    AST --> ASM
    AST --> ICGen

    %% Compiler / IR Layer
    subgraph "Compiler / IR"
        direction TB
        ASM["ASMCodeGenerator"]:::compiler
        CG["CodeGenerator"]:::compiler
        CD["ClassDefiner"]:::compiler
        ASM --> CG
        CG --> CD
    end

    %% Interpreter Layer
    subgraph "Interpreter"
        direction TB
        ICGen["InterpretCodeGenerator"]:::compiler
        IE["InterpretExpression"]:::compiler
        ICtx["InterpretContext"]:::compiler
        ICGen --> IE
        IE --> ICtx
    end

    CD --> Runtime
    ICCtx([InterpretContext]) --> Runtime
    click ICtx "https://github.com/killme2008/aviatorscript/blob/master/src/main/java/com/googlecode/aviator/code/interpreter/InterpretContext.java"

    ICGen -->|uses|FrontEnd
    click ICGen "https://github.com/killme2008/aviatorscript/blob/master/src/main/java/com/googlecode/aviator/code/interpreter/InterpretCodeGenerator.java"
    click IE "https://github.com/killme2008/aviatorscript/blob/master/src/main/java/com/googlecode/aviator/InterpretExpression.java"

    %% Runtime Layer
    subgraph "Runtime Engine"
        direction TB
        RU["RuntimeUtils"]:::runtime
        RF["runtime/function"]:::runtime
        RT["runtime/type"]:::runtime
        RM["IoModule"]:::runtime
    end

    RU & RF & RT & RM --> RuntimeExec

    RuntimeExec["JVM Execution / Runtime\n- Type System\n- Function Dispatch\n- Module System"]:::runtime

    %% Data Flow to Outputs
    RuntimeExec --> CLIOut["CLI Output"]:::clients
    RuntimeExec --> JSR

    %% Optional Serialization Store
    Store[(Serialized Script Store)]:::storage
    Main -.-> Store
    CD -.-> Store

    %% Lexing & Parsing connections
    Frontend --> Lex
    click Lex "https://github.com/killme2008/aviatorscript/blob/master/src/main/java/com/googlecode/aviator/lexer/ExpressionLexer.java"
    click Par "https://github.com/killme2008/aviatorscript/blob/master/src/main/java/com/googlecode/aviator/parser/ExpressionParser.java"

    %% CLI click
    click CLI "https://github.com/killme2008/aviatorscript/tree/master/bin/aviator"

    %% Compiler click events
    click ASM "https://github.com/killme2008/aviatorscript/blob/master/src/main/java/com/googlecode/aviator/code/asm/ASMCodeGenerator.java"
    click CG "https://github.com/killme2008/aviatorscript/blob/master/src/main/java/com/googlecode/aviator/code/CodeGenerator.java"
    click CD "https://github.com/killme2008/aviatorscript/blob/master/src/main/java/com/googlecode/aviator/code/asm/ClassDefiner.java"

    %% API click events
    click Main "https://github.com/killme2008/aviatorscript/blob/master/src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java"

    %% Runtime click events
    click RU "https://github.com/killme2008/aviatorscript/blob/master/src/main/java/com/googlecode/aviator/runtime/RuntimeUtils.java"
    click RF "https://github.com/killme2008/aviatorscript/tree/master/src/main/java/com/googlecode/aviator/runtime/function/"
    click RT "https://github.com/killme2008/aviatorscript/tree/master/src/main/java/com/googlecode/aviator/runtime/type/"
    click RM "https://github.com/killme2008/aviatorscript/blob/master/src/main/java/com/googlecode/aviator/runtime/module/IoModule.java"

    classDef clients fill:#b3cde3,stroke:#333,stroke-width:1px
    classDef api fill:#6497b1,stroke:#333,stroke-width:1px
    classDef frontend fill:#fdfd96,stroke:#333,stroke-width:1px
    classDef ir fill:#ffb347,stroke:#333,stroke-width:1px
    classDef compiler fill:#ff6961,stroke:#333,stroke-width:1px
    classDef runtime fill:#77dd77,stroke:#333,stroke-width:1px
    classDef storage fill:#cfcfc4,stroke:#333,stroke-width:1px
```
