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

david at codespeak.net david at codespeak.net
Wed Jun 3 11:15:56 CEST 2009


Author: david
Date: Wed Jun  3 11:15:56 2009
New Revision: 65554

Added:
   pypy/branch/io-lang/pypy/lang/io/io/
   pypy/branch/io-lang/pypy/lang/io/io/Map.io
   pypy/branch/io-lang/pypy/lang/io/test/test_io_extensions.py
Modified:
   pypy/branch/io-lang/pypy/lang/io/parserhack.py
Log:
First attempt at loading io files during interpreter bootstrapping
imported some io-methods from the c-io interpreter 


Added: pypy/branch/io-lang/pypy/lang/io/io/Map.io
==============================================================================
--- (empty file)
+++ pypy/branch/io-lang/pypy/lang/io/io/Map.io	Wed Jun  3 11:15:56 2009
@@ -0,0 +1,27 @@
+Map do(
+    //doc Map with(key1, value1, key2, value2, ...) Returns a new map containing the given keys and values
+    with := method(
+    	m := Map clone
+    	args := call message arguments 
+    	for(i, 0, args size - 1, 2,
+    		m atPut(call evalArgAt(i), call evalArgAt(i+1))
+    	)
+    	m
+    )
+
+    asJson := method(
+    	"{" .. self keys map(k, k asJson .. ":" .. self at(k) asJson) join(",") .. "}"
+    )
+
+    //doc Map asList Converts a Map to a list of lists. Each element in the returned list will be a list of two elements: the key, and the value.
+    asList := method(
+    	self keys map(k, list(k, self at(k)))
+    )
+ 
+        //doc Map asObject Create a new Object whose slotDescriptionMap will be equal to self
+    	asObject := method(
+    		o := Object clone
+    		self foreach(k, v, o setSlot(k, getSlot("v")))
+            o
+    	)
+)

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	Wed Jun  3 11:15:56 2009
@@ -1,5 +1,7 @@
 import py
 import os
+import glob
+
 from pypy.lang.io.model import W_Number, parse_literal, W_Message
 from pypy.lang.io.objspace import ObjSpace
 
@@ -15,6 +17,17 @@
 
 def interpret(code):
     space = ObjSpace()
+    load_io_files(space)
     ast = parse(code, space)
     return ast.eval(space, space.w_lobby, space.w_lobby), space
-    
\ No newline at end of file
+    
+def parse_file(filename, space=None):
+    f = file(filename)
+    code = f.read()
+    f.close()
+    return parse(code, space)
+    
+def load_io_files(space):
+    files = glob.glob('io/*.io')
+    for f in files:
+        parse_file(f, space).eval(space, space.w_lobby, space.w_lobby)
\ No newline at end of file

Added: pypy/branch/io-lang/pypy/lang/io/test/test_io_extensions.py
==============================================================================
--- (empty file)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_io_extensions.py	Wed Jun  3 11:15:56 2009
@@ -0,0 +1,26 @@
+from pypy.lang.io.parserhack import interpret
+import py
+from pypy.lang.io.model import W_Object, W_List
+
+def test_map_asObject():
+    inp = 'Map clone atPut("1", 12345) atPut("2", 99) atPut("3", 3) atPut("4", 234) asObject'
+    res, space = interpret(inp)
+    assert res.slots['1'].value == 12345
+    assert res.slots['2'].value == 99
+    assert res.slots['3'].value == 3
+    assert res.slots['4'].value == 234
+    
+def test_map_asObject_clones_object_proto():
+    inp = 'Map clone atPut("1", 12345) atPut("2", 99) atPut("3", 3) atPut("4", 234) asObject'
+    res, space = interpret(inp)
+    assert isinstance(res, W_Object)
+    assert res.protos == [space.w_object]
+    
+def test_map_as_lsit():
+    py.test.skip('Depends on Map map wich itself depends on corroutines')
+    inp = 'Map clone atPut("1", 12345) atPut("2", 99) atPut("3", 3) atPut("4", 234) asList'
+    res, space = interpret(inp)
+    assert isinstance(res, W_List)
+    l = [(ll.items[0].value, ll.items[1].value) for ll in res.items]
+    assert l == [('1', 12345), ('2', 99), ('3', 3), ('4', 234)]
+    



More information about the Pypy-commit mailing list