mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-05-20 03:30:27 +02:00
add srfi-39 parameters to boot-9
* module/ice-9/boot-9.scm (<parameter>, make-parameter, parameter?) (parameter-fluid, parameter-converter, parameterize): New top-level bindings, implementing SRFI-39 parameters. Currently, current-input-port and similar procedures are not yet parameters. * test-suite/Makefile.am: * test-suite/tests/parameters.test: Add tests, taken from srfi-39 tests.
This commit is contained in:
parent
6d346bb61a
commit
90de5c4c2e
3 changed files with 117 additions and 0 deletions
|
@ -2857,6 +2857,53 @@ module '(ice-9 q) '(make-q q-length))}."
|
|||
(define (unspecified? v) (eq? v *unspecified*))
|
||||
|
||||
|
||||
|
||||
|
||||
;;; {Parameters}
|
||||
;;;
|
||||
|
||||
(define <parameter>
|
||||
;; Three fields: the procedure itself, the fluid, and the converter.
|
||||
(make-struct <applicable-struct-vtable> 0 'pwprpr))
|
||||
(set-struct-vtable-name! <parameter> '<parameter>)
|
||||
|
||||
(define* (make-parameter init #:optional (conv (lambda (x) x)))
|
||||
(let ((fluid (make-fluid (conv init))))
|
||||
(make-struct <parameter> 0
|
||||
(case-lambda
|
||||
(() (fluid-ref fluid))
|
||||
((x) (fluid-set! fluid (conv x))))
|
||||
fluid conv)))
|
||||
|
||||
(define (parameter? x)
|
||||
(and (struct? x) (eq? (struct-vtable x) <parameter>)))
|
||||
|
||||
(define (parameter-fluid p)
|
||||
(if (parameter? p)
|
||||
(struct-ref p 1)
|
||||
(scm-error 'wrong-type-arg "parameter-fluid"
|
||||
"Not a parameter: ~S" (list p) #f)))
|
||||
|
||||
(define (parameter-converter p)
|
||||
(if (parameter? p)
|
||||
(struct-ref p 2)
|
||||
(scm-error 'wrong-type-arg "parameter-fluid"
|
||||
"Not a parameter: ~S" (list p) #f)))
|
||||
|
||||
(define-syntax parameterize
|
||||
(lambda (x)
|
||||
(syntax-case x ()
|
||||
((_ ((param value) ...) body body* ...)
|
||||
(with-syntax (((p ...) (generate-temporaries #'(param ...))))
|
||||
#'(let ((p param) ...)
|
||||
(if (not (parameter? p))
|
||||
(scm-error 'wrong-type-arg "parameterize"
|
||||
"Not a parameter: ~S" (list p) #f))
|
||||
...
|
||||
(with-fluids (((struct-ref p 1) ((struct-ref p 2) value))
|
||||
...)
|
||||
body body* ...)))))))
|
||||
|
||||
|
||||
|
||||
;;; {Running Repls}
|
||||
|
|
|
@ -74,6 +74,7 @@ SCM_TESTS = tests/00-initial-env.test \
|
|||
tests/numbers.test \
|
||||
tests/optargs.test \
|
||||
tests/options.test \
|
||||
tests/parameters.test \
|
||||
tests/print.test \
|
||||
tests/procprop.test \
|
||||
tests/procs.test \
|
||||
|
|
69
test-suite/tests/parameters.test
Normal file
69
test-suite/tests/parameters.test
Normal file
|
@ -0,0 +1,69 @@
|
|||
;;;; srfi-39.test --- -*- scheme -*-
|
||||
;;;;
|
||||
;;;; Copyright (C) 2004, 2005, 2006, 2008, 2011 Free Software Foundation, Inc.
|
||||
;;;;
|
||||
;;;; This library is free software; you can redistribute it and/or
|
||||
;;;; modify it under the terms of the GNU Lesser General Public
|
||||
;;;; License as published by the Free Software Foundation; either
|
||||
;;;; version 3 of the License, or (at your option) any later version.
|
||||
;;;;
|
||||
;;;; This library is distributed in the hope that it will be useful,
|
||||
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
;;;; Lesser General Public License for more details.
|
||||
;;;;
|
||||
;;;; You should have received a copy of the GNU Lesser General Public
|
||||
;;;; License along with this library; if not, write to the Free Software
|
||||
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
;; Testing the parameters implementation in boot-9.
|
||||
;;
|
||||
(define-module (test-parameters)
|
||||
#:use-module (srfi srfi-34)
|
||||
#:use-module (test-suite lib))
|
||||
|
||||
(define a (make-parameter 3))
|
||||
(define b (make-parameter 4))
|
||||
|
||||
(define (check a b a-val b-val)
|
||||
(and (eqv? (a) a-val)) (eqv? (b) b-val))
|
||||
|
||||
(define c (make-parameter 2 (lambda (x) (if (< x 10) x 10))))
|
||||
(define d (make-parameter 15 (lambda (x) (if (< x 10) x 10))))
|
||||
|
||||
(with-test-prefix "parameters"
|
||||
|
||||
(pass-if "test 1"
|
||||
(check a b 3 4))
|
||||
|
||||
(pass-if "test 2"
|
||||
(parameterize ((a 2) (b 1))
|
||||
(and (check a b 2 1)
|
||||
(parameterize ((b 8))
|
||||
(check a b 2 8)))))
|
||||
|
||||
(pass-if "test 3"
|
||||
(check a b 3 4))
|
||||
|
||||
(pass-if "test 4"
|
||||
(check c d 2 10))
|
||||
|
||||
(pass-if "test 5"
|
||||
(parameterize ((a 0) (b 1) (c 98) (d 9))
|
||||
(and (check a b 0 1)
|
||||
(check c d 10 9)
|
||||
(parameterize ((c (a)) (d (b)))
|
||||
(and (check a b 0 1)
|
||||
(check c d 0 1))))))
|
||||
|
||||
(pass-if "SRFI-34"
|
||||
(let ((inside? (make-parameter #f)))
|
||||
(call/cc (lambda (return)
|
||||
(with-exception-handler
|
||||
(lambda (c)
|
||||
;; This handler should be called in the dynamic
|
||||
;; environment installed by `parameterize'.
|
||||
(return (inside?)))
|
||||
(lambda ()
|
||||
(parameterize ((inside? #t))
|
||||
(raise 'some-exception)))))))))
|
Loading…
Add table
Add a link
Reference in a new issue