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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines