srfi-39.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ;;; srfi-39.scm --- Parameter objects
  2. ;; Copyright (C) 2004, 2005, 2006, 2008 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 2.1 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
  18. ;;; Date: 2004-05-05
  19. ;;; Commentary:
  20. ;; This is an implementation of SRFI-39 (Parameter objects).
  21. ;;
  22. ;; The implementation is based on Guile's fluid objects, and is, therefore,
  23. ;; thread-safe (parameters are thread-local).
  24. ;;
  25. ;; In addition to the forms defined in SRFI-39 (`make-parameter',
  26. ;; `parameterize'), a new procedure `with-parameters*' is provided.
  27. ;; This procedures is analogous to `with-fluids*' but taking as first
  28. ;; argument a list of parameter objects instead of a list of fluids.
  29. ;;
  30. ;;; Code:
  31. (define-module (srfi srfi-39)
  32. #:use-module (ice-9 syncase)
  33. #:use-module (srfi srfi-16)
  34. #:export (make-parameter)
  35. #:export-syntax (parameterize)
  36. ;; helper procedure not in srfi-39.
  37. #:export (with-parameters*)
  38. #:replace (current-input-port current-output-port current-error-port))
  39. ;; Make 'srfi-39 available as a feature identifiere to `cond-expand'.
  40. ;;
  41. (cond-expand-provide (current-module) '(srfi-39))
  42. (define make-parameter
  43. (case-lambda
  44. ((val) (make-parameter/helper val (lambda (x) x)))
  45. ((val conv) (make-parameter/helper val conv))))
  46. (define get-fluid-tag (lambda () 'get-fluid)) ;; arbitrary unique (as per eq?) value
  47. (define get-conv-tag (lambda () 'get-conv)) ;; arbitrary unique (as per eq?) value
  48. (define (make-parameter/helper val conv)
  49. (let ((value (make-fluid))
  50. (conv conv))
  51. (begin
  52. (fluid-set! value (conv val))
  53. (lambda new-value
  54. (cond
  55. ((null? new-value) (fluid-ref value))
  56. ((eq? (car new-value) get-fluid-tag) value)
  57. ((eq? (car new-value) get-conv-tag) conv)
  58. ((null? (cdr new-value)) (fluid-set! value (conv (car new-value))))
  59. (else (error "make-parameter expects 0 or 1 arguments" new-value)))))))
  60. (define-syntax parameterize
  61. (syntax-rules ()
  62. ((_ ((?param ?value) ...) ?body ...)
  63. (with-parameters* (list ?param ...)
  64. (list ?value ...)
  65. (lambda () ?body ...)))))
  66. (define (current-input-port . new-value)
  67. (if (null? new-value)
  68. ((@ (guile) current-input-port))
  69. (apply set-current-input-port new-value)))
  70. (define (current-output-port . new-value)
  71. (if (null? new-value)
  72. ((@ (guile) current-output-port))
  73. (apply set-current-output-port new-value)))
  74. (define (current-error-port . new-value)
  75. (if (null? new-value)
  76. ((@ (guile) current-error-port))
  77. (apply set-current-error-port new-value)))
  78. (define port-list
  79. (list current-input-port current-output-port current-error-port))
  80. ;; There are no fluids behind current-input-port etc, so those parameter
  81. ;; objects are picked out of the list and handled separately with a
  82. ;; dynamic-wind to swap their values to and from a location (the "value"
  83. ;; variable in the swapper procedure "let").
  84. ;;
  85. ;; current-input-port etc are already per-dynamic-root, so this arrangement
  86. ;; works the same as a fluid. Perhaps they could become fluids for ease of
  87. ;; implementation here.
  88. ;;
  89. ;; Notice the use of a param local variable for the swapper procedure. It
  90. ;; ensures any application changes to the PARAMS list won't affect the
  91. ;; winding.
  92. ;;
  93. (define (with-parameters* params values thunk)
  94. (let more ((params params)
  95. (values values)
  96. (fluids '()) ;; fluids from each of PARAMS
  97. (convs '()) ;; VALUES with conversion proc applied
  98. (swapper noop)) ;; wind/unwind procedure for ports handling
  99. (if (null? params)
  100. (if (eq? noop swapper)
  101. (with-fluids* fluids convs thunk)
  102. (dynamic-wind
  103. swapper
  104. (lambda ()
  105. (with-fluids* fluids convs thunk))
  106. swapper))
  107. (if (memq (car params) port-list)
  108. (more (cdr params) (cdr values)
  109. fluids convs
  110. (let ((param (car params))
  111. (value (car values))
  112. (prev-swapper swapper))
  113. (lambda ()
  114. (set! value (param value))
  115. (prev-swapper))))
  116. (more (cdr params) (cdr values)
  117. (cons ((car params) get-fluid-tag) fluids)
  118. (cons (((car params) get-conv-tag) (car values)) convs)
  119. swapper)))))