Control Flow
In other stack-based languages, control flow is handled within the stack, leading to some questionable syntax (looking at you, Forth). Henceforth fixes this by handling it imperatively, leading to a more familiar way of doing things.
While loops
while is the only looping construct in Henceforth (there are no for loops).
They are used in the following way:
while @(<condition>) <optional function calls> {
<body>
}
The condition is a stack block, optionally followed by function calls (e.g. while @(x) &> is_positive {), and it must leave a bool on top of the stack. The body repeats while that bool is true. Unlike in Forth, the bool is popped automatically, so the body never sees it.
Warning
A
whileloop must not change the stack. At the end of every iteration, the stack must have the same depth and types as it had at the start, since the next iteration begins from there.
In practice, anything the body produces has to be consumed before the next iteration, usually by moving it into a variable:
fn less_than_ten: (i32) -> (bool) { @(10 <) }
fn sum_below_ten: () -> (i32) {
let i: i32; @(0) &= i;
let sum: i32; @(0) &= sum;
while @(i) &> less_than_ten {
@(sum i +) &= sum; // the result is moved into `sum`, so the stack is unchanged
@(i 1 +) &= i;
}
@(sum);
}
The following keywords can be used inside while loops to change its execution:
break- leaves the loop entirelycontinue- skips the rest of the body’s execution, starting the next iteration
If statements
if statements can be used to conditionally perform operations.
They are used in the following way:
if @(<condition1>) <optional function calls> {
<if-body>
} else if @(<condition2>) <optional function calls> {
<else-if-body>
} else if ... {
...
} else {
<else-body>
}
As in while, each condition is a stack block, optionally followed by function calls, that leaves a bool, which is popped automatically.
Since conditions consume their operands, what the stack looks like when a body starts matters:
- Every condition is evaluated starting from the stack as it was at the beginning of the
ifstatement. If the condition isfalse, the stack is restored to that state and the next condition is tried. - If a condition is
true, itsboolis popped and the body runs on whatever the condition left behind. - The
elsebranch has no condition, so it runs on the stack as it was at the beginning of theifstatement. - All branches must end with the same stack (same depth and types).
Note
An
ifwithout anelsemust leave the stack as it was at the beginning of the statement, since the “nothing happened” path has to match.
Example
This all may seem very confusing, let’s look at a simple FizzBuzz program.
fn fizz_buzz: (i32) -> (str) {
if @(15 % 0 ==) {
@("fizzbuzz");
} else if @(3 % 0 ==) {
@("fizz");
} else if @(5 % 0 ==) {
@("buzz");
} else {
@pop;
@("no fizzbuzz");
}
}
Let’s trace the program’s execution:
- On entering the
ifstatement, the stack contains only the argument:(n). - The first condition runs.
15 %consumesn, so the stack ends up as just(bool). - If the condition was
true, theboolis popped, leaving(). The body pushes"fizzbuzz", so the stack is(str), which matches the return type. - If it was
false, the stack is restored to(n)and the next condition is evaluated the same way. - Reaching the
else, there is no condition, so the stack is still(n). This is why only theelseneeds a@pop: the conditions consumed the argument, but theelsenever ran one. We popn, push"no fizzbuzz", and end with(str)as expected.
Summary
- Every
ifcondition starts from the stack at the beginning of the statement, and afalsecondition restores it. - A
truebranch starts from what its condition left, minus thebool. Theelsestarts from the stack at the beginning of the statement. - All branches of an
ifstatement must end with the same stack. - A
whileloop must end every iteration with the stack it started with.