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