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

jlg at codespeak.net jlg at codespeak.net
Thu Aug 2 11:46:03 CEST 2007


Author: jlg
Date: Thu Aug  2 11:46:01 2007
New Revision: 45450

Modified:
   pypy/dist/pypy/lang/scheme/object.py
   pypy/dist/pypy/lang/scheme/test/test_macro.py
Log:
ellipsis in macro template can refer to list

Modified: pypy/dist/pypy/lang/scheme/object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/object.py	(original)
+++ pypy/dist/pypy/lang/scheme/object.py	Thu Aug  2 11:46:01 2007
@@ -862,9 +862,8 @@
         return W_Transformer(syntax_lst, ctx)
 
 class Ellipsis(W_Root):
-    def __init__(self, expr, level):
+    def __init__(self, expr):
         self.expr = expr
-        self.level = level
 
 class EllipsisException(SchemeException):
     def __init__(self, ellipsis):
@@ -904,7 +903,7 @@
             if isinstance(w_pattcdr, W_Pair) and w_pattcdr.car is w_ellipsis:
                 #w_pattcar should be matched 0-inf times in ellipsis
                 print w_patt, w_expr
-                match_dict[w_pattcar.to_string()] = Ellipsis(w_expr, 1)
+                match_dict[w_pattcar.to_string()] = Ellipsis(w_expr)
                 return (True, match_dict)
 
             if isinstance(w_pattcar, W_Pair):
@@ -1024,10 +1023,18 @@
                         self.substitute(ctx, sexpr.cdr, match_dict))
             except EllipsisException, e:
                 scdr = sexpr.cdr
+                print ">", sexpr, e.expr
                 if isinstance(scdr, W_Pair) and scdr.car is w_ellipsis:
                     return e.expr
                 else:
-                    raise SchemeSyntaxError
+                    plst = []
+                    w_pair = e.expr
+                    while isinstance(w_pair, W_Pair):
+                        plst.append(W_Pair(w_pair.car, scdr))
+                        w_pair = w_pair.cdr
+
+                    ellipsis = Ellipsis(plst2lst(plst))
+                    raise EllipsisException(ellipsis)
 
             w_paircar = w_pair.car
             if isinstance(w_paircar, W_Symbol):

Modified: pypy/dist/pypy/lang/scheme/test/test_macro.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/test/test_macro.py	(original)
+++ pypy/dist/pypy/lang/scheme/test/test_macro.py	Thu Aug  2 11:46:01 2007
@@ -302,3 +302,40 @@
     assert eval_(ctx, "(or #f #f 82)").to_number() == 82
     assert eval_(ctx, "(or #f #f #f 162)").to_number() == 162
 
+def test_ellipsis_list_template():
+    ctx = ExecutionContext()
+    eval_(ctx, """(define-syntax letzero
+                                 (syntax-rules ()
+                                    ((_ (sym ...) body ...)
+                                     (let ((sym 0) ...) body ...))))""")
+
+    assert eval_(ctx, "(letzero (x) x)").to_number() == 0
+    assert eval_(ctx, "(letzero (x) (set! x 1) x)").to_number() == 1
+
+    assert eval_(ctx, "(letzero (x y z) (+ x y z))").to_number() == 0
+    assert eval_(ctx, """(letzero (x y z) (set! x 1)
+                                          (set! y 1)
+                                          (set! z 1)
+                                          (+ x y z))""").to_number() == 3
+
+def test_ellipsis_list_pattern():
+    py.test.skip("in progress")
+    ctx = ExecutionContext()
+    eval_(ctx, """(define-syntax rlet
+                                 (syntax-rules ()
+                                    ((_ ((val sym) ...) body ...)
+                                     (let ((sym val) ...) body ...))))""")
+
+    assert eval_(ctx, "(rlet ((0 x)) x)").to_number() == 0
+    assert eval_(ctx, "(rlet ((0 x)) (set! x 1) x)").to_number() == 1
+
+    assert eval_(ctx, """(rlet ((0 x) (0 y) (0 z))
+                               (+ x y z))""").to_number() == 0
+    assert eval_(ctx, """(rlet ((0 x) (0 y) (0 z))
+                               (set! x 1)
+                               (set! y 1)
+                               (set! z 1)
+                               (+ x y z))""").to_number() == 3
+
+    assert False
+



More information about the Pypy-commit mailing list