How to use the pycparser.c_ast.BinaryOp function in pycparser

To help you get started, we’ve selected a few pycparser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github eliben / pycparser / tests / test_c_ast.py View on Github external
def test_scalar_children(self):
        b1 = c_ast.BinaryOp(
            op='+',
            left=c_ast.Constant(type='int', value='6'),
            right=c_ast.ID(name='joe'))

        cv = self.ConstantVisitor()
        cv.visit(b1)

        self.assertEqual(cv.values, ['6'])

        b2 = c_ast.BinaryOp(
            op='*',
            left=c_ast.Constant(type='int', value='111'),
            right=b1)

        b3 = c_ast.BinaryOp(
            op='^',
            left=b2,
            right=b1)

        cv = self.ConstantVisitor()
        cv.visit(b3)

        self.assertEqual(cv.values, ['111', '6', '6'])
github eliben / pycparser / tests / test_c_ast.py View on Github external
b1 = c_ast.BinaryOp(
            op='+',
            left=c_ast.Constant(type='int', value='6'),
            right=c_ast.ID(name='joe'))

        cv = self.ConstantVisitor()
        cv.visit(b1)

        self.assertEqual(cv.values, ['6'])

        b2 = c_ast.BinaryOp(
            op='*',
            left=c_ast.Constant(type='int', value='111'),
            right=b1)

        b3 = c_ast.BinaryOp(
            op='^',
            left=b2,
            right=b1)

        cv = self.ConstantVisitor()
        cv.visit(b3)

        self.assertEqual(cv.values, ['111', '6', '6'])
github eliben / pycparser / tests / test_c_ast.py View on Github external
def test_repr(self):
        c1 = c_ast.Constant(type='float', value='5.6')
        c2 = c_ast.Constant(type='char', value='t')

        b1 = c_ast.BinaryOp(
            op='+',
            left=c1,
            right=c2)

        b2 = c_ast.BinaryOp(
            op='-',
            left=b1,
            right=c2)

        comp = c_ast.Compound(
            block_items=[b1, b2, c1, c2])

        expected = ("Compound(block_items=[BinaryOp(op='+',\n"
                    "                               left=Constant(type='float',\n"
                    "                                             value='5.6'\n"
                    "                                             ),\n"
github eliben / pycparser / tests / test_c_ast.py View on Github external
def test_repr(self):
        c1 = c_ast.Constant(type='float', value='5.6')
        c2 = c_ast.Constant(type='char', value='t')

        b1 = c_ast.BinaryOp(
            op='+',
            left=c1,
            right=c2)

        b2 = c_ast.BinaryOp(
            op='-',
            left=b1,
            right=c2)

        comp = c_ast.Compound(
            block_items=[b1, b2, c1, c2])

        expected = ("Compound(block_items=[BinaryOp(op='+',\n"
                    "                               left=Constant(type='float',\n"
                    "                                             value='5.6'\n"
                    "                                             ),\n"
                    "                               right=Constant(type='char',\n"
                    "                                              value='t'\n"
                    "                                              )\n"
                    "                               ),\n"
                    "                      BinaryOp(op='-',\n"
github simonlindholm / decomp-permuter / src / randomizer.py View on Github external
def plus1(node: ca.Node) -> ca.BinaryOp:
        return ca.BinaryOp("+", node, ca.Constant("int", "1"))
github aws-quickstart / quickstart-git2s3 / functions / source / GitPullS3 / cffi / cparser.py View on Github external
return -self._parse_constant(exprnode.expr)
        # load previously defined int constant
        if (isinstance(exprnode, pycparser.c_ast.ID) and
                exprnode.name in self._int_constants):
            return self._int_constants[exprnode.name]
        #
        if (isinstance(exprnode, pycparser.c_ast.ID) and
                    exprnode.name == '__dotdotdotarray__'):
            if partial_length_ok:
                self._partial_length = True
                return '...'
            raise FFIError(":%d: unsupported '[...]' here, cannot derive "
                           "the actual array length in this context"
                           % exprnode.coord.line)
        #
        if (isinstance(exprnode, pycparser.c_ast.BinaryOp) and
                exprnode.op == '+'):
            return (self._parse_constant(exprnode.left) +
                    self._parse_constant(exprnode.right))
        #
        if (isinstance(exprnode, pycparser.c_ast.BinaryOp) and
                exprnode.op == '-'):
            return (self._parse_constant(exprnode.left) -
                    self._parse_constant(exprnode.right))
        #
        raise FFIError(":%d: unsupported expression: expected a "
                       "simple numeric constant" % exprnode.coord.line)
github simonlindholm / decomp-permuter / src / randomizer.py View on Github external
def randomize_associative_binop(left: ca.Node, right: ca.BinaryOp) -> ca.BinaryOp:
        """Try moving parentheses to the left side sometimes (sadly, it seems to matter)"""
        if random.choice([True, False]) and right.op in ["+", "-"]:
            # ((a + b) - c)
            return ca.BinaryOp(
                right.op, ca.BinaryOp("+", left, right.left), right.right
            )
        else:
            # (a + (b - c))
            return ca.BinaryOp("+", left, right)
github aws / lumberyard / dev / Gems / CloudGemFramework / v1 / AWS / common-code / Crypto / cffi / cparser.py View on Github external
#
        if (isinstance(exprnode, pycparser.c_ast.ID) and
                    exprnode.name == '__dotdotdotarray__'):
            if partial_length_ok:
                self._partial_length = True
                return '...'
            raise FFIError(":%d: unsupported '[...]' here, cannot derive "
                           "the actual array length in this context"
                           % exprnode.coord.line)
        #
        if (isinstance(exprnode, pycparser.c_ast.BinaryOp) and
                exprnode.op == '+'):
            return (self._parse_constant(exprnode.left) +
                    self._parse_constant(exprnode.right))
        #
        if (isinstance(exprnode, pycparser.c_ast.BinaryOp) and
                exprnode.op == '-'):
            return (self._parse_constant(exprnode.left) -
                    self._parse_constant(exprnode.right))
        #
        raise FFIError(":%d: unsupported expression: expected a "
                       "simple numeric constant" % exprnode.coord.line)
github eliben / pycparser / pycparser / c_parser.py View on Github external
| binary_expression LT binary_expression
                                | binary_expression LE binary_expression
                                | binary_expression GE binary_expression
                                | binary_expression GT binary_expression
                                | binary_expression EQ binary_expression
                                | binary_expression NE binary_expression
                                | binary_expression AND binary_expression
                                | binary_expression OR binary_expression
                                | binary_expression XOR binary_expression
                                | binary_expression LAND binary_expression
                                | binary_expression LOR binary_expression
        """
        if len(p) == 2:
            p[0] = p[1]
        else:
            p[0] = c_ast.BinaryOp(p[2], p[1], p[3], p[1].coord)