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