machdocs
Home Ecosystem Changelog

Statements

Statements are the runtime steps that fill function bodies and blocks. Every statement ends with ;, except the ones that end with a block {...}.

Conditionals: if / or

An if head opens a branch chain. Each or (cond) { ... } adds another conditional branch, and a trailing or { ... } with no condition is the catch-all. Every body is a block; there is no brace-less single-statement form.

if (cond) {
    ...
}
or (cond) {
    ...
}
or {
    ...                     # final else (no condition)
}

A condition has type u1, which comparisons, logical operators and sel produce. Any other type is refused, so an integer is compared explicitly: if (n != 0), never if (n).

Lexical payload guards

An arm whose condition is exactly sel p.c opens a payload guard for p.c inside its block. Reading, writing or taking the address of p.c is legal only inside a guard. A guard is a lexical region, not a flow fact, and there are three ways to open one:

tag Shape: u8 { dot; line: i64; square: i64; }

fun size(s: Shape) i64 {
    if (sel s.dot) {
        ret 0;
    }
    or (sel s.line) {
        ret s.line;                # guarded by the arm condition
    }
    or {
        ret s.square * s.square;   # the one case the arms left untested
    }
}
Guard limits

The body of an if (sel p.c && expr) arm is not guarded: only the right operand is. ||, ! and every other operator open no guard. Assigning a name the guarded place is spelled with, such as the i of sel items[i].value, ends the guard from that point on, for the whole of a loop that holds the assignment, so read the payload or bind it to a name first. A read after the assignment reports the guard for this payload ended at an assignment to a name in its place that runs before this point.

Loops: for

The one loop is for. The body repeats while the condition holds. There is no for-each form.

var i: i64 = 0;
for (i < 10) {
    i = i + 1;
}

A for with no condition loops until a brk or a ret leaves it.

for {
    i = i + 1;
    if (i == 3) { brk; }
}

Returning: ret

ret expr; returns a value; bare ret; returns from a void function.

ret expr;                   # return a value
ret;                        # return from a void function

Loop control: brk / cnt

brk exits the enclosing for; cnt skips to the next iteration. Either one outside any for in its function is refused.

for (i < 10) {
    i = i + 1;
    if (i == 3) { cnt; }
    if (i == 8) { brk; }
}
Note

Both are operand-less, so they are keywords only in their bare brk; / cnt; form. The same word followed by anything else is an ordinary identifier: a variable named cnt reads and assigns normally (cnt = x;), even inside a loop that also uses bare cnt; for control flow.

Deferred cleanup: fin

fin schedules a block to run when its enclosing block exits, in reverse order of declaration. It is for cleanup that should happen regardless of how the block is left.

{
    fin { counter = counter - 1; }
    fin { counter = counter * 2; }

    counter = 5;
}
# at block exit, in reverse order: counter * 2 runs first, then counter - 1, leaving 9

fin requires a block body. The single-statement form fin stmt; is refused with expected '{' to open 'fin' body.

A fin covers only the statements after it in its block. A loop body's fins replay at the end of every iteration. brk and cnt replay the fins of every block they leave, innermost first. ret expr; evaluates its expression first, then replays the fins of every open block, so a fin never changes the returned value. Control cannot leave a fin body outward: a ret inside one is refused, and so is a brk or cnt whose loop encloses the fin.

Blocks

{ ... } introduces a new lexical scope. Statements inside run in order, and a block can stand alone to bound the lifetime of locals.

{
    val tmp: i64 = compute();
    use_tmp(tmp);
}

See also