# aavtic/pine architecture

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

- Interactive diagram: https://gitdiagram.com/aavtic/pine
- Repository: https://github.com/aavtic/pine

## Overview

1. **Top-level architecture**  
   This is a Rust workspace for a compiled language toolchain. The repository is split into small crates that map directly to compiler stages, plus a CLI compiler (`tools/pinec`), C runtime support (`runtime/c`), Pine-language standard library (`stdlib/`), examples, and tests.

2. **Compiler pipeline boundary**  
   Pine’s flow is intentionally linear and inspectable: source `.alp` files are tokenized, parsed into an AST, semantically analyzed, lowered toward an internal IR, code generated to LLVM IR / object code, and then linked into a native executable. The CLI can stop at intermediate stages for debugging and teaching.

3. **Lexing front end**  
   `crates/lexer` is the first stage and produces the token stream from Pine source text. This is the syntax boundary of the system: everything after this stage works on structured compiler data rather than raw text.

4. **Parsing and syntax model**  
   `crates/parser` consumes tokens and builds the AST defined in `crates/ast`. The AST crate centralizes the language’s syntax nodes and type representations, making it the shared contract between parser, analyzer, and later lowering/codegen stages.

5. **Semantic analysis and type checking**  
   `crates/analyzer` performs static checks over the AST: type validation, scope resolution, and likely symbol tracking for functions, locals, and foreign declarations. This is where the “statically typed” nature of Pine is enforced before any backend work happens.

6. **Lowering / intermediate representation**  
   `crates/ir` is a dedicated boundary for a lower-level internal model used between analysis and backend emission. In architecture terms, it separates language semantics from target-specific code generation, so later stages can reason about a simplified compiler IR instead of full syntax trees.

7. **Backend code generation**  
   `crates/codegen` turns the compiler’s internal representation into LLVM-facing output and related backend artifacts. The presence of `function.rs` and `variable.rs` suggests the backend is organized around emitting function bodies and local/storage handling rather than monolithic code emission.

8. **Linking and native output**  
   `crates/linker` handles the final packaging step: combining generated object code with the runtime and producing the final executable. This is the boundary where Pine transitions from compiler output to an operating-system binary.

9. **Runtime and FFI boundary**  
   `runtime/c` contains C implementations of low-level services such as printing, string comparison, and stdin reading. Pine exposes these through `alien "c"` declarations, so the language runtime is split across Pine source wrappers in `stdlib/` and native C functions linked at build time.

10. **Standard library as Pine source**  
   `stdlib/` is not a separate compiled binary; it is language-level library code organized by domain (`io`, `math`, `time`). These modules wrap runtime/FFI primitives and provide the user-facing library surface that user programs import with `use`.

11. **CLI orchestration**  
   `tools/pinec` is the user entry point that drives the pipeline. It appears to support build modes that can emit tokens, AST, analysis output, LLVM IR, object files, or a final executable, making it the central control plane over the compiler stages.

12. **Cross-cutting support and tests**  
   `crates/errors`, `crates/config`, and `crates/utils` provide shared infrastructure: diagnostics, built-in constants/stdlib wiring, and common helpers. The `tests/` tree is organized by stage (`parser`, `test_analyzer`, `test_codegen`, `codegen/ir.rs`), which mirrors the architecture and validates each boundary independently.

13. **How to draw the diagram**  
   A good diagram would place user `.alp` source on the left, then show a pipeline through lexer → parser → AST → analyzer → IR/codegen → linker, with a side branch to the Pine stdlib and C runtime through FFI. The CLI should sit above the pipeline as the orchestrator, and tests should be shown as stage-specific verification around the corresponding compiler components.

Main entry points: [`tools/pinec`](https://github.com/aavtic/pine/tree/master/tools/pinec) (pinec), [`crates/parser`](https://github.com/aavtic/pine/tree/master/crates/parser) (Parser), [`crates/analyzer`](https://github.com/aavtic/pine/tree/master/crates/analyzer) (Analyzer), [`crates/codegen`](https://github.com/aavtic/pine/tree/master/crates/codegen) (Codegen)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_compiler["Compiler pipeline"]
  node_node_source["Pine source<br/>input"]
  node_node_lexer["Lexer<br/>front-end"]
  node_node_parser["Parser<br/>syntax"]
  node_node_ast["AST<br/>syntax model"]
  node_node_analyzer["Analyzer<br/>semantic"]
  node_node_ir["IR"]
  node_node_codegen["Codegen<br/>backend"]
  node_node_linker["Linker<br/>packaging"]
  node_node_errors["Errors<br/>shared infra"]
  node_node_config["Config<br/>shared infra"]
  node_node_utils["Utils<br/>shared infra"]
end

subgraph group_group_runtime["Runtime boundary"]
  node_node_stdlib["Stdlib<br/>Pine library"]
  node_node_runtime_c["C runtime<br/>native runtime"]
  node_node_ffi{{"FFI bridge<br/>foreign interface"}}
end

subgraph group_group_tooling["Tooling and tests"]
  node_node_cli["pinec<br/>CLI orchestrator"]
  node_node_examples["Examples<br/>sample programs"]
  node_node_parser_tests["Parser tests<br/>validation"]
  node_node_analyzer_tests["Analyzer tests<br/>validation"]
  node_node_codegen_tests["Codegen tests<br/>validation"]
end

node_node_source -->|"tokens"| node_node_lexer
node_node_lexer -->|"tokens"| node_node_parser
node_node_parser -->|"builds"| node_node_ast
node_node_ast -->|"analyzes"| node_node_analyzer
node_node_analyzer -->|"lowers to"| node_node_ir
node_node_ir -->|"emits"| node_node_codegen
node_node_codegen -->|"objects"| node_node_linker
node_node_cli -->|"drives"| node_node_lexer
node_node_cli -->|"drives"| node_node_parser
node_node_cli -->|"drives"| node_node_analyzer
node_node_cli -->|"drives"| node_node_codegen
node_node_cli -->|"drives"| node_node_linker
node_node_parser -->|"reports"| node_node_errors
node_node_analyzer -->|"reports"| node_node_errors
node_node_codegen -->|"reports"| node_node_errors
node_node_config -->|"maps"| node_node_stdlib
node_node_stdlib -->|"wraps"| node_node_ffi
node_node_ffi -->|"calls"| node_node_runtime_c
node_node_examples -->|"driven by"| node_node_cli
node_node_parser_tests -.->|"verifies"| node_node_parser
node_node_analyzer_tests -.->|"verifies"| node_node_analyzer
node_node_codegen_tests -.->|"verifies"| node_node_codegen

click node_node_lexer "https://github.com/aavtic/pine/tree/master/crates/lexer"
click node_node_parser "https://github.com/aavtic/pine/tree/master/crates/parser"
click node_node_ast "https://github.com/aavtic/pine/tree/master/crates/ast"
click node_node_analyzer "https://github.com/aavtic/pine/tree/master/crates/analyzer"
click node_node_ir "https://github.com/aavtic/pine/tree/master/crates/ir"
click node_node_codegen "https://github.com/aavtic/pine/tree/master/crates/codegen"
click node_node_linker "https://github.com/aavtic/pine/tree/master/crates/linker"
click node_node_cli "https://github.com/aavtic/pine/tree/master/tools/pinec"
click node_node_errors "https://github.com/aavtic/pine/tree/master/crates/errors"
click node_node_config "https://github.com/aavtic/pine/tree/master/crates/config"
click node_node_utils "https://github.com/aavtic/pine/tree/master/crates/utils"
click node_node_stdlib "https://github.com/aavtic/pine/tree/master/stdlib"
click node_node_runtime_c "https://github.com/aavtic/pine/tree/master/runtime/c"
click node_node_examples "https://github.com/aavtic/pine/tree/master/examples"
click node_node_parser_tests "https://github.com/aavtic/pine/tree/master/tests/parser"
click node_node_analyzer_tests "https://github.com/aavtic/pine/tree/master/tests/test_analyzer"
click node_node_codegen_tests "https://github.com/aavtic/pine/tree/master/tests/test_codegen"

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_node_source,node_node_lexer,node_node_parser,node_node_ast,node_node_analyzer,node_node_ir,node_node_codegen,node_node_linker,node_node_errors,node_node_config,node_node_utils toneBlue
class node_node_stdlib,node_node_runtime_c,node_node_ffi toneAmber
class node_node_cli,node_node_examples,node_node_parser_tests,node_node_analyzer_tests,node_node_codegen_tests toneMint
```

## Components

### Compiler pipeline

Core Rust crates that implement the Pine compilation stages.

- **Pine source**: input · User-authored .alp programs and library code.
- [Lexer](https://github.com/aavtic/pine/tree/master/crates/lexer): front-end · Tokenizes Pine source text into a token stream. · `crates/lexer`
- [Parser](https://github.com/aavtic/pine/tree/master/crates/parser): syntax · Consumes tokens and builds the AST. · `crates/parser`
- [AST](https://github.com/aavtic/pine/tree/master/crates/ast): syntax model · Shared syntax and type structures used across the compiler. · `crates/ast`
- [Analyzer](https://github.com/aavtic/pine/tree/master/crates/analyzer): semantic · Performs scope resolution, type checking, and static validation. · `crates/analyzer`
- [IR](https://github.com/aavtic/pine/tree/master/crates/ir): lowering IR · Lower-level intermediate representation between semantics and backend. · `crates/ir`
- [Codegen](https://github.com/aavtic/pine/tree/master/crates/codegen): backend · Emits LLVM-facing output and handles function and variable emission. · `crates/codegen`
- [Linker](https://github.com/aavtic/pine/tree/master/crates/linker): packaging · Combines generated objects with runtime pieces into a native executable. · `crates/linker`
- [Errors](https://github.com/aavtic/pine/tree/master/crates/errors): shared infra · Diagnostics and error handling shared across stages. · `crates/errors`
- [Config](https://github.com/aavtic/pine/tree/master/crates/config): shared infra · Compiler constants and standard library wiring. · `crates/config`
- [Utils](https://github.com/aavtic/pine/tree/master/crates/utils): shared infra · Common helpers used by multiple crates. · `crates/utils`

### Runtime boundary

Native runtime and language-level standard library surface.

- [Stdlib](https://github.com/aavtic/pine/tree/master/stdlib): Pine library · Language-level library modules such as io, math, and time. · `stdlib`
- [C runtime](https://github.com/aavtic/pine/tree/master/runtime/c): native runtime · C implementations of low-level services exposed through FFI. · `runtime/c`
- **FFI bridge**: foreign interface · Alien declarations connect Pine code to native runtime functions.

### Tooling and tests

CLI entrypoint and stage-specific validation binaries.

- [pinec](https://github.com/aavtic/pine/tree/master/tools/pinec): CLI orchestrator · Compiler entrypoint that drives the pipeline and can stop at intermediate stages. · `tools/pinec`
- [Examples](https://github.com/aavtic/pine/tree/master/examples): sample programs · Representative Pine programs demonstrating language features. · `examples`
- [Parser tests](https://github.com/aavtic/pine/tree/master/tests/parser): validation · Stage-specific checks for parsing behavior. · `tests/parser`
- [Analyzer tests](https://github.com/aavtic/pine/tree/master/tests/test_analyzer): validation · Stage-specific checks for semantic analysis. · `tests/test_analyzer`
- [Codegen tests](https://github.com/aavtic/pine/tree/master/tests/test_codegen): validation · Stage-specific checks for backend emission. · `tests/test_codegen`

## Connections

- Pine source → Lexer: tokens · Source text enters the front end.
- Lexer → Parser: tokens · Parser consumes the lexer output.
- Parser → AST: builds · Parser materializes the shared AST.
- AST → Analyzer: analyzes · Analyzer validates the syntax tree.
- Analyzer → IR: lowers to · Semantically valid programs are lowered into IR.
- IR → Codegen: emits · Backend turns IR into LLVM-facing output and objects.
- Codegen → Linker: objects · Generated code is packaged for linking.
- pinec → Lexer: drives · CLI can invoke and inspect the lexer stage.
- pinec → Parser: drives · CLI can stop after parsing for inspection.
- pinec → Analyzer: drives · CLI exposes semantic analysis output.
- pinec → Codegen: drives · CLI can emit backend artifacts and object code.
- pinec → Linker: drives · CLI can produce a final executable.
- Parser → Errors: reports · Parsing diagnostics use shared error handling.
- Analyzer → Errors: reports · Semantic diagnostics use shared error handling.
- Codegen → Errors: reports · Backend errors are surfaced through shared diagnostics.
- Config → Stdlib: maps · Configuration wires in standard library structure and constants.
- Stdlib → FFI bridge: wraps · Stdlib modules expose native capabilities through FFI.
- FFI bridge → C runtime: calls · Alien declarations bind to C runtime functions.
- Examples → pinec: driven by · Examples are compiled and run through the CLI.
- Parser tests → Parser: verifies · Parser tests exercise syntax handling.
- Analyzer tests → Analyzer: verifies · Analyzer tests exercise semantic checks.
- Codegen tests → Codegen: verifies · Codegen tests exercise backend emission.
