Module

compiler.statements.with_blocks

With-block statement compilation for Kida compiler.

Provides mixin for compiling with/endwith statements including both the simple form ({% with var=expr %}) and the conditional form ({% with expr as target %}).

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

Classes

WithBlockMixin 2
Mixin for compiling with-block statements. Host attributes and cross-mixin dependencies are declar…

Mixin for compiling with-block statements.

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

Methods

Internal Methods 2
_compile_with 1 list[ast.stmt]
Compile {% with var=value, ... %}...{% endwith %. Creates temporary variable b…
def _compile_with(self, node: With) -> list[ast.stmt]

Compile {% with var=value, ... %}...{% endwith %.

Creates temporary variable bindings scoped to the with block. We store old values and restore them after the block.

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_with_conditional 1 list[ast.stmt]
Compile {% with expr as target %}...{% end %} (conditional form). Renders body…
def _compile_with_conditional(self, node: WithConditional) -> list[ast.stmt]

Compile {% with expr as target %}...{% end %} (conditional form).

Renders body only if expr is truthy. Binds expr result to target. Supports multiple bindings and structural unpacking. Provides nil-resilience: block is silently skipped when expr is falsy.

Generates:

_with_val_N = expr
if _with_val_N:
    # [save old values]
    # [bind new values]
    ... body ...
    # [restore old values]
Parameters
Name Type Description
node
Returns
list[ast.stmt]