loki.ir.pprint module

Pretty-printer classes for IR

class Stringifier(depth=0, indent='  ', linewidth=90, line_cont=<function Stringifier.<lambda>>, symgen=<class 'str'>)

Bases: Visitor

Convert a given IR tree to a string representation.

This serves as base class for backends and provides a number of helpful routines that ease implementing automatic recursion and line wrapping. It doubles as a means to produce a human readable representation of the IR, which is useful for debugging purposes.

Parameters:
  • depth (int, optional) – The level of indentation to be applied initially.

  • indent (str, optional) – The string to be prepended to a line for each level of indentation.

  • linewidth (int, optional) – The line width limit after which to break a line.

  • line_cont (optional) – A function handle that accepts the current indentation string (Stringifier.indent) and returns the string for line continuation. This is inserted between two lines when they need to wrap to stay within the line width limit. Defaults to newline character plus indentation.

  • symgen (optional) – A function handle that accepts a pymbolic.primitives.Expression and produces a string representation for that.

property symgen

Formatter for expressions.

property indent

Yield indentation string according to current depth.

Returns:

A string containing indent * depth.

Return type:

str

static join_lines(*lines)

Combine multiple lines into a long string, inserting line breaks in between. Entries that are None are skipped.

Parameters:

lines (list) – The lines to be combined.

Returns:

The combined string or None if an empty list was given.

Return type:

str or None

join_items(items, sep=', ', separable=True)

Concatenate a list of items into JoinableStringList.

The return value can be passed to format_line() or format_node() or converted to a string with str, using the JoinableStringList as an argument. Upon expansion, lines will be wrapped automatically to stay within the linewidth limit.

Parameters:
  • items (list) – The list of strings to be joined.

  • sep (str, optional) – The separator to be inserted between items.

  • separable (bool, optional) – Allow line breaks between individual items.

Return type:

JoinableStringList

format_node(name, *items)

Default format for a node.

Creates a string of the form <name[, attribute, attribute, ...]>.

format_line(*items, comment=None, no_wrap=False, no_indent=False)

Format a line by concatenating all items and applying indentation while observing the allowed line width limit.

Note that the provided comment will simply be appended to the line and no line width limit will be enforced for that.

Parameters:
  • items (list) – the items to be put on that line.

  • comment (str) – an optional inline comment to be put at the end of the line.

  • no_wrap (bool) – disable line wrapping.

  • no_indent (bool) – do not apply indentation.

Returns:

the string of the current line, potentially including line breaks if required to observe the line width limit.

Return type:

str

visit_all(item, *args, **kwargs)

Convenience function to call visit() for all given arguments.

If only a single argument is given that is iterable, visit() is called on all of its elements instead.

visit_Module(o, **kwargs)

Format a Module as

<repr(Module)>
  ...spec...
  ...routines...
visit_Subroutine(o, **kwargs)

Format a Subroutine as

<repr(Subroutine)>
  ...docstring...
  ...spec...
  ...body...
  ...members...
visit_Node(o, **kwargs)

Format a Node as

<repr(Node)>
visit_Expression(o, **kwargs)

Dispatch routine to expression tree stringifier Stringifier.symgen.

visit_tuple(o, **kwargs)

Recurse for each item in the tuple and return as separate lines.

visit_list(o, **kwargs)

Recurse for each item in the tuple and return as separate lines.

visit_InternalNode(o, **kwargs)

Format InternalNode as

<repr(InternalNode)>
  ...body...
visit_Conditional(o, **kwargs)

Format Conditional as

<repr(Conditional)>
  <If [condition]>
    ...
  <Else>
    ...
visit_MultiConditional(o, **kwargs)

Format MultiConditional as

<repr(MultiConditional)>
  <Case [value(s)]>
    ...
  <Case [value(s)]>
    ...
  <Default>
    ...
pprint(ir, stream=None)

Pretty-print the given IR using Stringifier.

Parameters:
  • ir (Node) – The IR node starting from which to print the tree

  • stream (optional) – If given, call Stringifier.write() on this stream instead of sys.stdout