Control Flow Constructs
Gofra provides essential control flow constructs for conditional execution and looping.
if Statement
The if statement evaluates a boolean condition and executes the block if the condition is non-zero.
Syntax
<condition> if
// code to execute if condition is true
end
Example
// This block will NOT execute
0 1 == if
"This will not be printed" print
end
// This block WILL execute
1 1 == if
"This will be printed" print
end
// Using variables
var is_valid bool
&is_valid true !<
is_valid if
"Valid state" print
end
while Statement
The while loop repeatedly executes a block of code as long as the condition remains non-zero. The loop continues until the condition evaluates to false (zero).
Syntax
while <condition> do
// code to execute repeatedly
end
Example
var counter int = 0; // Initialize counter to 0
while
counter 10 < // condition
do
"Hello! Iteration: " print
counter print_integer // Print current counter value
"\n" print
&counter copy ?> 1 + !< // Increment counter
end
Control flow pattern
while → (condition check) → do → end → while (loop back)
│
↓ (condition false)
(exit loop)
for Statement
The for loop is under the hood is same/using while construction but adds syntactical sugar for auto-constraints and counters (iterator, iterable)
At entering the loop, iterator variable will be set to low threshold of range Range is exclusive (not inclusive)
Example
for i in 0..10 do
// i will be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
end
for i in 10..0 do
// i will be [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
end
var x = 5;
for i in 0..x do // variables is allowed only on RHS for now, TODO
// Step always will be 1, reversed iterations is not possible!
// i will be [0, 1, 2, 3, 4]
end
var xs = [5, 6, 7];
for i in 0..xs do // iterate up to size of xs (index iterator)
// i will be [0, 1, 2]
end
for x in xs do // iterate iterables
// x will be [*5, *6, *7]
// Iterates over pointer as does not have iterator in simple form - shifts array pointer
end
Comparison to while HIR syntactical sugar
Something like this:
for i in 0..10 do
end
Is being translated by compiler frontend into something like
var i int = 0;
while i 10 < do // For variable range 10 is replaced ith loading variable, for reversed range, will be `>`
// body of for
&i i 1 + !< // Decrement if reversed
end