lyra.frontend package

Submodules

class lyra.frontend.cfg_generator.CFGFactory(id_gen)[source]

Bases: object

A helper class that encapsulates a partial CFG and possibly some statements not yet attached to CFG.

Whenever the method complete_basic_block() is called, it is ensured that all unattached statements are properly attached to partial CFG. The partial CFG can be retrieved at any time by property cfg.

add_stmts(stmts)[source]

Adds statements to the currently open block. :param stmts: a single statement or an iterable of statements :return:

append_cfg(other)[source]
cfg
complete_basic_block()[source]
incomplete_block()[source]
prepend_cfg(other)[source]
class lyra.frontend.cfg_generator.CFGVisitor[source]

Bases: ast.NodeVisitor

AST visitor that generates a CFG.

class NodeIdentifierGenerator[source]

Bases: object

Helper class that generates an increasing sequence of node identifiers.

next
generic_visit(node, *args, **kwargs)[source]
visit(node, *args, **kwargs)[source]

Visit an AST node.

Parameters:
  • node – node to be visited
  • arguments
    • types – dictionary mapping (variable) names to their corresponding (lyra) type
    • typ – type of the current node
Returns:

either a statement or a partial CFG, depending on the visited node

visit_AnnAssign(node, types=None, typ=None)[source]

Visitor function for an assignment with a type annotation. The attribute target stores the target of the assignment (a Name, Attribute, or Subscript). The attribute annotation stores the type annotation (a Str or Name). The attribute value opionally stores the assigned value.

visit_Assign(node, types=None, typ=None)[source]

Visitor function for an assignment. The attribute targets stores a list of targets of the assignment. The attribute value stores the assigned value.

visit_AugAssign(node, types=None, typ=None)[source]

Visitor function for an augmented assignment. The attribute target stores the target of the assignment (a Name, Attribute, or Subscript). The attributes op and value store the operation and the assigned value, respectively.

visit_BinOp(node, types=None, typ=None)[source]

Visitor function for a binary operation. The attributes op, left, and right store the operator and any expression nodes, respectively.

visit_BoolOp(node, types=None, typ=None)[source]

Visitor function for a boolean operation. The attributes op and values store the operand and a list of any expression node representing the operand involed, respectively.

visit_Break(_, types=None, typ=None)[source]
visit_Call(node, types=None, typ=None)[source]

Visitor function for a call. The attribute func stores the function being called (often a Name or Attribute object). The attribute args stores a list fo the arguments passed by position.

visit_Compare(node, types=None, typ=None)[source]

Visitor function for a comparison operation. The attributes left, ops, and comparators store the first value in the comparison, the list of operators, and the list of compared values after the first.

visit_Continue(_, types=None, typ=None)[source]
visit_Dict(node, types=None, typ=None)[source]

Visitor function for a dictionary. The attributes keys and values store lists of nodes with matching order representing the keys and the values, respectively.

visit_Expr(node, types=None, typ=None)[source]

Visitor function for an expression statement (whose return value is unused). The attribute value stored another AST node.

visit_For(node, types=None, typ=None)[source]
visit_If(node, types=None, typ=None)[source]
visit_IfExp(node, targets, op=None, types=None, typ=None)[source]

Visitor function for an if expression. The components of the expression are stored in the attributes test, body, and orelse.

visit_List(node, types=None, typ=None)[source]

Visitor function for a list. The elts attribute stores a list of nodes representing the elements. The ctx attribute is Store if the container is an assignment target, and Load otherwise.

visit_Module(node, types=None, typ=None)[source]

Visitor function for a Python module.

visit_Name(node, types=None, typ=None)[source]

Visitor function for a variable name. The attribute id stores the name as a string. The attribute ctx is Store (to assign a new value to the variable), Load (to load the value of the variable), or Del (to delete the variable).

visit_NameConstant(node, types=None, typ=None)[source]

Visitor function for True, False or None. The value attribute stores the constant.

visit_Num(node, types=None, typ=None)[source]

Visitor function for a number (integer, float, or complex). The n attribute stores the value, already converted to the relevant type.

visit_Raise(node, types=None, typ=None)[source]

Visitor function for an exception raise. The attribute exc stores the exception object to be raised (normally a Call or Name, or None for a standalone raise).

visit_Set(node, types=None, typ=None)[source]

Visitor function for a set. The elts attribute stores a list of nodes representing the elements.

visit_Str(node, types=None, typ=None)[source]

Visitor function for a string. The s attribute stores the value.

visit_Subscript(node, types=None, typ=None)[source]

Visitor function for a subscript. The attribute value stores the target of the subscript (often a Name). The attribute slice is one of Index, Slice, or ExtSlice. The attribute ctx is Load, Store, or Del.

visit_Tuple(node, types=None, typ=None)[source]

Visitor function for a tuple. The elts attribute stores a list of nodes representing the elements. The ctx attribute is Store if the container is an assignment target, and Load otherwise.

visit_UnaryOp(node, types=None, typ=None)[source]

Visitor function for a unary operation. The attributes op and operand store the operator and any expression node, respectively.

visit_While(node, types=None, typ=None)[source]
class lyra.frontend.cfg_generator.LooseControlFlowGraph(nodes: typing.Set[lyra.core.cfg.Node] = None, in_node: lyra.core.cfg.Node = None, out_node: lyra.core.cfg.Node = None, edges: typing.Set[lyra.core.cfg.Edge] = None, loose_in_edges=None, loose_out_edges=None, both_loose_edges=None)[source]

Bases: object

class SpecialEdgeType[source]

Bases: enum.Enum

An enumeration.

BREAK = 1
CONTINUE = 2
add_edge(edge)[source]

Add a (loose/normal) edge to this loose CFG.

add_node(node)[source]
append(other)[source]
both_loose_edges
combine(other)[source]
edges
eject() → lyra.core.cfg.ControlFlowGraph[source]
in_node
loose()[source]
loose_in_edges
loose_out_edges
nodes
out_node
prepend(other)[source]
replace(other)[source]
special_edges
lyra.frontend.cfg_generator.ast_to_cfg(root_node)[source]

Create the control flow graph from a ast node. :param root_node: the root node of the AST to be translated to CFG :return: the CFG of the passed AST.

lyra.frontend.cfg_generator.main(args)[source]
lyra.frontend.cfg_generator.source_to_cfg(code)[source]

Parses the given code and creates its control flow graph. :param code: the code as a string :return: the CFG of code