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

david at codespeak.net david at codespeak.net
Sun Jul 26 17:22:11 CEST 2009


Author: david
Date: Sun Jul 26 17:22:10 2009
New Revision: 66635

Modified:
   pypy/branch/io-lang/pypy/lang/io/model.py
   pypy/branch/io-lang/pypy/lang/io/object.py
   pypy/branch/io-lang/pypy/lang/io/objspace.py
   pypy/branch/io-lang/pypy/lang/io/test/test_object.py
Log:
Ojbect return method and missing control flow code

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	Sun Jul 26 17:22:10 2009
@@ -198,6 +198,8 @@
             w_method = w_receiver.lookup(self.name)
             assert w_method is not None, 'Method "%s" not found in "%s"' % (self.name, w_receiver.__class__)
             w_result = w_method.apply(space, w_receiver, self, w_context)
+            if not space.is_normal_status():
+                return space.w_return_value
         if self.next:
             #TODO: optimize
             return self.next.eval(space, w_result, w_context)

Modified: pypy/branch/io-lang/pypy/lang/io/object.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/object.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/object.py	Sun Jul 26 17:22:10 2009
@@ -89,6 +89,7 @@
       
    key = w_message.arguments[0].name
    
+   space.normal_status()
    for i in range(start, stop, step):
       w_context.slots[key] = W_Number(space, i)
       t = body.eval(space, w_context, w_context)
@@ -123,4 +124,14 @@
     
 @register_method('Object', 'continue')
 def object_continue(space, w_target, w_message, w_context):
-    space.continue_status()
\ No newline at end of file
+    space.continue_status()
+    return w_target
+    
+ at register_method('Object', 'return')
+def object_return(space, w_target, w_message, w_context):
+    w_value = space.w_nil
+    if len(w_message.arguments) > 0:
+        w_value = w_message.arguments[0].eval(space, w_context, w_context)
+    
+    space.return_status(w_value)
+    return w_target
\ 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	Sun Jul 26 17:22:10 2009
@@ -1,5 +1,5 @@
 from pypy.rlib.objectmodel import instantiate
-from pypy.lang.io.model import W_Number, W_Object, W_CFunction, W_Block, W_Message, W_List, W_Map
+from pypy.lang.io.model import W_Number, W_Object, W_CFunction, W_Block, W_Message, W_List, W_Map, W_ImmutableSequence
 from pypy.lang.io.register import cfunction_definitions
 
 import pypy.lang.io.number
@@ -23,9 +23,18 @@
         self.w_false = W_Object(self, [self.w_object])
         self.w_nil = W_Object(self, [self.w_object])
         self.w_list = W_List(self, [self.w_object])
-        self.w_call = W_Object(self)
+        self.w_call = W_Object(self, [self.w_object])
         self.w_map = W_Map(self, [self.w_object])
         
+        # flow control objects
+        self.w_normal = W_Object(self, [self.w_object])
+        self.w_break = W_Object(self, [self.w_object])
+        self.w_continue = W_Object(self, [self.w_object])
+        self.w_return = W_Object(self, [self.w_object])
+        self.w_eol = W_Object(self, [self.w_object])
+        # default stop state
+        self.stop_status = self.w_normal
+        self.w_return_value = self.w_nil
         self.init_w_object()        
         
         self.init_w_protos()
@@ -48,6 +57,8 @@
          
         self.init_w_map()
         
+        self.init_w_flow_objects()
+        
     def init_w_map(self):
         for key, function in cfunction_definitions['Map'].items():
             self.w_map.slots[key] = W_CFunction(self, function)
@@ -104,7 +115,46 @@
         self.w_lobby.slots['Protos'] = self.w_protos
 
     def init_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
+            self.w_object.slots[key] = W_CFunction(self, function)
+            
+    def init_w_flow_objects(self):
+        # Flow control: Normal
+        self.w_core.slots['Normal'] = self.w_normal
+        self.w_core.slots['Normal'].slots['type'] = W_ImmutableSequence(self, 'Normal')
+    	
+    	# Flow control: Break
+    	self.w_core.slots['Break'] = self.w_block
+        self.w_core.slots['Break'].slots['type'] = W_ImmutableSequence(self, 'Break')
+
+    	# Flow control: Continue
+        self.w_core.slots['Continue'] = self.w_continue
+        self.w_core.slots['Continue'].slots['type'] = W_ImmutableSequence(self, 'Continue')
+
+    	# Flow control: Return
+    	self.w_core.slots['Return'] = self.w_return
+        self.w_core.slots['Return'].slots['type'] = W_ImmutableSequence(self, 'Return')
+
+    	# Flow control: Eol
+    	self.w_core.slots['Eol'] = self.w_eol
+        self.w_core.slots['Eol'].slots['type'] = W_ImmutableSequence(self, 'Eol')
+        
+        
+    def break_status(self, result):
+        self.stop_status = self.w_break
+        self.w_return_value = result
+        
+        
+    def normal_status(self):
+        self.stop_status = self.w_normal
+        self.w_return_value = self.w_nil
+        
+    def is_normal_status(self):
+        return self.stop_status == self.w_normal
+        
+    def continue_status(self):
+        self.stop_status = self.w_continue
+    
+    def return_status(self, result):
+        self.stop_status = self.w_return
+        self.w_return_value = result
\ No newline at end of file

Modified: pypy/branch/io-lang/pypy/lang/io/test/test_object.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/test/test_object.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_object.py	Sun Jul 26 17:22:10 2009
@@ -141,4 +141,28 @@
     for(x, 1, 10, continue; a append(x))
     a"""
     res, space = interpret(inp)
-    assert len(res.items) == 0
\ No newline at end of file
+    assert len(res.items) == 0
+    
+def test_object_return():
+    inp = """x := method(y, return)
+    x(99)"""
+    res, space = interpret(inp)
+    assert res == space.w_nil
+
+def test_object_return2():    
+    inp = """x := method(y, return; 666)
+    x(99)"""
+    res, space = interpret(inp)
+    assert res.value == 666
+    
+def test_object_return_value():
+    inp = """x := method(y, return 42)
+    x(99)"""
+    res, space = interpret(inp)
+    assert res.value == 42
+
+def test_object_return_value2():    
+    inp = """x := method(y, return(1024); 666)
+    x(99)"""
+    res, space = interpret(inp)
+    assert res.value == 1024
\ No newline at end of file



More information about the Pypy-commit mailing list