Variables
Variables let Wist programs name intermediate values and reuse them later.
Variable support is not part of the smallest arithmetic dialect. A dialect must select the modules that own identifiers and variable behavior.
When to read this page
Read this page when you want to write Wist programs with let, update values or pass named inputs into formula-like programs.
Goal
Understand declaration, lookup, reassignment and the module dependencies behind variable syntax.
Minimal declaration
let x = 10
xExpected result:
10The first line declares a variable. The final expression returns its value.
Variable in an expression
let x = 10
x + 5Expected result:
15This requires arithmetic, numbers, identifiers and variables.
Reassignment
A variable can be updated after declaration:
let x = 5
x = x + 1
xExpected result:
6Reassignment is useful in loops and accumulator-style programs.
Declaration depending on another variable
let x = 2
let y = x + 3
yExpected result:
5The second declaration reads the value created by the first declaration.
Required modules
A dialect with variables usually needs at least:
use Arithmetic,Numbers,Identifier,Variables,Scopes,WhitespacesIdentifier is important because variable names are not just numeric literals. If a dialect includes Variables but not the necessary identifier support, variable syntax should not be expected to work correctly.
Runtime inputs
Programmatic execution may provide named runtime inputs. For example, a pricing expression may use names such as price and fee:
price + fee * 2The runtime host must keep declared runtime inputs separate from local variables declared inside the program. Local variables should not be accidentally overwritten by unrelated external argument values.
This boundary is important for formula DSLs. Runtime bindings are part of the host boundary; let declarations are part of the program semantics.
Use before declaration
This is invalid in normal variable usage:
x
let x = 1The variable is read before it is declared.
A good dialect test suite should cover this kind of failure in both compiler and interpreter modes when both are enabled.
Variables in loops
Variables are commonly used as loop counters and accumulators:
let sum = 0
let i = 1
while i <= 5 (
sum = sum + i
i = i + 1
)
sumExpected result:
15This requires variables plus loop and comparison support. Parentheses around the while condition are optional when the condition parses as one expression node.
Common mistakes
- Adding
Variablesbut forgettingIdentifier. - Using variables in a minimal arithmetic dialect.
- Treating runtime arguments and local variables as the same storage concept.
- Testing only variable declaration but not reassignment.
- Testing compiler mode but forgetting interpreter/compiler parity for variable mutation.
Next
Continue with Conditions to use variables in branches.