-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
annotate.rkt
1408 lines (1222 loc) · 67.1 KB
/
annotate.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
#lang racket/base
(require (prefix-in kernel: syntax/kerncase)
racket/match
racket/contract
"marks.rkt"
"shared.rkt"
"shared-typed.rkt"
"syntax-hider.rkt"
"syntax-property.rkt"
"my-macros.rkt"
#;"xml-box.rkt"
(prefix-in beginner-defined: "beginner-defined.rkt")
(for-syntax racket/base))
(define-syntax (where stx)
(syntax-case stx ()
[(_ body bindings)
(syntax/loc stx (letrec bindings body))]))
; CONTRACTS
; PROVIDE
(provide/contract
[annotate
(syntax? ; syntax to annotate
((or/c continuation-mark-set? false/c)
break-kind?
(or/c list? false/c)
. -> .
any/c) ; procedure for runtime break
boolean? ; show-lambdas-as-lambdas?
. -> .
syntax?)] ; result
#;[top-level-rewrite (-> syntax? syntax?)])
; ;
; ; ; ; ;
; ; ; ; ;; ; ;; ;;; ;;;; ;;; ;;;; ;;;
; ; ; ;; ; ;; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ;;;;; ; ; ; ; ; ; ; ;;;; ; ;;;;;
; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ;;; ;; ;;;;; ;; ;;;;
; oh-say-can-you-see,by-the-dawn's-early-light,what-so-proudly-we-hailed,at-the-twilight's-last-gle
; a m i n g . W h o s e b r o a d s t r i p
; pe s a n d b r i g h t s t a r s , t hrough-the-perilous-night,o'er-the-ramparts-we-watched,were-
; s o g a l l a n t l y s t r e a m i n g .
; an d t h e r o c k e t ' s r e d g l are,the-bombs-bursting-in-air,gave-proof-through-the-night,,
; t h a t o u r f l a g w a s s t i l l t h
; er e . O h s a y , d o e s t h a t s tar-spangled-banner-yet-wave,o'er-the-land-of-the-free,and-t
; h e h o m e o f t h e b r a v e ? . . . .
; .. . . . . . . . . . . . . . . . . . ............................................................
; . . . . . . . . . . . . . . . . . . . . .
; .........you-know,-this-flag-doesn't-feel-quite-as...............................................
; . .
; ..........lighthearted-as-it-did-when-I-created-it-in-1998.......................................
; . .
; ...........And-now-it's-2022-and-it-feels-non-lighthearted-for-an-entirely-different-reason......
; . .
; .................................................................................................
; . .
; .................................................................................................
; . .
; .................................................................................................
; . .
; .................................................................................................
; . .
; .................................................................................................
;
;; given an expression to annotate, and a 'break' expression to call
;; when a breakpoint occurs, and a boolean indicating whether
;; lambdas are to be displayed as lambdas, return an annotated expression.
(define (annotate main-exp break show-lambdas-as-lambdas?)
;; check for identifiers ending in underscore numbers
;; invariant: the user's program is all wrapped in a single module.
(define max-underscore (program-max-underscore main-exp))
(define starting-index (cond [max-underscore (add1 max-underscore)]
[else 0]))
(define binding-indexer
(let ([binding-index starting-index])
(lambda ()
(let ([temp binding-index])
(set! binding-index (+ binding-index 1))
temp))))
(define (normal-break)
(break (current-continuation-marks) 'normal-break #f))
(define (result-exp-break)
(break (current-continuation-marks) 'result-exp-break #f))
(define (result-value-break vals-list)
(break (current-continuation-marks) 'result-value-break vals-list))
(define (normal-break/values vals-list)
(break (current-continuation-marks) 'normal-break/values vals-list))
(define (exp-finished-break info-list)
(break #f 'expr-finished-break info-list))
(define (double-break)
(break (current-continuation-marks) 'double-break #f))
(define ((make-opaque-exp-break exp))
(exp-finished-break
(list (list (lambda () exp)
#f
(lambda () (error 'make-define-struct-break
"no getter for a define-struct"))))))
; wcm-pre-break-wrap : call wcm-wrap with a result-exp-break on the expr
(define (wcm-result-exp-break-wrap debug-info exp)
(wcm-wrap debug-info (result-exp-break-wrap exp)))
;; wrap a pre-break around stx
(define (result-exp-break-wrap stx)
#`(begin (#%plain-app #,result-exp-break) #,stx))
;; wrap a normal break around stx
(define (break-wrap exp)
#`(begin (#%plain-app #,normal-break) #,exp));
;; wrap a double-break around exp
(define (double-break-wrap exp)
#`(begin (#%plain-app #,double-break) #,exp))
;; abstraction used in the next two defs
(define (return-value-wrap-maker break-proc)
(lambda (exp)
#`(#%plain-app
call-with-values
(#%plain-lambda () #,exp)
(#%plain-lambda args
(#%plain-app #,break-proc args)
(#%plain-app #,apply values args)))))
;; wrap a return-value-break around exp
(define return-value-wrap
(return-value-wrap-maker result-value-break))
;; wrap a normal-break/values around exp
(define normal-break/values-wrap
(return-value-wrap-maker normal-break/values))
(define (module-top-level-annotate/inner exp source-exp defined-name)
(match-let*
([(vector annotated dont-care)
(annotate/inner exp 'all #f defined-name)])
#`(with-continuation-mark #,debug-key
#,(make-top-level-mark source-exp)
;; inserting eta-expansion to prevent destruction of top-level mark
(#%plain-app
call-with-values (#%plain-lambda () #,annotated)
(#%plain-lambda args (#%plain-app #,apply values args))))))
; annotate/inner takes
; a) an expression to annotate
; b) a list of all bindings which this expression is tail w.r.t.
; or 'all to indicate that this expression is tail w.r.t. _all_ bindings.
; d) a boolean indicating whether this expression will be the r.h.s. of a reduction
; (and therefore should be broken before)
; g) information about the binding name of the given expression. This is used
; to associate a name with a closure mark (though this may now be redundant)
; it returns (as a 2vals)
; a) an annotated s-expression
; b) a list of varrefs for the variables which occur free in the expression
;
;(syntax-object BINDING-SET bool bool (or/c #f symbol (list binding symbol)) ->
; sexp (list-of z:varref))
; ; ;
; ; ; ;
; ;;; ; ;; ; ;; ;;; ;;;; ;;; ;;;; ;;; ; ; ; ;; ; ;; ;;; ; ;;
; ; ; ;; ; ;; ; ; ; ; ; ; ; ; ; ; ; ;; ; ;; ; ; ; ;;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ;;;; ; ; ; ; ; ; ; ;;;; ; ;;;;; ; ; ; ; ; ; ;;;;; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ;;;;; ; ; ; ; ;;; ;; ;;;;; ;; ;;;; ; ; ; ; ; ; ;;;; ;
; ;
;
(define annotate/inner
#;(syntax? binding-set? boolean? (or/c false/c syntax? (list/p syntax? syntax?)) (or/c false/c integer?)
. -> . (vector/p syntax? binding-set?))
(lambda (exp tail-bound pre-break? procedure-name-info)
;; annotate an exp with a stepper/skipto or stepper-skipto/discard
;; label. If force-discard? is true, then we use a discarding
;; reconstruction regardless of the tag
(define (dont-annotate)
;; mutable, to catch free vars. Mutated several times, we
;; only care about the last. A bit yecchy.
(define free-vars-captured #f)
(define (subterm-recur subterm)
(match-let*
([(vector stx free-vars)
(annotate/inner subterm tail-bound pre-break?
procedure-name-info)])
(set! free-vars-captured free-vars)
stx))
(define annotated (skipto/auto exp #f subterm-recur))
(vector (wcm-wrap skipto-mark annotated) free-vars-captured))
;; recurrence procedures, used to recur on sub-expressions:
(define (tail-recur exp) (annotate/inner exp tail-bound
#t procedure-name-info))
(define (non-tail-recur exp) (annotate/inner exp null #f #f))
(define (result-recur exp) (annotate/inner exp null
#f procedure-name-info))
(define (set!-rhs-recur exp name) (annotate/inner exp null #f name))
(define (let-rhs-recur exp binding-names dyn-index-syms)
(define proc-name-info
(if (not (null? binding-names))
(list (car binding-names) (car dyn-index-syms))
#f))
(annotate/inner exp null #f proc-name-info))
(define (lambda-body-recur exp) (annotate/inner exp 'all #t #f))
; let bodies have a startling number of recurrence patterns. ouch!
;; ... looks like these can maybe be collapsed with a simpler desired reduction sequence
;; (a.k.a. not safe-for-space).
;; no pre-break, tail w.r.t. new bindings:
(define (let-body-recur/single exp bindings)
(annotate/inner exp (binding-set-union (list tail-bound bindings))
#f procedure-name-info))
;; different flavors of make-debug-info allow users to provide only the needed fields:
(define (make-debug-info/normal free-bindings)
(make-debug-info exp tail-bound free-bindings 'none #t))
(define (make-debug-info/app tail-bound free-bindings label)
(make-debug-info exp tail-bound free-bindings label #t))
(define (make-debug-info/let free-bindings binding-list let-counter)
(make-debug-info
exp
(binding-set-union (list tail-bound
binding-list
(list let-counter)))
(varref-set-union (list free-bindings
binding-list
(list let-counter))) ; NB using bindings as varrefs
'let-body
#t))
(define (make-debug-info/fake-exp exp free-bindings)
(make-debug-info (stepper-syntax-property exp 'stepper-fake-exp #t)
tail-bound free-bindings 'none #t))
(define (make-debug-info/fake-exp/tail-bound exp tail-bound free-bindings)
(make-debug-info (stepper-syntax-property exp 'stepper-fake-exp #t)
tail-bound free-bindings 'none #t))
(define outer-wcm-wrap (if pre-break?
wcm-result-exp-break-wrap
wcm-wrap))
(define (wcm-break-wrap debug-info exp)
(outer-wcm-wrap debug-info (break-wrap exp)))
;; used for things that are values:
(define (normal-bundle free-vars annotated)
(vector (outer-wcm-wrap (make-debug-info/normal free-vars)
annotated)
free-vars))
; @@ @@ @@
; @ @ @
; @ $@$: @@+-$: @-@$ $@:@ $@$:
; @ -@ @+@$@ @+ *$ $* *@ -@
; @ -$@$@ @ @ @ @ @ @ @ -$@$@
; @ $* @ @ @ @ @ @ @ @ $* @
; @ @- *@ @ @ @ @ +$ $* *@ @- *@
; @@@@@ -$$-@@@@@@@@@@@+@$ $@:@@ -$$-@@
;
(define (lambda-clause-abstraction clause)
(with-syntax ([(args-stx . bodies) clause])
(match-let*
([(vector annotated-body free-varrefs)
; wrap bodies in explicit begin if more than 1
; user-introduced (non-skipped) bodies
; NB: generally can't happen in beginner up through int/lambda
(lambda-body-recur (syntax (begin . bodies)))]
[new-free-varrefs
(varref-set-remove-bindings
free-varrefs
(arglist-flatten #'args-stx))])
(vector (datum->syntax
#'here
`(,#'args-stx ,annotated-body) #'clause)
new-free-varrefs))))
(define (outer-lambda-abstraction annotated-lambda free-varrefs)
(let*
([closure-info (make-debug-info/app 'all free-varrefs 'none)]
;; if we manually disable the storage of names,
;; lambdas get rendered as lambdas.
;; Yikes, this seems like a pretty gross hack... JBC 2010-12
[closure-name
(if show-lambdas-as-lambdas?
#f
(cond [(syntax? procedure-name-info) procedure-name-info]
[(pair? procedure-name-info) (car procedure-name-info)]
[else #f]))]
[closure-storing-proc
(lambda (clo debug-info maybe-index)
(annotated-proc
clo
(Closure-Record
;; FIXME WHEN TR HAS SYNTAX CHAPERONES
(if closure-name (sstx closure-name) #f)
;; FIXME WHEN MARKS ENTER THE TYPED SIDE
(smrk debug-info)
;; FIXME WHEN TR HAS SYNTAX CHAPERONES
(if maybe-index (sstx maybe-index) #f))))]
[captured
(cond [(pair? procedure-name-info)
#`(#%plain-app
#,closure-storing-proc
#,annotated-lambda
#,closure-info
#,(cadr procedure-name-info))]
[else
#`(#%plain-app
#,closure-storing-proc
#,annotated-lambda
#,closure-info
#f)])]
;; gnarr! I can't find a test case
;; that depends on the attachment of the inferred name...
[inferred-name-struct
(if closure-name
(syntax-property
captured
'inferred-name
(syntax-e closure-name))
captured)])
(normal-bundle free-varrefs inferred-name-struct)))
; @@
; @ @
; @ -@@$ @@@@@
; @ $ -$ @
; @ @@@@@ @
; @ $ @
; @ +: @: :$
; @@@@@ $@@+ :@@$-
; The let transformation is complicated.
; here's a sample transformation (not including 'break's):
;(let-values ([(a b c) e1] [(d e) e2]) e3)
;
;turns into
;
;(let ([counter (<dynamic-counter-call>)])
;(let-values ([(a b c d e lifter-a-1 lifter-b-2 lifter-c-3 lifter-d-4 lifter-e-5 let-counter)
; (values *unevaluated* *unevaluated* *unevaluated* *unevaluated* *unevaluated*
; counter counter counter counter counter 0)])
; (with-continuation-mark
; key huge-value
; (begin
; (set!-values (a b c) e1)
; (set! let-counter 1)
; (set!-values (d e) e2)
; (set! let-counter 2)
; e3))))
;
; note that this elaboration looks exactly like the one for letrec, and that's
; okay, because expand guarantees that reordering them will not cause capture.
; this is because a bound variable answers is considered bound by a binding only when
; the pair answers true to bound-identifier=?, which is determined during (the first)
; expand.
; another irritating point: the mark and the break that must go immediately
; around the body. Irritating because they will be instantly replaced by
; the mark and the break produced by the annotated body itself. However,
; they're necessary, because the body may not contain free references to
; all of the variables defined in the let, and thus their values are not
; known otherwise.
; whoops! hold the phone. I think I can get away with a break before, and
; a mark after, so only one of each. groovy, eh?
; 2005-08: note that the set!-based approach on the let-counter is broken in the presence of
; continuations; backing up a computation using a set! will not revert the
; counter, and the stepper may think that the computation is in a different
; place. To fix this, we must go to a pure let* with nested marks at each right-hand-side.
; 2006-01: oh dear heaven. Begin expands into a let-values. This means that the
; let-values really has most of the complexity of the whole stepper, all in one
; place. Re-formulating the bodies as a begin and re-calling annotate/inner broke
; implied invariants (in particular, that annotate/inner was only called on subexprs)
; and confused the heck out of me for some time today. Bleah. I'm just going to
; do the whole expansion here. Also, I'm going to make this expansion call/cc-clean,
; because I think it'll actually be easier to state & read this way.
; 2006-11: appears to work now. I'm about to try to transfer this new idiom to begin0;
; wish me luck.
(define (let-abstraction stx output-identifier)
(with-syntax ([(_ ([(var ...) val] ...) . bodies) stx])
(match-let*
([binding-sets (map syntax->list (syntax->list #'((var ...) ...)))]
[binding-list (apply append binding-sets)]
[vals (syntax->list #'(val ...))]
[lifted-var-sets (map (lx (map get-lifted-var _)) binding-sets)]
[lifted-vars (apply append lifted-var-sets)]
[(vector annotated-vals free-varref-sets-vals)
(2vals-map let-rhs-recur vals binding-sets lifted-var-sets)]
[bodies-list (syntax->list #'bodies)]
[(vector annotated-body free-varrefs-body)
(if (= (length bodies-list) 1)
(let-body-recur/single (car bodies-list) binding-list)
;; oh dear Lord, we have to unfold these like an application:
(let unroll-loop ([bodies-list bodies-list] [outermost? #t])
(cond [(null? bodies-list)
(error 'annotate "no bodies in let")]
[(null? (cdr bodies-list))
(tail-recur (car bodies-list))]
[else
(match-let*
([(vector rest free-vars-rest)
(unroll-loop (cdr bodies-list) #f)]
[(vector this-one free-vars-this)
(non-tail-recur (car bodies-list))]
[free-vars-all
(varref-set-union (list free-vars-rest
free-vars-this))]
[debug-info (make-debug-info/fake-exp
#`(begin #,@bodies-list)
free-vars-all)]
[begin-form
#`(begin
#,(normal-break/values-wrap this-one)
#,rest)])
(vector (if outermost?
(wcm-wrap debug-info begin-form)
(wcm-result-exp-break-wrap debug-info
begin-form))
free-vars-all))])))])
((vector (quasisyntax/loc
exp
(let ([#,counter-id (#,binding-indexer)])
(#,output-identifier #,outer-initialization #,wrapped-begin)))
free-varrefs)
. where .
([free-varrefs (varref-set-remove-bindings
(varref-set-union (cons free-varrefs-body
free-varref-sets-vals))
binding-list)]
[counter-id #`lifting-counter]
[unevaluated-list (map (lambda (b) *unevaluated*) binding-list)]
[outer-initialization
#`([(#,@lifted-vars #,@binding-list #,let-counter)
(values #,@(append (map (lambda (dc_binding) counter-id)
binding-list)
unevaluated-list
(list 0)))])]
[counter-clauses (build-list
(length binding-sets)
(lambda (num)
#`(set! #,let-counter #,(+ num 1))))]
[set!-clauses
(map (lambda (binding-set val)
#`(set!-values #,binding-set #,val))
binding-sets
annotated-vals)]
[exp-finished-clauses
(with-syntax ([(_ let-clauses . dc) stx]
[((lifted-var ...) ...) lifted-var-sets])
(with-syntax ([(exp-thunk ...) (map (lx (lambda () _))
(syntax->list #`let-clauses))])
#`(#%plain-app
list
(#%plain-app
list exp-thunk
(#%plain-app
list lifted-var ...)
(#%plain-lambda () (#%plain-app list var ...))) ...)))]
; time to work from the inside out again
; without renaming, this would all be much much simpler.
[wrapped-begin (outer-wcm-wrap (make-debug-info/let free-varrefs
binding-list
let-counter)
(double-break-wrap
#`(begin #,@(apply append (zip set!-clauses counter-clauses))
(#%plain-app #,exp-finished-break #,exp-finished-clauses)
#,annotated-body)))])))))
; @ :@@$
; @:
; -@@ @@@@@
; @ @
; @ @
; @ @
; @ @
; @@@@@ @@@@@
; if-abstraction: (-> syntax? syntax? (or/c false/c syntax?) (values syntax? varref-set?))
(define (if-abstraction test then else)
(match-let*
([(vector annotated-test free-varrefs-test)
(non-tail-recur test)]
[test-with-break
(normal-break/values-wrap annotated-test)]
[(vector annotated-then free-varrefs-then)
(tail-recur then)]
[(vector annotated-else free-varrefs-else)
(if else
(tail-recur else)
(vector #f null))]
[free-varrefs (varref-set-union (list free-varrefs-test
free-varrefs-then
free-varrefs-else))]
[annotated-if
(if else
(quasisyntax/loc exp
(if #,test-with-break #,annotated-then #,annotated-else))
(quasisyntax/loc exp
(if #,test-with-break #,annotated-then)))])
(vector
(outer-wcm-wrap (make-debug-info/normal free-varrefs) annotated-if)
free-varrefs)))
;
;
; ;;;
; ;
; ;
; ; ; ;;;; ; ;;; ; ;;; ;;; ;;;;;;
; ; ; ; ; ;; ; ;; ; ; ; ;
; ; ; ; ; ; ; ;;;;; ;
; ; ; ; ; ; ; ; ;
; ; ; ; ;; ; ; ; ;
; ; ;; ; ; ; ;;;; ;
;
;
;
(define (varref-abstraction var)
(match-let*
([free-varrefs (list var)]
[varref-break-wrap
(lambda ()
(wcm-break-wrap (make-debug-info/normal free-varrefs)
(return-value-wrap var)))]
[varref-no-break-wrap
(lambda ()
(outer-wcm-wrap (make-debug-info/normal free-varrefs) var))]
;; JBC: shouldn't this be the namespace of the user's code... ?
[base-namespace-symbols (namespace-mapped-symbols (make-base-namespace))]
[module-bound-varref-break-wrap
(lambda ()
(varref-break-wrap)
#;(if (or (memq (syntax-e var) beginner-defined:must-reduce)
(and (stepper-syntax-property var 'lazy-op)
(not (memq (syntax->datum var) base-namespace-symbols))))
(varref-break-wrap)
(varref-no-break-wrap)))])
(vector
(match (stepper-syntax-property var 'stepper-binding-type)
[(or 'lambda-bound 'macro-bound) (varref-no-break-wrap)]
['let-bound (varref-break-wrap)]
['non-lexical ;; is it from this module or not?
(match (identifier-binding var)
;; this can only come up when stepping through non-module code...
;; perhaps we should just signal an error here.
(#f (varref-break-wrap))
['lexical
;; my reading of the docs suggest that this should not occur in v4...
(error 'varref-abstraction
"identifier-binding should not be 'lexical")]
[(list-rest (? module-path-index? path-index) dontcare)
(let-values ([(module-path dc5)
(module-path-index-split path-index)])
(if module-path
;; not a module-local variable:
(module-bound-varref-break-wrap)
;; a module-local-variable:
(varref-break-wrap)))]
[other (error
'annotate
"unexpected value for identifier-binding: ~v" other)])]
[other
(error 'annotate
"unexpected value for stepper-binding-type on variable ~e: ~e"
(syntax->datum var)
other)])
free-varrefs)))
(define (recertifier vals)
(match-let* ([(vector new-exp bindings) vals])
(vector new-exp
(map (lambda (b) b)
bindings))))
;; this is a terrible hack... until some other language form needs it.
;; It wraps the given annotated expression with a break that adds the
;; result to the list of completed expressions
(define maybe-final-val-wrap
(match-lambda
[(vector annotated free-vars)
(vector (if (stepper-syntax-property exp 'stepper-use-val-as-final)
#`(#%plain-app
call-with-values
(#%plain-lambda () #,annotated)
(#%plain-lambda
results
(#,exp-finished-break
(#%plain-app list
(#%plain-app
list
#,(lambda () exp)
#f
(#%plain-lambda () results))))
(#%plain-app apply values results)))
annotated)
free-vars)]
[error 'maybe-final-val-wrap "stepper internal error 20080527"]))
(cond [(or (stepper-syntax-property exp 'stepper-skipto)
(stepper-syntax-property exp 'stepper-skipto/discard))
(dont-annotate)]
[(stepper-syntax-property exp 'stepper-skip-completely)
(vector (wcm-wrap "supposed to be skipped" exp) null)]
[(stepper-syntax-property exp 'no-further-annotation)
(vector (wcm-break-wrap (make-debug-info/normal '()) exp) null)]
[else
(let ([exp (disarm exp)])
(recertifier
(maybe-final-val-wrap
(kernel:kernel-syntax-case
exp #f
[(#%plain-lambda . clause)
(match-let*
([(vector annotated-clause free-varrefs)
(lambda-clause-abstraction (syntax clause))]
[annotated-lambda
(with-syntax ([annotated-clause annotated-clause])
(syntax/loc exp (#%plain-lambda . annotated-clause)))])
(outer-lambda-abstraction annotated-lambda free-varrefs))]
[(case-lambda . clauses)
(match-let*
([(vector annotated-cases free-varrefs-cases)
(2vals-map lambda-clause-abstraction (syntax->list (syntax clauses)))]
[annotated-case-lambda (with-syntax ([annotated-cases annotated-cases])
(syntax/loc exp (case-lambda . annotated-cases)))]
[free-varrefs (varref-set-union free-varrefs-cases)])
(outer-lambda-abstraction annotated-case-lambda free-varrefs))]
[(if test then else) (if-abstraction (syntax test) (syntax then) (syntax else))]
;
;
; ; ;
; ;
; ;
; ; ;; ;;; ;;;; ;;; ; ;;
; ;; ; ; ; ; ; ; ;; ;
; ; ; ;;;;; ; ; ; ; ;
; ; ; ; ; ; ; ; ;
; ; ; ; ; ;; ; ; ;
; ;;;; ;;;; ;; ; ;;; ; ;
; ;
; ;;;;
;
[(begin . bodies)
;; begin is typically introduced by the use of a template, though
;; generally indirectly.
;; it may also happen in other ways as the body of a lambda.
(let ([non-skipped-bodies
(filter
(lambda (clause)
(not (to-be-skipped? clause)))
(syntax->list (syntax bodies)))])
(if (> (length non-skipped-bodies) 1)
(error 'annotate-inner "unexpected begin : ~v" (syntax->datum exp))
(match-let*
([(vector annotated-bodies free-var-sets)
(2vals-map tail-recur
(syntax->list #`bodies))])
(vector #`(begin . #,annotated-bodies)
(varref-set-union free-var-sets)))))]
;
;
; ; ; ;;
; ; ; ;
; ; ; ;;
; ; ;; ;;; ;;;; ;;; ; ;; ; ; ;
; ;; ; ; ; ; ; ; ;; ; ; ; ;
; ; ; ;;;;; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ;; ;
; ; ; ; ; ;; ; ; ; ;; ;
; ;;;; ;;;; ;; ; ;;; ; ; ;;
; ;
; ;;;;
;
;; one-element begin0 is a special case, because in this case only
;; the body of the begin0 is in tail position.
[(begin0 body)
(match-let* ([(vector annotated-body free-vars-body)
(tail-recur #'body)])
(vector (wcm-break-wrap (make-debug-info/normal free-vars-body)
(quasisyntax/loc exp (begin0 #,annotated-body)))
free-vars-body))]
[(begin0 first-body . bodies-stx)
(match-let*
([(vector annotated-first free-vars-first) (result-recur #'first-body)]
[(vector annotated-rest free-vars-rest) (2vals-map non-tail-recur (syntax->list #`bodies-stx))]
[wrapped-rest (map normal-break/values-wrap annotated-rest)]
[all-free-vars (varref-set-union (cons free-vars-first free-vars-rest))]
[early-debug-info (make-debug-info/normal all-free-vars)]
[tagged-temp (stepper-syntax-property begin0-temp 'stepper-binding-type 'stepper-temp)]
[debug-info-maker
(lambda (rest-exps)
(make-debug-info/fake-exp/tail-bound
#`(begin0 #,@rest-exps)
(binding-set-union (list (list tagged-temp) tail-bound))
(varref-set-union (list (list tagged-temp) all-free-vars))))]
[rolled-into-fakes
(let loop ([remaining-wrapped wrapped-rest]
[remaining-src (syntax->list #`bodies-stx)]
[first-time? #t])
((if first-time? wcm-wrap wcm-result-exp-break-wrap)
(debug-info-maker remaining-src)
(cond [(null? remaining-src) begin0-temp]
[else #`(begin #,(car remaining-wrapped) #,(loop (cdr remaining-wrapped)
(cdr remaining-src)
#f))])))])
(vector (wcm-wrap early-debug-info
#`(let ([#,begin0-temp #,annotated-first])
#,rolled-into-fakes))
all-free-vars))]
[(let-values . _) (let-abstraction exp #`let-values)]
[(letrec-values . _) (let-abstraction exp #`letrec-values)]
; $
; @ @
; :@@+@ -@@$ @@@@@ @
; @$ -@ $ -$ @ @
; :@@$- @@@@@ @ @
; *@ $ @
; @ :@ +: @: :$
; $+@@: $@@+ :@@$- $
[(set! var val)
(match-let*
([(vector annotated-val val-free-varrefs)
(set!-rhs-recur (syntax val)
(syntax-case (syntax var) (#%top)
[(#%top . real-var) (syntax-e (syntax real-var))]
[else (syntax var)]))]
[free-varrefs (varref-set-union (list val-free-varrefs (list #`var)))]
[annotated-set!
(return-value-wrap
(quasisyntax/loc exp (set! var #,(normal-break/values-wrap annotated-val))))])
(vector
(outer-wcm-wrap (make-debug-info/normal free-varrefs) annotated-set!)
free-varrefs))]
; @
; $@-@@@@ @@ $@$ @@@@@ -@@$
; $- :@ @ @ $- -$ @ $ -$
; @ @ @ @ @ @ @ @@@@@
; @ @ @ @ @ @ @ $
; $- :@ @: +@ $- -$ @: :$ +:
; $@-@ :@$-@@ $@$ :@@$- $@@+
; @
; @@@
[(quote _)
(normal-bundle null exp)]
[(quote-syntax _)
(normal-bundle null exp)]
;; we're going to try to handle (#%variable-reference) as an opaque datum:
[(#%variable-reference . _)
(normal-bundle null exp)]
; @@@ @@@ $@+@ @@+-$:
; @ @ $+ -@ @+@$@
; $-@ @ @@@@@ @ @@@@@ @ @ @
; ++@+$ @ @ @ @
; :@@$+ $* -$ @ @ @
; -@$@* $@$- @@@@@@@
[(with-continuation-mark key mark body)
;(match-let* ([(annotated-key free-varrefs-key)
; (non-tail-recur (syntax key))]
; [(annotated-mark free-varrefs-mark)
; (non-tail-recur (syntax mark))]
; [(annotated-body dc_free-varrefs-body)
; (result-recur (syntax body))])
(error 'annotate/inner "this region of code is still under construction")
; [annotated #`(let-values ([key-temp #,*unevaluated*]
; [mark-temp #,*unevaluated*]
;)
]
; @@ @ @
; @ @
; $@$: @@:@$- @@:@$- @ -@@ $@+@ $@$: @@@@@ -@@ $@$ @@:@@:
; -@ @: -$ @: -$ @ @ $+ -@ -@ @ @ $- -$ @+ :@
; -$@$@ @ @ @ @ @ @ @ -$@$@ @ @ @ @ @ @
; $* @ @ @ @ @ @ @ @ $* @ @ @ @ @ @ @
; @- *@ @: -$ @: -$ @ @ $* -$ @- *@ @: :$ @ $- -$ @ @
; -$$-@@ @-@$ @-@$ @@@@@ @@@@@ $@$- -$$-@@ :@@$- @@@@@ $@$ @@@ @@@
; @ @
; @@@ @@@
; [foot-wrap?
; (wcm-wrap debug-info annotated)])
; free-bindings))]
; the app form's elaboration looks like this, where M0 etc. stand for expressions, and t0 etc
; are temp identifiers that do not occur in the program:
; (M0 ...)
;
; goes to
;
;(let ([t0 *unevaluated*]
; ...)
; (with-continuation-mark
; debug-key
; huge-value
; (set! t0 M0)
; ...
; (with-continuation-mark
; debug-key
; much-smaller-value
; (t0 ...))))
;
; 'break's are not illustrated. An optimization is possible when all expressions M0 ... are
; varrefs. In particular (where v0 ... are varrefs):
; (v0 ...)
;
; goes to
;
; (with-continuation-mark
; debug-key
; debug-value
; (v0 ...))
;
; in other words, no real elaboration occurs. Note that this doesn't work as-is for the
; stepper, because there's nowhere to hang the breakpoint; you want to see the break
; occur after all vars have been evaluated. I suppose you could do (wcm ... (begin v0 ... (v0 ...)))
; where the second set are not annotated ... but stepper runtime is not at a premium.
;; the call/cc-safe version of this appears to work, and it lives in the definition of let. I should
;; transfer that knowledge to here. -- JBC, 2006-10-11
[(#%plain-app . terms)
(match-let*
([(vector annotated-terms free-varrefs-terms)
(2vals-map non-tail-recur (syntax->list (syntax terms)))]
[free-varrefs (varref-set-union free-varrefs-terms)])
(vector
(let* ([arg-temps (build-list (length annotated-terms) get-arg-var)]
[tagged-arg-temps (map (lambda (var) (stepper-syntax-property var 'stepper-binding-type 'stepper-temp))
arg-temps)]
[let-clauses #`((#,tagged-arg-temps
(#%plain-app values #,@(map (lambda (_) *unevaluated*) tagged-arg-temps))))]
[set!-list (map (lambda (arg-symbol annotated-sub-exp)
#`(set! #,arg-symbol #,annotated-sub-exp))
tagged-arg-temps annotated-terms)]
[new-tail-bound (binding-set-union (list tail-bound tagged-arg-temps))]
[app-debug-info (make-debug-info/app new-tail-bound tagged-arg-temps 'called)]
[app-term (quasisyntax/loc exp (#%plain-app #,@tagged-arg-temps))]
[debug-info (make-debug-info/app new-tail-bound
(varref-set-union (list free-varrefs tagged-arg-temps)) ; NB using bindings as vars
'not-yet-called)]
[let-body (outer-wcm-wrap debug-info #`(begin #,@set!-list
#,(break-wrap
(wcm-wrap
app-debug-info
#`(if (#%plain-app #,annotated-proc? #,(car tagged-arg-temps))
#,app-term
#,(return-value-wrap app-term))))))])
#`(let-values #,let-clauses #,let-body))
;)
free-varrefs))]
; @@
; @ @
; $@:@ $@$: @@@@@ @@ @@ @@+-$:
; $* *@ -@ @ @ @ @+@$@
; @ @ -$@$@ @ @ @ @ @ @
; @ @ $* @ @ @ @ @ @ @
; $* *@ @- *@ @: :$ @: +@ @ @ @
; $@:@@ -$$-@@ :@@$- :@$-@@@@@@@@@
[(#%top . var-stx) (varref-abstraction #`var-stx)]
[var-stx
(identifier? #`var-stx)
(varref-abstraction #`var-stx)]
[else
(error 'annotate "unexpected syntax for expression: ~v" (syntax->datum exp))]))))])))
;; annotate/top-level : syntax-> syntax
;; expansion of teaching level language programs produces two kinds of
;; expressions: modules containing all of the code in the def'ns window, and
;; require statements that invoke those modules. In the first case, we must annotate
;; the expressions inside the top-level module, and in the second, we should just
;; leave it alone.
(define/contract annotate/top-level
(syntax? . -> . syntax?)
(lambda (exp)
(syntax-case exp (module #%plain-module-begin let-values dynamic-wind #%plain-lambda #%plain-app define-values)
[(module name lang
(#%plain-module-begin . bodies))
#`(module name lang (#%plain-module-begin #,@(map annotate/module-top-level (syntax->list #`bodies))))]
; the 'require' form is used for the test harness
[(require module-name) exp]
; the 'dynamic-require' form is used by the actual expander
[(let-values ([(done-already?) . rest1])
(#%plain-app dynamic-wind