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)
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)
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)
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)
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)
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(), ast.Continue(),