[pypy-svn] r44466 - in pypy/dist/pypy/lang/scheme: . test

jlg at codespeak.net jlg at codespeak.net
Sat Jun 23 11:23:09 CEST 2007


Author: jlg
Date: Sat Jun 23 11:23:07 2007
New Revision: 44466

Modified:
   pypy/dist/pypy/lang/scheme/object.py
   pypy/dist/pypy/lang/scheme/test/test_object.py
   pypy/dist/pypy/lang/scheme/test/test_parser.py
Log:
added test_eval, simple operations works

Modified: pypy/dist/pypy/lang/scheme/object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/object.py	(original)
+++ pypy/dist/pypy/lang/scheme/object.py	Sat Jun 23 11:23:07 2007
@@ -43,25 +43,11 @@
         return "<W_Symbol " + self.name + ">"
 
     def eval(self, ctx):
-        if self.name == '+':
-            return add_lst
-
-        raise NotImplementedError
-
-#not sure though any operations should exist here
-#it its very similar to operation.add
-def add_lst(ctx, lst):
-    acc = 0
-    if not isinstance(lst, W_Pair):
-        #raise argument error
-        raise
-
-    arg = lst
-    while not isinstance(arg, W_Nil):
-        acc += arg.car.eval(ctx).to_number()
-        arg = arg.cdr
-
-    return W_Fixnum(acc)
+        # should be -> get form ctx dict the right method
+        try:
+            return OPERATION_MAP[self.name]
+        except KeyError:
+            raise NotImplementedError
 
 
 class W_Boolean(W_Root):
@@ -137,3 +123,40 @@
     def to_string(self):
         return "()"
 
+############################
+# operations
+#not sure though any operations should exist here
+#it its very similar to operation.add
+#############################
+
+def add_lst(ctx, lst):
+    return apply_lst(ctx, lambda x, y: x + y, lst)
+
+def mul_lst(ctx, lst):
+    return apply_lst(ctx, lambda x, y: x * y, lst)
+
+def apply_lst(ctx, fun, lst):
+    acc = None
+
+    if not isinstance(lst, W_Pair):
+        #raise argument error
+        raise
+
+    arg = lst
+    while not isinstance(arg, W_Nil):
+        if acc is None:
+            acc = arg.car.eval(ctx).to_number()
+        else:
+            acc = fun(acc, arg.car.eval(ctx).to_number())
+        arg = arg.cdr
+
+    if isinstance(acc, int):
+        return W_Fixnum(acc)
+    else:
+        return W_Float(acc)
+
+OPERATION_MAP = \
+    {
+        '+': add_lst,
+        '*': mul_lst,
+    }

Modified: pypy/dist/pypy/lang/scheme/test/test_object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/test/test_object.py	(original)
+++ pypy/dist/pypy/lang/scheme/test/test_object.py	Sat Jun 23 11:23:07 2007
@@ -39,29 +39,3 @@
     assert p.cdr.car == c2
     assert p.cdr.cdr.car == c3
     assert p.cdr.cdr.cdr == c4
-
-def test_eval_obj():
-    w_num = W_Pair(W_Symbol("+"),
-                   W_Pair(W_Fixnum(4), W_Pair(W_Fixnum(5), W_Nil())))
-    assert w_num.eval(None).to_number() == 9 
-
-def test_operations_simple():
-    w_num1 = W_Fixnum(4)
-    w_num2 = W_Fixnum(5)
-    w_num3 = W_Float(6.1)
-
-    w_num = mul(None, [w_num1])
-    assert w_num.to_number() == w_num1.to_number()
-    w_num = mul(None, [w_num1, w_num2])
-    assert w_num.to_number() == w_num1.to_number() * w_num2.to_number()
-    w_num = mul(None, [w_num1, w_num2, w_num3])
-    assert w_num.to_number() == (w_num1.to_number() * w_num2.to_number() * w_num3.to_number())
-
-    w_num = add(None, [w_num1])
-    assert w_num.to_number() == w_num1.to_number()
-    w_num = add(None, [w_num1, w_num2])
-    assert w_num.to_number() == w_num1.to_number() + w_num2.to_number()
-    w_num = add(None, [w_num1, w_num2, w_num3])
-    assert w_num.to_number() == (w_num1.to_number() + w_num2.to_number()
-            + w_num3.to_number())
-

Modified: pypy/dist/pypy/lang/scheme/test/test_parser.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/test/test_parser.py	(original)
+++ pypy/dist/pypy/lang/scheme/test/test_parser.py	Sat Jun 23 11:23:07 2007
@@ -19,7 +19,7 @@
             w_obj = w_obj.cdr
         return result
     raise NotImplementedError("don't know what to do with: %s" % (w_obj, ))
-    
+
 def test_simple():
     w_fixnum = parse(r'''1''')
     assert isinstance(w_fixnum, W_Fixnum)
@@ -55,7 +55,7 @@
     assert isinstance(t, W_Pair)
     assert unwrap(t) == ['define', ['fac', 'n'],
                             ['if', ['<', 'n', 2], 'n',
-                                   ['*', ['fac', ['-', 'n', 1]], 'n']]] 
+                                   ['*', ['fac', ['-', 'n', 1]], 'n']]]
 
 def test_ident_gen():
     ch_list = "+-*/azAZ<=>-_~!$%&:?^"



More information about the Pypy-commit mailing list