[pypy-svn] r65458 - in pypy/branch/io-lang/pypy/lang/io: . test
david at codespeak.net
david at codespeak.net
Wed May 27 17:18:09 CEST 2009
Author: david
Date: Wed May 27 17:18:09 2009
New Revision: 65458
Modified:
pypy/branch/io-lang/pypy/lang/io/list.py
pypy/branch/io-lang/pypy/lang/io/test/test_list.py
Log:
(cfbolz, david) List foreach implementation
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 Wed May 27 17:18:09 2009
@@ -9,4 +9,31 @@
@register_method('List', 'at', unwrap_spec=[object, int])
def list_at(space, target, argument):
- return target[argument]
\ No newline at end of file
+ return target[argument]
+
+ at register_method('List', 'foreach')
+def list_foreach(space, w_target, w_message, w_context):
+ argcount = len(w_message.arguments)
+ assert argcount > 0
+
+ body = w_message.arguments[-1]
+ if argcount == 3:
+ key = w_message.arguments[0].name
+ value = w_message.arguments[1].name
+
+ for i in range(len(w_target.items)):
+ w_context.slots[key] = W_Number(space, i)
+ w_context.slots[value] = w_target.items[i]
+ t = body.eval(space, w_context, w_context)
+ elif argcount == 2:
+ value = w_message.arguments[0].name
+
+ for i in range(len(w_target.items)):
+ w_context.slots[value] = w_target.items[i]
+ t = body.eval(space, w_context, w_context)
+
+ elif argcount == 1:
+ for i in range(len(w_target.items)):
+ t = body.eval(space, w_context, w_context)
+
+ return t
\ 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 Wed May 27 17:18:09 2009
@@ -46,4 +46,29 @@
inp = 'a := list(1,2,3); a at("2")'
# Unspecified exception until error handling are introduced
assert py.test.raises(Exception, 'interpret(inp)')
-
\ No newline at end of file
+
+def test_list_foreach_key_value_returns_last():
+ inp = 'a := list(1, 2, 3); a foreach(key, value, key+value)'
+ res,space = interpret(inp)
+ assert res.value == 5
+
+def test_list_foreach_value_returns_last():
+ inp = 'c := 99; a := list(1, 2, 3); a foreach(value, c)'
+ res,space = interpret(inp)
+ assert res.value == 99
+
+def test_list_foreach_wo_args_returns_last():
+ inp = 'c := 99; a := list(1, 2, 3); a foreach(c)'
+ res,space = interpret(inp)
+ assert res.value == 99
+
+def test_list_key_value():
+ inp = 'b := list(); a := list(99, 34); a foreach(key, value, b append(list(key, value))); b'
+ res,space = interpret(inp)
+ value = [(x.items[0].value, x.items[1].value) for x in res.items]
+ assert value == [(0, 99), (1, 34)]
+
+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
More information about the Pypy-commit
mailing list