srfi-39.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. ;;; srfi-39.scm --- Parameter objects
  2. ;; Copyright (C) 2004, 2005, 2006, 2008, 2011 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 3 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 (srfi srfi-16)
  33. #:export (make-parameter)
  34. #:export-syntax (parameterize)
  35. ;; helper procedure not in srfi-39.
  36. #:export (with-parameters*)
  37. #:replace (current-input-port current-output-port current-error-port))
  38. ;; Make 'srfi-39 available as a feature identifiere to `cond-expand'.
  39. ;;
  40. (cond-expand-provide (current-module) '(srfi-39))
  41. (define make-parameter
  42. (case-lambda
  43. ((val) (make-parameter/helper val (lambda (x) x)))
  44. ((val conv) (make-parameter/helper val conv))))
  45. (define get-fluid-tag (lambda () 'get-fluid)) ;; arbitrary unique (as per eq?) value
  46. (define get-conv-tag (lambda () 'get-conv)) ;; arbitrary unique (as per eq?) value
  47. (define (make-parameter/helper val conv)
  48. (let ((value (make-fluid))
  49. (conv conv))
  50. (begin
  51. (fluid-set! value (conv val))
  52. (lambda new-value
  53. (cond
  54. ((null? new-value) (fluid-ref value))
  55. ((eq? (car new-value) get-fluid-tag) value)
  56. ((eq? (car new-value) get-conv-tag) conv)
  57. ((null? (cdr new-value)) (fluid-set! value (conv (car new-value))))
  58. (else (error "make-parameter expects 0 or 1 arguments" new-value)))))))
  59. (define-syntax-rule (parameterize ((?param ?value) ...) ?body ...)
  60. (with-parameters* (list ?param ...)
  61. (list ?value ...)
  62. (lambda () ?body ...)))
  63. (define (current-input-port . new-value)
  64. (if (null? new-value)
  65. ((@ (guile) current-input-port))
  66. (apply set-current-input-port new-value)))
  67. (define (current-output-port . new-value)
  68. (if (null? new-value)
  69. ((@ (guile) current-output-port))
  70. (apply set-current-output-port new-value)))
  71. (define (current-error-port . new-value)
  72. (if (null? new-value)
  73. ((@ (guile) current-error-port))
  74. (apply set-current-error-port new-value)))
  75. (define port-list
  76. (list current-input-port current-output-port current-error-port))
  77. ;; There are no fluids behind current-input-port etc, so those parameter
  78. ;; objects are picked out of the list and handled separately with a
  79. ;; dynamic-wind to swap their values to and from a location (the "value"
  80. ;; variable in the swapper procedure "let").
  81. ;;
  82. ;; current-input-port etc are already per-dynamic-root, so this arrangement
  83. ;; works the same as a fluid. Perhaps they could become fluids for ease of
  84. ;; implementation here.
  85. ;;
  86. ;; Notice the use of a param local variable for the swapper procedure. It
  87. ;; ensures any application changes to the PARAMS list won't affect the
  88. ;; winding.
  89. ;;
  90. (define (with-parameters* params values thunk)
  91. (let more ((params params)
  92. (values values)
  93. (fluids '()) ;; fluids from each of PARAMS
  94. (convs '()) ;; VALUES with conversion proc applied
  95. (swapper noop)) ;; wind/unwind procedure for ports handling
  96. (if (null? params)
  97. (if (eq? noop swapper)
  98. (with-fluids* fluids convs thunk)
  99. (dynamic-wind
  100. swapper
  101. (lambda ()
  102. (with-fluids* fluids convs thunk))
  103. swapper))
  104. (if (memq (car params) port-list)
  105. (more (cdr params) (cdr values)
  106. fluids convs
  107. (let ((param (car params))
  108. (value (car values))
  109. (prev-swapper swapper))
  110. (lambda ()
  111. (set! value (param value))
  112. (prev-swapper))))
  113. (more (cdr params) (cdr values)
  114. (cons ((car params) get-fluid-tag) fluids)
  115. (cons (((car params) get-conv-tag) (car values)) convs)
  116. swapper)))))