def visit_Query(self, query): class BestMoveEliminator(ASTVisitor): def visit_Query(self, node): if node.type == "BESTMOVE": raise InvalidExpressionError("QUERY BESTMOVE as subquery") for child in query.children(): BestMoveEliminator().visit(child) self.generic_visit(query) if not is_numeric(query.op_expr.type): raise InvalidExpressionError( "operation clause has invalid type %s" % query.op_expr.type) if not is_numeric(query.where_cond.type): raise InvalidExpressionError( "WHERE clause has invalid type %s" % query.where_cond.type) if query.query_type == "UNIT": query.type = object_ else: query.type = int_ return query
def handle_equality(self, eqop): eqop = self.generic_visit(eqop) # You can always compare equal things of the same type (e.g. ints and # ints, and objects and objects), otherwise they must both be numeric. if eqop.lhs.type != eqop.rhs.type: if not is_numeric(eqop.lhs.type) or not is_numeric(eqop.rhs.type): raise IncompatibleTypeError(eqop) eqop.type = int_ return eqop
def visit_BinaryOp(self, binop): binop = self.generic_visit(binop) if is_numeric(binop.lhs.type) and is_numeric(binop.rhs.type): binop.type = common_arithmetic_type(binop.lhs.type, binop.rhs.type) else: raise InvalidExpressionError(binop) return binop
def codegen(self, code_generator, args): assert len(args) == 2 assert is_numeric(args[0].type) assert is_numeric(args[1].type) code_generator.emit("(") code_generator.visit(args[0]) code_generator.emit("<|") code_generator.visit(args[1]) code_generator.emit(")")
def visit_Constant(self, constant): if is_numeric(constant.type): self.emit(repr(constant.value)) elif constant.type is str_: self.emit('"%s"' % constant.value.encode("unicode_escape").decode("utf8").replace('"', '\"')) else: assert False, "constant of unknown type %r" % constant.type
def codegen(self, code_generator, args): assert len(args) == 1 assert is_numeric(args[0].type) code_generator.emit("(" + op + " ") code_generator.visit(args[0]) code_generator.emit(")")
def visit_RelationalOp(self, relop): relop = self.generic_visit(relop) for child in relop.children(): if not is_numeric(child.type): raise InvalidExpressionError(relop) relop.type = int_ return relop