Language / Flow Control Overview

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 / else
  • loop loops
  • while loops
  • for loops
  • match expressions
  • return
  • break
  • continue
  • 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:

Not implemented yet (design exists, but the current parser/checker do not accept these end-to-end):

  • if as a value-producing expression form

When in doubt, consult:

Principles#

These rules help keep control flow explicit and statically checkable:

  • Conditions are boolean: if and while require a bool condition (no integer “truthiness”).
  • Bodies are blocks: flow constructs use { ... } blocks as their bodies.
  • Statements are terminated: most statement forms end with ; (for example let, 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