Language Reference
The language keywords, grammar, condition operators, and comments.
New in 4.0: system cells and layouts
A cell that gives back a u64 or i64 is a system cell. Everything in it uses that one integer type, and it compiles to machine code:
cell demo_add(a: u64, b: u64) -> u64 {
output a + b;
}Inside a system cell you can use 0x hex numbers, math and comparisons in the cell's type, band, bor and bxor, shl(a, n) and shr(a, n) (there's no << or >>), typed memory with load8 through load64 and store8 through store64, and calls to other system cells. Strings, lists and floats aren't allowed there, and the compiler tells you so.
A layout describes a piece of memory once, and Sanguis writes the cells that read and write it:
layout Demo2 {
a: u32;
b: u64;
}Demo2 is 16 bytes, and you get Demo2_size, Demo2_a(p) and Demo2_set_a(p, v). Reading past the end gives 0, so you can't fall off the edge.
Keywords
The keywords the compiler recognises today:
| Keyword | Purpose |
|---|---|
| circuit | Declare a program body |
| organ | Declare an organ: fields plus methods |
| strain | Declare a typed variant |
| cell | Declare a function, a method inside an organ or a free function |
| let | Declare a variable. The type is inferred |
| new | Construct an organ instance |
| self | The instance a method was called on |
| if / else | Branching |
| for item in list | Loop over a list |
| while | Loops |
| break | Exit a loop |
| resume | Continue a loop |
| spawn | Start a task |
| yield | Hand the turn on |
| close | End the current task |
| when | Run a block if a condition holds |
| emit / return / output | Give a value back from a cell or the program |
| signal | Send a value to the screen |
| true / false / nil | Literal values |
Grammar
program := circuit*
circuit := "circuit" IDENT ";"
organ := "organ" IDENT "{" field* cell* "}"
strain := "strain" IDENT "{" field* cell* "}"
field := IDENT ":" type ";"
cell := "cell" IDENT "(" params? ")" "->" type "{" statement* "}"
statement:= "let" IDENT "=" expr ";"
| IDENT "=" expr ";"
| "for" IDENT "in" expr "{" statement* "}"
| "while" condition "{" statement* "}"
| "break" ";"
| "resume" ";"
| "if" condition "{" statement* "}" ("else" "{" statement* "}")?
| "when" condition "{" statement* "}"
| "emit" expr ";"
| "return" expr ";"
| "output" expr ";"
| expr ";"In Sanguis 3, organs compile into the grid. Their fields hold state, and their cells become wiring. There's an organ example on the Examples page.
Condition Operators
Sanguis supports five comparison operators:
| Operator | Meaning |
|---|---|
| < | Less than |
| > | Greater than |
| == | Equal to |
| <= | Less than or equal to |
| >= | Greater than or equal to |
Operands are a variable and a number, or two variables:
if x < 5.0 // variable vs number if x > y // variable vs variable if x == 0.0 // variable vs number (equality) if x >= 3.0 // variable vs number (greater or equal) if x <= 10.0 // variable vs number (less or equal)
Arithmetic
Arithmetic works anywhere an expression does:
let sum = a + b; // addition let diff = a - b; // subtraction let prod = a * b; // multiplication let quot = a / b; // division, 0.0 when b is 0.0 let next = pos + 1.0; // increment emit a + b;
You get +, -, * and /. Dividing by zero gives 0.0. Math is worked out from left to right, and you can use numbers, variables or what a builtin gives back.
Comments
// Comments start with // and run to end of line.
Comments are ignored by the parser. Use them freely.
Builtins
A builtin is a function that comes with Sanguis. You can call one from any cell without declaring it. The Builtins page lists them all, grouped by what they do.