forked from pixie-lang/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.pxi
More file actions
395 lines (342 loc) · 12.4 KB
/
compiler.pxi
File metadata and controls
395 lines (342 loc) · 12.4 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
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
(ns pixie.compiler
(:require [pixie.string :as string]
[pixie.ast :refer :all]))
(defn remove-bootstrap [env sym]
(if (= (namespace x) "pixie.bootstrap-macros")
(symbol (str "pixie.stdlib/" (name x)))
x))
(defmulti analyze-form (fn [_ x]
(cond
(identical? x true) :bool
(identical? x false) :bool
(keyword? x) :keyword
(nil? x) nil
(seq? x) :seq
(vector? x) :vector
(symbol? x) :symbol
(number? x) :number
(string? x) :string
(char? x) :char
(map? x) :map
(set? x) :set
:else (do (println "Cant' Analyze " x)
(assert false "Can't analyze")))))
(defmulti analyze-seq (fn [_ x]
(let [f (first x)]
(if (symbol? f)
(let [sname (symbol (name f))]
(if (get @(get-field analyze-seq :methods) sname)
sname
f))
:invoke))))
;; Special Forms
(defmethod analyze-seq 'do
[env x]
(let [statement-asts (mapv (partial analyze-form (assoc env :tail? false))
(butlast (next x)))]
(->Do statement-asts
(analyze-form env (last x))
x
env)))
(defmethod analyze-seq 'comment
[env x]
(->Const nil env))
(defmethod analyze-seq 'if
[env [_ test then else :as form]]
(->If (analyze-form (assoc env :tail? false) test)
(analyze-form env then)
(analyze-form env else)
form
env))
(defmethod analyze-seq 'this-ns-name
[env [_]]
(analyze-form env (name @(:ns env))))
(defmethod analyze-seq 'with-handler
[env [_ [h handler] & body]]
(analyze-form env
`(let [~h ~handler]
(~'pixie.stdlib/-effect-finally
~h
(~'pixie.stdlib/-with-handler
~h
(fn []
(~'pixie.stdlib/-effect-val
~h
(do ~@body))))))))
(defmethod analyze-seq 'defeffect
[env [_ nm & sigs]]
(analyze-form env
`(do (def ~nm (~'pixie.stdlib/protocol ~(str nm)))
~@(map (fn [[x]]
`(def ~x (~'pixie.stdlib/-effect-fn
(~'pixie.stdlib/polymorphic-fn ~(str x) ~nm))))
sigs))))
(defmethod analyze-seq 'fn*
[env [_ & body :as form]]
(let [[name body] (if (symbol? (first body))
[(first body)
(next body)]
[(gensym "ann-fn_")
body])
arities (if (vector? (first body))
[body]
body)
new-env (assoc env
:recur-point name
:tail? true)
analyzed-bodies (reduce
(partial analyze-fn-body new-env name)
{}
arities)]
(->Fn name
(vals analyzed-bodies)
form
new-env)))
(defmethod analyze-seq 'try
[env [_ & body :as form]]
(let [analyzed (reduce
(fn [acc f]
(cond
(and (seq? f)
(= (first f) 'catch))
(let [[_ k ex-nm & body] f]
(assert (keyword? k) "First argument to catch must be a keyword")
(assoc-in acc [:catches k] `(fn [~ex-nm]
~@body)))
(and (seq? f)
(= (first f) 'finally))
(let [[_ & body] f]
(assert (nil? (:finally acc)) "Can only have one finally block in a try")
(assoc acc :finally `(fn []
~@body)))
:else (update-in acc [:bodies] conj f)))
{}
body)]
(analyze-form env `(~'pixie.stdlib/-try
(fn []
~@(or (:bodies analyzed)
[]))
~(or (:catches analyzed)
{})
~(or (:finally analyzed)
`(fn [] nil))))))
(defn add-local [env type bind-name]
(assoc-in env [:locals bind-name] (->Binding type bind-name bind-name env)))
(defn analyze-fn-body [env fn-name acc [args & body :as form]]
(let [[args variadic?] (let [not& (vec (filter (complement (partial = '&)) args))]
[not& (= '& (last (butlast args)))])
arity (count args)
arity-idx (if variadic? -1 arity)
new-env (add-local env :fn-self fn-name)
new-env (reduce
(fn [acc arg-name]
(add-local acc :arg arg-name))
new-env
args)]
(assert (not (acc arity-idx)) (str "Duplicate arity for " (cons args body)))
(assoc acc arity-idx (->FnBody fn-name
arity
args
nil
variadic?
(analyze-form
(assoc new-env :tail? true)
(cons 'do body))
form
env))))
(defmethod analyze-seq 'let*
[env [_ bindings & body :as form]]
(assert (even? (count bindings)) "Let requires an even number of bindings")
(let [parted (partition 2 bindings)
[new-env bindings] (reduce
(fn [[new-env bindings] [name binding-form :as pair]]
(let [binding-ast (->LetBinding name
(analyze-form
(assoc new-env :tail? false)
binding-form)
binding-form
env)]
[(assoc-in new-env [:locals name] binding-ast)
(conj bindings binding-ast)]))
[env []]
parted)]
(->Let bindings
(analyze-form (assoc new-env
:tail? (:tail? env))
`(do ~@body))
form
env)))
(defmethod analyze-seq 'loop*
[env [_ bindings & body]]
(let [parted (partition 2 bindings)]
(analyze-form (assoc env :recur-point '__loop__fn__)
`((fn ~'__loop__fn__ ~(mapv first parted)
~@body)
~@(mapv second parted)))))
(defmethod analyze-seq 'recur
[env [_ & args]]
(assert (:tail? env) "Can only recur at tail position")
(assert (:recur-point env) "Don't have a point to recur to")
(analyze-form env `(~(:recur-point env) ~@args)))
(defmethod analyze-seq 'var
[env [_ nm]]
(->VarConst @(:ns env) nm nm env))
(defmethod analyze-seq 'def
[env [_ nm val :as form]]
(->Def
nm
(analyze-form env val)
form
env))
(defmethod analyze-seq 'quote
[env [_ val]]
(if (map? val)
(analyze-seq env
(with-meta
`(pixie.stdlib/hashmap ~@(reduce
(fn [acc [k v]]
(-> acc
(conj `(~'quote k))
(conj `(~'quote v))))
[]
val))
(meta val)))
(->Const val env)))
(defmethod analyze-seq 'in-ns
[env [_ nsp]]
(let [nsp (if (and (:bootstrap? env)
(= (name nsp) "pixie.bootstrap-macros"))
(symbol "pixie.stdlib")
nsp)]
(reset! (:ns env) (symbol (name nsp)))
(in-ns nsp)
(in-ns :pixie.compiler)
(analyze-form env (list 'pixie.stdlib/-in-ns (keyword (name nsp))))))
(defmethod analyze-seq 'local-macro
[env [_ [nm replace] & body :as form]]
(let [new-env (assoc-in env [:locals nm] {:op :local-macro
:name nm
:replace-with replace
:form form})]
(analyze-form new-env (cons 'do body))))
(defmethod analyze-form nil
[env _]
(->Const nil env))
(defmethod analyze-form :bool
[env form]
(->Const form env))
(defn keep-meta [new old]
(if-let [md (meta old)]
(if (satisfies? IMeta new)
(with-meta new md)
new)
new))
(defn resolve-sym [env sym]
(cond
(and (:bootstrap? env))
(try (resolve-in (the-ns :pixie.bootstrap-macros)
(symbol (name sym)))
(catch :pixie.stdlib/AssertionException ex
nil))
(namespace sym)
(try (resolve-in (the-ns (namespace sym))
(symbol (name sym)))
(catch :pixie.stdlib/AssertionException ex
nil))
:else
(try (resolve-in (the-ns @(:ns env))
sym)
(catch :pixie.stdlib/AssertionException ex
nil))))
(defmethod analyze-seq :default
[env [sym & args :as form]]
(let [sym (if (and (symbol? sym)
(= "pixie.bootstrap-macros" (namespace sym)))
(symbol (str "pixie.stdlib/" (name sym)))
sym)
resolved (when (symbol? sym)
(resolve-sym env sym))]
(cond
(and (symbol? sym)
(string/starts-with? (name sym) ".-"))
(let [sym-kw (keyword (string/substring (name sym) 2))
result (analyze-form env (keep-meta `(~'pixie.stdlib/-get-field ~@args ~sym-kw)
form))]
result)
(and resolved
(macro? @resolved))
(analyze-form env (keep-meta (apply @resolved args)
form))
:else
(->Invoke (let [new-env (assoc env :tail? false)]
(mapv (partial analyze-form new-env) form))
(:tail? env)
form
env))))
(defmethod analyze-form :number
[env x]
(->Const x env))
(defmethod analyze-form :keyword
[env x]
(->Const x env))
(defmethod analyze-form :string
[env x]
(->Const x env))
(defmethod analyze-form :char
[env x]
(->Const x env))
(defmethod analyze-form :seq
[env x]
(analyze-seq env x))
(defmethod analyze-form :symbol
[env x]
(if-let [local (get-in env [:locals x])]
(if (= (:op local) :local-macro)
(analyze-form env (:replace-with local))
(assoc local :form x))
(maybe-var env x)))
(defmethod analyze-form :vector
[env x]
(->Vector (mapv (partial analyze-form env) x)
x
env))
(defmethod analyze-form :map
[env x]
(analyze-seq env
(with-meta
`(pixie.stdlib/hashmap ~@(reduce
(fn [acc [k v]]
(-> acc
(conj k)
(conj v)))
[]
x))
(meta x))))
(defmethod analyze-form :set
[env x]
(analyze-seq env
(with-meta
`(pixie.stdlib/set ~(vec x))
(meta x))))
(defn maybe-var [env x]
(let [nsp (if (= (namespace x) "pixie.bootstrap-macros")
(symbol (str "pixie.stdlib/" (name x)))
x)]
(->Var @(:ns env) nsp nsp env)))
;; ENV Functions
(defn new-env
"Creates a new (empty) environment"
[bootstrap?]
(->Env
(atom (symbol "pixie.stdlib"))
nil
{}
true
nil
bootstrap?
nil))
(defn analyze
([form]
(analyze form (new-env false)))
([form env]
(analyze-form env form)))