From 5d68fc44bafe164913ff10decf74c3cc68225fbc Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Fri, 5 Jan 2018 10:01:50 +0100 Subject: [PATCH] mes: Evaluate test of COND only once. * module/mes/base-0.mes (cond): Evaluate test of COND only once. Fixes cond tests with side effects (Nyacc). * tests/base.test ("cond side effect"): Test it. --- module/mes/base-0.mes | 4 ++-- tests/base.test | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/module/mes/base-0.mes b/module/mes/base-0.mes index 7db153e5..727522e6 100644 --- a/module/mes/base-0.mes +++ b/module/mes/base-0.mes @@ -63,8 +63,8 @@ (if (pair? (cdr (car clauses))) (if (eq? (car (cdr (car clauses))) '=>) (append2 (cdr (cdr (car clauses))) '(test)) - (list (cons 'lambda (cons '() (car clauses))))) - (list (cons 'lambda (cons '() (car clauses))))) + (list (cons 'lambda (cons '() (cons 'test (cdar clauses)))))) + (list (cons 'lambda (cons '() (cons 'test (cdar clauses)))))) (if (pair? (cdr clauses)) (cons 'cond (cdr clauses))))))) (car (car clauses))))) diff --git a/tests/base.test b/tests/base.test index 784fe9c4..3ac1e130 100755 --- a/tests/base.test +++ b/tests/base.test @@ -61,6 +61,10 @@ exit $? (pass-if "cond 2" (seq? (cond (#f)) *unspecified*)) (pass-if "cond 3" (seq? (cond (#t 0)) 0)) (pass-if "cond 3" (seq? (cond (#f 1) (#t 0)) 0)) + (pass-if-equal "cond side effect" + 1 + (let ((i 0)) + (cond ((set! i (1+ i)) i)))) (pass-if-equal "cond => " 0 (let ((lst '(0 1 2))) (define (next)