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

jlg at codespeak.net jlg at codespeak.net
Thu Jul 5 18:32:05 CEST 2007


Author: jlg
Date: Thu Jul  5 18:32:05 2007
New Revision: 44747

Modified:
   pypy/dist/pypy/lang/scheme/object.py
   pypy/dist/pypy/lang/scheme/ssparser.py
   pypy/dist/pypy/lang/scheme/test/test_eval.py
Log:
quotation parsing

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 18:32:05 2007
@@ -16,6 +16,16 @@
     def eval(self, ctx):
         return self
 
+class W_Symbol(W_Root):
+    def __init__(self, val):
+        self.name = val
+
+    def to_string(self):
+        return self.name
+
+    def __repr__(self):
+        return "<W_symbol " + self.name + ">"
+
 class W_Identifier(W_Root):
     def __init__(self, val):
         self.name = val
@@ -40,17 +50,7 @@
         else:
             #reference to undefined identifier
             #unbound
-            raise "Unbound variable: %s" % (self.name, )
-
-class W_Symbol(W_Root):
-    def __init__(self, val):
-        self.name = val
-
-    def to_string(self):
-        return self.name
-
-    def __repr__(self):
-        return "<W_symbol " + self.name + ">"
+            raise Exception("Unbound variable: %s" % (self.name, ))
 
 class W_Boolean(W_Root):
     def __init__(self, val):
@@ -317,9 +317,11 @@
         w_body = lst.cdr #.car
         return W_Lambda(w_args, w_body, ctx.copy())
 
+def Literal(sexpr):
+    return W_Pair(W_Identifier('quote'), W_Pair(sexpr, W_Nil()))
+
 class Quote(W_Macro):
     def symbolize(self, lst):
-
         if isinstance(lst, W_Pair):
             arg = lst
             while not isinstance(arg, W_Nil):
@@ -418,7 +420,7 @@
             loc.obj = obj
             return obj
 
-        raise "Unbound"
+        raise Exception("Unbound variable: %s" % (name, ))
 
     def set(self, name, obj):
         """update existing location or create new location"""

Modified: pypy/dist/pypy/lang/scheme/ssparser.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/ssparser.py	(original)
+++ pypy/dist/pypy/lang/scheme/ssparser.py	Thu Jul  5 18:32:05 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_Fixnum, W_String, W_Identifier
-from pypy.lang.scheme.object import W_Nil, W_Boolean, W_Float
+from pypy.lang.scheme.object import W_Nil, W_Boolean, W_Float, Literal, W_Symbol
 
 def unquote(s):
     return s.replace('\\"', '"')
@@ -46,8 +46,14 @@
         EOF
         return {s};
     
+    literal:
+       `'`
+       s = sexpr
+       return {Literal(s)};
+    
     sexpr:
         list
+      | literal
       | FLOAT
       | FIXNUM
       | BOOLEAN

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 18:32:05 2007
@@ -79,7 +79,7 @@
     loc2 = ctx.get_location("x")
     assert ctx.get("x").to_number() == 43
     assert loc1 is loc2
-    py.test.raises("Unbound", eval_expr, ctx, "(set! y 42)")
+    py.test.raises(Exception, eval_expr, ctx, "(set! y 42)")
 
 def test_func():
     ctx = ExecutionContext()
@@ -268,3 +268,31 @@
     assert isinstance(w_pair.cdr.car, W_Symbol)
     assert w_pair.cdr.car.to_string() == "y"
 
+def test_quote_parse():
+    w_fnum = eval_noctx("'42")
+    assert isinstance(w_fnum, W_Fixnum)
+    assert w_fnum.to_number() == 42
+
+    w_sym = eval_noctx("'symbol")
+    assert isinstance(w_sym, W_Symbol)
+    assert w_sym.to_string() == "symbol"
+
+    w_lst = eval_noctx("'(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("'(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