-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompile.scm
1617 lines (1342 loc) · 45.3 KB
/
compile.scm
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Stak Scheme compiler.
;
; All compiler-internal variables contain at least one `$` or `%` character in their names.
(import
(scheme base)
(scheme cxr)
(scheme inexact)
(scheme lazy)
(scheme process-context)
(scheme read)
(scheme write))
(cond-expand
(stak
(define cons-rib cons)
(define target-procedure? procedure?))
(else
(define-record-type *rib*
(rib car cdr tag)
rib?
(car rib-car)
(cdr rib-cdr)
(tag rib-tag))
(define (cons-rib car cdr)
(rib car cdr pair-type))
(define (target-procedure? value)
(and (rib? value) (eq? (rib-tag value) procedure-type)))
(define string->uninterned-symbol string->symbol)))
; Instructions
(define constant-instruction 0)
(define get-instruction 1)
(define set-instruction 2)
(define if-instruction 3)
(define nop-instruction 4)
(define call-instruction 5)
; Primitives
(define primitives
'(($$rib 0)
($$close 1)
($$< 9)
($$+ 10)
($$- 11)
($$* 12)
($$/ 13)))
; Types
(define pair-type 0)
(define null-type 1)
(define boolean-type 2)
(define procedure-type 3)
(define symbol-type 4)
(define string-type 5)
(define char-type 6)
(define vector-type 7)
(define bytevector-type 8)
; Utility
(define (debug . xs)
(write xs (current-error-port))
(newline (current-error-port)))
(define (code-rib tag car cdr)
(rib car cdr tag))
(define (call-rib arity procedure continuation)
(code-rib (+ call-instruction arity) procedure continuation))
(define (constant-rib constant continuation)
(code-rib constant-instruction constant continuation))
(define (data-rib type car cdr)
(rib car cdr type))
(define (make-procedure arity code environment)
(data-rib procedure-type (cons-rib arity code) environment))
(define (procedure-code procedure)
(rib-cdr (rib-car procedure)))
(define (bytevector->list xs)
(let loop ((index 0) (result '()))
(if (< index (bytevector-length xs))
(cons
(bytevector-u8-ref xs index)
(loop (+ 1 index) result))
result)))
(define (last-cdr xs)
(if (pair? xs)
(last-cdr (cdr xs))
xs))
(define (set-last-cdr! xs x)
(if (pair? (cdr xs))
(set-last-cdr! (cdr xs) x)
(set-cdr! xs x)))
(define (filter f xs)
(if (null? xs)
'()
(let ((x (car xs))
(xs (filter f (cdr xs))))
(if (f x)
(cons x xs)
xs))))
(define (fold-left f y xs)
(if (null? xs)
y
(fold-left
f
(f y (car xs))
(cdr xs))))
(define (list-head xs n)
(if (zero? n)
'()
(cons
(car xs)
(list-head (cdr xs) (- n 1)))))
(define (list-position f xs)
(let loop ((xs xs) (index 0))
(cond
((null? xs)
#f)
((f (car xs))
index)
(else
(loop (cdr xs) (+ index 1))))))
(define (member-position x xs . rest)
(define eq? (if (null? rest) equal? (car rest)))
(list-position (lambda (y) (eq? x y)) xs))
(define (memv-position x xs)
(member-position x xs eqv?))
(define (memq-position x xs)
(member-position x xs eq?))
(define (flat-map f xs)
(apply append (map f xs)))
(define (relaxed-length xs)
(do ((xs xs (cdr xs)) (y 0 (+ y 1)))
((not (pair? xs))
y)))
(define (relaxed-map f xs)
(if (pair? xs)
(cons
(f (car xs))
(relaxed-map f (cdr xs)))
(f xs)))
(define (relaxed-deep-map f xs)
(if (pair? xs)
(cons
(relaxed-deep-map f (car xs))
(relaxed-deep-map f (cdr xs)))
(f xs)))
(define (maybe-append xs ys)
(and xs ys (append xs ys)))
(define (unique xs)
(if (null? xs)
'()
(let ((ys (unique (cdr xs))))
(if (memq (car xs) ys)
ys
(cons (car xs) ys)))))
(define (deep-unique x)
(cond
((and (pair? x) (symbol? (car x)))
(unique (cons (car x) (deep-unique (cdr x)))))
((pair? x)
(deep-unique
(append
(deep-unique (car x))
(deep-unique (cdr x)))))
((symbol? x)
(list x))
(else
'())))
(define (map-values f xs)
(map (lambda (pair) (cons (car pair) (f (cdr pair)))) xs))
(define (filter-values f xs)
(filter (lambda (pair) (f (cdr pair))) xs))
; TODO Set a true machine epsilon.
(define epsilon
(let ((x (/ 1 10000000 100000000)))
(if (zero? x) 1 x)))
(define (predicate expression)
(and (pair? expression) (car expression)))
(define (count-parameters parameters)
(if (pair? parameters)
(+ 1 (count-parameters (cdr parameters)))
0))
(define (parameter-names parameters)
(cond
((pair? parameters)
(cons (car parameters) (parameter-names (cdr parameters))))
((symbol? parameters)
(list parameters))
((null? parameters)
'())
(else
(error "invalid variadic parameter" parameters))))
(define (symbol-append . xs)
(string->symbol (apply string-append (map symbol->string xs))))
(define (id->string id)
(number->string id 32))
; Source code reading
(define (read-all)
(let ((x (read)))
(if (eof-object? x)
'()
(cons x (read-all)))))
(define (read-source)
(cons
'$$begin
; Keep an invariant that a `begin` body must not be empty.
(cons #f (read-all))))
; Library system
;; Types
(define-record-type library
(make-library id name exports imports body symbols)
library?
(id library-id)
(name library-name)
(exports library-exports)
(imports library-imports)
(body library-body)
(symbols library-symbols))
(define-record-type library-state
(make-library-state library imported)
library-state?
(library library-state-library)
(imported library-state-imported library-state-set-imported!))
(define-record-type library-context
(make-library-context libraries name-maps)
library-context?
(libraries library-context-libraries library-context-set-libraries!)
(name-maps library-context-name-maps library-context-set-name-maps!))
(define (library-context-assoc context name)
(cond
((assoc name (library-context-libraries context)) =>
cdr)
(else
(error "unknown library" name))))
(define (library-context-id context)
(length (library-context-libraries context)))
(define (library-context-find context name)
(library-state-library (library-context-assoc context name)))
(define (library-context-add! context library)
(library-context-set-libraries!
context
(cons
(cons
(library-name library)
(make-library-state library #f))
(library-context-libraries context))))
(define (library-context-import! context name)
(let* ((state (library-context-assoc context name))
(imported (library-state-imported state)))
(library-state-set-imported! state #t)
imported))
;; Procedures
(define library-symbol-separator #\%)
(define (library-symbol? name)
(memv library-symbol-separator (string->list (symbol->string name))))
(define (build-library-name id name)
(string-append
(id->string id)
(list->string (list library-symbol-separator))
(symbol->string name)))
; TODO Remove library symbol prefixes.
(define (resolve-library-symbol name)
(let* ((string (symbol->string name))
(position (memv-position library-symbol-separator (string->list string))))
(if position
(string->symbol (string-copy string (+ position 1)))
name)))
(define (rename-library-symbol context id name)
(if (or
(not id)
(let ((name (symbol->string name)))
(equal? (substring name 0 (min 2 (string-length name))) "$$")))
name
(let* ((maps (library-context-name-maps context))
(pair (or (assq id maps) (cons id '())))
(names (cdr pair)))
(when (null? names)
(library-context-set-name-maps! context (cons pair maps)))
(let ((names (cdr pair)))
(cond
((assq name names) =>
cdr)
(else
(let ((renamed (string->uninterned-symbol (build-library-name id name))))
(set-cdr! pair (cons (cons name renamed) names))
renamed)))))))
(define (expand-import-set context importer-id qualify set)
(define (expand qualify)
(expand-import-set context importer-id qualify (cadr set)))
(case (predicate set)
((except)
(let ((names (cddr set)))
(expand
(lambda (name)
(if (memq name names)
#f
(qualify name))))))
((only)
(let ((names (cddr set)))
(expand
(lambda (name)
(if (memq name names)
(qualify name)
#f)))))
((prefix)
(expand
(lambda (name)
(qualify (symbol-append (caddr set) name)))))
((rename)
(expand
(lambda (name)
(qualify
(cond
((assq name (cddr set)) =>
cadr)
(else
name))))))
(else
(let ((library (library-context-find context set)))
(append
(if (library-context-import! context set)
'()
(append
(expand-import-sets
context
(library-id library)
(library-symbols library)
(library-imports library))
(library-body library)))
(flat-map
(lambda (names)
(let ((name (qualify (car names))))
(if name
(list
(list
'$$alias
(rename-library-symbol context importer-id name)
(cdr names)))
'())))
(library-exports library)))))))
(define (expand-import-sets context importer-id importer-symbols sets)
(flat-map
(lambda (set)
(expand-import-set
context
importer-id
(lambda (name)
(and
(or
(not importer-symbols)
(memq name (force importer-symbols)))
name))
set))
sets))
(define (expand-library-expression context body-symbols expression)
(case (and (pair? expression) (car expression))
((define-library)
(let* ((collect-bodies
(lambda (predicate)
(flat-map
cdr
(filter
(lambda (body) (eq? (car body) predicate))
(cddr expression)))))
(id (library-context-id context))
(exports (collect-bodies 'export))
(bodies (collect-bodies 'begin)))
(library-context-add!
context
(make-library
id
(cadr expression)
(map
(lambda (name)
(if (eq? (predicate name) 'rename)
(cons (caddr name) (rename-library-symbol context id (cadr name)))
(cons name (rename-library-symbol context id name))))
exports)
(collect-bodies 'import)
(relaxed-deep-map
(lambda (value)
(if (symbol? value)
(rename-library-symbol context id value)
value))
bodies)
(delay (deep-unique (cons exports bodies)))))
'()))
((import)
(expand-import-sets context #f body-symbols (cdr expression)))
(else
(list expression))))
(define library-predicates '(define-library import))
(define (expand-libraries expression)
(let* ((context (make-library-context '() '()))
(body-symbols
(delay
(deep-unique
(filter
(lambda (expression)
(not (and (pair? expression) (memq (car expression) library-predicates))))
(cdr expression)))))
(expression
(cons
(car expression)
(flat-map
(lambda (expression)
(expand-library-expression context body-symbols expression))
(cdr expression)))))
(values
expression
(map-values
library-exports
(map-values library-state-library (library-context-libraries context))))))
; Macro system
;; Types
(define-record-type macro-state
(make-macro-state id literals)
macro-state?
(id macro-state-id macro-state-set-id!)
(literals macro-state-literals macro-state-set-literals!))
(define-record-type macro-context
(make-macro-context state environment)
macro-context?
(state macro-context-state)
(environment macro-context-environment macro-context-set-environment!))
(define (macro-context-append context pairs)
(make-macro-context
(macro-context-state context)
(append pairs (macro-context-environment context))))
(define (macro-context-set! context name denotation)
(let* ((environment (macro-context-environment context))
(pair (assq name environment)))
(when pair (set-cdr! pair denotation))
pair))
(define (macro-context-set-last! context name denotation)
(unless (macro-context-set! context name denotation)
(let ((environment (macro-context-environment context))
(tail (list (cons name denotation))))
(if (null? environment)
(macro-context-set-environment! context tail)
(set-last-cdr! environment tail)))))
(define (macro-context-generate-id! context)
(let* ((state (macro-context-state context))
(id (macro-state-id state)))
(macro-state-set-id! state (+ id 1))
id))
(define (macro-context-append-literal! context name syntax)
(define state (macro-context-state context))
(macro-state-set-literals!
state
(cons
(cons name syntax)
(macro-state-literals state))))
(define-record-type rule-context
(make-rule-context definition-context use-context ellipsis literals)
rule-context?
(definition-context rule-context-definition-context)
(use-context rule-context-use-context)
(ellipsis rule-context-ellipsis)
(literals rule-context-literals))
;; Procedures
(define (resolve-denotation context expression)
(cond
((assq expression (macro-context-environment context)) =>
cdr)
(else
expression)))
(define (rename-variable context name)
; Share tails when appending strings.
(string->uninterned-symbol
(string-append
(id->string (macro-context-generate-id! context))
"$"
(symbol->string name))))
(define (find-pattern-variables ellipsis bound-variables pattern)
(define excluded-variables (cons ellipsis bound-variables))
(let loop ((pattern pattern) (variables '()))
(cond
((pair? pattern)
(loop
(car pattern)
(loop
(cdr pattern)
variables)))
((and (symbol? pattern) (not (memq pattern excluded-variables)))
(cons pattern variables))
(else
variables))))
(define-record-type ellipsis-match
(make-ellipsis-match value)
ellipsis-match?
(value ellipsis-match-value))
(define-record-type ellipsis-pattern
(make-ellipsis-pattern element variables)
ellipsis-pattern?
(element ellipsis-pattern-element)
(variables ellipsis-pattern-variables))
(define (compile-pattern context ellipsis literals pattern)
(define (compile pattern)
(compile-pattern context ellipsis literals pattern))
(cond
((not (pair? pattern))
pattern)
((and
(pair? (cdr pattern))
(eq? ellipsis (resolve-denotation context (cadr pattern))))
(cons
(make-ellipsis-pattern
(compile (car pattern))
(find-pattern-variables ellipsis literals (car pattern)))
(compile (cddr pattern))))
(else
(cons
(compile (car pattern))
(compile (cdr pattern))))))
(define (match-ellipsis-pattern context pattern expression)
(map-values
make-ellipsis-match
(apply
map
list
(ellipsis-pattern-variables pattern)
(map
(lambda (expression)
(match-pattern context (ellipsis-pattern-element pattern) expression))
expression))))
(define (match-pattern context pattern expression)
(define (match pattern expression)
(match-pattern context pattern expression))
(cond
((and
(symbol? pattern)
(memq pattern (rule-context-literals context)))
(unless (eq?
(resolve-denotation (rule-context-use-context context) expression)
(resolve-denotation (rule-context-definition-context context) pattern))
(raise #f))
'())
((symbol? pattern)
(list (cons pattern expression)))
((pair? pattern)
(cond
((ellipsis-pattern? (car pattern))
(let ((length (- (relaxed-length expression) (relaxed-length (cdr pattern)))))
(when (negative? length)
(raise #f))
(append
(match-ellipsis-pattern context (car pattern) (list-head expression length))
(match (cdr pattern) (list-tail expression length)))))
((pair? expression)
(append
(match (car pattern) (car expression))
(match (cdr pattern) (cdr expression))))
(else
(raise #f))))
((equal? pattern expression)
'())
(else
(raise #f))))
(define (fill-ellipsis-template context matches template)
(let* ((variables (ellipsis-pattern-variables template))
(template (ellipsis-pattern-element template))
(matches (filter (lambda (pair) (memq (car pair) variables)) matches))
(singleton-matches (filter-values (lambda (match) (not (ellipsis-match? match))) matches))
(ellipsis-matches (filter-values ellipsis-match? matches)))
(when (null? ellipsis-matches)
(error "no ellipsis pattern variables" template))
(apply
map
(lambda matches (fill-template context (append matches singleton-matches) template))
(map (lambda (pair) (ellipsis-match-value (cdr pair))) ellipsis-matches))))
(define (fill-template context matches template)
(define (fill template)
(fill-template context matches template))
(cond
((and (symbol? template) (assq template matches)) =>
cdr)
((pair? template)
(append
(let ((first (car template)))
(if (ellipsis-pattern? first)
(fill-ellipsis-template context matches first)
(list (fill first))))
(fill (cdr template))))
(else
template)))
(define (make-transformer definition-context transformer)
(let-values (((transformer definition-context) (expand-outer-macro definition-context transformer)))
(case (resolve-denotation definition-context (predicate transformer))
(($$syntax-rules)
(let* ((ellipsis (resolve-denotation definition-context (cadr transformer)))
(literals (caddr transformer))
(rules
(map
(lambda (rule)
(map
(lambda (pattern)
(compile-pattern definition-context ellipsis literals pattern))
rule))
(cdddr transformer))))
(lambda (use-context expression)
(let loop ((rules rules))
(unless (pair? rules)
(error "invalid syntax" expression))
(let ((rule (car rules))
(rule-context (make-rule-context definition-context use-context ellipsis literals)))
(guard (value
((not value)
(loop (cdr rules))))
(let* ((matches (match-pattern rule-context (car rule) expression))
(template (cadr rule))
(names
(map
(lambda (name) (cons name (rename-variable use-context name)))
(find-pattern-variables ellipsis (append literals (map car matches)) template))))
(values
(fill-template rule-context (append names matches) template)
(macro-context-append
use-context
(map
(lambda (pair)
(cons
(cdr pair)
(resolve-denotation definition-context (car pair))))
names))))))))))
(else
(error "unsupported macro transformer" transformer)))))
(define (expand-outer-macro context expression)
(if (pair? expression)
(let ((value (resolve-denotation context (car expression))))
(if (procedure? value)
(let-values (((expression context) (value context expression)))
(expand-outer-macro context expression))
(values expression context)))
(values expression context)))
; https://www.researchgate.net/publication/220997237_Macros_That_Work
(define (expand-macro context expression)
(define (expand expression)
(expand-macro context expression))
(define (resolve name)
(resolve-denotation context name))
(cond
((symbol? expression)
(let ((value (resolve expression)))
(when (procedure? value)
(error "invalid syntax" expression))
value))
((pair? expression)
(case (resolve (car expression))
(($$alias)
(macro-context-set-last!
context
(cadr expression)
(resolve (caddr expression)))
(macro-context-append-literal!
context
(cadr expression)
(caddr expression))
#f)
(($$define)
(let ((name (cadr expression)))
(macro-context-set! context name name)
(expand (cons '$$set! (cdr expression)))))
(($$define-syntax)
(macro-context-set-last!
context
(cadr expression)
(make-transformer context (caddr expression)))
(macro-context-append-literal!
context
(cadr expression)
(caddr expression))
#f)
(($$lambda)
(let* ((parameters (cadr expression))
(context
(macro-context-append
context
(map
(lambda (name) (cons name (rename-variable context name)))
(parameter-names parameters))))
; We need to resolve parameter denotations before expanding a body.
(parameters
(relaxed-deep-map
(lambda (name) (resolve-denotation context name))
parameters)))
(list
'$$lambda
parameters
(expand-macro context (caddr expression)))))
(($$let-syntax)
(expand-macro
(macro-context-append
context
(map-values
(lambda (transformer)
(make-transformer context (car transformer)))
(cadr expression)))
(caddr expression)))
(($$letrec-syntax)
(let* ((bindings (cadr expression))
(context
(macro-context-append
context
(map-values
(lambda (value) #f)
bindings))))
(for-each
(lambda (pair)
(macro-context-set!
context
(car pair)
(make-transformer context (cadr pair))))
bindings)
(expand-macro context (caddr expression))))
(($$quote)
(cons
'$$quote
(relaxed-deep-map
(lambda (value)
(if (symbol? value)
(resolve-library-symbol value)
value))
(cdr expression))))
(else =>
(lambda (value)
(if (procedure? value)
(let-values (((expression context) (value context expression)))
(expand-macro context expression))
(map expand expression))))))
(else
expression)))
(define (expand-macros expression)
(let* ((context (make-macro-context (make-macro-state 0 '()) '()))
(expression (expand-macro context expression)))
(values
expression
(reverse
(filter
(lambda (pair) (library-symbol? (car pair)))
(macro-state-literals (macro-context-state context)))))))
; Optimization
(define-record-type optimization-context
(make-optimization-context optimizers literals)
optimization-context?
(optimizers optimization-context-optimizers optimization-context-set-optimizers!)
(literals optimization-context-literals optimization-context-set-literals!))
(define (optimization-context-append! context name optimizer)
(optimization-context-set-optimizers!
context
(cons
(cons name optimizer)
(optimization-context-optimizers context))))
(define (optimization-context-append-literal! context name literal)
(optimization-context-set-literals!
context
(cons
(cons name literal)
(optimization-context-literals context))))
(define (make-optimizer name optimizer)
(define (match-pattern pattern expression)
(cond
((and (pair? pattern) (pair? expression))
(maybe-append
(match-pattern (car pattern) (car expression))
(match-pattern (cdr pattern) (cdr expression))))
((symbol? pattern)
(list (cons pattern expression)))
((equal? pattern expression)
'())
(else
#f)))
(define (fill-template matches template)
(cond
((pair? template)
(cons
(fill-template matches (car template))
(fill-template matches (cdr template))))
((and (symbol? template) (assq template matches)) =>
cdr)
(else
template)))
(case (car optimizer)
(($$syntax-rules)
(let ((rules (cdddr optimizer)))
(lambda (expression)
(let loop ((rules rules))
(if (null? rules)
expression
(let ((rule (car rules)))
(cond
((match-pattern (car rule) expression) =>
(lambda (matches)
(fill-template matches (cadr rule))))
(else
(loop (cdr rules))))))))))
(else
(error "unsupported optimizer" optimizer))))
(define (optimize-expression context expression)
(define (optimize expression)
(optimize-expression context expression))
(if (pair? expression)
(let* ((expression (relaxed-map optimize expression))
(predicate (car expression)))
(cond
((eq? predicate '$$define-optimizer)
(let ((name (cadr expression)))
(optimization-context-append! context name (make-optimizer name (caddr expression)))
(optimization-context-append-literal! context name (caddr expression)))
#f)
((eq? predicate '$$begin)
; Omit top-level constants.
; TODO Define this pass by `define-optimizer`.
(cons '$$begin
(let loop ((expressions (cdr expression)))
(let ((expression (car expressions))
(expressions (cdr expressions)))
(cond
((null? expressions)
(list expression))
((pair? expression)
(cons expression (loop expressions)))
(else
(loop expressions)))))))
((assq predicate (optimization-context-optimizers context)) =>
(lambda (pair)
((cdr pair) expression)))
(else
expression)))
expression))
(define (optimize expression)
(let* ((context (make-optimization-context '() '()))
(expression (optimize-expression context expression)))
(values expression (optimization-context-literals context))))
; Compilation
;; Context
(define-record-type compilation-context
(make-compilation-context environment symbols libraries macros optimizers)
compilation-context?