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!')
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)
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!')
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)
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)
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)
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())), ], ))
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
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())
def _increment_depth(self) -> AugAssign: return AugAssign(Name(self._DEPTH_VAR, Store()), Add(), Constant(self._INDENT))
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())
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())
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
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))
def visit_Div(self, node): new_node = Add() return self.replace(node, new_node)
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()))
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)