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