-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbootstrap.lisp
More file actions
1623 lines (1383 loc) · 55.4 KB
/
bootstrap.lisp
File metadata and controls
1623 lines (1383 loc) · 55.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
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
;;This is the basis for bootstrapping clojure onto common lisp.
;;I figure if I can define the primitive forms that clojure requires,
;;there's already a ton of clojure written in clojure. The clojurescript
;;runtime actually has a significant portion of clojure defined via protocols,
;;which given limited forms, provides a pretty slick way to bootstrap an
;;implementation.
;;A couple of big hurdles:
;;1) Lisp1 vs Lisp2. I'll hack the evaluator for this.
;;2) Persistent structures. Already built Pvector and 1/2 done with Pmap.
;;3) Protocols. Already implemented as generic functions.
;;4) Multimethods. Need to find a way to implement multiple dispatch.
;;5) Reader. CL macros use , and ,@ in place of ~ and ~@ in Clojure.
;; We'll need to either cook the common lisp reader, or
;; build a separate clojure reader that will perform the
;; appropriate replacements.
;; @ is a literal for #'deref in clojure
;; , is whitespace in clojure.
;; [] denote vectors -> already have a reader macro in pvector.lisp
;; {} denote maps -> already have a reader macro in pmap.lisp
;; #{} denote sets
;;6) Destructuring. This may be a bit tricky, although there are a limited number of
;; clojure forms. Since we have reader
;;7)Seq library. This shouldn't be too hard. I already have a lazy list lib prototype
;; as well as generic functions for the basic ops. I think I'll try to
;; use the protocols defined in the clojurescript version as much possible,
;; rather than baking in a lot of the seq abstraction in the host language
;; like clojure does.
(defpackage :clclojure.base
(:use :common-lisp :common-utils
:clclojure.keywordfunc :clclojure.lexical
:clclojure.pvector :clclojure.cowmap :clclojure.protocols :clclojure.eval)
(:shadow :let :deftype :defmacro :map :reduce :first :rest :second :dotimes :nth :cons :count :do :get :assoc :when-let :vector
:odd? :even? :zero? :identity :filter :loop :if-let :throw :list* :cond
:=)
(:export :def :defn :fn :meta :with-meta :str :symbol? :first :rest :second :next
:deftype :defprotocol :reify :extend-type :nil? :identical?
:extend-protocol :let :into :take :drop :filter :seq :vec :empty :conj :concat :map :reduce :dotimes :nth :cons :count :do :get :assoc :when-let
:if-let :ns :even? :pos? :zero? :odd? :vector :hash-map :inc :dec :identity :loop :chunk-first
:doall :chunk-buffer :every? :chunk-rest :interleave :ffirst :partition :seq->list :fnext :chunk-cons :nthrest
:dorun :chunked-seq? :->iterator :chunk-append :throw :ex-info :ex-cause :ex-message :ex-data :list* :cond :try := :true :false) ; :defmacro
)
(in-package clclojure.base)
;;move this later...
(EVAL-WHEN (:compile-toplevel :load-toplevel :execute)
;;temporary hacks...
(define-symbol-macro true 't)
(define-symbol-macro false nil)
;;convenient placeholder
(defun ns (name &rest opts)
(eval `(progn (defpackage ,name
(:use :clclojure.base :common-lisp)
(:shadowing-import-from :clclojure.base :let :deftype :defmacro :map :reduce :first :rest :second :dotimes :nth :cons :count :do :get :assoc :when-let :vector))
(in-package ,name))))
(common-lisp:defmacro defmacro (name args &rest body)
;`(clclojure.eval:defmacro/literal-walker ,name ,args ,@body)
`(common-lisp:defmacro ,name ,args ,@body)
)
(defun vector? (x) (typep x 'clclojure.pvector::pvec))
;;Let's hack let to allow us to infer vector-binds
;;as a clojure let definition...
(defmacro let (bindings &body body)
(if ;(eq (common-lisp:first bindings) 'persistent-vector)
(vector? bindings)
`(unified-let* (,@(partition! 2 (vector-to-list bindings))) ,@body)
`(cl:let ,bindings ,@body)))
(defun macro? (s) (when (macro-function s) 't))
(defun function? (s) (fboundp s))
;;weak hack around lack of read-time vector creation.
(defun vector-form? (expr) (or (vector? expr) (eq (common-lisp:first expr) 'persistent-vector))))
(define-condition not-implemented (error) ())
(define-condition uneven-arguments (error) ())
(defgeneric destructure (bindings))
;;a single function definition
(EVAL-WHEN (:compile-toplevel :load-toplevel :execute)
(defstruct fn-def name args body)
;;a macro definition -- later
(defstruct macro-def name args body))
;;At compile-time, [x y] -> (persistent-vector x y).
;;This is upsetting us...
(defmacro quoted-vec (v)
(if (vector? v)
`(quote ,v);;`(persistent-vector ,@(mapcar #'quote-sym (vector-to-list v)))
`(persistent-vector ,@(mapcar #'quote-sym (rest v)))))
(defun variadic (v) (member '& (vector-to-list v)))
;;Todo: move this out to CLOS?
;;parse a clojure style function definition.
;; (defmacro read-fn (arg-vec body)
;; `(make-fn-def :args (quoted-vec ,arg-vec)
;; :body (quote ,body)))
(EVAL-WHEN (:compile-toplevel :load-toplevel :execute)
(defun read-fn (arg-vec body &optional name)
(make-fn-def :name name
:args arg-vec
:body body)))
(defgeneric arity (fd))
(defmethod arity ((fd clclojure.pvector::pvec))
(values (vector-count fd) (variadic fd)))
(defmethod arity ((fd fn-def)) (arity (slot-value fd 'args)))
;;since clojure allows multiple bodies, with fixed arity for each body, we
;;compose multiple function (arg body) pairs into a list of function definitions.
;;We should then be able to dispatch on the count of args, simply invoking
;;the appropriate function matched to arity.
;; (defmacro fn* (&rest specs)
;; `(list ,@(mapcar (lambda (vb) `(read-fn ,(common-lisp:first vb) ,(second vb))) specs)))
(EVAL-WHEN (:compile-toplevel :load-toplevel :execute)
;; (defun fn* (&rest specs)
;; (let* ((fst (car specs))
;; (named? nil)
;; (name (if (symbolp (common-lisp:first fst))
;; (progn (setf named? t)
;; (common-lisp:first fst) )
;; (symb (symbol-name (gensym "fn_")))))
;; (specs (if named? (common-lisp:rest (common-lisp:first specs)) specs)))
;; (pprint (list fst named? name specs))
;; `(,@(mapcar (lambda (vb)
;; (pprint vb)
;; (read-fn (common-lisp:first vb)
;; (if (cadr vb)
;; (common-lisp:cons 'progn (common-lisp:rest vb))
;; (second vb))
;; name))
;; specs
;; ))))
(defun fn* (name &rest specs)
`(,@(mapcar (lambda (vb) (read-fn (common-lisp:first vb)
(if (cadr vb)
(common-lisp:cons 'progn (common-lisp:rest vb))
(common-lisp:second vb)) name)) specs)))
;; (defparameter test-fn
;; '(fn* ([x] x)
;; ([x y] (+ x y))
;; ([x y & xs] (common-lisp:reduce #'+ xs :initial-value (+ x y)))))
(defstruct arg-parse lambda-list outer-let)
(define-condition no-matching-function (error) ())
(define-condition multiple-variadic-functions (error) ())
;;this is going to be somewhat tricky, since we'll probably have a little state
;;machine that parses the args, possibly destructuring recursively. Don't know all
;;the cases yet, but we'll need to be able to destructure vectors and maps into
;;corresponding lambda lists.
(defun parse-args (args)
(make-arg-parse :lambda-list (mapcar (lambda (x)
(if (and (symbolp x)
(string-equal (symbol-name x) "&"))
'&rest
x)) (vector-to-list args)))))
;;Compile a clojure fn special form into a common lisp lambda
(EVAL-WHEN (:compile-toplevel :load-toplevel :execute)
(defgeneric fndef->sexp (fd))
;; (defmethod fndef->sexp ((fd fn-def))
;; (with-slots (args body) fd
;; (with-slots (lambda-list outer-let) (parse-args args)
;; (let ((interior (if outer-let `(let* ,outer-let ,body)
;; body)))
;; `(lambda ,lambda-list ,interior)))))
(defmethod fndef->sexp ((fd fn-def))
(with-slots (args body name) fd
(with-slots (lambda-list outer-let) (parse-args args)
(let ((interior (if outer-let `(let* ,outer-let ,body)
body)))
`(named-fn ,name ,lambda-list ,interior)))))
;;parse a list of function definitions into an n-lambda dispatching function.
;; (defmethod fndef->sexp ((fd common-lisp:cons))
;; (if (= (length fd) 1) (fndef->sexp (common-lisp:first fd)) ;simple case
;; ;;case with multiple function definitions.
;; (progn (pprint fd)
;; `(common-utils:lambda* ,@(mapcar (lambda (body)
;; (common-lisp:rest (fndef->sexp body))) fd)))))
;; (defmethod fndef->sexp ((fd common-lisp:cons))
;; (if (= (length fd) 1) (fndef->sexp (common-lisp:first fd)) ;simple case
;; ;;case with multiple function definitions.
;; (let ((name (fn-def-name (common-lisp:first fd))))
;; `(common-utils:lambda* ,@(mapcar (lambda (body)
;; (common-lisp:rest (common-lisp:rest (fndef->sexp body)))) fd)))))
(defmethod fndef->sexp ((fd common-lisp:cons))
(if (common-lisp:= (length fd) 1) (fndef->sexp (common-lisp:first fd)) ;simple case
;;case with multiple function definitions.
(let ((name (fn-def-name (common-lisp:first fd))))
`(common-utils:named-fn* ,name
,@(mapcar (lambda (body)
(common-lisp:rest (common-lisp:rest (fndef->sexp body)))) fd)))))
)
;;Clojure's anonymous function special form.
;;Todo: support destructuring in the args.
;; (defmacro fn (&rest specs)
;; (pprint specs)
;; (let* ((res
;; (cond ((symbolp (common-lisp:first specs))
;; (fndef->sexp (fn* (cons (first specs) (list (rest specs))))))
;; ((vector-form? (common-lisp:first specs))
;; (fndef->sexp (fn* specs)))
;; ;;TODO get rid of this eval....
;; (t
;; (fndef->sexp (apply #'fn* specs))))))
;; `(,@(clclojure.eval::custom-eval-bindings (sb-cltl2::macroexpand-all res) nil))))
(defmacro fn (&rest specs)
(let* ((hd (common-lisp:first specs))
(name (if (symbolp hd) hd (symb (symbol-name (gensym "fn_")))))
(specs (if (symbolp hd) (common-lisp:rest specs) specs))
(res (if (vector-form? (common-lisp:first specs))
(fndef->sexp (fn* name specs))
;;TODO get rid of this eval....
(let ((bodies (apply #'fn* (common-lisp:cons name specs))))
(fndef->sexp bodies)))))
`(,@(clclojure.eval::custom-eval-bindings (sb-cltl2::macroexpand-all res) nil))))
;;def
;;===
;;Experimental. Not sure of how to approach this guy.
;;for now, default to everything being public / exported.
;;that should be toggled via metadata in real implementation.
(eval-when (:compile-toplevel :load-toplevel :execute)
(defmacro def (var &rest init-form)
`(progn (defparameter ,var ,@init-form)
(with-meta (quote ,var) '((SYMBOL . T) (DOC . "none")))
(when (functionp (symbol-value (quote ,var)))
(setf (symbol-function (quote ,var)) (symbol-value (quote ,var))))
(export ',var)
(quote ,var)
)))
;;A CHEAP implementation of defn, replace this...
(defmacro defn (name args &rest body)
`(def ,name (fn ,name ,args ,@body)))
;;Clojure Transformations (PENDING)
;;================================
;;we need some basic transformations....
;;I guess we can write a simple clojure reader by swapping some symbols around..
;;maybe even use read macros...
;;Clojure -- Common Lisp
;;@x (deref x) -> used in quasiquoted expression, splice-collection ,@
;;~x (insert x)? used to escape a quasiquote -> ,x
;;one simple transform is to scan the clojure expression, and change the following:
;;~ -> ,
;;~@ -> ,@
;;@x -> (deref x) ;;need to implement deref
;;[x] -> (pvector x) ;;more or less implemented
;;{x y} -> (hash-map x y)
;;(Blah. x) -> (make-Blah x) ;;CLOS constructor
;;(. obj method args) -> ((slot-value obj method) args)
;;(. obj (method args)) -> ((slot-value obj method) args)
;;(. obj method) -> ((slot-value obj method))
;;(.method obj args) -> (slot-value obj method) ;;CLOS accessor
;;(def x val) -> (defparameter x val)
;;(let [x y] expr) -> (let* ((x y)) expr)
;;(some-namespace-alias/the-function x) -> (some-package-alias::the-function x)
;;(ns blah) -> (defpackage blah)
;;#"some-regex" -> (make-regex "some-regex") ;;need to use cl-ppre probably...
(defmacro doc (v) `(pprint (rest (common-lisp:assoc 'DOC (meta ,v)))))
(defmacro do (&rest exprs)
`(progn ,@exprs))
;;Meta Data
;;=========
;;I think we want to use persistent maps for meta data, as clojure does.
;;I want to get the stubs in place, and am using property lists with a 'meta
;;entry pointing at an assoc list for now.
;;These should be pulled out into a protocol.
(defmacro symbol-meta (symb) `(common-lisp:get (quote ,symb) 'meta))
(defmacro with-symbol-meta (symb m) `(setf (common-lisp:get (quote ,symb) 'meta) ,m))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun meta (obj) (-meta obj))
(defun with-meta (obj m) (-with-meta obj m)))
;;One thing about metadata, and how it differs from property lists:
;;You can call meta on datastructures, or objects, and get a map back.
;;Symbols can have meta called on them with (meta #'the-symbol), which
;;uses sharp-quote to get the symbol, vs the symbol-name.
;;Clojure is a lisp-1, so we need to ensure that everything, even
;;functions, gets bound into the a single namespace.
;;another way to do this is to have clojure-specific symbols be actual
;;clos objects, which have meta data fields automatically. Then we
;;lose out on all the built in goodies from common lisp though.
;;Clojure Core (PENDING)
;;======================
;;hacky way to accomodate both forms...
;;we know we're in clojure if the args are vector
(defmacro deftype (&rest args)
(if (vector? (common-lisp:nth 1 args))
`(clojure-deftype ,@args)
`(common-lisp::deftype ,@args)))
;;reify is interesting.
;;we generate an instance of an anonymous class,
;;ala deftype, with protocol implementations.
;;TODO: look at the consequences of having bunches of
;;anonymous classes laying around, say evaluating
;;reify several times...Should we garbage collect this?
;;Or does that cut into dynamicity?
(defmacro reify (&rest implementations)
(let [classname (gentemp "REIFY")
ctor (gensym "CONSTRUCTOR")]
`(let ((,ctor (clojure-deftype ,classname ,'[] ,@implementations)))
(funcall ,ctor))))
;;;;;;;;;;;;;;;;;;;;;;;;;;; core protocols ;;;;;;;;;;;;;
;;Need to get back to this guy...multiple arity is not yet implemented...
(eval-when (:compile-toplevel :load-toplevel :execute)
(defprotocol IFn
(-invoke
[this]
[this a]
[this a b]
[this a b c]
[this a b c d]
[this a b c d e]
[this a b c d e f]
[this a b c d e f g]
[this a b c d e f g h]
[this a b c d e f g h i]
[this a b c d e f g h i j]
[this a b c d e f g h i j k]
[this a b c d e f g h i j k l]
[this a b c d e f g h i j k l m]
[this a b c d e f g h i j k l m n]
[this a b c d e f g h i j k l m n o]
[this a b c d e f g h i j k l m n o p]
[this a b c d e f g h i j k l m n o p q]
[this a b c d e f g h i j k l m n o p q s]
[this a b c d e f g h i j k l m n o p q s t]
[this a b c d e f g h i j k l m n o p q s t rest]))
;;These work
(defprotocol ICounted
(-count [coll] "constant time count"))
(defprotocol IEmptyableCollection
(-empty [coll]))
(defprotocol ICollection
(-conj [coll o]))
(defprotocol IOrdinal
(-index [coll]))
;;this will break. current implementation of defprotocol doesn't allow for
;;multiple arity functions like this. Need to handle variadic functions...
(defprotocol IIndexed
(-nth [coll n] [coll n not-found]))
(defprotocol ASeq)
(defprotocol ISeq
(-first [coll])
(-rest [coll]))
(defprotocol INext
(-next [coll]))
(defprotocol ILookup
(-lookup [o k] [o k not-found]))
(defprotocol IAssociative
(-contains-key? [coll k])
(-entry-at [coll k])
(-assoc [coll k v]))
(defprotocol IMap
(-assoc-ex [coll k v])
(-dissoc [coll k]))
(defprotocol IMapEntry
(-key [coll])
(-val [coll]))
(defprotocol ISet
(-disjoin [coll v]))
(defprotocol IStack
(-peek [coll])
(-pop [coll]))
(defprotocol IVector
(-assoc-n [coll n val]))
(defprotocol IDeref
(-deref [o]))
(defprotocol IDerefWithTimeout
(-deref-with-timeout [o msec timeout-val]))
(defprotocol IMeta
(-meta [o]))
(defprotocol IWithMeta
(-with-meta [o meta]))
(defprotocol IReduce
(-reduce [coll f]
[coll f start]))
(defprotocol IKVReduce
(-kv-reduce [coll f init]))
(defprotocol IEquiv
(-equiv [o other]))
(defprotocol IHash
(-hash [o]))
(defprotocol ISeqable
(-seq [o]))
(defprotocol ISequential
"Marker interface indicating a persistent collection of sequential items")
(defprotocol IList
"Marker interface indicating a persistent list")
(defprotocol IRecord
"Marker interface indicating a record object")
(defprotocol IReversible
(-rseq [coll]))
(defprotocol ISorted
(-sorted-seq [coll ascending?])
(-sorted-seq-from [coll k ascending?])
(-entry-key [coll entry])
(-comparator [coll]))
;; (defprotocol ^:deprecated IPrintable
;; "Do not use this. It is kept for backwards compatibility with existing
;; user code that depends on it, but it has been superceded by IPrintWithWriter
;; User code that depends on this should be changed to use -pr-writer instead."
;; (-pr-seq [o opts]))
(defprotocol IWriter
(-write [writer s])
(-flush [writer]))
(defprotocol IPrintWithWriter
"The old IPrintable protocol's implementation consisted of building a giant
list of strings to concatenate. This involved lots of concat calls,
intermediate vectors, and lazy-seqs, and was very slow in some older JS
engines. IPrintWithWriter implements printing via the IWriter protocol, so it
be implemented efficiently in terms of e.g. a StringBuffer append."
(-pr-writer [o writer opts]))
(defprotocol IPending
(-realized? [d]))
(defprotocol IWatchable
(-notify-watches [this oldval newval])
(-add-watch [this key f])
(-remove-watch [this key]))
(defprotocol IEditableCollection
(-as-transient [coll]))
(defprotocol ITransientCollection
(-conj! [tcoll val])
(-persistent! [tcoll]))
(defprotocol ITransientAssociative
(-assoc! [tcoll key val]))
(defprotocol ITransientMap
(-dissoc! [tcoll key]))
(defprotocol ITransientVector
(-assoc-n! [tcoll n val])
(-pop! [tcoll]))
(defprotocol ITransientSet
(-disjoin! [tcoll v]))
(defprotocol IComparable
(-compare [x y]))
(defprotocol IChunk
(-drop-first [coll]))
(defprotocol IChunkedSeq
(-chunked-first [coll])
(-chunked-rest [coll]))
(defprotocol IChunkedNext
(-chunked-next [coll]))
(defprotocol INamed
(-name [thing]))
)
;;Extending types to native structures and clojure literals:
;;==========================================================
(eval-when (:compile-toplevel :load-toplevel :execute)
(extend-type
null
ICounted
(-count [c] 0)
IEmptyableCollection
(-empty [c] nil)
ICollection
(-conj [coll itm] (common-lisp:cons itm nil))
IStack
(-peek [coll] nil)
(-pop [coll] nil)
ISeqable
(-seq [coll] nil)
IHash
(-hash [o] (sxhash nil))
IEquiv
(-equiv [o other] (error 'not-implemented))
ISeq
(-first [o] nil)
(-rest [o] nil)
IReversible
(-rseq [coll] nil))
;;We got a ton of goodies from
;;sb-sequences namespace to leverage here.
;;good opportunity for iterator-seq...
(extend-type
sequence
ICounted
(-count [c] (common-lisp:length c))
;; IEmptyableCollection
(-empty [c] (sb-sequence:make-sequence-like c 0))
;; ICollection
;; (-conj [coll itm] (cons itm nil))
IStack
(-peek [coll] (elt coll 0))
(-pop [coll] (error 'not-implemented))
ISeqable
(-seq [coll] (error 'not-implemented))
IHash
(-hash [o] (sxhash o))
;; IEquiv
;; (-equiv [o other] (error 'not-implemented))
ISeq
(-first [o] (elt o 0))
;;TODO pull this over...
;;Probably identical to array-seqs
(-rest [o] (error 'not-implemented))
IReversible
(-rseq [coll] (reverse coll)))
(extend-type
clclojure.pvector::pvec
ICounted
(-count [c] (vector-count c))
IIndexed
(-nth [coll n] (nth-vec coll n))
(-nth [coll n not-found] (nth-vec coll n))
IEmptyableCollection
(-empty [c] [])
ICollection
(-conj [coll itm] (vector-conj coll itm))
IVector
(-assoc-n [coll n val] (vector-assoc coll n val))
IStack
(-peek [coll]
(when (not (zerop (-count coll) )) (nth-vec coll 0)))
(-pop [coll] (subvec coll 1))
ISeqable
(-seq [coll] (vector-to-list coll ))
IHash
(-hash [o] (error 'not-implemented))
IMapEntry
(-key [coll] (-nth coll 0))
(-val [coll] (-nth coll 1))
IEquiv
(-equiv [o other] (error 'not-implemented))
IKVReduce
(-kv-reduce [coll f init] (error 'not-implemented))
IReversible
(-rseq [coll] (error 'not-implemented))
IChunk
(-drop-first [coll] (error 'not-implemented))
IChunkedSeq
(-chunked-first [coll] (error 'not-implemented))
(-chunked-rest [coll] (error 'not-implemented))
IChunkedNext
(-chunked-next [coll] (error 'not-implemented))))
(eval-when (:compile-toplevel :load-toplevel :execute)
(extend-type symbol
IMeta
(-meta [obj] (symbol-meta obj))
IWithMeta
(-with-meta [obj m] (with-symbol-meta obj m) obj)
IEquiv
;;dirty implementation....
;;We need to unify qualified and unqualified symbols..
;;in clojure, symbol equality is a bit more complex
;;since they're equiv iff unqualified.
;;unless we hack the reader to reader qualified
;;symbols as unqual, the preponderance of clojure
;;symbol comparisons will not be strict, so
;;we end up with a lot of unqualified symbols.
;;This is just to paper over the bootstrapping
;;process....
(-equiv [l r]
(or (eq l r)
(when (not (or (keywordp l) (keywordp r)))
(common-lisp:= (sxhash l) (sxhash r)))
))
INamed
(-name [this] (symbol-name this))
)
;;not applicable.
;; (extend-type keyword
;; IEquiv
;; ;;dirty implementation....
;; ;;We need to unify qualified and unqualified symbols..
;; ;;in clojure, symbol equality is a bit more complex
;; ;;since they're equiv iff unqualified.
;; ;;unless we hack the reader to reader qualified
;; ;;symbols as unqual, the preponderance of clojure
;; ;;symbol comparisons will not be strict, so
;; ;;we end up with a lot of unqualified symbols.
;; ;;This is just to paper over the bootstrapping
;; ;;process....
;; (-equiv [l r]
;; (or (eq l r)
;; ;;I don't even know if this is possible...
;; ;;I think keywords are always interned
;; ;;in the keyword package.
;; ;; (when (keywordp r)
;; ;; (common-lisp:= (sxhash l) (sxhash r)))
;; ))
;; IHash
;; (-hash [k] (hash-code k))
;; )
;;subvector impls...
(extend-type
clclojure.pvector::subvector
ICounted
(-count [c] (vector-count c))
IEmptyableCollection
(-empty [c] [])
ICollection
(-conj [coll itm] (vector-conj coll itm))
IVector
(-assoc-n [coll n val] (vector-assoc coll n val))
IStack
(-peek [coll]
(when (not (zerop (-count coll) )) (nth-vec coll 0)))
(-pop [coll] (subvec coll 1))
ISeqable
(-seq [coll] (vector-to-list coll)) ;poorly implemented. should be arrayseq
IHash
(-hash [o] (error 'not-implemented))
IMapEntry
(-key [coll] (-nth coll 0))
(-val [coll] (-nth coll 1))
IEquiv
(-equiv [o other] (error 'not-implemented))
IKVReduce
(-kv-reduce [coll f init] (error 'not-implemented))
IReversible
(-rseq [coll] (error 'not-implemented))
IChunk
(-drop-first [coll] (error 'not-implemented))
IChunkedSeq
(-chunked-first [coll] (error 'not-implemented))
(-chunked-rest [coll] (error 'not-implemented))
IChunkedNext
(-chunked-next [coll] (error 'not-implemented))
))
(eval-when (:compile-toplevel :load-toplevel :execute)
;;list operations.
(extend-type
common-lisp:cons
ICounted
(-count [c] (length c))
IEmptyableCollection
(-empty [c] '())
ICollection
(-conj [coll itm] (common-lisp:cons itm coll))
IStack
(-peek [coll] (common-lisp:first coll))
(-pop [coll] (common-lisp:rest coll))
ISeqable
(-seq [coll] coll)
IHash
(-hash [o] (sxhash o))
IEquiv
(-equiv [o other] (error 'not-implemented))
IMapEntry
(-key [coll] (common-lisp:first coll))
(-val [coll] (common-lisp:second coll))
ISeq
(-first [coll] (common-lisp:first coll))
(-rest [coll] (cdr coll))
)
(extend-type
sequences::lazyseq
ICounted
(-count [c] (sequences:seq-count c))
IEmptyableCollection
(-empty [c] '())
ICollection
(-conj [coll itm] (sequences::cons itm coll))
IStack
(-peek [coll] (sequences::first coll))
(-pop [coll] (sequences::rest coll))
ISeqable
(-seq [coll] (sequences::seq coll))
IHash
(-hash [o] (sxhash o)) ;;poorly implemented...
IEquiv
(-equiv [o other] (error 'not-implemented))
IMapEntry
(-key [coll] (sequences::first coll))
(-val [coll] (sequences::rest coll))
ISeq
(-first [coll] (sequences::first coll))
(-rest [coll] (sequences::rest coll))
)
(extend-type
sequences::funcseq
ICounted
(-count [c] (sequences:seq-count c))
IEmptyableCollection
(-empty [c] '())
ICollection
(-conj [coll itm] (sequences::cons itm coll))
IStack
(-peek [coll] (sequences::first coll))
(-pop [coll] (sequences::rest coll))
ISeqable
(-seq [coll] (sequences::seq coll))
IHash
(-hash [o] (sxhash o)) ;;poorly implemented...
IEquiv
(-equiv [o other] (error 'not-implemented))
IMapEntry
(-key [coll] (sequences::first coll))
(-val [coll] (sequences::rest coll))
ISeq
(-first [coll] (sequences::first coll))
(-rest [coll] (sequences::rest coll))
)
(extend-type
clclojure.cowmap::cowmap
ICounted
(-count [c] (map-count c))
IEmptyableCollection
(-empty [c] {})
ICollection
(-conj [coll itm] (map-assoc coll (common-lisp:first itm) (second itm)))
ISeqable
(-seq [coll] (map-seq coll))
ILookup
(-lookup [o k] (map-get o k))
(-lookup [o k not-found]
(or (map-get o k) not-found))
IAssociative
(-contains-key? [coll k] (map-contains? coll k))
(-entry-at [coll k] (map-entry-at coll k))
(-assoc [coll k v] (map-assoc coll k v))
IMap
(-assoc-ex [coll k v] (error 'not-implemented)) ;;apparently vestigial
(-dissoc [coll k] (map-dissoc coll k))
IHash
(-hash [o] (error 'not-implemented))
IEquiv
(-equiv [o other] (error 'not-implemented))
IKVReduce
(-kv-reduce [coll f init] (error 'not-implemented)))
(extend-type number
IEquiv
(-equiv [l r] (when (numberp r) (common-lisp:= l r)))
IHash
(-hash [n] (hash-code n)))
(extend-type string
INamed (-name [x] x))
)
;; IChunk
;; (-drop-first [coll] (error 'not-implemented))
;; IChunkedSeq
;; (-chunked-first [coll] (error 'not-implemented))
;; (-chunked-rest [coll] (error 'not-implemented))
;; IChunkedNext
;; (-chunked-next [coll] (error 'not-implemented))
;;friendly map printing
(defmethod print-object ((obj hash-table) stream)
(common-utils::print-map obj stream))
;;map printing compatibility
(defmethod print-object ((obj clclojure.cowmap::cowmap) stream)
(common-utils::print-map (cowmap-table obj) stream))
;;Core Lib
;;========
(eval-when (:compile-toplevel :load-toplevel :execute)
(defn seq [coll] (-seq coll))
(defn vec [coll]
(if (vector? coll) coll
(sequences:apply #'persistent-vector (seq coll))))
(defn vector [& xs]
(sequences:apply #'persistent-vector (seq xs)))
(defn hash-map [& xs]
(sequences:apply #'persistent-map (seq xs))
)
(defn identical? [l r]
(common-lisp:eq l r))
(defn nil? [x]
(identical? x nil))
;;need to implement arrayseq...
;;These are lame but easy, we really want to
;;get chunked-first and friends up and running.
;;Also want to bake in typecases for quick
;;dispatch...
(defn first [coll] (-first (seq coll)))
(defn rest [coll] (-rest (seq coll)))
;;TBD fix this for an actual -next implementation.
;; "Returns a seq of the items after the first. Calls seq on its
;; argument. If there are no more items, returns nil"
;;this isn't great....
(defn implements? [p obj]
(satisfies? p obj))
;;tbd : get metadata reader working...
;^seq
(defn next
[coll]
(when-not (nil? coll)
(if (implements? INext coll)
(-next coll)
(seq (rest coll)))))
(defn nnext
[coll]
(next (next coll)))
(defn second [coll] (first (rest coll)))
(defn ffirst [coll] (first (first coll)))
;;Inaccurate...
(defn fnext [coll] (first (rest coll)))
(defn get
([m k] (-lookup m k))
([m k not-found] (-lookup m k not-found)))
(def odd? #'common-utils:odd?)
(def even? #'common-utils:even?)
(def zero? #'common-utils:zero?)
(defn inc [x] (1+ x))
(defn dec [x] (1- x))
(defn =
([x] true)
([x y]
(if (and (numberp x) (numberp y))
(common-lisp:= x y)
(-equiv x y)))
([x y & more]
(if (-equiv x y)
(if (next more)
(recur y (first more) (next more))
(- y (first more)))
nil)))
(defn key [e] (-key e))
(defn val [e] (-val e))
(defn name [x] (-name x))
)
(defmacro when-let (binding &rest body)
(let [binding (seq binding)
arg (-first binding)
expr (-first (-rest binding))
tst (gensym "tst")]
(clclojure.eval::recover-literals
`(let ,(vector tst (second binding))
(when ,tst
(let ,(vector arg tst)
,@body))))))
(defmacro if-let (binding body &rest false-body)
(let [binding (seq binding)
arg (-first binding)
expr (-first (-rest binding))
tst (gensym "tst")]
(clclojure.eval::recover-literals
`(let ,(vector tst (second binding))
(if ,tst
(let ,(vector arg tst)
,body)
,@false-body)))))
(defn throw [e]
(error e))
;;try-catch-finally...
(defn ex-info
([msg map]
(make-instance 'exception-info :data map :cause msg :message msg))
([msg map cause]
(make-instance 'exception-info :data map :cause cause :message msg)))
(defn ex-data [e]
(common-utils::exception-info-data e))
(defn ex-cause [e]
(common-utils::exception-info-cause e))
(defn ex-message [e]
(common-utils::exception-info-message e))
;;using cl macros for now to get behavior in place..
;; "defs name to have the root value of the expr iff the named var has no root value,
;; else expr is unevaluated"