Do not defer expansion of internal define-syntax forms.
[bpt/guile.git] / module / ice-9 / psyntax-pp.scm
CommitLineData
9c35c579 1(eval-when (compile) (set-current-module (resolve-module (quote (guile)))))
41af2381 2(if #f #f)
65dd9e38 3
72ee0ef7
MW
4(letrec*
5 ((make-void
6 (lambda (src) (make-struct (vector-ref %expanded-vtables 0) 0 src)))
7 (make-const
8 (lambda (src exp)
9 (make-struct (vector-ref %expanded-vtables 1) 0 src exp)))
10 (make-primitive-ref
11 (lambda (src name)
12 (make-struct (vector-ref %expanded-vtables 2) 0 src name)))
13 (make-lexical-ref
14 (lambda (src name gensym)
15 (make-struct (vector-ref %expanded-vtables 3) 0 src name gensym)))
16 (make-lexical-set
17 (lambda (src name gensym exp)
18 (make-struct (vector-ref %expanded-vtables 4) 0 src name gensym exp)))
19 (make-module-ref
20 (lambda (src mod name public?)
21 (make-struct (vector-ref %expanded-vtables 5) 0 src mod name public?)))
22 (make-module-set
23 (lambda (src mod name public? exp)
24 (make-struct
25 (vector-ref %expanded-vtables 6)
26 0
27 src
28 mod
29 name
30 public?
31 exp)))
32 (make-toplevel-ref
33 (lambda (src name)
34 (make-struct (vector-ref %expanded-vtables 7) 0 src name)))
35 (make-toplevel-set
36 (lambda (src name exp)
37 (make-struct (vector-ref %expanded-vtables 8) 0 src name exp)))
38 (make-toplevel-define
39 (lambda (src name exp)
40 (make-struct (vector-ref %expanded-vtables 9) 0 src name exp)))
41 (make-conditional
42 (lambda (src test consequent alternate)
43 (make-struct
44 (vector-ref %expanded-vtables 10)
45 0
46 src
47 test
48 consequent
49 alternate)))
50 (make-application
51 (lambda (src proc args)
52 (make-struct (vector-ref %expanded-vtables 11) 0 src proc args)))
53 (make-sequence
54 (lambda (src exps)
55 (make-struct (vector-ref %expanded-vtables 12) 0 src exps)))
56 (make-lambda
57 (lambda (src meta body)
58 (make-struct (vector-ref %expanded-vtables 13) 0 src meta body)))
59 (make-lambda-case
60 (lambda (src req opt rest kw inits gensyms body alternate)
61 (make-struct
62 (vector-ref %expanded-vtables 14)
63 0
64 src
65 req
66 opt
67 rest
68 kw
69 inits
70 gensyms
71 body
72 alternate)))
73 (make-let
74 (lambda (src names gensyms vals body)
75 (make-struct
76 (vector-ref %expanded-vtables 15)
77 0
78 src
79 names
80 gensyms
81 vals
82 body)))
83 (make-letrec
84 (lambda (src in-order? names gensyms vals body)
85 (make-struct
86 (vector-ref %expanded-vtables 16)
87 0
88 src
89 in-order?
90 names
91 gensyms
92 vals
93 body)))
94 (make-dynlet
95 (lambda (src fluids vals body)
96 (make-struct
97 (vector-ref %expanded-vtables 17)
98 0
99 src
100 fluids
101 vals
102 body)))
103 (lambda?
104 (lambda (x)
105 (and (struct? x)
106 (eq? (struct-vtable x) (vector-ref %expanded-vtables 13)))))
107 (lambda-meta (lambda (x) (struct-ref x 1)))
108 (set-lambda-meta! (lambda (x v) (struct-set! x 1 v)))
109 (top-level-eval-hook (lambda (x mod) (primitive-eval x)))
110 (local-eval-hook (lambda (x mod) (primitive-eval x)))
111 (session-id
112 (let ((v (module-variable (current-module) 'syntax-session-id)))
113 (lambda () ((variable-ref v)))))
114 (put-global-definition-hook
115 (lambda (symbol type val)
116 (module-define!
117 (current-module)
118 symbol
119 (make-syntax-transformer symbol type val))))
120 (get-global-definition-hook
121 (lambda (symbol module)
122 (if (and (not module) (current-module))
123 (warn "module system is booted, we should have a module" symbol))
124 (let ((v (module-variable
125 (if module (resolve-module (cdr module)) (current-module))
126 symbol)))
127 (and v
128 (variable-bound? v)
129 (let ((val (variable-ref v)))
130 (and (macro? val)
131 (macro-type val)
132 (cons (macro-type val) (macro-binding val))))))))
133 (decorate-source
134 (lambda (e s)
135 (if (and s (supports-source-properties? e))
136 (set-source-properties! e s))
137 e))
138 (maybe-name-value!
139 (lambda (name val)
140 (if (lambda? val)
141 (let ((meta (lambda-meta val)))
142 (if (not (assq 'name meta))
143 (set-lambda-meta! val (acons 'name name meta)))))))
144 (build-void (lambda (source) (make-void source)))
145 (build-application
146 (lambda (source fun-exp arg-exps)
147 (make-application source fun-exp arg-exps)))
148 (build-conditional
149 (lambda (source test-exp then-exp else-exp)
150 (make-conditional source test-exp then-exp else-exp)))
151 (build-dynlet
152 (lambda (source fluids vals body)
153 (make-dynlet source fluids vals body)))
154 (build-lexical-reference
155 (lambda (type source name var) (make-lexical-ref source name var)))
156 (build-lexical-assignment
157 (lambda (source name var exp)
158 (maybe-name-value! name exp)
159 (make-lexical-set source name var exp)))
160 (analyze-variable
161 (lambda (mod var modref-cont bare-cont)
162 (if (not mod)
163 (bare-cont var)
164 (let ((kind (car mod)) (mod (cdr mod)))
165 (let ((key kind))
166 (cond ((memv key '(public)) (modref-cont mod var #t))
167 ((memv key '(private))
168 (if (not (equal? mod (module-name (current-module))))
169 (modref-cont mod var #f)
170 (bare-cont var)))
171 ((memv key '(bare)) (bare-cont var))
172 ((memv key '(hygiene))
173 (if (and (not (equal? mod (module-name (current-module))))
174 (module-variable (resolve-module mod) var))
175 (modref-cont mod var #f)
176 (bare-cont var)))
177 (else (syntax-violation #f "bad module kind" var mod))))))))
178 (build-global-reference
179 (lambda (source var mod)
180 (analyze-variable
181 mod
182 var
183 (lambda (mod var public?) (make-module-ref source mod var public?))
184 (lambda (var) (make-toplevel-ref source var)))))
185 (build-global-assignment
186 (lambda (source var exp mod)
187 (maybe-name-value! var exp)
188 (analyze-variable
189 mod
190 var
191 (lambda (mod var public?)
192 (make-module-set source mod var public? exp))
193 (lambda (var) (make-toplevel-set source var exp)))))
194 (build-global-definition
195 (lambda (source var exp)
196 (maybe-name-value! var exp)
197 (make-toplevel-define source var exp)))
198 (build-simple-lambda
199 (lambda (src req rest vars meta exp)
200 (make-lambda
201 src
202 meta
203 (make-lambda-case src req #f rest #f '() vars exp #f))))
204 (build-case-lambda
205 (lambda (src meta body) (make-lambda src meta body)))
206 (build-lambda-case
207 (lambda (src req opt rest kw inits vars body else-case)
208 (make-lambda-case src req opt rest kw inits vars body else-case)))
209 (build-primref
210 (lambda (src name)
211 (if (equal? (module-name (current-module)) '(guile))
212 (make-toplevel-ref src name)
213 (make-module-ref src '(guile) name #f))))
214 (build-data (lambda (src exp) (make-const src exp)))
215 (build-sequence
216 (lambda (src exps)
217 (if (null? (cdr exps)) (car exps) (make-sequence src exps))))
218 (build-let
219 (lambda (src ids vars val-exps body-exp)
220 (for-each maybe-name-value! ids val-exps)
221 (if (null? vars) body-exp (make-let src ids vars val-exps body-exp))))
222 (build-named-let
223 (lambda (src ids vars val-exps body-exp)
224 (let ((f (car vars)) (f-name (car ids)) (vars (cdr vars)) (ids (cdr ids)))
225 (let ((proc (build-simple-lambda src ids #f vars '() body-exp)))
226 (maybe-name-value! f-name proc)
227 (for-each maybe-name-value! ids val-exps)
228 (make-letrec
229 src
230 #f
231 (list f-name)
232 (list f)
233 (list proc)
234 (build-application
235 src
236 (build-lexical-reference 'fun src f-name f)
237 val-exps))))))
238 (build-letrec
239 (lambda (src in-order? ids vars val-exps body-exp)
240 (if (null? vars)
241 body-exp
9b0975f1 242 (begin
72ee0ef7
MW
243 (for-each maybe-name-value! ids val-exps)
244 (make-letrec src in-order? ids vars val-exps body-exp)))))
245 (make-syntax-object
246 (lambda (expression wrap module)
247 (vector 'syntax-object expression wrap module)))
248 (syntax-object?
249 (lambda (x)
250 (and (vector? x)
251 (= (vector-length x) 4)
252 (eq? (vector-ref x 0) 'syntax-object))))
253 (syntax-object-expression (lambda (x) (vector-ref x 1)))
254 (syntax-object-wrap (lambda (x) (vector-ref x 2)))
255 (syntax-object-module (lambda (x) (vector-ref x 3)))
256 (set-syntax-object-expression!
257 (lambda (x update) (vector-set! x 1 update)))
258 (set-syntax-object-wrap!
259 (lambda (x update) (vector-set! x 2 update)))
260 (set-syntax-object-module!
261 (lambda (x update) (vector-set! x 3 update)))
262 (source-annotation
263 (lambda (x)
264 (let ((props (source-properties
265 (if (syntax-object? x) (syntax-object-expression x) x))))
266 (and (pair? props) props))))
267 (extend-env
268 (lambda (labels bindings r)
269 (if (null? labels)
270 r
271 (extend-env
272 (cdr labels)
273 (cdr bindings)
274 (cons (cons (car labels) (car bindings)) r)))))
275 (extend-var-env
276 (lambda (labels vars r)
277 (if (null? labels)
278 r
279 (extend-var-env
280 (cdr labels)
281 (cdr vars)
282 (cons (cons (car labels) (cons 'lexical (car vars))) r)))))
283 (macros-only-env
284 (lambda (r)
285 (if (null? r)
286 '()
287 (let ((a (car r)))
288 (if (eq? (cadr a) 'macro)
289 (cons a (macros-only-env (cdr r)))
290 (macros-only-env (cdr r)))))))
291 (lookup
292 (lambda (x r mod)
293 (let ((t (assq x r)))
294 (cond (t (cdr t))
295 ((symbol? x) (or (get-global-definition-hook x mod) '(global)))
296 (else '(displaced-lexical))))))
297 (global-extend
298 (lambda (type sym val) (put-global-definition-hook sym type val)))
299 (nonsymbol-id?
300 (lambda (x)
301 (and (syntax-object? x) (symbol? (syntax-object-expression x)))))
302 (id? (lambda (x)
303 (if (symbol? x)
304 #t
305 (and (syntax-object? x) (symbol? (syntax-object-expression x))))))
306 (id-sym-name&marks
307 (lambda (x w)
308 (if (syntax-object? x)
309 (values
310 (syntax-object-expression x)
311 (join-marks (car w) (car (syntax-object-wrap x))))
312 (values x (car w)))))
313 (gen-label
314 (lambda ()
315 (string-append "l-" (session-id) (symbol->string (gensym "-")))))
316 (gen-labels
317 (lambda (ls)
318 (if (null? ls) '() (cons (gen-label) (gen-labels (cdr ls))))))
319 (make-ribcage
320 (lambda (symnames marks labels)
321 (vector 'ribcage symnames marks labels)))
322 (ribcage?
323 (lambda (x)
324 (and (vector? x)
325 (= (vector-length x) 4)
326 (eq? (vector-ref x 0) 'ribcage))))
327 (ribcage-symnames (lambda (x) (vector-ref x 1)))
328 (ribcage-marks (lambda (x) (vector-ref x 2)))
329 (ribcage-labels (lambda (x) (vector-ref x 3)))
330 (set-ribcage-symnames! (lambda (x update) (vector-set! x 1 update)))
331 (set-ribcage-marks! (lambda (x update) (vector-set! x 2 update)))
332 (set-ribcage-labels! (lambda (x update) (vector-set! x 3 update)))
333 (anti-mark
334 (lambda (w) (cons (cons #f (car w)) (cons 'shift (cdr w)))))
335 (extend-ribcage!
336 (lambda (ribcage id label)
337 (set-ribcage-symnames!
338 ribcage
339 (cons (syntax-object-expression id) (ribcage-symnames ribcage)))
340 (set-ribcage-marks!
341 ribcage
342 (cons (car (syntax-object-wrap id)) (ribcage-marks ribcage)))
343 (set-ribcage-labels! ribcage (cons label (ribcage-labels ribcage)))))
344 (make-binding-wrap
345 (lambda (ids labels w)
346 (if (null? ids)
347 w
348 (cons (car w)
349 (cons (let* ((labelvec (list->vector labels)) (n (vector-length labelvec)))
350 (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
351 (let f ((ids ids) (i 0))
352 (if (not (null? ids))
353 (call-with-values
354 (lambda () (id-sym-name&marks (car ids) w))
355 (lambda (symname marks)
356 (vector-set! symnamevec i symname)
357 (vector-set! marksvec i marks)
358 (f (cdr ids) (+ i 1))))))
359 (make-ribcage symnamevec marksvec labelvec)))
360 (cdr w))))))
361 (smart-append (lambda (m1 m2) (if (null? m2) m1 (append m1 m2))))
362 (join-wraps
363 (lambda (w1 w2)
364 (let ((m1 (car w1)) (s1 (cdr w1)))
365 (if (null? m1)
366 (if (null? s1) w2 (cons (car w2) (smart-append s1 (cdr w2))))
367 (cons (smart-append m1 (car w2)) (smart-append s1 (cdr w2)))))))
368 (join-marks (lambda (m1 m2) (smart-append m1 m2)))
369 (same-marks?
370 (lambda (x y)
371 (or (eq? x y)
372 (and (not (null? x))
373 (not (null? y))
374 (eq? (car x) (car y))
375 (same-marks? (cdr x) (cdr y))))))
376 (id-var-name
377 (lambda (id w)
378 (letrec*
379 ((search
380 (lambda (sym subst marks)
381 (if (null? subst)
382 (values #f marks)
383 (let ((fst (car subst)))
384 (if (eq? fst 'shift)
385 (search sym (cdr subst) (cdr marks))
386 (let ((symnames (ribcage-symnames fst)))
387 (if (vector? symnames)
388 (search-vector-rib sym subst marks symnames fst)
389 (search-list-rib sym subst marks symnames fst))))))))
390 (search-list-rib
391 (lambda (sym subst marks symnames ribcage)
392 (let f ((symnames symnames) (i 0))
393 (cond ((null? symnames) (search sym (cdr subst) marks))
394 ((and (eq? (car symnames) sym)
395 (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
396 (values (list-ref (ribcage-labels ribcage) i) marks))
397 (else (f (cdr symnames) (+ i 1)))))))
398 (search-vector-rib
399 (lambda (sym subst marks symnames ribcage)
400 (let ((n (vector-length symnames)))
401 (let f ((i 0))
402 (cond ((= i n) (search sym (cdr subst) marks))
403 ((and (eq? (vector-ref symnames i) sym)
404 (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
405 (values (vector-ref (ribcage-labels ribcage) i) marks))
406 (else (f (+ i 1)))))))))
407 (cond ((symbol? id) (or (search id (cdr w) (car w)) id))
408 ((syntax-object? id)
409 (let ((id (syntax-object-expression id)) (w1 (syntax-object-wrap id)))
410 (let ((marks (join-marks (car w) (car w1))))
411 (call-with-values
412 (lambda () (search id (cdr w) marks))
413 (lambda (new-id marks) (or new-id (search id (cdr w1) marks) id))))))
414 (else (syntax-violation 'id-var-name "invalid id" id))))))
415 (locally-bound-identifiers
416 (lambda (w mod)
417 (letrec*
418 ((scan (lambda (subst results)
419 (if (null? subst)
420 results
421 (let ((fst (car subst)))
422 (if (eq? fst 'shift)
423 (scan (cdr subst) results)
424 (let ((symnames (ribcage-symnames fst)) (marks (ribcage-marks fst)))
425 (if (vector? symnames)
426 (scan-vector-rib subst symnames marks results)
427 (scan-list-rib subst symnames marks results))))))))
428 (scan-list-rib
429 (lambda (subst symnames marks results)
430 (let f ((symnames symnames) (marks marks) (results results))
431 (if (null? symnames)
432 (scan (cdr subst) results)
433 (f (cdr symnames)
434 (cdr marks)
435 (cons (wrap (car symnames) (anti-mark (cons (car marks) subst)) mod)
436 results))))))
437 (scan-vector-rib
438 (lambda (subst symnames marks results)
439 (let ((n (vector-length symnames)))
440 (let f ((i 0) (results results))
441 (if (= i n)
442 (scan (cdr subst) results)
443 (f (+ i 1)
444 (cons (wrap (vector-ref symnames i)
445 (anti-mark (cons (vector-ref marks i) subst))
446 mod)
447 results))))))))
448 (scan (cdr w) '()))))
449 (resolve-identifier
450 (lambda (id w r mod)
451 (letrec*
452 ((resolve-global
453 (lambda (var mod)
454 (let ((b (or (get-global-definition-hook var mod) '(global))))
455 (if (eq? (car b) 'global)
456 (values 'global var mod)
457 (values (car b) (cdr b) mod)))))
458 (resolve-lexical
459 (lambda (label mod)
460 (let ((b (or (assq-ref r label) '(displaced-lexical))))
461 (values (car b) (cdr b) mod)))))
462 (let ((n (id-var-name id w)))
463 (cond ((symbol? n)
464 (resolve-global
465 n
466 (if (syntax-object? id) (syntax-object-module id) mod)))
467 ((string? n)
468 (resolve-lexical
469 n
470 (if (syntax-object? id) (syntax-object-module id) mod)))
471 (else (error "unexpected id-var-name" id w n)))))))
472 (transformer-environment
473 (make-fluid
474 (lambda (k)
475 (error "called outside the dynamic extent of a syntax transformer"))))
476 (with-transformer-environment
477 (lambda (k) ((fluid-ref transformer-environment) k)))
478 (free-id=?
479 (lambda (i j)
480 (and (eq? (let ((x i)) (if (syntax-object? x) (syntax-object-expression x) x))
481 (let ((x j)) (if (syntax-object? x) (syntax-object-expression x) x)))
482 (eq? (id-var-name i '(())) (id-var-name j '(()))))))
483 (bound-id=?
484 (lambda (i j)
485 (if (and (syntax-object? i) (syntax-object? j))
486 (and (eq? (syntax-object-expression i) (syntax-object-expression j))
487 (same-marks?
488 (car (syntax-object-wrap i))
489 (car (syntax-object-wrap j))))
490 (eq? i j))))
491 (valid-bound-ids?
492 (lambda (ids)
493 (and (let all-ids? ((ids ids))
494 (or (null? ids) (and (id? (car ids)) (all-ids? (cdr ids)))))
495 (distinct-bound-ids? ids))))
496 (distinct-bound-ids?
497 (lambda (ids)
498 (let distinct? ((ids ids))
499 (or (null? ids)
500 (and (not (bound-id-member? (car ids) (cdr ids)))
501 (distinct? (cdr ids)))))))
502 (bound-id-member?
503 (lambda (x list)
504 (and (not (null? list))
505 (or (bound-id=? x (car list)) (bound-id-member? x (cdr list))))))
506 (wrap (lambda (x w defmod)
507 (cond ((and (null? (car w)) (null? (cdr w))) x)
508 ((syntax-object? x)
509 (make-syntax-object
510 (syntax-object-expression x)
511 (join-wraps w (syntax-object-wrap x))
512 (syntax-object-module x)))
513 ((null? x) x)
514 (else (make-syntax-object x w defmod)))))
515 (source-wrap
516 (lambda (x w s defmod) (wrap (decorate-source x s) w defmod)))
517 (expand-sequence
518 (lambda (body r w s mod)
519 (build-sequence
520 s
521 (let dobody ((body body) (r r) (w w) (mod mod))
522 (if (null? body)
523 '()
524 (let ((first (expand (car body) r w mod)))
525 (cons first (dobody (cdr body) r w mod))))))))
526 (expand-top-sequence
527 (lambda (body r w s m esew mod)
528 (letrec*
529 ((scan (lambda (body r w s m esew mod exps)
530 (if (null? body)
531 exps
532 (call-with-values
533 (lambda ()
534 (call-with-values
535 (lambda ()
536 (let ((e (car body)))
537 (syntax-type e r w (or (source-annotation e) s) #f mod #f)))
538 (lambda (type value form e w s mod)
539 (let ((key type))
540 (cond ((memv key '(begin-form))
541 (let* ((tmp e) (tmp-1 ($sc-dispatch tmp '(_))))
542 (if tmp-1
543 (apply (lambda () exps) tmp-1)
544 (let ((tmp-1 ($sc-dispatch tmp '(_ any . each-any))))
545 (if tmp-1
546 (apply (lambda (e1 e2) (scan (cons e1 e2) r w s m esew mod exps))
547 tmp-1)
548 (syntax-violation
549 #f
550 "source expression failed to match any pattern"
551 tmp))))))
552 ((memv key '(local-syntax-form))
553 (expand-local-syntax
554 value
555 e
556 r
557 w
558 s
559 mod
560 (lambda (body r w s mod) (scan body r w s m esew mod exps))))
561 ((memv key '(eval-when-form))
562 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ each-any any . each-any))))
563 (if tmp
564 (apply (lambda (x e1 e2)
565 (let ((when-list (parse-when-list e x)) (body (cons e1 e2)))
566 (cond ((eq? m 'e)
567 (if (memq 'eval when-list)
568 (scan body
569 r
570 w
571 s
572 (if (memq 'expand when-list) 'c&e 'e)
573 '(eval)
574 mod
575 exps)
576 (begin
577 (if (memq 'expand when-list)
578 (top-level-eval-hook
579 (expand-top-sequence body r w s 'e '(eval) mod)
580 mod))
581 (values exps))))
582 ((memq 'load when-list)
583 (cond ((or (memq 'compile when-list)
584 (memq 'expand when-list)
585 (and (eq? m 'c&e) (memq 'eval when-list)))
586 (scan body r w s 'c&e '(compile load) mod exps))
587 ((memq m '(c c&e))
588 (scan body r w s 'c '(load) mod exps))
589 (else (values exps))))
590 ((or (memq 'compile when-list)
591 (memq 'expand when-list)
592 (and (eq? m 'c&e) (memq 'eval when-list)))
593 (top-level-eval-hook
594 (expand-top-sequence body r w s 'e '(eval) mod)
595 mod)
596 (values exps))
597 (else (values exps)))))
598 tmp)
599 (syntax-violation
600 #f
601 "source expression failed to match any pattern"
602 tmp-1))))
603 ((memv key '(define-syntax-form define-syntax-parameter-form))
604 (let ((n (id-var-name value w)) (r (macros-only-env r)))
605 (let ((key m))
606 (cond ((memv key '(c))
607 (cond ((memq 'compile esew)
608 (let ((e (expand-install-global n (expand e r w mod))))
609 (top-level-eval-hook e mod)
610 (if (memq 'load esew) (values (cons e exps)) (values exps))))
611 ((memq 'load esew)
612 (values
613 (cons (expand-install-global n (expand e r w mod)) exps)))
614 (else (values exps))))
615 ((memv key '(c&e))
616 (let ((e (expand-install-global n (expand e r w mod))))
617 (top-level-eval-hook e mod)
618 (values (cons e exps))))
619 (else
620 (if (memq 'eval esew)
621 (top-level-eval-hook
622 (expand-install-global n (expand e r w mod))
623 mod))
624 (values exps))))))
625 ((memv key '(define-form))
626 (let* ((n (id-var-name value w)) (type (car (lookup n r mod))) (key type))
627 (cond ((memv key '(global core macro module-ref))
628 (if (and (memq m '(c c&e))
629 (not (module-local-variable (current-module) n))
630 (current-module))
631 (let ((old (module-variable (current-module) n)))
632 (if (and (variable? old) (variable-bound? old))
633 (module-define! (current-module) n (variable-ref old))
634 (module-add! (current-module) n (make-undefined-variable)))))
635 (values
636 (cons (if (eq? m 'c&e)
637 (let ((x (build-global-definition s n (expand e r w mod))))
638 (top-level-eval-hook x mod)
639 x)
640 (lambda () (build-global-definition s n (expand e r w mod))))
641 exps)))
642 ((memv key '(displaced-lexical))
643 (syntax-violation
644 #f
645 "identifier out of context"
646 (source-wrap form w s mod)
647 (wrap value w mod)))
648 (else
649 (syntax-violation
650 #f
651 "cannot define keyword at top level"
652 (source-wrap form w s mod)
653 (wrap value w mod))))))
654 (else
34c5fe83 655 (values
72ee0ef7
MW
656 (cons (if (eq? m 'c&e)
657 (let ((x (expand-expr type value form e r w s mod)))
658 (top-level-eval-hook x mod)
659 x)
660 (lambda () (expand-expr type value form e r w s mod)))
661 exps))))))))
662 (lambda (exps) (scan (cdr body) r w s m esew mod exps)))))))
663 (call-with-values
664 (lambda () (scan body r w s m esew mod '()))
665 (lambda (exps)
666 (if (null? exps)
667 (build-void s)
668 (build-sequence
669 s
670 (let lp ((in exps) (out '()))
671 (if (null? in)
672 out
673 (let ((e (car in)))
674 (lp (cdr in) (cons (if (procedure? e) (e) e) out))))))))))))
675 (expand-install-global
676 (lambda (name e)
677 (build-global-definition
678 #f
679 name
680 (build-application
681 #f
682 (build-primref #f 'make-syntax-transformer)
683 (list (build-data #f name) (build-data #f 'macro) e)))))
684 (parse-when-list
685 (lambda (e when-list)
686 (let ((result (strip when-list '(()))))
687 (let lp ((l result))
688 (cond ((null? l) result)
689 ((memq (car l) '(compile load eval expand)) (lp (cdr l)))
690 (else (syntax-violation 'eval-when "invalid situation" e (car l))))))))
691 (syntax-type
692 (lambda (e r w s rib mod for-car?)
693 (cond ((symbol? e)
694 (let* ((n (id-var-name e w))
695 (b (lookup n r mod))
696 (type (car b))
697 (key type))
698 (cond ((memv key '(lexical)) (values type (cdr b) e e w s mod))
699 ((memv key '(global)) (values type n e e w s mod))
700 ((memv key '(macro))
701 (if for-car?
702 (values type (cdr b) e e w s mod)
703 (syntax-type
704 (expand-macro (cdr b) e r w s rib mod)
705 r
9b0975f1 706 '(())
72ee0ef7
MW
707 s
708 rib
709 mod
710 #f)))
711 (else (values type (cdr b) e e w s mod)))))
712 ((pair? e)
713 (let ((first (car e)))
714 (call-with-values
715 (lambda () (syntax-type first r w s rib mod #t))
716 (lambda (ftype fval fform fe fw fs fmod)
717 (let ((key ftype))
718 (cond ((memv key '(lexical)) (values 'lexical-call fval e e w s mod))
719 ((memv key '(global))
720 (values 'global-call (make-syntax-object fval w fmod) e e w s mod))
721 ((memv key '(macro))
722 (syntax-type
723 (expand-macro fval e r w s rib mod)
724 r
725 '(())
726 s
727 rib
728 mod
729 for-car?))
730 ((memv key '(module-ref))
731 (call-with-values
732 (lambda () (fval e r w))
733 (lambda (e r w s mod) (syntax-type e r w s rib mod for-car?))))
734 ((memv key '(core)) (values 'core-form fval e e w s mod))
735 ((memv key '(local-syntax))
736 (values 'local-syntax-form fval e e w s mod))
737 ((memv key '(begin)) (values 'begin-form #f e e w s mod))
738 ((memv key '(eval-when)) (values 'eval-when-form #f e e w s mod))
739 ((memv key '(define))
740 (let* ((tmp e) (tmp-1 ($sc-dispatch tmp '(_ any any))))
741 (if (and tmp-1 (apply (lambda (name val) (id? name)) tmp-1))
742 (apply (lambda (name val) (values 'define-form name e val w s mod))
743 tmp-1)
744 (let ((tmp-1 ($sc-dispatch tmp '(_ (any . any) any . each-any))))
745 (if (and tmp-1
746 (apply (lambda (name args e1 e2)
747 (and (id? name) (valid-bound-ids? (lambda-var-list args))))
748 tmp-1))
749 (apply (lambda (name args e1 e2)
750 (values
751 'define-form
752 (wrap name w mod)
753 (wrap e w mod)
754 (decorate-source
1af6d2a7 755 (cons '#(syntax-object lambda ((top)) (hygiene guile))
72ee0ef7
MW
756 (wrap (cons args (cons e1 e2)) w mod))
757 s)
758 '(())
759 s
760 mod))
761 tmp-1)
762 (let ((tmp-1 ($sc-dispatch tmp '(_ any))))
763 (if (and tmp-1 (apply (lambda (name) (id? name)) tmp-1))
764 (apply (lambda (name)
765 (values
766 'define-form
767 (wrap name w mod)
768 (wrap e w mod)
1af6d2a7 769 '(#(syntax-object if ((top)) (hygiene guile)) #f #f)
72ee0ef7
MW
770 '(())
771 s
772 mod))
773 tmp-1)
774 (syntax-violation
775 #f
776 "source expression failed to match any pattern"
777 tmp))))))))
778 ((memv key '(define-syntax))
779 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ any any))))
780 (if (and tmp (apply (lambda (name val) (id? name)) tmp))
781 (apply (lambda (name val) (values 'define-syntax-form name e val w s mod))
782 tmp)
783 (syntax-violation
784 #f
785 "source expression failed to match any pattern"
786 tmp-1))))
787 ((memv key '(define-syntax-parameter))
788 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ any any))))
789 (if (and tmp (apply (lambda (name val) (id? name)) tmp))
790 (apply (lambda (name val)
791 (values 'define-syntax-parameter-form name e val w s mod))
792 tmp)
793 (syntax-violation
794 #f
795 "source expression failed to match any pattern"
796 tmp-1))))
797 (else (values 'call #f e e w s mod))))))))
798 ((syntax-object? e)
799 (syntax-type
800 (syntax-object-expression e)
801 r
802 (join-wraps w (syntax-object-wrap e))
803 (or (source-annotation e) s)
804 rib
805 (or (syntax-object-module e) mod)
806 for-car?))
807 ((self-evaluating? e) (values 'constant #f e e w s mod))
808 (else (values 'other #f e e w s mod)))))
809 (expand
810 (lambda (e r w mod)
811 (call-with-values
812 (lambda () (syntax-type e r w (source-annotation e) #f mod #f))
813 (lambda (type value form e w s mod)
814 (expand-expr type value form e r w s mod)))))
815 (expand-expr
816 (lambda (type value form e r w s mod)
817 (let ((key type))
818 (cond ((memv key '(lexical)) (build-lexical-reference 'value s e value))
819 ((memv key '(core core-form)) (value e r w s mod))
820 ((memv key '(module-ref))
821 (call-with-values
822 (lambda () (value e r w))
823 (lambda (e r w s mod) (expand e r w mod))))
824 ((memv key '(lexical-call))
825 (expand-application
826 (let ((id (car e)))
827 (build-lexical-reference
828 'fun
829 (source-annotation id)
830 (if (syntax-object? id) (syntax->datum id) id)
831 value))
832 e
833 r
834 w
835 s
836 mod))
837 ((memv key '(global-call))
838 (expand-application
839 (build-global-reference
840 (source-annotation (car e))
841 (if (syntax-object? value) (syntax-object-expression value) value)
842 (if (syntax-object? value) (syntax-object-module value) mod))
843 e
844 r
845 w
846 s
847 mod))
848 ((memv key '(constant))
849 (build-data s (strip (source-wrap e w s mod) '(()))))
850 ((memv key '(global)) (build-global-reference s value mod))
851 ((memv key '(call))
852 (expand-application (expand (car e) r w mod) e r w s mod))
853 ((memv key '(begin-form))
854 (let* ((tmp e) (tmp-1 ($sc-dispatch tmp '(_ any . each-any))))
855 (if tmp-1
856 (apply (lambda (e1 e2) (expand-sequence (cons e1 e2) r w s mod))
857 tmp-1)
858 (let ((tmp-1 ($sc-dispatch tmp '(_))))
859 (if tmp-1
860 (apply (lambda ()
861 (if (include-deprecated-features)
862 (begin
863 (issue-deprecation-warning
864 "Sequences of zero expressions are deprecated. Use *unspecified*.")
865 (expand-void))
866 (syntax-violation
867 #f
868 "sequence of zero expressions"
869 (source-wrap e w s mod))))
870 tmp-1)
871 (syntax-violation
872 #f
873 "source expression failed to match any pattern"
874 tmp))))))
875 ((memv key '(local-syntax-form))
876 (expand-local-syntax value e r w s mod expand-sequence))
877 ((memv key '(eval-when-form))
878 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ each-any any . each-any))))
879 (if tmp
880 (apply (lambda (x e1 e2)
881 (let ((when-list (parse-when-list e x)))
882 (if (memq 'eval when-list)
883 (expand-sequence (cons e1 e2) r w s mod)
884 (expand-void))))
885 tmp)
886 (syntax-violation
887 #f
888 "source expression failed to match any pattern"
889 tmp-1))))
890 ((memv key
891 '(define-form define-syntax-form define-syntax-parameter-form))
892 (syntax-violation
893 #f
894 "definition in expression context, where definitions are not allowed,"
895 (source-wrap form w s mod)))
896 ((memv key '(syntax))
897 (syntax-violation
898 #f
899 "reference to pattern variable outside syntax form"
900 (source-wrap e w s mod)))
901 ((memv key '(displaced-lexical))
902 (syntax-violation
903 #f
904 "reference to identifier outside its scope"
905 (source-wrap e w s mod)))
906 (else
907 (syntax-violation #f "unexpected syntax" (source-wrap e w s mod)))))))
908 (expand-application
909 (lambda (x e r w s mod)
910 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(any . each-any))))
911 (if tmp
912 (apply (lambda (e0 e1)
913 (build-application s x (map (lambda (e) (expand e r w mod)) e1)))
914 tmp)
915 (syntax-violation
916 #f
917 "source expression failed to match any pattern"
918 tmp-1)))))
919 (expand-macro
920 (lambda (p e r w s rib mod)
921 (letrec*
922 ((rebuild-macro-output
923 (lambda (x m)
924 (cond ((pair? x)
925 (decorate-source
926 (cons (rebuild-macro-output (car x) m)
927 (rebuild-macro-output (cdr x) m))
928 s))
929 ((syntax-object? x)
930 (let ((w (syntax-object-wrap x)))
931 (let ((ms (car w)) (ss (cdr w)))
932 (if (and (pair? ms) (eq? (car ms) #f))
933 (make-syntax-object
934 (syntax-object-expression x)
935 (cons (cdr ms) (if rib (cons rib (cdr ss)) (cdr ss)))
936 (syntax-object-module x))
937 (make-syntax-object
938 (decorate-source (syntax-object-expression x) s)
939 (cons (cons m ms)
940 (if rib (cons rib (cons 'shift ss)) (cons 'shift ss)))
941 (syntax-object-module x))))))
942 ((vector? x)
943 (let* ((n (vector-length x)) (v (decorate-source (make-vector n) s)))
944 (let loop ((i 0))
945 (if (= i n)
946 (begin (if #f #f) v)
947 (begin
948 (vector-set! v i (rebuild-macro-output (vector-ref x i) m))
949 (loop (+ i 1)))))))
950 ((symbol? x)
951 (syntax-violation
9120f130 952 #f
72ee0ef7
MW
953 "encountered raw symbol in macro output"
954 (source-wrap e w (cdr w) mod)
955 x))
956 (else (decorate-source x s))))))
957 (with-fluids
958 ((transformer-environment (lambda (k) (k e r w s rib mod))))
959 (rebuild-macro-output
960 (p (source-wrap e (anti-mark w) s mod))
961 (gensym (string-append "m-" (session-id) "-")))))))
962 (expand-body
963 (lambda (body outer-form r w mod)
964 (let* ((r (cons '("placeholder" placeholder) r))
965 (ribcage (make-ribcage '() '() '()))
966 (w (cons (car w) (cons ribcage (cdr w)))))
967 (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
968 (ids '())
969 (labels '())
970 (var-ids '())
971 (vars '())
972 (vals '())
973 (bindings '()))
974 (if (null? body)
975 (syntax-violation #f "no expressions in body" outer-form)
976 (let ((e (cdar body)) (er (caar body)))
9120f130
MW
977 (call-with-values
978 (lambda ()
25645a0a 979 (syntax-type e er '(()) (source-annotation e) ribcage mod #f))
72ee0ef7
MW
980 (lambda (type value form e w s mod)
981 (let ((key type))
982 (cond ((memv key '(define-form))
983 (let ((id (wrap value w mod)) (label (gen-label)))
984 (let ((var (gen-var id)))
985 (extend-ribcage! ribcage id label)
986 (parse (cdr body)
987 (cons id ids)
988 (cons label labels)
989 (cons id var-ids)
990 (cons var vars)
991 (cons (cons er (wrap e w mod)) vals)
992 (cons (cons 'lexical var) bindings)))))
993 ((memv key '(define-syntax-form define-syntax-parameter-form))
ceb7f9cc
MW
994 (let ((id (wrap value w mod))
995 (label (gen-label))
996 (trans-r (macros-only-env er)))
72ee0ef7 997 (extend-ribcage! ribcage id label)
ceb7f9cc
MW
998 (set-cdr!
999 r
1000 (extend-env
1001 (list label)
1002 (list (cons 'macro (eval-local-transformer (expand e trans-r w mod) mod)))
1003 (cdr r)))
1004 (parse (cdr body) (cons id ids) labels var-ids vars vals bindings)))
72ee0ef7
MW
1005 ((memv key '(begin-form))
1006 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ . each-any))))
1007 (if tmp
1008 (apply (lambda (e1)
1009 (parse (let f ((forms e1))
1010 (if (null? forms)
1011 (cdr body)
1012 (cons (cons er (wrap (car forms) w mod)) (f (cdr forms)))))
1013 ids
1014 labels
1015 var-ids
1016 vars
1017 vals
1018 bindings))
1019 tmp)
1020 (syntax-violation
1021 #f
1022 "source expression failed to match any pattern"
1023 tmp-1))))
1024 ((memv key '(local-syntax-form))
1025 (expand-local-syntax
1026 value
1027 e
1028 er
1029 w
1030 s
1031 mod
1032 (lambda (forms er w s mod)
1033 (parse (let f ((forms forms))
1034 (if (null? forms)
1035 (cdr body)
1036 (cons (cons er (wrap (car forms) w mod)) (f (cdr forms)))))
1037 ids
1038 labels
1039 var-ids
1040 vars
1041 vals
1042 bindings))))
1043 ((null? ids)
1044 (build-sequence
1045 #f
1046 (map (lambda (x) (expand (cdr x) (car x) '(()) mod))
1047 (cons (cons er (source-wrap e w s mod)) (cdr body)))))
1048 (else
1049 (if (not (valid-bound-ids? ids))
1050 (syntax-violation
1051 #f
1052 "invalid or duplicate identifier in definition"
1053 outer-form))
72ee0ef7
MW
1054 (set-cdr! r (extend-env labels bindings (cdr r)))
1055 (build-letrec
1056 #f
1057 #t
1058 (reverse (map syntax->datum var-ids))
1059 (reverse vars)
1060 (map (lambda (x) (expand (cdr x) (car x) '(()) mod)) (reverse vals))
1061 (build-sequence
1062 #f
1063 (map (lambda (x) (expand (cdr x) (car x) '(()) mod))
1064 (cons (cons er (source-wrap e w s mod)) (cdr body))))))))))))))))
1065 (expand-local-syntax
1066 (lambda (rec? e r w s mod k)
1067 (let* ((tmp e)
1068 (tmp ($sc-dispatch tmp '(_ #(each (any any)) any . each-any))))
1069 (if tmp
1070 (apply (lambda (id val e1 e2)
1071 (let ((ids id))
1072 (if (not (valid-bound-ids? ids))
1073 (syntax-violation #f "duplicate bound keyword" e)
1074 (let* ((labels (gen-labels ids)) (new-w (make-binding-wrap ids labels w)))
1075 (k (cons e1 e2)
1076 (extend-env
1077 labels
1078 (let ((w (if rec? new-w w)) (trans-r (macros-only-env r)))
1079 (map (lambda (x)
1080 (cons 'macro (eval-local-transformer (expand x trans-r w mod) mod)))
1081 val))
1082 r)
1083 new-w
1084 s
1085 mod)))))
1086 tmp)
1087 (syntax-violation
1088 #f
1089 "bad local syntax definition"
1090 (source-wrap e w s mod))))))
1091 (eval-local-transformer
1092 (lambda (expanded mod)
1093 (let ((p (local-eval-hook expanded mod)))
1094 (if (procedure? p)
1095 p
1096 (syntax-violation #f "nonprocedure transformer" p)))))
1097 (expand-void (lambda () (build-void #f)))
1098 (ellipsis?
1099 (lambda (x)
1100 (and (nonsymbol-id? x)
1af6d2a7 1101 (free-id=? x '#(syntax-object ... ((top)) (hygiene guile))))))
72ee0ef7
MW
1102 (lambda-formals
1103 (lambda (orig-args)
1104 (letrec*
1105 ((req (lambda (args rreq)
1106 (let* ((tmp args) (tmp-1 ($sc-dispatch tmp '())))
1107 (if tmp-1
1108 (apply (lambda () (check (reverse rreq) #f)) tmp-1)
1109 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1110 (if (and tmp-1 (apply (lambda (a b) (id? a)) tmp-1))
1111 (apply (lambda (a b) (req b (cons a rreq))) tmp-1)
1112 (let ((tmp-1 (list tmp)))
1113 (if (and tmp-1 (apply (lambda (r) (id? r)) tmp-1))
1114 (apply (lambda (r) (check (reverse rreq) r)) tmp-1)
1115 (let ((else tmp))
1116 (syntax-violation 'lambda "invalid argument list" orig-args args))))))))))
1117 (check (lambda (req rest)
1118 (if (distinct-bound-ids? (if rest (cons rest req) req))
1119 (values req #f rest #f)
9120f130 1120 (syntax-violation
72ee0ef7
MW
1121 'lambda
1122 "duplicate identifier in argument list"
1123 orig-args)))))
1124 (req orig-args '()))))
1125 (expand-simple-lambda
1126 (lambda (e r w s mod req rest meta body)
1127 (let* ((ids (if rest (append req (list rest)) req))
1128 (vars (map gen-var ids))
1129 (labels (gen-labels ids)))
1130 (build-simple-lambda
1131 s
1132 (map syntax->datum req)
1133 (and rest (syntax->datum rest))
1134 vars
1135 meta
1136 (expand-body
1137 body
1138 (source-wrap e w s mod)
1139 (extend-var-env labels vars r)
1140 (make-binding-wrap ids labels w)
1141 mod)))))
1142 (lambda*-formals
1143 (lambda (orig-args)
1144 (letrec*
1145 ((req (lambda (args rreq)
1146 (let* ((tmp args) (tmp-1 ($sc-dispatch tmp '())))
1147 (if tmp-1
1148 (apply (lambda () (check (reverse rreq) '() #f '())) tmp-1)
1149 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1150 (if (and tmp-1 (apply (lambda (a b) (id? a)) tmp-1))
1151 (apply (lambda (a b) (req b (cons a rreq))) tmp-1)
1152 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1153 (if (and tmp-1
1154 (apply (lambda (a b) (eq? (syntax->datum a) #:optional)) tmp-1))
1155 (apply (lambda (a b) (opt b (reverse rreq) '())) tmp-1)
1156 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1157 (if (and tmp-1
1158 (apply (lambda (a b) (eq? (syntax->datum a) #:key)) tmp-1))
1159 (apply (lambda (a b) (key b (reverse rreq) '() '())) tmp-1)
1160 (let ((tmp-1 ($sc-dispatch tmp '(any any))))
1161 (if (and tmp-1
1162 (apply (lambda (a b) (eq? (syntax->datum a) #:rest)) tmp-1))
1163 (apply (lambda (a b) (rest b (reverse rreq) '() '())) tmp-1)
1164 (let ((tmp-1 (list tmp)))
1165 (if (and tmp-1 (apply (lambda (r) (id? r)) tmp-1))
1166 (apply (lambda (r) (rest r (reverse rreq) '() '())) tmp-1)
1167 (let ((else tmp))
1168 (syntax-violation
1169 'lambda*
1170 "invalid argument list"
1171 orig-args
1172 args))))))))))))))))
1173 (opt (lambda (args req ropt)
1174 (let* ((tmp args) (tmp-1 ($sc-dispatch tmp '())))
1175 (if tmp-1
1176 (apply (lambda () (check req (reverse ropt) #f '())) tmp-1)
1177 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1178 (if (and tmp-1 (apply (lambda (a b) (id? a)) tmp-1))
1af6d2a7 1179 (apply (lambda (a b) (opt b req (cons (cons a '(#f)) ropt))) tmp-1)
72ee0ef7
MW
1180 (let ((tmp-1 ($sc-dispatch tmp '((any any) . any))))
1181 (if (and tmp-1 (apply (lambda (a init b) (id? a)) tmp-1))
1182 (apply (lambda (a init b) (opt b req (cons (list a init) ropt)))
1183 tmp-1)
1184 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1185 (if (and tmp-1
1186 (apply (lambda (a b) (eq? (syntax->datum a) #:key)) tmp-1))
1187 (apply (lambda (a b) (key b req (reverse ropt) '())) tmp-1)
1188 (let ((tmp-1 ($sc-dispatch tmp '(any any))))
1189 (if (and tmp-1
1190 (apply (lambda (a b) (eq? (syntax->datum a) #:rest)) tmp-1))
1191 (apply (lambda (a b) (rest b req (reverse ropt) '())) tmp-1)
1192 (let ((tmp-1 (list tmp)))
1193 (if (and tmp-1 (apply (lambda (r) (id? r)) tmp-1))
1194 (apply (lambda (r) (rest r req (reverse ropt) '())) tmp-1)
1195 (let ((else tmp))
1196 (syntax-violation
1197 'lambda*
1198 "invalid optional argument list"
1199 orig-args
1200 args))))))))))))))))
1201 (key (lambda (args req opt rkey)
1202 (let* ((tmp args) (tmp-1 ($sc-dispatch tmp '())))
1203 (if tmp-1
1204 (apply (lambda () (check req opt #f (cons #f (reverse rkey)))) tmp-1)
1205 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1206 (if (and tmp-1 (apply (lambda (a b) (id? a)) tmp-1))
1207 (apply (lambda (a b)
1208 (let* ((tmp (symbol->keyword (syntax->datum a))) (k tmp))
1af6d2a7 1209 (key b req opt (cons (cons k (cons a '(#f))) rkey))))
72ee0ef7
MW
1210 tmp-1)
1211 (let ((tmp-1 ($sc-dispatch tmp '((any any) . any))))
1212 (if (and tmp-1 (apply (lambda (a init b) (id? a)) tmp-1))
1213 (apply (lambda (a init b)
1214 (let* ((tmp (symbol->keyword (syntax->datum a))) (k tmp))
1215 (key b req opt (cons (list k a init) rkey))))
1216 tmp-1)
1217 (let ((tmp-1 ($sc-dispatch tmp '((any any any) . any))))
1218 (if (and tmp-1
1219 (apply (lambda (a init k b) (and (id? a) (keyword? (syntax->datum k))))
1220 tmp-1))
1221 (apply (lambda (a init k b) (key b req opt (cons (list k a init) rkey)))
1222 tmp-1)
1223 (let ((tmp-1 ($sc-dispatch tmp '(any))))
1224 (if (and tmp-1
1225 (apply (lambda (aok) (eq? (syntax->datum aok) #:allow-other-keys))
1226 tmp-1))
1227 (apply (lambda (aok) (check req opt #f (cons #t (reverse rkey))))
1228 tmp-1)
1229 (let ((tmp-1 ($sc-dispatch tmp '(any any any))))
1230 (if (and tmp-1
1231 (apply (lambda (aok a b)
1232 (and (eq? (syntax->datum aok) #:allow-other-keys)
1233 (eq? (syntax->datum a) #:rest)))
1234 tmp-1))
1235 (apply (lambda (aok a b) (rest b req opt (cons #t (reverse rkey))))
1236 tmp-1)
1237 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1238 (if (and tmp-1
1239 (apply (lambda (aok r)
1240 (and (eq? (syntax->datum aok) #:allow-other-keys) (id? r)))
1241 tmp-1))
1242 (apply (lambda (aok r) (rest r req opt (cons #t (reverse rkey))))
1243 tmp-1)
1244 (let ((tmp-1 ($sc-dispatch tmp '(any any))))
1245 (if (and tmp-1
1246 (apply (lambda (a b) (eq? (syntax->datum a) #:rest)) tmp-1))
1247 (apply (lambda (a b) (rest b req opt (cons #f (reverse rkey))))
1248 tmp-1)
1249 (let ((tmp-1 (list tmp)))
1250 (if (and tmp-1 (apply (lambda (r) (id? r)) tmp-1))
1251 (apply (lambda (r) (rest r req opt (cons #f (reverse rkey))))
1252 tmp-1)
1253 (let ((else tmp))
1254 (syntax-violation
1255 'lambda*
1256 "invalid keyword argument list"
1257 orig-args
1258 args))))))))))))))))))))))
1259 (rest (lambda (args req opt kw)
1260 (let* ((tmp-1 args) (tmp (list tmp-1)))
1261 (if (and tmp (apply (lambda (r) (id? r)) tmp))
1262 (apply (lambda (r) (check req opt r kw)) tmp)
1263 (let ((else tmp-1))
1264 (syntax-violation 'lambda* "invalid rest argument" orig-args args))))))
1265 (check (lambda (req opt rest kw)
1266 (if (distinct-bound-ids?
1267 (append
1268 req
1269 (map car opt)
1270 (if rest (list rest) '())
1271 (if (pair? kw) (map cadr (cdr kw)) '())))
1272 (values req opt rest kw)
1273 (syntax-violation
1274 'lambda*
1275 "duplicate identifier in argument list"
1276 orig-args)))))
1277 (req orig-args '()))))
1278 (expand-lambda-case
1279 (lambda (e r w s mod get-formals clauses)
1280 (letrec*
1281 ((parse-req
1282 (lambda (req opt rest kw body)
1283 (let ((vars (map gen-var req)) (labels (gen-labels req)))
1284 (let ((r* (extend-var-env labels vars r))
1285 (w* (make-binding-wrap req labels w)))
1286 (parse-opt
1287 (map syntax->datum req)
1288 opt
1289 rest
1290 kw
1291 body
1292 (reverse vars)
1293 r*
1294 w*
1295 '()
1296 '())))))
1297 (parse-opt
1298 (lambda (req opt rest kw body vars r* w* out inits)
1299 (cond ((pair? opt)
1300 (let* ((tmp-1 (car opt)) (tmp ($sc-dispatch tmp-1 '(any any))))
1301 (if tmp
1302 (apply (lambda (id i)
1303 (let* ((v (gen-var id))
1304 (l (gen-labels (list v)))
1305 (r** (extend-var-env l (list v) r*))
1306 (w** (make-binding-wrap (list id) l w*)))
1307 (parse-opt
1308 req
1309 (cdr opt)
1310 rest
1311 kw
1312 body
1313 (cons v vars)
1314 r**
1315 w**
1316 (cons (syntax->datum id) out)
1317 (cons (expand i r* w* mod) inits))))
1318 tmp)
1319 (syntax-violation
9120f130 1320 #f
72ee0ef7
MW
1321 "source expression failed to match any pattern"
1322 tmp-1))))
1323 (rest
1324 (let* ((v (gen-var rest))
1325 (l (gen-labels (list v)))
1326 (r* (extend-var-env l (list v) r*))
1327 (w* (make-binding-wrap (list rest) l w*)))
1328 (parse-kw
1329 req
1330 (and (pair? out) (reverse out))
1331 (syntax->datum rest)
1332 (if (pair? kw) (cdr kw) kw)
1333 body
1334 (cons v vars)
1335 r*
1336 w*
1337 (and (pair? kw) (car kw))
1338 '()
1339 inits)))
1340 (else
1341 (parse-kw
1342 req
1343 (and (pair? out) (reverse out))
1344 #f
1345 (if (pair? kw) (cdr kw) kw)
1346 body
1347 vars
1348 r*
1349 w*
1350 (and (pair? kw) (car kw))
1351 '()
1352 inits)))))
1353 (parse-kw
1354 (lambda (req opt rest kw body vars r* w* aok out inits)
1355 (if (pair? kw)
1356 (let* ((tmp-1 (car kw)) (tmp ($sc-dispatch tmp-1 '(any any any))))
1357 (if tmp
1358 (apply (lambda (k id i)
1359 (let* ((v (gen-var id))
1360 (l (gen-labels (list v)))
1361 (r** (extend-var-env l (list v) r*))
1362 (w** (make-binding-wrap (list id) l w*)))
1363 (parse-kw
1364 req
1365 opt
1366 rest
1367 (cdr kw)
1368 body
1369 (cons v vars)
1370 r**
1371 w**
1372 aok
1373 (cons (list (syntax->datum k) (syntax->datum id) v) out)
1374 (cons (expand i r* w* mod) inits))))
1375 tmp)
1376 (syntax-violation
1377 #f
1378 "source expression failed to match any pattern"
1379 tmp-1)))
1380 (parse-body
1381 req
1382 opt
1383 rest
1384 (and (or aok (pair? out)) (cons aok (reverse out)))
1385 body
1386 (reverse vars)
1387 r*
1388 w*
1389 (reverse inits)
1390 '()))))
1391 (parse-body
1392 (lambda (req opt rest kw body vars r* w* inits meta)
1393 (let* ((tmp body) (tmp-1 ($sc-dispatch tmp '(any any . each-any))))
1394 (if (and tmp-1
1395 (apply (lambda (docstring e1 e2) (string? (syntax->datum docstring)))
1396 tmp-1))
1397 (apply (lambda (docstring e1 e2)
1398 (parse-body
1399 req
1400 opt
1401 rest
1402 kw
1403 (cons e1 e2)
1404 vars
1405 r*
1406 w*
1407 inits
1408 (append meta (list (cons 'documentation (syntax->datum docstring))))))
1409 tmp-1)
1410 (let ((tmp-1 ($sc-dispatch tmp '(#(vector #(each (any . any))) any . each-any))))
1411 (if tmp-1
1412 (apply (lambda (k v e1 e2)
1413 (parse-body
1414 req
1415 opt
1416 rest
1417 kw
1418 (cons e1 e2)
1419 vars
1420 r*
1421 w*
1422 inits
1423 (append meta (syntax->datum (map cons k v)))))
1424 tmp-1)
1425 (let ((tmp-1 ($sc-dispatch tmp '(any . each-any))))
1426 (if tmp-1
1427 (apply (lambda (e1 e2)
1428 (values
1429 meta
1430 req
1431 opt
1432 rest
1433 kw
1434 inits
1435 vars
1436 (expand-body (cons e1 e2) (source-wrap e w s mod) r* w* mod)))
1437 tmp-1)
1438 (syntax-violation
1439 #f
1440 "source expression failed to match any pattern"
1441 tmp))))))))))
1442 (let* ((tmp clauses) (tmp-1 ($sc-dispatch tmp '())))
1443 (if tmp-1
1444 (apply (lambda () (values '() #f)) tmp-1)
1445 (let ((tmp-1 ($sc-dispatch
1446 tmp
1447 '((any any . each-any) . #(each (any any . each-any))))))
1448 (if tmp-1
1449 (apply (lambda (args e1 e2 args* e1* e2*)
1450 (call-with-values
1451 (lambda () (get-formals args))
1452 (lambda (req opt rest kw)
1453 (call-with-values
1454 (lambda () (parse-req req opt rest kw (cons e1 e2)))
1455 (lambda (meta req opt rest kw inits vars body)
1456 (call-with-values
1457 (lambda ()
1458 (expand-lambda-case
1459 e
1460 r
1461 w
1462 s
1463 mod
1464 get-formals
1465 (map (lambda (tmp-2 tmp-1 tmp) (cons tmp (cons tmp-1 tmp-2)))
1466 e2*
1467 e1*
1468 args*)))
1469 (lambda (meta* else*)
1470 (values
1471 (append meta meta*)
1472 (build-lambda-case s req opt rest kw inits vars body else*)))))))))
1473 tmp-1)
1474 (syntax-violation
1475 #f
1476 "source expression failed to match any pattern"
1477 tmp))))))))
1478 (strip (lambda (x w)
1479 (if (memq 'top (car w))
1480 x
1481 (let f ((x x))
1482 (cond ((syntax-object? x)
1483 (strip (syntax-object-expression x) (syntax-object-wrap x)))
1484 ((pair? x)
1485 (let ((a (f (car x))) (d (f (cdr x))))
1486 (if (and (eq? a (car x)) (eq? d (cdr x))) x (cons a d))))
1487 ((vector? x)
1488 (let* ((old (vector->list x)) (new (map f old)))
1489 (let lp ((l1 old) (l2 new))
1490 (cond ((null? l1) x)
1491 ((eq? (car l1) (car l2)) (lp (cdr l1) (cdr l2)))
1492 (else (list->vector new))))))
1493 (else x))))))
1494 (gen-var
1495 (lambda (id)
1496 (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
1497 (gensym (string-append (symbol->string id) "-")))))
1498 (lambda-var-list
1499 (lambda (vars)
1500 (let lvl ((vars vars) (ls '()) (w '(())))
1501 (cond ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
1502 ((id? vars) (cons (wrap vars w #f) ls))
1503 ((null? vars) ls)
1504 ((syntax-object? vars)
1505 (lvl (syntax-object-expression vars)
1506 ls
1507 (join-wraps w (syntax-object-wrap vars))))
1508 (else (cons vars ls)))))))
1509 (global-extend 'local-syntax 'letrec-syntax #t)
1510 (global-extend 'local-syntax 'let-syntax #f)
1511 (global-extend
1512 'core
1513 'syntax-parameterize
1514 (lambda (e r w s mod)
1515 (let* ((tmp e)
1516 (tmp ($sc-dispatch tmp '(_ #(each (any any)) any . each-any))))
1517 (if (and tmp (apply (lambda (var val e1 e2) (valid-bound-ids? var)) tmp))
1518 (apply (lambda (var val e1 e2)
1519 (let ((names (map (lambda (x) (id-var-name x w)) var)))
1520 (for-each
1521 (lambda (id n)
1522 (let ((key (car (lookup n r mod))))
1523 (if (memv key '(displaced-lexical))
1524 (syntax-violation
1525 'syntax-parameterize
1526 "identifier out of context"
1527 e
1528 (source-wrap id w s mod)))))
1529 var
1530 names)
1531 (expand-body
1532 (cons e1 e2)
1533 (source-wrap e w s mod)
1534 (extend-env
1535 names
1536 (let ((trans-r (macros-only-env r)))
1537 (map (lambda (x)
1538 (cons 'macro (eval-local-transformer (expand x trans-r w mod) mod)))
1539 val))
1540 r)
1541 w
1542 mod)))
1543 tmp)
1544 (syntax-violation
1545 'syntax-parameterize
1546 "bad syntax"
1547 (source-wrap e w s mod))))))
1548 (global-extend
1549 'core
1550 'quote
1551 (lambda (e r w s mod)
1552 (let* ((tmp e) (tmp ($sc-dispatch tmp '(_ any))))
1553 (if tmp
1554 (apply (lambda (e) (build-data s (strip e w))) tmp)
1555 (syntax-violation 'quote "bad syntax" (source-wrap e w s mod))))))
1556 (global-extend
1557 'core
1558 'syntax
1559 (letrec*
1560 ((gen-syntax
1561 (lambda (src e r maps ellipsis? mod)
1562 (if (id? e)
1563 (let* ((label (id-var-name e '(()))) (b (lookup label r mod)))
1564 (cond ((eq? (car b) 'syntax)
1565 (call-with-values
1566 (lambda ()
1567 (let ((var.lev (cdr b)))
1568 (gen-ref src (car var.lev) (cdr var.lev) maps)))
1569 (lambda (var maps) (values (list 'ref var) maps))))
1570 ((ellipsis? e) (syntax-violation 'syntax "misplaced ellipsis" src))
1571 (else (values (list 'quote e) maps))))
1572 (let* ((tmp e) (tmp-1 ($sc-dispatch tmp '(any any))))
1573 (if (and tmp-1 (apply (lambda (dots e) (ellipsis? dots)) tmp-1))
1574 (apply (lambda (dots e) (gen-syntax src e r maps (lambda (x) #f) mod))
1575 tmp-1)
1576 (let ((tmp-1 ($sc-dispatch tmp '(any any . any))))
1577 (if (and tmp-1 (apply (lambda (x dots y) (ellipsis? dots)) tmp-1))
1578 (apply (lambda (x dots y)
1579 (let f ((y y)
1580 (k (lambda (maps)
1581 (call-with-values
1582 (lambda () (gen-syntax src x r (cons '() maps) ellipsis? mod))
1583 (lambda (x maps)
1584 (if (null? (car maps))
1585 (syntax-violation 'syntax "extra ellipsis" src)
1586 (values (gen-map x (car maps)) (cdr maps))))))))
1587 (let* ((tmp y) (tmp ($sc-dispatch tmp '(any . any))))
1588 (if (and tmp (apply (lambda (dots y) (ellipsis? dots)) tmp))
1589 (apply (lambda (dots y)
1590 (f y
1591 (lambda (maps)
1592 (call-with-values
1593 (lambda () (k (cons '() maps)))
1594 (lambda (x maps)
1595 (if (null? (car maps))
1596 (syntax-violation 'syntax "extra ellipsis" src)
1597 (values (gen-mappend x (car maps)) (cdr maps))))))))
1598 tmp)
1599 (call-with-values
1600 (lambda () (gen-syntax src y r maps ellipsis? mod))
1601 (lambda (y maps)
1602 (call-with-values
1603 (lambda () (k maps))
1604 (lambda (x maps) (values (gen-append x y) maps)))))))))
1605 tmp-1)
1606 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1607 (if tmp-1
1608 (apply (lambda (x y)
1609 (call-with-values
1610 (lambda () (gen-syntax src x r maps ellipsis? mod))
1611 (lambda (x maps)
1612 (call-with-values
1613 (lambda () (gen-syntax src y r maps ellipsis? mod))
1614 (lambda (y maps) (values (gen-cons x y) maps))))))
1615 tmp-1)
1616 (let ((tmp ($sc-dispatch tmp '#(vector (any . each-any)))))
1617 (if tmp
1618 (apply (lambda (e1 e2)
1619 (call-with-values
1620 (lambda () (gen-syntax src (cons e1 e2) r maps ellipsis? mod))
1621 (lambda (e maps) (values (gen-vector e) maps))))
1622 tmp)
1623 (values (list 'quote e) maps))))))))))))
1624 (gen-ref
1625 (lambda (src var level maps)
1626 (cond ((= level 0) (values var maps))
1627 ((null? maps) (syntax-violation 'syntax "missing ellipsis" src))
1628 (else
1629 (call-with-values
1630 (lambda () (gen-ref src var (- level 1) (cdr maps)))
1631 (lambda (outer-var outer-maps)
1632 (let ((b (assq outer-var (car maps))))
1633 (if b
1634 (values (cdr b) maps)
1635 (let ((inner-var (gen-var 'tmp)))
1636 (values
1637 inner-var
1638 (cons (cons (cons outer-var inner-var) (car maps)) outer-maps)))))))))))
1639 (gen-mappend
1640 (lambda (e map-env)
1641 (list 'apply '(primitive append) (gen-map e map-env))))
1642 (gen-map
1643 (lambda (e map-env)
1644 (let ((formals (map cdr map-env))
1645 (actuals (map (lambda (x) (list 'ref (car x))) map-env)))
1646 (cond ((eq? (car e) 'ref) (car actuals))
1647 ((and-map
1648 (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
1649 (cdr e))
1650 (cons 'map
1651 (cons (list 'primitive (car e))
1652 (map (let ((r (map cons formals actuals)))
1653 (lambda (x) (cdr (assq (cadr x) r))))
1654 (cdr e)))))
1655 (else (cons 'map (cons (list 'lambda formals e) actuals)))))))
1656 (gen-cons
1657 (lambda (x y)
1658 (let ((key (car y)))
1659 (cond ((memv key '(quote))
1660 (cond ((eq? (car x) 'quote) (list 'quote (cons (cadr x) (cadr y))))
1661 ((eq? (cadr y) '()) (list 'list x))
1662 (else (list 'cons x y))))
1663 ((memv key '(list)) (cons 'list (cons x (cdr y))))
1664 (else (list 'cons x y))))))
1665 (gen-append (lambda (x y) (if (equal? y ''()) x (list 'append x y))))
1666 (gen-vector
1667 (lambda (x)
1668 (cond ((eq? (car x) 'list) (cons 'vector (cdr x)))
1669 ((eq? (car x) 'quote) (list 'quote (list->vector (cadr x))))
1670 (else (list 'list->vector x)))))
1671 (regen (lambda (x)
1672 (let ((key (car x)))
1673 (cond ((memv key '(ref))
1674 (build-lexical-reference 'value #f (cadr x) (cadr x)))
1675 ((memv key '(primitive)) (build-primref #f (cadr x)))
1676 ((memv key '(quote)) (build-data #f (cadr x)))
1677 ((memv key '(lambda))
1678 (if (list? (cadr x))
1679 (build-simple-lambda #f (cadr x) #f (cadr x) '() (regen (caddr x)))
1680 (error "how did we get here" x)))
1681 (else
1682 (build-application #f (build-primref #f (car x)) (map regen (cdr x)))))))))
1683 (lambda (e r w s mod)
1684 (let* ((e (source-wrap e w s mod))
1685 (tmp e)
1686 (tmp ($sc-dispatch tmp '(_ any))))
1687 (if tmp
1688 (apply (lambda (x)
1689 (call-with-values
1690 (lambda () (gen-syntax e x r '() ellipsis? mod))
1691 (lambda (e maps) (regen e))))
1692 tmp)
1693 (syntax-violation 'syntax "bad `syntax' form" e))))))
1694 (global-extend
1695 'core
1696 'lambda
1697 (lambda (e r w s mod)
1698 (let* ((tmp e) (tmp ($sc-dispatch tmp '(_ any any . each-any))))
1699 (if tmp
1700 (apply (lambda (args e1 e2)
1701 (call-with-values
1702 (lambda () (lambda-formals args))
1703 (lambda (req opt rest kw)
1704 (let lp ((body (cons e1 e2)) (meta '()))
1705 (let* ((tmp-1 body) (tmp ($sc-dispatch tmp-1 '(any any . each-any))))
1706 (if (and tmp
1707 (apply (lambda (docstring e1 e2) (string? (syntax->datum docstring)))
1708 tmp))
1709 (apply (lambda (docstring e1 e2)
1710 (lp (cons e1 e2)
1711 (append meta (list (cons 'documentation (syntax->datum docstring))))))
1712 tmp)
1713 (let ((tmp ($sc-dispatch tmp-1 '(#(vector #(each (any . any))) any . each-any))))
1714 (if tmp
1715 (apply (lambda (k v e1 e2)
1716 (lp (cons e1 e2) (append meta (syntax->datum (map cons k v)))))
1717 tmp)
1718 (expand-simple-lambda e r w s mod req rest meta body)))))))))
1719 tmp)
1720 (syntax-violation 'lambda "bad lambda" e)))))
1721 (global-extend
1722 'core
1723 'lambda*
1724 (lambda (e r w s mod)
1725 (let* ((tmp e) (tmp ($sc-dispatch tmp '(_ any any . each-any))))
1726 (if tmp
1727 (apply (lambda (args e1 e2)
1728 (call-with-values
1729 (lambda ()
1730 (expand-lambda-case
1731 e
1732 r
1733 w
1734 s
1735 mod
1736 lambda*-formals
1737 (list (cons args (cons e1 e2)))))
1738 (lambda (meta lcase) (build-case-lambda s meta lcase))))
1739 tmp)
1740 (syntax-violation 'lambda "bad lambda*" e)))))
1741 (global-extend
1742 'core
1743 'case-lambda
1744 (lambda (e r w s mod)
1745 (let* ((tmp e)
1746 (tmp ($sc-dispatch
1747 tmp
1748 '(_ (any any . each-any) . #(each (any any . each-any))))))
1749 (if tmp
1750 (apply (lambda (args e1 e2 args* e1* e2*)
1751 (call-with-values
1752 (lambda ()
1753 (expand-lambda-case
1754 e
1755 r
1756 w
1757 s
1758 mod
1759 lambda-formals
1760 (cons (cons args (cons e1 e2))
1761 (map (lambda (tmp-2 tmp-1 tmp) (cons tmp (cons tmp-1 tmp-2)))
1762 e2*
1763 e1*
1764 args*))))
1765 (lambda (meta lcase) (build-case-lambda s meta lcase))))
1766 tmp)
1767 (syntax-violation 'case-lambda "bad case-lambda" e)))))
1768 (global-extend
1769 'core
1770 'case-lambda*
1771 (lambda (e r w s mod)
1772 (let* ((tmp e)
1773 (tmp ($sc-dispatch
1774 tmp
1775 '(_ (any any . each-any) . #(each (any any . each-any))))))
1776 (if tmp
1777 (apply (lambda (args e1 e2 args* e1* e2*)
1778 (call-with-values
1779 (lambda ()
1780 (expand-lambda-case
1781 e
1782 r
1783 w
1784 s
1785 mod
1786 lambda*-formals
1787 (cons (cons args (cons e1 e2))
1788 (map (lambda (tmp-2 tmp-1 tmp) (cons tmp (cons tmp-1 tmp-2)))
1789 e2*
1790 e1*
1791 args*))))
1792 (lambda (meta lcase) (build-case-lambda s meta lcase))))
1793 tmp)
1794 (syntax-violation 'case-lambda "bad case-lambda*" e)))))
1795 (global-extend
1796 'core
1797 'let
1798 (letrec*
1799 ((expand-let
1800 (lambda (e r w s mod constructor ids vals exps)
1801 (if (not (valid-bound-ids? ids))
1802 (syntax-violation 'let "duplicate bound variable" e)
1803 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
1804 (let ((nw (make-binding-wrap ids labels w))
1805 (nr (extend-var-env labels new-vars r)))
1806 (constructor
1807 s
1808 (map syntax->datum ids)
1809 new-vars
1810 (map (lambda (x) (expand x r w mod)) vals)
1811 (expand-body exps (source-wrap e nw s mod) nr nw mod))))))))
1812 (lambda (e r w s mod)
1813 (let* ((tmp-1 e)
1814 (tmp ($sc-dispatch tmp-1 '(_ #(each (any any)) any . each-any))))
1815 (if (and tmp (apply (lambda (id val e1 e2) (and-map id? id)) tmp))
1816 (apply (lambda (id val e1 e2)
1817 (expand-let e r w s mod build-let id val (cons e1 e2)))
1818 tmp)
1819 (let ((tmp ($sc-dispatch tmp-1 '(_ any #(each (any any)) any . each-any))))
1820 (if (and tmp
1821 (apply (lambda (f id val e1 e2) (and (id? f) (and-map id? id))) tmp))
1822 (apply (lambda (f id val e1 e2)
1823 (expand-let e r w s mod build-named-let (cons f id) val (cons e1 e2)))
1824 tmp)
1825 (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))))
1826 (global-extend
1827 'core
1828 'letrec
1829 (lambda (e r w s mod)
1830 (let* ((tmp e)
1831 (tmp ($sc-dispatch tmp '(_ #(each (any any)) any . each-any))))
1832 (if (and tmp (apply (lambda (id val e1 e2) (and-map id? id)) tmp))
1833 (apply (lambda (id val e1 e2)
1834 (let ((ids id))
1835 (if (not (valid-bound-ids? ids))
1836 (syntax-violation 'letrec "duplicate bound variable" e)
1837 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
1838 (let ((w (make-binding-wrap ids labels w))
1839 (r (extend-var-env labels new-vars r)))
1840 (build-letrec
1841 s
9120f130 1842 #f
72ee0ef7
MW
1843 (map syntax->datum ids)
1844 new-vars
1845 (map (lambda (x) (expand x r w mod)) val)
1846 (expand-body (cons e1 e2) (source-wrap e w s mod) r w mod)))))))
1847 tmp)
1848 (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
1849 (global-extend
1850 'core
1851 'letrec*
1852 (lambda (e r w s mod)
1853 (let* ((tmp e)
1854 (tmp ($sc-dispatch tmp '(_ #(each (any any)) any . each-any))))
1855 (if (and tmp (apply (lambda (id val e1 e2) (and-map id? id)) tmp))
1856 (apply (lambda (id val e1 e2)
1857 (let ((ids id))
1858 (if (not (valid-bound-ids? ids))
1859 (syntax-violation 'letrec* "duplicate bound variable" e)
1860 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
1861 (let ((w (make-binding-wrap ids labels w))
1862 (r (extend-var-env labels new-vars r)))
1863 (build-letrec
1864 s
1865 #t
1866 (map syntax->datum ids)
1867 new-vars
1868 (map (lambda (x) (expand x r w mod)) val)
1869 (expand-body (cons e1 e2) (source-wrap e w s mod) r w mod)))))))
1870 tmp)
1871 (syntax-violation 'letrec* "bad letrec*" (source-wrap e w s mod))))))
1872 (global-extend
1873 'core
1874 'set!
1875 (lambda (e r w s mod)
1876 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ any any))))
1877 (if (and tmp (apply (lambda (id val) (id? id)) tmp))
1878 (apply (lambda (id val)
1879 (let ((n (id-var-name id w))
1880 (id-mod (if (syntax-object? id) (syntax-object-module id) mod)))
1881 (let* ((b (lookup n r id-mod)) (key (car b)))
1882 (cond ((memv key '(lexical))
1883 (build-lexical-assignment
1884 s
1885 (syntax->datum id)
1886 (cdr b)
1887 (expand val r w mod)))
1888 ((memv key '(global))
1889 (build-global-assignment s n (expand val r w mod) id-mod))
1890 ((memv key '(macro))
1891 (let ((p (cdr b)))
1892 (if (procedure-property p 'variable-transformer)
1893 (expand (expand-macro p e r w s #f mod) r '(()) mod)
1894 (syntax-violation
1895 'set!
1896 "not a variable transformer"
1897 (wrap e w mod)
1898 (wrap id w id-mod)))))
1899 ((memv key '(displaced-lexical))
1900 (syntax-violation 'set! "identifier out of context" (wrap id w mod)))
1901 (else (syntax-violation 'set! "bad set!" (source-wrap e w s mod)))))))
1902 tmp)
1903 (let ((tmp ($sc-dispatch tmp-1 '(_ (any . each-any) any))))
1904 (if tmp
1905 (apply (lambda (head tail val)
1906 (call-with-values
1907 (lambda () (syntax-type head r '(()) #f #f mod #t))
1908 (lambda (type value formform ee ww ss modmod)
1909 (let ((key type))
1910 (if (memv key '(module-ref))
1911 (let ((val (expand val r w mod)))
1912 (call-with-values
1913 (lambda () (value (cons head tail) r w))
1914 (lambda (e r w s* mod)
1915 (let* ((tmp-1 e) (tmp (list tmp-1)))
1916 (if (and tmp (apply (lambda (e) (id? e)) tmp))
1917 (apply (lambda (e) (build-global-assignment s (syntax->datum e) val mod))
1918 tmp)
1919 (syntax-violation
1920 #f
1921 "source expression failed to match any pattern"
1922 tmp-1))))))
1923 (build-application
1924 s
1925 (expand
1af6d2a7 1926 (list '#(syntax-object setter ((top)) (hygiene guile)) head)
72ee0ef7
MW
1927 r
1928 w
1929 mod)
1930 (map (lambda (e) (expand e r w mod)) (append tail (list val)))))))))
1931 tmp)
1932 (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))))
1933 (global-extend
1934 'module-ref
1935 '@
1936 (lambda (e r w)
1937 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ each-any any))))
1938 (if (and tmp
1939 (apply (lambda (mod id) (and (and-map id? mod) (id? id))) tmp))
1940 (apply (lambda (mod id)
1941 (values
1942 (syntax->datum id)
1943 r
8210c853 1944 '((top))
72ee0ef7
MW
1945 #f
1946 (syntax->datum
1af6d2a7 1947 (cons '#(syntax-object public ((top)) (hygiene guile)) mod))))
72ee0ef7 1948 tmp)
014de9e2
AW
1949 (syntax-violation
1950 #f
1951 "source expression failed to match any pattern"
72ee0ef7
MW
1952 tmp-1)))))
1953 (global-extend
1954 'module-ref
1955 '@@
1956 (lambda (e r w)
1957 (letrec*
1958 ((remodulate
1959 (lambda (x mod)
1960 (cond ((pair? x) (cons (remodulate (car x) mod) (remodulate (cdr x) mod)))
1961 ((syntax-object? x)
1962 (make-syntax-object
1963 (remodulate (syntax-object-expression x) mod)
1964 (syntax-object-wrap x)
1965 mod))
1966 ((vector? x)
1967 (let* ((n (vector-length x)) (v (make-vector n)))
1968 (let loop ((i 0))
1969 (if (= i n)
1970 (begin (if #f #f) v)
1971 (begin
1972 (vector-set! v i (remodulate (vector-ref x i) mod))
1973 (loop (+ i 1)))))))
1974 (else x)))))
1975 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ each-any any))))
8210c853
MW
1976 (if (and tmp
1977 (apply (lambda (mod id) (and (and-map id? mod) (id? id))) tmp))
1978 (apply (lambda (mod id)
1979 (values
1980 (syntax->datum id)
1981 r
1982 '((top))
1983 #f
1984 (syntax->datum
1985 (cons '#(syntax-object private ((top)) (hygiene guile)) mod))))
72ee0ef7 1986 tmp)
8210c853
MW
1987 (let ((tmp ($sc-dispatch
1988 tmp-1
1989 '(_ #(free-id #(syntax-object @@ ((top)) (hygiene guile)))
1990 each-any
1991 any))))
1992 (if (and tmp (apply (lambda (mod exp) (and-map id? mod)) tmp))
1993 (apply (lambda (mod exp)
1994 (let ((mod (syntax->datum
1995 (cons '#(syntax-object private ((top)) (hygiene guile)) mod))))
1996 (values (remodulate exp mod) r w (source-annotation exp) mod)))
1997 tmp)
1998 (syntax-violation
1999 #f
2000 "source expression failed to match any pattern"
2001 tmp-1))))))))
72ee0ef7
MW
2002 (global-extend
2003 'core
2004 'if
2005 (lambda (e r w s mod)
2006 (let* ((tmp e) (tmp-1 ($sc-dispatch tmp '(_ any any))))
2007 (if tmp-1
2008 (apply (lambda (test then)
2009 (build-conditional
2010 s
2011 (expand test r w mod)
2012 (expand then r w mod)
2013 (build-void #f)))
2014 tmp-1)
2015 (let ((tmp-1 ($sc-dispatch tmp '(_ any any any))))
2016 (if tmp-1
2017 (apply (lambda (test then else)
2018 (build-conditional
2019 s
2020 (expand test r w mod)
2021 (expand then r w mod)
2022 (expand else r w mod)))
2023 tmp-1)
2024 (syntax-violation
2025 #f
2026 "source expression failed to match any pattern"
2027 tmp)))))))
2028 (global-extend
2029 'core
2030 'with-fluids
2031 (lambda (e r w s mod)
2032 (let* ((tmp-1 e)
2033 (tmp ($sc-dispatch tmp-1 '(_ #(each (any any)) any . each-any))))
2034 (if tmp
2035 (apply (lambda (fluid val b b*)
2036 (build-dynlet
2037 s
2038 (map (lambda (x) (expand x r w mod)) fluid)
2039 (map (lambda (x) (expand x r w mod)) val)
2040 (expand-body (cons b b*) (source-wrap e w s mod) r w mod)))
2041 tmp)
2042 (syntax-violation
2043 #f
2044 "source expression failed to match any pattern"
2045 tmp-1)))))
2046 (global-extend 'begin 'begin '())
2047 (global-extend 'define 'define '())
2048 (global-extend 'define-syntax 'define-syntax '())
2049 (global-extend 'define-syntax-parameter 'define-syntax-parameter '())
2050 (global-extend 'eval-when 'eval-when '())
2051 (global-extend
2052 'core
2053 'syntax-case
2054 (letrec*
2055 ((convert-pattern
2056 (lambda (pattern keys)
2057 (letrec*
2058 ((cvt* (lambda (p* n ids)
2059 (if (not (pair? p*))
2060 (cvt p* n ids)
2061 (call-with-values
2062 (lambda () (cvt* (cdr p*) n ids))
2063 (lambda (y ids)
2064 (call-with-values
2065 (lambda () (cvt (car p*) n ids))
2066 (lambda (x ids) (values (cons x y) ids))))))))
2067 (v-reverse
2068 (lambda (x)
2069 (let loop ((r '()) (x x))
2070 (if (not (pair? x)) (values r x) (loop (cons (car x) r) (cdr x))))))
2071 (cvt (lambda (p n ids)
2072 (if (id? p)
2073 (cond ((bound-id-member? p keys) (values (vector 'free-id p) ids))
1af6d2a7 2074 ((free-id=? p '#(syntax-object _ ((top)) (hygiene guile)))
72ee0ef7
MW
2075 (values '_ ids))
2076 (else (values 'any (cons (cons p n) ids))))
2077 (let* ((tmp p) (tmp-1 ($sc-dispatch tmp '(any any))))
2078 (if (and tmp-1 (apply (lambda (x dots) (ellipsis? dots)) tmp-1))
2079 (apply (lambda (x dots)
2080 (call-with-values
2081 (lambda () (cvt x (+ n 1) ids))
2082 (lambda (p ids)
2083 (values (if (eq? p 'any) 'each-any (vector 'each p)) ids))))
2084 tmp-1)
2085 (let ((tmp-1 ($sc-dispatch tmp '(any any . any))))
2086 (if (and tmp-1 (apply (lambda (x dots ys) (ellipsis? dots)) tmp-1))
2087 (apply (lambda (x dots ys)
2088 (call-with-values
2089 (lambda () (cvt* ys n ids))
2090 (lambda (ys ids)
2091 (call-with-values
2092 (lambda () (cvt x (+ n 1) ids))
2093 (lambda (x ids)
2094 (call-with-values
2095 (lambda () (v-reverse ys))
2096 (lambda (ys e) (values (vector 'each+ x ys e) ids))))))))
2097 tmp-1)
2098 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
2099 (if tmp-1
2100 (apply (lambda (x y)
2101 (call-with-values
2102 (lambda () (cvt y n ids))
2103 (lambda (y ids)
2104 (call-with-values
2105 (lambda () (cvt x n ids))
2106 (lambda (x ids) (values (cons x y) ids))))))
2107 tmp-1)
2108 (let ((tmp-1 ($sc-dispatch tmp '())))
2109 (if tmp-1
2110 (apply (lambda () (values '() ids)) tmp-1)
2111 (let ((tmp-1 ($sc-dispatch tmp '#(vector each-any))))
2112 (if tmp-1
2113 (apply (lambda (x)
2114 (call-with-values
2115 (lambda () (cvt x n ids))
2116 (lambda (p ids) (values (vector 'vector p) ids))))
2117 tmp-1)
2118 (let ((x tmp)) (values (vector 'atom (strip p '(()))) ids))))))))))))))))
2119 (cvt pattern 0 '()))))
2120 (build-dispatch-call
2121 (lambda (pvars exp y r mod)
2122 (let ((ids (map car pvars)) (levels (map cdr pvars)))
2123 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
2124 (build-application
2125 #f
2126 (build-primref #f 'apply)
2127 (list (build-simple-lambda
2128 #f
2129 (map syntax->datum ids)
2130 #f
2131 new-vars
2132 '()
2133 (expand
2134 exp
2135 (extend-env
2136 labels
2137 (map (lambda (var level) (cons 'syntax (cons var level)))
2138 new-vars
2139 (map cdr pvars))
2140 r)
2141 (make-binding-wrap ids labels '(()))
2142 mod))
2143 y))))))
2144 (gen-clause
2145 (lambda (x keys clauses r pat fender exp mod)
2146 (call-with-values
2147 (lambda () (convert-pattern pat keys))
2148 (lambda (p pvars)
2149 (cond ((not (distinct-bound-ids? (map car pvars)))
2150 (syntax-violation 'syntax-case "duplicate pattern variable" pat))
2151 ((not (and-map (lambda (x) (not (ellipsis? (car x)))) pvars))
2152 (syntax-violation 'syntax-case "misplaced ellipsis" pat))
2153 (else
2154 (let ((y (gen-var 'tmp)))
2155 (build-application
2156 #f
2157 (build-simple-lambda
2158 #f
2159 (list 'tmp)
2160 #f
2161 (list y)
2162 '()
2163 (let ((y (build-lexical-reference 'value #f 'tmp y)))
2164 (build-conditional
2165 #f
2166 (let* ((tmp fender) (tmp ($sc-dispatch tmp '#(atom #t))))
2167 (if tmp
2168 (apply (lambda () y) tmp)
2169 (build-conditional
2170 #f
2171 y
2172 (build-dispatch-call pvars fender y r mod)
2173 (build-data #f #f))))
2174 (build-dispatch-call pvars exp y r mod)
2175 (gen-syntax-case x keys clauses r mod))))
2176 (list (if (eq? p 'any)
2177 (build-application #f (build-primref #f 'list) (list x))
2178 (build-application
2179 #f
2180 (build-primref #f '$sc-dispatch)
2181 (list x (build-data #f p)))))))))))))
2182 (gen-syntax-case
2183 (lambda (x keys clauses r mod)
2184 (if (null? clauses)
2185 (build-application
2186 #f
2187 (build-primref #f 'syntax-violation)
2188 (list (build-data #f #f)
2189 (build-data #f "source expression failed to match any pattern")
2190 x))
2191 (let* ((tmp-1 (car clauses)) (tmp ($sc-dispatch tmp-1 '(any any))))
2192 (if tmp
2193 (apply (lambda (pat exp)
2194 (if (and (id? pat)
2195 (and-map
2196 (lambda (x) (not (free-id=? pat x)))
1af6d2a7
MW
2197 (cons '#(syntax-object ... ((top)) (hygiene guile)) keys)))
2198 (if (free-id=? pat '#(syntax-object _ ((top)) (hygiene guile)))
72ee0ef7
MW
2199 (expand exp r '(()) mod)
2200 (let ((labels (list (gen-label))) (var (gen-var pat)))
2201 (build-application
2202 #f
2203 (build-simple-lambda
2204 #f
2205 (list (syntax->datum pat))
2206 #f
2207 (list var)
2208 '()
2209 (expand
2210 exp
2211 (extend-env labels (list (cons 'syntax (cons var 0))) r)
2212 (make-binding-wrap (list pat) labels '(()))
2213 mod))
2214 (list x))))
2215 (gen-clause x keys (cdr clauses) r pat #t exp mod)))
2216 tmp)
2217 (let ((tmp ($sc-dispatch tmp-1 '(any any any))))
2218 (if tmp
2219 (apply (lambda (pat fender exp)
2220 (gen-clause x keys (cdr clauses) r pat fender exp mod))
2221 tmp)
2222 (syntax-violation 'syntax-case "invalid clause" (car clauses))))))))))
2223 (lambda (e r w s mod)
2224 (let* ((e (source-wrap e w s mod))
2225 (tmp-1 e)
2226 (tmp ($sc-dispatch tmp-1 '(_ any each-any . each-any))))
2227 (if tmp
2228 (apply (lambda (val key m)
2229 (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x)))) key)
2230 (let ((x (gen-var 'tmp)))
2231 (build-application
2232 s
2233 (build-simple-lambda
2234 #f
2235 (list 'tmp)
2236 #f
2237 (list x)
2238 '()
2239 (gen-syntax-case
2240 (build-lexical-reference 'value #f 'tmp x)
2241 key
2242 m
2243 r
2244 mod))
2245 (list (expand val r '(()) mod))))
2246 (syntax-violation 'syntax-case "invalid literals list" e)))
2247 tmp)
2248 (syntax-violation
2249 #f
2250 "source expression failed to match any pattern"
2251 tmp-1))))))
2252 (set! macroexpand
2253 (lambda* (x #:optional (m 'e) (esew '(eval)))
2254 (expand-top-sequence
2255 (list x)
2256 '()
2257 '((top))
2258 #f
2259 m
2260 esew
2261 (cons 'hygiene (module-name (current-module))))))
2262 (set! identifier? (lambda (x) (nonsymbol-id? x)))
2263 (set! datum->syntax
2264 (lambda (id datum)
2265 (make-syntax-object
2266 datum
2267 (syntax-object-wrap id)
2268 (syntax-object-module id))))
2269 (set! syntax->datum (lambda (x) (strip x '(()))))
2270 (set! syntax-source (lambda (x) (source-annotation x)))
2271 (set! generate-temporaries
2272 (lambda (ls)
2273 (let ((x ls))
2274 (if (not (list? x))
2275 (syntax-violation 'generate-temporaries "invalid argument" x)))
2276 (let ((mod (cons 'hygiene (module-name (current-module)))))
2277 (map (lambda (x) (wrap (gensym "t-") '((top)) mod)) ls))))
2278 (set! free-identifier=?
2279 (lambda (x y)
2280 (let ((x x))
2281 (if (not (nonsymbol-id? x))
2282 (syntax-violation 'free-identifier=? "invalid argument" x)))
2283 (let ((x y))
2284 (if (not (nonsymbol-id? x))
2285 (syntax-violation 'free-identifier=? "invalid argument" x)))
2286 (free-id=? x y)))
2287 (set! bound-identifier=?
2288 (lambda (x y)
2289 (let ((x x))
2290 (if (not (nonsymbol-id? x))
2291 (syntax-violation 'bound-identifier=? "invalid argument" x)))
2292 (let ((x y))
2293 (if (not (nonsymbol-id? x))
2294 (syntax-violation 'bound-identifier=? "invalid argument" x)))
2295 (bound-id=? x y)))
2296 (set! syntax-violation
2297 (lambda* (who message form #:optional (subform #f))
2298 (let ((x who))
2299 (if (not (let ((x x)) (or (not x) (string? x) (symbol? x))))
2300 (syntax-violation 'syntax-violation "invalid argument" x)))
2301 (let ((x message))
2302 (if (not (string? x))
2303 (syntax-violation 'syntax-violation "invalid argument" x)))
2304 (throw 'syntax-error
2305 who
2306 message
2307 (or (source-annotation subform) (source-annotation form))
2308 (strip form '(()))
2309 (and subform (strip subform '(()))))))
2310 (letrec*
2311 ((syntax-module
2312 (lambda (id)
2313 (let ((x id))
2314 (if (not (nonsymbol-id? x))
2315 (syntax-violation 'syntax-module "invalid argument" x)))
2316 (cdr (syntax-object-module id))))
2317 (syntax-local-binding
2318 (lambda (id)
2319 (let ((x id))
2320 (if (not (nonsymbol-id? x))
2321 (syntax-violation 'syntax-local-binding "invalid argument" x)))
2322 (with-transformer-environment
2323 (lambda (e r w s rib mod)
2324 (letrec*
2325 ((strip-anti-mark
2326 (lambda (w)
2327 (let ((ms (car w)) (s (cdr w)))
2328 (if (and (pair? ms) (eq? (car ms) #f))
2329 (cons (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
2330 (cons ms (if rib (cons rib s) s)))))))
2331 (call-with-values
2332 (lambda ()
2333 (resolve-identifier
2334 (syntax-object-expression id)
2335 (strip-anti-mark (syntax-object-wrap id))
2336 r
2337 (syntax-object-module id)))
2338 (lambda (type value mod)
2339 (let ((key type))
2340 (cond ((memv key '(lexical)) (values 'lexical value))
2341 ((memv key '(macro)) (values 'macro value))
2342 ((memv key '(syntax)) (values 'pattern-variable value))
2343 ((memv key '(displaced-lexical)) (values 'displaced-lexical #f))
2344 ((memv key '(global)) (values 'global (cons value (cdr mod))))
2345 (else (values 'other #f)))))))))))
2346 (syntax-locally-bound-identifiers
2347 (lambda (id)
2348 (let ((x id))
2349 (if (not (nonsymbol-id? x))
2350 (syntax-violation
2351 'syntax-locally-bound-identifiers
2352 "invalid argument"
2353 x)))
2354 (locally-bound-identifiers
2355 (syntax-object-wrap id)
2356 (syntax-object-module id)))))
2357 (define! 'syntax-module syntax-module)
2358 (define! 'syntax-local-binding syntax-local-binding)
2359 (define!
2360 'syntax-locally-bound-identifiers
2361 syntax-locally-bound-identifiers))
2362 (letrec*
2363 ((match-each
2364 (lambda (e p w mod)
2365 (cond ((pair? e)
2366 (let ((first (match (car e) p w '() mod)))
2367 (and first
2368 (let ((rest (match-each (cdr e) p w mod)))
2369 (and rest (cons first rest))))))
2370 ((null? e) '())
2371 ((syntax-object? e)
2372 (match-each
2373 (syntax-object-expression e)
2374 p
2375 (join-wraps w (syntax-object-wrap e))
2376 (syntax-object-module e)))
2377 (else #f))))
2378 (match-each+
2379 (lambda (e x-pat y-pat z-pat w r mod)
2380 (let f ((e e) (w w))
2381 (cond ((pair? e)
2382 (call-with-values
2383 (lambda () (f (cdr e) w))
2384 (lambda (xr* y-pat r)
2385 (if r
2386 (if (null? y-pat)
2387 (let ((xr (match (car e) x-pat w '() mod)))
2388 (if xr (values (cons xr xr*) y-pat r) (values #f #f #f)))
2389 (values '() (cdr y-pat) (match (car e) (car y-pat) w r mod)))
2390 (values #f #f #f)))))
2391 ((syntax-object? e)
2392 (f (syntax-object-expression e) (join-wraps w e)))
2393 (else (values '() y-pat (match e z-pat w r mod)))))))
2394 (match-each-any
2395 (lambda (e w mod)
2396 (cond ((pair? e)
2397 (let ((l (match-each-any (cdr e) w mod)))
2398 (and l (cons (wrap (car e) w mod) l))))
2399 ((null? e) '())
2400 ((syntax-object? e)
2401 (match-each-any
2402 (syntax-object-expression e)
2403 (join-wraps w (syntax-object-wrap e))
2404 mod))
2405 (else #f))))
2406 (match-empty
2407 (lambda (p r)
2408 (cond ((null? p) r)
2409 ((eq? p '_) r)
2410 ((eq? p 'any) (cons '() r))
2411 ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
2412 ((eq? p 'each-any) (cons '() r))
2413 (else
2414 (let ((key (vector-ref p 0)))
2415 (cond ((memv key '(each)) (match-empty (vector-ref p 1) r))
2416 ((memv key '(each+))
2417 (match-empty
2418 (vector-ref p 1)
2419 (match-empty
2420 (reverse (vector-ref p 2))
2421 (match-empty (vector-ref p 3) r))))
2422 ((memv key '(free-id atom)) r)
2423 ((memv key '(vector)) (match-empty (vector-ref p 1) r))))))))
2424 (combine
2425 (lambda (r* r)
2426 (if (null? (car r*)) r (cons (map car r*) (combine (map cdr r*) r)))))
2427 (match*
2428 (lambda (e p w r mod)
2429 (cond ((null? p) (and (null? e) r))
2430 ((pair? p)
2431 (and (pair? e)
2432 (match (car e) (car p) w (match (cdr e) (cdr p) w r mod) mod)))
2433 ((eq? p 'each-any)
2434 (let ((l (match-each-any e w mod))) (and l (cons l r))))
2435 (else
2436 (let ((key (vector-ref p 0)))
2437 (cond ((memv key '(each))
2438 (if (null? e)
2439 (match-empty (vector-ref p 1) r)
2440 (let ((l (match-each e (vector-ref p 1) w mod)))
2441 (and l
2442 (let collect ((l l))
2443 (if (null? (car l)) r (cons (map car l) (collect (map cdr l)))))))))
2444 ((memv key '(each+))
2445 (call-with-values
2446 (lambda ()
2447 (match-each+
2448 e
2449 (vector-ref p 1)
2450 (vector-ref p 2)
2451 (vector-ref p 3)
2452 w
2453 r
2454 mod))
2455 (lambda (xr* y-pat r)
2456 (and r
2457 (null? y-pat)
2458 (if (null? xr*) (match-empty (vector-ref p 1) r) (combine xr* r))))))
2459 ((memv key '(free-id))
2460 (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
2461 ((memv key '(atom)) (and (equal? (vector-ref p 1) (strip e w)) r))
2462 ((memv key '(vector))
2463 (and (vector? e) (match (vector->list e) (vector-ref p 1) w r mod)))))))))
2464 (match (lambda (e p w r mod)
2465 (cond ((not r) #f)
2466 ((eq? p '_) r)
2467 ((eq? p 'any) (cons (wrap e w mod) r))
2468 ((syntax-object? e)
2469 (match*
2470 (syntax-object-expression e)
2471 p
2472 (join-wraps w (syntax-object-wrap e))
2473 r
2474 (syntax-object-module e)))
2475 (else (match* e p w r mod))))))
2476 (set! $sc-dispatch
2477 (lambda (e p)
2478 (cond ((eq? p 'any) (list e))
2479 ((eq? p '_) '())
2480 ((syntax-object? e)
2481 (match*
2482 (syntax-object-expression e)
2483 p
2484 (syntax-object-wrap e)
2485 '()
2486 (syntax-object-module e)))
2487 (else (match* e p '(()) '() #f)))))))
2488
2489(define with-syntax
2490 (make-syntax-transformer
2491 'with-syntax
2492 'macro
2493 (lambda (x)
2494 (let ((tmp x))
2495 (let ((tmp-1 ($sc-dispatch tmp '(_ () any . each-any))))
2496 (if tmp-1
2497 (apply (lambda (e1 e2)
1af6d2a7 2498 (cons '#(syntax-object let ((top)) (hygiene guile))
72ee0ef7
MW
2499 (cons '() (cons e1 e2))))
2500 tmp-1)
2501 (let ((tmp-1 ($sc-dispatch tmp '(_ ((any any)) any . each-any))))
2502 (if tmp-1
2503 (apply (lambda (out in e1 e2)
1af6d2a7 2504 (list '#(syntax-object syntax-case ((top)) (hygiene guile))
72ee0ef7
MW
2505 in
2506 '()
2507 (list out
1af6d2a7 2508 (cons '#(syntax-object let ((top)) (hygiene guile))
72ee0ef7
MW
2509 (cons '() (cons e1 e2))))))
2510 tmp-1)
2511 (let ((tmp-1 ($sc-dispatch tmp '(_ #(each (any any)) any . each-any))))
2512 (if tmp-1
2513 (apply (lambda (out in e1 e2)
1af6d2a7
MW
2514 (list '#(syntax-object syntax-case ((top)) (hygiene guile))
2515 (cons '#(syntax-object list ((top)) (hygiene guile)) in)
72ee0ef7
MW
2516 '()
2517 (list out
1af6d2a7 2518 (cons '#(syntax-object let ((top)) (hygiene guile))
72ee0ef7
MW
2519 (cons '() (cons e1 e2))))))
2520 tmp-1)
2521 (syntax-violation
2522 #f
2523 "source expression failed to match any pattern"
2524 tmp)))))))))))
2525
2526(define syntax-rules
2527 (make-syntax-transformer
2528 'syntax-rules
2529 'macro
1af6d2a7
MW
2530 (lambda (xx)
2531 (let ((tmp-1 xx))
72ee0ef7
MW
2532 (let ((tmp ($sc-dispatch tmp-1 '(_ each-any . #(each ((any . any) any))))))
2533 (if tmp
2534 (apply (lambda (k keyword pattern template)
1af6d2a7
MW
2535 (list '#(syntax-object lambda ((top)) (hygiene guile))
2536 '(#(syntax-object x ((top)) (hygiene guile)))
72ee0ef7 2537 (vector
1af6d2a7 2538 '(#(syntax-object macro-type ((top)) (hygiene guile))
72ee0ef7 2539 .
1af6d2a7
MW
2540 #(syntax-object syntax-rules ((top)) (hygiene guile)))
2541 (cons '#(syntax-object patterns ((top)) (hygiene guile)) pattern))
2542 (cons '#(syntax-object syntax-case ((top)) (hygiene guile))
2543 (cons '#(syntax-object x ((top)) (hygiene guile))
72ee0ef7 2544 (cons k
8b22ced1
MW
2545 (map (lambda (tmp-1 tmp)
2546 (list (cons '#(syntax-object dummy ((top)) (hygiene guile)) tmp)
1af6d2a7 2547 (list '#(syntax-object syntax ((top)) (hygiene guile))
8b22ced1 2548 tmp-1)))
72ee0ef7 2549 template
8b22ced1 2550 pattern))))))
72ee0ef7
MW
2551 tmp)
2552 (let ((tmp ($sc-dispatch tmp-1 '(_ each-any any . #(each ((any . any) any))))))
2553 (if (if tmp
2554 (apply (lambda (k docstring keyword pattern template)
2555 (string? (syntax->datum docstring)))
2556 tmp)
2557 #f)
2558 (apply (lambda (k docstring keyword pattern template)
1af6d2a7
MW
2559 (list '#(syntax-object lambda ((top)) (hygiene guile))
2560 '(#(syntax-object x ((top)) (hygiene guile)))
72ee0ef7
MW
2561 docstring
2562 (vector
1af6d2a7 2563 '(#(syntax-object macro-type ((top)) (hygiene guile))
72ee0ef7 2564 .
1af6d2a7
MW
2565 #(syntax-object syntax-rules ((top)) (hygiene guile)))
2566 (cons '#(syntax-object patterns ((top)) (hygiene guile)) pattern))
2567 (cons '#(syntax-object syntax-case ((top)) (hygiene guile))
2568 (cons '#(syntax-object x ((top)) (hygiene guile))
72ee0ef7 2569 (cons k
8b22ced1
MW
2570 (map (lambda (tmp-1 tmp)
2571 (list (cons '#(syntax-object dummy ((top)) (hygiene guile)) tmp)
1af6d2a7 2572 (list '#(syntax-object syntax ((top)) (hygiene guile))
8b22ced1 2573 tmp-1)))
72ee0ef7 2574 template
8b22ced1 2575 pattern))))))
72ee0ef7
MW
2576 tmp)
2577 (syntax-violation
2578 #f
2579 "source expression failed to match any pattern"
2580 tmp-1)))))))))
2581
2582(define define-syntax-rule
2583 (make-syntax-transformer
2584 'define-syntax-rule
2585 'macro
2586 (lambda (x)
2587 (let ((tmp-1 x))
2588 (let ((tmp ($sc-dispatch tmp-1 '(_ (any . any) any))))
2589 (if tmp
2590 (apply (lambda (name pattern template)
1af6d2a7 2591 (list '#(syntax-object define-syntax ((top)) (hygiene guile))
72ee0ef7 2592 name
1af6d2a7 2593 (list '#(syntax-object syntax-rules ((top)) (hygiene guile))
72ee0ef7 2594 '()
8b22ced1
MW
2595 (list (cons '#(syntax-object _ ((top)) (hygiene guile)) pattern)
2596 template))))
72ee0ef7
MW
2597 tmp)
2598 (let ((tmp ($sc-dispatch tmp-1 '(_ (any . any) any any))))
2599 (if (if tmp
2600 (apply (lambda (name pattern docstring template)
2601 (string? (syntax->datum docstring)))
2602 tmp)
2603 #f)
2604 (apply (lambda (name pattern docstring template)
1af6d2a7 2605 (list '#(syntax-object define-syntax ((top)) (hygiene guile))
72ee0ef7 2606 name
1af6d2a7 2607 (list '#(syntax-object syntax-rules ((top)) (hygiene guile))
72ee0ef7
MW
2608 '()
2609 docstring
8b22ced1
MW
2610 (list (cons '#(syntax-object _ ((top)) (hygiene guile)) pattern)
2611 template))))
72ee0ef7
MW
2612 tmp)
2613 (syntax-violation
2614 #f
2615 "source expression failed to match any pattern"
2616 tmp-1)))))))))
2617
2618(define let*
2619 (make-syntax-transformer
2620 'let*
2621 'macro
2622 (lambda (x)
2623 (let ((tmp-1 x))
2624 (let ((tmp ($sc-dispatch tmp-1 '(any #(each (any any)) any . each-any))))
2625 (if (if tmp
2626 (apply (lambda (let* x v e1 e2) (and-map identifier? x)) tmp)
2627 #f)
2628 (apply (lambda (let* x v e1 e2)
2629 (let f ((bindings (map list x v)))
2630 (if (null? bindings)
1af6d2a7 2631 (cons '#(syntax-object let ((top)) (hygiene guile))
72ee0ef7
MW
2632 (cons '() (cons e1 e2)))
2633 (let ((tmp-1 (list (f (cdr bindings)) (car bindings))))
2634 (let ((tmp ($sc-dispatch tmp-1 '(any any))))
2635 (if tmp
2636 (apply (lambda (body binding)
1af6d2a7 2637 (list '#(syntax-object let ((top)) (hygiene guile))
72ee0ef7
MW
2638 (list binding)
2639 body))
2640 tmp)
2641 (syntax-violation
2642 #f
2643 "source expression failed to match any pattern"
2644 tmp-1)))))))
2645 tmp)
2646 (syntax-violation
2647 #f
2648 "source expression failed to match any pattern"
2649 tmp-1)))))))
2650
72ee0ef7
MW
2651(define quasiquote
2652 (make-syntax-transformer
2653 'quasiquote
2654 'macro
2655 (letrec*
2656 ((quasi (lambda (p lev)
2657 (let ((tmp p))
2658 (let ((tmp-1 ($sc-dispatch
2659 tmp
1af6d2a7 2660 '(#(free-id #(syntax-object unquote ((top)) (hygiene guile))) any))))
72ee0ef7
MW
2661 (if tmp-1
2662 (apply (lambda (p)
2663 (if (= lev 0)
1af6d2a7 2664 (list "value" p)
72ee0ef7 2665 (quasicons
1af6d2a7 2666 '("quote" #(syntax-object unquote ((top)) (hygiene guile)))
72ee0ef7
MW
2667 (quasi (list p) (- lev 1)))))
2668 tmp-1)
2669 (let ((tmp-1 ($sc-dispatch
2670 tmp
1af6d2a7 2671 '(#(free-id #(syntax-object quasiquote ((top)) (hygiene guile))) any))))
72ee0ef7
MW
2672 (if tmp-1
2673 (apply (lambda (p)
2674 (quasicons
1af6d2a7 2675 '("quote" #(syntax-object quasiquote ((top)) (hygiene guile)))
72ee0ef7
MW
2676 (quasi (list p) (+ lev 1))))
2677 tmp-1)
2678 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
2679 (if tmp-1
2680 (apply (lambda (p q)
2681 (let ((tmp-1 p))
2682 (let ((tmp ($sc-dispatch
2683 tmp-1
1af6d2a7 2684 '(#(free-id #(syntax-object unquote ((top)) (hygiene guile)))
72ee0ef7
MW
2685 .
2686 each-any))))
2687 (if tmp
2688 (apply (lambda (p)
2689 (if (= lev 0)
2690 (quasilist*
1af6d2a7 2691 (map (lambda (tmp) (list "value" tmp)) p)
72ee0ef7
MW
2692 (quasi q lev))
2693 (quasicons
2694 (quasicons
1af6d2a7 2695 '("quote" #(syntax-object unquote ((top)) (hygiene guile)))
72ee0ef7
MW
2696 (quasi p (- lev 1)))
2697 (quasi q lev))))
2698 tmp)
2699 (let ((tmp ($sc-dispatch
2700 tmp-1
2701 '(#(free-id
1af6d2a7 2702 #(syntax-object unquote-splicing ((top)) (hygiene guile)))
72ee0ef7
MW
2703 .
2704 each-any))))
2705 (if tmp
2706 (apply (lambda (p)
2707 (if (= lev 0)
2708 (quasiappend
1af6d2a7 2709 (map (lambda (tmp) (list "value" tmp)) p)
72ee0ef7
MW
2710 (quasi q lev))
2711 (quasicons
2712 (quasicons
1af6d2a7 2713 '("quote"
72ee0ef7
MW
2714 #(syntax-object
2715 unquote-splicing
1af6d2a7 2716 ((top))
72ee0ef7
MW
2717 (hygiene guile)))
2718 (quasi p (- lev 1)))
2719 (quasi q lev))))
2720 tmp)
2721 (quasicons (quasi p lev) (quasi q lev))))))))
2722 tmp-1)
2723 (let ((tmp-1 ($sc-dispatch tmp '#(vector each-any))))
2724 (if tmp-1
2725 (apply (lambda (x) (quasivector (vquasi x lev))) tmp-1)
1af6d2a7 2726 (let ((p tmp)) (list "quote" p)))))))))))))
72ee0ef7
MW
2727 (vquasi
2728 (lambda (p lev)
2729 (let ((tmp p))
2730 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
2731 (if tmp-1
2732 (apply (lambda (p q)
2733 (let ((tmp-1 p))
2734 (let ((tmp ($sc-dispatch
2735 tmp-1
1af6d2a7 2736 '(#(free-id #(syntax-object unquote ((top)) (hygiene guile)))
72ee0ef7
MW
2737 .
2738 each-any))))
2739 (if tmp
2740 (apply (lambda (p)
2741 (if (= lev 0)
1af6d2a7 2742 (quasilist* (map (lambda (tmp) (list "value" tmp)) p) (vquasi q lev))
72ee0ef7
MW
2743 (quasicons
2744 (quasicons
1af6d2a7 2745 '("quote" #(syntax-object unquote ((top)) (hygiene guile)))
72ee0ef7
MW
2746 (quasi p (- lev 1)))
2747 (vquasi q lev))))
2748 tmp)
2749 (let ((tmp ($sc-dispatch
2750 tmp-1
1af6d2a7 2751 '(#(free-id #(syntax-object unquote-splicing ((top)) (hygiene guile)))
72ee0ef7
MW
2752 .
2753 each-any))))
2754 (if tmp
2755 (apply (lambda (p)
2756 (if (= lev 0)
2757 (quasiappend
1af6d2a7 2758 (map (lambda (tmp) (list "value" tmp)) p)
72ee0ef7
MW
2759 (vquasi q lev))
2760 (quasicons
2761 (quasicons
1af6d2a7 2762 '("quote" #(syntax-object unquote-splicing ((top)) (hygiene guile)))
72ee0ef7
MW
2763 (quasi p (- lev 1)))
2764 (vquasi q lev))))
2765 tmp)
2766 (quasicons (quasi p lev) (vquasi q lev))))))))
2767 tmp-1)
2768 (let ((tmp-1 ($sc-dispatch tmp '())))
2769 (if tmp-1
1af6d2a7 2770 (apply (lambda () '("quote" ())) tmp-1)
72ee0ef7
MW
2771 (syntax-violation
2772 #f
2773 "source expression failed to match any pattern"
2774 tmp))))))))
2775 (quasicons
2776 (lambda (x y)
2777 (let ((tmp-1 (list x y)))
2778 (let ((tmp ($sc-dispatch tmp-1 '(any any))))
2779 (if tmp
2780 (apply (lambda (x y)
2781 (let ((tmp y))
2782 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "quote") any))))
2783 (if tmp-1
2784 (apply (lambda (dy)
2785 (let ((tmp x))
2786 (let ((tmp ($sc-dispatch tmp '(#(atom "quote") any))))
2787 (if tmp
1af6d2a7
MW
2788 (apply (lambda (dx) (list "quote" (cons dx dy))) tmp)
2789 (if (null? dy) (list "list" x) (list "list*" x y))))))
72ee0ef7
MW
2790 tmp-1)
2791 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list") . any))))
2792 (if tmp-1
1af6d2a7 2793 (apply (lambda (stuff) (cons "list" (cons x stuff))) tmp-1)
72ee0ef7
MW
2794 (let ((tmp ($sc-dispatch tmp '(#(atom "list*") . any))))
2795 (if tmp
1af6d2a7
MW
2796 (apply (lambda (stuff) (cons "list*" (cons x stuff))) tmp)
2797 (list "list*" x y)))))))))
72ee0ef7
MW
2798 tmp)
2799 (syntax-violation
2800 #f
2801 "source expression failed to match any pattern"
2802 tmp-1))))))
2803 (quasiappend
2804 (lambda (x y)
2805 (let ((tmp y))
2806 (let ((tmp ($sc-dispatch tmp '(#(atom "quote") ()))))
2807 (if tmp
2808 (apply (lambda ()
2809 (if (null? x)
1af6d2a7 2810 '("quote" ())
72ee0ef7
MW
2811 (if (null? (cdr x))
2812 (car x)
2813 (let ((tmp-1 x))
2814 (let ((tmp ($sc-dispatch tmp-1 'each-any)))
2815 (if tmp
1af6d2a7 2816 (apply (lambda (p) (cons "append" p)) tmp)
72ee0ef7
MW
2817 (syntax-violation
2818 #f
2819 "source expression failed to match any pattern"
2820 tmp-1)))))))
2821 tmp)
2822 (if (null? x)
2823 y
2824 (let ((tmp-1 (list x y)))
2825 (let ((tmp ($sc-dispatch tmp-1 '(each-any any))))
2826 (if tmp
1af6d2a7 2827 (apply (lambda (p y) (cons "append" (append p (list y)))) tmp)
72ee0ef7
MW
2828 (syntax-violation
2829 #f
2830 "source expression failed to match any pattern"
2831 tmp-1))))))))))
2832 (quasilist*
2833 (lambda (x y)
2834 (let f ((x x)) (if (null? x) y (quasicons (car x) (f (cdr x)))))))
2835 (quasivector
2836 (lambda (x)
2837 (let ((tmp x))
2838 (let ((tmp ($sc-dispatch tmp '(#(atom "quote") each-any))))
2839 (if tmp
1af6d2a7 2840 (apply (lambda (x) (list "quote" (list->vector x))) tmp)
72ee0ef7
MW
2841 (let f ((y x)
2842 (k (lambda (ls)
2843 (let ((tmp-1 ls))
2844 (let ((tmp ($sc-dispatch tmp-1 'each-any)))
2845 (if tmp
1af6d2a7 2846 (apply (lambda (t) (cons "vector" t)) tmp)
72ee0ef7
MW
2847 (syntax-violation
2848 #f
2849 "source expression failed to match any pattern"
2850 tmp-1)))))))
2851 (let ((tmp y))
2852 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "quote") each-any))))
2853 (if tmp-1
1af6d2a7 2854 (apply (lambda (y) (k (map (lambda (tmp) (list "quote" tmp)) y)))
72ee0ef7
MW
2855 tmp-1)
2856 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list") . each-any))))
2857 (if tmp-1
2858 (apply (lambda (y) (k y)) tmp-1)
2859 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list*") . #(each+ any (any) ())))))
2860 (if tmp-1
2861 (apply (lambda (y z) (f z (lambda (ls) (k (append y ls))))) tmp-1)
2862 (let ((else tmp))
1af6d2a7 2863 (let ((tmp x)) (let ((t tmp)) (list "list->vector" t)))))))))))))))))
72ee0ef7
MW
2864 (emit (lambda (x)
2865 (let ((tmp x))
2866 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "quote") any))))
2867 (if tmp-1
1af6d2a7 2868 (apply (lambda (x) (list '#(syntax-object quote ((top)) (hygiene guile)) x))
72ee0ef7
MW
2869 tmp-1)
2870 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list") . each-any))))
2871 (if tmp-1
2872 (apply (lambda (x)
2873 (let ((tmp-1 (map emit x)))
2874 (let ((tmp ($sc-dispatch tmp-1 'each-any)))
2875 (if tmp
1af6d2a7 2876 (apply (lambda (t) (cons '#(syntax-object list ((top)) (hygiene guile)) t))
72ee0ef7
MW
2877 tmp)
2878 (syntax-violation
2879 #f
2880 "source expression failed to match any pattern"
2881 tmp-1)))))
2882 tmp-1)
2883 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list*") . #(each+ any (any) ())))))
2884 (if tmp-1
2885 (apply (lambda (x y)
2886 (let f ((x* x))
2887 (if (null? x*)
2888 (emit y)
2889 (let ((tmp-1 (list (emit (car x*)) (f (cdr x*)))))
2890 (let ((tmp ($sc-dispatch tmp-1 '(any any))))
2891 (if tmp
2892 (apply (lambda (t-1 t)
1af6d2a7 2893 (list '#(syntax-object cons ((top)) (hygiene guile)) t-1 t))
72ee0ef7
MW
2894 tmp)
2895 (syntax-violation
2896 #f
2897 "source expression failed to match any pattern"
2898 tmp-1)))))))
2899 tmp-1)
2900 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "append") . each-any))))
2901 (if tmp-1
2902 (apply (lambda (x)
2903 (let ((tmp-1 (map emit x)))
2904 (let ((tmp ($sc-dispatch tmp-1 'each-any)))
2905 (if tmp
2906 (apply (lambda (t)
1af6d2a7 2907 (cons '#(syntax-object append ((top)) (hygiene guile)) t))
72ee0ef7
MW
2908 tmp)
2909 (syntax-violation
2910 #f
2911 "source expression failed to match any pattern"
2912 tmp-1)))))
2913 tmp-1)
2914 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "vector") . each-any))))
2915 (if tmp-1
2916 (apply (lambda (x)
2917 (let ((tmp-1 (map emit x)))
2918 (let ((tmp ($sc-dispatch tmp-1 'each-any)))
2919 (if tmp
2920 (apply (lambda (t)
1af6d2a7 2921 (cons '#(syntax-object vector ((top)) (hygiene guile)) t))
72ee0ef7
MW
2922 tmp)
2923 (syntax-violation
2924 #f
2925 "source expression failed to match any pattern"
2926 tmp-1)))))
2927 tmp-1)
2928 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list->vector") any))))
2929 (if tmp-1
2930 (apply (lambda (x)
2931 (let ((tmp (emit x)))
2932 (let ((t tmp))
1af6d2a7 2933 (list '#(syntax-object list->vector ((top)) (hygiene guile)) t))))
72ee0ef7
MW
2934 tmp-1)
2935 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "value") any))))
2936 (if tmp-1
2937 (apply (lambda (x) x) tmp-1)
2938 (syntax-violation
2939 #f
2940 "source expression failed to match any pattern"
2941 tmp)))))))))))))))))))
2942 (lambda (x)
2943 (let ((tmp-1 x))
2944 (let ((tmp ($sc-dispatch tmp-1 '(_ any))))
2945 (if tmp
2946 (apply (lambda (e) (emit (quasi e 0))) tmp)
2947 (syntax-violation
2948 #f
2949 "source expression failed to match any pattern"
2950 tmp-1))))))))
df40b969
AW
2951
2952(define include
2953 (make-syntax-transformer
2954 'include
2955 'macro
72ee0ef7 2956 (lambda (x)
34c5fe83 2957 (letrec*
e70b663c
MW
2958 ((absolute-path? (lambda (path) (string-prefix? "/" path)))
2959 (read-file
2960 (lambda (fn dir k)
2961 (let ((p (open-input-file (if (absolute-path? fn) fn (in-vicinity dir fn)))))
72ee0ef7
MW
2962 (let f ((x (read p)) (result '()))
2963 (if (eof-object? x)
2964 (begin (close-input-port p) (reverse result))
2965 (f (read p) (cons (datum->syntax k x) result))))))))
e70b663c
MW
2966 (let ((src (syntax-source x)))
2967 (let ((file (if src (assq-ref src 'filename) #f)))
2968 (let ((dir (if (string? file) (dirname file) #f)))
2969 (let ((tmp-1 x))
2970 (let ((tmp ($sc-dispatch tmp-1 '(any any))))
2971 (if tmp
2972 (apply (lambda (k filename)
2973 (let ((fn (syntax->datum filename)))
2974 (let ((tmp-1 (read-file fn dir filename)))
2975 (let ((tmp ($sc-dispatch tmp-1 'each-any)))
2976 (if tmp
2977 (apply (lambda (exp)
2978 (cons '#(syntax-object begin ((top)) (hygiene guile)) exp))
2979 tmp)
2980 (syntax-violation
2981 #f
2982 "source expression failed to match any pattern"
2983 tmp-1))))))
2984 tmp)
2985 (syntax-violation
2986 #f
2987 "source expression failed to match any pattern"
2988 tmp-1)))))))))))
df40b969
AW
2989
2990(define include-from-path
2991 (make-syntax-transformer
2992 'include-from-path
2993 'macro
72ee0ef7
MW
2994 (lambda (x)
2995 (let ((tmp-1 x))
2996 (let ((tmp ($sc-dispatch tmp-1 '(any any))))
2997 (if tmp
2998 (apply (lambda (k filename)
2999 (let ((fn (syntax->datum filename)))
3000 (let ((tmp (datum->syntax
3001 filename
3002 (let ((t (%search-load-path fn)))
3003 (if t
3004 t
3005 (syntax-violation
3006 'include-from-path
3007 "file not found in path"
3008 x
3009 filename))))))
3010 (let ((fn tmp))
1af6d2a7 3011 (list '#(syntax-object include ((top)) (hygiene guile)) fn)))))
72ee0ef7
MW
3012 tmp)
3013 (syntax-violation
3014 #f
3015 "source expression failed to match any pattern"
3016 tmp-1)))))))
df40b969
AW
3017
3018(define unquote
3019 (make-syntax-transformer
3020 'unquote
3021 'macro
72ee0ef7 3022 (lambda (x)
df40b969
AW
3023 (syntax-violation
3024 'unquote
3025 "expression not valid outside of quasiquote"
72ee0ef7 3026 x))))
df40b969
AW
3027
3028(define unquote-splicing
014de9e2
AW
3029 (make-syntax-transformer
3030 'unquote-splicing
3031 'macro
72ee0ef7 3032 (lambda (x)
014de9e2
AW
3033 (syntax-violation
3034 'unquote-splicing
3035 "expression not valid outside of quasiquote"
72ee0ef7 3036 x))))
014de9e2 3037
014de9e2 3038(define make-variable-transformer
72ee0ef7
MW
3039 (lambda (proc)
3040 (if (procedure? proc)
3041 (let ((trans (lambda (x) (proc x))))
3042 (set-procedure-property! trans 'variable-transformer #t)
3043 trans)
3044 (error "variable transformer not a procedure" proc))))
014de9e2
AW
3045
3046(define identifier-syntax
3047 (make-syntax-transformer
3048 'identifier-syntax
3049 'macro
1af6d2a7
MW
3050 (lambda (xx)
3051 (let ((tmp-1 xx))
72ee0ef7
MW
3052 (let ((tmp ($sc-dispatch tmp-1 '(_ any))))
3053 (if tmp
3054 (apply (lambda (e)
1af6d2a7
MW
3055 (list '#(syntax-object lambda ((top)) (hygiene guile))
3056 '(#(syntax-object x ((top)) (hygiene guile)))
3057 '#((#(syntax-object macro-type ((top)) (hygiene guile))
72ee0ef7 3058 .
1af6d2a7
MW
3059 #(syntax-object identifier-syntax ((top)) (hygiene guile))))
3060 (list '#(syntax-object syntax-case ((top)) (hygiene guile))
3061 '#(syntax-object x ((top)) (hygiene guile))
72ee0ef7 3062 '()
1af6d2a7
MW
3063 (list '#(syntax-object id ((top)) (hygiene guile))
3064 '(#(syntax-object identifier? ((top)) (hygiene guile))
3065 (#(syntax-object syntax ((top)) (hygiene guile))
3066 #(syntax-object id ((top)) (hygiene guile))))
3067 (list '#(syntax-object syntax ((top)) (hygiene guile)) e))
3068 (list '(#(syntax-object _ ((top)) (hygiene guile))
3069 #(syntax-object x ((top)) (hygiene guile))
3070 #(syntax-object ... ((top)) (hygiene guile)))
3071 (list '#(syntax-object syntax ((top)) (hygiene guile))
72ee0ef7 3072 (cons e
1af6d2a7
MW
3073 '(#(syntax-object x ((top)) (hygiene guile))
3074 #(syntax-object ... ((top)) (hygiene guile)))))))))
72ee0ef7
MW
3075 tmp)
3076 (let ((tmp ($sc-dispatch
3077 tmp-1
3078 '(_ (any any)
1af6d2a7 3079 ((#(free-id #(syntax-object set! ((top)) (hygiene guile))) any any)
72ee0ef7
MW
3080 any)))))
3081 (if (if tmp
3082 (apply (lambda (id exp1 var val exp2)
3083 (if (identifier? id) (identifier? var) #f))
3084 tmp)
3085 #f)
3086 (apply (lambda (id exp1 var val exp2)
1af6d2a7
MW
3087 (list '#(syntax-object make-variable-transformer ((top)) (hygiene guile))
3088 (list '#(syntax-object lambda ((top)) (hygiene guile))
3089 '(#(syntax-object x ((top)) (hygiene guile)))
3090 '#((#(syntax-object macro-type ((top)) (hygiene guile))
72ee0ef7 3091 .
1af6d2a7
MW
3092 #(syntax-object variable-transformer ((top)) (hygiene guile))))
3093 (list '#(syntax-object syntax-case ((top)) (hygiene guile))
3094 '#(syntax-object x ((top)) (hygiene guile))
3095 '(#(syntax-object set! ((top)) (hygiene guile)))
3096 (list (list '#(syntax-object set! ((top)) (hygiene guile)) var val)
3097 (list '#(syntax-object syntax ((top)) (hygiene guile)) exp2))
72ee0ef7 3098 (list (cons id
1af6d2a7
MW
3099 '(#(syntax-object x ((top)) (hygiene guile))
3100 #(syntax-object ... ((top)) (hygiene guile))))
3101 (list '#(syntax-object syntax ((top)) (hygiene guile))
72ee0ef7 3102 (cons exp1
1af6d2a7
MW
3103 '(#(syntax-object x ((top)) (hygiene guile))
3104 #(syntax-object ... ((top)) (hygiene guile))))))
72ee0ef7 3105 (list id
1af6d2a7
MW
3106 (list '#(syntax-object identifier? ((top)) (hygiene guile))
3107 (list '#(syntax-object syntax ((top)) (hygiene guile)) id))
3108 (list '#(syntax-object syntax ((top)) (hygiene guile)) exp1))))))
72ee0ef7
MW
3109 tmp)
3110 (syntax-violation
3111 #f
3112 "source expression failed to match any pattern"
3113 tmp-1)))))))))
97bc28b6
AW
3114
3115(define define*
e809758a
AW
3116 (make-syntax-transformer
3117 'define*
97bc28b6 3118 'macro
72ee0ef7
MW
3119 (lambda (x)
3120 (let ((tmp-1 x))
3121 (let ((tmp ($sc-dispatch tmp-1 '(_ (any . any) any . each-any))))
3122 (if tmp
3123 (apply (lambda (id args b0 b1)
1af6d2a7 3124 (list '#(syntax-object define ((top)) (hygiene guile))
72ee0ef7 3125 id
1af6d2a7 3126 (cons '#(syntax-object lambda* ((top)) (hygiene guile))
72ee0ef7
MW
3127 (cons args (cons b0 b1)))))
3128 tmp)
3129 (let ((tmp ($sc-dispatch tmp-1 '(_ any any))))
3130 (if (if tmp (apply (lambda (id val) (identifier? id)) tmp) #f)
3131 (apply (lambda (id val)
1af6d2a7 3132 (list '#(syntax-object define ((top)) (hygiene guile)) id val))
72ee0ef7
MW
3133 tmp)
3134 (syntax-violation
3135 #f
3136 "source expression failed to match any pattern"
3137 tmp-1)))))))))
65dd9e38 3138