Skip to content

Getting Started

Buntos edited this page Dec 5, 2026 · 1 revision

Getting Started

This guide covers building the H toolchain from source, installing it standalone with scripts/install.sh, running tests, and configuring projects with h.toml.

Prerequisites

Core Toolchain (Required)

  • Zig 0.17.1+ — builds the frozen bootstrap seed compiler (hlc) and C seed runtime (libhl_runtime.a)
  • LLVM 19–23 (libLLVM-19 through libLLVM-23, or libLLVM) — native IR verification, optimization (LLVMRunPasses), DWARF debug generation, and object emission. build.zig auto-detects the installed LLVM via llvm-config-23..llvm-config-19 or llvm-config, and accepts explicit overrides -Dllvm-lib=<name> and -Dllvm-libdir=<dir>
  • GCC (gcc) — links emitted .o object files with the runtime and system libraries
  • Boehm GC (libgc), standard math (libm), and POSIX threads (libpthread)

Optional Foreign-Interop Toolchains

Only required if you build and run the corresponding interop examples or projects:

  • C++ (g++ with C++17 & libstdc++) — for hl-bindgen-cpp shims (examples/cpp_interop/)
  • LDC (ldc2, libdruntime-ldc-shared, libphobos2-ldc-shared) — for GC-enabled D modules (examples/d_interop/)
  • Objective-C runtime (libobjc / GNUstep libobjc2) — for std/interop/objc (examples/objc_interop/)
  • .NET SDK + libhostfxr — for CoreCLR C# and F# hosting (examples/dotnet_interop/)

Building from Source

Clone the repository and use build.zig to drive the bootstrap and self-hosting builds:

git clone https://github.com/buntatoes/h-lang.git
cd h-lang

1. Build the Bootstrap Seed & Run Core Tests

zig build test

This builds the frozen Zig bootstrap seed (zig-out/bin/hlc) and C seed runtime (zig-out/lib/libhl_runtime.a), then runs the full compiler, runtime, interop, and fixed-point self-hosting test suite in src/tests.zig.

To target a non-default LLVM installation explicitly:

zig build self-host -Dllvm-lib=LLVM-20 -Dllvm-libdir=/usr/lib/llvm-20/lib

2. Build the Self-Hosted Compiler & Pure-H Runtime

zig build self-host

This executes the self-hosting sequence:

  1. Compiles compiler/main.hl with ./zig-out/bin/hlc (linked against the detected LLVM shared library) into zig-out/bin/hlc-self.
  2. Compiles runtime/hl_runtime.hl with ./zig-out/bin/hlc-self -c into the pure-H runtime object zig-out/lib/libhl_runtime_h.o.
  3. Compiles compiler/ with ./zig-out/bin/hlc-self against zig-out/lib/libhl_runtime_h.o into zig-out/bin/hlc-pure.

3. Verify Bit-for-Bit Fixed-Point Compilation

zig build fixed-point

Compiles compiler/ a second time using zig-out/bin/hlc-pure to produce zig-out/bin/hlc-pure2 and runs cmp zig-out/bin/hlc-pure zig-out/bin/hlc-pure2 to verify bit-for-bit identical ELF output.

4. Build Pure-H Developer Tools

zig build tools

Compiles the three pure-H tools in tools/ using hlc-pure and libhl_runtime_h.o:

  • zig-out/bin/hl-bindgen-cpp — C++ header shim and H binding generator
  • zig-out/bin/hlfmt — canonical H source formatter
  • zig-out/bin/hl-lsp — zero-LLVM Language Server Protocol server over stdio

5. Run the Native Pure-H Test Suite

zig build htest

Executes ./zig-out/bin/hlc-pure test tests/ --runtime zig-out/lib/libhl_runtime_h.o to run all *_test.hl test suites (tests/core_lang_test.hl, tests/stdlib_full_test.hl, and tests/http_and_ergonomics_test.hl).

Standalone Installation

Use scripts/install.sh to install the self-hosted H toolchain so hlc, hlfmt, hl-lsp, and hl-bindgen-cpp work from any directory:

./scripts/install.sh [--prefix=<dir>] [--no-build]

By default PREFIX is $HOME/.local. Unless --no-build is passed, the script builds zig build self-host && zig build tools automatically if the binaries are missing, then installs:

  • $PREFIX/bin/hlc (installed from zig-out/bin/hlc-pure)
  • $PREFIX/bin/hlfmt
  • $PREFIX/bin/hl-lsp
  • $PREFIX/bin/hl-bindgen-cpp
  • $PREFIX/share/h/libhl_runtime_h.o
  • $PREFIX/share/h/std/ (all standard library modules and std/interop/)

Ensure $PREFIX/bin is on your PATH.

How hlc Finds std/ and libhl_runtime_h.o Anywhere

hlc does not require running from the repository root. At startup it resolves H_HOME in order:

  1. $H_HOME environment variable (if it contains std/math.hl or libhl_runtime_h.o)
  2. Relative to the running binary's own path via /proc/self/exe:
    • <exe_dir>/../share/h (installed layout from install.sh)
    • <exe_dir>/../.. (repository checkout layout zig-out/bin/hlc-pure)
    • <exe_dir>
  3. Current working directory (.)

Additional module search directories can always be passed with -I <dir>.

Writing and Running Your First Program

Create hello.hl:

def main() -> I32
  name = "H"
  puts("Hello from #{name}!")
  0
end

Compile and run immediately (using installed hlc, or ./zig-out/bin/hlc-pure from a repo checkout):

hlc run hello.hl

Compile to a standalone native binary:

hlc hello.hl -o hello
./hello

Inspect the generated LLVM IR without linking:

hlc --emit-llvm hello.hl

Compile with DWARF debug information (defaults to -O0 when -g is passed without an explicit -O* flag):

hlc -g hello.hl -o hello_dbg

Compile a multi-file directory containing main.hl:

hlc examples/modules/ -o modules_demo

When given a directory path, hlc checks for <dir>/h.toml first and otherwise resolves <dir>/main.hl.

Running Native Unit Tests

The hlc test subcommand discovers *_test.hl files in a directory (or compiles a single .hl test file), finds every zero-parameter non-generic function whose name begins with test_, generates a temporary harness importing std/test, compiles and executes it, and reports per-test pass/fail status.

Example math_test.hl:

import test
import math

def test_gcd_and_pow() -> Void
  test.assert_eq(math.gcd(84, 36), 12, "gcd(84, 36)")
  test.assert_eq(math.pow_i64(2, 10), 1024, "2^10")
end

Run it:

hlc test math_test.hl
hlc test tests/

Project Manifests (h.toml)

When hlc build or hlc run is invoked with no file argument (or with a directory path containing h.toml), the compiler reads h.toml from that directory.

Supported Keys & Sections

Key / Section Type Description
name String Default output binary name for hlc build (ignored if -o is passed or during hlc run)
entry String Relative path to the entry .hl source file (resolved relative to the manifest directory)
opt Integer (0–3) Default optimization level (-O0 through -O3) unless overridden on the CLI
debug Boolean (true / false) Enables -g DWARF debug emission when true
libs String Space-separated libraries or linker flags (bare names like sqlite3 become -lsqlite3; flags starting with - such as -L/opt/lib are passed verbatim)
[dependencies] Section Maps package names to local directory paths (mylib = "../mylib"), resolved relative to the manifest directory

For a dependency mylib = "../mylib", import mylib checks ../mylib/lib.hl, then ../mylib/mylib.hl, then ../mylib/main.hl; submodule imports such as import "mylib/util" as util resolve ../mylib/util.hl.

Example h.toml

[package]
name = "myapp"
entry = "src/main.hl"
opt = 2
debug = false
libs = "z curl"

[dependencies]
mylib = "../mylib"

With h.toml in the current directory:

# Compiles src/main.hl at -O2, resolves [dependencies], links -lz -lcurl, outputs ./myapp
hlc build

# Compiles to a temporary binary and runs it immediately
hlc run

# CLI flags override manifest defaults
hlc build -g -O0 -o myapp_dbg

Editor Setup

Ready-to-use editor configurations are provided under editors/ for VS Code (editors/vscode/) and Neovim (editors/neovim/h.lua), integrating .hl syntax highlighting, hlfmt, and live semantic diagnostics from hl-lsp. See Tooling and LSP for installation steps.


Next: Language Guide

Clone this wiki locally