def test_assignment_promotion(): root = ast.Block([ ast.Var('foo', ast.FloatConstant(0.0), BuiltinTypes.F32), ast.Assignment(ast.Identifier('foo'), ast.IntegerConstant(1)) ]) tp = TypePass() tp.visit(root)
def test_cast(): root = ast.Cast(ast.FloatConstant(1.0), BuiltinTypes.I32) tp = TypePass() actual_ty = tp.visit(root) assert actual_ty == BuiltinTypes.I32
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)
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)
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)
]) def shift_binop(request): return request.param @pytest.fixture(params=[ Operator.LOGICAL_OR, Operator.LOGICAL_AND ]) def logical_binop(request): return request.param @pytest.mark.parametrize('value,expected_ty', [ (ast.IntegerConstant(1), BuiltinTypes.I32), (ast.FloatConstant(1.0), BuiltinTypes.F32), (ast.BooleanConstant(True), BuiltinTypes.BOOL), (ast.StringConstant('foo'), BuiltinTypes.STR) ]) def test_builtin_literal_types(value, expected_ty): tp = TypePass() actual_ty = tp.visit(value) assert actual_ty == expected_ty @pytest.mark.parametrize('left,right,expected_ty', [ (ast.IntegerConstant(1), ast.IntegerConstant(2), BuiltinTypes.I32), (ast.IntegerConstant(1), ast.FloatConstant(2.0), BuiltinTypes.F32), (ast.FloatConstant(2.0), ast.IntegerConstant(1), BuiltinTypes.F32),
def parse_float(self): """expr : FLOAT""" float_tok = self.curr_token node = ast.FloatConstant(float(float_tok.value), loc=float_tok.loc) self.advance() return node
class StubDeclVisitor(DeclVisitor): def visit_Function(self, node): 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),