A statically typed, design-by-contract programming language for low-level control.
Built around verifiable behaviour
Spectre is a statically typed, DbC programming language built for low-level control in combination with verifiably safe constructs.
It features a contract system integrated into the language grammar. Functions carry named preconditions and postconditions that participate in compilation and, depending on their form, in the generated binary. The programming language offers explicit control over which checks survive into release builds through the use of the guarded keyword. More notably there is a type-level invariant system, wherein conditions in type invariants are evaluated at compile time.
Spectre compiles to QBE IR which lowers to assembly, though there are also experimental LLVM and C99 backends as per v0.0.5.
tagged unions and pattern matching
val math = use("std/math")
union Shape = {
Circle(f64) | Rect(f64, f64) | Triangle(f64, f64, f64)
}
fn area(s: Shape) f64 = {
match s {
Circle r => { return 3.14159 * r * r }
Rect w, h => { return w * h }
Triangle a, b, c => {
val sp = (a + b + c) / 2.0
return math.sqrt(sp * (sp - a) * (sp - b) * (sp - c))
}
}
}
pre/postconditions and guarded checks
/// Return bytes remaining. Subtraction is safe because used <= cap.
pub fn (Arena) remaining(arena: self) usize = {
pre {
used_le_cap : trust @load(@ptradd(arena, 8)) <= trust @load(@ptradd(arena, 16))
}
val off: usize = trust @load(@ptradd(arena, 8))
val cap: usize = trust @load(@ptradd(arena, 16))
post {
result_le_cap : off <= cap
}
return cap - off
}
/// Open a file; mode must be non-empty in all build configurations.
pub fn open(path: ref char, mode: ref char) option[ref void] = {
guarded pre {
mode_nonempty : mode != ""
}
val f: ref void = trust fopen(path, mode)
if f == none { return none }
return some f
}
Installation
Run the install script for your platform. The installer sets up the compiler, adds spectre to PATH, and verifies the build. Requires git and cmake.
POSIX (Linux, MacOS, FreeBSD, etc...)
curl https://spectrelang.org/get.sh | sh
Windows
irm https://spectrelang.org/get.ps1 | iex
Built with Spectre
Real projects written in Spectre, demonstrating its range from hardware-level emulation to developer tooling.
A complete CHIP-8 emulator using SDL2 for display and input. Implements the full instruction set including the original COSMAC VIP opcodes, with configurable CPU speed for compatibility across ROM sets.
A devlogging CLI tool that converts .md files into modern HTML pages. Drives the Spectre devlog.
phantom
A HTTP/1.1 server with no runtime dependencies beyond the standard library. Built to demonstrate Spectre's suitability for network programming, contract-verified buffer handling, explicit allocation, and predictable latency.