-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.scm
6627 lines (5824 loc) · 223 KB
/
library.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
;;;; library.scm - R5RS library for the CHICKEN compiler
;
; Copyright (c) 2008-2021, The CHICKEN Team
; Copyright (c) 2000-2007, Felix L. Winkelmann
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
; conditions are met:
;
; Redistributions of source code must retain the above copyright notice, this list of conditions and the following
; disclaimer.
; Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
; disclaimer in the documentation and/or other materials provided with the distribution.
; Neither the name of the author nor the names of its contributors may be used to endorse or promote
; products derived from this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
; AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
; POSSIBILITY OF SUCH DAMAGE.
(declare
(unit library)
(uses build-version)
(disable-interrupts)
(hide ##sys#dynamic-unwind
##sys#vector-resize ##sys#default-parameter-vector
current-print-length setter-tag
##sys#print-exit
##sys#format-here-doc-warning
exit-in-progress cleanup-before-exit chicken.base#cleanup-tasks
maximal-string-length find-ratio-between find-ratio
make-complex flonum->ratnum ratnum
+maximum-allowed-exponent+ mantexp->dbl ldexp round-quotient
##sys#string->compnum ##sys#internal-gcd)
(not inline chicken.base#sleep-hook ##sys#change-directory-hook
##sys#user-read-hook ##sys#error-hook ##sys#signal-hook
##sys#default-read-info-hook ##sys#infix-list-hook
##sys#sharp-number-hook ##sys#user-print-hook
##sys#user-interrupt-hook ##sys#windows-platform
##sys#schedule ##sys#features)
(foreign-declare #<<EOF
#include <errno.h>
#include <float.h>
#ifdef HAVE_SYSEXITS_H
# include <sysexits.h>
#endif
#ifndef EX_SOFTWARE
# define EX_SOFTWARE 70
#endif
#define C_close_file(p) (C_fclose((C_FILEPTR)(C_port_file(p))), C_SCHEME_UNDEFINED)
#define C_a_f64peek(ptr, c, b, i) C_flonum(ptr, ((double *)C_data_pointer(b))[ C_unfix(i) ])
#define C_fetch_c_strlen(b, i) C_fix(strlen((C_char *)C_block_item(b, C_unfix(i))))
#define C_asciiz_strlen(str) C_fix(strlen(C_c_string(str)))
#define C_peek_c_string(b, i, to, len) (C_memcpy(C_data_pointer(to), (C_char *)C_block_item(b, C_unfix(i)), C_unfix(len)), C_SCHEME_UNDEFINED)
#define C_free_mptr(p, i) (C_free((void *)C_block_item(p, C_unfix(i))), C_SCHEME_UNDEFINED)
#define C_free_sptr(p, i) (C_free((void *)(((C_char **)C_block_item(p, 0))[ C_unfix(i) ])), C_SCHEME_UNDEFINED)
#define C_a_get_current_seconds(ptr, c, dummy) C_int64_to_num(ptr, time(NULL))
#define C_peek_c_string_at(ptr, i) ((C_char *)(((C_char **)ptr)[ i ]))
static C_word
fast_read_line_from_file(C_word str, C_word port, C_word size) {
int n = C_unfix(size);
int i;
int c;
char *buf = C_c_string(str);
C_FILEPTR fp = C_port_file(port);
if ((c = C_getc(fp)) == EOF) {
if (ferror(fp)) {
clearerr(fp);
return C_fix(-1);
} else { /* feof (fp) */
return C_SCHEME_END_OF_FILE;
}
}
C_ungetc(c, fp);
for (i = 0; i < n; i++) {
c = C_getc(fp);
if(c == EOF && ferror(fp)) {
clearerr(fp);
return C_fix(-(i + 1));
}
switch (c) {
case '\r': if ((c = C_getc(fp)) != '\n') C_ungetc(c, fp);
case EOF: clearerr(fp);
case '\n': return C_fix(i);
}
buf[i] = c;
}
return C_SCHEME_FALSE;
}
static C_word
fast_read_string_from_file(C_word dest, C_word port, C_word len, C_word pos)
{
size_t m;
int n = C_unfix (len);
char * buf = ((char *)C_data_pointer (dest) + C_unfix (pos));
C_FILEPTR fp = C_port_file (port);
if(feof(fp)) return C_SCHEME_END_OF_FILE;
m = fread (buf, sizeof (char), n, fp);
if (m < n) {
if (ferror(fp)) /* Report to Scheme, which may retry, so clear errors */
clearerr(fp);
else if (feof(fp) && 0 == m) /* eof but m > 0? Return data first, below */
return C_SCHEME_END_OF_FILE; /* Calling again will get us here */
}
return C_fix (m);
}
static C_word
shallow_equal(C_word x, C_word y)
{
/* assumes x and y are non-immediate */
int i, len = C_header_size(x);
if(C_header_size(y) != len) return C_SCHEME_FALSE;
else return C_mk_bool(!C_memcmp((void *)x, (void *)y, len * sizeof(C_word)));
}
static C_word
signal_debug_event(C_word mode, C_word msg, C_word args)
{
C_DEBUG_INFO cell;
C_word av[ 3 ];
cell.enabled = 1;
cell.event = C_DEBUG_SIGNAL;
cell.loc = "";
cell.val = "";
av[ 0 ] = mode;
av[ 1 ] = msg;
av[ 2 ] = args;
C_debugger(&cell, 3, av);
return C_SCHEME_UNDEFINED;
}
#ifdef NO_DLOAD2
# define HAVE_DLOAD 0
#else
# define HAVE_DLOAD 1
#endif
#ifdef C_ENABLE_PTABLES
# define HAVE_PTABLES 1
#else
# define HAVE_PTABLES 0
#endif
#ifdef C_GC_HOOKS
# define HAVE_GCHOOKS 1
#else
# define HAVE_GCHOOKS 0
#endif
#if defined(C_CROSS_CHICKEN) && C_CROSS_CHICKEN
# define IS_CROSS_CHICKEN 1
#else
# define IS_CROSS_CHICKEN 0
#endif
EOF
) )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; NOTE: Modules defined here will typically exclude syntax
;; definitions, those are handled by expand.scm or modules.scm.
;; Handwritten import libraries (or a special-case module in
;; modules.scm for scheme) contain the value exports merged with
;; syntactic exports. The upshot of this is that any module that
;; refers to another module defined *earlier* in this file cannot use
;; macros from the earlier module!
;;
;; We get around this problem by using the "chicken.internal.syntax"
;; module, which is baked in and exports *every* available core macro.
;; See modules.scm, expand.scm and chicken-syntax.scm for details.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre-declaration of scheme, so it can be used later on. We only use
;; scheme macros and core language forms in here, to avoid a cyclic
;; dependency on itself. All actual definitions are set! below.
;; Also, this declaration is incomplete: the module itself is defined
;; as a primitive module due to syntax exports, which are missing
;; here. See modules.scm for the full definition.
(module scheme
(;; [syntax]
;; We are reexporting these because otherwise the module here
;; will be inconsistent with the built-in one, and be void of
;; syntax definitions, causing problems below.
begin and case cond define define-syntax delay do lambda
if let let* let-syntax letrec letrec-syntax or
quasiquote quote set! syntax-rules
not boolean? eq? eqv? equal? pair?
cons car cdr caar cadr cdar cddr caaar caadr cadar caddr cdaar
cdadr cddar cdddr caaaar caaadr caadar caaddr cadaar cadadr
caddar cadddr cdaaar cdaadr cdadar cdaddr cddaar cddadr cdddar
cddddr set-car! set-cdr!
null? list? list length list-tail list-ref append reverse memq memv
member assq assv assoc symbol? symbol->string string->symbol number?
integer? exact? real? complex? inexact? rational? zero? odd? even?
positive? negative? max min + - * / = > < >= <= quotient remainder
modulo gcd lcm abs floor ceiling truncate round rationalize
exact->inexact inexact->exact exp log expt sqrt
sin cos tan asin acos atan
number->string string->number char? char=? char>? char<? char>=?
char<=? char-ci=? char-ci<? char-ci>? char-ci>=? char-ci<=?
char-alphabetic? char-whitespace? char-numeric? char-upper-case?
char-lower-case? char-upcase char-downcase
char->integer integer->char
string? string=? string>? string<? string>=? string<=? string-ci=?
string-ci<? string-ci>? string-ci>=? string-ci<=? make-string
string-length string-ref string-set! string-append string-copy
string->list list->string substring string-fill! vector? make-vector
vector-ref vector-set! string vector vector-length vector->list
list->vector vector-fill! procedure? map for-each apply force
call-with-current-continuation input-port? output-port?
current-input-port current-output-port call-with-input-file
call-with-output-file open-input-file open-output-file
close-input-port close-output-port
read read-char peek-char write display write-char newline
eof-object? with-input-from-file with-output-to-file
char-ready? imag-part real-part make-rectangular make-polar angle
magnitude numerator denominator values call-with-values dynamic-wind
;; The following procedures are overwritten in eval.scm:
eval interaction-environment null-environment
scheme-report-environment load)
(import chicken.internal.syntax) ;; See note above
;;; Operations on booleans:
(define (not x) (##core#inline "C_i_not" x))
(define (boolean? x) (##core#inline "C_booleanp" x))
;;; Equivalence predicates:
(define (eq? x y) (##core#inline "C_eqp" x y))
(define (eqv? x y) (##core#inline "C_i_eqvp" x y))
(define (equal? x y) (##core#inline "C_i_equalp" x y))
;;; Pairs and lists:
(define (pair? x) (##core#inline "C_i_pairp" x))
(define (cons x y) (##core#inline_allocate ("C_a_i_cons" 3) x y))
(define (car x) (##core#inline "C_i_car" x))
(define (cdr x) (##core#inline "C_i_cdr" x))
(define (set-car! x y) (##core#inline "C_i_set_car" x y))
(define (set-cdr! x y) (##core#inline "C_i_set_cdr" x y))
(define (cadr x) (##core#inline "C_i_cadr" x))
(define (caddr x) (##core#inline "C_i_caddr" x))
(define (cadddr x) (##core#inline "C_i_cadddr" x))
(define (cddddr x) (##core#inline "C_i_cddddr" x))
(define (caar x) (##core#inline "C_i_caar" x))
(define (cdar x) (##core#inline "C_i_cdar" x))
(define (cddr x) (##core#inline "C_i_cddr" x))
(define (caaar x) (car (car (car x))))
(define (caadr x) (car (##core#inline "C_i_cadr" x)))
(define (cadar x) (##core#inline "C_i_cadr" (car x)))
(define (cdaar x) (cdr (car (car x))))
(define (cdadr x) (cdr (##core#inline "C_i_cadr" x)))
(define (cddar x) (cdr (cdr (car x))))
(define (cdddr x) (cdr (cdr (cdr x))))
(define (caaaar x) (car (car (car (car x)))))
(define (caaadr x) (car (car (##core#inline "C_i_cadr" x))))
(define (caadar x) (car (##core#inline "C_i_cadr" (car x))))
(define (caaddr x) (car (##core#inline "C_i_caddr" x)))
(define (cadaar x) (##core#inline "C_i_cadr" (car (car x))))
(define (cadadr x) (##core#inline "C_i_cadr" (##core#inline "C_i_cadr" x)))
(define (caddar x) (##core#inline "C_i_caddr" (car x)))
(define (cdaaar x) (cdr (car (car (car x)))))
(define (cdaadr x) (cdr (car (##core#inline "C_i_cadr" x))))
(define (cdadar x) (cdr (##core#inline "C_i_cadr" (car x))))
(define (cdaddr x) (cdr (##core#inline "C_i_caddr" x)))
(define (cddaar x) (cdr (cdr (car (car x)))))
(define (cddadr x) (cdr (cdr (##core#inline "C_i_cadr" x))))
(define (cdddar x) (cdr (cdr (cdr (car x)))))
(define (null? x) (eq? x '()))
(define (list . lst) lst)
(define (length lst) (##core#inline "C_i_length" lst))
(define (list-tail lst i) (##core#inline "C_i_list_tail" lst i))
(define (list-ref lst i) (##core#inline "C_i_list_ref" lst i))
(define append)
(define (reverse lst0)
(let loop ((lst lst0) (rest '()))
(cond ((eq? lst '()) rest)
((pair? lst)
(loop (##sys#slot lst 1) (cons (##sys#slot lst 0) rest)) )
(else (##sys#error-not-a-proper-list lst0 'reverse)) ) ))
(define (memq x lst) (##core#inline "C_i_memq" x lst))
(define (memv x lst) (##core#inline "C_i_memv" x lst))
(define (member x lst) (##core#inline "C_i_member" x lst))
(define (assq x lst) (##core#inline "C_i_assq" x lst))
(define (assv x lst) (##core#inline "C_i_assv" x lst))
(define (assoc x lst) (##core#inline "C_i_assoc" x lst))
(define (list? x) (##core#inline "C_i_listp" x))
;;; Strings:
(define make-string)
(define (string? x) (##core#inline "C_i_stringp" x))
(define (string-length s) (##core#inline "C_i_string_length" s))
(define (string-ref s i) (##core#inline "C_i_string_ref" s i))
(define (string-set! s i c) (##core#inline "C_i_string_set" s i c))
(define (string=? x y)
(##core#inline "C_i_string_equal_p" x y))
(define (string-ci=? x y) (##core#inline "C_i_string_ci_equal_p" x y))
(define string->list)
(define list->string)
(define string-fill)
(define string-copy)
(define substring)
(define string-fill!)
(define string<?)
(define string>?)
(define string<=?)
(define string>=?)
(define string-ci<?)
(define string-ci>?)
(define string-ci<=?)
(define string-ci>=?)
(define string)
(define string-append)
;; Complex numbers
(define make-rectangular)
(define make-polar)
(define real-part)
(define imag-part)
(define angle)
(define magnitude)
;; Rational numbers
(define numerator)
(define denominator)
(define inexact->exact)
(define (exact->inexact x)
(##core#inline_allocate ("C_a_i_exact_to_inexact" 12) x))
;; Numerical operations
(define (abs x) (##core#inline_allocate ("C_s_a_i_abs" 7) x))
(define + (##core#primitive "C_plus"))
(define - (##core#primitive "C_minus"))
(define * (##core#primitive "C_times"))
(define /)
(define floor)
(define ceiling)
(define truncate)
(define round)
(define rationalize)
(define (quotient a b) (##core#inline_allocate ("C_s_a_i_quotient" 5) a b))
(define (remainder a b) (##core#inline_allocate ("C_s_a_i_remainder" 5) a b))
(define (modulo a b) (##core#inline_allocate ("C_s_a_i_modulo" 5) a b))
(define (even? n) (##core#inline "C_i_evenp" n))
(define (odd? n) (##core#inline "C_i_oddp" n))
(define max)
(define min)
(define exp)
(define log)
(define sin)
(define cos)
(define tan)
(define asin)
(define acos)
(define atan)
(define sqrt)
(define expt)
(define gcd)
(define lcm)
(define = (##core#primitive "C_nequalp"))
(define > (##core#primitive "C_greaterp"))
(define < (##core#primitive "C_lessp"))
(define >= (##core#primitive "C_greater_or_equal_p"))
(define <= (##core#primitive "C_less_or_equal_p"))
(define (number? x) (##core#inline "C_i_numberp" x))
(define complex? number?)
(define (real? x) (##core#inline "C_i_realp" x))
(define (rational? n) (##core#inline "C_i_rationalp" n))
(define (integer? x) (##core#inline "C_i_integerp" x))
(define (exact? x) (##core#inline "C_i_exactp" x))
(define (inexact? x) (##core#inline "C_i_inexactp" x))
(define (zero? n) (##core#inline "C_i_zerop" n))
(define (positive? n) (##core#inline "C_i_positivep" n))
(define (negative? n) (##core#inline "C_i_negativep" n))
(define number->string (##core#primitive "C_number_to_string"))
(define string->number)
;;; Symbols:
(define (symbol? x) (##core#inline "C_i_symbolp" x))
(define symbol->string)
(define string->symbol)
;;; Vectors:
(define (vector? x) (##core#inline "C_i_vectorp" x))
(define (vector-length v) (##core#inline "C_i_vector_length" v))
(define (vector-ref v i) (##core#inline "C_i_vector_ref" v i))
(define (vector-set! v i x) (##core#inline "C_i_vector_set" v i x))
(define make-vector)
(define list->vector)
(define vector->list)
(define vector)
(define vector-fill!)
;;; Characters:
(define (char? x) (##core#inline "C_charp" x))
(define (char->integer c)
(##sys#check-char c 'char->integer)
(##core#inline "C_fix" (##core#inline "C_character_code" c)) )
(define (integer->char n)
(##sys#check-fixnum n 'integer->char)
(##core#inline "C_make_character" (##core#inline "C_unfix" n)) )
(define (char=? c1 c2) (##core#inline "C_i_char_equalp" c1 c2))
(define (char>? c1 c2) (##core#inline "C_i_char_greaterp" c1 c2))
(define (char<? c1 c2) (##core#inline "C_i_char_lessp" c1 c2))
(define (char>=? c1 c2) (##core#inline "C_i_char_greater_or_equal_p" c1 c2))
(define (char<=? c1 c2) (##core#inline "C_i_char_less_or_equal_p" c1 c2))
(define (char-upcase c)
(##sys#check-char c 'char-upcase)
(##core#inline "C_u_i_char_upcase" c))
(define (char-downcase c)
(##sys#check-char c 'char-downcase)
(##core#inline "C_u_i_char_downcase" c))
(define char-ci=?)
(define char-ci>?)
(define char-ci<?)
(define char-ci>=?)
(define char-ci<=?)
(define (char-upper-case? c)
(##sys#check-char c 'char-upper-case?)
(##core#inline "C_u_i_char_upper_casep" c) )
(define (char-lower-case? c)
(##sys#check-char c 'char-lower-case?)
(##core#inline "C_u_i_char_lower_casep" c) )
(define (char-numeric? c)
(##sys#check-char c 'char-numeric?)
(##core#inline "C_u_i_char_numericp" c) )
(define (char-whitespace? c)
(##sys#check-char c 'char-whitespace?)
(##core#inline "C_u_i_char_whitespacep" c) )
(define (char-alphabetic? c)
(##sys#check-char c 'char-alphabetic?)
(##core#inline "C_u_i_char_alphabeticp" c) )
;;; Procedures:
(define (procedure? x) (##core#inline "C_i_closurep" x))
(define apply (##core#primitive "C_apply"))
(define values (##core#primitive "C_values"))
(define call-with-values (##core#primitive "C_call_with_values"))
(define call-with-current-continuation)
;;; Ports:
(define (input-port? x)
(and (##core#inline "C_blockp" x)
(##core#inline "C_input_portp" x)))
(define (output-port? x)
(and (##core#inline "C_blockp" x)
(##core#inline "C_output_portp" x)))
(define current-input-port)
(define current-output-port)
(define open-input-file)
(define open-output-file)
(define close-input-port)
(define close-output-port)
(define call-with-input-file)
(define call-with-output-file)
(define with-input-from-file)
(define with-output-to-file)
;;; Input:
(define (eof-object? x) (##core#inline "C_eofp" x))
(define char-ready?)
(define read-char)
(define peek-char)
(define read)
;;; Output:
(define write-char)
(define newline)
(define write)
(define display)
;;; Evaluation environments:
;; All of the stuff below is overwritten with their "real"
;; implementations by chicken.eval (see eval.scm)
(define (eval x . env)
(##sys#error 'eval "`eval' is not defined - the `eval' unit was probably not linked with this executable"))
(define (interaction-environment)
(##sys#error 'interaction-environment "`interaction-environment' is not defined - the `eval' unit was probably not linked with this executable"))
(define (scheme-report-environment n)
(##sys#error 'scheme-report-environment "`scheme-report-environment' is not defined - the `eval' unit was probably not linked with this executable"))
(define (null-environment)
(##sys#error 'null-environment "`null-environment' is not defined - the `eval' unit was probably not linked with this executable"))
(define (load filename . evaluator)
(##sys#error 'load "`load' is not defined - the `eval' unit was probably not linked with this executable"))
;; Other stuff:
(define force)
(define for-each)
(define map)
(define dynamic-wind)
) ; scheme
(import scheme)
;; Pre-declaration of chicken.base, so it can be used later on. Much
;; like the "scheme" module, most declarations will be set! further
;; down in this file, mostly to avoid a cyclic dependency on itself.
;; The full definition (with macros) is in its own import library.
(module chicken.base
(;; [syntax] and-let* case-lambda cut cute declare define-constant
;; define-inline define-record define-record-type
;; define-record-printer define-values delay-force fluid-let include
;; include-relative let-optionals let-values let*-values letrec*
;; letrec-values nth-value optional parameterize rec receive
;; require-library require-extension set!-values syntax unless when
bignum? flonum? fixnum? ratnum? cplxnum? finite? infinite? nan?
exact-integer? exact-integer-sqrt exact-integer-nth-root
port? port-closed? input-port-open? output-port-open? flush-output
get-output-string open-input-string open-output-string
get-call-chain print print* add1 sub1 sleep call/cc
current-error-port error void gensym print-call-chain
make-promise promise? char-name enable-warnings
equal=? finite? foldl foldr getter-with-setter make-parameter
notice procedure-information setter signum string->uninterned-symbol
subvector symbol-append vector-copy! vector-resize
warning quotient&remainder quotient&modulo
record-printer set-record-printer!
alist-ref alist-update alist-update! rassoc atom? butlast chop
compress flatten intersperse join list-of? tail? constantly
complement compose conjoin disjoin each flip identity o
case-sensitive keyword-style parentheses-synonyms symbol-escape
on-exit exit exit-handler implicit-exit-handler emergency-exit
)
(import scheme chicken.internal.syntax)
(define (fixnum? x) (##core#inline "C_fixnump" x))
(define (flonum? x) (##core#inline "C_i_flonump" x))
(define (bignum? x) (##core#inline "C_i_bignump" x))
(define (ratnum? x) (##core#inline "C_i_ratnump" x))
(define (cplxnum? x) (##core#inline "C_i_cplxnump" x))
(define (exact-integer? x) (##core#inline "C_i_exact_integerp" x))
(define exact-integer-sqrt)
(define exact-integer-nth-root)
(define quotient&remainder (##core#primitive "C_quotient_and_remainder"))
;; Modulo's sign follows y (whereas remainder's sign follows x)
;; Inlining this is not much use: quotient&remainder is primitive
(define (quotient&modulo x y)
(call-with-values (lambda () (quotient&remainder x y))
(lambda (div rem)
(if (positive? y)
(if (negative? rem)
(values div (+ rem y))
(values div rem))
(if (positive? rem)
(values div (+ rem y))
(values div rem))))))
(define (finite? x) (##core#inline "C_i_finitep" x))
(define (infinite? x) (##core#inline "C_i_infinitep" x))
(define (nan? x) (##core#inline "C_i_nanp" x))
(define signum (##core#primitive "C_signum"))
(define equal=?)
(define get-call-chain)
(define print-call-chain)
(define print)
(define print*)
(define (add1 n) (+ n 1))
(define (sub1 n) (- n 1))
(define current-error-port)
(define (error . args)
(if (pair? args)
(apply ##sys#signal-hook #:error args)
(##sys#signal-hook #:error #f)))
(define (void . _) (##core#undefined))
(define sleep)
(define call/cc)
(define char-name)
(define enable-warnings)
; (define enable-notices)???
(define getter-with-setter)
(define make-parameter)
(define procedure-information)
(define setter)
(define string->uninterned-symbol)
(define record-printer)
(define set-record-printer!)
(define gensym)
(define vector-copy!)
(define subvector)
(define vector-resize)
(define symbol-append)
(define warning)
(define notice)
(define port?)
(define port-closed?)
(define input-port-open?)
(define output-port-open?)
(define get-output-string)
(define open-input-string)
(define open-output-string)
(define flush-output)
;;; Promises:
(define (promise? x)
(##sys#structure? x 'promise))
(define (##sys#make-promise proc)
(##sys#make-structure 'promise proc))
(define (make-promise obj)
(if (promise? obj) obj
(##sys#make-promise (lambda () obj))))
;;; fast folds with correct argument order
(define (foldl f z lst)
(##sys#check-list lst 'foldl)
(let loop ((lst lst) (z z))
(if (not (pair? lst))
z
(loop (##sys#slot lst 1) (f z (##sys#slot lst 0))))))
(define (foldr f z lst)
(##sys#check-list lst 'foldr)
(let loop ((lst lst))
(if (not (pair? lst))
z
(f (##sys#slot lst 0) (loop (##sys#slot lst 1))))))
;;; Exit:
(define implicit-exit-handler)
(define exit-handler)
(define chicken.base#cleanup-tasks '())
(define (on-exit thunk)
(set! cleanup-tasks (cons thunk chicken.base#cleanup-tasks)))
(define (exit #!optional (code 0))
((exit-handler) code))
(define (emergency-exit #!optional (code 0))
(##sys#check-fixnum code 'emergency-exit)
(##core#inline "C_exit_runtime" code))
;;; Parameters:
(define case-sensitive)
(define keyword-style)
(define parentheses-synonyms)
(define symbol-escape)
;;; Combinators:
(define (identity x) x)
(define (conjoin . preds)
(lambda (x)
(let loop ((preds preds))
(or (null? preds)
(and ((##sys#slot preds 0) x)
(loop (##sys#slot preds 1)) ) ) ) ) )
(define (disjoin . preds)
(lambda (x)
(let loop ((preds preds))
(and (not (null? preds))
(or ((##sys#slot preds 0) x)
(loop (##sys#slot preds 1)) ) ) ) ) )
(define (constantly . xs)
(if (eq? 1 (length xs))
(let ((x (car xs)))
(lambda _ x) )
(lambda _ (apply values xs)) ) )
(define (flip proc) (lambda (x y) (proc y x)))
(define complement
(lambda (p)
(lambda args (not (apply p args))) ) )
(define (compose . fns)
(define (rec f0 . fns)
(if (null? fns)
f0
(lambda args
(call-with-values
(lambda () (apply (apply rec fns) args))
f0) ) ) )
(if (null? fns)
values
(apply rec fns) ) )
(define (o . fns)
(if (null? fns)
identity
(let loop ((fns fns))
(let ((h (##sys#slot fns 0))
(t (##sys#slot fns 1)) )
(if (null? t)
h
(lambda (x) (h ((loop t) x))))))))
(define (list-of? pred)
(lambda (lst)
(let loop ((lst lst))
(cond ((null? lst) #t)
((not (pair? lst)) #f)
((pred (##sys#slot lst 0)) (loop (##sys#slot lst 1)))
(else #f) ) ) ) )
(define (each . procs)
(cond ((null? procs) (lambda _ (void)))
((null? (##sys#slot procs 1)) (##sys#slot procs 0))
(else
(lambda args
(let loop ((procs procs))
(let ((h (##sys#slot procs 0))
(t (##sys#slot procs 1)) )
(if (null? t)
(apply h args)
(begin
(apply h args)
(loop t) ) ) ) ) ) ) ) )
;;; List operators:
(define (atom? x) (##core#inline "C_i_not_pair_p" x))
(define (tail? x y)
(##sys#check-list y 'tail?)
(or (##core#inline "C_eqp" x '())
(let loop ((y y))
(cond ((##core#inline "C_eqp" y '()) #f)
((##core#inline "C_eqp" x y) #t)
(else (loop (##sys#slot y 1))) ) ) ) )
(define intersperse
(lambda (lst x)
(let loop ((ns lst))
(if (##core#inline "C_eqp" ns '())
ns
(let ((tail (cdr ns)))
(if (##core#inline "C_eqp" tail '())
ns
(cons (##sys#slot ns 0) (cons x (loop tail))) ) ) ) ) ) )
(define (butlast lst)
(##sys#check-pair lst 'butlast)
(let loop ((lst lst))
(let ((next (##sys#slot lst 1)))
(if (and (##core#inline "C_blockp" next) (##core#inline "C_pairp" next))
(cons (##sys#slot lst 0) (loop next))
'() ) ) ) )
(define (flatten . lists0)
(let loop ((lists lists0) (rest '()))
(cond ((null? lists) rest)
(else
(let ((head (##sys#slot lists 0))
(tail (##sys#slot lists 1)) )
(if (list? head)
(loop head (loop tail rest))
(cons head (loop tail rest)) ) ) ) ) ) )
(define chop)
(define (join lsts . lst)
(let ((lst (if (pair? lst) (car lst) '())))
(##sys#check-list lst 'join)
(let loop ((lsts lsts))
(cond ((null? lsts) '())
((not (pair? lsts))
(##sys#error-not-a-proper-list lsts) )
(else
(let ((l (##sys#slot lsts 0))
(r (##sys#slot lsts 1)) )
(if (null? r)
l
(##sys#append l lst (loop r)) ) ) ) ) ) ) )
(define compress
(lambda (blst lst)
(let ((msg "bad argument type - not a proper list"))
(##sys#check-list lst 'compress)
(let loop ((blst blst) (lst lst))
(cond ((null? blst) '())
((not (pair? blst))
(##sys#signal-hook #:type-error 'compress msg blst) )
((not (pair? lst))
(##sys#signal-hook #:type-error 'compress msg lst) )
((##sys#slot blst 0)
(cons (##sys#slot lst 0) (loop (##sys#slot blst 1) (##sys#slot lst 1))))
(else (loop (##sys#slot blst 1) (##sys#slot lst 1))) ) ) ) ) )
;;; Alists:
(define (alist-update! x y lst #!optional (cmp eqv?))
(let* ((aq (cond ((eq? eq? cmp) assq)
((eq? eqv? cmp) assv)
((eq? equal? cmp) assoc)
(else
(lambda (x lst)
(let loop ((lst lst))
(and (pair? lst)
(let ((a (##sys#slot lst 0)))
(if (and (pair? a) (cmp x (##sys#slot a 0)))
a
(loop (##sys#slot lst 1)) ) ) ) ) ) ) ) )
(item (aq x lst)) )
(if item
(begin
(##sys#setslot item 1 y)
lst)
(cons (cons x y) lst) ) ) )
(define (alist-update k v lst #!optional (cmp eqv?))
(let loop ((lst lst))
(cond ((null? lst)
(list (cons k v)))
((not (pair? lst))
(error 'alist-update "bad argument type" lst))
(else
(let ((a (##sys#slot lst 0)))
(cond ((not (pair? a))
(error 'alist-update "bad argument type" a))
((cmp k (##sys#slot a 0))
(cons (cons k v) (##sys#slot lst 1)))
(else
(cons (cons (##sys#slot a 0) (##sys#slot a 1))
(loop (##sys#slot lst 1))))))))))
(define (alist-ref x lst #!optional (cmp eqv?) (default #f))
(let* ((aq (cond ((eq? eq? cmp) assq)
((eq? eqv? cmp) assv)
((eq? equal? cmp) assoc)
(else
(lambda (x lst)
(let loop ((lst lst))
(cond
((null? lst) #f)
((pair? lst)
(let ((a (##sys#slot lst 0)))
(##sys#check-pair a 'alist-ref)
(if (cmp x (##sys#slot a 0))
a
(loop (##sys#slot lst 1)) ) ))
(else (error 'alist-ref "bad argument type" lst)) ) ) ) ) ) )
(item (aq x lst)) )
(if item
(##sys#slot item 1)
default) ) )
;; TODO: Make inlineable in C without "tst", to be more like assoc?
(define (rassoc x lst . tst)
(##sys#check-list lst 'rassoc)
(let ((tst (if (pair? tst) (car tst) eqv?)))
(let loop ((l lst))
(and (pair? l)
(let ((a (##sys#slot l 0)))
(##sys#check-pair a 'rassoc)
(if (tst x (##sys#slot a 1))
a
(loop (##sys#slot l 1)) ) ) ) ) ) )
) ; chicken.base
(import chicken.base)
(define-constant char-name-table-size 37)
(define-constant output-string-initial-size 256)
(define-constant read-line-buffer-initial-size 1024)
(define-constant default-parameter-vector-size 16)
(define maximal-string-length (foreign-value "C_HEADER_SIZE_MASK" unsigned-long))
;;; Fixnum arithmetic:
(module chicken.fixnum *
(import scheme)
(import chicken.foreign)
(define most-positive-fixnum (foreign-value "C_MOST_POSITIVE_FIXNUM" int))
(define most-negative-fixnum (foreign-value "C_MOST_NEGATIVE_FIXNUM" int))
(define fixnum-bits (foreign-value "(C_WORD_SIZE - 1)" int))
(define fixnum-precision (foreign-value "(C_WORD_SIZE - (1 + 1))" int))
(define (fx+ x y) (##core#inline "C_fixnum_plus" x y))
(define (fx- x y) (##core#inline "C_fixnum_difference" x y))
(define (fx* x y) (##core#inline "C_fixnum_times" x y))
(define (fx= x y) (eq? x y))
(define (fx> x y) (##core#inline "C_fixnum_greaterp" x y))
(define (fx< x y) (##core#inline "C_fixnum_lessp" x y))
(define (fx>= x y) (##core#inline "C_fixnum_greater_or_equal_p" x y))
(define (fx<= x y) (##core#inline "C_fixnum_less_or_equal_p" x y))
(define (fxmin x y) (##core#inline "C_i_fixnum_min" x y))
(define (fxmax x y) (##core#inline "C_i_fixnum_max" x y))
(define (fxneg x) (##core#inline "C_fixnum_negate" x))
(define (fxand x y) (##core#inline "C_fixnum_and" x y))
(define (fxior x y) (##core#inline "C_fixnum_or" x y))
(define (fxxor x y) (##core#inline "C_fixnum_xor" x y))
(define (fxnot x) (##core#inline "C_fixnum_not" x))
(define (fxshl x y) (##core#inline "C_fixnum_shift_left" x y))
(define (fxshr x y) (##core#inline "C_fixnum_shift_right" x y))
(define (fxodd? x) (##core#inline "C_i_fixnumoddp" x))
(define (fxeven? x) (##core#inline "C_i_fixnumevenp" x))
(define (fxlen x) (##core#inline "C_i_fixnum_length" x))
(define (fx/ x y) (##core#inline "C_fixnum_divide" x y) )
(define (fxgcd x y) (##core#inline "C_i_fixnum_gcd" x y))
(define (fxmod x y) (##core#inline "C_fixnum_modulo" x y) )
(define (fxrem x y) (##core#inline "C_i_fixnum_remainder_checked" x y) )
;; Overflow-detecting versions of some of the above