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

Comparing cvsroot/microscheme/scheme.c (file contents):
Revision 1.15 by root, Thu Nov 26 09:05:20 2015 UTC vs.
Revision 1.57 by root, Tue Dec 1 04:57:49 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
222/* num, for generic arithmetic */
223struct num
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
213enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; 248enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
214 249
215static num num_op (enum num_op op, num a, num b); 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_rem (num a, num b); 252static num num_rem (num a, num b);
218static num num_mod (num a, num b); 253static num num_mod (num a, num b);
219 254
220#if USE_MATH
221static double round_per_R5RS (double x);
222#endif
223static int is_zero_rvalue (RVALUE x); 255static int is_zero_rvalue (RVALUE x);
224
225static INLINE int
226num_is_integer (pointer p)
227{
228 return num_is_fixnum (p->object.number);
229}
230 256
231static num num_zero; 257static num num_zero;
232static num num_one; 258static num num_one;
233 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
234/* macros for cell operations */ 264/* macros for cell operations */
235#define typeflag(p) ((p)->flag + 0) 265#define typeflag(p) (CELL(p)->flag + 0)
236#define set_typeflag(p,v) ((p)->flag = (v)) 266#define set_typeflag(p,v) (CELL(p)->flag = (v))
237#define type(p) (typeflag (p) & T_MASKTYPE) 267#define type(p) (typeflag (p) & T_MASKTYPE)
238 268
239INTERFACE INLINE int 269INTERFACE int
240is_string (pointer p) 270is_string (pointer p)
241{ 271{
242 return type (p) == T_STRING; 272 return type (p) == T_STRING;
243} 273}
244 274
245#define strvalue(p) ((p)->object.string.svalue) 275#define strvalue(p) (CELL(p)->object.string.svalue)
246#define strlength(p) ((p)->object.string.length) 276#define strlength(p) (CELL(p)->object.string.length)
247 277
248INTERFACE int is_list (SCHEME_P_ pointer p);
249INTERFACE INLINE int 278INTERFACE int
250is_vector (pointer p) 279is_vector (pointer p)
251{ 280{
252 return type (p) == T_VECTOR; 281 return type (p) == T_VECTOR;
253} 282}
254 283
255#define vecvalue(p) ((p)->object.vector.vvalue) 284#define vecvalue(p) (CELL(p)->object.vector.vvalue)
256#define veclength(p) ((p)->object.vector.length) 285#define veclength(p) (CELL(p)->object.vector.length)
257INTERFACE void fill_vector (pointer vec, pointer obj); 286INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
258INTERFACE uint32_t vector_length (pointer vec);
259INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); 287INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
260INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); 288INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
261 289
262INTERFACE uint32_t 290INTERFACE int
263vector_length (pointer vec) 291is_integer (pointer p)
264{ 292{
265 return vec->object.vector.length; 293 return type (p) == T_INTEGER;
266} 294}
267 295
296/* not the same as in scheme, where integers are (correctly :) reals */
268INTERFACE INLINE int 297INTERFACE int
298is_real (pointer p)
299{
300 return type (p) == T_REAL;
301}
302
303INTERFACE int
269is_number (pointer p) 304is_number (pointer p)
270{ 305{
271 return type (p) == T_NUMBER; 306 return is_integer (p) || is_real (p);
272} 307}
273 308
274INTERFACE INLINE int 309INTERFACE int
275is_integer (pointer p)
276{
277 if (!is_number (p))
278 return 0;
279
280 if (num_is_integer (p) || ivalue (p) == rvalue (p))
281 return 1;
282
283 return 0;
284}
285
286INTERFACE INLINE int
287is_real (pointer p)
288{
289 return is_number (p) && !num_is_fixnum (p->object.number);
290}
291
292INTERFACE INLINE int
293is_character (pointer p) 310is_character (pointer p)
294{ 311{
295 return type (p) == T_CHARACTER; 312 return type (p) == T_CHARACTER;
296} 313}
297 314
298INTERFACE INLINE char * 315INTERFACE char *
299string_value (pointer p) 316string_value (pointer p)
300{ 317{
301 return strvalue (p); 318 return strvalue (p);
302} 319}
303 320
304INLINE num
305nvalue (pointer p)
306{
307 return (p)->object.number;
308}
309
310static IVALUE
311num_get_ivalue (const num n)
312{
313 return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n);
314}
315
316static RVALUE
317num_get_rvalue (const num n)
318{
319 return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n);
320}
321
322INTERFACE IVALUE
323ivalue (pointer p)
324{
325 return num_get_ivalue (p->object.number);
326}
327
328INTERFACE RVALUE
329rvalue (pointer p)
330{
331 return num_get_rvalue (p->object.number);
332}
333
334#define ivalue_unchecked(p) ((p)->object.number.value.ivalue) 321#define ivalue_unchecked(p) CELL(p)->object.ivalue
322#define set_ivalue(p,v) CELL(p)->object.ivalue = (v)
323
335#if USE_REAL 324#if USE_REAL
336# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 325#define rvalue_unchecked(p) CELL(p)->object.rvalue
337# define set_num_integer(p) (p)->object.number.is_fixnum=1; 326#define set_rvalue(p,v) CELL(p)->object.rvalue = (v)
338# define set_num_real(p) (p)->object.number.is_fixnum=0;
339#else 327#else
340# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 328#define rvalue_unchecked(p) CELL(p)->object.ivalue
341# define set_num_integer(p) 0 329#define set_rvalue(p,v) CELL(p)->object.ivalue = (v)
342# define set_num_real(p) 0
343#endif 330#endif
331
344INTERFACE long 332INTERFACE long
345charvalue (pointer p) 333charvalue (pointer p)
346{ 334{
347 return ivalue_unchecked (p); 335 return ivalue_unchecked (p);
348} 336}
349 337
338#define port(p) CELL(p)->object.port
339#define set_port(p,v) port(p) = (v)
350INTERFACE INLINE int 340INTERFACE int
351is_port (pointer p) 341is_port (pointer p)
352{ 342{
353 return type (p) == T_PORT; 343 return type (p) == T_PORT;
354} 344}
355 345
356INTERFACE INLINE int 346INTERFACE int
357is_inport (pointer p) 347is_inport (pointer p)
358{ 348{
359 return is_port (p) && p->object.port->kind & port_input; 349 return is_port (p) && port (p)->kind & port_input;
360} 350}
361 351
362INTERFACE INLINE int 352INTERFACE int
363is_outport (pointer p) 353is_outport (pointer p)
364{ 354{
365 return is_port (p) && p->object.port->kind & port_output; 355 return is_port (p) && port (p)->kind & port_output;
366} 356}
367 357
368INTERFACE INLINE int 358INTERFACE int
369is_pair (pointer p) 359is_pair (pointer p)
370{ 360{
371 return type (p) == T_PAIR; 361 return type (p) == T_PAIR;
372} 362}
373 363
374#define car(p) ((p)->object.cons.car + 0) 364#define car(p) (POINTER (CELL(p)->object.cons.car))
375#define cdr(p) ((p)->object.cons.cdr + 0) 365#define cdr(p) (POINTER (CELL(p)->object.cons.cdr))
376 366
377static pointer caar (pointer p) { return car (car (p)); } 367static pointer caar (pointer p) { return car (car (p)); }
378static pointer cadr (pointer p) { return car (cdr (p)); } 368static pointer cadr (pointer p) { return car (cdr (p)); }
379static pointer cdar (pointer p) { return cdr (car (p)); } 369static pointer cdar (pointer p) { return cdr (car (p)); }
380static pointer cddr (pointer p) { return cdr (cdr (p)); } 370static pointer cddr (pointer p) { return cdr (cdr (p)); }
384static pointer cdaar (pointer p) { return cdr (car (car (p))); } 374static pointer cdaar (pointer p) { return cdr (car (car (p))); }
385 375
386INTERFACE void 376INTERFACE void
387set_car (pointer p, pointer q) 377set_car (pointer p, pointer q)
388{ 378{
389 p->object.cons.car = q; 379 CELL(p)->object.cons.car = CELL (q);
390} 380}
391 381
392INTERFACE void 382INTERFACE void
393set_cdr (pointer p, pointer q) 383set_cdr (pointer p, pointer q)
394{ 384{
395 p->object.cons.cdr = q; 385 CELL(p)->object.cons.cdr = CELL (q);
396} 386}
397 387
398INTERFACE pointer 388INTERFACE pointer
399pair_car (pointer p) 389pair_car (pointer p)
400{ 390{
405pair_cdr (pointer p) 395pair_cdr (pointer p)
406{ 396{
407 return cdr (p); 397 return cdr (p);
408} 398}
409 399
410INTERFACE INLINE int 400INTERFACE int
411is_symbol (pointer p) 401is_symbol (pointer p)
412{ 402{
413 return type (p) == T_SYMBOL; 403 return type (p) == T_SYMBOL;
414} 404}
415 405
416INTERFACE INLINE char * 406INTERFACE char *
417symname (pointer p) 407symname (pointer p)
418{ 408{
419 return strvalue (car (p)); 409 return strvalue (p);
420} 410}
421 411
422#if USE_PLIST 412#if USE_PLIST
413#error plists are broken because symbols are no longer pairs
414#define symprop(p) cdr(p)
423SCHEME_EXPORT INLINE int 415SCHEME_EXPORT int
424hasprop (pointer p) 416hasprop (pointer p)
425{ 417{
426 return typeflag (p) & T_SYMBOL; 418 return typeflag (p) & T_SYMBOL;
427} 419}
428
429# define symprop(p) cdr(p)
430#endif 420#endif
431 421
432INTERFACE INLINE int 422INTERFACE int
433is_syntax (pointer p) 423is_syntax (pointer p)
434{ 424{
435 return typeflag (p) & T_SYNTAX; 425 return typeflag (p) & T_SYNTAX;
436} 426}
437 427
438INTERFACE INLINE int 428INTERFACE int
439is_proc (pointer p) 429is_proc (pointer p)
440{ 430{
441 return type (p) == T_PROC; 431 return type (p) == T_PROC;
442} 432}
443 433
444INTERFACE INLINE int 434INTERFACE int
445is_foreign (pointer p) 435is_foreign (pointer p)
446{ 436{
447 return type (p) == T_FOREIGN; 437 return type (p) == T_FOREIGN;
448} 438}
449 439
450INTERFACE INLINE char * 440INTERFACE char *
451syntaxname (pointer p) 441syntaxname (pointer p)
452{ 442{
453 return strvalue (car (p)); 443 return strvalue (p);
454} 444}
455 445
456#define procnum(p) ivalue (p) 446#define procnum(p) ivalue_unchecked (p)
457static const char *procname (pointer x); 447static const char *procname (pointer x);
458 448
459INTERFACE INLINE int 449INTERFACE int
460is_closure (pointer p) 450is_closure (pointer p)
461{ 451{
462 return type (p) == T_CLOSURE; 452 return type (p) == T_CLOSURE;
463} 453}
464 454
465INTERFACE INLINE int 455INTERFACE int
466is_macro (pointer p) 456is_macro (pointer p)
467{ 457{
468 return type (p) == T_MACRO; 458 return type (p) == T_MACRO;
469} 459}
470 460
471INTERFACE INLINE pointer 461INTERFACE pointer
472closure_code (pointer p) 462closure_code (pointer p)
473{ 463{
474 return car (p); 464 return car (p);
475} 465}
476 466
477INTERFACE INLINE pointer 467INTERFACE pointer
478closure_env (pointer p) 468closure_env (pointer p)
479{ 469{
480 return cdr (p); 470 return cdr (p);
481} 471}
482 472
483INTERFACE INLINE int 473INTERFACE int
484is_continuation (pointer p) 474is_continuation (pointer p)
485{ 475{
486 return type (p) == T_CONTINUATION; 476 return type (p) == T_CONTINUATION;
487} 477}
488 478
489#define cont_dump(p) cdr (p) 479#define cont_dump(p) cdr (p)
490#define set_cont_dump(p,v) set_cdr ((p), (v)) 480#define set_cont_dump(p,v) set_cdr ((p), (v))
491 481
492/* To do: promise should be forced ONCE only */ 482/* To do: promise should be forced ONCE only */
493INTERFACE INLINE int 483INTERFACE int
494is_promise (pointer p) 484is_promise (pointer p)
495{ 485{
496 return type (p) == T_PROMISE; 486 return type (p) == T_PROMISE;
497} 487}
498 488
499INTERFACE INLINE int 489INTERFACE int
500is_environment (pointer p) 490is_environment (pointer p)
501{ 491{
502 return type (p) == T_ENVIRONMENT; 492 return type (p) == T_ENVIRONMENT;
503} 493}
504 494
510 500
511#define is_mark(p) (typeflag (p) & T_MARK) 501#define is_mark(p) (typeflag (p) & T_MARK)
512#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) 502#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
513#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) 503#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
514 504
515INTERFACE INLINE int 505INTERFACE int
516is_immutable (pointer p) 506is_immutable (pointer p)
517{ 507{
518 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; 508 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
519} 509}
520 510
521INTERFACE INLINE void 511INTERFACE void
522setimmutable (pointer p) 512setimmutable (pointer p)
523{ 513{
524#if USE_ERROR_CHECKING 514#if USE_ERROR_CHECKING
525 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 515 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
526#endif 516#endif
527} 517}
528 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
529#if USE_CHAR_CLASSIFIERS 573#if USE_CHAR_CLASSIFIERS
530static INLINE int 574ecb_inline int
531Cisalpha (int c) 575Cisalpha (int c)
532{ 576{
533 return isascii (c) && isalpha (c); 577 return isascii (c) && isalpha (c);
534} 578}
535 579
536static INLINE int 580ecb_inline int
537Cisdigit (int c) 581Cisdigit (int c)
538{ 582{
539 return isascii (c) && isdigit (c); 583 return isascii (c) && isdigit (c);
540} 584}
541 585
542static INLINE int 586ecb_inline int
543Cisspace (int c) 587Cisspace (int c)
544{ 588{
545 return isascii (c) && isspace (c); 589 return isascii (c) && isspace (c);
546} 590}
547 591
548static INLINE int 592ecb_inline int
549Cisupper (int c) 593Cisupper (int c)
550{ 594{
551 return isascii (c) && isupper (c); 595 return isascii (c) && isupper (c);
552} 596}
553 597
554static INLINE int 598ecb_inline int
555Cislower (int c) 599Cislower (int c)
556{ 600{
557 return isascii (c) && islower (c); 601 return isascii (c) && islower (c);
558} 602}
559#endif 603#endif
620#endif 664#endif
621 665
622static int file_push (SCHEME_P_ const char *fname); 666static int file_push (SCHEME_P_ const char *fname);
623static void file_pop (SCHEME_P); 667static void file_pop (SCHEME_P);
624static int file_interactive (SCHEME_P); 668static int file_interactive (SCHEME_P);
625static INLINE int is_one_of (char *s, int c); 669ecb_inline int is_one_of (const char *s, int c);
626static int alloc_cellseg (SCHEME_P_ int n); 670static int alloc_cellseg (SCHEME_P);
627static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); 671ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
628static void finalize_cell (SCHEME_P_ pointer a); 672static void finalize_cell (SCHEME_P_ pointer a);
629static int count_consecutive_cells (pointer x, int needed); 673static int count_consecutive_cells (pointer x, int needed);
630static 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);
631static pointer mk_number (SCHEME_P_ const num n); 675static pointer mk_number (SCHEME_P_ const num n);
632static 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);
647static void mark (pointer a); 691static void mark (pointer a);
648static void gc (SCHEME_P_ pointer a, pointer b); 692static void gc (SCHEME_P_ pointer a, pointer b);
649static int basic_inchar (port *pt); 693static int basic_inchar (port *pt);
650static int inchar (SCHEME_P); 694static int inchar (SCHEME_P);
651static void backchar (SCHEME_P_ int c); 695static void backchar (SCHEME_P_ int c);
652static char *readstr_upto (SCHEME_P_ char *delim); 696static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
653static pointer readstrexp (SCHEME_P); 697static pointer readstrexp (SCHEME_P_ char delim);
654static INLINE int skipspace (SCHEME_P); 698ecb_inline int skipspace (SCHEME_P);
655static int token (SCHEME_P); 699static int token (SCHEME_P);
656static void printslashstring (SCHEME_P_ char *s, int len); 700static void printslashstring (SCHEME_P_ char *s, int len);
657static 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);
658static void printatom (SCHEME_P_ pointer l, int f); 702static void printatom (SCHEME_P_ pointer l, int f);
659static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 703static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
663static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); 707static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list);
664static pointer revappend (SCHEME_P_ pointer a, pointer b); 708static pointer revappend (SCHEME_P_ pointer a, pointer b);
665static pointer ss_get_cont (SCHEME_P); 709static pointer ss_get_cont (SCHEME_P);
666static void ss_set_cont (SCHEME_P_ pointer cont); 710static void ss_set_cont (SCHEME_P_ pointer cont);
667static void dump_stack_mark (SCHEME_P); 711static void dump_stack_mark (SCHEME_P);
668static 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);
669static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); 714static int opexe_2 (SCHEME_P_ enum scheme_opcodes op);
670static pointer opexe_r (SCHEME_P_ enum scheme_opcodes op);
671static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); 715static int opexe_3 (SCHEME_P_ enum scheme_opcodes op);
672static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); 716static int opexe_4 (SCHEME_P_ enum scheme_opcodes op);
673static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); 717static int opexe_5 (SCHEME_P_ enum scheme_opcodes op);
674static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); 718static int opexe_6 (SCHEME_P_ enum scheme_opcodes op);
675static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 719static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
676static void assign_syntax (SCHEME_P_ const char *name); 720static void assign_syntax (SCHEME_P_ const char *name);
677static int syntaxnum (pointer p); 721static int syntaxnum (pointer p);
678static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 722static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
679 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
680static num 751static num
681num_op (enum num_op op, num a, num b) 752num_op (enum num_op op, num a, num b)
682{ 753{
683 num ret; 754 num ret;
684 755
685 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));
686 757
687 if (num_is_fixnum (ret)) 758 if (num_is_fixnum (ret))
688 { 759 {
689 IVALUE av = num_get_ivalue (a);
690 IVALUE bv = num_get_ivalue (b);
691
692 switch (op) 760 switch (op)
693 { 761 {
694 case NUM_ADD: av += bv; break; 762 case NUM_ADD: a.ivalue += b.ivalue; break;
695 case NUM_SUB: av -= bv; break; 763 case NUM_SUB: a.ivalue -= b.ivalue; break;
696 case NUM_MUL: av *= bv; break; 764 case NUM_MUL: a.ivalue *= b.ivalue; break;
697 case NUM_INTDIV: av /= bv; break; 765 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
698 } 766 }
699 767
700 num_set_ivalue (ret, av); 768 num_set_ivalue (ret, a.ivalue);
701 } 769 }
770#if USE_REAL
702 else 771 else
703 { 772 {
704 RVALUE av = num_get_rvalue (a);
705 RVALUE bv = num_get_rvalue (b);
706
707 switch (op) 773 switch (op)
708 { 774 {
709 case NUM_ADD: av += bv; break; 775 case NUM_ADD: a.rvalue += b.rvalue; break;
710 case NUM_SUB: av -= bv; break; 776 case NUM_SUB: a.rvalue -= b.rvalue; break;
711 case NUM_MUL: av *= bv; break; 777 case NUM_MUL: a.rvalue *= b.rvalue; break;
712 case NUM_INTDIV: av /= bv; break; 778 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
713 } 779 }
714 780
715 num_set_rvalue (ret, av); 781 num_set_rvalue (ret, a.rvalue);
716 } 782 }
783#endif
717 784
718 return ret; 785 return ret;
719} 786}
720 787
721static num 788static num
722num_div (num a, num b) 789num_div (num a, num b)
723{ 790{
724 num ret; 791 num ret;
725 792
726 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);
727 794
728 if (num_is_fixnum (ret)) 795 if (num_is_fixnum (ret))
729 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 796 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
730 else 797 else
731 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 798 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
732 799
733 return ret; 800 return ret;
734} 801}
735 802
736static num 803static num
738{ 805{
739 num ret; 806 num ret;
740 long e1, e2, res; 807 long e1, e2, res;
741 808
742 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));
743 e1 = num_get_ivalue (a); 810 e1 = num_ivalue (a);
744 e2 = num_get_ivalue (b); 811 e2 = num_ivalue (b);
745 res = e1 % e2; 812 res = e1 % e2;
746 813
747 /* remainder should have same sign as second operand */ 814 /* remainder should have same sign as second operand */
748 if (res > 0) 815 if (res > 0)
749 { 816 {
765{ 832{
766 num ret; 833 num ret;
767 long e1, e2, res; 834 long e1, e2, res;
768 835
769 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));
770 e1 = num_get_ivalue (a); 837 e1 = num_ivalue (a);
771 e2 = num_get_ivalue (b); 838 e2 = num_ivalue (b);
772 res = e1 % e2; 839 res = e1 % e2;
773 840
774 /* modulo should have same sign as second operand */ 841 /* modulo should have same sign as second operand */
775 if (res * e2 < 0) 842 if (res * e2 < 0)
776 res += e2; 843 res += e2;
777 844
778 num_set_ivalue (ret, res); 845 num_set_ivalue (ret, res);
779 return ret; 846 return ret;
780} 847}
781 848
782/* this completely disrespects NaNs */ 849/* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */
783static int 850static int
784num_cmp (num a, num b) 851num_cmp (num a, num b)
785{ 852{
786 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 853 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
787 int ret; 854 int ret;
788 855
789 if (is_fixnum) 856 if (is_fixnum)
790 { 857 {
791 IVALUE av = num_get_ivalue (a); 858 IVALUE av = num_ivalue (a);
792 IVALUE bv = num_get_ivalue (b); 859 IVALUE bv = num_ivalue (b);
793 860
794 ret = av == bv ? 0 : av < bv ? -1 : +1; 861 ret = av == bv ? 0 : av < bv ? -1 : +1;
795 } 862 }
796 else 863 else
797 { 864 {
798 RVALUE av = num_get_rvalue (a); 865 RVALUE av = num_rvalue (a);
799 RVALUE bv = num_get_rvalue (b); 866 RVALUE bv = num_rvalue (b);
800 867
801 ret = av == bv ? 0 : av < bv ? -1 : +1; 868 ret = av == bv ? 0 : av < bv ? -1 : +1;
802 } 869 }
803 870
804 return ret; 871 return ret;
805} 872}
806
807#if USE_MATH
808
809/* Round to nearest. Round to even if midway */
810static double
811round_per_R5RS (double x)
812{
813 double fl = floor (x);
814 double ce = ceil (x);
815 double dfl = x - fl;
816 double dce = ce - x;
817
818 if (dfl > dce)
819 return ce;
820 else if (dfl < dce)
821 return fl;
822 else
823 {
824 if (fmod (fl, 2.0) == 0.0) /* I imagine this holds */
825 return fl;
826 else
827 return ce;
828 }
829}
830#endif
831 873
832static int 874static int
833is_zero_rvalue (RVALUE x) 875is_zero_rvalue (RVALUE x)
834{ 876{
877 return x == 0;
878#if 0
835#if USE_REAL 879#if USE_REAL
836 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. */
837#else 881#else
838 return x == 0; 882 return x == 0;
839#endif 883#endif
884#endif
840} 885}
841 886
842/* allocate new cell segment */ 887/* allocate new cell segment */
843static int 888static int
844alloc_cellseg (SCHEME_P_ int n) 889alloc_cellseg (SCHEME_P)
845{ 890{
846 pointer newp; 891 struct cell *newp;
847 pointer last; 892 struct cell *last;
848 pointer p; 893 struct cell *p;
849 char *cp; 894 char *cp;
850 long i; 895 long i;
851 int k; 896 int k;
852 897
853 static int segsize = CELL_SEGSIZE >> 1; 898 static int segsize = CELL_SEGSIZE >> 1;
854 segsize <<= 1; 899 segsize <<= 1;
855 900
856 for (k = 0; k < n; k++)
857 {
858 if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1)
859 return k;
860
861 cp = malloc (segsize * sizeof (struct cell)); 901 cp = malloc (segsize * sizeof (struct cell));
862 902
863 if (!cp && USE_ERROR_CHECKING) 903 if (!cp && USE_ERROR_CHECKING)
864 return k; 904 return k;
865 905
866 i = ++SCHEME_V->last_cell_seg; 906 i = ++SCHEME_V->last_cell_seg;
867 SCHEME_V->alloc_seg[i] = cp; 907 SCHEME_V->alloc_seg[i] = cp;
868 908
869 /* insert new segment in address order */ 909 newp = (struct cell *)cp;
870 newp = (pointer)cp;
871 SCHEME_V->cell_seg[i] = newp; 910 SCHEME_V->cell_seg[i] = newp;
872 SCHEME_V->cell_segsize[i] = segsize; 911 SCHEME_V->cell_segsize[i] = segsize;
873
874 //TODO: insert, not swap
875 while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i])
876 {
877 p = SCHEME_V->cell_seg[i];
878 SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1];
879 SCHEME_V->cell_seg[i - 1] = p;
880
881 k = SCHEME_V->cell_segsize[i];
882 SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1];
883 SCHEME_V->cell_segsize[i - 1] = k;
884
885 --i;
886 }
887
888 SCHEME_V->fcells += segsize; 912 SCHEME_V->fcells += segsize;
889 last = newp + segsize - 1; 913 last = newp + segsize - 1;
890 914
891 for (p = newp; p <= last; p++) 915 for (p = newp; p <= last; p++)
892 { 916 {
917 pointer cp = POINTER (p);
893 set_typeflag (p, T_FREE); 918 set_typeflag (cp, T_PAIR);
894 set_car (p, NIL); 919 set_car (cp, NIL);
895 set_cdr (p, p + 1); 920 set_cdr (cp, POINTER (p + 1));
896 } 921 }
897 922
898 /* insert new cells in address order on free list */
899 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell)
900 {
901 set_cdr (last, SCHEME_V->free_cell); 923 set_cdr (POINTER (last), SCHEME_V->free_cell);
902 SCHEME_V->free_cell = newp; 924 SCHEME_V->free_cell = POINTER (newp);
903 }
904 else
905 {
906 p = SCHEME_V->free_cell;
907 925
908 while (cdr (p) != NIL && newp > cdr (p))
909 p = cdr (p);
910
911 set_cdr (last, cdr (p));
912 set_cdr (p, newp);
913 }
914 }
915
916 return n; 926 return 1;
917} 927}
918 928
919/* get new cell. parameter a, b is marked by gc. */ 929/* get new cell. parameter a, b is marked by gc. */
920static INLINE pointer 930ecb_inline pointer
921get_cell_x (SCHEME_P_ pointer a, pointer b) 931get_cell_x (SCHEME_P_ pointer a, pointer b)
922{ 932{
923 if (ecb_expect_false (SCHEME_V->free_cell == NIL)) 933 if (ecb_expect_false (SCHEME_V->free_cell == NIL))
924 { 934 {
925 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 935 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
926 return S_SINK; 936 return S_SINK;
927 937
928 if (SCHEME_V->free_cell == NIL) 938 if (SCHEME_V->free_cell == NIL)
929 { 939 {
930 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;
931 941
932 gc (SCHEME_A_ a, b); 942 gc (SCHEME_A_ a, b);
933 943
934 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)
935 { 945 {
936 /* 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 */
937 if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) 947 if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL)
938 { 948 {
939#if USE_ERROR_CHECKING 949#if USE_ERROR_CHECKING
940 SCHEME_V->no_memory = 1; 950 SCHEME_V->no_memory = 1;
941 return S_SINK; 951 return S_SINK;
942#endif 952#endif
954 } 964 }
955} 965}
956 966
957/* To retain recent allocs before interpreter knows about them - 967/* To retain recent allocs before interpreter knows about them -
958 Tehom */ 968 Tehom */
959
960static void 969static void
961push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 970push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
962{ 971{
963 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 972 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
964 973
986} 995}
987 996
988static pointer 997static pointer
989get_vector_object (SCHEME_P_ uint32_t len, pointer init) 998get_vector_object (SCHEME_P_ uint32_t len, pointer init)
990{ 999{
991 pointer v = get_cell_x (SCHEME_A_ 0, 0); 1000 pointer v = get_cell_x (SCHEME_A_ NIL, NIL);
992 pointer *e = malloc (len * sizeof (pointer)); 1001 pointer *e = malloc (len * sizeof (pointer));
993 1002
994 if (!e && USE_ERROR_CHECKING) 1003 if (!e && USE_ERROR_CHECKING)
995 return S_SINK; 1004 return S_SINK;
996 1005
997 /* Record it as a vector so that gc understands it. */ 1006 /* Record it as a vector so that gc understands it. */
998 set_typeflag (v, T_VECTOR | T_ATOM); 1007 set_typeflag (v, T_VECTOR | T_ATOM);
999 1008
1000 v->object.vector.vvalue = e; 1009 CELL(v)->object.vector.vvalue = e;
1001 v->object.vector.length = len; 1010 CELL(v)->object.vector.length = len;
1002 fill_vector (v, init); 1011 fill_vector (v, 0, init);
1003 push_recent_alloc (SCHEME_A_ v, NIL); 1012 push_recent_alloc (SCHEME_A_ v, NIL);
1004 1013
1005 return v; 1014 return v;
1006} 1015}
1007 1016
1008static INLINE void 1017ecb_inline void
1009ok_to_freely_gc (SCHEME_P) 1018ok_to_freely_gc (SCHEME_P)
1010{ 1019{
1011 set_car (S_SINK, NIL); 1020 set_car (S_SINK, NIL);
1012} 1021}
1013 1022
1015static void 1024static void
1016check_cell_alloced (pointer p, int expect_alloced) 1025check_cell_alloced (pointer p, int expect_alloced)
1017{ 1026{
1018 /* 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. */
1019 if (typeflag (p) & !expect_alloced) 1028 if (typeflag (p) & !expect_alloced)
1020 xwrstr ("Cell is already allocated!\n"); 1029 putstr (SCHEME_A_ "Cell is already allocated!\n");
1021 1030
1022 if (!(typeflag (p)) & expect_alloced) 1031 if (!(typeflag (p)) & expect_alloced)
1023 xwrstr ("Cell is not allocated!\n"); 1032 putstr (SCHEME_A_ "Cell is not allocated!\n");
1024} 1033}
1025 1034
1026static void 1035static void
1027check_range_alloced (pointer p, int n, int expect_alloced) 1036check_range_alloced (pointer p, int n, int expect_alloced)
1028{ 1037{
1050 set_cdr (x, b); 1059 set_cdr (x, b);
1051 1060
1052 return x; 1061 return x;
1053} 1062}
1054 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
1055/* ========== oblist implementation ========== */ 1073/* ========== oblist implementation ========== */
1056 1074
1057#ifndef USE_OBJECT_LIST 1075#ifndef USE_OBJECT_LIST
1058 1076
1077static int
1059static 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}
1060 1088
1061static pointer 1089static pointer
1062oblist_initial_value (SCHEME_P) 1090oblist_initial_value (SCHEME_P)
1063{ 1091{
1064 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1092 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1066 1094
1067/* returns the new symbol */ 1095/* returns the new symbol */
1068static pointer 1096static pointer
1069oblist_add_by_name (SCHEME_P_ const char *name) 1097oblist_add_by_name (SCHEME_P_ const char *name)
1070{ 1098{
1071 int location; 1099 pointer x = generate_symbol (SCHEME_A_ name);
1072
1073 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1074 set_typeflag (x, T_SYMBOL);
1075 setimmutable (car (x));
1076
1077 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1100 int location = hash_fn (name, veclength (SCHEME_V->oblist));
1078 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); 1101 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1079 return x; 1102 return x;
1080} 1103}
1081 1104
1082static INLINE pointer 1105ecb_inline pointer
1083oblist_find_by_name (SCHEME_P_ const char *name) 1106oblist_find_by_name (SCHEME_P_ const char *name)
1084{ 1107{
1085 int location; 1108 int location;
1086 pointer x; 1109 pointer x;
1087 char *s; 1110 char *s;
1088 1111
1089 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1112 location = hash_fn (name, veclength (SCHEME_V->oblist));
1090 1113
1091 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))
1092 { 1115 {
1093 s = symname (car (x)); 1116 s = symname (car (x));
1094 1117
1095 /* case-insensitive, per R5RS section 2 */ 1118 /* case-insensitive, per R5RS section 2 */
1096 if (stricmp (name, s) == 0) 1119 if (stricmp (name, s) == 0)
1106 int i; 1129 int i;
1107 pointer x; 1130 pointer x;
1108 pointer ob_list = NIL; 1131 pointer ob_list = NIL;
1109 1132
1110 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1133 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1111 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))
1112 ob_list = cons (x, ob_list); 1135 ob_list = cons (x, ob_list);
1113 1136
1114 return ob_list; 1137 return ob_list;
1115} 1138}
1116 1139
1120oblist_initial_value (SCHEME_P) 1143oblist_initial_value (SCHEME_P)
1121{ 1144{
1122 return NIL; 1145 return NIL;
1123} 1146}
1124 1147
1125static INLINE pointer 1148ecb_inline pointer
1126oblist_find_by_name (SCHEME_P_ const char *name) 1149oblist_find_by_name (SCHEME_P_ const char *name)
1127{ 1150{
1128 pointer x; 1151 pointer x;
1129 char *s; 1152 char *s;
1130 1153
1142 1165
1143/* returns the new symbol */ 1166/* returns the new symbol */
1144static pointer 1167static pointer
1145oblist_add_by_name (SCHEME_P_ const char *name) 1168oblist_add_by_name (SCHEME_P_ const char *name)
1146{ 1169{
1147 pointer x; 1170 pointer x = generate_symbol (SCHEME_A_ name);
1148
1149 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1150 set_typeflag (x, T_SYMBOL);
1151 setimmutable (car (x));
1152 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1171 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1153 return x; 1172 return x;
1154} 1173}
1155 1174
1156static pointer 1175static pointer
1166mk_port (SCHEME_P_ port *p) 1185mk_port (SCHEME_P_ port *p)
1167{ 1186{
1168 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1187 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1169 1188
1170 set_typeflag (x, T_PORT | T_ATOM); 1189 set_typeflag (x, T_PORT | T_ATOM);
1171 x->object.port = p; 1190 set_port (x, p);
1172 1191
1173 return x; 1192 return x;
1174} 1193}
1175#endif 1194#endif
1176 1195
1177pointer 1196pointer
1178mk_foreign_func (SCHEME_P_ foreign_func f) 1197mk_foreign_func (SCHEME_P_ foreign_func f)
1179{ 1198{
1180 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1199 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1181 1200
1182 set_typeflag (x, (T_FOREIGN | T_ATOM)); 1201 set_typeflag (x, T_FOREIGN | T_ATOM);
1183 x->object.ff = f; 1202 CELL(x)->object.ff = f;
1184 1203
1185 return x; 1204 return x;
1186} 1205}
1187 1206
1188INTERFACE pointer 1207INTERFACE pointer
1189mk_character (SCHEME_P_ int c) 1208mk_character (SCHEME_P_ int c)
1190{ 1209{
1191 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1210 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1192 1211
1193 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1212 set_typeflag (x, T_CHARACTER | T_ATOM);
1194 ivalue_unchecked (x) = c & 0xff; 1213 set_ivalue (x, c & 0xff);
1195 set_num_integer (x); 1214
1196 return x; 1215 return x;
1197} 1216}
1198 1217
1199/* get number atom (integer) */ 1218/* get number atom (integer) */
1200INTERFACE pointer 1219INTERFACE pointer
1201mk_integer (SCHEME_P_ long num) 1220mk_integer (SCHEME_P_ long n)
1202{ 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 {
1203 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1232 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1204 1233
1205 set_typeflag (x, (T_NUMBER | T_ATOM)); 1234 set_typeflag (x, T_INTEGER | T_ATOM);
1206 ivalue_unchecked (x) = num; 1235 setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */
1207 set_num_integer (x); 1236 set_ivalue (x, n);
1237
1238 *pp = x;
1239 }
1240
1208 return x; 1241 return *pp;
1209} 1242}
1210 1243
1211INTERFACE pointer 1244INTERFACE pointer
1212mk_real (SCHEME_P_ RVALUE n) 1245mk_real (SCHEME_P_ RVALUE n)
1213{ 1246{
1247#if USE_REAL
1214 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1248 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1215 1249
1216 set_typeflag (x, (T_NUMBER | T_ATOM)); 1250 set_typeflag (x, T_REAL | T_ATOM);
1217 rvalue_unchecked (x) = n; 1251 set_rvalue (x, n);
1218 set_num_real (x); 1252
1219 return x; 1253 return x;
1254#else
1255 return mk_integer (SCHEME_A_ n);
1256#endif
1220} 1257}
1221 1258
1222static pointer 1259static pointer
1223mk_number (SCHEME_P_ const num n) 1260mk_number (SCHEME_P_ const num n)
1224{ 1261{
1262#if USE_REAL
1225 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
1226 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1267 return mk_integer (SCHEME_A_ num_ivalue (n));
1227 else 1268#endif
1228 return mk_real (SCHEME_A_ num_get_rvalue (n));
1229} 1269}
1230 1270
1231/* allocate name to string area */ 1271/* allocate name to string area */
1232static char * 1272static char *
1233store_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)
1239 SCHEME_V->no_memory = 1; 1279 SCHEME_V->no_memory = 1;
1240 return SCHEME_V->strbuff; 1280 return SCHEME_V->strbuff;
1241 } 1281 }
1242 1282
1243 if (str) 1283 if (str)
1244 { 1284 memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */
1245 int l = strlen (str);
1246
1247 if (l > len_str)
1248 l = len_str;
1249
1250 memcpy (q, str, l);
1251 q[l] = 0;
1252 }
1253 else 1285 else
1254 {
1255 memset (q, fill, len_str); 1286 memset (q, fill, len_str);
1287
1256 q[len_str] = 0; 1288 q[len_str] = 0;
1257 }
1258 1289
1259 return q; 1290 return q;
1260} 1291}
1261 1292
1262INTERFACE pointer 1293INTERFACE pointer
1276 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1307 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1277 1308
1278 set_typeflag (x, T_STRING | T_ATOM); 1309 set_typeflag (x, T_STRING | T_ATOM);
1279 strvalue (x) = store_string (SCHEME_A_ len, str, 0); 1310 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1280 strlength (x) = len; 1311 strlength (x) = len;
1312
1281 return x; 1313 return x;
1282} 1314}
1283 1315
1284INTERFACE pointer 1316INTERFACE pointer
1285mk_string (SCHEME_P_ const char *str) 1317mk_string (SCHEME_P_ const char *str)
1292{ 1324{
1293 return get_vector_object (SCHEME_A_ len, NIL); 1325 return get_vector_object (SCHEME_A_ len, NIL);
1294} 1326}
1295 1327
1296INTERFACE void 1328INTERFACE void
1297fill_vector (pointer vec, pointer obj) 1329fill_vector (pointer vec, uint32_t start, pointer obj)
1298{ 1330{
1299 int i; 1331 int i;
1300 1332
1301 for (i = 0; i < vec->object.vector.length; i++) 1333 for (i = start; i < veclength (vec); i++)
1302 vecvalue (vec)[i] = obj; 1334 vecvalue (vec)[i] = obj;
1303} 1335}
1304 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
1305INTERFACE pointer 1346INTERFACE pointer
1306vector_elem (pointer vec, uint32_t ielem) 1347vector_get (pointer vec, uint32_t ielem)
1307{ 1348{
1308 return vecvalue(vec)[ielem]; 1349 return vecvalue(vec)[ielem];
1309} 1350}
1310 1351
1311INTERFACE void 1352INTERFACE void
1312set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1353vector_set (pointer vec, uint32_t ielem, pointer a)
1313{ 1354{
1314 vecvalue(vec)[ielem] = a; 1355 vecvalue(vec)[ielem] = a;
1315} 1356}
1316 1357
1317/* get new symbol */ 1358/* get new symbol */
1329 1370
1330INTERFACE pointer 1371INTERFACE pointer
1331gensym (SCHEME_P) 1372gensym (SCHEME_P)
1332{ 1373{
1333 pointer x; 1374 pointer x;
1334
1335 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1336 {
1337 char name[40] = "gensym-"; 1375 char name[40] = "gensym-";
1338 xnum (name + 7, SCHEME_V->gensym_cnt); 1376 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1339 1377
1340 /* first check oblist */ 1378 return generate_symbol (SCHEME_A_ name);
1341 x = oblist_find_by_name (SCHEME_A_ name); 1379}
1342 1380
1343 if (x == NIL) 1381static int
1344 { 1382is_gensym (SCHEME_P_ pointer x)
1345 x = oblist_add_by_name (SCHEME_A_ name); 1383{
1346 return x; 1384 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1347 }
1348 }
1349
1350 return NIL;
1351} 1385}
1352 1386
1353/* make symbol or number atom from string */ 1387/* make symbol or number atom from string */
1354static pointer 1388static pointer
1355mk_atom (SCHEME_P_ char *q) 1389mk_atom (SCHEME_P_ char *q)
1409 } 1443 }
1410 else if ((c == 'e') || (c == 'E')) 1444 else if ((c == 'e') || (c == 'E'))
1411 { 1445 {
1412 if (!has_fp_exp) 1446 if (!has_fp_exp)
1413 { 1447 {
1414 has_dec_point = 1; /* decimal point illegal 1448 has_dec_point = 1; /* decimal point illegal from now on */
1415 from now on */
1416 p++; 1449 p++;
1417 1450
1418 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1451 if ((*p == '-') || (*p == '+') || isdigit (*p))
1419 continue; 1452 continue;
1420 } 1453 }
1442 return S_F; 1475 return S_F;
1443 else if (*name == '\\') /* #\w (character) */ 1476 else if (*name == '\\') /* #\w (character) */
1444 { 1477 {
1445 int c; 1478 int c;
1446 1479
1480 // TODO: optimise
1447 if (stricmp (name + 1, "space") == 0) 1481 if (stricmp (name + 1, "space") == 0)
1448 c = ' '; 1482 c = ' ';
1449 else if (stricmp (name + 1, "newline") == 0) 1483 else if (stricmp (name + 1, "newline") == 0)
1450 c = '\n'; 1484 c = '\n';
1451 else if (stricmp (name + 1, "return") == 0) 1485 else if (stricmp (name + 1, "return") == 0)
1452 c = '\r'; 1486 c = '\r';
1453 else if (stricmp (name + 1, "tab") == 0) 1487 else if (stricmp (name + 1, "tab") == 0)
1454 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;
1455 else if (name[1] == 'x' && name[2] != 0) 1499 else if (name[1] == 'x' && name[2] != 0)
1456 { 1500 {
1457 long c1 = strtol (name + 2, 0, 16); 1501 long c1 = strtol (name + 2, 0, 16);
1458 1502
1459 if (0 <= c1 && c1 <= UCHAR_MAX) 1503 if (0 <= c1 && c1 <= UCHAR_MAX)
1508 1552
1509 if (ecb_expect_false (is_vector (p))) 1553 if (ecb_expect_false (is_vector (p)))
1510 { 1554 {
1511 int i; 1555 int i;
1512 1556
1513 for (i = 0; i < p->object.vector.length; i++) 1557 for (i = 0; i < veclength (p); i++)
1514 mark (vecvalue (p)[i]); 1558 mark (vecvalue (p)[i]);
1515 } 1559 }
1516 1560
1517 if (is_atom (p)) 1561 if (is_atom (p))
1518 goto E6; 1562 goto E6;
1565 1609
1566/* garbage collection. parameter a, b is marked. */ 1610/* garbage collection. parameter a, b is marked. */
1567static void 1611static void
1568gc (SCHEME_P_ pointer a, pointer b) 1612gc (SCHEME_P_ pointer a, pointer b)
1569{ 1613{
1570 pointer p;
1571 int i; 1614 int i;
1572 1615
1573 if (SCHEME_V->gc_verbose) 1616 if (SCHEME_V->gc_verbose)
1574 putstr (SCHEME_A_ "gc..."); 1617 putstr (SCHEME_A_ "gc...");
1575 1618
1591 /* Mark recent objects the interpreter doesn't know about yet. */ 1634 /* Mark recent objects the interpreter doesn't know about yet. */
1592 mark (car (S_SINK)); 1635 mark (car (S_SINK));
1593 /* Mark any older stuff above nested C calls */ 1636 /* Mark any older stuff above nested C calls */
1594 mark (SCHEME_V->c_nest); 1637 mark (SCHEME_V->c_nest);
1595 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
1596 /* mark variables a, b */ 1646 /* mark variables a, b */
1597 mark (a); 1647 mark (a);
1598 mark (b); 1648 mark (b);
1599 1649
1600 /* garbage collect */ 1650 /* garbage collect */
1601 clrmark (NIL); 1651 clrmark (NIL);
1602 SCHEME_V->fcells = 0; 1652 SCHEME_V->fcells = 0;
1603 SCHEME_V->free_cell = NIL; 1653 SCHEME_V->free_cell = NIL;
1604 1654
1605 /* free-list is kept sorted by address so as to maintain consecutive 1655 if (SCHEME_V->gc_verbose)
1606 ranges, if possible, for use with vectors. Here we scan the cells 1656 putstr (SCHEME_A_ "freeing...");
1607 (which are also kept sorted by address) downwards to build the 1657
1608 free-list in sorted order. 1658 uint32_t total = 0;
1609 */ 1659
1660 /* Here we scan the cells to build the free-list. */
1610 for (i = SCHEME_V->last_cell_seg; i >= 0; i--) 1661 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1611 { 1662 {
1612 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];
1613 1666
1614 while (--p >= SCHEME_V->cell_seg[i]) 1667 for (p = SCHEME_V->cell_seg[i]; p < end; ++p)
1615 { 1668 {
1669 pointer c = POINTER (p);
1670
1616 if (is_mark (p)) 1671 if (is_mark (c))
1617 clrmark (p); 1672 clrmark (c);
1618 else 1673 else
1619 { 1674 {
1620 /* reclaim cell */ 1675 /* reclaim cell */
1621 if (typeflag (p) != T_FREE) 1676 if (typeflag (c) != T_PAIR)
1622 { 1677 {
1623 finalize_cell (SCHEME_A_ p); 1678 finalize_cell (SCHEME_A_ c);
1624 set_typeflag (p, T_FREE); 1679 set_typeflag (c, T_PAIR);
1625 set_car (p, NIL); 1680 set_car (c, NIL);
1626 } 1681 }
1627 1682
1628 ++SCHEME_V->fcells; 1683 ++SCHEME_V->fcells;
1629 set_cdr (p, SCHEME_V->free_cell); 1684 set_cdr (c, SCHEME_V->free_cell);
1630 SCHEME_V->free_cell = p; 1685 SCHEME_V->free_cell = c;
1631 } 1686 }
1632 } 1687 }
1633 } 1688 }
1634 1689
1635 if (SCHEME_V->gc_verbose) 1690 if (SCHEME_V->gc_verbose)
1636 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 }
1637} 1694}
1638 1695
1639static void 1696static void
1640finalize_cell (SCHEME_P_ pointer a) 1697finalize_cell (SCHEME_P_ pointer a)
1641{ 1698{
1642 /* TODO, fast bitmap check? */ 1699 /* TODO, fast bitmap check? */
1643 if (is_string (a)) 1700 if (is_string (a) || is_symbol (a))
1644 free (strvalue (a)); 1701 free (strvalue (a));
1645 else if (is_vector (a)) 1702 else if (is_vector (a))
1646 free (vecvalue (a)); 1703 free (vecvalue (a));
1647#if USE_PORTS 1704#if USE_PORTS
1648 else if (is_port (a)) 1705 else if (is_port (a))
1649 { 1706 {
1650 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)
1651 port_close (SCHEME_A_ a, port_input | port_output); 1708 port_close (SCHEME_A_ a, port_input | port_output);
1652 1709
1653 free (a->object.port); 1710 free (port (a));
1654 } 1711 }
1655#endif 1712#endif
1656} 1713}
1657 1714
1658/* ========== Routines for Reading ========== */ 1715/* ========== Routines for Reading ========== */
1674 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; 1731 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1;
1675 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;
1676 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;
1677 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;
1678 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; 1735 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0;
1679 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);
1680 1737
1681#if SHOW_ERROR_LINE 1738#if SHOW_ERROR_LINE
1682 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;
1683 1740
1684 if (fname) 1741 if (fname)
1701 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1758 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1702#if USE_PORTS 1759#if USE_PORTS
1703 port_close (SCHEME_A_ SCHEME_V->loadport, port_input); 1760 port_close (SCHEME_A_ SCHEME_V->loadport, port_input);
1704#endif 1761#endif
1705 SCHEME_V->file_i--; 1762 SCHEME_V->file_i--;
1706 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);
1707 } 1764 }
1708} 1765}
1709 1766
1710static int 1767static int
1711file_interactive (SCHEME_P) 1768file_interactive (SCHEME_P)
1712{ 1769{
1713#if USE_PORTS 1770#if USE_PORTS
1714 return SCHEME_V->file_i == 0 1771 return SCHEME_V->file_i == 0
1715 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1772 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1716 && (SCHEME_V->inport->object.port->kind & port_file); 1773 && (port (SCHEME_V->inport)->kind & port_file);
1717#else 1774#else
1718 return 0; 1775 return 0;
1719#endif 1776#endif
1720} 1777}
1721 1778
1855} 1912}
1856 1913
1857static void 1914static void
1858port_close (SCHEME_P_ pointer p, int flag) 1915port_close (SCHEME_P_ pointer p, int flag)
1859{ 1916{
1860 port *pt = p->object.port; 1917 port *pt = port (p);
1861 1918
1862 pt->kind &= ~flag; 1919 pt->kind &= ~flag;
1863 1920
1864 if ((pt->kind & (port_input | port_output)) == 0) 1921 if ((pt->kind & (port_input | port_output)) == 0)
1865 { 1922 {
1886/* get new character from input file */ 1943/* get new character from input file */
1887static int 1944static int
1888inchar (SCHEME_P) 1945inchar (SCHEME_P)
1889{ 1946{
1890 int c; 1947 int c;
1891 port *pt; 1948 port *pt = port (SCHEME_V->inport);
1892
1893 pt = SCHEME_V->inport->object.port;
1894 1949
1895 if (pt->kind & port_saw_EOF) 1950 if (pt->kind & port_saw_EOF)
1896 return EOF; 1951 return EOF;
1897 1952
1898 c = basic_inchar (pt); 1953 c = basic_inchar (pt);
1965 port *pt; 2020 port *pt;
1966 2021
1967 if (c == EOF) 2022 if (c == EOF)
1968 return; 2023 return;
1969 2024
1970 pt = SCHEME_V->inport->object.port; 2025 pt = port (SCHEME_V->inport);
1971 pt->unget = c; 2026 pt->unget = c;
1972#else 2027#else
1973 if (c == EOF) 2028 if (c == EOF)
1974 return; 2029 return;
1975 2030
2003 2058
2004INTERFACE void 2059INTERFACE void
2005putstr (SCHEME_P_ const char *s) 2060putstr (SCHEME_P_ const char *s)
2006{ 2061{
2007#if USE_PORTS 2062#if USE_PORTS
2008 port *pt = SCHEME_V->outport->object.port; 2063 port *pt = port (SCHEME_V->outport);
2009 2064
2010 if (pt->kind & port_file) 2065 if (pt->kind & port_file)
2011 write (pt->rep.stdio.file, s, strlen (s)); 2066 write (pt->rep.stdio.file, s, strlen (s));
2012 else 2067 else
2013 for (; *s; s++) 2068 for (; *s; s++)
2015 *pt->rep.string.curr++ = *s; 2070 *pt->rep.string.curr++ = *s;
2016 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))
2017 *pt->rep.string.curr++ = *s; 2072 *pt->rep.string.curr++ = *s;
2018 2073
2019#else 2074#else
2020 xwrstr (s); 2075 write (pt->rep.stdio.file, s, strlen (s));
2021#endif 2076#endif
2022} 2077}
2023 2078
2024static void 2079static void
2025putchars (SCHEME_P_ const char *s, int len) 2080putchars (SCHEME_P_ const char *s, int len)
2026{ 2081{
2027#if USE_PORTS 2082#if USE_PORTS
2028 port *pt = SCHEME_V->outport->object.port; 2083 port *pt = port (SCHEME_V->outport);
2029 2084
2030 if (pt->kind & port_file) 2085 if (pt->kind & port_file)
2031 write (pt->rep.stdio.file, s, len); 2086 write (pt->rep.stdio.file, s, len);
2032 else 2087 else
2033 { 2088 {
2047 2102
2048INTERFACE void 2103INTERFACE void
2049putcharacter (SCHEME_P_ int c) 2104putcharacter (SCHEME_P_ int c)
2050{ 2105{
2051#if USE_PORTS 2106#if USE_PORTS
2052 port *pt = SCHEME_V->outport->object.port; 2107 port *pt = port (SCHEME_V->outport);
2053 2108
2054 if (pt->kind & port_file) 2109 if (pt->kind & port_file)
2055 { 2110 {
2056 char cc = c; 2111 char cc = c;
2057 write (pt->rep.stdio.file, &cc, 1); 2112 write (pt->rep.stdio.file, &cc, 1);
2070#endif 2125#endif
2071} 2126}
2072 2127
2073/* read characters up to delimiter, but cater to character constants */ 2128/* read characters up to delimiter, but cater to character constants */
2074static char * 2129static char *
2075readstr_upto (SCHEME_P_ char *delim) 2130readstr_upto (SCHEME_P_ int skip, const char *delim)
2076{ 2131{
2077 char *p = SCHEME_V->strbuff; 2132 char *p = SCHEME_V->strbuff + skip;
2078 2133
2079 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))));
2080 2135
2081 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2136 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2082 *p = 0; 2137 *p = 0;
2089 return SCHEME_V->strbuff; 2144 return SCHEME_V->strbuff;
2090} 2145}
2091 2146
2092/* read string expression "xxx...xxx" */ 2147/* read string expression "xxx...xxx" */
2093static pointer 2148static pointer
2094readstrexp (SCHEME_P) 2149readstrexp (SCHEME_P_ char delim)
2095{ 2150{
2096 char *p = SCHEME_V->strbuff; 2151 char *p = SCHEME_V->strbuff;
2097 int c; 2152 int c;
2098 int c1 = 0; 2153 int c1 = 0;
2099 enum
2100 { 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;
2101 2155
2102 for (;;) 2156 for (;;)
2103 { 2157 {
2104 c = inchar (SCHEME_A); 2158 c = inchar (SCHEME_A);
2105 2159
2107 return S_F; 2161 return S_F;
2108 2162
2109 switch (state) 2163 switch (state)
2110 { 2164 {
2111 case st_ok: 2165 case st_ok:
2112 switch (c) 2166 if (ecb_expect_false (c == delim))
2113 {
2114 case '\\':
2115 state = st_bsl;
2116 break;
2117
2118 case '"':
2119 *p = 0;
2120 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); 2167 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff);
2121 2168
2122 default: 2169 if (ecb_expect_false (c == '\\'))
2170 state = st_bsl;
2171 else
2123 *p++ = c; 2172 *p++ = c;
2124 break;
2125 }
2126 2173
2127 break; 2174 break;
2128 2175
2129 case st_bsl: 2176 case st_bsl:
2130 switch (c) 2177 switch (c)
2139 case '7': 2186 case '7':
2140 state = st_oct1; 2187 state = st_oct1;
2141 c1 = c - '0'; 2188 c1 = c - '0';
2142 break; 2189 break;
2143 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
2144 case 'x': 2199 case 'x':
2145 case 'X': 2200 case 'X':
2146 state = st_x1; 2201 state = st_x1;
2147 c1 = 0; 2202 c1 = 0;
2148 break; 2203 break;
2149 2204
2150 case 'n':
2151 *p++ = '\n';
2152 state = st_ok;
2153 break;
2154
2155 case 't':
2156 *p++ = '\t';
2157 state = st_ok;
2158 break;
2159
2160 case 'r':
2161 *p++ = '\r';
2162 state = st_ok;
2163 break;
2164
2165 case '"':
2166 *p++ = '"';
2167 state = st_ok;
2168 break;
2169
2170 default: 2205 default:
2171 *p++ = c; 2206 *p++ = c;
2172 state = st_ok; 2207 state = st_ok;
2173 break; 2208 break;
2174 } 2209 }
2175 2210
2176 break; 2211 break;
2177 2212
2178 case st_x1: 2213 case st_x1:
2179 case st_x2: 2214 case st_x2:
2180 c = toupper (c); 2215 c = tolower (c);
2181 2216
2182 if (c >= '0' && c <= 'F') 2217 if (c >= '0' && c <= '9')
2183 {
2184 if (c <= '9')
2185 c1 = (c1 << 4) + c - '0'; 2218 c1 = (c1 << 4) + c - '0';
2186 else 2219 else if (c >= 'a' && c <= 'f')
2187 c1 = (c1 << 4) + c - 'A' + 10; 2220 c1 = (c1 << 4) + c - 'a' + 10;
2188
2189 if (state == st_x1)
2190 state = st_x2;
2191 else
2192 {
2193 *p++ = c1;
2194 state = st_ok;
2195 }
2196 }
2197 else 2221 else
2198 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 }
2199 2231
2200 break; 2232 break;
2201 2233
2202 case st_oct1: 2234 case st_oct1:
2203 case st_oct2: 2235 case st_oct2:
2207 backchar (SCHEME_A_ c); 2239 backchar (SCHEME_A_ c);
2208 state = st_ok; 2240 state = st_ok;
2209 } 2241 }
2210 else 2242 else
2211 { 2243 {
2212 if (state == st_oct2 && c1 >= 32) 2244 if (state == st_oct2 && c1 >= ' ')
2213 return S_F; 2245 return S_F;
2214 2246
2215 c1 = (c1 << 3) + (c - '0'); 2247 c1 = (c1 << 3) + (c - '0');
2216 2248
2217 if (state == st_oct1) 2249 if (state == st_oct1)
2222 state = st_ok; 2254 state = st_ok;
2223 } 2255 }
2224 } 2256 }
2225 2257
2226 break; 2258 break;
2227
2228 } 2259 }
2229 } 2260 }
2230} 2261}
2231 2262
2232/* check c is in chars */ 2263/* check c is in chars */
2233static INLINE int 2264ecb_inline int
2234is_one_of (char *s, int c) 2265is_one_of (const char *s, int c)
2235{ 2266{
2236 if (c == EOF)
2237 return 1;
2238
2239 return !!strchr (s, c); 2267 return c == EOF || !!strchr (s, c);
2240} 2268}
2241 2269
2242/* skip white characters */ 2270/* skip white characters */
2243static INLINE int 2271ecb_inline int
2244skipspace (SCHEME_P) 2272skipspace (SCHEME_P)
2245{ 2273{
2246 int c, curr_line = 0; 2274 int c, curr_line = 0;
2247 2275
2248 do 2276 do
2249 { 2277 {
2250 c = inchar (SCHEME_A); 2278 c = inchar (SCHEME_A);
2279
2251#if SHOW_ERROR_LINE 2280#if SHOW_ERROR_LINE
2252 if (c == '\n') 2281 if (ecb_expect_false (c == '\n'))
2253 curr_line++; 2282 curr_line++;
2254#endif 2283#endif
2284
2285 if (ecb_expect_false (c == EOF))
2286 return c;
2255 } 2287 }
2256 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2288 while (is_one_of (WHITESPACE, c));
2257 2289
2258 /* record it */ 2290 /* record it */
2259#if SHOW_ERROR_LINE 2291#if SHOW_ERROR_LINE
2260 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)
2261 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;
2262#endif 2294#endif
2263 2295
2264 if (c != EOF)
2265 {
2266 backchar (SCHEME_A_ c); 2296 backchar (SCHEME_A_ c);
2267 return 1; 2297 return 1;
2268 }
2269 else
2270 return EOF;
2271} 2298}
2272 2299
2273/* get token */ 2300/* get token */
2274static int 2301static int
2275token (SCHEME_P) 2302token (SCHEME_P)
2291 return TOK_RPAREN; 2318 return TOK_RPAREN;
2292 2319
2293 case '.': 2320 case '.':
2294 c = inchar (SCHEME_A); 2321 c = inchar (SCHEME_A);
2295 2322
2296 if (is_one_of (" \n\t", c)) 2323 if (is_one_of (WHITESPACE, c))
2297 return TOK_DOT; 2324 return TOK_DOT;
2298 else 2325 else
2299 { 2326 {
2300 //TODO: ungetc twice in a row is not supported in C
2301 backchar (SCHEME_A_ c); 2327 backchar (SCHEME_A_ c);
2302 backchar (SCHEME_A_ '.');
2303 return TOK_ATOM; 2328 return TOK_DOTATOM;
2304 } 2329 }
2330
2331 case '|':
2332 return TOK_STRATOM;
2305 2333
2306 case '\'': 2334 case '\'':
2307 return TOK_QUOTE; 2335 return TOK_QUOTE;
2308 2336
2309 case ';': 2337 case ';':
2441 } 2469 }
2442 2470
2443 putcharacter (SCHEME_A_ '"'); 2471 putcharacter (SCHEME_A_ '"');
2444} 2472}
2445 2473
2446
2447/* print atoms */ 2474/* print atoms */
2448static void 2475static void
2449printatom (SCHEME_P_ pointer l, int f) 2476printatom (SCHEME_P_ pointer l, int f)
2450{ 2477{
2451 char *p; 2478 char *p;
2452 int len; 2479 int len;
2453 2480
2454 atom2str (SCHEME_A_ l, f, &p, &len); 2481 atom2str (SCHEME_A_ l, f, &p, &len);
2455 putchars (SCHEME_A_ p, len); 2482 putchars (SCHEME_A_ p, len);
2456} 2483}
2457
2458 2484
2459/* Uses internal buffer unless string pointer is already available */ 2485/* Uses internal buffer unless string pointer is already available */
2460static void 2486static void
2461atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2487atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2462{ 2488{
2476 { 2502 {
2477 p = SCHEME_V->strbuff; 2503 p = SCHEME_V->strbuff;
2478 2504
2479 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 */
2480 { 2506 {
2481 if (num_is_integer (l)) 2507 if (is_integer (l))
2482 xnum (p, ivalue_unchecked (l)); 2508 xnum (p, ivalue_unchecked (l));
2483#if USE_REAL 2509#if USE_REAL
2484 else 2510 else
2485 { 2511 {
2486 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2512 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2623#endif 2649#endif
2624 } 2650 }
2625 else if (is_continuation (l)) 2651 else if (is_continuation (l))
2626 p = "#<CONTINUATION>"; 2652 p = "#<CONTINUATION>";
2627 else 2653 else
2654 {
2655#if USE_PRINTF
2656 p = SCHEME_V->strbuff;
2657 snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l));
2658#else
2628 p = "#<ERROR>"; 2659 p = "#<ERROR>";
2660#endif
2661 }
2629 2662
2630 *pp = p; 2663 *pp = p;
2631 *plen = strlen (p); 2664 *plen = strlen (p);
2632} 2665}
2633 2666
2741 return 0; 2774 return 0;
2742 } 2775 }
2743 else if (is_number (a)) 2776 else if (is_number (a))
2744 { 2777 {
2745 if (is_number (b)) 2778 if (is_number (b))
2746 if (num_is_integer (a) == num_is_integer (b))
2747 return num_cmp (nvalue (a), nvalue (b)) == 0; 2779 return num_cmp (nvalue (a), nvalue (b)) == 0;
2748 2780
2749 return 0; 2781 return 0;
2750 } 2782 }
2751 else if (is_character (a)) 2783 else if (is_character (a))
2752 { 2784 {
2778/* () is #t in R5RS */ 2810/* () is #t in R5RS */
2779#define is_true(p) ((p) != S_F) 2811#define is_true(p) ((p) != S_F)
2780#define is_false(p) ((p) == S_F) 2812#define is_false(p) ((p) == S_F)
2781 2813
2782/* ========== Environment implementation ========== */ 2814/* ========== Environment implementation ========== */
2783
2784#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST)
2785
2786static int
2787hash_fn (const char *key, int table_size)
2788{
2789 const unsigned char *p = key;
2790 uint32_t hash = 2166136261;
2791
2792 while (*p)
2793 hash = (hash ^ *p++) * 16777619;
2794
2795 return hash % table_size;
2796}
2797#endif
2798 2815
2799#ifndef USE_ALIST_ENV 2816#ifndef USE_ALIST_ENV
2800 2817
2801/* 2818/*
2802 * In this implementation, each frame of the environment may be 2819 * In this implementation, each frame of the environment may be
2819 2836
2820 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2837 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2821 setenvironment (SCHEME_V->envir); 2838 setenvironment (SCHEME_V->envir);
2822} 2839}
2823 2840
2824static 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
2825new_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)
2826{ 2858{
2827 pointer slot = immutable_cons (variable, value); 2859 pointer slot = immutable_cons (variable, value);
2828 2860
2829 if (is_vector (car (env))) 2861 if (is_vector (car (env)))
2830 { 2862 {
2831 int location = hash_fn (symname (variable), veclength (car (env))); 2863 int location = sym_hash (variable, veclength (car (env)));
2832
2833 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)));
2834 } 2865 }
2835 else 2866 else
2836 set_car (env, immutable_cons (slot, car (env))); 2867 set_car (env, immutable_cons (slot, car (env)));
2837} 2868}
2838 2869
2839static pointer 2870static pointer
2840find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2871find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2841{ 2872{
2842 pointer x, y; 2873 pointer x, y;
2843 int location;
2844 2874
2845 for (x = env; x != NIL; x = cdr (x)) 2875 for (x = env; x != NIL; x = cdr (x))
2846 { 2876 {
2847 if (is_vector (car (x))) 2877 if (is_vector (car (x)))
2848 { 2878 {
2849 location = hash_fn (symname (hdl), veclength (car (x))); 2879 int location = sym_hash (hdl, veclength (car (x)));
2850 y = vector_elem (car (x), location); 2880 y = vector_get (car (x), location);
2851 } 2881 }
2852 else 2882 else
2853 y = car (x); 2883 y = car (x);
2854 2884
2855 for (; y != NIL; y = cdr (y)) 2885 for (; y != NIL; y = cdr (y))
2856 if (caar (y) == hdl) 2886 if (caar (y) == hdl)
2857 break; 2887 break;
2858 2888
2859 if (y != NIL) 2889 if (y != NIL)
2890 return car (y);
2891
2892 if (!all)
2860 break; 2893 break;
2861
2862 if (!all)
2863 return NIL;
2864 } 2894 }
2865
2866 if (x != NIL)
2867 return car (y);
2868 2895
2869 return NIL; 2896 return NIL;
2870} 2897}
2871 2898
2872#else /* USE_ALIST_ENV */ 2899#else /* USE_ALIST_ENV */
2873 2900
2874static INLINE void 2901ecb_inline void
2875new_frame_in_env (SCHEME_P_ pointer old_env) 2902new_frame_in_env (SCHEME_P_ pointer old_env)
2876{ 2903{
2877 SCHEME_V->envir = immutable_cons (NIL, old_env); 2904 SCHEME_V->envir = immutable_cons (NIL, old_env);
2878 setenvironment (SCHEME_V->envir); 2905 setenvironment (SCHEME_V->envir);
2879} 2906}
2880 2907
2881static INLINE void 2908ecb_inline void
2882new_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)
2883{ 2910{
2884 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2911 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2885} 2912}
2886 2913
2894 for (y = car (x); y != NIL; y = cdr (y)) 2921 for (y = car (x); y != NIL; y = cdr (y))
2895 if (caar (y) == hdl) 2922 if (caar (y) == hdl)
2896 break; 2923 break;
2897 2924
2898 if (y != NIL) 2925 if (y != NIL)
2926 return car (y);
2899 break; 2927 break;
2900 2928
2901 if (!all) 2929 if (!all)
2902 return NIL; 2930 break;
2903 } 2931 }
2904
2905 if (x != NIL)
2906 return car (y);
2907 2932
2908 return NIL; 2933 return NIL;
2909} 2934}
2910 2935
2911#endif /* USE_ALIST_ENV else */ 2936#endif /* USE_ALIST_ENV else */
2912 2937
2913static INLINE void 2938ecb_inline void
2914new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2939new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2915{ 2940{
2941 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2916 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);
2917} 2943}
2918 2944
2919static INLINE void 2945ecb_inline void
2920set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2946set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2921{ 2947{
2922 set_cdr (slot, value); 2948 set_cdr (slot, value);
2923} 2949}
2924 2950
2925static INLINE pointer 2951ecb_inline pointer
2926slot_value_in_env (pointer slot) 2952slot_value_in_env (pointer slot)
2927{ 2953{
2928 return cdr (slot); 2954 return cdr (slot);
2929} 2955}
2930 2956
2931/* ========== Evaluation Cycle ========== */ 2957/* ========== Evaluation Cycle ========== */
2932 2958
2933static pointer 2959static int
2934xError_1 (SCHEME_P_ const char *s, pointer a) 2960xError_1 (SCHEME_P_ const char *s, pointer a)
2935{ 2961{
2936#if USE_ERROR_HOOK 2962#if USE_ERROR_HOOK
2937 pointer x; 2963 pointer x;
2938 pointer hdl = SCHEME_V->ERROR_HOOK; 2964 pointer hdl = SCHEME_V->ERROR_HOOK;
2973 code = cons (mk_string (SCHEME_A_ s), code); 2999 code = cons (mk_string (SCHEME_A_ s), code);
2974 setimmutable (car (code)); 3000 setimmutable (car (code));
2975 SCHEME_V->code = cons (slot_value_in_env (x), code); 3001 SCHEME_V->code = cons (slot_value_in_env (x), code);
2976 SCHEME_V->op = OP_EVAL; 3002 SCHEME_V->op = OP_EVAL;
2977 3003
2978 return S_T; 3004 return 0;
2979 } 3005 }
2980#endif 3006#endif
2981 3007
2982 if (a) 3008 if (a)
2983 SCHEME_V->args = cons (a, NIL); 3009 SCHEME_V->args = cons (a, NIL);
2985 SCHEME_V->args = NIL; 3011 SCHEME_V->args = NIL;
2986 3012
2987 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);
2988 setimmutable (car (SCHEME_V->args)); 3014 setimmutable (car (SCHEME_V->args));
2989 SCHEME_V->op = OP_ERR0; 3015 SCHEME_V->op = OP_ERR0;
3016
2990 return S_T; 3017 return 0;
2991} 3018}
2992 3019
2993#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)
2994#define Error_0(s) Error_1 (s, 0) 3021#define Error_0(s) Error_1 (s, 0)
2995 3022
2996/* Too small to turn into function */ 3023/* Too small to turn into function */
2997#define BEGIN do { 3024#define BEGIN do {
2998#define END } while (0) 3025#define END } while (0)
2999#define s_goto(a) BEGIN \ 3026#define s_goto(a) BEGIN \
3000 SCHEME_V->op = a; \ 3027 SCHEME_V->op = a; \
3001 return S_T; END 3028 return 0; END
3002 3029
3003#define s_return(a) return xs_return (SCHEME_A_ a) 3030#define s_return(a) return xs_return (SCHEME_A_ a)
3004 3031
3005#ifndef USE_SCHEME_STACK 3032#ifndef USE_SCHEME_STACK
3006 3033
3020{ 3047{
3021 int nframes = (uintptr_t)SCHEME_V->dump; 3048 int nframes = (uintptr_t)SCHEME_V->dump;
3022 struct dump_stack_frame *next_frame; 3049 struct dump_stack_frame *next_frame;
3023 3050
3024 /* enough room for the next frame? */ 3051 /* enough room for the next frame? */
3025 if (nframes >= SCHEME_V->dump_size) 3052 if (ecb_expect_false (nframes >= SCHEME_V->dump_size))
3026 { 3053 {
3027 SCHEME_V->dump_size += STACK_GROWTH; 3054 SCHEME_V->dump_size += STACK_GROWTH;
3028 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); 3055 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size);
3029 } 3056 }
3030 3057
3031 next_frame = SCHEME_V->dump_base + nframes; 3058 next_frame = SCHEME_V->dump_base + nframes;
3032 3059
3033 next_frame->op = op; 3060 next_frame->op = op;
3034 next_frame->args = args; 3061 next_frame->args = args;
3035 next_frame->envir = SCHEME_V->envir; 3062 next_frame->envir = SCHEME_V->envir;
3036 next_frame->code = code; 3063 next_frame->code = code;
3037 3064
3038 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3065 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3039} 3066}
3040 3067
3041static pointer 3068static int
3042xs_return (SCHEME_P_ pointer a) 3069xs_return (SCHEME_P_ pointer a)
3043{ 3070{
3044 int nframes = (uintptr_t)SCHEME_V->dump; 3071 int nframes = (uintptr_t)SCHEME_V->dump;
3045 struct dump_stack_frame *frame; 3072 struct dump_stack_frame *frame;
3046 3073
3047 SCHEME_V->value = a; 3074 SCHEME_V->value = a;
3048 3075
3049 if (nframes <= 0) 3076 if (nframes <= 0)
3050 return NIL; 3077 return -1;
3051 3078
3052 frame = &SCHEME_V->dump_base[--nframes]; 3079 frame = &SCHEME_V->dump_base[--nframes];
3053 SCHEME_V->op = frame->op; 3080 SCHEME_V->op = frame->op;
3054 SCHEME_V->args = frame->args; 3081 SCHEME_V->args = frame->args;
3055 SCHEME_V->envir = frame->envir; 3082 SCHEME_V->envir = frame->envir;
3056 SCHEME_V->code = frame->code; 3083 SCHEME_V->code = frame->code;
3057 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3084 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3058 3085
3059 return S_T; 3086 return 0;
3060} 3087}
3061 3088
3062static INLINE void 3089ecb_inline void
3063dump_stack_reset (SCHEME_P) 3090dump_stack_reset (SCHEME_P)
3064{ 3091{
3065 /* 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 */
3066 SCHEME_V->dump = (pointer)+0; 3093 SCHEME_V->dump = (pointer)+0;
3067} 3094}
3068 3095
3069static INLINE void 3096ecb_inline void
3070dump_stack_initialize (SCHEME_P) 3097dump_stack_initialize (SCHEME_P)
3071{ 3098{
3072 SCHEME_V->dump_size = 0; 3099 SCHEME_V->dump_size = 0;
3073 SCHEME_V->dump_base = 0; 3100 SCHEME_V->dump_base = 0;
3074 dump_stack_reset (SCHEME_A); 3101 dump_stack_reset (SCHEME_A);
3127 int i = 0; 3154 int i = 0;
3128 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3155 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3129 3156
3130 while (cont != NIL) 3157 while (cont != NIL)
3131 { 3158 {
3132 frame->op = ivalue (car (cont)); cont = cdr (cont); 3159 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3133 frame->args = car (cont) ; cont = cdr (cont); 3160 frame->args = car (cont) ; cont = cdr (cont);
3134 frame->envir = car (cont) ; cont = cdr (cont); 3161 frame->envir = car (cont) ; cont = cdr (cont);
3135 frame->code = car (cont) ; cont = cdr (cont); 3162 frame->code = car (cont) ; cont = cdr (cont);
3136 3163
3137 ++frame; 3164 ++frame;
3138 ++i; 3165 ++i;
3139 } 3166 }
3140 3167
3141 SCHEME_V->dump = (pointer)(uintptr_t)i; 3168 SCHEME_V->dump = (pointer)(uintptr_t)i;
3142} 3169}
3143 3170
3144#else 3171#else
3145 3172
3146static INLINE void 3173ecb_inline void
3147dump_stack_reset (SCHEME_P) 3174dump_stack_reset (SCHEME_P)
3148{ 3175{
3149 SCHEME_V->dump = NIL; 3176 SCHEME_V->dump = NIL;
3150} 3177}
3151 3178
3152static INLINE void 3179ecb_inline void
3153dump_stack_initialize (SCHEME_P) 3180dump_stack_initialize (SCHEME_P)
3154{ 3181{
3155 dump_stack_reset (SCHEME_A); 3182 dump_stack_reset (SCHEME_A);
3156} 3183}
3157 3184
3159dump_stack_free (SCHEME_P) 3186dump_stack_free (SCHEME_P)
3160{ 3187{
3161 SCHEME_V->dump = NIL; 3188 SCHEME_V->dump = NIL;
3162} 3189}
3163 3190
3164static pointer 3191static int
3165xs_return (SCHEME_P_ pointer a) 3192xs_return (SCHEME_P_ pointer a)
3166{ 3193{
3167 pointer dump = SCHEME_V->dump; 3194 pointer dump = SCHEME_V->dump;
3168 3195
3169 SCHEME_V->value = a; 3196 SCHEME_V->value = a;
3170 3197
3171 if (dump == NIL) 3198 if (dump == NIL)
3172 return NIL; 3199 return -1;
3173 3200
3174 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3201 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3175 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3202 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3176 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3203 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3177 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3204 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3178 3205
3179 SCHEME_V->dump = dump; 3206 SCHEME_V->dump = dump;
3180 3207
3181 return S_T; 3208 return 0;
3182} 3209}
3183 3210
3184static void 3211static void
3185s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3212s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3186{ 3213{
3211 3238
3212#endif 3239#endif
3213 3240
3214#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3241#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3215 3242
3243#if EXPERIMENT
3216static 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
3217opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3302opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3218{ 3303{
3304 pointer args = SCHEME_V->args;
3219 pointer x, y; 3305 pointer x, y;
3220 3306
3221 switch (op) 3307 switch (op)
3222 { 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
3223 case OP_LOAD: /* load */ 3315 case OP_LOAD: /* load */
3224 if (file_interactive (SCHEME_A)) 3316 if (file_interactive (SCHEME_A))
3225 { 3317 {
3226 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");
3227 //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)));
3228 } 3320 }
3229 3321
3230 if (!file_push (SCHEME_A_ strvalue (car (SCHEME_V->args)))) 3322 if (!file_push (SCHEME_A_ strvalue (car (args))))
3231 Error_1 ("unable to open", car (SCHEME_V->args)); 3323 Error_1 ("unable to open", car (args));
3232 else 3324 else
3233 { 3325 {
3234 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3326 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3235 s_goto (OP_T0LVL); 3327 s_goto (OP_T0LVL);
3236 } 3328 }
3237 3329
3238 case OP_T0LVL: /* top level */ 3330 case OP_T0LVL: /* top level */
3239 3331
3240 /* If we reached the end of file, this loop is done. */ 3332 /* If we reached the end of file, this loop is done. */
3241 if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) 3333 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3242 { 3334 {
3243 if (SCHEME_V->file_i == 0) 3335 if (SCHEME_V->file_i == 0)
3244 { 3336 {
3245 SCHEME_V->args = NIL; 3337 SCHEME_V->args = NIL;
3246 s_goto (OP_QUIT); 3338 s_goto (OP_QUIT);
3310 case OP_EVAL: /* main part of evaluation */ 3402 case OP_EVAL: /* main part of evaluation */
3311#if USE_TRACING 3403#if USE_TRACING
3312 if (SCHEME_V->tracing) 3404 if (SCHEME_V->tracing)
3313 { 3405 {
3314 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ 3406 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */
3315 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);
3316 SCHEME_V->args = SCHEME_V->code; 3408 SCHEME_V->args = SCHEME_V->code;
3317 putstr (SCHEME_A_ "\nEval: "); 3409 putstr (SCHEME_A_ "\nEval: ");
3318 s_goto (OP_P0LIST); 3410 s_goto (OP_P0LIST);
3319 } 3411 }
3320 3412
3324#endif 3416#endif
3325 if (is_symbol (SCHEME_V->code)) /* symbol */ 3417 if (is_symbol (SCHEME_V->code)) /* symbol */
3326 { 3418 {
3327 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);
3328 3420
3329 if (x != NIL) 3421 if (x == NIL)
3330 s_return (slot_value_in_env (x));
3331 else
3332 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));
3333 } 3425 }
3334 else if (is_pair (SCHEME_V->code)) 3426 else if (is_pair (SCHEME_V->code))
3335 { 3427 {
3336 x = car (SCHEME_V->code); 3428 x = car (SCHEME_V->code);
3337 3429
3350 } 3442 }
3351 else 3443 else
3352 s_return (SCHEME_V->code); 3444 s_return (SCHEME_V->code);
3353 3445
3354 case OP_E0ARGS: /* eval arguments */ 3446 case OP_E0ARGS: /* eval arguments */
3355 if (is_macro (SCHEME_V->value)) /* macro expansion */ 3447 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3356 { 3448 {
3357 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3449 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3358 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3450 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3359 SCHEME_V->code = SCHEME_V->value; 3451 SCHEME_V->code = SCHEME_V->value;
3360 s_goto (OP_APPLY); 3452 s_goto (OP_APPLY);
3364 SCHEME_V->code = cdr (SCHEME_V->code); 3456 SCHEME_V->code = cdr (SCHEME_V->code);
3365 s_goto (OP_E1ARGS); 3457 s_goto (OP_E1ARGS);
3366 } 3458 }
3367 3459
3368 case OP_E1ARGS: /* eval arguments */ 3460 case OP_E1ARGS: /* eval arguments */
3369 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3461 args = cons (SCHEME_V->value, args);
3370 3462
3371 if (is_pair (SCHEME_V->code)) /* continue */ 3463 if (is_pair (SCHEME_V->code)) /* continue */
3372 { 3464 {
3373 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));
3374 SCHEME_V->code = car (SCHEME_V->code); 3466 SCHEME_V->code = car (SCHEME_V->code);
3375 SCHEME_V->args = NIL; 3467 SCHEME_V->args = NIL;
3376 s_goto (OP_EVAL); 3468 s_goto (OP_EVAL);
3377 } 3469 }
3378 else /* end */ 3470 else /* end */
3379 { 3471 {
3380 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3472 args = reverse_in_place (SCHEME_A_ NIL, args);
3381 SCHEME_V->code = car (SCHEME_V->args); 3473 SCHEME_V->code = car (args);
3382 SCHEME_V->args = cdr (SCHEME_V->args); 3474 SCHEME_V->args = cdr (args);
3383 s_goto (OP_APPLY); 3475 s_goto (OP_APPLY);
3384 } 3476 }
3385 3477
3386#if USE_TRACING 3478#if USE_TRACING
3387 3479
3388 case OP_TRACING: 3480 case OP_TRACING:
3389 { 3481 {
3390 int tr = SCHEME_V->tracing; 3482 int tr = SCHEME_V->tracing;
3391 3483
3392 SCHEME_V->tracing = ivalue (car (SCHEME_V->args)); 3484 SCHEME_V->tracing = ivalue_unchecked (car (args));
3393 s_return (mk_integer (SCHEME_A_ tr)); 3485 s_return (mk_integer (SCHEME_A_ tr));
3394 } 3486 }
3395 3487
3396#endif 3488#endif
3397 3489
3398 case OP_APPLY: /* apply 'code' to 'args' */ 3490 case OP_APPLY: /* apply 'code' to 'args' */
3399#if USE_TRACING 3491#if USE_TRACING
3400 if (SCHEME_V->tracing) 3492 if (SCHEME_V->tracing)
3401 { 3493 {
3402 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);
3403 SCHEME_V->print_flag = 1; 3495 SCHEME_V->print_flag = 1;
3404 /* SCHEME_V->args=cons(SCHEME_V->code,SCHEME_V->args); */ 3496 /* args=cons(SCHEME_V->code,args); */
3405 putstr (SCHEME_A_ "\nApply to: "); 3497 putstr (SCHEME_A_ "\nApply to: ");
3406 s_goto (OP_P0LIST); 3498 s_goto (OP_P0LIST);
3407 } 3499 }
3408 3500
3409 /* fall through */ 3501 /* fall through */
3410 3502
3411 case OP_REAL_APPLY: 3503 case OP_REAL_APPLY:
3412#endif 3504#endif
3413 if (is_proc (SCHEME_V->code)) 3505 if (is_proc (SCHEME_V->code))
3414 {
3415 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3506 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3416 }
3417 else if (is_foreign (SCHEME_V->code)) 3507 else if (is_foreign (SCHEME_V->code))
3418 { 3508 {
3419 /* Keep nested calls from GC'ing the arglist */ 3509 /* Keep nested calls from GC'ing the arglist */
3420 push_recent_alloc (SCHEME_A_ SCHEME_V->args, NIL); 3510 push_recent_alloc (SCHEME_A_ args, NIL);
3421 x = SCHEME_V->code->object.ff (SCHEME_A_ SCHEME_V->args); 3511 x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args);
3422 3512
3423 s_return (x); 3513 s_return (x);
3424 } 3514 }
3425 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 */
3426 { 3516 {
3427 /* Should not accept promise */ 3517 /* Should not accept promise */
3428 /* make environment */ 3518 /* make environment */
3429 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); 3519 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code));
3430 3520
3431 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))
3432 { 3522 {
3433 if (y == NIL) 3523 if (y == NIL)
3434 Error_0 ("not enough arguments"); 3524 Error_0 ("not enough arguments");
3435 else 3525 else
3436 new_slot_in_env (SCHEME_A_ car (x), car (y)); 3526 new_slot_in_env (SCHEME_A_ car (x), car (y));
3454 s_goto (OP_BEGIN); 3544 s_goto (OP_BEGIN);
3455 } 3545 }
3456 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ 3546 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */
3457 { 3547 {
3458 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); 3548 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code));
3459 s_return (SCHEME_V->args != NIL ? car (SCHEME_V->args) : NIL); 3549 s_return (args != NIL ? car (args) : NIL);
3460 } 3550 }
3461 else 3551 else
3462 Error_0 ("illegal function"); 3552 Error_0 ("illegal function");
3463 3553
3464 case OP_DOMACRO: /* do macro */ 3554 case OP_DOMACRO: /* do macro */
3465 SCHEME_V->code = SCHEME_V->value; 3555 SCHEME_V->code = SCHEME_V->value;
3466 s_goto (OP_EVAL); 3556 s_goto (OP_EVAL);
3467
3468#if 1
3469 3557
3470 case OP_LAMBDA: /* lambda */ 3558 case OP_LAMBDA: /* lambda */
3471 /* 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
3472 set SCHEME_V->value fall thru */ 3560 set SCHEME_V->value fall thru */
3473 { 3561 {
3474 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);
3475 3563
3476 if (f != NIL) 3564 if (f != NIL)
3477 { 3565 {
3478 s_save (SCHEME_A_ OP_LAMBDA1, SCHEME_V->args, SCHEME_V->code); 3566 s_save (SCHEME_A_ OP_LAMBDA1, args, SCHEME_V->code);
3479 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3567 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3480 SCHEME_V->code = slot_value_in_env (f); 3568 SCHEME_V->code = slot_value_in_env (f);
3481 s_goto (OP_APPLY); 3569 s_goto (OP_APPLY);
3482 } 3570 }
3483 3571
3484 SCHEME_V->value = SCHEME_V->code; 3572 SCHEME_V->value = SCHEME_V->code;
3485 /* Fallthru */
3486 } 3573 }
3574 /* Fallthru */
3487 3575
3488 case OP_LAMBDA1: 3576 case OP_LAMBDA1:
3489 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));
3490 3578
3491#else
3492
3493 case OP_LAMBDA: /* lambda */
3494 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir));
3495
3496#endif
3497
3498 case OP_MKCLOSURE: /* make-closure */ 3579 case OP_MKCLOSURE: /* make-closure */
3499 x = car (SCHEME_V->args); 3580 x = car (args);
3500 3581
3501 if (car (x) == SCHEME_V->LAMBDA) 3582 if (car (x) == SCHEME_V->LAMBDA)
3502 x = cdr (x); 3583 x = cdr (x);
3503 3584
3504 if (cdr (SCHEME_V->args) == NIL) 3585 if (cdr (args) == NIL)
3505 y = SCHEME_V->envir; 3586 y = SCHEME_V->envir;
3506 else 3587 else
3507 y = cadr (SCHEME_V->args); 3588 y = cadr (args);
3508 3589
3509 s_return (mk_closure (SCHEME_A_ x, y)); 3590 s_return (mk_closure (SCHEME_A_ x, y));
3510 3591
3511 case OP_QUOTE: /* quote */ 3592 case OP_QUOTE: /* quote */
3512 s_return (car (SCHEME_V->code)); 3593 s_return (car (SCHEME_V->code));
3544 3625
3545 3626
3546 case OP_DEFP: /* defined? */ 3627 case OP_DEFP: /* defined? */
3547 x = SCHEME_V->envir; 3628 x = SCHEME_V->envir;
3548 3629
3549 if (cdr (SCHEME_V->args) != NIL) 3630 if (cdr (args) != NIL)
3550 x = cadr (SCHEME_V->args); 3631 x = cadr (args);
3551 3632
3552 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);
3553 3634
3554 case OP_SET0: /* set! */ 3635 case OP_SET0: /* set! */
3555 if (is_immutable (car (SCHEME_V->code))) 3636 if (is_immutable (car (SCHEME_V->code)))
3556 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));
3557 3638
3588 3669
3589 case OP_IF1: /* if */ 3670 case OP_IF1: /* if */
3590 if (is_true (SCHEME_V->value)) 3671 if (is_true (SCHEME_V->value))
3591 SCHEME_V->code = car (SCHEME_V->code); 3672 SCHEME_V->code = car (SCHEME_V->code);
3592 else 3673 else
3593 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 */
3594
3595 * car(NIL) = NIL */
3596 s_goto (OP_EVAL); 3675 s_goto (OP_EVAL);
3597 3676
3598 case OP_LET0: /* let */ 3677 case OP_LET0: /* let */
3599 SCHEME_V->args = NIL; 3678 SCHEME_V->args = NIL;
3600 SCHEME_V->value = SCHEME_V->code; 3679 SCHEME_V->value = SCHEME_V->code;
3601 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);
3602 s_goto (OP_LET1); 3681 s_goto (OP_LET1);
3603 3682
3604 case OP_LET1: /* let (calculate parameters) */ 3683 case OP_LET1: /* let (calculate parameters) */
3605 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3684 args = cons (SCHEME_V->value, args);
3606 3685
3607 if (is_pair (SCHEME_V->code)) /* continue */ 3686 if (is_pair (SCHEME_V->code)) /* continue */
3608 { 3687 {
3609 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)))
3610 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));
3611 3690
3612 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));
3613 SCHEME_V->code = cadar (SCHEME_V->code); 3692 SCHEME_V->code = cadar (SCHEME_V->code);
3614 SCHEME_V->args = NIL; 3693 SCHEME_V->args = NIL;
3615 s_goto (OP_EVAL); 3694 s_goto (OP_EVAL);
3616 } 3695 }
3617 else /* end */ 3696 else /* end */
3618 { 3697 {
3619 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3698 args = reverse_in_place (SCHEME_A_ NIL, args);
3620 SCHEME_V->code = car (SCHEME_V->args); 3699 SCHEME_V->code = car (args);
3621 SCHEME_V->args = cdr (SCHEME_V->args); 3700 SCHEME_V->args = cdr (args);
3622 s_goto (OP_LET2); 3701 s_goto (OP_LET2);
3623 } 3702 }
3624 3703
3625 case OP_LET2: /* let */ 3704 case OP_LET2: /* let */
3626 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 3705 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3627 3706
3628 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;
3629 y != NIL; x = cdr (x), y = cdr (y)) 3708 y != NIL; x = cdr (x), y = cdr (y))
3630 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3709 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3631 3710
3632 if (is_symbol (car (SCHEME_V->code))) /* named let */ 3711 if (is_symbol (car (SCHEME_V->code))) /* named let */
3633 { 3712 {
3634 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))
3635 { 3714 {
3636 if (!is_pair (x)) 3715 if (!is_pair (x))
3637 Error_1 ("Bad syntax of binding in let :", x); 3716 Error_1 ("Bad syntax of binding in let :", x);
3638 3717
3639 if (!is_list (SCHEME_A_ car (x))) 3718 if (!is_list (SCHEME_A_ car (x)))
3640 Error_1 ("Bad syntax of binding in let :", car (x)); 3719 Error_1 ("Bad syntax of binding in let :", car (x));
3641 3720
3642 SCHEME_V->args = cons (caar (x), SCHEME_V->args); 3721 args = cons (caar (x), args);
3643 } 3722 }
3644 3723
3645 x =
3646 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)),
3647 SCHEME_V->envir); 3725 SCHEME_V->envir);
3648 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); 3726 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x);
3649 SCHEME_V->code = cddr (SCHEME_V->code); 3727 SCHEME_V->code = cddr (SCHEME_V->code);
3650 SCHEME_V->args = NIL;
3651 } 3728 }
3652 else 3729 else
3653 { 3730 {
3654 SCHEME_V->code = cdr (SCHEME_V->code); 3731 SCHEME_V->code = cdr (SCHEME_V->code);
3732 }
3733
3655 SCHEME_V->args = NIL; 3734 SCHEME_V->args = NIL;
3656 }
3657
3658 s_goto (OP_BEGIN); 3735 s_goto (OP_BEGIN);
3659 3736
3660 case OP_LET0AST: /* let* */ 3737 case OP_LET0AST: /* let* */
3661 if (car (SCHEME_V->code) == NIL) 3738 if (car (SCHEME_V->code) == NIL)
3662 { 3739 {
3680 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);
3681 SCHEME_V->code = cdr (SCHEME_V->code); 3758 SCHEME_V->code = cdr (SCHEME_V->code);
3682 3759
3683 if (is_pair (SCHEME_V->code)) /* continue */ 3760 if (is_pair (SCHEME_V->code)) /* continue */
3684 { 3761 {
3685 s_save (SCHEME_A_ OP_LET2AST, SCHEME_V->args, SCHEME_V->code); 3762 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code);
3686 SCHEME_V->code = cadar (SCHEME_V->code); 3763 SCHEME_V->code = cadar (SCHEME_V->code);
3687 SCHEME_V->args = NIL; 3764 SCHEME_V->args = NIL;
3688 s_goto (OP_EVAL); 3765 s_goto (OP_EVAL);
3689 } 3766 }
3690 else /* end */ 3767 else /* end */
3691 { 3768 {
3692 SCHEME_V->code = SCHEME_V->args; 3769 SCHEME_V->code = args;
3693 SCHEME_V->args = NIL; 3770 SCHEME_V->args = NIL;
3694 s_goto (OP_BEGIN); 3771 s_goto (OP_BEGIN);
3695 } 3772 }
3696 3773
3697 case OP_LET0REC: /* letrec */ 3774 case OP_LET0REC: /* letrec */
3700 SCHEME_V->value = SCHEME_V->code; 3777 SCHEME_V->value = SCHEME_V->code;
3701 SCHEME_V->code = car (SCHEME_V->code); 3778 SCHEME_V->code = car (SCHEME_V->code);
3702 s_goto (OP_LET1REC); 3779 s_goto (OP_LET1REC);
3703 3780
3704 case OP_LET1REC: /* letrec (calculate parameters) */ 3781 case OP_LET1REC: /* letrec (calculate parameters) */
3705 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3782 args = cons (SCHEME_V->value, args);
3706 3783
3707 if (is_pair (SCHEME_V->code)) /* continue */ 3784 if (is_pair (SCHEME_V->code)) /* continue */
3708 { 3785 {
3709 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)))
3710 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));
3711 3788
3712 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));
3713 SCHEME_V->code = cadar (SCHEME_V->code); 3790 SCHEME_V->code = cadar (SCHEME_V->code);
3714 SCHEME_V->args = NIL; 3791 SCHEME_V->args = NIL;
3715 s_goto (OP_EVAL); 3792 s_goto (OP_EVAL);
3716 } 3793 }
3717 else /* end */ 3794 else /* end */
3718 { 3795 {
3719 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3796 args = reverse_in_place (SCHEME_A_ NIL, args);
3720 SCHEME_V->code = car (SCHEME_V->args); 3797 SCHEME_V->code = car (args);
3721 SCHEME_V->args = cdr (SCHEME_V->args); 3798 SCHEME_V->args = cdr (args);
3722 s_goto (OP_LET2REC); 3799 s_goto (OP_LET2REC);
3723 } 3800 }
3724 3801
3725 case OP_LET2REC: /* letrec */ 3802 case OP_LET2REC: /* letrec */
3726 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))
3727 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3804 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3728 3805
3729 SCHEME_V->code = cdr (SCHEME_V->code); 3806 SCHEME_V->code = cdr (SCHEME_V->code);
3730 SCHEME_V->args = NIL; 3807 SCHEME_V->args = NIL;
3731 s_goto (OP_BEGIN); 3808 s_goto (OP_BEGIN);
3817 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 3894 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3818 SCHEME_V->code = car (SCHEME_V->code); 3895 SCHEME_V->code = car (SCHEME_V->code);
3819 s_goto (OP_EVAL); 3896 s_goto (OP_EVAL);
3820 3897
3821 case OP_C1STREAM: /* cons-stream */ 3898 case OP_C1STREAM: /* cons-stream */
3822 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 */
3823 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);
3824 set_typeflag (x, T_PROMISE); 3901 set_typeflag (x, T_PROMISE);
3825 s_return (cons (SCHEME_V->args, x)); 3902 s_return (cons (args, x));
3826 3903
3827 case OP_MACRO0: /* macro */ 3904 case OP_MACRO0: /* macro */
3828 if (is_pair (car (SCHEME_V->code))) 3905 if (is_pair (car (SCHEME_V->code)))
3829 { 3906 {
3830 x = caar (SCHEME_V->code); 3907 x = caar (SCHEME_V->code);
3863 { 3940 {
3864 if (!is_pair (y = caar (x))) 3941 if (!is_pair (y = caar (x)))
3865 break; 3942 break;
3866 3943
3867 for (; y != NIL; y = cdr (y)) 3944 for (; y != NIL; y = cdr (y))
3868 {
3869 if (eqv (car (y), SCHEME_V->value)) 3945 if (eqv (car (y), SCHEME_V->value))
3870 break; 3946 break;
3871 }
3872 3947
3873 if (y != NIL) 3948 if (y != NIL)
3874 break; 3949 break;
3875 } 3950 }
3876 3951
3896 s_goto (OP_BEGIN); 3971 s_goto (OP_BEGIN);
3897 else 3972 else
3898 s_return (NIL); 3973 s_return (NIL);
3899 3974
3900 case OP_PAPPLY: /* apply */ 3975 case OP_PAPPLY: /* apply */
3901 SCHEME_V->code = car (SCHEME_V->args); 3976 SCHEME_V->code = car (args);
3902 SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); 3977 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
3903 /*SCHEME_V->args = cadr(SCHEME_V->args); */ 3978 /*SCHEME_V->args = cadr(args); */
3904 s_goto (OP_APPLY); 3979 s_goto (OP_APPLY);
3905 3980
3906 case OP_PEVAL: /* eval */ 3981 case OP_PEVAL: /* eval */
3907 if (cdr (SCHEME_V->args) != NIL) 3982 if (cdr (args) != NIL)
3908 SCHEME_V->envir = cadr (SCHEME_V->args); 3983 SCHEME_V->envir = cadr (args);
3909 3984
3910 SCHEME_V->code = car (SCHEME_V->args); 3985 SCHEME_V->code = car (args);
3911 s_goto (OP_EVAL); 3986 s_goto (OP_EVAL);
3912 3987
3913 case OP_CONTINUATION: /* call-with-current-continuation */ 3988 case OP_CONTINUATION: /* call-with-current-continuation */
3914 SCHEME_V->code = car (SCHEME_V->args); 3989 SCHEME_V->code = car (args);
3915 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);
3916 s_goto (OP_APPLY); 3991 s_goto (OP_APPLY);
3917 } 3992 }
3918 3993
3919 abort (); 3994 if (USE_ERROR_CHECKING) abort ();
3920} 3995}
3921 3996
3922static pointer 3997static int
3923opexe_2 (SCHEME_P_ enum scheme_opcodes op) 3998opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3924{ 3999{
3925 pointer x; 4000 pointer args = SCHEME_V->args;
4001 pointer x = car (args);
3926 num v; 4002 num v;
3927 4003
4004 switch (op)
4005 {
3928#if USE_MATH 4006#if USE_MATH
3929 RVALUE dd;
3930#endif
3931
3932 switch (op)
3933 {
3934#if USE_MATH
3935
3936 case OP_INEX2EX: /* inexact->exact */ 4007 case OP_INEX2EX: /* inexact->exact */
3937 x = car (SCHEME_V->args);
3938
3939 if (num_is_integer (x)) 4008 if (!is_integer (x))
3940 s_return (x); 4009 {
3941 else if (modf (rvalue_unchecked (x), &dd) == 0.0) 4010 RVALUE r = rvalue_unchecked (x);
3942 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4011
4012 if (r == (RVALUE)(IVALUE)r)
4013 x = mk_integer (SCHEME_A_ rvalue_unchecked (x));
3943 else 4014 else
3944 Error_1 ("inexact->exact: not integral:", x); 4015 Error_1 ("inexact->exact: not integral:", x);
4016 }
3945 4017
3946 case OP_EXP: 4018 s_return (x);
3947 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))));
3948 s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4026 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
3949
3950 case OP_LOG:
3951 x = car (SCHEME_V->args);
3952 s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 4027 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))
3953 4028 / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args))))));
3954 case OP_SIN:
3955 x = car (SCHEME_V->args);
3956 s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 4029 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
3957
3958 case OP_COS:
3959 x = car (SCHEME_V->args);
3960 s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 4030 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3961
3962 case OP_TAN:
3963 x = car (SCHEME_V->args);
3964 s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); 4031 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x))));
3965
3966 case OP_ASIN:
3967 x = car (SCHEME_V->args);
3968 s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); 4032 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x))));
3969
3970 case OP_ACOS:
3971 x = car (SCHEME_V->args);
3972 s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); 4033 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x))));
3973 4034
3974 case OP_ATAN: 4035 case OP_ATAN:
3975 x = car (SCHEME_V->args);
3976
3977 if (cdr (SCHEME_V->args) == NIL)
3978 s_return (mk_real (SCHEME_A_ atan (rvalue (x))));
3979 else
3980 {
3981 pointer y = cadr (SCHEME_V->args);
3982
3983 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
3984 }
3985
3986 case OP_SQRT:
3987 x = car (SCHEME_V->args);
3988 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); 4036 s_return (mk_real (SCHEME_A_
4037 cdr (args) == NIL
4038 ? atan (rvalue (x))
4039 : atan2 (rvalue (x), rvalue (cadr (args)))));
3989 4040
3990 case OP_EXPT: 4041 case OP_EXPT:
3991 { 4042 {
3992 RVALUE result; 4043 RVALUE result;
3993 int real_result = 1; 4044 int real_result = 1;
3994 pointer y = cadr (SCHEME_V->args); 4045 pointer y = cadr (args);
3995 4046
3996 x = car (SCHEME_V->args);
3997
3998 if (num_is_integer (x) && num_is_integer (y)) 4047 if (is_integer (x) && is_integer (y))
3999 real_result = 0; 4048 real_result = 0;
4000 4049
4001 /* This 'if' is an R5RS compatibility fix. */ 4050 /* This 'if' is an R5RS compatibility fix. */
4002 /* NOTE: Remove this 'if' fix for R6RS. */ 4051 /* NOTE: Remove this 'if' fix for R6RS. */
4003 if (rvalue (x) == 0 && rvalue (y) < 0) 4052 if (rvalue (x) == 0 && rvalue (y) < 0)
4004 result = 0.0; 4053 result = 0;
4005 else 4054 else
4006 result = pow (rvalue (x), rvalue (y)); 4055 result = pow (rvalue (x), rvalue (y));
4007 4056
4008 /* Before returning integer result make sure we can. */ 4057 /* Before returning integer result make sure we can. */
4009 /* If the test fails, result is too big for integer. */ 4058 /* If the test fails, result is too big for integer. */
4010 if (!real_result) 4059 if (!real_result)
4011 { 4060 {
4012 long result_as_long = (long) result; 4061 long result_as_long = result;
4013 4062
4014 if (result != (RVALUE) result_as_long) 4063 if (result != result_as_long)
4015 real_result = 1; 4064 real_result = 1;
4016 } 4065 }
4017 4066
4018 if (real_result) 4067 if (real_result)
4019 s_return (mk_real (SCHEME_A_ result)); 4068 s_return (mk_real (SCHEME_A_ result));
4020 else 4069 else
4021 s_return (mk_integer (SCHEME_A_ result)); 4070 s_return (mk_integer (SCHEME_A_ result));
4022 } 4071 }
4023
4024 case OP_FLOOR:
4025 x = car (SCHEME_V->args);
4026 s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4027
4028 case OP_CEILING:
4029 x = car (SCHEME_V->args);
4030 s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4031
4032 case OP_TRUNCATE:
4033 {
4034 RVALUE rvalue_of_x;
4035
4036 x = car (SCHEME_V->args);
4037 rvalue_of_x = rvalue (x);
4038
4039 if (rvalue_of_x > 0)
4040 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x)));
4041 else
4042 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
4043 }
4044
4045 case OP_ROUND:
4046 x = car (SCHEME_V->args);
4047
4048 if (num_is_integer (x))
4049 s_return (x);
4050
4051 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4052#endif 4072#endif
4053 4073
4054 case OP_ADD: /* + */ 4074 case OP_ADD: /* + */
4055 v = num_zero; 4075 v = num_zero;
4056 4076
4057 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4077 for (x = args; x != NIL; x = cdr (x))
4058 v = num_op ('+', v, nvalue (car (x))); 4078 v = num_op (NUM_ADD, v, nvalue (car (x)));
4059 4079
4060 s_return (mk_number (SCHEME_A_ v)); 4080 s_return (mk_number (SCHEME_A_ v));
4061 4081
4062 case OP_MUL: /* * */ 4082 case OP_MUL: /* * */
4063 v = num_one; 4083 v = num_one;
4064 4084
4065 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4085 for (x = args; x != NIL; x = cdr (x))
4066 v = num_op ('+', v, nvalue (car (x))); 4086 v = num_op (NUM_MUL, v, nvalue (car (x)));
4067 4087
4068 s_return (mk_number (SCHEME_A_ v)); 4088 s_return (mk_number (SCHEME_A_ v));
4069 4089
4070 case OP_SUB: /* - */ 4090 case OP_SUB: /* - */
4071 if (cdr (SCHEME_V->args) == NIL) 4091 if (cdr (args) == NIL)
4072 { 4092 {
4073 x = SCHEME_V->args; 4093 x = args;
4074 v = num_zero; 4094 v = num_zero;
4075 } 4095 }
4076 else 4096 else
4077 { 4097 {
4078 x = cdr (SCHEME_V->args); 4098 x = cdr (args);
4079 v = nvalue (car (SCHEME_V->args)); 4099 v = nvalue (car (args));
4080 } 4100 }
4081 4101
4082 for (; x != NIL; x = cdr (x)) 4102 for (; x != NIL; x = cdr (x))
4083 v = num_op ('+', v, nvalue (car (x))); 4103 v = num_op (NUM_SUB, v, nvalue (car (x)));
4084 4104
4085 s_return (mk_number (SCHEME_A_ v)); 4105 s_return (mk_number (SCHEME_A_ v));
4086 4106
4087 case OP_DIV: /* / */ 4107 case OP_DIV: /* / */
4088 if (cdr (SCHEME_V->args) == NIL) 4108 if (cdr (args) == NIL)
4089 { 4109 {
4090 x = SCHEME_V->args; 4110 x = args;
4091 v = num_one; 4111 v = num_one;
4092 } 4112 }
4093 else 4113 else
4094 { 4114 {
4095 x = cdr (SCHEME_V->args); 4115 x = cdr (args);
4096 v = nvalue (car (SCHEME_V->args)); 4116 v = nvalue (car (args));
4097 } 4117 }
4098 4118
4099 for (; x != NIL; x = cdr (x)) 4119 for (; x != NIL; x = cdr (x))
4100 {
4101 if (!is_zero_rvalue (rvalue (car (x)))) 4120 if (!is_zero_rvalue (rvalue (car (x))))
4102 v = num_div (v, nvalue (car (x))); 4121 v = num_div (v, nvalue (car (x)));
4103 else 4122 else
4104 Error_0 ("/: division by zero"); 4123 Error_0 ("/: division by zero");
4105 }
4106 4124
4107 s_return (mk_number (SCHEME_A_ v)); 4125 s_return (mk_number (SCHEME_A_ v));
4108 4126
4109 case OP_INTDIV: /* quotient */ 4127 case OP_INTDIV: /* quotient */
4110 if (cdr (SCHEME_V->args) == NIL) 4128 if (cdr (args) == NIL)
4111 { 4129 {
4112 x = SCHEME_V->args; 4130 x = args;
4113 v = num_one; 4131 v = num_one;
4114 } 4132 }
4115 else 4133 else
4116 { 4134 {
4117 x = cdr (SCHEME_V->args); 4135 x = cdr (args);
4118 v = nvalue (car (SCHEME_V->args)); 4136 v = nvalue (car (args));
4119 } 4137 }
4120 4138
4121 for (; x != NIL; x = cdr (x)) 4139 for (; x != NIL; x = cdr (x))
4122 { 4140 {
4123 if (ivalue (car (x)) != 0) 4141 if (ivalue (car (x)) != 0)
4124 v = num_op ('/', v, nvalue (car (x))); 4142 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4125 else 4143 else
4126 Error_0 ("quotient: division by zero"); 4144 Error_0 ("quotient: division by zero");
4127 } 4145 }
4128 4146
4129 s_return (mk_number (SCHEME_A_ v)); 4147 s_return (mk_number (SCHEME_A_ v));
4130 4148
4131 case OP_REM: /* remainder */ 4149 case OP_REM: /* remainder */
4132 v = nvalue (car (SCHEME_V->args)); 4150 v = nvalue (x);
4133 4151
4134 if (ivalue (cadr (SCHEME_V->args)) != 0) 4152 if (ivalue (cadr (args)) != 0)
4135 v = num_rem (v, nvalue (cadr (SCHEME_V->args))); 4153 v = num_rem (v, nvalue (cadr (args)));
4136 else 4154 else
4137 Error_0 ("remainder: division by zero"); 4155 Error_0 ("remainder: division by zero");
4138 4156
4139 s_return (mk_number (SCHEME_A_ v)); 4157 s_return (mk_number (SCHEME_A_ v));
4140 4158
4141 case OP_MOD: /* modulo */ 4159 case OP_MOD: /* modulo */
4142 v = nvalue (car (SCHEME_V->args)); 4160 v = nvalue (x);
4143 4161
4144 if (ivalue (cadr (SCHEME_V->args)) != 0) 4162 if (ivalue (cadr (args)) != 0)
4145 v = num_mod (v, nvalue (cadr (SCHEME_V->args))); 4163 v = num_mod (v, nvalue (cadr (args)));
4146 else 4164 else
4147 Error_0 ("modulo: division by zero"); 4165 Error_0 ("modulo: division by zero");
4148 4166
4149 s_return (mk_number (SCHEME_A_ v)); 4167 s_return (mk_number (SCHEME_A_ v));
4150 4168
4151 case OP_CAR: /* car */ 4169 /* the compiler will optimize this mess... */
4152 s_return (caar (SCHEME_V->args)); 4170 case OP_CAR: op_car: s_return (car (x));
4153 4171 case OP_CDR: op_cdr: s_return (cdr (x));
4154 case OP_CDR: /* cdr */ 4172 case OP_CAAR: op_caar: x = car (x); goto op_car;
4155 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;
4156 4200
4157 case OP_CONS: /* cons */ 4201 case OP_CONS: /* cons */
4158 set_cdr (SCHEME_V->args, cadr (SCHEME_V->args)); 4202 set_cdr (args, cadr (args));
4159 s_return (SCHEME_V->args); 4203 s_return (args);
4160 4204
4161 case OP_SETCAR: /* set-car! */ 4205 case OP_SETCAR: /* set-car! */
4162 if (!is_immutable (car (SCHEME_V->args))) 4206 if (!is_immutable (x))
4163 { 4207 {
4164 set_car (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4208 set_car (x, cadr (args));
4165 s_return (car (SCHEME_V->args)); 4209 s_return (car (args));
4166 } 4210 }
4167 else 4211 else
4168 Error_0 ("set-car!: unable to alter immutable pair"); 4212 Error_0 ("set-car!: unable to alter immutable pair");
4169 4213
4170 case OP_SETCDR: /* set-cdr! */ 4214 case OP_SETCDR: /* set-cdr! */
4171 if (!is_immutable (car (SCHEME_V->args))) 4215 if (!is_immutable (x))
4172 { 4216 {
4173 set_cdr (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4217 set_cdr (x, cadr (args));
4174 s_return (car (SCHEME_V->args)); 4218 s_return (car (args));
4175 } 4219 }
4176 else 4220 else
4177 Error_0 ("set-cdr!: unable to alter immutable pair"); 4221 Error_0 ("set-cdr!: unable to alter immutable pair");
4178 4222
4179 case OP_CHAR2INT: /* char->integer */ 4223 case OP_CHAR2INT: /* char->integer */
4180 s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4224 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4181 4225
4182 case OP_INT2CHAR: /* integer->char */ 4226 case OP_INT2CHAR: /* integer->char */
4183 s_return (mk_character (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4227 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4184 4228
4185 case OP_CHARUPCASE: 4229 case OP_CHARUPCASE:
4186 { 4230 {
4187 unsigned char c = ivalue (car (SCHEME_V->args)); 4231 unsigned char c = ivalue_unchecked (x);
4188 c = toupper (c); 4232 c = toupper (c);
4189 s_return (mk_character (SCHEME_A_ c)); 4233 s_return (mk_character (SCHEME_A_ c));
4190 } 4234 }
4191 4235
4192 case OP_CHARDNCASE: 4236 case OP_CHARDNCASE:
4193 { 4237 {
4194 unsigned char c = ivalue (car (SCHEME_V->args)); 4238 unsigned char c = ivalue_unchecked (x);
4195 c = tolower (c); 4239 c = tolower (c);
4196 s_return (mk_character (SCHEME_A_ c)); 4240 s_return (mk_character (SCHEME_A_ c));
4197 } 4241 }
4198 4242
4199 case OP_STR2SYM: /* string->symbol */ 4243 case OP_STR2SYM: /* string->symbol */
4200 s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); 4244 s_return (mk_symbol (SCHEME_A_ strvalue (x)));
4201 4245
4202 case OP_STR2ATOM: /* string->atom */ 4246 case OP_STR2ATOM: /* string->atom */
4203 { 4247 {
4204 char *s = strvalue (car (SCHEME_V->args)); 4248 char *s = strvalue (x);
4205 long pf = 0; 4249 long pf = 0;
4206 4250
4207 if (cdr (SCHEME_V->args) != NIL) 4251 if (cdr (args) != NIL)
4208 { 4252 {
4209 /* we know cadr(SCHEME_V->args) is a natural number */ 4253 /* we know cadr(args) is a natural number */
4210 /* see if it is 2, 8, 10, or 16, or error */ 4254 /* see if it is 2, 8, 10, or 16, or error */
4211 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4255 pf = ivalue_unchecked (cadr (args));
4212 4256
4213 if (pf == 16 || pf == 10 || pf == 8 || pf == 2) 4257 if (pf == 16 || pf == 10 || pf == 8 || pf == 2)
4214 { 4258 {
4215 /* base is OK */ 4259 /* base is OK */
4216 } 4260 }
4217 else 4261 else
4218 pf = -1; 4262 pf = -1;
4219 } 4263 }
4220 4264
4221 if (pf < 0) 4265 if (pf < 0)
4222 Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); 4266 Error_1 ("string->atom: bad base:", cadr (args));
4223 else if (*s == '#') /* no use of base! */ 4267 else if (*s == '#') /* no use of base! */
4224 s_return (mk_sharp_const (SCHEME_A_ s + 1)); 4268 s_return (mk_sharp_const (SCHEME_A_ s + 1));
4225 else 4269 else
4226 { 4270 {
4227 if (pf == 0 || pf == 10) 4271 if (pf == 0 || pf == 10)
4238 } 4282 }
4239 } 4283 }
4240 } 4284 }
4241 4285
4242 case OP_SYM2STR: /* symbol->string */ 4286 case OP_SYM2STR: /* symbol->string */
4243 x = mk_string (SCHEME_A_ symname (car (SCHEME_V->args))); 4287 x = mk_string (SCHEME_A_ symname (x));
4244 setimmutable (x); 4288 setimmutable (x);
4245 s_return (x); 4289 s_return (x);
4246 4290
4247 case OP_ATOM2STR: /* atom->string */ 4291 case OP_ATOM2STR: /* atom->string */
4248 { 4292 {
4249 long pf = 0; 4293 long pf = 0;
4250 4294
4251 x = car (SCHEME_V->args);
4252
4253 if (cdr (SCHEME_V->args) != NIL) 4295 if (cdr (args) != NIL)
4254 { 4296 {
4255 /* we know cadr(SCHEME_V->args) is a natural number */ 4297 /* we know cadr(args) is a natural number */
4256 /* see if it is 2, 8, 10, or 16, or error */ 4298 /* see if it is 2, 8, 10, or 16, or error */
4257 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4299 pf = ivalue_unchecked (cadr (args));
4258 4300
4259 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))
4260 { 4302 {
4261 /* base is OK */ 4303 /* base is OK */
4262 } 4304 }
4263 else 4305 else
4264 pf = -1; 4306 pf = -1;
4265 } 4307 }
4266 4308
4267 if (pf < 0) 4309 if (pf < 0)
4268 Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); 4310 Error_1 ("atom->string: bad base:", cadr (args));
4269 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))
4270 { 4312 {
4271 char *p; 4313 char *p;
4272 int len; 4314 int len;
4273 4315
4278 Error_1 ("atom->string: not an atom:", x); 4320 Error_1 ("atom->string: not an atom:", x);
4279 } 4321 }
4280 4322
4281 case OP_MKSTRING: /* make-string */ 4323 case OP_MKSTRING: /* make-string */
4282 { 4324 {
4283 int fill = ' '; 4325 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4284 int len; 4326 int len = ivalue_unchecked (x);
4285 4327
4286 len = ivalue (car (SCHEME_V->args));
4287
4288 if (cdr (SCHEME_V->args) != NIL)
4289 fill = charvalue (cadr (SCHEME_V->args));
4290
4291 s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); 4328 s_return (mk_empty_string (SCHEME_A_ len, fill));
4292 } 4329 }
4293 4330
4294 case OP_STRLEN: /* string-length */ 4331 case OP_STRLEN: /* string-length */
4295 s_return (mk_integer (SCHEME_A_ strlength (car (SCHEME_V->args)))); 4332 s_return (mk_integer (SCHEME_A_ strlength (x)));
4296 4333
4297 case OP_STRREF: /* string-ref */ 4334 case OP_STRREF: /* string-ref */
4298 { 4335 {
4299 char *str; 4336 char *str = strvalue (x);
4300 int index;
4301
4302 str = strvalue (car (SCHEME_V->args));
4303
4304 index = ivalue (cadr (SCHEME_V->args)); 4337 int index = ivalue_unchecked (cadr (args));
4305 4338
4306 if (index >= strlength (car (SCHEME_V->args))) 4339 if (index >= strlength (x))
4307 Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); 4340 Error_1 ("string-ref: out of bounds:", cadr (args));
4308 4341
4309 s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); 4342 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4310 } 4343 }
4311 4344
4312 case OP_STRSET: /* string-set! */ 4345 case OP_STRSET: /* string-set! */
4313 { 4346 {
4314 char *str; 4347 char *str = strvalue (x);
4315 int index; 4348 int index = ivalue_unchecked (cadr (args));
4316 int c; 4349 int c;
4317 4350
4318 if (is_immutable (car (SCHEME_V->args))) 4351 if (is_immutable (x))
4319 Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); 4352 Error_1 ("string-set!: unable to alter immutable string:", x);
4320 4353
4321 str = strvalue (car (SCHEME_V->args));
4322
4323 index = ivalue (cadr (SCHEME_V->args));
4324
4325 if (index >= strlength (car (SCHEME_V->args))) 4354 if (index >= strlength (x))
4326 Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); 4355 Error_1 ("string-set!: out of bounds:", cadr (args));
4327 4356
4328 c = charvalue (caddr (SCHEME_V->args)); 4357 c = charvalue (caddr (args));
4329 4358
4330 str[index] = (char) c; 4359 str[index] = c;
4331 s_return (car (SCHEME_V->args)); 4360 s_return (car (args));
4332 } 4361 }
4333 4362
4334 case OP_STRAPPEND: /* string-append */ 4363 case OP_STRAPPEND: /* string-append */
4335 { 4364 {
4336 /* 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 */
4337 int len = 0; 4366 int len = 0;
4338 pointer newstr; 4367 pointer newstr;
4339 char *pos; 4368 char *pos;
4340 4369
4341 /* compute needed length for new string */ 4370 /* compute needed length for new string */
4342 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4371 for (x = args; x != NIL; x = cdr (x))
4343 len += strlength (car (x)); 4372 len += strlength (car (x));
4344 4373
4345 newstr = mk_empty_string (SCHEME_A_ len, ' '); 4374 newstr = mk_empty_string (SCHEME_A_ len, ' ');
4346 4375
4347 /* store the contents of the argument strings into the new string */ 4376 /* store the contents of the argument strings into the new string */
4348 for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x)) 4377 for (pos = strvalue (newstr), x = args; x != NIL; pos += strlength (car (x)), x = cdr (x))
4349 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4378 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4350 4379
4351 s_return (newstr); 4380 s_return (newstr);
4352 } 4381 }
4353 4382
4354 case OP_SUBSTR: /* substring */ 4383 case OP_STRING_COPY: /* substring/string-copy */
4355 { 4384 {
4356 char *str; 4385 char *str = strvalue (x);
4357 int index0; 4386 int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args));
4358 int index1; 4387 int index1;
4359 int len; 4388 int len;
4360 4389
4361 str = strvalue (car (SCHEME_V->args));
4362
4363 index0 = ivalue (cadr (SCHEME_V->args));
4364
4365 if (index0 > strlength (car (SCHEME_V->args))) 4390 if (index0 > strlength (x))
4366 Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); 4391 Error_1 ("string->copy: start out of bounds:", cadr (args));
4367 4392
4368 if (cddr (SCHEME_V->args) != NIL) 4393 if (cddr (args) != NIL)
4369 { 4394 {
4370 index1 = ivalue (caddr (SCHEME_V->args)); 4395 index1 = ivalue_unchecked (caddr (args));
4371 4396
4372 if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) 4397 if (index1 > strlength (x) || index1 < index0)
4373 Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); 4398 Error_1 ("string->copy: end out of bounds:", caddr (args));
4374 } 4399 }
4375 else 4400 else
4376 index1 = strlength (car (SCHEME_V->args)); 4401 index1 = strlength (x);
4377 4402
4378 len = index1 - index0; 4403 len = index1 - index0;
4379 x = mk_empty_string (SCHEME_A_ len, ' '); 4404 x = mk_counted_string (SCHEME_A_ str + index0, len);
4380 memcpy (strvalue (x), str + index0, len);
4381 strvalue (x)[len] = 0;
4382 4405
4383 s_return (x); 4406 s_return (x);
4384 } 4407 }
4385 4408
4386 case OP_VECTOR: /* vector */ 4409 case OP_VECTOR: /* vector */
4387 { 4410 {
4388 int i; 4411 int i;
4389 pointer vec; 4412 pointer vec;
4390 int len = list_length (SCHEME_A_ SCHEME_V->args); 4413 int len = list_length (SCHEME_A_ args);
4391 4414
4392 if (len < 0) 4415 if (len < 0)
4393 Error_1 ("vector: not a proper list:", SCHEME_V->args); 4416 Error_1 ("vector: not a proper list:", args);
4394 4417
4395 vec = mk_vector (SCHEME_A_ len); 4418 vec = mk_vector (SCHEME_A_ len);
4396 4419
4397#if USE_ERROR_CHECKING 4420#if USE_ERROR_CHECKING
4398 if (SCHEME_V->no_memory) 4421 if (SCHEME_V->no_memory)
4399 s_return (S_SINK); 4422 s_return (S_SINK);
4400#endif 4423#endif
4401 4424
4402 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++)
4403 set_vector_elem (vec, i, car (x)); 4426 vector_set (vec, i, car (x));
4404 4427
4405 s_return (vec); 4428 s_return (vec);
4406 } 4429 }
4407 4430
4408 case OP_MKVECTOR: /* make-vector */ 4431 case OP_MKVECTOR: /* make-vector */
4409 { 4432 {
4410 pointer fill = NIL; 4433 pointer fill = NIL;
4411 int len;
4412 pointer vec; 4434 pointer vec;
4435 int len = ivalue_unchecked (x);
4413 4436
4414 len = ivalue (car (SCHEME_V->args));
4415
4416 if (cdr (SCHEME_V->args) != NIL) 4437 if (cdr (args) != NIL)
4417 fill = cadr (SCHEME_V->args); 4438 fill = cadr (args);
4418 4439
4419 vec = mk_vector (SCHEME_A_ len); 4440 vec = mk_vector (SCHEME_A_ len);
4420 4441
4421#if USE_ERROR_CHECKING 4442#if USE_ERROR_CHECKING
4422 if (SCHEME_V->no_memory) 4443 if (SCHEME_V->no_memory)
4423 s_return (S_SINK); 4444 s_return (S_SINK);
4424#endif 4445#endif
4425 4446
4426 if (fill != NIL) 4447 if (fill != NIL)
4427 fill_vector (vec, fill); 4448 fill_vector (vec, 0, fill);
4428 4449
4429 s_return (vec); 4450 s_return (vec);
4430 } 4451 }
4431 4452
4432 case OP_VECLEN: /* vector-length */ 4453 case OP_VECLEN: /* vector-length */
4433 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);
4434 4459
4435 case OP_VECREF: /* vector-ref */ 4460 case OP_VECREF: /* vector-ref */
4436 { 4461 {
4437 int index;
4438
4439 index = ivalue (cadr (SCHEME_V->args)); 4462 int index = ivalue_unchecked (cadr (args));
4440 4463
4441 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) 4464 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4442 Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); 4465 Error_1 ("vector-ref: out of bounds:", cadr (args));
4443 4466
4444 s_return (vector_elem (car (SCHEME_V->args), index)); 4467 s_return (vector_get (x, index));
4445 } 4468 }
4446 4469
4447 case OP_VECSET: /* vector-set! */ 4470 case OP_VECSET: /* vector-set! */
4448 { 4471 {
4449 int index; 4472 int index = ivalue_unchecked (cadr (args));
4450 4473
4451 if (is_immutable (car (SCHEME_V->args))) 4474 if (is_immutable (x))
4452 Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); 4475 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4453 4476
4454 index = ivalue (cadr (SCHEME_V->args));
4455
4456 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) 4477 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4457 Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); 4478 Error_1 ("vector-set!: out of bounds:", cadr (args));
4458 4479
4459 set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); 4480 vector_set (x, index, caddr (args));
4460 s_return (car (SCHEME_V->args)); 4481 s_return (x);
4461 } 4482 }
4462 } 4483 }
4463 4484
4464 return S_T; 4485 if (USE_ERROR_CHECKING) abort ();
4465} 4486}
4466 4487
4467INTERFACE int
4468is_list (SCHEME_P_ pointer a)
4469{
4470 return list_length (SCHEME_A_ a) >= 0;
4471}
4472
4473/* Result is:
4474 proper list: length
4475 circular list: -1
4476 not even a pair: -2
4477 dotted list: -2 minus length before dot
4478*/
4479INTERFACE int
4480list_length (SCHEME_P_ pointer a)
4481{
4482 int i = 0;
4483 pointer slow, fast;
4484
4485 slow = fast = a;
4486
4487 while (1)
4488 {
4489 if (fast == NIL)
4490 return i;
4491
4492 if (!is_pair (fast))
4493 return -2 - i;
4494
4495 fast = cdr (fast);
4496 ++i;
4497
4498 if (fast == NIL)
4499 return i;
4500
4501 if (!is_pair (fast))
4502 return -2 - i;
4503
4504 ++i;
4505 fast = cdr (fast);
4506
4507 /* Safe because we would have already returned if `fast'
4508 encountered a non-pair. */
4509 slow = cdr (slow);
4510
4511 if (fast == slow)
4512 {
4513 /* the fast pointer has looped back around and caught up
4514 with the slow pointer, hence the structure is circular,
4515 not of finite length, and therefore not a list */
4516 return -1;
4517 }
4518 }
4519}
4520
4521static pointer 4488static int
4522opexe_r (SCHEME_P_ enum scheme_opcodes op) 4489opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4523{ 4490{
4524 pointer x = SCHEME_V->args; 4491 pointer x = SCHEME_V->args;
4525 4492
4526 for (;;) 4493 for (;;)
4527 { 4494 {
4547 } 4514 }
4548 4515
4549 s_return (S_T); 4516 s_return (S_T);
4550} 4517}
4551 4518
4552static pointer 4519static int
4553opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4520opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4554{ 4521{
4555 pointer x = SCHEME_V->args; 4522 pointer args = SCHEME_V->args;
4556 pointer a = car (x); 4523 pointer a = car (args);
4557 pointer d = cdr (x); 4524 pointer d = cdr (args);
4558 int r; 4525 int r;
4559 4526
4560 switch (op) 4527 switch (op)
4561 { 4528 {
4562 case OP_NOT: /* not */ r = is_false (a) ; break; 4529 case OP_NOT: /* not */ r = is_false (a) ; break;
4563 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; 4530 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break;
4564 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; 4531 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break;
4565 case OP_NULLP: /* null? */ r = a == NIL ; break; 4532 case OP_NULLP: /* null? */ r = a == NIL ; break;
4566 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; 4533 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break;
4534 case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break;
4567 case OP_NUMBERP: /* number? */ r = is_number (a) ; break; 4535 case OP_NUMBERP: /* number? */ r = is_number (a) ; break;
4568 case OP_STRINGP: /* string? */ r = is_string (a) ; break; 4536 case OP_STRINGP: /* string? */ r = is_string (a) ; break;
4569 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4537 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4570 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4538 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4571 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4539 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4572 4540
4573#if USE_CHAR_CLASSIFIERS 4541#if USE_CHAR_CLASSIFIERS
4574 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4542 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4575 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4543 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4576 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4544 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4577 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4545 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4578 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4546 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4579#endif 4547#endif
4580 4548
4581#if USE_PORTS 4549#if USE_PORTS
4582 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4550 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4583 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4551 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4596 4564
4597 case OP_PAIRP: /* pair? */ r = is_pair (a) ; break; 4565 case OP_PAIRP: /* pair? */ r = is_pair (a) ; break;
4598 case OP_LISTP: /* list? */ r = list_length (SCHEME_A_ a) >= 0; break; 4566 case OP_LISTP: /* list? */ r = list_length (SCHEME_A_ a) >= 0; break;
4599 case OP_ENVP: /* environment? */ r = is_environment (a) ; break; 4567 case OP_ENVP: /* environment? */ r = is_environment (a) ; break;
4600 case OP_VECTORP: /* vector? */ r = is_vector (a) ; break; 4568 case OP_VECTORP: /* vector? */ r = is_vector (a) ; break;
4601 case OP_EQ: /* eq? */ r = a == cadr (x) ; break; 4569 case OP_EQ: /* eq? */ r = a == cadr (args) ; break;
4602 case OP_EQV: /* eqv? */ r = eqv (a, cadr (x)) ; break; 4570 case OP_EQV: /* eqv? */ r = eqv (a, cadr (args)) ; break;
4603 } 4571 }
4604 4572
4605 s_retbool (r); 4573 s_retbool (r);
4606} 4574}
4607 4575
4608static pointer 4576static int
4609opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4577opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4610{ 4578{
4579 pointer args = SCHEME_V->args;
4580 pointer a = car (args);
4611 pointer x, y; 4581 pointer x, y;
4612 4582
4613 switch (op) 4583 switch (op)
4614 { 4584 {
4615 case OP_FORCE: /* force */ 4585 case OP_FORCE: /* force */
4616 SCHEME_V->code = car (SCHEME_V->args); 4586 SCHEME_V->code = a;
4617 4587
4618 if (is_promise (SCHEME_V->code)) 4588 if (is_promise (SCHEME_V->code))
4619 { 4589 {
4620 /* Should change type to closure here */ 4590 /* Should change type to closure here */
4621 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); 4591 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code);
4624 } 4594 }
4625 else 4595 else
4626 s_return (SCHEME_V->code); 4596 s_return (SCHEME_V->code);
4627 4597
4628 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4598 case OP_SAVE_FORCED: /* Save forced value replacing promise */
4629 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); 4599 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
4630 s_return (SCHEME_V->value); 4600 s_return (SCHEME_V->value);
4631 4601
4632#if USE_PORTS 4602#if USE_PORTS
4633 4603
4634 case OP_WRITE: /* write */ 4604 case OP_WRITE: /* write */
4642 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4612 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4643 SCHEME_V->outport = cadr (SCHEME_V->args); 4613 SCHEME_V->outport = cadr (SCHEME_V->args);
4644 } 4614 }
4645 } 4615 }
4646 4616
4647 SCHEME_V->args = car (SCHEME_V->args); 4617 SCHEME_V->args = a;
4648 4618
4649 if (op == OP_WRITE) 4619 if (op == OP_WRITE)
4650 SCHEME_V->print_flag = 1; 4620 SCHEME_V->print_flag = 1;
4651 else 4621 else
4652 SCHEME_V->print_flag = 0; 4622 SCHEME_V->print_flag = 0;
4653 4623
4654 s_goto (OP_P0LIST); 4624 s_goto (OP_P0LIST);
4655 4625
4656 case OP_NEWLINE: /* newline */ 4626 case OP_NEWLINE: /* newline */
4657 if (is_pair (SCHEME_V->args)) 4627 if (is_pair (args))
4658 { 4628 {
4659 if (car (SCHEME_V->args) != SCHEME_V->outport) 4629 if (a != SCHEME_V->outport)
4660 { 4630 {
4661 x = cons (SCHEME_V->outport, NIL); 4631 x = cons (SCHEME_V->outport, NIL);
4662 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4632 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4663 SCHEME_V->outport = car (SCHEME_V->args); 4633 SCHEME_V->outport = a;
4664 } 4634 }
4665 } 4635 }
4666 4636
4667 putstr (SCHEME_A_ "\n"); 4637 putstr (SCHEME_A_ "\n");
4668 s_return (S_T); 4638 s_return (S_T);
4669#endif 4639#endif
4670 4640
4671 case OP_ERR0: /* error */ 4641 case OP_ERR0: /* error */
4672 SCHEME_V->retcode = -1; 4642 SCHEME_V->retcode = -1;
4673 4643
4674 if (!is_string (car (SCHEME_V->args))) 4644 if (!is_string (a))
4675 { 4645 {
4676 SCHEME_V->args = cons (mk_string (SCHEME_A_ " -- "), SCHEME_V->args); 4646 args = cons (mk_string (SCHEME_A_ " -- "), args);
4677 setimmutable (car (SCHEME_V->args)); 4647 setimmutable (car (args));
4678 } 4648 }
4679 4649
4680 putstr (SCHEME_A_ "Error: "); 4650 putstr (SCHEME_A_ "Error: ");
4681 putstr (SCHEME_A_ strvalue (car (SCHEME_V->args))); 4651 putstr (SCHEME_A_ strvalue (car (args)));
4682 SCHEME_V->args = cdr (SCHEME_V->args); 4652 SCHEME_V->args = cdr (args);
4683 s_goto (OP_ERR1); 4653 s_goto (OP_ERR1);
4684 4654
4685 case OP_ERR1: /* error */ 4655 case OP_ERR1: /* error */
4686 putstr (SCHEME_A_ " "); 4656 putstr (SCHEME_A_ " ");
4687 4657
4688 if (SCHEME_V->args != NIL) 4658 if (args != NIL)
4689 { 4659 {
4690 s_save (SCHEME_A_ OP_ERR1, cdr (SCHEME_V->args), NIL); 4660 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4691 SCHEME_V->args = car (SCHEME_V->args); 4661 SCHEME_V->args = a;
4692 SCHEME_V->print_flag = 1; 4662 SCHEME_V->print_flag = 1;
4693 s_goto (OP_P0LIST); 4663 s_goto (OP_P0LIST);
4694 } 4664 }
4695 else 4665 else
4696 { 4666 {
4697 putstr (SCHEME_A_ "\n"); 4667 putstr (SCHEME_A_ "\n");
4698 4668
4699 if (SCHEME_V->interactive_repl) 4669 if (SCHEME_V->interactive_repl)
4700 s_goto (OP_T0LVL); 4670 s_goto (OP_T0LVL);
4701 else 4671 else
4702 return NIL; 4672 return -1;
4703 } 4673 }
4704 4674
4705 case OP_REVERSE: /* reverse */ 4675 case OP_REVERSE: /* reverse */
4706 s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); 4676 s_return (reverse (SCHEME_A_ a));
4707 4677
4708 case OP_LIST_STAR: /* list* */ 4678 case OP_LIST_STAR: /* list* */
4709 s_return (list_star (SCHEME_A_ SCHEME_V->args)); 4679 s_return (list_star (SCHEME_A_ SCHEME_V->args));
4710 4680
4711 case OP_APPEND: /* append */ 4681 case OP_APPEND: /* append */
4712 x = NIL; 4682 x = NIL;
4713 y = SCHEME_V->args; 4683 y = args;
4714 4684
4715 if (y == x) 4685 if (y == x)
4716 s_return (x); 4686 s_return (x);
4717 4687
4718 /* cdr() in the while condition is not a typo. If car() */ 4688 /* cdr() in the while condition is not a typo. If car() */
4729 s_return (reverse_in_place (SCHEME_A_ car (y), x)); 4699 s_return (reverse_in_place (SCHEME_A_ car (y), x));
4730 4700
4731#if USE_PLIST 4701#if USE_PLIST
4732 4702
4733 case OP_PUT: /* put */ 4703 case OP_PUT: /* put */
4734 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 4704 if (!hasprop (a) || !hasprop (cadr (args)))
4735 Error_0 ("illegal use of put"); 4705 Error_0 ("illegal use of put");
4736 4706
4737 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 4707 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
4738 { 4708 {
4739 if (caar (x) == y) 4709 if (caar (x) == y)
4740 break; 4710 break;
4741 } 4711 }
4742 4712
4743 if (x != NIL) 4713 if (x != NIL)
4744 cdar (x) = caddr (SCHEME_V->args); 4714 cdar (x) = caddr (args);
4745 else 4715 else
4746 symprop (car (SCHEME_V->args)) = cons (cons (y, caddr (SCHEME_V->args)), symprop (car (SCHEME_V->args))); 4716 symprop (a) = cons (cons (y, caddr (args)), symprop (a));
4747 4717
4748 s_return (S_T); 4718 s_return (S_T);
4749 4719
4750 case OP_GET: /* get */ 4720 case OP_GET: /* get */
4751 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 4721 if (!hasprop (a) || !hasprop (cadr (args)))
4752 Error_0 ("illegal use of get"); 4722 Error_0 ("illegal use of get");
4753 4723
4754 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 4724 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
4755 if (caar (x) == y) 4725 if (caar (x) == y)
4756 break; 4726 break;
4757 4727
4758 if (x != NIL) 4728 if (x != NIL)
4759 s_return (cdar (x)); 4729 s_return (cdar (x));
4761 s_return (NIL); 4731 s_return (NIL);
4762 4732
4763#endif /* USE_PLIST */ 4733#endif /* USE_PLIST */
4764 4734
4765 case OP_QUIT: /* quit */ 4735 case OP_QUIT: /* quit */
4766 if (is_pair (SCHEME_V->args)) 4736 if (is_pair (args))
4767 SCHEME_V->retcode = ivalue (car (SCHEME_V->args)); 4737 SCHEME_V->retcode = ivalue (a);
4768 4738
4769 return NIL; 4739 return -1;
4770 4740
4771 case OP_GC: /* gc */ 4741 case OP_GC: /* gc */
4772 gc (SCHEME_A_ NIL, NIL); 4742 gc (SCHEME_A_ NIL, NIL);
4773 s_return (S_T); 4743 s_return (S_T);
4774 4744
4775 case OP_GCVERB: /* gc-verbose */ 4745 case OP_GCVERB: /* gc-verbose */
4776 { 4746 {
4777 int was = SCHEME_V->gc_verbose; 4747 int was = SCHEME_V->gc_verbose;
4778 4748
4779 SCHEME_V->gc_verbose = (car (SCHEME_V->args) != S_F); 4749 SCHEME_V->gc_verbose = (a != S_F);
4780 s_retbool (was); 4750 s_retbool (was);
4781 } 4751 }
4782 4752
4783 case OP_NEWSEGMENT: /* new-segment */ 4753 case OP_NEWSEGMENT: /* new-segment */
4754#if 0
4784 if (!is_pair (SCHEME_V->args) || !is_number (car (SCHEME_V->args))) 4755 if (!is_pair (args) || !is_number (a))
4785 Error_0 ("new-segment: argument must be a number"); 4756 Error_0 ("new-segment: argument must be a number");
4786 4757#endif
4787 alloc_cellseg (SCHEME_A_ (int)ivalue (car (SCHEME_V->args))); 4758 s_retbool (alloc_cellseg (SCHEME_A));
4788
4789 s_return (S_T);
4790 4759
4791 case OP_OBLIST: /* oblist */ 4760 case OP_OBLIST: /* oblist */
4792 s_return (oblist_all_symbols (SCHEME_A)); 4761 s_return (oblist_all_symbols (SCHEME_A));
4793 4762
4794#if USE_PORTS 4763#if USE_PORTS
4819 case OP_OPEN_INOUTFILE: 4788 case OP_OPEN_INOUTFILE:
4820 prop = port_input | port_output; 4789 prop = port_input | port_output;
4821 break; 4790 break;
4822 } 4791 }
4823 4792
4824 p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); 4793 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4825 4794
4826 if (p == NIL) 4795 s_return (p == NIL ? S_F : p);
4827 s_return (S_F);
4828
4829 s_return (p);
4830 } 4796 }
4831 4797
4832# if USE_STRING_PORTS 4798# if USE_STRING_PORTS
4833 4799
4834 case OP_OPEN_INSTRING: /* open-input-string */ 4800 case OP_OPEN_INSTRING: /* open-input-string */
4846 case OP_OPEN_INOUTSTRING: 4812 case OP_OPEN_INOUTSTRING:
4847 prop = port_input | port_output; 4813 prop = port_input | port_output;
4848 break; 4814 break;
4849 } 4815 }
4850 4816
4851 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4817 p = port_from_string (SCHEME_A_ strvalue (a),
4852 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); 4818 strvalue (a) + strlength (a), prop);
4853 4819
4854 if (p == NIL) 4820 s_return (p == NIL ? S_F : p);
4855 s_return (S_F);
4856
4857 s_return (p);
4858 } 4821 }
4859 4822
4860 case OP_OPEN_OUTSTRING: /* open-output-string */ 4823 case OP_OPEN_OUTSTRING: /* open-output-string */
4861 { 4824 {
4862 pointer p; 4825 pointer p;
4863 4826
4864 if (car (SCHEME_V->args) == NIL) 4827 if (a == NIL)
4865 {
4866 p = port_from_scratch (SCHEME_A); 4828 p = port_from_scratch (SCHEME_A);
4867
4868 if (p == NIL)
4869 s_return (S_F);
4870 }
4871 else 4829 else
4872 {
4873 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4830 p = port_from_string (SCHEME_A_ strvalue (a),
4874 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); 4831 strvalue (a) + strlength (a), port_output);
4875 4832
4876 if (p == NIL) 4833 s_return (p == NIL ? S_F : p);
4877 s_return (S_F);
4878 }
4879
4880 s_return (p);
4881 } 4834 }
4882 4835
4883 case OP_GET_OUTSTRING: /* get-output-string */ 4836 case OP_GET_OUTSTRING: /* get-output-string */
4884 { 4837 {
4885 port *p; 4838 port *p = port (a);
4886 4839
4887 if ((p = car (SCHEME_V->args)->object.port)->kind & port_string) 4840 if (p->kind & port_string)
4888 { 4841 {
4889 off_t size; 4842 off_t size;
4890 char *str; 4843 char *str;
4891 4844
4892 size = p->rep.string.curr - p->rep.string.start + 1; 4845 size = p->rep.string.curr - p->rep.string.start + 1;
4908 } 4861 }
4909 4862
4910# endif 4863# endif
4911 4864
4912 case OP_CLOSE_INPORT: /* close-input-port */ 4865 case OP_CLOSE_INPORT: /* close-input-port */
4913 port_close (SCHEME_A_ car (SCHEME_V->args), port_input); 4866 port_close (SCHEME_A_ a, port_input);
4914 s_return (S_T); 4867 s_return (S_T);
4915 4868
4916 case OP_CLOSE_OUTPORT: /* close-output-port */ 4869 case OP_CLOSE_OUTPORT: /* close-output-port */
4917 port_close (SCHEME_A_ car (SCHEME_V->args), port_output); 4870 port_close (SCHEME_A_ a, port_output);
4918 s_return (S_T); 4871 s_return (S_T);
4919#endif 4872#endif
4920 4873
4921 case OP_INT_ENV: /* interaction-environment */ 4874 case OP_INT_ENV: /* interaction-environment */
4922 s_return (SCHEME_V->global_env); 4875 s_return (SCHEME_V->global_env);
4924 case OP_CURR_ENV: /* current-environment */ 4877 case OP_CURR_ENV: /* current-environment */
4925 s_return (SCHEME_V->envir); 4878 s_return (SCHEME_V->envir);
4926 4879
4927 } 4880 }
4928 4881
4929 abort (); 4882 if (USE_ERROR_CHECKING) abort ();
4930} 4883}
4931 4884
4932static pointer 4885static int
4933opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4886opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4934{ 4887{
4888 pointer args = SCHEME_V->args;
4935 pointer x; 4889 pointer x;
4936 4890
4937 if (SCHEME_V->nesting != 0) 4891 if (SCHEME_V->nesting != 0)
4938 { 4892 {
4939 int n = SCHEME_V->nesting; 4893 int n = SCHEME_V->nesting;
4946 switch (op) 4900 switch (op)
4947 { 4901 {
4948 /* ========== reading part ========== */ 4902 /* ========== reading part ========== */
4949#if USE_PORTS 4903#if USE_PORTS
4950 case OP_READ: 4904 case OP_READ:
4951 if (!is_pair (SCHEME_V->args)) 4905 if (!is_pair (args))
4952 s_goto (OP_READ_INTERNAL); 4906 s_goto (OP_READ_INTERNAL);
4953 4907
4954 if (!is_inport (car (SCHEME_V->args))) 4908 if (!is_inport (car (args)))
4955 Error_1 ("read: not an input port:", car (SCHEME_V->args)); 4909 Error_1 ("read: not an input port:", car (args));
4956 4910
4957 if (car (SCHEME_V->args) == SCHEME_V->inport) 4911 if (car (args) == SCHEME_V->inport)
4958 s_goto (OP_READ_INTERNAL); 4912 s_goto (OP_READ_INTERNAL);
4959 4913
4960 x = SCHEME_V->inport; 4914 x = SCHEME_V->inport;
4961 SCHEME_V->inport = car (SCHEME_V->args); 4915 SCHEME_V->inport = car (args);
4962 x = cons (x, NIL); 4916 x = cons (x, NIL);
4963 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4917 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
4964 s_goto (OP_READ_INTERNAL); 4918 s_goto (OP_READ_INTERNAL);
4965 4919
4966 case OP_READ_CHAR: /* read-char */ 4920 case OP_READ_CHAR: /* read-char */
4967 case OP_PEEK_CHAR: /* peek-char */ 4921 case OP_PEEK_CHAR: /* peek-char */
4968 { 4922 {
4969 int c; 4923 int c;
4970 4924
4971 if (is_pair (SCHEME_V->args)) 4925 if (is_pair (args))
4972 { 4926 {
4973 if (car (SCHEME_V->args) != SCHEME_V->inport) 4927 if (car (args) != SCHEME_V->inport)
4974 { 4928 {
4975 x = SCHEME_V->inport; 4929 x = SCHEME_V->inport;
4976 x = cons (x, NIL); 4930 x = cons (x, NIL);
4977 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4931 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
4978 SCHEME_V->inport = car (SCHEME_V->args); 4932 SCHEME_V->inport = car (args);
4979 } 4933 }
4980 } 4934 }
4981 4935
4982 c = inchar (SCHEME_A); 4936 c = inchar (SCHEME_A);
4983 4937
4993 case OP_CHAR_READY: /* char-ready? */ 4947 case OP_CHAR_READY: /* char-ready? */
4994 { 4948 {
4995 pointer p = SCHEME_V->inport; 4949 pointer p = SCHEME_V->inport;
4996 int res; 4950 int res;
4997 4951
4998 if (is_pair (SCHEME_V->args)) 4952 if (is_pair (args))
4999 p = car (SCHEME_V->args); 4953 p = car (args);
5000 4954
5001 res = p->object.port->kind & port_string; 4955 res = port (p)->kind & port_string;
5002 4956
5003 s_retbool (res); 4957 s_retbool (res);
5004 } 4958 }
5005 4959
5006 case OP_SET_INPORT: /* set-input-port */ 4960 case OP_SET_INPORT: /* set-input-port */
5007 SCHEME_V->inport = car (SCHEME_V->args); 4961 SCHEME_V->inport = car (args);
5008 s_return (SCHEME_V->value); 4962 s_return (SCHEME_V->value);
5009 4963
5010 case OP_SET_OUTPORT: /* set-output-port */ 4964 case OP_SET_OUTPORT: /* set-output-port */
5011 SCHEME_V->outport = car (SCHEME_V->args); 4965 SCHEME_V->outport = car (args);
5012 s_return (SCHEME_V->value); 4966 s_return (SCHEME_V->value);
5013#endif 4967#endif
5014 4968
5015 case OP_RDSEXPR: 4969 case OP_RDSEXPR:
5016 switch (SCHEME_V->tok) 4970 switch (SCHEME_V->tok)
5065 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 5019 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
5066 SCHEME_V->tok = token (SCHEME_A); 5020 SCHEME_V->tok = token (SCHEME_A);
5067 s_goto (OP_RDSEXPR); 5021 s_goto (OP_RDSEXPR);
5068 5022
5069 case TOK_ATOM: 5023 case TOK_ATOM:
5070 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); 5024 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS)));
5025
5026 case TOK_DOTATOM:
5027 SCHEME_V->strbuff[0] = '.';
5028 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5029
5030 case TOK_STRATOM:
5031 x = readstrexp (SCHEME_A_ '|');
5032 //TODO: haven't checked whether the garbage collector could interfere
5033 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5071 5034
5072 case TOK_DQUOTE: 5035 case TOK_DQUOTE:
5073 x = readstrexp (SCHEME_A); 5036 x = readstrexp (SCHEME_A_ '"');
5074 5037
5075 if (x == S_F) 5038 if (x == S_F)
5076 Error_0 ("Error reading string"); 5039 Error_0 ("Error reading string");
5077 5040
5078 setimmutable (x); 5041 setimmutable (x);
5090 s_goto (OP_EVAL); 5053 s_goto (OP_EVAL);
5091 } 5054 }
5092 } 5055 }
5093 5056
5094 case TOK_SHARP_CONST: 5057 case TOK_SHARP_CONST:
5095 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) 5058 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5096 Error_0 ("undefined sharp expression"); 5059 Error_0 ("undefined sharp expression");
5097 else 5060 else
5098 s_return (x); 5061 s_return (x);
5099 5062
5100 default: 5063 default:
5102 } 5065 }
5103 5066
5104 break; 5067 break;
5105 5068
5106 case OP_RDLIST: 5069 case OP_RDLIST:
5107 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 5070 SCHEME_V->args = cons (SCHEME_V->value, args);
5108 SCHEME_V->tok = token (SCHEME_A); 5071 SCHEME_V->tok = token (SCHEME_A);
5109 5072
5110 switch (SCHEME_V->tok) 5073 switch (SCHEME_V->tok)
5111 { 5074 {
5112 case TOK_EOF: 5075 case TOK_EOF:
5140 case OP_RDDOT: 5103 case OP_RDDOT:
5141 if (token (SCHEME_A) != TOK_RPAREN) 5104 if (token (SCHEME_A) != TOK_RPAREN)
5142 Error_0 ("syntax error: illegal dot expression"); 5105 Error_0 ("syntax error: illegal dot expression");
5143 5106
5144 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5107 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5145 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); 5108 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, args));
5146 5109
5147 case OP_RDQUOTE: 5110 case OP_RDQUOTE:
5148 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); 5111 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)));
5149 5112
5150 case OP_RDQQUOTE: 5113 case OP_RDQQUOTE:
5172 SCHEME_V->args = SCHEME_V->value; 5135 SCHEME_V->args = SCHEME_V->value;
5173 s_goto (OP_VECTOR); 5136 s_goto (OP_VECTOR);
5174 5137
5175 /* ========== printing part ========== */ 5138 /* ========== printing part ========== */
5176 case OP_P0LIST: 5139 case OP_P0LIST:
5177 if (is_vector (SCHEME_V->args)) 5140 if (is_vector (args))
5178 { 5141 {
5179 putstr (SCHEME_A_ "#("); 5142 putstr (SCHEME_A_ "#(");
5180 SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); 5143 SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0));
5181 s_goto (OP_PVECFROM); 5144 s_goto (OP_PVECFROM);
5182 } 5145 }
5183 else if (is_environment (SCHEME_V->args)) 5146 else if (is_environment (args))
5184 { 5147 {
5185 putstr (SCHEME_A_ "#<ENVIRONMENT>"); 5148 putstr (SCHEME_A_ "#<ENVIRONMENT>");
5186 s_return (S_T); 5149 s_return (S_T);
5187 } 5150 }
5188 else if (!is_pair (SCHEME_V->args)) 5151 else if (!is_pair (args))
5189 { 5152 {
5190 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5153 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5191 s_return (S_T); 5154 s_return (S_T);
5192 } 5155 }
5193 else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5156 else
5194 { 5157 {
5158 pointer a = car (args);
5159 pointer b = cdr (args);
5160 int ok_abbr = ok_abbrev (b);
5161 SCHEME_V->args = car (b);
5162
5163 if (a == SCHEME_V->QUOTE && ok_abbr)
5195 putstr (SCHEME_A_ "'"); 5164 putstr (SCHEME_A_ "'");
5196 SCHEME_V->args = cadr (SCHEME_V->args); 5165 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5166 putstr (SCHEME_A_ "`");
5167 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5168 putstr (SCHEME_A_ ",");
5169 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5170 putstr (SCHEME_A_ ",@");
5171 else
5172 {
5173 putstr (SCHEME_A_ "(");
5174 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5175 SCHEME_V->args = a;
5176 }
5177
5197 s_goto (OP_P0LIST); 5178 s_goto (OP_P0LIST);
5198 } 5179 }
5199 else if (car (SCHEME_V->args) == SCHEME_V->QQUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5180
5181 case OP_P1LIST:
5182 if (is_pair (args))
5200 { 5183 {
5184 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5201 putstr (SCHEME_A_ "`"); 5185 putstr (SCHEME_A_ " ");
5202 SCHEME_V->args = cadr (SCHEME_V->args); 5186 SCHEME_V->args = car (args);
5203 s_goto (OP_P0LIST); 5187 s_goto (OP_P0LIST);
5204 } 5188 }
5205 else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTE && ok_abbrev (cdr (SCHEME_V->args)))
5206 {
5207 putstr (SCHEME_A_ ",");
5208 SCHEME_V->args = cadr (SCHEME_V->args);
5209 s_goto (OP_P0LIST);
5210 }
5211 else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTESP && ok_abbrev (cdr (SCHEME_V->args)))
5212 {
5213 putstr (SCHEME_A_ ",@");
5214 SCHEME_V->args = cadr (SCHEME_V->args);
5215 s_goto (OP_P0LIST);
5216 }
5217 else
5218 {
5219 putstr (SCHEME_A_ "(");
5220 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL);
5221 SCHEME_V->args = car (SCHEME_V->args);
5222 s_goto (OP_P0LIST);
5223 }
5224
5225 case OP_P1LIST:
5226 if (is_pair (SCHEME_V->args))
5227 {
5228 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL);
5229 putstr (SCHEME_A_ " ");
5230 SCHEME_V->args = car (SCHEME_V->args);
5231 s_goto (OP_P0LIST);
5232 }
5233 else if (is_vector (SCHEME_V->args)) 5189 else if (is_vector (args))
5234 { 5190 {
5235 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); 5191 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL);
5236 putstr (SCHEME_A_ " . "); 5192 putstr (SCHEME_A_ " . ");
5237 s_goto (OP_P0LIST); 5193 s_goto (OP_P0LIST);
5238 } 5194 }
5239 else 5195 else
5240 { 5196 {
5241 if (SCHEME_V->args != NIL) 5197 if (args != NIL)
5242 { 5198 {
5243 putstr (SCHEME_A_ " . "); 5199 putstr (SCHEME_A_ " . ");
5244 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5200 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5245 } 5201 }
5246 5202
5247 putstr (SCHEME_A_ ")"); 5203 putstr (SCHEME_A_ ")");
5248 s_return (S_T); 5204 s_return (S_T);
5249 } 5205 }
5250 5206
5251 case OP_PVECFROM: 5207 case OP_PVECFROM:
5252 { 5208 {
5253 int i = ivalue_unchecked (cdr (SCHEME_V->args)); 5209 int i = ivalue_unchecked (cdr (args));
5254 pointer vec = car (SCHEME_V->args); 5210 pointer vec = car (args);
5255 int len = veclength (vec); 5211 int len = veclength (vec);
5256 5212
5257 if (i == len) 5213 if (i == len)
5258 { 5214 {
5259 putstr (SCHEME_A_ ")"); 5215 putstr (SCHEME_A_ ")");
5260 s_return (S_T); 5216 s_return (S_T);
5261 } 5217 }
5262 else 5218 else
5263 { 5219 {
5264 pointer elem = vector_elem (vec, i); 5220 pointer elem = vector_get (vec, i);
5265 5221
5266 ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; 5222 ivalue_unchecked (cdr (args)) = i + 1;
5267 s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); 5223 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5268 SCHEME_V->args = elem; 5224 SCHEME_V->args = elem;
5269 5225
5270 if (i > 0) 5226 if (i > 0)
5271 putstr (SCHEME_A_ " "); 5227 putstr (SCHEME_A_ " ");
5272 5228
5273 s_goto (OP_P0LIST); 5229 s_goto (OP_P0LIST);
5274 } 5230 }
5275 } 5231 }
5276 } 5232 }
5277 5233
5278 abort (); 5234 if (USE_ERROR_CHECKING) abort ();
5279} 5235}
5280 5236
5281static pointer 5237static int
5282opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5238opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5283{ 5239{
5240 pointer args = SCHEME_V->args;
5241 pointer a = car (args);
5284 pointer x, y; 5242 pointer x, y;
5285 5243
5286 switch (op) 5244 switch (op)
5287 { 5245 {
5288 case OP_LIST_LENGTH: /* length *//* a.k */ 5246 case OP_LIST_LENGTH: /* length *//* a.k */
5289 { 5247 {
5290 long v = list_length (SCHEME_A_ car (SCHEME_V->args)); 5248 long v = list_length (SCHEME_A_ a);
5291 5249
5292 if (v < 0) 5250 if (v < 0)
5293 Error_1 ("length: not a list:", car (SCHEME_V->args)); 5251 Error_1 ("length: not a list:", a);
5294 5252
5295 s_return (mk_integer (SCHEME_A_ v)); 5253 s_return (mk_integer (SCHEME_A_ v));
5296 } 5254 }
5297 5255
5298 case OP_ASSQ: /* assq *//* a.k */ 5256 case OP_ASSQ: /* assq *//* a.k */
5299 x = car (SCHEME_V->args); 5257 x = a;
5300 5258
5301 for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) 5259 for (y = cadr (args); is_pair (y); y = cdr (y))
5302 { 5260 {
5303 if (!is_pair (car (y))) 5261 if (!is_pair (car (y)))
5304 Error_0 ("unable to handle non pair element"); 5262 Error_0 ("unable to handle non pair element");
5305 5263
5306 if (x == caar (y)) 5264 if (x == caar (y))
5312 else 5270 else
5313 s_return (S_F); 5271 s_return (S_F);
5314 5272
5315 5273
5316 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5274 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5317 SCHEME_V->args = car (SCHEME_V->args); 5275 SCHEME_V->args = a;
5318 5276
5319 if (SCHEME_V->args == NIL) 5277 if (SCHEME_V->args == NIL)
5320 s_return (S_F); 5278 s_return (S_F);
5321 else if (is_closure (SCHEME_V->args)) 5279 else if (is_closure (SCHEME_V->args))
5322 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5280 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5327 5285
5328 case OP_CLOSUREP: /* closure? */ 5286 case OP_CLOSUREP: /* closure? */
5329 /* 5287 /*
5330 * Note, macro object is also a closure. 5288 * Note, macro object is also a closure.
5331 * Therefore, (closure? <#MACRO>) ==> #t 5289 * Therefore, (closure? <#MACRO>) ==> #t
5290 * (schmorp) well, obviously not, fix? TODO
5332 */ 5291 */
5333 s_retbool (is_closure (car (SCHEME_V->args))); 5292 s_retbool (is_closure (a));
5334 5293
5335 case OP_MACROP: /* macro? */ 5294 case OP_MACROP: /* macro? */
5336 s_retbool (is_macro (car (SCHEME_V->args))); 5295 s_retbool (is_macro (a));
5337 } 5296 }
5338 5297
5339 abort (); 5298 if (USE_ERROR_CHECKING) abort ();
5340} 5299}
5341 5300
5301/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5342typedef pointer (*dispatch_func) (SCHEME_P_ enum scheme_opcodes); 5302typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5343 5303
5344typedef int (*test_predicate) (pointer); 5304typedef int (*test_predicate)(pointer);
5345static int 5305static int
5346is_any (pointer p) 5306tst_any (pointer p)
5347{ 5307{
5348 return 1; 5308 return 1;
5349} 5309}
5350 5310
5351static int 5311static int
5352is_nonneg (pointer p) 5312tst_inonneg (pointer p)
5353{ 5313{
5354 return ivalue (p) >= 0 && is_integer (p); 5314 return is_integer (p) && ivalue_unchecked (p) >= 0;
5315}
5316
5317static int
5318tst_is_list (SCHEME_P_ pointer p)
5319{
5320 return p == NIL || is_pair (p);
5355} 5321}
5356 5322
5357/* Correspond carefully with following defines! */ 5323/* Correspond carefully with following defines! */
5358static struct 5324static struct
5359{ 5325{
5360 test_predicate fct; 5326 test_predicate fct;
5361 const char *kind; 5327 const char *kind;
5362} tests[] = 5328} tests[] = {
5363{ 5329 { tst_any , 0 },
5364 { 0, 0}, /* unused */ 5330 { is_string , "string" },
5365 { is_any, 0}, 5331 { is_symbol , "symbol" },
5366 { is_string, "string" }, 5332 { is_port , "port" },
5367 { is_symbol, "symbol" },
5368 { is_port, "port" },
5369 { is_inport, "input port" }, 5333 { is_inport , "input port" },
5370 { is_outport, "output port" }, 5334 { is_outport , "output port" },
5371 { is_environment, "environment" }, 5335 { is_environment, "environment" },
5372 { is_pair, "pair" }, 5336 { is_pair , "pair" },
5373 { 0, "pair or '()" }, 5337 { 0 , "pair or '()" },
5374 { is_character, "character" }, 5338 { is_character , "character" },
5375 { is_vector, "vector" }, 5339 { is_vector , "vector" },
5376 { is_number, "number" }, 5340 { is_number , "number" },
5377 { is_integer, "integer" }, 5341 { is_integer , "integer" },
5378 { is_nonneg, "non-negative integer" } 5342 { tst_inonneg , "non-negative integer" }
5379}; 5343};
5380 5344
5381#define TST_NONE 0 5345#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5382#define TST_ANY "\001" 5346#define TST_ANY "\001"
5383#define TST_STRING "\002" 5347#define TST_STRING "\002"
5384#define TST_SYMBOL "\003" 5348#define TST_SYMBOL "\003"
5385#define TST_PORT "\004" 5349#define TST_PORT "\004"
5386#define TST_INPORT "\005" 5350#define TST_INPORT "\005"
5387#define TST_OUTPORT "\006" 5351#define TST_OUTPORT "\006"
5388#define TST_ENVIRONMENT "\007" 5352#define TST_ENVIRONMENT "\007"
5389#define TST_PAIR "\010" 5353#define TST_PAIR "\010"
5390#define TST_LIST "\011" 5354#define TST_LIST "\011"
5391#define TST_CHAR "\012" 5355#define TST_CHAR "\012"
5392#define TST_VECTOR "\013" 5356#define TST_VECTOR "\013"
5393#define TST_NUMBER "\014" 5357#define TST_NUMBER "\014"
5394#define TST_INTEGER "\015" 5358#define TST_INTEGER "\015"
5395#define TST_NATURAL "\016" 5359#define TST_NATURAL "\016"
5360
5361#define INF_ARG 0xff
5362#define UNNAMED_OP ""
5363
5364static const char opnames[] =
5365#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5366#include "opdefines.h"
5367#undef OP_DEF
5368;
5369
5370static const char *
5371opname (int idx)
5372{
5373 const char *name = opnames;
5374
5375 /* should do this at compile time, but would require external program, right? */
5376 while (idx--)
5377 name += strlen (name) + 1;
5378
5379 return *name ? name : "ILLEGAL";
5380}
5381
5382static const char *
5383procname (pointer x)
5384{
5385 return opname (procnum (x));
5386}
5396 5387
5397typedef struct 5388typedef struct
5398{ 5389{
5399 dispatch_func func; 5390 uint8_t func;
5400 char *name; 5391 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5392 uint8_t builtin;
5393#if USE_ERROR_CHECKING
5401 int min_arity; 5394 uint8_t min_arity;
5402 int max_arity; 5395 uint8_t max_arity;
5403 char *arg_tests_encoding; 5396 char arg_tests_encoding[3];
5397#endif
5404} op_code_info; 5398} op_code_info;
5405 5399
5406#define INF_ARG 0xffff
5407
5408static op_code_info dispatch_table[] = { 5400static const op_code_info dispatch_table[] = {
5409#define OP_DEF(A,B,C,D,E,OP) {A,B,C,D,E}, 5401#if USE_ERROR_CHECKING
5402#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5403#else
5404#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5405#endif
5410#include "opdefines.h" 5406#include "opdefines.h"
5407#undef OP_DEF
5411 {0} 5408 {0}
5412}; 5409};
5413 5410
5414static const char *
5415procname (pointer x)
5416{
5417 int n = procnum (x);
5418 const char *name = dispatch_table[n].name;
5419
5420 if (name == 0)
5421 name = "ILLEGAL!";
5422
5423 return name;
5424}
5425
5426/* kernel of this interpreter */ 5411/* kernel of this interpreter */
5427static void 5412static void ecb_hot
5428Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5413Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5429{ 5414{
5430 SCHEME_V->op = op; 5415 SCHEME_V->op = op;
5431 5416
5432 for (;;) 5417 for (;;)
5433 { 5418 {
5434 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5419 const op_code_info *pcd = dispatch_table + SCHEME_V->op;
5435 5420
5436#if USE_ERROR_CHECKING 5421#if USE_ERROR_CHECKING
5437 if (pcd->name) /* if built-in function, check arguments */ 5422 if (pcd->builtin) /* if built-in function, check arguments */
5438 { 5423 {
5439 int ok = 1;
5440 char msg[STRBUFFSIZE]; 5424 char msg[STRBUFFSIZE];
5441 int n = list_length (SCHEME_A_ SCHEME_V->args); 5425 int n = list_length (SCHEME_A_ SCHEME_V->args);
5442 5426
5443 /* Check number of arguments */ 5427 /* Check number of arguments */
5444 if (ecb_expect_false (n < pcd->min_arity)) 5428 if (ecb_expect_false (n < pcd->min_arity))
5445 { 5429 {
5446 ok = 0;
5447 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5430 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5448 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); 5431 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity);
5432 xError_1 (SCHEME_A_ msg, 0);
5433 continue;
5449 } 5434 }
5450 else if (ecb_excpect_false (n > pcd->max_arity)) 5435 else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG))
5451 { 5436 {
5452 ok = 0;
5453 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5437 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5454 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); 5438 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity);
5439 xError_1 (SCHEME_A_ msg, 0);
5440 continue;
5455 } 5441 }
5456 5442 else
5457 if (ecb_expect_false (ok))
5458 { 5443 {
5459 if (pcd->arg_tests_encoding) 5444 if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */
5460 { 5445 {
5461 int i = 0; 5446 int i = 0;
5462 int j; 5447 int j;
5463 const char *t = pcd->arg_tests_encoding; 5448 const char *t = pcd->arg_tests_encoding;
5464 pointer arglist = SCHEME_V->args; 5449 pointer arglist = SCHEME_V->args;
5465 5450
5466 do 5451 do
5467 { 5452 {
5468 pointer arg = car (arglist); 5453 pointer arg = car (arglist);
5469 5454
5470 j = (int) t[0]; 5455 j = t[0];
5471 5456
5457 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5472 if (j == TST_LIST[0]) 5458 if (j == TST_LIST[0])
5473 { 5459 {
5474 if (arg != NIL && !is_pair (arg)) 5460 if (!tst_is_list (SCHEME_A_ arg))
5475 break; 5461 break;
5476 } 5462 }
5477 else 5463 else
5478 { 5464 {
5479 if (!tests[j].fct (arg)) 5465 if (!tests[j - 1].fct (arg))
5480 break; 5466 break;
5481 } 5467 }
5482 5468
5483 if (t[1] != 0) /* last test is replicated as necessary */ 5469 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5484 t++; 5470 t++;
5485 5471
5486 arglist = cdr (arglist); 5472 arglist = cdr (arglist);
5487 i++; 5473 i++;
5488 } 5474 }
5489 while (i < n); 5475 while (i < n);
5490 5476
5491 if (i < n) 5477 if (i < n)
5492 { 5478 {
5493 ok = 0;
5494 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); 5479 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind);
5480 xError_1 (SCHEME_A_ msg, 0);
5481 continue;
5495 } 5482 }
5496 } 5483 }
5497 } 5484 }
5498
5499 if (!ok)
5500 {
5501 if (xError_1 (SCHEME_A_ msg, 0) == NIL)
5502 return;
5503
5504 pcd = dispatch_table + SCHEME_V->op;
5505 }
5506 } 5485 }
5507#endif 5486#endif
5508 5487
5509 ok_to_freely_gc (SCHEME_A); 5488 ok_to_freely_gc (SCHEME_A);
5510 5489
5490 static const dispatch_func dispatch_funcs[] = {
5491 opexe_0,
5492 opexe_1,
5493 opexe_2,
5494 opexe_3,
5495 opexe_4,
5496 opexe_5,
5497 opexe_6,
5498 };
5499
5511 if (ecb_expect_false (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL)) 5500 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5512 return; 5501 return;
5513 5502
5514 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5503 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5515 { 5504 {
5516 xwrstr ("No memory!\n"); 5505 putstr (SCHEME_A_ "No memory!\n");
5517 return; 5506 return;
5518 } 5507 }
5519 } 5508 }
5520} 5509}
5521 5510
5540mk_proc (SCHEME_P_ enum scheme_opcodes op) 5529mk_proc (SCHEME_P_ enum scheme_opcodes op)
5541{ 5530{
5542 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5531 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5543 set_typeflag (y, (T_PROC | T_ATOM)); 5532 set_typeflag (y, (T_PROC | T_ATOM));
5544 ivalue_unchecked (y) = op; 5533 ivalue_unchecked (y) = op;
5545 set_num_integer (y);
5546 return y; 5534 return y;
5547} 5535}
5548 5536
5549/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5537/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5550static int 5538static int
5551syntaxnum (pointer p) 5539syntaxnum (pointer p)
5552{ 5540{
5553 const char *s = strvalue (car (p)); 5541 const char *s = strvalue (p);
5554 5542
5555 switch (strlength (car (p))) 5543 switch (strlength (p))
5556 { 5544 {
5557 case 2: 5545 case 2:
5558 if (s[0] == 'i') 5546 if (s[0] == 'i')
5559 return OP_IF0; /* if */ 5547 return OP_IF0; /* if */
5560 else 5548 else
5615 return OP_C0STREAM; /* cons-stream */ 5603 return OP_C0STREAM; /* cons-stream */
5616 } 5604 }
5617} 5605}
5618 5606
5619#if USE_MULTIPLICITY 5607#if USE_MULTIPLICITY
5620scheme * 5608ecb_cold scheme *
5621scheme_init_new () 5609scheme_init_new ()
5622{ 5610{
5623 scheme *sc = malloc (sizeof (scheme)); 5611 scheme *sc = malloc (sizeof (scheme));
5624 5612
5625 if (!scheme_init (SCHEME_A)) 5613 if (!scheme_init (SCHEME_A))
5630 else 5618 else
5631 return sc; 5619 return sc;
5632} 5620}
5633#endif 5621#endif
5634 5622
5635int 5623ecb_cold int
5636scheme_init (SCHEME_P) 5624scheme_init (SCHEME_P)
5637{ 5625{
5638 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5626 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5639 pointer x; 5627 pointer x;
5628
5629 /* this memset is not strictly correct, as we assume (intcache)
5630 * that memset 0 will also set pointers to 0, but memset does
5631 * of course not guarantee that. screw such systems.
5632 */
5633 memset (SCHEME_V, 0, sizeof (*SCHEME_V));
5640 5634
5641 num_set_fixnum (num_zero, 1); 5635 num_set_fixnum (num_zero, 1);
5642 num_set_ivalue (num_zero, 0); 5636 num_set_ivalue (num_zero, 0);
5643 num_set_fixnum (num_one, 1); 5637 num_set_fixnum (num_one, 1);
5644 num_set_ivalue (num_one, 1); 5638 num_set_ivalue (num_one, 1);
5656 SCHEME_V->save_inport = NIL; 5650 SCHEME_V->save_inport = NIL;
5657 SCHEME_V->loadport = NIL; 5651 SCHEME_V->loadport = NIL;
5658 SCHEME_V->nesting = 0; 5652 SCHEME_V->nesting = 0;
5659 SCHEME_V->interactive_repl = 0; 5653 SCHEME_V->interactive_repl = 0;
5660 5654
5661 if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) 5655 if (!alloc_cellseg (SCHEME_A))
5662 { 5656 {
5663#if USE_ERROR_CHECKING 5657#if USE_ERROR_CHECKING
5664 SCHEME_V->no_memory = 1; 5658 SCHEME_V->no_memory = 1;
5665 return 0; 5659 return 0;
5666#endif 5660#endif
5713 5707
5714 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) 5708 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
5715 assign_syntax (SCHEME_A_ syntax_names[i]); 5709 assign_syntax (SCHEME_A_ syntax_names[i]);
5716 } 5710 }
5717 5711
5712 // TODO: should iterate via strlen, to avoid n² complexity
5718 for (i = 0; i < n; i++) 5713 for (i = 0; i < n; i++)
5719 if (dispatch_table[i].name != 0) 5714 if (dispatch_table[i].builtin)
5720 assign_proc (SCHEME_A_ i, dispatch_table[i].name); 5715 assign_proc (SCHEME_A_ i, opname (i));
5721 5716
5722 /* initialization of global pointers to special symbols */ 5717 /* initialization of global pointers to special symbols */
5723 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 5718 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
5724 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 5719 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
5725 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 5720 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
5764scheme_set_external_data (SCHEME_P_ void *p) 5759scheme_set_external_data (SCHEME_P_ void *p)
5765{ 5760{
5766 SCHEME_V->ext_data = p; 5761 SCHEME_V->ext_data = p;
5767} 5762}
5768 5763
5769void 5764ecb_cold void
5770scheme_deinit (SCHEME_P) 5765scheme_deinit (SCHEME_P)
5771{ 5766{
5772 int i; 5767 int i;
5773 5768
5774#if SHOW_ERROR_LINE 5769#if SHOW_ERROR_LINE
5866{ 5861{
5867 dump_stack_reset (SCHEME_A); 5862 dump_stack_reset (SCHEME_A);
5868 SCHEME_V->envir = SCHEME_V->global_env; 5863 SCHEME_V->envir = SCHEME_V->global_env;
5869 SCHEME_V->file_i = 0; 5864 SCHEME_V->file_i = 0;
5870 SCHEME_V->load_stack[0].kind = port_input | port_string; 5865 SCHEME_V->load_stack[0].kind = port_input | port_string;
5871 SCHEME_V->load_stack[0].rep.string.start = (char *) cmd; /* This func respects const */ 5866 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */
5872 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *) cmd + strlen (cmd); 5867 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd);
5873 SCHEME_V->load_stack[0].rep.string.curr = (char *) cmd; 5868 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
5874#if USE_PORTS 5869#if USE_PORTS
5875 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 5870 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5876#endif 5871#endif
5877 SCHEME_V->retcode = 0; 5872 SCHEME_V->retcode = 0;
5878 SCHEME_V->interactive_repl = 0; 5873 SCHEME_V->interactive_repl = 0;
5995# endif 5990# endif
5996 int fin; 5991 int fin;
5997 char *file_name = InitFile; 5992 char *file_name = InitFile;
5998 int retcode; 5993 int retcode;
5999 int isfile = 1; 5994 int isfile = 1;
5995 system ("ps v $PPID");//D
6000 5996
6001 if (argc == 2 && strcmp (argv[1], "-?") == 0) 5997 if (argc == 2 && strcmp (argv[1], "-?") == 0)
6002 { 5998 {
6003 xwrstr ("Usage: tinyscheme -?\n"); 5999 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
6004 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6000 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
6005 xwrstr ("followed by\n"); 6001 putstr (SCHEME_A_ "followed by\n");
6006 xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); 6002 putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n");
6007 xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); 6003 putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n");
6008 xwrstr ("assuming that the executable is named tinyscheme.\n"); 6004 putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n");
6009 xwrstr ("Use - as filename for stdin.\n"); 6005 putstr (SCHEME_A_ "Use - as filename for stdin.\n");
6010 return 1; 6006 return 1;
6011 } 6007 }
6012 6008
6013 if (!scheme_init (SCHEME_A)) 6009 if (!scheme_init (SCHEME_A))
6014 { 6010 {
6015 xwrstr ("Could not initialize!\n"); 6011 putstr (SCHEME_A_ "Could not initialize!\n");
6016 return 2; 6012 return 2;
6017 } 6013 }
6018 6014
6019# if USE_PORTS 6015# if USE_PORTS
6020 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); 6016 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO);
6065 fin = open (file_name, O_RDONLY); 6061 fin = open (file_name, O_RDONLY);
6066#endif 6062#endif
6067 6063
6068 if (isfile && fin < 0) 6064 if (isfile && fin < 0)
6069 { 6065 {
6070 xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); 6066 putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n");
6071 } 6067 }
6072 else 6068 else
6073 { 6069 {
6074 if (isfile) 6070 if (isfile)
6075 scheme_load_named_file (SCHEME_A_ fin, file_name); 6071 scheme_load_named_file (SCHEME_A_ fin, file_name);
6079#if USE_PORTS 6075#if USE_PORTS
6080 if (!isfile || fin != STDIN_FILENO) 6076 if (!isfile || fin != STDIN_FILENO)
6081 { 6077 {
6082 if (SCHEME_V->retcode != 0) 6078 if (SCHEME_V->retcode != 0)
6083 { 6079 {
6084 xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); 6080 putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n");
6085 } 6081 }
6086 6082
6087 if (isfile) 6083 if (isfile)
6088 close (fin); 6084 close (fin);
6089 } 6085 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines