Skip to main content
Klar

CLI Reference

The Klar compiler provides several commands for running, building, and analyzing programs.

Usage

klar <command> [options]

Commands

init

Create a new Klar project with the standard directory structure.

klar init [name] [options]

Options:

OptionDescription
--libCreate a library project (with src/lib.kl)

Examples:

# Create project in current directory
klar init

# Create project with specific name
klar init my-project

# Create a library project
klar init my-lib --lib

This creates:

  • klar.json — Project manifest
  • src/main.kl — Entry point (or src/lib.kl for libraries)

run

Run a Klar program. By default, compiles to native code and executes.

klar run <file.kl> [options] [-- program_args...]

Options:

OptionDescription
--vmUse bytecode VM instead of native compilation
--interpretUse tree-walking interpreter
--debugEnable instruction tracing (VM only)

Examples:

# Run with native compilation (default, fastest)
klar run hello.kl

# Run with bytecode VM
klar run hello.kl --vm

# Run with interpreter (slowest, for debugging)
klar run hello.kl --interpret

# Pass arguments to the program
klar run hello.kl arg1 arg2

# Use -- to pass flags to the program
klar run hello.kl -- --verbose --count=5

build

Compile a Klar program or project to a native executable.

klar build [file.kl] [options]

If no file is specified, builds the project defined in klar.json.

Options:

OptionDescription
-o <name>Output file name (default: build/<basename>)
-O0No optimizations (default)
-O1Basic optimizations (constant folding, DCE)
-O2Standard optimizations
-O3Aggressive optimizations
-gGenerate debug information (DWARF)
-l <lib>Link with library (e.g., -lm, -lcurl)
-L <path>Add library search path
--emit-llvmOutput LLVM IR (.ll file)
--emit-asmOutput assembly (.s file)
--emit-irOutput Klar IR (.ir file)
--target <triple>Cross-compile for target
--verbose-optShow optimization statistics

Examples:

# Basic build
klar build hello.kl              # Creates build/hello

# Custom output path
klar build hello.kl -o myapp     # Creates myapp

# Optimized build
klar build hello.kl -O2          # Standard optimizations

# Debug build
klar build hello.kl -g           # With debug symbols

# Link with external library
klar build math.kl -lm           # Link with libm

# Multiple libraries with search path
klar build app.kl -L/opt/lib -lmylib -lcurl

# Inspect generated code
klar build hello.kl --emit-llvm  # Creates hello.ll
klar build hello.kl --emit-asm   # Creates hello.s

# Cross-compilation
klar build hello.kl --target x86_64-linux-gnu

# WebAssembly
klar build hello.kl --target wasm          # Freestanding wasm32
klar build hello.kl --target wasi          # WASI target
klar build hello.kl --target wasm -c       # Object file only

Target Triples:

TripleDescription
x86_64-linux-gnuLinux on x86_64
aarch64-linux-gnuLinux on ARM64
x86_64-apple-macosxmacOS on Intel
arm64-apple-macosxmacOS on Apple Silicon
x86_64-windows-msvcWindows on x86_64
aarch64-none-elfBare-metal ARM64 (ELF)
aarch64-none-eabiBare-metal ARM64 (EABI)
wasm32-unknown-unknownWebAssembly (freestanding)
wasm32-unknown-wasiWebAssembly (WASI)

Shorthand targets: --target wasm expands to wasm32-unknown-unknown, --target wasi expands to wasm32-unknown-wasi.

Bare-Metal Options:

For embedded and OS development, Klar supports freestanding compilation:

OptionDescription
--freestandingCompile without standard library (no libc)
--entry <symbol>Set entry point symbol (default: main)
-T <path>Use custom linker script
# Build a freestanding kernel
klar build kernel.kl --freestanding --target aarch64-none-elf -T linker.ld

check

Type-check a file without compiling or running.

klar check <file.kl> [options]

Options:

OptionDescription
--dump-ownershipShow ownership analysis output

Examples:

# Type check a file
klar check myprogram.kl

# Check with ownership analysis output
klar check myprogram.kl --dump-ownership

repl

Start an interactive REPL (Read-Eval-Print Loop).

klar repl

See REPL Guide for detailed usage.

update

Regenerate the lock file from the project manifest.

klar update

This reads klar.json and regenerates klar.lock with resolved dependency information.

Other Commands

CommandDescription
tokenize <file>Show lexer output (tokens)
parse <file>Show parser output (AST)
disasm <file>Disassemble bytecode
helpShow help message
versionShow version
testRun tests (not yet implemented)
fmtFormat source files (not yet implemented)

Exit Codes

The klar run command returns the exit code from your program's main function:

fn main() -> i32 {
    return 0  // Exit code 0 = success
}

If compilation fails, klar returns a non-zero exit code.

Environment

Klar does not currently use any environment variables for configuration.

Next Steps