Module

compiler.statements.control_flow

Control flow statement compilation for Kida compiler.

Provides mixin for compiling control flow statements (if, for).

Uses inline TYPE_CHECKING declarations for host attributes. See: plan/rfc-mixin-protocol-typing.md

Classes

ControlFlowMixin 10
Mixin for compiling control flow statements. Host attributes and cross-mixin dependencies are decl…

Mixin for compiling control flow statements.

Host attributes and cross-mixin dependencies are declared via inline TYPE_CHECKING blocks.

Methods

Internal Methods 10
_wrap_with_scope 1 list[ast.stmt]
Wrap statements with scope push/pop for block-scoped variables. Generates: …
def _wrap_with_scope(self, body_stmts: list[ast.stmt]) -> list[ast.stmt]

Wrap statements with scope push/pop for block-scoped variables.

Generates:

_scope_stack.append({})
... body statements ...
_scope_stack.pop()
Parameters
Name Type Description
body_stmts
Returns
list[ast.stmt]
_compile_break 1 list[ast.stmt]
Compile {% break %} loop control. Part of RFC: kida-modern-syntax-features.
def _compile_break(self, node: Any) -> list[ast.stmt]
Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_continue 1 list[ast.stmt]
Compile {% continue %} loop control. Part of RFC: kida-modern-syntax-features.
def _compile_continue(self, node: Any) -> list[ast.stmt]

Compile {% continue %} loop control.

Part of RFC: kida-modern-syntax-features.

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_while 1 list[ast.stmt]
Compile {% while cond %}...{% end %} loop. **Generates:** while condition: …
def _compile_while(self, node: Any) -> list[ast.stmt]

Compile {% while cond %}...{% end %} loop.

Generates: while condition: ... body ...

Part of RFC: kida-2.0-moonshot (While Loops).

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_if 1 list[ast.stmt]
Compile {% if %} conditional.
def _compile_if(self, node: Any) -> list[ast.stmt]
Parameters
Name Type Description
node
Returns
list[ast.stmt]
_uses_loop_variable 1 bool
Check if any node in the tree references the 'loop' variable. This enables laz…
def _uses_loop_variable(self, nodes: Any) -> bool

Check if any node in the tree references the 'loop' variable.

This enables lazy LoopContext optimization: when loop.index, loop.first, etc. are not used, we can skip creating the LoopContext wrapper and iterate directly over the items (16% faster per benchmark).

Parameters
Name Type Description
nodes

A node or sequence of nodes to check

Returns
bool True if 'loop' is referenced anywhere in the tree
_compile_for 1 list[ast.stmt]
Compile {% for %} loop with optional LoopContext. Generates one of two forms b…
def _compile_for(self, node: Any) -> list[ast.stmt]

Compile {% for %} loop with optional LoopContext.

Generates one of two forms based on whether loop.* is used:

When loop.* IS used (loop.index, loop.first, etc.): _iter_source = iterable _loop_items = list(_iter_source) if _iter_source is not None else [] if _loop_items: loop = _LoopContext(_loop_items) for item in loop: ... body ... else: ... empty block ...

When loop.* is NOT used (16% faster): _iter_source = iterable _loop_items = list(_iter_source) if _iter_source is not None else [] if _loop_items: for item in _loop_items: ... body ... else: ... empty block ...

Optimization: Loop variables are tracked as locals and accessed directly (O(1) LOAD_FAST) instead of through ctx dict lookup.

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_extract_names 1 list[str]
Extract variable names from a target expression.
def _extract_names(self, node: Any) -> list[str]
Parameters
Name Type Description
node
Returns
list[str]
_compile_match 1 list[ast.stmt]
Compile {% match expr %}{% case pattern [if guard] %}...{% end %}. Generates c…
def _compile_match(self, node: Any) -> list[ast.stmt]

Compile {% match expr %}{% case pattern [if guard] %}...{% end %}.

Generates chained if/elif comparisons with structural pattern matching and variable binding support.

Generates: _match_subject_N = (site.logo, site.logo_text) if isinstance(_match_subject_N, (list, tuple)) and len(_match_subject_N) == 2: logo = _match_subject_N[0] if logo: ...

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_make_pattern_match 2 tuple[ast.expr, list[tup…
Generate match test and bindings for a pattern.
def _make_pattern_match(self, pattern: Any, subject_ast: ast.expr) -> tuple[ast.expr, list[tuple[str, ast.expr]]]
Parameters
Name Type Description
pattern
subject_ast
Returns
tuple[ast.expr, list[tuple[str, ast.expr]]] (test_ast, [(name, value_ast), ...])