Exemple #1
0
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)
Exemple #2
0
def test_assignment_bad_lvalue(lvalue):
    root = ast.Block([
        ast.Assignment(lvalue, ast.IntegerConstant(1))
    ])
    tp = TypePass()

    with pytest.raises(DumbTypeError):
        tp.visit(root)
Exemple #3
0
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)
Exemple #4
0
def test_var_with_promotion():
    root = ast.Block([
        ast.Var('foo',
                ast.IntegerConstant(0.0),
                BuiltinTypes.F32)
    ])
    tp = TypePass()

    tp.visit(root)
Exemple #5
0
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)
Exemple #6
0
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)
Exemple #7
0
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)
Exemple #8
0
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)
Exemple #9
0
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)
Exemple #10
0
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)
Exemple #11
0
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)
Exemple #12
0
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)
Exemple #13
0
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)
Exemple #14
0
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)
Exemple #15
0
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)
Exemple #16
0
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)
Exemple #17
0
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)
Exemple #18
0
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
Exemple #19
0
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
Exemple #20
0
def test_unaryop(op, value, expected_ty):
    root = ast.UnaryOp(op, value)
    tp = TypePass()

    actual_ty = tp.visit(root)

    assert actual_ty == expected_ty
Exemple #21
0
def test_cast():
    root = ast.Cast(ast.FloatConstant(1.0),
                    BuiltinTypes.I32)
    tp = TypePass()

    actual_ty = tp.visit(root)

    assert actual_ty == BuiltinTypes.I32
Exemple #22
0
def test_var_incompatible_types(value, ty):
    root = ast.Block([ast.Var('foo', value, ty)])
    tp = TypePass()

    with pytest.raises(DumbTypeError):
        tp.visit(root)
Exemple #23
0
def test_var():
    root = ast.Block([ast.Var('foo', ast.IntegerConstant(0))])
    tp = TypePass()

    tp.visit(root)
Exemple #24
0
def test_cast_bad_type(value, ty):
    root = ast.Cast(value, ty)
    tp = TypePass()

    with pytest.raises(DumbTypeError):
        tp.visit(root)
Exemple #25
0
def test_while(cond):
    root = ast.While(cond, ast.Block([]))
    tp = TypePass()

    tp.visit(root)
Exemple #26
0
def test_while_bad_cond(cond):
    root = ast.While(cond, ast.Block([]))
    tp = TypePass()

    with pytest.raises(DumbTypeError):
        tp.visit(root)
Exemple #27
0
def test_if_with_else():
    cond = ast.BooleanConstant(True)
    root = ast.If(cond, ast.Block([]), ast.Block([]))
    tp = TypePass()

    tp.visit(root)
Exemple #28
0
def test_identifier_unknown():
    root = ast.Identifier('foo')
    tp = TypePass()

    with pytest.raises(DumbNameError):
        tp.visit(root)
Exemple #29
0
def test_if(cond):
    root = ast.If(cond, ast.Block([]))
    tp = TypePass()

    tp.visit(root)
Exemple #30
0
def test_builtin_literal_types(value, expected_ty):
    tp = TypePass()

    actual_ty = tp.visit(value)

    assert actual_ty == expected_ty