7ad8a70182448b7e00e3ddc2b5074797ca6a92d0
[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 ellipsis))
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 (call-with-values
595 (lambda () (resolve-identifier id '(()) r mod #t))
596 (lambda (type* value* mod*)
597 (if (eq? type* 'macro)
598 (top-level-eval-hook
599 (build-global-definition s var (build-void s))
600 mod))
601 (lambda () (build-global-definition s var (expand e r w mod)))))))))
602 ((memv key '(define-syntax-form define-syntax-parameter-form))
603 (let* ((id (wrap value w mod))
604 (label (gen-label))
605 (var (if (macro-introduced-identifier? id)
606 (fresh-derived-name id x)
607 (syntax-object-expression id))))
608 (record-definition! id var)
609 (let ((key m))
610 (cond ((memv key '(c))
611 (cond ((memq 'compile esew)
612 (let ((e (expand-install-global var type (expand e r w mod))))
613 (top-level-eval-hook e mod)
614 (if (memq 'load esew) (list (lambda () e)) '())))
615 ((memq 'load esew)
616 (list (lambda () (expand-install-global var type (expand e r w mod)))))
617 (else '())))
618 ((memv key '(c&e))
619 (let ((e (expand-install-global var type (expand e r w mod))))
620 (top-level-eval-hook e mod)
621 (list (lambda () e))))
622 (else
623 (if (memq 'eval esew)
624 (top-level-eval-hook
625 (expand-install-global var type (expand e r w mod))
626 mod))
627 '())))))
628 ((memv key '(begin-form))
629 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ . each-any))))
630 (if tmp
631 (apply (lambda (e1) (parse e1 r w s m esew mod)) tmp)
632 (syntax-violation
633 #f
634 "source expression failed to match any pattern"
635 tmp-1))))
636 ((memv key '(local-syntax-form))
637 (expand-local-syntax
638 value
639 e
640 r
641 w
642 s
643 mod
644 (lambda (forms r w s mod) (parse forms r w s m esew mod))))
645 ((memv key '(eval-when-form))
646 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ each-any any . each-any))))
647 (if tmp
648 (apply (lambda (x e1 e2)
649 (let ((when-list (parse-when-list e x)) (body (cons e1 e2)))
650 (letrec*
651 ((recurse (lambda (m esew) (parse body r w s m esew mod))))
652 (cond ((eq? m 'e)
653 (if (memq 'eval when-list)
654 (recurse (if (memq 'expand when-list) 'c&e 'e) '(eval))
655 (begin
656 (if (memq 'expand when-list)
657 (top-level-eval-hook
658 (expand-top-sequence body r w s 'e '(eval) mod)
659 mod))
660 '())))
661 ((memq 'load when-list)
662 (cond ((or (memq 'compile when-list)
663 (memq 'expand when-list)
664 (and (eq? m 'c&e) (memq 'eval when-list)))
665 (recurse 'c&e '(compile load)))
666 ((memq m '(c c&e)) (recurse 'c '(load)))
667 (else '())))
668 ((or (memq 'compile when-list)
669 (memq 'expand when-list)
670 (and (eq? m 'c&e) (memq 'eval when-list)))
671 (top-level-eval-hook
672 (expand-top-sequence body r w s 'e '(eval) mod)
673 mod)
674 '())
675 (else '())))))
676 tmp)
677 (syntax-violation
678 #f
679 "source expression failed to match any pattern"
680 tmp-1))))
681 (else
682 (list (if (eq? m 'c&e)
683 (let ((x (expand-expr type value form e r w s mod)))
684 (top-level-eval-hook x mod)
685 (lambda () x))
686 (lambda () (expand-expr type value form e r w s mod))))))))))))
687 (let ((exps (map (lambda (x) (x)) (reverse (parse body r w s m esew mod)))))
688 (if (null? exps) (build-void s) (build-sequence s exps)))))))
689 (expand-install-global
690 (lambda (name type e)
691 (build-global-definition
692 #f
693 name
694 (build-primcall
695 #f
696 'make-syntax-transformer
697 (if (eq? type 'define-syntax-parameter-form)
698 (list (build-data #f name)
699 (build-data #f 'syntax-parameter)
700 (build-primcall #f 'list (list e)))
701 (list (build-data #f name) (build-data #f 'macro) e))))))
702 (parse-when-list
703 (lambda (e when-list)
704 (let ((result (strip when-list '(()))))
705 (let lp ((l result))
706 (cond ((null? l) result)
707 ((memq (car l) '(compile load eval expand)) (lp (cdr l)))
708 (else (syntax-violation 'eval-when "invalid situation" e (car l))))))))
709 (syntax-type
710 (lambda (e r w s rib mod for-car?)
711 (cond ((symbol? e)
712 (call-with-values
713 (lambda () (resolve-identifier e w r mod #t))
714 (lambda (type value mod*)
715 (let ((key type))
716 (cond ((memv key '(macro))
717 (if for-car?
718 (values type value e e w s mod)
719 (syntax-type
720 (expand-macro value e r w s rib mod)
721 r
722 '(())
723 s
724 rib
725 mod
726 #f)))
727 ((memv key '(global)) (values type value e value w s mod*))
728 (else (values type value e e w s mod)))))))
729 ((pair? e)
730 (let ((first (car e)))
731 (call-with-values
732 (lambda () (syntax-type first r w s rib mod #t))
733 (lambda (ftype fval fform fe fw fs fmod)
734 (let ((key ftype))
735 (cond ((memv key '(lexical)) (values 'lexical-call fval e e w s mod))
736 ((memv key '(global))
737 (if (equal? fmod '(primitive))
738 (values 'primitive-call fval e e w s mod)
739 (values 'global-call (make-syntax-object fval w fmod) e e w s mod)))
740 ((memv key '(macro))
741 (syntax-type
742 (expand-macro fval e r w s rib mod)
743 r
744 '(())
745 s
746 rib
747 mod
748 for-car?))
749 ((memv key '(module-ref))
750 (call-with-values
751 (lambda () (fval e r w mod))
752 (lambda (e r w s mod) (syntax-type e r w s rib mod for-car?))))
753 ((memv key '(core)) (values 'core-form fval e e w s mod))
754 ((memv key '(local-syntax))
755 (values 'local-syntax-form fval e e w s mod))
756 ((memv key '(begin)) (values 'begin-form #f e e w s mod))
757 ((memv key '(eval-when)) (values 'eval-when-form #f e e w s mod))
758 ((memv key '(define))
759 (let* ((tmp e) (tmp-1 ($sc-dispatch tmp '(_ any any))))
760 (if (and tmp-1 (apply (lambda (name val) (id? name)) tmp-1))
761 (apply (lambda (name val) (values 'define-form name e val w s mod))
762 tmp-1)
763 (let ((tmp-1 ($sc-dispatch tmp '(_ (any . any) any . each-any))))
764 (if (and tmp-1
765 (apply (lambda (name args e1 e2)
766 (and (id? name) (valid-bound-ids? (lambda-var-list args))))
767 tmp-1))
768 (apply (lambda (name args e1 e2)
769 (values
770 'define-form
771 (wrap name w mod)
772 (wrap e w mod)
773 (decorate-source
774 (cons '#(syntax-object lambda ((top)) (hygiene guile))
775 (wrap (cons args (cons e1 e2)) w mod))
776 s)
777 '(())
778 s
779 mod))
780 tmp-1)
781 (let ((tmp-1 ($sc-dispatch tmp '(_ any))))
782 (if (and tmp-1 (apply (lambda (name) (id? name)) tmp-1))
783 (apply (lambda (name)
784 (values
785 'define-form
786 (wrap name w mod)
787 (wrap e w mod)
788 '(#(syntax-object if ((top)) (hygiene guile)) #f #f)
789 '(())
790 s
791 mod))
792 tmp-1)
793 (syntax-violation
794 #f
795 "source expression failed to match any pattern"
796 tmp))))))))
797 ((memv key '(define-syntax))
798 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ any any))))
799 (if (and tmp (apply (lambda (name val) (id? name)) tmp))
800 (apply (lambda (name val) (values 'define-syntax-form name e val w s mod))
801 tmp)
802 (syntax-violation
803 #f
804 "source expression failed to match any pattern"
805 tmp-1))))
806 ((memv key '(define-syntax-parameter))
807 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ any any))))
808 (if (and tmp (apply (lambda (name val) (id? name)) tmp))
809 (apply (lambda (name val)
810 (values 'define-syntax-parameter-form name e val w s mod))
811 tmp)
812 (syntax-violation
813 #f
814 "source expression failed to match any pattern"
815 tmp-1))))
816 (else (values 'call #f e e w s mod))))))))
817 ((syntax-object? e)
818 (syntax-type
819 (syntax-object-expression e)
820 r
821 (join-wraps w (syntax-object-wrap e))
822 (or (source-annotation e) s)
823 rib
824 (or (syntax-object-module e) mod)
825 for-car?))
826 ((self-evaluating? e) (values 'constant #f e e w s mod))
827 (else (values 'other #f e e w s mod)))))
828 (expand
829 (lambda (e r w mod)
830 (call-with-values
831 (lambda () (syntax-type e r w (source-annotation e) #f mod #f))
832 (lambda (type value form e w s mod)
833 (expand-expr type value form e r w s mod)))))
834 (expand-expr
835 (lambda (type value form e r w s mod)
836 (let ((key type))
837 (cond ((memv key '(lexical)) (build-lexical-reference 'value s e value))
838 ((memv key '(core core-form)) (value e r w s mod))
839 ((memv key '(module-ref))
840 (call-with-values
841 (lambda () (value e r w mod))
842 (lambda (e r w s mod) (expand e r w mod))))
843 ((memv key '(lexical-call))
844 (expand-call
845 (let ((id (car e)))
846 (build-lexical-reference
847 'fun
848 (source-annotation id)
849 (if (syntax-object? id) (syntax->datum id) id)
850 value))
851 e
852 r
853 w
854 s
855 mod))
856 ((memv key '(global-call))
857 (expand-call
858 (build-global-reference
859 (source-annotation (car e))
860 (if (syntax-object? value) (syntax-object-expression value) value)
861 (if (syntax-object? value) (syntax-object-module value) mod))
862 e
863 r
864 w
865 s
866 mod))
867 ((memv key '(primitive-call))
868 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ . each-any))))
869 (if tmp
870 (apply (lambda (e)
871 (build-primcall s value (map (lambda (e) (expand e r w mod)) e)))
872 tmp)
873 (syntax-violation
874 #f
875 "source expression failed to match any pattern"
876 tmp-1))))
877 ((memv key '(constant))
878 (build-data s (strip (source-wrap e w s mod) '(()))))
879 ((memv key '(global)) (build-global-reference s value mod))
880 ((memv key '(call))
881 (expand-call (expand (car e) r w mod) e r w s mod))
882 ((memv key '(begin-form))
883 (let* ((tmp e) (tmp-1 ($sc-dispatch tmp '(_ any . each-any))))
884 (if tmp-1
885 (apply (lambda (e1 e2) (expand-sequence (cons e1 e2) r w s mod))
886 tmp-1)
887 (let ((tmp-1 ($sc-dispatch tmp '(_))))
888 (if tmp-1
889 (apply (lambda ()
890 (syntax-violation
891 #f
892 "sequence of zero expressions"
893 (source-wrap e w s mod)))
894 tmp-1)
895 (syntax-violation
896 #f
897 "source expression failed to match any pattern"
898 tmp))))))
899 ((memv key '(local-syntax-form))
900 (expand-local-syntax value e r w s mod expand-sequence))
901 ((memv key '(eval-when-form))
902 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ each-any any . each-any))))
903 (if tmp
904 (apply (lambda (x e1 e2)
905 (let ((when-list (parse-when-list e x)))
906 (if (memq 'eval when-list)
907 (expand-sequence (cons e1 e2) r w s mod)
908 (expand-void))))
909 tmp)
910 (syntax-violation
911 #f
912 "source expression failed to match any pattern"
913 tmp-1))))
914 ((memv key
915 '(define-form define-syntax-form define-syntax-parameter-form))
916 (syntax-violation
917 #f
918 "definition in expression context, where definitions are not allowed,"
919 (source-wrap form w s mod)))
920 ((memv key '(syntax))
921 (syntax-violation
922 #f
923 "reference to pattern variable outside syntax form"
924 (source-wrap e w s mod)))
925 ((memv key '(displaced-lexical))
926 (syntax-violation
927 #f
928 "reference to identifier outside its scope"
929 (source-wrap e w s mod)))
930 (else
931 (syntax-violation #f "unexpected syntax" (source-wrap e w s mod)))))))
932 (expand-call
933 (lambda (x e r w s mod)
934 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(any . each-any))))
935 (if tmp
936 (apply (lambda (e0 e1)
937 (build-call s x (map (lambda (e) (expand e r w mod)) e1)))
938 tmp)
939 (syntax-violation
940 #f
941 "source expression failed to match any pattern"
942 tmp-1)))))
943 (expand-macro
944 (lambda (p e r w s rib mod)
945 (letrec*
946 ((rebuild-macro-output
947 (lambda (x m)
948 (cond ((pair? x)
949 (decorate-source
950 (cons (rebuild-macro-output (car x) m)
951 (rebuild-macro-output (cdr x) m))
952 s))
953 ((syntax-object? x)
954 (let ((w (syntax-object-wrap x)))
955 (let ((ms (car w)) (ss (cdr w)))
956 (if (and (pair? ms) (eq? (car ms) #f))
957 (make-syntax-object
958 (syntax-object-expression x)
959 (cons (cdr ms) (if rib (cons rib (cdr ss)) (cdr ss)))
960 (syntax-object-module x))
961 (make-syntax-object
962 (decorate-source (syntax-object-expression x) s)
963 (cons (cons m ms)
964 (if rib (cons rib (cons 'shift ss)) (cons 'shift ss)))
965 (syntax-object-module x))))))
966 ((vector? x)
967 (let* ((n (vector-length x)) (v (decorate-source (make-vector n) s)))
968 (let loop ((i 0))
969 (if (= i n)
970 (begin (if #f #f) v)
971 (begin
972 (vector-set! v i (rebuild-macro-output (vector-ref x i) m))
973 (loop (+ i 1)))))))
974 ((symbol? x)
975 (syntax-violation
976 #f
977 "encountered raw symbol in macro output"
978 (source-wrap e w (cdr w) mod)
979 x))
980 (else (decorate-source x s))))))
981 (let* ((t-1 transformer-environment) (t (lambda (k) (k e r w s rib mod))))
982 (with-fluid*
983 t-1
984 t
985 (lambda ()
986 (rebuild-macro-output
987 (p (source-wrap e (anti-mark w) s mod))
988 (gensym (string-append "m-" (session-id) "-")))))))))
989 (expand-body
990 (lambda (body outer-form r w mod)
991 (let* ((r (cons '("placeholder" placeholder) r))
992 (ribcage (make-ribcage '() '() '()))
993 (w (cons (car w) (cons ribcage (cdr w)))))
994 (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
995 (ids '())
996 (labels '())
997 (var-ids '())
998 (vars '())
999 (vals '())
1000 (bindings '()))
1001 (if (null? body)
1002 (syntax-violation #f "no expressions in body" outer-form)
1003 (let ((e (cdar body)) (er (caar body)))
1004 (call-with-values
1005 (lambda ()
1006 (syntax-type e er '(()) (source-annotation e) ribcage mod #f))
1007 (lambda (type value form e w s mod)
1008 (let ((key type))
1009 (cond ((memv key '(define-form))
1010 (let ((id (wrap value w mod)) (label (gen-label)))
1011 (let ((var (gen-var id)))
1012 (extend-ribcage! ribcage id label)
1013 (parse (cdr body)
1014 (cons id ids)
1015 (cons label labels)
1016 (cons id var-ids)
1017 (cons var vars)
1018 (cons (cons er (wrap e w mod)) vals)
1019 (cons (cons 'lexical var) bindings)))))
1020 ((memv key '(define-syntax-form))
1021 (let ((id (wrap value w mod))
1022 (label (gen-label))
1023 (trans-r (macros-only-env er)))
1024 (extend-ribcage! ribcage id label)
1025 (set-cdr!
1026 r
1027 (extend-env
1028 (list label)
1029 (list (cons 'macro (eval-local-transformer (expand e trans-r w mod) mod)))
1030 (cdr r)))
1031 (parse (cdr body) (cons id ids) labels var-ids vars vals bindings)))
1032 ((memv key '(define-syntax-parameter-form))
1033 (let ((id (wrap value w mod))
1034 (label (gen-label))
1035 (trans-r (macros-only-env er)))
1036 (extend-ribcage! ribcage id label)
1037 (set-cdr!
1038 r
1039 (extend-env
1040 (list label)
1041 (list (cons 'syntax-parameter
1042 (list (eval-local-transformer (expand e trans-r w mod) mod))))
1043 (cdr r)))
1044 (parse (cdr body) (cons id ids) labels var-ids vars vals bindings)))
1045 ((memv key '(begin-form))
1046 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ . each-any))))
1047 (if tmp
1048 (apply (lambda (e1)
1049 (parse (let f ((forms e1))
1050 (if (null? forms)
1051 (cdr body)
1052 (cons (cons er (wrap (car forms) w mod)) (f (cdr forms)))))
1053 ids
1054 labels
1055 var-ids
1056 vars
1057 vals
1058 bindings))
1059 tmp)
1060 (syntax-violation
1061 #f
1062 "source expression failed to match any pattern"
1063 tmp-1))))
1064 ((memv key '(local-syntax-form))
1065 (expand-local-syntax
1066 value
1067 e
1068 er
1069 w
1070 s
1071 mod
1072 (lambda (forms er w s mod)
1073 (parse (let f ((forms forms))
1074 (if (null? forms)
1075 (cdr body)
1076 (cons (cons er (wrap (car forms) w mod)) (f (cdr forms)))))
1077 ids
1078 labels
1079 var-ids
1080 vars
1081 vals
1082 bindings))))
1083 ((null? ids)
1084 (build-sequence
1085 #f
1086 (map (lambda (x) (expand (cdr x) (car x) '(()) mod))
1087 (cons (cons er (source-wrap e w s mod)) (cdr body)))))
1088 (else
1089 (if (not (valid-bound-ids? ids))
1090 (syntax-violation
1091 #f
1092 "invalid or duplicate identifier in definition"
1093 outer-form))
1094 (set-cdr! r (extend-env labels bindings (cdr r)))
1095 (build-letrec
1096 #f
1097 #t
1098 (reverse (map syntax->datum var-ids))
1099 (reverse vars)
1100 (map (lambda (x) (expand (cdr x) (car x) '(()) mod)) (reverse vals))
1101 (build-sequence
1102 #f
1103 (map (lambda (x) (expand (cdr x) (car x) '(()) mod))
1104 (cons (cons er (source-wrap e w s mod)) (cdr body))))))))))))))))
1105 (expand-local-syntax
1106 (lambda (rec? e r w s mod k)
1107 (let* ((tmp e)
1108 (tmp ($sc-dispatch tmp '(_ #(each (any any)) any . each-any))))
1109 (if tmp
1110 (apply (lambda (id val e1 e2)
1111 (let ((ids id))
1112 (if (not (valid-bound-ids? ids))
1113 (syntax-violation #f "duplicate bound keyword" e)
1114 (let* ((labels (gen-labels ids)) (new-w (make-binding-wrap ids labels w)))
1115 (k (cons e1 e2)
1116 (extend-env
1117 labels
1118 (let ((w (if rec? new-w w)) (trans-r (macros-only-env r)))
1119 (map (lambda (x)
1120 (cons 'macro (eval-local-transformer (expand x trans-r w mod) mod)))
1121 val))
1122 r)
1123 new-w
1124 s
1125 mod)))))
1126 tmp)
1127 (syntax-violation
1128 #f
1129 "bad local syntax definition"
1130 (source-wrap e w s mod))))))
1131 (eval-local-transformer
1132 (lambda (expanded mod)
1133 (let ((p (local-eval-hook expanded mod)))
1134 (if (procedure? p)
1135 p
1136 (syntax-violation #f "nonprocedure transformer" p)))))
1137 (expand-void (lambda () (build-void #f)))
1138 (ellipsis?
1139 (lambda (e r mod)
1140 (and (nonsymbol-id? e)
1141 (call-with-values
1142 (lambda ()
1143 (resolve-identifier
1144 (make-syntax-object
1145 '#{ $sc-ellipsis }#
1146 (syntax-object-wrap e)
1147 (syntax-object-module e))
1148 '(())
1149 r
1150 mod
1151 #f))
1152 (lambda (type value mod)
1153 (if (eq? type 'ellipsis)
1154 (bound-id=? e value)
1155 (free-id=? e '#(syntax-object ... ((top)) (hygiene guile)))))))))
1156 (lambda-formals
1157 (lambda (orig-args)
1158 (letrec*
1159 ((req (lambda (args rreq)
1160 (let* ((tmp args) (tmp-1 ($sc-dispatch tmp '())))
1161 (if tmp-1
1162 (apply (lambda () (check (reverse rreq) #f)) tmp-1)
1163 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1164 (if (and tmp-1 (apply (lambda (a b) (id? a)) tmp-1))
1165 (apply (lambda (a b) (req b (cons a rreq))) tmp-1)
1166 (let ((tmp-1 (list tmp)))
1167 (if (and tmp-1 (apply (lambda (r) (id? r)) tmp-1))
1168 (apply (lambda (r) (check (reverse rreq) r)) tmp-1)
1169 (let ((else tmp))
1170 (syntax-violation 'lambda "invalid argument list" orig-args args))))))))))
1171 (check (lambda (req rest)
1172 (if (distinct-bound-ids? (if rest (cons rest req) req))
1173 (values req #f rest #f)
1174 (syntax-violation
1175 'lambda
1176 "duplicate identifier in argument list"
1177 orig-args)))))
1178 (req orig-args '()))))
1179 (expand-simple-lambda
1180 (lambda (e r w s mod req rest meta body)
1181 (let* ((ids (if rest (append req (list rest)) req))
1182 (vars (map gen-var ids))
1183 (labels (gen-labels ids)))
1184 (build-simple-lambda
1185 s
1186 (map syntax->datum req)
1187 (and rest (syntax->datum rest))
1188 vars
1189 meta
1190 (expand-body
1191 body
1192 (source-wrap e w s mod)
1193 (extend-var-env labels vars r)
1194 (make-binding-wrap ids labels w)
1195 mod)))))
1196 (lambda*-formals
1197 (lambda (orig-args)
1198 (letrec*
1199 ((req (lambda (args rreq)
1200 (let* ((tmp args) (tmp-1 ($sc-dispatch tmp '())))
1201 (if tmp-1
1202 (apply (lambda () (check (reverse rreq) '() #f '())) tmp-1)
1203 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1204 (if (and tmp-1 (apply (lambda (a b) (id? a)) tmp-1))
1205 (apply (lambda (a b) (req b (cons a rreq))) tmp-1)
1206 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1207 (if (and tmp-1
1208 (apply (lambda (a b) (eq? (syntax->datum a) #:optional)) tmp-1))
1209 (apply (lambda (a b) (opt b (reverse rreq) '())) tmp-1)
1210 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1211 (if (and tmp-1
1212 (apply (lambda (a b) (eq? (syntax->datum a) #:key)) tmp-1))
1213 (apply (lambda (a b) (key b (reverse rreq) '() '())) tmp-1)
1214 (let ((tmp-1 ($sc-dispatch tmp '(any any))))
1215 (if (and tmp-1
1216 (apply (lambda (a b) (eq? (syntax->datum a) #:rest)) tmp-1))
1217 (apply (lambda (a b) (rest b (reverse rreq) '() '())) tmp-1)
1218 (let ((tmp-1 (list tmp)))
1219 (if (and tmp-1 (apply (lambda (r) (id? r)) tmp-1))
1220 (apply (lambda (r) (rest r (reverse rreq) '() '())) tmp-1)
1221 (let ((else tmp))
1222 (syntax-violation
1223 'lambda*
1224 "invalid argument list"
1225 orig-args
1226 args))))))))))))))))
1227 (opt (lambda (args req ropt)
1228 (let* ((tmp args) (tmp-1 ($sc-dispatch tmp '())))
1229 (if tmp-1
1230 (apply (lambda () (check req (reverse ropt) #f '())) tmp-1)
1231 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1232 (if (and tmp-1 (apply (lambda (a b) (id? a)) tmp-1))
1233 (apply (lambda (a b) (opt b req (cons (cons a '(#f)) ropt))) tmp-1)
1234 (let ((tmp-1 ($sc-dispatch tmp '((any any) . any))))
1235 (if (and tmp-1 (apply (lambda (a init b) (id? a)) tmp-1))
1236 (apply (lambda (a init b) (opt b req (cons (list a init) ropt)))
1237 tmp-1)
1238 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1239 (if (and tmp-1
1240 (apply (lambda (a b) (eq? (syntax->datum a) #:key)) tmp-1))
1241 (apply (lambda (a b) (key b req (reverse ropt) '())) tmp-1)
1242 (let ((tmp-1 ($sc-dispatch tmp '(any any))))
1243 (if (and tmp-1
1244 (apply (lambda (a b) (eq? (syntax->datum a) #:rest)) tmp-1))
1245 (apply (lambda (a b) (rest b req (reverse ropt) '())) tmp-1)
1246 (let ((tmp-1 (list tmp)))
1247 (if (and tmp-1 (apply (lambda (r) (id? r)) tmp-1))
1248 (apply (lambda (r) (rest r req (reverse ropt) '())) tmp-1)
1249 (let ((else tmp))
1250 (syntax-violation
1251 'lambda*
1252 "invalid optional argument list"
1253 orig-args
1254 args))))))))))))))))
1255 (key (lambda (args req opt rkey)
1256 (let* ((tmp args) (tmp-1 ($sc-dispatch tmp '())))
1257 (if tmp-1
1258 (apply (lambda () (check req opt #f (cons #f (reverse rkey)))) tmp-1)
1259 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1260 (if (and tmp-1 (apply (lambda (a b) (id? a)) tmp-1))
1261 (apply (lambda (a b)
1262 (let* ((tmp (symbol->keyword (syntax->datum a))) (k tmp))
1263 (key b req opt (cons (cons k (cons a '(#f))) rkey))))
1264 tmp-1)
1265 (let ((tmp-1 ($sc-dispatch tmp '((any any) . any))))
1266 (if (and tmp-1 (apply (lambda (a init b) (id? a)) tmp-1))
1267 (apply (lambda (a init b)
1268 (let* ((tmp (symbol->keyword (syntax->datum a))) (k tmp))
1269 (key b req opt (cons (list k a init) rkey))))
1270 tmp-1)
1271 (let ((tmp-1 ($sc-dispatch tmp '((any any any) . any))))
1272 (if (and tmp-1
1273 (apply (lambda (a init k b) (and (id? a) (keyword? (syntax->datum k))))
1274 tmp-1))
1275 (apply (lambda (a init k b) (key b req opt (cons (list k a init) rkey)))
1276 tmp-1)
1277 (let ((tmp-1 ($sc-dispatch tmp '(any))))
1278 (if (and tmp-1
1279 (apply (lambda (aok) (eq? (syntax->datum aok) #:allow-other-keys))
1280 tmp-1))
1281 (apply (lambda (aok) (check req opt #f (cons #t (reverse rkey))))
1282 tmp-1)
1283 (let ((tmp-1 ($sc-dispatch tmp '(any any any))))
1284 (if (and tmp-1
1285 (apply (lambda (aok a b)
1286 (and (eq? (syntax->datum aok) #:allow-other-keys)
1287 (eq? (syntax->datum a) #:rest)))
1288 tmp-1))
1289 (apply (lambda (aok a b) (rest b req opt (cons #t (reverse rkey))))
1290 tmp-1)
1291 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1292 (if (and tmp-1
1293 (apply (lambda (aok r)
1294 (and (eq? (syntax->datum aok) #:allow-other-keys) (id? r)))
1295 tmp-1))
1296 (apply (lambda (aok r) (rest r req opt (cons #t (reverse rkey))))
1297 tmp-1)
1298 (let ((tmp-1 ($sc-dispatch tmp '(any any))))
1299 (if (and tmp-1
1300 (apply (lambda (a b) (eq? (syntax->datum a) #:rest)) tmp-1))
1301 (apply (lambda (a b) (rest b req opt (cons #f (reverse rkey))))
1302 tmp-1)
1303 (let ((tmp-1 (list tmp)))
1304 (if (and tmp-1 (apply (lambda (r) (id? r)) tmp-1))
1305 (apply (lambda (r) (rest r req opt (cons #f (reverse rkey))))
1306 tmp-1)
1307 (let ((else tmp))
1308 (syntax-violation
1309 'lambda*
1310 "invalid keyword argument list"
1311 orig-args
1312 args))))))))))))))))))))))
1313 (rest (lambda (args req opt kw)
1314 (let* ((tmp-1 args) (tmp (list tmp-1)))
1315 (if (and tmp (apply (lambda (r) (id? r)) tmp))
1316 (apply (lambda (r) (check req opt r kw)) tmp)
1317 (let ((else tmp-1))
1318 (syntax-violation 'lambda* "invalid rest argument" orig-args args))))))
1319 (check (lambda (req opt rest kw)
1320 (if (distinct-bound-ids?
1321 (append
1322 req
1323 (map car opt)
1324 (if rest (list rest) '())
1325 (if (pair? kw) (map cadr (cdr kw)) '())))
1326 (values req opt rest kw)
1327 (syntax-violation
1328 'lambda*
1329 "duplicate identifier in argument list"
1330 orig-args)))))
1331 (req orig-args '()))))
1332 (expand-lambda-case
1333 (lambda (e r w s mod get-formals clauses)
1334 (letrec*
1335 ((parse-req
1336 (lambda (req opt rest kw body)
1337 (let ((vars (map gen-var req)) (labels (gen-labels req)))
1338 (let ((r* (extend-var-env labels vars r))
1339 (w* (make-binding-wrap req labels w)))
1340 (parse-opt
1341 (map syntax->datum req)
1342 opt
1343 rest
1344 kw
1345 body
1346 (reverse vars)
1347 r*
1348 w*
1349 '()
1350 '())))))
1351 (parse-opt
1352 (lambda (req opt rest kw body vars r* w* out inits)
1353 (cond ((pair? opt)
1354 (let* ((tmp-1 (car opt)) (tmp ($sc-dispatch tmp-1 '(any any))))
1355 (if tmp
1356 (apply (lambda (id i)
1357 (let* ((v (gen-var id))
1358 (l (gen-labels (list v)))
1359 (r** (extend-var-env l (list v) r*))
1360 (w** (make-binding-wrap (list id) l w*)))
1361 (parse-opt
1362 req
1363 (cdr opt)
1364 rest
1365 kw
1366 body
1367 (cons v vars)
1368 r**
1369 w**
1370 (cons (syntax->datum id) out)
1371 (cons (expand i r* w* mod) inits))))
1372 tmp)
1373 (syntax-violation
1374 #f
1375 "source expression failed to match any pattern"
1376 tmp-1))))
1377 (rest
1378 (let* ((v (gen-var rest))
1379 (l (gen-labels (list v)))
1380 (r* (extend-var-env l (list v) r*))
1381 (w* (make-binding-wrap (list rest) l w*)))
1382 (parse-kw
1383 req
1384 (and (pair? out) (reverse out))
1385 (syntax->datum rest)
1386 (if (pair? kw) (cdr kw) kw)
1387 body
1388 (cons v vars)
1389 r*
1390 w*
1391 (and (pair? kw) (car kw))
1392 '()
1393 inits)))
1394 (else
1395 (parse-kw
1396 req
1397 (and (pair? out) (reverse out))
1398 #f
1399 (if (pair? kw) (cdr kw) kw)
1400 body
1401 vars
1402 r*
1403 w*
1404 (and (pair? kw) (car kw))
1405 '()
1406 inits)))))
1407 (parse-kw
1408 (lambda (req opt rest kw body vars r* w* aok out inits)
1409 (if (pair? kw)
1410 (let* ((tmp-1 (car kw)) (tmp ($sc-dispatch tmp-1 '(any any any))))
1411 (if tmp
1412 (apply (lambda (k id i)
1413 (let* ((v (gen-var id))
1414 (l (gen-labels (list v)))
1415 (r** (extend-var-env l (list v) r*))
1416 (w** (make-binding-wrap (list id) l w*)))
1417 (parse-kw
1418 req
1419 opt
1420 rest
1421 (cdr kw)
1422 body
1423 (cons v vars)
1424 r**
1425 w**
1426 aok
1427 (cons (list (syntax->datum k) (syntax->datum id) v) out)
1428 (cons (expand i r* w* mod) inits))))
1429 tmp)
1430 (syntax-violation
1431 #f
1432 "source expression failed to match any pattern"
1433 tmp-1)))
1434 (parse-body
1435 req
1436 opt
1437 rest
1438 (and (or aok (pair? out)) (cons aok (reverse out)))
1439 body
1440 (reverse vars)
1441 r*
1442 w*
1443 (reverse inits)
1444 '()))))
1445 (parse-body
1446 (lambda (req opt rest kw body vars r* w* inits meta)
1447 (let* ((tmp body) (tmp-1 ($sc-dispatch tmp '(any any . each-any))))
1448 (if (and tmp-1
1449 (apply (lambda (docstring e1 e2) (string? (syntax->datum docstring)))
1450 tmp-1))
1451 (apply (lambda (docstring e1 e2)
1452 (parse-body
1453 req
1454 opt
1455 rest
1456 kw
1457 (cons e1 e2)
1458 vars
1459 r*
1460 w*
1461 inits
1462 (append meta (list (cons 'documentation (syntax->datum docstring))))))
1463 tmp-1)
1464 (let ((tmp-1 ($sc-dispatch tmp '(#(vector #(each (any . any))) any . each-any))))
1465 (if tmp-1
1466 (apply (lambda (k v e1 e2)
1467 (parse-body
1468 req
1469 opt
1470 rest
1471 kw
1472 (cons e1 e2)
1473 vars
1474 r*
1475 w*
1476 inits
1477 (append meta (syntax->datum (map cons k v)))))
1478 tmp-1)
1479 (let ((tmp-1 ($sc-dispatch tmp '(any . each-any))))
1480 (if tmp-1
1481 (apply (lambda (e1 e2)
1482 (values
1483 meta
1484 req
1485 opt
1486 rest
1487 kw
1488 inits
1489 vars
1490 (expand-body (cons e1 e2) (source-wrap e w s mod) r* w* mod)))
1491 tmp-1)
1492 (syntax-violation
1493 #f
1494 "source expression failed to match any pattern"
1495 tmp))))))))))
1496 (let* ((tmp clauses) (tmp-1 ($sc-dispatch tmp '())))
1497 (if tmp-1
1498 (apply (lambda () (values '() #f)) tmp-1)
1499 (let ((tmp-1 ($sc-dispatch
1500 tmp
1501 '((any any . each-any) . #(each (any any . each-any))))))
1502 (if tmp-1
1503 (apply (lambda (args e1 e2 args* e1* e2*)
1504 (call-with-values
1505 (lambda () (get-formals args))
1506 (lambda (req opt rest kw)
1507 (call-with-values
1508 (lambda () (parse-req req opt rest kw (cons e1 e2)))
1509 (lambda (meta req opt rest kw inits vars body)
1510 (call-with-values
1511 (lambda ()
1512 (expand-lambda-case
1513 e
1514 r
1515 w
1516 s
1517 mod
1518 get-formals
1519 (map (lambda (tmp-2 tmp-1 tmp) (cons tmp (cons tmp-1 tmp-2)))
1520 e2*
1521 e1*
1522 args*)))
1523 (lambda (meta* else*)
1524 (values
1525 (append meta meta*)
1526 (build-lambda-case s req opt rest kw inits vars body else*)))))))))
1527 tmp-1)
1528 (syntax-violation
1529 #f
1530 "source expression failed to match any pattern"
1531 tmp))))))))
1532 (strip (lambda (x w)
1533 (if (memq 'top (car w))
1534 x
1535 (let f ((x x))
1536 (cond ((syntax-object? x)
1537 (strip (syntax-object-expression x) (syntax-object-wrap x)))
1538 ((pair? x)
1539 (let ((a (f (car x))) (d (f (cdr x))))
1540 (if (and (eq? a (car x)) (eq? d (cdr x))) x (cons a d))))
1541 ((vector? x)
1542 (let* ((old (vector->list x)) (new (map f old)))
1543 (let lp ((l1 old) (l2 new))
1544 (cond ((null? l1) x)
1545 ((eq? (car l1) (car l2)) (lp (cdr l1) (cdr l2)))
1546 (else (list->vector new))))))
1547 (else x))))))
1548 (gen-var
1549 (lambda (id)
1550 (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
1551 (gensym (string-append (symbol->string id) "-")))))
1552 (lambda-var-list
1553 (lambda (vars)
1554 (let lvl ((vars vars) (ls '()) (w '(())))
1555 (cond ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
1556 ((id? vars) (cons (wrap vars w #f) ls))
1557 ((null? vars) ls)
1558 ((syntax-object? vars)
1559 (lvl (syntax-object-expression vars)
1560 ls
1561 (join-wraps w (syntax-object-wrap vars))))
1562 (else (cons vars ls)))))))
1563 (global-extend 'local-syntax 'letrec-syntax #t)
1564 (global-extend 'local-syntax 'let-syntax #f)
1565 (global-extend
1566 'core
1567 'syntax-parameterize
1568 (lambda (e r w s mod)
1569 (let* ((tmp e)
1570 (tmp ($sc-dispatch tmp '(_ #(each (any any)) any . each-any))))
1571 (if (and tmp (apply (lambda (var val e1 e2) (valid-bound-ids? var)) tmp))
1572 (apply (lambda (var val e1 e2)
1573 (let ((names (map (lambda (x)
1574 (call-with-values
1575 (lambda () (resolve-identifier x w r mod #f))
1576 (lambda (type value mod)
1577 (let ((key type))
1578 (cond ((memv key '(displaced-lexical))
1579 (syntax-violation
1580 'syntax-parameterize
1581 "identifier out of context"
1582 e
1583 (source-wrap x w s mod)))
1584 ((memv key '(syntax-parameter)) value)
1585 (else
1586 (syntax-violation
1587 'syntax-parameterize
1588 "invalid syntax parameter"
1589 e
1590 (source-wrap x w s mod))))))))
1591 var))
1592 (bindings
1593 (let ((trans-r (macros-only-env r)))
1594 (map (lambda (x)
1595 (cons 'macro (eval-local-transformer (expand x trans-r w mod) mod)))
1596 val))))
1597 (expand-body
1598 (cons e1 e2)
1599 (source-wrap e w s mod)
1600 (extend-env names bindings r)
1601 w
1602 mod)))
1603 tmp)
1604 (syntax-violation
1605 'syntax-parameterize
1606 "bad syntax"
1607 (source-wrap e w s mod))))))
1608 (global-extend
1609 'core
1610 'quote
1611 (lambda (e r w s mod)
1612 (let* ((tmp e) (tmp ($sc-dispatch tmp '(_ any))))
1613 (if tmp
1614 (apply (lambda (e) (build-data s (strip e w))) tmp)
1615 (syntax-violation 'quote "bad syntax" (source-wrap e w s mod))))))
1616 (global-extend
1617 'core
1618 'syntax
1619 (letrec*
1620 ((gen-syntax
1621 (lambda (src e r maps ellipsis? mod)
1622 (if (id? e)
1623 (call-with-values
1624 (lambda () (resolve-identifier e '(()) r mod #f))
1625 (lambda (type value mod)
1626 (let ((key type))
1627 (cond ((memv key '(syntax))
1628 (call-with-values
1629 (lambda () (gen-ref src (car value) (cdr value) maps))
1630 (lambda (var maps) (values (list 'ref var) maps))))
1631 ((ellipsis? e r mod)
1632 (syntax-violation 'syntax "misplaced ellipsis" src))
1633 (else (values (list 'quote e) maps))))))
1634 (let* ((tmp e) (tmp-1 ($sc-dispatch tmp '(any any))))
1635 (if (and tmp-1 (apply (lambda (dots e) (ellipsis? dots r mod)) tmp-1))
1636 (apply (lambda (dots e) (gen-syntax src e r maps (lambda (e r mod) #f) mod))
1637 tmp-1)
1638 (let ((tmp-1 ($sc-dispatch tmp '(any any . any))))
1639 (if (and tmp-1 (apply (lambda (x dots y) (ellipsis? dots r mod)) tmp-1))
1640 (apply (lambda (x dots y)
1641 (let f ((y y)
1642 (k (lambda (maps)
1643 (call-with-values
1644 (lambda () (gen-syntax src x r (cons '() maps) ellipsis? mod))
1645 (lambda (x maps)
1646 (if (null? (car maps))
1647 (syntax-violation 'syntax "extra ellipsis" src)
1648 (values (gen-map x (car maps)) (cdr maps))))))))
1649 (let* ((tmp y) (tmp ($sc-dispatch tmp '(any . any))))
1650 (if (and tmp (apply (lambda (dots y) (ellipsis? dots r mod)) tmp))
1651 (apply (lambda (dots y)
1652 (f y
1653 (lambda (maps)
1654 (call-with-values
1655 (lambda () (k (cons '() maps)))
1656 (lambda (x maps)
1657 (if (null? (car maps))
1658 (syntax-violation 'syntax "extra ellipsis" src)
1659 (values (gen-mappend x (car maps)) (cdr maps))))))))
1660 tmp)
1661 (call-with-values
1662 (lambda () (gen-syntax src y r maps ellipsis? mod))
1663 (lambda (y maps)
1664 (call-with-values
1665 (lambda () (k maps))
1666 (lambda (x maps) (values (gen-append x y) maps)))))))))
1667 tmp-1)
1668 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
1669 (if tmp-1
1670 (apply (lambda (x y)
1671 (call-with-values
1672 (lambda () (gen-syntax src x r maps ellipsis? mod))
1673 (lambda (x maps)
1674 (call-with-values
1675 (lambda () (gen-syntax src y r maps ellipsis? mod))
1676 (lambda (y maps) (values (gen-cons x y) maps))))))
1677 tmp-1)
1678 (let ((tmp ($sc-dispatch tmp '#(vector (any . each-any)))))
1679 (if tmp
1680 (apply (lambda (e1 e2)
1681 (call-with-values
1682 (lambda () (gen-syntax src (cons e1 e2) r maps ellipsis? mod))
1683 (lambda (e maps) (values (gen-vector e) maps))))
1684 tmp)
1685 (values (list 'quote e) maps))))))))))))
1686 (gen-ref
1687 (lambda (src var level maps)
1688 (cond ((= level 0) (values var maps))
1689 ((null? maps) (syntax-violation 'syntax "missing ellipsis" src))
1690 (else
1691 (call-with-values
1692 (lambda () (gen-ref src var (- level 1) (cdr maps)))
1693 (lambda (outer-var outer-maps)
1694 (let ((b (assq outer-var (car maps))))
1695 (if b
1696 (values (cdr b) maps)
1697 (let ((inner-var (gen-var 'tmp)))
1698 (values
1699 inner-var
1700 (cons (cons (cons outer-var inner-var) (car maps)) outer-maps)))))))))))
1701 (gen-mappend
1702 (lambda (e map-env)
1703 (list 'apply '(primitive append) (gen-map e map-env))))
1704 (gen-map
1705 (lambda (e map-env)
1706 (let ((formals (map cdr map-env))
1707 (actuals (map (lambda (x) (list 'ref (car x))) map-env)))
1708 (cond ((eq? (car e) 'ref) (car actuals))
1709 ((and-map
1710 (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
1711 (cdr e))
1712 (cons 'map
1713 (cons (list 'primitive (car e))
1714 (map (let ((r (map cons formals actuals)))
1715 (lambda (x) (cdr (assq (cadr x) r))))
1716 (cdr e)))))
1717 (else (cons 'map (cons (list 'lambda formals e) actuals)))))))
1718 (gen-cons
1719 (lambda (x y)
1720 (let ((key (car y)))
1721 (cond ((memv key '(quote))
1722 (cond ((eq? (car x) 'quote) (list 'quote (cons (cadr x) (cadr y))))
1723 ((eq? (cadr y) '()) (list 'list x))
1724 (else (list 'cons x y))))
1725 ((memv key '(list)) (cons 'list (cons x (cdr y))))
1726 (else (list 'cons x y))))))
1727 (gen-append (lambda (x y) (if (equal? y ''()) x (list 'append x y))))
1728 (gen-vector
1729 (lambda (x)
1730 (cond ((eq? (car x) 'list) (cons 'vector (cdr x)))
1731 ((eq? (car x) 'quote) (list 'quote (list->vector (cadr x))))
1732 (else (list 'list->vector x)))))
1733 (regen (lambda (x)
1734 (let ((key (car x)))
1735 (cond ((memv key '(ref))
1736 (build-lexical-reference 'value #f (cadr x) (cadr x)))
1737 ((memv key '(primitive)) (build-primref #f (cadr x)))
1738 ((memv key '(quote)) (build-data #f (cadr x)))
1739 ((memv key '(lambda))
1740 (if (list? (cadr x))
1741 (build-simple-lambda #f (cadr x) #f (cadr x) '() (regen (caddr x)))
1742 (error "how did we get here" x)))
1743 (else (build-primcall #f (car x) (map regen (cdr x)))))))))
1744 (lambda (e r w s mod)
1745 (let* ((e (source-wrap e w s mod))
1746 (tmp e)
1747 (tmp ($sc-dispatch tmp '(_ any))))
1748 (if tmp
1749 (apply (lambda (x)
1750 (call-with-values
1751 (lambda () (gen-syntax e x r '() ellipsis? mod))
1752 (lambda (e maps) (regen e))))
1753 tmp)
1754 (syntax-violation 'syntax "bad `syntax' form" e))))))
1755 (global-extend
1756 'core
1757 'lambda
1758 (lambda (e r w s mod)
1759 (let* ((tmp e) (tmp ($sc-dispatch tmp '(_ any any . each-any))))
1760 (if tmp
1761 (apply (lambda (args e1 e2)
1762 (call-with-values
1763 (lambda () (lambda-formals args))
1764 (lambda (req opt rest kw)
1765 (let lp ((body (cons e1 e2)) (meta '()))
1766 (let* ((tmp-1 body) (tmp ($sc-dispatch tmp-1 '(any any . each-any))))
1767 (if (and tmp
1768 (apply (lambda (docstring e1 e2) (string? (syntax->datum docstring)))
1769 tmp))
1770 (apply (lambda (docstring e1 e2)
1771 (lp (cons e1 e2)
1772 (append meta (list (cons 'documentation (syntax->datum docstring))))))
1773 tmp)
1774 (let ((tmp ($sc-dispatch tmp-1 '(#(vector #(each (any . any))) any . each-any))))
1775 (if tmp
1776 (apply (lambda (k v e1 e2)
1777 (lp (cons e1 e2) (append meta (syntax->datum (map cons k v)))))
1778 tmp)
1779 (expand-simple-lambda e r w s mod req rest meta body)))))))))
1780 tmp)
1781 (syntax-violation 'lambda "bad lambda" e)))))
1782 (global-extend
1783 'core
1784 'lambda*
1785 (lambda (e r w s mod)
1786 (let* ((tmp e) (tmp ($sc-dispatch tmp '(_ any any . each-any))))
1787 (if tmp
1788 (apply (lambda (args e1 e2)
1789 (call-with-values
1790 (lambda ()
1791 (expand-lambda-case
1792 e
1793 r
1794 w
1795 s
1796 mod
1797 lambda*-formals
1798 (list (cons args (cons e1 e2)))))
1799 (lambda (meta lcase) (build-case-lambda s meta lcase))))
1800 tmp)
1801 (syntax-violation 'lambda "bad lambda*" e)))))
1802 (global-extend
1803 'core
1804 'case-lambda
1805 (lambda (e r w s mod)
1806 (letrec*
1807 ((build-it
1808 (lambda (meta clauses)
1809 (call-with-values
1810 (lambda () (expand-lambda-case e r w s mod lambda-formals clauses))
1811 (lambda (meta* lcase)
1812 (build-case-lambda s (append meta meta*) lcase))))))
1813 (let* ((tmp-1 e)
1814 (tmp ($sc-dispatch tmp-1 '(_ . #(each (any any . each-any))))))
1815 (if tmp
1816 (apply (lambda (args e1 e2)
1817 (build-it
1818 '()
1819 (map (lambda (tmp-2 tmp-1 tmp) (cons tmp (cons tmp-1 tmp-2)))
1820 e2
1821 e1
1822 args)))
1823 tmp)
1824 (let ((tmp ($sc-dispatch tmp-1 '(_ any . #(each (any any . each-any))))))
1825 (if (and tmp
1826 (apply (lambda (docstring args e1 e2) (string? (syntax->datum docstring)))
1827 tmp))
1828 (apply (lambda (docstring args e1 e2)
1829 (build-it
1830 (list (cons 'documentation (syntax->datum docstring)))
1831 (map (lambda (tmp-2 tmp-1 tmp) (cons tmp (cons tmp-1 tmp-2)))
1832 e2
1833 e1
1834 args)))
1835 tmp)
1836 (syntax-violation 'case-lambda "bad case-lambda" e))))))))
1837 (global-extend
1838 'core
1839 'case-lambda*
1840 (lambda (e r w s mod)
1841 (letrec*
1842 ((build-it
1843 (lambda (meta clauses)
1844 (call-with-values
1845 (lambda () (expand-lambda-case e r w s mod lambda*-formals clauses))
1846 (lambda (meta* lcase)
1847 (build-case-lambda s (append meta meta*) lcase))))))
1848 (let* ((tmp-1 e)
1849 (tmp ($sc-dispatch tmp-1 '(_ . #(each (any any . each-any))))))
1850 (if tmp
1851 (apply (lambda (args e1 e2)
1852 (build-it
1853 '()
1854 (map (lambda (tmp-2 tmp-1 tmp) (cons tmp (cons tmp-1 tmp-2)))
1855 e2
1856 e1
1857 args)))
1858 tmp)
1859 (let ((tmp ($sc-dispatch tmp-1 '(_ any . #(each (any any . each-any))))))
1860 (if (and tmp
1861 (apply (lambda (docstring args e1 e2) (string? (syntax->datum docstring)))
1862 tmp))
1863 (apply (lambda (docstring args e1 e2)
1864 (build-it
1865 (list (cons 'documentation (syntax->datum docstring)))
1866 (map (lambda (tmp-2 tmp-1 tmp) (cons tmp (cons tmp-1 tmp-2)))
1867 e2
1868 e1
1869 args)))
1870 tmp)
1871 (syntax-violation 'case-lambda "bad case-lambda*" e))))))))
1872 (global-extend
1873 'core
1874 'with-ellipsis
1875 (lambda (e r w s mod)
1876 (let* ((tmp e) (tmp ($sc-dispatch tmp '(_ any any . each-any))))
1877 (if (and tmp (apply (lambda (dots e1 e2) (id? dots)) tmp))
1878 (apply (lambda (dots e1 e2)
1879 (let ((id (if (symbol? dots)
1880 '#{ $sc-ellipsis }#
1881 (make-syntax-object
1882 '#{ $sc-ellipsis }#
1883 (syntax-object-wrap dots)
1884 (syntax-object-module dots)))))
1885 (let ((ids (list id))
1886 (labels (list (gen-label)))
1887 (bindings (list (cons 'ellipsis (source-wrap dots w s mod)))))
1888 (let ((nw (make-binding-wrap ids labels w))
1889 (nr (extend-env labels bindings r)))
1890 (expand-body (cons e1 e2) (source-wrap e nw s mod) nr nw mod)))))
1891 tmp)
1892 (syntax-violation
1893 'with-ellipsis
1894 "bad syntax"
1895 (source-wrap e w s mod))))))
1896 (global-extend
1897 'core
1898 'let
1899 (letrec*
1900 ((expand-let
1901 (lambda (e r w s mod constructor ids vals exps)
1902 (if (not (valid-bound-ids? ids))
1903 (syntax-violation 'let "duplicate bound variable" e)
1904 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
1905 (let ((nw (make-binding-wrap ids labels w))
1906 (nr (extend-var-env labels new-vars r)))
1907 (constructor
1908 s
1909 (map syntax->datum ids)
1910 new-vars
1911 (map (lambda (x) (expand x r w mod)) vals)
1912 (expand-body exps (source-wrap e nw s mod) nr nw mod))))))))
1913 (lambda (e r w s mod)
1914 (let* ((tmp-1 e)
1915 (tmp ($sc-dispatch tmp-1 '(_ #(each (any any)) any . each-any))))
1916 (if (and tmp (apply (lambda (id val e1 e2) (and-map id? id)) tmp))
1917 (apply (lambda (id val e1 e2)
1918 (expand-let e r w s mod build-let id val (cons e1 e2)))
1919 tmp)
1920 (let ((tmp ($sc-dispatch tmp-1 '(_ any #(each (any any)) any . each-any))))
1921 (if (and tmp
1922 (apply (lambda (f id val e1 e2) (and (id? f) (and-map id? id))) tmp))
1923 (apply (lambda (f id val e1 e2)
1924 (expand-let e r w s mod build-named-let (cons f id) val (cons e1 e2)))
1925 tmp)
1926 (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))))
1927 (global-extend
1928 'core
1929 'letrec
1930 (lambda (e r w s mod)
1931 (let* ((tmp e)
1932 (tmp ($sc-dispatch tmp '(_ #(each (any any)) any . each-any))))
1933 (if (and tmp (apply (lambda (id val e1 e2) (and-map id? id)) tmp))
1934 (apply (lambda (id val e1 e2)
1935 (let ((ids id))
1936 (if (not (valid-bound-ids? ids))
1937 (syntax-violation 'letrec "duplicate bound variable" e)
1938 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
1939 (let ((w (make-binding-wrap ids labels w))
1940 (r (extend-var-env labels new-vars r)))
1941 (build-letrec
1942 s
1943 #f
1944 (map syntax->datum ids)
1945 new-vars
1946 (map (lambda (x) (expand x r w mod)) val)
1947 (expand-body (cons e1 e2) (source-wrap e w s mod) r w mod)))))))
1948 tmp)
1949 (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
1950 (global-extend
1951 'core
1952 'letrec*
1953 (lambda (e r w s mod)
1954 (let* ((tmp e)
1955 (tmp ($sc-dispatch tmp '(_ #(each (any any)) any . each-any))))
1956 (if (and tmp (apply (lambda (id val e1 e2) (and-map id? id)) tmp))
1957 (apply (lambda (id val e1 e2)
1958 (let ((ids id))
1959 (if (not (valid-bound-ids? ids))
1960 (syntax-violation 'letrec* "duplicate bound variable" e)
1961 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
1962 (let ((w (make-binding-wrap ids labels w))
1963 (r (extend-var-env labels new-vars r)))
1964 (build-letrec
1965 s
1966 #t
1967 (map syntax->datum ids)
1968 new-vars
1969 (map (lambda (x) (expand x r w mod)) val)
1970 (expand-body (cons e1 e2) (source-wrap e w s mod) r w mod)))))))
1971 tmp)
1972 (syntax-violation 'letrec* "bad letrec*" (source-wrap e w s mod))))))
1973 (global-extend
1974 'core
1975 'set!
1976 (lambda (e r w s mod)
1977 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ any any))))
1978 (if (and tmp (apply (lambda (id val) (id? id)) tmp))
1979 (apply (lambda (id val)
1980 (call-with-values
1981 (lambda () (resolve-identifier id w r mod #t))
1982 (lambda (type value id-mod)
1983 (let ((key type))
1984 (cond ((memv key '(lexical))
1985 (build-lexical-assignment
1986 s
1987 (syntax->datum id)
1988 value
1989 (expand val r w mod)))
1990 ((memv key '(global))
1991 (build-global-assignment s value (expand val r w mod) id-mod))
1992 ((memv key '(macro))
1993 (if (procedure-property value 'variable-transformer)
1994 (expand (expand-macro value e r w s #f mod) r '(()) mod)
1995 (syntax-violation
1996 'set!
1997 "not a variable transformer"
1998 (wrap e w mod)
1999 (wrap id w id-mod))))
2000 ((memv key '(displaced-lexical))
2001 (syntax-violation 'set! "identifier out of context" (wrap id w mod)))
2002 (else (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))))
2003 tmp)
2004 (let ((tmp ($sc-dispatch tmp-1 '(_ (any . each-any) any))))
2005 (if tmp
2006 (apply (lambda (head tail val)
2007 (call-with-values
2008 (lambda () (syntax-type head r '(()) #f #f mod #t))
2009 (lambda (type value ee* ee ww ss modmod)
2010 (let ((key type))
2011 (if (memv key '(module-ref))
2012 (let ((val (expand val r w mod)))
2013 (call-with-values
2014 (lambda () (value (cons head tail) r w mod))
2015 (lambda (e r w s* mod)
2016 (let* ((tmp-1 e) (tmp (list tmp-1)))
2017 (if (and tmp (apply (lambda (e) (id? e)) tmp))
2018 (apply (lambda (e) (build-global-assignment s (syntax->datum e) val mod))
2019 tmp)
2020 (syntax-violation
2021 #f
2022 "source expression failed to match any pattern"
2023 tmp-1))))))
2024 (build-call
2025 s
2026 (expand
2027 (list '#(syntax-object setter ((top)) (hygiene guile)) head)
2028 r
2029 w
2030 mod)
2031 (map (lambda (e) (expand e r w mod)) (append tail (list val)))))))))
2032 tmp)
2033 (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))))
2034 (global-extend
2035 'module-ref
2036 '@
2037 (lambda (e r w mod)
2038 (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ each-any any))))
2039 (if (and tmp
2040 (apply (lambda (mod id) (and (and-map id? mod) (id? id))) tmp))
2041 (apply (lambda (mod id)
2042 (values
2043 (syntax->datum id)
2044 r
2045 '((top))
2046 #f
2047 (syntax->datum
2048 (cons '#(syntax-object public ((top)) (hygiene guile)) mod))))
2049 tmp)
2050 (syntax-violation
2051 #f
2052 "source expression failed to match any pattern"
2053 tmp-1)))))
2054 (global-extend
2055 'module-ref
2056 '@@
2057 (lambda (e r w mod)
2058 (letrec*
2059 ((remodulate
2060 (lambda (x mod)
2061 (cond ((pair? x) (cons (remodulate (car x) mod) (remodulate (cdr x) mod)))
2062 ((syntax-object? x)
2063 (make-syntax-object
2064 (remodulate (syntax-object-expression x) mod)
2065 (syntax-object-wrap x)
2066 mod))
2067 ((vector? x)
2068 (let* ((n (vector-length x)) (v (make-vector n)))
2069 (let loop ((i 0))
2070 (if (= i n)
2071 (begin (if #f #f) v)
2072 (begin
2073 (vector-set! v i (remodulate (vector-ref x i) mod))
2074 (loop (+ i 1)))))))
2075 (else x)))))
2076 (let* ((tmp e)
2077 (tmp-1 ($sc-dispatch
2078 tmp
2079 '(_ #(free-id #(syntax-object primitive ((top)) (hygiene guile))) any))))
2080 (if (and tmp-1
2081 (apply (lambda (id)
2082 (and (id? id)
2083 (equal?
2084 (cdr (if (syntax-object? id) (syntax-object-module id) mod))
2085 '(guile))))
2086 tmp-1))
2087 (apply (lambda (id) (values (syntax->datum id) r '((top)) #f '(primitive)))
2088 tmp-1)
2089 (let ((tmp-1 ($sc-dispatch tmp '(_ each-any any))))
2090 (if (and tmp-1
2091 (apply (lambda (mod id) (and (and-map id? mod) (id? id))) tmp-1))
2092 (apply (lambda (mod id)
2093 (values
2094 (syntax->datum id)
2095 r
2096 '((top))
2097 #f
2098 (syntax->datum
2099 (cons '#(syntax-object private ((top)) (hygiene guile)) mod))))
2100 tmp-1)
2101 (let ((tmp-1 ($sc-dispatch
2102 tmp
2103 '(_ #(free-id #(syntax-object @@ ((top)) (hygiene guile)))
2104 each-any
2105 any))))
2106 (if (and tmp-1 (apply (lambda (mod exp) (and-map id? mod)) tmp-1))
2107 (apply (lambda (mod exp)
2108 (let ((mod (syntax->datum
2109 (cons '#(syntax-object private ((top)) (hygiene guile)) mod))))
2110 (values (remodulate exp mod) r w (source-annotation exp) mod)))
2111 tmp-1)
2112 (syntax-violation
2113 #f
2114 "source expression failed to match any pattern"
2115 tmp))))))))))
2116 (global-extend
2117 'core
2118 'if
2119 (lambda (e r w s mod)
2120 (let* ((tmp e) (tmp-1 ($sc-dispatch tmp '(_ any any))))
2121 (if tmp-1
2122 (apply (lambda (test then)
2123 (build-conditional
2124 s
2125 (expand test r w mod)
2126 (expand then r w mod)
2127 (build-void #f)))
2128 tmp-1)
2129 (let ((tmp-1 ($sc-dispatch tmp '(_ any any any))))
2130 (if tmp-1
2131 (apply (lambda (test then else)
2132 (build-conditional
2133 s
2134 (expand test r w mod)
2135 (expand then r w mod)
2136 (expand else r w mod)))
2137 tmp-1)
2138 (syntax-violation
2139 #f
2140 "source expression failed to match any pattern"
2141 tmp)))))))
2142 (global-extend 'begin 'begin '())
2143 (global-extend 'define 'define '())
2144 (global-extend 'define-syntax 'define-syntax '())
2145 (global-extend 'define-syntax-parameter 'define-syntax-parameter '())
2146 (global-extend 'eval-when 'eval-when '())
2147 (global-extend
2148 'core
2149 'syntax-case
2150 (letrec*
2151 ((convert-pattern
2152 (lambda (pattern keys ellipsis?)
2153 (letrec*
2154 ((cvt* (lambda (p* n ids)
2155 (let* ((tmp p*) (tmp ($sc-dispatch tmp '(any . any))))
2156 (if tmp
2157 (apply (lambda (x y)
2158 (call-with-values
2159 (lambda () (cvt* y n ids))
2160 (lambda (y ids)
2161 (call-with-values
2162 (lambda () (cvt x n ids))
2163 (lambda (x ids) (values (cons x y) ids))))))
2164 tmp)
2165 (cvt p* n ids)))))
2166 (v-reverse
2167 (lambda (x)
2168 (let loop ((r '()) (x x))
2169 (if (not (pair? x)) (values r x) (loop (cons (car x) r) (cdr x))))))
2170 (cvt (lambda (p n ids)
2171 (if (id? p)
2172 (cond ((bound-id-member? p keys) (values (vector 'free-id p) ids))
2173 ((free-id=? p '#(syntax-object _ ((top)) (hygiene guile)))
2174 (values '_ ids))
2175 (else (values 'any (cons (cons p n) ids))))
2176 (let* ((tmp p) (tmp-1 ($sc-dispatch tmp '(any any))))
2177 (if (and tmp-1 (apply (lambda (x dots) (ellipsis? dots)) tmp-1))
2178 (apply (lambda (x dots)
2179 (call-with-values
2180 (lambda () (cvt x (+ n 1) ids))
2181 (lambda (p ids)
2182 (values (if (eq? p 'any) 'each-any (vector 'each p)) ids))))
2183 tmp-1)
2184 (let ((tmp-1 ($sc-dispatch tmp '(any any . any))))
2185 (if (and tmp-1 (apply (lambda (x dots ys) (ellipsis? dots)) tmp-1))
2186 (apply (lambda (x dots ys)
2187 (call-with-values
2188 (lambda () (cvt* ys n ids))
2189 (lambda (ys ids)
2190 (call-with-values
2191 (lambda () (cvt x (+ n 1) ids))
2192 (lambda (x ids)
2193 (call-with-values
2194 (lambda () (v-reverse ys))
2195 (lambda (ys e) (values (vector 'each+ x ys e) ids))))))))
2196 tmp-1)
2197 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
2198 (if tmp-1
2199 (apply (lambda (x y)
2200 (call-with-values
2201 (lambda () (cvt y n ids))
2202 (lambda (y ids)
2203 (call-with-values
2204 (lambda () (cvt x n ids))
2205 (lambda (x ids) (values (cons x y) ids))))))
2206 tmp-1)
2207 (let ((tmp-1 ($sc-dispatch tmp '())))
2208 (if tmp-1
2209 (apply (lambda () (values '() ids)) tmp-1)
2210 (let ((tmp-1 ($sc-dispatch tmp '#(vector each-any))))
2211 (if tmp-1
2212 (apply (lambda (x)
2213 (call-with-values
2214 (lambda () (cvt x n ids))
2215 (lambda (p ids) (values (vector 'vector p) ids))))
2216 tmp-1)
2217 (let ((x tmp)) (values (vector 'atom (strip p '(()))) ids))))))))))))))))
2218 (cvt pattern 0 '()))))
2219 (build-dispatch-call
2220 (lambda (pvars exp y r mod)
2221 (let ((ids (map car pvars)) (levels (map cdr pvars)))
2222 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
2223 (build-primcall
2224 #f
2225 'apply
2226 (list (build-simple-lambda
2227 #f
2228 (map syntax->datum ids)
2229 #f
2230 new-vars
2231 '()
2232 (expand
2233 exp
2234 (extend-env
2235 labels
2236 (map (lambda (var level) (cons 'syntax (cons var level)))
2237 new-vars
2238 (map cdr pvars))
2239 r)
2240 (make-binding-wrap ids labels '(()))
2241 mod))
2242 y))))))
2243 (gen-clause
2244 (lambda (x keys clauses r pat fender exp mod)
2245 (call-with-values
2246 (lambda ()
2247 (convert-pattern pat keys (lambda (e) (ellipsis? e r mod))))
2248 (lambda (p pvars)
2249 (cond ((not (and-map (lambda (x) (not (ellipsis? (car x) r mod))) pvars))
2250 (syntax-violation 'syntax-case "misplaced ellipsis" pat))
2251 ((not (distinct-bound-ids? (map car pvars)))
2252 (syntax-violation 'syntax-case "duplicate pattern variable" pat))
2253 (else
2254 (let ((y (gen-var 'tmp)))
2255 (build-call
2256 #f
2257 (build-simple-lambda
2258 #f
2259 (list 'tmp)
2260 #f
2261 (list y)
2262 '()
2263 (let ((y (build-lexical-reference 'value #f 'tmp y)))
2264 (build-conditional
2265 #f
2266 (let* ((tmp fender) (tmp ($sc-dispatch tmp '#(atom #t))))
2267 (if tmp
2268 (apply (lambda () y) tmp)
2269 (build-conditional
2270 #f
2271 y
2272 (build-dispatch-call pvars fender y r mod)
2273 (build-data #f #f))))
2274 (build-dispatch-call pvars exp y r mod)
2275 (gen-syntax-case x keys clauses r mod))))
2276 (list (if (eq? p 'any)
2277 (build-primcall #f 'list (list x))
2278 (build-primcall #f '$sc-dispatch (list x (build-data #f p)))))))))))))
2279 (gen-syntax-case
2280 (lambda (x keys clauses r mod)
2281 (if (null? clauses)
2282 (build-primcall
2283 #f
2284 'syntax-violation
2285 (list (build-data #f #f)
2286 (build-data #f "source expression failed to match any pattern")
2287 x))
2288 (let* ((tmp-1 (car clauses)) (tmp ($sc-dispatch tmp-1 '(any any))))
2289 (if tmp
2290 (apply (lambda (pat exp)
2291 (if (and (id? pat)
2292 (and-map
2293 (lambda (x) (not (free-id=? pat x)))
2294 (cons '#(syntax-object ... ((top)) (hygiene guile)) keys)))
2295 (if (free-id=? pat '#(syntax-object _ ((top)) (hygiene guile)))
2296 (expand exp r '(()) mod)
2297 (let ((labels (list (gen-label))) (var (gen-var pat)))
2298 (build-call
2299 #f
2300 (build-simple-lambda
2301 #f
2302 (list (syntax->datum pat))
2303 #f
2304 (list var)
2305 '()
2306 (expand
2307 exp
2308 (extend-env labels (list (cons 'syntax (cons var 0))) r)
2309 (make-binding-wrap (list pat) labels '(()))
2310 mod))
2311 (list x))))
2312 (gen-clause x keys (cdr clauses) r pat #t exp mod)))
2313 tmp)
2314 (let ((tmp ($sc-dispatch tmp-1 '(any any any))))
2315 (if tmp
2316 (apply (lambda (pat fender exp)
2317 (gen-clause x keys (cdr clauses) r pat fender exp mod))
2318 tmp)
2319 (syntax-violation 'syntax-case "invalid clause" (car clauses))))))))))
2320 (lambda (e r w s mod)
2321 (let* ((e (source-wrap e w s mod))
2322 (tmp-1 e)
2323 (tmp ($sc-dispatch tmp-1 '(_ any each-any . each-any))))
2324 (if tmp
2325 (apply (lambda (val key m)
2326 (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x r mod)))) key)
2327 (let ((x (gen-var 'tmp)))
2328 (build-call
2329 s
2330 (build-simple-lambda
2331 #f
2332 (list 'tmp)
2333 #f
2334 (list x)
2335 '()
2336 (gen-syntax-case
2337 (build-lexical-reference 'value #f 'tmp x)
2338 key
2339 m
2340 r
2341 mod))
2342 (list (expand val r '(()) mod))))
2343 (syntax-violation 'syntax-case "invalid literals list" e)))
2344 tmp)
2345 (syntax-violation
2346 #f
2347 "source expression failed to match any pattern"
2348 tmp-1))))))
2349 (set! macroexpand
2350 (lambda* (x #:optional (m 'e) (esew '(eval)))
2351 (expand-top-sequence
2352 (list x)
2353 '()
2354 '((top))
2355 #f
2356 m
2357 esew
2358 (cons 'hygiene (module-name (current-module))))))
2359 (set! identifier? (lambda (x) (nonsymbol-id? x)))
2360 (set! datum->syntax
2361 (lambda (id datum)
2362 (make-syntax-object
2363 datum
2364 (syntax-object-wrap id)
2365 (syntax-object-module id))))
2366 (set! syntax->datum (lambda (x) (strip x '(()))))
2367 (set! syntax-source (lambda (x) (source-annotation x)))
2368 (set! generate-temporaries
2369 (lambda (ls)
2370 (let ((x ls))
2371 (if (not (list? x))
2372 (syntax-violation 'generate-temporaries "invalid argument" x)))
2373 (let ((mod (cons 'hygiene (module-name (current-module)))))
2374 (map (lambda (x) (wrap (gensym "t-") '((top)) mod)) ls))))
2375 (set! free-identifier=?
2376 (lambda (x y)
2377 (let ((x x))
2378 (if (not (nonsymbol-id? x))
2379 (syntax-violation 'free-identifier=? "invalid argument" x)))
2380 (let ((x y))
2381 (if (not (nonsymbol-id? x))
2382 (syntax-violation 'free-identifier=? "invalid argument" x)))
2383 (free-id=? x y)))
2384 (set! bound-identifier=?
2385 (lambda (x y)
2386 (let ((x x))
2387 (if (not (nonsymbol-id? x))
2388 (syntax-violation 'bound-identifier=? "invalid argument" x)))
2389 (let ((x y))
2390 (if (not (nonsymbol-id? x))
2391 (syntax-violation 'bound-identifier=? "invalid argument" x)))
2392 (bound-id=? x y)))
2393 (set! syntax-violation
2394 (lambda* (who message form #:optional (subform #f))
2395 (let ((x who))
2396 (if (not (let ((x x)) (or (not x) (string? x) (symbol? x))))
2397 (syntax-violation 'syntax-violation "invalid argument" x)))
2398 (let ((x message))
2399 (if (not (string? x))
2400 (syntax-violation 'syntax-violation "invalid argument" x)))
2401 (throw 'syntax-error
2402 who
2403 message
2404 (or (source-annotation subform) (source-annotation form))
2405 (strip form '(()))
2406 (and subform (strip subform '(()))))))
2407 (letrec*
2408 ((syntax-module
2409 (lambda (id)
2410 (let ((x id))
2411 (if (not (nonsymbol-id? x))
2412 (syntax-violation 'syntax-module "invalid argument" x)))
2413 (let ((mod (syntax-object-module id)))
2414 (and (not (equal? mod '(primitive))) (cdr mod)))))
2415 (syntax-local-binding
2416 (lambda* (id
2417 #:key
2418 (resolve-syntax-parameters? #t #:resolve-syntax-parameters?))
2419 (let ((x id))
2420 (if (not (nonsymbol-id? x))
2421 (syntax-violation 'syntax-local-binding "invalid argument" x)))
2422 (with-transformer-environment
2423 (lambda (e r w s rib mod)
2424 (letrec*
2425 ((strip-anti-mark
2426 (lambda (w)
2427 (let ((ms (car w)) (s (cdr w)))
2428 (if (and (pair? ms) (eq? (car ms) #f))
2429 (cons (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
2430 (cons ms (if rib (cons rib s) s)))))))
2431 (call-with-values
2432 (lambda ()
2433 (resolve-identifier
2434 (syntax-object-expression id)
2435 (strip-anti-mark (syntax-object-wrap id))
2436 r
2437 (syntax-object-module id)
2438 resolve-syntax-parameters?))
2439 (lambda (type value mod)
2440 (let ((key type))
2441 (cond ((memv key '(lexical)) (values 'lexical value))
2442 ((memv key '(macro)) (values 'macro value))
2443 ((memv key '(syntax-parameter))
2444 (values 'syntax-parameter (car value)))
2445 ((memv key '(syntax)) (values 'pattern-variable value))
2446 ((memv key '(displaced-lexical)) (values 'displaced-lexical #f))
2447 ((memv key '(global))
2448 (if (equal? mod '(primitive))
2449 (values 'primitive value)
2450 (values 'global (cons value (cdr mod)))))
2451 ((memv key '(ellipsis))
2452 (values
2453 'ellipsis
2454 (make-syntax-object
2455 (syntax-object-expression value)
2456 (anti-mark (syntax-object-wrap value))
2457 (syntax-object-module value))))
2458 (else (values 'other #f)))))))))))
2459 (syntax-locally-bound-identifiers
2460 (lambda (id)
2461 (let ((x id))
2462 (if (not (nonsymbol-id? x))
2463 (syntax-violation
2464 'syntax-locally-bound-identifiers
2465 "invalid argument"
2466 x)))
2467 (locally-bound-identifiers
2468 (syntax-object-wrap id)
2469 (syntax-object-module id)))))
2470 (define! 'syntax-module syntax-module)
2471 (define! 'syntax-local-binding syntax-local-binding)
2472 (define!
2473 'syntax-locally-bound-identifiers
2474 syntax-locally-bound-identifiers))
2475 (letrec*
2476 ((match-each
2477 (lambda (e p w mod)
2478 (cond ((pair? e)
2479 (let ((first (match (car e) p w '() mod)))
2480 (and first
2481 (let ((rest (match-each (cdr e) p w mod)))
2482 (and rest (cons first rest))))))
2483 ((null? e) '())
2484 ((syntax-object? e)
2485 (match-each
2486 (syntax-object-expression e)
2487 p
2488 (join-wraps w (syntax-object-wrap e))
2489 (syntax-object-module e)))
2490 (else #f))))
2491 (match-each+
2492 (lambda (e x-pat y-pat z-pat w r mod)
2493 (let f ((e e) (w w))
2494 (cond ((pair? e)
2495 (call-with-values
2496 (lambda () (f (cdr e) w))
2497 (lambda (xr* y-pat r)
2498 (if r
2499 (if (null? y-pat)
2500 (let ((xr (match (car e) x-pat w '() mod)))
2501 (if xr (values (cons xr xr*) y-pat r) (values #f #f #f)))
2502 (values '() (cdr y-pat) (match (car e) (car y-pat) w r mod)))
2503 (values #f #f #f)))))
2504 ((syntax-object? e)
2505 (f (syntax-object-expression e) (join-wraps w e)))
2506 (else (values '() y-pat (match e z-pat w r mod)))))))
2507 (match-each-any
2508 (lambda (e w mod)
2509 (cond ((pair? e)
2510 (let ((l (match-each-any (cdr e) w mod)))
2511 (and l (cons (wrap (car e) w mod) l))))
2512 ((null? e) '())
2513 ((syntax-object? e)
2514 (match-each-any
2515 (syntax-object-expression e)
2516 (join-wraps w (syntax-object-wrap e))
2517 mod))
2518 (else #f))))
2519 (match-empty
2520 (lambda (p r)
2521 (cond ((null? p) r)
2522 ((eq? p '_) r)
2523 ((eq? p 'any) (cons '() r))
2524 ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
2525 ((eq? p 'each-any) (cons '() r))
2526 (else
2527 (let ((key (vector-ref p 0)))
2528 (cond ((memv key '(each)) (match-empty (vector-ref p 1) r))
2529 ((memv key '(each+))
2530 (match-empty
2531 (vector-ref p 1)
2532 (match-empty
2533 (reverse (vector-ref p 2))
2534 (match-empty (vector-ref p 3) r))))
2535 ((memv key '(free-id atom)) r)
2536 ((memv key '(vector)) (match-empty (vector-ref p 1) r))))))))
2537 (combine
2538 (lambda (r* r)
2539 (if (null? (car r*)) r (cons (map car r*) (combine (map cdr r*) r)))))
2540 (match*
2541 (lambda (e p w r mod)
2542 (cond ((null? p) (and (null? e) r))
2543 ((pair? p)
2544 (and (pair? e)
2545 (match (car e) (car p) w (match (cdr e) (cdr p) w r mod) mod)))
2546 ((eq? p 'each-any)
2547 (let ((l (match-each-any e w mod))) (and l (cons l r))))
2548 (else
2549 (let ((key (vector-ref p 0)))
2550 (cond ((memv key '(each))
2551 (if (null? e)
2552 (match-empty (vector-ref p 1) r)
2553 (let ((l (match-each e (vector-ref p 1) w mod)))
2554 (and l
2555 (let collect ((l l))
2556 (if (null? (car l)) r (cons (map car l) (collect (map cdr l)))))))))
2557 ((memv key '(each+))
2558 (call-with-values
2559 (lambda ()
2560 (match-each+
2561 e
2562 (vector-ref p 1)
2563 (vector-ref p 2)
2564 (vector-ref p 3)
2565 w
2566 r
2567 mod))
2568 (lambda (xr* y-pat r)
2569 (and r
2570 (null? y-pat)
2571 (if (null? xr*) (match-empty (vector-ref p 1) r) (combine xr* r))))))
2572 ((memv key '(free-id))
2573 (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
2574 ((memv key '(atom)) (and (equal? (vector-ref p 1) (strip e w)) r))
2575 ((memv key '(vector))
2576 (and (vector? e) (match (vector->list e) (vector-ref p 1) w r mod)))))))))
2577 (match (lambda (e p w r mod)
2578 (cond ((not r) #f)
2579 ((eq? p '_) r)
2580 ((eq? p 'any) (cons (wrap e w mod) r))
2581 ((syntax-object? e)
2582 (match*
2583 (syntax-object-expression e)
2584 p
2585 (join-wraps w (syntax-object-wrap e))
2586 r
2587 (syntax-object-module e)))
2588 (else (match* e p w r mod))))))
2589 (set! $sc-dispatch
2590 (lambda (e p)
2591 (cond ((eq? p 'any) (list e))
2592 ((eq? p '_) '())
2593 ((syntax-object? e)
2594 (match*
2595 (syntax-object-expression e)
2596 p
2597 (syntax-object-wrap e)
2598 '()
2599 (syntax-object-module e)))
2600 (else (match* e p '(()) '() #f)))))))
2601
2602 (define with-syntax
2603 (make-syntax-transformer
2604 'with-syntax
2605 'macro
2606 (lambda (x)
2607 (let ((tmp x))
2608 (let ((tmp-1 ($sc-dispatch tmp '(_ () any . each-any))))
2609 (if tmp-1
2610 (apply (lambda (e1 e2)
2611 (cons '#(syntax-object let ((top)) (hygiene guile))
2612 (cons '() (cons e1 e2))))
2613 tmp-1)
2614 (let ((tmp-1 ($sc-dispatch tmp '(_ ((any any)) any . each-any))))
2615 (if tmp-1
2616 (apply (lambda (out in e1 e2)
2617 (list '#(syntax-object syntax-case ((top)) (hygiene guile))
2618 in
2619 '()
2620 (list out
2621 (cons '#(syntax-object let ((top)) (hygiene guile))
2622 (cons '() (cons e1 e2))))))
2623 tmp-1)
2624 (let ((tmp-1 ($sc-dispatch tmp '(_ #(each (any any)) any . each-any))))
2625 (if tmp-1
2626 (apply (lambda (out in e1 e2)
2627 (list '#(syntax-object syntax-case ((top)) (hygiene guile))
2628 (cons '#(syntax-object list ((top)) (hygiene guile)) in)
2629 '()
2630 (list out
2631 (cons '#(syntax-object let ((top)) (hygiene guile))
2632 (cons '() (cons e1 e2))))))
2633 tmp-1)
2634 (syntax-violation
2635 #f
2636 "source expression failed to match any pattern"
2637 tmp)))))))))))
2638
2639 (define syntax-error
2640 (make-syntax-transformer
2641 'syntax-error
2642 'macro
2643 (lambda (x)
2644 (let ((tmp-1 x))
2645 (let ((tmp ($sc-dispatch tmp-1 '(_ (any . any) any . each-any))))
2646 (if (if tmp
2647 (apply (lambda (keyword operands message arg)
2648 (string? (syntax->datum message)))
2649 tmp)
2650 #f)
2651 (apply (lambda (keyword operands message arg)
2652 (syntax-violation
2653 (syntax->datum keyword)
2654 (string-join
2655 (cons (syntax->datum message)
2656 (map (lambda (x) (object->string (syntax->datum x))) arg)))
2657 (if (syntax->datum keyword) (cons keyword operands) #f)))
2658 tmp)
2659 (let ((tmp ($sc-dispatch tmp-1 '(_ any . each-any))))
2660 (if (if tmp
2661 (apply (lambda (message arg) (string? (syntax->datum message))) tmp)
2662 #f)
2663 (apply (lambda (message arg)
2664 (cons '#(syntax-object
2665 syntax-error
2666 ((top)
2667 #(ribcage
2668 #(syntax-error)
2669 #((top))
2670 #(((hygiene guile)
2671 .
2672 #(syntax-object syntax-error ((top)) (hygiene guile))))))
2673 (hygiene guile))
2674 (cons '(#f) (cons message arg))))
2675 tmp)
2676 (syntax-violation
2677 #f
2678 "source expression failed to match any pattern"
2679 tmp-1)))))))))
2680
2681 (define syntax-rules
2682 (make-syntax-transformer
2683 'syntax-rules
2684 'macro
2685 (lambda (xx)
2686 (letrec*
2687 ((expand-clause
2688 (lambda (clause)
2689 (let ((tmp-1 clause))
2690 (let ((tmp ($sc-dispatch
2691 tmp-1
2692 '((any . any)
2693 (#(free-id #(syntax-object syntax-error ((top)) (hygiene guile)))
2694 any
2695 .
2696 each-any)))))
2697 (if (if tmp
2698 (apply (lambda (keyword pattern message arg)
2699 (string? (syntax->datum message)))
2700 tmp)
2701 #f)
2702 (apply (lambda (keyword pattern message arg)
2703 (list (cons '#(syntax-object dummy ((top)) (hygiene guile)) pattern)
2704 (list '#(syntax-object syntax ((top)) (hygiene guile))
2705 (cons '#(syntax-object syntax-error ((top)) (hygiene guile))
2706 (cons (cons '#(syntax-object dummy ((top)) (hygiene guile)) pattern)
2707 (cons message arg))))))
2708 tmp)
2709 (let ((tmp ($sc-dispatch tmp-1 '((any . any) any))))
2710 (if tmp
2711 (apply (lambda (keyword pattern template)
2712 (list (cons '#(syntax-object dummy ((top)) (hygiene guile)) pattern)
2713 (list '#(syntax-object syntax ((top)) (hygiene guile)) template)))
2714 tmp)
2715 (syntax-violation
2716 #f
2717 "source expression failed to match any pattern"
2718 tmp-1))))))))
2719 (expand-syntax-rules
2720 (lambda (dots keys docstrings clauses)
2721 (let ((tmp-1 (list keys docstrings clauses (map expand-clause clauses))))
2722 (let ((tmp ($sc-dispatch
2723 tmp-1
2724 '(each-any each-any #(each ((any . any) any)) each-any))))
2725 (if tmp
2726 (apply (lambda (k docstring keyword pattern template clause)
2727 (let ((tmp (cons '#(syntax-object lambda ((top)) (hygiene guile))
2728 (cons '(#(syntax-object x ((top)) (hygiene guile)))
2729 (append
2730 docstring
2731 (list (vector
2732 '(#(syntax-object macro-type ((top)) (hygiene guile))
2733 .
2734 #(syntax-object
2735 syntax-rules
2736 ((top)
2737 #(ribcage
2738 #(syntax-rules)
2739 #((top))
2740 #(((hygiene guile)
2741 .
2742 #(syntax-object
2743 syntax-rules
2744 ((top))
2745 (hygiene guile))))))
2746 (hygiene guile)))
2747 (cons '#(syntax-object patterns ((top)) (hygiene guile))
2748 pattern))
2749 (cons '#(syntax-object syntax-case ((top)) (hygiene guile))
2750 (cons '#(syntax-object x ((top)) (hygiene guile))
2751 (cons k clause)))))))))
2752 (let ((form tmp))
2753 (if dots
2754 (let ((tmp dots))
2755 (let ((dots tmp))
2756 (list '#(syntax-object with-ellipsis ((top)) (hygiene guile))
2757 dots
2758 form)))
2759 form))))
2760 tmp)
2761 (syntax-violation
2762 #f
2763 "source expression failed to match any pattern"
2764 tmp-1)))))))
2765 (let ((tmp xx))
2766 (let ((tmp-1 ($sc-dispatch tmp '(_ each-any . #(each ((any . any) any))))))
2767 (if tmp-1
2768 (apply (lambda (k keyword pattern template)
2769 (expand-syntax-rules
2770 #f
2771 k
2772 '()
2773 (map (lambda (tmp-2 tmp-1 tmp) (list (cons tmp tmp-1) tmp-2))
2774 template
2775 pattern
2776 keyword)))
2777 tmp-1)
2778 (let ((tmp-1 ($sc-dispatch tmp '(_ each-any any . #(each ((any . any) any))))))
2779 (if (if tmp-1
2780 (apply (lambda (k docstring keyword pattern template)
2781 (string? (syntax->datum docstring)))
2782 tmp-1)
2783 #f)
2784 (apply (lambda (k docstring keyword pattern template)
2785 (expand-syntax-rules
2786 #f
2787 k
2788 (list docstring)
2789 (map (lambda (tmp-2 tmp-1 tmp) (list (cons tmp tmp-1) tmp-2))
2790 template
2791 pattern
2792 keyword)))
2793 tmp-1)
2794 (let ((tmp-1 ($sc-dispatch tmp '(_ any each-any . #(each ((any . any) any))))))
2795 (if (if tmp-1
2796 (apply (lambda (dots k keyword pattern template) (identifier? dots))
2797 tmp-1)
2798 #f)
2799 (apply (lambda (dots k keyword pattern template)
2800 (expand-syntax-rules
2801 dots
2802 k
2803 '()
2804 (map (lambda (tmp-2 tmp-1 tmp) (list (cons tmp tmp-1) tmp-2))
2805 template
2806 pattern
2807 keyword)))
2808 tmp-1)
2809 (let ((tmp-1 ($sc-dispatch tmp '(_ any each-any any . #(each ((any . any) any))))))
2810 (if (if tmp-1
2811 (apply (lambda (dots k docstring keyword pattern template)
2812 (if (identifier? dots) (string? (syntax->datum docstring)) #f))
2813 tmp-1)
2814 #f)
2815 (apply (lambda (dots k docstring keyword pattern template)
2816 (expand-syntax-rules
2817 dots
2818 k
2819 (list docstring)
2820 (map (lambda (tmp-2 tmp-1 tmp) (list (cons tmp tmp-1) tmp-2))
2821 template
2822 pattern
2823 keyword)))
2824 tmp-1)
2825 (syntax-violation
2826 #f
2827 "source expression failed to match any pattern"
2828 tmp))))))))))))))
2829
2830 (define define-syntax-rule
2831 (make-syntax-transformer
2832 'define-syntax-rule
2833 'macro
2834 (lambda (x)
2835 (let ((tmp-1 x))
2836 (let ((tmp ($sc-dispatch tmp-1 '(_ (any . any) any))))
2837 (if tmp
2838 (apply (lambda (name pattern template)
2839 (list '#(syntax-object define-syntax ((top)) (hygiene guile))
2840 name
2841 (list '#(syntax-object syntax-rules ((top)) (hygiene guile))
2842 '()
2843 (list (cons '#(syntax-object _ ((top)) (hygiene guile)) pattern)
2844 template))))
2845 tmp)
2846 (let ((tmp ($sc-dispatch tmp-1 '(_ (any . any) any any))))
2847 (if (if tmp
2848 (apply (lambda (name pattern docstring template)
2849 (string? (syntax->datum docstring)))
2850 tmp)
2851 #f)
2852 (apply (lambda (name pattern docstring template)
2853 (list '#(syntax-object define-syntax ((top)) (hygiene guile))
2854 name
2855 (list '#(syntax-object syntax-rules ((top)) (hygiene guile))
2856 '()
2857 docstring
2858 (list (cons '#(syntax-object _ ((top)) (hygiene guile)) pattern)
2859 template))))
2860 tmp)
2861 (syntax-violation
2862 #f
2863 "source expression failed to match any pattern"
2864 tmp-1)))))))))
2865
2866 (define let*
2867 (make-syntax-transformer
2868 'let*
2869 'macro
2870 (lambda (x)
2871 (let ((tmp-1 x))
2872 (let ((tmp ($sc-dispatch tmp-1 '(any #(each (any any)) any . each-any))))
2873 (if (if tmp
2874 (apply (lambda (let* x v e1 e2) (and-map identifier? x)) tmp)
2875 #f)
2876 (apply (lambda (let* x v e1 e2)
2877 (let f ((bindings (map list x v)))
2878 (if (null? bindings)
2879 (cons '#(syntax-object let ((top)) (hygiene guile))
2880 (cons '() (cons e1 e2)))
2881 (let ((tmp-1 (list (f (cdr bindings)) (car bindings))))
2882 (let ((tmp ($sc-dispatch tmp-1 '(any any))))
2883 (if tmp
2884 (apply (lambda (body binding)
2885 (list '#(syntax-object let ((top)) (hygiene guile))
2886 (list binding)
2887 body))
2888 tmp)
2889 (syntax-violation
2890 #f
2891 "source expression failed to match any pattern"
2892 tmp-1)))))))
2893 tmp)
2894 (syntax-violation
2895 #f
2896 "source expression failed to match any pattern"
2897 tmp-1)))))))
2898
2899 (define quasiquote
2900 (make-syntax-transformer
2901 'quasiquote
2902 'macro
2903 (letrec*
2904 ((quasi (lambda (p lev)
2905 (let ((tmp p))
2906 (let ((tmp-1 ($sc-dispatch
2907 tmp
2908 '(#(free-id #(syntax-object unquote ((top)) (hygiene guile))) any))))
2909 (if tmp-1
2910 (apply (lambda (p)
2911 (if (= lev 0)
2912 (list "value" p)
2913 (quasicons
2914 '("quote" #(syntax-object unquote ((top)) (hygiene guile)))
2915 (quasi (list p) (- lev 1)))))
2916 tmp-1)
2917 (let ((tmp-1 ($sc-dispatch
2918 tmp
2919 '(#(free-id
2920 #(syntax-object
2921 quasiquote
2922 ((top)
2923 #(ribcage
2924 #(quasiquote)
2925 #((top))
2926 #(((hygiene guile)
2927 .
2928 #(syntax-object quasiquote ((top)) (hygiene guile))))))
2929 (hygiene guile)))
2930 any))))
2931 (if tmp-1
2932 (apply (lambda (p)
2933 (quasicons
2934 '("quote"
2935 #(syntax-object
2936 quasiquote
2937 ((top)
2938 #(ribcage
2939 #(quasiquote)
2940 #((top))
2941 #(((hygiene guile)
2942 .
2943 #(syntax-object quasiquote ((top)) (hygiene guile))))))
2944 (hygiene guile)))
2945 (quasi (list p) (+ lev 1))))
2946 tmp-1)
2947 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
2948 (if tmp-1
2949 (apply (lambda (p q)
2950 (let ((tmp-1 p))
2951 (let ((tmp ($sc-dispatch
2952 tmp-1
2953 '(#(free-id #(syntax-object unquote ((top)) (hygiene guile)))
2954 .
2955 each-any))))
2956 (if tmp
2957 (apply (lambda (p)
2958 (if (= lev 0)
2959 (quasilist*
2960 (map (lambda (tmp) (list "value" tmp)) p)
2961 (quasi q lev))
2962 (quasicons
2963 (quasicons
2964 '("quote" #(syntax-object unquote ((top)) (hygiene guile)))
2965 (quasi p (- lev 1)))
2966 (quasi q lev))))
2967 tmp)
2968 (let ((tmp ($sc-dispatch
2969 tmp-1
2970 '(#(free-id
2971 #(syntax-object unquote-splicing ((top)) (hygiene guile)))
2972 .
2973 each-any))))
2974 (if tmp
2975 (apply (lambda (p)
2976 (if (= lev 0)
2977 (quasiappend
2978 (map (lambda (tmp) (list "value" tmp)) p)
2979 (quasi q lev))
2980 (quasicons
2981 (quasicons
2982 '("quote"
2983 #(syntax-object
2984 unquote-splicing
2985 ((top))
2986 (hygiene guile)))
2987 (quasi p (- lev 1)))
2988 (quasi q lev))))
2989 tmp)
2990 (quasicons (quasi p lev) (quasi q lev))))))))
2991 tmp-1)
2992 (let ((tmp-1 ($sc-dispatch tmp '#(vector each-any))))
2993 (if tmp-1
2994 (apply (lambda (x) (quasivector (vquasi x lev))) tmp-1)
2995 (let ((p tmp)) (list "quote" p)))))))))))))
2996 (vquasi
2997 (lambda (p lev)
2998 (let ((tmp p))
2999 (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
3000 (if tmp-1
3001 (apply (lambda (p q)
3002 (let ((tmp-1 p))
3003 (let ((tmp ($sc-dispatch
3004 tmp-1
3005 '(#(free-id #(syntax-object unquote ((top)) (hygiene guile)))
3006 .
3007 each-any))))
3008 (if tmp
3009 (apply (lambda (p)
3010 (if (= lev 0)
3011 (quasilist* (map (lambda (tmp) (list "value" tmp)) p) (vquasi q lev))
3012 (quasicons
3013 (quasicons
3014 '("quote" #(syntax-object unquote ((top)) (hygiene guile)))
3015 (quasi p (- lev 1)))
3016 (vquasi q lev))))
3017 tmp)
3018 (let ((tmp ($sc-dispatch
3019 tmp-1
3020 '(#(free-id #(syntax-object unquote-splicing ((top)) (hygiene guile)))
3021 .
3022 each-any))))
3023 (if tmp
3024 (apply (lambda (p)
3025 (if (= lev 0)
3026 (quasiappend
3027 (map (lambda (tmp) (list "value" tmp)) p)
3028 (vquasi q lev))
3029 (quasicons
3030 (quasicons
3031 '("quote" #(syntax-object unquote-splicing ((top)) (hygiene guile)))
3032 (quasi p (- lev 1)))
3033 (vquasi q lev))))
3034 tmp)
3035 (quasicons (quasi p lev) (vquasi q lev))))))))
3036 tmp-1)
3037 (let ((tmp-1 ($sc-dispatch tmp '())))
3038 (if tmp-1
3039 (apply (lambda () '("quote" ())) tmp-1)
3040 (syntax-violation
3041 #f
3042 "source expression failed to match any pattern"
3043 tmp))))))))
3044 (quasicons
3045 (lambda (x y)
3046 (let ((tmp-1 (list x y)))
3047 (let ((tmp ($sc-dispatch tmp-1 '(any any))))
3048 (if tmp
3049 (apply (lambda (x y)
3050 (let ((tmp y))
3051 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "quote") any))))
3052 (if tmp-1
3053 (apply (lambda (dy)
3054 (let ((tmp x))
3055 (let ((tmp ($sc-dispatch tmp '(#(atom "quote") any))))
3056 (if tmp
3057 (apply (lambda (dx) (list "quote" (cons dx dy))) tmp)
3058 (if (null? dy) (list "list" x) (list "list*" x y))))))
3059 tmp-1)
3060 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list") . any))))
3061 (if tmp-1
3062 (apply (lambda (stuff) (cons "list" (cons x stuff))) tmp-1)
3063 (let ((tmp ($sc-dispatch tmp '(#(atom "list*") . any))))
3064 (if tmp
3065 (apply (lambda (stuff) (cons "list*" (cons x stuff))) tmp)
3066 (list "list*" x y)))))))))
3067 tmp)
3068 (syntax-violation
3069 #f
3070 "source expression failed to match any pattern"
3071 tmp-1))))))
3072 (quasiappend
3073 (lambda (x y)
3074 (let ((tmp y))
3075 (let ((tmp ($sc-dispatch tmp '(#(atom "quote") ()))))
3076 (if tmp
3077 (apply (lambda ()
3078 (if (null? x)
3079 '("quote" ())
3080 (if (null? (cdr x))
3081 (car x)
3082 (let ((tmp-1 x))
3083 (let ((tmp ($sc-dispatch tmp-1 'each-any)))
3084 (if tmp
3085 (apply (lambda (p) (cons "append" p)) tmp)
3086 (syntax-violation
3087 #f
3088 "source expression failed to match any pattern"
3089 tmp-1)))))))
3090 tmp)
3091 (if (null? x)
3092 y
3093 (let ((tmp-1 (list x y)))
3094 (let ((tmp ($sc-dispatch tmp-1 '(each-any any))))
3095 (if tmp
3096 (apply (lambda (p y) (cons "append" (append p (list y)))) tmp)
3097 (syntax-violation
3098 #f
3099 "source expression failed to match any pattern"
3100 tmp-1))))))))))
3101 (quasilist*
3102 (lambda (x y)
3103 (let f ((x x)) (if (null? x) y (quasicons (car x) (f (cdr x)))))))
3104 (quasivector
3105 (lambda (x)
3106 (let ((tmp x))
3107 (let ((tmp ($sc-dispatch tmp '(#(atom "quote") each-any))))
3108 (if tmp
3109 (apply (lambda (x) (list "quote" (list->vector x))) tmp)
3110 (let f ((y x)
3111 (k (lambda (ls)
3112 (let ((tmp-1 ls))
3113 (let ((tmp ($sc-dispatch tmp-1 'each-any)))
3114 (if tmp
3115 (apply (lambda (t) (cons "vector" t)) tmp)
3116 (syntax-violation
3117 #f
3118 "source expression failed to match any pattern"
3119 tmp-1)))))))
3120 (let ((tmp y))
3121 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "quote") each-any))))
3122 (if tmp-1
3123 (apply (lambda (y) (k (map (lambda (tmp) (list "quote" tmp)) y)))
3124 tmp-1)
3125 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list") . each-any))))
3126 (if tmp-1
3127 (apply (lambda (y) (k y)) tmp-1)
3128 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list*") . #(each+ any (any) ())))))
3129 (if tmp-1
3130 (apply (lambda (y z) (f z (lambda (ls) (k (append y ls))))) tmp-1)
3131 (let ((else tmp))
3132 (let ((tmp x)) (let ((t tmp)) (list "list->vector" t)))))))))))))))))
3133 (emit (lambda (x)
3134 (let ((tmp x))
3135 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "quote") any))))
3136 (if tmp-1
3137 (apply (lambda (x) (list '#(syntax-object quote ((top)) (hygiene guile)) x))
3138 tmp-1)
3139 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list") . each-any))))
3140 (if tmp-1
3141 (apply (lambda (x)
3142 (let ((tmp-1 (map emit x)))
3143 (let ((tmp ($sc-dispatch tmp-1 'each-any)))
3144 (if tmp
3145 (apply (lambda (t) (cons '#(syntax-object list ((top)) (hygiene guile)) t))
3146 tmp)
3147 (syntax-violation
3148 #f
3149 "source expression failed to match any pattern"
3150 tmp-1)))))
3151 tmp-1)
3152 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list*") . #(each+ any (any) ())))))
3153 (if tmp-1
3154 (apply (lambda (x y)
3155 (let f ((x* x))
3156 (if (null? x*)
3157 (emit y)
3158 (let ((tmp-1 (list (emit (car x*)) (f (cdr x*)))))
3159 (let ((tmp ($sc-dispatch tmp-1 '(any any))))
3160 (if tmp
3161 (apply (lambda (t-1 t)
3162 (list '#(syntax-object cons ((top)) (hygiene guile)) t-1 t))
3163 tmp)
3164 (syntax-violation
3165 #f
3166 "source expression failed to match any pattern"
3167 tmp-1)))))))
3168 tmp-1)
3169 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "append") . each-any))))
3170 (if tmp-1
3171 (apply (lambda (x)
3172 (let ((tmp-1 (map emit x)))
3173 (let ((tmp ($sc-dispatch tmp-1 'each-any)))
3174 (if tmp
3175 (apply (lambda (t)
3176 (cons '#(syntax-object append ((top)) (hygiene guile)) t))
3177 tmp)
3178 (syntax-violation
3179 #f
3180 "source expression failed to match any pattern"
3181 tmp-1)))))
3182 tmp-1)
3183 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "vector") . each-any))))
3184 (if tmp-1
3185 (apply (lambda (x)
3186 (let ((tmp-1 (map emit x)))
3187 (let ((tmp ($sc-dispatch tmp-1 'each-any)))
3188 (if tmp
3189 (apply (lambda (t)
3190 (cons '#(syntax-object vector ((top)) (hygiene guile)) t))
3191 tmp)
3192 (syntax-violation
3193 #f
3194 "source expression failed to match any pattern"
3195 tmp-1)))))
3196 tmp-1)
3197 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list->vector") any))))
3198 (if tmp-1
3199 (apply (lambda (x)
3200 (let ((tmp (emit x)))
3201 (let ((t tmp))
3202 (list '#(syntax-object list->vector ((top)) (hygiene guile)) t))))
3203 tmp-1)
3204 (let ((tmp-1 ($sc-dispatch tmp '(#(atom "value") any))))
3205 (if tmp-1
3206 (apply (lambda (x) x) tmp-1)
3207 (syntax-violation
3208 #f
3209 "source expression failed to match any pattern"
3210 tmp)))))))))))))))))))
3211 (lambda (x)
3212 (let ((tmp-1 x))
3213 (let ((tmp ($sc-dispatch tmp-1 '(_ any))))
3214 (if tmp
3215 (apply (lambda (e) (emit (quasi e 0))) tmp)
3216 (syntax-violation
3217 #f
3218 "source expression failed to match any pattern"
3219 tmp-1))))))))
3220
3221 (define include
3222 (make-syntax-transformer
3223 'include
3224 'macro
3225 (lambda (x)
3226 (letrec*
3227 ((read-file
3228 (lambda (fn dir k)
3229 (let ((p (open-input-file
3230 (if (absolute-file-name? fn)
3231 fn
3232 (if dir
3233 (in-vicinity dir fn)
3234 (syntax-violation
3235 'include
3236 "relative file name only allowed when the include form is in a file"
3237 x))))))
3238 (let ((enc (file-encoding p)))
3239 (set-port-encoding! p (let ((t enc)) (if t t "UTF-8")))
3240 (let f ((x (read p)) (result '()))
3241 (if (eof-object? x)
3242 (begin (close-input-port p) (reverse result))
3243 (f (read p) (cons (datum->syntax k x) result)))))))))
3244 (let ((src (syntax-source x)))
3245 (let ((file (if src (assq-ref src 'filename) #f)))
3246 (let ((dir (if (string? file) (dirname file) #f)))
3247 (let ((tmp-1 x))
3248 (let ((tmp ($sc-dispatch tmp-1 '(any any))))
3249 (if tmp
3250 (apply (lambda (k filename)
3251 (let ((fn (syntax->datum filename)))
3252 (let ((tmp-1 (read-file fn dir filename)))
3253 (let ((tmp ($sc-dispatch tmp-1 'each-any)))
3254 (if tmp
3255 (apply (lambda (exp)
3256 (cons '#(syntax-object begin ((top)) (hygiene guile)) exp))
3257 tmp)
3258 (syntax-violation
3259 #f
3260 "source expression failed to match any pattern"
3261 tmp-1))))))
3262 tmp)
3263 (syntax-violation
3264 #f
3265 "source expression failed to match any pattern"
3266 tmp-1)))))))))))
3267
3268 (define include-from-path
3269 (make-syntax-transformer
3270 'include-from-path
3271 'macro
3272 (lambda (x)
3273 (let ((tmp-1 x))
3274 (let ((tmp ($sc-dispatch tmp-1 '(any any))))
3275 (if tmp
3276 (apply (lambda (k filename)
3277 (let ((fn (syntax->datum filename)))
3278 (let ((tmp (datum->syntax
3279 filename
3280 (let ((t (%search-load-path fn)))
3281 (if t
3282 t
3283 (syntax-violation
3284 'include-from-path
3285 "file not found in path"
3286 x
3287 filename))))))
3288 (let ((fn tmp))
3289 (list '#(syntax-object include ((top)) (hygiene guile)) fn)))))
3290 tmp)
3291 (syntax-violation
3292 #f
3293 "source expression failed to match any pattern"
3294 tmp-1)))))))
3295
3296 (define unquote
3297 (make-syntax-transformer
3298 'unquote
3299 'macro
3300 (lambda (x)
3301 (syntax-violation
3302 'unquote
3303 "expression not valid outside of quasiquote"
3304 x))))
3305
3306 (define unquote-splicing
3307 (make-syntax-transformer
3308 'unquote-splicing
3309 'macro
3310 (lambda (x)
3311 (syntax-violation
3312 'unquote-splicing
3313 "expression not valid outside of quasiquote"
3314 x))))
3315
3316 (define make-variable-transformer
3317 (lambda (proc)
3318 (if (procedure? proc)
3319 (let ((trans (lambda (x) (proc x))))
3320 (set-procedure-property! trans 'variable-transformer #t)
3321 trans)
3322 (error "variable transformer not a procedure" proc))))
3323
3324 (define identifier-syntax
3325 (make-syntax-transformer
3326 'identifier-syntax
3327 'macro
3328 (lambda (xx)
3329 (let ((tmp-1 xx))
3330 (let ((tmp ($sc-dispatch tmp-1 '(_ any))))
3331 (if tmp
3332 (apply (lambda (e)
3333 (list '#(syntax-object lambda ((top)) (hygiene guile))
3334 '(#(syntax-object x ((top)) (hygiene guile)))
3335 '#((#(syntax-object macro-type ((top)) (hygiene guile))
3336 .
3337 #(syntax-object
3338 identifier-syntax
3339 ((top)
3340 #(ribcage
3341 #(identifier-syntax)
3342 #((top))
3343 #(((hygiene guile)
3344 .
3345 #(syntax-object identifier-syntax ((top)) (hygiene guile))))))
3346 (hygiene guile))))
3347 (list '#(syntax-object syntax-case ((top)) (hygiene guile))
3348 '#(syntax-object x ((top)) (hygiene guile))
3349 '()
3350 (list '#(syntax-object id ((top)) (hygiene guile))
3351 '(#(syntax-object identifier? ((top)) (hygiene guile))
3352 (#(syntax-object syntax ((top)) (hygiene guile))
3353 #(syntax-object id ((top)) (hygiene guile))))
3354 (list '#(syntax-object syntax ((top)) (hygiene guile)) e))
3355 (list '(#(syntax-object _ ((top)) (hygiene guile))
3356 #(syntax-object x ((top)) (hygiene guile))
3357 #(syntax-object ... ((top)) (hygiene guile)))
3358 (list '#(syntax-object syntax ((top)) (hygiene guile))
3359 (cons e
3360 '(#(syntax-object x ((top)) (hygiene guile))
3361 #(syntax-object ... ((top)) (hygiene guile)))))))))
3362 tmp)
3363 (let ((tmp ($sc-dispatch
3364 tmp-1
3365 '(_ (any any)
3366 ((#(free-id #(syntax-object set! ((top)) (hygiene guile))) any any)
3367 any)))))
3368 (if (if tmp
3369 (apply (lambda (id exp1 var val exp2)
3370 (if (identifier? id) (identifier? var) #f))
3371 tmp)
3372 #f)
3373 (apply (lambda (id exp1 var val exp2)
3374 (list '#(syntax-object make-variable-transformer ((top)) (hygiene guile))
3375 (list '#(syntax-object lambda ((top)) (hygiene guile))
3376 '(#(syntax-object x ((top)) (hygiene guile)))
3377 '#((#(syntax-object macro-type ((top)) (hygiene guile))
3378 .
3379 #(syntax-object variable-transformer ((top)) (hygiene guile))))
3380 (list '#(syntax-object syntax-case ((top)) (hygiene guile))
3381 '#(syntax-object x ((top)) (hygiene guile))
3382 '(#(syntax-object set! ((top)) (hygiene guile)))
3383 (list (list '#(syntax-object set! ((top)) (hygiene guile)) var val)
3384 (list '#(syntax-object syntax ((top)) (hygiene guile)) exp2))
3385 (list (cons id
3386 '(#(syntax-object x ((top)) (hygiene guile))
3387 #(syntax-object ... ((top)) (hygiene guile))))
3388 (list '#(syntax-object syntax ((top)) (hygiene guile))
3389 (cons exp1
3390 '(#(syntax-object x ((top)) (hygiene guile))
3391 #(syntax-object ... ((top)) (hygiene guile))))))
3392 (list id
3393 (list '#(syntax-object identifier? ((top)) (hygiene guile))
3394 (list '#(syntax-object syntax ((top)) (hygiene guile)) id))
3395 (list '#(syntax-object syntax ((top)) (hygiene guile)) exp1))))))
3396 tmp)
3397 (syntax-violation
3398 #f
3399 "source expression failed to match any pattern"
3400 tmp-1)))))))))
3401
3402 (define define*
3403 (make-syntax-transformer
3404 'define*
3405 'macro
3406 (lambda (x)
3407 (let ((tmp-1 x))
3408 (let ((tmp ($sc-dispatch tmp-1 '(_ (any . any) any . each-any))))
3409 (if tmp
3410 (apply (lambda (id args b0 b1)
3411 (list '#(syntax-object define ((top)) (hygiene guile))
3412 id
3413 (cons '#(syntax-object lambda* ((top)) (hygiene guile))
3414 (cons args (cons b0 b1)))))
3415 tmp)
3416 (let ((tmp ($sc-dispatch tmp-1 '(_ any any))))
3417 (if (if tmp (apply (lambda (id val) (identifier? id)) tmp) #f)
3418 (apply (lambda (id val)
3419 (list '#(syntax-object define ((top)) (hygiene guile)) id val))
3420 tmp)
3421 (syntax-violation
3422 #f
3423 "source expression failed to match any pattern"
3424 tmp-1)))))))))
3425