예제 #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)
예제 #2
0
def test_var_with_promotion():
    root = ast.Block([
        ast.Var('foo',
                ast.IntegerConstant(0.0),
                BuiltinTypes.F32)
    ])
    tp = TypePass()

    tp.visit(root)
예제 #3
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)
예제 #4
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)
예제 #5
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)
예제 #6
0
파일: parser.py 프로젝트: zzag/dumb-lang
 def parse_var(self):
     """
     var_stmt : VAR IDENT = expr
              | VAR IDENT : ty = expr
     """
     loc = self.curr_token.loc
     self.advance('VAR')
     name = self.curr_token.value
     self.advance('IDENT')
     if self.curr_token.kind == 'COLON':
         self.advance()
         ty = self.parse_type()
     else:
         ty = None
     self.advance('ASSIGN')
     initial_value = self.parse_expr()
     return ast.Var(name, initial_value, ty, loc=loc)
예제 #7
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)
예제 #8
0
def test_var():
    root = ast.Block([ast.Var('foo', ast.IntegerConstant(0))])
    tp = TypePass()

    tp.visit(root)
예제 #9
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):
    visitor = StubDeclVisitor()