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

Comparing cvsroot/microscheme/scheme.c (file contents):
Revision 1.10 by root, Thu Nov 26 00:03:19 2015 UTC vs.
Revision 1.68 by root, Mon Dec 7 21:12:56 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines