-
Notifications
You must be signed in to change notification settings - Fork 67
/
procs.go
2162 lines (1932 loc) · 50.1 KB
/
procs.go
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
package core
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"math/big"
"math/rand"
"os"
"path/filepath"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"time"
"unicode/utf8"
)
var coreNamespaces []string
type (
Phase int
Dialect int
StringReader interface {
ReadString(delim byte) (s string, e error)
}
)
const (
READ Phase = iota
FORMAT
PARSE
EVAL
PRINT_IF_NOT_NIL
)
const VERSION = "v0.17.1"
const (
CLJ Dialect = iota
CLJS
JOKER
EDN
UNKNOWN
)
func ExtractCallable(args []Object, index int) Callable {
return EnsureArgIsCallable(args, index)
}
func ExtractObject(args []Object, index int) Object {
return args[index]
}
func ExtractString(args []Object, index int) string {
return EnsureArgIsString(args, index).S
}
func ExtractKeyword(args []Object, index int) string {
return EnsureArgIsKeyword(args, index).ToString(false)
}
func ExtractStringable(args []Object, index int) string {
return EnsureArgIsStringable(args, index).S
}
func ExtractStrings(args []Object, index int) []string {
strs := make([]string, 0)
for i := index; i < len(args); i++ {
strs = append(strs, EnsureArgIsString(args, i).S)
}
return strs
}
func ExtractInt(args []Object, index int) int {
return EnsureArgIsInt(args, index).I
}
func ExtractInteger(args []Object, index int) int {
switch c := args[index].(type) {
case Number:
return c.Int().I
default:
panic(RT.NewArgTypeError(index, c, "Number"))
}
}
func ExtractBoolean(args []Object, index int) bool {
return EnsureArgIsBoolean(args, index).B
}
func FailArg(obj Object, typeName string, index int) *EvalError {
return RT.NewArgTypeError(index, obj, typeName)
}
func FailObject(obj Object, typeName, pattern string) *EvalError {
if pattern == "" {
pattern = "%s"
}
msg := fmt.Sprintf("Expected %s, got %s", typeName, obj.GetType().ToString(false))
return RT.NewError(fmt.Sprintf(pattern, msg))
}
func ExtractChar(args []Object, index int) rune {
return EnsureArgIsChar(args, index).Ch
}
func ExtractTime(args []Object, index int) time.Time {
return EnsureArgIsTime(args, index).T
}
func ExtractDouble(args []Object, index int) float64 {
return EnsureArgIsDouble(args, index).D
}
func ExtractNumber(args []Object, index int) Number {
return EnsureArgIsNumber(args, index)
}
func ExtractBigInt(args []Object, index int) *big.Int {
return EnsureArgIsBigInt(args, index).b
}
func ExtractBigFloat(args []Object, index int) *big.Float {
return EnsureArgIsBigFloat(args, index).b
}
func ExtractRegex(args []Object, index int) *regexp.Regexp {
return EnsureArgIsRegex(args, index).R
}
func ExtractSeqable(args []Object, index int) Seqable {
return EnsureArgIsSeqable(args, index)
}
func ExtractMap(args []Object, index int) Map {
return EnsureArgIsMap(args, index)
}
func ExtractIOReader(args []Object, index int) io.Reader {
return EnsureArgIsio_Reader(args, index)
}
func ExtractIOWriter(args []Object, index int) io.Writer {
return EnsureArgIsio_Writer(args, index)
}
var procMeta = func(args []Object) Object {
CheckArity(args, 1, 1)
switch obj := args[0].(type) {
case Meta:
meta := obj.GetMeta()
if meta != nil {
return meta
}
case *Type:
meta := obj.GetMeta()
if meta != nil {
return meta
}
}
return NIL
}
var procWithMeta = func(args []Object) Object {
CheckArity(args, 2, 2)
m := EnsureArgIsMeta(args, 0)
if args[1].Equals(NIL) {
return args[0]
}
return m.WithMeta(EnsureArgIsMap(args, 1))
}
var procIsZero = func(args []Object) Object {
n := EnsureArgIsNumber(args, 0)
ops := GetOps(n)
return Boolean{B: ops.IsZero(n)}
}
var procIsPos = func(args []Object) Object {
n := EnsureArgIsNumber(args, 0)
ops := GetOps(n)
return Boolean{B: ops.Gt(n, Int{I: 0})}
}
var procIsNeg = func(args []Object) Object {
n := EnsureArgIsNumber(args, 0)
ops := GetOps(n)
return Boolean{B: ops.Lt(n, Int{I: 0})}
}
var procAdd = func(args []Object) Object {
x := EnsureObjectIsNumber(args[0], "")
y := EnsureObjectIsNumber(args[1], "")
ops := GetOps(x).Combine(GetOps(y))
return ops.Add(x, y)
}
var procAddEx = func(args []Object) Object {
x := EnsureObjectIsNumber(args[0], "")
y := EnsureObjectIsNumber(args[1], "")
ops := GetOps(x).Combine(GetOps(y)).Combine(BIGINT_OPS)
return ops.Add(x, y)
}
var procMultiply = func(args []Object) Object {
x := EnsureObjectIsNumber(args[0], "")
y := EnsureObjectIsNumber(args[1], "")
ops := GetOps(x).Combine(GetOps(y))
return ops.Multiply(x, y)
}
var procMultiplyEx = func(args []Object) Object {
x := EnsureObjectIsNumber(args[0], "")
y := EnsureObjectIsNumber(args[1], "")
ops := GetOps(x).Combine(GetOps(y)).Combine(BIGINT_OPS)
return ops.Multiply(x, y)
}
var procSubtract = func(args []Object) Object {
var a, b Object
if len(args) == 1 {
a = Int{I: 0}
b = args[0]
} else {
a = args[0]
b = args[1]
}
ops := GetOps(a).Combine(GetOps(b))
return ops.Subtract(EnsureObjectIsNumber(a, ""), EnsureObjectIsNumber(b, ""))
}
var procSubtractEx = func(args []Object) Object {
var a, b Object
if len(args) == 1 {
a = Int{I: 0}
b = args[0]
} else {
a = args[0]
b = args[1]
}
ops := GetOps(a).Combine(GetOps(b)).Combine(BIGINT_OPS)
return ops.Subtract(EnsureObjectIsNumber(a, ""), EnsureObjectIsNumber(b, ""))
}
var procDivide = func(args []Object) Object {
x := EnsureArgIsNumber(args, 0)
y := EnsureArgIsNumber(args, 1)
ops := GetOps(x).Combine(GetOps(y))
return ops.Divide(x, y)
}
var procQuot = func(args []Object) Object {
x := EnsureArgIsNumber(args, 0)
y := EnsureArgIsNumber(args, 1)
ops := GetOps(x).Combine(GetOps(y))
return ops.Quotient(x, y)
}
var procRem = func(args []Object) Object {
x := EnsureArgIsNumber(args, 0)
y := EnsureArgIsNumber(args, 1)
ops := GetOps(x).Combine(GetOps(y))
return ops.Rem(x, y)
}
var procBitNot = func(args []Object) Object {
x := EnsureObjectIsInt(args[0], "Bit operation not supported for "+args[0].GetType().ToString(false))
return Int{I: ^x.I}
}
func EnsureObjectIsInts(args []Object) (Int, Int) {
x := EnsureObjectIsInt(args[0], "Bit operation not supported: %s")
y := EnsureObjectIsInt(args[1], "Bit operation not supported: %s")
return x, y
}
var procBitAnd = func(args []Object) Object {
x, y := EnsureObjectIsInts(args)
return Int{I: x.I & y.I}
}
var procBitOr = func(args []Object) Object {
x, y := EnsureObjectIsInts(args)
return Int{I: x.I | y.I}
}
var procBitXor = func(args []Object) Object {
x, y := EnsureObjectIsInts(args)
return Int{I: x.I ^ y.I}
}
var procBitAndNot = func(args []Object) Object {
x, y := EnsureObjectIsInts(args)
return Int{I: x.I &^ y.I}
}
var procBitClear = func(args []Object) Object {
x, y := EnsureObjectIsInts(args)
return Int{I: x.I &^ (1 << uint(y.I))}
}
var procBitSet = func(args []Object) Object {
x, y := EnsureObjectIsInts(args)
return Int{I: x.I | (1 << uint(y.I))}
}
var procBitFlip = func(args []Object) Object {
x, y := EnsureObjectIsInts(args)
return Int{I: x.I ^ (1 << uint(y.I))}
}
var procBitTest = func(args []Object) Object {
x, y := EnsureObjectIsInts(args)
return Boolean{B: x.I&(1<<uint(y.I)) != 0}
}
var procBitShiftLeft = func(args []Object) Object {
x, y := EnsureObjectIsInts(args)
return Int{I: x.I << uint(y.I)}
}
var procBitShiftRight = func(args []Object) Object {
x, y := EnsureObjectIsInts(args)
return Int{I: x.I >> uint(y.I)}
}
var procUnsignedBitShiftRight = func(args []Object) Object {
x, y := EnsureObjectIsInts(args)
return Int{I: int(uint(x.I) >> uint(y.I))}
}
var procExInfo = func(args []Object) Object {
CheckArity(args, 2, 3)
res := &ExInfo{
rt: RT.clone(),
}
res.Add(KEYWORDS.message, EnsureArgIsString(args, 0))
res.Add(KEYWORDS.data, EnsureArgIsMap(args, 1))
if len(args) == 3 {
res.Add(KEYWORDS.cause, EnsureArgIsError(args, 2))
}
return res
}
var procExData = func(args []Object) Object {
if ok, res := args[0].(*ExInfo).Get(KEYWORDS.data); ok {
return res
}
return NIL
}
var procExCause = func(args []Object) Object {
if ok, res := args[0].(*ExInfo).Get(KEYWORDS.cause); ok {
return res
}
return NIL
}
var procExMessage = func(args []Object) Object {
return args[0].(Error).Message()
}
var procRegex = func(args []Object) Object {
r, err := regexp.Compile(EnsureArgIsString(args, 0).S)
if err != nil {
panic(RT.NewError("Invalid regex: " + err.Error()))
}
return &Regex{R: r}
}
func reGroups(s string, indexes []int) Object {
if indexes == nil {
return NIL
} else if len(indexes) == 2 {
if indexes[0] == -1 {
return NIL
} else {
return String{S: s[indexes[0]:indexes[1]]}
}
} else {
v := EmptyVector()
for i := 0; i < len(indexes); i += 2 {
if indexes[i] == -1 {
v = v.Conjoin(NIL)
} else {
v = v.Conjoin(String{S: s[indexes[i]:indexes[i+1]]})
}
}
return v
}
}
var procReSeq = func(args []Object) Object {
re := EnsureArgIsRegex(args, 0)
s := EnsureArgIsString(args, 1)
matches := re.R.FindAllStringSubmatchIndex(s.S, -1)
if matches == nil {
return NIL
}
res := make([]Object, len(matches))
for i, match := range matches {
res[i] = reGroups(s.S, match)
}
return &ArraySeq{arr: res}
}
var procReFind = func(args []Object) Object {
re := EnsureArgIsRegex(args, 0)
s := EnsureArgIsString(args, 1)
match := re.R.FindStringSubmatchIndex(s.S)
return reGroups(s.S, match)
}
var procRand = func(args []Object) Object {
r := rand.Float64()
return Double{D: r}
}
var procIsSpecialSymbol = func(args []Object) Object {
return Boolean{B: IsSpecialSymbol(args[0])}
}
var procSubs = func(args []Object) Object {
s := EnsureArgIsString(args, 0).S
start := EnsureArgIsInt(args, 1).I
slen := utf8.RuneCountInString(s)
end := slen
if len(args) > 2 {
end = EnsureArgIsInt(args, 2).I
}
if start < 0 || start > slen {
panic(RT.NewError(fmt.Sprintf("String index out of range: %d", start)))
}
if end < 0 || end > slen {
panic(RT.NewError(fmt.Sprintf("String index out of range: %d", end)))
}
return String{S: string([]rune(s)[start:end])}
}
var procIntern = func(args []Object) Object {
ns := EnsureArgIsNamespace(args, 0)
sym := EnsureArgIsSymbol(args, 1)
vr := ns.Intern(sym)
if len(args) == 3 {
vr.Value = args[2]
}
return vr
}
var procSetMeta = func(args []Object) Object {
vr := EnsureArgIsVar(args, 0)
meta := EnsureArgIsMap(args, 1)
vr.meta = meta
return NIL
}
var procAtom = func(args []Object) Object {
res := &Atom{
value: args[0],
}
if len(args) > 1 {
m := NewHashMap(args[1:]...)
if ok, v := m.Get(KEYWORDS.meta); ok {
res.meta = EnsureObjectIsMap(v, "")
}
}
return res
}
var procDeref = func(args []Object) Object {
return EnsureArgIsDeref(args, 0).Deref()
}
var procSwap = func(args []Object) Object {
a := EnsureArgIsAtom(args, 0)
f := EnsureArgIsCallable(args, 1)
fargs := append([]Object{a.value}, args[2:]...)
a.value = f.Call(fargs)
return a.value
}
var procSwapVals = func(args []Object) Object {
a := EnsureArgIsAtom(args, 0)
f := EnsureArgIsCallable(args, 1)
fargs := append([]Object{a.value}, args[2:]...)
oldValue := a.value
a.value = f.Call(fargs)
return NewVectorFrom(oldValue, a.value)
}
var procReset = func(args []Object) Object {
a := EnsureArgIsAtom(args, 0)
a.value = args[1]
return a.value
}
var procResetVals = func(args []Object) Object {
a := EnsureArgIsAtom(args, 0)
oldValue := a.value
a.value = args[1]
return NewVectorFrom(oldValue, a.value)
}
var procAlterMeta = func(args []Object) Object {
r := EnsureArgIsRef(args, 0)
f := EnsureArgIsFn(args, 1)
return r.AlterMeta(f, args[2:])
}
var procResetMeta = func(args []Object) Object {
r := EnsureArgIsRef(args, 0)
m := EnsureArgIsMap(args, 1)
return r.ResetMeta(m)
}
var procEmpty = func(args []Object) Object {
switch c := args[0].(type) {
case Collection:
return c.Empty()
default:
return NIL
}
}
var procIsBound = func(args []Object) Object {
vr := EnsureArgIsVar(args, 0)
return Boolean{B: vr.Value != nil}
}
// Convert Joker object to native Go object. For those satisfying the
// Native type, that's straightforward. For other Joker objects, try
// converting them to suitable native Go objects. E.g. a BigInt might
// hold a value > MaxInt64 but < MaxUint64, in which case conversion
// to a uint64 makes more sense than returning the stringized version,
// for use cases such as `(format "%x" value)`. Even for BigFloat and
// BigRat, try to (accurately) convert them to native types so they
// can be formatted via the usual ways.
func ToNative(obj Object) interface{} {
switch obj := obj.(type) {
case Native:
return obj.Native()
case *BigInt:
b := obj.BigInt()
if b.IsInt64() {
return b.Int64()
}
if b.IsUint64() {
return b.Uint64()
}
case *BigFloat:
b := obj.BigFloat()
if f, acc := b.Float64(); acc == big.Exact {
return f
}
case *Ratio:
b := obj.Ratio()
if f, exact := b.Float64(); exact {
return f
}
}
return obj.ToString(false)
}
var procFormat = func(args []Object) Object {
s := EnsureArgIsString(args, 0)
objs := args[1:]
fargs := make([]interface{}, len(objs))
for i, v := range objs {
fargs[i] = ToNative(v)
}
res := fmt.Sprintf(s.S, fargs...)
return String{S: res}
}
var procList = func(args []Object) Object {
return NewListFrom(args...)
}
var procCons = func(args []Object) Object {
CheckArity(args, 2, 2)
s := EnsureArgIsSeqable(args, 1).Seq()
return s.Cons(args[0])
}
var procFirst = func(args []Object) Object {
CheckArity(args, 1, 1)
s := EnsureArgIsSeqable(args, 0).Seq()
return s.First()
}
var procNext = func(args []Object) Object {
CheckArity(args, 1, 1)
s := EnsureArgIsSeqable(args, 0).Seq()
res := s.Rest()
if res.IsEmpty() {
return NIL
}
return res
}
var procRest = func(args []Object) Object {
CheckArity(args, 1, 1)
s := EnsureArgIsSeqable(args, 0).Seq()
return s.Rest()
}
var procConj = func(args []Object) Object {
switch c := args[0].(type) {
case Conjable:
return c.Conj(args[1])
case Seq:
return c.Cons(args[1])
default:
panic(RT.NewError("conj's first argument must be a collection, got " + c.GetType().ToString(false)))
}
}
var procSeq = func(args []Object) Object {
CheckArity(args, 1, 1)
s := EnsureArgIsSeqable(args, 0).Seq()
if s.IsEmpty() {
return NIL
}
return s
}
var procIsInstance = func(args []Object) Object {
CheckArity(args, 2, 2)
t := EnsureArgIsType(args, 0)
return Boolean{B: IsInstance(t, args[1])}
}
var procAssoc = func(args []Object) Object {
return EnsureArgIsAssociative(args, 0).Assoc(args[1], args[2])
}
var procEquals = func(args []Object) Object {
return Boolean{B: args[0].Equals(args[1])}
}
var procCount = func(args []Object) Object {
switch obj := args[0].(type) {
case Counted:
return Int{I: obj.Count()}
default:
s := EnsureObjectIsSeqable(obj, "count not supported on this type: %s")
return Int{I: SeqCount(s.Seq())}
}
}
var procSubvec = func(args []Object) Object {
// TODO: implement proper Subvector structure
v := EnsureArgIsVector(args, 0)
start := EnsureArgIsInt(args, 1).I
end := EnsureArgIsInt(args, 2).I
if start > end {
panic(RT.NewError(fmt.Sprintf("subvec's start index (%d) is greater than end index (%d)", start, end)))
}
subv := make([]Object, 0, end-start)
for i := start; i < end; i++ {
subv = append(subv, v.at(i))
}
return NewVectorFrom(subv...)
}
var procCast = func(args []Object) Object {
t := EnsureArgIsType(args, 0)
if t.reflectType.Kind() == reflect.Interface &&
args[1].GetType().reflectType.Implements(t.reflectType) ||
args[1].GetType().reflectType == t.reflectType {
return args[1]
}
panic(RT.NewError("Cannot cast " + args[1].GetType().ToString(false) + " to " + t.ToString(false)))
}
var procVec = func(args []Object) Object {
return NewVectorFromSeq(EnsureArgIsSeqable(args, 0).Seq())
}
var procHashMap = func(args []Object) Object {
if len(args)%2 != 0 {
panic(RT.NewError("No value supplied for key " + args[len(args)-1].ToString(false)))
}
return NewHashMap(args...)
}
var procHashSet = func(args []Object) Object {
res := EmptySet()
for i := 0; i < len(args); i++ {
res.Add(args[i])
}
return res
}
func str(args ...Object) string {
var buffer bytes.Buffer
for _, obj := range args {
if !obj.Equals(NIL) {
t := obj.GetType()
// TODO: this is a hack. Rethink escape parameter in ToString
escaped := (t == TYPE.String) || (t == TYPE.Char) || (t == TYPE.Regex)
buffer.WriteString(obj.ToString(!escaped))
}
}
return buffer.String()
}
var procStr = func(args []Object) Object {
return String{S: str(args...)}
}
var procSymbol = func(args []Object) Object {
if len(args) == 1 {
return MakeSymbol(EnsureArgIsString(args, 0).S)
}
var ns *string = nil
if !args[0].Equals(NIL) {
ns = STRINGS.Intern(EnsureArgIsString(args, 0).S)
}
return Symbol{
ns: ns,
name: STRINGS.Intern(EnsureArgIsString(args, 1).S),
}
}
var procKeyword = func(args []Object) Object {
if len(args) == 1 {
switch obj := args[0].(type) {
case String:
return MakeKeyword(obj.S)
case Symbol:
return Keyword{
ns: obj.ns,
name: obj.name,
hash: hashSymbol(obj.ns, obj.name) ^ KeywordHashMask,
}
default:
return NIL
}
}
var ns *string = nil
if !args[0].Equals(NIL) {
ns = STRINGS.Intern(EnsureArgIsString(args, 0).S)
}
name := STRINGS.Intern(EnsureArgIsString(args, 1).S)
return Keyword{
ns: ns,
name: name,
hash: hashSymbol(ns, name) ^ KeywordHashMask,
}
}
var procGensym = func(args []Object) Object {
return genSym(EnsureArgIsString(args, 0).S, "")
}
var procApply = func(args []Object) Object {
// TODO:
// Stacktrace is broken. Need to somehow know
// the name of the function passed ...
f := EnsureArgIsCallable(args, 0)
return f.Call(ToSlice(EnsureArgIsSeqable(args, 1).Seq()))
}
var procLazySeq = func(args []Object) Object {
return &LazySeq{
fn: args[0].(*Fn),
}
}
var procDelay = func(args []Object) Object {
return &Delay{
fn: args[0].(*Fn),
}
}
var procForce = func(args []Object) Object {
switch d := args[0].(type) {
case *Delay:
return d.Force()
default:
return d
}
}
var procIdentical = func(args []Object) Object {
return Boolean{B: args[0] == args[1]}
}
var procCompare = func(args []Object) Object {
k1, k2 := args[0], args[1]
if k1.Equals(k2) {
return Int{I: 0}
}
switch k2.(type) {
case Nil:
return Int{I: 1}
}
switch k1 := k1.(type) {
case Nil:
return Int{I: -1}
case Comparable:
return Int{I: k1.Compare(k2)}
}
panic(RT.NewError(fmt.Sprintf("%s (type: %s) is not a Comparable", k1.ToString(true), k1.GetType().ToString(false))))
}
var procInt = func(args []Object) Object {
switch obj := args[0].(type) {
case Char:
return Int{I: int(obj.Ch)}
case Number:
return obj.Int()
default:
panic(RT.NewError(fmt.Sprintf("Cannot cast %s (type: %s) to Int", obj.ToString(true), obj.GetType().ToString(false))))
}
}
var procNumber = func(args []Object) Object {
return EnsureObjectIsNumber(args[0], "Cannot cast "+args[0].ToString(true)+": %s")
}
var procDouble = func(args []Object) Object {
n := EnsureObjectIsNumber(args[0], "Cannot cast "+args[0].ToString(true)+": %s")
return n.Double()
}
var procChar = func(args []Object) Object {
switch c := args[0].(type) {
case Char:
return c
case Number:
i := c.Int().I
if i < MIN_RUNE || i > MAX_RUNE {
panic(RT.NewError(fmt.Sprintf("Value out of range for char: %d", i)))
}
return Char{Ch: rune(i)}
default:
panic(RT.NewError(fmt.Sprintf("Cannot cast %s (type: %s) to Char", c.ToString(true), c.GetType().ToString(false))))
}
}
var procBoolean = func(args []Object) Object {
return Boolean{B: ToBool(args[0])}
}
var procNumerator = func(args []Object) Object {
bi := EnsureArgIsRatio(args, 0).r.Num()
return &BigInt{b: bi}
}
var procDenominator = func(args []Object) Object {
bi := EnsureArgIsRatio(args, 0).r.Denom()
return &BigInt{b: bi}
}
var procBigInt = func(args []Object) Object {
switch n := args[0].(type) {
case Number:
return &BigInt{b: n.BigInt()}
case String:
bi := &big.Int{}
if _, ok := bi.SetString(n.S, 10); ok {
return &BigInt{b: bi}
}
panic(RT.NewError("Invalid number format " + n.S))
default:
panic(RT.NewError(fmt.Sprintf("Cannot cast %s (type: %s) to BigInt", n.ToString(true), n.GetType().ToString(false))))
}
}
var procBigFloat = func(args []Object) Object {
switch n := args[0].(type) {
case Number:
return &BigFloat{b: n.BigFloat()}
case String:
b := &big.Float{}
if _, ok := b.SetString(n.S); ok {
return &BigFloat{b: b}
}
panic(RT.NewError("Invalid number format " + n.S))
default:
panic(RT.NewError(fmt.Sprintf("Cannot cast %s (type: %s) to BigFloat", n.ToString(true), n.GetType().ToString(false))))
}
}
var procNth = func(args []Object) Object {
n := EnsureArgIsNumber(args, 1).Int().I
switch coll := args[0].(type) {
case Indexed:
if len(args) == 3 {
return coll.TryNth(n, args[2])
}
return coll.Nth(n)
case Nil:
return NIL
case Sequential:
switch coll := args[0].(type) {
case Seqable:
if len(args) == 3 {
return SeqTryNth(coll.Seq(), n, args[2])
}
return SeqNth(coll.Seq(), n)
}
}
panic(RT.NewError("nth not supported on this type: " + args[0].GetType().ToString(false)))
}
var procLt = func(args []Object) Object {
a := EnsureObjectIsNumber(args[0], "")
b := EnsureObjectIsNumber(args[1], "")
return Boolean{B: GetOps(a).Combine(GetOps(b)).Lt(a, b)}
}
var procLte = func(args []Object) Object {
a := EnsureObjectIsNumber(args[0], "")
b := EnsureObjectIsNumber(args[1], "")
return Boolean{B: GetOps(a).Combine(GetOps(b)).Lte(a, b)}
}
var procGt = func(args []Object) Object {
a := EnsureObjectIsNumber(args[0], "")
b := EnsureObjectIsNumber(args[1], "")
return Boolean{B: GetOps(a).Combine(GetOps(b)).Gt(a, b)}
}
var procGte = func(args []Object) Object {
a := EnsureObjectIsNumber(args[0], "")
b := EnsureObjectIsNumber(args[1], "")
return Boolean{B: GetOps(a).Combine(GetOps(b)).Gte(a, b)}
}
var procEq = func(args []Object) Object {
a := EnsureObjectIsNumber(args[0], "")
b := EnsureObjectIsNumber(args[1], "")
return MakeBoolean(numbersEq(a, b))
}
var procMax = func(args []Object) Object {
a := EnsureObjectIsNumber(args[0], "")
b := EnsureObjectIsNumber(args[1], "")
return Max(a, b)
}
var procMin = func(args []Object) Object {
a := EnsureObjectIsNumber(args[0], "")
b := EnsureObjectIsNumber(args[1], "")
return Min(a, b)
}
var procIncEx = func(args []Object) Object {
x := EnsureArgIsNumber(args, 0)
ops := GetOps(x).Combine(BIGINT_OPS)
return ops.Add(x, Int{I: 1})
}
var procDecEx = func(args []Object) Object {
x := EnsureArgIsNumber(args, 0)
ops := GetOps(x).Combine(BIGINT_OPS)
return ops.Subtract(x, Int{I: 1})
}
var procInc = func(args []Object) Object {
x := EnsureArgIsNumber(args, 0)
ops := GetOps(x).Combine(INT_OPS)
return ops.Add(x, Int{I: 1})
}
var procDec = func(args []Object) Object {
x := EnsureArgIsNumber(args, 0)
ops := GetOps(x).Combine(INT_OPS)
return ops.Subtract(x, Int{I: 1})
}
var procPeek = func(args []Object) Object {
s := EnsureObjectIsStack(args[0], "")
return s.Peek()
}
var procPop = func(args []Object) Object {
s := EnsureObjectIsStack(args[0], "")
return s.Pop().(Object)
}
var procContains = func(args []Object) Object {
switch c := args[0].(type) {
case Gettable:
ok, _ := c.Get(args[1])
if ok {
return Boolean{B: true}
}
return Boolean{B: false}
}
panic(RT.NewError("contains? not supported on type " + args[0].GetType().ToString(false)))
}