forked from sctb/lumen
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmacros.l
289 lines (233 loc) · 7.9 KB
/
macros.l
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
(define-macro %brackets body
`(%array ,@body))
(define-macro %braces body
`(%object ,@body))
(define-macro do body
`(%do ,@body))
(define-macro quote (form)
(quoted form))
(define-macro quasiquote (form)
(quasiexpand form 1))
(define-macro while (test rest: body)
`(%while ,test (%do ,@body) ,@(props body)))
(define-global get-place (place setfn)
(let place (macroexpand place)
(if (or (atom? place)
(and (= (hd place) 'get)
(nil? (getenv 'get 'place-expander)))
(accessor-literal? (hd (tl place))))
(setfn place (fn (v) `(%set ,place ,v)))
(let (head (hd place)
gf (getenv head 'place-expander))
(if gf (apply gf setfn (tl place))
(error (cat (str place) " is not a valid place expression")))))))
(define-macro let-place (vars place rest: body)
`(get-place ,place (fn ,vars ,@body)))
(define-macro define-expander (name handler)
(with form `(setenv ',name place-expander: ,handler)
(eval form)))
(define-global define-setter (name setter setfn args vars)
(if (none? args)
(let vars (reverse (or vars ()))
(setfn `(,name ,@vars) (fn (v) (apply setter v vars))))
(let v (hd args)
(%call define-setter name setter setfn (tl args) `(,v ,@vars)))))
(define-macro define-setter (name arglist rest: body)
`(define-expander ,name
(fn (setfn rest: args)
(%call define-setter ',name (fn ,arglist ,@body) setfn args))))
(define-macro set args
`(%do ,@(map (fn ((lh rh))
(let-place (getter setter) lh
(setter rh)))
(pair args))))
(define-macro at (l i)
(if (and (= target* 'lua) (number? i))
(inc i)
(= target* 'lua)
(set i `(+ ,i 1)))
`(get ,l ,i))
(define-macro wipe (place)
(if (= target* 'lua)
`(set ,place nil)
`(%delete ,place)))
(define-macro list body
(let-unique (x)
(let (l () forms ())
(each (k v) body
(if (number? k)
(set (get l k) v)
(add forms `(set (get ,x ',k) ,v))))
(if (some? forms)
`(let ,x (%array ,@l) ,@forms ,x)
`(%array ,@l)))))
(define-macro if branches
(hd (expand-if branches)))
(define-macro case (expr rest: clauses)
(let-unique (x)
(let (eq (fn (_) `(= ',_ ,x))
cl (fn ((a b))
(if (nil? b) (list a)
(or (string? a) (number? a)) (list (eq a) b)
(one? a) (list (eq (hd a)) b)
(> (# a) 1) (list `(or ,@(map eq a)) b))))
`(let ,x ,expr
(if ,@(apply join (map cl (pair clauses))))))))
(define-macro when (cond rest: body)
`(if ,cond (%do ,@body)))
(define-macro unless (cond rest: body)
`(if (not ,cond) (%do ,@body)))
(define-macro obj body
`(%object ,@(mapo (fn (x) x) body)))
(define-macro let (bs rest: body)
(if (atom? bs) `(let (,bs ,(hd body)) ,@(tl body))
(none? bs) `(%do ,@body)
(brackets? bs) `(let (,bs ,(hd body)) ,@(tl body))
(let ((lh rh rest: bs2) bs
(id val rest: bs1) (bind lh rh)
id1 (unique id))
`(%do (%local ,id1 ,val)
(let-symbol (,id ,id1)
(let ,(join bs1 bs2) ,@body))))))
(define-macro with (x v rest: body)
`(let (,x ,v) ,@body ,x))
(define-macro let-when (x v rest: body)
(let-unique (y)
`(let ,y ,v
(when (yes ,y)
(let (,x ,y)
,@body)))))
(define-macro define-transformer (name form rest: body)
`(setenv ',name transformer: (fn (,form) ,@body) ,@(props body)))
(define-macro define-macro (name args rest: body)
`(setenv ',name macro: (fn ,args ,@body) ,@(props body)))
(define-macro define-special (name args rest: body)
`(setenv ',name special: (fn ,args ,@body) ,@(props body)))
(define-macro define-symbol (name expansion)
`(setenv ',name symbol: ',expansion))
(define-macro define-reader ((char s) rest: body)
`(set (get read-table ,char) (fn (,s) ,@body)))
(define-macro define (name x rest: body)
(if (some? body)
`(%local-function ,name ,@(bind-function x body) ,@(props body))
`(%local ,name ,x)))
(define-macro define-global (name x rest: body)
(if (some? body)
`(%global-function ,name ,@(bind-function x body) ,@(props body))
(global-id? name)
`(set ,name ,x)
`(set (get _G ',(compile name)) ,x)))
(define-macro with-frame body
(let-unique (x)
`(%do (add environment* (obj))
(with ,x (%do ,@body)
(drop environment*)))))
(define-macro with-bindings ((names) rest: body)
(let-unique (x)
`(with-frame
(each ,x ,names
(setenv ,x variable: true))
,@body)))
(define-macro let-macro (definitions rest: body)
(with-frame
(step m definitions
(eval `(define-macro ,@m)))
`(%do ,@(macroexpand body))))
(define-macro let-symbol (expansions rest: body)
(with-frame
(step (name exp) (pair expansions)
(eval `(define-symbol ,name ,exp)))
`(%do ,@(macroexpand body))))
(define-macro let-unique (names rest: body)
(let bs (map (fn (n)
(list n `(unique ',n)))
names)
`(let ,(apply join bs)
,@body)))
(define-macro fn (args rest: body)
`(%function ,@(bind-function args body) ,@(props body)))
(define-macro apply (f rest: args)
(if (> (# args) 1)
`(%call apply ,f (join (list ,@(almost args)) ,(last args)))
`(%call apply ,f ,@args)))
(define-macro guard (expr)
(if (= target* 'js)
`((fn () (%try (list true ,expr))))
`(list (xpcall
(fn () ,expr)
(fn (m)
(if (obj? m) m
(obj stack: ((get debug 'traceback))
message: (if (string? m) (clip m (+ (or (search m ": ") -2) 2))
(nil? m) ""
(str m)))))))))
(define-macro each (x t rest: body)
(let-unique (o n i)
(let ((k v) (if (atom? x) (list i x)
(if (> (# x) 1) x
(list i (hd x)))))
`(let (,o ,t)
(for (,k) (pairs ,o)
(let (,v (get ,o ,k))
,@body))))))
(define-macro for (i to rest: body)
(if (obj? i)
`(let ,(apply join (map (fn (x) `(,x nil)) i))
(%for ,to (%names ,@i) (%do ,@body) ,@(props body)))
`(let ,i 0
(while (< ,i ,to)
,@body
(inc ,i)))))
(define-macro step (v t rest: body)
(let-unique (x i)
`(let (,x ,t)
(for ,i (# ,x)
(let (,v (at ,x ,i))
,@body)))))
(define-macro set-of xs
(let l ()
(each x xs
(set (get l x) true))
`(obj ,@l)))
(define-macro language () `',target*)
(define-macro target clauses
(get clauses target*))
(define-macro join! (a rest: bs)
`(set ,a (join ,a ,@bs)))
(define-macro cat! (a rest: bs)
`(set ,a (cat ,a ,@bs)))
(define-macro inc (n by)
`(set ,n (+ ,n ,(if (nil? by) 1 by))))
(define-macro dec (n by)
`(set ,n (- ,n ,(if (nil? by) 1 by))))
(define-macro with-indent (form)
(let-unique (x)
`(%do (inc indent-level*)
(with ,x ,form
(dec indent-level*)))))
(define-macro undefined? (x)
`(target lua: (= ,x nil)
js: (= (typeof ,x) "undefined")))
(define-macro export names
`(with exports (if (undefined? exports) (obj) exports)
,@(map (fn (k)
`(set (exports ,(cat "." k)) ,k))
names)
(target lua: (return exports))))
(define-macro when-compiling body
(eval `(%do ,@body)))
(define-macro during-compilation body
(with form `(%do ,@body (%do))
(eval form)))
(define-transformer compose ((compose rest: fns) rest: body)
(if (none? fns) (macroexpand `(do ,@body))
(one? fns) (macroexpand `(,@fns ,@body))
(macroexpand `((,compose ,@(almost fns)) (,(last fns) ,@body)))))
(define-transformer complement ((complement form) rest: body)
(if (hd? form 'complement)
(macroexpand `(,(at form 1) ,@body))
(macroexpand `(no (,form ,@body)))))
(define-transformer expansion ((expansion) form)
form)
(define-setter hd (v l)
`(set (at ,l 0) ,v))