forked from pixie-lang/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib.lisp
More file actions
405 lines (316 loc) · 10 KB
/
stdlib.lisp
File metadata and controls
405 lines (316 loc) · 10 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
394
395
396
397
398
399
400
401
402
403
404
405
(__ns__ pixie.stdlib)
(def reset! -reset!)
(def map (fn map [f]
(fn [xf]
(fn
([] (xf))
([result] (xf result))
([result item] (xf result (f item)))))))
(def conj (fn conj
([] [])
([result] result)
([result item] (-conj result item))))
(def conj! (fn conj!
([] (-transient []))
([result] (-persistent! result))
([result item] (-conj! result item))))
(def transduce (fn transduce
([f coll]
(let [result (-reduce coll f (f))]
(f result)))
([xform rf coll]
(let [f (xform rf)
result (-reduce coll f (f))]
(f result)))
([xform rf init coll]
(let [f (xform rf)
result (-reduce coll f init)]
(f result)))))
(def reduce (fn [rf init col]
(-reduce col rf init)))
(def interpose
(fn interpose [val]
(fn [xf]
(let [first? (atom true)]
(fn
([] (xf))
([result] (xf result))
([result item] (if @first?
(do (reset! first? false)
(xf result item))
(xf (xf result val) item))))))))
(def preserving-reduced
(fn [rf]
(fn [a b]
(let [ret (rf a b)]
(if (reduced? ret)
(reduced ret)
ret)))))
(def cat
(fn cat [rf]
(let [rrf (preserving-reduced rf)]
(fn cat-inner
([] (rf))
([result] (rf result))
([result input]
(reduce rrf result input))))))
(def seq-reduce (fn seq-reduce
[coll f init]
(loop [init init
coll (seq coll)]
(if (reduced? init)
@init
(if (seq coll)
(recur (f init (first coll))
(seq (next coll)))
init)))))
(def indexed-reduce (fn indexed-reduce
[coll f init]
(let [max (count coll)]
(loop [init init
i 0]
(if (reduced? init)
@init
(if (-eq i max)
init
(recur (f init (nth coll i)) (+ i 1))))))))
(extend -reduce Cons seq-reduce)
(extend -reduce PersistentList seq-reduce)
(extend -reduce LazySeq seq-reduce)
(comment (extend -reduce Array indexed-reduce))
(extend -str Bool
(fn [x]
(if (identical? x true)
"true"
"false")))
(extend -str Nil (fn [x] "nil"))
(extend -reduce Nil (fn [self f init] init))
(extend -hash Nil (fn [self] 100000))
(extend -hash Integer hash-int)
(extend -eq Integer -num-eq)
(def ordered-hash-reducing-fn
(fn ordered-hash-reducing-fn
([] (new-hash-state))
([state] (finish-hash-state state))
([state itm] (update-hash-ordered! state itm))))
(def unordered-hash-reducing-fn
(fn unordered-hash-reducing-fn
([] (new-hash-state))
([state] (finish-hash-state state))
([state itm] (update-hash-unordered! state itm))))
(extend -str PersistentVector
(fn [v]
(apply str "[" (conj (transduce (interpose ", ") conj v) "]"))))
(extend -str Cons
(fn [v]
(apply str "(" (conj (transduce (interpose ", ") conj v) ")"))))
(extend -hash Cons
(fn [v]
(transduce ordered-hash-reducing-fn v)))
(extend -str PersistentList
(fn [v]
(apply str "(" (conj (transduce (interpose ", ") conj v) ")"))))
(extend -str LazySeq
(fn [v]
(apply str "(" (conj (transduce (interpose ", ") conj v) ")"))))
(extend -hash PersistentVector
(fn [v]
(transduce ordered-hash-reducing-fn v)))
(def stacklet->lazy-seq
(fn [f]
(let [val (f nil)]
(if (identical? val :end)
nil
(cons val (lazy-seq* (fn [] (stacklet->lazy-seq f))))))))
(def sequence
(fn
([data]
(let [f (create-stacklet
(fn [h]
(reduce (fn ([h item] (h item) h)) h data)
(h :end)))]
(stacklet->lazy-seq f)))
([xform data]
(let [f (create-stacklet
(fn [h]
(transduce xform
(fn ([] h)
([h item] (h item) h)
([h] (h :end)))
data)))]
(stacklet->lazy-seq f)))))
(extend -seq PersistentVector sequence)
(extend -seq Array sequence)
(def concat (fn [& args] (transduce cat conj args)))
(def defn (fn [nm & rest] `(def ~nm (fn ~nm ~@rest))))
(set-macro! defn)
(defn defmacro [nm & rest]
`(do (defn ~nm ~@rest)
(set-macro! ~nm)
~nm))
(set-macro! defmacro)
(defn +
[& args]
(reduce -add 0 args))
(defn -
([] 0)
([x] (-sub 0 x))
([x & args]
(reduce -sub x args)))
(defn =
([x] true)
([x y] (eq x y))
([x y & rest] (if (eq x y)
(apply = y rest)
false)))
(def inc (fn [x] (+ x 1)))
(def dec (fn [x] (- x 1)))
(defn assoc
([m] m)
([m k v]
(-assoc m k v))
([m k v & rest]
(apply assoc (-assoc m k v) rest)))
(def slot-tp (create-type :slot [:val]))
(defn ->Slot [x]
(let [inst (new slot-tp)]
(set-field! inst :val x)))
(defn get-val [inst]
(get-field inst :val))
(defn comp
([f] f)
([f1 f2]
(fn [& args]
(f1 (apply f2 args))))
([f1 f2 f3]
(fn [& args]
(f1 (f2 (apply f3 args))))))
(defn not [x]
(if x false true))
(defmacro cond
([] nil)
([test then & clauses]
`(if ~test
~then
(cond ~@clauses))))
(defmacro try [& body]
(loop [catch nil
catch-sym nil
body-items []
finally nil
body (seq body)]
(let [form (first body)]
(if form
(if (not (seq? form))
(recur catch catch-sym (conj body-items form) finally (next body))
(let [head (first form)]
(cond
(= head 'catch) (if catch
(throw "Can only have one catch clause per try")
(recur (next (next form)) (first (next form)) body-items finally (next body)))
(= head 'finally) (if finally
(throw "Can only have one finally clause per try")
(recur catch catch-sym body-items (next form) (next body)))
:else (recur catch catch-sym (conj body-items form) finally (next body)))))
`(-try-catch
(fn [] ~@body-items)
~(if catch
`(fn [~catch-sym] ~@catch)
`(fn [] nil))
(fn [] ~@finally))))))
(defn .
([obj sym]
(get-field obj sym))
([obj sym & args]
(apply (get-field obj sym) args)))
(extend -count MapEntry (fn [self] 2))
(extend -nth MapEntry (fn [self idx not-found]
(cond (= idx 0) (-key self)
(= idx 1) (-val self)
:else not-found)))
(defn key [x]
(-key x))
(defn val [x]
(-val x))
(extend -reduce MapEntry indexed-reduce)
(extend -str MapEntry
(fn [v]
(apply str "[" (conj (transduce (interpose ", ") conj v) "]"))))
(extend -hash MapEntry
(fn [v]
(transduce ordered-hash-reducing-fn v)))
(extend -str PersistentHashMap
(fn [v]
(apply str "{" (conj (transduce (comp cat (interpose " ")) conj v) "}"))))
(extend -hash PersistentHashMap
(fn [v]
(transduce cat unordered-hash-reducing-fn v)))
(extend -str Keyword
(fn [k]
(if (namespace k)
(str ":" (namespace k) "/" (name k))
(str ":" (name k)))))
(defn get
([mp k]
(get mp k nil))
([mp k not-found]
(-val-at mp k not-found)))
(defmacro assert
([test]
`(if ~test
nil
(throw "Assert failed")))
([test msg]
`(if ~test
nil
(throw (str "Assert failed " ~msg)))))
(defmacro resolve [sym]
`(resolve-in (this-ns-name) ~sym))
(defmacro with-bindings [binds & body]
`(do (push-binding-frame!)
(reduce (fn [_ map-entry]
(set! (resolve (key map-entry)) (val map-entry)))
nil
(apply hashmap ~binds))
(let [ret (do ~@body)]
(pop-binding-frame!)
ret)))
(def foo 42)
(set-dynamic! (resolve 'pixie.stdlib/foo))
(defmacro require [ns kw as-nm]
(assert (= kw :as) "Require expects :as as the second argument")
`(do (load-file (quote ~ns))
(refer (this-ns-name) (the-ns (quote ~ns)) (quote ~as-nm))))
(defmacro ns [nm & body]
`(do (__ns__ ~nm)
~@body))
(defn symbol? [x]
(identical? Symbol (type x)))
(defmacro deftype [nm fields & body]
(let [ctor-name (symbol (str "->" (name nm)))
type-decl `(def ~nm (create-type ~(keyword (name nm)) ~fields))
field-syms (transduce (map (comp symbol name)) conj fields)
inst (gensym)
ctor `(defn ~ctor-name ~field-syms
(let [~inst (new ~nm)]
~@(transduce
(map (fn [field]
`(set-field! ~inst ~field ~(symbol (name field)))))
conj
fields)
~inst))
proto-bodies (transduce
(map (fn [body]
(cond
(symbol? body) `(satisfy ~body ~nm)
(seq? body) `(extend ~(first body) ~nm (fn ~@body))
:else (assert false "Unknown body element in deftype, expected symbol or seq"))))
conj
body)]
`(do ~type-decl
~ctor
~@proto-bodies)))
(def libc (ffi-library (str "libc" "." pixie.platform/so-ext)))
(def exit (ffi-fn libc "exit" [Integer] Integer))
(def puts (ffi-fn libc "puts" [String] Integer))
(def printf (ffi-fn libc "printf" [String] Integer))