Commits

Anonymous committed 9fb5387

Fill classes with some code

Comments (0)

Files changed (2)

codegen/classes.py

 from pygen.cgen import *
-from arithgen import ArithGen
+from arithgen import ArithGen, gen_max_int_gen
 import iterables
 
 from utils import eval_branches, FunctionGenerator
 
         return l
 
+    def make_fill(self, m):
+        filled = [(1.0, self.fill_zero),
+                  (1.0, self.fill_some_arith)]
+
+        branch = eval_branches(self.rng, filled)
+        branch(m)
+
+    def fill_zero(self, m):
+        m.content.append('return 0')
+
+    def fill_some_arith(self, m):
+        def low_numbers():
+            return str(self.rng.randint(-1,1))
+
+        numbers = [gen_max_int_gen().set_rng(self.rng), low_numbers]
+        exp = ArithGen(5, self.rng).generate(m.args + numbers)
+
+        m.content.extend([
+            Assignment('result', '=', [exp]),
+            'return result',
+        ])
+
     def generate_inline(self, literals):
         branch = eval_branches(self.rng, self.branches)
         return branch(literals)
         """Generates a monomorphic callsite"""
         c, m = self.make_class_function()
 
+        self.make_fill(m)
+
         result = []
 
         class_var = self.next_variable()
 
         c.super = [c_super.name]
 
+        self.make_fill(m)
+        self.make_fill(m_super)
+
         class_var = self.next_variable()
         clause = self.rng.choice(list(literals)) + " < " + self.rng.choice(list(literals))
         i = IfStatement(clause,
         c_super, m_super = self.make_class_function()
         m_super.name = m.name
 
+        self.make_fill(m)
+        self.make_fill(m_super)
+
         class_var = self.next_variable()
         clause = self.rng.choice(list(literals)) + " < " + self.rng.choice(list(literals))
         i = IfStatement(clause,
     def __init__(self):
         self.content = []
 
+class TRng(object):
+    def __init__(self, tc):
+        self.tc = tc
+
+    def random(self):
+        return 0.0
+
+    def randint(self, a, b):
+        self.tc.assert_(a <= b)
+        return a
+
+    def choice(self, seq):
+        self.tc.assert_(len(seq) > 0)
+        return seq[0]
+
 class TestClassesGenerator(unittest.TestCase):
 
     def codegen(self, l):
         self.module = TModule()
         self.stats = TStats()
 
-        self.gen = ClassGenerator(self.module, self.stats, self.opts)
+        self.gen = ClassGenerator(self.module, self.stats, self.opts, TRng(self))
 
         self.fgen = FixGenerator()
         self.cgen = CodeGenerator()
 
+    def testFillSomeArith(self):
+        m = Method("M", ['arg'])
+        self.gen.fill_some_arith(m)
+
+        code = self.codegen([m])
+        self.assert_('def M' in code)
+        self.assert_('return result' in code)
+        self.assert_('result =' in code)
+
     def testMonomorphic(self):
         result = self.gen.generate_monomorphic(['0', '1'])
         result.extend(self.module.content)
         self.assert_('for' in code)
         self.assert_('.func' in code)
         self.assert_('= class' in code)
+        self.assert_('return 0' in code)
 
     def testPolymorphic(self):
         result = self.gen.generate_polymorphic(['0', '1'])