-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode-combine.rkt
268 lines (231 loc) · 7.88 KB
/
code-combine.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
#lang racket
(require pict slideshow/fullscreen "util.rkt")
(provide
(contract-out
[ghost/p (-> (or/c pict? cp?) cp?)]
[launder/p (-> (or/c pict? cp?) cp?)]
[tt/p (-> string? cp?)]
[nt/p (-> string? cp?)]
[refocus/p (-> (or/c cp? pict?) (or/c cp? pict?) cp?)]
[pict-width/p (-> cp? real?)]
[pict-height/p (-> cp? real?)]
[get-pict (-> cp? pict?)]
[read-it (-> cp? (or/c eof-object? syntax?))]
[read-all (-> cp? (listof syntax?))]
[make-sure-contract (->* (cp?) (#:extras (listof any/c)) pict?)]
[make-sure/defn (->* (cp?) (#:extras (listof any/c)) pict?)]
[frame/p (-> cp? cp?)]
[inject (-> pict? string? cp?)]
[colorize/p (-> (or/c pict? cp?) string? cp?)]))
(provide blank/p
inset/p
cellophane/p
freeze/p)
(struct cp (string-tree pict cello))
(define (inject p str) (cp str p 1))
(define-for-syntax (add/p id)
(datum->syntax
id
(string->symbol (format "~a/p" (syntax-e id)))))
(define-syntax (do-append/p stx)
(syntax-case stx ()
[(_ x-append extra)
(with-syntax ([name (add/p #'x-append)])
#'(begin
(define name (make-append/p/proc x-append extra 'name))
(provide
(contract-out
[name superimpose-append/p/c]))))]))
(define superimpose-append/p/c
(->* ((or/c cp? pict?)) #:rest (listof (or/c cp? pict?)) cp?))
(define (make-append/p/proc x-append extra name)
(define (result arg1 . args)
(define cps (map (λ (x) (coerce-to-cp name x)) (cons arg1 args)))
(define (combine2 a b)
(match a
[(cp string-tree-a pict-a cello-a)
(match b
[(cp string-tree-b pict-b cello-b)
(cp (if (and string-tree-a string-tree-b)
(cons string-tree-a string-tree-b)
(or string-tree-a string-tree-b))
(x-append pict-a pict-b)
(max cello-a cello-b))])]))
(let loop ([cps cps])
(cond
[(null? (cdr cps)) (car cps)]
[else (combine2 (car cps) (loop (cdr cps)))])))
(procedure-rename result name))
(define (append-the-ports . ports)
(define real-ports (filter values ports))
(cond
[(null? real-ports) #f]
[else (apply input-port-append #t real-ports)]))
(do-append/p vl-append "\n")
(do-append/p vr-append "\n")
(do-append/p vc-append "\n")
(do-append/p hb-append "")
(do-append/p ht-append "")
(do-append/p hc-append "")
(do-append/p hbl-append "")
(do-append/p htl-append "")
(define (ghost/p a-cp)
(match (coerce-to-cp 'ghost/p a-cp)
[(cp string-tree pict cello)
(cp #f (ghost pict) 0)]))
(define (launder/p a-cp)
(match (coerce-to-cp 'launder/p a-cp)
[(cp string-tree pict cello)
(cp string-tree (launder pict) cello)]))
(define-syntax (do-superimpose/p stx)
(syntax-case stx ()
[(_ x-superimpose)
(with-syntax ([name (add/p #'x-superimpose)])
#'(begin
(define name (make-superimpose/p/proc x-superimpose 'name))
(provide (contract-out [name superimpose-append/p/c]))))]))
(define (make-superimpose/p/proc x-superimpose name)
(define (result . args)
(define cps (map (λ (x) (coerce-to-cp name x)) args))
(define with-string-trees
(sort (filter cp-string-tree cps)
>
#:key cp-cello))
(define with-ones (filter (λ (x) (= 1 (cp-cello x))) with-string-trees))
(cond
[((length with-ones) . >= . 2)
(error name
"expected only one argument to have content, got ~a"
(length with-ones))]
[else
(define picts (map cp-pict cps))
(define new-pict (apply x-superimpose picts))
(cond
[(null? with-string-trees) (cp #f new-pict 1)]
[else
(define fst (car with-string-trees))
(cp (cp-string-tree fst) new-pict (cp-cello fst))])]))
(procedure-rename result name))
(do-superimpose/p lt-superimpose)
(do-superimpose/p lc-superimpose)
(do-superimpose/p lb-superimpose)
(do-superimpose/p lbl-superimpose)
(do-superimpose/p ltl-superimpose)
(do-superimpose/p ct-superimpose)
(do-superimpose/p cc-superimpose)
(do-superimpose/p cb-superimpose)
(do-superimpose/p cbl-superimpose)
(do-superimpose/p ctl-superimpose)
(do-superimpose/p rt-superimpose)
(do-superimpose/p rc-superimpose)
(do-superimpose/p rb-superimpose)
(do-superimpose/p rbl-superimpose)
(do-superimpose/p rtl-superimpose)
(define (tt/p str) (cp str (tt str) 1))
(define (nt/p str) (cp str (text str '(italic . roman) (current-font-size)) 1))
(define (coerce-to-cp who p)
(cond
;; picts become literals in the ports
[(pict? p) (cp p p 1)]
[(cp? p) p]
[else (error who "expected a cp or a pict, got ~e" p)]))
(define (read-all cp)
(define t (cp-string-tree cp))
(define-values (in out) (make-pipe-with-specials))
(cond
[t
(thread
(λ ()
(let loop ([t t])
(cond
[(pair? t)
(loop (car t))
(loop (cdr t))]
[(string? t)
(write-string t out)]
[(pict? t)
(write-special t out)]
[else (error 'read-it "unknown thing: ~s" t)]))
(close-output-port out)))]
[else
(close-output-port out)])
(port-count-lines! in)
(let loop ()
(define ans (read-syntax 'cp in))
(cond
[(eof-object? ans) '()]
[else (cons ans (loop))])))
(define (read-it cp)
(define anses (read-all cp))
(cond
[(null? anses)
eof]
[(null? (cdr anses))
(car anses)]
[else
(error 'read-it "found more than one expression: ~s" anses)]))
(define (get-pict cp)
(cp-pict cp))
(define (blank/p . args)
(cp #f (apply blank args) 0))
(define (pict-width/p a-cp) (pict-width (cp-pict a-cp)))
(define (pict-height/p a-cp) (pict-height (cp-pict a-cp)))
(define (inset/p a-cp . args)
(match (coerce-to-cp 'inset/p a-cp)
[(cp string-tree pict cello)
(cp string-tree (apply inset pict args) cello)]))
(define (frame/p a-cp)
(match (coerce-to-cp 'frame/p a-cp)
[(cp string-tree pict cello)
(cp string-tree (frame pict) cello)]))
(define (cellophane/p a-cp n)
(match (coerce-to-cp 'cellophane/p a-cp)
[(cp string-tree pict cello)
(define new-n (* cello n))
(cp (if (zero? new-n) #f string-tree)
(cellophane pict n)
new-n)]))
(define (refocus/p p1 p2)
(match (coerce-to-cp 'refocus/p p1)
[(cp string-tree1 pict1 cello-1)
(match (coerce-to-cp 'refocus/p p2)
[(cp string-tree2 pict2 cello2)
(cp string-tree1 (refocus pict1 pict2) cello-1)])]))
(define (colorize/p p c)
(match (coerce-to-cp 'colorize/p p)
[(cp string-tree pict cello)
(cp string-tree (colorize pict c) cello)]))
(define (freeze/p a-cp l t r b)
(match (coerce-to-cp 'freeze/p a-cp)
[(cp string-tree pict cello)
(cp string-tree (freeze* pict l t r b) cello)]))
(define (make-sure-contract a-cp #:extras [extras '()])
(make-sure-ok a-cp 'contract? #:extras extras)
(get-pict a-cp))
(define (make-sure/defn a-cp #:extras [extras '()])
(define ns (make-pict-namespace))
(parameterize ([current-namespace ns])
(namespace-require 'racket/contract)
(for ([extra (in-list extras)])
(eval extra))
(eval (read-it a-cp)))
(get-pict a-cp))
(define (make-sure-ok a-cp ok? #:extras [extras '()])
(define ns (make-pict-namespace))
(define-values (is-contract? val)
(parameterize ([current-namespace ns])
(namespace-require 'racket/contract)
(eval '(define p-w pict-width))
(eval '(define p-h pict-height))
(for ([extra (in-list extras)])
(eval extra))
(define expr (read-it a-cp))
(with-handlers ([exn:fail?
(λ (exn)
(eprintf "error while evaluating:\n")
(pretty-print (syntax->datum expr) (current-error-port))
(raise exn))])
(eval `(let ([c ,expr])
(values (,ok? c) c))))))
(unless is-contract?
(error 'make-sure-contract "expected a contract, got ~s" val)))