-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdiagrams.rkt
More file actions
150 lines (120 loc) · 3.05 KB
/
diagrams.rkt
File metadata and controls
150 lines (120 loc) · 3.05 KB
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
#lang racket
(provide make-heap-diagram)
(require pict)
(require pict/code)
(define pi 3.14)
(define n 40)
(define (make-imm-cell i)
(cc-superimpose
(code #,i)
(rectangle n n)))
(define (make-cons-cell)
(cb-superimpose (rectangle n n)
(code cons)))
(define (make-box-cell)
(cb-superimpose (rectangle n n)
(code box)))
(define (make-vect-cell)
(cb-superimpose (rectangle n n)
(code vect)))
(define (fwd-pts-to a b p)
(pin-arrow-line 7 p
a cc-find
b lt-find
#:start-angle (/ pi 2)
#:end-angle (- (/ pi 2))
#:start-pull 1/4
#:end-pull 1/2))
#|
(define rax (make-cons-cell))
(define m
(let ((a (make-imm-cell 1))
(b (make-cons-cell))
(c (make-imm-cell 2))
(d (make-cons-cell))
(e (make-imm-cell 3))
(f (make-imm-cell ''())))
(define pre
(foldr (λ (p1 p2)
(hc-append 0 p1 p2))
(rectangle 0 n)
(list a b c d e f)))
(define heap
(vc-append 0 (fwd-pts-to d e (fwd-pts-to b c pre))
(text "heap")))
(define all
(hc-append n (vc-append 0 rax (text "rax")) heap))
(define q
(fwd-pts-to rax heap all))
(inset q 20)))
|#
(define (make-cell v)
(match v
[`(cons ,_) (make-cons-cell)]
[`(box ,_) (make-box-cell)]
[`(vect ,_) (make-vect-cell)]
[_ (make-imm-cell v)]))
(define (add-arrows spec cells p)
;(printf "~a~n" spec)
(match spec
['() p]
[(cons `(_ ,i) s)
(add-arrows s
cells
(fwd-pts-to (list-ref cells (sub1 (- (length cells) (length s))))
(list-ref cells i)
p))]
[(cons _ s) (add-arrows s cells p)]))
(define (make-heap-diagram spec)
(match spec
[(cons (and `(,_ ,i) r) h)
(define rax (make-cell r))
(define heap (map make-cell h))
(define heap/arrows
(add-arrows (rest spec) heap
(foldr (λ (p1 p2)
(hc-append 0 p1 p2))
(rectangle 0 n)
heap)))
(define heap/arrows/label
(vc-append
0
heap/arrows
(text "heap")))
(define rax/label
(vc-append 0 rax (text "rax")))
(inset
(fwd-pts-to rax (list-ref heap i) (hc-append n rax/label heap/arrows/label))
(* n 2))]))
#;
(make-heap-diagram
'((cons 0)
1
(cons 2)
2
(cons 4)
3
'()))
#;
(make-heap-diagram
'((cons 4)
3
'()
2
(cons 0)
1
(cons 2)))
#;
(let ((a (make-imm-cell 3))
(b (make-imm-cell ''()))
(c (make-imm-cell 2))
(d (make-cons-cell))
(e (make-imm-cell 1))
(f (make-cons-cell))
(g (make-cocell ''())))
(define pre
(foldr (λ (p1 p2)
(hc-append 0 p1 p2))
(rectangle 0 n)
(list a b c d e f g)))
(inset (fwd-pts-to f g (fwd-pts-to d e (fwd-pts-to b c pre))) 20))