[pypy-svn] rev 1696 - in pypy/trunk/src/pypy/translator: . test

sanxiyn at codespeak.net sanxiyn at codespeak.net
Sat Oct 11 10:10:30 CEST 2003


Author: sanxiyn
Date: Sat Oct 11 10:10:29 2003
New Revision: 1696

Modified:
   pypy/trunk/src/pypy/translator/gencl.py
   pypy/trunk/src/pypy/translator/test/buildclisp.py
   pypy/trunk/src/pypy/translator/test/test_cltrans.py
   pypy/trunk/src/pypy/translator/translator.py
Log:
Now GenCL has emitcode() interface, and integrated into
translator.py. (Try 'print translator.cl()')

Tests test_cltrans iff environment variable PYPY_CL is
defined. Otherwise skips by raising TestSkip.


Modified: pypy/trunk/src/pypy/translator/gencl.py
==============================================================================
--- pypy/trunk/src/pypy/translator/gencl.py	(original)
+++ pypy/trunk/src/pypy/translator/gencl.py	Sat Oct 11 10:10:29 2003
@@ -8,15 +8,21 @@
         self.args = op.args
         self.result = op.result
     def __call__(self):
-        meth_name = "op_" + self.opname
-        meth_default = self.op_default
-        meth = getattr(self, meth_name, meth_default)
-        meth()
+        if self.opname in self.binary_ops:
+            self.op_binary(self.opname)
+        else:
+            self.op_default()
+    binary_ops = {
+        "add": "+",
+        "mod": "mod",
+    }
     def op_default(self):
-        print self.opname, "is missing"
-    def op_mod(self):
-        result, arg1, arg2 = map(self.str, (self.result,) + self.args)
-        print "(setq", result, "(mod", arg1, arg2, "))"
+        print "; Op", self.opname, "is missing"
+    def op_binary(self, op):
+        s = self.str
+        result, (arg1, arg2) = self.result, self.args
+        cl_op = self.binary_ops[op]
+        print "(setq", s(result), "(", cl_op, s(arg1), s(arg2), "))"
 
 class GenCL:
     def __init__(self, fun):
@@ -25,6 +31,25 @@
     def str(self, obj):
         if isinstance(obj, Variable):
             return obj.pseudoname
+        elif isinstance(obj, Constant):
+            return self.conv(obj.value)
+        else:
+            return "#<" # unreadable
+    def conv(self, val):
+        if val is None:
+            return "nil"
+        elif isinstance(val, int):
+            return str(val)
+        else:
+            return "#<" # unreadable
+    def emitcode(self):
+        import sys
+        from cStringIO import StringIO
+        out = StringIO()
+        sys.stdout = out
+        self.emit()
+        sys.stdout = sys.__stdout__
+        return out.getvalue()
     def emit(self):
         self.emit_defun(self.fun)
     def emit_defun(self, fun):

Modified: pypy/trunk/src/pypy/translator/test/buildclisp.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/buildclisp.py	(original)
+++ pypy/trunk/src/pypy/translator/test/buildclisp.py	Sat Oct 11 10:10:29 2003
@@ -10,19 +10,16 @@
     # For now, let's return int only
     return int(s)
 
-def make_cl_func(func, path):
+def make_cl_func(func, cl, path):
     fun = Space().build_flow(func)
     gen = GenCL(fun)
-    out = StringIO()
-    sys.stdout = out
-    gen.emit()
-    sys.stdout = sys.__stdout__
+    out = gen.emitcode()
     fp = path.join("test.lisp")
     i = 0
     while fp.exists():
         fp = path.join("test%d.lisp" % i)
         i += 1
-    fp.write(out.getvalue())
+    fp.write(out)
     fname = fp.path
     def _(*args):
         fp = file(fname, "a")
@@ -31,6 +28,6 @@
             print >>fp, str(arg),
         print >>fp, "))"
         fp.close()
-        output = exec_cmd("clisp %s" % fname)
+        output = exec_cmd("%s %s" % (cl, fname))
         return readlisp(output)
     return _

Modified: pypy/trunk/src/pypy/translator/test/test_cltrans.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_cltrans.py	(original)
+++ pypy/trunk/src/pypy/translator/test/test_cltrans.py	Sat Oct 11 10:10:29 2003
@@ -5,6 +5,14 @@
 
 class GenCLTestCase(test.IntTestCase):
 
+    def setUp(self):
+        import os
+        cl = os.getenv("PYPY_CL")
+        if cl:
+            self.cl = cl
+        else:
+            raise test.TestSkip
+
     #___________________________________
     def my_gcd(a, b):
         r = a % b
@@ -13,11 +21,8 @@
             b = r
             r = a % b
         return b
-    def XXXtest_gcd(self):
-        # disabled because it's rude to fail just because the clisp
-        # common lisp implementation isn't installed.
-        # (will arrange to skip the test in that case eventually) -- mwh
-        cl_gcd = make_cl_func(self.my_gcd, udir)
+    def test_gcd(self):
+        cl_gcd = make_cl_func(self.my_gcd, self.cl, udir)
         self.assertEquals(cl_gcd(96, 64), 32)
 
 if __name__ == '__main__':

Modified: pypy/trunk/src/pypy/translator/translator.py
==============================================================================
--- pypy/trunk/src/pypy/translator/translator.py	(original)
+++ pypy/trunk/src/pypy/translator/translator.py	Sat Oct 11 10:10:29 2003
@@ -36,6 +36,7 @@
 from pypy.translator.annotation import Annotator
 from pypy.translator.simplify import simplify_graph
 from pypy.translator.genpyrex import GenPyrex
+from pypy.translator.gencl import GenCL
 from pypy.translator.test.buildpyxmodule import make_module_from_pyxstring
 from pypy.objspace.flow import FlowObjSpace
 
@@ -77,6 +78,10 @@
             g.setannotator(self.annotator)
         return g.emitcode()
 
+    def cl(self):
+        g = GenCL(self.flowgraph)
+        return g.emitcode()
+
     def compile(self):
         from pypy.tool.udir import udir
         name = self.entrypoint.func_name


More information about the Pypy-commit mailing list