*** empty log message ***
[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 ;;; Utilities
118
119 (define (psyncomp)
120 (system "mv -f psyntax.pp psyntax.pp~")
121 (let ((in (open-input-file "psyntax.ss"))
122 (out (open-output-file "psyntax.pp")))
123 (let loop ((x (read in)))
124 (if (eof-object? x)
125 (begin
126 (close-port out)
127 (close-port in))
128 (begin
129 (write (sc-expand3 x 'c '(compile load eval)) out)
130 (newline out)
131 (loop (read in)))))))
132
133 ;;; Load the preprocessed code
134
135 (let ((old-debug #f)
136 (old-read #f))
137 (dynamic-wind (lambda ()
138 (set! old-debug (debug-options))
139 (set! old-read (read-options)))
140 (lambda ()
141 (debug-disable 'debug 'procnames)
142 (read-disable 'positions)
143 (load-from-path "ice-9/psyntax.pp"))
144 (lambda ()
145 (debug-options old-debug)
146 (read-options old-read))))
147
148
149 ;;; The following line is necessary only if we start making changes
150 ;; (load-from-path "ice-9/psyntax.ss")
151
152 (define internal-eval (nested-ref the-scm-module '(app modules guile eval)))
153
154 (define-public (eval x)
155 (internal-eval (if (and (pair? x)
156 (string=? (car x) "noexpand"))
157 (cadr x)
158 (sc-expand x))))
159
160 ;;; Hack to make syncase macros work in the slib module
161 (let ((m (nested-ref the-root-module '(app modules ice-9 slib))))
162 (if m
163 (set-object-property! (module-local-variable m 'define)
164 '*sc-expander*
165 '(define))))
166
167 (define-public syncase sc-expand)