On Sun, Nov 12, 2017 at 07:14:45PM +0100, lemonboy wrote:
> Hello hackers,
> here's a patch to make and-let* behave as specified in the SRFI-2, I've also
> included the "official" test suite and all the checks pass now.

Looks good, signed-off patch attached.

I think it's probably best to apply it only to chicken-5 unless anyone
is strongly in favour of applying it to master as well.

> One minor nit: with regards to the variables bound in this construct, the 
> SRFI-2
> states the following
> 
> > As usual, all VARIABLEs must be unique (like in let*)
> 
> I wasn't aware of such a restriction in let* nor I have found any reference to
> that in the R5RS, a single test has been modified to take into account this
> difference.

hm, I guess it depends on how you read it.  The description of "let" says:

  It is an error for a <variable> to appear more than once in the list of
  variables being bound.

The description of let* just says

  Let* is similar to let, but the bindings are performed sequentially
  from left to right, and the region of a binding indicated by
  (<variable> <init>) is that part of the let* expression to the right
  of the binding.

So depending on how you read it, the restriction in "let" that the
variables may appear only once might also apply to "let*".  But that's
a silly restriction, and I haven't been able to find a Scheme
implementation that uses this restricted interpretation.  So I think
we're fine by dropping that particular restriction of and-let*.

Besides, "it is an error" just means "you can do whatever you want",
anyway ;)

Cheers,
Peter
From 9a01271e7986bba46c8c373d0217ffac202c23ba Mon Sep 17 00:00:00 2001
From: LemonBoy <[email protected]>
Date: Sun, 12 Nov 2017 19:04:21 +0100
Subject: [PATCH] Make and-let* behave as specified in SRFI-2

When the body is missing the construction should return the last
expression that's been evaluated or the last variable that's been bound.
If there are no bindings and no body then #t is returned, like (and)
does.
Make sure the non-braced expressions refer to a variable and throw a
syntax error otherwise.

Signed-off-by: Peter Bex <[email protected]>
---
 chicken-syntax.scm     | 17 ++++++++++++-----
 tests/syntax-tests.scm | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 5 deletions(-)

diff --git a/chicken-syntax.scm b/chicken-syntax.scm
index 48a84726..d423371a 100644
--- a/chicken-syntax.scm
+++ b/chicken-syntax.scm
@@ -688,18 +688,25 @@
     (##sys#check-syntax 'and-let* form '(_ #(_ 0) . _))
     (let ((bindings (cadr form))
 	  (body (cddr form)))
-      (let fold ([bs bindings])
+      (let fold ([bs bindings] [last #t])
 	(if (null? bs)
-	    `(##core#begin ,@body)
+	    `(##core#begin ,last . ,body)
 	    (let ([b (car bs)]
 		  [bs2 (cdr bs)] )
-	      (cond [(not (pair? b)) `(##core#if ,b ,(fold bs2) #f)]
-		    [(null? (cdr b)) `(##core#if ,(car b) ,(fold bs2) #f)]
+	      (cond [(not (pair? b))
+                     (##sys#check-syntax 'and-let* b 'symbol)
+                     (let ((var (r (gensym))))
+                       `(##core#let ((,var ,b))
+                          (##core#if ,var ,(fold bs2 var) #f)))]
+		    [(null? (cdr b))
+                     (let ((var (r (gensym))))
+                       `(##core#let ((,var ,(car b)))
+                          (##core#if ,var ,(fold bs2 var) #f)))]
 		    [else
 		     (##sys#check-syntax 'and-let* b '(symbol _))
 		     (let ((var (car b)))
 		       `(##core#let ((,var ,(cadr b)))
-			 (##core#if ,var ,(fold bs2) #f) ) ) ] ) ) ) ) ) ) ) )
+			 (##core#if ,var ,(fold bs2 var) #f)))]))))))))
 
 
 
diff --git a/tests/syntax-tests.scm b/tests/syntax-tests.scm
index de754e95..9e750b17 100644
--- a/tests/syntax-tests.scm
+++ b/tests/syntax-tests.scm
@@ -914,6 +914,42 @@
 (t 'foo (and-let* (((= 4 4)) (a 'foo)) a))
 (t #f (and-let* ((a #f) ((error "not reached 1"))) (error "not reached 2")))
 
+(t  (and-let* () 1) 1)
+(t  (and-let* () 1 2) 2)
+(t  (and-let* () ) #t)
+
+(t (let ((x #f)) (and-let* (x))) #f)
+(t (let ((x 1)) (and-let* (x))) 1)
+(t (and-let* ((x #f)) ) #f)
+(t (and-let* ((x 1)) ) 1)
+(f (eval '(and-let* ( #f (x 1))) ))
+(t (and-let* ( (#f) (x 1)) ) #f)
+(f (eval '(and-let* (2 (x 1))) ))
+(t (and-let* ( (2) (x 1)) ) 1)
+(t (and-let* ( (x 1) (2)) ) 2)
+(t (let ((x #f)) (and-let* (x) x)) #f)
+(t (let ((x "")) (and-let* (x) x)) "")
+(t (let ((x "")) (and-let* (x)  )) "")
+(t (let ((x 1)) (and-let* (x) (+ x 1))) 2)
+(t (let ((x #f)) (and-let* (x) (+ x 1))) #f)
+(t (let ((x 1)) (and-let* (((positive? x))) (+ x 1))) 2)
+(t (let ((x 1)) (and-let* (((positive? x))) )) #t)
+(t (let ((x 0)) (and-let* (((positive? x))) (+ x 1))) #f)
+(t (let ((x 1)) (and-let* (((positive? x)) (x (+ x 1))) (+ x 1)))  3)
+; The uniqueness of the bindings isn't enforced
+(t (let ((x 1)) (and-let* (((positive? x)) (x (+ x 1)) (x (+ x 1))) (+ x 1))) 4)
+
+(t (let ((x 1)) (and-let* (x ((positive? x))) (+ x 1))) 2)
+(t (let ((x 1)) (and-let* ( ((begin x)) ((positive? x))) (+ x 1))) 2)
+(t (let ((x 0)) (and-let* (x ((positive? x))) (+ x 1))) #f)
+(t (let ((x #f)) (and-let* (x ((positive? x))) (+ x 1))) #f)
+(t (let ((x #f)) (and-let* ( ((begin x)) ((positive? x))) (+ x 1))) #f)
+
+(t  (let ((x 1)) (and-let* (x (y (- x 1)) ((positive? y))) (/ x y))) #f)
+(t  (let ((x 0)) (and-let* (x (y (- x 1)) ((positive? y))) (/ x y))) #f)
+(t  (let ((x #f)) (and-let* (x (y (- x 1)) ((positive? y))) (/ x y))) #f)
+(t  (let ((x 3)) (and-let* (x (y (- x 1)) ((positive? y))) (/ x y))) 3/2)
+
 ;;; SRFI-26
 
 ;; Cut
-- 
2.11.0

Attachment: signature.asc
Description: PGP signature

_______________________________________________
Chicken-hackers mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/chicken-hackers

Reply via email to