def compile_UnaryOperator(self, expr, type_ctx): func = runtime.get_unary_op(expr.operator) compiled_val = self.compile_expr(expr.expr, type_ctx) try: result_type = func.check_types(compiled_val.type) except TypeError: raise CompileError('Invalid type for operator {}: {}'.format( expr.operator, [compiled_val.type])) return typed_ast.FunctionCall(func, [compiled_val], result_type)
def test_unary_operator(self): self.assert_compiled_select( 'SELECT -5', typed_ast.Select( [typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_unary_op('-'), [typed_ast.Literal(5, tq_types.INT)], tq_types.INT), 'f0_' )], typed_ast.NoTable(), typed_ast.Literal(True, tq_types.BOOL), None, None, self.make_type_context( [(None, 'f0_', tq_types.INT)], self.make_type_context([])) ) )
def test_function_calls(self): self.assert_compiled_select( 'SELECT ABS(-3), POW(2, 3), NOW()', typed_ast.Select([ typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_func('abs'), [typed_ast.FunctionCall( runtime.get_unary_op('-'), [typed_ast.Literal(3, tq_types.INT)], tq_types.INT )], tq_types.INT), 'f0_'), typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_func('pow'), [ typed_ast.Literal(2, tq_types.INT), typed_ast.Literal(3, tq_types.INT)], tq_types.INT ), 'f1_' ), typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_func('now'), [], tq_types.INT ), 'f2_' )], typed_ast.NoTable(), typed_ast.Literal(True, tq_types.BOOL), None, typed_ast.Literal(True, tq_types.BOOL), None, None, self.make_type_context([ (None, 'f0_', tq_types.INT), (None, 'f1_', tq_types.INT), (None, 'f2_', tq_types.INT)], self.make_type_context([])) ) )
def test_function_calls(self): self.assert_compiled_select( 'SELECT ABS(-3), POW(2, 3), NOW()', typed_ast.Select([ typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_func('abs'), [typed_ast.FunctionCall( runtime.get_unary_op('-'), [typed_ast.Literal(3, tq_types.INT)], tq_types.INT )], tq_types.INT), 'f0_', None), typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_func('pow'), [ typed_ast.Literal(2, tq_types.INT), typed_ast.Literal(3, tq_types.INT)], tq_types.INT ), 'f1_', None ), typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_func('now'), [], tq_types.INT ), 'f2_', None )], typed_ast.NoTable(), typed_ast.Literal(True, tq_types.BOOL), None, typed_ast.Literal(True, tq_types.BOOL), None, None, self.make_type_context([ (None, 'f0_', tq_types.INT), (None, 'f1_', tq_types.INT), (None, 'f2_', tq_types.INT)], self.make_type_context([])) ) )
def test_unary_operator(self): self.assert_compiled_select( 'SELECT -5', typed_ast.Select( [typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_unary_op('-'), [typed_ast.Literal(5, tq_types.INT)], tq_types.INT), 'f0_' )], typed_ast.NoTable(), typed_ast.Literal(True, tq_types.BOOL), None, typed_ast.Literal(True, tq_types.BOOL), None, None, self.make_type_context( [(None, 'f0_', tq_types.INT)], self.make_type_context([])) ) )
def compile_UnaryOperator(self, expr, type_ctx): func = runtime.get_unary_op(expr.operator) compiled_val = self.compile_expr(expr.expr, type_ctx) result_type = func.check_types(compiled_val.type) return typed_ast.FunctionCall(func, [compiled_val], result_type)