ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/microscheme/scheme.c
(Generate patch)

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines