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

jlg at codespeak.net jlg at codespeak.net
Thu Jul 5 17:50:50 CEST 2007


Author: jlg
Date: Thu Jul  5 17:50:49 2007
New Revision: 44744

Modified:
   pypy/dist/pypy/lang/scheme/object.py
   pypy/dist/pypy/lang/scheme/test/test_eval.py
Log:
(quote ...); W_Identifier.to_symbol() method

Modified: pypy/dist/pypy/lang/scheme/object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/object.py	(original)
+++ pypy/dist/pypy/lang/scheme/object.py	Thu Jul  5 17:50:49 2007
@@ -26,6 +26,9 @@
     def __repr__(self):
         return "<W_Identifier " + self.name + ">"
 
+    def to_symbol(self):
+        return W_Symbol(self.name)
+
     def eval(self, ctx):
 
         if ctx is None:
@@ -314,6 +317,24 @@
         w_body = lst.cdr #.car
         return W_Lambda(w_args, w_body, ctx.copy())
 
+class Quote(W_Macro):
+    def symbolize(self, lst):
+
+        if isinstance(lst, W_Pair):
+            arg = lst
+            while not isinstance(arg, W_Nil):
+                arg.car = self.symbolize(arg.car)
+                arg = arg.cdr
+
+        if isinstance(lst, W_Identifier):
+            lst = lst.to_symbol()
+
+        return lst
+
+    def eval(self, ctx, lst):
+        w_obj = self.symbolize(lst.car)
+        return w_obj
+
 ##
 # Location()
 ##
@@ -344,6 +365,7 @@
         'set!': Sete,
         'if': MacroIf,
         'lambda': Lambda,
+        'quote': Quote,
     }
 
 OPERATION_MAP = {}

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	Thu Jul  5 17:50:49 2007
@@ -240,3 +240,31 @@
     assert w_result.cdr.car.to_number() == 2
     assert w_result.cdr.cdr.car.to_number() == 3
 
+def test_quote():
+    w_fnum = eval_noctx("(quote 42)")
+    assert isinstance(w_fnum, W_Fixnum)
+    assert w_fnum.to_number() == 42
+
+    w_sym = eval_noctx("(quote symbol)")
+    assert isinstance(w_sym, W_Symbol)
+    assert w_sym.to_string() == "symbol"
+
+    w_lst = eval_noctx("(quote (1 2 3))")
+    assert isinstance(w_lst, W_Pair)
+    assert w_lst.car.to_number() == 1
+    assert w_lst.cdr.car.to_number() == 2
+    assert w_lst.cdr.cdr.car.to_number() == 3
+
+    w_lst = eval_noctx("(quote (a (x y) c))")
+    assert isinstance(w_lst, W_Pair)
+    assert isinstance(w_lst.car, W_Symbol)
+    assert w_lst.car.to_string() == "a"
+    w_pair = w_lst.cdr.car
+    assert isinstance(w_lst.cdr.cdr.car, W_Symbol)
+    assert w_lst.cdr.cdr.car.to_string() == "c"
+
+    assert isinstance(w_pair.car, W_Symbol)
+    assert w_pair.car.to_string() == "x"
+    assert isinstance(w_pair.cdr.car, W_Symbol)
+    assert w_pair.cdr.car.to_string() == "y"
+



More information about the Pypy-commit mailing list