Spectre

The Spectre Cube...

A statically typed, design-by-contract programming language for building fast, scalable, and maintainable systems.

Install - POSIX (Linux, MacOS, FreeBSD, etc...)

curl https://spectrelang.org/get.sh | sh

Install - Windows

irm https://spectrelang.org/get.ps1 | iex

Spectre features a contract system integrated into the language grammar, an explicit allocatorized API for low-level control with no implicit allocations, and performance gains of up to 6x that of Zig and Rust in general purpose tasks, primarily due to autovectorization, SIMD instruction injection per-platform, and a highly optimized standard library. For DbC, the programming language offers explicit control over which checks survive into release builds through the use of the guarded construct, though in most cases contract verification is done at compile time.

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. To accomodate for function-level contracts, 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. Windows is supported via an MSVC backend, though can also work under MinGW, Clang, and TCC (though TCC will always produce dynamically linked binaries, thus is never the default). The QBE backend has been forked for the purpose of optimizing Spectre's default code generation further.

val netx       = use("std/netx")
val allocators = use("std/allocators")
val stdio      = use("std/stdio")

fn hello(_: ref netx.Request, res: mut ref netx.Response) void = trust {
    netx.Response.json(
        res,
        \\{
        \\  "header": "Greeting",
        \\  "message": "Hello, world."
        \\}
    )
}

pub fn main() void = trust {
    val ac: allocators.HeapAllocator = @zeroed
    val app: mut netx.WebServer = netx.WebServer.new[_](ac, 8080 as i32)
    if app == none {
        stdio.puts("Failed to start server")
        return
    }
    netx.WebServer.get(app, "/", hello)
    netx.WebServer.serve(app)
}
val stdio = use("std/stdio")

pub type Percentage = {
    value: f64

    invariant {
        value >= 0.0
        value <= 100.0
    }
}

pub fn (Percentage) halve_gt_one(p: self) f64 = {
    guarded pre {
        over_one: p.value > 1.0
    }
    return p.value / 2.0
}

pub fn main() void = {
    val p: Percentage = {value: 75.0}
    trust {
        stdio.print("percentage: {f}\n", {p.value})
        stdio.print("half: {f}\n", {Percentage.halve_gt_one(p)})
    }
}
val stdio = use("std/stdio")

union Payload = {
    Int(i32) | BigInt(i64) | Str(ref u8)
}

fn describe(x: Payload) void = {
    match x {
        Int _ => {
            stdio.puts("32-bit integer")
        }
        BigInt _ => {
            stdio.puts("64-bit integer")
        }
        else => {
            stdio.puts("string pointer")
        }
    }
}

pub fn main() void = {
    describe(Int(12 as i32))
}
val ostr  = use("std/string/owned")
val alloc = use("std/allocators")
val stdio = use("std/stdio")
val str   = use("std/string")

fn normalize_path[A: alloc.Allocator](ac: A, raw: ref u8) str.String = {
    val path: mut str.StringView = str.from_cstr(raw)
    val path_str: mut str.String = @zeroed
    trust {
        path = str.trim(path)
        path_str = str.to_lower[A](ac, path)
    }
    val result = trust ostr.trim_prefix(path_str, ostr.from_cstr[A](ac, c"/api/"))
    post {
        result.len > 0
    }
    return result
}

pub fn main() void = trust {
    val arena: alloc.Arena = alloc.Arena.new(1024 as usize)?
    val route = normalize_path[alloc.Arena](arena, c"  /API/Users/42  ")
    stdio.print("route: {s}\n", {route})
}

Built with Spectre

  • chip8emu screenshot

    chip8emu

    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.

  • 3D pacman running on exo game engine

    exo

    A 3D game engine written from scratch in Spectre, leveraging SDL3, complete with a full post-processing stack, event loop, audio, and input handling.

  • Spectre mascot

    spectre

    The Spectre compiler itself is written in Spectre.

  • Formant

    formant

    A cross-platform UI framework for seamless deployment of native-looking apps across all major desktops.

  • blood screenshot

    blood

    A high performance, modern shell. Outperforms ksh, bash, dash and zsh by several orders of magnitude.

  • cubedoom screenshot

    cubedoom

    A singleplayer arena shooter using raycasting rendering, uses SDL2 for graphics and input handling.

  • sunmap screenshot

    sunmap

    A 3D map editor with an intuitive UI, support for Quake WAD imports, and Valve 220 .map file imports. Along with export to glTF, GLB, and WaveFront OBJ 3D formats.

  • deadzone screenshot

    deadzone

    A singleplayer FPS used to demonstrate the surface-level technical capabilities of the Exo game engine.

  • md2html screenshot

    md2html

    A devlogging CLI tool that converts markdown files into modern HTML pages. Powers the Spectre devlog.