/
clojure-base.scm
1219 lines (1087 loc) · 38.6 KB
/
clojure-base.scm
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
;;; Copyright 2019 by Christian Jaeger <ch@christianjaeger.ch>
;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License (GPL) as published
;;; by the Free Software Foundation, either version 2 of the License, or
;;; (at your option) any later version.
(require easy
(cj-source-util-2 form-unquote-splicing?)
(cj-gambit-sys maybe-procedure-name))
(export (macro use-clojure-base))
(defmacro (use-clojure-base)
`(##namespace ("clojure#" defn fn defmacro quasiquote macroexpand macroexpand-1
false true nil nil? = not if if-not when let if-let cond
false? true? keyword?
lazy-seq
seq sequence seq? chunked-seq? first rest next)))
(defmacro (scheme . body)
`(##let ()
(##namespace (""))
,@body))
;; This uses unsafe ops, as length is supposed to be used and this
;; code only used with correct number of argumetns in VS
(def (scheme:@bind-dispatch sbinds body VS)
(let rec ((sbinds sbinds))
(cond ((null? sbinds)
`(##begin ,@body))
((pair? sbinds)
(let-pair ((V sbinds) sbinds)
`(##let ((,V (##car ,VS))
(,VS (##cdr ,VS)))
,(rec sbinds))))
(else
;; rest argument
`(##let ((,sbinds ,VS))
,@body)))))
(TEST
> (scheme:@bind-dispatch '() '(body0 body1) 'GEN:VS)
(##begin body0 body1)
> (scheme:@bind-dispatch '(a) '(body0 body1) 'GEN:VS)
(##let ((a (##car GEN:VS)) (GEN:VS (##cdr GEN:VS))) (##begin body0 body1))
;; ^ could optimize away the last ##cdr call; although compiler
;; 'should' rather do that.
> (scheme:@bind-dispatch '(a b) '(body0 body1) 'GEN:VS)
(##let ((a (##car GEN:VS)) (GEN:VS (##cdr GEN:VS)))
(##let ((b (##car GEN:VS)) (GEN:VS (##cdr GEN:VS))) (##begin body0 body1)))
> (scheme:@bind-dispatch '(a b . c) '(body0 body1) 'GEN:VS)
(##let ((a (##car GEN:VS)) (GEN:VS (##cdr GEN:VS)))
(##let ((b (##car GEN:VS)) (GEN:VS (##cdr GEN:VS)))
(##let ((c GEN:VS)) body0 body1))))
(def (clojure->scheme-args args)
(assert* vector? args
(lambda (args*)
(let (rl (vector.list-reverse args*))
(if (and (pair? rl)
(let (rl* (cdr rl))
(and (pair? rl*)
(eq? (source-code (car rl*)) '&))))
(append-reverse (cddr rl) (car rl))
(reverse rl))))))
(def (clojure:fixbody body)
(if (null? body)
`((void))
body))
(def (clojure:arity-error len)
(error "missing definition for arity:" len))
(defclass (clojure-definition-case [fixnum? arity]
binds-scheme
binds-clojure
body-clojure)
(defmethod (body-scheme _ VS)
(scheme:@bind-dispatch binds-scheme
(clojure:fixbody body-clojure)
VS)))
(defclass (clojure-definition [(source-of symbol?) name]
[(list-of (source-of string?)) docstrings]
[false? attributes] ;; currently unused
)
(defclass (clojure-definition-singlecase binds
[list? body])
(defmethod (scheme-code s DEF name-prefixer lead-in-forms)
`(,DEF (,(name-prefixer name) ,@(clojure->scheme-args binds))
,@lead-in-forms ;; possible namespacing sh; could this be after docstrings?
,@docstrings
,@(clojure:fixbody body))))
(defclass (clojure-definition-multicase [(list-of clojure-definition-case?) real-cases]
[(list-of clojure-definition-case?) else-cases])
(defmethod (scheme-code s DEF name-prefixer lead-in-forms)
(with-gensyms
(VS LEN)
`(,DEF (,(name-prefixer name) . ,VS)
,@lead-in-forms ;; ditto
,@docstrings
(##let
((,LEN (length ,VS)))
(##case
,LEN
,@(map (lambda (c)
`((,(.arity c)) ,(.body-scheme c VS)))
real-cases)
(else
,(cond ((null? else-cases)
`(clojure:arity-error ,LEN))
(((list-of-length 1) else-cases)
(let (s (car else-cases))
`(##if (> ,LEN ,(.arity s))
,(.body-scheme s VS)
(clojure:arity-error ,LEN))))
(else
(raise-source-error
(second else-cases)
"Can't have more than 1 variadic overload")))))))))))
(def (clojure:definition-parse stx args)
(let*-values
(((name args) (if (pair? args)
(values (car args) (cdr args))
(raise-source-error stx "missing definition name")))
((docstrings args) (if (and (pair? args)
(string? (source-code (car args))))
(values (list (car args)) (cdr args))
(values (list) args)))
;; ((maybe-attributes args)
;; ...)
)
(assert* symbol? name)
(if (pair? args)
(let-pair
((a args*) args)
(mcase a
(vector?
(clojure-definition-singlecase name
docstrings
#f
a
args*))
(pair?
;; multiple-match form
;; (M (list 3 4) ((a b) b)) gah not supported yet.
;; and not optimized anyway.
(let (cases ;; (list-of clojure-definition-case)
(map (lambda (c)
(assert*
pair? c
(lambda (c*)
(let-pair
((binds body) c*)
(let (sbinds (clojure->scheme-args binds))
(clojure-definition-case (improper-length sbinds)
sbinds
binds
body))))))
args))
(let ((real-cases (filter (comp (complement negative?) .arity)
cases))
(else-cases (filter (comp negative? .arity)
cases)))
(let ((duplicate-cases
(=> (sort real-cases (on .arity <))
(group-by (on .arity =))
(.filter (=>* length ((C > _ 1)))))))
(if
(pair? duplicate-cases)
(raise-source-error
(.binds-clojure (second (first duplicate-cases)))
"Can't have 2 overloads with same arity")
(if-let (bad (and (pair? else-cases)
(let* ((toolargelen (- (.arity (first else-cases))))
(bad (filter (lambda (c)
(>= (.arity c) toolargelen))
real-cases)))
(and (pair? bad)
(first bad)))))
(raise-source-error
(.binds-clojure bad)
"Can't have fixed arity function with more params than variadic function")
(clojure-definition-multicase name
docstrings
#f
real-cases
else-cases)))))))))
(raise-source-error stx "missing function arguments/body"))
))
(defmacro (clojure#defn . args)
(=> (clojure:definition-parse stx args)
(.scheme-code `def identity '())))
(def (in-namespace namespace sym)
(assert*
symbol? sym
(lambda (sym*)
(let* ((str (symbol.string sym*))
(prefix-it
(lambda ()
(assert*
string? namespace
(lambda (ns)
(=> (string-append ns str)
string.symbol
(possibly-sourcify sym)))))))
(if-let (i (string-find-char str #\#))
(let (len (string.length str))
(if (= i (dec len))
(prefix-it)
sym))
(prefix-it))))))
(TEST
> (in-namespace "foo#" 'bar)
foo#bar
> (in-namespace "foo#" 'bar#)
foo#bar#
> (in-namespace "foo#" 'bar#baz)
bar#baz)
(defmacro (clojure#defmacro . args)
(=> (clojure:definition-parse stx args)
(.scheme-code `define-macro*
;; XX will need a way to get the namespace of the
;; current module! (Or change define-macro*
;; implementation); for now HACK in just clojure#
;; as a constant.
(C in-namespace "clojure#" _)
;; Do not (use-clojure), breaks argument bindings:
'())))
;; Clojure's macroexpand is a function, not a macro!
(def (clojure#macroexpand e)
(cj-desourcify (macro-star-expand e)))
(def (clojure#macroexpand-1 e)
(cj-desourcify (macro-star-expand-1 e)))
(TEST
> (use-clojure-base)
> (defn t "fun"
([a b] (list 2 a b))
([c] (list 1 c))
([x y z] (list 3 x z)))
> (t 10)
(1 10)
> (%try (t))
(exception text: "missing definition for arity: 0\n")
> (%try (t 3 4 5 6))
(exception text: "missing definition for arity: 4\n")
> (t 3 4)
(2 3 4)
> (t 3 4 5)
(3 3 5)
> (defn t
[a b & r] (list a b r))
> (%try (t 1))
(exception text: "Wrong number of arguments passed to procedure\n(t 1)\n")
> (t 2 3)
(2 3 ())
> (t 2 3 4 5)
(2 3 (4 5))
> (with-exception-catcher
source-error-message
(& (eval (quote-source (defn t
([a b & r] (list 'first a b r))
([a b] (list 'second a b))
([a b c & r] (list 'third a b c)))))))
"Can't have more than 1 variadic overload"
> (with-exception-catcher
source-error-message
(& (eval (quote-source (defn t
([a b & r] (list 'first a b r))
([a b] (list 'second a b))
([c d] (list 'third c d)))))))
"Can't have 2 overloads with same arity"
> (with-exception-catcher
source-error-message
(& (eval '(defn t
([a b & r] (list 'first a b r))
([a b] (list 'second a b))
([a b c] (list 'third a b c))))))
"Can't have fixed arity function with more params than variadic function"
> (defn t
([a b & r] (list 'first a b r))
([a b] (list 'second a b)))
> (t 3 4)
(second 3 4)
> (t 3 4 5)
(first 3 4 (5))
> (defmacro t-fixx [x] x)
> (##namespace ("clojure#" t-fixx)) ;; *Sigh* is this a hack now.
> (t-fixx 10)
10
> (defmacro t-fixx
([x] x)
([x y] (list '* x y)))
> (t-fixx 10)
10
> (t-fixx 10 20)
200)
;; Hack to get ~foo and foo~ syntax since we don't have custom reader
;; yet.
(def (clojure:quasiquote-symbol-kind sym)
-> (values-of symbol? any?) ;; any = parsed thing, could be a constant
(let* ((str (symbol.string (source-code sym)))
(len (string.length str)))
(if (< len 2)
(values 'none sym)
(if (eq? (string.ref str 0) #\~)
(let (fixup (lambda (str*)
(or (string->number str*)
(string->symbol str*))))
(if (and (>= len 3)
(eq? (string.ref str 1) #\@))
(values 'unquote-splicing
(fixup (substring str 2 len)))
(values 'unquote
(fixup (substring str 1 len)))))
(if (eq? (string.ref str (dec len)) #\#)
(values 'gensym
(string->symbol (substring str 0 (dec len))))
(values 'none sym))))))
(def (clojure:quasiquote:append a b)
"For syntax, need eager list, but a is any kind of Clojure
sequence-compatible datum, thus pass through |sequence| and force."
(append (.list (clojure#sequence a))
;; b should already be an eager list since built via cons
;; from the code emitted by quasiquote.
b))
(defmacro (clojure#quasiquote e)
"Supporting ~foo (etc.?) via HACK; also still supporting Scheme's
unquote and unquote-splicing at the same time"
(let (gensyms (table))
(let expand ((e e))
(mcase e
(pair?
(let-pair
((a e*) (source-code e))
(let (a* (source-code a))
(case a*
((~ ~@)
(let (e* (source-code e*))
;; ( ^ shouldn't have any source anyway)
(if (pair? e*)
(let-pair
((b e**) e*)
`(,(if (eq? a* '~)
`cons
`clojure:quasiquote:append)
,b
,(expand e**)))
;; Clojure fails this at read time already,
;; but we're "hacked"...:
(raise-source-error a
($ "missing item after $a*")))))
((unquote)
(let (e* (source-code e*))
(if (pair? e*)
(let-pair
((b e**) e*)
(if (null? e**)
b
`(cons ',a
,(expand e*))))
`(cons ',a
,(expand e*)))))
(else
(let (other
(& (mcase a
(form-unquote-splicing?
(let (b (cadr (source-code a)))
(if (null? e*) ;; optim
b
`(append ,b
,(expand e*)))))
(else
`(cons ,(expand a)
,(expand e*))))))
(if (symbol? a*)
(letv ((kind thing) (clojure:quasiquote-symbol-kind a))
(case kind
((unquote-splicing)
`(clojure:quasiquote:append ,thing
,(expand e*)))
(else
(other))))
(other))))))))
(null?
`'())
(vector?
(possibly-sourcify
(=> (source-code e)
vector->list
expand
;; Now we've got code that generates a list; leave
;; it at that, and turn that to a vector at
;; runtime. Only reasonable way to handle ~@ within
;; [ ]. (And yes, Gambit native does actually
;; support that, too!)
((lambda (listgencode)
`(list->vector ,listgencode))))
e))
(symbol?
;; XXX forgot about quote. Don't recurse into
;; quote. (And yes nested quasiquote is still pending,
;; too.)
(letv ((kind thing) (clojure:quasiquote-symbol-kind e))
(xcase kind
((unquote)
thing)
((unquote-splicing)
(raise-source-error
e "invalid place for ~@ (or BUG?)"))
((gensym)
`',(or (table-ref gensyms thing #f)
(let (sym* (gensym thing))
(table-set! gensyms thing sym*)
sym*)))
((none)
`',e))))
((either string? number? boolean?) ;; self-quoting
e)
(else
;; homogenous vectors? What else?
(WARN "unfinished?")
e)))))
(TEST
> (use-clojure-base)
> (expansion#clojure#quasiquote (a 1))
(cons 'a (cons 1 '()))
> (expansion#clojure#quasiquote (a ~1))
(cons 'a (cons 1 '()))
> (clojure#quasiquote (a ~1))
(a 1)
> (clojure#quasiquote (a ~(+ 1 2)))
(a 3)
> (expansion#clojure#quasiquote (a ~ 1))
(cons 'a (cons 1 '()))
> (expansion#clojure#quasiquote (a b (c) 1))
(cons 'a (cons 'b (cons (cons 'c '()) (cons 1 '()))))
> (eval #)
(a b (c) 1)
> (expansion#clojure#quasiquote (a b (~ c) 1))
(cons 'a (cons 'b (cons (cons c '()) (cons 1 '()))))
> (def c 13)
> (clojure#quasiquote (a b (~ c) 1))
(a b (13) 1)
> `(a b (~c d) 1)
(a b (13 d) 1)
;; ^ all of these: XX: pending: namespacing, i.e.
;; (do4clojure.core/a do4clojure.core/b (13 do4clojure.core/d) 1)
> (def cs '(13 14))
> `(a b (~@ cs d) 1)
(a b (13 14 d) 1)
> `(a b (~@cs d) 1)
(a b (13 14 d) 1)
> (def cs '[13 14])
> `(a b (~@ cs d) 1)
(a b (13 14 d) 1)
> `(a b (~@cs d) 1)
(a b (13 14 d) 1)
> `(a b (~@'[13 14] d) 1)
(a b (13 14 d) 1)
;;(do4clojure.core/a do4clojure.core/b (13 14 do4clojure.core/d) 1)
> `(foo 'bar)
(foo (quote bar))
;; (do4clojure.core/foo (quote do4clojure.core/bar))
> `(foo '~cs)
(foo (quote [13 14]))
> `(foo '(bla [~cs] ~@cs))
(foo (quote (bla [[13 14]] 13 14)))
;; (do4clojure.core/foo (quote (do4clojure.core/bla [[13 14]] 13 14)))
> `(foo '(bla [~@cs] ~@cs))
(foo (quote (bla [13 14] 13 14)))
;; (do4clojure.core/foo (quote (do4clojure.core/bla [13 14] 13 14)))
;; Still support Scheme, too:
> `(a b unquote c)
(a b . 13)
> `(a b unquote c d)
(a b unquote c d)
> `(a ,@(list 1 2))
(a 1 2)
> `(,@(list 1 2))
(1 2)
> `,@(list 1 2)
,@(list 1 2)
> `(a b (,c ,@(list 1 2) d) 1)
(a b (13 1 2 d) 1)
> (defmacro t-fixx ([x] x) ([x y] `(/ ~x ~ y)))
> (t-fixx 10 20)
1/2
;;> (##namespace ("clojure#" t-fixx)) ;; *Sigh* is this a hack now.[again]
;; oh that doesn't even work. TOO BAD XX.
> (macroexpand (quote-source (clojure#t-fixx 10 20)))
(/ 10 20)
> (defmacro t-fixx
([x] x)
([x y]
(first x)
`(list (first ~y) ~ x)))
> (macroexpand (quote-source (clojure#t-fixx (clojure#t-fixx "10" "30") "20")))
(list (first "20") (clojure#t-fixx "10" "30"))
;; (clojure.core/list (clojure.core/first "20") (t-fixx "10" "30"))
> (eval #)
(#\2 (#\3 "10"))
;; Gensym
> (define TEST:equal? syntax-equal?)
> `(foo# foo# bar#)
;; (foo__1949__auto__ foo__1949__auto__ bar__1950__auto__)
(GEN:foo-1 GEN:foo-1 GEN:bar-2)
> (%try `(~foo#))
;; Syntax error compiling at (form-init6177359002304238130.clj:1:1621).
;; Unable to resolve symbol: foo# in this context
(exception text: "Unbound variable: foo#\n"))
(defmacro (clojure#fn arg0 . args)
(let*-values (((maybe-name args)
(mcase arg0
(symbol?
(values arg0 args))
(else
(values #f (cons arg0 args)))))
((binds args)
(if (null? args)
(raise-source-error stx "fn: not enough arguments")
(let-pair
((a args) args)
(mcase a
(vector?
(values a args))
(else
(raise-source-error
stx "fn: missing bindings list")))))))
(let ((code `(lambda ,(clojure->scheme-args binds) ,@args)))
(if maybe-name
`(named ,maybe-name ,code)
code))))
(def clojure#false #f)
(def clojure#true #t)
(def clojure#nil #!void)
(def (clojure#nil? v)
(eq? v #!void))
(TEST
> (use-clojure-base)
> (nil? '())
#f
> (nil? nil)
#t
;; > (nil? 'nil)
;; true
)
(def (clojure-=/2 a b)
(FV (a b)
(let retry ((a a)
(b b))
(cond ((pair? a)
(cond ((pair? b)
;; heh back at treating pairs as
;; cdr-type-flexible entities, since
;; clojure#concat will re-use vectors as cdr,
;; at least currently (not sure what Clojure
;; does internally)
(let-pair ((a0 a) a)
(let-pair ((b0 b) b)
(and (clojure-=/2 a0 b0)
(clojure-=/2 a b)))))
((vector? b)
(let (len (vector-length b))
(let lp ((s a)
(i 0))
(FV (s)
(if (null? s)
(= i len)
(if (< i len)
(let-pair ((s0 s*) s)
(and (clojure-=/2
s0 (vector-ref b i))
(lp s*
(inc i))))
#f))))))
((string? b)
#f)
(else
(WARN "don't know how to handle these" a b)
(equal? a b))))
((vector? a)
(if (vector? b)
(let ((la (vector-length a))
(lb (vector-length b)))
(and (= a b)
(let lp ((i 0))
(if (< i len)
(and (clojure-=/2 (vector-ref a i)
(vector-ref b i))
(lp (inc i)))
#t))))
;; save flipped duplication
(retry b a)))
;; (((either number? string?) a)
;; (equal? a b))
;; ((boolean? a)
;; (if (boolean? b)
;; (eq? a b)
;; (retry b a)))
;; ((eq? a #!void)
;; )
(else
(equal? a b))))))
(def (clojure#= a b . rest)
;; Clojure allows calling it with a single argument; what point
;; is that for? Try to avoid alloc.
(and (clojure-=/2 a b)
(or (null? rest)
(every (lambda (v)
;; or should it be the one before each time ??
(clojure-=/2 a v))
rest))))
(TEST
> (clojure#= '(a b c . [d e f]) '(a b c d e f))
#t
;; This was done since (concat '[a b c] '[d e f]) used to return that
;; value in the first argument. Not any more. Leave it as is?
)
(TEST
> (use-clojure-base)
> (= true true)
#t
> (= true false)
#f
> (= false false)
#t
> (= false nil)
#f
> (= false '())
#f
> (= nil '())
#f ;; false
> (= nil nil)
#t ;; true
> (= '(a) '[a])
#t ;; true
> (= '(a) '[a b])
#f ;; false
> (= '(a) '"a")
#f ;; false
> (= '"a" '"a")
#t ;; true
> (= '"a" '"ab")
#f ;; false
)
(insert-result-of
(let ((test '(or (eq? val #f)
;; (eq? val '()) NO! This is true in Clojure!
;; Heh. Well, convenient.
(eq? val #!void))))
`(begin
(def (clojure#not val) ,test)
(def (clojure#not-not val) (##not ,test)))))
(def (clojure#false? v)
(eq? v #f))
(def (clojure#true? v)
(eq? v #t))
(defmacro (clojure#if test yes #!optional (no #!void))
`(##if (clojure#not-not ,test)
,yes
,no))
(defmacro (clojure#if-not test yes #!optional (no #!void))
`(##if (clojure#not ,test)
,yes
,no))
(defmacro (clojure#when test . branches)
`(##if (clojure#not-not ,test)
(begin ,@branches)))
(TEST
> (use-clojure-base)
> (if '[] 'y 'n)
y
> (if '() 'y 'n)
y
> (if nil 'y 'n)
n)
(def (clojure#keyword? v)
(or (keyword? v) ;; ?
(and (symbol? v)
(let* ((s (symbol.string v))
(len (string-length s)))
(and (>= len 1)
(eq? (string-ref s 0) #\:))))
;; For predefine-keywords hack in clojure.scm:
(and (procedure? v)
(if-let (n (maybe-procedure-name v))
(clojure#keyword? n)
;; Okay? Just assume that all clojure keywords
;; are defined with debugging
;; information?..... evil. Use symboltable
;; instead?
#f))))
(defmacro (clojure#cond . cases)
(if (even? (length cases))
`(##cond ,@(map (lambda-pair ((t e))
`(,(if (clojure#keyword? (source-code t))
`',t
`(clojure#not-not ,t))
,e))
(sequential-pairs cases cons)))
(raise-source-error stx "cond requires an even number of forms")))
(TEST
> (use-clojure-base)
> (cond true 'a)
a
> (cond false 'a)
#!void
> (cond false 'a true 'b)
b
> (cond false 'a true 'b else 'c)
;; Syntax error compiling at (form-init5831580352679050351.clj:1:1).
;; Unable to resolve symbol: else in this context
b
;; ^ because Gambit doesn't stop statically.
> (%try (cond false 'a else 'c))
(exception text: "Unbound variable: else\n")
> (with-exception-catcher
source-error-message
(& (eval (quote-source (cond false)))))
;; Syntax error macroexpanding cond at (form-init5831580352679050351.clj:1:1).
;; cond requires an even number of forms
"cond requires an even number of forms"
> (cond false 'a true 'b :else 'c)
b
> (cond false 'a nil 'b :else 'c)
c
> (cond false 'a '() 'b :else 'c)
b
> (cond false 'a :else 'c '() 'b)
c
;; :else is just taken as keyword, true value
> (cond false 'a nil 'b :eff 'c)
c)
(defmacro (clojure#let binds . body)
(mcase binds
(vector?
`(##let ,(sourcify
(sequential-pairs (=> binds source-code vector.list)
list)
binds)
,@body))
;; fall back to Scheme
((either pair? symbol?)
`(easy#let ,binds ,@body))))
(def (myscheme#cond-expand cases IF use-else-keyword? no-match)
(fold-right
(lambda (c res)
(assert*
pair? c
(lambda (c*)
(let-pair
((t c*) c*)
(if (and use-else-keyword?
(eq? (source-code t) 'else))
(if (eq? res no-match)
`(##begin ,@c*)
(raise-source-error c "else clause must be last"))
(let ((normal (lambda ()
`(,IF ,t
(##begin ,@c*)
,res))))
(if (pair? c*)
(let-pair
((a c**) c*)
(if (eq? (source-code a) '=>)
(with-gensym
V
`(##let ((,V ,t))
(,IF ,V
(,@c** ,V)
,res)))
(normal)))
(normal))))))))
no-match
cases))
(defmacro (myscheme#cond . cases)
(myscheme#cond-expand cases
`##if ;; hygienic
#t
`(##void)))
(defmacro (myscheme-unhygienic#cond . cases)
(myscheme#cond-expand cases
`if
#f ;; OK? used for generated code anyway, so,
;; fine ?
`(void)))
(TEST
> (define TEST:equal? syntax-equal?)
> (expansion#myscheme#cond ((x v) (newline) #t) (ELSE 'fun))
(##if (x v)
(##begin (newline) #t)
(##if ELSE
(##begin 'fun)
(##void)))
> (expansion#myscheme#cond ((x v) (newline) #t) (else 'fun))
(##if (x v)
(##begin (newline) #t)
(##begin 'fun))
> (expansion#myscheme-unhygienic#cond ((x v) (newline) #t) (else 'fun))
(if (x v)
(##begin (newline) #t)
(if else
(##begin 'fun)
(void)))
> (expansion#myscheme-unhygienic#cond ((x v) (newline) #t) ((dd) => meh) (else 'fun))
(if (x v)
(##begin (newline) #t)
(##let ((GEN:V-10932 (dd)))
(if GEN:V-10932
(meh GEN:V-10932)
(if else
(##begin 'fun)
(void))))))
(defmacro (clojure#if-let binds yes #!optional (no #!void))
(mcase binds
(vector?
;; Thanks for offering me if-let-expand (working around ""
;; namespace macro referral issue--OK actually have got
;; access to expander# anyway, thanks myself again) and
;; taking cond form as a parameter (was already there)!
;; Doesn't allow to directly parameterize the boolean
;; testing, but we can use an indirection via a custom cond
;; macro.
(if-let-expand `myscheme-unhygienic#cond
`#t
(=> binds
source-code
vector->list
list
(sourcify binds))
yes no))
;; fall back to Scheme
((either pair? symbol?)
(if-let-expand `##cond `#t binds yes no))))
(TEST
> (use-clojure-base)
;; > (if-let [a '()])
;; Syntax error macroexpanding clojure.core/if-let at (form-init5831580352679050351.clj:1:1).
;; () - failed: Insufficient input at: [:then]
> (if-let [a nil] (list a) 'n)
n
> (if-let [a ""] (list a) 'n)
("")
> (if-let [a '()] (list a) 'n)
(())
> (if-let [a '()] (list a))
(())
> (if-let [a false] (list a))
#!void
> (with-exception-catcher
source-error-message
(& (eval '(if-let [a false] (list a) 'n 'z))))
;; Syntax error macroexpanding clojure.core/if-let at (form-init5831580352679050351.clj:1:1).
;; ((quote z)) - failed: Extra input
"too many arguments"
> (with-exception-catcher
source-error-message
(& (eval (quote-source (let [nil 1] (if-let [a false] (list a)))))))
;; Syntax error macroexpanding clojure.core/let at (form-init5831580352679050351.clj:1:1).
;; nil - failed: simple-symbol? at: [:bindings :form :local-symbol] spec: :clojure.core.specs.alpha/local-name
;; nil - failed: vector? at: [:bindings :form :seq-destructure] spec: :clojure.core.specs.alpha/seq-binding-form
;; nil - failed: map? at: [:bindings :form :map-destructure] spec: :clojure.core.specs.alpha/map-bindings
;; nil - failed: map? at: [:bindings :form :map-destructure] spec: :clojure.core.specs.alpha/map-special-binding
#!void
;; binds nil but it's a different variable than the nil that is
;; mapped to clojure#nil.
;; BTW: this is interesting: Gambit's namespacing does more than
;; first thought? Or, let really binds the given symbol doesn't
;; namespace-prefix it?
> (let [nil 1] nil)
1
> (let [nil 1] clojure#nil)
#!void
> (let [clojure#nil 1] clojure#nil)
1
> (let [clojure#nil 1] nil)
#!void)
(def (clojure#first v)
(FV (v)
(cond ((pair? v)
(car v))
((null? v)
clojure#nil)
(else
(clojure#first (.stream v))))))
(def (clojure#rest v)
(FV (v)
(cond ((pair? v)
(cdr v))
((null? v)
'())
(else
(clojure#rest (.stream v))))))
(def clojure#next (comp clojure#seq clojure#rest))
(TEST
> (use-clojure-base)
> (def os '(() [] (a) [a] (a b) [a b]))
> (F (map next os))
(#!void #!void #!void #!void (b) (b))
> (F (map rest os))
(() () () () (b) (b))
> (F (map first os))
(#!void #!void a a a a))
(def (empty-vector? v)
(and (vector? v)
(zero? (vector-length v))))
(def (clojure#seq v)
(FV (v)
(case v