-
Notifications
You must be signed in to change notification settings - Fork 0
/
entish.rkt
205 lines (182 loc) · 7.48 KB
/
entish.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
#lang racket
(require (for-syntax syntax/keyword
racket/list
racket/match
racket/port)
racket/generator)
(provide dir file root
stdin-lines
(struct-out state)
with-new-state-frame
#%datum #%app #%top quote)
;;; SYNTAX
(define-for-syntax (build-name-stx stx . parts)
(datum->syntax stx
(string->symbol
(with-output-to-string
(lambda ()
(for ([p parts])
(display (if (syntax? p)
(syntax-e p)
p))))))))
(define-for-syntax (syntax->keyword syn)
(string->keyword (symbol->string (syntax-e syn))))
(define-syntax (defnode stx)
(define (build-name . parts)
(apply build-name-stx (cons stx parts)))
(syntax-case stx ()
([defnode (name ((k t) ... )) body ...]
(with-syntax ([fname (build-name #'name "-func")]
[children (datum->syntax stx 'children)]
[kws-pairs (foldl (lambda (cur acum)
(cons (syntax->keyword cur)
(cons (list cur #f)
acum)))
'()
(syntax->list #'(k ...)))]
[kws-table (cons 'list
(for/list ([kw-syn (syntax->list #'(k ...))]
[type-syn (syntax->list #'(t ...))])
`(list ',(syntax->keyword kw-syn)
,(build-name #'check- type-syn))))])
#`(begin
(define-syntax (name stx)
(syntax-case stx ()
([_ . rest ] (let-values ([(kw nokw) (parse-keyword-options
#'rest
kws-table)])
(define syn-kw (map cdr kw))
#`(fname #,@(flatten syn-kw) #,@nokw)))))
(define (fname #,@#'kws-pairs . children)
(lambda ()
(bind-aggregate (lambda () (call-with-values
(lambda ()
(call/cc (lambda (#,(datum->syntax stx 'return))
body ... )))
list))
'()))))))))
(define-syntax (fetch stx)
(syntax-case stx ()
([_ id] (with-syntax ([name (build-name-stx stx #'state- #'id)])
#'(name (current-state))))
([_ id def] (with-syntax ([name (build-name-stx stx #'state- #'id)])
#'(or (name (current-state)) def)))))
(define-syntax (with-new-state-frame stx)
(syntax-case stx ()
([_ ((k v) ...) body ...]
#'(parameterize ([current-state (struct-copy state (current-state) (k v) ...)])
body ...))))
(define-syntax-rule (case-mode (k exp ... ) ...)
(cond ([equal? 'k (fetch mode)] exp ... ) ... ))
(define-syntax (maybe-loop-with stx)
(syntax-case stx ()
[(maybe-loop-with body ... )
(with-syntax ([foreach (datum->syntax stx 'foreach)]
[vals (datum->syntax stx 'values)]
[return (datum->syntax stx 'return)]
[name (datum->syntax stx 'name)])
#'(if foreach
(let loop ([acum '()])
(call-with-values (lambda () (foreach))
(lambda vals
(if (not (or (eof-object? (car vals))
(void? (car vals))))
(let ([name (interpolate name vals)])
(loop
(call-with-values
(lambda ()
(call/cc (lambda (return)
body ...
'())))
(match-lambda*
['(()) acum]
[(list (? list? x)) (append x acum)]
[(? list y) (cons y acum)]))))
;else
acum))))
(begin
body ... '())))]))
;;; STATE
(define-struct state
(base-path mode)
#:transparent)
(define current-state
(make-parameter (make-state #f #f)))
;;; AUX FUNCS
(define (interpolate str vals)
(regexp-replace* #px"\\?([0-9]+)"
str
(lambda (all capture)
(format "~a"
(list-ref vals (string->number capture))))))
(define bind-aggregate
(lambda (func acum)
(let ([val (func)])
(match val
['() acum]
[(list (? list? x)) (append x acum)]
[(list (? list? x) ... ) (append x acum)]
[(list y ...) (cons y acum)]))))
(define (aggregate children)
;; Fer amb un fold.
(foldl bind-aggregate
'()
children))
;;; NODES
(defnode (file ([name expression]
[foreach expression]))
(maybe-loop-with
(let ([file-path (build-path (fetch base-path) name)])
(case-mode
[test ;; In test mode, check content matches regexp
(or (file-exists? file-path)
(return 'file-does-not-exist file-path))
(or (regexp-match (pregexp (apply string-append children))
(file->string file-path))
(return 'file-contents-mismatch file-path))]
[build
(display-to-file (apply string-append children)
file-path
#:exists 'truncate/replace)]
[remove
(when (file-exists? file-path)
(delete-file file-path))]))))
(defnode (dir ([name expression]
[foreach expression]))
(maybe-loop-with
(let ([full-path (build-path (fetch base-path) name)])
(case-mode
[test
(or (directory-exists? full-path)
(return 'directory-does-not-exist full-path))]
(return
(with-new-state-frame ([base-path full-path])
(aggregate children)))
[build
(unless (directory-exists? full-path)
(make-directory full-path))]
[remove
(with-new-state-frame ([base-path full-path])
(aggregate children))
(when (and (directory-exists? full-path)
(null? (directory-list full-path)))
(delete-directory full-path))]))))
(define (root-func #:path [path (fetch base-path ".")]
#:mode [mode (fetch mode 'test)]
. children)
(with-new-state-frame ([base-path path]
[mode mode])
(aggregate children)))
(define-syntax (root stx)
(syntax-case stx ()
([_ . rest ] (let-values ([(kw nokw) (parse-keyword-options
#'rest
(list (list '#:path check-expression)
(list '#:mode check-expression)) )])
(define syn-kw (map cdr kw))
#`(root-func #,@(flatten syn-kw) #,@nokw)))))
;;; GENERATORS
(define stdin-lines
(generator () (let loop ([line (read-line (current-input-port) 'any)])
(yield line)
(loop (read-line (current-input-port) 'any)))))