Exemplo n.º 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)
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
0
    def parse_ident(self, no_func_call=False):
        """
        expr : IDENT
             | IDENT func_call_args
        """
        ident_tok = self.curr_token
        self.advance()

        if self.curr_token.kind != 'LEFT_PAREN' or no_func_call:
            return ast.Identifier(ident_tok.value, loc=ident_tok.loc)

        func_args = self.parse_func_call_args()
        return ast.FuncCall(ident_tok.value, func_args, loc=ident_tok.loc)
Exemplo n.º 5
0
def test_identifier_unknown():
    root = ast.Identifier('foo')
    tp = TypePass()

    with pytest.raises(DumbNameError):
        tp.visit(root)
Exemplo n.º 6
0
        raise RuntimeError('Unexpected call.')

    def visit_TranslationUnit(self, node):
        raise RuntimeError('Unexpected call.')


@pytest.mark.parametrize('node', [
    ast.BinaryOp(None, None, None),
    ast.Assignment(None, None),
    ast.UnaryOp(None, None),
    ast.Cast(None, None),
    ast.IntegerConstant(None),
    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(),