Welcome to the clean, self-hosted version of the Kai systems programming language compiler!
This repository has been fully transitioned from the original Python bootstrap compiler. All compiler development, standard library modules, and feature enhancements are now written entirely in Kai and compiled by Kai itself.
src/: The self-hosted compiler source code:compiler.kai: Main compiler driver and command-line entrypoint.lexer.kai: Lexical analyzer converting source code to tokens.parser.kai: Parser converting tokens to AST nodes. Supports parsing traits, generics, error unions, block curly braces, and inline assembly.type_checker.kai: Resolves types, tracks mutability/move semantics, and validates symbol scopes.codegen.kai: Transpiles the AST to highly portable C code.ast_nodes.kai&token_defs.kai: AST node structure and token type definitions.symbol_table.kai: Manages scopes and variable descriptors.
std/: The standard library:std/core/allocator.kai: Allocators and memory management.std/collections/: Core collections (ArrayList,HashMap,StringBuilder).std/io/: Basic I/O and file handling.
compiler_bootstrap.c: The verified C transpilation output of the compiler, used as the zero-dependency bootstrapper.
- Types: We only have
*T(const/immutable pointer) and*mut T(mutable pointer). Legacy references (&T/&mut T) are mapped to pointers in the parser. - Address-Of:
&xautomatically infers pointer mutability (*mut Tif the target is avar,*Tif the target is alet). - Dereference:
*ptris safe by default and does not require anunsafeblock. - Type Checking: Strict type checking prevents assigning
*Tto*mut T(const correctness).
The self-hosted compiler supports all core language features 1:1 with the Python reference backend, including:
- Struct/Enum factory constructors (e.g.,
ArrayList<Int>(&allocator)). - Semicolons and curly brace delimiters.
- C-style
sizeof(Type)parsing and codegen. - Slices, ranges, array literals, and tuple initialization expressions.
- GCC Inline Assembly (
asm) blocks with volatile prefixes, inputs, outputs, and clobbers.
To build the compiler from scratch without any pre-existing binaries or Python dependencies:
Compile the C bootstrap file directly using clang or gcc:
clang -O3 compiler_bootstrap.c -o ./kai_bootstrapCompile the Kai source files using the bootstrapped compiler binary:
./kai_bootstrap src/compiler.kai -o ./kaiVerify that the new compiler binary is functional by compiling a target program or recompiling itself:
./kai src/compiler.kai -o ./kai_self_compiledFor the next agent working on this codebase, the priorities are:
- Error Unions implementation (Phase 3):
- Implement status-integer error unions (
T!E), restoring thetry/catchpropagation check logic natively inside the self-hosted type checker and C code generator.
- Implement status-integer error unions (
- Standard Library Expansion:
- Add native networking, threading wrappers, and standard formatting routines directly into the
std/directory.
- Add native networking, threading wrappers, and standard formatting routines directly into the
- Compiler Speed Optimizations:
- Modifier compiler structures (e.g., AST pooling, string allocation) to increase the self-compilation throughput.