Skip to content

quivers.dsl.emit

Emission from an AST to canonical .qvr source. The emitter walks a Module; reparsing its output with quivers.dsl.loads produces the same compiled program.

emit

AST -> .qvr source emit.

Walks a quivers.dsl.ast_nodes.Module and produces canonical .qvr source text.

The emit is a one-way printer, not a panproto ParseEmitLens: quivers' AST is already the resolved form. The printer's contract is semantic: the emitted source, re-parsed by quivers.dsl.loads, produces a Module that compiles to the same program as the original AST. The printer is additionally a canonical fixed point: emit(parse(emit(ast))) == emit(ast) byte for byte.

Canonical formatting conventions:

  • 4-space indentation per nesting level.
  • One blank line between top-level statements; a single trailing newline at end of file.
  • Option blocks, constructor brace options, lists, and tuples are always emitted inline.
  • Doc comments are emitted as text lines directly above the declaration they document.
  • Numeric option values and draw-arg scalars render integral floats as X.0; constructor keyword arguments render int values without a decimal point and float values in repr form.
  • Convenience fields that mirror option-block entries (axes, via, over, reduction on program steps; level on composition rides its own option block) are not re-emitted from the mirror: the option block is the single surface source.

EmitError

Bases: Exception

Raised when an AST value has no valid .qvr surface form.

module_to_source

module_to_source(module: Module) -> str

Serialize a Module AST to canonical .qvr source.

PARAMETER DESCRIPTION
module

The module to print.

TYPE: Module

RETURNS DESCRIPTION
str

The canonical source, ending in a single newline.

RAISES DESCRIPTION
EmitError

If a node in module has no valid .qvr surface form.

Source code in src/quivers/dsl/emit.py
def module_to_source(module: Module) -> str:
    """Serialize a `Module` AST to canonical ``.qvr`` source.

    Parameters
    ----------
    module : Module
        The module to print.

    Returns
    -------
    str
        The canonical source, ending in a single newline.

    Raises
    ------
    EmitError
        If a node in ``module`` has no valid ``.qvr`` surface form.
    """
    parts = [_emit_statement(stmt, 0) for stmt in module.statements]
    return "\n\n".join(parts) + "\n"

static_argument_to_source

static_argument_to_source(argument: QiecStaticArgument) -> str

Emit one static argument of an effect or type application.

PARAMETER DESCRIPTION
argument

A type, an effect written as type syntax, or an index literal.

TYPE: QiecStaticArgument

RETURNS DESCRIPTION
str

The argument's source form, as the module emitter writes it.

Source code in src/quivers/dsl/emit.py
def static_argument_to_source(argument: QiecStaticArgument) -> str:
    """Emit one static argument of an effect or type application.

    Parameters
    ----------
    argument : QiecStaticArgument
        A type, an effect written as type syntax, or an index literal.

    Returns
    -------
    str
        The argument's source form, as the module emitter writes it.
    """
    return _emit_qiec_static_argument(argument)