-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.rkt
2505 lines (2295 loc) · 95.8 KB
/
utilities.rkt
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
;;
;; WARNING!!!!
;;
;; The original version of this file lives in the course-compiler repository.
;; There is a copy of this file in the public-student-support-code
;; repository.
;;
;; DO NOT EDIT the version of this file in public-student-support-code
;; because any changes you make will be OBLITERATED the next time
;; someone edits the version in the course-compiler repository
;; and then copies it over to public-student-support-code.
#lang racket
(require racket/struct)
;; Version 0.2
;; ---------------------
#|
This file is updated periodically based on the internal, reference
copy of the P423/523 compiler.
Changelog:
0.1: initial release, used 2016-2017.
0.2: update during Fall'17 semester.
We removed these functions:
* interp-test-suite
* compiler-test-suite
We added:
* parallel test running
* various other changes
0.3: updated during Fall'18 semester.
We removed multithreading
We added rackunit integration
0.4: update during Fall'20 semester.
Added structs for AST nodes.
|#
(require racket/pretty racket/match)
(require (for-syntax racket))
;(require racket/async-channel)
(require rackunit rackunit/text-ui );rackunit/gui
(define (symbolic? e)
(match e
[(AssignedFree x)
(symbol? x)]
[else
(symbol? e)]))
(provide debug-level AST-output-syntax at-debug-level? debug verbose copious
map2 map3 b2i i2b
racket-id->c-id
hash-union set-union*
fix while
label-name lookup extend-env make-dispatcher assert
fun-call? indirect-call?
read-fixnum read-program
compile compile-file check-passes-suite interp-tests compiler-tests
compiler-tests-gui compiler-tests-suite
use-minimal-set-of-registers!
general-registers num-registers-for-alloc registers-for-alloc
caller-save callee-save caller-save-for-alloc callee-save-for-alloc
arg-registers rootstack-reg register->color color->register
registers align byte-reg->full-reg print-by-type strip-has-type
make-lets dict-set-all dict-remove-all goto-label get-basic-blocks
symbol-append any-tag parse-program vector->set atm? fst
print-x86 print-x86-class
(contract-out [struct Prim ((op symbol?) (arg* exp-list?))])
(contract-out [struct Var ((name symbol?))])
(contract-out [struct Int ((value fixnum?))])
(contract-out [struct Let ((var symbolic?) (rhs exp?) (body exp?))])
(struct-out Program)
(struct-out ProgramDefsExp)
(struct-out ProgramDefs)
(struct-out CProgram)
(struct-out X86Program)
(contract-out [struct WhileLoop ((cnd exp?) (body exp?))])
(contract-out [struct SetBang ((var symbol?) (rhs exp?))])
(contract-out [struct GetBang ((var symbol?))])
(contract-out [struct Begin ((es exp-list?) (body exp?))])
(contract-out [struct Bool ((value boolean?))])
(contract-out [struct If ((cnd exp?) (thn exp?) (els exp?))])
(contract-out [struct HasType ((expr exp?) (type type?))])
(struct-out Void)
(contract-out [struct StructDef ((name symbol?) (field* param-list?))])
(contract-out [struct Apply ((fun exp?) (arg* exp-list?))])
(contract-out [struct Def ((name symbol?) (param* param-list?) (rty type?) (info any?)
(body any?))])
(contract-out [struct Lambda ((param* param-list?) (rty type?) (body exp?))])
(contract-out [struct FunRef ((name symbol?) (arity fixnum?))])
(struct-out Inject)
(struct-out Project)
(struct-out ValueOf)
(contract-out [struct Cast ((expr exp?) (source type?) (target type?))])
(struct-out Value)
(contract-out [struct Decl ((name symbol?) (type type?))])
(struct-out Poly)
(contract-out [struct Inst ((expr exp?)
(type type?)
(types type-list?))])
#;(struct-out TagOf)
(struct-out Exit)
(struct-out Closure)
(struct-out AssignedFree)
(contract-out [struct Assign ((lhs lhs?) (rhs exp?))])
(contract-out [struct Seq ((fst stmt?) (snd tail?))])
(contract-out [struct Return ((arg exp?))])
(contract-out [struct IfStmt ((cnd cmp?) (thn goto?) (els goto?))])
(contract-out [struct Goto ((label symbol?))])
(struct-out Collect)
(struct-out CollectionNeeded?)
(struct-out GlobalValue)
(struct-out Allocate)
(struct-out AllocateHom)
(struct-out AllocateClosure)
(struct-out AllocateProxy)
(contract-out [struct Call ((fun atm?) (arg* atm-list?))])
(contract-out [struct TailCall ((fun atm?) (arg* atm-list?))])
(contract-out [struct Imm ((value integer?))]) ;; allow 64-bit
(contract-out [struct Reg ((name symbol?))])
(contract-out [struct Deref ((reg symbol?) (offset fixnum?))])
(contract-out [struct Instr ((name symbol?) (arg* arg-list?))])
(contract-out [struct Callq ((target symbol?) (arity fixnum?))])
(contract-out [struct IndirectCallq ((target arg?) (arity fixnum?))])
(contract-out [struct IndirectJmp ((target arg?))])
(struct-out Retq)
(contract-out [struct Jmp ((target symbol?))])
(contract-out [struct TailJmp ((target arg?) (arity fixnum?))])
(contract-out [struct Block ((info any?) (instr* instr-list?))])
(struct-out StackArg)
(struct-out Global)
(contract-out [struct JmpIf ((cnd symbol?) (target symbol?))])
(contract-out [struct ByteReg ((name symbol?))])
(struct-out Tagged)
)
;; debug state is a nonnegative integer.
;; The easiest way to increment it is passing the -d option
;; to run-tests.rkt
;; 0 none
;; 1 trace passes in run-test
;; 2 debug macros
;; 3 verbose debugging
;; 4 (copious) absolutely everything
;; The higher the setting the more information is reported.
;; If you want the same functionality as previous incarnation
;; of utilities then uncomment the line after this definition
;; and change the number there.
(define debug-level
(make-parameter
0
(lambda (d)
(unless (exact-nonnegative-integer? d)
(error 'debug-state "expected nonnegative-integer in ~a" d))
d)))
;; (debug-level 2)
;; Check to see if debug state is at least some level
(define (at-debug-level? n)
(unless (exact-nonnegative-integer? n)
(error 'at-debug-level? "expected non-negative integer ~a" n))
(>= (debug-level) n))
(define (test-verbosity)
(cond [(>= (debug-level) 1) 'verbose]
[else 'normal]))
;; print-label-and-values prints out the label followed the values
;; and the expression that generated those values
;; The label is formated with the file and line number of the
;; debubing expression for easier location.
(define-syntax (print-label-and-values stx)
(syntax-case stx ()
[(_ label value ...)
(let* ([src (syntax-source stx)]
[src (if (path? src)
(find-relative-path (current-directory) src)
src)]
[lno (syntax-line stx)])
#`(begin
(printf "~a @ ~a:~a\n" label #,src #,lno)
(begin
(printf "~a:\n" 'value)
(pretty-print value)
#;(if (string? value)
(display value)
(pretty-display value))
(newline))
...
(newline)))]))
;; This series of macros are used for debuging purposes
;; and print out
(define-syntax-rule (define-debug-level name level)
(...
(define-syntax (name stx)
(syntax-case stx ()
[(_ label value ...)
#`(when (at-debug-level? level)
#,(syntax/loc stx
(print-label-and-values label value ...)))]))))
;; Print out debugging info in a somewhat organized manner
;; (debug "foo" (car '(1 2)) 'foo) should print
;; foo @ utilities.rkt:77
;; (car '(1 2)):
;; 1
;; 'foo:
;; foo
(define-debug-level trace 1)
(define-debug-level debug 2)
(define-debug-level verbose 3)
(define-debug-level copious 4)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Abstract Syntax Tree struct definitions
(define AST-output-syntax (make-parameter 'abstract-syntax))
;(define AST-output-syntax (make-parameter 'concrete-syntax))
(define (make-recur port mode)
(case mode
[(#t) write]
[(#f) display]
[else (lambda (p port) (print p port mode))]
))
(define (newline-and-indent port col)
(let ([lead (if col (make-string col #\space) "")])
(newline port)
(write-string lead port)
))
(struct Value (val) #:transparent #:property prop:custom-print-quotable 'never)
(struct Decl (name type)
#:transparent #:property prop:custom-print-quotable 'never)
(struct Poly (name type)
#:transparent #:property prop:custom-print-quotable 'never)
(struct Inst (expr type types)
#:transparent #:property prop:custom-print-quotable 'never)
(struct Var (name) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Var)
(lambda (obj) (list (Var-name obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Var x)
(write-string (symbol->string x) port)]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct Int (value) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Int)
(lambda (obj) (list (Int-value obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Int n)
(recur n port)]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct Prim (op arg*) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Prim)
(lambda (obj) (list (Prim-op obj) (Prim-arg* obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Prim op arg*)
(write-string "(" port)
(write-string (symbol->string op) port)
(for ([arg arg*])
(write-string " " port)
(recur arg port))
(write-string ")" port)
]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]))))])
(define (unsymbolic e)
(match e
[(AssignedFree x) x]
[else e]))
(struct Let (var rhs body) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Let)
(lambda (obj) (list (Let-var obj) (Let-rhs obj)
(Let-body obj))))])
(lambda ( ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Let x rhs body)
(let-values ([(line col pos) (port-next-location port)])
(write-string "(let ([" port)
(write-string (symbol->string (unsymbolic x)) port)
(write-string " " port)
(recur rhs port)
(write-string "])" port)
(newline-and-indent port col)
(write-string " " port) ;; indent body
(recur body port)
(write-string ")" port)
;(newline-and-indent port col)
)]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(define (print-info info port mode)
(let ([recur (make-recur port mode)])
(for ([(label data) (in-dict info)])
(match label
['locals-types
(write-string "locals-types:" port)
(newline port)
(cond [(dict? data)
(write-string " " port)
(for ([(var type) (in-dict data)])
(write-string (symbol->string var) port)
(write-string " : " port)
(recur type port)
(write-string ", " port)
)
(newline port)]
[else
(recur data port)
(newline port)])]
[else
(write-string (symbol->string label) port)
(newline port)
(write-string ":" port)
(newline port)
(recur data port)
(newline port)
]))))
(struct WhileLoop (cnd body) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'WhileLoop)
(lambda (obj) (list (WhileLoop-cnd obj)
(WhileLoop-body obj))))])
(lambda ( ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(WhileLoop cnd body)
(let-values ([(line col pos) (port-next-location port)])
(write-string "(while " port)
(recur cnd port)
(newline-and-indent port col)
(write-string " " port) ;; indent body
(recur body port)
(write-string ")" port)
)]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct SetBang (var rhs) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'SetBang)
(lambda (obj) (list (SetBang-var obj) (SetBang-rhs obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(SetBang var rhs)
(let-values ([(line col pos) (port-next-location port)])
(write-string "(set! " port)
(write-string (symbol->string var) port)
(newline-and-indent port col)
(write-string " " port) ;; indent body
(recur rhs port)
(write-string ")" port)
)]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct GetBang (var) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'GetBang)
(lambda (obj) (list (GetBang-var obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(GetBang var)
(let-values ([(line col pos) (port-next-location port)])
(write-string "(get! " port)
(write-string (symbol->string var) port)
(write-string ")" port)
)]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct Begin (es body) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Begin)
(lambda (obj) (list (Begin-es obj)
(Begin-body obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Begin es body)
(let-values ([(line col pos) (port-next-location port)])
(write-string "(begin " port)
(newline-and-indent port col)
(for ([e es])
(write-string " " port) ;; indent
(recur e port)
(newline-and-indent port col))
(write-string " " port) ;; indent
(recur body port)
(write-string ")" port)
)]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct Program (info body) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Program)
(lambda (obj) (list (Program-info obj) (Program-body obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Program info body)
(write-string "program:" port)
(newline port)
(print-info info port mode)
(cond [(list? body)
(for ([def body])
(recur def port)
(newline port))]
[else
(recur body port)])]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct ProgramDefsExp (info def* body) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'ProgramDefsExp)
(lambda (obj) (list (ProgramDefsExp-info obj)
(ProgramDefsExp-def* obj)
(ProgramDefsExp-body obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(ProgramDefsExp info def* body)
(write-string "functions:" port)
(newline port)
(for ([def def*]) (recur def port)(newline port))
(write-string "program:" port)
(newline port)
(recur body port)
]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct ProgramDefs (info def*) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'ProgramDefs)
(lambda (obj) (list (ProgramDefs-info obj)
(ProgramDefs-def* obj)
)))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(ProgramDefs info def*)
(write-string "functions:" port)
(newline port)
(for ([def def*])
(recur def port)
(newline port)(newline port))
]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct CProgram (info blocks)
#:transparent
#:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'CProgram)
(lambda (obj) (list (CProgram-info obj)
(CProgram-blocks obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(CProgram info blocks)
(write-string "program:" port)
(newline port)
(print-info info port mode)
(for/list ([(label tail) (in-dict blocks)])
(write-string (symbol->string label) port)
(write-string ":" port)
(newline port)
(write-string " " port)
(recur tail port)
(newline port))]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct X86Program (info blocks)
#:transparent
#:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'X86Program)
(lambda (obj) (list (X86Program-info obj)
(X86Program-blocks obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(X86Program info blocks)
(write-string "program:" port)
(newline port)
(print-info info port mode)
(for/list ([(label tail) (in-dict blocks)])
(write-string (symbol->string label) port)
(write-string ":" port)
(newline port)
(write-string " " port)
(recur tail port)
(newline port))]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct Bool (value) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Bool)
(lambda (obj) (list (Bool-value obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Bool b)
(recur b port)]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct If (cnd thn els) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'If)
(lambda (obj) (list (If-cnd obj) (If-thn obj) (If-els obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(If cnd thn els)
(let-values ([(line col pos) (port-next-location port)])
(write-string "(if" port)
(write-string " " port)
(recur cnd port)
(newline-and-indent port col)
(write-string " " port) ;; indent
(recur thn port)
(newline-and-indent port col)
(write-string " " port) ;; indent
(recur els port)
(write-string ")" port)
(newline-and-indent port col)
)]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]))))])
(struct Cast (expr source target) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Cast)
(lambda (obj) (list (Cast-expr obj) (Cast-source obj) (Cast-target obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Cast expr src tgt)
(let-values ([(line col pos) (port-next-location port)])
(write-string "(cast " port)
(recur expr port)
(write-string " " port)
(write-type src port)
(write-string " " port)
(write-type tgt port)
(write-string ")" port)
)]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]))))])
(struct IfStmt (cnd thn els) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'IfStmt)
(lambda (obj) (list (IfStmt-cnd obj) (IfStmt-thn obj)
(IfStmt-els obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(IfStmt cnd thn els)
(let-values ([(line col pos) (port-next-location port)])
(write-string "if " port)
(recur cnd port)
(newline-and-indent port col)
(write-string " " port) ;; indent
(recur thn port)
(newline-and-indent port col)
(write-string "else" port)
(newline-and-indent port col)
(write-string " " port) ;; indent
(recur els port)
)]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct Void () #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Void)
(lambda (obj) (list)))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(match ast
[(Void)
(write-string "(void)" port)])]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct Apply (fun arg*) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Apply)
(lambda (obj) (list (Apply-fun obj) (Apply-arg* obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Apply fun arg*)
(write-string "(" port)
(recur fun port)
(for ([arg arg*])
(write-string " " port)
(recur arg port))
(write-string ")" port)
]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(define (write-type ty port)
(match ty
[`(Vector ,tys ...)
(write-string "(Vector" port)
(for ([ty tys])
(write-string " " port)
(write-type ty port))
(write-string ")" port)]
[`(PVector ,tys ...)
(write-string "(PVector" port)
(for ([ty tys])
(write-string " " port)
(write-type ty port))
(write-string ")" port)]
[`(Vectorof ,ty)
(write-string "(Vectorof " port)
(write-type ty port)
(write-string ")" port)]
[`(,ts ... -> ,rt)
(write-string "(" port)
(for ([t ts])
(write-type t port)
(write-string " " port))
(write-string "-> " port)
(write-type rt port)
(write-string ")" port)]
[(? symbol?)
(write-string (symbol->string ty) port)]
))
(define (write-params param* port)
(define fst #t)
(for ([param param*])
(match param
[(? symbol?)
(write-string (symbol->string param) port)]
[`(,x : ,t)
(if fst (set! fst #f) (write-string " " port))
(write-string "[" port)
(write-string (symbol->string x) port)
(write-string " : " port)
(write-type t port)
(write-string "]" port)])))
(struct Def (name param* rty info body) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Def)
(lambda (obj) (list (Def-name obj) (Def-param* obj)
(Def-rty obj) (Def-info obj)
(Def-body obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Def name ps rty info body)
(let-values ([(line col pos) (port-next-location port)])
(write-string "(define (" port)
(write-string (symbol->string name) port)
(if (< 0 (length ps)) (write-string " " port) (void))
(write-params ps port)
(write-string ") " port)
(write-string ":" port)
(write-string " " port)
(write-type rty port)
(newline-and-indent port col)
(print-info info port mode)
(newline-and-indent port col)
(write-string " " port)
(cond [(list? body)
(for ([block body])
(cond [(pair? block)
(write-string (symbol->string (car block)) port)
(write-string ":" port)
(newline-and-indent port col)
(write-string " " port)
(recur (cdr block) port)
(newline-and-indent port col)
(write-string " " port)]
[else
(recur block port)
(newline-and-indent port col)
(write-string " " port)]))]
[else
(recur body port)])
(newline-and-indent port col)
(write-string ")" port)
)]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct StructDef (name field*) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'StructDef)
(lambda (obj) (list (StructDef-name obj) (StructDef-field* obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(StructDef name ps)
(let-values ([(line col pos) (port-next-location port)])
(write-string "(struct " port)
(write-string (symbol->string name) port)
(write-string "(" port)
(write-params ps port)
(write-string "))" port)
)]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct Lambda (param* rty body) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Lambda)
(lambda (obj) (list (Lambda-param* obj) (Lambda-rty obj)
(Lambda-body obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Lambda ps rty body)
(let-values ([(line col pos) (port-next-location port)])
(write-string "(lambda: (" port)
(write-params ps port)
(write-string ") " port)
(write-string ":" port)
(write-string " " port)
(write-type rty port)
(newline-and-indent port col)
(write-string " " port)
(recur body port)
(write-string ")" port)
(newline-and-indent port col)
)]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct Inject (value type)
#:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Inject)
(lambda (obj) (list (Inject-value obj) (Inject-type obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Inject value type)
(write-string "(inject " port)
(recur value port)
(write-string " " port)
(recur type port)
(write-string ")" port)
]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct ValueOf (value type) #:transparent #:property prop:custom-print-quotable 'never)
;;(struct TagOf (value) #:transparent #:property prop:custom-print-quotable 'never)
(struct Project (value type)
#:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Project)
(lambda (obj) (list (Project-value obj) (Project-type obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Project value type)
(write-string "(project " port)
(recur value port)
(write-string " " port)
(recur type port)
(write-string ")" port)
]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct Exit () #:transparent #:property prop:custom-print-quotable 'never)
(struct AssignedFree (var)
#:transparent #:property prop:custom-print-quotable 'never)
(struct Closure (arity fvs)
#:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Closure)
(lambda (obj) (list (Closure-arity obj)(Closure-fvs obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Closure arity fvs)
(write-string "(closure " port)
(recur arity port)
(write-string " " port)
(recur fvs port)
(write-string ")" port)
]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct FunRef (name arity) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'FunRef)
(lambda (obj) (list (FunRef-name obj)
(FunRef-arity obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(FunRef f n)
(write-string "(fun-ref" port)
(write-string " " port)
(write-string (symbol->string f) port)
(write-string " " port)
(recur n port)
(write-string ")" port)
]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]
))))])
(struct Assign (lhs rhs) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Assign)
(lambda (obj) (list (Assign-lhs obj) (Assign-rhs obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Assign lhs rhs)
(let-values ([(line col pos) (port-next-location port)])
(recur lhs port)
(write-string " " port)
(write-string "=" port)
(write-string " " port)
(recur rhs port)
(write-string ";" port)
)]))]
[(eq? (AST-output-syntax) 'abstract-syntax)
(csp ast port mode)]))))])
(struct Seq (fst snd) #:transparent #:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(let ([csp (make-constructor-style-printer
(lambda (obj) 'Seq)
(lambda (obj) (list (Seq-fst obj) (Seq-snd obj))))])
(lambda (ast port mode)
(cond [(eq? (AST-output-syntax) 'concrete-syntax)
(let ([recur (make-recur port mode)])
(match ast
[(Seq fst snd)
(let-values ([(line col pos) (port-next-location port)])
(recur fst port)
(newline-and-indent port col)