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

jlg at codespeak.net jlg at codespeak.net
Tue Jul 3 11:23:47 CEST 2007


Author: jlg
Date: Tue Jul  3 11:23:46 2007
New Revision: 44689

Modified:
   pypy/dist/pypy/lang/scheme/object.py
   pypy/dist/pypy/lang/scheme/test/test_eval.py
   pypy/dist/pypy/lang/scheme/test/test_object.py
   pypy/dist/pypy/lang/scheme/test/test_parser.py
Log:
procedure identifier evals to W_Procedure; W_procedure refactoring

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 11:23:46 2007
@@ -33,7 +33,7 @@
 
         w_obj = ctx.get(self.name)
         if w_obj is not None:
-            return w_obj.eval(ctx)
+            return w_obj #.eval(ctx)
         else:
             #reference to undefined identifier
             #unbound
@@ -117,7 +117,7 @@
 
     def eval(self, ctx):
         oper = self.car.eval(ctx)
-        return oper(ctx, self.cdr)
+        return oper.eval(ctx, self.cdr)
 
 class W_Nil(W_Root):
     def to_string(self):
@@ -129,7 +129,6 @@
 #it its very similar to operation.add
 #############################
 class W_Procedure(W_Root):
-
     def __init__(self, pname=""):
         self.pname = pname
 
@@ -137,19 +136,10 @@
         return "#<procedure:%s>" % (self.pname,)
 
     def eval(self, ctx, lst=None):
-        raise NotImplementedError
-
-def add_lst(ctx, lst):
-    def adder(x, y):
-        return x + y
-
-    return apply_lst(ctx, adder, lst)
-
-def mul_lst(ctx, lst):
-    def multiplier(x, y):
-        return x * y
+        return self.oper(ctx, lst)
 
-    return apply_lst(ctx, multiplier, lst)
+    def oper(self, ctx, lst):
+        raise NotImplementedError
 
 def apply_lst(ctx, fun, lst):
     acc = None
@@ -172,67 +162,59 @@
         return W_Float(acc)
 
 class Add(W_Procedure):
-    def eval(self, ctx):
-        return add_lst
+    def adder(self, x, y):
+        return x + y
+
+    def oper(self, ctx, lst):
+        return apply_lst(ctx, self.adder, lst)
 
 class Mul(W_Procedure):
-    def eval(self, ctx):
-        return mul_lst
+    def multiplier(self, x, y):
+        return x * y
 
-def define(ctx, lst):
-    w_identifier = lst.car
-    assert isinstance(w_identifier, W_Identifier)
-
-    w_val = lst.cdr.car.eval(ctx)
-    ctx.put(w_identifier.name, w_val)
-    return w_val
+    def oper(self, ctx, lst):
+        return apply_lst(ctx, self.multiplier, lst)
 
 class Define(W_Procedure):
-    def eval(self, ctx):
-        return define
-
-def macro_if(ctx, lst):
-    w_condition = lst.car
-    w_then = lst.cdr.car
-    if isinstance(lst.cdr.cdr, W_Nil):
-        w_else = W_Boolean(False)
-    else:
-        w_else = lst.cdr.cdr.car
-
-    w_cond_val = w_condition.eval(ctx)
-    if w_cond_val.to_boolean() is True:
-        return w_then.eval(ctx)
-    else:
-        return w_else.eval(ctx)
+    def oper(self, ctx, lst):
+        w_identifier = lst.car
+        assert isinstance(w_identifier, W_Identifier)
+
+        w_val = lst.cdr.car.eval(ctx)
+        ctx.put(w_identifier.name, w_val)
+        return w_val
 
 class MacroIf(W_Procedure):
-    def eval(self, ctx):
-        return macro_if
+    def oper(self, ctx, lst):
+        w_condition = lst.car
+        w_then = lst.cdr.car
+        if isinstance(lst.cdr.cdr, W_Nil):
+            w_else = W_Boolean(False)
+        else:
+            w_else = lst.cdr.cdr.car
 
-def cons(ctx, lst):
-    w_car = lst.car.eval(ctx)
-    w_cdr = lst.cdr.car.eval(ctx)
-    return W_Pair(w_car, w_cdr)
+        w_cond_val = w_condition.eval(ctx)
+        if w_cond_val.to_boolean() is True:
+            return w_then.eval(ctx)
+        else:
+            return w_else.eval(ctx)
 
 class Cons(W_Procedure):
-    def eval(self, ctx):
-        return cons
-
-def car(ctx, lst):
-    w_pair = lst.car.eval(ctx)
-    return w_pair.car
+    def oper(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 eval(self, ctx):
-        return car
-
-def cdr(ctx, lst):
-    w_pair = lst.car.eval(ctx)
-    return w_pair.cdr
+    def oper(self, ctx, lst):
+        w_pair = lst.car.eval(ctx)
+        return w_pair.car
 
 class Cdr(W_Procedure):
-    def eval(self, ctx):
-        return cdr
+    def oper(self, ctx, lst):
+        w_pair = lst.car.eval(ctx)
+        return w_pair.cdr
 
 ######################################
 # dict mapping operations to callables

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 11:23:46 2007
@@ -2,6 +2,7 @@
 from pypy.lang.scheme.ssparser import parse
 from pypy.lang.scheme.object import W_Boolean, W_Fixnum, W_Float, W_String
 from pypy.lang.scheme.object import W_Nil, W_Pair, W_Symbol, W_Identifier
+from pypy.lang.scheme.object import W_Procedure
 from pypy.lang.scheme.object import ExecutionContext
 from pypy.lang.scheme.operation import mul, add
 
@@ -85,6 +86,11 @@
     w_num = eval_expr(ctx, "(+ 1 v1 v2)")
     assert w_num.to_number() == 46.1
 
+def test_func():
+    ctx = ExecutionContext()
+    w_func = eval_expr(ctx, "+")
+    assert isinstance(w_func, W_Procedure)
+
 def test_if_simple():
     ctx = ExecutionContext()
     w_t = eval_expr(ctx, "(if #t #t #f)")

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	Tue Jul  3 11:23:46 2007
@@ -67,3 +67,26 @@
     assert w_fnum is ctx.get("v1")
     assert ctx.get("no_such_key") is None
 
+def test_location():
+    w_fnum = W_Fixnum(42)
+    loc = Location(w_fnum)
+    assert isinstance(loc, Location)
+    assert loc.obj is w_fnum
+
+def test_ctx_sets():
+    w_fnum = W_Fixnum(42)
+    w_fnum2 = W_Fixnum(43)
+    w_fnum3 = W_Fixnum(44)
+
+    ctx = ExecutionContext({})
+    ctx.put("v1", w_fnum)
+
+    ctx2 = ctx.copy()
+    assert w_fnum is ctx2.get("v1")
+    ctx.set("v1", w_fnum2)
+    assert w_fnum2 is ctx2.get("v1")
+    assert w_fnum2 is ctx.get("v1")
+
+    ctx2.put("v1", w_fnum3)
+    assert w_fnum3 is ctx2.get("v1")
+

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	Tue Jul  3 11:23:46 2007
@@ -54,6 +54,14 @@
     assert unwrap(w_float) == -123456.1234
 
 def test_sexpr():
+    w_list = parse('( 1 )')
+    assert isinstance(w_list, W_Pair)
+    assert isinstance(w_list.car, W_Fixnum)
+    assert isinstance(w_list.cdr, W_Nil)
+
+    #w_list = parse('()')
+    #assert isinstance(w_list, W_Nil)
+
     w_list = parse('(+ 1 2)')
     assert isinstance(w_list, W_Pair)
     assert isinstance(w_list.car, W_Identifier)



More information about the Pypy-commit mailing list