srfi-39.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 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 parameterize
  60. (syntax-rules ()
  61. ((_ ((?param ?value) ...) ?body ...)
  62. (with-parameters* (list ?param ...)
  63. (list ?value ...)
  64. (lambda () ?body ...)))))
  65. (define (current-input-port . new-value)
  66. (if (null? new-value)
  67. ((@ (guile) current-input-port))
  68. (apply set-current-input-port new-value)))
  69. (define (current-output-port . new-value)
  70. (if (null? new-value)
  71. ((@ (guile) current-output-port))
  72. (apply set-current-output-port new-value)))
  73. (define (current-error-port . new-value)
  74. (if (null? new-value)
  75. ((@ (guile) current-error-port))
  76. (apply set-current-error-port new-value)))
  77. (define port-list
  78. (list current-input-port current-output-port current-error-port))
  79. ;; There are no fluids behind current-input-port etc, so those parameter
  80. ;; objects are picked out of the list and handled separately with a
  81. ;; dynamic-wind to swap their values to and from a location (the "value"
  82. ;; variable in the swapper procedure "let").
  83. ;;
  84. ;; current-input-port etc are already per-dynamic-root, so this arrangement
  85. ;; works the same as a fluid. Perhaps they could become fluids for ease of
  86. ;; implementation here.
  87. ;;
  88. ;; Notice the use of a param local variable for the swapper procedure. It
  89. ;; ensures any application changes to the PARAMS list won't affect the
  90. ;; winding.
  91. ;;
  92. (define (with-parameters* params values thunk)
  93. (let more ((params params)
  94. (values values)
  95. (fluids '()) ;; fluids from each of PARAMS
  96. (convs '()) ;; VALUES with conversion proc applied
  97. (swapper noop)) ;; wind/unwind procedure for ports handling
  98. (if (null? params)
  99. (if (eq? noop swapper)
  100. (with-fluids* fluids convs thunk)
  101. (dynamic-wind
  102. swapper
  103. (lambda ()
  104. (with-fluids* fluids convs thunk))
  105. swapper))
  106. (if (memq (car params) port-list)
  107. (more (cdr params) (cdr values)
  108. fluids convs
  109. (let ((param (car params))
  110. (value (car values))
  111. (prev-swapper swapper))
  112. (lambda ()
  113. (set! value (param value))
  114. (prev-swapper))))
  115. (more (cdr params) (cdr values)
  116. (cons ((car params) get-fluid-tag) fluids)
  117. (cons (((car params) get-conv-tag) (car values)) convs)
  118. swapper)))))