ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/microscheme/scheme.c
(Generate patch)

Comparing cvsroot/microscheme/scheme.c (file contents):
Revision 1.41 by root, Mon Nov 30 05:20:10 2015 UTC vs.
Revision 1.60 by root, Wed Dec 2 02:59:36 2015 UTC

16 * (MINISCM) This is a revised and modified version by Akira KIDA. 16 * (MINISCM) This is a revised and modified version by Akira KIDA.
17 * (MINISCM) current version is 0.85k4 (15 May 1994) 17 * (MINISCM) current version is 0.85k4 (15 May 1994)
18 * 18 *
19 */ 19 */
20 20
21#define EXPERIMENT 1
22
23#if 1
21#define PAGE_SIZE 4096 /* does not work on sparc/alpha */ 24#define PAGE_SIZE 4096 /* does not work on sparc/alpha */
22#include "malloc.c" 25#include "malloc.c"
26#endif
23 27
24#define SCHEME_SOURCE 28#define SCHEME_SOURCE
25#include "scheme-private.h" 29#include "scheme-private.h"
26#ifndef WIN32 30#ifndef WIN32
27# include <unistd.h> 31# include <unistd.h>
77 81
78#define BACKQUOTE '`' 82#define BACKQUOTE '`'
79#define WHITESPACE " \t\r\n\v\f" 83#define WHITESPACE " \t\r\n\v\f"
80#define DELIMITERS "()\";" WHITESPACE 84#define DELIMITERS "()\";" WHITESPACE
81 85
82#define NIL (&SCHEME_V->xNIL) //TODO: make this 0? 86#define NIL POINTER (&SCHEME_V->xNIL)
83#define S_T (&SCHEME_V->xT) //TODO: magic ptr value? 87#define S_T POINTER (&SCHEME_V->xT)
84#define S_F (&SCHEME_V->xF) //TODO: magic ptr value? 88#define S_F POINTER (&SCHEME_V->xF)
85#define S_SINK (&SCHEME_V->xsink) 89#define S_SINK POINTER (&SCHEME_V->xsink)
86#define S_EOF (&SCHEME_V->xEOF_OBJ) 90#define S_EOF POINTER (&SCHEME_V->xEOF_OBJ)
87 91
88#if !USE_MULTIPLICITY 92#if !USE_MULTIPLICITY
89static scheme sc; 93static scheme sc;
90#endif 94#endif
91 95
92static void 96ecb_cold static void
93xbase (char *s, long n, int base) 97xbase (char *s, long n, int base)
94{ 98{
95 if (n < 0) 99 if (n < 0)
96 { 100 {
97 *s++ = '-'; 101 *s++ = '-';
99 } 103 }
100 104
101 char *p = s; 105 char *p = s;
102 106
103 do { 107 do {
104 *p++ = '0' + n % base; 108 *p++ = "0123456789abcdef"[n % base];
105 n /= base; 109 n /= base;
106 } while (n); 110 } while (n);
107 111
108 *p-- = 0; 112 *p-- = 0;
109 113
112 char x = *s; *s = *p; *p = x; 116 char x = *s; *s = *p; *p = x;
113 --p; ++s; 117 --p; ++s;
114 } 118 }
115} 119}
116 120
117static void 121ecb_cold static void
118xnum (char *s, long n) 122xnum (char *s, long n)
119{ 123{
120 xbase (s, n, 10); 124 xbase (s, n, 10);
121} 125}
122 126
123static void 127ecb_cold static void
124xwrstr (const char *s) 128putnum (SCHEME_P_ long n)
125{
126 write (1, s, strlen (s));
127}
128
129static void
130xwrnum (long n)
131{ 129{
132 char buf[64]; 130 char buf[64];
133 131
134 xnum (buf, n); 132 xnum (buf, n);
135 xwrstr (buf); 133 putstr (SCHEME_A_ buf);
136} 134}
137 135
138static char 136ecb_cold static char
139xtoupper (char c) 137xtoupper (char c)
140{ 138{
141 if (c >= 'a' && c <= 'z') 139 if (c >= 'a' && c <= 'z')
142 c -= 'a' - 'A'; 140 c -= 'a' - 'A';
143 141
144 return c; 142 return c;
145} 143}
146 144
147static char 145ecb_cold static char
148xtolower (char c) 146xtolower (char c)
149{ 147{
150 if (c >= 'A' && c <= 'Z') 148 if (c >= 'A' && c <= 'Z')
151 c += 'a' - 'A'; 149 c += 'a' - 'A';
152 150
153 return c; 151 return c;
154} 152}
155 153
156static int 154ecb_cold static int
157xisdigit (char c) 155xisdigit (char c)
158{ 156{
159 return c >= '0' && c <= '9'; 157 return c >= '0' && c <= '9';
160} 158}
161 159
162#define toupper(c) xtoupper (c) 160#define toupper(c) xtoupper (c)
163#define tolower(c) xtolower (c) 161#define tolower(c) xtolower (c)
164#define isdigit(c) xisdigit (c) 162#define isdigit(c) xisdigit (c)
165 163
166#if USE_IGNORECASE 164#if USE_IGNORECASE
167static const char * 165ecb_cold static const char *
168xstrlwr (char *s) 166xstrlwr (char *s)
169{ 167{
170 const char *p = s; 168 const char *p = s;
171 169
172 while (*s) 170 while (*s)
192 190
193#ifndef InitFile 191#ifndef InitFile
194# define InitFile "init.scm" 192# define InitFile "init.scm"
195#endif 193#endif
196 194
197#ifndef FIRST_CELLSEGS
198# define FIRST_CELLSEGS 3
199#endif
200
201enum scheme_types 195enum scheme_types
202{ 196{
203 T_INTEGER, 197 T_INTEGER,
198 T_CHARACTER,
204 T_REAL, 199 T_REAL,
205 T_STRING, 200 T_STRING,
206 T_SYMBOL, 201 T_SYMBOL,
207 T_PROC, 202 T_PROC,
208 T_PAIR, /* also used for free cells */ 203 T_PAIR, /* also used for free cells */
209 T_CLOSURE, 204 T_CLOSURE,
205 T_MACRO,
210 T_CONTINUATION, 206 T_CONTINUATION,
211 T_FOREIGN, 207 T_FOREIGN,
212 T_CHARACTER,
213 T_PORT, 208 T_PORT,
214 T_VECTOR, 209 T_VECTOR,
215 T_MACRO,
216 T_PROMISE, 210 T_PROMISE,
217 T_ENVIRONMENT, 211 T_ENVIRONMENT,
218 /* one more... */ 212 /* one more... */
219 T_NUM_SYSTEM_TYPES 213 T_NUM_SYSTEM_TYPES
220}; 214};
256static num num_op (enum num_op op, num a, num b); 250static num num_op (enum num_op op, num a, num b);
257static num num_intdiv (num a, num b); 251static num num_intdiv (num a, num b);
258static num num_rem (num a, num b); 252static num num_rem (num a, num b);
259static num num_mod (num a, num b); 253static num num_mod (num a, num b);
260 254
261#if USE_MATH
262static double round_per_R5RS (double x);
263#endif
264static int is_zero_rvalue (RVALUE x); 255static int is_zero_rvalue (RVALUE x);
265 256
266static num num_zero; 257static num num_zero;
267static num num_one; 258static num num_one;
268 259
260/* convert "pointer" to cell* / cell* to pointer */
261#define CELL(p) ((struct cell *)(p) + 0)
262#define POINTER(c) ((void *)((c) - 0))
263
269/* macros for cell operations */ 264/* macros for cell operations */
270#define typeflag(p) ((p)->flag + 0) 265#define typeflag(p) (CELL(p)->flag + 0)
271#define set_typeflag(p,v) ((p)->flag = (v)) 266#define set_typeflag(p,v) (CELL(p)->flag = (v))
272#define type(p) (typeflag (p) & T_MASKTYPE) 267#define type(p) (typeflag (p) & T_MASKTYPE)
273 268
274INTERFACE int 269INTERFACE int
275is_string (pointer p) 270is_string (pointer p)
276{ 271{
277 return type (p) == T_STRING; 272 return type (p) == T_STRING;
278} 273}
279 274
280#define strvalue(p) ((p)->object.string.svalue) 275#define strvalue(p) (CELL(p)->object.string.svalue)
281#define strlength(p) ((p)->object.string.length) 276#define strlength(p) (CELL(p)->object.string.length)
282 277
283INTERFACE int 278INTERFACE int
284is_vector (pointer p) 279is_vector (pointer p)
285{ 280{
286 return type (p) == T_VECTOR; 281 return type (p) == T_VECTOR;
287} 282}
288 283
289#define vecvalue(p) ((p)->object.vector.vvalue) 284#define vecvalue(p) (CELL(p)->object.vector.vvalue)
290#define veclength(p) ((p)->object.vector.length) 285#define veclength(p) (CELL(p)->object.vector.length)
291INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); 286INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
292INTERFACE pointer vector_get (pointer vec, uint32_t ielem); 287INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
293INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); 288INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
294 289
295INTERFACE int 290INTERFACE int
321string_value (pointer p) 316string_value (pointer p)
322{ 317{
323 return strvalue (p); 318 return strvalue (p);
324} 319}
325 320
326#define ivalue_unchecked(p) (p)->object.ivalue 321#define ivalue_unchecked(p) CELL(p)->object.ivalue
327#define set_ivalue(p,v) (p)->object.ivalue = (v) 322#define set_ivalue(p,v) CELL(p)->object.ivalue = (v)
328 323
329#if USE_REAL 324#if USE_REAL
330#define rvalue_unchecked(p) (p)->object.rvalue 325#define rvalue_unchecked(p) CELL(p)->object.rvalue
331#define set_rvalue(p,v) (p)->object.rvalue = (v) 326#define set_rvalue(p,v) CELL(p)->object.rvalue = (v)
332#else 327#else
333#define rvalue_unchecked(p) (p)->object.ivalue 328#define rvalue_unchecked(p) CELL(p)->object.ivalue
334#define set_rvalue(p,v) (p)->object.ivalue = (v) 329#define set_rvalue(p,v) CELL(p)->object.ivalue = (v)
335#endif 330#endif
336 331
337INTERFACE long 332INTERFACE long
338charvalue (pointer p) 333charvalue (pointer p)
339{ 334{
340 return ivalue_unchecked (p); 335 return ivalue_unchecked (p);
341} 336}
342 337
338#define port(p) CELL(p)->object.port
339#define set_port(p,v) port(p) = (v)
343INTERFACE int 340INTERFACE int
344is_port (pointer p) 341is_port (pointer p)
345{ 342{
346 return type (p) == T_PORT; 343 return type (p) == T_PORT;
347} 344}
348 345
349INTERFACE int 346INTERFACE int
350is_inport (pointer p) 347is_inport (pointer p)
351{ 348{
352 return is_port (p) && p->object.port->kind & port_input; 349 return is_port (p) && port (p)->kind & port_input;
353} 350}
354 351
355INTERFACE int 352INTERFACE int
356is_outport (pointer p) 353is_outport (pointer p)
357{ 354{
358 return is_port (p) && p->object.port->kind & port_output; 355 return is_port (p) && port (p)->kind & port_output;
359} 356}
360 357
361INTERFACE int 358INTERFACE int
362is_pair (pointer p) 359is_pair (pointer p)
363{ 360{
364 return type (p) == T_PAIR; 361 return type (p) == T_PAIR;
365} 362}
366 363
367#define car(p) ((p)->object.cons.car + 0) 364#define car(p) (POINTER (CELL(p)->object.cons.car))
368#define cdr(p) ((p)->object.cons.cdr + 0) 365#define cdr(p) (POINTER (CELL(p)->object.cons.cdr))
369 366
370static pointer caar (pointer p) { return car (car (p)); } 367static pointer caar (pointer p) { return car (car (p)); }
371static pointer cadr (pointer p) { return car (cdr (p)); } 368static pointer cadr (pointer p) { return car (cdr (p)); }
372static pointer cdar (pointer p) { return cdr (car (p)); } 369static pointer cdar (pointer p) { return cdr (car (p)); }
373static pointer cddr (pointer p) { return cdr (cdr (p)); } 370static pointer cddr (pointer p) { return cdr (cdr (p)); }
377static pointer cdaar (pointer p) { return cdr (car (car (p))); } 374static pointer cdaar (pointer p) { return cdr (car (car (p))); }
378 375
379INTERFACE void 376INTERFACE void
380set_car (pointer p, pointer q) 377set_car (pointer p, pointer q)
381{ 378{
382 p->object.cons.car = q; 379 CELL(p)->object.cons.car = CELL (q);
383} 380}
384 381
385INTERFACE void 382INTERFACE void
386set_cdr (pointer p, pointer q) 383set_cdr (pointer p, pointer q)
387{ 384{
388 p->object.cons.cdr = q; 385 CELL(p)->object.cons.cdr = CELL (q);
389} 386}
390 387
391INTERFACE pointer 388INTERFACE pointer
392pair_car (pointer p) 389pair_car (pointer p)
393{ 390{
411{ 408{
412 return strvalue (p); 409 return strvalue (p);
413} 410}
414 411
415#if USE_PLIST 412#if USE_PLIST
413#error plists are broken because symbols are no longer pairs
414#define symprop(p) cdr(p)
416SCHEME_EXPORT int 415SCHEME_EXPORT int
417hasprop (pointer p) 416hasprop (pointer p)
418{ 417{
419 return typeflag (p) & T_SYMBOL; 418 return typeflag (p) & T_SYMBOL;
420} 419}
421
422# define symprop(p) cdr(p)
423#endif 420#endif
424 421
425INTERFACE int 422INTERFACE int
426is_syntax (pointer p) 423is_syntax (pointer p)
427{ 424{
523 proper list: length 520 proper list: length
524 circular list: -1 521 circular list: -1
525 not even a pair: -2 522 not even a pair: -2
526 dotted list: -2 minus length before dot 523 dotted list: -2 minus length before dot
527*/ 524*/
528INTERFACE int 525ecb_hot INTERFACE int
529list_length (SCHEME_P_ pointer a) 526list_length (SCHEME_P_ pointer a)
530{ 527{
531 int i = 0; 528 int i = 0;
532 pointer slow, fast; 529 pointer slow, fast;
533 530
639 "gs", 636 "gs",
640 "rs", 637 "rs",
641 "us" 638 "us"
642}; 639};
643 640
644static int 641ecb_cold static int
645is_ascii_name (const char *name, int *pc) 642is_ascii_name (const char *name, int *pc)
646{ 643{
647 int i; 644 int i;
648 645
649 for (i = 0; i < 32; i++) 646 for (i = 0; i < 32; i++)
668 665
669static int file_push (SCHEME_P_ const char *fname); 666static int file_push (SCHEME_P_ const char *fname);
670static void file_pop (SCHEME_P); 667static void file_pop (SCHEME_P);
671static int file_interactive (SCHEME_P); 668static int file_interactive (SCHEME_P);
672ecb_inline int is_one_of (const char *s, int c); 669ecb_inline int is_one_of (const char *s, int c);
673static int alloc_cellseg (SCHEME_P_ int n); 670static int alloc_cellseg (SCHEME_P);
674ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); 671ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
675static void finalize_cell (SCHEME_P_ pointer a); 672static void finalize_cell (SCHEME_P_ pointer a);
676static int count_consecutive_cells (pointer x, int needed); 673static int count_consecutive_cells (pointer x, int needed);
677static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 674static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
678static pointer mk_number (SCHEME_P_ const num n); 675static pointer mk_number (SCHEME_P_ const num n);
872 } 869 }
873 870
874 return ret; 871 return ret;
875} 872}
876 873
877#if USE_MATH
878
879/* Round to nearest. Round to even if midway */
880static double
881round_per_R5RS (double x)
882{
883 double fl = floor (x);
884 double ce = ceil (x);
885 double dfl = x - fl;
886 double dce = ce - x;
887
888 if (dfl > dce)
889 return ce;
890 else if (dfl < dce)
891 return fl;
892 else
893 {
894 if (fmod (fl, 2) == 0) /* I imagine this holds */
895 return fl;
896 else
897 return ce;
898 }
899}
900#endif
901
902static int 874static int
903is_zero_rvalue (RVALUE x) 875is_zero_rvalue (RVALUE x)
904{ 876{
905 return x == 0; 877 return x == 0;
906#if 0 878#if 0
911#endif 883#endif
912#endif 884#endif
913} 885}
914 886
915/* allocate new cell segment */ 887/* allocate new cell segment */
916static int 888ecb_cold static int
917alloc_cellseg (SCHEME_P_ int n) 889alloc_cellseg (SCHEME_P)
918{ 890{
919 pointer newp; 891 struct cell *newp;
920 pointer last; 892 struct cell *last;
921 pointer p; 893 struct cell *p;
922 char *cp; 894 char *cp;
923 long i; 895 long i;
924 int k; 896 int k;
925 897
926 static int segsize = CELL_SEGSIZE >> 1; 898 static int segsize = CELL_SEGSIZE >> 1;
927 segsize <<= 1; 899 segsize <<= 1;
928 900
929 for (k = 0; k < n; k++)
930 {
931 if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1)
932 return k;
933
934 cp = malloc (segsize * sizeof (struct cell)); 901 cp = malloc (segsize * sizeof (struct cell));
935 902
936 if (!cp && USE_ERROR_CHECKING) 903 if (!cp && USE_ERROR_CHECKING)
937 return k; 904 return k;
938 905
939 i = ++SCHEME_V->last_cell_seg; 906 i = ++SCHEME_V->last_cell_seg;
940 SCHEME_V->alloc_seg[i] = cp; 907 SCHEME_V->alloc_seg[i] = cp;
941 908
942 newp = (pointer)cp; 909 newp = (struct cell *)cp;
943 SCHEME_V->cell_seg[i] = newp; 910 SCHEME_V->cell_seg[i] = newp;
944 SCHEME_V->cell_segsize[i] = segsize; 911 SCHEME_V->cell_segsize[i] = segsize;
945 SCHEME_V->fcells += segsize; 912 SCHEME_V->fcells += segsize;
946 last = newp + segsize - 1; 913 last = newp + segsize - 1;
947 914
948 for (p = newp; p <= last; p++) 915 for (p = newp; p <= last; p++)
949 { 916 {
917 pointer cp = POINTER (p);
950 set_typeflag (p, T_PAIR); 918 set_typeflag (cp, T_PAIR);
951 set_car (p, NIL); 919 set_car (cp, NIL);
952 set_cdr (p, p + 1); 920 set_cdr (cp, POINTER (p + 1));
953 } 921 }
954 922
955 set_cdr (last, SCHEME_V->free_cell); 923 set_cdr (POINTER (last), SCHEME_V->free_cell);
956 SCHEME_V->free_cell = newp; 924 SCHEME_V->free_cell = POINTER (newp);
957 }
958 925
959 return n; 926 return 1;
960} 927}
961 928
962/* get new cell. parameter a, b is marked by gc. */ 929/* get new cell. parameter a, b is marked by gc. */
963ecb_inline pointer 930ecb_inline pointer
964get_cell_x (SCHEME_P_ pointer a, pointer b) 931get_cell_x (SCHEME_P_ pointer a, pointer b)
968 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 935 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
969 return S_SINK; 936 return S_SINK;
970 937
971 if (SCHEME_V->free_cell == NIL) 938 if (SCHEME_V->free_cell == NIL)
972 { 939 {
973 const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; 940 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1;
974 941
975 gc (SCHEME_A_ a, b); 942 gc (SCHEME_A_ a, b);
976 943
977 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) 944 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL)
978 { 945 {
979 /* if only a few recovered, get more to avoid fruitless gc's */ 946 /* if only a few recovered, get more to avoid fruitless gc's */
980 if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) 947 if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL)
981 { 948 {
982#if USE_ERROR_CHECKING 949#if USE_ERROR_CHECKING
983 SCHEME_V->no_memory = 1; 950 SCHEME_V->no_memory = 1;
984 return S_SINK; 951 return S_SINK;
985#endif 952#endif
997 } 964 }
998} 965}
999 966
1000/* To retain recent allocs before interpreter knows about them - 967/* To retain recent allocs before interpreter knows about them -
1001 Tehom */ 968 Tehom */
1002 969ecb_hot static void
1003static void
1004push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 970push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
1005{ 971{
1006 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 972 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
1007 973
1008 set_typeflag (holder, T_PAIR); 974 set_typeflag (holder, T_PAIR);
1010 set_car (holder, recent); 976 set_car (holder, recent);
1011 set_cdr (holder, car (S_SINK)); 977 set_cdr (holder, car (S_SINK));
1012 set_car (S_SINK, holder); 978 set_car (S_SINK, holder);
1013} 979}
1014 980
1015static pointer 981ecb_hot static pointer
1016get_cell (SCHEME_P_ pointer a, pointer b) 982get_cell (SCHEME_P_ pointer a, pointer b)
1017{ 983{
1018 pointer cell = get_cell_x (SCHEME_A_ a, b); 984 pointer cell = get_cell_x (SCHEME_A_ a, b);
1019 985
1020 /* For right now, include "a" and "b" in "cell" so that gc doesn't 986 /* For right now, include "a" and "b" in "cell" so that gc doesn't
1029} 995}
1030 996
1031static pointer 997static pointer
1032get_vector_object (SCHEME_P_ uint32_t len, pointer init) 998get_vector_object (SCHEME_P_ uint32_t len, pointer init)
1033{ 999{
1034 pointer v = get_cell_x (SCHEME_A_ 0, 0); 1000 pointer v = get_cell_x (SCHEME_A_ NIL, NIL);
1035 pointer *e = malloc (len * sizeof (pointer)); 1001 pointer *e = malloc (len * sizeof (pointer));
1036 1002
1037 if (!e && USE_ERROR_CHECKING) 1003 if (!e && USE_ERROR_CHECKING)
1038 return S_SINK; 1004 return S_SINK;
1039 1005
1040 /* Record it as a vector so that gc understands it. */ 1006 /* Record it as a vector so that gc understands it. */
1041 set_typeflag (v, T_VECTOR | T_ATOM); 1007 set_typeflag (v, T_VECTOR | T_ATOM);
1042 1008
1043 v->object.vector.vvalue = e; 1009 CELL(v)->object.vector.vvalue = e;
1044 v->object.vector.length = len; 1010 CELL(v)->object.vector.length = len;
1045 fill_vector (v, 0, init); 1011 fill_vector (v, 0, init);
1046 push_recent_alloc (SCHEME_A_ v, NIL); 1012 push_recent_alloc (SCHEME_A_ v, NIL);
1047 1013
1048 return v; 1014 return v;
1049} 1015}
1058static void 1024static void
1059check_cell_alloced (pointer p, int expect_alloced) 1025check_cell_alloced (pointer p, int expect_alloced)
1060{ 1026{
1061 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ 1027 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */
1062 if (typeflag (p) & !expect_alloced) 1028 if (typeflag (p) & !expect_alloced)
1063 xwrstr ("Cell is already allocated!\n"); 1029 putstr (SCHEME_A_ "Cell is already allocated!\n");
1064 1030
1065 if (!(typeflag (p)) & expect_alloced) 1031 if (!(typeflag (p)) & expect_alloced)
1066 xwrstr ("Cell is not allocated!\n"); 1032 putstr (SCHEME_A_ "Cell is not allocated!\n");
1067} 1033}
1068 1034
1069static void 1035static void
1070check_range_alloced (pointer p, int n, int expect_alloced) 1036check_range_alloced (pointer p, int n, int expect_alloced)
1071{ 1037{
1077#endif 1043#endif
1078 1044
1079/* Medium level cell allocation */ 1045/* Medium level cell allocation */
1080 1046
1081/* get new cons cell */ 1047/* get new cons cell */
1082pointer 1048ecb_hot pointer
1083xcons (SCHEME_P_ pointer a, pointer b, int immutable) 1049xcons (SCHEME_P_ pointer a, pointer b, int immutable)
1084{ 1050{
1085 pointer x = get_cell (SCHEME_A_ a, b); 1051 pointer x = get_cell (SCHEME_A_ a, b);
1086 1052
1087 set_typeflag (x, T_PAIR); 1053 set_typeflag (x, T_PAIR);
1093 set_cdr (x, b); 1059 set_cdr (x, b);
1094 1060
1095 return x; 1061 return x;
1096} 1062}
1097 1063
1098/* ========== oblist implementation ========== */
1099
1100static pointer 1064ecb_cold static pointer
1101generate_symbol (SCHEME_P_ const char *name) 1065generate_symbol (SCHEME_P_ const char *name)
1102{ 1066{
1103 pointer x = mk_string (SCHEME_A_ name); 1067 pointer x = mk_string (SCHEME_A_ name);
1104 setimmutable (x); 1068 setimmutable (x);
1105 set_typeflag (x, T_SYMBOL | T_ATOM); 1069 set_typeflag (x, T_SYMBOL | T_ATOM);
1106 return x; 1070 return x;
1107} 1071}
1108 1072
1073/* ========== oblist implementation ========== */
1074
1109#ifndef USE_OBJECT_LIST 1075#ifndef USE_OBJECT_LIST
1110 1076
1111static int 1077static int
1112hash_fn (const char *key, int table_size) 1078hash_fn (const char *key, int table_size)
1113{ 1079{
1118 hash = (hash ^ *p++) * 16777619; 1084 hash = (hash ^ *p++) * 16777619;
1119 1085
1120 return hash % table_size; 1086 return hash % table_size;
1121} 1087}
1122 1088
1123static pointer 1089ecb_cold static pointer
1124oblist_initial_value (SCHEME_P) 1090oblist_initial_value (SCHEME_P)
1125{ 1091{
1126 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1092 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1127} 1093}
1128 1094
1129/* returns the new symbol */ 1095/* returns the new symbol */
1130static pointer 1096ecb_cold static pointer
1131oblist_add_by_name (SCHEME_P_ const char *name) 1097oblist_add_by_name (SCHEME_P_ const char *name)
1132{ 1098{
1133 pointer x = generate_symbol (SCHEME_A_ name); 1099 pointer x = generate_symbol (SCHEME_A_ name);
1134 int location = hash_fn (name, veclength (SCHEME_V->oblist)); 1100 int location = hash_fn (name, veclength (SCHEME_V->oblist));
1135 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); 1101 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1136 return x; 1102 return x;
1137} 1103}
1138 1104
1139ecb_inline pointer 1105ecb_cold static pointer
1140oblist_find_by_name (SCHEME_P_ const char *name) 1106oblist_find_by_name (SCHEME_P_ const char *name)
1141{ 1107{
1142 int location; 1108 int location;
1143 pointer x; 1109 pointer x;
1144 char *s; 1110 char *s;
1155 } 1121 }
1156 1122
1157 return NIL; 1123 return NIL;
1158} 1124}
1159 1125
1160static pointer 1126ecb_cold static pointer
1161oblist_all_symbols (SCHEME_P) 1127oblist_all_symbols (SCHEME_P)
1162{ 1128{
1163 int i; 1129 int i;
1164 pointer x; 1130 pointer x;
1165 pointer ob_list = NIL; 1131 pointer ob_list = NIL;
1171 return ob_list; 1137 return ob_list;
1172} 1138}
1173 1139
1174#else 1140#else
1175 1141
1176static pointer 1142ecb_cold static pointer
1177oblist_initial_value (SCHEME_P) 1143oblist_initial_value (SCHEME_P)
1178{ 1144{
1179 return NIL; 1145 return NIL;
1180} 1146}
1181 1147
1182ecb_inline pointer 1148ecb_cold static pointer
1183oblist_find_by_name (SCHEME_P_ const char *name) 1149oblist_find_by_name (SCHEME_P_ const char *name)
1184{ 1150{
1185 pointer x; 1151 pointer x;
1186 char *s; 1152 char *s;
1187 1153
1196 1162
1197 return NIL; 1163 return NIL;
1198} 1164}
1199 1165
1200/* returns the new symbol */ 1166/* returns the new symbol */
1201static pointer 1167ecb_cold static pointer
1202oblist_add_by_name (SCHEME_P_ const char *name) 1168oblist_add_by_name (SCHEME_P_ const char *name)
1203{ 1169{
1204 pointer x = mk_string (SCHEME_A_ name); 1170 pointer x = generate_symbol (SCHEME_A_ name);
1205 set_typeflag (x, T_SYMBOL);
1206 setimmutable (x);
1207 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1171 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1208 return x; 1172 return x;
1209} 1173}
1210 1174
1211static pointer 1175ecb_cold static pointer
1212oblist_all_symbols (SCHEME_P) 1176oblist_all_symbols (SCHEME_P)
1213{ 1177{
1214 return SCHEME_V->oblist; 1178 return SCHEME_V->oblist;
1215} 1179}
1216 1180
1217#endif 1181#endif
1218 1182
1219#if USE_PORTS 1183#if USE_PORTS
1220static pointer 1184ecb_cold static pointer
1221mk_port (SCHEME_P_ port *p) 1185mk_port (SCHEME_P_ port *p)
1222{ 1186{
1223 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1187 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1224 1188
1225 set_typeflag (x, T_PORT | T_ATOM); 1189 set_typeflag (x, T_PORT | T_ATOM);
1226 x->object.port = p; 1190 set_port (x, p);
1227 1191
1228 return x; 1192 return x;
1229} 1193}
1230#endif 1194#endif
1231 1195
1232pointer 1196ecb_cold pointer
1233mk_foreign_func (SCHEME_P_ foreign_func f) 1197mk_foreign_func (SCHEME_P_ foreign_func f)
1234{ 1198{
1235 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1199 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1236 1200
1237 set_typeflag (x, (T_FOREIGN | T_ATOM)); 1201 set_typeflag (x, T_FOREIGN | T_ATOM);
1238 x->object.ff = f; 1202 CELL(x)->object.ff = f;
1239 1203
1240 return x; 1204 return x;
1241} 1205}
1242 1206
1243INTERFACE pointer 1207INTERFACE pointer
1244mk_character (SCHEME_P_ int c) 1208mk_character (SCHEME_P_ int c)
1245{ 1209{
1246 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1210 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1247 1211
1248 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1212 set_typeflag (x, T_CHARACTER | T_ATOM);
1249 set_ivalue (x, c & 0xff); 1213 set_ivalue (x, c & 0xff);
1250 1214
1251 return x; 1215 return x;
1252} 1216}
1253 1217
1254/* get number atom (integer) */ 1218/* get number atom (integer) */
1255INTERFACE pointer 1219INTERFACE pointer
1256mk_integer (SCHEME_P_ long n) 1220mk_integer (SCHEME_P_ long n)
1257{ 1221{
1222 pointer p = 0;
1223 pointer *pp = &p;
1224
1225#if USE_INTCACHE
1226 if (n >= INTCACHE_MIN && n <= INTCACHE_MAX)
1227 pp = &SCHEME_V->intcache[n - INTCACHE_MIN];
1228#endif
1229
1230 if (!*pp)
1231 {
1258 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1232 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1259 1233
1260 set_typeflag (x, (T_INTEGER | T_ATOM)); 1234 set_typeflag (x, T_INTEGER | T_ATOM);
1235 setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */
1261 set_ivalue (x, n); 1236 set_ivalue (x, n);
1262 1237
1238 *pp = x;
1239 }
1240
1263 return x; 1241 return *pp;
1264} 1242}
1265 1243
1266INTERFACE pointer 1244INTERFACE pointer
1267mk_real (SCHEME_P_ RVALUE n) 1245mk_real (SCHEME_P_ RVALUE n)
1268{ 1246{
1269#if USE_REAL 1247#if USE_REAL
1270 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1248 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1271 1249
1272 set_typeflag (x, (T_REAL | T_ATOM)); 1250 set_typeflag (x, T_REAL | T_ATOM);
1273 set_rvalue (x, n); 1251 set_rvalue (x, n);
1274 1252
1275 return x; 1253 return x;
1276#else 1254#else
1277 return mk_integer (SCHEME_A_ n); 1255 return mk_integer (SCHEME_A_ n);
1388 x = oblist_add_by_name (SCHEME_A_ name); 1366 x = oblist_add_by_name (SCHEME_A_ name);
1389 1367
1390 return x; 1368 return x;
1391} 1369}
1392 1370
1393INTERFACE pointer 1371ecb_cold INTERFACE pointer
1394gensym (SCHEME_P) 1372gensym (SCHEME_P)
1395{ 1373{
1396 pointer x; 1374 pointer x;
1397 char name[40] = "gensym-"; 1375 char name[40] = "gensym-";
1398 xnum (name + 7, SCHEME_V->gensym_cnt); 1376 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1399 1377
1400 return generate_symbol (SCHEME_A_ name); 1378 return generate_symbol (SCHEME_A_ name);
1401} 1379}
1402 1380
1381static int
1382is_gensym (SCHEME_P_ pointer x)
1383{
1384 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1385}
1386
1403/* make symbol or number atom from string */ 1387/* make symbol or number atom from string */
1404static pointer 1388ecb_cold static pointer
1405mk_atom (SCHEME_P_ char *q) 1389mk_atom (SCHEME_P_ char *q)
1406{ 1390{
1407 char c, *p; 1391 char c, *p;
1408 int has_dec_point = 0; 1392 int has_dec_point = 0;
1409 int has_fp_exp = 0; 1393 int has_fp_exp = 0;
1480 1464
1481 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1465 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1482} 1466}
1483 1467
1484/* make constant */ 1468/* make constant */
1485static pointer 1469ecb_cold static pointer
1486mk_sharp_const (SCHEME_P_ char *name) 1470mk_sharp_const (SCHEME_P_ char *name)
1487{ 1471{
1488 if (!strcmp (name, "t")) 1472 if (!strcmp (name, "t"))
1489 return S_T; 1473 return S_T;
1490 else if (!strcmp (name, "f")) 1474 else if (!strcmp (name, "f"))
1491 return S_F; 1475 return S_F;
1492 else if (*name == '\\') /* #\w (character) */ 1476 else if (*name == '\\') /* #\w (character) */
1493 { 1477 {
1494 int c; 1478 int c;
1495 1479
1480 // TODO: optimise
1496 if (stricmp (name + 1, "space") == 0) 1481 if (stricmp (name + 1, "space") == 0)
1497 c = ' '; 1482 c = ' ';
1498 else if (stricmp (name + 1, "newline") == 0) 1483 else if (stricmp (name + 1, "newline") == 0)
1499 c = '\n'; 1484 c = '\n';
1500 else if (stricmp (name + 1, "return") == 0) 1485 else if (stricmp (name + 1, "return") == 0)
1501 c = '\r'; 1486 c = '\r';
1502 else if (stricmp (name + 1, "tab") == 0) 1487 else if (stricmp (name + 1, "tab") == 0)
1503 c = '\t'; 1488 c = '\t';
1489 else if (stricmp (name + 1, "alarm") == 0)
1490 c = 0x07;
1491 else if (stricmp (name + 1, "backspace") == 0)
1492 c = 0x08;
1493 else if (stricmp (name + 1, "escape") == 0)
1494 c = 0x1b;
1495 else if (stricmp (name + 1, "delete") == 0)
1496 c = 0x7f;
1497 else if (stricmp (name + 1, "null") == 0)
1498 c = 0;
1504 else if (name[1] == 'x' && name[2] != 0) 1499 else if (name[1] == 'x' && name[2] != 0)
1505 { 1500 {
1506 long c1 = strtol (name + 2, 0, 16); 1501 long c1 = strtol (name + 2, 0, 16);
1507 1502
1508 if (0 <= c1 && c1 <= UCHAR_MAX) 1503 if (0 <= c1 && c1 <= UCHAR_MAX)
1533 return NIL; 1528 return NIL;
1534 } 1529 }
1535} 1530}
1536 1531
1537/* ========== garbage collector ========== */ 1532/* ========== garbage collector ========== */
1533
1534static void
1535finalize_cell (SCHEME_P_ pointer a)
1536{
1537 /* TODO, fast bitmap check? */
1538 if (is_string (a) || is_symbol (a))
1539 free (strvalue (a));
1540 else if (is_vector (a))
1541 free (vecvalue (a));
1542#if USE_PORTS
1543 else if (is_port (a))
1544 {
1545 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit)
1546 port_close (SCHEME_A_ a, port_input | port_output);
1547
1548 free (port (a));
1549 }
1550#endif
1551}
1538 1552
1539/*-- 1553/*--
1540 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, 1554 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1,
1541 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1555 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1542 * for marking. 1556 * for marking.
1543 * 1557 *
1544 * The exception is vectors - vectors are currently marked recursively, 1558 * The exception is vectors - vectors are currently marked recursively,
1545 * which is inherited form tinyscheme and could be fixed by having another 1559 * which is inherited form tinyscheme and could be fixed by having another
1546 * word of context in the vector 1560 * word of context in the vector
1547 */ 1561 */
1548static void 1562ecb_hot static void
1549mark (pointer a) 1563mark (pointer a)
1550{ 1564{
1551 pointer t, q, p; 1565 pointer t, q, p;
1552 1566
1553 t = 0; 1567 t = 0;
1610 p = q; 1624 p = q;
1611 goto E6; 1625 goto E6;
1612 } 1626 }
1613} 1627}
1614 1628
1629ecb_hot static void
1630gc_free (SCHEME_P)
1631{
1632 int i;
1633 uint32_t total = 0;
1634
1635 /* Here we scan the cells to build the free-list. */
1636 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1637 {
1638 struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1639 struct cell *p;
1640 total += SCHEME_V->cell_segsize [i];
1641
1642 for (p = SCHEME_V->cell_seg[i]; p < end; ++p)
1643 {
1644 pointer c = POINTER (p);
1645
1646 if (is_mark (c))
1647 clrmark (c);
1648 else
1649 {
1650 /* reclaim cell */
1651 if (typeflag (c) != T_PAIR)
1652 {
1653 finalize_cell (SCHEME_A_ c);
1654 set_typeflag (c, T_PAIR);
1655 set_car (c, NIL);
1656 }
1657
1658 ++SCHEME_V->fcells;
1659 set_cdr (c, SCHEME_V->free_cell);
1660 SCHEME_V->free_cell = c;
1661 }
1662 }
1663 }
1664
1665 if (SCHEME_V->gc_verbose)
1666 {
1667 putstr (SCHEME_A_ "done: "); putnum (SCHEME_A_ SCHEME_V->fcells); putstr (SCHEME_A_ " out of "); putnum (SCHEME_A_ total); putstr (SCHEME_A_ " cells were recovered.\n");
1668 }
1669}
1670
1615/* garbage collection. parameter a, b is marked. */ 1671/* garbage collection. parameter a, b is marked. */
1616static void 1672ecb_cold static void
1617gc (SCHEME_P_ pointer a, pointer b) 1673gc (SCHEME_P_ pointer a, pointer b)
1618{ 1674{
1619 pointer p;
1620 int i; 1675 int i;
1621 1676
1622 if (SCHEME_V->gc_verbose) 1677 if (SCHEME_V->gc_verbose)
1623 putstr (SCHEME_A_ "gc..."); 1678 putstr (SCHEME_A_ "gc...");
1624 1679
1640 /* Mark recent objects the interpreter doesn't know about yet. */ 1695 /* Mark recent objects the interpreter doesn't know about yet. */
1641 mark (car (S_SINK)); 1696 mark (car (S_SINK));
1642 /* Mark any older stuff above nested C calls */ 1697 /* Mark any older stuff above nested C calls */
1643 mark (SCHEME_V->c_nest); 1698 mark (SCHEME_V->c_nest);
1644 1699
1700#if USE_INTCACHE
1701 /* mark intcache */
1702 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1703 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1704 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1705#endif
1706
1645 /* mark variables a, b */ 1707 /* mark variables a, b */
1646 mark (a); 1708 mark (a);
1647 mark (b); 1709 mark (b);
1648 1710
1649 /* garbage collect */ 1711 /* garbage collect */
1650 clrmark (NIL); 1712 clrmark (NIL);
1651 SCHEME_V->fcells = 0; 1713 SCHEME_V->fcells = 0;
1652 SCHEME_V->free_cell = NIL; 1714 SCHEME_V->free_cell = NIL;
1653 1715
1654 /* free-list is kept sorted by address so as to maintain consecutive
1655 ranges, if possible, for use with vectors. Here we scan the cells
1656 (which are also kept sorted by address) downwards to build the
1657 free-list in sorted order.
1658 */
1659 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1660 {
1661 p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1662
1663 while (--p >= SCHEME_V->cell_seg[i])
1664 {
1665 if (is_mark (p))
1666 clrmark (p);
1667 else
1668 {
1669 /* reclaim cell */
1670 if (typeflag (p) != T_PAIR)
1671 {
1672 finalize_cell (SCHEME_A_ p);
1673 set_typeflag (p, T_PAIR);
1674 set_car (p, NIL);
1675 }
1676
1677 ++SCHEME_V->fcells;
1678 set_cdr (p, SCHEME_V->free_cell);
1679 SCHEME_V->free_cell = p;
1680 }
1681 }
1682 }
1683
1684 if (SCHEME_V->gc_verbose) 1716 if (SCHEME_V->gc_verbose)
1685 { 1717 putstr (SCHEME_A_ "freeing...");
1686 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1687 }
1688}
1689 1718
1690static void 1719 gc_free (SCHEME_A);
1691finalize_cell (SCHEME_P_ pointer a)
1692{
1693 /* TODO, fast bitmap check? */
1694 if (is_string (a) || is_symbol (a))
1695 free (strvalue (a));
1696 else if (is_vector (a))
1697 free (vecvalue (a));
1698#if USE_PORTS
1699 else if (is_port (a))
1700 {
1701 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit)
1702 port_close (SCHEME_A_ a, port_input | port_output);
1703
1704 free (a->object.port);
1705 }
1706#endif
1707} 1720}
1708 1721
1709/* ========== Routines for Reading ========== */ 1722/* ========== Routines for Reading ========== */
1710 1723
1711static int 1724ecb_cold static int
1712file_push (SCHEME_P_ const char *fname) 1725file_push (SCHEME_P_ const char *fname)
1713{ 1726{
1714#if USE_PORTS 1727#if USE_PORTS
1715 int fin; 1728 int fin;
1716 1729
1725 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; 1738 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1;
1726 SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; 1739 SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input;
1727 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; 1740 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin;
1728 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; 1741 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1;
1729 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; 1742 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0;
1730 SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; 1743 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1731 1744
1732#if SHOW_ERROR_LINE 1745#if SHOW_ERROR_LINE
1733 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; 1746 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0;
1734 1747
1735 if (fname) 1748 if (fname)
1742#else 1755#else
1743 return 1; 1756 return 1;
1744#endif 1757#endif
1745} 1758}
1746 1759
1747static void 1760ecb_cold static void
1748file_pop (SCHEME_P) 1761file_pop (SCHEME_P)
1749{ 1762{
1750 if (SCHEME_V->file_i != 0) 1763 if (SCHEME_V->file_i != 0)
1751 { 1764 {
1752 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1765 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1753#if USE_PORTS 1766#if USE_PORTS
1754 port_close (SCHEME_A_ SCHEME_V->loadport, port_input); 1767 port_close (SCHEME_A_ SCHEME_V->loadport, port_input);
1755#endif 1768#endif
1756 SCHEME_V->file_i--; 1769 SCHEME_V->file_i--;
1757 SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; 1770 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1758 } 1771 }
1759} 1772}
1760 1773
1761static int 1774ecb_cold static int
1762file_interactive (SCHEME_P) 1775file_interactive (SCHEME_P)
1763{ 1776{
1764#if USE_PORTS 1777#if USE_PORTS
1765 return SCHEME_V->file_i == 0 1778 return SCHEME_V->file_i == 0
1766 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1779 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1767 && (SCHEME_V->inport->object.port->kind & port_file); 1780 && (port (SCHEME_V->inport)->kind & port_file);
1768#else 1781#else
1769 return 0; 1782 return 0;
1770#endif 1783#endif
1771} 1784}
1772 1785
1773#if USE_PORTS 1786#if USE_PORTS
1774static port * 1787ecb_cold static port *
1775port_rep_from_filename (SCHEME_P_ const char *fn, int prop) 1788port_rep_from_filename (SCHEME_P_ const char *fn, int prop)
1776{ 1789{
1777 int fd; 1790 int fd;
1778 int flags; 1791 int flags;
1779 char *rw; 1792 char *rw;
1802# endif 1815# endif
1803 1816
1804 return pt; 1817 return pt;
1805} 1818}
1806 1819
1807static pointer 1820ecb_cold static pointer
1808port_from_filename (SCHEME_P_ const char *fn, int prop) 1821port_from_filename (SCHEME_P_ const char *fn, int prop)
1809{ 1822{
1810 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); 1823 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop);
1811 1824
1812 if (!pt && USE_ERROR_CHECKING) 1825 if (!pt && USE_ERROR_CHECKING)
1813 return NIL; 1826 return NIL;
1814 1827
1815 return mk_port (SCHEME_A_ pt); 1828 return mk_port (SCHEME_A_ pt);
1816} 1829}
1817 1830
1818static port * 1831ecb_cold static port *
1819port_rep_from_file (SCHEME_P_ int f, int prop) 1832port_rep_from_file (SCHEME_P_ int f, int prop)
1820{ 1833{
1821 port *pt = malloc (sizeof *pt); 1834 port *pt = malloc (sizeof *pt);
1822 1835
1823 if (!pt && USE_ERROR_CHECKING) 1836 if (!pt && USE_ERROR_CHECKING)
1828 pt->rep.stdio.file = f; 1841 pt->rep.stdio.file = f;
1829 pt->rep.stdio.closeit = 0; 1842 pt->rep.stdio.closeit = 0;
1830 return pt; 1843 return pt;
1831} 1844}
1832 1845
1833static pointer 1846ecb_cold static pointer
1834port_from_file (SCHEME_P_ int f, int prop) 1847port_from_file (SCHEME_P_ int f, int prop)
1835{ 1848{
1836 port *pt = port_rep_from_file (SCHEME_A_ f, prop); 1849 port *pt = port_rep_from_file (SCHEME_A_ f, prop);
1837 1850
1838 if (!pt && USE_ERROR_CHECKING) 1851 if (!pt && USE_ERROR_CHECKING)
1839 return NIL; 1852 return NIL;
1840 1853
1841 return mk_port (SCHEME_A_ pt); 1854 return mk_port (SCHEME_A_ pt);
1842} 1855}
1843 1856
1844static port * 1857ecb_cold static port *
1845port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1858port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
1846{ 1859{
1847 port *pt = malloc (sizeof (port)); 1860 port *pt = malloc (sizeof (port));
1848 1861
1849 if (!pt && USE_ERROR_CHECKING) 1862 if (!pt && USE_ERROR_CHECKING)
1855 pt->rep.string.curr = start; 1868 pt->rep.string.curr = start;
1856 pt->rep.string.past_the_end = past_the_end; 1869 pt->rep.string.past_the_end = past_the_end;
1857 return pt; 1870 return pt;
1858} 1871}
1859 1872
1860static pointer 1873ecb_cold static pointer
1861port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1874port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
1862{ 1875{
1863 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); 1876 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop);
1864 1877
1865 if (!pt && USE_ERROR_CHECKING) 1878 if (!pt && USE_ERROR_CHECKING)
1868 return mk_port (SCHEME_A_ pt); 1881 return mk_port (SCHEME_A_ pt);
1869} 1882}
1870 1883
1871# define BLOCK_SIZE 256 1884# define BLOCK_SIZE 256
1872 1885
1873static port * 1886ecb_cold static port *
1874port_rep_from_scratch (SCHEME_P) 1887port_rep_from_scratch (SCHEME_P)
1875{ 1888{
1876 char *start; 1889 char *start;
1877 port *pt = malloc (sizeof (port)); 1890 port *pt = malloc (sizeof (port));
1878 1891
1892 pt->rep.string.curr = start; 1905 pt->rep.string.curr = start;
1893 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; 1906 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1;
1894 return pt; 1907 return pt;
1895} 1908}
1896 1909
1897static pointer 1910ecb_cold static pointer
1898port_from_scratch (SCHEME_P) 1911port_from_scratch (SCHEME_P)
1899{ 1912{
1900 port *pt = port_rep_from_scratch (SCHEME_A); 1913 port *pt = port_rep_from_scratch (SCHEME_A);
1901 1914
1902 if (!pt && USE_ERROR_CHECKING) 1915 if (!pt && USE_ERROR_CHECKING)
1903 return NIL; 1916 return NIL;
1904 1917
1905 return mk_port (SCHEME_A_ pt); 1918 return mk_port (SCHEME_A_ pt);
1906} 1919}
1907 1920
1908static void 1921ecb_cold static void
1909port_close (SCHEME_P_ pointer p, int flag) 1922port_close (SCHEME_P_ pointer p, int flag)
1910{ 1923{
1911 port *pt = p->object.port; 1924 port *pt = port (p);
1912 1925
1913 pt->kind &= ~flag; 1926 pt->kind &= ~flag;
1914 1927
1915 if ((pt->kind & (port_input | port_output)) == 0) 1928 if ((pt->kind & (port_input | port_output)) == 0)
1916 { 1929 {
1937/* get new character from input file */ 1950/* get new character from input file */
1938static int 1951static int
1939inchar (SCHEME_P) 1952inchar (SCHEME_P)
1940{ 1953{
1941 int c; 1954 int c;
1942 port *pt; 1955 port *pt = port (SCHEME_V->inport);
1943
1944 pt = SCHEME_V->inport->object.port;
1945 1956
1946 if (pt->kind & port_saw_EOF) 1957 if (pt->kind & port_saw_EOF)
1947 return EOF; 1958 return EOF;
1948 1959
1949 c = basic_inchar (pt); 1960 c = basic_inchar (pt);
2016 port *pt; 2027 port *pt;
2017 2028
2018 if (c == EOF) 2029 if (c == EOF)
2019 return; 2030 return;
2020 2031
2021 pt = SCHEME_V->inport->object.port; 2032 pt = port (SCHEME_V->inport);
2022 pt->unget = c; 2033 pt->unget = c;
2023#else 2034#else
2024 if (c == EOF) 2035 if (c == EOF)
2025 return; 2036 return;
2026 2037
2027 ungot = c; 2038 ungot = c;
2028#endif 2039#endif
2029} 2040}
2030 2041
2031#if USE_PORTS 2042#if USE_PORTS
2032static int 2043ecb_cold static int
2033realloc_port_string (SCHEME_P_ port *p) 2044realloc_port_string (SCHEME_P_ port *p)
2034{ 2045{
2035 char *start = p->rep.string.start; 2046 char *start = p->rep.string.start;
2036 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; 2047 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE;
2037 char *str = malloc (new_size); 2048 char *str = malloc (new_size);
2050 else 2061 else
2051 return 0; 2062 return 0;
2052} 2063}
2053#endif 2064#endif
2054 2065
2055INTERFACE void 2066ecb_cold INTERFACE void
2056putstr (SCHEME_P_ const char *s) 2067putstr (SCHEME_P_ const char *s)
2057{ 2068{
2058#if USE_PORTS 2069#if USE_PORTS
2059 port *pt = SCHEME_V->outport->object.port; 2070 port *pt = port (SCHEME_V->outport);
2060 2071
2061 if (pt->kind & port_file) 2072 if (pt->kind & port_file)
2062 write (pt->rep.stdio.file, s, strlen (s)); 2073 write (pt->rep.stdio.file, s, strlen (s));
2063 else 2074 else
2064 for (; *s; s++) 2075 for (; *s; s++)
2066 *pt->rep.string.curr++ = *s; 2077 *pt->rep.string.curr++ = *s;
2067 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) 2078 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2068 *pt->rep.string.curr++ = *s; 2079 *pt->rep.string.curr++ = *s;
2069 2080
2070#else 2081#else
2071 xwrstr (s); 2082 write (pt->rep.stdio.file, s, strlen (s));
2072#endif 2083#endif
2073} 2084}
2074 2085
2075static void 2086ecb_cold static void
2076putchars (SCHEME_P_ const char *s, int len) 2087putchars (SCHEME_P_ const char *s, int len)
2077{ 2088{
2078#if USE_PORTS 2089#if USE_PORTS
2079 port *pt = SCHEME_V->outport->object.port; 2090 port *pt = port (SCHEME_V->outport);
2080 2091
2081 if (pt->kind & port_file) 2092 if (pt->kind & port_file)
2082 write (pt->rep.stdio.file, s, len); 2093 write (pt->rep.stdio.file, s, len);
2083 else 2094 else
2084 { 2095 {
2094#else 2105#else
2095 write (1, s, len); 2106 write (1, s, len);
2096#endif 2107#endif
2097} 2108}
2098 2109
2099INTERFACE void 2110ecb_cold INTERFACE void
2100putcharacter (SCHEME_P_ int c) 2111putcharacter (SCHEME_P_ int c)
2101{ 2112{
2102#if USE_PORTS 2113#if USE_PORTS
2103 port *pt = SCHEME_V->outport->object.port; 2114 port *pt = port (SCHEME_V->outport);
2104 2115
2105 if (pt->kind & port_file) 2116 if (pt->kind & port_file)
2106 { 2117 {
2107 char cc = c; 2118 char cc = c;
2108 write (pt->rep.stdio.file, &cc, 1); 2119 write (pt->rep.stdio.file, &cc, 1);
2120 write (1, &c, 1); 2131 write (1, &c, 1);
2121#endif 2132#endif
2122} 2133}
2123 2134
2124/* read characters up to delimiter, but cater to character constants */ 2135/* read characters up to delimiter, but cater to character constants */
2125static char * 2136ecb_cold static char *
2126readstr_upto (SCHEME_P_ int skip, const char *delim) 2137readstr_upto (SCHEME_P_ int skip, const char *delim)
2127{ 2138{
2128 char *p = SCHEME_V->strbuff + skip; 2139 char *p = SCHEME_V->strbuff + skip;
2129 2140
2130 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2141 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2139 2150
2140 return SCHEME_V->strbuff; 2151 return SCHEME_V->strbuff;
2141} 2152}
2142 2153
2143/* read string expression "xxx...xxx" */ 2154/* read string expression "xxx...xxx" */
2144static pointer 2155ecb_cold static pointer
2145readstrexp (SCHEME_P_ char delim) 2156readstrexp (SCHEME_P_ char delim)
2146{ 2157{
2147 char *p = SCHEME_V->strbuff; 2158 char *p = SCHEME_V->strbuff;
2148 int c; 2159 int c;
2149 int c1 = 0; 2160 int c1 = 0;
2182 case '7': 2193 case '7':
2183 state = st_oct1; 2194 state = st_oct1;
2184 c1 = c - '0'; 2195 c1 = c - '0';
2185 break; 2196 break;
2186 2197
2198 case 'a': *p++ = '\a'; state = st_ok; break;
2199 case 'n': *p++ = '\n'; state = st_ok; break;
2200 case 'r': *p++ = '\r'; state = st_ok; break;
2201 case 't': *p++ = '\t'; state = st_ok; break;
2202
2203 //TODO: \whitespace eol whitespace
2204
2205 //TODO: x should end in ;, not two-digit hex
2187 case 'x': 2206 case 'x':
2188 case 'X': 2207 case 'X':
2189 state = st_x1; 2208 state = st_x1;
2190 c1 = 0; 2209 c1 = 0;
2191 break;
2192
2193 case 'n':
2194 *p++ = '\n';
2195 state = st_ok;
2196 break;
2197
2198 case 't':
2199 *p++ = '\t';
2200 state = st_ok;
2201 break;
2202
2203 case 'r':
2204 *p++ = '\r';
2205 state = st_ok;
2206 break; 2210 break;
2207 2211
2208 default: 2212 default:
2209 *p++ = c; 2213 *p++ = c;
2210 state = st_ok; 2214 state = st_ok;
2262 } 2266 }
2263 } 2267 }
2264} 2268}
2265 2269
2266/* check c is in chars */ 2270/* check c is in chars */
2267ecb_inline int 2271ecb_cold int
2268is_one_of (const char *s, int c) 2272is_one_of (const char *s, int c)
2269{ 2273{
2270 return c == EOF || !!strchr (s, c); 2274 return c == EOF || !!strchr (s, c);
2271} 2275}
2272 2276
2273/* skip white characters */ 2277/* skip white characters */
2274ecb_inline int 2278ecb_cold int
2275skipspace (SCHEME_P) 2279skipspace (SCHEME_P)
2276{ 2280{
2277 int c, curr_line = 0; 2281 int c, curr_line = 0;
2278 2282
2279 do 2283 do
2299 backchar (SCHEME_A_ c); 2303 backchar (SCHEME_A_ c);
2300 return 1; 2304 return 1;
2301} 2305}
2302 2306
2303/* get token */ 2307/* get token */
2304static int 2308ecb_cold static int
2305token (SCHEME_P) 2309token (SCHEME_P)
2306{ 2310{
2307 int c = skipspace (SCHEME_A); 2311 int c = skipspace (SCHEME_A);
2308 2312
2309 if (c == EOF) 2313 if (c == EOF)
2407} 2411}
2408 2412
2409/* ========== Routines for Printing ========== */ 2413/* ========== Routines for Printing ========== */
2410#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) 2414#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL)
2411 2415
2412static void 2416ecb_cold static void
2413printslashstring (SCHEME_P_ char *p, int len) 2417printslashstring (SCHEME_P_ char *p, int len)
2414{ 2418{
2415 int i; 2419 int i;
2416 unsigned char *s = (unsigned char *) p; 2420 unsigned char *s = (unsigned char *) p;
2417 2421
2473 2477
2474 putcharacter (SCHEME_A_ '"'); 2478 putcharacter (SCHEME_A_ '"');
2475} 2479}
2476 2480
2477/* print atoms */ 2481/* print atoms */
2478static void 2482ecb_cold static void
2479printatom (SCHEME_P_ pointer l, int f) 2483printatom (SCHEME_P_ pointer l, int f)
2480{ 2484{
2481 char *p; 2485 char *p;
2482 int len; 2486 int len;
2483 2487
2484 atom2str (SCHEME_A_ l, f, &p, &len); 2488 atom2str (SCHEME_A_ l, f, &p, &len);
2485 putchars (SCHEME_A_ p, len); 2489 putchars (SCHEME_A_ p, len);
2486} 2490}
2487 2491
2488/* Uses internal buffer unless string pointer is already available */ 2492/* Uses internal buffer unless string pointer is already available */
2489static void 2493ecb_cold static void
2490atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2494atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2491{ 2495{
2492 char *p; 2496 char *p;
2493 2497
2494 if (l == NIL) 2498 if (l == NIL)
2701 return car (d); 2705 return car (d);
2702 2706
2703 p = cons (car (d), cdr (d)); 2707 p = cons (car (d), cdr (d));
2704 q = p; 2708 q = p;
2705 2709
2706 while (cdr (cdr (p)) != NIL) 2710 while (cddr (p) != NIL)
2707 { 2711 {
2708 d = cons (car (p), cdr (p)); 2712 d = cons (car (p), cdr (p));
2709 2713
2710 if (cdr (cdr (p)) != NIL) 2714 if (cddr (p) != NIL)
2711 p = cdr (d); 2715 p = cdr (d);
2712 } 2716 }
2713 2717
2714 set_cdr (p, car (cdr (p))); 2718 set_cdr (p, cadr (p));
2715 return q; 2719 return q;
2716} 2720}
2717 2721
2718/* reverse list -- produce new list */ 2722/* reverse list -- produce new list */
2719static pointer 2723ecb_hot static pointer
2720reverse (SCHEME_P_ pointer a) 2724reverse (SCHEME_P_ pointer a)
2721{ 2725{
2722 /* a must be checked by gc */ 2726 /* a must be checked by gc */
2723 pointer p = NIL; 2727 pointer p = NIL;
2724 2728
2727 2731
2728 return p; 2732 return p;
2729} 2733}
2730 2734
2731/* reverse list --- in-place */ 2735/* reverse list --- in-place */
2732static pointer 2736ecb_hot static pointer
2733reverse_in_place (SCHEME_P_ pointer term, pointer list) 2737reverse_in_place (SCHEME_P_ pointer term, pointer list)
2734{ 2738{
2735 pointer result = term; 2739 pointer result = term;
2736 pointer p = list; 2740 pointer p = list;
2737 2741
2745 2749
2746 return result; 2750 return result;
2747} 2751}
2748 2752
2749/* append list -- produce new list (in reverse order) */ 2753/* append list -- produce new list (in reverse order) */
2750static pointer 2754ecb_hot static pointer
2751revappend (SCHEME_P_ pointer a, pointer b) 2755revappend (SCHEME_P_ pointer a, pointer b)
2752{ 2756{
2753 pointer result = a; 2757 pointer result = a;
2754 pointer p = b; 2758 pointer p = b;
2755 2759
2764 2768
2765 return S_F; /* signal an error */ 2769 return S_F; /* signal an error */
2766} 2770}
2767 2771
2768/* equivalence of atoms */ 2772/* equivalence of atoms */
2769int 2773ecb_hot int
2770eqv (pointer a, pointer b) 2774eqv (pointer a, pointer b)
2771{ 2775{
2772 if (is_string (a)) 2776 if (is_string (a))
2773 { 2777 {
2774 if (is_string (b)) 2778 if (is_string (b))
2868 } 2872 }
2869 else 2873 else
2870 set_car (env, immutable_cons (slot, car (env))); 2874 set_car (env, immutable_cons (slot, car (env)));
2871} 2875}
2872 2876
2873static pointer 2877ecb_hot static pointer
2874find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2878find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2875{ 2879{
2876 pointer x, y; 2880 pointer x, y;
2877 2881
2878 for (x = env; x != NIL; x = cdr (x)) 2882 for (x = env; x != NIL; x = cdr (x))
2899 return NIL; 2903 return NIL;
2900} 2904}
2901 2905
2902#else /* USE_ALIST_ENV */ 2906#else /* USE_ALIST_ENV */
2903 2907
2904ecb_inline void 2908static void
2905new_frame_in_env (SCHEME_P_ pointer old_env) 2909new_frame_in_env (SCHEME_P_ pointer old_env)
2906{ 2910{
2907 SCHEME_V->envir = immutable_cons (NIL, old_env); 2911 SCHEME_V->envir = immutable_cons (NIL, old_env);
2908 setenvironment (SCHEME_V->envir); 2912 setenvironment (SCHEME_V->envir);
2909} 2913}
2910 2914
2911ecb_inline void 2915static void
2912new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2916new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2913{ 2917{
2914 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2918 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2915} 2919}
2916 2920
2917static pointer 2921ecb_hot static pointer
2918find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2922find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2919{ 2923{
2920 pointer x, y; 2924 pointer x, y;
2921 2925
2922 for (x = env; x != NIL; x = cdr (x)) 2926 for (x = env; x != NIL; x = cdr (x))
2936 return NIL; 2940 return NIL;
2937} 2941}
2938 2942
2939#endif /* USE_ALIST_ENV else */ 2943#endif /* USE_ALIST_ENV else */
2940 2944
2941ecb_inline void 2945static void
2942new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2946new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2943{ 2947{
2944 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 2948 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2945 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2949 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2946} 2950}
2947 2951
2948ecb_inline void 2952static void
2949set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2953set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2950{ 2954{
2951 set_cdr (slot, value); 2955 set_cdr (slot, value);
2952} 2956}
2953 2957
2954ecb_inline pointer 2958static pointer
2955slot_value_in_env (pointer slot) 2959slot_value_in_env (pointer slot)
2956{ 2960{
2957 return cdr (slot); 2961 return cdr (slot);
2958} 2962}
2959 2963
2960/* ========== Evaluation Cycle ========== */ 2964/* ========== Evaluation Cycle ========== */
2961 2965
2962static int 2966ecb_cold static int
2963xError_1 (SCHEME_P_ const char *s, pointer a) 2967xError_1 (SCHEME_P_ const char *s, pointer a)
2964{ 2968{
2965#if USE_ERROR_HOOK 2969#if USE_ERROR_HOOK
2966 pointer x; 2970 pointer x;
2967 pointer hdl = SCHEME_V->ERROR_HOOK; 2971 pointer hdl = SCHEME_V->ERROR_HOOK;
3043 pointer code; 3047 pointer code;
3044}; 3048};
3045 3049
3046# define STACK_GROWTH 3 3050# define STACK_GROWTH 3
3047 3051
3048static void 3052ecb_hot static void
3049s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3053s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3050{ 3054{
3051 int nframes = (uintptr_t)SCHEME_V->dump; 3055 int nframes = (uintptr_t)SCHEME_V->dump;
3052 struct dump_stack_frame *next_frame; 3056 struct dump_stack_frame *next_frame;
3053 3057
3054 /* enough room for the next frame? */ 3058 /* enough room for the next frame? */
3055 if (nframes >= SCHEME_V->dump_size) 3059 if (ecb_expect_false (nframes >= SCHEME_V->dump_size))
3056 { 3060 {
3057 SCHEME_V->dump_size += STACK_GROWTH; 3061 SCHEME_V->dump_size += STACK_GROWTH;
3058 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); 3062 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size);
3059 } 3063 }
3060 3064
3066 next_frame->code = code; 3070 next_frame->code = code;
3067 3071
3068 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3072 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3069} 3073}
3070 3074
3071static int 3075static ecb_hot int
3072xs_return (SCHEME_P_ pointer a) 3076xs_return (SCHEME_P_ pointer a)
3073{ 3077{
3074 int nframes = (uintptr_t)SCHEME_V->dump; 3078 int nframes = (uintptr_t)SCHEME_V->dump;
3075 struct dump_stack_frame *frame; 3079 struct dump_stack_frame *frame;
3076 3080
3087 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3091 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3088 3092
3089 return 0; 3093 return 0;
3090} 3094}
3091 3095
3092ecb_inline void 3096ecb_cold void
3093dump_stack_reset (SCHEME_P) 3097dump_stack_reset (SCHEME_P)
3094{ 3098{
3095 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3099 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3096 SCHEME_V->dump = (pointer)+0; 3100 SCHEME_V->dump = (pointer)+0;
3097} 3101}
3098 3102
3099ecb_inline void 3103ecb_cold void
3100dump_stack_initialize (SCHEME_P) 3104dump_stack_initialize (SCHEME_P)
3101{ 3105{
3102 SCHEME_V->dump_size = 0; 3106 SCHEME_V->dump_size = 0;
3103 SCHEME_V->dump_base = 0; 3107 SCHEME_V->dump_base = 0;
3104 dump_stack_reset (SCHEME_A); 3108 dump_stack_reset (SCHEME_A);
3105} 3109}
3106 3110
3107static void 3111ecb_cold static void
3108dump_stack_free (SCHEME_P) 3112dump_stack_free (SCHEME_P)
3109{ 3113{
3110 free (SCHEME_V->dump_base); 3114 free (SCHEME_V->dump_base);
3111 SCHEME_V->dump_base = 0; 3115 SCHEME_V->dump_base = 0;
3112 SCHEME_V->dump = (pointer)0; 3116 SCHEME_V->dump = (pointer)0;
3113 SCHEME_V->dump_size = 0; 3117 SCHEME_V->dump_size = 0;
3114} 3118}
3115 3119
3116static void 3120ecb_cold static void
3117dump_stack_mark (SCHEME_P) 3121dump_stack_mark (SCHEME_P)
3118{ 3122{
3119 int nframes = (uintptr_t)SCHEME_V->dump; 3123 int nframes = (uintptr_t)SCHEME_V->dump;
3120 int i; 3124 int i;
3121 3125
3127 mark (frame->envir); 3131 mark (frame->envir);
3128 mark (frame->code); 3132 mark (frame->code);
3129 } 3133 }
3130} 3134}
3131 3135
3132static pointer 3136ecb_cold static pointer
3133ss_get_cont (SCHEME_P) 3137ss_get_cont (SCHEME_P)
3134{ 3138{
3135 int nframes = (uintptr_t)SCHEME_V->dump; 3139 int nframes = (uintptr_t)SCHEME_V->dump;
3136 int i; 3140 int i;
3137 3141
3149 } 3153 }
3150 3154
3151 return cont; 3155 return cont;
3152} 3156}
3153 3157
3154static void 3158ecb_cold static void
3155ss_set_cont (SCHEME_P_ pointer cont) 3159ss_set_cont (SCHEME_P_ pointer cont)
3156{ 3160{
3157 int i = 0; 3161 int i = 0;
3158 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3162 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3159 3163
3171 SCHEME_V->dump = (pointer)(uintptr_t)i; 3175 SCHEME_V->dump = (pointer)(uintptr_t)i;
3172} 3176}
3173 3177
3174#else 3178#else
3175 3179
3176ecb_inline void 3180ecb_cold void
3177dump_stack_reset (SCHEME_P) 3181dump_stack_reset (SCHEME_P)
3178{ 3182{
3179 SCHEME_V->dump = NIL; 3183 SCHEME_V->dump = NIL;
3180} 3184}
3181 3185
3182ecb_inline void 3186ecb_cold void
3183dump_stack_initialize (SCHEME_P) 3187dump_stack_initialize (SCHEME_P)
3184{ 3188{
3185 dump_stack_reset (SCHEME_A); 3189 dump_stack_reset (SCHEME_A);
3186} 3190}
3187 3191
3188static void 3192ecb_cold static void
3189dump_stack_free (SCHEME_P) 3193dump_stack_free (SCHEME_P)
3190{ 3194{
3191 SCHEME_V->dump = NIL; 3195 SCHEME_V->dump = NIL;
3192} 3196}
3193 3197
3194static int 3198ecb_hot static int
3195xs_return (SCHEME_P_ pointer a) 3199xs_return (SCHEME_P_ pointer a)
3196{ 3200{
3197 pointer dump = SCHEME_V->dump; 3201 pointer dump = SCHEME_V->dump;
3198 3202
3199 SCHEME_V->value = a; 3203 SCHEME_V->value = a;
3209 SCHEME_V->dump = dump; 3213 SCHEME_V->dump = dump;
3210 3214
3211 return 0; 3215 return 0;
3212} 3216}
3213 3217
3214static void 3218ecb_hot static void
3215s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3219s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3216{ 3220{
3217 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), 3221 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op),
3218 cons (args, 3222 cons (args,
3219 cons (SCHEME_V->envir, 3223 cons (SCHEME_V->envir,
3220 cons (code, 3224 cons (code,
3221 SCHEME_V->dump)))); 3225 SCHEME_V->dump))));
3222} 3226}
3223 3227
3224static void 3228ecb_cold static void
3225dump_stack_mark (SCHEME_P) 3229dump_stack_mark (SCHEME_P)
3226{ 3230{
3227 mark (SCHEME_V->dump); 3231 mark (SCHEME_V->dump);
3228} 3232}
3229 3233
3230static pointer 3234ecb_cold static pointer
3231ss_get_cont (SCHEME_P) 3235ss_get_cont (SCHEME_P)
3232{ 3236{
3233 return SCHEME_V->dump; 3237 return SCHEME_V->dump;
3234} 3238}
3235 3239
3236static void 3240ecb_cold static void
3237ss_set_cont (SCHEME_P_ pointer cont) 3241ss_set_cont (SCHEME_P_ pointer cont)
3238{ 3242{
3239 SCHEME_V->dump = cont; 3243 SCHEME_V->dump = cont;
3240} 3244}
3241 3245
3242#endif 3246#endif
3243 3247
3244#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3248#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3245 3249
3246#if 1 3250#if EXPERIMENT
3247static int 3251static int
3248debug (SCHEME_P_ int indent, pointer x) 3252debug (SCHEME_P_ int indent, pointer x)
3249{ 3253{
3250 int c; 3254 int c;
3251 3255
3299 break; 3303 break;
3300 } 3304 }
3301} 3305}
3302#endif 3306#endif
3303 3307
3304static int 3308/* syntax, eval, core, ... */
3309ecb_hot static int
3305opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3310opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3306{ 3311{
3307 pointer args = SCHEME_V->args; 3312 pointer args = SCHEME_V->args;
3308 pointer x, y; 3313 pointer x, y;
3309 3314
3310 switch (op) 3315 switch (op)
3311 { 3316 {
3312#if 1 //D 3317#if EXPERIMENT //D
3313 case OP_DEBUG: 3318 case OP_DEBUG:
3314 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); 3319 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8);
3315 printf ("\n"); 3320 printf ("\n");
3316 s_return (S_T); 3321 s_return (S_T);
3317#endif 3322#endif
3318 case OP_LOAD: /* load */ 3323 case OP_LOAD: /* load */
3319 if (file_interactive (SCHEME_A)) 3324 if (file_interactive (SCHEME_A))
3320 { 3325 {
3321 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); 3326 putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n");
3322 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); 3327 //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args)));
3323 } 3328 }
3324 3329
3325 if (!file_push (SCHEME_A_ strvalue (car (args)))) 3330 if (!file_push (SCHEME_A_ strvalue (car (args))))
3326 Error_1 ("unable to open", car (args)); 3331 Error_1 ("unable to open", car (args));
3327 else 3332 else
3331 } 3336 }
3332 3337
3333 case OP_T0LVL: /* top level */ 3338 case OP_T0LVL: /* top level */
3334 3339
3335 /* If we reached the end of file, this loop is done. */ 3340 /* If we reached the end of file, this loop is done. */
3336 if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) 3341 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3337 { 3342 {
3338 if (SCHEME_V->file_i == 0) 3343 if (SCHEME_V->file_i == 0)
3339 { 3344 {
3340 SCHEME_V->args = NIL; 3345 SCHEME_V->args = NIL;
3341 s_goto (OP_QUIT); 3346 s_goto (OP_QUIT);
3419#endif 3424#endif
3420 if (is_symbol (SCHEME_V->code)) /* symbol */ 3425 if (is_symbol (SCHEME_V->code)) /* symbol */
3421 { 3426 {
3422 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); 3427 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1);
3423 3428
3424 if (x != NIL) 3429 if (x == NIL)
3425 s_return (slot_value_in_env (x));
3426 else
3427 Error_1 ("eval: unbound variable:", SCHEME_V->code); 3430 Error_1 ("eval: unbound variable:", SCHEME_V->code);
3431
3432 s_return (slot_value_in_env (x));
3428 } 3433 }
3429 else if (is_pair (SCHEME_V->code)) 3434 else if (is_pair (SCHEME_V->code))
3430 { 3435 {
3431 x = car (SCHEME_V->code); 3436 x = car (SCHEME_V->code);
3432 3437
3509 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3514 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3510 else if (is_foreign (SCHEME_V->code)) 3515 else if (is_foreign (SCHEME_V->code))
3511 { 3516 {
3512 /* Keep nested calls from GC'ing the arglist */ 3517 /* Keep nested calls from GC'ing the arglist */
3513 push_recent_alloc (SCHEME_A_ args, NIL); 3518 push_recent_alloc (SCHEME_A_ args, NIL);
3514 x = SCHEME_V->code->object.ff (SCHEME_A_ args); 3519 x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args);
3515 3520
3516 s_return (x); 3521 s_return (x);
3517 } 3522 }
3518 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ 3523 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */
3519 { 3524 {
3556 3561
3557 case OP_DOMACRO: /* do macro */ 3562 case OP_DOMACRO: /* do macro */
3558 SCHEME_V->code = SCHEME_V->value; 3563 SCHEME_V->code = SCHEME_V->value;
3559 s_goto (OP_EVAL); 3564 s_goto (OP_EVAL);
3560 3565
3561#if 1
3562
3563 case OP_LAMBDA: /* lambda */ 3566 case OP_LAMBDA: /* lambda */
3564 /* If the hook is defined, apply it to SCHEME_V->code, otherwise 3567 /* If the hook is defined, apply it to SCHEME_V->code, otherwise
3565 set SCHEME_V->value fall thru */ 3568 set SCHEME_V->value fall thru */
3566 { 3569 {
3567 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); 3570 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1);
3573 SCHEME_V->code = slot_value_in_env (f); 3576 SCHEME_V->code = slot_value_in_env (f);
3574 s_goto (OP_APPLY); 3577 s_goto (OP_APPLY);
3575 } 3578 }
3576 3579
3577 SCHEME_V->value = SCHEME_V->code; 3580 SCHEME_V->value = SCHEME_V->code;
3578 /* Fallthru */
3579 } 3581 }
3582 /* Fallthru */
3580 3583
3581 case OP_LAMBDA1: 3584 case OP_LAMBDA1:
3582 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); 3585 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir));
3583
3584#else
3585
3586 case OP_LAMBDA: /* lambda */
3587 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir));
3588
3589#endif
3590 3586
3591 case OP_MKCLOSURE: /* make-closure */ 3587 case OP_MKCLOSURE: /* make-closure */
3592 x = car (args); 3588 x = car (args);
3593 3589
3594 if (car (x) == SCHEME_V->LAMBDA) 3590 if (car (x) == SCHEME_V->LAMBDA)
4004 } 4000 }
4005 4001
4006 if (USE_ERROR_CHECKING) abort (); 4002 if (USE_ERROR_CHECKING) abort ();
4007} 4003}
4008 4004
4009static int 4005/* math, cxr */
4006ecb_hot static int
4010opexe_1 (SCHEME_P_ enum scheme_opcodes op) 4007opexe_1 (SCHEME_P_ enum scheme_opcodes op)
4011{ 4008{
4012 pointer args = SCHEME_V->args; 4009 pointer args = SCHEME_V->args;
4013 pointer x = car (args); 4010 pointer x = car (args);
4014 num v; 4011 num v;
4015 4012
4016 switch (op) 4013 switch (op)
4017 { 4014 {
4018#if USE_MATH 4015#if USE_MATH
4019 case OP_INEX2EX: /* inexact->exact */ 4016 case OP_INEX2EX: /* inexact->exact */
4020 {
4021 if (is_integer (x)) 4017 if (!is_integer (x))
4022 s_return (x); 4018 {
4023
4024 RVALUE r = rvalue_unchecked (x); 4019 RVALUE r = rvalue_unchecked (x);
4025 4020
4026 if (r == (RVALUE)(IVALUE)r) 4021 if (r == (RVALUE)(IVALUE)r)
4027 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); 4022 x = mk_integer (SCHEME_A_ rvalue_unchecked (x));
4028 else 4023 else
4029 Error_1 ("inexact->exact: not integral:", x); 4024 Error_1 ("inexact->exact: not integral:", x);
4030 } 4025 }
4031 4026
4027 s_return (x);
4028
4029 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4030 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4031 case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x))));
4032 case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x))));
4033
4034 case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4032 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4035 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
4033 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 4036 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))
4037 / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args))))));
4034 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 4038 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
4035 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 4039 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
4036 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); 4040 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x))));
4037 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); 4041 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x))));
4038 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); 4042 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x))));
4039 4043
4040 case OP_ATAN: 4044 case OP_ATAN:
4045 s_return (mk_real (SCHEME_A_
4041 if (cdr (args) == NIL) 4046 cdr (args) == NIL
4042 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 4047 ? atan (rvalue (x))
4043 else 4048 : atan2 (rvalue (x), rvalue (cadr (args)))));
4044 {
4045 pointer y = cadr (args);
4046 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
4047 }
4048
4049 case OP_SQRT:
4050 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4051 4049
4052 case OP_EXPT: 4050 case OP_EXPT:
4053 { 4051 {
4054 RVALUE result; 4052 RVALUE result;
4055 int real_result = 1; 4053 int real_result = 1;
4078 if (real_result) 4076 if (real_result)
4079 s_return (mk_real (SCHEME_A_ result)); 4077 s_return (mk_real (SCHEME_A_ result));
4080 else 4078 else
4081 s_return (mk_integer (SCHEME_A_ result)); 4079 s_return (mk_integer (SCHEME_A_ result));
4082 } 4080 }
4083
4084 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4085 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4086
4087 case OP_TRUNCATE:
4088 {
4089 RVALUE n = rvalue (x);
4090 s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n)));
4091 }
4092
4093 case OP_ROUND:
4094 if (is_integer (x))
4095 s_return (x);
4096
4097 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4098#endif 4081#endif
4099 4082
4100 case OP_ADD: /* + */ 4083 case OP_ADD: /* + */
4101 v = num_zero; 4084 v = num_zero;
4102 4085
4190 else 4173 else
4191 Error_0 ("modulo: division by zero"); 4174 Error_0 ("modulo: division by zero");
4192 4175
4193 s_return (mk_number (SCHEME_A_ v)); 4176 s_return (mk_number (SCHEME_A_ v));
4194 4177
4195 case OP_CAR: /* car */ 4178 /* the compiler will optimize this mess... */
4196 s_return (caar (args)); 4179 case OP_CAR: op_car: s_return (car (x));
4197 4180 case OP_CDR: op_cdr: s_return (cdr (x));
4198 case OP_CDR: /* cdr */ 4181 case OP_CAAR: op_caar: x = car (x); goto op_car;
4199 s_return (cdar (args)); 4182 case OP_CADR: op_cadr: x = cdr (x); goto op_car;
4183 case OP_CDAR: op_cdar: x = car (x); goto op_cdr;
4184 case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr;
4185 case OP_CAAAR: op_caaar: x = car (x); goto op_caar;
4186 case OP_CAADR: op_caadr: x = cdr (x); goto op_caar;
4187 case OP_CADAR: op_cadar: x = car (x); goto op_cadr;
4188 case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr;
4189 case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar;
4190 case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar;
4191 case OP_CDDAR: op_cddar: x = car (x); goto op_cddr;
4192 case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr;
4193 case OP_CAAAAR: x = car (x); goto op_caaar;
4194 case OP_CAAADR: x = cdr (x); goto op_caaar;
4195 case OP_CAADAR: x = car (x); goto op_caadr;
4196 case OP_CAADDR: x = cdr (x); goto op_caadr;
4197 case OP_CADAAR: x = car (x); goto op_cadar;
4198 case OP_CADADR: x = cdr (x); goto op_cadar;
4199 case OP_CADDAR: x = car (x); goto op_caddr;
4200 case OP_CADDDR: x = cdr (x); goto op_caddr;
4201 case OP_CDAAAR: x = car (x); goto op_cdaar;
4202 case OP_CDAADR: x = cdr (x); goto op_cdaar;
4203 case OP_CDADAR: x = car (x); goto op_cdadr;
4204 case OP_CDADDR: x = cdr (x); goto op_cdadr;
4205 case OP_CDDAAR: x = car (x); goto op_cddar;
4206 case OP_CDDADR: x = cdr (x); goto op_cddar;
4207 case OP_CDDDAR: x = car (x); goto op_cdddr;
4208 case OP_CDDDDR: x = cdr (x); goto op_cdddr;
4200 4209
4201 case OP_CONS: /* cons */ 4210 case OP_CONS: /* cons */
4202 set_cdr (args, cadr (args)); 4211 set_cdr (args, cadr (args));
4203 s_return (args); 4212 s_return (args);
4204 4213
4378 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4387 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4379 4388
4380 s_return (newstr); 4389 s_return (newstr);
4381 } 4390 }
4382 4391
4383 case OP_SUBSTR: /* substring */ 4392 case OP_STRING_COPY: /* substring/string-copy */
4384 { 4393 {
4385 char *str = strvalue (x); 4394 char *str = strvalue (x);
4386 int index0 = ivalue_unchecked (cadr (args)); 4395 int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args));
4387 int index1; 4396 int index1;
4388 int len; 4397 int len;
4389 4398
4390 if (index0 > strlength (x)) 4399 if (index0 > strlength (x))
4391 Error_1 ("substring: start out of bounds:", cadr (args)); 4400 Error_1 ("string->copy: start out of bounds:", cadr (args));
4392 4401
4393 if (cddr (args) != NIL) 4402 if (cddr (args) != NIL)
4394 { 4403 {
4395 index1 = ivalue_unchecked (caddr (args)); 4404 index1 = ivalue_unchecked (caddr (args));
4396 4405
4397 if (index1 > strlength (x) || index1 < index0) 4406 if (index1 > strlength (x) || index1 < index0)
4398 Error_1 ("substring: end out of bounds:", caddr (args)); 4407 Error_1 ("string->copy: end out of bounds:", caddr (args));
4399 } 4408 }
4400 else 4409 else
4401 index1 = strlength (x); 4410 index1 = strlength (x);
4402 4411
4403 len = index1 - index0; 4412 len = index1 - index0;
4404 x = mk_empty_string (SCHEME_A_ len, ' '); 4413 x = mk_counted_string (SCHEME_A_ str + index0, len);
4405 memcpy (strvalue (x), str + index0, len);
4406 strvalue (x)[len] = 0;
4407 4414
4408 s_return (x); 4415 s_return (x);
4409 } 4416 }
4410 4417
4411 case OP_VECTOR: /* vector */ 4418 case OP_VECTOR: /* vector */
4485 } 4492 }
4486 4493
4487 if (USE_ERROR_CHECKING) abort (); 4494 if (USE_ERROR_CHECKING) abort ();
4488} 4495}
4489 4496
4490static int 4497/* relational ops */
4498ecb_hot static int
4491opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4499opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4492{ 4500{
4493 pointer x = SCHEME_V->args; 4501 pointer x = SCHEME_V->args;
4494 4502
4495 for (;;) 4503 for (;;)
4516 } 4524 }
4517 4525
4518 s_return (S_T); 4526 s_return (S_T);
4519} 4527}
4520 4528
4521static int 4529/* predicates */
4530ecb_hot static int
4522opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4531opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4523{ 4532{
4524 pointer args = SCHEME_V->args; 4533 pointer args = SCHEME_V->args;
4525 pointer a = car (args); 4534 pointer a = car (args);
4526 pointer d = cdr (args); 4535 pointer d = cdr (args);
4527 int r; 4536 int r;
4528 4537
4529 switch (op) 4538 switch (op)
4530 { 4539 {
4531 case OP_NOT: /* not */ r = is_false (a) ; break; 4540 case OP_NOT: /* not */ r = is_false (a) ; break;
4532 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; 4541 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break;
4533 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; 4542 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break;
4534 case OP_NULLP: /* null? */ r = a == NIL ; break; 4543 case OP_NULLP: /* null? */ r = a == NIL ; break;
4535 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; 4544 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break;
4545 case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break;
4536 case OP_NUMBERP: /* number? */ r = is_number (a) ; break; 4546 case OP_NUMBERP: /* number? */ r = is_number (a) ; break;
4537 case OP_STRINGP: /* string? */ r = is_string (a) ; break; 4547 case OP_STRINGP: /* string? */ r = is_string (a) ; break;
4538 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4548 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4539 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4549 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4540 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4550 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4541 4551
4542#if USE_CHAR_CLASSIFIERS 4552#if USE_CHAR_CLASSIFIERS
4543 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; 4553 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4544 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; 4554 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4545 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; 4555 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4572 } 4582 }
4573 4583
4574 s_retbool (r); 4584 s_retbool (r);
4575} 4585}
4576 4586
4577static int 4587/* promises, list ops, ports */
4588ecb_hot static int
4578opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4589opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4579{ 4590{
4580 pointer args = SCHEME_V->args; 4591 pointer args = SCHEME_V->args;
4581 pointer a = car (args); 4592 pointer a = car (args);
4582 pointer x, y; 4593 pointer x, y;
4595 } 4606 }
4596 else 4607 else
4597 s_return (SCHEME_V->code); 4608 s_return (SCHEME_V->code);
4598 4609
4599 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4610 case OP_SAVE_FORCED: /* Save forced value replacing promise */
4600 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); 4611 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
4601 s_return (SCHEME_V->value); 4612 s_return (SCHEME_V->value);
4602 4613
4603#if USE_PORTS 4614#if USE_PORTS
4615
4616 case OP_EOF_OBJECT: /* eof-object */
4617 s_return (S_EOF);
4604 4618
4605 case OP_WRITE: /* write */ 4619 case OP_WRITE: /* write */
4606 case OP_DISPLAY: /* display */ 4620 case OP_DISPLAY: /* display */
4607 case OP_WRITE_CHAR: /* write-char */ 4621 case OP_WRITE_CHAR: /* write-char */
4608 if (is_pair (cdr (SCHEME_V->args))) 4622 if (is_pair (cdr (SCHEME_V->args)))
4750 SCHEME_V->gc_verbose = (a != S_F); 4764 SCHEME_V->gc_verbose = (a != S_F);
4751 s_retbool (was); 4765 s_retbool (was);
4752 } 4766 }
4753 4767
4754 case OP_NEWSEGMENT: /* new-segment */ 4768 case OP_NEWSEGMENT: /* new-segment */
4769#if 0
4755 if (!is_pair (args) || !is_number (a)) 4770 if (!is_pair (args) || !is_number (a))
4756 Error_0 ("new-segment: argument must be a number"); 4771 Error_0 ("new-segment: argument must be a number");
4757 4772#endif
4758 alloc_cellseg (SCHEME_A_ ivalue (a)); 4773 s_retbool (alloc_cellseg (SCHEME_A));
4759
4760 s_return (S_T);
4761 4774
4762 case OP_OBLIST: /* oblist */ 4775 case OP_OBLIST: /* oblist */
4763 s_return (oblist_all_symbols (SCHEME_A)); 4776 s_return (oblist_all_symbols (SCHEME_A));
4764 4777
4765#if USE_PORTS 4778#if USE_PORTS
4835 s_return (p == NIL ? S_F : p); 4848 s_return (p == NIL ? S_F : p);
4836 } 4849 }
4837 4850
4838 case OP_GET_OUTSTRING: /* get-output-string */ 4851 case OP_GET_OUTSTRING: /* get-output-string */
4839 { 4852 {
4840 port *p; 4853 port *p = port (a);
4841 4854
4842 if ((p = a->object.port)->kind & port_string) 4855 if (p->kind & port_string)
4843 { 4856 {
4844 off_t size; 4857 off_t size;
4845 char *str; 4858 char *str;
4846 4859
4847 size = p->rep.string.curr - p->rep.string.start + 1; 4860 size = p->rep.string.curr - p->rep.string.start + 1;
4882 } 4895 }
4883 4896
4884 if (USE_ERROR_CHECKING) abort (); 4897 if (USE_ERROR_CHECKING) abort ();
4885} 4898}
4886 4899
4887static int 4900/* reading */
4901ecb_cold static int
4888opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4902opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4889{ 4903{
4890 pointer args = SCHEME_V->args; 4904 pointer args = SCHEME_V->args;
4891 pointer x; 4905 pointer x;
4892 4906
4952 int res; 4966 int res;
4953 4967
4954 if (is_pair (args)) 4968 if (is_pair (args))
4955 p = car (args); 4969 p = car (args);
4956 4970
4957 res = p->object.port->kind & port_string; 4971 res = port (p)->kind & port_string;
4958 4972
4959 s_retbool (res); 4973 s_retbool (res);
4960 } 4974 }
4961 4975
4962 case OP_SET_INPORT: /* set-input-port */ 4976 case OP_SET_INPORT: /* set-input-port */
5234 } 5248 }
5235 5249
5236 if (USE_ERROR_CHECKING) abort (); 5250 if (USE_ERROR_CHECKING) abort ();
5237} 5251}
5238 5252
5239static int 5253/* list ops */
5254ecb_hot static int
5240opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5255opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5241{ 5256{
5242 pointer args = SCHEME_V->args; 5257 pointer args = SCHEME_V->args;
5243 pointer a = car (args); 5258 pointer a = car (args);
5244 pointer x, y; 5259 pointer x, y;
5302 5317
5303/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5318/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5304typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); 5319typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5305 5320
5306typedef int (*test_predicate)(pointer); 5321typedef int (*test_predicate)(pointer);
5307static int 5322
5323ecb_hot static int
5308tst_any (pointer p) 5324tst_any (pointer p)
5309{ 5325{
5310 return 1; 5326 return 1;
5311} 5327}
5312 5328
5313static int 5329ecb_hot static int
5314tst_inonneg (pointer p) 5330tst_inonneg (pointer p)
5315{ 5331{
5316 return is_integer (p) && ivalue_unchecked (p) >= 0; 5332 return is_integer (p) && ivalue_unchecked (p) >= 0;
5317} 5333}
5318 5334
5319static int 5335ecb_hot static int
5320tst_is_list (SCHEME_P_ pointer p) 5336tst_is_list (SCHEME_P_ pointer p)
5321{ 5337{
5322 return p == NIL || is_pair (p); 5338 return p == NIL || is_pair (p);
5323} 5339}
5324 5340
5367#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" 5383#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5368#include "opdefines.h" 5384#include "opdefines.h"
5369#undef OP_DEF 5385#undef OP_DEF
5370; 5386;
5371 5387
5372static const char * 5388ecb_cold static const char *
5373opname (int idx) 5389opname (int idx)
5374{ 5390{
5375 const char *name = opnames; 5391 const char *name = opnames;
5376 5392
5377 /* should do this at compile time, but would require external program, right? */ 5393 /* should do this at compile time, but would require external program, right? */
5379 name += strlen (name) + 1; 5395 name += strlen (name) + 1;
5380 5396
5381 return *name ? name : "ILLEGAL"; 5397 return *name ? name : "ILLEGAL";
5382} 5398}
5383 5399
5384static const char * 5400ecb_cold static const char *
5385procname (pointer x) 5401procname (pointer x)
5386{ 5402{
5387 return opname (procnum (x)); 5403 return opname (procnum (x));
5388} 5404}
5389 5405
5409#undef OP_DEF 5425#undef OP_DEF
5410 {0} 5426 {0}
5411}; 5427};
5412 5428
5413/* kernel of this interpreter */ 5429/* kernel of this interpreter */
5414static void ecb_hot 5430ecb_hot static void
5415Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5431Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5416{ 5432{
5417 SCHEME_V->op = op; 5433 SCHEME_V->op = op;
5418 5434
5419 for (;;) 5435 for (;;)
5502 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) 5518 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5503 return; 5519 return;
5504 5520
5505 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5521 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5506 { 5522 {
5507 xwrstr ("No memory!\n"); 5523 putstr (SCHEME_A_ "No memory!\n");
5508 return; 5524 return;
5509 } 5525 }
5510 } 5526 }
5511} 5527}
5512 5528
5513/* ========== Initialization of internal keywords ========== */ 5529/* ========== Initialization of internal keywords ========== */
5514 5530
5515static void 5531ecb_cold static void
5516assign_syntax (SCHEME_P_ const char *name) 5532assign_syntax (SCHEME_P_ const char *name)
5517{ 5533{
5518 pointer x = oblist_add_by_name (SCHEME_A_ name); 5534 pointer x = oblist_add_by_name (SCHEME_A_ name);
5519 set_typeflag (x, typeflag (x) | T_SYNTAX); 5535 set_typeflag (x, typeflag (x) | T_SYNTAX);
5520} 5536}
5521 5537
5522static void 5538ecb_cold static void
5523assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) 5539assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
5524{ 5540{
5525 pointer x = mk_symbol (SCHEME_A_ name); 5541 pointer x = mk_symbol (SCHEME_A_ name);
5526 pointer y = mk_proc (SCHEME_A_ op); 5542 pointer y = mk_proc (SCHEME_A_ op);
5527 new_slot_in_env (SCHEME_A_ x, y); 5543 new_slot_in_env (SCHEME_A_ x, y);
5535 ivalue_unchecked (y) = op; 5551 ivalue_unchecked (y) = op;
5536 return y; 5552 return y;
5537} 5553}
5538 5554
5539/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5555/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5540static int 5556ecb_hot static int
5541syntaxnum (pointer p) 5557syntaxnum (pointer p)
5542{ 5558{
5543 const char *s = strvalue (p); 5559 const char *s = strvalue (p);
5544 5560
5545 switch (strlength (p)) 5561 switch (strlength (p))
5625ecb_cold int 5641ecb_cold int
5626scheme_init (SCHEME_P) 5642scheme_init (SCHEME_P)
5627{ 5643{
5628 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5644 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5629 pointer x; 5645 pointer x;
5646
5647 /* this memset is not strictly correct, as we assume (intcache)
5648 * that memset 0 will also set pointers to 0, but memset does
5649 * of course not guarantee that. screw such systems.
5650 */
5651 memset (SCHEME_V, 0, sizeof (*SCHEME_V));
5630 5652
5631 num_set_fixnum (num_zero, 1); 5653 num_set_fixnum (num_zero, 1);
5632 num_set_ivalue (num_zero, 0); 5654 num_set_ivalue (num_zero, 0);
5633 num_set_fixnum (num_one, 1); 5655 num_set_fixnum (num_one, 1);
5634 num_set_ivalue (num_one, 1); 5656 num_set_ivalue (num_one, 1);
5646 SCHEME_V->save_inport = NIL; 5668 SCHEME_V->save_inport = NIL;
5647 SCHEME_V->loadport = NIL; 5669 SCHEME_V->loadport = NIL;
5648 SCHEME_V->nesting = 0; 5670 SCHEME_V->nesting = 0;
5649 SCHEME_V->interactive_repl = 0; 5671 SCHEME_V->interactive_repl = 0;
5650 5672
5651 if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) 5673 if (!alloc_cellseg (SCHEME_A))
5652 { 5674 {
5653#if USE_ERROR_CHECKING 5675#if USE_ERROR_CHECKING
5654 SCHEME_V->no_memory = 1; 5676 SCHEME_V->no_memory = 1;
5655 return 0; 5677 return 0;
5656#endif 5678#endif
5724 5746
5725 return !SCHEME_V->no_memory; 5747 return !SCHEME_V->no_memory;
5726} 5748}
5727 5749
5728#if USE_PORTS 5750#if USE_PORTS
5729void 5751ecb_cold void
5730scheme_set_input_port_file (SCHEME_P_ int fin) 5752scheme_set_input_port_file (SCHEME_P_ int fin)
5731{ 5753{
5732 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); 5754 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input);
5733} 5755}
5734 5756
5735void 5757ecb_cold void
5736scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) 5758scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end)
5737{ 5759{
5738 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); 5760 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input);
5739} 5761}
5740 5762
5741void 5763ecb_cold void
5742scheme_set_output_port_file (SCHEME_P_ int fout) 5764scheme_set_output_port_file (SCHEME_P_ int fout)
5743{ 5765{
5744 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); 5766 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output);
5745} 5767}
5746 5768
5747void 5769ecb_cold void
5748scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) 5770scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end)
5749{ 5771{
5750 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); 5772 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output);
5751} 5773}
5752#endif 5774#endif
5753 5775
5754void 5776ecb_cold void
5755scheme_set_external_data (SCHEME_P_ void *p) 5777scheme_set_external_data (SCHEME_P_ void *p)
5756{ 5778{
5757 SCHEME_V->ext_data = p; 5779 SCHEME_V->ext_data = p;
5758} 5780}
5759 5781
5807 } 5829 }
5808 } 5830 }
5809#endif 5831#endif
5810} 5832}
5811 5833
5812void 5834ecb_cold void
5813scheme_load_file (SCHEME_P_ int fin) 5835scheme_load_file (SCHEME_P_ int fin)
5814{ 5836{
5815 scheme_load_named_file (SCHEME_A_ fin, 0); 5837 scheme_load_named_file (SCHEME_A_ fin, 0);
5816} 5838}
5817 5839
5818void 5840ecb_cold void
5819scheme_load_named_file (SCHEME_P_ int fin, const char *filename) 5841scheme_load_named_file (SCHEME_P_ int fin, const char *filename)
5820{ 5842{
5821 dump_stack_reset (SCHEME_A); 5843 dump_stack_reset (SCHEME_A);
5822 SCHEME_V->envir = SCHEME_V->global_env; 5844 SCHEME_V->envir = SCHEME_V->global_env;
5823 SCHEME_V->file_i = 0; 5845 SCHEME_V->file_i = 0;
5850 5872
5851 if (SCHEME_V->retcode == 0) 5873 if (SCHEME_V->retcode == 0)
5852 SCHEME_V->retcode = SCHEME_V->nesting != 0; 5874 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5853} 5875}
5854 5876
5855void 5877ecb_cold void
5856scheme_load_string (SCHEME_P_ const char *cmd) 5878scheme_load_string (SCHEME_P_ const char *cmd)
5857{ 5879{
5858 dump_stack_reset (SCHEME_A); 5880 dump_stack_reset (SCHEME_A);
5859 SCHEME_V->envir = SCHEME_V->global_env; 5881 SCHEME_V->envir = SCHEME_V->global_env;
5860 SCHEME_V->file_i = 0; 5882 SCHEME_V->file_i = 0;
5874 5896
5875 if (SCHEME_V->retcode == 0) 5897 if (SCHEME_V->retcode == 0)
5876 SCHEME_V->retcode = SCHEME_V->nesting != 0; 5898 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5877} 5899}
5878 5900
5879void 5901ecb_cold void
5880scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) 5902scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value)
5881{ 5903{
5882 pointer x; 5904 pointer x;
5883 5905
5884 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 5906 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
5889 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 5911 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
5890} 5912}
5891 5913
5892#if !STANDALONE 5914#if !STANDALONE
5893 5915
5894void 5916ecb_cold void
5895scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 5917scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
5896{ 5918{
5897 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); 5919 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f));
5898} 5920}
5899 5921
5900void 5922ecb_cold void
5901scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 5923scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
5902{ 5924{
5903 int i; 5925 int i;
5904 5926
5905 for (i = 0; i < count; i++) 5927 for (i = 0; i < count; i++)
5906 scheme_register_foreign_func (SCHEME_A_ list + i); 5928 scheme_register_foreign_func (SCHEME_A_ list + i);
5907} 5929}
5908 5930
5909pointer 5931ecb_cold pointer
5910scheme_apply0 (SCHEME_P_ const char *procname) 5932scheme_apply0 (SCHEME_P_ const char *procname)
5911{ 5933{
5912 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); 5934 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL));
5913} 5935}
5914 5936
5915void 5937ecb_cold void
5916save_from_C_call (SCHEME_P) 5938save_from_C_call (SCHEME_P)
5917{ 5939{
5918 pointer saved_data = cons (car (S_SINK), 5940 pointer saved_data = cons (car (S_SINK),
5919 cons (SCHEME_V->envir, 5941 cons (SCHEME_V->envir,
5920 SCHEME_V->dump)); 5942 SCHEME_V->dump));
5924 /* Truncate the dump stack so TS will return here when done, not 5946 /* Truncate the dump stack so TS will return here when done, not
5925 directly resume pre-C-call operations. */ 5947 directly resume pre-C-call operations. */
5926 dump_stack_reset (SCHEME_A); 5948 dump_stack_reset (SCHEME_A);
5927} 5949}
5928 5950
5929void 5951ecb_cold void
5930restore_from_C_call (SCHEME_P) 5952restore_from_C_call (SCHEME_P)
5931{ 5953{
5932 set_car (S_SINK, caar (SCHEME_V->c_nest)); 5954 set_car (S_SINK, caar (SCHEME_V->c_nest));
5933 SCHEME_V->envir = cadar (SCHEME_V->c_nest); 5955 SCHEME_V->envir = cadar (SCHEME_V->c_nest);
5934 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); 5956 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest));
5935 /* Pop */ 5957 /* Pop */
5936 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); 5958 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest);
5937} 5959}
5938 5960
5939/* "func" and "args" are assumed to be already eval'ed. */ 5961/* "func" and "args" are assumed to be already eval'ed. */
5940pointer 5962ecb_cold pointer
5941scheme_call (SCHEME_P_ pointer func, pointer args) 5963scheme_call (SCHEME_P_ pointer func, pointer args)
5942{ 5964{
5943 int old_repl = SCHEME_V->interactive_repl; 5965 int old_repl = SCHEME_V->interactive_repl;
5944 5966
5945 SCHEME_V->interactive_repl = 0; 5967 SCHEME_V->interactive_repl = 0;
5952 SCHEME_V->interactive_repl = old_repl; 5974 SCHEME_V->interactive_repl = old_repl;
5953 restore_from_C_call (SCHEME_A); 5975 restore_from_C_call (SCHEME_A);
5954 return SCHEME_V->value; 5976 return SCHEME_V->value;
5955} 5977}
5956 5978
5957pointer 5979ecb_cold pointer
5958scheme_eval (SCHEME_P_ pointer obj) 5980scheme_eval (SCHEME_P_ pointer obj)
5959{ 5981{
5960 int old_repl = SCHEME_V->interactive_repl; 5982 int old_repl = SCHEME_V->interactive_repl;
5961 5983
5962 SCHEME_V->interactive_repl = 0; 5984 SCHEME_V->interactive_repl = 0;
5974 5996
5975/* ========== Main ========== */ 5997/* ========== Main ========== */
5976 5998
5977#if STANDALONE 5999#if STANDALONE
5978 6000
5979int 6001ecb_cold int
5980main (int argc, char **argv) 6002main (int argc, char **argv)
5981{ 6003{
5982# if USE_MULTIPLICITY 6004# if USE_MULTIPLICITY
5983 scheme ssc; 6005 scheme ssc;
5984 scheme *const SCHEME_V = &ssc; 6006 scheme *const SCHEME_V = &ssc;
5990 int isfile = 1; 6012 int isfile = 1;
5991 system ("ps v $PPID");//D 6013 system ("ps v $PPID");//D
5992 6014
5993 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6015 if (argc == 2 && strcmp (argv[1], "-?") == 0)
5994 { 6016 {
5995 xwrstr ("Usage: tinyscheme -?\n"); 6017 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
5996 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6018 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
5997 xwrstr ("followed by\n"); 6019 putstr (SCHEME_A_ "followed by\n");
5998 xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); 6020 putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n");
5999 xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); 6021 putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n");
6000 xwrstr ("assuming that the executable is named tinyscheme.\n"); 6022 putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n");
6001 xwrstr ("Use - as filename for stdin.\n"); 6023 putstr (SCHEME_A_ "Use - as filename for stdin.\n");
6002 return 1; 6024 return 1;
6003 } 6025 }
6004 6026
6005 if (!scheme_init (SCHEME_A)) 6027 if (!scheme_init (SCHEME_A))
6006 { 6028 {
6007 xwrstr ("Could not initialize!\n"); 6029 putstr (SCHEME_A_ "Could not initialize!\n");
6008 return 2; 6030 return 2;
6009 } 6031 }
6010 6032
6011# if USE_PORTS 6033# if USE_PORTS
6012 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); 6034 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO);
6057 fin = open (file_name, O_RDONLY); 6079 fin = open (file_name, O_RDONLY);
6058#endif 6080#endif
6059 6081
6060 if (isfile && fin < 0) 6082 if (isfile && fin < 0)
6061 { 6083 {
6062 xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); 6084 putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n");
6063 } 6085 }
6064 else 6086 else
6065 { 6087 {
6066 if (isfile) 6088 if (isfile)
6067 scheme_load_named_file (SCHEME_A_ fin, file_name); 6089 scheme_load_named_file (SCHEME_A_ fin, file_name);
6071#if USE_PORTS 6093#if USE_PORTS
6072 if (!isfile || fin != STDIN_FILENO) 6094 if (!isfile || fin != STDIN_FILENO)
6073 { 6095 {
6074 if (SCHEME_V->retcode != 0) 6096 if (SCHEME_V->retcode != 0)
6075 { 6097 {
6076 xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); 6098 putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n");
6077 } 6099 }
6078 6100
6079 if (isfile) 6101 if (isfile)
6080 close (fin); 6102 close (fin);
6081 } 6103 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines