Python ast.BitXor() Examples

The following are 21 code examples of ast.BitXor(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ast , or try the search function .
Example #1
Source File: helper.py    From YAPyPy with MIT License 6 votes vote down vote up
def augassign_rewrite(it: Tokenizer):
    return {
        '+=': ast.Add,
        '-=': ast.Sub,
        '*=': ast.Mult,
        '/=': ast.Div,
        '//=': ast.FloorDiv,
        '@=': ast.MatMult,
        '%=': ast.Mod,
        '&=': ast.BitAnd,
        '|=': ast.BitOr,
        '^=': ast.BitXor,
        '<<=': ast.LShift,
        '>>=': ast.RShift,
        '**=': ast.Pow,
    }[it.value] 
Example #2
Source File: asttools.py    From sspam with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def visit_BoolOp(self, node):
        'A custom BoolOp can be used in flattened AST'
        if type(node.op) not in (ast.Add, ast.Mult,
                                 ast.BitXor, ast.BitAnd, ast.BitOr):
            return self.generic_visit(node)
        # get constant parts of node:
        list_cste = [child for child in node.values
                     if isinstance(child, ast.Num)]
        if len(list_cste) < 2:
            return self.generic_visit(node)
        rest_values = [n for n in node.values if n not in list_cste]
        fake_node = Unflattening().visit(ast.BoolOp(node.op, list_cste))
        fake_node = ast.Expression(fake_node)
        ast.fix_missing_locations(fake_node)
        code = compile(fake_node, '<constant folding>', 'eval')
        obj_env = globals().copy()
        exec code in obj_env
        value = eval(code, obj_env)

        new_node = ast.Num(value)
        rest_values.append(new_node)
        return ast.BoolOp(node.op, rest_values) 
Example #3
Source File: asttools.py    From sspam with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def visit_BinOp(self, node):
        'Replace bitwise operation with function call'
        self.generic_visit(node)
        if isinstance(node.op, ast.BitAnd):
            return ast.Call(ast.Name('mand', ast.Load()),
                            [node.left, node.right], [], None, None)
        if isinstance(node.op, ast.BitOr):
            return ast.Call(ast.Name('mor', ast.Load()),
                            [node.left, node.right], [], None, None)
        if isinstance(node.op, ast.BitXor):
            return ast.Call(ast.Name('mxor', ast.Load()),
                            [node.left, node.right], [], None, None)
        if isinstance(node.op, ast.LShift):
            return ast.Call(ast.Name('mlshift', ast.Load()),
                            [node.left, node.right], [], None, None)
        if isinstance(node.op, ast.RShift):
            return ast.Call(ast.Name('mrshift', ast.Load()),
                            [node.left, node.right], [], None, None)
        return node 
Example #4
Source File: asttools.py    From sspam with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def visit_BinOp(self, node1, node2):
        'Check type of operation and operands'
        if type(node1.op) != type(node2.op):
            return False

        # if operation is commutative, left and right operands are
        # interchangeable
        cond1 = (self.visit(node1.left, node2.left) and
                 self.visit(node1.right, node2.right))
        cond2 = (self.visit(node1.left, node2.right)
                 and self.visit(node1.right, node2.left))

        # non-commutative comparator
        if not self.commut:
            return cond1
        if isinstance(node1.op, (ast.Add, ast.Mult,
                                 ast.BitAnd, ast.BitOr, ast.BitXor)):
            if cond1 or cond2:
                return True
            else:
                return False
        else:
            if cond1:
                return True
        return False 
Example #5
Source File: _toVHDL.py    From myhdl with GNU Lesser General Public License v2.1 5 votes vote down vote up
def visit_BinOp(self, node):
        if isinstance(node.op, (ast.LShift, ast.RShift)):
            self.shiftOp(node)
        elif isinstance(node.op, (ast.BitAnd, ast.BitOr, ast.BitXor)):
            self.BitOp(node)
        else:
            self.BinOp(node) 
Example #6
Source File: hgawk_grammar.py    From histogrammar-python with Apache License 2.0 5 votes vote down vote up
def p_xor_expr_star_2(p):
    '''xor_expr_star : xor_expr_star CIRCUMFLEX and_expr'''
    #                              1          2        3
    p[0] = p[1] + [ast.BitXor(rule=inspect.currentframe().f_code.co_name, **p[2][1]), p[3]]

# and_expr: shift_expr ('&' shift_expr)* 
Example #7
Source File: hgawk_grammar.py    From histogrammar-python with Apache License 2.0 5 votes vote down vote up
def p_xor_expr_star_1(p):
    '''xor_expr_star : CIRCUMFLEX and_expr'''
    #                           1        2
    p[0] = [ast.BitXor(rule=inspect.currentframe().f_code.co_name, **p[1][1]), p[2]] 
Example #8
Source File: hgawk_grammar.py    From histogrammar-python with Apache License 2.0 5 votes vote down vote up
def p_augassign_8(p):
    '''augassign : CIRCUMFLEXEQUAL'''
    #                            1
    p[0] = ast.BitXor(rule=inspect.currentframe().f_code.co_name, **p[1][1]) 
Example #9
Source File: parser.py    From vecpy with MIT License 5 votes vote down vote up
def binop(self, block, node, var=None):
    if var is None:
      var = self.add_variable(None)
    left = self.expression(block, node.left)
    right = self.expression(block, node.right)
    if isinstance(node.op, ast.Add):
      op = Operator.add
    elif isinstance(node.op, ast.Sub):
      op = Operator.subtract
    elif isinstance(node.op, ast.Mult):
      op = Operator.multiply
    elif isinstance(node.op, ast.Div):
      op = Operator.divide
    elif isinstance(node.op, ast.FloorDiv):
      op = Operator.divide_int
    elif isinstance(node.op, ast.Mod):
      op = Operator.mod
    elif isinstance(node.op, ast.Pow):
      op = Operator.pow
    elif isinstance(node.op, ast.BitAnd):
      op = Operator.bit_and
    elif isinstance(node.op, ast.BitOr):
      op = Operator.bit_or
    elif isinstance(node.op, ast.BitXor):
      op = Operator.bit_xor
    elif isinstance(node.op, ast.LShift):
      op = Operator.shift_left
    elif isinstance(node.op, ast.RShift):
      op = Operator.shift_right
    else:
      raise Exception('Unexpected BinOp (%s)'%(node.op.__class__))
    operation = BinaryOperation(left, op, right)
    assignment = Assignment(var, operation)
    block.add(assignment)
    return var

  #Parses a unary uperation (AST UnaryOp) 
Example #10
Source File: _recompute.py    From icontract with MIT License 5 votes vote down vote up
def visit_BinOp(self, node: ast.BinOp) -> Any:
        """Recursively visit the left and right operand, respectively, and apply the operation on the results."""
        # pylint: disable=too-many-branches
        left = self.visit(node=node.left)
        right = self.visit(node=node.right)

        if isinstance(node.op, ast.Add):
            result = left + right
        elif isinstance(node.op, ast.Sub):
            result = left - right
        elif isinstance(node.op, ast.Mult):
            result = left * right
        elif isinstance(node.op, ast.Div):
            result = left / right
        elif isinstance(node.op, ast.FloorDiv):
            result = left // right
        elif isinstance(node.op, ast.Mod):
            result = left % right
        elif isinstance(node.op, ast.Pow):
            result = left**right
        elif isinstance(node.op, ast.LShift):
            result = left << right
        elif isinstance(node.op, ast.RShift):
            result = left >> right
        elif isinstance(node.op, ast.BitOr):
            result = left | right
        elif isinstance(node.op, ast.BitXor):
            result = left ^ right
        elif isinstance(node.op, ast.BitAnd):
            result = left & right
        elif isinstance(node.op, ast.MatMult):
            result = left @ right
        else:
            raise NotImplementedError("Unhandled op of {}: {}".format(node, node.op))

        self.recomputed_values[node] = result
        return result 
Example #11
Source File: BehavioralRTLIRGenL2Pass.py    From pymtl3 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__( s, component ):
    super().__init__( component )
    s.loop_var_env = set()
    s.tmp_var_env = set()

    # opmap maps an ast operator to its RTLIR counterpart.
    s.opmap = {
      # Bool operators
      # Note: we do not support boolean operators because Python does
      # not allow overloading And and Or operators. Using them in
      # expressions might lead to inconsistent semantics.
      # ast.And    : bir.And(),       ast.Or     : bir.Or(),
      # Unary operators
      # Note: ast.Not is disallowed because it is a boolean operator
      # ast.Not    : bir.Not(),
      ast.Invert : bir.Invert(),
      ast.UAdd   : bir.UAdd(),      ast.USub   : bir.USub(),
      # Binary operators
      ast.Add    : bir.Add(),       ast.Sub    : bir.Sub(),
      ast.Mult   : bir.Mult(),      ast.Div    : bir.Div(),
      ast.Mod    : bir.Mod(),       ast.Pow    : bir.Pow(),
      ast.LShift : bir.ShiftLeft(), ast.RShift : bir.ShiftRightLogic(),
      ast.BitOr  : bir.BitOr(),     ast.BitAnd : bir.BitAnd(),
      ast.BitXor : bir.BitXor(),
      # Compare bir.bir.operators
      ast.Eq     : bir.Eq(),        ast.NotEq  : bir.NotEq(),
      ast.Lt     : bir.Lt(),        ast.LtE    : bir.LtE(),
      ast.Gt     : bir.Gt(),        ast.GtE    : bir.GtE()
    }

  # Override 
Example #12
Source File: simplifier.py    From sspam with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def simplify(self, expr_ast, nbits):
        'Apply pattern matching and arithmetic simplification'
        expr_ast = arithm_simpl.run(expr_ast, nbits)
        expr_ast = asttools.GetConstMod(self.nbits).visit(expr_ast)
        if DEBUG:
            print "arithm simpl: "
            print unparse(expr_ast)
        if DEBUG:
            print "before matching: "
            print unparse(expr_ast)
        expr_ast = all_preprocessings(expr_ast, self.nbits)
        # only flattening ADD nodes because of traditionnal MBA patterns
        expr_ast = Flattening(ast.Add).visit(expr_ast)
        for pattern, repl in self.patterns:
            rep = pattern_matcher.PatternReplacement(pattern, expr_ast, repl)
            new_ast = rep.visit(deepcopy(expr_ast))
            if not asttools.Comparator().visit(new_ast, expr_ast):
                if DEBUG:
                    print "replaced! "
                    dispat = deepcopy(pattern)
                    dispat = Unflattening().visit(dispat)
                    print "pattern:  ", unparse(dispat)
                    disnew = deepcopy(new_ast)
                    disnew = Unflattening().visit(disnew)
                    print "after:    ", unparse(disnew)
                    print ""
                expr_ast = new_ast
                break
        # bitwise simplification: this is a ugly hack, should be
        # "generalized"
        expr_ast = Flattening(ast.BitXor).visit(expr_ast)
        expr_ast = asttools.ConstFolding(expr_ast, self.nbits).visit(expr_ast)
        expr_ast = Unflattening().visit(expr_ast)
        if DEBUG:
            print "after PM: "
            print unparse(expr_ast)
        return expr_ast 
Example #13
Source File: cse.py    From sspam with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_UnaryOp(self, node):
        'Change USub and Invert'
        operand = self.visit(node.operand)
        if isinstance(node.op, ast.UAdd):
            return operand
        if isinstance(node.op, ast.USub):
            return ast.BinOp(ast.Num(-1), ast.Mult(), operand)
        if isinstance(node.op, ast.Invert):
            return ast.BinOp(ast.Num(-1), ast.BitXor(), operand)
        assert False, 'unhandled node type: ' + ast.dump(node) 
Example #14
Source File: asttools.py    From sspam with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_Call(self, node):
        'Replace custom function with bitwise operators'
        self.generic_visit(node)
        if isinstance(node.func, ast.Name):
            if len(node.args) == 2:
                if node.func.id == "mand":
                    op = ast.BitAnd()
                elif node.func.id == "mxor":
                    op = ast.BitXor()
                elif node.func.id == "mor":
                    op = ast.BitOr()
                elif node.func.id == "mlshift":
                    op = ast.LShift()
                elif node.func.id == "mrshift":
                    op = ast.RShift()
                else:
                    return node

                return ast.BinOp(node.args[0],
                                 op,
                                 node.args[1])

            elif len(node.args) == 1 and node.func.id == "mnot":
                arg = node.args[0]
                self.generic_visit(node)
                return ast.UnaryOp(ast.Invert(), arg)

        return self.generic_visit(node) 
Example #15
Source File: test_flattening.py    From sspam with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_differentops(self):
        'Test with other types of operators'
        tests = [("(3 & 5 & 6)",
                  ast.BoolOp(ast.BitAnd(),
                             [ast.Num(3), ast.Num(5), ast.Num(6)])),
                 ("(1 ^ 2 ^ 3) - 4",
                  ast.BinOp(ast.BoolOp(ast.BitXor(),
                                       [ast.Num(1), ast.Num(2), ast.Num(3)]),
                            ast.Add(),
                            ast.BinOp(ast.Num(-1), ast.Mult(), ast.Num(4)))),
                 ("((1 + 2 + 3) & (4 + 5))",
                  ast.BinOp(ast.BoolOp(ast.Add(),
                                       [ast.Num(1), ast.Num(2), ast.Num(3)]),
                            ast.BitAnd(),
                            ast.BinOp(ast.Num(4), ast.Add(), ast.Num(5)))),
                 ("(1 & 2 & 3) - (4 & 5)",
                  ast.BinOp(ast.BoolOp(ast.BitAnd(),
                                       [ast.Num(1), ast.Num(2), ast.Num(3)]),
                            ast.Add(),
                            ast.BinOp(ast.Num(-1), ast.Mult(),
                                      ast.BinOp(ast.Num(4), ast.BitAnd(),
                                                ast.Num(5))))),
                 ("(1 & 2 & 3) << (4 & 5)",
                  ast.BinOp(ast.BoolOp(ast.BitAnd(),
                                       [ast.Num(1), ast.Num(2), ast.Num(3)]),
                            ast.LShift(),
                            ast.BinOp(ast.Num(4), ast.BitAnd(), ast.Num(5))))]
        for teststring, ref_ast in tests:
            test_ast = ast.parse(teststring, mode="eval").body
            test_ast = pre_processing.all_preprocessings(test_ast)
            test_ast = Flattening().visit(test_ast)
            self.assertTrue(Comparator().visit(test_ast, ref_ast)) 
Example #16
Source File: helper.py    From YAPyPy with MIT License 5 votes vote down vote up
def xor_expr_rewrite(head, tail):
    if tail:
        for op, each in tail:
            head = ast.BinOp(head, ast.BitXor(), each, **loc @ op)
    return head 
Example #17
Source File: _toVHDL.py    From myhdl with GNU Lesser General Public License v2.1 5 votes vote down vote up
def visit_BinOp(self, node):
        self.generic_visit(node)
        if isinstance(node.op, (ast.LShift, ast.RShift)):
            self.inferShiftType(node)
        elif isinstance(node.op, (ast.BitAnd, ast.BitOr, ast.BitXor)):
            self.inferBitOpType(node)
        elif isinstance(node.op, ast.Mod) and isinstance(node.left, ast.Str):  # format string
            pass
        else:
            self.inferBinOpType(node) 
Example #18
Source File: _toVHDL.py    From myhdl with GNU Lesser General Public License v2.1 5 votes vote down vote up
def visit_AugAssign(self, node):
        self.visit(node.target)
        self.visit(node.value)
        if isinstance(node.op, (ast.BitOr, ast.BitAnd, ast.BitXor)):
            node.value.vhd = copy(node.target.vhd)
            node.vhdOri = copy(node.target.vhd)
        elif isinstance(node.op, (ast.RShift, ast.LShift)):
            node.value.vhd = vhd_int()
            node.vhdOri = copy(node.target.vhd)
        else:
            node.left, node.right = node.target, node.value
            self.inferBinOpType(node)
        node.vhd = copy(node.target.vhd) 
Example #19
Source File: transformers.py    From mutatest with MIT License 5 votes vote down vote up
def visit_BinOp(self, node: ast.BinOp) -> ast.AST:
        """BinOp nodes are bit-shifts and general operators like add, divide, etc."""
        self.generic_visit(node)
        log_header = f"visit_BinOp: {self.src_file}:"

        # default case for this node, can be BinOpBC or BinOpBS
        ast_class = "BinOp"
        op_type = type(node.op)

        # binop_bit_cmp_types: Set[type] = {ast.BitAnd, ast.BitOr, ast.BitXor}
        if op_type in {ast.BitAnd, ast.BitOr, ast.BitXor}:
            ast_class = "BinOpBC"

        # binop_bit_shift_types: Set[type] = {ast.LShift, ast.RShift}
        if op_type in {ast.LShift, ast.RShift}:
            ast_class = "BinOpBS"

        node_span = NodeSpan(node)
        idx = LocIndex(
            ast_class=ast_class,
            lineno=node_span.lineno,
            col_offset=node_span.col_offset,
            op_type=op_type,
            end_lineno=node_span.end_lineno,
            end_col_offset=node_span.end_col_offset,
        )

        self.locs.add(idx)

        if idx == self.target_idx and self.mutation and not self.readonly:
            LOGGER.debug("%s mutating idx: %s with %s", log_header, self.target_idx, self.mutation)
            return ast.copy_location(
                ast.BinOp(left=node.left, op=self.mutation(), right=node.right), node
            )

        LOGGER.debug("%s (%s, %s): no mutations applied.", log_header, node.lineno, node.col_offset)
        return node 
Example #20
Source File: gtoken.py    From py-googletrans with MIT License 4 votes vote down vote up
def _update(self):
        """update tkk
        """
        # we don't need to update the base TKK value when it is still valid
        now = math.floor(int(time.time() * 1000) / 3600000.0)
        if self.tkk and int(self.tkk.split('.')[0]) == now:
            return

        r = self.client.get(self.host)

        raw_tkk = self.RE_TKK.search(r.text)
        if raw_tkk:
            self.tkk = raw_tkk.group(1)
            return

        # this will be the same as python code after stripping out a reserved word 'var'
        code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
        # unescape special ascii characters such like a \x3d(=)
        code = code.encode().decode('unicode-escape')

        if code:
            tree = ast.parse(code)
            visit_return = False
            operator = '+'
            n, keys = 0, dict(a=0, b=0)
            for node in ast.walk(tree):
                if isinstance(node, ast.Assign):
                    name = node.targets[0].id
                    if name in keys:
                        if isinstance(node.value, ast.Num):
                            keys[name] = node.value.n
                        # the value can sometimes be negative
                        elif isinstance(node.value, ast.UnaryOp) and \
                                isinstance(node.value.op, ast.USub):  # pragma: nocover
                            keys[name] = -node.value.operand.n
                elif isinstance(node, ast.Return):
                    # parameters should be set after this point
                    visit_return = True
                elif visit_return and isinstance(node, ast.Num):
                    n = node.n
                elif visit_return and n > 0:
                    # the default operator is '+' but implement some more for
                    # all possible scenarios
                    if isinstance(node, ast.Add):  # pragma: nocover
                        pass
                    elif isinstance(node, ast.Sub):  # pragma: nocover
                        operator = '-'
                    elif isinstance(node, ast.Mult):  # pragma: nocover
                        operator = '*'
                    elif isinstance(node, ast.Pow):  # pragma: nocover
                        operator = '**'
                    elif isinstance(node, ast.BitXor):  # pragma: nocover
                        operator = '^'
            # a safety way to avoid Exceptions
            clause = compile('{1}{0}{2}'.format(
                operator, keys['a'], keys['b']), '', 'eval')
            value = eval(clause, dict(__builtin__={}))
            result = '{}.{}'.format(n, value)

            self.tkk = result 
Example #21
Source File: compiler.py    From Transcrypt with Apache License 2.0 4 votes vote down vote up
def visit_BinOp (self, node):
        if type (node.op) == ast.FloorDiv:
            if self.allowOperatorOverloading:
                self.emit ('__floordiv__ (')
                self.visitSubExpr (node, node.left)
                self.emit (', ')
                self.visitSubExpr (node, node.right)
                self.emit (')')
            else:
                self.emit ('Math.floor (')
                self.visitSubExpr (node, node.left)
                self.emit (' / ')
                self.visitSubExpr (node, node.right)
                self.emit (')')
        elif (
            type (node.op) in (ast.Pow, ast.MatMult) or
            (type (node.op) == ast.Mod and (self.allowOperatorOverloading or not self.allowJavaScriptMod)) or
            (type (node.op) in (
                ast.Mult, ast.Div, ast.Add, ast.Sub,
                ast.LShift, ast.RShift, ast.BitOr, ast.BitXor, ast.BitAnd
            ) and self.allowOperatorOverloading)
        ):
            self.emit ('{} ('.format (self.filterId (
                # Non-overloaded
                ('__floordiv__' if self.allowOperatorOverloading else 'Math.floor') if type (node.op) == ast.FloorDiv else
                ('__pow__' if self.allowOperatorOverloading else 'Math.pow') if type (node.op) == ast.Pow else
                '__matmul__' if type (node.op) == ast.MatMult else
                ('__jsmod__' if self.allowJavaScriptMod else '__mod__') if type (node.op) == ast.Mod else

                # Overloaded arithmetic
                '__mul__' if type (node.op) == ast.Mult else
                '__truediv__' if type (node.op) == ast.Div else
                '__add__' if type (node.op) == ast.Add else
                '__sub__' if type (node.op) == ast.Sub else

                # Overloaded bitwise
                '__lshift__' if type (node.op) == ast.LShift else
                '__rshift__' if type (node.op) == ast.RShift else
                '__or__' if type (node.op) == ast.BitOr else
                '__xor__' if type (node.op) == ast.BitXor else
                '__and__' if type (node.op) == ast.BitAnd else

                'Never here'
            )))
            self.visit (node.left)
            self.emit (', ')
            self.visit (node.right)
            self.emit (')')
        else:
            self.visitSubExpr (node, node.left)
            self.emit (' {} '.format (self.operators [type (node.op)][0]))
            self.visitSubExpr (node, node.right)