Module

compiler.stream_transform

Sync-to-stream Python AST transformer.

Transforms compiled sync block stmts (_append calls) into streaming stmts (yield expressions). This eliminates a redundant full Kida AST compilation per block by deriving the stream variant from the sync compilation output.

The transform replaces:

_append(expr)  →  yield expr
return ''.join(buf)  →  (removed)

The preamble and return/sentinel are handled by the caller, not this transform.

Uses copy-on-write: only nodes on the path to_appendcall sites are copied. Unchanged subtrees are shared with the original AST, making this safe to call multiple times on the same input (e.g. for stream + async stream variants).

Classes

_AppendToYield 2
Replace ``_append(expr)`` with ``yield expr`` in Python AST. Overrides ``generic_visit`` with a co…

Replace_append(expr) with yield exprin Python AST.

Overridesgeneric_visitwith a copy-on-write strategy: only nodes whose children actually changed are shallow-copied. Unchanged subtrees are returned as-is (identity), so the original AST is never mutated.

Methods

visit_Expr 1 ast.Expr
Transform _append(x) expression statements to yield x.
def visit_Expr(self, node: ast.Expr) -> ast.Expr
Parameters
Name Type Description
node
Returns
ast.Expr
generic_visit 1 ast.AST
Copy-on-write: only copy nodes whose children changed.
def generic_visit(self, node: ast.AST) -> ast.AST
Parameters
Name Type Description
node
Returns
ast.AST

Functions

sync_body_to_stream 1 list[ast.stmt]
Transform compiled sync body statements into streaming equivalents. Walks the …
def sync_body_to_stream(stmts: list[ast.stmt]) -> list[ast.stmt]

Transform compiled sync body statements into streaming equivalents.

Walks the AST and replaces_append(expr) calls with yield expr. Uses copy-on-write so the original stmts are never modified — safe to call repeatedly on the same input.

Parameters
Name Type Description
stmts list[ast.stmt]

Compiled Python AST statements from sync block compilation.

Returns
list[ast.stmt]