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.31 by root, Sat Nov 28 10:54:41 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
258#define vecvalue(p) ((p)->object.vector.vvalue) 273#define vecvalue(p) ((p)->object.vector.vvalue)
259#define veclength(p) ((p)->object.vector.length) 274#define veclength(p) ((p)->object.vector.length)
260INTERFACE void fill_vector (pointer vec, pointer obj); 275INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
261INTERFACE uint32_t vector_length (pointer vec);
262INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); 276INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
263INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); 277INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
264 278
265INTERFACE uint32_t 279INTERFACE int
266vector_length (pointer vec) 280is_integer (pointer p)
267{ 281{
268 return vec->object.vector.length; 282 return type (p) == T_INTEGER;
269} 283}
270 284
285/* not the same as in scheme, where integers are (correctly :) reals */
271INTERFACE INLINE int 286INTERFACE int
287is_real (pointer p)
288{
289 return type (p) == T_REAL;
290}
291
292INTERFACE int
272is_number (pointer p) 293is_number (pointer p)
273{ 294{
274 return type (p) == T_NUMBER; 295 return is_integer (p) || is_real (p);
275} 296}
276 297
277INTERFACE INLINE int 298INTERFACE 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) 299is_character (pointer p)
297{ 300{
298 return type (p) == T_CHARACTER; 301 return type (p) == T_CHARACTER;
299} 302}
300 303
301INTERFACE INLINE char * 304INTERFACE char *
302string_value (pointer p) 305string_value (pointer p)
303{ 306{
304 return strvalue (p); 307 return strvalue (p);
305} 308}
306 309
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) 310#define ivalue_unchecked(p) (p)->object.ivalue
311#define set_ivalue(p,v) (p)->object.ivalue = (v)
312
338#if USE_REAL 313#if USE_REAL
339# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 314#define rvalue_unchecked(p) (p)->object.rvalue
340# define set_num_integer(p) (p)->object.number.is_fixnum=1; 315#define set_rvalue(p,v) (p)->object.rvalue = (v)
341# define set_num_real(p) (p)->object.number.is_fixnum=0;
342#else 316#else
343# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 317#define rvalue_unchecked(p) (p)->object.ivalue
344# define set_num_integer(p) 0 318#define set_rvalue(p,v) (p)->object.ivalue = (v)
345# define set_num_real(p) 0
346#endif 319#endif
320
347INTERFACE long 321INTERFACE long
348charvalue (pointer p) 322charvalue (pointer p)
349{ 323{
350 return ivalue_unchecked (p); 324 return ivalue_unchecked (p);
351} 325}
352 326
353INTERFACE INLINE int 327INTERFACE int
354is_port (pointer p) 328is_port (pointer p)
355{ 329{
356 return type (p) == T_PORT; 330 return type (p) == T_PORT;
357} 331}
358 332
359INTERFACE INLINE int 333INTERFACE int
360is_inport (pointer p) 334is_inport (pointer p)
361{ 335{
362 return is_port (p) && p->object.port->kind & port_input; 336 return is_port (p) && p->object.port->kind & port_input;
363} 337}
364 338
365INTERFACE INLINE int 339INTERFACE int
366is_outport (pointer p) 340is_outport (pointer p)
367{ 341{
368 return is_port (p) && p->object.port->kind & port_output; 342 return is_port (p) && p->object.port->kind & port_output;
369} 343}
370 344
371INTERFACE INLINE int 345INTERFACE int
372is_pair (pointer p) 346is_pair (pointer p)
373{ 347{
374 return type (p) == T_PAIR; 348 return type (p) == T_PAIR;
375} 349}
376 350
408pair_cdr (pointer p) 382pair_cdr (pointer p)
409{ 383{
410 return cdr (p); 384 return cdr (p);
411} 385}
412 386
413INTERFACE INLINE int 387INTERFACE int
414is_symbol (pointer p) 388is_symbol (pointer p)
415{ 389{
416 return type (p) == T_SYMBOL; 390 return type (p) == T_SYMBOL;
417} 391}
418 392
419INTERFACE INLINE char * 393INTERFACE char *
420symname (pointer p) 394symname (pointer p)
421{ 395{
422 return strvalue (car (p)); 396 return strvalue (car (p));
423} 397}
424 398
425#if USE_PLIST 399#if USE_PLIST
426SCHEME_EXPORT INLINE int 400SCHEME_EXPORT int
427hasprop (pointer p) 401hasprop (pointer p)
428{ 402{
429 return typeflag (p) & T_SYMBOL; 403 return typeflag (p) & T_SYMBOL;
430} 404}
431 405
432# define symprop(p) cdr(p) 406# define symprop(p) cdr(p)
433#endif 407#endif
434 408
435INTERFACE INLINE int 409INTERFACE int
436is_syntax (pointer p) 410is_syntax (pointer p)
437{ 411{
438 return typeflag (p) & T_SYNTAX; 412 return typeflag (p) & T_SYNTAX;
439} 413}
440 414
441INTERFACE INLINE int 415INTERFACE int
442is_proc (pointer p) 416is_proc (pointer p)
443{ 417{
444 return type (p) == T_PROC; 418 return type (p) == T_PROC;
445} 419}
446 420
447INTERFACE INLINE int 421INTERFACE int
448is_foreign (pointer p) 422is_foreign (pointer p)
449{ 423{
450 return type (p) == T_FOREIGN; 424 return type (p) == T_FOREIGN;
451} 425}
452 426
453INTERFACE INLINE char * 427INTERFACE char *
454syntaxname (pointer p) 428syntaxname (pointer p)
455{ 429{
456 return strvalue (car (p)); 430 return strvalue (car (p));
457} 431}
458 432
459#define procnum(p) ivalue (p) 433#define procnum(p) ivalue_unchecked (p)
460static const char *procname (pointer x); 434static const char *procname (pointer x);
461 435
462INTERFACE INLINE int 436INTERFACE int
463is_closure (pointer p) 437is_closure (pointer p)
464{ 438{
465 return type (p) == T_CLOSURE; 439 return type (p) == T_CLOSURE;
466} 440}
467 441
468INTERFACE INLINE int 442INTERFACE int
469is_macro (pointer p) 443is_macro (pointer p)
470{ 444{
471 return type (p) == T_MACRO; 445 return type (p) == T_MACRO;
472} 446}
473 447
474INTERFACE INLINE pointer 448INTERFACE pointer
475closure_code (pointer p) 449closure_code (pointer p)
476{ 450{
477 return car (p); 451 return car (p);
478} 452}
479 453
480INTERFACE INLINE pointer 454INTERFACE pointer
481closure_env (pointer p) 455closure_env (pointer p)
482{ 456{
483 return cdr (p); 457 return cdr (p);
484} 458}
485 459
486INTERFACE INLINE int 460INTERFACE int
487is_continuation (pointer p) 461is_continuation (pointer p)
488{ 462{
489 return type (p) == T_CONTINUATION; 463 return type (p) == T_CONTINUATION;
490} 464}
491 465
492#define cont_dump(p) cdr (p) 466#define cont_dump(p) cdr (p)
493#define set_cont_dump(p,v) set_cdr ((p), (v)) 467#define set_cont_dump(p,v) set_cdr ((p), (v))
494 468
495/* To do: promise should be forced ONCE only */ 469/* To do: promise should be forced ONCE only */
496INTERFACE INLINE int 470INTERFACE int
497is_promise (pointer p) 471is_promise (pointer p)
498{ 472{
499 return type (p) == T_PROMISE; 473 return type (p) == T_PROMISE;
500} 474}
501 475
502INTERFACE INLINE int 476INTERFACE int
503is_environment (pointer p) 477is_environment (pointer p)
504{ 478{
505 return type (p) == T_ENVIRONMENT; 479 return type (p) == T_ENVIRONMENT;
506} 480}
507 481
513 487
514#define is_mark(p) (typeflag (p) & T_MARK) 488#define is_mark(p) (typeflag (p) & T_MARK)
515#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) 489#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
516#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) 490#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
517 491
518INTERFACE INLINE int 492INTERFACE int
519is_immutable (pointer p) 493is_immutable (pointer p)
520{ 494{
521 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; 495 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
522} 496}
523 497
524INTERFACE INLINE void 498INTERFACE void
525setimmutable (pointer p) 499setimmutable (pointer p)
526{ 500{
527#if USE_ERROR_CHECKING 501#if USE_ERROR_CHECKING
528 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 502 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
529#endif 503#endif
530} 504}
531 505
506/* Result is:
507 proper list: length
508 circular list: -1
509 not even a pair: -2
510 dotted list: -2 minus length before dot
511*/
512INTERFACE int
513list_length (SCHEME_P_ pointer a)
514{
515 int i = 0;
516 pointer slow, fast;
517
518 slow = fast = a;
519
520 while (1)
521 {
522 if (fast == NIL)
523 return i;
524
525 if (!is_pair (fast))
526 return -2 - i;
527
528 fast = cdr (fast);
529 ++i;
530
531 if (fast == NIL)
532 return i;
533
534 if (!is_pair (fast))
535 return -2 - i;
536
537 ++i;
538 fast = cdr (fast);
539
540 /* Safe because we would have already returned if `fast'
541 encountered a non-pair. */
542 slow = cdr (slow);
543
544 if (fast == slow)
545 {
546 /* the fast pointer has looped back around and caught up
547 with the slow pointer, hence the structure is circular,
548 not of finite length, and therefore not a list */
549 return -1;
550 }
551 }
552}
553
554INTERFACE int
555is_list (SCHEME_P_ pointer a)
556{
557 return list_length (SCHEME_A_ a) >= 0;
558}
559
532#if USE_CHAR_CLASSIFIERS 560#if USE_CHAR_CLASSIFIERS
533static INLINE int 561ecb_inline int
534Cisalpha (int c) 562Cisalpha (int c)
535{ 563{
536 return isascii (c) && isalpha (c); 564 return isascii (c) && isalpha (c);
537} 565}
538 566
539static INLINE int 567ecb_inline int
540Cisdigit (int c) 568Cisdigit (int c)
541{ 569{
542 return isascii (c) && isdigit (c); 570 return isascii (c) && isdigit (c);
543} 571}
544 572
545static INLINE int 573ecb_inline int
546Cisspace (int c) 574Cisspace (int c)
547{ 575{
548 return isascii (c) && isspace (c); 576 return isascii (c) && isspace (c);
549} 577}
550 578
551static INLINE int 579ecb_inline int
552Cisupper (int c) 580Cisupper (int c)
553{ 581{
554 return isascii (c) && isupper (c); 582 return isascii (c) && isupper (c);
555} 583}
556 584
557static INLINE int 585ecb_inline int
558Cislower (int c) 586Cislower (int c)
559{ 587{
560 return isascii (c) && islower (c); 588 return isascii (c) && islower (c);
561} 589}
562#endif 590#endif
623#endif 651#endif
624 652
625static int file_push (SCHEME_P_ const char *fname); 653static int file_push (SCHEME_P_ const char *fname);
626static void file_pop (SCHEME_P); 654static void file_pop (SCHEME_P);
627static int file_interactive (SCHEME_P); 655static int file_interactive (SCHEME_P);
628static INLINE int is_one_of (char *s, int c); 656ecb_inline int is_one_of (char *s, int c);
629static int alloc_cellseg (SCHEME_P_ int n); 657static int alloc_cellseg (SCHEME_P_ int n);
630static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); 658ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
631static void finalize_cell (SCHEME_P_ pointer a); 659static void finalize_cell (SCHEME_P_ pointer a);
632static int count_consecutive_cells (pointer x, int needed); 660static int count_consecutive_cells (pointer x, int needed);
633static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 661static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
634static pointer mk_number (SCHEME_P_ const num n); 662static pointer mk_number (SCHEME_P_ const num n);
635static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); 663static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
652static int basic_inchar (port *pt); 680static int basic_inchar (port *pt);
653static int inchar (SCHEME_P); 681static int inchar (SCHEME_P);
654static void backchar (SCHEME_P_ int c); 682static void backchar (SCHEME_P_ int c);
655static char *readstr_upto (SCHEME_P_ char *delim); 683static char *readstr_upto (SCHEME_P_ char *delim);
656static pointer readstrexp (SCHEME_P); 684static pointer readstrexp (SCHEME_P);
657static INLINE int skipspace (SCHEME_P); 685ecb_inline int skipspace (SCHEME_P);
658static int token (SCHEME_P); 686static int token (SCHEME_P);
659static void printslashstring (SCHEME_P_ char *s, int len); 687static void printslashstring (SCHEME_P_ char *s, int len);
660static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 688static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
661static void printatom (SCHEME_P_ pointer l, int f); 689static void printatom (SCHEME_P_ pointer l, int f);
662static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 690static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
666static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); 694static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list);
667static pointer revappend (SCHEME_P_ pointer a, pointer b); 695static pointer revappend (SCHEME_P_ pointer a, pointer b);
668static pointer ss_get_cont (SCHEME_P); 696static pointer ss_get_cont (SCHEME_P);
669static void ss_set_cont (SCHEME_P_ pointer cont); 697static void ss_set_cont (SCHEME_P_ pointer cont);
670static void dump_stack_mark (SCHEME_P); 698static void dump_stack_mark (SCHEME_P);
671static pointer opexe_0 (SCHEME_P_ enum scheme_opcodes op); 699static int opexe_0 (SCHEME_P_ enum scheme_opcodes op);
700static int opexe_1 (SCHEME_P_ enum scheme_opcodes op);
672static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); 701static 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); 702static int opexe_3 (SCHEME_P_ enum scheme_opcodes op);
675static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); 703static int opexe_4 (SCHEME_P_ enum scheme_opcodes op);
676static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); 704static int opexe_5 (SCHEME_P_ enum scheme_opcodes op);
677static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); 705static int opexe_6 (SCHEME_P_ enum scheme_opcodes op);
678static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 706static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
679static void assign_syntax (SCHEME_P_ const char *name); 707static void assign_syntax (SCHEME_P_ const char *name);
680static int syntaxnum (pointer p); 708static int syntaxnum (pointer p);
681static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 709static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
682 710
711static IVALUE
712ivalue (pointer x)
713{
714 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
715}
716
717static RVALUE
718rvalue (pointer x)
719{
720 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
721}
722
723INTERFACE num
724nvalue (pointer x)
725{
726 num n;
727
728 num_set_fixnum (n, is_integer (x));
729
730 if (num_is_fixnum (n))
731 num_set_ivalue (n, ivalue_unchecked (x));
732 else
733 num_set_rvalue (n, rvalue_unchecked (x));
734
735 return n;
736}
737
683static num 738static num
684num_op (enum num_op op, num a, num b) 739num_op (enum num_op op, num a, num b)
685{ 740{
686 num ret; 741 num ret;
687 742
688 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 743 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
689 744
690 if (num_is_fixnum (ret)) 745 if (num_is_fixnum (ret))
691 { 746 {
692 IVALUE av = num_get_ivalue (a);
693 IVALUE bv = num_get_ivalue (b);
694
695 switch (op) 747 switch (op)
696 { 748 {
697 case NUM_ADD: av += bv; break; 749 case NUM_ADD: a.ivalue += b.ivalue; break;
698 case NUM_SUB: av -= bv; break; 750 case NUM_SUB: a.ivalue -= b.ivalue; break;
699 case NUM_MUL: av *= bv; break; 751 case NUM_MUL: a.ivalue *= b.ivalue; break;
700 case NUM_INTDIV: av /= bv; break; 752 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
701 } 753 }
702 754
703 num_set_ivalue (ret, av); 755 num_set_ivalue (ret, a.ivalue);
704 } 756 }
757#if USE_REAL
705 else 758 else
706 { 759 {
707 RVALUE av = num_get_rvalue (a);
708 RVALUE bv = num_get_rvalue (b);
709
710 switch (op) 760 switch (op)
711 { 761 {
712 case NUM_ADD: av += bv; break; 762 case NUM_ADD: a.rvalue += b.rvalue; break;
713 case NUM_SUB: av -= bv; break; 763 case NUM_SUB: a.rvalue -= b.rvalue; break;
714 case NUM_MUL: av *= bv; break; 764 case NUM_MUL: a.rvalue *= b.rvalue; break;
715 case NUM_INTDIV: av /= bv; break; 765 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
716 } 766 }
717 767
718 num_set_rvalue (ret, av); 768 num_set_rvalue (ret, a.rvalue);
719 } 769 }
770#endif
720 771
721 return ret; 772 return ret;
722} 773}
723 774
724static num 775static num
725num_div (num a, num b) 776num_div (num a, num b)
726{ 777{
727 num ret; 778 num ret;
728 779
729 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); 780 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0);
730 781
731 if (num_is_fixnum (ret)) 782 if (num_is_fixnum (ret))
732 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 783 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
733 else 784 else
734 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 785 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
735 786
736 return ret; 787 return ret;
737} 788}
738 789
739static num 790static num
741{ 792{
742 num ret; 793 num ret;
743 long e1, e2, res; 794 long e1, e2, res;
744 795
745 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 796 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
746 e1 = num_get_ivalue (a); 797 e1 = num_ivalue (a);
747 e2 = num_get_ivalue (b); 798 e2 = num_ivalue (b);
748 res = e1 % e2; 799 res = e1 % e2;
749 800
750 /* remainder should have same sign as second operand */ 801 /* remainder should have same sign as second operand */
751 if (res > 0) 802 if (res > 0)
752 { 803 {
768{ 819{
769 num ret; 820 num ret;
770 long e1, e2, res; 821 long e1, e2, res;
771 822
772 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 823 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
773 e1 = num_get_ivalue (a); 824 e1 = num_ivalue (a);
774 e2 = num_get_ivalue (b); 825 e2 = num_ivalue (b);
775 res = e1 % e2; 826 res = e1 % e2;
776 827
777 /* modulo should have same sign as second operand */ 828 /* modulo should have same sign as second operand */
778 if (res * e2 < 0) 829 if (res * e2 < 0)
779 res += e2; 830 res += e2;
780 831
781 num_set_ivalue (ret, res); 832 num_set_ivalue (ret, res);
782 return ret; 833 return ret;
783} 834}
784 835
785/* this completely disrespects NaNs */ 836/* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */
786static int 837static int
787num_cmp (num a, num b) 838num_cmp (num a, num b)
788{ 839{
789 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 840 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
790 int ret; 841 int ret;
791 842
792 if (is_fixnum) 843 if (is_fixnum)
793 { 844 {
794 IVALUE av = num_get_ivalue (a); 845 IVALUE av = num_ivalue (a);
795 IVALUE bv = num_get_ivalue (b); 846 IVALUE bv = num_ivalue (b);
796 847
797 ret = av == bv ? 0 : av < bv ? -1 : +1; 848 ret = av == bv ? 0 : av < bv ? -1 : +1;
798 } 849 }
799 else 850 else
800 { 851 {
801 RVALUE av = num_get_rvalue (a); 852 RVALUE av = num_rvalue (a);
802 RVALUE bv = num_get_rvalue (b); 853 RVALUE bv = num_rvalue (b);
803 854
804 ret = av == bv ? 0 : av < bv ? -1 : +1; 855 ret = av == bv ? 0 : av < bv ? -1 : +1;
805 } 856 }
806 857
807 return ret; 858 return ret;
833#endif 884#endif
834 885
835static int 886static int
836is_zero_rvalue (RVALUE x) 887is_zero_rvalue (RVALUE x)
837{ 888{
889 return x == 0;
890#if 0
838#if USE_REAL 891#if USE_REAL
839 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ 892 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */
840#else 893#else
841 return x == 0; 894 return x == 0;
895#endif
842#endif 896#endif
843} 897}
844 898
845/* allocate new cell segment */ 899/* allocate new cell segment */
846static int 900static int
867 return k; 921 return k;
868 922
869 i = ++SCHEME_V->last_cell_seg; 923 i = ++SCHEME_V->last_cell_seg;
870 SCHEME_V->alloc_seg[i] = cp; 924 SCHEME_V->alloc_seg[i] = cp;
871 925
872 /* insert new segment in address order */
873 newp = (pointer)cp; 926 newp = (pointer)cp;
874 SCHEME_V->cell_seg[i] = newp; 927 SCHEME_V->cell_seg[i] = newp;
875 SCHEME_V->cell_segsize[i] = segsize; 928 SCHEME_V->cell_segsize[i] = segsize;
876
877 //TODO: insert, not swap
878 while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i])
879 {
880 p = SCHEME_V->cell_seg[i];
881 SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1];
882 SCHEME_V->cell_seg[i - 1] = p;
883
884 k = SCHEME_V->cell_segsize[i];
885 SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1];
886 SCHEME_V->cell_segsize[i - 1] = k;
887
888 --i;
889 }
890
891 SCHEME_V->fcells += segsize; 929 SCHEME_V->fcells += segsize;
892 last = newp + segsize - 1; 930 last = newp + segsize - 1;
893 931
894 for (p = newp; p <= last; p++) 932 for (p = newp; p <= last; p++)
895 { 933 {
896 set_typeflag (p, T_FREE); 934 set_typeflag (p, T_PAIR);
897 set_car (p, NIL); 935 set_car (p, NIL);
898 set_cdr (p, p + 1); 936 set_cdr (p, p + 1);
899 } 937 }
900 938
901 /* insert new cells in address order on free list */
902 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell)
903 {
904 set_cdr (last, SCHEME_V->free_cell); 939 set_cdr (last, SCHEME_V->free_cell);
905 SCHEME_V->free_cell = newp; 940 SCHEME_V->free_cell = newp;
906 }
907 else
908 {
909 p = SCHEME_V->free_cell;
910
911 while (cdr (p) != NIL && newp > cdr (p))
912 p = cdr (p);
913
914 set_cdr (last, cdr (p));
915 set_cdr (p, newp);
916 }
917 } 941 }
918 942
919 return n; 943 return n;
920} 944}
921 945
922/* get new cell. parameter a, b is marked by gc. */ 946/* get new cell. parameter a, b is marked by gc. */
923static INLINE pointer 947ecb_inline pointer
924get_cell_x (SCHEME_P_ pointer a, pointer b) 948get_cell_x (SCHEME_P_ pointer a, pointer b)
925{ 949{
926 if (ecb_expect_false (SCHEME_V->free_cell == NIL)) 950 if (ecb_expect_false (SCHEME_V->free_cell == NIL))
927 { 951 {
928 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 952 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
1000 /* Record it as a vector so that gc understands it. */ 1024 /* Record it as a vector so that gc understands it. */
1001 set_typeflag (v, T_VECTOR | T_ATOM); 1025 set_typeflag (v, T_VECTOR | T_ATOM);
1002 1026
1003 v->object.vector.vvalue = e; 1027 v->object.vector.vvalue = e;
1004 v->object.vector.length = len; 1028 v->object.vector.length = len;
1005 fill_vector (v, init); 1029 fill_vector (v, 0, init);
1006 push_recent_alloc (SCHEME_A_ v, NIL); 1030 push_recent_alloc (SCHEME_A_ v, NIL);
1007 1031
1008 return v; 1032 return v;
1009} 1033}
1010 1034
1011static INLINE void 1035ecb_inline void
1012ok_to_freely_gc (SCHEME_P) 1036ok_to_freely_gc (SCHEME_P)
1013{ 1037{
1014 set_car (S_SINK, NIL); 1038 set_car (S_SINK, NIL);
1015} 1039}
1016 1040
1076 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); 1100 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1077 set_typeflag (x, T_SYMBOL); 1101 set_typeflag (x, T_SYMBOL);
1078 setimmutable (car (x)); 1102 setimmutable (car (x));
1079 1103
1080 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1104 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))); 1105 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1082 return x; 1106 return x;
1083} 1107}
1084 1108
1085static INLINE pointer 1109ecb_inline pointer
1086oblist_find_by_name (SCHEME_P_ const char *name) 1110oblist_find_by_name (SCHEME_P_ const char *name)
1087{ 1111{
1088 int location; 1112 int location;
1089 pointer x; 1113 pointer x;
1090 char *s; 1114 char *s;
1091 1115
1092 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1116 location = hash_fn (name, veclength (SCHEME_V->oblist));
1093 1117
1094 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1118 for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1095 { 1119 {
1096 s = symname (car (x)); 1120 s = symname (car (x));
1097 1121
1098 /* case-insensitive, per R5RS section 2 */ 1122 /* case-insensitive, per R5RS section 2 */
1099 if (stricmp (name, s) == 0) 1123 if (stricmp (name, s) == 0)
1109 int i; 1133 int i;
1110 pointer x; 1134 pointer x;
1111 pointer ob_list = NIL; 1135 pointer ob_list = NIL;
1112 1136
1113 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1137 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1114 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1138 for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1115 ob_list = cons (x, ob_list); 1139 ob_list = cons (x, ob_list);
1116 1140
1117 return ob_list; 1141 return ob_list;
1118} 1142}
1119 1143
1123oblist_initial_value (SCHEME_P) 1147oblist_initial_value (SCHEME_P)
1124{ 1148{
1125 return NIL; 1149 return NIL;
1126} 1150}
1127 1151
1128static INLINE pointer 1152ecb_inline pointer
1129oblist_find_by_name (SCHEME_P_ const char *name) 1153oblist_find_by_name (SCHEME_P_ const char *name)
1130{ 1154{
1131 pointer x; 1155 pointer x;
1132 char *s; 1156 char *s;
1133 1157
1145 1169
1146/* returns the new symbol */ 1170/* returns the new symbol */
1147static pointer 1171static pointer
1148oblist_add_by_name (SCHEME_P_ const char *name) 1172oblist_add_by_name (SCHEME_P_ const char *name)
1149{ 1173{
1150 pointer x; 1174 pointer x = mk_string (SCHEME_A_ name);
1151
1152 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1153 set_typeflag (x, T_SYMBOL); 1175 set_typeflag (x, T_SYMBOL);
1154 setimmutable (car (x)); 1176 setimmutable (x);
1155 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1177 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1156 return x; 1178 return x;
1157} 1179}
1158 1180
1159static pointer 1181static pointer
1192mk_character (SCHEME_P_ int c) 1214mk_character (SCHEME_P_ int c)
1193{ 1215{
1194 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1216 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1195 1217
1196 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1218 set_typeflag (x, (T_CHARACTER | T_ATOM));
1197 ivalue_unchecked (x) = c & 0xff; 1219 set_ivalue (x, c & 0xff);
1198 set_num_integer (x); 1220
1199 return x; 1221 return x;
1200} 1222}
1201 1223
1202/* get number atom (integer) */ 1224/* get number atom (integer) */
1203INTERFACE pointer 1225INTERFACE pointer
1204mk_integer (SCHEME_P_ long num) 1226mk_integer (SCHEME_P_ long n)
1205{ 1227{
1206 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1228 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1207 1229
1208 set_typeflag (x, (T_NUMBER | T_ATOM)); 1230 set_typeflag (x, (T_INTEGER | T_ATOM));
1209 ivalue_unchecked (x) = num; 1231 set_ivalue (x, n);
1210 set_num_integer (x); 1232
1211 return x; 1233 return x;
1212} 1234}
1213 1235
1214INTERFACE pointer 1236INTERFACE pointer
1215mk_real (SCHEME_P_ RVALUE n) 1237mk_real (SCHEME_P_ RVALUE n)
1216{ 1238{
1239#if USE_REAL
1217 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1240 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1218 1241
1219 set_typeflag (x, (T_NUMBER | T_ATOM)); 1242 set_typeflag (x, (T_REAL | T_ATOM));
1220 rvalue_unchecked (x) = n; 1243 set_rvalue (x, n);
1221 set_num_real (x); 1244
1222 return x; 1245 return x;
1246#else
1247 return mk_integer (SCHEME_A_ n);
1248#endif
1223} 1249}
1224 1250
1225static pointer 1251static pointer
1226mk_number (SCHEME_P_ const num n) 1252mk_number (SCHEME_P_ const num n)
1227{ 1253{
1254#if USE_REAL
1228 if (num_is_fixnum (n)) 1255 return num_is_fixnum (n)
1256 ? mk_integer (SCHEME_A_ num_ivalue (n))
1257 : mk_real (SCHEME_A_ num_rvalue (n));
1258#else
1229 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1259 return mk_integer (SCHEME_A_ num_ivalue (n));
1230 else 1260#endif
1231 return mk_real (SCHEME_A_ num_get_rvalue (n));
1232} 1261}
1233 1262
1234/* allocate name to string area */ 1263/* allocate name to string area */
1235static char * 1264static char *
1236store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) 1265store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1295{ 1324{
1296 return get_vector_object (SCHEME_A_ len, NIL); 1325 return get_vector_object (SCHEME_A_ len, NIL);
1297} 1326}
1298 1327
1299INTERFACE void 1328INTERFACE void
1300fill_vector (pointer vec, pointer obj) 1329fill_vector (pointer vec, uint32_t start, pointer obj)
1301{ 1330{
1302 int i; 1331 int i;
1303 1332
1304 for (i = 0; i < vec->object.vector.length; i++) 1333 for (i = start; i < veclength (vec); i++)
1305 vecvalue (vec)[i] = obj; 1334 vecvalue (vec)[i] = obj;
1306} 1335}
1307 1336
1308INTERFACE pointer 1337INTERFACE pointer
1309vector_elem (pointer vec, uint32_t ielem) 1338vector_get (pointer vec, uint32_t ielem)
1310{ 1339{
1311 return vecvalue(vec)[ielem]; 1340 return vecvalue(vec)[ielem];
1312} 1341}
1313 1342
1314INTERFACE void 1343INTERFACE void
1315set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1344vector_set (pointer vec, uint32_t ielem, pointer a)
1316{ 1345{
1317 vecvalue(vec)[ielem] = a; 1346 vecvalue(vec)[ielem] = a;
1318} 1347}
1319 1348
1320/* get new symbol */ 1349/* get new symbol */
1412 } 1441 }
1413 else if ((c == 'e') || (c == 'E')) 1442 else if ((c == 'e') || (c == 'E'))
1414 { 1443 {
1415 if (!has_fp_exp) 1444 if (!has_fp_exp)
1416 { 1445 {
1417 has_dec_point = 1; /* decimal point illegal 1446 has_dec_point = 1; /* decimal point illegal from now on */
1418 from now on */
1419 p++; 1447 p++;
1420 1448
1421 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1449 if ((*p == '-') || (*p == '+') || isdigit (*p))
1422 continue; 1450 continue;
1423 } 1451 }
1511 1539
1512 if (ecb_expect_false (is_vector (p))) 1540 if (ecb_expect_false (is_vector (p)))
1513 { 1541 {
1514 int i; 1542 int i;
1515 1543
1516 for (i = 0; i < p->object.vector.length; i++) 1544 for (i = 0; i < veclength (p); i++)
1517 mark (vecvalue (p)[i]); 1545 mark (vecvalue (p)[i]);
1518 } 1546 }
1519 1547
1520 if (is_atom (p)) 1548 if (is_atom (p))
1521 goto E6; 1549 goto E6;
1619 if (is_mark (p)) 1647 if (is_mark (p))
1620 clrmark (p); 1648 clrmark (p);
1621 else 1649 else
1622 { 1650 {
1623 /* reclaim cell */ 1651 /* reclaim cell */
1624 if (typeflag (p) != T_FREE) 1652 if (typeflag (p) != T_PAIR)
1625 { 1653 {
1626 finalize_cell (SCHEME_A_ p); 1654 finalize_cell (SCHEME_A_ p);
1627 set_typeflag (p, T_FREE); 1655 set_typeflag (p, T_PAIR);
1628 set_car (p, NIL); 1656 set_car (p, NIL);
1629 } 1657 }
1630 1658
1631 ++SCHEME_V->fcells; 1659 ++SCHEME_V->fcells;
1632 set_cdr (p, SCHEME_V->free_cell); 1660 set_cdr (p, SCHEME_V->free_cell);
1634 } 1662 }
1635 } 1663 }
1636 } 1664 }
1637 1665
1638 if (SCHEME_V->gc_verbose) 1666 if (SCHEME_V->gc_verbose)
1667 {
1639 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1668 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1669 }
1640} 1670}
1641 1671
1642static void 1672static void
1643finalize_cell (SCHEME_P_ pointer a) 1673finalize_cell (SCHEME_P_ pointer a)
1644{ 1674{
2231 } 2261 }
2232 } 2262 }
2233} 2263}
2234 2264
2235/* check c is in chars */ 2265/* check c is in chars */
2236static INLINE int 2266ecb_inline int
2237is_one_of (char *s, int c) 2267is_one_of (char *s, int c)
2238{ 2268{
2239 if (c == EOF) 2269 if (c == EOF)
2240 return 1; 2270 return 1;
2241 2271
2242 return !!strchr (s, c); 2272 return !!strchr (s, c);
2243} 2273}
2244 2274
2245/* skip white characters */ 2275/* skip white characters */
2246static INLINE int 2276ecb_inline int
2247skipspace (SCHEME_P) 2277skipspace (SCHEME_P)
2248{ 2278{
2249 int c, curr_line = 0; 2279 int c, curr_line = 0;
2250 2280
2251 do 2281 do
2479 { 2509 {
2480 p = SCHEME_V->strbuff; 2510 p = SCHEME_V->strbuff;
2481 2511
2482 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2512 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2483 { 2513 {
2484 if (num_is_integer (l)) 2514 if (is_integer (l))
2485 xnum (p, ivalue_unchecked (l)); 2515 xnum (p, ivalue_unchecked (l));
2486#if USE_REAL 2516#if USE_REAL
2487 else 2517 else
2488 { 2518 {
2489 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2519 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2744 return 0; 2774 return 0;
2745 } 2775 }
2746 else if (is_number (a)) 2776 else if (is_number (a))
2747 { 2777 {
2748 if (is_number (b)) 2778 if (is_number (b))
2749 if (num_is_integer (a) == num_is_integer (b))
2750 return num_cmp (nvalue (a), nvalue (b)) == 0; 2779 return num_cmp (nvalue (a), nvalue (b)) == 0;
2751 2780
2752 return 0; 2781 return 0;
2753 } 2782 }
2754 else if (is_character (a)) 2783 else if (is_character (a))
2755 { 2784 {
2822 2851
2823 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2852 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2824 setenvironment (SCHEME_V->envir); 2853 setenvironment (SCHEME_V->envir);
2825} 2854}
2826 2855
2827static INLINE void 2856static uint32_t
2857sym_hash (pointer sym, uint32_t size)
2858{
2859 uintptr_t ptr = (uintptr_t)sym;
2860
2861#if 0
2862 /* tqable size is prime, so why mix */
2863 ptr += ptr >> 32;
2864 ptr += ptr >> 16;
2865 ptr += ptr >> 8;
2866#endif
2867
2868 return ptr % size;
2869}
2870
2871ecb_inline void
2828new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2872new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2829{ 2873{
2830 pointer slot = immutable_cons (variable, value); 2874 pointer slot = immutable_cons (variable, value);
2831 2875
2832 if (is_vector (car (env))) 2876 if (is_vector (car (env)))
2833 { 2877 {
2834 int location = hash_fn (symname (variable), veclength (car (env))); 2878 int location = sym_hash (variable, veclength (car (env)));
2835
2836 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2879 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2837 } 2880 }
2838 else 2881 else
2839 set_car (env, immutable_cons (slot, car (env))); 2882 set_car (env, immutable_cons (slot, car (env)));
2840} 2883}
2841 2884
2842static pointer 2885static pointer
2843find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2886find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2844{ 2887{
2845 pointer x, y; 2888 pointer x, y;
2846 int location;
2847 2889
2848 for (x = env; x != NIL; x = cdr (x)) 2890 for (x = env; x != NIL; x = cdr (x))
2849 { 2891 {
2850 if (is_vector (car (x))) 2892 if (is_vector (car (x)))
2851 { 2893 {
2852 location = hash_fn (symname (hdl), veclength (car (x))); 2894 int location = sym_hash (hdl, veclength (car (x)));
2853 y = vector_elem (car (x), location); 2895 y = vector_get (car (x), location);
2854 } 2896 }
2855 else 2897 else
2856 y = car (x); 2898 y = car (x);
2857 2899
2858 for (; y != NIL; y = cdr (y)) 2900 for (; y != NIL; y = cdr (y))
2859 if (caar (y) == hdl) 2901 if (caar (y) == hdl)
2860 break; 2902 break;
2861 2903
2862 if (y != NIL) 2904 if (y != NIL)
2905 return car (y);
2906
2907 if (!all)
2863 break; 2908 break;
2864
2865 if (!all)
2866 return NIL;
2867 } 2909 }
2868
2869 if (x != NIL)
2870 return car (y);
2871 2910
2872 return NIL; 2911 return NIL;
2873} 2912}
2874 2913
2875#else /* USE_ALIST_ENV */ 2914#else /* USE_ALIST_ENV */
2876 2915
2877static INLINE void 2916ecb_inline void
2878new_frame_in_env (SCHEME_P_ pointer old_env) 2917new_frame_in_env (SCHEME_P_ pointer old_env)
2879{ 2918{
2880 SCHEME_V->envir = immutable_cons (NIL, old_env); 2919 SCHEME_V->envir = immutable_cons (NIL, old_env);
2881 setenvironment (SCHEME_V->envir); 2920 setenvironment (SCHEME_V->envir);
2882} 2921}
2883 2922
2884static INLINE void 2923ecb_inline void
2885new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2924new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2886{ 2925{
2887 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2926 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2888} 2927}
2889 2928
2911 return NIL; 2950 return NIL;
2912} 2951}
2913 2952
2914#endif /* USE_ALIST_ENV else */ 2953#endif /* USE_ALIST_ENV else */
2915 2954
2916static INLINE void 2955ecb_inline void
2917new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2956new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2918{ 2957{
2919 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2958 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2920} 2959}
2921 2960
2922static INLINE void 2961ecb_inline void
2923set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2962set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2924{ 2963{
2925 set_cdr (slot, value); 2964 set_cdr (slot, value);
2926} 2965}
2927 2966
2928static INLINE pointer 2967ecb_inline pointer
2929slot_value_in_env (pointer slot) 2968slot_value_in_env (pointer slot)
2930{ 2969{
2931 return cdr (slot); 2970 return cdr (slot);
2932} 2971}
2933 2972
2934/* ========== Evaluation Cycle ========== */ 2973/* ========== Evaluation Cycle ========== */
2935 2974
2936static pointer 2975static int
2937xError_1 (SCHEME_P_ const char *s, pointer a) 2976xError_1 (SCHEME_P_ const char *s, pointer a)
2938{ 2977{
2939#if USE_ERROR_HOOK 2978#if USE_ERROR_HOOK
2940 pointer x; 2979 pointer x;
2941 pointer hdl = SCHEME_V->ERROR_HOOK; 2980 pointer hdl = SCHEME_V->ERROR_HOOK;
2976 code = cons (mk_string (SCHEME_A_ s), code); 3015 code = cons (mk_string (SCHEME_A_ s), code);
2977 setimmutable (car (code)); 3016 setimmutable (car (code));
2978 SCHEME_V->code = cons (slot_value_in_env (x), code); 3017 SCHEME_V->code = cons (slot_value_in_env (x), code);
2979 SCHEME_V->op = OP_EVAL; 3018 SCHEME_V->op = OP_EVAL;
2980 3019
2981 return S_T; 3020 return 0;
2982 } 3021 }
2983#endif 3022#endif
2984 3023
2985 if (a) 3024 if (a)
2986 SCHEME_V->args = cons (a, NIL); 3025 SCHEME_V->args = cons (a, NIL);
2988 SCHEME_V->args = NIL; 3027 SCHEME_V->args = NIL;
2989 3028
2990 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); 3029 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args);
2991 setimmutable (car (SCHEME_V->args)); 3030 setimmutable (car (SCHEME_V->args));
2992 SCHEME_V->op = OP_ERR0; 3031 SCHEME_V->op = OP_ERR0;
3032
2993 return S_T; 3033 return 0;
2994} 3034}
2995 3035
2996#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) 3036#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a)
2997#define Error_0(s) Error_1 (s, 0) 3037#define Error_0(s) Error_1 (s, 0)
2998 3038
2999/* Too small to turn into function */ 3039/* Too small to turn into function */
3000#define BEGIN do { 3040#define BEGIN do {
3001#define END } while (0) 3041#define END } while (0)
3002#define s_goto(a) BEGIN \ 3042#define s_goto(a) BEGIN \
3003 SCHEME_V->op = a; \ 3043 SCHEME_V->op = a; \
3004 return S_T; END 3044 return 0; END
3005 3045
3006#define s_return(a) return xs_return (SCHEME_A_ a) 3046#define s_return(a) return xs_return (SCHEME_A_ a)
3007 3047
3008#ifndef USE_SCHEME_STACK 3048#ifndef USE_SCHEME_STACK
3009 3049
3039 next_frame->code = code; 3079 next_frame->code = code;
3040 3080
3041 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3081 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3042} 3082}
3043 3083
3044static pointer 3084static int
3045xs_return (SCHEME_P_ pointer a) 3085xs_return (SCHEME_P_ pointer a)
3046{ 3086{
3047 int nframes = (uintptr_t)SCHEME_V->dump; 3087 int nframes = (uintptr_t)SCHEME_V->dump;
3048 struct dump_stack_frame *frame; 3088 struct dump_stack_frame *frame;
3049 3089
3050 SCHEME_V->value = a; 3090 SCHEME_V->value = a;
3051 3091
3052 if (nframes <= 0) 3092 if (nframes <= 0)
3053 return NIL; 3093 return -1;
3054 3094
3055 frame = &SCHEME_V->dump_base[--nframes]; 3095 frame = &SCHEME_V->dump_base[--nframes];
3056 SCHEME_V->op = frame->op; 3096 SCHEME_V->op = frame->op;
3057 SCHEME_V->args = frame->args; 3097 SCHEME_V->args = frame->args;
3058 SCHEME_V->envir = frame->envir; 3098 SCHEME_V->envir = frame->envir;
3059 SCHEME_V->code = frame->code; 3099 SCHEME_V->code = frame->code;
3060 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3100 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3061 3101
3062 return S_T; 3102 return 0;
3063} 3103}
3064 3104
3065static INLINE void 3105ecb_inline void
3066dump_stack_reset (SCHEME_P) 3106dump_stack_reset (SCHEME_P)
3067{ 3107{
3068 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3108 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3069 SCHEME_V->dump = (pointer)+0; 3109 SCHEME_V->dump = (pointer)+0;
3070} 3110}
3071 3111
3072static INLINE void 3112ecb_inline void
3073dump_stack_initialize (SCHEME_P) 3113dump_stack_initialize (SCHEME_P)
3074{ 3114{
3075 SCHEME_V->dump_size = 0; 3115 SCHEME_V->dump_size = 0;
3076 SCHEME_V->dump_base = 0; 3116 SCHEME_V->dump_base = 0;
3077 dump_stack_reset (SCHEME_A); 3117 dump_stack_reset (SCHEME_A);
3130 int i = 0; 3170 int i = 0;
3131 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3171 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3132 3172
3133 while (cont != NIL) 3173 while (cont != NIL)
3134 { 3174 {
3135 frame->op = ivalue (car (cont)); cont = cdr (cont); 3175 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3136 frame->args = car (cont) ; cont = cdr (cont); 3176 frame->args = car (cont) ; cont = cdr (cont);
3137 frame->envir = car (cont) ; cont = cdr (cont); 3177 frame->envir = car (cont) ; cont = cdr (cont);
3138 frame->code = car (cont) ; cont = cdr (cont); 3178 frame->code = car (cont) ; cont = cdr (cont);
3139 3179
3140 ++frame; 3180 ++frame;
3141 ++i; 3181 ++i;
3142 } 3182 }
3143 3183
3144 SCHEME_V->dump = (pointer)(uintptr_t)i; 3184 SCHEME_V->dump = (pointer)(uintptr_t)i;
3145} 3185}
3146 3186
3147#else 3187#else
3148 3188
3149static INLINE void 3189ecb_inline void
3150dump_stack_reset (SCHEME_P) 3190dump_stack_reset (SCHEME_P)
3151{ 3191{
3152 SCHEME_V->dump = NIL; 3192 SCHEME_V->dump = NIL;
3153} 3193}
3154 3194
3155static INLINE void 3195ecb_inline void
3156dump_stack_initialize (SCHEME_P) 3196dump_stack_initialize (SCHEME_P)
3157{ 3197{
3158 dump_stack_reset (SCHEME_A); 3198 dump_stack_reset (SCHEME_A);
3159} 3199}
3160 3200
3162dump_stack_free (SCHEME_P) 3202dump_stack_free (SCHEME_P)
3163{ 3203{
3164 SCHEME_V->dump = NIL; 3204 SCHEME_V->dump = NIL;
3165} 3205}
3166 3206
3167static pointer 3207static int
3168xs_return (SCHEME_P_ pointer a) 3208xs_return (SCHEME_P_ pointer a)
3169{ 3209{
3170 pointer dump = SCHEME_V->dump; 3210 pointer dump = SCHEME_V->dump;
3171 3211
3172 SCHEME_V->value = a; 3212 SCHEME_V->value = a;
3173 3213
3174 if (dump == NIL) 3214 if (dump == NIL)
3175 return NIL; 3215 return -1;
3176 3216
3177 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3217 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3178 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3218 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3179 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3219 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3180 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3220 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3181 3221
3182 SCHEME_V->dump = dump; 3222 SCHEME_V->dump = dump;
3183 3223
3184 return S_T; 3224 return 0;
3185} 3225}
3186 3226
3187static void 3227static void
3188s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3228s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3189{ 3229{
3214 3254
3215#endif 3255#endif
3216 3256
3217#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3257#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3218 3258
3219static pointer 3259static int
3220opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3260opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3221{ 3261{
3222 pointer args = SCHEME_V->args; 3262 pointer args = SCHEME_V->args;
3223 pointer x, y; 3263 pointer x, y;
3224 3264
3391 3431
3392 case OP_TRACING: 3432 case OP_TRACING:
3393 { 3433 {
3394 int tr = SCHEME_V->tracing; 3434 int tr = SCHEME_V->tracing;
3395 3435
3396 SCHEME_V->tracing = ivalue (car (args)); 3436 SCHEME_V->tracing = ivalue_unchecked (car (args));
3397 s_return (mk_integer (SCHEME_A_ tr)); 3437 s_return (mk_integer (SCHEME_A_ tr));
3398 } 3438 }
3399 3439
3400#endif 3440#endif
3401 3441
3413 /* fall through */ 3453 /* fall through */
3414 3454
3415 case OP_REAL_APPLY: 3455 case OP_REAL_APPLY:
3416#endif 3456#endif
3417 if (is_proc (SCHEME_V->code)) 3457 if (is_proc (SCHEME_V->code))
3418 {
3419 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3458 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3420 }
3421 else if (is_foreign (SCHEME_V->code)) 3459 else if (is_foreign (SCHEME_V->code))
3422 { 3460 {
3423 /* Keep nested calls from GC'ing the arglist */ 3461 /* Keep nested calls from GC'ing the arglist */
3424 push_recent_alloc (SCHEME_A_ args, NIL); 3462 push_recent_alloc (SCHEME_A_ args, NIL);
3425 x = SCHEME_V->code->object.ff (SCHEME_A_ args); 3463 x = SCHEME_V->code->object.ff (SCHEME_A_ args);
3592 3630
3593 case OP_IF1: /* if */ 3631 case OP_IF1: /* if */
3594 if (is_true (SCHEME_V->value)) 3632 if (is_true (SCHEME_V->value))
3595 SCHEME_V->code = car (SCHEME_V->code); 3633 SCHEME_V->code = car (SCHEME_V->code);
3596 else 3634 else
3597 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because 3635 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
3598
3599 * car(NIL) = NIL */
3600 s_goto (OP_EVAL); 3636 s_goto (OP_EVAL);
3601 3637
3602 case OP_LET0: /* let */ 3638 case OP_LET0: /* let */
3603 SCHEME_V->args = NIL; 3639 SCHEME_V->args = NIL;
3604 SCHEME_V->value = SCHEME_V->code; 3640 SCHEME_V->value = SCHEME_V->code;
3914 SCHEME_V->code = car (args); 3950 SCHEME_V->code = car (args);
3915 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); 3951 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
3916 s_goto (OP_APPLY); 3952 s_goto (OP_APPLY);
3917 } 3953 }
3918 3954
3919 abort (); 3955 if (USE_ERROR_CHECKING) abort ();
3920} 3956}
3921 3957
3922static pointer 3958static int
3923opexe_2 (SCHEME_P_ enum scheme_opcodes op) 3959opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3924{ 3960{
3925 pointer args = SCHEME_V->args; 3961 pointer args = SCHEME_V->args;
3926 pointer x = car (args); 3962 pointer x = car (args);
3927 num v; 3963 num v;
3928 3964
3929#if USE_MATH
3930 RVALUE dd;
3931#endif
3932
3933 switch (op) 3965 switch (op)
3934 { 3966 {
3935#if USE_MATH 3967#if USE_MATH
3936 case OP_INEX2EX: /* inexact->exact */ 3968 case OP_INEX2EX: /* inexact->exact */
3969 {
3937 if (num_is_integer (x)) 3970 if (is_integer (x))
3938 s_return (x); 3971 s_return (x);
3939 else if (modf (rvalue_unchecked (x), &dd) == 0) 3972
3973 RVALUE r = rvalue_unchecked (x);
3974
3975 if (r == (RVALUE)(IVALUE)r)
3940 s_return (mk_integer (SCHEME_A_ ivalue (x))); 3976 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3941 else 3977 else
3942 Error_1 ("inexact->exact: not integral:", x); 3978 Error_1 ("inexact->exact: not integral:", x);
3979 }
3943 3980
3944 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 3981 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)))); 3982 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)))); 3983 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)))); 3984 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3965 { 4002 {
3966 RVALUE result; 4003 RVALUE result;
3967 int real_result = 1; 4004 int real_result = 1;
3968 pointer y = cadr (args); 4005 pointer y = cadr (args);
3969 4006
3970 if (num_is_integer (x) && num_is_integer (y)) 4007 if (is_integer (x) && is_integer (y))
3971 real_result = 0; 4008 real_result = 0;
3972 4009
3973 /* This 'if' is an R5RS compatibility fix. */ 4010 /* This 'if' is an R5RS compatibility fix. */
3974 /* NOTE: Remove this 'if' fix for R6RS. */ 4011 /* NOTE: Remove this 'if' fix for R6RS. */
3975 if (rvalue (x) == 0 && rvalue (y) < 0) 4012 if (rvalue (x) == 0 && rvalue (y) < 0)
3981 /* If the test fails, result is too big for integer. */ 4018 /* If the test fails, result is too big for integer. */
3982 if (!real_result) 4019 if (!real_result)
3983 { 4020 {
3984 long result_as_long = result; 4021 long result_as_long = result;
3985 4022
3986 if (result != (RVALUE) result_as_long) 4023 if (result != result_as_long)
3987 real_result = 1; 4024 real_result = 1;
3988 } 4025 }
3989 4026
3990 if (real_result) 4027 if (real_result)
3991 s_return (mk_real (SCHEME_A_ result)); 4028 s_return (mk_real (SCHEME_A_ result));
3996 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4033 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)))); 4034 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
3998 4035
3999 case OP_TRUNCATE: 4036 case OP_TRUNCATE:
4000 { 4037 {
4001 RVALUE rvalue_of_x; 4038 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))); 4039 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 } 4040 }
4010 4041
4011 case OP_ROUND: 4042 case OP_ROUND:
4012 if (num_is_integer (x)) 4043 if (is_integer (x))
4013 s_return (x); 4044 s_return (x);
4014 4045
4015 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4046 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4016#endif 4047#endif
4017 4048
4018 case OP_ADD: /* + */ 4049 case OP_ADD: /* + */
4019 v = num_zero; 4050 v = num_zero;
4020 4051
4021 for (x = args; x != NIL; x = cdr (x)) 4052 for (x = args; x != NIL; x = cdr (x))
4022 v = num_op ('+', v, nvalue (car (x))); 4053 v = num_op (NUM_ADD, v, nvalue (car (x)));
4023 4054
4024 s_return (mk_number (SCHEME_A_ v)); 4055 s_return (mk_number (SCHEME_A_ v));
4025 4056
4026 case OP_MUL: /* * */ 4057 case OP_MUL: /* * */
4027 v = num_one; 4058 v = num_one;
4028 4059
4029 for (x = args; x != NIL; x = cdr (x)) 4060 for (x = args; x != NIL; x = cdr (x))
4030 v = num_op ('+', v, nvalue (car (x))); 4061 v = num_op (NUM_MUL, v, nvalue (car (x)));
4031 4062
4032 s_return (mk_number (SCHEME_A_ v)); 4063 s_return (mk_number (SCHEME_A_ v));
4033 4064
4034 case OP_SUB: /* - */ 4065 case OP_SUB: /* - */
4035 if (cdr (args) == NIL) 4066 if (cdr (args) == NIL)
4042 x = cdr (args); 4073 x = cdr (args);
4043 v = nvalue (car (args)); 4074 v = nvalue (car (args));
4044 } 4075 }
4045 4076
4046 for (; x != NIL; x = cdr (x)) 4077 for (; x != NIL; x = cdr (x))
4047 v = num_op ('+', v, nvalue (car (x))); 4078 v = num_op (NUM_SUB, v, nvalue (car (x)));
4048 4079
4049 s_return (mk_number (SCHEME_A_ v)); 4080 s_return (mk_number (SCHEME_A_ v));
4050 4081
4051 case OP_DIV: /* / */ 4082 case OP_DIV: /* / */
4052 if (cdr (args) == NIL) 4083 if (cdr (args) == NIL)
4059 x = cdr (args); 4090 x = cdr (args);
4060 v = nvalue (car (args)); 4091 v = nvalue (car (args));
4061 } 4092 }
4062 4093
4063 for (; x != NIL; x = cdr (x)) 4094 for (; x != NIL; x = cdr (x))
4064 {
4065 if (!is_zero_rvalue (rvalue (car (x)))) 4095 if (!is_zero_rvalue (rvalue (car (x))))
4066 v = num_div (v, nvalue (car (x))); 4096 v = num_div (v, nvalue (car (x)));
4067 else 4097 else
4068 Error_0 ("/: division by zero"); 4098 Error_0 ("/: division by zero");
4069 }
4070 4099
4071 s_return (mk_number (SCHEME_A_ v)); 4100 s_return (mk_number (SCHEME_A_ v));
4072 4101
4073 case OP_INTDIV: /* quotient */ 4102 case OP_INTDIV: /* quotient */
4074 if (cdr (args) == NIL) 4103 if (cdr (args) == NIL)
4083 } 4112 }
4084 4113
4085 for (; x != NIL; x = cdr (x)) 4114 for (; x != NIL; x = cdr (x))
4086 { 4115 {
4087 if (ivalue (car (x)) != 0) 4116 if (ivalue (car (x)) != 0)
4088 v = num_op ('/', v, nvalue (car (x))); 4117 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4089 else 4118 else
4090 Error_0 ("quotient: division by zero"); 4119 Error_0 ("quotient: division by zero");
4091 } 4120 }
4092 4121
4093 s_return (mk_number (SCHEME_A_ v)); 4122 s_return (mk_number (SCHEME_A_ v));
4139 } 4168 }
4140 else 4169 else
4141 Error_0 ("set-cdr!: unable to alter immutable pair"); 4170 Error_0 ("set-cdr!: unable to alter immutable pair");
4142 4171
4143 case OP_CHAR2INT: /* char->integer */ 4172 case OP_CHAR2INT: /* char->integer */
4144 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4173 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4145 4174
4146 case OP_INT2CHAR: /* integer->char */ 4175 case OP_INT2CHAR: /* integer->char */
4147 s_return (mk_character (SCHEME_A_ ivalue (x))); 4176 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4148 4177
4149 case OP_CHARUPCASE: 4178 case OP_CHARUPCASE:
4150 { 4179 {
4151 unsigned char c = ivalue (x); 4180 unsigned char c = ivalue_unchecked (x);
4152 c = toupper (c); 4181 c = toupper (c);
4153 s_return (mk_character (SCHEME_A_ c)); 4182 s_return (mk_character (SCHEME_A_ c));
4154 } 4183 }
4155 4184
4156 case OP_CHARDNCASE: 4185 case OP_CHARDNCASE:
4157 { 4186 {
4158 unsigned char c = ivalue (x); 4187 unsigned char c = ivalue_unchecked (x);
4159 c = tolower (c); 4188 c = tolower (c);
4160 s_return (mk_character (SCHEME_A_ c)); 4189 s_return (mk_character (SCHEME_A_ c));
4161 } 4190 }
4162 4191
4163 case OP_STR2SYM: /* string->symbol */ 4192 case OP_STR2SYM: /* string->symbol */
4240 Error_1 ("atom->string: not an atom:", x); 4269 Error_1 ("atom->string: not an atom:", x);
4241 } 4270 }
4242 4271
4243 case OP_MKSTRING: /* make-string */ 4272 case OP_MKSTRING: /* make-string */
4244 { 4273 {
4245 int fill = ' '; 4274 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4246 int len;
4247
4248 len = ivalue (x); 4275 int len = ivalue_unchecked (x);
4249
4250 if (cdr (args) != NIL)
4251 fill = charvalue (cadr (args));
4252 4276
4253 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4277 s_return (mk_empty_string (SCHEME_A_ len, fill));
4254 } 4278 }
4255 4279
4256 case OP_STRLEN: /* string-length */ 4280 case OP_STRLEN: /* string-length */
4257 s_return (mk_integer (SCHEME_A_ strlength (x))); 4281 s_return (mk_integer (SCHEME_A_ strlength (x)));
4258 4282
4259 case OP_STRREF: /* string-ref */ 4283 case OP_STRREF: /* string-ref */
4260 { 4284 {
4261 char *str;
4262 int index;
4263
4264 str = strvalue (x); 4285 char *str = strvalue (x);
4265
4266 index = ivalue (cadr (args)); 4286 int index = ivalue_unchecked (cadr (args));
4267 4287
4268 if (index >= strlength (x)) 4288 if (index >= strlength (x))
4269 Error_1 ("string-ref: out of bounds:", cadr (args)); 4289 Error_1 ("string-ref: out of bounds:", cadr (args));
4270 4290
4271 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4291 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4272 } 4292 }
4273 4293
4274 case OP_STRSET: /* string-set! */ 4294 case OP_STRSET: /* string-set! */
4275 { 4295 {
4276 char *str; 4296 char *str = strvalue (x);
4277 int index; 4297 int index = ivalue_unchecked (cadr (args));
4278 int c; 4298 int c;
4279 4299
4280 if (is_immutable (x)) 4300 if (is_immutable (x))
4281 Error_1 ("string-set!: unable to alter immutable string:", x); 4301 Error_1 ("string-set!: unable to alter immutable string:", x);
4282
4283 str = strvalue (x);
4284
4285 index = ivalue (cadr (args));
4286 4302
4287 if (index >= strlength (x)) 4303 if (index >= strlength (x))
4288 Error_1 ("string-set!: out of bounds:", cadr (args)); 4304 Error_1 ("string-set!: out of bounds:", cadr (args));
4289 4305
4290 c = charvalue (caddr (args)); 4306 c = charvalue (caddr (args));
4313 s_return (newstr); 4329 s_return (newstr);
4314 } 4330 }
4315 4331
4316 case OP_SUBSTR: /* substring */ 4332 case OP_SUBSTR: /* substring */
4317 { 4333 {
4318 char *str; 4334 char *str = strvalue (x);
4319 int index0; 4335 int index0 = ivalue_unchecked (cadr (args));
4320 int index1; 4336 int index1;
4321 int len; 4337 int len;
4322 4338
4323 str = strvalue (x);
4324
4325 index0 = ivalue (cadr (args));
4326
4327 if (index0 > strlength (x)) 4339 if (index0 > strlength (x))
4328 Error_1 ("substring: start out of bounds:", cadr (args)); 4340 Error_1 ("substring: start out of bounds:", cadr (args));
4329 4341
4330 if (cddr (args) != NIL) 4342 if (cddr (args) != NIL)
4331 { 4343 {
4332 index1 = ivalue (caddr (args)); 4344 index1 = ivalue_unchecked (caddr (args));
4333 4345
4334 if (index1 > strlength (x) || index1 < index0) 4346 if (index1 > strlength (x) || index1 < index0)
4335 Error_1 ("substring: end out of bounds:", caddr (args)); 4347 Error_1 ("substring: end out of bounds:", caddr (args));
4336 } 4348 }
4337 else 4349 else
4360 if (SCHEME_V->no_memory) 4372 if (SCHEME_V->no_memory)
4361 s_return (S_SINK); 4373 s_return (S_SINK);
4362#endif 4374#endif
4363 4375
4364 for (x = args, i = 0; is_pair (x); x = cdr (x), i++) 4376 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4365 set_vector_elem (vec, i, car (x)); 4377 vector_set (vec, i, car (x));
4366 4378
4367 s_return (vec); 4379 s_return (vec);
4368 } 4380 }
4369 4381
4370 case OP_MKVECTOR: /* make-vector */ 4382 case OP_MKVECTOR: /* make-vector */
4371 { 4383 {
4372 pointer fill = NIL; 4384 pointer fill = NIL;
4373 int len;
4374 pointer vec; 4385 pointer vec;
4375
4376 len = ivalue (x); 4386 int len = ivalue_unchecked (x);
4377 4387
4378 if (cdr (args) != NIL) 4388 if (cdr (args) != NIL)
4379 fill = cadr (args); 4389 fill = cadr (args);
4380 4390
4381 vec = mk_vector (SCHEME_A_ len); 4391 vec = mk_vector (SCHEME_A_ len);
4384 if (SCHEME_V->no_memory) 4394 if (SCHEME_V->no_memory)
4385 s_return (S_SINK); 4395 s_return (S_SINK);
4386#endif 4396#endif
4387 4397
4388 if (fill != NIL) 4398 if (fill != NIL)
4389 fill_vector (vec, fill); 4399 fill_vector (vec, 0, fill);
4390 4400
4391 s_return (vec); 4401 s_return (vec);
4392 } 4402 }
4393 4403
4394 case OP_VECLEN: /* vector-length */ 4404 case OP_VECLEN: /* vector-length */
4395 s_return (mk_integer (SCHEME_A_ veclength (x))); 4405 s_return (mk_integer (SCHEME_A_ veclength (x)));
4396 4406
4397 case OP_VECREF: /* vector-ref */ 4407 case OP_VECREF: /* vector-ref */
4398 { 4408 {
4399 int index;
4400
4401 index = ivalue (cadr (args)); 4409 int index = ivalue_unchecked (cadr (args));
4402 4410
4403 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4411 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4404 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4412 Error_1 ("vector-ref: out of bounds:", cadr (args));
4405 4413
4406 s_return (vector_elem (x, index)); 4414 s_return (vector_get (x, index));
4407 } 4415 }
4408 4416
4409 case OP_VECSET: /* vector-set! */ 4417 case OP_VECSET: /* vector-set! */
4410 { 4418 {
4411 int index; 4419 int index = ivalue_unchecked (cadr (args));
4412 4420
4413 if (is_immutable (x)) 4421 if (is_immutable (x))
4414 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4422 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4415 4423
4416 index = ivalue (cadr (args));
4417
4418 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4424 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4419 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4425 Error_1 ("vector-set!: out of bounds:", cadr (args));
4420 4426
4421 set_vector_elem (x, index, caddr (args)); 4427 vector_set (x, index, caddr (args));
4422 s_return (x); 4428 s_return (x);
4423 } 4429 }
4424 } 4430 }
4425 4431
4426 return S_T; 4432 if (USE_ERROR_CHECKING) abort ();
4427} 4433}
4428 4434
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 4435static int
4484opexe_r (SCHEME_P_ enum scheme_opcodes op) 4436opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4485{ 4437{
4486 pointer x = SCHEME_V->args; 4438 pointer x = SCHEME_V->args;
4487 4439
4488 for (;;) 4440 for (;;)
4489 { 4441 {
4509 } 4461 }
4510 4462
4511 s_return (S_T); 4463 s_return (S_T);
4512} 4464}
4513 4465
4514static pointer 4466static int
4515opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4467opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4516{ 4468{
4517 pointer args = SCHEME_V->args; 4469 pointer args = SCHEME_V->args;
4518 pointer a = car (args); 4470 pointer a = car (args);
4519 pointer d = cdr (args); 4471 pointer d = cdr (args);
4531 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4483 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4532 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4484 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4533 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4485 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4534 4486
4535#if USE_CHAR_CLASSIFIERS 4487#if USE_CHAR_CLASSIFIERS
4536 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4488 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4537 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4489 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4538 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4490 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4539 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4491 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4540 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4492 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4541#endif 4493#endif
4542 4494
4543#if USE_PORTS 4495#if USE_PORTS
4544 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4496 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4545 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4497 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4565 } 4517 }
4566 4518
4567 s_retbool (r); 4519 s_retbool (r);
4568} 4520}
4569 4521
4570static pointer 4522static int
4571opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4523opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4572{ 4524{
4573 pointer args = SCHEME_V->args; 4525 pointer args = SCHEME_V->args;
4574 pointer a = car (args); 4526 pointer a = car (args);
4575 pointer x, y; 4527 pointer x, y;
4661 putstr (SCHEME_A_ "\n"); 4613 putstr (SCHEME_A_ "\n");
4662 4614
4663 if (SCHEME_V->interactive_repl) 4615 if (SCHEME_V->interactive_repl)
4664 s_goto (OP_T0LVL); 4616 s_goto (OP_T0LVL);
4665 else 4617 else
4666 return NIL; 4618 return -1;
4667 } 4619 }
4668 4620
4669 case OP_REVERSE: /* reverse */ 4621 case OP_REVERSE: /* reverse */
4670 s_return (reverse (SCHEME_A_ a)); 4622 s_return (reverse (SCHEME_A_ a));
4671 4623
4728 4680
4729 case OP_QUIT: /* quit */ 4681 case OP_QUIT: /* quit */
4730 if (is_pair (args)) 4682 if (is_pair (args))
4731 SCHEME_V->retcode = ivalue (a); 4683 SCHEME_V->retcode = ivalue (a);
4732 4684
4733 return NIL; 4685 return -1;
4734 4686
4735 case OP_GC: /* gc */ 4687 case OP_GC: /* gc */
4736 gc (SCHEME_A_ NIL, NIL); 4688 gc (SCHEME_A_ NIL, NIL);
4737 s_return (S_T); 4689 s_return (S_T);
4738 4690
4746 4698
4747 case OP_NEWSEGMENT: /* new-segment */ 4699 case OP_NEWSEGMENT: /* new-segment */
4748 if (!is_pair (args) || !is_number (a)) 4700 if (!is_pair (args) || !is_number (a))
4749 Error_0 ("new-segment: argument must be a number"); 4701 Error_0 ("new-segment: argument must be a number");
4750 4702
4751 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 4703 alloc_cellseg (SCHEME_A_ ivalue (a));
4752 4704
4753 s_return (S_T); 4705 s_return (S_T);
4754 4706
4755 case OP_OBLIST: /* oblist */ 4707 case OP_OBLIST: /* oblist */
4756 s_return (oblist_all_symbols (SCHEME_A)); 4708 s_return (oblist_all_symbols (SCHEME_A));
4785 break; 4737 break;
4786 } 4738 }
4787 4739
4788 p = port_from_filename (SCHEME_A_ strvalue (a), prop); 4740 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4789 4741
4790 if (p == NIL) 4742 s_return (p == NIL ? S_F : p);
4791 s_return (S_F);
4792
4793 s_return (p);
4794 } 4743 }
4795 4744
4796# if USE_STRING_PORTS 4745# if USE_STRING_PORTS
4797 4746
4798 case OP_OPEN_INSTRING: /* open-input-string */ 4747 case OP_OPEN_INSTRING: /* open-input-string */
4813 } 4762 }
4814 4763
4815 p = port_from_string (SCHEME_A_ strvalue (a), 4764 p = port_from_string (SCHEME_A_ strvalue (a),
4816 strvalue (a) + strlength (a), prop); 4765 strvalue (a) + strlength (a), prop);
4817 4766
4818 if (p == NIL) 4767 s_return (p == NIL ? S_F : p);
4819 s_return (S_F);
4820
4821 s_return (p);
4822 } 4768 }
4823 4769
4824 case OP_OPEN_OUTSTRING: /* open-output-string */ 4770 case OP_OPEN_OUTSTRING: /* open-output-string */
4825 { 4771 {
4826 pointer p; 4772 pointer p;
4827 4773
4828 if (a == NIL) 4774 if (a == NIL)
4829 {
4830 p = port_from_scratch (SCHEME_A); 4775 p = port_from_scratch (SCHEME_A);
4831
4832 if (p == NIL)
4833 s_return (S_F);
4834 }
4835 else 4776 else
4836 {
4837 p = port_from_string (SCHEME_A_ strvalue (a), 4777 p = port_from_string (SCHEME_A_ strvalue (a),
4838 strvalue (a) + strlength (a), port_output); 4778 strvalue (a) + strlength (a), port_output);
4839 4779
4840 if (p == NIL) 4780 s_return (p == NIL ? S_F : p);
4841 s_return (S_F);
4842 }
4843
4844 s_return (p);
4845 } 4781 }
4846 4782
4847 case OP_GET_OUTSTRING: /* get-output-string */ 4783 case OP_GET_OUTSTRING: /* get-output-string */
4848 { 4784 {
4849 port *p; 4785 port *p;
4888 case OP_CURR_ENV: /* current-environment */ 4824 case OP_CURR_ENV: /* current-environment */
4889 s_return (SCHEME_V->envir); 4825 s_return (SCHEME_V->envir);
4890 4826
4891 } 4827 }
4892 4828
4893 abort (); 4829 if (USE_ERROR_CHECKING) abort ();
4894} 4830}
4895 4831
4896static pointer 4832static int
4897opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4833opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4898{ 4834{
4835 pointer args = SCHEME_V->args;
4899 pointer x; 4836 pointer x;
4900 4837
4901 if (SCHEME_V->nesting != 0) 4838 if (SCHEME_V->nesting != 0)
4902 { 4839 {
4903 int n = SCHEME_V->nesting; 4840 int n = SCHEME_V->nesting;
4910 switch (op) 4847 switch (op)
4911 { 4848 {
4912 /* ========== reading part ========== */ 4849 /* ========== reading part ========== */
4913#if USE_PORTS 4850#if USE_PORTS
4914 case OP_READ: 4851 case OP_READ:
4915 if (!is_pair (SCHEME_V->args)) 4852 if (!is_pair (args))
4916 s_goto (OP_READ_INTERNAL); 4853 s_goto (OP_READ_INTERNAL);
4917 4854
4918 if (!is_inport (car (SCHEME_V->args))) 4855 if (!is_inport (car (args)))
4919 Error_1 ("read: not an input port:", car (SCHEME_V->args)); 4856 Error_1 ("read: not an input port:", car (args));
4920 4857
4921 if (car (SCHEME_V->args) == SCHEME_V->inport) 4858 if (car (args) == SCHEME_V->inport)
4922 s_goto (OP_READ_INTERNAL); 4859 s_goto (OP_READ_INTERNAL);
4923 4860
4924 x = SCHEME_V->inport; 4861 x = SCHEME_V->inport;
4925 SCHEME_V->inport = car (SCHEME_V->args); 4862 SCHEME_V->inport = car (args);
4926 x = cons (x, NIL); 4863 x = cons (x, NIL);
4927 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4864 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
4928 s_goto (OP_READ_INTERNAL); 4865 s_goto (OP_READ_INTERNAL);
4929 4866
4930 case OP_READ_CHAR: /* read-char */ 4867 case OP_READ_CHAR: /* read-char */
4931 case OP_PEEK_CHAR: /* peek-char */ 4868 case OP_PEEK_CHAR: /* peek-char */
4932 { 4869 {
4933 int c; 4870 int c;
4934 4871
4935 if (is_pair (SCHEME_V->args)) 4872 if (is_pair (args))
4936 { 4873 {
4937 if (car (SCHEME_V->args) != SCHEME_V->inport) 4874 if (car (args) != SCHEME_V->inport)
4938 { 4875 {
4939 x = SCHEME_V->inport; 4876 x = SCHEME_V->inport;
4940 x = cons (x, NIL); 4877 x = cons (x, NIL);
4941 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4878 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
4942 SCHEME_V->inport = car (SCHEME_V->args); 4879 SCHEME_V->inport = car (args);
4943 } 4880 }
4944 } 4881 }
4945 4882
4946 c = inchar (SCHEME_A); 4883 c = inchar (SCHEME_A);
4947 4884
4957 case OP_CHAR_READY: /* char-ready? */ 4894 case OP_CHAR_READY: /* char-ready? */
4958 { 4895 {
4959 pointer p = SCHEME_V->inport; 4896 pointer p = SCHEME_V->inport;
4960 int res; 4897 int res;
4961 4898
4962 if (is_pair (SCHEME_V->args)) 4899 if (is_pair (args))
4963 p = car (SCHEME_V->args); 4900 p = car (args);
4964 4901
4965 res = p->object.port->kind & port_string; 4902 res = p->object.port->kind & port_string;
4966 4903
4967 s_retbool (res); 4904 s_retbool (res);
4968 } 4905 }
4969 4906
4970 case OP_SET_INPORT: /* set-input-port */ 4907 case OP_SET_INPORT: /* set-input-port */
4971 SCHEME_V->inport = car (SCHEME_V->args); 4908 SCHEME_V->inport = car (args);
4972 s_return (SCHEME_V->value); 4909 s_return (SCHEME_V->value);
4973 4910
4974 case OP_SET_OUTPORT: /* set-output-port */ 4911 case OP_SET_OUTPORT: /* set-output-port */
4975 SCHEME_V->outport = car (SCHEME_V->args); 4912 SCHEME_V->outport = car (args);
4976 s_return (SCHEME_V->value); 4913 s_return (SCHEME_V->value);
4977#endif 4914#endif
4978 4915
4979 case OP_RDSEXPR: 4916 case OP_RDSEXPR:
4980 switch (SCHEME_V->tok) 4917 switch (SCHEME_V->tok)
5066 } 5003 }
5067 5004
5068 break; 5005 break;
5069 5006
5070 case OP_RDLIST: 5007 case OP_RDLIST:
5071 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 5008 SCHEME_V->args = cons (SCHEME_V->value, args);
5072 SCHEME_V->tok = token (SCHEME_A); 5009 SCHEME_V->tok = token (SCHEME_A);
5073 5010
5074 switch (SCHEME_V->tok) 5011 switch (SCHEME_V->tok)
5075 { 5012 {
5076 case TOK_EOF: 5013 case TOK_EOF:
5104 case OP_RDDOT: 5041 case OP_RDDOT:
5105 if (token (SCHEME_A) != TOK_RPAREN) 5042 if (token (SCHEME_A) != TOK_RPAREN)
5106 Error_0 ("syntax error: illegal dot expression"); 5043 Error_0 ("syntax error: illegal dot expression");
5107 5044
5108 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5045 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5109 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); 5046 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, args));
5110 5047
5111 case OP_RDQUOTE: 5048 case OP_RDQUOTE:
5112 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); 5049 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)));
5113 5050
5114 case OP_RDQQUOTE: 5051 case OP_RDQQUOTE:
5136 SCHEME_V->args = SCHEME_V->value; 5073 SCHEME_V->args = SCHEME_V->value;
5137 s_goto (OP_VECTOR); 5074 s_goto (OP_VECTOR);
5138 5075
5139 /* ========== printing part ========== */ 5076 /* ========== printing part ========== */
5140 case OP_P0LIST: 5077 case OP_P0LIST:
5141 if (is_vector (SCHEME_V->args)) 5078 if (is_vector (args))
5142 { 5079 {
5143 putstr (SCHEME_A_ "#("); 5080 putstr (SCHEME_A_ "#(");
5144 SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); 5081 SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0));
5145 s_goto (OP_PVECFROM); 5082 s_goto (OP_PVECFROM);
5146 } 5083 }
5147 else if (is_environment (SCHEME_V->args)) 5084 else if (is_environment (args))
5148 { 5085 {
5149 putstr (SCHEME_A_ "#<ENVIRONMENT>"); 5086 putstr (SCHEME_A_ "#<ENVIRONMENT>");
5150 s_return (S_T); 5087 s_return (S_T);
5151 } 5088 }
5152 else if (!is_pair (SCHEME_V->args)) 5089 else if (!is_pair (args))
5153 { 5090 {
5154 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5091 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5155 s_return (S_T); 5092 s_return (S_T);
5156 } 5093 }
5157 else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5094 else
5158 { 5095 {
5096 pointer a = car (args);
5097 pointer b = cdr (args);
5098 int ok_abbr = ok_abbrev (b);
5099 SCHEME_V->args = car (b);
5100
5101 if (a == SCHEME_V->QUOTE && ok_abbr)
5159 putstr (SCHEME_A_ "'"); 5102 putstr (SCHEME_A_ "'");
5160 SCHEME_V->args = cadr (SCHEME_V->args); 5103 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5104 putstr (SCHEME_A_ "`");
5105 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5106 putstr (SCHEME_A_ ",");
5107 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5108 putstr (SCHEME_A_ ",@");
5109 else
5110 {
5111 putstr (SCHEME_A_ "(");
5112 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5113 SCHEME_V->args = a;
5114 }
5115
5161 s_goto (OP_P0LIST); 5116 s_goto (OP_P0LIST);
5162 } 5117 }
5163 else if (car (SCHEME_V->args) == SCHEME_V->QQUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5118
5119 case OP_P1LIST:
5120 if (is_pair (args))
5164 { 5121 {
5122 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5165 putstr (SCHEME_A_ "`"); 5123 putstr (SCHEME_A_ " ");
5166 SCHEME_V->args = cadr (SCHEME_V->args); 5124 SCHEME_V->args = car (args);
5167 s_goto (OP_P0LIST); 5125 s_goto (OP_P0LIST);
5168 } 5126 }
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)) 5127 else if (is_vector (args))
5198 { 5128 {
5199 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); 5129 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL);
5200 putstr (SCHEME_A_ " . "); 5130 putstr (SCHEME_A_ " . ");
5201 s_goto (OP_P0LIST); 5131 s_goto (OP_P0LIST);
5202 } 5132 }
5203 else 5133 else
5204 { 5134 {
5205 if (SCHEME_V->args != NIL) 5135 if (args != NIL)
5206 { 5136 {
5207 putstr (SCHEME_A_ " . "); 5137 putstr (SCHEME_A_ " . ");
5208 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5138 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5209 } 5139 }
5210 5140
5211 putstr (SCHEME_A_ ")"); 5141 putstr (SCHEME_A_ ")");
5212 s_return (S_T); 5142 s_return (S_T);
5213 } 5143 }
5214 5144
5215 case OP_PVECFROM: 5145 case OP_PVECFROM:
5216 { 5146 {
5217 int i = ivalue_unchecked (cdr (SCHEME_V->args)); 5147 int i = ivalue_unchecked (cdr (args));
5218 pointer vec = car (SCHEME_V->args); 5148 pointer vec = car (args);
5219 int len = veclength (vec); 5149 int len = veclength (vec);
5220 5150
5221 if (i == len) 5151 if (i == len)
5222 { 5152 {
5223 putstr (SCHEME_A_ ")"); 5153 putstr (SCHEME_A_ ")");
5224 s_return (S_T); 5154 s_return (S_T);
5225 } 5155 }
5226 else 5156 else
5227 { 5157 {
5228 pointer elem = vector_elem (vec, i); 5158 pointer elem = vector_get (vec, i);
5229 5159
5230 ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; 5160 ivalue_unchecked (cdr (args)) = i + 1;
5231 s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); 5161 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5232 SCHEME_V->args = elem; 5162 SCHEME_V->args = elem;
5233 5163
5234 if (i > 0) 5164 if (i > 0)
5235 putstr (SCHEME_A_ " "); 5165 putstr (SCHEME_A_ " ");
5236 5166
5237 s_goto (OP_P0LIST); 5167 s_goto (OP_P0LIST);
5238 } 5168 }
5239 } 5169 }
5240 } 5170 }
5241 5171
5242 abort (); 5172 if (USE_ERROR_CHECKING) abort ();
5243} 5173}
5244 5174
5245static pointer 5175static int
5246opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5176opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5247{ 5177{
5178 pointer args = SCHEME_V->args;
5179 pointer a = car (args);
5248 pointer x, y; 5180 pointer x, y;
5249 5181
5250 switch (op) 5182 switch (op)
5251 { 5183 {
5252 case OP_LIST_LENGTH: /* length *//* a.k */ 5184 case OP_LIST_LENGTH: /* length *//* a.k */
5253 { 5185 {
5254 long v = list_length (SCHEME_A_ car (SCHEME_V->args)); 5186 long v = list_length (SCHEME_A_ a);
5255 5187
5256 if (v < 0) 5188 if (v < 0)
5257 Error_1 ("length: not a list:", car (SCHEME_V->args)); 5189 Error_1 ("length: not a list:", a);
5258 5190
5259 s_return (mk_integer (SCHEME_A_ v)); 5191 s_return (mk_integer (SCHEME_A_ v));
5260 } 5192 }
5261 5193
5262 case OP_ASSQ: /* assq *//* a.k */ 5194 case OP_ASSQ: /* assq *//* a.k */
5263 x = car (SCHEME_V->args); 5195 x = a;
5264 5196
5265 for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) 5197 for (y = cadr (args); is_pair (y); y = cdr (y))
5266 { 5198 {
5267 if (!is_pair (car (y))) 5199 if (!is_pair (car (y)))
5268 Error_0 ("unable to handle non pair element"); 5200 Error_0 ("unable to handle non pair element");
5269 5201
5270 if (x == caar (y)) 5202 if (x == caar (y))
5276 else 5208 else
5277 s_return (S_F); 5209 s_return (S_F);
5278 5210
5279 5211
5280 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5212 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5281 SCHEME_V->args = car (SCHEME_V->args); 5213 SCHEME_V->args = a;
5282 5214
5283 if (SCHEME_V->args == NIL) 5215 if (SCHEME_V->args == NIL)
5284 s_return (S_F); 5216 s_return (S_F);
5285 else if (is_closure (SCHEME_V->args)) 5217 else if (is_closure (SCHEME_V->args))
5286 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5218 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5292 case OP_CLOSUREP: /* closure? */ 5224 case OP_CLOSUREP: /* closure? */
5293 /* 5225 /*
5294 * Note, macro object is also a closure. 5226 * Note, macro object is also a closure.
5295 * Therefore, (closure? <#MACRO>) ==> #t 5227 * Therefore, (closure? <#MACRO>) ==> #t
5296 */ 5228 */
5297 s_retbool (is_closure (car (SCHEME_V->args))); 5229 s_retbool (is_closure (a));
5298 5230
5299 case OP_MACROP: /* macro? */ 5231 case OP_MACROP: /* macro? */
5300 s_retbool (is_macro (car (SCHEME_V->args))); 5232 s_retbool (is_macro (a));
5301 } 5233 }
5302 5234
5303 abort (); 5235 if (USE_ERROR_CHECKING) abort ();
5304} 5236}
5305 5237
5238/* 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); 5239typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5307 5240
5308typedef int (*test_predicate) (pointer); 5241typedef int (*test_predicate)(pointer);
5309static int 5242static int
5310is_any (pointer p) 5243tst_any (pointer p)
5311{ 5244{
5312 return 1; 5245 return 1;
5313} 5246}
5314 5247
5315static int 5248static int
5316is_nonneg (pointer p) 5249tst_inonneg (pointer p)
5317{ 5250{
5318 return ivalue (p) >= 0 && is_integer (p); 5251 return is_integer (p) && ivalue_unchecked (p) >= 0;
5252}
5253
5254static int
5255tst_is_list (SCHEME_P_ pointer p)
5256{
5257 return p == NIL || is_pair (p);
5319} 5258}
5320 5259
5321/* Correspond carefully with following defines! */ 5260/* Correspond carefully with following defines! */
5322static struct 5261static struct
5323{ 5262{
5324 test_predicate fct; 5263 test_predicate fct;
5325 const char *kind; 5264 const char *kind;
5326} tests[] = 5265} tests[] = {
5327{ 5266 { tst_any , 0 },
5328 { 0, 0}, /* unused */ 5267 { is_string , "string" },
5329 { is_any, 0}, 5268 { is_symbol , "symbol" },
5330 { is_string, "string" }, 5269 { is_port , "port" },
5331 { is_symbol, "symbol" },
5332 { is_port, "port" },
5333 { is_inport, "input port" }, 5270 { is_inport , "input port" },
5334 { is_outport, "output port" }, 5271 { is_outport , "output port" },
5335 { is_environment, "environment" }, 5272 { is_environment, "environment" },
5336 { is_pair, "pair" }, 5273 { is_pair , "pair" },
5337 { 0, "pair or '()" }, 5274 { 0 , "pair or '()" },
5338 { is_character, "character" }, 5275 { is_character , "character" },
5339 { is_vector, "vector" }, 5276 { is_vector , "vector" },
5340 { is_number, "number" }, 5277 { is_number , "number" },
5341 { is_integer, "integer" }, 5278 { is_integer , "integer" },
5342 { is_nonneg, "non-negative integer" } 5279 { tst_inonneg , "non-negative integer" }
5343}; 5280};
5344 5281
5345#define TST_NONE 0 5282#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5346#define TST_ANY "\001" 5283#define TST_ANY "\001"
5347#define TST_STRING "\002" 5284#define TST_STRING "\002"
5348#define TST_SYMBOL "\003" 5285#define TST_SYMBOL "\003"
5349#define TST_PORT "\004" 5286#define TST_PORT "\004"
5350#define TST_INPORT "\005" 5287#define TST_INPORT "\005"
5351#define TST_OUTPORT "\006" 5288#define TST_OUTPORT "\006"
5352#define TST_ENVIRONMENT "\007" 5289#define TST_ENVIRONMENT "\007"
5353#define TST_PAIR "\010" 5290#define TST_PAIR "\010"
5354#define TST_LIST "\011" 5291#define TST_LIST "\011"
5355#define TST_CHAR "\012" 5292#define TST_CHAR "\012"
5356#define TST_VECTOR "\013" 5293#define TST_VECTOR "\013"
5357#define TST_NUMBER "\014" 5294#define TST_NUMBER "\014"
5358#define TST_INTEGER "\015" 5295#define TST_INTEGER "\015"
5359#define TST_NATURAL "\016" 5296#define TST_NATURAL "\016"
5297
5298#define INF_ARG 0xff
5299#define UNNAMED_OP ""
5300
5301static const char opnames[] =
5302#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5303#include "opdefines.h"
5304#undef OP_DEF
5305;
5306
5307static const char *
5308opname (int idx)
5309{
5310 const char *name = opnames;
5311
5312 /* should do this at compile time, but would require external program, right? */
5313 while (idx--)
5314 name += strlen (name) + 1;
5315
5316 return *name ? name : "ILLEGAL";
5317}
5318
5319static const char *
5320procname (pointer x)
5321{
5322 return opname (procnum (x));
5323}
5360 5324
5361typedef struct 5325typedef struct
5362{ 5326{
5363 dispatch_func func; 5327 uint8_t func;
5364 char *name; 5328 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5329 uint8_t builtin;
5330#if USE_ERROR_CHECKING
5365 int min_arity; 5331 uint8_t min_arity;
5366 int max_arity; 5332 uint8_t max_arity;
5367 char *arg_tests_encoding; 5333 char arg_tests_encoding[3];
5334#endif
5368} op_code_info; 5335} op_code_info;
5369 5336
5370#define INF_ARG 0xffff
5371
5372static op_code_info dispatch_table[] = { 5337static const op_code_info dispatch_table[] = {
5373#define OP_DEF(A,B,C,D,E,OP) {A,B,C,D,E}, 5338#if USE_ERROR_CHECKING
5339#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5340#else
5341#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5342#endif
5374#include "opdefines.h" 5343#include "opdefines.h"
5344#undef OP_DEF
5375 {0} 5345 {0}
5376}; 5346};
5377 5347
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 */ 5348/* kernel of this interpreter */
5391static void 5349static void ecb_hot
5392Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5350Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5393{ 5351{
5394 SCHEME_V->op = op; 5352 SCHEME_V->op = op;
5395 5353
5396 for (;;) 5354 for (;;)
5397 { 5355 {
5398 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5356 const op_code_info *pcd = dispatch_table + SCHEME_V->op;
5399 5357
5400#if USE_ERROR_CHECKING 5358#if USE_ERROR_CHECKING
5401 if (pcd->name) /* if built-in function, check arguments */ 5359 if (pcd->builtin) /* if built-in function, check arguments */
5402 { 5360 {
5403 int ok = 1;
5404 char msg[STRBUFFSIZE]; 5361 char msg[STRBUFFSIZE];
5405 int n = list_length (SCHEME_A_ SCHEME_V->args); 5362 int n = list_length (SCHEME_A_ SCHEME_V->args);
5406 5363
5407 /* Check number of arguments */ 5364 /* Check number of arguments */
5408 if (ecb_expect_false (n < pcd->min_arity)) 5365 if (ecb_expect_false (n < pcd->min_arity))
5409 { 5366 {
5410 ok = 0;
5411 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5367 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5412 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); 5368 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity);
5369 xError_1 (SCHEME_A_ msg, 0);
5370 continue;
5413 } 5371 }
5414 else if (ecb_expect_false (n > pcd->max_arity)) 5372 else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG))
5415 { 5373 {
5416 ok = 0;
5417 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5374 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5418 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); 5375 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity);
5376 xError_1 (SCHEME_A_ msg, 0);
5377 continue;
5419 } 5378 }
5420 5379 else
5421 if (ecb_expect_false (ok))
5422 { 5380 {
5423 if (pcd->arg_tests_encoding) 5381 if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */
5424 { 5382 {
5425 int i = 0; 5383 int i = 0;
5426 int j; 5384 int j;
5427 const char *t = pcd->arg_tests_encoding; 5385 const char *t = pcd->arg_tests_encoding;
5428 pointer arglist = SCHEME_V->args; 5386 pointer arglist = SCHEME_V->args;
5429 5387
5430 do 5388 do
5431 { 5389 {
5432 pointer arg = car (arglist); 5390 pointer arg = car (arglist);
5433 5391
5434 j = (int) t[0]; 5392 j = t[0];
5435 5393
5394 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5436 if (j == TST_LIST[0]) 5395 if (j == TST_LIST[0])
5437 { 5396 {
5438 if (arg != NIL && !is_pair (arg)) 5397 if (!tst_is_list (SCHEME_A_ arg))
5439 break; 5398 break;
5440 } 5399 }
5441 else 5400 else
5442 { 5401 {
5443 if (!tests[j].fct (arg)) 5402 if (!tests[j - 1].fct (arg))
5444 break; 5403 break;
5445 } 5404 }
5446 5405
5447 if (t[1] != 0) /* last test is replicated as necessary */ 5406 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5448 t++; 5407 t++;
5449 5408
5450 arglist = cdr (arglist); 5409 arglist = cdr (arglist);
5451 i++; 5410 i++;
5452 } 5411 }
5453 while (i < n); 5412 while (i < n);
5454 5413
5455 if (i < n) 5414 if (i < n)
5456 { 5415 {
5457 ok = 0;
5458 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); 5416 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind);
5417 xError_1 (SCHEME_A_ msg, 0);
5418 continue;
5459 } 5419 }
5460 } 5420 }
5461 } 5421 }
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 } 5422 }
5471#endif 5423#endif
5472 5424
5473 ok_to_freely_gc (SCHEME_A); 5425 ok_to_freely_gc (SCHEME_A);
5474 5426
5427 static const dispatch_func dispatch_funcs[] = {
5428 opexe_0,
5429 opexe_1,
5430 opexe_2,
5431 opexe_3,
5432 opexe_4,
5433 opexe_5,
5434 opexe_6,
5435 };
5436
5475 if (ecb_expect_false (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL)) 5437 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5476 return; 5438 return;
5477 5439
5478 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5440 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5479 { 5441 {
5480 xwrstr ("No memory!\n"); 5442 xwrstr ("No memory!\n");
5504mk_proc (SCHEME_P_ enum scheme_opcodes op) 5466mk_proc (SCHEME_P_ enum scheme_opcodes op)
5505{ 5467{
5506 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5468 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5507 set_typeflag (y, (T_PROC | T_ATOM)); 5469 set_typeflag (y, (T_PROC | T_ATOM));
5508 ivalue_unchecked (y) = op; 5470 ivalue_unchecked (y) = op;
5509 set_num_integer (y);
5510 return y; 5471 return y;
5511} 5472}
5512 5473
5513/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5474/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5514static int 5475static int
5579 return OP_C0STREAM; /* cons-stream */ 5540 return OP_C0STREAM; /* cons-stream */
5580 } 5541 }
5581} 5542}
5582 5543
5583#if USE_MULTIPLICITY 5544#if USE_MULTIPLICITY
5584scheme * 5545ecb_cold scheme *
5585scheme_init_new () 5546scheme_init_new ()
5586{ 5547{
5587 scheme *sc = malloc (sizeof (scheme)); 5548 scheme *sc = malloc (sizeof (scheme));
5588 5549
5589 if (!scheme_init (SCHEME_A)) 5550 if (!scheme_init (SCHEME_A))
5594 else 5555 else
5595 return sc; 5556 return sc;
5596} 5557}
5597#endif 5558#endif
5598 5559
5599int 5560ecb_cold int
5600scheme_init (SCHEME_P) 5561scheme_init (SCHEME_P)
5601{ 5562{
5602 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5563 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5603 pointer x; 5564 pointer x;
5604 5565
5677 5638
5678 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) 5639 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
5679 assign_syntax (SCHEME_A_ syntax_names[i]); 5640 assign_syntax (SCHEME_A_ syntax_names[i]);
5680 } 5641 }
5681 5642
5643 // TODO: should iterate via strlen, to avoid n² complexity
5682 for (i = 0; i < n; i++) 5644 for (i = 0; i < n; i++)
5683 if (dispatch_table[i].name != 0) 5645 if (dispatch_table[i].builtin)
5684 assign_proc (SCHEME_A_ i, dispatch_table[i].name); 5646 assign_proc (SCHEME_A_ i, opname (i));
5685 5647
5686 /* initialization of global pointers to special symbols */ 5648 /* initialization of global pointers to special symbols */
5687 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 5649 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
5688 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 5650 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
5689 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 5651 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
5728scheme_set_external_data (SCHEME_P_ void *p) 5690scheme_set_external_data (SCHEME_P_ void *p)
5729{ 5691{
5730 SCHEME_V->ext_data = p; 5692 SCHEME_V->ext_data = p;
5731} 5693}
5732 5694
5733void 5695ecb_cold void
5734scheme_deinit (SCHEME_P) 5696scheme_deinit (SCHEME_P)
5735{ 5697{
5736 int i; 5698 int i;
5737 5699
5738#if SHOW_ERROR_LINE 5700#if SHOW_ERROR_LINE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines