Loops
Loops let Wist programs repeat a grouped body while updating local state.
Loop support is dialect-dependent. A dialect must select the module that owns loop syntax and the modules required by the loop condition and body.
When to read this page
Read this page when you want to use while or for constructs, or when you are deciding whether a DSL should allow repeated execution at all.
Goal
Understand the current loop syntax, typical accumulator examples and the modules needed for loop programs.
while loop
A while loop repeats while its condition remains satisfied:
let sum = 0
let i = 1
while i <= 5 (
sum = sum + i
i = i + 1
)
sumExpected result:
15This program sums numbers from 1 to 5.
The condition does not have to be wrapped in parentheses when it parses as one expression node. The parser builds the while node from the next AST node as the condition and the following AST node as the body. Parentheses around the condition are still allowed and can be used for readability:
while (i <= 5) (
sum = sum + i
i = i + 1
)For multi-operation loop bodies, keep the body grouped with parentheses.
Zero-iteration loop
If the condition is not satisfied initially, the body is not executed:
let marker = 17
let i = 10
while i < 10 (
marker = marker + 100
i = i + 1
)
markerExpected result:
17This is an important test case because it proves that loop entry logic is correct.
for loop
The shipped full-default example uses this form:
let sum = 0
for (let i = 1) (i <= 5) (i = i + 1) (
sum = sum + i
)
sumExpected result:
15The for loop groups initialization, condition, update and body:
for (initialization) (condition) (update) (body)Use this as the canonical documented shape for current Wist for syntax. Unlike while, the current for parser expects the initialization, condition, update and body to be grouped as scopes.
Required modules
Loop programs usually need a broader module set than arithmetic-only programs:
use Loops,Variables,Identifier,Arithmetic,Numbers,ComparisonConditions,Scopes,WhitespacesDepending on the condition expression, equality or boolean modules may also be needed.
Loops and restricted DSLs
Loops are powerful, but they broaden the runtime surface. A formula DSL for pricing, scoring or simple validation often does not need loops.
Before adding loops to a user-facing DSL, check:
- whether a loop is part of the actual product requirement;
- whether repeated execution should be bounded externally;
- whether both compiler and interpreter paths handle the loop consistently;
- whether a simpler expression or aggregate input would be safer and easier to test.
What to test
For a dialect that enables loops, test:
- normal loop execution;
- zero iterations;
- mutation of a loop variable;
- parenthesized and unparenthesized
whileconditions when both are intended to remain valid; - nested loops if they are supported by the intended dialect;
- malformed loop syntax;
- compiler/interpreter parity when both backends are exposed.
Common mistakes
- Adding
Loopswithout the variable and comparison modules required by the loop body. - Assuming
whilerequires a parenthesized condition. - Forgetting to update the loop variable.
- Using loops in a restricted formula DSL when a simpler expression would be enough.
- Testing only the happy path and missing zero-iteration behavior.
- Assuming
whileandforare available under every Wist dialect.
Next
Continue with Scopes to understand grouped expressions and variable visibility notes.