예제 #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)
예제 #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)
예제 #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)
예제 #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)
예제 #5
0
파일: parser.py 프로젝트: zzag/dumb-lang
 def parse_func(self):
     """
     func : func_proto
          | func_proto block
     """
     loc = self.curr_token.loc
     proto = self.parse_func_proto()
     if self.curr_token.kind != 'LEFT_CURLY_BRACKET':
         return ast.Function(proto, loc=loc)
     body = self.parse_block()
     return ast.Function(proto, body, loc=loc)
예제 #6
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)
예제 #7
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)
예제 #8
0
파일: injector.py 프로젝트: zzag/dumb-lang
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
예제 #9
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)
예제 #10
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)
예제 #11
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)
예제 #12
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)
예제 #13
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)
예제 #14
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)
예제 #15
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)
예제 #16
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)
예제 #17
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)
예제 #18
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)
예제 #19
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)
예제 #20
0
        getattr(visitor, method_name).assert_called_once_with(node)


@pytest.mark.parametrize('node', [
    ast.Block(None),
    ast.If(None, None),
    ast.While(None, None),
    ast.Break(),
    ast.Continue(),
    ast.Return(),
    ast.Var(None, None),
    ast.Expression(None)
])
def test_stmt_visitor(node):
    visitor = StubStmtVisitor()
    method_name = 'visit_' + type(node).__name__
    with mock.patch.object(visitor, method_name):
        visitor.visit(node)
        getattr(visitor, method_name).assert_called_once_with(node)


@pytest.mark.parametrize('node',
                         [ast.Function(None, None),
                          ast.TranslationUnit(None)])
def test_decl_visitor(node):
    visitor = StubDeclVisitor()
    method_name = 'visit_' + type(node).__name__
    with mock.patch.object(visitor, method_name):
        visitor.visit(node)
        getattr(visitor, method_name).assert_called_once_with(node)
예제 #21
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)
예제 #22
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)