Circuits, Organs, Cells, Emits
The building blocks of a Sanguis program.
The kit's examples
These come in the 4.0 kit, and every output below is from a real run. Run them as they are first. Then try breaking one and running sanguis verify. It'll tell you which step changed.
| Example | What it shows |
|---|---|
| hello.sanguis | Prints one line. |
| cellsdemo.sanguis | Every kind of cell, each with an answer we already know. It packs into a native module of 12 cells, 992 bytes, and the test driver says 10 tests, 0 failures. |
| roles.sanguis | All five jobs a cell can take on: stem, tagger, pruner, helper and immune guard. The last line is t6 all tasks ended ok. |
| shader.sanguis | The Mandelbrot set, drawn with 64-bit whole numbers. We check three frames against a separate C version, and they match: probe mismatches 0. |
| divide_probe.sanguis | Prints how integer division, the bias and shifts really behave, signed and unsigned. |
circuit
Every program is a circuit. A circuit has a name, and it holds your cells.
circuit hello;
cell main() -> float {
print(99.0);
emit 0.0;
}Run it: sanguis drip hello.sanguis prints 99.
let
Makes a variable. You don't need to say its type; Sanguis works it out.
circuit let_test;
cell main() -> float {
let n = 1.0;
emit n;
}Emits 1.
cells and functions
Cells are functions. You give them values, and they give one back.
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. Control flow (if/else, while, for, when) works inside cells.
while
circuit control;
cell main() -> float {
let counter = 0;
while counter < 3 {
counter = counter + 1;
print(counter);
}
emit 0.0;
}Prints 1, 2, 3, then the 0 it emitted. Use print inside a loop, because emit gives its value back right away and ends the cell.
organ: state that persists
An organ is like a struct: it has fields, and cells that work on them. In Sanguis 3 it compiles into the grid, where its fields become wires that hold their values.
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;
}A Counter starts at 10 and tick runs three times. The compiled file describes the organ at the top, so it runs without needing the source.
ext: talking to the outside
ext:name reads a value from outside the program. It's how a program hears 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 sanguis bleed, run it with sanguis drip.
builtins
You can use the text, file and conversion builtins inside any cell.
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 a.