# grain-lang/grain architecture

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

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

## Overview

Grain is a programming language toolchain that compiles Grain source to WebAssembly. Users enter through the CLI to compile, compile-and-run, or run programs; separate CLI commands provide documentation generation, formatting, and an LSP server. The compiler tree indicates parsing, typing, optimization, and code generation stages, but sampled compiler implementation was unavailable, so those internal relationships are shown as unverified. The README identifies WebAssembly and Binaryen as compilation targets/technology; compiler internals and optional integrations were not inspected.

Main entry points: [`cli/bin/grain.js`](https://github.com/grain-lang/grain/blob/main/cli/bin/grain.js) (Grain CLI), [`cli/bin/exec.js`](https://github.com/grain-lang/grain/blob/main/cli/bin/exec.js) (Command launcher), [`compiler/src/codegen/compcore.re`](https://github.com/grain-lang/grain/blob/main/compiler/src/codegen/compcore.re) (Wasm codegen), [`cli/bin/grainrun.js`](https://github.com/grain-lang/grain/blob/main/cli/bin/grainrun.js) (WASI runner)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI and runtime"]
  node_graincli["Grain CLI<br/>[grain.js]"]
  node_exec["Command launcher<br/>[exec.js]"]
  node_grainrun["WASI runner<br/>[grainrun.js]"]
end

subgraph group_compiler["Compiler pipeline"]
  node_compiler_cmd["Compiler command<br/>[grainc.re]"]
  node_parser["Source parsing<br/>[driver.re]"]
  node_typing["Type checking<br/>[typecore.re]"]
  node_middle["ANF optimization<br/>[optimize.re]"]
  node_codegen["Wasm codegen<br/>[compcore.re]"]
  node_linker["Wasm linking<br/>[link.re]"]
end

subgraph group_services["Developer services"]
  node_doccli["Documentation CLI<br/>[graindoc.re]"]
  node_formatcli["Formatter CLI<br/>[grainformat.re]"]
  node_lspcli["LSP CLI<br/>[grainlsp.re]"]
  node_lsp["Language server<br/>[driver.re]"]
  node_formatting["Formatting engine<br/>[fmt.re]"]
  node_diagnostics["Doc comments<br/>[comments.re]"]
end

subgraph group_language["Language support"]
  node_stdlib["Grain standard library<br/>[index.js]"]
end

node_user(("Grain user"))
node_wasm["WebAssembly module"]
node_binaryen["Binaryen"]

node_user -->|"invokes"| node_graincli
node_graincli -->|"dispatches"| node_exec
node_exec -->|"launches compiler"| node_compiler_cmd
node_graincli -->|"runs compiled file"| node_exec
node_exec -->|"launches runner"| node_grainrun
node_grainrun -->|"loads and runs"| node_wasm
node_compiler_cmd -.->|"processes source"| node_parser
node_parser -.->|"produces syntax"| node_typing
node_typing -.->|"typed input"| node_middle
node_middle -.->|"optimized input"| node_codegen
node_codegen -.->|"emits code"| node_linker
node_linker -.->|"produces module"| node_wasm
node_codegen -.->|"uses technology"| node_binaryen
node_graincli -->|"dispatches docs"| node_doccli
node_graincli -->|"dispatches format"| node_formatcli
node_graincli -->|"starts server"| node_lspcli
node_lspcli -.->|"starts"| node_lsp
node_formatcli -.->|"formats with"| node_formatting
node_doccli -.->|"reads doc comments"| node_diagnostics
node_graincli -->|"sets default library"| node_stdlib

click node_graincli "https://github.com/grain-lang/grain/blob/main/cli/bin/grain.js"
click node_exec "https://github.com/grain-lang/grain/blob/main/cli/bin/exec.js"
click node_grainrun "https://github.com/grain-lang/grain/blob/main/cli/bin/grainrun.js"
click node_compiler_cmd "https://github.com/grain-lang/grain/blob/main/compiler/grainc/grainc.re"
click node_parser "https://github.com/grain-lang/grain/blob/main/compiler/src/parsing/driver.re"
click node_typing "https://github.com/grain-lang/grain/blob/main/compiler/src/typed/typecore.re"
click node_middle "https://github.com/grain-lang/grain/blob/main/compiler/src/middle_end/optimize.re"
click node_codegen "https://github.com/grain-lang/grain/blob/main/compiler/src/codegen/compcore.re"
click node_linker "https://github.com/grain-lang/grain/blob/main/compiler/src/link.re"
click node_stdlib "https://github.com/grain-lang/grain/blob/main/stdlib/index.js"
click node_doccli "https://github.com/grain-lang/grain/blob/main/compiler/graindoc/graindoc.re"
click node_formatcli "https://github.com/grain-lang/grain/blob/main/compiler/grainformat/grainformat.re"
click node_lspcli "https://github.com/grain-lang/grain/blob/main/compiler/grainlsp/grainlsp.re"
click node_lsp "https://github.com/grain-lang/grain/blob/main/compiler/src/language_server/driver.re"
click node_formatting "https://github.com/grain-lang/grain/blob/main/compiler/src/formatting/fmt.re"
click node_diagnostics "https://github.com/grain-lang/grain/blob/main/compiler/src/diagnostics/comments.re"

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_graincli,node_exec,node_grainrun,node_user toneBlue
class node_compiler_cmd,node_parser,node_typing,node_middle,node_codegen,node_linker toneAmber
class node_doccli,node_formatcli,node_lspcli,node_lsp,node_formatting,node_diagnostics toneMint
class node_stdlib toneRose
class node_wasm,node_binaryen toneIndigo
```

## Components

### CLI and runtime

- [Grain CLI](https://github.com/grain-lang/grain/blob/main/cli/bin/grain.js): `cli/bin/grain.js`
- [Command launcher](https://github.com/grain-lang/grain/blob/main/cli/bin/exec.js): `cli/bin/exec.js`
- [WASI runner](https://github.com/grain-lang/grain/blob/main/cli/bin/grainrun.js): `cli/bin/grainrun.js`

### Compiler pipeline

- [Compiler command](https://github.com/grain-lang/grain/blob/main/compiler/grainc/grainc.re): `compiler/grainc/grainc.re`
- [Source parsing](https://github.com/grain-lang/grain/blob/main/compiler/src/parsing/driver.re): `compiler/src/parsing/driver.re`
- [Type checking](https://github.com/grain-lang/grain/blob/main/compiler/src/typed/typecore.re): `compiler/src/typed/typecore.re`
- [ANF optimization](https://github.com/grain-lang/grain/blob/main/compiler/src/middle_end/optimize.re): `compiler/src/middle_end/optimize.re`
- [Wasm codegen](https://github.com/grain-lang/grain/blob/main/compiler/src/codegen/compcore.re): `compiler/src/codegen/compcore.re`
- [Wasm linking](https://github.com/grain-lang/grain/blob/main/compiler/src/link.re): `compiler/src/link.re`

### Developer services

- [Documentation CLI](https://github.com/grain-lang/grain/blob/main/compiler/graindoc/graindoc.re): `compiler/graindoc/graindoc.re`
- [Formatter CLI](https://github.com/grain-lang/grain/blob/main/compiler/grainformat/grainformat.re): `compiler/grainformat/grainformat.re`
- [LSP CLI](https://github.com/grain-lang/grain/blob/main/compiler/grainlsp/grainlsp.re): `compiler/grainlsp/grainlsp.re`
- [Language server](https://github.com/grain-lang/grain/blob/main/compiler/src/language_server/driver.re): `compiler/src/language_server/driver.re`
- [Formatting engine](https://github.com/grain-lang/grain/blob/main/compiler/src/formatting/fmt.re): `compiler/src/formatting/fmt.re`
- [Doc comments](https://github.com/grain-lang/grain/blob/main/compiler/src/diagnostics/comments.re): `compiler/src/diagnostics/comments.re`

### Language support

- [Grain standard library](https://github.com/grain-lang/grain/blob/main/stdlib/index.js): `stdlib/index.js`

### Other components

- **Grain user**
- **WebAssembly module**
- **Binaryen**

## Connections

- Grain user → Grain CLI: invokes (evidence: `cli/bin/grain.js`)
- Grain CLI → Command launcher: dispatches (evidence: `cli/bin/grain.js`)
- Command launcher → Compiler command: launches compiler (evidence: `cli/bin/exec.js`)
- Grain CLI → Command launcher: runs compiled file (evidence: `cli/bin/grain.js`)
- Command launcher → WASI runner: launches runner (evidence: `cli/bin/exec.js`)
- WASI runner → WebAssembly module: loads and runs (evidence: `cli/bin/grainrun.js`)
- Compiler command → Source parsing: processes source
- Source parsing → Type checking: produces syntax
- Type checking → ANF optimization: typed input
- ANF optimization → Wasm codegen: optimized input
- Wasm codegen → Wasm linking: emits code
- Wasm linking → WebAssembly module: produces module
- Wasm codegen → Binaryen: uses technology (evidence: `README.md`)
- Grain CLI → Documentation CLI: dispatches docs (evidence: `cli/bin/grain.js`)
- Grain CLI → Formatter CLI: dispatches format (evidence: `cli/bin/grain.js`)
- Grain CLI → LSP CLI: starts server (evidence: `cli/bin/grain.js`)
- LSP CLI → Language server: starts
- Formatter CLI → Formatting engine: formats with
- Documentation CLI → Doc comments: reads doc comments
- Grain CLI → Grain standard library: sets default library (evidence: `cli/bin/grain.js`)
