[pypy-svn] r63389 - in pypy/branch/io-lang/pypy/lang/io: . test

david at codespeak.net david at codespeak.net
Fri Mar 27 15:38:56 CET 2009


Author: david
Date: Fri Mar 27 15:38:54 2009
New Revision: 63389

Added:
   pypy/branch/io-lang/pypy/lang/io/object.py
Modified:
   pypy/branch/io-lang/pypy/lang/io/model.py
   pypy/branch/io-lang/pypy/lang/io/objspace.py
   pypy/branch/io-lang/pypy/lang/io/parserhack.io
   pypy/branch/io-lang/pypy/lang/io/parserhack.py
   pypy/branch/io-lang/pypy/lang/io/test/test_interpreter.py
   pypy/branch/io-lang/pypy/lang/io/test/test_model.py
   pypy/branch/io-lang/pypy/lang/io/test/test_parse.py
Log:
(cfbolz, david) Added setSlot and basic object graph


Modified: pypy/branch/io-lang/pypy/lang/io/model.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/model.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/model.py	Fri Mar 27 15:38:54 2009
@@ -5,6 +5,13 @@
         self.protos = list(protos)
         self.space = space
     
+    def __eq__(self, other):
+        return (self.__class__ is other.__class__ and 
+                self.__dict__ == other.__dict__)
+
+    def __ne__(self, other):
+        return not self == other
+
     def lookup(self, name):
         try:
             return self.slots[name]
@@ -19,19 +26,14 @@
     """Number"""
     def __init__(self, space, value):
         self.value = value
-        W_Object.__init__(self, space, [space.w_number])
-    
-    def __eq__(self, other):
-        return (self.__class__ is other.__class__ and 
-                self.__dict__ == other.__dict__)
-
-    def __ne__(self, other):
-        return not self == other    
+        W_Object.__init__(self, space, [space.w_number]) 
 
 class W_List(W_Object):
     pass
-class W_Sequence(W_Object):
-    pass
+class W_ImmutableSequence(W_Object):
+    def __init__(self, space, string):
+        self.value = string
+    
 
 class W_CFunction(W_Object):
     def __init__(self, space, function):
@@ -52,12 +54,7 @@
     def __repr__(self):
         return "Message(%r, %r, %r)" % (self.name, self.arguments, self.next)
 
-    def __eq__(self, other):
-        return (self.__class__ is other.__class__ and 
-                self.__dict__ == other.__dict__)
-
-    def __ne__(self, other):
-        return not self == other
+    
     def eval(self, w_receiver):
         if self.literal_value is not None:
             w_result = self.literal_value
@@ -76,4 +73,7 @@
             return W_Number(space, t(literal))
         except ValueError:
             pass
+    if literal.startswith('"') and literal.endswith('"'):
+        return W_ImmutableSequence(space, literal[1:-1])
+        
     

Added: pypy/branch/io-lang/pypy/lang/io/object.py
==============================================================================
--- (empty file)
+++ pypy/branch/io-lang/pypy/lang/io/object.py	Fri Mar 27 15:38:54 2009
@@ -0,0 +1,12 @@
+from pypy.lang.io.register import register_method
+from pypy.lang.io.model import W_ImmutableSequence
+
+ at register_method('Object', 'setSlot')
+def w_object_set_slot(w_target, w_message, w_context):
+    w_name = w_message.arguments[0].eval(w_context)
+    w_value = w_message.arguments[1].eval(w_context)
+    assert isinstance(w_name, W_ImmutableSequence)
+    w_target.slots[w_name.value] = w_value
+
+# def w_object_get_slot(w_target, w_message, w_context):
+#     pass
\ No newline at end of file

Modified: pypy/branch/io-lang/pypy/lang/io/objspace.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/objspace.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/objspace.py	Fri Mar 27 15:38:54 2009
@@ -1,13 +1,25 @@
 from pypy.rlib.objectmodel import instantiate
 from pypy.lang.io.model import W_Number, W_Object, W_CFunction
-import pypy.lang.io.number
 from pypy.lang.io.register import cfunction_definitions
 
+import pypy.lang.io.number
+import pypy.lang.io.object
+
 class ObjSpace(object):
     """docstring for ObjSpace"""
     def __init__(self):
-        self.w_obj = W_Object(self)
+        self.init_w_object()
         self.w_lobby = W_Object(self)
+        self.w_protos = W_Object(self)
+        self.w_core = W_Object(self)
+        
+        self.w_core.protos.append(self.w_object)
+        
+        self.w_protos.protos.append(self.w_core)
+        self.w_protos.slots['Core'] = self.w_core
+        
+        self.init_w_lobby()
+        
         self.init_w_number()
         
     def init_w_number(self):
@@ -15,4 +27,14 @@
         W_Object.__init__(self.w_number, self)
         self.w_number.value = 0
         for key, function in cfunction_definitions['Number'].items():
-            self.w_number.slots[key] = W_CFunction(self, function)
\ No newline at end of file
+            self.w_number.slots[key] = W_CFunction(self, function)
+            
+    def init_w_lobby(self):
+        self.w_lobby.protos.append(self.w_protos)
+        self.w_lobby.slots['Lobby'] = self.w_lobby
+        self.w_lobby.slots['Protos'] = self.w_protos
+
+    def init_w_object(self):
+        self.w_object = W_Object(self)
+        for key, function in cfunction_definitions['Object'].items():
+            self.w_object.slots[key] = W_CFunction(self, function)
\ No newline at end of file

Modified: pypy/branch/io-lang/pypy/lang/io/parserhack.io
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/parserhack.io	(original)
+++ pypy/branch/io-lang/pypy/lang/io/parserhack.io	Fri Mar 27 15:38:54 2009
@@ -10,7 +10,7 @@
 )
 Message addArguments := method(
     "\"" print
-    name print
+    name asMutable escape print
     "\"" print
     ", [" print 
     arguments foreach(i, argument, argument pythonize; ", " print)

Modified: pypy/branch/io-lang/pypy/lang/io/parserhack.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/parserhack.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/parserhack.py	Fri Mar 27 15:38:54 2009
@@ -16,5 +16,5 @@
 def interpret(code):
     space = ObjSpace()
     ast = parse(code, space)
-    return ast.eval(None)
+    return ast.eval(space.w_lobby), space
     
\ No newline at end of file

Modified: pypy/branch/io-lang/pypy/lang/io/test/test_interpreter.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/test/test_interpreter.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_interpreter.py	Fri Mar 27 15:38:54 2009
@@ -1,13 +1,17 @@
 from pypy.lang.io.parserhack import interpret
-
+from pypy.lang.io.model import W_Number
 def test_even_simpler():
-    x = interpret("2")
+    x, _ = interpret("2")
     assert x.value == 2
 
 def test_simple():
-    x = interpret("2 + 2")
+    x, _ = interpret("2 + 2")
     assert x.value == 4
     
 def test_simple_minus():
-    x = interpret("2 - 2")
-    assert x.value == 0
\ No newline at end of file
+    x, _ = interpret("2 - 2")
+    assert x.value == 0
+    
+def test_set_slot():
+    x, space = interpret("a := 1")
+    assert space.w_lobby.slots['a'] == W_Number(space, 1)
\ No newline at end of file

Modified: pypy/branch/io-lang/pypy/lang/io/test/test_model.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/test/test_model.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_model.py	Fri Mar 27 15:38:54 2009
@@ -6,6 +6,7 @@
     assert parse_literal(space, "2").value == 2
     assert parse_literal(space, "0xFF").value == 255
     assert parse_literal(space, "2.3").value == 2.3
+    assert parse_literal(space, '"a"').value == 'a'
     
 def test_lookup():
     obj = W_Object(None, )

Modified: pypy/branch/io-lang/pypy/lang/io/test/test_parse.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/test/test_parse.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_parse.py	Fri Mar 27 15:38:54 2009
@@ -1,4 +1,4 @@
-from pypy.lang.io.model import W_Message
+from pypy.lang.io.model import W_Message, W_ImmutableSequence
 from pypy.lang.io.parserhack import parse
 from pypy.lang.io.objspace import ObjSpace
 
@@ -14,3 +14,11 @@
     ast = parse(input, space)
     assert ast == W_Message(space, "a", [], W_Message(space, '+', [W_Message(space, "b", [], W_Message(space, 'c', [],))]))
 
+def test_set_slot():
+    space = ObjSpace()
+    input = "a := b"
+    ast = parse(input, space)
+    a = W_Message(space, '"a"', [])
+    a.literal_value = W_ImmutableSequence(space, 'a')
+    
+    assert ast == W_Message(space, "setSlot", [a, W_Message(space, 'b', [])], )
\ No newline at end of file



More information about the Pypy-commit mailing list