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

Comparing microscheme/scheme.c (file contents):
Revision 1.14 by root, Thu Nov 26 08:56:32 2015 UTC vs.
Revision 1.69 by root, Mon Dec 7 22:12:53 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines