-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode.go
More file actions
1294 lines (1214 loc) · 33.9 KB
/
code.go
File metadata and controls
1294 lines (1214 loc) · 33.9 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
// Copyright (c) 2022, Peter Ohler, All rights reserved.
package slip
import (
"bytes"
"errors"
"fmt"
"io"
"math/big"
"regexp"
"strconv"
"strings"
"time"
"unicode"
"unicode/utf8"
)
const (
skipByte = 'a'
skipNewline = 'k'
commentByte = ';'
commentDone = 'c'
openParen = '('
closeParen = ')'
tokenStart = 't'
tokenDone = 'T'
doubleQuote = 'Q'
stringByte = 's'
stringDone = 'S'
escByte = 'e'
escOne = 'E'
escUnicode4 = 'u'
escUnicode8 = 'U'
runeDigit = '1'
runeHexA = 'H'
runeHexa = 'h'
pipeByte = 'P'
pipeDone = 'p'
sharpByte = '#'
charSlash = '/'
charDone = 'C'
vectorByte = 'V'
binaryByte = 'b'
octByte = 'o'
hexByte = 'x'
intDone = 'I'
sharpIntByte = '9'
sharpNumByte = '8'
sharpQuote = 'G'
sharpComplex = 'i'
radixByte = 'r'
arrayByte = 'A'
swallowOpen = '{'
bitVectorByte = 'Z'
bitVectorDone = 'z'
singleQuote = 'q'
backquote = 'B'
comma = ','
commaAt = '@'
blockStart = '|'
blockEnd0 = '}'
// 0123456789abcdef0123456789abcdef
valueMode = "" +
".........ak..a.................." + // 0x00
"a.Q#tttq()tt,tttttttttttttt;ttt." + // 0x20
"@tttttttttttttttttttttttttt...tt" + // 0x40
"Btttttttttttttttttttttttttt.P.t." + // 0x60
"................................" + // 0x80
"................................" + // 0xa0
"................................" + // 0xc0
"................................v" // 0xe0
// 0123456789abcdef0123456789abcdef
commentMode = "" +
".........ac..a.................." + // 0x00
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + // 0x20
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + // 0x40
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + // 0x60
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + // 0x80
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + // 0xa0
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + // 0xc0
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;" // 0xe0
// 0123456789abcdef0123456789abcdef
tokenMode = "" +
".........TT..T.................." + // 0x00
"T...aa..TTaa.aaaaaaaaaaaaaa.aaa." + // 0x20
"aaaaaaaaaaaaaaaaaaaaaaaaaaa...aa" + // 0x40
".aaaaaaaaaaaaaaaaaaaaaaaaaa...a." + // 0x60
"................................" + // 0x80
"................................" + // 0xa0
"................................" + // 0xc0
"................................t" // 0xe0
// 0123456789abcdef0123456789abcdef
stringMode = "" +
".........ss..s.................." + // 0x00
"ssSsssssssssssssssssssssssssssss" + // 0x20
"ssssssssssssssssssssssssssssesss" + // 0x40
"ssssssssssssssssssssssssssssssss" + // 0x60
"ssssssssssssssssssssssssssssssss" + // 0x80
"ssssssssssssssssssssssssssssssss" + // 0xa0
"ssssssssssssssssssssssssssssssss" + // 0xc0
"sssssssssssssssssssssssssssssssss" // 0xe0
// 0123456789abcdef0123456789abcdef
symbolMode = "" +
".........ss..s.................." + // 0x00
"ssssssssssssssssssssssssssssssss" + // 0x20
"ssssssssssssssssssssssssssssesss" + // 0x40
"sssssssssssssssssssssssssssspsss" + // 0x60
"ssssssssssssssssssssssssssssssss" + // 0x80
"ssssssssssssssssssssssssssssssss" + // 0xa0
"ssssssssssssssssssssssssssssssss" + // 0xc0
"ssssssssssssssssssssssssssssssssS" // 0xe0
// 0123456789abcdef0123456789abcdef
escMode = "" +
"................................" + // 0x00
"..E............................." + // 0x20
".....................U......E..." + // 0x40
"..E...E.......E...E.Eu.........." + // 0x60
"................................" + // 0x80
"................................" + // 0xa0
"................................" + // 0xc0
"................................e" // 0xe0
// 0123456789abcdef0123456789abcdef
escByteMap = "" +
"................................" + // 0x00
"..\"............................." + // 0x20
"............................\\..." + // 0x40
"..\b...\f.......\n...\r.\t.........." + // 0x60
"................................" + // 0x80
"................................" + // 0xa0
"................................" + // 0xc0
"................................E" // 0xe0
// 0123456789abcdef0123456789abcdef
runeMode = "" +
"................................" + // 0x00
"................1111111111......" + // 0x20
".HHHHHH........................." + // 0x40
".hhhhhh........................." + // 0x60
"................................" + // 0x80
"................................" + // 0xa0
"................................" + // 0xc0
"................................r" // 0xe0
// 0123456789abcdef0123456789abcdef
sharpMode = "" +
"................................" + // 0x00
".......GV.Z.....9999999999......" + // 0x20
"..bi...........o........x.../..." + // 0x40
"..bi...........o........x...|..." + // 0x60
"................................" + // 0x80
"................................" + // 0xa0
"................................" + // 0xc0
"................................#" // 0xe0
// 0123456789abcdef0123456789abcdef
charMode = "" +
".........CC..C.................." + // 0x00
"C..a....CCaaaaaaaaaaaaaaaaa.aaa." + // 0x20
"aaaaaaaaaaaaaaaaaaaaaaaaaaa...aa" + // 0x40
".aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa." + // 0x60
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + // 0x80
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + // 0xa0
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + // 0xc0
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaC" // 0xe0
// 0123456789abcdef0123456789abcdef
intMode = "" +
".........II..I.................." + // 0x00
"I.......II.a.a..aaaaaaaaaa......" + // 0x20
".aaaaaaaaaaaaaaaaaaaaaaaaaa....." + // 0x40
".aaaaaaaaaaaaaaaaaaaaaaaaaa......" + // 0x60
"................................" + // 0x80
"................................" + // 0xa0
"................................" + // 0xc0
"................................i" // 0xe0
// 0123456789abcdef0123456789abcdef
sharpNumMode = "" +
"................................" + // 0x00
"................8888888888......" + // 0x20
".A................r............." + // 0x40
".A................r............." + // 0x60
"................................" + // 0x80
"................................" + // 0xa0
"................................" + // 0xc0
"................................8" // 0xe0
// 0123456789abcdef0123456789abcdef
mustArrayMode = "" +
"................................" + // 0x00
"........{......................." + // 0x20
"................................" + // 0x40
"................................" + // 0x60
"................................" + // 0x80
"................................" + // 0xa0
"................................" + // 0xc0
"................................(" // 0xe0
// 0123456789abcdef0123456789abcdef
blockCommentMode = "" +
"aaaaaaaaaakaaaaaaaaaaaaaaaaaaaaa" + // 0x00
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + // 0x20
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + // 0x40
"aaaaaaaaaaaaaaaaaaaaaaaaaaaa}aaa" + // 0x60
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + // 0x80
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + // 0xa0
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + // 0xc0
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|" // 0xe0
// 0123456789abcdef0123456789abcdef
blockEndMode = "" +
"||||||||||k|||||||||||||||||||||" + // 0x00
"|||c||||||||||||||||||||||||||||" + // 0x20
"||||||||||||||||||||||||||||||||" + // 0x40
"||||||||||||||||||||||||||||||||" + // 0x60
"||||||||||||||||||||||||||||||||" + // 0x80
"||||||||||||||||||||||||||||||||" + // 0xa0
"||||||||||||||||||||||||||||||||" + // 0xc0
"||||||||||||||||||||||||||||||||;" // 0xe0
// 0123456789abcdef0123456789abcdef
bitVectorMode = "" +
".........zz..z.................." + // 0x00
"z.......zz......aa.............." + // 0x20
"................................" + // 0x40
"................................" + // 0x60
"................................" + // 0x80
"................................" + // 0xa0
"................................" + // 0xc0
"................................z" // 0xe0
quoteMarker = marker('q')
sharpQuoteMarker = marker('#')
backquoteMarker = marker('b')
commaMarker = marker(',')
commaAtMarker = marker('@')
)
var (
// marker on stack indicating a vector and not a list.
vectorMarker = &Vector{}
// marker on stack indicating a vector and not a list.
complexMarker = Complex(complex(0, 0))
// Set as needed. Global since they are build in but from a different go
// package.
newQuote func(args List) Object
newSharpQuote func(args List) Object
newBackquote func(args List) Object
newComma func(args List) Object
newCommaAt func(args List) Object
decimalRegex = regexp.MustCompile(`^[-+]?[0-9]+\.?[0-9]*$`)
eFloatRegex = regexp.MustCompile(`^[-+]?[0-9]+\.?[0-9]*e[-+]?[0-9]+?$`)
shortFloatRegex = regexp.MustCompile(`^[-+]?[0-9]+\.?[0-9]*s[-+]?[0-9]+?$`)
singleFloatRegex = regexp.MustCompile(`^[-+]?[0-9]+\.?[0-9]*f[-+]?[0-9]+?$`)
doubleFloatRegex = regexp.MustCompile(`^[-+]?[0-9]+\.?[0-9]*d[-+]?[0-9]+?$`)
longFloatRegex = regexp.MustCompile(`^[-+]?[0-9]+\.?[0-9]*l[-+]?[0-9]+?$`)
intRxs = []*regexp.Regexp{
nil, nil,
regexp.MustCompile(`^[-+]?[0-1]+\.?$`),
regexp.MustCompile(`^[-+]?[0-2]+\.?$`),
regexp.MustCompile(`^[-+]?[0-3]+\.?$`),
regexp.MustCompile(`^[-+]?[0-4]+\.?$`),
regexp.MustCompile(`^[-+]?[0-5]+\.?$`),
regexp.MustCompile(`^[-+]?[0-6]+\.?$`),
regexp.MustCompile(`^[-+]?[0-7]+\.?$`),
regexp.MustCompile(`^[-+]?[0-8]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-b]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-c]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-d]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-e]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-f]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-g]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-h]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-i]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-j]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-k]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-l]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-m]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-n]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-o]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-p]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-q]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-r]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-s]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-t]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-u]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-v]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-w]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-x]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-y]+\.?$`),
regexp.MustCompile(`^[-+]?[0-9a-z]+\.?$`),
}
ratioRxs = []*regexp.Regexp{
nil, nil,
regexp.MustCompile(`^[-+]?[0-1]+/[-+]?[0-1]+$`),
regexp.MustCompile(`^[-+]?[0-2]+/[-+]?[0-2]+$`),
regexp.MustCompile(`^[-+]?[0-3]+/[-+]?[0-3]+$`),
regexp.MustCompile(`^[-+]?[0-4]+/[-+]?[0-4]+$`),
regexp.MustCompile(`^[-+]?[0-5]+/[-+]?[0-5]+$`),
regexp.MustCompile(`^[-+]?[0-6]+/[-+]?[0-6]+$`),
regexp.MustCompile(`^[-+]?[0-7]+/[-+]?[0-7]+$`),
regexp.MustCompile(`^[-+]?[0-8]+/[-+]?[0-8]+$`),
regexp.MustCompile(`^[-+]?[0-9]+/[-+]?[0-9]+$`),
regexp.MustCompile(`^[-+]?[0-9a]+/[-+]?[0-9a]+$`),
regexp.MustCompile(`^[-+]?[0-9a-b]+/[-+]?[0-9a-b]+$`),
regexp.MustCompile(`^[-+]?[0-9a-c]+/[-+]?[0-9a-c]+$`),
regexp.MustCompile(`^[-+]?[0-9a-d]+/[-+]?[0-9a-d]+$`),
regexp.MustCompile(`^[-+]?[0-9a-e]+/[-+]?[0-9a-e]+$`),
regexp.MustCompile(`^[-+]?[0-9a-f]+/[-+]?[0-9a-f]+$`),
regexp.MustCompile(`^[-+]?[0-9a-g]+/[-+]?[0-9a-g]+$`),
regexp.MustCompile(`^[-+]?[0-9a-h]+/[-+]?[0-9a-h]+$`),
regexp.MustCompile(`^[-+]?[0-9a-i]+/[-+]?[0-9a-i]+$`),
regexp.MustCompile(`^[-+]?[0-9a-j]+/[-+]?[0-9a-j]+$`),
regexp.MustCompile(`^[-+]?[0-9a-k]+/[-+]?[0-9a-k]+$`),
regexp.MustCompile(`^[-+]?[0-9a-l]+/[-+]?[0-9a-l]+$`),
regexp.MustCompile(`^[-+]?[0-9a-m]+/[-+]?[0-9a-m]+$`),
regexp.MustCompile(`^[-+]?[0-9a-n]+/[-+]?[0-9a-n]+$`),
regexp.MustCompile(`^[-+]?[0-9a-o]+/[-+]?[0-9a-o]+$`),
regexp.MustCompile(`^[-+]?[0-9a-p]+/[-+]?[0-9a-p]+$`),
regexp.MustCompile(`^[-+]?[0-9a-q]+/[-+]?[0-9a-q]+$`),
regexp.MustCompile(`^[-+]?[0-9a-r]+/[-+]?[0-9a-r]+$`),
regexp.MustCompile(`^[-+]?[0-9a-s]+/[-+]?[0-9a-s]+$`),
regexp.MustCompile(`^[-+]?[0-9a-t]+/[-+]?[0-9a-t]+$`),
regexp.MustCompile(`^[-+]?[0-9a-u]+/[-+]?[0-9a-u]+$`),
regexp.MustCompile(`^[-+]?[0-9a-v]+/[-+]?[0-9a-v]+$`),
regexp.MustCompile(`^[-+]?[0-9a-w]+/[-+]?[0-9a-w]+$`),
regexp.MustCompile(`^[-+]?[0-9a-x]+/[-+]?[0-9a-x]+$`),
regexp.MustCompile(`^[-+]?[0-9a-y]+/[-+]?[0-9a-y]+$`),
regexp.MustCompile(`^[-+]?[0-9a-z]+/[-+]?[0-9a-z]+$`),
}
)
// Code is a list of S-Expressions read from LISP source code. It is a means
// of keeping loaded code together so that it can be evaluated and optimized
// for subsequent evaluations.
type Code []Object
type reader struct {
tokenStart int
mode string
nextMode string
stack []Object
starts []int
carry []byte // carry over from previous stream read
buf []byte
line int
lineStart int
pos int
base int // temp base
rbase int // read base
sharpNum int
code Code
rb []byte
rn rune
rcnt int
intRx *regexp.Regexp
ratioRx *regexp.Regexp
floatType Symbol
one bool
more bool // more to read
}
func (r *reader) scoped(s *Scope) {
r.rbase = 10
r.intRx = intRxs[10]
r.ratioRx = ratioRxs[10]
if num, ok := s.get("*read-base*").(Fixnum); ok && 1 < num && num <= 36 {
r.rbase = int(num)
r.intRx = intRxs[num]
r.ratioRx = ratioRxs[num]
}
ff, _ := s.get("*read-default-float-format*").(Symbol)
switch ff {
case SingleFloatSymbol, ShortFloatSymbol, DoubleFloatSymbol, LongFloatSymbol:
r.floatType = ff
default: // default to double-float
r.floatType = DoubleFloatSymbol
}
}
// ReadString reads LISP source code and return a Code instance.
func ReadString(src string, s *Scope) (code Code) {
cr := reader{
mode: valueMode,
nextMode: valueMode,
}
cr.scoped(s)
cr.read([]byte(src))
return cr.code
}
// Read LISP source code and return a Code instance.
func Read(src []byte, s *Scope) (code Code) {
cr := reader{
mode: valueMode,
nextMode: valueMode,
}
cr.scoped(s)
cr.read(src)
return cr.code
}
// ReadOne LISP source code and return a Code instance.
func ReadOne(src []byte, s *Scope) (code Code, pos int) {
cr := reader{
mode: valueMode,
nextMode: valueMode,
one: true,
}
cr.scoped(s)
cr.read(src)
return cr.code, cr.pos
}
const readBlockSize = 65536
// ReadStream reads LISP source code from a stream and return a Code instance.
func ReadStream(r io.Reader, s *Scope, one ...bool) (Code, int) {
// Note: Using a separate chan for reading from disk was tried but since a
// new buffer had to be created each time the overall performance was
// slightly worse. That would be different with slower disks but that is
// more a issue left behind years ago.
cr := reader{
more: true,
mode: valueMode,
nextMode: valueMode,
one: (0 < len(one) && one[0]),
}
cr.scoped(s)
var pos int
buf := make([]byte, readBlockSize)
for {
cnt, err := r.Read(buf)
src := buf[:cnt]
cr.tokenStart = 0
if err != nil {
if errors.Is(err, io.EOF) {
cr.more = false
} else {
panic(err)
}
}
cr.read(src)
pos += cr.pos
if cr.one && 0 < len(cr.code) {
break
}
if !cr.more {
break
}
}
return cr.code, pos
}
// ReadStreamPush reads LISP source code from a stream and pushes
// s-expressions read onto a channel.
func ReadStreamPush(r io.Reader, s *Scope, channel chan Object) {
cr := reader{
more: true,
mode: valueMode,
nextMode: valueMode,
}
cr.scoped(s)
buf := make([]byte, readBlockSize)
for {
cnt, err := r.Read(buf)
src := buf[:cnt]
cr.tokenStart = 0
if err != nil {
if errors.Is(err, io.EOF) {
cr.more = false
} else {
panic(err)
}
}
cr.read(src)
if 0 < len(cr.code) {
for _, obj := range cr.code {
channel <- obj
}
cr.code = cr.code[:0]
}
if !cr.more {
break
}
}
}
// ReadStreamEach reads LISP source code from a stream and calls the callback
// for each s-expressions read.
func ReadStreamEach(r io.Reader, s *Scope, caller Caller) {
cr := reader{
more: true,
mode: valueMode,
nextMode: valueMode,
}
cr.scoped(s)
buf := make([]byte, readBlockSize)
for {
cnt, err := r.Read(buf)
src := buf[:cnt]
cr.tokenStart = 0
if err != nil {
if errors.Is(err, io.EOF) {
cr.more = false
} else {
panic(err)
}
}
cr.read(src)
if 0 < len(cr.code) {
for _, obj := range cr.code {
_ = caller.Call(s, List{obj}, 0)
}
cr.code = cr.code[:0]
}
if !cr.more {
break
}
}
}
// CompileString LISP string source code and return an Object.
func CompileString(src string, s *Scope) Object {
return Compile([]byte(src), s)
}
// Compile LISP source code and return an Object.
func Compile(src []byte, s *Scope) (result Object) {
cr := reader{
mode: valueMode,
nextMode: valueMode,
}
cr.scoped(s)
cr.read(src)
cr.code.Compile()
if 0 < len(cr.code) {
result = cr.code[len(cr.code)-1]
}
return
}
func (r *reader) runeAppendByte(b byte) {
r.rn = r.rn<<4 | rune(b)
r.rcnt--
if r.rcnt == 0 {
if len(r.rb) < 8 {
r.rb = make([]byte, 8)
}
n := utf8.EncodeRune(r.rb, r.rn)
r.buf = append(r.buf, r.rb[:n]...)
r.mode = r.nextMode
}
}
func (r *reader) read(src []byte) {
var b byte
// If src is empty then the for loop will not set r.pos so initialize to
// -1 to keep r.pos where it should be.
r.pos = -1
for r.pos, b = range src {
Retry:
switch r.mode[b] {
case skipNewline:
r.line++
r.lineStart = r.pos
case skipByte:
// skip
case commentByte:
r.mode = commentMode
case commentDone:
r.mode = valueMode
case openParen:
r.starts = append(r.starts, len(r.stack))
r.stack = append(r.stack, nil)
case closeParen:
r.closeList()
case tokenStart:
if r.mode != tokenMode {
r.tokenStart = r.pos
r.mode = tokenMode
}
case tokenDone:
r.pushToken(src)
r.mode = valueMode
goto Retry
case doubleQuote:
r.tokenStart = r.pos + 1
r.buf = r.buf[:0]
r.mode = stringMode
r.nextMode = stringMode
case pipeByte:
r.tokenStart = r.pos + 1
r.buf = r.buf[:0]
r.mode = symbolMode
r.nextMode = symbolMode
case stringByte:
if 0 < len(r.buf) {
r.buf = append(r.buf, b)
}
case stringDone:
var obj Object
if 0 < len(r.buf) {
obj = String(r.buf)
} else {
obj = String(src[r.tokenStart:r.pos])
}
if 0 < len(r.stack) {
r.stack = append(r.stack, obj)
} else {
r.code = append(r.code, obj)
}
r.mode = valueMode
case pipeDone:
var obj Object
if 0 < len(r.buf) {
obj = Symbol(r.buf)
} else {
obj = Symbol(src[r.tokenStart:r.pos])
}
if 0 < len(r.stack) {
r.stack = append(r.stack, obj)
} else {
r.code = append(r.code, obj)
}
r.mode = valueMode
case escByte:
if len(r.buf) == 0 && r.tokenStart < r.pos {
r.buf = append(r.buf, src[r.tokenStart:r.pos]...)
}
r.mode = escMode
case escOne:
r.buf = append(r.buf, escByteMap[b])
r.mode = r.nextMode
case escUnicode4:
r.rn = 0
r.rcnt = 4
r.mode = runeMode
case escUnicode8:
r.rn = 0
r.rcnt = 8
r.mode = runeMode
case runeDigit:
r.runeAppendByte(b - '0')
case runeHexA:
r.runeAppendByte(b - 'A' + 10)
case runeHexa:
r.runeAppendByte(b - 'a' + 10)
case sharpByte:
r.mode = sharpMode
case charSlash:
r.tokenStart = r.pos + 1
r.mode = charMode
case charDone:
r.pushChar(src)
r.mode = valueMode
goto Retry
case vectorByte:
r.starts = append(r.starts, len(r.stack))
r.stack = append(r.stack, vectorMarker)
r.mode = valueMode
case binaryByte:
r.tokenStart = r.pos + 1
r.mode = intMode
r.base = 2
case octByte:
r.tokenStart = r.pos + 1
r.mode = intMode
r.base = 8
case hexByte:
r.tokenStart = r.pos + 1
r.mode = intMode
r.base = 16
case intDone:
r.pushInteger(src)
r.mode = valueMode
goto Retry
case sharpIntByte:
r.mode = sharpNumMode
r.sharpNum = int(b - '0')
case sharpNumByte:
r.sharpNum = r.sharpNum*10 + int(b-'0')
case radixByte:
r.tokenStart = r.pos + 1
r.mode = intMode
r.base = r.sharpNum
case sharpComplex:
r.starts = append(r.starts, len(r.stack))
r.stack = append(r.stack, complexMarker)
r.mode = mustArrayMode
case arrayByte:
r.starts = append(r.starts, len(r.stack))
switch r.sharpNum {
case 0:
r.stack = append(r.stack, &Array{elementType: TrueSymbol})
case 1:
r.stack = append(r.stack, vectorMarker)
default:
if ArrayMaxRank < r.sharpNum {
r.raise("%d exceeds the maximum Array rank of %d dimensions.", r.sharpNum, ArrayMaxRank)
}
r.stack = append(r.stack, &Array{
elementType: TrueSymbol,
dims: make([]int, r.sharpNum),
sizes: make([]int, r.sharpNum),
})
}
r.mode = mustArrayMode
case swallowOpen:
r.mode = valueMode
case singleQuote:
r.stack = append(r.stack, quoteMarker)
case sharpQuote:
r.stack = append(r.stack, sharpQuoteMarker)
r.mode = valueMode
case backquote:
r.stack = append(r.stack, backquoteMarker)
case comma:
if !r.inBackquote() {
r.raise("comma not inside a backquote")
}
r.stack = append(r.stack, commaMarker)
case commaAt:
if 0 < len(r.stack) && r.stack[len(r.stack)-1] == commaMarker {
r.stack[len(r.stack)-1] = commaAtMarker
} else if r.mode != tokenMode {
r.tokenStart = r.pos
r.mode = tokenMode
}
case blockStart:
r.mode = blockCommentMode
case blockEnd0:
r.mode = blockEndMode
case bitVectorByte:
r.tokenStart = r.pos + 1
r.mode = bitVectorMode
case bitVectorDone:
token := r.makeToken(src)
if 0 < len(r.stack) {
r.stack = append(r.stack, ReadBitVector(token))
} else {
r.code = append(r.code, ReadBitVector(token))
}
r.mode = valueMode
goto Retry
default:
switch r.mode {
case sharpMode:
r.raise("illegal sharp macro character: #%c", b)
case intMode:
r.raise("illegal base %d digit: #%c", r.base, b)
default:
r.raise("unexpected character: '%c' (0x%02x)", b, b)
}
}
if r.one && 0 < len(r.code) {
if b == ')' {
r.pos++
}
return
}
}
r.pos++
if r.more {
r.carry = append(r.carry, src[r.tokenStart:r.pos]...)
} else {
switch r.mode {
case tokenMode:
r.pushToken(src)
case stringMode:
r.partial("string not terminated")
case runeMode:
r.raise("rune not terminated")
case escMode:
r.raise("escaped character not terminated")
case symbolMode:
r.raise("|symbol| not terminated")
case charMode:
r.pushChar(src)
case intMode:
r.pushInteger(src)
}
if 0 < len(r.stack) {
r.partial("list not terminated")
}
}
}
func (r *reader) inBackquote() bool {
for _, v := range r.stack {
if backquoteMarker == v {
return true
}
}
return false
}
func (r *reader) raise(format string, args ...any) {
f := make([]byte, 0, len(format)+9)
f = append(f, format...)
f = append(f, " at %d:%d"...)
args = append(args, r.line, r.pos-r.lineStart)
ParsePanic(NewScope(), 0, string(f), args...)
}
func (r *reader) partial(format string, args ...any) {
f := make([]byte, 0, len(format)+9)
f = append(f, format...)
f = append(f, " at %d:%d"...)
args = append(args, r.line, r.pos-r.lineStart)
panic(NewPartial(len(r.starts), string(f), args...))
}
func (r *reader) closeList() {
if len(r.starts) == 0 {
r.raise("unmatched close parenthesis")
}
start := r.starts[len(r.starts)-1]
size := len(r.stack) - start - 1
list := make(List, size)
copy(list, r.stack[start+1:])
// TBD does the stack need to be cleared (set to nil) before shrinking?
r.stack = r.stack[:start+1]
var obj Object
switch to := r.stack[start].(type) {
case *Vector:
obj = NewVector(len(list), TrueSymbol, nil, list, true)
case *Array:
to.calcAndSet(list)
obj = to
case Complex:
obj = newComplex(list)
default:
if 3 <= len(list) && list[len(list)-2] == Symbol(".") {
if list[len(list)-1] == nil {
list[len(list)-2] = nil
} else {
list[len(list)-2] = Tail{Value: list[len(list)-1]}
}
list = list[:len(list)-1]
obj = list
} else {
obj = list
}
if 0 < start {
switch r.stack[start-1] {
case quoteMarker:
if newQuote == nil {
newQuote = CLPkg.GetFunc("quote").Create
}
obj = newQuote(List{obj})
start--
r.stack[start] = nil
r.stack = r.stack[:start+1]
case sharpQuoteMarker:
if newSharpQuote == nil {
newSharpQuote = CLPkg.GetFunc("function").Create
}
obj = newSharpQuote(List{obj})
start--
r.stack[start] = nil
r.stack = r.stack[:start+1]
case backquoteMarker:
if newBackquote == nil {
newBackquote = CLPkg.GetFunc("backquote").Create
}
obj = newBackquote(List{obj})
start--
r.stack[start] = nil
r.stack = r.stack[:start+1]
case commaMarker:
if newComma == nil {
newComma = CLPkg.GetFunc("comma").Create
}
obj = newComma(List{obj})
start--
r.stack[start] = nil
r.stack = r.stack[:start+1]
case commaAtMarker:
if newCommaAt == nil {
newCommaAt = CLPkg.GetFunc("comma-at").Create
}
obj = newCommaAt(List{obj})
start--
r.stack[start] = nil
r.stack = r.stack[:start+1]
}
}
}
if 0 < start {
r.stack[start] = obj
r.starts = r.starts[:len(r.starts)-1]
} else {
r.stack = r.stack[:0]
r.starts = r.starts[:0]
r.code = append(r.code, obj)
}
}
// Converts tokens to the correct type and then pushes that value onto the
// stack.
func (r *reader) pushToken(src []byte) {
size := r.pos - r.tokenStart
var (
obj Object
token []byte
)
if size == 1 && (src[r.tokenStart] == 't' || src[r.tokenStart] == 'T') {
obj = True
goto Push
}
token = r.makeToken(src)
if size == 3 && bytes.EqualFold([]byte("nil"), token) {
obj = nil
goto Push
}
if 0 < len(r.stack) {
switch r.stack[len(r.stack)-1] {
case quoteMarker:
if newQuote == nil {
newQuote = CLPkg.GetFunc("quote").Create
}
if len(r.stack) == 1 {
r.code = append(r.code, newQuote(List{Symbol(token)}))
r.stack[len(r.stack)-1] = nil
r.stack = r.stack[:0]
} else {
r.stack[len(r.stack)-1] = newQuote(List{Symbol(token)})
}
return
case sharpQuoteMarker:
if newSharpQuote == nil {
newSharpQuote = CLPkg.GetFunc("function").Create
}
if len(r.stack) == 1 {
r.code = append(r.code, newSharpQuote(List{Symbol(token)}))
r.stack[len(r.stack)-1] = nil
r.stack = r.stack[:0]
} else {
r.stack[len(r.stack)-1] = newSharpQuote(List{Symbol(token)})
}
return
case backquoteMarker:
if newBackquote == nil {
newBackquote = CLPkg.GetFunc("backquote").Create
}
if len(r.stack) == 1 {
r.code = append(r.code, newBackquote(List{Symbol(token)}))
r.stack[len(r.stack)-1] = nil
r.stack = r.stack[:0]
} else {
r.stack[len(r.stack)-1] = newBackquote(List{Symbol(token)})
}
return
case commaMarker:
if newComma == nil {
newComma = CLPkg.GetFunc("comma").Create
}
if len(r.stack) == 1 {