Beispiel #1
0
def test_with_main_func():
    root = ast.TranslationUnit([
        ast.Function(ast.FunctionProto('foo', [], ast.BuiltinTypes.VOID)),
        ast.Function(ast.FunctionProto('main', [], ast.BuiltinTypes.I32))
    ])
    mfp = MainFuncPass()

    mfp.visit(root)
Beispiel #2
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)
Beispiel #3
0
def test_main_func_bad_ret_ty():
    root = ast.TranslationUnit([
        ast.Function(ast.FunctionProto('foo', [], ast.BuiltinTypes.VOID)),
        ast.Function(ast.FunctionProto('main', [], ast.BuiltinTypes.VOID))
    ])
    mfp = MainFuncPass()

    with pytest.raises(DumbTypeError):
        mfp.visit(root)
Beispiel #4
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)
Beispiel #5
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)
Beispiel #6
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)
Beispiel #7
0
def _build_function(description):
    name, ret_ty, args = description
    args = list(map(lambda arg: ast.Argument(*arg), args))
    attrs = [ast.Attribute('external')]
    proto = ast.FunctionProto(name, args, ret_ty, attrs=attrs)
    func = ast.Function(proto)
    return func
Beispiel #8
0
def test_unknown_attr():
    attrs = [ast.Attribute('foobar')]
    foo_func = ast.Function(
        ast.FunctionProto('foo', [], ast.BuiltinTypes.I32, attrs))
    ap = AttrPass()
    with pytest.raises(DumbNameError):
        ap.visit(foo_func)
Beispiel #9
0
def test_external_attr_with_args():
    attrs = [ast.Attribute('external', args=(ast.BooleanConstant(True), ))]
    foo_func = ast.Function(
        ast.FunctionProto('foo', [], ast.BuiltinTypes.I32, attrs))
    ap = AttrPass()
    with pytest.raises(DumbTypeError):
        ap.visit(foo_func)
Beispiel #10
0
def test_external_attr_has_body():
    attrs = [ast.Attribute('external')]
    foo_func = ast.Function(
        ast.FunctionProto('foo', [], ast.BuiltinTypes.I32, attrs),
        ast.Block([ast.Return()]))
    ap = AttrPass()
    with pytest.raises(DumbTypeError):
        ap.visit(foo_func)
Beispiel #11
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)
Beispiel #12
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)
Beispiel #13
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)
Beispiel #14
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)
Beispiel #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)
Beispiel #16
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)
Beispiel #17
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)
Beispiel #18
0
 def parse_func_proto(self):
     """
     func_proto : FUNC IDENT func_args
                | FUNC IDENT func_args : ty
     """
     loc = self.curr_token.loc
     self.advance('FUNC')
     name = self.curr_token.value
     self.advance('IDENT')
     func_args = self.parse_func_args()
     if self.curr_token.kind == 'COLON':
         self.advance()
         ret_ty = self.parse_type()
     else:
         ret_ty = ast.BuiltinTypes.VOID
     return ast.FunctionProto(name, func_args, ret_ty, loc=loc)
Beispiel #19
0
def test_no_attrs_has_body():
    foo_func = ast.Function(ast.FunctionProto('foo', [], ast.BuiltinTypes.I32),
                            ast.Block([ast.Return()]))
    ap = AttrPass()
    ap.visit(foo_func)
Beispiel #20
0
def test_external_attr_no_body():
    attrs = [ast.Attribute('external')]
    foo_func = ast.Function(
        ast.FunctionProto('foo', [], ast.BuiltinTypes.I32, attrs))
    ap = AttrPass()
    ap.visit(foo_func)
Beispiel #21
0
def test_no_attrs_no_body():
    foo_func = ast.Function(ast.FunctionProto('foo', [], ast.BuiltinTypes.I32))
    ap = AttrPass()
    with pytest.raises(DumbTypeError):
        ap.visit(foo_func)