# bernardo-castilho/calcengine architecture

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

- Interactive diagram: https://gitdiagram.com/bernardo-castilho/calcengine
- Repository: https://github.com/bernardo-castilho/calcengine

## Overview

CalcEngine is a small C# library for parsing and evaluating Excel-like expressions, with extensible functions and variables. Its main workflow is a caller submitting an expression to CalcEngine, which parses it into expression objects that evaluate operators, variables, and registered functions. The repository also includes a DataGridView spreadsheet demo and a calculated-variables example. Source excerpts show parser construction, function registration, and grid behavior; some internal dispatch details and the calculated-variables example are not excerpted, so those links are omitted or marked unverified.

Main entry points: [`Source/CalcEngine/CalcEngine/CalcEngine.cs`](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/CalcEngine.cs) (CalcEngine), [`Source/CalcEngine/CalcEngine/Functions/Statistical.cs`](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Functions/Statistical.cs) (Statistical functions), [`Source/CalcEngineDemo/CalcEngineDemo/Form1.cs`](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngineDemo/CalcEngineDemo/Form1.cs) (Demo form), [`Source/CalcEngine/CalcEngine/Functions/MathTrig.cs`](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Functions/MathTrig.cs) (Math and trig)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Expression runtime"]
  node_engine["CalcEngine<br/>[CalcEngine.cs]"]
  node_expression["Expressions<br/>[Expression.cs]"]
  node_tokens["Tokens<br/>[Token.cs]"]
  node_definitions["Function definitions"]
  node_cache["Expression cache<br/>[ExpressionCache.cs]"]
end

subgraph group_functions["Built-in functions"]
  node_math["Math and trig<br/>[MathTrig.cs]"]
  node_logical["Logical functions<br/>[Logical.cs]"]
  node_stats["Statistical functions<br/>[Statistical.cs]"]
  node_text["Text functions<br/>[Text.cs]"]
  node_tally["Tally calculations<br/>[Tally.cs]"]
  node_extended["Extended tally<br/>[ExtendedTally.cs]"]
end

subgraph group_spreadsheet["Grid demo"]
  node_grid["Formula grid<br/>[DataGridCalc.cs]"]
  node_gridengine["Grid engine"]
  node_menu["Function menu<br/>[FunctionMenu.cs]"]
  node_form["Demo form<br/>[Form1.cs]"]
end

subgraph group_examples["Variable example"]
  node_variables["Calculated variables<br/>[Form1.cs]"]
end

node_caller(("Library caller"))

node_caller -->|"submits expression"| node_engine
node_engine -->|"parses into"| node_expression
node_engine -->|"builds tokens"| node_tokens
node_engine -->|"registers functions"| node_definitions
node_engine -->|"creates cache"| node_cache
node_engine -->|"registers functions"| node_math
node_engine -->|"registers functions"| node_logical
node_engine -->|"registers functions"| node_stats
node_engine -->|"registers functions"| node_text
node_stats -->|"accumulates values"| node_tally
node_math -->|"accumulates values"| node_tally
node_stats -->|"uses tally helpers"| node_extended
node_grid -->|"evaluates formulas"| node_gridengine
node_gridengine -->|"extends engine"| node_engine
node_form -->|"uses grid"| node_grid
node_form -->|"uses menu"| node_menu
node_variables -.->|"uses engine"| node_engine

click node_engine "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/CalcEngine.cs"
click node_expression "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Expression.cs"
click node_tokens "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Token.cs"
click node_definitions "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/FunctionDefinition.cs"
click node_cache "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/ExpressionCache.cs"
click node_math "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Functions/MathTrig.cs"
click node_logical "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Functions/Logical.cs"
click node_stats "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Functions/Statistical.cs"
click node_text "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Functions/Text.cs"
click node_tally "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Functions/Tally.cs"
click node_extended "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Functions/ExtendedTally.cs"
click node_grid "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngineDemo/CalcEngineDemo/DataGridCalc.cs"
click node_gridengine "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngineDemo/CalcEngineDemo/DataGridCalcEngine.cs"
click node_menu "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngineDemo/CalcEngineDemo/FunctionMenu.cs"
click node_form "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngineDemo/CalcEngineDemo/Form1.cs"
click node_variables "https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalculatedVariables/CalculatedVariables/Form1.cs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_engine,node_expression,node_tokens,node_definitions,node_cache toneBlue
class node_math,node_logical,node_stats,node_text,node_tally,node_extended toneAmber
class node_grid,node_gridengine,node_menu,node_form toneMint
class node_variables toneRose
class node_caller toneIndigo
```

## Components

### Expression runtime

- [CalcEngine](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/CalcEngine.cs): `Source/CalcEngine/CalcEngine/CalcEngine.cs`
- [Expressions](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Expression.cs): `Source/CalcEngine/CalcEngine/Expression.cs`
- [Tokens](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Token.cs): `Source/CalcEngine/CalcEngine/Token.cs`
- [Function definitions](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/FunctionDefinition.cs): `Source/CalcEngine/CalcEngine/FunctionDefinition.cs`
- [Expression cache](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/ExpressionCache.cs): `Source/CalcEngine/CalcEngine/ExpressionCache.cs`

### Built-in functions

- [Math and trig](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Functions/MathTrig.cs): `Source/CalcEngine/CalcEngine/Functions/MathTrig.cs`
- [Logical functions](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Functions/Logical.cs): `Source/CalcEngine/CalcEngine/Functions/Logical.cs`
- [Statistical functions](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Functions/Statistical.cs): `Source/CalcEngine/CalcEngine/Functions/Statistical.cs`
- [Text functions](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Functions/Text.cs): `Source/CalcEngine/CalcEngine/Functions/Text.cs`
- [Tally calculations](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Functions/Tally.cs): `Source/CalcEngine/CalcEngine/Functions/Tally.cs`
- [Extended tally](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngine/CalcEngine/Functions/ExtendedTally.cs): `Source/CalcEngine/CalcEngine/Functions/ExtendedTally.cs`

### Grid demo

- [Formula grid](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngineDemo/CalcEngineDemo/DataGridCalc.cs): `Source/CalcEngineDemo/CalcEngineDemo/DataGridCalc.cs`
- [Grid engine](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngineDemo/CalcEngineDemo/DataGridCalcEngine.cs): `Source/CalcEngineDemo/CalcEngineDemo/DataGridCalcEngine.cs`
- [Function menu](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngineDemo/CalcEngineDemo/FunctionMenu.cs): `Source/CalcEngineDemo/CalcEngineDemo/FunctionMenu.cs`
- [Demo form](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalcEngineDemo/CalcEngineDemo/Form1.cs): `Source/CalcEngineDemo/CalcEngineDemo/Form1.cs`

### Variable example

- [Calculated variables](https://github.com/bernardo-castilho/calcengine/blob/master/Source/CalculatedVariables/CalculatedVariables/Form1.cs): `Source/CalculatedVariables/CalculatedVariables/Form1.cs`

### Other components

- **Library caller**

## Connections

- Library caller → CalcEngine: submits expression (evidence: `README.md`)
- CalcEngine → Expressions: parses into (evidence: `Source/CalcEngine/CalcEngine/CalcEngine.cs`)
- CalcEngine → Tokens: builds tokens (evidence: `Source/CalcEngine/CalcEngine/CalcEngine.cs`)
- CalcEngine → Function definitions: registers functions (evidence: `Source/CalcEngine/CalcEngine/CalcEngine.cs`)
- CalcEngine → Expression cache: creates cache (evidence: `Source/CalcEngine/CalcEngine/CalcEngine.cs`)
- CalcEngine → Math and trig: registers functions (evidence: `Source/CalcEngine/CalcEngine/CalcEngine.cs`)
- CalcEngine → Logical functions: registers functions (evidence: `Source/CalcEngine/CalcEngine/CalcEngine.cs`)
- CalcEngine → Statistical functions: registers functions (evidence: `Source/CalcEngine/CalcEngine/CalcEngine.cs`)
- CalcEngine → Text functions: registers functions (evidence: `Source/CalcEngine/CalcEngine/CalcEngine.cs`)
- Statistical functions → Tally calculations: accumulates values (evidence: `Source/CalcEngine/CalcEngine/Functions/Statistical.cs`)
- Math and trig → Tally calculations: accumulates values (evidence: `Source/CalcEngine/CalcEngine/Functions/MathTrig.cs`)
- Statistical functions → Extended tally: uses tally helpers (evidence: `Source/CalcEngine/CalcEngine/Functions/Statistical.cs`)
- Formula grid → Grid engine: evaluates formulas (evidence: `Source/CalcEngineDemo/CalcEngineDemo/DataGridCalc.cs`)
- Grid engine → CalcEngine: extends engine (evidence: `Source/CalcEngineDemo/CalcEngineDemo/DataGridCalcEngine.cs`)
- Demo form → Formula grid: uses grid (evidence: `Source/CalcEngineDemo/CalcEngineDemo/Form1.cs`)
- Demo form → Function menu: uses menu (evidence: `Source/CalcEngineDemo/CalcEngineDemo/Form1.cs`)
- Calculated variables → CalcEngine: uses engine (evidence: `Source/CalculatedVariables/CalculatedVariables/Form1.cs`)
