This repository has been archived by the owner on Jun 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheme.c
5017 lines (4464 loc) · 137 KB
/
scheme.c
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
/* T I N Y S C H E M E 1 . 4 1
* Dimitrios Souflis (dsouflis@acm.org)
* Based on MiniScheme (original credits follow)
* (MINISCM) coded by Atsushi Moriwaki (11/5/1989)
* (MINISCM) E-MAIL : moriwaki@kurims.kurims.kyoto-u.ac.jp
* (MINISCM) This version has been modified by R.C. Secrist.
* (MINISCM)
* (MINISCM) Mini-Scheme is now maintained by Akira KIDA.
* (MINISCM)
* (MINISCM) This is a revised and modified version by Akira KIDA.
* (MINISCM) current version is 0.85k4 (15 May 1994)
*
*/
#define _SCHEME_SOURCE
#include "scheme-private.h"
#ifndef WIN32
# include <unistd.h>
#endif
#ifdef WIN32
#define snprintf _snprintf
#endif
#if USE_DL
# include "dynload.h"
#endif
#if USE_MATH
# include <math.h>
#endif
#include <limits.h>
#include <float.h>
#include <ctype.h>
#if USE_STRCASECMP
#include <strings.h>
# ifndef __APPLE__
# define stricmp strcasecmp
# endif
#endif
/* Used for documentation purposes, to signal functions in 'interface' */
#define INTERFACE
#define TOK_EOF (-1)
#define TOK_LPAREN 0
#define TOK_RPAREN 1
#define TOK_DOT 2
#define TOK_ATOM 3
#define TOK_QUOTE 4
#define TOK_COMMENT 5
#define TOK_DQUOTE 6
#define TOK_BQUOTE 7
#define TOK_COMMA 8
#define TOK_ATMARK 9
#define TOK_SHARP 10
#define TOK_SHARP_CONST 11
#define TOK_VEC 12
#define BACKQUOTE '`'
#define DELIMITERS "()\";\f\t\v\n\r "
/*
* Basic memory allocation units
*/
#define banner "TinyScheme 1.41"
#include <string.h>
#include <stdlib.h>
#ifdef __APPLE__
static int stricmp(const char *s1, const char *s2)
{
unsigned char c1, c2;
do {
c1 = tolower(*s1);
c2 = tolower(*s2);
if (c1 < c2)
return -1;
else if (c1 > c2)
return 1;
s1++, s2++;
} while (c1 != 0);
return 0;
}
#endif /* __APPLE__ */
#if USE_STRLWR
static const char *strlwr(char *s) {
const char *p=s;
while(*s) {
*s=tolower(*s);
s++;
}
return p;
}
#endif
#ifndef prompt
# define prompt "ts> "
#endif
/* Include and use the hexcode char
* array version of the init.scm file
* to ensure we don't have to try and
* locate it at run time */
unsigned char init_scm[] = {
#include "init_scm.h"
};
#ifndef FIRST_CELLSEGS
# define FIRST_CELLSEGS 3
#endif
enum scheme_types {
T_STRING=1,
T_NUMBER=2,
T_SYMBOL=3,
T_PROC=4,
T_PAIR=5,
T_CLOSURE=6,
T_CONTINUATION=7,
T_FOREIGN=8,
T_CHARACTER=9,
T_PORT=10,
T_VECTOR=11,
T_MACRO=12,
T_PROMISE=13,
T_ENVIRONMENT=14,
T_LAST_SYSTEM_TYPE=14
};
/* ADJ is enough slack to align cells in a TYPE_BITS-bit boundary */
#define ADJ 32
#define TYPE_BITS 5
#define T_MASKTYPE 31 /* 0000000000011111 */
#define T_SYNTAX 4096 /* 0001000000000000 */
#define T_IMMUTABLE 8192 /* 0010000000000000 */
#define T_ATOM 16384 /* 0100000000000000 */ /* only for gc */
#define CLRATOM 49151 /* 1011111111111111 */ /* only for gc */
#define MARK 32768 /* 1000000000000000 */
#define UNMARK 32767 /* 0111111111111111 */
static num num_add(num a, num b);
static num num_mul(num a, num b);
static num num_div(num a, num b);
static num num_intdiv(num a, num b);
static num num_sub(num a, num b);
static num num_rem(num a, num b);
static num num_mod(num a, num b);
static int num_eq(num a, num b);
static int num_gt(num a, num b);
static int num_ge(num a, num b);
static int num_lt(num a, num b);
static int num_le(num a, num b);
#if USE_MATH
static double round_per_R5RS(double x);
#endif
static int is_zero_double(double x);
static INLINE int num_is_integer(pointer p) {
return ((p)->_object._number.is_fixnum);
}
static num num_zero;
static num num_one;
/* macros for cell operations */
#define typeflag(p) ((p)->_flag)
#define type(p) (typeflag(p)&T_MASKTYPE)
INTERFACE INLINE int is_string(pointer p) { return (type(p)==T_STRING); }
#define strvalue(p) ((p)->_object._string._svalue)
#define strlength(p) ((p)->_object._string._length)
INTERFACE static int is_list(scheme *sc, pointer p);
INTERFACE INLINE int is_vector(pointer p) { return (type(p)==T_VECTOR); }
INTERFACE static void fill_vector(pointer vec, pointer obj);
INTERFACE static pointer vector_elem(pointer vec, int ielem);
INTERFACE static pointer set_vector_elem(pointer vec, int ielem, pointer a);
INTERFACE INLINE int is_number(pointer p) { return (type(p)==T_NUMBER); }
INTERFACE INLINE int is_integer(pointer p) {
if (!is_number(p))
return 0;
if (num_is_integer(p) || (double)ivalue(p) == rvalue(p))
return 1;
return 0;
}
INTERFACE INLINE int is_real(pointer p) {
return is_number(p) && (!(p)->_object._number.is_fixnum);
}
INTERFACE INLINE int is_character(pointer p) { return (type(p)==T_CHARACTER); }
INTERFACE INLINE char *string_value(pointer p) { return strvalue(p); }
INLINE num nvalue(pointer p) { return ((p)->_object._number); }
INTERFACE long ivalue(pointer p) { return (num_is_integer(p)?(p)->_object._number.value.ivalue:(long)(p)->_object._number.value.rvalue); }
INTERFACE double rvalue(pointer p) { return (!num_is_integer(p)?(p)->_object._number.value.rvalue:(double)(p)->_object._number.value.ivalue); }
#define ivalue_unchecked(p) ((p)->_object._number.value.ivalue)
#define rvalue_unchecked(p) ((p)->_object._number.value.rvalue)
#define set_num_integer(p) (p)->_object._number.is_fixnum=1;
#define set_num_real(p) (p)->_object._number.is_fixnum=0;
INTERFACE long charvalue(pointer p) { return ivalue_unchecked(p); }
INTERFACE INLINE int is_port(pointer p) { return (type(p)==T_PORT); }
INTERFACE INLINE int is_inport(pointer p) { return is_port(p) && p->_object._port->kind & port_input; }
INTERFACE INLINE int is_outport(pointer p) { return is_port(p) && p->_object._port->kind & port_output; }
INTERFACE INLINE int is_pair(pointer p) { return (type(p)==T_PAIR); }
#define car(p) ((p)->_object._cons._car)
#define cdr(p) ((p)->_object._cons._cdr)
INTERFACE pointer pair_car(pointer p) { return car(p); }
INTERFACE pointer pair_cdr(pointer p) { return cdr(p); }
INTERFACE pointer set_car(pointer p, pointer q) { return car(p)=q; }
INTERFACE pointer set_cdr(pointer p, pointer q) { return cdr(p)=q; }
INTERFACE INLINE int is_symbol(pointer p) { return (type(p)==T_SYMBOL); }
INTERFACE INLINE char *symname(pointer p) { return strvalue(car(p)); }
#if USE_PLIST
SCHEME_EXPORT INLINE int hasprop(pointer p) { return (typeflag(p)&T_SYMBOL); }
#define symprop(p) cdr(p)
#endif
INTERFACE INLINE int is_syntax(pointer p) { return (typeflag(p)&T_SYNTAX); }
INTERFACE INLINE int is_proc(pointer p) { return (type(p)==T_PROC); }
INTERFACE INLINE int is_foreign(pointer p) { return (type(p)==T_FOREIGN); }
INTERFACE INLINE char *syntaxname(pointer p) { return strvalue(car(p)); }
#define procnum(p) ivalue(p)
static const char *procname(pointer x);
INTERFACE INLINE int is_closure(pointer p) { return (type(p)==T_CLOSURE); }
INTERFACE INLINE int is_macro(pointer p) { return (type(p)==T_MACRO); }
INTERFACE INLINE pointer closure_code(pointer p) { return car(p); }
INTERFACE INLINE pointer closure_env(pointer p) { return cdr(p); }
INTERFACE INLINE int is_continuation(pointer p) { return (type(p)==T_CONTINUATION); }
#define cont_dump(p) cdr(p)
/* To do: promise should be forced ONCE only */
INTERFACE INLINE int is_promise(pointer p) { return (type(p)==T_PROMISE); }
INTERFACE INLINE int is_environment(pointer p) { return (type(p)==T_ENVIRONMENT); }
#define setenvironment(p) typeflag(p) = T_ENVIRONMENT
#define is_atom(p) (typeflag(p)&T_ATOM)
#define setatom(p) typeflag(p) |= T_ATOM
#define clratom(p) typeflag(p) &= CLRATOM
#define is_mark(p) (typeflag(p)&MARK)
#define setmark(p) typeflag(p) |= MARK
#define clrmark(p) typeflag(p) &= UNMARK
INTERFACE INLINE int is_immutable(pointer p) { return (typeflag(p)&T_IMMUTABLE); }
/*#define setimmutable(p) typeflag(p) |= T_IMMUTABLE*/
INTERFACE INLINE void setimmutable(pointer p) { typeflag(p) |= T_IMMUTABLE; }
#define caar(p) car(car(p))
#define cadr(p) car(cdr(p))
#define cdar(p) cdr(car(p))
#define cddr(p) cdr(cdr(p))
#define cadar(p) car(cdr(car(p)))
#define caddr(p) car(cdr(cdr(p)))
#define cdaar(p) cdr(car(car(p)))
#define cadaar(p) car(cdr(car(car(p))))
#define cadddr(p) car(cdr(cdr(cdr(p))))
#define cddddr(p) cdr(cdr(cdr(cdr(p))))
#if USE_CHAR_CLASSIFIERS
static INLINE int Cisalpha(int c) { return isascii(c) && isalpha(c); }
static INLINE int Cisdigit(int c) { return isascii(c) && isdigit(c); }
static INLINE int Cisspace(int c) { return isascii(c) && isspace(c); }
static INLINE int Cisupper(int c) { return isascii(c) && isupper(c); }
static INLINE int Cislower(int c) { return isascii(c) && islower(c); }
#endif
#if USE_ASCII_NAMES
static const char *charnames[32]={
"nul",
"soh",
"stx",
"etx",
"eot",
"enq",
"ack",
"bel",
"bs",
"ht",
"lf",
"vt",
"ff",
"cr",
"so",
"si",
"dle",
"dc1",
"dc2",
"dc3",
"dc4",
"nak",
"syn",
"etb",
"can",
"em",
"sub",
"esc",
"fs",
"gs",
"rs",
"us"
};
static int is_ascii_name(const char *name, int *pc) {
int i;
for(i=0; i<32; i++) {
if(stricmp(name,charnames[i])==0) {
*pc=i;
return 1;
}
}
if(stricmp(name,"del")==0) {
*pc=127;
return 1;
}
return 0;
}
#endif
static int file_push(scheme *sc, const char *fname);
static void file_pop(scheme *sc);
static int file_interactive(scheme *sc);
static INLINE int is_one_of(char *s, int c);
static int alloc_cellseg(scheme *sc, int n);
static long binary_decode(const char *s);
static INLINE pointer get_cell(scheme *sc, pointer a, pointer b);
static pointer _get_cell(scheme *sc, pointer a, pointer b);
static pointer reserve_cells(scheme *sc, int n);
static pointer get_consecutive_cells(scheme *sc, int n);
static pointer find_consecutive_cells(scheme *sc, int n);
static void finalize_cell(scheme *sc, pointer a);
static int count_consecutive_cells(pointer x, int needed);
static pointer find_slot_in_env(scheme *sc, pointer env, pointer sym, int all);
static pointer mk_number(scheme *sc, num n);
static char *store_string(scheme *sc, int len, const char *str, char fill);
static pointer mk_vector(scheme *sc, int len);
static pointer mk_atom(scheme *sc, char *q);
static pointer mk_sharp_const(scheme *sc, char *name);
static pointer mk_port(scheme *sc, port *p);
static pointer port_from_filename(scheme *sc, const char *fn, int prop);
static pointer port_from_file(scheme *sc, FILE *, int prop);
static pointer port_from_string(scheme *sc, char *start, char *past_the_end, int prop);
static port *port_rep_from_filename(scheme *sc, const char *fn, int prop);
static port *port_rep_from_file(scheme *sc, FILE *, int prop);
static port *port_rep_from_string(scheme *sc, char *start, char *past_the_end, int prop);
static void port_close(scheme *sc, pointer p, int flag);
static void mark(pointer a);
static void gc(scheme *sc, pointer a, pointer b);
static int basic_inchar(port *pt);
static int inchar(scheme *sc);
static void backchar(scheme *sc, int c);
static char *readstr_upto(scheme *sc, char *delim);
static pointer readstrexp(scheme *sc);
static INLINE int skipspace(scheme *sc);
static int token(scheme *sc);
static void printslashstring(scheme *sc, char *s, int len);
static void atom2str(scheme *sc, pointer l, int f, char **pp, int *plen);
static void printatom(scheme *sc, pointer l, int f);
static pointer mk_proc(scheme *sc, enum scheme_opcodes op);
static pointer mk_closure(scheme *sc, pointer c, pointer e);
static pointer mk_continuation(scheme *sc, pointer d);
static pointer reverse(scheme *sc, pointer a);
static pointer reverse_in_place(scheme *sc, pointer term, pointer list);
static pointer revappend(scheme *sc, pointer a, pointer b);
static void dump_stack_mark(scheme *);
static pointer opexe_0(scheme *sc, enum scheme_opcodes op);
static pointer opexe_1(scheme *sc, enum scheme_opcodes op);
static pointer opexe_2(scheme *sc, enum scheme_opcodes op);
static pointer opexe_3(scheme *sc, enum scheme_opcodes op);
static pointer opexe_4(scheme *sc, enum scheme_opcodes op);
static pointer opexe_5(scheme *sc, enum scheme_opcodes op);
static pointer opexe_6(scheme *sc, enum scheme_opcodes op);
static void Eval_Cycle(scheme *sc, enum scheme_opcodes op);
static void assign_syntax(scheme *sc, char *name);
static int syntaxnum(pointer p);
static void assign_proc(scheme *sc, enum scheme_opcodes, char *name);
#define num_ivalue(n) (n.is_fixnum?(n).value.ivalue:(long)(n).value.rvalue)
#define num_rvalue(n) (!n.is_fixnum?(n).value.rvalue:(double)(n).value.ivalue)
static num num_add(num a, num b) {
num ret;
ret.is_fixnum=a.is_fixnum && b.is_fixnum;
if(ret.is_fixnum) {
ret.value.ivalue= a.value.ivalue+b.value.ivalue;
} else {
ret.value.rvalue=num_rvalue(a)+num_rvalue(b);
}
return ret;
}
static num num_mul(num a, num b) {
num ret;
ret.is_fixnum=a.is_fixnum && b.is_fixnum;
if(ret.is_fixnum) {
ret.value.ivalue= a.value.ivalue*b.value.ivalue;
} else {
ret.value.rvalue=num_rvalue(a)*num_rvalue(b);
}
return ret;
}
static num num_div(num a, num b) {
num ret;
ret.is_fixnum=a.is_fixnum && b.is_fixnum && a.value.ivalue%b.value.ivalue==0;
if(ret.is_fixnum) {
ret.value.ivalue= a.value.ivalue/b.value.ivalue;
} else {
ret.value.rvalue=num_rvalue(a)/num_rvalue(b);
}
return ret;
}
static num num_intdiv(num a, num b) {
num ret;
ret.is_fixnum=a.is_fixnum && b.is_fixnum;
if(ret.is_fixnum) {
ret.value.ivalue= a.value.ivalue/b.value.ivalue;
} else {
ret.value.rvalue=num_rvalue(a)/num_rvalue(b);
}
return ret;
}
static num num_sub(num a, num b) {
num ret;
ret.is_fixnum=a.is_fixnum && b.is_fixnum;
if(ret.is_fixnum) {
ret.value.ivalue= a.value.ivalue-b.value.ivalue;
} else {
ret.value.rvalue=num_rvalue(a)-num_rvalue(b);
}
return ret;
}
static num num_rem(num a, num b) {
num ret;
long e1, e2, res;
ret.is_fixnum=a.is_fixnum && b.is_fixnum;
e1=num_ivalue(a);
e2=num_ivalue(b);
res=e1%e2;
/* remainder should have same sign as second operand */
if (res > 0) {
if (e1 < 0) {
res -= labs(e2);
}
} else if (res < 0) {
if (e1 > 0) {
res += labs(e2);
}
}
ret.value.ivalue=res;
return ret;
}
static num num_mod(num a, num b) {
num ret;
long e1, e2, res;
ret.is_fixnum=a.is_fixnum && b.is_fixnum;
e1=num_ivalue(a);
e2=num_ivalue(b);
res=e1%e2;
/* modulo should have same sign as second operand */
if (res * e2 < 0) {
res += e2;
}
ret.value.ivalue=res;
return ret;
}
static int num_eq(num a, num b) {
int ret;
int is_fixnum=a.is_fixnum && b.is_fixnum;
if(is_fixnum) {
ret= a.value.ivalue==b.value.ivalue;
} else {
ret=num_rvalue(a)==num_rvalue(b);
}
return ret;
}
static int num_gt(num a, num b) {
int ret;
int is_fixnum=a.is_fixnum && b.is_fixnum;
if(is_fixnum) {
ret= a.value.ivalue>b.value.ivalue;
} else {
ret=num_rvalue(a)>num_rvalue(b);
}
return ret;
}
static int num_ge(num a, num b) {
return !num_lt(a,b);
}
static int num_lt(num a, num b) {
int ret;
int is_fixnum=a.is_fixnum && b.is_fixnum;
if(is_fixnum) {
ret= a.value.ivalue<b.value.ivalue;
} else {
ret=num_rvalue(a)<num_rvalue(b);
}
return ret;
}
static int num_le(num a, num b) {
return !num_gt(a,b);
}
#if USE_MATH
/* Round to nearest. Round to even if midway */
static double round_per_R5RS(double x) {
double fl=floor(x);
double ce=ceil(x);
double dfl=x-fl;
double dce=ce-x;
if(dfl>dce) {
return ce;
} else if(dfl<dce) {
return fl;
} else {
if(fmod(fl,2.0)==0.0) { /* I imagine this holds */
return fl;
} else {
return ce;
}
}
}
#endif
static int is_zero_double(double x) {
return x<DBL_MIN && x>-DBL_MIN;
}
static long binary_decode(const char *s) {
long x=0;
while(*s!=0 && (*s=='1' || *s=='0')) {
x<<=1;
x+=*s-'0';
s++;
}
return x;
}
/* allocate new cell segment */
static int alloc_cellseg(scheme *sc, int n) {
pointer newp;
pointer last;
pointer p;
char *cp;
long i;
int k;
int adj=ADJ;
if(adj<sizeof(struct cell)) {
adj=sizeof(struct cell);
}
for (k = 0; k < n; k++) {
if (sc->last_cell_seg >= CELL_NSEGMENT - 1)
return k;
cp = (char*) sc->malloc(CELL_SEGSIZE * sizeof(struct cell)+adj);
if (cp == 0)
return k;
i = ++sc->last_cell_seg ;
sc->alloc_seg[i] = cp;
/* adjust in TYPE_BITS-bit boundary */
if(((unsigned long)cp)%adj!=0) {
cp=(char*)(adj*((unsigned long)cp/adj+1));
}
/* insert new segment in address order */
newp=(pointer)cp;
sc->cell_seg[i] = newp;
while (i > 0 && sc->cell_seg[i - 1] > sc->cell_seg[i]) {
p = sc->cell_seg[i];
sc->cell_seg[i] = sc->cell_seg[i - 1];
sc->cell_seg[--i] = p;
}
sc->fcells += CELL_SEGSIZE;
last = newp + CELL_SEGSIZE - 1;
for (p = newp; p <= last; p++) {
typeflag(p) = 0;
cdr(p) = p + 1;
car(p) = sc->NIL;
}
/* insert new cells in address order on free list */
if (sc->free_cell == sc->NIL || p < sc->free_cell) {
cdr(last) = sc->free_cell;
sc->free_cell = newp;
} else {
p = sc->free_cell;
while (cdr(p) != sc->NIL && newp > cdr(p))
p = cdr(p);
cdr(last) = cdr(p);
cdr(p) = newp;
}
}
return n;
}
static INLINE pointer get_cell_x(scheme *sc, pointer a, pointer b) {
if (sc->free_cell != sc->NIL) {
pointer x = sc->free_cell;
sc->free_cell = cdr(x);
--sc->fcells;
return (x);
}
return _get_cell (sc, a, b);
}
/* get new cell. parameter a, b is marked by gc. */
static pointer _get_cell(scheme *sc, pointer a, pointer b) {
pointer x;
if(sc->no_memory) {
return sc->sink;
}
if (sc->free_cell == sc->NIL) {
const int min_to_be_recovered = sc->last_cell_seg*8;
gc(sc,a, b);
if (sc->fcells < min_to_be_recovered
|| sc->free_cell == sc->NIL) {
/* if only a few recovered, get more to avoid fruitless gc's */
if (!alloc_cellseg(sc,1) && sc->free_cell == sc->NIL) {
sc->no_memory=1;
return sc->sink;
}
}
}
x = sc->free_cell;
sc->free_cell = cdr(x);
--sc->fcells;
return (x);
}
/* make sure that there is a given number of cells free */
static pointer reserve_cells(scheme *sc, int n) {
if(sc->no_memory) {
return sc->NIL;
}
/* Are there enough cells available? */
if (sc->fcells < n) {
/* If not, try gc'ing some */
gc(sc, sc->NIL, sc->NIL);
if (sc->fcells < n) {
/* If there still aren't, try getting more heap */
if (!alloc_cellseg(sc,1)) {
sc->no_memory=1;
return sc->NIL;
}
}
if (sc->fcells < n) {
/* If all fail, report failure */
sc->no_memory=1;
return sc->NIL;
}
}
return (sc->T);
}
static pointer get_consecutive_cells(scheme *sc, int n) {
pointer x;
if(sc->no_memory) { return sc->sink; }
/* Are there any cells available? */
x=find_consecutive_cells(sc,n);
if (x != sc->NIL) { return x; }
/* If not, try gc'ing some */
gc(sc, sc->NIL, sc->NIL);
x=find_consecutive_cells(sc,n);
if (x != sc->NIL) { return x; }
/* If there still aren't, try getting more heap */
if (!alloc_cellseg(sc,1))
{
sc->no_memory=1;
return sc->sink;
}
x=find_consecutive_cells(sc,n);
if (x != sc->NIL) { return x; }
/* If all fail, report failure */
sc->no_memory=1;
return sc->sink;
}
static int count_consecutive_cells(pointer x, int needed) {
int n=1;
while(cdr(x)==x+1) {
x=cdr(x);
n++;
if(n>needed) return n;
}
return n;
}
static pointer find_consecutive_cells(scheme *sc, int n) {
pointer *pp;
int cnt;
pp=&sc->free_cell;
while(*pp!=sc->NIL) {
cnt=count_consecutive_cells(*pp,n);
if(cnt>=n) {
pointer x=*pp;
*pp=cdr(*pp+n-1);
sc->fcells -= n;
return x;
}
pp=&cdr(*pp+cnt-1);
}
return sc->NIL;
}
/* To retain recent allocs before interpreter knows about them -
Tehom */
static void push_recent_alloc(scheme *sc, pointer recent, pointer extra)
{
pointer holder = get_cell_x(sc, recent, extra);
typeflag(holder) = T_PAIR | T_IMMUTABLE;
car(holder) = recent;
cdr(holder) = car(sc->sink);
car(sc->sink) = holder;
}
static pointer get_cell(scheme *sc, pointer a, pointer b)
{
pointer cell = get_cell_x(sc, a, b);
/* For right now, include "a" and "b" in "cell" so that gc doesn't
think they are garbage. */
/* Tentatively record it as a pair so gc understands it. */
typeflag(cell) = T_PAIR;
car(cell) = a;
cdr(cell) = b;
push_recent_alloc(sc, cell, sc->NIL);
return cell;
}
static pointer get_vector_object(scheme *sc, int len, pointer init)
{
pointer cells = get_consecutive_cells(sc,len/2+len%2+1);
if(sc->no_memory) { return sc->sink; }
/* Record it as a vector so that gc understands it. */
typeflag(cells) = (T_VECTOR | T_ATOM);
ivalue_unchecked(cells)=len;
set_num_integer(cells);
fill_vector(cells,init);
push_recent_alloc(sc, cells, sc->NIL);
return cells;
}
static INLINE void ok_to_freely_gc(scheme *sc)
{
car(sc->sink) = sc->NIL;
}
#if defined TSGRIND
static void check_cell_alloced(pointer p, int expect_alloced)
{
/* Can't use putstr(sc,str) because callers have no access to
sc. */
if(typeflag(p) & !expect_alloced)
{
fprintf(stderr,"Cell is already allocated!\n");
}
if(!(typeflag(p)) & expect_alloced)
{
fprintf(stderr,"Cell is not allocated!\n");
}
}
static void check_range_alloced(pointer p, int n, int expect_alloced)
{
int i;
for(i = 0;i<n;i++)
{ (void)check_cell_alloced(p+i,expect_alloced); }
}
#endif
/* Medium level cell allocation */
/* get new cons cell */
pointer _cons(scheme *sc, pointer a, pointer b, int immutable) {
pointer x = get_cell(sc,a, b);
typeflag(x) = T_PAIR;
if(immutable) {
setimmutable(x);
}
car(x) = a;
cdr(x) = b;
return (x);
}
/* ========== oblist implementation ========== */
#ifndef USE_OBJECT_LIST
static int hash_fn(const char *key, int table_size);
static pointer oblist_initial_value(scheme *sc)
{
return mk_vector(sc, 461); /* probably should be bigger */
}
/* returns the new symbol */
static pointer oblist_add_by_name(scheme *sc, const char *name)
{
pointer x;
int location;
x = immutable_cons(sc, mk_string(sc, name), sc->NIL);
typeflag(x) = T_SYMBOL;
setimmutable(car(x));
location = hash_fn(name, ivalue_unchecked(sc->oblist));
set_vector_elem(sc->oblist, location,
immutable_cons(sc, x, vector_elem(sc->oblist, location)));
return x;
}
static INLINE pointer oblist_find_by_name(scheme *sc, const char *name)
{
int location;
pointer x;
char *s;
location = hash_fn(name, ivalue_unchecked(sc->oblist));
for (x = vector_elem(sc->oblist, location); x != sc->NIL; x = cdr(x)) {
s = symname(car(x));
/* case-insensitive, per R5RS section 2. */
if(stricmp(name, s) == 0) {
return car(x);
}
}
return sc->NIL;
}
static pointer oblist_all_symbols(scheme *sc)
{
int i;
pointer x;
pointer ob_list = sc->NIL;
for (i = 0; i < ivalue_unchecked(sc->oblist); i++) {
for (x = vector_elem(sc->oblist, i); x != sc->NIL; x = cdr(x)) {
ob_list = cons(sc, x, ob_list);
}
}
return ob_list;
}
#else
static pointer oblist_initial_value(scheme *sc)
{
return sc->NIL;
}
static INLINE pointer oblist_find_by_name(scheme *sc, const char *name)
{
pointer x;
char *s;
for (x = sc->oblist; x != sc->NIL; x = cdr(x)) {
s = symname(car(x));
/* case-insensitive, per R5RS section 2. */
if(stricmp(name, s) == 0) {
return car(x);
}
}
return sc->NIL;
}
/* returns the new symbol */
static pointer oblist_add_by_name(scheme *sc, const char *name)
{
pointer x;
x = immutable_cons(sc, mk_string(sc, name), sc->NIL);
typeflag(x) = T_SYMBOL;
setimmutable(car(x));
sc->oblist = immutable_cons(sc, x, sc->oblist);
return x;
}
static pointer oblist_all_symbols(scheme *sc)
{
return sc->oblist;
}
#endif
static pointer mk_port(scheme *sc, port *p) {
pointer x = get_cell(sc, sc->NIL, sc->NIL);
typeflag(x) = T_PORT|T_ATOM;
x->_object._port=p;
return (x);
}
pointer mk_foreign_func(scheme *sc, foreign_func f) {
pointer x = get_cell(sc, sc->NIL, sc->NIL);
typeflag(x) = (T_FOREIGN | T_ATOM);
x->_object._ff=f;
return (x);
}
INTERFACE pointer mk_character(scheme *sc, int c) {
pointer x = get_cell(sc,sc->NIL, sc->NIL);
typeflag(x) = (T_CHARACTER | T_ATOM);
ivalue_unchecked(x)= c;
set_num_integer(x);
return (x);
}
/* get number atom (integer) */
INTERFACE pointer mk_integer(scheme *sc, long num) {
pointer x = get_cell(sc,sc->NIL, sc->NIL);
typeflag(x) = (T_NUMBER | T_ATOM);
ivalue_unchecked(x)= num;
set_num_integer(x);
return (x);
}
INTERFACE pointer mk_real(scheme *sc, double n) {
pointer x = get_cell(sc,sc->NIL, sc->NIL);
typeflag(x) = (T_NUMBER | T_ATOM);
rvalue_unchecked(x)= n;
set_num_real(x);
return (x);
}
static pointer mk_number(scheme *sc, num n) {
if(n.is_fixnum) {
return mk_integer(sc,n.value.ivalue);
} else {
return mk_real(sc,n.value.rvalue);
}
}
/* allocate name to string area */
static char *store_string(scheme *sc, int len_str, const char *str, char fill) {
char *q;
q=(char*)sc->malloc(len_str+1);
if(q==0) {
sc->no_memory=1;
return sc->strbuff;
}
if(str!=0) {
snprintf(q, len_str+1, "%s", str);
} else {
memset(q, fill, len_str);
q[len_str]=0;
}
return (q);
}
/* get new string */
INTERFACE pointer mk_string(scheme *sc, const char *str) {
return mk_counted_string(sc,str,strlen(str));
}
INTERFACE pointer mk_counted_string(scheme *sc, const char *str, int len) {
pointer x = get_cell(sc, sc->NIL, sc->NIL);
typeflag(x) = (T_STRING | T_ATOM);
strvalue(x) = store_string(sc,len,str,0);
strlength(x) = len;
return (x);
}