Module

compiler.statements.pattern_matching

Pattern matching statement compilation for Kida compiler.

Provides mixin for compiling match/case statements.

Extracted from control_flow.py for module focus (RFC: compiler decomposition). Uses inline TYPE_CHECKING declarations for host attributes. See: plan/rfc-mixin-protocol-typing.md

Classes

PatternMatchingMixin 3
Mixin for compiling match/case statements. Host attributes and cross-mixin dependencies are declar…

Mixin for compiling match/case statements.

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

Methods

Internal Methods 3
_compile_match 1 list[ast.stmt]
Compile {% match expr %}{% case pattern [if guard] %}...{% end %}. Generates c…
def _compile_match(self, node: Match) -> 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: ...

Valueless match (switch-true): {% match %} {% case _ if user.is_admin %}Admin {% case _ %}Member {% end %}

When subject is None, generates pure if/elif/else using guard expressions only.

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_valueless_match 1 list[ast.stmt]
Compile valueless {% match %} as pure if/elif/else chain. Each case must use w…
def _compile_valueless_match(self, node: Match) -> list[ast.stmt]

Compile valueless {% match %} as pure if/elif/else chain.

Each case must use wildcard pattern (_) with an optional guard. Non-wildcard patterns are a compile error since there's no subject.

{% match %} {% case _ if x > 0 %}positive {% case _ if x == 0 %}zero {% case _ %}negative {% end %}

Generates:

if x > 0:
    _append('positive')
elif x == 0:
    _append('zero')
else:
    _append('negative')
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: Node, 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), ...])