Skip to content

Tooling and LSP

Buntos edited this page Dec 5, 2026 · 1 revision

Tooling and LSP

The H toolchain ships with the hlc compiler CLI (including a built-in test runner and DWARF debug support), a canonical source formatter (hlfmt), a zero-LLVM semantic Language Server Protocol server (hl-lsp), a C++ binding generator (hl-bindgen-cpp), a standalone install script (scripts/install.sh), and VS Code / Neovim editor integrations under editors/.


1. hlc Compiler CLI

Usage: hlc [build|run|test] [file.hl|dir] [-g] [-c] [-o <out>] [-O0|-O1|-O2|-O3]
           [-I <dir>] [--emit-llvm] [--run] [-l<lib>] [-L<dir>] [<extra.o>] [--runtime <path>]

Subcommands

Subcommand Behavior
build (default) Compiles input .hl file, directory (<dir>/main.hl), or h.toml project into a native executable
run (or --run / -r) Compiles to a temporary binary (or -o <out>), executes it, deletes the temporary binary, and propagates its exit code
test Discovers *_test.hl files and def test_*() functions, compiles and runs a native test harness with std/test

Flags

Flag Description
-o <path> Output executable or object file path (defaults to stripping .hl from entry path, or name from h.toml)
-c Emit a native object file (.o) without invoking the linker (used to compile runtime/hl_runtime.hl)
-g Emit DWARF v4 debug info via LLVM DIBuilder and pass -g to gcc; defaults optimization level to -O0 (skipping LLVMRunPasses) unless -O* is explicitly supplied
-O0, -O1, -O2, -O3 Optimization level (default -O2, or -O0 when -g is set without an explicit -O*). Runs LLVM's LLVMRunPasses with default<O1>, default<O2>, or default<O3> when opt_level > 0, and skips IR optimization passes at -O0
-I <dir> / -I<dir> Adds an extra module import search directory (repeatable)
--emit-llvm Verify the LLVM module and print textual LLVM IR to stdout without emitting an object file
--runtime <path> Explicit path to runtime object/archive. By default, hlc auto-detects libhl_runtime_h.o relative to $H_HOME / /proc/self/exe before falling back to zig-out/lib/libhl_runtime.a
-l<lib>, -L<dir>, -Wl,..., *.o, *.a Forwarded to the gcc link step (which always links -lgc -lm -lpthread)
-h, --help Print usage text and exit 0

Portable Path Resolution (H_HOME & /proc/self/exe)

hlc works from any working directory by locating std/ and libhl_runtime_h.o relative to its own executable path (/proc/self/exe -> <exe_dir>/../share/h for installed binaries, <exe_dir>/../.. for repo builds), via the $H_HOME environment variable, or via -I <dir>.

Frozen Bootstrap Seed CLI Note

The frozen Zig bootstrap seed (zig-out/bin/hlc) implements only the minimal flag subset (<file.hl>, run, -o, --runtime, --emit-llvm, -O0..-O3, -l, -L) needed to compile compiler/main.hl once. All full CLI subcommands, -g, -c, -I, h.toml ([dependencies]), and new language features live exclusively in the self-hosted compiler (hlc / hlc-pure).


2. DWARF Debugging (-g)

Passing -g activates Codegen.enable_debug():

  • Emits module metadata flags Debug Info Version = 3 and Dwarf Version = 4.
  • Creates a DICompileUnit (producer "hlc") and a DIFile per loaded module.
  • Attaches a DISubprogram to every emitted H function and method.
  • Updates the builder's current DILocation (LLVMSetCurrentDebugLocation2) as statements are emitted.
  • Registers function parameters (LLVMDIBuilderCreateParameterVariable) and local variables (LLVMDIBuilderCreateAutoVariable) via LLVMDIBuilderInsertDeclareRecordAtEnd.
  • Defaults to -O0 (skipping LLVMRunPasses) and passes -g to gcc when linking so gdb and lldb resolve source file names, line numbers, stack frames, and local values cleanly:
hlc -g examples/chaining_fn.hl -o chaining_fn
gdb ./chaining_fn

3. Native Test Runner (hlc test)

hlc test [path] automates unit testing:

  • Defaults to tests when path is omitted.
  • Accepts either a single .hl file or a directory containing *_test.hl files.
  • Parses each file with Lexer and Parser to collect all top-level functions matching test_* with zero parameters and zero type parameters.
  • Synthesizes a temporary H harness importing std/test and the test file, runs TypeChecker + Codegen, executes the compiled test binary, prints per-test PASS / FAIL and check counts, and exits 1 on failure.
hlc test tests/
hlc test tests/http_and_ergonomics_test.hl -g

4. Canonical Source Formatter (hlfmt)

tools/hlfmt.hl (hlfmt) formats .hl source files according to canonical H style:

# Format to stdout
hlfmt examples/vec2.hl

# Rewrite files in place
hlfmt --write src/main.hl src/util.hl

# CI check mode (exits 1 if any file differs from canonical formatting)
hlfmt --check compiler/*.hl std/*.hl

Formatting Rules

  • Indentation: 2 spaces per block level (def, mut def, struct, class, enum, interface, if/elsif/else, unless, while, for, do ... end).
  • match alignment: when and else arms align with their enclosing match, while arm bodies indent one level deeper.
  • Multi-line continuation: Lines starting with . or &. indent one extra level; open parenthesized lists indent inner lines by current parenthesis nesting depth while closing ) dedents cleanly.
  • String/comment awareness: Token scanning ignores keywords inside strings, character literals, and # comments, and preserves single-line closures (fn(x: I64) -> I64 do x * 2 end).
  • Whitespace hygiene: Strips trailing whitespace, collapses consecutive blank lines to at most one, and ensures a single trailing newline.

5. Zero-LLVM Semantic Language Server (hl-lsp)

tools/hl_lsp.hl (hl-lsp) implements a JSON-RPC 2.0 Language Server over stdio (Content-Length framing). Because compiler/typecheck.hl is written in pure H with zero LLVM dependency, hl-lsp imports the exact same frontend and typechecker modules as hlc (compiler/token.hl, compiler/lexer.hl, compiler/ast.hl, compiler/parser.hl, and compiler/typecheck.hl) while linking zero libLLVM (~218 KB standalone binary).

Advertised Capabilities & Supported Requests

Method Behavior
initialize Advertises textDocumentSync: 1 (full sync), documentSymbolProvider, definitionProvider, hoverProvider, completionProvider (trigger character .), and server info hl-lsp 0.1.0
textDocument/didOpen, didChange, didClose Maintains an in-memory document map (StrMap), loads imported modules via LspLoader (supporting relative paths, std/, and $H_HOME/std/), runs TypeChecker.check_modules, and publishes multi-diagnostic semantic and type errors (type mismatches, nil safety & Bool?, @[nogc] violations, match exhaustiveness, interface bounds, missing fields/methods) alongside lexer/parser errors via textDocument/publishDiagnostics
textDocument/documentSymbol Returns outline symbols for interface, enum, struct, class, Type.method, and top-level def declarations
textDocument/definition Jumps to the declaration line of top-level functions, structs/classes, methods, enums, and interfaces in the open document
textDocument/hover Displays formatted Markdown h signatures (@[nogc], mut def, generic type parameters & bounds, parameter types, return type) and field/method/variant counts
textDocument/completion Suggests H keywords, built-in types, and all functions, structs, classes, enums, and interfaces parsed from the current document
shutdown, exit Clean protocol shutdown (exit returns 0 after shutdown, 1 otherwise)

6. Standalone Installation & Editor Integrations

Standalone Install (scripts/install.sh)

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

Installs hlc, hlfmt, hl-lsp, and hl-bindgen-cpp to $PREFIX/bin (default $HOME/.local/bin) and installs libhl_runtime_h.o plus std/ (and std/interop/) to $PREFIX/share/h/.

VS Code (editors/vscode/)

Contains a TextMate grammar (syntaxes/h.tmLanguage.json), language-configuration.json (comment toggling, bracket/quote pairs, do/end block folding), and package.json:

  1. Copy or symlink editors/vscode into ~/.vscode/extensions/h-language/.
  2. Reload VS Code for .hl syntax highlighting and block folding.
  3. Install hl-lsp via ./scripts/install.sh and connect it with any generic stdio LSP extension for language ID h.

Neovim (editors/neovim/h.lua)

Drop editors/neovim/h.lua into ~/.config/nvim/after/plugin/h.lua (or require() it from init.lua). It automatically configures:

  • .hl -> h filetype detection
  • 2-space indentation (shiftwidth = 2, tabstop = 2, expandtab = true) and # %s comments
  • Automatic hl-lsp attachment via vim.lsp.start rooted at h.toml or .git
  • :HFormat user command running hlfmt --write and reloading the buffer

7. C++ Binding Generator (hl-bindgen-cpp)

tools/hl_bindgen_cpp.hl (hl-bindgen-cpp) generates extern "C" C++ shims and H wrapper bindings:

hl-bindgen-cpp <header.hpp> \
  --out-cpp <shim.cpp> \
  --out-hl <bindings.hl> \
  [--inst <fn>:<cpp_type>]

See Foreign Interop for details on exception boundaries, template instantiation directives, and generated class wrappers.


8. build.zig Build Targets & LLVM Auto-Detection

build.zig automatically probes llvm-config-23, llvm-config-22, llvm-config-21, llvm-config-20, llvm-config-19, and llvm-config to discover the installed LLVM major version and library directory. Override manually with -Dllvm-lib=<name> and -Dllvm-libdir=<dir>.

Command Dependencies Outputs / Action
zig build — Builds zig-out/bin/hlc (frozen Zig bootstrap seed) and zig-out/lib/libhl_runtime.a (C seed runtime)
zig build test zig build Runs src/tests.zig: bootstrap checks, native execution tests, fixed-point self-hosting verification, pure-H runtime verification, and C++/D/Objective-C/.NET interop tests
zig build self-host zig build Builds zig-out/bin/hlc-self, compiles runtime/hl_runtime.hl -> zig-out/lib/libhl_runtime_h.o, and builds zig-out/bin/hlc-pure
zig build fixed-point zig build self-host Compiles compiler/ with hlc-pure into zig-out/bin/hlc-pure2 and verifies bit-for-bit equality via cmp
zig build tools zig build self-host Compiles zig-out/bin/hl-bindgen-cpp, zig-out/bin/hlfmt, and zig-out/bin/hl-lsp (zero LLVM) using hlc-pure + libhl_runtime_h.o
zig build htest zig build self-host Runs ./zig-out/bin/hlc-pure test tests/ --runtime zig-out/lib/libhl_runtime_h.o

Next: Compiler Architecture

Clone this wiki locally