const_fold.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:fatoptimizer 作者: vstinner 项目源码 文件源码
def visit_BinOp(self, node):
        if not self.config.constant_folding:
            return

        eval_binop = EVAL_BINOP.get(node.op.__class__)
        if not eval_binop:
            return

        if isinstance(node.op, ast.Mod):
            # FIXME: optimize str%args and bytes%args
            left_types = COMPLEX_TYPES
        else:
            left_types = None

        left = get_constant(node.left, types=left_types)
        if left is UNSET:
            return

        right = get_constant(node.right)
        if right is UNSET:
            return

        ok = self.check_binop(node.op, left, right)
        if not ok:
            return

        result = eval_binop(left, right)
        new_node = self.new_constant(node, result)
        if new_node is None:
            return

        op_str = BINOP_STR[node.op.__class__]
        self.log(node, "constant folding: replace %s %s %s with %s",
                 compact_ascii(left), op_str, compact_ascii(right),
                 compact_ascii(result), add_line=True)
        return new_node
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号