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.34 by root, Sat Nov 28 22:14:49 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
1055 return x; 1079 return x;
1056} 1080}
1057 1081
1058/* ========== oblist implementation ========== */ 1082/* ========== oblist implementation ========== */
1059 1083
1084static pointer
1085generate_symbol (SCHEME_P_ const char *name)
1086{
1087 pointer x = mk_string (SCHEME_A_ name);
1088 setimmutable (x);
1089 x = immutable_cons (x, NIL);
1090 set_typeflag (x, T_SYMBOL);
1091 return x;
1092}
1093
1060#ifndef USE_OBJECT_LIST 1094#ifndef USE_OBJECT_LIST
1061 1095
1096static int
1062static int hash_fn (const char *key, int table_size); 1097hash_fn (const char *key, int table_size)
1098{
1099 const unsigned char *p = key;
1100 uint32_t hash = 2166136261;
1101
1102 while (*p)
1103 hash = (hash ^ *p++) * 16777619;
1104
1105 return hash % table_size;
1106}
1063 1107
1064static pointer 1108static pointer
1065oblist_initial_value (SCHEME_P) 1109oblist_initial_value (SCHEME_P)
1066{ 1110{
1067 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1111 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1069 1113
1070/* returns the new symbol */ 1114/* returns the new symbol */
1071static pointer 1115static pointer
1072oblist_add_by_name (SCHEME_P_ const char *name) 1116oblist_add_by_name (SCHEME_P_ const char *name)
1073{ 1117{
1074 int location; 1118 pointer x = generate_symbol (SCHEME_A_ name);
1075
1076 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1077 set_typeflag (x, T_SYMBOL);
1078 setimmutable (car (x));
1079
1080 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1119 int 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))); 1120 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1082 return x; 1121 return x;
1083} 1122}
1084 1123
1085static INLINE pointer 1124ecb_inline pointer
1086oblist_find_by_name (SCHEME_P_ const char *name) 1125oblist_find_by_name (SCHEME_P_ const char *name)
1087{ 1126{
1088 int location; 1127 int location;
1089 pointer x; 1128 pointer x;
1090 char *s; 1129 char *s;
1091 1130
1092 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1131 location = hash_fn (name, veclength (SCHEME_V->oblist));
1093 1132
1094 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1133 for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1095 { 1134 {
1096 s = symname (car (x)); 1135 s = symname (car (x));
1097 1136
1098 /* case-insensitive, per R5RS section 2 */ 1137 /* case-insensitive, per R5RS section 2 */
1099 if (stricmp (name, s) == 0) 1138 if (stricmp (name, s) == 0)
1109 int i; 1148 int i;
1110 pointer x; 1149 pointer x;
1111 pointer ob_list = NIL; 1150 pointer ob_list = NIL;
1112 1151
1113 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1152 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1114 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1153 for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1115 ob_list = cons (x, ob_list); 1154 ob_list = cons (x, ob_list);
1116 1155
1117 return ob_list; 1156 return ob_list;
1118} 1157}
1119 1158
1123oblist_initial_value (SCHEME_P) 1162oblist_initial_value (SCHEME_P)
1124{ 1163{
1125 return NIL; 1164 return NIL;
1126} 1165}
1127 1166
1128static INLINE pointer 1167ecb_inline pointer
1129oblist_find_by_name (SCHEME_P_ const char *name) 1168oblist_find_by_name (SCHEME_P_ const char *name)
1130{ 1169{
1131 pointer x; 1170 pointer x;
1132 char *s; 1171 char *s;
1133 1172
1145 1184
1146/* returns the new symbol */ 1185/* returns the new symbol */
1147static pointer 1186static pointer
1148oblist_add_by_name (SCHEME_P_ const char *name) 1187oblist_add_by_name (SCHEME_P_ const char *name)
1149{ 1188{
1150 pointer x; 1189 pointer x = mk_string (SCHEME_A_ name);
1151
1152 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1153 set_typeflag (x, T_SYMBOL); 1190 set_typeflag (x, T_SYMBOL);
1154 setimmutable (car (x)); 1191 setimmutable (x);
1155 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1192 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1156 return x; 1193 return x;
1157} 1194}
1158 1195
1159static pointer 1196static pointer
1192mk_character (SCHEME_P_ int c) 1229mk_character (SCHEME_P_ int c)
1193{ 1230{
1194 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1231 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1195 1232
1196 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1233 set_typeflag (x, (T_CHARACTER | T_ATOM));
1197 ivalue_unchecked (x) = c & 0xff; 1234 set_ivalue (x, c & 0xff);
1198 set_num_integer (x); 1235
1199 return x; 1236 return x;
1200} 1237}
1201 1238
1202/* get number atom (integer) */ 1239/* get number atom (integer) */
1203INTERFACE pointer 1240INTERFACE pointer
1204mk_integer (SCHEME_P_ long num) 1241mk_integer (SCHEME_P_ long n)
1205{ 1242{
1206 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1243 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1207 1244
1208 set_typeflag (x, (T_NUMBER | T_ATOM)); 1245 set_typeflag (x, (T_INTEGER | T_ATOM));
1209 ivalue_unchecked (x) = num; 1246 set_ivalue (x, n);
1210 set_num_integer (x); 1247
1211 return x; 1248 return x;
1212} 1249}
1213 1250
1214INTERFACE pointer 1251INTERFACE pointer
1215mk_real (SCHEME_P_ RVALUE n) 1252mk_real (SCHEME_P_ RVALUE n)
1216{ 1253{
1254#if USE_REAL
1217 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1255 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1218 1256
1219 set_typeflag (x, (T_NUMBER | T_ATOM)); 1257 set_typeflag (x, (T_REAL | T_ATOM));
1220 rvalue_unchecked (x) = n; 1258 set_rvalue (x, n);
1221 set_num_real (x); 1259
1222 return x; 1260 return x;
1261#else
1262 return mk_integer (SCHEME_A_ n);
1263#endif
1223} 1264}
1224 1265
1225static pointer 1266static pointer
1226mk_number (SCHEME_P_ const num n) 1267mk_number (SCHEME_P_ const num n)
1227{ 1268{
1269#if USE_REAL
1228 if (num_is_fixnum (n)) 1270 return num_is_fixnum (n)
1271 ? mk_integer (SCHEME_A_ num_ivalue (n))
1272 : mk_real (SCHEME_A_ num_rvalue (n));
1273#else
1229 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1274 return mk_integer (SCHEME_A_ num_ivalue (n));
1230 else 1275#endif
1231 return mk_real (SCHEME_A_ num_get_rvalue (n));
1232} 1276}
1233 1277
1234/* allocate name to string area */ 1278/* allocate name to string area */
1235static char * 1279static char *
1236store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) 1280store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1295{ 1339{
1296 return get_vector_object (SCHEME_A_ len, NIL); 1340 return get_vector_object (SCHEME_A_ len, NIL);
1297} 1341}
1298 1342
1299INTERFACE void 1343INTERFACE void
1300fill_vector (pointer vec, pointer obj) 1344fill_vector (pointer vec, uint32_t start, pointer obj)
1301{ 1345{
1302 int i; 1346 int i;
1303 1347
1304 for (i = 0; i < vec->object.vector.length; i++) 1348 for (i = start; i < veclength (vec); i++)
1305 vecvalue (vec)[i] = obj; 1349 vecvalue (vec)[i] = obj;
1306} 1350}
1307 1351
1308INTERFACE pointer 1352INTERFACE pointer
1309vector_elem (pointer vec, uint32_t ielem) 1353vector_get (pointer vec, uint32_t ielem)
1310{ 1354{
1311 return vecvalue(vec)[ielem]; 1355 return vecvalue(vec)[ielem];
1312} 1356}
1313 1357
1314INTERFACE void 1358INTERFACE void
1315set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1359vector_set (pointer vec, uint32_t ielem, pointer a)
1316{ 1360{
1317 vecvalue(vec)[ielem] = a; 1361 vecvalue(vec)[ielem] = a;
1318} 1362}
1319 1363
1320/* get new symbol */ 1364/* get new symbol */
1332 1376
1333INTERFACE pointer 1377INTERFACE pointer
1334gensym (SCHEME_P) 1378gensym (SCHEME_P)
1335{ 1379{
1336 pointer x; 1380 pointer x;
1337
1338 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1339 {
1340 char name[40] = "gensym-"; 1381 char name[40] = "gensym-";
1341 xnum (name + 7, SCHEME_V->gensym_cnt); 1382 xnum (name + 7, SCHEME_V->gensym_cnt);
1342 1383
1343 /* first check oblist */ 1384 return generate_symbol (SCHEME_A_ name);
1344 x = oblist_find_by_name (SCHEME_A_ name);
1345
1346 if (x == NIL)
1347 {
1348 x = oblist_add_by_name (SCHEME_A_ name);
1349 return x;
1350 }
1351 }
1352
1353 return NIL;
1354} 1385}
1355 1386
1356/* make symbol or number atom from string */ 1387/* make symbol or number atom from string */
1357static pointer 1388static pointer
1358mk_atom (SCHEME_P_ char *q) 1389mk_atom (SCHEME_P_ char *q)
1412 } 1443 }
1413 else if ((c == 'e') || (c == 'E')) 1444 else if ((c == 'e') || (c == 'E'))
1414 { 1445 {
1415 if (!has_fp_exp) 1446 if (!has_fp_exp)
1416 { 1447 {
1417 has_dec_point = 1; /* decimal point illegal 1448 has_dec_point = 1; /* decimal point illegal from now on */
1418 from now on */
1419 p++; 1449 p++;
1420 1450
1421 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1451 if ((*p == '-') || (*p == '+') || isdigit (*p))
1422 continue; 1452 continue;
1423 } 1453 }
1511 1541
1512 if (ecb_expect_false (is_vector (p))) 1542 if (ecb_expect_false (is_vector (p)))
1513 { 1543 {
1514 int i; 1544 int i;
1515 1545
1516 for (i = 0; i < p->object.vector.length; i++) 1546 for (i = 0; i < veclength (p); i++)
1517 mark (vecvalue (p)[i]); 1547 mark (vecvalue (p)[i]);
1518 } 1548 }
1519 1549
1520 if (is_atom (p)) 1550 if (is_atom (p))
1521 goto E6; 1551 goto E6;
1619 if (is_mark (p)) 1649 if (is_mark (p))
1620 clrmark (p); 1650 clrmark (p);
1621 else 1651 else
1622 { 1652 {
1623 /* reclaim cell */ 1653 /* reclaim cell */
1624 if (typeflag (p) != T_FREE) 1654 if (typeflag (p) != T_PAIR)
1625 { 1655 {
1626 finalize_cell (SCHEME_A_ p); 1656 finalize_cell (SCHEME_A_ p);
1627 set_typeflag (p, T_FREE); 1657 set_typeflag (p, T_PAIR);
1628 set_car (p, NIL); 1658 set_car (p, NIL);
1629 } 1659 }
1630 1660
1631 ++SCHEME_V->fcells; 1661 ++SCHEME_V->fcells;
1632 set_cdr (p, SCHEME_V->free_cell); 1662 set_cdr (p, SCHEME_V->free_cell);
1634 } 1664 }
1635 } 1665 }
1636 } 1666 }
1637 1667
1638 if (SCHEME_V->gc_verbose) 1668 if (SCHEME_V->gc_verbose)
1669 {
1639 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1670 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1671 }
1640} 1672}
1641 1673
1642static void 1674static void
1643finalize_cell (SCHEME_P_ pointer a) 1675finalize_cell (SCHEME_P_ pointer a)
1644{ 1676{
2231 } 2263 }
2232 } 2264 }
2233} 2265}
2234 2266
2235/* check c is in chars */ 2267/* check c is in chars */
2236static INLINE int 2268ecb_inline int
2237is_one_of (char *s, int c) 2269is_one_of (char *s, int c)
2238{ 2270{
2239 if (c == EOF) 2271 if (c == EOF)
2240 return 1; 2272 return 1;
2241 2273
2242 return !!strchr (s, c); 2274 return !!strchr (s, c);
2243} 2275}
2244 2276
2245/* skip white characters */ 2277/* skip white characters */
2246static INLINE int 2278ecb_inline int
2247skipspace (SCHEME_P) 2279skipspace (SCHEME_P)
2248{ 2280{
2249 int c, curr_line = 0; 2281 int c, curr_line = 0;
2250 2282
2251 do 2283 do
2479 { 2511 {
2480 p = SCHEME_V->strbuff; 2512 p = SCHEME_V->strbuff;
2481 2513
2482 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2514 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2483 { 2515 {
2484 if (num_is_integer (l)) 2516 if (is_integer (l))
2485 xnum (p, ivalue_unchecked (l)); 2517 xnum (p, ivalue_unchecked (l));
2486#if USE_REAL 2518#if USE_REAL
2487 else 2519 else
2488 { 2520 {
2489 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2521 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2744 return 0; 2776 return 0;
2745 } 2777 }
2746 else if (is_number (a)) 2778 else if (is_number (a))
2747 { 2779 {
2748 if (is_number (b)) 2780 if (is_number (b))
2749 if (num_is_integer (a) == num_is_integer (b))
2750 return num_cmp (nvalue (a), nvalue (b)) == 0; 2781 return num_cmp (nvalue (a), nvalue (b)) == 0;
2751 2782
2752 return 0; 2783 return 0;
2753 } 2784 }
2754 else if (is_character (a)) 2785 else if (is_character (a))
2755 { 2786 {
2781/* () is #t in R5RS */ 2812/* () is #t in R5RS */
2782#define is_true(p) ((p) != S_F) 2813#define is_true(p) ((p) != S_F)
2783#define is_false(p) ((p) == S_F) 2814#define is_false(p) ((p) == S_F)
2784 2815
2785/* ========== Environment implementation ========== */ 2816/* ========== Environment implementation ========== */
2786
2787#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST)
2788
2789static int
2790hash_fn (const char *key, int table_size)
2791{
2792 const unsigned char *p = key;
2793 uint32_t hash = 2166136261;
2794
2795 while (*p)
2796 hash = (hash ^ *p++) * 16777619;
2797
2798 return hash % table_size;
2799}
2800#endif
2801 2817
2802#ifndef USE_ALIST_ENV 2818#ifndef USE_ALIST_ENV
2803 2819
2804/* 2820/*
2805 * In this implementation, each frame of the environment may be 2821 * In this implementation, each frame of the environment may be
2822 2838
2823 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2839 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2824 setenvironment (SCHEME_V->envir); 2840 setenvironment (SCHEME_V->envir);
2825} 2841}
2826 2842
2827static INLINE void 2843static uint32_t
2844sym_hash (pointer sym, uint32_t size)
2845{
2846 uintptr_t ptr = (uintptr_t)sym;
2847
2848#if 0
2849 /* table size is prime, so why mix */
2850 ptr += ptr >> 32;
2851 ptr += ptr >> 16;
2852 ptr += ptr >> 8;
2853#endif
2854
2855 return ptr % size;
2856}
2857
2858ecb_inline void
2828new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2859new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2829{ 2860{
2830 pointer slot = immutable_cons (variable, value); 2861 pointer slot = immutable_cons (variable, value);
2831 2862
2832 if (is_vector (car (env))) 2863 if (is_vector (car (env)))
2833 { 2864 {
2834 int location = hash_fn (symname (variable), veclength (car (env))); 2865 int location = sym_hash (variable, veclength (car (env)));
2835
2836 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2866 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2837 } 2867 }
2838 else 2868 else
2839 set_car (env, immutable_cons (slot, car (env))); 2869 set_car (env, immutable_cons (slot, car (env)));
2840} 2870}
2841 2871
2842static pointer 2872static pointer
2843find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2873find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2844{ 2874{
2845 pointer x, y; 2875 pointer x, y;
2846 int location;
2847 2876
2848 for (x = env; x != NIL; x = cdr (x)) 2877 for (x = env; x != NIL; x = cdr (x))
2849 { 2878 {
2850 if (is_vector (car (x))) 2879 if (is_vector (car (x)))
2851 { 2880 {
2852 location = hash_fn (symname (hdl), veclength (car (x))); 2881 int location = sym_hash (hdl, veclength (car (x)));
2853 y = vector_elem (car (x), location); 2882 y = vector_get (car (x), location);
2854 } 2883 }
2855 else 2884 else
2856 y = car (x); 2885 y = car (x);
2857 2886
2858 for (; y != NIL; y = cdr (y)) 2887 for (; y != NIL; y = cdr (y))
2859 if (caar (y) == hdl) 2888 if (caar (y) == hdl)
2860 break; 2889 break;
2861 2890
2862 if (y != NIL) 2891 if (y != NIL)
2892 return car (y);
2893
2894 if (!all)
2863 break; 2895 break;
2864
2865 if (!all)
2866 return NIL;
2867 } 2896 }
2868
2869 if (x != NIL)
2870 return car (y);
2871 2897
2872 return NIL; 2898 return NIL;
2873} 2899}
2874 2900
2875#else /* USE_ALIST_ENV */ 2901#else /* USE_ALIST_ENV */
2876 2902
2877static INLINE void 2903ecb_inline void
2878new_frame_in_env (SCHEME_P_ pointer old_env) 2904new_frame_in_env (SCHEME_P_ pointer old_env)
2879{ 2905{
2880 SCHEME_V->envir = immutable_cons (NIL, old_env); 2906 SCHEME_V->envir = immutable_cons (NIL, old_env);
2881 setenvironment (SCHEME_V->envir); 2907 setenvironment (SCHEME_V->envir);
2882} 2908}
2883 2909
2884static INLINE void 2910ecb_inline void
2885new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2911new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2886{ 2912{
2887 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2913 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2888} 2914}
2889 2915
2897 for (y = car (x); y != NIL; y = cdr (y)) 2923 for (y = car (x); y != NIL; y = cdr (y))
2898 if (caar (y) == hdl) 2924 if (caar (y) == hdl)
2899 break; 2925 break;
2900 2926
2901 if (y != NIL) 2927 if (y != NIL)
2928 return car (y);
2902 break; 2929 break;
2903 2930
2904 if (!all) 2931 if (!all)
2905 return NIL; 2932 break;
2906 } 2933 }
2907
2908 if (x != NIL)
2909 return car (y);
2910 2934
2911 return NIL; 2935 return NIL;
2912} 2936}
2913 2937
2914#endif /* USE_ALIST_ENV else */ 2938#endif /* USE_ALIST_ENV else */
2915 2939
2916static INLINE void 2940ecb_inline void
2917new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2941new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2918{ 2942{
2919 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2943 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2920} 2944}
2921 2945
2922static INLINE void 2946ecb_inline void
2923set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2947set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2924{ 2948{
2925 set_cdr (slot, value); 2949 set_cdr (slot, value);
2926} 2950}
2927 2951
2928static INLINE pointer 2952ecb_inline pointer
2929slot_value_in_env (pointer slot) 2953slot_value_in_env (pointer slot)
2930{ 2954{
2931 return cdr (slot); 2955 return cdr (slot);
2932} 2956}
2933 2957
2934/* ========== Evaluation Cycle ========== */ 2958/* ========== Evaluation Cycle ========== */
2935 2959
2936static pointer 2960static int
2937xError_1 (SCHEME_P_ const char *s, pointer a) 2961xError_1 (SCHEME_P_ const char *s, pointer a)
2938{ 2962{
2939#if USE_ERROR_HOOK 2963#if USE_ERROR_HOOK
2940 pointer x; 2964 pointer x;
2941 pointer hdl = SCHEME_V->ERROR_HOOK; 2965 pointer hdl = SCHEME_V->ERROR_HOOK;
2976 code = cons (mk_string (SCHEME_A_ s), code); 3000 code = cons (mk_string (SCHEME_A_ s), code);
2977 setimmutable (car (code)); 3001 setimmutable (car (code));
2978 SCHEME_V->code = cons (slot_value_in_env (x), code); 3002 SCHEME_V->code = cons (slot_value_in_env (x), code);
2979 SCHEME_V->op = OP_EVAL; 3003 SCHEME_V->op = OP_EVAL;
2980 3004
2981 return S_T; 3005 return 0;
2982 } 3006 }
2983#endif 3007#endif
2984 3008
2985 if (a) 3009 if (a)
2986 SCHEME_V->args = cons (a, NIL); 3010 SCHEME_V->args = cons (a, NIL);
2988 SCHEME_V->args = NIL; 3012 SCHEME_V->args = NIL;
2989 3013
2990 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); 3014 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args);
2991 setimmutable (car (SCHEME_V->args)); 3015 setimmutable (car (SCHEME_V->args));
2992 SCHEME_V->op = OP_ERR0; 3016 SCHEME_V->op = OP_ERR0;
3017
2993 return S_T; 3018 return 0;
2994} 3019}
2995 3020
2996#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) 3021#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a)
2997#define Error_0(s) Error_1 (s, 0) 3022#define Error_0(s) Error_1 (s, 0)
2998 3023
2999/* Too small to turn into function */ 3024/* Too small to turn into function */
3000#define BEGIN do { 3025#define BEGIN do {
3001#define END } while (0) 3026#define END } while (0)
3002#define s_goto(a) BEGIN \ 3027#define s_goto(a) BEGIN \
3003 SCHEME_V->op = a; \ 3028 SCHEME_V->op = a; \
3004 return S_T; END 3029 return 0; END
3005 3030
3006#define s_return(a) return xs_return (SCHEME_A_ a) 3031#define s_return(a) return xs_return (SCHEME_A_ a)
3007 3032
3008#ifndef USE_SCHEME_STACK 3033#ifndef USE_SCHEME_STACK
3009 3034
3039 next_frame->code = code; 3064 next_frame->code = code;
3040 3065
3041 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3066 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3042} 3067}
3043 3068
3044static pointer 3069static int
3045xs_return (SCHEME_P_ pointer a) 3070xs_return (SCHEME_P_ pointer a)
3046{ 3071{
3047 int nframes = (uintptr_t)SCHEME_V->dump; 3072 int nframes = (uintptr_t)SCHEME_V->dump;
3048 struct dump_stack_frame *frame; 3073 struct dump_stack_frame *frame;
3049 3074
3050 SCHEME_V->value = a; 3075 SCHEME_V->value = a;
3051 3076
3052 if (nframes <= 0) 3077 if (nframes <= 0)
3053 return NIL; 3078 return -1;
3054 3079
3055 frame = &SCHEME_V->dump_base[--nframes]; 3080 frame = &SCHEME_V->dump_base[--nframes];
3056 SCHEME_V->op = frame->op; 3081 SCHEME_V->op = frame->op;
3057 SCHEME_V->args = frame->args; 3082 SCHEME_V->args = frame->args;
3058 SCHEME_V->envir = frame->envir; 3083 SCHEME_V->envir = frame->envir;
3059 SCHEME_V->code = frame->code; 3084 SCHEME_V->code = frame->code;
3060 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3085 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3061 3086
3062 return S_T; 3087 return 0;
3063} 3088}
3064 3089
3065static INLINE void 3090ecb_inline void
3066dump_stack_reset (SCHEME_P) 3091dump_stack_reset (SCHEME_P)
3067{ 3092{
3068 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3093 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3069 SCHEME_V->dump = (pointer)+0; 3094 SCHEME_V->dump = (pointer)+0;
3070} 3095}
3071 3096
3072static INLINE void 3097ecb_inline void
3073dump_stack_initialize (SCHEME_P) 3098dump_stack_initialize (SCHEME_P)
3074{ 3099{
3075 SCHEME_V->dump_size = 0; 3100 SCHEME_V->dump_size = 0;
3076 SCHEME_V->dump_base = 0; 3101 SCHEME_V->dump_base = 0;
3077 dump_stack_reset (SCHEME_A); 3102 dump_stack_reset (SCHEME_A);
3130 int i = 0; 3155 int i = 0;
3131 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3156 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3132 3157
3133 while (cont != NIL) 3158 while (cont != NIL)
3134 { 3159 {
3135 frame->op = ivalue (car (cont)); cont = cdr (cont); 3160 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3136 frame->args = car (cont) ; cont = cdr (cont); 3161 frame->args = car (cont) ; cont = cdr (cont);
3137 frame->envir = car (cont) ; cont = cdr (cont); 3162 frame->envir = car (cont) ; cont = cdr (cont);
3138 frame->code = car (cont) ; cont = cdr (cont); 3163 frame->code = car (cont) ; cont = cdr (cont);
3139 3164
3140 ++frame; 3165 ++frame;
3141 ++i; 3166 ++i;
3142 } 3167 }
3143 3168
3144 SCHEME_V->dump = (pointer)(uintptr_t)i; 3169 SCHEME_V->dump = (pointer)(uintptr_t)i;
3145} 3170}
3146 3171
3147#else 3172#else
3148 3173
3149static INLINE void 3174ecb_inline void
3150dump_stack_reset (SCHEME_P) 3175dump_stack_reset (SCHEME_P)
3151{ 3176{
3152 SCHEME_V->dump = NIL; 3177 SCHEME_V->dump = NIL;
3153} 3178}
3154 3179
3155static INLINE void 3180ecb_inline void
3156dump_stack_initialize (SCHEME_P) 3181dump_stack_initialize (SCHEME_P)
3157{ 3182{
3158 dump_stack_reset (SCHEME_A); 3183 dump_stack_reset (SCHEME_A);
3159} 3184}
3160 3185
3162dump_stack_free (SCHEME_P) 3187dump_stack_free (SCHEME_P)
3163{ 3188{
3164 SCHEME_V->dump = NIL; 3189 SCHEME_V->dump = NIL;
3165} 3190}
3166 3191
3167static pointer 3192static int
3168xs_return (SCHEME_P_ pointer a) 3193xs_return (SCHEME_P_ pointer a)
3169{ 3194{
3170 pointer dump = SCHEME_V->dump; 3195 pointer dump = SCHEME_V->dump;
3171 3196
3172 SCHEME_V->value = a; 3197 SCHEME_V->value = a;
3173 3198
3174 if (dump == NIL) 3199 if (dump == NIL)
3175 return NIL; 3200 return -1;
3176 3201
3177 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3202 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3178 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3203 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3179 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3204 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3180 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3205 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3181 3206
3182 SCHEME_V->dump = dump; 3207 SCHEME_V->dump = dump;
3183 3208
3184 return S_T; 3209 return 0;
3185} 3210}
3186 3211
3187static void 3212static void
3188s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3213s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3189{ 3214{
3214 3239
3215#endif 3240#endif
3216 3241
3217#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3242#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3218 3243
3219static pointer 3244static int
3220opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3245opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3221{ 3246{
3222 pointer args = SCHEME_V->args; 3247 pointer args = SCHEME_V->args;
3223 pointer x, y; 3248 pointer x, y;
3224 3249
3391 3416
3392 case OP_TRACING: 3417 case OP_TRACING:
3393 { 3418 {
3394 int tr = SCHEME_V->tracing; 3419 int tr = SCHEME_V->tracing;
3395 3420
3396 SCHEME_V->tracing = ivalue (car (args)); 3421 SCHEME_V->tracing = ivalue_unchecked (car (args));
3397 s_return (mk_integer (SCHEME_A_ tr)); 3422 s_return (mk_integer (SCHEME_A_ tr));
3398 } 3423 }
3399 3424
3400#endif 3425#endif
3401 3426
3413 /* fall through */ 3438 /* fall through */
3414 3439
3415 case OP_REAL_APPLY: 3440 case OP_REAL_APPLY:
3416#endif 3441#endif
3417 if (is_proc (SCHEME_V->code)) 3442 if (is_proc (SCHEME_V->code))
3418 {
3419 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3443 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3420 }
3421 else if (is_foreign (SCHEME_V->code)) 3444 else if (is_foreign (SCHEME_V->code))
3422 { 3445 {
3423 /* Keep nested calls from GC'ing the arglist */ 3446 /* Keep nested calls from GC'ing the arglist */
3424 push_recent_alloc (SCHEME_A_ args, NIL); 3447 push_recent_alloc (SCHEME_A_ args, NIL);
3425 x = SCHEME_V->code->object.ff (SCHEME_A_ args); 3448 x = SCHEME_V->code->object.ff (SCHEME_A_ args);
3592 3615
3593 case OP_IF1: /* if */ 3616 case OP_IF1: /* if */
3594 if (is_true (SCHEME_V->value)) 3617 if (is_true (SCHEME_V->value))
3595 SCHEME_V->code = car (SCHEME_V->code); 3618 SCHEME_V->code = car (SCHEME_V->code);
3596 else 3619 else
3597 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because 3620 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
3598
3599 * car(NIL) = NIL */
3600 s_goto (OP_EVAL); 3621 s_goto (OP_EVAL);
3601 3622
3602 case OP_LET0: /* let */ 3623 case OP_LET0: /* let */
3603 SCHEME_V->args = NIL; 3624 SCHEME_V->args = NIL;
3604 SCHEME_V->value = SCHEME_V->code; 3625 SCHEME_V->value = SCHEME_V->code;
3914 SCHEME_V->code = car (args); 3935 SCHEME_V->code = car (args);
3915 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); 3936 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
3916 s_goto (OP_APPLY); 3937 s_goto (OP_APPLY);
3917 } 3938 }
3918 3939
3919 abort (); 3940 if (USE_ERROR_CHECKING) abort ();
3920} 3941}
3921 3942
3922static pointer 3943static int
3923opexe_2 (SCHEME_P_ enum scheme_opcodes op) 3944opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3924{ 3945{
3925 pointer args = SCHEME_V->args; 3946 pointer args = SCHEME_V->args;
3926 pointer x = car (args); 3947 pointer x = car (args);
3927 num v; 3948 num v;
3928 3949
3929#if USE_MATH
3930 RVALUE dd;
3931#endif
3932
3933 switch (op) 3950 switch (op)
3934 { 3951 {
3935#if USE_MATH 3952#if USE_MATH
3936 case OP_INEX2EX: /* inexact->exact */ 3953 case OP_INEX2EX: /* inexact->exact */
3954 {
3937 if (num_is_integer (x)) 3955 if (is_integer (x))
3938 s_return (x); 3956 s_return (x);
3939 else if (modf (rvalue_unchecked (x), &dd) == 0) 3957
3958 RVALUE r = rvalue_unchecked (x);
3959
3960 if (r == (RVALUE)(IVALUE)r)
3940 s_return (mk_integer (SCHEME_A_ ivalue (x))); 3961 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3941 else 3962 else
3942 Error_1 ("inexact->exact: not integral:", x); 3963 Error_1 ("inexact->exact: not integral:", x);
3964 }
3943 3965
3944 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 3966 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)))); 3967 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)))); 3968 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)))); 3969 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3965 { 3987 {
3966 RVALUE result; 3988 RVALUE result;
3967 int real_result = 1; 3989 int real_result = 1;
3968 pointer y = cadr (args); 3990 pointer y = cadr (args);
3969 3991
3970 if (num_is_integer (x) && num_is_integer (y)) 3992 if (is_integer (x) && is_integer (y))
3971 real_result = 0; 3993 real_result = 0;
3972 3994
3973 /* This 'if' is an R5RS compatibility fix. */ 3995 /* This 'if' is an R5RS compatibility fix. */
3974 /* NOTE: Remove this 'if' fix for R6RS. */ 3996 /* NOTE: Remove this 'if' fix for R6RS. */
3975 if (rvalue (x) == 0 && rvalue (y) < 0) 3997 if (rvalue (x) == 0 && rvalue (y) < 0)
3981 /* If the test fails, result is too big for integer. */ 4003 /* If the test fails, result is too big for integer. */
3982 if (!real_result) 4004 if (!real_result)
3983 { 4005 {
3984 long result_as_long = result; 4006 long result_as_long = result;
3985 4007
3986 if (result != (RVALUE) result_as_long) 4008 if (result != result_as_long)
3987 real_result = 1; 4009 real_result = 1;
3988 } 4010 }
3989 4011
3990 if (real_result) 4012 if (real_result)
3991 s_return (mk_real (SCHEME_A_ result)); 4013 s_return (mk_real (SCHEME_A_ result));
3996 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4018 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)))); 4019 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
3998 4020
3999 case OP_TRUNCATE: 4021 case OP_TRUNCATE:
4000 { 4022 {
4001 RVALUE rvalue_of_x; 4023 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))); 4024 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 } 4025 }
4010 4026
4011 case OP_ROUND: 4027 case OP_ROUND:
4012 if (num_is_integer (x)) 4028 if (is_integer (x))
4013 s_return (x); 4029 s_return (x);
4014 4030
4015 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4031 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4016#endif 4032#endif
4017 4033
4018 case OP_ADD: /* + */ 4034 case OP_ADD: /* + */
4019 v = num_zero; 4035 v = num_zero;
4020 4036
4021 for (x = args; x != NIL; x = cdr (x)) 4037 for (x = args; x != NIL; x = cdr (x))
4022 v = num_op ('+', v, nvalue (car (x))); 4038 v = num_op (NUM_ADD, v, nvalue (car (x)));
4023 4039
4024 s_return (mk_number (SCHEME_A_ v)); 4040 s_return (mk_number (SCHEME_A_ v));
4025 4041
4026 case OP_MUL: /* * */ 4042 case OP_MUL: /* * */
4027 v = num_one; 4043 v = num_one;
4028 4044
4029 for (x = args; x != NIL; x = cdr (x)) 4045 for (x = args; x != NIL; x = cdr (x))
4030 v = num_op ('+', v, nvalue (car (x))); 4046 v = num_op (NUM_MUL, v, nvalue (car (x)));
4031 4047
4032 s_return (mk_number (SCHEME_A_ v)); 4048 s_return (mk_number (SCHEME_A_ v));
4033 4049
4034 case OP_SUB: /* - */ 4050 case OP_SUB: /* - */
4035 if (cdr (args) == NIL) 4051 if (cdr (args) == NIL)
4042 x = cdr (args); 4058 x = cdr (args);
4043 v = nvalue (car (args)); 4059 v = nvalue (car (args));
4044 } 4060 }
4045 4061
4046 for (; x != NIL; x = cdr (x)) 4062 for (; x != NIL; x = cdr (x))
4047 v = num_op ('+', v, nvalue (car (x))); 4063 v = num_op (NUM_SUB, v, nvalue (car (x)));
4048 4064
4049 s_return (mk_number (SCHEME_A_ v)); 4065 s_return (mk_number (SCHEME_A_ v));
4050 4066
4051 case OP_DIV: /* / */ 4067 case OP_DIV: /* / */
4052 if (cdr (args) == NIL) 4068 if (cdr (args) == NIL)
4059 x = cdr (args); 4075 x = cdr (args);
4060 v = nvalue (car (args)); 4076 v = nvalue (car (args));
4061 } 4077 }
4062 4078
4063 for (; x != NIL; x = cdr (x)) 4079 for (; x != NIL; x = cdr (x))
4064 {
4065 if (!is_zero_rvalue (rvalue (car (x)))) 4080 if (!is_zero_rvalue (rvalue (car (x))))
4066 v = num_div (v, nvalue (car (x))); 4081 v = num_div (v, nvalue (car (x)));
4067 else 4082 else
4068 Error_0 ("/: division by zero"); 4083 Error_0 ("/: division by zero");
4069 }
4070 4084
4071 s_return (mk_number (SCHEME_A_ v)); 4085 s_return (mk_number (SCHEME_A_ v));
4072 4086
4073 case OP_INTDIV: /* quotient */ 4087 case OP_INTDIV: /* quotient */
4074 if (cdr (args) == NIL) 4088 if (cdr (args) == NIL)
4083 } 4097 }
4084 4098
4085 for (; x != NIL; x = cdr (x)) 4099 for (; x != NIL; x = cdr (x))
4086 { 4100 {
4087 if (ivalue (car (x)) != 0) 4101 if (ivalue (car (x)) != 0)
4088 v = num_op ('/', v, nvalue (car (x))); 4102 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4089 else 4103 else
4090 Error_0 ("quotient: division by zero"); 4104 Error_0 ("quotient: division by zero");
4091 } 4105 }
4092 4106
4093 s_return (mk_number (SCHEME_A_ v)); 4107 s_return (mk_number (SCHEME_A_ v));
4139 } 4153 }
4140 else 4154 else
4141 Error_0 ("set-cdr!: unable to alter immutable pair"); 4155 Error_0 ("set-cdr!: unable to alter immutable pair");
4142 4156
4143 case OP_CHAR2INT: /* char->integer */ 4157 case OP_CHAR2INT: /* char->integer */
4144 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4158 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4145 4159
4146 case OP_INT2CHAR: /* integer->char */ 4160 case OP_INT2CHAR: /* integer->char */
4147 s_return (mk_character (SCHEME_A_ ivalue (x))); 4161 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4148 4162
4149 case OP_CHARUPCASE: 4163 case OP_CHARUPCASE:
4150 { 4164 {
4151 unsigned char c = ivalue (x); 4165 unsigned char c = ivalue_unchecked (x);
4152 c = toupper (c); 4166 c = toupper (c);
4153 s_return (mk_character (SCHEME_A_ c)); 4167 s_return (mk_character (SCHEME_A_ c));
4154 } 4168 }
4155 4169
4156 case OP_CHARDNCASE: 4170 case OP_CHARDNCASE:
4157 { 4171 {
4158 unsigned char c = ivalue (x); 4172 unsigned char c = ivalue_unchecked (x);
4159 c = tolower (c); 4173 c = tolower (c);
4160 s_return (mk_character (SCHEME_A_ c)); 4174 s_return (mk_character (SCHEME_A_ c));
4161 } 4175 }
4162 4176
4163 case OP_STR2SYM: /* string->symbol */ 4177 case OP_STR2SYM: /* string->symbol */
4240 Error_1 ("atom->string: not an atom:", x); 4254 Error_1 ("atom->string: not an atom:", x);
4241 } 4255 }
4242 4256
4243 case OP_MKSTRING: /* make-string */ 4257 case OP_MKSTRING: /* make-string */
4244 { 4258 {
4245 int fill = ' '; 4259 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4246 int len;
4247
4248 len = ivalue (x); 4260 int len = ivalue_unchecked (x);
4249
4250 if (cdr (args) != NIL)
4251 fill = charvalue (cadr (args));
4252 4261
4253 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4262 s_return (mk_empty_string (SCHEME_A_ len, fill));
4254 } 4263 }
4255 4264
4256 case OP_STRLEN: /* string-length */ 4265 case OP_STRLEN: /* string-length */
4257 s_return (mk_integer (SCHEME_A_ strlength (x))); 4266 s_return (mk_integer (SCHEME_A_ strlength (x)));
4258 4267
4259 case OP_STRREF: /* string-ref */ 4268 case OP_STRREF: /* string-ref */
4260 { 4269 {
4261 char *str;
4262 int index;
4263
4264 str = strvalue (x); 4270 char *str = strvalue (x);
4265
4266 index = ivalue (cadr (args)); 4271 int index = ivalue_unchecked (cadr (args));
4267 4272
4268 if (index >= strlength (x)) 4273 if (index >= strlength (x))
4269 Error_1 ("string-ref: out of bounds:", cadr (args)); 4274 Error_1 ("string-ref: out of bounds:", cadr (args));
4270 4275
4271 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4276 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4272 } 4277 }
4273 4278
4274 case OP_STRSET: /* string-set! */ 4279 case OP_STRSET: /* string-set! */
4275 { 4280 {
4276 char *str; 4281 char *str = strvalue (x);
4277 int index; 4282 int index = ivalue_unchecked (cadr (args));
4278 int c; 4283 int c;
4279 4284
4280 if (is_immutable (x)) 4285 if (is_immutable (x))
4281 Error_1 ("string-set!: unable to alter immutable string:", x); 4286 Error_1 ("string-set!: unable to alter immutable string:", x);
4282
4283 str = strvalue (x);
4284
4285 index = ivalue (cadr (args));
4286 4287
4287 if (index >= strlength (x)) 4288 if (index >= strlength (x))
4288 Error_1 ("string-set!: out of bounds:", cadr (args)); 4289 Error_1 ("string-set!: out of bounds:", cadr (args));
4289 4290
4290 c = charvalue (caddr (args)); 4291 c = charvalue (caddr (args));
4313 s_return (newstr); 4314 s_return (newstr);
4314 } 4315 }
4315 4316
4316 case OP_SUBSTR: /* substring */ 4317 case OP_SUBSTR: /* substring */
4317 { 4318 {
4318 char *str; 4319 char *str = strvalue (x);
4319 int index0; 4320 int index0 = ivalue_unchecked (cadr (args));
4320 int index1; 4321 int index1;
4321 int len; 4322 int len;
4322 4323
4323 str = strvalue (x);
4324
4325 index0 = ivalue (cadr (args));
4326
4327 if (index0 > strlength (x)) 4324 if (index0 > strlength (x))
4328 Error_1 ("substring: start out of bounds:", cadr (args)); 4325 Error_1 ("substring: start out of bounds:", cadr (args));
4329 4326
4330 if (cddr (args) != NIL) 4327 if (cddr (args) != NIL)
4331 { 4328 {
4332 index1 = ivalue (caddr (args)); 4329 index1 = ivalue_unchecked (caddr (args));
4333 4330
4334 if (index1 > strlength (x) || index1 < index0) 4331 if (index1 > strlength (x) || index1 < index0)
4335 Error_1 ("substring: end out of bounds:", caddr (args)); 4332 Error_1 ("substring: end out of bounds:", caddr (args));
4336 } 4333 }
4337 else 4334 else
4360 if (SCHEME_V->no_memory) 4357 if (SCHEME_V->no_memory)
4361 s_return (S_SINK); 4358 s_return (S_SINK);
4362#endif 4359#endif
4363 4360
4364 for (x = args, i = 0; is_pair (x); x = cdr (x), i++) 4361 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4365 set_vector_elem (vec, i, car (x)); 4362 vector_set (vec, i, car (x));
4366 4363
4367 s_return (vec); 4364 s_return (vec);
4368 } 4365 }
4369 4366
4370 case OP_MKVECTOR: /* make-vector */ 4367 case OP_MKVECTOR: /* make-vector */
4371 { 4368 {
4372 pointer fill = NIL; 4369 pointer fill = NIL;
4373 int len;
4374 pointer vec; 4370 pointer vec;
4375
4376 len = ivalue (x); 4371 int len = ivalue_unchecked (x);
4377 4372
4378 if (cdr (args) != NIL) 4373 if (cdr (args) != NIL)
4379 fill = cadr (args); 4374 fill = cadr (args);
4380 4375
4381 vec = mk_vector (SCHEME_A_ len); 4376 vec = mk_vector (SCHEME_A_ len);
4384 if (SCHEME_V->no_memory) 4379 if (SCHEME_V->no_memory)
4385 s_return (S_SINK); 4380 s_return (S_SINK);
4386#endif 4381#endif
4387 4382
4388 if (fill != NIL) 4383 if (fill != NIL)
4389 fill_vector (vec, fill); 4384 fill_vector (vec, 0, fill);
4390 4385
4391 s_return (vec); 4386 s_return (vec);
4392 } 4387 }
4393 4388
4394 case OP_VECLEN: /* vector-length */ 4389 case OP_VECLEN: /* vector-length */
4395 s_return (mk_integer (SCHEME_A_ veclength (x))); 4390 s_return (mk_integer (SCHEME_A_ veclength (x)));
4396 4391
4397 case OP_VECREF: /* vector-ref */ 4392 case OP_VECREF: /* vector-ref */
4398 { 4393 {
4399 int index;
4400
4401 index = ivalue (cadr (args)); 4394 int index = ivalue_unchecked (cadr (args));
4402 4395
4403 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4396 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4404 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4397 Error_1 ("vector-ref: out of bounds:", cadr (args));
4405 4398
4406 s_return (vector_elem (x, index)); 4399 s_return (vector_get (x, index));
4407 } 4400 }
4408 4401
4409 case OP_VECSET: /* vector-set! */ 4402 case OP_VECSET: /* vector-set! */
4410 { 4403 {
4411 int index; 4404 int index = ivalue_unchecked (cadr (args));
4412 4405
4413 if (is_immutable (x)) 4406 if (is_immutable (x))
4414 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4407 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4415 4408
4416 index = ivalue (cadr (args));
4417
4418 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4409 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4419 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4410 Error_1 ("vector-set!: out of bounds:", cadr (args));
4420 4411
4421 set_vector_elem (x, index, caddr (args)); 4412 vector_set (x, index, caddr (args));
4422 s_return (x); 4413 s_return (x);
4423 } 4414 }
4424 } 4415 }
4425 4416
4426 return S_T; 4417 if (USE_ERROR_CHECKING) abort ();
4427} 4418}
4428 4419
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 4420static int
4484opexe_r (SCHEME_P_ enum scheme_opcodes op) 4421opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4485{ 4422{
4486 pointer x = SCHEME_V->args; 4423 pointer x = SCHEME_V->args;
4487 4424
4488 for (;;) 4425 for (;;)
4489 { 4426 {
4509 } 4446 }
4510 4447
4511 s_return (S_T); 4448 s_return (S_T);
4512} 4449}
4513 4450
4514static pointer 4451static int
4515opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4452opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4516{ 4453{
4517 pointer args = SCHEME_V->args; 4454 pointer args = SCHEME_V->args;
4518 pointer a = car (args); 4455 pointer a = car (args);
4519 pointer d = cdr (args); 4456 pointer d = cdr (args);
4531 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4468 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4532 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4469 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4533 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4470 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4534 4471
4535#if USE_CHAR_CLASSIFIERS 4472#if USE_CHAR_CLASSIFIERS
4536 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4473 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4537 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4474 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4538 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4475 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4539 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4476 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4540 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4477 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4541#endif 4478#endif
4542 4479
4543#if USE_PORTS 4480#if USE_PORTS
4544 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4481 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4545 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4482 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4565 } 4502 }
4566 4503
4567 s_retbool (r); 4504 s_retbool (r);
4568} 4505}
4569 4506
4570static pointer 4507static int
4571opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4508opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4572{ 4509{
4573 pointer args = SCHEME_V->args; 4510 pointer args = SCHEME_V->args;
4574 pointer a = car (args); 4511 pointer a = car (args);
4575 pointer x, y; 4512 pointer x, y;
4661 putstr (SCHEME_A_ "\n"); 4598 putstr (SCHEME_A_ "\n");
4662 4599
4663 if (SCHEME_V->interactive_repl) 4600 if (SCHEME_V->interactive_repl)
4664 s_goto (OP_T0LVL); 4601 s_goto (OP_T0LVL);
4665 else 4602 else
4666 return NIL; 4603 return -1;
4667 } 4604 }
4668 4605
4669 case OP_REVERSE: /* reverse */ 4606 case OP_REVERSE: /* reverse */
4670 s_return (reverse (SCHEME_A_ a)); 4607 s_return (reverse (SCHEME_A_ a));
4671 4608
4728 4665
4729 case OP_QUIT: /* quit */ 4666 case OP_QUIT: /* quit */
4730 if (is_pair (args)) 4667 if (is_pair (args))
4731 SCHEME_V->retcode = ivalue (a); 4668 SCHEME_V->retcode = ivalue (a);
4732 4669
4733 return NIL; 4670 return -1;
4734 4671
4735 case OP_GC: /* gc */ 4672 case OP_GC: /* gc */
4736 gc (SCHEME_A_ NIL, NIL); 4673 gc (SCHEME_A_ NIL, NIL);
4737 s_return (S_T); 4674 s_return (S_T);
4738 4675
4746 4683
4747 case OP_NEWSEGMENT: /* new-segment */ 4684 case OP_NEWSEGMENT: /* new-segment */
4748 if (!is_pair (args) || !is_number (a)) 4685 if (!is_pair (args) || !is_number (a))
4749 Error_0 ("new-segment: argument must be a number"); 4686 Error_0 ("new-segment: argument must be a number");
4750 4687
4751 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 4688 alloc_cellseg (SCHEME_A_ ivalue (a));
4752 4689
4753 s_return (S_T); 4690 s_return (S_T);
4754 4691
4755 case OP_OBLIST: /* oblist */ 4692 case OP_OBLIST: /* oblist */
4756 s_return (oblist_all_symbols (SCHEME_A)); 4693 s_return (oblist_all_symbols (SCHEME_A));
4785 break; 4722 break;
4786 } 4723 }
4787 4724
4788 p = port_from_filename (SCHEME_A_ strvalue (a), prop); 4725 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4789 4726
4790 if (p == NIL) 4727 s_return (p == NIL ? S_F : p);
4791 s_return (S_F);
4792
4793 s_return (p);
4794 } 4728 }
4795 4729
4796# if USE_STRING_PORTS 4730# if USE_STRING_PORTS
4797 4731
4798 case OP_OPEN_INSTRING: /* open-input-string */ 4732 case OP_OPEN_INSTRING: /* open-input-string */
4813 } 4747 }
4814 4748
4815 p = port_from_string (SCHEME_A_ strvalue (a), 4749 p = port_from_string (SCHEME_A_ strvalue (a),
4816 strvalue (a) + strlength (a), prop); 4750 strvalue (a) + strlength (a), prop);
4817 4751
4818 if (p == NIL) 4752 s_return (p == NIL ? S_F : p);
4819 s_return (S_F);
4820
4821 s_return (p);
4822 } 4753 }
4823 4754
4824 case OP_OPEN_OUTSTRING: /* open-output-string */ 4755 case OP_OPEN_OUTSTRING: /* open-output-string */
4825 { 4756 {
4826 pointer p; 4757 pointer p;
4827 4758
4828 if (a == NIL) 4759 if (a == NIL)
4829 {
4830 p = port_from_scratch (SCHEME_A); 4760 p = port_from_scratch (SCHEME_A);
4831
4832 if (p == NIL)
4833 s_return (S_F);
4834 }
4835 else 4761 else
4836 {
4837 p = port_from_string (SCHEME_A_ strvalue (a), 4762 p = port_from_string (SCHEME_A_ strvalue (a),
4838 strvalue (a) + strlength (a), port_output); 4763 strvalue (a) + strlength (a), port_output);
4839 4764
4840 if (p == NIL) 4765 s_return (p == NIL ? S_F : p);
4841 s_return (S_F);
4842 }
4843
4844 s_return (p);
4845 } 4766 }
4846 4767
4847 case OP_GET_OUTSTRING: /* get-output-string */ 4768 case OP_GET_OUTSTRING: /* get-output-string */
4848 { 4769 {
4849 port *p; 4770 port *p;
4888 case OP_CURR_ENV: /* current-environment */ 4809 case OP_CURR_ENV: /* current-environment */
4889 s_return (SCHEME_V->envir); 4810 s_return (SCHEME_V->envir);
4890 4811
4891 } 4812 }
4892 4813
4893 abort (); 4814 if (USE_ERROR_CHECKING) abort ();
4894} 4815}
4895 4816
4896static pointer 4817static int
4897opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4818opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4898{ 4819{
4820 pointer args = SCHEME_V->args;
4899 pointer x; 4821 pointer x;
4900 4822
4901 if (SCHEME_V->nesting != 0) 4823 if (SCHEME_V->nesting != 0)
4902 { 4824 {
4903 int n = SCHEME_V->nesting; 4825 int n = SCHEME_V->nesting;
4910 switch (op) 4832 switch (op)
4911 { 4833 {
4912 /* ========== reading part ========== */ 4834 /* ========== reading part ========== */
4913#if USE_PORTS 4835#if USE_PORTS
4914 case OP_READ: 4836 case OP_READ:
4915 if (!is_pair (SCHEME_V->args)) 4837 if (!is_pair (args))
4916 s_goto (OP_READ_INTERNAL); 4838 s_goto (OP_READ_INTERNAL);
4917 4839
4918 if (!is_inport (car (SCHEME_V->args))) 4840 if (!is_inport (car (args)))
4919 Error_1 ("read: not an input port:", car (SCHEME_V->args)); 4841 Error_1 ("read: not an input port:", car (args));
4920 4842
4921 if (car (SCHEME_V->args) == SCHEME_V->inport) 4843 if (car (args) == SCHEME_V->inport)
4922 s_goto (OP_READ_INTERNAL); 4844 s_goto (OP_READ_INTERNAL);
4923 4845
4924 x = SCHEME_V->inport; 4846 x = SCHEME_V->inport;
4925 SCHEME_V->inport = car (SCHEME_V->args); 4847 SCHEME_V->inport = car (args);
4926 x = cons (x, NIL); 4848 x = cons (x, NIL);
4927 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4849 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
4928 s_goto (OP_READ_INTERNAL); 4850 s_goto (OP_READ_INTERNAL);
4929 4851
4930 case OP_READ_CHAR: /* read-char */ 4852 case OP_READ_CHAR: /* read-char */
4931 case OP_PEEK_CHAR: /* peek-char */ 4853 case OP_PEEK_CHAR: /* peek-char */
4932 { 4854 {
4933 int c; 4855 int c;
4934 4856
4935 if (is_pair (SCHEME_V->args)) 4857 if (is_pair (args))
4936 { 4858 {
4937 if (car (SCHEME_V->args) != SCHEME_V->inport) 4859 if (car (args) != SCHEME_V->inport)
4938 { 4860 {
4939 x = SCHEME_V->inport; 4861 x = SCHEME_V->inport;
4940 x = cons (x, NIL); 4862 x = cons (x, NIL);
4941 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4863 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
4942 SCHEME_V->inport = car (SCHEME_V->args); 4864 SCHEME_V->inport = car (args);
4943 } 4865 }
4944 } 4866 }
4945 4867
4946 c = inchar (SCHEME_A); 4868 c = inchar (SCHEME_A);
4947 4869
4957 case OP_CHAR_READY: /* char-ready? */ 4879 case OP_CHAR_READY: /* char-ready? */
4958 { 4880 {
4959 pointer p = SCHEME_V->inport; 4881 pointer p = SCHEME_V->inport;
4960 int res; 4882 int res;
4961 4883
4962 if (is_pair (SCHEME_V->args)) 4884 if (is_pair (args))
4963 p = car (SCHEME_V->args); 4885 p = car (args);
4964 4886
4965 res = p->object.port->kind & port_string; 4887 res = p->object.port->kind & port_string;
4966 4888
4967 s_retbool (res); 4889 s_retbool (res);
4968 } 4890 }
4969 4891
4970 case OP_SET_INPORT: /* set-input-port */ 4892 case OP_SET_INPORT: /* set-input-port */
4971 SCHEME_V->inport = car (SCHEME_V->args); 4893 SCHEME_V->inport = car (args);
4972 s_return (SCHEME_V->value); 4894 s_return (SCHEME_V->value);
4973 4895
4974 case OP_SET_OUTPORT: /* set-output-port */ 4896 case OP_SET_OUTPORT: /* set-output-port */
4975 SCHEME_V->outport = car (SCHEME_V->args); 4897 SCHEME_V->outport = car (args);
4976 s_return (SCHEME_V->value); 4898 s_return (SCHEME_V->value);
4977#endif 4899#endif
4978 4900
4979 case OP_RDSEXPR: 4901 case OP_RDSEXPR:
4980 switch (SCHEME_V->tok) 4902 switch (SCHEME_V->tok)
5066 } 4988 }
5067 4989
5068 break; 4990 break;
5069 4991
5070 case OP_RDLIST: 4992 case OP_RDLIST:
5071 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 4993 SCHEME_V->args = cons (SCHEME_V->value, args);
5072 SCHEME_V->tok = token (SCHEME_A); 4994 SCHEME_V->tok = token (SCHEME_A);
5073 4995
5074 switch (SCHEME_V->tok) 4996 switch (SCHEME_V->tok)
5075 { 4997 {
5076 case TOK_EOF: 4998 case TOK_EOF:
5104 case OP_RDDOT: 5026 case OP_RDDOT:
5105 if (token (SCHEME_A) != TOK_RPAREN) 5027 if (token (SCHEME_A) != TOK_RPAREN)
5106 Error_0 ("syntax error: illegal dot expression"); 5028 Error_0 ("syntax error: illegal dot expression");
5107 5029
5108 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5030 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5109 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); 5031 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, args));
5110 5032
5111 case OP_RDQUOTE: 5033 case OP_RDQUOTE:
5112 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); 5034 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)));
5113 5035
5114 case OP_RDQQUOTE: 5036 case OP_RDQQUOTE:
5136 SCHEME_V->args = SCHEME_V->value; 5058 SCHEME_V->args = SCHEME_V->value;
5137 s_goto (OP_VECTOR); 5059 s_goto (OP_VECTOR);
5138 5060
5139 /* ========== printing part ========== */ 5061 /* ========== printing part ========== */
5140 case OP_P0LIST: 5062 case OP_P0LIST:
5141 if (is_vector (SCHEME_V->args)) 5063 if (is_vector (args))
5142 { 5064 {
5143 putstr (SCHEME_A_ "#("); 5065 putstr (SCHEME_A_ "#(");
5144 SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); 5066 SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0));
5145 s_goto (OP_PVECFROM); 5067 s_goto (OP_PVECFROM);
5146 } 5068 }
5147 else if (is_environment (SCHEME_V->args)) 5069 else if (is_environment (args))
5148 { 5070 {
5149 putstr (SCHEME_A_ "#<ENVIRONMENT>"); 5071 putstr (SCHEME_A_ "#<ENVIRONMENT>");
5150 s_return (S_T); 5072 s_return (S_T);
5151 } 5073 }
5152 else if (!is_pair (SCHEME_V->args)) 5074 else if (!is_pair (args))
5153 { 5075 {
5154 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5076 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5155 s_return (S_T); 5077 s_return (S_T);
5156 } 5078 }
5157 else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5079 else
5158 { 5080 {
5081 pointer a = car (args);
5082 pointer b = cdr (args);
5083 int ok_abbr = ok_abbrev (b);
5084 SCHEME_V->args = car (b);
5085
5086 if (a == SCHEME_V->QUOTE && ok_abbr)
5159 putstr (SCHEME_A_ "'"); 5087 putstr (SCHEME_A_ "'");
5160 SCHEME_V->args = cadr (SCHEME_V->args); 5088 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5089 putstr (SCHEME_A_ "`");
5090 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5091 putstr (SCHEME_A_ ",");
5092 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5093 putstr (SCHEME_A_ ",@");
5094 else
5095 {
5096 putstr (SCHEME_A_ "(");
5097 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5098 SCHEME_V->args = a;
5099 }
5100
5161 s_goto (OP_P0LIST); 5101 s_goto (OP_P0LIST);
5162 } 5102 }
5163 else if (car (SCHEME_V->args) == SCHEME_V->QQUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5103
5104 case OP_P1LIST:
5105 if (is_pair (args))
5164 { 5106 {
5107 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5165 putstr (SCHEME_A_ "`"); 5108 putstr (SCHEME_A_ " ");
5166 SCHEME_V->args = cadr (SCHEME_V->args); 5109 SCHEME_V->args = car (args);
5167 s_goto (OP_P0LIST); 5110 s_goto (OP_P0LIST);
5168 } 5111 }
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)) 5112 else if (is_vector (args))
5198 { 5113 {
5199 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); 5114 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL);
5200 putstr (SCHEME_A_ " . "); 5115 putstr (SCHEME_A_ " . ");
5201 s_goto (OP_P0LIST); 5116 s_goto (OP_P0LIST);
5202 } 5117 }
5203 else 5118 else
5204 { 5119 {
5205 if (SCHEME_V->args != NIL) 5120 if (args != NIL)
5206 { 5121 {
5207 putstr (SCHEME_A_ " . "); 5122 putstr (SCHEME_A_ " . ");
5208 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5123 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5209 } 5124 }
5210 5125
5211 putstr (SCHEME_A_ ")"); 5126 putstr (SCHEME_A_ ")");
5212 s_return (S_T); 5127 s_return (S_T);
5213 } 5128 }
5214 5129
5215 case OP_PVECFROM: 5130 case OP_PVECFROM:
5216 { 5131 {
5217 int i = ivalue_unchecked (cdr (SCHEME_V->args)); 5132 int i = ivalue_unchecked (cdr (args));
5218 pointer vec = car (SCHEME_V->args); 5133 pointer vec = car (args);
5219 int len = veclength (vec); 5134 int len = veclength (vec);
5220 5135
5221 if (i == len) 5136 if (i == len)
5222 { 5137 {
5223 putstr (SCHEME_A_ ")"); 5138 putstr (SCHEME_A_ ")");
5224 s_return (S_T); 5139 s_return (S_T);
5225 } 5140 }
5226 else 5141 else
5227 { 5142 {
5228 pointer elem = vector_elem (vec, i); 5143 pointer elem = vector_get (vec, i);
5229 5144
5230 ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; 5145 ivalue_unchecked (cdr (args)) = i + 1;
5231 s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); 5146 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5232 SCHEME_V->args = elem; 5147 SCHEME_V->args = elem;
5233 5148
5234 if (i > 0) 5149 if (i > 0)
5235 putstr (SCHEME_A_ " "); 5150 putstr (SCHEME_A_ " ");
5236 5151
5237 s_goto (OP_P0LIST); 5152 s_goto (OP_P0LIST);
5238 } 5153 }
5239 } 5154 }
5240 } 5155 }
5241 5156
5242 abort (); 5157 if (USE_ERROR_CHECKING) abort ();
5243} 5158}
5244 5159
5245static pointer 5160static int
5246opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5161opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5247{ 5162{
5163 pointer args = SCHEME_V->args;
5164 pointer a = car (args);
5248 pointer x, y; 5165 pointer x, y;
5249 5166
5250 switch (op) 5167 switch (op)
5251 { 5168 {
5252 case OP_LIST_LENGTH: /* length *//* a.k */ 5169 case OP_LIST_LENGTH: /* length *//* a.k */
5253 { 5170 {
5254 long v = list_length (SCHEME_A_ car (SCHEME_V->args)); 5171 long v = list_length (SCHEME_A_ a);
5255 5172
5256 if (v < 0) 5173 if (v < 0)
5257 Error_1 ("length: not a list:", car (SCHEME_V->args)); 5174 Error_1 ("length: not a list:", a);
5258 5175
5259 s_return (mk_integer (SCHEME_A_ v)); 5176 s_return (mk_integer (SCHEME_A_ v));
5260 } 5177 }
5261 5178
5262 case OP_ASSQ: /* assq *//* a.k */ 5179 case OP_ASSQ: /* assq *//* a.k */
5263 x = car (SCHEME_V->args); 5180 x = a;
5264 5181
5265 for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) 5182 for (y = cadr (args); is_pair (y); y = cdr (y))
5266 { 5183 {
5267 if (!is_pair (car (y))) 5184 if (!is_pair (car (y)))
5268 Error_0 ("unable to handle non pair element"); 5185 Error_0 ("unable to handle non pair element");
5269 5186
5270 if (x == caar (y)) 5187 if (x == caar (y))
5276 else 5193 else
5277 s_return (S_F); 5194 s_return (S_F);
5278 5195
5279 5196
5280 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5197 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5281 SCHEME_V->args = car (SCHEME_V->args); 5198 SCHEME_V->args = a;
5282 5199
5283 if (SCHEME_V->args == NIL) 5200 if (SCHEME_V->args == NIL)
5284 s_return (S_F); 5201 s_return (S_F);
5285 else if (is_closure (SCHEME_V->args)) 5202 else if (is_closure (SCHEME_V->args))
5286 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5203 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5292 case OP_CLOSUREP: /* closure? */ 5209 case OP_CLOSUREP: /* closure? */
5293 /* 5210 /*
5294 * Note, macro object is also a closure. 5211 * Note, macro object is also a closure.
5295 * Therefore, (closure? <#MACRO>) ==> #t 5212 * Therefore, (closure? <#MACRO>) ==> #t
5296 */ 5213 */
5297 s_retbool (is_closure (car (SCHEME_V->args))); 5214 s_retbool (is_closure (a));
5298 5215
5299 case OP_MACROP: /* macro? */ 5216 case OP_MACROP: /* macro? */
5300 s_retbool (is_macro (car (SCHEME_V->args))); 5217 s_retbool (is_macro (a));
5301 } 5218 }
5302 5219
5303 abort (); 5220 if (USE_ERROR_CHECKING) abort ();
5304} 5221}
5305 5222
5223/* 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); 5224typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5307 5225
5308typedef int (*test_predicate) (pointer); 5226typedef int (*test_predicate)(pointer);
5309static int 5227static int
5310is_any (pointer p) 5228tst_any (pointer p)
5311{ 5229{
5312 return 1; 5230 return 1;
5313} 5231}
5314 5232
5315static int 5233static int
5316is_nonneg (pointer p) 5234tst_inonneg (pointer p)
5317{ 5235{
5318 return ivalue (p) >= 0 && is_integer (p); 5236 return is_integer (p) && ivalue_unchecked (p) >= 0;
5237}
5238
5239static int
5240tst_is_list (SCHEME_P_ pointer p)
5241{
5242 return p == NIL || is_pair (p);
5319} 5243}
5320 5244
5321/* Correspond carefully with following defines! */ 5245/* Correspond carefully with following defines! */
5322static struct 5246static struct
5323{ 5247{
5324 test_predicate fct; 5248 test_predicate fct;
5325 const char *kind; 5249 const char *kind;
5326} tests[] = 5250} tests[] = {
5327{ 5251 { tst_any , 0 },
5328 { 0, 0}, /* unused */ 5252 { is_string , "string" },
5329 { is_any, 0}, 5253 { is_symbol , "symbol" },
5330 { is_string, "string" }, 5254 { is_port , "port" },
5331 { is_symbol, "symbol" },
5332 { is_port, "port" },
5333 { is_inport, "input port" }, 5255 { is_inport , "input port" },
5334 { is_outport, "output port" }, 5256 { is_outport , "output port" },
5335 { is_environment, "environment" }, 5257 { is_environment, "environment" },
5336 { is_pair, "pair" }, 5258 { is_pair , "pair" },
5337 { 0, "pair or '()" }, 5259 { 0 , "pair or '()" },
5338 { is_character, "character" }, 5260 { is_character , "character" },
5339 { is_vector, "vector" }, 5261 { is_vector , "vector" },
5340 { is_number, "number" }, 5262 { is_number , "number" },
5341 { is_integer, "integer" }, 5263 { is_integer , "integer" },
5342 { is_nonneg, "non-negative integer" } 5264 { tst_inonneg , "non-negative integer" }
5343}; 5265};
5344 5266
5345#define TST_NONE 0 5267#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5346#define TST_ANY "\001" 5268#define TST_ANY "\001"
5347#define TST_STRING "\002" 5269#define TST_STRING "\002"
5348#define TST_SYMBOL "\003" 5270#define TST_SYMBOL "\003"
5349#define TST_PORT "\004" 5271#define TST_PORT "\004"
5350#define TST_INPORT "\005" 5272#define TST_INPORT "\005"
5351#define TST_OUTPORT "\006" 5273#define TST_OUTPORT "\006"
5352#define TST_ENVIRONMENT "\007" 5274#define TST_ENVIRONMENT "\007"
5353#define TST_PAIR "\010" 5275#define TST_PAIR "\010"
5354#define TST_LIST "\011" 5276#define TST_LIST "\011"
5355#define TST_CHAR "\012" 5277#define TST_CHAR "\012"
5356#define TST_VECTOR "\013" 5278#define TST_VECTOR "\013"
5357#define TST_NUMBER "\014" 5279#define TST_NUMBER "\014"
5358#define TST_INTEGER "\015" 5280#define TST_INTEGER "\015"
5359#define TST_NATURAL "\016" 5281#define TST_NATURAL "\016"
5282
5283#define INF_ARG 0xff
5284#define UNNAMED_OP ""
5285
5286static const char opnames[] =
5287#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5288#include "opdefines.h"
5289#undef OP_DEF
5290;
5291
5292static const char *
5293opname (int idx)
5294{
5295 const char *name = opnames;
5296
5297 /* should do this at compile time, but would require external program, right? */
5298 while (idx--)
5299 name += strlen (name) + 1;
5300
5301 return *name ? name : "ILLEGAL";
5302}
5303
5304static const char *
5305procname (pointer x)
5306{
5307 return opname (procnum (x));
5308}
5360 5309
5361typedef struct 5310typedef struct
5362{ 5311{
5363 dispatch_func func; 5312 uint8_t func;
5364 char *name; 5313 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5314 uint8_t builtin;
5315#if USE_ERROR_CHECKING
5365 int min_arity; 5316 uint8_t min_arity;
5366 int max_arity; 5317 uint8_t max_arity;
5367 char *arg_tests_encoding; 5318 char arg_tests_encoding[3];
5319#endif
5368} op_code_info; 5320} op_code_info;
5369 5321
5370#define INF_ARG 0xffff
5371
5372static op_code_info dispatch_table[] = { 5322static const op_code_info dispatch_table[] = {
5373#define OP_DEF(A,B,C,D,E,OP) {A,B,C,D,E}, 5323#if USE_ERROR_CHECKING
5324#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5325#else
5326#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5327#endif
5374#include "opdefines.h" 5328#include "opdefines.h"
5329#undef OP_DEF
5375 {0} 5330 {0}
5376}; 5331};
5377 5332
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 */ 5333/* kernel of this interpreter */
5391static void 5334static void ecb_hot
5392Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5335Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5393{ 5336{
5394 SCHEME_V->op = op; 5337 SCHEME_V->op = op;
5395 5338
5396 for (;;) 5339 for (;;)
5397 { 5340 {
5398 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5341 const op_code_info *pcd = dispatch_table + SCHEME_V->op;
5399 5342
5400#if USE_ERROR_CHECKING 5343#if USE_ERROR_CHECKING
5401 if (pcd->name) /* if built-in function, check arguments */ 5344 if (pcd->builtin) /* if built-in function, check arguments */
5402 { 5345 {
5403 int ok = 1;
5404 char msg[STRBUFFSIZE]; 5346 char msg[STRBUFFSIZE];
5405 int n = list_length (SCHEME_A_ SCHEME_V->args); 5347 int n = list_length (SCHEME_A_ SCHEME_V->args);
5406 5348
5407 /* Check number of arguments */ 5349 /* Check number of arguments */
5408 if (ecb_expect_false (n < pcd->min_arity)) 5350 if (ecb_expect_false (n < pcd->min_arity))
5409 { 5351 {
5410 ok = 0;
5411 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5352 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5412 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); 5353 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity);
5354 xError_1 (SCHEME_A_ msg, 0);
5355 continue;
5413 } 5356 }
5414 else if (ecb_expect_false (n > pcd->max_arity)) 5357 else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG))
5415 { 5358 {
5416 ok = 0;
5417 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5359 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5418 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); 5360 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity);
5361 xError_1 (SCHEME_A_ msg, 0);
5362 continue;
5419 } 5363 }
5420 5364 else
5421 if (ecb_expect_false (ok))
5422 { 5365 {
5423 if (pcd->arg_tests_encoding) 5366 if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */
5424 { 5367 {
5425 int i = 0; 5368 int i = 0;
5426 int j; 5369 int j;
5427 const char *t = pcd->arg_tests_encoding; 5370 const char *t = pcd->arg_tests_encoding;
5428 pointer arglist = SCHEME_V->args; 5371 pointer arglist = SCHEME_V->args;
5429 5372
5430 do 5373 do
5431 { 5374 {
5432 pointer arg = car (arglist); 5375 pointer arg = car (arglist);
5433 5376
5434 j = (int) t[0]; 5377 j = t[0];
5435 5378
5379 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5436 if (j == TST_LIST[0]) 5380 if (j == TST_LIST[0])
5437 { 5381 {
5438 if (arg != NIL && !is_pair (arg)) 5382 if (!tst_is_list (SCHEME_A_ arg))
5439 break; 5383 break;
5440 } 5384 }
5441 else 5385 else
5442 { 5386 {
5443 if (!tests[j].fct (arg)) 5387 if (!tests[j - 1].fct (arg))
5444 break; 5388 break;
5445 } 5389 }
5446 5390
5447 if (t[1] != 0) /* last test is replicated as necessary */ 5391 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5448 t++; 5392 t++;
5449 5393
5450 arglist = cdr (arglist); 5394 arglist = cdr (arglist);
5451 i++; 5395 i++;
5452 } 5396 }
5453 while (i < n); 5397 while (i < n);
5454 5398
5455 if (i < n) 5399 if (i < n)
5456 { 5400 {
5457 ok = 0;
5458 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); 5401 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind);
5402 xError_1 (SCHEME_A_ msg, 0);
5403 continue;
5459 } 5404 }
5460 } 5405 }
5461 } 5406 }
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 } 5407 }
5471#endif 5408#endif
5472 5409
5473 ok_to_freely_gc (SCHEME_A); 5410 ok_to_freely_gc (SCHEME_A);
5474 5411
5412 static const dispatch_func dispatch_funcs[] = {
5413 opexe_0,
5414 opexe_1,
5415 opexe_2,
5416 opexe_3,
5417 opexe_4,
5418 opexe_5,
5419 opexe_6,
5420 };
5421
5475 if (ecb_expect_false (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL)) 5422 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5476 return; 5423 return;
5477 5424
5478 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5425 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5479 { 5426 {
5480 xwrstr ("No memory!\n"); 5427 xwrstr ("No memory!\n");
5504mk_proc (SCHEME_P_ enum scheme_opcodes op) 5451mk_proc (SCHEME_P_ enum scheme_opcodes op)
5505{ 5452{
5506 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5453 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5507 set_typeflag (y, (T_PROC | T_ATOM)); 5454 set_typeflag (y, (T_PROC | T_ATOM));
5508 ivalue_unchecked (y) = op; 5455 ivalue_unchecked (y) = op;
5509 set_num_integer (y);
5510 return y; 5456 return y;
5511} 5457}
5512 5458
5513/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5459/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5514static int 5460static int
5579 return OP_C0STREAM; /* cons-stream */ 5525 return OP_C0STREAM; /* cons-stream */
5580 } 5526 }
5581} 5527}
5582 5528
5583#if USE_MULTIPLICITY 5529#if USE_MULTIPLICITY
5584scheme * 5530ecb_cold scheme *
5585scheme_init_new () 5531scheme_init_new ()
5586{ 5532{
5587 scheme *sc = malloc (sizeof (scheme)); 5533 scheme *sc = malloc (sizeof (scheme));
5588 5534
5589 if (!scheme_init (SCHEME_A)) 5535 if (!scheme_init (SCHEME_A))
5594 else 5540 else
5595 return sc; 5541 return sc;
5596} 5542}
5597#endif 5543#endif
5598 5544
5599int 5545ecb_cold int
5600scheme_init (SCHEME_P) 5546scheme_init (SCHEME_P)
5601{ 5547{
5602 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5548 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5603 pointer x; 5549 pointer x;
5604 5550
5677 5623
5678 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) 5624 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
5679 assign_syntax (SCHEME_A_ syntax_names[i]); 5625 assign_syntax (SCHEME_A_ syntax_names[i]);
5680 } 5626 }
5681 5627
5628 // TODO: should iterate via strlen, to avoid n² complexity
5682 for (i = 0; i < n; i++) 5629 for (i = 0; i < n; i++)
5683 if (dispatch_table[i].name != 0) 5630 if (dispatch_table[i].builtin)
5684 assign_proc (SCHEME_A_ i, dispatch_table[i].name); 5631 assign_proc (SCHEME_A_ i, opname (i));
5685 5632
5686 /* initialization of global pointers to special symbols */ 5633 /* initialization of global pointers to special symbols */
5687 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 5634 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
5688 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 5635 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
5689 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 5636 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
5728scheme_set_external_data (SCHEME_P_ void *p) 5675scheme_set_external_data (SCHEME_P_ void *p)
5729{ 5676{
5730 SCHEME_V->ext_data = p; 5677 SCHEME_V->ext_data = p;
5731} 5678}
5732 5679
5733void 5680ecb_cold void
5734scheme_deinit (SCHEME_P) 5681scheme_deinit (SCHEME_P)
5735{ 5682{
5736 int i; 5683 int i;
5737 5684
5738#if SHOW_ERROR_LINE 5685#if SHOW_ERROR_LINE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines