(ice9_sources): Add gap-buffer.scm.
[bpt/guile.git] / ice-9 / syncase.scm
1 ;;;; Copyright (C) 1997, 2000, 2001 Free Software Foundation, Inc.
2 ;;;;
3 ;;;; This program is free software; you can redistribute it and/or modify
4 ;;;; it under the terms of the GNU General Public License as published by
5 ;;;; the Free Software Foundation; either version 2, or (at your option)
6 ;;;; any later version.
7 ;;;;
8 ;;;; This program is distributed in the hope that it will be useful,
9 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ;;;; GNU General Public License for more details.
12 ;;;;
13 ;;;; You should have received a copy of the GNU General Public License
14 ;;;; along with this software; see the file COPYING. If not, write to
15 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 ;;;; Boston, MA 02111-1307 USA
17 ;;;;
18 ;;;; As a special exception, the Free Software Foundation gives permission
19 ;;;; for additional uses of the text contained in its release of GUILE.
20 ;;;;
21 ;;;; The exception is that, if you link the GUILE library with other files
22 ;;;; to produce an executable, this does not by itself cause the
23 ;;;; resulting executable to be covered by the GNU General Public License.
24 ;;;; Your use of that executable is in no way restricted on account of
25 ;;;; linking the GUILE library code into it.
26 ;;;;
27 ;;;; This exception does not however invalidate any other reasons why
28 ;;;; the executable file might be covered by the GNU General Public License.
29 ;;;;
30 ;;;; This exception applies only to the code released by the
31 ;;;; Free Software Foundation under the name GUILE. If you copy
32 ;;;; code from other Free Software Foundation releases into a copy of
33 ;;;; GUILE, as the General Public License permits, the exception does
34 ;;;; not apply to the code that you add in this way. To avoid misleading
35 ;;;; anyone as to the status of such modified files, you must delete
36 ;;;; this exception notice from them.
37 ;;;;
38 ;;;; If you write modifications of your own for GUILE, it is your choice
39 ;;;; whether to permit this exception to apply to your modifications.
40 ;;;; If you do not wish that, delete this exception notice.
41 ;;;;
42 \f
43
44 (define-module (ice-9 syncase)
45 :use-module (ice-9 debug)
46 :export-syntax (sc-macro define-syntax eval-when fluid-let-syntax
47 identifier-syntax let-syntax
48 letrec-syntax syntax syntax-case syntax-rules
49 with-syntax
50 include)
51 :export (sc-expand sc-expand3 install-global-transformer
52 syntax-dispatch syntax-error bound-identifier=?
53 datum->syntax-object free-identifier=?
54 generate-temporaries identifier? syntax-object->datum
55 void eval syncase))
56
57 \f
58
59 (define sc-macro
60 (procedure->memoizing-macro
61 (lambda (exp env)
62 (sc-expand exp))))
63
64 ;;; Exported variables
65
66 (define sc-expand #f)
67 (define sc-expand3 #f)
68 (define install-global-transformer #f)
69 (define syntax-dispatch #f)
70 (define syntax-error #f)
71
72 (define bound-identifier=? #f)
73 (define datum->syntax-object #f)
74 (define define-syntax sc-macro)
75 (define eval-when sc-macro)
76 (define fluid-let-syntax sc-macro)
77 (define free-identifier=? #f)
78 (define generate-temporaries #f)
79 (define identifier? #f)
80 (define identifier-syntax sc-macro)
81 (define let-syntax sc-macro)
82 (define letrec-syntax sc-macro)
83 (define syntax sc-macro)
84 (define syntax-case sc-macro)
85 (define syntax-object->datum #f)
86 (define syntax-rules sc-macro)
87 (define with-syntax sc-macro)
88 (define include sc-macro)
89
90 (define primitive-syntax '(quote lambda letrec if set! begin define or
91 and let let* cond do quasiquote unquote
92 unquote-splicing case))
93
94 (for-each (lambda (symbol)
95 (set-symbol-property! symbol 'primitive-syntax #t))
96 primitive-syntax)
97
98 ;;; Hooks needed by the syntax-case macro package
99
100 (define (void) *unspecified*)
101
102 (define andmap
103 (lambda (f first . rest)
104 (or (null? first)
105 (if (null? rest)
106 (let andmap ((first first))
107 (let ((x (car first)) (first (cdr first)))
108 (if (null? first)
109 (f x)
110 (and (f x) (andmap first)))))
111 (let andmap ((first first) (rest rest))
112 (let ((x (car first))
113 (xr (map car rest))
114 (first (cdr first))
115 (rest (map cdr rest)))
116 (if (null? first)
117 (apply f (cons x xr))
118 (and (apply f (cons x xr)) (andmap first rest)))))))))
119
120 (define (error who format-string why what)
121 (start-stack 'syncase-stack
122 (scm-error 'misc-error
123 who
124 "~A ~S"
125 (list why what)
126 '())))
127
128 (define the-syncase-module (current-module))
129
130 (define (putprop symbol key binding)
131 (let* ((m (current-module))
132 (v (or (module-variable m symbol)
133 (module-make-local-var! m symbol))))
134 (if (symbol-property symbol 'primitive-syntax)
135 (if (eq? (current-module) the-syncase-module)
136 (set-object-property! (module-variable the-root-module symbol)
137 key
138 binding))
139 (variable-set! v sc-macro))
140 (set-object-property! v key binding)))
141
142 (define (getprop symbol key)
143 (let* ((m (current-module))
144 (v (module-variable m symbol)))
145 (and v (or (object-property v key)
146 (let ((root-v (module-local-variable the-root-module symbol)))
147 (and (equal? root-v v)
148 (object-property root-v key)))))))
149
150 (define generated-symbols (make-weak-key-hash-table 1019))
151
152 ;; We define our own gensym here because the Guile built-in one will
153 ;; eventually produce uninterned and unreadable symbols (as needed for
154 ;; safe macro expansions) and will the be inappropriate for dumping to
155 ;; pssyntax.pp.
156 ;;
157 ;; syncase is supposed to only require that gensym produce unique
158 ;; readable symbols, and they only need be unique with respect to
159 ;; multiple calls to gensym, not globally unique.
160 ;;
161 (define gensym
162 (let ((counter 0))
163
164 (define next-id
165 (if (provided? 'threads)
166 (let ((symlock (make-mutex)))
167 (lambda ()
168 (let ((result #f))
169 (with-mutex symlock
170 (set! result counter)
171 (set! counter (+ counter 1)))
172 result)))
173 ;; faster, non-threaded case.
174 (lambda ()
175 (let ((result counter))
176 (set! counter (+ counter 1))
177 result))))
178
179 ;; actual gensym body code.
180 (lambda (. rest)
181 (let* ((next-val (next-id))
182 (valstr (number->string next-val)))
183 (cond
184 ((null? rest)
185 (string->symbol (string-append "syntmp-" valstr)))
186 ((null? (cdr rest))
187 (string->symbol (string-append "syntmp-" (car rest) "-" valstr)))
188 (else
189 (error
190 (string-append
191 "syncase's gensym expected 0 or 1 arguments, got "
192 (length rest)))))))))
193
194 ;;; Load the preprocessed code
195
196 (let ((old-debug #f)
197 (old-read #f))
198 (dynamic-wind (lambda ()
199 (set! old-debug (debug-options))
200 (set! old-read (read-options)))
201 (lambda ()
202 (debug-disable 'debug 'procnames)
203 (read-disable 'positions)
204 (load-from-path "ice-9/psyntax.pp"))
205 (lambda ()
206 (debug-options old-debug)
207 (read-options old-read))))
208
209
210 ;;; The following lines are necessary only if we start making changes
211 ;; (use-syntax sc-expand)
212 ;; (load-from-path "ice-9/psyntax.ss")
213
214 (define internal-eval (nested-ref the-scm-module '(app modules guile eval)))
215
216 (define (eval x environment)
217 (internal-eval (if (and (pair? x)
218 (equal? (car x) "noexpand"))
219 (cadr x)
220 (sc-expand x))
221 environment))
222
223 ;;; Hack to make syncase macros work in the slib module
224 (let ((m (nested-ref the-root-module '(app modules ice-9 slib))))
225 (if m
226 (set-object-property! (module-local-variable m 'define)
227 '*sc-expander*
228 '(define))))
229
230 (define syncase sc-expand)