DocsWelcome to Sable
Getting Started
Build the toolchain, run a file, make a package, and set up your editor.
This chapter gets a working Sable toolchain onto your machine, runs a single file, makes a package, and sets up your editor. By the end you will have written and run a small program, and know where to look when the compiler reports a problem.
Everything you do with Sable goes through one command, sable. It builds, runs, tests, formats, lints, documents and debugs your code, manages packages, and serves the language server your editor talks to.
Building the toolchain#
Sable has no binary releases yet, so you build the toolchain from source. You need:
- the .NET 11 SDK, which the compiler is written for;
clang, whichsableuses to compile and link (or set$CCto a clang that can link for your machine);lldb, but only if you wantsable debug.
Clone the repository:
git clone https://github.com/Rikarin/Sable.gitThen publish the driver into ~/.sable/bin:
cd Sable && dotnet publish src/Sable.Driver -c Release -o ~/.sable/binAdd ~/.sable/bin to your PATH, and check that it works:
sable --versionsable 0.5.0 (21eb4c62cd67)Your version and commit will differ. sable -V prints the same thing.
Sable supports macOS (Apple silicon and Intel) and Linux (x86-64 and arm64). Windows support is in progress.
Your first program#
Make a file called hello.sb:
module Hellofn main() { print("Hello, world!")}Then run it:
sable run hello.sbHello, world!That is a complete program. Three things are worth noticing:
- Every file that declares anything starts with a
moduledeclaration, which names the module the file belongs to. A module may span many files, and file names don't have to match anything inside them. Leaving the line out is an error (S2007). - The program starts at
main, a function that takes nothing. It may also return anInt, which becomes the process's exit status. You can mark the entry point explicitly with@main, which the examples in the language repository do. printis available without an import. It writes a value and a line break to standard output.
sable run compiles the file to a native executable and runs it. A single file is built optimised (-O2) by default; pass -O0 for an unoptimised build, or -g to include debug information. Arguments after -- are passed to your program:
sable run hello.sb -O0 -- first secondsable build hello.sb compiles without running, and sable check hello.sb only type-checks, without generating any code, which is much faster.
Scripts#
A file may start with a shebang line, which the compiler treats as a comment. That lets you make a single-file program directly executable:
#!/usr/bin/env -S sable runmodule Greetfn main() { print("hi from a script")}chmod +x greet.sb && ./greet.sbhi from a scriptNote
The shebang has to run sable run, not just sable, because sable with a file name and no command is not a command. env -S splits the line into the two words.
Making a package#
Anything bigger than one file belongs in a package. sable new makes one in a new directory:
sable new hello Created binary package `hello` in /home/you/hellocd hello && sable run Compiling hello v0.1.0 (.) Finished `dev` profile [unoptimised + debuginfo] target(s) in 7.59s Running `target/dev/hello`Hello, world!Inside a package, sable run, build, check and test need no file name: they work on the package. A package build uses the dev profile, unoptimised with debug information, unless you pass --release. sable init does the same as sable new, but in the current directory.
sable new makes a program (a binary package) by default. sable new geometry --lib makes a library instead, whose entry is src/lib.sb rather than src/main.sb.
Package layout#
A new binary package looks like this:
hello/├── package.sb the manifest├── .gitignore└── src/ └── main.sb the entry pointAs the package grows you add files under src/, all in the same module or in modules of their own. Other directories have meanings too: tests can go in tests/, benchmarks in benches/, and source generators in generators/. Building creates target/, which holds the build output and caches, and resolving dependencies writes sable.lock, which pins the exact version of each one so every machine builds the same thing.
The manifest#
A package is described by package.sb at its root. Unlike Cargo's TOML or a .csproj, the manifest is written in Sable itself, so it is type-checked and your editor completes it. This is what sable new writes:
import Sable.Buildlet manifest = Package { name: "hello", version: "0.1.0", edition: 2027, dependencies: [],}The file declares exactly one top-level let manifest, built from types in Sable.Build, a library that comes with the toolchain. It has no module line: each manifest is a module of its own. A manifest with dependencies looks like this:
import Sable.Buildlet manifest = Package { name: "hello", version: "0.1.0", edition: 2027, dependencies: [ .registry("sable-json", "^1.2"), .path("shared", "../shared"), .git("http", "https://github.com/example/http.git", tag: "v0.4.0"), ],}A dependency comes from a registry, from a path on disk, or from a git repository (with a tag:, branch: or rev:). You rarely edit this list by hand: sable add sable-json and sable remove sable-json change it for you, and keep your comments and layout as they are.
Although the manifest is a program, it is a restricted one. It is evaluated while the package builds, in a sandbox with no file or network access, no clock and no environment, so the same manifest always describes the same package on every machine. Within those rules you may use the whole language: lets, functions, if, loops and string interpolation, which helps when several targets share a version or a list of features.
Manifests can also declare several targets, optional dependencies, features, build profiles, build scripts, and workspaces of several packages (in a workspace.sb at the workspace root). Those are beyond a first package.
The sable command#
Here are the commands you will use most, grouped by what they are for. sable help <command> (or sable <command> --help) shows each one's options.
| Command | What it does |
|---|---|
sable new, sable init |
Start a package, in a new directory or the current one |
sable run, sable build |
Compile, and run, a package or a single file |
sable check |
Type-check without generating code: fast, and what your editor runs |
sable test |
Run @test functions; --doc also runs the examples in doc comments |
sable bench |
Run @bench functions and compare them with a baseline |
sable fuzz |
Fuzz a @fuzz function with libFuzzer |
sable fmt |
Format source code; there are no options, by design |
sable lint, sable fix |
Run the lints, and apply the fixes they suggest |
sable doc |
Generate documentation |
sable debug |
Build with debug information and run under lldb, with Sable-aware formatters |
sable repl |
An interactive session |
sable add, remove, update, tree, vendor |
Manage dependencies |
sable publish, yank, search, install |
Work with a registry |
sable explain S3057 |
Print the full explanation of a diagnostic |
sable bindgen header.h |
Write Sable declarations for a C header |
sable toolchain |
Install, list and pin toolchain versions |
sable clean |
Remove the target directory |
sable lsp |
Run the language server, for editors |
A few options work with every command: -q hides the status lines, -v also shows the commands sable runs, --color auto|always|never controls colour (auto honours NO_COLOR), --offline never reaches the network, and --message-format json prints each diagnostic as a JSON object on its own line, for editors and CI. --target <triple> cross-compiles.
Two design rules hold for all of them. Nothing in your environment changes what a build produces: the manifest, the lock file and the toolchain version decide it completely. And every command works offline once the lock file's dependencies are in the cache.
When the compiler says no#
Diagnostics show the code, where it is, and what went wrong. Here is a program that reads a variable before giving it a value:
module Hellofn main() { var total: Int print(total)}error[S6008]: 'total' is read here before anything assigns to it --> hello.sb:5:11 |5 | print(total) | ^^^^^note: 'sable explain S6008' says more about this diagnosticEvery diagnostic has a code. sable explain prints the full explanation of one, with an example of the problem and how to fix it:
sable explain S6008The same pages are in the language repository's docs/diagnostics/, and the VS Code extension offers Explain S#### as a quick fix on every diagnostic.
Some problems are found by lints rather than by the compiler proper: a var that is never changed, a result that is silently dropped. sable lint runs them, at the levels your manifest sets, and sable fix applies the fixes they suggest.
Formatting is never a matter of taste in Sable. sable fmt writes the one style (four-space indents, the opening brace on the same line, no semicolons), and sable fmt --check fails if a file is not already formatted.
Editor support#
Every editor gets the same features from the language server, sable lsp: diagnostics as you type (including the lints), completion, hover, go to definition, find references, rename, signature help, inlay hints and formatting. The server takes two settings, inlayHints.enable and lints.enable, both on by default, and serves every folder of a multi-root workspace.
Visual Studio Code#
The VS Code extension lives in the language repository's editors/vscode. Build and install it with:
cd editors/vscode && npm install && npm run packagecode --install-extension sable.vsixIt needs sable on your PATH, or the sable.path setting pointing at it, and clang. Besides the language server's features, it gives you:
- Run and Debug above
main, andCmd/Ctrl+F5to run; - the Test Explorer, with each package's tests, run or debugged from the gutter, and test coverage;
- tasks for
build,check,run,test,bench,fmtandclean, whose diagnostics reach the Problems panel; - debugging with lldb (through LLDB DAP or CodeLLDB), with formatters that show a
String's text, a list's elements and a union's case; - inlay hints for inferred types, and for the roots the compiler infers for references;
- completion and errors in
package.sbandworkspace.sb, which are Sable like any other file.
Other editors#
editors/ in the language repository has setups for other editors as well:
| Editor | Highlighting | Language server |
|---|---|---|
| Vim, Neovim | A Vim syntax file; in Neovim, the tree-sitter grammar | Neovim 0.11's vim.lsp.enable("sable"); vim-lsp in Vim |
| Helix | The tree-sitter grammar | languages.toml |
| Emacs | sable-mode |
Eglot or lsp-mode |
| Sublime Text | The TextMate grammar | The LSP package |
Any other editor that speaks LSP can run sable lsp over standard input and output. There is no Zed extension yet.
Where to go next#
A Sable Tour shows a little of every feature, enough to start writing programs. To learn the language properly, one idea at a time, start the guide with The Basics.