1 ;;;; Copyright (C) 1997, 2000, 2001 Free Software Foundation, Inc.
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.
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.
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
18 ;;;; As a special exception, the Free Software Foundation gives permission
19 ;;;; for additional uses of the text contained in its release of GUILE.
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.
27 ;;;; This exception does not however invalidate any other reasons why
28 ;;;; the executable file might be covered by the GNU General Public License.
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.
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.
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
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
60 (procedure->memoizing-macro
64 ;;; Exported variables
67 (define sc-expand3 #f)
68 (define install-global-transformer #f)
69 (define syntax-dispatch #f)
70 (define syntax-error #f)
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)
90 (define primitive-syntax '(quote lambda letrec if set! begin define or
91 and let let* cond do quasiquote unquote
92 unquote-splicing case))
94 (for-each (lambda (symbol)
95 (set-symbol-property! symbol 'primitive-syntax #t))
98 ;;; Hooks needed by the syntax-case macro package
100 (define (void) *unspecified*)
103 (lambda (f first . rest)
106 (let andmap ((first first))
107 (let ((x (car first)) (first (cdr first)))
110 (and (f x) (andmap first)))))
111 (let andmap ((first first) (rest rest))
112 (let ((x (car first))
115 (rest (map cdr rest)))
117 (apply f (cons x xr))
118 (and (apply f (cons x xr)) (andmap first rest)))))))))
120 (define (error who format-string why what)
121 (start-stack 'syncase-stack
122 (scm-error 'misc-error
128 (define the-syncase-module (current-module))
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)
139 (variable-set! v sc-macro))
140 (set-object-property! v key binding)))
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)))))))
150 (define generated-symbols (make-weak-key-hash-table 1019))
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
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.
165 (if (provided? 'threads)
166 (let ((symlock (make-mutex)))
170 (set! result counter)
171 (set! counter (+ counter 1)))
173 ;; faster, non-threaded case.
175 (let ((result counter))
176 (set! counter (+ counter 1))
179 ;; actual gensym body code.
181 (let* ((next-val (next-id))
182 (valstr (number->string next-val)))
185 (string->symbol (string-append "syntmp-" valstr)))
187 (string->symbol (string-append "syntmp-" (car rest) "-" valstr)))
191 "syncase's gensym expected 0 or 1 arguments, got "
192 (length rest)))))))))
194 ;;; Load the preprocessed code
198 (dynamic-wind (lambda ()
199 (set! old-debug (debug-options))
200 (set! old-read (read-options)))
202 (debug-disable 'debug 'procnames)
203 (read-disable 'positions)
204 (load-from-path "ice-9/psyntax.pp"))
206 (debug-options old-debug)
207 (read-options old-read))))
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")
214 (define internal-eval (nested-ref the-scm-module '(app modules guile eval)))
216 (define (eval x environment)
217 (internal-eval (if (and (pair? x)
218 (equal? (car x) "noexpand"))
223 ;;; Hack to make syncase macros work in the slib module
224 (let ((m (nested-ref the-root-module '(app modules ice-9 slib))))
226 (set-object-property! (module-local-variable m 'define)
230 (define syncase sc-expand)