# tenri69/tnsrlang architecture

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

- Interactive diagram: https://gitdiagram.com/tenri69/tnsrlang
- Repository: https://github.com/tenri69/tnsrlang

## Overview

TnsrLang is a C++17 DSL interpreter for tensor computation and neural-network workflows. A script runs from source text through tokenization, parsing into an AST, and tree-walk evaluation; native tensor operations provide AutoGrad and model-training primitives. The CLI also supports AST visualization and neural-network diagnostics, while server mode serves browser tools and executes submitted scripts. README capabilities include model persistence and the web MNIST canvas. Some implementation details, especially persistence and HTTP UI wiring, are documented but were not sampled directly.

Main entry points: [`src/Evaluator.cpp`](https://github.com/tenri69/tnsrlang/blob/main/src/Evaluator.cpp) (Tree-walk evaluator), [`src/main.cpp`](https://github.com/tenri69/tnsrlang/blob/main/src/main.cpp) (CLI entrypoint), [`src/httplib.h`](https://github.com/tenri69/tnsrlang/blob/main/src/httplib.h) (HTTP server), [`public`](https://github.com/tenri69/tnsrlang/tree/main/public) (Browser terminal and canvas)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_language["Language Front End"]
  node_lexer["Lexer<br/>[Lexer.cpp]"]
  node_parser["Parser<br/>[Parser.cpp]"]
  node_ast["Syntax tree<br/>[AST.cpp]"]
  node_ast_printer["AST visualizer<br/>[ASTPrinter.cpp]"]
end

subgraph group_runtime["Execution Runtime"]
  node_evaluator["Tree-walk evaluator<br/>[Evaluator.cpp]"]
  node_environment["Scopes and functions<br/>[Evaluator.h]"]
  node_stdlib["Native functions<br/>[Evaluator.cpp]"]
  node_persistence["Model persistence<br/>[Evaluator.cpp]"]
end

subgraph group_tensor["Tensor and Learning"]
  node_math["Tensor operations<br/>[MathEngine.cpp]"]
  node_autograd["AutoGrad graph<br/>[MathEngine.cpp]"]
  node_training["NN training primitives<br/>[MathEngine.cpp]"]
  node_diagnostics["NN diagnostics<br/>[Evaluator.cpp]"]
end

subgraph group_interfaces["User Interfaces"]
  node_cli["CLI entrypoint<br/>[main.cpp]"]
  node_http_server["HTTP server<br/>[httplib.h]"]
  node_web_ui["Browser terminal and canvas"]
end

node_script_author(("Script author"))
node_source["TnsrLang source"]
node_browser_user(("Browser user"))
node_model_files[("Saved model files")]

node_script_author -->|"writes"| node_source
node_source -->|"runs"| node_cli
node_cli -->|"tokenizes with"| node_lexer
node_lexer -->|"tokens to"| node_parser
node_parser -->|"builds"| node_ast
node_cli -->|"parses with"| node_parser
node_cli -->|"evaluates with"| node_evaluator
node_evaluator -->|"uses scopes"| node_environment
node_evaluator -->|"registers natives"| node_stdlib
node_stdlib -->|"invokes operations"| node_math
node_evaluator -->|"backpropagates"| node_autograd
node_math -->|"records gradients"| node_autograd
node_stdlib -->|"exposes"| node_training
node_evaluator -->|"checks tensors"| node_diagnostics
node_cli -->|"optionally dumps"| node_ast_printer
node_evaluator -.->|"saves and loads"| node_persistence
node_persistence -.->|"reads and writes"| node_model_files
node_browser_user -->|"uses"| node_web_ui
node_cli -->|"starts server"| node_http_server
node_http_server -->|"serves assets"| node_web_ui
node_web_ui -->|"submits scripts"| node_http_server
node_http_server -->|"executes scripts"| node_evaluator
node_web_ui -.->|"requests inference"| node_training

click node_cli "https://github.com/tenri69/tnsrlang/blob/main/src/main.cpp"
click node_lexer "https://github.com/tenri69/tnsrlang/blob/main/src/Lexer.cpp"
click node_parser "https://github.com/tenri69/tnsrlang/blob/main/src/Parser.cpp"
click node_ast "https://github.com/tenri69/tnsrlang/blob/main/src/AST.cpp"
click node_ast_printer "https://github.com/tenri69/tnsrlang/blob/main/src/ASTPrinter.cpp"
click node_evaluator "https://github.com/tenri69/tnsrlang/blob/main/src/Evaluator.cpp"
click node_environment "https://github.com/tenri69/tnsrlang/blob/main/src/Evaluator.h"
click node_stdlib "https://github.com/tenri69/tnsrlang/blob/main/src/Evaluator.cpp"
click node_math "https://github.com/tenri69/tnsrlang/blob/main/src/MathEngine.cpp"
click node_autograd "https://github.com/tenri69/tnsrlang/blob/main/src/MathEngine.cpp"
click node_training "https://github.com/tenri69/tnsrlang/blob/main/src/MathEngine.cpp"
click node_diagnostics "https://github.com/tenri69/tnsrlang/blob/main/src/Evaluator.cpp"
click node_persistence "https://github.com/tenri69/tnsrlang/blob/main/src/Evaluator.cpp"
click node_http_server "https://github.com/tenri69/tnsrlang/blob/main/src/httplib.h"
click node_web_ui "https://github.com/tenri69/tnsrlang/tree/main/public"

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_lexer,node_parser,node_ast,node_ast_printer,node_browser_user toneBlue
class node_evaluator,node_environment,node_stdlib,node_persistence,node_model_files toneAmber
class node_math,node_autograd,node_training,node_diagnostics toneMint
class node_cli,node_http_server,node_web_ui toneRose
class node_script_author,node_source toneIndigo
```

## Components

### Language Front End

- [Lexer](https://github.com/tenri69/tnsrlang/blob/main/src/Lexer.cpp): `src/Lexer.cpp`
- [Parser](https://github.com/tenri69/tnsrlang/blob/main/src/Parser.cpp): `src/Parser.cpp`
- [Syntax tree](https://github.com/tenri69/tnsrlang/blob/main/src/AST.cpp): `src/AST.cpp`
- [AST visualizer](https://github.com/tenri69/tnsrlang/blob/main/src/ASTPrinter.cpp): `src/ASTPrinter.cpp`

### Execution Runtime

- [Tree-walk evaluator](https://github.com/tenri69/tnsrlang/blob/main/src/Evaluator.cpp): `src/Evaluator.cpp`
- [Scopes and functions](https://github.com/tenri69/tnsrlang/blob/main/src/Evaluator.h): `src/Evaluator.h`
- [Native functions](https://github.com/tenri69/tnsrlang/blob/main/src/Evaluator.cpp): `src/Evaluator.cpp`
- [Model persistence](https://github.com/tenri69/tnsrlang/blob/main/src/Evaluator.cpp): `src/Evaluator.cpp`

### Tensor and Learning

- [Tensor operations](https://github.com/tenri69/tnsrlang/blob/main/src/MathEngine.cpp): `src/MathEngine.cpp`
- [AutoGrad graph](https://github.com/tenri69/tnsrlang/blob/main/src/MathEngine.cpp): `src/MathEngine.cpp`
- [NN training primitives](https://github.com/tenri69/tnsrlang/blob/main/src/MathEngine.cpp): `src/MathEngine.cpp`
- [NN diagnostics](https://github.com/tenri69/tnsrlang/blob/main/src/Evaluator.cpp): `src/Evaluator.cpp`

### User Interfaces

- [CLI entrypoint](https://github.com/tenri69/tnsrlang/blob/main/src/main.cpp): `src/main.cpp`
- [HTTP server](https://github.com/tenri69/tnsrlang/blob/main/src/httplib.h): `src/httplib.h`
- [Browser terminal and canvas](https://github.com/tenri69/tnsrlang/tree/main/public): `public`

### Other components

- **Script author**
- **TnsrLang source**
- **Browser user**
- **Saved model files**

## Connections

- Script author → TnsrLang source: writes (evidence: `README.md`)
- TnsrLang source → CLI entrypoint: runs (evidence: `README.md`)
- CLI entrypoint → Lexer: tokenizes with (evidence: `src/main.cpp`)
- Lexer → Parser: tokens to
- Parser → Syntax tree: builds (evidence: `src/Parser.cpp`)
- CLI entrypoint → Parser: parses with (evidence: `src/main.cpp`)
- CLI entrypoint → Tree-walk evaluator: evaluates with (evidence: `src/main.cpp`)
- Tree-walk evaluator → Scopes and functions: uses scopes (evidence: `src/Evaluator.h`)
- Tree-walk evaluator → Native functions: registers natives (evidence: `src/Evaluator.cpp`)
- Native functions → Tensor operations: invokes operations (evidence: `src/Evaluator.cpp`)
- Tree-walk evaluator → AutoGrad graph: backpropagates (evidence: `README.md`)
- Tensor operations → AutoGrad graph: records gradients (evidence: `src/MathEngine.cpp`)
- Native functions → NN training primitives: exposes (evidence: `src/Evaluator.cpp`)
- Tree-walk evaluator → NN diagnostics: checks tensors (evidence: `src/Evaluator.cpp`)
- CLI entrypoint → AST visualizer: optionally dumps (evidence: `src/main.cpp`)
- Tree-walk evaluator → Model persistence: saves and loads (evidence: `README.md`)
- Model persistence → Saved model files: reads and writes (evidence: `README.md`)
- Browser user → Browser terminal and canvas: uses (evidence: `README.md`)
- CLI entrypoint → HTTP server: starts server (evidence: `src/main.cpp`)
- HTTP server → Browser terminal and canvas: serves assets
- Browser terminal and canvas → HTTP server: submits scripts (evidence: `README.md`)
- HTTP server → Tree-walk evaluator: executes scripts
- Browser terminal and canvas → NN training primitives: requests inference (evidence: `README.md`)
