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.18 by root, Thu Nov 26 21:32:16 2015 UTC vs.
Revision 1.66 by root, Mon Dec 7 18:10:57 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 PAGE_SIZE 4096 /* does not work on sparc/alpha */ 21#define _POSIX_C_SOURCE 200201
22#include "malloc.c" 22#define _XOPEN_SOURCE 600
23#define _GNU_SOURCE 1 /* for malloc mremap */
23 24
24#define SCHEME_SOURCE 25#define SCHEME_SOURCE
25#include "scheme-private.h" 26#include "scheme-private.h"
26#ifndef WIN32 27#ifndef WIN32
27# include <unistd.h> 28# include <unistd.h>
28#endif 29#endif
29#if USE_MATH 30#if USE_MATH
30# include <math.h> 31# include <math.h>
31#endif 32#endif
32 33
34#define ECB_NO_THREADS 1
35#include "ecb.h"
36
33#include <sys/types.h> 37#include <sys/types.h>
34#include <sys/stat.h> 38#include <sys/stat.h>
35#include <fcntl.h> 39#include <fcntl.h>
36 40
41#if !USE_ERROR_CHECKING
42# define NDEBUG
43#endif
44
45#include <assert.h>
46#include <stdlib.h>
37#include <string.h> 47#include <string.h>
38#include <stdlib.h>
39 48
40#include <limits.h> 49#include <limits.h>
41#include <inttypes.h> 50#include <inttypes.h>
42#include <float.h> 51#include <float.h>
43//#include <ctype.h> 52
53#if !USE_SYSTEM_MALLOC
54# define PAGE_SIZE 4096 /* does not work on sparc/alpha */
55# include "malloc.c"
56# define malloc(n) tiny_malloc (n)
57# define realloc(p,n) tiny_realloc (p, n)
58# define free(p) tiny_free (p)
59#endif
60
61#if '1' != '0' + 1 \
62 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \
63 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \
64 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \
65 || 'f' != 'a' + 5
66# error "execution character set digits not consecutive"
67#endif
44 68
45enum { 69enum {
46 TOK_EOF, 70 TOK_EOF,
47 TOK_LPAREN, 71 TOK_LPAREN,
48 TOK_RPAREN, 72 TOK_RPAREN,
49 TOK_DOT, 73 TOK_DOT,
50 TOK_ATOM, 74 TOK_ATOM,
75 TOK_DOTATOM, /* atom name starting with '.' */
76 TOK_STRATOM, /* atom name enclosed in | */
51 TOK_QUOTE, 77 TOK_QUOTE,
52 TOK_DQUOTE, 78 TOK_DQUOTE,
53 TOK_BQUOTE, 79 TOK_BQUOTE,
54 TOK_COMMA, 80 TOK_COMMA,
55 TOK_ATMARK, 81 TOK_ATMARK,
57 TOK_SHARP_CONST, 83 TOK_SHARP_CONST,
58 TOK_VEC 84 TOK_VEC
59}; 85};
60 86
61#define BACKQUOTE '`' 87#define BACKQUOTE '`'
62#define DELIMITERS "()\";\f\t\v\n\r " 88#define WHITESPACE " \t\r\n\v\f"
89#define DELIMITERS "()\";" WHITESPACE
63 90
64#define NIL (&SCHEME_V->xNIL) //TODO: make this 0? 91#define NIL POINTER (&SCHEME_V->xNIL)
65#define S_T (&SCHEME_V->xT) //TODO: magic ptr value? 92#define S_T POINTER (&SCHEME_V->xT)
66#define S_F (&SCHEME_V->xF) //TODO: magic ptr value? 93#define S_F POINTER (&SCHEME_V->xF)
67#define S_SINK (&SCHEME_V->xsink) 94#define S_SINK POINTER (&SCHEME_V->xsink)
68#define S_EOF (&SCHEME_V->xEOF_OBJ) 95#define S_EOF POINTER (&SCHEME_V->xEOF_OBJ)
69
70/* should use libecb */
71#if __GNUC__ >= 4
72# define ecb_expect(expr,value) __builtin_expect ((expr),(value))
73# define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
74# define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
75#else
76# define ecb_expect_false(expr) !!(expr)
77# define ecb_expect_true(expr) !!(expr)
78#endif
79 96
80#if !USE_MULTIPLICITY 97#if !USE_MULTIPLICITY
81static scheme sc; 98static scheme sc;
82#endif 99#endif
83 100
84static void 101ecb_cold static void
85xbase (char *s, long n, int base) 102xbase (char *s, long n, int base)
86{ 103{
87 if (n < 0) 104 if (n < 0)
88 { 105 {
89 *s++ = '-'; 106 *s++ = '-';
91 } 108 }
92 109
93 char *p = s; 110 char *p = s;
94 111
95 do { 112 do {
96 *p++ = '0' + n % base; 113 *p++ = "0123456789abcdef"[n % base];
97 n /= base; 114 n /= base;
98 } while (n); 115 } while (n);
99 116
100 *p-- = 0; 117 *p-- = 0;
101 118
104 char x = *s; *s = *p; *p = x; 121 char x = *s; *s = *p; *p = x;
105 --p; ++s; 122 --p; ++s;
106 } 123 }
107} 124}
108 125
109static void 126ecb_cold static void
110xnum (char *s, long n) 127xnum (char *s, long n)
111{ 128{
112 xbase (s, n, 10); 129 xbase (s, n, 10);
113} 130}
114 131
115static void 132ecb_cold static void
116xwrstr (const char *s) 133putnum (SCHEME_P_ long n)
117{
118 write (1, s, strlen (s));
119}
120
121static void
122xwrnum (long n)
123{ 134{
124 char buf[64]; 135 char buf[64];
125 136
126 xnum (buf, n); 137 xnum (buf, n);
127 xwrstr (buf); 138 putstr (SCHEME_A_ buf);
128} 139}
140
141#if USE_CHAR_CLASSIFIERS
142#include <ctype.h>
143#else
129 144
130static char 145static char
131xtoupper (char c) 146xtoupper (char c)
132{ 147{
133 if (c >= 'a' && c <= 'z') 148 if (c >= 'a' && c <= 'z')
153 168
154#define toupper(c) xtoupper (c) 169#define toupper(c) xtoupper (c)
155#define tolower(c) xtolower (c) 170#define tolower(c) xtolower (c)
156#define isdigit(c) xisdigit (c) 171#define isdigit(c) xisdigit (c)
157 172
158#if USE_STRLWR 173#endif
174
175#if USE_IGNORECASE
159static const char * 176ecb_cold static const char *
160strlwr (char *s) 177xstrlwr (char *s)
161{ 178{
162 const char *p = s; 179 const char *p = s;
163 180
164 while (*s) 181 while (*s)
165 { 182 {
167 s++; 184 s++;
168 } 185 }
169 186
170 return p; 187 return p;
171} 188}
172#endif
173 189
190#define stricmp(a,b) strcasecmp (a, b)
191#define strlwr(s) xstrlwr (s)
192
193#else
174#define stricmp(a,b) strcmp (a, b) 194# define stricmp(a,b) strcmp (a, b)
175#define strlwr(s) (s) 195# define strlwr(s) (s)
196#endif
176 197
177#ifndef prompt 198#ifndef prompt
178# define prompt "ts> " 199# define prompt "ms> "
179#endif 200#endif
180 201
181#ifndef InitFile 202#ifndef InitFile
182# define InitFile "init.scm" 203# define InitFile "init.scm"
183#endif 204#endif
184 205
185#ifndef FIRST_CELLSEGS
186# define FIRST_CELLSEGS 3
187#endif
188
189enum scheme_types 206enum scheme_types
190{ 207{
208 T_INTEGER,
209 T_CHARACTER,
191 T_FREE, 210 T_REAL,
192 T_STRING, 211 T_STRING,
193 T_NUMBER,
194 T_SYMBOL, 212 T_SYMBOL,
195 T_PROC, 213 T_PROC,
196 T_PAIR, 214 T_PAIR, /* also used for free cells */
197 T_CLOSURE, 215 T_CLOSURE,
216 T_BYTECODE, // temp
217 T_MACRO,
198 T_CONTINUATION, 218 T_CONTINUATION,
199 T_FOREIGN, 219 T_FOREIGN,
200 T_CHARACTER,
201 T_PORT, 220 T_PORT,
202 T_VECTOR, 221 T_VECTOR,
203 T_MACRO,
204 T_PROMISE, 222 T_PROMISE,
205 T_ENVIRONMENT, 223 T_ENVIRONMENT,
206 /* one more... */ 224 T_SPECIAL, // #t, #f, '(), eof-object
225
207 T_NUM_SYSTEM_TYPES 226 T_NUM_SYSTEM_TYPES
208}; 227};
209 228
210#define T_MASKTYPE 0x000f 229#define T_MASKTYPE 0x001f
211#define T_SYNTAX 0x0010 230#define T_SYNTAX 0x0020
212#define T_IMMUTABLE 0x0020 231#define T_IMMUTABLE 0x0040
213#define T_ATOM 0x0040 /* only for gc */ 232#define T_ATOM 0x0080 /* only for gc */
214#define T_MARK 0x0080 /* only for gc */ 233//#define T_MARK 0x0080 /* only for gc */
234
235/* num, for generic arithmetic */
236struct num
237{
238 IVALUE ivalue;
239#if USE_REAL
240 RVALUE rvalue;
241 char is_fixnum;
242#endif
243};
244
245#if USE_REAL
246# define num_is_fixnum(n) (n).is_fixnum
247# define num_set_fixnum(n,f) (n).is_fixnum = (f)
248# define num_ivalue(n) (n).ivalue
249# define num_rvalue(n) (n).rvalue
250# define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i)
251# define num_set_rvalue(n,r) (n).rvalue = (r)
252#else
253# define num_is_fixnum(n) 1
254# define num_set_fixnum(n,f) 0
255# define num_ivalue(n) (n).ivalue
256# define num_rvalue(n) (n).ivalue
257# define num_set_ivalue(n,i) (n).ivalue = (i)
258# define num_set_rvalue(n,r) (n).ivalue = (r)
259#endif
215 260
216enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; 261enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
217 262
218static num num_op (enum num_op op, num a, num b); 263static num num_op (enum num_op op, num a, num b);
219static num num_intdiv (num a, num b); 264static num num_intdiv (num a, num b);
220static num num_rem (num a, num b); 265static num num_rem (num a, num b);
221static num num_mod (num a, num b); 266static num num_mod (num a, num b);
222 267
223#if USE_MATH
224static double round_per_R5RS (double x);
225#endif
226static int is_zero_rvalue (RVALUE x); 268static int is_zero_rvalue (RVALUE x);
227
228static INLINE int
229num_is_integer (pointer p)
230{
231 return num_is_fixnum (p->object.number);
232}
233 269
234static num num_zero; 270static num num_zero;
235static num num_one; 271static num num_one;
236 272
273/* convert "pointer" to cell* / cell* to pointer */
274#define CELL(p) ((struct cell *)(p) + 0)
275#define POINTER(c) ((void *)((c) - 0))
276
237/* macros for cell operations */ 277/* macros for cell operations */
238#define typeflag(p) ((p)->flag + 0) 278#define typeflag(p) (CELL(p)->flag + 0)
239#define set_typeflag(p,v) ((p)->flag = (v)) 279#define set_typeflag(p,v) (CELL(p)->flag = (v))
240#define type(p) (typeflag (p) & T_MASKTYPE) 280#define type(p) (typeflag (p) & T_MASKTYPE)
241 281
242INTERFACE INLINE int 282INTERFACE int
243is_string (pointer p) 283is_string (pointer p)
244{ 284{
245 return type (p) == T_STRING; 285 return type (p) == T_STRING;
246} 286}
247 287
248#define strvalue(p) ((p)->object.string.svalue) 288#define strvalue(p) (CELL(p)->object.string.svalue)
249#define strlength(p) ((p)->object.string.length) 289#define strlength(p) (CELL(p)->object.string.length)
250 290
251INTERFACE int is_list (SCHEME_P_ pointer p);
252INTERFACE INLINE int 291INTERFACE int
253is_vector (pointer p) 292is_vector (pointer p)
254{ 293{
255 return type (p) == T_VECTOR; 294 return type (p) == T_VECTOR;
256} 295}
257 296
258#define vecvalue(p) ((p)->object.vector.vvalue) 297#define vecvalue(p) (CELL(p)->object.vector.vvalue)
259#define veclength(p) ((p)->object.vector.length) 298#define veclength(p) (CELL(p)->object.vector.length)
260INTERFACE void fill_vector (pointer vec, pointer obj); 299INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
261INTERFACE uint32_t vector_length (pointer vec);
262INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); 300INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
263INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); 301INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
264 302
265INTERFACE uint32_t 303INTERFACE int
266vector_length (pointer vec) 304is_integer (pointer p)
267{ 305{
268 return vec->object.vector.length; 306 return type (p) == T_INTEGER;
269} 307}
270 308
309/* not the same as in scheme, where integers are (correctly :) reals */
271INTERFACE INLINE int 310INTERFACE int
311is_real (pointer p)
312{
313 return type (p) == T_REAL;
314}
315
316INTERFACE int
272is_number (pointer p) 317is_number (pointer p)
273{ 318{
274 return type (p) == T_NUMBER; 319 return is_integer (p) || is_real (p);
275} 320}
276 321
277INTERFACE INLINE int 322INTERFACE int
278is_integer (pointer p)
279{
280 if (!is_number (p))
281 return 0;
282
283 if (num_is_integer (p) || ivalue (p) == rvalue (p))
284 return 1;
285
286 return 0;
287}
288
289INTERFACE INLINE int
290is_real (pointer p)
291{
292 return is_number (p) && !num_is_fixnum (p->object.number);
293}
294
295INTERFACE INLINE int
296is_character (pointer p) 323is_character (pointer p)
297{ 324{
298 return type (p) == T_CHARACTER; 325 return type (p) == T_CHARACTER;
299} 326}
300 327
301INTERFACE INLINE char * 328INTERFACE char *
302string_value (pointer p) 329string_value (pointer p)
303{ 330{
304 return strvalue (p); 331 return strvalue (p);
305} 332}
306 333
307INLINE num
308nvalue (pointer p)
309{
310 return (p)->object.number;
311}
312
313static IVALUE
314num_get_ivalue (const num n)
315{
316 return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n);
317}
318
319static RVALUE
320num_get_rvalue (const num n)
321{
322 return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n);
323}
324
325INTERFACE IVALUE
326ivalue (pointer p)
327{
328 return num_get_ivalue (p->object.number);
329}
330
331INTERFACE RVALUE
332rvalue (pointer p)
333{
334 return num_get_rvalue (p->object.number);
335}
336
337#define ivalue_unchecked(p) ((p)->object.number.value.ivalue) 334#define ivalue_unchecked(p) CELL(p)->object.ivalue
335#define set_ivalue(p,v) CELL(p)->object.ivalue = (v)
336
338#if USE_REAL 337#if USE_REAL
339# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 338#define rvalue_unchecked(p) CELL(p)->object.rvalue
340# define set_num_integer(p) (p)->object.number.is_fixnum=1; 339#define set_rvalue(p,v) CELL(p)->object.rvalue = (v)
341# define set_num_real(p) (p)->object.number.is_fixnum=0;
342#else 340#else
343# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 341#define rvalue_unchecked(p) CELL(p)->object.ivalue
344# define set_num_integer(p) 0 342#define set_rvalue(p,v) CELL(p)->object.ivalue = (v)
345# define set_num_real(p) 0
346#endif 343#endif
344
347INTERFACE long 345INTERFACE long
348charvalue (pointer p) 346charvalue (pointer p)
349{ 347{
350 return ivalue_unchecked (p); 348 return ivalue_unchecked (p);
351} 349}
352 350
351#define port(p) CELL(p)->object.port
352#define set_port(p,v) port(p) = (v)
353INTERFACE INLINE int 353INTERFACE int
354is_port (pointer p) 354is_port (pointer p)
355{ 355{
356 return type (p) == T_PORT; 356 return type (p) == T_PORT;
357} 357}
358 358
359INTERFACE INLINE int 359INTERFACE int
360is_inport (pointer p) 360is_inport (pointer p)
361{ 361{
362 return is_port (p) && p->object.port->kind & port_input; 362 return is_port (p) && port (p)->kind & port_input;
363} 363}
364 364
365INTERFACE INLINE int 365INTERFACE int
366is_outport (pointer p) 366is_outport (pointer p)
367{ 367{
368 return is_port (p) && p->object.port->kind & port_output; 368 return is_port (p) && port (p)->kind & port_output;
369} 369}
370 370
371INTERFACE INLINE int 371INTERFACE int
372is_pair (pointer p) 372is_pair (pointer p)
373{ 373{
374 return type (p) == T_PAIR; 374 return type (p) == T_PAIR;
375} 375}
376 376
377#define car(p) ((p)->object.cons.car + 0) 377#define car(p) (POINTER (CELL(p)->object.cons.car))
378#define cdr(p) ((p)->object.cons.cdr + 0) 378#define cdr(p) (POINTER (CELL(p)->object.cons.cdr))
379 379
380static pointer caar (pointer p) { return car (car (p)); } 380static pointer caar (pointer p) { return car (car (p)); }
381static pointer cadr (pointer p) { return car (cdr (p)); } 381static pointer cadr (pointer p) { return car (cdr (p)); }
382static pointer cdar (pointer p) { return cdr (car (p)); } 382static pointer cdar (pointer p) { return cdr (car (p)); }
383static pointer cddr (pointer p) { return cdr (cdr (p)); } 383static pointer cddr (pointer p) { return cdr (cdr (p)); }
384 384
385static pointer cadar (pointer p) { return car (cdr (car (p))); } 385static pointer cadar (pointer p) { return car (cdr (car (p))); }
386static pointer caddr (pointer p) { return car (cdr (cdr (p))); } 386static pointer caddr (pointer p) { return car (cdr (cdr (p))); }
387static pointer cdaar (pointer p) { return cdr (car (car (p))); } 387static pointer cdaar (pointer p) { return cdr (car (car (p))); }
388 388
389static pointer cadddr (pointer p) { return car (car (car (cdr (p)))); }
390
389INTERFACE void 391INTERFACE void
390set_car (pointer p, pointer q) 392set_car (pointer p, pointer q)
391{ 393{
392 p->object.cons.car = q; 394 CELL(p)->object.cons.car = CELL (q);
393} 395}
394 396
395INTERFACE void 397INTERFACE void
396set_cdr (pointer p, pointer q) 398set_cdr (pointer p, pointer q)
397{ 399{
398 p->object.cons.cdr = q; 400 CELL(p)->object.cons.cdr = CELL (q);
399} 401}
400 402
401INTERFACE pointer 403INTERFACE pointer
402pair_car (pointer p) 404pair_car (pointer p)
403{ 405{
408pair_cdr (pointer p) 410pair_cdr (pointer p)
409{ 411{
410 return cdr (p); 412 return cdr (p);
411} 413}
412 414
413INTERFACE INLINE int 415INTERFACE int
414is_symbol (pointer p) 416is_symbol (pointer p)
415{ 417{
416 return type (p) == T_SYMBOL; 418 return type (p) == T_SYMBOL;
417} 419}
418 420
419INTERFACE INLINE char * 421INTERFACE char *
420symname (pointer p) 422symname (pointer p)
421{ 423{
422 return strvalue (car (p)); 424 return strvalue (p);
423} 425}
424 426
425#if USE_PLIST 427#if USE_PLIST
428#error plists are broken because symbols are no longer pairs
429#define symprop(p) cdr(p)
426SCHEME_EXPORT INLINE int 430SCHEME_EXPORT int
427hasprop (pointer p) 431hasprop (pointer p)
428{ 432{
429 return typeflag (p) & T_SYMBOL; 433 return typeflag (p) & T_SYMBOL;
430} 434}
431
432# define symprop(p) cdr(p)
433#endif 435#endif
434 436
435INTERFACE INLINE int 437INTERFACE int
436is_syntax (pointer p) 438is_syntax (pointer p)
437{ 439{
438 return typeflag (p) & T_SYNTAX; 440 return typeflag (p) & T_SYNTAX;
439} 441}
440 442
441INTERFACE INLINE int 443INTERFACE int
442is_proc (pointer p) 444is_proc (pointer p)
443{ 445{
444 return type (p) == T_PROC; 446 return type (p) == T_PROC;
445} 447}
446 448
447INTERFACE INLINE int 449INTERFACE int
448is_foreign (pointer p) 450is_foreign (pointer p)
449{ 451{
450 return type (p) == T_FOREIGN; 452 return type (p) == T_FOREIGN;
451} 453}
452 454
453INTERFACE INLINE char * 455INTERFACE char *
454syntaxname (pointer p) 456syntaxname (pointer p)
455{ 457{
456 return strvalue (car (p)); 458 return strvalue (p);
457} 459}
458 460
459#define procnum(p) ivalue (p) 461#define procnum(p) ivalue_unchecked (p)
460static const char *procname (pointer x); 462static const char *procname (pointer x);
461 463
462INTERFACE INLINE int 464INTERFACE int
463is_closure (pointer p) 465is_closure (pointer p)
464{ 466{
465 return type (p) == T_CLOSURE; 467 return type (p) == T_CLOSURE;
466} 468}
467 469
468INTERFACE INLINE int 470INTERFACE int
469is_macro (pointer p) 471is_macro (pointer p)
470{ 472{
471 return type (p) == T_MACRO; 473 return type (p) == T_MACRO;
472} 474}
473 475
474INTERFACE INLINE pointer 476INTERFACE pointer
475closure_code (pointer p) 477closure_code (pointer p)
476{ 478{
477 return car (p); 479 return car (p);
478} 480}
479 481
480INTERFACE INLINE pointer 482INTERFACE pointer
481closure_env (pointer p) 483closure_env (pointer p)
482{ 484{
483 return cdr (p); 485 return cdr (p);
484} 486}
485 487
486INTERFACE INLINE int 488INTERFACE int
487is_continuation (pointer p) 489is_continuation (pointer p)
488{ 490{
489 return type (p) == T_CONTINUATION; 491 return type (p) == T_CONTINUATION;
490} 492}
491 493
492#define cont_dump(p) cdr (p) 494#define cont_dump(p) cdr (p)
493#define set_cont_dump(p,v) set_cdr ((p), (v)) 495#define set_cont_dump(p,v) set_cdr ((p), (v))
494 496
495/* To do: promise should be forced ONCE only */ 497/* To do: promise should be forced ONCE only */
496INTERFACE INLINE int 498INTERFACE int
497is_promise (pointer p) 499is_promise (pointer p)
498{ 500{
499 return type (p) == T_PROMISE; 501 return type (p) == T_PROMISE;
500} 502}
501 503
502INTERFACE INLINE int 504INTERFACE int
503is_environment (pointer p) 505is_environment (pointer p)
504{ 506{
505 return type (p) == T_ENVIRONMENT; 507 return type (p) == T_ENVIRONMENT;
506} 508}
507 509
509 511
510#define is_atom(p) (typeflag (p) & T_ATOM) 512#define is_atom(p) (typeflag (p) & T_ATOM)
511#define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) 513#define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM)
512#define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM) 514#define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM)
513 515
516#if 1
517#define is_mark(p) (CELL(p)->mark)
518#define setmark(p) (CELL(p)->mark = 1)
519#define clrmark(p) (CELL(p)->mark = 0)
520#else
514#define is_mark(p) (typeflag (p) & T_MARK) 521#define is_mark(p) (typeflag (p) & T_MARK)
515#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) 522#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
516#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) 523#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
524#endif
517 525
518INTERFACE INLINE int 526INTERFACE int
519is_immutable (pointer p) 527is_immutable (pointer p)
520{ 528{
521 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; 529 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
522} 530}
523 531
524INTERFACE INLINE void 532INTERFACE void
525setimmutable (pointer p) 533setimmutable (pointer p)
526{ 534{
527#if USE_ERROR_CHECKING 535#if USE_ERROR_CHECKING
528 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 536 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
529#endif 537#endif
530} 538}
531 539
540/* Result is:
541 proper list: length
542 circular list: -1
543 not even a pair: -2
544 dotted list: -2 minus length before dot
545*/
546ecb_hot INTERFACE int
547list_length (SCHEME_P_ pointer a)
548{
549 int i = 0;
550 pointer slow, fast;
551
552 slow = fast = a;
553
554 while (1)
555 {
556 if (fast == NIL)
557 return i;
558
559 if (!is_pair (fast))
560 return -2 - i;
561
562 fast = cdr (fast);
563 ++i;
564
565 if (fast == NIL)
566 return i;
567
568 if (!is_pair (fast))
569 return -2 - i;
570
571 ++i;
572 fast = cdr (fast);
573
574 /* Safe because we would have already returned if `fast'
575 encountered a non-pair. */
576 slow = cdr (slow);
577
578 if (fast == slow)
579 {
580 /* the fast pointer has looped back around and caught up
581 with the slow pointer, hence the structure is circular,
582 not of finite length, and therefore not a list */
583 return -1;
584 }
585 }
586}
587
588INTERFACE int
589is_list (SCHEME_P_ pointer a)
590{
591 return list_length (SCHEME_A_ a) >= 0;
592}
593
532#if USE_CHAR_CLASSIFIERS 594#if USE_CHAR_CLASSIFIERS
533static INLINE int 595
596ecb_inline int
534Cisalpha (int c) 597Cisalpha (int c)
535{ 598{
536 return isascii (c) && isalpha (c); 599 return isascii (c) && isalpha (c);
537} 600}
538 601
539static INLINE int 602ecb_inline int
540Cisdigit (int c) 603Cisdigit (int c)
541{ 604{
542 return isascii (c) && isdigit (c); 605 return isascii (c) && isdigit (c);
543} 606}
544 607
545static INLINE int 608ecb_inline int
546Cisspace (int c) 609Cisspace (int c)
547{ 610{
548 return isascii (c) && isspace (c); 611 return isascii (c) && isspace (c);
549} 612}
550 613
551static INLINE int 614ecb_inline int
552Cisupper (int c) 615Cisupper (int c)
553{ 616{
554 return isascii (c) && isupper (c); 617 return isascii (c) && isupper (c);
555} 618}
556 619
557static INLINE int 620ecb_inline int
558Cislower (int c) 621Cislower (int c)
559{ 622{
560 return isascii (c) && islower (c); 623 return isascii (c) && islower (c);
561} 624}
562#endif 625#endif
595 "gs", 658 "gs",
596 "rs", 659 "rs",
597 "us" 660 "us"
598}; 661};
599 662
600static int 663ecb_cold static int
601is_ascii_name (const char *name, int *pc) 664is_ascii_name (const char *name, int *pc)
602{ 665{
603 int i; 666 int i;
604 667
605 for (i = 0; i < 32; i++) 668 for (i = 0; i < 32; i++)
623#endif 686#endif
624 687
625static int file_push (SCHEME_P_ const char *fname); 688static int file_push (SCHEME_P_ const char *fname);
626static void file_pop (SCHEME_P); 689static void file_pop (SCHEME_P);
627static int file_interactive (SCHEME_P); 690static int file_interactive (SCHEME_P);
628static INLINE int is_one_of (char *s, int c); 691ecb_inline int is_one_of (const char *s, int c);
629static int alloc_cellseg (SCHEME_P_ int n); 692static int alloc_cellseg (SCHEME_P);
630static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); 693ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
631static void finalize_cell (SCHEME_P_ pointer a); 694static void finalize_cell (SCHEME_P_ pointer a);
632static int count_consecutive_cells (pointer x, int needed);
633static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 695static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
634static pointer mk_number (SCHEME_P_ const num n); 696static pointer mk_number (SCHEME_P_ const num n);
635static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); 697static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
636static pointer mk_vector (SCHEME_P_ uint32_t len); 698static pointer mk_vector (SCHEME_P_ uint32_t len);
637static pointer mk_atom (SCHEME_P_ char *q); 699static pointer mk_atom (SCHEME_P_ char *q);
638static pointer mk_sharp_const (SCHEME_P_ char *name); 700static pointer mk_sharp_const (SCHEME_P_ char *name);
639 701
702static pointer mk_port (SCHEME_P_ port *p);
703
640#if USE_PORTS 704#if USE_PORTS
641static pointer mk_port (SCHEME_P_ port *p);
642static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); 705static pointer port_from_filename (SCHEME_P_ const char *fn, int prop);
643static pointer port_from_file (SCHEME_P_ int, int prop); 706static pointer port_from_file (SCHEME_P_ int, int prop);
644static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); 707static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop);
645static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); 708static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop);
646static port *port_rep_from_file (SCHEME_P_ int, int prop); 709static port *port_rep_from_file (SCHEME_P_ int, int prop);
647static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); 710static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop);
648static void port_close (SCHEME_P_ pointer p, int flag); 711static void port_close (SCHEME_P_ pointer p, int flag);
649#endif 712#endif
713
650static void mark (pointer a); 714static void mark (pointer a);
651static void gc (SCHEME_P_ pointer a, pointer b); 715static void gc (SCHEME_P_ pointer a, pointer b);
652static int basic_inchar (port *pt); 716static int basic_inchar (port *pt);
653static int inchar (SCHEME_P); 717static int inchar (SCHEME_P);
654static void backchar (SCHEME_P_ int c); 718static void backchar (SCHEME_P_ int c);
655static char *readstr_upto (SCHEME_P_ char *delim); 719static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
656static pointer readstrexp (SCHEME_P); 720static pointer readstrexp (SCHEME_P_ char delim);
657static INLINE int skipspace (SCHEME_P); 721static int skipspace (SCHEME_P);
658static int token (SCHEME_P); 722static int token (SCHEME_P);
659static void printslashstring (SCHEME_P_ char *s, int len); 723static void printslashstring (SCHEME_P_ char *s, int len);
660static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 724static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
661static void printatom (SCHEME_P_ pointer l, int f); 725static void printatom (SCHEME_P_ pointer l, int f);
662static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 726static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
666static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); 730static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list);
667static pointer revappend (SCHEME_P_ pointer a, pointer b); 731static pointer revappend (SCHEME_P_ pointer a, pointer b);
668static pointer ss_get_cont (SCHEME_P); 732static pointer ss_get_cont (SCHEME_P);
669static void ss_set_cont (SCHEME_P_ pointer cont); 733static void ss_set_cont (SCHEME_P_ pointer cont);
670static void dump_stack_mark (SCHEME_P); 734static void dump_stack_mark (SCHEME_P);
671static pointer opexe_0 (SCHEME_P_ enum scheme_opcodes op); 735static int opexe_0 (SCHEME_P_ enum scheme_opcodes op);
736static int opexe_1 (SCHEME_P_ enum scheme_opcodes op);
672static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); 737static int opexe_2 (SCHEME_P_ enum scheme_opcodes op);
673static pointer opexe_r (SCHEME_P_ enum scheme_opcodes op);
674static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); 738static int opexe_3 (SCHEME_P_ enum scheme_opcodes op);
675static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); 739static int opexe_4 (SCHEME_P_ enum scheme_opcodes op);
676static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); 740static int opexe_5 (SCHEME_P_ enum scheme_opcodes op);
677static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); 741static int opexe_6 (SCHEME_P_ enum scheme_opcodes op);
678static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 742static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
679static void assign_syntax (SCHEME_P_ const char *name); 743static void assign_syntax (SCHEME_P_ const char *name);
680static int syntaxnum (pointer p); 744static int syntaxnum (pointer p);
681static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 745static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
682 746
747static IVALUE
748ivalue (pointer x)
749{
750 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
751}
752
753static RVALUE
754rvalue (pointer x)
755{
756 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
757}
758
759INTERFACE num
760nvalue (pointer x)
761{
762 num n;
763
764 num_set_fixnum (n, is_integer (x));
765
766 if (num_is_fixnum (n))
767 num_set_ivalue (n, ivalue_unchecked (x));
768 else
769 num_set_rvalue (n, rvalue_unchecked (x));
770
771 return n;
772}
773
683static num 774static num
684num_op (enum num_op op, num a, num b) 775num_op (enum num_op op, num a, num b)
685{ 776{
686 num ret; 777 num ret;
687 778
688 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 779 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
689 780
690 if (num_is_fixnum (ret)) 781 if (num_is_fixnum (ret))
691 { 782 {
692 IVALUE av = num_get_ivalue (a);
693 IVALUE bv = num_get_ivalue (b);
694
695 switch (op) 783 switch (op)
696 { 784 {
697 case NUM_ADD: av += bv; break; 785 case NUM_ADD: a.ivalue += b.ivalue; break;
698 case NUM_SUB: av -= bv; break; 786 case NUM_SUB: a.ivalue -= b.ivalue; break;
699 case NUM_MUL: av *= bv; break; 787 case NUM_MUL: a.ivalue *= b.ivalue; break;
700 case NUM_INTDIV: av /= bv; break; 788 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
701 } 789 }
702 790
703 num_set_ivalue (ret, av); 791 num_set_ivalue (ret, a.ivalue);
704 } 792 }
793#if USE_REAL
705 else 794 else
706 { 795 {
707 RVALUE av = num_get_rvalue (a);
708 RVALUE bv = num_get_rvalue (b);
709
710 switch (op) 796 switch (op)
711 { 797 {
712 case NUM_ADD: av += bv; break; 798 case NUM_ADD: a.rvalue += b.rvalue; break;
713 case NUM_SUB: av -= bv; break; 799 case NUM_SUB: a.rvalue -= b.rvalue; break;
714 case NUM_MUL: av *= bv; break; 800 case NUM_MUL: a.rvalue *= b.rvalue; break;
715 case NUM_INTDIV: av /= bv; break; 801 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
716 } 802 }
717 803
718 num_set_rvalue (ret, av); 804 num_set_rvalue (ret, a.rvalue);
719 } 805 }
806#endif
720 807
721 return ret; 808 return ret;
722} 809}
723 810
724static num 811static num
725num_div (num a, num b) 812num_div (num a, num b)
726{ 813{
727 num ret; 814 num ret;
728 815
729 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); 816 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0);
730 817
731 if (num_is_fixnum (ret)) 818 if (num_is_fixnum (ret))
732 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 819 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
733 else 820 else
734 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 821 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
735 822
736 return ret; 823 return ret;
737} 824}
738 825
739static num 826static num
741{ 828{
742 num ret; 829 num ret;
743 long e1, e2, res; 830 long e1, e2, res;
744 831
745 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 832 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
746 e1 = num_get_ivalue (a); 833 e1 = num_ivalue (a);
747 e2 = num_get_ivalue (b); 834 e2 = num_ivalue (b);
748 res = e1 % e2; 835 res = e1 % e2;
749 836
750 /* remainder should have same sign as second operand */ 837 /* remainder should have same sign as second operand */
751 if (res > 0) 838 if (res > 0)
752 { 839 {
768{ 855{
769 num ret; 856 num ret;
770 long e1, e2, res; 857 long e1, e2, res;
771 858
772 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 859 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
773 e1 = num_get_ivalue (a); 860 e1 = num_ivalue (a);
774 e2 = num_get_ivalue (b); 861 e2 = num_ivalue (b);
775 res = e1 % e2; 862 res = e1 % e2;
776 863
777 /* modulo should have same sign as second operand */ 864 /* modulo should have same sign as second operand */
778 if (res * e2 < 0) 865 if (res * e2 < 0)
779 res += e2; 866 res += e2;
780 867
781 num_set_ivalue (ret, res); 868 num_set_ivalue (ret, res);
782 return ret; 869 return ret;
783} 870}
784 871
785/* this completely disrespects NaNs */ 872/* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */
786static int 873static int
787num_cmp (num a, num b) 874num_cmp (num a, num b)
788{ 875{
789 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 876 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
790 int ret; 877 int ret;
791 878
792 if (is_fixnum) 879 if (is_fixnum)
793 { 880 {
794 IVALUE av = num_get_ivalue (a); 881 IVALUE av = num_ivalue (a);
795 IVALUE bv = num_get_ivalue (b); 882 IVALUE bv = num_ivalue (b);
796 883
797 ret = av == bv ? 0 : av < bv ? -1 : +1; 884 ret = av == bv ? 0 : av < bv ? -1 : +1;
798 } 885 }
799 else 886 else
800 { 887 {
801 RVALUE av = num_get_rvalue (a); 888 RVALUE av = num_rvalue (a);
802 RVALUE bv = num_get_rvalue (b); 889 RVALUE bv = num_rvalue (b);
803 890
804 ret = av == bv ? 0 : av < bv ? -1 : +1; 891 ret = av == bv ? 0 : av < bv ? -1 : +1;
805 } 892 }
806 893
807 return ret; 894 return ret;
808} 895}
809
810#if USE_MATH
811
812/* Round to nearest. Round to even if midway */
813static double
814round_per_R5RS (double x)
815{
816 double fl = floor (x);
817 double ce = ceil (x);
818 double dfl = x - fl;
819 double dce = ce - x;
820
821 if (dfl > dce)
822 return ce;
823 else if (dfl < dce)
824 return fl;
825 else
826 {
827 if (fmod (fl, 2) == 0) /* I imagine this holds */
828 return fl;
829 else
830 return ce;
831 }
832}
833#endif
834 896
835static int 897static int
836is_zero_rvalue (RVALUE x) 898is_zero_rvalue (RVALUE x)
837{ 899{
900 return x == 0;
901#if 0
838#if USE_REAL 902#if USE_REAL
839 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ 903 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */
840#else 904#else
841 return x == 0; 905 return x == 0;
842#endif 906#endif
907#endif
843} 908}
844 909
845/* allocate new cell segment */ 910/* allocate new cell segment */
846static int 911ecb_cold static int
847alloc_cellseg (SCHEME_P_ int n) 912alloc_cellseg (SCHEME_P)
848{ 913{
849 pointer newp; 914 struct cell *newp;
850 pointer last; 915 struct cell *last;
851 pointer p; 916 struct cell *p;
852 char *cp; 917 char *cp;
853 long i; 918 long i;
854 int k; 919 int k;
855 920
856 static int segsize = CELL_SEGSIZE >> 1; 921 static int segsize = CELL_SEGSIZE >> 1;
857 segsize <<= 1; 922 segsize <<= 1;
858 923
859 for (k = 0; k < n; k++)
860 {
861 if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1)
862 return k;
863
864 cp = malloc (segsize * sizeof (struct cell)); 924 cp = malloc (segsize * sizeof (struct cell));
865 925
866 if (!cp && USE_ERROR_CHECKING) 926 if (!cp && USE_ERROR_CHECKING)
867 return k; 927 return k;
868 928
869 i = ++SCHEME_V->last_cell_seg; 929 i = ++SCHEME_V->last_cell_seg;
870 SCHEME_V->alloc_seg[i] = cp;
871 930
872 /* insert new segment in address order */ 931 newp = (struct cell *)cp;
873 newp = (pointer)cp;
874 SCHEME_V->cell_seg[i] = newp; 932 SCHEME_V->cell_seg[i] = newp;
875 SCHEME_V->cell_segsize[i] = segsize; 933 SCHEME_V->cell_segsize[i] = segsize;
876
877 //TODO: insert, not swap
878 while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i])
879 {
880 p = SCHEME_V->cell_seg[i];
881 SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1];
882 SCHEME_V->cell_seg[i - 1] = p;
883
884 k = SCHEME_V->cell_segsize[i];
885 SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1];
886 SCHEME_V->cell_segsize[i - 1] = k;
887
888 --i;
889 }
890
891 SCHEME_V->fcells += segsize; 934 SCHEME_V->fcells += segsize;
892 last = newp + segsize - 1; 935 last = newp + segsize - 1;
893 936
894 for (p = newp; p <= last; p++) 937 for (p = newp; p <= last; p++)
895 { 938 {
939 pointer cp = POINTER (p);
940 clrmark (cp);
896 set_typeflag (p, T_FREE); 941 set_typeflag (cp, T_PAIR);
897 set_car (p, NIL); 942 set_car (cp, NIL);
898 set_cdr (p, p + 1); 943 set_cdr (cp, POINTER (p + 1));
899 } 944 }
900 945
901 /* insert new cells in address order on free list */
902 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell)
903 {
904 set_cdr (last, SCHEME_V->free_cell); 946 set_cdr (POINTER (last), SCHEME_V->free_cell);
905 SCHEME_V->free_cell = newp; 947 SCHEME_V->free_cell = POINTER (newp);
906 }
907 else
908 {
909 p = SCHEME_V->free_cell;
910 948
911 while (cdr (p) != NIL && newp > cdr (p))
912 p = cdr (p);
913
914 set_cdr (last, cdr (p));
915 set_cdr (p, newp);
916 }
917 }
918
919 return n; 949 return 1;
920} 950}
921 951
922/* get new cell. parameter a, b is marked by gc. */ 952/* get new cell. parameter a, b is marked by gc. */
923static INLINE pointer 953ecb_inline pointer
924get_cell_x (SCHEME_P_ pointer a, pointer b) 954get_cell_x (SCHEME_P_ pointer a, pointer b)
925{ 955{
926 if (ecb_expect_false (SCHEME_V->free_cell == NIL)) 956 if (ecb_expect_false (SCHEME_V->free_cell == NIL))
927 { 957 {
928 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 958 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
929 return S_SINK; 959 return S_SINK;
930 960
931 if (SCHEME_V->free_cell == NIL) 961 if (SCHEME_V->free_cell == NIL)
932 { 962 {
933 const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; 963 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 2;
934 964
935 gc (SCHEME_A_ a, b); 965 gc (SCHEME_A_ a, b);
936 966
937 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) 967 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL)
938 { 968 {
939 /* if only a few recovered, get more to avoid fruitless gc's */ 969 /* if only a few recovered, get more to avoid fruitless gc's */
940 if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) 970 if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL)
941 { 971 {
942#if USE_ERROR_CHECKING 972#if USE_ERROR_CHECKING
943 SCHEME_V->no_memory = 1; 973 SCHEME_V->no_memory = 1;
944 return S_SINK; 974 return S_SINK;
945#endif 975#endif
957 } 987 }
958} 988}
959 989
960/* To retain recent allocs before interpreter knows about them - 990/* To retain recent allocs before interpreter knows about them -
961 Tehom */ 991 Tehom */
962 992ecb_hot static void
963static void
964push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 993push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
965{ 994{
966 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 995 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
967 996
968 set_typeflag (holder, T_PAIR); 997 set_typeflag (holder, T_PAIR);
970 set_car (holder, recent); 999 set_car (holder, recent);
971 set_cdr (holder, car (S_SINK)); 1000 set_cdr (holder, car (S_SINK));
972 set_car (S_SINK, holder); 1001 set_car (S_SINK, holder);
973} 1002}
974 1003
975static pointer 1004ecb_hot static pointer
976get_cell (SCHEME_P_ pointer a, pointer b) 1005get_cell (SCHEME_P_ pointer a, pointer b)
977{ 1006{
978 pointer cell = get_cell_x (SCHEME_A_ a, b); 1007 pointer cell = get_cell_x (SCHEME_A_ a, b);
979 1008
980 /* For right now, include "a" and "b" in "cell" so that gc doesn't 1009 /* For right now, include "a" and "b" in "cell" so that gc doesn't
989} 1018}
990 1019
991static pointer 1020static pointer
992get_vector_object (SCHEME_P_ uint32_t len, pointer init) 1021get_vector_object (SCHEME_P_ uint32_t len, pointer init)
993{ 1022{
994 pointer v = get_cell_x (SCHEME_A_ 0, 0); 1023 pointer v = get_cell_x (SCHEME_A_ NIL, NIL);
995 pointer *e = malloc (len * sizeof (pointer)); 1024 pointer *e = malloc (len * sizeof (pointer));
996 1025
997 if (!e && USE_ERROR_CHECKING) 1026 if (!e && USE_ERROR_CHECKING)
998 return S_SINK; 1027 return S_SINK;
999 1028
1000 /* Record it as a vector so that gc understands it. */ 1029 /* Record it as a vector so that gc understands it. */
1001 set_typeflag (v, T_VECTOR | T_ATOM); 1030 set_typeflag (v, T_VECTOR | T_ATOM);
1002 1031
1003 v->object.vector.vvalue = e; 1032 CELL(v)->object.vector.vvalue = e;
1004 v->object.vector.length = len; 1033 CELL(v)->object.vector.length = len;
1005 fill_vector (v, init); 1034 fill_vector (v, 0, init);
1006 push_recent_alloc (SCHEME_A_ v, NIL); 1035 push_recent_alloc (SCHEME_A_ v, NIL);
1007 1036
1008 return v; 1037 return v;
1009} 1038}
1010 1039
1011static INLINE void 1040ecb_inline void
1012ok_to_freely_gc (SCHEME_P) 1041ok_to_freely_gc (SCHEME_P)
1013{ 1042{
1014 set_car (S_SINK, NIL); 1043 set_car (S_SINK, NIL);
1015} 1044}
1016 1045
1018static void 1047static void
1019check_cell_alloced (pointer p, int expect_alloced) 1048check_cell_alloced (pointer p, int expect_alloced)
1020{ 1049{
1021 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ 1050 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */
1022 if (typeflag (p) & !expect_alloced) 1051 if (typeflag (p) & !expect_alloced)
1023 xwrstr ("Cell is already allocated!\n"); 1052 putstr (SCHEME_A_ "Cell is already allocated!\n");
1024 1053
1025 if (!(typeflag (p)) & expect_alloced) 1054 if (!(typeflag (p)) & expect_alloced)
1026 xwrstr ("Cell is not allocated!\n"); 1055 putstr (SCHEME_A_ "Cell is not allocated!\n");
1027} 1056}
1028 1057
1029static void 1058static void
1030check_range_alloced (pointer p, int n, int expect_alloced) 1059check_range_alloced (pointer p, int n, int expect_alloced)
1031{ 1060{
1037#endif 1066#endif
1038 1067
1039/* Medium level cell allocation */ 1068/* Medium level cell allocation */
1040 1069
1041/* get new cons cell */ 1070/* get new cons cell */
1042pointer 1071ecb_hot static pointer
1043xcons (SCHEME_P_ pointer a, pointer b, int immutable) 1072xcons (SCHEME_P_ pointer a, pointer b)
1044{ 1073{
1045 pointer x = get_cell (SCHEME_A_ a, b); 1074 pointer x = get_cell (SCHEME_A_ a, b);
1046 1075
1047 set_typeflag (x, T_PAIR); 1076 set_typeflag (x, T_PAIR);
1048
1049 if (immutable)
1050 setimmutable (x);
1051 1077
1052 set_car (x, a); 1078 set_car (x, a);
1053 set_cdr (x, b); 1079 set_cdr (x, b);
1054 1080
1055 return x; 1081 return x;
1056} 1082}
1057 1083
1084ecb_hot static pointer
1085ximmutable_cons (SCHEME_P_ pointer a, pointer b)
1086{
1087 pointer x = xcons (SCHEME_A_ a, b);
1088 setimmutable (x);
1089 return x;
1090}
1091
1092#define cons(a,b) xcons (SCHEME_A_ a, b)
1093#define immutable_cons(a,b) ximmutable_cons (SCHEME_A_ a, b)
1094
1095ecb_cold static pointer
1096generate_symbol (SCHEME_P_ const char *name)
1097{
1098 pointer x = mk_string (SCHEME_A_ name);
1099 setimmutable (x);
1100 set_typeflag (x, T_SYMBOL | T_ATOM);
1101 return x;
1102}
1103
1058/* ========== oblist implementation ========== */ 1104/* ========== oblist implementation ========== */
1059 1105
1060#ifndef USE_OBJECT_LIST 1106#ifndef USE_OBJECT_LIST
1061 1107
1108static int
1062static int hash_fn (const char *key, int table_size); 1109hash_fn (const char *key, int table_size)
1110{
1111 const unsigned char *p = (unsigned char *)key;
1112 uint32_t hash = 2166136261U;
1063 1113
1114 while (*p)
1115 hash = (hash ^ *p++) * 16777619;
1116
1117 return hash % table_size;
1118}
1119
1064static pointer 1120ecb_cold static pointer
1065oblist_initial_value (SCHEME_P) 1121oblist_initial_value (SCHEME_P)
1066{ 1122{
1067 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1123 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1068} 1124}
1069 1125
1070/* returns the new symbol */ 1126/* returns the new symbol */
1071static pointer 1127ecb_cold static pointer
1072oblist_add_by_name (SCHEME_P_ const char *name) 1128oblist_add_by_name (SCHEME_P_ const char *name)
1073{ 1129{
1074 int location; 1130 pointer x = generate_symbol (SCHEME_A_ name);
1075
1076 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1077 set_typeflag (x, T_SYMBOL);
1078 setimmutable (car (x));
1079
1080 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1131 int location = hash_fn (name, veclength (SCHEME_V->oblist));
1081 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); 1132 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1082 return x; 1133 return x;
1083} 1134}
1084 1135
1085static INLINE pointer 1136ecb_cold static pointer
1086oblist_find_by_name (SCHEME_P_ const char *name) 1137oblist_find_by_name (SCHEME_P_ const char *name)
1087{ 1138{
1088 int location; 1139 int location;
1089 pointer x; 1140 pointer x;
1090 char *s; 1141 char *s;
1091 1142
1092 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1143 location = hash_fn (name, veclength (SCHEME_V->oblist));
1093 1144
1094 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1145 for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1095 { 1146 {
1096 s = symname (car (x)); 1147 s = symname (car (x));
1097 1148
1098 /* case-insensitive, per R5RS section 2 */ 1149 /* case-insensitive, per R5RS section 2 */
1099 if (stricmp (name, s) == 0) 1150 if (stricmp (name, s) == 0)
1101 } 1152 }
1102 1153
1103 return NIL; 1154 return NIL;
1104} 1155}
1105 1156
1106static pointer 1157ecb_cold static pointer
1107oblist_all_symbols (SCHEME_P) 1158oblist_all_symbols (SCHEME_P)
1108{ 1159{
1109 int i; 1160 int i;
1110 pointer x; 1161 pointer x;
1111 pointer ob_list = NIL; 1162 pointer ob_list = NIL;
1112 1163
1113 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1164 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1114 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1165 for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1115 ob_list = cons (x, ob_list); 1166 ob_list = cons (x, ob_list);
1116 1167
1117 return ob_list; 1168 return ob_list;
1118} 1169}
1119 1170
1120#else 1171#else
1121 1172
1122static pointer 1173ecb_cold static pointer
1123oblist_initial_value (SCHEME_P) 1174oblist_initial_value (SCHEME_P)
1124{ 1175{
1125 return NIL; 1176 return NIL;
1126} 1177}
1127 1178
1128static INLINE pointer 1179ecb_cold static pointer
1129oblist_find_by_name (SCHEME_P_ const char *name) 1180oblist_find_by_name (SCHEME_P_ const char *name)
1130{ 1181{
1131 pointer x; 1182 pointer x;
1132 char *s; 1183 char *s;
1133 1184
1142 1193
1143 return NIL; 1194 return NIL;
1144} 1195}
1145 1196
1146/* returns the new symbol */ 1197/* returns the new symbol */
1147static pointer 1198ecb_cold static pointer
1148oblist_add_by_name (SCHEME_P_ const char *name) 1199oblist_add_by_name (SCHEME_P_ const char *name)
1149{ 1200{
1150 pointer x; 1201 pointer x = generate_symbol (SCHEME_A_ name);
1151
1152 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1153 set_typeflag (x, T_SYMBOL);
1154 setimmutable (car (x));
1155 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1202 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1156 return x; 1203 return x;
1157} 1204}
1158 1205
1159static pointer 1206ecb_cold static pointer
1160oblist_all_symbols (SCHEME_P) 1207oblist_all_symbols (SCHEME_P)
1161{ 1208{
1162 return SCHEME_V->oblist; 1209 return SCHEME_V->oblist;
1163} 1210}
1164 1211
1165#endif 1212#endif
1166 1213
1167#if USE_PORTS
1168static pointer 1214ecb_cold static pointer
1169mk_port (SCHEME_P_ port *p) 1215mk_port (SCHEME_P_ port *p)
1170{ 1216{
1171 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1217 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1172 1218
1173 set_typeflag (x, T_PORT | T_ATOM); 1219 set_typeflag (x, T_PORT | T_ATOM);
1174 x->object.port = p; 1220 set_port (x, p);
1175 1221
1176 return x; 1222 return x;
1177} 1223}
1178#endif
1179 1224
1180pointer 1225ecb_cold pointer
1181mk_foreign_func (SCHEME_P_ foreign_func f) 1226mk_foreign_func (SCHEME_P_ foreign_func f)
1182{ 1227{
1183 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1228 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1184 1229
1185 set_typeflag (x, (T_FOREIGN | T_ATOM)); 1230 set_typeflag (x, T_FOREIGN | T_ATOM);
1186 x->object.ff = f; 1231 CELL(x)->object.ff = f;
1187 1232
1188 return x; 1233 return x;
1189} 1234}
1190 1235
1191INTERFACE pointer 1236INTERFACE pointer
1192mk_character (SCHEME_P_ int c) 1237mk_character (SCHEME_P_ int c)
1193{ 1238{
1194 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1239 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1195 1240
1196 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1241 set_typeflag (x, T_CHARACTER | T_ATOM);
1197 ivalue_unchecked (x) = c & 0xff; 1242 set_ivalue (x, c & 0xff);
1198 set_num_integer (x); 1243
1199 return x; 1244 return x;
1200} 1245}
1201 1246
1202/* get number atom (integer) */ 1247/* get number atom (integer) */
1203INTERFACE pointer 1248INTERFACE pointer
1204mk_integer (SCHEME_P_ long num) 1249mk_integer (SCHEME_P_ long n)
1205{ 1250{
1251 pointer p = 0;
1252 pointer *pp = &p;
1253
1254#if USE_INTCACHE
1255 if (n >= INTCACHE_MIN && n <= INTCACHE_MAX)
1256 pp = &SCHEME_V->intcache[n - INTCACHE_MIN];
1257#endif
1258
1259 if (!*pp)
1260 {
1206 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1261 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1207 1262
1208 set_typeflag (x, (T_NUMBER | T_ATOM)); 1263 set_typeflag (x, T_INTEGER | T_ATOM);
1209 ivalue_unchecked (x) = num; 1264 setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */
1210 set_num_integer (x); 1265 set_ivalue (x, n);
1266
1267 *pp = x;
1268 }
1269
1211 return x; 1270 return *pp;
1212} 1271}
1213 1272
1214INTERFACE pointer 1273INTERFACE pointer
1215mk_real (SCHEME_P_ RVALUE n) 1274mk_real (SCHEME_P_ RVALUE n)
1216{ 1275{
1276#if USE_REAL
1217 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1277 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1218 1278
1219 set_typeflag (x, (T_NUMBER | T_ATOM)); 1279 set_typeflag (x, T_REAL | T_ATOM);
1220 rvalue_unchecked (x) = n; 1280 set_rvalue (x, n);
1221 set_num_real (x); 1281
1222 return x; 1282 return x;
1283#else
1284 return mk_integer (SCHEME_A_ n);
1285#endif
1223} 1286}
1224 1287
1225static pointer 1288static pointer
1226mk_number (SCHEME_P_ const num n) 1289mk_number (SCHEME_P_ const num n)
1227{ 1290{
1291#if USE_REAL
1228 if (num_is_fixnum (n)) 1292 return num_is_fixnum (n)
1293 ? mk_integer (SCHEME_A_ num_ivalue (n))
1294 : mk_real (SCHEME_A_ num_rvalue (n));
1295#else
1229 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1296 return mk_integer (SCHEME_A_ num_ivalue (n));
1230 else 1297#endif
1231 return mk_real (SCHEME_A_ num_get_rvalue (n));
1232} 1298}
1233 1299
1234/* allocate name to string area */ 1300/* allocate name to string area */
1235static char * 1301static char *
1236store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) 1302store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1242 SCHEME_V->no_memory = 1; 1308 SCHEME_V->no_memory = 1;
1243 return SCHEME_V->strbuff; 1309 return SCHEME_V->strbuff;
1244 } 1310 }
1245 1311
1246 if (str) 1312 if (str)
1247 { 1313 memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */
1248 int l = strlen (str);
1249
1250 if (l > len_str)
1251 l = len_str;
1252
1253 memcpy (q, str, l);
1254 q[l] = 0;
1255 }
1256 else 1314 else
1257 {
1258 memset (q, fill, len_str); 1315 memset (q, fill, len_str);
1316
1259 q[len_str] = 0; 1317 q[len_str] = 0;
1260 }
1261 1318
1262 return q; 1319 return q;
1263} 1320}
1264 1321
1265INTERFACE pointer 1322INTERFACE pointer
1279 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1336 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1280 1337
1281 set_typeflag (x, T_STRING | T_ATOM); 1338 set_typeflag (x, T_STRING | T_ATOM);
1282 strvalue (x) = store_string (SCHEME_A_ len, str, 0); 1339 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1283 strlength (x) = len; 1340 strlength (x) = len;
1341
1284 return x; 1342 return x;
1285} 1343}
1286 1344
1287INTERFACE pointer 1345INTERFACE pointer
1288mk_string (SCHEME_P_ const char *str) 1346mk_string (SCHEME_P_ const char *str)
1295{ 1353{
1296 return get_vector_object (SCHEME_A_ len, NIL); 1354 return get_vector_object (SCHEME_A_ len, NIL);
1297} 1355}
1298 1356
1299INTERFACE void 1357INTERFACE void
1300fill_vector (pointer vec, pointer obj) 1358fill_vector (pointer vec, uint32_t start, pointer obj)
1301{ 1359{
1302 int i; 1360 int i;
1303 1361
1304 for (i = 0; i < vec->object.vector.length; i++) 1362 for (i = start; i < veclength (vec); i++)
1305 vecvalue (vec)[i] = obj; 1363 vecvalue (vec)[i] = obj;
1306} 1364}
1307 1365
1366INTERFACE void
1367vector_resize (pointer vec, uint32_t newsize, pointer fill)
1368{
1369 uint32_t oldsize = veclength (vec);
1370 vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer));
1371 veclength (vec) = newsize;
1372 fill_vector (vec, oldsize, fill);
1373}
1374
1308INTERFACE pointer 1375INTERFACE pointer
1309vector_elem (pointer vec, uint32_t ielem) 1376vector_get (pointer vec, uint32_t ielem)
1310{ 1377{
1311 return vecvalue(vec)[ielem]; 1378 return vecvalue(vec)[ielem];
1312} 1379}
1313 1380
1314INTERFACE void 1381INTERFACE void
1315set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1382vector_set (pointer vec, uint32_t ielem, pointer a)
1316{ 1383{
1317 vecvalue(vec)[ielem] = a; 1384 vecvalue(vec)[ielem] = a;
1318} 1385}
1319 1386
1320/* get new symbol */ 1387/* get new symbol */
1328 x = oblist_add_by_name (SCHEME_A_ name); 1395 x = oblist_add_by_name (SCHEME_A_ name);
1329 1396
1330 return x; 1397 return x;
1331} 1398}
1332 1399
1333INTERFACE pointer 1400ecb_cold INTERFACE pointer
1334gensym (SCHEME_P) 1401gensym (SCHEME_P)
1335{ 1402{
1336 pointer x; 1403 pointer x;
1337
1338 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1339 {
1340 char name[40] = "gensym-"; 1404 char name[40] = "gensym-";
1341 xnum (name + 7, SCHEME_V->gensym_cnt); 1405 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1342 1406
1343 /* first check oblist */ 1407 return generate_symbol (SCHEME_A_ name);
1344 x = oblist_find_by_name (SCHEME_A_ name); 1408}
1345 1409
1346 if (x == NIL) 1410static int
1347 { 1411is_gensym (SCHEME_P_ pointer x)
1348 x = oblist_add_by_name (SCHEME_A_ name); 1412{
1349 return x; 1413 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1350 }
1351 }
1352
1353 return NIL;
1354} 1414}
1355 1415
1356/* make symbol or number atom from string */ 1416/* make symbol or number atom from string */
1357static pointer 1417ecb_cold static pointer
1358mk_atom (SCHEME_P_ char *q) 1418mk_atom (SCHEME_P_ char *q)
1359{ 1419{
1360 char c, *p; 1420 char c, *p;
1361 int has_dec_point = 0; 1421 int has_dec_point = 0;
1362 int has_fp_exp = 0; 1422 int has_fp_exp = 0;
1412 } 1472 }
1413 else if ((c == 'e') || (c == 'E')) 1473 else if ((c == 'e') || (c == 'E'))
1414 { 1474 {
1415 if (!has_fp_exp) 1475 if (!has_fp_exp)
1416 { 1476 {
1417 has_dec_point = 1; /* decimal point illegal 1477 has_dec_point = 1; /* decimal point illegal from now on */
1418 from now on */
1419 p++; 1478 p++;
1420 1479
1421 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1480 if ((*p == '-') || (*p == '+') || isdigit (*p))
1422 continue; 1481 continue;
1423 } 1482 }
1434 1493
1435 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1494 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1436} 1495}
1437 1496
1438/* make constant */ 1497/* make constant */
1439static pointer 1498ecb_cold static pointer
1440mk_sharp_const (SCHEME_P_ char *name) 1499mk_sharp_const (SCHEME_P_ char *name)
1441{ 1500{
1442 if (!strcmp (name, "t")) 1501 if (!strcmp (name, "t"))
1443 return S_T; 1502 return S_T;
1444 else if (!strcmp (name, "f")) 1503 else if (!strcmp (name, "f"))
1445 return S_F; 1504 return S_F;
1446 else if (*name == '\\') /* #\w (character) */ 1505 else if (*name == '\\') /* #\w (character) */
1447 { 1506 {
1448 int c; 1507 int c;
1449 1508
1509 // TODO: optimise
1450 if (stricmp (name + 1, "space") == 0) 1510 if (stricmp (name + 1, "space") == 0)
1451 c = ' '; 1511 c = ' ';
1452 else if (stricmp (name + 1, "newline") == 0) 1512 else if (stricmp (name + 1, "newline") == 0)
1453 c = '\n'; 1513 c = '\n';
1454 else if (stricmp (name + 1, "return") == 0) 1514 else if (stricmp (name + 1, "return") == 0)
1455 c = '\r'; 1515 c = '\r';
1456 else if (stricmp (name + 1, "tab") == 0) 1516 else if (stricmp (name + 1, "tab") == 0)
1457 c = '\t'; 1517 c = '\t';
1518 else if (stricmp (name + 1, "alarm") == 0)
1519 c = 0x07;
1520 else if (stricmp (name + 1, "backspace") == 0)
1521 c = 0x08;
1522 else if (stricmp (name + 1, "escape") == 0)
1523 c = 0x1b;
1524 else if (stricmp (name + 1, "delete") == 0)
1525 c = 0x7f;
1526 else if (stricmp (name + 1, "null") == 0)
1527 c = 0;
1458 else if (name[1] == 'x' && name[2] != 0) 1528 else if (name[1] == 'x' && name[2] != 0)
1459 { 1529 {
1460 long c1 = strtol (name + 2, 0, 16); 1530 long c1 = strtol (name + 2, 0, 16);
1461 1531
1462 if (0 <= c1 && c1 <= UCHAR_MAX) 1532 if (0 <= c1 && c1 <= UCHAR_MAX)
1487 return NIL; 1557 return NIL;
1488 } 1558 }
1489} 1559}
1490 1560
1491/* ========== garbage collector ========== */ 1561/* ========== garbage collector ========== */
1562
1563static void
1564finalize_cell (SCHEME_P_ pointer a)
1565{
1566 /* TODO, fast bitmap check? */
1567 if (is_string (a) || is_symbol (a))
1568 free (strvalue (a));
1569 else if (is_vector (a))
1570 free (vecvalue (a));
1571#if USE_PORTS
1572 else if (is_port (a))
1573 {
1574 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit)
1575 port_close (SCHEME_A_ a, port_input | port_output);
1576
1577 free (port (a));
1578 }
1579#endif
1580}
1492 1581
1493/*-- 1582/*--
1494 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, 1583 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1,
1495 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1584 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1496 * for marking. 1585 * for marking.
1497 * 1586 *
1498 * The exception is vectors - vectors are currently marked recursively, 1587 * The exception is vectors - vectors are currently marked recursively,
1499 * which is inherited form tinyscheme and could be fixed by having another 1588 * which is inherited form tinyscheme and could be fixed by having another
1500 * word of context in the vector 1589 * word of context in the vector
1501 */ 1590 */
1502static void 1591ecb_hot static void
1503mark (pointer a) 1592mark (pointer a)
1504{ 1593{
1505 pointer t, q, p; 1594 pointer t, q, p;
1506 1595
1507 t = 0; 1596 t = 0;
1511 1600
1512 if (ecb_expect_false (is_vector (p))) 1601 if (ecb_expect_false (is_vector (p)))
1513 { 1602 {
1514 int i; 1603 int i;
1515 1604
1516 for (i = 0; i < p->object.vector.length; i++) 1605 for (i = 0; i < veclength (p); i++)
1517 mark (vecvalue (p)[i]); 1606 mark (vecvalue (p)[i]);
1518 } 1607 }
1519 1608
1520 if (is_atom (p)) 1609 if (is_atom (p))
1521 goto E6; 1610 goto E6;
1564 p = q; 1653 p = q;
1565 goto E6; 1654 goto E6;
1566 } 1655 }
1567} 1656}
1568 1657
1658ecb_hot static void
1659gc_free (SCHEME_P)
1660{
1661 int i;
1662 uint32_t total = 0;
1663
1664 /* Here we scan the cells to build the free-list. */
1665 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1666 {
1667 struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1668 struct cell *p;
1669 total += SCHEME_V->cell_segsize [i];
1670
1671 for (p = SCHEME_V->cell_seg[i]; p < end; ++p)
1672 {
1673 pointer c = POINTER (p);
1674
1675 if (is_mark (c))
1676 clrmark (c);
1677 else
1678 {
1679 /* reclaim cell */
1680 if (typeflag (c) != T_PAIR)
1681 {
1682 finalize_cell (SCHEME_A_ c);
1683 set_typeflag (c, T_PAIR);
1684 set_car (c, NIL);
1685 }
1686
1687 ++SCHEME_V->fcells;
1688 set_cdr (c, SCHEME_V->free_cell);
1689 SCHEME_V->free_cell = c;
1690 }
1691 }
1692 }
1693
1694 if (SCHEME_V->gc_verbose)
1695 {
1696 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");
1697 }
1698}
1699
1569/* garbage collection. parameter a, b is marked. */ 1700/* garbage collection. parameter a, b is marked. */
1570static void 1701ecb_cold static void
1571gc (SCHEME_P_ pointer a, pointer b) 1702gc (SCHEME_P_ pointer a, pointer b)
1572{ 1703{
1573 pointer p;
1574 int i; 1704 int i;
1575 1705
1576 if (SCHEME_V->gc_verbose) 1706 if (SCHEME_V->gc_verbose)
1577 putstr (SCHEME_A_ "gc..."); 1707 putstr (SCHEME_A_ "gc...");
1578 1708
1594 /* Mark recent objects the interpreter doesn't know about yet. */ 1724 /* Mark recent objects the interpreter doesn't know about yet. */
1595 mark (car (S_SINK)); 1725 mark (car (S_SINK));
1596 /* Mark any older stuff above nested C calls */ 1726 /* Mark any older stuff above nested C calls */
1597 mark (SCHEME_V->c_nest); 1727 mark (SCHEME_V->c_nest);
1598 1728
1729#if USE_INTCACHE
1730 /* mark intcache */
1731 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1732 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1733 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1734#endif
1735
1599 /* mark variables a, b */ 1736 /* mark variables a, b */
1600 mark (a); 1737 mark (a);
1601 mark (b); 1738 mark (b);
1602 1739
1603 /* garbage collect */ 1740 /* garbage collect */
1604 clrmark (NIL); 1741 clrmark (NIL);
1605 SCHEME_V->fcells = 0; 1742 SCHEME_V->fcells = 0;
1606 SCHEME_V->free_cell = NIL; 1743 SCHEME_V->free_cell = NIL;
1607 1744
1608 /* free-list is kept sorted by address so as to maintain consecutive
1609 ranges, if possible, for use with vectors. Here we scan the cells
1610 (which are also kept sorted by address) downwards to build the
1611 free-list in sorted order.
1612 */
1613 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1614 {
1615 p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1616
1617 while (--p >= SCHEME_V->cell_seg[i])
1618 {
1619 if (is_mark (p))
1620 clrmark (p);
1621 else
1622 {
1623 /* reclaim cell */
1624 if (typeflag (p) != T_FREE)
1625 {
1626 finalize_cell (SCHEME_A_ p);
1627 set_typeflag (p, T_FREE);
1628 set_car (p, NIL);
1629 }
1630
1631 ++SCHEME_V->fcells;
1632 set_cdr (p, SCHEME_V->free_cell);
1633 SCHEME_V->free_cell = p;
1634 }
1635 }
1636 }
1637
1638 if (SCHEME_V->gc_verbose) 1745 if (SCHEME_V->gc_verbose)
1639 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1746 putstr (SCHEME_A_ "freeing...");
1640}
1641 1747
1642static void 1748 gc_free (SCHEME_A);
1643finalize_cell (SCHEME_P_ pointer a)
1644{
1645 /* TODO, fast bitmap check? */
1646 if (is_string (a))
1647 free (strvalue (a));
1648 else if (is_vector (a))
1649 free (vecvalue (a));
1650#if USE_PORTS
1651 else if (is_port (a))
1652 {
1653 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit)
1654 port_close (SCHEME_A_ a, port_input | port_output);
1655
1656 free (a->object.port);
1657 }
1658#endif
1659} 1749}
1660 1750
1661/* ========== Routines for Reading ========== */ 1751/* ========== Routines for Reading ========== */
1662 1752
1663static int 1753ecb_cold static int
1664file_push (SCHEME_P_ const char *fname) 1754file_push (SCHEME_P_ const char *fname)
1665{ 1755{
1666#if USE_PORTS
1667 int fin; 1756 int fin;
1668 1757
1669 if (SCHEME_V->file_i == MAXFIL - 1) 1758 if (SCHEME_V->file_i == MAXFIL - 1)
1670 return 0; 1759 return 0;
1671 1760
1677 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; 1766 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1;
1678 SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; 1767 SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input;
1679 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; 1768 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin;
1680 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; 1769 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1;
1681 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; 1770 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0;
1682 SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; 1771 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1683 1772
1684#if SHOW_ERROR_LINE 1773#if SHOW_ERROR_LINE
1685 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; 1774 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0;
1686 1775
1687 if (fname) 1776 if (fname)
1688 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0); 1777 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0);
1689#endif 1778#endif
1690 } 1779 }
1691 1780
1692 return fin >= 0; 1781 return fin >= 0;
1693
1694#else
1695 return 1;
1696#endif
1697} 1782}
1698 1783
1699static void 1784ecb_cold static void
1700file_pop (SCHEME_P) 1785file_pop (SCHEME_P)
1701{ 1786{
1702 if (SCHEME_V->file_i != 0) 1787 if (SCHEME_V->file_i != 0)
1703 { 1788 {
1704 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1789 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1705#if USE_PORTS 1790#if USE_PORTS
1706 port_close (SCHEME_A_ SCHEME_V->loadport, port_input); 1791 port_close (SCHEME_A_ SCHEME_V->loadport, port_input);
1707#endif 1792#endif
1708 SCHEME_V->file_i--; 1793 SCHEME_V->file_i--;
1709 SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; 1794 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1710 } 1795 }
1711} 1796}
1712 1797
1713static int 1798ecb_cold static int
1714file_interactive (SCHEME_P) 1799file_interactive (SCHEME_P)
1715{ 1800{
1716#if USE_PORTS 1801#if USE_PORTS
1717 return SCHEME_V->file_i == 0 1802 return SCHEME_V->file_i == 0
1718 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1803 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1719 && (SCHEME_V->inport->object.port->kind & port_file); 1804 && (port (SCHEME_V->inport)->kind & port_file);
1720#else 1805#else
1721 return 0; 1806 return 0;
1722#endif 1807#endif
1723} 1808}
1724 1809
1725#if USE_PORTS 1810#if USE_PORTS
1726static port * 1811ecb_cold static port *
1727port_rep_from_filename (SCHEME_P_ const char *fn, int prop) 1812port_rep_from_filename (SCHEME_P_ const char *fn, int prop)
1728{ 1813{
1729 int fd; 1814 int fd;
1730 int flags; 1815 int flags;
1731 char *rw; 1816 char *rw;
1754# endif 1839# endif
1755 1840
1756 return pt; 1841 return pt;
1757} 1842}
1758 1843
1759static pointer 1844ecb_cold static pointer
1760port_from_filename (SCHEME_P_ const char *fn, int prop) 1845port_from_filename (SCHEME_P_ const char *fn, int prop)
1761{ 1846{
1762 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); 1847 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop);
1763 1848
1764 if (!pt && USE_ERROR_CHECKING) 1849 if (!pt && USE_ERROR_CHECKING)
1765 return NIL; 1850 return NIL;
1766 1851
1767 return mk_port (SCHEME_A_ pt); 1852 return mk_port (SCHEME_A_ pt);
1768} 1853}
1769 1854
1770static port * 1855ecb_cold static port *
1771port_rep_from_file (SCHEME_P_ int f, int prop) 1856port_rep_from_file (SCHEME_P_ int f, int prop)
1772{ 1857{
1773 port *pt = malloc (sizeof *pt); 1858 port *pt = malloc (sizeof *pt);
1774 1859
1775 if (!pt && USE_ERROR_CHECKING) 1860 if (!pt && USE_ERROR_CHECKING)
1780 pt->rep.stdio.file = f; 1865 pt->rep.stdio.file = f;
1781 pt->rep.stdio.closeit = 0; 1866 pt->rep.stdio.closeit = 0;
1782 return pt; 1867 return pt;
1783} 1868}
1784 1869
1785static pointer 1870ecb_cold static pointer
1786port_from_file (SCHEME_P_ int f, int prop) 1871port_from_file (SCHEME_P_ int f, int prop)
1787{ 1872{
1788 port *pt = port_rep_from_file (SCHEME_A_ f, prop); 1873 port *pt = port_rep_from_file (SCHEME_A_ f, prop);
1789 1874
1790 if (!pt && USE_ERROR_CHECKING) 1875 if (!pt && USE_ERROR_CHECKING)
1791 return NIL; 1876 return NIL;
1792 1877
1793 return mk_port (SCHEME_A_ pt); 1878 return mk_port (SCHEME_A_ pt);
1794} 1879}
1795 1880
1796static port * 1881ecb_cold static port *
1797port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1882port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
1798{ 1883{
1799 port *pt = malloc (sizeof (port)); 1884 port *pt = malloc (sizeof (port));
1800 1885
1801 if (!pt && USE_ERROR_CHECKING) 1886 if (!pt && USE_ERROR_CHECKING)
1807 pt->rep.string.curr = start; 1892 pt->rep.string.curr = start;
1808 pt->rep.string.past_the_end = past_the_end; 1893 pt->rep.string.past_the_end = past_the_end;
1809 return pt; 1894 return pt;
1810} 1895}
1811 1896
1812static pointer 1897ecb_cold static pointer
1813port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1898port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
1814{ 1899{
1815 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); 1900 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop);
1816 1901
1817 if (!pt && USE_ERROR_CHECKING) 1902 if (!pt && USE_ERROR_CHECKING)
1820 return mk_port (SCHEME_A_ pt); 1905 return mk_port (SCHEME_A_ pt);
1821} 1906}
1822 1907
1823# define BLOCK_SIZE 256 1908# define BLOCK_SIZE 256
1824 1909
1825static port * 1910ecb_cold static port *
1826port_rep_from_scratch (SCHEME_P) 1911port_rep_from_scratch (SCHEME_P)
1827{ 1912{
1828 char *start; 1913 char *start;
1829 port *pt = malloc (sizeof (port)); 1914 port *pt = malloc (sizeof (port));
1830 1915
1844 pt->rep.string.curr = start; 1929 pt->rep.string.curr = start;
1845 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; 1930 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1;
1846 return pt; 1931 return pt;
1847} 1932}
1848 1933
1849static pointer 1934ecb_cold static pointer
1850port_from_scratch (SCHEME_P) 1935port_from_scratch (SCHEME_P)
1851{ 1936{
1852 port *pt = port_rep_from_scratch (SCHEME_A); 1937 port *pt = port_rep_from_scratch (SCHEME_A);
1853 1938
1854 if (!pt && USE_ERROR_CHECKING) 1939 if (!pt && USE_ERROR_CHECKING)
1855 return NIL; 1940 return NIL;
1856 1941
1857 return mk_port (SCHEME_A_ pt); 1942 return mk_port (SCHEME_A_ pt);
1858} 1943}
1859 1944
1860static void 1945ecb_cold static void
1861port_close (SCHEME_P_ pointer p, int flag) 1946port_close (SCHEME_P_ pointer p, int flag)
1862{ 1947{
1863 port *pt = p->object.port; 1948 port *pt = port (p);
1864 1949
1865 pt->kind &= ~flag; 1950 pt->kind &= ~flag;
1866 1951
1867 if ((pt->kind & (port_input | port_output)) == 0) 1952 if ((pt->kind & (port_input | port_output)) == 0)
1868 { 1953 {
1885 } 1970 }
1886} 1971}
1887#endif 1972#endif
1888 1973
1889/* get new character from input file */ 1974/* get new character from input file */
1890static int 1975ecb_cold static int
1891inchar (SCHEME_P) 1976inchar (SCHEME_P)
1892{ 1977{
1893 int c; 1978 int c;
1894 port *pt; 1979 port *pt = port (SCHEME_V->inport);
1895
1896 pt = SCHEME_V->inport->object.port;
1897 1980
1898 if (pt->kind & port_saw_EOF) 1981 if (pt->kind & port_saw_EOF)
1899 return EOF; 1982 return EOF;
1900 1983
1901 c = basic_inchar (pt); 1984 c = basic_inchar (pt);
1911 } 1994 }
1912 1995
1913 return c; 1996 return c;
1914} 1997}
1915 1998
1916static int ungot = -1; 1999ecb_cold static int
1917
1918static int
1919basic_inchar (port *pt) 2000basic_inchar (port *pt)
1920{ 2001{
1921#if USE_PORTS
1922 if (pt->unget != -1) 2002 if (pt->unget != -1)
1923 { 2003 {
1924 int r = pt->unget; 2004 int r = pt->unget;
1925 pt->unget = -1; 2005 pt->unget = -1;
1926 return r; 2006 return r;
1927 } 2007 }
1928 2008
2009#if USE_PORTS
1929 if (pt->kind & port_file) 2010 if (pt->kind & port_file)
1930 { 2011 {
1931 char c; 2012 char c;
1932 2013
1933 if (!read (pt->rep.stdio.file, &c, 1)) 2014 if (!read (pt->rep.stdio.file, &c, 1))
1941 return EOF; 2022 return EOF;
1942 else 2023 else
1943 return *pt->rep.string.curr++; 2024 return *pt->rep.string.curr++;
1944 } 2025 }
1945#else 2026#else
1946 if (ungot == -1)
1947 {
1948 char c; 2027 char c;
1949 if (!read (0, &c, 1)) 2028
2029 if (!read (pt->rep.stdio.file, &c, 1))
1950 return EOF; 2030 return EOF;
1951 2031
1952 ungot = c;
1953 }
1954
1955 {
1956 int r = ungot;
1957 ungot = -1;
1958 return r; 2032 return c;
1959 }
1960#endif 2033#endif
1961} 2034}
1962 2035
1963/* back character to input buffer */ 2036/* back character to input buffer */
1964static void 2037ecb_cold static void
1965backchar (SCHEME_P_ int c) 2038backchar (SCHEME_P_ int c)
1966{ 2039{
1967#if USE_PORTS 2040 port *pt = port (SCHEME_V->inport);
1968 port *pt;
1969 2041
1970 if (c == EOF) 2042 if (c == EOF)
1971 return; 2043 return;
1972 2044
1973 pt = SCHEME_V->inport->object.port;
1974 pt->unget = c; 2045 pt->unget = c;
1975#else
1976 if (c == EOF)
1977 return;
1978
1979 ungot = c;
1980#endif
1981} 2046}
1982 2047
1983#if USE_PORTS 2048#if USE_PORTS
1984static int 2049ecb_cold static int
1985realloc_port_string (SCHEME_P_ port *p) 2050realloc_port_string (SCHEME_P_ port *p)
1986{ 2051{
1987 char *start = p->rep.string.start; 2052 char *start = p->rep.string.start;
1988 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; 2053 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE;
1989 char *str = malloc (new_size); 2054 char *str = malloc (new_size);
2002 else 2067 else
2003 return 0; 2068 return 0;
2004} 2069}
2005#endif 2070#endif
2006 2071
2007INTERFACE void 2072ecb_cold static void
2008putstr (SCHEME_P_ const char *s) 2073putchars (SCHEME_P_ const char *s, int len)
2009{ 2074{
2075 port *pt = port (SCHEME_V->outport);
2076
2010#if USE_PORTS 2077#if USE_PORTS
2011 port *pt = SCHEME_V->outport->object.port;
2012
2013 if (pt->kind & port_file)
2014 write (pt->rep.stdio.file, s, strlen (s));
2015 else
2016 for (; *s; s++)
2017 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2018 *pt->rep.string.curr++ = *s;
2019 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2020 *pt->rep.string.curr++ = *s;
2021
2022#else
2023 xwrstr (s);
2024#endif
2025}
2026
2027static void
2028putchars (SCHEME_P_ const char *s, int len)
2029{
2030#if USE_PORTS
2031 port *pt = SCHEME_V->outport->object.port;
2032
2033 if (pt->kind & port_file) 2078 if (pt->kind & port_file)
2034 write (pt->rep.stdio.file, s, len); 2079 write (pt->rep.stdio.file, s, len);
2035 else 2080 else
2036 { 2081 {
2037 for (; len; len--) 2082 for (; len; len--)
2042 *pt->rep.string.curr++ = *s++; 2087 *pt->rep.string.curr++ = *s++;
2043 } 2088 }
2044 } 2089 }
2045 2090
2046#else 2091#else
2047 write (1, s, len); 2092 write (1, s, len); // output not initialised
2048#endif 2093#endif
2094}
2095
2096INTERFACE void
2097putstr (SCHEME_P_ const char *s)
2098{
2099 putchars (SCHEME_A_ s, strlen (s));
2049} 2100}
2050 2101
2051INTERFACE void 2102INTERFACE void
2052putcharacter (SCHEME_P_ int c) 2103putcharacter (SCHEME_P_ int c)
2053{ 2104{
2054#if USE_PORTS
2055 port *pt = SCHEME_V->outport->object.port;
2056
2057 if (pt->kind & port_file)
2058 {
2059 char cc = c;
2060 write (pt->rep.stdio.file, &cc, 1);
2061 }
2062 else
2063 {
2064 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2065 *pt->rep.string.curr++ = c;
2066 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2067 *pt->rep.string.curr++ = c;
2068 }
2069
2070#else
2071 char cc = c; 2105 char cc = c;
2072 write (1, &c, 1); 2106
2073#endif 2107 putchars (SCHEME_A_ &cc, 1);
2074} 2108}
2075 2109
2076/* read characters up to delimiter, but cater to character constants */ 2110/* read characters up to delimiter, but cater to character constants */
2077static char * 2111ecb_cold static char *
2078readstr_upto (SCHEME_P_ char *delim) 2112readstr_upto (SCHEME_P_ int skip, const char *delim)
2079{ 2113{
2080 char *p = SCHEME_V->strbuff; 2114 char *p = SCHEME_V->strbuff + skip;
2081 2115
2082 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2116 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2083 2117
2084 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2118 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2085 *p = 0; 2119 *p = 0;
2091 2125
2092 return SCHEME_V->strbuff; 2126 return SCHEME_V->strbuff;
2093} 2127}
2094 2128
2095/* read string expression "xxx...xxx" */ 2129/* read string expression "xxx...xxx" */
2096static pointer 2130ecb_cold static pointer
2097readstrexp (SCHEME_P) 2131readstrexp (SCHEME_P_ char delim)
2098{ 2132{
2099 char *p = SCHEME_V->strbuff; 2133 char *p = SCHEME_V->strbuff;
2100 int c; 2134 int c;
2101 int c1 = 0; 2135 int c1 = 0;
2102 enum
2103 { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; 2136 enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok;
2104 2137
2105 for (;;) 2138 for (;;)
2106 { 2139 {
2107 c = inchar (SCHEME_A); 2140 c = inchar (SCHEME_A);
2108 2141
2110 return S_F; 2143 return S_F;
2111 2144
2112 switch (state) 2145 switch (state)
2113 { 2146 {
2114 case st_ok: 2147 case st_ok:
2115 switch (c) 2148 if (ecb_expect_false (c == delim))
2116 {
2117 case '\\':
2118 state = st_bsl;
2119 break;
2120
2121 case '"':
2122 *p = 0;
2123 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); 2149 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff);
2124 2150
2125 default: 2151 if (ecb_expect_false (c == '\\'))
2152 state = st_bsl;
2153 else
2126 *p++ = c; 2154 *p++ = c;
2127 break;
2128 }
2129 2155
2130 break; 2156 break;
2131 2157
2132 case st_bsl: 2158 case st_bsl:
2133 switch (c) 2159 switch (c)
2142 case '7': 2168 case '7':
2143 state = st_oct1; 2169 state = st_oct1;
2144 c1 = c - '0'; 2170 c1 = c - '0';
2145 break; 2171 break;
2146 2172
2173 case 'a': *p++ = '\a'; state = st_ok; break;
2174 case 'n': *p++ = '\n'; state = st_ok; break;
2175 case 'r': *p++ = '\r'; state = st_ok; break;
2176 case 't': *p++ = '\t'; state = st_ok; break;
2177
2178 // this overshoots the minimum requirements of r7rs
2179 case ' ':
2180 case '\t':
2181 case '\r':
2182 case '\n':
2183 skipspace (SCHEME_A);
2184 state = st_ok;
2185 break;
2186
2187 //TODO: x should end in ;, not two-digit hex
2147 case 'x': 2188 case 'x':
2148 case 'X': 2189 case 'X':
2149 state = st_x1; 2190 state = st_x1;
2150 c1 = 0; 2191 c1 = 0;
2151 break; 2192 break;
2152 2193
2153 case 'n':
2154 *p++ = '\n';
2155 state = st_ok;
2156 break;
2157
2158 case 't':
2159 *p++ = '\t';
2160 state = st_ok;
2161 break;
2162
2163 case 'r':
2164 *p++ = '\r';
2165 state = st_ok;
2166 break;
2167
2168 case '"':
2169 *p++ = '"';
2170 state = st_ok;
2171 break;
2172
2173 default: 2194 default:
2174 *p++ = c; 2195 *p++ = c;
2175 state = st_ok; 2196 state = st_ok;
2176 break; 2197 break;
2177 } 2198 }
2178 2199
2179 break; 2200 break;
2180 2201
2181 case st_x1: 2202 case st_x1:
2182 case st_x2: 2203 case st_x2:
2183 c = toupper (c); 2204 c = tolower (c);
2184 2205
2185 if (c >= '0' && c <= 'F') 2206 if (c >= '0' && c <= '9')
2186 {
2187 if (c <= '9')
2188 c1 = (c1 << 4) + c - '0'; 2207 c1 = (c1 << 4) + c - '0';
2189 else 2208 else if (c >= 'a' && c <= 'f')
2190 c1 = (c1 << 4) + c - 'A' + 10; 2209 c1 = (c1 << 4) + c - 'a' + 10;
2191
2192 if (state == st_x1)
2193 state = st_x2;
2194 else
2195 {
2196 *p++ = c1;
2197 state = st_ok;
2198 }
2199 }
2200 else 2210 else
2201 return S_F; 2211 return S_F;
2212
2213 if (state == st_x1)
2214 state = st_x2;
2215 else
2216 {
2217 *p++ = c1;
2218 state = st_ok;
2219 }
2202 2220
2203 break; 2221 break;
2204 2222
2205 case st_oct1: 2223 case st_oct1:
2206 case st_oct2: 2224 case st_oct2:
2210 backchar (SCHEME_A_ c); 2228 backchar (SCHEME_A_ c);
2211 state = st_ok; 2229 state = st_ok;
2212 } 2230 }
2213 else 2231 else
2214 { 2232 {
2215 if (state == st_oct2 && c1 >= 32) 2233 if (state == st_oct2 && c1 >= ' ')
2216 return S_F; 2234 return S_F;
2217 2235
2218 c1 = (c1 << 3) + (c - '0'); 2236 c1 = (c1 << 3) + (c - '0');
2219 2237
2220 if (state == st_oct1) 2238 if (state == st_oct1)
2225 state = st_ok; 2243 state = st_ok;
2226 } 2244 }
2227 } 2245 }
2228 2246
2229 break; 2247 break;
2230
2231 } 2248 }
2232 } 2249 }
2233} 2250}
2234 2251
2235/* check c is in chars */ 2252/* check c is in chars */
2236static INLINE int 2253ecb_cold int
2237is_one_of (char *s, int c) 2254is_one_of (const char *s, int c)
2238{ 2255{
2239 if (c == EOF)
2240 return 1;
2241
2242 return !!strchr (s, c); 2256 return c == EOF || !!strchr (s, c);
2243} 2257}
2244 2258
2245/* skip white characters */ 2259/* skip white characters */
2246static INLINE int 2260ecb_cold int
2247skipspace (SCHEME_P) 2261skipspace (SCHEME_P)
2248{ 2262{
2249 int c, curr_line = 0; 2263 int c, curr_line = 0;
2250 2264
2251 do 2265 do
2252 { 2266 {
2253 c = inchar (SCHEME_A); 2267 c = inchar (SCHEME_A);
2268
2254#if SHOW_ERROR_LINE 2269#if SHOW_ERROR_LINE
2255 if (c == '\n') 2270 if (ecb_expect_false (c == '\n'))
2256 curr_line++; 2271 curr_line++;
2257#endif 2272#endif
2273
2274 if (ecb_expect_false (c == EOF))
2275 return c;
2258 } 2276 }
2259 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2277 while (is_one_of (WHITESPACE, c));
2260 2278
2261 /* record it */ 2279 /* record it */
2262#if SHOW_ERROR_LINE 2280#if SHOW_ERROR_LINE
2263 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) 2281 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file)
2264 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; 2282 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line;
2265#endif 2283#endif
2266 2284
2267 if (c != EOF)
2268 {
2269 backchar (SCHEME_A_ c); 2285 backchar (SCHEME_A_ c);
2270 return 1; 2286 return 1;
2271 }
2272 else
2273 return EOF;
2274} 2287}
2275 2288
2276/* get token */ 2289/* get token */
2277static int 2290ecb_cold static int
2278token (SCHEME_P) 2291token (SCHEME_P)
2279{ 2292{
2280 int c = skipspace (SCHEME_A); 2293 int c = skipspace (SCHEME_A);
2281 2294
2282 if (c == EOF) 2295 if (c == EOF)
2294 return TOK_RPAREN; 2307 return TOK_RPAREN;
2295 2308
2296 case '.': 2309 case '.':
2297 c = inchar (SCHEME_A); 2310 c = inchar (SCHEME_A);
2298 2311
2299 if (is_one_of (" \n\t", c)) 2312 if (is_one_of (WHITESPACE, c))
2300 return TOK_DOT; 2313 return TOK_DOT;
2301 else 2314 else
2302 { 2315 {
2303 //TODO: ungetc twice in a row is not supported in C
2304 backchar (SCHEME_A_ c); 2316 backchar (SCHEME_A_ c);
2305 backchar (SCHEME_A_ '.');
2306 return TOK_ATOM; 2317 return TOK_DOTATOM;
2307 } 2318 }
2319
2320 case '|':
2321 return TOK_STRATOM;
2308 2322
2309 case '\'': 2323 case '\'':
2310 return TOK_QUOTE; 2324 return TOK_QUOTE;
2311 2325
2312 case ';': 2326 case ';':
2379} 2393}
2380 2394
2381/* ========== Routines for Printing ========== */ 2395/* ========== Routines for Printing ========== */
2382#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) 2396#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL)
2383 2397
2384static void 2398ecb_cold static void
2385printslashstring (SCHEME_P_ char *p, int len) 2399printslashstring (SCHEME_P_ char *p, int len)
2386{ 2400{
2387 int i; 2401 int i;
2388 unsigned char *s = (unsigned char *) p; 2402 unsigned char *s = (unsigned char *) p;
2389 2403
2444 } 2458 }
2445 2459
2446 putcharacter (SCHEME_A_ '"'); 2460 putcharacter (SCHEME_A_ '"');
2447} 2461}
2448 2462
2449
2450/* print atoms */ 2463/* print atoms */
2451static void 2464ecb_cold static void
2452printatom (SCHEME_P_ pointer l, int f) 2465printatom (SCHEME_P_ pointer l, int f)
2453{ 2466{
2454 char *p; 2467 char *p;
2455 int len; 2468 int len;
2456 2469
2457 atom2str (SCHEME_A_ l, f, &p, &len); 2470 atom2str (SCHEME_A_ l, f, &p, &len);
2458 putchars (SCHEME_A_ p, len); 2471 putchars (SCHEME_A_ p, len);
2459} 2472}
2460 2473
2461
2462/* Uses internal buffer unless string pointer is already available */ 2474/* Uses internal buffer unless string pointer is already available */
2463static void 2475ecb_cold static void
2464atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2476atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2465{ 2477{
2466 char *p; 2478 char *p;
2467 2479
2468 if (l == NIL) 2480 if (l == NIL)
2479 { 2491 {
2480 p = SCHEME_V->strbuff; 2492 p = SCHEME_V->strbuff;
2481 2493
2482 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2494 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2483 { 2495 {
2484 if (num_is_integer (l)) 2496 if (is_integer (l))
2485 xnum (p, ivalue_unchecked (l)); 2497 xnum (p, ivalue_unchecked (l));
2486#if USE_REAL 2498#if USE_REAL
2487 else 2499 else
2488 { 2500 {
2489 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2501 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2626#endif 2638#endif
2627 } 2639 }
2628 else if (is_continuation (l)) 2640 else if (is_continuation (l))
2629 p = "#<CONTINUATION>"; 2641 p = "#<CONTINUATION>";
2630 else 2642 else
2643 {
2644#if USE_PRINTF
2645 p = SCHEME_V->strbuff;
2646 snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l));
2647#else
2631 p = "#<ERROR>"; 2648 p = "#<ERROR>";
2649#endif
2650 }
2632 2651
2633 *pp = p; 2652 *pp = p;
2634 *plen = strlen (p); 2653 *plen = strlen (p);
2635} 2654}
2636 2655
2668 return car (d); 2687 return car (d);
2669 2688
2670 p = cons (car (d), cdr (d)); 2689 p = cons (car (d), cdr (d));
2671 q = p; 2690 q = p;
2672 2691
2673 while (cdr (cdr (p)) != NIL) 2692 while (cddr (p) != NIL)
2674 { 2693 {
2675 d = cons (car (p), cdr (p)); 2694 d = cons (car (p), cdr (p));
2676 2695
2677 if (cdr (cdr (p)) != NIL) 2696 if (cddr (p) != NIL)
2678 p = cdr (d); 2697 p = cdr (d);
2679 } 2698 }
2680 2699
2681 set_cdr (p, car (cdr (p))); 2700 set_cdr (p, cadr (p));
2682 return q; 2701 return q;
2683} 2702}
2684 2703
2685/* reverse list -- produce new list */ 2704/* reverse list -- produce new list */
2686static pointer 2705ecb_hot static pointer
2687reverse (SCHEME_P_ pointer a) 2706reverse (SCHEME_P_ pointer a)
2688{ 2707{
2689 /* a must be checked by gc */ 2708 /* a must be checked by gc */
2690 pointer p = NIL; 2709 pointer p = NIL;
2691 2710
2694 2713
2695 return p; 2714 return p;
2696} 2715}
2697 2716
2698/* reverse list --- in-place */ 2717/* reverse list --- in-place */
2699static pointer 2718ecb_hot static pointer
2700reverse_in_place (SCHEME_P_ pointer term, pointer list) 2719reverse_in_place (SCHEME_P_ pointer term, pointer list)
2701{ 2720{
2702 pointer result = term; 2721 pointer result = term;
2703 pointer p = list; 2722 pointer p = list;
2704 2723
2712 2731
2713 return result; 2732 return result;
2714} 2733}
2715 2734
2716/* append list -- produce new list (in reverse order) */ 2735/* append list -- produce new list (in reverse order) */
2717static pointer 2736ecb_hot static pointer
2718revappend (SCHEME_P_ pointer a, pointer b) 2737revappend (SCHEME_P_ pointer a, pointer b)
2719{ 2738{
2720 pointer result = a; 2739 pointer result = a;
2721 pointer p = b; 2740 pointer p = b;
2722 2741
2731 2750
2732 return S_F; /* signal an error */ 2751 return S_F; /* signal an error */
2733} 2752}
2734 2753
2735/* equivalence of atoms */ 2754/* equivalence of atoms */
2736int 2755ecb_hot int
2737eqv (pointer a, pointer b) 2756eqv (pointer a, pointer b)
2738{ 2757{
2739 if (is_string (a)) 2758 if (is_string (a))
2740 { 2759 {
2741 if (is_string (b)) 2760 if (is_string (b))
2744 return 0; 2763 return 0;
2745 } 2764 }
2746 else if (is_number (a)) 2765 else if (is_number (a))
2747 { 2766 {
2748 if (is_number (b)) 2767 if (is_number (b))
2749 if (num_is_integer (a) == num_is_integer (b))
2750 return num_cmp (nvalue (a), nvalue (b)) == 0; 2768 return num_cmp (nvalue (a), nvalue (b)) == 0;
2751 2769
2752 return 0; 2770 return 0;
2753 } 2771 }
2754 else if (is_character (a)) 2772 else if (is_character (a))
2755 { 2773 {
2781/* () is #t in R5RS */ 2799/* () is #t in R5RS */
2782#define is_true(p) ((p) != S_F) 2800#define is_true(p) ((p) != S_F)
2783#define is_false(p) ((p) == S_F) 2801#define is_false(p) ((p) == S_F)
2784 2802
2785/* ========== Environment implementation ========== */ 2803/* ========== Environment implementation ========== */
2786
2787#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST)
2788
2789static int
2790hash_fn (const char *key, int table_size)
2791{
2792 const unsigned char *p = key;
2793 uint32_t hash = 2166136261;
2794
2795 while (*p)
2796 hash = (hash ^ *p++) * 16777619;
2797
2798 return hash % table_size;
2799}
2800#endif
2801 2804
2802#ifndef USE_ALIST_ENV 2805#ifndef USE_ALIST_ENV
2803 2806
2804/* 2807/*
2805 * In this implementation, each frame of the environment may be 2808 * In this implementation, each frame of the environment may be
2822 2825
2823 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2826 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2824 setenvironment (SCHEME_V->envir); 2827 setenvironment (SCHEME_V->envir);
2825} 2828}
2826 2829
2827static INLINE void 2830static uint32_t
2831sym_hash (pointer sym, uint32_t size)
2832{
2833 uintptr_t ptr = (uintptr_t)sym;
2834
2835#if 0
2836 /* table size is prime, so why mix */
2837 ptr += ptr >> 32;
2838 ptr += ptr >> 16;
2839 ptr += ptr >> 8;
2840#endif
2841
2842 return ptr % size;
2843}
2844
2845ecb_inline void
2828new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2846new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2829{ 2847{
2830 pointer slot = immutable_cons (variable, value); 2848 pointer slot = immutable_cons (variable, value);
2831 2849
2832 if (is_vector (car (env))) 2850 if (is_vector (car (env)))
2833 { 2851 {
2834 int location = hash_fn (symname (variable), veclength (car (env))); 2852 int location = sym_hash (variable, veclength (car (env)));
2835
2836 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2853 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2837 } 2854 }
2838 else 2855 else
2839 set_car (env, immutable_cons (slot, car (env))); 2856 set_car (env, immutable_cons (slot, car (env)));
2840} 2857}
2841 2858
2842static pointer 2859ecb_hot static pointer
2843find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2860find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2844{ 2861{
2845 pointer x, y; 2862 pointer x, y;
2846 int location;
2847 2863
2848 for (x = env; x != NIL; x = cdr (x)) 2864 for (x = env; x != NIL; x = cdr (x))
2849 { 2865 {
2850 if (is_vector (car (x))) 2866 if (is_vector (car (x)))
2851 { 2867 {
2852 location = hash_fn (symname (hdl), veclength (car (x))); 2868 int location = sym_hash (hdl, veclength (car (x)));
2853 y = vector_elem (car (x), location); 2869 y = vector_get (car (x), location);
2854 } 2870 }
2855 else 2871 else
2856 y = car (x); 2872 y = car (x);
2857 2873
2858 for (; y != NIL; y = cdr (y)) 2874 for (; y != NIL; y = cdr (y))
2859 if (caar (y) == hdl) 2875 if (caar (y) == hdl)
2860 break; 2876 break;
2861 2877
2862 if (y != NIL) 2878 if (y != NIL)
2879 return car (y);
2880
2881 if (!all)
2863 break; 2882 break;
2864
2865 if (!all)
2866 return NIL;
2867 } 2883 }
2868
2869 if (x != NIL)
2870 return car (y);
2871 2884
2872 return NIL; 2885 return NIL;
2873} 2886}
2874 2887
2875#else /* USE_ALIST_ENV */ 2888#else /* USE_ALIST_ENV */
2876 2889
2877static INLINE void 2890static void
2878new_frame_in_env (SCHEME_P_ pointer old_env) 2891new_frame_in_env (SCHEME_P_ pointer old_env)
2879{ 2892{
2880 SCHEME_V->envir = immutable_cons (NIL, old_env); 2893 SCHEME_V->envir = immutable_cons (NIL, old_env);
2881 setenvironment (SCHEME_V->envir); 2894 setenvironment (SCHEME_V->envir);
2882} 2895}
2883 2896
2884static INLINE void 2897static void
2885new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2898new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2886{ 2899{
2887 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2900 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2888} 2901}
2889 2902
2890static pointer 2903ecb_hot static pointer
2891find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2904find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2892{ 2905{
2893 pointer x, y; 2906 pointer x, y;
2894 2907
2895 for (x = env; x != NIL; x = cdr (x)) 2908 for (x = env; x != NIL; x = cdr (x))
2897 for (y = car (x); y != NIL; y = cdr (y)) 2910 for (y = car (x); y != NIL; y = cdr (y))
2898 if (caar (y) == hdl) 2911 if (caar (y) == hdl)
2899 break; 2912 break;
2900 2913
2901 if (y != NIL) 2914 if (y != NIL)
2915 return car (y);
2902 break; 2916 break;
2903 2917
2904 if (!all) 2918 if (!all)
2905 return NIL; 2919 break;
2906 } 2920 }
2907
2908 if (x != NIL)
2909 return car (y);
2910 2921
2911 return NIL; 2922 return NIL;
2912} 2923}
2913 2924
2914#endif /* USE_ALIST_ENV else */ 2925#endif /* USE_ALIST_ENV else */
2915 2926
2916static INLINE void 2927static void
2917new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2928new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2918{ 2929{
2930 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2919 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2931 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2920} 2932}
2921 2933
2922static INLINE void 2934static void
2923set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2935set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2924{ 2936{
2925 set_cdr (slot, value); 2937 set_cdr (slot, value);
2926} 2938}
2927 2939
2928static INLINE pointer 2940static pointer
2929slot_value_in_env (pointer slot) 2941slot_value_in_env (pointer slot)
2930{ 2942{
2931 return cdr (slot); 2943 return cdr (slot);
2932} 2944}
2933 2945
2934/* ========== Evaluation Cycle ========== */ 2946/* ========== Evaluation Cycle ========== */
2935 2947
2936static pointer 2948ecb_cold static int
2937xError_1 (SCHEME_P_ const char *s, pointer a) 2949xError_1 (SCHEME_P_ const char *s, pointer a)
2938{ 2950{
2939#if USE_ERROR_HOOK
2940 pointer x;
2941 pointer hdl = SCHEME_V->ERROR_HOOK;
2942#endif
2943
2944#if USE_PRINTF 2951#if USE_PRINTF
2945#if SHOW_ERROR_LINE 2952#if SHOW_ERROR_LINE
2946 char sbuf[STRBUFFSIZE]; 2953 char sbuf[STRBUFFSIZE];
2947 2954
2948 /* make sure error is not in REPL */ 2955 /* make sure error is not in REPL */
2963 } 2970 }
2964#endif 2971#endif
2965#endif 2972#endif
2966 2973
2967#if USE_ERROR_HOOK 2974#if USE_ERROR_HOOK
2968 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); 2975 pointer x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->ERROR_HOOK, 1);
2969 2976
2970 if (x != NIL) 2977 if (x != NIL)
2971 { 2978 {
2972 pointer code = a 2979 pointer code = a
2973 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) 2980 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL)
2976 code = cons (mk_string (SCHEME_A_ s), code); 2983 code = cons (mk_string (SCHEME_A_ s), code);
2977 setimmutable (car (code)); 2984 setimmutable (car (code));
2978 SCHEME_V->code = cons (slot_value_in_env (x), code); 2985 SCHEME_V->code = cons (slot_value_in_env (x), code);
2979 SCHEME_V->op = OP_EVAL; 2986 SCHEME_V->op = OP_EVAL;
2980 2987
2981 return S_T; 2988 return 0;
2982 } 2989 }
2983#endif 2990#endif
2984 2991
2985 if (a) 2992 if (a)
2986 SCHEME_V->args = cons (a, NIL); 2993 SCHEME_V->args = cons (a, NIL);
2988 SCHEME_V->args = NIL; 2995 SCHEME_V->args = NIL;
2989 2996
2990 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); 2997 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args);
2991 setimmutable (car (SCHEME_V->args)); 2998 setimmutable (car (SCHEME_V->args));
2992 SCHEME_V->op = OP_ERR0; 2999 SCHEME_V->op = OP_ERR0;
3000
2993 return S_T; 3001 return 0;
2994} 3002}
2995 3003
2996#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) 3004#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a)
2997#define Error_0(s) Error_1 (s, 0) 3005#define Error_0(s) Error_1 (s, 0)
2998 3006
2999/* Too small to turn into function */ 3007/* Too small to turn into function */
3000#define BEGIN do { 3008#define BEGIN do {
3001#define END } while (0) 3009#define END } while (0)
3002#define s_goto(a) BEGIN \ 3010#define s_goto(a) BEGIN \
3003 SCHEME_V->op = a; \ 3011 SCHEME_V->op = a; \
3004 return S_T; END 3012 return 0; END
3005 3013
3006#define s_return(a) return xs_return (SCHEME_A_ a) 3014#define s_return(a) return xs_return (SCHEME_A_ a)
3007 3015
3008#ifndef USE_SCHEME_STACK 3016#ifndef USE_SCHEME_STACK
3009 3017
3016 pointer code; 3024 pointer code;
3017}; 3025};
3018 3026
3019# define STACK_GROWTH 3 3027# define STACK_GROWTH 3
3020 3028
3021static void 3029ecb_hot static void
3022s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3030s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3023{ 3031{
3024 int nframes = (uintptr_t)SCHEME_V->dump; 3032 int nframes = (uintptr_t)SCHEME_V->dump;
3025 struct dump_stack_frame *next_frame; 3033 struct dump_stack_frame *next_frame;
3026 3034
3027 /* enough room for the next frame? */ 3035 /* enough room for the next frame? */
3028 if (nframes >= SCHEME_V->dump_size) 3036 if (ecb_expect_false (nframes >= SCHEME_V->dump_size))
3029 { 3037 {
3030 SCHEME_V->dump_size += STACK_GROWTH; 3038 SCHEME_V->dump_size += STACK_GROWTH;
3031 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); 3039 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size);
3032 } 3040 }
3033 3041
3039 next_frame->code = code; 3047 next_frame->code = code;
3040 3048
3041 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3049 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3042} 3050}
3043 3051
3044static pointer 3052static ecb_hot int
3045xs_return (SCHEME_P_ pointer a) 3053xs_return (SCHEME_P_ pointer a)
3046{ 3054{
3047 int nframes = (uintptr_t)SCHEME_V->dump; 3055 int nframes = (uintptr_t)SCHEME_V->dump;
3048 struct dump_stack_frame *frame; 3056 struct dump_stack_frame *frame;
3049 3057
3050 SCHEME_V->value = a; 3058 SCHEME_V->value = a;
3051 3059
3052 if (nframes <= 0) 3060 if (nframes <= 0)
3053 return NIL; 3061 return -1;
3054 3062
3055 frame = &SCHEME_V->dump_base[--nframes]; 3063 frame = &SCHEME_V->dump_base[--nframes];
3056 SCHEME_V->op = frame->op; 3064 SCHEME_V->op = frame->op;
3057 SCHEME_V->args = frame->args; 3065 SCHEME_V->args = frame->args;
3058 SCHEME_V->envir = frame->envir; 3066 SCHEME_V->envir = frame->envir;
3059 SCHEME_V->code = frame->code; 3067 SCHEME_V->code = frame->code;
3060 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3068 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3061 3069
3062 return S_T; 3070 return 0;
3063} 3071}
3064 3072
3065static INLINE void 3073ecb_cold void
3066dump_stack_reset (SCHEME_P) 3074dump_stack_reset (SCHEME_P)
3067{ 3075{
3068 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3076 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3069 SCHEME_V->dump = (pointer)+0; 3077 SCHEME_V->dump = (pointer)+0;
3070} 3078}
3071 3079
3072static INLINE void 3080ecb_cold void
3073dump_stack_initialize (SCHEME_P) 3081dump_stack_initialize (SCHEME_P)
3074{ 3082{
3075 SCHEME_V->dump_size = 0; 3083 SCHEME_V->dump_size = 0;
3076 SCHEME_V->dump_base = 0; 3084 SCHEME_V->dump_base = 0;
3077 dump_stack_reset (SCHEME_A); 3085 dump_stack_reset (SCHEME_A);
3078} 3086}
3079 3087
3080static void 3088ecb_cold static void
3081dump_stack_free (SCHEME_P) 3089dump_stack_free (SCHEME_P)
3082{ 3090{
3083 free (SCHEME_V->dump_base); 3091 free (SCHEME_V->dump_base);
3084 SCHEME_V->dump_base = 0; 3092 SCHEME_V->dump_base = 0;
3085 SCHEME_V->dump = (pointer)0; 3093 SCHEME_V->dump = (pointer)0;
3086 SCHEME_V->dump_size = 0; 3094 SCHEME_V->dump_size = 0;
3087} 3095}
3088 3096
3089static void 3097ecb_cold static void
3090dump_stack_mark (SCHEME_P) 3098dump_stack_mark (SCHEME_P)
3091{ 3099{
3092 int nframes = (uintptr_t)SCHEME_V->dump; 3100 int nframes = (uintptr_t)SCHEME_V->dump;
3093 int i; 3101 int i;
3094 3102
3100 mark (frame->envir); 3108 mark (frame->envir);
3101 mark (frame->code); 3109 mark (frame->code);
3102 } 3110 }
3103} 3111}
3104 3112
3105static pointer 3113ecb_cold static pointer
3106ss_get_cont (SCHEME_P) 3114ss_get_cont (SCHEME_P)
3107{ 3115{
3108 int nframes = (uintptr_t)SCHEME_V->dump; 3116 int nframes = (uintptr_t)SCHEME_V->dump;
3109 int i; 3117 int i;
3110 3118
3122 } 3130 }
3123 3131
3124 return cont; 3132 return cont;
3125} 3133}
3126 3134
3127static void 3135ecb_cold static void
3128ss_set_cont (SCHEME_P_ pointer cont) 3136ss_set_cont (SCHEME_P_ pointer cont)
3129{ 3137{
3130 int i = 0; 3138 int i = 0;
3131 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3139 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3132 3140
3133 while (cont != NIL) 3141 while (cont != NIL)
3134 { 3142 {
3135 frame->op = ivalue (car (cont)); cont = cdr (cont); 3143 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3136 frame->args = car (cont) ; cont = cdr (cont); 3144 frame->args = car (cont) ; cont = cdr (cont);
3137 frame->envir = car (cont) ; cont = cdr (cont); 3145 frame->envir = car (cont) ; cont = cdr (cont);
3138 frame->code = car (cont) ; cont = cdr (cont); 3146 frame->code = car (cont) ; cont = cdr (cont);
3139 3147
3140 ++frame; 3148 ++frame;
3141 ++i; 3149 ++i;
3142 } 3150 }
3143 3151
3144 SCHEME_V->dump = (pointer)(uintptr_t)i; 3152 SCHEME_V->dump = (pointer)(uintptr_t)i;
3145} 3153}
3146 3154
3147#else 3155#else
3148 3156
3149static INLINE void 3157ecb_cold void
3150dump_stack_reset (SCHEME_P) 3158dump_stack_reset (SCHEME_P)
3151{ 3159{
3152 SCHEME_V->dump = NIL; 3160 SCHEME_V->dump = NIL;
3153} 3161}
3154 3162
3155static INLINE void 3163ecb_cold void
3156dump_stack_initialize (SCHEME_P) 3164dump_stack_initialize (SCHEME_P)
3157{ 3165{
3158 dump_stack_reset (SCHEME_A); 3166 dump_stack_reset (SCHEME_A);
3159} 3167}
3160 3168
3161static void 3169ecb_cold static void
3162dump_stack_free (SCHEME_P) 3170dump_stack_free (SCHEME_P)
3163{ 3171{
3164 SCHEME_V->dump = NIL; 3172 SCHEME_V->dump = NIL;
3165} 3173}
3166 3174
3167static pointer 3175ecb_hot static int
3168xs_return (SCHEME_P_ pointer a) 3176xs_return (SCHEME_P_ pointer a)
3169{ 3177{
3170 pointer dump = SCHEME_V->dump; 3178 pointer dump = SCHEME_V->dump;
3171 3179
3172 SCHEME_V->value = a; 3180 SCHEME_V->value = a;
3173 3181
3174 if (dump == NIL) 3182 if (dump == NIL)
3175 return NIL; 3183 return -1;
3176 3184
3177 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3185 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3178 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3186 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3179 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3187 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3180 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3188 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3181 3189
3182 SCHEME_V->dump = dump; 3190 SCHEME_V->dump = dump;
3183 3191
3184 return S_T; 3192 return 0;
3185} 3193}
3186 3194
3187static void 3195ecb_hot static void
3188s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3196s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3189{ 3197{
3190 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), 3198 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op),
3191 cons (args, 3199 cons (args,
3192 cons (SCHEME_V->envir, 3200 cons (SCHEME_V->envir,
3193 cons (code, 3201 cons (code,
3194 SCHEME_V->dump)))); 3202 SCHEME_V->dump))));
3195} 3203}
3196 3204
3205ecb_cold static void
3206dump_stack_mark (SCHEME_P)
3207{
3208 mark (SCHEME_V->dump);
3209}
3210
3211ecb_cold static pointer
3212ss_get_cont (SCHEME_P)
3213{
3214 return SCHEME_V->dump;
3215}
3216
3217ecb_cold static void
3218ss_set_cont (SCHEME_P_ pointer cont)
3219{
3220 SCHEME_V->dump = cont;
3221}
3222
3223#endif
3224
3225#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3226
3227#if EXPERIMENT
3228
3229static int
3230dtree (SCHEME_P_ int indent, pointer x)
3231{
3232 int c;
3233
3234 if (is_syntax (x))
3235 {
3236 printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x));
3237 return 8 + 8;
3238 }
3239
3240 if (x == NIL)
3241 {
3242 printf ("%*sNIL\n", indent, "");
3243 return 3;
3244 }
3245
3246 switch (type (x))
3247 {
3248 case T_INTEGER:
3249 printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x);
3250 return 32+8;
3251
3252 case T_SYMBOL:
3253 printf ("%*sS<%s>\n", indent, "", symname (x));
3254 return 24+8;
3255
3256 case T_CLOSURE:
3257 printf ("%*sS<%s>\n", indent, "", "closure");
3258 dtree (SCHEME_A_ indent + 3, cdr(x));
3259 return 32 + dtree (SCHEME_A_ indent + 3, car (x));
3260
3261 case T_PAIR:
3262 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3263 c = dtree (SCHEME_A_ indent + 3, car (x));
3264 c += dtree (SCHEME_A_ indent + 3, cdr (x));
3265 return c + 1;
3266
3267 case T_PORT:
3268 printf ("%*sS<%s>\n", indent, "", "port");
3269 return 24+8;
3270
3271 case T_VECTOR:
3272 printf ("%*sS<%s>\n", indent, "", "vector");
3273 return 24+8;
3274
3275 case T_ENVIRONMENT:
3276 printf ("%*sS<%s>\n", indent, "", "environment");
3277 return 0 + dtree (SCHEME_A_ indent + 3, car (x));
3278
3279 default:
3280 printf ("unhandled type %d\n", type (x));
3281 break;
3282 }
3283}
3284
3285#define DUMP(t) do { printf ("DUMP %s:%d\n", __FILE__, __LINE__); dtree (SCHEME_A_ 0, (t)); } while (0)
3286
3287typedef void *stream[1];
3288
3289#define stream_init() { 0 }
3290#define stream_data(s) ((char *)(s)[0] + sizeof (uint32_t) * 2)
3291#define stream_size(s) (((uint32_t *)(s)[0])[0] - sizeof (uint32_t) * 2)
3292#define stream_free(s) free (s[0])
3293
3294ecb_cold static void
3295stream_put (stream s, uint8_t byte)
3296{
3297 uint32_t *sp = *s;
3298 uint32_t size = sizeof (uint32_t) * 2;
3299 uint32_t offs = size;
3300
3301 if (ecb_expect_true (sp))
3302 {
3303 offs = sp[0];
3304 size = sp[1];
3305 }
3306
3307 if (ecb_expect_false (offs == size))
3308 {
3309 size *= 2;
3310 sp = realloc (sp, size);
3311 *s = sp;
3312 sp[1] = size;
3313
3314 }
3315
3316 ((uint8_t *)sp)[offs++] = byte;
3317 sp[0] = offs;
3318}
3319
3320ecb_cold static void
3321stream_put_v (stream s, uint32_t v)
3322{
3323 while (v > 0x7f)
3324 {
3325 stream_put (s, v | 0x80);
3326 v >>= 7;
3327 }
3328
3329 stream_put (s, v);
3330}
3331
3332ecb_cold static void
3333stream_put_tv (stream s, int bop, uint32_t v)
3334{
3335 printf ("put tv %d %d\n", bop, v);//D
3336 stream_put (s, bop);
3337 stream_put_v (s, v);
3338}
3339
3340ecb_cold static void
3341stream_put_stream (stream s, stream o)
3342{
3343 uint32_t i;
3344
3345 for (i = 0; i < stream_size (o); ++i)
3346 stream_put (s, stream_data (o)[i]);
3347
3348 stream_free (o);
3349}
3350
3351ecb_cold static uint32_t
3352cell_id (SCHEME_P_ pointer x)
3353{
3354 struct cell *p = CELL (x);
3355 int i;
3356
3357 for (i = SCHEME_V->last_cell_seg; i >= 0; --i)
3358 if (SCHEME_V->cell_seg[i] <= p && p < SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize[i])
3359 return i | ((p - SCHEME_V->cell_seg[i]) << CELL_NSEGMENT_LOG);
3360
3361 abort ();
3362}
3363
3364// calculates a (preferably small) integer that makes it possible to find
3365// the symbol again. if pointers were offsets into a memory area... until
3366// then, we return segment number in the low bits, and offset in the high
3367// bits.
3368// also, this function must never return 0.
3369ecb_cold static uint32_t
3370symbol_id (SCHEME_P_ pointer sym)
3371{
3372 return cell_id (SCHEME_A_ sym);
3373}
3374
3375enum byteop
3376{
3377 BOP_NIL,
3378 BOP_INTEGER,
3379 BOP_SYMBOL,
3380 BOP_DATUM,
3381 BOP_LIST_BEG,
3382 BOP_LIST_END,
3383 BOP_IF,
3384 BOP_AND,
3385 BOP_OR,
3386 BOP_CASE,
3387 BOP_COND,
3388 BOP_LET,
3389 BOP_LETAST,
3390 BOP_LETREC,
3391 BOP_DEFINE,
3392 BOP_MACRO,
3393 BOP_SET,
3394 BOP_BEGIN,
3395 BOP_LAMBDA,
3396 BOP_OP,
3397};
3398
3399ecb_cold static void compile_expr (SCHEME_P_ stream s, pointer x);
3400
3401ecb_cold static void
3402compile_list (SCHEME_P_ stream s, pointer x)
3403{
3404 // TODO: improper list
3405
3406 for (; x != NIL; x = cdr (x))
3407 {
3408 stream t = stream_init ();
3409 compile_expr (SCHEME_A_ t, car (x));
3410 stream_put_v (s, stream_size (t));
3411 stream_put_stream (s, t);
3412 }
3413
3414 stream_put_v (s, 0);
3415}
3416
3197static void 3417static void
3198dump_stack_mark (SCHEME_P) 3418compile_if (SCHEME_P_ stream s, pointer cond, pointer ift, pointer iff)
3199{ 3419{
3200 mark (SCHEME_V->dump); 3420 stream sift = stream_init (); compile_expr (SCHEME_A_ sift, ift);
3421
3422 stream_put (s, BOP_IF);
3423 compile_expr (SCHEME_A_ s, cond);
3424 stream_put_v (s, stream_size (sift));
3425 stream_put_stream (s, sift);
3426 compile_expr (SCHEME_A_ s, iff);
3427}
3428
3429typedef uint32_t stream_fixup;
3430
3431static stream_fixup
3432stream_put_fixup (stream s)
3433{
3434 stream_put (s, 0);
3435 stream_put (s, 0);
3436
3437 return stream_size (s);
3438}
3439
3440static void
3441stream_fix_fixup (stream s, stream_fixup fixup, uint32_t target)
3442{
3443 target -= fixup;
3444 assert (target < (1 << 14));
3445 stream_data (s)[fixup - 2] = target | 0x80;
3446 stream_data (s)[fixup - 1] = target >> 7;
3447}
3448
3449static void
3450compile_and_or (SCHEME_P_ stream s, int and, pointer x)
3451{
3452 for (; cdr (x) != NIL; x = cdr (x))
3453 {
3454 stream t = stream_init ();
3455 compile_expr (SCHEME_A_ t, car (x));
3456 stream_put_v (s, stream_size (t));
3457 stream_put_stream (s, t);
3458 }
3459
3460 stream_put_v (s, 0);
3461}
3462
3463static void
3464compile_case (SCHEME_P_ stream s, pointer x)
3465{
3466 compile_expr (SCHEME_A_ s, caar (x));
3467
3468 for (;;)
3469 {
3470 x = cdr (x);
3471
3472 if (x == NIL)
3473 break;
3474
3475 compile_expr (SCHEME_A_ s, caar (x));
3476 stream t = stream_init (); compile_expr (SCHEME_A_ t, cdar (x));
3477 stream_put_v (s, stream_size (t));
3478 stream_put_stream (s, t);
3479 }
3480
3481 stream_put_v (s, 0);
3482}
3483
3484static void
3485compile_cond (SCHEME_P_ stream s, pointer x)
3486{
3487 for ( ; x != NIL; x = cdr (x))
3488 {
3489 compile_expr (SCHEME_A_ s, caar (x));
3490 stream t = stream_init (); compile_expr (SCHEME_A_ t, cdar (x));
3491 stream_put_v (s, stream_size (t));
3492 stream_put_stream (s, t);
3493 }
3494
3495 stream_put_v (s, 0);
3201} 3496}
3202 3497
3203static pointer 3498static pointer
3204ss_get_cont (SCHEME_P) 3499lookup (SCHEME_P_ pointer x)
3205{ 3500{
3206 return SCHEME_V->dump; 3501 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, x, 1);
3207}
3208 3502
3209static void 3503 if (x != NIL)
3210ss_set_cont (SCHEME_P_ pointer cont) 3504 x = slot_value_in_env (x);
3211{
3212 SCHEME_V->dump = cont;
3213}
3214 3505
3215#endif 3506 return x;
3507}
3216 3508
3217#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3509ecb_cold static void
3510compile_expr (SCHEME_P_ stream s, pointer x)
3511{
3512 if (x == NIL)
3513 {
3514 stream_put (s, BOP_NIL);
3515 return;
3516 }
3218 3517
3219static pointer 3518 if (is_pair (x))
3519 {
3520 pointer head = car (x);
3521
3522 if (is_syntax (head))
3523 {
3524 x = cdr (x);
3525
3526 switch (syntaxnum (head))
3527 {
3528 case OP_IF0: /* if */
3529 stream_put_v (s, BOP_IF);
3530 compile_if (SCHEME_A_ s, car (x), cadr (x), caddr (x));
3531 break;
3532
3533 case OP_OR0: /* or */
3534 stream_put_v (s, BOP_OR);
3535 compile_and_or (SCHEME_A_ s, 0, x);
3536 break;
3537
3538 case OP_AND0: /* and */
3539 stream_put_v (s, BOP_AND);
3540 compile_and_or (SCHEME_A_ s, 1, x);
3541 break;
3542
3543 case OP_CASE0: /* case */
3544 stream_put_v (s, BOP_CASE);
3545 compile_case (SCHEME_A_ s, x);
3546 break;
3547
3548 case OP_COND0: /* cond */
3549 stream_put_v (s, BOP_COND);
3550 compile_cond (SCHEME_A_ s, x);
3551 break;
3552
3553 case OP_LET0: /* let */
3554 case OP_LET0AST: /* let* */
3555 case OP_LET0REC: /* letrec */
3556 switch (syntaxnum (head))
3557 {
3558 case OP_LET0: stream_put (s, BOP_LET ); break;
3559 case OP_LET0AST: stream_put (s, BOP_LETAST); break;
3560 case OP_LET0REC: stream_put (s, BOP_LETREC); break;
3561 }
3562
3563 {
3564 pointer bindings = car (x);
3565 pointer body = cadr (x);
3566
3567 for (x = bindings; x != NIL; x = cdr (x))
3568 {
3569 pointer init = NIL;
3570 pointer var = car (x);
3571
3572 if (is_pair (var))
3573 {
3574 init = cdr (var);
3575 var = car (var);
3576 }
3577
3578 stream_put_v (s, symbol_id (SCHEME_A_ var));
3579 compile_expr (SCHEME_A_ s, init);
3580 }
3581
3582 stream_put_v (s, 0);
3583 compile_expr (SCHEME_A_ s, body);
3584 }
3585 break;
3586
3587 case OP_DEF0: /* define */
3588 case OP_MACRO0: /* macro */
3589 stream_put (s, syntaxnum (head) == OP_DEF0 ? BOP_DEFINE : BOP_MACRO);
3590 stream_put_v (s, cell_id (SCHEME_A_ car (x)));
3591 compile_expr (SCHEME_A_ s, cadr (x));
3592 break;
3593
3594 case OP_SET0: /* set! */
3595 stream_put (s, BOP_SET);
3596 stream_put_v (s, symbol_id (SCHEME_A_ car (x)));
3597 compile_expr (SCHEME_A_ s, cadr (x));
3598 break;
3599
3600 case OP_BEGIN: /* begin */
3601 stream_put (s, BOP_BEGIN);
3602 compile_list (SCHEME_A_ s, x);
3603 return;
3604
3605 case OP_DELAY: /* delay */
3606 abort ();
3607 break;
3608
3609 case OP_QUOTE: /* quote */
3610 stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x));
3611 break;
3612
3613 case OP_LAMBDA: /* lambda */
3614 {
3615 pointer formals = car (x);
3616 pointer body = cadr (x);
3617
3618 stream_put (s, BOP_LAMBDA);
3619
3620 for (; is_pair (formals); formals = cdr (formals))
3621 stream_put_v (s, symbol_id (SCHEME_A_ car (formals)));
3622
3623 stream_put_v (s, 0);
3624 stream_put_v (s, formals == NIL ? 0 : symbol_id (SCHEME_A_ formals));
3625
3626 compile_expr (SCHEME_A_ s, body);
3627 }
3628 break;
3629
3630 case OP_C0STREAM:/* cons-stream */
3631 abort ();
3632 break;
3633 }
3634
3635 return;
3636 }
3637
3638 pointer m = lookup (SCHEME_A_ head);
3639
3640 if (is_macro (m))
3641 {
3642 s_save (SCHEME_A_ OP_DEBUG2, SCHEME_V->args, SCHEME_V->code);
3643 SCHEME_V->code = m;
3644 SCHEME_V->args = cons (x, NIL);
3645 Eval_Cycle (SCHEME_A_ OP_APPLY);
3646 x = SCHEME_V->value;
3647 compile_expr (SCHEME_A_ s, SCHEME_V->value);
3648 return;
3649 }
3650
3651 stream_put (s, BOP_LIST_BEG);
3652
3653 for (; x != NIL; x = cdr (x))
3654 compile_expr (SCHEME_A_ s, car (x));
3655
3656 stream_put (s, BOP_LIST_END);
3657 return;
3658 }
3659
3660 switch (type (x))
3661 {
3662 case T_INTEGER:
3663 {
3664 IVALUE iv = ivalue_unchecked (x);
3665 iv = iv < 0 ? ((~(uint32_t)iv) << 1) | 1 : (uint32_t)iv << 1;
3666 stream_put_tv (s, BOP_INTEGER, iv);
3667 }
3668 return;
3669
3670 case T_SYMBOL:
3671 if (0)
3672 {
3673 // no can do without more analysis
3674 pointer m = lookup (SCHEME_A_ x);
3675
3676 if (is_proc (m))
3677 {
3678 printf ("compile proc %s %d\n", procname(m), procnum(m));
3679 stream_put_tv (s, BOP_SYMBOL, BOP_OP + procnum (m));
3680 }
3681 else
3682 stream_put_tv (s, BOP_SYMBOL, symbol_id (SCHEME_A_ x));
3683 }
3684
3685 stream_put_tv (s, BOP_SYMBOL, symbol_id (SCHEME_A_ x));
3686 return;
3687
3688 default:
3689 stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x));
3690 break;
3691 }
3692}
3693
3694ecb_cold static int
3695compile_closure (SCHEME_P_ pointer p)
3696{
3697 stream s = stream_init ();
3698
3699 compile_list (SCHEME_A_ s, cdar (p));
3700
3701 FILE *xxd = popen ("xxd", "we");
3702 fwrite (stream_data (s), 1, stream_size (s), xxd);
3703 fclose (xxd);
3704
3705 return stream_size (s);
3706}
3707
3708#endif
3709
3710/* syntax, eval, core, ... */
3711ecb_hot static int
3220opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3712opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3221{ 3713{
3222 pointer args = SCHEME_V->args; 3714 pointer args = SCHEME_V->args;
3223 pointer x, y; 3715 pointer x, y;
3224 3716
3225 switch (op) 3717 switch (op)
3226 { 3718 {
3719#if EXPERIMENT //D
3720 case OP_DEBUG:
3721 {
3722 uint32_t len = compile_closure (SCHEME_A_ car (args));
3723 printf ("len = %d\n", len);
3724 printf ("\n");
3725 s_return (S_T);
3726 }
3727
3728 case OP_DEBUG2:
3729 return -1;
3730#endif
3731
3227 case OP_LOAD: /* load */ 3732 case OP_LOAD: /* load */
3228 if (file_interactive (SCHEME_A)) 3733 if (file_interactive (SCHEME_A))
3229 { 3734 {
3230 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); 3735 putstr (SCHEME_A_ "Loading ");
3231 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); 3736 putstr (SCHEME_A_ strvalue (car (args)));
3737 putcharacter (SCHEME_A_ '\n');
3232 } 3738 }
3233 3739
3234 if (!file_push (SCHEME_A_ strvalue (car (args)))) 3740 if (!file_push (SCHEME_A_ strvalue (car (args))))
3235 Error_1 ("unable to open", car (args)); 3741 Error_1 ("unable to open", car (args));
3236 else 3742
3237 {
3238 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3743 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3239 s_goto (OP_T0LVL); 3744 s_goto (OP_T0LVL);
3240 }
3241 3745
3242 case OP_T0LVL: /* top level */ 3746 case OP_T0LVL: /* top level */
3243 3747
3244 /* If we reached the end of file, this loop is done. */ 3748 /* If we reached the end of file, this loop is done. */
3245 if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) 3749 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3246 { 3750 {
3247 if (SCHEME_V->file_i == 0) 3751 if (SCHEME_V->file_i == 0)
3248 { 3752 {
3249 SCHEME_V->args = NIL; 3753 SCHEME_V->args = NIL;
3250 s_goto (OP_QUIT); 3754 s_goto (OP_QUIT);
3261 /* If interactive, be nice to user. */ 3765 /* If interactive, be nice to user. */
3262 if (file_interactive (SCHEME_A)) 3766 if (file_interactive (SCHEME_A))
3263 { 3767 {
3264 SCHEME_V->envir = SCHEME_V->global_env; 3768 SCHEME_V->envir = SCHEME_V->global_env;
3265 dump_stack_reset (SCHEME_A); 3769 dump_stack_reset (SCHEME_A);
3266 putstr (SCHEME_A_ "\n"); 3770 putcharacter (SCHEME_A_ '\n');
3771#if EXPERIMENT
3772 system ("ps v $PPID");
3773#endif
3267 putstr (SCHEME_A_ prompt); 3774 putstr (SCHEME_A_ prompt);
3268 } 3775 }
3269 3776
3270 /* Set up another iteration of REPL */ 3777 /* Set up another iteration of REPL */
3271 SCHEME_V->nesting = 0; 3778 SCHEME_V->nesting = 0;
3306 { 3813 {
3307 SCHEME_V->print_flag = 1; 3814 SCHEME_V->print_flag = 1;
3308 SCHEME_V->args = SCHEME_V->value; 3815 SCHEME_V->args = SCHEME_V->value;
3309 s_goto (OP_P0LIST); 3816 s_goto (OP_P0LIST);
3310 } 3817 }
3311 else 3818
3312 s_return (SCHEME_V->value); 3819 s_return (SCHEME_V->value);
3313 3820
3314 case OP_EVAL: /* main part of evaluation */ 3821 case OP_EVAL: /* main part of evaluation */
3315#if USE_TRACING 3822#if USE_TRACING
3316 if (SCHEME_V->tracing) 3823 if (SCHEME_V->tracing)
3317 { 3824 {
3328#endif 3835#endif
3329 if (is_symbol (SCHEME_V->code)) /* symbol */ 3836 if (is_symbol (SCHEME_V->code)) /* symbol */
3330 { 3837 {
3331 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); 3838 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1);
3332 3839
3333 if (x != NIL) 3840 if (x == NIL)
3334 s_return (slot_value_in_env (x));
3335 else
3336 Error_1 ("eval: unbound variable:", SCHEME_V->code); 3841 Error_1 ("eval: unbound variable:", SCHEME_V->code);
3842
3843 s_return (slot_value_in_env (x));
3337 } 3844 }
3338 else if (is_pair (SCHEME_V->code)) 3845 else if (is_pair (SCHEME_V->code))
3339 { 3846 {
3340 x = car (SCHEME_V->code); 3847 x = car (SCHEME_V->code);
3341 3848
3350 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ 3857 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */
3351 SCHEME_V->code = x; 3858 SCHEME_V->code = x;
3352 s_goto (OP_EVAL); 3859 s_goto (OP_EVAL);
3353 } 3860 }
3354 } 3861 }
3355 else 3862
3356 s_return (SCHEME_V->code); 3863 s_return (SCHEME_V->code);
3357 3864
3358 case OP_E0ARGS: /* eval arguments */ 3865 case OP_E0ARGS: /* eval arguments */
3359 if (is_macro (SCHEME_V->value)) /* macro expansion */ 3866 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3360 { 3867 {
3361 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3868 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3362 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3869 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3363 SCHEME_V->code = SCHEME_V->value; 3870 SCHEME_V->code = SCHEME_V->value;
3364 s_goto (OP_APPLY); 3871 s_goto (OP_APPLY);
3365 } 3872 }
3366 else 3873
3367 {
3368 SCHEME_V->code = cdr (SCHEME_V->code); 3874 SCHEME_V->code = cdr (SCHEME_V->code);
3369 s_goto (OP_E1ARGS); 3875 s_goto (OP_E1ARGS);
3370 }
3371 3876
3372 case OP_E1ARGS: /* eval arguments */ 3877 case OP_E1ARGS: /* eval arguments */
3373 args = cons (SCHEME_V->value, args); 3878 args = cons (SCHEME_V->value, args);
3374 3879
3375 if (is_pair (SCHEME_V->code)) /* continue */ 3880 if (is_pair (SCHEME_V->code)) /* continue */
3386 SCHEME_V->args = cdr (args); 3891 SCHEME_V->args = cdr (args);
3387 s_goto (OP_APPLY); 3892 s_goto (OP_APPLY);
3388 } 3893 }
3389 3894
3390#if USE_TRACING 3895#if USE_TRACING
3391
3392 case OP_TRACING: 3896 case OP_TRACING:
3393 { 3897 {
3394 int tr = SCHEME_V->tracing; 3898 int tr = SCHEME_V->tracing;
3395 3899
3396 SCHEME_V->tracing = ivalue (car (args)); 3900 SCHEME_V->tracing = ivalue_unchecked (car (args));
3397 s_return (mk_integer (SCHEME_A_ tr)); 3901 s_return (mk_integer (SCHEME_A_ tr));
3398 } 3902 }
3399
3400#endif 3903#endif
3401 3904
3402 case OP_APPLY: /* apply 'code' to 'args' */ 3905 case OP_APPLY: /* apply 'code' to 'args' */
3403#if USE_TRACING 3906#if USE_TRACING
3404 if (SCHEME_V->tracing) 3907 if (SCHEME_V->tracing)
3418 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3921 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3419 else if (is_foreign (SCHEME_V->code)) 3922 else if (is_foreign (SCHEME_V->code))
3420 { 3923 {
3421 /* Keep nested calls from GC'ing the arglist */ 3924 /* Keep nested calls from GC'ing the arglist */
3422 push_recent_alloc (SCHEME_A_ args, NIL); 3925 push_recent_alloc (SCHEME_A_ args, NIL);
3423 x = SCHEME_V->code->object.ff (SCHEME_A_ args); 3926 x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args);
3424 3927
3425 s_return (x); 3928 s_return (x);
3426 } 3929 }
3427 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ 3930 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */
3428 { 3931 {
3458 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ 3961 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */
3459 { 3962 {
3460 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); 3963 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code));
3461 s_return (args != NIL ? car (args) : NIL); 3964 s_return (args != NIL ? car (args) : NIL);
3462 } 3965 }
3463 else 3966
3464 Error_0 ("illegal function"); 3967 Error_0 ("illegal function");
3465 3968
3466 case OP_DOMACRO: /* do macro */ 3969 case OP_DOMACRO: /* do macro */
3467 SCHEME_V->code = SCHEME_V->value; 3970 SCHEME_V->code = SCHEME_V->value;
3468 s_goto (OP_EVAL); 3971 s_goto (OP_EVAL);
3469
3470#if 1
3471 3972
3472 case OP_LAMBDA: /* lambda */ 3973 case OP_LAMBDA: /* lambda */
3473 /* If the hook is defined, apply it to SCHEME_V->code, otherwise 3974 /* If the hook is defined, apply it to SCHEME_V->code, otherwise
3474 set SCHEME_V->value fall thru */ 3975 set SCHEME_V->value fall thru */
3475 { 3976 {
3482 SCHEME_V->code = slot_value_in_env (f); 3983 SCHEME_V->code = slot_value_in_env (f);
3483 s_goto (OP_APPLY); 3984 s_goto (OP_APPLY);
3484 } 3985 }
3485 3986
3486 SCHEME_V->value = SCHEME_V->code; 3987 SCHEME_V->value = SCHEME_V->code;
3487 /* Fallthru */
3488 } 3988 }
3989 /* Fallthru */
3489 3990
3490 case OP_LAMBDA1: 3991 case OP_LAMBDA1:
3491 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); 3992 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir));
3492
3493#else
3494
3495 case OP_LAMBDA: /* lambda */
3496 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir));
3497
3498#endif
3499 3993
3500 case OP_MKCLOSURE: /* make-closure */ 3994 case OP_MKCLOSURE: /* make-closure */
3501 x = car (args); 3995 x = car (args);
3502 3996
3503 if (car (x) == SCHEME_V->LAMBDA) 3997 if (car (x) == SCHEME_V->LAMBDA)
3542 else 4036 else
3543 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); 4037 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value);
3544 4038
3545 s_return (SCHEME_V->code); 4039 s_return (SCHEME_V->code);
3546 4040
3547
3548 case OP_DEFP: /* defined? */ 4041 case OP_DEFP: /* defined? */
3549 x = SCHEME_V->envir; 4042 x = SCHEME_V->envir;
3550 4043
3551 if (cdr (args) != NIL) 4044 if (cdr (args) != NIL)
3552 x = cadr (args); 4045 x = cadr (args);
3570 s_return (SCHEME_V->value); 4063 s_return (SCHEME_V->value);
3571 } 4064 }
3572 else 4065 else
3573 Error_1 ("set!: unbound variable:", SCHEME_V->code); 4066 Error_1 ("set!: unbound variable:", SCHEME_V->code);
3574 4067
3575
3576 case OP_BEGIN: /* begin */ 4068 case OP_BEGIN: /* begin */
3577 if (!is_pair (SCHEME_V->code)) 4069 if (!is_pair (SCHEME_V->code))
3578 s_return (SCHEME_V->code); 4070 s_return (SCHEME_V->code);
3579 4071
3580 if (cdr (SCHEME_V->code) != NIL) 4072 if (cdr (SCHEME_V->code) != NIL)
3591 case OP_IF1: /* if */ 4083 case OP_IF1: /* if */
3592 if (is_true (SCHEME_V->value)) 4084 if (is_true (SCHEME_V->value))
3593 SCHEME_V->code = car (SCHEME_V->code); 4085 SCHEME_V->code = car (SCHEME_V->code);
3594 else 4086 else
3595 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ 4087 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
4088
3596 s_goto (OP_EVAL); 4089 s_goto (OP_EVAL);
3597 4090
3598 case OP_LET0: /* let */ 4091 case OP_LET0: /* let */
3599 SCHEME_V->args = NIL; 4092 SCHEME_V->args = NIL;
3600 SCHEME_V->value = SCHEME_V->code; 4093 SCHEME_V->value = SCHEME_V->code;
3601 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); 4094 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code);
3602 s_goto (OP_LET1); 4095 s_goto (OP_LET1);
3603 4096
3604 case OP_LET1: /* let (calculate parameters) */ 4097 case OP_LET1: /* let (calculate parameters) */
4098 case OP_LET1REC: /* letrec (calculate parameters) */
3605 args = cons (SCHEME_V->value, args); 4099 args = cons (SCHEME_V->value, args);
3606 4100
3607 if (is_pair (SCHEME_V->code)) /* continue */ 4101 if (is_pair (SCHEME_V->code)) /* continue */
3608 { 4102 {
3609 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 4103 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3610 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); 4104 Error_1 ("Bad syntax of binding spec in let/letrec:", car (SCHEME_V->code));
3611 4105
3612 s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code)); 4106 s_save (SCHEME_A_ op, args, cdr (SCHEME_V->code));
3613 SCHEME_V->code = cadar (SCHEME_V->code); 4107 SCHEME_V->code = cadar (SCHEME_V->code);
3614 SCHEME_V->args = NIL; 4108 SCHEME_V->args = NIL;
3615 s_goto (OP_EVAL); 4109 s_goto (OP_EVAL);
3616 } 4110 }
3617 else /* end */ 4111
3618 { 4112 /* end */
3619 args = reverse_in_place (SCHEME_A_ NIL, args); 4113 args = reverse_in_place (SCHEME_A_ NIL, args);
3620 SCHEME_V->code = car (args); 4114 SCHEME_V->code = car (args);
3621 SCHEME_V->args = cdr (args); 4115 SCHEME_V->args = cdr (args);
3622 s_goto (OP_LET2); 4116 s_goto (op == OP_LET1 ? OP_LET2 : OP_LET2REC);
3623 }
3624 4117
3625 case OP_LET2: /* let */ 4118 case OP_LET2: /* let */
3626 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4119 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3627 4120
3628 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args; 4121 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args;
3632 if (is_symbol (car (SCHEME_V->code))) /* named let */ 4125 if (is_symbol (car (SCHEME_V->code))) /* named let */
3633 { 4126 {
3634 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x)) 4127 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x))
3635 { 4128 {
3636 if (!is_pair (x)) 4129 if (!is_pair (x))
3637 Error_1 ("Bad syntax of binding in let :", x); 4130 Error_1 ("Bad syntax of binding in let:", x);
3638 4131
3639 if (!is_list (SCHEME_A_ car (x))) 4132 if (!is_list (SCHEME_A_ car (x)))
3640 Error_1 ("Bad syntax of binding in let :", car (x)); 4133 Error_1 ("Bad syntax of binding in let:", car (x));
3641 4134
3642 args = cons (caar (x), args); 4135 args = cons (caar (x), args);
3643 } 4136 }
3644 4137
3645 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)), 4138 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)),
3662 SCHEME_V->code = cdr (SCHEME_V->code); 4155 SCHEME_V->code = cdr (SCHEME_V->code);
3663 s_goto (OP_BEGIN); 4156 s_goto (OP_BEGIN);
3664 } 4157 }
3665 4158
3666 if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code))) 4159 if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code)))
3667 Error_1 ("Bad syntax of binding spec in let* :", car (SCHEME_V->code)); 4160 Error_1 ("Bad syntax of binding spec in let*:", car (SCHEME_V->code));
3668 4161
3669 s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code)); 4162 s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code));
3670 SCHEME_V->code = car (cdaar (SCHEME_V->code)); 4163 SCHEME_V->code = car (cdaar (SCHEME_V->code));
3671 s_goto (OP_EVAL); 4164 s_goto (OP_EVAL);
3672 4165
3683 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); 4176 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code);
3684 SCHEME_V->code = cadar (SCHEME_V->code); 4177 SCHEME_V->code = cadar (SCHEME_V->code);
3685 SCHEME_V->args = NIL; 4178 SCHEME_V->args = NIL;
3686 s_goto (OP_EVAL); 4179 s_goto (OP_EVAL);
3687 } 4180 }
3688 else /* end */ 4181
4182 /* end */
3689 { 4183
3690 SCHEME_V->code = args; 4184 SCHEME_V->code = args;
3691 SCHEME_V->args = NIL; 4185 SCHEME_V->args = NIL;
3692 s_goto (OP_BEGIN); 4186 s_goto (OP_BEGIN);
3693 }
3694 4187
3695 case OP_LET0REC: /* letrec */ 4188 case OP_LET0REC: /* letrec */
3696 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4189 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3697 SCHEME_V->args = NIL; 4190 SCHEME_V->args = NIL;
3698 SCHEME_V->value = SCHEME_V->code; 4191 SCHEME_V->value = SCHEME_V->code;
3699 SCHEME_V->code = car (SCHEME_V->code); 4192 SCHEME_V->code = car (SCHEME_V->code);
3700 s_goto (OP_LET1REC); 4193 s_goto (OP_LET1REC);
3701 4194
3702 case OP_LET1REC: /* letrec (calculate parameters) */ 4195 /* OP_LET1REC handled by OP_LET1 */
3703 args = cons (SCHEME_V->value, args);
3704
3705 if (is_pair (SCHEME_V->code)) /* continue */
3706 {
3707 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3708 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3709
3710 s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code));
3711 SCHEME_V->code = cadar (SCHEME_V->code);
3712 SCHEME_V->args = NIL;
3713 s_goto (OP_EVAL);
3714 }
3715 else /* end */
3716 {
3717 args = reverse_in_place (SCHEME_A_ NIL, args);
3718 SCHEME_V->code = car (args);
3719 SCHEME_V->args = cdr (args);
3720 s_goto (OP_LET2REC);
3721 }
3722 4196
3723 case OP_LET2REC: /* letrec */ 4197 case OP_LET2REC: /* letrec */
3724 for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y)) 4198 for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y))
3725 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 4199 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3726 4200
3756 } 4230 }
3757 else 4231 else
3758 { 4232 {
3759 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) 4233 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL)
3760 s_return (NIL); 4234 s_return (NIL);
3761 else 4235
3762 {
3763 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 4236 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
3764 SCHEME_V->code = caar (SCHEME_V->code); 4237 SCHEME_V->code = caar (SCHEME_V->code);
3765 s_goto (OP_EVAL); 4238 s_goto (OP_EVAL);
3766 }
3767 } 4239 }
3768 4240
3769 case OP_DELAY: /* delay */ 4241 case OP_DELAY: /* delay */
3770 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); 4242 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir);
3771 set_typeflag (x, T_PROMISE); 4243 set_typeflag (x, T_PROMISE);
3782 case OP_AND1: /* and */ 4254 case OP_AND1: /* and */
3783 if (is_false (SCHEME_V->value)) 4255 if (is_false (SCHEME_V->value))
3784 s_return (SCHEME_V->value); 4256 s_return (SCHEME_V->value);
3785 else if (SCHEME_V->code == NIL) 4257 else if (SCHEME_V->code == NIL)
3786 s_return (SCHEME_V->value); 4258 s_return (SCHEME_V->value);
3787 else 4259
3788 {
3789 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 4260 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
3790 SCHEME_V->code = car (SCHEME_V->code); 4261 SCHEME_V->code = car (SCHEME_V->code);
3791 s_goto (OP_EVAL); 4262 s_goto (OP_EVAL);
3792 }
3793 4263
3794 case OP_OR0: /* or */ 4264 case OP_OR0: /* or */
3795 if (SCHEME_V->code == NIL) 4265 if (SCHEME_V->code == NIL)
3796 s_return (S_F); 4266 s_return (S_F);
3797 4267
3802 case OP_OR1: /* or */ 4272 case OP_OR1: /* or */
3803 if (is_true (SCHEME_V->value)) 4273 if (is_true (SCHEME_V->value))
3804 s_return (SCHEME_V->value); 4274 s_return (SCHEME_V->value);
3805 else if (SCHEME_V->code == NIL) 4275 else if (SCHEME_V->code == NIL)
3806 s_return (SCHEME_V->value); 4276 s_return (SCHEME_V->value);
3807 else 4277
3808 {
3809 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 4278 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
3810 SCHEME_V->code = car (SCHEME_V->code); 4279 SCHEME_V->code = car (SCHEME_V->code);
3811 s_goto (OP_EVAL); 4280 s_goto (OP_EVAL);
3812 }
3813 4281
3814 case OP_C0STREAM: /* cons-stream */ 4282 case OP_C0STREAM: /* cons-stream */
3815 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 4283 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3816 SCHEME_V->code = car (SCHEME_V->code); 4284 SCHEME_V->code = car (SCHEME_V->code);
3817 s_goto (OP_EVAL); 4285 s_goto (OP_EVAL);
3882 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); 4350 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x));
3883 SCHEME_V->code = caar (x); 4351 SCHEME_V->code = caar (x);
3884 s_goto (OP_EVAL); 4352 s_goto (OP_EVAL);
3885 } 4353 }
3886 } 4354 }
3887 else 4355
3888 s_return (NIL); 4356 s_return (NIL);
3889 4357
3890 case OP_CASE2: /* case */ 4358 case OP_CASE2: /* case */
3891 if (is_true (SCHEME_V->value)) 4359 if (is_true (SCHEME_V->value))
3892 s_goto (OP_BEGIN); 4360 s_goto (OP_BEGIN);
3893 else 4361
3894 s_return (NIL); 4362 s_return (NIL);
3895 4363
3896 case OP_PAPPLY: /* apply */ 4364 case OP_PAPPLY: /* apply */
3897 SCHEME_V->code = car (args); 4365 SCHEME_V->code = car (args);
3898 SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); 4366 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
3899 /*SCHEME_V->args = cadr(args); */ 4367 /*SCHEME_V->args = cadr(args); */
3910 SCHEME_V->code = car (args); 4378 SCHEME_V->code = car (args);
3911 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); 4379 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
3912 s_goto (OP_APPLY); 4380 s_goto (OP_APPLY);
3913 } 4381 }
3914 4382
3915 abort (); 4383 if (USE_ERROR_CHECKING) abort ();
3916} 4384}
3917 4385
3918static pointer 4386/* math, cxr */
4387ecb_hot static int
3919opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4388opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3920{ 4389{
3921 pointer args = SCHEME_V->args; 4390 pointer args = SCHEME_V->args;
3922 pointer x = car (args); 4391 pointer x = car (args);
3923 num v; 4392 num v;
3924 4393
3925#if USE_MATH
3926 RVALUE dd;
3927#endif
3928
3929 switch (op) 4394 switch (op)
3930 { 4395 {
3931#if USE_MATH 4396#if USE_MATH
3932 case OP_INEX2EX: /* inexact->exact */ 4397 case OP_INEX2EX: /* inexact->exact */
3933 if (num_is_integer (x)) 4398 if (!is_integer (x))
3934 s_return (x); 4399 {
3935 else if (modf (rvalue_unchecked (x), &dd) == 0) 4400 RVALUE r = rvalue_unchecked (x);
3936 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4401
4402 if (r == (RVALUE)(IVALUE)r)
4403 x = mk_integer (SCHEME_A_ rvalue_unchecked (x));
3937 else 4404 else
3938 Error_1 ("inexact->exact: not integral:", x); 4405 Error_1 ("inexact->exact: not integral:", x);
4406 }
3939 4407
4408 s_return (x);
4409
4410 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4411 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4412 case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x))));
4413 case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x))));
4414
4415 case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
3940 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4416 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
3941 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 4417 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))
4418 / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args))))));
3942 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 4419 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
3943 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 4420 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3944 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); 4421 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x))));
3945 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); 4422 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x))));
3946 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); 4423 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x))));
3947 4424
3948 case OP_ATAN: 4425 case OP_ATAN:
4426 s_return (mk_real (SCHEME_A_
3949 if (cdr (args) == NIL) 4427 cdr (args) == NIL
3950 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 4428 ? atan (rvalue (x))
3951 else 4429 : atan2 (rvalue (x), rvalue (cadr (args)))));
3952 {
3953 pointer y = cadr (args);
3954 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
3955 }
3956
3957 case OP_SQRT:
3958 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
3959 4430
3960 case OP_EXPT: 4431 case OP_EXPT:
3961 { 4432 {
3962 RVALUE result; 4433 RVALUE result;
3963 int real_result = 1; 4434 int real_result = 1;
3964 pointer y = cadr (args); 4435 pointer y = cadr (args);
3965 4436
3966 if (num_is_integer (x) && num_is_integer (y)) 4437 if (is_integer (x) && is_integer (y))
3967 real_result = 0; 4438 real_result = 0;
3968 4439
3969 /* This 'if' is an R5RS compatibility fix. */ 4440 /* This 'if' is an R5RS compatibility fix. */
3970 /* NOTE: Remove this 'if' fix for R6RS. */ 4441 /* NOTE: Remove this 'if' fix for R6RS. */
3971 if (rvalue (x) == 0 && rvalue (y) < 0) 4442 if (rvalue (x) == 0 && rvalue (y) < 0)
3977 /* If the test fails, result is too big for integer. */ 4448 /* If the test fails, result is too big for integer. */
3978 if (!real_result) 4449 if (!real_result)
3979 { 4450 {
3980 long result_as_long = result; 4451 long result_as_long = result;
3981 4452
3982 if (result != (RVALUE) result_as_long) 4453 if (result != result_as_long)
3983 real_result = 1; 4454 real_result = 1;
3984 } 4455 }
3985 4456
3986 if (real_result) 4457 if (real_result)
3987 s_return (mk_real (SCHEME_A_ result)); 4458 s_return (mk_real (SCHEME_A_ result));
3988 else 4459 else
3989 s_return (mk_integer (SCHEME_A_ result)); 4460 s_return (mk_integer (SCHEME_A_ result));
3990 } 4461 }
3991
3992 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
3993 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
3994
3995 case OP_TRUNCATE:
3996 {
3997 RVALUE rvalue_of_x;
3998
3999 rvalue_of_x = rvalue (x);
4000
4001 if (rvalue_of_x > 0)
4002 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x)));
4003 else
4004 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
4005 }
4006
4007 case OP_ROUND:
4008 if (num_is_integer (x))
4009 s_return (x);
4010
4011 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4012#endif 4462#endif
4013 4463
4014 case OP_ADD: /* + */ 4464 case OP_ADD: /* + */
4015 v = num_zero; 4465 v = num_zero;
4016 4466
4017 for (x = args; x != NIL; x = cdr (x)) 4467 for (x = args; x != NIL; x = cdr (x))
4018 v = num_op ('+', v, nvalue (car (x))); 4468 v = num_op (NUM_ADD, v, nvalue (car (x)));
4019 4469
4020 s_return (mk_number (SCHEME_A_ v)); 4470 s_return (mk_number (SCHEME_A_ v));
4021 4471
4022 case OP_MUL: /* * */ 4472 case OP_MUL: /* * */
4023 v = num_one; 4473 v = num_one;
4024 4474
4025 for (x = args; x != NIL; x = cdr (x)) 4475 for (x = args; x != NIL; x = cdr (x))
4026 v = num_op ('+', v, nvalue (car (x))); 4476 v = num_op (NUM_MUL, v, nvalue (car (x)));
4027 4477
4028 s_return (mk_number (SCHEME_A_ v)); 4478 s_return (mk_number (SCHEME_A_ v));
4029 4479
4030 case OP_SUB: /* - */ 4480 case OP_SUB: /* - */
4031 if (cdr (args) == NIL) 4481 if (cdr (args) == NIL)
4038 x = cdr (args); 4488 x = cdr (args);
4039 v = nvalue (car (args)); 4489 v = nvalue (car (args));
4040 } 4490 }
4041 4491
4042 for (; x != NIL; x = cdr (x)) 4492 for (; x != NIL; x = cdr (x))
4043 v = num_op ('+', v, nvalue (car (x))); 4493 v = num_op (NUM_SUB, v, nvalue (car (x)));
4044 4494
4045 s_return (mk_number (SCHEME_A_ v)); 4495 s_return (mk_number (SCHEME_A_ v));
4046 4496
4047 case OP_DIV: /* / */ 4497 case OP_DIV: /* / */
4048 if (cdr (args) == NIL) 4498 if (cdr (args) == NIL)
4055 x = cdr (args); 4505 x = cdr (args);
4056 v = nvalue (car (args)); 4506 v = nvalue (car (args));
4057 } 4507 }
4058 4508
4059 for (; x != NIL; x = cdr (x)) 4509 for (; x != NIL; x = cdr (x))
4060 {
4061 if (!is_zero_rvalue (rvalue (car (x)))) 4510 if (!is_zero_rvalue (rvalue (car (x))))
4062 v = num_div (v, nvalue (car (x))); 4511 v = num_div (v, nvalue (car (x)));
4063 else 4512 else
4064 Error_0 ("/: division by zero"); 4513 Error_0 ("/: division by zero");
4065 }
4066 4514
4067 s_return (mk_number (SCHEME_A_ v)); 4515 s_return (mk_number (SCHEME_A_ v));
4068 4516
4069 case OP_INTDIV: /* quotient */ 4517 case OP_INTDIV: /* quotient */
4070 if (cdr (args) == NIL) 4518 if (cdr (args) == NIL)
4079 } 4527 }
4080 4528
4081 for (; x != NIL; x = cdr (x)) 4529 for (; x != NIL; x = cdr (x))
4082 { 4530 {
4083 if (ivalue (car (x)) != 0) 4531 if (ivalue (car (x)) != 0)
4084 v = num_op ('/', v, nvalue (car (x))); 4532 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4085 else 4533 else
4086 Error_0 ("quotient: division by zero"); 4534 Error_0 ("quotient: division by zero");
4087 } 4535 }
4088 4536
4089 s_return (mk_number (SCHEME_A_ v)); 4537 s_return (mk_number (SCHEME_A_ v));
4106 else 4554 else
4107 Error_0 ("modulo: division by zero"); 4555 Error_0 ("modulo: division by zero");
4108 4556
4109 s_return (mk_number (SCHEME_A_ v)); 4557 s_return (mk_number (SCHEME_A_ v));
4110 4558
4111 case OP_CAR: /* car */ 4559 /* the compiler will optimize this mess... */
4112 s_return (caar (args)); 4560 case OP_CAR: op_car: s_return (car (x));
4113 4561 case OP_CDR: op_cdr: s_return (cdr (x));
4114 case OP_CDR: /* cdr */ 4562 case OP_CAAR: op_caar: x = car (x); goto op_car;
4115 s_return (cdar (args)); 4563 case OP_CADR: op_cadr: x = cdr (x); goto op_car;
4564 case OP_CDAR: op_cdar: x = car (x); goto op_cdr;
4565 case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr;
4566 case OP_CAAAR: op_caaar: x = car (x); goto op_caar;
4567 case OP_CAADR: op_caadr: x = cdr (x); goto op_caar;
4568 case OP_CADAR: op_cadar: x = car (x); goto op_cadr;
4569 case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr;
4570 case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar;
4571 case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar;
4572 case OP_CDDAR: op_cddar: x = car (x); goto op_cddr;
4573 case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr;
4574 case OP_CAAAAR: x = car (x); goto op_caaar;
4575 case OP_CAAADR: x = cdr (x); goto op_caaar;
4576 case OP_CAADAR: x = car (x); goto op_caadr;
4577 case OP_CAADDR: x = cdr (x); goto op_caadr;
4578 case OP_CADAAR: x = car (x); goto op_cadar;
4579 case OP_CADADR: x = cdr (x); goto op_cadar;
4580 case OP_CADDAR: x = car (x); goto op_caddr;
4581 case OP_CADDDR: x = cdr (x); goto op_caddr;
4582 case OP_CDAAAR: x = car (x); goto op_cdaar;
4583 case OP_CDAADR: x = cdr (x); goto op_cdaar;
4584 case OP_CDADAR: x = car (x); goto op_cdadr;
4585 case OP_CDADDR: x = cdr (x); goto op_cdadr;
4586 case OP_CDDAAR: x = car (x); goto op_cddar;
4587 case OP_CDDADR: x = cdr (x); goto op_cddar;
4588 case OP_CDDDAR: x = car (x); goto op_cdddr;
4589 case OP_CDDDDR: x = cdr (x); goto op_cdddr;
4116 4590
4117 case OP_CONS: /* cons */ 4591 case OP_CONS: /* cons */
4118 set_cdr (args, cadr (args)); 4592 set_cdr (args, cadr (args));
4119 s_return (args); 4593 s_return (args);
4120 4594
4135 } 4609 }
4136 else 4610 else
4137 Error_0 ("set-cdr!: unable to alter immutable pair"); 4611 Error_0 ("set-cdr!: unable to alter immutable pair");
4138 4612
4139 case OP_CHAR2INT: /* char->integer */ 4613 case OP_CHAR2INT: /* char->integer */
4140 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4614 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4141 4615
4142 case OP_INT2CHAR: /* integer->char */ 4616 case OP_INT2CHAR: /* integer->char */
4143 s_return (mk_character (SCHEME_A_ ivalue (x))); 4617 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4144 4618
4145 case OP_CHARUPCASE: 4619 case OP_CHARUPCASE:
4146 { 4620 {
4147 unsigned char c = ivalue (x); 4621 unsigned char c = ivalue_unchecked (x);
4148 c = toupper (c); 4622 c = toupper (c);
4149 s_return (mk_character (SCHEME_A_ c)); 4623 s_return (mk_character (SCHEME_A_ c));
4150 } 4624 }
4151 4625
4152 case OP_CHARDNCASE: 4626 case OP_CHARDNCASE:
4153 { 4627 {
4154 unsigned char c = ivalue (x); 4628 unsigned char c = ivalue_unchecked (x);
4155 c = tolower (c); 4629 c = tolower (c);
4156 s_return (mk_character (SCHEME_A_ c)); 4630 s_return (mk_character (SCHEME_A_ c));
4157 } 4631 }
4158 4632
4159 case OP_STR2SYM: /* string->symbol */ 4633 case OP_STR2SYM: /* string->symbol */
4236 Error_1 ("atom->string: not an atom:", x); 4710 Error_1 ("atom->string: not an atom:", x);
4237 } 4711 }
4238 4712
4239 case OP_MKSTRING: /* make-string */ 4713 case OP_MKSTRING: /* make-string */
4240 { 4714 {
4241 int fill = ' '; 4715 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4242 int len;
4243
4244 len = ivalue (x); 4716 int len = ivalue_unchecked (x);
4245
4246 if (cdr (args) != NIL)
4247 fill = charvalue (cadr (args));
4248 4717
4249 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4718 s_return (mk_empty_string (SCHEME_A_ len, fill));
4250 } 4719 }
4251 4720
4252 case OP_STRLEN: /* string-length */ 4721 case OP_STRLEN: /* string-length */
4253 s_return (mk_integer (SCHEME_A_ strlength (x))); 4722 s_return (mk_integer (SCHEME_A_ strlength (x)));
4254 4723
4255 case OP_STRREF: /* string-ref */ 4724 case OP_STRREF: /* string-ref */
4256 { 4725 {
4257 char *str;
4258 int index;
4259
4260 str = strvalue (x); 4726 char *str = strvalue (x);
4261
4262 index = ivalue (cadr (args)); 4727 int index = ivalue_unchecked (cadr (args));
4263 4728
4264 if (index >= strlength (x)) 4729 if (index >= strlength (x))
4265 Error_1 ("string-ref: out of bounds:", cadr (args)); 4730 Error_1 ("string-ref: out of bounds:", cadr (args));
4266 4731
4267 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4732 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4268 } 4733 }
4269 4734
4270 case OP_STRSET: /* string-set! */ 4735 case OP_STRSET: /* string-set! */
4271 { 4736 {
4272 char *str; 4737 char *str = strvalue (x);
4273 int index; 4738 int index = ivalue_unchecked (cadr (args));
4274 int c; 4739 int c;
4275 4740
4276 if (is_immutable (x)) 4741 if (is_immutable (x))
4277 Error_1 ("string-set!: unable to alter immutable string:", x); 4742 Error_1 ("string-set!: unable to alter immutable string:", x);
4278
4279 str = strvalue (x);
4280
4281 index = ivalue (cadr (args));
4282 4743
4283 if (index >= strlength (x)) 4744 if (index >= strlength (x))
4284 Error_1 ("string-set!: out of bounds:", cadr (args)); 4745 Error_1 ("string-set!: out of bounds:", cadr (args));
4285 4746
4286 c = charvalue (caddr (args)); 4747 c = charvalue (caddr (args));
4307 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4768 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4308 4769
4309 s_return (newstr); 4770 s_return (newstr);
4310 } 4771 }
4311 4772
4312 case OP_SUBSTR: /* substring */ 4773 case OP_STRING_COPY: /* substring/string-copy */
4313 { 4774 {
4314 char *str; 4775 char *str = strvalue (x);
4315 int index0; 4776 int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args));
4316 int index1; 4777 int index1;
4317 int len; 4778 int len;
4318 4779
4319 str = strvalue (x);
4320
4321 index0 = ivalue (cadr (args));
4322
4323 if (index0 > strlength (x)) 4780 if (index0 > strlength (x))
4324 Error_1 ("substring: start out of bounds:", cadr (args)); 4781 Error_1 ("string->copy: start out of bounds:", cadr (args));
4325 4782
4326 if (cddr (args) != NIL) 4783 if (cddr (args) != NIL)
4327 { 4784 {
4328 index1 = ivalue (caddr (args)); 4785 index1 = ivalue_unchecked (caddr (args));
4329 4786
4330 if (index1 > strlength (x) || index1 < index0) 4787 if (index1 > strlength (x) || index1 < index0)
4331 Error_1 ("substring: end out of bounds:", caddr (args)); 4788 Error_1 ("string->copy: end out of bounds:", caddr (args));
4332 } 4789 }
4333 else 4790 else
4334 index1 = strlength (x); 4791 index1 = strlength (x);
4335 4792
4336 len = index1 - index0; 4793 len = index1 - index0;
4337 x = mk_empty_string (SCHEME_A_ len, ' '); 4794 x = mk_counted_string (SCHEME_A_ str + index0, len);
4338 memcpy (strvalue (x), str + index0, len);
4339 strvalue (x)[len] = 0;
4340 4795
4341 s_return (x); 4796 s_return (x);
4342 } 4797 }
4343 4798
4344 case OP_VECTOR: /* vector */ 4799 case OP_VECTOR: /* vector */
4356 if (SCHEME_V->no_memory) 4811 if (SCHEME_V->no_memory)
4357 s_return (S_SINK); 4812 s_return (S_SINK);
4358#endif 4813#endif
4359 4814
4360 for (x = args, i = 0; is_pair (x); x = cdr (x), i++) 4815 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4361 set_vector_elem (vec, i, car (x)); 4816 vector_set (vec, i, car (x));
4362 4817
4363 s_return (vec); 4818 s_return (vec);
4364 } 4819 }
4365 4820
4366 case OP_MKVECTOR: /* make-vector */ 4821 case OP_MKVECTOR: /* make-vector */
4367 { 4822 {
4368 pointer fill = NIL; 4823 pointer fill = NIL;
4369 int len;
4370 pointer vec; 4824 pointer vec;
4371
4372 len = ivalue (x); 4825 int len = ivalue_unchecked (x);
4373 4826
4374 if (cdr (args) != NIL) 4827 if (cdr (args) != NIL)
4375 fill = cadr (args); 4828 fill = cadr (args);
4376 4829
4377 vec = mk_vector (SCHEME_A_ len); 4830 vec = mk_vector (SCHEME_A_ len);
4380 if (SCHEME_V->no_memory) 4833 if (SCHEME_V->no_memory)
4381 s_return (S_SINK); 4834 s_return (S_SINK);
4382#endif 4835#endif
4383 4836
4384 if (fill != NIL) 4837 if (fill != NIL)
4385 fill_vector (vec, fill); 4838 fill_vector (vec, 0, fill);
4386 4839
4387 s_return (vec); 4840 s_return (vec);
4388 } 4841 }
4389 4842
4390 case OP_VECLEN: /* vector-length */ 4843 case OP_VECLEN: /* vector-length */
4391 s_return (mk_integer (SCHEME_A_ veclength (x))); 4844 s_return (mk_integer (SCHEME_A_ veclength (x)));
4392 4845
4846 case OP_VECRESIZE:
4847 vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args));
4848 s_return (x);
4849
4393 case OP_VECREF: /* vector-ref */ 4850 case OP_VECREF: /* vector-ref */
4394 { 4851 {
4395 int index;
4396
4397 index = ivalue (cadr (args)); 4852 int index = ivalue_unchecked (cadr (args));
4398 4853
4399 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4854 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4400 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4855 Error_1 ("vector-ref: out of bounds:", cadr (args));
4401 4856
4402 s_return (vector_elem (x, index)); 4857 s_return (vector_get (x, index));
4403 } 4858 }
4404 4859
4405 case OP_VECSET: /* vector-set! */ 4860 case OP_VECSET: /* vector-set! */
4406 { 4861 {
4407 int index; 4862 int index = ivalue_unchecked (cadr (args));
4408 4863
4409 if (is_immutable (x)) 4864 if (is_immutable (x))
4410 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4865 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4411 4866
4412 index = ivalue (cadr (args));
4413
4414 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4867 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4415 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4868 Error_1 ("vector-set!: out of bounds:", cadr (args));
4416 4869
4417 set_vector_elem (x, index, caddr (args)); 4870 vector_set (x, index, caddr (args));
4418 s_return (x); 4871 s_return (x);
4419 } 4872 }
4420 } 4873 }
4421 4874
4422 return S_T; 4875 if (USE_ERROR_CHECKING) abort ();
4423} 4876}
4424 4877
4425INTERFACE int 4878/* relational ops */
4426is_list (SCHEME_P_ pointer a) 4879ecb_hot static int
4427{
4428 return list_length (SCHEME_A_ a) >= 0;
4429}
4430
4431/* Result is:
4432 proper list: length
4433 circular list: -1
4434 not even a pair: -2
4435 dotted list: -2 minus length before dot
4436*/
4437INTERFACE int
4438list_length (SCHEME_P_ pointer a)
4439{
4440 int i = 0;
4441 pointer slow, fast;
4442
4443 slow = fast = a;
4444
4445 while (1)
4446 {
4447 if (fast == NIL)
4448 return i;
4449
4450 if (!is_pair (fast))
4451 return -2 - i;
4452
4453 fast = cdr (fast);
4454 ++i;
4455
4456 if (fast == NIL)
4457 return i;
4458
4459 if (!is_pair (fast))
4460 return -2 - i;
4461
4462 ++i;
4463 fast = cdr (fast);
4464
4465 /* Safe because we would have already returned if `fast'
4466 encountered a non-pair. */
4467 slow = cdr (slow);
4468
4469 if (fast == slow)
4470 {
4471 /* the fast pointer has looped back around and caught up
4472 with the slow pointer, hence the structure is circular,
4473 not of finite length, and therefore not a list */
4474 return -1;
4475 }
4476 }
4477}
4478
4479static pointer
4480opexe_r (SCHEME_P_ enum scheme_opcodes op) 4880opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4481{ 4881{
4482 pointer x = SCHEME_V->args; 4882 pointer x = SCHEME_V->args;
4483 4883
4484 for (;;) 4884 for (;;)
4485 { 4885 {
4505 } 4905 }
4506 4906
4507 s_return (S_T); 4907 s_return (S_T);
4508} 4908}
4509 4909
4510static pointer 4910/* predicates */
4911ecb_hot static int
4511opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4912opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4512{ 4913{
4513 pointer args = SCHEME_V->args; 4914 pointer args = SCHEME_V->args;
4514 pointer a = car (args); 4915 pointer a = car (args);
4515 pointer d = cdr (args); 4916 pointer d = cdr (args);
4516 int r; 4917 int r;
4517 4918
4518 switch (op) 4919 switch (op)
4519 { 4920 {
4520 case OP_NOT: /* not */ r = is_false (a) ; break; 4921 case OP_NOT: /* not */ r = is_false (a) ; break;
4521 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; 4922 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break;
4522 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; 4923 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break;
4523 case OP_NULLP: /* null? */ r = a == NIL ; break; 4924 case OP_NULLP: /* null? */ r = a == NIL ; break;
4524 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; 4925 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break;
4926 case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break;
4525 case OP_NUMBERP: /* number? */ r = is_number (a) ; break; 4927 case OP_NUMBERP: /* number? */ r = is_number (a) ; break;
4526 case OP_STRINGP: /* string? */ r = is_string (a) ; break; 4928 case OP_STRINGP: /* string? */ r = is_string (a) ; break;
4527 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4929 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4528 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4930 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4529 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4931 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4530 4932
4531#if USE_CHAR_CLASSIFIERS 4933#if USE_CHAR_CLASSIFIERS
4532 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4934 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4533 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4935 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4534 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4936 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4535 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4937 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4536 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4938 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4537#endif 4939#endif
4538 4940
4539#if USE_PORTS 4941#if USE_PORTS
4540 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4942 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4541 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4943 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4561 } 4963 }
4562 4964
4563 s_retbool (r); 4965 s_retbool (r);
4564} 4966}
4565 4967
4566static pointer 4968/* promises, list ops, ports */
4969ecb_hot static int
4567opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4970opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4568{ 4971{
4569 pointer args = SCHEME_V->args; 4972 pointer args = SCHEME_V->args;
4570 pointer a = car (args); 4973 pointer a = car (args);
4571 pointer x, y; 4974 pointer x, y;
4584 } 4987 }
4585 else 4988 else
4586 s_return (SCHEME_V->code); 4989 s_return (SCHEME_V->code);
4587 4990
4588 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4991 case OP_SAVE_FORCED: /* Save forced value replacing promise */
4589 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); 4992 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
4590 s_return (SCHEME_V->value); 4993 s_return (SCHEME_V->value);
4591 4994
4592#if USE_PORTS 4995#if USE_PORTS
4996
4997 case OP_EOF_OBJECT: /* eof-object */
4998 s_return (S_EOF);
4593 4999
4594 case OP_WRITE: /* write */ 5000 case OP_WRITE: /* write */
4595 case OP_DISPLAY: /* display */ 5001 case OP_DISPLAY: /* display */
4596 case OP_WRITE_CHAR: /* write-char */ 5002 case OP_WRITE_CHAR: /* write-char */
4597 if (is_pair (cdr (SCHEME_V->args))) 5003 if (is_pair (cdr (SCHEME_V->args)))
4611 else 5017 else
4612 SCHEME_V->print_flag = 0; 5018 SCHEME_V->print_flag = 0;
4613 5019
4614 s_goto (OP_P0LIST); 5020 s_goto (OP_P0LIST);
4615 5021
5022 //TODO: move to scheme
4616 case OP_NEWLINE: /* newline */ 5023 case OP_NEWLINE: /* newline */
4617 if (is_pair (args)) 5024 if (is_pair (args))
4618 { 5025 {
4619 if (a != SCHEME_V->outport) 5026 if (a != SCHEME_V->outport)
4620 { 5027 {
4622 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 5029 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4623 SCHEME_V->outport = a; 5030 SCHEME_V->outport = a;
4624 } 5031 }
4625 } 5032 }
4626 5033
4627 putstr (SCHEME_A_ "\n"); 5034 putcharacter (SCHEME_A_ '\n');
4628 s_return (S_T); 5035 s_return (S_T);
4629#endif 5036#endif
4630 5037
4631 case OP_ERR0: /* error */ 5038 case OP_ERR0: /* error */
4632 SCHEME_V->retcode = -1; 5039 SCHEME_V->retcode = -1;
4641 putstr (SCHEME_A_ strvalue (car (args))); 5048 putstr (SCHEME_A_ strvalue (car (args)));
4642 SCHEME_V->args = cdr (args); 5049 SCHEME_V->args = cdr (args);
4643 s_goto (OP_ERR1); 5050 s_goto (OP_ERR1);
4644 5051
4645 case OP_ERR1: /* error */ 5052 case OP_ERR1: /* error */
4646 putstr (SCHEME_A_ " "); 5053 putcharacter (SCHEME_A_ ' ');
4647 5054
4648 if (args != NIL) 5055 if (args != NIL)
4649 { 5056 {
4650 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); 5057 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4651 SCHEME_V->args = a; 5058 SCHEME_V->args = a;
4652 SCHEME_V->print_flag = 1; 5059 SCHEME_V->print_flag = 1;
4653 s_goto (OP_P0LIST); 5060 s_goto (OP_P0LIST);
4654 } 5061 }
4655 else 5062 else
4656 { 5063 {
4657 putstr (SCHEME_A_ "\n"); 5064 putcharacter (SCHEME_A_ '\n');
4658 5065
4659 if (SCHEME_V->interactive_repl) 5066 if (SCHEME_V->interactive_repl)
4660 s_goto (OP_T0LVL); 5067 s_goto (OP_T0LVL);
4661 else 5068 else
4662 return NIL; 5069 return -1;
4663 } 5070 }
4664 5071
4665 case OP_REVERSE: /* reverse */ 5072 case OP_REVERSE: /* reverse */
4666 s_return (reverse (SCHEME_A_ a)); 5073 s_return (reverse (SCHEME_A_ a));
4667 5074
4724 5131
4725 case OP_QUIT: /* quit */ 5132 case OP_QUIT: /* quit */
4726 if (is_pair (args)) 5133 if (is_pair (args))
4727 SCHEME_V->retcode = ivalue (a); 5134 SCHEME_V->retcode = ivalue (a);
4728 5135
4729 return NIL; 5136 return -1;
4730 5137
4731 case OP_GC: /* gc */ 5138 case OP_GC: /* gc */
4732 gc (SCHEME_A_ NIL, NIL); 5139 gc (SCHEME_A_ NIL, NIL);
4733 s_return (S_T); 5140 s_return (S_T);
4734 5141
4739 SCHEME_V->gc_verbose = (a != S_F); 5146 SCHEME_V->gc_verbose = (a != S_F);
4740 s_retbool (was); 5147 s_retbool (was);
4741 } 5148 }
4742 5149
4743 case OP_NEWSEGMENT: /* new-segment */ 5150 case OP_NEWSEGMENT: /* new-segment */
5151#if 0
4744 if (!is_pair (args) || !is_number (a)) 5152 if (!is_pair (args) || !is_number (a))
4745 Error_0 ("new-segment: argument must be a number"); 5153 Error_0 ("new-segment: argument must be a number");
4746 5154#endif
4747 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 5155 s_retbool (alloc_cellseg (SCHEME_A));
4748
4749 s_return (S_T);
4750 5156
4751 case OP_OBLIST: /* oblist */ 5157 case OP_OBLIST: /* oblist */
4752 s_return (oblist_all_symbols (SCHEME_A)); 5158 s_return (oblist_all_symbols (SCHEME_A));
4753 5159
4754#if USE_PORTS 5160#if USE_PORTS
4781 break; 5187 break;
4782 } 5188 }
4783 5189
4784 p = port_from_filename (SCHEME_A_ strvalue (a), prop); 5190 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4785 5191
4786 if (p == NIL) 5192 s_return (p == NIL ? S_F : p);
4787 s_return (S_F);
4788
4789 s_return (p);
4790 } 5193 }
4791 5194
4792# if USE_STRING_PORTS 5195# if USE_STRING_PORTS
4793 5196
4794 case OP_OPEN_INSTRING: /* open-input-string */ 5197 case OP_OPEN_INSTRING: /* open-input-string */
4809 } 5212 }
4810 5213
4811 p = port_from_string (SCHEME_A_ strvalue (a), 5214 p = port_from_string (SCHEME_A_ strvalue (a),
4812 strvalue (a) + strlength (a), prop); 5215 strvalue (a) + strlength (a), prop);
4813 5216
4814 if (p == NIL) 5217 s_return (p == NIL ? S_F : p);
4815 s_return (S_F);
4816
4817 s_return (p);
4818 } 5218 }
4819 5219
4820 case OP_OPEN_OUTSTRING: /* open-output-string */ 5220 case OP_OPEN_OUTSTRING: /* open-output-string */
4821 { 5221 {
4822 pointer p; 5222 pointer p;
4823 5223
4824 if (a == NIL) 5224 if (a == NIL)
4825 {
4826 p = port_from_scratch (SCHEME_A); 5225 p = port_from_scratch (SCHEME_A);
4827
4828 if (p == NIL)
4829 s_return (S_F);
4830 }
4831 else 5226 else
4832 {
4833 p = port_from_string (SCHEME_A_ strvalue (a), 5227 p = port_from_string (SCHEME_A_ strvalue (a),
4834 strvalue (a) + strlength (a), port_output); 5228 strvalue (a) + strlength (a), port_output);
4835 5229
4836 if (p == NIL) 5230 s_return (p == NIL ? S_F : p);
4837 s_return (S_F);
4838 }
4839
4840 s_return (p);
4841 } 5231 }
4842 5232
4843 case OP_GET_OUTSTRING: /* get-output-string */ 5233 case OP_GET_OUTSTRING: /* get-output-string */
4844 { 5234 {
4845 port *p; 5235 port *p = port (a);
4846 5236
4847 if ((p = a->object.port)->kind & port_string) 5237 if (p->kind & port_string)
4848 { 5238 {
4849 off_t size; 5239 off_t size;
4850 char *str; 5240 char *str;
4851 5241
4852 size = p->rep.string.curr - p->rep.string.start + 1; 5242 size = p->rep.string.curr - p->rep.string.start + 1;
4884 case OP_CURR_ENV: /* current-environment */ 5274 case OP_CURR_ENV: /* current-environment */
4885 s_return (SCHEME_V->envir); 5275 s_return (SCHEME_V->envir);
4886 5276
4887 } 5277 }
4888 5278
4889 abort (); 5279 if (USE_ERROR_CHECKING) abort ();
4890} 5280}
4891 5281
4892static pointer 5282/* reading */
5283ecb_cold static int
4893opexe_5 (SCHEME_P_ enum scheme_opcodes op) 5284opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4894{ 5285{
4895 pointer args = SCHEME_V->args; 5286 pointer args = SCHEME_V->args;
4896 pointer x; 5287 pointer x;
4897 5288
4957 int res; 5348 int res;
4958 5349
4959 if (is_pair (args)) 5350 if (is_pair (args))
4960 p = car (args); 5351 p = car (args);
4961 5352
4962 res = p->object.port->kind & port_string; 5353 res = port (p)->kind & port_string;
4963 5354
4964 s_retbool (res); 5355 s_retbool (res);
4965 } 5356 }
4966 5357
4967 case OP_SET_INPORT: /* set-input-port */ 5358 case OP_SET_INPORT: /* set-input-port */
4976 case OP_RDSEXPR: 5367 case OP_RDSEXPR:
4977 switch (SCHEME_V->tok) 5368 switch (SCHEME_V->tok)
4978 { 5369 {
4979 case TOK_EOF: 5370 case TOK_EOF:
4980 s_return (S_EOF); 5371 s_return (S_EOF);
4981 /* NOTREACHED */
4982 5372
4983 case TOK_VEC: 5373 case TOK_VEC:
4984 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); 5374 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL);
4985 /* fall through */ 5375 /* fall through */
4986 5376
4989 5379
4990 if (SCHEME_V->tok == TOK_RPAREN) 5380 if (SCHEME_V->tok == TOK_RPAREN)
4991 s_return (NIL); 5381 s_return (NIL);
4992 else if (SCHEME_V->tok == TOK_DOT) 5382 else if (SCHEME_V->tok == TOK_DOT)
4993 Error_0 ("syntax error: illegal dot expression"); 5383 Error_0 ("syntax error: illegal dot expression");
4994 else 5384
4995 {
4996 SCHEME_V->nesting_stack[SCHEME_V->file_i]++; 5385 SCHEME_V->nesting_stack[SCHEME_V->file_i]++;
4997 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL); 5386 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL);
4998 s_goto (OP_RDSEXPR); 5387 s_goto (OP_RDSEXPR);
4999 }
5000 5388
5001 case TOK_QUOTE: 5389 case TOK_QUOTE:
5002 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL); 5390 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL);
5003 SCHEME_V->tok = token (SCHEME_A); 5391 SCHEME_V->tok = token (SCHEME_A);
5004 s_goto (OP_RDSEXPR); 5392 s_goto (OP_RDSEXPR);
5010 { 5398 {
5011 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); 5399 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL);
5012 SCHEME_V->tok = TOK_LPAREN; 5400 SCHEME_V->tok = TOK_LPAREN;
5013 s_goto (OP_RDSEXPR); 5401 s_goto (OP_RDSEXPR);
5014 } 5402 }
5015 else 5403
5016 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); 5404 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL);
5017
5018 s_goto (OP_RDSEXPR); 5405 s_goto (OP_RDSEXPR);
5019 5406
5020 case TOK_COMMA: 5407 case TOK_COMMA:
5021 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); 5408 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL);
5022 SCHEME_V->tok = token (SCHEME_A); 5409 SCHEME_V->tok = token (SCHEME_A);
5026 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 5413 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
5027 SCHEME_V->tok = token (SCHEME_A); 5414 SCHEME_V->tok = token (SCHEME_A);
5028 s_goto (OP_RDSEXPR); 5415 s_goto (OP_RDSEXPR);
5029 5416
5030 case TOK_ATOM: 5417 case TOK_ATOM:
5031 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); 5418 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS)));
5419
5420 case TOK_DOTATOM:
5421 SCHEME_V->strbuff[0] = '.';
5422 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5423
5424 case TOK_STRATOM:
5425 //TODO: haven't checked whether the garbage collector could interfere and free x
5426 gc (SCHEME_A_ NIL, NIL); //TODO: superheavyhanded
5427 x = readstrexp (SCHEME_A_ '|');
5428 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5032 5429
5033 case TOK_DQUOTE: 5430 case TOK_DQUOTE:
5034 x = readstrexp (SCHEME_A); 5431 x = readstrexp (SCHEME_A_ '"');
5035 5432
5036 if (x == S_F) 5433 if (x == S_F)
5037 Error_0 ("Error reading string"); 5434 Error_0 ("Error reading string");
5038 5435
5039 setimmutable (x); 5436 setimmutable (x);
5043 { 5440 {
5044 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1); 5441 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1);
5045 5442
5046 if (f == NIL) 5443 if (f == NIL)
5047 Error_0 ("undefined sharp expression"); 5444 Error_0 ("undefined sharp expression");
5048 else 5445
5049 {
5050 SCHEME_V->code = cons (slot_value_in_env (f), NIL); 5446 SCHEME_V->code = cons (slot_value_in_env (f), NIL);
5051 s_goto (OP_EVAL); 5447 s_goto (OP_EVAL);
5052 }
5053 } 5448 }
5054 5449
5055 case TOK_SHARP_CONST: 5450 case TOK_SHARP_CONST:
5056 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) 5451 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5057 Error_0 ("undefined sharp expression"); 5452 Error_0 ("undefined sharp expression");
5058 else 5453
5059 s_return (x); 5454 s_return (x);
5060 5455
5061 default: 5456 default:
5062 Error_0 ("syntax error: illegal token"); 5457 Error_0 ("syntax error: illegal token");
5063 } 5458 }
5064 5459
5157 pointer b = cdr (args); 5552 pointer b = cdr (args);
5158 int ok_abbr = ok_abbrev (b); 5553 int ok_abbr = ok_abbrev (b);
5159 SCHEME_V->args = car (b); 5554 SCHEME_V->args = car (b);
5160 5555
5161 if (a == SCHEME_V->QUOTE && ok_abbr) 5556 if (a == SCHEME_V->QUOTE && ok_abbr)
5162 putstr (SCHEME_A_ "'"); 5557 putcharacter (SCHEME_A_ '\'');
5163 else if (a == SCHEME_V->QQUOTE && ok_abbr) 5558 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5164 putstr (SCHEME_A_ "`"); 5559 putcharacter (SCHEME_A_ '`');
5165 else if (a == SCHEME_V->UNQUOTE && ok_abbr) 5560 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5166 putstr (SCHEME_A_ ","); 5561 putcharacter (SCHEME_A_ ',');
5167 else if (a == SCHEME_V->UNQUOTESP && ok_abbr) 5562 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5168 putstr (SCHEME_A_ ",@"); 5563 putstr (SCHEME_A_ ",@");
5169 else 5564 else
5170 { 5565 {
5171 putstr (SCHEME_A_ "("); 5566 putcharacter (SCHEME_A_ '(');
5172 s_save (SCHEME_A_ OP_P1LIST, b, NIL); 5567 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5173 SCHEME_V->args = a; 5568 SCHEME_V->args = a;
5174 } 5569 }
5175 5570
5176 s_goto (OP_P0LIST); 5571 s_goto (OP_P0LIST);
5178 5573
5179 case OP_P1LIST: 5574 case OP_P1LIST:
5180 if (is_pair (args)) 5575 if (is_pair (args))
5181 { 5576 {
5182 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); 5577 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5183 putstr (SCHEME_A_ " "); 5578 putcharacter (SCHEME_A_ ' ');
5184 SCHEME_V->args = car (args); 5579 SCHEME_V->args = car (args);
5185 s_goto (OP_P0LIST); 5580 s_goto (OP_P0LIST);
5186 } 5581 }
5187 else if (is_vector (args)) 5582 else if (is_vector (args))
5188 { 5583 {
5196 { 5591 {
5197 putstr (SCHEME_A_ " . "); 5592 putstr (SCHEME_A_ " . ");
5198 printatom (SCHEME_A_ args, SCHEME_V->print_flag); 5593 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5199 } 5594 }
5200 5595
5201 putstr (SCHEME_A_ ")"); 5596 putcharacter (SCHEME_A_ ')');
5202 s_return (S_T); 5597 s_return (S_T);
5203 } 5598 }
5204 5599
5205 case OP_PVECFROM: 5600 case OP_PVECFROM:
5206 { 5601 {
5208 pointer vec = car (args); 5603 pointer vec = car (args);
5209 int len = veclength (vec); 5604 int len = veclength (vec);
5210 5605
5211 if (i == len) 5606 if (i == len)
5212 { 5607 {
5213 putstr (SCHEME_A_ ")"); 5608 putcharacter (SCHEME_A_ ')');
5214 s_return (S_T); 5609 s_return (S_T);
5215 } 5610 }
5216 else 5611 else
5217 { 5612 {
5218 pointer elem = vector_elem (vec, i); 5613 pointer elem = vector_get (vec, i);
5219 5614
5220 ivalue_unchecked (cdr (args)) = i + 1; 5615 ivalue_unchecked (cdr (args)) = i + 1;
5221 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5616 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5222 SCHEME_V->args = elem; 5617 SCHEME_V->args = elem;
5223 5618
5224 if (i > 0) 5619 if (i > 0)
5225 putstr (SCHEME_A_ " "); 5620 putcharacter (SCHEME_A_ ' ');
5226 5621
5227 s_goto (OP_P0LIST); 5622 s_goto (OP_P0LIST);
5228 } 5623 }
5229 } 5624 }
5230 } 5625 }
5231 5626
5232 abort (); 5627 if (USE_ERROR_CHECKING) abort ();
5233} 5628}
5234 5629
5235static pointer 5630/* list ops */
5631ecb_hot static int
5236opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5632opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5237{ 5633{
5238 pointer args = SCHEME_V->args; 5634 pointer args = SCHEME_V->args;
5239 pointer a = car (args); 5635 pointer a = car (args);
5240 pointer x, y; 5636 pointer x, y;
5263 break; 5659 break;
5264 } 5660 }
5265 5661
5266 if (is_pair (y)) 5662 if (is_pair (y))
5267 s_return (car (y)); 5663 s_return (car (y));
5268 else 5664
5269 s_return (S_F); 5665 s_return (S_F);
5270
5271 5666
5272 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5667 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5273 SCHEME_V->args = a; 5668 SCHEME_V->args = a;
5274 5669
5275 if (SCHEME_V->args == NIL) 5670 if (SCHEME_V->args == NIL)
5276 s_return (S_F); 5671 s_return (S_F);
5277 else if (is_closure (SCHEME_V->args)) 5672 else if (is_closure (SCHEME_V->args) || is_macro (SCHEME_V->args))
5278 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5673 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5279 else if (is_macro (SCHEME_V->args)) 5674
5280 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5281 else
5282 s_return (S_F); 5675 s_return (S_F);
5283 5676
5284 case OP_CLOSUREP: /* closure? */ 5677 case OP_CLOSUREP: /* closure? */
5285 /* 5678 /*
5286 * Note, macro object is also a closure. 5679 * Note, macro object is also a closure.
5287 * Therefore, (closure? <#MACRO>) ==> #t 5680 * Therefore, (closure? <#MACRO>) ==> #t
5681 * (schmorp) well, obviously not, fix? TODO
5288 */ 5682 */
5289 s_retbool (is_closure (a)); 5683 s_retbool (is_closure (a));
5290 5684
5291 case OP_MACROP: /* macro? */ 5685 case OP_MACROP: /* macro? */
5292 s_retbool (is_macro (a)); 5686 s_retbool (is_macro (a));
5293 } 5687 }
5294 5688
5295 abort (); 5689 if (USE_ERROR_CHECKING) abort ();
5296} 5690}
5297 5691
5692/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5298typedef pointer (*dispatch_func) (SCHEME_P_ enum scheme_opcodes); 5693typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5299 5694
5300typedef int (*test_predicate) (pointer); 5695typedef int (*test_predicate)(pointer);
5301static int 5696
5697ecb_hot static int
5302is_any (pointer p) 5698tst_any (pointer p)
5303{ 5699{
5304 return 1; 5700 return 1;
5305} 5701}
5306 5702
5307static int 5703ecb_hot static int
5308is_nonneg (pointer p) 5704tst_inonneg (pointer p)
5309{ 5705{
5310 return ivalue (p) >= 0 && is_integer (p); 5706 return is_integer (p) && ivalue_unchecked (p) >= 0;
5707}
5708
5709ecb_hot static int
5710tst_is_list (SCHEME_P_ pointer p)
5711{
5712 return p == NIL || is_pair (p);
5311} 5713}
5312 5714
5313/* Correspond carefully with following defines! */ 5715/* Correspond carefully with following defines! */
5314static struct 5716static struct
5315{ 5717{
5316 test_predicate fct; 5718 test_predicate fct;
5317 const char *kind; 5719 const char *kind;
5318} tests[] = 5720} tests[] = {
5319{ 5721 { tst_any , 0 },
5320 { 0, 0}, /* unused */ 5722 { is_string , "string" },
5321 { is_any, 0}, 5723 { is_symbol , "symbol" },
5322 { is_string, "string" }, 5724 { is_port , "port" },
5323 { is_symbol, "symbol" },
5324 { is_port, "port" },
5325 { is_inport, "input port" }, 5725 { is_inport , "input port" },
5326 { is_outport, "output port" }, 5726 { is_outport , "output port" },
5327 { is_environment, "environment" }, 5727 { is_environment, "environment" },
5328 { is_pair, "pair" }, 5728 { is_pair , "pair" },
5329 { 0, "pair or '()" }, 5729 { 0 , "pair or '()" },
5330 { is_character, "character" }, 5730 { is_character , "character" },
5331 { is_vector, "vector" }, 5731 { is_vector , "vector" },
5332 { is_number, "number" }, 5732 { is_number , "number" },
5333 { is_integer, "integer" }, 5733 { is_integer , "integer" },
5334 { is_nonneg, "non-negative integer" } 5734 { tst_inonneg , "non-negative integer" }
5335}; 5735};
5336 5736
5337#define TST_NONE 0 /* TST_NONE used for standard procedures, for internal ops, 0 is used */ 5737#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5338#define TST_ANY "\001" 5738#define TST_ANY "\001"
5339#define TST_STRING "\002" 5739#define TST_STRING "\002"
5340#define TST_SYMBOL "\003" 5740#define TST_SYMBOL "\003"
5341#define TST_PORT "\004" 5741#define TST_PORT "\004"
5342#define TST_INPORT "\005" 5742#define TST_INPORT "\005"
5348#define TST_VECTOR "\013" 5748#define TST_VECTOR "\013"
5349#define TST_NUMBER "\014" 5749#define TST_NUMBER "\014"
5350#define TST_INTEGER "\015" 5750#define TST_INTEGER "\015"
5351#define TST_NATURAL "\016" 5751#define TST_NATURAL "\016"
5352 5752
5753#define INF_ARG 0xff
5754#define UNNAMED_OP ""
5755
5756static const char opnames[] =
5757#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5758#include "opdefines.h"
5759#undef OP_DEF
5760;
5761
5762ecb_cold static const char *
5763opname (int idx)
5764{
5765 const char *name = opnames;
5766
5767 /* should do this at compile time, but would require external program, right? */
5768 while (idx--)
5769 name += strlen (name) + 1;
5770
5771 return *name ? name : "ILLEGAL";
5772}
5773
5774ecb_cold static const char *
5775procname (pointer x)
5776{
5777 return opname (procnum (x));
5778}
5779
5353typedef struct 5780typedef struct
5354{ 5781{
5355 dispatch_func func; 5782 uint8_t func;
5356 char *name; 5783 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5784 uint8_t builtin;
5785#if USE_ERROR_CHECKING
5357 int min_arity; 5786 uint8_t min_arity;
5358 int max_arity; 5787 uint8_t max_arity;
5359 char arg_tests_encoding[3]; 5788 char arg_tests_encoding[3];
5789#endif
5360} op_code_info; 5790} op_code_info;
5361 5791
5362#define INF_ARG 0xffff
5363
5364static op_code_info dispatch_table[] = { 5792static const op_code_info dispatch_table[] = {
5793#if USE_ERROR_CHECKING
5365#define OP_DEF(func,name,minarity,maxarity,argtest,op) { opexe_ ## func, name, minarity, maxarity, argtest }, 5794#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5795#else
5796#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5797#endif
5366#include "opdefines.h" 5798#include "opdefines.h"
5367#undef OP_DEF 5799#undef OP_DEF
5368 {0} 5800 {0}
5369}; 5801};
5370 5802
5371static const char *
5372procname (pointer x)
5373{
5374 int n = procnum (x);
5375 const char *name = dispatch_table[n].name;
5376
5377 if (name == 0)
5378 name = "ILLEGAL!";
5379
5380 return name;
5381}
5382
5383/* kernel of this interpreter */ 5803/* kernel of this interpreter */
5384static void 5804ecb_hot static void
5385Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5805Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5386{ 5806{
5387 SCHEME_V->op = op; 5807 SCHEME_V->op = op;
5388 5808
5389 for (;;) 5809 for (;;)
5390 { 5810 {
5391 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5811 const op_code_info *pcd = dispatch_table + SCHEME_V->op;
5392 5812
5393#if USE_ERROR_CHECKING 5813#if USE_ERROR_CHECKING
5394 if (pcd->name) /* if built-in function, check arguments */ 5814 if (pcd->builtin) /* if built-in function, check arguments */
5395 { 5815 {
5396 int ok = 1;
5397 char msg[STRBUFFSIZE]; 5816 char msg[STRBUFFSIZE];
5398 int n = list_length (SCHEME_A_ SCHEME_V->args); 5817 int n = list_length (SCHEME_A_ SCHEME_V->args);
5399 5818
5400 /* Check number of arguments */ 5819 /* Check number of arguments */
5401 if (ecb_expect_false (n < pcd->min_arity)) 5820 if (ecb_expect_false (n < pcd->min_arity))
5402 { 5821 {
5403 ok = 0;
5404 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5822 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5405 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); 5823 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity);
5824 xError_1 (SCHEME_A_ msg, 0);
5825 continue;
5406 } 5826 }
5407 else if (ecb_expect_false (n > pcd->max_arity)) 5827 else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG))
5408 { 5828 {
5409 ok = 0;
5410 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5829 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5411 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); 5830 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity);
5831 xError_1 (SCHEME_A_ msg, 0);
5832 continue;
5412 } 5833 }
5413 5834 else
5414 if (ecb_expect_false (ok))
5415 { 5835 {
5416 if (*pcd->arg_tests_encoding) 5836 if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */
5417 { 5837 {
5418 int i = 0; 5838 int i = 0;
5419 int j; 5839 int j;
5420 const char *t = pcd->arg_tests_encoding; 5840 const char *t = pcd->arg_tests_encoding;
5421 pointer arglist = SCHEME_V->args; 5841 pointer arglist = SCHEME_V->args;
5424 { 5844 {
5425 pointer arg = car (arglist); 5845 pointer arg = car (arglist);
5426 5846
5427 j = t[0]; 5847 j = t[0];
5428 5848
5849 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5429 if (j == TST_LIST[0]) 5850 if (j == TST_LIST[0])
5430 { 5851 {
5431 if (arg != NIL && !is_pair (arg)) 5852 if (!tst_is_list (SCHEME_A_ arg))
5432 break; 5853 break;
5433 } 5854 }
5434 else 5855 else
5435 { 5856 {
5436 if (!tests[j].fct (arg)) 5857 if (!tests[j - 1].fct (arg))
5437 break; 5858 break;
5438 } 5859 }
5439 5860
5440 if (t[1]) /* last test is replicated as necessary */ 5861 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5441 t++; 5862 t++;
5442 5863
5443 arglist = cdr (arglist); 5864 arglist = cdr (arglist);
5444 i++; 5865 i++;
5445 } 5866 }
5446 while (i < n); 5867 while (i < n);
5447 5868
5448 if (i < n) 5869 if (i < n)
5449 { 5870 {
5450 ok = 0;
5451 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); 5871 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind);
5872 xError_1 (SCHEME_A_ msg, 0);
5873 continue;
5452 } 5874 }
5453 } 5875 }
5454 } 5876 }
5455
5456 if (!ok)
5457 {
5458 if (xError_1 (SCHEME_A_ msg, 0) == NIL)
5459 return;
5460
5461 pcd = dispatch_table + SCHEME_V->op;
5462 }
5463 } 5877 }
5464#endif 5878#endif
5465 5879
5466 ok_to_freely_gc (SCHEME_A); 5880 ok_to_freely_gc (SCHEME_A);
5467 5881
5882 static const dispatch_func dispatch_funcs[] = {
5883 opexe_0,
5884 opexe_1,
5885 opexe_2,
5886 opexe_3,
5887 opexe_4,
5888 opexe_5,
5889 opexe_6,
5890 };
5891
5468 if (ecb_expect_false (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL)) 5892 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5469 return; 5893 return;
5470 5894
5471 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5895 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5472 { 5896 {
5473 xwrstr ("No memory!\n"); 5897 putstr (SCHEME_A_ "No memory!\n");
5474 return; 5898 return;
5475 } 5899 }
5476 } 5900 }
5477} 5901}
5478 5902
5479/* ========== Initialization of internal keywords ========== */ 5903/* ========== Initialization of internal keywords ========== */
5480 5904
5481static void 5905ecb_cold static void
5482assign_syntax (SCHEME_P_ const char *name) 5906assign_syntax (SCHEME_P_ const char *name)
5483{ 5907{
5484 pointer x = oblist_add_by_name (SCHEME_A_ name); 5908 pointer x = oblist_add_by_name (SCHEME_A_ name);
5485 set_typeflag (x, typeflag (x) | T_SYNTAX); 5909 set_typeflag (x, typeflag (x) | T_SYNTAX);
5486} 5910}
5487 5911
5488static void 5912ecb_cold static void
5489assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) 5913assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
5490{ 5914{
5491 pointer x = mk_symbol (SCHEME_A_ name); 5915 pointer x = mk_symbol (SCHEME_A_ name);
5492 pointer y = mk_proc (SCHEME_A_ op); 5916 pointer y = mk_proc (SCHEME_A_ op);
5493 new_slot_in_env (SCHEME_A_ x, y); 5917 new_slot_in_env (SCHEME_A_ x, y);
5497mk_proc (SCHEME_P_ enum scheme_opcodes op) 5921mk_proc (SCHEME_P_ enum scheme_opcodes op)
5498{ 5922{
5499 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5923 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5500 set_typeflag (y, (T_PROC | T_ATOM)); 5924 set_typeflag (y, (T_PROC | T_ATOM));
5501 ivalue_unchecked (y) = op; 5925 ivalue_unchecked (y) = op;
5502 set_num_integer (y);
5503 return y; 5926 return y;
5504} 5927}
5505 5928
5506/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5929/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5507static int 5930ecb_hot static int
5508syntaxnum (pointer p) 5931syntaxnum (pointer p)
5509{ 5932{
5510 const char *s = strvalue (car (p)); 5933 const char *s = strvalue (p);
5511 5934
5512 switch (strlength (car (p))) 5935 switch (strlength (p))
5513 { 5936 {
5514 case 2: 5937 case 2:
5515 if (s[0] == 'i') 5938 if (s[0] == 'i')
5516 return OP_IF0; /* if */ 5939 return OP_IF0; /* if */
5517 else 5940 else
5572 return OP_C0STREAM; /* cons-stream */ 5995 return OP_C0STREAM; /* cons-stream */
5573 } 5996 }
5574} 5997}
5575 5998
5576#if USE_MULTIPLICITY 5999#if USE_MULTIPLICITY
5577scheme * 6000ecb_cold scheme *
5578scheme_init_new () 6001scheme_init_new ()
5579{ 6002{
5580 scheme *sc = malloc (sizeof (scheme)); 6003 scheme *sc = malloc (sizeof (scheme));
5581 6004
5582 if (!scheme_init (SCHEME_A)) 6005 if (!scheme_init (SCHEME_A))
5587 else 6010 else
5588 return sc; 6011 return sc;
5589} 6012}
5590#endif 6013#endif
5591 6014
5592int 6015ecb_cold int
5593scheme_init (SCHEME_P) 6016scheme_init (SCHEME_P)
5594{ 6017{
5595 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 6018 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5596 pointer x; 6019
6020 /* this memset is not strictly correct, as we assume (intcache)
6021 * that memset 0 will also set pointers to 0, but memset does
6022 * of course not guarantee that. screw such systems.
6023 */
6024 memset (SCHEME_V, 0, sizeof (*SCHEME_V));
5597 6025
5598 num_set_fixnum (num_zero, 1); 6026 num_set_fixnum (num_zero, 1);
5599 num_set_ivalue (num_zero, 0); 6027 num_set_ivalue (num_zero, 0);
5600 num_set_fixnum (num_one, 1); 6028 num_set_fixnum (num_one, 1);
5601 num_set_ivalue (num_one, 1); 6029 num_set_ivalue (num_one, 1);
5613 SCHEME_V->save_inport = NIL; 6041 SCHEME_V->save_inport = NIL;
5614 SCHEME_V->loadport = NIL; 6042 SCHEME_V->loadport = NIL;
5615 SCHEME_V->nesting = 0; 6043 SCHEME_V->nesting = 0;
5616 SCHEME_V->interactive_repl = 0; 6044 SCHEME_V->interactive_repl = 0;
5617 6045
5618 if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) 6046 if (!alloc_cellseg (SCHEME_A))
5619 { 6047 {
5620#if USE_ERROR_CHECKING 6048#if USE_ERROR_CHECKING
5621 SCHEME_V->no_memory = 1; 6049 SCHEME_V->no_memory = 1;
5622 return 0; 6050 return 0;
5623#endif 6051#endif
5624 } 6052 }
5625 6053
5626 SCHEME_V->gc_verbose = 0; 6054 SCHEME_V->gc_verbose = 0;
5627 dump_stack_initialize (SCHEME_A); 6055 dump_stack_initialize (SCHEME_A);
5628 SCHEME_V->code = NIL; 6056 SCHEME_V->code = NIL;
5629 SCHEME_V->args = NIL; 6057 SCHEME_V->args = NIL;
5630 SCHEME_V->envir = NIL; 6058 SCHEME_V->envir = NIL;
6059 SCHEME_V->value = NIL;
5631 SCHEME_V->tracing = 0; 6060 SCHEME_V->tracing = 0;
5632 6061
5633 /* init NIL */ 6062 /* init NIL */
5634 set_typeflag (NIL, T_ATOM | T_MARK); 6063 set_typeflag (NIL, T_SPECIAL | T_ATOM);
5635 set_car (NIL, NIL); 6064 set_car (NIL, NIL);
5636 set_cdr (NIL, NIL); 6065 set_cdr (NIL, NIL);
5637 /* init T */ 6066 /* init T */
5638 set_typeflag (S_T, T_ATOM | T_MARK); 6067 set_typeflag (S_T, T_SPECIAL | T_ATOM);
5639 set_car (S_T, S_T); 6068 set_car (S_T, S_T);
5640 set_cdr (S_T, S_T); 6069 set_cdr (S_T, S_T);
5641 /* init F */ 6070 /* init F */
5642 set_typeflag (S_F, T_ATOM | T_MARK); 6071 set_typeflag (S_F, T_SPECIAL | T_ATOM);
5643 set_car (S_F, S_F); 6072 set_car (S_F, S_F);
5644 set_cdr (S_F, S_F); 6073 set_cdr (S_F, S_F);
5645 /* init EOF_OBJ */ 6074 /* init EOF_OBJ */
5646 set_typeflag (S_EOF, T_ATOM | T_MARK); 6075 set_typeflag (S_EOF, T_SPECIAL | T_ATOM);
5647 set_car (S_EOF, S_EOF); 6076 set_car (S_EOF, S_EOF);
5648 set_cdr (S_EOF, S_EOF); 6077 set_cdr (S_EOF, S_EOF);
5649 /* init sink */ 6078 /* init sink */
5650 set_typeflag (S_SINK, T_PAIR | T_MARK); 6079 set_typeflag (S_SINK, T_PAIR);
5651 set_car (S_SINK, NIL); 6080 set_car (S_SINK, NIL);
5652 6081
5653 /* init c_nest */ 6082 /* init c_nest */
5654 SCHEME_V->c_nest = NIL; 6083 SCHEME_V->c_nest = NIL;
5655 6084
5656 SCHEME_V->oblist = oblist_initial_value (SCHEME_A); 6085 SCHEME_V->oblist = oblist_initial_value (SCHEME_A);
5657 /* init global_env */ 6086 /* init global_env */
5658 new_frame_in_env (SCHEME_A_ NIL); 6087 new_frame_in_env (SCHEME_A_ NIL);
5659 SCHEME_V->global_env = SCHEME_V->envir; 6088 SCHEME_V->global_env = SCHEME_V->envir;
5660 /* init else */ 6089 /* init else */
5661 x = mk_symbol (SCHEME_A_ "else"); 6090 new_slot_in_env (SCHEME_A_ mk_symbol (SCHEME_A_ "else"), S_T);
5662 new_slot_in_env (SCHEME_A_ x, S_T);
5663 6091
5664 { 6092 {
5665 static const char *syntax_names[] = { 6093 static const char *syntax_names[] = {
5666 "lambda", "quote", "define", "if", "begin", "set!", 6094 "lambda", "quote", "define", "if", "begin", "set!",
5667 "let", "let*", "letrec", "cond", "delay", "and", 6095 "let", "let*", "letrec", "cond", "delay", "and",
5670 6098
5671 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) 6099 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
5672 assign_syntax (SCHEME_A_ syntax_names[i]); 6100 assign_syntax (SCHEME_A_ syntax_names[i]);
5673 } 6101 }
5674 6102
6103 // TODO: should iterate via strlen, to avoid n² complexity
5675 for (i = 0; i < n; i++) 6104 for (i = 0; i < n; i++)
5676 if (dispatch_table[i].name != 0) 6105 if (dispatch_table[i].builtin)
5677 assign_proc (SCHEME_A_ i, dispatch_table[i].name); 6106 assign_proc (SCHEME_A_ i, opname (i));
5678 6107
5679 /* initialization of global pointers to special symbols */ 6108 /* initialization of global pointers to special symbols */
5680 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 6109 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
5681 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 6110 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
5682 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 6111 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
5690 6119
5691 return !SCHEME_V->no_memory; 6120 return !SCHEME_V->no_memory;
5692} 6121}
5693 6122
5694#if USE_PORTS 6123#if USE_PORTS
5695void 6124ecb_cold void
5696scheme_set_input_port_file (SCHEME_P_ int fin) 6125scheme_set_input_port_file (SCHEME_P_ int fin)
5697{ 6126{
5698 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); 6127 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input);
5699} 6128}
5700 6129
5701void 6130ecb_cold void
5702scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) 6131scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end)
5703{ 6132{
5704 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); 6133 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input);
5705} 6134}
5706 6135
5707void 6136ecb_cold void
5708scheme_set_output_port_file (SCHEME_P_ int fout) 6137scheme_set_output_port_file (SCHEME_P_ int fout)
5709{ 6138{
5710 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); 6139 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output);
5711} 6140}
5712 6141
5713void 6142ecb_cold void
5714scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) 6143scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end)
5715{ 6144{
5716 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); 6145 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output);
5717} 6146}
5718#endif 6147#endif
5719 6148
5720void 6149ecb_cold void
5721scheme_set_external_data (SCHEME_P_ void *p) 6150scheme_set_external_data (SCHEME_P_ void *p)
5722{ 6151{
5723 SCHEME_V->ext_data = p; 6152 SCHEME_V->ext_data = p;
5724} 6153}
5725 6154
5726void 6155ecb_cold void
5727scheme_deinit (SCHEME_P) 6156scheme_deinit (SCHEME_P)
5728{ 6157{
5729 int i; 6158 int i;
5730 6159
5731#if SHOW_ERROR_LINE 6160#if SHOW_ERROR_LINE
5757 SCHEME_V->loadport = NIL; 6186 SCHEME_V->loadport = NIL;
5758 SCHEME_V->gc_verbose = 0; 6187 SCHEME_V->gc_verbose = 0;
5759 gc (SCHEME_A_ NIL, NIL); 6188 gc (SCHEME_A_ NIL, NIL);
5760 6189
5761 for (i = 0; i <= SCHEME_V->last_cell_seg; i++) 6190 for (i = 0; i <= SCHEME_V->last_cell_seg; i++)
5762 free (SCHEME_V->alloc_seg[i]); 6191 free (SCHEME_V->cell_seg[i]);
5763 6192
5764#if SHOW_ERROR_LINE 6193#if SHOW_ERROR_LINE
5765 for (i = 0; i <= SCHEME_V->file_i; i++) 6194 for (i = 0; i <= SCHEME_V->file_i; i++)
5766 {
5767 if (SCHEME_V->load_stack[i].kind & port_file) 6195 if (SCHEME_V->load_stack[i].kind & port_file)
5768 { 6196 {
5769 fname = SCHEME_V->load_stack[i].rep.stdio.filename; 6197 fname = SCHEME_V->load_stack[i].rep.stdio.filename;
5770 6198
5771 if (fname) 6199 if (fname)
5772 free (fname); 6200 free (fname);
5773 } 6201 }
5774 }
5775#endif 6202#endif
5776} 6203}
5777 6204
5778void 6205ecb_cold void
5779scheme_load_file (SCHEME_P_ int fin) 6206scheme_load_file (SCHEME_P_ int fin)
5780{ 6207{
5781 scheme_load_named_file (SCHEME_A_ fin, 0); 6208 scheme_load_named_file (SCHEME_A_ fin, 0);
5782} 6209}
5783 6210
5784void 6211ecb_cold void
5785scheme_load_named_file (SCHEME_P_ int fin, const char *filename) 6212scheme_load_named_file (SCHEME_P_ int fin, const char *filename)
5786{ 6213{
5787 dump_stack_reset (SCHEME_A); 6214 dump_stack_reset (SCHEME_A);
5788 SCHEME_V->envir = SCHEME_V->global_env; 6215 SCHEME_V->envir = SCHEME_V->global_env;
5789 SCHEME_V->file_i = 0; 6216 SCHEME_V->file_i = 0;
5790 SCHEME_V->load_stack[0].unget = -1; 6217 SCHEME_V->load_stack[0].unget = -1;
5791 SCHEME_V->load_stack[0].kind = port_input | port_file; 6218 SCHEME_V->load_stack[0].kind = port_input | port_file;
5792 SCHEME_V->load_stack[0].rep.stdio.file = fin; 6219 SCHEME_V->load_stack[0].rep.stdio.file = fin;
5793#if USE_PORTS
5794 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6220 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5795#endif
5796 SCHEME_V->retcode = 0; 6221 SCHEME_V->retcode = 0;
5797 6222
5798#if USE_PORTS
5799 if (fin == STDIN_FILENO) 6223 if (fin == STDIN_FILENO)
5800 SCHEME_V->interactive_repl = 1; 6224 SCHEME_V->interactive_repl = 1;
5801#endif
5802 6225
5803#if USE_PORTS 6226#if USE_PORTS
5804#if SHOW_ERROR_LINE 6227#if SHOW_ERROR_LINE
5805 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; 6228 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0;
5806 6229
5810#endif 6233#endif
5811 6234
5812 SCHEME_V->inport = SCHEME_V->loadport; 6235 SCHEME_V->inport = SCHEME_V->loadport;
5813 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6236 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5814 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6237 Eval_Cycle (SCHEME_A_ OP_T0LVL);
6238
5815 set_typeflag (SCHEME_V->loadport, T_ATOM); 6239 set_typeflag (SCHEME_V->loadport, T_ATOM);
5816 6240
5817 if (SCHEME_V->retcode == 0) 6241 if (SCHEME_V->retcode == 0)
5818 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6242 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5819} 6243}
5820 6244
5821void 6245ecb_cold void
5822scheme_load_string (SCHEME_P_ const char *cmd) 6246scheme_load_string (SCHEME_P_ const char *cmd)
5823{ 6247{
6248#if USE_PORTs
5824 dump_stack_reset (SCHEME_A); 6249 dump_stack_reset (SCHEME_A);
5825 SCHEME_V->envir = SCHEME_V->global_env; 6250 SCHEME_V->envir = SCHEME_V->global_env;
5826 SCHEME_V->file_i = 0; 6251 SCHEME_V->file_i = 0;
5827 SCHEME_V->load_stack[0].kind = port_input | port_string; 6252 SCHEME_V->load_stack[0].kind = port_input | port_string;
5828 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ 6253 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */
5829 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); 6254 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd);
5830 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; 6255 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
5831#if USE_PORTS
5832 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6256 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5833#endif
5834 SCHEME_V->retcode = 0; 6257 SCHEME_V->retcode = 0;
5835 SCHEME_V->interactive_repl = 0; 6258 SCHEME_V->interactive_repl = 0;
5836 SCHEME_V->inport = SCHEME_V->loadport; 6259 SCHEME_V->inport = SCHEME_V->loadport;
5837 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6260 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5838 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6261 Eval_Cycle (SCHEME_A_ OP_T0LVL);
5839 set_typeflag (SCHEME_V->loadport, T_ATOM); 6262 set_typeflag (SCHEME_V->loadport, T_ATOM);
5840 6263
5841 if (SCHEME_V->retcode == 0) 6264 if (SCHEME_V->retcode == 0)
5842 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6265 SCHEME_V->retcode = SCHEME_V->nesting != 0;
6266#else
6267 abort ();
6268#endif
5843} 6269}
5844 6270
5845void 6271ecb_cold void
5846scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) 6272scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value)
5847{ 6273{
5848 pointer x; 6274 pointer x;
5849 6275
5850 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 6276 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
5855 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 6281 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
5856} 6282}
5857 6283
5858#if !STANDALONE 6284#if !STANDALONE
5859 6285
5860void 6286ecb_cold void
5861scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 6287scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
5862{ 6288{
5863 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); 6289 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f));
5864} 6290}
5865 6291
5866void 6292ecb_cold void
5867scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 6293scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
5868{ 6294{
5869 int i; 6295 int i;
5870 6296
5871 for (i = 0; i < count; i++) 6297 for (i = 0; i < count; i++)
5872 scheme_register_foreign_func (SCHEME_A_ list + i); 6298 scheme_register_foreign_func (SCHEME_A_ list + i);
5873} 6299}
5874 6300
5875pointer 6301ecb_cold pointer
5876scheme_apply0 (SCHEME_P_ const char *procname) 6302scheme_apply0 (SCHEME_P_ const char *procname)
5877{ 6303{
5878 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); 6304 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL));
5879} 6305}
5880 6306
5881void 6307ecb_cold void
5882save_from_C_call (SCHEME_P) 6308save_from_C_call (SCHEME_P)
5883{ 6309{
5884 pointer saved_data = cons (car (S_SINK), 6310 pointer saved_data = cons (car (S_SINK),
5885 cons (SCHEME_V->envir, 6311 cons (SCHEME_V->envir,
5886 SCHEME_V->dump)); 6312 SCHEME_V->dump));
5890 /* Truncate the dump stack so TS will return here when done, not 6316 /* Truncate the dump stack so TS will return here when done, not
5891 directly resume pre-C-call operations. */ 6317 directly resume pre-C-call operations. */
5892 dump_stack_reset (SCHEME_A); 6318 dump_stack_reset (SCHEME_A);
5893} 6319}
5894 6320
5895void 6321ecb_cold void
5896restore_from_C_call (SCHEME_P) 6322restore_from_C_call (SCHEME_P)
5897{ 6323{
5898 set_car (S_SINK, caar (SCHEME_V->c_nest)); 6324 set_car (S_SINK, caar (SCHEME_V->c_nest));
5899 SCHEME_V->envir = cadar (SCHEME_V->c_nest); 6325 SCHEME_V->envir = cadar (SCHEME_V->c_nest);
5900 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); 6326 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest));
5901 /* Pop */ 6327 /* Pop */
5902 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); 6328 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest);
5903} 6329}
5904 6330
5905/* "func" and "args" are assumed to be already eval'ed. */ 6331/* "func" and "args" are assumed to be already eval'ed. */
5906pointer 6332ecb_cold pointer
5907scheme_call (SCHEME_P_ pointer func, pointer args) 6333scheme_call (SCHEME_P_ pointer func, pointer args)
5908{ 6334{
5909 int old_repl = SCHEME_V->interactive_repl; 6335 int old_repl = SCHEME_V->interactive_repl;
5910 6336
5911 SCHEME_V->interactive_repl = 0; 6337 SCHEME_V->interactive_repl = 0;
5918 SCHEME_V->interactive_repl = old_repl; 6344 SCHEME_V->interactive_repl = old_repl;
5919 restore_from_C_call (SCHEME_A); 6345 restore_from_C_call (SCHEME_A);
5920 return SCHEME_V->value; 6346 return SCHEME_V->value;
5921} 6347}
5922 6348
5923pointer 6349ecb_cold pointer
5924scheme_eval (SCHEME_P_ pointer obj) 6350scheme_eval (SCHEME_P_ pointer obj)
5925{ 6351{
5926 int old_repl = SCHEME_V->interactive_repl; 6352 int old_repl = SCHEME_V->interactive_repl;
5927 6353
5928 SCHEME_V->interactive_repl = 0; 6354 SCHEME_V->interactive_repl = 0;
5940 6366
5941/* ========== Main ========== */ 6367/* ========== Main ========== */
5942 6368
5943#if STANDALONE 6369#if STANDALONE
5944 6370
5945int 6371ecb_cold int
5946main (int argc, char **argv) 6372main (int argc, char **argv)
5947{ 6373{
5948# if USE_MULTIPLICITY 6374# if USE_MULTIPLICITY
5949 scheme ssc; 6375 scheme ssc;
5950 scheme *const SCHEME_V = &ssc; 6376 scheme *const SCHEME_V = &ssc;
5952# endif 6378# endif
5953 int fin; 6379 int fin;
5954 char *file_name = InitFile; 6380 char *file_name = InitFile;
5955 int retcode; 6381 int retcode;
5956 int isfile = 1; 6382 int isfile = 1;
6383#if EXPERIMENT
6384 system ("ps v $PPID");
6385#endif
5957 6386
5958 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6387 if (argc == 2 && strcmp (argv[1], "-?") == 0)
5959 { 6388 {
5960 xwrstr ("Usage: tinyscheme -?\n"); 6389 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
5961 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6390 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
5962 xwrstr ("followed by\n"); 6391 putstr (SCHEME_A_ "followed by\n");
5963 xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); 6392 putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n");
5964 xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); 6393 putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n");
5965 xwrstr ("assuming that the executable is named tinyscheme.\n"); 6394 putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n");
5966 xwrstr ("Use - as filename for stdin.\n"); 6395 putstr (SCHEME_A_ "Use - as filename for stdin.\n");
5967 return 1; 6396 return 1;
5968 } 6397 }
5969 6398
5970 if (!scheme_init (SCHEME_A)) 6399 if (!scheme_init (SCHEME_A))
5971 { 6400 {
5972 xwrstr ("Could not initialize!\n"); 6401 putstr (SCHEME_A_ "Could not initialize!\n");
5973 return 2; 6402 return 2;
5974 } 6403 }
5975 6404
5976# if USE_PORTS 6405# if USE_PORTS
5977 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); 6406 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO);
5990 } 6419 }
5991#endif 6420#endif
5992 6421
5993 do 6422 do
5994 { 6423 {
5995#if USE_PORTS
5996 if (strcmp (file_name, "-") == 0) 6424 if (strcmp (file_name, "-") == 0)
5997 fin = STDIN_FILENO; 6425 fin = STDIN_FILENO;
5998 else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) 6426 else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0)
5999 { 6427 {
6000 pointer args = NIL; 6428 pointer args = NIL;
6018 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); 6446 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args);
6019 6447
6020 } 6448 }
6021 else 6449 else
6022 fin = open (file_name, O_RDONLY); 6450 fin = open (file_name, O_RDONLY);
6023#endif
6024 6451
6025 if (isfile && fin < 0) 6452 if (isfile && fin < 0)
6026 { 6453 {
6027 xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); 6454 putstr (SCHEME_A_ "Could not open file ");
6455 putstr (SCHEME_A_ file_name);
6456 putcharacter (SCHEME_A_ '\n');
6028 } 6457 }
6029 else 6458 else
6030 { 6459 {
6031 if (isfile) 6460 if (isfile)
6032 scheme_load_named_file (SCHEME_A_ fin, file_name); 6461 scheme_load_named_file (SCHEME_A_ fin, file_name);
6033 else 6462 else
6034 scheme_load_string (SCHEME_A_ file_name); 6463 scheme_load_string (SCHEME_A_ file_name);
6035 6464
6036#if USE_PORTS
6037 if (!isfile || fin != STDIN_FILENO) 6465 if (!isfile || fin != STDIN_FILENO)
6038 { 6466 {
6039 if (SCHEME_V->retcode != 0) 6467 if (SCHEME_V->retcode != 0)
6040 { 6468 {
6041 xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); 6469 putstr (SCHEME_A_ "Errors encountered reading ");
6470 putstr (SCHEME_A_ file_name);
6471 putcharacter (SCHEME_A_ '\n');
6042 } 6472 }
6043 6473
6044 if (isfile) 6474 if (isfile)
6045 close (fin); 6475 close (fin);
6046 } 6476 }
6047#endif
6048 } 6477 }
6049 6478
6050 file_name = *argv++; 6479 file_name = *argv++;
6051 } 6480 }
6052 while (file_name != 0); 6481 while (file_name != 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines