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.49 by root, Mon Nov 30 13:09:56 2015 UTC vs.
Revision 1.60 by root, Wed Dec 2 02:59:36 2015 UTC

18 * 18 *
19 */ 19 */
20 20
21#define EXPERIMENT 1 21#define EXPERIMENT 1
22 22
23#if 1
23#define PAGE_SIZE 4096 /* does not work on sparc/alpha */ 24#define PAGE_SIZE 4096 /* does not work on sparc/alpha */
24#include "malloc.c" 25#include "malloc.c"
26#endif
25 27
26#define SCHEME_SOURCE 28#define SCHEME_SOURCE
27#include "scheme-private.h" 29#include "scheme-private.h"
28#ifndef WIN32 30#ifndef WIN32
29# include <unistd.h> 31# include <unistd.h>
79 81
80#define BACKQUOTE '`' 82#define BACKQUOTE '`'
81#define WHITESPACE " \t\r\n\v\f" 83#define WHITESPACE " \t\r\n\v\f"
82#define DELIMITERS "()\";" WHITESPACE 84#define DELIMITERS "()\";" WHITESPACE
83 85
84#define NIL (&SCHEME_V->xNIL) //TODO: make this 0? 86#define NIL POINTER (&SCHEME_V->xNIL)
85#define S_T (&SCHEME_V->xT) //TODO: magic ptr value? 87#define S_T POINTER (&SCHEME_V->xT)
86#define S_F (&SCHEME_V->xF) //TODO: magic ptr value? 88#define S_F POINTER (&SCHEME_V->xF)
87#define S_SINK (&SCHEME_V->xsink) 89#define S_SINK POINTER (&SCHEME_V->xsink)
88#define S_EOF (&SCHEME_V->xEOF_OBJ) 90#define S_EOF POINTER (&SCHEME_V->xEOF_OBJ)
89 91
90#if !USE_MULTIPLICITY 92#if !USE_MULTIPLICITY
91static scheme sc; 93static scheme sc;
92#endif 94#endif
93 95
94static void 96ecb_cold static void
95xbase (char *s, long n, int base) 97xbase (char *s, long n, int base)
96{ 98{
97 if (n < 0) 99 if (n < 0)
98 { 100 {
99 *s++ = '-'; 101 *s++ = '-';
101 } 103 }
102 104
103 char *p = s; 105 char *p = s;
104 106
105 do { 107 do {
106 *p++ = '0' + n % base; 108 *p++ = "0123456789abcdef"[n % base];
107 n /= base; 109 n /= base;
108 } while (n); 110 } while (n);
109 111
110 *p-- = 0; 112 *p-- = 0;
111 113
114 char x = *s; *s = *p; *p = x; 116 char x = *s; *s = *p; *p = x;
115 --p; ++s; 117 --p; ++s;
116 } 118 }
117} 119}
118 120
119static void 121ecb_cold static void
120xnum (char *s, long n) 122xnum (char *s, long n)
121{ 123{
122 xbase (s, n, 10); 124 xbase (s, n, 10);
123} 125}
124 126
125static void 127ecb_cold static void
126xwrstr (const char *s) 128putnum (SCHEME_P_ long n)
127{
128 write (1, s, strlen (s));
129}
130
131static void
132xwrnum (long n)
133{ 129{
134 char buf[64]; 130 char buf[64];
135 131
136 xnum (buf, n); 132 xnum (buf, n);
137 xwrstr (buf); 133 putstr (SCHEME_A_ buf);
138} 134}
139 135
140static char 136ecb_cold static char
141xtoupper (char c) 137xtoupper (char c)
142{ 138{
143 if (c >= 'a' && c <= 'z') 139 if (c >= 'a' && c <= 'z')
144 c -= 'a' - 'A'; 140 c -= 'a' - 'A';
145 141
146 return c; 142 return c;
147} 143}
148 144
149static char 145ecb_cold static char
150xtolower (char c) 146xtolower (char c)
151{ 147{
152 if (c >= 'A' && c <= 'Z') 148 if (c >= 'A' && c <= 'Z')
153 c += 'a' - 'A'; 149 c += 'a' - 'A';
154 150
155 return c; 151 return c;
156} 152}
157 153
158static int 154ecb_cold static int
159xisdigit (char c) 155xisdigit (char c)
160{ 156{
161 return c >= '0' && c <= '9'; 157 return c >= '0' && c <= '9';
162} 158}
163 159
164#define toupper(c) xtoupper (c) 160#define toupper(c) xtoupper (c)
165#define tolower(c) xtolower (c) 161#define tolower(c) xtolower (c)
166#define isdigit(c) xisdigit (c) 162#define isdigit(c) xisdigit (c)
167 163
168#if USE_IGNORECASE 164#if USE_IGNORECASE
169static const char * 165ecb_cold static const char *
170xstrlwr (char *s) 166xstrlwr (char *s)
171{ 167{
172 const char *p = s; 168 const char *p = s;
173 169
174 while (*s) 170 while (*s)
194 190
195#ifndef InitFile 191#ifndef InitFile
196# define InitFile "init.scm" 192# define InitFile "init.scm"
197#endif 193#endif
198 194
199#ifndef FIRST_CELLSEGS
200# define FIRST_CELLSEGS 3
201#endif
202
203enum scheme_types 195enum scheme_types
204{ 196{
205 T_INTEGER, 197 T_INTEGER,
198 T_CHARACTER,
206 T_REAL, 199 T_REAL,
207 T_STRING, 200 T_STRING,
208 T_SYMBOL, 201 T_SYMBOL,
209 T_PROC, 202 T_PROC,
210 T_PAIR, /* also used for free cells */ 203 T_PAIR, /* also used for free cells */
211 T_CLOSURE, 204 T_CLOSURE,
205 T_MACRO,
212 T_CONTINUATION, 206 T_CONTINUATION,
213 T_FOREIGN, 207 T_FOREIGN,
214 T_CHARACTER,
215 T_PORT, 208 T_PORT,
216 T_VECTOR, 209 T_VECTOR,
217 T_MACRO,
218 T_PROMISE, 210 T_PROMISE,
219 T_ENVIRONMENT, 211 T_ENVIRONMENT,
220 /* one more... */ 212 /* one more... */
221 T_NUM_SYSTEM_TYPES 213 T_NUM_SYSTEM_TYPES
222}; 214};
258static num num_op (enum num_op op, num a, num b); 250static num num_op (enum num_op op, num a, num b);
259static num num_intdiv (num a, num b); 251static num num_intdiv (num a, num b);
260static num num_rem (num a, num b); 252static num num_rem (num a, num b);
261static num num_mod (num a, num b); 253static num num_mod (num a, num b);
262 254
263#if USE_MATH
264static double round_per_R5RS (double x);
265#endif
266static int is_zero_rvalue (RVALUE x); 255static int is_zero_rvalue (RVALUE x);
267 256
268static num num_zero; 257static num num_zero;
269static num num_one; 258static num num_one;
270 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
271/* macros for cell operations */ 264/* macros for cell operations */
272#define typeflag(p) ((p)->flag + 0) 265#define typeflag(p) (CELL(p)->flag + 0)
273#define set_typeflag(p,v) ((p)->flag = (v)) 266#define set_typeflag(p,v) (CELL(p)->flag = (v))
274#define type(p) (typeflag (p) & T_MASKTYPE) 267#define type(p) (typeflag (p) & T_MASKTYPE)
275 268
276INTERFACE int 269INTERFACE int
277is_string (pointer p) 270is_string (pointer p)
278{ 271{
279 return type (p) == T_STRING; 272 return type (p) == T_STRING;
280} 273}
281 274
282#define strvalue(p) ((p)->object.string.svalue) 275#define strvalue(p) (CELL(p)->object.string.svalue)
283#define strlength(p) ((p)->object.string.length) 276#define strlength(p) (CELL(p)->object.string.length)
284 277
285INTERFACE int 278INTERFACE int
286is_vector (pointer p) 279is_vector (pointer p)
287{ 280{
288 return type (p) == T_VECTOR; 281 return type (p) == T_VECTOR;
289} 282}
290 283
291#define vecvalue(p) ((p)->object.vector.vvalue) 284#define vecvalue(p) (CELL(p)->object.vector.vvalue)
292#define veclength(p) ((p)->object.vector.length) 285#define veclength(p) (CELL(p)->object.vector.length)
293INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); 286INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
294INTERFACE pointer vector_get (pointer vec, uint32_t ielem); 287INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
295INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); 288INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
296 289
297INTERFACE int 290INTERFACE int
323string_value (pointer p) 316string_value (pointer p)
324{ 317{
325 return strvalue (p); 318 return strvalue (p);
326} 319}
327 320
328#define ivalue_unchecked(p) (p)->object.ivalue 321#define ivalue_unchecked(p) CELL(p)->object.ivalue
329#define set_ivalue(p,v) (p)->object.ivalue = (v) 322#define set_ivalue(p,v) CELL(p)->object.ivalue = (v)
330 323
331#if USE_REAL 324#if USE_REAL
332#define rvalue_unchecked(p) (p)->object.rvalue 325#define rvalue_unchecked(p) CELL(p)->object.rvalue
333#define set_rvalue(p,v) (p)->object.rvalue = (v) 326#define set_rvalue(p,v) CELL(p)->object.rvalue = (v)
334#else 327#else
335#define rvalue_unchecked(p) (p)->object.ivalue 328#define rvalue_unchecked(p) CELL(p)->object.ivalue
336#define set_rvalue(p,v) (p)->object.ivalue = (v) 329#define set_rvalue(p,v) CELL(p)->object.ivalue = (v)
337#endif 330#endif
338 331
339INTERFACE long 332INTERFACE long
340charvalue (pointer p) 333charvalue (pointer p)
341{ 334{
342 return ivalue_unchecked (p); 335 return ivalue_unchecked (p);
343} 336}
344 337
338#define port(p) CELL(p)->object.port
339#define set_port(p,v) port(p) = (v)
345INTERFACE int 340INTERFACE int
346is_port (pointer p) 341is_port (pointer p)
347{ 342{
348 return type (p) == T_PORT; 343 return type (p) == T_PORT;
349} 344}
350 345
351INTERFACE int 346INTERFACE int
352is_inport (pointer p) 347is_inport (pointer p)
353{ 348{
354 return is_port (p) && p->object.port->kind & port_input; 349 return is_port (p) && port (p)->kind & port_input;
355} 350}
356 351
357INTERFACE int 352INTERFACE int
358is_outport (pointer p) 353is_outport (pointer p)
359{ 354{
360 return is_port (p) && p->object.port->kind & port_output; 355 return is_port (p) && port (p)->kind & port_output;
361} 356}
362 357
363INTERFACE int 358INTERFACE int
364is_pair (pointer p) 359is_pair (pointer p)
365{ 360{
366 return type (p) == T_PAIR; 361 return type (p) == T_PAIR;
367} 362}
368 363
369#define car(p) ((p)->object.cons.car + 0) 364#define car(p) (POINTER (CELL(p)->object.cons.car))
370#define cdr(p) ((p)->object.cons.cdr + 0) 365#define cdr(p) (POINTER (CELL(p)->object.cons.cdr))
371 366
372static pointer caar (pointer p) { return car (car (p)); } 367static pointer caar (pointer p) { return car (car (p)); }
373static pointer cadr (pointer p) { return car (cdr (p)); } 368static pointer cadr (pointer p) { return car (cdr (p)); }
374static pointer cdar (pointer p) { return cdr (car (p)); } 369static pointer cdar (pointer p) { return cdr (car (p)); }
375static pointer cddr (pointer p) { return cdr (cdr (p)); } 370static pointer cddr (pointer p) { return cdr (cdr (p)); }
379static pointer cdaar (pointer p) { return cdr (car (car (p))); } 374static pointer cdaar (pointer p) { return cdr (car (car (p))); }
380 375
381INTERFACE void 376INTERFACE void
382set_car (pointer p, pointer q) 377set_car (pointer p, pointer q)
383{ 378{
384 p->object.cons.car = q; 379 CELL(p)->object.cons.car = CELL (q);
385} 380}
386 381
387INTERFACE void 382INTERFACE void
388set_cdr (pointer p, pointer q) 383set_cdr (pointer p, pointer q)
389{ 384{
390 p->object.cons.cdr = q; 385 CELL(p)->object.cons.cdr = CELL (q);
391} 386}
392 387
393INTERFACE pointer 388INTERFACE pointer
394pair_car (pointer p) 389pair_car (pointer p)
395{ 390{
525 proper list: length 520 proper list: length
526 circular list: -1 521 circular list: -1
527 not even a pair: -2 522 not even a pair: -2
528 dotted list: -2 minus length before dot 523 dotted list: -2 minus length before dot
529*/ 524*/
530INTERFACE int 525ecb_hot INTERFACE int
531list_length (SCHEME_P_ pointer a) 526list_length (SCHEME_P_ pointer a)
532{ 527{
533 int i = 0; 528 int i = 0;
534 pointer slow, fast; 529 pointer slow, fast;
535 530
641 "gs", 636 "gs",
642 "rs", 637 "rs",
643 "us" 638 "us"
644}; 639};
645 640
646static int 641ecb_cold static int
647is_ascii_name (const char *name, int *pc) 642is_ascii_name (const char *name, int *pc)
648{ 643{
649 int i; 644 int i;
650 645
651 for (i = 0; i < 32; i++) 646 for (i = 0; i < 32; i++)
670 665
671static int file_push (SCHEME_P_ const char *fname); 666static int file_push (SCHEME_P_ const char *fname);
672static void file_pop (SCHEME_P); 667static void file_pop (SCHEME_P);
673static int file_interactive (SCHEME_P); 668static int file_interactive (SCHEME_P);
674ecb_inline int is_one_of (const char *s, int c); 669ecb_inline int is_one_of (const char *s, int c);
675static int alloc_cellseg (SCHEME_P_ int n); 670static int alloc_cellseg (SCHEME_P);
676ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); 671ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
677static void finalize_cell (SCHEME_P_ pointer a); 672static void finalize_cell (SCHEME_P_ pointer a);
678static int count_consecutive_cells (pointer x, int needed); 673static int count_consecutive_cells (pointer x, int needed);
679static 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);
680static pointer mk_number (SCHEME_P_ const num n); 675static pointer mk_number (SCHEME_P_ const num n);
874 } 869 }
875 870
876 return ret; 871 return ret;
877} 872}
878 873
879#if USE_MATH
880
881/* Round to nearest. Round to even if midway */
882static double
883round_per_R5RS (double x)
884{
885 double fl = floor (x);
886 double ce = ceil (x);
887 double dfl = x - fl;
888 double dce = ce - x;
889
890 if (dfl > dce)
891 return ce;
892 else if (dfl < dce)
893 return fl;
894 else
895 {
896 if (fmod (fl, 2) == 0) /* I imagine this holds */
897 return fl;
898 else
899 return ce;
900 }
901}
902#endif
903
904static int 874static int
905is_zero_rvalue (RVALUE x) 875is_zero_rvalue (RVALUE x)
906{ 876{
907 return x == 0; 877 return x == 0;
908#if 0 878#if 0
913#endif 883#endif
914#endif 884#endif
915} 885}
916 886
917/* allocate new cell segment */ 887/* allocate new cell segment */
918static int 888ecb_cold static int
919alloc_cellseg (SCHEME_P_ int n) 889alloc_cellseg (SCHEME_P)
920{ 890{
921 pointer newp; 891 struct cell *newp;
922 pointer last; 892 struct cell *last;
923 pointer p; 893 struct cell *p;
924 char *cp; 894 char *cp;
925 long i; 895 long i;
926 int k; 896 int k;
927 897
928 static int segsize = CELL_SEGSIZE >> 1; 898 static int segsize = CELL_SEGSIZE >> 1;
929 segsize <<= 1; 899 segsize <<= 1;
930 900
931 for (k = 0; k < n; k++)
932 {
933 if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1)
934 return k;
935
936 cp = malloc (segsize * sizeof (struct cell)); 901 cp = malloc (segsize * sizeof (struct cell));
937 902
938 if (!cp && USE_ERROR_CHECKING) 903 if (!cp && USE_ERROR_CHECKING)
939 return k; 904 return k;
940 905
941 i = ++SCHEME_V->last_cell_seg; 906 i = ++SCHEME_V->last_cell_seg;
942 SCHEME_V->alloc_seg[i] = cp; 907 SCHEME_V->alloc_seg[i] = cp;
943 908
944 newp = (pointer)cp; 909 newp = (struct cell *)cp;
945 SCHEME_V->cell_seg[i] = newp; 910 SCHEME_V->cell_seg[i] = newp;
946 SCHEME_V->cell_segsize[i] = segsize; 911 SCHEME_V->cell_segsize[i] = segsize;
947 SCHEME_V->fcells += segsize; 912 SCHEME_V->fcells += segsize;
948 last = newp + segsize - 1; 913 last = newp + segsize - 1;
949 914
950 for (p = newp; p <= last; p++) 915 for (p = newp; p <= last; p++)
951 { 916 {
917 pointer cp = POINTER (p);
952 set_typeflag (p, T_PAIR); 918 set_typeflag (cp, T_PAIR);
953 set_car (p, NIL); 919 set_car (cp, NIL);
954 set_cdr (p, p + 1); 920 set_cdr (cp, POINTER (p + 1));
955 } 921 }
956 922
957 set_cdr (last, SCHEME_V->free_cell); 923 set_cdr (POINTER (last), SCHEME_V->free_cell);
958 SCHEME_V->free_cell = newp; 924 SCHEME_V->free_cell = POINTER (newp);
959 }
960 925
961 return n; 926 return 1;
962} 927}
963 928
964/* get new cell. parameter a, b is marked by gc. */ 929/* get new cell. parameter a, b is marked by gc. */
965ecb_inline pointer 930ecb_inline pointer
966get_cell_x (SCHEME_P_ pointer a, pointer b) 931get_cell_x (SCHEME_P_ pointer a, pointer b)
977 gc (SCHEME_A_ a, b); 942 gc (SCHEME_A_ a, b);
978 943
979 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)
980 { 945 {
981 /* 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 */
982 if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) 947 if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL)
983 { 948 {
984#if USE_ERROR_CHECKING 949#if USE_ERROR_CHECKING
985 SCHEME_V->no_memory = 1; 950 SCHEME_V->no_memory = 1;
986 return S_SINK; 951 return S_SINK;
987#endif 952#endif
999 } 964 }
1000} 965}
1001 966
1002/* To retain recent allocs before interpreter knows about them - 967/* To retain recent allocs before interpreter knows about them -
1003 Tehom */ 968 Tehom */
1004 969ecb_hot static void
1005static void
1006push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 970push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
1007{ 971{
1008 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 972 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
1009 973
1010 set_typeflag (holder, T_PAIR); 974 set_typeflag (holder, T_PAIR);
1012 set_car (holder, recent); 976 set_car (holder, recent);
1013 set_cdr (holder, car (S_SINK)); 977 set_cdr (holder, car (S_SINK));
1014 set_car (S_SINK, holder); 978 set_car (S_SINK, holder);
1015} 979}
1016 980
1017static pointer 981ecb_hot static pointer
1018get_cell (SCHEME_P_ pointer a, pointer b) 982get_cell (SCHEME_P_ pointer a, pointer b)
1019{ 983{
1020 pointer cell = get_cell_x (SCHEME_A_ a, b); 984 pointer cell = get_cell_x (SCHEME_A_ a, b);
1021 985
1022 /* 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
1040 return S_SINK; 1004 return S_SINK;
1041 1005
1042 /* Record it as a vector so that gc understands it. */ 1006 /* Record it as a vector so that gc understands it. */
1043 set_typeflag (v, T_VECTOR | T_ATOM); 1007 set_typeflag (v, T_VECTOR | T_ATOM);
1044 1008
1045 v->object.vector.vvalue = e; 1009 CELL(v)->object.vector.vvalue = e;
1046 v->object.vector.length = len; 1010 CELL(v)->object.vector.length = len;
1047 fill_vector (v, 0, init); 1011 fill_vector (v, 0, init);
1048 push_recent_alloc (SCHEME_A_ v, NIL); 1012 push_recent_alloc (SCHEME_A_ v, NIL);
1049 1013
1050 return v; 1014 return v;
1051} 1015}
1060static void 1024static void
1061check_cell_alloced (pointer p, int expect_alloced) 1025check_cell_alloced (pointer p, int expect_alloced)
1062{ 1026{
1063 /* 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. */
1064 if (typeflag (p) & !expect_alloced) 1028 if (typeflag (p) & !expect_alloced)
1065 xwrstr ("Cell is already allocated!\n"); 1029 putstr (SCHEME_A_ "Cell is already allocated!\n");
1066 1030
1067 if (!(typeflag (p)) & expect_alloced) 1031 if (!(typeflag (p)) & expect_alloced)
1068 xwrstr ("Cell is not allocated!\n"); 1032 putstr (SCHEME_A_ "Cell is not allocated!\n");
1069} 1033}
1070 1034
1071static void 1035static void
1072check_range_alloced (pointer p, int n, int expect_alloced) 1036check_range_alloced (pointer p, int n, int expect_alloced)
1073{ 1037{
1079#endif 1043#endif
1080 1044
1081/* Medium level cell allocation */ 1045/* Medium level cell allocation */
1082 1046
1083/* get new cons cell */ 1047/* get new cons cell */
1084pointer 1048ecb_hot pointer
1085xcons (SCHEME_P_ pointer a, pointer b, int immutable) 1049xcons (SCHEME_P_ pointer a, pointer b, int immutable)
1086{ 1050{
1087 pointer x = get_cell (SCHEME_A_ a, b); 1051 pointer x = get_cell (SCHEME_A_ a, b);
1088 1052
1089 set_typeflag (x, T_PAIR); 1053 set_typeflag (x, T_PAIR);
1095 set_cdr (x, b); 1059 set_cdr (x, b);
1096 1060
1097 return x; 1061 return x;
1098} 1062}
1099 1063
1100static pointer 1064ecb_cold static pointer
1101generate_symbol (SCHEME_P_ const char *name) 1065generate_symbol (SCHEME_P_ const char *name)
1102{ 1066{
1103 pointer x = mk_string (SCHEME_A_ name); 1067 pointer x = mk_string (SCHEME_A_ name);
1104 setimmutable (x); 1068 setimmutable (x);
1105 set_typeflag (x, T_SYMBOL | T_ATOM); 1069 set_typeflag (x, T_SYMBOL | T_ATOM);
1120 hash = (hash ^ *p++) * 16777619; 1084 hash = (hash ^ *p++) * 16777619;
1121 1085
1122 return hash % table_size; 1086 return hash % table_size;
1123} 1087}
1124 1088
1125static pointer 1089ecb_cold static pointer
1126oblist_initial_value (SCHEME_P) 1090oblist_initial_value (SCHEME_P)
1127{ 1091{
1128 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1092 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1129} 1093}
1130 1094
1131/* returns the new symbol */ 1095/* returns the new symbol */
1132static pointer 1096ecb_cold static pointer
1133oblist_add_by_name (SCHEME_P_ const char *name) 1097oblist_add_by_name (SCHEME_P_ const char *name)
1134{ 1098{
1135 pointer x = generate_symbol (SCHEME_A_ name); 1099 pointer x = generate_symbol (SCHEME_A_ name);
1136 int location = hash_fn (name, veclength (SCHEME_V->oblist)); 1100 int location = hash_fn (name, veclength (SCHEME_V->oblist));
1137 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)));
1138 return x; 1102 return x;
1139} 1103}
1140 1104
1141ecb_inline pointer 1105ecb_cold static pointer
1142oblist_find_by_name (SCHEME_P_ const char *name) 1106oblist_find_by_name (SCHEME_P_ const char *name)
1143{ 1107{
1144 int location; 1108 int location;
1145 pointer x; 1109 pointer x;
1146 char *s; 1110 char *s;
1157 } 1121 }
1158 1122
1159 return NIL; 1123 return NIL;
1160} 1124}
1161 1125
1162static pointer 1126ecb_cold static pointer
1163oblist_all_symbols (SCHEME_P) 1127oblist_all_symbols (SCHEME_P)
1164{ 1128{
1165 int i; 1129 int i;
1166 pointer x; 1130 pointer x;
1167 pointer ob_list = NIL; 1131 pointer ob_list = NIL;
1173 return ob_list; 1137 return ob_list;
1174} 1138}
1175 1139
1176#else 1140#else
1177 1141
1178static pointer 1142ecb_cold static pointer
1179oblist_initial_value (SCHEME_P) 1143oblist_initial_value (SCHEME_P)
1180{ 1144{
1181 return NIL; 1145 return NIL;
1182} 1146}
1183 1147
1184ecb_inline pointer 1148ecb_cold static pointer
1185oblist_find_by_name (SCHEME_P_ const char *name) 1149oblist_find_by_name (SCHEME_P_ const char *name)
1186{ 1150{
1187 pointer x; 1151 pointer x;
1188 char *s; 1152 char *s;
1189 1153
1198 1162
1199 return NIL; 1163 return NIL;
1200} 1164}
1201 1165
1202/* returns the new symbol */ 1166/* returns the new symbol */
1203static pointer 1167ecb_cold static pointer
1204oblist_add_by_name (SCHEME_P_ const char *name) 1168oblist_add_by_name (SCHEME_P_ const char *name)
1205{ 1169{
1206 pointer x = generate_symbol (SCHEME_A_ name); 1170 pointer x = generate_symbol (SCHEME_A_ name);
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
1402 x = oblist_add_by_name (SCHEME_A_ name); 1366 x = oblist_add_by_name (SCHEME_A_ name);
1403 1367
1404 return x; 1368 return x;
1405} 1369}
1406 1370
1407INTERFACE pointer 1371ecb_cold INTERFACE pointer
1408gensym (SCHEME_P) 1372gensym (SCHEME_P)
1409{ 1373{
1410 pointer x; 1374 pointer x;
1411 char name[40] = "gensym-"; 1375 char name[40] = "gensym-";
1412 xnum (name + 7, ++SCHEME_V->gensym_cnt); 1376 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1419{ 1383{
1420 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; 1384 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1421} 1385}
1422 1386
1423/* make symbol or number atom from string */ 1387/* make symbol or number atom from string */
1424static pointer 1388ecb_cold static pointer
1425mk_atom (SCHEME_P_ char *q) 1389mk_atom (SCHEME_P_ char *q)
1426{ 1390{
1427 char c, *p; 1391 char c, *p;
1428 int has_dec_point = 0; 1392 int has_dec_point = 0;
1429 int has_fp_exp = 0; 1393 int has_fp_exp = 0;
1500 1464
1501 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1465 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1502} 1466}
1503 1467
1504/* make constant */ 1468/* make constant */
1505static pointer 1469ecb_cold static pointer
1506mk_sharp_const (SCHEME_P_ char *name) 1470mk_sharp_const (SCHEME_P_ char *name)
1507{ 1471{
1508 if (!strcmp (name, "t")) 1472 if (!strcmp (name, "t"))
1509 return S_T; 1473 return S_T;
1510 else if (!strcmp (name, "f")) 1474 else if (!strcmp (name, "f"))
1511 return S_F; 1475 return S_F;
1512 else if (*name == '\\') /* #\w (character) */ 1476 else if (*name == '\\') /* #\w (character) */
1513 { 1477 {
1514 int c; 1478 int c;
1515 1479
1480 // TODO: optimise
1516 if (stricmp (name + 1, "space") == 0) 1481 if (stricmp (name + 1, "space") == 0)
1517 c = ' '; 1482 c = ' ';
1518 else if (stricmp (name + 1, "newline") == 0) 1483 else if (stricmp (name + 1, "newline") == 0)
1519 c = '\n'; 1484 c = '\n';
1520 else if (stricmp (name + 1, "return") == 0) 1485 else if (stricmp (name + 1, "return") == 0)
1521 c = '\r'; 1486 c = '\r';
1522 else if (stricmp (name + 1, "tab") == 0) 1487 else if (stricmp (name + 1, "tab") == 0)
1523 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;
1524 else if (name[1] == 'x' && name[2] != 0) 1499 else if (name[1] == 'x' && name[2] != 0)
1525 { 1500 {
1526 long c1 = strtol (name + 2, 0, 16); 1501 long c1 = strtol (name + 2, 0, 16);
1527 1502
1528 if (0 <= c1 && c1 <= UCHAR_MAX) 1503 if (0 <= c1 && c1 <= UCHAR_MAX)
1553 return NIL; 1528 return NIL;
1554 } 1529 }
1555} 1530}
1556 1531
1557/* ========== 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}
1558 1552
1559/*-- 1553/*--
1560 * 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,
1561 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1555 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1562 * for marking. 1556 * for marking.
1563 * 1557 *
1564 * The exception is vectors - vectors are currently marked recursively, 1558 * The exception is vectors - vectors are currently marked recursively,
1565 * 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
1566 * word of context in the vector 1560 * word of context in the vector
1567 */ 1561 */
1568static void 1562ecb_hot static void
1569mark (pointer a) 1563mark (pointer a)
1570{ 1564{
1571 pointer t, q, p; 1565 pointer t, q, p;
1572 1566
1573 t = 0; 1567 t = 0;
1630 p = q; 1624 p = q;
1631 goto E6; 1625 goto E6;
1632 } 1626 }
1633} 1627}
1634 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
1635/* garbage collection. parameter a, b is marked. */ 1671/* garbage collection. parameter a, b is marked. */
1636static void 1672ecb_cold static void
1637gc (SCHEME_P_ pointer a, pointer b) 1673gc (SCHEME_P_ pointer a, pointer b)
1638{ 1674{
1639 pointer p;
1640 int i; 1675 int i;
1641 1676
1642 if (SCHEME_V->gc_verbose) 1677 if (SCHEME_V->gc_verbose)
1643 putstr (SCHEME_A_ "gc..."); 1678 putstr (SCHEME_A_ "gc...");
1644 1679
1677 clrmark (NIL); 1712 clrmark (NIL);
1678 SCHEME_V->fcells = 0; 1713 SCHEME_V->fcells = 0;
1679 SCHEME_V->free_cell = NIL; 1714 SCHEME_V->free_cell = NIL;
1680 1715
1681 if (SCHEME_V->gc_verbose) 1716 if (SCHEME_V->gc_verbose)
1682 xwrstr ("freeing..."); 1717 putstr (SCHEME_A_ "freeing...");
1683 1718
1684 uint32_t total = 0; 1719 gc_free (SCHEME_A);
1685
1686 /* Here we scan the cells to build the free-list. */
1687 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1688 {
1689 pointer end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1690 total += SCHEME_V->cell_segsize [i];
1691
1692 for (p = SCHEME_V->cell_seg[i]; p < end; ++p)
1693 {
1694 if (is_mark (p))
1695 clrmark (p);
1696 else
1697 {
1698 /* reclaim cell */
1699 if (typeflag (p) != T_PAIR)
1700 {
1701 finalize_cell (SCHEME_A_ p);
1702 set_typeflag (p, T_PAIR);
1703 set_car (p, NIL);
1704 }
1705
1706 ++SCHEME_V->fcells;
1707 set_cdr (p, SCHEME_V->free_cell);
1708 SCHEME_V->free_cell = p;
1709 }
1710 }
1711 }
1712
1713 if (SCHEME_V->gc_verbose)
1714 {
1715 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" out of "); xwrnum (total); xwrstr (" cells were recovered.\n");
1716 }
1717}
1718
1719static void
1720finalize_cell (SCHEME_P_ pointer a)
1721{
1722 /* TODO, fast bitmap check? */
1723 if (is_string (a) || is_symbol (a))
1724 free (strvalue (a));
1725 else if (is_vector (a))
1726 free (vecvalue (a));
1727#if USE_PORTS
1728 else if (is_port (a))
1729 {
1730 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit)
1731 port_close (SCHEME_A_ a, port_input | port_output);
1732
1733 free (a->object.port);
1734 }
1735#endif
1736} 1720}
1737 1721
1738/* ========== Routines for Reading ========== */ 1722/* ========== Routines for Reading ========== */
1739 1723
1740static int 1724ecb_cold static int
1741file_push (SCHEME_P_ const char *fname) 1725file_push (SCHEME_P_ const char *fname)
1742{ 1726{
1743#if USE_PORTS 1727#if USE_PORTS
1744 int fin; 1728 int fin;
1745 1729
1754 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; 1738 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1;
1755 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;
1756 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;
1757 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;
1758 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; 1742 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0;
1759 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);
1760 1744
1761#if SHOW_ERROR_LINE 1745#if SHOW_ERROR_LINE
1762 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;
1763 1747
1764 if (fname) 1748 if (fname)
1771#else 1755#else
1772 return 1; 1756 return 1;
1773#endif 1757#endif
1774} 1758}
1775 1759
1776static void 1760ecb_cold static void
1777file_pop (SCHEME_P) 1761file_pop (SCHEME_P)
1778{ 1762{
1779 if (SCHEME_V->file_i != 0) 1763 if (SCHEME_V->file_i != 0)
1780 { 1764 {
1781 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1765 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1782#if USE_PORTS 1766#if USE_PORTS
1783 port_close (SCHEME_A_ SCHEME_V->loadport, port_input); 1767 port_close (SCHEME_A_ SCHEME_V->loadport, port_input);
1784#endif 1768#endif
1785 SCHEME_V->file_i--; 1769 SCHEME_V->file_i--;
1786 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);
1787 } 1771 }
1788} 1772}
1789 1773
1790static int 1774ecb_cold static int
1791file_interactive (SCHEME_P) 1775file_interactive (SCHEME_P)
1792{ 1776{
1793#if USE_PORTS 1777#if USE_PORTS
1794 return SCHEME_V->file_i == 0 1778 return SCHEME_V->file_i == 0
1795 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1779 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1796 && (SCHEME_V->inport->object.port->kind & port_file); 1780 && (port (SCHEME_V->inport)->kind & port_file);
1797#else 1781#else
1798 return 0; 1782 return 0;
1799#endif 1783#endif
1800} 1784}
1801 1785
1802#if USE_PORTS 1786#if USE_PORTS
1803static port * 1787ecb_cold static port *
1804port_rep_from_filename (SCHEME_P_ const char *fn, int prop) 1788port_rep_from_filename (SCHEME_P_ const char *fn, int prop)
1805{ 1789{
1806 int fd; 1790 int fd;
1807 int flags; 1791 int flags;
1808 char *rw; 1792 char *rw;
1831# endif 1815# endif
1832 1816
1833 return pt; 1817 return pt;
1834} 1818}
1835 1819
1836static pointer 1820ecb_cold static pointer
1837port_from_filename (SCHEME_P_ const char *fn, int prop) 1821port_from_filename (SCHEME_P_ const char *fn, int prop)
1838{ 1822{
1839 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); 1823 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop);
1840 1824
1841 if (!pt && USE_ERROR_CHECKING) 1825 if (!pt && USE_ERROR_CHECKING)
1842 return NIL; 1826 return NIL;
1843 1827
1844 return mk_port (SCHEME_A_ pt); 1828 return mk_port (SCHEME_A_ pt);
1845} 1829}
1846 1830
1847static port * 1831ecb_cold static port *
1848port_rep_from_file (SCHEME_P_ int f, int prop) 1832port_rep_from_file (SCHEME_P_ int f, int prop)
1849{ 1833{
1850 port *pt = malloc (sizeof *pt); 1834 port *pt = malloc (sizeof *pt);
1851 1835
1852 if (!pt && USE_ERROR_CHECKING) 1836 if (!pt && USE_ERROR_CHECKING)
1857 pt->rep.stdio.file = f; 1841 pt->rep.stdio.file = f;
1858 pt->rep.stdio.closeit = 0; 1842 pt->rep.stdio.closeit = 0;
1859 return pt; 1843 return pt;
1860} 1844}
1861 1845
1862static pointer 1846ecb_cold static pointer
1863port_from_file (SCHEME_P_ int f, int prop) 1847port_from_file (SCHEME_P_ int f, int prop)
1864{ 1848{
1865 port *pt = port_rep_from_file (SCHEME_A_ f, prop); 1849 port *pt = port_rep_from_file (SCHEME_A_ f, prop);
1866 1850
1867 if (!pt && USE_ERROR_CHECKING) 1851 if (!pt && USE_ERROR_CHECKING)
1868 return NIL; 1852 return NIL;
1869 1853
1870 return mk_port (SCHEME_A_ pt); 1854 return mk_port (SCHEME_A_ pt);
1871} 1855}
1872 1856
1873static port * 1857ecb_cold static port *
1874port_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)
1875{ 1859{
1876 port *pt = malloc (sizeof (port)); 1860 port *pt = malloc (sizeof (port));
1877 1861
1878 if (!pt && USE_ERROR_CHECKING) 1862 if (!pt && USE_ERROR_CHECKING)
1884 pt->rep.string.curr = start; 1868 pt->rep.string.curr = start;
1885 pt->rep.string.past_the_end = past_the_end; 1869 pt->rep.string.past_the_end = past_the_end;
1886 return pt; 1870 return pt;
1887} 1871}
1888 1872
1889static pointer 1873ecb_cold static pointer
1890port_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)
1891{ 1875{
1892 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);
1893 1877
1894 if (!pt && USE_ERROR_CHECKING) 1878 if (!pt && USE_ERROR_CHECKING)
1897 return mk_port (SCHEME_A_ pt); 1881 return mk_port (SCHEME_A_ pt);
1898} 1882}
1899 1883
1900# define BLOCK_SIZE 256 1884# define BLOCK_SIZE 256
1901 1885
1902static port * 1886ecb_cold static port *
1903port_rep_from_scratch (SCHEME_P) 1887port_rep_from_scratch (SCHEME_P)
1904{ 1888{
1905 char *start; 1889 char *start;
1906 port *pt = malloc (sizeof (port)); 1890 port *pt = malloc (sizeof (port));
1907 1891
1921 pt->rep.string.curr = start; 1905 pt->rep.string.curr = start;
1922 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; 1906 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1;
1923 return pt; 1907 return pt;
1924} 1908}
1925 1909
1926static pointer 1910ecb_cold static pointer
1927port_from_scratch (SCHEME_P) 1911port_from_scratch (SCHEME_P)
1928{ 1912{
1929 port *pt = port_rep_from_scratch (SCHEME_A); 1913 port *pt = port_rep_from_scratch (SCHEME_A);
1930 1914
1931 if (!pt && USE_ERROR_CHECKING) 1915 if (!pt && USE_ERROR_CHECKING)
1932 return NIL; 1916 return NIL;
1933 1917
1934 return mk_port (SCHEME_A_ pt); 1918 return mk_port (SCHEME_A_ pt);
1935} 1919}
1936 1920
1937static void 1921ecb_cold static void
1938port_close (SCHEME_P_ pointer p, int flag) 1922port_close (SCHEME_P_ pointer p, int flag)
1939{ 1923{
1940 port *pt = p->object.port; 1924 port *pt = port (p);
1941 1925
1942 pt->kind &= ~flag; 1926 pt->kind &= ~flag;
1943 1927
1944 if ((pt->kind & (port_input | port_output)) == 0) 1928 if ((pt->kind & (port_input | port_output)) == 0)
1945 { 1929 {
1966/* get new character from input file */ 1950/* get new character from input file */
1967static int 1951static int
1968inchar (SCHEME_P) 1952inchar (SCHEME_P)
1969{ 1953{
1970 int c; 1954 int c;
1971 port *pt; 1955 port *pt = port (SCHEME_V->inport);
1972
1973 pt = SCHEME_V->inport->object.port;
1974 1956
1975 if (pt->kind & port_saw_EOF) 1957 if (pt->kind & port_saw_EOF)
1976 return EOF; 1958 return EOF;
1977 1959
1978 c = basic_inchar (pt); 1960 c = basic_inchar (pt);
2045 port *pt; 2027 port *pt;
2046 2028
2047 if (c == EOF) 2029 if (c == EOF)
2048 return; 2030 return;
2049 2031
2050 pt = SCHEME_V->inport->object.port; 2032 pt = port (SCHEME_V->inport);
2051 pt->unget = c; 2033 pt->unget = c;
2052#else 2034#else
2053 if (c == EOF) 2035 if (c == EOF)
2054 return; 2036 return;
2055 2037
2056 ungot = c; 2038 ungot = c;
2057#endif 2039#endif
2058} 2040}
2059 2041
2060#if USE_PORTS 2042#if USE_PORTS
2061static int 2043ecb_cold static int
2062realloc_port_string (SCHEME_P_ port *p) 2044realloc_port_string (SCHEME_P_ port *p)
2063{ 2045{
2064 char *start = p->rep.string.start; 2046 char *start = p->rep.string.start;
2065 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;
2066 char *str = malloc (new_size); 2048 char *str = malloc (new_size);
2079 else 2061 else
2080 return 0; 2062 return 0;
2081} 2063}
2082#endif 2064#endif
2083 2065
2084INTERFACE void 2066ecb_cold INTERFACE void
2085putstr (SCHEME_P_ const char *s) 2067putstr (SCHEME_P_ const char *s)
2086{ 2068{
2087#if USE_PORTS 2069#if USE_PORTS
2088 port *pt = SCHEME_V->outport->object.port; 2070 port *pt = port (SCHEME_V->outport);
2089 2071
2090 if (pt->kind & port_file) 2072 if (pt->kind & port_file)
2091 write (pt->rep.stdio.file, s, strlen (s)); 2073 write (pt->rep.stdio.file, s, strlen (s));
2092 else 2074 else
2093 for (; *s; s++) 2075 for (; *s; s++)
2095 *pt->rep.string.curr++ = *s; 2077 *pt->rep.string.curr++ = *s;
2096 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))
2097 *pt->rep.string.curr++ = *s; 2079 *pt->rep.string.curr++ = *s;
2098 2080
2099#else 2081#else
2100 xwrstr (s); 2082 write (pt->rep.stdio.file, s, strlen (s));
2101#endif 2083#endif
2102} 2084}
2103 2085
2104static void 2086ecb_cold static void
2105putchars (SCHEME_P_ const char *s, int len) 2087putchars (SCHEME_P_ const char *s, int len)
2106{ 2088{
2107#if USE_PORTS 2089#if USE_PORTS
2108 port *pt = SCHEME_V->outport->object.port; 2090 port *pt = port (SCHEME_V->outport);
2109 2091
2110 if (pt->kind & port_file) 2092 if (pt->kind & port_file)
2111 write (pt->rep.stdio.file, s, len); 2093 write (pt->rep.stdio.file, s, len);
2112 else 2094 else
2113 { 2095 {
2123#else 2105#else
2124 write (1, s, len); 2106 write (1, s, len);
2125#endif 2107#endif
2126} 2108}
2127 2109
2128INTERFACE void 2110ecb_cold INTERFACE void
2129putcharacter (SCHEME_P_ int c) 2111putcharacter (SCHEME_P_ int c)
2130{ 2112{
2131#if USE_PORTS 2113#if USE_PORTS
2132 port *pt = SCHEME_V->outport->object.port; 2114 port *pt = port (SCHEME_V->outport);
2133 2115
2134 if (pt->kind & port_file) 2116 if (pt->kind & port_file)
2135 { 2117 {
2136 char cc = c; 2118 char cc = c;
2137 write (pt->rep.stdio.file, &cc, 1); 2119 write (pt->rep.stdio.file, &cc, 1);
2149 write (1, &c, 1); 2131 write (1, &c, 1);
2150#endif 2132#endif
2151} 2133}
2152 2134
2153/* read characters up to delimiter, but cater to character constants */ 2135/* read characters up to delimiter, but cater to character constants */
2154static char * 2136ecb_cold static char *
2155readstr_upto (SCHEME_P_ int skip, const char *delim) 2137readstr_upto (SCHEME_P_ int skip, const char *delim)
2156{ 2138{
2157 char *p = SCHEME_V->strbuff + skip; 2139 char *p = SCHEME_V->strbuff + skip;
2158 2140
2159 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))));
2168 2150
2169 return SCHEME_V->strbuff; 2151 return SCHEME_V->strbuff;
2170} 2152}
2171 2153
2172/* read string expression "xxx...xxx" */ 2154/* read string expression "xxx...xxx" */
2173static pointer 2155ecb_cold static pointer
2174readstrexp (SCHEME_P_ char delim) 2156readstrexp (SCHEME_P_ char delim)
2175{ 2157{
2176 char *p = SCHEME_V->strbuff; 2158 char *p = SCHEME_V->strbuff;
2177 int c; 2159 int c;
2178 int c1 = 0; 2160 int c1 = 0;
2211 case '7': 2193 case '7':
2212 state = st_oct1; 2194 state = st_oct1;
2213 c1 = c - '0'; 2195 c1 = c - '0';
2214 break; 2196 break;
2215 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
2216 case 'x': 2206 case 'x':
2217 case 'X': 2207 case 'X':
2218 state = st_x1; 2208 state = st_x1;
2219 c1 = 0; 2209 c1 = 0;
2220 break;
2221
2222 case 'n':
2223 *p++ = '\n';
2224 state = st_ok;
2225 break;
2226
2227 case 't':
2228 *p++ = '\t';
2229 state = st_ok;
2230 break;
2231
2232 case 'r':
2233 *p++ = '\r';
2234 state = st_ok;
2235 break; 2210 break;
2236 2211
2237 default: 2212 default:
2238 *p++ = c; 2213 *p++ = c;
2239 state = st_ok; 2214 state = st_ok;
2291 } 2266 }
2292 } 2267 }
2293} 2268}
2294 2269
2295/* check c is in chars */ 2270/* check c is in chars */
2296ecb_inline int 2271ecb_cold int
2297is_one_of (const char *s, int c) 2272is_one_of (const char *s, int c)
2298{ 2273{
2299 return c == EOF || !!strchr (s, c); 2274 return c == EOF || !!strchr (s, c);
2300} 2275}
2301 2276
2302/* skip white characters */ 2277/* skip white characters */
2303ecb_inline int 2278ecb_cold int
2304skipspace (SCHEME_P) 2279skipspace (SCHEME_P)
2305{ 2280{
2306 int c, curr_line = 0; 2281 int c, curr_line = 0;
2307 2282
2308 do 2283 do
2328 backchar (SCHEME_A_ c); 2303 backchar (SCHEME_A_ c);
2329 return 1; 2304 return 1;
2330} 2305}
2331 2306
2332/* get token */ 2307/* get token */
2333static int 2308ecb_cold static int
2334token (SCHEME_P) 2309token (SCHEME_P)
2335{ 2310{
2336 int c = skipspace (SCHEME_A); 2311 int c = skipspace (SCHEME_A);
2337 2312
2338 if (c == EOF) 2313 if (c == EOF)
2436} 2411}
2437 2412
2438/* ========== Routines for Printing ========== */ 2413/* ========== Routines for Printing ========== */
2439#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) 2414#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL)
2440 2415
2441static void 2416ecb_cold static void
2442printslashstring (SCHEME_P_ char *p, int len) 2417printslashstring (SCHEME_P_ char *p, int len)
2443{ 2418{
2444 int i; 2419 int i;
2445 unsigned char *s = (unsigned char *) p; 2420 unsigned char *s = (unsigned char *) p;
2446 2421
2502 2477
2503 putcharacter (SCHEME_A_ '"'); 2478 putcharacter (SCHEME_A_ '"');
2504} 2479}
2505 2480
2506/* print atoms */ 2481/* print atoms */
2507static void 2482ecb_cold static void
2508printatom (SCHEME_P_ pointer l, int f) 2483printatom (SCHEME_P_ pointer l, int f)
2509{ 2484{
2510 char *p; 2485 char *p;
2511 int len; 2486 int len;
2512 2487
2513 atom2str (SCHEME_A_ l, f, &p, &len); 2488 atom2str (SCHEME_A_ l, f, &p, &len);
2514 putchars (SCHEME_A_ p, len); 2489 putchars (SCHEME_A_ p, len);
2515} 2490}
2516 2491
2517/* Uses internal buffer unless string pointer is already available */ 2492/* Uses internal buffer unless string pointer is already available */
2518static void 2493ecb_cold static void
2519atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2494atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2520{ 2495{
2521 char *p; 2496 char *p;
2522 2497
2523 if (l == NIL) 2498 if (l == NIL)
2730 return car (d); 2705 return car (d);
2731 2706
2732 p = cons (car (d), cdr (d)); 2707 p = cons (car (d), cdr (d));
2733 q = p; 2708 q = p;
2734 2709
2735 while (cdr (cdr (p)) != NIL) 2710 while (cddr (p) != NIL)
2736 { 2711 {
2737 d = cons (car (p), cdr (p)); 2712 d = cons (car (p), cdr (p));
2738 2713
2739 if (cdr (cdr (p)) != NIL) 2714 if (cddr (p) != NIL)
2740 p = cdr (d); 2715 p = cdr (d);
2741 } 2716 }
2742 2717
2743 set_cdr (p, car (cdr (p))); 2718 set_cdr (p, cadr (p));
2744 return q; 2719 return q;
2745} 2720}
2746 2721
2747/* reverse list -- produce new list */ 2722/* reverse list -- produce new list */
2748static pointer 2723ecb_hot static pointer
2749reverse (SCHEME_P_ pointer a) 2724reverse (SCHEME_P_ pointer a)
2750{ 2725{
2751 /* a must be checked by gc */ 2726 /* a must be checked by gc */
2752 pointer p = NIL; 2727 pointer p = NIL;
2753 2728
2756 2731
2757 return p; 2732 return p;
2758} 2733}
2759 2734
2760/* reverse list --- in-place */ 2735/* reverse list --- in-place */
2761static pointer 2736ecb_hot static pointer
2762reverse_in_place (SCHEME_P_ pointer term, pointer list) 2737reverse_in_place (SCHEME_P_ pointer term, pointer list)
2763{ 2738{
2764 pointer result = term; 2739 pointer result = term;
2765 pointer p = list; 2740 pointer p = list;
2766 2741
2774 2749
2775 return result; 2750 return result;
2776} 2751}
2777 2752
2778/* append list -- produce new list (in reverse order) */ 2753/* append list -- produce new list (in reverse order) */
2779static pointer 2754ecb_hot static pointer
2780revappend (SCHEME_P_ pointer a, pointer b) 2755revappend (SCHEME_P_ pointer a, pointer b)
2781{ 2756{
2782 pointer result = a; 2757 pointer result = a;
2783 pointer p = b; 2758 pointer p = b;
2784 2759
2793 2768
2794 return S_F; /* signal an error */ 2769 return S_F; /* signal an error */
2795} 2770}
2796 2771
2797/* equivalence of atoms */ 2772/* equivalence of atoms */
2798int 2773ecb_hot int
2799eqv (pointer a, pointer b) 2774eqv (pointer a, pointer b)
2800{ 2775{
2801 if (is_string (a)) 2776 if (is_string (a))
2802 { 2777 {
2803 if (is_string (b)) 2778 if (is_string (b))
2897 } 2872 }
2898 else 2873 else
2899 set_car (env, immutable_cons (slot, car (env))); 2874 set_car (env, immutable_cons (slot, car (env)));
2900} 2875}
2901 2876
2902static pointer 2877ecb_hot static pointer
2903find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2878find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2904{ 2879{
2905 pointer x, y; 2880 pointer x, y;
2906 2881
2907 for (x = env; x != NIL; x = cdr (x)) 2882 for (x = env; x != NIL; x = cdr (x))
2928 return NIL; 2903 return NIL;
2929} 2904}
2930 2905
2931#else /* USE_ALIST_ENV */ 2906#else /* USE_ALIST_ENV */
2932 2907
2933ecb_inline void 2908static void
2934new_frame_in_env (SCHEME_P_ pointer old_env) 2909new_frame_in_env (SCHEME_P_ pointer old_env)
2935{ 2910{
2936 SCHEME_V->envir = immutable_cons (NIL, old_env); 2911 SCHEME_V->envir = immutable_cons (NIL, old_env);
2937 setenvironment (SCHEME_V->envir); 2912 setenvironment (SCHEME_V->envir);
2938} 2913}
2939 2914
2940ecb_inline void 2915static void
2941new_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)
2942{ 2917{
2943 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2918 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2944} 2919}
2945 2920
2946static pointer 2921ecb_hot static pointer
2947find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2922find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2948{ 2923{
2949 pointer x, y; 2924 pointer x, y;
2950 2925
2951 for (x = env; x != NIL; x = cdr (x)) 2926 for (x = env; x != NIL; x = cdr (x))
2965 return NIL; 2940 return NIL;
2966} 2941}
2967 2942
2968#endif /* USE_ALIST_ENV else */ 2943#endif /* USE_ALIST_ENV else */
2969 2944
2970ecb_inline void 2945static void
2971new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2946new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2972{ 2947{
2973 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 2948 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2974 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);
2975} 2950}
2976 2951
2977ecb_inline void 2952static void
2978set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2953set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2979{ 2954{
2980 set_cdr (slot, value); 2955 set_cdr (slot, value);
2981} 2956}
2982 2957
2983ecb_inline pointer 2958static pointer
2984slot_value_in_env (pointer slot) 2959slot_value_in_env (pointer slot)
2985{ 2960{
2986 return cdr (slot); 2961 return cdr (slot);
2987} 2962}
2988 2963
2989/* ========== Evaluation Cycle ========== */ 2964/* ========== Evaluation Cycle ========== */
2990 2965
2991static int 2966ecb_cold static int
2992xError_1 (SCHEME_P_ const char *s, pointer a) 2967xError_1 (SCHEME_P_ const char *s, pointer a)
2993{ 2968{
2994#if USE_ERROR_HOOK 2969#if USE_ERROR_HOOK
2995 pointer x; 2970 pointer x;
2996 pointer hdl = SCHEME_V->ERROR_HOOK; 2971 pointer hdl = SCHEME_V->ERROR_HOOK;
3072 pointer code; 3047 pointer code;
3073}; 3048};
3074 3049
3075# define STACK_GROWTH 3 3050# define STACK_GROWTH 3
3076 3051
3077static void 3052ecb_hot static void
3078s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3053s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3079{ 3054{
3080 int nframes = (uintptr_t)SCHEME_V->dump; 3055 int nframes = (uintptr_t)SCHEME_V->dump;
3081 struct dump_stack_frame *next_frame; 3056 struct dump_stack_frame *next_frame;
3082 3057
3083 /* enough room for the next frame? */ 3058 /* enough room for the next frame? */
3084 if (nframes >= SCHEME_V->dump_size) 3059 if (ecb_expect_false (nframes >= SCHEME_V->dump_size))
3085 { 3060 {
3086 SCHEME_V->dump_size += STACK_GROWTH; 3061 SCHEME_V->dump_size += STACK_GROWTH;
3087 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);
3088 } 3063 }
3089 3064
3095 next_frame->code = code; 3070 next_frame->code = code;
3096 3071
3097 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3072 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3098} 3073}
3099 3074
3100static int 3075static ecb_hot int
3101xs_return (SCHEME_P_ pointer a) 3076xs_return (SCHEME_P_ pointer a)
3102{ 3077{
3103 int nframes = (uintptr_t)SCHEME_V->dump; 3078 int nframes = (uintptr_t)SCHEME_V->dump;
3104 struct dump_stack_frame *frame; 3079 struct dump_stack_frame *frame;
3105 3080
3116 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3091 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3117 3092
3118 return 0; 3093 return 0;
3119} 3094}
3120 3095
3121ecb_inline void 3096ecb_cold void
3122dump_stack_reset (SCHEME_P) 3097dump_stack_reset (SCHEME_P)
3123{ 3098{
3124 /* 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 */
3125 SCHEME_V->dump = (pointer)+0; 3100 SCHEME_V->dump = (pointer)+0;
3126} 3101}
3127 3102
3128ecb_inline void 3103ecb_cold void
3129dump_stack_initialize (SCHEME_P) 3104dump_stack_initialize (SCHEME_P)
3130{ 3105{
3131 SCHEME_V->dump_size = 0; 3106 SCHEME_V->dump_size = 0;
3132 SCHEME_V->dump_base = 0; 3107 SCHEME_V->dump_base = 0;
3133 dump_stack_reset (SCHEME_A); 3108 dump_stack_reset (SCHEME_A);
3134} 3109}
3135 3110
3136static void 3111ecb_cold static void
3137dump_stack_free (SCHEME_P) 3112dump_stack_free (SCHEME_P)
3138{ 3113{
3139 free (SCHEME_V->dump_base); 3114 free (SCHEME_V->dump_base);
3140 SCHEME_V->dump_base = 0; 3115 SCHEME_V->dump_base = 0;
3141 SCHEME_V->dump = (pointer)0; 3116 SCHEME_V->dump = (pointer)0;
3142 SCHEME_V->dump_size = 0; 3117 SCHEME_V->dump_size = 0;
3143} 3118}
3144 3119
3145static void 3120ecb_cold static void
3146dump_stack_mark (SCHEME_P) 3121dump_stack_mark (SCHEME_P)
3147{ 3122{
3148 int nframes = (uintptr_t)SCHEME_V->dump; 3123 int nframes = (uintptr_t)SCHEME_V->dump;
3149 int i; 3124 int i;
3150 3125
3156 mark (frame->envir); 3131 mark (frame->envir);
3157 mark (frame->code); 3132 mark (frame->code);
3158 } 3133 }
3159} 3134}
3160 3135
3161static pointer 3136ecb_cold static pointer
3162ss_get_cont (SCHEME_P) 3137ss_get_cont (SCHEME_P)
3163{ 3138{
3164 int nframes = (uintptr_t)SCHEME_V->dump; 3139 int nframes = (uintptr_t)SCHEME_V->dump;
3165 int i; 3140 int i;
3166 3141
3178 } 3153 }
3179 3154
3180 return cont; 3155 return cont;
3181} 3156}
3182 3157
3183static void 3158ecb_cold static void
3184ss_set_cont (SCHEME_P_ pointer cont) 3159ss_set_cont (SCHEME_P_ pointer cont)
3185{ 3160{
3186 int i = 0; 3161 int i = 0;
3187 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3162 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3188 3163
3200 SCHEME_V->dump = (pointer)(uintptr_t)i; 3175 SCHEME_V->dump = (pointer)(uintptr_t)i;
3201} 3176}
3202 3177
3203#else 3178#else
3204 3179
3205ecb_inline void 3180ecb_cold void
3206dump_stack_reset (SCHEME_P) 3181dump_stack_reset (SCHEME_P)
3207{ 3182{
3208 SCHEME_V->dump = NIL; 3183 SCHEME_V->dump = NIL;
3209} 3184}
3210 3185
3211ecb_inline void 3186ecb_cold void
3212dump_stack_initialize (SCHEME_P) 3187dump_stack_initialize (SCHEME_P)
3213{ 3188{
3214 dump_stack_reset (SCHEME_A); 3189 dump_stack_reset (SCHEME_A);
3215} 3190}
3216 3191
3217static void 3192ecb_cold static void
3218dump_stack_free (SCHEME_P) 3193dump_stack_free (SCHEME_P)
3219{ 3194{
3220 SCHEME_V->dump = NIL; 3195 SCHEME_V->dump = NIL;
3221} 3196}
3222 3197
3223static int 3198ecb_hot static int
3224xs_return (SCHEME_P_ pointer a) 3199xs_return (SCHEME_P_ pointer a)
3225{ 3200{
3226 pointer dump = SCHEME_V->dump; 3201 pointer dump = SCHEME_V->dump;
3227 3202
3228 SCHEME_V->value = a; 3203 SCHEME_V->value = a;
3238 SCHEME_V->dump = dump; 3213 SCHEME_V->dump = dump;
3239 3214
3240 return 0; 3215 return 0;
3241} 3216}
3242 3217
3243static void 3218ecb_hot static void
3244s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3219s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3245{ 3220{
3246 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), 3221 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op),
3247 cons (args, 3222 cons (args,
3248 cons (SCHEME_V->envir, 3223 cons (SCHEME_V->envir,
3249 cons (code, 3224 cons (code,
3250 SCHEME_V->dump)))); 3225 SCHEME_V->dump))));
3251} 3226}
3252 3227
3253static void 3228ecb_cold static void
3254dump_stack_mark (SCHEME_P) 3229dump_stack_mark (SCHEME_P)
3255{ 3230{
3256 mark (SCHEME_V->dump); 3231 mark (SCHEME_V->dump);
3257} 3232}
3258 3233
3259static pointer 3234ecb_cold static pointer
3260ss_get_cont (SCHEME_P) 3235ss_get_cont (SCHEME_P)
3261{ 3236{
3262 return SCHEME_V->dump; 3237 return SCHEME_V->dump;
3263} 3238}
3264 3239
3265static void 3240ecb_cold static void
3266ss_set_cont (SCHEME_P_ pointer cont) 3241ss_set_cont (SCHEME_P_ pointer cont)
3267{ 3242{
3268 SCHEME_V->dump = cont; 3243 SCHEME_V->dump = cont;
3269} 3244}
3270 3245
3328 break; 3303 break;
3329 } 3304 }
3330} 3305}
3331#endif 3306#endif
3332 3307
3333static int 3308/* syntax, eval, core, ... */
3309ecb_hot static int
3334opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3310opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3335{ 3311{
3336 pointer args = SCHEME_V->args; 3312 pointer args = SCHEME_V->args;
3337 pointer x, y; 3313 pointer x, y;
3338 3314
3345 s_return (S_T); 3321 s_return (S_T);
3346#endif 3322#endif
3347 case OP_LOAD: /* load */ 3323 case OP_LOAD: /* load */
3348 if (file_interactive (SCHEME_A)) 3324 if (file_interactive (SCHEME_A))
3349 { 3325 {
3350 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); 3326 putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n");
3351 //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)));
3352 } 3328 }
3353 3329
3354 if (!file_push (SCHEME_A_ strvalue (car (args)))) 3330 if (!file_push (SCHEME_A_ strvalue (car (args))))
3355 Error_1 ("unable to open", car (args)); 3331 Error_1 ("unable to open", car (args));
3356 else 3332 else
3360 } 3336 }
3361 3337
3362 case OP_T0LVL: /* top level */ 3338 case OP_T0LVL: /* top level */
3363 3339
3364 /* If we reached the end of file, this loop is done. */ 3340 /* If we reached the end of file, this loop is done. */
3365 if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) 3341 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3366 { 3342 {
3367 if (SCHEME_V->file_i == 0) 3343 if (SCHEME_V->file_i == 0)
3368 { 3344 {
3369 SCHEME_V->args = NIL; 3345 SCHEME_V->args = NIL;
3370 s_goto (OP_QUIT); 3346 s_goto (OP_QUIT);
3448#endif 3424#endif
3449 if (is_symbol (SCHEME_V->code)) /* symbol */ 3425 if (is_symbol (SCHEME_V->code)) /* symbol */
3450 { 3426 {
3451 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);
3452 3428
3453 if (x != NIL) 3429 if (x == NIL)
3454 s_return (slot_value_in_env (x));
3455 else
3456 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));
3457 } 3433 }
3458 else if (is_pair (SCHEME_V->code)) 3434 else if (is_pair (SCHEME_V->code))
3459 { 3435 {
3460 x = car (SCHEME_V->code); 3436 x = car (SCHEME_V->code);
3461 3437
3538 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3514 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3539 else if (is_foreign (SCHEME_V->code)) 3515 else if (is_foreign (SCHEME_V->code))
3540 { 3516 {
3541 /* Keep nested calls from GC'ing the arglist */ 3517 /* Keep nested calls from GC'ing the arglist */
3542 push_recent_alloc (SCHEME_A_ args, NIL); 3518 push_recent_alloc (SCHEME_A_ args, NIL);
3543 x = SCHEME_V->code->object.ff (SCHEME_A_ args); 3519 x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args);
3544 3520
3545 s_return (x); 3521 s_return (x);
3546 } 3522 }
3547 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 */
3548 { 3524 {
4024 } 4000 }
4025 4001
4026 if (USE_ERROR_CHECKING) abort (); 4002 if (USE_ERROR_CHECKING) abort ();
4027} 4003}
4028 4004
4029static int 4005/* math, cxr */
4006ecb_hot static int
4030opexe_1 (SCHEME_P_ enum scheme_opcodes op) 4007opexe_1 (SCHEME_P_ enum scheme_opcodes op)
4031{ 4008{
4032 pointer args = SCHEME_V->args; 4009 pointer args = SCHEME_V->args;
4033 pointer x = car (args); 4010 pointer x = car (args);
4034 num v; 4011 num v;
4035 4012
4036 switch (op) 4013 switch (op)
4037 { 4014 {
4038#if USE_MATH 4015#if USE_MATH
4039 case OP_INEX2EX: /* inexact->exact */ 4016 case OP_INEX2EX: /* inexact->exact */
4040 {
4041 if (is_integer (x)) 4017 if (!is_integer (x))
4042 s_return (x); 4018 {
4043
4044 RVALUE r = rvalue_unchecked (x); 4019 RVALUE r = rvalue_unchecked (x);
4045 4020
4046 if (r == (RVALUE)(IVALUE)r) 4021 if (r == (RVALUE)(IVALUE)r)
4047 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); 4022 x = mk_integer (SCHEME_A_ rvalue_unchecked (x));
4048 else 4023 else
4049 Error_1 ("inexact->exact: not integral:", x); 4024 Error_1 ("inexact->exact: not integral:", x);
4050 } 4025 }
4051 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))));
4052 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))));
4053 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))))));
4054 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))));
4055 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))));
4056 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))));
4057 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))));
4058 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))));
4059 4043
4060 case OP_ATAN: 4044 case OP_ATAN:
4045 s_return (mk_real (SCHEME_A_
4061 if (cdr (args) == NIL) 4046 cdr (args) == NIL
4062 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 4047 ? atan (rvalue (x))
4063 else 4048 : atan2 (rvalue (x), rvalue (cadr (args)))));
4064 {
4065 pointer y = cadr (args);
4066 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
4067 }
4068
4069 case OP_SQRT:
4070 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4071 4049
4072 case OP_EXPT: 4050 case OP_EXPT:
4073 { 4051 {
4074 RVALUE result; 4052 RVALUE result;
4075 int real_result = 1; 4053 int real_result = 1;
4098 if (real_result) 4076 if (real_result)
4099 s_return (mk_real (SCHEME_A_ result)); 4077 s_return (mk_real (SCHEME_A_ result));
4100 else 4078 else
4101 s_return (mk_integer (SCHEME_A_ result)); 4079 s_return (mk_integer (SCHEME_A_ result));
4102 } 4080 }
4103
4104 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4105 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4106
4107 case OP_TRUNCATE:
4108 {
4109 RVALUE n = rvalue (x);
4110 s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n)));
4111 }
4112
4113 case OP_ROUND:
4114 if (is_integer (x))
4115 s_return (x);
4116
4117 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4118#endif 4081#endif
4119 4082
4120 case OP_ADD: /* + */ 4083 case OP_ADD: /* + */
4121 v = num_zero; 4084 v = num_zero;
4122 4085
4424 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4387 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4425 4388
4426 s_return (newstr); 4389 s_return (newstr);
4427 } 4390 }
4428 4391
4429 case OP_SUBSTR: /* substring */ 4392 case OP_STRING_COPY: /* substring/string-copy */
4430 { 4393 {
4431 char *str = strvalue (x); 4394 char *str = strvalue (x);
4432 int index0 = ivalue_unchecked (cadr (args)); 4395 int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args));
4433 int index1; 4396 int index1;
4434 int len; 4397 int len;
4435 4398
4436 if (index0 > strlength (x)) 4399 if (index0 > strlength (x))
4437 Error_1 ("substring: start out of bounds:", cadr (args)); 4400 Error_1 ("string->copy: start out of bounds:", cadr (args));
4438 4401
4439 if (cddr (args) != NIL) 4402 if (cddr (args) != NIL)
4440 { 4403 {
4441 index1 = ivalue_unchecked (caddr (args)); 4404 index1 = ivalue_unchecked (caddr (args));
4442 4405
4443 if (index1 > strlength (x) || index1 < index0) 4406 if (index1 > strlength (x) || index1 < index0)
4444 Error_1 ("substring: end out of bounds:", caddr (args)); 4407 Error_1 ("string->copy: end out of bounds:", caddr (args));
4445 } 4408 }
4446 else 4409 else
4447 index1 = strlength (x); 4410 index1 = strlength (x);
4448 4411
4449 len = index1 - index0; 4412 len = index1 - index0;
4450 x = mk_empty_string (SCHEME_A_ len, ' '); 4413 x = mk_counted_string (SCHEME_A_ str + index0, len);
4451 memcpy (strvalue (x), str + index0, len);
4452 strvalue (x)[len] = 0;
4453 4414
4454 s_return (x); 4415 s_return (x);
4455 } 4416 }
4456 4417
4457 case OP_VECTOR: /* vector */ 4418 case OP_VECTOR: /* vector */
4531 } 4492 }
4532 4493
4533 if (USE_ERROR_CHECKING) abort (); 4494 if (USE_ERROR_CHECKING) abort ();
4534} 4495}
4535 4496
4536static int 4497/* relational ops */
4498ecb_hot static int
4537opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4499opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4538{ 4500{
4539 pointer x = SCHEME_V->args; 4501 pointer x = SCHEME_V->args;
4540 4502
4541 for (;;) 4503 for (;;)
4562 } 4524 }
4563 4525
4564 s_return (S_T); 4526 s_return (S_T);
4565} 4527}
4566 4528
4567static int 4529/* predicates */
4530ecb_hot static int
4568opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4531opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4569{ 4532{
4570 pointer args = SCHEME_V->args; 4533 pointer args = SCHEME_V->args;
4571 pointer a = car (args); 4534 pointer a = car (args);
4572 pointer d = cdr (args); 4535 pointer d = cdr (args);
4619 } 4582 }
4620 4583
4621 s_retbool (r); 4584 s_retbool (r);
4622} 4585}
4623 4586
4624static int 4587/* promises, list ops, ports */
4588ecb_hot static int
4625opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4589opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4626{ 4590{
4627 pointer args = SCHEME_V->args; 4591 pointer args = SCHEME_V->args;
4628 pointer a = car (args); 4592 pointer a = car (args);
4629 pointer x, y; 4593 pointer x, y;
4642 } 4606 }
4643 else 4607 else
4644 s_return (SCHEME_V->code); 4608 s_return (SCHEME_V->code);
4645 4609
4646 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4610 case OP_SAVE_FORCED: /* Save forced value replacing promise */
4647 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); 4611 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
4648 s_return (SCHEME_V->value); 4612 s_return (SCHEME_V->value);
4649 4613
4650#if USE_PORTS 4614#if USE_PORTS
4615
4616 case OP_EOF_OBJECT: /* eof-object */
4617 s_return (S_EOF);
4651 4618
4652 case OP_WRITE: /* write */ 4619 case OP_WRITE: /* write */
4653 case OP_DISPLAY: /* display */ 4620 case OP_DISPLAY: /* display */
4654 case OP_WRITE_CHAR: /* write-char */ 4621 case OP_WRITE_CHAR: /* write-char */
4655 if (is_pair (cdr (SCHEME_V->args))) 4622 if (is_pair (cdr (SCHEME_V->args)))
4797 SCHEME_V->gc_verbose = (a != S_F); 4764 SCHEME_V->gc_verbose = (a != S_F);
4798 s_retbool (was); 4765 s_retbool (was);
4799 } 4766 }
4800 4767
4801 case OP_NEWSEGMENT: /* new-segment */ 4768 case OP_NEWSEGMENT: /* new-segment */
4769#if 0
4802 if (!is_pair (args) || !is_number (a)) 4770 if (!is_pair (args) || !is_number (a))
4803 Error_0 ("new-segment: argument must be a number"); 4771 Error_0 ("new-segment: argument must be a number");
4804 4772#endif
4805 alloc_cellseg (SCHEME_A_ ivalue (a)); 4773 s_retbool (alloc_cellseg (SCHEME_A));
4806
4807 s_return (S_T);
4808 4774
4809 case OP_OBLIST: /* oblist */ 4775 case OP_OBLIST: /* oblist */
4810 s_return (oblist_all_symbols (SCHEME_A)); 4776 s_return (oblist_all_symbols (SCHEME_A));
4811 4777
4812#if USE_PORTS 4778#if USE_PORTS
4882 s_return (p == NIL ? S_F : p); 4848 s_return (p == NIL ? S_F : p);
4883 } 4849 }
4884 4850
4885 case OP_GET_OUTSTRING: /* get-output-string */ 4851 case OP_GET_OUTSTRING: /* get-output-string */
4886 { 4852 {
4887 port *p; 4853 port *p = port (a);
4888 4854
4889 if ((p = a->object.port)->kind & port_string) 4855 if (p->kind & port_string)
4890 { 4856 {
4891 off_t size; 4857 off_t size;
4892 char *str; 4858 char *str;
4893 4859
4894 size = p->rep.string.curr - p->rep.string.start + 1; 4860 size = p->rep.string.curr - p->rep.string.start + 1;
4929 } 4895 }
4930 4896
4931 if (USE_ERROR_CHECKING) abort (); 4897 if (USE_ERROR_CHECKING) abort ();
4932} 4898}
4933 4899
4934static int 4900/* reading */
4901ecb_cold static int
4935opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4902opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4936{ 4903{
4937 pointer args = SCHEME_V->args; 4904 pointer args = SCHEME_V->args;
4938 pointer x; 4905 pointer x;
4939 4906
4999 int res; 4966 int res;
5000 4967
5001 if (is_pair (args)) 4968 if (is_pair (args))
5002 p = car (args); 4969 p = car (args);
5003 4970
5004 res = p->object.port->kind & port_string; 4971 res = port (p)->kind & port_string;
5005 4972
5006 s_retbool (res); 4973 s_retbool (res);
5007 } 4974 }
5008 4975
5009 case OP_SET_INPORT: /* set-input-port */ 4976 case OP_SET_INPORT: /* set-input-port */
5281 } 5248 }
5282 5249
5283 if (USE_ERROR_CHECKING) abort (); 5250 if (USE_ERROR_CHECKING) abort ();
5284} 5251}
5285 5252
5286static int 5253/* list ops */
5254ecb_hot static int
5287opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5255opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5288{ 5256{
5289 pointer args = SCHEME_V->args; 5257 pointer args = SCHEME_V->args;
5290 pointer a = car (args); 5258 pointer a = car (args);
5291 pointer x, y; 5259 pointer x, y;
5349 5317
5350/* 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 */
5351typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); 5319typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5352 5320
5353typedef int (*test_predicate)(pointer); 5321typedef int (*test_predicate)(pointer);
5354static int 5322
5323ecb_hot static int
5355tst_any (pointer p) 5324tst_any (pointer p)
5356{ 5325{
5357 return 1; 5326 return 1;
5358} 5327}
5359 5328
5360static int 5329ecb_hot static int
5361tst_inonneg (pointer p) 5330tst_inonneg (pointer p)
5362{ 5331{
5363 return is_integer (p) && ivalue_unchecked (p) >= 0; 5332 return is_integer (p) && ivalue_unchecked (p) >= 0;
5364} 5333}
5365 5334
5366static int 5335ecb_hot static int
5367tst_is_list (SCHEME_P_ pointer p) 5336tst_is_list (SCHEME_P_ pointer p)
5368{ 5337{
5369 return p == NIL || is_pair (p); 5338 return p == NIL || is_pair (p);
5370} 5339}
5371 5340
5414#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" 5383#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5415#include "opdefines.h" 5384#include "opdefines.h"
5416#undef OP_DEF 5385#undef OP_DEF
5417; 5386;
5418 5387
5419static const char * 5388ecb_cold static const char *
5420opname (int idx) 5389opname (int idx)
5421{ 5390{
5422 const char *name = opnames; 5391 const char *name = opnames;
5423 5392
5424 /* 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? */
5426 name += strlen (name) + 1; 5395 name += strlen (name) + 1;
5427 5396
5428 return *name ? name : "ILLEGAL"; 5397 return *name ? name : "ILLEGAL";
5429} 5398}
5430 5399
5431static const char * 5400ecb_cold static const char *
5432procname (pointer x) 5401procname (pointer x)
5433{ 5402{
5434 return opname (procnum (x)); 5403 return opname (procnum (x));
5435} 5404}
5436 5405
5456#undef OP_DEF 5425#undef OP_DEF
5457 {0} 5426 {0}
5458}; 5427};
5459 5428
5460/* kernel of this interpreter */ 5429/* kernel of this interpreter */
5461static void ecb_hot 5430ecb_hot static void
5462Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5431Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5463{ 5432{
5464 SCHEME_V->op = op; 5433 SCHEME_V->op = op;
5465 5434
5466 for (;;) 5435 for (;;)
5549 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))
5550 return; 5519 return;
5551 5520
5552 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5521 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5553 { 5522 {
5554 xwrstr ("No memory!\n"); 5523 putstr (SCHEME_A_ "No memory!\n");
5555 return; 5524 return;
5556 } 5525 }
5557 } 5526 }
5558} 5527}
5559 5528
5560/* ========== Initialization of internal keywords ========== */ 5529/* ========== Initialization of internal keywords ========== */
5561 5530
5562static void 5531ecb_cold static void
5563assign_syntax (SCHEME_P_ const char *name) 5532assign_syntax (SCHEME_P_ const char *name)
5564{ 5533{
5565 pointer x = oblist_add_by_name (SCHEME_A_ name); 5534 pointer x = oblist_add_by_name (SCHEME_A_ name);
5566 set_typeflag (x, typeflag (x) | T_SYNTAX); 5535 set_typeflag (x, typeflag (x) | T_SYNTAX);
5567} 5536}
5568 5537
5569static void 5538ecb_cold static void
5570assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) 5539assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
5571{ 5540{
5572 pointer x = mk_symbol (SCHEME_A_ name); 5541 pointer x = mk_symbol (SCHEME_A_ name);
5573 pointer y = mk_proc (SCHEME_A_ op); 5542 pointer y = mk_proc (SCHEME_A_ op);
5574 new_slot_in_env (SCHEME_A_ x, y); 5543 new_slot_in_env (SCHEME_A_ x, y);
5582 ivalue_unchecked (y) = op; 5551 ivalue_unchecked (y) = op;
5583 return y; 5552 return y;
5584} 5553}
5585 5554
5586/* 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! */
5587static int 5556ecb_hot static int
5588syntaxnum (pointer p) 5557syntaxnum (pointer p)
5589{ 5558{
5590 const char *s = strvalue (p); 5559 const char *s = strvalue (p);
5591 5560
5592 switch (strlength (p)) 5561 switch (strlength (p))
5699 SCHEME_V->save_inport = NIL; 5668 SCHEME_V->save_inport = NIL;
5700 SCHEME_V->loadport = NIL; 5669 SCHEME_V->loadport = NIL;
5701 SCHEME_V->nesting = 0; 5670 SCHEME_V->nesting = 0;
5702 SCHEME_V->interactive_repl = 0; 5671 SCHEME_V->interactive_repl = 0;
5703 5672
5704 if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) 5673 if (!alloc_cellseg (SCHEME_A))
5705 { 5674 {
5706#if USE_ERROR_CHECKING 5675#if USE_ERROR_CHECKING
5707 SCHEME_V->no_memory = 1; 5676 SCHEME_V->no_memory = 1;
5708 return 0; 5677 return 0;
5709#endif 5678#endif
5777 5746
5778 return !SCHEME_V->no_memory; 5747 return !SCHEME_V->no_memory;
5779} 5748}
5780 5749
5781#if USE_PORTS 5750#if USE_PORTS
5782void 5751ecb_cold void
5783scheme_set_input_port_file (SCHEME_P_ int fin) 5752scheme_set_input_port_file (SCHEME_P_ int fin)
5784{ 5753{
5785 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); 5754 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input);
5786} 5755}
5787 5756
5788void 5757ecb_cold void
5789scheme_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)
5790{ 5759{
5791 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);
5792} 5761}
5793 5762
5794void 5763ecb_cold void
5795scheme_set_output_port_file (SCHEME_P_ int fout) 5764scheme_set_output_port_file (SCHEME_P_ int fout)
5796{ 5765{
5797 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); 5766 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output);
5798} 5767}
5799 5768
5800void 5769ecb_cold void
5801scheme_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)
5802{ 5771{
5803 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);
5804} 5773}
5805#endif 5774#endif
5806 5775
5807void 5776ecb_cold void
5808scheme_set_external_data (SCHEME_P_ void *p) 5777scheme_set_external_data (SCHEME_P_ void *p)
5809{ 5778{
5810 SCHEME_V->ext_data = p; 5779 SCHEME_V->ext_data = p;
5811} 5780}
5812 5781
5860 } 5829 }
5861 } 5830 }
5862#endif 5831#endif
5863} 5832}
5864 5833
5865void 5834ecb_cold void
5866scheme_load_file (SCHEME_P_ int fin) 5835scheme_load_file (SCHEME_P_ int fin)
5867{ 5836{
5868 scheme_load_named_file (SCHEME_A_ fin, 0); 5837 scheme_load_named_file (SCHEME_A_ fin, 0);
5869} 5838}
5870 5839
5871void 5840ecb_cold void
5872scheme_load_named_file (SCHEME_P_ int fin, const char *filename) 5841scheme_load_named_file (SCHEME_P_ int fin, const char *filename)
5873{ 5842{
5874 dump_stack_reset (SCHEME_A); 5843 dump_stack_reset (SCHEME_A);
5875 SCHEME_V->envir = SCHEME_V->global_env; 5844 SCHEME_V->envir = SCHEME_V->global_env;
5876 SCHEME_V->file_i = 0; 5845 SCHEME_V->file_i = 0;
5903 5872
5904 if (SCHEME_V->retcode == 0) 5873 if (SCHEME_V->retcode == 0)
5905 SCHEME_V->retcode = SCHEME_V->nesting != 0; 5874 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5906} 5875}
5907 5876
5908void 5877ecb_cold void
5909scheme_load_string (SCHEME_P_ const char *cmd) 5878scheme_load_string (SCHEME_P_ const char *cmd)
5910{ 5879{
5911 dump_stack_reset (SCHEME_A); 5880 dump_stack_reset (SCHEME_A);
5912 SCHEME_V->envir = SCHEME_V->global_env; 5881 SCHEME_V->envir = SCHEME_V->global_env;
5913 SCHEME_V->file_i = 0; 5882 SCHEME_V->file_i = 0;
5927 5896
5928 if (SCHEME_V->retcode == 0) 5897 if (SCHEME_V->retcode == 0)
5929 SCHEME_V->retcode = SCHEME_V->nesting != 0; 5898 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5930} 5899}
5931 5900
5932void 5901ecb_cold void
5933scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) 5902scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value)
5934{ 5903{
5935 pointer x; 5904 pointer x;
5936 5905
5937 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 5906 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
5942 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 5911 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
5943} 5912}
5944 5913
5945#if !STANDALONE 5914#if !STANDALONE
5946 5915
5947void 5916ecb_cold void
5948scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 5917scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
5949{ 5918{
5950 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));
5951} 5920}
5952 5921
5953void 5922ecb_cold void
5954scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 5923scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
5955{ 5924{
5956 int i; 5925 int i;
5957 5926
5958 for (i = 0; i < count; i++) 5927 for (i = 0; i < count; i++)
5959 scheme_register_foreign_func (SCHEME_A_ list + i); 5928 scheme_register_foreign_func (SCHEME_A_ list + i);
5960} 5929}
5961 5930
5962pointer 5931ecb_cold pointer
5963scheme_apply0 (SCHEME_P_ const char *procname) 5932scheme_apply0 (SCHEME_P_ const char *procname)
5964{ 5933{
5965 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));
5966} 5935}
5967 5936
5968void 5937ecb_cold void
5969save_from_C_call (SCHEME_P) 5938save_from_C_call (SCHEME_P)
5970{ 5939{
5971 pointer saved_data = cons (car (S_SINK), 5940 pointer saved_data = cons (car (S_SINK),
5972 cons (SCHEME_V->envir, 5941 cons (SCHEME_V->envir,
5973 SCHEME_V->dump)); 5942 SCHEME_V->dump));
5977 /* 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
5978 directly resume pre-C-call operations. */ 5947 directly resume pre-C-call operations. */
5979 dump_stack_reset (SCHEME_A); 5948 dump_stack_reset (SCHEME_A);
5980} 5949}
5981 5950
5982void 5951ecb_cold void
5983restore_from_C_call (SCHEME_P) 5952restore_from_C_call (SCHEME_P)
5984{ 5953{
5985 set_car (S_SINK, caar (SCHEME_V->c_nest)); 5954 set_car (S_SINK, caar (SCHEME_V->c_nest));
5986 SCHEME_V->envir = cadar (SCHEME_V->c_nest); 5955 SCHEME_V->envir = cadar (SCHEME_V->c_nest);
5987 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); 5956 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest));
5988 /* Pop */ 5957 /* Pop */
5989 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); 5958 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest);
5990} 5959}
5991 5960
5992/* "func" and "args" are assumed to be already eval'ed. */ 5961/* "func" and "args" are assumed to be already eval'ed. */
5993pointer 5962ecb_cold pointer
5994scheme_call (SCHEME_P_ pointer func, pointer args) 5963scheme_call (SCHEME_P_ pointer func, pointer args)
5995{ 5964{
5996 int old_repl = SCHEME_V->interactive_repl; 5965 int old_repl = SCHEME_V->interactive_repl;
5997 5966
5998 SCHEME_V->interactive_repl = 0; 5967 SCHEME_V->interactive_repl = 0;
6005 SCHEME_V->interactive_repl = old_repl; 5974 SCHEME_V->interactive_repl = old_repl;
6006 restore_from_C_call (SCHEME_A); 5975 restore_from_C_call (SCHEME_A);
6007 return SCHEME_V->value; 5976 return SCHEME_V->value;
6008} 5977}
6009 5978
6010pointer 5979ecb_cold pointer
6011scheme_eval (SCHEME_P_ pointer obj) 5980scheme_eval (SCHEME_P_ pointer obj)
6012{ 5981{
6013 int old_repl = SCHEME_V->interactive_repl; 5982 int old_repl = SCHEME_V->interactive_repl;
6014 5983
6015 SCHEME_V->interactive_repl = 0; 5984 SCHEME_V->interactive_repl = 0;
6027 5996
6028/* ========== Main ========== */ 5997/* ========== Main ========== */
6029 5998
6030#if STANDALONE 5999#if STANDALONE
6031 6000
6032int 6001ecb_cold int
6033main (int argc, char **argv) 6002main (int argc, char **argv)
6034{ 6003{
6035# if USE_MULTIPLICITY 6004# if USE_MULTIPLICITY
6036 scheme ssc; 6005 scheme ssc;
6037 scheme *const SCHEME_V = &ssc; 6006 scheme *const SCHEME_V = &ssc;
6043 int isfile = 1; 6012 int isfile = 1;
6044 system ("ps v $PPID");//D 6013 system ("ps v $PPID");//D
6045 6014
6046 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6015 if (argc == 2 && strcmp (argv[1], "-?") == 0)
6047 { 6016 {
6048 xwrstr ("Usage: tinyscheme -?\n"); 6017 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
6049 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6018 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
6050 xwrstr ("followed by\n"); 6019 putstr (SCHEME_A_ "followed by\n");
6051 xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); 6020 putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n");
6052 xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); 6021 putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n");
6053 xwrstr ("assuming that the executable is named tinyscheme.\n"); 6022 putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n");
6054 xwrstr ("Use - as filename for stdin.\n"); 6023 putstr (SCHEME_A_ "Use - as filename for stdin.\n");
6055 return 1; 6024 return 1;
6056 } 6025 }
6057 6026
6058 if (!scheme_init (SCHEME_A)) 6027 if (!scheme_init (SCHEME_A))
6059 { 6028 {
6060 xwrstr ("Could not initialize!\n"); 6029 putstr (SCHEME_A_ "Could not initialize!\n");
6061 return 2; 6030 return 2;
6062 } 6031 }
6063 6032
6064# if USE_PORTS 6033# if USE_PORTS
6065 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); 6034 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO);
6110 fin = open (file_name, O_RDONLY); 6079 fin = open (file_name, O_RDONLY);
6111#endif 6080#endif
6112 6081
6113 if (isfile && fin < 0) 6082 if (isfile && fin < 0)
6114 { 6083 {
6115 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");
6116 } 6085 }
6117 else 6086 else
6118 { 6087 {
6119 if (isfile) 6088 if (isfile)
6120 scheme_load_named_file (SCHEME_A_ fin, file_name); 6089 scheme_load_named_file (SCHEME_A_ fin, file_name);
6124#if USE_PORTS 6093#if USE_PORTS
6125 if (!isfile || fin != STDIN_FILENO) 6094 if (!isfile || fin != STDIN_FILENO)
6126 { 6095 {
6127 if (SCHEME_V->retcode != 0) 6096 if (SCHEME_V->retcode != 0)
6128 { 6097 {
6129 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");
6130 } 6099 }
6131 6100
6132 if (isfile) 6101 if (isfile)
6133 close (fin); 6102 close (fin);
6134 } 6103 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines