-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.rkt
469 lines (404 loc) · 11.5 KB
/
writer.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
#lang racket/base
#|
|#
(require (for-syntax racket/base)
racket/function
racket/list
racket/match
racket/port
racket/string
racket/unit
"attribute.rkt"
"util.rkt")
;;;
;;; generic utilities
;;;
(define-syntax on-fail
(syntax-rules ()
((_ fail-expr expr)
(with-handlers
((exn:fail?
(lambda (e) fail-expr)))
expr))))
(define (file-read file)
(call-with-input-file*
file
(lambda (in)
(port->string in))))
;; Checks whether a file either does not exist or has been changed.
(define (file-changed? file s)
;; Would there be a good way to write a function for comparing two
;; input streams? Then we could handle large files as well. ((nin
;; (open-input-string s))) and then compare to file input.
(on-fail #t (not (equal? (file-read file) s))))
;;(write-nl (file-changed? configure-script (file-read configure-script)))
(define* (write-changed-file file s)
(when (file-changed? file s)
(call-with-output-file*
file
(lambda (out)
(display s out))
#:exists 'truncate/replace)
(displayln file)))
(define (capture-output f)
(let ((output (open-output-string)))
(parameterize ((current-output-port output))
(f))
(get-output-string output)))
(define-syntax* capture
(syntax-rules ()
((_ body ...)
(capture-output (lambda () body ...)))))
(define* (space-join l)
(string-join l " "))
(define (for-each-sep elemact sepact lst)
(define first #t)
(for-each
(lambda (elem)
(if first
(set! first #f)
(when sepact (sepact)))
(when elemact (elemact elem)))
lst))
;;;
;;; local utilities
;;;
(define (disp . args)
(display (apply format args)))
(define (disp-nl . args)
(apply disp args) (newline))
;;;
;;; pretty printing
;;;
(define* (display-generated-notice pfx)
(display pfx)
(displayln " generated -- do not edit"))
(define* (write-scheme-symlink file target)
(write-changed-file
file
(capture
(display-generated-notice ";;")
(displayln "#lang racket/base")
(disp-nl "(require ~s)" target)
(disp-nl "(provide (all-from-out ~s))" target))))
(define path-censor-re #rx"[-.]")
(define* (path-h-ifdefy p)
(string-append
"__"
(string-downcase
(regexp-replace* path-censor-re (path->string (path-basename p)) "_"))
"__"
))
(define (ident-sanitize a-s)
(define s a-s)
(set! s (regexp-replace* #rx"^[-]" s "_"))
(when-let r (regexp-match #rx"^(.*)[?]$" s)
(set! s (string-append "is_" (second r))))
(set! s (regexp-replace* #rx"^[^a-zA-Z_]+" s ""))
(set! s (regexp-replace* #rx"[^a-zA-Z0-9_]+" s ""))
(when (string=? s "")
(raise-argument-error 'ident-sanitize "emittable name" a-s))
s)
(define* (name-to-c sym)
(string->symbol
(string-upcase
(ident-sanitize (symbol->string sym)))))
(define* (name-to-ruby sym)
(string->symbol
(string-append
"$"
(string-upcase
(ident-sanitize (symbol->string sym))))))
(define* (name-to-gmake sym)
(name-to-c sym))
(define* (name-to-gmake/negate sym)
(string->symbol
(string-append
"NOT__"
(symbol->string (name-to-gmake sym)))))
(define (bool-attr? attr)
(boolean? (second attr)))
(define (true-attr? attr)
(eqv? #t (second attr)))
;; Returns a list of symbols.
(define (bool-attrs-to-qmake-list attrs)
(map
(lambda (entry)
(let ((name (first entry)))
(name-to-gmake name)))
(filter true-attr? attrs)))
;; Returns a list of symbols.
(define (bool-attrs-to-qmake-list/with-negates attrs)
(map
(lambda (entry)
(let ((name (first entry))
(value (second entry)))
((if value name-to-gmake name-to-gmake/negate) name)))
(filter bool-attr? attrs)))
(define* (display/c value)
(cond
((or (attr-defined? value) (eqv? value #t))
(display "1"))
((eqv? value #f)
(display "0"))
((number? value)
(write value))
((hexnum? value)
(display (format "0x~a" (number->string (hexnum-num value) 16))))
((string? value)
(write value))
((symbol? value)
(display/c (symbol->string value)))
((list? value)
(begin
(display "{")
(for-each-sep display/c (thunk (display ", ")) value)
(display "}")))
(else
(error "cannot display as C" value))
))
(define* (display-attr/c name value)
(cond
((attr-undefined? value)
(void))
(else
(begin
(disp "#define ~a " (name-to-c name))
(display/c value)
(newline)))
))
(define* (display/ruby value)
(cond
((or (attr-defined? value) (eqv? value #t))
(display "true"))
((eqv? value #f)
(display "false"))
((number? value)
(write value))
((hexnum? value)
(disp "0x~a" (number->string (hexnum-num value) 16)))
((string? value)
(write value))
((symbol? value)
(display/ruby (symbol->string value)))
((list? value)
(begin
(display "[")
(for-each-sep display/c (thunk (display ", ")) value)
(display "]")))
(else
(error "cannot display as Ruby" value))
))
(define* (display-attr/ruby name value)
(cond
((attr-undefined? value)
(void))
(else
(begin
(display (name-to-ruby name))
(display " = ")
(display/ruby value)
(newline)))
))
(define* (display-attr/rkt name value)
(cond
[(attr-undefined? value)
(void)]
[else
(display "(define ")
(print name (current-output-port) 1)
(display " ")
(print value (current-output-port) 0)
(displayln ")")]))
(define* (display/gmake value)
(cond
((or (attr-defined? value) (eqv? value #t))
(display "true"))
((number? value)
(write value))
((string? value)
(display value))
((symbol? value)
(display/gmake (symbol->string value)))
((list? value)
(for-each-sep display/gmake (thunk (display " ")) value))
(else
(error "cannot display as GNU Make" value))
))
(define* (display-attr/gmake name value)
(set! name (name-to-gmake name))
(cond
((attr-undefined? value)
(void))
((eqv? value #t)
(begin (disp-nl "~a := true" name)
(disp-nl "NOT__~a :=" name)))
((eqv? value #f)
(begin (disp-nl "~a :=" name)
(disp-nl "NOT__~a := true" name)))
((hexnum? value)
(begin
(disp-nl "~a__DEC := ~s" name (hexnum-num value))
(disp-nl "~a__HEX := ~a"
name (number->string (hexnum-num value) 16))))
(else
(begin
(display name)
(display " := ")
(display/gmake value)
(newline)))
))
(define* (display-attr/qmake name value)
(set! name (name-to-gmake name))
(cond
((attr-undefined? value)
(void))
((eqv? value #t)
(begin (disp-nl "~a = true" name)
;;(disp-nl "NOT__~a =" name)
))
((eqv? value #f)
(begin (disp-nl "~a =" name)
;;(disp-nl "NOT__~a = true" name)
))
((hexnum? value)
(begin
(disp-nl "~a__DEC = ~s" name (hexnum-num value))
(disp-nl "~a__HEX = ~a"
name (number->string (hexnum-num value) 16))))
(else
(begin
(display name)
(display " = ")
(display/gmake value)
(newline)))
))
;; For convenience, we add all boolean variables (or their
;; negations) to the CONFIG variable with the += operator.
(define (display-qmake-bools attrs)
(display "CONFIG += ")
(for-each-sep display (thunk (display " "))
(bool-attrs-to-qmake-list attrs))
(newline))
;;;
;;; generic API
;;;
;; To define a new back end, implement the desired `lang^` as a unit,
;; and then call `make-write-include-file` to get a code generation
;; routine for the specified language.
(provide (struct-out Model) lang^)
(struct Model
(path ; path-string?
attrs ; (listof (list/c symbol? any/c))
) #:transparent)
(define-signature writer^
(write-file ; (-> Model? any/c)
))
(define-signature lang^
(line-comment-prefix ; string?
display-attr ; (-> symbol? any/c any/c)
display-preamble ; (-> Model? any/c)
display-postamble ; (-> Model? any/c)
))
(define-unit ruby-lang@
(import)
(export lang^)
(define line-comment-prefix "#")
(define display-attr display-attr/ruby)
(define display-preamble void)
(define display-postamble void))
(define-unit gmake-lang@
(import)
(export lang^)
(define line-comment-prefix "#")
(define display-attr display-attr/gmake)
(define display-preamble void)
(define display-postamble void))
(define-unit qmake-lang@
(import)
(export lang^)
(define line-comment-prefix "#")
(define display-attr display-attr/qmake)
(define display-preamble void)
(define (display-postamble m)
(display-qmake-bools (Model-attrs m))))
(define-unit rkt-lang@
(import)
(export lang^)
(define line-comment-prefix ";;")
(define display-attr display-attr/rkt)
(define (display-preamble m)
(displayln "#lang racket/base")
(displayln "(provide (all-defined-out))"))
(define display-postamble void))
(define-unit c-lang@
(import)
(export lang^)
(define line-comment-prefix "//")
(define display-attr display-attr/c)
(define (display-preamble m)
(define harness-name (path-h-ifdefy (Model-path m)))
(disp-nl "#ifndef ~a" harness-name)
(disp-nl "#define ~a" harness-name))
(define (display-postamble m)
(define harness-name (path-h-ifdefy (Model-path m)))
(disp-nl "#endif // ~a" harness-name)))
(define-unit include-file-writer@
(import lang^)
(export writer^)
(define (write-file m)
(write-changed-file
(Model-path m)
(capture
(display-file m))))
(define (display-file m)
(display-generated-notice line-comment-prefix)
(display-preamble m)
(display-attrs m)
(display-postamble m))
(define (display-attrs m)
(for ([entry (in-list (Model-attrs m))])
(match-define (list name value) entry)
(display-attr name value))))
(define* (make-write-include-file lang@)
(define-compound-unit writer@
(import)
(export (tag writer^ W))
(link
[([L : lang^]) lang@]
[([W : writer^]) include-file-writer@ L]))
(define-values/invoke-unit/infer writer@)
(lambda (pn attrs)
(write-file (Model pn attrs))))
(define* write-ruby-file
(make-write-include-file ruby-lang@))
(define* write-gmake-file
(make-write-include-file gmake-lang@))
(define* write-qmake-file
(make-write-include-file qmake-lang@))
(define* write-c-file
(make-write-include-file c-lang@))
(define* write-rkt-file
(make-write-include-file rkt-lang@))
#|
Copyright 2009 Helsinki Institute for Information Technology (HIIT)
and the authors. All rights reserved.
Authors: Tero Hasu <[email protected]>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
|#