Commits

ebo  committed e43b786

Whitespace cleanup.

  • Participants
  • Parent commits 6242f84

Comments (0)

Files changed (12)

File codegen/arithgen.py

 
         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 = ['+', '-', '&', '^', '|'] #, '%', '/', '//', '*' 
-    
+
+    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(l)
             else:
                 expr.append(l())
-            
+
         expr.append(self.rng.choice(self.bin_ops))
 
         if self.bin_ops_left and self.rng.randint(0, 1):
                 expr.append(l)
             else:
                 expr.append(l())
-            
+
         return " ".join(expr)
 
     def add_bracket(self, literals):
         return "".join(['(', self.add_op(literals), ')'])
-    
-    
+

File codegen/fungen.py

 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
 
     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, 
+            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),
                         )
             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
 

File codegen/globalsgen.py

 
 class ChangeGlobalGenerator(FunctionGenerator):
     """
-    This generates some code to change a global 
+    This generates some code to change a global
     and test if the changed function is executed.
     """
     def __init__(self, module, stats, opts, rng):
 
     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

File codegen/integergen.py

 from arithgen import ArithGen
 
 from utils import eval_branches, FunctionGenerator
-from iterables import IterableGenerator, ListComprehensionGenerator 
+from iterables import IterableGenerator, ListComprehensionGenerator
 from globalsgen import ChangeGlobalGenerator
 from recursion import TailRecursionGenerator
 
     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, 
+
+            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)
             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)
 
 
         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, [])
     def generate(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 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)      
-               
+                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)):
     def generate(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)
 
         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, 
+
+            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)
         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)
         else:
             number = 0
 
-        
+
         gen.content.extend(
             [
                 "closure = [%s]" % (number, ),
                 "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.generate(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
-        
 
 
+

File codegen/iterables.py

         c = gen.generate(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)])

File codegen/recursion.py

         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]))
 
             ["return %s" % (var,)],
             [Assignment("result", "=", [CallStatement(func, ["acc - 1", var])]),
             "return result"])
-            
+
         func.content.append(end)
         return func
 
             ["return %s" % (result,)],
             [Assignment("result", "=", [CallStatement(func, ["acc - 1", result])]),
             "return result"])
-            
+
         func.content.append(end)
         return func
 
         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]))
             ["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":
             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
 
 
 from codegen.integergen import LoopIntegerGenerator, ArithIntegerGenerator
 
- 
+
 
 class ProgGenerator(object):
     def __init__(self, opts, rng):
         self.prog_size = lopts["prog_size"]
         self.module_size = lopts["module_size"] - self.prog_size
 
- 
+
         while self.module_size > 0 or self.prog_size > 0:
             main = []
 
                     main.append(Assignment('x', '=', ['5.0']))
                     main.append('print x,')
 
-            self.module.main_body.append("print 'prog_size: %d'" % 
+            self.module.main_body.append("print 'prog_size: %d'" %
                             (lopts["prog_size"] - self.prog_size,))
             self.module.main_body.append("print 'func_number: %d'" % (self.func_number,))
             self.module.main_body.append("print 'arg_number: %d'" % (self.arg_number,))
         f = gen.generate(opts, args_num, globals)
 
         return f
-        
 
+

File pygen-example.py

         seed = int(rng.random() * sys.maxint)
     print "Using random seed", seed
     rng.seed(seed)
-    
+
     pgen = ProgGenerator(pgen_opts, rng)
-    
+
     gen = CodeGenerator()
-    fix = FixGenerator() 
+    fix = FixGenerator()
     totaltime = 0.0
     time_test = 0.0
     time_base = 0.0
     failed_a_test = False
-    
+
     for i in xrange(options.iterations):
-        
+
         failed_this_test = False
         clock = time.time()
         mod = pgen.generate()
         prefix = os.path.join(options.codepath, prefix)
         with open(prefix + ".pickle", "w") as code_pickle:
             pickle.dump(fixed, code_pickle)
-            
+
         code = gen.generate(fixed)
 
         code_path = prefix + ".py"
         with open(code_path, "w") as code_file:
             code_file.write(code)
-        
-        clock_test = time.time()    
+
+        clock_test = time.time()
         test_command = [options.test] + options.testargs.split() + [code_path]
         p_test = subprocess.Popen(test_command,
                     stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
             if options.break_on_error:
                 return
 
-       
+
         clock_base = time.time()
         base_command = [options.base] + options.baseargs.split() + [code_path]
         p_base = subprocess.Popen(base_command,
         clock_base = time.time() - clock_base
         time_base += clock_base
         totaltime += clock_base
-        
+
 
         if p_base.returncode < 0:
             print "------- Encountered crash: Base -------"
             if options.break_on_error:
                 return
 
-           
+
 #        if clock_test < clock_base/2.0:
 #            print "------- Fast test -------"
 #            print "Test", clock_test
                 return
 
 
-        
+
 #        if clock > 10.0:
 #            print code
-        
+
         failed_a_test |= failed_this_test
         if not failed_this_test:
             print "Iteration %s: PASS" % (i + 1,)

File pygen/cgen.py

     def __init__(self, main = False):
         self.has_main = main
         self.main_body = []
-        
+
         self.content = []
-        
+
 
 class ForLoop(object):
     def __init__(self, pointer, iterable, content = None):
             self.content = content
         else:
             self.content = []
-        
-        
+
+
 class IfStatement(object):
     def __init__(self, clause, true_content = None, false_content = None):
         self.clause = clause
             self.content = content
         else:
             self.content = []
-        
+
 class CallStatement(object):
     def __init__(self, func, args):
         self.func = func
         self.target = target
         self.operator = operator
         self.expression = expression
-        
+
 class Statement(object):
     pass
 
 class FixGenerator(object):
     def __init__(self):
         pass
-    
+
     def visit_block(self, block):
         fixed = []
         for stmt in block:
             if callable(stmt):
                 fixed.append(stmt())
                 continue
-            
+
             fixed.append(self.visit(stmt))
         return fixed
-    
+
     def visit_args(self, args):
         return [self.visit_expr(arg) for arg in args]
 
-    def visit_expr(self, expr):   
+    def visit_expr(self, expr):
         if isinstance(expr, str):
             return expr
-        
+
         if isinstance(expr, list):
             return self.visit_args(expr)
 
         if callable(expr):
             return expr()
-         
+
         return self.visit(expr)
- 
+
     def generate(self, node):
         return self.visit(node)
-    
+
     @dispatch.on('node')
     def visit(self, node):
         pass
-    
+
     @visit.when(Module)
     def visit(self, node):
         m = Module(node.has_main)
         m.content = self.visit_block(node.content)
         m.main_body = self.visit_block(node.main_body)
-        
+
         return m
 
     @visit.when(Statement)
     def visit(self, node):
         return node.fix()
-    
+
     @visit.when(CallStatement)
     def visit(self, node):
-        stmt = CallStatement(node.func, 
+        stmt = CallStatement(node.func,
                     self.visit_args(node.args))
-        
-        return stmt 
+
+        return stmt
 
     @visit.when(Function)
     def visit(self, node):
         func = Function(node.name)
         func.args = self.visit_args(node.args)
         func.content = self.visit_block(node.content)
-        
+
         return func
 
     @visit.when(IfStatement)
         stmt = IfStatement(node.clause)
         stmt.true_content = self.visit_block(node.true_content)
         stmt.false_content = self.visit_block(node.false_content)
-        
+
         return stmt
-        
+
     @visit.when(ForLoop)
     def visit(self, node):
-        stmt = ForLoop(node.pointer, node.iterable, 
+        stmt = ForLoop(node.pointer, node.iterable,
                        self.visit_block(node.content))
-        
+
         return stmt
-    
+
     @visit.when(Assignment)
     def visit(self, node):
         stmt = Assignment(node.target, node.operator, self.visit_block(node.expression))
 class CodeGenerator(object):
     def __init__(self):
         pass
-    
+
     def generate(self, node):
         code = self.visit(0, node)
         return "\n".join(code)
-        
+
     def code(self, depth, line):
         code = []
         for i in xrange(depth):
         if isinstance(line, list):
             line = "".join(line)
         code.append(line)
-        
+
         return "".join(code)
-    
+
     def visit_block(self, depth, block):
         if len(block) == 0:
             return [self.code(depth+1, 'pass')]
             if callable(node):
                 content.append(self.code(depth+1, node()))
                 continue
-            
+
             content += self.visit(depth+1, node)
         return content
 
     def visit(self, depth, node):
         '''Generic visit function'''
         return []
-        
-       
+
+
     @visit.when(Statement)
     def visit(self, depth, node):
         return [self.code(depth, node.get())]
-       
+
     @visit.when(Module)
     def visit(self, depth, node):
         if depth != 0:
             raise CodeGenIndentException()
-    
+
         content = []
         for n in node.content:
             if isinstance(n, str):
                 content.append(self.code(depth, n))
                 continue
             content += self.visit(depth, n)
-    
+
         if node.has_main:
             content.append(self.code(depth, 'if __name__ == "__main__":'))
             content += self.visit_block(depth, node.main_body)
         content.append(self.code(depth, "else:"))
         content += self.visit_block(depth, node.false_content)
         return content
-        
+
 
     @visit.when(Function)
     def visit(self, depth, node):
     @visit.when(CallStatement)
     def visit(self, depth, node):
         args = ", ".join(self.visit_args(node.args))
-        
+
         if isinstance(node.func, basestring):
             fun = "".join([node.func, '(', args, ')'])
         else:

File pygen/qndispatch/qndispatch.py

             def when_decorate(func):
                 dispatcher.register(func, arg)
                 return decorator
-            
+
             return when_decorate
 
         decorator.when = when

File pygen/tests.py

     def testEmptyModule(self):
         p = Module()
         p = self.fix.visit(p)
-        
-        
+
+
         self.assertEqual(self.gen.generate(p), '')
-        
+
     def testEmptyMainModule(self):
         p = Module(main=True)
         p = self.fix.generate(p)
-        
-        
+
+
         code = self.gen.generate(p)
         self.assert_('__main__' in code)
         self.assert_('    pass' in code)
-        
+
     def testContent(self):
         p = Module()
         p.content.append("a")
         self.assert_('a' in self.gen.generate(p))
-        
+
         p = self.fix.generate(p)
-        
+
         self.assert_('a' in self.gen.generate(p))
 
     def testContentExtended(self):
         p = Module()
         p.content.append(Function("function"))
-        
+
         p = self.fix.generate(p)
-        
+
         self.assert_('function' in self.gen.generate(p))
 
 class TestForLoop(unittest.TestCase):
     def testEmptyForLoop(self):
         n = ForLoop("i", ["xrange(10)"], [])
         n = self.fix.generate(n)
-        
-        
+
+
         code = self.gen.generate(n)
         self.assert_("for i" in code)
         self.assert_("xrange(10):" in code)
         self.assert_("    pass" in code)
 
-    
+
     def testStringForLoop(self):
         n = ForLoop("i", ["xrange(10)"], ['pass'])
         n = self.fix.generate(n)
-        
-        
+
+
         code = self.gen.generate(n)
         self.assert_("for i" in code)
         self.assert_("xrange(10):" in code)
     def testEmptyIfStatement(self):
         n = IfStatement("i", [])
         n = self.fix.generate(n)
-        
-        
+
+
         code = self.gen.generate(n)
         self.assert_("if i:" in code)
         self.assert_("pass" in code)
 
         n = IfStatement("i", [], [])
         n = self.fix.generate(n)
-        
-        
+
+
         code = self.gen.generate(n)
         self.assert_("if i:" in code)
         self.assert_("pass" in code)
     def testEmptyFunction(self):
         n = Function("test")
         n = self.fix.generate(n)
-        
-        
+
+
         code = self.gen.generate(n)
         self.assert_("def test" in code)
         self.assert_("pass" in code)
 
         def CallableStatement():
             return "CallableStatement"
-        
+
         n = Function("test")
         n.content.append(TestStatement())
         n.content.append("StringStatement")
         n.content.append(CallableStatement)
 
-        
+
         code = self.gen.generate(n)
 
         self.assert_("def test" in code)
         self.assert_("pass" not in code)
-        
+
         self.assert_("TestStatement" in code)
         self.assert_("StringStatement" in code)
         self.assert_("CallableStatement" in code)
 
         # Now fix all statements
-        
+
         n = self.fix.generate(n)
-        
+
         code = self.gen.generate(n)
-        
+
         self.assert_("def test" in code)
         self.assert_("pass" not in code)
-        
+
         self.assert_("TestStatement" in code)
         self.assert_("StringStatement" in code)
         self.assert_("CallableStatement" in code)
         func = Function("test")
         n = CallStatement(func, ['arg1', 'arg2'])
         n = self.fix.generate(n)
-        
+
         code = self.gen.generate(n)
-        
+
         self.assert_("test" in code)
         self.assert_("(arg1, arg2)" in code)
 
     def testAssignment(self):
         def CallableStatement():
             return "CallableStatement"
-        
+
         n = Assignment("target", "=", [CallableStatement])
-        
+
         code = self.gen.generate(n)
-        
+
         self.assert_("target = CallableStatement" in code)
-        
+
         n = self.fix.generate(n)
         code = self.gen.generate(n)
-        
+
         self.assert_("target = CallableStatement" in code)
-        
+
 class TestFixGenerator(unittest.TestCase):
     def setUp(self):
         self.gen = FixGenerator()
-        
+
     def testEmptyGenerator(self):
         try:
             self.gen.generate(None)
                 return "a"
             def fix(self):
                 return "a"
-            
+
         fixed = self.gen.visit(TestStatement())
         self.assertEqual(fixed, "a")
 
                 return "a"
             def fix(self):
                 return "a"
-        
+
         def func():
             return "c"
 
 
     def setUp(self):
         self.gen = CodeGenerator()
-    
+
     def testEmptyGenerator(self):
         try:
             self.gen.generate(None)
             self.fail()
         except:
             pass
-        
+
     def testCodeFunction(self):
         self.assertEqual(self.gen.code(4, 'a'), ('    '*4) + 'a')
-        
+
     def testStatement(self):
         class TestStatement(Statement):
             def get(self):
                 return "a"
-            
+
         self.assert_("a" in self.gen.generate(TestStatement()))
-        
+
     def testVisitArgs(self):
         code = self.gen.visit_args(['b'])
         self.assert_('b' in code)
-        
+
         code = self.gen.visit_args([['b']])
         self.assert_('b' in code)
-          
+
         def func():
             return "c"
 
 if __name__ == "__main__":
     #import sys;sys.argv = ['', 'Test.testName']
     unittest.main()
-    
-    
+
+
 import unittest
 
 from pgen import *
-
+from utils import FunctionGenerator
 
 class TestUtilFunctions(unittest.TestCase):
     def testEvalBranches(self):
 
         result = eval_branches(rng, [(0.5, 0), (1.0, 1)])
         self.assertEqual(result, 1)
-    
+
 class TestFunctionGenerator(unittest.TestCase):
 
     def setUp(self):
         self.assertEqual(module.func_number, 1)
         self.assert_(f.name == "func0")
         self.assert_("args" in f.args)
- 
+
     def testGenerateArguments(self):
         class TestModule(object):
             pass
         fgen.stats = module
 
         args = fgen.generate_arguments(5)
-           
+
         self.assertEqual(len(args), 5)
         self.assert_("arg0" in args)
         self.assert_("arg4" in args)
         self.assertEqual("var0", var)
         self.assertEqual(module.arg_number, 1)
 
-       
+
 
 if __name__ == "__main__":
     #import sys;sys.argv = ['', 'Test.testName']
     unittest.main()
-    
-    
+
+