(scm_char_set_xor): bug fix: characters should only be included if
[bpt/guile.git] / ice-9 / syncase.scm
1 ;;;; Copyright (C) 1997, 2000 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
47 \f
48
49 (define-public sc-macro
50 (procedure->memoizing-macro
51 (lambda (exp env)
52 (sc-expand exp))))
53
54 ;;; Exported variables
55
56 (define-public sc-expand #f)
57 (define-public sc-expand3 #f)
58 (define-public install-global-transformer #f)
59 (define-public syntax-dispatch #f)
60 (define-public syntax-error #f)
61
62 (define-public bound-identifier=? #f)
63 (define-public datum->syntax-object #f)
64 (define-public define-syntax sc-macro)
65 (define-public eval-when sc-macro)
66 (define-public fluid-let-syntax sc-macro)
67 (define-public free-identifier=? #f)
68 (define-public generate-temporaries #f)
69 (define-public identifier? #f)
70 (define-public identifier-syntax sc-macro)
71 (define-public let-syntax sc-macro)
72 (define-public letrec-syntax sc-macro)
73 (define-public syntax sc-macro)
74 (define-public syntax-case sc-macro)
75 (define-public syntax-object->datum #f)
76 (define-public syntax-rules sc-macro)
77 (define-public with-syntax sc-macro)
78 (define-public include sc-macro)
79
80 (define primitive-syntax '(quote lambda letrec if set! begin define or
81 and let let* cond do quasiquote unquote
82 unquote-splicing case))
83
84 (for-each (lambda (symbol)
85 (set-symbol-property! symbol 'primitive-syntax #t))
86 primitive-syntax)
87
88 ;;; Hooks needed by the syntax-case macro package
89
90 (define-public (void) *unspecified*)
91
92 (define andmap
93 (lambda (f first . rest)
94 (or (null? first)
95 (if (null? rest)
96 (let andmap ((first first))
97 (let ((x (car first)) (first (cdr first)))
98 (if (null? first)
99 (f x)
100 (and (f x) (andmap first)))))
101 (let andmap ((first first) (rest rest))
102 (let ((x (car first))
103 (xr (map car rest))
104 (first (cdr first))
105 (rest (map cdr rest)))
106 (if (null? first)
107 (apply f (cons x xr))
108 (and (apply f (cons x xr)) (andmap first rest)))))))))
109
110 (define (error who format-string why what)
111 (start-stack 'syncase-stack
112 (scm-error 'misc-error
113 who
114 "~A ~S"
115 (list why what)
116 '())))
117
118 (define the-syncase-module (current-module))
119
120 (define (putprop symbol key binding)
121 (let* ((m (current-module))
122 (v (or (module-variable m symbol)
123 (module-make-local-var! m symbol))))
124 (if (symbol-property symbol 'primitive-syntax)
125 (if (eq? (current-module) the-syncase-module)
126 (set-object-property! (module-variable the-root-module symbol)
127 key
128 binding))
129 (variable-set! v sc-macro))
130 (set-object-property! v key binding)))
131
132 (define (getprop symbol key)
133 (let* ((m (current-module))
134 (v (module-variable m symbol)))
135 (and v (or (object-property v key)
136 (let ((root-v (module-local-variable the-root-module symbol)))
137 (and (equal? root-v v)
138 (object-property root-v key)))))))
139
140 (define generated-symbols (make-weak-key-hash-table 1019))
141
142 ;;; Utilities
143
144 (define (psyncomp)
145 (system "mv -f psyntax.pp psyntax.pp~")
146 (let ((in (open-input-file "psyntax.ss"))
147 (out (open-output-file "psyntax.pp")))
148 (let loop ((x (read in)))
149 (if (eof-object? x)
150 (begin
151 (close-port out)
152 (close-port in))
153 (begin
154 (write (sc-expand3 x 'c '(compile load eval)) out)
155 (newline out)
156 (loop (read in)))))))
157
158 ;;; Load the preprocessed code
159
160 (let ((old-debug #f)
161 (old-read #f))
162 (dynamic-wind (lambda ()
163 (set! old-debug (debug-options))
164 (set! old-read (read-options)))
165 (lambda ()
166 (debug-disable 'debug 'procnames)
167 (read-disable 'positions)
168 (load-from-path "ice-9/psyntax.pp"))
169 (lambda ()
170 (debug-options old-debug)
171 (read-options old-read))))
172
173
174 ;;; The following lines are necessary only if we start making changes
175 ;; (use-syntax sc-expand)
176 ;; (load-from-path "ice-9/psyntax.ss")
177
178 (define internal-eval (nested-ref the-scm-module '(app modules guile eval)))
179
180 (define-public (eval x environment)
181 (internal-eval (if (and (pair? x)
182 (equal? (car x) "noexpand"))
183 (cadr x)
184 (sc-expand x))
185 environment))
186
187 ;;; Hack to make syncase macros work in the slib module
188 (let ((m (nested-ref the-root-module '(app modules ice-9 slib))))
189 (if m
190 (set-object-property! (module-local-variable m 'define)
191 '*sc-expander*
192 '(define))))
193
194 (define-public syncase sc-expand)