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.13 by root, Thu Nov 26 07:59:42 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#endif
76 96
77#if !USE_MULTIPLICITY 97#if !USE_MULTIPLICITY
78static scheme sc; 98static scheme sc;
79#endif 99#endif
80 100
81static void 101ecb_cold static void
82xbase (char *s, long n, int base) 102xbase (char *s, long n, int base)
83{ 103{
84 if (n < 0) 104 if (n < 0)
85 { 105 {
86 *s++ = '-'; 106 *s++ = '-';
88 } 108 }
89 109
90 char *p = s; 110 char *p = s;
91 111
92 do { 112 do {
93 *p++ = '0' + n % base; 113 *p++ = "0123456789abcdef"[n % base];
94 n /= base; 114 n /= base;
95 } while (n); 115 } while (n);
96 116
97 *p-- = 0; 117 *p-- = 0;
98 118
101 char x = *s; *s = *p; *p = x; 121 char x = *s; *s = *p; *p = x;
102 --p; ++s; 122 --p; ++s;
103 } 123 }
104} 124}
105 125
106static void 126ecb_cold static void
107xnum (char *s, long n) 127xnum (char *s, long n)
108{ 128{
109 xbase (s, n, 10); 129 xbase (s, n, 10);
110} 130}
111 131
112static void 132ecb_cold static void
113xwrstr (const char *s) 133putnum (SCHEME_P_ long n)
114{
115 write (1, s, strlen (s));
116}
117
118static void
119xwrnum (long n)
120{ 134{
121 char buf[64]; 135 char buf[64];
122 136
123 xnum (buf, n); 137 xnum (buf, n);
124 xwrstr (buf); 138 putstr (SCHEME_A_ buf);
125} 139}
140
141#if USE_CHAR_CLASSIFIERS
142#include <ctype.h>
143#else
126 144
127static char 145static char
128xtoupper (char c) 146xtoupper (char c)
129{ 147{
130 if (c >= 'a' && c <= 'z') 148 if (c >= 'a' && c <= 'z')
150 168
151#define toupper(c) xtoupper (c) 169#define toupper(c) xtoupper (c)
152#define tolower(c) xtolower (c) 170#define tolower(c) xtolower (c)
153#define isdigit(c) xisdigit (c) 171#define isdigit(c) xisdigit (c)
154 172
155#if USE_STRLWR 173#endif
174
175#if USE_IGNORECASE
156static const char * 176ecb_cold static const char *
157strlwr (char *s) 177xstrlwr (char *s)
158{ 178{
159 const char *p = s; 179 const char *p = s;
160 180
161 while (*s) 181 while (*s)
162 { 182 {
164 s++; 184 s++;
165 } 185 }
166 186
167 return p; 187 return p;
168} 188}
169#endif
170 189
190#define stricmp(a,b) strcasecmp (a, b)
191#define strlwr(s) xstrlwr (s)
192
193#else
171#define stricmp(a,b) strcmp (a, b) 194# define stricmp(a,b) strcmp (a, b)
172#define strlwr(s) (s) 195# define strlwr(s) (s)
196#endif
173 197
174#ifndef prompt 198#ifndef prompt
175# define prompt "ts> " 199# define prompt "ms> "
176#endif 200#endif
177 201
178#ifndef InitFile 202#ifndef InitFile
179# define InitFile "init.scm" 203# define InitFile "init.scm"
180#endif 204#endif
181 205
182#ifndef FIRST_CELLSEGS
183# define FIRST_CELLSEGS 3
184#endif
185
186enum scheme_types 206enum scheme_types
187{ 207{
208 T_INTEGER,
209 T_CHARACTER,
188 T_FREE, 210 T_REAL,
189 T_STRING, 211 T_STRING,
190 T_NUMBER,
191 T_SYMBOL, 212 T_SYMBOL,
192 T_PROC, 213 T_PROC,
193 T_PAIR, 214 T_PAIR, /* also used for free cells */
194 T_CLOSURE, 215 T_CLOSURE,
216 T_BYTECODE, // temp
217 T_MACRO,
195 T_CONTINUATION, 218 T_CONTINUATION,
196 T_FOREIGN, 219 T_FOREIGN,
197 T_CHARACTER,
198 T_PORT, 220 T_PORT,
199 T_VECTOR, 221 T_VECTOR,
200 T_MACRO,
201 T_PROMISE, 222 T_PROMISE,
202 T_ENVIRONMENT, 223 T_ENVIRONMENT,
203 /* one more... */ 224 T_SPECIAL, // #t, #f, '(), eof-object
225
204 T_NUM_SYSTEM_TYPES 226 T_NUM_SYSTEM_TYPES
205}; 227};
206 228
207#define T_MASKTYPE 0x000f 229#define T_MASKTYPE 0x001f
208#define T_SYNTAX 0x0010 230#define T_SYNTAX 0x0020
209#define T_IMMUTABLE 0x0020 231#define T_IMMUTABLE 0x0040
210#define T_ATOM 0x0040 /* only for gc */ 232#define T_ATOM 0x0080 /* only for gc */
211#define T_MARK 0x0080 /* only for gc */ 233//#define T_MARK 0x0080 /* only for gc */
212 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
260
261enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
262
213static num num_op (char op, num a, num b); 263static num num_op (enum num_op op, num a, num b);
214static num num_intdiv (num a, num b); 264static num num_intdiv (num a, num b);
215static num num_rem (num a, num b); 265static num num_rem (num a, num b);
216static num num_mod (num a, num b); 266static num num_mod (num a, num b);
217static int num_eq (num a, num b);
218static int num_gt (num a, num b);
219static int num_ge (num a, num b);
220static int num_lt (num a, num b);
221static int num_le (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_3 (SCHEME_P_ enum scheme_opcodes op); 738static int opexe_3 (SCHEME_P_ enum scheme_opcodes op);
674static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); 739static int opexe_4 (SCHEME_P_ enum scheme_opcodes op);
675static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); 740static int opexe_5 (SCHEME_P_ enum scheme_opcodes op);
676static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); 741static int opexe_6 (SCHEME_P_ enum scheme_opcodes op);
677static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 742static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
678static void assign_syntax (SCHEME_P_ const char *name); 743static void assign_syntax (SCHEME_P_ const char *name);
679static int syntaxnum (pointer p); 744static int syntaxnum (pointer p);
680static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 745static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
681 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
682static num 774static num
683num_op (char op, num a, num b) 775num_op (enum num_op op, num a, num b)
684{ 776{
685 num ret; 777 num ret;
686 778
687 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));
688 780
689 if (num_is_fixnum (ret)) 781 if (num_is_fixnum (ret))
690 { 782 {
691 IVALUE av = num_get_ivalue (a);
692 IVALUE bv = num_get_ivalue (b);
693
694 switch (op) 783 switch (op)
695 { 784 {
696 case '+': av += bv; break; 785 case NUM_ADD: a.ivalue += b.ivalue; break;
697 case '-': av -= bv; break; 786 case NUM_SUB: a.ivalue -= b.ivalue; break;
698 case '*': av *= bv; break; 787 case NUM_MUL: a.ivalue *= b.ivalue; break;
699 case '/': av /= bv; break; 788 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
700 } 789 }
701 790
702 num_set_ivalue (ret, av); 791 num_set_ivalue (ret, a.ivalue);
703 } 792 }
793#if USE_REAL
704 else 794 else
705 { 795 {
706 RVALUE av = num_get_rvalue (a);
707 RVALUE bv = num_get_rvalue (b);
708
709 switch (op) 796 switch (op)
710 { 797 {
711 case '+': av += bv; break; 798 case NUM_ADD: a.rvalue += b.rvalue; break;
712 case '-': av -= bv; break; 799 case NUM_SUB: a.rvalue -= b.rvalue; break;
713 case '*': av *= bv; break; 800 case NUM_MUL: a.rvalue *= b.rvalue; break;
714 case '/': av /= bv; break; 801 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
715 } 802 }
716 803
717 num_set_rvalue (ret, av); 804 num_set_rvalue (ret, a.rvalue);
718 } 805 }
806#endif
719 807
720 return ret; 808 return ret;
721} 809}
722 810
723static num 811static num
724num_div (num a, num b) 812num_div (num a, num b)
725{ 813{
726 num ret; 814 num ret;
727 815
728 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);
729 817
730 if (num_is_fixnum (ret)) 818 if (num_is_fixnum (ret))
731 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 819 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
732 else 820 else
733 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 821 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
734 822
735 return ret; 823 return ret;
736} 824}
737 825
738static num 826static num
740{ 828{
741 num ret; 829 num ret;
742 long e1, e2, res; 830 long e1, e2, res;
743 831
744 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));
745 e1 = num_get_ivalue (a); 833 e1 = num_ivalue (a);
746 e2 = num_get_ivalue (b); 834 e2 = num_ivalue (b);
747 res = e1 % e2; 835 res = e1 % e2;
748 836
749 /* remainder should have same sign as second operand */ 837 /* remainder should have same sign as second operand */
750 if (res > 0) 838 if (res > 0)
751 { 839 {
767{ 855{
768 num ret; 856 num ret;
769 long e1, e2, res; 857 long e1, e2, res;
770 858
771 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));
772 e1 = num_get_ivalue (a); 860 e1 = num_ivalue (a);
773 e2 = num_get_ivalue (b); 861 e2 = num_ivalue (b);
774 res = e1 % e2; 862 res = e1 % e2;
775 863
776 /* modulo should have same sign as second operand */ 864 /* modulo should have same sign as second operand */
777 if (res * e2 < 0) 865 if (res * e2 < 0)
778 res += e2; 866 res += e2;
779 867
780 num_set_ivalue (ret, res); 868 num_set_ivalue (ret, res);
781 return ret; 869 return ret;
782} 870}
783 871
872/* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */
784static int 873static int
785num_eq (num a, num b) 874num_cmp (num a, num b)
786{ 875{
876 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
787 int ret; 877 int ret;
788 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
789 878
790 if (is_fixnum) 879 if (is_fixnum)
791 ret = num_get_ivalue (a) == num_get_ivalue (b); 880 {
881 IVALUE av = num_ivalue (a);
882 IVALUE bv = num_ivalue (b);
883
884 ret = av == bv ? 0 : av < bv ? -1 : +1;
885 }
792 else 886 else
793 ret = num_get_rvalue (a) == num_get_rvalue (b); 887 {
888 RVALUE av = num_rvalue (a);
889 RVALUE bv = num_rvalue (b);
890
891 ret = av == bv ? 0 : av < bv ? -1 : +1;
892 }
794 893
795 return ret; 894 return ret;
796} 895}
797
798
799static int
800num_gt (num a, num b)
801{
802 int ret;
803 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
804
805 if (is_fixnum)
806 ret = num_get_ivalue (a) > num_get_ivalue (b);
807 else
808 ret = num_get_rvalue (a) > num_get_rvalue (b);
809
810 return ret;
811}
812
813static int
814num_ge (num a, num b)
815{
816 return !num_lt (a, b);
817}
818
819static int
820num_lt (num a, num b)
821{
822 int ret;
823 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
824
825 if (is_fixnum)
826 ret = num_get_ivalue (a) < num_get_ivalue (b);
827 else
828 ret = num_get_rvalue (a) < num_get_rvalue (b);
829
830 return ret;
831}
832
833static int
834num_le (num a, num b)
835{
836 return !num_gt (a, b);
837}
838
839#if USE_MATH
840
841/* Round to nearest. Round to even if midway */
842static double
843round_per_R5RS (double x)
844{
845 double fl = floor (x);
846 double ce = ceil (x);
847 double dfl = x - fl;
848 double dce = ce - x;
849
850 if (dfl > dce)
851 return ce;
852 else if (dfl < dce)
853 return fl;
854 else
855 {
856 if (fmod (fl, 2.0) == 0.0) /* I imagine this holds */
857 return fl;
858 else
859 return ce;
860 }
861}
862#endif
863 896
864static int 897static int
865is_zero_rvalue (RVALUE x) 898is_zero_rvalue (RVALUE x)
866{ 899{
900 return x == 0;
901#if 0
867#if USE_REAL 902#if USE_REAL
868 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. */
869#else 904#else
870 return x == 0; 905 return x == 0;
871#endif 906#endif
907#endif
872} 908}
873 909
874/* allocate new cell segment */ 910/* allocate new cell segment */
875static int 911ecb_cold static int
876alloc_cellseg (SCHEME_P_ int n) 912alloc_cellseg (SCHEME_P)
877{ 913{
878 pointer newp; 914 struct cell *newp;
879 pointer last; 915 struct cell *last;
880 pointer p; 916 struct cell *p;
881 char *cp; 917 char *cp;
882 long i; 918 long i;
883 int k; 919 int k;
884 920
885 static int segsize = CELL_SEGSIZE >> 1; 921 static int segsize = CELL_SEGSIZE >> 1;
886 segsize <<= 1; 922 segsize <<= 1;
887 923
888 for (k = 0; k < n; k++)
889 {
890 if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1)
891 return k;
892
893 cp = malloc (segsize * sizeof (struct cell)); 924 cp = malloc (segsize * sizeof (struct cell));
894 925
895 if (!cp && USE_ERROR_CHECKING) 926 if (!cp && USE_ERROR_CHECKING)
896 return k; 927 return k;
897 928
898 i = ++SCHEME_V->last_cell_seg; 929 i = ++SCHEME_V->last_cell_seg;
899 SCHEME_V->alloc_seg[i] = cp;
900 930
901 /* insert new segment in address order */ 931 newp = (struct cell *)cp;
902 newp = (pointer)cp;
903 SCHEME_V->cell_seg[i] = newp; 932 SCHEME_V->cell_seg[i] = newp;
904 SCHEME_V->cell_segsize[i] = segsize; 933 SCHEME_V->cell_segsize[i] = segsize;
905
906 //TODO: insert, not swap
907 while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i])
908 {
909 p = SCHEME_V->cell_seg[i];
910 SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1];
911 SCHEME_V->cell_seg[i - 1] = p;
912
913 k = SCHEME_V->cell_segsize[i];
914 SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1];
915 SCHEME_V->cell_segsize[i - 1] = k;
916
917 --i;
918 }
919
920 SCHEME_V->fcells += segsize; 934 SCHEME_V->fcells += segsize;
921 last = newp + segsize - 1; 935 last = newp + segsize - 1;
922 936
923 for (p = newp; p <= last; p++) 937 for (p = newp; p <= last; p++)
924 { 938 {
939 pointer cp = POINTER (p);
940 clrmark (cp);
925 set_typeflag (p, T_FREE); 941 set_typeflag (cp, T_PAIR);
926 set_car (p, NIL); 942 set_car (cp, NIL);
927 set_cdr (p, p + 1); 943 set_cdr (cp, POINTER (p + 1));
928 } 944 }
929 945
930 /* insert new cells in address order on free list */
931 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell)
932 {
933 set_cdr (last, SCHEME_V->free_cell); 946 set_cdr (POINTER (last), SCHEME_V->free_cell);
934 SCHEME_V->free_cell = newp; 947 SCHEME_V->free_cell = POINTER (newp);
935 }
936 else
937 {
938 p = SCHEME_V->free_cell;
939 948
940 while (cdr (p) != NIL && newp > cdr (p))
941 p = cdr (p);
942
943 set_cdr (last, cdr (p));
944 set_cdr (p, newp);
945 }
946 }
947
948 return n; 949 return 1;
949} 950}
950 951
951/* get new cell. parameter a, b is marked by gc. */ 952/* get new cell. parameter a, b is marked by gc. */
952static INLINE pointer 953ecb_inline pointer
953get_cell_x (SCHEME_P_ pointer a, pointer b) 954get_cell_x (SCHEME_P_ pointer a, pointer b)
954{ 955{
955 if (ecb_expect_false (SCHEME_V->free_cell == NIL)) 956 if (ecb_expect_false (SCHEME_V->free_cell == NIL))
956 { 957 {
957 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 958 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
958 return S_SINK; 959 return S_SINK;
959 960
960 if (SCHEME_V->free_cell == NIL) 961 if (SCHEME_V->free_cell == NIL)
961 { 962 {
962 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;
963 964
964 gc (SCHEME_A_ a, b); 965 gc (SCHEME_A_ a, b);
965 966
966 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)
967 { 968 {
968 /* 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 */
969 if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) 970 if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL)
970 { 971 {
971#if USE_ERROR_CHECKING 972#if USE_ERROR_CHECKING
972 SCHEME_V->no_memory = 1; 973 SCHEME_V->no_memory = 1;
973 return S_SINK; 974 return S_SINK;
974#endif 975#endif
986 } 987 }
987} 988}
988 989
989/* To retain recent allocs before interpreter knows about them - 990/* To retain recent allocs before interpreter knows about them -
990 Tehom */ 991 Tehom */
991 992ecb_hot static void
992static void
993push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 993push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
994{ 994{
995 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 995 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
996 996
997 set_typeflag (holder, T_PAIR); 997 set_typeflag (holder, T_PAIR);
999 set_car (holder, recent); 999 set_car (holder, recent);
1000 set_cdr (holder, car (S_SINK)); 1000 set_cdr (holder, car (S_SINK));
1001 set_car (S_SINK, holder); 1001 set_car (S_SINK, holder);
1002} 1002}
1003 1003
1004static pointer 1004ecb_hot static pointer
1005get_cell (SCHEME_P_ pointer a, pointer b) 1005get_cell (SCHEME_P_ pointer a, pointer b)
1006{ 1006{
1007 pointer cell = get_cell_x (SCHEME_A_ a, b); 1007 pointer cell = get_cell_x (SCHEME_A_ a, b);
1008 1008
1009 /* 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
1018} 1018}
1019 1019
1020static pointer 1020static pointer
1021get_vector_object (SCHEME_P_ uint32_t len, pointer init) 1021get_vector_object (SCHEME_P_ uint32_t len, pointer init)
1022{ 1022{
1023 pointer v = get_cell_x (SCHEME_A_ 0, 0); 1023 pointer v = get_cell_x (SCHEME_A_ NIL, NIL);
1024 pointer *e = malloc (len * sizeof (pointer)); 1024 pointer *e = malloc (len * sizeof (pointer));
1025 1025
1026 if (!e && USE_ERROR_CHECKING) 1026 if (!e && USE_ERROR_CHECKING)
1027 return S_SINK; 1027 return S_SINK;
1028 1028
1029 /* Record it as a vector so that gc understands it. */ 1029 /* Record it as a vector so that gc understands it. */
1030 set_typeflag (v, T_VECTOR | T_ATOM); 1030 set_typeflag (v, T_VECTOR | T_ATOM);
1031 1031
1032 v->object.vector.vvalue = e; 1032 CELL(v)->object.vector.vvalue = e;
1033 v->object.vector.length = len; 1033 CELL(v)->object.vector.length = len;
1034 fill_vector (v, init); 1034 fill_vector (v, 0, init);
1035 push_recent_alloc (SCHEME_A_ v, NIL); 1035 push_recent_alloc (SCHEME_A_ v, NIL);
1036 1036
1037 return v; 1037 return v;
1038} 1038}
1039 1039
1040static INLINE void 1040ecb_inline void
1041ok_to_freely_gc (SCHEME_P) 1041ok_to_freely_gc (SCHEME_P)
1042{ 1042{
1043 set_car (S_SINK, NIL); 1043 set_car (S_SINK, NIL);
1044} 1044}
1045 1045
1047static void 1047static void
1048check_cell_alloced (pointer p, int expect_alloced) 1048check_cell_alloced (pointer p, int expect_alloced)
1049{ 1049{
1050 /* 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. */
1051 if (typeflag (p) & !expect_alloced) 1051 if (typeflag (p) & !expect_alloced)
1052 xwrstr ("Cell is already allocated!\n"); 1052 putstr (SCHEME_A_ "Cell is already allocated!\n");
1053 1053
1054 if (!(typeflag (p)) & expect_alloced) 1054 if (!(typeflag (p)) & expect_alloced)
1055 xwrstr ("Cell is not allocated!\n"); 1055 putstr (SCHEME_A_ "Cell is not allocated!\n");
1056} 1056}
1057 1057
1058static void 1058static void
1059check_range_alloced (pointer p, int n, int expect_alloced) 1059check_range_alloced (pointer p, int n, int expect_alloced)
1060{ 1060{
1066#endif 1066#endif
1067 1067
1068/* Medium level cell allocation */ 1068/* Medium level cell allocation */
1069 1069
1070/* get new cons cell */ 1070/* get new cons cell */
1071pointer 1071ecb_hot static pointer
1072xcons (SCHEME_P_ pointer a, pointer b, int immutable) 1072xcons (SCHEME_P_ pointer a, pointer b)
1073{ 1073{
1074 pointer x = get_cell (SCHEME_A_ a, b); 1074 pointer x = get_cell (SCHEME_A_ a, b);
1075 1075
1076 set_typeflag (x, T_PAIR); 1076 set_typeflag (x, T_PAIR);
1077
1078 if (immutable)
1079 setimmutable (x);
1080 1077
1081 set_car (x, a); 1078 set_car (x, a);
1082 set_cdr (x, b); 1079 set_cdr (x, b);
1083 1080
1084 return x; 1081 return x;
1085} 1082}
1086 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
1087/* ========== oblist implementation ========== */ 1104/* ========== oblist implementation ========== */
1088 1105
1089#ifndef USE_OBJECT_LIST 1106#ifndef USE_OBJECT_LIST
1090 1107
1108static int
1091static 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;
1092 1113
1114 while (*p)
1115 hash = (hash ^ *p++) * 16777619;
1116
1117 return hash % table_size;
1118}
1119
1093static pointer 1120ecb_cold static pointer
1094oblist_initial_value (SCHEME_P) 1121oblist_initial_value (SCHEME_P)
1095{ 1122{
1096 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1123 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1097} 1124}
1098 1125
1099/* returns the new symbol */ 1126/* returns the new symbol */
1100static pointer 1127ecb_cold static pointer
1101oblist_add_by_name (SCHEME_P_ const char *name) 1128oblist_add_by_name (SCHEME_P_ const char *name)
1102{ 1129{
1103 int location; 1130 pointer x = generate_symbol (SCHEME_A_ name);
1104
1105 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1106 set_typeflag (x, T_SYMBOL);
1107 setimmutable (car (x));
1108
1109 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1131 int location = hash_fn (name, veclength (SCHEME_V->oblist));
1110 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)));
1111 return x; 1133 return x;
1112} 1134}
1113 1135
1114static INLINE pointer 1136ecb_cold static pointer
1115oblist_find_by_name (SCHEME_P_ const char *name) 1137oblist_find_by_name (SCHEME_P_ const char *name)
1116{ 1138{
1117 int location; 1139 int location;
1118 pointer x; 1140 pointer x;
1119 char *s; 1141 char *s;
1120 1142
1121 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1143 location = hash_fn (name, veclength (SCHEME_V->oblist));
1122 1144
1123 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))
1124 { 1146 {
1125 s = symname (car (x)); 1147 s = symname (car (x));
1126 1148
1127 /* case-insensitive, per R5RS section 2 */ 1149 /* case-insensitive, per R5RS section 2 */
1128 if (stricmp (name, s) == 0) 1150 if (stricmp (name, s) == 0)
1130 } 1152 }
1131 1153
1132 return NIL; 1154 return NIL;
1133} 1155}
1134 1156
1135static pointer 1157ecb_cold static pointer
1136oblist_all_symbols (SCHEME_P) 1158oblist_all_symbols (SCHEME_P)
1137{ 1159{
1138 int i; 1160 int i;
1139 pointer x; 1161 pointer x;
1140 pointer ob_list = NIL; 1162 pointer ob_list = NIL;
1141 1163
1142 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1164 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1143 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))
1144 ob_list = cons (x, ob_list); 1166 ob_list = cons (x, ob_list);
1145 1167
1146 return ob_list; 1168 return ob_list;
1147} 1169}
1148 1170
1149#else 1171#else
1150 1172
1151static pointer 1173ecb_cold static pointer
1152oblist_initial_value (SCHEME_P) 1174oblist_initial_value (SCHEME_P)
1153{ 1175{
1154 return NIL; 1176 return NIL;
1155} 1177}
1156 1178
1157static INLINE pointer 1179ecb_cold static pointer
1158oblist_find_by_name (SCHEME_P_ const char *name) 1180oblist_find_by_name (SCHEME_P_ const char *name)
1159{ 1181{
1160 pointer x; 1182 pointer x;
1161 char *s; 1183 char *s;
1162 1184
1171 1193
1172 return NIL; 1194 return NIL;
1173} 1195}
1174 1196
1175/* returns the new symbol */ 1197/* returns the new symbol */
1176static pointer 1198ecb_cold static pointer
1177oblist_add_by_name (SCHEME_P_ const char *name) 1199oblist_add_by_name (SCHEME_P_ const char *name)
1178{ 1200{
1179 pointer x; 1201 pointer x = generate_symbol (SCHEME_A_ name);
1180
1181 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1182 set_typeflag (x, T_SYMBOL);
1183 setimmutable (car (x));
1184 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1202 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1185 return x; 1203 return x;
1186} 1204}
1187 1205
1188static pointer 1206ecb_cold static pointer
1189oblist_all_symbols (SCHEME_P) 1207oblist_all_symbols (SCHEME_P)
1190{ 1208{
1191 return SCHEME_V->oblist; 1209 return SCHEME_V->oblist;
1192} 1210}
1193 1211
1194#endif 1212#endif
1195 1213
1196#if USE_PORTS
1197static pointer 1214ecb_cold static pointer
1198mk_port (SCHEME_P_ port *p) 1215mk_port (SCHEME_P_ port *p)
1199{ 1216{
1200 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1217 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1201 1218
1202 set_typeflag (x, T_PORT | T_ATOM); 1219 set_typeflag (x, T_PORT | T_ATOM);
1203 x->object.port = p; 1220 set_port (x, p);
1204 1221
1205 return x; 1222 return x;
1206} 1223}
1207#endif
1208 1224
1209pointer 1225ecb_cold pointer
1210mk_foreign_func (SCHEME_P_ foreign_func f) 1226mk_foreign_func (SCHEME_P_ foreign_func f)
1211{ 1227{
1212 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1228 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1213 1229
1214 set_typeflag (x, (T_FOREIGN | T_ATOM)); 1230 set_typeflag (x, T_FOREIGN | T_ATOM);
1215 x->object.ff = f; 1231 CELL(x)->object.ff = f;
1216 1232
1217 return x; 1233 return x;
1218} 1234}
1219 1235
1220INTERFACE pointer 1236INTERFACE pointer
1221mk_character (SCHEME_P_ int c) 1237mk_character (SCHEME_P_ int c)
1222{ 1238{
1223 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1239 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1224 1240
1225 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1241 set_typeflag (x, T_CHARACTER | T_ATOM);
1226 ivalue_unchecked (x) = c & 0xff; 1242 set_ivalue (x, c & 0xff);
1227 set_num_integer (x); 1243
1228 return x; 1244 return x;
1229} 1245}
1230 1246
1231/* get number atom (integer) */ 1247/* get number atom (integer) */
1232INTERFACE pointer 1248INTERFACE pointer
1233mk_integer (SCHEME_P_ long num) 1249mk_integer (SCHEME_P_ long n)
1234{ 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 {
1235 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1261 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1236 1262
1237 set_typeflag (x, (T_NUMBER | T_ATOM)); 1263 set_typeflag (x, T_INTEGER | T_ATOM);
1238 ivalue_unchecked (x) = num; 1264 setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */
1239 set_num_integer (x); 1265 set_ivalue (x, n);
1266
1267 *pp = x;
1268 }
1269
1240 return x; 1270 return *pp;
1241} 1271}
1242 1272
1243INTERFACE pointer 1273INTERFACE pointer
1244mk_real (SCHEME_P_ RVALUE n) 1274mk_real (SCHEME_P_ RVALUE n)
1245{ 1275{
1276#if USE_REAL
1246 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1277 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1247 1278
1248 set_typeflag (x, (T_NUMBER | T_ATOM)); 1279 set_typeflag (x, T_REAL | T_ATOM);
1249 rvalue_unchecked (x) = n; 1280 set_rvalue (x, n);
1250 set_num_real (x); 1281
1251 return x; 1282 return x;
1283#else
1284 return mk_integer (SCHEME_A_ n);
1285#endif
1252} 1286}
1253 1287
1254static pointer 1288static pointer
1255mk_number (SCHEME_P_ const num n) 1289mk_number (SCHEME_P_ const num n)
1256{ 1290{
1291#if USE_REAL
1257 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
1258 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1296 return mk_integer (SCHEME_A_ num_ivalue (n));
1259 else 1297#endif
1260 return mk_real (SCHEME_A_ num_get_rvalue (n));
1261} 1298}
1262 1299
1263/* allocate name to string area */ 1300/* allocate name to string area */
1264static char * 1301static char *
1265store_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)
1271 SCHEME_V->no_memory = 1; 1308 SCHEME_V->no_memory = 1;
1272 return SCHEME_V->strbuff; 1309 return SCHEME_V->strbuff;
1273 } 1310 }
1274 1311
1275 if (str) 1312 if (str)
1276 { 1313 memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */
1277 int l = strlen (str);
1278
1279 if (l > len_str)
1280 l = len_str;
1281
1282 memcpy (q, str, l);
1283 q[l] = 0;
1284 }
1285 else 1314 else
1286 {
1287 memset (q, fill, len_str); 1315 memset (q, fill, len_str);
1316
1288 q[len_str] = 0; 1317 q[len_str] = 0;
1289 }
1290 1318
1291 return q; 1319 return q;
1292} 1320}
1293 1321
1294INTERFACE pointer 1322INTERFACE pointer
1308 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1336 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1309 1337
1310 set_typeflag (x, T_STRING | T_ATOM); 1338 set_typeflag (x, T_STRING | T_ATOM);
1311 strvalue (x) = store_string (SCHEME_A_ len, str, 0); 1339 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1312 strlength (x) = len; 1340 strlength (x) = len;
1341
1313 return x; 1342 return x;
1314} 1343}
1315 1344
1316INTERFACE pointer 1345INTERFACE pointer
1317mk_string (SCHEME_P_ const char *str) 1346mk_string (SCHEME_P_ const char *str)
1324{ 1353{
1325 return get_vector_object (SCHEME_A_ len, NIL); 1354 return get_vector_object (SCHEME_A_ len, NIL);
1326} 1355}
1327 1356
1328INTERFACE void 1357INTERFACE void
1329fill_vector (pointer vec, pointer obj) 1358fill_vector (pointer vec, uint32_t start, pointer obj)
1330{ 1359{
1331 int i; 1360 int i;
1332 1361
1333 for (i = 0; i < vec->object.vector.length; i++) 1362 for (i = start; i < veclength (vec); i++)
1334 vecvalue (vec)[i] = obj; 1363 vecvalue (vec)[i] = obj;
1335} 1364}
1336 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
1337INTERFACE pointer 1375INTERFACE pointer
1338vector_elem (pointer vec, uint32_t ielem) 1376vector_get (pointer vec, uint32_t ielem)
1339{ 1377{
1340 return vecvalue(vec)[ielem]; 1378 return vecvalue(vec)[ielem];
1341} 1379}
1342 1380
1343INTERFACE void 1381INTERFACE void
1344set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1382vector_set (pointer vec, uint32_t ielem, pointer a)
1345{ 1383{
1346 vecvalue(vec)[ielem] = a; 1384 vecvalue(vec)[ielem] = a;
1347} 1385}
1348 1386
1349/* get new symbol */ 1387/* get new symbol */
1357 x = oblist_add_by_name (SCHEME_A_ name); 1395 x = oblist_add_by_name (SCHEME_A_ name);
1358 1396
1359 return x; 1397 return x;
1360} 1398}
1361 1399
1362INTERFACE pointer 1400ecb_cold INTERFACE pointer
1363gensym (SCHEME_P) 1401gensym (SCHEME_P)
1364{ 1402{
1365 pointer x; 1403 pointer x;
1366
1367 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1368 {
1369 char name[40] = "gensym-"; 1404 char name[40] = "gensym-";
1370 xnum (name + 7, SCHEME_V->gensym_cnt); 1405 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1371 1406
1372 /* first check oblist */ 1407 return generate_symbol (SCHEME_A_ name);
1373 x = oblist_find_by_name (SCHEME_A_ name); 1408}
1374 1409
1375 if (x == NIL) 1410static int
1376 { 1411is_gensym (SCHEME_P_ pointer x)
1377 x = oblist_add_by_name (SCHEME_A_ name); 1412{
1378 return x; 1413 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1379 }
1380 }
1381
1382 return NIL;
1383} 1414}
1384 1415
1385/* make symbol or number atom from string */ 1416/* make symbol or number atom from string */
1386static pointer 1417ecb_cold static pointer
1387mk_atom (SCHEME_P_ char *q) 1418mk_atom (SCHEME_P_ char *q)
1388{ 1419{
1389 char c, *p; 1420 char c, *p;
1390 int has_dec_point = 0; 1421 int has_dec_point = 0;
1391 int has_fp_exp = 0; 1422 int has_fp_exp = 0;
1441 } 1472 }
1442 else if ((c == 'e') || (c == 'E')) 1473 else if ((c == 'e') || (c == 'E'))
1443 { 1474 {
1444 if (!has_fp_exp) 1475 if (!has_fp_exp)
1445 { 1476 {
1446 has_dec_point = 1; /* decimal point illegal 1477 has_dec_point = 1; /* decimal point illegal from now on */
1447 from now on */
1448 p++; 1478 p++;
1449 1479
1450 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1480 if ((*p == '-') || (*p == '+') || isdigit (*p))
1451 continue; 1481 continue;
1452 } 1482 }
1463 1493
1464 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1494 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1465} 1495}
1466 1496
1467/* make constant */ 1497/* make constant */
1468static pointer 1498ecb_cold static pointer
1469mk_sharp_const (SCHEME_P_ char *name) 1499mk_sharp_const (SCHEME_P_ char *name)
1470{ 1500{
1471 if (!strcmp (name, "t")) 1501 if (!strcmp (name, "t"))
1472 return S_T; 1502 return S_T;
1473 else if (!strcmp (name, "f")) 1503 else if (!strcmp (name, "f"))
1474 return S_F; 1504 return S_F;
1475 else if (*name == '\\') /* #\w (character) */ 1505 else if (*name == '\\') /* #\w (character) */
1476 { 1506 {
1477 int c; 1507 int c;
1478 1508
1509 // TODO: optimise
1479 if (stricmp (name + 1, "space") == 0) 1510 if (stricmp (name + 1, "space") == 0)
1480 c = ' '; 1511 c = ' ';
1481 else if (stricmp (name + 1, "newline") == 0) 1512 else if (stricmp (name + 1, "newline") == 0)
1482 c = '\n'; 1513 c = '\n';
1483 else if (stricmp (name + 1, "return") == 0) 1514 else if (stricmp (name + 1, "return") == 0)
1484 c = '\r'; 1515 c = '\r';
1485 else if (stricmp (name + 1, "tab") == 0) 1516 else if (stricmp (name + 1, "tab") == 0)
1486 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;
1487 else if (name[1] == 'x' && name[2] != 0) 1528 else if (name[1] == 'x' && name[2] != 0)
1488 { 1529 {
1489 long c1 = strtol (name + 2, 0, 16); 1530 long c1 = strtol (name + 2, 0, 16);
1490 1531
1491 if (0 <= c1 && c1 <= UCHAR_MAX) 1532 if (0 <= c1 && c1 <= UCHAR_MAX)
1516 return NIL; 1557 return NIL;
1517 } 1558 }
1518} 1559}
1519 1560
1520/* ========== 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}
1521 1581
1522/*-- 1582/*--
1523 * 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,
1524 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1584 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1525 * for marking. 1585 * for marking.
1526 * 1586 *
1527 * The exception is vectors - vectors are currently marked recursively, 1587 * The exception is vectors - vectors are currently marked recursively,
1528 * 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
1529 * word of context in the vector 1589 * word of context in the vector
1530 */ 1590 */
1531static void 1591ecb_hot static void
1532mark (pointer a) 1592mark (pointer a)
1533{ 1593{
1534 pointer t, q, p; 1594 pointer t, q, p;
1535 1595
1536 t = 0; 1596 t = 0;
1540 1600
1541 if (ecb_expect_false (is_vector (p))) 1601 if (ecb_expect_false (is_vector (p)))
1542 { 1602 {
1543 int i; 1603 int i;
1544 1604
1545 for (i = 0; i < p->object.vector.length; i++) 1605 for (i = 0; i < veclength (p); i++)
1546 mark (vecvalue (p)[i]); 1606 mark (vecvalue (p)[i]);
1547 } 1607 }
1548 1608
1549 if (is_atom (p)) 1609 if (is_atom (p))
1550 goto E6; 1610 goto E6;
1593 p = q; 1653 p = q;
1594 goto E6; 1654 goto E6;
1595 } 1655 }
1596} 1656}
1597 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
1598/* garbage collection. parameter a, b is marked. */ 1700/* garbage collection. parameter a, b is marked. */
1599static void 1701ecb_cold static void
1600gc (SCHEME_P_ pointer a, pointer b) 1702gc (SCHEME_P_ pointer a, pointer b)
1601{ 1703{
1602 pointer p;
1603 int i; 1704 int i;
1604 1705
1605 if (SCHEME_V->gc_verbose) 1706 if (SCHEME_V->gc_verbose)
1606 putstr (SCHEME_A_ "gc..."); 1707 putstr (SCHEME_A_ "gc...");
1607 1708
1623 /* Mark recent objects the interpreter doesn't know about yet. */ 1724 /* Mark recent objects the interpreter doesn't know about yet. */
1624 mark (car (S_SINK)); 1725 mark (car (S_SINK));
1625 /* Mark any older stuff above nested C calls */ 1726 /* Mark any older stuff above nested C calls */
1626 mark (SCHEME_V->c_nest); 1727 mark (SCHEME_V->c_nest);
1627 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
1628 /* mark variables a, b */ 1736 /* mark variables a, b */
1629 mark (a); 1737 mark (a);
1630 mark (b); 1738 mark (b);
1631 1739
1632 /* garbage collect */ 1740 /* garbage collect */
1633 clrmark (NIL); 1741 clrmark (NIL);
1634 SCHEME_V->fcells = 0; 1742 SCHEME_V->fcells = 0;
1635 SCHEME_V->free_cell = NIL; 1743 SCHEME_V->free_cell = NIL;
1636 1744
1637 /* free-list is kept sorted by address so as to maintain consecutive
1638 ranges, if possible, for use with vectors. Here we scan the cells
1639 (which are also kept sorted by address) downwards to build the
1640 free-list in sorted order.
1641 */
1642 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1643 {
1644 p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1645
1646 while (--p >= SCHEME_V->cell_seg[i])
1647 {
1648 if (is_mark (p))
1649 clrmark (p);
1650 else
1651 {
1652 /* reclaim cell */
1653 if (typeflag (p) != T_FREE)
1654 {
1655 finalize_cell (SCHEME_A_ p);
1656 set_typeflag (p, T_FREE);
1657 set_car (p, NIL);
1658 }
1659
1660 ++SCHEME_V->fcells;
1661 set_cdr (p, SCHEME_V->free_cell);
1662 SCHEME_V->free_cell = p;
1663 }
1664 }
1665 }
1666
1667 if (SCHEME_V->gc_verbose) 1745 if (SCHEME_V->gc_verbose)
1668 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1746 putstr (SCHEME_A_ "freeing...");
1669}
1670 1747
1671static void 1748 gc_free (SCHEME_A);
1672finalize_cell (SCHEME_P_ pointer a)
1673{
1674 /* TODO, fast bitmap check? */
1675 if (is_string (a))
1676 free (strvalue (a));
1677 else if (is_vector (a))
1678 free (vecvalue (a));
1679#if USE_PORTS
1680 else if (is_port (a))
1681 {
1682 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit)
1683 port_close (SCHEME_A_ a, port_input | port_output);
1684
1685 free (a->object.port);
1686 }
1687#endif
1688} 1749}
1689 1750
1690/* ========== Routines for Reading ========== */ 1751/* ========== Routines for Reading ========== */
1691 1752
1692static int 1753ecb_cold static int
1693file_push (SCHEME_P_ const char *fname) 1754file_push (SCHEME_P_ const char *fname)
1694{ 1755{
1695#if USE_PORTS
1696 int fin; 1756 int fin;
1697 1757
1698 if (SCHEME_V->file_i == MAXFIL - 1) 1758 if (SCHEME_V->file_i == MAXFIL - 1)
1699 return 0; 1759 return 0;
1700 1760
1706 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; 1766 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1;
1707 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;
1708 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;
1709 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;
1710 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; 1770 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0;
1711 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);
1712 1772
1713#if SHOW_ERROR_LINE 1773#if SHOW_ERROR_LINE
1714 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;
1715 1775
1716 if (fname) 1776 if (fname)
1717 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);
1718#endif 1778#endif
1719 } 1779 }
1720 1780
1721 return fin >= 0; 1781 return fin >= 0;
1722
1723#else
1724 return 1;
1725#endif
1726} 1782}
1727 1783
1728static void 1784ecb_cold static void
1729file_pop (SCHEME_P) 1785file_pop (SCHEME_P)
1730{ 1786{
1731 if (SCHEME_V->file_i != 0) 1787 if (SCHEME_V->file_i != 0)
1732 { 1788 {
1733 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1789 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1734#if USE_PORTS 1790#if USE_PORTS
1735 port_close (SCHEME_A_ SCHEME_V->loadport, port_input); 1791 port_close (SCHEME_A_ SCHEME_V->loadport, port_input);
1736#endif 1792#endif
1737 SCHEME_V->file_i--; 1793 SCHEME_V->file_i--;
1738 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);
1739 } 1795 }
1740} 1796}
1741 1797
1742static int 1798ecb_cold static int
1743file_interactive (SCHEME_P) 1799file_interactive (SCHEME_P)
1744{ 1800{
1745#if USE_PORTS 1801#if USE_PORTS
1746 return SCHEME_V->file_i == 0 1802 return SCHEME_V->file_i == 0
1747 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1803 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1748 && (SCHEME_V->inport->object.port->kind & port_file); 1804 && (port (SCHEME_V->inport)->kind & port_file);
1749#else 1805#else
1750 return 0; 1806 return 0;
1751#endif 1807#endif
1752} 1808}
1753 1809
1754#if USE_PORTS 1810#if USE_PORTS
1755static port * 1811ecb_cold static port *
1756port_rep_from_filename (SCHEME_P_ const char *fn, int prop) 1812port_rep_from_filename (SCHEME_P_ const char *fn, int prop)
1757{ 1813{
1758 int fd; 1814 int fd;
1759 int flags; 1815 int flags;
1760 char *rw; 1816 char *rw;
1783# endif 1839# endif
1784 1840
1785 return pt; 1841 return pt;
1786} 1842}
1787 1843
1788static pointer 1844ecb_cold static pointer
1789port_from_filename (SCHEME_P_ const char *fn, int prop) 1845port_from_filename (SCHEME_P_ const char *fn, int prop)
1790{ 1846{
1791 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); 1847 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop);
1792 1848
1793 if (!pt && USE_ERROR_CHECKING) 1849 if (!pt && USE_ERROR_CHECKING)
1794 return NIL; 1850 return NIL;
1795 1851
1796 return mk_port (SCHEME_A_ pt); 1852 return mk_port (SCHEME_A_ pt);
1797} 1853}
1798 1854
1799static port * 1855ecb_cold static port *
1800port_rep_from_file (SCHEME_P_ int f, int prop) 1856port_rep_from_file (SCHEME_P_ int f, int prop)
1801{ 1857{
1802 port *pt = malloc (sizeof *pt); 1858 port *pt = malloc (sizeof *pt);
1803 1859
1804 if (!pt && USE_ERROR_CHECKING) 1860 if (!pt && USE_ERROR_CHECKING)
1809 pt->rep.stdio.file = f; 1865 pt->rep.stdio.file = f;
1810 pt->rep.stdio.closeit = 0; 1866 pt->rep.stdio.closeit = 0;
1811 return pt; 1867 return pt;
1812} 1868}
1813 1869
1814static pointer 1870ecb_cold static pointer
1815port_from_file (SCHEME_P_ int f, int prop) 1871port_from_file (SCHEME_P_ int f, int prop)
1816{ 1872{
1817 port *pt = port_rep_from_file (SCHEME_A_ f, prop); 1873 port *pt = port_rep_from_file (SCHEME_A_ f, prop);
1818 1874
1819 if (!pt && USE_ERROR_CHECKING) 1875 if (!pt && USE_ERROR_CHECKING)
1820 return NIL; 1876 return NIL;
1821 1877
1822 return mk_port (SCHEME_A_ pt); 1878 return mk_port (SCHEME_A_ pt);
1823} 1879}
1824 1880
1825static port * 1881ecb_cold static port *
1826port_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)
1827{ 1883{
1828 port *pt = malloc (sizeof (port)); 1884 port *pt = malloc (sizeof (port));
1829 1885
1830 if (!pt && USE_ERROR_CHECKING) 1886 if (!pt && USE_ERROR_CHECKING)
1836 pt->rep.string.curr = start; 1892 pt->rep.string.curr = start;
1837 pt->rep.string.past_the_end = past_the_end; 1893 pt->rep.string.past_the_end = past_the_end;
1838 return pt; 1894 return pt;
1839} 1895}
1840 1896
1841static pointer 1897ecb_cold static pointer
1842port_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)
1843{ 1899{
1844 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);
1845 1901
1846 if (!pt && USE_ERROR_CHECKING) 1902 if (!pt && USE_ERROR_CHECKING)
1849 return mk_port (SCHEME_A_ pt); 1905 return mk_port (SCHEME_A_ pt);
1850} 1906}
1851 1907
1852# define BLOCK_SIZE 256 1908# define BLOCK_SIZE 256
1853 1909
1854static port * 1910ecb_cold static port *
1855port_rep_from_scratch (SCHEME_P) 1911port_rep_from_scratch (SCHEME_P)
1856{ 1912{
1857 char *start; 1913 char *start;
1858 port *pt = malloc (sizeof (port)); 1914 port *pt = malloc (sizeof (port));
1859 1915
1873 pt->rep.string.curr = start; 1929 pt->rep.string.curr = start;
1874 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; 1930 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1;
1875 return pt; 1931 return pt;
1876} 1932}
1877 1933
1878static pointer 1934ecb_cold static pointer
1879port_from_scratch (SCHEME_P) 1935port_from_scratch (SCHEME_P)
1880{ 1936{
1881 port *pt = port_rep_from_scratch (SCHEME_A); 1937 port *pt = port_rep_from_scratch (SCHEME_A);
1882 1938
1883 if (!pt && USE_ERROR_CHECKING) 1939 if (!pt && USE_ERROR_CHECKING)
1884 return NIL; 1940 return NIL;
1885 1941
1886 return mk_port (SCHEME_A_ pt); 1942 return mk_port (SCHEME_A_ pt);
1887} 1943}
1888 1944
1889static void 1945ecb_cold static void
1890port_close (SCHEME_P_ pointer p, int flag) 1946port_close (SCHEME_P_ pointer p, int flag)
1891{ 1947{
1892 port *pt = p->object.port; 1948 port *pt = port (p);
1893 1949
1894 pt->kind &= ~flag; 1950 pt->kind &= ~flag;
1895 1951
1896 if ((pt->kind & (port_input | port_output)) == 0) 1952 if ((pt->kind & (port_input | port_output)) == 0)
1897 { 1953 {
1914 } 1970 }
1915} 1971}
1916#endif 1972#endif
1917 1973
1918/* get new character from input file */ 1974/* get new character from input file */
1919static int 1975ecb_cold static int
1920inchar (SCHEME_P) 1976inchar (SCHEME_P)
1921{ 1977{
1922 int c; 1978 int c;
1923 port *pt; 1979 port *pt = port (SCHEME_V->inport);
1924
1925 pt = SCHEME_V->inport->object.port;
1926 1980
1927 if (pt->kind & port_saw_EOF) 1981 if (pt->kind & port_saw_EOF)
1928 return EOF; 1982 return EOF;
1929 1983
1930 c = basic_inchar (pt); 1984 c = basic_inchar (pt);
1940 } 1994 }
1941 1995
1942 return c; 1996 return c;
1943} 1997}
1944 1998
1945static int ungot = -1; 1999ecb_cold static int
1946
1947static int
1948basic_inchar (port *pt) 2000basic_inchar (port *pt)
1949{ 2001{
1950#if USE_PORTS
1951 if (pt->unget != -1) 2002 if (pt->unget != -1)
1952 { 2003 {
1953 int r = pt->unget; 2004 int r = pt->unget;
1954 pt->unget = -1; 2005 pt->unget = -1;
1955 return r; 2006 return r;
1956 } 2007 }
1957 2008
2009#if USE_PORTS
1958 if (pt->kind & port_file) 2010 if (pt->kind & port_file)
1959 { 2011 {
1960 char c; 2012 char c;
1961 2013
1962 if (!read (pt->rep.stdio.file, &c, 1)) 2014 if (!read (pt->rep.stdio.file, &c, 1))
1970 return EOF; 2022 return EOF;
1971 else 2023 else
1972 return *pt->rep.string.curr++; 2024 return *pt->rep.string.curr++;
1973 } 2025 }
1974#else 2026#else
1975 if (ungot == -1)
1976 {
1977 char c; 2027 char c;
1978 if (!read (0, &c, 1)) 2028
2029 if (!read (pt->rep.stdio.file, &c, 1))
1979 return EOF; 2030 return EOF;
1980 2031
1981 ungot = c;
1982 }
1983
1984 {
1985 int r = ungot;
1986 ungot = -1;
1987 return r; 2032 return c;
1988 }
1989#endif 2033#endif
1990} 2034}
1991 2035
1992/* back character to input buffer */ 2036/* back character to input buffer */
1993static void 2037ecb_cold static void
1994backchar (SCHEME_P_ int c) 2038backchar (SCHEME_P_ int c)
1995{ 2039{
1996#if USE_PORTS 2040 port *pt = port (SCHEME_V->inport);
1997 port *pt;
1998 2041
1999 if (c == EOF) 2042 if (c == EOF)
2000 return; 2043 return;
2001 2044
2002 pt = SCHEME_V->inport->object.port;
2003 pt->unget = c; 2045 pt->unget = c;
2004#else
2005 if (c == EOF)
2006 return;
2007
2008 ungot = c;
2009#endif
2010} 2046}
2011 2047
2012#if USE_PORTS 2048#if USE_PORTS
2013static int 2049ecb_cold static int
2014realloc_port_string (SCHEME_P_ port *p) 2050realloc_port_string (SCHEME_P_ port *p)
2015{ 2051{
2016 char *start = p->rep.string.start; 2052 char *start = p->rep.string.start;
2017 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;
2018 char *str = malloc (new_size); 2054 char *str = malloc (new_size);
2031 else 2067 else
2032 return 0; 2068 return 0;
2033} 2069}
2034#endif 2070#endif
2035 2071
2036INTERFACE void 2072ecb_cold static void
2037putstr (SCHEME_P_ const char *s) 2073putchars (SCHEME_P_ const char *s, int len)
2038{ 2074{
2075 port *pt = port (SCHEME_V->outport);
2076
2039#if USE_PORTS 2077#if USE_PORTS
2040 port *pt = SCHEME_V->outport->object.port;
2041
2042 if (pt->kind & port_file)
2043 write (pt->rep.stdio.file, s, strlen (s));
2044 else
2045 for (; *s; s++)
2046 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2047 *pt->rep.string.curr++ = *s;
2048 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2049 *pt->rep.string.curr++ = *s;
2050
2051#else
2052 xwrstr (s);
2053#endif
2054}
2055
2056static void
2057putchars (SCHEME_P_ const char *s, int len)
2058{
2059#if USE_PORTS
2060 port *pt = SCHEME_V->outport->object.port;
2061
2062 if (pt->kind & port_file) 2078 if (pt->kind & port_file)
2063 write (pt->rep.stdio.file, s, len); 2079 write (pt->rep.stdio.file, s, len);
2064 else 2080 else
2065 { 2081 {
2066 for (; len; len--) 2082 for (; len; len--)
2071 *pt->rep.string.curr++ = *s++; 2087 *pt->rep.string.curr++ = *s++;
2072 } 2088 }
2073 } 2089 }
2074 2090
2075#else 2091#else
2076 write (1, s, len); 2092 write (1, s, len); // output not initialised
2077#endif 2093#endif
2094}
2095
2096INTERFACE void
2097putstr (SCHEME_P_ const char *s)
2098{
2099 putchars (SCHEME_A_ s, strlen (s));
2078} 2100}
2079 2101
2080INTERFACE void 2102INTERFACE void
2081putcharacter (SCHEME_P_ int c) 2103putcharacter (SCHEME_P_ int c)
2082{ 2104{
2083#if USE_PORTS
2084 port *pt = SCHEME_V->outport->object.port;
2085
2086 if (pt->kind & port_file)
2087 {
2088 char cc = c;
2089 write (pt->rep.stdio.file, &cc, 1);
2090 }
2091 else
2092 {
2093 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2094 *pt->rep.string.curr++ = c;
2095 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2096 *pt->rep.string.curr++ = c;
2097 }
2098
2099#else
2100 char cc = c; 2105 char cc = c;
2101 write (1, &c, 1); 2106
2102#endif 2107 putchars (SCHEME_A_ &cc, 1);
2103} 2108}
2104 2109
2105/* read characters up to delimiter, but cater to character constants */ 2110/* read characters up to delimiter, but cater to character constants */
2106static char * 2111ecb_cold static char *
2107readstr_upto (SCHEME_P_ char *delim) 2112readstr_upto (SCHEME_P_ int skip, const char *delim)
2108{ 2113{
2109 char *p = SCHEME_V->strbuff; 2114 char *p = SCHEME_V->strbuff + skip;
2110 2115
2111 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))));
2112 2117
2113 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2118 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2114 *p = 0; 2119 *p = 0;
2120 2125
2121 return SCHEME_V->strbuff; 2126 return SCHEME_V->strbuff;
2122} 2127}
2123 2128
2124/* read string expression "xxx...xxx" */ 2129/* read string expression "xxx...xxx" */
2125static pointer 2130ecb_cold static pointer
2126readstrexp (SCHEME_P) 2131readstrexp (SCHEME_P_ char delim)
2127{ 2132{
2128 char *p = SCHEME_V->strbuff; 2133 char *p = SCHEME_V->strbuff;
2129 int c; 2134 int c;
2130 int c1 = 0; 2135 int c1 = 0;
2131 enum
2132 { 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;
2133 2137
2134 for (;;) 2138 for (;;)
2135 { 2139 {
2136 c = inchar (SCHEME_A); 2140 c = inchar (SCHEME_A);
2137 2141
2139 return S_F; 2143 return S_F;
2140 2144
2141 switch (state) 2145 switch (state)
2142 { 2146 {
2143 case st_ok: 2147 case st_ok:
2144 switch (c) 2148 if (ecb_expect_false (c == delim))
2145 {
2146 case '\\':
2147 state = st_bsl;
2148 break;
2149
2150 case '"':
2151 *p = 0;
2152 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);
2153 2150
2154 default: 2151 if (ecb_expect_false (c == '\\'))
2152 state = st_bsl;
2153 else
2155 *p++ = c; 2154 *p++ = c;
2156 break;
2157 }
2158 2155
2159 break; 2156 break;
2160 2157
2161 case st_bsl: 2158 case st_bsl:
2162 switch (c) 2159 switch (c)
2171 case '7': 2168 case '7':
2172 state = st_oct1; 2169 state = st_oct1;
2173 c1 = c - '0'; 2170 c1 = c - '0';
2174 break; 2171 break;
2175 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
2176 case 'x': 2188 case 'x':
2177 case 'X': 2189 case 'X':
2178 state = st_x1; 2190 state = st_x1;
2179 c1 = 0; 2191 c1 = 0;
2180 break; 2192 break;
2181 2193
2182 case 'n':
2183 *p++ = '\n';
2184 state = st_ok;
2185 break;
2186
2187 case 't':
2188 *p++ = '\t';
2189 state = st_ok;
2190 break;
2191
2192 case 'r':
2193 *p++ = '\r';
2194 state = st_ok;
2195 break;
2196
2197 case '"':
2198 *p++ = '"';
2199 state = st_ok;
2200 break;
2201
2202 default: 2194 default:
2203 *p++ = c; 2195 *p++ = c;
2204 state = st_ok; 2196 state = st_ok;
2205 break; 2197 break;
2206 } 2198 }
2207 2199
2208 break; 2200 break;
2209 2201
2210 case st_x1: 2202 case st_x1:
2211 case st_x2: 2203 case st_x2:
2212 c = toupper (c); 2204 c = tolower (c);
2213 2205
2214 if (c >= '0' && c <= 'F') 2206 if (c >= '0' && c <= '9')
2215 {
2216 if (c <= '9')
2217 c1 = (c1 << 4) + c - '0'; 2207 c1 = (c1 << 4) + c - '0';
2218 else 2208 else if (c >= 'a' && c <= 'f')
2219 c1 = (c1 << 4) + c - 'A' + 10; 2209 c1 = (c1 << 4) + c - 'a' + 10;
2220
2221 if (state == st_x1)
2222 state = st_x2;
2223 else
2224 {
2225 *p++ = c1;
2226 state = st_ok;
2227 }
2228 }
2229 else 2210 else
2230 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 }
2231 2220
2232 break; 2221 break;
2233 2222
2234 case st_oct1: 2223 case st_oct1:
2235 case st_oct2: 2224 case st_oct2:
2239 backchar (SCHEME_A_ c); 2228 backchar (SCHEME_A_ c);
2240 state = st_ok; 2229 state = st_ok;
2241 } 2230 }
2242 else 2231 else
2243 { 2232 {
2244 if (state == st_oct2 && c1 >= 32) 2233 if (state == st_oct2 && c1 >= ' ')
2245 return S_F; 2234 return S_F;
2246 2235
2247 c1 = (c1 << 3) + (c - '0'); 2236 c1 = (c1 << 3) + (c - '0');
2248 2237
2249 if (state == st_oct1) 2238 if (state == st_oct1)
2254 state = st_ok; 2243 state = st_ok;
2255 } 2244 }
2256 } 2245 }
2257 2246
2258 break; 2247 break;
2259
2260 } 2248 }
2261 } 2249 }
2262} 2250}
2263 2251
2264/* check c is in chars */ 2252/* check c is in chars */
2265static INLINE int 2253ecb_cold int
2266is_one_of (char *s, int c) 2254is_one_of (const char *s, int c)
2267{ 2255{
2268 if (c == EOF)
2269 return 1;
2270
2271 return !!strchr (s, c); 2256 return c == EOF || !!strchr (s, c);
2272} 2257}
2273 2258
2274/* skip white characters */ 2259/* skip white characters */
2275static INLINE int 2260ecb_cold int
2276skipspace (SCHEME_P) 2261skipspace (SCHEME_P)
2277{ 2262{
2278 int c, curr_line = 0; 2263 int c, curr_line = 0;
2279 2264
2280 do 2265 do
2281 { 2266 {
2282 c = inchar (SCHEME_A); 2267 c = inchar (SCHEME_A);
2268
2283#if SHOW_ERROR_LINE 2269#if SHOW_ERROR_LINE
2284 if (c == '\n') 2270 if (ecb_expect_false (c == '\n'))
2285 curr_line++; 2271 curr_line++;
2286#endif 2272#endif
2273
2274 if (ecb_expect_false (c == EOF))
2275 return c;
2287 } 2276 }
2288 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2277 while (is_one_of (WHITESPACE, c));
2289 2278
2290 /* record it */ 2279 /* record it */
2291#if SHOW_ERROR_LINE 2280#if SHOW_ERROR_LINE
2292 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)
2293 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;
2294#endif 2283#endif
2295 2284
2296 if (c != EOF)
2297 {
2298 backchar (SCHEME_A_ c); 2285 backchar (SCHEME_A_ c);
2299 return 1; 2286 return 1;
2300 }
2301 else
2302 return EOF;
2303} 2287}
2304 2288
2305/* get token */ 2289/* get token */
2306static int 2290ecb_cold static int
2307token (SCHEME_P) 2291token (SCHEME_P)
2308{ 2292{
2309 int c = skipspace (SCHEME_A); 2293 int c = skipspace (SCHEME_A);
2310 2294
2311 if (c == EOF) 2295 if (c == EOF)
2323 return TOK_RPAREN; 2307 return TOK_RPAREN;
2324 2308
2325 case '.': 2309 case '.':
2326 c = inchar (SCHEME_A); 2310 c = inchar (SCHEME_A);
2327 2311
2328 if (is_one_of (" \n\t", c)) 2312 if (is_one_of (WHITESPACE, c))
2329 return TOK_DOT; 2313 return TOK_DOT;
2330 else 2314 else
2331 { 2315 {
2332 //TODO: ungetc twice in a row is not supported in C
2333 backchar (SCHEME_A_ c); 2316 backchar (SCHEME_A_ c);
2334 backchar (SCHEME_A_ '.');
2335 return TOK_ATOM; 2317 return TOK_DOTATOM;
2336 } 2318 }
2319
2320 case '|':
2321 return TOK_STRATOM;
2337 2322
2338 case '\'': 2323 case '\'':
2339 return TOK_QUOTE; 2324 return TOK_QUOTE;
2340 2325
2341 case ';': 2326 case ';':
2408} 2393}
2409 2394
2410/* ========== Routines for Printing ========== */ 2395/* ========== Routines for Printing ========== */
2411#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) 2396#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL)
2412 2397
2413static void 2398ecb_cold static void
2414printslashstring (SCHEME_P_ char *p, int len) 2399printslashstring (SCHEME_P_ char *p, int len)
2415{ 2400{
2416 int i; 2401 int i;
2417 unsigned char *s = (unsigned char *) p; 2402 unsigned char *s = (unsigned char *) p;
2418 2403
2473 } 2458 }
2474 2459
2475 putcharacter (SCHEME_A_ '"'); 2460 putcharacter (SCHEME_A_ '"');
2476} 2461}
2477 2462
2478
2479/* print atoms */ 2463/* print atoms */
2480static void 2464ecb_cold static void
2481printatom (SCHEME_P_ pointer l, int f) 2465printatom (SCHEME_P_ pointer l, int f)
2482{ 2466{
2483 char *p; 2467 char *p;
2484 int len; 2468 int len;
2485 2469
2486 atom2str (SCHEME_A_ l, f, &p, &len); 2470 atom2str (SCHEME_A_ l, f, &p, &len);
2487 putchars (SCHEME_A_ p, len); 2471 putchars (SCHEME_A_ p, len);
2488} 2472}
2489 2473
2490
2491/* Uses internal buffer unless string pointer is already available */ 2474/* Uses internal buffer unless string pointer is already available */
2492static void 2475ecb_cold static void
2493atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2476atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2494{ 2477{
2495 char *p; 2478 char *p;
2496 2479
2497 if (l == NIL) 2480 if (l == NIL)
2508 { 2491 {
2509 p = SCHEME_V->strbuff; 2492 p = SCHEME_V->strbuff;
2510 2493
2511 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 */
2512 { 2495 {
2513 if (num_is_integer (l)) 2496 if (is_integer (l))
2514 xnum (p, ivalue_unchecked (l)); 2497 xnum (p, ivalue_unchecked (l));
2515#if USE_REAL 2498#if USE_REAL
2516 else 2499 else
2517 { 2500 {
2518 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2501 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2655#endif 2638#endif
2656 } 2639 }
2657 else if (is_continuation (l)) 2640 else if (is_continuation (l))
2658 p = "#<CONTINUATION>"; 2641 p = "#<CONTINUATION>";
2659 else 2642 else
2643 {
2644#if USE_PRINTF
2645 p = SCHEME_V->strbuff;
2646 snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l));
2647#else
2660 p = "#<ERROR>"; 2648 p = "#<ERROR>";
2649#endif
2650 }
2661 2651
2662 *pp = p; 2652 *pp = p;
2663 *plen = strlen (p); 2653 *plen = strlen (p);
2664} 2654}
2665 2655
2697 return car (d); 2687 return car (d);
2698 2688
2699 p = cons (car (d), cdr (d)); 2689 p = cons (car (d), cdr (d));
2700 q = p; 2690 q = p;
2701 2691
2702 while (cdr (cdr (p)) != NIL) 2692 while (cddr (p) != NIL)
2703 { 2693 {
2704 d = cons (car (p), cdr (p)); 2694 d = cons (car (p), cdr (p));
2705 2695
2706 if (cdr (cdr (p)) != NIL) 2696 if (cddr (p) != NIL)
2707 p = cdr (d); 2697 p = cdr (d);
2708 } 2698 }
2709 2699
2710 set_cdr (p, car (cdr (p))); 2700 set_cdr (p, cadr (p));
2711 return q; 2701 return q;
2712} 2702}
2713 2703
2714/* reverse list -- produce new list */ 2704/* reverse list -- produce new list */
2715static pointer 2705ecb_hot static pointer
2716reverse (SCHEME_P_ pointer a) 2706reverse (SCHEME_P_ pointer a)
2717{ 2707{
2718 /* a must be checked by gc */ 2708 /* a must be checked by gc */
2719 pointer p = NIL; 2709 pointer p = NIL;
2720 2710
2723 2713
2724 return p; 2714 return p;
2725} 2715}
2726 2716
2727/* reverse list --- in-place */ 2717/* reverse list --- in-place */
2728static pointer 2718ecb_hot static pointer
2729reverse_in_place (SCHEME_P_ pointer term, pointer list) 2719reverse_in_place (SCHEME_P_ pointer term, pointer list)
2730{ 2720{
2731 pointer result = term; 2721 pointer result = term;
2732 pointer p = list; 2722 pointer p = list;
2733 2723
2741 2731
2742 return result; 2732 return result;
2743} 2733}
2744 2734
2745/* append list -- produce new list (in reverse order) */ 2735/* append list -- produce new list (in reverse order) */
2746static pointer 2736ecb_hot static pointer
2747revappend (SCHEME_P_ pointer a, pointer b) 2737revappend (SCHEME_P_ pointer a, pointer b)
2748{ 2738{
2749 pointer result = a; 2739 pointer result = a;
2750 pointer p = b; 2740 pointer p = b;
2751 2741
2760 2750
2761 return S_F; /* signal an error */ 2751 return S_F; /* signal an error */
2762} 2752}
2763 2753
2764/* equivalence of atoms */ 2754/* equivalence of atoms */
2765int 2755ecb_hot int
2766eqv (pointer a, pointer b) 2756eqv (pointer a, pointer b)
2767{ 2757{
2768 if (is_string (a)) 2758 if (is_string (a))
2769 { 2759 {
2770 if (is_string (b)) 2760 if (is_string (b))
2773 return 0; 2763 return 0;
2774 } 2764 }
2775 else if (is_number (a)) 2765 else if (is_number (a))
2776 { 2766 {
2777 if (is_number (b)) 2767 if (is_number (b))
2778 if (num_is_integer (a) == num_is_integer (b))
2779 return num_eq (nvalue (a), nvalue (b)); 2768 return num_cmp (nvalue (a), nvalue (b)) == 0;
2780 2769
2781 return 0; 2770 return 0;
2782 } 2771 }
2783 else if (is_character (a)) 2772 else if (is_character (a))
2784 { 2773 {
2810/* () is #t in R5RS */ 2799/* () is #t in R5RS */
2811#define is_true(p) ((p) != S_F) 2800#define is_true(p) ((p) != S_F)
2812#define is_false(p) ((p) == S_F) 2801#define is_false(p) ((p) == S_F)
2813 2802
2814/* ========== Environment implementation ========== */ 2803/* ========== Environment implementation ========== */
2815
2816#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST)
2817
2818static int
2819hash_fn (const char *key, int table_size)
2820{
2821 const unsigned char *p = key;
2822 uint32_t hash = 2166136261;
2823
2824 while (*p)
2825 hash = (hash ^ *p++) * 16777619;
2826
2827 return hash % table_size;
2828}
2829#endif
2830 2804
2831#ifndef USE_ALIST_ENV 2805#ifndef USE_ALIST_ENV
2832 2806
2833/* 2807/*
2834 * In this implementation, each frame of the environment may be 2808 * In this implementation, each frame of the environment may be
2851 2825
2852 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2826 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2853 setenvironment (SCHEME_V->envir); 2827 setenvironment (SCHEME_V->envir);
2854} 2828}
2855 2829
2856static 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
2857new_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)
2858{ 2847{
2859 pointer slot = immutable_cons (variable, value); 2848 pointer slot = immutable_cons (variable, value);
2860 2849
2861 if (is_vector (car (env))) 2850 if (is_vector (car (env)))
2862 { 2851 {
2863 int location = hash_fn (symname (variable), veclength (car (env))); 2852 int location = sym_hash (variable, veclength (car (env)));
2864
2865 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)));
2866 } 2854 }
2867 else 2855 else
2868 set_car (env, immutable_cons (slot, car (env))); 2856 set_car (env, immutable_cons (slot, car (env)));
2869} 2857}
2870 2858
2871static pointer 2859ecb_hot static pointer
2872find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2860find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2873{ 2861{
2874 pointer x, y; 2862 pointer x, y;
2875 int location;
2876 2863
2877 for (x = env; x != NIL; x = cdr (x)) 2864 for (x = env; x != NIL; x = cdr (x))
2878 { 2865 {
2879 if (is_vector (car (x))) 2866 if (is_vector (car (x)))
2880 { 2867 {
2881 location = hash_fn (symname (hdl), veclength (car (x))); 2868 int location = sym_hash (hdl, veclength (car (x)));
2882 y = vector_elem (car (x), location); 2869 y = vector_get (car (x), location);
2883 } 2870 }
2884 else 2871 else
2885 y = car (x); 2872 y = car (x);
2886 2873
2887 for (; y != NIL; y = cdr (y)) 2874 for (; y != NIL; y = cdr (y))
2888 if (caar (y) == hdl) 2875 if (caar (y) == hdl)
2889 break; 2876 break;
2890 2877
2891 if (y != NIL) 2878 if (y != NIL)
2879 return car (y);
2880
2881 if (!all)
2892 break; 2882 break;
2893
2894 if (!all)
2895 return NIL;
2896 } 2883 }
2897
2898 if (x != NIL)
2899 return car (y);
2900 2884
2901 return NIL; 2885 return NIL;
2902} 2886}
2903 2887
2904#else /* USE_ALIST_ENV */ 2888#else /* USE_ALIST_ENV */
2905 2889
2906static INLINE void 2890static void
2907new_frame_in_env (SCHEME_P_ pointer old_env) 2891new_frame_in_env (SCHEME_P_ pointer old_env)
2908{ 2892{
2909 SCHEME_V->envir = immutable_cons (NIL, old_env); 2893 SCHEME_V->envir = immutable_cons (NIL, old_env);
2910 setenvironment (SCHEME_V->envir); 2894 setenvironment (SCHEME_V->envir);
2911} 2895}
2912 2896
2913static INLINE void 2897static void
2914new_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)
2915{ 2899{
2916 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2900 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2917} 2901}
2918 2902
2919static pointer 2903ecb_hot static pointer
2920find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2904find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2921{ 2905{
2922 pointer x, y; 2906 pointer x, y;
2923 2907
2924 for (x = env; x != NIL; x = cdr (x)) 2908 for (x = env; x != NIL; x = cdr (x))
2926 for (y = car (x); y != NIL; y = cdr (y)) 2910 for (y = car (x); y != NIL; y = cdr (y))
2927 if (caar (y) == hdl) 2911 if (caar (y) == hdl)
2928 break; 2912 break;
2929 2913
2930 if (y != NIL) 2914 if (y != NIL)
2915 return car (y);
2931 break; 2916 break;
2932 2917
2933 if (!all) 2918 if (!all)
2934 return NIL; 2919 break;
2935 } 2920 }
2936
2937 if (x != NIL)
2938 return car (y);
2939 2921
2940 return NIL; 2922 return NIL;
2941} 2923}
2942 2924
2943#endif /* USE_ALIST_ENV else */ 2925#endif /* USE_ALIST_ENV else */
2944 2926
2945static INLINE void 2927static void
2946new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2928new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2947{ 2929{
2930 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2948 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);
2949} 2932}
2950 2933
2951static INLINE void 2934static void
2952set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2935set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2953{ 2936{
2954 set_cdr (slot, value); 2937 set_cdr (slot, value);
2955} 2938}
2956 2939
2957static INLINE pointer 2940static pointer
2958slot_value_in_env (pointer slot) 2941slot_value_in_env (pointer slot)
2959{ 2942{
2960 return cdr (slot); 2943 return cdr (slot);
2961} 2944}
2962 2945
2963/* ========== Evaluation Cycle ========== */ 2946/* ========== Evaluation Cycle ========== */
2964 2947
2965static pointer 2948ecb_cold static int
2966xError_1 (SCHEME_P_ const char *s, pointer a) 2949xError_1 (SCHEME_P_ const char *s, pointer a)
2967{ 2950{
2968#if USE_ERROR_HOOK
2969 pointer x;
2970 pointer hdl = SCHEME_V->ERROR_HOOK;
2971#endif
2972
2973#if USE_PRINTF 2951#if USE_PRINTF
2974#if SHOW_ERROR_LINE 2952#if SHOW_ERROR_LINE
2975 char sbuf[STRBUFFSIZE]; 2953 char sbuf[STRBUFFSIZE];
2976 2954
2977 /* make sure error is not in REPL */ 2955 /* make sure error is not in REPL */
2992 } 2970 }
2993#endif 2971#endif
2994#endif 2972#endif
2995 2973
2996#if USE_ERROR_HOOK 2974#if USE_ERROR_HOOK
2997 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);
2998 2976
2999 if (x != NIL) 2977 if (x != NIL)
3000 { 2978 {
3001 pointer code = a 2979 pointer code = a
3002 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) 2980 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL)
3005 code = cons (mk_string (SCHEME_A_ s), code); 2983 code = cons (mk_string (SCHEME_A_ s), code);
3006 setimmutable (car (code)); 2984 setimmutable (car (code));
3007 SCHEME_V->code = cons (slot_value_in_env (x), code); 2985 SCHEME_V->code = cons (slot_value_in_env (x), code);
3008 SCHEME_V->op = OP_EVAL; 2986 SCHEME_V->op = OP_EVAL;
3009 2987
3010 return S_T; 2988 return 0;
3011 } 2989 }
3012#endif 2990#endif
3013 2991
3014 if (a) 2992 if (a)
3015 SCHEME_V->args = cons (a, NIL); 2993 SCHEME_V->args = cons (a, NIL);
3017 SCHEME_V->args = NIL; 2995 SCHEME_V->args = NIL;
3018 2996
3019 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);
3020 setimmutable (car (SCHEME_V->args)); 2998 setimmutable (car (SCHEME_V->args));
3021 SCHEME_V->op = OP_ERR0; 2999 SCHEME_V->op = OP_ERR0;
3000
3022 return S_T; 3001 return 0;
3023} 3002}
3024 3003
3025#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)
3026#define Error_0(s) Error_1 (s, 0) 3005#define Error_0(s) Error_1 (s, 0)
3027 3006
3028/* Too small to turn into function */ 3007/* Too small to turn into function */
3029#define BEGIN do { 3008#define BEGIN do {
3030#define END } while (0) 3009#define END } while (0)
3031#define s_goto(a) BEGIN \ 3010#define s_goto(a) BEGIN \
3032 SCHEME_V->op = a; \ 3011 SCHEME_V->op = a; \
3033 return S_T; END 3012 return 0; END
3034 3013
3035#define s_return(a) return xs_return (SCHEME_A_ a) 3014#define s_return(a) return xs_return (SCHEME_A_ a)
3036 3015
3037#ifndef USE_SCHEME_STACK 3016#ifndef USE_SCHEME_STACK
3038 3017
3045 pointer code; 3024 pointer code;
3046}; 3025};
3047 3026
3048# define STACK_GROWTH 3 3027# define STACK_GROWTH 3
3049 3028
3050static void 3029ecb_hot static void
3051s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3030s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3052{ 3031{
3053 int nframes = (uintptr_t)SCHEME_V->dump; 3032 int nframes = (uintptr_t)SCHEME_V->dump;
3054 struct dump_stack_frame *next_frame; 3033 struct dump_stack_frame *next_frame;
3055 3034
3056 /* enough room for the next frame? */ 3035 /* enough room for the next frame? */
3057 if (nframes >= SCHEME_V->dump_size) 3036 if (ecb_expect_false (nframes >= SCHEME_V->dump_size))
3058 { 3037 {
3059 SCHEME_V->dump_size += STACK_GROWTH; 3038 SCHEME_V->dump_size += STACK_GROWTH;
3060 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);
3061 } 3040 }
3062 3041
3063 next_frame = SCHEME_V->dump_base + nframes; 3042 next_frame = SCHEME_V->dump_base + nframes;
3064 3043
3065 next_frame->op = op; 3044 next_frame->op = op;
3066 next_frame->args = args; 3045 next_frame->args = args;
3067 next_frame->envir = SCHEME_V->envir; 3046 next_frame->envir = SCHEME_V->envir;
3068 next_frame->code = code; 3047 next_frame->code = code;
3069 3048
3070 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3049 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3071} 3050}
3072 3051
3073static pointer 3052static ecb_hot int
3074xs_return (SCHEME_P_ pointer a) 3053xs_return (SCHEME_P_ pointer a)
3075{ 3054{
3076 int nframes = (uintptr_t)SCHEME_V->dump; 3055 int nframes = (uintptr_t)SCHEME_V->dump;
3077 struct dump_stack_frame *frame; 3056 struct dump_stack_frame *frame;
3078 3057
3079 SCHEME_V->value = a; 3058 SCHEME_V->value = a;
3080 3059
3081 if (nframes <= 0) 3060 if (nframes <= 0)
3082 return NIL; 3061 return -1;
3083 3062
3084 frame = &SCHEME_V->dump_base[--nframes]; 3063 frame = &SCHEME_V->dump_base[--nframes];
3085 SCHEME_V->op = frame->op; 3064 SCHEME_V->op = frame->op;
3086 SCHEME_V->args = frame->args; 3065 SCHEME_V->args = frame->args;
3087 SCHEME_V->envir = frame->envir; 3066 SCHEME_V->envir = frame->envir;
3088 SCHEME_V->code = frame->code; 3067 SCHEME_V->code = frame->code;
3089 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3068 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3090 3069
3091 return S_T; 3070 return 0;
3092} 3071}
3093 3072
3094static INLINE void 3073ecb_cold void
3095dump_stack_reset (SCHEME_P) 3074dump_stack_reset (SCHEME_P)
3096{ 3075{
3097 /* 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 */
3098 SCHEME_V->dump = (pointer)+0; 3077 SCHEME_V->dump = (pointer)+0;
3099} 3078}
3100 3079
3101static INLINE void 3080ecb_cold void
3102dump_stack_initialize (SCHEME_P) 3081dump_stack_initialize (SCHEME_P)
3103{ 3082{
3104 SCHEME_V->dump_size = 0; 3083 SCHEME_V->dump_size = 0;
3105 SCHEME_V->dump_base = 0; 3084 SCHEME_V->dump_base = 0;
3106 dump_stack_reset (SCHEME_A); 3085 dump_stack_reset (SCHEME_A);
3107} 3086}
3108 3087
3109static void 3088ecb_cold static void
3110dump_stack_free (SCHEME_P) 3089dump_stack_free (SCHEME_P)
3111{ 3090{
3112 free (SCHEME_V->dump_base); 3091 free (SCHEME_V->dump_base);
3113 SCHEME_V->dump_base = 0; 3092 SCHEME_V->dump_base = 0;
3114 SCHEME_V->dump = (pointer)0; 3093 SCHEME_V->dump = (pointer)0;
3115 SCHEME_V->dump_size = 0; 3094 SCHEME_V->dump_size = 0;
3116} 3095}
3117 3096
3118static void 3097ecb_cold static void
3119dump_stack_mark (SCHEME_P) 3098dump_stack_mark (SCHEME_P)
3120{ 3099{
3121 int nframes = (uintptr_t)SCHEME_V->dump; 3100 int nframes = (uintptr_t)SCHEME_V->dump;
3122 int i; 3101 int i;
3123 3102
3129 mark (frame->envir); 3108 mark (frame->envir);
3130 mark (frame->code); 3109 mark (frame->code);
3131 } 3110 }
3132} 3111}
3133 3112
3134static pointer 3113ecb_cold static pointer
3135ss_get_cont (SCHEME_P) 3114ss_get_cont (SCHEME_P)
3136{ 3115{
3137 int nframes = (uintptr_t)SCHEME_V->dump; 3116 int nframes = (uintptr_t)SCHEME_V->dump;
3138 int i; 3117 int i;
3139 3118
3151 } 3130 }
3152 3131
3153 return cont; 3132 return cont;
3154} 3133}
3155 3134
3156static void 3135ecb_cold static void
3157ss_set_cont (SCHEME_P_ pointer cont) 3136ss_set_cont (SCHEME_P_ pointer cont)
3158{ 3137{
3159 int i = 0; 3138 int i = 0;
3160 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3139 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3161 3140
3162 while (cont != NIL) 3141 while (cont != NIL)
3163 { 3142 {
3164 frame->op = ivalue (car (cont)); cont = cdr (cont); 3143 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3165 frame->args = car (cont) ; cont = cdr (cont); 3144 frame->args = car (cont) ; cont = cdr (cont);
3166 frame->envir = car (cont) ; cont = cdr (cont); 3145 frame->envir = car (cont) ; cont = cdr (cont);
3167 frame->code = car (cont) ; cont = cdr (cont); 3146 frame->code = car (cont) ; cont = cdr (cont);
3168 3147
3169 ++frame; 3148 ++frame;
3170 ++i; 3149 ++i;
3171 } 3150 }
3172 3151
3173 SCHEME_V->dump = (pointer)(uintptr_t)i; 3152 SCHEME_V->dump = (pointer)(uintptr_t)i;
3174} 3153}
3175 3154
3176#else 3155#else
3177 3156
3178static INLINE void 3157ecb_cold void
3179dump_stack_reset (SCHEME_P) 3158dump_stack_reset (SCHEME_P)
3180{ 3159{
3181 SCHEME_V->dump = NIL; 3160 SCHEME_V->dump = NIL;
3182} 3161}
3183 3162
3184static INLINE void 3163ecb_cold void
3185dump_stack_initialize (SCHEME_P) 3164dump_stack_initialize (SCHEME_P)
3186{ 3165{
3187 dump_stack_reset (SCHEME_A); 3166 dump_stack_reset (SCHEME_A);
3188} 3167}
3189 3168
3190static void 3169ecb_cold static void
3191dump_stack_free (SCHEME_P) 3170dump_stack_free (SCHEME_P)
3192{ 3171{
3193 SCHEME_V->dump = NIL; 3172 SCHEME_V->dump = NIL;
3194} 3173}
3195 3174
3196static pointer 3175ecb_hot static int
3197xs_return (SCHEME_P_ pointer a) 3176xs_return (SCHEME_P_ pointer a)
3198{ 3177{
3199 pointer dump = SCHEME_V->dump; 3178 pointer dump = SCHEME_V->dump;
3200 3179
3201 SCHEME_V->value = a; 3180 SCHEME_V->value = a;
3202 3181
3203 if (dump == NIL) 3182 if (dump == NIL)
3204 return NIL; 3183 return -1;
3205 3184
3206 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3185 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3207 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3186 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3208 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3187 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3209 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3188 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3210 3189
3211 SCHEME_V->dump = dump; 3190 SCHEME_V->dump = dump;
3212 3191
3213 return S_T; 3192 return 0;
3214} 3193}
3215 3194
3216static void 3195ecb_hot static void
3217s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3196s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3218{ 3197{
3219 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), 3198 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op),
3220 cons (args, 3199 cons (args,
3221 cons (SCHEME_V->envir, 3200 cons (SCHEME_V->envir,
3222 cons (code, 3201 cons (code,
3223 SCHEME_V->dump)))); 3202 SCHEME_V->dump))));
3224} 3203}
3225 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
3226static void 3417static void
3227dump_stack_mark (SCHEME_P) 3418compile_if (SCHEME_P_ stream s, pointer cond, pointer ift, pointer iff)
3228{ 3419{
3229 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);
3230} 3496}
3231 3497
3232static pointer 3498static pointer
3233ss_get_cont (SCHEME_P) 3499lookup (SCHEME_P_ pointer x)
3234{ 3500{
3235 return SCHEME_V->dump; 3501 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, x, 1);
3236}
3237 3502
3238static void 3503 if (x != NIL)
3239ss_set_cont (SCHEME_P_ pointer cont) 3504 x = slot_value_in_env (x);
3240{
3241 SCHEME_V->dump = cont;
3242}
3243 3505
3244#endif 3506 return x;
3507}
3245 3508
3246#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 }
3247 3517
3248static 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
3249opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3712opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3250{ 3713{
3714 pointer args = SCHEME_V->args;
3251 pointer x, y; 3715 pointer x, y;
3252 3716
3253 switch (op) 3717 switch (op)
3254 { 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
3255 case OP_LOAD: /* load */ 3732 case OP_LOAD: /* load */
3256 if (file_interactive (SCHEME_A)) 3733 if (file_interactive (SCHEME_A))
3257 { 3734 {
3258 xwrstr ("Loading "); xwrstr (strvalue (car (SCHEME_V->args))); xwrstr ("\n"); 3735 putstr (SCHEME_A_ "Loading ");
3259 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (SCHEME_V->args))); 3736 putstr (SCHEME_A_ strvalue (car (args)));
3737 putcharacter (SCHEME_A_ '\n');
3260 } 3738 }
3261 3739
3262 if (!file_push (SCHEME_A_ strvalue (car (SCHEME_V->args)))) 3740 if (!file_push (SCHEME_A_ strvalue (car (args))))
3263 Error_1 ("unable to open", car (SCHEME_V->args)); 3741 Error_1 ("unable to open", car (args));
3264 else 3742
3265 {
3266 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3743 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3267 s_goto (OP_T0LVL); 3744 s_goto (OP_T0LVL);
3268 }
3269 3745
3270 case OP_T0LVL: /* top level */ 3746 case OP_T0LVL: /* top level */
3271 3747
3272 /* If we reached the end of file, this loop is done. */ 3748 /* If we reached the end of file, this loop is done. */
3273 if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) 3749 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3274 { 3750 {
3275 if (SCHEME_V->file_i == 0) 3751 if (SCHEME_V->file_i == 0)
3276 { 3752 {
3277 SCHEME_V->args = NIL; 3753 SCHEME_V->args = NIL;
3278 s_goto (OP_QUIT); 3754 s_goto (OP_QUIT);
3289 /* If interactive, be nice to user. */ 3765 /* If interactive, be nice to user. */
3290 if (file_interactive (SCHEME_A)) 3766 if (file_interactive (SCHEME_A))
3291 { 3767 {
3292 SCHEME_V->envir = SCHEME_V->global_env; 3768 SCHEME_V->envir = SCHEME_V->global_env;
3293 dump_stack_reset (SCHEME_A); 3769 dump_stack_reset (SCHEME_A);
3294 putstr (SCHEME_A_ "\n"); 3770 putcharacter (SCHEME_A_ '\n');
3771#if EXPERIMENT
3772 system ("ps v $PPID");
3773#endif
3295 putstr (SCHEME_A_ prompt); 3774 putstr (SCHEME_A_ prompt);
3296 } 3775 }
3297 3776
3298 /* Set up another iteration of REPL */ 3777 /* Set up another iteration of REPL */
3299 SCHEME_V->nesting = 0; 3778 SCHEME_V->nesting = 0;
3334 { 3813 {
3335 SCHEME_V->print_flag = 1; 3814 SCHEME_V->print_flag = 1;
3336 SCHEME_V->args = SCHEME_V->value; 3815 SCHEME_V->args = SCHEME_V->value;
3337 s_goto (OP_P0LIST); 3816 s_goto (OP_P0LIST);
3338 } 3817 }
3339 else 3818
3340 s_return (SCHEME_V->value); 3819 s_return (SCHEME_V->value);
3341 3820
3342 case OP_EVAL: /* main part of evaluation */ 3821 case OP_EVAL: /* main part of evaluation */
3343#if USE_TRACING 3822#if USE_TRACING
3344 if (SCHEME_V->tracing) 3823 if (SCHEME_V->tracing)
3345 { 3824 {
3346 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ 3825 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */
3347 s_save (SCHEME_A_ OP_REAL_EVAL, SCHEME_V->args, SCHEME_V->code); 3826 s_save (SCHEME_A_ OP_REAL_EVAL, args, SCHEME_V->code);
3348 SCHEME_V->args = SCHEME_V->code; 3827 SCHEME_V->args = SCHEME_V->code;
3349 putstr (SCHEME_A_ "\nEval: "); 3828 putstr (SCHEME_A_ "\nEval: ");
3350 s_goto (OP_P0LIST); 3829 s_goto (OP_P0LIST);
3351 } 3830 }
3352 3831
3356#endif 3835#endif
3357 if (is_symbol (SCHEME_V->code)) /* symbol */ 3836 if (is_symbol (SCHEME_V->code)) /* symbol */
3358 { 3837 {
3359 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);
3360 3839
3361 if (x != NIL) 3840 if (x == NIL)
3362 s_return (slot_value_in_env (x));
3363 else
3364 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));
3365 } 3844 }
3366 else if (is_pair (SCHEME_V->code)) 3845 else if (is_pair (SCHEME_V->code))
3367 { 3846 {
3368 x = car (SCHEME_V->code); 3847 x = car (SCHEME_V->code);
3369 3848
3378 /* 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)); */
3379 SCHEME_V->code = x; 3858 SCHEME_V->code = x;
3380 s_goto (OP_EVAL); 3859 s_goto (OP_EVAL);
3381 } 3860 }
3382 } 3861 }
3383 else 3862
3384 s_return (SCHEME_V->code); 3863 s_return (SCHEME_V->code);
3385 3864
3386 case OP_E0ARGS: /* eval arguments */ 3865 case OP_E0ARGS: /* eval arguments */
3387 if (is_macro (SCHEME_V->value)) /* macro expansion */ 3866 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3388 { 3867 {
3389 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3868 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3390 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3869 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3391 SCHEME_V->code = SCHEME_V->value; 3870 SCHEME_V->code = SCHEME_V->value;
3392 s_goto (OP_APPLY); 3871 s_goto (OP_APPLY);
3393 } 3872 }
3394 else 3873
3395 {
3396 SCHEME_V->code = cdr (SCHEME_V->code); 3874 SCHEME_V->code = cdr (SCHEME_V->code);
3397 s_goto (OP_E1ARGS); 3875 s_goto (OP_E1ARGS);
3398 }
3399 3876
3400 case OP_E1ARGS: /* eval arguments */ 3877 case OP_E1ARGS: /* eval arguments */
3401 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3878 args = cons (SCHEME_V->value, args);
3402 3879
3403 if (is_pair (SCHEME_V->code)) /* continue */ 3880 if (is_pair (SCHEME_V->code)) /* continue */
3404 { 3881 {
3405 s_save (SCHEME_A_ OP_E1ARGS, SCHEME_V->args, cdr (SCHEME_V->code)); 3882 s_save (SCHEME_A_ OP_E1ARGS, args, cdr (SCHEME_V->code));
3406 SCHEME_V->code = car (SCHEME_V->code); 3883 SCHEME_V->code = car (SCHEME_V->code);
3407 SCHEME_V->args = NIL; 3884 SCHEME_V->args = NIL;
3408 s_goto (OP_EVAL); 3885 s_goto (OP_EVAL);
3409 } 3886 }
3410 else /* end */ 3887 else /* end */
3411 { 3888 {
3412 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3889 args = reverse_in_place (SCHEME_A_ NIL, args);
3413 SCHEME_V->code = car (SCHEME_V->args); 3890 SCHEME_V->code = car (args);
3414 SCHEME_V->args = cdr (SCHEME_V->args); 3891 SCHEME_V->args = cdr (args);
3415 s_goto (OP_APPLY); 3892 s_goto (OP_APPLY);
3416 } 3893 }
3417 3894
3418#if USE_TRACING 3895#if USE_TRACING
3419
3420 case OP_TRACING: 3896 case OP_TRACING:
3421 { 3897 {
3422 int tr = SCHEME_V->tracing; 3898 int tr = SCHEME_V->tracing;
3423 3899
3424 SCHEME_V->tracing = ivalue (car (SCHEME_V->args)); 3900 SCHEME_V->tracing = ivalue_unchecked (car (args));
3425 s_return (mk_integer (SCHEME_A_ tr)); 3901 s_return (mk_integer (SCHEME_A_ tr));
3426 } 3902 }
3427
3428#endif 3903#endif
3429 3904
3430 case OP_APPLY: /* apply 'code' to 'args' */ 3905 case OP_APPLY: /* apply 'code' to 'args' */
3431#if USE_TRACING 3906#if USE_TRACING
3432 if (SCHEME_V->tracing) 3907 if (SCHEME_V->tracing)
3433 { 3908 {
3434 s_save (SCHEME_A_ OP_REAL_APPLY, SCHEME_V->args, SCHEME_V->code); 3909 s_save (SCHEME_A_ OP_REAL_APPLY, args, SCHEME_V->code);
3435 SCHEME_V->print_flag = 1; 3910 SCHEME_V->print_flag = 1;
3436 /* SCHEME_V->args=cons(SCHEME_V->code,SCHEME_V->args); */ 3911 /* args=cons(SCHEME_V->code,args); */
3437 putstr (SCHEME_A_ "\nApply to: "); 3912 putstr (SCHEME_A_ "\nApply to: ");
3438 s_goto (OP_P0LIST); 3913 s_goto (OP_P0LIST);
3439 } 3914 }
3440 3915
3441 /* fall through */ 3916 /* fall through */
3442 3917
3443 case OP_REAL_APPLY: 3918 case OP_REAL_APPLY:
3444#endif 3919#endif
3445 if (is_proc (SCHEME_V->code)) 3920 if (is_proc (SCHEME_V->code))
3446 {
3447 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3921 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3448 }
3449 else if (is_foreign (SCHEME_V->code)) 3922 else if (is_foreign (SCHEME_V->code))
3450 { 3923 {
3451 /* Keep nested calls from GC'ing the arglist */ 3924 /* Keep nested calls from GC'ing the arglist */
3452 push_recent_alloc (SCHEME_A_ SCHEME_V->args, NIL); 3925 push_recent_alloc (SCHEME_A_ args, NIL);
3453 x = SCHEME_V->code->object.ff (SCHEME_A_ SCHEME_V->args); 3926 x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args);
3454 3927
3455 s_return (x); 3928 s_return (x);
3456 } 3929 }
3457 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 */
3458 { 3931 {
3459 /* Should not accept promise */ 3932 /* Should not accept promise */
3460 /* make environment */ 3933 /* make environment */
3461 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); 3934 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code));
3462 3935
3463 for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) 3936 for (x = car (closure_code (SCHEME_V->code)), y = args; is_pair (x); x = cdr (x), y = cdr (y))
3464 { 3937 {
3465 if (y == NIL) 3938 if (y == NIL)
3466 Error_0 ("not enough arguments"); 3939 Error_0 ("not enough arguments");
3467 else 3940 else
3468 new_slot_in_env (SCHEME_A_ car (x), car (y)); 3941 new_slot_in_env (SCHEME_A_ car (x), car (y));
3486 s_goto (OP_BEGIN); 3959 s_goto (OP_BEGIN);
3487 } 3960 }
3488 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ 3961 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */
3489 { 3962 {
3490 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); 3963 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code));
3491 s_return (SCHEME_V->args != NIL ? car (SCHEME_V->args) : NIL); 3964 s_return (args != NIL ? car (args) : NIL);
3492 } 3965 }
3493 else 3966
3494 Error_0 ("illegal function"); 3967 Error_0 ("illegal function");
3495 3968
3496 case OP_DOMACRO: /* do macro */ 3969 case OP_DOMACRO: /* do macro */
3497 SCHEME_V->code = SCHEME_V->value; 3970 SCHEME_V->code = SCHEME_V->value;
3498 s_goto (OP_EVAL); 3971 s_goto (OP_EVAL);
3499
3500#if 1
3501 3972
3502 case OP_LAMBDA: /* lambda */ 3973 case OP_LAMBDA: /* lambda */
3503 /* 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
3504 set SCHEME_V->value fall thru */ 3975 set SCHEME_V->value fall thru */
3505 { 3976 {
3506 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); 3977 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1);
3507 3978
3508 if (f != NIL) 3979 if (f != NIL)
3509 { 3980 {
3510 s_save (SCHEME_A_ OP_LAMBDA1, SCHEME_V->args, SCHEME_V->code); 3981 s_save (SCHEME_A_ OP_LAMBDA1, args, SCHEME_V->code);
3511 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3982 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3512 SCHEME_V->code = slot_value_in_env (f); 3983 SCHEME_V->code = slot_value_in_env (f);
3513 s_goto (OP_APPLY); 3984 s_goto (OP_APPLY);
3514 } 3985 }
3515 3986
3516 SCHEME_V->value = SCHEME_V->code; 3987 SCHEME_V->value = SCHEME_V->code;
3517 /* Fallthru */
3518 } 3988 }
3989 /* Fallthru */
3519 3990
3520 case OP_LAMBDA1: 3991 case OP_LAMBDA1:
3521 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));
3522 3993
3523#else
3524
3525 case OP_LAMBDA: /* lambda */
3526 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir));
3527
3528#endif
3529
3530 case OP_MKCLOSURE: /* make-closure */ 3994 case OP_MKCLOSURE: /* make-closure */
3531 x = car (SCHEME_V->args); 3995 x = car (args);
3532 3996
3533 if (car (x) == SCHEME_V->LAMBDA) 3997 if (car (x) == SCHEME_V->LAMBDA)
3534 x = cdr (x); 3998 x = cdr (x);
3535 3999
3536 if (cdr (SCHEME_V->args) == NIL) 4000 if (cdr (args) == NIL)
3537 y = SCHEME_V->envir; 4001 y = SCHEME_V->envir;
3538 else 4002 else
3539 y = cadr (SCHEME_V->args); 4003 y = cadr (args);
3540 4004
3541 s_return (mk_closure (SCHEME_A_ x, y)); 4005 s_return (mk_closure (SCHEME_A_ x, y));
3542 4006
3543 case OP_QUOTE: /* quote */ 4007 case OP_QUOTE: /* quote */
3544 s_return (car (SCHEME_V->code)); 4008 s_return (car (SCHEME_V->code));
3572 else 4036 else
3573 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);
3574 4038
3575 s_return (SCHEME_V->code); 4039 s_return (SCHEME_V->code);
3576 4040
3577
3578 case OP_DEFP: /* defined? */ 4041 case OP_DEFP: /* defined? */
3579 x = SCHEME_V->envir; 4042 x = SCHEME_V->envir;
3580 4043
3581 if (cdr (SCHEME_V->args) != NIL) 4044 if (cdr (args) != NIL)
3582 x = cadr (SCHEME_V->args); 4045 x = cadr (args);
3583 4046
3584 s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); 4047 s_retbool (find_slot_in_env (SCHEME_A_ x, car (args), 1) != NIL);
3585 4048
3586 case OP_SET0: /* set! */ 4049 case OP_SET0: /* set! */
3587 if (is_immutable (car (SCHEME_V->code))) 4050 if (is_immutable (car (SCHEME_V->code)))
3588 Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code)); 4051 Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code));
3589 4052
3600 s_return (SCHEME_V->value); 4063 s_return (SCHEME_V->value);
3601 } 4064 }
3602 else 4065 else
3603 Error_1 ("set!: unbound variable:", SCHEME_V->code); 4066 Error_1 ("set!: unbound variable:", SCHEME_V->code);
3604 4067
3605
3606 case OP_BEGIN: /* begin */ 4068 case OP_BEGIN: /* begin */
3607 if (!is_pair (SCHEME_V->code)) 4069 if (!is_pair (SCHEME_V->code))
3608 s_return (SCHEME_V->code); 4070 s_return (SCHEME_V->code);
3609 4071
3610 if (cdr (SCHEME_V->code) != NIL) 4072 if (cdr (SCHEME_V->code) != NIL)
3620 4082
3621 case OP_IF1: /* if */ 4083 case OP_IF1: /* if */
3622 if (is_true (SCHEME_V->value)) 4084 if (is_true (SCHEME_V->value))
3623 SCHEME_V->code = car (SCHEME_V->code); 4085 SCHEME_V->code = car (SCHEME_V->code);
3624 else 4086 else
3625 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because 4087 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
3626 4088
3627 * car(NIL) = NIL */
3628 s_goto (OP_EVAL); 4089 s_goto (OP_EVAL);
3629 4090
3630 case OP_LET0: /* let */ 4091 case OP_LET0: /* let */
3631 SCHEME_V->args = NIL; 4092 SCHEME_V->args = NIL;
3632 SCHEME_V->value = SCHEME_V->code; 4093 SCHEME_V->value = SCHEME_V->code;
3633 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);
3634 s_goto (OP_LET1); 4095 s_goto (OP_LET1);
3635 4096
3636 case OP_LET1: /* let (calculate parameters) */ 4097 case OP_LET1: /* let (calculate parameters) */
4098 case OP_LET1REC: /* letrec (calculate parameters) */
3637 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 4099 args = cons (SCHEME_V->value, args);
3638 4100
3639 if (is_pair (SCHEME_V->code)) /* continue */ 4101 if (is_pair (SCHEME_V->code)) /* continue */
3640 { 4102 {
3641 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)))
3642 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));
3643 4105
3644 s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); 4106 s_save (SCHEME_A_ op, args, cdr (SCHEME_V->code));
3645 SCHEME_V->code = cadar (SCHEME_V->code); 4107 SCHEME_V->code = cadar (SCHEME_V->code);
3646 SCHEME_V->args = NIL; 4108 SCHEME_V->args = NIL;
3647 s_goto (OP_EVAL); 4109 s_goto (OP_EVAL);
3648 } 4110 }
3649 else /* end */ 4111
3650 { 4112 /* end */
3651 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 4113 args = reverse_in_place (SCHEME_A_ NIL, args);
3652 SCHEME_V->code = car (SCHEME_V->args); 4114 SCHEME_V->code = car (args);
3653 SCHEME_V->args = cdr (SCHEME_V->args); 4115 SCHEME_V->args = cdr (args);
3654 s_goto (OP_LET2); 4116 s_goto (op == OP_LET1 ? OP_LET2 : OP_LET2REC);
3655 }
3656 4117
3657 case OP_LET2: /* let */ 4118 case OP_LET2: /* let */
3658 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4119 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3659 4120
3660 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; 4121 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args;
3661 y != NIL; x = cdr (x), y = cdr (y)) 4122 y != NIL; x = cdr (x), y = cdr (y))
3662 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 4123 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3663 4124
3664 if (is_symbol (car (SCHEME_V->code))) /* named let */ 4125 if (is_symbol (car (SCHEME_V->code))) /* named let */
3665 { 4126 {
3666 for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) 4127 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x))
3667 { 4128 {
3668 if (!is_pair (x)) 4129 if (!is_pair (x))
3669 Error_1 ("Bad syntax of binding in let :", x); 4130 Error_1 ("Bad syntax of binding in let:", x);
3670 4131
3671 if (!is_list (SCHEME_A_ car (x))) 4132 if (!is_list (SCHEME_A_ car (x)))
3672 Error_1 ("Bad syntax of binding in let :", car (x)); 4133 Error_1 ("Bad syntax of binding in let:", car (x));
3673 4134
3674 SCHEME_V->args = cons (caar (x), SCHEME_V->args); 4135 args = cons (caar (x), args);
3675 } 4136 }
3676 4137
3677 x =
3678 mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args), cddr (SCHEME_V->code)), 4138 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)),
3679 SCHEME_V->envir); 4139 SCHEME_V->envir);
3680 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); 4140 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x);
3681 SCHEME_V->code = cddr (SCHEME_V->code); 4141 SCHEME_V->code = cddr (SCHEME_V->code);
3682 SCHEME_V->args = NIL;
3683 } 4142 }
3684 else 4143 else
3685 { 4144 {
3686 SCHEME_V->code = cdr (SCHEME_V->code); 4145 SCHEME_V->code = cdr (SCHEME_V->code);
3687 SCHEME_V->args = NIL;
3688 } 4146 }
3689 4147
4148 SCHEME_V->args = NIL;
3690 s_goto (OP_BEGIN); 4149 s_goto (OP_BEGIN);
3691 4150
3692 case OP_LET0AST: /* let* */ 4151 case OP_LET0AST: /* let* */
3693 if (car (SCHEME_V->code) == NIL) 4152 if (car (SCHEME_V->code) == NIL)
3694 { 4153 {
3696 SCHEME_V->code = cdr (SCHEME_V->code); 4155 SCHEME_V->code = cdr (SCHEME_V->code);
3697 s_goto (OP_BEGIN); 4156 s_goto (OP_BEGIN);
3698 } 4157 }
3699 4158
3700 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)))
3701 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));
3702 4161
3703 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));
3704 SCHEME_V->code = car (cdaar (SCHEME_V->code)); 4163 SCHEME_V->code = car (cdaar (SCHEME_V->code));
3705 s_goto (OP_EVAL); 4164 s_goto (OP_EVAL);
3706 4165
3712 new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value); 4171 new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value);
3713 SCHEME_V->code = cdr (SCHEME_V->code); 4172 SCHEME_V->code = cdr (SCHEME_V->code);
3714 4173
3715 if (is_pair (SCHEME_V->code)) /* continue */ 4174 if (is_pair (SCHEME_V->code)) /* continue */
3716 { 4175 {
3717 s_save (SCHEME_A_ OP_LET2AST, SCHEME_V->args, SCHEME_V->code); 4176 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code);
3718 SCHEME_V->code = cadar (SCHEME_V->code); 4177 SCHEME_V->code = cadar (SCHEME_V->code);
3719 SCHEME_V->args = NIL; 4178 SCHEME_V->args = NIL;
3720 s_goto (OP_EVAL); 4179 s_goto (OP_EVAL);
3721 } 4180 }
3722 else /* end */ 4181
4182 /* end */
3723 { 4183
3724 SCHEME_V->code = SCHEME_V->args; 4184 SCHEME_V->code = args;
3725 SCHEME_V->args = NIL; 4185 SCHEME_V->args = NIL;
3726 s_goto (OP_BEGIN); 4186 s_goto (OP_BEGIN);
3727 }
3728 4187
3729 case OP_LET0REC: /* letrec */ 4188 case OP_LET0REC: /* letrec */
3730 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4189 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3731 SCHEME_V->args = NIL; 4190 SCHEME_V->args = NIL;
3732 SCHEME_V->value = SCHEME_V->code; 4191 SCHEME_V->value = SCHEME_V->code;
3733 SCHEME_V->code = car (SCHEME_V->code); 4192 SCHEME_V->code = car (SCHEME_V->code);
3734 s_goto (OP_LET1REC); 4193 s_goto (OP_LET1REC);
3735 4194
3736 case OP_LET1REC: /* letrec (calculate parameters) */ 4195 /* OP_LET1REC handled by OP_LET1 */
3737 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args);
3738
3739 if (is_pair (SCHEME_V->code)) /* continue */
3740 {
3741 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3742 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3743
3744 s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code));
3745 SCHEME_V->code = cadar (SCHEME_V->code);
3746 SCHEME_V->args = NIL;
3747 s_goto (OP_EVAL);
3748 }
3749 else /* end */
3750 {
3751 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args);
3752 SCHEME_V->code = car (SCHEME_V->args);
3753 SCHEME_V->args = cdr (SCHEME_V->args);
3754 s_goto (OP_LET2REC);
3755 }
3756 4196
3757 case OP_LET2REC: /* letrec */ 4197 case OP_LET2REC: /* letrec */
3758 for (x = car (SCHEME_V->code), y = SCHEME_V->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))
3759 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 4199 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3760 4200
3761 SCHEME_V->code = cdr (SCHEME_V->code); 4201 SCHEME_V->code = cdr (SCHEME_V->code);
3762 SCHEME_V->args = NIL; 4202 SCHEME_V->args = NIL;
3763 s_goto (OP_BEGIN); 4203 s_goto (OP_BEGIN);
3790 } 4230 }
3791 else 4231 else
3792 { 4232 {
3793 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) 4233 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL)
3794 s_return (NIL); 4234 s_return (NIL);
3795 else 4235
3796 {
3797 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 4236 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
3798 SCHEME_V->code = caar (SCHEME_V->code); 4237 SCHEME_V->code = caar (SCHEME_V->code);
3799 s_goto (OP_EVAL); 4238 s_goto (OP_EVAL);
3800 }
3801 } 4239 }
3802 4240
3803 case OP_DELAY: /* delay */ 4241 case OP_DELAY: /* delay */
3804 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);
3805 set_typeflag (x, T_PROMISE); 4243 set_typeflag (x, T_PROMISE);
3816 case OP_AND1: /* and */ 4254 case OP_AND1: /* and */
3817 if (is_false (SCHEME_V->value)) 4255 if (is_false (SCHEME_V->value))
3818 s_return (SCHEME_V->value); 4256 s_return (SCHEME_V->value);
3819 else if (SCHEME_V->code == NIL) 4257 else if (SCHEME_V->code == NIL)
3820 s_return (SCHEME_V->value); 4258 s_return (SCHEME_V->value);
3821 else 4259
3822 {
3823 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 4260 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
3824 SCHEME_V->code = car (SCHEME_V->code); 4261 SCHEME_V->code = car (SCHEME_V->code);
3825 s_goto (OP_EVAL); 4262 s_goto (OP_EVAL);
3826 }
3827 4263
3828 case OP_OR0: /* or */ 4264 case OP_OR0: /* or */
3829 if (SCHEME_V->code == NIL) 4265 if (SCHEME_V->code == NIL)
3830 s_return (S_F); 4266 s_return (S_F);
3831 4267
3836 case OP_OR1: /* or */ 4272 case OP_OR1: /* or */
3837 if (is_true (SCHEME_V->value)) 4273 if (is_true (SCHEME_V->value))
3838 s_return (SCHEME_V->value); 4274 s_return (SCHEME_V->value);
3839 else if (SCHEME_V->code == NIL) 4275 else if (SCHEME_V->code == NIL)
3840 s_return (SCHEME_V->value); 4276 s_return (SCHEME_V->value);
3841 else 4277
3842 {
3843 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 4278 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
3844 SCHEME_V->code = car (SCHEME_V->code); 4279 SCHEME_V->code = car (SCHEME_V->code);
3845 s_goto (OP_EVAL); 4280 s_goto (OP_EVAL);
3846 }
3847 4281
3848 case OP_C0STREAM: /* cons-stream */ 4282 case OP_C0STREAM: /* cons-stream */
3849 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 4283 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3850 SCHEME_V->code = car (SCHEME_V->code); 4284 SCHEME_V->code = car (SCHEME_V->code);
3851 s_goto (OP_EVAL); 4285 s_goto (OP_EVAL);
3852 4286
3853 case OP_C1STREAM: /* cons-stream */ 4287 case OP_C1STREAM: /* cons-stream */
3854 SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register SCHEME_V->args for gc */ 4288 SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register args for gc */
3855 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); 4289 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir);
3856 set_typeflag (x, T_PROMISE); 4290 set_typeflag (x, T_PROMISE);
3857 s_return (cons (SCHEME_V->args, x)); 4291 s_return (cons (args, x));
3858 4292
3859 case OP_MACRO0: /* macro */ 4293 case OP_MACRO0: /* macro */
3860 if (is_pair (car (SCHEME_V->code))) 4294 if (is_pair (car (SCHEME_V->code)))
3861 { 4295 {
3862 x = caar (SCHEME_V->code); 4296 x = caar (SCHEME_V->code);
3895 { 4329 {
3896 if (!is_pair (y = caar (x))) 4330 if (!is_pair (y = caar (x)))
3897 break; 4331 break;
3898 4332
3899 for (; y != NIL; y = cdr (y)) 4333 for (; y != NIL; y = cdr (y))
3900 {
3901 if (eqv (car (y), SCHEME_V->value)) 4334 if (eqv (car (y), SCHEME_V->value))
3902 break; 4335 break;
3903 }
3904 4336
3905 if (y != NIL) 4337 if (y != NIL)
3906 break; 4338 break;
3907 } 4339 }
3908 4340
3918 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); 4350 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x));
3919 SCHEME_V->code = caar (x); 4351 SCHEME_V->code = caar (x);
3920 s_goto (OP_EVAL); 4352 s_goto (OP_EVAL);
3921 } 4353 }
3922 } 4354 }
3923 else 4355
3924 s_return (NIL); 4356 s_return (NIL);
3925 4357
3926 case OP_CASE2: /* case */ 4358 case OP_CASE2: /* case */
3927 if (is_true (SCHEME_V->value)) 4359 if (is_true (SCHEME_V->value))
3928 s_goto (OP_BEGIN); 4360 s_goto (OP_BEGIN);
3929 else 4361
3930 s_return (NIL); 4362 s_return (NIL);
3931 4363
3932 case OP_PAPPLY: /* apply */ 4364 case OP_PAPPLY: /* apply */
3933 SCHEME_V->code = car (SCHEME_V->args); 4365 SCHEME_V->code = car (args);
3934 SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); 4366 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
3935 /*SCHEME_V->args = cadr(SCHEME_V->args); */ 4367 /*SCHEME_V->args = cadr(args); */
3936 s_goto (OP_APPLY); 4368 s_goto (OP_APPLY);
3937 4369
3938 case OP_PEVAL: /* eval */ 4370 case OP_PEVAL: /* eval */
3939 if (cdr (SCHEME_V->args) != NIL) 4371 if (cdr (args) != NIL)
3940 SCHEME_V->envir = cadr (SCHEME_V->args); 4372 SCHEME_V->envir = cadr (args);
3941 4373
3942 SCHEME_V->code = car (SCHEME_V->args); 4374 SCHEME_V->code = car (args);
3943 s_goto (OP_EVAL); 4375 s_goto (OP_EVAL);
3944 4376
3945 case OP_CONTINUATION: /* call-with-current-continuation */ 4377 case OP_CONTINUATION: /* call-with-current-continuation */
3946 SCHEME_V->code = car (SCHEME_V->args); 4378 SCHEME_V->code = car (args);
3947 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);
3948 s_goto (OP_APPLY); 4380 s_goto (OP_APPLY);
3949 } 4381 }
3950 4382
3951 return S_T; 4383 if (USE_ERROR_CHECKING) abort ();
3952} 4384}
3953 4385
3954static pointer 4386/* math, cxr */
4387ecb_hot static int
3955opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4388opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3956{ 4389{
3957 pointer x; 4390 pointer args = SCHEME_V->args;
4391 pointer x = car (args);
3958 num v; 4392 num v;
3959 4393
4394 switch (op)
4395 {
3960#if USE_MATH 4396#if USE_MATH
3961 RVALUE dd;
3962#endif
3963
3964 switch (op)
3965 {
3966#if USE_MATH
3967
3968 case OP_INEX2EX: /* inexact->exact */ 4397 case OP_INEX2EX: /* inexact->exact */
3969 x = car (SCHEME_V->args);
3970
3971 if (num_is_integer (x)) 4398 if (!is_integer (x))
3972 s_return (x); 4399 {
3973 else if (modf (rvalue_unchecked (x), &dd) == 0.0) 4400 RVALUE r = rvalue_unchecked (x);
3974 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4401
4402 if (r == (RVALUE)(IVALUE)r)
4403 x = mk_integer (SCHEME_A_ rvalue_unchecked (x));
3975 else 4404 else
3976 Error_1 ("inexact->exact: not integral:", x); 4405 Error_1 ("inexact->exact: not integral:", x);
4406 }
3977 4407
3978 case OP_EXP: 4408 s_return (x);
3979 x = car (SCHEME_V->args); 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))));
3980 s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4416 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
3981
3982 case OP_LOG:
3983 x = car (SCHEME_V->args);
3984 s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 4417 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))
3985 4418 / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args))))));
3986 case OP_SIN:
3987 x = car (SCHEME_V->args);
3988 s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 4419 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
3989
3990 case OP_COS:
3991 x = car (SCHEME_V->args);
3992 s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 4420 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3993
3994 case OP_TAN:
3995 x = car (SCHEME_V->args);
3996 s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); 4421 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x))));
3997
3998 case OP_ASIN:
3999 x = car (SCHEME_V->args);
4000 s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); 4422 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x))));
4001
4002 case OP_ACOS:
4003 x = car (SCHEME_V->args);
4004 s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); 4423 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x))));
4005 4424
4006 case OP_ATAN: 4425 case OP_ATAN:
4007 x = car (SCHEME_V->args);
4008
4009 if (cdr (SCHEME_V->args) == NIL)
4010 s_return (mk_real (SCHEME_A_ atan (rvalue (x))));
4011 else
4012 {
4013 pointer y = cadr (SCHEME_V->args);
4014
4015 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
4016 }
4017
4018 case OP_SQRT:
4019 x = car (SCHEME_V->args);
4020 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); 4426 s_return (mk_real (SCHEME_A_
4427 cdr (args) == NIL
4428 ? atan (rvalue (x))
4429 : atan2 (rvalue (x), rvalue (cadr (args)))));
4021 4430
4022 case OP_EXPT: 4431 case OP_EXPT:
4023 { 4432 {
4024 RVALUE result; 4433 RVALUE result;
4025 int real_result = 1; 4434 int real_result = 1;
4026 pointer y = cadr (SCHEME_V->args); 4435 pointer y = cadr (args);
4027 4436
4028 x = car (SCHEME_V->args);
4029
4030 if (num_is_integer (x) && num_is_integer (y)) 4437 if (is_integer (x) && is_integer (y))
4031 real_result = 0; 4438 real_result = 0;
4032 4439
4033 /* This 'if' is an R5RS compatibility fix. */ 4440 /* This 'if' is an R5RS compatibility fix. */
4034 /* NOTE: Remove this 'if' fix for R6RS. */ 4441 /* NOTE: Remove this 'if' fix for R6RS. */
4035 if (rvalue (x) == 0 && rvalue (y) < 0) 4442 if (rvalue (x) == 0 && rvalue (y) < 0)
4036 result = 0.0; 4443 result = 0;
4037 else 4444 else
4038 result = pow (rvalue (x), rvalue (y)); 4445 result = pow (rvalue (x), rvalue (y));
4039 4446
4040 /* Before returning integer result make sure we can. */ 4447 /* Before returning integer result make sure we can. */
4041 /* If the test fails, result is too big for integer. */ 4448 /* If the test fails, result is too big for integer. */
4042 if (!real_result) 4449 if (!real_result)
4043 { 4450 {
4044 long result_as_long = (long) result; 4451 long result_as_long = result;
4045 4452
4046 if (result != (RVALUE) result_as_long) 4453 if (result != result_as_long)
4047 real_result = 1; 4454 real_result = 1;
4048 } 4455 }
4049 4456
4050 if (real_result) 4457 if (real_result)
4051 s_return (mk_real (SCHEME_A_ result)); 4458 s_return (mk_real (SCHEME_A_ result));
4052 else 4459 else
4053 s_return (mk_integer (SCHEME_A_ result)); 4460 s_return (mk_integer (SCHEME_A_ result));
4054 } 4461 }
4055
4056 case OP_FLOOR:
4057 x = car (SCHEME_V->args);
4058 s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4059
4060 case OP_CEILING:
4061 x = car (SCHEME_V->args);
4062 s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4063
4064 case OP_TRUNCATE:
4065 {
4066 RVALUE rvalue_of_x;
4067
4068 x = car (SCHEME_V->args);
4069 rvalue_of_x = rvalue (x);
4070
4071 if (rvalue_of_x > 0)
4072 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x)));
4073 else
4074 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
4075 }
4076
4077 case OP_ROUND:
4078 x = car (SCHEME_V->args);
4079
4080 if (num_is_integer (x))
4081 s_return (x);
4082
4083 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4084#endif 4462#endif
4085 4463
4086 case OP_ADD: /* + */ 4464 case OP_ADD: /* + */
4087 v = num_zero; 4465 v = num_zero;
4088 4466
4089 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4467 for (x = args; x != NIL; x = cdr (x))
4090 v = num_op ('+', v, nvalue (car (x))); 4468 v = num_op (NUM_ADD, v, nvalue (car (x)));
4091 4469
4092 s_return (mk_number (SCHEME_A_ v)); 4470 s_return (mk_number (SCHEME_A_ v));
4093 4471
4094 case OP_MUL: /* * */ 4472 case OP_MUL: /* * */
4095 v = num_one; 4473 v = num_one;
4096 4474
4097 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4475 for (x = args; x != NIL; x = cdr (x))
4098 v = num_op ('+', v, nvalue (car (x))); 4476 v = num_op (NUM_MUL, v, nvalue (car (x)));
4099 4477
4100 s_return (mk_number (SCHEME_A_ v)); 4478 s_return (mk_number (SCHEME_A_ v));
4101 4479
4102 case OP_SUB: /* - */ 4480 case OP_SUB: /* - */
4103 if (cdr (SCHEME_V->args) == NIL) 4481 if (cdr (args) == NIL)
4104 { 4482 {
4105 x = SCHEME_V->args; 4483 x = args;
4106 v = num_zero; 4484 v = num_zero;
4107 } 4485 }
4108 else 4486 else
4109 { 4487 {
4110 x = cdr (SCHEME_V->args); 4488 x = cdr (args);
4111 v = nvalue (car (SCHEME_V->args)); 4489 v = nvalue (car (args));
4112 } 4490 }
4113 4491
4114 for (; x != NIL; x = cdr (x)) 4492 for (; x != NIL; x = cdr (x))
4115 v = num_op ('+', v, nvalue (car (x))); 4493 v = num_op (NUM_SUB, v, nvalue (car (x)));
4116 4494
4117 s_return (mk_number (SCHEME_A_ v)); 4495 s_return (mk_number (SCHEME_A_ v));
4118 4496
4119 case OP_DIV: /* / */ 4497 case OP_DIV: /* / */
4120 if (cdr (SCHEME_V->args) == NIL) 4498 if (cdr (args) == NIL)
4121 { 4499 {
4122 x = SCHEME_V->args; 4500 x = args;
4123 v = num_one; 4501 v = num_one;
4124 } 4502 }
4125 else 4503 else
4126 { 4504 {
4127 x = cdr (SCHEME_V->args); 4505 x = cdr (args);
4128 v = nvalue (car (SCHEME_V->args)); 4506 v = nvalue (car (args));
4129 } 4507 }
4130 4508
4131 for (; x != NIL; x = cdr (x)) 4509 for (; x != NIL; x = cdr (x))
4510 if (!is_zero_rvalue (rvalue (car (x))))
4511 v = num_div (v, nvalue (car (x)));
4512 else
4513 Error_0 ("/: division by zero");
4514
4515 s_return (mk_number (SCHEME_A_ v));
4516
4517 case OP_INTDIV: /* quotient */
4518 if (cdr (args) == NIL)
4132 { 4519 {
4133 if (!is_zero_rvalue (rvalue (car (x))))
4134 v = num_div (v, nvalue (car (x)));
4135 else
4136 Error_0 ("/: division by zero");
4137 }
4138
4139 s_return (mk_number (SCHEME_A_ v));
4140
4141 case OP_INTDIV: /* quotient */
4142 if (cdr (SCHEME_V->args) == NIL)
4143 {
4144 x = SCHEME_V->args; 4520 x = args;
4145 v = num_one; 4521 v = num_one;
4146 } 4522 }
4147 else 4523 else
4148 { 4524 {
4149 x = cdr (SCHEME_V->args); 4525 x = cdr (args);
4150 v = nvalue (car (SCHEME_V->args)); 4526 v = nvalue (car (args));
4151 } 4527 }
4152 4528
4153 for (; x != NIL; x = cdr (x)) 4529 for (; x != NIL; x = cdr (x))
4154 { 4530 {
4155 if (ivalue (car (x)) != 0) 4531 if (ivalue (car (x)) != 0)
4156 v = num_op ('/', v, nvalue (car (x))); 4532 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4157 else 4533 else
4158 Error_0 ("quotient: division by zero"); 4534 Error_0 ("quotient: division by zero");
4159 } 4535 }
4160 4536
4161 s_return (mk_number (SCHEME_A_ v)); 4537 s_return (mk_number (SCHEME_A_ v));
4162 4538
4163 case OP_REM: /* remainder */ 4539 case OP_REM: /* remainder */
4164 v = nvalue (car (SCHEME_V->args)); 4540 v = nvalue (x);
4165 4541
4166 if (ivalue (cadr (SCHEME_V->args)) != 0) 4542 if (ivalue (cadr (args)) != 0)
4167 v = num_rem (v, nvalue (cadr (SCHEME_V->args))); 4543 v = num_rem (v, nvalue (cadr (args)));
4168 else 4544 else
4169 Error_0 ("remainder: division by zero"); 4545 Error_0 ("remainder: division by zero");
4170 4546
4171 s_return (mk_number (SCHEME_A_ v)); 4547 s_return (mk_number (SCHEME_A_ v));
4172 4548
4173 case OP_MOD: /* modulo */ 4549 case OP_MOD: /* modulo */
4174 v = nvalue (car (SCHEME_V->args)); 4550 v = nvalue (x);
4175 4551
4176 if (ivalue (cadr (SCHEME_V->args)) != 0) 4552 if (ivalue (cadr (args)) != 0)
4177 v = num_mod (v, nvalue (cadr (SCHEME_V->args))); 4553 v = num_mod (v, nvalue (cadr (args)));
4178 else 4554 else
4179 Error_0 ("modulo: division by zero"); 4555 Error_0 ("modulo: division by zero");
4180 4556
4181 s_return (mk_number (SCHEME_A_ v)); 4557 s_return (mk_number (SCHEME_A_ v));
4182 4558
4183 case OP_CAR: /* car */ 4559 /* the compiler will optimize this mess... */
4184 s_return (caar (SCHEME_V->args)); 4560 case OP_CAR: op_car: s_return (car (x));
4185 4561 case OP_CDR: op_cdr: s_return (cdr (x));
4186 case OP_CDR: /* cdr */ 4562 case OP_CAAR: op_caar: x = car (x); goto op_car;
4187 s_return (cdar (SCHEME_V->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;
4188 4590
4189 case OP_CONS: /* cons */ 4591 case OP_CONS: /* cons */
4190 set_cdr (SCHEME_V->args, cadr (SCHEME_V->args)); 4592 set_cdr (args, cadr (args));
4191 s_return (SCHEME_V->args); 4593 s_return (args);
4192 4594
4193 case OP_SETCAR: /* set-car! */ 4595 case OP_SETCAR: /* set-car! */
4194 if (!is_immutable (car (SCHEME_V->args))) 4596 if (!is_immutable (x))
4195 { 4597 {
4196 set_car (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4598 set_car (x, cadr (args));
4197 s_return (car (SCHEME_V->args)); 4599 s_return (car (args));
4198 } 4600 }
4199 else 4601 else
4200 Error_0 ("set-car!: unable to alter immutable pair"); 4602 Error_0 ("set-car!: unable to alter immutable pair");
4201 4603
4202 case OP_SETCDR: /* set-cdr! */ 4604 case OP_SETCDR: /* set-cdr! */
4203 if (!is_immutable (car (SCHEME_V->args))) 4605 if (!is_immutable (x))
4204 { 4606 {
4205 set_cdr (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4607 set_cdr (x, cadr (args));
4206 s_return (car (SCHEME_V->args)); 4608 s_return (car (args));
4207 } 4609 }
4208 else 4610 else
4209 Error_0 ("set-cdr!: unable to alter immutable pair"); 4611 Error_0 ("set-cdr!: unable to alter immutable pair");
4210 4612
4211 case OP_CHAR2INT: /* char->integer */ 4613 case OP_CHAR2INT: /* char->integer */
4212 s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4614 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4213 4615
4214 case OP_INT2CHAR: /* integer->char */ 4616 case OP_INT2CHAR: /* integer->char */
4215 s_return (mk_character (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4617 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4216 4618
4217 case OP_CHARUPCASE: 4619 case OP_CHARUPCASE:
4218 { 4620 {
4219 unsigned char c = ivalue (car (SCHEME_V->args)); 4621 unsigned char c = ivalue_unchecked (x);
4220 c = toupper (c); 4622 c = toupper (c);
4221 s_return (mk_character (SCHEME_A_ c)); 4623 s_return (mk_character (SCHEME_A_ c));
4222 } 4624 }
4223 4625
4224 case OP_CHARDNCASE: 4626 case OP_CHARDNCASE:
4225 { 4627 {
4226 unsigned char c = ivalue (car (SCHEME_V->args)); 4628 unsigned char c = ivalue_unchecked (x);
4227 c = tolower (c); 4629 c = tolower (c);
4228 s_return (mk_character (SCHEME_A_ c)); 4630 s_return (mk_character (SCHEME_A_ c));
4229 } 4631 }
4230 4632
4231 case OP_STR2SYM: /* string->symbol */ 4633 case OP_STR2SYM: /* string->symbol */
4232 s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); 4634 s_return (mk_symbol (SCHEME_A_ strvalue (x)));
4233 4635
4234 case OP_STR2ATOM: /* string->atom */ 4636 case OP_STR2ATOM: /* string->atom */
4235 { 4637 {
4236 char *s = strvalue (car (SCHEME_V->args)); 4638 char *s = strvalue (x);
4237 long pf = 0; 4639 long pf = 0;
4238 4640
4239 if (cdr (SCHEME_V->args) != NIL) 4641 if (cdr (args) != NIL)
4240 { 4642 {
4241 /* we know cadr(SCHEME_V->args) is a natural number */ 4643 /* we know cadr(args) is a natural number */
4242 /* see if it is 2, 8, 10, or 16, or error */ 4644 /* see if it is 2, 8, 10, or 16, or error */
4243 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4645 pf = ivalue_unchecked (cadr (args));
4244 4646
4245 if (pf == 16 || pf == 10 || pf == 8 || pf == 2) 4647 if (pf == 16 || pf == 10 || pf == 8 || pf == 2)
4246 { 4648 {
4247 /* base is OK */ 4649 /* base is OK */
4248 } 4650 }
4249 else 4651 else
4250 pf = -1; 4652 pf = -1;
4251 } 4653 }
4252 4654
4253 if (pf < 0) 4655 if (pf < 0)
4254 Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); 4656 Error_1 ("string->atom: bad base:", cadr (args));
4255 else if (*s == '#') /* no use of base! */ 4657 else if (*s == '#') /* no use of base! */
4256 s_return (mk_sharp_const (SCHEME_A_ s + 1)); 4658 s_return (mk_sharp_const (SCHEME_A_ s + 1));
4257 else 4659 else
4258 { 4660 {
4259 if (pf == 0 || pf == 10) 4661 if (pf == 0 || pf == 10)
4270 } 4672 }
4271 } 4673 }
4272 } 4674 }
4273 4675
4274 case OP_SYM2STR: /* symbol->string */ 4676 case OP_SYM2STR: /* symbol->string */
4275 x = mk_string (SCHEME_A_ symname (car (SCHEME_V->args))); 4677 x = mk_string (SCHEME_A_ symname (x));
4276 setimmutable (x); 4678 setimmutable (x);
4277 s_return (x); 4679 s_return (x);
4278 4680
4279 case OP_ATOM2STR: /* atom->string */ 4681 case OP_ATOM2STR: /* atom->string */
4280 { 4682 {
4281 long pf = 0; 4683 long pf = 0;
4282 4684
4283 x = car (SCHEME_V->args);
4284
4285 if (cdr (SCHEME_V->args) != NIL) 4685 if (cdr (args) != NIL)
4286 { 4686 {
4287 /* we know cadr(SCHEME_V->args) is a natural number */ 4687 /* we know cadr(args) is a natural number */
4288 /* see if it is 2, 8, 10, or 16, or error */ 4688 /* see if it is 2, 8, 10, or 16, or error */
4289 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4689 pf = ivalue_unchecked (cadr (args));
4290 4690
4291 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) 4691 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2))
4292 { 4692 {
4293 /* base is OK */ 4693 /* base is OK */
4294 } 4694 }
4295 else 4695 else
4296 pf = -1; 4696 pf = -1;
4297 } 4697 }
4298 4698
4299 if (pf < 0) 4699 if (pf < 0)
4300 Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); 4700 Error_1 ("atom->string: bad base:", cadr (args));
4301 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) 4701 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x))
4302 { 4702 {
4303 char *p; 4703 char *p;
4304 int len; 4704 int len;
4305 4705
4310 Error_1 ("atom->string: not an atom:", x); 4710 Error_1 ("atom->string: not an atom:", x);
4311 } 4711 }
4312 4712
4313 case OP_MKSTRING: /* make-string */ 4713 case OP_MKSTRING: /* make-string */
4314 { 4714 {
4315 int fill = ' '; 4715 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4316 int len; 4716 int len = ivalue_unchecked (x);
4317 4717
4318 len = ivalue (car (SCHEME_V->args));
4319
4320 if (cdr (SCHEME_V->args) != NIL)
4321 fill = charvalue (cadr (SCHEME_V->args));
4322
4323 s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); 4718 s_return (mk_empty_string (SCHEME_A_ len, fill));
4324 } 4719 }
4325 4720
4326 case OP_STRLEN: /* string-length */ 4721 case OP_STRLEN: /* string-length */
4327 s_return (mk_integer (SCHEME_A_ strlength (car (SCHEME_V->args)))); 4722 s_return (mk_integer (SCHEME_A_ strlength (x)));
4328 4723
4329 case OP_STRREF: /* string-ref */ 4724 case OP_STRREF: /* string-ref */
4330 { 4725 {
4331 char *str; 4726 char *str = strvalue (x);
4332 int index;
4333
4334 str = strvalue (car (SCHEME_V->args));
4335
4336 index = ivalue (cadr (SCHEME_V->args)); 4727 int index = ivalue_unchecked (cadr (args));
4337 4728
4338 if (index >= strlength (car (SCHEME_V->args))) 4729 if (index >= strlength (x))
4339 Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); 4730 Error_1 ("string-ref: out of bounds:", cadr (args));
4340 4731
4341 s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); 4732 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4342 } 4733 }
4343 4734
4344 case OP_STRSET: /* string-set! */ 4735 case OP_STRSET: /* string-set! */
4345 { 4736 {
4346 char *str; 4737 char *str = strvalue (x);
4347 int index; 4738 int index = ivalue_unchecked (cadr (args));
4348 int c; 4739 int c;
4349 4740
4350 if (is_immutable (car (SCHEME_V->args))) 4741 if (is_immutable (x))
4351 Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); 4742 Error_1 ("string-set!: unable to alter immutable string:", x);
4352 4743
4353 str = strvalue (car (SCHEME_V->args));
4354
4355 index = ivalue (cadr (SCHEME_V->args));
4356
4357 if (index >= strlength (car (SCHEME_V->args))) 4744 if (index >= strlength (x))
4358 Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); 4745 Error_1 ("string-set!: out of bounds:", cadr (args));
4359 4746
4360 c = charvalue (caddr (SCHEME_V->args)); 4747 c = charvalue (caddr (args));
4361 4748
4362 str[index] = (char) c; 4749 str[index] = c;
4363 s_return (car (SCHEME_V->args)); 4750 s_return (car (args));
4364 } 4751 }
4365 4752
4366 case OP_STRAPPEND: /* string-append */ 4753 case OP_STRAPPEND: /* string-append */
4367 { 4754 {
4368 /* in 1.29 string-append was in Scheme in init.scm but was too slow */ 4755 /* in 1.29 string-append was in Scheme in init.scm but was too slow */
4369 int len = 0; 4756 int len = 0;
4370 pointer newstr; 4757 pointer newstr;
4371 char *pos; 4758 char *pos;
4372 4759
4373 /* compute needed length for new string */ 4760 /* compute needed length for new string */
4374 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4761 for (x = args; x != NIL; x = cdr (x))
4375 len += strlength (car (x)); 4762 len += strlength (car (x));
4376 4763
4377 newstr = mk_empty_string (SCHEME_A_ len, ' '); 4764 newstr = mk_empty_string (SCHEME_A_ len, ' ');
4378 4765
4379 /* store the contents of the argument strings into the new string */ 4766 /* store the contents of the argument strings into the new string */
4380 for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x)) 4767 for (pos = strvalue (newstr), x = args; x != NIL; pos += strlength (car (x)), x = cdr (x))
4381 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4768 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4382 4769
4383 s_return (newstr); 4770 s_return (newstr);
4384 } 4771 }
4385 4772
4386 case OP_SUBSTR: /* substring */ 4773 case OP_STRING_COPY: /* substring/string-copy */
4387 { 4774 {
4388 char *str; 4775 char *str = strvalue (x);
4389 int index0; 4776 int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args));
4390 int index1; 4777 int index1;
4391 int len; 4778 int len;
4392 4779
4393 str = strvalue (car (SCHEME_V->args));
4394
4395 index0 = ivalue (cadr (SCHEME_V->args));
4396
4397 if (index0 > strlength (car (SCHEME_V->args))) 4780 if (index0 > strlength (x))
4398 Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); 4781 Error_1 ("string->copy: start out of bounds:", cadr (args));
4399 4782
4400 if (cddr (SCHEME_V->args) != NIL) 4783 if (cddr (args) != NIL)
4401 { 4784 {
4402 index1 = ivalue (caddr (SCHEME_V->args)); 4785 index1 = ivalue_unchecked (caddr (args));
4403 4786
4404 if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) 4787 if (index1 > strlength (x) || index1 < index0)
4405 Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); 4788 Error_1 ("string->copy: end out of bounds:", caddr (args));
4406 } 4789 }
4407 else 4790 else
4408 index1 = strlength (car (SCHEME_V->args)); 4791 index1 = strlength (x);
4409 4792
4410 len = index1 - index0; 4793 len = index1 - index0;
4411 x = mk_empty_string (SCHEME_A_ len, ' '); 4794 x = mk_counted_string (SCHEME_A_ str + index0, len);
4412 memcpy (strvalue (x), str + index0, len);
4413 strvalue (x)[len] = 0;
4414 4795
4415 s_return (x); 4796 s_return (x);
4416 } 4797 }
4417 4798
4418 case OP_VECTOR: /* vector */ 4799 case OP_VECTOR: /* vector */
4419 { 4800 {
4420 int i; 4801 int i;
4421 pointer vec; 4802 pointer vec;
4422 int len = list_length (SCHEME_A_ SCHEME_V->args); 4803 int len = list_length (SCHEME_A_ args);
4423 4804
4424 if (len < 0) 4805 if (len < 0)
4425 Error_1 ("vector: not a proper list:", SCHEME_V->args); 4806 Error_1 ("vector: not a proper list:", args);
4426 4807
4427 vec = mk_vector (SCHEME_A_ len); 4808 vec = mk_vector (SCHEME_A_ len);
4428 4809
4429#if USE_ERROR_CHECKING 4810#if USE_ERROR_CHECKING
4430 if (SCHEME_V->no_memory) 4811 if (SCHEME_V->no_memory)
4431 s_return (S_SINK); 4812 s_return (S_SINK);
4432#endif 4813#endif
4433 4814
4434 for (x = SCHEME_V->args, i = 0; is_pair (x); x = cdr (x), i++) 4815 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4435 set_vector_elem (vec, i, car (x)); 4816 vector_set (vec, i, car (x));
4436 4817
4437 s_return (vec); 4818 s_return (vec);
4438 } 4819 }
4439 4820
4440 case OP_MKVECTOR: /* make-vector */ 4821 case OP_MKVECTOR: /* make-vector */
4441 { 4822 {
4442 pointer fill = NIL; 4823 pointer fill = NIL;
4443 int len;
4444 pointer vec; 4824 pointer vec;
4825 int len = ivalue_unchecked (x);
4445 4826
4446 len = ivalue (car (SCHEME_V->args));
4447
4448 if (cdr (SCHEME_V->args) != NIL) 4827 if (cdr (args) != NIL)
4449 fill = cadr (SCHEME_V->args); 4828 fill = cadr (args);
4450 4829
4451 vec = mk_vector (SCHEME_A_ len); 4830 vec = mk_vector (SCHEME_A_ len);
4452 4831
4453#if USE_ERROR_CHECKING 4832#if USE_ERROR_CHECKING
4454 if (SCHEME_V->no_memory) 4833 if (SCHEME_V->no_memory)
4455 s_return (S_SINK); 4834 s_return (S_SINK);
4456#endif 4835#endif
4457 4836
4458 if (fill != NIL) 4837 if (fill != NIL)
4459 fill_vector (vec, fill); 4838 fill_vector (vec, 0, fill);
4460 4839
4461 s_return (vec); 4840 s_return (vec);
4462 } 4841 }
4463 4842
4464 case OP_VECLEN: /* vector-length */ 4843 case OP_VECLEN: /* vector-length */
4465 s_return (mk_integer (SCHEME_A_ veclength (car (SCHEME_V->args)))); 4844 s_return (mk_integer (SCHEME_A_ veclength (x)));
4845
4846 case OP_VECRESIZE:
4847 vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args));
4848 s_return (x);
4466 4849
4467 case OP_VECREF: /* vector-ref */ 4850 case OP_VECREF: /* vector-ref */
4468 { 4851 {
4469 int index;
4470
4471 index = ivalue (cadr (SCHEME_V->args)); 4852 int index = ivalue_unchecked (cadr (args));
4472 4853
4473 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) 4854 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4474 Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); 4855 Error_1 ("vector-ref: out of bounds:", cadr (args));
4475 4856
4476 s_return (vector_elem (car (SCHEME_V->args), index)); 4857 s_return (vector_get (x, index));
4477 } 4858 }
4478 4859
4479 case OP_VECSET: /* vector-set! */ 4860 case OP_VECSET: /* vector-set! */
4480 { 4861 {
4481 int index; 4862 int index = ivalue_unchecked (cadr (args));
4482 4863
4483 if (is_immutable (car (SCHEME_V->args))) 4864 if (is_immutable (x))
4484 Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); 4865 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4485 4866
4486 index = ivalue (cadr (SCHEME_V->args));
4487
4488 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) 4867 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4489 Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); 4868 Error_1 ("vector-set!: out of bounds:", cadr (args));
4490 4869
4491 set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); 4870 vector_set (x, index, caddr (args));
4492 s_return (car (SCHEME_V->args)); 4871 s_return (x);
4493 } 4872 }
4494 } 4873 }
4495 4874
4496 return S_T; 4875 if (USE_ERROR_CHECKING) abort ();
4497} 4876}
4498 4877
4499INTERFACE int 4878/* relational ops */
4500is_list (SCHEME_P_ pointer a) 4879ecb_hot static int
4880opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4501{ 4881{
4502 return list_length (SCHEME_A_ a) >= 0; 4882 pointer x = SCHEME_V->args;
4503}
4504 4883
4505/* Result is: 4884 for (;;)
4506 proper list: length
4507 circular list: -1
4508 not even a pair: -2
4509 dotted list: -2 minus length before dot
4510*/
4511INTERFACE int
4512list_length (SCHEME_P_ pointer a)
4513{
4514 int i = 0;
4515 pointer slow, fast;
4516
4517 slow = fast = a;
4518
4519 while (1)
4520 { 4885 {
4886 num v = nvalue (car (x));
4887 x = cdr (x);
4888
4521 if (fast == NIL) 4889 if (x == NIL)
4522 return i; 4890 break;
4523 4891
4524 if (!is_pair (fast)) 4892 int r = num_cmp (v, nvalue (car (x)));
4525 return -2 - i;
4526 4893
4527 fast = cdr (fast); 4894 switch (op)
4528 ++i;
4529
4530 if (fast == NIL)
4531 return i;
4532
4533 if (!is_pair (fast))
4534 return -2 - i;
4535
4536 ++i;
4537 fast = cdr (fast);
4538
4539 /* Safe because we would have already returned if `fast'
4540 encountered a non-pair. */
4541 slow = cdr (slow);
4542
4543 if (fast == slow)
4544 { 4895 {
4545 /* the fast pointer has looped back around and caught up 4896 case OP_NUMEQ: r = r == 0; break;
4546 with the slow pointer, hence the structure is circular, 4897 case OP_LESS: r = r < 0; break;
4547 not of finite length, and therefore not a list */ 4898 case OP_GRE: r = r > 0; break;
4548 return -1; 4899 case OP_LEQ: r = r <= 0; break;
4900 case OP_GEQ: r = r >= 0; break;
4549 } 4901 }
4550 }
4551}
4552 4902
4553static pointer 4903 if (!r)
4904 s_return (S_F);
4905 }
4906
4907 s_return (S_T);
4908}
4909
4910/* predicates */
4911ecb_hot static int
4554opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4912opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4555{ 4913{
4556 pointer x; 4914 pointer args = SCHEME_V->args;
4557 num v; 4915 pointer a = car (args);
4558 int (*comp_func) (num, num); 4916 pointer d = cdr (args);
4917 int r;
4559 4918
4560 switch (op) 4919 switch (op)
4561 { 4920 {
4562 case OP_NOT: /* not */ 4921 case OP_NOT: /* not */ r = is_false (a) ; break;
4563 s_retbool (is_false (car (SCHEME_V->args))); 4922 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break;
4923 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break;
4924 case OP_NULLP: /* null? */ r = a == NIL ; break;
4925 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break;
4926 case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break;
4927 case OP_NUMBERP: /* number? */ r = is_number (a) ; break;
4928 case OP_STRINGP: /* string? */ r = is_string (a) ; break;
4929 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4930 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4931 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4564 4932
4565 case OP_BOOLP: /* boolean? */
4566 s_retbool (car (SCHEME_V->args) == S_F || car (SCHEME_V->args) == S_T);
4567
4568 case OP_EOFOBJP: /* boolean? */
4569 s_retbool (car (SCHEME_V->args) == S_EOF);
4570
4571 case OP_NULLP: /* null? */
4572 s_retbool (car (SCHEME_V->args) == NIL);
4573
4574 case OP_NUMEQ: /* = */
4575 case OP_LESS: /* < */
4576 case OP_GRE: /* > */
4577 case OP_LEQ: /* <= */
4578 case OP_GEQ: /* >= */
4579 switch (op)
4580 {
4581 case OP_NUMEQ:
4582 comp_func = num_eq;
4583 break;
4584
4585 case OP_LESS:
4586 comp_func = num_lt;
4587 break;
4588
4589 case OP_GRE:
4590 comp_func = num_gt;
4591 break;
4592
4593 case OP_LEQ:
4594 comp_func = num_le;
4595 break;
4596
4597 case OP_GEQ:
4598 comp_func = num_ge;
4599 break;
4600 }
4601
4602 x = SCHEME_V->args;
4603 v = nvalue (car (x));
4604 x = cdr (x);
4605
4606 for (; x != NIL; x = cdr (x))
4607 {
4608 if (!comp_func (v, nvalue (car (x))))
4609 s_retbool (0);
4610
4611 v = nvalue (car (x));
4612 }
4613
4614 s_retbool (1);
4615
4616 case OP_SYMBOLP: /* symbol? */
4617 s_retbool (is_symbol (car (SCHEME_V->args)));
4618
4619 case OP_NUMBERP: /* number? */
4620 s_retbool (is_number (car (SCHEME_V->args)));
4621
4622 case OP_STRINGP: /* string? */
4623 s_retbool (is_string (car (SCHEME_V->args)));
4624
4625 case OP_INTEGERP: /* integer? */
4626 s_retbool (is_integer (car (SCHEME_V->args)));
4627
4628 case OP_REALP: /* real? */
4629 s_retbool (is_number (car (SCHEME_V->args))); /* All numbers are real */
4630
4631 case OP_CHARP: /* char? */
4632 s_retbool (is_character (car (SCHEME_V->args)));
4633#if USE_CHAR_CLASSIFIERS 4933#if USE_CHAR_CLASSIFIERS
4634 4934 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4635 case OP_CHARAP: /* char-alphabetic? */ 4935 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4636 s_retbool (Cisalpha (ivalue (car (SCHEME_V->args)))); 4936 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4637 4937 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4638 case OP_CHARNP: /* char-numeric? */ 4938 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4639 s_retbool (Cisdigit (ivalue (car (SCHEME_V->args))));
4640
4641 case OP_CHARWP: /* char-whitespace? */
4642 s_retbool (Cisspace (ivalue (car (SCHEME_V->args))));
4643
4644 case OP_CHARUP: /* char-upper-case? */
4645 s_retbool (Cisupper (ivalue (car (SCHEME_V->args))));
4646
4647 case OP_CHARLP: /* char-lower-case? */
4648 s_retbool (Cislower (ivalue (car (SCHEME_V->args))));
4649#endif 4939#endif
4940
4650#if USE_PORTS 4941#if USE_PORTS
4651 4942 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4652 case OP_PORTP: /* port? */
4653 s_retbool (is_port (car (SCHEME_V->args)));
4654
4655 case OP_INPORTP: /* input-port? */ 4943 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4656 s_retbool (is_inport (car (SCHEME_V->args)));
4657
4658 case OP_OUTPORTP: /* output-port? */ 4944 case OP_OUTPORTP: /* output-port? */ r = is_outport (a); break;
4659 s_retbool (is_outport (car (SCHEME_V->args)));
4660#endif 4945#endif
4661 4946
4662 case OP_PROCP: /* procedure? */ 4947 case OP_PROCP: /* procedure? */
4663 4948
4664 /*-- 4949 /*--
4665 * continuation should be procedure by the example 4950 * continuation should be procedure by the example
4666 * (call-with-current-continuation procedure?) ==> #t 4951 * (call-with-current-continuation procedure?) ==> #t
4667 * in R^3 report sec. 6.9 4952 * in R^3 report sec. 6.9
4668 */ 4953 */
4669 s_retbool (is_proc (car (SCHEME_V->args)) || is_closure (car (SCHEME_V->args)) 4954 r = is_proc (a) || is_closure (a) || is_continuation (a) || is_foreign (a);
4670 || is_continuation (car (SCHEME_V->args)) || is_foreign (car (SCHEME_V->args))); 4955 break;
4671 4956
4672 case OP_PAIRP: /* pair? */ 4957 case OP_PAIRP: /* pair? */ r = is_pair (a) ; break;
4673 s_retbool (is_pair (car (SCHEME_V->args))); 4958 case OP_LISTP: /* list? */ r = list_length (SCHEME_A_ a) >= 0; break;
4674 4959 case OP_ENVP: /* environment? */ r = is_environment (a) ; break;
4675 case OP_LISTP: /* list? */ 4960 case OP_VECTORP: /* vector? */ r = is_vector (a) ; break;
4676 s_retbool (list_length (SCHEME_A_ car (SCHEME_V->args)) >= 0); 4961 case OP_EQ: /* eq? */ r = a == cadr (args) ; break;
4677 4962 case OP_EQV: /* eqv? */ r = eqv (a, cadr (args)) ; break;
4678 case OP_ENVP: /* environment? */
4679 s_retbool (is_environment (car (SCHEME_V->args)));
4680
4681 case OP_VECTORP: /* vector? */
4682 s_retbool (is_vector (car (SCHEME_V->args)));
4683
4684 case OP_EQ: /* eq? */
4685 s_retbool (car (SCHEME_V->args) == cadr (SCHEME_V->args));
4686
4687 case OP_EQV: /* eqv? */
4688 s_retbool (eqv (car (SCHEME_V->args), cadr (SCHEME_V->args)));
4689 } 4963 }
4690 4964
4691 return S_T; 4965 s_retbool (r);
4692} 4966}
4693 4967
4694static pointer 4968/* promises, list ops, ports */
4969ecb_hot static int
4695opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4970opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4696{ 4971{
4972 pointer args = SCHEME_V->args;
4973 pointer a = car (args);
4697 pointer x, y; 4974 pointer x, y;
4698 4975
4699 switch (op) 4976 switch (op)
4700 { 4977 {
4701 case OP_FORCE: /* force */ 4978 case OP_FORCE: /* force */
4702 SCHEME_V->code = car (SCHEME_V->args); 4979 SCHEME_V->code = a;
4703 4980
4704 if (is_promise (SCHEME_V->code)) 4981 if (is_promise (SCHEME_V->code))
4705 { 4982 {
4706 /* Should change type to closure here */ 4983 /* Should change type to closure here */
4707 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); 4984 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code);
4710 } 4987 }
4711 else 4988 else
4712 s_return (SCHEME_V->code); 4989 s_return (SCHEME_V->code);
4713 4990
4714 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4991 case OP_SAVE_FORCED: /* Save forced value replacing promise */
4715 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); 4992 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
4716 s_return (SCHEME_V->value); 4993 s_return (SCHEME_V->value);
4717 4994
4718#if USE_PORTS 4995#if USE_PORTS
4996
4997 case OP_EOF_OBJECT: /* eof-object */
4998 s_return (S_EOF);
4719 4999
4720 case OP_WRITE: /* write */ 5000 case OP_WRITE: /* write */
4721 case OP_DISPLAY: /* display */ 5001 case OP_DISPLAY: /* display */
4722 case OP_WRITE_CHAR: /* write-char */ 5002 case OP_WRITE_CHAR: /* write-char */
4723 if (is_pair (cdr (SCHEME_V->args))) 5003 if (is_pair (cdr (SCHEME_V->args)))
4728 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 5008 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4729 SCHEME_V->outport = cadr (SCHEME_V->args); 5009 SCHEME_V->outport = cadr (SCHEME_V->args);
4730 } 5010 }
4731 } 5011 }
4732 5012
4733 SCHEME_V->args = car (SCHEME_V->args); 5013 SCHEME_V->args = a;
4734 5014
4735 if (op == OP_WRITE) 5015 if (op == OP_WRITE)
4736 SCHEME_V->print_flag = 1; 5016 SCHEME_V->print_flag = 1;
4737 else 5017 else
4738 SCHEME_V->print_flag = 0; 5018 SCHEME_V->print_flag = 0;
4739 5019
4740 s_goto (OP_P0LIST); 5020 s_goto (OP_P0LIST);
4741 5021
5022 //TODO: move to scheme
4742 case OP_NEWLINE: /* newline */ 5023 case OP_NEWLINE: /* newline */
4743 if (is_pair (SCHEME_V->args)) 5024 if (is_pair (args))
4744 { 5025 {
4745 if (car (SCHEME_V->args) != SCHEME_V->outport) 5026 if (a != SCHEME_V->outport)
4746 { 5027 {
4747 x = cons (SCHEME_V->outport, NIL); 5028 x = cons (SCHEME_V->outport, NIL);
4748 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 5029 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4749 SCHEME_V->outport = car (SCHEME_V->args); 5030 SCHEME_V->outport = a;
4750 } 5031 }
4751 } 5032 }
4752 5033
4753 putstr (SCHEME_A_ "\n"); 5034 putcharacter (SCHEME_A_ '\n');
4754 s_return (S_T); 5035 s_return (S_T);
4755#endif 5036#endif
4756 5037
4757 case OP_ERR0: /* error */ 5038 case OP_ERR0: /* error */
4758 SCHEME_V->retcode = -1; 5039 SCHEME_V->retcode = -1;
4759 5040
4760 if (!is_string (car (SCHEME_V->args))) 5041 if (!is_string (a))
4761 { 5042 {
4762 SCHEME_V->args = cons (mk_string (SCHEME_A_ " -- "), SCHEME_V->args); 5043 args = cons (mk_string (SCHEME_A_ " -- "), args);
4763 setimmutable (car (SCHEME_V->args)); 5044 setimmutable (car (args));
4764 } 5045 }
4765 5046
4766 putstr (SCHEME_A_ "Error: "); 5047 putstr (SCHEME_A_ "Error: ");
4767 putstr (SCHEME_A_ strvalue (car (SCHEME_V->args))); 5048 putstr (SCHEME_A_ strvalue (car (args)));
4768 SCHEME_V->args = cdr (SCHEME_V->args); 5049 SCHEME_V->args = cdr (args);
4769 s_goto (OP_ERR1); 5050 s_goto (OP_ERR1);
4770 5051
4771 case OP_ERR1: /* error */ 5052 case OP_ERR1: /* error */
4772 putstr (SCHEME_A_ " "); 5053 putcharacter (SCHEME_A_ ' ');
4773 5054
4774 if (SCHEME_V->args != NIL) 5055 if (args != NIL)
4775 { 5056 {
4776 s_save (SCHEME_A_ OP_ERR1, cdr (SCHEME_V->args), NIL); 5057 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4777 SCHEME_V->args = car (SCHEME_V->args); 5058 SCHEME_V->args = a;
4778 SCHEME_V->print_flag = 1; 5059 SCHEME_V->print_flag = 1;
4779 s_goto (OP_P0LIST); 5060 s_goto (OP_P0LIST);
4780 } 5061 }
4781 else 5062 else
4782 { 5063 {
4783 putstr (SCHEME_A_ "\n"); 5064 putcharacter (SCHEME_A_ '\n');
4784 5065
4785 if (SCHEME_V->interactive_repl) 5066 if (SCHEME_V->interactive_repl)
4786 s_goto (OP_T0LVL); 5067 s_goto (OP_T0LVL);
4787 else 5068 else
4788 return NIL; 5069 return -1;
4789 } 5070 }
4790 5071
4791 case OP_REVERSE: /* reverse */ 5072 case OP_REVERSE: /* reverse */
4792 s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); 5073 s_return (reverse (SCHEME_A_ a));
4793 5074
4794 case OP_LIST_STAR: /* list* */ 5075 case OP_LIST_STAR: /* list* */
4795 s_return (list_star (SCHEME_A_ SCHEME_V->args)); 5076 s_return (list_star (SCHEME_A_ SCHEME_V->args));
4796 5077
4797 case OP_APPEND: /* append */ 5078 case OP_APPEND: /* append */
4798 x = NIL; 5079 x = NIL;
4799 y = SCHEME_V->args; 5080 y = args;
4800 5081
4801 if (y == x) 5082 if (y == x)
4802 s_return (x); 5083 s_return (x);
4803 5084
4804 /* cdr() in the while condition is not a typo. If car() */ 5085 /* cdr() in the while condition is not a typo. If car() */
4815 s_return (reverse_in_place (SCHEME_A_ car (y), x)); 5096 s_return (reverse_in_place (SCHEME_A_ car (y), x));
4816 5097
4817#if USE_PLIST 5098#if USE_PLIST
4818 5099
4819 case OP_PUT: /* put */ 5100 case OP_PUT: /* put */
4820 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 5101 if (!hasprop (a) || !hasprop (cadr (args)))
4821 Error_0 ("illegal use of put"); 5102 Error_0 ("illegal use of put");
4822 5103
4823 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 5104 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
4824 { 5105 {
4825 if (caar (x) == y) 5106 if (caar (x) == y)
4826 break; 5107 break;
4827 } 5108 }
4828 5109
4829 if (x != NIL) 5110 if (x != NIL)
4830 cdar (x) = caddr (SCHEME_V->args); 5111 cdar (x) = caddr (args);
4831 else 5112 else
4832 symprop (car (SCHEME_V->args)) = cons (cons (y, caddr (SCHEME_V->args)), symprop (car (SCHEME_V->args))); 5113 symprop (a) = cons (cons (y, caddr (args)), symprop (a));
4833 5114
4834 s_return (S_T); 5115 s_return (S_T);
4835 5116
4836 case OP_GET: /* get */ 5117 case OP_GET: /* get */
4837 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 5118 if (!hasprop (a) || !hasprop (cadr (args)))
4838 Error_0 ("illegal use of get"); 5119 Error_0 ("illegal use of get");
4839 5120
4840 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 5121 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
4841 if (caar (x) == y) 5122 if (caar (x) == y)
4842 break; 5123 break;
4843 5124
4844 if (x != NIL) 5125 if (x != NIL)
4845 s_return (cdar (x)); 5126 s_return (cdar (x));
4847 s_return (NIL); 5128 s_return (NIL);
4848 5129
4849#endif /* USE_PLIST */ 5130#endif /* USE_PLIST */
4850 5131
4851 case OP_QUIT: /* quit */ 5132 case OP_QUIT: /* quit */
4852 if (is_pair (SCHEME_V->args)) 5133 if (is_pair (args))
4853 SCHEME_V->retcode = ivalue (car (SCHEME_V->args)); 5134 SCHEME_V->retcode = ivalue (a);
4854 5135
4855 return NIL; 5136 return -1;
4856 5137
4857 case OP_GC: /* gc */ 5138 case OP_GC: /* gc */
4858 gc (SCHEME_A_ NIL, NIL); 5139 gc (SCHEME_A_ NIL, NIL);
4859 s_return (S_T); 5140 s_return (S_T);
4860 5141
4861 case OP_GCVERB: /* gc-verbose */ 5142 case OP_GCVERB: /* gc-verbose */
4862 { 5143 {
4863 int was = SCHEME_V->gc_verbose; 5144 int was = SCHEME_V->gc_verbose;
4864 5145
4865 SCHEME_V->gc_verbose = (car (SCHEME_V->args) != S_F); 5146 SCHEME_V->gc_verbose = (a != S_F);
4866 s_retbool (was); 5147 s_retbool (was);
4867 } 5148 }
4868 5149
4869 case OP_NEWSEGMENT: /* new-segment */ 5150 case OP_NEWSEGMENT: /* new-segment */
5151#if 0
4870 if (!is_pair (SCHEME_V->args) || !is_number (car (SCHEME_V->args))) 5152 if (!is_pair (args) || !is_number (a))
4871 Error_0 ("new-segment: argument must be a number"); 5153 Error_0 ("new-segment: argument must be a number");
4872 5154#endif
4873 alloc_cellseg (SCHEME_A_ (int)ivalue (car (SCHEME_V->args))); 5155 s_retbool (alloc_cellseg (SCHEME_A));
4874
4875 s_return (S_T);
4876 5156
4877 case OP_OBLIST: /* oblist */ 5157 case OP_OBLIST: /* oblist */
4878 s_return (oblist_all_symbols (SCHEME_A)); 5158 s_return (oblist_all_symbols (SCHEME_A));
4879 5159
4880#if USE_PORTS 5160#if USE_PORTS
4905 case OP_OPEN_INOUTFILE: 5185 case OP_OPEN_INOUTFILE:
4906 prop = port_input | port_output; 5186 prop = port_input | port_output;
4907 break; 5187 break;
4908 } 5188 }
4909 5189
4910 p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); 5190 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4911 5191
4912 if (p == NIL) 5192 s_return (p == NIL ? S_F : p);
4913 s_return (S_F);
4914
4915 s_return (p);
4916 } 5193 }
4917 5194
4918# if USE_STRING_PORTS 5195# if USE_STRING_PORTS
4919 5196
4920 case OP_OPEN_INSTRING: /* open-input-string */ 5197 case OP_OPEN_INSTRING: /* open-input-string */
4932 case OP_OPEN_INOUTSTRING: 5209 case OP_OPEN_INOUTSTRING:
4933 prop = port_input | port_output; 5210 prop = port_input | port_output;
4934 break; 5211 break;
4935 } 5212 }
4936 5213
4937 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 5214 p = port_from_string (SCHEME_A_ strvalue (a),
4938 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); 5215 strvalue (a) + strlength (a), prop);
4939 5216
4940 if (p == NIL) 5217 s_return (p == NIL ? S_F : p);
4941 s_return (S_F);
4942
4943 s_return (p);
4944 } 5218 }
4945 5219
4946 case OP_OPEN_OUTSTRING: /* open-output-string */ 5220 case OP_OPEN_OUTSTRING: /* open-output-string */
4947 { 5221 {
4948 pointer p; 5222 pointer p;
4949 5223
4950 if (car (SCHEME_V->args) == NIL) 5224 if (a == NIL)
4951 {
4952 p = port_from_scratch (SCHEME_A); 5225 p = port_from_scratch (SCHEME_A);
4953
4954 if (p == NIL)
4955 s_return (S_F);
4956 }
4957 else 5226 else
4958 {
4959 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 5227 p = port_from_string (SCHEME_A_ strvalue (a),
4960 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); 5228 strvalue (a) + strlength (a), port_output);
4961 5229
4962 if (p == NIL) 5230 s_return (p == NIL ? S_F : p);
4963 s_return (S_F);
4964 }
4965
4966 s_return (p);
4967 } 5231 }
4968 5232
4969 case OP_GET_OUTSTRING: /* get-output-string */ 5233 case OP_GET_OUTSTRING: /* get-output-string */
4970 { 5234 {
4971 port *p; 5235 port *p = port (a);
4972 5236
4973 if ((p = car (SCHEME_V->args)->object.port)->kind & port_string) 5237 if (p->kind & port_string)
4974 { 5238 {
4975 off_t size; 5239 off_t size;
4976 char *str; 5240 char *str;
4977 5241
4978 size = p->rep.string.curr - p->rep.string.start + 1; 5242 size = p->rep.string.curr - p->rep.string.start + 1;
4994 } 5258 }
4995 5259
4996# endif 5260# endif
4997 5261
4998 case OP_CLOSE_INPORT: /* close-input-port */ 5262 case OP_CLOSE_INPORT: /* close-input-port */
4999 port_close (SCHEME_A_ car (SCHEME_V->args), port_input); 5263 port_close (SCHEME_A_ a, port_input);
5000 s_return (S_T); 5264 s_return (S_T);
5001 5265
5002 case OP_CLOSE_OUTPORT: /* close-output-port */ 5266 case OP_CLOSE_OUTPORT: /* close-output-port */
5003 port_close (SCHEME_A_ car (SCHEME_V->args), port_output); 5267 port_close (SCHEME_A_ a, port_output);
5004 s_return (S_T); 5268 s_return (S_T);
5005#endif 5269#endif
5006 5270
5007 case OP_INT_ENV: /* interaction-environment */ 5271 case OP_INT_ENV: /* interaction-environment */
5008 s_return (SCHEME_V->global_env); 5272 s_return (SCHEME_V->global_env);
5010 case OP_CURR_ENV: /* current-environment */ 5274 case OP_CURR_ENV: /* current-environment */
5011 s_return (SCHEME_V->envir); 5275 s_return (SCHEME_V->envir);
5012 5276
5013 } 5277 }
5014 5278
5015 return S_T; 5279 if (USE_ERROR_CHECKING) abort ();
5016} 5280}
5017 5281
5018static pointer 5282/* reading */
5283ecb_cold static int
5019opexe_5 (SCHEME_P_ enum scheme_opcodes op) 5284opexe_5 (SCHEME_P_ enum scheme_opcodes op)
5020{ 5285{
5286 pointer args = SCHEME_V->args;
5021 pointer x; 5287 pointer x;
5022 5288
5023 if (SCHEME_V->nesting != 0) 5289 if (SCHEME_V->nesting != 0)
5024 { 5290 {
5025 int n = SCHEME_V->nesting; 5291 int n = SCHEME_V->nesting;
5032 switch (op) 5298 switch (op)
5033 { 5299 {
5034 /* ========== reading part ========== */ 5300 /* ========== reading part ========== */
5035#if USE_PORTS 5301#if USE_PORTS
5036 case OP_READ: 5302 case OP_READ:
5037 if (!is_pair (SCHEME_V->args)) 5303 if (!is_pair (args))
5038 s_goto (OP_READ_INTERNAL); 5304 s_goto (OP_READ_INTERNAL);
5039 5305
5040 if (!is_inport (car (SCHEME_V->args))) 5306 if (!is_inport (car (args)))
5041 Error_1 ("read: not an input port:", car (SCHEME_V->args)); 5307 Error_1 ("read: not an input port:", car (args));
5042 5308
5043 if (car (SCHEME_V->args) == SCHEME_V->inport) 5309 if (car (args) == SCHEME_V->inport)
5044 s_goto (OP_READ_INTERNAL); 5310 s_goto (OP_READ_INTERNAL);
5045 5311
5046 x = SCHEME_V->inport; 5312 x = SCHEME_V->inport;
5047 SCHEME_V->inport = car (SCHEME_V->args); 5313 SCHEME_V->inport = car (args);
5048 x = cons (x, NIL); 5314 x = cons (x, NIL);
5049 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 5315 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5050 s_goto (OP_READ_INTERNAL); 5316 s_goto (OP_READ_INTERNAL);
5051 5317
5052 case OP_READ_CHAR: /* read-char */ 5318 case OP_READ_CHAR: /* read-char */
5053 case OP_PEEK_CHAR: /* peek-char */ 5319 case OP_PEEK_CHAR: /* peek-char */
5054 { 5320 {
5055 int c; 5321 int c;
5056 5322
5057 if (is_pair (SCHEME_V->args)) 5323 if (is_pair (args))
5058 { 5324 {
5059 if (car (SCHEME_V->args) != SCHEME_V->inport) 5325 if (car (args) != SCHEME_V->inport)
5060 { 5326 {
5061 x = SCHEME_V->inport; 5327 x = SCHEME_V->inport;
5062 x = cons (x, NIL); 5328 x = cons (x, NIL);
5063 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 5329 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5064 SCHEME_V->inport = car (SCHEME_V->args); 5330 SCHEME_V->inport = car (args);
5065 } 5331 }
5066 } 5332 }
5067 5333
5068 c = inchar (SCHEME_A); 5334 c = inchar (SCHEME_A);
5069 5335
5079 case OP_CHAR_READY: /* char-ready? */ 5345 case OP_CHAR_READY: /* char-ready? */
5080 { 5346 {
5081 pointer p = SCHEME_V->inport; 5347 pointer p = SCHEME_V->inport;
5082 int res; 5348 int res;
5083 5349
5084 if (is_pair (SCHEME_V->args)) 5350 if (is_pair (args))
5085 p = car (SCHEME_V->args); 5351 p = car (args);
5086 5352
5087 res = p->object.port->kind & port_string; 5353 res = port (p)->kind & port_string;
5088 5354
5089 s_retbool (res); 5355 s_retbool (res);
5090 } 5356 }
5091 5357
5092 case OP_SET_INPORT: /* set-input-port */ 5358 case OP_SET_INPORT: /* set-input-port */
5093 SCHEME_V->inport = car (SCHEME_V->args); 5359 SCHEME_V->inport = car (args);
5094 s_return (SCHEME_V->value); 5360 s_return (SCHEME_V->value);
5095 5361
5096 case OP_SET_OUTPORT: /* set-output-port */ 5362 case OP_SET_OUTPORT: /* set-output-port */
5097 SCHEME_V->outport = car (SCHEME_V->args); 5363 SCHEME_V->outport = car (args);
5098 s_return (SCHEME_V->value); 5364 s_return (SCHEME_V->value);
5099#endif 5365#endif
5100 5366
5101 case OP_RDSEXPR: 5367 case OP_RDSEXPR:
5102 switch (SCHEME_V->tok) 5368 switch (SCHEME_V->tok)
5103 { 5369 {
5104 case TOK_EOF: 5370 case TOK_EOF:
5105 s_return (S_EOF); 5371 s_return (S_EOF);
5106 /* NOTREACHED */
5107 5372
5108 case TOK_VEC: 5373 case TOK_VEC:
5109 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); 5374 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL);
5110 /* fall through */ 5375 /* fall through */
5111 5376
5114 5379
5115 if (SCHEME_V->tok == TOK_RPAREN) 5380 if (SCHEME_V->tok == TOK_RPAREN)
5116 s_return (NIL); 5381 s_return (NIL);
5117 else if (SCHEME_V->tok == TOK_DOT) 5382 else if (SCHEME_V->tok == TOK_DOT)
5118 Error_0 ("syntax error: illegal dot expression"); 5383 Error_0 ("syntax error: illegal dot expression");
5119 else 5384
5120 {
5121 SCHEME_V->nesting_stack[SCHEME_V->file_i]++; 5385 SCHEME_V->nesting_stack[SCHEME_V->file_i]++;
5122 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL); 5386 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL);
5123 s_goto (OP_RDSEXPR); 5387 s_goto (OP_RDSEXPR);
5124 }
5125 5388
5126 case TOK_QUOTE: 5389 case TOK_QUOTE:
5127 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL); 5390 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL);
5128 SCHEME_V->tok = token (SCHEME_A); 5391 SCHEME_V->tok = token (SCHEME_A);
5129 s_goto (OP_RDSEXPR); 5392 s_goto (OP_RDSEXPR);
5135 { 5398 {
5136 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); 5399 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL);
5137 SCHEME_V->tok = TOK_LPAREN; 5400 SCHEME_V->tok = TOK_LPAREN;
5138 s_goto (OP_RDSEXPR); 5401 s_goto (OP_RDSEXPR);
5139 } 5402 }
5140 else 5403
5141 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); 5404 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL);
5142
5143 s_goto (OP_RDSEXPR); 5405 s_goto (OP_RDSEXPR);
5144 5406
5145 case TOK_COMMA: 5407 case TOK_COMMA:
5146 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); 5408 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL);
5147 SCHEME_V->tok = token (SCHEME_A); 5409 SCHEME_V->tok = token (SCHEME_A);
5151 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 5413 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
5152 SCHEME_V->tok = token (SCHEME_A); 5414 SCHEME_V->tok = token (SCHEME_A);
5153 s_goto (OP_RDSEXPR); 5415 s_goto (OP_RDSEXPR);
5154 5416
5155 case TOK_ATOM: 5417 case TOK_ATOM:
5156 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)));
5157 5429
5158 case TOK_DQUOTE: 5430 case TOK_DQUOTE:
5159 x = readstrexp (SCHEME_A); 5431 x = readstrexp (SCHEME_A_ '"');
5160 5432
5161 if (x == S_F) 5433 if (x == S_F)
5162 Error_0 ("Error reading string"); 5434 Error_0 ("Error reading string");
5163 5435
5164 setimmutable (x); 5436 setimmutable (x);
5168 { 5440 {
5169 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);
5170 5442
5171 if (f == NIL) 5443 if (f == NIL)
5172 Error_0 ("undefined sharp expression"); 5444 Error_0 ("undefined sharp expression");
5173 else 5445
5174 {
5175 SCHEME_V->code = cons (slot_value_in_env (f), NIL); 5446 SCHEME_V->code = cons (slot_value_in_env (f), NIL);
5176 s_goto (OP_EVAL); 5447 s_goto (OP_EVAL);
5177 }
5178 } 5448 }
5179 5449
5180 case TOK_SHARP_CONST: 5450 case TOK_SHARP_CONST:
5181 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)
5182 Error_0 ("undefined sharp expression"); 5452 Error_0 ("undefined sharp expression");
5183 else 5453
5184 s_return (x); 5454 s_return (x);
5185 5455
5186 default: 5456 default:
5187 Error_0 ("syntax error: illegal token"); 5457 Error_0 ("syntax error: illegal token");
5188 } 5458 }
5189 5459
5190 break; 5460 break;
5191 5461
5192 case OP_RDLIST: 5462 case OP_RDLIST:
5193 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 5463 SCHEME_V->args = cons (SCHEME_V->value, args);
5194 SCHEME_V->tok = token (SCHEME_A); 5464 SCHEME_V->tok = token (SCHEME_A);
5195 5465
5196 switch (SCHEME_V->tok) 5466 switch (SCHEME_V->tok)
5197 { 5467 {
5198 case TOK_EOF: 5468 case TOK_EOF:
5226 case OP_RDDOT: 5496 case OP_RDDOT:
5227 if (token (SCHEME_A) != TOK_RPAREN) 5497 if (token (SCHEME_A) != TOK_RPAREN)
5228 Error_0 ("syntax error: illegal dot expression"); 5498 Error_0 ("syntax error: illegal dot expression");
5229 5499
5230 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5500 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5231 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); 5501 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, args));
5232 5502
5233 case OP_RDQUOTE: 5503 case OP_RDQUOTE:
5234 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); 5504 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)));
5235 5505
5236 case OP_RDQQUOTE: 5506 case OP_RDQQUOTE:
5258 SCHEME_V->args = SCHEME_V->value; 5528 SCHEME_V->args = SCHEME_V->value;
5259 s_goto (OP_VECTOR); 5529 s_goto (OP_VECTOR);
5260 5530
5261 /* ========== printing part ========== */ 5531 /* ========== printing part ========== */
5262 case OP_P0LIST: 5532 case OP_P0LIST:
5263 if (is_vector (SCHEME_V->args)) 5533 if (is_vector (args))
5264 { 5534 {
5265 putstr (SCHEME_A_ "#("); 5535 putstr (SCHEME_A_ "#(");
5266 SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); 5536 SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0));
5267 s_goto (OP_PVECFROM); 5537 s_goto (OP_PVECFROM);
5268 } 5538 }
5269 else if (is_environment (SCHEME_V->args)) 5539 else if (is_environment (args))
5270 { 5540 {
5271 putstr (SCHEME_A_ "#<ENVIRONMENT>"); 5541 putstr (SCHEME_A_ "#<ENVIRONMENT>");
5272 s_return (S_T); 5542 s_return (S_T);
5273 } 5543 }
5274 else if (!is_pair (SCHEME_V->args)) 5544 else if (!is_pair (args))
5275 { 5545 {
5276 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5546 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5277 s_return (S_T); 5547 s_return (S_T);
5278 }
5279 else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args)))
5280 {
5281 putstr (SCHEME_A_ "'");
5282 SCHEME_V->args = cadr (SCHEME_V->args);
5283 s_goto (OP_P0LIST);
5284 }
5285 else if (car (SCHEME_V->args) == SCHEME_V->QQUOTE && ok_abbrev (cdr (SCHEME_V->args)))
5286 {
5287 putstr (SCHEME_A_ "`");
5288 SCHEME_V->args = cadr (SCHEME_V->args);
5289 s_goto (OP_P0LIST);
5290 }
5291 else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTE && ok_abbrev (cdr (SCHEME_V->args)))
5292 {
5293 putstr (SCHEME_A_ ",");
5294 SCHEME_V->args = cadr (SCHEME_V->args);
5295 s_goto (OP_P0LIST);
5296 }
5297 else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTESP && ok_abbrev (cdr (SCHEME_V->args)))
5298 {
5299 putstr (SCHEME_A_ ",@");
5300 SCHEME_V->args = cadr (SCHEME_V->args);
5301 s_goto (OP_P0LIST);
5302 } 5548 }
5303 else 5549 else
5304 { 5550 {
5551 pointer a = car (args);
5552 pointer b = cdr (args);
5553 int ok_abbr = ok_abbrev (b);
5554 SCHEME_V->args = car (b);
5555
5556 if (a == SCHEME_V->QUOTE && ok_abbr)
5557 putcharacter (SCHEME_A_ '\'');
5558 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5559 putcharacter (SCHEME_A_ '`');
5560 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5561 putcharacter (SCHEME_A_ ',');
5562 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5305 putstr (SCHEME_A_ "("); 5563 putstr (SCHEME_A_ ",@");
5564 else
5565 {
5566 putcharacter (SCHEME_A_ '(');
5306 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL); 5567 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5307 SCHEME_V->args = car (SCHEME_V->args); 5568 SCHEME_V->args = a;
5569 }
5570
5308 s_goto (OP_P0LIST); 5571 s_goto (OP_P0LIST);
5309 } 5572 }
5310 5573
5311 case OP_P1LIST: 5574 case OP_P1LIST:
5312 if (is_pair (SCHEME_V->args)) 5575 if (is_pair (args))
5313 { 5576 {
5314 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL); 5577 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5315 putstr (SCHEME_A_ " "); 5578 putcharacter (SCHEME_A_ ' ');
5316 SCHEME_V->args = car (SCHEME_V->args); 5579 SCHEME_V->args = car (args);
5317 s_goto (OP_P0LIST); 5580 s_goto (OP_P0LIST);
5318 } 5581 }
5319 else if (is_vector (SCHEME_V->args)) 5582 else if (is_vector (args))
5320 { 5583 {
5321 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); 5584 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL);
5322 putstr (SCHEME_A_ " . "); 5585 putstr (SCHEME_A_ " . ");
5323 s_goto (OP_P0LIST); 5586 s_goto (OP_P0LIST);
5324 } 5587 }
5325 else 5588 else
5326 { 5589 {
5327 if (SCHEME_V->args != NIL) 5590 if (args != NIL)
5328 { 5591 {
5329 putstr (SCHEME_A_ " . "); 5592 putstr (SCHEME_A_ " . ");
5330 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5593 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5331 } 5594 }
5332 5595
5333 putstr (SCHEME_A_ ")"); 5596 putcharacter (SCHEME_A_ ')');
5334 s_return (S_T); 5597 s_return (S_T);
5335 } 5598 }
5336 5599
5337 case OP_PVECFROM: 5600 case OP_PVECFROM:
5338 { 5601 {
5339 int i = ivalue_unchecked (cdr (SCHEME_V->args)); 5602 int i = ivalue_unchecked (cdr (args));
5340 pointer vec = car (SCHEME_V->args); 5603 pointer vec = car (args);
5341 int len = veclength (vec); 5604 int len = veclength (vec);
5342 5605
5343 if (i == len) 5606 if (i == len)
5344 { 5607 {
5345 putstr (SCHEME_A_ ")"); 5608 putcharacter (SCHEME_A_ ')');
5346 s_return (S_T); 5609 s_return (S_T);
5347 } 5610 }
5348 else 5611 else
5349 { 5612 {
5350 pointer elem = vector_elem (vec, i); 5613 pointer elem = vector_get (vec, i);
5351 5614
5352 ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; 5615 ivalue_unchecked (cdr (args)) = i + 1;
5353 s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); 5616 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5354 SCHEME_V->args = elem; 5617 SCHEME_V->args = elem;
5355 5618
5356 if (i > 0) 5619 if (i > 0)
5357 putstr (SCHEME_A_ " "); 5620 putcharacter (SCHEME_A_ ' ');
5358 5621
5359 s_goto (OP_P0LIST); 5622 s_goto (OP_P0LIST);
5360 } 5623 }
5361 } 5624 }
5362 } 5625 }
5363 5626
5364 return S_T; 5627 if (USE_ERROR_CHECKING) abort ();
5365} 5628}
5366 5629
5367static pointer 5630/* list ops */
5631ecb_hot static int
5368opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5632opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5369{ 5633{
5634 pointer args = SCHEME_V->args;
5635 pointer a = car (args);
5370 pointer x, y; 5636 pointer x, y;
5371 5637
5372 switch (op) 5638 switch (op)
5373 { 5639 {
5374 case OP_LIST_LENGTH: /* length *//* a.k */ 5640 case OP_LIST_LENGTH: /* length *//* a.k */
5375 { 5641 {
5376 long v = list_length (SCHEME_A_ car (SCHEME_V->args)); 5642 long v = list_length (SCHEME_A_ a);
5377 5643
5378 if (v < 0) 5644 if (v < 0)
5379 Error_1 ("length: not a list:", car (SCHEME_V->args)); 5645 Error_1 ("length: not a list:", a);
5380 5646
5381 s_return (mk_integer (SCHEME_A_ v)); 5647 s_return (mk_integer (SCHEME_A_ v));
5382 } 5648 }
5383 5649
5384 case OP_ASSQ: /* assq *//* a.k */ 5650 case OP_ASSQ: /* assq *//* a.k */
5385 x = car (SCHEME_V->args); 5651 x = a;
5386 5652
5387 for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) 5653 for (y = cadr (args); is_pair (y); y = cdr (y))
5388 { 5654 {
5389 if (!is_pair (car (y))) 5655 if (!is_pair (car (y)))
5390 Error_0 ("unable to handle non pair element"); 5656 Error_0 ("unable to handle non pair element");
5391 5657
5392 if (x == caar (y)) 5658 if (x == caar (y))
5393 break; 5659 break;
5394 } 5660 }
5395 5661
5396 if (is_pair (y)) 5662 if (is_pair (y))
5397 s_return (car (y)); 5663 s_return (car (y));
5398 else 5664
5399 s_return (S_F); 5665 s_return (S_F);
5400
5401 5666
5402 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5667 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5403 SCHEME_V->args = car (SCHEME_V->args); 5668 SCHEME_V->args = a;
5404 5669
5405 if (SCHEME_V->args == NIL) 5670 if (SCHEME_V->args == NIL)
5406 s_return (S_F); 5671 s_return (S_F);
5407 else if (is_closure (SCHEME_V->args)) 5672 else if (is_closure (SCHEME_V->args) || is_macro (SCHEME_V->args))
5408 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5673 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5409 else if (is_macro (SCHEME_V->args)) 5674
5410 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5411 else
5412 s_return (S_F); 5675 s_return (S_F);
5413 5676
5414 case OP_CLOSUREP: /* closure? */ 5677 case OP_CLOSUREP: /* closure? */
5415 /* 5678 /*
5416 * Note, macro object is also a closure. 5679 * Note, macro object is also a closure.
5417 * Therefore, (closure? <#MACRO>) ==> #t 5680 * Therefore, (closure? <#MACRO>) ==> #t
5681 * (schmorp) well, obviously not, fix? TODO
5418 */ 5682 */
5419 s_retbool (is_closure (car (SCHEME_V->args))); 5683 s_retbool (is_closure (a));
5420 5684
5421 case OP_MACROP: /* macro? */ 5685 case OP_MACROP: /* macro? */
5422 s_retbool (is_macro (car (SCHEME_V->args))); 5686 s_retbool (is_macro (a));
5423 } 5687 }
5424 5688
5425 return S_T; /* NOTREACHED */ 5689 if (USE_ERROR_CHECKING) abort ();
5426} 5690}
5427 5691
5692/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5428typedef pointer (*dispatch_func) (SCHEME_P_ enum scheme_opcodes); 5693typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5429 5694
5430typedef int (*test_predicate) (pointer); 5695typedef int (*test_predicate)(pointer);
5431static int 5696
5697ecb_hot static int
5432is_any (pointer p) 5698tst_any (pointer p)
5433{ 5699{
5434 return 1; 5700 return 1;
5435} 5701}
5436 5702
5437static int 5703ecb_hot static int
5438is_nonneg (pointer p) 5704tst_inonneg (pointer p)
5439{ 5705{
5440 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);
5441} 5713}
5442 5714
5443/* Correspond carefully with following defines! */ 5715/* Correspond carefully with following defines! */
5444static struct 5716static struct
5445{ 5717{
5446 test_predicate fct; 5718 test_predicate fct;
5447 const char *kind; 5719 const char *kind;
5448} tests[] = 5720} tests[] = {
5449{ 5721 { tst_any , 0 },
5450 { 0, 0}, /* unused */ 5722 { is_string , "string" },
5451 { is_any, 0}, 5723 { is_symbol , "symbol" },
5452 { is_string, "string" }, 5724 { is_port , "port" },
5453 { is_symbol, "symbol" },
5454 { is_port, "port" },
5455 { is_inport, "input port" }, 5725 { is_inport , "input port" },
5456 { is_outport, "output port" }, 5726 { is_outport , "output port" },
5457 { is_environment, "environment" }, 5727 { is_environment, "environment" },
5458 { is_pair, "pair" }, 5728 { is_pair , "pair" },
5459 { 0, "pair or '()" }, 5729 { 0 , "pair or '()" },
5460 { is_character, "character" }, 5730 { is_character , "character" },
5461 { is_vector, "vector" }, 5731 { is_vector , "vector" },
5462 { is_number, "number" }, 5732 { is_number , "number" },
5463 { is_integer, "integer" }, 5733 { is_integer , "integer" },
5464 { is_nonneg, "non-negative integer" } 5734 { tst_inonneg , "non-negative integer" }
5465}; 5735};
5466 5736
5467#define TST_NONE 0 5737#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5468#define TST_ANY "\001" 5738#define TST_ANY "\001"
5469#define TST_STRING "\002" 5739#define TST_STRING "\002"
5470#define TST_SYMBOL "\003" 5740#define TST_SYMBOL "\003"
5471#define TST_PORT "\004" 5741#define TST_PORT "\004"
5472#define TST_INPORT "\005" 5742#define TST_INPORT "\005"
5473#define TST_OUTPORT "\006" 5743#define TST_OUTPORT "\006"
5474#define TST_ENVIRONMENT "\007" 5744#define TST_ENVIRONMENT "\007"
5475#define TST_PAIR "\010" 5745#define TST_PAIR "\010"
5476#define TST_LIST "\011" 5746#define TST_LIST "\011"
5477#define TST_CHAR "\012" 5747#define TST_CHAR "\012"
5478#define TST_VECTOR "\013" 5748#define TST_VECTOR "\013"
5479#define TST_NUMBER "\014" 5749#define TST_NUMBER "\014"
5480#define TST_INTEGER "\015" 5750#define TST_INTEGER "\015"
5481#define TST_NATURAL "\016" 5751#define TST_NATURAL "\016"
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}
5482 5779
5483typedef struct 5780typedef struct
5484{ 5781{
5485 dispatch_func func; 5782 uint8_t func;
5486 char *name; 5783 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5784 uint8_t builtin;
5785#if USE_ERROR_CHECKING
5487 int min_arity; 5786 uint8_t min_arity;
5488 int max_arity; 5787 uint8_t max_arity;
5489 char *arg_tests_encoding; 5788 char arg_tests_encoding[3];
5789#endif
5490} op_code_info; 5790} op_code_info;
5491 5791
5492#define INF_ARG 0xffff
5493
5494static op_code_info dispatch_table[] = { 5792static const op_code_info dispatch_table[] = {
5495#define OP_DEF(A,B,C,D,E,OP) {A,B,C,D,E}, 5793#if USE_ERROR_CHECKING
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
5496#include "opdefines.h" 5798#include "opdefines.h"
5799#undef OP_DEF
5497 {0} 5800 {0}
5498}; 5801};
5499 5802
5500static const char *
5501procname (pointer x)
5502{
5503 int n = procnum (x);
5504 const char *name = dispatch_table[n].name;
5505
5506 if (name == 0)
5507 name = "ILLEGAL!";
5508
5509 return name;
5510}
5511
5512/* kernel of this interpreter */ 5803/* kernel of this interpreter */
5513static void 5804ecb_hot static void
5514Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5805Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5515{ 5806{
5516 SCHEME_V->op = op; 5807 SCHEME_V->op = op;
5517 5808
5518 for (;;) 5809 for (;;)
5519 { 5810 {
5520 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5811 const op_code_info *pcd = dispatch_table + SCHEME_V->op;
5521 5812
5522#if USE_ERROR_CHECKING 5813#if USE_ERROR_CHECKING
5523 if (pcd->name) /* if built-in function, check arguments */ 5814 if (pcd->builtin) /* if built-in function, check arguments */
5524 { 5815 {
5525 int ok = 1;
5526 char msg[STRBUFFSIZE]; 5816 char msg[STRBUFFSIZE];
5527 int n = list_length (SCHEME_A_ SCHEME_V->args); 5817 int n = list_length (SCHEME_A_ SCHEME_V->args);
5528 5818
5529 /* Check number of arguments */ 5819 /* Check number of arguments */
5530 if (ecb_expect_false (n < pcd->min_arity)) 5820 if (ecb_expect_false (n < pcd->min_arity))
5531 { 5821 {
5532 ok = 0;
5533 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5822 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5534 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;
5535 } 5826 }
5536 else if (ecb_excpect_false (n > pcd->max_arity)) 5827 else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG))
5537 { 5828 {
5538 ok = 0;
5539 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5829 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5540 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;
5541 } 5833 }
5542 5834 else
5543 if (ecb_expect_false (ok))
5544 { 5835 {
5545 if (pcd->arg_tests_encoding) 5836 if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */
5546 { 5837 {
5547 int i = 0; 5838 int i = 0;
5548 int j; 5839 int j;
5549 const char *t = pcd->arg_tests_encoding; 5840 const char *t = pcd->arg_tests_encoding;
5550 pointer arglist = SCHEME_V->args; 5841 pointer arglist = SCHEME_V->args;
5551 5842
5552 do 5843 do
5553 { 5844 {
5554 pointer arg = car (arglist); 5845 pointer arg = car (arglist);
5555 5846
5556 j = (int) t[0]; 5847 j = t[0];
5557 5848
5849 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5558 if (j == TST_LIST[0]) 5850 if (j == TST_LIST[0])
5559 { 5851 {
5560 if (arg != NIL && !is_pair (arg)) 5852 if (!tst_is_list (SCHEME_A_ arg))
5561 break; 5853 break;
5562 } 5854 }
5563 else 5855 else
5564 { 5856 {
5565 if (!tests[j].fct (arg)) 5857 if (!tests[j - 1].fct (arg))
5566 break; 5858 break;
5567 } 5859 }
5568 5860
5569 if (t[1] != 0) /* 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 */
5570 t++; 5862 t++;
5571 5863
5572 arglist = cdr (arglist); 5864 arglist = cdr (arglist);
5573 i++; 5865 i++;
5574 } 5866 }
5575 while (i < n); 5867 while (i < n);
5576 5868
5577 if (i < n) 5869 if (i < n)
5578 { 5870 {
5579 ok = 0;
5580 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;
5581 } 5874 }
5582 } 5875 }
5583 } 5876 }
5584
5585 if (!ok)
5586 {
5587 if (xError_1 (SCHEME_A_ msg, 0) == NIL)
5588 return;
5589
5590 pcd = dispatch_table + SCHEME_V->op;
5591 }
5592 } 5877 }
5593#endif 5878#endif
5594 5879
5595 ok_to_freely_gc (SCHEME_A); 5880 ok_to_freely_gc (SCHEME_A);
5596 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
5597 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))
5598 return; 5893 return;
5599 5894
5600 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5895 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5601 { 5896 {
5602 xwrstr ("No memory!\n"); 5897 putstr (SCHEME_A_ "No memory!\n");
5603 return; 5898 return;
5604 } 5899 }
5605 } 5900 }
5606} 5901}
5607 5902
5608/* ========== Initialization of internal keywords ========== */ 5903/* ========== Initialization of internal keywords ========== */
5609 5904
5610static void 5905ecb_cold static void
5611assign_syntax (SCHEME_P_ const char *name) 5906assign_syntax (SCHEME_P_ const char *name)
5612{ 5907{
5613 pointer x = oblist_add_by_name (SCHEME_A_ name); 5908 pointer x = oblist_add_by_name (SCHEME_A_ name);
5614 set_typeflag (x, typeflag (x) | T_SYNTAX); 5909 set_typeflag (x, typeflag (x) | T_SYNTAX);
5615} 5910}
5616 5911
5617static void 5912ecb_cold static void
5618assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) 5913assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
5619{ 5914{
5620 pointer x = mk_symbol (SCHEME_A_ name); 5915 pointer x = mk_symbol (SCHEME_A_ name);
5621 pointer y = mk_proc (SCHEME_A_ op); 5916 pointer y = mk_proc (SCHEME_A_ op);
5622 new_slot_in_env (SCHEME_A_ x, y); 5917 new_slot_in_env (SCHEME_A_ x, y);
5626mk_proc (SCHEME_P_ enum scheme_opcodes op) 5921mk_proc (SCHEME_P_ enum scheme_opcodes op)
5627{ 5922{
5628 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5923 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5629 set_typeflag (y, (T_PROC | T_ATOM)); 5924 set_typeflag (y, (T_PROC | T_ATOM));
5630 ivalue_unchecked (y) = op; 5925 ivalue_unchecked (y) = op;
5631 set_num_integer (y);
5632 return y; 5926 return y;
5633} 5927}
5634 5928
5635/* 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! */
5636static int 5930ecb_hot static int
5637syntaxnum (pointer p) 5931syntaxnum (pointer p)
5638{ 5932{
5639 const char *s = strvalue (car (p)); 5933 const char *s = strvalue (p);
5640 5934
5641 switch (strlength (car (p))) 5935 switch (strlength (p))
5642 { 5936 {
5643 case 2: 5937 case 2:
5644 if (s[0] == 'i') 5938 if (s[0] == 'i')
5645 return OP_IF0; /* if */ 5939 return OP_IF0; /* if */
5646 else 5940 else
5701 return OP_C0STREAM; /* cons-stream */ 5995 return OP_C0STREAM; /* cons-stream */
5702 } 5996 }
5703} 5997}
5704 5998
5705#if USE_MULTIPLICITY 5999#if USE_MULTIPLICITY
5706scheme * 6000ecb_cold scheme *
5707scheme_init_new () 6001scheme_init_new ()
5708{ 6002{
5709 scheme *sc = malloc (sizeof (scheme)); 6003 scheme *sc = malloc (sizeof (scheme));
5710 6004
5711 if (!scheme_init (SCHEME_A)) 6005 if (!scheme_init (SCHEME_A))
5716 else 6010 else
5717 return sc; 6011 return sc;
5718} 6012}
5719#endif 6013#endif
5720 6014
5721int 6015ecb_cold int
5722scheme_init (SCHEME_P) 6016scheme_init (SCHEME_P)
5723{ 6017{
5724 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 6018 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5725 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));
5726 6025
5727 num_set_fixnum (num_zero, 1); 6026 num_set_fixnum (num_zero, 1);
5728 num_set_ivalue (num_zero, 0); 6027 num_set_ivalue (num_zero, 0);
5729 num_set_fixnum (num_one, 1); 6028 num_set_fixnum (num_one, 1);
5730 num_set_ivalue (num_one, 1); 6029 num_set_ivalue (num_one, 1);
5742 SCHEME_V->save_inport = NIL; 6041 SCHEME_V->save_inport = NIL;
5743 SCHEME_V->loadport = NIL; 6042 SCHEME_V->loadport = NIL;
5744 SCHEME_V->nesting = 0; 6043 SCHEME_V->nesting = 0;
5745 SCHEME_V->interactive_repl = 0; 6044 SCHEME_V->interactive_repl = 0;
5746 6045
5747 if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) 6046 if (!alloc_cellseg (SCHEME_A))
5748 { 6047 {
5749#if USE_ERROR_CHECKING 6048#if USE_ERROR_CHECKING
5750 SCHEME_V->no_memory = 1; 6049 SCHEME_V->no_memory = 1;
5751 return 0; 6050 return 0;
5752#endif 6051#endif
5753 } 6052 }
5754 6053
5755 SCHEME_V->gc_verbose = 0; 6054 SCHEME_V->gc_verbose = 0;
5756 dump_stack_initialize (SCHEME_A); 6055 dump_stack_initialize (SCHEME_A);
5757 SCHEME_V->code = NIL; 6056 SCHEME_V->code = NIL;
5758 SCHEME_V->args = NIL; 6057 SCHEME_V->args = NIL;
5759 SCHEME_V->envir = NIL; 6058 SCHEME_V->envir = NIL;
6059 SCHEME_V->value = NIL;
5760 SCHEME_V->tracing = 0; 6060 SCHEME_V->tracing = 0;
5761 6061
5762 /* init NIL */ 6062 /* init NIL */
5763 set_typeflag (NIL, T_ATOM | T_MARK); 6063 set_typeflag (NIL, T_SPECIAL | T_ATOM);
5764 set_car (NIL, NIL); 6064 set_car (NIL, NIL);
5765 set_cdr (NIL, NIL); 6065 set_cdr (NIL, NIL);
5766 /* init T */ 6066 /* init T */
5767 set_typeflag (S_T, T_ATOM | T_MARK); 6067 set_typeflag (S_T, T_SPECIAL | T_ATOM);
5768 set_car (S_T, S_T); 6068 set_car (S_T, S_T);
5769 set_cdr (S_T, S_T); 6069 set_cdr (S_T, S_T);
5770 /* init F */ 6070 /* init F */
5771 set_typeflag (S_F, T_ATOM | T_MARK); 6071 set_typeflag (S_F, T_SPECIAL | T_ATOM);
5772 set_car (S_F, S_F); 6072 set_car (S_F, S_F);
5773 set_cdr (S_F, S_F); 6073 set_cdr (S_F, S_F);
5774 /* init EOF_OBJ */ 6074 /* init EOF_OBJ */
5775 set_typeflag (S_EOF, T_ATOM | T_MARK); 6075 set_typeflag (S_EOF, T_SPECIAL | T_ATOM);
5776 set_car (S_EOF, S_EOF); 6076 set_car (S_EOF, S_EOF);
5777 set_cdr (S_EOF, S_EOF); 6077 set_cdr (S_EOF, S_EOF);
5778 /* init sink */ 6078 /* init sink */
5779 set_typeflag (S_SINK, T_PAIR | T_MARK); 6079 set_typeflag (S_SINK, T_PAIR);
5780 set_car (S_SINK, NIL); 6080 set_car (S_SINK, NIL);
5781 6081
5782 /* init c_nest */ 6082 /* init c_nest */
5783 SCHEME_V->c_nest = NIL; 6083 SCHEME_V->c_nest = NIL;
5784 6084
5785 SCHEME_V->oblist = oblist_initial_value (SCHEME_A); 6085 SCHEME_V->oblist = oblist_initial_value (SCHEME_A);
5786 /* init global_env */ 6086 /* init global_env */
5787 new_frame_in_env (SCHEME_A_ NIL); 6087 new_frame_in_env (SCHEME_A_ NIL);
5788 SCHEME_V->global_env = SCHEME_V->envir; 6088 SCHEME_V->global_env = SCHEME_V->envir;
5789 /* init else */ 6089 /* init else */
5790 x = mk_symbol (SCHEME_A_ "else"); 6090 new_slot_in_env (SCHEME_A_ mk_symbol (SCHEME_A_ "else"), S_T);
5791 new_slot_in_env (SCHEME_A_ x, S_T);
5792 6091
5793 { 6092 {
5794 static const char *syntax_names[] = { 6093 static const char *syntax_names[] = {
5795 "lambda", "quote", "define", "if", "begin", "set!", 6094 "lambda", "quote", "define", "if", "begin", "set!",
5796 "let", "let*", "letrec", "cond", "delay", "and", 6095 "let", "let*", "letrec", "cond", "delay", "and",
5799 6098
5800 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) 6099 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
5801 assign_syntax (SCHEME_A_ syntax_names[i]); 6100 assign_syntax (SCHEME_A_ syntax_names[i]);
5802 } 6101 }
5803 6102
6103 // TODO: should iterate via strlen, to avoid n² complexity
5804 for (i = 0; i < n; i++) 6104 for (i = 0; i < n; i++)
5805 if (dispatch_table[i].name != 0) 6105 if (dispatch_table[i].builtin)
5806 assign_proc (SCHEME_A_ i, dispatch_table[i].name); 6106 assign_proc (SCHEME_A_ i, opname (i));
5807 6107
5808 /* initialization of global pointers to special symbols */ 6108 /* initialization of global pointers to special symbols */
5809 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 6109 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
5810 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 6110 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
5811 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 6111 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
5819 6119
5820 return !SCHEME_V->no_memory; 6120 return !SCHEME_V->no_memory;
5821} 6121}
5822 6122
5823#if USE_PORTS 6123#if USE_PORTS
5824void 6124ecb_cold void
5825scheme_set_input_port_file (SCHEME_P_ int fin) 6125scheme_set_input_port_file (SCHEME_P_ int fin)
5826{ 6126{
5827 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); 6127 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input);
5828} 6128}
5829 6129
5830void 6130ecb_cold void
5831scheme_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)
5832{ 6132{
5833 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);
5834} 6134}
5835 6135
5836void 6136ecb_cold void
5837scheme_set_output_port_file (SCHEME_P_ int fout) 6137scheme_set_output_port_file (SCHEME_P_ int fout)
5838{ 6138{
5839 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); 6139 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output);
5840} 6140}
5841 6141
5842void 6142ecb_cold void
5843scheme_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)
5844{ 6144{
5845 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);
5846} 6146}
5847#endif 6147#endif
5848 6148
5849void 6149ecb_cold void
5850scheme_set_external_data (SCHEME_P_ void *p) 6150scheme_set_external_data (SCHEME_P_ void *p)
5851{ 6151{
5852 SCHEME_V->ext_data = p; 6152 SCHEME_V->ext_data = p;
5853} 6153}
5854 6154
5855void 6155ecb_cold void
5856scheme_deinit (SCHEME_P) 6156scheme_deinit (SCHEME_P)
5857{ 6157{
5858 int i; 6158 int i;
5859 6159
5860#if SHOW_ERROR_LINE 6160#if SHOW_ERROR_LINE
5886 SCHEME_V->loadport = NIL; 6186 SCHEME_V->loadport = NIL;
5887 SCHEME_V->gc_verbose = 0; 6187 SCHEME_V->gc_verbose = 0;
5888 gc (SCHEME_A_ NIL, NIL); 6188 gc (SCHEME_A_ NIL, NIL);
5889 6189
5890 for (i = 0; i <= SCHEME_V->last_cell_seg; i++) 6190 for (i = 0; i <= SCHEME_V->last_cell_seg; i++)
5891 free (SCHEME_V->alloc_seg[i]); 6191 free (SCHEME_V->cell_seg[i]);
5892 6192
5893#if SHOW_ERROR_LINE 6193#if SHOW_ERROR_LINE
5894 for (i = 0; i <= SCHEME_V->file_i; i++) 6194 for (i = 0; i <= SCHEME_V->file_i; i++)
5895 {
5896 if (SCHEME_V->load_stack[i].kind & port_file) 6195 if (SCHEME_V->load_stack[i].kind & port_file)
5897 { 6196 {
5898 fname = SCHEME_V->load_stack[i].rep.stdio.filename; 6197 fname = SCHEME_V->load_stack[i].rep.stdio.filename;
5899 6198
5900 if (fname) 6199 if (fname)
5901 free (fname); 6200 free (fname);
5902 } 6201 }
5903 }
5904#endif 6202#endif
5905} 6203}
5906 6204
5907void 6205ecb_cold void
5908scheme_load_file (SCHEME_P_ int fin) 6206scheme_load_file (SCHEME_P_ int fin)
5909{ 6207{
5910 scheme_load_named_file (SCHEME_A_ fin, 0); 6208 scheme_load_named_file (SCHEME_A_ fin, 0);
5911} 6209}
5912 6210
5913void 6211ecb_cold void
5914scheme_load_named_file (SCHEME_P_ int fin, const char *filename) 6212scheme_load_named_file (SCHEME_P_ int fin, const char *filename)
5915{ 6213{
5916 dump_stack_reset (SCHEME_A); 6214 dump_stack_reset (SCHEME_A);
5917 SCHEME_V->envir = SCHEME_V->global_env; 6215 SCHEME_V->envir = SCHEME_V->global_env;
5918 SCHEME_V->file_i = 0; 6216 SCHEME_V->file_i = 0;
5919 SCHEME_V->load_stack[0].unget = -1; 6217 SCHEME_V->load_stack[0].unget = -1;
5920 SCHEME_V->load_stack[0].kind = port_input | port_file; 6218 SCHEME_V->load_stack[0].kind = port_input | port_file;
5921 SCHEME_V->load_stack[0].rep.stdio.file = fin; 6219 SCHEME_V->load_stack[0].rep.stdio.file = fin;
5922#if USE_PORTS
5923 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6220 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5924#endif
5925 SCHEME_V->retcode = 0; 6221 SCHEME_V->retcode = 0;
5926 6222
5927#if USE_PORTS
5928 if (fin == STDIN_FILENO) 6223 if (fin == STDIN_FILENO)
5929 SCHEME_V->interactive_repl = 1; 6224 SCHEME_V->interactive_repl = 1;
5930#endif
5931 6225
5932#if USE_PORTS 6226#if USE_PORTS
5933#if SHOW_ERROR_LINE 6227#if SHOW_ERROR_LINE
5934 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; 6228 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0;
5935 6229
5939#endif 6233#endif
5940 6234
5941 SCHEME_V->inport = SCHEME_V->loadport; 6235 SCHEME_V->inport = SCHEME_V->loadport;
5942 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6236 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5943 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6237 Eval_Cycle (SCHEME_A_ OP_T0LVL);
6238
5944 set_typeflag (SCHEME_V->loadport, T_ATOM); 6239 set_typeflag (SCHEME_V->loadport, T_ATOM);
5945 6240
5946 if (SCHEME_V->retcode == 0) 6241 if (SCHEME_V->retcode == 0)
5947 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6242 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5948} 6243}
5949 6244
5950void 6245ecb_cold void
5951scheme_load_string (SCHEME_P_ const char *cmd) 6246scheme_load_string (SCHEME_P_ const char *cmd)
5952{ 6247{
6248#if USE_PORTs
5953 dump_stack_reset (SCHEME_A); 6249 dump_stack_reset (SCHEME_A);
5954 SCHEME_V->envir = SCHEME_V->global_env; 6250 SCHEME_V->envir = SCHEME_V->global_env;
5955 SCHEME_V->file_i = 0; 6251 SCHEME_V->file_i = 0;
5956 SCHEME_V->load_stack[0].kind = port_input | port_string; 6252 SCHEME_V->load_stack[0].kind = port_input | port_string;
5957 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 */
5958 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);
5959 SCHEME_V->load_stack[0].rep.string.curr = (char *) cmd; 6255 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
5960#if USE_PORTS
5961 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6256 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5962#endif
5963 SCHEME_V->retcode = 0; 6257 SCHEME_V->retcode = 0;
5964 SCHEME_V->interactive_repl = 0; 6258 SCHEME_V->interactive_repl = 0;
5965 SCHEME_V->inport = SCHEME_V->loadport; 6259 SCHEME_V->inport = SCHEME_V->loadport;
5966 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6260 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5967 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6261 Eval_Cycle (SCHEME_A_ OP_T0LVL);
5968 set_typeflag (SCHEME_V->loadport, T_ATOM); 6262 set_typeflag (SCHEME_V->loadport, T_ATOM);
5969 6263
5970 if (SCHEME_V->retcode == 0) 6264 if (SCHEME_V->retcode == 0)
5971 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6265 SCHEME_V->retcode = SCHEME_V->nesting != 0;
6266#else
6267 abort ();
6268#endif
5972} 6269}
5973 6270
5974void 6271ecb_cold void
5975scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) 6272scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value)
5976{ 6273{
5977 pointer x; 6274 pointer x;
5978 6275
5979 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 6276 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
5984 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 6281 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
5985} 6282}
5986 6283
5987#if !STANDALONE 6284#if !STANDALONE
5988 6285
5989void 6286ecb_cold void
5990scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 6287scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
5991{ 6288{
5992 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));
5993} 6290}
5994 6291
5995void 6292ecb_cold void
5996scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 6293scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
5997{ 6294{
5998 int i; 6295 int i;
5999 6296
6000 for (i = 0; i < count; i++) 6297 for (i = 0; i < count; i++)
6001 scheme_register_foreign_func (SCHEME_A_ list + i); 6298 scheme_register_foreign_func (SCHEME_A_ list + i);
6002} 6299}
6003 6300
6004pointer 6301ecb_cold pointer
6005scheme_apply0 (SCHEME_P_ const char *procname) 6302scheme_apply0 (SCHEME_P_ const char *procname)
6006{ 6303{
6007 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));
6008} 6305}
6009 6306
6010void 6307ecb_cold void
6011save_from_C_call (SCHEME_P) 6308save_from_C_call (SCHEME_P)
6012{ 6309{
6013 pointer saved_data = cons (car (S_SINK), 6310 pointer saved_data = cons (car (S_SINK),
6014 cons (SCHEME_V->envir, 6311 cons (SCHEME_V->envir,
6015 SCHEME_V->dump)); 6312 SCHEME_V->dump));
6019 /* 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
6020 directly resume pre-C-call operations. */ 6317 directly resume pre-C-call operations. */
6021 dump_stack_reset (SCHEME_A); 6318 dump_stack_reset (SCHEME_A);
6022} 6319}
6023 6320
6024void 6321ecb_cold void
6025restore_from_C_call (SCHEME_P) 6322restore_from_C_call (SCHEME_P)
6026{ 6323{
6027 set_car (S_SINK, caar (SCHEME_V->c_nest)); 6324 set_car (S_SINK, caar (SCHEME_V->c_nest));
6028 SCHEME_V->envir = cadar (SCHEME_V->c_nest); 6325 SCHEME_V->envir = cadar (SCHEME_V->c_nest);
6029 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); 6326 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest));
6030 /* Pop */ 6327 /* Pop */
6031 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); 6328 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest);
6032} 6329}
6033 6330
6034/* "func" and "args" are assumed to be already eval'ed. */ 6331/* "func" and "args" are assumed to be already eval'ed. */
6035pointer 6332ecb_cold pointer
6036scheme_call (SCHEME_P_ pointer func, pointer args) 6333scheme_call (SCHEME_P_ pointer func, pointer args)
6037{ 6334{
6038 int old_repl = SCHEME_V->interactive_repl; 6335 int old_repl = SCHEME_V->interactive_repl;
6039 6336
6040 SCHEME_V->interactive_repl = 0; 6337 SCHEME_V->interactive_repl = 0;
6047 SCHEME_V->interactive_repl = old_repl; 6344 SCHEME_V->interactive_repl = old_repl;
6048 restore_from_C_call (SCHEME_A); 6345 restore_from_C_call (SCHEME_A);
6049 return SCHEME_V->value; 6346 return SCHEME_V->value;
6050} 6347}
6051 6348
6052pointer 6349ecb_cold pointer
6053scheme_eval (SCHEME_P_ pointer obj) 6350scheme_eval (SCHEME_P_ pointer obj)
6054{ 6351{
6055 int old_repl = SCHEME_V->interactive_repl; 6352 int old_repl = SCHEME_V->interactive_repl;
6056 6353
6057 SCHEME_V->interactive_repl = 0; 6354 SCHEME_V->interactive_repl = 0;
6069 6366
6070/* ========== Main ========== */ 6367/* ========== Main ========== */
6071 6368
6072#if STANDALONE 6369#if STANDALONE
6073 6370
6074int 6371ecb_cold int
6075main (int argc, char **argv) 6372main (int argc, char **argv)
6076{ 6373{
6077# if USE_MULTIPLICITY 6374# if USE_MULTIPLICITY
6078 scheme ssc; 6375 scheme ssc;
6079 scheme *const SCHEME_V = &ssc; 6376 scheme *const SCHEME_V = &ssc;
6081# endif 6378# endif
6082 int fin; 6379 int fin;
6083 char *file_name = InitFile; 6380 char *file_name = InitFile;
6084 int retcode; 6381 int retcode;
6085 int isfile = 1; 6382 int isfile = 1;
6383#if EXPERIMENT
6384 system ("ps v $PPID");
6385#endif
6086 6386
6087 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6387 if (argc == 2 && strcmp (argv[1], "-?") == 0)
6088 { 6388 {
6089 xwrstr ("Usage: tinyscheme -?\n"); 6389 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
6090 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6390 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
6091 xwrstr ("followed by\n"); 6391 putstr (SCHEME_A_ "followed by\n");
6092 xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); 6392 putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n");
6093 xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); 6393 putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n");
6094 xwrstr ("assuming that the executable is named tinyscheme.\n"); 6394 putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n");
6095 xwrstr ("Use - as filename for stdin.\n"); 6395 putstr (SCHEME_A_ "Use - as filename for stdin.\n");
6096 return 1; 6396 return 1;
6097 } 6397 }
6098 6398
6099 if (!scheme_init (SCHEME_A)) 6399 if (!scheme_init (SCHEME_A))
6100 { 6400 {
6101 xwrstr ("Could not initialize!\n"); 6401 putstr (SCHEME_A_ "Could not initialize!\n");
6102 return 2; 6402 return 2;
6103 } 6403 }
6104 6404
6105# if USE_PORTS 6405# if USE_PORTS
6106 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); 6406 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO);
6119 } 6419 }
6120#endif 6420#endif
6121 6421
6122 do 6422 do
6123 { 6423 {
6124#if USE_PORTS
6125 if (strcmp (file_name, "-") == 0) 6424 if (strcmp (file_name, "-") == 0)
6126 fin = STDIN_FILENO; 6425 fin = STDIN_FILENO;
6127 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)
6128 { 6427 {
6129 pointer args = NIL; 6428 pointer args = NIL;
6147 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);
6148 6447
6149 } 6448 }
6150 else 6449 else
6151 fin = open (file_name, O_RDONLY); 6450 fin = open (file_name, O_RDONLY);
6152#endif
6153 6451
6154 if (isfile && fin < 0) 6452 if (isfile && fin < 0)
6155 { 6453 {
6156 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');
6157 } 6457 }
6158 else 6458 else
6159 { 6459 {
6160 if (isfile) 6460 if (isfile)
6161 scheme_load_named_file (SCHEME_A_ fin, file_name); 6461 scheme_load_named_file (SCHEME_A_ fin, file_name);
6162 else 6462 else
6163 scheme_load_string (SCHEME_A_ file_name); 6463 scheme_load_string (SCHEME_A_ file_name);
6164 6464
6165#if USE_PORTS
6166 if (!isfile || fin != STDIN_FILENO) 6465 if (!isfile || fin != STDIN_FILENO)
6167 { 6466 {
6168 if (SCHEME_V->retcode != 0) 6467 if (SCHEME_V->retcode != 0)
6169 { 6468 {
6170 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');
6171 } 6472 }
6172 6473
6173 if (isfile) 6474 if (isfile)
6174 close (fin); 6475 close (fin);
6175 } 6476 }
6176#endif
6177 } 6477 }
6178 6478
6179 file_name = *argv++; 6479 file_name = *argv++;
6180 } 6480 }
6181 while (file_name != 0); 6481 while (file_name != 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines