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

Comparing microscheme/scheme.c (file contents):
Revision 1.12 by root, Thu Nov 26 07:30:25 2015 UTC vs.
Revision 1.56 by root, Tue Dec 1 03:44:32 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 EXPERIMENT 1
22
23#if 1
21#define PAGE_SIZE 4096 /* does not work on sparc/alpha */ 24#define PAGE_SIZE 4096 /* does not work on sparc/alpha */
22#include "malloc.c" 25#include "malloc.c"
26#endif
23 27
24#define SCHEME_SOURCE 28#define SCHEME_SOURCE
25#include "scheme-private.h" 29#include "scheme-private.h"
26#ifndef WIN32 30#ifndef WIN32
27# include <unistd.h> 31# include <unistd.h>
28#endif 32#endif
29#if USE_MATH 33#if USE_MATH
30# include <math.h> 34# include <math.h>
31#endif 35#endif
32 36
37#include "ecb.h"
38
33#include <sys/types.h> 39#include <sys/types.h>
34#include <sys/stat.h> 40#include <sys/stat.h>
35#include <fcntl.h> 41#include <fcntl.h>
36 42
43#if !USE_ERROR_CHECKING
44# define NDEBUG
45#endif
46
47#include <assert.h>
48#include <stdlib.h>
37#include <string.h> 49#include <string.h>
38#include <stdlib.h>
39 50
40#include <limits.h> 51#include <limits.h>
41#include <inttypes.h> 52#include <inttypes.h>
42#include <float.h> 53#include <float.h>
43//#include <ctype.h> 54//#include <ctype.h>
55
56#if '1' != '0' + 1 \
57 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \
58 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \
59 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \
60 || 'f' != 'a' + 5
61# error "execution character set digits not consecutive"
62#endif
44 63
45enum { 64enum {
46 TOK_EOF, 65 TOK_EOF,
47 TOK_LPAREN, 66 TOK_LPAREN,
48 TOK_RPAREN, 67 TOK_RPAREN,
49 TOK_DOT, 68 TOK_DOT,
50 TOK_ATOM, 69 TOK_ATOM,
70 TOK_DOTATOM, /* atom name starting with '.' */
71 TOK_STRATOM, /* atom name enclosed in | */
51 TOK_QUOTE, 72 TOK_QUOTE,
52 TOK_DQUOTE, 73 TOK_DQUOTE,
53 TOK_BQUOTE, 74 TOK_BQUOTE,
54 TOK_COMMA, 75 TOK_COMMA,
55 TOK_ATMARK, 76 TOK_ATMARK,
57 TOK_SHARP_CONST, 78 TOK_SHARP_CONST,
58 TOK_VEC 79 TOK_VEC
59}; 80};
60 81
61#define BACKQUOTE '`' 82#define BACKQUOTE '`'
62#define DELIMITERS "()\";\f\t\v\n\r " 83#define WHITESPACE " \t\r\n\v\f"
84#define DELIMITERS "()\";" WHITESPACE
63 85
64#define NIL (&SCHEME_V->xNIL) //TODO: make this 0? 86#define NIL POINTER (&SCHEME_V->xNIL)
65#define S_T (&SCHEME_V->xT) //TODO: magic ptr value? 87#define S_T POINTER (&SCHEME_V->xT)
66#define S_F (&SCHEME_V->xF) //TODO: magic ptr value? 88#define S_F POINTER (&SCHEME_V->xF)
67#define S_SINK (&SCHEME_V->xsink) 89#define S_SINK POINTER (&SCHEME_V->xsink)
68#define S_EOF (&SCHEME_V->xEOF_OBJ) 90#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 91
77#if !USE_MULTIPLICITY 92#if !USE_MULTIPLICITY
78static scheme sc; 93static scheme sc;
79#endif 94#endif
80 95
88 } 103 }
89 104
90 char *p = s; 105 char *p = s;
91 106
92 do { 107 do {
93 *p++ = '0' + n % base; 108 *p++ = "0123456789abcdef"[n % base];
94 n /= base; 109 n /= base;
95 } while (n); 110 } while (n);
96 111
97 *p-- = 0; 112 *p-- = 0;
98 113
108{ 123{
109 xbase (s, n, 10); 124 xbase (s, n, 10);
110} 125}
111 126
112static void 127static void
113xwrstr (const char *s) 128putnum (SCHEME_P_ long n)
114{
115 write (1, s, strlen (s));
116}
117
118static void
119xwrnum (long n)
120{ 129{
121 char buf[64]; 130 char buf[64];
122 131
123 xnum (buf, n); 132 xnum (buf, n);
124 xwrstr (buf); 133 putstr (SCHEME_A_ buf);
125} 134}
126 135
127static char 136static char
128xtoupper (char c) 137xtoupper (char c)
129{ 138{
150 159
151#define toupper(c) xtoupper (c) 160#define toupper(c) xtoupper (c)
152#define tolower(c) xtolower (c) 161#define tolower(c) xtolower (c)
153#define isdigit(c) xisdigit (c) 162#define isdigit(c) xisdigit (c)
154 163
155#if USE_STRLWR 164#if USE_IGNORECASE
156static const char * 165static const char *
157strlwr (char *s) 166xstrlwr (char *s)
158{ 167{
159 const char *p = s; 168 const char *p = s;
160 169
161 while (*s) 170 while (*s)
162 { 171 {
164 s++; 173 s++;
165 } 174 }
166 175
167 return p; 176 return p;
168} 177}
169#endif
170 178
179#define stricmp(a,b) strcasecmp (a, b)
180#define strlwr(s) xstrlwr (s)
181
182#else
171#define stricmp(a,b) strcmp (a, b) 183# define stricmp(a,b) strcmp (a, b)
172#define strlwr(s) (s) 184# define strlwr(s) (s)
185#endif
173 186
174#ifndef prompt 187#ifndef prompt
175# define prompt "ts> " 188# define prompt "ts> "
176#endif 189#endif
177 190
178#ifndef InitFile 191#ifndef InitFile
179# define InitFile "init.scm" 192# define InitFile "init.scm"
180#endif 193#endif
181 194
182#ifndef FIRST_CELLSEGS
183# define FIRST_CELLSEGS 3
184#endif
185
186enum scheme_types 195enum scheme_types
187{ 196{
197 T_INTEGER,
188 T_FREE, 198 T_REAL,
189 T_STRING, 199 T_STRING,
190 T_NUMBER,
191 T_SYMBOL, 200 T_SYMBOL,
192 T_PROC, 201 T_PROC,
193 T_PAIR, 202 T_PAIR, /* also used for free cells */
194 T_CLOSURE, 203 T_CLOSURE,
195 T_CONTINUATION, 204 T_CONTINUATION,
196 T_FOREIGN, 205 T_FOREIGN,
197 T_CHARACTER, 206 T_CHARACTER,
198 T_PORT, 207 T_PORT,
208#define T_SYNTAX 0x0010 217#define T_SYNTAX 0x0010
209#define T_IMMUTABLE 0x0020 218#define T_IMMUTABLE 0x0020
210#define T_ATOM 0x0040 /* only for gc */ 219#define T_ATOM 0x0040 /* only for gc */
211#define T_MARK 0x0080 /* only for gc */ 220#define T_MARK 0x0080 /* only for gc */
212 221
213static num num_add (num a, num b); 222/* num, for generic arithmetic */
214static num num_mul (num a, num b); 223struct num
215static num num_div (num a, num b); 224{
225 IVALUE ivalue;
226#if USE_REAL
227 RVALUE rvalue;
228 char is_fixnum;
229#endif
230};
231
232#if USE_REAL
233# define num_is_fixnum(n) (n).is_fixnum
234# define num_set_fixnum(n,f) (n).is_fixnum = (f)
235# define num_ivalue(n) (n).ivalue
236# define num_rvalue(n) (n).rvalue
237# define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i)
238# define num_set_rvalue(n,r) (n).rvalue = (r)
239#else
240# define num_is_fixnum(n) 1
241# define num_set_fixnum(n,f) 0
242# define num_ivalue(n) (n).ivalue
243# define num_rvalue(n) (n).ivalue
244# define num_set_ivalue(n,i) (n).ivalue = (i)
245# define num_set_rvalue(n,r) (n).ivalue = (r)
246#endif
247
248enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
249
250static num num_op (enum num_op op, num a, num b);
216static num num_intdiv (num a, num b); 251static num num_intdiv (num a, num b);
217static num num_sub (num a, num b);
218static num num_rem (num a, num b); 252static num num_rem (num a, num b);
219static num num_mod (num a, num b); 253static 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 254
226#if USE_MATH
227static double round_per_R5RS (double x);
228#endif
229static int is_zero_rvalue (RVALUE x); 255static 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 256
237static num num_zero; 257static num num_zero;
238static num num_one; 258static num num_one;
239 259
260/* convert "pointer" to cell* / cell* to pointer */
261#define CELL(p) ((struct cell *)(p) + 0)
262#define POINTER(c) ((void *)((c) - 0))
263
240/* macros for cell operations */ 264/* macros for cell operations */
241#define typeflag(p) ((p)->flag + 0) 265#define typeflag(p) (CELL(p)->flag + 0)
242#define set_typeflag(p,v) ((p)->flag = (v)) 266#define set_typeflag(p,v) (CELL(p)->flag = (v))
243#define type(p) (typeflag (p) & T_MASKTYPE) 267#define type(p) (typeflag (p) & T_MASKTYPE)
244 268
245INTERFACE INLINE int 269INTERFACE int
246is_string (pointer p) 270is_string (pointer p)
247{ 271{
248 return type (p) == T_STRING; 272 return type (p) == T_STRING;
249} 273}
250 274
251#define strvalue(p) ((p)->object.string.svalue) 275#define strvalue(p) (CELL(p)->object.string.svalue)
252#define strlength(p) ((p)->object.string.length) 276#define strlength(p) (CELL(p)->object.string.length)
253 277
254INTERFACE int is_list (SCHEME_P_ pointer p);
255INTERFACE INLINE int 278INTERFACE int
256is_vector (pointer p) 279is_vector (pointer p)
257{ 280{
258 return type (p) == T_VECTOR; 281 return type (p) == T_VECTOR;
259} 282}
260 283
261#define vecvalue(p) ((p)->object.vector.vvalue) 284#define vecvalue(p) (CELL(p)->object.vector.vvalue)
262#define veclength(p) ((p)->object.vector.length) 285#define veclength(p) (CELL(p)->object.vector.length)
263INTERFACE void fill_vector (pointer vec, pointer obj); 286INTERFACE 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); 287INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
266INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); 288INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
267 289
268INTERFACE uint32_t 290INTERFACE int
269vector_length (pointer vec) 291is_integer (pointer p)
270{ 292{
271 return vec->object.vector.length; 293 return type (p) == T_INTEGER;
272} 294}
273 295
296/* not the same as in scheme, where integers are (correctly :) reals */
274INTERFACE INLINE int 297INTERFACE int
298is_real (pointer p)
299{
300 return type (p) == T_REAL;
301}
302
303INTERFACE int
275is_number (pointer p) 304is_number (pointer p)
276{ 305{
277 return type (p) == T_NUMBER; 306 return is_integer (p) || is_real (p);
278} 307}
279 308
280INTERFACE INLINE int 309INTERFACE 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) 310is_character (pointer p)
300{ 311{
301 return type (p) == T_CHARACTER; 312 return type (p) == T_CHARACTER;
302} 313}
303 314
304INTERFACE INLINE char * 315INTERFACE char *
305string_value (pointer p) 316string_value (pointer p)
306{ 317{
307 return strvalue (p); 318 return strvalue (p);
308} 319}
309 320
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) 321#define ivalue_unchecked(p) CELL(p)->object.ivalue
322#define set_ivalue(p,v) CELL(p)->object.ivalue = (v)
323
341#if USE_REAL 324#if USE_REAL
342# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 325#define rvalue_unchecked(p) CELL(p)->object.rvalue
343# define set_num_integer(p) (p)->object.number.is_fixnum=1; 326#define set_rvalue(p,v) CELL(p)->object.rvalue = (v)
344# define set_num_real(p) (p)->object.number.is_fixnum=0;
345#else 327#else
346# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 328#define rvalue_unchecked(p) CELL(p)->object.ivalue
347# define set_num_integer(p) 0 329#define set_rvalue(p,v) CELL(p)->object.ivalue = (v)
348# define set_num_real(p) 0
349#endif 330#endif
331
350INTERFACE long 332INTERFACE long
351charvalue (pointer p) 333charvalue (pointer p)
352{ 334{
353 return ivalue_unchecked (p); 335 return ivalue_unchecked (p);
354} 336}
355 337
338#define port(p) CELL(p)->object.port
339#define set_port(p,v) port(p) = (v)
356INTERFACE INLINE int 340INTERFACE int
357is_port (pointer p) 341is_port (pointer p)
358{ 342{
359 return type (p) == T_PORT; 343 return type (p) == T_PORT;
360} 344}
361 345
362INTERFACE INLINE int 346INTERFACE int
363is_inport (pointer p) 347is_inport (pointer p)
364{ 348{
365 return is_port (p) && p->object.port->kind & port_input; 349 return is_port (p) && port (p)->kind & port_input;
366} 350}
367 351
368INTERFACE INLINE int 352INTERFACE int
369is_outport (pointer p) 353is_outport (pointer p)
370{ 354{
371 return is_port (p) && p->object.port->kind & port_output; 355 return is_port (p) && port (p)->kind & port_output;
372} 356}
373 357
374INTERFACE INLINE int 358INTERFACE int
375is_pair (pointer p) 359is_pair (pointer p)
376{ 360{
377 return type (p) == T_PAIR; 361 return type (p) == T_PAIR;
378} 362}
379 363
380#define car(p) ((p)->object.cons.car + 0) 364#define car(p) (POINTER (CELL(p)->object.cons.car))
381#define cdr(p) ((p)->object.cons.cdr + 0) 365#define cdr(p) (POINTER (CELL(p)->object.cons.cdr))
382 366
383static pointer caar (pointer p) { return car (car (p)); } 367static pointer caar (pointer p) { return car (car (p)); }
384static pointer cadr (pointer p) { return car (cdr (p)); } 368static pointer cadr (pointer p) { return car (cdr (p)); }
385static pointer cdar (pointer p) { return cdr (car (p)); } 369static pointer cdar (pointer p) { return cdr (car (p)); }
386static pointer cddr (pointer p) { return cdr (cdr (p)); } 370static pointer cddr (pointer p) { return cdr (cdr (p)); }
390static pointer cdaar (pointer p) { return cdr (car (car (p))); } 374static pointer cdaar (pointer p) { return cdr (car (car (p))); }
391 375
392INTERFACE void 376INTERFACE void
393set_car (pointer p, pointer q) 377set_car (pointer p, pointer q)
394{ 378{
395 p->object.cons.car = q; 379 CELL(p)->object.cons.car = CELL (q);
396} 380}
397 381
398INTERFACE void 382INTERFACE void
399set_cdr (pointer p, pointer q) 383set_cdr (pointer p, pointer q)
400{ 384{
401 p->object.cons.cdr = q; 385 CELL(p)->object.cons.cdr = CELL (q);
402} 386}
403 387
404INTERFACE pointer 388INTERFACE pointer
405pair_car (pointer p) 389pair_car (pointer p)
406{ 390{
411pair_cdr (pointer p) 395pair_cdr (pointer p)
412{ 396{
413 return cdr (p); 397 return cdr (p);
414} 398}
415 399
416INTERFACE INLINE int 400INTERFACE int
417is_symbol (pointer p) 401is_symbol (pointer p)
418{ 402{
419 return type (p) == T_SYMBOL; 403 return type (p) == T_SYMBOL;
420} 404}
421 405
422INTERFACE INLINE char * 406INTERFACE char *
423symname (pointer p) 407symname (pointer p)
424{ 408{
425 return strvalue (car (p)); 409 return strvalue (p);
426} 410}
427 411
428#if USE_PLIST 412#if USE_PLIST
413#error plists are broken because symbols are no longer pairs
414#define symprop(p) cdr(p)
429SCHEME_EXPORT INLINE int 415SCHEME_EXPORT int
430hasprop (pointer p) 416hasprop (pointer p)
431{ 417{
432 return typeflag (p) & T_SYMBOL; 418 return typeflag (p) & T_SYMBOL;
433} 419}
434
435# define symprop(p) cdr(p)
436#endif 420#endif
437 421
438INTERFACE INLINE int 422INTERFACE int
439is_syntax (pointer p) 423is_syntax (pointer p)
440{ 424{
441 return typeflag (p) & T_SYNTAX; 425 return typeflag (p) & T_SYNTAX;
442} 426}
443 427
444INTERFACE INLINE int 428INTERFACE int
445is_proc (pointer p) 429is_proc (pointer p)
446{ 430{
447 return type (p) == T_PROC; 431 return type (p) == T_PROC;
448} 432}
449 433
450INTERFACE INLINE int 434INTERFACE int
451is_foreign (pointer p) 435is_foreign (pointer p)
452{ 436{
453 return type (p) == T_FOREIGN; 437 return type (p) == T_FOREIGN;
454} 438}
455 439
456INTERFACE INLINE char * 440INTERFACE char *
457syntaxname (pointer p) 441syntaxname (pointer p)
458{ 442{
459 return strvalue (car (p)); 443 return strvalue (p);
460} 444}
461 445
462#define procnum(p) ivalue (p) 446#define procnum(p) ivalue_unchecked (p)
463static const char *procname (pointer x); 447static const char *procname (pointer x);
464 448
465INTERFACE INLINE int 449INTERFACE int
466is_closure (pointer p) 450is_closure (pointer p)
467{ 451{
468 return type (p) == T_CLOSURE; 452 return type (p) == T_CLOSURE;
469} 453}
470 454
471INTERFACE INLINE int 455INTERFACE int
472is_macro (pointer p) 456is_macro (pointer p)
473{ 457{
474 return type (p) == T_MACRO; 458 return type (p) == T_MACRO;
475} 459}
476 460
477INTERFACE INLINE pointer 461INTERFACE pointer
478closure_code (pointer p) 462closure_code (pointer p)
479{ 463{
480 return car (p); 464 return car (p);
481} 465}
482 466
483INTERFACE INLINE pointer 467INTERFACE pointer
484closure_env (pointer p) 468closure_env (pointer p)
485{ 469{
486 return cdr (p); 470 return cdr (p);
487} 471}
488 472
489INTERFACE INLINE int 473INTERFACE int
490is_continuation (pointer p) 474is_continuation (pointer p)
491{ 475{
492 return type (p) == T_CONTINUATION; 476 return type (p) == T_CONTINUATION;
493} 477}
494 478
495#define cont_dump(p) cdr (p) 479#define cont_dump(p) cdr (p)
496#define set_cont_dump(p,v) set_cdr ((p), (v)) 480#define set_cont_dump(p,v) set_cdr ((p), (v))
497 481
498/* To do: promise should be forced ONCE only */ 482/* To do: promise should be forced ONCE only */
499INTERFACE INLINE int 483INTERFACE int
500is_promise (pointer p) 484is_promise (pointer p)
501{ 485{
502 return type (p) == T_PROMISE; 486 return type (p) == T_PROMISE;
503} 487}
504 488
505INTERFACE INLINE int 489INTERFACE int
506is_environment (pointer p) 490is_environment (pointer p)
507{ 491{
508 return type (p) == T_ENVIRONMENT; 492 return type (p) == T_ENVIRONMENT;
509} 493}
510 494
516 500
517#define is_mark(p) (typeflag (p) & T_MARK) 501#define is_mark(p) (typeflag (p) & T_MARK)
518#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) 502#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
519#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) 503#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
520 504
521INTERFACE INLINE int 505INTERFACE int
522is_immutable (pointer p) 506is_immutable (pointer p)
523{ 507{
524 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; 508 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
525} 509}
526 510
527INTERFACE INLINE void 511INTERFACE void
528setimmutable (pointer p) 512setimmutable (pointer p)
529{ 513{
530#if USE_ERROR_CHECKING 514#if USE_ERROR_CHECKING
531 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 515 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
532#endif 516#endif
533} 517}
534 518
519/* Result is:
520 proper list: length
521 circular list: -1
522 not even a pair: -2
523 dotted list: -2 minus length before dot
524*/
525INTERFACE int
526list_length (SCHEME_P_ pointer a)
527{
528 int i = 0;
529 pointer slow, fast;
530
531 slow = fast = a;
532
533 while (1)
534 {
535 if (fast == NIL)
536 return i;
537
538 if (!is_pair (fast))
539 return -2 - i;
540
541 fast = cdr (fast);
542 ++i;
543
544 if (fast == NIL)
545 return i;
546
547 if (!is_pair (fast))
548 return -2 - i;
549
550 ++i;
551 fast = cdr (fast);
552
553 /* Safe because we would have already returned if `fast'
554 encountered a non-pair. */
555 slow = cdr (slow);
556
557 if (fast == slow)
558 {
559 /* the fast pointer has looped back around and caught up
560 with the slow pointer, hence the structure is circular,
561 not of finite length, and therefore not a list */
562 return -1;
563 }
564 }
565}
566
567INTERFACE int
568is_list (SCHEME_P_ pointer a)
569{
570 return list_length (SCHEME_A_ a) >= 0;
571}
572
535#if USE_CHAR_CLASSIFIERS 573#if USE_CHAR_CLASSIFIERS
536static INLINE int 574ecb_inline int
537Cisalpha (int c) 575Cisalpha (int c)
538{ 576{
539 return isascii (c) && isalpha (c); 577 return isascii (c) && isalpha (c);
540} 578}
541 579
542static INLINE int 580ecb_inline int
543Cisdigit (int c) 581Cisdigit (int c)
544{ 582{
545 return isascii (c) && isdigit (c); 583 return isascii (c) && isdigit (c);
546} 584}
547 585
548static INLINE int 586ecb_inline int
549Cisspace (int c) 587Cisspace (int c)
550{ 588{
551 return isascii (c) && isspace (c); 589 return isascii (c) && isspace (c);
552} 590}
553 591
554static INLINE int 592ecb_inline int
555Cisupper (int c) 593Cisupper (int c)
556{ 594{
557 return isascii (c) && isupper (c); 595 return isascii (c) && isupper (c);
558} 596}
559 597
560static INLINE int 598ecb_inline int
561Cislower (int c) 599Cislower (int c)
562{ 600{
563 return isascii (c) && islower (c); 601 return isascii (c) && islower (c);
564} 602}
565#endif 603#endif
626#endif 664#endif
627 665
628static int file_push (SCHEME_P_ const char *fname); 666static int file_push (SCHEME_P_ const char *fname);
629static void file_pop (SCHEME_P); 667static void file_pop (SCHEME_P);
630static int file_interactive (SCHEME_P); 668static int file_interactive (SCHEME_P);
631static INLINE int is_one_of (char *s, int c); 669ecb_inline int is_one_of (const char *s, int c);
632static int alloc_cellseg (SCHEME_P_ int n); 670static int alloc_cellseg (SCHEME_P);
633static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); 671ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
634static void finalize_cell (SCHEME_P_ pointer a); 672static void finalize_cell (SCHEME_P_ pointer a);
635static int count_consecutive_cells (pointer x, int needed); 673static int count_consecutive_cells (pointer x, int needed);
636static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 674static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
637static pointer mk_number (SCHEME_P_ const num n); 675static pointer mk_number (SCHEME_P_ const num n);
638static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); 676static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
653static void mark (pointer a); 691static void mark (pointer a);
654static void gc (SCHEME_P_ pointer a, pointer b); 692static void gc (SCHEME_P_ pointer a, pointer b);
655static int basic_inchar (port *pt); 693static int basic_inchar (port *pt);
656static int inchar (SCHEME_P); 694static int inchar (SCHEME_P);
657static void backchar (SCHEME_P_ int c); 695static void backchar (SCHEME_P_ int c);
658static char *readstr_upto (SCHEME_P_ char *delim); 696static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
659static pointer readstrexp (SCHEME_P); 697static pointer readstrexp (SCHEME_P_ char delim);
660static INLINE int skipspace (SCHEME_P); 698ecb_inline int skipspace (SCHEME_P);
661static int token (SCHEME_P); 699static int token (SCHEME_P);
662static void printslashstring (SCHEME_P_ char *s, int len); 700static void printslashstring (SCHEME_P_ char *s, int len);
663static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 701static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
664static void printatom (SCHEME_P_ pointer l, int f); 702static void printatom (SCHEME_P_ pointer l, int f);
665static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 703static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
669static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); 707static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list);
670static pointer revappend (SCHEME_P_ pointer a, pointer b); 708static pointer revappend (SCHEME_P_ pointer a, pointer b);
671static pointer ss_get_cont (SCHEME_P); 709static pointer ss_get_cont (SCHEME_P);
672static void ss_set_cont (SCHEME_P_ pointer cont); 710static void ss_set_cont (SCHEME_P_ pointer cont);
673static void dump_stack_mark (SCHEME_P); 711static void dump_stack_mark (SCHEME_P);
674static pointer opexe_0 (SCHEME_P_ enum scheme_opcodes op); 712static int opexe_0 (SCHEME_P_ enum scheme_opcodes op);
713static int opexe_1 (SCHEME_P_ enum scheme_opcodes op);
675static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); 714static int opexe_2 (SCHEME_P_ enum scheme_opcodes op);
676static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); 715static int opexe_3 (SCHEME_P_ enum scheme_opcodes op);
677static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); 716static int opexe_4 (SCHEME_P_ enum scheme_opcodes op);
678static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); 717static int opexe_5 (SCHEME_P_ enum scheme_opcodes op);
679static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); 718static int opexe_6 (SCHEME_P_ enum scheme_opcodes op);
680static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 719static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
681static void assign_syntax (SCHEME_P_ const char *name); 720static void assign_syntax (SCHEME_P_ const char *name);
682static int syntaxnum (pointer p); 721static int syntaxnum (pointer p);
683static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 722static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
684 723
724static IVALUE
725ivalue (pointer x)
726{
727 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
728}
729
730static RVALUE
731rvalue (pointer x)
732{
733 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
734}
735
736INTERFACE num
737nvalue (pointer x)
738{
739 num n;
740
741 num_set_fixnum (n, is_integer (x));
742
743 if (num_is_fixnum (n))
744 num_set_ivalue (n, ivalue_unchecked (x));
745 else
746 num_set_rvalue (n, rvalue_unchecked (x));
747
748 return n;
749}
750
685static num 751static num
686num_add (num a, num b) 752num_op (enum num_op op, num a, num b)
687{ 753{
688 num ret; 754 num ret;
689 755
690 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 756 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
691 757
692 if (num_is_fixnum (ret)) 758 if (num_is_fixnum (ret))
693 num_set_ivalue (ret, num_get_ivalue (a) + num_get_ivalue (b)); 759 {
760 switch (op)
761 {
762 case NUM_ADD: a.ivalue += b.ivalue; break;
763 case NUM_SUB: a.ivalue -= b.ivalue; break;
764 case NUM_MUL: a.ivalue *= b.ivalue; break;
765 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
766 }
767
768 num_set_ivalue (ret, a.ivalue);
769 }
770#if USE_REAL
694 else 771 else
695 num_set_rvalue (ret, num_get_rvalue (a) + num_get_rvalue (b)); 772 {
773 switch (op)
774 {
775 case NUM_ADD: a.rvalue += b.rvalue; break;
776 case NUM_SUB: a.rvalue -= b.rvalue; break;
777 case NUM_MUL: a.rvalue *= b.rvalue; break;
778 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
779 }
696 780
697 return ret; 781 num_set_rvalue (ret, a.rvalue);
698} 782 }
699 783#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 784
712 return ret; 785 return ret;
713} 786}
714 787
715static num 788static num
716num_div (num a, num b) 789num_div (num a, num b)
717{ 790{
718 num ret; 791 num ret;
719 792
720 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); 793 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0);
721 794
722 if (num_is_fixnum (ret)) 795 if (num_is_fixnum (ret))
723 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 796 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
724 else 797 else
725 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 798 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 799
757 return ret; 800 return ret;
758} 801}
759 802
760static num 803static num
762{ 805{
763 num ret; 806 num ret;
764 long e1, e2, res; 807 long e1, e2, res;
765 808
766 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 809 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
767 e1 = num_get_ivalue (a); 810 e1 = num_ivalue (a);
768 e2 = num_get_ivalue (b); 811 e2 = num_ivalue (b);
769 res = e1 % e2; 812 res = e1 % e2;
770 813
771 /* remainder should have same sign as second operand */ 814 /* remainder should have same sign as second operand */
772 if (res > 0) 815 if (res > 0)
773 { 816 {
789{ 832{
790 num ret; 833 num ret;
791 long e1, e2, res; 834 long e1, e2, res;
792 835
793 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 836 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
794 e1 = num_get_ivalue (a); 837 e1 = num_ivalue (a);
795 e2 = num_get_ivalue (b); 838 e2 = num_ivalue (b);
796 res = e1 % e2; 839 res = e1 % e2;
797 840
798 /* modulo should have same sign as second operand */ 841 /* modulo should have same sign as second operand */
799 if (res * e2 < 0) 842 if (res * e2 < 0)
800 res += e2; 843 res += e2;
801 844
802 num_set_ivalue (ret, res); 845 num_set_ivalue (ret, res);
803 return ret; 846 return ret;
804} 847}
805 848
849/* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */
806static int 850static int
807num_eq (num a, num b) 851num_cmp (num a, num b)
808{ 852{
853 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
809 int ret; 854 int ret;
810 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
811 855
812 if (is_fixnum) 856 if (is_fixnum)
813 ret = num_get_ivalue (a) == num_get_ivalue (b); 857 {
858 IVALUE av = num_ivalue (a);
859 IVALUE bv = num_ivalue (b);
860
861 ret = av == bv ? 0 : av < bv ? -1 : +1;
862 }
814 else 863 else
815 ret = num_get_rvalue (a) == num_get_rvalue (b); 864 {
865 RVALUE av = num_rvalue (a);
866 RVALUE bv = num_rvalue (b);
867
868 ret = av == bv ? 0 : av < bv ? -1 : +1;
869 }
816 870
817 return ret; 871 return ret;
818} 872}
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 873
886static int 874static int
887is_zero_rvalue (RVALUE x) 875is_zero_rvalue (RVALUE x)
888{ 876{
877 return x == 0;
878#if 0
889#if USE_REAL 879#if USE_REAL
890 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ 880 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */
891#else 881#else
892 return x == 0; 882 return x == 0;
893#endif 883#endif
884#endif
894} 885}
895 886
896/* allocate new cell segment */ 887/* allocate new cell segment */
897static int 888static int
898alloc_cellseg (SCHEME_P_ int n) 889alloc_cellseg (SCHEME_P)
899{ 890{
900 pointer newp; 891 struct cell *newp;
901 pointer last; 892 struct cell *last;
902 pointer p; 893 struct cell *p;
903 char *cp; 894 char *cp;
904 long i; 895 long i;
905 int k; 896 int k;
906 897
907 static int segsize = CELL_SEGSIZE >> 1; 898 static int segsize = CELL_SEGSIZE >> 1;
908 segsize <<= 1; 899 segsize <<= 1;
909 900
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)); 901 cp = malloc (segsize * sizeof (struct cell));
916 902
917 if (!cp && USE_ERROR_CHECKING) 903 if (!cp && USE_ERROR_CHECKING)
918 return k; 904 return k;
919 905
920 i = ++SCHEME_V->last_cell_seg; 906 i = ++SCHEME_V->last_cell_seg;
921 SCHEME_V->alloc_seg[i] = cp; 907 SCHEME_V->alloc_seg[i] = cp;
922 908
923 /* insert new segment in address order */ 909 newp = (struct cell *)cp;
924 newp = (pointer)cp;
925 SCHEME_V->cell_seg[i] = newp; 910 SCHEME_V->cell_seg[i] = newp;
926 SCHEME_V->cell_segsize[i] = segsize; 911 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; 912 SCHEME_V->fcells += segsize;
943 last = newp + segsize - 1; 913 last = newp + segsize - 1;
944 914
945 for (p = newp; p <= last; p++) 915 for (p = newp; p <= last; p++)
946 { 916 {
917 pointer cp = POINTER (p);
947 set_typeflag (p, T_FREE); 918 set_typeflag (cp, T_PAIR);
948 set_car (p, NIL); 919 set_car (cp, NIL);
949 set_cdr (p, p + 1); 920 set_cdr (cp, POINTER (p + 1));
950 } 921 }
951 922
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); 923 set_cdr (POINTER (last), SCHEME_V->free_cell);
956 SCHEME_V->free_cell = newp; 924 SCHEME_V->free_cell = POINTER (newp);
957 }
958 else
959 {
960 p = SCHEME_V->free_cell;
961 925
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; 926 return 1;
971} 927}
972 928
973/* get new cell. parameter a, b is marked by gc. */ 929/* get new cell. parameter a, b is marked by gc. */
974static INLINE pointer 930ecb_inline pointer
975get_cell_x (SCHEME_P_ pointer a, pointer b) 931get_cell_x (SCHEME_P_ pointer a, pointer b)
976{ 932{
977 if (ecb_expect_false (SCHEME_V->free_cell == NIL)) 933 if (ecb_expect_false (SCHEME_V->free_cell == NIL))
978 { 934 {
979 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 935 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
980 return S_SINK; 936 return S_SINK;
981 937
982 if (SCHEME_V->free_cell == NIL) 938 if (SCHEME_V->free_cell == NIL)
983 { 939 {
984 const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; 940 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1;
985 941
986 gc (SCHEME_A_ a, b); 942 gc (SCHEME_A_ a, b);
987 943
988 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) 944 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL)
989 { 945 {
990 /* if only a few recovered, get more to avoid fruitless gc's */ 946 /* if only a few recovered, get more to avoid fruitless gc's */
991 if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) 947 if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL)
992 { 948 {
993#if USE_ERROR_CHECKING 949#if USE_ERROR_CHECKING
994 SCHEME_V->no_memory = 1; 950 SCHEME_V->no_memory = 1;
995 return S_SINK; 951 return S_SINK;
996#endif 952#endif
1008 } 964 }
1009} 965}
1010 966
1011/* To retain recent allocs before interpreter knows about them - 967/* To retain recent allocs before interpreter knows about them -
1012 Tehom */ 968 Tehom */
1013
1014static void 969static void
1015push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 970push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
1016{ 971{
1017 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 972 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
1018 973
1040} 995}
1041 996
1042static pointer 997static pointer
1043get_vector_object (SCHEME_P_ uint32_t len, pointer init) 998get_vector_object (SCHEME_P_ uint32_t len, pointer init)
1044{ 999{
1045 pointer v = get_cell_x (SCHEME_A_ 0, 0); 1000 pointer v = get_cell_x (SCHEME_A_ NIL, NIL);
1046 pointer *e = malloc (len * sizeof (pointer)); 1001 pointer *e = malloc (len * sizeof (pointer));
1047 1002
1048 if (!e && USE_ERROR_CHECKING) 1003 if (!e && USE_ERROR_CHECKING)
1049 return S_SINK; 1004 return S_SINK;
1050 1005
1051 /* Record it as a vector so that gc understands it. */ 1006 /* Record it as a vector so that gc understands it. */
1052 set_typeflag (v, T_VECTOR | T_ATOM); 1007 set_typeflag (v, T_VECTOR | T_ATOM);
1053 1008
1054 v->object.vector.vvalue = e; 1009 CELL(v)->object.vector.vvalue = e;
1055 v->object.vector.length = len; 1010 CELL(v)->object.vector.length = len;
1056 fill_vector (v, init); 1011 fill_vector (v, 0, init);
1057 push_recent_alloc (SCHEME_A_ v, NIL); 1012 push_recent_alloc (SCHEME_A_ v, NIL);
1058 1013
1059 return v; 1014 return v;
1060} 1015}
1061 1016
1062static INLINE void 1017ecb_inline void
1063ok_to_freely_gc (SCHEME_P) 1018ok_to_freely_gc (SCHEME_P)
1064{ 1019{
1065 set_car (S_SINK, NIL); 1020 set_car (S_SINK, NIL);
1066} 1021}
1067 1022
1069static void 1024static void
1070check_cell_alloced (pointer p, int expect_alloced) 1025check_cell_alloced (pointer p, int expect_alloced)
1071{ 1026{
1072 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ 1027 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */
1073 if (typeflag (p) & !expect_alloced) 1028 if (typeflag (p) & !expect_alloced)
1074 xwrstr ("Cell is already allocated!\n"); 1029 putstr (SCHEME_A_ "Cell is already allocated!\n");
1075 1030
1076 if (!(typeflag (p)) & expect_alloced) 1031 if (!(typeflag (p)) & expect_alloced)
1077 xwrstr ("Cell is not allocated!\n"); 1032 putstr (SCHEME_A_ "Cell is not allocated!\n");
1078} 1033}
1079 1034
1080static void 1035static void
1081check_range_alloced (pointer p, int n, int expect_alloced) 1036check_range_alloced (pointer p, int n, int expect_alloced)
1082{ 1037{
1104 set_cdr (x, b); 1059 set_cdr (x, b);
1105 1060
1106 return x; 1061 return x;
1107} 1062}
1108 1063
1064static pointer
1065generate_symbol (SCHEME_P_ const char *name)
1066{
1067 pointer x = mk_string (SCHEME_A_ name);
1068 setimmutable (x);
1069 set_typeflag (x, T_SYMBOL | T_ATOM);
1070 return x;
1071}
1072
1109/* ========== oblist implementation ========== */ 1073/* ========== oblist implementation ========== */
1110 1074
1111#ifndef USE_OBJECT_LIST 1075#ifndef USE_OBJECT_LIST
1112 1076
1077static int
1113static int hash_fn (const char *key, int table_size); 1078hash_fn (const char *key, int table_size)
1079{
1080 const unsigned char *p = key;
1081 uint32_t hash = 2166136261;
1082
1083 while (*p)
1084 hash = (hash ^ *p++) * 16777619;
1085
1086 return hash % table_size;
1087}
1114 1088
1115static pointer 1089static pointer
1116oblist_initial_value (SCHEME_P) 1090oblist_initial_value (SCHEME_P)
1117{ 1091{
1118 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1092 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1120 1094
1121/* returns the new symbol */ 1095/* returns the new symbol */
1122static pointer 1096static pointer
1123oblist_add_by_name (SCHEME_P_ const char *name) 1097oblist_add_by_name (SCHEME_P_ const char *name)
1124{ 1098{
1125 int location; 1099 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)); 1100 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))); 1101 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1133 return x; 1102 return x;
1134} 1103}
1135 1104
1136static INLINE pointer 1105ecb_inline pointer
1137oblist_find_by_name (SCHEME_P_ const char *name) 1106oblist_find_by_name (SCHEME_P_ const char *name)
1138{ 1107{
1139 int location; 1108 int location;
1140 pointer x; 1109 pointer x;
1141 char *s; 1110 char *s;
1142 1111
1143 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1112 location = hash_fn (name, veclength (SCHEME_V->oblist));
1144 1113
1145 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1114 for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1146 { 1115 {
1147 s = symname (car (x)); 1116 s = symname (car (x));
1148 1117
1149 /* case-insensitive, per R5RS section 2 */ 1118 /* case-insensitive, per R5RS section 2 */
1150 if (stricmp (name, s) == 0) 1119 if (stricmp (name, s) == 0)
1160 int i; 1129 int i;
1161 pointer x; 1130 pointer x;
1162 pointer ob_list = NIL; 1131 pointer ob_list = NIL;
1163 1132
1164 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1133 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1165 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1134 for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1166 ob_list = cons (x, ob_list); 1135 ob_list = cons (x, ob_list);
1167 1136
1168 return ob_list; 1137 return ob_list;
1169} 1138}
1170 1139
1174oblist_initial_value (SCHEME_P) 1143oblist_initial_value (SCHEME_P)
1175{ 1144{
1176 return NIL; 1145 return NIL;
1177} 1146}
1178 1147
1179static INLINE pointer 1148ecb_inline pointer
1180oblist_find_by_name (SCHEME_P_ const char *name) 1149oblist_find_by_name (SCHEME_P_ const char *name)
1181{ 1150{
1182 pointer x; 1151 pointer x;
1183 char *s; 1152 char *s;
1184 1153
1196 1165
1197/* returns the new symbol */ 1166/* returns the new symbol */
1198static pointer 1167static pointer
1199oblist_add_by_name (SCHEME_P_ const char *name) 1168oblist_add_by_name (SCHEME_P_ const char *name)
1200{ 1169{
1201 pointer x; 1170 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); 1171 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1207 return x; 1172 return x;
1208} 1173}
1209 1174
1210static pointer 1175static pointer
1220mk_port (SCHEME_P_ port *p) 1185mk_port (SCHEME_P_ port *p)
1221{ 1186{
1222 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1187 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1223 1188
1224 set_typeflag (x, T_PORT | T_ATOM); 1189 set_typeflag (x, T_PORT | T_ATOM);
1225 x->object.port = p; 1190 set_port (x, p);
1226 1191
1227 return x; 1192 return x;
1228} 1193}
1229#endif 1194#endif
1230 1195
1231pointer 1196pointer
1232mk_foreign_func (SCHEME_P_ foreign_func f) 1197mk_foreign_func (SCHEME_P_ foreign_func f)
1233{ 1198{
1234 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1199 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1235 1200
1236 set_typeflag (x, (T_FOREIGN | T_ATOM)); 1201 set_typeflag (x, T_FOREIGN | T_ATOM);
1237 x->object.ff = f; 1202 CELL(x)->object.ff = f;
1238 1203
1239 return x; 1204 return x;
1240} 1205}
1241 1206
1242INTERFACE pointer 1207INTERFACE pointer
1243mk_character (SCHEME_P_ int c) 1208mk_character (SCHEME_P_ int c)
1244{ 1209{
1245 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1210 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1246 1211
1247 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1212 set_typeflag (x, T_CHARACTER | T_ATOM);
1248 ivalue_unchecked (x) = c & 0xff; 1213 set_ivalue (x, c & 0xff);
1249 set_num_integer (x); 1214
1250 return x; 1215 return x;
1251} 1216}
1252 1217
1253/* get number atom (integer) */ 1218/* get number atom (integer) */
1254INTERFACE pointer 1219INTERFACE pointer
1255mk_integer (SCHEME_P_ long num) 1220mk_integer (SCHEME_P_ long n)
1256{ 1221{
1222 pointer p = 0;
1223 pointer *pp = &p;
1224
1225#if USE_INTCACHE
1226 if (n >= INTCACHE_MIN && n <= INTCACHE_MAX)
1227 pp = &SCHEME_V->intcache[n - INTCACHE_MIN];
1228#endif
1229
1230 if (!*pp)
1231 {
1257 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1232 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1258 1233
1259 set_typeflag (x, (T_NUMBER | T_ATOM)); 1234 set_typeflag (x, T_INTEGER | T_ATOM);
1260 ivalue_unchecked (x) = num; 1235 setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */
1261 set_num_integer (x); 1236 set_ivalue (x, n);
1237
1238 *pp = x;
1239 }
1240
1262 return x; 1241 return *pp;
1263} 1242}
1264 1243
1265INTERFACE pointer 1244INTERFACE pointer
1266mk_real (SCHEME_P_ RVALUE n) 1245mk_real (SCHEME_P_ RVALUE n)
1267{ 1246{
1247#if USE_REAL
1268 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1248 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1269 1249
1270 set_typeflag (x, (T_NUMBER | T_ATOM)); 1250 set_typeflag (x, T_REAL | T_ATOM);
1271 rvalue_unchecked (x) = n; 1251 set_rvalue (x, n);
1272 set_num_real (x); 1252
1273 return x; 1253 return x;
1254#else
1255 return mk_integer (SCHEME_A_ n);
1256#endif
1274} 1257}
1275 1258
1276static pointer 1259static pointer
1277mk_number (SCHEME_P_ const num n) 1260mk_number (SCHEME_P_ const num n)
1278{ 1261{
1262#if USE_REAL
1279 if (num_is_fixnum (n)) 1263 return num_is_fixnum (n)
1264 ? mk_integer (SCHEME_A_ num_ivalue (n))
1265 : mk_real (SCHEME_A_ num_rvalue (n));
1266#else
1280 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1267 return mk_integer (SCHEME_A_ num_ivalue (n));
1281 else 1268#endif
1282 return mk_real (SCHEME_A_ num_get_rvalue (n));
1283} 1269}
1284 1270
1285/* allocate name to string area */ 1271/* allocate name to string area */
1286static char * 1272static char *
1287store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) 1273store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1293 SCHEME_V->no_memory = 1; 1279 SCHEME_V->no_memory = 1;
1294 return SCHEME_V->strbuff; 1280 return SCHEME_V->strbuff;
1295 } 1281 }
1296 1282
1297 if (str) 1283 if (str)
1298 { 1284 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 1285 else
1308 {
1309 memset (q, fill, len_str); 1286 memset (q, fill, len_str);
1287
1310 q[len_str] = 0; 1288 q[len_str] = 0;
1311 }
1312 1289
1313 return q; 1290 return q;
1314} 1291}
1315 1292
1316INTERFACE pointer 1293INTERFACE pointer
1330 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1307 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1331 1308
1332 set_typeflag (x, T_STRING | T_ATOM); 1309 set_typeflag (x, T_STRING | T_ATOM);
1333 strvalue (x) = store_string (SCHEME_A_ len, str, 0); 1310 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1334 strlength (x) = len; 1311 strlength (x) = len;
1312
1335 return x; 1313 return x;
1336} 1314}
1337 1315
1338INTERFACE pointer 1316INTERFACE pointer
1339mk_string (SCHEME_P_ const char *str) 1317mk_string (SCHEME_P_ const char *str)
1346{ 1324{
1347 return get_vector_object (SCHEME_A_ len, NIL); 1325 return get_vector_object (SCHEME_A_ len, NIL);
1348} 1326}
1349 1327
1350INTERFACE void 1328INTERFACE void
1351fill_vector (pointer vec, pointer obj) 1329fill_vector (pointer vec, uint32_t start, pointer obj)
1352{ 1330{
1353 int i; 1331 int i;
1354 1332
1355 for (i = 0; i < vec->object.vector.length; i++) 1333 for (i = start; i < veclength (vec); i++)
1356 vecvalue (vec)[i] = obj; 1334 vecvalue (vec)[i] = obj;
1357} 1335}
1358 1336
1337INTERFACE void
1338vector_resize (pointer vec, uint32_t newsize, pointer fill)
1339{
1340 uint32_t oldsize = veclength (vec);
1341 vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer));
1342 veclength (vec) = newsize;
1343 fill_vector (vec, oldsize, fill);
1344}
1345
1359INTERFACE pointer 1346INTERFACE pointer
1360vector_elem (pointer vec, uint32_t ielem) 1347vector_get (pointer vec, uint32_t ielem)
1361{ 1348{
1362 return vecvalue(vec)[ielem]; 1349 return vecvalue(vec)[ielem];
1363} 1350}
1364 1351
1365INTERFACE void 1352INTERFACE void
1366set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1353vector_set (pointer vec, uint32_t ielem, pointer a)
1367{ 1354{
1368 vecvalue(vec)[ielem] = a; 1355 vecvalue(vec)[ielem] = a;
1369} 1356}
1370 1357
1371/* get new symbol */ 1358/* get new symbol */
1383 1370
1384INTERFACE pointer 1371INTERFACE pointer
1385gensym (SCHEME_P) 1372gensym (SCHEME_P)
1386{ 1373{
1387 pointer x; 1374 pointer x;
1388
1389 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1390 {
1391 char name[40] = "gensym-"; 1375 char name[40] = "gensym-";
1392 xnum (name + 7, SCHEME_V->gensym_cnt); 1376 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1393 1377
1394 /* first check oblist */ 1378 return generate_symbol (SCHEME_A_ name);
1395 x = oblist_find_by_name (SCHEME_A_ name); 1379}
1396 1380
1397 if (x == NIL) 1381static int
1398 { 1382is_gensym (SCHEME_P_ pointer x)
1399 x = oblist_add_by_name (SCHEME_A_ name); 1383{
1400 return x; 1384 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1401 }
1402 }
1403
1404 return NIL;
1405} 1385}
1406 1386
1407/* make symbol or number atom from string */ 1387/* make symbol or number atom from string */
1408static pointer 1388static pointer
1409mk_atom (SCHEME_P_ char *q) 1389mk_atom (SCHEME_P_ char *q)
1463 } 1443 }
1464 else if ((c == 'e') || (c == 'E')) 1444 else if ((c == 'e') || (c == 'E'))
1465 { 1445 {
1466 if (!has_fp_exp) 1446 if (!has_fp_exp)
1467 { 1447 {
1468 has_dec_point = 1; /* decimal point illegal 1448 has_dec_point = 1; /* decimal point illegal from now on */
1469 from now on */
1470 p++; 1449 p++;
1471 1450
1472 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1451 if ((*p == '-') || (*p == '+') || isdigit (*p))
1473 continue; 1452 continue;
1474 } 1453 }
1562 1541
1563 if (ecb_expect_false (is_vector (p))) 1542 if (ecb_expect_false (is_vector (p)))
1564 { 1543 {
1565 int i; 1544 int i;
1566 1545
1567 for (i = 0; i < p->object.vector.length; i++) 1546 for (i = 0; i < veclength (p); i++)
1568 mark (vecvalue (p)[i]); 1547 mark (vecvalue (p)[i]);
1569 } 1548 }
1570 1549
1571 if (is_atom (p)) 1550 if (is_atom (p))
1572 goto E6; 1551 goto E6;
1619 1598
1620/* garbage collection. parameter a, b is marked. */ 1599/* garbage collection. parameter a, b is marked. */
1621static void 1600static void
1622gc (SCHEME_P_ pointer a, pointer b) 1601gc (SCHEME_P_ pointer a, pointer b)
1623{ 1602{
1624 pointer p;
1625 int i; 1603 int i;
1626 1604
1627 if (SCHEME_V->gc_verbose) 1605 if (SCHEME_V->gc_verbose)
1628 putstr (SCHEME_A_ "gc..."); 1606 putstr (SCHEME_A_ "gc...");
1629 1607
1645 /* Mark recent objects the interpreter doesn't know about yet. */ 1623 /* Mark recent objects the interpreter doesn't know about yet. */
1646 mark (car (S_SINK)); 1624 mark (car (S_SINK));
1647 /* Mark any older stuff above nested C calls */ 1625 /* Mark any older stuff above nested C calls */
1648 mark (SCHEME_V->c_nest); 1626 mark (SCHEME_V->c_nest);
1649 1627
1628#if USE_INTCACHE
1629 /* mark intcache */
1630 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1631 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1632 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1633#endif
1634
1650 /* mark variables a, b */ 1635 /* mark variables a, b */
1651 mark (a); 1636 mark (a);
1652 mark (b); 1637 mark (b);
1653 1638
1654 /* garbage collect */ 1639 /* garbage collect */
1655 clrmark (NIL); 1640 clrmark (NIL);
1656 SCHEME_V->fcells = 0; 1641 SCHEME_V->fcells = 0;
1657 SCHEME_V->free_cell = NIL; 1642 SCHEME_V->free_cell = NIL;
1658 1643
1659 /* free-list is kept sorted by address so as to maintain consecutive 1644 if (SCHEME_V->gc_verbose)
1660 ranges, if possible, for use with vectors. Here we scan the cells 1645 putstr (SCHEME_A_ "freeing...");
1661 (which are also kept sorted by address) downwards to build the 1646
1662 free-list in sorted order. 1647 uint32_t total = 0;
1663 */ 1648
1649 /* Here we scan the cells to build the free-list. */
1664 for (i = SCHEME_V->last_cell_seg; i >= 0; i--) 1650 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1665 { 1651 {
1666 p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; 1652 struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1653 struct cell *p;
1654 total += SCHEME_V->cell_segsize [i];
1667 1655
1668 while (--p >= SCHEME_V->cell_seg[i]) 1656 for (p = SCHEME_V->cell_seg[i]; p < end; ++p)
1669 { 1657 {
1658 pointer c = POINTER (p);
1659
1670 if (is_mark (p)) 1660 if (is_mark (c))
1671 clrmark (p); 1661 clrmark (c);
1672 else 1662 else
1673 { 1663 {
1674 /* reclaim cell */ 1664 /* reclaim cell */
1675 if (typeflag (p) != T_FREE) 1665 if (typeflag (c) != T_PAIR)
1676 { 1666 {
1677 finalize_cell (SCHEME_A_ p); 1667 finalize_cell (SCHEME_A_ c);
1678 set_typeflag (p, T_FREE); 1668 set_typeflag (c, T_PAIR);
1679 set_car (p, NIL); 1669 set_car (c, NIL);
1680 } 1670 }
1681 1671
1682 ++SCHEME_V->fcells; 1672 ++SCHEME_V->fcells;
1683 set_cdr (p, SCHEME_V->free_cell); 1673 set_cdr (c, SCHEME_V->free_cell);
1684 SCHEME_V->free_cell = p; 1674 SCHEME_V->free_cell = c;
1685 } 1675 }
1686 } 1676 }
1687 } 1677 }
1688 1678
1689 if (SCHEME_V->gc_verbose) 1679 if (SCHEME_V->gc_verbose)
1690 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1680 {
1681 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");
1682 }
1691} 1683}
1692 1684
1693static void 1685static void
1694finalize_cell (SCHEME_P_ pointer a) 1686finalize_cell (SCHEME_P_ pointer a)
1695{ 1687{
1696 /* TODO, fast bitmap check? */ 1688 /* TODO, fast bitmap check? */
1697 if (is_string (a)) 1689 if (is_string (a) || is_symbol (a))
1698 free (strvalue (a)); 1690 free (strvalue (a));
1699 else if (is_vector (a)) 1691 else if (is_vector (a))
1700 free (vecvalue (a)); 1692 free (vecvalue (a));
1701#if USE_PORTS 1693#if USE_PORTS
1702 else if (is_port (a)) 1694 else if (is_port (a))
1703 { 1695 {
1704 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) 1696 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit)
1705 port_close (SCHEME_A_ a, port_input | port_output); 1697 port_close (SCHEME_A_ a, port_input | port_output);
1706 1698
1707 free (a->object.port); 1699 free (port (a));
1708 } 1700 }
1709#endif 1701#endif
1710} 1702}
1711 1703
1712/* ========== Routines for Reading ========== */ 1704/* ========== Routines for Reading ========== */
1728 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; 1720 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1;
1729 SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; 1721 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; 1722 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; 1723 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1;
1732 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; 1724 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0;
1733 SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; 1725 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1734 1726
1735#if SHOW_ERROR_LINE 1727#if SHOW_ERROR_LINE
1736 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; 1728 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0;
1737 1729
1738 if (fname) 1730 if (fname)
1755 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1747 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1756#if USE_PORTS 1748#if USE_PORTS
1757 port_close (SCHEME_A_ SCHEME_V->loadport, port_input); 1749 port_close (SCHEME_A_ SCHEME_V->loadport, port_input);
1758#endif 1750#endif
1759 SCHEME_V->file_i--; 1751 SCHEME_V->file_i--;
1760 SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; 1752 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1761 } 1753 }
1762} 1754}
1763 1755
1764static int 1756static int
1765file_interactive (SCHEME_P) 1757file_interactive (SCHEME_P)
1766{ 1758{
1767#if USE_PORTS 1759#if USE_PORTS
1768 return SCHEME_V->file_i == 0 1760 return SCHEME_V->file_i == 0
1769 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1761 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1770 && (SCHEME_V->inport->object.port->kind & port_file); 1762 && (port (SCHEME_V->inport)->kind & port_file);
1771#else 1763#else
1772 return 0; 1764 return 0;
1773#endif 1765#endif
1774} 1766}
1775 1767
1909} 1901}
1910 1902
1911static void 1903static void
1912port_close (SCHEME_P_ pointer p, int flag) 1904port_close (SCHEME_P_ pointer p, int flag)
1913{ 1905{
1914 port *pt = p->object.port; 1906 port *pt = port (p);
1915 1907
1916 pt->kind &= ~flag; 1908 pt->kind &= ~flag;
1917 1909
1918 if ((pt->kind & (port_input | port_output)) == 0) 1910 if ((pt->kind & (port_input | port_output)) == 0)
1919 { 1911 {
1940/* get new character from input file */ 1932/* get new character from input file */
1941static int 1933static int
1942inchar (SCHEME_P) 1934inchar (SCHEME_P)
1943{ 1935{
1944 int c; 1936 int c;
1945 port *pt; 1937 port *pt = port (SCHEME_V->inport);
1946
1947 pt = SCHEME_V->inport->object.port;
1948 1938
1949 if (pt->kind & port_saw_EOF) 1939 if (pt->kind & port_saw_EOF)
1950 return EOF; 1940 return EOF;
1951 1941
1952 c = basic_inchar (pt); 1942 c = basic_inchar (pt);
2019 port *pt; 2009 port *pt;
2020 2010
2021 if (c == EOF) 2011 if (c == EOF)
2022 return; 2012 return;
2023 2013
2024 pt = SCHEME_V->inport->object.port; 2014 pt = port (SCHEME_V->inport);
2025 pt->unget = c; 2015 pt->unget = c;
2026#else 2016#else
2027 if (c == EOF) 2017 if (c == EOF)
2028 return; 2018 return;
2029 2019
2057 2047
2058INTERFACE void 2048INTERFACE void
2059putstr (SCHEME_P_ const char *s) 2049putstr (SCHEME_P_ const char *s)
2060{ 2050{
2061#if USE_PORTS 2051#if USE_PORTS
2062 port *pt = SCHEME_V->outport->object.port; 2052 port *pt = port (SCHEME_V->outport);
2063 2053
2064 if (pt->kind & port_file) 2054 if (pt->kind & port_file)
2065 write (pt->rep.stdio.file, s, strlen (s)); 2055 write (pt->rep.stdio.file, s, strlen (s));
2066 else 2056 else
2067 for (; *s; s++) 2057 for (; *s; s++)
2069 *pt->rep.string.curr++ = *s; 2059 *pt->rep.string.curr++ = *s;
2070 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) 2060 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2071 *pt->rep.string.curr++ = *s; 2061 *pt->rep.string.curr++ = *s;
2072 2062
2073#else 2063#else
2074 xwrstr (s); 2064 write (pt->rep.stdio.file, s, strlen (s));
2075#endif 2065#endif
2076} 2066}
2077 2067
2078static void 2068static void
2079putchars (SCHEME_P_ const char *s, int len) 2069putchars (SCHEME_P_ const char *s, int len)
2080{ 2070{
2081#if USE_PORTS 2071#if USE_PORTS
2082 port *pt = SCHEME_V->outport->object.port; 2072 port *pt = port (SCHEME_V->outport);
2083 2073
2084 if (pt->kind & port_file) 2074 if (pt->kind & port_file)
2085 write (pt->rep.stdio.file, s, len); 2075 write (pt->rep.stdio.file, s, len);
2086 else 2076 else
2087 { 2077 {
2101 2091
2102INTERFACE void 2092INTERFACE void
2103putcharacter (SCHEME_P_ int c) 2093putcharacter (SCHEME_P_ int c)
2104{ 2094{
2105#if USE_PORTS 2095#if USE_PORTS
2106 port *pt = SCHEME_V->outport->object.port; 2096 port *pt = port (SCHEME_V->outport);
2107 2097
2108 if (pt->kind & port_file) 2098 if (pt->kind & port_file)
2109 { 2099 {
2110 char cc = c; 2100 char cc = c;
2111 write (pt->rep.stdio.file, &cc, 1); 2101 write (pt->rep.stdio.file, &cc, 1);
2124#endif 2114#endif
2125} 2115}
2126 2116
2127/* read characters up to delimiter, but cater to character constants */ 2117/* read characters up to delimiter, but cater to character constants */
2128static char * 2118static char *
2129readstr_upto (SCHEME_P_ char *delim) 2119readstr_upto (SCHEME_P_ int skip, const char *delim)
2130{ 2120{
2131 char *p = SCHEME_V->strbuff; 2121 char *p = SCHEME_V->strbuff + skip;
2132 2122
2133 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2123 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2134 2124
2135 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2125 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2136 *p = 0; 2126 *p = 0;
2143 return SCHEME_V->strbuff; 2133 return SCHEME_V->strbuff;
2144} 2134}
2145 2135
2146/* read string expression "xxx...xxx" */ 2136/* read string expression "xxx...xxx" */
2147static pointer 2137static pointer
2148readstrexp (SCHEME_P) 2138readstrexp (SCHEME_P_ char delim)
2149{ 2139{
2150 char *p = SCHEME_V->strbuff; 2140 char *p = SCHEME_V->strbuff;
2151 int c; 2141 int c;
2152 int c1 = 0; 2142 int c1 = 0;
2153 enum
2154 { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; 2143 enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok;
2155 2144
2156 for (;;) 2145 for (;;)
2157 { 2146 {
2158 c = inchar (SCHEME_A); 2147 c = inchar (SCHEME_A);
2159 2148
2161 return S_F; 2150 return S_F;
2162 2151
2163 switch (state) 2152 switch (state)
2164 { 2153 {
2165 case st_ok: 2154 case st_ok:
2166 switch (c) 2155 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); 2156 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff);
2175 2157
2176 default: 2158 if (ecb_expect_false (c == '\\'))
2159 state = st_bsl;
2160 else
2177 *p++ = c; 2161 *p++ = c;
2178 break;
2179 }
2180 2162
2181 break; 2163 break;
2182 2164
2183 case st_bsl: 2165 case st_bsl:
2184 switch (c) 2166 switch (c)
2214 case 'r': 2196 case 'r':
2215 *p++ = '\r'; 2197 *p++ = '\r';
2216 state = st_ok; 2198 state = st_ok;
2217 break; 2199 break;
2218 2200
2219 case '"':
2220 *p++ = '"';
2221 state = st_ok;
2222 break;
2223
2224 default: 2201 default:
2225 *p++ = c; 2202 *p++ = c;
2226 state = st_ok; 2203 state = st_ok;
2227 break; 2204 break;
2228 } 2205 }
2229 2206
2230 break; 2207 break;
2231 2208
2232 case st_x1: 2209 case st_x1:
2233 case st_x2: 2210 case st_x2:
2234 c = toupper (c); 2211 c = tolower (c);
2235 2212
2236 if (c >= '0' && c <= 'F') 2213 if (c >= '0' && c <= '9')
2237 {
2238 if (c <= '9')
2239 c1 = (c1 << 4) + c - '0'; 2214 c1 = (c1 << 4) + c - '0';
2240 else 2215 else if (c >= 'a' && c <= 'f')
2241 c1 = (c1 << 4) + c - 'A' + 10; 2216 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 2217 else
2252 return S_F; 2218 return S_F;
2219
2220 if (state == st_x1)
2221 state = st_x2;
2222 else
2223 {
2224 *p++ = c1;
2225 state = st_ok;
2226 }
2253 2227
2254 break; 2228 break;
2255 2229
2256 case st_oct1: 2230 case st_oct1:
2257 case st_oct2: 2231 case st_oct2:
2261 backchar (SCHEME_A_ c); 2235 backchar (SCHEME_A_ c);
2262 state = st_ok; 2236 state = st_ok;
2263 } 2237 }
2264 else 2238 else
2265 { 2239 {
2266 if (state == st_oct2 && c1 >= 32) 2240 if (state == st_oct2 && c1 >= ' ')
2267 return S_F; 2241 return S_F;
2268 2242
2269 c1 = (c1 << 3) + (c - '0'); 2243 c1 = (c1 << 3) + (c - '0');
2270 2244
2271 if (state == st_oct1) 2245 if (state == st_oct1)
2276 state = st_ok; 2250 state = st_ok;
2277 } 2251 }
2278 } 2252 }
2279 2253
2280 break; 2254 break;
2281
2282 } 2255 }
2283 } 2256 }
2284} 2257}
2285 2258
2286/* check c is in chars */ 2259/* check c is in chars */
2287static INLINE int 2260ecb_inline int
2288is_one_of (char *s, int c) 2261is_one_of (const char *s, int c)
2289{ 2262{
2290 if (c == EOF)
2291 return 1;
2292
2293 return !!strchr (s, c); 2263 return c == EOF || !!strchr (s, c);
2294} 2264}
2295 2265
2296/* skip white characters */ 2266/* skip white characters */
2297static INLINE int 2267ecb_inline int
2298skipspace (SCHEME_P) 2268skipspace (SCHEME_P)
2299{ 2269{
2300 int c, curr_line = 0; 2270 int c, curr_line = 0;
2301 2271
2302 do 2272 do
2303 { 2273 {
2304 c = inchar (SCHEME_A); 2274 c = inchar (SCHEME_A);
2275
2305#if SHOW_ERROR_LINE 2276#if SHOW_ERROR_LINE
2306 if (c == '\n') 2277 if (ecb_expect_false (c == '\n'))
2307 curr_line++; 2278 curr_line++;
2308#endif 2279#endif
2280
2281 if (ecb_expect_false (c == EOF))
2282 return c;
2309 } 2283 }
2310 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2284 while (is_one_of (WHITESPACE, c));
2311 2285
2312 /* record it */ 2286 /* record it */
2313#if SHOW_ERROR_LINE 2287#if SHOW_ERROR_LINE
2314 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) 2288 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; 2289 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line;
2316#endif 2290#endif
2317 2291
2318 if (c != EOF)
2319 {
2320 backchar (SCHEME_A_ c); 2292 backchar (SCHEME_A_ c);
2321 return 1; 2293 return 1;
2322 }
2323 else
2324 return EOF;
2325} 2294}
2326 2295
2327/* get token */ 2296/* get token */
2328static int 2297static int
2329token (SCHEME_P) 2298token (SCHEME_P)
2345 return TOK_RPAREN; 2314 return TOK_RPAREN;
2346 2315
2347 case '.': 2316 case '.':
2348 c = inchar (SCHEME_A); 2317 c = inchar (SCHEME_A);
2349 2318
2350 if (is_one_of (" \n\t", c)) 2319 if (is_one_of (WHITESPACE, c))
2351 return TOK_DOT; 2320 return TOK_DOT;
2352 else 2321 else
2353 { 2322 {
2354 //TODO: ungetc twice in a row is not supported in C
2355 backchar (SCHEME_A_ c); 2323 backchar (SCHEME_A_ c);
2356 backchar (SCHEME_A_ '.');
2357 return TOK_ATOM; 2324 return TOK_DOTATOM;
2358 } 2325 }
2326
2327 case '|':
2328 return TOK_STRATOM;
2359 2329
2360 case '\'': 2330 case '\'':
2361 return TOK_QUOTE; 2331 return TOK_QUOTE;
2362 2332
2363 case ';': 2333 case ';':
2495 } 2465 }
2496 2466
2497 putcharacter (SCHEME_A_ '"'); 2467 putcharacter (SCHEME_A_ '"');
2498} 2468}
2499 2469
2500
2501/* print atoms */ 2470/* print atoms */
2502static void 2471static void
2503printatom (SCHEME_P_ pointer l, int f) 2472printatom (SCHEME_P_ pointer l, int f)
2504{ 2473{
2505 char *p; 2474 char *p;
2506 int len; 2475 int len;
2507 2476
2508 atom2str (SCHEME_A_ l, f, &p, &len); 2477 atom2str (SCHEME_A_ l, f, &p, &len);
2509 putchars (SCHEME_A_ p, len); 2478 putchars (SCHEME_A_ p, len);
2510} 2479}
2511
2512 2480
2513/* Uses internal buffer unless string pointer is already available */ 2481/* Uses internal buffer unless string pointer is already available */
2514static void 2482static void
2515atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2483atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2516{ 2484{
2530 { 2498 {
2531 p = SCHEME_V->strbuff; 2499 p = SCHEME_V->strbuff;
2532 2500
2533 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2501 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2534 { 2502 {
2535 if (num_is_integer (l)) 2503 if (is_integer (l))
2536 xnum (p, ivalue_unchecked (l)); 2504 xnum (p, ivalue_unchecked (l));
2537#if USE_REAL 2505#if USE_REAL
2538 else 2506 else
2539 { 2507 {
2540 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2508 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2677#endif 2645#endif
2678 } 2646 }
2679 else if (is_continuation (l)) 2647 else if (is_continuation (l))
2680 p = "#<CONTINUATION>"; 2648 p = "#<CONTINUATION>";
2681 else 2649 else
2650 {
2651#if USE_PRINTF
2652 p = SCHEME_V->strbuff;
2653 snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l));
2654#else
2682 p = "#<ERROR>"; 2655 p = "#<ERROR>";
2656#endif
2657 }
2683 2658
2684 *pp = p; 2659 *pp = p;
2685 *plen = strlen (p); 2660 *plen = strlen (p);
2686} 2661}
2687 2662
2795 return 0; 2770 return 0;
2796 } 2771 }
2797 else if (is_number (a)) 2772 else if (is_number (a))
2798 { 2773 {
2799 if (is_number (b)) 2774 if (is_number (b))
2800 if (num_is_integer (a) == num_is_integer (b))
2801 return num_eq (nvalue (a), nvalue (b)); 2775 return num_cmp (nvalue (a), nvalue (b)) == 0;
2802 2776
2803 return 0; 2777 return 0;
2804 } 2778 }
2805 else if (is_character (a)) 2779 else if (is_character (a))
2806 { 2780 {
2832/* () is #t in R5RS */ 2806/* () is #t in R5RS */
2833#define is_true(p) ((p) != S_F) 2807#define is_true(p) ((p) != S_F)
2834#define is_false(p) ((p) == S_F) 2808#define is_false(p) ((p) == S_F)
2835 2809
2836/* ========== Environment implementation ========== */ 2810/* ========== 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 2811
2853#ifndef USE_ALIST_ENV 2812#ifndef USE_ALIST_ENV
2854 2813
2855/* 2814/*
2856 * In this implementation, each frame of the environment may be 2815 * In this implementation, each frame of the environment may be
2873 2832
2874 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2833 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2875 setenvironment (SCHEME_V->envir); 2834 setenvironment (SCHEME_V->envir);
2876} 2835}
2877 2836
2878static INLINE void 2837static uint32_t
2838sym_hash (pointer sym, uint32_t size)
2839{
2840 uintptr_t ptr = (uintptr_t)sym;
2841
2842#if 0
2843 /* table size is prime, so why mix */
2844 ptr += ptr >> 32;
2845 ptr += ptr >> 16;
2846 ptr += ptr >> 8;
2847#endif
2848
2849 return ptr % size;
2850}
2851
2852ecb_inline void
2879new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2853new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2880{ 2854{
2881 pointer slot = immutable_cons (variable, value); 2855 pointer slot = immutable_cons (variable, value);
2882 2856
2883 if (is_vector (car (env))) 2857 if (is_vector (car (env)))
2884 { 2858 {
2885 int location = hash_fn (symname (variable), veclength (car (env))); 2859 int location = sym_hash (variable, veclength (car (env)));
2886
2887 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2860 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2888 } 2861 }
2889 else 2862 else
2890 set_car (env, immutable_cons (slot, car (env))); 2863 set_car (env, immutable_cons (slot, car (env)));
2891} 2864}
2892 2865
2893static pointer 2866static pointer
2894find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2867find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2895{ 2868{
2896 pointer x, y; 2869 pointer x, y;
2897 int location;
2898 2870
2899 for (x = env; x != NIL; x = cdr (x)) 2871 for (x = env; x != NIL; x = cdr (x))
2900 { 2872 {
2901 if (is_vector (car (x))) 2873 if (is_vector (car (x)))
2902 { 2874 {
2903 location = hash_fn (symname (hdl), veclength (car (x))); 2875 int location = sym_hash (hdl, veclength (car (x)));
2904 y = vector_elem (car (x), location); 2876 y = vector_get (car (x), location);
2905 } 2877 }
2906 else 2878 else
2907 y = car (x); 2879 y = car (x);
2908 2880
2909 for (; y != NIL; y = cdr (y)) 2881 for (; y != NIL; y = cdr (y))
2910 if (caar (y) == hdl) 2882 if (caar (y) == hdl)
2911 break; 2883 break;
2912 2884
2913 if (y != NIL) 2885 if (y != NIL)
2886 return car (y);
2887
2888 if (!all)
2914 break; 2889 break;
2915
2916 if (!all)
2917 return NIL;
2918 } 2890 }
2919
2920 if (x != NIL)
2921 return car (y);
2922 2891
2923 return NIL; 2892 return NIL;
2924} 2893}
2925 2894
2926#else /* USE_ALIST_ENV */ 2895#else /* USE_ALIST_ENV */
2927 2896
2928static INLINE void 2897ecb_inline void
2929new_frame_in_env (SCHEME_P_ pointer old_env) 2898new_frame_in_env (SCHEME_P_ pointer old_env)
2930{ 2899{
2931 SCHEME_V->envir = immutable_cons (NIL, old_env); 2900 SCHEME_V->envir = immutable_cons (NIL, old_env);
2932 setenvironment (SCHEME_V->envir); 2901 setenvironment (SCHEME_V->envir);
2933} 2902}
2934 2903
2935static INLINE void 2904ecb_inline void
2936new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2905new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2937{ 2906{
2938 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2907 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2939} 2908}
2940 2909
2948 for (y = car (x); y != NIL; y = cdr (y)) 2917 for (y = car (x); y != NIL; y = cdr (y))
2949 if (caar (y) == hdl) 2918 if (caar (y) == hdl)
2950 break; 2919 break;
2951 2920
2952 if (y != NIL) 2921 if (y != NIL)
2922 return car (y);
2953 break; 2923 break;
2954 2924
2955 if (!all) 2925 if (!all)
2956 return NIL; 2926 break;
2957 } 2927 }
2958
2959 if (x != NIL)
2960 return car (y);
2961 2928
2962 return NIL; 2929 return NIL;
2963} 2930}
2964 2931
2965#endif /* USE_ALIST_ENV else */ 2932#endif /* USE_ALIST_ENV else */
2966 2933
2967static INLINE void 2934ecb_inline void
2968new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2935new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2969{ 2936{
2937 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2970 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2938 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2971} 2939}
2972 2940
2973static INLINE void 2941ecb_inline void
2974set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2942set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2975{ 2943{
2976 set_cdr (slot, value); 2944 set_cdr (slot, value);
2977} 2945}
2978 2946
2979static INLINE pointer 2947ecb_inline pointer
2980slot_value_in_env (pointer slot) 2948slot_value_in_env (pointer slot)
2981{ 2949{
2982 return cdr (slot); 2950 return cdr (slot);
2983} 2951}
2984 2952
2985/* ========== Evaluation Cycle ========== */ 2953/* ========== Evaluation Cycle ========== */
2986 2954
2987static pointer 2955static int
2988xError_1 (SCHEME_P_ const char *s, pointer a) 2956xError_1 (SCHEME_P_ const char *s, pointer a)
2989{ 2957{
2990#if USE_ERROR_HOOK 2958#if USE_ERROR_HOOK
2991 pointer x; 2959 pointer x;
2992 pointer hdl = SCHEME_V->ERROR_HOOK; 2960 pointer hdl = SCHEME_V->ERROR_HOOK;
3027 code = cons (mk_string (SCHEME_A_ s), code); 2995 code = cons (mk_string (SCHEME_A_ s), code);
3028 setimmutable (car (code)); 2996 setimmutable (car (code));
3029 SCHEME_V->code = cons (slot_value_in_env (x), code); 2997 SCHEME_V->code = cons (slot_value_in_env (x), code);
3030 SCHEME_V->op = OP_EVAL; 2998 SCHEME_V->op = OP_EVAL;
3031 2999
3032 return S_T; 3000 return 0;
3033 } 3001 }
3034#endif 3002#endif
3035 3003
3036 if (a) 3004 if (a)
3037 SCHEME_V->args = cons (a, NIL); 3005 SCHEME_V->args = cons (a, NIL);
3039 SCHEME_V->args = NIL; 3007 SCHEME_V->args = NIL;
3040 3008
3041 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); 3009 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args);
3042 setimmutable (car (SCHEME_V->args)); 3010 setimmutable (car (SCHEME_V->args));
3043 SCHEME_V->op = OP_ERR0; 3011 SCHEME_V->op = OP_ERR0;
3012
3044 return S_T; 3013 return 0;
3045} 3014}
3046 3015
3047#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) 3016#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a)
3048#define Error_0(s) Error_1 (s, 0) 3017#define Error_0(s) Error_1 (s, 0)
3049 3018
3050/* Too small to turn into function */ 3019/* Too small to turn into function */
3051#define BEGIN do { 3020#define BEGIN do {
3052#define END } while (0) 3021#define END } while (0)
3053#define s_goto(a) BEGIN \ 3022#define s_goto(a) BEGIN \
3054 SCHEME_V->op = a; \ 3023 SCHEME_V->op = a; \
3055 return S_T; END 3024 return 0; END
3056 3025
3057#define s_return(a) return xs_return (SCHEME_A_ a) 3026#define s_return(a) return xs_return (SCHEME_A_ a)
3058 3027
3059#ifndef USE_SCHEME_STACK 3028#ifndef USE_SCHEME_STACK
3060 3029
3074{ 3043{
3075 int nframes = (uintptr_t)SCHEME_V->dump; 3044 int nframes = (uintptr_t)SCHEME_V->dump;
3076 struct dump_stack_frame *next_frame; 3045 struct dump_stack_frame *next_frame;
3077 3046
3078 /* enough room for the next frame? */ 3047 /* enough room for the next frame? */
3079 if (nframes >= SCHEME_V->dump_size) 3048 if (ecb_expect_false (nframes >= SCHEME_V->dump_size))
3080 { 3049 {
3081 SCHEME_V->dump_size += STACK_GROWTH; 3050 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); 3051 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size);
3083 } 3052 }
3084 3053
3085 next_frame = SCHEME_V->dump_base + nframes; 3054 next_frame = SCHEME_V->dump_base + nframes;
3086 3055
3087 next_frame->op = op; 3056 next_frame->op = op;
3088 next_frame->args = args; 3057 next_frame->args = args;
3089 next_frame->envir = SCHEME_V->envir; 3058 next_frame->envir = SCHEME_V->envir;
3090 next_frame->code = code; 3059 next_frame->code = code;
3091 3060
3092 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3061 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3093} 3062}
3094 3063
3095static pointer 3064static int
3096xs_return (SCHEME_P_ pointer a) 3065xs_return (SCHEME_P_ pointer a)
3097{ 3066{
3098 int nframes = (uintptr_t)SCHEME_V->dump; 3067 int nframes = (uintptr_t)SCHEME_V->dump;
3099 struct dump_stack_frame *frame; 3068 struct dump_stack_frame *frame;
3100 3069
3101 SCHEME_V->value = a; 3070 SCHEME_V->value = a;
3102 3071
3103 if (nframes <= 0) 3072 if (nframes <= 0)
3104 return NIL; 3073 return -1;
3105 3074
3106 frame = &SCHEME_V->dump_base[--nframes]; 3075 frame = &SCHEME_V->dump_base[--nframes];
3107 SCHEME_V->op = frame->op; 3076 SCHEME_V->op = frame->op;
3108 SCHEME_V->args = frame->args; 3077 SCHEME_V->args = frame->args;
3109 SCHEME_V->envir = frame->envir; 3078 SCHEME_V->envir = frame->envir;
3110 SCHEME_V->code = frame->code; 3079 SCHEME_V->code = frame->code;
3111 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3080 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3112 3081
3113 return S_T; 3082 return 0;
3114} 3083}
3115 3084
3116static INLINE void 3085ecb_inline void
3117dump_stack_reset (SCHEME_P) 3086dump_stack_reset (SCHEME_P)
3118{ 3087{
3119 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3088 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3120 SCHEME_V->dump = (pointer)+0; 3089 SCHEME_V->dump = (pointer)+0;
3121} 3090}
3122 3091
3123static INLINE void 3092ecb_inline void
3124dump_stack_initialize (SCHEME_P) 3093dump_stack_initialize (SCHEME_P)
3125{ 3094{
3126 SCHEME_V->dump_size = 0; 3095 SCHEME_V->dump_size = 0;
3127 SCHEME_V->dump_base = 0; 3096 SCHEME_V->dump_base = 0;
3128 dump_stack_reset (SCHEME_A); 3097 dump_stack_reset (SCHEME_A);
3181 int i = 0; 3150 int i = 0;
3182 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3151 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3183 3152
3184 while (cont != NIL) 3153 while (cont != NIL)
3185 { 3154 {
3186 frame->op = ivalue (car (cont)); cont = cdr (cont); 3155 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3187 frame->args = car (cont) ; cont = cdr (cont); 3156 frame->args = car (cont) ; cont = cdr (cont);
3188 frame->envir = car (cont) ; cont = cdr (cont); 3157 frame->envir = car (cont) ; cont = cdr (cont);
3189 frame->code = car (cont) ; cont = cdr (cont); 3158 frame->code = car (cont) ; cont = cdr (cont);
3190 3159
3191 ++frame; 3160 ++frame;
3192 ++i; 3161 ++i;
3193 } 3162 }
3194 3163
3195 SCHEME_V->dump = (pointer)(uintptr_t)i; 3164 SCHEME_V->dump = (pointer)(uintptr_t)i;
3196} 3165}
3197 3166
3198#else 3167#else
3199 3168
3200static INLINE void 3169ecb_inline void
3201dump_stack_reset (SCHEME_P) 3170dump_stack_reset (SCHEME_P)
3202{ 3171{
3203 SCHEME_V->dump = NIL; 3172 SCHEME_V->dump = NIL;
3204} 3173}
3205 3174
3206static INLINE void 3175ecb_inline void
3207dump_stack_initialize (SCHEME_P) 3176dump_stack_initialize (SCHEME_P)
3208{ 3177{
3209 dump_stack_reset (SCHEME_A); 3178 dump_stack_reset (SCHEME_A);
3210} 3179}
3211 3180
3213dump_stack_free (SCHEME_P) 3182dump_stack_free (SCHEME_P)
3214{ 3183{
3215 SCHEME_V->dump = NIL; 3184 SCHEME_V->dump = NIL;
3216} 3185}
3217 3186
3218static pointer 3187static int
3219xs_return (SCHEME_P_ pointer a) 3188xs_return (SCHEME_P_ pointer a)
3220{ 3189{
3221 pointer dump = SCHEME_V->dump; 3190 pointer dump = SCHEME_V->dump;
3222 3191
3223 SCHEME_V->value = a; 3192 SCHEME_V->value = a;
3224 3193
3225 if (dump == NIL) 3194 if (dump == NIL)
3226 return NIL; 3195 return -1;
3227 3196
3228 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3197 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3229 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3198 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3230 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3199 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3231 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3200 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3232 3201
3233 SCHEME_V->dump = dump; 3202 SCHEME_V->dump = dump;
3234 3203
3235 return S_T; 3204 return 0;
3236} 3205}
3237 3206
3238static void 3207static void
3239s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3208s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3240{ 3209{
3265 3234
3266#endif 3235#endif
3267 3236
3268#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3237#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3269 3238
3239#if EXPERIMENT
3270static pointer 3240static int
3241debug (SCHEME_P_ int indent, pointer x)
3242{
3243 int c;
3244
3245 if (is_syntax (x))
3246 {
3247 printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x));
3248 return 8 + 8;
3249 }
3250
3251 if (x == NIL)
3252 {
3253 printf ("%*sNIL\n", indent, "");
3254 return 3;
3255 }
3256
3257 switch (type (x))
3258 {
3259 case T_INTEGER:
3260 printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x);
3261 return 32+8;
3262
3263 case T_SYMBOL:
3264 printf ("%*sS<%s>\n", indent, "", symname (x));
3265 return 24+8;
3266
3267 case T_CLOSURE:
3268 printf ("%*sS<%s>\n", indent, "", "closure");
3269 debug (SCHEME_A_ indent + 3, cdr(x));
3270 return 32 + debug (SCHEME_A_ indent + 3, car (x));
3271
3272 case T_PAIR:
3273 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3274 c = debug (SCHEME_A_ indent + 3, car (x));
3275 c += debug (SCHEME_A_ indent + 3, cdr (x));
3276 return c + 1;
3277
3278 case T_PORT:
3279 printf ("%*sS<%s>\n", indent, "", "port");
3280 return 24+8;
3281
3282 case T_VECTOR:
3283 printf ("%*sS<%s>\n", indent, "", "vector");
3284 return 24+8;
3285
3286 case T_ENVIRONMENT:
3287 printf ("%*sS<%s>\n", indent, "", "environment");
3288 return 0 + debug (SCHEME_A_ indent + 3, car (x));
3289
3290 default:
3291 printf ("unhandled type %d\n", type (x));
3292 break;
3293 }
3294}
3295#endif
3296
3297static int
3271opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3298opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3272{ 3299{
3300 pointer args = SCHEME_V->args;
3273 pointer x, y; 3301 pointer x, y;
3274 3302
3275 switch (op) 3303 switch (op)
3276 { 3304 {
3305#if EXPERIMENT //D
3306 case OP_DEBUG:
3307 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8);
3308 printf ("\n");
3309 s_return (S_T);
3310#endif
3277 case OP_LOAD: /* load */ 3311 case OP_LOAD: /* load */
3278 if (file_interactive (SCHEME_A)) 3312 if (file_interactive (SCHEME_A))
3279 { 3313 {
3280 xwrstr ("Loading "); xwrstr (strvalue (car (SCHEME_V->args))); xwrstr ("\n"); 3314 putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n");
3281 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (SCHEME_V->args))); 3315 //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args)));
3282 } 3316 }
3283 3317
3284 if (!file_push (SCHEME_A_ strvalue (car (SCHEME_V->args)))) 3318 if (!file_push (SCHEME_A_ strvalue (car (args))))
3285 Error_1 ("unable to open", car (SCHEME_V->args)); 3319 Error_1 ("unable to open", car (args));
3286 else 3320 else
3287 { 3321 {
3288 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3322 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3289 s_goto (OP_T0LVL); 3323 s_goto (OP_T0LVL);
3290 } 3324 }
3291 3325
3292 case OP_T0LVL: /* top level */ 3326 case OP_T0LVL: /* top level */
3293 3327
3294 /* If we reached the end of file, this loop is done. */ 3328 /* If we reached the end of file, this loop is done. */
3295 if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) 3329 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3296 { 3330 {
3297 if (SCHEME_V->file_i == 0) 3331 if (SCHEME_V->file_i == 0)
3298 { 3332 {
3299 SCHEME_V->args = NIL; 3333 SCHEME_V->args = NIL;
3300 s_goto (OP_QUIT); 3334 s_goto (OP_QUIT);
3364 case OP_EVAL: /* main part of evaluation */ 3398 case OP_EVAL: /* main part of evaluation */
3365#if USE_TRACING 3399#if USE_TRACING
3366 if (SCHEME_V->tracing) 3400 if (SCHEME_V->tracing)
3367 { 3401 {
3368 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ 3402 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */
3369 s_save (SCHEME_A_ OP_REAL_EVAL, SCHEME_V->args, SCHEME_V->code); 3403 s_save (SCHEME_A_ OP_REAL_EVAL, args, SCHEME_V->code);
3370 SCHEME_V->args = SCHEME_V->code; 3404 SCHEME_V->args = SCHEME_V->code;
3371 putstr (SCHEME_A_ "\nEval: "); 3405 putstr (SCHEME_A_ "\nEval: ");
3372 s_goto (OP_P0LIST); 3406 s_goto (OP_P0LIST);
3373 } 3407 }
3374 3408
3378#endif 3412#endif
3379 if (is_symbol (SCHEME_V->code)) /* symbol */ 3413 if (is_symbol (SCHEME_V->code)) /* symbol */
3380 { 3414 {
3381 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); 3415 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1);
3382 3416
3383 if (x != NIL) 3417 if (x == NIL)
3384 s_return (slot_value_in_env (x));
3385 else
3386 Error_1 ("eval: unbound variable:", SCHEME_V->code); 3418 Error_1 ("eval: unbound variable:", SCHEME_V->code);
3419
3420 s_return (slot_value_in_env (x));
3387 } 3421 }
3388 else if (is_pair (SCHEME_V->code)) 3422 else if (is_pair (SCHEME_V->code))
3389 { 3423 {
3390 x = car (SCHEME_V->code); 3424 x = car (SCHEME_V->code);
3391 3425
3404 } 3438 }
3405 else 3439 else
3406 s_return (SCHEME_V->code); 3440 s_return (SCHEME_V->code);
3407 3441
3408 case OP_E0ARGS: /* eval arguments */ 3442 case OP_E0ARGS: /* eval arguments */
3409 if (is_macro (SCHEME_V->value)) /* macro expansion */ 3443 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3410 { 3444 {
3411 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3445 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3412 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3446 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3413 SCHEME_V->code = SCHEME_V->value; 3447 SCHEME_V->code = SCHEME_V->value;
3414 s_goto (OP_APPLY); 3448 s_goto (OP_APPLY);
3418 SCHEME_V->code = cdr (SCHEME_V->code); 3452 SCHEME_V->code = cdr (SCHEME_V->code);
3419 s_goto (OP_E1ARGS); 3453 s_goto (OP_E1ARGS);
3420 } 3454 }
3421 3455
3422 case OP_E1ARGS: /* eval arguments */ 3456 case OP_E1ARGS: /* eval arguments */
3423 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3457 args = cons (SCHEME_V->value, args);
3424 3458
3425 if (is_pair (SCHEME_V->code)) /* continue */ 3459 if (is_pair (SCHEME_V->code)) /* continue */
3426 { 3460 {
3427 s_save (SCHEME_A_ OP_E1ARGS, SCHEME_V->args, cdr (SCHEME_V->code)); 3461 s_save (SCHEME_A_ OP_E1ARGS, args, cdr (SCHEME_V->code));
3428 SCHEME_V->code = car (SCHEME_V->code); 3462 SCHEME_V->code = car (SCHEME_V->code);
3429 SCHEME_V->args = NIL; 3463 SCHEME_V->args = NIL;
3430 s_goto (OP_EVAL); 3464 s_goto (OP_EVAL);
3431 } 3465 }
3432 else /* end */ 3466 else /* end */
3433 { 3467 {
3434 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3468 args = reverse_in_place (SCHEME_A_ NIL, args);
3435 SCHEME_V->code = car (SCHEME_V->args); 3469 SCHEME_V->code = car (args);
3436 SCHEME_V->args = cdr (SCHEME_V->args); 3470 SCHEME_V->args = cdr (args);
3437 s_goto (OP_APPLY); 3471 s_goto (OP_APPLY);
3438 } 3472 }
3439 3473
3440#if USE_TRACING 3474#if USE_TRACING
3441 3475
3442 case OP_TRACING: 3476 case OP_TRACING:
3443 { 3477 {
3444 int tr = SCHEME_V->tracing; 3478 int tr = SCHEME_V->tracing;
3445 3479
3446 SCHEME_V->tracing = ivalue (car (SCHEME_V->args)); 3480 SCHEME_V->tracing = ivalue_unchecked (car (args));
3447 s_return (mk_integer (SCHEME_A_ tr)); 3481 s_return (mk_integer (SCHEME_A_ tr));
3448 } 3482 }
3449 3483
3450#endif 3484#endif
3451 3485
3452 case OP_APPLY: /* apply 'code' to 'args' */ 3486 case OP_APPLY: /* apply 'code' to 'args' */
3453#if USE_TRACING 3487#if USE_TRACING
3454 if (SCHEME_V->tracing) 3488 if (SCHEME_V->tracing)
3455 { 3489 {
3456 s_save (SCHEME_A_ OP_REAL_APPLY, SCHEME_V->args, SCHEME_V->code); 3490 s_save (SCHEME_A_ OP_REAL_APPLY, args, SCHEME_V->code);
3457 SCHEME_V->print_flag = 1; 3491 SCHEME_V->print_flag = 1;
3458 /* SCHEME_V->args=cons(SCHEME_V->code,SCHEME_V->args); */ 3492 /* args=cons(SCHEME_V->code,args); */
3459 putstr (SCHEME_A_ "\nApply to: "); 3493 putstr (SCHEME_A_ "\nApply to: ");
3460 s_goto (OP_P0LIST); 3494 s_goto (OP_P0LIST);
3461 } 3495 }
3462 3496
3463 /* fall through */ 3497 /* fall through */
3464 3498
3465 case OP_REAL_APPLY: 3499 case OP_REAL_APPLY:
3466#endif 3500#endif
3467 if (is_proc (SCHEME_V->code)) 3501 if (is_proc (SCHEME_V->code))
3468 {
3469 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3502 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3470 }
3471 else if (is_foreign (SCHEME_V->code)) 3503 else if (is_foreign (SCHEME_V->code))
3472 { 3504 {
3473 /* Keep nested calls from GC'ing the arglist */ 3505 /* Keep nested calls from GC'ing the arglist */
3474 push_recent_alloc (SCHEME_A_ SCHEME_V->args, NIL); 3506 push_recent_alloc (SCHEME_A_ args, NIL);
3475 x = SCHEME_V->code->object.ff (SCHEME_A_ SCHEME_V->args); 3507 x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args);
3476 3508
3477 s_return (x); 3509 s_return (x);
3478 } 3510 }
3479 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ 3511 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */
3480 { 3512 {
3481 /* Should not accept promise */ 3513 /* Should not accept promise */
3482 /* make environment */ 3514 /* make environment */
3483 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); 3515 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code));
3484 3516
3485 for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) 3517 for (x = car (closure_code (SCHEME_V->code)), y = args; is_pair (x); x = cdr (x), y = cdr (y))
3486 { 3518 {
3487 if (y == NIL) 3519 if (y == NIL)
3488 Error_0 ("not enough arguments"); 3520 Error_0 ("not enough arguments");
3489 else 3521 else
3490 new_slot_in_env (SCHEME_A_ car (x), car (y)); 3522 new_slot_in_env (SCHEME_A_ car (x), car (y));
3508 s_goto (OP_BEGIN); 3540 s_goto (OP_BEGIN);
3509 } 3541 }
3510 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ 3542 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */
3511 { 3543 {
3512 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); 3544 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code));
3513 s_return (SCHEME_V->args != NIL ? car (SCHEME_V->args) : NIL); 3545 s_return (args != NIL ? car (args) : NIL);
3514 } 3546 }
3515 else 3547 else
3516 Error_0 ("illegal function"); 3548 Error_0 ("illegal function");
3517 3549
3518 case OP_DOMACRO: /* do macro */ 3550 case OP_DOMACRO: /* do macro */
3519 SCHEME_V->code = SCHEME_V->value; 3551 SCHEME_V->code = SCHEME_V->value;
3520 s_goto (OP_EVAL); 3552 s_goto (OP_EVAL);
3521
3522#if 1
3523 3553
3524 case OP_LAMBDA: /* lambda */ 3554 case OP_LAMBDA: /* lambda */
3525 /* If the hook is defined, apply it to SCHEME_V->code, otherwise 3555 /* If the hook is defined, apply it to SCHEME_V->code, otherwise
3526 set SCHEME_V->value fall thru */ 3556 set SCHEME_V->value fall thru */
3527 { 3557 {
3528 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); 3558 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1);
3529 3559
3530 if (f != NIL) 3560 if (f != NIL)
3531 { 3561 {
3532 s_save (SCHEME_A_ OP_LAMBDA1, SCHEME_V->args, SCHEME_V->code); 3562 s_save (SCHEME_A_ OP_LAMBDA1, args, SCHEME_V->code);
3533 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3563 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3534 SCHEME_V->code = slot_value_in_env (f); 3564 SCHEME_V->code = slot_value_in_env (f);
3535 s_goto (OP_APPLY); 3565 s_goto (OP_APPLY);
3536 } 3566 }
3537 3567
3538 SCHEME_V->value = SCHEME_V->code; 3568 SCHEME_V->value = SCHEME_V->code;
3539 /* Fallthru */
3540 } 3569 }
3570 /* Fallthru */
3541 3571
3542 case OP_LAMBDA1: 3572 case OP_LAMBDA1:
3543 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); 3573 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir));
3544 3574
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 */ 3575 case OP_MKCLOSURE: /* make-closure */
3553 x = car (SCHEME_V->args); 3576 x = car (args);
3554 3577
3555 if (car (x) == SCHEME_V->LAMBDA) 3578 if (car (x) == SCHEME_V->LAMBDA)
3556 x = cdr (x); 3579 x = cdr (x);
3557 3580
3558 if (cdr (SCHEME_V->args) == NIL) 3581 if (cdr (args) == NIL)
3559 y = SCHEME_V->envir; 3582 y = SCHEME_V->envir;
3560 else 3583 else
3561 y = cadr (SCHEME_V->args); 3584 y = cadr (args);
3562 3585
3563 s_return (mk_closure (SCHEME_A_ x, y)); 3586 s_return (mk_closure (SCHEME_A_ x, y));
3564 3587
3565 case OP_QUOTE: /* quote */ 3588 case OP_QUOTE: /* quote */
3566 s_return (car (SCHEME_V->code)); 3589 s_return (car (SCHEME_V->code));
3598 3621
3599 3622
3600 case OP_DEFP: /* defined? */ 3623 case OP_DEFP: /* defined? */
3601 x = SCHEME_V->envir; 3624 x = SCHEME_V->envir;
3602 3625
3603 if (cdr (SCHEME_V->args) != NIL) 3626 if (cdr (args) != NIL)
3604 x = cadr (SCHEME_V->args); 3627 x = cadr (args);
3605 3628
3606 s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); 3629 s_retbool (find_slot_in_env (SCHEME_A_ x, car (args), 1) != NIL);
3607 3630
3608 case OP_SET0: /* set! */ 3631 case OP_SET0: /* set! */
3609 if (is_immutable (car (SCHEME_V->code))) 3632 if (is_immutable (car (SCHEME_V->code)))
3610 Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code)); 3633 Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code));
3611 3634
3642 3665
3643 case OP_IF1: /* if */ 3666 case OP_IF1: /* if */
3644 if (is_true (SCHEME_V->value)) 3667 if (is_true (SCHEME_V->value))
3645 SCHEME_V->code = car (SCHEME_V->code); 3668 SCHEME_V->code = car (SCHEME_V->code);
3646 else 3669 else
3647 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because 3670 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
3648
3649 * car(NIL) = NIL */
3650 s_goto (OP_EVAL); 3671 s_goto (OP_EVAL);
3651 3672
3652 case OP_LET0: /* let */ 3673 case OP_LET0: /* let */
3653 SCHEME_V->args = NIL; 3674 SCHEME_V->args = NIL;
3654 SCHEME_V->value = SCHEME_V->code; 3675 SCHEME_V->value = SCHEME_V->code;
3655 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); 3676 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code);
3656 s_goto (OP_LET1); 3677 s_goto (OP_LET1);
3657 3678
3658 case OP_LET1: /* let (calculate parameters) */ 3679 case OP_LET1: /* let (calculate parameters) */
3659 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3680 args = cons (SCHEME_V->value, args);
3660 3681
3661 if (is_pair (SCHEME_V->code)) /* continue */ 3682 if (is_pair (SCHEME_V->code)) /* continue */
3662 { 3683 {
3663 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3684 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)); 3685 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code));
3665 3686
3666 s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); 3687 s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code));
3667 SCHEME_V->code = cadar (SCHEME_V->code); 3688 SCHEME_V->code = cadar (SCHEME_V->code);
3668 SCHEME_V->args = NIL; 3689 SCHEME_V->args = NIL;
3669 s_goto (OP_EVAL); 3690 s_goto (OP_EVAL);
3670 } 3691 }
3671 else /* end */ 3692 else /* end */
3672 { 3693 {
3673 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3694 args = reverse_in_place (SCHEME_A_ NIL, args);
3674 SCHEME_V->code = car (SCHEME_V->args); 3695 SCHEME_V->code = car (args);
3675 SCHEME_V->args = cdr (SCHEME_V->args); 3696 SCHEME_V->args = cdr (args);
3676 s_goto (OP_LET2); 3697 s_goto (OP_LET2);
3677 } 3698 }
3678 3699
3679 case OP_LET2: /* let */ 3700 case OP_LET2: /* let */
3680 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 3701 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3681 3702
3682 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; 3703 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)) 3704 y != NIL; x = cdr (x), y = cdr (y))
3684 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3705 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3685 3706
3686 if (is_symbol (car (SCHEME_V->code))) /* named let */ 3707 if (is_symbol (car (SCHEME_V->code))) /* named let */
3687 { 3708 {
3688 for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) 3709 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x))
3689 { 3710 {
3690 if (!is_pair (x)) 3711 if (!is_pair (x))
3691 Error_1 ("Bad syntax of binding in let :", x); 3712 Error_1 ("Bad syntax of binding in let :", x);
3692 3713
3693 if (!is_list (SCHEME_A_ car (x))) 3714 if (!is_list (SCHEME_A_ car (x)))
3694 Error_1 ("Bad syntax of binding in let :", car (x)); 3715 Error_1 ("Bad syntax of binding in let :", car (x));
3695 3716
3696 SCHEME_V->args = cons (caar (x), SCHEME_V->args); 3717 args = cons (caar (x), args);
3697 } 3718 }
3698 3719
3699 x =
3700 mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args), cddr (SCHEME_V->code)), 3720 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)),
3701 SCHEME_V->envir); 3721 SCHEME_V->envir);
3702 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); 3722 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x);
3703 SCHEME_V->code = cddr (SCHEME_V->code); 3723 SCHEME_V->code = cddr (SCHEME_V->code);
3704 SCHEME_V->args = NIL;
3705 } 3724 }
3706 else 3725 else
3707 { 3726 {
3708 SCHEME_V->code = cdr (SCHEME_V->code); 3727 SCHEME_V->code = cdr (SCHEME_V->code);
3728 }
3729
3709 SCHEME_V->args = NIL; 3730 SCHEME_V->args = NIL;
3710 }
3711
3712 s_goto (OP_BEGIN); 3731 s_goto (OP_BEGIN);
3713 3732
3714 case OP_LET0AST: /* let* */ 3733 case OP_LET0AST: /* let* */
3715 if (car (SCHEME_V->code) == NIL) 3734 if (car (SCHEME_V->code) == NIL)
3716 { 3735 {
3734 new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value); 3753 new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value);
3735 SCHEME_V->code = cdr (SCHEME_V->code); 3754 SCHEME_V->code = cdr (SCHEME_V->code);
3736 3755
3737 if (is_pair (SCHEME_V->code)) /* continue */ 3756 if (is_pair (SCHEME_V->code)) /* continue */
3738 { 3757 {
3739 s_save (SCHEME_A_ OP_LET2AST, SCHEME_V->args, SCHEME_V->code); 3758 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code);
3740 SCHEME_V->code = cadar (SCHEME_V->code); 3759 SCHEME_V->code = cadar (SCHEME_V->code);
3741 SCHEME_V->args = NIL; 3760 SCHEME_V->args = NIL;
3742 s_goto (OP_EVAL); 3761 s_goto (OP_EVAL);
3743 } 3762 }
3744 else /* end */ 3763 else /* end */
3745 { 3764 {
3746 SCHEME_V->code = SCHEME_V->args; 3765 SCHEME_V->code = args;
3747 SCHEME_V->args = NIL; 3766 SCHEME_V->args = NIL;
3748 s_goto (OP_BEGIN); 3767 s_goto (OP_BEGIN);
3749 } 3768 }
3750 3769
3751 case OP_LET0REC: /* letrec */ 3770 case OP_LET0REC: /* letrec */
3754 SCHEME_V->value = SCHEME_V->code; 3773 SCHEME_V->value = SCHEME_V->code;
3755 SCHEME_V->code = car (SCHEME_V->code); 3774 SCHEME_V->code = car (SCHEME_V->code);
3756 s_goto (OP_LET1REC); 3775 s_goto (OP_LET1REC);
3757 3776
3758 case OP_LET1REC: /* letrec (calculate parameters) */ 3777 case OP_LET1REC: /* letrec (calculate parameters) */
3759 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3778 args = cons (SCHEME_V->value, args);
3760 3779
3761 if (is_pair (SCHEME_V->code)) /* continue */ 3780 if (is_pair (SCHEME_V->code)) /* continue */
3762 { 3781 {
3763 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3782 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)); 3783 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3765 3784
3766 s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code)); 3785 s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code));
3767 SCHEME_V->code = cadar (SCHEME_V->code); 3786 SCHEME_V->code = cadar (SCHEME_V->code);
3768 SCHEME_V->args = NIL; 3787 SCHEME_V->args = NIL;
3769 s_goto (OP_EVAL); 3788 s_goto (OP_EVAL);
3770 } 3789 }
3771 else /* end */ 3790 else /* end */
3772 { 3791 {
3773 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3792 args = reverse_in_place (SCHEME_A_ NIL, args);
3774 SCHEME_V->code = car (SCHEME_V->args); 3793 SCHEME_V->code = car (args);
3775 SCHEME_V->args = cdr (SCHEME_V->args); 3794 SCHEME_V->args = cdr (args);
3776 s_goto (OP_LET2REC); 3795 s_goto (OP_LET2REC);
3777 } 3796 }
3778 3797
3779 case OP_LET2REC: /* letrec */ 3798 case OP_LET2REC: /* letrec */
3780 for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y)) 3799 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)); 3800 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3782 3801
3783 SCHEME_V->code = cdr (SCHEME_V->code); 3802 SCHEME_V->code = cdr (SCHEME_V->code);
3784 SCHEME_V->args = NIL; 3803 SCHEME_V->args = NIL;
3785 s_goto (OP_BEGIN); 3804 s_goto (OP_BEGIN);
3871 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 3890 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3872 SCHEME_V->code = car (SCHEME_V->code); 3891 SCHEME_V->code = car (SCHEME_V->code);
3873 s_goto (OP_EVAL); 3892 s_goto (OP_EVAL);
3874 3893
3875 case OP_C1STREAM: /* cons-stream */ 3894 case OP_C1STREAM: /* cons-stream */
3876 SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register SCHEME_V->args for gc */ 3895 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); 3896 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir);
3878 set_typeflag (x, T_PROMISE); 3897 set_typeflag (x, T_PROMISE);
3879 s_return (cons (SCHEME_V->args, x)); 3898 s_return (cons (args, x));
3880 3899
3881 case OP_MACRO0: /* macro */ 3900 case OP_MACRO0: /* macro */
3882 if (is_pair (car (SCHEME_V->code))) 3901 if (is_pair (car (SCHEME_V->code)))
3883 { 3902 {
3884 x = caar (SCHEME_V->code); 3903 x = caar (SCHEME_V->code);
3917 { 3936 {
3918 if (!is_pair (y = caar (x))) 3937 if (!is_pair (y = caar (x)))
3919 break; 3938 break;
3920 3939
3921 for (; y != NIL; y = cdr (y)) 3940 for (; y != NIL; y = cdr (y))
3922 {
3923 if (eqv (car (y), SCHEME_V->value)) 3941 if (eqv (car (y), SCHEME_V->value))
3924 break; 3942 break;
3925 }
3926 3943
3927 if (y != NIL) 3944 if (y != NIL)
3928 break; 3945 break;
3929 } 3946 }
3930 3947
3950 s_goto (OP_BEGIN); 3967 s_goto (OP_BEGIN);
3951 else 3968 else
3952 s_return (NIL); 3969 s_return (NIL);
3953 3970
3954 case OP_PAPPLY: /* apply */ 3971 case OP_PAPPLY: /* apply */
3955 SCHEME_V->code = car (SCHEME_V->args); 3972 SCHEME_V->code = car (args);
3956 SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); 3973 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
3957 /*SCHEME_V->args = cadr(SCHEME_V->args); */ 3974 /*SCHEME_V->args = cadr(args); */
3958 s_goto (OP_APPLY); 3975 s_goto (OP_APPLY);
3959 3976
3960 case OP_PEVAL: /* eval */ 3977 case OP_PEVAL: /* eval */
3961 if (cdr (SCHEME_V->args) != NIL) 3978 if (cdr (args) != NIL)
3962 SCHEME_V->envir = cadr (SCHEME_V->args); 3979 SCHEME_V->envir = cadr (args);
3963 3980
3964 SCHEME_V->code = car (SCHEME_V->args); 3981 SCHEME_V->code = car (args);
3965 s_goto (OP_EVAL); 3982 s_goto (OP_EVAL);
3966 3983
3967 case OP_CONTINUATION: /* call-with-current-continuation */ 3984 case OP_CONTINUATION: /* call-with-current-continuation */
3968 SCHEME_V->code = car (SCHEME_V->args); 3985 SCHEME_V->code = car (args);
3969 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); 3986 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
3970 s_goto (OP_APPLY); 3987 s_goto (OP_APPLY);
3971 } 3988 }
3972 3989
3973 return S_T; 3990 if (USE_ERROR_CHECKING) abort ();
3974} 3991}
3975 3992
3976static pointer 3993static int
3977opexe_2 (SCHEME_P_ enum scheme_opcodes op) 3994opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3978{ 3995{
3979 pointer x; 3996 pointer args = SCHEME_V->args;
3997 pointer x = car (args);
3980 num v; 3998 num v;
3981 3999
4000 switch (op)
4001 {
3982#if USE_MATH 4002#if USE_MATH
3983 RVALUE dd;
3984#endif
3985
3986 switch (op)
3987 {
3988#if USE_MATH
3989
3990 case OP_INEX2EX: /* inexact->exact */ 4003 case OP_INEX2EX: /* inexact->exact */
3991 x = car (SCHEME_V->args);
3992
3993 if (num_is_integer (x)) 4004 if (!is_integer (x))
3994 s_return (x); 4005 {
3995 else if (modf (rvalue_unchecked (x), &dd) == 0.0) 4006 RVALUE r = rvalue_unchecked (x);
3996 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4007
4008 if (r == (RVALUE)(IVALUE)r)
4009 x = mk_integer (SCHEME_A_ rvalue_unchecked (x));
3997 else 4010 else
3998 Error_1 ("inexact->exact: not integral:", x); 4011 Error_1 ("inexact->exact: not integral:", x);
4012 }
3999 4013
4000 case OP_EXP: 4014 s_return (x);
4001 x = car (SCHEME_V->args); 4015
4016 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4017 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4018 case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x))));
4019 case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x))));
4020
4021 case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4002 s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4022 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)))); 4023 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))
4007 4024 / (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)))); 4025 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)))); 4026 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)))); 4027 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)))); 4028 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)))); 4029 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x))));
4027 4030
4028 case OP_ATAN: 4031 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)))); 4032 s_return (mk_real (SCHEME_A_
4033 cdr (args) == NIL
4034 ? atan (rvalue (x))
4035 : atan2 (rvalue (x), rvalue (cadr (args)))));
4043 4036
4044 case OP_EXPT: 4037 case OP_EXPT:
4045 { 4038 {
4046 RVALUE result; 4039 RVALUE result;
4047 int real_result = 1; 4040 int real_result = 1;
4048 pointer y = cadr (SCHEME_V->args); 4041 pointer y = cadr (args);
4049 4042
4050 x = car (SCHEME_V->args);
4051
4052 if (num_is_integer (x) && num_is_integer (y)) 4043 if (is_integer (x) && is_integer (y))
4053 real_result = 0; 4044 real_result = 0;
4054 4045
4055 /* This 'if' is an R5RS compatibility fix. */ 4046 /* This 'if' is an R5RS compatibility fix. */
4056 /* NOTE: Remove this 'if' fix for R6RS. */ 4047 /* NOTE: Remove this 'if' fix for R6RS. */
4057 if (rvalue (x) == 0 && rvalue (y) < 0) 4048 if (rvalue (x) == 0 && rvalue (y) < 0)
4058 result = 0.0; 4049 result = 0;
4059 else 4050 else
4060 result = pow (rvalue (x), rvalue (y)); 4051 result = pow (rvalue (x), rvalue (y));
4061 4052
4062 /* Before returning integer result make sure we can. */ 4053 /* Before returning integer result make sure we can. */
4063 /* If the test fails, result is too big for integer. */ 4054 /* If the test fails, result is too big for integer. */
4064 if (!real_result) 4055 if (!real_result)
4065 { 4056 {
4066 long result_as_long = (long) result; 4057 long result_as_long = result;
4067 4058
4068 if (result != (RVALUE) result_as_long) 4059 if (result != result_as_long)
4069 real_result = 1; 4060 real_result = 1;
4070 } 4061 }
4071 4062
4072 if (real_result) 4063 if (real_result)
4073 s_return (mk_real (SCHEME_A_ result)); 4064 s_return (mk_real (SCHEME_A_ result));
4074 else 4065 else
4075 s_return (mk_integer (SCHEME_A_ result)); 4066 s_return (mk_integer (SCHEME_A_ result));
4076 } 4067 }
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 4068#endif
4107 4069
4108 case OP_ADD: /* + */ 4070 case OP_ADD: /* + */
4109 v = num_zero; 4071 v = num_zero;
4110 4072
4111 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4073 for (x = args; x != NIL; x = cdr (x))
4112 v = num_add (v, nvalue (car (x))); 4074 v = num_op (NUM_ADD, v, nvalue (car (x)));
4113 4075
4114 s_return (mk_number (SCHEME_A_ v)); 4076 s_return (mk_number (SCHEME_A_ v));
4115 4077
4116 case OP_MUL: /* * */ 4078 case OP_MUL: /* * */
4117 v = num_one; 4079 v = num_one;
4118 4080
4119 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4081 for (x = args; x != NIL; x = cdr (x))
4120 v = num_mul (v, nvalue (car (x))); 4082 v = num_op (NUM_MUL, v, nvalue (car (x)));
4121 4083
4122 s_return (mk_number (SCHEME_A_ v)); 4084 s_return (mk_number (SCHEME_A_ v));
4123 4085
4124 case OP_SUB: /* - */ 4086 case OP_SUB: /* - */
4125 if (cdr (SCHEME_V->args) == NIL) 4087 if (cdr (args) == NIL)
4126 { 4088 {
4127 x = SCHEME_V->args; 4089 x = args;
4128 v = num_zero; 4090 v = num_zero;
4129 } 4091 }
4130 else 4092 else
4131 { 4093 {
4132 x = cdr (SCHEME_V->args); 4094 x = cdr (args);
4133 v = nvalue (car (SCHEME_V->args)); 4095 v = nvalue (car (args));
4134 } 4096 }
4135 4097
4136 for (; x != NIL; x = cdr (x)) 4098 for (; x != NIL; x = cdr (x))
4137 v = num_sub (v, nvalue (car (x))); 4099 v = num_op (NUM_SUB, v, nvalue (car (x)));
4138 4100
4139 s_return (mk_number (SCHEME_A_ v)); 4101 s_return (mk_number (SCHEME_A_ v));
4140 4102
4141 case OP_DIV: /* / */ 4103 case OP_DIV: /* / */
4142 if (cdr (SCHEME_V->args) == NIL) 4104 if (cdr (args) == NIL)
4143 { 4105 {
4144 x = SCHEME_V->args; 4106 x = args;
4145 v = num_one; 4107 v = num_one;
4146 } 4108 }
4147 else 4109 else
4148 { 4110 {
4149 x = cdr (SCHEME_V->args); 4111 x = cdr (args);
4150 v = nvalue (car (SCHEME_V->args)); 4112 v = nvalue (car (args));
4151 } 4113 }
4152 4114
4153 for (; x != NIL; x = cdr (x)) 4115 for (; x != NIL; x = cdr (x))
4154 {
4155 if (!is_zero_rvalue (rvalue (car (x)))) 4116 if (!is_zero_rvalue (rvalue (car (x))))
4156 v = num_div (v, nvalue (car (x))); 4117 v = num_div (v, nvalue (car (x)));
4157 else 4118 else
4158 Error_0 ("/: division by zero"); 4119 Error_0 ("/: division by zero");
4159 }
4160 4120
4161 s_return (mk_number (SCHEME_A_ v)); 4121 s_return (mk_number (SCHEME_A_ v));
4162 4122
4163 case OP_INTDIV: /* quotient */ 4123 case OP_INTDIV: /* quotient */
4164 if (cdr (SCHEME_V->args) == NIL) 4124 if (cdr (args) == NIL)
4165 { 4125 {
4166 x = SCHEME_V->args; 4126 x = args;
4167 v = num_one; 4127 v = num_one;
4168 } 4128 }
4169 else 4129 else
4170 { 4130 {
4171 x = cdr (SCHEME_V->args); 4131 x = cdr (args);
4172 v = nvalue (car (SCHEME_V->args)); 4132 v = nvalue (car (args));
4173 } 4133 }
4174 4134
4175 for (; x != NIL; x = cdr (x)) 4135 for (; x != NIL; x = cdr (x))
4176 { 4136 {
4177 if (ivalue (car (x)) != 0) 4137 if (ivalue (car (x)) != 0)
4178 v = num_intdiv (v, nvalue (car (x))); 4138 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4179 else 4139 else
4180 Error_0 ("quotient: division by zero"); 4140 Error_0 ("quotient: division by zero");
4181 } 4141 }
4182 4142
4183 s_return (mk_number (SCHEME_A_ v)); 4143 s_return (mk_number (SCHEME_A_ v));
4184 4144
4185 case OP_REM: /* remainder */ 4145 case OP_REM: /* remainder */
4186 v = nvalue (car (SCHEME_V->args)); 4146 v = nvalue (x);
4187 4147
4188 if (ivalue (cadr (SCHEME_V->args)) != 0) 4148 if (ivalue (cadr (args)) != 0)
4189 v = num_rem (v, nvalue (cadr (SCHEME_V->args))); 4149 v = num_rem (v, nvalue (cadr (args)));
4190 else 4150 else
4191 Error_0 ("remainder: division by zero"); 4151 Error_0 ("remainder: division by zero");
4192 4152
4193 s_return (mk_number (SCHEME_A_ v)); 4153 s_return (mk_number (SCHEME_A_ v));
4194 4154
4195 case OP_MOD: /* modulo */ 4155 case OP_MOD: /* modulo */
4196 v = nvalue (car (SCHEME_V->args)); 4156 v = nvalue (x);
4197 4157
4198 if (ivalue (cadr (SCHEME_V->args)) != 0) 4158 if (ivalue (cadr (args)) != 0)
4199 v = num_mod (v, nvalue (cadr (SCHEME_V->args))); 4159 v = num_mod (v, nvalue (cadr (args)));
4200 else 4160 else
4201 Error_0 ("modulo: division by zero"); 4161 Error_0 ("modulo: division by zero");
4202 4162
4203 s_return (mk_number (SCHEME_A_ v)); 4163 s_return (mk_number (SCHEME_A_ v));
4204 4164
4205 case OP_CAR: /* car */ 4165 /* the compiler will optimize this mess... */
4206 s_return (caar (SCHEME_V->args)); 4166 case OP_CAR: op_car: s_return (car (x));
4207 4167 case OP_CDR: op_cdr: s_return (cdr (x));
4208 case OP_CDR: /* cdr */ 4168 case OP_CAAR: op_caar: x = car (x); goto op_car;
4209 s_return (cdar (SCHEME_V->args)); 4169 case OP_CADR: op_cadr: x = cdr (x); goto op_car;
4170 case OP_CDAR: op_cdar: x = car (x); goto op_cdr;
4171 case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr;
4172 case OP_CAAAR: op_caaar: x = car (x); goto op_caar;
4173 case OP_CAADR: op_caadr: x = cdr (x); goto op_caar;
4174 case OP_CADAR: op_cadar: x = car (x); goto op_cadr;
4175 case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr;
4176 case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar;
4177 case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar;
4178 case OP_CDDAR: op_cddar: x = car (x); goto op_cddr;
4179 case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr;
4180 case OP_CAAAAR: x = car (x); goto op_caaar;
4181 case OP_CAAADR: x = cdr (x); goto op_caaar;
4182 case OP_CAADAR: x = car (x); goto op_caadr;
4183 case OP_CAADDR: x = cdr (x); goto op_caadr;
4184 case OP_CADAAR: x = car (x); goto op_cadar;
4185 case OP_CADADR: x = cdr (x); goto op_cadar;
4186 case OP_CADDAR: x = car (x); goto op_caddr;
4187 case OP_CADDDR: x = cdr (x); goto op_caddr;
4188 case OP_CDAAAR: x = car (x); goto op_cdaar;
4189 case OP_CDAADR: x = cdr (x); goto op_cdaar;
4190 case OP_CDADAR: x = car (x); goto op_cdadr;
4191 case OP_CDADDR: x = cdr (x); goto op_cdadr;
4192 case OP_CDDAAR: x = car (x); goto op_cddar;
4193 case OP_CDDADR: x = cdr (x); goto op_cddar;
4194 case OP_CDDDAR: x = car (x); goto op_cdddr;
4195 case OP_CDDDDR: x = cdr (x); goto op_cdddr;
4210 4196
4211 case OP_CONS: /* cons */ 4197 case OP_CONS: /* cons */
4212 set_cdr (SCHEME_V->args, cadr (SCHEME_V->args)); 4198 set_cdr (args, cadr (args));
4213 s_return (SCHEME_V->args); 4199 s_return (args);
4214 4200
4215 case OP_SETCAR: /* set-car! */ 4201 case OP_SETCAR: /* set-car! */
4216 if (!is_immutable (car (SCHEME_V->args))) 4202 if (!is_immutable (x))
4217 { 4203 {
4218 set_car (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4204 set_car (x, cadr (args));
4219 s_return (car (SCHEME_V->args)); 4205 s_return (car (args));
4220 } 4206 }
4221 else 4207 else
4222 Error_0 ("set-car!: unable to alter immutable pair"); 4208 Error_0 ("set-car!: unable to alter immutable pair");
4223 4209
4224 case OP_SETCDR: /* set-cdr! */ 4210 case OP_SETCDR: /* set-cdr! */
4225 if (!is_immutable (car (SCHEME_V->args))) 4211 if (!is_immutable (x))
4226 { 4212 {
4227 set_cdr (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4213 set_cdr (x, cadr (args));
4228 s_return (car (SCHEME_V->args)); 4214 s_return (car (args));
4229 } 4215 }
4230 else 4216 else
4231 Error_0 ("set-cdr!: unable to alter immutable pair"); 4217 Error_0 ("set-cdr!: unable to alter immutable pair");
4232 4218
4233 case OP_CHAR2INT: /* char->integer */ 4219 case OP_CHAR2INT: /* char->integer */
4234 s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4220 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4235 4221
4236 case OP_INT2CHAR: /* integer->char */ 4222 case OP_INT2CHAR: /* integer->char */
4237 s_return (mk_character (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4223 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4238 4224
4239 case OP_CHARUPCASE: 4225 case OP_CHARUPCASE:
4240 { 4226 {
4241 unsigned char c = ivalue (car (SCHEME_V->args)); 4227 unsigned char c = ivalue_unchecked (x);
4242 c = toupper (c); 4228 c = toupper (c);
4243 s_return (mk_character (SCHEME_A_ c)); 4229 s_return (mk_character (SCHEME_A_ c));
4244 } 4230 }
4245 4231
4246 case OP_CHARDNCASE: 4232 case OP_CHARDNCASE:
4247 { 4233 {
4248 unsigned char c = ivalue (car (SCHEME_V->args)); 4234 unsigned char c = ivalue_unchecked (x);
4249 c = tolower (c); 4235 c = tolower (c);
4250 s_return (mk_character (SCHEME_A_ c)); 4236 s_return (mk_character (SCHEME_A_ c));
4251 } 4237 }
4252 4238
4253 case OP_STR2SYM: /* string->symbol */ 4239 case OP_STR2SYM: /* string->symbol */
4254 s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); 4240 s_return (mk_symbol (SCHEME_A_ strvalue (x)));
4255 4241
4256 case OP_STR2ATOM: /* string->atom */ 4242 case OP_STR2ATOM: /* string->atom */
4257 { 4243 {
4258 char *s = strvalue (car (SCHEME_V->args)); 4244 char *s = strvalue (x);
4259 long pf = 0; 4245 long pf = 0;
4260 4246
4261 if (cdr (SCHEME_V->args) != NIL) 4247 if (cdr (args) != NIL)
4262 { 4248 {
4263 /* we know cadr(SCHEME_V->args) is a natural number */ 4249 /* we know cadr(args) is a natural number */
4264 /* see if it is 2, 8, 10, or 16, or error */ 4250 /* see if it is 2, 8, 10, or 16, or error */
4265 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4251 pf = ivalue_unchecked (cadr (args));
4266 4252
4267 if (pf == 16 || pf == 10 || pf == 8 || pf == 2) 4253 if (pf == 16 || pf == 10 || pf == 8 || pf == 2)
4268 { 4254 {
4269 /* base is OK */ 4255 /* base is OK */
4270 } 4256 }
4271 else 4257 else
4272 pf = -1; 4258 pf = -1;
4273 } 4259 }
4274 4260
4275 if (pf < 0) 4261 if (pf < 0)
4276 Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); 4262 Error_1 ("string->atom: bad base:", cadr (args));
4277 else if (*s == '#') /* no use of base! */ 4263 else if (*s == '#') /* no use of base! */
4278 s_return (mk_sharp_const (SCHEME_A_ s + 1)); 4264 s_return (mk_sharp_const (SCHEME_A_ s + 1));
4279 else 4265 else
4280 { 4266 {
4281 if (pf == 0 || pf == 10) 4267 if (pf == 0 || pf == 10)
4292 } 4278 }
4293 } 4279 }
4294 } 4280 }
4295 4281
4296 case OP_SYM2STR: /* symbol->string */ 4282 case OP_SYM2STR: /* symbol->string */
4297 x = mk_string (SCHEME_A_ symname (car (SCHEME_V->args))); 4283 x = mk_string (SCHEME_A_ symname (x));
4298 setimmutable (x); 4284 setimmutable (x);
4299 s_return (x); 4285 s_return (x);
4300 4286
4301 case OP_ATOM2STR: /* atom->string */ 4287 case OP_ATOM2STR: /* atom->string */
4302 { 4288 {
4303 long pf = 0; 4289 long pf = 0;
4304 4290
4305 x = car (SCHEME_V->args);
4306
4307 if (cdr (SCHEME_V->args) != NIL) 4291 if (cdr (args) != NIL)
4308 { 4292 {
4309 /* we know cadr(SCHEME_V->args) is a natural number */ 4293 /* we know cadr(args) is a natural number */
4310 /* see if it is 2, 8, 10, or 16, or error */ 4294 /* see if it is 2, 8, 10, or 16, or error */
4311 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4295 pf = ivalue_unchecked (cadr (args));
4312 4296
4313 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) 4297 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2))
4314 { 4298 {
4315 /* base is OK */ 4299 /* base is OK */
4316 } 4300 }
4317 else 4301 else
4318 pf = -1; 4302 pf = -1;
4319 } 4303 }
4320 4304
4321 if (pf < 0) 4305 if (pf < 0)
4322 Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); 4306 Error_1 ("atom->string: bad base:", cadr (args));
4323 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) 4307 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x))
4324 { 4308 {
4325 char *p; 4309 char *p;
4326 int len; 4310 int len;
4327 4311
4332 Error_1 ("atom->string: not an atom:", x); 4316 Error_1 ("atom->string: not an atom:", x);
4333 } 4317 }
4334 4318
4335 case OP_MKSTRING: /* make-string */ 4319 case OP_MKSTRING: /* make-string */
4336 { 4320 {
4337 int fill = ' '; 4321 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4338 int len; 4322 int len = ivalue_unchecked (x);
4339 4323
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)); 4324 s_return (mk_empty_string (SCHEME_A_ len, fill));
4346 } 4325 }
4347 4326
4348 case OP_STRLEN: /* string-length */ 4327 case OP_STRLEN: /* string-length */
4349 s_return (mk_integer (SCHEME_A_ strlength (car (SCHEME_V->args)))); 4328 s_return (mk_integer (SCHEME_A_ strlength (x)));
4350 4329
4351 case OP_STRREF: /* string-ref */ 4330 case OP_STRREF: /* string-ref */
4352 { 4331 {
4353 char *str; 4332 char *str = strvalue (x);
4354 int index;
4355
4356 str = strvalue (car (SCHEME_V->args));
4357
4358 index = ivalue (cadr (SCHEME_V->args)); 4333 int index = ivalue_unchecked (cadr (args));
4359 4334
4360 if (index >= strlength (car (SCHEME_V->args))) 4335 if (index >= strlength (x))
4361 Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); 4336 Error_1 ("string-ref: out of bounds:", cadr (args));
4362 4337
4363 s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); 4338 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4364 } 4339 }
4365 4340
4366 case OP_STRSET: /* string-set! */ 4341 case OP_STRSET: /* string-set! */
4367 { 4342 {
4368 char *str; 4343 char *str = strvalue (x);
4369 int index; 4344 int index = ivalue_unchecked (cadr (args));
4370 int c; 4345 int c;
4371 4346
4372 if (is_immutable (car (SCHEME_V->args))) 4347 if (is_immutable (x))
4373 Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); 4348 Error_1 ("string-set!: unable to alter immutable string:", x);
4374 4349
4375 str = strvalue (car (SCHEME_V->args));
4376
4377 index = ivalue (cadr (SCHEME_V->args));
4378
4379 if (index >= strlength (car (SCHEME_V->args))) 4350 if (index >= strlength (x))
4380 Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); 4351 Error_1 ("string-set!: out of bounds:", cadr (args));
4381 4352
4382 c = charvalue (caddr (SCHEME_V->args)); 4353 c = charvalue (caddr (args));
4383 4354
4384 str[index] = (char) c; 4355 str[index] = c;
4385 s_return (car (SCHEME_V->args)); 4356 s_return (car (args));
4386 } 4357 }
4387 4358
4388 case OP_STRAPPEND: /* string-append */ 4359 case OP_STRAPPEND: /* string-append */
4389 { 4360 {
4390 /* in 1.29 string-append was in Scheme in init.scm but was too slow */ 4361 /* in 1.29 string-append was in Scheme in init.scm but was too slow */
4391 int len = 0; 4362 int len = 0;
4392 pointer newstr; 4363 pointer newstr;
4393 char *pos; 4364 char *pos;
4394 4365
4395 /* compute needed length for new string */ 4366 /* compute needed length for new string */
4396 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4367 for (x = args; x != NIL; x = cdr (x))
4397 len += strlength (car (x)); 4368 len += strlength (car (x));
4398 4369
4399 newstr = mk_empty_string (SCHEME_A_ len, ' '); 4370 newstr = mk_empty_string (SCHEME_A_ len, ' ');
4400 4371
4401 /* store the contents of the argument strings into the new string */ 4372 /* 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)) 4373 for (pos = strvalue (newstr), x = args; x != NIL; pos += strlength (car (x)), x = cdr (x))
4403 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4374 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4404 4375
4405 s_return (newstr); 4376 s_return (newstr);
4406 } 4377 }
4407 4378
4408 case OP_SUBSTR: /* substring */ 4379 case OP_SUBSTR: /* substring */
4409 { 4380 {
4410 char *str; 4381 char *str = strvalue (x);
4411 int index0; 4382 int index0 = ivalue_unchecked (cadr (args));
4412 int index1; 4383 int index1;
4413 int len; 4384 int len;
4414 4385
4415 str = strvalue (car (SCHEME_V->args));
4416
4417 index0 = ivalue (cadr (SCHEME_V->args));
4418
4419 if (index0 > strlength (car (SCHEME_V->args))) 4386 if (index0 > strlength (x))
4420 Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); 4387 Error_1 ("substring: start out of bounds:", cadr (args));
4421 4388
4422 if (cddr (SCHEME_V->args) != NIL) 4389 if (cddr (args) != NIL)
4423 { 4390 {
4424 index1 = ivalue (caddr (SCHEME_V->args)); 4391 index1 = ivalue_unchecked (caddr (args));
4425 4392
4426 if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) 4393 if (index1 > strlength (x) || index1 < index0)
4427 Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); 4394 Error_1 ("substring: end out of bounds:", caddr (args));
4428 } 4395 }
4429 else 4396 else
4430 index1 = strlength (car (SCHEME_V->args)); 4397 index1 = strlength (x);
4431 4398
4432 len = index1 - index0; 4399 len = index1 - index0;
4433 x = mk_empty_string (SCHEME_A_ len, ' '); 4400 x = mk_empty_string (SCHEME_A_ len, ' ');
4434 memcpy (strvalue (x), str + index0, len); 4401 memcpy (strvalue (x), str + index0, len);
4435 strvalue (x)[len] = 0; 4402 strvalue (x)[len] = 0;
4439 4406
4440 case OP_VECTOR: /* vector */ 4407 case OP_VECTOR: /* vector */
4441 { 4408 {
4442 int i; 4409 int i;
4443 pointer vec; 4410 pointer vec;
4444 int len = list_length (SCHEME_A_ SCHEME_V->args); 4411 int len = list_length (SCHEME_A_ args);
4445 4412
4446 if (len < 0) 4413 if (len < 0)
4447 Error_1 ("vector: not a proper list:", SCHEME_V->args); 4414 Error_1 ("vector: not a proper list:", args);
4448 4415
4449 vec = mk_vector (SCHEME_A_ len); 4416 vec = mk_vector (SCHEME_A_ len);
4450 4417
4451#if USE_ERROR_CHECKING 4418#if USE_ERROR_CHECKING
4452 if (SCHEME_V->no_memory) 4419 if (SCHEME_V->no_memory)
4453 s_return (S_SINK); 4420 s_return (S_SINK);
4454#endif 4421#endif
4455 4422
4456 for (x = SCHEME_V->args, i = 0; is_pair (x); x = cdr (x), i++) 4423 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4457 set_vector_elem (vec, i, car (x)); 4424 vector_set (vec, i, car (x));
4458 4425
4459 s_return (vec); 4426 s_return (vec);
4460 } 4427 }
4461 4428
4462 case OP_MKVECTOR: /* make-vector */ 4429 case OP_MKVECTOR: /* make-vector */
4463 { 4430 {
4464 pointer fill = NIL; 4431 pointer fill = NIL;
4465 int len;
4466 pointer vec; 4432 pointer vec;
4433 int len = ivalue_unchecked (x);
4467 4434
4468 len = ivalue (car (SCHEME_V->args));
4469
4470 if (cdr (SCHEME_V->args) != NIL) 4435 if (cdr (args) != NIL)
4471 fill = cadr (SCHEME_V->args); 4436 fill = cadr (args);
4472 4437
4473 vec = mk_vector (SCHEME_A_ len); 4438 vec = mk_vector (SCHEME_A_ len);
4474 4439
4475#if USE_ERROR_CHECKING 4440#if USE_ERROR_CHECKING
4476 if (SCHEME_V->no_memory) 4441 if (SCHEME_V->no_memory)
4477 s_return (S_SINK); 4442 s_return (S_SINK);
4478#endif 4443#endif
4479 4444
4480 if (fill != NIL) 4445 if (fill != NIL)
4481 fill_vector (vec, fill); 4446 fill_vector (vec, 0, fill);
4482 4447
4483 s_return (vec); 4448 s_return (vec);
4484 } 4449 }
4485 4450
4486 case OP_VECLEN: /* vector-length */ 4451 case OP_VECLEN: /* vector-length */
4487 s_return (mk_integer (SCHEME_A_ veclength (car (SCHEME_V->args)))); 4452 s_return (mk_integer (SCHEME_A_ veclength (x)));
4453
4454 case OP_VECRESIZE:
4455 vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args));
4456 s_return (x);
4488 4457
4489 case OP_VECREF: /* vector-ref */ 4458 case OP_VECREF: /* vector-ref */
4490 { 4459 {
4491 int index;
4492
4493 index = ivalue (cadr (SCHEME_V->args)); 4460 int index = ivalue_unchecked (cadr (args));
4494 4461
4495 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) 4462 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4496 Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); 4463 Error_1 ("vector-ref: out of bounds:", cadr (args));
4497 4464
4498 s_return (vector_elem (car (SCHEME_V->args), index)); 4465 s_return (vector_get (x, index));
4499 } 4466 }
4500 4467
4501 case OP_VECSET: /* vector-set! */ 4468 case OP_VECSET: /* vector-set! */
4502 { 4469 {
4503 int index; 4470 int index = ivalue_unchecked (cadr (args));
4504 4471
4505 if (is_immutable (car (SCHEME_V->args))) 4472 if (is_immutable (x))
4506 Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); 4473 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4507 4474
4508 index = ivalue (cadr (SCHEME_V->args));
4509
4510 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) 4475 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4511 Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); 4476 Error_1 ("vector-set!: out of bounds:", cadr (args));
4512 4477
4513 set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); 4478 vector_set (x, index, caddr (args));
4514 s_return (car (SCHEME_V->args)); 4479 s_return (x);
4515 } 4480 }
4516 } 4481 }
4517 4482
4518 return S_T; 4483 if (USE_ERROR_CHECKING) abort ();
4519} 4484}
4520 4485
4521INTERFACE int 4486static int
4522is_list (SCHEME_P_ pointer a) 4487opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4523{ 4488{
4524 return list_length (SCHEME_A_ a) >= 0; 4489 pointer x = SCHEME_V->args;
4525}
4526 4490
4527/* Result is: 4491 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 { 4492 {
4493 num v = nvalue (car (x));
4494 x = cdr (x);
4495
4543 if (fast == NIL) 4496 if (x == NIL)
4544 return i; 4497 break;
4545 4498
4546 if (!is_pair (fast)) 4499 int r = num_cmp (v, nvalue (car (x)));
4547 return -2 - i;
4548 4500
4549 fast = cdr (fast); 4501 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 { 4502 {
4567 /* the fast pointer has looped back around and caught up 4503 case OP_NUMEQ: r = r == 0; break;
4568 with the slow pointer, hence the structure is circular, 4504 case OP_LESS: r = r < 0; break;
4569 not of finite length, and therefore not a list */ 4505 case OP_GRE: r = r > 0; break;
4570 return -1; 4506 case OP_LEQ: r = r <= 0; break;
4507 case OP_GEQ: r = r >= 0; break;
4571 } 4508 }
4572 }
4573}
4574 4509
4510 if (!r)
4511 s_return (S_F);
4512 }
4513
4514 s_return (S_T);
4515}
4516
4575static pointer 4517static int
4576opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4518opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4577{ 4519{
4578 pointer x; 4520 pointer args = SCHEME_V->args;
4579 num v; 4521 pointer a = car (args);
4580 int (*comp_func) (num, num); 4522 pointer d = cdr (args);
4523 int r;
4581 4524
4582 switch (op) 4525 switch (op)
4583 { 4526 {
4584 case OP_NOT: /* not */ 4527 case OP_NOT: /* not */ r = is_false (a) ; break;
4585 s_retbool (is_false (car (SCHEME_V->args))); 4528 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break;
4529 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break;
4530 case OP_NULLP: /* null? */ r = a == NIL ; break;
4531 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break;
4532 case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break;
4533 case OP_NUMBERP: /* number? */ r = is_number (a) ; break;
4534 case OP_STRINGP: /* string? */ r = is_string (a) ; break;
4535 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4536 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4537 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4586 4538
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 4539#if USE_CHAR_CLASSIFIERS
4656 4540 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4657 case OP_CHARAP: /* char-alphabetic? */ 4541 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4658 s_retbool (Cisalpha (ivalue (car (SCHEME_V->args)))); 4542 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4659 4543 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4660 case OP_CHARNP: /* char-numeric? */ 4544 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 4545#endif
4546
4672#if USE_PORTS 4547#if USE_PORTS
4673 4548 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? */ 4549 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? */ 4550 case OP_OUTPORTP: /* output-port? */ r = is_outport (a); break;
4681 s_retbool (is_outport (car (SCHEME_V->args)));
4682#endif 4551#endif
4683 4552
4684 case OP_PROCP: /* procedure? */ 4553 case OP_PROCP: /* procedure? */
4685 4554
4686 /*-- 4555 /*--
4687 * continuation should be procedure by the example 4556 * continuation should be procedure by the example
4688 * (call-with-current-continuation procedure?) ==> #t 4557 * (call-with-current-continuation procedure?) ==> #t
4689 * in R^3 report sec. 6.9 4558 * in R^3 report sec. 6.9
4690 */ 4559 */
4691 s_retbool (is_proc (car (SCHEME_V->args)) || is_closure (car (SCHEME_V->args)) 4560 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))); 4561 break;
4693 4562
4694 case OP_PAIRP: /* pair? */ 4563 case OP_PAIRP: /* pair? */ r = is_pair (a) ; break;
4695 s_retbool (is_pair (car (SCHEME_V->args))); 4564 case OP_LISTP: /* list? */ r = list_length (SCHEME_A_ a) >= 0; break;
4696 4565 case OP_ENVP: /* environment? */ r = is_environment (a) ; break;
4697 case OP_LISTP: /* list? */ 4566 case OP_VECTORP: /* vector? */ r = is_vector (a) ; break;
4698 s_retbool (list_length (SCHEME_A_ car (SCHEME_V->args)) >= 0); 4567 case OP_EQ: /* eq? */ r = a == cadr (args) ; break;
4699 4568 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 } 4569 }
4712 4570
4713 return S_T; 4571 s_retbool (r);
4714} 4572}
4715 4573
4716static pointer 4574static int
4717opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4575opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4718{ 4576{
4577 pointer args = SCHEME_V->args;
4578 pointer a = car (args);
4719 pointer x, y; 4579 pointer x, y;
4720 4580
4721 switch (op) 4581 switch (op)
4722 { 4582 {
4723 case OP_FORCE: /* force */ 4583 case OP_FORCE: /* force */
4724 SCHEME_V->code = car (SCHEME_V->args); 4584 SCHEME_V->code = a;
4725 4585
4726 if (is_promise (SCHEME_V->code)) 4586 if (is_promise (SCHEME_V->code))
4727 { 4587 {
4728 /* Should change type to closure here */ 4588 /* Should change type to closure here */
4729 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); 4589 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code);
4732 } 4592 }
4733 else 4593 else
4734 s_return (SCHEME_V->code); 4594 s_return (SCHEME_V->code);
4735 4595
4736 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4596 case OP_SAVE_FORCED: /* Save forced value replacing promise */
4737 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); 4597 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
4738 s_return (SCHEME_V->value); 4598 s_return (SCHEME_V->value);
4739 4599
4740#if USE_PORTS 4600#if USE_PORTS
4741 4601
4742 case OP_WRITE: /* write */ 4602 case OP_WRITE: /* write */
4750 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4610 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4751 SCHEME_V->outport = cadr (SCHEME_V->args); 4611 SCHEME_V->outport = cadr (SCHEME_V->args);
4752 } 4612 }
4753 } 4613 }
4754 4614
4755 SCHEME_V->args = car (SCHEME_V->args); 4615 SCHEME_V->args = a;
4756 4616
4757 if (op == OP_WRITE) 4617 if (op == OP_WRITE)
4758 SCHEME_V->print_flag = 1; 4618 SCHEME_V->print_flag = 1;
4759 else 4619 else
4760 SCHEME_V->print_flag = 0; 4620 SCHEME_V->print_flag = 0;
4761 4621
4762 s_goto (OP_P0LIST); 4622 s_goto (OP_P0LIST);
4763 4623
4764 case OP_NEWLINE: /* newline */ 4624 case OP_NEWLINE: /* newline */
4765 if (is_pair (SCHEME_V->args)) 4625 if (is_pair (args))
4766 { 4626 {
4767 if (car (SCHEME_V->args) != SCHEME_V->outport) 4627 if (a != SCHEME_V->outport)
4768 { 4628 {
4769 x = cons (SCHEME_V->outport, NIL); 4629 x = cons (SCHEME_V->outport, NIL);
4770 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4630 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4771 SCHEME_V->outport = car (SCHEME_V->args); 4631 SCHEME_V->outport = a;
4772 } 4632 }
4773 } 4633 }
4774 4634
4775 putstr (SCHEME_A_ "\n"); 4635 putstr (SCHEME_A_ "\n");
4776 s_return (S_T); 4636 s_return (S_T);
4777#endif 4637#endif
4778 4638
4779 case OP_ERR0: /* error */ 4639 case OP_ERR0: /* error */
4780 SCHEME_V->retcode = -1; 4640 SCHEME_V->retcode = -1;
4781 4641
4782 if (!is_string (car (SCHEME_V->args))) 4642 if (!is_string (a))
4783 { 4643 {
4784 SCHEME_V->args = cons (mk_string (SCHEME_A_ " -- "), SCHEME_V->args); 4644 args = cons (mk_string (SCHEME_A_ " -- "), args);
4785 setimmutable (car (SCHEME_V->args)); 4645 setimmutable (car (args));
4786 } 4646 }
4787 4647
4788 putstr (SCHEME_A_ "Error: "); 4648 putstr (SCHEME_A_ "Error: ");
4789 putstr (SCHEME_A_ strvalue (car (SCHEME_V->args))); 4649 putstr (SCHEME_A_ strvalue (car (args)));
4790 SCHEME_V->args = cdr (SCHEME_V->args); 4650 SCHEME_V->args = cdr (args);
4791 s_goto (OP_ERR1); 4651 s_goto (OP_ERR1);
4792 4652
4793 case OP_ERR1: /* error */ 4653 case OP_ERR1: /* error */
4794 putstr (SCHEME_A_ " "); 4654 putstr (SCHEME_A_ " ");
4795 4655
4796 if (SCHEME_V->args != NIL) 4656 if (args != NIL)
4797 { 4657 {
4798 s_save (SCHEME_A_ OP_ERR1, cdr (SCHEME_V->args), NIL); 4658 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4799 SCHEME_V->args = car (SCHEME_V->args); 4659 SCHEME_V->args = a;
4800 SCHEME_V->print_flag = 1; 4660 SCHEME_V->print_flag = 1;
4801 s_goto (OP_P0LIST); 4661 s_goto (OP_P0LIST);
4802 } 4662 }
4803 else 4663 else
4804 { 4664 {
4805 putstr (SCHEME_A_ "\n"); 4665 putstr (SCHEME_A_ "\n");
4806 4666
4807 if (SCHEME_V->interactive_repl) 4667 if (SCHEME_V->interactive_repl)
4808 s_goto (OP_T0LVL); 4668 s_goto (OP_T0LVL);
4809 else 4669 else
4810 return NIL; 4670 return -1;
4811 } 4671 }
4812 4672
4813 case OP_REVERSE: /* reverse */ 4673 case OP_REVERSE: /* reverse */
4814 s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); 4674 s_return (reverse (SCHEME_A_ a));
4815 4675
4816 case OP_LIST_STAR: /* list* */ 4676 case OP_LIST_STAR: /* list* */
4817 s_return (list_star (SCHEME_A_ SCHEME_V->args)); 4677 s_return (list_star (SCHEME_A_ SCHEME_V->args));
4818 4678
4819 case OP_APPEND: /* append */ 4679 case OP_APPEND: /* append */
4820 x = NIL; 4680 x = NIL;
4821 y = SCHEME_V->args; 4681 y = args;
4822 4682
4823 if (y == x) 4683 if (y == x)
4824 s_return (x); 4684 s_return (x);
4825 4685
4826 /* cdr() in the while condition is not a typo. If car() */ 4686 /* cdr() in the while condition is not a typo. If car() */
4837 s_return (reverse_in_place (SCHEME_A_ car (y), x)); 4697 s_return (reverse_in_place (SCHEME_A_ car (y), x));
4838 4698
4839#if USE_PLIST 4699#if USE_PLIST
4840 4700
4841 case OP_PUT: /* put */ 4701 case OP_PUT: /* put */
4842 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 4702 if (!hasprop (a) || !hasprop (cadr (args)))
4843 Error_0 ("illegal use of put"); 4703 Error_0 ("illegal use of put");
4844 4704
4845 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 4705 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
4846 { 4706 {
4847 if (caar (x) == y) 4707 if (caar (x) == y)
4848 break; 4708 break;
4849 } 4709 }
4850 4710
4851 if (x != NIL) 4711 if (x != NIL)
4852 cdar (x) = caddr (SCHEME_V->args); 4712 cdar (x) = caddr (args);
4853 else 4713 else
4854 symprop (car (SCHEME_V->args)) = cons (cons (y, caddr (SCHEME_V->args)), symprop (car (SCHEME_V->args))); 4714 symprop (a) = cons (cons (y, caddr (args)), symprop (a));
4855 4715
4856 s_return (S_T); 4716 s_return (S_T);
4857 4717
4858 case OP_GET: /* get */ 4718 case OP_GET: /* get */
4859 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 4719 if (!hasprop (a) || !hasprop (cadr (args)))
4860 Error_0 ("illegal use of get"); 4720 Error_0 ("illegal use of get");
4861 4721
4862 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 4722 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
4863 if (caar (x) == y) 4723 if (caar (x) == y)
4864 break; 4724 break;
4865 4725
4866 if (x != NIL) 4726 if (x != NIL)
4867 s_return (cdar (x)); 4727 s_return (cdar (x));
4869 s_return (NIL); 4729 s_return (NIL);
4870 4730
4871#endif /* USE_PLIST */ 4731#endif /* USE_PLIST */
4872 4732
4873 case OP_QUIT: /* quit */ 4733 case OP_QUIT: /* quit */
4874 if (is_pair (SCHEME_V->args)) 4734 if (is_pair (args))
4875 SCHEME_V->retcode = ivalue (car (SCHEME_V->args)); 4735 SCHEME_V->retcode = ivalue (a);
4876 4736
4877 return NIL; 4737 return -1;
4878 4738
4879 case OP_GC: /* gc */ 4739 case OP_GC: /* gc */
4880 gc (SCHEME_A_ NIL, NIL); 4740 gc (SCHEME_A_ NIL, NIL);
4881 s_return (S_T); 4741 s_return (S_T);
4882 4742
4883 case OP_GCVERB: /* gc-verbose */ 4743 case OP_GCVERB: /* gc-verbose */
4884 { 4744 {
4885 int was = SCHEME_V->gc_verbose; 4745 int was = SCHEME_V->gc_verbose;
4886 4746
4887 SCHEME_V->gc_verbose = (car (SCHEME_V->args) != S_F); 4747 SCHEME_V->gc_verbose = (a != S_F);
4888 s_retbool (was); 4748 s_retbool (was);
4889 } 4749 }
4890 4750
4891 case OP_NEWSEGMENT: /* new-segment */ 4751 case OP_NEWSEGMENT: /* new-segment */
4752#if 0
4892 if (!is_pair (SCHEME_V->args) || !is_number (car (SCHEME_V->args))) 4753 if (!is_pair (args) || !is_number (a))
4893 Error_0 ("new-segment: argument must be a number"); 4754 Error_0 ("new-segment: argument must be a number");
4894 4755#endif
4895 alloc_cellseg (SCHEME_A_ (int)ivalue (car (SCHEME_V->args))); 4756 s_retbool (alloc_cellseg (SCHEME_A));
4896
4897 s_return (S_T);
4898 4757
4899 case OP_OBLIST: /* oblist */ 4758 case OP_OBLIST: /* oblist */
4900 s_return (oblist_all_symbols (SCHEME_A)); 4759 s_return (oblist_all_symbols (SCHEME_A));
4901 4760
4902#if USE_PORTS 4761#if USE_PORTS
4927 case OP_OPEN_INOUTFILE: 4786 case OP_OPEN_INOUTFILE:
4928 prop = port_input | port_output; 4787 prop = port_input | port_output;
4929 break; 4788 break;
4930 } 4789 }
4931 4790
4932 p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); 4791 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4933 4792
4934 if (p == NIL) 4793 s_return (p == NIL ? S_F : p);
4935 s_return (S_F);
4936
4937 s_return (p);
4938 } 4794 }
4939 4795
4940# if USE_STRING_PORTS 4796# if USE_STRING_PORTS
4941 4797
4942 case OP_OPEN_INSTRING: /* open-input-string */ 4798 case OP_OPEN_INSTRING: /* open-input-string */
4954 case OP_OPEN_INOUTSTRING: 4810 case OP_OPEN_INOUTSTRING:
4955 prop = port_input | port_output; 4811 prop = port_input | port_output;
4956 break; 4812 break;
4957 } 4813 }
4958 4814
4959 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4815 p = port_from_string (SCHEME_A_ strvalue (a),
4960 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); 4816 strvalue (a) + strlength (a), prop);
4961 4817
4962 if (p == NIL) 4818 s_return (p == NIL ? S_F : p);
4963 s_return (S_F);
4964
4965 s_return (p);
4966 } 4819 }
4967 4820
4968 case OP_OPEN_OUTSTRING: /* open-output-string */ 4821 case OP_OPEN_OUTSTRING: /* open-output-string */
4969 { 4822 {
4970 pointer p; 4823 pointer p;
4971 4824
4972 if (car (SCHEME_V->args) == NIL) 4825 if (a == NIL)
4973 {
4974 p = port_from_scratch (SCHEME_A); 4826 p = port_from_scratch (SCHEME_A);
4975
4976 if (p == NIL)
4977 s_return (S_F);
4978 }
4979 else 4827 else
4980 {
4981 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4828 p = port_from_string (SCHEME_A_ strvalue (a),
4982 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); 4829 strvalue (a) + strlength (a), port_output);
4983 4830
4984 if (p == NIL) 4831 s_return (p == NIL ? S_F : p);
4985 s_return (S_F);
4986 }
4987
4988 s_return (p);
4989 } 4832 }
4990 4833
4991 case OP_GET_OUTSTRING: /* get-output-string */ 4834 case OP_GET_OUTSTRING: /* get-output-string */
4992 { 4835 {
4993 port *p; 4836 port *p = port (a);
4994 4837
4995 if ((p = car (SCHEME_V->args)->object.port)->kind & port_string) 4838 if (p->kind & port_string)
4996 { 4839 {
4997 off_t size; 4840 off_t size;
4998 char *str; 4841 char *str;
4999 4842
5000 size = p->rep.string.curr - p->rep.string.start + 1; 4843 size = p->rep.string.curr - p->rep.string.start + 1;
5016 } 4859 }
5017 4860
5018# endif 4861# endif
5019 4862
5020 case OP_CLOSE_INPORT: /* close-input-port */ 4863 case OP_CLOSE_INPORT: /* close-input-port */
5021 port_close (SCHEME_A_ car (SCHEME_V->args), port_input); 4864 port_close (SCHEME_A_ a, port_input);
5022 s_return (S_T); 4865 s_return (S_T);
5023 4866
5024 case OP_CLOSE_OUTPORT: /* close-output-port */ 4867 case OP_CLOSE_OUTPORT: /* close-output-port */
5025 port_close (SCHEME_A_ car (SCHEME_V->args), port_output); 4868 port_close (SCHEME_A_ a, port_output);
5026 s_return (S_T); 4869 s_return (S_T);
5027#endif 4870#endif
5028 4871
5029 case OP_INT_ENV: /* interaction-environment */ 4872 case OP_INT_ENV: /* interaction-environment */
5030 s_return (SCHEME_V->global_env); 4873 s_return (SCHEME_V->global_env);
5032 case OP_CURR_ENV: /* current-environment */ 4875 case OP_CURR_ENV: /* current-environment */
5033 s_return (SCHEME_V->envir); 4876 s_return (SCHEME_V->envir);
5034 4877
5035 } 4878 }
5036 4879
5037 return S_T; 4880 if (USE_ERROR_CHECKING) abort ();
5038} 4881}
5039 4882
5040static pointer 4883static int
5041opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4884opexe_5 (SCHEME_P_ enum scheme_opcodes op)
5042{ 4885{
4886 pointer args = SCHEME_V->args;
5043 pointer x; 4887 pointer x;
5044 4888
5045 if (SCHEME_V->nesting != 0) 4889 if (SCHEME_V->nesting != 0)
5046 { 4890 {
5047 int n = SCHEME_V->nesting; 4891 int n = SCHEME_V->nesting;
5054 switch (op) 4898 switch (op)
5055 { 4899 {
5056 /* ========== reading part ========== */ 4900 /* ========== reading part ========== */
5057#if USE_PORTS 4901#if USE_PORTS
5058 case OP_READ: 4902 case OP_READ:
5059 if (!is_pair (SCHEME_V->args)) 4903 if (!is_pair (args))
5060 s_goto (OP_READ_INTERNAL); 4904 s_goto (OP_READ_INTERNAL);
5061 4905
5062 if (!is_inport (car (SCHEME_V->args))) 4906 if (!is_inport (car (args)))
5063 Error_1 ("read: not an input port:", car (SCHEME_V->args)); 4907 Error_1 ("read: not an input port:", car (args));
5064 4908
5065 if (car (SCHEME_V->args) == SCHEME_V->inport) 4909 if (car (args) == SCHEME_V->inport)
5066 s_goto (OP_READ_INTERNAL); 4910 s_goto (OP_READ_INTERNAL);
5067 4911
5068 x = SCHEME_V->inport; 4912 x = SCHEME_V->inport;
5069 SCHEME_V->inport = car (SCHEME_V->args); 4913 SCHEME_V->inport = car (args);
5070 x = cons (x, NIL); 4914 x = cons (x, NIL);
5071 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4915 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5072 s_goto (OP_READ_INTERNAL); 4916 s_goto (OP_READ_INTERNAL);
5073 4917
5074 case OP_READ_CHAR: /* read-char */ 4918 case OP_READ_CHAR: /* read-char */
5075 case OP_PEEK_CHAR: /* peek-char */ 4919 case OP_PEEK_CHAR: /* peek-char */
5076 { 4920 {
5077 int c; 4921 int c;
5078 4922
5079 if (is_pair (SCHEME_V->args)) 4923 if (is_pair (args))
5080 { 4924 {
5081 if (car (SCHEME_V->args) != SCHEME_V->inport) 4925 if (car (args) != SCHEME_V->inport)
5082 { 4926 {
5083 x = SCHEME_V->inport; 4927 x = SCHEME_V->inport;
5084 x = cons (x, NIL); 4928 x = cons (x, NIL);
5085 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4929 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5086 SCHEME_V->inport = car (SCHEME_V->args); 4930 SCHEME_V->inport = car (args);
5087 } 4931 }
5088 } 4932 }
5089 4933
5090 c = inchar (SCHEME_A); 4934 c = inchar (SCHEME_A);
5091 4935
5101 case OP_CHAR_READY: /* char-ready? */ 4945 case OP_CHAR_READY: /* char-ready? */
5102 { 4946 {
5103 pointer p = SCHEME_V->inport; 4947 pointer p = SCHEME_V->inport;
5104 int res; 4948 int res;
5105 4949
5106 if (is_pair (SCHEME_V->args)) 4950 if (is_pair (args))
5107 p = car (SCHEME_V->args); 4951 p = car (args);
5108 4952
5109 res = p->object.port->kind & port_string; 4953 res = port (p)->kind & port_string;
5110 4954
5111 s_retbool (res); 4955 s_retbool (res);
5112 } 4956 }
5113 4957
5114 case OP_SET_INPORT: /* set-input-port */ 4958 case OP_SET_INPORT: /* set-input-port */
5115 SCHEME_V->inport = car (SCHEME_V->args); 4959 SCHEME_V->inport = car (args);
5116 s_return (SCHEME_V->value); 4960 s_return (SCHEME_V->value);
5117 4961
5118 case OP_SET_OUTPORT: /* set-output-port */ 4962 case OP_SET_OUTPORT: /* set-output-port */
5119 SCHEME_V->outport = car (SCHEME_V->args); 4963 SCHEME_V->outport = car (args);
5120 s_return (SCHEME_V->value); 4964 s_return (SCHEME_V->value);
5121#endif 4965#endif
5122 4966
5123 case OP_RDSEXPR: 4967 case OP_RDSEXPR:
5124 switch (SCHEME_V->tok) 4968 switch (SCHEME_V->tok)
5173 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 5017 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
5174 SCHEME_V->tok = token (SCHEME_A); 5018 SCHEME_V->tok = token (SCHEME_A);
5175 s_goto (OP_RDSEXPR); 5019 s_goto (OP_RDSEXPR);
5176 5020
5177 case TOK_ATOM: 5021 case TOK_ATOM:
5178 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); 5022 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS)));
5023
5024 case TOK_DOTATOM:
5025 SCHEME_V->strbuff[0] = '.';
5026 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5027
5028 case TOK_STRATOM:
5029 x = readstrexp (SCHEME_A_ '|');
5030 //TODO: haven't checked whether the garbage collector could interfere
5031 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5179 5032
5180 case TOK_DQUOTE: 5033 case TOK_DQUOTE:
5181 x = readstrexp (SCHEME_A); 5034 x = readstrexp (SCHEME_A_ '"');
5182 5035
5183 if (x == S_F) 5036 if (x == S_F)
5184 Error_0 ("Error reading string"); 5037 Error_0 ("Error reading string");
5185 5038
5186 setimmutable (x); 5039 setimmutable (x);
5198 s_goto (OP_EVAL); 5051 s_goto (OP_EVAL);
5199 } 5052 }
5200 } 5053 }
5201 5054
5202 case TOK_SHARP_CONST: 5055 case TOK_SHARP_CONST:
5203 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) 5056 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5204 Error_0 ("undefined sharp expression"); 5057 Error_0 ("undefined sharp expression");
5205 else 5058 else
5206 s_return (x); 5059 s_return (x);
5207 5060
5208 default: 5061 default:
5210 } 5063 }
5211 5064
5212 break; 5065 break;
5213 5066
5214 case OP_RDLIST: 5067 case OP_RDLIST:
5215 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 5068 SCHEME_V->args = cons (SCHEME_V->value, args);
5216 SCHEME_V->tok = token (SCHEME_A); 5069 SCHEME_V->tok = token (SCHEME_A);
5217 5070
5218 switch (SCHEME_V->tok) 5071 switch (SCHEME_V->tok)
5219 { 5072 {
5220 case TOK_EOF: 5073 case TOK_EOF:
5248 case OP_RDDOT: 5101 case OP_RDDOT:
5249 if (token (SCHEME_A) != TOK_RPAREN) 5102 if (token (SCHEME_A) != TOK_RPAREN)
5250 Error_0 ("syntax error: illegal dot expression"); 5103 Error_0 ("syntax error: illegal dot expression");
5251 5104
5252 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5105 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5253 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); 5106 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, args));
5254 5107
5255 case OP_RDQUOTE: 5108 case OP_RDQUOTE:
5256 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); 5109 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)));
5257 5110
5258 case OP_RDQQUOTE: 5111 case OP_RDQQUOTE:
5280 SCHEME_V->args = SCHEME_V->value; 5133 SCHEME_V->args = SCHEME_V->value;
5281 s_goto (OP_VECTOR); 5134 s_goto (OP_VECTOR);
5282 5135
5283 /* ========== printing part ========== */ 5136 /* ========== printing part ========== */
5284 case OP_P0LIST: 5137 case OP_P0LIST:
5285 if (is_vector (SCHEME_V->args)) 5138 if (is_vector (args))
5286 { 5139 {
5287 putstr (SCHEME_A_ "#("); 5140 putstr (SCHEME_A_ "#(");
5288 SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); 5141 SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0));
5289 s_goto (OP_PVECFROM); 5142 s_goto (OP_PVECFROM);
5290 } 5143 }
5291 else if (is_environment (SCHEME_V->args)) 5144 else if (is_environment (args))
5292 { 5145 {
5293 putstr (SCHEME_A_ "#<ENVIRONMENT>"); 5146 putstr (SCHEME_A_ "#<ENVIRONMENT>");
5294 s_return (S_T); 5147 s_return (S_T);
5295 } 5148 }
5296 else if (!is_pair (SCHEME_V->args)) 5149 else if (!is_pair (args))
5297 { 5150 {
5298 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5151 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5299 s_return (S_T); 5152 s_return (S_T);
5300 } 5153 }
5301 else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5154 else
5302 { 5155 {
5156 pointer a = car (args);
5157 pointer b = cdr (args);
5158 int ok_abbr = ok_abbrev (b);
5159 SCHEME_V->args = car (b);
5160
5161 if (a == SCHEME_V->QUOTE && ok_abbr)
5303 putstr (SCHEME_A_ "'"); 5162 putstr (SCHEME_A_ "'");
5304 SCHEME_V->args = cadr (SCHEME_V->args); 5163 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5164 putstr (SCHEME_A_ "`");
5165 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5166 putstr (SCHEME_A_ ",");
5167 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5168 putstr (SCHEME_A_ ",@");
5169 else
5170 {
5171 putstr (SCHEME_A_ "(");
5172 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5173 SCHEME_V->args = a;
5174 }
5175
5305 s_goto (OP_P0LIST); 5176 s_goto (OP_P0LIST);
5306 } 5177 }
5307 else if (car (SCHEME_V->args) == SCHEME_V->QQUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5178
5179 case OP_P1LIST:
5180 if (is_pair (args))
5308 { 5181 {
5182 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5309 putstr (SCHEME_A_ "`"); 5183 putstr (SCHEME_A_ " ");
5310 SCHEME_V->args = cadr (SCHEME_V->args); 5184 SCHEME_V->args = car (args);
5311 s_goto (OP_P0LIST); 5185 s_goto (OP_P0LIST);
5312 } 5186 }
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 }
5325 else
5326 {
5327 putstr (SCHEME_A_ "(");
5328 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL);
5329 SCHEME_V->args = car (SCHEME_V->args);
5330 s_goto (OP_P0LIST);
5331 }
5332
5333 case OP_P1LIST:
5334 if (is_pair (SCHEME_V->args))
5335 {
5336 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL);
5337 putstr (SCHEME_A_ " ");
5338 SCHEME_V->args = car (SCHEME_V->args);
5339 s_goto (OP_P0LIST);
5340 }
5341 else if (is_vector (SCHEME_V->args)) 5187 else if (is_vector (args))
5342 { 5188 {
5343 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); 5189 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL);
5344 putstr (SCHEME_A_ " . "); 5190 putstr (SCHEME_A_ " . ");
5345 s_goto (OP_P0LIST); 5191 s_goto (OP_P0LIST);
5346 } 5192 }
5347 else 5193 else
5348 { 5194 {
5349 if (SCHEME_V->args != NIL) 5195 if (args != NIL)
5350 { 5196 {
5351 putstr (SCHEME_A_ " . "); 5197 putstr (SCHEME_A_ " . ");
5352 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5198 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5353 } 5199 }
5354 5200
5355 putstr (SCHEME_A_ ")"); 5201 putstr (SCHEME_A_ ")");
5356 s_return (S_T); 5202 s_return (S_T);
5357 } 5203 }
5358 5204
5359 case OP_PVECFROM: 5205 case OP_PVECFROM:
5360 { 5206 {
5361 int i = ivalue_unchecked (cdr (SCHEME_V->args)); 5207 int i = ivalue_unchecked (cdr (args));
5362 pointer vec = car (SCHEME_V->args); 5208 pointer vec = car (args);
5363 int len = veclength (vec); 5209 int len = veclength (vec);
5364 5210
5365 if (i == len) 5211 if (i == len)
5366 { 5212 {
5367 putstr (SCHEME_A_ ")"); 5213 putstr (SCHEME_A_ ")");
5368 s_return (S_T); 5214 s_return (S_T);
5369 } 5215 }
5370 else 5216 else
5371 { 5217 {
5372 pointer elem = vector_elem (vec, i); 5218 pointer elem = vector_get (vec, i);
5373 5219
5374 ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; 5220 ivalue_unchecked (cdr (args)) = i + 1;
5375 s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); 5221 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5376 SCHEME_V->args = elem; 5222 SCHEME_V->args = elem;
5377 5223
5378 if (i > 0) 5224 if (i > 0)
5379 putstr (SCHEME_A_ " "); 5225 putstr (SCHEME_A_ " ");
5380 5226
5381 s_goto (OP_P0LIST); 5227 s_goto (OP_P0LIST);
5382 } 5228 }
5383 } 5229 }
5384 } 5230 }
5385 5231
5386 return S_T; 5232 if (USE_ERROR_CHECKING) abort ();
5387} 5233}
5388 5234
5389static pointer 5235static int
5390opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5236opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5391{ 5237{
5238 pointer args = SCHEME_V->args;
5239 pointer a = car (args);
5392 pointer x, y; 5240 pointer x, y;
5393 5241
5394 switch (op) 5242 switch (op)
5395 { 5243 {
5396 case OP_LIST_LENGTH: /* length *//* a.k */ 5244 case OP_LIST_LENGTH: /* length *//* a.k */
5397 { 5245 {
5398 long v = list_length (SCHEME_A_ car (SCHEME_V->args)); 5246 long v = list_length (SCHEME_A_ a);
5399 5247
5400 if (v < 0) 5248 if (v < 0)
5401 Error_1 ("length: not a list:", car (SCHEME_V->args)); 5249 Error_1 ("length: not a list:", a);
5402 5250
5403 s_return (mk_integer (SCHEME_A_ v)); 5251 s_return (mk_integer (SCHEME_A_ v));
5404 } 5252 }
5405 5253
5406 case OP_ASSQ: /* assq *//* a.k */ 5254 case OP_ASSQ: /* assq *//* a.k */
5407 x = car (SCHEME_V->args); 5255 x = a;
5408 5256
5409 for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) 5257 for (y = cadr (args); is_pair (y); y = cdr (y))
5410 { 5258 {
5411 if (!is_pair (car (y))) 5259 if (!is_pair (car (y)))
5412 Error_0 ("unable to handle non pair element"); 5260 Error_0 ("unable to handle non pair element");
5413 5261
5414 if (x == caar (y)) 5262 if (x == caar (y))
5420 else 5268 else
5421 s_return (S_F); 5269 s_return (S_F);
5422 5270
5423 5271
5424 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5272 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5425 SCHEME_V->args = car (SCHEME_V->args); 5273 SCHEME_V->args = a;
5426 5274
5427 if (SCHEME_V->args == NIL) 5275 if (SCHEME_V->args == NIL)
5428 s_return (S_F); 5276 s_return (S_F);
5429 else if (is_closure (SCHEME_V->args)) 5277 else if (is_closure (SCHEME_V->args))
5430 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5278 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5435 5283
5436 case OP_CLOSUREP: /* closure? */ 5284 case OP_CLOSUREP: /* closure? */
5437 /* 5285 /*
5438 * Note, macro object is also a closure. 5286 * Note, macro object is also a closure.
5439 * Therefore, (closure? <#MACRO>) ==> #t 5287 * Therefore, (closure? <#MACRO>) ==> #t
5288 * (schmorp) well, obviously not, fix? TODO
5440 */ 5289 */
5441 s_retbool (is_closure (car (SCHEME_V->args))); 5290 s_retbool (is_closure (a));
5442 5291
5443 case OP_MACROP: /* macro? */ 5292 case OP_MACROP: /* macro? */
5444 s_retbool (is_macro (car (SCHEME_V->args))); 5293 s_retbool (is_macro (a));
5445 } 5294 }
5446 5295
5447 return S_T; /* NOTREACHED */ 5296 if (USE_ERROR_CHECKING) abort ();
5448} 5297}
5449 5298
5299/* 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); 5300typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5451 5301
5452typedef int (*test_predicate) (pointer); 5302typedef int (*test_predicate)(pointer);
5453static int 5303static int
5454is_any (pointer p) 5304tst_any (pointer p)
5455{ 5305{
5456 return 1; 5306 return 1;
5457} 5307}
5458 5308
5459static int 5309static int
5460is_nonneg (pointer p) 5310tst_inonneg (pointer p)
5461{ 5311{
5462 return ivalue (p) >= 0 && is_integer (p); 5312 return is_integer (p) && ivalue_unchecked (p) >= 0;
5313}
5314
5315static int
5316tst_is_list (SCHEME_P_ pointer p)
5317{
5318 return p == NIL || is_pair (p);
5463} 5319}
5464 5320
5465/* Correspond carefully with following defines! */ 5321/* Correspond carefully with following defines! */
5466static struct 5322static struct
5467{ 5323{
5468 test_predicate fct; 5324 test_predicate fct;
5469 const char *kind; 5325 const char *kind;
5470} tests[] = 5326} tests[] = {
5471{ 5327 { tst_any , 0 },
5472 { 0, 0}, /* unused */ 5328 { is_string , "string" },
5473 { is_any, 0}, 5329 { is_symbol , "symbol" },
5474 { is_string, "string" }, 5330 { is_port , "port" },
5475 { is_symbol, "symbol" },
5476 { is_port, "port" },
5477 { is_inport, "input port" }, 5331 { is_inport , "input port" },
5478 { is_outport, "output port" }, 5332 { is_outport , "output port" },
5479 { is_environment, "environment" }, 5333 { is_environment, "environment" },
5480 { is_pair, "pair" }, 5334 { is_pair , "pair" },
5481 { 0, "pair or '()" }, 5335 { 0 , "pair or '()" },
5482 { is_character, "character" }, 5336 { is_character , "character" },
5483 { is_vector, "vector" }, 5337 { is_vector , "vector" },
5484 { is_number, "number" }, 5338 { is_number , "number" },
5485 { is_integer, "integer" }, 5339 { is_integer , "integer" },
5486 { is_nonneg, "non-negative integer" } 5340 { tst_inonneg , "non-negative integer" }
5487}; 5341};
5488 5342
5489#define TST_NONE 0 5343#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5490#define TST_ANY "\001" 5344#define TST_ANY "\001"
5491#define TST_STRING "\002" 5345#define TST_STRING "\002"
5492#define TST_SYMBOL "\003" 5346#define TST_SYMBOL "\003"
5493#define TST_PORT "\004" 5347#define TST_PORT "\004"
5494#define TST_INPORT "\005" 5348#define TST_INPORT "\005"
5495#define TST_OUTPORT "\006" 5349#define TST_OUTPORT "\006"
5496#define TST_ENVIRONMENT "\007" 5350#define TST_ENVIRONMENT "\007"
5497#define TST_PAIR "\010" 5351#define TST_PAIR "\010"
5498#define TST_LIST "\011" 5352#define TST_LIST "\011"
5499#define TST_CHAR "\012" 5353#define TST_CHAR "\012"
5500#define TST_VECTOR "\013" 5354#define TST_VECTOR "\013"
5501#define TST_NUMBER "\014" 5355#define TST_NUMBER "\014"
5502#define TST_INTEGER "\015" 5356#define TST_INTEGER "\015"
5503#define TST_NATURAL "\016" 5357#define TST_NATURAL "\016"
5358
5359#define INF_ARG 0xff
5360#define UNNAMED_OP ""
5361
5362static const char opnames[] =
5363#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5364#include "opdefines.h"
5365#undef OP_DEF
5366;
5367
5368static const char *
5369opname (int idx)
5370{
5371 const char *name = opnames;
5372
5373 /* should do this at compile time, but would require external program, right? */
5374 while (idx--)
5375 name += strlen (name) + 1;
5376
5377 return *name ? name : "ILLEGAL";
5378}
5379
5380static const char *
5381procname (pointer x)
5382{
5383 return opname (procnum (x));
5384}
5504 5385
5505typedef struct 5386typedef struct
5506{ 5387{
5507 dispatch_func func; 5388 uint8_t func;
5508 char *name; 5389 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5390 uint8_t builtin;
5391#if USE_ERROR_CHECKING
5509 int min_arity; 5392 uint8_t min_arity;
5510 int max_arity; 5393 uint8_t max_arity;
5511 char *arg_tests_encoding; 5394 char arg_tests_encoding[3];
5395#endif
5512} op_code_info; 5396} op_code_info;
5513 5397
5514#define INF_ARG 0xffff
5515
5516static op_code_info dispatch_table[] = { 5398static const op_code_info dispatch_table[] = {
5517#define OP_DEF(A,B,C,D,E,OP) {A,B,C,D,E}, 5399#if USE_ERROR_CHECKING
5400#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5401#else
5402#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5403#endif
5518#include "opdefines.h" 5404#include "opdefines.h"
5405#undef OP_DEF
5519 {0} 5406 {0}
5520}; 5407};
5521 5408
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 */ 5409/* kernel of this interpreter */
5535static void 5410static void ecb_hot
5536Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5411Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5537{ 5412{
5538 SCHEME_V->op = op; 5413 SCHEME_V->op = op;
5539 5414
5540 for (;;) 5415 for (;;)
5541 { 5416 {
5542 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5417 const op_code_info *pcd = dispatch_table + SCHEME_V->op;
5543 5418
5544#if USE_ERROR_CHECKING 5419#if USE_ERROR_CHECKING
5545 if (pcd->name) /* if built-in function, check arguments */ 5420 if (pcd->builtin) /* if built-in function, check arguments */
5546 { 5421 {
5547 int ok = 1;
5548 char msg[STRBUFFSIZE]; 5422 char msg[STRBUFFSIZE];
5549 int n = list_length (SCHEME_A_ SCHEME_V->args); 5423 int n = list_length (SCHEME_A_ SCHEME_V->args);
5550 5424
5551 /* Check number of arguments */ 5425 /* Check number of arguments */
5552 if (ecb_expect_false (n < pcd->min_arity)) 5426 if (ecb_expect_false (n < pcd->min_arity))
5553 { 5427 {
5554 ok = 0;
5555 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5428 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5556 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); 5429 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity);
5430 xError_1 (SCHEME_A_ msg, 0);
5431 continue;
5557 } 5432 }
5558 else if (ecb_excpect_false (n > pcd->max_arity)) 5433 else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG))
5559 { 5434 {
5560 ok = 0;
5561 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5435 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5562 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); 5436 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity);
5437 xError_1 (SCHEME_A_ msg, 0);
5438 continue;
5563 } 5439 }
5564 5440 else
5565 if (ecb_expect_false (ok))
5566 { 5441 {
5567 if (pcd->arg_tests_encoding) 5442 if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */
5568 { 5443 {
5569 int i = 0; 5444 int i = 0;
5570 int j; 5445 int j;
5571 const char *t = pcd->arg_tests_encoding; 5446 const char *t = pcd->arg_tests_encoding;
5572 pointer arglist = SCHEME_V->args; 5447 pointer arglist = SCHEME_V->args;
5573 5448
5574 do 5449 do
5575 { 5450 {
5576 pointer arg = car (arglist); 5451 pointer arg = car (arglist);
5577 5452
5578 j = (int) t[0]; 5453 j = t[0];
5579 5454
5455 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5580 if (j == TST_LIST[0]) 5456 if (j == TST_LIST[0])
5581 { 5457 {
5582 if (arg != NIL && !is_pair (arg)) 5458 if (!tst_is_list (SCHEME_A_ arg))
5583 break; 5459 break;
5584 } 5460 }
5585 else 5461 else
5586 { 5462 {
5587 if (!tests[j].fct (arg)) 5463 if (!tests[j - 1].fct (arg))
5588 break; 5464 break;
5589 } 5465 }
5590 5466
5591 if (t[1] != 0) /* last test is replicated as necessary */ 5467 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5592 t++; 5468 t++;
5593 5469
5594 arglist = cdr (arglist); 5470 arglist = cdr (arglist);
5595 i++; 5471 i++;
5596 } 5472 }
5597 while (i < n); 5473 while (i < n);
5598 5474
5599 if (i < n) 5475 if (i < n)
5600 { 5476 {
5601 ok = 0;
5602 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); 5477 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind);
5478 xError_1 (SCHEME_A_ msg, 0);
5479 continue;
5603 } 5480 }
5604 } 5481 }
5605 } 5482 }
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 } 5483 }
5615#endif 5484#endif
5616 5485
5617 ok_to_freely_gc (SCHEME_A); 5486 ok_to_freely_gc (SCHEME_A);
5618 5487
5488 static const dispatch_func dispatch_funcs[] = {
5489 opexe_0,
5490 opexe_1,
5491 opexe_2,
5492 opexe_3,
5493 opexe_4,
5494 opexe_5,
5495 opexe_6,
5496 };
5497
5619 if (ecb_expect_false (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL)) 5498 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5620 return; 5499 return;
5621 5500
5622 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5501 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5623 { 5502 {
5624 xwrstr ("No memory!\n"); 5503 putstr (SCHEME_A_ "No memory!\n");
5625 return; 5504 return;
5626 } 5505 }
5627 } 5506 }
5628} 5507}
5629 5508
5648mk_proc (SCHEME_P_ enum scheme_opcodes op) 5527mk_proc (SCHEME_P_ enum scheme_opcodes op)
5649{ 5528{
5650 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5529 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5651 set_typeflag (y, (T_PROC | T_ATOM)); 5530 set_typeflag (y, (T_PROC | T_ATOM));
5652 ivalue_unchecked (y) = op; 5531 ivalue_unchecked (y) = op;
5653 set_num_integer (y);
5654 return y; 5532 return y;
5655} 5533}
5656 5534
5657/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5535/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5658static int 5536static int
5659syntaxnum (pointer p) 5537syntaxnum (pointer p)
5660{ 5538{
5661 const char *s = strvalue (car (p)); 5539 const char *s = strvalue (p);
5662 5540
5663 switch (strlength (car (p))) 5541 switch (strlength (p))
5664 { 5542 {
5665 case 2: 5543 case 2:
5666 if (s[0] == 'i') 5544 if (s[0] == 'i')
5667 return OP_IF0; /* if */ 5545 return OP_IF0; /* if */
5668 else 5546 else
5723 return OP_C0STREAM; /* cons-stream */ 5601 return OP_C0STREAM; /* cons-stream */
5724 } 5602 }
5725} 5603}
5726 5604
5727#if USE_MULTIPLICITY 5605#if USE_MULTIPLICITY
5728scheme * 5606ecb_cold scheme *
5729scheme_init_new () 5607scheme_init_new ()
5730{ 5608{
5731 scheme *sc = malloc (sizeof (scheme)); 5609 scheme *sc = malloc (sizeof (scheme));
5732 5610
5733 if (!scheme_init (SCHEME_A)) 5611 if (!scheme_init (SCHEME_A))
5738 else 5616 else
5739 return sc; 5617 return sc;
5740} 5618}
5741#endif 5619#endif
5742 5620
5743int 5621ecb_cold int
5744scheme_init (SCHEME_P) 5622scheme_init (SCHEME_P)
5745{ 5623{
5746 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5624 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5747 pointer x; 5625 pointer x;
5626
5627 /* this memset is not strictly correct, as we assume (intcache)
5628 * that memset 0 will also set pointers to 0, but memset does
5629 * of course not guarantee that. screw such systems.
5630 */
5631 memset (SCHEME_V, 0, sizeof (*SCHEME_V));
5748 5632
5749 num_set_fixnum (num_zero, 1); 5633 num_set_fixnum (num_zero, 1);
5750 num_set_ivalue (num_zero, 0); 5634 num_set_ivalue (num_zero, 0);
5751 num_set_fixnum (num_one, 1); 5635 num_set_fixnum (num_one, 1);
5752 num_set_ivalue (num_one, 1); 5636 num_set_ivalue (num_one, 1);
5764 SCHEME_V->save_inport = NIL; 5648 SCHEME_V->save_inport = NIL;
5765 SCHEME_V->loadport = NIL; 5649 SCHEME_V->loadport = NIL;
5766 SCHEME_V->nesting = 0; 5650 SCHEME_V->nesting = 0;
5767 SCHEME_V->interactive_repl = 0; 5651 SCHEME_V->interactive_repl = 0;
5768 5652
5769 if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) 5653 if (!alloc_cellseg (SCHEME_A))
5770 { 5654 {
5771#if USE_ERROR_CHECKING 5655#if USE_ERROR_CHECKING
5772 SCHEME_V->no_memory = 1; 5656 SCHEME_V->no_memory = 1;
5773 return 0; 5657 return 0;
5774#endif 5658#endif
5821 5705
5822 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) 5706 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
5823 assign_syntax (SCHEME_A_ syntax_names[i]); 5707 assign_syntax (SCHEME_A_ syntax_names[i]);
5824 } 5708 }
5825 5709
5710 // TODO: should iterate via strlen, to avoid n² complexity
5826 for (i = 0; i < n; i++) 5711 for (i = 0; i < n; i++)
5827 if (dispatch_table[i].name != 0) 5712 if (dispatch_table[i].builtin)
5828 assign_proc (SCHEME_A_ i, dispatch_table[i].name); 5713 assign_proc (SCHEME_A_ i, opname (i));
5829 5714
5830 /* initialization of global pointers to special symbols */ 5715 /* initialization of global pointers to special symbols */
5831 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 5716 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
5832 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 5717 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
5833 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 5718 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
5872scheme_set_external_data (SCHEME_P_ void *p) 5757scheme_set_external_data (SCHEME_P_ void *p)
5873{ 5758{
5874 SCHEME_V->ext_data = p; 5759 SCHEME_V->ext_data = p;
5875} 5760}
5876 5761
5877void 5762ecb_cold void
5878scheme_deinit (SCHEME_P) 5763scheme_deinit (SCHEME_P)
5879{ 5764{
5880 int i; 5765 int i;
5881 5766
5882#if SHOW_ERROR_LINE 5767#if SHOW_ERROR_LINE
5974{ 5859{
5975 dump_stack_reset (SCHEME_A); 5860 dump_stack_reset (SCHEME_A);
5976 SCHEME_V->envir = SCHEME_V->global_env; 5861 SCHEME_V->envir = SCHEME_V->global_env;
5977 SCHEME_V->file_i = 0; 5862 SCHEME_V->file_i = 0;
5978 SCHEME_V->load_stack[0].kind = port_input | port_string; 5863 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 */ 5864 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); 5865 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; 5866 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
5982#if USE_PORTS 5867#if USE_PORTS
5983 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 5868 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5984#endif 5869#endif
5985 SCHEME_V->retcode = 0; 5870 SCHEME_V->retcode = 0;
5986 SCHEME_V->interactive_repl = 0; 5871 SCHEME_V->interactive_repl = 0;
6103# endif 5988# endif
6104 int fin; 5989 int fin;
6105 char *file_name = InitFile; 5990 char *file_name = InitFile;
6106 int retcode; 5991 int retcode;
6107 int isfile = 1; 5992 int isfile = 1;
5993 system ("ps v $PPID");//D
6108 5994
6109 if (argc == 2 && strcmp (argv[1], "-?") == 0) 5995 if (argc == 2 && strcmp (argv[1], "-?") == 0)
6110 { 5996 {
6111 xwrstr ("Usage: tinyscheme -?\n"); 5997 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
6112 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 5998 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
6113 xwrstr ("followed by\n"); 5999 putstr (SCHEME_A_ "followed by\n");
6114 xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); 6000 putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n");
6115 xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); 6001 putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n");
6116 xwrstr ("assuming that the executable is named tinyscheme.\n"); 6002 putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n");
6117 xwrstr ("Use - as filename for stdin.\n"); 6003 putstr (SCHEME_A_ "Use - as filename for stdin.\n");
6118 return 1; 6004 return 1;
6119 } 6005 }
6120 6006
6121 if (!scheme_init (SCHEME_A)) 6007 if (!scheme_init (SCHEME_A))
6122 { 6008 {
6123 xwrstr ("Could not initialize!\n"); 6009 putstr (SCHEME_A_ "Could not initialize!\n");
6124 return 2; 6010 return 2;
6125 } 6011 }
6126 6012
6127# if USE_PORTS 6013# if USE_PORTS
6128 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); 6014 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO);
6173 fin = open (file_name, O_RDONLY); 6059 fin = open (file_name, O_RDONLY);
6174#endif 6060#endif
6175 6061
6176 if (isfile && fin < 0) 6062 if (isfile && fin < 0)
6177 { 6063 {
6178 xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); 6064 putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n");
6179 } 6065 }
6180 else 6066 else
6181 { 6067 {
6182 if (isfile) 6068 if (isfile)
6183 scheme_load_named_file (SCHEME_A_ fin, file_name); 6069 scheme_load_named_file (SCHEME_A_ fin, file_name);
6187#if USE_PORTS 6073#if USE_PORTS
6188 if (!isfile || fin != STDIN_FILENO) 6074 if (!isfile || fin != STDIN_FILENO)
6189 { 6075 {
6190 if (SCHEME_V->retcode != 0) 6076 if (SCHEME_V->retcode != 0)
6191 { 6077 {
6192 xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); 6078 putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n");
6193 } 6079 }
6194 6080
6195 if (isfile) 6081 if (isfile)
6196 close (fin); 6082 close (fin);
6197 } 6083 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines