Module opshin.optimize.optimize_fold_bool
Expand source code
from ast import *
from opshin.util import CompilingNodeTransformer
from opshin.rewrite.rewrite_cast_condition import SPECIAL_BOOL
from opshin.type_impls import BoolType, InstanceType
"""
Pre-evaluates ~bool to constants
"""
class OptimizeFoldBoolCast(CompilingNodeTransformer):
step = "Removing ~bool calls to bools"
def visit_Call(self, node: Call) -> Call:
if (
isinstance(node.func, Name)
and hasattr(node.func, "orig_id")
and node.func.orig_id == SPECIAL_BOOL
):
if len(node.args) != 1:
return self.generic_visit(node)
arg = self.visit(node.args[0])
if isinstance(arg.typ, InstanceType) and isinstance(arg.typ.typ, BoolType):
return arg
return self.generic_visit(node)
Classes
class OptimizeFoldBoolCast-
A :class:
NodeVisitorsubclass that walks the abstract syntax tree and allows modification of nodes.The
NodeTransformerwill walk the AST and use the return value of the visitor methods to replace or remove the old node. If the return value of the visitor method isNone, the node will be removed from its location, otherwise it is replaced with the return value. The return value may be the original node in which case no replacement takes place.Here is an example transformer that rewrites all occurrences of name lookups (
foo) todata['foo']::class RewriteName(NodeTransformer):
def visit_Name(self, node): return Subscript( value=Name(id='data', ctx=Load()), slice=Constant(value=node.id), ctx=node.ctx )Keep in mind that if the node you're operating on has child nodes you must either transform the child nodes yourself or call the :meth:
generic_visitmethod for the node first.For nodes that were part of a collection of statements (that applies to all statement nodes), the visitor may also return a list of nodes rather than just a single node.
Usually you use the transformer like this::
node = YourTransformer().visit(node)
Expand source code
class OptimizeFoldBoolCast(CompilingNodeTransformer): step = "Removing ~bool calls to bools" def visit_Call(self, node: Call) -> Call: if ( isinstance(node.func, Name) and hasattr(node.func, "orig_id") and node.func.orig_id == SPECIAL_BOOL ): if len(node.args) != 1: return self.generic_visit(node) arg = self.visit(node.args[0]) if isinstance(arg.typ, InstanceType) and isinstance(arg.typ.typ, BoolType): return arg return self.generic_visit(node)Ancestors
- CompilingNodeTransformer
- TypedNodeTransformer
- ast.NodeTransformer
- ast.NodeVisitor
Class variables
var step
Methods
def visit(self, node)-
Inherited from:
CompilingNodeTransformer.visitVisit a node.
def visit_Call(self, node: ast.Call) ‑> ast.Call-
Expand source code
def visit_Call(self, node: Call) -> Call: if ( isinstance(node.func, Name) and hasattr(node.func, "orig_id") and node.func.orig_id == SPECIAL_BOOL ): if len(node.args) != 1: return self.generic_visit(node) arg = self.visit(node.args[0]) if isinstance(arg.typ, InstanceType) and isinstance(arg.typ.typ, BoolType): return arg return self.generic_visit(node)