def test_dce(with_dead_code, without_dead_code): root = ast.Block(with_dead_code) dcp = DeadCodePass() dcp.visit(root) assert_tree(root.stmts, without_dead_code)
def test_assignment_bad_lvalue(lvalue): root = ast.Block([ ast.Assignment(lvalue, ast.IntegerConstant(1)) ]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
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)
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)
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_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_var_with_promotion(): root = ast.Block([ ast.Var('foo', ast.IntegerConstant(0.0), BuiltinTypes.F32) ]) tp = TypePass() 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_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)
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_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)
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)
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)
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_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 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 test_func(): args = [ ast.Argument('a', BuiltinTypes.I32), ast.Argument('b', BuiltinTypes.I32) ] foo_func = ast.Function( ast.FunctionProto('foo', args, BuiltinTypes.VOID), ast.Block([])) root = ast.TranslationUnit([foo_func]) tp = TypePass() tp.visit(root)
def parse_block(self): """ block : { stmt_list } stmt_list : stmt | stmt_list stmt """ loc = self.curr_token.loc stmts = [] self.advance('LEFT_CURLY_BRACKET') self.skip_semicolon() while self.curr_token.kind != 'RIGHT_CURLY_BRACKET': stmts.append(self.parse_stmt()) self.advance('RIGHT_CURLY_BRACKET') return ast.Block(stmts, loc=loc)
def test_break_outside_loop(): root = ast.Block([ast.Break()]) lp = LoopPass() with pytest.raises(DumbSyntaxError): lp.visit(root)
def test_break(): root = ast.Block( [ast.While(ast.BooleanConstant(True), ast.Block([ast.Break()]))]) lp = LoopPass() lp.visit(root)
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(), 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)
def test_var_incompatible_types(value, ty): root = ast.Block([ast.Var('foo', value, ty)]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_var(): root = ast.Block([ast.Var('foo', ast.IntegerConstant(0))]) tp = TypePass() tp.visit(root)
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)
def test_continue_outside_loop(): root = ast.Block([ast.Continue()]) lp = LoopPass() with pytest.raises(DumbSyntaxError): lp.visit(root)
def test_if(cond): root = ast.If(cond, ast.Block([])) tp = TypePass() tp.visit(root)
def test_while(cond): root = ast.While(cond, ast.Block([])) tp = TypePass() tp.visit(root)
def test_while_bad_cond(cond): root = ast.While(cond, ast.Block([])) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_if_with_else(): cond = ast.BooleanConstant(True) root = ast.If(cond, ast.Block([]), ast.Block([])) tp = TypePass() tp.visit(root)