Anonymous avatar Anonymous committed 52fc444

Added simple classes handling

Comments (0)

Files changed (8)

codegen/classes.py

+from pygen.cgen import *
+from arithgen import ArithGen
+import iterables
+
+from utils import eval_branches, FunctionGenerator
+
+import pgen
+
+import random
+
+
+class ClassGenerator(FunctionGenerator):
+    def __init__(self, module, stats, opts, rng=None):
+        self.opts = opts
+        self.module = module
+        self.rng = rng
+        if not rng:
+            self.rng = random.Random()
+        self.stats = stats
+
+        self.branches = [
+            (1.0, self.generate_monomorphic),
+#            (),
+        ]
+
+    def get_iterable(self, literals):
+        iter_gen = iterables.IterableGenerator(self.module, self.stats, self.opts, self.rng)
+        return iter_gen.get_iterable(literals)
+
+
+    def generate_inline(self, literals):
+        c = self.create_class()
+        self.module.content.insert(0, c)
+
+        args = self.generate_arguments(2)
+        m = self.create_method(args)
+
+        c.content.append(m)
+
+        branch = eval_branches(self.rng, self.branches)
+        return branch(c, m, literals)
+        
+
+
+    def generate_monomorphic(self, c, m, literals):
+        result = []
+
+        class_var = self.next_variable()
+        result.append(Assignment(class_var, '=', [CallStatement(c, [])]))
+
+        loop_var = self.next_variable()
+        iter = self.get_iterable(literals)
+ 
+        l = ForLoop(loop_var, iter)
+        result.append(l)
+        
+        loop_literals = list(literals) + [loop_var]
+
+        args = [self.rng.choice(loop_literals) for i in m.args]
+        l.content.append(CallStatement(class_var + '.' + m.name, args))
+
+        return result
+
+
+
+

codegen/globalsgen.py

 from pygen.cgen import *
 from arithgen import ArithGen
-from iterables import IterableGenerator
+#from iterables import IterableGenerator
+import iterables
 
 from utils import eval_branches, FunctionGenerator
 
         self.module.content.insert(0, fon)
         self.module.content.insert(0, foff)
 
-        iter_gen = IterableGenerator(self.module, self.stats, self.opts, self.rng)
+        iter_gen = iterables.IterableGenerator(self.module, self.stats, self.opts, self.rng)
 
         if opts["numbers"]:
             numbers = []

codegen/integergen.py

 from arithgen import ArithGen
 
 from utils import eval_branches, FunctionGenerator
-from iterables import IterableGenerator, ListComprehensionGenerator
+#from iterables import IterableGenerator, ListComprehensionGenerator
+import iterables
 from globalsgen import ChangeGlobalGenerator
 from recursion import TailRecursionGenerator
-
+import classes
 
 class ArithIntegerGenerator(FunctionGenerator):
     def __init__(self, module, stats, opts, rng):
             f.content.append(call)
             literals.add(result)
 
+        if branch == "classes":
+            gen = classes.ClassGenerator(self.module, self.stats, self.opts, self.rng)
+            result = gen.generate_inline(literals)
 
+            f.content.extend(result)
 
     def generate(self, opts, args_num, globals=[]):
         '''Insert a new arithmetic function using only integers'''
         self.stats = stats
 
     def get_iterable(self, opts, literals):
-        iter_gen = IterableGenerator(self.module, self.stats, self.opts, self.rng)
+        iter_gen = iterables.IterableGenerator(self.module, self.stats, self.opts, self.rng)
         return iter_gen.get_iterable(literals)
 
     def generate(self, opts, args_num, globals):

codegen/recursion.py

 from pygen.cgen import *
 from arithgen import ArithGen
-from iterables import IterableGenerator
 
 from utils import eval_branches, FunctionGenerator
 
+import unittest
+
+from pygen.cgen import *
+from classes import ClassGenerator
+
+class Stats(object):
+    arg_number = 0
+    prog_size = 100
+
+class TestClassesGenerator(unittest.TestCase):
+
+    def codegen(self, l):
+        result = []
+        for line in l:
+            result.append(self.cgen.generate(line))
+
+        return '\n'.join(result)
+
+    def setUp(self):
+        self.opts = {'iter_gen' : {'type' : [(1.0, "xrange")], 'children' : []}}
+        self.module = {}
+        self.stats = Stats()
+
+        self.gen = ClassGenerator(self.module, self.stats, self.opts)
+
+        self.c = Class('C1')
+        self.m = Method('M', ['a','b'])
+        self.c.content.append(self.m)
+
+        self.cgen = CodeGenerator()
+
+    def testMonomorphic(self):
+        result = self.gen.generate_monomorphic(self.c, self.m, ['1','1'])
+        result.insert(0, self.c)
+
+        code = self.codegen(result)
+
+        self.assert_('for' in code)
+        self.assert_('.M(' in code)
+        self.assert_('C1()' in code)
+
                         (1.0, "change_global"),
                         (1.0, "integer_closure"),
                         (1.0, "tail_recursion"),
+                        (1.0, "classes"),
                         ],
                 "max_children" : 5,
                 "numbers" : [gen_max_int_gen(), IntegerGen(-1000, 1000)],
 #!/bin/bash
 
-nosetests --with-coverage --cover-package=pygen,pgen,arithgen,utils,iterables --cover-erase
+nosetests --with-coverage --cover-package=pygen,pgen,utils,codegen --cover-erase
         self.stats.func_number += 1
         return f
 
+    def create_class(self):
+        c = Class("class%d" % (self.stats.func_number,))
+        self.stats.func_number += 1
+        return c
 
+    def create_method(self, args):
+        m = Method("func%d" % (self.stats.func_number,), args)
+        self.stats.func_number += 1
+        return m
+
+
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.