[pypy-svn] r26472 - pypy/dist/pypy/translator/cl

dialtone at codespeak.net dialtone at codespeak.net
Fri Apr 28 03:36:25 CEST 2006


Author: dialtone
Date: Fri Apr 28 03:36:17 2006
New Revision: 26472

Modified:
   pypy/dist/pypy/translator/cl/buildcl.py
   pypy/dist/pypy/translator/cl/clrepr.py
   pypy/dist/pypy/translator/cl/gencl.py
Log:
further cleanup of clrepr

Modified: pypy/dist/pypy/translator/cl/buildcl.py
==============================================================================
--- pypy/dist/pypy/translator/cl/buildcl.py	(original)
+++ pypy/dist/pypy/translator/cl/buildcl.py	Fri Apr 28 03:36:17 2006
@@ -4,7 +4,7 @@
 from pypy.tool.udir import udir
 from pypy.translator.translator import TranslationContext
 from pypy.translator.cl.gencl import GenCL
-from pypy.translator.cl.clrepr import clrepr, repr_fun_name
+from pypy.translator.cl.clrepr import clrepr
 from pypy import conftest
 from pypy.translator.cl import conftest as clconftest
 
@@ -88,7 +88,7 @@
     def _(*args):
         fpath.write(out)
         fp = file(str(fpath), "a")
-        print >>fp, "(write (", repr_fun_name(func.func_name),
+        print >>fp, "(write (", clrepr(func.func_name, symbol=True),
         for arg in args:
             print >>fp, clrepr(arg),
         print >>fp, "))"

Modified: pypy/dist/pypy/translator/cl/clrepr.py
==============================================================================
--- pypy/dist/pypy/translator/cl/clrepr.py	(original)
+++ pypy/dist/pypy/translator/cl/clrepr.py	Fri Apr 28 03:36:17 2006
@@ -4,91 +4,129 @@
 from pypy.rpython.ootypesystem.ootype import Signed, Unsigned, Float, Char
 from pypy.rpython.ootypesystem.ootype import Bool, Void, UniChar, Class
 from pypy.rpython.ootypesystem.ootype import StaticMethod, Meth, typeOf
-from pypy.rpython.ootypesystem.rclass import CLASSTYPE
 
-def clrepr(item):
-    if isinstance(item, str):
-        if len(item) == 1:
-            return "#\\%c" % (item,)
-        return '"%s"' % (item,)
-    if isinstance(item, bool):
-        if item: 
-            return "t"
-        else:
-            return "nil"
-    if isinstance(item, (int, long, float)):
-        return str(item)
+def clrepr(item, symbol=False):
+    """ This is the main repr function and is the only one that should be
+    used to represent python values in lisp.
+    """
     if item is None:
         return "nil"
-    if isinstance(item, (list, tuple)):
-        return "'(" + ' '.join(item) + ")"
-    if isinstance(item, Variable):
-        return repr_var(item)
-    if isinstance(item, Constant):
-        return repr_const(item)
-    if isinstance(item, Instance):
-        return "'" + repr_class_name(item._name)
+
+    fun = bltn_dispatch.get(type(item), None)
+    if fun is not None:
+        return fun(item, symbol)
+
     if typeOf(item) is Class:
         return "'" + item._INSTANCE._name
     return repr_unknown(item)
 
-def repr_unknown(obj):
-    name = obj.__class__.__name__
-    raise NotImplementedError("cannot represent %s" % (name,))
-
-def repr_var(var):
-    return var.name
-
-def repr_atom(atom):
-    return "'" + str(atom)
-
-def repr_class_name(name):
-    return name.replace('_', '-')
-
-def repr_fun_name(name):
-    return name.replace('_', '-')
-
 def repr_const(item):
     if isinstance(item.value, HalfConcreteWrapper):
         item = item.concretize()
 
-    if isinstance(item.concretetype, Atom):
-        return repr_atom(val)
+    fun = dispatch.get(type(item.concretetype), None)
+    if fun is not None:
+        return fun(item)
+
+    fun = dispatch.get(item.concretetype, None)
+    if fun is not None:
+        return fun(item)
 
-    if isinstance(item.concretetype, List):
-        val = map(repr_const, item.value)
-        return "#(%s)" % ' '.join(val)
+    if item.value is None:
+        return "nil"
 
-    if isinstance(item.concretetype, Record):
-        val = map(repr_const, item.value)
-        return "'(%s)" % ' '.join(val)
+    return repr_unknown(item)
 
-    if isinstance(item.concretetype, Instance):
-        return "'" + repr_class_name(item.value._name)
+def repr_bltn_str(item, symbol):
+    if symbol:
+        return item.replace('_', '-')
+    if len(item) == 1:
+        return "#\\%c" % (item,)
+    return '"%s"' % (item,)
+
+def repr_bltn_bool(item, _):
+    if item: 
+        return "t"
+    else:
+        return "nil"
 
-    if item.concretetype is Class:
-        return "'" + repr_class_name(item.value._INSTANCE._name)
+def repr_bltn_number(item, _):
+    return str(item)
 
-    if item.concretetype is Void:
-        return "nil"
+def repr_bltn_seq(item, _):
+    return "'(" + ' '.join(item) + ")"
 
-    if isinstance(item.concretetype, StaticMethod):
-        return repr_fun_name(item.value._name)
+def repr_Variable(item, _):
+    return clrepr(item.name, symbol=True)
 
-    if item.concretetype is Bool: # should precede int
-        if item.value:
-            return "t"
-        else:
-            return "nil"
-
-    if item.concretetype is Signed or item.concretetype is Unsigned:
-        #, long)): Not yet real longs
-        return str(item.value)
+def repr_Constant(item, _):
+    return repr_const(item)
+
+def repr_Instance(item, _):
+    return "'" + clrepr(item._name, symbol=True)
+
+bltn_dispatch = {
+    str: repr_bltn_str,
+    bool: repr_bltn_bool,
+    int: repr_bltn_number,
+    long: repr_bltn_number,
+    float: repr_bltn_number,
+    list: repr_bltn_seq,
+    tuple: repr_bltn_seq,
+    Variable: repr_Variable,
+    Constant: repr_Constant,
+    Instance: repr_Instance
+}
 
-    if item.concretetype is Float:
-        return str(item.value)
+def repr_atom(atom):
+    return "'" + clrepr(str(atom), symbol=True)
 
-    if item.value is None:
+def repr_class(item):
+    return clrepr(item.value._INSTANCE._name, symbol=True)
+
+def repr_void(item):
+    return "nil"
+
+def repr_bool(item):
+    if item.value:
+        return "t"
+    else:
         return "nil"
 
-    return repr_unknown(item)
+def repr_int(item):
+    return str(item.value)
+    
+def repr_float(item):
+    return str(item.value)
+
+def repr_list(item):
+    val = map(clrepr, item.value)
+    return "'(%s)" % ' '.join(val)
+
+def repr_record(item):
+    val = map(clrepr, item.value)
+    return "#(%s)" % ' '.join(val)
+
+def repr_instance(item):
+    return "'" + repr_class(item)
+
+def repr_static_method(item):
+    return clrepr(item.value._name, symbol=True)
+
+dispatch = {
+        Class: repr_class,
+        Void: repr_void,
+        Bool: repr_bool,
+        Signed: repr_int,
+        Unsigned: repr_int,
+        Float: repr_float,
+        Atom: repr_atom,
+        List: repr_list,
+        Record: repr_record,
+        Instance: repr_instance,
+        StaticMethod: repr_static_method
+}
+
+def repr_unknown(obj):
+    name = obj.__class__.__name__
+    raise NotImplementedError("cannot represent %s" % (name,))

Modified: pypy/dist/pypy/translator/cl/gencl.py
==============================================================================
--- pypy/dist/pypy/translator/cl/gencl.py	(original)
+++ pypy/dist/pypy/translator/cl/gencl.py	Fri Apr 28 03:36:17 2006
@@ -5,7 +5,7 @@
 from pypy.translator.translator import graphof
 from pypy.rpython.ootypesystem.ootype import dynamicType, oodowncast, List, Record, Instance, _class, _static_meth, _meth, ROOT
 from pypy.rpython.ootypesystem.rclass import OBJECT
-from pypy.translator.cl.clrepr import clrepr, repr_fun_name, repr_class_name
+from pypy.translator.cl.clrepr import clrepr
 
 class InsertionOrderedDict(dict):
     def __init__(self):
@@ -118,7 +118,7 @@
             methodobj = cls._methods[method]
             methodobj._method_name = method # XXX
             self.gen.pendinggraphs.append(methodobj)
-            name = repr_fun_name(method)
+            name = clrepr(method, symbol=True)
             selfvar = clrepr(receiver)
             args = map(self.gen.check_declaration, args)
             args = " ".join(args)
@@ -192,14 +192,14 @@
 
     def declare_class(self, cls):
         # cls is Instance
-        name = repr_class_name(cls._name)
+        name = clrepr(cls._name, symbol=True)
         field_declaration = ['('+field+')' for field in cls._fields]
         field_declaration = " ".join(field_declaration)
         if cls._superclass is ROOT:
             class_declaration = "(defclass %s () (%s))" % (name, field_declaration)
         else:
             self.declare_class(cls._superclass)
-            supername = repr_class_name(cls._superclass._name)
+            supername = clrepr(cls._superclass._name, symbol=True)
             class_declaration = "(defclass %s (%s) (%s))" % (name, supername, field_declaration)
         self.declarations[name] = class_declaration
 
@@ -258,7 +258,7 @@
                     yield line
 
     def emit_defun(self, fun):
-        yield "(defun " + repr_fun_name(fun.name)
+        yield "(defun " + clrepr(fun.name, symbol=True)
         arglist = fun.getargs()
         args = " ".join(map(clrepr, arglist))
         yield "(%s)" % (args,)
@@ -266,10 +266,10 @@
             yield line
 
     def emit_defmethod(self, fun, name):
-        yield "(defmethod %s" % (repr_fun_name(name))
+        yield "(defmethod %s" % (clrepr(name, symbol=True))
         arglist = fun.getargs()
         selfvar = clrepr(arglist[0])
-        clsname = repr_class_name(arglist[0].concretetype._name)
+        clsname = clrepr(arglist[0].concretetype._name, symbol=True)
         args = " ".join(map(clrepr, arglist[1:]))
         if args:
             yield "((%s %s) %s)" % (selfvar, clsname, args)



More information about the Pypy-commit mailing list