# valen-lang/valen architecture

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

- Interactive diagram: https://gitdiagram.com/valen-lang/valen
- Repository: https://github.com/valen-lang/valen

## Overview

Valen is a prototype programming-language compiler. The main workflow takes project source through lexing, parsing, post-parsing, typing and instantiation, then lowers the program through a Rust FFI into an LLVM-oriented C++ backend. Separate capabilities include Rust-library interop, builtins and memory-management strategies. The README documents the overall compile-and-run and Rust interop workflows; source-index references support several internal links, but many implementation details were not sampled, so uncertain connections are omitted.

Main entry points: [`src/backend_ffi/mod.rs`](https://github.com/valen-lang/valen/blob/main/src/backend_ffi/mod.rs) (Backend FFI), [`src/parsing/parser.rs`](https://github.com/valen-lang/valen/blob/main/src/parsing/parser.rs) (Parser), [`Backend/src/vale.cpp`](https://github.com/valen-lang/valen/blob/main/Backend/src/vale.cpp) (LLVM backend), [`src/pass_manager/pass_manager.rs`](https://github.com/valen-lang/valen/blob/main/src/pass_manager/pass_manager.rs) (Source loading)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Source Frontend"]
  node_valec["valec CLI<br/>[main.rs]"]
  node_source_loader["Source loading<br/>[pass_manager.rs]"]
  node_lexer["Lexer<br/>[lexer.rs]"]
  node_parser["Parser<br/>[parser.rs]"]
  node_parsed_ast["Parsed AST<br/>[ast.rs]"]
end

subgraph group_semantic["Semantic Compilation"]
  node_postparser["Post-parser<br/>[post_parser.rs]"]
  node_post_ast["Post-parsing AST<br/>[ast.rs]"]
  node_type_compiler["Type compiler<br/>[compiler.rs]"]
  node_borrow_checker["Borrow checker<br/>[check_usages.rs]"]
  node_instantiator["Template instantiation<br/>[instantiator.rs]"]
  node_inst_ast["Instantiated AST<br/>[ast.rs]"]
  node_pass_manager["Compilation passes<br/>[pass_manager.rs]"]
end

subgraph group_backend["Code Generation"]
  node_backend_ffi["Backend FFI<br/>[mod.rs]"]
  node_metal_cache[("Metal cache<br/>[metal_cache.rs]")]
  node_metal_lowerer["Metal lowerer<br/>[metal_lowerer.rs]"]
  node_cpp_backend["LLVM backend<br/>[vale.cpp]"]
end

subgraph group_runtime["Runtime and Interop"]
  node_regions["Memory regions<br/>[regions.cpp]"]
  node_rc_runtime["Reference counting<br/>[rcimm.cpp]"]
  node_builtins["Language builtins<br/>[builtins.rs]"]
  node_rust_interop["Rust interop<br/>[orchestrator.rs]"]
end

node_developer(("Valen programmer"))
node_project["Project source"]
node_rust_libraries["Rust libraries"]
node_program["Compiled program"]

node_developer -->|"invokes"| node_valec
node_project -->|"provides source"| node_source_loader
node_valec -.->|"starts compilation"| node_source_loader
node_source_loader -.->|"compilation input"| node_parser
node_parser -->|"uses tokens"| node_lexer
node_parser -->|"builds"| node_parsed_ast
node_postparser -->|"reads"| node_parsed_ast
node_postparser -->|"builds"| node_post_ast
node_type_compiler -.->|"consumes"| node_post_ast
node_borrow_checker -.->|"checks ownership"| node_type_compiler
node_instantiator -->|"resolves names"| node_post_ast
node_instantiator -->|"produces"| node_inst_ast
node_pass_manager -->|"invokes backend"| node_backend_ffi
node_backend_ffi -->|"passes cache"| node_metal_cache
node_backend_ffi -->|"uses lowerer"| node_metal_lowerer
node_backend_ffi -->|"calls FFI"| node_cpp_backend
node_cpp_backend -->|"emits executable"| node_program
node_cpp_backend -->|"uses region runtime"| node_regions
node_regions -.->|"supports reference counting"| node_rc_runtime
node_pass_manager -.->|"loads builtins"| node_builtins
node_developer -->|"selects dependencies"| node_rust_libraries
node_rust_interop -->|"bridges calls"| node_rust_libraries
node_rust_interop -.->|"configures interop compilation"| node_backend_ffi

click node_valec "https://github.com/valen-lang/valen/blob/main/src/bin/valec/main.rs"
click node_source_loader "https://github.com/valen-lang/valen/blob/main/src/pass_manager/pass_manager.rs"
click node_lexer "https://github.com/valen-lang/valen/blob/main/src/lexing/lexer.rs"
click node_parser "https://github.com/valen-lang/valen/blob/main/src/parsing/parser.rs"
click node_parsed_ast "https://github.com/valen-lang/valen/blob/main/src/parsing/ast/ast.rs"
click node_postparser "https://github.com/valen-lang/valen/blob/main/src/postparsing/post_parser.rs"
click node_post_ast "https://github.com/valen-lang/valen/blob/main/src/postparsing/ast.rs"
click node_type_compiler "https://github.com/valen-lang/valen/blob/main/src/typing/compiler.rs"
click node_borrow_checker "https://github.com/valen-lang/valen/blob/main/src/typing/borrow_checker/symphony/check_usages.rs"
click node_instantiator "https://github.com/valen-lang/valen/blob/main/src/instantiating/instantiator.rs"
click node_inst_ast "https://github.com/valen-lang/valen/blob/main/src/instantiating/ast/ast.rs"
click node_pass_manager "https://github.com/valen-lang/valen/blob/main/src/pass_manager/pass_manager.rs"
click node_backend_ffi "https://github.com/valen-lang/valen/blob/main/src/backend_ffi/mod.rs"
click node_metal_cache "https://github.com/valen-lang/valen/blob/main/src/backend_ffi/metal_cache.rs"
click node_metal_lowerer "https://github.com/valen-lang/valen/blob/main/src/backend_ffi/metal_lowerer.rs"
click node_cpp_backend "https://github.com/valen-lang/valen/blob/main/Backend/src/vale.cpp"
click node_regions "https://github.com/valen-lang/valen/blob/main/Backend/src/region/regions.cpp"
click node_rc_runtime "https://github.com/valen-lang/valen/blob/main/Backend/src/region/rcimm/rcimm.cpp"
click node_builtins "https://github.com/valen-lang/valen/blob/main/src/builtins/builtins.rs"
click node_rust_interop "https://github.com/valen-lang/valen/blob/main/src/typing/rust_interop/bifrost/orchestrator.rs"

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_valec,node_source_loader,node_lexer,node_parser,node_parsed_ast toneBlue
class node_postparser,node_post_ast,node_type_compiler,node_borrow_checker,node_instantiator,node_inst_ast,node_pass_manager toneAmber
class node_backend_ffi,node_metal_cache,node_metal_lowerer,node_cpp_backend toneMint
class node_regions,node_rc_runtime,node_builtins,node_rust_interop toneRose
class node_developer,node_project,node_rust_libraries,node_program toneIndigo
```

## Components

### Source Frontend

- [valec CLI](https://github.com/valen-lang/valen/blob/main/src/bin/valec/main.rs): `src/bin/valec/main.rs`
- [Source loading](https://github.com/valen-lang/valen/blob/main/src/pass_manager/pass_manager.rs): `src/pass_manager/pass_manager.rs`
- [Lexer](https://github.com/valen-lang/valen/blob/main/src/lexing/lexer.rs): `src/lexing/lexer.rs`
- [Parser](https://github.com/valen-lang/valen/blob/main/src/parsing/parser.rs): `src/parsing/parser.rs`
- [Parsed AST](https://github.com/valen-lang/valen/blob/main/src/parsing/ast/ast.rs): `src/parsing/ast/ast.rs`

### Semantic Compilation

- [Post-parser](https://github.com/valen-lang/valen/blob/main/src/postparsing/post_parser.rs): `src/postparsing/post_parser.rs`
- [Post-parsing AST](https://github.com/valen-lang/valen/blob/main/src/postparsing/ast.rs): `src/postparsing/ast.rs`
- [Type compiler](https://github.com/valen-lang/valen/blob/main/src/typing/compiler.rs): `src/typing/compiler.rs`
- [Borrow checker](https://github.com/valen-lang/valen/blob/main/src/typing/borrow_checker/symphony/check_usages.rs): `src/typing/borrow_checker/symphony/check_usages.rs`
- [Template instantiation](https://github.com/valen-lang/valen/blob/main/src/instantiating/instantiator.rs): `src/instantiating/instantiator.rs`
- [Instantiated AST](https://github.com/valen-lang/valen/blob/main/src/instantiating/ast/ast.rs): `src/instantiating/ast/ast.rs`
- [Compilation passes](https://github.com/valen-lang/valen/blob/main/src/pass_manager/pass_manager.rs): `src/pass_manager/pass_manager.rs`

### Code Generation

- [Backend FFI](https://github.com/valen-lang/valen/blob/main/src/backend_ffi/mod.rs): `src/backend_ffi/mod.rs`
- [Metal cache](https://github.com/valen-lang/valen/blob/main/src/backend_ffi/metal_cache.rs): `src/backend_ffi/metal_cache.rs`
- [Metal lowerer](https://github.com/valen-lang/valen/blob/main/src/backend_ffi/metal_lowerer.rs): `src/backend_ffi/metal_lowerer.rs`
- [LLVM backend](https://github.com/valen-lang/valen/blob/main/Backend/src/vale.cpp): `Backend/src/vale.cpp`

### Runtime and Interop

- [Memory regions](https://github.com/valen-lang/valen/blob/main/Backend/src/region/regions.cpp): `Backend/src/region/regions.cpp`
- [Reference counting](https://github.com/valen-lang/valen/blob/main/Backend/src/region/rcimm/rcimm.cpp): `Backend/src/region/rcimm/rcimm.cpp`
- [Language builtins](https://github.com/valen-lang/valen/blob/main/src/builtins/builtins.rs): `src/builtins/builtins.rs`
- [Rust interop](https://github.com/valen-lang/valen/blob/main/src/typing/rust_interop/bifrost/orchestrator.rs): `src/typing/rust_interop/bifrost/orchestrator.rs`

### Other components

- **Valen programmer**
- **Project source**
- **Rust libraries**
- **Compiled program**

## Connections

- Valen programmer → valec CLI: invokes
- Project source → Source loading: provides source (evidence: `src/pass_manager/pass_manager.rs`)
- valec CLI → Source loading: starts compilation
- Source loading → Parser: compilation input
- Parser → Lexer: uses tokens (evidence: `src/parsing/parser.rs`)
- Parser → Parsed AST: builds (evidence: `src/parsing/parser.rs`)
- Post-parser → Parsed AST: reads (evidence: `src/postparsing/post_parser.rs`)
- Post-parser → Post-parsing AST: builds (evidence: `src/postparsing/post_parser.rs`)
- Type compiler → Post-parsing AST: consumes
- Borrow checker → Type compiler: checks ownership
- Template instantiation → Post-parsing AST: resolves names (evidence: `src/instantiating/instantiator.rs`)
- Template instantiation → Instantiated AST: produces (evidence: `src/instantiating/instantiator.rs`)
- Compilation passes → Backend FFI: invokes backend (evidence: `src/pass_manager/pass_manager.rs`)
- Backend FFI → Metal cache: passes cache (evidence: `src/backend_ffi/mod.rs`)
- Backend FFI → Metal lowerer: uses lowerer (evidence: `src/backend_ffi/mod.rs`)
- Backend FFI → LLVM backend: calls FFI (evidence: `src/backend_ffi/mod.rs`)
- LLVM backend → Compiled program: emits executable
- LLVM backend → Memory regions: uses region runtime (evidence: `Backend/src/vale.cpp`)
- Memory regions → Reference counting: supports reference counting
- Compilation passes → Language builtins: loads builtins
- Valen programmer → Rust libraries: selects dependencies
- Rust interop → Rust libraries: bridges calls
- Rust interop → Backend FFI: configures interop compilation
