Skip to content

quivers.dsl.emit

module_to_source prints an AST Module as .qvr source in a fixed layout (module to source). Reparsing its output with quivers.dsl.loads gives the same compiled program, which makes it suited to inspecting generated programs and to testing that parsing and printing agree. Translation into other languages is transpilation.

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.
    """
    declared = [test for test in module.tests if not test.subject]
    entries = [
        (getattr(stmt, "line", 0), 0, index, _emit_statement(stmt, 0))
        for index, stmt in enumerate(module.statements)
    ]
    entries.extend(
        (test.line, 1, index, _emit_test(test)) for index, test in enumerate(declared)
    )
    entries.extend(
        (experiment.line, 1, index, _emit_experiment(experiment))
        for index, experiment in enumerate(module.experiments)
    )
    entries.sort(key=lambda entry: (entry[0] or _LAST, entry[1], entry[2]))
    return "\n\n".join(entry[3] for entry in entries) + "\n"

draw_call_to_source

draw_call_to_source(head: str, args: tuple[DrawArg, ...] | None, *, static_args: tuple[DrawArg, ...] = ()) -> str

A draw's right-hand side as QVR source.

PARAMETER DESCRIPTION
head

The family or morphism drawn from.

TYPE: str

args

The draw's runtime arguments, keyword or positional.

TYPE: tuple[DrawArg, ...] or None

static_args

Static specialization arguments, written in square brackets.

TYPE: tuple[DrawArg, ...] DEFAULT: ()

RETURNS DESCRIPTION
str

head followed by its parenthesized arguments as written, as in Dirichlet(concentration=1.0). Static arguments precede the value list, including an empty list in head[Object](). A call without either argument list emits head alone.

RAISES DESCRIPTION
EmitError

If an argument has no source spelling.

Source code in src/quivers/dsl/emit.py
def draw_call_to_source(
    head: str,
    args: tuple[DrawArg, ...] | None,
    *,
    static_args: tuple[DrawArg, ...] = (),
) -> str:
    """A draw's right-hand side as QVR source.

    Parameters
    ----------
    head : str
        The family or morphism drawn from.
    args : tuple[DrawArg, ...] or None
        The draw's runtime arguments, keyword or positional.
    static_args : tuple[DrawArg, ...]
        Static specialization arguments, written in square brackets.

    Returns
    -------
    str
        ``head`` followed by its parenthesized arguments as written,
        as in ``Dirichlet(concentration=1.0)``. Static arguments precede
        the value list, including an empty list in ``head[Object]()``.
        A call without either argument list emits ``head`` alone.

    Raises
    ------
    EmitError
        If an argument has no source spelling.
    """
    static = (
        "[" + ", ".join(_emit_draw_arg(argument) for argument in static_args) + "]"
        if static_args
        else ""
    )
    runtime = _emit_draw_args(args) if args else "()" if static_args else ""
    return f"{head}{static}{runtime}"

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)