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

david at codespeak.net david at codespeak.net
Thu May 28 15:00:22 CEST 2009


Author: david
Date: Thu May 28 15:00:20 2009
New Revision: 65479

Modified:
   pypy/branch/io-lang/pypy/lang/io/list.py
   pypy/branch/io-lang/pypy/lang/io/test/test_list.py
Log:
Implemented some built-in operations on lists (with, indexOf, contains, size, first)


Modified: pypy/branch/io-lang/pypy/lang/io/list.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/list.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/list.py	Thu May 28 15:00:20 2009
@@ -36,4 +36,49 @@
         for i in range(len(w_target.items)):
             t = body.eval(space, w_context, w_context)
 
-    return t 
\ No newline at end of file
+    return t 
+    
+ at register_method('List', 'with')
+def list_with(space, w_target, w_message, w_context):
+    new_w_list = w_target.clone()
+    items_w = [x.eval(space, w_target, w_context) for x in w_message.arguments]
+    new_w_list.extend(items_w)
+    return new_w_list
+    
+# TODO: Not sure if this is rpython
+ at register_method('List', 'indexOf', unwrap_spec=[object, object])
+def list_index_of(space, w_target, item):
+    try:
+        return W_Number(space, w_target.items.index(item))
+    except ValueError, e:
+        return space.w_nil
+
+# TODO: Not sure if this is rpython
+ at register_method('List', 'contains', unwrap_spec=[object, object])
+def list_contains(space, w_target, item):
+    if item in w_target.items:
+        return space.w_true
+    return space.w_false
+    
+ at register_method('List', 'size')
+def list_size(space, w_target, w_message, w_context):
+    return W_Number(space, len(w_target.items))
+    
+ at register_method('List', 'first')
+def list_size(space, w_target, w_message, w_context):
+    if len(w_message.arguments) != 0:
+        t = w_message.arguments[0].eval(space, w_target, w_context)
+        assert isinstance(t, W_Number)
+        nfirst = t.value
+    else:
+        nfirst = 1
+    
+    if len(w_target.items) == 0 and nfirst == 1:
+        return space.w_nil
+
+    flist_w = w_target.clone()
+    if nfirst < 1:
+        flist_w.items = []
+    else:
+        flist_w.items = flist_w.items[0:nfirst]
+    return flist_w
\ No newline at end of file

Modified: pypy/branch/io-lang/pypy/lang/io/test/test_list.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/test/test_list.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_list.py	Thu May 28 15:00:20 2009
@@ -80,4 +80,63 @@
 def test_list_foreach_leaks_variables():
     inp = 'b := list(); a := list(99, 34); a foreach(key, value, b append(list(key, value))); key+value'
     res,space = interpret(inp)
-    assert res.value == 35
\ No newline at end of file
+    assert res.value == 35
+    
+def test_list_with():
+    inp = 'a := list(1,2,3); b := a with(99, 34); list(a,b)'
+    res, space = interpret(inp)
+    a, b = res.items
+    # a is proto of b
+    assert b.protos == [a]
+    
+    # b has 1,2,3,99,34 as element
+    assert [x.value for x in b.items] == [1, 2, 3, 99, 34]
+    
+def test_list_index_of():
+    inp = 'list(9,8,7,7) indexOf(7)'
+    res, _ = interpret(inp)
+    assert res.value == 2
+    
+    inp = 'list(9,8,7,7) indexOf(42)'
+    res, space = interpret(inp)
+    assert res == space.w_nil
+    
+def test_list_contains():
+    inp = 'list(9,8,7,7) contains(7)'
+    res, space = interpret(inp)
+    assert res == space.w_true
+    
+    inp = 'list(9,8,7,7) contains(42)'
+    res, space = interpret(inp)
+    assert res == space.w_false
+    
+def test_list_size():
+    inp = 'list(9,8,7,7) size'
+    res, _ = interpret(inp)
+    assert res.value == 4
+    
+    inp = 'list() size'
+    res, _ = interpret(inp)
+    assert res.value ==  0
+    
+def test_list_first_empty():
+    inp = 'list() first'
+    res, space = interpret(inp)
+    assert res == space.w_nil
+    
+    inp = 'list() first(3)'
+    res, space = interpret(inp)
+    assert isinstance(res, W_List)
+    assert len(res.items) == 0
+    
+def test_list_first():
+    inp = 'list(9,8,7,6,5,4,3,2,1,1) first'
+    res, _ = interpret(inp)
+    assert isinstance(res, W_List)
+    assert res.items[0].value == 9
+    
+def test_list_first_n():
+    inp = 'list(9,8,7,6,5,4,3,2,1,1) first(3)'
+    res, _ = interpret(inp)
+    assert isinstance(res, W_List)
+    assert [x.value for x in res.items] == [9,8,7]
\ No newline at end of file



More information about the Pypy-commit mailing list