How grain-lang/grain works
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: grain.js Grain CLI · exec.js Command launcher · compcore.re Wasm codegen · grainrun.js WASI runner
Components 19
CLI and runtime
- Grain CLI
cli/bin/grain.js - Command launcher
cli/bin/exec.js - WASI runner
cli/bin/grainrun.js
Compiler pipeline
- Compiler command
compiler/grainc/grainc.re - Source parsing
compiler/src/parsing/driver.re - Type checking
compiler/src/typed/typecore.re - ANF optimization
compiler/src/middle_end/optimize.re - Wasm codegen
compiler/src/codegen/compcore.re - Wasm linking
compiler/src/link.re
Developer services
- Documentation CLI
compiler/graindoc/graindoc.re - Formatter CLI
compiler/grainformat/grainformat.re - LSP CLI
compiler/grainlsp/grainlsp.re - Language server
compiler/src/language_server/driver.re - Formatting engine
compiler/src/formatting/fmt.re - Doc comments
compiler/src/diagnostics/comments.re
Language support
- Grain standard library
stdlib/index.js
Other components
- Grain user
- WebAssembly module
- Binaryen
Connections 20
- Grain user → Grain CLI: invokes
cli/bin/grain.js - Grain CLI → Command launcher: dispatches
cli/bin/grain.js - Command launcher → Compiler command: launches compiler
cli/bin/exec.js - Grain CLI → Command launcher: runs compiled file
cli/bin/grain.js - Command launcher → WASI runner: launches runner
cli/bin/exec.js - WASI runner → WebAssembly module: loads and runs
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
README.md - Grain CLI → Documentation CLI: dispatches docs
cli/bin/grain.js - Grain CLI → Formatter CLI: dispatches format
cli/bin/grain.js - Grain CLI → LSP CLI: starts server
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
cli/bin/grain.js