def test_logical_binop(logical_binop, left, right): root = ast.BinaryOp(logical_binop, left, right) tp = TypePass() actual_ty = tp.visit(root) assert actual_ty == BuiltinTypes.BOOL
def test_unaryop(op, value, expected_ty): root = ast.UnaryOp(op, value) tp = TypePass() actual_ty = tp.visit(root) assert actual_ty == expected_ty
def test_shift_binop(shift_binop, left, right, expected_ty): root = ast.BinaryOp(shift_binop, left, right) tp = TypePass() actual_ty = tp.visit(root) assert actual_ty == expected_ty
def test_assignment(): root = ast.Block([ ast.Var('foo', ast.IntegerConstant(0), BuiltinTypes.I32), ast.Assignment(ast.Identifier('foo'), ast.IntegerConstant(1)) ]) tp = TypePass() tp.visit(root)
def test_cast(): root = ast.Cast(ast.FloatConstant(1.0), BuiltinTypes.I32) tp = TypePass() actual_ty = tp.visit(root) assert actual_ty == BuiltinTypes.I32
def test_assignment_bad_lvalue(lvalue): root = ast.Block([ ast.Assignment(lvalue, ast.IntegerConstant(1)) ]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_assignment_incompatible_types(): root = ast.Block([ ast.Var('foo', ast.IntegerConstant(0), BuiltinTypes.I32), ast.Assignment(ast.Identifier('foo'), ast.FloatConstant(1.0)) ]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_func_redeclaration(): foo_func = ast.Function(ast.FunctionProto('foo', [], BuiltinTypes.VOID)) foo_func_dup = ast.Function( ast.FunctionProto('foo', [], BuiltinTypes.I32)) root = ast.TranslationUnit([foo_func, foo_func_dup]) tp = TypePass() with pytest.raises(DumbNameError): tp.visit(root)
def test_var_with_promotion(): root = ast.Block([ ast.Var('foo', ast.IntegerConstant(0.0), BuiltinTypes.F32) ]) tp = TypePass() tp.visit(root)
def test_funccall_unknown_func(): foo_call = ast.FuncCall('foo', [ast.IntegerConstant(0)]) main_func = ast.Function(ast.FunctionProto('main', [], BuiltinTypes.VOID), ast.Block([foo_call])) root = ast.TranslationUnit([main_func]) tp = TypePass() with pytest.raises(DumbNameError): tp.visit(root)
def test_return_no_ret_no_val(): foo_func = ast.Function( ast.FunctionProto('foo', [], BuiltinTypes.VOID), ast.Block([ ast.Return() ])) root = ast.TranslationUnit([foo_func]) tp = TypePass() tp.visit(root)
def test_funccall_wrong_num_args(): foo_func = ast.Function(ast.FunctionProto('foo', [], BuiltinTypes.VOID)) foo_call = ast.FuncCall('foo', [ast.IntegerConstant(1)]) main_func = ast.Function(ast.FunctionProto('main', [], BuiltinTypes.VOID), ast.Block([foo_call])) root = ast.TranslationUnit([foo_func, main_func]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_return_promote_val(): foo_func = ast.Function( ast.FunctionProto('foo', [], BuiltinTypes.F32), ast.Block([ ast.Return(ast.IntegerConstant(1)) ])) root = ast.TranslationUnit([foo_func]) tp = TypePass() tp.visit(root)
def test_var_unknown_type(): root = ast.Block([ ast.Var('foo', ast.FloatConstant(0.0), ast.Type('mytype')) ]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_return_no_ret_val(): foo_func = ast.Function( ast.FunctionProto('foo', [], BuiltinTypes.VOID), ast.Block([ ast.Return(ast.IntegerConstant(1)) ])) root = ast.TranslationUnit([foo_func]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_func_bad_arg_type(): args = [ ast.Argument('a', BuiltinTypes.VOID) ] foo_func = ast.Function( ast.FunctionProto('foo', args, BuiltinTypes.VOID)) root = ast.TranslationUnit([foo_func]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_funccall_bad_arg(arg_list, val_list): foo_func = ast.Function( ast.FunctionProto('foo', arg_list, BuiltinTypes.VOID)) foo_call = ast.FuncCall('foo', val_list) main_func = ast.Function(ast.FunctionProto('main', [], BuiltinTypes.VOID), ast.Block([foo_call])) root = ast.TranslationUnit([foo_func, main_func]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_augmented_assignment_bad(): root = ast.Block([ ast.Var('foo', ast.IntegerConstant(0), BuiltinTypes.I32), ast.Assignment(ast.Identifier('foo'), ast.IntegerConstant(1), Operator.LOGICAL_OR) ]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_return_incompatible_val(): foo_func = ast.Function( ast.FunctionProto('foo', [], BuiltinTypes.I32), ast.Block([ ast.Return(ast.FloatConstant(1.0)) ])) root = ast.TranslationUnit([foo_func]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_func_proto(): args = [ ast.Argument('a', BuiltinTypes.I32), ast.Argument('b', BuiltinTypes.I32) ] foo_func = ast.Function( ast.FunctionProto('foo', args, BuiltinTypes.VOID)) root = ast.TranslationUnit([foo_func]) tp = TypePass() tp.visit(root)
def test_funccall(): foo_func = ast.Function( ast.FunctionProto('foo', [ast.Argument('foo', BuiltinTypes.I32)], BuiltinTypes.VOID)) foo_call = ast.FuncCall('foo', [ast.IntegerConstant(0)]) main_func = ast.Function(ast.FunctionProto('main', [], BuiltinTypes.VOID), ast.Block([foo_call])) root = ast.TranslationUnit([foo_func, main_func]) tp = TypePass() tp.visit(root)
def transform_ast(ast): passes = [TypePass(), LoopPass(), DeadCodePass(), AttrPass(), MainFuncPass()] for _pass in passes: _pass.visit(ast) return ast
def test_if(cond): root = ast.If(cond, ast.Block([])) tp = TypePass() tp.visit(root)
def test_identifier_unknown(): root = ast.Identifier('foo') tp = TypePass() with pytest.raises(DumbNameError): tp.visit(root)
def test_cast_bad_type(value, ty): root = ast.Cast(value, ty) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_if_with_else(): cond = ast.BooleanConstant(True) root = ast.If(cond, ast.Block([]), ast.Block([])) tp = TypePass() tp.visit(root)
def test_while_bad_cond(cond): root = ast.While(cond, ast.Block([])) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_var_incompatible_types(value, ty): root = ast.Block([ast.Var('foo', value, ty)]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_var(): root = ast.Block([ast.Var('foo', ast.IntegerConstant(0))]) tp = TypePass() tp.visit(root)
def test_while(cond): root = ast.While(cond, ast.Block([])) tp = TypePass() tp.visit(root)