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.58 by root, Tue Dec 1 05:12:33 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 }
1496 return S_F; 1475 return S_F;
1497 else if (*name == '\\') /* #\w (character) */ 1476 else if (*name == '\\') /* #\w (character) */
1498 { 1477 {
1499 int c; 1478 int c;
1500 1479
1480 // TODO: optimise
1501 if (stricmp (name + 1, "space") == 0) 1481 if (stricmp (name + 1, "space") == 0)
1502 c = ' '; 1482 c = ' ';
1503 else if (stricmp (name + 1, "newline") == 0) 1483 else if (stricmp (name + 1, "newline") == 0)
1504 c = '\n'; 1484 c = '\n';
1505 else if (stricmp (name + 1, "return") == 0) 1485 else if (stricmp (name + 1, "return") == 0)
1506 c = '\r'; 1486 c = '\r';
1507 else if (stricmp (name + 1, "tab") == 0) 1487 else if (stricmp (name + 1, "tab") == 0)
1508 c = '\t'; 1488 c = '\t';
1489 else if (stricmp (name + 1, "alarm") == 0)
1490 c = 0x07;
1491 else if (stricmp (name + 1, "backspace") == 0)
1492 c = 0x08;
1493 else if (stricmp (name + 1, "escape") == 0)
1494 c = 0x1b;
1495 else if (stricmp (name + 1, "delete") == 0)
1496 c = 0x7f;
1497 else if (stricmp (name + 1, "null") == 0)
1498 c = 0;
1509 else if (name[1] == 'x' && name[2] != 0) 1499 else if (name[1] == 'x' && name[2] != 0)
1510 { 1500 {
1511 long c1 = strtol (name + 2, 0, 16); 1501 long c1 = strtol (name + 2, 0, 16);
1512 1502
1513 if (0 <= c1 && c1 <= UCHAR_MAX) 1503 if (0 <= c1 && c1 <= UCHAR_MAX)
1562 1552
1563 if (ecb_expect_false (is_vector (p))) 1553 if (ecb_expect_false (is_vector (p)))
1564 { 1554 {
1565 int i; 1555 int i;
1566 1556
1567 for (i = 0; i < p->object.vector.length; i++) 1557 for (i = 0; i < veclength (p); i++)
1568 mark (vecvalue (p)[i]); 1558 mark (vecvalue (p)[i]);
1569 } 1559 }
1570 1560
1571 if (is_atom (p)) 1561 if (is_atom (p))
1572 goto E6; 1562 goto E6;
1619 1609
1620/* garbage collection. parameter a, b is marked. */ 1610/* garbage collection. parameter a, b is marked. */
1621static void 1611static void
1622gc (SCHEME_P_ pointer a, pointer b) 1612gc (SCHEME_P_ pointer a, pointer b)
1623{ 1613{
1624 pointer p;
1625 int i; 1614 int i;
1626 1615
1627 if (SCHEME_V->gc_verbose) 1616 if (SCHEME_V->gc_verbose)
1628 putstr (SCHEME_A_ "gc..."); 1617 putstr (SCHEME_A_ "gc...");
1629 1618
1645 /* Mark recent objects the interpreter doesn't know about yet. */ 1634 /* Mark recent objects the interpreter doesn't know about yet. */
1646 mark (car (S_SINK)); 1635 mark (car (S_SINK));
1647 /* Mark any older stuff above nested C calls */ 1636 /* Mark any older stuff above nested C calls */
1648 mark (SCHEME_V->c_nest); 1637 mark (SCHEME_V->c_nest);
1649 1638
1639#if USE_INTCACHE
1640 /* mark intcache */
1641 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1642 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1643 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1644#endif
1645
1650 /* mark variables a, b */ 1646 /* mark variables a, b */
1651 mark (a); 1647 mark (a);
1652 mark (b); 1648 mark (b);
1653 1649
1654 /* garbage collect */ 1650 /* garbage collect */
1655 clrmark (NIL); 1651 clrmark (NIL);
1656 SCHEME_V->fcells = 0; 1652 SCHEME_V->fcells = 0;
1657 SCHEME_V->free_cell = NIL; 1653 SCHEME_V->free_cell = NIL;
1658 1654
1659 /* free-list is kept sorted by address so as to maintain consecutive 1655 if (SCHEME_V->gc_verbose)
1660 ranges, if possible, for use with vectors. Here we scan the cells 1656 putstr (SCHEME_A_ "freeing...");
1661 (which are also kept sorted by address) downwards to build the 1657
1662 free-list in sorted order. 1658 uint32_t total = 0;
1663 */ 1659
1660 /* Here we scan the cells to build the free-list. */
1664 for (i = SCHEME_V->last_cell_seg; i >= 0; i--) 1661 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1665 { 1662 {
1666 p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; 1663 struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1664 struct cell *p;
1665 total += SCHEME_V->cell_segsize [i];
1667 1666
1668 while (--p >= SCHEME_V->cell_seg[i]) 1667 for (p = SCHEME_V->cell_seg[i]; p < end; ++p)
1669 { 1668 {
1669 pointer c = POINTER (p);
1670
1670 if (is_mark (p)) 1671 if (is_mark (c))
1671 clrmark (p); 1672 clrmark (c);
1672 else 1673 else
1673 { 1674 {
1674 /* reclaim cell */ 1675 /* reclaim cell */
1675 if (typeflag (p) != T_FREE) 1676 if (typeflag (c) != T_PAIR)
1676 { 1677 {
1677 finalize_cell (SCHEME_A_ p); 1678 finalize_cell (SCHEME_A_ c);
1678 set_typeflag (p, T_FREE); 1679 set_typeflag (c, T_PAIR);
1679 set_car (p, NIL); 1680 set_car (c, NIL);
1680 } 1681 }
1681 1682
1682 ++SCHEME_V->fcells; 1683 ++SCHEME_V->fcells;
1683 set_cdr (p, SCHEME_V->free_cell); 1684 set_cdr (c, SCHEME_V->free_cell);
1684 SCHEME_V->free_cell = p; 1685 SCHEME_V->free_cell = c;
1685 } 1686 }
1686 } 1687 }
1687 } 1688 }
1688 1689
1689 if (SCHEME_V->gc_verbose) 1690 if (SCHEME_V->gc_verbose)
1690 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1691 {
1692 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");
1693 }
1691} 1694}
1692 1695
1693static void 1696static void
1694finalize_cell (SCHEME_P_ pointer a) 1697finalize_cell (SCHEME_P_ pointer a)
1695{ 1698{
1696 /* TODO, fast bitmap check? */ 1699 /* TODO, fast bitmap check? */
1697 if (is_string (a)) 1700 if (is_string (a) || is_symbol (a))
1698 free (strvalue (a)); 1701 free (strvalue (a));
1699 else if (is_vector (a)) 1702 else if (is_vector (a))
1700 free (vecvalue (a)); 1703 free (vecvalue (a));
1701#if USE_PORTS 1704#if USE_PORTS
1702 else if (is_port (a)) 1705 else if (is_port (a))
1703 { 1706 {
1704 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) 1707 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit)
1705 port_close (SCHEME_A_ a, port_input | port_output); 1708 port_close (SCHEME_A_ a, port_input | port_output);
1706 1709
1707 free (a->object.port); 1710 free (port (a));
1708 } 1711 }
1709#endif 1712#endif
1710} 1713}
1711 1714
1712/* ========== Routines for Reading ========== */ 1715/* ========== Routines for Reading ========== */
1728 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; 1731 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1;
1729 SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; 1732 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; 1733 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; 1734 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1;
1732 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; 1735 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0;
1733 SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; 1736 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1734 1737
1735#if SHOW_ERROR_LINE 1738#if SHOW_ERROR_LINE
1736 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; 1739 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0;
1737 1740
1738 if (fname) 1741 if (fname)
1755 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1758 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1756#if USE_PORTS 1759#if USE_PORTS
1757 port_close (SCHEME_A_ SCHEME_V->loadport, port_input); 1760 port_close (SCHEME_A_ SCHEME_V->loadport, port_input);
1758#endif 1761#endif
1759 SCHEME_V->file_i--; 1762 SCHEME_V->file_i--;
1760 SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; 1763 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1761 } 1764 }
1762} 1765}
1763 1766
1764static int 1767static int
1765file_interactive (SCHEME_P) 1768file_interactive (SCHEME_P)
1766{ 1769{
1767#if USE_PORTS 1770#if USE_PORTS
1768 return SCHEME_V->file_i == 0 1771 return SCHEME_V->file_i == 0
1769 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1772 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1770 && (SCHEME_V->inport->object.port->kind & port_file); 1773 && (port (SCHEME_V->inport)->kind & port_file);
1771#else 1774#else
1772 return 0; 1775 return 0;
1773#endif 1776#endif
1774} 1777}
1775 1778
1909} 1912}
1910 1913
1911static void 1914static void
1912port_close (SCHEME_P_ pointer p, int flag) 1915port_close (SCHEME_P_ pointer p, int flag)
1913{ 1916{
1914 port *pt = p->object.port; 1917 port *pt = port (p);
1915 1918
1916 pt->kind &= ~flag; 1919 pt->kind &= ~flag;
1917 1920
1918 if ((pt->kind & (port_input | port_output)) == 0) 1921 if ((pt->kind & (port_input | port_output)) == 0)
1919 { 1922 {
1940/* get new character from input file */ 1943/* get new character from input file */
1941static int 1944static int
1942inchar (SCHEME_P) 1945inchar (SCHEME_P)
1943{ 1946{
1944 int c; 1947 int c;
1945 port *pt; 1948 port *pt = port (SCHEME_V->inport);
1946
1947 pt = SCHEME_V->inport->object.port;
1948 1949
1949 if (pt->kind & port_saw_EOF) 1950 if (pt->kind & port_saw_EOF)
1950 return EOF; 1951 return EOF;
1951 1952
1952 c = basic_inchar (pt); 1953 c = basic_inchar (pt);
2019 port *pt; 2020 port *pt;
2020 2021
2021 if (c == EOF) 2022 if (c == EOF)
2022 return; 2023 return;
2023 2024
2024 pt = SCHEME_V->inport->object.port; 2025 pt = port (SCHEME_V->inport);
2025 pt->unget = c; 2026 pt->unget = c;
2026#else 2027#else
2027 if (c == EOF) 2028 if (c == EOF)
2028 return; 2029 return;
2029 2030
2057 2058
2058INTERFACE void 2059INTERFACE void
2059putstr (SCHEME_P_ const char *s) 2060putstr (SCHEME_P_ const char *s)
2060{ 2061{
2061#if USE_PORTS 2062#if USE_PORTS
2062 port *pt = SCHEME_V->outport->object.port; 2063 port *pt = port (SCHEME_V->outport);
2063 2064
2064 if (pt->kind & port_file) 2065 if (pt->kind & port_file)
2065 write (pt->rep.stdio.file, s, strlen (s)); 2066 write (pt->rep.stdio.file, s, strlen (s));
2066 else 2067 else
2067 for (; *s; s++) 2068 for (; *s; s++)
2069 *pt->rep.string.curr++ = *s; 2070 *pt->rep.string.curr++ = *s;
2070 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) 2071 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2071 *pt->rep.string.curr++ = *s; 2072 *pt->rep.string.curr++ = *s;
2072 2073
2073#else 2074#else
2074 xwrstr (s); 2075 write (pt->rep.stdio.file, s, strlen (s));
2075#endif 2076#endif
2076} 2077}
2077 2078
2078static void 2079static void
2079putchars (SCHEME_P_ const char *s, int len) 2080putchars (SCHEME_P_ const char *s, int len)
2080{ 2081{
2081#if USE_PORTS 2082#if USE_PORTS
2082 port *pt = SCHEME_V->outport->object.port; 2083 port *pt = port (SCHEME_V->outport);
2083 2084
2084 if (pt->kind & port_file) 2085 if (pt->kind & port_file)
2085 write (pt->rep.stdio.file, s, len); 2086 write (pt->rep.stdio.file, s, len);
2086 else 2087 else
2087 { 2088 {
2101 2102
2102INTERFACE void 2103INTERFACE void
2103putcharacter (SCHEME_P_ int c) 2104putcharacter (SCHEME_P_ int c)
2104{ 2105{
2105#if USE_PORTS 2106#if USE_PORTS
2106 port *pt = SCHEME_V->outport->object.port; 2107 port *pt = port (SCHEME_V->outport);
2107 2108
2108 if (pt->kind & port_file) 2109 if (pt->kind & port_file)
2109 { 2110 {
2110 char cc = c; 2111 char cc = c;
2111 write (pt->rep.stdio.file, &cc, 1); 2112 write (pt->rep.stdio.file, &cc, 1);
2124#endif 2125#endif
2125} 2126}
2126 2127
2127/* read characters up to delimiter, but cater to character constants */ 2128/* read characters up to delimiter, but cater to character constants */
2128static char * 2129static char *
2129readstr_upto (SCHEME_P_ char *delim) 2130readstr_upto (SCHEME_P_ int skip, const char *delim)
2130{ 2131{
2131 char *p = SCHEME_V->strbuff; 2132 char *p = SCHEME_V->strbuff + skip;
2132 2133
2133 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2134 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2134 2135
2135 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2136 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2136 *p = 0; 2137 *p = 0;
2143 return SCHEME_V->strbuff; 2144 return SCHEME_V->strbuff;
2144} 2145}
2145 2146
2146/* read string expression "xxx...xxx" */ 2147/* read string expression "xxx...xxx" */
2147static pointer 2148static pointer
2148readstrexp (SCHEME_P) 2149readstrexp (SCHEME_P_ char delim)
2149{ 2150{
2150 char *p = SCHEME_V->strbuff; 2151 char *p = SCHEME_V->strbuff;
2151 int c; 2152 int c;
2152 int c1 = 0; 2153 int c1 = 0;
2153 enum
2154 { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; 2154 enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok;
2155 2155
2156 for (;;) 2156 for (;;)
2157 { 2157 {
2158 c = inchar (SCHEME_A); 2158 c = inchar (SCHEME_A);
2159 2159
2161 return S_F; 2161 return S_F;
2162 2162
2163 switch (state) 2163 switch (state)
2164 { 2164 {
2165 case st_ok: 2165 case st_ok:
2166 switch (c) 2166 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); 2167 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff);
2175 2168
2176 default: 2169 if (ecb_expect_false (c == '\\'))
2170 state = st_bsl;
2171 else
2177 *p++ = c; 2172 *p++ = c;
2178 break;
2179 }
2180 2173
2181 break; 2174 break;
2182 2175
2183 case st_bsl: 2176 case st_bsl:
2184 switch (c) 2177 switch (c)
2193 case '7': 2186 case '7':
2194 state = st_oct1; 2187 state = st_oct1;
2195 c1 = c - '0'; 2188 c1 = c - '0';
2196 break; 2189 break;
2197 2190
2191 case 'a': *p++ = '\a'; state = st_ok; break;
2192 case 'n': *p++ = '\n'; state = st_ok; break;
2193 case 'r': *p++ = '\r'; state = st_ok; break;
2194 case 't': *p++ = '\t'; state = st_ok; break;
2195
2196 //TODO: \whitespace eol whitespace
2197
2198 //TODO: x should end in ;, not two-digit hex
2198 case 'x': 2199 case 'x':
2199 case 'X': 2200 case 'X':
2200 state = st_x1; 2201 state = st_x1;
2201 c1 = 0; 2202 c1 = 0;
2202 break; 2203 break;
2203 2204
2204 case 'n':
2205 *p++ = '\n';
2206 state = st_ok;
2207 break;
2208
2209 case 't':
2210 *p++ = '\t';
2211 state = st_ok;
2212 break;
2213
2214 case 'r':
2215 *p++ = '\r';
2216 state = st_ok;
2217 break;
2218
2219 case '"':
2220 *p++ = '"';
2221 state = st_ok;
2222 break;
2223
2224 default: 2205 default:
2225 *p++ = c; 2206 *p++ = c;
2226 state = st_ok; 2207 state = st_ok;
2227 break; 2208 break;
2228 } 2209 }
2229 2210
2230 break; 2211 break;
2231 2212
2232 case st_x1: 2213 case st_x1:
2233 case st_x2: 2214 case st_x2:
2234 c = toupper (c); 2215 c = tolower (c);
2235 2216
2236 if (c >= '0' && c <= 'F') 2217 if (c >= '0' && c <= '9')
2237 {
2238 if (c <= '9')
2239 c1 = (c1 << 4) + c - '0'; 2218 c1 = (c1 << 4) + c - '0';
2240 else 2219 else if (c >= 'a' && c <= 'f')
2241 c1 = (c1 << 4) + c - 'A' + 10; 2220 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 2221 else
2252 return S_F; 2222 return S_F;
2223
2224 if (state == st_x1)
2225 state = st_x2;
2226 else
2227 {
2228 *p++ = c1;
2229 state = st_ok;
2230 }
2253 2231
2254 break; 2232 break;
2255 2233
2256 case st_oct1: 2234 case st_oct1:
2257 case st_oct2: 2235 case st_oct2:
2261 backchar (SCHEME_A_ c); 2239 backchar (SCHEME_A_ c);
2262 state = st_ok; 2240 state = st_ok;
2263 } 2241 }
2264 else 2242 else
2265 { 2243 {
2266 if (state == st_oct2 && c1 >= 32) 2244 if (state == st_oct2 && c1 >= ' ')
2267 return S_F; 2245 return S_F;
2268 2246
2269 c1 = (c1 << 3) + (c - '0'); 2247 c1 = (c1 << 3) + (c - '0');
2270 2248
2271 if (state == st_oct1) 2249 if (state == st_oct1)
2276 state = st_ok; 2254 state = st_ok;
2277 } 2255 }
2278 } 2256 }
2279 2257
2280 break; 2258 break;
2281
2282 } 2259 }
2283 } 2260 }
2284} 2261}
2285 2262
2286/* check c is in chars */ 2263/* check c is in chars */
2287static INLINE int 2264ecb_inline int
2288is_one_of (char *s, int c) 2265is_one_of (const char *s, int c)
2289{ 2266{
2290 if (c == EOF)
2291 return 1;
2292
2293 return !!strchr (s, c); 2267 return c == EOF || !!strchr (s, c);
2294} 2268}
2295 2269
2296/* skip white characters */ 2270/* skip white characters */
2297static INLINE int 2271ecb_inline int
2298skipspace (SCHEME_P) 2272skipspace (SCHEME_P)
2299{ 2273{
2300 int c, curr_line = 0; 2274 int c, curr_line = 0;
2301 2275
2302 do 2276 do
2303 { 2277 {
2304 c = inchar (SCHEME_A); 2278 c = inchar (SCHEME_A);
2279
2305#if SHOW_ERROR_LINE 2280#if SHOW_ERROR_LINE
2306 if (c == '\n') 2281 if (ecb_expect_false (c == '\n'))
2307 curr_line++; 2282 curr_line++;
2308#endif 2283#endif
2284
2285 if (ecb_expect_false (c == EOF))
2286 return c;
2309 } 2287 }
2310 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2288 while (is_one_of (WHITESPACE, c));
2311 2289
2312 /* record it */ 2290 /* record it */
2313#if SHOW_ERROR_LINE 2291#if SHOW_ERROR_LINE
2314 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) 2292 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; 2293 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line;
2316#endif 2294#endif
2317 2295
2318 if (c != EOF)
2319 {
2320 backchar (SCHEME_A_ c); 2296 backchar (SCHEME_A_ c);
2321 return 1; 2297 return 1;
2322 }
2323 else
2324 return EOF;
2325} 2298}
2326 2299
2327/* get token */ 2300/* get token */
2328static int 2301static int
2329token (SCHEME_P) 2302token (SCHEME_P)
2345 return TOK_RPAREN; 2318 return TOK_RPAREN;
2346 2319
2347 case '.': 2320 case '.':
2348 c = inchar (SCHEME_A); 2321 c = inchar (SCHEME_A);
2349 2322
2350 if (is_one_of (" \n\t", c)) 2323 if (is_one_of (WHITESPACE, c))
2351 return TOK_DOT; 2324 return TOK_DOT;
2352 else 2325 else
2353 { 2326 {
2354 //TODO: ungetc twice in a row is not supported in C
2355 backchar (SCHEME_A_ c); 2327 backchar (SCHEME_A_ c);
2356 backchar (SCHEME_A_ '.');
2357 return TOK_ATOM; 2328 return TOK_DOTATOM;
2358 } 2329 }
2330
2331 case '|':
2332 return TOK_STRATOM;
2359 2333
2360 case '\'': 2334 case '\'':
2361 return TOK_QUOTE; 2335 return TOK_QUOTE;
2362 2336
2363 case ';': 2337 case ';':
2495 } 2469 }
2496 2470
2497 putcharacter (SCHEME_A_ '"'); 2471 putcharacter (SCHEME_A_ '"');
2498} 2472}
2499 2473
2500
2501/* print atoms */ 2474/* print atoms */
2502static void 2475static void
2503printatom (SCHEME_P_ pointer l, int f) 2476printatom (SCHEME_P_ pointer l, int f)
2504{ 2477{
2505 char *p; 2478 char *p;
2506 int len; 2479 int len;
2507 2480
2508 atom2str (SCHEME_A_ l, f, &p, &len); 2481 atom2str (SCHEME_A_ l, f, &p, &len);
2509 putchars (SCHEME_A_ p, len); 2482 putchars (SCHEME_A_ p, len);
2510} 2483}
2511
2512 2484
2513/* Uses internal buffer unless string pointer is already available */ 2485/* Uses internal buffer unless string pointer is already available */
2514static void 2486static void
2515atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2487atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2516{ 2488{
2530 { 2502 {
2531 p = SCHEME_V->strbuff; 2503 p = SCHEME_V->strbuff;
2532 2504
2533 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2505 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2534 { 2506 {
2535 if (num_is_integer (l)) 2507 if (is_integer (l))
2536 xnum (p, ivalue_unchecked (l)); 2508 xnum (p, ivalue_unchecked (l));
2537#if USE_REAL 2509#if USE_REAL
2538 else 2510 else
2539 { 2511 {
2540 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2512 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2677#endif 2649#endif
2678 } 2650 }
2679 else if (is_continuation (l)) 2651 else if (is_continuation (l))
2680 p = "#<CONTINUATION>"; 2652 p = "#<CONTINUATION>";
2681 else 2653 else
2654 {
2655#if USE_PRINTF
2656 p = SCHEME_V->strbuff;
2657 snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l));
2658#else
2682 p = "#<ERROR>"; 2659 p = "#<ERROR>";
2660#endif
2661 }
2683 2662
2684 *pp = p; 2663 *pp = p;
2685 *plen = strlen (p); 2664 *plen = strlen (p);
2686} 2665}
2687 2666
2795 return 0; 2774 return 0;
2796 } 2775 }
2797 else if (is_number (a)) 2776 else if (is_number (a))
2798 { 2777 {
2799 if (is_number (b)) 2778 if (is_number (b))
2800 if (num_is_integer (a) == num_is_integer (b))
2801 return num_eq (nvalue (a), nvalue (b)); 2779 return num_cmp (nvalue (a), nvalue (b)) == 0;
2802 2780
2803 return 0; 2781 return 0;
2804 } 2782 }
2805 else if (is_character (a)) 2783 else if (is_character (a))
2806 { 2784 {
2832/* () is #t in R5RS */ 2810/* () is #t in R5RS */
2833#define is_true(p) ((p) != S_F) 2811#define is_true(p) ((p) != S_F)
2834#define is_false(p) ((p) == S_F) 2812#define is_false(p) ((p) == S_F)
2835 2813
2836/* ========== Environment implementation ========== */ 2814/* ========== 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 2815
2853#ifndef USE_ALIST_ENV 2816#ifndef USE_ALIST_ENV
2854 2817
2855/* 2818/*
2856 * In this implementation, each frame of the environment may be 2819 * In this implementation, each frame of the environment may be
2873 2836
2874 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2837 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2875 setenvironment (SCHEME_V->envir); 2838 setenvironment (SCHEME_V->envir);
2876} 2839}
2877 2840
2878static INLINE void 2841static uint32_t
2842sym_hash (pointer sym, uint32_t size)
2843{
2844 uintptr_t ptr = (uintptr_t)sym;
2845
2846#if 0
2847 /* table size is prime, so why mix */
2848 ptr += ptr >> 32;
2849 ptr += ptr >> 16;
2850 ptr += ptr >> 8;
2851#endif
2852
2853 return ptr % size;
2854}
2855
2856ecb_inline void
2879new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2857new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2880{ 2858{
2881 pointer slot = immutable_cons (variable, value); 2859 pointer slot = immutable_cons (variable, value);
2882 2860
2883 if (is_vector (car (env))) 2861 if (is_vector (car (env)))
2884 { 2862 {
2885 int location = hash_fn (symname (variable), veclength (car (env))); 2863 int location = sym_hash (variable, veclength (car (env)));
2886
2887 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2864 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2888 } 2865 }
2889 else 2866 else
2890 set_car (env, immutable_cons (slot, car (env))); 2867 set_car (env, immutable_cons (slot, car (env)));
2891} 2868}
2892 2869
2893static pointer 2870static pointer
2894find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2871find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2895{ 2872{
2896 pointer x, y; 2873 pointer x, y;
2897 int location;
2898 2874
2899 for (x = env; x != NIL; x = cdr (x)) 2875 for (x = env; x != NIL; x = cdr (x))
2900 { 2876 {
2901 if (is_vector (car (x))) 2877 if (is_vector (car (x)))
2902 { 2878 {
2903 location = hash_fn (symname (hdl), veclength (car (x))); 2879 int location = sym_hash (hdl, veclength (car (x)));
2904 y = vector_elem (car (x), location); 2880 y = vector_get (car (x), location);
2905 } 2881 }
2906 else 2882 else
2907 y = car (x); 2883 y = car (x);
2908 2884
2909 for (; y != NIL; y = cdr (y)) 2885 for (; y != NIL; y = cdr (y))
2910 if (caar (y) == hdl) 2886 if (caar (y) == hdl)
2911 break; 2887 break;
2912 2888
2913 if (y != NIL) 2889 if (y != NIL)
2890 return car (y);
2891
2892 if (!all)
2914 break; 2893 break;
2915
2916 if (!all)
2917 return NIL;
2918 } 2894 }
2919
2920 if (x != NIL)
2921 return car (y);
2922 2895
2923 return NIL; 2896 return NIL;
2924} 2897}
2925 2898
2926#else /* USE_ALIST_ENV */ 2899#else /* USE_ALIST_ENV */
2927 2900
2928static INLINE void 2901ecb_inline void
2929new_frame_in_env (SCHEME_P_ pointer old_env) 2902new_frame_in_env (SCHEME_P_ pointer old_env)
2930{ 2903{
2931 SCHEME_V->envir = immutable_cons (NIL, old_env); 2904 SCHEME_V->envir = immutable_cons (NIL, old_env);
2932 setenvironment (SCHEME_V->envir); 2905 setenvironment (SCHEME_V->envir);
2933} 2906}
2934 2907
2935static INLINE void 2908ecb_inline void
2936new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2909new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2937{ 2910{
2938 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2911 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2939} 2912}
2940 2913
2948 for (y = car (x); y != NIL; y = cdr (y)) 2921 for (y = car (x); y != NIL; y = cdr (y))
2949 if (caar (y) == hdl) 2922 if (caar (y) == hdl)
2950 break; 2923 break;
2951 2924
2952 if (y != NIL) 2925 if (y != NIL)
2926 return car (y);
2953 break; 2927 break;
2954 2928
2955 if (!all) 2929 if (!all)
2956 return NIL; 2930 break;
2957 } 2931 }
2958
2959 if (x != NIL)
2960 return car (y);
2961 2932
2962 return NIL; 2933 return NIL;
2963} 2934}
2964 2935
2965#endif /* USE_ALIST_ENV else */ 2936#endif /* USE_ALIST_ENV else */
2966 2937
2967static INLINE void 2938ecb_inline void
2968new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2939new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2969{ 2940{
2941 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2970 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2942 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2971} 2943}
2972 2944
2973static INLINE void 2945ecb_inline void
2974set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2946set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2975{ 2947{
2976 set_cdr (slot, value); 2948 set_cdr (slot, value);
2977} 2949}
2978 2950
2979static INLINE pointer 2951ecb_inline pointer
2980slot_value_in_env (pointer slot) 2952slot_value_in_env (pointer slot)
2981{ 2953{
2982 return cdr (slot); 2954 return cdr (slot);
2983} 2955}
2984 2956
2985/* ========== Evaluation Cycle ========== */ 2957/* ========== Evaluation Cycle ========== */
2986 2958
2987static pointer 2959static int
2988xError_1 (SCHEME_P_ const char *s, pointer a) 2960xError_1 (SCHEME_P_ const char *s, pointer a)
2989{ 2961{
2990#if USE_ERROR_HOOK 2962#if USE_ERROR_HOOK
2991 pointer x; 2963 pointer x;
2992 pointer hdl = SCHEME_V->ERROR_HOOK; 2964 pointer hdl = SCHEME_V->ERROR_HOOK;
3027 code = cons (mk_string (SCHEME_A_ s), code); 2999 code = cons (mk_string (SCHEME_A_ s), code);
3028 setimmutable (car (code)); 3000 setimmutable (car (code));
3029 SCHEME_V->code = cons (slot_value_in_env (x), code); 3001 SCHEME_V->code = cons (slot_value_in_env (x), code);
3030 SCHEME_V->op = OP_EVAL; 3002 SCHEME_V->op = OP_EVAL;
3031 3003
3032 return S_T; 3004 return 0;
3033 } 3005 }
3034#endif 3006#endif
3035 3007
3036 if (a) 3008 if (a)
3037 SCHEME_V->args = cons (a, NIL); 3009 SCHEME_V->args = cons (a, NIL);
3039 SCHEME_V->args = NIL; 3011 SCHEME_V->args = NIL;
3040 3012
3041 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); 3013 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args);
3042 setimmutable (car (SCHEME_V->args)); 3014 setimmutable (car (SCHEME_V->args));
3043 SCHEME_V->op = OP_ERR0; 3015 SCHEME_V->op = OP_ERR0;
3016
3044 return S_T; 3017 return 0;
3045} 3018}
3046 3019
3047#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) 3020#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a)
3048#define Error_0(s) Error_1 (s, 0) 3021#define Error_0(s) Error_1 (s, 0)
3049 3022
3050/* Too small to turn into function */ 3023/* Too small to turn into function */
3051#define BEGIN do { 3024#define BEGIN do {
3052#define END } while (0) 3025#define END } while (0)
3053#define s_goto(a) BEGIN \ 3026#define s_goto(a) BEGIN \
3054 SCHEME_V->op = a; \ 3027 SCHEME_V->op = a; \
3055 return S_T; END 3028 return 0; END
3056 3029
3057#define s_return(a) return xs_return (SCHEME_A_ a) 3030#define s_return(a) return xs_return (SCHEME_A_ a)
3058 3031
3059#ifndef USE_SCHEME_STACK 3032#ifndef USE_SCHEME_STACK
3060 3033
3074{ 3047{
3075 int nframes = (uintptr_t)SCHEME_V->dump; 3048 int nframes = (uintptr_t)SCHEME_V->dump;
3076 struct dump_stack_frame *next_frame; 3049 struct dump_stack_frame *next_frame;
3077 3050
3078 /* enough room for the next frame? */ 3051 /* enough room for the next frame? */
3079 if (nframes >= SCHEME_V->dump_size) 3052 if (ecb_expect_false (nframes >= SCHEME_V->dump_size))
3080 { 3053 {
3081 SCHEME_V->dump_size += STACK_GROWTH; 3054 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); 3055 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size);
3083 } 3056 }
3084 3057
3085 next_frame = SCHEME_V->dump_base + nframes; 3058 next_frame = SCHEME_V->dump_base + nframes;
3086 3059
3087 next_frame->op = op; 3060 next_frame->op = op;
3088 next_frame->args = args; 3061 next_frame->args = args;
3089 next_frame->envir = SCHEME_V->envir; 3062 next_frame->envir = SCHEME_V->envir;
3090 next_frame->code = code; 3063 next_frame->code = code;
3091 3064
3092 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3065 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3093} 3066}
3094 3067
3095static pointer 3068static int
3096xs_return (SCHEME_P_ pointer a) 3069xs_return (SCHEME_P_ pointer a)
3097{ 3070{
3098 int nframes = (uintptr_t)SCHEME_V->dump; 3071 int nframes = (uintptr_t)SCHEME_V->dump;
3099 struct dump_stack_frame *frame; 3072 struct dump_stack_frame *frame;
3100 3073
3101 SCHEME_V->value = a; 3074 SCHEME_V->value = a;
3102 3075
3103 if (nframes <= 0) 3076 if (nframes <= 0)
3104 return NIL; 3077 return -1;
3105 3078
3106 frame = &SCHEME_V->dump_base[--nframes]; 3079 frame = &SCHEME_V->dump_base[--nframes];
3107 SCHEME_V->op = frame->op; 3080 SCHEME_V->op = frame->op;
3108 SCHEME_V->args = frame->args; 3081 SCHEME_V->args = frame->args;
3109 SCHEME_V->envir = frame->envir; 3082 SCHEME_V->envir = frame->envir;
3110 SCHEME_V->code = frame->code; 3083 SCHEME_V->code = frame->code;
3111 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3084 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3112 3085
3113 return S_T; 3086 return 0;
3114} 3087}
3115 3088
3116static INLINE void 3089ecb_inline void
3117dump_stack_reset (SCHEME_P) 3090dump_stack_reset (SCHEME_P)
3118{ 3091{
3119 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3092 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3120 SCHEME_V->dump = (pointer)+0; 3093 SCHEME_V->dump = (pointer)+0;
3121} 3094}
3122 3095
3123static INLINE void 3096ecb_inline void
3124dump_stack_initialize (SCHEME_P) 3097dump_stack_initialize (SCHEME_P)
3125{ 3098{
3126 SCHEME_V->dump_size = 0; 3099 SCHEME_V->dump_size = 0;
3127 SCHEME_V->dump_base = 0; 3100 SCHEME_V->dump_base = 0;
3128 dump_stack_reset (SCHEME_A); 3101 dump_stack_reset (SCHEME_A);
3181 int i = 0; 3154 int i = 0;
3182 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3155 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3183 3156
3184 while (cont != NIL) 3157 while (cont != NIL)
3185 { 3158 {
3186 frame->op = ivalue (car (cont)); cont = cdr (cont); 3159 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3187 frame->args = car (cont) ; cont = cdr (cont); 3160 frame->args = car (cont) ; cont = cdr (cont);
3188 frame->envir = car (cont) ; cont = cdr (cont); 3161 frame->envir = car (cont) ; cont = cdr (cont);
3189 frame->code = car (cont) ; cont = cdr (cont); 3162 frame->code = car (cont) ; cont = cdr (cont);
3190 3163
3191 ++frame; 3164 ++frame;
3192 ++i; 3165 ++i;
3193 } 3166 }
3194 3167
3195 SCHEME_V->dump = (pointer)(uintptr_t)i; 3168 SCHEME_V->dump = (pointer)(uintptr_t)i;
3196} 3169}
3197 3170
3198#else 3171#else
3199 3172
3200static INLINE void 3173ecb_inline void
3201dump_stack_reset (SCHEME_P) 3174dump_stack_reset (SCHEME_P)
3202{ 3175{
3203 SCHEME_V->dump = NIL; 3176 SCHEME_V->dump = NIL;
3204} 3177}
3205 3178
3206static INLINE void 3179ecb_inline void
3207dump_stack_initialize (SCHEME_P) 3180dump_stack_initialize (SCHEME_P)
3208{ 3181{
3209 dump_stack_reset (SCHEME_A); 3182 dump_stack_reset (SCHEME_A);
3210} 3183}
3211 3184
3213dump_stack_free (SCHEME_P) 3186dump_stack_free (SCHEME_P)
3214{ 3187{
3215 SCHEME_V->dump = NIL; 3188 SCHEME_V->dump = NIL;
3216} 3189}
3217 3190
3218static pointer 3191static int
3219xs_return (SCHEME_P_ pointer a) 3192xs_return (SCHEME_P_ pointer a)
3220{ 3193{
3221 pointer dump = SCHEME_V->dump; 3194 pointer dump = SCHEME_V->dump;
3222 3195
3223 SCHEME_V->value = a; 3196 SCHEME_V->value = a;
3224 3197
3225 if (dump == NIL) 3198 if (dump == NIL)
3226 return NIL; 3199 return -1;
3227 3200
3228 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3201 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3229 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3202 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3230 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3203 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3231 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3204 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3232 3205
3233 SCHEME_V->dump = dump; 3206 SCHEME_V->dump = dump;
3234 3207
3235 return S_T; 3208 return 0;
3236} 3209}
3237 3210
3238static void 3211static void
3239s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3212s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3240{ 3213{
3265 3238
3266#endif 3239#endif
3267 3240
3268#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3241#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3269 3242
3243#if EXPERIMENT
3270static pointer 3244static int
3245debug (SCHEME_P_ int indent, pointer x)
3246{
3247 int c;
3248
3249 if (is_syntax (x))
3250 {
3251 printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x));
3252 return 8 + 8;
3253 }
3254
3255 if (x == NIL)
3256 {
3257 printf ("%*sNIL\n", indent, "");
3258 return 3;
3259 }
3260
3261 switch (type (x))
3262 {
3263 case T_INTEGER:
3264 printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x);
3265 return 32+8;
3266
3267 case T_SYMBOL:
3268 printf ("%*sS<%s>\n", indent, "", symname (x));
3269 return 24+8;
3270
3271 case T_CLOSURE:
3272 printf ("%*sS<%s>\n", indent, "", "closure");
3273 debug (SCHEME_A_ indent + 3, cdr(x));
3274 return 32 + debug (SCHEME_A_ indent + 3, car (x));
3275
3276 case T_PAIR:
3277 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3278 c = debug (SCHEME_A_ indent + 3, car (x));
3279 c += debug (SCHEME_A_ indent + 3, cdr (x));
3280 return c + 1;
3281
3282 case T_PORT:
3283 printf ("%*sS<%s>\n", indent, "", "port");
3284 return 24+8;
3285
3286 case T_VECTOR:
3287 printf ("%*sS<%s>\n", indent, "", "vector");
3288 return 24+8;
3289
3290 case T_ENVIRONMENT:
3291 printf ("%*sS<%s>\n", indent, "", "environment");
3292 return 0 + debug (SCHEME_A_ indent + 3, car (x));
3293
3294 default:
3295 printf ("unhandled type %d\n", type (x));
3296 break;
3297 }
3298}
3299#endif
3300
3301static int
3271opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3302opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3272{ 3303{
3304 pointer args = SCHEME_V->args;
3273 pointer x, y; 3305 pointer x, y;
3274 3306
3275 switch (op) 3307 switch (op)
3276 { 3308 {
3309#if EXPERIMENT //D
3310 case OP_DEBUG:
3311 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8);
3312 printf ("\n");
3313 s_return (S_T);
3314#endif
3277 case OP_LOAD: /* load */ 3315 case OP_LOAD: /* load */
3278 if (file_interactive (SCHEME_A)) 3316 if (file_interactive (SCHEME_A))
3279 { 3317 {
3280 xwrstr ("Loading "); xwrstr (strvalue (car (SCHEME_V->args))); xwrstr ("\n"); 3318 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))); 3319 //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args)));
3282 } 3320 }
3283 3321
3284 if (!file_push (SCHEME_A_ strvalue (car (SCHEME_V->args)))) 3322 if (!file_push (SCHEME_A_ strvalue (car (args))))
3285 Error_1 ("unable to open", car (SCHEME_V->args)); 3323 Error_1 ("unable to open", car (args));
3286 else 3324 else
3287 { 3325 {
3288 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3326 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3289 s_goto (OP_T0LVL); 3327 s_goto (OP_T0LVL);
3290 } 3328 }
3291 3329
3292 case OP_T0LVL: /* top level */ 3330 case OP_T0LVL: /* top level */
3293 3331
3294 /* If we reached the end of file, this loop is done. */ 3332 /* If we reached the end of file, this loop is done. */
3295 if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) 3333 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3296 { 3334 {
3297 if (SCHEME_V->file_i == 0) 3335 if (SCHEME_V->file_i == 0)
3298 { 3336 {
3299 SCHEME_V->args = NIL; 3337 SCHEME_V->args = NIL;
3300 s_goto (OP_QUIT); 3338 s_goto (OP_QUIT);
3364 case OP_EVAL: /* main part of evaluation */ 3402 case OP_EVAL: /* main part of evaluation */
3365#if USE_TRACING 3403#if USE_TRACING
3366 if (SCHEME_V->tracing) 3404 if (SCHEME_V->tracing)
3367 { 3405 {
3368 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ 3406 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */
3369 s_save (SCHEME_A_ OP_REAL_EVAL, SCHEME_V->args, SCHEME_V->code); 3407 s_save (SCHEME_A_ OP_REAL_EVAL, args, SCHEME_V->code);
3370 SCHEME_V->args = SCHEME_V->code; 3408 SCHEME_V->args = SCHEME_V->code;
3371 putstr (SCHEME_A_ "\nEval: "); 3409 putstr (SCHEME_A_ "\nEval: ");
3372 s_goto (OP_P0LIST); 3410 s_goto (OP_P0LIST);
3373 } 3411 }
3374 3412
3378#endif 3416#endif
3379 if (is_symbol (SCHEME_V->code)) /* symbol */ 3417 if (is_symbol (SCHEME_V->code)) /* symbol */
3380 { 3418 {
3381 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); 3419 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1);
3382 3420
3383 if (x != NIL) 3421 if (x == NIL)
3384 s_return (slot_value_in_env (x));
3385 else
3386 Error_1 ("eval: unbound variable:", SCHEME_V->code); 3422 Error_1 ("eval: unbound variable:", SCHEME_V->code);
3423
3424 s_return (slot_value_in_env (x));
3387 } 3425 }
3388 else if (is_pair (SCHEME_V->code)) 3426 else if (is_pair (SCHEME_V->code))
3389 { 3427 {
3390 x = car (SCHEME_V->code); 3428 x = car (SCHEME_V->code);
3391 3429
3404 } 3442 }
3405 else 3443 else
3406 s_return (SCHEME_V->code); 3444 s_return (SCHEME_V->code);
3407 3445
3408 case OP_E0ARGS: /* eval arguments */ 3446 case OP_E0ARGS: /* eval arguments */
3409 if (is_macro (SCHEME_V->value)) /* macro expansion */ 3447 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3410 { 3448 {
3411 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3449 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3412 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3450 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3413 SCHEME_V->code = SCHEME_V->value; 3451 SCHEME_V->code = SCHEME_V->value;
3414 s_goto (OP_APPLY); 3452 s_goto (OP_APPLY);
3418 SCHEME_V->code = cdr (SCHEME_V->code); 3456 SCHEME_V->code = cdr (SCHEME_V->code);
3419 s_goto (OP_E1ARGS); 3457 s_goto (OP_E1ARGS);
3420 } 3458 }
3421 3459
3422 case OP_E1ARGS: /* eval arguments */ 3460 case OP_E1ARGS: /* eval arguments */
3423 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3461 args = cons (SCHEME_V->value, args);
3424 3462
3425 if (is_pair (SCHEME_V->code)) /* continue */ 3463 if (is_pair (SCHEME_V->code)) /* continue */
3426 { 3464 {
3427 s_save (SCHEME_A_ OP_E1ARGS, SCHEME_V->args, cdr (SCHEME_V->code)); 3465 s_save (SCHEME_A_ OP_E1ARGS, args, cdr (SCHEME_V->code));
3428 SCHEME_V->code = car (SCHEME_V->code); 3466 SCHEME_V->code = car (SCHEME_V->code);
3429 SCHEME_V->args = NIL; 3467 SCHEME_V->args = NIL;
3430 s_goto (OP_EVAL); 3468 s_goto (OP_EVAL);
3431 } 3469 }
3432 else /* end */ 3470 else /* end */
3433 { 3471 {
3434 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3472 args = reverse_in_place (SCHEME_A_ NIL, args);
3435 SCHEME_V->code = car (SCHEME_V->args); 3473 SCHEME_V->code = car (args);
3436 SCHEME_V->args = cdr (SCHEME_V->args); 3474 SCHEME_V->args = cdr (args);
3437 s_goto (OP_APPLY); 3475 s_goto (OP_APPLY);
3438 } 3476 }
3439 3477
3440#if USE_TRACING 3478#if USE_TRACING
3441 3479
3442 case OP_TRACING: 3480 case OP_TRACING:
3443 { 3481 {
3444 int tr = SCHEME_V->tracing; 3482 int tr = SCHEME_V->tracing;
3445 3483
3446 SCHEME_V->tracing = ivalue (car (SCHEME_V->args)); 3484 SCHEME_V->tracing = ivalue_unchecked (car (args));
3447 s_return (mk_integer (SCHEME_A_ tr)); 3485 s_return (mk_integer (SCHEME_A_ tr));
3448 } 3486 }
3449 3487
3450#endif 3488#endif
3451 3489
3452 case OP_APPLY: /* apply 'code' to 'args' */ 3490 case OP_APPLY: /* apply 'code' to 'args' */
3453#if USE_TRACING 3491#if USE_TRACING
3454 if (SCHEME_V->tracing) 3492 if (SCHEME_V->tracing)
3455 { 3493 {
3456 s_save (SCHEME_A_ OP_REAL_APPLY, SCHEME_V->args, SCHEME_V->code); 3494 s_save (SCHEME_A_ OP_REAL_APPLY, args, SCHEME_V->code);
3457 SCHEME_V->print_flag = 1; 3495 SCHEME_V->print_flag = 1;
3458 /* SCHEME_V->args=cons(SCHEME_V->code,SCHEME_V->args); */ 3496 /* args=cons(SCHEME_V->code,args); */
3459 putstr (SCHEME_A_ "\nApply to: "); 3497 putstr (SCHEME_A_ "\nApply to: ");
3460 s_goto (OP_P0LIST); 3498 s_goto (OP_P0LIST);
3461 } 3499 }
3462 3500
3463 /* fall through */ 3501 /* fall through */
3464 3502
3465 case OP_REAL_APPLY: 3503 case OP_REAL_APPLY:
3466#endif 3504#endif
3467 if (is_proc (SCHEME_V->code)) 3505 if (is_proc (SCHEME_V->code))
3468 {
3469 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3506 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3470 }
3471 else if (is_foreign (SCHEME_V->code)) 3507 else if (is_foreign (SCHEME_V->code))
3472 { 3508 {
3473 /* Keep nested calls from GC'ing the arglist */ 3509 /* Keep nested calls from GC'ing the arglist */
3474 push_recent_alloc (SCHEME_A_ SCHEME_V->args, NIL); 3510 push_recent_alloc (SCHEME_A_ args, NIL);
3475 x = SCHEME_V->code->object.ff (SCHEME_A_ SCHEME_V->args); 3511 x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args);
3476 3512
3477 s_return (x); 3513 s_return (x);
3478 } 3514 }
3479 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ 3515 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */
3480 { 3516 {
3481 /* Should not accept promise */ 3517 /* Should not accept promise */
3482 /* make environment */ 3518 /* make environment */
3483 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); 3519 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code));
3484 3520
3485 for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) 3521 for (x = car (closure_code (SCHEME_V->code)), y = args; is_pair (x); x = cdr (x), y = cdr (y))
3486 { 3522 {
3487 if (y == NIL) 3523 if (y == NIL)
3488 Error_0 ("not enough arguments"); 3524 Error_0 ("not enough arguments");
3489 else 3525 else
3490 new_slot_in_env (SCHEME_A_ car (x), car (y)); 3526 new_slot_in_env (SCHEME_A_ car (x), car (y));
3508 s_goto (OP_BEGIN); 3544 s_goto (OP_BEGIN);
3509 } 3545 }
3510 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ 3546 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */
3511 { 3547 {
3512 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); 3548 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code));
3513 s_return (SCHEME_V->args != NIL ? car (SCHEME_V->args) : NIL); 3549 s_return (args != NIL ? car (args) : NIL);
3514 } 3550 }
3515 else 3551 else
3516 Error_0 ("illegal function"); 3552 Error_0 ("illegal function");
3517 3553
3518 case OP_DOMACRO: /* do macro */ 3554 case OP_DOMACRO: /* do macro */
3519 SCHEME_V->code = SCHEME_V->value; 3555 SCHEME_V->code = SCHEME_V->value;
3520 s_goto (OP_EVAL); 3556 s_goto (OP_EVAL);
3521
3522#if 1
3523 3557
3524 case OP_LAMBDA: /* lambda */ 3558 case OP_LAMBDA: /* lambda */
3525 /* If the hook is defined, apply it to SCHEME_V->code, otherwise 3559 /* If the hook is defined, apply it to SCHEME_V->code, otherwise
3526 set SCHEME_V->value fall thru */ 3560 set SCHEME_V->value fall thru */
3527 { 3561 {
3528 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); 3562 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1);
3529 3563
3530 if (f != NIL) 3564 if (f != NIL)
3531 { 3565 {
3532 s_save (SCHEME_A_ OP_LAMBDA1, SCHEME_V->args, SCHEME_V->code); 3566 s_save (SCHEME_A_ OP_LAMBDA1, args, SCHEME_V->code);
3533 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3567 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3534 SCHEME_V->code = slot_value_in_env (f); 3568 SCHEME_V->code = slot_value_in_env (f);
3535 s_goto (OP_APPLY); 3569 s_goto (OP_APPLY);
3536 } 3570 }
3537 3571
3538 SCHEME_V->value = SCHEME_V->code; 3572 SCHEME_V->value = SCHEME_V->code;
3539 /* Fallthru */
3540 } 3573 }
3574 /* Fallthru */
3541 3575
3542 case OP_LAMBDA1: 3576 case OP_LAMBDA1:
3543 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); 3577 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir));
3544 3578
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 */ 3579 case OP_MKCLOSURE: /* make-closure */
3553 x = car (SCHEME_V->args); 3580 x = car (args);
3554 3581
3555 if (car (x) == SCHEME_V->LAMBDA) 3582 if (car (x) == SCHEME_V->LAMBDA)
3556 x = cdr (x); 3583 x = cdr (x);
3557 3584
3558 if (cdr (SCHEME_V->args) == NIL) 3585 if (cdr (args) == NIL)
3559 y = SCHEME_V->envir; 3586 y = SCHEME_V->envir;
3560 else 3587 else
3561 y = cadr (SCHEME_V->args); 3588 y = cadr (args);
3562 3589
3563 s_return (mk_closure (SCHEME_A_ x, y)); 3590 s_return (mk_closure (SCHEME_A_ x, y));
3564 3591
3565 case OP_QUOTE: /* quote */ 3592 case OP_QUOTE: /* quote */
3566 s_return (car (SCHEME_V->code)); 3593 s_return (car (SCHEME_V->code));
3598 3625
3599 3626
3600 case OP_DEFP: /* defined? */ 3627 case OP_DEFP: /* defined? */
3601 x = SCHEME_V->envir; 3628 x = SCHEME_V->envir;
3602 3629
3603 if (cdr (SCHEME_V->args) != NIL) 3630 if (cdr (args) != NIL)
3604 x = cadr (SCHEME_V->args); 3631 x = cadr (args);
3605 3632
3606 s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); 3633 s_retbool (find_slot_in_env (SCHEME_A_ x, car (args), 1) != NIL);
3607 3634
3608 case OP_SET0: /* set! */ 3635 case OP_SET0: /* set! */
3609 if (is_immutable (car (SCHEME_V->code))) 3636 if (is_immutable (car (SCHEME_V->code)))
3610 Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code)); 3637 Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code));
3611 3638
3642 3669
3643 case OP_IF1: /* if */ 3670 case OP_IF1: /* if */
3644 if (is_true (SCHEME_V->value)) 3671 if (is_true (SCHEME_V->value))
3645 SCHEME_V->code = car (SCHEME_V->code); 3672 SCHEME_V->code = car (SCHEME_V->code);
3646 else 3673 else
3647 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because 3674 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
3648
3649 * car(NIL) = NIL */
3650 s_goto (OP_EVAL); 3675 s_goto (OP_EVAL);
3651 3676
3652 case OP_LET0: /* let */ 3677 case OP_LET0: /* let */
3653 SCHEME_V->args = NIL; 3678 SCHEME_V->args = NIL;
3654 SCHEME_V->value = SCHEME_V->code; 3679 SCHEME_V->value = SCHEME_V->code;
3655 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); 3680 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code);
3656 s_goto (OP_LET1); 3681 s_goto (OP_LET1);
3657 3682
3658 case OP_LET1: /* let (calculate parameters) */ 3683 case OP_LET1: /* let (calculate parameters) */
3659 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3684 args = cons (SCHEME_V->value, args);
3660 3685
3661 if (is_pair (SCHEME_V->code)) /* continue */ 3686 if (is_pair (SCHEME_V->code)) /* continue */
3662 { 3687 {
3663 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3688 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)); 3689 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code));
3665 3690
3666 s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); 3691 s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code));
3667 SCHEME_V->code = cadar (SCHEME_V->code); 3692 SCHEME_V->code = cadar (SCHEME_V->code);
3668 SCHEME_V->args = NIL; 3693 SCHEME_V->args = NIL;
3669 s_goto (OP_EVAL); 3694 s_goto (OP_EVAL);
3670 } 3695 }
3671 else /* end */ 3696 else /* end */
3672 { 3697 {
3673 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3698 args = reverse_in_place (SCHEME_A_ NIL, args);
3674 SCHEME_V->code = car (SCHEME_V->args); 3699 SCHEME_V->code = car (args);
3675 SCHEME_V->args = cdr (SCHEME_V->args); 3700 SCHEME_V->args = cdr (args);
3676 s_goto (OP_LET2); 3701 s_goto (OP_LET2);
3677 } 3702 }
3678 3703
3679 case OP_LET2: /* let */ 3704 case OP_LET2: /* let */
3680 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 3705 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3681 3706
3682 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; 3707 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)) 3708 y != NIL; x = cdr (x), y = cdr (y))
3684 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3709 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3685 3710
3686 if (is_symbol (car (SCHEME_V->code))) /* named let */ 3711 if (is_symbol (car (SCHEME_V->code))) /* named let */
3687 { 3712 {
3688 for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) 3713 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x))
3689 { 3714 {
3690 if (!is_pair (x)) 3715 if (!is_pair (x))
3691 Error_1 ("Bad syntax of binding in let :", x); 3716 Error_1 ("Bad syntax of binding in let :", x);
3692 3717
3693 if (!is_list (SCHEME_A_ car (x))) 3718 if (!is_list (SCHEME_A_ car (x)))
3694 Error_1 ("Bad syntax of binding in let :", car (x)); 3719 Error_1 ("Bad syntax of binding in let :", car (x));
3695 3720
3696 SCHEME_V->args = cons (caar (x), SCHEME_V->args); 3721 args = cons (caar (x), args);
3697 } 3722 }
3698 3723
3699 x =
3700 mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args), cddr (SCHEME_V->code)), 3724 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)),
3701 SCHEME_V->envir); 3725 SCHEME_V->envir);
3702 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); 3726 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x);
3703 SCHEME_V->code = cddr (SCHEME_V->code); 3727 SCHEME_V->code = cddr (SCHEME_V->code);
3704 SCHEME_V->args = NIL;
3705 } 3728 }
3706 else 3729 else
3707 { 3730 {
3708 SCHEME_V->code = cdr (SCHEME_V->code); 3731 SCHEME_V->code = cdr (SCHEME_V->code);
3732 }
3733
3709 SCHEME_V->args = NIL; 3734 SCHEME_V->args = NIL;
3710 }
3711
3712 s_goto (OP_BEGIN); 3735 s_goto (OP_BEGIN);
3713 3736
3714 case OP_LET0AST: /* let* */ 3737 case OP_LET0AST: /* let* */
3715 if (car (SCHEME_V->code) == NIL) 3738 if (car (SCHEME_V->code) == NIL)
3716 { 3739 {
3734 new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value); 3757 new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value);
3735 SCHEME_V->code = cdr (SCHEME_V->code); 3758 SCHEME_V->code = cdr (SCHEME_V->code);
3736 3759
3737 if (is_pair (SCHEME_V->code)) /* continue */ 3760 if (is_pair (SCHEME_V->code)) /* continue */
3738 { 3761 {
3739 s_save (SCHEME_A_ OP_LET2AST, SCHEME_V->args, SCHEME_V->code); 3762 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code);
3740 SCHEME_V->code = cadar (SCHEME_V->code); 3763 SCHEME_V->code = cadar (SCHEME_V->code);
3741 SCHEME_V->args = NIL; 3764 SCHEME_V->args = NIL;
3742 s_goto (OP_EVAL); 3765 s_goto (OP_EVAL);
3743 } 3766 }
3744 else /* end */ 3767 else /* end */
3745 { 3768 {
3746 SCHEME_V->code = SCHEME_V->args; 3769 SCHEME_V->code = args;
3747 SCHEME_V->args = NIL; 3770 SCHEME_V->args = NIL;
3748 s_goto (OP_BEGIN); 3771 s_goto (OP_BEGIN);
3749 } 3772 }
3750 3773
3751 case OP_LET0REC: /* letrec */ 3774 case OP_LET0REC: /* letrec */
3754 SCHEME_V->value = SCHEME_V->code; 3777 SCHEME_V->value = SCHEME_V->code;
3755 SCHEME_V->code = car (SCHEME_V->code); 3778 SCHEME_V->code = car (SCHEME_V->code);
3756 s_goto (OP_LET1REC); 3779 s_goto (OP_LET1REC);
3757 3780
3758 case OP_LET1REC: /* letrec (calculate parameters) */ 3781 case OP_LET1REC: /* letrec (calculate parameters) */
3759 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3782 args = cons (SCHEME_V->value, args);
3760 3783
3761 if (is_pair (SCHEME_V->code)) /* continue */ 3784 if (is_pair (SCHEME_V->code)) /* continue */
3762 { 3785 {
3763 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3786 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)); 3787 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3765 3788
3766 s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code)); 3789 s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code));
3767 SCHEME_V->code = cadar (SCHEME_V->code); 3790 SCHEME_V->code = cadar (SCHEME_V->code);
3768 SCHEME_V->args = NIL; 3791 SCHEME_V->args = NIL;
3769 s_goto (OP_EVAL); 3792 s_goto (OP_EVAL);
3770 } 3793 }
3771 else /* end */ 3794 else /* end */
3772 { 3795 {
3773 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3796 args = reverse_in_place (SCHEME_A_ NIL, args);
3774 SCHEME_V->code = car (SCHEME_V->args); 3797 SCHEME_V->code = car (args);
3775 SCHEME_V->args = cdr (SCHEME_V->args); 3798 SCHEME_V->args = cdr (args);
3776 s_goto (OP_LET2REC); 3799 s_goto (OP_LET2REC);
3777 } 3800 }
3778 3801
3779 case OP_LET2REC: /* letrec */ 3802 case OP_LET2REC: /* letrec */
3780 for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y)) 3803 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)); 3804 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3782 3805
3783 SCHEME_V->code = cdr (SCHEME_V->code); 3806 SCHEME_V->code = cdr (SCHEME_V->code);
3784 SCHEME_V->args = NIL; 3807 SCHEME_V->args = NIL;
3785 s_goto (OP_BEGIN); 3808 s_goto (OP_BEGIN);
3871 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 3894 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3872 SCHEME_V->code = car (SCHEME_V->code); 3895 SCHEME_V->code = car (SCHEME_V->code);
3873 s_goto (OP_EVAL); 3896 s_goto (OP_EVAL);
3874 3897
3875 case OP_C1STREAM: /* cons-stream */ 3898 case OP_C1STREAM: /* cons-stream */
3876 SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register SCHEME_V->args for gc */ 3899 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); 3900 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir);
3878 set_typeflag (x, T_PROMISE); 3901 set_typeflag (x, T_PROMISE);
3879 s_return (cons (SCHEME_V->args, x)); 3902 s_return (cons (args, x));
3880 3903
3881 case OP_MACRO0: /* macro */ 3904 case OP_MACRO0: /* macro */
3882 if (is_pair (car (SCHEME_V->code))) 3905 if (is_pair (car (SCHEME_V->code)))
3883 { 3906 {
3884 x = caar (SCHEME_V->code); 3907 x = caar (SCHEME_V->code);
3917 { 3940 {
3918 if (!is_pair (y = caar (x))) 3941 if (!is_pair (y = caar (x)))
3919 break; 3942 break;
3920 3943
3921 for (; y != NIL; y = cdr (y)) 3944 for (; y != NIL; y = cdr (y))
3922 {
3923 if (eqv (car (y), SCHEME_V->value)) 3945 if (eqv (car (y), SCHEME_V->value))
3924 break; 3946 break;
3925 }
3926 3947
3927 if (y != NIL) 3948 if (y != NIL)
3928 break; 3949 break;
3929 } 3950 }
3930 3951
3950 s_goto (OP_BEGIN); 3971 s_goto (OP_BEGIN);
3951 else 3972 else
3952 s_return (NIL); 3973 s_return (NIL);
3953 3974
3954 case OP_PAPPLY: /* apply */ 3975 case OP_PAPPLY: /* apply */
3955 SCHEME_V->code = car (SCHEME_V->args); 3976 SCHEME_V->code = car (args);
3956 SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); 3977 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
3957 /*SCHEME_V->args = cadr(SCHEME_V->args); */ 3978 /*SCHEME_V->args = cadr(args); */
3958 s_goto (OP_APPLY); 3979 s_goto (OP_APPLY);
3959 3980
3960 case OP_PEVAL: /* eval */ 3981 case OP_PEVAL: /* eval */
3961 if (cdr (SCHEME_V->args) != NIL) 3982 if (cdr (args) != NIL)
3962 SCHEME_V->envir = cadr (SCHEME_V->args); 3983 SCHEME_V->envir = cadr (args);
3963 3984
3964 SCHEME_V->code = car (SCHEME_V->args); 3985 SCHEME_V->code = car (args);
3965 s_goto (OP_EVAL); 3986 s_goto (OP_EVAL);
3966 3987
3967 case OP_CONTINUATION: /* call-with-current-continuation */ 3988 case OP_CONTINUATION: /* call-with-current-continuation */
3968 SCHEME_V->code = car (SCHEME_V->args); 3989 SCHEME_V->code = car (args);
3969 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); 3990 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
3970 s_goto (OP_APPLY); 3991 s_goto (OP_APPLY);
3971 } 3992 }
3972 3993
3973 return S_T; 3994 if (USE_ERROR_CHECKING) abort ();
3974} 3995}
3975 3996
3976static pointer 3997static int
3977opexe_2 (SCHEME_P_ enum scheme_opcodes op) 3998opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3978{ 3999{
3979 pointer x; 4000 pointer args = SCHEME_V->args;
4001 pointer x = car (args);
3980 num v; 4002 num v;
3981 4003
4004 switch (op)
4005 {
3982#if USE_MATH 4006#if USE_MATH
3983 RVALUE dd;
3984#endif
3985
3986 switch (op)
3987 {
3988#if USE_MATH
3989
3990 case OP_INEX2EX: /* inexact->exact */ 4007 case OP_INEX2EX: /* inexact->exact */
3991 x = car (SCHEME_V->args);
3992
3993 if (num_is_integer (x)) 4008 if (!is_integer (x))
3994 s_return (x); 4009 {
3995 else if (modf (rvalue_unchecked (x), &dd) == 0.0) 4010 RVALUE r = rvalue_unchecked (x);
3996 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4011
4012 if (r == (RVALUE)(IVALUE)r)
4013 x = mk_integer (SCHEME_A_ rvalue_unchecked (x));
3997 else 4014 else
3998 Error_1 ("inexact->exact: not integral:", x); 4015 Error_1 ("inexact->exact: not integral:", x);
4016 }
3999 4017
4000 case OP_EXP: 4018 s_return (x);
4001 x = car (SCHEME_V->args); 4019
4020 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4021 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4022 case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x))));
4023 case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x))));
4024
4025 case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4002 s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4026 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)))); 4027 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))
4007 4028 / (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)))); 4029 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)))); 4030 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)))); 4031 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)))); 4032 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)))); 4033 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x))));
4027 4034
4028 case OP_ATAN: 4035 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)))); 4036 s_return (mk_real (SCHEME_A_
4037 cdr (args) == NIL
4038 ? atan (rvalue (x))
4039 : atan2 (rvalue (x), rvalue (cadr (args)))));
4043 4040
4044 case OP_EXPT: 4041 case OP_EXPT:
4045 { 4042 {
4046 RVALUE result; 4043 RVALUE result;
4047 int real_result = 1; 4044 int real_result = 1;
4048 pointer y = cadr (SCHEME_V->args); 4045 pointer y = cadr (args);
4049 4046
4050 x = car (SCHEME_V->args);
4051
4052 if (num_is_integer (x) && num_is_integer (y)) 4047 if (is_integer (x) && is_integer (y))
4053 real_result = 0; 4048 real_result = 0;
4054 4049
4055 /* This 'if' is an R5RS compatibility fix. */ 4050 /* This 'if' is an R5RS compatibility fix. */
4056 /* NOTE: Remove this 'if' fix for R6RS. */ 4051 /* NOTE: Remove this 'if' fix for R6RS. */
4057 if (rvalue (x) == 0 && rvalue (y) < 0) 4052 if (rvalue (x) == 0 && rvalue (y) < 0)
4058 result = 0.0; 4053 result = 0;
4059 else 4054 else
4060 result = pow (rvalue (x), rvalue (y)); 4055 result = pow (rvalue (x), rvalue (y));
4061 4056
4062 /* Before returning integer result make sure we can. */ 4057 /* Before returning integer result make sure we can. */
4063 /* If the test fails, result is too big for integer. */ 4058 /* If the test fails, result is too big for integer. */
4064 if (!real_result) 4059 if (!real_result)
4065 { 4060 {
4066 long result_as_long = (long) result; 4061 long result_as_long = result;
4067 4062
4068 if (result != (RVALUE) result_as_long) 4063 if (result != result_as_long)
4069 real_result = 1; 4064 real_result = 1;
4070 } 4065 }
4071 4066
4072 if (real_result) 4067 if (real_result)
4073 s_return (mk_real (SCHEME_A_ result)); 4068 s_return (mk_real (SCHEME_A_ result));
4074 else 4069 else
4075 s_return (mk_integer (SCHEME_A_ result)); 4070 s_return (mk_integer (SCHEME_A_ result));
4076 } 4071 }
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 4072#endif
4107 4073
4108 case OP_ADD: /* + */ 4074 case OP_ADD: /* + */
4109 v = num_zero; 4075 v = num_zero;
4110 4076
4111 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4077 for (x = args; x != NIL; x = cdr (x))
4112 v = num_add (v, nvalue (car (x))); 4078 v = num_op (NUM_ADD, v, nvalue (car (x)));
4113 4079
4114 s_return (mk_number (SCHEME_A_ v)); 4080 s_return (mk_number (SCHEME_A_ v));
4115 4081
4116 case OP_MUL: /* * */ 4082 case OP_MUL: /* * */
4117 v = num_one; 4083 v = num_one;
4118 4084
4119 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4085 for (x = args; x != NIL; x = cdr (x))
4120 v = num_mul (v, nvalue (car (x))); 4086 v = num_op (NUM_MUL, v, nvalue (car (x)));
4121 4087
4122 s_return (mk_number (SCHEME_A_ v)); 4088 s_return (mk_number (SCHEME_A_ v));
4123 4089
4124 case OP_SUB: /* - */ 4090 case OP_SUB: /* - */
4125 if (cdr (SCHEME_V->args) == NIL) 4091 if (cdr (args) == NIL)
4126 { 4092 {
4127 x = SCHEME_V->args; 4093 x = args;
4128 v = num_zero; 4094 v = num_zero;
4129 } 4095 }
4130 else 4096 else
4131 { 4097 {
4132 x = cdr (SCHEME_V->args); 4098 x = cdr (args);
4133 v = nvalue (car (SCHEME_V->args)); 4099 v = nvalue (car (args));
4134 } 4100 }
4135 4101
4136 for (; x != NIL; x = cdr (x)) 4102 for (; x != NIL; x = cdr (x))
4137 v = num_sub (v, nvalue (car (x))); 4103 v = num_op (NUM_SUB, v, nvalue (car (x)));
4138 4104
4139 s_return (mk_number (SCHEME_A_ v)); 4105 s_return (mk_number (SCHEME_A_ v));
4140 4106
4141 case OP_DIV: /* / */ 4107 case OP_DIV: /* / */
4142 if (cdr (SCHEME_V->args) == NIL) 4108 if (cdr (args) == NIL)
4143 { 4109 {
4144 x = SCHEME_V->args; 4110 x = args;
4145 v = num_one; 4111 v = num_one;
4146 } 4112 }
4147 else 4113 else
4148 { 4114 {
4149 x = cdr (SCHEME_V->args); 4115 x = cdr (args);
4150 v = nvalue (car (SCHEME_V->args)); 4116 v = nvalue (car (args));
4151 } 4117 }
4152 4118
4153 for (; x != NIL; x = cdr (x)) 4119 for (; x != NIL; x = cdr (x))
4154 {
4155 if (!is_zero_rvalue (rvalue (car (x)))) 4120 if (!is_zero_rvalue (rvalue (car (x))))
4156 v = num_div (v, nvalue (car (x))); 4121 v = num_div (v, nvalue (car (x)));
4157 else 4122 else
4158 Error_0 ("/: division by zero"); 4123 Error_0 ("/: division by zero");
4159 }
4160 4124
4161 s_return (mk_number (SCHEME_A_ v)); 4125 s_return (mk_number (SCHEME_A_ v));
4162 4126
4163 case OP_INTDIV: /* quotient */ 4127 case OP_INTDIV: /* quotient */
4164 if (cdr (SCHEME_V->args) == NIL) 4128 if (cdr (args) == NIL)
4165 { 4129 {
4166 x = SCHEME_V->args; 4130 x = args;
4167 v = num_one; 4131 v = num_one;
4168 } 4132 }
4169 else 4133 else
4170 { 4134 {
4171 x = cdr (SCHEME_V->args); 4135 x = cdr (args);
4172 v = nvalue (car (SCHEME_V->args)); 4136 v = nvalue (car (args));
4173 } 4137 }
4174 4138
4175 for (; x != NIL; x = cdr (x)) 4139 for (; x != NIL; x = cdr (x))
4176 { 4140 {
4177 if (ivalue (car (x)) != 0) 4141 if (ivalue (car (x)) != 0)
4178 v = num_intdiv (v, nvalue (car (x))); 4142 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4179 else 4143 else
4180 Error_0 ("quotient: division by zero"); 4144 Error_0 ("quotient: division by zero");
4181 } 4145 }
4182 4146
4183 s_return (mk_number (SCHEME_A_ v)); 4147 s_return (mk_number (SCHEME_A_ v));
4184 4148
4185 case OP_REM: /* remainder */ 4149 case OP_REM: /* remainder */
4186 v = nvalue (car (SCHEME_V->args)); 4150 v = nvalue (x);
4187 4151
4188 if (ivalue (cadr (SCHEME_V->args)) != 0) 4152 if (ivalue (cadr (args)) != 0)
4189 v = num_rem (v, nvalue (cadr (SCHEME_V->args))); 4153 v = num_rem (v, nvalue (cadr (args)));
4190 else 4154 else
4191 Error_0 ("remainder: division by zero"); 4155 Error_0 ("remainder: division by zero");
4192 4156
4193 s_return (mk_number (SCHEME_A_ v)); 4157 s_return (mk_number (SCHEME_A_ v));
4194 4158
4195 case OP_MOD: /* modulo */ 4159 case OP_MOD: /* modulo */
4196 v = nvalue (car (SCHEME_V->args)); 4160 v = nvalue (x);
4197 4161
4198 if (ivalue (cadr (SCHEME_V->args)) != 0) 4162 if (ivalue (cadr (args)) != 0)
4199 v = num_mod (v, nvalue (cadr (SCHEME_V->args))); 4163 v = num_mod (v, nvalue (cadr (args)));
4200 else 4164 else
4201 Error_0 ("modulo: division by zero"); 4165 Error_0 ("modulo: division by zero");
4202 4166
4203 s_return (mk_number (SCHEME_A_ v)); 4167 s_return (mk_number (SCHEME_A_ v));
4204 4168
4205 case OP_CAR: /* car */ 4169 /* the compiler will optimize this mess... */
4206 s_return (caar (SCHEME_V->args)); 4170 case OP_CAR: op_car: s_return (car (x));
4207 4171 case OP_CDR: op_cdr: s_return (cdr (x));
4208 case OP_CDR: /* cdr */ 4172 case OP_CAAR: op_caar: x = car (x); goto op_car;
4209 s_return (cdar (SCHEME_V->args)); 4173 case OP_CADR: op_cadr: x = cdr (x); goto op_car;
4174 case OP_CDAR: op_cdar: x = car (x); goto op_cdr;
4175 case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr;
4176 case OP_CAAAR: op_caaar: x = car (x); goto op_caar;
4177 case OP_CAADR: op_caadr: x = cdr (x); goto op_caar;
4178 case OP_CADAR: op_cadar: x = car (x); goto op_cadr;
4179 case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr;
4180 case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar;
4181 case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar;
4182 case OP_CDDAR: op_cddar: x = car (x); goto op_cddr;
4183 case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr;
4184 case OP_CAAAAR: x = car (x); goto op_caaar;
4185 case OP_CAAADR: x = cdr (x); goto op_caaar;
4186 case OP_CAADAR: x = car (x); goto op_caadr;
4187 case OP_CAADDR: x = cdr (x); goto op_caadr;
4188 case OP_CADAAR: x = car (x); goto op_cadar;
4189 case OP_CADADR: x = cdr (x); goto op_cadar;
4190 case OP_CADDAR: x = car (x); goto op_caddr;
4191 case OP_CADDDR: x = cdr (x); goto op_caddr;
4192 case OP_CDAAAR: x = car (x); goto op_cdaar;
4193 case OP_CDAADR: x = cdr (x); goto op_cdaar;
4194 case OP_CDADAR: x = car (x); goto op_cdadr;
4195 case OP_CDADDR: x = cdr (x); goto op_cdadr;
4196 case OP_CDDAAR: x = car (x); goto op_cddar;
4197 case OP_CDDADR: x = cdr (x); goto op_cddar;
4198 case OP_CDDDAR: x = car (x); goto op_cdddr;
4199 case OP_CDDDDR: x = cdr (x); goto op_cdddr;
4210 4200
4211 case OP_CONS: /* cons */ 4201 case OP_CONS: /* cons */
4212 set_cdr (SCHEME_V->args, cadr (SCHEME_V->args)); 4202 set_cdr (args, cadr (args));
4213 s_return (SCHEME_V->args); 4203 s_return (args);
4214 4204
4215 case OP_SETCAR: /* set-car! */ 4205 case OP_SETCAR: /* set-car! */
4216 if (!is_immutable (car (SCHEME_V->args))) 4206 if (!is_immutable (x))
4217 { 4207 {
4218 set_car (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4208 set_car (x, cadr (args));
4219 s_return (car (SCHEME_V->args)); 4209 s_return (car (args));
4220 } 4210 }
4221 else 4211 else
4222 Error_0 ("set-car!: unable to alter immutable pair"); 4212 Error_0 ("set-car!: unable to alter immutable pair");
4223 4213
4224 case OP_SETCDR: /* set-cdr! */ 4214 case OP_SETCDR: /* set-cdr! */
4225 if (!is_immutable (car (SCHEME_V->args))) 4215 if (!is_immutable (x))
4226 { 4216 {
4227 set_cdr (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4217 set_cdr (x, cadr (args));
4228 s_return (car (SCHEME_V->args)); 4218 s_return (car (args));
4229 } 4219 }
4230 else 4220 else
4231 Error_0 ("set-cdr!: unable to alter immutable pair"); 4221 Error_0 ("set-cdr!: unable to alter immutable pair");
4232 4222
4233 case OP_CHAR2INT: /* char->integer */ 4223 case OP_CHAR2INT: /* char->integer */
4234 s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4224 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4235 4225
4236 case OP_INT2CHAR: /* integer->char */ 4226 case OP_INT2CHAR: /* integer->char */
4237 s_return (mk_character (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4227 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4238 4228
4239 case OP_CHARUPCASE: 4229 case OP_CHARUPCASE:
4240 { 4230 {
4241 unsigned char c = ivalue (car (SCHEME_V->args)); 4231 unsigned char c = ivalue_unchecked (x);
4242 c = toupper (c); 4232 c = toupper (c);
4243 s_return (mk_character (SCHEME_A_ c)); 4233 s_return (mk_character (SCHEME_A_ c));
4244 } 4234 }
4245 4235
4246 case OP_CHARDNCASE: 4236 case OP_CHARDNCASE:
4247 { 4237 {
4248 unsigned char c = ivalue (car (SCHEME_V->args)); 4238 unsigned char c = ivalue_unchecked (x);
4249 c = tolower (c); 4239 c = tolower (c);
4250 s_return (mk_character (SCHEME_A_ c)); 4240 s_return (mk_character (SCHEME_A_ c));
4251 } 4241 }
4252 4242
4253 case OP_STR2SYM: /* string->symbol */ 4243 case OP_STR2SYM: /* string->symbol */
4254 s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); 4244 s_return (mk_symbol (SCHEME_A_ strvalue (x)));
4255 4245
4256 case OP_STR2ATOM: /* string->atom */ 4246 case OP_STR2ATOM: /* string->atom */
4257 { 4247 {
4258 char *s = strvalue (car (SCHEME_V->args)); 4248 char *s = strvalue (x);
4259 long pf = 0; 4249 long pf = 0;
4260 4250
4261 if (cdr (SCHEME_V->args) != NIL) 4251 if (cdr (args) != NIL)
4262 { 4252 {
4263 /* we know cadr(SCHEME_V->args) is a natural number */ 4253 /* we know cadr(args) is a natural number */
4264 /* see if it is 2, 8, 10, or 16, or error */ 4254 /* see if it is 2, 8, 10, or 16, or error */
4265 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4255 pf = ivalue_unchecked (cadr (args));
4266 4256
4267 if (pf == 16 || pf == 10 || pf == 8 || pf == 2) 4257 if (pf == 16 || pf == 10 || pf == 8 || pf == 2)
4268 { 4258 {
4269 /* base is OK */ 4259 /* base is OK */
4270 } 4260 }
4271 else 4261 else
4272 pf = -1; 4262 pf = -1;
4273 } 4263 }
4274 4264
4275 if (pf < 0) 4265 if (pf < 0)
4276 Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); 4266 Error_1 ("string->atom: bad base:", cadr (args));
4277 else if (*s == '#') /* no use of base! */ 4267 else if (*s == '#') /* no use of base! */
4278 s_return (mk_sharp_const (SCHEME_A_ s + 1)); 4268 s_return (mk_sharp_const (SCHEME_A_ s + 1));
4279 else 4269 else
4280 { 4270 {
4281 if (pf == 0 || pf == 10) 4271 if (pf == 0 || pf == 10)
4292 } 4282 }
4293 } 4283 }
4294 } 4284 }
4295 4285
4296 case OP_SYM2STR: /* symbol->string */ 4286 case OP_SYM2STR: /* symbol->string */
4297 x = mk_string (SCHEME_A_ symname (car (SCHEME_V->args))); 4287 x = mk_string (SCHEME_A_ symname (x));
4298 setimmutable (x); 4288 setimmutable (x);
4299 s_return (x); 4289 s_return (x);
4300 4290
4301 case OP_ATOM2STR: /* atom->string */ 4291 case OP_ATOM2STR: /* atom->string */
4302 { 4292 {
4303 long pf = 0; 4293 long pf = 0;
4304 4294
4305 x = car (SCHEME_V->args);
4306
4307 if (cdr (SCHEME_V->args) != NIL) 4295 if (cdr (args) != NIL)
4308 { 4296 {
4309 /* we know cadr(SCHEME_V->args) is a natural number */ 4297 /* we know cadr(args) is a natural number */
4310 /* see if it is 2, 8, 10, or 16, or error */ 4298 /* see if it is 2, 8, 10, or 16, or error */
4311 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4299 pf = ivalue_unchecked (cadr (args));
4312 4300
4313 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) 4301 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2))
4314 { 4302 {
4315 /* base is OK */ 4303 /* base is OK */
4316 } 4304 }
4317 else 4305 else
4318 pf = -1; 4306 pf = -1;
4319 } 4307 }
4320 4308
4321 if (pf < 0) 4309 if (pf < 0)
4322 Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); 4310 Error_1 ("atom->string: bad base:", cadr (args));
4323 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) 4311 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x))
4324 { 4312 {
4325 char *p; 4313 char *p;
4326 int len; 4314 int len;
4327 4315
4332 Error_1 ("atom->string: not an atom:", x); 4320 Error_1 ("atom->string: not an atom:", x);
4333 } 4321 }
4334 4322
4335 case OP_MKSTRING: /* make-string */ 4323 case OP_MKSTRING: /* make-string */
4336 { 4324 {
4337 int fill = ' '; 4325 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4338 int len; 4326 int len = ivalue_unchecked (x);
4339 4327
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)); 4328 s_return (mk_empty_string (SCHEME_A_ len, fill));
4346 } 4329 }
4347 4330
4348 case OP_STRLEN: /* string-length */ 4331 case OP_STRLEN: /* string-length */
4349 s_return (mk_integer (SCHEME_A_ strlength (car (SCHEME_V->args)))); 4332 s_return (mk_integer (SCHEME_A_ strlength (x)));
4350 4333
4351 case OP_STRREF: /* string-ref */ 4334 case OP_STRREF: /* string-ref */
4352 { 4335 {
4353 char *str; 4336 char *str = strvalue (x);
4354 int index;
4355
4356 str = strvalue (car (SCHEME_V->args));
4357
4358 index = ivalue (cadr (SCHEME_V->args)); 4337 int index = ivalue_unchecked (cadr (args));
4359 4338
4360 if (index >= strlength (car (SCHEME_V->args))) 4339 if (index >= strlength (x))
4361 Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); 4340 Error_1 ("string-ref: out of bounds:", cadr (args));
4362 4341
4363 s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); 4342 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4364 } 4343 }
4365 4344
4366 case OP_STRSET: /* string-set! */ 4345 case OP_STRSET: /* string-set! */
4367 { 4346 {
4368 char *str; 4347 char *str = strvalue (x);
4369 int index; 4348 int index = ivalue_unchecked (cadr (args));
4370 int c; 4349 int c;
4371 4350
4372 if (is_immutable (car (SCHEME_V->args))) 4351 if (is_immutable (x))
4373 Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); 4352 Error_1 ("string-set!: unable to alter immutable string:", x);
4374 4353
4375 str = strvalue (car (SCHEME_V->args));
4376
4377 index = ivalue (cadr (SCHEME_V->args));
4378
4379 if (index >= strlength (car (SCHEME_V->args))) 4354 if (index >= strlength (x))
4380 Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); 4355 Error_1 ("string-set!: out of bounds:", cadr (args));
4381 4356
4382 c = charvalue (caddr (SCHEME_V->args)); 4357 c = charvalue (caddr (args));
4383 4358
4384 str[index] = (char) c; 4359 str[index] = c;
4385 s_return (car (SCHEME_V->args)); 4360 s_return (car (args));
4386 } 4361 }
4387 4362
4388 case OP_STRAPPEND: /* string-append */ 4363 case OP_STRAPPEND: /* string-append */
4389 { 4364 {
4390 /* in 1.29 string-append was in Scheme in init.scm but was too slow */ 4365 /* in 1.29 string-append was in Scheme in init.scm but was too slow */
4391 int len = 0; 4366 int len = 0;
4392 pointer newstr; 4367 pointer newstr;
4393 char *pos; 4368 char *pos;
4394 4369
4395 /* compute needed length for new string */ 4370 /* compute needed length for new string */
4396 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4371 for (x = args; x != NIL; x = cdr (x))
4397 len += strlength (car (x)); 4372 len += strlength (car (x));
4398 4373
4399 newstr = mk_empty_string (SCHEME_A_ len, ' '); 4374 newstr = mk_empty_string (SCHEME_A_ len, ' ');
4400 4375
4401 /* store the contents of the argument strings into the new string */ 4376 /* 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)) 4377 for (pos = strvalue (newstr), x = args; x != NIL; pos += strlength (car (x)), x = cdr (x))
4403 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4378 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4404 4379
4405 s_return (newstr); 4380 s_return (newstr);
4406 } 4381 }
4407 4382
4408 case OP_SUBSTR: /* substring */ 4383 case OP_STRING_COPY: /* substring/string-copy */
4409 { 4384 {
4410 char *str; 4385 char *str = strvalue (x);
4411 int index0; 4386 int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args));
4412 int index1; 4387 int index1;
4413 int len; 4388 int len;
4414 4389
4415 str = strvalue (car (SCHEME_V->args));
4416
4417 index0 = ivalue (cadr (SCHEME_V->args));
4418
4419 if (index0 > strlength (car (SCHEME_V->args))) 4390 if (index0 > strlength (x))
4420 Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); 4391 Error_1 ("string->copy: start out of bounds:", cadr (args));
4421 4392
4422 if (cddr (SCHEME_V->args) != NIL) 4393 if (cddr (args) != NIL)
4423 { 4394 {
4424 index1 = ivalue (caddr (SCHEME_V->args)); 4395 index1 = ivalue_unchecked (caddr (args));
4425 4396
4426 if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) 4397 if (index1 > strlength (x) || index1 < index0)
4427 Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); 4398 Error_1 ("string->copy: end out of bounds:", caddr (args));
4428 } 4399 }
4429 else 4400 else
4430 index1 = strlength (car (SCHEME_V->args)); 4401 index1 = strlength (x);
4431 4402
4432 len = index1 - index0; 4403 len = index1 - index0;
4433 x = mk_empty_string (SCHEME_A_ len, ' '); 4404 x = mk_counted_string (SCHEME_A_ str + index0, len);
4434 memcpy (strvalue (x), str + index0, len);
4435 strvalue (x)[len] = 0;
4436 4405
4437 s_return (x); 4406 s_return (x);
4438 } 4407 }
4439 4408
4440 case OP_VECTOR: /* vector */ 4409 case OP_VECTOR: /* vector */
4441 { 4410 {
4442 int i; 4411 int i;
4443 pointer vec; 4412 pointer vec;
4444 int len = list_length (SCHEME_A_ SCHEME_V->args); 4413 int len = list_length (SCHEME_A_ args);
4445 4414
4446 if (len < 0) 4415 if (len < 0)
4447 Error_1 ("vector: not a proper list:", SCHEME_V->args); 4416 Error_1 ("vector: not a proper list:", args);
4448 4417
4449 vec = mk_vector (SCHEME_A_ len); 4418 vec = mk_vector (SCHEME_A_ len);
4450 4419
4451#if USE_ERROR_CHECKING 4420#if USE_ERROR_CHECKING
4452 if (SCHEME_V->no_memory) 4421 if (SCHEME_V->no_memory)
4453 s_return (S_SINK); 4422 s_return (S_SINK);
4454#endif 4423#endif
4455 4424
4456 for (x = SCHEME_V->args, i = 0; is_pair (x); x = cdr (x), i++) 4425 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4457 set_vector_elem (vec, i, car (x)); 4426 vector_set (vec, i, car (x));
4458 4427
4459 s_return (vec); 4428 s_return (vec);
4460 } 4429 }
4461 4430
4462 case OP_MKVECTOR: /* make-vector */ 4431 case OP_MKVECTOR: /* make-vector */
4463 { 4432 {
4464 pointer fill = NIL; 4433 pointer fill = NIL;
4465 int len;
4466 pointer vec; 4434 pointer vec;
4435 int len = ivalue_unchecked (x);
4467 4436
4468 len = ivalue (car (SCHEME_V->args));
4469
4470 if (cdr (SCHEME_V->args) != NIL) 4437 if (cdr (args) != NIL)
4471 fill = cadr (SCHEME_V->args); 4438 fill = cadr (args);
4472 4439
4473 vec = mk_vector (SCHEME_A_ len); 4440 vec = mk_vector (SCHEME_A_ len);
4474 4441
4475#if USE_ERROR_CHECKING 4442#if USE_ERROR_CHECKING
4476 if (SCHEME_V->no_memory) 4443 if (SCHEME_V->no_memory)
4477 s_return (S_SINK); 4444 s_return (S_SINK);
4478#endif 4445#endif
4479 4446
4480 if (fill != NIL) 4447 if (fill != NIL)
4481 fill_vector (vec, fill); 4448 fill_vector (vec, 0, fill);
4482 4449
4483 s_return (vec); 4450 s_return (vec);
4484 } 4451 }
4485 4452
4486 case OP_VECLEN: /* vector-length */ 4453 case OP_VECLEN: /* vector-length */
4487 s_return (mk_integer (SCHEME_A_ veclength (car (SCHEME_V->args)))); 4454 s_return (mk_integer (SCHEME_A_ veclength (x)));
4455
4456 case OP_VECRESIZE:
4457 vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args));
4458 s_return (x);
4488 4459
4489 case OP_VECREF: /* vector-ref */ 4460 case OP_VECREF: /* vector-ref */
4490 { 4461 {
4491 int index;
4492
4493 index = ivalue (cadr (SCHEME_V->args)); 4462 int index = ivalue_unchecked (cadr (args));
4494 4463
4495 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) 4464 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4496 Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); 4465 Error_1 ("vector-ref: out of bounds:", cadr (args));
4497 4466
4498 s_return (vector_elem (car (SCHEME_V->args), index)); 4467 s_return (vector_get (x, index));
4499 } 4468 }
4500 4469
4501 case OP_VECSET: /* vector-set! */ 4470 case OP_VECSET: /* vector-set! */
4502 { 4471 {
4503 int index; 4472 int index = ivalue_unchecked (cadr (args));
4504 4473
4505 if (is_immutable (car (SCHEME_V->args))) 4474 if (is_immutable (x))
4506 Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); 4475 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4507 4476
4508 index = ivalue (cadr (SCHEME_V->args));
4509
4510 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) 4477 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4511 Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); 4478 Error_1 ("vector-set!: out of bounds:", cadr (args));
4512 4479
4513 set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); 4480 vector_set (x, index, caddr (args));
4514 s_return (car (SCHEME_V->args)); 4481 s_return (x);
4515 } 4482 }
4516 } 4483 }
4517 4484
4518 return S_T; 4485 if (USE_ERROR_CHECKING) abort ();
4519} 4486}
4520 4487
4521INTERFACE int 4488static int
4522is_list (SCHEME_P_ pointer a) 4489opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4523{ 4490{
4524 return list_length (SCHEME_A_ a) >= 0; 4491 pointer x = SCHEME_V->args;
4525}
4526 4492
4527/* Result is: 4493 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 { 4494 {
4495 num v = nvalue (car (x));
4496 x = cdr (x);
4497
4543 if (fast == NIL) 4498 if (x == NIL)
4544 return i; 4499 break;
4545 4500
4546 if (!is_pair (fast)) 4501 int r = num_cmp (v, nvalue (car (x)));
4547 return -2 - i;
4548 4502
4549 fast = cdr (fast); 4503 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 { 4504 {
4567 /* the fast pointer has looped back around and caught up 4505 case OP_NUMEQ: r = r == 0; break;
4568 with the slow pointer, hence the structure is circular, 4506 case OP_LESS: r = r < 0; break;
4569 not of finite length, and therefore not a list */ 4507 case OP_GRE: r = r > 0; break;
4570 return -1; 4508 case OP_LEQ: r = r <= 0; break;
4509 case OP_GEQ: r = r >= 0; break;
4571 } 4510 }
4572 }
4573}
4574 4511
4512 if (!r)
4513 s_return (S_F);
4514 }
4515
4516 s_return (S_T);
4517}
4518
4575static pointer 4519static int
4576opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4520opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4577{ 4521{
4578 pointer x; 4522 pointer args = SCHEME_V->args;
4579 num v; 4523 pointer a = car (args);
4580 int (*comp_func) (num, num); 4524 pointer d = cdr (args);
4525 int r;
4581 4526
4582 switch (op) 4527 switch (op)
4583 { 4528 {
4584 case OP_NOT: /* not */ 4529 case OP_NOT: /* not */ r = is_false (a) ; break;
4585 s_retbool (is_false (car (SCHEME_V->args))); 4530 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break;
4531 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break;
4532 case OP_NULLP: /* null? */ r = a == NIL ; break;
4533 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break;
4534 case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break;
4535 case OP_NUMBERP: /* number? */ r = is_number (a) ; break;
4536 case OP_STRINGP: /* string? */ r = is_string (a) ; break;
4537 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4538 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4539 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4586 4540
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 4541#if USE_CHAR_CLASSIFIERS
4656 4542 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4657 case OP_CHARAP: /* char-alphabetic? */ 4543 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4658 s_retbool (Cisalpha (ivalue (car (SCHEME_V->args)))); 4544 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4659 4545 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4660 case OP_CHARNP: /* char-numeric? */ 4546 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 4547#endif
4548
4672#if USE_PORTS 4549#if USE_PORTS
4673 4550 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? */ 4551 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? */ 4552 case OP_OUTPORTP: /* output-port? */ r = is_outport (a); break;
4681 s_retbool (is_outport (car (SCHEME_V->args)));
4682#endif 4553#endif
4683 4554
4684 case OP_PROCP: /* procedure? */ 4555 case OP_PROCP: /* procedure? */
4685 4556
4686 /*-- 4557 /*--
4687 * continuation should be procedure by the example 4558 * continuation should be procedure by the example
4688 * (call-with-current-continuation procedure?) ==> #t 4559 * (call-with-current-continuation procedure?) ==> #t
4689 * in R^3 report sec. 6.9 4560 * in R^3 report sec. 6.9
4690 */ 4561 */
4691 s_retbool (is_proc (car (SCHEME_V->args)) || is_closure (car (SCHEME_V->args)) 4562 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))); 4563 break;
4693 4564
4694 case OP_PAIRP: /* pair? */ 4565 case OP_PAIRP: /* pair? */ r = is_pair (a) ; break;
4695 s_retbool (is_pair (car (SCHEME_V->args))); 4566 case OP_LISTP: /* list? */ r = list_length (SCHEME_A_ a) >= 0; break;
4696 4567 case OP_ENVP: /* environment? */ r = is_environment (a) ; break;
4697 case OP_LISTP: /* list? */ 4568 case OP_VECTORP: /* vector? */ r = is_vector (a) ; break;
4698 s_retbool (list_length (SCHEME_A_ car (SCHEME_V->args)) >= 0); 4569 case OP_EQ: /* eq? */ r = a == cadr (args) ; break;
4699 4570 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 } 4571 }
4712 4572
4713 return S_T; 4573 s_retbool (r);
4714} 4574}
4715 4575
4716static pointer 4576static int
4717opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4577opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4718{ 4578{
4579 pointer args = SCHEME_V->args;
4580 pointer a = car (args);
4719 pointer x, y; 4581 pointer x, y;
4720 4582
4721 switch (op) 4583 switch (op)
4722 { 4584 {
4723 case OP_FORCE: /* force */ 4585 case OP_FORCE: /* force */
4724 SCHEME_V->code = car (SCHEME_V->args); 4586 SCHEME_V->code = a;
4725 4587
4726 if (is_promise (SCHEME_V->code)) 4588 if (is_promise (SCHEME_V->code))
4727 { 4589 {
4728 /* Should change type to closure here */ 4590 /* Should change type to closure here */
4729 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); 4591 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code);
4732 } 4594 }
4733 else 4595 else
4734 s_return (SCHEME_V->code); 4596 s_return (SCHEME_V->code);
4735 4597
4736 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4598 case OP_SAVE_FORCED: /* Save forced value replacing promise */
4737 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); 4599 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
4738 s_return (SCHEME_V->value); 4600 s_return (SCHEME_V->value);
4739 4601
4740#if USE_PORTS 4602#if USE_PORTS
4603
4604 case OP_EOF_OBJECT: /* eof-object */
4605 s_return (S_EOF);
4741 4606
4742 case OP_WRITE: /* write */ 4607 case OP_WRITE: /* write */
4743 case OP_DISPLAY: /* display */ 4608 case OP_DISPLAY: /* display */
4744 case OP_WRITE_CHAR: /* write-char */ 4609 case OP_WRITE_CHAR: /* write-char */
4745 if (is_pair (cdr (SCHEME_V->args))) 4610 if (is_pair (cdr (SCHEME_V->args)))
4750 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4615 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4751 SCHEME_V->outport = cadr (SCHEME_V->args); 4616 SCHEME_V->outport = cadr (SCHEME_V->args);
4752 } 4617 }
4753 } 4618 }
4754 4619
4755 SCHEME_V->args = car (SCHEME_V->args); 4620 SCHEME_V->args = a;
4756 4621
4757 if (op == OP_WRITE) 4622 if (op == OP_WRITE)
4758 SCHEME_V->print_flag = 1; 4623 SCHEME_V->print_flag = 1;
4759 else 4624 else
4760 SCHEME_V->print_flag = 0; 4625 SCHEME_V->print_flag = 0;
4761 4626
4762 s_goto (OP_P0LIST); 4627 s_goto (OP_P0LIST);
4763 4628
4764 case OP_NEWLINE: /* newline */ 4629 case OP_NEWLINE: /* newline */
4765 if (is_pair (SCHEME_V->args)) 4630 if (is_pair (args))
4766 { 4631 {
4767 if (car (SCHEME_V->args) != SCHEME_V->outport) 4632 if (a != SCHEME_V->outport)
4768 { 4633 {
4769 x = cons (SCHEME_V->outport, NIL); 4634 x = cons (SCHEME_V->outport, NIL);
4770 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4635 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4771 SCHEME_V->outport = car (SCHEME_V->args); 4636 SCHEME_V->outport = a;
4772 } 4637 }
4773 } 4638 }
4774 4639
4775 putstr (SCHEME_A_ "\n"); 4640 putstr (SCHEME_A_ "\n");
4776 s_return (S_T); 4641 s_return (S_T);
4777#endif 4642#endif
4778 4643
4779 case OP_ERR0: /* error */ 4644 case OP_ERR0: /* error */
4780 SCHEME_V->retcode = -1; 4645 SCHEME_V->retcode = -1;
4781 4646
4782 if (!is_string (car (SCHEME_V->args))) 4647 if (!is_string (a))
4783 { 4648 {
4784 SCHEME_V->args = cons (mk_string (SCHEME_A_ " -- "), SCHEME_V->args); 4649 args = cons (mk_string (SCHEME_A_ " -- "), args);
4785 setimmutable (car (SCHEME_V->args)); 4650 setimmutable (car (args));
4786 } 4651 }
4787 4652
4788 putstr (SCHEME_A_ "Error: "); 4653 putstr (SCHEME_A_ "Error: ");
4789 putstr (SCHEME_A_ strvalue (car (SCHEME_V->args))); 4654 putstr (SCHEME_A_ strvalue (car (args)));
4790 SCHEME_V->args = cdr (SCHEME_V->args); 4655 SCHEME_V->args = cdr (args);
4791 s_goto (OP_ERR1); 4656 s_goto (OP_ERR1);
4792 4657
4793 case OP_ERR1: /* error */ 4658 case OP_ERR1: /* error */
4794 putstr (SCHEME_A_ " "); 4659 putstr (SCHEME_A_ " ");
4795 4660
4796 if (SCHEME_V->args != NIL) 4661 if (args != NIL)
4797 { 4662 {
4798 s_save (SCHEME_A_ OP_ERR1, cdr (SCHEME_V->args), NIL); 4663 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4799 SCHEME_V->args = car (SCHEME_V->args); 4664 SCHEME_V->args = a;
4800 SCHEME_V->print_flag = 1; 4665 SCHEME_V->print_flag = 1;
4801 s_goto (OP_P0LIST); 4666 s_goto (OP_P0LIST);
4802 } 4667 }
4803 else 4668 else
4804 { 4669 {
4805 putstr (SCHEME_A_ "\n"); 4670 putstr (SCHEME_A_ "\n");
4806 4671
4807 if (SCHEME_V->interactive_repl) 4672 if (SCHEME_V->interactive_repl)
4808 s_goto (OP_T0LVL); 4673 s_goto (OP_T0LVL);
4809 else 4674 else
4810 return NIL; 4675 return -1;
4811 } 4676 }
4812 4677
4813 case OP_REVERSE: /* reverse */ 4678 case OP_REVERSE: /* reverse */
4814 s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); 4679 s_return (reverse (SCHEME_A_ a));
4815 4680
4816 case OP_LIST_STAR: /* list* */ 4681 case OP_LIST_STAR: /* list* */
4817 s_return (list_star (SCHEME_A_ SCHEME_V->args)); 4682 s_return (list_star (SCHEME_A_ SCHEME_V->args));
4818 4683
4819 case OP_APPEND: /* append */ 4684 case OP_APPEND: /* append */
4820 x = NIL; 4685 x = NIL;
4821 y = SCHEME_V->args; 4686 y = args;
4822 4687
4823 if (y == x) 4688 if (y == x)
4824 s_return (x); 4689 s_return (x);
4825 4690
4826 /* cdr() in the while condition is not a typo. If car() */ 4691 /* cdr() in the while condition is not a typo. If car() */
4837 s_return (reverse_in_place (SCHEME_A_ car (y), x)); 4702 s_return (reverse_in_place (SCHEME_A_ car (y), x));
4838 4703
4839#if USE_PLIST 4704#if USE_PLIST
4840 4705
4841 case OP_PUT: /* put */ 4706 case OP_PUT: /* put */
4842 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 4707 if (!hasprop (a) || !hasprop (cadr (args)))
4843 Error_0 ("illegal use of put"); 4708 Error_0 ("illegal use of put");
4844 4709
4845 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 4710 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
4846 { 4711 {
4847 if (caar (x) == y) 4712 if (caar (x) == y)
4848 break; 4713 break;
4849 } 4714 }
4850 4715
4851 if (x != NIL) 4716 if (x != NIL)
4852 cdar (x) = caddr (SCHEME_V->args); 4717 cdar (x) = caddr (args);
4853 else 4718 else
4854 symprop (car (SCHEME_V->args)) = cons (cons (y, caddr (SCHEME_V->args)), symprop (car (SCHEME_V->args))); 4719 symprop (a) = cons (cons (y, caddr (args)), symprop (a));
4855 4720
4856 s_return (S_T); 4721 s_return (S_T);
4857 4722
4858 case OP_GET: /* get */ 4723 case OP_GET: /* get */
4859 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 4724 if (!hasprop (a) || !hasprop (cadr (args)))
4860 Error_0 ("illegal use of get"); 4725 Error_0 ("illegal use of get");
4861 4726
4862 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 4727 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
4863 if (caar (x) == y) 4728 if (caar (x) == y)
4864 break; 4729 break;
4865 4730
4866 if (x != NIL) 4731 if (x != NIL)
4867 s_return (cdar (x)); 4732 s_return (cdar (x));
4869 s_return (NIL); 4734 s_return (NIL);
4870 4735
4871#endif /* USE_PLIST */ 4736#endif /* USE_PLIST */
4872 4737
4873 case OP_QUIT: /* quit */ 4738 case OP_QUIT: /* quit */
4874 if (is_pair (SCHEME_V->args)) 4739 if (is_pair (args))
4875 SCHEME_V->retcode = ivalue (car (SCHEME_V->args)); 4740 SCHEME_V->retcode = ivalue (a);
4876 4741
4877 return NIL; 4742 return -1;
4878 4743
4879 case OP_GC: /* gc */ 4744 case OP_GC: /* gc */
4880 gc (SCHEME_A_ NIL, NIL); 4745 gc (SCHEME_A_ NIL, NIL);
4881 s_return (S_T); 4746 s_return (S_T);
4882 4747
4883 case OP_GCVERB: /* gc-verbose */ 4748 case OP_GCVERB: /* gc-verbose */
4884 { 4749 {
4885 int was = SCHEME_V->gc_verbose; 4750 int was = SCHEME_V->gc_verbose;
4886 4751
4887 SCHEME_V->gc_verbose = (car (SCHEME_V->args) != S_F); 4752 SCHEME_V->gc_verbose = (a != S_F);
4888 s_retbool (was); 4753 s_retbool (was);
4889 } 4754 }
4890 4755
4891 case OP_NEWSEGMENT: /* new-segment */ 4756 case OP_NEWSEGMENT: /* new-segment */
4757#if 0
4892 if (!is_pair (SCHEME_V->args) || !is_number (car (SCHEME_V->args))) 4758 if (!is_pair (args) || !is_number (a))
4893 Error_0 ("new-segment: argument must be a number"); 4759 Error_0 ("new-segment: argument must be a number");
4894 4760#endif
4895 alloc_cellseg (SCHEME_A_ (int)ivalue (car (SCHEME_V->args))); 4761 s_retbool (alloc_cellseg (SCHEME_A));
4896
4897 s_return (S_T);
4898 4762
4899 case OP_OBLIST: /* oblist */ 4763 case OP_OBLIST: /* oblist */
4900 s_return (oblist_all_symbols (SCHEME_A)); 4764 s_return (oblist_all_symbols (SCHEME_A));
4901 4765
4902#if USE_PORTS 4766#if USE_PORTS
4927 case OP_OPEN_INOUTFILE: 4791 case OP_OPEN_INOUTFILE:
4928 prop = port_input | port_output; 4792 prop = port_input | port_output;
4929 break; 4793 break;
4930 } 4794 }
4931 4795
4932 p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); 4796 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4933 4797
4934 if (p == NIL) 4798 s_return (p == NIL ? S_F : p);
4935 s_return (S_F);
4936
4937 s_return (p);
4938 } 4799 }
4939 4800
4940# if USE_STRING_PORTS 4801# if USE_STRING_PORTS
4941 4802
4942 case OP_OPEN_INSTRING: /* open-input-string */ 4803 case OP_OPEN_INSTRING: /* open-input-string */
4954 case OP_OPEN_INOUTSTRING: 4815 case OP_OPEN_INOUTSTRING:
4955 prop = port_input | port_output; 4816 prop = port_input | port_output;
4956 break; 4817 break;
4957 } 4818 }
4958 4819
4959 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4820 p = port_from_string (SCHEME_A_ strvalue (a),
4960 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); 4821 strvalue (a) + strlength (a), prop);
4961 4822
4962 if (p == NIL) 4823 s_return (p == NIL ? S_F : p);
4963 s_return (S_F);
4964
4965 s_return (p);
4966 } 4824 }
4967 4825
4968 case OP_OPEN_OUTSTRING: /* open-output-string */ 4826 case OP_OPEN_OUTSTRING: /* open-output-string */
4969 { 4827 {
4970 pointer p; 4828 pointer p;
4971 4829
4972 if (car (SCHEME_V->args) == NIL) 4830 if (a == NIL)
4973 {
4974 p = port_from_scratch (SCHEME_A); 4831 p = port_from_scratch (SCHEME_A);
4975
4976 if (p == NIL)
4977 s_return (S_F);
4978 }
4979 else 4832 else
4980 {
4981 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4833 p = port_from_string (SCHEME_A_ strvalue (a),
4982 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); 4834 strvalue (a) + strlength (a), port_output);
4983 4835
4984 if (p == NIL) 4836 s_return (p == NIL ? S_F : p);
4985 s_return (S_F);
4986 }
4987
4988 s_return (p);
4989 } 4837 }
4990 4838
4991 case OP_GET_OUTSTRING: /* get-output-string */ 4839 case OP_GET_OUTSTRING: /* get-output-string */
4992 { 4840 {
4993 port *p; 4841 port *p = port (a);
4994 4842
4995 if ((p = car (SCHEME_V->args)->object.port)->kind & port_string) 4843 if (p->kind & port_string)
4996 { 4844 {
4997 off_t size; 4845 off_t size;
4998 char *str; 4846 char *str;
4999 4847
5000 size = p->rep.string.curr - p->rep.string.start + 1; 4848 size = p->rep.string.curr - p->rep.string.start + 1;
5016 } 4864 }
5017 4865
5018# endif 4866# endif
5019 4867
5020 case OP_CLOSE_INPORT: /* close-input-port */ 4868 case OP_CLOSE_INPORT: /* close-input-port */
5021 port_close (SCHEME_A_ car (SCHEME_V->args), port_input); 4869 port_close (SCHEME_A_ a, port_input);
5022 s_return (S_T); 4870 s_return (S_T);
5023 4871
5024 case OP_CLOSE_OUTPORT: /* close-output-port */ 4872 case OP_CLOSE_OUTPORT: /* close-output-port */
5025 port_close (SCHEME_A_ car (SCHEME_V->args), port_output); 4873 port_close (SCHEME_A_ a, port_output);
5026 s_return (S_T); 4874 s_return (S_T);
5027#endif 4875#endif
5028 4876
5029 case OP_INT_ENV: /* interaction-environment */ 4877 case OP_INT_ENV: /* interaction-environment */
5030 s_return (SCHEME_V->global_env); 4878 s_return (SCHEME_V->global_env);
5032 case OP_CURR_ENV: /* current-environment */ 4880 case OP_CURR_ENV: /* current-environment */
5033 s_return (SCHEME_V->envir); 4881 s_return (SCHEME_V->envir);
5034 4882
5035 } 4883 }
5036 4884
5037 return S_T; 4885 if (USE_ERROR_CHECKING) abort ();
5038} 4886}
5039 4887
5040static pointer 4888static int
5041opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4889opexe_5 (SCHEME_P_ enum scheme_opcodes op)
5042{ 4890{
4891 pointer args = SCHEME_V->args;
5043 pointer x; 4892 pointer x;
5044 4893
5045 if (SCHEME_V->nesting != 0) 4894 if (SCHEME_V->nesting != 0)
5046 { 4895 {
5047 int n = SCHEME_V->nesting; 4896 int n = SCHEME_V->nesting;
5054 switch (op) 4903 switch (op)
5055 { 4904 {
5056 /* ========== reading part ========== */ 4905 /* ========== reading part ========== */
5057#if USE_PORTS 4906#if USE_PORTS
5058 case OP_READ: 4907 case OP_READ:
5059 if (!is_pair (SCHEME_V->args)) 4908 if (!is_pair (args))
5060 s_goto (OP_READ_INTERNAL); 4909 s_goto (OP_READ_INTERNAL);
5061 4910
5062 if (!is_inport (car (SCHEME_V->args))) 4911 if (!is_inport (car (args)))
5063 Error_1 ("read: not an input port:", car (SCHEME_V->args)); 4912 Error_1 ("read: not an input port:", car (args));
5064 4913
5065 if (car (SCHEME_V->args) == SCHEME_V->inport) 4914 if (car (args) == SCHEME_V->inport)
5066 s_goto (OP_READ_INTERNAL); 4915 s_goto (OP_READ_INTERNAL);
5067 4916
5068 x = SCHEME_V->inport; 4917 x = SCHEME_V->inport;
5069 SCHEME_V->inport = car (SCHEME_V->args); 4918 SCHEME_V->inport = car (args);
5070 x = cons (x, NIL); 4919 x = cons (x, NIL);
5071 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4920 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5072 s_goto (OP_READ_INTERNAL); 4921 s_goto (OP_READ_INTERNAL);
5073 4922
5074 case OP_READ_CHAR: /* read-char */ 4923 case OP_READ_CHAR: /* read-char */
5075 case OP_PEEK_CHAR: /* peek-char */ 4924 case OP_PEEK_CHAR: /* peek-char */
5076 { 4925 {
5077 int c; 4926 int c;
5078 4927
5079 if (is_pair (SCHEME_V->args)) 4928 if (is_pair (args))
5080 { 4929 {
5081 if (car (SCHEME_V->args) != SCHEME_V->inport) 4930 if (car (args) != SCHEME_V->inport)
5082 { 4931 {
5083 x = SCHEME_V->inport; 4932 x = SCHEME_V->inport;
5084 x = cons (x, NIL); 4933 x = cons (x, NIL);
5085 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4934 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5086 SCHEME_V->inport = car (SCHEME_V->args); 4935 SCHEME_V->inport = car (args);
5087 } 4936 }
5088 } 4937 }
5089 4938
5090 c = inchar (SCHEME_A); 4939 c = inchar (SCHEME_A);
5091 4940
5101 case OP_CHAR_READY: /* char-ready? */ 4950 case OP_CHAR_READY: /* char-ready? */
5102 { 4951 {
5103 pointer p = SCHEME_V->inport; 4952 pointer p = SCHEME_V->inport;
5104 int res; 4953 int res;
5105 4954
5106 if (is_pair (SCHEME_V->args)) 4955 if (is_pair (args))
5107 p = car (SCHEME_V->args); 4956 p = car (args);
5108 4957
5109 res = p->object.port->kind & port_string; 4958 res = port (p)->kind & port_string;
5110 4959
5111 s_retbool (res); 4960 s_retbool (res);
5112 } 4961 }
5113 4962
5114 case OP_SET_INPORT: /* set-input-port */ 4963 case OP_SET_INPORT: /* set-input-port */
5115 SCHEME_V->inport = car (SCHEME_V->args); 4964 SCHEME_V->inport = car (args);
5116 s_return (SCHEME_V->value); 4965 s_return (SCHEME_V->value);
5117 4966
5118 case OP_SET_OUTPORT: /* set-output-port */ 4967 case OP_SET_OUTPORT: /* set-output-port */
5119 SCHEME_V->outport = car (SCHEME_V->args); 4968 SCHEME_V->outport = car (args);
5120 s_return (SCHEME_V->value); 4969 s_return (SCHEME_V->value);
5121#endif 4970#endif
5122 4971
5123 case OP_RDSEXPR: 4972 case OP_RDSEXPR:
5124 switch (SCHEME_V->tok) 4973 switch (SCHEME_V->tok)
5173 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 5022 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
5174 SCHEME_V->tok = token (SCHEME_A); 5023 SCHEME_V->tok = token (SCHEME_A);
5175 s_goto (OP_RDSEXPR); 5024 s_goto (OP_RDSEXPR);
5176 5025
5177 case TOK_ATOM: 5026 case TOK_ATOM:
5178 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); 5027 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS)));
5028
5029 case TOK_DOTATOM:
5030 SCHEME_V->strbuff[0] = '.';
5031 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5032
5033 case TOK_STRATOM:
5034 x = readstrexp (SCHEME_A_ '|');
5035 //TODO: haven't checked whether the garbage collector could interfere
5036 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5179 5037
5180 case TOK_DQUOTE: 5038 case TOK_DQUOTE:
5181 x = readstrexp (SCHEME_A); 5039 x = readstrexp (SCHEME_A_ '"');
5182 5040
5183 if (x == S_F) 5041 if (x == S_F)
5184 Error_0 ("Error reading string"); 5042 Error_0 ("Error reading string");
5185 5043
5186 setimmutable (x); 5044 setimmutable (x);
5198 s_goto (OP_EVAL); 5056 s_goto (OP_EVAL);
5199 } 5057 }
5200 } 5058 }
5201 5059
5202 case TOK_SHARP_CONST: 5060 case TOK_SHARP_CONST:
5203 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) 5061 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5204 Error_0 ("undefined sharp expression"); 5062 Error_0 ("undefined sharp expression");
5205 else 5063 else
5206 s_return (x); 5064 s_return (x);
5207 5065
5208 default: 5066 default:
5210 } 5068 }
5211 5069
5212 break; 5070 break;
5213 5071
5214 case OP_RDLIST: 5072 case OP_RDLIST:
5215 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 5073 SCHEME_V->args = cons (SCHEME_V->value, args);
5216 SCHEME_V->tok = token (SCHEME_A); 5074 SCHEME_V->tok = token (SCHEME_A);
5217 5075
5218 switch (SCHEME_V->tok) 5076 switch (SCHEME_V->tok)
5219 { 5077 {
5220 case TOK_EOF: 5078 case TOK_EOF:
5248 case OP_RDDOT: 5106 case OP_RDDOT:
5249 if (token (SCHEME_A) != TOK_RPAREN) 5107 if (token (SCHEME_A) != TOK_RPAREN)
5250 Error_0 ("syntax error: illegal dot expression"); 5108 Error_0 ("syntax error: illegal dot expression");
5251 5109
5252 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5110 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5253 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); 5111 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, args));
5254 5112
5255 case OP_RDQUOTE: 5113 case OP_RDQUOTE:
5256 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); 5114 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)));
5257 5115
5258 case OP_RDQQUOTE: 5116 case OP_RDQQUOTE:
5280 SCHEME_V->args = SCHEME_V->value; 5138 SCHEME_V->args = SCHEME_V->value;
5281 s_goto (OP_VECTOR); 5139 s_goto (OP_VECTOR);
5282 5140
5283 /* ========== printing part ========== */ 5141 /* ========== printing part ========== */
5284 case OP_P0LIST: 5142 case OP_P0LIST:
5285 if (is_vector (SCHEME_V->args)) 5143 if (is_vector (args))
5286 { 5144 {
5287 putstr (SCHEME_A_ "#("); 5145 putstr (SCHEME_A_ "#(");
5288 SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); 5146 SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0));
5289 s_goto (OP_PVECFROM); 5147 s_goto (OP_PVECFROM);
5290 } 5148 }
5291 else if (is_environment (SCHEME_V->args)) 5149 else if (is_environment (args))
5292 { 5150 {
5293 putstr (SCHEME_A_ "#<ENVIRONMENT>"); 5151 putstr (SCHEME_A_ "#<ENVIRONMENT>");
5294 s_return (S_T); 5152 s_return (S_T);
5295 } 5153 }
5296 else if (!is_pair (SCHEME_V->args)) 5154 else if (!is_pair (args))
5297 { 5155 {
5298 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5156 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5299 s_return (S_T); 5157 s_return (S_T);
5300 } 5158 }
5301 else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5159 else
5302 { 5160 {
5161 pointer a = car (args);
5162 pointer b = cdr (args);
5163 int ok_abbr = ok_abbrev (b);
5164 SCHEME_V->args = car (b);
5165
5166 if (a == SCHEME_V->QUOTE && ok_abbr)
5303 putstr (SCHEME_A_ "'"); 5167 putstr (SCHEME_A_ "'");
5304 SCHEME_V->args = cadr (SCHEME_V->args); 5168 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5169 putstr (SCHEME_A_ "`");
5170 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5171 putstr (SCHEME_A_ ",");
5172 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5173 putstr (SCHEME_A_ ",@");
5174 else
5175 {
5176 putstr (SCHEME_A_ "(");
5177 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5178 SCHEME_V->args = a;
5179 }
5180
5305 s_goto (OP_P0LIST); 5181 s_goto (OP_P0LIST);
5306 } 5182 }
5307 else if (car (SCHEME_V->args) == SCHEME_V->QQUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5183
5184 case OP_P1LIST:
5185 if (is_pair (args))
5308 { 5186 {
5187 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5309 putstr (SCHEME_A_ "`"); 5188 putstr (SCHEME_A_ " ");
5310 SCHEME_V->args = cadr (SCHEME_V->args); 5189 SCHEME_V->args = car (args);
5311 s_goto (OP_P0LIST); 5190 s_goto (OP_P0LIST);
5312 } 5191 }
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)) 5192 else if (is_vector (args))
5342 { 5193 {
5343 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); 5194 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL);
5344 putstr (SCHEME_A_ " . "); 5195 putstr (SCHEME_A_ " . ");
5345 s_goto (OP_P0LIST); 5196 s_goto (OP_P0LIST);
5346 } 5197 }
5347 else 5198 else
5348 { 5199 {
5349 if (SCHEME_V->args != NIL) 5200 if (args != NIL)
5350 { 5201 {
5351 putstr (SCHEME_A_ " . "); 5202 putstr (SCHEME_A_ " . ");
5352 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5203 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5353 } 5204 }
5354 5205
5355 putstr (SCHEME_A_ ")"); 5206 putstr (SCHEME_A_ ")");
5356 s_return (S_T); 5207 s_return (S_T);
5357 } 5208 }
5358 5209
5359 case OP_PVECFROM: 5210 case OP_PVECFROM:
5360 { 5211 {
5361 int i = ivalue_unchecked (cdr (SCHEME_V->args)); 5212 int i = ivalue_unchecked (cdr (args));
5362 pointer vec = car (SCHEME_V->args); 5213 pointer vec = car (args);
5363 int len = veclength (vec); 5214 int len = veclength (vec);
5364 5215
5365 if (i == len) 5216 if (i == len)
5366 { 5217 {
5367 putstr (SCHEME_A_ ")"); 5218 putstr (SCHEME_A_ ")");
5368 s_return (S_T); 5219 s_return (S_T);
5369 } 5220 }
5370 else 5221 else
5371 { 5222 {
5372 pointer elem = vector_elem (vec, i); 5223 pointer elem = vector_get (vec, i);
5373 5224
5374 ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; 5225 ivalue_unchecked (cdr (args)) = i + 1;
5375 s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); 5226 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5376 SCHEME_V->args = elem; 5227 SCHEME_V->args = elem;
5377 5228
5378 if (i > 0) 5229 if (i > 0)
5379 putstr (SCHEME_A_ " "); 5230 putstr (SCHEME_A_ " ");
5380 5231
5381 s_goto (OP_P0LIST); 5232 s_goto (OP_P0LIST);
5382 } 5233 }
5383 } 5234 }
5384 } 5235 }
5385 5236
5386 return S_T; 5237 if (USE_ERROR_CHECKING) abort ();
5387} 5238}
5388 5239
5389static pointer 5240static int
5390opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5241opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5391{ 5242{
5243 pointer args = SCHEME_V->args;
5244 pointer a = car (args);
5392 pointer x, y; 5245 pointer x, y;
5393 5246
5394 switch (op) 5247 switch (op)
5395 { 5248 {
5396 case OP_LIST_LENGTH: /* length *//* a.k */ 5249 case OP_LIST_LENGTH: /* length *//* a.k */
5397 { 5250 {
5398 long v = list_length (SCHEME_A_ car (SCHEME_V->args)); 5251 long v = list_length (SCHEME_A_ a);
5399 5252
5400 if (v < 0) 5253 if (v < 0)
5401 Error_1 ("length: not a list:", car (SCHEME_V->args)); 5254 Error_1 ("length: not a list:", a);
5402 5255
5403 s_return (mk_integer (SCHEME_A_ v)); 5256 s_return (mk_integer (SCHEME_A_ v));
5404 } 5257 }
5405 5258
5406 case OP_ASSQ: /* assq *//* a.k */ 5259 case OP_ASSQ: /* assq *//* a.k */
5407 x = car (SCHEME_V->args); 5260 x = a;
5408 5261
5409 for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) 5262 for (y = cadr (args); is_pair (y); y = cdr (y))
5410 { 5263 {
5411 if (!is_pair (car (y))) 5264 if (!is_pair (car (y)))
5412 Error_0 ("unable to handle non pair element"); 5265 Error_0 ("unable to handle non pair element");
5413 5266
5414 if (x == caar (y)) 5267 if (x == caar (y))
5420 else 5273 else
5421 s_return (S_F); 5274 s_return (S_F);
5422 5275
5423 5276
5424 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5277 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5425 SCHEME_V->args = car (SCHEME_V->args); 5278 SCHEME_V->args = a;
5426 5279
5427 if (SCHEME_V->args == NIL) 5280 if (SCHEME_V->args == NIL)
5428 s_return (S_F); 5281 s_return (S_F);
5429 else if (is_closure (SCHEME_V->args)) 5282 else if (is_closure (SCHEME_V->args))
5430 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5283 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5435 5288
5436 case OP_CLOSUREP: /* closure? */ 5289 case OP_CLOSUREP: /* closure? */
5437 /* 5290 /*
5438 * Note, macro object is also a closure. 5291 * Note, macro object is also a closure.
5439 * Therefore, (closure? <#MACRO>) ==> #t 5292 * Therefore, (closure? <#MACRO>) ==> #t
5293 * (schmorp) well, obviously not, fix? TODO
5440 */ 5294 */
5441 s_retbool (is_closure (car (SCHEME_V->args))); 5295 s_retbool (is_closure (a));
5442 5296
5443 case OP_MACROP: /* macro? */ 5297 case OP_MACROP: /* macro? */
5444 s_retbool (is_macro (car (SCHEME_V->args))); 5298 s_retbool (is_macro (a));
5445 } 5299 }
5446 5300
5447 return S_T; /* NOTREACHED */ 5301 if (USE_ERROR_CHECKING) abort ();
5448} 5302}
5449 5303
5304/* 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); 5305typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5451 5306
5452typedef int (*test_predicate) (pointer); 5307typedef int (*test_predicate)(pointer);
5453static int 5308static int
5454is_any (pointer p) 5309tst_any (pointer p)
5455{ 5310{
5456 return 1; 5311 return 1;
5457} 5312}
5458 5313
5459static int 5314static int
5460is_nonneg (pointer p) 5315tst_inonneg (pointer p)
5461{ 5316{
5462 return ivalue (p) >= 0 && is_integer (p); 5317 return is_integer (p) && ivalue_unchecked (p) >= 0;
5318}
5319
5320static int
5321tst_is_list (SCHEME_P_ pointer p)
5322{
5323 return p == NIL || is_pair (p);
5463} 5324}
5464 5325
5465/* Correspond carefully with following defines! */ 5326/* Correspond carefully with following defines! */
5466static struct 5327static struct
5467{ 5328{
5468 test_predicate fct; 5329 test_predicate fct;
5469 const char *kind; 5330 const char *kind;
5470} tests[] = 5331} tests[] = {
5471{ 5332 { tst_any , 0 },
5472 { 0, 0}, /* unused */ 5333 { is_string , "string" },
5473 { is_any, 0}, 5334 { is_symbol , "symbol" },
5474 { is_string, "string" }, 5335 { is_port , "port" },
5475 { is_symbol, "symbol" },
5476 { is_port, "port" },
5477 { is_inport, "input port" }, 5336 { is_inport , "input port" },
5478 { is_outport, "output port" }, 5337 { is_outport , "output port" },
5479 { is_environment, "environment" }, 5338 { is_environment, "environment" },
5480 { is_pair, "pair" }, 5339 { is_pair , "pair" },
5481 { 0, "pair or '()" }, 5340 { 0 , "pair or '()" },
5482 { is_character, "character" }, 5341 { is_character , "character" },
5483 { is_vector, "vector" }, 5342 { is_vector , "vector" },
5484 { is_number, "number" }, 5343 { is_number , "number" },
5485 { is_integer, "integer" }, 5344 { is_integer , "integer" },
5486 { is_nonneg, "non-negative integer" } 5345 { tst_inonneg , "non-negative integer" }
5487}; 5346};
5488 5347
5489#define TST_NONE 0 5348#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5490#define TST_ANY "\001" 5349#define TST_ANY "\001"
5491#define TST_STRING "\002" 5350#define TST_STRING "\002"
5492#define TST_SYMBOL "\003" 5351#define TST_SYMBOL "\003"
5493#define TST_PORT "\004" 5352#define TST_PORT "\004"
5494#define TST_INPORT "\005" 5353#define TST_INPORT "\005"
5495#define TST_OUTPORT "\006" 5354#define TST_OUTPORT "\006"
5496#define TST_ENVIRONMENT "\007" 5355#define TST_ENVIRONMENT "\007"
5497#define TST_PAIR "\010" 5356#define TST_PAIR "\010"
5498#define TST_LIST "\011" 5357#define TST_LIST "\011"
5499#define TST_CHAR "\012" 5358#define TST_CHAR "\012"
5500#define TST_VECTOR "\013" 5359#define TST_VECTOR "\013"
5501#define TST_NUMBER "\014" 5360#define TST_NUMBER "\014"
5502#define TST_INTEGER "\015" 5361#define TST_INTEGER "\015"
5503#define TST_NATURAL "\016" 5362#define TST_NATURAL "\016"
5363
5364#define INF_ARG 0xff
5365#define UNNAMED_OP ""
5366
5367static const char opnames[] =
5368#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5369#include "opdefines.h"
5370#undef OP_DEF
5371;
5372
5373static const char *
5374opname (int idx)
5375{
5376 const char *name = opnames;
5377
5378 /* should do this at compile time, but would require external program, right? */
5379 while (idx--)
5380 name += strlen (name) + 1;
5381
5382 return *name ? name : "ILLEGAL";
5383}
5384
5385static const char *
5386procname (pointer x)
5387{
5388 return opname (procnum (x));
5389}
5504 5390
5505typedef struct 5391typedef struct
5506{ 5392{
5507 dispatch_func func; 5393 uint8_t func;
5508 char *name; 5394 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5395 uint8_t builtin;
5396#if USE_ERROR_CHECKING
5509 int min_arity; 5397 uint8_t min_arity;
5510 int max_arity; 5398 uint8_t max_arity;
5511 char *arg_tests_encoding; 5399 char arg_tests_encoding[3];
5400#endif
5512} op_code_info; 5401} op_code_info;
5513 5402
5514#define INF_ARG 0xffff
5515
5516static op_code_info dispatch_table[] = { 5403static const op_code_info dispatch_table[] = {
5517#define OP_DEF(A,B,C,D,E,OP) {A,B,C,D,E}, 5404#if USE_ERROR_CHECKING
5405#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5406#else
5407#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5408#endif
5518#include "opdefines.h" 5409#include "opdefines.h"
5410#undef OP_DEF
5519 {0} 5411 {0}
5520}; 5412};
5521 5413
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 */ 5414/* kernel of this interpreter */
5535static void 5415static void ecb_hot
5536Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5416Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5537{ 5417{
5538 SCHEME_V->op = op; 5418 SCHEME_V->op = op;
5539 5419
5540 for (;;) 5420 for (;;)
5541 { 5421 {
5542 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5422 const op_code_info *pcd = dispatch_table + SCHEME_V->op;
5543 5423
5544#if USE_ERROR_CHECKING 5424#if USE_ERROR_CHECKING
5545 if (pcd->name) /* if built-in function, check arguments */ 5425 if (pcd->builtin) /* if built-in function, check arguments */
5546 { 5426 {
5547 int ok = 1;
5548 char msg[STRBUFFSIZE]; 5427 char msg[STRBUFFSIZE];
5549 int n = list_length (SCHEME_A_ SCHEME_V->args); 5428 int n = list_length (SCHEME_A_ SCHEME_V->args);
5550 5429
5551 /* Check number of arguments */ 5430 /* Check number of arguments */
5552 if (ecb_expect_false (n < pcd->min_arity)) 5431 if (ecb_expect_false (n < pcd->min_arity))
5553 { 5432 {
5554 ok = 0;
5555 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5433 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5556 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); 5434 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity);
5435 xError_1 (SCHEME_A_ msg, 0);
5436 continue;
5557 } 5437 }
5558 else if (ecb_excpect_false (n > pcd->max_arity)) 5438 else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG))
5559 { 5439 {
5560 ok = 0;
5561 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5440 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5562 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); 5441 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity);
5442 xError_1 (SCHEME_A_ msg, 0);
5443 continue;
5563 } 5444 }
5564 5445 else
5565 if (ecb_expect_false (ok))
5566 { 5446 {
5567 if (pcd->arg_tests_encoding) 5447 if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */
5568 { 5448 {
5569 int i = 0; 5449 int i = 0;
5570 int j; 5450 int j;
5571 const char *t = pcd->arg_tests_encoding; 5451 const char *t = pcd->arg_tests_encoding;
5572 pointer arglist = SCHEME_V->args; 5452 pointer arglist = SCHEME_V->args;
5573 5453
5574 do 5454 do
5575 { 5455 {
5576 pointer arg = car (arglist); 5456 pointer arg = car (arglist);
5577 5457
5578 j = (int) t[0]; 5458 j = t[0];
5579 5459
5460 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5580 if (j == TST_LIST[0]) 5461 if (j == TST_LIST[0])
5581 { 5462 {
5582 if (arg != NIL && !is_pair (arg)) 5463 if (!tst_is_list (SCHEME_A_ arg))
5583 break; 5464 break;
5584 } 5465 }
5585 else 5466 else
5586 { 5467 {
5587 if (!tests[j].fct (arg)) 5468 if (!tests[j - 1].fct (arg))
5588 break; 5469 break;
5589 } 5470 }
5590 5471
5591 if (t[1] != 0) /* last test is replicated as necessary */ 5472 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5592 t++; 5473 t++;
5593 5474
5594 arglist = cdr (arglist); 5475 arglist = cdr (arglist);
5595 i++; 5476 i++;
5596 } 5477 }
5597 while (i < n); 5478 while (i < n);
5598 5479
5599 if (i < n) 5480 if (i < n)
5600 { 5481 {
5601 ok = 0;
5602 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); 5482 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind);
5483 xError_1 (SCHEME_A_ msg, 0);
5484 continue;
5603 } 5485 }
5604 } 5486 }
5605 } 5487 }
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 } 5488 }
5615#endif 5489#endif
5616 5490
5617 ok_to_freely_gc (SCHEME_A); 5491 ok_to_freely_gc (SCHEME_A);
5618 5492
5493 static const dispatch_func dispatch_funcs[] = {
5494 opexe_0,
5495 opexe_1,
5496 opexe_2,
5497 opexe_3,
5498 opexe_4,
5499 opexe_5,
5500 opexe_6,
5501 };
5502
5619 if (ecb_expect_false (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL)) 5503 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5620 return; 5504 return;
5621 5505
5622 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5506 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5623 { 5507 {
5624 xwrstr ("No memory!\n"); 5508 putstr (SCHEME_A_ "No memory!\n");
5625 return; 5509 return;
5626 } 5510 }
5627 } 5511 }
5628} 5512}
5629 5513
5648mk_proc (SCHEME_P_ enum scheme_opcodes op) 5532mk_proc (SCHEME_P_ enum scheme_opcodes op)
5649{ 5533{
5650 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5534 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5651 set_typeflag (y, (T_PROC | T_ATOM)); 5535 set_typeflag (y, (T_PROC | T_ATOM));
5652 ivalue_unchecked (y) = op; 5536 ivalue_unchecked (y) = op;
5653 set_num_integer (y);
5654 return y; 5537 return y;
5655} 5538}
5656 5539
5657/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5540/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5658static int 5541static int
5659syntaxnum (pointer p) 5542syntaxnum (pointer p)
5660{ 5543{
5661 const char *s = strvalue (car (p)); 5544 const char *s = strvalue (p);
5662 5545
5663 switch (strlength (car (p))) 5546 switch (strlength (p))
5664 { 5547 {
5665 case 2: 5548 case 2:
5666 if (s[0] == 'i') 5549 if (s[0] == 'i')
5667 return OP_IF0; /* if */ 5550 return OP_IF0; /* if */
5668 else 5551 else
5723 return OP_C0STREAM; /* cons-stream */ 5606 return OP_C0STREAM; /* cons-stream */
5724 } 5607 }
5725} 5608}
5726 5609
5727#if USE_MULTIPLICITY 5610#if USE_MULTIPLICITY
5728scheme * 5611ecb_cold scheme *
5729scheme_init_new () 5612scheme_init_new ()
5730{ 5613{
5731 scheme *sc = malloc (sizeof (scheme)); 5614 scheme *sc = malloc (sizeof (scheme));
5732 5615
5733 if (!scheme_init (SCHEME_A)) 5616 if (!scheme_init (SCHEME_A))
5738 else 5621 else
5739 return sc; 5622 return sc;
5740} 5623}
5741#endif 5624#endif
5742 5625
5743int 5626ecb_cold int
5744scheme_init (SCHEME_P) 5627scheme_init (SCHEME_P)
5745{ 5628{
5746 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5629 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5747 pointer x; 5630 pointer x;
5631
5632 /* this memset is not strictly correct, as we assume (intcache)
5633 * that memset 0 will also set pointers to 0, but memset does
5634 * of course not guarantee that. screw such systems.
5635 */
5636 memset (SCHEME_V, 0, sizeof (*SCHEME_V));
5748 5637
5749 num_set_fixnum (num_zero, 1); 5638 num_set_fixnum (num_zero, 1);
5750 num_set_ivalue (num_zero, 0); 5639 num_set_ivalue (num_zero, 0);
5751 num_set_fixnum (num_one, 1); 5640 num_set_fixnum (num_one, 1);
5752 num_set_ivalue (num_one, 1); 5641 num_set_ivalue (num_one, 1);
5764 SCHEME_V->save_inport = NIL; 5653 SCHEME_V->save_inport = NIL;
5765 SCHEME_V->loadport = NIL; 5654 SCHEME_V->loadport = NIL;
5766 SCHEME_V->nesting = 0; 5655 SCHEME_V->nesting = 0;
5767 SCHEME_V->interactive_repl = 0; 5656 SCHEME_V->interactive_repl = 0;
5768 5657
5769 if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) 5658 if (!alloc_cellseg (SCHEME_A))
5770 { 5659 {
5771#if USE_ERROR_CHECKING 5660#if USE_ERROR_CHECKING
5772 SCHEME_V->no_memory = 1; 5661 SCHEME_V->no_memory = 1;
5773 return 0; 5662 return 0;
5774#endif 5663#endif
5821 5710
5822 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) 5711 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
5823 assign_syntax (SCHEME_A_ syntax_names[i]); 5712 assign_syntax (SCHEME_A_ syntax_names[i]);
5824 } 5713 }
5825 5714
5715 // TODO: should iterate via strlen, to avoid n² complexity
5826 for (i = 0; i < n; i++) 5716 for (i = 0; i < n; i++)
5827 if (dispatch_table[i].name != 0) 5717 if (dispatch_table[i].builtin)
5828 assign_proc (SCHEME_A_ i, dispatch_table[i].name); 5718 assign_proc (SCHEME_A_ i, opname (i));
5829 5719
5830 /* initialization of global pointers to special symbols */ 5720 /* initialization of global pointers to special symbols */
5831 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 5721 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
5832 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 5722 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
5833 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 5723 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
5872scheme_set_external_data (SCHEME_P_ void *p) 5762scheme_set_external_data (SCHEME_P_ void *p)
5873{ 5763{
5874 SCHEME_V->ext_data = p; 5764 SCHEME_V->ext_data = p;
5875} 5765}
5876 5766
5877void 5767ecb_cold void
5878scheme_deinit (SCHEME_P) 5768scheme_deinit (SCHEME_P)
5879{ 5769{
5880 int i; 5770 int i;
5881 5771
5882#if SHOW_ERROR_LINE 5772#if SHOW_ERROR_LINE
5974{ 5864{
5975 dump_stack_reset (SCHEME_A); 5865 dump_stack_reset (SCHEME_A);
5976 SCHEME_V->envir = SCHEME_V->global_env; 5866 SCHEME_V->envir = SCHEME_V->global_env;
5977 SCHEME_V->file_i = 0; 5867 SCHEME_V->file_i = 0;
5978 SCHEME_V->load_stack[0].kind = port_input | port_string; 5868 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 */ 5869 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); 5870 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; 5871 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
5982#if USE_PORTS 5872#if USE_PORTS
5983 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 5873 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5984#endif 5874#endif
5985 SCHEME_V->retcode = 0; 5875 SCHEME_V->retcode = 0;
5986 SCHEME_V->interactive_repl = 0; 5876 SCHEME_V->interactive_repl = 0;
6103# endif 5993# endif
6104 int fin; 5994 int fin;
6105 char *file_name = InitFile; 5995 char *file_name = InitFile;
6106 int retcode; 5996 int retcode;
6107 int isfile = 1; 5997 int isfile = 1;
5998 system ("ps v $PPID");//D
6108 5999
6109 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6000 if (argc == 2 && strcmp (argv[1], "-?") == 0)
6110 { 6001 {
6111 xwrstr ("Usage: tinyscheme -?\n"); 6002 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
6112 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6003 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
6113 xwrstr ("followed by\n"); 6004 putstr (SCHEME_A_ "followed by\n");
6114 xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); 6005 putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n");
6115 xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); 6006 putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n");
6116 xwrstr ("assuming that the executable is named tinyscheme.\n"); 6007 putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n");
6117 xwrstr ("Use - as filename for stdin.\n"); 6008 putstr (SCHEME_A_ "Use - as filename for stdin.\n");
6118 return 1; 6009 return 1;
6119 } 6010 }
6120 6011
6121 if (!scheme_init (SCHEME_A)) 6012 if (!scheme_init (SCHEME_A))
6122 { 6013 {
6123 xwrstr ("Could not initialize!\n"); 6014 putstr (SCHEME_A_ "Could not initialize!\n");
6124 return 2; 6015 return 2;
6125 } 6016 }
6126 6017
6127# if USE_PORTS 6018# if USE_PORTS
6128 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); 6019 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO);
6173 fin = open (file_name, O_RDONLY); 6064 fin = open (file_name, O_RDONLY);
6174#endif 6065#endif
6175 6066
6176 if (isfile && fin < 0) 6067 if (isfile && fin < 0)
6177 { 6068 {
6178 xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); 6069 putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n");
6179 } 6070 }
6180 else 6071 else
6181 { 6072 {
6182 if (isfile) 6073 if (isfile)
6183 scheme_load_named_file (SCHEME_A_ fin, file_name); 6074 scheme_load_named_file (SCHEME_A_ fin, file_name);
6187#if USE_PORTS 6078#if USE_PORTS
6188 if (!isfile || fin != STDIN_FILENO) 6079 if (!isfile || fin != STDIN_FILENO)
6189 { 6080 {
6190 if (SCHEME_V->retcode != 0) 6081 if (SCHEME_V->retcode != 0)
6191 { 6082 {
6192 xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); 6083 putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n");
6193 } 6084 }
6194 6085
6195 if (isfile) 6086 if (isfile)
6196 close (fin); 6087 close (fin);
6197 } 6088 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines