Flow Control Overview
Flow control describes how Silk programs sequence work, branch, loop, and exit. This concept spans several surface constructs and their static rules (typing, scoping, and diagnostics).
Core Constructs#
if/elselooploopswhileloopsforloopsmatchexpressionsreturnbreakcontinue- blocks and statement composition
- expression statements
Each construct has defined syntax, typing, and evaluation semantics which the compiler must implement.
Notes#
Implemented end-to-end in the current compiler:
if/elseas statement forms (flow if else)looploops (flow loop)whileloops (flow while)forloops (ranges, builtin arrays/slices, and C-stylefor (init; cond; step); flow for)break/continueinside loops (flow break, flow continue)returnstatements, including “all paths must return” checking for non-voidfunctions (flow return)matchas an expression for optionals and enums (flow match)matchas a statement for typed errors (typed errors)- Expression statements for calls and assignments only (flow expression statements)
Not implemented yet (design exists, but the current parser/checker do not accept these end-to-end):
ifas a value-producing expression form
When in doubt, consult:
- implementation status (implementation snapshot)
- diagnostics (error codes)
Principles#
These rules help keep control flow explicit and statically checkable:
- Conditions are boolean:
ifandwhilerequire aboolcondition (no integer “truthiness”). - Bodies are blocks: flow constructs use
{ ... }blocks as their bodies. - Statements are terminated: most statement forms end with
;(for examplelet,return,break,continue,panic,assert, and expression statements).
Quick Examples#
Branching:
fn main () -> int {
let x: int = 1;
if x == 0 {
return 0;
} else {
return 1;
}
}
Looping:
fn main () -> int {
let mut i: int = 0;
while i < 3 {
i += 1;
}
return 0;
}
Matching:
fn main () -> int {
let x: int? = Some(7);
let y: int = match x {
None => 0,
Some(v) => v,
};
return y;
}
See the dedicated documents:
Source repository · Edit this page · View Markdown