Commits

Anonymous committed 36b7a1f

Added testcase which changes globals

Comments (0)

Files changed (3)

+from pygen.cgen import *
+from arithgen import ArithGen
+
+from utils import eval_branches, FunctionGenerator
+
+import pgen
+
+
+
+class ChangeGlobalGenerator(FunctionGenerator):
+    def __init__(self, module, stats, opts, rng):
+        self.opts = opts
+        self.module = module
+        self.rng = rng
+        self.stats = stats
+
+
+    def generate_globalon(self):
+        f = self.create_function(0)
+        f.content.append("global len")
+        f.content.append("len = lambda x : 1")
+        return f
+
+    def generate_globaloff(self):
+        f = self.create_function(0)
+        f.content.append("global len")
+        f.content.append("del len")
+        return f
+        
+
+
+    def generate(self, opts, args_num, globals):
+        fon = self.generate_globalon()
+        foff = self.generate_globaloff()
+        
+        self.module.content.insert(0,fon)
+        self.module.content.insert(0,foff)
+        
+        f = self.create_function(0)
+        f.content.extend(
+        [
+            CallStatement(fon, []),
+            "result = len(range(100))",
+            CallStatement(foff, []),
+            "return result"
+        ]
+        )
+        
+        return f
+
                 "mainloop" : 2000, "prog_size" : 20,
                },
     "arith_integer" : {
-                "children" : [(1.0, "arith_integer"), (1.0, ("arith_integer", "local")), (2.0, "loop_integer")],
+                "children" : [
+                        (1.0, "arith_integer"),
+                        (1.0, ("arith_integer", "local")),
+                        (2.0, "loop_integer"),
+                        (1.0, "change_global"),
+                        ],
                 "max_children" : 5,
                 "numbers" : [gen_max_int_gen(), IntegerGen(-1000, 1000)],
                 "type" : [(1.0, "thin"), (1.0, "fat")],
     "yieldfunction" : {
                 "numbers" : [gen_max_int_gen(), IntegerGen(-1000, 1000)],
                },
+    "change_global" : {
+               },
 }
 
 from pygen.cgen import *
 
 from utils import eval_branches, FunctionGenerator
 from iterables import IterableGenerator, ListComprehensionGenerator 
+from globalsgen import ChangeGlobalGenerator
 
 class LoopIntegerGenerator(FunctionGenerator):
     def __init__(self, module, stats, opts, rng):
             f.content.append(call)
             literals.add(result)
 
+        if branch == "change_global":
+            gen = ChangeGlobalGenerator(self.module, self.stats, self.opts, self.rng)
+            
+            c = gen.generate(self.opts['change_global'], 0, [])
+            self.module.content.insert(0, c)
+            
+            result = self.next_variable()
+            
+            call = Assignment(result, '=', [CallStatement(c, [])])
+            f.content.append(call)
+            literals.add(result)
+            
+
 
 
     def arith_integer(self, opts, args_num, globals=[]):
 
     @visit.when(Function)
     def visit(self, depth, node):
-        args = ", ".join(node.args)
+        if node.args:
+            args = ", ".join(node.args)
+        else:
+            args = ""
+
         fun = "".join(['def ', node.name, '(', args, '):'])
         content = [self.code(depth, fun)]
         content += self.visit_block(depth, node.content)