-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathstdlib.pxi
More file actions
1239 lines (1050 loc) · 34.6 KB
/
stdlib.pxi
File metadata and controls
1239 lines (1050 loc) · 34.6 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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(__ns__ pixie.stdlib)
(def map (fn [f]
(fn [from]
(foreach [x from]
(yield (f x))))))
(comment
(def libc (ffi-library pixie.platform/lib-c-name))
(def exit (ffi-fn libc "exit" [Integer] Integer))
(def puts (ffi-fn libc "puts" [String] Integer))
(def libreadline (ffi-library (str "libreadline." pixie.platform/so-ext)))
(def readline (ffi-fn libreadline "readline" [String] String))
(def rand (ffi-fn libc "rand" [Integer] Integer))
(def srand (ffi-fn libc "srand" [Integer] Integer))
(def reset! -reset!)
(def program-arguments [])
(def fn (fn* [& args]
(cons 'fn* args)))
(set-macro! fn)
(def let (fn* [& args]
(cons 'let* args)))
(set-macro! let)
(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 disj (fn disj
([] [])
([result] result)
([result item] (-disj result item))))
(def disj! (fn conj!
([] (-transient []))
([result] (-persistent! result))
([result item] (-disj! result item))))
(def transient (fn [coll] (-transient coll)))
(def persistent! (fn [coll] (-persistent! coll)))
(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 map (fn ^{:doc "map - creates a transducer that applies f to every input element"
:added "0.1"}
map
([f]
(fn [xf]
(fn
([] (xf))
([result] (xf result))
([result item] (xf result (f item))))))
([f coll]
(let [i (iterator coll)]
(loop []
(if (at-end? i)
nil
(do (yield (f (current i)))
(move-next! i)
(recur))))))))
(def reduce (fn [rf init col]
(-reduce col rf init)))
(def into (fn [to from]
(if (satisfies? IToTransient to)
(persistent! (reduce conj! (transient to) from))
(reduce conj to from))))
(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))))))))
;; Make all Function types extend IFn
(extend -invoke Code -invoke)
(extend -invoke NativeFn -invoke)
(extend -invoke VariadicCode -invoke)
(extend -invoke Closure -invoke)
(extend -invoke Var -invoke)
(extend -invoke PolymorphicFn -invoke)
(extend -invoke DoublePolymorphicFn -invoke)
(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)
(extend -eq Float -num-eq)
(extend -eq Ratio -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)))
(extend -hash PersistentHashSet
(fn [v]
(transduce ordered-hash-reducing-fn v)))
(extend -hash PersistentHashMap
(fn [v]
(transduce ordered-hash-reducing-fn v)))
(extend -hash EmptyList (fn [v] 5555555))
(extend -hash Bool
(fn [v]
(if v
1111111
3333333)))
(def stacklet->lazy-seq
(fn [k]
(if (-at-end? k)
nil
(cons (-current k)
(lazy-seq* (fn [] (stacklet->lazy-seq (-move-next! k))))))))
(def sequence
(fn
([data]
(let [f (create-stacklet
(fn [h]
(reduce (fn ([h item] (h item) h)) h data)))]
(stacklet->lazy-seq f)))
([xform data]
(let [f (create-stacklet
(fn [h]
(transduce xform
(fn ([] h)
([h item] (h item) h)
([h] nil))
data)))]
(stacklet->lazy-seq f)))))
(extend -seq PersistentVector sequence)
(def concat (fn [& args] (transduce cat conj args)))
(def key (fn [x] (-key x)))
(def val (fn [x] (-val x)))
(def defn (fn [nm & rest]
(let [meta (if (instance? String (first rest))
{:doc (first rest)}
{})
rest (if (instance? String (first rest)) (next rest) rest)
meta (if (satisfies? IMap (first rest))
(merge meta (first rest))
meta)
rest (if (satisfies? IMap (first rest)) (next rest) rest)
nm (with-meta nm meta)]
`(def ~nm (fn ~nm ~@rest)))))
(set-macro! defn)
(defn defmacro [nm & rest]
`(do (defn ~nm ~@rest)
(set-macro! ~nm)
~nm))
(set-macro! defmacro)
(defmacro ->
[x & forms]
(loop [x x, forms forms]
(if forms
(let [form (first forms)
threaded (if (seq? form)
(with-meta `(~(first form) ~x ~@(next form)) (meta form))
(list form x))]
(recur threaded (next forms)))
x)))
(defmacro ->>
[x & forms]
(loop [x x, forms forms]
(if forms
(let [form (first forms)
threaded (if (seq? form)
(with-meta `(~(first form) ~@(next form) ~x) (meta form))
(list form x))]
(recur threaded (next forms)))
x)))
(defn not
{:doc "Inverts the input, if a truthy value is supplied, returns false, otherwise
returns true"
:signatures [[arg]]
:added "0.1"}
[x]
(if x false true))
(defn +
{:doc "Adds the arguments"
:signatures [[& args]]
:added "0.1"}
([] 0)
([x] x)
([x y] (-add x y))
([x y & args]
(reduce -add (-add x y) args)))
(defn -
([] 0)
([x] (-sub 0 x))
([x y] (-sub x y))
([x y & args]
(reduce -sub (-sub x y) args)))
(defn *
([] 1)
([x] x)
([x y] (-mul x y))
([x y & args]
(reduce -mul (-mul x y) args)))
(defn /
([x] (-div 1 x))
([x y] (-div x y))
([x y & args]
(reduce -div (-div x y) args)))
(defn quot [num div]
(-quot num div))
(defn rem [num div]
(-rem num div))
(defn =
{:doc "Returns true if all the arguments are equivalent. Otherwise, returns false. Uses
-eq to perform equality checks."
:signatures [[& args]]
:added "0.1"}
([x] true)
([x y] (eq x y))
([x y & rest] (if (eq x y)
(apply = y rest)
false)))
(defn not=
{:doc "Returns true if one (or more) of the arguments are not equivalent to the others. Uses
-eq to perform equality checks."
:signatures [[& args]]
:added "0.1"}
([x] false)
([x y] (not (eq x y)))
([x y & rest] (not (apply = x y rest))))
(defn <
([x] true)
([x y] (-lt x y))
([x y & rest] (if (-lt x y)
(apply < y rest)
false)))
(defn >
([x] true)
([x y] (-gt x y))
([x y & rest] (if (-gt x y)
(apply > y rest)
false)))
(defn <=
([x] true)
([x y] (-lte x y))
([x y & rest] (if (-lte x y)
(apply <= y rest)
false)))
(defn >=
([x] true)
([x y] (-gte x y))
([x y & rest] (if (-gte x y)
(apply >= y rest)
false)))
(defn pos?
{:doc "Returns true if x is greater than zero"
:signatures [[x]]
:added "0.1"}
[x]
(> x 0))
(defn neg?
{:doc "Returns true if x is less than zero"
:signatures [[x]]
:added "0.1"}
[x]
(< x 0))
(defn zero?
{:doc "Returns true if x is equal to zero"
:signatures [[x]]
:added "0.1"}
[x]
(= x 0))
(defn inc
{:doc "Increments x by one"
:signatures [[x]]
:added "0.1"}
[x]
(+ x 1))
(defn dec
{:doc "Decrements x by one"
:signatures [[x]]
:added "0.1"}
[x]
(- x 1))
(defn empty?
{:doc "returns true if the collection has no items, otherwise false"
:signatures [[coll]]
:added "0.1"}
[coll]
(not (seq coll)))
(defn not-empty?
{:doc "returns true if the collection has items, otherwise false"
:signatures [[coll]]
:added "0.1"}
[coll]
(if (seq coll) true false))
(defn first
{:doc "Returns the first item in coll, if coll implements IIndexed nth will be used to retreive
the item from the collection."
:signatures [[coll]]
:added "0.1"}
[coll]
(if (satisfies? IIndexed coll)
(nth coll 0)
(-first coll)))
(defn second
{:doc "Returns the second item in coll, if coll implements IIndexed nth will be used to retreive
the item from the collection."
:signatures [[coll]]
:added "0.1"}
[coll]
(if (satisfies? IIndexed coll)
(nth coll 1)
(first (next coll))))
(defn third
{:doc "Returns the third item in coll, if coll implements IIndexed nth will be used to retreive
the item from the collection."
:signatures [[coll]]
:added "0.1"}
[coll]
(if (satisfies? IIndexed coll)
(nth coll 2)
(first (next (next coll)))))
(defn fourth
{:doc "Returns the fourth item in coll, if coll implements IIndexed nth will be used to retreive
the item from the collection."
:signatures [[coll]]
:added "0.1"}
[coll]
(if (satisfies? IIndexed coll)
(nth coll 3)
(first (next (next (next coll))))))
(defn assoc
([m] m)
([m k v]
(-assoc m k v))
([m k v & rest]
(apply assoc (-assoc m k v) rest)))
(defn dissoc
([m] m)
([m & ks]
(reduce -dissoc m ks)))
(defn contains? [coll key]
(-contains-key coll key))
(defn vec
{:doc "Converts a reducable collection into a vector using the (optional) transducer."
:signatures [[coll] [xform coll]]
:added "0.1"}
([coll]
(transduce conj! coll))
([xform coll]
(transduce xform conj! coll)))
(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)))))
([f1 f2 f3 & fs]
(fn [& args]
(apply (transduce comp (apply list f1 f2 f3 fs)) args))))
(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) obj args)))
(defn true? [v] (identical? v true))
(defn false? [v] (identical? v false))
(defn number? [v] (instance? Number v))
(defn integer? [v] (instance? Integer v))
(defn float? [v] (instance? Float v))
(defn ratio? [v] (instance? Ratio v))
(defn string? [v] (instance? String v))
(defn keyword? [v] (instance? Keyword v))
(defn list? [v] (instance? PersistentList v))
(defn map? [v] (satisfies? IMap v))
(defn fn? [v] (satisfies? IFn v))
(defn indexed? [v] (satisfies? IIndexed v))
(defn counted? [v] (satisfies? ICounted v))
(defn last [coll]
(if (next coll)
(recur (next coll))
(first coll)))
(defn butlast [coll]
(loop [res []
coll coll]
(if (next coll)
(recur (conj res (first coll)) (next coll))
(seq res))))
(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)))
(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)))
(defn keys
{:doc "If called with no arguments returns a transducer that will extract the key from each map entry. If passed
a collection, will assume that it is a hashmap and return a vector of all keys from the collection."
:signatures [[] [coll]]
:added "0.1"}
([] (map key))
([m]
(transduce (map key) conj! m)))
(defn vals
{:doc "If called with no arguments returns a transducer that will extract the key from each map entry. If passed
a collection, will assume that it is a hashmap and return a vector of all keys from the collection."
:signatures [[] [coll]]
:added "0.1"}
([] (map val))
([m]
(transduce (map val) conj! m)))
(extend -seq PersistentHashMap
(fn [m]
(reduce conj nil m)))
(extend -str PersistentHashMap
(fn [v]
(let [entry->str (map (fn [e] (vector (key e) " " (val e))))]
(apply str "{" (conj (transduce (comp entry->str (interpose [", "]) cat) conj v) "}")))))
(extend -hash PersistentHashMap
(fn [v]
(transduce cat unordered-hash-reducing-fn v)))
(extend -seq PersistentHashSet sequence)
(extend -str PersistentHashSet
(fn [s]
(apply str "#{" (conj (transduce (interpose " ") conj s) "}"))))
(extend -empty Cons (fn [_] '()))
(extend -empty LazySeq (fn [_] '()))
(extend -empty PersistentList (fn [_] '()))
(extend -empty EmptyList (fn [_] '()))
(extend -empty PersistentVector (fn [_] []))
(extend -empty Array (fn [_] (make-array 0)))
(extend -empty PersistentHashMap (fn [_] {}))
(extend -empty PersistentHashSet (fn [_] #{}))
(extend -conj PersistentHashMap
(fn [coll x]
(cond
(instance? MapEntry x)
(assoc coll (key x) (val x))
(instance? PersistentVector x)
(if (= (count x) 2)
(assoc coll (nth x 0) (nth x 1))
(throw "Vector arg to map conj must be a pair"))
(satisfies? ISeqable x)
(reduce conj coll (-seq x))
:else
(throw (str (type x) " cannot be conjed to a map")))))
(defn empty
{:doc "Returns an empty collection of the same type, or nil."
:added "0.1"}
[coll]
(if (satisfies? IEmpty coll)
(-empty coll)
nil))
(extend -str Keyword
(fn [k]
(if (namespace k)
(str ":" (namespace k) "/" (name k))
(str ":" (name k)))))
(extend -invoke Keyword (fn [k m] (-val-at m k nil)))
(extend -invoke PersistentHashMap (fn [m k] (-val-at m k nil)))
(extend -invoke PersistentHashSet (fn [m k] (-val-at m k nil)))
(defn get
([mp k]
(get mp k nil))
([mp k not-found]
(-val-at mp k not-found)))
(defn get-in
([m ks]
(reduce get m ks))
([m ks not-found]
(loop [sentinel 'x
m m
ks (seq ks)]
(if ks
(let [m (get m (first ks) sentinel)]
(if (identical? sentinel m)
not-found
(recur sentinel m (next ks))))
m))))
(defn assoc-in
([m ks v]
(let [ks (seq ks)
k (first ks)
ks (next ks)]
(if ks
(assoc m k (assoc-in (get m k) ks v))
(assoc m k v)))))
(def subs pixie.string/substring)
(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-ns (quote ~ns))
(refer-ns (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 lazy-seq [& body]
`(lazy-seq* (fn [] ~@body)))
(def Protocol @(resolve (symbol "/Protocol")))
(defn protocol? [x]
(instance? Protocol x))
(defmacro deftype [nm fields & body]
(let [ctor-name (symbol (str "->" (name nm)))
fields (transduce (map (comp keyword name)) conj fields)
field-syms (transduce (map (comp symbol name)) conj fields)
mk-body (fn [body]
(let [fn-name (first body)
_ (assert (symbol? fn-name) "protocol override must have a name")
args (second body)
_ (assert (vector? args) "protocol override must have arguments")
self-arg (first args)
_ (assert (symbol? self-arg) "protocol override must have at least one `self' argument")
field-lets (transduce (comp (map (fn [f]
[(symbol (name f)) (list 'get-field self-arg f)]))
cat)
conj fields)
rest (next (next body))]
`(fn ~fn-name ~args (let ~field-lets ~@rest))))
bodies (reduce
(fn [res body]
(cond
(symbol? body) (cond
(= body 'Object) [body (second res) (third res)]
(protocol? @(resolve body)) [@(resolve body) (second res) (conj (third res) body)]
:else (throw (str "can only extend protocols or Object, not " body)))
(seq? body) (let [proto (first res) tbs (second res) pbs (third res)]
(if (protocol? proto)
[proto tbs (conj pbs body)]
[proto (conj tbs body) pbs]))))
[nil [] []]
body)
type-bodies (second bodies)
proto-bodies (third bodies)
all-fields (reduce (fn [r tb] (conj r (keyword (name (first tb))))) fields type-bodies)
type-decl `(def ~nm (create-type ~(keyword (name nm)) ~all-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)
~@(transduce
(map (fn [type-body]
`(set-field! ~inst ~(keyword (name (first type-body))) ~(mk-body type-body))))
conj
type-bodies)
~inst))
proto-bodies (transduce
(map (fn [body]
(cond
(symbol? body) `(satisfy ~body ~nm)
(seq? body) `(extend ~(first body) ~nm ~(mk-body body))
:else (assert false "Unknown body element in deftype, expected symbol or seq"))))
conj
proto-bodies)]
`(do ~type-decl
~ctor
~@proto-bodies)))
(defmacro defrecord [nm fields & body]
(let [ctor-name (symbol (str "->" (name nm)))
map-ctor-name (symbol (str "map" (name ctor-name)))
fields (transduce (map (comp keyword name)) conj fields)
type-from-map `(defn ~map-ctor-name [m]
(apply ~ctor-name (map #(get m %) ~fields)))
default-bodies ['IAssociative
`(-assoc [self k v]
(let [m (reduce #(assoc %1 %2 (. self %2)) {} ~fields)]
(~map-ctor-name (assoc m k v))))
`(-contains-key [self k]
(contains? ~(set fields) k))
`(-dissoc [self k]
(throw "dissoc is not supported on defrecords"))
'ILookup
`(-val-at [self k not-found]
(if (contains? ~(set fields) k)
(. self k)
not-found))
'IObject
`(-str [self]
(str "<" ~(name nm) " " (reduce #(assoc %1 %2 (. self %2)) {} ~fields) ">"))
`(-eq [self other]
(and (instance? ~nm other)
~@(map (fn [field]
`(= (. self ~field) (. other ~field)))
fields)))
`(-hash [self]
(throw "not implemented"))]
deftype-decl `(deftype ~nm ~fields ~@default-bodies ~@body)]
`(do ~type-from-map
~deftype-decl)))
(def libc (ffi-library pixie.platform/lib-c-name))
(def exit (ffi-fn libc "exit" [Integer] Integer))
(def puts (ffi-fn libc "puts" [String] Integer))
(def printf (ffi-fn libc "printf" [String] Integer))
(def getenv (ffi-fn libc "getenv" [String] String))
(defn print [& args]
(puts (apply str args)))
(defn doc [x]
(get (meta x) :doc))
(defn swap! [a f & args]
(reset! a (apply f @a args)))
(defn update-in
[m ks f & args]
(let [f (fn [m] (apply f m args))
update-inner-f (fn update-inner-f
([m f k]
(assoc m k (f (get m k))))
([m f k & ks]
(assoc m k (apply update-inner-f m f ks))))]
(apply update-inner-f m f ks)))
(defn nil? [x]
(identical? x nil))
(defn fnil [f else]
(fn [x & args]
(apply f (if (nil? x) else x) args)))
(defmacro foreach [binding & body]
(assert (= 2 (count binding)) "binding and collection required")
`(reduce
(fn [_ ~ (nth binding 0)]
~@body
nil)
nil
~(nth binding 1)))
(defmacro iterate [binding & body]
(assert (= 2 (count binding)) "binding and collection required")
`(let [i# (iterator ~(second binding))]
(loop []
(if (at-end? i#)
nil
(let [~(first binding) (current i#)]
~@body
(move-next! i#)
(recur))))))
(defmacro dotimes [bind & body]
(let [b (nth bind 0)]
`(let [max# ~(nth bind 1)]
(loop [~b 0]
(if (= ~b max#)
nil
(do ~@body
(recur (inc ~b))))))))
(extend -iterator PersistentVector
(fn [v]
(dotimes [x (count v)]
(yield (nth v x)))))
(defmacro and
([] true)
([x] x)
([x y] `(if ~x ~y false))
([x y & more] `(if ~x (and ~y ~@more))))
(defmacro or
([] false)
([x] x)
([x y] `(let [r# ~x]
(if r# r# ~y)))
([x y & more] `(let [r# ~x]
(if r# r# (or ~y ~@more)))))
(defmacro when [test & body]
`(if ~test (do ~@body)))
(defmacro when-not [test & body]
`(if (not ~test) (do ~@body)))
(defmacro when-let [binding & body]
`(let ~binding
(when ~(first binding)
~@body)))
(defn nnext [coll]
(next (next coll)))
(defn nthnext [coll n]
(loop [n n
xs (seq coll)]
(if (and xs (pos? n))
(recur (dec n) (next xs))
xs)))
(defn take [n coll]
(when (pos? n)
(when-let [s (seq coll)]
(cons (first s) (take (dec n) (next s))))))
(defn drop [n coll]
(let [s (seq coll)]
(if (and (pos? n) s)
(recur (dec n) (next s))
s)))
(defn partition
([n coll] (partition n n coll))
([n step coll]
(when-let [s (seq coll)]
(cons (take n s) (partition n step (drop step s))))))
(defn destructure [binding expr]
(cond
(symbol? binding) [binding expr]
(vector? binding) (let [name (gensym "vec__")]
(reduce conj [name expr]
(destructure-vector binding name)))
(map? binding) (let [name (gensym "map__")]
(reduce conj [name expr]
(destructure-map binding name)))
:else (throw (str "unsupported binding form: " binding))))
(defn destructure-vector [binding-vector expr]
(loop [bindings (seq binding-vector)