def term(p): left = p[0] right = p[2] if p[1].gettokentype() == 'MUL': return Mul(self.builder, self.module, left, right) elif p[1].gettokentype() == 'DIV': return Div(self.builder, self.module, left, right) else: raise AssertionError('Oops, this should not be possible!')
def p_term(tokens): op = tokens[1].gettokentype() left = tokens[0] right = tokens[2] if (op == 'STAR'): return Mult(left, right) if (op == 'SLASH'): return Div(left, right) else: raise Exception(f"{tokens} passaram liso")
def expression(p): left = p[0] right = p[2] operator = p[1] if operator.gettokentype() == 'SUM': return Sum(left, right) elif operator.gettokentype() == 'SUB': return Sub(left, right) elif operator.gettokentype() == 'DIV': return Div(left, right)
def expression(p): left = p[0] right = p[2] operator = p[1] if operator.gettokentype() == 'SUM': return Sum(self.builder, self.module, left, right) elif operator.gettokentype() == 'SUB': return Sub(self.builder, self.module, left, right) elif operator.gettokentype() == 'MUL' : return Mul(self.builder, self.module, left, right) elif operator.gettokentype() == 'DIV' : return Div(self.builder, self.module, left, right)
def expression(p): left = p[0] right = p[2] operator = p[1] if operator.gettokentype() == 'NHAN': return Mul(self.builder, self.module, left, right) elif operator.gettokentype() == 'CHIA': return Div(self.builder, self.module, left, right) elif operator.gettokentype() == 'CONG': return Sum(self.builder, self.module, left, right) elif operator.gettokentype() == 'TRU': 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 expression(p): left = p[0] right = p[2] operator = p[1] if operator.gettokentype() == 'SUM': return Sum(left, right) elif operator.gettokentype() == 'SUB': return Sub(left, right) elif operator.gettokentype() == 'MUL': return Mul(left, right) elif operator.gettokentype() == 'DIV': return Div(left, right) elif operator.gettokentype() == 'POW': return Pow(left, right) elif operator.gettokentype() == 'MOD': return Mod(left, right)
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(p): left = p[0] right = p[2] operator = p[1] if operator.gettokentype() == 'PLUS': return Sum(left, right) elif operator.gettokentype() == 'MINUS': return Sub(left, right) elif operator.gettokentype() == 'MULT': return Mult(left, right) elif operator.gettokentype() == 'DIV': return Div(left, right) elif operator.gettokentype() == 'ASSIGN': return Assign(left, right) elif operator.gettokentype() == 'GREATERTHAN': return GreaterThan(left, right) elif operator.gettokentype() == 'LESSTHAN': return LessThan(left, right) elif operator.gettokentype() == 'EQUAL': return Equal(left, right)
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 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