Conditionals
If-Else
if-else-expression ::= if $expression { $expression } [ else ( { $expression } | $if-else-expression ) ]
The expression if e1 { e2 } else { e3 } selects a branch using the result of e1. This result must already be available and must still be valid. If it is all zero, evaluation starts in e3; otherwise, it starts in e2. The expression returns the selected branch’s result.
The else clause is optional: if e1 { e2 } is equivalent to if e1 { e2 } else { () }. Multiple conditionals can also be chained, for example:
if e1 { e2 } else if e3 { e4 } else { ... }.
For example:
On each iteration, the program prints whether the current value of counter is even or odd. The even branch introduces a delay of three cycles, and the odd branch introduces a delay of one cycle. Branches can therefore take different times to complete. Anvil’s semantics and type system support this behavior.
Match
Match expressions provide a pattern-matching primitive.
match-expression ::= match $expression { ($expression | _) => $expression {, ($expression | _) => $expression } }
The expression
match e { e1 => e1', e2 => e2', ..., en => en', _ => e' }
is syntax sugar for:
if e == e1 { e1' } else if e == e2 { e2' } else if ... else if e == en { en' } else { e' }
The _ => e' (default branch) must appear exactly once in the match expression.
For example:
This program prints whether the current cycle (value of counter) is 0, 1, 2, or Many in each iteration of the loop.