-
Notifications
You must be signed in to change notification settings - Fork 12
/
Goldfish.tmu
20105 lines (11000 loc) · 456 KB
/
Goldfish.tmu
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
<TMU|<tuple|1.0.5|1.2.9.7>>
<style|<tuple|book|chinese|literate|goldfish|reduced-margins|guile|smart-ref|preview-ref|python>>
<\body>
<\hide-preamble>
<assign|scm|<macro|body|<goldfish-lang|<arg|body>>>>
<assign|scm-chunk|<\macro|name|prev|next|body>
<\generic-chunk|<arg|name>|<arg|prev>|<arg|next>>
<with|mode|prog|prog-language|goldfish|font-family|rm|<arg|body>>
</generic-chunk>
</macro>>
<assign|subparagraph|<macro|title|<assign|subparagraph-numbered|<compound|subparagraph-display-numbers>><assign|subparagraph-prefix|<macro|<compound|the-subparagraph>.>><compound|next-subparagraph><compound|subparagraph-clean><compound|subparagraph-header|<arg|title>><compound|subparagraph-toc|<arg|title>><small|<if|<value|subparagraph-numbered>|<compound|subparagraph-numbered-title|<arg|title>>|<compound|subparagraph-unnumbered-title|<arg|title>>>>>>
<assign|r7rs|<flag|R7RS|dark cyan>>
<assign|srfi|<flag|SRFI|dark red>>
<assign|goldfish-version|17.11.0>
<assign|typehint|<macro|body|<goldfish-lang|<arg|body>>>>
<assign|font|math=Latin Modern Math,cjk=Noto CJK SC,CMU>
</hide-preamble>
<part|语言和数据>
<chapter|金鱼Scheme简介>
\;
金鱼Scheme,又称Goldfish Scheme,是三鲤网络发起的Scheme解释器实现。
金鱼Scheme解释器具有以下特性:
<\itemize>
<item>兼容R7RS small标准
<item>提供类似Python的标准库
<item>小巧且快速
</itemize>
<section|背景>
金鱼Scheme创建之初的目的是为了服务墨干理工套件及其商业版Liii STEM。
墨干一开始使用的是S7 Scheme,然而S7 Scheme并不严格遵循R7RS small标准,也没有提供常见的SRFI实现。故而我们决定在S7 Scheme的基础上,发布金鱼Scheme,更加严格地遵循R7RS small标准,并提供常见的SRFI实现作为金鱼Scheme的内置标准库。另外,无论是R7RS small还是SRFI都没有提供同时兼容macOS, GNU/Linux和Windows的类似于Python的os模块的标准库,故而我们模仿了Python的os模块,实现金鱼的内置标准库<scm|(liii os)>,以提供<scm|listdir>、<scm|mkdir>等常见功能。
<section|文学编程>
金鱼Scheme最初的三位作者是沈达、刘念和Yansong Li,项目立项时就是采用文学编程的方式实现金鱼Scheme的Scheme部分。
一开始,我们的文学编程文档<shell|Goldfish.tmu>是不开源的,我们只开源了金鱼Scheme的代码,其中代码是几乎没有任何注释的,因为我们在文学编程文档中的代码块之外会撰写注释。后来来自Emacs China社区的StarSugar开始向金鱼Scheme项目贡献代码,于是我们决定将金鱼Scheme的文学编程文档<shell|Goldfish.tmu>也开源出来,整个项目以开源的方式推进。
从金鱼Scheme V17.10.8开始,我们确定了金鱼Scheme的文学编程文档的许可证为<with|font-series|bold|知识共享署名-相同方式共享4.0国际许可协议>。
本文档是采用中文编写的。在2024年,开源软件项目的文档采用中文编写并不是主流。首先,金鱼Scheme项目的前三位作者的母语都是中文,采用母语作为文档的语言是自然而然的;其次,在大模型时代,计算机辅助翻译已经非常成熟且廉价,我们相信中文文档不会是金鱼Scheme走向世界的阻碍。
<section|许可证>
本文档采用文学编程方式编写,金鱼Scheme相关的所有代码几乎都包含在本文档中。本文档的许可证分为代码和文档两部分,其中代码部分采用Apache许可证,相关信息在每一个源代码文件的头部都会显式声明,未显式声明的源代码默认采用Apache许可证;其中文档部分采用<with|font-series|bold|知识共享署名-相同方式共享4.0国际许可协议>授权。您可以自由地分享、修改本作品,只要您遵循以下条件:
<\description>
<item*|署名>您必须给出适当的署名,提供作品的链接,并指明是否有任何变更。您可以以任何合理的方式做这些事,但不得以任何方式暗示许可人赞同您或您的使用。
<item*|相同方式共享>如果您再混合、转换或基于本作品创作,您必须以相同的许可协议发布其成果。
</description>
金鱼Scheme是浙江三鲤网络科技有限公司发起的开源项目,我们在分发源代码时,版权信息统一采用The Goldfish Scheme Authors作为金鱼Scheme的作者。那么谁是The Goldfish Scheme Authors呢?这些信息我们会在金鱼Scheme的仓库的根目录下的<shell|AUTHORS>文件中维护:
<\verbatim-chunk|AUTHORS|false|false>
# The original authors of the SRFI reference IMPL are kept in indivisual files
# This AUTHORS file is for The Goldfish Scheme Authors.
#
# Names should be added to this file like so:
# Name or Organization \<less\>email address\<gtr\>
\;
Liii Network Inc. \<less\>*@liii.pro\<gtr\>
沈达 \<less\>da@liii.pro\<gtr\>
刘念 \<less\>nian@liii.pro\<gtr\>
\;
Yansong Li \<less\>haggittli@gmail.com\<gtr\>
Leiyu He \<less\>heleiyu1231@gmail.com\<gtr\>
Duolei Wang \<less\>duolei.wang@gmail.com\<gtr\>
Yingyao Zhou \<less\>yingyaozhou51@gmail.com\<gtr\>
赵淑婷 \<less\>shuting.zhao@outlook.com\<gtr\>
Shen Wei \<less\>weishen1900@gmail.com\<gtr\>
Noctis Zhang \<less\>noctis@umass.edu\<gtr\>
Andy Yu \<less\>andy87654@outlook.com\<gtr\>
\;
</verbatim-chunk>
我们会联系金鱼Scheme的贡献者,更新<shell|AUTHORS>文件。金鱼Scheme的贡献者并不一定在这个文件中,有可能我们没有及时更新,也有可能相关贡献者希望保持匿名。
<section|相关机构>
支持金鱼Scheme的相关机构如下所示:
<\itemize>
<item>浙江三鲤网络科技有限公司
<item>中国科学技术大学-德清阿尔法创新研究院
</itemize>
<chapter|boot.scm><label|chapter:scheme_boot>
S7 Scheme默认并不遵循R7RS,在启动S7 Scheme之后,我们需要做的第一件事就是加载<scm|boot.scm>,实现R7RS的<scm|define-library>和<scm|import>。
<value|r7rs><paragraph|file-exists?><index|file-exists?>
<\scm-chunk|goldfish/scheme/boot.scm|false|true>
(define (file-exists? path)
\ \ (if (string? path)
\ \ \ \ (if (not (g_access path 0)) ; F_OK
\ \ \ \ \ \ #f
\ \ \ \ \ \ (if (g_access path 4) ; R_OK
\ \ \ \ \ \ \ \ \ \ #t
\ \ \ \ \ \ \ \ \ \ (error 'permission-error (string-append "No permission: " path))))
\ \ \ \ (error 'type-error "(file-exists? path): path should be string")))
\;
</scm-chunk>
<value|r7rs><paragraph|delete-file><index|delete-file>
<\scm-chunk|goldfish/scheme/boot.scm|true|true>
(define (delete-file path)
\ \ (if (not (string? path))
\ \ \ \ (error 'type-error "(delete-file path): path should be string")
\ \ \ \ (if (not (file-exists? path))
\ \ \ \ \ \ (error 'read-error (string-append path " does not exist"))
\ \ \ \ \ \ (g_delete-file path))))
\;
</scm-chunk>
<value|r7rs><paragraph|define-library><index|define-library>
<\scm-chunk|goldfish/scheme/boot.scm|true|true>
; 0-clause BSD
; Adapted from S7 Scheme's r7rs.scm
(define-macro (define-library libname . body) ; \|(lib name)\| -\<gtr\> environment
\ \ `(define ,(symbol (object-\<gtr\>string libname))
\ \ \ \ \ (with-let (sublet (unlet)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cons 'import import)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cons '*export* ())
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cons 'export (define-macro (,(gensym) . names)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ `(set! *export* (append ',names *export*)))))
\ \ \ \ \ \ \ ,@body
\ \ \ \ \ \ \ (apply inlet
\ \ \ \ \ \ \ \ \ \ \ \ \ \ (map (lambda (entry)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (if (or (member (car entry) '(*export* export import))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (and (pair? *export*)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (not (member (car entry) *export*))))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (values)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ entry))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (curlet))))))
\;
(unless (defined? 'r7rs-import-library-filename)
\ \ (define (r7rs-import-library-filename libs)
\ \ \ \ (when (pair? libs)
\ \ \ \ \ \ (let ((lib-filename (let loop ((lib (if (memq (caar libs) '(only except prefix rename))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cadar libs)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (car libs)))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (name ""))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (set! name (string-append name (symbol-\<gtr\>string (car lib))))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (if (null? (cdr lib))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (string-append name ".scm")
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (begin
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (set! name (string-append name "/"))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (loop (cdr lib) name))))))
\ \ \ \ \ \ \ \ (unless (member lib-filename (*s7* 'file-names))
\ \ \ \ \ \ \ \ \ \ (load lib-filename)))
\ \ \ \ \ \ (r7rs-import-library-filename (cdr libs)))))
\;
</scm-chunk>
<value|r7rs><paragraph|import><index|import>
<\scm-chunk|goldfish/scheme/boot.scm|true|false>
(define-macro (import . libs)
\ \ `(begin
\ \ \ \ \ (r7rs-import-library-filename ',libs)
\ \ \ \ \ (varlet (curlet)
\ \ \ \ \ \ \ ,@(map (lambda (lib)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (case (car lib)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ((only)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ `((lambda (e names)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (apply inlet
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (map (lambda (name)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cons name (e name)))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ names)))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (symbol-\<gtr\>value (symbol (object-\<gtr\>string (cadr ',lib))))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cddr ',lib)))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ((except)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ `((lambda (e names)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (apply inlet
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (map (lambda (entry)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (if (member (car entry) names)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (values)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ entry))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ e)))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (symbol-\<gtr\>value (symbol (object-\<gtr\>string (cadr ',lib))))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cddr ',lib)))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ((prefix)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ `((lambda (e prefx)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (apply inlet
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (map (lambda (entry)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cons (string-\<gtr\>symbol\
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (string-append (symbol-\<gtr\>string prefx)\
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (symbol-\<gtr\>string (car entry))))\
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cdr entry)))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ e)))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (symbol-\<gtr\>value (symbol (object-\<gtr\>string (cadr ',lib))))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (caddr ',lib)))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ((rename)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ `((lambda (e names)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (apply inlet
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (map (lambda (entry)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (let ((info (assoc (car entry) names)))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (if info
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cons (cadr info) (cdr entry))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ entry)))\
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ e)))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (symbol-\<gtr\>value (symbol (object-\<gtr\>string (cadr ',lib))))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cddr ',lib)))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (else
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ `(let ((sym (symbol (object-\<gtr\>string ',lib))))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (if (not (defined? sym))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (format () "~A not loaded~%" sym)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (symbol-\<gtr\>value sym))))))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ libs))))
\;
</scm-chunk>
<chapter|(liii base)><label|chapter:liii_base>
\;
Goldfish Scheme解释器默认会加载<scm|(liii base)>。如果不想默认加载<scm|(liii base)>,请使用s7、r7rs或者sicp模式。
<scm|(liii base)>由以下函数库组成:
<\description>
<item*|(scheme base)>由R7RS定义的Scheme基础函数库
<item*|(srfi srfi-2)>由SRFI-2定义的<scm|and-let*>
<item*|(srfi srfi-8)>由SRFI-8定义的<scm|receive>
<item*|(liii base)>三鲤扩展函数和S7内置的非R7RS函数,例如<scm|display*>
</description>
<section|许可证>
<\scm-chunk|goldfish/scheme/base.scm|false|true>
;
; Copyright (C) 2024 The Goldfish Scheme Authors
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
; License for the specific language governing permissions and limitations
; under the License.
;
\;
</scm-chunk>
<\scm-chunk|goldfish/liii/base.scm|false|true>
;
; Copyright (C) 2024 The Goldfish Scheme Authors
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
; License for the specific language governing permissions and limitations
; under the License.
;
\;
</scm-chunk>
\;
<\scm-chunk|tests/goldfish/liii/base-test.scm|false|true>
;
; Copyright (C) 2024 The Goldfish Scheme Authors
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
; License for the specific language governing permissions and limitations
; under the License.
;
\;
</scm-chunk>
<section|接口>
<\scm-chunk|goldfish/scheme/base.scm|true|true>
(define-library (scheme base)
(export
\ \ let-values
\ \ ; R7RS 5: Program Structure
\ \ define-values define-record-type
\ \ ; R7RS 6.2: Numbers
\ \ square exact inexact floor s7-floor ceiling s7-ceiling truncate s7-truncate
\ \ round s7-round floor-quotient gcd lcm s7-lcm boolean=?
\ \ ; R7RS 6.4: list
\ \ pair? cons car cdr set-car! set-cdr! caar cadr cdar cddr
\ \ null? list? make-list list length append reverse list-tail
\ \ list-ref list-set! memq memv member assq assv assoc list-copy
\ \ ; R7RS 6.5: Symbol
\ \ symbol? symbol=? string-\<gtr\>symbol symbol-\<gtr\>string
\ \ ; R7RS 6.6: Characters
\ \ digit-value
\ \ ; R7RS 6.7: String
\ \ string-copy
\ \ ; R7RS 6.8: Vector
\ \ vector-\<gtr\>string string-\<gtr\>vector vector-copy vector-copy! vector-fill!
\ \ ; R7RS 6.9: Bytevectors
\ \ bytevector? make-bytevector bytevector bytevector-length bytevector-u8-ref
\ \ bytevector-u8-set! bytevector-append utf8-\<gtr\>string string-\<gtr\>utf8 u8-string-length
\ \ ; Input and Output
\ \ call-with-port port? binary-port? textual-port? input-port-open? output-port-open?
\ \ open-binary-input-file open-binary-output-file close-port eof-object
\ \ ; Control flow
\ \ string-map vector-map string-for-each vector-for-each
\ \ ; Exception
\ \ raise guard read-error? file-error?)
(begin
\;
</scm-chunk>
<\scm-chunk|goldfish/liii/base.scm|true|true>
(define-library (liii base)
(import (scheme base)
\ \ \ \ \ \ \ \ (srfi srfi-2)
\ \ \ \ \ \ \ \ (srfi srfi-8))
(export
\ \ ; (scheme base) defined by R7RS
\ \ let-values
\ \ ; R7RS 5: Program Structure
\ \ define-values define-record-type
\ \ ; R7RS 6.2: Numbers
\ \ square exact inexact floor s7-floor ceiling s7-ceiling truncate s7-truncate
\ \ round s7-round floor-quotient gcd lcm s7-lcm
\ \ ; R7RS 6.3: Booleans
\ \ boolean=?
\ \ ; R7RS 6.4: list
\ \ pair? cons car cdr set-car! set-cdr! caar cadr cdar cddr
\ \ null? list? make-list list length append reverse list-tail
\ \ list-ref list-set! memq memv member assq assv assoc list-copy
\ \ ; R7RS 6.5: Symbol
\ \ symbol? symbol=? string-\<gtr\>symbol symbol-\<gtr\>string
\ \ ; R7RS 6.6: Characters
\ \ digit-value
\ \ ; R7RS 6.7: String
\ \ string-copy
\ \ ; R7RS 6.8 Vector
\ \ vector-\<gtr\>string string-\<gtr\>vector vector-copy vector-copy! vector-fill!
\ \ ; R7RS 6.9 Bytevectors
\ \ bytevector? make-bytevector bytevector bytevector-length bytevector-u8-ref
\ \ bytevector-u8-set! bytevector-append utf8-\<gtr\>string string-\<gtr\>utf8 u8-string-length
\ \ u8-substring
\ \ ; Input and Output
\ \ call-with-port port? binary-port? textual-port? input-port-open? output-port-open?
\ \ open-binary-input-file open-binary-output-file close-port eof-object
\ \ ; Control flow
\ \ string-map vector-map string-for-each vector-for-each
\ \ ; Exception
\ \ raise guard read-error? file-error?
\ \ ; SRFI-2
\ \ and-let*
\ \ ; SRFI-8
\ \ receive
\ \ ; Extra routines for (liii base)
\ \ == != display* in? let1 compose identity typed-lambda
)
(begin
\;
</scm-chunk>
<section|测试>
<\scm-chunk|tests/goldfish/liii/base-test.scm|true|true>
(import (liii check)
\ \ \ \ \ \ \ \ (liii base)
\ \ \ \ \ \ \ \ (liii list))
\;
(check-set-mode! 'report-failed)
\;
</scm-chunk>
<section|表达式>
本节对应R7RS的第四节:表达式。
<subsection|原语表达式>
原语表达式可以分为:变量、字面量、函数的应用、匿名函数、分支表达式、赋值表达式、文件包含表达式。
<paragraph|lambda>
<paragraph|quote>
<paragraph|if>
<paragraph|set!>
<paragraph|include>
<paragraph|include-ci>
\;
<subsection|派生表达式>
<paragraph|cond>
<value|r7rs><paragraph|case><index|case>
<\scm-chunk|tests/goldfish/liii/base-test.scm|true|true>
(check (case '+
\ \ \ \ \ \ \ \ \ ((+ -) 'p0)
\ \ \ \ \ \ \ \ \ ((* /) 'p1))
\ \ =\<gtr\> 'p0)
\;
(check (case '-
\ \ \ \ \ \ \ \ \ ((+ -) 'p0)
\ \ \ \ \ \ \ \ \ ((* /) 'p1))
\ \ =\<gtr\> 'p0)
\;
(check (case '*
\ \ \ \ \ \ \ \ \ ((+ -) 'p0)
\ \ \ \ \ \ \ \ \ ((* /) 'p1))
\ \ =\<gtr\> 'p1)
\;
(check (case '@
\ \ \ \ \ \ \ \ \ ((+ -) 'p0)
\ \ \ \ \ \ \ \ \ ((* /) 'p1))
\ \ =\<gtr\> #\<less\>unspecified\<gtr\>)
\;
(check (case '&
\ \ \ \ \ \ \ \ \ ((+ -) 'p0)
\ \ \ \ \ \ \ \ \ ((* /) 'p1))
\ \ =\<gtr\> #\<less\>unspecified\<gtr\>)
\;
</scm-chunk>
<value|r7rs><paragraph|and><index|and>
检查 <code*|and> 是否正确处理多个布尔表达式。
<\scm-chunk|tests/goldfish/liii/base-test.scm|true|true>
(check-true (and #t #t #t))
(check-false (and #t #f #t))
(check-false (and #f #t #f))
(check-false (and #f #f #f))
\;
</scm-chunk>
验证当 <code*|and> 没有参数时的行为。
<\scm-chunk|tests/goldfish/liii/base-test.scm|true|true>
(check-true (and))
\;
</scm-chunk>
测试 <code*|and> 与混合类型参数的组合。
<\scm-chunk|tests/goldfish/liii/base-test.scm|true|true>
(check-true (and 1 '() "non-empty" #t))
(check-false (and #f '() "non-empty" #t))
(check-false (and 1 '() "non-empty" #f))
\;
</scm-chunk>
检查 <code*|and> 在复合表达式中的行为。
<\scm-chunk|tests/goldfish/liii/base-test.scm|true|true>
(check-true (and (\<gtr\> 5 3) (\<less\> 5 10)))
(check-false (and (\<gtr\> 5 3) (\<gtr\> 5 10)))
\;
</scm-chunk>
验证 <code*|and> 的短路行为。
<\scm-chunk|tests/goldfish/liii/base-test.scm|true|true>
(check-catch 'error-name
\ \ (and (error 'error-name "This should not be evaluated") #f))
(check-false (and #f (error "This should not be evaluated")))
\;
</scm-chunk>
验证and返回值非布尔值的情况。
<\scm-chunk|tests/goldfish/liii/base-test.scm|true|true>
(check (and #t 1) =\<gtr\> 1)
\;
</scm-chunk>
<value|r7rs><paragraph|or><index|or>
<value|r7rs><paragraph|when><index|when>
<value|r7rs><paragraph|unless><index|unless>
<value|r7rs><paragraph|let><index|let>
<value|r7rs><paragraph|let*><index|let*>
<value|r7rs><paragraph|letrec>
<\scm-chunk|tests/goldfish/liii/base-test.scm|true|true>
(define (test-letrec)
\ \ (letrec ((even?
\ \ \ \ \ \ \ \ \ \ \ \ \ (lambda (n)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (if (= n 0)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ #t
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (odd? (- n 1)))))
\ \ \ \ \ \ \ \ \ \ \ \ (odd?
\ \ \ \ \ \ \ \ \ \ \ \ \ (lambda (n)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (if (= n 0)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ #f
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (even? (- n 1))))))
\ \ \ \ (list (even? 10) (odd? 10))))
\;
(check (test-letrec) =\<gtr\> (list #t #f))
\;
(check-catch 'wrong-type-arg
\ \ (letrec ((a 1) (b (+ a 1))) (list a b)))
\;
</scm-chunk>
<value|r7rs><paragraph|letrec*><index|letrec*>
<\scm-chunk|tests/goldfish/liii/base-test.scm|true|true>
(check
\ \ (letrec* ((a 1) (b (+ a 1))) (list a b))
\ \ =\<gtr\> (list 1 2))
\;
</scm-chunk>
\;
<value|r7rs><paragraph|let-values><index|let-values>
<\scm-chunk|goldfish/scheme/base.scm|true|true>
; 0-clause BSD
; Bill Schottstaedt
; from S7 source repo: r7rs.scm
(define-macro (let-values vars . body)
\ \ (if (and (pair? vars)
\ \ \ \ \ \ \ \ \ \ \ (pair? (car vars))
\ \ \ \ \ \ \ \ \ \ \ (null? (cdar vars)))
\ \ \ \ \ \ `((lambda ,(caar vars)
\ \ \ \ \ \ \ \ \ \ ,@body)
\ \ \ \ \ \ \ \ ,(cadar vars))
\ \ \ \ \ \ `(with-let
\ \ \ \ \ \ \ \ (apply sublet (curlet)
\ \ \ \ \ \ \ \ \ \ (list
\ \ \ \ \ \ \ \ \ \ \ \ ,@(map
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (lambda (v)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ `((lambda ,(car v)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (values ,@(map (lambda (name)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (values (symbol-\<gtr\>keyword name) name))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (let args-\<gtr\>proper-list ((args (car v)))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cond ((symbol? args)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (list args))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ((not (pair? args))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ args)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ((pair? (car args))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cons (caar args)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (args-\<gtr\>proper-list (cdr args))))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (else
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cons (car args)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (args-\<gtr\>proper-list (cdr args)))))))))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ,(cadr v)))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ vars)))
\ \ \ \ \ \ \ \ ,@body)))
\;
</scm-chunk>
<\scm-chunk|tests/goldfish/liii/base-test.scm|true|true>
(check (let-values (((ret) (+ 1 2))) (+ ret 4)) =\<gtr\> 7)
(check (let-values (((a b) (values 3 4))) (+ a b)) =\<gtr\> 7)
\;
</scm-chunk>
<value|r7rs><paragraph|let*-values>
<paragraph|and-let*><index|and-let*>
<\scm-chunk|tests/goldfish/liii/base-test.scm|true|true>
(check (and-let* ((hi 3) (ho #f)) (+ hi 1)) =\<gtr\> #f)
(check (and-let* ((hi 3) (ho #t)) (+ hi 1)) =\<gtr\> 4)
\;
</scm-chunk>
<subparagraph|SRFI>
<\scm-chunk|goldfish/srfi/srfi-2.scm|false|false>
; 0-clause BSD by Bill Schottstaedt from S7 source repo: s7test.scm
(define-library (srfi srfi-2)
(export and-let*)
(begin
\;
(define-macro (and-let* vars . body)
\ \ `(let () (and ,@(map (lambda (v) `(define ,@v)) vars) (begin ,@body))))
\;
) ; end of begin
) ; end of define-library
\;
</scm-chunk>
<value|r7rs><paragraph|do><index|do>
检查 <scm|do> 是否能够正确执行迭代并在结束时返回正确的值。
<\scm-chunk|tests/goldfish/liii/base-test.scm|true|true>
(check
\ \ (do ((i 0 (+ i 1)))
\ \ \ \ \ \ ((= i 5) i))
\ \ =\<gtr\> 5)
\;
</scm-chunk>
检查 <scm|do> 是否能正确处理多个变量的绑定和更新。