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.17 by root, Thu Nov 26 10:15:51 2015 UTC vs.
Revision 1.27 by root, Sat Nov 28 05:13:08 2015 UTC

28#endif 28#endif
29#if USE_MATH 29#if USE_MATH
30# include <math.h> 30# include <math.h>
31#endif 31#endif
32 32
33#include "ecb.h"
34
33#include <sys/types.h> 35#include <sys/types.h>
34#include <sys/stat.h> 36#include <sys/stat.h>
35#include <fcntl.h> 37#include <fcntl.h>
36 38
37#include <string.h> 39#include <string.h>
65#define S_T (&SCHEME_V->xT) //TODO: magic ptr value? 67#define S_T (&SCHEME_V->xT) //TODO: magic ptr value?
66#define S_F (&SCHEME_V->xF) //TODO: magic ptr value? 68#define S_F (&SCHEME_V->xF) //TODO: magic ptr value?
67#define S_SINK (&SCHEME_V->xsink) 69#define S_SINK (&SCHEME_V->xsink)
68#define S_EOF (&SCHEME_V->xEOF_OBJ) 70#define S_EOF (&SCHEME_V->xEOF_OBJ)
69 71
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#else
76# define ecb_expect_false(expr) !!(expr)
77# define ecb_expect_true(expr) !!(expr)
78#endif
79
80#if !USE_MULTIPLICITY 72#if !USE_MULTIPLICITY
81static scheme sc; 73static scheme sc;
82#endif 74#endif
83 75
84static void 76static void
153 145
154#define toupper(c) xtoupper (c) 146#define toupper(c) xtoupper (c)
155#define tolower(c) xtolower (c) 147#define tolower(c) xtolower (c)
156#define isdigit(c) xisdigit (c) 148#define isdigit(c) xisdigit (c)
157 149
158#if USE_STRLWR 150#if USE_IGNORECASE
159static const char * 151static const char *
160strlwr (char *s) 152xstrlwr (char *s)
161{ 153{
162 const char *p = s; 154 const char *p = s;
163 155
164 while (*s) 156 while (*s)
165 { 157 {
167 s++; 159 s++;
168 } 160 }
169 161
170 return p; 162 return p;
171} 163}
172#endif
173 164
165#define stricmp(a,b) strcasecmp (a, b)
166#define strlwr(s) xstrlwr (s)
167
168#else
174#define stricmp(a,b) strcmp (a, b) 169# define stricmp(a,b) strcmp (a, b)
175#define strlwr(s) (s) 170# define strlwr(s) (s)
171#endif
176 172
177#ifndef prompt 173#ifndef prompt
178# define prompt "ts> " 174# define prompt "ts> "
179#endif 175#endif
180 176
186# define FIRST_CELLSEGS 3 182# define FIRST_CELLSEGS 3
187#endif 183#endif
188 184
189enum scheme_types 185enum scheme_types
190{ 186{
187 T_INTEGER,
191 T_FREE, 188 T_REAL,
192 T_STRING, 189 T_STRING,
193 T_NUMBER,
194 T_SYMBOL, 190 T_SYMBOL,
195 T_PROC, 191 T_PROC,
196 T_PAIR, 192 T_PAIR, /* also used for free cells */
197 T_CLOSURE, 193 T_CLOSURE,
198 T_CONTINUATION, 194 T_CONTINUATION,
199 T_FOREIGN, 195 T_FOREIGN,
200 T_CHARACTER, 196 T_CHARACTER,
201 T_PORT, 197 T_PORT,
211#define T_SYNTAX 0x0010 207#define T_SYNTAX 0x0010
212#define T_IMMUTABLE 0x0020 208#define T_IMMUTABLE 0x0020
213#define T_ATOM 0x0040 /* only for gc */ 209#define T_ATOM 0x0040 /* only for gc */
214#define T_MARK 0x0080 /* only for gc */ 210#define T_MARK 0x0080 /* only for gc */
215 211
212/* num, for generic arithmetic */
213struct num
214{
215 IVALUE ivalue;
216#if USE_REAL
217 RVALUE rvalue;
218 char is_fixnum;
219#endif
220};
221
222#if USE_REAL
223# define num_is_fixnum(n) (n).is_fixnum
224# define num_set_fixnum(n,f) (n).is_fixnum = (f)
225# define num_ivalue(n) (n).ivalue
226# define num_rvalue(n) (n).rvalue
227# define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i)
228# define num_set_rvalue(n,r) (n).rvalue = (r)
229#else
230# define num_is_fixnum(n) 1
231# define num_set_fixnum(n,f) 0
232# define num_ivalue(n) (n).ivalue
233# define num_rvalue(n) (n).ivalue
234# define num_set_ivalue(n,i) (n).ivalue = (i)
235# define num_set_rvalue(n,r) (n).ivalue = (r)
236#endif
237
216enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; 238enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
217 239
218static num num_op (enum num_op op, num a, num b); 240static num num_op (enum num_op op, num a, num b);
219static num num_intdiv (num a, num b); 241static num num_intdiv (num a, num b);
220static num num_rem (num a, num b); 242static num num_rem (num a, num b);
223#if USE_MATH 245#if USE_MATH
224static double round_per_R5RS (double x); 246static double round_per_R5RS (double x);
225#endif 247#endif
226static int is_zero_rvalue (RVALUE x); 248static int is_zero_rvalue (RVALUE x);
227 249
228static INLINE int
229num_is_integer (pointer p)
230{
231 return num_is_fixnum (p->object.number);
232}
233
234static num num_zero; 250static num num_zero;
235static num num_one; 251static num num_one;
236 252
237/* macros for cell operations */ 253/* macros for cell operations */
238#define typeflag(p) ((p)->flag + 0) 254#define typeflag(p) ((p)->flag + 0)
239#define set_typeflag(p,v) ((p)->flag = (v)) 255#define set_typeflag(p,v) ((p)->flag = (v))
240#define type(p) (typeflag (p) & T_MASKTYPE) 256#define type(p) (typeflag (p) & T_MASKTYPE)
241 257
242INTERFACE INLINE int 258INTERFACE int
243is_string (pointer p) 259is_string (pointer p)
244{ 260{
245 return type (p) == T_STRING; 261 return type (p) == T_STRING;
246} 262}
247 263
248#define strvalue(p) ((p)->object.string.svalue) 264#define strvalue(p) ((p)->object.string.svalue)
249#define strlength(p) ((p)->object.string.length) 265#define strlength(p) ((p)->object.string.length)
250 266
251INTERFACE int is_list (SCHEME_P_ pointer p);
252INTERFACE INLINE int 267INTERFACE int
253is_vector (pointer p) 268is_vector (pointer p)
254{ 269{
255 return type (p) == T_VECTOR; 270 return type (p) == T_VECTOR;
256} 271}
257 272
266vector_length (pointer vec) 281vector_length (pointer vec)
267{ 282{
268 return vec->object.vector.length; 283 return vec->object.vector.length;
269} 284}
270 285
271INTERFACE INLINE int 286INTERFACE int
287is_integer (pointer p)
288{
289 return type (p) == T_INTEGER;
290}
291
292/* not the same as in scheme, where integers are (correctly :) reals */
293INTERFACE int
294is_real (pointer p)
295{
296 return type (p) == T_REAL;
297}
298
299INTERFACE int
272is_number (pointer p) 300is_number (pointer p)
273{ 301{
274 return type (p) == T_NUMBER; 302 return is_integer (p) || is_real (p);
275} 303}
276 304
277INTERFACE INLINE int 305INTERFACE int
278is_integer (pointer p)
279{
280 if (!is_number (p))
281 return 0;
282
283 if (num_is_integer (p) || ivalue (p) == rvalue (p))
284 return 1;
285
286 return 0;
287}
288
289INTERFACE INLINE int
290is_real (pointer p)
291{
292 return is_number (p) && !num_is_fixnum (p->object.number);
293}
294
295INTERFACE INLINE int
296is_character (pointer p) 306is_character (pointer p)
297{ 307{
298 return type (p) == T_CHARACTER; 308 return type (p) == T_CHARACTER;
299} 309}
300 310
301INTERFACE INLINE char * 311INTERFACE char *
302string_value (pointer p) 312string_value (pointer p)
303{ 313{
304 return strvalue (p); 314 return strvalue (p);
305} 315}
306 316
307INLINE num
308nvalue (pointer p)
309{
310 return (p)->object.number;
311}
312
313static IVALUE
314num_get_ivalue (const num n)
315{
316 return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n);
317}
318
319static RVALUE
320num_get_rvalue (const num n)
321{
322 return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n);
323}
324
325INTERFACE IVALUE
326ivalue (pointer p)
327{
328 return num_get_ivalue (p->object.number);
329}
330
331INTERFACE RVALUE
332rvalue (pointer p)
333{
334 return num_get_rvalue (p->object.number);
335}
336
337#define ivalue_unchecked(p) ((p)->object.number.value.ivalue) 317#define ivalue_unchecked(p) (p)->object.ivalue
318#define set_ivalue(p,v) (p)->object.ivalue = (v)
319
338#if USE_REAL 320#if USE_REAL
339# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 321#define rvalue_unchecked(p) (p)->object.rvalue
340# define set_num_integer(p) (p)->object.number.is_fixnum=1; 322#define set_rvalue(p,v) (p)->object.rvalue = (v)
341# define set_num_real(p) (p)->object.number.is_fixnum=0;
342#else 323#else
343# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 324#define rvalue_unchecked(p) (p)->object.ivalue
344# define set_num_integer(p) 0 325#define set_rvalue(p,v) (p)->object.ivalue = (v)
345# define set_num_real(p) 0
346#endif 326#endif
327
347INTERFACE long 328INTERFACE long
348charvalue (pointer p) 329charvalue (pointer p)
349{ 330{
350 return ivalue_unchecked (p); 331 return ivalue_unchecked (p);
351} 332}
352 333
353INTERFACE INLINE int 334INTERFACE int
354is_port (pointer p) 335is_port (pointer p)
355{ 336{
356 return type (p) == T_PORT; 337 return type (p) == T_PORT;
357} 338}
358 339
359INTERFACE INLINE int 340INTERFACE int
360is_inport (pointer p) 341is_inport (pointer p)
361{ 342{
362 return is_port (p) && p->object.port->kind & port_input; 343 return is_port (p) && p->object.port->kind & port_input;
363} 344}
364 345
365INTERFACE INLINE int 346INTERFACE int
366is_outport (pointer p) 347is_outport (pointer p)
367{ 348{
368 return is_port (p) && p->object.port->kind & port_output; 349 return is_port (p) && p->object.port->kind & port_output;
369} 350}
370 351
371INTERFACE INLINE int 352INTERFACE int
372is_pair (pointer p) 353is_pair (pointer p)
373{ 354{
374 return type (p) == T_PAIR; 355 return type (p) == T_PAIR;
375} 356}
376 357
408pair_cdr (pointer p) 389pair_cdr (pointer p)
409{ 390{
410 return cdr (p); 391 return cdr (p);
411} 392}
412 393
413INTERFACE INLINE int 394INTERFACE int
414is_symbol (pointer p) 395is_symbol (pointer p)
415{ 396{
416 return type (p) == T_SYMBOL; 397 return type (p) == T_SYMBOL;
417} 398}
418 399
419INTERFACE INLINE char * 400INTERFACE char *
420symname (pointer p) 401symname (pointer p)
421{ 402{
422 return strvalue (car (p)); 403 return strvalue (car (p));
423} 404}
424 405
425#if USE_PLIST 406#if USE_PLIST
426SCHEME_EXPORT INLINE int 407SCHEME_EXPORT int
427hasprop (pointer p) 408hasprop (pointer p)
428{ 409{
429 return typeflag (p) & T_SYMBOL; 410 return typeflag (p) & T_SYMBOL;
430} 411}
431 412
432# define symprop(p) cdr(p) 413# define symprop(p) cdr(p)
433#endif 414#endif
434 415
435INTERFACE INLINE int 416INTERFACE int
436is_syntax (pointer p) 417is_syntax (pointer p)
437{ 418{
438 return typeflag (p) & T_SYNTAX; 419 return typeflag (p) & T_SYNTAX;
439} 420}
440 421
441INTERFACE INLINE int 422INTERFACE int
442is_proc (pointer p) 423is_proc (pointer p)
443{ 424{
444 return type (p) == T_PROC; 425 return type (p) == T_PROC;
445} 426}
446 427
447INTERFACE INLINE int 428INTERFACE int
448is_foreign (pointer p) 429is_foreign (pointer p)
449{ 430{
450 return type (p) == T_FOREIGN; 431 return type (p) == T_FOREIGN;
451} 432}
452 433
453INTERFACE INLINE char * 434INTERFACE char *
454syntaxname (pointer p) 435syntaxname (pointer p)
455{ 436{
456 return strvalue (car (p)); 437 return strvalue (car (p));
457} 438}
458 439
459#define procnum(p) ivalue (p) 440#define procnum(p) ivalue_unchecked (p)
460static const char *procname (pointer x); 441static const char *procname (pointer x);
461 442
462INTERFACE INLINE int 443INTERFACE int
463is_closure (pointer p) 444is_closure (pointer p)
464{ 445{
465 return type (p) == T_CLOSURE; 446 return type (p) == T_CLOSURE;
466} 447}
467 448
468INTERFACE INLINE int 449INTERFACE int
469is_macro (pointer p) 450is_macro (pointer p)
470{ 451{
471 return type (p) == T_MACRO; 452 return type (p) == T_MACRO;
472} 453}
473 454
474INTERFACE INLINE pointer 455INTERFACE pointer
475closure_code (pointer p) 456closure_code (pointer p)
476{ 457{
477 return car (p); 458 return car (p);
478} 459}
479 460
480INTERFACE INLINE pointer 461INTERFACE pointer
481closure_env (pointer p) 462closure_env (pointer p)
482{ 463{
483 return cdr (p); 464 return cdr (p);
484} 465}
485 466
486INTERFACE INLINE int 467INTERFACE int
487is_continuation (pointer p) 468is_continuation (pointer p)
488{ 469{
489 return type (p) == T_CONTINUATION; 470 return type (p) == T_CONTINUATION;
490} 471}
491 472
492#define cont_dump(p) cdr (p) 473#define cont_dump(p) cdr (p)
493#define set_cont_dump(p,v) set_cdr ((p), (v)) 474#define set_cont_dump(p,v) set_cdr ((p), (v))
494 475
495/* To do: promise should be forced ONCE only */ 476/* To do: promise should be forced ONCE only */
496INTERFACE INLINE int 477INTERFACE int
497is_promise (pointer p) 478is_promise (pointer p)
498{ 479{
499 return type (p) == T_PROMISE; 480 return type (p) == T_PROMISE;
500} 481}
501 482
502INTERFACE INLINE int 483INTERFACE int
503is_environment (pointer p) 484is_environment (pointer p)
504{ 485{
505 return type (p) == T_ENVIRONMENT; 486 return type (p) == T_ENVIRONMENT;
506} 487}
507 488
513 494
514#define is_mark(p) (typeflag (p) & T_MARK) 495#define is_mark(p) (typeflag (p) & T_MARK)
515#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) 496#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
516#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) 497#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
517 498
518INTERFACE INLINE int 499INTERFACE int
519is_immutable (pointer p) 500is_immutable (pointer p)
520{ 501{
521 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; 502 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
522} 503}
523 504
524INTERFACE INLINE void 505INTERFACE void
525setimmutable (pointer p) 506setimmutable (pointer p)
526{ 507{
527#if USE_ERROR_CHECKING 508#if USE_ERROR_CHECKING
528 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 509 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
529#endif 510#endif
530} 511}
531 512
513/* Result is:
514 proper list: length
515 circular list: -1
516 not even a pair: -2
517 dotted list: -2 minus length before dot
518*/
519INTERFACE int
520list_length (SCHEME_P_ pointer a)
521{
522 int i = 0;
523 pointer slow, fast;
524
525 slow = fast = a;
526
527 while (1)
528 {
529 if (fast == NIL)
530 return i;
531
532 if (!is_pair (fast))
533 return -2 - i;
534
535 fast = cdr (fast);
536 ++i;
537
538 if (fast == NIL)
539 return i;
540
541 if (!is_pair (fast))
542 return -2 - i;
543
544 ++i;
545 fast = cdr (fast);
546
547 /* Safe because we would have already returned if `fast'
548 encountered a non-pair. */
549 slow = cdr (slow);
550
551 if (fast == slow)
552 {
553 /* the fast pointer has looped back around and caught up
554 with the slow pointer, hence the structure is circular,
555 not of finite length, and therefore not a list */
556 return -1;
557 }
558 }
559}
560
561INTERFACE int
562is_list (SCHEME_P_ pointer a)
563{
564 return list_length (SCHEME_A_ a) >= 0;
565}
566
532#if USE_CHAR_CLASSIFIERS 567#if USE_CHAR_CLASSIFIERS
533static INLINE int 568ecb_inline int
534Cisalpha (int c) 569Cisalpha (int c)
535{ 570{
536 return isascii (c) && isalpha (c); 571 return isascii (c) && isalpha (c);
537} 572}
538 573
539static INLINE int 574ecb_inline int
540Cisdigit (int c) 575Cisdigit (int c)
541{ 576{
542 return isascii (c) && isdigit (c); 577 return isascii (c) && isdigit (c);
543} 578}
544 579
545static INLINE int 580ecb_inline int
546Cisspace (int c) 581Cisspace (int c)
547{ 582{
548 return isascii (c) && isspace (c); 583 return isascii (c) && isspace (c);
549} 584}
550 585
551static INLINE int 586ecb_inline int
552Cisupper (int c) 587Cisupper (int c)
553{ 588{
554 return isascii (c) && isupper (c); 589 return isascii (c) && isupper (c);
555} 590}
556 591
557static INLINE int 592ecb_inline int
558Cislower (int c) 593Cislower (int c)
559{ 594{
560 return isascii (c) && islower (c); 595 return isascii (c) && islower (c);
561} 596}
562#endif 597#endif
623#endif 658#endif
624 659
625static int file_push (SCHEME_P_ const char *fname); 660static int file_push (SCHEME_P_ const char *fname);
626static void file_pop (SCHEME_P); 661static void file_pop (SCHEME_P);
627static int file_interactive (SCHEME_P); 662static int file_interactive (SCHEME_P);
628static INLINE int is_one_of (char *s, int c); 663ecb_inline int is_one_of (char *s, int c);
629static int alloc_cellseg (SCHEME_P_ int n); 664static int alloc_cellseg (SCHEME_P_ int n);
630static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); 665ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
631static void finalize_cell (SCHEME_P_ pointer a); 666static void finalize_cell (SCHEME_P_ pointer a);
632static int count_consecutive_cells (pointer x, int needed); 667static int count_consecutive_cells (pointer x, int needed);
633static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 668static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
634static pointer mk_number (SCHEME_P_ const num n); 669static pointer mk_number (SCHEME_P_ const num n);
635static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); 670static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
652static int basic_inchar (port *pt); 687static int basic_inchar (port *pt);
653static int inchar (SCHEME_P); 688static int inchar (SCHEME_P);
654static void backchar (SCHEME_P_ int c); 689static void backchar (SCHEME_P_ int c);
655static char *readstr_upto (SCHEME_P_ char *delim); 690static char *readstr_upto (SCHEME_P_ char *delim);
656static pointer readstrexp (SCHEME_P); 691static pointer readstrexp (SCHEME_P);
657static INLINE int skipspace (SCHEME_P); 692ecb_inline int skipspace (SCHEME_P);
658static int token (SCHEME_P); 693static int token (SCHEME_P);
659static void printslashstring (SCHEME_P_ char *s, int len); 694static void printslashstring (SCHEME_P_ char *s, int len);
660static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 695static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
661static void printatom (SCHEME_P_ pointer l, int f); 696static void printatom (SCHEME_P_ pointer l, int f);
662static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 697static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
666static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); 701static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list);
667static pointer revappend (SCHEME_P_ pointer a, pointer b); 702static pointer revappend (SCHEME_P_ pointer a, pointer b);
668static pointer ss_get_cont (SCHEME_P); 703static pointer ss_get_cont (SCHEME_P);
669static void ss_set_cont (SCHEME_P_ pointer cont); 704static void ss_set_cont (SCHEME_P_ pointer cont);
670static void dump_stack_mark (SCHEME_P); 705static void dump_stack_mark (SCHEME_P);
671static pointer opexe_0 (SCHEME_P_ enum scheme_opcodes op); 706static int opexe_0 (SCHEME_P_ enum scheme_opcodes op);
707static int opexe_1 (SCHEME_P_ enum scheme_opcodes op);
672static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); 708static int opexe_2 (SCHEME_P_ enum scheme_opcodes op);
673static pointer opexe_r (SCHEME_P_ enum scheme_opcodes op);
674static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); 709static int opexe_3 (SCHEME_P_ enum scheme_opcodes op);
675static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); 710static int opexe_4 (SCHEME_P_ enum scheme_opcodes op);
676static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); 711static int opexe_5 (SCHEME_P_ enum scheme_opcodes op);
677static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); 712static int opexe_6 (SCHEME_P_ enum scheme_opcodes op);
678static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 713static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
679static void assign_syntax (SCHEME_P_ const char *name); 714static void assign_syntax (SCHEME_P_ const char *name);
680static int syntaxnum (pointer p); 715static int syntaxnum (pointer p);
681static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 716static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
682 717
718static IVALUE
719ivalue (pointer x)
720{
721 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
722}
723
724static RVALUE
725rvalue (pointer x)
726{
727 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
728}
729
730INTERFACE num
731nvalue (pointer x)
732{
733 num n;
734
735 num_set_fixnum (n, is_integer (x));
736
737 if (num_is_fixnum (n))
738 num_set_ivalue (n, ivalue_unchecked (x));
739 else
740 num_set_rvalue (n, rvalue_unchecked (x));
741
742 return n;
743}
744
683static num 745static num
684num_op (enum num_op op, num a, num b) 746num_op (enum num_op op, num a, num b)
685{ 747{
686 num ret; 748 num ret;
687 749
688 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 750 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
689 751
690 if (num_is_fixnum (ret)) 752 if (num_is_fixnum (ret))
691 { 753 {
692 IVALUE av = num_get_ivalue (a);
693 IVALUE bv = num_get_ivalue (b);
694
695 switch (op) 754 switch (op)
696 { 755 {
697 case NUM_ADD: av += bv; break; 756 case NUM_ADD: a.ivalue += b.ivalue; break;
698 case NUM_SUB: av -= bv; break; 757 case NUM_SUB: a.ivalue -= b.ivalue; break;
699 case NUM_MUL: av *= bv; break; 758 case NUM_MUL: a.ivalue *= b.ivalue; break;
700 case NUM_INTDIV: av /= bv; break; 759 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
701 } 760 }
702 761
703 num_set_ivalue (ret, av); 762 num_set_ivalue (ret, a.ivalue);
704 } 763 }
764#if USE_REAL
705 else 765 else
706 { 766 {
707 RVALUE av = num_get_rvalue (a);
708 RVALUE bv = num_get_rvalue (b);
709
710 switch (op) 767 switch (op)
711 { 768 {
712 case NUM_ADD: av += bv; break; 769 case NUM_ADD: a.rvalue += b.rvalue; break;
713 case NUM_SUB: av -= bv; break; 770 case NUM_SUB: a.rvalue -= b.rvalue; break;
714 case NUM_MUL: av *= bv; break; 771 case NUM_MUL: a.rvalue *= b.rvalue; break;
715 case NUM_INTDIV: av /= bv; break; 772 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
716 } 773 }
717 774
718 num_set_rvalue (ret, av); 775 num_set_rvalue (ret, a.rvalue);
719 } 776 }
777#endif
720 778
721 return ret; 779 return ret;
722} 780}
723 781
724static num 782static num
725num_div (num a, num b) 783num_div (num a, num b)
726{ 784{
727 num ret; 785 num ret;
728 786
729 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); 787 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0);
730 788
731 if (num_is_fixnum (ret)) 789 if (num_is_fixnum (ret))
732 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 790 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
733 else 791 else
734 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 792 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
735 793
736 return ret; 794 return ret;
737} 795}
738 796
739static num 797static num
741{ 799{
742 num ret; 800 num ret;
743 long e1, e2, res; 801 long e1, e2, res;
744 802
745 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 803 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
746 e1 = num_get_ivalue (a); 804 e1 = num_ivalue (a);
747 e2 = num_get_ivalue (b); 805 e2 = num_ivalue (b);
748 res = e1 % e2; 806 res = e1 % e2;
749 807
750 /* remainder should have same sign as second operand */ 808 /* remainder should have same sign as second operand */
751 if (res > 0) 809 if (res > 0)
752 { 810 {
768{ 826{
769 num ret; 827 num ret;
770 long e1, e2, res; 828 long e1, e2, res;
771 829
772 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 830 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
773 e1 = num_get_ivalue (a); 831 e1 = num_ivalue (a);
774 e2 = num_get_ivalue (b); 832 e2 = num_ivalue (b);
775 res = e1 % e2; 833 res = e1 % e2;
776 834
777 /* modulo should have same sign as second operand */ 835 /* modulo should have same sign as second operand */
778 if (res * e2 < 0) 836 if (res * e2 < 0)
779 res += e2; 837 res += e2;
780 838
781 num_set_ivalue (ret, res); 839 num_set_ivalue (ret, res);
782 return ret; 840 return ret;
783} 841}
784 842
785/* this completely disrespects NaNs */ 843/* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */
786static int 844static int
787num_cmp (num a, num b) 845num_cmp (num a, num b)
788{ 846{
789 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 847 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
790 int ret; 848 int ret;
791 849
792 if (is_fixnum) 850 if (is_fixnum)
793 { 851 {
794 IVALUE av = num_get_ivalue (a); 852 IVALUE av = num_ivalue (a);
795 IVALUE bv = num_get_ivalue (b); 853 IVALUE bv = num_ivalue (b);
796 854
797 ret = av == bv ? 0 : av < bv ? -1 : +1; 855 ret = av == bv ? 0 : av < bv ? -1 : +1;
798 } 856 }
799 else 857 else
800 { 858 {
801 RVALUE av = num_get_rvalue (a); 859 RVALUE av = num_rvalue (a);
802 RVALUE bv = num_get_rvalue (b); 860 RVALUE bv = num_rvalue (b);
803 861
804 ret = av == bv ? 0 : av < bv ? -1 : +1; 862 ret = av == bv ? 0 : av < bv ? -1 : +1;
805 } 863 }
806 864
807 return ret; 865 return ret;
833#endif 891#endif
834 892
835static int 893static int
836is_zero_rvalue (RVALUE x) 894is_zero_rvalue (RVALUE x)
837{ 895{
896 return x == 0;
897#if 0
838#if USE_REAL 898#if USE_REAL
839 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ 899 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */
840#else 900#else
841 return x == 0; 901 return x == 0;
902#endif
842#endif 903#endif
843} 904}
844 905
845/* allocate new cell segment */ 906/* allocate new cell segment */
846static int 907static int
891 SCHEME_V->fcells += segsize; 952 SCHEME_V->fcells += segsize;
892 last = newp + segsize - 1; 953 last = newp + segsize - 1;
893 954
894 for (p = newp; p <= last; p++) 955 for (p = newp; p <= last; p++)
895 { 956 {
896 set_typeflag (p, T_FREE); 957 set_typeflag (p, T_PAIR);
897 set_car (p, NIL); 958 set_car (p, NIL);
898 set_cdr (p, p + 1); 959 set_cdr (p, p + 1);
899 } 960 }
900 961
901 /* insert new cells in address order on free list */ 962 /* insert new cells in address order on free list */
918 979
919 return n; 980 return n;
920} 981}
921 982
922/* get new cell. parameter a, b is marked by gc. */ 983/* get new cell. parameter a, b is marked by gc. */
923static INLINE pointer 984ecb_inline pointer
924get_cell_x (SCHEME_P_ pointer a, pointer b) 985get_cell_x (SCHEME_P_ pointer a, pointer b)
925{ 986{
926 if (ecb_expect_false (SCHEME_V->free_cell == NIL)) 987 if (ecb_expect_false (SCHEME_V->free_cell == NIL))
927 { 988 {
928 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 989 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
1006 push_recent_alloc (SCHEME_A_ v, NIL); 1067 push_recent_alloc (SCHEME_A_ v, NIL);
1007 1068
1008 return v; 1069 return v;
1009} 1070}
1010 1071
1011static INLINE void 1072ecb_inline void
1012ok_to_freely_gc (SCHEME_P) 1073ok_to_freely_gc (SCHEME_P)
1013{ 1074{
1014 set_car (S_SINK, NIL); 1075 set_car (S_SINK, NIL);
1015} 1076}
1016 1077
1080 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1141 location = hash_fn (name, veclength (SCHEME_V->oblist));
1081 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); 1142 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location)));
1082 return x; 1143 return x;
1083} 1144}
1084 1145
1085static INLINE pointer 1146ecb_inline pointer
1086oblist_find_by_name (SCHEME_P_ const char *name) 1147oblist_find_by_name (SCHEME_P_ const char *name)
1087{ 1148{
1088 int location; 1149 int location;
1089 pointer x; 1150 pointer x;
1090 char *s; 1151 char *s;
1123oblist_initial_value (SCHEME_P) 1184oblist_initial_value (SCHEME_P)
1124{ 1185{
1125 return NIL; 1186 return NIL;
1126} 1187}
1127 1188
1128static INLINE pointer 1189ecb_inline pointer
1129oblist_find_by_name (SCHEME_P_ const char *name) 1190oblist_find_by_name (SCHEME_P_ const char *name)
1130{ 1191{
1131 pointer x; 1192 pointer x;
1132 char *s; 1193 char *s;
1133 1194
1192mk_character (SCHEME_P_ int c) 1253mk_character (SCHEME_P_ int c)
1193{ 1254{
1194 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1255 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1195 1256
1196 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1257 set_typeflag (x, (T_CHARACTER | T_ATOM));
1197 ivalue_unchecked (x) = c & 0xff; 1258 set_ivalue (x, c & 0xff);
1198 set_num_integer (x); 1259
1199 return x; 1260 return x;
1200} 1261}
1201 1262
1202/* get number atom (integer) */ 1263/* get number atom (integer) */
1203INTERFACE pointer 1264INTERFACE pointer
1204mk_integer (SCHEME_P_ long num) 1265mk_integer (SCHEME_P_ long n)
1205{ 1266{
1206 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1267 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1207 1268
1208 set_typeflag (x, (T_NUMBER | T_ATOM)); 1269 set_typeflag (x, (T_INTEGER | T_ATOM));
1209 ivalue_unchecked (x) = num; 1270 set_ivalue (x, n);
1210 set_num_integer (x); 1271
1211 return x; 1272 return x;
1212} 1273}
1213 1274
1214INTERFACE pointer 1275INTERFACE pointer
1215mk_real (SCHEME_P_ RVALUE n) 1276mk_real (SCHEME_P_ RVALUE n)
1216{ 1277{
1278#if USE_REAL
1217 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1279 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1218 1280
1219 set_typeflag (x, (T_NUMBER | T_ATOM)); 1281 set_typeflag (x, (T_REAL | T_ATOM));
1220 rvalue_unchecked (x) = n; 1282 set_rvalue (x, n);
1221 set_num_real (x); 1283
1222 return x; 1284 return x;
1285#else
1286 return mk_integer (SCHEME_A_ n);
1287#endif
1223} 1288}
1224 1289
1225static pointer 1290static pointer
1226mk_number (SCHEME_P_ const num n) 1291mk_number (SCHEME_P_ const num n)
1227{ 1292{
1293#if USE_REAL
1228 if (num_is_fixnum (n)) 1294 return num_is_fixnum (n)
1295 ? mk_integer (SCHEME_A_ num_ivalue (n))
1296 : mk_real (SCHEME_A_ num_rvalue (n));
1297#else
1229 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1298 return mk_integer (SCHEME_A_ num_ivalue (n));
1230 else 1299#endif
1231 return mk_real (SCHEME_A_ num_get_rvalue (n));
1232} 1300}
1233 1301
1234/* allocate name to string area */ 1302/* allocate name to string area */
1235static char * 1303static char *
1236store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) 1304store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1412 } 1480 }
1413 else if ((c == 'e') || (c == 'E')) 1481 else if ((c == 'e') || (c == 'E'))
1414 { 1482 {
1415 if (!has_fp_exp) 1483 if (!has_fp_exp)
1416 { 1484 {
1417 has_dec_point = 1; /* decimal point illegal 1485 has_dec_point = 1; /* decimal point illegal from now on */
1418 from now on */
1419 p++; 1486 p++;
1420 1487
1421 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1488 if ((*p == '-') || (*p == '+') || isdigit (*p))
1422 continue; 1489 continue;
1423 } 1490 }
1619 if (is_mark (p)) 1686 if (is_mark (p))
1620 clrmark (p); 1687 clrmark (p);
1621 else 1688 else
1622 { 1689 {
1623 /* reclaim cell */ 1690 /* reclaim cell */
1624 if (typeflag (p) != T_FREE) 1691 if (typeflag (p) != T_PAIR)
1625 { 1692 {
1626 finalize_cell (SCHEME_A_ p); 1693 finalize_cell (SCHEME_A_ p);
1627 set_typeflag (p, T_FREE); 1694 set_typeflag (p, T_PAIR);
1628 set_car (p, NIL); 1695 set_car (p, NIL);
1629 } 1696 }
1630 1697
1631 ++SCHEME_V->fcells; 1698 ++SCHEME_V->fcells;
1632 set_cdr (p, SCHEME_V->free_cell); 1699 set_cdr (p, SCHEME_V->free_cell);
1634 } 1701 }
1635 } 1702 }
1636 } 1703 }
1637 1704
1638 if (SCHEME_V->gc_verbose) 1705 if (SCHEME_V->gc_verbose)
1706 {
1639 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1707 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1708 }
1640} 1709}
1641 1710
1642static void 1711static void
1643finalize_cell (SCHEME_P_ pointer a) 1712finalize_cell (SCHEME_P_ pointer a)
1644{ 1713{
2231 } 2300 }
2232 } 2301 }
2233} 2302}
2234 2303
2235/* check c is in chars */ 2304/* check c is in chars */
2236static INLINE int 2305ecb_inline int
2237is_one_of (char *s, int c) 2306is_one_of (char *s, int c)
2238{ 2307{
2239 if (c == EOF) 2308 if (c == EOF)
2240 return 1; 2309 return 1;
2241 2310
2242 return !!strchr (s, c); 2311 return !!strchr (s, c);
2243} 2312}
2244 2313
2245/* skip white characters */ 2314/* skip white characters */
2246static INLINE int 2315ecb_inline int
2247skipspace (SCHEME_P) 2316skipspace (SCHEME_P)
2248{ 2317{
2249 int c, curr_line = 0; 2318 int c, curr_line = 0;
2250 2319
2251 do 2320 do
2479 { 2548 {
2480 p = SCHEME_V->strbuff; 2549 p = SCHEME_V->strbuff;
2481 2550
2482 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2551 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2483 { 2552 {
2484 if (num_is_integer (l)) 2553 if (is_integer (l))
2485 xnum (p, ivalue_unchecked (l)); 2554 xnum (p, ivalue_unchecked (l));
2486#if USE_REAL 2555#if USE_REAL
2487 else 2556 else
2488 { 2557 {
2489 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2558 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2744 return 0; 2813 return 0;
2745 } 2814 }
2746 else if (is_number (a)) 2815 else if (is_number (a))
2747 { 2816 {
2748 if (is_number (b)) 2817 if (is_number (b))
2749 if (num_is_integer (a) == num_is_integer (b))
2750 return num_cmp (nvalue (a), nvalue (b)) == 0; 2818 return num_cmp (nvalue (a), nvalue (b)) == 0;
2751 2819
2752 return 0; 2820 return 0;
2753 } 2821 }
2754 else if (is_character (a)) 2822 else if (is_character (a))
2755 { 2823 {
2822 2890
2823 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2891 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2824 setenvironment (SCHEME_V->envir); 2892 setenvironment (SCHEME_V->envir);
2825} 2893}
2826 2894
2827static INLINE void 2895ecb_inline void
2828new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2896new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2829{ 2897{
2830 pointer slot = immutable_cons (variable, value); 2898 pointer slot = immutable_cons (variable, value);
2831 2899
2832 if (is_vector (car (env))) 2900 if (is_vector (car (env)))
2872 return NIL; 2940 return NIL;
2873} 2941}
2874 2942
2875#else /* USE_ALIST_ENV */ 2943#else /* USE_ALIST_ENV */
2876 2944
2877static INLINE void 2945ecb_inline void
2878new_frame_in_env (SCHEME_P_ pointer old_env) 2946new_frame_in_env (SCHEME_P_ pointer old_env)
2879{ 2947{
2880 SCHEME_V->envir = immutable_cons (NIL, old_env); 2948 SCHEME_V->envir = immutable_cons (NIL, old_env);
2881 setenvironment (SCHEME_V->envir); 2949 setenvironment (SCHEME_V->envir);
2882} 2950}
2883 2951
2884static INLINE void 2952ecb_inline void
2885new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2953new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2886{ 2954{
2887 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2955 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2888} 2956}
2889 2957
2911 return NIL; 2979 return NIL;
2912} 2980}
2913 2981
2914#endif /* USE_ALIST_ENV else */ 2982#endif /* USE_ALIST_ENV else */
2915 2983
2916static INLINE void 2984ecb_inline void
2917new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2985new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2918{ 2986{
2919 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2987 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2920} 2988}
2921 2989
2922static INLINE void 2990ecb_inline void
2923set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2991set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2924{ 2992{
2925 set_cdr (slot, value); 2993 set_cdr (slot, value);
2926} 2994}
2927 2995
2928static INLINE pointer 2996ecb_inline pointer
2929slot_value_in_env (pointer slot) 2997slot_value_in_env (pointer slot)
2930{ 2998{
2931 return cdr (slot); 2999 return cdr (slot);
2932} 3000}
2933 3001
2934/* ========== Evaluation Cycle ========== */ 3002/* ========== Evaluation Cycle ========== */
2935 3003
2936static pointer 3004static int
2937xError_1 (SCHEME_P_ const char *s, pointer a) 3005xError_1 (SCHEME_P_ const char *s, pointer a)
2938{ 3006{
2939#if USE_ERROR_HOOK 3007#if USE_ERROR_HOOK
2940 pointer x; 3008 pointer x;
2941 pointer hdl = SCHEME_V->ERROR_HOOK; 3009 pointer hdl = SCHEME_V->ERROR_HOOK;
2976 code = cons (mk_string (SCHEME_A_ s), code); 3044 code = cons (mk_string (SCHEME_A_ s), code);
2977 setimmutable (car (code)); 3045 setimmutable (car (code));
2978 SCHEME_V->code = cons (slot_value_in_env (x), code); 3046 SCHEME_V->code = cons (slot_value_in_env (x), code);
2979 SCHEME_V->op = OP_EVAL; 3047 SCHEME_V->op = OP_EVAL;
2980 3048
2981 return S_T; 3049 return 0;
2982 } 3050 }
2983#endif 3051#endif
2984 3052
2985 if (a) 3053 if (a)
2986 SCHEME_V->args = cons (a, NIL); 3054 SCHEME_V->args = cons (a, NIL);
2988 SCHEME_V->args = NIL; 3056 SCHEME_V->args = NIL;
2989 3057
2990 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); 3058 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args);
2991 setimmutable (car (SCHEME_V->args)); 3059 setimmutable (car (SCHEME_V->args));
2992 SCHEME_V->op = OP_ERR0; 3060 SCHEME_V->op = OP_ERR0;
3061
2993 return S_T; 3062 return 0;
2994} 3063}
2995 3064
2996#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) 3065#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a)
2997#define Error_0(s) Error_1 (s, 0) 3066#define Error_0(s) Error_1 (s, 0)
2998 3067
2999/* Too small to turn into function */ 3068/* Too small to turn into function */
3000#define BEGIN do { 3069#define BEGIN do {
3001#define END } while (0) 3070#define END } while (0)
3002#define s_goto(a) BEGIN \ 3071#define s_goto(a) BEGIN \
3003 SCHEME_V->op = a; \ 3072 SCHEME_V->op = a; \
3004 return S_T; END 3073 return 0; END
3005 3074
3006#define s_return(a) return xs_return (SCHEME_A_ a) 3075#define s_return(a) return xs_return (SCHEME_A_ a)
3007 3076
3008#ifndef USE_SCHEME_STACK 3077#ifndef USE_SCHEME_STACK
3009 3078
3039 next_frame->code = code; 3108 next_frame->code = code;
3040 3109
3041 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3110 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3042} 3111}
3043 3112
3044static pointer 3113static int
3045xs_return (SCHEME_P_ pointer a) 3114xs_return (SCHEME_P_ pointer a)
3046{ 3115{
3047 int nframes = (uintptr_t)SCHEME_V->dump; 3116 int nframes = (uintptr_t)SCHEME_V->dump;
3048 struct dump_stack_frame *frame; 3117 struct dump_stack_frame *frame;
3049 3118
3050 SCHEME_V->value = a; 3119 SCHEME_V->value = a;
3051 3120
3052 if (nframes <= 0) 3121 if (nframes <= 0)
3053 return NIL; 3122 return -1;
3054 3123
3055 frame = &SCHEME_V->dump_base[--nframes]; 3124 frame = &SCHEME_V->dump_base[--nframes];
3056 SCHEME_V->op = frame->op; 3125 SCHEME_V->op = frame->op;
3057 SCHEME_V->args = frame->args; 3126 SCHEME_V->args = frame->args;
3058 SCHEME_V->envir = frame->envir; 3127 SCHEME_V->envir = frame->envir;
3059 SCHEME_V->code = frame->code; 3128 SCHEME_V->code = frame->code;
3060 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3129 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3061 3130
3062 return S_T; 3131 return 0;
3063} 3132}
3064 3133
3065static INLINE void 3134ecb_inline void
3066dump_stack_reset (SCHEME_P) 3135dump_stack_reset (SCHEME_P)
3067{ 3136{
3068 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3137 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3069 SCHEME_V->dump = (pointer)+0; 3138 SCHEME_V->dump = (pointer)+0;
3070} 3139}
3071 3140
3072static INLINE void 3141ecb_inline void
3073dump_stack_initialize (SCHEME_P) 3142dump_stack_initialize (SCHEME_P)
3074{ 3143{
3075 SCHEME_V->dump_size = 0; 3144 SCHEME_V->dump_size = 0;
3076 SCHEME_V->dump_base = 0; 3145 SCHEME_V->dump_base = 0;
3077 dump_stack_reset (SCHEME_A); 3146 dump_stack_reset (SCHEME_A);
3130 int i = 0; 3199 int i = 0;
3131 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3200 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3132 3201
3133 while (cont != NIL) 3202 while (cont != NIL)
3134 { 3203 {
3135 frame->op = ivalue (car (cont)); cont = cdr (cont); 3204 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3136 frame->args = car (cont) ; cont = cdr (cont); 3205 frame->args = car (cont) ; cont = cdr (cont);
3137 frame->envir = car (cont) ; cont = cdr (cont); 3206 frame->envir = car (cont) ; cont = cdr (cont);
3138 frame->code = car (cont) ; cont = cdr (cont); 3207 frame->code = car (cont) ; cont = cdr (cont);
3139 3208
3140 ++frame; 3209 ++frame;
3141 ++i; 3210 ++i;
3142 } 3211 }
3143 3212
3144 SCHEME_V->dump = (pointer)(uintptr_t)i; 3213 SCHEME_V->dump = (pointer)(uintptr_t)i;
3145} 3214}
3146 3215
3147#else 3216#else
3148 3217
3149static INLINE void 3218ecb_inline void
3150dump_stack_reset (SCHEME_P) 3219dump_stack_reset (SCHEME_P)
3151{ 3220{
3152 SCHEME_V->dump = NIL; 3221 SCHEME_V->dump = NIL;
3153} 3222}
3154 3223
3155static INLINE void 3224ecb_inline void
3156dump_stack_initialize (SCHEME_P) 3225dump_stack_initialize (SCHEME_P)
3157{ 3226{
3158 dump_stack_reset (SCHEME_A); 3227 dump_stack_reset (SCHEME_A);
3159} 3228}
3160 3229
3162dump_stack_free (SCHEME_P) 3231dump_stack_free (SCHEME_P)
3163{ 3232{
3164 SCHEME_V->dump = NIL; 3233 SCHEME_V->dump = NIL;
3165} 3234}
3166 3235
3167static pointer 3236static int
3168xs_return (SCHEME_P_ pointer a) 3237xs_return (SCHEME_P_ pointer a)
3169{ 3238{
3170 pointer dump = SCHEME_V->dump; 3239 pointer dump = SCHEME_V->dump;
3171 3240
3172 SCHEME_V->value = a; 3241 SCHEME_V->value = a;
3173 3242
3174 if (dump == NIL) 3243 if (dump == NIL)
3175 return NIL; 3244 return -1;
3176 3245
3177 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3246 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3178 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3247 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3179 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3248 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3180 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3249 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3181 3250
3182 SCHEME_V->dump = dump; 3251 SCHEME_V->dump = dump;
3183 3252
3184 return S_T; 3253 return 0;
3185} 3254}
3186 3255
3187static void 3256static void
3188s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3257s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3189{ 3258{
3214 3283
3215#endif 3284#endif
3216 3285
3217#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3286#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3218 3287
3219static pointer 3288static int
3220opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3289opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3221{ 3290{
3222 pointer args = SCHEME_V->args; 3291 pointer args = SCHEME_V->args;
3223 pointer x, y; 3292 pointer x, y;
3224 3293
3391 3460
3392 case OP_TRACING: 3461 case OP_TRACING:
3393 { 3462 {
3394 int tr = SCHEME_V->tracing; 3463 int tr = SCHEME_V->tracing;
3395 3464
3396 SCHEME_V->tracing = ivalue (car (args)); 3465 SCHEME_V->tracing = ivalue_unchecked (car (args));
3397 s_return (mk_integer (SCHEME_A_ tr)); 3466 s_return (mk_integer (SCHEME_A_ tr));
3398 } 3467 }
3399 3468
3400#endif 3469#endif
3401 3470
3413 /* fall through */ 3482 /* fall through */
3414 3483
3415 case OP_REAL_APPLY: 3484 case OP_REAL_APPLY:
3416#endif 3485#endif
3417 if (is_proc (SCHEME_V->code)) 3486 if (is_proc (SCHEME_V->code))
3418 {
3419 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3487 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3420 }
3421 else if (is_foreign (SCHEME_V->code)) 3488 else if (is_foreign (SCHEME_V->code))
3422 { 3489 {
3423 /* Keep nested calls from GC'ing the arglist */ 3490 /* Keep nested calls from GC'ing the arglist */
3424 push_recent_alloc (SCHEME_A_ args, NIL); 3491 push_recent_alloc (SCHEME_A_ args, NIL);
3425 x = SCHEME_V->code->object.ff (SCHEME_A_ args); 3492 x = SCHEME_V->code->object.ff (SCHEME_A_ args);
3592 3659
3593 case OP_IF1: /* if */ 3660 case OP_IF1: /* if */
3594 if (is_true (SCHEME_V->value)) 3661 if (is_true (SCHEME_V->value))
3595 SCHEME_V->code = car (SCHEME_V->code); 3662 SCHEME_V->code = car (SCHEME_V->code);
3596 else 3663 else
3597 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because 3664 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
3598
3599 * car(NIL) = NIL */
3600 s_goto (OP_EVAL); 3665 s_goto (OP_EVAL);
3601 3666
3602 case OP_LET0: /* let */ 3667 case OP_LET0: /* let */
3603 SCHEME_V->args = NIL; 3668 SCHEME_V->args = NIL;
3604 SCHEME_V->value = SCHEME_V->code; 3669 SCHEME_V->value = SCHEME_V->code;
3914 SCHEME_V->code = car (args); 3979 SCHEME_V->code = car (args);
3915 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); 3980 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
3916 s_goto (OP_APPLY); 3981 s_goto (OP_APPLY);
3917 } 3982 }
3918 3983
3919 abort (); 3984 if (USE_ERROR_CHECKING) abort ();
3920} 3985}
3921 3986
3922static pointer 3987static int
3923opexe_2 (SCHEME_P_ enum scheme_opcodes op) 3988opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3924{ 3989{
3925 pointer args = SCHEME_V->args; 3990 pointer args = SCHEME_V->args;
3926 pointer x = car (args); 3991 pointer x = car (args);
3927 num v; 3992 num v;
3928 3993
3929#if USE_MATH
3930 RVALUE dd;
3931#endif
3932
3933 switch (op) 3994 switch (op)
3934 { 3995 {
3935#if USE_MATH 3996#if USE_MATH
3936 case OP_INEX2EX: /* inexact->exact */ 3997 case OP_INEX2EX: /* inexact->exact */
3998 {
3937 if (num_is_integer (x)) 3999 if (is_integer (x))
3938 s_return (x); 4000 s_return (x);
3939 else if (modf (rvalue_unchecked (x), &dd) == 0) 4001
4002 RVALUE r = rvalue_unchecked (x);
4003
4004 if (r == (RVALUE)(IVALUE)r)
3940 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4005 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3941 else 4006 else
3942 Error_1 ("inexact->exact: not integral:", x); 4007 Error_1 ("inexact->exact: not integral:", x);
4008 }
3943 4009
3944 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4010 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
3945 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 4011 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))));
3946 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 4012 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
3947 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 4013 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3965 { 4031 {
3966 RVALUE result; 4032 RVALUE result;
3967 int real_result = 1; 4033 int real_result = 1;
3968 pointer y = cadr (args); 4034 pointer y = cadr (args);
3969 4035
3970 if (num_is_integer (x) && num_is_integer (y)) 4036 if (is_integer (x) && is_integer (y))
3971 real_result = 0; 4037 real_result = 0;
3972 4038
3973 /* This 'if' is an R5RS compatibility fix. */ 4039 /* This 'if' is an R5RS compatibility fix. */
3974 /* NOTE: Remove this 'if' fix for R6RS. */ 4040 /* NOTE: Remove this 'if' fix for R6RS. */
3975 if (rvalue (x) == 0 && rvalue (y) < 0) 4041 if (rvalue (x) == 0 && rvalue (y) < 0)
3981 /* If the test fails, result is too big for integer. */ 4047 /* If the test fails, result is too big for integer. */
3982 if (!real_result) 4048 if (!real_result)
3983 { 4049 {
3984 long result_as_long = result; 4050 long result_as_long = result;
3985 4051
3986 if (result != (RVALUE) result_as_long) 4052 if (result != result_as_long)
3987 real_result = 1; 4053 real_result = 1;
3988 } 4054 }
3989 4055
3990 if (real_result) 4056 if (real_result)
3991 s_return (mk_real (SCHEME_A_ result)); 4057 s_return (mk_real (SCHEME_A_ result));
3996 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4062 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
3997 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); 4063 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
3998 4064
3999 case OP_TRUNCATE: 4065 case OP_TRUNCATE:
4000 { 4066 {
4001 RVALUE rvalue_of_x; 4067 RVALUE n = rvalue (x);
4002
4003 rvalue_of_x = rvalue (x);
4004
4005 if (rvalue_of_x > 0)
4006 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); 4068 s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n)));
4007 else
4008 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
4009 } 4069 }
4010 4070
4011 case OP_ROUND: 4071 case OP_ROUND:
4012 if (num_is_integer (x)) 4072 if (is_integer (x))
4013 s_return (x); 4073 s_return (x);
4014 4074
4015 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4075 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4016#endif 4076#endif
4017 4077
4018 case OP_ADD: /* + */ 4078 case OP_ADD: /* + */
4019 v = num_zero; 4079 v = num_zero;
4020 4080
4021 for (x = args; x != NIL; x = cdr (x)) 4081 for (x = args; x != NIL; x = cdr (x))
4022 v = num_op ('+', v, nvalue (car (x))); 4082 v = num_op (NUM_ADD, v, nvalue (car (x)));
4023 4083
4024 s_return (mk_number (SCHEME_A_ v)); 4084 s_return (mk_number (SCHEME_A_ v));
4025 4085
4026 case OP_MUL: /* * */ 4086 case OP_MUL: /* * */
4027 v = num_one; 4087 v = num_one;
4028 4088
4029 for (x = args; x != NIL; x = cdr (x)) 4089 for (x = args; x != NIL; x = cdr (x))
4030 v = num_op ('+', v, nvalue (car (x))); 4090 v = num_op (NUM_MUL, v, nvalue (car (x)));
4031 4091
4032 s_return (mk_number (SCHEME_A_ v)); 4092 s_return (mk_number (SCHEME_A_ v));
4033 4093
4034 case OP_SUB: /* - */ 4094 case OP_SUB: /* - */
4035 if (cdr (args) == NIL) 4095 if (cdr (args) == NIL)
4042 x = cdr (args); 4102 x = cdr (args);
4043 v = nvalue (car (args)); 4103 v = nvalue (car (args));
4044 } 4104 }
4045 4105
4046 for (; x != NIL; x = cdr (x)) 4106 for (; x != NIL; x = cdr (x))
4047 v = num_op ('+', v, nvalue (car (x))); 4107 v = num_op (NUM_SUB, v, nvalue (car (x)));
4048 4108
4049 s_return (mk_number (SCHEME_A_ v)); 4109 s_return (mk_number (SCHEME_A_ v));
4050 4110
4051 case OP_DIV: /* / */ 4111 case OP_DIV: /* / */
4052 if (cdr (args) == NIL) 4112 if (cdr (args) == NIL)
4059 x = cdr (args); 4119 x = cdr (args);
4060 v = nvalue (car (args)); 4120 v = nvalue (car (args));
4061 } 4121 }
4062 4122
4063 for (; x != NIL; x = cdr (x)) 4123 for (; x != NIL; x = cdr (x))
4064 {
4065 if (!is_zero_rvalue (rvalue (car (x)))) 4124 if (!is_zero_rvalue (rvalue (car (x))))
4066 v = num_div (v, nvalue (car (x))); 4125 v = num_div (v, nvalue (car (x)));
4067 else 4126 else
4068 Error_0 ("/: division by zero"); 4127 Error_0 ("/: division by zero");
4069 }
4070 4128
4071 s_return (mk_number (SCHEME_A_ v)); 4129 s_return (mk_number (SCHEME_A_ v));
4072 4130
4073 case OP_INTDIV: /* quotient */ 4131 case OP_INTDIV: /* quotient */
4074 if (cdr (args) == NIL) 4132 if (cdr (args) == NIL)
4083 } 4141 }
4084 4142
4085 for (; x != NIL; x = cdr (x)) 4143 for (; x != NIL; x = cdr (x))
4086 { 4144 {
4087 if (ivalue (car (x)) != 0) 4145 if (ivalue (car (x)) != 0)
4088 v = num_op ('/', v, nvalue (car (x))); 4146 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4089 else 4147 else
4090 Error_0 ("quotient: division by zero"); 4148 Error_0 ("quotient: division by zero");
4091 } 4149 }
4092 4150
4093 s_return (mk_number (SCHEME_A_ v)); 4151 s_return (mk_number (SCHEME_A_ v));
4139 } 4197 }
4140 else 4198 else
4141 Error_0 ("set-cdr!: unable to alter immutable pair"); 4199 Error_0 ("set-cdr!: unable to alter immutable pair");
4142 4200
4143 case OP_CHAR2INT: /* char->integer */ 4201 case OP_CHAR2INT: /* char->integer */
4144 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4202 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4145 4203
4146 case OP_INT2CHAR: /* integer->char */ 4204 case OP_INT2CHAR: /* integer->char */
4147 s_return (mk_character (SCHEME_A_ ivalue (x))); 4205 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4148 4206
4149 case OP_CHARUPCASE: 4207 case OP_CHARUPCASE:
4150 { 4208 {
4151 unsigned char c = ivalue (x); 4209 unsigned char c = ivalue_unchecked (x);
4152 c = toupper (c); 4210 c = toupper (c);
4153 s_return (mk_character (SCHEME_A_ c)); 4211 s_return (mk_character (SCHEME_A_ c));
4154 } 4212 }
4155 4213
4156 case OP_CHARDNCASE: 4214 case OP_CHARDNCASE:
4157 { 4215 {
4158 unsigned char c = ivalue (x); 4216 unsigned char c = ivalue_unchecked (x);
4159 c = tolower (c); 4217 c = tolower (c);
4160 s_return (mk_character (SCHEME_A_ c)); 4218 s_return (mk_character (SCHEME_A_ c));
4161 } 4219 }
4162 4220
4163 case OP_STR2SYM: /* string->symbol */ 4221 case OP_STR2SYM: /* string->symbol */
4240 Error_1 ("atom->string: not an atom:", x); 4298 Error_1 ("atom->string: not an atom:", x);
4241 } 4299 }
4242 4300
4243 case OP_MKSTRING: /* make-string */ 4301 case OP_MKSTRING: /* make-string */
4244 { 4302 {
4245 int fill = ' '; 4303 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4246 int len;
4247
4248 len = ivalue (x); 4304 int len = ivalue_unchecked (x);
4249
4250 if (cdr (args) != NIL)
4251 fill = charvalue (cadr (args));
4252 4305
4253 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4306 s_return (mk_empty_string (SCHEME_A_ len, fill));
4254 } 4307 }
4255 4308
4256 case OP_STRLEN: /* string-length */ 4309 case OP_STRLEN: /* string-length */
4257 s_return (mk_integer (SCHEME_A_ strlength (x))); 4310 s_return (mk_integer (SCHEME_A_ strlength (x)));
4258 4311
4259 case OP_STRREF: /* string-ref */ 4312 case OP_STRREF: /* string-ref */
4260 { 4313 {
4261 char *str;
4262 int index;
4263
4264 str = strvalue (x); 4314 char *str = strvalue (x);
4265
4266 index = ivalue (cadr (args)); 4315 int index = ivalue_unchecked (cadr (args));
4267 4316
4268 if (index >= strlength (x)) 4317 if (index >= strlength (x))
4269 Error_1 ("string-ref: out of bounds:", cadr (args)); 4318 Error_1 ("string-ref: out of bounds:", cadr (args));
4270 4319
4271 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4320 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4272 } 4321 }
4273 4322
4274 case OP_STRSET: /* string-set! */ 4323 case OP_STRSET: /* string-set! */
4275 { 4324 {
4276 char *str; 4325 char *str = strvalue (x);
4277 int index; 4326 int index = ivalue_unchecked (cadr (args));
4278 int c; 4327 int c;
4279 4328
4280 if (is_immutable (x)) 4329 if (is_immutable (x))
4281 Error_1 ("string-set!: unable to alter immutable string:", x); 4330 Error_1 ("string-set!: unable to alter immutable string:", x);
4282
4283 str = strvalue (x);
4284
4285 index = ivalue (cadr (args));
4286 4331
4287 if (index >= strlength (x)) 4332 if (index >= strlength (x))
4288 Error_1 ("string-set!: out of bounds:", cadr (args)); 4333 Error_1 ("string-set!: out of bounds:", cadr (args));
4289 4334
4290 c = charvalue (caddr (args)); 4335 c = charvalue (caddr (args));
4313 s_return (newstr); 4358 s_return (newstr);
4314 } 4359 }
4315 4360
4316 case OP_SUBSTR: /* substring */ 4361 case OP_SUBSTR: /* substring */
4317 { 4362 {
4318 char *str; 4363 char *str = strvalue (x);
4319 int index0; 4364 int index0 = ivalue_unchecked (cadr (args));
4320 int index1; 4365 int index1;
4321 int len; 4366 int len;
4322 4367
4323 str = strvalue (x);
4324
4325 index0 = ivalue (cadr (args));
4326
4327 if (index0 > strlength (x)) 4368 if (index0 > strlength (x))
4328 Error_1 ("substring: start out of bounds:", cadr (args)); 4369 Error_1 ("substring: start out of bounds:", cadr (args));
4329 4370
4330 if (cddr (args) != NIL) 4371 if (cddr (args) != NIL)
4331 { 4372 {
4332 index1 = ivalue (caddr (args)); 4373 index1 = ivalue_unchecked (caddr (args));
4333 4374
4334 if (index1 > strlength (x) || index1 < index0) 4375 if (index1 > strlength (x) || index1 < index0)
4335 Error_1 ("substring: end out of bounds:", caddr (args)); 4376 Error_1 ("substring: end out of bounds:", caddr (args));
4336 } 4377 }
4337 else 4378 else
4368 } 4409 }
4369 4410
4370 case OP_MKVECTOR: /* make-vector */ 4411 case OP_MKVECTOR: /* make-vector */
4371 { 4412 {
4372 pointer fill = NIL; 4413 pointer fill = NIL;
4373 int len;
4374 pointer vec; 4414 pointer vec;
4375
4376 len = ivalue (x); 4415 int len = ivalue_unchecked (x);
4377 4416
4378 if (cdr (args) != NIL) 4417 if (cdr (args) != NIL)
4379 fill = cadr (args); 4418 fill = cadr (args);
4380 4419
4381 vec = mk_vector (SCHEME_A_ len); 4420 vec = mk_vector (SCHEME_A_ len);
4394 case OP_VECLEN: /* vector-length */ 4433 case OP_VECLEN: /* vector-length */
4395 s_return (mk_integer (SCHEME_A_ veclength (x))); 4434 s_return (mk_integer (SCHEME_A_ veclength (x)));
4396 4435
4397 case OP_VECREF: /* vector-ref */ 4436 case OP_VECREF: /* vector-ref */
4398 { 4437 {
4399 int index;
4400
4401 index = ivalue (cadr (args)); 4438 int index = ivalue_unchecked (cadr (args));
4402 4439
4403 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4440 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4404 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4441 Error_1 ("vector-ref: out of bounds:", cadr (args));
4405 4442
4406 s_return (vector_elem (x, index)); 4443 s_return (vector_elem (x, index));
4407 } 4444 }
4408 4445
4409 case OP_VECSET: /* vector-set! */ 4446 case OP_VECSET: /* vector-set! */
4410 { 4447 {
4411 int index; 4448 int index = ivalue_unchecked (cadr (args));
4412 4449
4413 if (is_immutable (x)) 4450 if (is_immutable (x))
4414 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4451 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4415
4416 index = ivalue (cadr (args));
4417 4452
4418 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4453 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4419 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4454 Error_1 ("vector-set!: out of bounds:", cadr (args));
4420 4455
4421 set_vector_elem (x, index, caddr (args)); 4456 set_vector_elem (x, index, caddr (args));
4422 s_return (x); 4457 s_return (x);
4423 } 4458 }
4424 } 4459 }
4425 4460
4426 return S_T; 4461 if (USE_ERROR_CHECKING) abort ();
4427} 4462}
4428 4463
4429INTERFACE int
4430is_list (SCHEME_P_ pointer a)
4431{
4432 return list_length (SCHEME_A_ a) >= 0;
4433}
4434
4435/* Result is:
4436 proper list: length
4437 circular list: -1
4438 not even a pair: -2
4439 dotted list: -2 minus length before dot
4440*/
4441INTERFACE int
4442list_length (SCHEME_P_ pointer a)
4443{
4444 int i = 0;
4445 pointer slow, fast;
4446
4447 slow = fast = a;
4448
4449 while (1)
4450 {
4451 if (fast == NIL)
4452 return i;
4453
4454 if (!is_pair (fast))
4455 return -2 - i;
4456
4457 fast = cdr (fast);
4458 ++i;
4459
4460 if (fast == NIL)
4461 return i;
4462
4463 if (!is_pair (fast))
4464 return -2 - i;
4465
4466 ++i;
4467 fast = cdr (fast);
4468
4469 /* Safe because we would have already returned if `fast'
4470 encountered a non-pair. */
4471 slow = cdr (slow);
4472
4473 if (fast == slow)
4474 {
4475 /* the fast pointer has looped back around and caught up
4476 with the slow pointer, hence the structure is circular,
4477 not of finite length, and therefore not a list */
4478 return -1;
4479 }
4480 }
4481}
4482
4483static pointer 4464static int
4484opexe_r (SCHEME_P_ enum scheme_opcodes op) 4465opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4485{ 4466{
4486 pointer x = SCHEME_V->args; 4467 pointer x = SCHEME_V->args;
4487 4468
4488 for (;;) 4469 for (;;)
4489 { 4470 {
4509 } 4490 }
4510 4491
4511 s_return (S_T); 4492 s_return (S_T);
4512} 4493}
4513 4494
4514static pointer 4495static int
4515opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4496opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4516{ 4497{
4517 pointer args = SCHEME_V->args; 4498 pointer args = SCHEME_V->args;
4518 pointer a = car (args); 4499 pointer a = car (args);
4519 pointer d = cdr (args); 4500 pointer d = cdr (args);
4531 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4512 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4532 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4513 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4533 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4514 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4534 4515
4535#if USE_CHAR_CLASSIFIERS 4516#if USE_CHAR_CLASSIFIERS
4536 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4517 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4537 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4518 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4538 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4519 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4539 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4520 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4540 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4521 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4541#endif 4522#endif
4542 4523
4543#if USE_PORTS 4524#if USE_PORTS
4544 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4525 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4545 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4526 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4565 } 4546 }
4566 4547
4567 s_retbool (r); 4548 s_retbool (r);
4568} 4549}
4569 4550
4570static pointer 4551static int
4571opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4552opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4572{ 4553{
4573 pointer args = SCHEME_V->args; 4554 pointer args = SCHEME_V->args;
4574 pointer a = car (args); 4555 pointer a = car (args);
4575 pointer x, y; 4556 pointer x, y;
4661 putstr (SCHEME_A_ "\n"); 4642 putstr (SCHEME_A_ "\n");
4662 4643
4663 if (SCHEME_V->interactive_repl) 4644 if (SCHEME_V->interactive_repl)
4664 s_goto (OP_T0LVL); 4645 s_goto (OP_T0LVL);
4665 else 4646 else
4666 return NIL; 4647 return -1;
4667 } 4648 }
4668 4649
4669 case OP_REVERSE: /* reverse */ 4650 case OP_REVERSE: /* reverse */
4670 s_return (reverse (SCHEME_A_ a)); 4651 s_return (reverse (SCHEME_A_ a));
4671 4652
4728 4709
4729 case OP_QUIT: /* quit */ 4710 case OP_QUIT: /* quit */
4730 if (is_pair (args)) 4711 if (is_pair (args))
4731 SCHEME_V->retcode = ivalue (a); 4712 SCHEME_V->retcode = ivalue (a);
4732 4713
4733 return NIL; 4714 return -1;
4734 4715
4735 case OP_GC: /* gc */ 4716 case OP_GC: /* gc */
4736 gc (SCHEME_A_ NIL, NIL); 4717 gc (SCHEME_A_ NIL, NIL);
4737 s_return (S_T); 4718 s_return (S_T);
4738 4719
4746 4727
4747 case OP_NEWSEGMENT: /* new-segment */ 4728 case OP_NEWSEGMENT: /* new-segment */
4748 if (!is_pair (args) || !is_number (a)) 4729 if (!is_pair (args) || !is_number (a))
4749 Error_0 ("new-segment: argument must be a number"); 4730 Error_0 ("new-segment: argument must be a number");
4750 4731
4751 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 4732 alloc_cellseg (SCHEME_A_ ivalue (a));
4752 4733
4753 s_return (S_T); 4734 s_return (S_T);
4754 4735
4755 case OP_OBLIST: /* oblist */ 4736 case OP_OBLIST: /* oblist */
4756 s_return (oblist_all_symbols (SCHEME_A)); 4737 s_return (oblist_all_symbols (SCHEME_A));
4785 break; 4766 break;
4786 } 4767 }
4787 4768
4788 p = port_from_filename (SCHEME_A_ strvalue (a), prop); 4769 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4789 4770
4790 if (p == NIL) 4771 s_return (p == NIL ? S_F : p);
4791 s_return (S_F);
4792
4793 s_return (p);
4794 } 4772 }
4795 4773
4796# if USE_STRING_PORTS 4774# if USE_STRING_PORTS
4797 4775
4798 case OP_OPEN_INSTRING: /* open-input-string */ 4776 case OP_OPEN_INSTRING: /* open-input-string */
4813 } 4791 }
4814 4792
4815 p = port_from_string (SCHEME_A_ strvalue (a), 4793 p = port_from_string (SCHEME_A_ strvalue (a),
4816 strvalue (a) + strlength (a), prop); 4794 strvalue (a) + strlength (a), prop);
4817 4795
4818 if (p == NIL) 4796 s_return (p == NIL ? S_F : p);
4819 s_return (S_F);
4820
4821 s_return (p);
4822 } 4797 }
4823 4798
4824 case OP_OPEN_OUTSTRING: /* open-output-string */ 4799 case OP_OPEN_OUTSTRING: /* open-output-string */
4825 { 4800 {
4826 pointer p; 4801 pointer p;
4827 4802
4828 if (a == NIL) 4803 if (a == NIL)
4829 {
4830 p = port_from_scratch (SCHEME_A); 4804 p = port_from_scratch (SCHEME_A);
4831
4832 if (p == NIL)
4833 s_return (S_F);
4834 }
4835 else 4805 else
4836 {
4837 p = port_from_string (SCHEME_A_ strvalue (a), 4806 p = port_from_string (SCHEME_A_ strvalue (a),
4838 strvalue (a) + strlength (a), port_output); 4807 strvalue (a) + strlength (a), port_output);
4839 4808
4840 if (p == NIL) 4809 s_return (p == NIL ? S_F : p);
4841 s_return (S_F);
4842 }
4843
4844 s_return (p);
4845 } 4810 }
4846 4811
4847 case OP_GET_OUTSTRING: /* get-output-string */ 4812 case OP_GET_OUTSTRING: /* get-output-string */
4848 { 4813 {
4849 port *p; 4814 port *p;
4888 case OP_CURR_ENV: /* current-environment */ 4853 case OP_CURR_ENV: /* current-environment */
4889 s_return (SCHEME_V->envir); 4854 s_return (SCHEME_V->envir);
4890 4855
4891 } 4856 }
4892 4857
4893 abort (); 4858 if (USE_ERROR_CHECKING) abort ();
4894} 4859}
4895 4860
4896static pointer 4861static int
4897opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4862opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4898{ 4863{
4864 pointer args = SCHEME_V->args;
4899 pointer x; 4865 pointer x;
4900 4866
4901 if (SCHEME_V->nesting != 0) 4867 if (SCHEME_V->nesting != 0)
4902 { 4868 {
4903 int n = SCHEME_V->nesting; 4869 int n = SCHEME_V->nesting;
4910 switch (op) 4876 switch (op)
4911 { 4877 {
4912 /* ========== reading part ========== */ 4878 /* ========== reading part ========== */
4913#if USE_PORTS 4879#if USE_PORTS
4914 case OP_READ: 4880 case OP_READ:
4915 if (!is_pair (SCHEME_V->args)) 4881 if (!is_pair (args))
4916 s_goto (OP_READ_INTERNAL); 4882 s_goto (OP_READ_INTERNAL);
4917 4883
4918 if (!is_inport (car (SCHEME_V->args))) 4884 if (!is_inport (car (args)))
4919 Error_1 ("read: not an input port:", car (SCHEME_V->args)); 4885 Error_1 ("read: not an input port:", car (args));
4920 4886
4921 if (car (SCHEME_V->args) == SCHEME_V->inport) 4887 if (car (args) == SCHEME_V->inport)
4922 s_goto (OP_READ_INTERNAL); 4888 s_goto (OP_READ_INTERNAL);
4923 4889
4924 x = SCHEME_V->inport; 4890 x = SCHEME_V->inport;
4925 SCHEME_V->inport = car (SCHEME_V->args); 4891 SCHEME_V->inport = car (args);
4926 x = cons (x, NIL); 4892 x = cons (x, NIL);
4927 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4893 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
4928 s_goto (OP_READ_INTERNAL); 4894 s_goto (OP_READ_INTERNAL);
4929 4895
4930 case OP_READ_CHAR: /* read-char */ 4896 case OP_READ_CHAR: /* read-char */
4931 case OP_PEEK_CHAR: /* peek-char */ 4897 case OP_PEEK_CHAR: /* peek-char */
4932 { 4898 {
4933 int c; 4899 int c;
4934 4900
4935 if (is_pair (SCHEME_V->args)) 4901 if (is_pair (args))
4936 { 4902 {
4937 if (car (SCHEME_V->args) != SCHEME_V->inport) 4903 if (car (args) != SCHEME_V->inport)
4938 { 4904 {
4939 x = SCHEME_V->inport; 4905 x = SCHEME_V->inport;
4940 x = cons (x, NIL); 4906 x = cons (x, NIL);
4941 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4907 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
4942 SCHEME_V->inport = car (SCHEME_V->args); 4908 SCHEME_V->inport = car (args);
4943 } 4909 }
4944 } 4910 }
4945 4911
4946 c = inchar (SCHEME_A); 4912 c = inchar (SCHEME_A);
4947 4913
4957 case OP_CHAR_READY: /* char-ready? */ 4923 case OP_CHAR_READY: /* char-ready? */
4958 { 4924 {
4959 pointer p = SCHEME_V->inport; 4925 pointer p = SCHEME_V->inport;
4960 int res; 4926 int res;
4961 4927
4962 if (is_pair (SCHEME_V->args)) 4928 if (is_pair (args))
4963 p = car (SCHEME_V->args); 4929 p = car (args);
4964 4930
4965 res = p->object.port->kind & port_string; 4931 res = p->object.port->kind & port_string;
4966 4932
4967 s_retbool (res); 4933 s_retbool (res);
4968 } 4934 }
4969 4935
4970 case OP_SET_INPORT: /* set-input-port */ 4936 case OP_SET_INPORT: /* set-input-port */
4971 SCHEME_V->inport = car (SCHEME_V->args); 4937 SCHEME_V->inport = car (args);
4972 s_return (SCHEME_V->value); 4938 s_return (SCHEME_V->value);
4973 4939
4974 case OP_SET_OUTPORT: /* set-output-port */ 4940 case OP_SET_OUTPORT: /* set-output-port */
4975 SCHEME_V->outport = car (SCHEME_V->args); 4941 SCHEME_V->outport = car (args);
4976 s_return (SCHEME_V->value); 4942 s_return (SCHEME_V->value);
4977#endif 4943#endif
4978 4944
4979 case OP_RDSEXPR: 4945 case OP_RDSEXPR:
4980 switch (SCHEME_V->tok) 4946 switch (SCHEME_V->tok)
5066 } 5032 }
5067 5033
5068 break; 5034 break;
5069 5035
5070 case OP_RDLIST: 5036 case OP_RDLIST:
5071 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 5037 SCHEME_V->args = cons (SCHEME_V->value, args);
5072 SCHEME_V->tok = token (SCHEME_A); 5038 SCHEME_V->tok = token (SCHEME_A);
5073 5039
5074 switch (SCHEME_V->tok) 5040 switch (SCHEME_V->tok)
5075 { 5041 {
5076 case TOK_EOF: 5042 case TOK_EOF:
5104 case OP_RDDOT: 5070 case OP_RDDOT:
5105 if (token (SCHEME_A) != TOK_RPAREN) 5071 if (token (SCHEME_A) != TOK_RPAREN)
5106 Error_0 ("syntax error: illegal dot expression"); 5072 Error_0 ("syntax error: illegal dot expression");
5107 5073
5108 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5074 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5109 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); 5075 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, args));
5110 5076
5111 case OP_RDQUOTE: 5077 case OP_RDQUOTE:
5112 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); 5078 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)));
5113 5079
5114 case OP_RDQQUOTE: 5080 case OP_RDQQUOTE:
5136 SCHEME_V->args = SCHEME_V->value; 5102 SCHEME_V->args = SCHEME_V->value;
5137 s_goto (OP_VECTOR); 5103 s_goto (OP_VECTOR);
5138 5104
5139 /* ========== printing part ========== */ 5105 /* ========== printing part ========== */
5140 case OP_P0LIST: 5106 case OP_P0LIST:
5141 if (is_vector (SCHEME_V->args)) 5107 if (is_vector (args))
5142 { 5108 {
5143 putstr (SCHEME_A_ "#("); 5109 putstr (SCHEME_A_ "#(");
5144 SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); 5110 SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0));
5145 s_goto (OP_PVECFROM); 5111 s_goto (OP_PVECFROM);
5146 } 5112 }
5147 else if (is_environment (SCHEME_V->args)) 5113 else if (is_environment (args))
5148 { 5114 {
5149 putstr (SCHEME_A_ "#<ENVIRONMENT>"); 5115 putstr (SCHEME_A_ "#<ENVIRONMENT>");
5150 s_return (S_T); 5116 s_return (S_T);
5151 } 5117 }
5152 else if (!is_pair (SCHEME_V->args)) 5118 else if (!is_pair (args))
5153 { 5119 {
5154 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5120 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5155 s_return (S_T); 5121 s_return (S_T);
5156 } 5122 }
5157 else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5123 else
5158 { 5124 {
5125 pointer a = car (args);
5126 pointer b = cdr (args);
5127 int ok_abbr = ok_abbrev (b);
5128 SCHEME_V->args = car (b);
5129
5130 if (a == SCHEME_V->QUOTE && ok_abbr)
5159 putstr (SCHEME_A_ "'"); 5131 putstr (SCHEME_A_ "'");
5160 SCHEME_V->args = cadr (SCHEME_V->args); 5132 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5133 putstr (SCHEME_A_ "`");
5134 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5135 putstr (SCHEME_A_ ",");
5136 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5137 putstr (SCHEME_A_ ",@");
5138 else
5139 {
5140 putstr (SCHEME_A_ "(");
5141 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5142 SCHEME_V->args = a;
5143 }
5144
5161 s_goto (OP_P0LIST); 5145 s_goto (OP_P0LIST);
5162 } 5146 }
5163 else if (car (SCHEME_V->args) == SCHEME_V->QQUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5147
5148 case OP_P1LIST:
5149 if (is_pair (args))
5164 { 5150 {
5151 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5165 putstr (SCHEME_A_ "`"); 5152 putstr (SCHEME_A_ " ");
5166 SCHEME_V->args = cadr (SCHEME_V->args); 5153 SCHEME_V->args = car (args);
5167 s_goto (OP_P0LIST); 5154 s_goto (OP_P0LIST);
5168 } 5155 }
5169 else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTE && ok_abbrev (cdr (SCHEME_V->args)))
5170 {
5171 putstr (SCHEME_A_ ",");
5172 SCHEME_V->args = cadr (SCHEME_V->args);
5173 s_goto (OP_P0LIST);
5174 }
5175 else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTESP && ok_abbrev (cdr (SCHEME_V->args)))
5176 {
5177 putstr (SCHEME_A_ ",@");
5178 SCHEME_V->args = cadr (SCHEME_V->args);
5179 s_goto (OP_P0LIST);
5180 }
5181 else
5182 {
5183 putstr (SCHEME_A_ "(");
5184 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL);
5185 SCHEME_V->args = car (SCHEME_V->args);
5186 s_goto (OP_P0LIST);
5187 }
5188
5189 case OP_P1LIST:
5190 if (is_pair (SCHEME_V->args))
5191 {
5192 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL);
5193 putstr (SCHEME_A_ " ");
5194 SCHEME_V->args = car (SCHEME_V->args);
5195 s_goto (OP_P0LIST);
5196 }
5197 else if (is_vector (SCHEME_V->args)) 5156 else if (is_vector (args))
5198 { 5157 {
5199 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); 5158 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL);
5200 putstr (SCHEME_A_ " . "); 5159 putstr (SCHEME_A_ " . ");
5201 s_goto (OP_P0LIST); 5160 s_goto (OP_P0LIST);
5202 } 5161 }
5203 else 5162 else
5204 { 5163 {
5205 if (SCHEME_V->args != NIL) 5164 if (args != NIL)
5206 { 5165 {
5207 putstr (SCHEME_A_ " . "); 5166 putstr (SCHEME_A_ " . ");
5208 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5167 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5209 } 5168 }
5210 5169
5211 putstr (SCHEME_A_ ")"); 5170 putstr (SCHEME_A_ ")");
5212 s_return (S_T); 5171 s_return (S_T);
5213 } 5172 }
5214 5173
5215 case OP_PVECFROM: 5174 case OP_PVECFROM:
5216 { 5175 {
5217 int i = ivalue_unchecked (cdr (SCHEME_V->args)); 5176 int i = ivalue_unchecked (cdr (args));
5218 pointer vec = car (SCHEME_V->args); 5177 pointer vec = car (args);
5219 int len = veclength (vec); 5178 int len = veclength (vec);
5220 5179
5221 if (i == len) 5180 if (i == len)
5222 { 5181 {
5223 putstr (SCHEME_A_ ")"); 5182 putstr (SCHEME_A_ ")");
5225 } 5184 }
5226 else 5185 else
5227 { 5186 {
5228 pointer elem = vector_elem (vec, i); 5187 pointer elem = vector_elem (vec, i);
5229 5188
5230 ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; 5189 ivalue_unchecked (cdr (args)) = i + 1;
5231 s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); 5190 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5232 SCHEME_V->args = elem; 5191 SCHEME_V->args = elem;
5233 5192
5234 if (i > 0) 5193 if (i > 0)
5235 putstr (SCHEME_A_ " "); 5194 putstr (SCHEME_A_ " ");
5236 5195
5237 s_goto (OP_P0LIST); 5196 s_goto (OP_P0LIST);
5238 } 5197 }
5239 } 5198 }
5240 } 5199 }
5241 5200
5242 abort (); 5201 if (USE_ERROR_CHECKING) abort ();
5243} 5202}
5244 5203
5245static pointer 5204static int
5246opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5205opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5247{ 5206{
5207 pointer args = SCHEME_V->args;
5208 pointer a = car (args);
5248 pointer x, y; 5209 pointer x, y;
5249 5210
5250 switch (op) 5211 switch (op)
5251 { 5212 {
5252 case OP_LIST_LENGTH: /* length *//* a.k */ 5213 case OP_LIST_LENGTH: /* length *//* a.k */
5253 { 5214 {
5254 long v = list_length (SCHEME_A_ car (SCHEME_V->args)); 5215 long v = list_length (SCHEME_A_ a);
5255 5216
5256 if (v < 0) 5217 if (v < 0)
5257 Error_1 ("length: not a list:", car (SCHEME_V->args)); 5218 Error_1 ("length: not a list:", a);
5258 5219
5259 s_return (mk_integer (SCHEME_A_ v)); 5220 s_return (mk_integer (SCHEME_A_ v));
5260 } 5221 }
5261 5222
5262 case OP_ASSQ: /* assq *//* a.k */ 5223 case OP_ASSQ: /* assq *//* a.k */
5263 x = car (SCHEME_V->args); 5224 x = a;
5264 5225
5265 for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) 5226 for (y = cadr (args); is_pair (y); y = cdr (y))
5266 { 5227 {
5267 if (!is_pair (car (y))) 5228 if (!is_pair (car (y)))
5268 Error_0 ("unable to handle non pair element"); 5229 Error_0 ("unable to handle non pair element");
5269 5230
5270 if (x == caar (y)) 5231 if (x == caar (y))
5276 else 5237 else
5277 s_return (S_F); 5238 s_return (S_F);
5278 5239
5279 5240
5280 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5241 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5281 SCHEME_V->args = car (SCHEME_V->args); 5242 SCHEME_V->args = a;
5282 5243
5283 if (SCHEME_V->args == NIL) 5244 if (SCHEME_V->args == NIL)
5284 s_return (S_F); 5245 s_return (S_F);
5285 else if (is_closure (SCHEME_V->args)) 5246 else if (is_closure (SCHEME_V->args))
5286 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5247 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5292 case OP_CLOSUREP: /* closure? */ 5253 case OP_CLOSUREP: /* closure? */
5293 /* 5254 /*
5294 * Note, macro object is also a closure. 5255 * Note, macro object is also a closure.
5295 * Therefore, (closure? <#MACRO>) ==> #t 5256 * Therefore, (closure? <#MACRO>) ==> #t
5296 */ 5257 */
5297 s_retbool (is_closure (car (SCHEME_V->args))); 5258 s_retbool (is_closure (a));
5298 5259
5299 case OP_MACROP: /* macro? */ 5260 case OP_MACROP: /* macro? */
5300 s_retbool (is_macro (car (SCHEME_V->args))); 5261 s_retbool (is_macro (a));
5301 } 5262 }
5302 5263
5303 abort (); 5264 if (USE_ERROR_CHECKING) abort ();
5304} 5265}
5305 5266
5267/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5306typedef pointer (*dispatch_func) (SCHEME_P_ enum scheme_opcodes); 5268typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5307 5269
5308typedef int (*test_predicate) (pointer); 5270typedef int (*test_predicate)(pointer);
5309static int 5271static int
5310is_any (pointer p) 5272tst_any (pointer p)
5311{ 5273{
5312 return 1; 5274 return 1;
5313} 5275}
5314 5276
5315static int 5277static int
5316is_nonneg (pointer p) 5278tst_inonneg (pointer p)
5317{ 5279{
5318 return ivalue (p) >= 0 && is_integer (p); 5280 return is_integer (p) && ivalue_unchecked (p) >= 0;
5281}
5282
5283static int
5284tst_is_list (SCHEME_P_ pointer p)
5285{
5286 return p == NIL || is_pair (p);
5319} 5287}
5320 5288
5321/* Correspond carefully with following defines! */ 5289/* Correspond carefully with following defines! */
5322static struct 5290static struct
5323{ 5291{
5324 test_predicate fct; 5292 test_predicate fct;
5325 const char *kind; 5293 const char *kind;
5326} tests[] = 5294} tests[] = {
5327{ 5295 { tst_any , 0 },
5328 { 0, 0}, /* unused */ 5296 { is_string , "string" },
5329 { is_any, 0}, 5297 { is_symbol , "symbol" },
5330 { is_string, "string" }, 5298 { is_port , "port" },
5331 { is_symbol, "symbol" },
5332 { is_port, "port" },
5333 { is_inport, "input port" }, 5299 { is_inport , "input port" },
5334 { is_outport, "output port" }, 5300 { is_outport , "output port" },
5335 { is_environment, "environment" }, 5301 { is_environment, "environment" },
5336 { is_pair, "pair" }, 5302 { is_pair , "pair" },
5337 { 0, "pair or '()" }, 5303 { 0 , "pair or '()" },
5338 { is_character, "character" }, 5304 { is_character , "character" },
5339 { is_vector, "vector" }, 5305 { is_vector , "vector" },
5340 { is_number, "number" }, 5306 { is_number , "number" },
5341 { is_integer, "integer" }, 5307 { is_integer , "integer" },
5342 { is_nonneg, "non-negative integer" } 5308 { tst_inonneg , "non-negative integer" }
5343}; 5309};
5344 5310
5345#define TST_NONE 0 5311#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5346#define TST_ANY "\001" 5312#define TST_ANY "\001"
5347#define TST_STRING "\002" 5313#define TST_STRING "\002"
5348#define TST_SYMBOL "\003" 5314#define TST_SYMBOL "\003"
5349#define TST_PORT "\004" 5315#define TST_PORT "\004"
5350#define TST_INPORT "\005" 5316#define TST_INPORT "\005"
5351#define TST_OUTPORT "\006" 5317#define TST_OUTPORT "\006"
5352#define TST_ENVIRONMENT "\007" 5318#define TST_ENVIRONMENT "\007"
5353#define TST_PAIR "\010" 5319#define TST_PAIR "\010"
5354#define TST_LIST "\011" 5320#define TST_LIST "\011"
5355#define TST_CHAR "\012" 5321#define TST_CHAR "\012"
5356#define TST_VECTOR "\013" 5322#define TST_VECTOR "\013"
5357#define TST_NUMBER "\014" 5323#define TST_NUMBER "\014"
5358#define TST_INTEGER "\015" 5324#define TST_INTEGER "\015"
5359#define TST_NATURAL "\016" 5325#define TST_NATURAL "\016"
5326
5327#define INF_ARG 0xff
5328#define UNNAMED_OP ""
5329
5330static const char opnames[] =
5331#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5332#include "opdefines.h"
5333#undef OP_DEF
5334;
5335
5336static const char *
5337opname (int idx)
5338{
5339 const char *name = opnames;
5340
5341 /* should do this at compile time, but would require external program, right? */
5342 while (idx--)
5343 name += strlen (name) + 1;
5344
5345 return *name ? name : "ILLEGAL";
5346}
5347
5348static const char *
5349procname (pointer x)
5350{
5351 return opname (procnum (x));
5352}
5360 5353
5361typedef struct 5354typedef struct
5362{ 5355{
5363 dispatch_func func; 5356 uint8_t func;
5364 char *name; 5357 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5358 uint8_t builtin;
5359#if USE_ERROR_CHECKING
5365 int min_arity; 5360 uint8_t min_arity;
5366 int max_arity; 5361 uint8_t max_arity;
5367 char *arg_tests_encoding; 5362 char arg_tests_encoding[3];
5363#endif
5368} op_code_info; 5364} op_code_info;
5369 5365
5370#define INF_ARG 0xffff
5371
5372static op_code_info dispatch_table[] = { 5366static const op_code_info dispatch_table[] = {
5373#define OP_DEF(A,B,C,D,E,OP) {A,B,C,D,E}, 5367#if USE_ERROR_CHECKING
5368#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5369#else
5370#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5371#endif
5374#include "opdefines.h" 5372#include "opdefines.h"
5373#undef OP_DEF
5375 {0} 5374 {0}
5376}; 5375};
5377 5376
5378static const char *
5379procname (pointer x)
5380{
5381 int n = procnum (x);
5382 const char *name = dispatch_table[n].name;
5383
5384 if (name == 0)
5385 name = "ILLEGAL!";
5386
5387 return name;
5388}
5389
5390/* kernel of this interpreter */ 5377/* kernel of this interpreter */
5391static void 5378static void ecb_hot
5392Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5379Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5393{ 5380{
5394 SCHEME_V->op = op; 5381 SCHEME_V->op = op;
5395 5382
5396 for (;;) 5383 for (;;)
5397 { 5384 {
5398 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5385 const op_code_info *pcd = dispatch_table + SCHEME_V->op;
5399 5386
5400#if USE_ERROR_CHECKING 5387#if USE_ERROR_CHECKING
5401 if (pcd->name) /* if built-in function, check arguments */ 5388 if (pcd->builtin) /* if built-in function, check arguments */
5402 { 5389 {
5403 int ok = 1;
5404 char msg[STRBUFFSIZE]; 5390 char msg[STRBUFFSIZE];
5405 int n = list_length (SCHEME_A_ SCHEME_V->args); 5391 int n = list_length (SCHEME_A_ SCHEME_V->args);
5406 5392
5407 /* Check number of arguments */ 5393 /* Check number of arguments */
5408 if (ecb_expect_false (n < pcd->min_arity)) 5394 if (ecb_expect_false (n < pcd->min_arity))
5409 { 5395 {
5410 ok = 0;
5411 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5396 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5412 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); 5397 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity);
5398 xError_1 (SCHEME_A_ msg, 0);
5399 continue;
5413 } 5400 }
5414 else if (ecb_expect_false (n > pcd->max_arity)) 5401 else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG))
5415 { 5402 {
5416 ok = 0;
5417 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5403 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5418 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); 5404 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity);
5405 xError_1 (SCHEME_A_ msg, 0);
5406 continue;
5419 } 5407 }
5420 5408 else
5421 if (ecb_expect_false (ok))
5422 { 5409 {
5423 if (pcd->arg_tests_encoding) 5410 if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */
5424 { 5411 {
5425 int i = 0; 5412 int i = 0;
5426 int j; 5413 int j;
5427 const char *t = pcd->arg_tests_encoding; 5414 const char *t = pcd->arg_tests_encoding;
5428 pointer arglist = SCHEME_V->args; 5415 pointer arglist = SCHEME_V->args;
5429 5416
5430 do 5417 do
5431 { 5418 {
5432 pointer arg = car (arglist); 5419 pointer arg = car (arglist);
5433 5420
5434 j = (int) t[0]; 5421 j = t[0];
5435 5422
5423 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5436 if (j == TST_LIST[0]) 5424 if (j == TST_LIST[0])
5437 { 5425 {
5438 if (arg != NIL && !is_pair (arg)) 5426 if (!tst_is_list (SCHEME_A_ arg))
5439 break; 5427 break;
5440 } 5428 }
5441 else 5429 else
5442 { 5430 {
5443 if (!tests[j].fct (arg)) 5431 if (!tests[j - 1].fct (arg))
5444 break; 5432 break;
5445 } 5433 }
5446 5434
5447 if (t[1] != 0) /* last test is replicated as necessary */ 5435 if (t[1]) /* last test is replicated as necessary */
5448 t++; 5436 t++;
5449 5437
5450 arglist = cdr (arglist); 5438 arglist = cdr (arglist);
5451 i++; 5439 i++;
5452 } 5440 }
5453 while (i < n); 5441 while (i < n);
5454 5442
5455 if (i < n) 5443 if (i < n)
5456 { 5444 {
5457 ok = 0;
5458 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); 5445 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind);
5446 xError_1 (SCHEME_A_ msg, 0);
5447 continue;
5459 } 5448 }
5460 } 5449 }
5461 } 5450 }
5462
5463 if (!ok)
5464 {
5465 if (xError_1 (SCHEME_A_ msg, 0) == NIL)
5466 return;
5467
5468 pcd = dispatch_table + SCHEME_V->op;
5469 }
5470 } 5451 }
5471#endif 5452#endif
5472 5453
5473 ok_to_freely_gc (SCHEME_A); 5454 ok_to_freely_gc (SCHEME_A);
5474 5455
5456 static const dispatch_func dispatch_funcs[] = {
5457 opexe_0,
5458 opexe_1,
5459 opexe_2,
5460 opexe_3,
5461 opexe_4,
5462 opexe_5,
5463 opexe_6,
5464 };
5465
5475 if (ecb_expect_false (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL)) 5466 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5476 return; 5467 return;
5477 5468
5478 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5469 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5479 { 5470 {
5480 xwrstr ("No memory!\n"); 5471 xwrstr ("No memory!\n");
5504mk_proc (SCHEME_P_ enum scheme_opcodes op) 5495mk_proc (SCHEME_P_ enum scheme_opcodes op)
5505{ 5496{
5506 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5497 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5507 set_typeflag (y, (T_PROC | T_ATOM)); 5498 set_typeflag (y, (T_PROC | T_ATOM));
5508 ivalue_unchecked (y) = op; 5499 ivalue_unchecked (y) = op;
5509 set_num_integer (y);
5510 return y; 5500 return y;
5511} 5501}
5512 5502
5513/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5503/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5514static int 5504static int
5579 return OP_C0STREAM; /* cons-stream */ 5569 return OP_C0STREAM; /* cons-stream */
5580 } 5570 }
5581} 5571}
5582 5572
5583#if USE_MULTIPLICITY 5573#if USE_MULTIPLICITY
5584scheme * 5574ecb_cold scheme *
5585scheme_init_new () 5575scheme_init_new ()
5586{ 5576{
5587 scheme *sc = malloc (sizeof (scheme)); 5577 scheme *sc = malloc (sizeof (scheme));
5588 5578
5589 if (!scheme_init (SCHEME_A)) 5579 if (!scheme_init (SCHEME_A))
5594 else 5584 else
5595 return sc; 5585 return sc;
5596} 5586}
5597#endif 5587#endif
5598 5588
5599int 5589ecb_cold int
5600scheme_init (SCHEME_P) 5590scheme_init (SCHEME_P)
5601{ 5591{
5602 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5592 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5603 pointer x; 5593 pointer x;
5604 5594
5677 5667
5678 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) 5668 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
5679 assign_syntax (SCHEME_A_ syntax_names[i]); 5669 assign_syntax (SCHEME_A_ syntax_names[i]);
5680 } 5670 }
5681 5671
5672 // TODO: should iterate via strlen, to avoid n² complexity
5682 for (i = 0; i < n; i++) 5673 for (i = 0; i < n; i++)
5683 if (dispatch_table[i].name != 0) 5674 if (dispatch_table[i].builtin)
5684 assign_proc (SCHEME_A_ i, dispatch_table[i].name); 5675 assign_proc (SCHEME_A_ i, opname (i));
5685 5676
5686 /* initialization of global pointers to special symbols */ 5677 /* initialization of global pointers to special symbols */
5687 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 5678 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
5688 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 5679 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
5689 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 5680 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
5728scheme_set_external_data (SCHEME_P_ void *p) 5719scheme_set_external_data (SCHEME_P_ void *p)
5729{ 5720{
5730 SCHEME_V->ext_data = p; 5721 SCHEME_V->ext_data = p;
5731} 5722}
5732 5723
5733void 5724ecb_cold void
5734scheme_deinit (SCHEME_P) 5725scheme_deinit (SCHEME_P)
5735{ 5726{
5736 int i; 5727 int i;
5737 5728
5738#if SHOW_ERROR_LINE 5729#if SHOW_ERROR_LINE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines