Anonymous avatar Anonymous committed 2243815

Refactored generator code.

Comments (0)

Files changed (13)

arithgen.py

-
-class IntegerGen(object):
-    def __init__(self, min_range, max_range, rng = None):
-        self.rng = rng
-
-        self.min_range = min_range
-        self.max_range = max_range
-        
-    def __call__(self):
-        return str(self.rng.randrange(self.min_range, self.max_range))
-
-    def set_rng(self, rng):
-        self.rng = rng
-        return self
-        
-
-
-def gen_max_int_gen():
-    return IntegerGen(-2**31, 2**31)
-
-class ArithGen(object):
-    
-    bin_ops = ['+', '-', '&', '^', '|'] #, '%', '/', '//', '*' 
-    
-    def __init__(self, nr_bin_ops, rng):
-        self.nr_bin_ops = nr_bin_ops
-        self.rng = rng
-        if self.rng == None:
-            import random
-            self.rng = random.Random()
-        
-    def generate(self, literals = []):
-        self.bin_ops_left = self.nr_bin_ops
-        return self.add_op(literals)
-        
-    def gen(self, stmt, literals):
-        def gen_stmt():
-            return stmt % (self.generate(literals), )
-        
-        return gen_stmt
-        
-    def add_op(self, literals):
-        
-        expr = []
-        
-        if self.bin_ops_left and self.rng.randint(0, 1):
-            self.bin_ops_left -= 1
-            if self.rng.randint(0, 1):
-                expr.append(self.add_op(literals))
-            else:
-                expr.append(self.add_bracket(literals))
-        else:
-            l = self.rng.choice(literals)
-            if isinstance(l, str):
-                expr.append(l)
-            else:
-                expr.append(l())
-            
-        expr.append(self.rng.choice(self.bin_ops))
-
-        if self.bin_ops_left and self.rng.randint(0, 1):
-            self.bin_ops_left -= 1
-            if self.rng.randint(0, 1):
-                expr.append(self.add_op(literals))
-            else:
-                expr.append(self.add_bracket(literals))
-        else:
-            l = self.rng.choice(literals)
-            if isinstance(l, str):
-                expr.append(l)
-            else:
-                expr.append(l())
-            
-        return " ".join(expr)
-
-    def add_bracket(self, literals):
-        return "".join(['(', self.add_op(literals), ')'])
-    
-    

Empty file added.

codegen/arithgen.py

+
+class IntegerGen(object):
+    def __init__(self, min_range, max_range, rng = None):
+        self.rng = rng
+
+        self.min_range = min_range
+        self.max_range = max_range
+        
+    def __call__(self):
+        return str(self.rng.randrange(self.min_range, self.max_range))
+
+    def set_rng(self, rng):
+        self.rng = rng
+        return self
+        
+
+
+def gen_max_int_gen():
+    return IntegerGen(-2**31, 2**31)
+
+class ArithGen(object):
+    
+    bin_ops = ['+', '-', '&', '^', '|'] #, '%', '/', '//', '*' 
+    
+    def __init__(self, nr_bin_ops, rng):
+        self.nr_bin_ops = nr_bin_ops
+        self.rng = rng
+        if self.rng == None:
+            import random
+            self.rng = random.Random()
+        
+    def generate(self, literals = []):
+        self.bin_ops_left = self.nr_bin_ops
+        return self.add_op(literals)
+        
+    def gen(self, stmt, literals):
+        def gen_stmt():
+            return stmt % (self.generate(literals), )
+        
+        return gen_stmt
+        
+    def add_op(self, literals):
+        
+        expr = []
+        
+        if self.bin_ops_left and self.rng.randint(0, 1):
+            self.bin_ops_left -= 1
+            if self.rng.randint(0, 1):
+                expr.append(self.add_op(literals))
+            else:
+                expr.append(self.add_bracket(literals))
+        else:
+            l = self.rng.choice(literals)
+            if isinstance(l, str):
+                expr.append(l)
+            else:
+                expr.append(l())
+            
+        expr.append(self.rng.choice(self.bin_ops))
+
+        if self.bin_ops_left and self.rng.randint(0, 1):
+            self.bin_ops_left -= 1
+            if self.rng.randint(0, 1):
+                expr.append(self.add_op(literals))
+            else:
+                expr.append(self.add_bracket(literals))
+        else:
+            l = self.rng.choice(literals)
+            if isinstance(l, str):
+                expr.append(l)
+            else:
+                expr.append(l())
+            
+        return " ".join(expr)
+
+    def add_bracket(self, literals):
+        return "".join(['(', self.add_op(literals), ')'])
+    
+    

codegen/fungen.py

+from pygen.cgen import *
+from arithgen import ArithGen
+
+class FunWithIf(object):
+    
+    def __init__(self, literals):
+        pass
+    
+    
+    def gen(self):
+        pass
+    
+
+class FunWithFunctions(object):
+    def __init__(self, variables, literals, rng):
+        self.variables = variables
+        self.literals = literals
+        
+        self.rng = rng
+        if self.rng == None:
+            import random
+            self.rng = random.Random()
+
+    def gen(self, funcs, prefix, nr):
+        newfuncs = []
+        
+        allfuncs = [f for f in funcs]
+        
+        for i in xrange(nr):
+            f = Function(prefix + str(i), self.variables, 
+                         [CallStatement(self.rng.choice(allfuncs), self.variables),
+                          CallStatement(self.rng.choice(allfuncs), self.variables),
+                          CallStatement(self.rng.choice(allfuncs), self.variables),
+#                          CallStatement(self.rng.choice(allfuncs), self.variables),
+                         ]
+                        )
+            newfuncs.append(f)
+            allfuncs.append(f)
+            
+        return newfuncs
+        
+    
+    
+class FunWithArith(object):
+    
+    inplace_ops = ['+=', '-='] # '/=', '*='
+    
+    def __init__(self, variables, literals, rng):
+        self.variables = variables
+        self.literals = literals
+        
+        self.rng = rng
+        if self.rng == None:
+            import random
+            self.rng = random.Random()
+    
+    def gen_content(self, nr, binops):
+        content = []
+        
+        agen = ArithGen(binops, self.rng)
+        
+        for i in xrange(nr):
+            line = [self.rng.choice(self.variables), self.rng.choice(self.inplace_ops)]
+            line.append(agen.generate(self.literals))
+            
+            content.append(" ".join(line))
+            
+        content.append("return %s" % (self.rng.choice(self.variables),))
+        return content
+
+
+    def gen(self, name, nr, binops):
+        f = Function("name", self.variables, self.gen_content(nr, binops))
+        return f

codegen/globalsgen.py

+from pygen.cgen import *
+from arithgen import ArithGen
+from iterables import IterableGenerator
+
+from utils import eval_branches, FunctionGenerator
+
+import pgen
+
+
+
+class ChangeGlobalGenerator(FunctionGenerator):
+    """
+    This generates some code to change a global 
+    and test if the changed function is executed.
+    """
+    def __init__(self, module, stats, opts, rng):
+        self.opts = opts
+        self.module = module
+        self.rng = rng
+        self.stats = stats
+
+
+    def generate_globalon(self, opts):
+        f = self.create_function([])
+        
+        if opts["numbers"]:
+            gen = self.rng.choice(opts["numbers"])
+            gen.set_rng(self.rng)
+            number = gen()
+        else:
+            number = 1
+        
+        f.content.append("global len")
+        f.content.append("len = lambda x : %s" % (number,))
+        return f
+
+    def generate_globaloff(self, opts):
+        f = self.create_function([])
+        f.content.append("global len")
+        f.content.append("del len")
+        return f
+
+
+
+    def generate(self, opts, args_num, globals):
+        fon = self.generate_globalon(opts)
+        foff = self.generate_globaloff(opts)
+
+        self.module.content.insert(0, fon)
+        self.module.content.insert(0, foff)
+
+        iter_gen = IterableGenerator(self.module, self.stats, self.opts, self.rng)
+
+        if opts["numbers"]:
+            numbers = []
+            for i in xrange(4):
+                gen = self.rng.choice(opts["numbers"])
+                gen.set_rng(self.rng)
+                numbers.append(gen())
+        else:
+            numbers = ["1", "2", "3", "4"]
+
+
+        iter = iter_gen.get_iterable(numbers)
+
+        f = self.create_function([])
+        f.content.extend(
+            [
+                CallStatement(fon, []),
+                Assignment("result", '=', [CallStatement("len", iter)]),
+                CallStatement(foff, []),
+                "return result"
+            ]
+        )
+
+        return f
+

codegen/integergen.py

+from pygen.cgen import *
+from arithgen import ArithGen
+
+from utils import eval_branches, FunctionGenerator
+from iterables import IterableGenerator, ListComprehensionGenerator 
+from globalsgen import ChangeGlobalGenerator
+from recursion import TailRecursionGenerator
+
+
+class ArithIntegerGenerator(FunctionGenerator):
+    def __init__(self, module, stats, opts, rng):
+        self.opts = opts
+        self.module = module
+        self.rng = rng
+        self.stats = stats
+
+    def generate_statement(self, opts, f, gen, literals, numbers):
+        if opts["if"] > self.rng.random():
+            result = self.next_variable()
+                   
+            exp1 = gen.generate(list(literals) + numbers)
+            exp2 = gen.generate(list(literals) + numbers)
+                    
+            clause = self.rng.choice(list(literals)) + " < " + self.rng.choice(list(literals))
+                   
+            i = IfStatement(clause, 
+                [Assignment(result, '=', [exp1])],
+                [Assignment(result, '=', [exp2])])
+            f.content.append(i)
+
+        else:
+            result = self.next_variable()
+    
+            exp = gen.generate(list(literals) + numbers)
+            f.content.append(Assignment(result, '=', [exp]))
+            literals.add(result)
+
+    def generate_child(self, opts, f, literals):
+        branch = eval_branches(self.rng, opts["children"])
+        if branch == "arith_integer":
+            gen  = ArithIntegerGenerator(self.module, self.stats, self.opts, self.rng)
+            c = gen.arith_integer(opts, 2)
+            self.module.content.insert(0, c)
+
+            args = self.rng.sample(list(literals), 2)
+            result = self.next_variable()
+
+            call = Assignment(result, '=', [CallStatement(c, args)])
+            f.content.append(call)
+            literals.add(result)
+
+        if branch == ("arith_integer", "local"):
+            gen  = ArithIntegerGenerator(self.module, self.stats, self.opts, self.rng)
+            c = gen.arith_integer(opts, 2, list(literals))
+
+            f.content.append(c)
+
+            args = self.rng.sample(list(literals), 2)
+            result = self.next_variable()
+
+            call = Assignment(result, '=', [CallStatement(c, args)])
+            f.content.append(call)
+            literals.add(result)
+                   
+        if branch == "loop_integer":
+            gen  = LoopIntegerGenerator(self.module, self.stats, self.opts, self.rng)
+
+            c = gen.loop_integer(self.opts['loop_integer'], 2, [])
+            self.module.content.insert(0, c)
+
+            args = self.rng.sample(list(literals), 2)
+            result = self.next_variable()
+
+            call = Assignment(result, '=', [CallStatement(c, args)])
+            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)
+            
+        if branch == "integer_closure":
+            gen = IntegerClosureGenerator(self.module, self.stats, self.opts, self.rng)
+            func = gen.generate(self.opts['integer_closure'], 2, [])
+
+            args = self.rng.sample(list(literals), 2)
+            result = self.next_variable()
+
+            call = Assignment(result, '=', [CallStatement(func, args)])
+            f.content.append(call)
+            literals.add(result)
+
+        if branch == "tail_recursion":
+            gen = TailRecursionGenerator(self.module, self.stats, self.opts, self.rng)
+            func = gen.generate(self.opts['tail_recursion'], 2, [])
+
+            args = self.rng.sample(list(literals), 2)
+            result = self.next_variable()
+
+            call = Assignment(result, '=', [CallStatement(func, args)])
+            f.content.append(call)
+            literals.add(result)
+
+
+
+    def arith_integer(self, opts, args_num, globals=[]):
+        '''Insert a new arithmetic function using only integers'''
+        args = self.generate_arguments(args_num)
+        
+        f = self.create_function(args)
+ 
+        literals = set(args) | set(globals)
+
+        children = min(self.rng.randint(0, opts["max_children"]), self.stats.prog_size)
+        if children > 0:
+            self.stats.prog_size -= children
+            for i in xrange(children):
+                self.generate_child(opts, f, literals)
+
+        numbers = [n.set_rng(self.rng) for n in opts["numbers"]]
+        branch_type = eval_branches(self.rng, opts["type"])
+        if branch_type == "thin":
+            gen = ArithGen(2, self.rng)
+            for i in xrange(self.rng.randint(10,25)):
+                self.generate_statement(opts, f, gen, literals, numbers)      
+               
+        if branch_type == "fat":
+            gen = ArithGen(20, self.rng)
+            for i in xrange(self.rng.randint(0,5)):
+                self.generate_statement(opts, f, gen, literals, numbers)
+
+        exp = ArithGen(10, self.rng).generate(list(literals) + numbers)
+        f.content.append(Assignment('result', '=', [exp]))
+        f.content.append('return result')
+
+        return f
+
+
+
+class LoopIntegerGenerator(FunctionGenerator):
+    def __init__(self, module, stats, opts, rng):
+        self.opts = opts
+        self.module = module
+        self.rng = rng
+        self.stats = stats
+
+    def get_iterable(self, opts, literals):
+        iter_gen = IterableGenerator(self.module, self.stats, self.opts, self.rng)
+        return iter_gen.get_iterable(literals)
+
+    def loop_integer(self, opts, args_num, globals):
+        '''Insert a new function with a loop containing some integer operations'''
+        args = self.generate_arguments(args_num)
+ 
+        literals = set(args) | set(globals)
+        numbers = [n.set_rng(self.rng) for n in opts["numbers"]]
+        
+        f = self.create_function(args)
+ 
+        result = self.next_variable()
+        literals.add(result)
+
+        iter = self.get_iterable(opts, literals)
+
+        loop_var = self.next_variable()
+        literals.add(loop_var)
+
+        l = ForLoop(loop_var, iter)
+        
+        if opts["if"] > self.rng.random():
+            exp1 = ArithGen(1, self.rng).generate(list(literals) + numbers)
+            exp2 = ArithGen(1, self.rng).generate(list(literals) + numbers)
+            
+            clause = " ".join([self.rng.choice(list(literals)), "<", self.rng.choice(list(literals))])
+            
+            i = IfStatement(clause, 
+                            [Assignment(result, '+=', [exp1])],
+                            [Assignment(result, '+=', [exp2])])
+            l.content.append(i)
+            
+        else:
+            exp = ArithGen(1, self.rng).generate(list(literals) + numbers)
+            l.content.append(Assignment(result, '+=', [exp]))
+            
+       
+        f.content.append(Assignment(result, '=', ['0']))
+        f.content.append(l)
+        f.content.append("return " + result)
+
+        return f
+
+class IntegerClosureGenerator(FunctionGenerator):
+    def __init__(self, module, stats, opts, rng):
+        self.opts = opts
+        self.module = module
+        self.rng = rng
+        self.stats = stats
+
+    def generate(self, opts, args_num, globals=[]):
+        '''Insert a new arithmetic function using only integers'''
+        args = self.generate_arguments(args_num)
+
+        closure = self.create_function(args)
+        
+        gen = self.create_function([])
+        
+        if opts["numbers"]:
+            number_gen = self.rng.choice(opts["numbers"])
+            number_gen.set_rng(self.rng)
+            number = number_gen()
+        else:
+            number = 0
+
+        
+        gen.content.extend(
+            [
+                "closure = [%s]" % (number, ),
+                closure,
+                Assignment("func", "=", [closure.name]),
+                "return func",
+            ]
+        )
+        
+        c_var = self.next_variable()
+        
+        self.module.content.insert(0, gen)
+        self.module.content.insert(1, Assignment(c_var, "=", [CallStatement(gen, [])]))
+ 
+        gen_ai = ArithIntegerGenerator(self.module, self.stats, self.opts, self.rng)
+        f = gen_ai.arith_integer(self.opts["arith_integer"], args_num, [])
+        
+        self.module.content.insert(0, f)
+        
+        closure.content.append(Assignment("closure[0]", "+=", [CallStatement(f, args)]))
+        closure.content.append("return closure[0]")
+        
+        return c_var
+        
+
+

codegen/iterables.py

+
+from pygen.cgen import *
+from arithgen import ArithGen
+
+from utils import eval_branches, FunctionGenerator
+
+import pgen
+
+class IterableGenerator(object):
+    def __init__(self, module, stats, opts, rng):
+        self.opts = opts
+        self.module = module
+        self.rng = rng
+        self.stats = stats
+
+    def get_iterable(self, literals):
+        opts = self.opts["iter_gen"]
+
+        types = list(opts["type"]) # iterables that dont require size
+        if self.stats.prog_size > 0:
+            types = types + opts["children"]
+
+        branch = eval_branches(self.rng, types)
+
+        if branch == "range":
+            return ["range(%d)" % (self.rng.randint(1,50))]
+
+        if branch == "xrange":
+            return ["xrange(%d)" % (self.rng.randint(1,50))]
+
+
+        if branch == "list_comp_gen":
+            self.stats.prog_size -= 1
+
+            gen = ListComprehensionGenerator(self.module, self.stats, self.opts, self.rng)
+            return [gen.get_generator(self.opts["list_comp_small_int"], literals)]
+
+        if branch == "list_comp_list":
+            self.stats.prog_size -= 1
+
+            gen = ListComprehensionGenerator(self.module, self.stats, self.opts, self.rng)
+            return [gen.get_list(self.opts["list_comp_small_int"], literals)]
+
+        if branch == "yield_func":
+            self.stats.prog_size -= 1
+            gen = YieldFunctionGenerator(self.module, self.stats, self.opts, self.rng)
+            return [gen.generate(2, literals)]
+
+class YieldFunctionGenerator(FunctionGenerator):
+    """
+    Returns a generator which uses yield.
+    """
+    def __init__(self, module, stats, opts, rng):
+        self.opts = opts
+        self.module = module
+        self.rng = rng
+        self.stats = stats
+
+    def generate_child(self, func, literals):
+        '''Insert a function call to calculate some numbers'''
+        gen = pgen.ArithIntegerGenerator(self.module, self.stats, self.opts, self.rng)
+        c = gen.arith_integer(self.opts["arith_integer"], 2)
+
+        self.module.content.insert(0, c)
+        
+        args = self.rng.sample(literals, 2)
+        result = self.next_variable()
+        call = Assignment(result, '=', [CallStatement(c, args)])
+        func.content.append(call)
+        func.content.append("yield %s" % (result, ))
+
+
+    def generate(self, args_num, pliterals):
+        '''Returns a CallStatement'''
+
+        opts = self.opts["yieldfunction"]
+
+        args = self.generate_arguments(args_num)
+        f = self.create_function(args)
+        self.module.content.insert(0, f)
+
+        literals = list(args) + [n.set_rng(self.rng) for n in opts["numbers"]]
+
+        if self.stats.prog_size > 0:
+            self.generate_child(f, literals)
+
+        for i in xrange(10):
+            result = self.next_variable()
+            exp = ArithGen(2, self.rng).generate(literals)
+            literals.append(result)
+
+            f.content.append(Assignment(result, '=', [exp]))
+            f.content.append("yield %s" % (result, ))
+
+        pargs = self.rng.sample(pliterals, args_num)
+        return CallStatement(f, pargs)
+
+class ListComprehensionGenerator(FunctionGenerator):
+    """
+    Returns a listcomprehension either as a list or a generator.
+    """
+    def __init__(self, module, stats, opts, rng):
+        self.opts = opts
+        self.module = module
+        self.rng = rng
+        self.stats = stats
+
+    def get_expression(self, opts, literals):
+        literals = list(literals) + [n.set_rng(self.rng) for n in opts["numbers"]]
+        branch = eval_branches(self.rng, opts["type"])
+
+        iterable = IterableGenerator(self.module, self.stats, self.opts, self.rng).get_iterable(literals)
+
+        literals.append('i')
+        if branch == "fat":
+            exp = ArithGen(10, self.rng).generate(literals)
+        if branch == "thin":
+            exp = ArithGen(1, self.rng).generate(literals)
+        return ["%s for i in " % (exp, ), iterable]
+
+    def get_generator(self, opts, literals):
+        return ["(", self.get_expression(opts, literals), ")"]
+    def get_list(self, opts, literals):
+        return ["[", self.get_expression(opts, literals), "]"]
+

codegen/recursion.py

+from pygen.cgen import *
+from arithgen import ArithGen
+from iterables import IterableGenerator
+
+from utils import eval_branches, FunctionGenerator
+
+import pgen
+
+
+
+class TailRecursionGenerator(FunctionGenerator):
+    """
+    This generates some code to test tail recursion handling.
+    """
+    def __init__(self, module, stats, opts, rng):
+        self.opts = opts
+        self.module = module
+        self.rng = rng
+        self.stats = stats
+
+
+    def generate_standard_tail_call(self, opts):
+        if opts["numbers"]:
+            numbers = []
+            for i in xrange(4):
+                gen = self.rng.choice(opts["numbers"])
+                gen.set_rng(self.rng)
+                numbers.append(gen())
+        else:
+            numbers = ["1", "2", "3", "4"]
+
+        
+        func = self.create_function(["acc", "rest"])
+
+        exp = ArithGen(1, self.rng).generate(["acc", "rest"] + numbers)
+        
+        var = self.next_variable()
+        func.content.append(Assignment(var, '=', [exp]))
+
+        end = IfStatement("acc == 0",
+            ["return %s" % (var,)],
+            [Assignment("result", "=", [CallStatement(func, ["acc - 1", var])]),
+            "return result"])
+            
+        func.content.append(end)
+        return func
+
+    def generate_fcall_tail_call(self, opts):
+        if opts["numbers"]:
+            numbers = []
+            for i in xrange(4):
+                gen = self.rng.choice(opts["numbers"])
+                gen.set_rng(self.rng)
+                numbers.append(gen())
+        else:
+            numbers = ["1", "2", "3", "4"]
+
+        func = self.create_function(["acc", "rest"])
+
+        # generate an arith_integer function
+        gen  = pgen.ArithIntegerGenerator(self.module, self.stats, self.opts, self.rng)
+        c = gen.arith_integer(self.opts["arith_integer"], 2)
+        self.module.content.insert(0, c)
+
+        args = self.rng.sample(["acc", "rest"] + numbers, 2)
+        result = self.next_variable()
+
+        call = Assignment(result, '=', [CallStatement(c, args)])
+        func.content.append(call)
+
+        end = IfStatement("acc == 0",
+            ["return %s" % (result,)],
+            [Assignment("result", "=", [CallStatement(func, ["acc - 1", result])]),
+            "return result"])
+            
+        func.content.append(end)
+        return func
+
+
+    def generate_closure_tail_call(self, opts):
+        if opts["numbers"]:
+            numbers = []
+            for i in xrange(4):
+                gen = self.rng.choice(opts["numbers"])
+                gen.set_rng(self.rng)
+                numbers.append(gen())
+        else:
+            numbers = ["1", "2", "3", "4"]
+
+        
+        func = self.create_function(["acc", "rest"])
+
+        exp = ArithGen(5, self.rng).generate(["closure[0]", "acc", "rest"] + numbers)
+        
+        var = self.next_variable()
+        func.content.append(Assignment(var, '=', [exp]))
+        func.content.append(Assignment("closure[0]", '+=', [var]))
+
+        end = IfStatement("acc == 0",
+            ["return %s" % (var,)],
+            [Assignment("result", "=", [CallStatement(func, ["acc - 1", var])]),
+            "return result"])
+            
+        func.content.append(end)
+        return func
+
+
+    def generate(self, opts, args_num, globals):
+        args = self.generate_arguments(args_num)
+        func = self.create_function(args)
+        
+        branch = eval_branches(self.rng, opts["type"])
+        
+        if branch == "standard":
+            rec = self.generate_standard_tail_call(opts)
+        elif branch == "closure":
+            func.content.append(Assignment("closure", "=", ["[0]"]))
+            rec = self.generate_closure_tail_call(opts)
+        else:
+            rec = self.generate_fcall_tail_call(opts)
+            
+        func.content.append(rec)
+        
+        func.content.extend(
+            [Assignment("result", "=", [CallStatement(rec, ["10", "0"])]),
+            "return result"])
+        
+        self.module.content.append(func)
+        
+        return func
+

fungen.py

-from pygen.cgen import *
-from arithgen import ArithGen
-
-class FunWithIf(object):
-    
-    def __init__(self, literals):
-        pass
-    
-    
-    def gen(self):
-        pass
-    
-
-class FunWithFunctions(object):
-    def __init__(self, variables, literals, rng):
-        self.variables = variables
-        self.literals = literals
-        
-        self.rng = rng
-        if self.rng == None:
-            import random
-            self.rng = random.Random()
-
-    def gen(self, funcs, prefix, nr):
-        newfuncs = []
-        
-        allfuncs = [f for f in funcs]
-        
-        for i in xrange(nr):
-            f = Function(prefix + str(i), self.variables, 
-                         [CallStatement(self.rng.choice(allfuncs), self.variables),
-                          CallStatement(self.rng.choice(allfuncs), self.variables),
-                          CallStatement(self.rng.choice(allfuncs), self.variables),
-#                          CallStatement(self.rng.choice(allfuncs), self.variables),
-                         ]
-                        )
-            newfuncs.append(f)
-            allfuncs.append(f)
-            
-        return newfuncs
-        
-    
-    
-class FunWithArith(object):
-    
-    inplace_ops = ['+=', '-='] # '/=', '*='
-    
-    def __init__(self, variables, literals, rng):
-        self.variables = variables
-        self.literals = literals
-        
-        self.rng = rng
-        if self.rng == None:
-            import random
-            self.rng = random.Random()
-    
-    def gen_content(self, nr, binops):
-        content = []
-        
-        agen = ArithGen(binops, self.rng)
-        
-        for i in xrange(nr):
-            line = [self.rng.choice(self.variables), self.rng.choice(self.inplace_ops)]
-            line.append(agen.generate(self.literals))
-            
-            content.append(" ".join(line))
-            
-        content.append("return %s" % (self.rng.choice(self.variables),))
-        return content
-
-
-    def gen(self, name, nr, binops):
-        f = Function("name", self.variables, self.gen_content(nr, binops))
-        return f

globalsgen.py

-from pygen.cgen import *
-from arithgen import ArithGen
-from iterables import IterableGenerator
-
-from utils import eval_branches, FunctionGenerator
-
-import pgen
-
-
-
-class ChangeGlobalGenerator(FunctionGenerator):
-    """
-    This generates some code to change a global 
-    and test if the changed function is executed.
-    """
-    def __init__(self, module, stats, opts, rng):
-        self.opts = opts
-        self.module = module
-        self.rng = rng
-        self.stats = stats
-
-
-    def generate_globalon(self, opts):
-        f = self.create_function([])
-        
-        if opts["numbers"]:
-            gen = self.rng.choice(opts["numbers"])
-            gen.set_rng(self.rng)
-            number = gen()
-        else:
-            number = 1
-        
-        f.content.append("global len")
-        f.content.append("len = lambda x : %s" % (number,))
-        return f
-
-    def generate_globaloff(self, opts):
-        f = self.create_function([])
-        f.content.append("global len")
-        f.content.append("del len")
-        return f
-
-
-
-    def generate(self, opts, args_num, globals):
-        fon = self.generate_globalon(opts)
-        foff = self.generate_globaloff(opts)
-
-        self.module.content.insert(0, fon)
-        self.module.content.insert(0, foff)
-
-        iter_gen = IterableGenerator(self.module, self.stats, self.opts, self.rng)
-
-        if opts["numbers"]:
-            numbers = []
-            for i in xrange(4):
-                gen = self.rng.choice(opts["numbers"])
-                gen.set_rng(self.rng)
-                numbers.append(gen())
-        else:
-            numbers = ["1", "2", "3", "4"]
-
-
-        iter = iter_gen.get_iterable(numbers)
-
-        f = self.create_function([])
-        f.content.extend(
-            [
-                CallStatement(fon, []),
-                Assignment("result", '=', [CallStatement("len", iter)]),
-                CallStatement(foff, []),
-                "return result"
-            ]
-        )
-
-        return f
-

iterables.py

-
-from pygen.cgen import *
-from arithgen import ArithGen
-
-from utils import eval_branches, FunctionGenerator
-
-import pgen
-
-class IterableGenerator(object):
-    def __init__(self, module, stats, opts, rng):
-        self.opts = opts
-        self.module = module
-        self.rng = rng
-        self.stats = stats
-
-    def get_iterable(self, literals):
-        opts = self.opts["iter_gen"]
-
-        types = list(opts["type"]) # iterables that dont require size
-        if self.stats.prog_size > 0:
-            types = types + opts["children"]
-
-        branch = eval_branches(self.rng, types)
-
-        if branch == "range":
-            return ["range(%d)" % (self.rng.randint(1,50))]
-
-        if branch == "xrange":
-            return ["xrange(%d)" % (self.rng.randint(1,50))]
-
-
-        if branch == "list_comp_gen":
-            self.stats.prog_size -= 1
-
-            gen = ListComprehensionGenerator(self.module, self.stats, self.opts, self.rng)
-            return [gen.get_generator(self.opts["list_comp_small_int"], literals)]
-
-        if branch == "list_comp_list":
-            self.stats.prog_size -= 1
-
-            gen = ListComprehensionGenerator(self.module, self.stats, self.opts, self.rng)
-            return [gen.get_list(self.opts["list_comp_small_int"], literals)]
-
-        if branch == "yield_func":
-            self.stats.prog_size -= 1
-            gen = YieldFunctionGenerator(self.module, self.stats, self.opts, self.rng)
-            return [gen.generate(2, literals)]
-
-class YieldFunctionGenerator(FunctionGenerator):
-    """
-    Returns a generator which uses yield.
-    """
-    def __init__(self, module, stats, opts, rng):
-        self.opts = opts
-        self.module = module
-        self.rng = rng
-        self.stats = stats
-
-    def generate_child(self, func, literals):
-        '''Insert a function call to calculate some numbers'''
-        gen = pgen.ArithIntegerGenerator(self.module, self.stats, self.opts, self.rng)
-        c = gen.arith_integer(self.opts["arith_integer"], 2)
-
-        self.module.content.insert(0, c)
-        
-        args = self.rng.sample(literals, 2)
-        result = self.next_variable()
-        call = Assignment(result, '=', [CallStatement(c, args)])
-        func.content.append(call)
-        func.content.append("yield %s" % (result, ))
-
-
-    def generate(self, args_num, pliterals):
-        '''Returns a CallStatement'''
-
-        opts = self.opts["yieldfunction"]
-
-        args = self.generate_arguments(args_num)
-        f = self.create_function(args)
-        self.module.content.insert(0, f)
-
-        literals = list(args) + [n.set_rng(self.rng) for n in opts["numbers"]]
-
-        if self.stats.prog_size > 0:
-            self.generate_child(f, literals)
-
-        for i in xrange(10):
-            result = self.next_variable()
-            exp = ArithGen(2, self.rng).generate(literals)
-            literals.append(result)
-
-            f.content.append(Assignment(result, '=', [exp]))
-            f.content.append("yield %s" % (result, ))
-
-        pargs = self.rng.sample(pliterals, args_num)
-        return CallStatement(f, pargs)
-
-class ListComprehensionGenerator(FunctionGenerator):
-    """
-    Returns a listcomprehension either as a list or a generator.
-    """
-    def __init__(self, module, stats, opts, rng):
-        self.opts = opts
-        self.module = module
-        self.rng = rng
-        self.stats = stats
-
-    def get_expression(self, opts, literals):
-        literals = list(literals) + [n.set_rng(self.rng) for n in opts["numbers"]]
-        branch = eval_branches(self.rng, opts["type"])
-
-        iterable = IterableGenerator(self.module, self.stats, self.opts, self.rng).get_iterable(literals)
-
-        literals.append('i')
-        if branch == "fat":
-            exp = ArithGen(10, self.rng).generate(literals)
-        if branch == "thin":
-            exp = ArithGen(1, self.rng).generate(literals)
-        return ["%s for i in " % (exp, ), iterable]
-
-    def get_generator(self, opts, literals):
-        return ["(", self.get_expression(opts, literals), ")"]
-    def get_list(self, opts, literals):
-        return ["[", self.get_expression(opts, literals), "]"]
-
-from arithgen import IntegerGen, gen_max_int_gen
+from codegen.arithgen import IntegerGen, gen_max_int_gen
 
 pgen_opts = {
     "module" : {"children" : [(1.0, "arith_integer"), (0.0, "arith_float")],
-                "mainloop" : 2000, "prog_size" : 10, "module_size" : 100,
+                "mainloop" : 20000, "prog_size" : 5, "module_size" : 25,
                },
+
+
     "arith_integer" : {
                 "children" : [
                         (1.0, "arith_integer"),
                 "numbers" : [IntegerGen(-10, 10)],
                 "type" : [(1.0, "thin"), (1.0, "fat")],
                },
-    "tuple" : {},
     "iter_gen" : {
                 "type" : [(1.0, "xrange"), (1.0, "range")],
                 "children" : [(1.0, "list_comp_gen"), (1.0, "list_comp_list"), (1.0, "yield_func")],
 }
 
 from pygen.cgen import *
-from arithgen import ArithGen
 
-from utils import eval_branches, FunctionGenerator
-from iterables import IterableGenerator, ListComprehensionGenerator 
-from globalsgen import ChangeGlobalGenerator
-from recursion import TailRecursionGenerator
+from utils import eval_branches
 
-class LoopIntegerGenerator(FunctionGenerator):
-    def __init__(self, module, stats, opts, rng):
-        self.opts = opts
-        self.module = module
-        self.rng = rng
-        self.stats = stats
+from codegen.integergen import LoopIntegerGenerator, ArithIntegerGenerator
 
-    def get_iterable(self, opts, literals):
-        iter_gen = IterableGenerator(self.module, self.stats, self.opts, self.rng)
-        return iter_gen.get_iterable(literals)
-
-    def loop_integer(self, opts, args_num, globals):
-        '''Insert a new function with a loop containing some integer operations'''
-        args = self.generate_arguments(args_num)
- 
-        literals = set(args) | set(globals)
-        numbers = [n.set_rng(self.rng) for n in opts["numbers"]]
-        
-        f = self.create_function(args)
- 
-        result = self.next_variable()
-        literals.add(result)
-
-        iter = self.get_iterable(opts, literals)
-
-        loop_var = self.next_variable()
-        literals.add(loop_var)
-
-        l = ForLoop(loop_var, iter)
-        
-        if opts["if"] > self.rng.random():
-            exp1 = ArithGen(1, self.rng).generate(list(literals) + numbers)
-            exp2 = ArithGen(1, self.rng).generate(list(literals) + numbers)
-            
-            clause = " ".join([self.rng.choice(list(literals)), "<", self.rng.choice(list(literals))])
-            
-            i = IfStatement(clause, 
-                            [Assignment(result, '+=', [exp1])],
-                            [Assignment(result, '+=', [exp2])])
-            l.content.append(i)
-            
-        else:
-            exp = ArithGen(1, self.rng).generate(list(literals) + numbers)
-            l.content.append(Assignment(result, '+=', [exp]))
-            
-       
-        f.content.append(Assignment(result, '=', ['0']))
-        f.content.append(l)
-        f.content.append("return " + result)
-
-        return f
-
-class ArithIntegerGenerator(FunctionGenerator):
-    def __init__(self, module, stats, opts, rng):
-        self.opts = opts
-        self.module = module
-        self.rng = rng
-        self.stats = stats
-
-    def generate_statement(self, opts, f, gen, literals, numbers):
-        if opts["if"] > self.rng.random():
-            result = self.next_variable()
-                   
-            exp1 = gen.generate(list(literals) + numbers)
-            exp2 = gen.generate(list(literals) + numbers)
-                    
-            clause = self.rng.choice(list(literals)) + " < " + self.rng.choice(list(literals))
-                   
-            i = IfStatement(clause, 
-                [Assignment(result, '=', [exp1])],
-                [Assignment(result, '=', [exp2])])
-            f.content.append(i)
-
-        else:
-            result = self.next_variable()
-    
-            exp = gen.generate(list(literals) + numbers)
-            f.content.append(Assignment(result, '=', [exp]))
-            literals.add(result)
-
-    def generate_child(self, opts, f, literals):
-        branch = eval_branches(self.rng, opts["children"])
-        if branch == "arith_integer":
-            gen  = ArithIntegerGenerator(self.module, self.stats, self.opts, self.rng)
-            c = gen.arith_integer(opts, 2)
-            self.module.content.insert(0, c)
-
-            args = self.rng.sample(list(literals), 2)
-            result = self.next_variable()
-
-            call = Assignment(result, '=', [CallStatement(c, args)])
-            f.content.append(call)
-            literals.add(result)
-
-        if branch == ("arith_integer", "local"):
-            gen  = ArithIntegerGenerator(self.module, self.stats, self.opts, self.rng)
-            c = gen.arith_integer(opts, 2, list(literals))
-
-            f.content.append(c)
-
-            args = self.rng.sample(list(literals), 2)
-            result = self.next_variable()
-
-            call = Assignment(result, '=', [CallStatement(c, args)])
-            f.content.append(call)
-            literals.add(result)
-                   
-        if branch == "loop_integer":
-            gen  = LoopIntegerGenerator(self.module, self.stats, self.opts, self.rng)
-
-            c = gen.loop_integer(self.opts['loop_integer'], 2, [])
-            self.module.content.insert(0, c)
-
-            args = self.rng.sample(list(literals), 2)
-            result = self.next_variable()
-
-            call = Assignment(result, '=', [CallStatement(c, args)])
-            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)
-            
-        if branch == "integer_closure":
-            gen = IntegerClosureGenerator(self.module, self.stats, self.opts, self.rng)
-            func = gen.generate(self.opts['integer_closure'], 2, [])
-
-            args = self.rng.sample(list(literals), 2)
-            result = self.next_variable()
-
-            call = Assignment(result, '=', [CallStatement(func, args)])
-            f.content.append(call)
-            literals.add(result)
-
-        if branch == "tail_recursion":
-            gen = TailRecursionGenerator(self.module, self.stats, self.opts, self.rng)
-            func = gen.generate(self.opts['tail_recursion'], 2, [])
-
-            args = self.rng.sample(list(literals), 2)
-            result = self.next_variable()
-
-            call = Assignment(result, '=', [CallStatement(func, args)])
-            f.content.append(call)
-            literals.add(result)
-
-
-
-    def arith_integer(self, opts, args_num, globals=[]):
-        '''Insert a new arithmetic function using only integers'''
-        args = self.generate_arguments(args_num)
-        
-        f = self.create_function(args)
- 
-        literals = set(args) | set(globals)
-
-        children = min(self.rng.randint(0, opts["max_children"]), self.stats.prog_size)
-        if children > 0:
-            self.stats.prog_size -= children
-            for i in xrange(children):
-                self.generate_child(opts, f, literals)
-
-        numbers = [n.set_rng(self.rng) for n in opts["numbers"]]
-        branch_type = eval_branches(self.rng, opts["type"])
-        if branch_type == "thin":
-            gen = ArithGen(2, self.rng)
-            for i in xrange(self.rng.randint(10,25)):
-                self.generate_statement(opts, f, gen, literals, numbers)      
-               
-        if branch_type == "fat":
-            gen = ArithGen(20, self.rng)
-            for i in xrange(self.rng.randint(0,5)):
-                self.generate_statement(opts, f, gen, literals, numbers)
-
-        exp = ArithGen(10, self.rng).generate(list(literals) + numbers)
-        f.content.append(Assignment('result', '=', [exp]))
-        f.content.append('return result')
-
-        return f
  
 
 class ProgGenerator(object):
 
 
 
-class IntegerClosureGenerator(FunctionGenerator):
-    def __init__(self, module, stats, opts, rng):
-        self.opts = opts
-        self.module = module
-        self.rng = rng
-        self.stats = stats
 
-    def generate(self, opts, args_num, globals=[]):
-        '''Insert a new arithmetic function using only integers'''
-        args = self.generate_arguments(args_num)
-
-        closure = self.create_function(args)
-        
-        gen = self.create_function([])
-        
-        if opts["numbers"]:
-            number_gen = self.rng.choice(opts["numbers"])
-            number_gen.set_rng(self.rng)
-            number = number_gen()
-        else:
-            number = 0
-
-        
-        gen.content.extend(
-            [
-                "closure = [%s]" % (number, ),
-                closure,
-                Assignment("func", "=", [closure.name]),
-                "return func",
-            ]
-        )
-        
-        c_var = self.next_variable()
-        
-        self.module.content.insert(0, gen)
-        self.module.content.insert(1, Assignment(c_var, "=", [CallStatement(gen, [])]))
- 
-        gen_ai = ArithIntegerGenerator(self.module, self.stats, self.opts, self.rng)
-        f = gen_ai.arith_integer(self.opts["arith_integer"], args_num, [])
-        
-        self.module.content.insert(0, f)
-        
-        closure.content.append(Assignment("closure[0]", "+=", [CallStatement(f, args)]))
-        closure.content.append("return closure[0]")
-        
-        return c_var
-        
-

recursion.py

-from pygen.cgen import *
-from arithgen import ArithGen
-from iterables import IterableGenerator
-
-from utils import eval_branches, FunctionGenerator
-
-import pgen
-
-
-
-class TailRecursionGenerator(FunctionGenerator):
-    """
-    This generates some code to test tail recursion handling.
-    """
-    def __init__(self, module, stats, opts, rng):
-        self.opts = opts
-        self.module = module
-        self.rng = rng
-        self.stats = stats
-
-
-    def generate_standard_tail_call(self, opts):
-        if opts["numbers"]:
-            numbers = []
-            for i in xrange(4):
-                gen = self.rng.choice(opts["numbers"])
-                gen.set_rng(self.rng)
-                numbers.append(gen())
-        else:
-            numbers = ["1", "2", "3", "4"]
-
-        
-        func = self.create_function(["acc", "rest"])
-
-        exp = ArithGen(1, self.rng).generate(["acc", "rest"] + numbers)
-        
-        var = self.next_variable()
-        func.content.append(Assignment(var, '=', [exp]))
-
-        end = IfStatement("acc == 0",
-            ["return %s" % (var,)],
-            [Assignment("result", "=", [CallStatement(func, ["acc - 1", var])]),
-            "return result"])
-            
-        func.content.append(end)
-        return func
-
-    def generate_fcall_tail_call(self, opts):
-        if opts["numbers"]:
-            numbers = []
-            for i in xrange(4):
-                gen = self.rng.choice(opts["numbers"])
-                gen.set_rng(self.rng)
-                numbers.append(gen())
-        else:
-            numbers = ["1", "2", "3", "4"]
-
-        func = self.create_function(["acc", "rest"])
-
-        # generate an arith_integer function
-        gen  = pgen.ArithIntegerGenerator(self.module, self.stats, self.opts, self.rng)
-        c = gen.arith_integer(self.opts["arith_integer"], 2)
-        self.module.content.insert(0, c)
-
-        args = self.rng.sample(["acc", "rest"] + numbers, 2)
-        result = self.next_variable()
-
-        call = Assignment(result, '=', [CallStatement(c, args)])
-        func.content.append(call)
-
-        end = IfStatement("acc == 0",
-            ["return %s" % (result,)],
-            [Assignment("result", "=", [CallStatement(func, ["acc - 1", result])]),
-            "return result"])
-            
-        func.content.append(end)
-        return func
-
-
-    def generate_closure_tail_call(self, opts):
-        if opts["numbers"]:
-            numbers = []
-            for i in xrange(4):
-                gen = self.rng.choice(opts["numbers"])
-                gen.set_rng(self.rng)
-                numbers.append(gen())
-        else:
-            numbers = ["1", "2", "3", "4"]
-
-        
-        func = self.create_function(["acc", "rest"])
-
-        exp = ArithGen(5, self.rng).generate(["closure[0]", "acc", "rest"] + numbers)
-        
-        var = self.next_variable()
-        func.content.append(Assignment(var, '=', [exp]))
-        func.content.append(Assignment("closure[0]", '+=', [var]))
-
-        end = IfStatement("acc == 0",
-            ["return %s" % (var,)],
-            [Assignment("result", "=", [CallStatement(func, ["acc - 1", var])]),
-            "return result"])
-            
-        func.content.append(end)
-        return func
-
-
-    def generate(self, opts, args_num, globals):
-        args = self.generate_arguments(args_num)
-        func = self.create_function(args)
-        
-        branch = eval_branches(self.rng, opts["type"])
-        
-        if branch == "standard":
-            rec = self.generate_standard_tail_call(opts)
-        elif branch == "closure":
-            func.content.append(Assignment("closure", "=", ["[0]"]))
-            rec = self.generate_closure_tail_call(opts)
-        else:
-            rec = self.generate_fcall_tail_call(opts)
-            
-        func.content.append(rec)
-        
-        func.content.extend(
-            [Assignment("result", "=", [CallStatement(rec, ["10", "0"])]),
-            "return result"])
-        
-        self.module.content.append(func)
-        
-        return func
-
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.