[pypy-svn] r24385 - in pypy/dist/pypy/translator/squeak: . test

nik at codespeak.net nik at codespeak.net
Wed Mar 15 15:05:15 CET 2006


Author: nik
Date: Wed Mar 15 15:05:12 2006
New Revision: 24385

Added:
   pypy/dist/pypy/translator/squeak/message.py
   pypy/dist/pypy/translator/squeak/test/test_message.py
Modified:
   pypy/dist/pypy/translator/squeak/gensqueak.py
   pypy/dist/pypy/translator/squeak/test/runtest.py
   pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py
Log:
rename Selector class to Message. move it into its own module. this
might actually be refactored away at some later point, but need to
keep it around for further refactoring steps.


Modified: pypy/dist/pypy/translator/squeak/gensqueak.py
==============================================================================
--- pypy/dist/pypy/translator/squeak/gensqueak.py	(original)
+++ pypy/dist/pypy/translator/squeak/gensqueak.py	Wed Mar 15 15:05:12 2006
@@ -1,11 +1,8 @@
 import datetime, sys
-from pypy.objspace.flow.model import traverse
-from pypy.objspace.flow import FlowObjSpace
 from pypy.objspace.flow.model import Constant, Variable, Block
 from pypy.objspace.flow.model import last_exception, checkgraph
 from pypy.translator.gensupp import NameManager
-from pypy.translator.unsimplify import remove_direct_loops
-from pypy.translator.simplify import simplify_graph
+from pypy.translator.squeak.message import Message, camel_case
 from pypy.rpython.ootypesystem.ootype import Instance, ROOT
 from pypy.rpython.rarithmetic import r_int, r_uint
 from pypy import conftest
@@ -123,45 +120,6 @@
         return unique
 
 
-def camel_case(identifier):
-    identifier = identifier.replace(".", "_")
-    words = identifier.split('_')
-    return ''.join([words[0]] + [w.capitalize() for w in words[1:]])
-
-class Selector:
-
-    def __init__(self, function_name, arg_count):
-        self.parts = [camel_case(function_name)]
-        self.arg_count = arg_count
-        self.infix = False
-        if len(self.parts[0]) <= 2 and not self.parts[0].isalnum():
-            # Binary infix selector, e.g. "+"
-            assert arg_count == 1
-            self.infix = True
-        if arg_count > 1:
-            self.parts += ["with"] * (arg_count - 1)
-
-    def __str__(self):
-        if self.arg_count == 0 or self.infix:
-            return self.parts[0]
-        else:
-            return "%s:%s" % (self.parts[0],
-                    "".join([p + ":" for p in self.parts[1:]]))
-
-    def symbol(self):
-        return str(self)
-
-    def signature(self, arg_names):
-        assert len(arg_names) == self.arg_count
-        if self.arg_count == 0:
-            return self.parts[0]
-        elif self.infix:
-            return "%s %s" % (self.parts[0], arg_names[0])
-        else:
-            return " ".join(["%s: %s" % (p, a)
-                    for (p, a) in zip(self.parts, arg_names)])
-
-
 class CodeNode:
 
     def __hash__(self):
@@ -241,10 +199,10 @@
     selectormap = {
         #'setitem:with:': 'at:put:',
         #'getitem:':      'at:',
-        'new':           Selector('new', 0),
-        'runtimenew':    Selector('new', 0),
-        'classof':       Selector('class', 0),
-        'sameAs':        Selector('yourself', 0), 
+        'new':           'new',
+        'runtimenew':    'new',
+        'classof':       'class',
+        'sameAs':        'yourself', 
     }
 
     primitive_ops = {
@@ -267,13 +225,13 @@
 
     primitive_masks = {
         # XXX horrendous, but I can't figure out how to do this cleanly
-        "int": (Selector("maskInt", 1),
+        "int": (Message("maskInt"),
                 """maskInt: i 
                     ((i <= %s) & (i >= %s)) ifTrue: [^i].
                     ^ i + %s \\\\ %s - %s
                   """ % (sys.maxint, -sys.maxint-1,
                       sys.maxint+1, 2*(sys.maxint+1), sys.maxint+1)),
-        "uint": (Selector("maskUint", 1),
+        "uint": (Message("maskUint"),
                 """maskUint: i 
                     ^ i bitAnd: %s""" % r_uint.MASK),
     }
@@ -281,7 +239,7 @@
     def render_body(self, startblock):
         self.loops = LoopFinder(startblock).loops
         args = self.arguments(startblock)
-        sel = Selector(self.name, len(args))
+        sel = Message(self.name)
         yield sel.signature([self.expr(v) for v in args])
  
         # XXX should declare local variables here
@@ -319,7 +277,7 @@
     def oper_primitive(self, op, ptype, opname):
         receiver = self.expr(op.args[0])
         args = [self.expr(arg) for arg in op.args[1:]]
-        sel = Selector(self.primitive_ops[opname], len(args))
+        sel = Message(self.primitive_ops[opname])
         message = "%s %s" % (receiver, sel.signature(args))
         if opname in self.primitive_wrapping_ops \
                 and self.primitive_masks.has_key(ptype):
@@ -330,9 +288,10 @@
         return "%s := %s." % (self.expr(op.result), message)
 
     def assignment(self, op, receiver_name, sel_name, arg_names):
-        sel = Selector(sel_name, len(arg_names))
+        sel_name = camel_case(sel_name)
         if op.opname != "oosend":
-            sel = self.selectormap.get(sel.symbol(), sel)
+            sel_name = self.selectormap.get(sel_name, sel_name)
+        sel = Message(sel_name)
         return "%s := %s %s." % (self.expr(op.result),
                 receiver_name, sel.signature(arg_names))
 
@@ -555,7 +514,7 @@
         yield self.render_fileout_header(
                 self.gen.nameof_Instance(self.INSTANCE), "initializers")
         fields = self.INSTANCE._allfields()
-        sel = Selector("field_init", len(fields))
+        sel = Message("field_init")
         arg_names = ["a%s" % i for i in range(len(fields))]
         yield sel.signature(arg_names)
         for field_name, arg_name in zip(fields.keys(), arg_names):
@@ -581,7 +540,7 @@
 
     def render(self):
         yield self.render_fileout_header("PyConstants class", "internals")
-        sel = Selector("setupConstants", 0)
+        sel = Message("setupConstants")
         yield sel.signature([])
         yield "    Constants := Dictionary new."
         for const, const_id in self.constants.iteritems():
@@ -590,7 +549,7 @@
             field_names = INST._allfields().keys()
             field_values = [self.gen.nameof(getattr(const.value, f))
                     for f in field_names]
-            init_sel = Selector("field_init", len(field_values))
+            init_sel = Message("field_init")
             yield "    Constants at: '%s' put: (%s new %s)." \
                     % (const_id, class_name,
                         init_sel.signature(field_values))
@@ -598,7 +557,7 @@
         yield ""
 
         yield self.render_fileout_header("PyConstants class", "internals")
-        sel = Selector("getConstant", 1)
+        sel = Message("getConstant")
         yield sel.signature(["constId"])
         yield "    ^ Constants at: constId"
         yield "! !"

Added: pypy/dist/pypy/translator/squeak/message.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/squeak/message.py	Wed Mar 15 15:05:12 2006
@@ -0,0 +1,38 @@
+def camel_case(identifier):
+    identifier = identifier.replace(".", "_")
+    words = identifier.split('_')
+    return ''.join([words[0]] + [w.capitalize() for w in words[1:]])
+
+class Message:
+
+    def __init__(self, name):
+        self.name = camel_case(name) # XXX Should not use camel_case here
+        self.infix = False
+        if len(name) <= 2 and not name.isalnum():
+            # Binary infix selector, e.g. "+"
+            self.infix = True
+
+    def _selector_parts(self, arg_count):
+        parts = [self.name]
+        if arg_count > 1:
+            parts += ["with"] * (arg_count - 1)
+        return parts
+
+    def symbol(self, arg_count):
+        if arg_count == 0 or self.infix:
+            return self.name
+        else:
+            parts = self._selector_parts(arg_count)
+            return "%s:%s" % (parts[0], "".join([p + ":" for p in parts[1:]]))
+
+    def signature(self, arg_strings):
+        if len(arg_strings) == 0:
+            return self.name
+        elif self.infix:
+            assert len(arg_strings) == 1
+            return "%s %s" % (self.name, arg_strings[0])
+        else:
+            parts = self._selector_parts(len(arg_strings))
+            return " ".join(["%s: %s" % (p, a)
+                    for (p, a) in zip(parts, arg_strings)])
+

Modified: pypy/dist/pypy/translator/squeak/test/runtest.py
==============================================================================
--- pypy/dist/pypy/translator/squeak/test/runtest.py	(original)
+++ pypy/dist/pypy/translator/squeak/test/runtest.py	Wed Mar 15 15:05:12 2006
@@ -1,7 +1,8 @@
 import os
 import py
 from pypy.tool.udir import udir
-from pypy.translator.squeak.gensqueak import GenSqueak, Selector
+from pypy.translator.squeak.gensqueak import GenSqueak
+from pypy.translator.squeak.message import Message
 from pypy.translator.translator import TranslationContext
 from pypy import conftest
 
@@ -85,7 +86,7 @@
             options = ""
         cmd = 'squeak %s -- %s %s "%s" %s' \
                 % (options, startup_st, udir.join(self._gen.filename),
-                   Selector(self._func.__name__, len(args)).symbol(),
+                   Message(self._func.__name__).symbol(len(args)),
                    " ".join(['"%s"' % a for a in args]))
         squeak_process = os.popen(cmd)
         result = squeak_process.read()

Added: pypy/dist/pypy/translator/squeak/test/test_message.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/squeak/test/test_message.py	Wed Mar 15 15:05:12 2006
@@ -0,0 +1,16 @@
+from pypy.translator.squeak.message import Message
+
+class TestMessage:
+
+    def test_symbol(self):
+        assert Message("bla_bla").symbol(0) == "blaBla"
+        assert Message("bla").symbol(1) == "bla:"
+        assert Message("bla_bla_bla").symbol(3) == "blaBlaBla:with:with:"
+        assert Message("+").symbol(1) == "+"
+
+    def test_signature(self):
+        assert Message("bla").signature([]) == "bla"
+        assert Message("bla").signature(["v"]) == "bla: v"
+        assert Message("bla").signature(["v0", "v1"]) == "bla: v0 with: v1"
+        assert Message("+").signature(["v"]) == "+ v"
+

Modified: pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py
==============================================================================
--- pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py	(original)
+++ pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py	Wed Mar 15 15:05:12 2006
@@ -1,7 +1,6 @@
 import os
 import py
 from pypy.translator.test import snippet
-from pypy.translator.squeak.gensqueak import Selector, camel_case
 from pypy.translator.squeak.test.runtest import compile_function
 
 class TestGenSqueak:
@@ -118,18 +117,3 @@
         assert fn(1) == "1"
         assert fn(2) == "2"
 
-
-class TestSelector:
-
-    def test_selector(self):
-        assert Selector("bla_bla", 0).symbol() == "blaBla"
-        assert Selector("bla", 1).symbol() == "bla:"
-        assert Selector("bla_bla_bla", 3).symbol() == "blaBlaBla:with:with:"
-        assert Selector("+", 1).symbol() == "+"
-
-    def test_signature(self):
-        assert Selector("bla", 0).signature([]) == "bla"
-        assert Selector("bla", 1).signature(["v"]) == "bla: v"
-        assert Selector("bla", 2).signature(["v0", "v1"]) == "bla: v0 with: v1"
-        assert Selector("+", 1).signature(["v"]) == "+ v"
-



More information about the Pypy-commit mailing list