示例#1
0
def operator(mod):
	op = ast.Add()
	if mod == 'or': op = ast.Or()
	if mod == '|': op = ast.Or()
	if mod == '||': op = ast.Or()
	if mod == 'and': op = ast.And()
	if mod == '&': op = ast.And()
	if mod == '&&': op = ast.And()
	if mod == 'plus': op = ast.Add()
	if mod == '+': op = ast.Add()
	if mod == '-': op = ast.Sub()
	if mod == 'minus': op = ast.Sub()
	if mod == 'times': op = ast.Mult()
	if mod == '*': op = ast.Mult()
	if mod == '**': op = ast.Pow()
	if mod == 'divide': op = ast.Div()
	if mod == 'divided': op = ast.Div()
	if mod == 'divided by': op = ast.Div()
	if mod == '/': op = ast.Div()
	if mod == '//': op = ast.FloorDiv()
	if mod == 'floor div': op = ast.FloorDiv()
	if mod == '%': op = ast.Mod()
	if mod == 'mod': op = ast.Mod()
	if mod == 'modulus': op = ast.Mod()
	if mod == 'modulo': op = ast.Mod()
	if mod == '^': op = ast.BitXor()
	if mod == 'xor': op = ast.BitXor()
	if mod == '<<': op = ast.LShift()
	if mod == '>>': op = ast.RShift()
	return op
示例#2
0
def compile_floordivide(p):
    if len(p) == 2:
        return ast.BinOp(ast.Num(1), ast.FloorDiv(), build_ast(p[1]))
    elif len(p) == 3:
        return ast.BinOp(build_ast(p[1]), ast.FloorDiv(), build_ast(p[2]))
    else:
        return ast.BinOp(compile_divide(p[:-1]), ast.FloorDiv(), build_ast(p[-1]))
示例#3
0
    def visit_BinOp(self, node):
        # operator = Add | Sub | Mult | MatMult | Div | Mod | Pow | LShift | RShift | BitOr | BitXor | BitAnd | FloorDiv
        print("  in MyTransformer.visit_BinOp()")
        print("   curr op =", node.op)

        bin_negate = node.op

        # use pseudorandomness to determine whether to negate or just mix up
        rand_num = random.randint(1, 10)

        # negate
        if rand_num >= 4:
            print("   negating...")
            if isinstance(node.op, ast.Add): bin_negate = ast.Sub()
            elif isinstance(node.op, ast.Sub): bin_negate = ast.Add()

            elif isinstance(node.op, ast.Mult): bin_negate = ast.Div()
            elif isinstance(node.op, ast.Div): bin_negate = ast.FloorDiv()
            elif isinstance(node.op, ast.FloorDiv): bin_negate = ast.Div()

            elif isinstance(node.op, ast.LShift): bin_negate = ast.RShift()
            elif isinstance(node.op, ast.RShift): bin_negate = ast.LShift()

            elif isinstance(node.op, ast.BitOr): bin_negate = ast.BitAnd()
            elif isinstance(node.op, ast.BitAnd): bin_negate = ast.BitXor()
            elif isinstance(node.op, ast.BitXor): bin_negate = ast.BitOr()

            elif isinstance(node.op, ast.Pow): bin_negate = ast.Mult()
            elif isinstance(node.op, ast.Mod): bin_negate = ast.Div()
            elif isinstance(node.op, ast.MatMult): bin_negate = ast.Mult()

            else: print("    did not find negation for", node.op)
        # mix up
        else:
            print("   mixing up...")
            if isinstance(node.op, ast.Add): bin_negate = ast.Mult()
            elif isinstance(node.op, ast.Sub): bin_negate = ast.Div()

            elif isinstance(node.op, ast.Mult): bin_negate = ast.Pow()
            elif isinstance(node.op, ast.Div): bin_negate = ast.FloorDiv()
            elif isinstance(node.op, ast.FloorDiv): bin_negate = ast.Div()

            elif isinstance(node.op, ast.BitOr): bin_negate = ast.BitXor()
            elif isinstance(node.op, ast.BitAnd): bin_negate = ast.BitOr()
            elif isinstance(node.op, ast.BitXor): bin_negate = ast.BitOr()

            elif isinstance(node.op, ast.Pow): bin_negate = ast.Mult()
            elif isinstance(node.op, ast.Mod): bin_negate = ast.FloorDiv()

            else: print("    did not find negation for", node.op)

        print("   bin_negate =", bin_negate)

        # create negated node | BinOp(expr left, operator op, expr right)
        new_node = node
        new_node.op = bin_negate
        ast.copy_location(new_node, node)
        ast.fix_missing_locations(new_node)
        return new_node
 def visit_Operator(self, node: Operator, *args, **kwargs) -> C.operator:
     if node == Operator.Add:
         return C.Add()
     elif node == Operator.Sub:
         return C.Sub()
     elif node == Operator.Mult:
         return C.Mult()
     elif node == Operator.MatMult:
         return C.MatMult()
     elif node == Operator.Div:
         return C.Div()
     elif node == Operator.Mod:
         return C.Mod()
     elif node == Operator.Pow:
         return C.Pow()
     elif node == Operator.LShift:
         return C.LShift()
     elif node == Operator.RShift:
         return C.RShift()
     elif node == Operator.BitOr:
         return C.BitOr()
     elif node == Operator.BitXor:
         return C.BitXor()
     elif node == Operator.BitAnd:
         return C.BitAnd()
     elif node == Operator.FloorDiv:
         return C.FloorDiv()
     else:
         raise Exception(f'unknown Operator {node!r}')
    def __init__(self, base_node):
        BaseMutator.__init__(self, base_node)
        self.original_bin_op = base_node.op

        if type(base_node.op) in [
                ast.LShift, ast.RShift, ast.BitOr, ast.BitXor, ast.BitAnd,
                ast.FloorDiv
        ]:
            if type(base_node.op) is ast.LShift:
                self.mutations.append({"op": ast.RShift()})

            if type(base_node.op) is ast.RShift:
                self.mutations.append({"op": ast.LShift()})

            if type(base_node.op) is ast.BitOr:
                self.mutations.append({"op": ast.BitAnd()})

            if type(base_node.op) is ast.BitXor:
                self.mutations.append({"op": ast.FloorDiv()})

            if type(base_node.op) is ast.BitAnd:
                self.mutations.append({"op": ast.BitOr()})

            if type(base_node.op) is ast.FloorDiv:
                self.mutations.append({"op": ast.BitXor()})
示例#6
0
 def visit_BinOp(self, node):
     # Replace x / y with x // y if x and y are integers
     if isinstance(node.op, ast.Div) and \
             isinstance(node.left, ast.Num) and \
             isinstance(node.right, ast.Num) and \
             isinstance(node.left.n, int) and \
             isinstance(node.right.n, int):
         node.op = ast.FloorDiv()
     return node
 def visit_BinOp(self, node):
     global visit_count, visit_target
     self.generic_visit(node)
     visit_count = visit_count + 1
     #print(visit_count)
     if (visit_count == visit_target):
         #print("Hi")
         node.op = ast.FloorDiv()
     return node
    def visitMult(self, ctx: vjjParser.MultContext):
        left = self.visit(ctx.left)
        if ctx.right:
            right = self.visit(ctx.right)
            if ctx.op.text == '*':
                op = ast.Mult()
            elif ctx.op.text == '/':
                op = ast.FloorDiv()

            return ast.BinOp(left=left, op=op, right=right)
        return left
示例#9
0
    def visit_AugAssign(self, node):
        target = self.visit(node.target)
        value = self.visit(node.value)

        if not self.future_division and isinstance(node.op, ast.Div):
            node.op = ast.FloorDiv()

        name = node.op.__class__.__name__
        if name in self.ops_augassign:
            return self.visit_AssignSimple(node.target,
                "%s.PY$__%s__(%s)" % (target, self.ops_augassign[name], value))
        else:
            raise JSError("Unsupported AugAssign type %s" % node.op)
示例#10
0
    def mutate(cls, node):
        """
        mutate augmented assignment operators: +=, -=, *=, /=, //=
        """
        if node not in config.visited_nodes:
            if node.__class__ is ast.AugAssign:

                if node.op.__class__ is ast.Add:
                    if node.value.__class__ is ast.Str and node.value.__class__ is ast.Call and hasattr(
                            node.value.func,
                            'id') and node.value.func.id == 'str':
                        return node

                if node.op.__class__ in config.arithmetic_operators:
                    config.arithmetic_operators.remove(node.op.__class__)

                while len(config.arithmetic_operators) > 0:

                    original_node = deepcopy(node)
                    parent = config.parent_dict[node]
                    del config.parent_dict[node]

                    op_node = None
                    op_type = config.arithmetic_operators.pop()
                    if op_type is ast.Add:
                        op_node = ast.Add()
                    elif op_type is ast.Sub:
                        op_node = ast.Sub()
                    elif op_type is ast.Mult:
                        op_node = ast.Mult()
                    elif op_type is ast.Div:
                        op_node = ast.Div()
                    elif op_type is ast.FloorDiv:
                        op_node = ast.FloorDiv()
                    else:
                        print "TypeError in ASR"
                    if op_node:
                        node.op = op_node
                        config.parent_dict[node] = parent
                        config.node_pairs[node] = original_node
                        config.current_mutated_node = node
                        config.mutated = True
                        return node

                if len(config.arithmetic_operators) == 0:
                    config.arithmetic_operators = [
                        ast.Add, ast.Sub, ast.Mult, ast.Div, ast.FloorDiv
                    ]
                    config.visited_nodes.add(node)

        return node
 def test_scalar_bin_op_init(self):
     ScalarBinOp(self.constant, ast.Add(), self.constant)
     ScalarBinOp(self.output_element, ast.Add(), self.neighbor)
     ScalarBinOp(self.output_element, ast.Sub(), self.neighbor)
     ScalarBinOp(self.output_element, ast.Mult(), self.neighbor)
     ScalarBinOp(self.output_element, ast.Div(), self.neighbor)
     ScalarBinOp(self.output_element, ast.FloorDiv(), self.neighbor)
     ScalarBinOp(self.output_element, ast.Mod(), self.neighbor)
     for op in [
             ast.Mod, ast.Pow, ast.LShift, ast.RShift, ast.BitOr,
             ast.BitXor, ast.BitAnd
     ]:
         with self.assertRaises(AssertionError):
             ScalarBinOp(self.output_element, op, self.neighbor)
示例#12
0
    def visit_BinOp(self, node):
        left = self.visit(node.left)
        right = self.visit(node.right)

        if isinstance(node.op, ast.Mod) and isinstance(node.left, ast.Str):
            return "%s.PY$__mod__(%s)" % (left, right)

        if not self.future_division and isinstance(node.op, ast.Div):
            node.op = ast.FloorDiv()

        name = node.op.__class__.__name__

        if name in self.ops_binop:
            return "%s.PY$__%s__(%s)" % (left, self.ops_binop[name], right)
        else:
            raise JSError("Unknown binary operation type %s" % node.op)
示例#13
0
    def visit_BinOp(self, node):
        global visit_count, visit_target
        visit_count += 1
        if (visit_count == visit_target):
            ##print("Rewrite_binary Line: ", node.lineno)
            if (isinstance(node.op, ast.Add)):
                node.op = ast.Sub()
            elif (isinstance(node.op, ast.Sub)):
                node.op = ast.Add()
            elif (isinstance(node.op, ast.Mult)):
                node.op = ast.FloorDiv()
            elif (isinstance(node.op, ast.Div)
                  or isinstance(node.op, ast.FloorDiv)):
                node.op = ast.Mult()

        return node
示例#14
0
def operator_equals(mod):
	op = ast.Add()
	if mod == '|=': op = ast.Or()
	if mod == '||=': op = ast.Or()
	if mod == '&=': op = ast.And()
	if mod == '&&=': op = ast.And()
	if mod == '+=': op = ast.Add()
	if mod == '-=': op = ast.Sub()
	if mod == '*=': op = ast.Mult()
	if mod == '**=': op = ast.Pow()
	if mod == '/=': op = ast.Div()
	if mod == '//=': op = ast.FloorDiv()
	if mod == '%=': op = ast.Mod()
	if mod == '^=': op = ast.BitXor()
	if mod == '<<': op = ast.LShift()
	if mod == '>>': op = ast.RShift()
	return op
示例#15
0
 def to_node(self):
     node = self.left.to_node()
     if len(self.operator) == 0:
         return node
     else:
         for i in range(len(self.right)):
             if self.operator[i] in ['*', 'times']:
                 node = ast.BinOp(node, ast.Mult(), self.right[i].to_node())
             elif self.operator[i] in ['/', 'divided by']:
                 node = ast.BinOp(node, ast.Div(), self.right[i].to_node())
             elif self.operator[i] in ['%', 'modulo']:
                 node = ast.BinOp(node, ast.Mod(), self.right[i].to_node())
             elif self.operator[i] == '@':
                 node = ast.BinOp(node, ast.MatMult(),
                                  self.right[i].to_node())
             elif self.operator[i] == '//':
                 node = ast.BinOp(node, ast.FloorDiv(),
                                  self.right[i].to_node())
         return node
示例#16
0
def BinOp(draw, expression) -> ast.BinOp:
    op = draw(
        sampled_from([
            ast.Add(),
            ast.Sub(),
            ast.Mult(),
            ast.Div(),
            ast.FloorDiv(),
            ast.Mod(),
            ast.Pow(),
            ast.LShift(),
            ast.RShift(),
            ast.BitOr(),
            ast.BitXor(),
            ast.BitOr(),
            ast.BitAnd(),
            ast.MatMult()
        ]))

    le = draw(lists(expression, min_size=2, max_size=2))

    return ast.BinOp(le[0], op, le[1])
示例#17
0
def AugAssign(draw):
    op = draw(
        sampled_from([
            ast.Add(),
            ast.Sub(),
            ast.Mult(),
            ast.Div(),
            ast.FloorDiv(),
            ast.Mod(),
            ast.Pow(),
            ast.LShift(),
            ast.RShift(),
            ast.BitOr(),
            ast.BitXor(),
            ast.BitOr(),
            ast.BitAnd(),
            ast.MatMult()
        ]))

    return ast.AugAssign(target=draw(Name(ast.Store)),
                         op=op,
                         value=draw(expression()))
示例#18
0
    def visit_BinOp(self, node):
        if isinstance(node.op, ast.Add):
            self.counter += 1

            if self.counter == self.nodeToMutate:
                new_node = ast.BinOp()
                new_node.left = node.left
                new_node.right = node.right

                new_node.ops = [ast.Sub()]

                print('changing add {} to sub.'.format(self.counter))
                return ast.copy_location(new_node, node)  # helps debugging

        if isinstance(node.op, ast.Sub):
            self.counter += 1

            if self.counter == self.nodeToMutate:
                new_node = ast.BinOp()
                new_node.left = node.left
                new_node.right = node.right

                new_node.op = ast.Add()

                print('changing sub {} to add.'.format(self.counter))
                return ast.copy_location(new_node, node)  # helps debugging

        if isinstance(node.op, ast.FloorDiv):
            self.counter += 1

            if self.counter == self.nodeToMutate:
                new_node = ast.BinOp()
                new_node.left = node.left
                new_node.right = node.right

                new_node.op = ast.Div()

                print('changing floorDiv {} to div.'.format(self.counter))
                return ast.copy_location(new_node, node)  # helps debugging

        if isinstance(node.op, ast.Div):
            self.counter += 1

            if self.counter == self.nodeToMutate:
                new_node = ast.BinOp()
                new_node.left = node.left
                new_node.right = node.right

                new_node.op = ast.FloorDiv()

                print('changing div {} to floordiv.'.format(self.counter))
                return ast.copy_location(new_node, node)  # helps debugging

        if isinstance(node.op, ast.Mult):
            self.counter += 1

            if self.counter == self.nodeToMutate:
                new_node = ast.BinOp()
                new_node.left = node.left
                new_node.right = node.right

                new_node.op = ast.Pow()

                print('changing mult {} to pow.'.format(self.counter))
                return ast.copy_location(new_node, node)  # helps debugging

        return self.generic_visit(node)
示例#19
0
 def mutate_Div_to_FloorDiv(self, node):
     if self.should_mutate(node):
         return ast.FloorDiv()
     raise MutationResign()
示例#20
0
    def mutate(cls, node):
        """
        mutate binary mathematical operators: +, -, *, /, %, **
        """
        if node not in config.visited_nodes:
            if node.__class__ is ast.BinOp and node.op.__class__ in [
                    ast.Add, ast.Sub, ast.Mult, ast.Div, ast.FloorDiv, ast.Mod,
                    ast.Pow
            ]:

                if node.op.__class__ is ast.Add:
                    if node.left.__class__ in [
                            ast.Str, ast.List
                    ] or node.right.__class__ in [ast.Str, ast.List]:
                        return node

                if node.op.__class__ in config.arithmetic_operators:
                    config.arithmetic_operators.remove(node.op.__class__)

                while len(config.arithmetic_operators) > 0:

                    original_node = deepcopy(node)

                    if node in config.parent_dict:
                        parent = config.parent_dict[node]
                        del config.parent_dict[node]

                        op_node = None
                        op_type = config.arithmetic_operators.pop()
                        if op_type is ast.Add:
                            op_node = ast.Add()
                        elif op_type is ast.Sub:
                            op_node = ast.Sub()
                        elif op_type is ast.Mult:
                            op_node = ast.Mult()
                        elif op_type is ast.Div:
                            op_node = ast.Div()
                        elif op_type is ast.FloorDiv:
                            op_node = ast.FloorDiv()
                        elif op_type is ast.Mod:
                            op_node = ast.Div()
                        elif op_type is ast.Pow:
                            op_node = ast.Mult()
                        else:
                            print "TypeError in AOR"
                        if op_node:
                            node.op = op_node
                            config.parent_dict[node] = parent
                            config.node_pairs[node] = original_node
                            config.current_mutated_node = node
                            config.mutated = True
                            return node

                if len(config.arithmetic_operators) == 0:
                    config.arithmetic_operators = [
                        ast.Add, ast.Sub, ast.Mult, ast.Div, ast.FloorDiv
                    ]
                    config.visited_nodes.add(node)

            elif node.__class__ is ast.UnaryOp:
                if node.op.__class__ is ast.UAdd:
                    config.mutated = True
                    original_node = deepcopy(node)
                    parent = config.parent_dict[node]
                    del config.parent_dict[node]
                    node.op = ast.USub()
                    config.parent_dict[node] = parent
                    config.node_pairs[node] = original_node
                    config.current_mutated_node = node

                elif node.op.__class__ is ast.USub:
                    config.mutated = True
                    original_node = deepcopy(node)
                    parent = config.parent_dict[node]
                    del config.parent_dict[node]
                    node.op = ast.UAdd()
                    config.parent_dict[node] = parent
                    config.node_pairs[node] = original_node
                    config.current_mutated_node = node

                    # todo: try more unary operations

        return node
示例#21
0
 def mutate_Div_to_FloorDiv(self, node):
     if self.should_mutate(node):
         return ast.FloorDiv()
示例#22
0
 def __floordiv__(self, other):
     return _make_binop(ast.FloorDiv(), self._expr, other)
示例#23
0
#                     [ast.Name(array_id.name, ast.Load())],
#                     [],
#                 ),
#             )
#         )

#     return inner

register(Content(NPArray(w("array_init"))),
         lambda array_init: PythonContent(array_init))

CONTENT_OPERATIONS = [
    (Multiply, ast.Mult()),
    (Add, ast.Add()),
    (Remainder, ast.Mod()),
    (Quotient, ast.FloorDiv()),
]

for _op, _a in CONTENT_OPERATIONS:

    def _op_python_content(l_init, r_init, a_=_a):
        @PythonContent
        @statements_then_init
        def inner():
            l_id = identifier()
            r_id = identifier()
            yield CallUnary(l_init, l_id)
            yield CallUnary(r_init, r_id)
            return Expression(
                ast.BinOp(ast.Name(l_id.name, ast.Load()), a_,
                          ast.Name(r_id.name, ast.Load())))
示例#24
0
    def test_empty_init(self):
        # Jython 2.5.0 did not allow empty constructors for many ast node types
        # but CPython ast nodes do allow this.  For the moment, I don't see a
        # reason to allow construction of the super types (like ast.AST and
        # ast.stmt) as well as the op types that are implemented as enums in
        # Jython (like boolop), but I've left them in but commented out for
        # now.  We may need them in the future since CPython allows this, but
        # it may fall under implementation detail.

        #ast.AST()
        ast.Add()
        ast.And()
        ast.Assert()
        ast.Assign()
        ast.Attribute()
        ast.AugAssign()
        ast.AugLoad()
        ast.AugStore()
        ast.BinOp()
        ast.BitAnd()
        ast.BitOr()
        ast.BitXor()
        ast.BoolOp()
        ast.Break()
        ast.Call()
        ast.ClassDef()
        ast.Compare()
        ast.Continue()
        ast.Del()
        ast.Delete()
        ast.Dict()
        ast.Div()
        ast.Ellipsis()
        ast.Eq()
        ast.Exec()
        ast.Expr()
        ast.Expression()
        ast.ExtSlice()
        ast.FloorDiv()
        ast.For()
        ast.FunctionDef()
        ast.GeneratorExp()
        ast.Global()
        ast.Gt()
        ast.GtE()
        ast.If()
        ast.IfExp()
        ast.Import()
        ast.ImportFrom()
        ast.In()
        ast.Index()
        ast.Interactive()
        ast.Invert()
        ast.Is()
        ast.IsNot()
        ast.LShift()
        ast.Lambda()
        ast.List()
        ast.ListComp()
        ast.Load()
        ast.Lt()
        ast.LtE()
        ast.Mod()
        ast.Module()
        ast.Mult()
        ast.Name()
        ast.Not()
        ast.NotEq()
        ast.NotIn()
        ast.Num()
        ast.Or()
        ast.Param()
        ast.Pass()
        ast.Pow()
        ast.Print()
        ast.RShift()
        ast.Raise()
        ast.Repr()
        ast.Return()
        ast.Slice()
        ast.Store()
        ast.Str()
        ast.Sub()
        ast.Subscript()
        ast.Suite()
        ast.TryExcept()
        ast.TryFinally()
        ast.Tuple()
        ast.UAdd()
        ast.USub()
        ast.UnaryOp()
        ast.While()
        ast.With()
        ast.Yield()
        ast.alias()
        ast.arguments()
        #ast.boolop()
        #ast.cmpop()
        ast.comprehension()
        #ast.excepthandler()
        #ast.expr()
        #ast.expr_context()
        ast.keyword()
示例#25
0
 def __floordiv__(self, other: Union[T, Expr[T]]) -> BinOp[T]:
     return BinOp(self, ast.FloorDiv(), other)
示例#26
0
def translateExpr(n):
    if isinstance(n, Token):
        if n.type.name == 'INTEGER':
            if n.arg > 2**63:
                return ast.NameConstant('__BIGINT__')
            return ast.Num(n.arg)
        if n.type.name == 'FLOAT':
            return ast.Num(n.arg)
        if n.type.name == 'IDENT':
            return ast.NameConstant(mungeName(n.arg))
        if n.type.name == 'FALSE':
            return ast.NameConstant('False')
        if n.type.name == 'TRUE':
            return ast.NameConstant('True')
        if n.type.name == 'BITSTRING':
            if n.arg.find('x') >= 0:
                return ast.Call(ast.NameConstant('__BITSTRING_MATCH__'),
                                [ast.Str(n.arg)], [])
            return ast.NameConstant('0b' + n.arg)
        raise Exception('q %s' % n)

    if isinstance(n, Type):
        return ast.Num(800)

    if n.type == 'e-land':
        return ast.BoolOp(
            ast.And(),
            [translateExpr(n.children[0]),
             translateExpr(n.children[1])])
    elif n.type == 'e-lor':
        return ast.BoolOp(
            ast.Or(),
            [translateExpr(n.children[0]),
             translateExpr(n.children[1])])
    elif n.type == 'e-eq':
        return ast.Compare(translateExpr(n.children[0]), [ast.Eq()],
                           [translateExpr(n.children[1])])
    elif n.type == 'e-ne':
        return ast.Compare(translateExpr(n.children[0]), [ast.NotEq()],
                           [translateExpr(n.children[1])])
    elif n.type == 'e-call':
        return ast.Call(translateExpr(n.children[0]),
                        [translateExpr(x) for x in n.children[1]], [])
    elif n.type == 'e-range':
        return ast.Subscript(translateExpr(n.children[0]),
                             translateRange(n.children[1]), None)
    elif n.type == 'e-not':
        return ast.UnaryOp(ast.Not(), translateExpr(n.children[0]))
    elif n.type == 'e-negate':
        return ast.UnaryOp(ast.USub(), translateExpr(n.children[0]))
    elif n.type == 'e-add':
        return ast.BinOp(translateExpr(n.children[0]), ast.Add(),
                         translateExpr(n.children[1]))
    elif n.type == 'e-sub':
        return ast.BinOp(translateExpr(n.children[0]), ast.Sub(),
                         translateExpr(n.children[1]))
    elif n.type == 'e-mul':
        return ast.BinOp(translateExpr(n.children[0]), ast.Mult(),
                         translateExpr(n.children[1]))
    elif n.type == 'e-div':
        return ast.BinOp(translateExpr(n.children[0]), ast.FloorDiv(),
                         translateExpr(n.children[1]))
    elif n.type == 'e-fdiv':
        return ast.BinOp(translateExpr(n.children[0]), ast.Div(),
                         translateExpr(n.children[1]))
    elif n.type == 'e-rem':
        return ast.BinOp(translateExpr(n.children[0]), ast.Mod(),
                         translateExpr(n.children[1]))
    elif n.type == 'e-lt':
        return ast.Compare(translateExpr(n.children[0]), [ast.Lt()],
                           [translateExpr(n.children[1])])
    elif n.type == 'e-le':
        return ast.Compare(translateExpr(n.children[0]), [ast.LtE()],
                           [translateExpr(n.children[1])])
    elif n.type == 'e-gt':
        return ast.Compare(translateExpr(n.children[0]), [ast.Gt()],
                           [translateExpr(n.children[1])])
    elif n.type == 'e-ge':
        return ast.Compare(translateExpr(n.children[0]), [ast.GtE()],
                           [translateExpr(n.children[1])])
    elif n.type == 'e-lsh':
        return ast.BinOp(translateExpr(n.children[0]), ast.LShift(),
                         translateExpr(n.children[1]))
    elif n.type == 'e-rsh':
        return ast.BinOp(translateExpr(n.children[0]), ast.RShift(),
                         translateExpr(n.children[1]))
    elif n.type == 'e-eor':
        return ast.BinOp(translateExpr(n.children[0]), ast.BitXor(),
                         translateExpr(n.children[1]))
    elif n.type == 'e-or':
        return ast.BinOp(translateExpr(n.children[0]), ast.BitOr(),
                         translateExpr(n.children[1]))
    elif n.type == 'e-and':
        return ast.BinOp(translateExpr(n.children[0]), ast.BitAnd(),
                         translateExpr(n.children[1]))
    elif n.type == 'e-ternary':
        return ast.IfExp(translateExpr(n.children[0]),
                         translateExpr(n.children[1]),
                         translateExpr(n.children[2]))
    elif n.type == 'e-subrange':
        return ast.Num(113)
    elif n.type == 'e-lnegate':
        return ast.UnaryOp(ast.Invert(), translateExpr(n.children[0]))
    elif n.type == 'e-subscript':
        return ast.Attribute(translateExpr(n.children[0]), n.children[1].arg,
                             None)
    elif n.type == 'e-indexempty':
        return ast.Subscript(translateExpr(n.children[0]),
                             ast.Slice(None, None, None), None)
    elif n.type == 'e-implementation-defined':
        return ast.Call(ast.NameConstant('IMPLEMENTATION_DEFINED'), [], [])
    elif n.type == 'e-exp':
        return ast.BinOp(translateExpr(n.children[0]), ast.Pow(),
                         translateExpr(n.children[1]))
    elif n.type == 'e-mod':
        return ast.BinOp(translateExpr(n.children[0]), ast.Mod(),
                         translateExpr(n.children[1]))
    elif n.type == 'e-unknown':
        return ast.Call(ast.NameConstant('UNKNOWN'), [], [])
    elif n.type == 'e-index':
        return ast.Subscript(translateExpr(n.children[0]),
                             ast.Slice(None, None, None), None)
    elif n.type == 'e-tuple':
        return ast.Tuple([translateExpr(x) for x in n.children[0]], None)
    elif n.type == 'e-set-in':
        return ast.Num(114)
    elif n.type == 'e-concat':
        return ast.Call(
            ast.NameConstant('_CONCAT_'),
            [translateExpr(n.children[0]),
             translateExpr(n.children[1])], [])
    elif n.type == 'tuple-nomatch':
        return ast.NameConstant('_')
    elif n.type == 'range':
        return ast.Num(122)  #return translateRange(n)
    else:
        raise Exception('unknown node type: %s' % (n, ))

    return ast.Num(42)
示例#27
0
    def __init__(self, str, lineno=0):
        self.value = str
        self.lineno = lineno


op_ast_map = {
    '+': ast.Add(),
    '-': ast.Sub(),
    '*': ast.Mult(),
    '/': ast.Div(),
    '%': ast.Mod(),
    '**': ast.Pow(),
    '<<': ast.LShift(),
    '>>': ast.RShift(),
    '|': ast.BitOr(),
    '^^': ast.BitXor(),
    '&&': ast.BitAnd(),
    '//': ast.FloorDiv(),
    '==': ast.Eq(),
    '!=': ast.NotEq(),
    '<': ast.Lt(),
    '<=': ast.LtE(),
    '>': ast.Gt(),
    '>=': ast.GtE(),
    'is': ast.Is(),
    'is_not': ast.IsNot(),
    'in': ast.In(),
    'not_in': ast.NotIn(),
    'and': ast.And(),
    'or': ast.Or()
}
示例#28
0
    def visit_BinOp(self, node):
        self.generic_visit(node)
        self.binop_count += 1

        # Check if this is the node we want to alter. We can accomplish this by
        # keeping track of a counter, which we increment every time encounter
        # a BinOp. Since the traversal through the AST is deterministic using the visitor
        # pattern (IT IS NOT DETERMINISTIC IF YOU USE ast.walk), we can identify AST nodes
        # uniquely by the value of the counter
        if (self.binop_count == self.count_of_node_to_mutate):
            # We make sure to use deepcopy so that we preserve all extra
            # information we don't explicitly modify
            new_node = copy.deepcopy(node)
            ast.copy_location(new_node, node)

            # figure out a way to randomize what operator it transforms to based on the current operator

            if isinstance(node.op, ast.Add):
                #randomly generate a number which will associate to a certain type of transformation
                num = random.randint(0, 2)
                print('random number', num)
                if num == 0:
                    new_node.op = ast.Mult()
                if num == 1:
                    new_node.op = ast.Sub()
                if num == 2:
                    new_node.op = ast.Div()
            if isinstance(node.op, ast.Mult):
                num = random.randint(0, 3)
                if num == 0:
                    new_node.op = ast.Div()
                if num == 1:
                    new_node.op = ast.Add()
                if num == 2:
                    new_node.op = ast.FloorDiv()
                if num == 3:
                    new_node.op = ast.Sub()
            if isinstance(node.op, ast.Div):
                num = random.randint(0, 2)
                if num == 0:
                    new_node.op = ast.FloorDiv()
                if num == 1:
                    new_node.op = ast.Mult()
                if num == 2:
                    new_node.op = ast.Add()
            if isinstance(node.op, ast.Sub):
                num = random.randint(0, 2)
                if num == 0:
                    new_node.op = ast.Add()
                if num == 1:
                    new_node.op = ast.Mult()
                if num == 2:
                    new_node.op = ast.Div()
            if isinstance(node.op, ast.FloorDiv):
                new_node.op = ast.Div()
            print('I AM CREATING A NEW NODE HERE', self.binop_count)
            return new_node
        else:
            # If we're not looking at an add node we want to change, don't modify
            # this node whatsoever
            return node
示例#29
0
文件: unparse.py 项目: sherfert/GTR
def as_ast(dct):
    """See https://docs.python.org/2/library/ast.html"""
    if dct['ast_type'] == "Module":
        return ast.Module(dct["body"])
    elif dct['ast_type'] == "Interactive":
        return ast.Interactive(dct["body"])
    elif dct['ast_type'] == "Expression":
        return ast.Expression(dct["body"])
    elif dct['ast_type'] == "Suite":
        return ast.Suite(dct["body"])
    elif dct['ast_type'] == "FunctionDef":
        return ast.FunctionDef(dct["name"], dct["args"], dct["body"],
                               dct["decorator_list"])
    elif dct['ast_type'] == "ClassDef":
        return ast.ClassDef(dct["name"], dct["bases"], dct["body"],
                            dct["decorator_list"])
    elif dct['ast_type'] == "Return":
        return ast.Return(dct["value"])
    elif dct['ast_type'] == "Delete":
        return ast.Delete(dct["targets"])
    elif dct['ast_type'] == "Assign":
        return ast.Assign(dct["targets"], dct["value"])
    elif dct['ast_type'] == "AugAssign":
        return ast.AugAssign(dct["target"], dct["op"], dct["value"])
    elif dct['ast_type'] == "Print":
        return ast.Print(dct["dest"], dct["values"], dct["nl"])
    elif dct['ast_type'] == "For":
        return ast.For(dct["target"], dct["iter"], dct["body"], dct["orelse"])
    elif dct['ast_type'] == "While":
        return ast.While(dct["test"], dct["body"], dct["orelse"])
    elif dct['ast_type'] == "If":
        return ast.If(dct["test"], dct["body"], dct["orelse"])
    elif dct['ast_type'] == "With":
        return ast.With(dct["context_expr"], dct["optional_vars"], dct["body"])
    elif dct['ast_type'] == "Raise":
        return ast.Raise(dct["type"], dct["inst"], dct["tback"])
    elif dct['ast_type'] == "TryExcept":
        return ast.TryExcept(dct["body"], dct["handlers"], dct["orelse"])
    elif dct['ast_type'] == "TryFinally":
        return ast.TryFinally(dct["body"], dct["finalbody"])
    elif dct['ast_type'] == "Assert":
        return ast.Assert(dct["test"], dct["msg"])
    elif dct['ast_type'] == "Import":
        return ast.Import(dct["names"])
    elif dct['ast_type'] == "ImportFrom":
        return ast.ImportFrom(dct["module"], dct["names"], dct["level"])
    elif dct['ast_type'] == "Exec":
        return ast.Exec(dct["body"], dct["globals"], dct["locals"])
    elif dct['ast_type'] == "Global":
        return ast.Global(dct["names"])
    elif dct['ast_type'] == "Expr":
        return ast.Expr(dct["value"])
    elif dct['ast_type'] == "Pass":
        return ast.Pass()
    elif dct['ast_type'] == "Break":
        return ast.Break()
    elif dct['ast_type'] == "Continue":
        return ast.Continue()
    elif dct['ast_type'] == "BoolOp":
        return ast.BoolOp(dct["op"], dct["values"])
    elif dct['ast_type'] == "BinOp":
        return ast.BinOp(dct["left"], dct["op"], dct["right"])
    elif dct['ast_type'] == "UnaryOp":
        return ast.UnaryOp(dct["op"], dct["operand"])
    elif dct['ast_type'] == "Lambda":
        return ast.Lambda(dct["args"], dct["body"])
    elif dct['ast_type'] == "IfExp":
        return ast.IfExp(dct["test"], dct["body"], dct["orelse"])
    elif dct['ast_type'] == "Dict":
        return ast.Dict(dct["keys"], dct["values"])
    elif dct['ast_type'] == "Set":
        return ast.Set(dct["elts"])
    elif dct['ast_type'] == "ListComp":
        return ast.ListComp(dct["elt"], dct["generators"])
    elif dct['ast_type'] == "SetComp":
        return ast.SetComp(dct["elt"], dct["generators"])
    elif dct['ast_type'] == "DictComp":
        return ast.DictComp(dct["key"], dct["value"], dct["generators"])
    elif dct['ast_type'] == "GeneratorExp":
        return ast.GeneratorExp(dct["elt"], dct["generators"])
    elif dct['ast_type'] == "Yield":
        return ast.Yield(dct["value"])
    elif dct['ast_type'] == "Compare":
        return ast.Compare(dct["left"], dct["ops"], dct["comparators"])
    elif dct['ast_type'] == "Call":
        return ast.Call(dct["func"], dct["args"], dct["keywords"],
                        dct["starargs"], dct["kwargs"])
    elif dct['ast_type'] == "Repr":
        return ast.Repr(dct["value"])
    elif dct['ast_type'] == "Num":
        return ast.Num(dct["n"])
    elif dct['ast_type'] == "Str":
        # Converting to ASCII
        return ast.Str(dct["s"].encode('ascii', 'ignore'))
    elif dct['ast_type'] == "Attribute":
        return ast.Attribute(dct["value"], dct["attr"], dct["ctx"])
    elif dct['ast_type'] == "Subscript":
        return ast.Subscript(dct["value"], dct["slice"], dct["ctx"])
    elif dct['ast_type'] == "Name":
        return ast.Name(dct["id"], dct["ctx"])
    elif dct['ast_type'] == "List":
        return ast.List(dct["elts"], dct["ctx"])
    elif dct['ast_type'] == "Tuple":
        return ast.Tuple(dct["elts"], dct["ctx"])
    elif dct['ast_type'] == "Load":
        return ast.Load()
    elif dct['ast_type'] == "Store":
        return ast.Store()
    elif dct['ast_type'] == "Del":
        return ast.Del()
    elif dct['ast_type'] == "AugLoad":
        return ast.AugLoad()
    elif dct['ast_type'] == "AugStore":
        return ast.AugStore()
    elif dct['ast_type'] == "Param":
        return ast.Param()
    elif dct['ast_type'] == "Ellipsis":
        return ast.Ellipsis()
    elif dct['ast_type'] == "Slice":
        return ast.Slice(dct["lower"], dct["upper"], dct["step"])
    elif dct['ast_type'] == "ExtSlice":
        return ast.ExtSlice(dct["dims"])
    elif dct['ast_type'] == "Index":
        return ast.Index(dct["value"])
    elif dct['ast_type'] == "And":
        return ast.And()
    elif dct['ast_type'] == "Or":
        return ast.Or()
    elif dct['ast_type'] == "Add":
        return ast.Add()
    elif dct['ast_type'] == "Sub":
        return ast.Sub()
    elif dct['ast_type'] == "Mult":
        return ast.Mult()
    elif dct['ast_type'] == "Div":
        return ast.Div()
    elif dct['ast_type'] == "Mod":
        return ast.Mod()
    elif dct['ast_type'] == "Pow":
        return ast.Pow()
    elif dct['ast_type'] == "LShift":
        return ast.LShift()
    elif dct['ast_type'] == "RShift":
        return ast.RShift()
    elif dct['ast_type'] == "BitOr":
        return ast.BitOr()
    elif dct['ast_type'] == "BitXor":
        return ast.BitXor()
    elif dct['ast_type'] == "BitAnd":
        return ast.BitAnd()
    elif dct['ast_type'] == "FloorDiv":
        return ast.FloorDiv()
    elif dct['ast_type'] == "Invert":
        return ast.Invert()
    elif dct['ast_type'] == "Not":
        return ast.Not()
    elif dct['ast_type'] == "UAdd":
        return ast.UAdd()
    elif dct['ast_type'] == "USub":
        return ast.USub()
    elif dct['ast_type'] == "Eq":
        return ast.Eq()
    elif dct['ast_type'] == "NotEq":
        return ast.NotEq()
    elif dct['ast_type'] == "Lt":
        return ast.Lt()
    elif dct['ast_type'] == "LtE":
        return ast.LtE()
    elif dct['ast_type'] == "Gt":
        return ast.Gt()
    elif dct['ast_type'] == "GtE":
        return ast.GtE()
    elif dct['ast_type'] == "Is":
        return ast.Is()
    elif dct['ast_type'] == "IsNot":
        return ast.IsNot()
    elif dct['ast_type'] == "In":
        return ast.In()
    elif dct['ast_type'] == "NotIn":
        return ast.NotIn()
    elif dct['ast_type'] == "comprehension":
        return ast.comprehension(dct["target"], dct["iter"], dct["ifs"])
    elif dct['ast_type'] == "ExceptHandler":
        return ast.ExceptHandler(dct["type"], dct["name"], dct["body"])
    elif dct['ast_type'] == "arguments":
        return ast.arguments(dct["args"], dct["vararg"], dct["kwarg"],
                             dct["defaults"])
    elif dct['ast_type'] == "keyword":
        return ast.keyword(dct["arg"], dct["value"])
    elif dct['ast_type'] == "alias":
        return ast.alias(dct["name"], dct["asname"])
    else:
        return dct
示例#30
0
文件: ddl.py 项目: zzl200012/railgun
    'NAME',
    'NUMBER',
)

Add = ast.Add()
Sub = ast.Sub()
Mult = ast.Mult()
Div = ast.Div()
Mod = ast.Mod()
Pow = ast.Pow()
LShift = ast.LShift()
RShift = ast.RShift()
BitOr = ast.BitOr()
BitAnd = ast.BitAnd()
BitXor = ast.BitXor()
FloorDiv = ast.FloorDiv()

op_dict = {
    "+": Add,
    "-": Sub,
    "*": Mult,
    "/": Div,
    "//": FloorDiv,
    "%": Mod,
    "**": Pow,
    "<<": LShift,
    ">>": RShift,
    "|": BitOr,
    "&": BitAnd,
    "^": BitXor,
    "+=": Add,