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

jlg at codespeak.net jlg at codespeak.net
Tue Jul 3 17:08:01 CEST 2007


Author: jlg
Date: Tue Jul  3 17:08:00 2007
New Revision: 44704

Modified:
   pypy/dist/pypy/lang/scheme/object.py
   pypy/dist/pypy/lang/scheme/test/test_eval.py
Log:
procedures refactoring; Sub procedure added

Modified: pypy/dist/pypy/lang/scheme/object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/object.py	(original)
+++ pypy/dist/pypy/lang/scheme/object.py	Tue Jul  3 17:08:00 2007
@@ -123,10 +123,37 @@
     def to_string(self):
         return "()"
 
-class W_Lambda(W_Root):
-    def __init__(self, args, body):
+class W_Procedure(W_Root):
+    def __init__(self, pname=""):
+        self.pname = pname
+
+    def to_string(self):
+        return "#<primitive-procedure %s>" % (self.pname,)
+
+    def eval(self, ctx, lst):
+        return self.procedure(ctx, lst)
+
+    def procedure(self, ctx, lst):
+        raise NotImplementedError
+
+class W_Macro(W_Root):
+    def __init__(self, pname=""):
+        self.pname = pname
+
+    def to_string(self):
+        return "#<primitive-macro %s>" % (self.pname,)
+
+    def eval(self, ctx, lst=None):
+        raise NotImplementedError
+
+class W_Lambda(W_Procedure):
+    def __init__(self, args, body, pname="#f"):
         self.args = args
         self.body = body
+        self.pname = pname
+
+    def to_string(self):
+        return "#<procedure %s>" % (self.pname,)
 
     def eval(self, ctx, lst):
         name = self.args
@@ -145,29 +172,6 @@
 ##
 # operations
 ##
-class W_Procedure(W_Root):
-    def __init__(self, pname=""):
-        self.pname = pname
-
-    def to_string(self):
-        return "#<procedure:%s>" % (self.pname,)
-
-    def eval(self, ctx, lst=None):
-        return self.oper(ctx, lst)
-
-    def oper(self, ctx, lst):
-        raise NotImplementedError
-
-class W_Macro(W_Root):
-    def __init__(self, pname=""):
-        self.pname = pname
-
-    def to_string(self):
-        return "#<macro:%s>" % (self.pname,)
-
-    def eval(self, ctx, lst=None):
-        raise NotImplementedError
-
 def apply_lst(ctx, fun, lst):
     acc = None
 
@@ -188,19 +192,27 @@
     else:
         return W_Float(acc)
 
-class Add(W_Procedure):
-    def adder(self, x, y):
+class ListOper(W_Procedure):
+    def procedure(self, ctx, lst):
+        return apply_lst(ctx, self.oper, lst)
+
+class Add(ListOper):
+    def oper(self, x, y):
         return x + y
 
-    def oper(self, ctx, lst):
-        return apply_lst(ctx, self.adder, lst)
+class Sub(ListOper):
+    def procedure(self, ctx, lst):
+        if isinstance(lst.cdr, W_Nil):
+            return apply_lst(ctx, self.oper, W_Pair( W_Fixnum(0), lst))
+        else:
+            return apply_lst(ctx, self.oper, lst)
 
-class Mul(W_Procedure):
-    def multiplier(self, x, y):
-        return x * y
+    def oper(self, x, y):
+        return x - y
 
-    def oper(self, ctx, lst):
-        return apply_lst(ctx, self.multiplier, lst)
+class Mul(ListOper):
+    def oper(self, x, y):
+        return x * y
 
 class Define(W_Macro):
     def eval(self, ctx, lst):
@@ -227,19 +239,19 @@
             return w_else.eval(ctx)
 
 class Cons(W_Procedure):
-    def oper(self, ctx, lst):
+    def procedure(self, ctx, lst):
         w_car = lst.car.eval(ctx)
         w_cdr = lst.cdr.car.eval(ctx)
         #cons is always creating a new pair
         return W_Pair(w_car, w_cdr)
 
 class Car(W_Procedure):
-    def oper(self, ctx, lst):
+    def procedure(self, ctx, lst):
         w_pair = lst.car.eval(ctx)
         return w_pair.car
 
 class Cdr(W_Procedure):
-    def oper(self, ctx, lst):
+    def procedure(self, ctx, lst):
         w_pair = lst.car.eval(ctx)
         return w_pair.cdr
 
@@ -265,6 +277,7 @@
 OMAP = \
     {
         '+': Add,
+        '-': Sub,
         '*': Mul,
         'define': Define,
         'if': MacroIf,

Modified: pypy/dist/pypy/lang/scheme/test/test_eval.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/test/test_eval.py	(original)
+++ pypy/dist/pypy/lang/scheme/test/test_eval.py	Tue Jul  3 17:08:00 2007
@@ -53,6 +53,13 @@
     w_num = eval_noctx("(* 4 -5 6.1)")
     assert w_num.to_number() == (4 * -5 * 6.1)
 
+    w_num = eval_noctx("(- 4)")
+    assert w_num.to_number() == -4
+    w_num = eval_noctx("(- 4 5)")
+    assert w_num.to_number() == -1
+    w_num = eval_noctx("(- 4 -5 6.1)")
+    assert w_num.to_number() == 4 - (-5) - 6.1
+
 def test_numerical_nested():
     w_num = eval_noctx("(+ 4 (* (+ 5) 6) (+ 1 2))")
     assert w_num.to_number() == 37



More information about the Pypy-commit mailing list