Anonymous avatar Anonymous committed f552d2a

separated polymorphis and duck typing

Comments (0)

Files changed (2)

codegen/classes.py

         return result
 
 
-    def generate_polymorphic(self, literals, use_duck_typing=True):
+    def generate_polymorphic(self, literals):
         """Generate a polymorphic callsite"""
         c, m = self.generate_class_function()
         c_super, m_super = self.generate_class_function()
         m_super.name = m.name
 
         # test duck typing and class polymorphism
-        if not use_duck_typing or self.rng.random() < 0.5:
-            c.super = [c_super.name]
-            if self.rng.random() < 0.5:
-                c.content.remove(m)
+        c.super = [c_super.name]
 
         loop_var = self.next_variable()
         iter = self.get_iterable(literals)
         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()
+        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,
+            [Assignment(class_var, '=', [CallStatement(c, [])])],
+            [Assignment(class_var, '=', [CallStatement(c_super, [])])]
+        )
+        result = [i]
+
+        l = ForLoop(loop_var, iter)
+        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
+
         self.assert_('= class' in code)
 
     def testPolymorphic(self):
-        result = self.gen.generate_polymorphic(['0', '1'], use_duck_typing=False)
+        result = self.gen.generate_polymorphic(['0', '1'])
         result.extend(self.module.content)
 
         code = self.codegen(result)
         self.assert_('= class' in code) # object instantiation
         self.assert_('(class' in code) # inheritance
 
+    def testDuck(self):
+        result = self.gen.generate_duck(['0', '1'])
+        result.extend(self.module.content)
+
+        code = self.codegen(result)
+
+        self.assert_('for' in code)
+        self.assert_('.func' in code) # method call
+        self.assert_('= class' in code) # object instantiation
+
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.