Commits

Anonymous committed d671c71

refactored classes

Comments (0)

Files changed (1)

codegen/classes.py

         self.branches = [
             (1.0, self.generate_monomorphic),
             (1.0, self.generate_polymorphic),
+            (1.0, self.generate_duck),
         ]
 
     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):
-        branch = eval_branches(self.rng, self.branches)
-        return branch(literals)
-
-    def generate_class_function(self):
+    def make_class_function(self):
         c = self.create_class()
         self.module.content.insert(0, c)
 
         c.content.append(m)
         return c, m
 
-
-    def generate_monomorphic(self, literals):
-        """Generates a monomorphic callsite"""
-        c, m = self.generate_class_function()
-
-        result = []
-
-        class_var = self.next_variable()
-        result.append(Assignment(class_var, '=', [CallStatement(c, [])]))
-
+    def make_loop(self, literals, class_var, m):
         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]
         if self.rng.random() < 0.5:
             func = class_var + '.' + m.name
 
         l.content.append(CallStatement(func, args))
 
+        return l
+
+    def generate_inline(self, literals):
+        branch = eval_branches(self.rng, self.branches)
+        return branch(literals)
+
+    def generate_monomorphic(self, literals):
+        """Generates a monomorphic callsite"""
+        c, m = self.make_class_function()
+
+        result = []
+
+        class_var = self.next_variable()
+        result.append(Assignment(class_var, '=', [CallStatement(c, [])]))
+
+        l = self.make_loop(literals, class_var, m)
+        result.append(l)
         return result
 
-
     def generate_polymorphic(self, literals):
         """Generate a polymorphic callsite"""
-        c, m = self.generate_class_function()
-        c_super, m_super = self.generate_class_function()
+        c, m = self.make_class_function()
+        c_super, m_super = self.make_class_function()
         m_super.name = m.name
 
-        # test duck typing and class polymorphism
         c.super = [c_super.name]
 
-        loop_var = self.next_variable()
-        iter = self.get_iterable(literals)
-
         class_var = self.next_variable()
         clause = self.rng.choice(list(literals)) + " < " + self.rng.choice(list(literals))
         i = IfStatement(clause,
         )
         result = [i]
 
-        l = ForLoop(loop_var, iter)
+        l = self.make_loop(literals, class_var, m)
         result.append(l)
-        loop_literals = list(literals) + [loop_var]
 
-        if self.rng.random() < 0.5:
-            func = class_var + '.' + m.name
-        else: # Sometimes copy the function into a variable
-            func = self.next_variable()
-            l.content.append(Assignment(func, '=', [class_var + '.' + m.name]))
-
-        args = [self.rng.choice(loop_literals) for i in m.args]
-        l.content.append(CallStatement(func, args))
         return result
 
-
     def generate_duck(self, literals):
         """Generate a duck typing callsite"""
-        c, m = self.generate_class_function()
-        c_super, m_super = self.generate_class_function()
+        c, m = self.make_class_function()
+        c_super, m_super = self.make_class_function()
         m_super.name = m.name
 
-        loop_var = self.next_variable()
-        iter = self.get_iterable(literals)
-
         class_var = self.next_variable()
         clause = self.rng.choice(list(literals)) + " < " + self.rng.choice(list(literals))
         i = IfStatement(clause,
         )
         result = [i]
 
-        l = ForLoop(loop_var, iter)
+        l = self.make_loop(literals, class_var, m)
         result.append(l)
-        loop_literals = list(literals) + [loop_var]
 
-        if self.rng.random() < 0.5:
-            func = class_var + '.' + m.name
-        else: # Sometimes copy the function into a variable
-            func = self.next_variable()
-            l.content.append(Assignment(func, '=', [class_var + '.' + m.name]))
-
-        args = [self.rng.choice(loop_literals) for i in m.args]
-        l.content.append(CallStatement(func, args))
         return result
 
-    def generate_super(self, literals):
-        pass
 
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.