Circuits, Holds, Routes, Emits

The building blocks of a Sanguis program.

circuit

Every program is a circuit: a named body containing cells.

circuit hello;

cell main() -> float {
    print(99.0);
    emit 0.0;
}

Run it: sanguis drip hello.sanguis prints 99.0.

let

Declare a variable. Types are inferred.

circuit let_test;

cell main() -> float {
    let n = 1.0;
    emit n;
}

Emits 1.0.

cells and functions

Cells are functions. They take parameters and return values.

circuit math_test;

cell add(a: float, b: float) -> float {
    return a + b;
}

cell main() -> float {
    let x = add(1.0, 2.0);
    let y = x * 4.0;
    if y >= 10.0 {
        emit y;
    } else {
        emit 0.0;
    }
}

Emits 12.0. Control flow (if/else, while) works inside cells.

while

circuit control;

cell main() -> float {
    let counter = 0;
    while counter < 3 {
        counter = counter + 1;
        emit counter;
    }
    emit 0.0;
}

Emits 1, 2, 3, then 0.

organ: state that persists

An organ is a struct with fields and methods. It compiles into the grid: fields become wires that hold state, methods become inlined wiring.

circuit counter_demo;

organ Counter {
    count: float;

    cell new(start: float) -> Counter {
        let c = new Counter { count: start };
        output c;
    }

    cell tick(self) -> Counter {
        self.count = self.count + 1.0;
        output self;
    }
}

cell main() -> float {
    let c = Counter.new(10.0);
    c = c.tick();
    c = c.tick();
    c = c.tick();
    signal(c.count);
    output 1.0;
}

Three ticks on a Counter starting at 10. The compiled artifact declares the organ's layout up front, so the program runs without its source file.

ext: talking to the outside

ext:name reads an external input wire. This is how a program receives values from the world.

circuit bool_and_test;

cell main() -> float {
    let a = ext:a;
    let b = ext:b;
    let result = a * b;
    signal(result);
    output 1.0;
}

After bleed, run it with ad --target vulkan and the truth table settles on your GPU.

builtins

String, file, and conversion builtins work inside cells.

circuit builtin_test;

cell main() -> float {
    write_file("out.txt", "hello from sanguis");
    let contents = read_file("out.txt");
    print(contents);

    let c = concat("abc", "def");
    print(c);

    let ch = char_at("voltage", 4.0);
    print(ch);
    emit 0.0;
}

Prints the file contents, abcdef, then l.