Esempio n. 1
0
def test_dce(with_dead_code, without_dead_code):
    root = ast.Block(with_dead_code)
    dcp = DeadCodePass()

    dcp.visit(root)

    assert_tree(root.stmts, without_dead_code)
Esempio n. 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)
Esempio n. 3
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)
Esempio n. 4
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)
Esempio n. 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)
Esempio n. 6
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)
Esempio n. 7
0
def test_var_with_promotion():
    root = ast.Block([
        ast.Var('foo',
                ast.IntegerConstant(0.0),
                BuiltinTypes.F32)
    ])
    tp = TypePass()

    tp.visit(root)
Esempio n. 8
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)
Esempio n. 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)
Esempio n. 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)
Esempio n. 11
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)
Esempio n. 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)
Esempio n. 13
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)
Esempio n. 14
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)
Esempio n. 15
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)
Esempio n. 16
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)
Esempio n. 17
0
def test_func():
    args = [
        ast.Argument('a', BuiltinTypes.I32),
        ast.Argument('b', BuiltinTypes.I32)
    ]
    foo_func = ast.Function(
        ast.FunctionProto('foo', args, BuiltinTypes.VOID),
        ast.Block([]))
    root = ast.TranslationUnit([foo_func])
    tp = TypePass()

    tp.visit(root)
Esempio n. 18
0
 def parse_block(self):
     """
     block : { stmt_list }
     stmt_list : stmt
               | stmt_list stmt
     """
     loc = self.curr_token.loc
     stmts = []
     self.advance('LEFT_CURLY_BRACKET')
     self.skip_semicolon()
     while self.curr_token.kind != 'RIGHT_CURLY_BRACKET':
         stmts.append(self.parse_stmt())
     self.advance('RIGHT_CURLY_BRACKET')
     return ast.Block(stmts, loc=loc)
Esempio n. 19
0
def test_break_outside_loop():
    root = ast.Block([ast.Break()])
    lp = LoopPass()
    with pytest.raises(DumbSyntaxError):
        lp.visit(root)
Esempio n. 20
0
def test_break():
    root = ast.Block(
        [ast.While(ast.BooleanConstant(True), ast.Block([ast.Break()]))])
    lp = LoopPass()
    lp.visit(root)
Esempio n. 21
0
    ast.FloatConstant(None),
    ast.BooleanConstant(None),
    ast.StringConstant(None),
    ast.Identifier(None),
    ast.FuncCall(None, None)
])
def test_expr_visitor(node):
    visitor = StubExprVisitor()
    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.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)
Esempio n. 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)
Esempio n. 23
0
def test_var():
    root = ast.Block([ast.Var('foo', ast.IntegerConstant(0))])
    tp = TypePass()

    tp.visit(root)
Esempio n. 24
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)
Esempio n. 25
0
def test_continue_outside_loop():
    root = ast.Block([ast.Continue()])
    lp = LoopPass()
    with pytest.raises(DumbSyntaxError):
        lp.visit(root)
Esempio n. 26
0
def test_if(cond):
    root = ast.If(cond, ast.Block([]))
    tp = TypePass()

    tp.visit(root)
Esempio n. 27
0
def test_while(cond):
    root = ast.While(cond, ast.Block([]))
    tp = TypePass()

    tp.visit(root)
Esempio n. 28
0
def test_while_bad_cond(cond):
    root = ast.While(cond, ast.Block([]))
    tp = TypePass()

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

    tp.visit(root)