[pypy-svn] r26406 - in pypy/dist/pypy/translator/cl: . test

sanxiyn at codespeak.net sanxiyn at codespeak.net
Thu Apr 27 08:55:47 CEST 2006


Author: sanxiyn
Date: Thu Apr 27 08:55:37 2006
New Revision: 26406

Modified:
   pypy/dist/pypy/translator/cl/buildcl.py
   pypy/dist/pypy/translator/cl/clrepr.py
   pypy/dist/pypy/translator/cl/gencl.py
   pypy/dist/pypy/translator/cl/test/test_clrepr.py
   pypy/dist/pypy/translator/cl/test/test_cltrans.py
Log:
(dialtone, sanxiyn)
Get rid of Literal in readlisp() and raise error instead. Support for
passing float to and fro Lisp. Test. Refactor unary operations.


Modified: pypy/dist/pypy/translator/cl/buildcl.py
==============================================================================
--- pypy/dist/pypy/translator/cl/buildcl.py	(original)
+++ pypy/dist/pypy/translator/cl/buildcl.py	Thu Apr 27 08:55:37 2006
@@ -35,12 +35,8 @@
             return "sbclinvoke.sh"
     return None
 
-class Literal:
-    def __init__(self, val):
-        self.val = val
-
 def readlisp(s):
-    # Return bool/int/char/str or give up
+    # Return bool/char/str/int/float or give up
     lines = s.splitlines()
     lines = [ line for line in lines if line and not line.startswith(';') ]
     assert len(lines) == 1
@@ -56,8 +52,11 @@
         return s[1:-1]
     elif s.isdigit():
         return int(s)
-    else:
-        return Literal(s)
+    try:
+        return float(s)
+    except ValueError:
+        pass
+    raise NotImplementedError("cannot read %s" % (s,))
 
 def make_cl_func(func, argtypes=[]):
     global global_cl

Modified: pypy/dist/pypy/translator/cl/clrepr.py
==============================================================================
--- pypy/dist/pypy/translator/cl/clrepr.py	(original)
+++ pypy/dist/pypy/translator/cl/clrepr.py	Thu Apr 27 08:55:37 2006
@@ -53,6 +53,8 @@
             return "nil"
     if isinstance(val, (int, long)):
         return str(val)
+    if isinstance(val, float):
+        return str(val)
     if val is None:
         return "nil"
     if isinstance(val, str):

Modified: pypy/dist/pypy/translator/cl/gencl.py
==============================================================================
--- pypy/dist/pypy/translator/cl/gencl.py	(original)
+++ pypy/dist/pypy/translator/cl/gencl.py	Thu Apr 27 08:55:37 2006
@@ -31,6 +31,17 @@
     op_ooupcast = nop
     op_oodowncast = nop
 
+    def make_unary_op(cl_op):
+        def _(self, result, arg):
+            yield "(setf %s (%s %s))" % (result, cl_op, arg)
+        return _
+
+    op_bool_not = make_unary_op("not")
+    op_cast_char_to_int = make_unary_op("char-code")
+    op_cast_int_to_char = make_unary_op("code-char")
+    op_cast_float_to_int = make_unary_op("truncate")
+    op_cast_int_to_float = make_unary_op("float")
+
     def make_binary_op(cl_op):
         def _(self, result, arg1, arg2):
             yield "(setf %s (%s %s %s))" % (result, cl_op, arg1, arg2)
@@ -52,18 +63,6 @@
     op_char_le = make_binary_op("char<=")
     op_char_ne = make_binary_op("char/=")
 
-    def op_cast_char_to_int(self, result, arg):
-        yield "(setf %s (char-code %s))" % (result, arg)
-
-    def op_cast_int_to_char(self, result, arg):
-        yield "(setf %s (code-char %s))" % (result, arg)
-
-    def op_cast_int_to_float(self, result, arg):
-        yield "(setf %s (float %s))" % (result, arg)
-
-    def op_bool_not(self, result, arg):
-        yield "(setf %s (not %s))" % (result, arg)
-
     def op_int_is_true(self, result, arg):
         yield "(setf %s (not (zerop %s)))" % (result, arg)
 

Modified: pypy/dist/pypy/translator/cl/test/test_clrepr.py
==============================================================================
--- pypy/dist/pypy/translator/cl/test/test_clrepr.py	(original)
+++ pypy/dist/pypy/translator/cl/test/test_clrepr.py	Thu Apr 27 08:55:37 2006
@@ -4,6 +4,7 @@
     assert repr_const(True) == 't'
     assert repr_const(False) == 'nil'
     assert repr_const(42) == '42'
+    assert repr_const(1.5) == '1.5'
     assert repr_const(None) == 'nil'
     assert repr_const('a') == '#\\a'
     assert repr_const('answer') == '"answer"'

Modified: pypy/dist/pypy/translator/cl/test/test_cltrans.py
==============================================================================
--- pypy/dist/pypy/translator/cl/test/test_cltrans.py	(original)
+++ pypy/dist/pypy/translator/cl/test/test_cltrans.py	Thu Apr 27 08:55:37 2006
@@ -4,7 +4,6 @@
 
 from pypy.annotation.model import SomeChar
 from pypy.translator.cl.buildcl import make_cl_func
-from pypy.translator.cl.buildcl import Literal
 
 from pypy.translator.test import snippet as t
 
@@ -26,6 +25,18 @@
     cl_ord_chr = make_cl_func(ord_chr, [SomeChar()])
     assert cl_ord_chr('a') == 'a'
 
+def test_float_int():
+    def cast_float(num):
+        return float(num)
+    cl_cast_float = make_cl_func(cast_float, [int])
+    assert cl_cast_float(1) == 1.0
+    def cast_int(num):
+        return int(num)
+    cl_cast_int = make_cl_func(cast_int, [float])
+    assert cl_cast_int(1.0) == 1
+    assert cl_cast_int(1.5) == 1
+    assert cl_cast_int(-1.5) == -1
+
 def test_range():
     def get_three():
         lst = range(7)



More information about the Pypy-commit mailing list