machdocs
Home Ecosystem Changelog

Expressions

Expressions evaluate to values. They appear on the right of a binding, as conditions, and as call arguments.

Reading an aggregate captures its value at that evaluation point, so a later argument, assignment destination or fin body cannot change the captured value by modifying its storage. Call arguments evaluate left to right. Assignment evaluates and captures the right side before evaluating the destination.

Names

A bare identifier references a name in scope. Module-qualified names use the dot path.

counter        # local or module-level binding
core.add       # symbol from module core

Literals

The primitive literal forms are numeric, char, string, and nil.

42         # numeric literal
'a'        # char literal
"hello"    # string literal
nil        # the nil value

Composite literals

A type name followed by a brace-delimited initializer builds a record, array, union, or tag value. For generics, the type arguments appear in brackets before the body.

val p:    Point         = Point{ x: 1, y: 2 };
val a:    [3]i64        = [3]i64{ 10, 20, 30 };
val u:    Number        = Number{ i: 99 };
val pair: Pair[i64, u8] = Pair[i64, u8]{ left: 5, right: 6u8 };

A tag value specifies the case name before the braces. A payload case supplies its value inside the braces, while a unit case uses empty braces.

val s: Shape = Shape.circle{4.5};
val p: Shape = Shape.point{};

A vector literal takes the same shape and is full-arity: one initializer per lane, with too few or too many a compile error. See Types.

val v: f32x4 = f32x4{1.0, 2.0, 3.0, 4.0};

Field, index, and tag access

A field is read with the dot, an array element with a bracketed index.

val x:     i64 = p.x;     # record field
val first: i64 = a[0];    # array index

A constant index is bounds-checked against a statically known length, such as an array's N or a vector's lane count. x[start, count] is a range of count consecutive elements from start, as a [count]T value, where count is a comptime constant of at least 1.

A tag payload is read with place.case, which is legal only inside a lexical guard for that place and case. TypeName.case alone is a case selector, not a value, and cannot be stored or passed. In comptime reflection over $cases(T), a tag case can also be accessed through place.[case].

Function calls

A call applies a function to a parenthesized argument list.

add(2, 3)

Generic calls

Type arguments for a generic function appear in brackets before the call parentheses.

identity[i64](42)     # generic call: type args in [ ]

Variadic calls

A call to a function with a pack tail (va: ...) passes extra trailing arguments past the fixed parameters. The call is monomorphized per distinct list of trailing types, and g(va...) forwards a whole pack. A suffix types a literal where nothing else does. See Variadic packs.

sum(3, 10i64, 20i64, 30i64)

Comptime arguments

A comptime argument is passed positionally, just like a runtime argument. The function signature decides whether a given argument must be comptime-knowable.

checked_add(MODE_FAST, 1, 2)    # MODE_FAST is comptime-knowable

Operators

Operators combine expressions into larger expressions. Multiplication binds tighter than addition, and comparison binds tighter than &, ^ and |, so a bit test is parenthesized.

val n: i64 = a + b * c;    # * binds tighter than +
if ((x & 0x0f) != 0) { }     # x & 0x0f != 0 would be x & (0x0f != 0)

Comparison

== != < > <= >= produce a u1, and they relate mathematical values. Integers of any signedness and width compare by their true values, so a negative i64 is never equal to, and always less than, any u64. Floats of any width compare after the narrower widens exactly. An integer against a float is a compile error: cast one operand with ::.

Pointers compare as addresses. A string is a *u8, so == on two strings compares where they are, never their bytes: use std.types.string.str_equals for the contents. == and != on a whole rec, uni or tag value are refused, since comparing representations would relate padding bytes and inactive cases:

if (p == q) { }     # error: cannot compare `Point` values with `==` or `!=`. compare their fields or elements, or use `std.derive.eq`

An operator over unsuffixed literals takes the type of its context and wraps there, so val max: u8 = 0 - 1; is 255. A suffixed literal is that type and is range-checked against it: 256u8 and -1u32 are refused.

Lane-wise operators

Vector types carry lane-wise operators at every lane count, not only the 128-bit shapes, and which operators are legal is target-independent: an f32x8 add is as legal as an f32x4 one, and the two differ only in how they are realized.

Lane family+ -*/%& | ^ ~<< >>comparisons
floatyesyesyesno-nosame-shape unsigned mask
integeryesyesyesnoyesyes, by a same-shape countsame-shape unsigned mask

Both operands must be the same vector shape: there is no implicit scalar-to-vector mixing and no cross-shape widening. Anything the table marks no is a compile error, not a silent fallback. A shift's count is a vector of the shifted type, so v << 3 is an error and v << u32x4{3, 3, 3, 3} is the spelling. An operation the target computes lane by lane, such as an integer vector divide, warns at its site under vector.scalarize in a build with debug information.

A comparison produces the same-shape unsigned mask vector - one lane per input lane, all-ones bits for true and all-zeros for false, exactly what the hardware compare yields. There is no vector-bool type, and select is not an operator: it is the library idiom (mask & a) | (~mask & b) over matching integer lanes.

val a: f32x4 = f32x4{1.0, 2.0, 3.0, 4.0};
val b: f32x4 = f32x4{4.0, 3.0, 2.0, 1.0};
val sum:  f32x4 = a + b;       # lane-wise -> {5.0, 5.0, 5.0, 5.0}
val mask: u32x4 = a < b;       # -> {0xFFFFFFFF, 0xFFFFFFFF, 0, 0}

Case selection: sel

The sel place.case expression tests the active case of a tag. It reads only the discriminator and is an ordinary u1, so it can be stored, negated, or combined with && and ||. In comptime reflection over $cases(T), sel place.[case] tests the reflected case.

if (sel r.ok) {
    val value: i64 = r.ok;
}

Declassification: :>T

The declassify operator x:>T converts a secret value of type ^T into a public value of type T. The public destination type must always be written explicitly. See Secrecy.

val diff: ^u64 = ct_diff(secret, given);
val ok:   bool = (diff:>u64) == 0;

See also