Example #1
0
        def expression_binaryop(p):
            left = p[0]
            right = p[2]

            if isinstance(left, Variable_type) and isinstance(
                    right, Variable_type):
                if left.type == right.type:
                    if p[1].gettokentype() == "ADD":
                        return Add(left, right)
                    elif p[1].gettokentype() == "SUBSTRACT":
                        return Substract(left, right)
                    elif p[1].gettokentype() == "MULTIPLY":
                        return Multiply(left, right)
                    elif p[1].gettokentype() == "DIVIDE":
                        return Divide(left, right)
                    else:
                        err = Error_type(
                            "OPERATOR NOT VALID meow",
                            f"Ba-aaaka {p[1].value} is not a valid operator.",
                        )
                        self.print_stack.append(err)
                        raise ValueError()
                        return err
                else:
                    left_type = get_var_type(left)
                    right_type = get_var_type(right)

                    err = Error_type(
                        "Can't operate with different type of waifus",
                        f"you tried to {p[1].gettokentype()} waifu '{left.name}-chan' which is of type {left_type} and "
                        + f"'{right.name}-chan' which is of type {right_type}",
                    )
                    self.print_stack.append(err)
                    raise ValueError()

            if p[1].gettokentype() == "ADD":
                return Add(left, right)
            elif p[1].gettokentype() == "SUBSTRACT":
                return Substract(left, right)
            elif p[1].gettokentype() == "MULTIPLY":
                return Multiply(left, right)
            elif p[1].gettokentype() == "DIVIDE":
                return Divide(left, right)
            else:
                err = Error_type(
                    "OPERATOR NOT VALID meow",
                    f"Ba-aaaka {p[1].value} is not a valid operator.",
                )
                self.print_stack.append(err)
                raise ValueError()
                return err
 def expr_binop(p):
     left = p[0]
     right = p[2]
     if p[1].gettokentype() == 'PLUS':
         return Add(self.builder, self.module, left, right)
     elif p[1].gettokentype() == 'MINUS':
         return Sub(self.builder, self.module, left, right)
     else:
         raise AssertionError('Oops, this should not be possible!')
Example #3
0
        def expression(p):
            left = p[0]
            right = p[2]
            op = p[1]

            if op.gettokentype() == 'ADD':
                return Add(self.builder, self.module, left, right)
            elif op.gettokentype() == 'SUB':
                return Sub(self.builder, self.module, left, right)
Example #4
0
def expression_binop(p):
    left = p[0]
    right = p[2]
    if p[1].gettokentype() == 'PLUS':
        return Add(left, right)
    elif p[1].gettokentype() == 'MINUS':
        return Sub(left, right)
    elif p[1].gettokentype() == 'MUL':
        return Mul(left, right)
    elif p[1].gettokentype() == 'DIV':
        return Div(left, right)
    else:
        raise AssertionError('Oops, this should not be possible!')
Example #5
0
def token2obj(token):
    if token == '+':
        return Add()
    elif token == '-':
        return Sub()
    elif token == '*':
        return Mult()
    elif token == '/':
        return Div()
    try:
        return Num(int(token))
    except:
        pass
    return Name(id=token)
Example #6
0
    def arith_expr(self, a):
        a, op, b = a

        op = {
            '+': Add(),
            '-': Sub(),
            '*': Mult(),
            '@': MatMult(),
            '/': Div(),
            '//': FloorDiv(),
            '%': Mod(),
            '&': BitAnd(),
            '|': BitOr(),
            '^': BitXor(),
            '<<': LShift(),
            '>>': RShift(),
        }.get(op, None)

        return BinOp(left=a, op=op, right=b)
Example #7
0
        def expression_arg(p):
            left = p[0]
            right = p[2]
            operator = p[1]

            if isinstance(p[0], Identificador):
                if p[0].nome in self.vars.keys():
                    left = self.vars[p[0].nome]

            if isinstance(p[2], Identificador):
                if p[2].nome in self.vars.keys():
                    right = self.vars[p[2].nome]

            if operator.gettokentype() == 'ADD':
                return Add(left, right)
            elif operator.gettokentype() == 'SUB':
                return Sub(left, right)
            elif operator.gettokentype() == 'MULT':
                return Mult(left, right)
            elif operator.gettokentype() == 'DIV':
                return Div(left, right)
Example #8
0
    def _make_print(self, ns: List[expr], prefix: str = None) -> Expr:
        # create the indent: ' ' * depth
        mul_by = Name(self._DEPTH_VAR, Load())
        indent = BinOp(Constant(" "), Mult(), mul_by)
        # if prefix is given, indent is: ' ' * (depth - len(prefix)) + prefix
        if prefix is not None:
            assert len(
                prefix
            ) <= self._INDENT, f"too long {prefix} for given indent {self._INDENT}"
            indent.right = BinOp(mul_by, Sub(), Constant(len(prefix)))
            indent = BinOp(indent, Add(), Constant(prefix))

        return Expr(
            Call(
                Name("print", Load()),
                args=cast(List[expr], [indent]) + ns,
                keywords=[
                    keyword("sep", Constant("")),
                    keyword("file",
                            Attribute(Name("sys", Load()), "stderr", Load())),
                ],
            ))
Example #9
0
    def visit_AugAssign(self, ast_aug_assign: AugAssign):
        # need to do some trick of +=, -=, *=, /=
        if type(ast_aug_assign.op) == Add:
            new_op = BinOp(ast_aug_assign.target, Add(), ast_aug_assign.value)
        elif type(ast_aug_assign.op) == Sub:
            new_op = BinOp(ast_aug_assign.target, Sub(), ast_aug_assign.value)
        elif type(ast_aug_assign.op) == Mult:
            new_op = BinOp(ast_aug_assign.target, Mult(), ast_aug_assign.value)
        elif type(ast_aug_assign.op) == Div:
            new_op = BinOp(ast_aug_assign.target, Div(), ast_aug_assign.value)
        else:
            raise Exception("does not support operator: ", ast_aug_assign.op)
        ast_assign = Assign(ast_aug_assign.target, new_op)

        # create Big-O AST assign node
        assign_node = AssignNode()
        # coord = coordinate(ast_aug_assign.col_offset, ast_aug_assign.lineno)
        # self.set_coordinate(assign_node, coord)
        assign_node.target = self.visit(ast_assign.targets)
        assign_node.value = self.visit(ast_assign.value)

        return assign_node
Example #10
0
 def int_Split(node, range=100):  # n -> (n-s) + (s)
     s = random.randint(-range, range)
     return BinOp(left=Num(n=node.n - s), right=Num(n=s), op=Add())
Example #11
0
 def _increment_depth(self) -> AugAssign:
     return AugAssign(Name(self._DEPTH_VAR, Store()), Add(),
                      Constant(self._INDENT))
Example #12
0
 def many_Split(node):  # 'hello' -> 'h' + 'ello' (with randomly chosen cut)
     s = node.s
     i = random.randrange(len(s))
     return BinOp(left=Str(s=s[:i]), right=Str(s=s[i:]), op=Add())
Example #13
0
 def float_Split(node):  # n -> (n-s) + (s)
     s = random.random()
     return BinOp(left=Num(n=node.n - s), right=Num(n=s), op=Add())
Example #14
0
	def __visit_loop(self, node, inject_counter):
		loop_start,loop_end = node.lineno,FindEnd(node).end + 1
		target = inject_counter(node)
		update_call = self.env.update_loop(loop_start,loop_start,loop_end)
		
		self.__visit_loop_body(node,loop_end)
		
		node.body.insert(0,update_call)
		
		if self.env.lineno<=self.cur:
			node.body.append(self.env.update_loop(self.cur,loop_start,loop_end))
		
		if isinstance(node,While):
			node.body.append(AugAssign(target=Name(id=Environment.iter_num, ctx=Store()), value=Num(n=1), op=Add()))
		else:
			node.body[1:1] = self.env.assign(loop_start,[target.id],loop=True)
		
		new_nodes = [self.env.init_loop(loop_start),
				Assign(targets=[Name(id=Environment.iter_num, ctx=Store())], value=Num(n=1)),
				node]
		new_nodes.extend(self.env.end_loop(loop_end,loop_start))
		return new_nodes
Example #15
0
def replace_ops_1(node):
    if isinstance(node, BinOp) and isinstance(node.op, Sub):
        node.op = Mult(left=replace_ops_1(node.left), right=replace_ops_1(node.right))
    elif isinstance(node, BinOp) and isinstance(node.op, Add):
        node.op = Add(left=replace_ops_1(node.left), right=replace_ops_1(node.right))
Example #16
0
 def visit_Div(self, node):
     new_node = Add()
     return self.replace(node, new_node)
Example #17
0
def main():
    expr = Add(Add(Int(4), Int(3)), Mul(Int(10), Add(Int(1), Int(1))))
    print("PRINT:", expr.accept(Print()))
    print("EVAL:", expr.accept(Eval()))
Example #18
0
    set_value(
        "\n"
        "Class mock"
        "\n\n"
        ":cvar a: One swell num"
        "\n\n"
        ":cvar b: Unlucky num"
        "\n"
    )
)

assign_with_type_comment = Assign(
    targets=[Name("res", Store())],
    value=BinOp(
        left=Name("a", Load()),
        op=Add(),
        right=Name("b", Load()),
    ),
    type_comment=Name("int", Load()),
    lineno=None,
)
ann_assign_with_annotation = AnnAssign(
    annotation=assign_with_type_comment.type_comment,
    value=assign_with_type_comment.value,
    simple=1,
    target=assign_with_type_comment.targets[0],
    type_comment=None,
    expr=None,
    expr_target=None,
    expr_annotation=None,
    lineno=None,
 def handle_concatpair(self, pair):
     a, b = pair.get_source_expressions()
     return BinOp(left=self.build_expression(a),
                  op=Add(),
                  right=self.build_expression(b),
                  **self.file)