summaryrefslogtreecommitdiffstats
path: root/Macexp.scm
blob: 68026998d2218908ca29c779990bfa25e97f0603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
;; Copyright (C) 1999 Free Software Foundation, Inc.
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this software; see the file COPYING.  If not, write to
;; the Free Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111, USA.
;;
;; As a special exception, the Free Software Foundation gives permission
;; for additional uses of the text contained in its release of SCM.
;;
;; The exception is that, if you link the SCM library with other files
;; to produce an executable, this does not by itself cause the
;; resulting executable to be covered by the GNU General Public License.
;; Your use of that executable is in no way restricted on account of
;; linking the SCM library code into it.
;;
;; This exception does not however invalidate any other reasons why
;; the executable file might be covered by the GNU General Public License.
;;
;; This exception applies only to the code released by the
;; Free Software Foundation under the name SCM.  If you copy
;; code from other Free Software Foundation releases into a copy of
;; SCM, as the General Public License permits, the exception does
;; not apply to the code that you add in this way.  To avoid misleading
;; anyone as to the status of such modified files, you must delete
;; this exception notice from them.
;;
;; If you write modifications of your own for SCM, it is your choice
;; whether to permit this exception to apply to your modifications.
;; If you do not wish that, delete this exception notice.

;;;; "Macexp.scm", macro expansion, respecting hygiene.
;;; Author: Radey Shouman

;; LLIST is a lambda list, BINDINGS an alist using the same identifiers.
(define extended-environment
  (let ((env:annotation-marker 4))
    (lambda (llist bindings env)
      (cons llist (cons env:annotation-marker (cons bindings env))))))

(define syntax-extended-environment
  (let ((env:syntax-marker 6))
    ;;BINDINGS is an alist
    (lambda (bindings env)
      (cons (cons env:syntax-marker bindings) env))))

(define (environment-ref env id)
  (environment-annotation id env))

;;(debind:if_ ?llist ?val ?body ?alt)
;;?alt should evaluate to a procedure taking two arguments, a pattern
;;and an object.  This macro requires the (... ...) ellipsis quote
;;extension.
(define-syntax debind:if
  (syntax-rules ()
    ((_ "PARSE LLIST" () ?val ?body ?alt)
     (if (null? ?val) ?body (?alt '() ?val)))

    ((_ "PARSE LLIST" (?pattern (... ...)) ?val ?body ?alt)
     (let loop ((v ?val))
       (cond ((null? v)
	      (debind:if "PARSE ..." ?pattern ?val ?body))
	     ((pair? v)
	      (let ((carv (car v)))
		(debind:if "PARSE LLIST" ?pattern carv
			   (loop (cdr v)) ?alt)))
	     (else
	      (?alt '(?pattern (... ...)) ?val)))))

    ((_ "PARSE LLIST" (?first . ?rest) ?val ?body ?alt)
     (if (pair? ?val)
	 (let ((carv (car ?val))
	       (cdrv (cdr ?val)))
	   (debind:if "PARSE LLIST" ?first carv
		      (debind:if "PARSE LLIST" ?rest cdrv ?body ?alt)
		      ?alt))
	 (?alt '(?first . ?rest) ?val)))

    ((_ "PARSE LLIST" ?name ?val ?body ?alt)
     (let ((?name ?val)) ?body))

    ((_ "PARSE ..." () ?val ?body)
     ?body)

    ((_ "PARSE ..." (?pattern (... ...)) ?val ?body)
     (debind:if "PARSE ..." ?pattern ?val ?body))

    ((_ "PARSE ..." (?first . ?rest) ?val ?body)
     (debind:if "PARSE ..." ?first (map car ?val)
	(debind:if "PARSE ..." ?rest (map cdr ?val)
			    ?body)))

    ((_ "PARSE ..." ?name ?val ?body)
     (let ((?name ?val)) ?body))

    ((_ ?llist ?val ?body)
     (debind:if ?llist ?val ?body
		(lambda (pat val "debind:if" '?llist val
			     "does not match" pat))))

    ((_ ?llist ?val ?body ?alt)
     (let ((val ?val)
	   (alt ?alt))
       (debind:if "PARSE LLIST" ?llist val ?body alt)))))

;; Uncomment for DESTRUCTURING-BIND enhanced with ellipsis (...) patterns.
;(define-syntax destructuring-bind
;  (syntax-rules ()
;    ((_ ?llist ?val ?body1 ?body ...)
;     (debind:if ?llist ?val
;		(let () ?body1 ?body ...)
;		(lambda (pat val)
;		  (slib:error 'destructuring-bind '?llist
;			      val "does not match" pat))))))

;; This should really dispatch on the keyword only, then
;; use destructuring-case for each keyword, that way errors
;; may be more accurately reported for primitives.
;;
;;(keyword-case expr env (pattern body ...) ...)
(define-syntax keyword-case
  (syntax-rules (else)
    ((_ "RECURSE" ?expr ?env)
     (error 'keyword-case ?expr "not matched"))
    ((_ "RECURSE" ?expr ?env
	(else ?body1 ?body ...))
     (let () ?body1 ?body ...))
    ((_ "RECURSE" ?expr ?env
	((?keyword . ?pattern) ?body1 ?body ...)
	?clause ...)
     (let ((alt (lambda (ignore1 ignore2)
		  (keyword-case "RECURSE" ?expr ?env ?clause ...))))
       ;;Keywords are renamed in the top-level environment for each
       ;;comparison, this is wasteful and somewhat ugly.
       (if (identifier-equal? (renamed-identifier '?keyword '())
			      (car ?expr) ?env)
	   (debind:if ?pattern (cdr ?expr)
		      (let () ?body1 ?body ...)
		      alt)
	   (alt #f #f))))
    ((_ ?expr ?env ?clause1 ?clause ...)
     (let ((expr ?expr))
       (if (or (not (pair? expr))
	       (not (identifier? (car expr))))
	   (error 'keyword-case expr "bad form")
	   (keyword-case "RECURSE" expr ?env ?clause1 ?clause ...))))))

;; This is still not safe when ENV has non-macro bindings in it.
;; It could be made safe by rebuilding an equivalent environment,
;; retaining values only for syntactic bindings.
(define (macro:expand-syntax form env pretty? verbose?)
  (define globals '())
  (define shadowed-globals '())
  (define top-lambda (renamed-identifier 'LAMBDA #f))
  (define top-let (renamed-identifier 'LET #f))
  (define top-let* (renamed-identifier 'LET* #f))
  (define top-letrec (renamed-identifier 'LETREC #f))
  (define top-arrow (renamed-identifier '=> #f))
  (define top-else (renamed-identifier 'ELSE #f))
  (define top-define (renamed-identifier 'DEFINE #f))
  (define top-begin (renamed-identifier 'BEGIN #f))
  (define (arrow? id env)
    (and (identifier? id)
	 (identifier-equal? id top-arrow env)))
  (define (else? id env)
    (and (identifier? id)
	 (identifier-equal? id top-else env)))
  (define (define? form env)
    (and (list? form)			;FORM will have been expanded.
	 (identifier? (car form))
	 (identifier-equal? top-define (car form) env)))
  (define (begin? form env)
    (and (list? form)
	 (identifier? (car form))
	 (identifier-equal? (car form) top-begin env)))

  (define locally-bound? environment-annotation)

  (define pretty-name
    (if pretty?
	(letrec ((counter 0)
		 (genname
		  (lambda (sym)
		    (set! counter (+ counter 1))
		    (string->symbol
		     (string-append (symbol->string sym)
				    "|" (number->string counter))))))
	  (lambda (name env)
	    (if (symbol? name)
		(if (or (memq name
			      '(LAMBDA LET LET* LETREC DO DEFINE SET!
				       BEGIN IF COND CASE AND OR QUOTE
				       QUASIQUOTE UNQUOTE UNQUOTE-SPLICING
				       DEFINE-SYNTAX LET-SYNTAX LETREC-SYNTAX
				       SYNTAX-QUOTE ELSE =>))
			(locally-bound? name env))
		    (genname name)
		    name)
		(genname (identifier->symbol name)))))
	identity))

  ;; Local bindings -> (identifier pretty-name (usage-context ...))
  ;; This will change.
  (define (initial-binding name env)
    (or (identifier? name)
	(slib:error 'macro:expand name "not identifier"))
    (list name (pretty-name name env) '()))
  (define binding->name cadr)
  (define binding->contexts caddr)
  (define (binding-add-context! b context)
    (let ((ctx (caddr b)))
      (if (not (list? ctx))
	  (error 'not-a-list ctx))
      (or (memq context ctx)
	  (set-car! (cddr b) (cons context ctx)))))

  ;; Produces an alist
  (define (llist->bindings llist env)
    (let recurse ((ll llist))
      (cond ((pair? ll)
	     (cons (initial-binding (car ll) env)
		   (recurse (cdr ll))))
	    ((identifier? ll)
	     (list (initial-binding ll env)))
	    ((null? ll) ll)
	    (else (error 'strange-lambda-list llist)))))

  (define (expand-begin forms env context)
    (if (null? forms)
	'()
	(let recurse ((forms forms))
	  (if (null? (cdr forms))
	      (list (expand (car forms) env context))
	      (cons (expand (car forms) env 'SIDE-EFFECT)
		    (recurse (cdr forms)))))))

  (define (expand-body forms env context)
    (define (rewrite forms defs)
      (if (null? defs)
	  (expand-begin forms env context)
	  (list
	   (expand-primitive
	    `(,top-letrec ,(reverse defs) ;reverse just to make it pretty
			  ,@forms) env context))))
    (let loop ((forms forms)
	       (defs '()))
      (if (null? (cdr forms))
	  (rewrite forms defs)
	  (let ((form1 (expand (car forms) env 'SIDE-EFFECT)))
	    (cond ((define? form1 env)
		   (loop (cdr forms)
			 (cons (cdr form1) defs)))
		  ((begin? form1 env)
		   (loop (append (cdr form1) (cdr forms))
			 defs))
		  (else (rewrite forms defs)))))))

  (define (lookup id env)
    (or (environment-ref env id)
	(let* ((sym (identifier->symbol id))
	       (binding (cond ((assq sym globals))
			      (else
			       (let ((b (initial-binding sym env)))
				 (set! globals (cons b globals))
				 b)))))
	  (cond ((not pretty?) id)
		((not (locally-bound? sym env)))
		((assq sym shadowed-globals))
		(else
		 (set! shadowed-globals
		       (cons (cons sym (binding->name binding))
			     shadowed-globals))))
	    binding)))

  (define pretty-varref
    (if pretty?
	(lambda (id env)
	  (if (symbol? id)
	      id
	      (let ((sym (identifier->symbol id)))
		(if (identifier-equal? id sym env) sym id))))
	(lambda (id env) id)))

  (define unpaint
    (if pretty?
	(lambda (x)
	  (cond ((symbol? x) x)
		((identifier? x) (identifier->symbol x))
		((pair? x) (cons (unpaint (car x)) (unpaint (cdr x))))
		((vector? x) (let* ((n (vector-length x))
				    (v (make-vector n)))
			       (do ((i 0 (+ i 1)))
				   ((>= i n) v)
				 (vector-set! v i (unpaint (vector-ref x i))))))
		(else x)))
	identity))

  (define (expand* forms env context)
    (map (lambda (form) (expand form env context)) forms))

  (define (expand-primitive form env context)
    (define keyword (and (pair? form)
			 (if pretty?
			     (identifier->symbol (car form))
			     (car form))))
    (keyword-case
     form env
     ;;Binding forms
     ((LAMBDA llist body1 body ...)
      (let* ((bindings (llist->bindings llist env))
	     (env (extended-environment llist bindings env))
	     (body (expand-body (cons body1 body) env context))
	     (llist (let recurse ((ll llist)
				  (bl bindings))
		      (cond ((null? ll) '())
			    ((pair? ll) (cons (binding->name (car bl))
					      (recurse (cdr ll) (cdr bl))))
			    (else (binding->name  bl))))))
	`(,keyword ,llist ,@body)))
     ((LET ((names values) ...) body1 body ...)
      (let* ((values (expand* values env 'VALUE))
	     (bindings (llist->bindings names env))
	     (env (extended-environment names bindings env))
	     (body (expand-body (cons body1 body) env context)))
	`(,keyword ,(map (lambda (b val) (list (binding->name b) val))
			 bindings values)
		   ,@body)))
     ((LET name1 ((names values) ...) body1 body ...)
      (expand `((,top-letrec
		 ((,name1 (,(pretty-varref top-lambda env) ,names
			   ,@(cons body1 body))))
		 ,name1) ,@values)
	      env context))
     ((LETREC ((names values) ...) body1 body ...)
      (let* ((bindings (llist->bindings names env))
	     (env (extended-environment names bindings env))
	     (values (expand* values env 'VALUE))
	     (body (expand-body (cons body1 body) env context)))
	`(,keyword ,(map (lambda (b val) (list (binding->name b) val))
			 bindings values)
		   ,@body)))
     ((LET* ((names values) ...) body1 body ...)
      (let recurse ((ns names)
		    (vs values)
		    (env env)
		    (bs '()))
	(if (null? ns)
	    (let ((body (expand-body (cons body1 body) env context)))
	      `(,keyword ,(reverse bs) ,@body))
	    (let ((binding (initial-binding (car ns) env)))
	      (recurse (cdr ns) (cdr vs)
		       (extended-environment (car ns)
					     (list binding)
					     env)
		       (cons (list (binding->name binding)
				   (expand (car vs) env 'VALUE))
			     bs))))))
     ((DO ((names inits . steps) ...)
	  (test exit ...)
	  body ...)
      (let* ((steps (map (lambda (name step)
			   (if (null? step) name (car step)))
			 names steps))
	     (inits (expand* inits env 'VALUE))
	     (bindings (llist->bindings names env))
	     (env (extended-environment names bindings env))
	     (steps (expand* steps env 'VALUE))
	     (test (expand test env 'BOOLEAN))
	     (exit (expand-begin exit env context))
	     (body (expand-begin body env 'SIDE-EFFECT)))
	`(,keyword
	  ,(map (lambda (binding init step)
		  (list (binding->name binding) init step))
		bindings inits steps)
	  ,(cons test exit)
	  ,@body)))
     ((DEFINE (name . llist) body ...)
      (expand-primitive
       `(,keyword ,name (,top-lambda ,llist ,@body)) env context))
     ((DEFINE name value)
      (cond ((null? env)		;Top level
	     (binding-add-context! (lookup name env) 'DEFINE)
	     `(,keyword ,(pretty-varref name env)
			,(expand value env 'VALUE)))
	    (else
	     `(,keyword ,name ,value)))) ;Expansion will be done by expand-body.

     ((SET! var value)
      (let ((b (lookup var env)))
	(binding-add-context! b 'SET!)
	`(,keyword ,(binding->name b) ,(expand value env 'VALUE))))

     ;;Non-binding forms
     ((BEGIN body ...)
      (let ((body (expand-begin body env context)))
	(if (null? (cdr body))
	    (car body)
	    `(,keyword ,@body))))
     ((IF test conseq . alt)
      `(,keyword ,(expand test env 'BOOLEAN)
		 ,(expand conseq env context)
		 ,@(if (pair? alt)
		       (list (expand (car alt) env context))
		       '())))
     ((COND (test exprs ...) ...)
      `(,keyword
	,@(map (lambda (test exprs)
		 (cond ((null? exprs) (list (expand test env context)))
		       ((arrow? (car exprs) env)
			(list (expand test env 'VALUE)
			      (pretty-varref top-arrow env)
			      (expand (cadr exprs) env 'PROCEDURE)))
		       ((else? test env)
			(cons (pretty-varref top-else env)
			      (expand-begin exprs env context)))
		       (else
			(cons (expand test env 'BOOLEAN)
			      (expand-begin exprs env context)))))
	       test exprs)))
     ((CASE obj (datums exprs ...) ...)
      `(,keyword ,(expand obj env 'VALUE)
	,@(map (lambda (datums exprs)
		 (cons (if (else? datums env)
			   (pretty-varref datums env)
			   (unpaint datums))
		       (expand-begin exprs env context)))
	       datums exprs)))
     ((AND forms ...)
      `(,keyword ,@(expand* forms env context)))
     ((OR forms ...)
      `(,keyword ,@(expand* forms env context)))

     ;; Should unpaint synthetic identifiers
     ((QUOTE obj)
      `(,keyword ,(unpaint obj)))
     ((QUASIQUOTE obj)
      `(,keyword
	,(let qexp ((obj obj)
		    (depth 0))
	   (cond ((not (pair? obj))
		  (unpaint obj))
		 ((identifier? (car obj))
		  (let ((keyword (car obj)))
		    (keyword-case
		     obj env
		     ((QUASIQUOTE arg)
		      (list keyword (qexp arg (+ depth 1))))
		     ((UNQUOTE arg)
		      (list keyword
			    (if (zero? depth)
				(expand arg env context)
				(qexp arg (- depth 1)))))
		     ((UNQUOTE-SPLICING arg)
		      (list keyword
			    (if (zero? depth)
				(expand arg env context)
				(qexp arg (- depth 1)))))
		     (else
		      (cons (unpaint keyword) (qexp (cdr obj) depth))))))
		 (else
		  (cons (qexp (car obj) depth)
			(qexp (cdr obj) depth)))))))
     ((DEFINE-SYNTAX name def)
      form)
     ((LET-SYNTAX ((names defs) ...) body1 body ...)
      (let* ((env (syntax-extended-environment '() env))
	     (defs (map (lambda (name def)
			  (cons name (eval-syntax def env)))
			names defs))
	     (env (syntax-extended-environment defs env))
	     (body (expand-body (cons body1 body) env context)))
	(if pretty?
	    `(,(pretty-varref top-let env) () ,@body)
	    `(,top-let* (,(list marker #f)) ,@body))))
     ((LETREC-SYNTAX ((names defs) ...) body1 body ...)
      (let* ((eframe (map (lambda (name) (cons name #f)) names))
	     (env (syntax-extended-environment eframe env)))
	(do ((ds defs (cdr ds))
	     (ef eframe (cdr ef)))
	    ((null? ds))
	  (set-cdr! (car ef) (eval-syntax (car ds) env)))
	(let ((body (expand-body (cons body1 body) env context)))
	  (if pretty?
	      `(,(pretty-varref top-let env) () ,@body)
	      `(,top-let* (,(list marker #f)) ,@body)))))
     ;;SCM extension
     ((SYNTAX-QUOTE obj)
      `(,keyword ,obj))
     (else
      (warn 'expand-syntax "Unexpected primitive syntax" form)
      form)))

  (define (handle-shadowed form env)
    (if (define? form env)
	(list (car form) (cadr form)
	      (handle-shadowed (caddr form)))
	`(,(pretty-varref top-let env)
	  ,(map (lambda (s)
		  (list (cdr s)
			(if (environment-ref env (car s))
			    (renamed-identifier (car s) #f)
			    (car s))))
		shadowed-globals)
	  ,form)))

  (define (expand form env context)
    (cond
     ((identifier? form)
      (let ((expanded (@macroexpand1 form env)))
	(cond ((eq? form expanded) form)
	      ((not expanded)
	       (let* ((b (lookup form env))
		      (name (binding->name b)))
		 (binding-add-context! b context)
		 name))
	      (else expanded))))
     ((number? form) form)
     ((char? form) form)
     ((boolean? form) form)
     ((null? form) form)
     ((string? form) form)
     ((list? form)
      (if (identifier? (car form))
	  (let ((expanded (@macroexpand1 form env)))
	    (cond ((eq? expanded form)
		   (expand-primitive form env context))
		  ((not expanded)
		   (cons (expand (car form) env 'PROCEDURE)
			 (map (lambda (arg)
				(expand arg env 'VALUE))
			      (cdr form))))
		  (else
		   (expand expanded env context))))
	  (cons (expand (car form) env 'PROCEDURE)
		(expand* (cdr form) env 'VALUE))))
     (else
      (warn 'expand-syntax "Unexpected type of form" form)
      form)))

  (let ((res (expand form env 'TOP)))
    (cond (verbose?
	   (display "Globals: ")
	   (pretty-print globals)
	   (display "Shadowed Globals: ")
	   (pretty-print shadowed-globals)))
    (cond ((null? shadowed-globals) res)
	  ((not (begin? res env)) (handle-shadowed res env))
	  (else (cons (car res)
		      (map (lambda (form)
			     (handle-shadowed form env))
			   (cdr res)))))))

(define (macro:expand form . opt)
  (macro:expand-syntax form '()
		       (not (memq 'not-pretty opt))
		       (memq 'verbose opt)))

;; Debugging fodder.
(begin
  (define (read* filename)
    (call-with-input-file filename
      (lambda (p)
	(let loop ((forms '()))
	  (let ((form (read p)))
	    (if (eof-object? form)
		(cons 'BEGIN (reverse forms))
		(loop (cons form forms))))))))
  (define (expand-file filename . opt)
    (apply macro:expand (read* filename) '() opt))
  (define s (read*  (or *load-pathname* "Macexp.scm"))))


;;; Local Variables:
;;; mode:scheme
;;; eval:(put 'destructuring-bind 'scheme-indent-function 1)
;;; eval:(put 'destructuring-case 'scheme-indent-function 1)
;;; End: