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

jlg at codespeak.net jlg at codespeak.net
Fri Jul 20 16:00:56 CEST 2007


Author: jlg
Date: Fri Jul 20 16:00:49 2007
New Revision: 45213

Modified:
   pypy/dist/pypy/lang/scheme/TODO.txt
   pypy/dist/pypy/lang/scheme/object.py
   pypy/dist/pypy/lang/scheme/ssparser.py
   pypy/dist/pypy/lang/scheme/test/test_eval.py
Log:
single global w_nil and w_undefined instances of respectively W_Nil and W_Undefined, use them instead of creating new instances

Modified: pypy/dist/pypy/lang/scheme/TODO.txt
==============================================================================
--- pypy/dist/pypy/lang/scheme/TODO.txt	(original)
+++ pypy/dist/pypy/lang/scheme/TODO.txt	Fri Jul 20 16:00:49 2007
@@ -8,7 +8,6 @@
 
 - implement key functions
   (apply, reduce, mapcar and so on)
-  # reduce will not make into python 3.0 ;)
 
 - comparison: < > eq? eqv?
 

Modified: pypy/dist/pypy/lang/scheme/object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/object.py	(original)
+++ pypy/dist/pypy/lang/scheme/object.py	Fri Jul 20 16:00:49 2007
@@ -47,7 +47,6 @@
 
         assert isinstance(w_expr, W_Root)
         return w_expr
-        #return self
 
     def eval_tr(self, ctx):
         return (self, None)
@@ -56,6 +55,8 @@
     def to_string(self):
         return "#<undefined>"
 
+w_undefined = W_Undefined()
+
 class W_Symbol(W_Root):
     #class dictionary for symbol storage
     obarray = {}
@@ -171,6 +172,12 @@
 class W_List(W_Root):
     pass
 
+class W_Nil(W_List):
+    def to_string(self):
+        return "()"
+
+w_nil = W_Nil()
+
 class W_Pair(W_List):
     def __init__(self, car, cdr):
         self.car = car
@@ -181,7 +188,7 @@
         cdr = self.cdr
         if isinstance(cdr, W_Pair): #proper list
             return "(" + car + " " + cdr.to_lstring() + ")"
-        elif isinstance(cdr, W_Nil): #one element proper list
+        elif cdr is w_nil: #one element proper list
             return "(" + car + ")"
 
         #dotted list/pair
@@ -192,7 +199,7 @@
         cdr = self.cdr
         if isinstance(cdr, W_Pair): #still proper list
             return car + " " + cdr.to_lstring()
-        elif isinstance(cdr, W_Nil): #end of proper list
+        elif cdr is w_nil: #end of proper list
             return car
 
         #end proper list with dotted
@@ -223,10 +230,6 @@
             raise SchemeSyntaxError
         return res
 
-class W_Nil(W_List):
-    def to_string(self):
-        return "()"
-
 class W_Callable(W_Root):
     def call_tr(self, ctx, lst):
         #usually tail-recursive call is normal call
@@ -241,7 +244,7 @@
         while True:
             if not isinstance(body_expression, W_Pair):
                 raise SchemeSyntaxError
-            elif isinstance(body_expression.cdr, W_Nil):
+            elif body_expression.cdr is w_nil:
                 return (body_expression.car, ctx)
             else:
                 body_expression.car.eval(ctx)
@@ -259,7 +262,7 @@
         #evaluate all arguments into list
         arg_lst = []
         arg = lst
-        while not isinstance(arg, W_Nil):
+        while not arg is w_nil:
             if not isinstance(arg, W_Pair):
                 raise SchemeSyntaxError
             w_obj = arg.car.eval(ctx)
@@ -292,7 +295,7 @@
     def __init__(self, args, body, closure, pname="#f"):
         self.args = []
         arg = args
-        while not isinstance(arg, W_Nil):
+        while not arg is w_nil:
             if isinstance(arg, W_Symbol):
                 self.args.append(Formal(arg.to_string(), True))
                 break
@@ -330,7 +333,7 @@
 
 def plst2lst(plst):
     """coverts python list() of W_Root into W_Pair scheme list"""
-    w_cdr = W_Nil()
+    w_cdr = w_nil
     plst.reverse()
     for w_obj in plst:
         w_cdr = W_Pair(w_obj, w_cdr)
@@ -492,7 +495,7 @@
             raise WrongArgType(w_pair, "Pair")
 
         w_pair.car = w_obj
-        return W_Undefined()
+        return w_undefined
 
 class SetCdr(W_Procedure):
     def procedure(self, ctx, lst):
@@ -502,7 +505,7 @@
             raise WrongArgType(w_pair, "Pair")
 
         w_pair.cdr = w_obj
-        return W_Undefined()
+        return w_undefined
 
 class Quit(W_Procedure):
     def procedure(self, ctx, lst):
@@ -624,7 +627,7 @@
         w_condition = lst.car
         lst_cdr = lst.get_cdr_as_pair()
         w_then = lst_cdr.car
-        if isinstance(lst_cdr.cdr, W_Nil):
+        if lst_cdr.cdr is w_nil:
             w_else = W_Boolean(False)
         else:
             w_else = lst_cdr.get_cdr_as_pair().car
@@ -707,16 +710,16 @@
         return self.eval_body(local_ctx, lst.cdr)
 
 def quote(sexpr):
-    return W_Pair(W_Symbol('quote'), W_Pair(sexpr, W_Nil()))
+    return W_Pair(W_Symbol('quote'), W_Pair(sexpr, w_nil))
 
 def qq(sexpr):
-    return W_Pair(W_Symbol('quasiquote'), W_Pair(sexpr, W_Nil()))
+    return W_Pair(W_Symbol('quasiquote'), W_Pair(sexpr, w_nil))
 
 def unquote(sexpr):
-    return W_Pair(W_Symbol('unquote'), W_Pair(sexpr, W_Nil()))
+    return W_Pair(W_Symbol('unquote'), W_Pair(sexpr, w_nil))
 
 def unquote_splicing(sexpr):
-    return W_Pair(W_Symbol('unquote-splicing'), W_Pair(sexpr, W_Nil()))
+    return W_Pair(W_Symbol('unquote-splicing'), W_Pair(sexpr, w_nil))
 
 class Quote(W_Macro):
     def call(self, ctx, lst):
@@ -752,14 +755,14 @@
                                 w_lst.get_cdr_as_pair().car,
                                 deep-1)
 
-                        return W_Pair(w_oper, W_Pair(w_unq, W_Nil()))
+                        return W_Pair(w_oper, W_Pair(w_unq, w_nil))
 
                 #increment nesting level
                 if w_oper.to_string() == "quasiquote":
                     w_unq = self.unquote(ctx,
                             w_lst.get_cdr_as_pair().car,
                             deep+1)
-                    return W_Pair(w_oper, W_Pair(w_unq, W_Nil()))
+                    return W_Pair(w_oper, W_Pair(w_unq, w_nil))
 
                 #not first level, look deeper, with lower nesting level
                 if deep > 1 and w_oper.to_string() == "unquote-splicing":
@@ -767,7 +770,7 @@
                             w_lst.get_cdr_as_pair().car,
                             deep-1)
 
-                    return W_Pair(w_oper, W_Pair(w_unq, W_Nil()))
+                    return W_Pair(w_oper, W_Pair(w_unq, w_nil))
 
             #for unquote-splice we need to check one level earlier
             #cond = if we have w_oper = (unquote-splice <sexpr>)
@@ -781,14 +784,14 @@
                 #unquote into list
                 w_unq = w_oper.get_cdr_as_pair().car.eval(ctx)
                 #w_unq must be proper list
-                if isinstance(w_unq, W_Nil):
+                if w_unq is w_nil:
                     #if nil: reeturn only rest of list
                     return w_unq_cdr
 
                 #traverse w_unq to find last cdr and set it to w_cdr
                 w_pair = w_unq
                 while isinstance(w_pair, W_Pair):
-                    if isinstance(w_pair.cdr, W_Nil):
+                    if w_pair.cdr is w_nil:
                         w_pair.cdr = w_unq_cdr
                         break
 

Modified: pypy/dist/pypy/lang/scheme/ssparser.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/ssparser.py	(original)
+++ pypy/dist/pypy/lang/scheme/ssparser.py	Fri Jul 20 16:00:49 2007
@@ -2,7 +2,7 @@
 from pypy.rlib.parsing.pypackrat import PackratParser
 from pypy.rlib.parsing.makepackrat import BacktrackException, Status
 from pypy.lang.scheme.object import W_Pair, W_Integer, W_String, symbol, \
-        W_Nil, W_Boolean, W_Real, quote, qq, unquote, unquote_splicing
+        w_nil, W_Boolean, W_Real, quote, qq, unquote, unquote_splicing
 
 def str_unquote(s):
     str_lst = []
@@ -106,7 +106,7 @@
       | car = sexpr
         cdr = pair
         return {W_Pair(car, cdr)}
-      | return {W_Nil()};
+      | return {w_nil};
     """
 
 def parse(code):

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	Fri Jul 20 16:00:49 2007
@@ -5,7 +5,7 @@
 
 def test_eval_obj():
     w_num = W_Pair(W_Symbol("+"),
-                   W_Pair(W_Integer(4), W_Pair(W_Integer(5), W_Nil())))
+                   W_Pair(W_Integer(4), W_Pair(W_Integer(5), w_nil)))
     assert w_num.eval(ExecutionContext()).to_number() == 9 
 
 def eval_expr(ctx, expr):
@@ -288,13 +288,13 @@
 def test_lambda_dotted_lstarg():
     ctx = ExecutionContext()
     w_result = eval_expr(ctx, """((lambda (x y . z) z) 3 4)""")
-    assert isinstance(w_result, W_Nil)
+    assert w_result is w_nil
 
     w_result = eval_expr(ctx, """((lambda (x y . z) z) 3 4 5 6)""")
     assert isinstance(w_result, W_Pair)
     assert w_result.car.to_number() == 5
     assert w_result.cdr.car.to_number() == 6
-    assert isinstance(w_result.cdr.cdr, W_Nil)
+    assert w_result.cdr.cdr is w_nil
 
 def test_define_lambda_sugar():
     ctx = ExecutionContext()
@@ -318,7 +318,7 @@
     w_result = eval_expr(ctx, "(f4 1 2)")
     assert isinstance(w_result, W_Pair)
     assert w_result.car.to_number() == 2
-    assert isinstance(w_result.cdr, W_Nil)
+    assert w_result.cdr is w_nil
 
 def test_quote():
     w_fnum = eval_noctx("(quote 42)")
@@ -385,7 +385,7 @@
     assert w_lst.cdr.car.to_number() == 42
     assert w_lst.cdr.cdr.car.to_number() == 3
     assert w_lst.cdr.cdr.cdr.car.to_string() == "a"
-    assert isinstance(w_lst.cdr.cdr.cdr.cdr, W_Nil)
+    assert w_lst.cdr.cdr.cdr.cdr is w_nil
 
 def test_begin():
     ctx = ExecutionContext()



More information about the Pypy-commit mailing list