# mparlak/flee architecture

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

- Interactive diagram: https://gitdiagram.com/mparlak/flee
- Repository: https://github.com/mparlak/flee

## Overview

##### Project overview and purpose
- Flee is a library designed to parse, compile, and evaluate mathematical and logical expressions at runtime. It translates expressions into IL (Intermediate Language) using a custom compiler and code generation technique, ensuring high-performance evaluation.

##### Main components
a. Parsing Subsystem:
- Located in the src/Flee/Parsing directory.
- Responsible for tokenizing strings, building syntactic/grammatical representations, and processing production patterns.
- Key elements include classes like ExpressionTokenizer, ExpressionParser, RecursiveDescentParser, Token*, and related exception handling components.

b. Expression Elements:
- Found in src/Flee/ExpressionElements and its subdirectories.
- Contains various classes representing parts of an expression, such as arithmetic operations, literals (integral, real, string, boolean, etc.), binary/unary expressions, member access, casts, comparisons, and logical/bitwise operations.
- This layer forms the abstract syntax tree (AST) or expression tree used throughout the system.

c. Calculation Engine:
- Components in src/Flee/CalcEngine (for example, CalculationEngine, SimpleCalcEngine, BatchLoader) show how expression evaluations can be batched or executed in a controlled environment.
- This layer uses the expression trees built earlier and executes them, possibly taking advantage of caching and dynamic IL code generation.

d. Internal/Utility Components:
- Modules under src/Flee/InternalTypes provide additional support for IL generation (e.g., FleeILGenerator), dependency management, implicit conversions, and miscellaneous utility functions.
- They bridge between the high-level expression representation and the low-level code generation aspects.

e. Public API:
- The src/Flee/PublicTypes directory exposes the library’s surface. These types (like ExpressionContext, ExpressionImports, ExpressionOptions, VariableCollection) allow consumers to interact with the Flee engine.
- This layer is critical for external integration and is what client applications will use.

f. Resources:
- Located in src/Flee/Resources, these manage error messages, documentation comments, and resource keys.
- While not core to the evaluation engine’s function, they are essential for developer experience and debugging.

g. Testing Infrastructure:
- Found in the test directory (Flee.Test), these include tests for the CalcEngine, Expression tests, and extension methods.
- They ensure that any component interaction (e.g., between parsing and evaluation) functions as expected.

##### Relationships and interactions
- The Parsing Subsystem processes the raw expression strings to produce token/AST output.
- The Expression Elements component then uses this AST to create a structured object model representing the expression.
- The Calculation Engine (via public interfaces) takes these expression trees and compiles them into IL. Internal utilities ensure that IL code generation and dependency management are handled efficiently.
- The Public API wraps these inner operations, exposing methods for creating expression contexts, importing types, and running the evaluations.
- Data flow flows from input (raw strings) through parsing, expression tree construction, IL generation, and then evaluation.
- Each module is decoupled via clear interfaces, allowing unit testing and potential future substitutions (e.g., different parsers or expression element implementations).

##### Architectural patterns and design principles
- Layered Architecture: The project is structured in layers – from parsing, abstract syntax tree (AST) generation, intermediate IL code generation, to evaluation.
- Separation of Concerns: Distinct modules take responsibility for parsing (grammar handling), expression representation (element trees), and evaluation (calculation engines).
- Extensibility/Plugability: Public interfaces allow clients to interact and extend the evaluation process (e.g., custom operators, extension methods).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Input & Parsing Layer
    subgraph "Input & Parsing Layer"
        IE("Input Expression")
        PS("Parsing Subsystem")
    end

    %% Expression & Calculation Layer
    subgraph "Expression & Calculation"
        EE("Expression Elements")
        CE("Calculation Engine")
    end

    %% Support Layer
    subgraph "Support Layer"
        IU("Internal Utilities")
        R("Resources")
    end

    %% External Interface
    subgraph "External Interface"
        PA("Public API")
    end

    %% Testing Infrastructure
    subgraph "Testing"
        TI("Testing Infrastructure")
    end

    %% Connections: Data Flow and Interactions
    IE -->|"parses"| PS
    PS -->|"produces_AST"| EE
    EE -->|"builds_expression_tree"| CE
    CE -->|"compiles_and_executes"| PA
    PA -->|"exposes_functionality"| CE

    %% Internal utilities support core processing components
    IU ---|"supports"| EE
    IU ---|"supports"| CE

    %% Resources supporting developer experience
    R ---|"provides_resources"| PA

    %% Testing connections ensuring quality
    TI ---|"validates"| PS
    TI ---|"validates"| EE
    TI ---|"validates"| CE

    %% Usage annotation
    PA ---|"Usage: Create ExpressionContext & Evaluate"| IE

    %% Click Events mapping to file tree paths
    click PS "https://github.com/mparlak/flee/tree/master/src/Flee/Parsing"
    click EE "https://github.com/mparlak/flee/tree/master/src/Flee/ExpressionElements"
    click CE "https://github.com/mparlak/flee/tree/master/src/Flee/CalcEngine"
    click IU "https://github.com/mparlak/flee/tree/master/src/Flee/InternalTypes"
    click PA "https://github.com/mparlak/flee/tree/master/src/Flee/PublicTypes"
    click R "https://github.com/mparlak/flee/tree/master/src/Flee/Resources"
    click TI "https://github.com/mparlak/flee/blob/master/test/Flee.Test"

    %% Styles: Color Coding
    class PS,EE,CE core
    class IU,R support
    class PA external
    class TI test

    %% Class Definitions
    classDef core fill:#ffcccb,stroke:#000,stroke-width:1px;
    classDef support fill:#ccccff,stroke:#000,stroke-width:1px;
    classDef external fill:#ccffcc,stroke:#000,stroke-width:1px;
    classDef test fill:#ffff99,stroke:#000,stroke-width:1px;
```
