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.35 by root, Sun Nov 29 00:02:21 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>
74 TOK_SHARP_CONST, 78 TOK_SHARP_CONST,
75 TOK_VEC 79 TOK_VEC
76}; 80};
77 81
78#define BACKQUOTE '`' 82#define BACKQUOTE '`'
79#define DELIMITERS "()\";\f\t\v\n\r " 83#define WHITESPACE " \t\r\n\v\f"
84#define DELIMITERS "()\";" WHITESPACE
80 85
81#define NIL (&SCHEME_V->xNIL) //TODO: make this 0? 86#define NIL POINTER (&SCHEME_V->xNIL)
82#define S_T (&SCHEME_V->xT) //TODO: magic ptr value? 87#define S_T POINTER (&SCHEME_V->xT)
83#define S_F (&SCHEME_V->xF) //TODO: magic ptr value? 88#define S_F POINTER (&SCHEME_V->xF)
84#define S_SINK (&SCHEME_V->xsink) 89#define S_SINK POINTER (&SCHEME_V->xsink)
85#define S_EOF (&SCHEME_V->xEOF_OBJ) 90#define S_EOF POINTER (&SCHEME_V->xEOF_OBJ)
86 91
87#if !USE_MULTIPLICITY 92#if !USE_MULTIPLICITY
88static scheme sc; 93static scheme sc;
89#endif 94#endif
90 95
91static void 96ecb_cold static void
92xbase (char *s, long n, int base) 97xbase (char *s, long n, int base)
93{ 98{
94 if (n < 0) 99 if (n < 0)
95 { 100 {
96 *s++ = '-'; 101 *s++ = '-';
98 } 103 }
99 104
100 char *p = s; 105 char *p = s;
101 106
102 do { 107 do {
103 *p++ = '0' + n % base; 108 *p++ = "0123456789abcdef"[n % base];
104 n /= base; 109 n /= base;
105 } while (n); 110 } while (n);
106 111
107 *p-- = 0; 112 *p-- = 0;
108 113
111 char x = *s; *s = *p; *p = x; 116 char x = *s; *s = *p; *p = x;
112 --p; ++s; 117 --p; ++s;
113 } 118 }
114} 119}
115 120
116static void 121ecb_cold static void
117xnum (char *s, long n) 122xnum (char *s, long n)
118{ 123{
119 xbase (s, n, 10); 124 xbase (s, n, 10);
120} 125}
121 126
122static void 127ecb_cold static void
123xwrstr (const char *s) 128putnum (SCHEME_P_ long n)
124{
125 write (1, s, strlen (s));
126}
127
128static void
129xwrnum (long n)
130{ 129{
131 char buf[64]; 130 char buf[64];
132 131
133 xnum (buf, n); 132 xnum (buf, n);
134 xwrstr (buf); 133 putstr (SCHEME_A_ buf);
135} 134}
136 135
137static char 136ecb_cold static char
138xtoupper (char c) 137xtoupper (char c)
139{ 138{
140 if (c >= 'a' && c <= 'z') 139 if (c >= 'a' && c <= 'z')
141 c -= 'a' - 'A'; 140 c -= 'a' - 'A';
142 141
143 return c; 142 return c;
144} 143}
145 144
146static char 145ecb_cold static char
147xtolower (char c) 146xtolower (char c)
148{ 147{
149 if (c >= 'A' && c <= 'Z') 148 if (c >= 'A' && c <= 'Z')
150 c += 'a' - 'A'; 149 c += 'a' - 'A';
151 150
152 return c; 151 return c;
153} 152}
154 153
155static int 154ecb_cold static int
156xisdigit (char c) 155xisdigit (char c)
157{ 156{
158 return c >= '0' && c <= '9'; 157 return c >= '0' && c <= '9';
159} 158}
160 159
161#define toupper(c) xtoupper (c) 160#define toupper(c) xtoupper (c)
162#define tolower(c) xtolower (c) 161#define tolower(c) xtolower (c)
163#define isdigit(c) xisdigit (c) 162#define isdigit(c) xisdigit (c)
164 163
165#if USE_IGNORECASE 164#if USE_IGNORECASE
166static const char * 165ecb_cold static const char *
167xstrlwr (char *s) 166xstrlwr (char *s)
168{ 167{
169 const char *p = s; 168 const char *p = s;
170 169
171 while (*s) 170 while (*s)
191 190
192#ifndef InitFile 191#ifndef InitFile
193# define InitFile "init.scm" 192# define InitFile "init.scm"
194#endif 193#endif
195 194
196#ifndef FIRST_CELLSEGS
197# define FIRST_CELLSEGS 3
198#endif
199
200enum scheme_types 195enum scheme_types
201{ 196{
202 T_INTEGER, 197 T_INTEGER,
198 T_CHARACTER,
203 T_REAL, 199 T_REAL,
204 T_STRING, 200 T_STRING,
205 T_SYMBOL, 201 T_SYMBOL,
206 T_PROC, 202 T_PROC,
207 T_PAIR, /* also used for free cells */ 203 T_PAIR, /* also used for free cells */
208 T_CLOSURE, 204 T_CLOSURE,
205 T_MACRO,
209 T_CONTINUATION, 206 T_CONTINUATION,
210 T_FOREIGN, 207 T_FOREIGN,
211 T_CHARACTER,
212 T_PORT, 208 T_PORT,
213 T_VECTOR, 209 T_VECTOR,
214 T_MACRO,
215 T_PROMISE, 210 T_PROMISE,
216 T_ENVIRONMENT, 211 T_ENVIRONMENT,
217 /* one more... */ 212 /* one more... */
218 T_NUM_SYSTEM_TYPES 213 T_NUM_SYSTEM_TYPES
219}; 214};
255static num num_op (enum num_op op, num a, num b); 250static num num_op (enum num_op op, num a, num b);
256static num num_intdiv (num a, num b); 251static num num_intdiv (num a, num b);
257static num num_rem (num a, num b); 252static num num_rem (num a, num b);
258static num num_mod (num a, num b); 253static num num_mod (num a, num b);
259 254
260#if USE_MATH
261static double round_per_R5RS (double x);
262#endif
263static int is_zero_rvalue (RVALUE x); 255static int is_zero_rvalue (RVALUE x);
264 256
265static num num_zero; 257static num num_zero;
266static num num_one; 258static num num_one;
267 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
268/* macros for cell operations */ 264/* macros for cell operations */
269#define typeflag(p) ((p)->flag + 0) 265#define typeflag(p) (CELL(p)->flag + 0)
270#define set_typeflag(p,v) ((p)->flag = (v)) 266#define set_typeflag(p,v) (CELL(p)->flag = (v))
271#define type(p) (typeflag (p) & T_MASKTYPE) 267#define type(p) (typeflag (p) & T_MASKTYPE)
272 268
273INTERFACE int 269INTERFACE int
274is_string (pointer p) 270is_string (pointer p)
275{ 271{
276 return type (p) == T_STRING; 272 return type (p) == T_STRING;
277} 273}
278 274
279#define strvalue(p) ((p)->object.string.svalue) 275#define strvalue(p) (CELL(p)->object.string.svalue)
280#define strlength(p) ((p)->object.string.length) 276#define strlength(p) (CELL(p)->object.string.length)
281 277
282INTERFACE int 278INTERFACE int
283is_vector (pointer p) 279is_vector (pointer p)
284{ 280{
285 return type (p) == T_VECTOR; 281 return type (p) == T_VECTOR;
286} 282}
287 283
288#define vecvalue(p) ((p)->object.vector.vvalue) 284#define vecvalue(p) (CELL(p)->object.vector.vvalue)
289#define veclength(p) ((p)->object.vector.length) 285#define veclength(p) (CELL(p)->object.vector.length)
290INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); 286INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
291INTERFACE pointer vector_get (pointer vec, uint32_t ielem); 287INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
292INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); 288INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
293 289
294INTERFACE int 290INTERFACE int
320string_value (pointer p) 316string_value (pointer p)
321{ 317{
322 return strvalue (p); 318 return strvalue (p);
323} 319}
324 320
325#define ivalue_unchecked(p) (p)->object.ivalue 321#define ivalue_unchecked(p) CELL(p)->object.ivalue
326#define set_ivalue(p,v) (p)->object.ivalue = (v) 322#define set_ivalue(p,v) CELL(p)->object.ivalue = (v)
327 323
328#if USE_REAL 324#if USE_REAL
329#define rvalue_unchecked(p) (p)->object.rvalue 325#define rvalue_unchecked(p) CELL(p)->object.rvalue
330#define set_rvalue(p,v) (p)->object.rvalue = (v) 326#define set_rvalue(p,v) CELL(p)->object.rvalue = (v)
331#else 327#else
332#define rvalue_unchecked(p) (p)->object.ivalue 328#define rvalue_unchecked(p) CELL(p)->object.ivalue
333#define set_rvalue(p,v) (p)->object.ivalue = (v) 329#define set_rvalue(p,v) CELL(p)->object.ivalue = (v)
334#endif 330#endif
335 331
336INTERFACE long 332INTERFACE long
337charvalue (pointer p) 333charvalue (pointer p)
338{ 334{
339 return ivalue_unchecked (p); 335 return ivalue_unchecked (p);
340} 336}
341 337
338#define port(p) CELL(p)->object.port
339#define set_port(p,v) port(p) = (v)
342INTERFACE int 340INTERFACE int
343is_port (pointer p) 341is_port (pointer p)
344{ 342{
345 return type (p) == T_PORT; 343 return type (p) == T_PORT;
346} 344}
347 345
348INTERFACE int 346INTERFACE int
349is_inport (pointer p) 347is_inport (pointer p)
350{ 348{
351 return is_port (p) && p->object.port->kind & port_input; 349 return is_port (p) && port (p)->kind & port_input;
352} 350}
353 351
354INTERFACE int 352INTERFACE int
355is_outport (pointer p) 353is_outport (pointer p)
356{ 354{
357 return is_port (p) && p->object.port->kind & port_output; 355 return is_port (p) && port (p)->kind & port_output;
358} 356}
359 357
360INTERFACE int 358INTERFACE int
361is_pair (pointer p) 359is_pair (pointer p)
362{ 360{
363 return type (p) == T_PAIR; 361 return type (p) == T_PAIR;
364} 362}
365 363
366#define car(p) ((p)->object.cons.car + 0) 364#define car(p) (POINTER (CELL(p)->object.cons.car))
367#define cdr(p) ((p)->object.cons.cdr + 0) 365#define cdr(p) (POINTER (CELL(p)->object.cons.cdr))
368 366
369static pointer caar (pointer p) { return car (car (p)); } 367static pointer caar (pointer p) { return car (car (p)); }
370static pointer cadr (pointer p) { return car (cdr (p)); } 368static pointer cadr (pointer p) { return car (cdr (p)); }
371static pointer cdar (pointer p) { return cdr (car (p)); } 369static pointer cdar (pointer p) { return cdr (car (p)); }
372static pointer cddr (pointer p) { return cdr (cdr (p)); } 370static pointer cddr (pointer p) { return cdr (cdr (p)); }
376static pointer cdaar (pointer p) { return cdr (car (car (p))); } 374static pointer cdaar (pointer p) { return cdr (car (car (p))); }
377 375
378INTERFACE void 376INTERFACE void
379set_car (pointer p, pointer q) 377set_car (pointer p, pointer q)
380{ 378{
381 p->object.cons.car = q; 379 CELL(p)->object.cons.car = CELL (q);
382} 380}
383 381
384INTERFACE void 382INTERFACE void
385set_cdr (pointer p, pointer q) 383set_cdr (pointer p, pointer q)
386{ 384{
387 p->object.cons.cdr = q; 385 CELL(p)->object.cons.cdr = CELL (q);
388} 386}
389 387
390INTERFACE pointer 388INTERFACE pointer
391pair_car (pointer p) 389pair_car (pointer p)
392{ 390{
406} 404}
407 405
408INTERFACE char * 406INTERFACE char *
409symname (pointer p) 407symname (pointer p)
410{ 408{
411 return strvalue (car (p)); 409 return strvalue (p);
412} 410}
413 411
414#if USE_PLIST 412#if USE_PLIST
413#error plists are broken because symbols are no longer pairs
414#define symprop(p) cdr(p)
415SCHEME_EXPORT int 415SCHEME_EXPORT int
416hasprop (pointer p) 416hasprop (pointer p)
417{ 417{
418 return typeflag (p) & T_SYMBOL; 418 return typeflag (p) & T_SYMBOL;
419} 419}
420
421# define symprop(p) cdr(p)
422#endif 420#endif
423 421
424INTERFACE int 422INTERFACE int
425is_syntax (pointer p) 423is_syntax (pointer p)
426{ 424{
440} 438}
441 439
442INTERFACE char * 440INTERFACE char *
443syntaxname (pointer p) 441syntaxname (pointer p)
444{ 442{
445 return strvalue (car (p)); 443 return strvalue (p);
446} 444}
447 445
448#define procnum(p) ivalue_unchecked (p) 446#define procnum(p) ivalue_unchecked (p)
449static const char *procname (pointer x); 447static const char *procname (pointer x);
450 448
522 proper list: length 520 proper list: length
523 circular list: -1 521 circular list: -1
524 not even a pair: -2 522 not even a pair: -2
525 dotted list: -2 minus length before dot 523 dotted list: -2 minus length before dot
526*/ 524*/
527INTERFACE int 525ecb_hot INTERFACE int
528list_length (SCHEME_P_ pointer a) 526list_length (SCHEME_P_ pointer a)
529{ 527{
530 int i = 0; 528 int i = 0;
531 pointer slow, fast; 529 pointer slow, fast;
532 530
638 "gs", 636 "gs",
639 "rs", 637 "rs",
640 "us" 638 "us"
641}; 639};
642 640
643static int 641ecb_cold static int
644is_ascii_name (const char *name, int *pc) 642is_ascii_name (const char *name, int *pc)
645{ 643{
646 int i; 644 int i;
647 645
648 for (i = 0; i < 32; i++) 646 for (i = 0; i < 32; i++)
667 665
668static int file_push (SCHEME_P_ const char *fname); 666static int file_push (SCHEME_P_ const char *fname);
669static void file_pop (SCHEME_P); 667static void file_pop (SCHEME_P);
670static int file_interactive (SCHEME_P); 668static int file_interactive (SCHEME_P);
671ecb_inline int is_one_of (const char *s, int c); 669ecb_inline int is_one_of (const char *s, int c);
672static int alloc_cellseg (SCHEME_P_ int n); 670static int alloc_cellseg (SCHEME_P);
673ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); 671ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
674static void finalize_cell (SCHEME_P_ pointer a); 672static void finalize_cell (SCHEME_P_ pointer a);
675static int count_consecutive_cells (pointer x, int needed); 673static int count_consecutive_cells (pointer x, int needed);
676static 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);
677static pointer mk_number (SCHEME_P_ const num n); 675static pointer mk_number (SCHEME_P_ const num n);
871 } 869 }
872 870
873 return ret; 871 return ret;
874} 872}
875 873
876#if USE_MATH
877
878/* Round to nearest. Round to even if midway */
879static double
880round_per_R5RS (double x)
881{
882 double fl = floor (x);
883 double ce = ceil (x);
884 double dfl = x - fl;
885 double dce = ce - x;
886
887 if (dfl > dce)
888 return ce;
889 else if (dfl < dce)
890 return fl;
891 else
892 {
893 if (fmod (fl, 2) == 0) /* I imagine this holds */
894 return fl;
895 else
896 return ce;
897 }
898}
899#endif
900
901static int 874static int
902is_zero_rvalue (RVALUE x) 875is_zero_rvalue (RVALUE x)
903{ 876{
904 return x == 0; 877 return x == 0;
905#if 0 878#if 0
910#endif 883#endif
911#endif 884#endif
912} 885}
913 886
914/* allocate new cell segment */ 887/* allocate new cell segment */
915static int 888ecb_cold static int
916alloc_cellseg (SCHEME_P_ int n) 889alloc_cellseg (SCHEME_P)
917{ 890{
918 pointer newp; 891 struct cell *newp;
919 pointer last; 892 struct cell *last;
920 pointer p; 893 struct cell *p;
921 char *cp; 894 char *cp;
922 long i; 895 long i;
923 int k; 896 int k;
924 897
925 static int segsize = CELL_SEGSIZE >> 1; 898 static int segsize = CELL_SEGSIZE >> 1;
926 segsize <<= 1; 899 segsize <<= 1;
927 900
928 for (k = 0; k < n; k++)
929 {
930 if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1)
931 return k;
932
933 cp = malloc (segsize * sizeof (struct cell)); 901 cp = malloc (segsize * sizeof (struct cell));
934 902
935 if (!cp && USE_ERROR_CHECKING) 903 if (!cp && USE_ERROR_CHECKING)
936 return k; 904 return k;
937 905
938 i = ++SCHEME_V->last_cell_seg; 906 i = ++SCHEME_V->last_cell_seg;
939 SCHEME_V->alloc_seg[i] = cp; 907 SCHEME_V->alloc_seg[i] = cp;
940 908
941 newp = (pointer)cp; 909 newp = (struct cell *)cp;
942 SCHEME_V->cell_seg[i] = newp; 910 SCHEME_V->cell_seg[i] = newp;
943 SCHEME_V->cell_segsize[i] = segsize; 911 SCHEME_V->cell_segsize[i] = segsize;
944 SCHEME_V->fcells += segsize; 912 SCHEME_V->fcells += segsize;
945 last = newp + segsize - 1; 913 last = newp + segsize - 1;
946 914
947 for (p = newp; p <= last; p++) 915 for (p = newp; p <= last; p++)
948 { 916 {
917 pointer cp = POINTER (p);
949 set_typeflag (p, T_PAIR); 918 set_typeflag (cp, T_PAIR);
950 set_car (p, NIL); 919 set_car (cp, NIL);
951 set_cdr (p, p + 1); 920 set_cdr (cp, POINTER (p + 1));
952 } 921 }
953 922
954 set_cdr (last, SCHEME_V->free_cell); 923 set_cdr (POINTER (last), SCHEME_V->free_cell);
955 SCHEME_V->free_cell = newp; 924 SCHEME_V->free_cell = POINTER (newp);
956 }
957 925
958 return n; 926 return 1;
959} 927}
960 928
961/* get new cell. parameter a, b is marked by gc. */ 929/* get new cell. parameter a, b is marked by gc. */
962ecb_inline pointer 930ecb_inline pointer
963get_cell_x (SCHEME_P_ pointer a, pointer b) 931get_cell_x (SCHEME_P_ pointer a, pointer b)
967 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 935 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
968 return S_SINK; 936 return S_SINK;
969 937
970 if (SCHEME_V->free_cell == NIL) 938 if (SCHEME_V->free_cell == NIL)
971 { 939 {
972 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;
973 941
974 gc (SCHEME_A_ a, b); 942 gc (SCHEME_A_ a, b);
975 943
976 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)
977 { 945 {
978 /* 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 */
979 if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) 947 if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL)
980 { 948 {
981#if USE_ERROR_CHECKING 949#if USE_ERROR_CHECKING
982 SCHEME_V->no_memory = 1; 950 SCHEME_V->no_memory = 1;
983 return S_SINK; 951 return S_SINK;
984#endif 952#endif
996 } 964 }
997} 965}
998 966
999/* To retain recent allocs before interpreter knows about them - 967/* To retain recent allocs before interpreter knows about them -
1000 Tehom */ 968 Tehom */
1001 969ecb_hot static void
1002static void
1003push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 970push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
1004{ 971{
1005 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 972 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
1006 973
1007 set_typeflag (holder, T_PAIR); 974 set_typeflag (holder, T_PAIR);
1009 set_car (holder, recent); 976 set_car (holder, recent);
1010 set_cdr (holder, car (S_SINK)); 977 set_cdr (holder, car (S_SINK));
1011 set_car (S_SINK, holder); 978 set_car (S_SINK, holder);
1012} 979}
1013 980
1014static pointer 981ecb_hot static pointer
1015get_cell (SCHEME_P_ pointer a, pointer b) 982get_cell (SCHEME_P_ pointer a, pointer b)
1016{ 983{
1017 pointer cell = get_cell_x (SCHEME_A_ a, b); 984 pointer cell = get_cell_x (SCHEME_A_ a, b);
1018 985
1019 /* 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
1028} 995}
1029 996
1030static pointer 997static pointer
1031get_vector_object (SCHEME_P_ uint32_t len, pointer init) 998get_vector_object (SCHEME_P_ uint32_t len, pointer init)
1032{ 999{
1033 pointer v = get_cell_x (SCHEME_A_ 0, 0); 1000 pointer v = get_cell_x (SCHEME_A_ NIL, NIL);
1034 pointer *e = malloc (len * sizeof (pointer)); 1001 pointer *e = malloc (len * sizeof (pointer));
1035 1002
1036 if (!e && USE_ERROR_CHECKING) 1003 if (!e && USE_ERROR_CHECKING)
1037 return S_SINK; 1004 return S_SINK;
1038 1005
1039 /* Record it as a vector so that gc understands it. */ 1006 /* Record it as a vector so that gc understands it. */
1040 set_typeflag (v, T_VECTOR | T_ATOM); 1007 set_typeflag (v, T_VECTOR | T_ATOM);
1041 1008
1042 v->object.vector.vvalue = e; 1009 CELL(v)->object.vector.vvalue = e;
1043 v->object.vector.length = len; 1010 CELL(v)->object.vector.length = len;
1044 fill_vector (v, 0, init); 1011 fill_vector (v, 0, init);
1045 push_recent_alloc (SCHEME_A_ v, NIL); 1012 push_recent_alloc (SCHEME_A_ v, NIL);
1046 1013
1047 return v; 1014 return v;
1048} 1015}
1057static void 1024static void
1058check_cell_alloced (pointer p, int expect_alloced) 1025check_cell_alloced (pointer p, int expect_alloced)
1059{ 1026{
1060 /* 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. */
1061 if (typeflag (p) & !expect_alloced) 1028 if (typeflag (p) & !expect_alloced)
1062 xwrstr ("Cell is already allocated!\n"); 1029 putstr (SCHEME_A_ "Cell is already allocated!\n");
1063 1030
1064 if (!(typeflag (p)) & expect_alloced) 1031 if (!(typeflag (p)) & expect_alloced)
1065 xwrstr ("Cell is not allocated!\n"); 1032 putstr (SCHEME_A_ "Cell is not allocated!\n");
1066} 1033}
1067 1034
1068static void 1035static void
1069check_range_alloced (pointer p, int n, int expect_alloced) 1036check_range_alloced (pointer p, int n, int expect_alloced)
1070{ 1037{
1076#endif 1043#endif
1077 1044
1078/* Medium level cell allocation */ 1045/* Medium level cell allocation */
1079 1046
1080/* get new cons cell */ 1047/* get new cons cell */
1081pointer 1048ecb_hot pointer
1082xcons (SCHEME_P_ pointer a, pointer b, int immutable) 1049xcons (SCHEME_P_ pointer a, pointer b, int immutable)
1083{ 1050{
1084 pointer x = get_cell (SCHEME_A_ a, b); 1051 pointer x = get_cell (SCHEME_A_ a, b);
1085 1052
1086 set_typeflag (x, T_PAIR); 1053 set_typeflag (x, T_PAIR);
1092 set_cdr (x, b); 1059 set_cdr (x, b);
1093 1060
1094 return x; 1061 return x;
1095} 1062}
1096 1063
1097/* ========== oblist implementation ========== */
1098
1099static pointer 1064ecb_cold static pointer
1100generate_symbol (SCHEME_P_ const char *name) 1065generate_symbol (SCHEME_P_ const char *name)
1101{ 1066{
1102 pointer x = mk_string (SCHEME_A_ name); 1067 pointer x = mk_string (SCHEME_A_ name);
1103 setimmutable (x); 1068 setimmutable (x);
1104 x = immutable_cons (x, NIL);
1105 set_typeflag (x, T_SYMBOL); 1069 set_typeflag (x, T_SYMBOL | T_ATOM);
1106 return x; 1070 return x;
1107} 1071}
1072
1073/* ========== oblist implementation ========== */
1108 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)
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);
1354 1332
1355 for (i = start; i < veclength (vec); i++) 1333 for (i = start; i < veclength (vec); i++)
1356 vecvalue (vec)[i] = obj; 1334 vecvalue (vec)[i] = obj;
1357} 1335}
1358 1336
1337INTERFACE void
1338vector_resize (pointer vec, uint32_t newsize, pointer fill)
1339{
1340 uint32_t oldsize = veclength (vec);
1341 vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer));
1342 veclength (vec) = newsize;
1343 fill_vector (vec, oldsize, fill);
1344}
1345
1359INTERFACE pointer 1346INTERFACE pointer
1360vector_get (pointer vec, uint32_t ielem) 1347vector_get (pointer vec, uint32_t ielem)
1361{ 1348{
1362 return vecvalue(vec)[ielem]; 1349 return vecvalue(vec)[ielem];
1363} 1350}
1379 x = oblist_add_by_name (SCHEME_A_ name); 1366 x = oblist_add_by_name (SCHEME_A_ name);
1380 1367
1381 return x; 1368 return x;
1382} 1369}
1383 1370
1384INTERFACE pointer 1371ecb_cold INTERFACE pointer
1385gensym (SCHEME_P) 1372gensym (SCHEME_P)
1386{ 1373{
1387 pointer x; 1374 pointer x;
1388 char name[40] = "gensym-"; 1375 char name[40] = "gensym-";
1389 xnum (name + 7, SCHEME_V->gensym_cnt); 1376 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1390 1377
1391 return generate_symbol (SCHEME_A_ name); 1378 return generate_symbol (SCHEME_A_ name);
1392} 1379}
1393 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
1394/* make symbol or number atom from string */ 1387/* make symbol or number atom from string */
1395static pointer 1388ecb_cold static pointer
1396mk_atom (SCHEME_P_ char *q) 1389mk_atom (SCHEME_P_ char *q)
1397{ 1390{
1398 char c, *p; 1391 char c, *p;
1399 int has_dec_point = 0; 1392 int has_dec_point = 0;
1400 int has_fp_exp = 0; 1393 int has_fp_exp = 0;
1471 1464
1472 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1465 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1473} 1466}
1474 1467
1475/* make constant */ 1468/* make constant */
1476static pointer 1469ecb_cold static pointer
1477mk_sharp_const (SCHEME_P_ char *name) 1470mk_sharp_const (SCHEME_P_ char *name)
1478{ 1471{
1479 if (!strcmp (name, "t")) 1472 if (!strcmp (name, "t"))
1480 return S_T; 1473 return S_T;
1481 else if (!strcmp (name, "f")) 1474 else if (!strcmp (name, "f"))
1482 return S_F; 1475 return S_F;
1483 else if (*name == '\\') /* #\w (character) */ 1476 else if (*name == '\\') /* #\w (character) */
1484 { 1477 {
1485 int c; 1478 int c;
1486 1479
1480 // TODO: optimise
1487 if (stricmp (name + 1, "space") == 0) 1481 if (stricmp (name + 1, "space") == 0)
1488 c = ' '; 1482 c = ' ';
1489 else if (stricmp (name + 1, "newline") == 0) 1483 else if (stricmp (name + 1, "newline") == 0)
1490 c = '\n'; 1484 c = '\n';
1491 else if (stricmp (name + 1, "return") == 0) 1485 else if (stricmp (name + 1, "return") == 0)
1492 c = '\r'; 1486 c = '\r';
1493 else if (stricmp (name + 1, "tab") == 0) 1487 else if (stricmp (name + 1, "tab") == 0)
1494 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;
1495 else if (name[1] == 'x' && name[2] != 0) 1499 else if (name[1] == 'x' && name[2] != 0)
1496 { 1500 {
1497 long c1 = strtol (name + 2, 0, 16); 1501 long c1 = strtol (name + 2, 0, 16);
1498 1502
1499 if (0 <= c1 && c1 <= UCHAR_MAX) 1503 if (0 <= c1 && c1 <= UCHAR_MAX)
1524 return NIL; 1528 return NIL;
1525 } 1529 }
1526} 1530}
1527 1531
1528/* ========== 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}
1529 1552
1530/*-- 1553/*--
1531 * 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,
1532 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1555 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1533 * for marking. 1556 * for marking.
1534 * 1557 *
1535 * The exception is vectors - vectors are currently marked recursively, 1558 * The exception is vectors - vectors are currently marked recursively,
1536 * 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
1537 * word of context in the vector 1560 * word of context in the vector
1538 */ 1561 */
1539static void 1562ecb_hot static void
1540mark (pointer a) 1563mark (pointer a)
1541{ 1564{
1542 pointer t, q, p; 1565 pointer t, q, p;
1543 1566
1544 t = 0; 1567 t = 0;
1601 p = q; 1624 p = q;
1602 goto E6; 1625 goto E6;
1603 } 1626 }
1604} 1627}
1605 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
1606/* garbage collection. parameter a, b is marked. */ 1671/* garbage collection. parameter a, b is marked. */
1607static void 1672ecb_cold static void
1608gc (SCHEME_P_ pointer a, pointer b) 1673gc (SCHEME_P_ pointer a, pointer b)
1609{ 1674{
1610 pointer p;
1611 int i; 1675 int i;
1612 1676
1613 if (SCHEME_V->gc_verbose) 1677 if (SCHEME_V->gc_verbose)
1614 putstr (SCHEME_A_ "gc..."); 1678 putstr (SCHEME_A_ "gc...");
1615 1679
1631 /* Mark recent objects the interpreter doesn't know about yet. */ 1695 /* Mark recent objects the interpreter doesn't know about yet. */
1632 mark (car (S_SINK)); 1696 mark (car (S_SINK));
1633 /* Mark any older stuff above nested C calls */ 1697 /* Mark any older stuff above nested C calls */
1634 mark (SCHEME_V->c_nest); 1698 mark (SCHEME_V->c_nest);
1635 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
1636 /* mark variables a, b */ 1707 /* mark variables a, b */
1637 mark (a); 1708 mark (a);
1638 mark (b); 1709 mark (b);
1639 1710
1640 /* garbage collect */ 1711 /* garbage collect */
1641 clrmark (NIL); 1712 clrmark (NIL);
1642 SCHEME_V->fcells = 0; 1713 SCHEME_V->fcells = 0;
1643 SCHEME_V->free_cell = NIL; 1714 SCHEME_V->free_cell = NIL;
1644 1715
1645 /* free-list is kept sorted by address so as to maintain consecutive
1646 ranges, if possible, for use with vectors. Here we scan the cells
1647 (which are also kept sorted by address) downwards to build the
1648 free-list in sorted order.
1649 */
1650 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1651 {
1652 p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1653
1654 while (--p >= SCHEME_V->cell_seg[i])
1655 {
1656 if (is_mark (p))
1657 clrmark (p);
1658 else
1659 {
1660 /* reclaim cell */
1661 if (typeflag (p) != T_PAIR)
1662 {
1663 finalize_cell (SCHEME_A_ p);
1664 set_typeflag (p, T_PAIR);
1665 set_car (p, NIL);
1666 }
1667
1668 ++SCHEME_V->fcells;
1669 set_cdr (p, SCHEME_V->free_cell);
1670 SCHEME_V->free_cell = p;
1671 }
1672 }
1673 }
1674
1675 if (SCHEME_V->gc_verbose) 1716 if (SCHEME_V->gc_verbose)
1676 { 1717 putstr (SCHEME_A_ "freeing...");
1677 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1678 }
1679}
1680 1718
1681static void 1719 gc_free (SCHEME_A);
1682finalize_cell (SCHEME_P_ pointer a)
1683{
1684 /* TODO, fast bitmap check? */
1685 if (is_string (a))
1686 free (strvalue (a));
1687 else if (is_vector (a))
1688 free (vecvalue (a));
1689#if USE_PORTS
1690 else if (is_port (a))
1691 {
1692 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit)
1693 port_close (SCHEME_A_ a, port_input | port_output);
1694
1695 free (a->object.port);
1696 }
1697#endif
1698} 1720}
1699 1721
1700/* ========== Routines for Reading ========== */ 1722/* ========== Routines for Reading ========== */
1701 1723
1702static int 1724ecb_cold static int
1703file_push (SCHEME_P_ const char *fname) 1725file_push (SCHEME_P_ const char *fname)
1704{ 1726{
1705#if USE_PORTS 1727#if USE_PORTS
1706 int fin; 1728 int fin;
1707 1729
1716 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; 1738 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1;
1717 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;
1718 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;
1719 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;
1720 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; 1742 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0;
1721 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);
1722 1744
1723#if SHOW_ERROR_LINE 1745#if SHOW_ERROR_LINE
1724 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;
1725 1747
1726 if (fname) 1748 if (fname)
1733#else 1755#else
1734 return 1; 1756 return 1;
1735#endif 1757#endif
1736} 1758}
1737 1759
1738static void 1760ecb_cold static void
1739file_pop (SCHEME_P) 1761file_pop (SCHEME_P)
1740{ 1762{
1741 if (SCHEME_V->file_i != 0) 1763 if (SCHEME_V->file_i != 0)
1742 { 1764 {
1743 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1765 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1744#if USE_PORTS 1766#if USE_PORTS
1745 port_close (SCHEME_A_ SCHEME_V->loadport, port_input); 1767 port_close (SCHEME_A_ SCHEME_V->loadport, port_input);
1746#endif 1768#endif
1747 SCHEME_V->file_i--; 1769 SCHEME_V->file_i--;
1748 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);
1749 } 1771 }
1750} 1772}
1751 1773
1752static int 1774ecb_cold static int
1753file_interactive (SCHEME_P) 1775file_interactive (SCHEME_P)
1754{ 1776{
1755#if USE_PORTS 1777#if USE_PORTS
1756 return SCHEME_V->file_i == 0 1778 return SCHEME_V->file_i == 0
1757 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1779 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1758 && (SCHEME_V->inport->object.port->kind & port_file); 1780 && (port (SCHEME_V->inport)->kind & port_file);
1759#else 1781#else
1760 return 0; 1782 return 0;
1761#endif 1783#endif
1762} 1784}
1763 1785
1764#if USE_PORTS 1786#if USE_PORTS
1765static port * 1787ecb_cold static port *
1766port_rep_from_filename (SCHEME_P_ const char *fn, int prop) 1788port_rep_from_filename (SCHEME_P_ const char *fn, int prop)
1767{ 1789{
1768 int fd; 1790 int fd;
1769 int flags; 1791 int flags;
1770 char *rw; 1792 char *rw;
1793# endif 1815# endif
1794 1816
1795 return pt; 1817 return pt;
1796} 1818}
1797 1819
1798static pointer 1820ecb_cold static pointer
1799port_from_filename (SCHEME_P_ const char *fn, int prop) 1821port_from_filename (SCHEME_P_ const char *fn, int prop)
1800{ 1822{
1801 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); 1823 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop);
1802 1824
1803 if (!pt && USE_ERROR_CHECKING) 1825 if (!pt && USE_ERROR_CHECKING)
1804 return NIL; 1826 return NIL;
1805 1827
1806 return mk_port (SCHEME_A_ pt); 1828 return mk_port (SCHEME_A_ pt);
1807} 1829}
1808 1830
1809static port * 1831ecb_cold static port *
1810port_rep_from_file (SCHEME_P_ int f, int prop) 1832port_rep_from_file (SCHEME_P_ int f, int prop)
1811{ 1833{
1812 port *pt = malloc (sizeof *pt); 1834 port *pt = malloc (sizeof *pt);
1813 1835
1814 if (!pt && USE_ERROR_CHECKING) 1836 if (!pt && USE_ERROR_CHECKING)
1819 pt->rep.stdio.file = f; 1841 pt->rep.stdio.file = f;
1820 pt->rep.stdio.closeit = 0; 1842 pt->rep.stdio.closeit = 0;
1821 return pt; 1843 return pt;
1822} 1844}
1823 1845
1824static pointer 1846ecb_cold static pointer
1825port_from_file (SCHEME_P_ int f, int prop) 1847port_from_file (SCHEME_P_ int f, int prop)
1826{ 1848{
1827 port *pt = port_rep_from_file (SCHEME_A_ f, prop); 1849 port *pt = port_rep_from_file (SCHEME_A_ f, prop);
1828 1850
1829 if (!pt && USE_ERROR_CHECKING) 1851 if (!pt && USE_ERROR_CHECKING)
1830 return NIL; 1852 return NIL;
1831 1853
1832 return mk_port (SCHEME_A_ pt); 1854 return mk_port (SCHEME_A_ pt);
1833} 1855}
1834 1856
1835static port * 1857ecb_cold static port *
1836port_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)
1837{ 1859{
1838 port *pt = malloc (sizeof (port)); 1860 port *pt = malloc (sizeof (port));
1839 1861
1840 if (!pt && USE_ERROR_CHECKING) 1862 if (!pt && USE_ERROR_CHECKING)
1846 pt->rep.string.curr = start; 1868 pt->rep.string.curr = start;
1847 pt->rep.string.past_the_end = past_the_end; 1869 pt->rep.string.past_the_end = past_the_end;
1848 return pt; 1870 return pt;
1849} 1871}
1850 1872
1851static pointer 1873ecb_cold static pointer
1852port_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)
1853{ 1875{
1854 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);
1855 1877
1856 if (!pt && USE_ERROR_CHECKING) 1878 if (!pt && USE_ERROR_CHECKING)
1859 return mk_port (SCHEME_A_ pt); 1881 return mk_port (SCHEME_A_ pt);
1860} 1882}
1861 1883
1862# define BLOCK_SIZE 256 1884# define BLOCK_SIZE 256
1863 1885
1864static port * 1886ecb_cold static port *
1865port_rep_from_scratch (SCHEME_P) 1887port_rep_from_scratch (SCHEME_P)
1866{ 1888{
1867 char *start; 1889 char *start;
1868 port *pt = malloc (sizeof (port)); 1890 port *pt = malloc (sizeof (port));
1869 1891
1883 pt->rep.string.curr = start; 1905 pt->rep.string.curr = start;
1884 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; 1906 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1;
1885 return pt; 1907 return pt;
1886} 1908}
1887 1909
1888static pointer 1910ecb_cold static pointer
1889port_from_scratch (SCHEME_P) 1911port_from_scratch (SCHEME_P)
1890{ 1912{
1891 port *pt = port_rep_from_scratch (SCHEME_A); 1913 port *pt = port_rep_from_scratch (SCHEME_A);
1892 1914
1893 if (!pt && USE_ERROR_CHECKING) 1915 if (!pt && USE_ERROR_CHECKING)
1894 return NIL; 1916 return NIL;
1895 1917
1896 return mk_port (SCHEME_A_ pt); 1918 return mk_port (SCHEME_A_ pt);
1897} 1919}
1898 1920
1899static void 1921ecb_cold static void
1900port_close (SCHEME_P_ pointer p, int flag) 1922port_close (SCHEME_P_ pointer p, int flag)
1901{ 1923{
1902 port *pt = p->object.port; 1924 port *pt = port (p);
1903 1925
1904 pt->kind &= ~flag; 1926 pt->kind &= ~flag;
1905 1927
1906 if ((pt->kind & (port_input | port_output)) == 0) 1928 if ((pt->kind & (port_input | port_output)) == 0)
1907 { 1929 {
1928/* get new character from input file */ 1950/* get new character from input file */
1929static int 1951static int
1930inchar (SCHEME_P) 1952inchar (SCHEME_P)
1931{ 1953{
1932 int c; 1954 int c;
1933 port *pt; 1955 port *pt = port (SCHEME_V->inport);
1934
1935 pt = SCHEME_V->inport->object.port;
1936 1956
1937 if (pt->kind & port_saw_EOF) 1957 if (pt->kind & port_saw_EOF)
1938 return EOF; 1958 return EOF;
1939 1959
1940 c = basic_inchar (pt); 1960 c = basic_inchar (pt);
2007 port *pt; 2027 port *pt;
2008 2028
2009 if (c == EOF) 2029 if (c == EOF)
2010 return; 2030 return;
2011 2031
2012 pt = SCHEME_V->inport->object.port; 2032 pt = port (SCHEME_V->inport);
2013 pt->unget = c; 2033 pt->unget = c;
2014#else 2034#else
2015 if (c == EOF) 2035 if (c == EOF)
2016 return; 2036 return;
2017 2037
2018 ungot = c; 2038 ungot = c;
2019#endif 2039#endif
2020} 2040}
2021 2041
2022#if USE_PORTS 2042#if USE_PORTS
2023static int 2043ecb_cold static int
2024realloc_port_string (SCHEME_P_ port *p) 2044realloc_port_string (SCHEME_P_ port *p)
2025{ 2045{
2026 char *start = p->rep.string.start; 2046 char *start = p->rep.string.start;
2027 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;
2028 char *str = malloc (new_size); 2048 char *str = malloc (new_size);
2041 else 2061 else
2042 return 0; 2062 return 0;
2043} 2063}
2044#endif 2064#endif
2045 2065
2046INTERFACE void 2066ecb_cold INTERFACE void
2047putstr (SCHEME_P_ const char *s) 2067putstr (SCHEME_P_ const char *s)
2048{ 2068{
2049#if USE_PORTS 2069#if USE_PORTS
2050 port *pt = SCHEME_V->outport->object.port; 2070 port *pt = port (SCHEME_V->outport);
2051 2071
2052 if (pt->kind & port_file) 2072 if (pt->kind & port_file)
2053 write (pt->rep.stdio.file, s, strlen (s)); 2073 write (pt->rep.stdio.file, s, strlen (s));
2054 else 2074 else
2055 for (; *s; s++) 2075 for (; *s; s++)
2057 *pt->rep.string.curr++ = *s; 2077 *pt->rep.string.curr++ = *s;
2058 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))
2059 *pt->rep.string.curr++ = *s; 2079 *pt->rep.string.curr++ = *s;
2060 2080
2061#else 2081#else
2062 xwrstr (s); 2082 write (pt->rep.stdio.file, s, strlen (s));
2063#endif 2083#endif
2064} 2084}
2065 2085
2066static void 2086ecb_cold static void
2067putchars (SCHEME_P_ const char *s, int len) 2087putchars (SCHEME_P_ const char *s, int len)
2068{ 2088{
2069#if USE_PORTS 2089#if USE_PORTS
2070 port *pt = SCHEME_V->outport->object.port; 2090 port *pt = port (SCHEME_V->outport);
2071 2091
2072 if (pt->kind & port_file) 2092 if (pt->kind & port_file)
2073 write (pt->rep.stdio.file, s, len); 2093 write (pt->rep.stdio.file, s, len);
2074 else 2094 else
2075 { 2095 {
2085#else 2105#else
2086 write (1, s, len); 2106 write (1, s, len);
2087#endif 2107#endif
2088} 2108}
2089 2109
2090INTERFACE void 2110ecb_cold INTERFACE void
2091putcharacter (SCHEME_P_ int c) 2111putcharacter (SCHEME_P_ int c)
2092{ 2112{
2093#if USE_PORTS 2113#if USE_PORTS
2094 port *pt = SCHEME_V->outport->object.port; 2114 port *pt = port (SCHEME_V->outport);
2095 2115
2096 if (pt->kind & port_file) 2116 if (pt->kind & port_file)
2097 { 2117 {
2098 char cc = c; 2118 char cc = c;
2099 write (pt->rep.stdio.file, &cc, 1); 2119 write (pt->rep.stdio.file, &cc, 1);
2111 write (1, &c, 1); 2131 write (1, &c, 1);
2112#endif 2132#endif
2113} 2133}
2114 2134
2115/* read characters up to delimiter, but cater to character constants */ 2135/* read characters up to delimiter, but cater to character constants */
2116static char * 2136ecb_cold static char *
2117readstr_upto (SCHEME_P_ int skip, const char *delim) 2137readstr_upto (SCHEME_P_ int skip, const char *delim)
2118{ 2138{
2119 char *p = SCHEME_V->strbuff + skip; 2139 char *p = SCHEME_V->strbuff + skip;
2120 2140
2121 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))));
2130 2150
2131 return SCHEME_V->strbuff; 2151 return SCHEME_V->strbuff;
2132} 2152}
2133 2153
2134/* read string expression "xxx...xxx" */ 2154/* read string expression "xxx...xxx" */
2135static pointer 2155ecb_cold static pointer
2136readstrexp (SCHEME_P_ char delim) 2156readstrexp (SCHEME_P_ char delim)
2137{ 2157{
2138 char *p = SCHEME_V->strbuff; 2158 char *p = SCHEME_V->strbuff;
2139 int c; 2159 int c;
2140 int c1 = 0; 2160 int c1 = 0;
2173 case '7': 2193 case '7':
2174 state = st_oct1; 2194 state = st_oct1;
2175 c1 = c - '0'; 2195 c1 = c - '0';
2176 break; 2196 break;
2177 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
2178 case 'x': 2206 case 'x':
2179 case 'X': 2207 case 'X':
2180 state = st_x1; 2208 state = st_x1;
2181 c1 = 0; 2209 c1 = 0;
2182 break;
2183
2184 case 'n':
2185 *p++ = '\n';
2186 state = st_ok;
2187 break;
2188
2189 case 't':
2190 *p++ = '\t';
2191 state = st_ok;
2192 break;
2193
2194 case 'r':
2195 *p++ = '\r';
2196 state = st_ok;
2197 break; 2210 break;
2198 2211
2199 default: 2212 default:
2200 *p++ = c; 2213 *p++ = c;
2201 state = st_ok; 2214 state = st_ok;
2253 } 2266 }
2254 } 2267 }
2255} 2268}
2256 2269
2257/* check c is in chars */ 2270/* check c is in chars */
2258ecb_inline int 2271ecb_cold int
2259is_one_of (const char *s, int c) 2272is_one_of (const char *s, int c)
2260{ 2273{
2261 if (c == EOF)
2262 return 1;
2263
2264 return !!strchr (s, c); 2274 return c == EOF || !!strchr (s, c);
2265} 2275}
2266 2276
2267/* skip white characters */ 2277/* skip white characters */
2268ecb_inline int 2278ecb_cold int
2269skipspace (SCHEME_P) 2279skipspace (SCHEME_P)
2270{ 2280{
2271 int c, curr_line = 0; 2281 int c, curr_line = 0;
2272 2282
2273 do 2283 do
2274 { 2284 {
2275 c = inchar (SCHEME_A); 2285 c = inchar (SCHEME_A);
2286
2276#if SHOW_ERROR_LINE 2287#if SHOW_ERROR_LINE
2277 if (c == '\n') 2288 if (ecb_expect_false (c == '\n'))
2278 curr_line++; 2289 curr_line++;
2279#endif 2290#endif
2291
2292 if (ecb_expect_false (c == EOF))
2293 return c;
2280 } 2294 }
2281 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2295 while (is_one_of (WHITESPACE, c));
2282 2296
2283 /* record it */ 2297 /* record it */
2284#if SHOW_ERROR_LINE 2298#if SHOW_ERROR_LINE
2285 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) 2299 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file)
2286 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; 2300 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line;
2287#endif 2301#endif
2288 2302
2289 if (c != EOF)
2290 {
2291 backchar (SCHEME_A_ c); 2303 backchar (SCHEME_A_ c);
2292 return 1; 2304 return 1;
2293 }
2294 else
2295 return EOF;
2296} 2305}
2297 2306
2298/* get token */ 2307/* get token */
2299static int 2308ecb_cold static int
2300token (SCHEME_P) 2309token (SCHEME_P)
2301{ 2310{
2302 int c = skipspace (SCHEME_A); 2311 int c = skipspace (SCHEME_A);
2303 2312
2304 if (c == EOF) 2313 if (c == EOF)
2316 return TOK_RPAREN; 2325 return TOK_RPAREN;
2317 2326
2318 case '.': 2327 case '.':
2319 c = inchar (SCHEME_A); 2328 c = inchar (SCHEME_A);
2320 2329
2321 if (is_one_of (" \n\t", c)) 2330 if (is_one_of (WHITESPACE, c))
2322 return TOK_DOT; 2331 return TOK_DOT;
2323 else 2332 else
2324 { 2333 {
2325 backchar (SCHEME_A_ c); 2334 backchar (SCHEME_A_ c);
2326 return TOK_DOTATOM; 2335 return TOK_DOTATOM;
2402} 2411}
2403 2412
2404/* ========== Routines for Printing ========== */ 2413/* ========== Routines for Printing ========== */
2405#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) 2414#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL)
2406 2415
2407static void 2416ecb_cold static void
2408printslashstring (SCHEME_P_ char *p, int len) 2417printslashstring (SCHEME_P_ char *p, int len)
2409{ 2418{
2410 int i; 2419 int i;
2411 unsigned char *s = (unsigned char *) p; 2420 unsigned char *s = (unsigned char *) p;
2412 2421
2467 } 2476 }
2468 2477
2469 putcharacter (SCHEME_A_ '"'); 2478 putcharacter (SCHEME_A_ '"');
2470} 2479}
2471 2480
2472
2473/* print atoms */ 2481/* print atoms */
2474static void 2482ecb_cold static void
2475printatom (SCHEME_P_ pointer l, int f) 2483printatom (SCHEME_P_ pointer l, int f)
2476{ 2484{
2477 char *p; 2485 char *p;
2478 int len; 2486 int len;
2479 2487
2480 atom2str (SCHEME_A_ l, f, &p, &len); 2488 atom2str (SCHEME_A_ l, f, &p, &len);
2481 putchars (SCHEME_A_ p, len); 2489 putchars (SCHEME_A_ p, len);
2482} 2490}
2483 2491
2484
2485/* Uses internal buffer unless string pointer is already available */ 2492/* Uses internal buffer unless string pointer is already available */
2486static void 2493ecb_cold static void
2487atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2494atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2488{ 2495{
2489 char *p; 2496 char *p;
2490 2497
2491 if (l == NIL) 2498 if (l == NIL)
2649#endif 2656#endif
2650 } 2657 }
2651 else if (is_continuation (l)) 2658 else if (is_continuation (l))
2652 p = "#<CONTINUATION>"; 2659 p = "#<CONTINUATION>";
2653 else 2660 else
2661 {
2662#if USE_PRINTF
2663 p = SCHEME_V->strbuff;
2664 snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l));
2665#else
2654 p = "#<ERROR>"; 2666 p = "#<ERROR>";
2667#endif
2668 }
2655 2669
2656 *pp = p; 2670 *pp = p;
2657 *plen = strlen (p); 2671 *plen = strlen (p);
2658} 2672}
2659 2673
2691 return car (d); 2705 return car (d);
2692 2706
2693 p = cons (car (d), cdr (d)); 2707 p = cons (car (d), cdr (d));
2694 q = p; 2708 q = p;
2695 2709
2696 while (cdr (cdr (p)) != NIL) 2710 while (cddr (p) != NIL)
2697 { 2711 {
2698 d = cons (car (p), cdr (p)); 2712 d = cons (car (p), cdr (p));
2699 2713
2700 if (cdr (cdr (p)) != NIL) 2714 if (cddr (p) != NIL)
2701 p = cdr (d); 2715 p = cdr (d);
2702 } 2716 }
2703 2717
2704 set_cdr (p, car (cdr (p))); 2718 set_cdr (p, cadr (p));
2705 return q; 2719 return q;
2706} 2720}
2707 2721
2708/* reverse list -- produce new list */ 2722/* reverse list -- produce new list */
2709static pointer 2723ecb_hot static pointer
2710reverse (SCHEME_P_ pointer a) 2724reverse (SCHEME_P_ pointer a)
2711{ 2725{
2712 /* a must be checked by gc */ 2726 /* a must be checked by gc */
2713 pointer p = NIL; 2727 pointer p = NIL;
2714 2728
2717 2731
2718 return p; 2732 return p;
2719} 2733}
2720 2734
2721/* reverse list --- in-place */ 2735/* reverse list --- in-place */
2722static pointer 2736ecb_hot static pointer
2723reverse_in_place (SCHEME_P_ pointer term, pointer list) 2737reverse_in_place (SCHEME_P_ pointer term, pointer list)
2724{ 2738{
2725 pointer result = term; 2739 pointer result = term;
2726 pointer p = list; 2740 pointer p = list;
2727 2741
2735 2749
2736 return result; 2750 return result;
2737} 2751}
2738 2752
2739/* append list -- produce new list (in reverse order) */ 2753/* append list -- produce new list (in reverse order) */
2740static pointer 2754ecb_hot static pointer
2741revappend (SCHEME_P_ pointer a, pointer b) 2755revappend (SCHEME_P_ pointer a, pointer b)
2742{ 2756{
2743 pointer result = a; 2757 pointer result = a;
2744 pointer p = b; 2758 pointer p = b;
2745 2759
2754 2768
2755 return S_F; /* signal an error */ 2769 return S_F; /* signal an error */
2756} 2770}
2757 2771
2758/* equivalence of atoms */ 2772/* equivalence of atoms */
2759int 2773ecb_hot int
2760eqv (pointer a, pointer b) 2774eqv (pointer a, pointer b)
2761{ 2775{
2762 if (is_string (a)) 2776 if (is_string (a))
2763 { 2777 {
2764 if (is_string (b)) 2778 if (is_string (b))
2858 } 2872 }
2859 else 2873 else
2860 set_car (env, immutable_cons (slot, car (env))); 2874 set_car (env, immutable_cons (slot, car (env)));
2861} 2875}
2862 2876
2863static pointer 2877ecb_hot static pointer
2864find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2878find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2865{ 2879{
2866 pointer x, y; 2880 pointer x, y;
2867 2881
2868 for (x = env; x != NIL; x = cdr (x)) 2882 for (x = env; x != NIL; x = cdr (x))
2889 return NIL; 2903 return NIL;
2890} 2904}
2891 2905
2892#else /* USE_ALIST_ENV */ 2906#else /* USE_ALIST_ENV */
2893 2907
2894ecb_inline void 2908static void
2895new_frame_in_env (SCHEME_P_ pointer old_env) 2909new_frame_in_env (SCHEME_P_ pointer old_env)
2896{ 2910{
2897 SCHEME_V->envir = immutable_cons (NIL, old_env); 2911 SCHEME_V->envir = immutable_cons (NIL, old_env);
2898 setenvironment (SCHEME_V->envir); 2912 setenvironment (SCHEME_V->envir);
2899} 2913}
2900 2914
2901ecb_inline void 2915static void
2902new_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)
2903{ 2917{
2904 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2918 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2905} 2919}
2906 2920
2907static pointer 2921ecb_hot static pointer
2908find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2922find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2909{ 2923{
2910 pointer x, y; 2924 pointer x, y;
2911 2925
2912 for (x = env; x != NIL; x = cdr (x)) 2926 for (x = env; x != NIL; x = cdr (x))
2926 return NIL; 2940 return NIL;
2927} 2941}
2928 2942
2929#endif /* USE_ALIST_ENV else */ 2943#endif /* USE_ALIST_ENV else */
2930 2944
2931ecb_inline void 2945static void
2932new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2946new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2933{ 2947{
2948 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2934 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);
2935} 2950}
2936 2951
2937ecb_inline void 2952static void
2938set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2953set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2939{ 2954{
2940 set_cdr (slot, value); 2955 set_cdr (slot, value);
2941} 2956}
2942 2957
2943ecb_inline pointer 2958static pointer
2944slot_value_in_env (pointer slot) 2959slot_value_in_env (pointer slot)
2945{ 2960{
2946 return cdr (slot); 2961 return cdr (slot);
2947} 2962}
2948 2963
2949/* ========== Evaluation Cycle ========== */ 2964/* ========== Evaluation Cycle ========== */
2950 2965
2951static int 2966ecb_cold static int
2952xError_1 (SCHEME_P_ const char *s, pointer a) 2967xError_1 (SCHEME_P_ const char *s, pointer a)
2953{ 2968{
2954#if USE_ERROR_HOOK 2969#if USE_ERROR_HOOK
2955 pointer x; 2970 pointer x;
2956 pointer hdl = SCHEME_V->ERROR_HOOK; 2971 pointer hdl = SCHEME_V->ERROR_HOOK;
3032 pointer code; 3047 pointer code;
3033}; 3048};
3034 3049
3035# define STACK_GROWTH 3 3050# define STACK_GROWTH 3
3036 3051
3037static void 3052ecb_hot static void
3038s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3053s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3039{ 3054{
3040 int nframes = (uintptr_t)SCHEME_V->dump; 3055 int nframes = (uintptr_t)SCHEME_V->dump;
3041 struct dump_stack_frame *next_frame; 3056 struct dump_stack_frame *next_frame;
3042 3057
3043 /* enough room for the next frame? */ 3058 /* enough room for the next frame? */
3044 if (nframes >= SCHEME_V->dump_size) 3059 if (ecb_expect_false (nframes >= SCHEME_V->dump_size))
3045 { 3060 {
3046 SCHEME_V->dump_size += STACK_GROWTH; 3061 SCHEME_V->dump_size += STACK_GROWTH;
3047 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);
3048 } 3063 }
3049 3064
3055 next_frame->code = code; 3070 next_frame->code = code;
3056 3071
3057 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3072 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3058} 3073}
3059 3074
3060static int 3075static ecb_hot int
3061xs_return (SCHEME_P_ pointer a) 3076xs_return (SCHEME_P_ pointer a)
3062{ 3077{
3063 int nframes = (uintptr_t)SCHEME_V->dump; 3078 int nframes = (uintptr_t)SCHEME_V->dump;
3064 struct dump_stack_frame *frame; 3079 struct dump_stack_frame *frame;
3065 3080
3076 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3091 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3077 3092
3078 return 0; 3093 return 0;
3079} 3094}
3080 3095
3081ecb_inline void 3096ecb_cold void
3082dump_stack_reset (SCHEME_P) 3097dump_stack_reset (SCHEME_P)
3083{ 3098{
3084 /* 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 */
3085 SCHEME_V->dump = (pointer)+0; 3100 SCHEME_V->dump = (pointer)+0;
3086} 3101}
3087 3102
3088ecb_inline void 3103ecb_cold void
3089dump_stack_initialize (SCHEME_P) 3104dump_stack_initialize (SCHEME_P)
3090{ 3105{
3091 SCHEME_V->dump_size = 0; 3106 SCHEME_V->dump_size = 0;
3092 SCHEME_V->dump_base = 0; 3107 SCHEME_V->dump_base = 0;
3093 dump_stack_reset (SCHEME_A); 3108 dump_stack_reset (SCHEME_A);
3094} 3109}
3095 3110
3096static void 3111ecb_cold static void
3097dump_stack_free (SCHEME_P) 3112dump_stack_free (SCHEME_P)
3098{ 3113{
3099 free (SCHEME_V->dump_base); 3114 free (SCHEME_V->dump_base);
3100 SCHEME_V->dump_base = 0; 3115 SCHEME_V->dump_base = 0;
3101 SCHEME_V->dump = (pointer)0; 3116 SCHEME_V->dump = (pointer)0;
3102 SCHEME_V->dump_size = 0; 3117 SCHEME_V->dump_size = 0;
3103} 3118}
3104 3119
3105static void 3120ecb_cold static void
3106dump_stack_mark (SCHEME_P) 3121dump_stack_mark (SCHEME_P)
3107{ 3122{
3108 int nframes = (uintptr_t)SCHEME_V->dump; 3123 int nframes = (uintptr_t)SCHEME_V->dump;
3109 int i; 3124 int i;
3110 3125
3116 mark (frame->envir); 3131 mark (frame->envir);
3117 mark (frame->code); 3132 mark (frame->code);
3118 } 3133 }
3119} 3134}
3120 3135
3121static pointer 3136ecb_cold static pointer
3122ss_get_cont (SCHEME_P) 3137ss_get_cont (SCHEME_P)
3123{ 3138{
3124 int nframes = (uintptr_t)SCHEME_V->dump; 3139 int nframes = (uintptr_t)SCHEME_V->dump;
3125 int i; 3140 int i;
3126 3141
3138 } 3153 }
3139 3154
3140 return cont; 3155 return cont;
3141} 3156}
3142 3157
3143static void 3158ecb_cold static void
3144ss_set_cont (SCHEME_P_ pointer cont) 3159ss_set_cont (SCHEME_P_ pointer cont)
3145{ 3160{
3146 int i = 0; 3161 int i = 0;
3147 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3162 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3148 3163
3160 SCHEME_V->dump = (pointer)(uintptr_t)i; 3175 SCHEME_V->dump = (pointer)(uintptr_t)i;
3161} 3176}
3162 3177
3163#else 3178#else
3164 3179
3165ecb_inline void 3180ecb_cold void
3166dump_stack_reset (SCHEME_P) 3181dump_stack_reset (SCHEME_P)
3167{ 3182{
3168 SCHEME_V->dump = NIL; 3183 SCHEME_V->dump = NIL;
3169} 3184}
3170 3185
3171ecb_inline void 3186ecb_cold void
3172dump_stack_initialize (SCHEME_P) 3187dump_stack_initialize (SCHEME_P)
3173{ 3188{
3174 dump_stack_reset (SCHEME_A); 3189 dump_stack_reset (SCHEME_A);
3175} 3190}
3176 3191
3177static void 3192ecb_cold static void
3178dump_stack_free (SCHEME_P) 3193dump_stack_free (SCHEME_P)
3179{ 3194{
3180 SCHEME_V->dump = NIL; 3195 SCHEME_V->dump = NIL;
3181} 3196}
3182 3197
3183static int 3198ecb_hot static int
3184xs_return (SCHEME_P_ pointer a) 3199xs_return (SCHEME_P_ pointer a)
3185{ 3200{
3186 pointer dump = SCHEME_V->dump; 3201 pointer dump = SCHEME_V->dump;
3187 3202
3188 SCHEME_V->value = a; 3203 SCHEME_V->value = a;
3198 SCHEME_V->dump = dump; 3213 SCHEME_V->dump = dump;
3199 3214
3200 return 0; 3215 return 0;
3201} 3216}
3202 3217
3203static void 3218ecb_hot static void
3204s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3219s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3205{ 3220{
3206 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), 3221 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op),
3207 cons (args, 3222 cons (args,
3208 cons (SCHEME_V->envir, 3223 cons (SCHEME_V->envir,
3209 cons (code, 3224 cons (code,
3210 SCHEME_V->dump)))); 3225 SCHEME_V->dump))));
3211} 3226}
3212 3227
3213static void 3228ecb_cold static void
3214dump_stack_mark (SCHEME_P) 3229dump_stack_mark (SCHEME_P)
3215{ 3230{
3216 mark (SCHEME_V->dump); 3231 mark (SCHEME_V->dump);
3217} 3232}
3218 3233
3219static pointer 3234ecb_cold static pointer
3220ss_get_cont (SCHEME_P) 3235ss_get_cont (SCHEME_P)
3221{ 3236{
3222 return SCHEME_V->dump; 3237 return SCHEME_V->dump;
3223} 3238}
3224 3239
3225static void 3240ecb_cold static void
3226ss_set_cont (SCHEME_P_ pointer cont) 3241ss_set_cont (SCHEME_P_ pointer cont)
3227{ 3242{
3228 SCHEME_V->dump = cont; 3243 SCHEME_V->dump = cont;
3229} 3244}
3230 3245
3231#endif 3246#endif
3232 3247
3233#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3248#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3234 3249
3250#if EXPERIMENT
3235static int 3251static int
3252debug (SCHEME_P_ int indent, pointer x)
3253{
3254 int c;
3255
3256 if (is_syntax (x))
3257 {
3258 printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x));
3259 return 8 + 8;
3260 }
3261
3262 if (x == NIL)
3263 {
3264 printf ("%*sNIL\n", indent, "");
3265 return 3;
3266 }
3267
3268 switch (type (x))
3269 {
3270 case T_INTEGER:
3271 printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x);
3272 return 32+8;
3273
3274 case T_SYMBOL:
3275 printf ("%*sS<%s>\n", indent, "", symname (x));
3276 return 24+8;
3277
3278 case T_CLOSURE:
3279 printf ("%*sS<%s>\n", indent, "", "closure");
3280 debug (SCHEME_A_ indent + 3, cdr(x));
3281 return 32 + debug (SCHEME_A_ indent + 3, car (x));
3282
3283 case T_PAIR:
3284 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3285 c = debug (SCHEME_A_ indent + 3, car (x));
3286 c += debug (SCHEME_A_ indent + 3, cdr (x));
3287 return c + 1;
3288
3289 case T_PORT:
3290 printf ("%*sS<%s>\n", indent, "", "port");
3291 return 24+8;
3292
3293 case T_VECTOR:
3294 printf ("%*sS<%s>\n", indent, "", "vector");
3295 return 24+8;
3296
3297 case T_ENVIRONMENT:
3298 printf ("%*sS<%s>\n", indent, "", "environment");
3299 return 0 + debug (SCHEME_A_ indent + 3, car (x));
3300
3301 default:
3302 printf ("unhandled type %d\n", type (x));
3303 break;
3304 }
3305}
3306#endif
3307
3308/* syntax, eval, core, ... */
3309ecb_hot static int
3236opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3310opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3237{ 3311{
3238 pointer args = SCHEME_V->args; 3312 pointer args = SCHEME_V->args;
3239 pointer x, y; 3313 pointer x, y;
3240 3314
3241 switch (op) 3315 switch (op)
3242 { 3316 {
3317#if EXPERIMENT //D
3318 case OP_DEBUG:
3319 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8);
3320 printf ("\n");
3321 s_return (S_T);
3322#endif
3243 case OP_LOAD: /* load */ 3323 case OP_LOAD: /* load */
3244 if (file_interactive (SCHEME_A)) 3324 if (file_interactive (SCHEME_A))
3245 { 3325 {
3246 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); 3326 putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n");
3247 //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)));
3248 } 3328 }
3249 3329
3250 if (!file_push (SCHEME_A_ strvalue (car (args)))) 3330 if (!file_push (SCHEME_A_ strvalue (car (args))))
3251 Error_1 ("unable to open", car (args)); 3331 Error_1 ("unable to open", car (args));
3252 else 3332 else
3256 } 3336 }
3257 3337
3258 case OP_T0LVL: /* top level */ 3338 case OP_T0LVL: /* top level */
3259 3339
3260 /* If we reached the end of file, this loop is done. */ 3340 /* If we reached the end of file, this loop is done. */
3261 if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) 3341 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3262 { 3342 {
3263 if (SCHEME_V->file_i == 0) 3343 if (SCHEME_V->file_i == 0)
3264 { 3344 {
3265 SCHEME_V->args = NIL; 3345 SCHEME_V->args = NIL;
3266 s_goto (OP_QUIT); 3346 s_goto (OP_QUIT);
3344#endif 3424#endif
3345 if (is_symbol (SCHEME_V->code)) /* symbol */ 3425 if (is_symbol (SCHEME_V->code)) /* symbol */
3346 { 3426 {
3347 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);
3348 3428
3349 if (x != NIL) 3429 if (x == NIL)
3350 s_return (slot_value_in_env (x));
3351 else
3352 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));
3353 } 3433 }
3354 else if (is_pair (SCHEME_V->code)) 3434 else if (is_pair (SCHEME_V->code))
3355 { 3435 {
3356 x = car (SCHEME_V->code); 3436 x = car (SCHEME_V->code);
3357 3437
3370 } 3450 }
3371 else 3451 else
3372 s_return (SCHEME_V->code); 3452 s_return (SCHEME_V->code);
3373 3453
3374 case OP_E0ARGS: /* eval arguments */ 3454 case OP_E0ARGS: /* eval arguments */
3375 if (is_macro (SCHEME_V->value)) /* macro expansion */ 3455 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3376 { 3456 {
3377 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3457 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3378 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3458 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3379 SCHEME_V->code = SCHEME_V->value; 3459 SCHEME_V->code = SCHEME_V->value;
3380 s_goto (OP_APPLY); 3460 s_goto (OP_APPLY);
3434 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3514 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3435 else if (is_foreign (SCHEME_V->code)) 3515 else if (is_foreign (SCHEME_V->code))
3436 { 3516 {
3437 /* Keep nested calls from GC'ing the arglist */ 3517 /* Keep nested calls from GC'ing the arglist */
3438 push_recent_alloc (SCHEME_A_ args, NIL); 3518 push_recent_alloc (SCHEME_A_ args, NIL);
3439 x = SCHEME_V->code->object.ff (SCHEME_A_ args); 3519 x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args);
3440 3520
3441 s_return (x); 3521 s_return (x);
3442 } 3522 }
3443 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 */
3444 { 3524 {
3481 3561
3482 case OP_DOMACRO: /* do macro */ 3562 case OP_DOMACRO: /* do macro */
3483 SCHEME_V->code = SCHEME_V->value; 3563 SCHEME_V->code = SCHEME_V->value;
3484 s_goto (OP_EVAL); 3564 s_goto (OP_EVAL);
3485 3565
3486#if 1
3487
3488 case OP_LAMBDA: /* lambda */ 3566 case OP_LAMBDA: /* lambda */
3489 /* 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
3490 set SCHEME_V->value fall thru */ 3568 set SCHEME_V->value fall thru */
3491 { 3569 {
3492 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);
3498 SCHEME_V->code = slot_value_in_env (f); 3576 SCHEME_V->code = slot_value_in_env (f);
3499 s_goto (OP_APPLY); 3577 s_goto (OP_APPLY);
3500 } 3578 }
3501 3579
3502 SCHEME_V->value = SCHEME_V->code; 3580 SCHEME_V->value = SCHEME_V->code;
3503 /* Fallthru */
3504 } 3581 }
3582 /* Fallthru */
3505 3583
3506 case OP_LAMBDA1: 3584 case OP_LAMBDA1:
3507 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));
3508
3509#else
3510
3511 case OP_LAMBDA: /* lambda */
3512 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir));
3513
3514#endif
3515 3586
3516 case OP_MKCLOSURE: /* make-closure */ 3587 case OP_MKCLOSURE: /* make-closure */
3517 x = car (args); 3588 x = car (args);
3518 3589
3519 if (car (x) == SCHEME_V->LAMBDA) 3590 if (car (x) == SCHEME_V->LAMBDA)
3929 } 4000 }
3930 4001
3931 if (USE_ERROR_CHECKING) abort (); 4002 if (USE_ERROR_CHECKING) abort ();
3932} 4003}
3933 4004
3934static int 4005/* math, cxr */
4006ecb_hot static int
3935opexe_1 (SCHEME_P_ enum scheme_opcodes op) 4007opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3936{ 4008{
3937 pointer args = SCHEME_V->args; 4009 pointer args = SCHEME_V->args;
3938 pointer x = car (args); 4010 pointer x = car (args);
3939 num v; 4011 num v;
3940 4012
3941 switch (op) 4013 switch (op)
3942 { 4014 {
3943#if USE_MATH 4015#if USE_MATH
3944 case OP_INEX2EX: /* inexact->exact */ 4016 case OP_INEX2EX: /* inexact->exact */
3945 {
3946 if (is_integer (x)) 4017 if (!is_integer (x))
3947 s_return (x); 4018 {
3948
3949 RVALUE r = rvalue_unchecked (x); 4019 RVALUE r = rvalue_unchecked (x);
3950 4020
3951 if (r == (RVALUE)(IVALUE)r) 4021 if (r == (RVALUE)(IVALUE)r)
3952 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); 4022 x = mk_integer (SCHEME_A_ rvalue_unchecked (x));
3953 else 4023 else
3954 Error_1 ("inexact->exact: not integral:", x); 4024 Error_1 ("inexact->exact: not integral:", x);
3955 } 4025 }
3956 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))));
3957 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))));
3958 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))))));
3959 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))));
3960 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))));
3961 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))));
3962 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))));
3963 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))));
3964 4043
3965 case OP_ATAN: 4044 case OP_ATAN:
4045 s_return (mk_real (SCHEME_A_
3966 if (cdr (args) == NIL) 4046 cdr (args) == NIL
3967 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 4047 ? atan (rvalue (x))
3968 else 4048 : atan2 (rvalue (x), rvalue (cadr (args)))));
3969 {
3970 pointer y = cadr (args);
3971 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
3972 }
3973
3974 case OP_SQRT:
3975 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
3976 4049
3977 case OP_EXPT: 4050 case OP_EXPT:
3978 { 4051 {
3979 RVALUE result; 4052 RVALUE result;
3980 int real_result = 1; 4053 int real_result = 1;
4003 if (real_result) 4076 if (real_result)
4004 s_return (mk_real (SCHEME_A_ result)); 4077 s_return (mk_real (SCHEME_A_ result));
4005 else 4078 else
4006 s_return (mk_integer (SCHEME_A_ result)); 4079 s_return (mk_integer (SCHEME_A_ result));
4007 } 4080 }
4008
4009 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4010 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4011
4012 case OP_TRUNCATE:
4013 {
4014 RVALUE n = rvalue (x);
4015 s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n)));
4016 }
4017
4018 case OP_ROUND:
4019 if (is_integer (x))
4020 s_return (x);
4021
4022 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4023#endif 4081#endif
4024 4082
4025 case OP_ADD: /* + */ 4083 case OP_ADD: /* + */
4026 v = num_zero; 4084 v = num_zero;
4027 4085
4115 else 4173 else
4116 Error_0 ("modulo: division by zero"); 4174 Error_0 ("modulo: division by zero");
4117 4175
4118 s_return (mk_number (SCHEME_A_ v)); 4176 s_return (mk_number (SCHEME_A_ v));
4119 4177
4120 case OP_CAR: /* car */ 4178 /* the compiler will optimize this mess... */
4121 s_return (caar (args)); 4179 case OP_CAR: op_car: s_return (car (x));
4122 4180 case OP_CDR: op_cdr: s_return (cdr (x));
4123 case OP_CDR: /* cdr */ 4181 case OP_CAAR: op_caar: x = car (x); goto op_car;
4124 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;
4125 4209
4126 case OP_CONS: /* cons */ 4210 case OP_CONS: /* cons */
4127 set_cdr (args, cadr (args)); 4211 set_cdr (args, cadr (args));
4128 s_return (args); 4212 s_return (args);
4129 4213
4303 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4387 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4304 4388
4305 s_return (newstr); 4389 s_return (newstr);
4306 } 4390 }
4307 4391
4308 case OP_SUBSTR: /* substring */ 4392 case OP_STRING_COPY: /* substring/string-copy */
4309 { 4393 {
4310 char *str = strvalue (x); 4394 char *str = strvalue (x);
4311 int index0 = ivalue_unchecked (cadr (args)); 4395 int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args));
4312 int index1; 4396 int index1;
4313 int len; 4397 int len;
4314 4398
4315 if (index0 > strlength (x)) 4399 if (index0 > strlength (x))
4316 Error_1 ("substring: start out of bounds:", cadr (args)); 4400 Error_1 ("string->copy: start out of bounds:", cadr (args));
4317 4401
4318 if (cddr (args) != NIL) 4402 if (cddr (args) != NIL)
4319 { 4403 {
4320 index1 = ivalue_unchecked (caddr (args)); 4404 index1 = ivalue_unchecked (caddr (args));
4321 4405
4322 if (index1 > strlength (x) || index1 < index0) 4406 if (index1 > strlength (x) || index1 < index0)
4323 Error_1 ("substring: end out of bounds:", caddr (args)); 4407 Error_1 ("string->copy: end out of bounds:", caddr (args));
4324 } 4408 }
4325 else 4409 else
4326 index1 = strlength (x); 4410 index1 = strlength (x);
4327 4411
4328 len = index1 - index0; 4412 len = index1 - index0;
4329 x = mk_empty_string (SCHEME_A_ len, ' '); 4413 x = mk_counted_string (SCHEME_A_ str + index0, len);
4330 memcpy (strvalue (x), str + index0, len);
4331 strvalue (x)[len] = 0;
4332 4414
4333 s_return (x); 4415 s_return (x);
4334 } 4416 }
4335 4417
4336 case OP_VECTOR: /* vector */ 4418 case OP_VECTOR: /* vector */
4378 } 4460 }
4379 4461
4380 case OP_VECLEN: /* vector-length */ 4462 case OP_VECLEN: /* vector-length */
4381 s_return (mk_integer (SCHEME_A_ veclength (x))); 4463 s_return (mk_integer (SCHEME_A_ veclength (x)));
4382 4464
4465 case OP_VECRESIZE:
4466 vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args));
4467 s_return (x);
4468
4383 case OP_VECREF: /* vector-ref */ 4469 case OP_VECREF: /* vector-ref */
4384 { 4470 {
4385 int index = ivalue_unchecked (cadr (args)); 4471 int index = ivalue_unchecked (cadr (args));
4386 4472
4387 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4473 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4406 } 4492 }
4407 4493
4408 if (USE_ERROR_CHECKING) abort (); 4494 if (USE_ERROR_CHECKING) abort ();
4409} 4495}
4410 4496
4411static int 4497/* relational ops */
4498ecb_hot static int
4412opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4499opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4413{ 4500{
4414 pointer x = SCHEME_V->args; 4501 pointer x = SCHEME_V->args;
4415 4502
4416 for (;;) 4503 for (;;)
4437 } 4524 }
4438 4525
4439 s_return (S_T); 4526 s_return (S_T);
4440} 4527}
4441 4528
4442static int 4529/* predicates */
4530ecb_hot static int
4443opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4531opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4444{ 4532{
4445 pointer args = SCHEME_V->args; 4533 pointer args = SCHEME_V->args;
4446 pointer a = car (args); 4534 pointer a = car (args);
4447 pointer d = cdr (args); 4535 pointer d = cdr (args);
4448 int r; 4536 int r;
4449 4537
4450 switch (op) 4538 switch (op)
4451 { 4539 {
4452 case OP_NOT: /* not */ r = is_false (a) ; break; 4540 case OP_NOT: /* not */ r = is_false (a) ; break;
4453 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;
4454 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; 4542 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break;
4455 case OP_NULLP: /* null? */ r = a == NIL ; break; 4543 case OP_NULLP: /* null? */ r = a == NIL ; break;
4456 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;
4457 case OP_NUMBERP: /* number? */ r = is_number (a) ; break; 4546 case OP_NUMBERP: /* number? */ r = is_number (a) ; break;
4458 case OP_STRINGP: /* string? */ r = is_string (a) ; break; 4547 case OP_STRINGP: /* string? */ r = is_string (a) ; break;
4459 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4548 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4460 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 */
4461 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4550 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4462 4551
4463#if USE_CHAR_CLASSIFIERS 4552#if USE_CHAR_CLASSIFIERS
4464 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; 4553 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4465 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; 4554 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4466 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; 4555 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4493 } 4582 }
4494 4583
4495 s_retbool (r); 4584 s_retbool (r);
4496} 4585}
4497 4586
4498static int 4587/* promises, list ops, ports */
4588ecb_hot static int
4499opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4589opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4500{ 4590{
4501 pointer args = SCHEME_V->args; 4591 pointer args = SCHEME_V->args;
4502 pointer a = car (args); 4592 pointer a = car (args);
4503 pointer x, y; 4593 pointer x, y;
4516 } 4606 }
4517 else 4607 else
4518 s_return (SCHEME_V->code); 4608 s_return (SCHEME_V->code);
4519 4609
4520 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4610 case OP_SAVE_FORCED: /* Save forced value replacing promise */
4521 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); 4611 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
4522 s_return (SCHEME_V->value); 4612 s_return (SCHEME_V->value);
4523 4613
4524#if USE_PORTS 4614#if USE_PORTS
4615
4616 case OP_EOF_OBJECT: /* eof-object */
4617 s_return (S_EOF);
4525 4618
4526 case OP_WRITE: /* write */ 4619 case OP_WRITE: /* write */
4527 case OP_DISPLAY: /* display */ 4620 case OP_DISPLAY: /* display */
4528 case OP_WRITE_CHAR: /* write-char */ 4621 case OP_WRITE_CHAR: /* write-char */
4529 if (is_pair (cdr (SCHEME_V->args))) 4622 if (is_pair (cdr (SCHEME_V->args)))
4671 SCHEME_V->gc_verbose = (a != S_F); 4764 SCHEME_V->gc_verbose = (a != S_F);
4672 s_retbool (was); 4765 s_retbool (was);
4673 } 4766 }
4674 4767
4675 case OP_NEWSEGMENT: /* new-segment */ 4768 case OP_NEWSEGMENT: /* new-segment */
4769#if 0
4676 if (!is_pair (args) || !is_number (a)) 4770 if (!is_pair (args) || !is_number (a))
4677 Error_0 ("new-segment: argument must be a number"); 4771 Error_0 ("new-segment: argument must be a number");
4678 4772#endif
4679 alloc_cellseg (SCHEME_A_ ivalue (a)); 4773 s_retbool (alloc_cellseg (SCHEME_A));
4680
4681 s_return (S_T);
4682 4774
4683 case OP_OBLIST: /* oblist */ 4775 case OP_OBLIST: /* oblist */
4684 s_return (oblist_all_symbols (SCHEME_A)); 4776 s_return (oblist_all_symbols (SCHEME_A));
4685 4777
4686#if USE_PORTS 4778#if USE_PORTS
4756 s_return (p == NIL ? S_F : p); 4848 s_return (p == NIL ? S_F : p);
4757 } 4849 }
4758 4850
4759 case OP_GET_OUTSTRING: /* get-output-string */ 4851 case OP_GET_OUTSTRING: /* get-output-string */
4760 { 4852 {
4761 port *p; 4853 port *p = port (a);
4762 4854
4763 if ((p = a->object.port)->kind & port_string) 4855 if (p->kind & port_string)
4764 { 4856 {
4765 off_t size; 4857 off_t size;
4766 char *str; 4858 char *str;
4767 4859
4768 size = p->rep.string.curr - p->rep.string.start + 1; 4860 size = p->rep.string.curr - p->rep.string.start + 1;
4803 } 4895 }
4804 4896
4805 if (USE_ERROR_CHECKING) abort (); 4897 if (USE_ERROR_CHECKING) abort ();
4806} 4898}
4807 4899
4808static int 4900/* reading */
4901ecb_cold static int
4809opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4902opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4810{ 4903{
4811 pointer args = SCHEME_V->args; 4904 pointer args = SCHEME_V->args;
4812 pointer x; 4905 pointer x;
4813 4906
4873 int res; 4966 int res;
4874 4967
4875 if (is_pair (args)) 4968 if (is_pair (args))
4876 p = car (args); 4969 p = car (args);
4877 4970
4878 res = p->object.port->kind & port_string; 4971 res = port (p)->kind & port_string;
4879 4972
4880 s_retbool (res); 4973 s_retbool (res);
4881 } 4974 }
4882 4975
4883 case OP_SET_INPORT: /* set-input-port */ 4976 case OP_SET_INPORT: /* set-input-port */
4947 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); 5040 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS)));
4948 5041
4949 case TOK_DOTATOM: 5042 case TOK_DOTATOM:
4950 SCHEME_V->strbuff[0] = '.'; 5043 SCHEME_V->strbuff[0] = '.';
4951 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); 5044 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5045
5046 case TOK_STRATOM:
5047 x = readstrexp (SCHEME_A_ '|');
5048 //TODO: haven't checked whether the garbage collector could interfere
5049 s_return (mk_atom (SCHEME_A_ strvalue (x)));
4952 5050
4953 case TOK_DQUOTE: 5051 case TOK_DQUOTE:
4954 x = readstrexp (SCHEME_A_ '"'); 5052 x = readstrexp (SCHEME_A_ '"');
4955 5053
4956 if (x == S_F) 5054 if (x == S_F)
5150 } 5248 }
5151 5249
5152 if (USE_ERROR_CHECKING) abort (); 5250 if (USE_ERROR_CHECKING) abort ();
5153} 5251}
5154 5252
5155static int 5253/* list ops */
5254ecb_hot static int
5156opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5255opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5157{ 5256{
5158 pointer args = SCHEME_V->args; 5257 pointer args = SCHEME_V->args;
5159 pointer a = car (args); 5258 pointer a = car (args);
5160 pointer x, y; 5259 pointer x, y;
5203 5302
5204 case OP_CLOSUREP: /* closure? */ 5303 case OP_CLOSUREP: /* closure? */
5205 /* 5304 /*
5206 * Note, macro object is also a closure. 5305 * Note, macro object is also a closure.
5207 * Therefore, (closure? <#MACRO>) ==> #t 5306 * Therefore, (closure? <#MACRO>) ==> #t
5307 * (schmorp) well, obviously not, fix? TODO
5208 */ 5308 */
5209 s_retbool (is_closure (a)); 5309 s_retbool (is_closure (a));
5210 5310
5211 case OP_MACROP: /* macro? */ 5311 case OP_MACROP: /* macro? */
5212 s_retbool (is_macro (a)); 5312 s_retbool (is_macro (a));
5217 5317
5218/* 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 */
5219typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); 5319typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5220 5320
5221typedef int (*test_predicate)(pointer); 5321typedef int (*test_predicate)(pointer);
5222static int 5322
5323ecb_hot static int
5223tst_any (pointer p) 5324tst_any (pointer p)
5224{ 5325{
5225 return 1; 5326 return 1;
5226} 5327}
5227 5328
5228static int 5329ecb_hot static int
5229tst_inonneg (pointer p) 5330tst_inonneg (pointer p)
5230{ 5331{
5231 return is_integer (p) && ivalue_unchecked (p) >= 0; 5332 return is_integer (p) && ivalue_unchecked (p) >= 0;
5232} 5333}
5233 5334
5234static int 5335ecb_hot static int
5235tst_is_list (SCHEME_P_ pointer p) 5336tst_is_list (SCHEME_P_ pointer p)
5236{ 5337{
5237 return p == NIL || is_pair (p); 5338 return p == NIL || is_pair (p);
5238} 5339}
5239 5340
5282#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" 5383#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5283#include "opdefines.h" 5384#include "opdefines.h"
5284#undef OP_DEF 5385#undef OP_DEF
5285; 5386;
5286 5387
5287static const char * 5388ecb_cold static const char *
5288opname (int idx) 5389opname (int idx)
5289{ 5390{
5290 const char *name = opnames; 5391 const char *name = opnames;
5291 5392
5292 /* 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? */
5294 name += strlen (name) + 1; 5395 name += strlen (name) + 1;
5295 5396
5296 return *name ? name : "ILLEGAL"; 5397 return *name ? name : "ILLEGAL";
5297} 5398}
5298 5399
5299static const char * 5400ecb_cold static const char *
5300procname (pointer x) 5401procname (pointer x)
5301{ 5402{
5302 return opname (procnum (x)); 5403 return opname (procnum (x));
5303} 5404}
5304 5405
5324#undef OP_DEF 5425#undef OP_DEF
5325 {0} 5426 {0}
5326}; 5427};
5327 5428
5328/* kernel of this interpreter */ 5429/* kernel of this interpreter */
5329static void ecb_hot 5430ecb_hot static void
5330Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5431Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5331{ 5432{
5332 SCHEME_V->op = op; 5433 SCHEME_V->op = op;
5333 5434
5334 for (;;) 5435 for (;;)
5417 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))
5418 return; 5519 return;
5419 5520
5420 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5521 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5421 { 5522 {
5422 xwrstr ("No memory!\n"); 5523 putstr (SCHEME_A_ "No memory!\n");
5423 return; 5524 return;
5424 } 5525 }
5425 } 5526 }
5426} 5527}
5427 5528
5428/* ========== Initialization of internal keywords ========== */ 5529/* ========== Initialization of internal keywords ========== */
5429 5530
5430static void 5531ecb_cold static void
5431assign_syntax (SCHEME_P_ const char *name) 5532assign_syntax (SCHEME_P_ const char *name)
5432{ 5533{
5433 pointer x = oblist_add_by_name (SCHEME_A_ name); 5534 pointer x = oblist_add_by_name (SCHEME_A_ name);
5434 set_typeflag (x, typeflag (x) | T_SYNTAX); 5535 set_typeflag (x, typeflag (x) | T_SYNTAX);
5435} 5536}
5436 5537
5437static void 5538ecb_cold static void
5438assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) 5539assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
5439{ 5540{
5440 pointer x = mk_symbol (SCHEME_A_ name); 5541 pointer x = mk_symbol (SCHEME_A_ name);
5441 pointer y = mk_proc (SCHEME_A_ op); 5542 pointer y = mk_proc (SCHEME_A_ op);
5442 new_slot_in_env (SCHEME_A_ x, y); 5543 new_slot_in_env (SCHEME_A_ x, y);
5450 ivalue_unchecked (y) = op; 5551 ivalue_unchecked (y) = op;
5451 return y; 5552 return y;
5452} 5553}
5453 5554
5454/* 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! */
5455static int 5556ecb_hot static int
5456syntaxnum (pointer p) 5557syntaxnum (pointer p)
5457{ 5558{
5458 const char *s = strvalue (car (p)); 5559 const char *s = strvalue (p);
5459 5560
5460 switch (strlength (car (p))) 5561 switch (strlength (p))
5461 { 5562 {
5462 case 2: 5563 case 2:
5463 if (s[0] == 'i') 5564 if (s[0] == 'i')
5464 return OP_IF0; /* if */ 5565 return OP_IF0; /* if */
5465 else 5566 else
5540ecb_cold int 5641ecb_cold int
5541scheme_init (SCHEME_P) 5642scheme_init (SCHEME_P)
5542{ 5643{
5543 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5644 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5544 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));
5545 5652
5546 num_set_fixnum (num_zero, 1); 5653 num_set_fixnum (num_zero, 1);
5547 num_set_ivalue (num_zero, 0); 5654 num_set_ivalue (num_zero, 0);
5548 num_set_fixnum (num_one, 1); 5655 num_set_fixnum (num_one, 1);
5549 num_set_ivalue (num_one, 1); 5656 num_set_ivalue (num_one, 1);
5561 SCHEME_V->save_inport = NIL; 5668 SCHEME_V->save_inport = NIL;
5562 SCHEME_V->loadport = NIL; 5669 SCHEME_V->loadport = NIL;
5563 SCHEME_V->nesting = 0; 5670 SCHEME_V->nesting = 0;
5564 SCHEME_V->interactive_repl = 0; 5671 SCHEME_V->interactive_repl = 0;
5565 5672
5566 if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) 5673 if (!alloc_cellseg (SCHEME_A))
5567 { 5674 {
5568#if USE_ERROR_CHECKING 5675#if USE_ERROR_CHECKING
5569 SCHEME_V->no_memory = 1; 5676 SCHEME_V->no_memory = 1;
5570 return 0; 5677 return 0;
5571#endif 5678#endif
5639 5746
5640 return !SCHEME_V->no_memory; 5747 return !SCHEME_V->no_memory;
5641} 5748}
5642 5749
5643#if USE_PORTS 5750#if USE_PORTS
5644void 5751ecb_cold void
5645scheme_set_input_port_file (SCHEME_P_ int fin) 5752scheme_set_input_port_file (SCHEME_P_ int fin)
5646{ 5753{
5647 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); 5754 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input);
5648} 5755}
5649 5756
5650void 5757ecb_cold void
5651scheme_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)
5652{ 5759{
5653 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);
5654} 5761}
5655 5762
5656void 5763ecb_cold void
5657scheme_set_output_port_file (SCHEME_P_ int fout) 5764scheme_set_output_port_file (SCHEME_P_ int fout)
5658{ 5765{
5659 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); 5766 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output);
5660} 5767}
5661 5768
5662void 5769ecb_cold void
5663scheme_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)
5664{ 5771{
5665 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);
5666} 5773}
5667#endif 5774#endif
5668 5775
5669void 5776ecb_cold void
5670scheme_set_external_data (SCHEME_P_ void *p) 5777scheme_set_external_data (SCHEME_P_ void *p)
5671{ 5778{
5672 SCHEME_V->ext_data = p; 5779 SCHEME_V->ext_data = p;
5673} 5780}
5674 5781
5722 } 5829 }
5723 } 5830 }
5724#endif 5831#endif
5725} 5832}
5726 5833
5727void 5834ecb_cold void
5728scheme_load_file (SCHEME_P_ int fin) 5835scheme_load_file (SCHEME_P_ int fin)
5729{ 5836{
5730 scheme_load_named_file (SCHEME_A_ fin, 0); 5837 scheme_load_named_file (SCHEME_A_ fin, 0);
5731} 5838}
5732 5839
5733void 5840ecb_cold void
5734scheme_load_named_file (SCHEME_P_ int fin, const char *filename) 5841scheme_load_named_file (SCHEME_P_ int fin, const char *filename)
5735{ 5842{
5736 dump_stack_reset (SCHEME_A); 5843 dump_stack_reset (SCHEME_A);
5737 SCHEME_V->envir = SCHEME_V->global_env; 5844 SCHEME_V->envir = SCHEME_V->global_env;
5738 SCHEME_V->file_i = 0; 5845 SCHEME_V->file_i = 0;
5765 5872
5766 if (SCHEME_V->retcode == 0) 5873 if (SCHEME_V->retcode == 0)
5767 SCHEME_V->retcode = SCHEME_V->nesting != 0; 5874 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5768} 5875}
5769 5876
5770void 5877ecb_cold void
5771scheme_load_string (SCHEME_P_ const char *cmd) 5878scheme_load_string (SCHEME_P_ const char *cmd)
5772{ 5879{
5773 dump_stack_reset (SCHEME_A); 5880 dump_stack_reset (SCHEME_A);
5774 SCHEME_V->envir = SCHEME_V->global_env; 5881 SCHEME_V->envir = SCHEME_V->global_env;
5775 SCHEME_V->file_i = 0; 5882 SCHEME_V->file_i = 0;
5789 5896
5790 if (SCHEME_V->retcode == 0) 5897 if (SCHEME_V->retcode == 0)
5791 SCHEME_V->retcode = SCHEME_V->nesting != 0; 5898 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5792} 5899}
5793 5900
5794void 5901ecb_cold void
5795scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) 5902scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value)
5796{ 5903{
5797 pointer x; 5904 pointer x;
5798 5905
5799 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 5906 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
5804 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 5911 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
5805} 5912}
5806 5913
5807#if !STANDALONE 5914#if !STANDALONE
5808 5915
5809void 5916ecb_cold void
5810scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 5917scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
5811{ 5918{
5812 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));
5813} 5920}
5814 5921
5815void 5922ecb_cold void
5816scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 5923scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
5817{ 5924{
5818 int i; 5925 int i;
5819 5926
5820 for (i = 0; i < count; i++) 5927 for (i = 0; i < count; i++)
5821 scheme_register_foreign_func (SCHEME_A_ list + i); 5928 scheme_register_foreign_func (SCHEME_A_ list + i);
5822} 5929}
5823 5930
5824pointer 5931ecb_cold pointer
5825scheme_apply0 (SCHEME_P_ const char *procname) 5932scheme_apply0 (SCHEME_P_ const char *procname)
5826{ 5933{
5827 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));
5828} 5935}
5829 5936
5830void 5937ecb_cold void
5831save_from_C_call (SCHEME_P) 5938save_from_C_call (SCHEME_P)
5832{ 5939{
5833 pointer saved_data = cons (car (S_SINK), 5940 pointer saved_data = cons (car (S_SINK),
5834 cons (SCHEME_V->envir, 5941 cons (SCHEME_V->envir,
5835 SCHEME_V->dump)); 5942 SCHEME_V->dump));
5839 /* 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
5840 directly resume pre-C-call operations. */ 5947 directly resume pre-C-call operations. */
5841 dump_stack_reset (SCHEME_A); 5948 dump_stack_reset (SCHEME_A);
5842} 5949}
5843 5950
5844void 5951ecb_cold void
5845restore_from_C_call (SCHEME_P) 5952restore_from_C_call (SCHEME_P)
5846{ 5953{
5847 set_car (S_SINK, caar (SCHEME_V->c_nest)); 5954 set_car (S_SINK, caar (SCHEME_V->c_nest));
5848 SCHEME_V->envir = cadar (SCHEME_V->c_nest); 5955 SCHEME_V->envir = cadar (SCHEME_V->c_nest);
5849 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); 5956 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest));
5850 /* Pop */ 5957 /* Pop */
5851 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); 5958 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest);
5852} 5959}
5853 5960
5854/* "func" and "args" are assumed to be already eval'ed. */ 5961/* "func" and "args" are assumed to be already eval'ed. */
5855pointer 5962ecb_cold pointer
5856scheme_call (SCHEME_P_ pointer func, pointer args) 5963scheme_call (SCHEME_P_ pointer func, pointer args)
5857{ 5964{
5858 int old_repl = SCHEME_V->interactive_repl; 5965 int old_repl = SCHEME_V->interactive_repl;
5859 5966
5860 SCHEME_V->interactive_repl = 0; 5967 SCHEME_V->interactive_repl = 0;
5867 SCHEME_V->interactive_repl = old_repl; 5974 SCHEME_V->interactive_repl = old_repl;
5868 restore_from_C_call (SCHEME_A); 5975 restore_from_C_call (SCHEME_A);
5869 return SCHEME_V->value; 5976 return SCHEME_V->value;
5870} 5977}
5871 5978
5872pointer 5979ecb_cold pointer
5873scheme_eval (SCHEME_P_ pointer obj) 5980scheme_eval (SCHEME_P_ pointer obj)
5874{ 5981{
5875 int old_repl = SCHEME_V->interactive_repl; 5982 int old_repl = SCHEME_V->interactive_repl;
5876 5983
5877 SCHEME_V->interactive_repl = 0; 5984 SCHEME_V->interactive_repl = 0;
5889 5996
5890/* ========== Main ========== */ 5997/* ========== Main ========== */
5891 5998
5892#if STANDALONE 5999#if STANDALONE
5893 6000
5894int 6001ecb_cold int
5895main (int argc, char **argv) 6002main (int argc, char **argv)
5896{ 6003{
5897# if USE_MULTIPLICITY 6004# if USE_MULTIPLICITY
5898 scheme ssc; 6005 scheme ssc;
5899 scheme *const SCHEME_V = &ssc; 6006 scheme *const SCHEME_V = &ssc;
5901# endif 6008# endif
5902 int fin; 6009 int fin;
5903 char *file_name = InitFile; 6010 char *file_name = InitFile;
5904 int retcode; 6011 int retcode;
5905 int isfile = 1; 6012 int isfile = 1;
6013 system ("ps v $PPID");//D
5906 6014
5907 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6015 if (argc == 2 && strcmp (argv[1], "-?") == 0)
5908 { 6016 {
5909 xwrstr ("Usage: tinyscheme -?\n"); 6017 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
5910 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6018 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
5911 xwrstr ("followed by\n"); 6019 putstr (SCHEME_A_ "followed by\n");
5912 xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); 6020 putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n");
5913 xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); 6021 putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n");
5914 xwrstr ("assuming that the executable is named tinyscheme.\n"); 6022 putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n");
5915 xwrstr ("Use - as filename for stdin.\n"); 6023 putstr (SCHEME_A_ "Use - as filename for stdin.\n");
5916 return 1; 6024 return 1;
5917 } 6025 }
5918 6026
5919 if (!scheme_init (SCHEME_A)) 6027 if (!scheme_init (SCHEME_A))
5920 { 6028 {
5921 xwrstr ("Could not initialize!\n"); 6029 putstr (SCHEME_A_ "Could not initialize!\n");
5922 return 2; 6030 return 2;
5923 } 6031 }
5924 6032
5925# if USE_PORTS 6033# if USE_PORTS
5926 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); 6034 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO);
5971 fin = open (file_name, O_RDONLY); 6079 fin = open (file_name, O_RDONLY);
5972#endif 6080#endif
5973 6081
5974 if (isfile && fin < 0) 6082 if (isfile && fin < 0)
5975 { 6083 {
5976 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");
5977 } 6085 }
5978 else 6086 else
5979 { 6087 {
5980 if (isfile) 6088 if (isfile)
5981 scheme_load_named_file (SCHEME_A_ fin, file_name); 6089 scheme_load_named_file (SCHEME_A_ fin, file_name);
5985#if USE_PORTS 6093#if USE_PORTS
5986 if (!isfile || fin != STDIN_FILENO) 6094 if (!isfile || fin != STDIN_FILENO)
5987 { 6095 {
5988 if (SCHEME_V->retcode != 0) 6096 if (SCHEME_V->retcode != 0)
5989 { 6097 {
5990 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");
5991 } 6099 }
5992 6100
5993 if (isfile) 6101 if (isfile)
5994 close (fin); 6102 close (fin);
5995 } 6103 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines