Пример #1
0
 def parse_return(self):
     """
     return_stmt : RETURN
                 | RETURN expr
     """
     loc = self.curr_token.loc
     self.advance('RETURN')
     if self.curr_token.kind in EXPR_BEGIN_TOKENS:
         ret_val = self.parse_expr()
         return ast.Return(ret_val, loc=loc)
     return ast.Return(loc=loc)
Пример #2
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)
Пример #3
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)
Пример #4
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)
Пример #5
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)
Пример #6
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)
Пример #7
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)
Пример #8
0
])
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)


@pytest.mark.parametrize('node',
                         [ast.Function(None, None),
                          ast.TranslationUnit(None)])
def test_decl_visitor(node):
Пример #9
0
        for _tree, _expected in zip(tree, expected):
            assert_tree(_tree, _expected)
    elif isinstance(tree, ast.Node):
        for attr_name, expected_value in vars(expected).items():
            assert_tree(getattr(tree, attr_name), expected_value)
    else:
        assert tree == expected


@pytest.mark.parametrize('with_dead_code,without_dead_code', [
    ([], []),
    ([
        ast.BinaryOp(ast.Operator.ADD,
                     ast.IntegerConstant(0),
                     ast.IntegerConstant(1)),
        ast.Return(),
        ast.Break()
    ],
    [
        ast.BinaryOp(ast.Operator.ADD,
                     ast.IntegerConstant(0),
                     ast.IntegerConstant(1)),
        ast.Return()
    ]),
    ([
        ast.BinaryOp(ast.Operator.ADD,
                     ast.IntegerConstant(0),
                     ast.IntegerConstant(1)),
        ast.Break(),
        ast.Return()
    ],