How Kova is built
For people who want to read or change the compiler. Everything is in the repository’s src/ and
runtime/ folders, written in Rust.
The pipeline
Section titled “The pipeline”text ─ lexer.rs ─ parser.rs ─ types.rs ─ ownership.rs ─ kir.rs ─┬─ interp.rs (kova run, kova test) └─ codegen.rs (kova build) ─ link.rslexer.rscuts the text into tokens, each with its position.parser.rsbuilds the tree (ast.rs). It keeps going after an error, so the editor can show several at once.types.rsis the checker: it types every expression and records what the later passes need (Info). Types are mostly written by the programmer; almost nothing is inferred.ownership.rsis the only judge of moves and borrows (Ownership: no lifetimes, markersvar/moveat each call).kir.rsturns the checked tree into KIR, a small typed instruction set shared by both backends. It also makes copies of generic functions and builds the helper functions (drop, clone, show, equal) of each type.interp.rsruns KIR directly.codegen.rsturns it into machine code with Cranelift for x86-64 and arm64.link.rsis Kova’s own linker: it writes ELF (Linux), PE (Windows) and Mach-O (macOS) executables from the program’s object and the runtime. No C compiler or system linker is needed.
kova run and kova build must print exactly the same: the tests run every program both ways.
The runtime
Section titled “The runtime”runtime/kova.rs is a no_std Rust library (texts, lists, files, the web, regular expressions, JSON,
cryptography…), with one file per system for what it asks of it: linux.rs (raw system calls, its own
memory allocator), windows.rs (kernel32.dll and a few others), macos.rs (libSystem).
Code both the interpreter and the runtime need (number formatting, dates, HTTP, src/shared.rs,
src/json.rs, src/regex.rs, src/crypto.rs…) is written once and included in both, so that they
cannot differ.
The editor side
Section titled “The editor side”lsp.rs (language server) and debug.rs (debug adapter) call the same analyze function the
command line does, on text that is not saved yet.
Messages and languages
Section titled “Messages and languages”Every text Kova shows is a key in locales/en.kova and locales/fr.kova; the unit test
complete_catalogs checks that both have the same keys and holes. The runtime’s own texts are
written into each executable, which picks its language when it starts.
cargo testruns the unit tests andtests/kova.rs: everytests/ok/*.kova(with// expect:lines, run and built), everytests/error/*.kova(with// error:lines), every example of this site, the language server and the debugger.- A program of several files is a folder
tests/ok/<name>/main.kova.// stdin:and// args:feed the program. - Code behind
cfg(target_os = …)is checked from any machine withcargo check --tests --target aarch64-apple-darwinand--target x86_64-pc-windows-msvc. .github/workflows/tests.ymlruns everything on Linux, Windows and macOS (x86-64 and arm64) when started by hand.
Adding something
Section titled “Adding something”- A message: a key in both
locales/en.kovaandlocales/fr.kova. - A built-in method: its signature in
types.rs, its translation inkir.rs, a test intests/ok. - A runtime function: write it once in
src/shared.rswhen both backends need it.