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.28 by root, Sat Nov 28 08:09:04 2015 UTC

28#endif 28#endif
29#if USE_MATH 29#if USE_MATH
30# include <math.h> 30# include <math.h>
31#endif 31#endif
32 32
33#include "ecb.h"
34
33#include <sys/types.h> 35#include <sys/types.h>
34#include <sys/stat.h> 36#include <sys/stat.h>
35#include <fcntl.h> 37#include <fcntl.h>
36 38
37#include <string.h> 39#include <string.h>
65#define S_T (&SCHEME_V->xT) //TODO: magic ptr value? 67#define S_T (&SCHEME_V->xT) //TODO: magic ptr value?
66#define S_F (&SCHEME_V->xF) //TODO: magic ptr value? 68#define S_F (&SCHEME_V->xF) //TODO: magic ptr value?
67#define S_SINK (&SCHEME_V->xsink) 69#define S_SINK (&SCHEME_V->xsink)
68#define S_EOF (&SCHEME_V->xEOF_OBJ) 70#define S_EOF (&SCHEME_V->xEOF_OBJ)
69 71
70/* should use libecb */
71#if __GNUC__ >= 4
72# define ecb_expect(expr,value) __builtin_expect ((expr),(value))
73# define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
74# define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
75#else
76# define ecb_expect_false(expr) !!(expr)
77# define ecb_expect_true(expr) !!(expr)
78#endif
79
80#if !USE_MULTIPLICITY 72#if !USE_MULTIPLICITY
81static scheme sc; 73static scheme sc;
82#endif 74#endif
83 75
84static void 76static void
153 145
154#define toupper(c) xtoupper (c) 146#define toupper(c) xtoupper (c)
155#define tolower(c) xtolower (c) 147#define tolower(c) xtolower (c)
156#define isdigit(c) xisdigit (c) 148#define isdigit(c) xisdigit (c)
157 149
158#if USE_STRLWR 150#if USE_IGNORECASE
159static const char * 151static const char *
160strlwr (char *s) 152xstrlwr (char *s)
161{ 153{
162 const char *p = s; 154 const char *p = s;
163 155
164 while (*s) 156 while (*s)
165 { 157 {
167 s++; 159 s++;
168 } 160 }
169 161
170 return p; 162 return p;
171} 163}
172#endif
173 164
165#define stricmp(a,b) strcasecmp (a, b)
166#define strlwr(s) xstrlwr (s)
167
168#else
174#define stricmp(a,b) strcmp (a, b) 169# define stricmp(a,b) strcmp (a, b)
175#define strlwr(s) (s) 170# define strlwr(s) (s)
171#endif
176 172
177#ifndef prompt 173#ifndef prompt
178# define prompt "ts> " 174# define prompt "ts> "
179#endif 175#endif
180 176
186# define FIRST_CELLSEGS 3 182# define FIRST_CELLSEGS 3
187#endif 183#endif
188 184
189enum scheme_types 185enum scheme_types
190{ 186{
187 T_INTEGER,
191 T_FREE, 188 T_REAL,
192 T_STRING, 189 T_STRING,
193 T_NUMBER,
194 T_SYMBOL, 190 T_SYMBOL,
195 T_PROC, 191 T_PROC,
196 T_PAIR, 192 T_PAIR, /* also used for free cells */
197 T_CLOSURE, 193 T_CLOSURE,
198 T_CONTINUATION, 194 T_CONTINUATION,
199 T_FOREIGN, 195 T_FOREIGN,
200 T_CHARACTER, 196 T_CHARACTER,
201 T_PORT, 197 T_PORT,
211#define T_SYNTAX 0x0010 207#define T_SYNTAX 0x0010
212#define T_IMMUTABLE 0x0020 208#define T_IMMUTABLE 0x0020
213#define T_ATOM 0x0040 /* only for gc */ 209#define T_ATOM 0x0040 /* only for gc */
214#define T_MARK 0x0080 /* only for gc */ 210#define T_MARK 0x0080 /* only for gc */
215 211
212/* num, for generic arithmetic */
213struct num
214{
215 IVALUE ivalue;
216#if USE_REAL
217 RVALUE rvalue;
218 char is_fixnum;
219#endif
220};
221
222#if USE_REAL
223# define num_is_fixnum(n) (n).is_fixnum
224# define num_set_fixnum(n,f) (n).is_fixnum = (f)
225# define num_ivalue(n) (n).ivalue
226# define num_rvalue(n) (n).rvalue
227# define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i)
228# define num_set_rvalue(n,r) (n).rvalue = (r)
229#else
230# define num_is_fixnum(n) 1
231# define num_set_fixnum(n,f) 0
232# define num_ivalue(n) (n).ivalue
233# define num_rvalue(n) (n).ivalue
234# define num_set_ivalue(n,i) (n).ivalue = (i)
235# define num_set_rvalue(n,r) (n).ivalue = (r)
236#endif
237
216enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; 238enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
217 239
218static num num_op (enum num_op op, num a, num b); 240static num num_op (enum num_op op, num a, num b);
219static num num_intdiv (num a, num b); 241static num num_intdiv (num a, num b);
220static num num_rem (num a, num b); 242static num num_rem (num a, num b);
223#if USE_MATH 245#if USE_MATH
224static double round_per_R5RS (double x); 246static double round_per_R5RS (double x);
225#endif 247#endif
226static int is_zero_rvalue (RVALUE x); 248static int is_zero_rvalue (RVALUE x);
227 249
228static INLINE int
229num_is_integer (pointer p)
230{
231 return num_is_fixnum (p->object.number);
232}
233
234static num num_zero; 250static num num_zero;
235static num num_one; 251static num num_one;
236 252
237/* macros for cell operations */ 253/* macros for cell operations */
238#define typeflag(p) ((p)->flag + 0) 254#define typeflag(p) ((p)->flag + 0)
239#define set_typeflag(p,v) ((p)->flag = (v)) 255#define set_typeflag(p,v) ((p)->flag = (v))
240#define type(p) (typeflag (p) & T_MASKTYPE) 256#define type(p) (typeflag (p) & T_MASKTYPE)
241 257
242INTERFACE INLINE int 258INTERFACE int
243is_string (pointer p) 259is_string (pointer p)
244{ 260{
245 return type (p) == T_STRING; 261 return type (p) == T_STRING;
246} 262}
247 263
248#define strvalue(p) ((p)->object.string.svalue) 264#define strvalue(p) ((p)->object.string.svalue)
249#define strlength(p) ((p)->object.string.length) 265#define strlength(p) ((p)->object.string.length)
250 266
251INTERFACE int is_list (SCHEME_P_ pointer p);
252INTERFACE INLINE int 267INTERFACE int
253is_vector (pointer p) 268is_vector (pointer p)
254{ 269{
255 return type (p) == T_VECTOR; 270 return type (p) == T_VECTOR;
256} 271}
257 272
258#define vecvalue(p) ((p)->object.vector.vvalue) 273#define vecvalue(p) ((p)->object.vector.vvalue)
259#define veclength(p) ((p)->object.vector.length) 274#define veclength(p) ((p)->object.vector.length)
260INTERFACE void fill_vector (pointer vec, pointer obj); 275INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
261INTERFACE uint32_t vector_length (pointer vec);
262INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); 276INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
263INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); 277INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
264 278
265INTERFACE uint32_t 279INTERFACE int
266vector_length (pointer vec) 280is_integer (pointer p)
267{ 281{
268 return vec->object.vector.length; 282 return type (p) == T_INTEGER;
269} 283}
270 284
285/* not the same as in scheme, where integers are (correctly :) reals */
271INTERFACE INLINE int 286INTERFACE int
287is_real (pointer p)
288{
289 return type (p) == T_REAL;
290}
291
292INTERFACE int
272is_number (pointer p) 293is_number (pointer p)
273{ 294{
274 return type (p) == T_NUMBER; 295 return is_integer (p) || is_real (p);
275} 296}
276 297
277INTERFACE INLINE int 298INTERFACE int
278is_integer (pointer p)
279{
280 if (!is_number (p))
281 return 0;
282
283 if (num_is_integer (p) || ivalue (p) == rvalue (p))
284 return 1;
285
286 return 0;
287}
288
289INTERFACE INLINE int
290is_real (pointer p)
291{
292 return is_number (p) && !num_is_fixnum (p->object.number);
293}
294
295INTERFACE INLINE int
296is_character (pointer p) 299is_character (pointer p)
297{ 300{
298 return type (p) == T_CHARACTER; 301 return type (p) == T_CHARACTER;
299} 302}
300 303
301INTERFACE INLINE char * 304INTERFACE char *
302string_value (pointer p) 305string_value (pointer p)
303{ 306{
304 return strvalue (p); 307 return strvalue (p);
305} 308}
306 309
307INLINE num
308nvalue (pointer p)
309{
310 return (p)->object.number;
311}
312
313static IVALUE
314num_get_ivalue (const num n)
315{
316 return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n);
317}
318
319static RVALUE
320num_get_rvalue (const num n)
321{
322 return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n);
323}
324
325INTERFACE IVALUE
326ivalue (pointer p)
327{
328 return num_get_ivalue (p->object.number);
329}
330
331INTERFACE RVALUE
332rvalue (pointer p)
333{
334 return num_get_rvalue (p->object.number);
335}
336
337#define ivalue_unchecked(p) ((p)->object.number.value.ivalue) 310#define ivalue_unchecked(p) (p)->object.ivalue
311#define set_ivalue(p,v) (p)->object.ivalue = (v)
312
338#if USE_REAL 313#if USE_REAL
339# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 314#define rvalue_unchecked(p) (p)->object.rvalue
340# define set_num_integer(p) (p)->object.number.is_fixnum=1; 315#define set_rvalue(p,v) (p)->object.rvalue = (v)
341# define set_num_real(p) (p)->object.number.is_fixnum=0;
342#else 316#else
343# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 317#define rvalue_unchecked(p) (p)->object.ivalue
344# define set_num_integer(p) 0 318#define set_rvalue(p,v) (p)->object.ivalue = (v)
345# define set_num_real(p) 0
346#endif 319#endif
320
347INTERFACE long 321INTERFACE long
348charvalue (pointer p) 322charvalue (pointer p)
349{ 323{
350 return ivalue_unchecked (p); 324 return ivalue_unchecked (p);
351} 325}
352 326
353INTERFACE INLINE int 327INTERFACE int
354is_port (pointer p) 328is_port (pointer p)
355{ 329{
356 return type (p) == T_PORT; 330 return type (p) == T_PORT;
357} 331}
358 332
359INTERFACE INLINE int 333INTERFACE int
360is_inport (pointer p) 334is_inport (pointer p)
361{ 335{
362 return is_port (p) && p->object.port->kind & port_input; 336 return is_port (p) && p->object.port->kind & port_input;
363} 337}
364 338
365INTERFACE INLINE int 339INTERFACE int
366is_outport (pointer p) 340is_outport (pointer p)
367{ 341{
368 return is_port (p) && p->object.port->kind & port_output; 342 return is_port (p) && p->object.port->kind & port_output;
369} 343}
370 344
371INTERFACE INLINE int 345INTERFACE int
372is_pair (pointer p) 346is_pair (pointer p)
373{ 347{
374 return type (p) == T_PAIR; 348 return type (p) == T_PAIR;
375} 349}
376 350
408pair_cdr (pointer p) 382pair_cdr (pointer p)
409{ 383{
410 return cdr (p); 384 return cdr (p);
411} 385}
412 386
413INTERFACE INLINE int 387INTERFACE int
414is_symbol (pointer p) 388is_symbol (pointer p)
415{ 389{
416 return type (p) == T_SYMBOL; 390 return type (p) == T_SYMBOL;
417} 391}
418 392
419INTERFACE INLINE char * 393INTERFACE char *
420symname (pointer p) 394symname (pointer p)
421{ 395{
422 return strvalue (car (p)); 396 return strvalue (car (p));
423} 397}
424 398
425#if USE_PLIST 399#if USE_PLIST
426SCHEME_EXPORT INLINE int 400SCHEME_EXPORT int
427hasprop (pointer p) 401hasprop (pointer p)
428{ 402{
429 return typeflag (p) & T_SYMBOL; 403 return typeflag (p) & T_SYMBOL;
430} 404}
431 405
432# define symprop(p) cdr(p) 406# define symprop(p) cdr(p)
433#endif 407#endif
434 408
435INTERFACE INLINE int 409INTERFACE int
436is_syntax (pointer p) 410is_syntax (pointer p)
437{ 411{
438 return typeflag (p) & T_SYNTAX; 412 return typeflag (p) & T_SYNTAX;
439} 413}
440 414
441INTERFACE INLINE int 415INTERFACE int
442is_proc (pointer p) 416is_proc (pointer p)
443{ 417{
444 return type (p) == T_PROC; 418 return type (p) == T_PROC;
445} 419}
446 420
447INTERFACE INLINE int 421INTERFACE int
448is_foreign (pointer p) 422is_foreign (pointer p)
449{ 423{
450 return type (p) == T_FOREIGN; 424 return type (p) == T_FOREIGN;
451} 425}
452 426
453INTERFACE INLINE char * 427INTERFACE char *
454syntaxname (pointer p) 428syntaxname (pointer p)
455{ 429{
456 return strvalue (car (p)); 430 return strvalue (car (p));
457} 431}
458 432
459#define procnum(p) ivalue (p) 433#define procnum(p) ivalue_unchecked (p)
460static const char *procname (pointer x); 434static const char *procname (pointer x);
461 435
462INTERFACE INLINE int 436INTERFACE int
463is_closure (pointer p) 437is_closure (pointer p)
464{ 438{
465 return type (p) == T_CLOSURE; 439 return type (p) == T_CLOSURE;
466} 440}
467 441
468INTERFACE INLINE int 442INTERFACE int
469is_macro (pointer p) 443is_macro (pointer p)
470{ 444{
471 return type (p) == T_MACRO; 445 return type (p) == T_MACRO;
472} 446}
473 447
474INTERFACE INLINE pointer 448INTERFACE pointer
475closure_code (pointer p) 449closure_code (pointer p)
476{ 450{
477 return car (p); 451 return car (p);
478} 452}
479 453
480INTERFACE INLINE pointer 454INTERFACE pointer
481closure_env (pointer p) 455closure_env (pointer p)
482{ 456{
483 return cdr (p); 457 return cdr (p);
484} 458}
485 459
486INTERFACE INLINE int 460INTERFACE int
487is_continuation (pointer p) 461is_continuation (pointer p)
488{ 462{
489 return type (p) == T_CONTINUATION; 463 return type (p) == T_CONTINUATION;
490} 464}
491 465
492#define cont_dump(p) cdr (p) 466#define cont_dump(p) cdr (p)
493#define set_cont_dump(p,v) set_cdr ((p), (v)) 467#define set_cont_dump(p,v) set_cdr ((p), (v))
494 468
495/* To do: promise should be forced ONCE only */ 469/* To do: promise should be forced ONCE only */
496INTERFACE INLINE int 470INTERFACE int
497is_promise (pointer p) 471is_promise (pointer p)
498{ 472{
499 return type (p) == T_PROMISE; 473 return type (p) == T_PROMISE;
500} 474}
501 475
502INTERFACE INLINE int 476INTERFACE int
503is_environment (pointer p) 477is_environment (pointer p)
504{ 478{
505 return type (p) == T_ENVIRONMENT; 479 return type (p) == T_ENVIRONMENT;
506} 480}
507 481
513 487
514#define is_mark(p) (typeflag (p) & T_MARK) 488#define is_mark(p) (typeflag (p) & T_MARK)
515#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) 489#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
516#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) 490#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
517 491
518INTERFACE INLINE int 492INTERFACE int
519is_immutable (pointer p) 493is_immutable (pointer p)
520{ 494{
521 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; 495 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
522} 496}
523 497
524INTERFACE INLINE void 498INTERFACE void
525setimmutable (pointer p) 499setimmutable (pointer p)
526{ 500{
527#if USE_ERROR_CHECKING 501#if USE_ERROR_CHECKING
528 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 502 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
529#endif 503#endif
530} 504}
531 505
506/* Result is:
507 proper list: length
508 circular list: -1
509 not even a pair: -2
510 dotted list: -2 minus length before dot
511*/
512INTERFACE int
513list_length (SCHEME_P_ pointer a)
514{
515 int i = 0;
516 pointer slow, fast;
517
518 slow = fast = a;
519
520 while (1)
521 {
522 if (fast == NIL)
523 return i;
524
525 if (!is_pair (fast))
526 return -2 - i;
527
528 fast = cdr (fast);
529 ++i;
530
531 if (fast == NIL)
532 return i;
533
534 if (!is_pair (fast))
535 return -2 - i;
536
537 ++i;
538 fast = cdr (fast);
539
540 /* Safe because we would have already returned if `fast'
541 encountered a non-pair. */
542 slow = cdr (slow);
543
544 if (fast == slow)
545 {
546 /* the fast pointer has looped back around and caught up
547 with the slow pointer, hence the structure is circular,
548 not of finite length, and therefore not a list */
549 return -1;
550 }
551 }
552}
553
554INTERFACE int
555is_list (SCHEME_P_ pointer a)
556{
557 return list_length (SCHEME_A_ a) >= 0;
558}
559
532#if USE_CHAR_CLASSIFIERS 560#if USE_CHAR_CLASSIFIERS
533static INLINE int 561ecb_inline int
534Cisalpha (int c) 562Cisalpha (int c)
535{ 563{
536 return isascii (c) && isalpha (c); 564 return isascii (c) && isalpha (c);
537} 565}
538 566
539static INLINE int 567ecb_inline int
540Cisdigit (int c) 568Cisdigit (int c)
541{ 569{
542 return isascii (c) && isdigit (c); 570 return isascii (c) && isdigit (c);
543} 571}
544 572
545static INLINE int 573ecb_inline int
546Cisspace (int c) 574Cisspace (int c)
547{ 575{
548 return isascii (c) && isspace (c); 576 return isascii (c) && isspace (c);
549} 577}
550 578
551static INLINE int 579ecb_inline int
552Cisupper (int c) 580Cisupper (int c)
553{ 581{
554 return isascii (c) && isupper (c); 582 return isascii (c) && isupper (c);
555} 583}
556 584
557static INLINE int 585ecb_inline int
558Cislower (int c) 586Cislower (int c)
559{ 587{
560 return isascii (c) && islower (c); 588 return isascii (c) && islower (c);
561} 589}
562#endif 590#endif
623#endif 651#endif
624 652
625static int file_push (SCHEME_P_ const char *fname); 653static int file_push (SCHEME_P_ const char *fname);
626static void file_pop (SCHEME_P); 654static void file_pop (SCHEME_P);
627static int file_interactive (SCHEME_P); 655static int file_interactive (SCHEME_P);
628static INLINE int is_one_of (char *s, int c); 656ecb_inline int is_one_of (char *s, int c);
629static int alloc_cellseg (SCHEME_P_ int n); 657static int alloc_cellseg (SCHEME_P_ int n);
630static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); 658ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
631static void finalize_cell (SCHEME_P_ pointer a); 659static void finalize_cell (SCHEME_P_ pointer a);
632static int count_consecutive_cells (pointer x, int needed); 660static int count_consecutive_cells (pointer x, int needed);
633static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 661static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
634static pointer mk_number (SCHEME_P_ const num n); 662static pointer mk_number (SCHEME_P_ const num n);
635static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); 663static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
652static int basic_inchar (port *pt); 680static int basic_inchar (port *pt);
653static int inchar (SCHEME_P); 681static int inchar (SCHEME_P);
654static void backchar (SCHEME_P_ int c); 682static void backchar (SCHEME_P_ int c);
655static char *readstr_upto (SCHEME_P_ char *delim); 683static char *readstr_upto (SCHEME_P_ char *delim);
656static pointer readstrexp (SCHEME_P); 684static pointer readstrexp (SCHEME_P);
657static INLINE int skipspace (SCHEME_P); 685ecb_inline int skipspace (SCHEME_P);
658static int token (SCHEME_P); 686static int token (SCHEME_P);
659static void printslashstring (SCHEME_P_ char *s, int len); 687static void printslashstring (SCHEME_P_ char *s, int len);
660static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 688static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
661static void printatom (SCHEME_P_ pointer l, int f); 689static void printatom (SCHEME_P_ pointer l, int f);
662static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 690static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
666static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); 694static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list);
667static pointer revappend (SCHEME_P_ pointer a, pointer b); 695static pointer revappend (SCHEME_P_ pointer a, pointer b);
668static pointer ss_get_cont (SCHEME_P); 696static pointer ss_get_cont (SCHEME_P);
669static void ss_set_cont (SCHEME_P_ pointer cont); 697static void ss_set_cont (SCHEME_P_ pointer cont);
670static void dump_stack_mark (SCHEME_P); 698static void dump_stack_mark (SCHEME_P);
671static pointer opexe_0 (SCHEME_P_ enum scheme_opcodes op); 699static int opexe_0 (SCHEME_P_ enum scheme_opcodes op);
700static int opexe_1 (SCHEME_P_ enum scheme_opcodes op);
672static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); 701static int opexe_2 (SCHEME_P_ enum scheme_opcodes op);
673static pointer opexe_r (SCHEME_P_ enum scheme_opcodes op);
674static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); 702static int opexe_3 (SCHEME_P_ enum scheme_opcodes op);
675static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); 703static int opexe_4 (SCHEME_P_ enum scheme_opcodes op);
676static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); 704static int opexe_5 (SCHEME_P_ enum scheme_opcodes op);
677static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); 705static int opexe_6 (SCHEME_P_ enum scheme_opcodes op);
678static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 706static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
679static void assign_syntax (SCHEME_P_ const char *name); 707static void assign_syntax (SCHEME_P_ const char *name);
680static int syntaxnum (pointer p); 708static int syntaxnum (pointer p);
681static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 709static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
682 710
711static IVALUE
712ivalue (pointer x)
713{
714 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
715}
716
717static RVALUE
718rvalue (pointer x)
719{
720 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
721}
722
723INTERFACE num
724nvalue (pointer x)
725{
726 num n;
727
728 num_set_fixnum (n, is_integer (x));
729
730 if (num_is_fixnum (n))
731 num_set_ivalue (n, ivalue_unchecked (x));
732 else
733 num_set_rvalue (n, rvalue_unchecked (x));
734
735 return n;
736}
737
683static num 738static num
684num_op (enum num_op op, num a, num b) 739num_op (enum num_op op, num a, num b)
685{ 740{
686 num ret; 741 num ret;
687 742
688 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 743 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
689 744
690 if (num_is_fixnum (ret)) 745 if (num_is_fixnum (ret))
691 { 746 {
692 IVALUE av = num_get_ivalue (a);
693 IVALUE bv = num_get_ivalue (b);
694
695 switch (op) 747 switch (op)
696 { 748 {
697 case NUM_ADD: av += bv; break; 749 case NUM_ADD: a.ivalue += b.ivalue; break;
698 case NUM_SUB: av -= bv; break; 750 case NUM_SUB: a.ivalue -= b.ivalue; break;
699 case NUM_MUL: av *= bv; break; 751 case NUM_MUL: a.ivalue *= b.ivalue; break;
700 case NUM_INTDIV: av /= bv; break; 752 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
701 } 753 }
702 754
703 num_set_ivalue (ret, av); 755 num_set_ivalue (ret, a.ivalue);
704 } 756 }
757#if USE_REAL
705 else 758 else
706 { 759 {
707 RVALUE av = num_get_rvalue (a);
708 RVALUE bv = num_get_rvalue (b);
709
710 switch (op) 760 switch (op)
711 { 761 {
712 case NUM_ADD: av += bv; break; 762 case NUM_ADD: a.rvalue += b.rvalue; break;
713 case NUM_SUB: av -= bv; break; 763 case NUM_SUB: a.rvalue -= b.rvalue; break;
714 case NUM_MUL: av *= bv; break; 764 case NUM_MUL: a.rvalue *= b.rvalue; break;
715 case NUM_INTDIV: av /= bv; break; 765 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
716 } 766 }
717 767
718 num_set_rvalue (ret, av); 768 num_set_rvalue (ret, a.rvalue);
719 } 769 }
770#endif
720 771
721 return ret; 772 return ret;
722} 773}
723 774
724static num 775static num
725num_div (num a, num b) 776num_div (num a, num b)
726{ 777{
727 num ret; 778 num ret;
728 779
729 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); 780 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0);
730 781
731 if (num_is_fixnum (ret)) 782 if (num_is_fixnum (ret))
732 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 783 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
733 else 784 else
734 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 785 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
735 786
736 return ret; 787 return ret;
737} 788}
738 789
739static num 790static num
741{ 792{
742 num ret; 793 num ret;
743 long e1, e2, res; 794 long e1, e2, res;
744 795
745 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 796 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
746 e1 = num_get_ivalue (a); 797 e1 = num_ivalue (a);
747 e2 = num_get_ivalue (b); 798 e2 = num_ivalue (b);
748 res = e1 % e2; 799 res = e1 % e2;
749 800
750 /* remainder should have same sign as second operand */ 801 /* remainder should have same sign as second operand */
751 if (res > 0) 802 if (res > 0)
752 { 803 {
768{ 819{
769 num ret; 820 num ret;
770 long e1, e2, res; 821 long e1, e2, res;
771 822
772 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 823 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
773 e1 = num_get_ivalue (a); 824 e1 = num_ivalue (a);
774 e2 = num_get_ivalue (b); 825 e2 = num_ivalue (b);
775 res = e1 % e2; 826 res = e1 % e2;
776 827
777 /* modulo should have same sign as second operand */ 828 /* modulo should have same sign as second operand */
778 if (res * e2 < 0) 829 if (res * e2 < 0)
779 res += e2; 830 res += e2;
780 831
781 num_set_ivalue (ret, res); 832 num_set_ivalue (ret, res);
782 return ret; 833 return ret;
783} 834}
784 835
785/* this completely disrespects NaNs */ 836/* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */
786static int 837static int
787num_cmp (num a, num b) 838num_cmp (num a, num b)
788{ 839{
789 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 840 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
790 int ret; 841 int ret;
791 842
792 if (is_fixnum) 843 if (is_fixnum)
793 { 844 {
794 IVALUE av = num_get_ivalue (a); 845 IVALUE av = num_ivalue (a);
795 IVALUE bv = num_get_ivalue (b); 846 IVALUE bv = num_ivalue (b);
796 847
797 ret = av == bv ? 0 : av < bv ? -1 : +1; 848 ret = av == bv ? 0 : av < bv ? -1 : +1;
798 } 849 }
799 else 850 else
800 { 851 {
801 RVALUE av = num_get_rvalue (a); 852 RVALUE av = num_rvalue (a);
802 RVALUE bv = num_get_rvalue (b); 853 RVALUE bv = num_rvalue (b);
803 854
804 ret = av == bv ? 0 : av < bv ? -1 : +1; 855 ret = av == bv ? 0 : av < bv ? -1 : +1;
805 } 856 }
806 857
807 return ret; 858 return ret;
833#endif 884#endif
834 885
835static int 886static int
836is_zero_rvalue (RVALUE x) 887is_zero_rvalue (RVALUE x)
837{ 888{
889 return x == 0;
890#if 0
838#if USE_REAL 891#if USE_REAL
839 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ 892 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */
840#else 893#else
841 return x == 0; 894 return x == 0;
895#endif
842#endif 896#endif
843} 897}
844 898
845/* allocate new cell segment */ 899/* allocate new cell segment */
846static int 900static int
867 return k; 921 return k;
868 922
869 i = ++SCHEME_V->last_cell_seg; 923 i = ++SCHEME_V->last_cell_seg;
870 SCHEME_V->alloc_seg[i] = cp; 924 SCHEME_V->alloc_seg[i] = cp;
871 925
872 /* insert new segment in address order */
873 newp = (pointer)cp; 926 newp = (pointer)cp;
874 SCHEME_V->cell_seg[i] = newp; 927 SCHEME_V->cell_seg[i] = newp;
875 SCHEME_V->cell_segsize[i] = segsize; 928 SCHEME_V->cell_segsize[i] = segsize;
876
877 //TODO: insert, not swap
878 while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i])
879 {
880 p = SCHEME_V->cell_seg[i];
881 SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1];
882 SCHEME_V->cell_seg[i - 1] = p;
883
884 k = SCHEME_V->cell_segsize[i];
885 SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1];
886 SCHEME_V->cell_segsize[i - 1] = k;
887
888 --i;
889 }
890
891 SCHEME_V->fcells += segsize; 929 SCHEME_V->fcells += segsize;
892 last = newp + segsize - 1; 930 last = newp + segsize - 1;
893 931
894 for (p = newp; p <= last; p++) 932 for (p = newp; p <= last; p++)
895 { 933 {
896 set_typeflag (p, T_FREE); 934 set_typeflag (p, T_PAIR);
897 set_car (p, NIL); 935 set_car (p, NIL);
898 set_cdr (p, p + 1); 936 set_cdr (p, p + 1);
899 } 937 }
900 938
901 /* insert new cells in address order on free list */
902 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell)
903 {
904 set_cdr (last, SCHEME_V->free_cell); 939 set_cdr (last, SCHEME_V->free_cell);
905 SCHEME_V->free_cell = newp; 940 SCHEME_V->free_cell = newp;
906 }
907 else
908 {
909 p = SCHEME_V->free_cell;
910
911 while (cdr (p) != NIL && newp > cdr (p))
912 p = cdr (p);
913
914 set_cdr (last, cdr (p));
915 set_cdr (p, newp);
916 }
917 } 941 }
918 942
919 return n; 943 return n;
920} 944}
921 945
922/* get new cell. parameter a, b is marked by gc. */ 946/* get new cell. parameter a, b is marked by gc. */
923static INLINE pointer 947ecb_inline pointer
924get_cell_x (SCHEME_P_ pointer a, pointer b) 948get_cell_x (SCHEME_P_ pointer a, pointer b)
925{ 949{
926 if (ecb_expect_false (SCHEME_V->free_cell == NIL)) 950 if (ecb_expect_false (SCHEME_V->free_cell == NIL))
927 { 951 {
928 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 952 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
1000 /* Record it as a vector so that gc understands it. */ 1024 /* Record it as a vector so that gc understands it. */
1001 set_typeflag (v, T_VECTOR | T_ATOM); 1025 set_typeflag (v, T_VECTOR | T_ATOM);
1002 1026
1003 v->object.vector.vvalue = e; 1027 v->object.vector.vvalue = e;
1004 v->object.vector.length = len; 1028 v->object.vector.length = len;
1005 fill_vector (v, init); 1029 fill_vector (v, 0, init);
1006 push_recent_alloc (SCHEME_A_ v, NIL); 1030 push_recent_alloc (SCHEME_A_ v, NIL);
1007 1031
1008 return v; 1032 return v;
1009} 1033}
1010 1034
1011static INLINE void 1035ecb_inline void
1012ok_to_freely_gc (SCHEME_P) 1036ok_to_freely_gc (SCHEME_P)
1013{ 1037{
1014 set_car (S_SINK, NIL); 1038 set_car (S_SINK, NIL);
1015} 1039}
1016 1040
1076 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); 1100 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1077 set_typeflag (x, T_SYMBOL); 1101 set_typeflag (x, T_SYMBOL);
1078 setimmutable (car (x)); 1102 setimmutable (car (x));
1079 1103
1080 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1104 location = hash_fn (name, veclength (SCHEME_V->oblist));
1081 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); 1105 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1082 return x; 1106 return x;
1083} 1107}
1084 1108
1085static INLINE pointer 1109ecb_inline pointer
1086oblist_find_by_name (SCHEME_P_ const char *name) 1110oblist_find_by_name (SCHEME_P_ const char *name)
1087{ 1111{
1088 int location; 1112 int location;
1089 pointer x; 1113 pointer x;
1090 char *s; 1114 char *s;
1091 1115
1092 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1116 location = hash_fn (name, veclength (SCHEME_V->oblist));
1093 1117
1094 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1118 for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1095 { 1119 {
1096 s = symname (car (x)); 1120 s = symname (car (x));
1097 1121
1098 /* case-insensitive, per R5RS section 2 */ 1122 /* case-insensitive, per R5RS section 2 */
1099 if (stricmp (name, s) == 0) 1123 if (stricmp (name, s) == 0)
1109 int i; 1133 int i;
1110 pointer x; 1134 pointer x;
1111 pointer ob_list = NIL; 1135 pointer ob_list = NIL;
1112 1136
1113 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1137 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1114 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1138 for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1115 ob_list = cons (x, ob_list); 1139 ob_list = cons (x, ob_list);
1116 1140
1117 return ob_list; 1141 return ob_list;
1118} 1142}
1119 1143
1123oblist_initial_value (SCHEME_P) 1147oblist_initial_value (SCHEME_P)
1124{ 1148{
1125 return NIL; 1149 return NIL;
1126} 1150}
1127 1151
1128static INLINE pointer 1152ecb_inline pointer
1129oblist_find_by_name (SCHEME_P_ const char *name) 1153oblist_find_by_name (SCHEME_P_ const char *name)
1130{ 1154{
1131 pointer x; 1155 pointer x;
1132 char *s; 1156 char *s;
1133 1157
1192mk_character (SCHEME_P_ int c) 1216mk_character (SCHEME_P_ int c)
1193{ 1217{
1194 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1218 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1195 1219
1196 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1220 set_typeflag (x, (T_CHARACTER | T_ATOM));
1197 ivalue_unchecked (x) = c & 0xff; 1221 set_ivalue (x, c & 0xff);
1198 set_num_integer (x); 1222
1199 return x; 1223 return x;
1200} 1224}
1201 1225
1202/* get number atom (integer) */ 1226/* get number atom (integer) */
1203INTERFACE pointer 1227INTERFACE pointer
1204mk_integer (SCHEME_P_ long num) 1228mk_integer (SCHEME_P_ long n)
1205{ 1229{
1206 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1230 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1207 1231
1208 set_typeflag (x, (T_NUMBER | T_ATOM)); 1232 set_typeflag (x, (T_INTEGER | T_ATOM));
1209 ivalue_unchecked (x) = num; 1233 set_ivalue (x, n);
1210 set_num_integer (x); 1234
1211 return x; 1235 return x;
1212} 1236}
1213 1237
1214INTERFACE pointer 1238INTERFACE pointer
1215mk_real (SCHEME_P_ RVALUE n) 1239mk_real (SCHEME_P_ RVALUE n)
1216{ 1240{
1241#if USE_REAL
1217 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1242 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1218 1243
1219 set_typeflag (x, (T_NUMBER | T_ATOM)); 1244 set_typeflag (x, (T_REAL | T_ATOM));
1220 rvalue_unchecked (x) = n; 1245 set_rvalue (x, n);
1221 set_num_real (x); 1246
1222 return x; 1247 return x;
1248#else
1249 return mk_integer (SCHEME_A_ n);
1250#endif
1223} 1251}
1224 1252
1225static pointer 1253static pointer
1226mk_number (SCHEME_P_ const num n) 1254mk_number (SCHEME_P_ const num n)
1227{ 1255{
1256#if USE_REAL
1228 if (num_is_fixnum (n)) 1257 return num_is_fixnum (n)
1258 ? mk_integer (SCHEME_A_ num_ivalue (n))
1259 : mk_real (SCHEME_A_ num_rvalue (n));
1260#else
1229 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1261 return mk_integer (SCHEME_A_ num_ivalue (n));
1230 else 1262#endif
1231 return mk_real (SCHEME_A_ num_get_rvalue (n));
1232} 1263}
1233 1264
1234/* allocate name to string area */ 1265/* allocate name to string area */
1235static char * 1266static char *
1236store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) 1267store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1295{ 1326{
1296 return get_vector_object (SCHEME_A_ len, NIL); 1327 return get_vector_object (SCHEME_A_ len, NIL);
1297} 1328}
1298 1329
1299INTERFACE void 1330INTERFACE void
1300fill_vector (pointer vec, pointer obj) 1331fill_vector (pointer vec, uint32_t start, pointer obj)
1301{ 1332{
1302 int i; 1333 int i;
1303 1334
1304 for (i = 0; i < vec->object.vector.length; i++) 1335 for (i = start; i < veclength (vec); i++)
1305 vecvalue (vec)[i] = obj; 1336 vecvalue (vec)[i] = obj;
1306} 1337}
1307 1338
1308INTERFACE pointer 1339INTERFACE pointer
1309vector_elem (pointer vec, uint32_t ielem) 1340vector_get (pointer vec, uint32_t ielem)
1310{ 1341{
1311 return vecvalue(vec)[ielem]; 1342 return vecvalue(vec)[ielem];
1312} 1343}
1313 1344
1314INTERFACE void 1345INTERFACE void
1315set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1346vector_set (pointer vec, uint32_t ielem, pointer a)
1316{ 1347{
1317 vecvalue(vec)[ielem] = a; 1348 vecvalue(vec)[ielem] = a;
1318} 1349}
1319 1350
1320/* get new symbol */ 1351/* get new symbol */
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 {
2822 2853
2823 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2854 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2824 setenvironment (SCHEME_V->envir); 2855 setenvironment (SCHEME_V->envir);
2825} 2856}
2826 2857
2827static INLINE void 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 = hash_fn (symname (variable), veclength (car (env)));
2835 2866
2836 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2867 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2837 } 2868 }
2838 else 2869 else
2839 set_car (env, immutable_cons (slot, car (env))); 2870 set_car (env, immutable_cons (slot, car (env)));
2840} 2871}
2841 2872
2848 for (x = env; x != NIL; x = cdr (x)) 2879 for (x = env; x != NIL; x = cdr (x))
2849 { 2880 {
2850 if (is_vector (car (x))) 2881 if (is_vector (car (x)))
2851 { 2882 {
2852 location = hash_fn (symname (hdl), veclength (car (x))); 2883 location = hash_fn (symname (hdl), veclength (car (x)));
2853 y = vector_elem (car (x), location); 2884 y = vector_get (car (x), location);
2854 } 2885 }
2855 else 2886 else
2856 y = car (x); 2887 y = car (x);
2857 2888
2858 for (; y != NIL; y = cdr (y)) 2889 for (; y != NIL; y = cdr (y))
2872 return NIL; 2903 return NIL;
2873} 2904}
2874 2905
2875#else /* USE_ALIST_ENV */ 2906#else /* USE_ALIST_ENV */
2876 2907
2877static INLINE void 2908ecb_inline void
2878new_frame_in_env (SCHEME_P_ pointer old_env) 2909new_frame_in_env (SCHEME_P_ pointer old_env)
2879{ 2910{
2880 SCHEME_V->envir = immutable_cons (NIL, old_env); 2911 SCHEME_V->envir = immutable_cons (NIL, old_env);
2881 setenvironment (SCHEME_V->envir); 2912 setenvironment (SCHEME_V->envir);
2882} 2913}
2883 2914
2884static INLINE void 2915ecb_inline void
2885new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2916new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2886{ 2917{
2887 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2918 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2888} 2919}
2889 2920
2911 return NIL; 2942 return NIL;
2912} 2943}
2913 2944
2914#endif /* USE_ALIST_ENV else */ 2945#endif /* USE_ALIST_ENV else */
2915 2946
2916static INLINE void 2947ecb_inline void
2917new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2948new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2918{ 2949{
2919 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2950 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2920} 2951}
2921 2952
2922static INLINE void 2953ecb_inline void
2923set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2954set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2924{ 2955{
2925 set_cdr (slot, value); 2956 set_cdr (slot, value);
2926} 2957}
2927 2958
2928static INLINE pointer 2959ecb_inline pointer
2929slot_value_in_env (pointer slot) 2960slot_value_in_env (pointer slot)
2930{ 2961{
2931 return cdr (slot); 2962 return cdr (slot);
2932} 2963}
2933 2964
2934/* ========== Evaluation Cycle ========== */ 2965/* ========== Evaluation Cycle ========== */
2935 2966
2936static pointer 2967static int
2937xError_1 (SCHEME_P_ const char *s, pointer a) 2968xError_1 (SCHEME_P_ const char *s, pointer a)
2938{ 2969{
2939#if USE_ERROR_HOOK 2970#if USE_ERROR_HOOK
2940 pointer x; 2971 pointer x;
2941 pointer hdl = SCHEME_V->ERROR_HOOK; 2972 pointer hdl = SCHEME_V->ERROR_HOOK;
2976 code = cons (mk_string (SCHEME_A_ s), code); 3007 code = cons (mk_string (SCHEME_A_ s), code);
2977 setimmutable (car (code)); 3008 setimmutable (car (code));
2978 SCHEME_V->code = cons (slot_value_in_env (x), code); 3009 SCHEME_V->code = cons (slot_value_in_env (x), code);
2979 SCHEME_V->op = OP_EVAL; 3010 SCHEME_V->op = OP_EVAL;
2980 3011
2981 return S_T; 3012 return 0;
2982 } 3013 }
2983#endif 3014#endif
2984 3015
2985 if (a) 3016 if (a)
2986 SCHEME_V->args = cons (a, NIL); 3017 SCHEME_V->args = cons (a, NIL);
2988 SCHEME_V->args = NIL; 3019 SCHEME_V->args = NIL;
2989 3020
2990 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); 3021 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args);
2991 setimmutable (car (SCHEME_V->args)); 3022 setimmutable (car (SCHEME_V->args));
2992 SCHEME_V->op = OP_ERR0; 3023 SCHEME_V->op = OP_ERR0;
3024
2993 return S_T; 3025 return 0;
2994} 3026}
2995 3027
2996#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) 3028#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a)
2997#define Error_0(s) Error_1 (s, 0) 3029#define Error_0(s) Error_1 (s, 0)
2998 3030
2999/* Too small to turn into function */ 3031/* Too small to turn into function */
3000#define BEGIN do { 3032#define BEGIN do {
3001#define END } while (0) 3033#define END } while (0)
3002#define s_goto(a) BEGIN \ 3034#define s_goto(a) BEGIN \
3003 SCHEME_V->op = a; \ 3035 SCHEME_V->op = a; \
3004 return S_T; END 3036 return 0; END
3005 3037
3006#define s_return(a) return xs_return (SCHEME_A_ a) 3038#define s_return(a) return xs_return (SCHEME_A_ a)
3007 3039
3008#ifndef USE_SCHEME_STACK 3040#ifndef USE_SCHEME_STACK
3009 3041
3039 next_frame->code = code; 3071 next_frame->code = code;
3040 3072
3041 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3073 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3042} 3074}
3043 3075
3044static pointer 3076static int
3045xs_return (SCHEME_P_ pointer a) 3077xs_return (SCHEME_P_ pointer a)
3046{ 3078{
3047 int nframes = (uintptr_t)SCHEME_V->dump; 3079 int nframes = (uintptr_t)SCHEME_V->dump;
3048 struct dump_stack_frame *frame; 3080 struct dump_stack_frame *frame;
3049 3081
3050 SCHEME_V->value = a; 3082 SCHEME_V->value = a;
3051 3083
3052 if (nframes <= 0) 3084 if (nframes <= 0)
3053 return NIL; 3085 return -1;
3054 3086
3055 frame = &SCHEME_V->dump_base[--nframes]; 3087 frame = &SCHEME_V->dump_base[--nframes];
3056 SCHEME_V->op = frame->op; 3088 SCHEME_V->op = frame->op;
3057 SCHEME_V->args = frame->args; 3089 SCHEME_V->args = frame->args;
3058 SCHEME_V->envir = frame->envir; 3090 SCHEME_V->envir = frame->envir;
3059 SCHEME_V->code = frame->code; 3091 SCHEME_V->code = frame->code;
3060 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3092 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3061 3093
3062 return S_T; 3094 return 0;
3063} 3095}
3064 3096
3065static INLINE void 3097ecb_inline void
3066dump_stack_reset (SCHEME_P) 3098dump_stack_reset (SCHEME_P)
3067{ 3099{
3068 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3100 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3069 SCHEME_V->dump = (pointer)+0; 3101 SCHEME_V->dump = (pointer)+0;
3070} 3102}
3071 3103
3072static INLINE void 3104ecb_inline void
3073dump_stack_initialize (SCHEME_P) 3105dump_stack_initialize (SCHEME_P)
3074{ 3106{
3075 SCHEME_V->dump_size = 0; 3107 SCHEME_V->dump_size = 0;
3076 SCHEME_V->dump_base = 0; 3108 SCHEME_V->dump_base = 0;
3077 dump_stack_reset (SCHEME_A); 3109 dump_stack_reset (SCHEME_A);
3130 int i = 0; 3162 int i = 0;
3131 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3163 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3132 3164
3133 while (cont != NIL) 3165 while (cont != NIL)
3134 { 3166 {
3135 frame->op = ivalue (car (cont)); cont = cdr (cont); 3167 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3136 frame->args = car (cont) ; cont = cdr (cont); 3168 frame->args = car (cont) ; cont = cdr (cont);
3137 frame->envir = car (cont) ; cont = cdr (cont); 3169 frame->envir = car (cont) ; cont = cdr (cont);
3138 frame->code = car (cont) ; cont = cdr (cont); 3170 frame->code = car (cont) ; cont = cdr (cont);
3139 3171
3140 ++frame; 3172 ++frame;
3141 ++i; 3173 ++i;
3142 } 3174 }
3143 3175
3144 SCHEME_V->dump = (pointer)(uintptr_t)i; 3176 SCHEME_V->dump = (pointer)(uintptr_t)i;
3145} 3177}
3146 3178
3147#else 3179#else
3148 3180
3149static INLINE void 3181ecb_inline void
3150dump_stack_reset (SCHEME_P) 3182dump_stack_reset (SCHEME_P)
3151{ 3183{
3152 SCHEME_V->dump = NIL; 3184 SCHEME_V->dump = NIL;
3153} 3185}
3154 3186
3155static INLINE void 3187ecb_inline void
3156dump_stack_initialize (SCHEME_P) 3188dump_stack_initialize (SCHEME_P)
3157{ 3189{
3158 dump_stack_reset (SCHEME_A); 3190 dump_stack_reset (SCHEME_A);
3159} 3191}
3160 3192
3162dump_stack_free (SCHEME_P) 3194dump_stack_free (SCHEME_P)
3163{ 3195{
3164 SCHEME_V->dump = NIL; 3196 SCHEME_V->dump = NIL;
3165} 3197}
3166 3198
3167static pointer 3199static int
3168xs_return (SCHEME_P_ pointer a) 3200xs_return (SCHEME_P_ pointer a)
3169{ 3201{
3170 pointer dump = SCHEME_V->dump; 3202 pointer dump = SCHEME_V->dump;
3171 3203
3172 SCHEME_V->value = a; 3204 SCHEME_V->value = a;
3173 3205
3174 if (dump == NIL) 3206 if (dump == NIL)
3175 return NIL; 3207 return -1;
3176 3208
3177 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3209 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3178 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3210 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3179 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3211 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3180 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3212 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3181 3213
3182 SCHEME_V->dump = dump; 3214 SCHEME_V->dump = dump;
3183 3215
3184 return S_T; 3216 return 0;
3185} 3217}
3186 3218
3187static void 3219static void
3188s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3220s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3189{ 3221{
3214 3246
3215#endif 3247#endif
3216 3248
3217#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3249#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3218 3250
3219static pointer 3251static int
3220opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3252opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3221{ 3253{
3222 pointer args = SCHEME_V->args; 3254 pointer args = SCHEME_V->args;
3223 pointer x, y; 3255 pointer x, y;
3224 3256
3391 3423
3392 case OP_TRACING: 3424 case OP_TRACING:
3393 { 3425 {
3394 int tr = SCHEME_V->tracing; 3426 int tr = SCHEME_V->tracing;
3395 3427
3396 SCHEME_V->tracing = ivalue (car (args)); 3428 SCHEME_V->tracing = ivalue_unchecked (car (args));
3397 s_return (mk_integer (SCHEME_A_ tr)); 3429 s_return (mk_integer (SCHEME_A_ tr));
3398 } 3430 }
3399 3431
3400#endif 3432#endif
3401 3433
3413 /* fall through */ 3445 /* fall through */
3414 3446
3415 case OP_REAL_APPLY: 3447 case OP_REAL_APPLY:
3416#endif 3448#endif
3417 if (is_proc (SCHEME_V->code)) 3449 if (is_proc (SCHEME_V->code))
3418 {
3419 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3450 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3420 }
3421 else if (is_foreign (SCHEME_V->code)) 3451 else if (is_foreign (SCHEME_V->code))
3422 { 3452 {
3423 /* Keep nested calls from GC'ing the arglist */ 3453 /* Keep nested calls from GC'ing the arglist */
3424 push_recent_alloc (SCHEME_A_ args, NIL); 3454 push_recent_alloc (SCHEME_A_ args, NIL);
3425 x = SCHEME_V->code->object.ff (SCHEME_A_ args); 3455 x = SCHEME_V->code->object.ff (SCHEME_A_ args);
3592 3622
3593 case OP_IF1: /* if */ 3623 case OP_IF1: /* if */
3594 if (is_true (SCHEME_V->value)) 3624 if (is_true (SCHEME_V->value))
3595 SCHEME_V->code = car (SCHEME_V->code); 3625 SCHEME_V->code = car (SCHEME_V->code);
3596 else 3626 else
3597 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because 3627 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
3598
3599 * car(NIL) = NIL */
3600 s_goto (OP_EVAL); 3628 s_goto (OP_EVAL);
3601 3629
3602 case OP_LET0: /* let */ 3630 case OP_LET0: /* let */
3603 SCHEME_V->args = NIL; 3631 SCHEME_V->args = NIL;
3604 SCHEME_V->value = SCHEME_V->code; 3632 SCHEME_V->value = SCHEME_V->code;
3914 SCHEME_V->code = car (args); 3942 SCHEME_V->code = car (args);
3915 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); 3943 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
3916 s_goto (OP_APPLY); 3944 s_goto (OP_APPLY);
3917 } 3945 }
3918 3946
3919 abort (); 3947 if (USE_ERROR_CHECKING) abort ();
3920} 3948}
3921 3949
3922static pointer 3950static int
3923opexe_2 (SCHEME_P_ enum scheme_opcodes op) 3951opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3924{ 3952{
3925 pointer args = SCHEME_V->args; 3953 pointer args = SCHEME_V->args;
3926 pointer x = car (args); 3954 pointer x = car (args);
3927 num v; 3955 num v;
3928 3956
3929#if USE_MATH
3930 RVALUE dd;
3931#endif
3932
3933 switch (op) 3957 switch (op)
3934 { 3958 {
3935#if USE_MATH 3959#if USE_MATH
3936 case OP_INEX2EX: /* inexact->exact */ 3960 case OP_INEX2EX: /* inexact->exact */
3961 {
3937 if (num_is_integer (x)) 3962 if (is_integer (x))
3938 s_return (x); 3963 s_return (x);
3939 else if (modf (rvalue_unchecked (x), &dd) == 0) 3964
3965 RVALUE r = rvalue_unchecked (x);
3966
3967 if (r == (RVALUE)(IVALUE)r)
3940 s_return (mk_integer (SCHEME_A_ ivalue (x))); 3968 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3941 else 3969 else
3942 Error_1 ("inexact->exact: not integral:", x); 3970 Error_1 ("inexact->exact: not integral:", x);
3971 }
3943 3972
3944 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 3973 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)))); 3974 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)))); 3975 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)))); 3976 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3965 { 3994 {
3966 RVALUE result; 3995 RVALUE result;
3967 int real_result = 1; 3996 int real_result = 1;
3968 pointer y = cadr (args); 3997 pointer y = cadr (args);
3969 3998
3970 if (num_is_integer (x) && num_is_integer (y)) 3999 if (is_integer (x) && is_integer (y))
3971 real_result = 0; 4000 real_result = 0;
3972 4001
3973 /* This 'if' is an R5RS compatibility fix. */ 4002 /* This 'if' is an R5RS compatibility fix. */
3974 /* NOTE: Remove this 'if' fix for R6RS. */ 4003 /* NOTE: Remove this 'if' fix for R6RS. */
3975 if (rvalue (x) == 0 && rvalue (y) < 0) 4004 if (rvalue (x) == 0 && rvalue (y) < 0)
3981 /* If the test fails, result is too big for integer. */ 4010 /* If the test fails, result is too big for integer. */
3982 if (!real_result) 4011 if (!real_result)
3983 { 4012 {
3984 long result_as_long = result; 4013 long result_as_long = result;
3985 4014
3986 if (result != (RVALUE) result_as_long) 4015 if (result != result_as_long)
3987 real_result = 1; 4016 real_result = 1;
3988 } 4017 }
3989 4018
3990 if (real_result) 4019 if (real_result)
3991 s_return (mk_real (SCHEME_A_ result)); 4020 s_return (mk_real (SCHEME_A_ result));
3996 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4025 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)))); 4026 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
3998 4027
3999 case OP_TRUNCATE: 4028 case OP_TRUNCATE:
4000 { 4029 {
4001 RVALUE rvalue_of_x; 4030 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))); 4031 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 } 4032 }
4010 4033
4011 case OP_ROUND: 4034 case OP_ROUND:
4012 if (num_is_integer (x)) 4035 if (is_integer (x))
4013 s_return (x); 4036 s_return (x);
4014 4037
4015 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4038 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4016#endif 4039#endif
4017 4040
4018 case OP_ADD: /* + */ 4041 case OP_ADD: /* + */
4019 v = num_zero; 4042 v = num_zero;
4020 4043
4021 for (x = args; x != NIL; x = cdr (x)) 4044 for (x = args; x != NIL; x = cdr (x))
4022 v = num_op ('+', v, nvalue (car (x))); 4045 v = num_op (NUM_ADD, v, nvalue (car (x)));
4023 4046
4024 s_return (mk_number (SCHEME_A_ v)); 4047 s_return (mk_number (SCHEME_A_ v));
4025 4048
4026 case OP_MUL: /* * */ 4049 case OP_MUL: /* * */
4027 v = num_one; 4050 v = num_one;
4028 4051
4029 for (x = args; x != NIL; x = cdr (x)) 4052 for (x = args; x != NIL; x = cdr (x))
4030 v = num_op ('+', v, nvalue (car (x))); 4053 v = num_op (NUM_MUL, v, nvalue (car (x)));
4031 4054
4032 s_return (mk_number (SCHEME_A_ v)); 4055 s_return (mk_number (SCHEME_A_ v));
4033 4056
4034 case OP_SUB: /* - */ 4057 case OP_SUB: /* - */
4035 if (cdr (args) == NIL) 4058 if (cdr (args) == NIL)
4042 x = cdr (args); 4065 x = cdr (args);
4043 v = nvalue (car (args)); 4066 v = nvalue (car (args));
4044 } 4067 }
4045 4068
4046 for (; x != NIL; x = cdr (x)) 4069 for (; x != NIL; x = cdr (x))
4047 v = num_op ('+', v, nvalue (car (x))); 4070 v = num_op (NUM_SUB, v, nvalue (car (x)));
4048 4071
4049 s_return (mk_number (SCHEME_A_ v)); 4072 s_return (mk_number (SCHEME_A_ v));
4050 4073
4051 case OP_DIV: /* / */ 4074 case OP_DIV: /* / */
4052 if (cdr (args) == NIL) 4075 if (cdr (args) == NIL)
4059 x = cdr (args); 4082 x = cdr (args);
4060 v = nvalue (car (args)); 4083 v = nvalue (car (args));
4061 } 4084 }
4062 4085
4063 for (; x != NIL; x = cdr (x)) 4086 for (; x != NIL; x = cdr (x))
4064 {
4065 if (!is_zero_rvalue (rvalue (car (x)))) 4087 if (!is_zero_rvalue (rvalue (car (x))))
4066 v = num_div (v, nvalue (car (x))); 4088 v = num_div (v, nvalue (car (x)));
4067 else 4089 else
4068 Error_0 ("/: division by zero"); 4090 Error_0 ("/: division by zero");
4069 }
4070 4091
4071 s_return (mk_number (SCHEME_A_ v)); 4092 s_return (mk_number (SCHEME_A_ v));
4072 4093
4073 case OP_INTDIV: /* quotient */ 4094 case OP_INTDIV: /* quotient */
4074 if (cdr (args) == NIL) 4095 if (cdr (args) == NIL)
4083 } 4104 }
4084 4105
4085 for (; x != NIL; x = cdr (x)) 4106 for (; x != NIL; x = cdr (x))
4086 { 4107 {
4087 if (ivalue (car (x)) != 0) 4108 if (ivalue (car (x)) != 0)
4088 v = num_op ('/', v, nvalue (car (x))); 4109 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4089 else 4110 else
4090 Error_0 ("quotient: division by zero"); 4111 Error_0 ("quotient: division by zero");
4091 } 4112 }
4092 4113
4093 s_return (mk_number (SCHEME_A_ v)); 4114 s_return (mk_number (SCHEME_A_ v));
4139 } 4160 }
4140 else 4161 else
4141 Error_0 ("set-cdr!: unable to alter immutable pair"); 4162 Error_0 ("set-cdr!: unable to alter immutable pair");
4142 4163
4143 case OP_CHAR2INT: /* char->integer */ 4164 case OP_CHAR2INT: /* char->integer */
4144 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4165 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4145 4166
4146 case OP_INT2CHAR: /* integer->char */ 4167 case OP_INT2CHAR: /* integer->char */
4147 s_return (mk_character (SCHEME_A_ ivalue (x))); 4168 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4148 4169
4149 case OP_CHARUPCASE: 4170 case OP_CHARUPCASE:
4150 { 4171 {
4151 unsigned char c = ivalue (x); 4172 unsigned char c = ivalue_unchecked (x);
4152 c = toupper (c); 4173 c = toupper (c);
4153 s_return (mk_character (SCHEME_A_ c)); 4174 s_return (mk_character (SCHEME_A_ c));
4154 } 4175 }
4155 4176
4156 case OP_CHARDNCASE: 4177 case OP_CHARDNCASE:
4157 { 4178 {
4158 unsigned char c = ivalue (x); 4179 unsigned char c = ivalue_unchecked (x);
4159 c = tolower (c); 4180 c = tolower (c);
4160 s_return (mk_character (SCHEME_A_ c)); 4181 s_return (mk_character (SCHEME_A_ c));
4161 } 4182 }
4162 4183
4163 case OP_STR2SYM: /* string->symbol */ 4184 case OP_STR2SYM: /* string->symbol */
4240 Error_1 ("atom->string: not an atom:", x); 4261 Error_1 ("atom->string: not an atom:", x);
4241 } 4262 }
4242 4263
4243 case OP_MKSTRING: /* make-string */ 4264 case OP_MKSTRING: /* make-string */
4244 { 4265 {
4245 int fill = ' '; 4266 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4246 int len;
4247
4248 len = ivalue (x); 4267 int len = ivalue_unchecked (x);
4249
4250 if (cdr (args) != NIL)
4251 fill = charvalue (cadr (args));
4252 4268
4253 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4269 s_return (mk_empty_string (SCHEME_A_ len, fill));
4254 } 4270 }
4255 4271
4256 case OP_STRLEN: /* string-length */ 4272 case OP_STRLEN: /* string-length */
4257 s_return (mk_integer (SCHEME_A_ strlength (x))); 4273 s_return (mk_integer (SCHEME_A_ strlength (x)));
4258 4274
4259 case OP_STRREF: /* string-ref */ 4275 case OP_STRREF: /* string-ref */
4260 { 4276 {
4261 char *str;
4262 int index;
4263
4264 str = strvalue (x); 4277 char *str = strvalue (x);
4265
4266 index = ivalue (cadr (args)); 4278 int index = ivalue_unchecked (cadr (args));
4267 4279
4268 if (index >= strlength (x)) 4280 if (index >= strlength (x))
4269 Error_1 ("string-ref: out of bounds:", cadr (args)); 4281 Error_1 ("string-ref: out of bounds:", cadr (args));
4270 4282
4271 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4283 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4272 } 4284 }
4273 4285
4274 case OP_STRSET: /* string-set! */ 4286 case OP_STRSET: /* string-set! */
4275 { 4287 {
4276 char *str; 4288 char *str = strvalue (x);
4277 int index; 4289 int index = ivalue_unchecked (cadr (args));
4278 int c; 4290 int c;
4279 4291
4280 if (is_immutable (x)) 4292 if (is_immutable (x))
4281 Error_1 ("string-set!: unable to alter immutable string:", x); 4293 Error_1 ("string-set!: unable to alter immutable string:", x);
4282
4283 str = strvalue (x);
4284
4285 index = ivalue (cadr (args));
4286 4294
4287 if (index >= strlength (x)) 4295 if (index >= strlength (x))
4288 Error_1 ("string-set!: out of bounds:", cadr (args)); 4296 Error_1 ("string-set!: out of bounds:", cadr (args));
4289 4297
4290 c = charvalue (caddr (args)); 4298 c = charvalue (caddr (args));
4313 s_return (newstr); 4321 s_return (newstr);
4314 } 4322 }
4315 4323
4316 case OP_SUBSTR: /* substring */ 4324 case OP_SUBSTR: /* substring */
4317 { 4325 {
4318 char *str; 4326 char *str = strvalue (x);
4319 int index0; 4327 int index0 = ivalue_unchecked (cadr (args));
4320 int index1; 4328 int index1;
4321 int len; 4329 int len;
4322 4330
4323 str = strvalue (x);
4324
4325 index0 = ivalue (cadr (args));
4326
4327 if (index0 > strlength (x)) 4331 if (index0 > strlength (x))
4328 Error_1 ("substring: start out of bounds:", cadr (args)); 4332 Error_1 ("substring: start out of bounds:", cadr (args));
4329 4333
4330 if (cddr (args) != NIL) 4334 if (cddr (args) != NIL)
4331 { 4335 {
4332 index1 = ivalue (caddr (args)); 4336 index1 = ivalue_unchecked (caddr (args));
4333 4337
4334 if (index1 > strlength (x) || index1 < index0) 4338 if (index1 > strlength (x) || index1 < index0)
4335 Error_1 ("substring: end out of bounds:", caddr (args)); 4339 Error_1 ("substring: end out of bounds:", caddr (args));
4336 } 4340 }
4337 else 4341 else
4360 if (SCHEME_V->no_memory) 4364 if (SCHEME_V->no_memory)
4361 s_return (S_SINK); 4365 s_return (S_SINK);
4362#endif 4366#endif
4363 4367
4364 for (x = args, i = 0; is_pair (x); x = cdr (x), i++) 4368 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4365 set_vector_elem (vec, i, car (x)); 4369 vector_set (vec, i, car (x));
4366 4370
4367 s_return (vec); 4371 s_return (vec);
4368 } 4372 }
4369 4373
4370 case OP_MKVECTOR: /* make-vector */ 4374 case OP_MKVECTOR: /* make-vector */
4371 { 4375 {
4372 pointer fill = NIL; 4376 pointer fill = NIL;
4373 int len;
4374 pointer vec; 4377 pointer vec;
4375
4376 len = ivalue (x); 4378 int len = ivalue_unchecked (x);
4377 4379
4378 if (cdr (args) != NIL) 4380 if (cdr (args) != NIL)
4379 fill = cadr (args); 4381 fill = cadr (args);
4380 4382
4381 vec = mk_vector (SCHEME_A_ len); 4383 vec = mk_vector (SCHEME_A_ len);
4384 if (SCHEME_V->no_memory) 4386 if (SCHEME_V->no_memory)
4385 s_return (S_SINK); 4387 s_return (S_SINK);
4386#endif 4388#endif
4387 4389
4388 if (fill != NIL) 4390 if (fill != NIL)
4389 fill_vector (vec, fill); 4391 fill_vector (vec, 0, fill);
4390 4392
4391 s_return (vec); 4393 s_return (vec);
4392 } 4394 }
4393 4395
4394 case OP_VECLEN: /* vector-length */ 4396 case OP_VECLEN: /* vector-length */
4395 s_return (mk_integer (SCHEME_A_ veclength (x))); 4397 s_return (mk_integer (SCHEME_A_ veclength (x)));
4396 4398
4397 case OP_VECREF: /* vector-ref */ 4399 case OP_VECREF: /* vector-ref */
4398 { 4400 {
4399 int index;
4400
4401 index = ivalue (cadr (args)); 4401 int index = ivalue_unchecked (cadr (args));
4402 4402
4403 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4403 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4404 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4404 Error_1 ("vector-ref: out of bounds:", cadr (args));
4405 4405
4406 s_return (vector_elem (x, index)); 4406 s_return (vector_get (x, index));
4407 } 4407 }
4408 4408
4409 case OP_VECSET: /* vector-set! */ 4409 case OP_VECSET: /* vector-set! */
4410 { 4410 {
4411 int index; 4411 int index = ivalue_unchecked (cadr (args));
4412 4412
4413 if (is_immutable (x)) 4413 if (is_immutable (x))
4414 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4414 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4415 4415
4416 index = ivalue (cadr (args));
4417
4418 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4416 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4419 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4417 Error_1 ("vector-set!: out of bounds:", cadr (args));
4420 4418
4421 set_vector_elem (x, index, caddr (args)); 4419 vector_set (x, index, caddr (args));
4422 s_return (x); 4420 s_return (x);
4423 } 4421 }
4424 } 4422 }
4425 4423
4426 return S_T; 4424 if (USE_ERROR_CHECKING) abort ();
4427} 4425}
4428 4426
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 4427static int
4484opexe_r (SCHEME_P_ enum scheme_opcodes op) 4428opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4485{ 4429{
4486 pointer x = SCHEME_V->args; 4430 pointer x = SCHEME_V->args;
4487 4431
4488 for (;;) 4432 for (;;)
4489 { 4433 {
4509 } 4453 }
4510 4454
4511 s_return (S_T); 4455 s_return (S_T);
4512} 4456}
4513 4457
4514static pointer 4458static int
4515opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4459opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4516{ 4460{
4517 pointer args = SCHEME_V->args; 4461 pointer args = SCHEME_V->args;
4518 pointer a = car (args); 4462 pointer a = car (args);
4519 pointer d = cdr (args); 4463 pointer d = cdr (args);
4531 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4475 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4532 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4476 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4533 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4477 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4534 4478
4535#if USE_CHAR_CLASSIFIERS 4479#if USE_CHAR_CLASSIFIERS
4536 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4480 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4537 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4481 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4538 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4482 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4539 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4483 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4540 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4484 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4541#endif 4485#endif
4542 4486
4543#if USE_PORTS 4487#if USE_PORTS
4544 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4488 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4545 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4489 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4565 } 4509 }
4566 4510
4567 s_retbool (r); 4511 s_retbool (r);
4568} 4512}
4569 4513
4570static pointer 4514static int
4571opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4515opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4572{ 4516{
4573 pointer args = SCHEME_V->args; 4517 pointer args = SCHEME_V->args;
4574 pointer a = car (args); 4518 pointer a = car (args);
4575 pointer x, y; 4519 pointer x, y;
4661 putstr (SCHEME_A_ "\n"); 4605 putstr (SCHEME_A_ "\n");
4662 4606
4663 if (SCHEME_V->interactive_repl) 4607 if (SCHEME_V->interactive_repl)
4664 s_goto (OP_T0LVL); 4608 s_goto (OP_T0LVL);
4665 else 4609 else
4666 return NIL; 4610 return -1;
4667 } 4611 }
4668 4612
4669 case OP_REVERSE: /* reverse */ 4613 case OP_REVERSE: /* reverse */
4670 s_return (reverse (SCHEME_A_ a)); 4614 s_return (reverse (SCHEME_A_ a));
4671 4615
4728 4672
4729 case OP_QUIT: /* quit */ 4673 case OP_QUIT: /* quit */
4730 if (is_pair (args)) 4674 if (is_pair (args))
4731 SCHEME_V->retcode = ivalue (a); 4675 SCHEME_V->retcode = ivalue (a);
4732 4676
4733 return NIL; 4677 return -1;
4734 4678
4735 case OP_GC: /* gc */ 4679 case OP_GC: /* gc */
4736 gc (SCHEME_A_ NIL, NIL); 4680 gc (SCHEME_A_ NIL, NIL);
4737 s_return (S_T); 4681 s_return (S_T);
4738 4682
4746 4690
4747 case OP_NEWSEGMENT: /* new-segment */ 4691 case OP_NEWSEGMENT: /* new-segment */
4748 if (!is_pair (args) || !is_number (a)) 4692 if (!is_pair (args) || !is_number (a))
4749 Error_0 ("new-segment: argument must be a number"); 4693 Error_0 ("new-segment: argument must be a number");
4750 4694
4751 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 4695 alloc_cellseg (SCHEME_A_ ivalue (a));
4752 4696
4753 s_return (S_T); 4697 s_return (S_T);
4754 4698
4755 case OP_OBLIST: /* oblist */ 4699 case OP_OBLIST: /* oblist */
4756 s_return (oblist_all_symbols (SCHEME_A)); 4700 s_return (oblist_all_symbols (SCHEME_A));
4785 break; 4729 break;
4786 } 4730 }
4787 4731
4788 p = port_from_filename (SCHEME_A_ strvalue (a), prop); 4732 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4789 4733
4790 if (p == NIL) 4734 s_return (p == NIL ? S_F : p);
4791 s_return (S_F);
4792
4793 s_return (p);
4794 } 4735 }
4795 4736
4796# if USE_STRING_PORTS 4737# if USE_STRING_PORTS
4797 4738
4798 case OP_OPEN_INSTRING: /* open-input-string */ 4739 case OP_OPEN_INSTRING: /* open-input-string */
4813 } 4754 }
4814 4755
4815 p = port_from_string (SCHEME_A_ strvalue (a), 4756 p = port_from_string (SCHEME_A_ strvalue (a),
4816 strvalue (a) + strlength (a), prop); 4757 strvalue (a) + strlength (a), prop);
4817 4758
4818 if (p == NIL) 4759 s_return (p == NIL ? S_F : p);
4819 s_return (S_F);
4820
4821 s_return (p);
4822 } 4760 }
4823 4761
4824 case OP_OPEN_OUTSTRING: /* open-output-string */ 4762 case OP_OPEN_OUTSTRING: /* open-output-string */
4825 { 4763 {
4826 pointer p; 4764 pointer p;
4827 4765
4828 if (a == NIL) 4766 if (a == NIL)
4829 {
4830 p = port_from_scratch (SCHEME_A); 4767 p = port_from_scratch (SCHEME_A);
4831
4832 if (p == NIL)
4833 s_return (S_F);
4834 }
4835 else 4768 else
4836 {
4837 p = port_from_string (SCHEME_A_ strvalue (a), 4769 p = port_from_string (SCHEME_A_ strvalue (a),
4838 strvalue (a) + strlength (a), port_output); 4770 strvalue (a) + strlength (a), port_output);
4839 4771
4840 if (p == NIL) 4772 s_return (p == NIL ? S_F : p);
4841 s_return (S_F);
4842 }
4843
4844 s_return (p);
4845 } 4773 }
4846 4774
4847 case OP_GET_OUTSTRING: /* get-output-string */ 4775 case OP_GET_OUTSTRING: /* get-output-string */
4848 { 4776 {
4849 port *p; 4777 port *p;
4888 case OP_CURR_ENV: /* current-environment */ 4816 case OP_CURR_ENV: /* current-environment */
4889 s_return (SCHEME_V->envir); 4817 s_return (SCHEME_V->envir);
4890 4818
4891 } 4819 }
4892 4820
4893 abort (); 4821 if (USE_ERROR_CHECKING) abort ();
4894} 4822}
4895 4823
4896static pointer 4824static int
4897opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4825opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4898{ 4826{
4827 pointer args = SCHEME_V->args;
4899 pointer x; 4828 pointer x;
4900 4829
4901 if (SCHEME_V->nesting != 0) 4830 if (SCHEME_V->nesting != 0)
4902 { 4831 {
4903 int n = SCHEME_V->nesting; 4832 int n = SCHEME_V->nesting;
4910 switch (op) 4839 switch (op)
4911 { 4840 {
4912 /* ========== reading part ========== */ 4841 /* ========== reading part ========== */
4913#if USE_PORTS 4842#if USE_PORTS
4914 case OP_READ: 4843 case OP_READ:
4915 if (!is_pair (SCHEME_V->args)) 4844 if (!is_pair (args))
4916 s_goto (OP_READ_INTERNAL); 4845 s_goto (OP_READ_INTERNAL);
4917 4846
4918 if (!is_inport (car (SCHEME_V->args))) 4847 if (!is_inport (car (args)))
4919 Error_1 ("read: not an input port:", car (SCHEME_V->args)); 4848 Error_1 ("read: not an input port:", car (args));
4920 4849
4921 if (car (SCHEME_V->args) == SCHEME_V->inport) 4850 if (car (args) == SCHEME_V->inport)
4922 s_goto (OP_READ_INTERNAL); 4851 s_goto (OP_READ_INTERNAL);
4923 4852
4924 x = SCHEME_V->inport; 4853 x = SCHEME_V->inport;
4925 SCHEME_V->inport = car (SCHEME_V->args); 4854 SCHEME_V->inport = car (args);
4926 x = cons (x, NIL); 4855 x = cons (x, NIL);
4927 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4856 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
4928 s_goto (OP_READ_INTERNAL); 4857 s_goto (OP_READ_INTERNAL);
4929 4858
4930 case OP_READ_CHAR: /* read-char */ 4859 case OP_READ_CHAR: /* read-char */
4931 case OP_PEEK_CHAR: /* peek-char */ 4860 case OP_PEEK_CHAR: /* peek-char */
4932 { 4861 {
4933 int c; 4862 int c;
4934 4863
4935 if (is_pair (SCHEME_V->args)) 4864 if (is_pair (args))
4936 { 4865 {
4937 if (car (SCHEME_V->args) != SCHEME_V->inport) 4866 if (car (args) != SCHEME_V->inport)
4938 { 4867 {
4939 x = SCHEME_V->inport; 4868 x = SCHEME_V->inport;
4940 x = cons (x, NIL); 4869 x = cons (x, NIL);
4941 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4870 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
4942 SCHEME_V->inport = car (SCHEME_V->args); 4871 SCHEME_V->inport = car (args);
4943 } 4872 }
4944 } 4873 }
4945 4874
4946 c = inchar (SCHEME_A); 4875 c = inchar (SCHEME_A);
4947 4876
4957 case OP_CHAR_READY: /* char-ready? */ 4886 case OP_CHAR_READY: /* char-ready? */
4958 { 4887 {
4959 pointer p = SCHEME_V->inport; 4888 pointer p = SCHEME_V->inport;
4960 int res; 4889 int res;
4961 4890
4962 if (is_pair (SCHEME_V->args)) 4891 if (is_pair (args))
4963 p = car (SCHEME_V->args); 4892 p = car (args);
4964 4893
4965 res = p->object.port->kind & port_string; 4894 res = p->object.port->kind & port_string;
4966 4895
4967 s_retbool (res); 4896 s_retbool (res);
4968 } 4897 }
4969 4898
4970 case OP_SET_INPORT: /* set-input-port */ 4899 case OP_SET_INPORT: /* set-input-port */
4971 SCHEME_V->inport = car (SCHEME_V->args); 4900 SCHEME_V->inport = car (args);
4972 s_return (SCHEME_V->value); 4901 s_return (SCHEME_V->value);
4973 4902
4974 case OP_SET_OUTPORT: /* set-output-port */ 4903 case OP_SET_OUTPORT: /* set-output-port */
4975 SCHEME_V->outport = car (SCHEME_V->args); 4904 SCHEME_V->outport = car (args);
4976 s_return (SCHEME_V->value); 4905 s_return (SCHEME_V->value);
4977#endif 4906#endif
4978 4907
4979 case OP_RDSEXPR: 4908 case OP_RDSEXPR:
4980 switch (SCHEME_V->tok) 4909 switch (SCHEME_V->tok)
5066 } 4995 }
5067 4996
5068 break; 4997 break;
5069 4998
5070 case OP_RDLIST: 4999 case OP_RDLIST:
5071 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 5000 SCHEME_V->args = cons (SCHEME_V->value, args);
5072 SCHEME_V->tok = token (SCHEME_A); 5001 SCHEME_V->tok = token (SCHEME_A);
5073 5002
5074 switch (SCHEME_V->tok) 5003 switch (SCHEME_V->tok)
5075 { 5004 {
5076 case TOK_EOF: 5005 case TOK_EOF:
5104 case OP_RDDOT: 5033 case OP_RDDOT:
5105 if (token (SCHEME_A) != TOK_RPAREN) 5034 if (token (SCHEME_A) != TOK_RPAREN)
5106 Error_0 ("syntax error: illegal dot expression"); 5035 Error_0 ("syntax error: illegal dot expression");
5107 5036
5108 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5037 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5109 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); 5038 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, args));
5110 5039
5111 case OP_RDQUOTE: 5040 case OP_RDQUOTE:
5112 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); 5041 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)));
5113 5042
5114 case OP_RDQQUOTE: 5043 case OP_RDQQUOTE:
5136 SCHEME_V->args = SCHEME_V->value; 5065 SCHEME_V->args = SCHEME_V->value;
5137 s_goto (OP_VECTOR); 5066 s_goto (OP_VECTOR);
5138 5067
5139 /* ========== printing part ========== */ 5068 /* ========== printing part ========== */
5140 case OP_P0LIST: 5069 case OP_P0LIST:
5141 if (is_vector (SCHEME_V->args)) 5070 if (is_vector (args))
5142 { 5071 {
5143 putstr (SCHEME_A_ "#("); 5072 putstr (SCHEME_A_ "#(");
5144 SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); 5073 SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0));
5145 s_goto (OP_PVECFROM); 5074 s_goto (OP_PVECFROM);
5146 } 5075 }
5147 else if (is_environment (SCHEME_V->args)) 5076 else if (is_environment (args))
5148 { 5077 {
5149 putstr (SCHEME_A_ "#<ENVIRONMENT>"); 5078 putstr (SCHEME_A_ "#<ENVIRONMENT>");
5150 s_return (S_T); 5079 s_return (S_T);
5151 } 5080 }
5152 else if (!is_pair (SCHEME_V->args)) 5081 else if (!is_pair (args))
5153 { 5082 {
5154 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5083 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5155 s_return (S_T); 5084 s_return (S_T);
5156 } 5085 }
5157 else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5086 else
5158 { 5087 {
5088 pointer a = car (args);
5089 pointer b = cdr (args);
5090 int ok_abbr = ok_abbrev (b);
5091 SCHEME_V->args = car (b);
5092
5093 if (a == SCHEME_V->QUOTE && ok_abbr)
5159 putstr (SCHEME_A_ "'"); 5094 putstr (SCHEME_A_ "'");
5160 SCHEME_V->args = cadr (SCHEME_V->args); 5095 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5096 putstr (SCHEME_A_ "`");
5097 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5098 putstr (SCHEME_A_ ",");
5099 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5100 putstr (SCHEME_A_ ",@");
5101 else
5102 {
5103 putstr (SCHEME_A_ "(");
5104 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5105 SCHEME_V->args = a;
5106 }
5107
5161 s_goto (OP_P0LIST); 5108 s_goto (OP_P0LIST);
5162 } 5109 }
5163 else if (car (SCHEME_V->args) == SCHEME_V->QQUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5110
5111 case OP_P1LIST:
5112 if (is_pair (args))
5164 { 5113 {
5114 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5165 putstr (SCHEME_A_ "`"); 5115 putstr (SCHEME_A_ " ");
5166 SCHEME_V->args = cadr (SCHEME_V->args); 5116 SCHEME_V->args = car (args);
5167 s_goto (OP_P0LIST); 5117 s_goto (OP_P0LIST);
5168 } 5118 }
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)) 5119 else if (is_vector (args))
5198 { 5120 {
5199 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); 5121 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL);
5200 putstr (SCHEME_A_ " . "); 5122 putstr (SCHEME_A_ " . ");
5201 s_goto (OP_P0LIST); 5123 s_goto (OP_P0LIST);
5202 } 5124 }
5203 else 5125 else
5204 { 5126 {
5205 if (SCHEME_V->args != NIL) 5127 if (args != NIL)
5206 { 5128 {
5207 putstr (SCHEME_A_ " . "); 5129 putstr (SCHEME_A_ " . ");
5208 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5130 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5209 } 5131 }
5210 5132
5211 putstr (SCHEME_A_ ")"); 5133 putstr (SCHEME_A_ ")");
5212 s_return (S_T); 5134 s_return (S_T);
5213 } 5135 }
5214 5136
5215 case OP_PVECFROM: 5137 case OP_PVECFROM:
5216 { 5138 {
5217 int i = ivalue_unchecked (cdr (SCHEME_V->args)); 5139 int i = ivalue_unchecked (cdr (args));
5218 pointer vec = car (SCHEME_V->args); 5140 pointer vec = car (args);
5219 int len = veclength (vec); 5141 int len = veclength (vec);
5220 5142
5221 if (i == len) 5143 if (i == len)
5222 { 5144 {
5223 putstr (SCHEME_A_ ")"); 5145 putstr (SCHEME_A_ ")");
5224 s_return (S_T); 5146 s_return (S_T);
5225 } 5147 }
5226 else 5148 else
5227 { 5149 {
5228 pointer elem = vector_elem (vec, i); 5150 pointer elem = vector_get (vec, i);
5229 5151
5230 ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; 5152 ivalue_unchecked (cdr (args)) = i + 1;
5231 s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); 5153 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5232 SCHEME_V->args = elem; 5154 SCHEME_V->args = elem;
5233 5155
5234 if (i > 0) 5156 if (i > 0)
5235 putstr (SCHEME_A_ " "); 5157 putstr (SCHEME_A_ " ");
5236 5158
5237 s_goto (OP_P0LIST); 5159 s_goto (OP_P0LIST);
5238 } 5160 }
5239 } 5161 }
5240 } 5162 }
5241 5163
5242 abort (); 5164 if (USE_ERROR_CHECKING) abort ();
5243} 5165}
5244 5166
5245static pointer 5167static int
5246opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5168opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5247{ 5169{
5170 pointer args = SCHEME_V->args;
5171 pointer a = car (args);
5248 pointer x, y; 5172 pointer x, y;
5249 5173
5250 switch (op) 5174 switch (op)
5251 { 5175 {
5252 case OP_LIST_LENGTH: /* length *//* a.k */ 5176 case OP_LIST_LENGTH: /* length *//* a.k */
5253 { 5177 {
5254 long v = list_length (SCHEME_A_ car (SCHEME_V->args)); 5178 long v = list_length (SCHEME_A_ a);
5255 5179
5256 if (v < 0) 5180 if (v < 0)
5257 Error_1 ("length: not a list:", car (SCHEME_V->args)); 5181 Error_1 ("length: not a list:", a);
5258 5182
5259 s_return (mk_integer (SCHEME_A_ v)); 5183 s_return (mk_integer (SCHEME_A_ v));
5260 } 5184 }
5261 5185
5262 case OP_ASSQ: /* assq *//* a.k */ 5186 case OP_ASSQ: /* assq *//* a.k */
5263 x = car (SCHEME_V->args); 5187 x = a;
5264 5188
5265 for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) 5189 for (y = cadr (args); is_pair (y); y = cdr (y))
5266 { 5190 {
5267 if (!is_pair (car (y))) 5191 if (!is_pair (car (y)))
5268 Error_0 ("unable to handle non pair element"); 5192 Error_0 ("unable to handle non pair element");
5269 5193
5270 if (x == caar (y)) 5194 if (x == caar (y))
5276 else 5200 else
5277 s_return (S_F); 5201 s_return (S_F);
5278 5202
5279 5203
5280 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5204 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5281 SCHEME_V->args = car (SCHEME_V->args); 5205 SCHEME_V->args = a;
5282 5206
5283 if (SCHEME_V->args == NIL) 5207 if (SCHEME_V->args == NIL)
5284 s_return (S_F); 5208 s_return (S_F);
5285 else if (is_closure (SCHEME_V->args)) 5209 else if (is_closure (SCHEME_V->args))
5286 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5210 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5292 case OP_CLOSUREP: /* closure? */ 5216 case OP_CLOSUREP: /* closure? */
5293 /* 5217 /*
5294 * Note, macro object is also a closure. 5218 * Note, macro object is also a closure.
5295 * Therefore, (closure? <#MACRO>) ==> #t 5219 * Therefore, (closure? <#MACRO>) ==> #t
5296 */ 5220 */
5297 s_retbool (is_closure (car (SCHEME_V->args))); 5221 s_retbool (is_closure (a));
5298 5222
5299 case OP_MACROP: /* macro? */ 5223 case OP_MACROP: /* macro? */
5300 s_retbool (is_macro (car (SCHEME_V->args))); 5224 s_retbool (is_macro (a));
5301 } 5225 }
5302 5226
5303 abort (); 5227 if (USE_ERROR_CHECKING) abort ();
5304} 5228}
5305 5229
5230/* 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); 5231typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5307 5232
5308typedef int (*test_predicate) (pointer); 5233typedef int (*test_predicate)(pointer);
5309static int 5234static int
5310is_any (pointer p) 5235tst_any (pointer p)
5311{ 5236{
5312 return 1; 5237 return 1;
5313} 5238}
5314 5239
5315static int 5240static int
5316is_nonneg (pointer p) 5241tst_inonneg (pointer p)
5317{ 5242{
5318 return ivalue (p) >= 0 && is_integer (p); 5243 return is_integer (p) && ivalue_unchecked (p) >= 0;
5244}
5245
5246static int
5247tst_is_list (SCHEME_P_ pointer p)
5248{
5249 return p == NIL || is_pair (p);
5319} 5250}
5320 5251
5321/* Correspond carefully with following defines! */ 5252/* Correspond carefully with following defines! */
5322static struct 5253static struct
5323{ 5254{
5324 test_predicate fct; 5255 test_predicate fct;
5325 const char *kind; 5256 const char *kind;
5326} tests[] = 5257} tests[] = {
5327{ 5258 { tst_any , 0 },
5328 { 0, 0}, /* unused */ 5259 { is_string , "string" },
5329 { is_any, 0}, 5260 { is_symbol , "symbol" },
5330 { is_string, "string" }, 5261 { is_port , "port" },
5331 { is_symbol, "symbol" },
5332 { is_port, "port" },
5333 { is_inport, "input port" }, 5262 { is_inport , "input port" },
5334 { is_outport, "output port" }, 5263 { is_outport , "output port" },
5335 { is_environment, "environment" }, 5264 { is_environment, "environment" },
5336 { is_pair, "pair" }, 5265 { is_pair , "pair" },
5337 { 0, "pair or '()" }, 5266 { 0 , "pair or '()" },
5338 { is_character, "character" }, 5267 { is_character , "character" },
5339 { is_vector, "vector" }, 5268 { is_vector , "vector" },
5340 { is_number, "number" }, 5269 { is_number , "number" },
5341 { is_integer, "integer" }, 5270 { is_integer , "integer" },
5342 { is_nonneg, "non-negative integer" } 5271 { tst_inonneg , "non-negative integer" }
5343}; 5272};
5344 5273
5345#define TST_NONE 0 5274#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5346#define TST_ANY "\001" 5275#define TST_ANY "\001"
5347#define TST_STRING "\002" 5276#define TST_STRING "\002"
5348#define TST_SYMBOL "\003" 5277#define TST_SYMBOL "\003"
5349#define TST_PORT "\004" 5278#define TST_PORT "\004"
5350#define TST_INPORT "\005" 5279#define TST_INPORT "\005"
5351#define TST_OUTPORT "\006" 5280#define TST_OUTPORT "\006"
5352#define TST_ENVIRONMENT "\007" 5281#define TST_ENVIRONMENT "\007"
5353#define TST_PAIR "\010" 5282#define TST_PAIR "\010"
5354#define TST_LIST "\011" 5283#define TST_LIST "\011"
5355#define TST_CHAR "\012" 5284#define TST_CHAR "\012"
5356#define TST_VECTOR "\013" 5285#define TST_VECTOR "\013"
5357#define TST_NUMBER "\014" 5286#define TST_NUMBER "\014"
5358#define TST_INTEGER "\015" 5287#define TST_INTEGER "\015"
5359#define TST_NATURAL "\016" 5288#define TST_NATURAL "\016"
5289
5290#define INF_ARG 0xff
5291#define UNNAMED_OP ""
5292
5293static const char opnames[] =
5294#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5295#include "opdefines.h"
5296#undef OP_DEF
5297;
5298
5299static const char *
5300opname (int idx)
5301{
5302 const char *name = opnames;
5303
5304 /* should do this at compile time, but would require external program, right? */
5305 while (idx--)
5306 name += strlen (name) + 1;
5307
5308 return *name ? name : "ILLEGAL";
5309}
5310
5311static const char *
5312procname (pointer x)
5313{
5314 return opname (procnum (x));
5315}
5360 5316
5361typedef struct 5317typedef struct
5362{ 5318{
5363 dispatch_func func; 5319 uint8_t func;
5364 char *name; 5320 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5321 uint8_t builtin;
5322#if USE_ERROR_CHECKING
5365 int min_arity; 5323 uint8_t min_arity;
5366 int max_arity; 5324 uint8_t max_arity;
5367 char *arg_tests_encoding; 5325 char arg_tests_encoding[3];
5326#endif
5368} op_code_info; 5327} op_code_info;
5369 5328
5370#define INF_ARG 0xffff
5371
5372static op_code_info dispatch_table[] = { 5329static const op_code_info dispatch_table[] = {
5373#define OP_DEF(A,B,C,D,E,OP) {A,B,C,D,E}, 5330#if USE_ERROR_CHECKING
5331#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5332#else
5333#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5334#endif
5374#include "opdefines.h" 5335#include "opdefines.h"
5336#undef OP_DEF
5375 {0} 5337 {0}
5376}; 5338};
5377 5339
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 */ 5340/* kernel of this interpreter */
5391static void 5341static void ecb_hot
5392Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5342Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5393{ 5343{
5394 SCHEME_V->op = op; 5344 SCHEME_V->op = op;
5395 5345
5396 for (;;) 5346 for (;;)
5397 { 5347 {
5398 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5348 const op_code_info *pcd = dispatch_table + SCHEME_V->op;
5399 5349
5400#if USE_ERROR_CHECKING 5350#if USE_ERROR_CHECKING
5401 if (pcd->name) /* if built-in function, check arguments */ 5351 if (pcd->builtin) /* if built-in function, check arguments */
5402 { 5352 {
5403 int ok = 1;
5404 char msg[STRBUFFSIZE]; 5353 char msg[STRBUFFSIZE];
5405 int n = list_length (SCHEME_A_ SCHEME_V->args); 5354 int n = list_length (SCHEME_A_ SCHEME_V->args);
5406 5355
5407 /* Check number of arguments */ 5356 /* Check number of arguments */
5408 if (ecb_expect_false (n < pcd->min_arity)) 5357 if (ecb_expect_false (n < pcd->min_arity))
5409 { 5358 {
5410 ok = 0;
5411 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5359 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5412 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); 5360 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity);
5361 xError_1 (SCHEME_A_ msg, 0);
5362 continue;
5413 } 5363 }
5414 else if (ecb_expect_false (n > pcd->max_arity)) 5364 else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG))
5415 { 5365 {
5416 ok = 0;
5417 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5366 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5418 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); 5367 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity);
5368 xError_1 (SCHEME_A_ msg, 0);
5369 continue;
5419 } 5370 }
5420 5371 else
5421 if (ecb_expect_false (ok))
5422 { 5372 {
5423 if (pcd->arg_tests_encoding) 5373 if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */
5424 { 5374 {
5425 int i = 0; 5375 int i = 0;
5426 int j; 5376 int j;
5427 const char *t = pcd->arg_tests_encoding; 5377 const char *t = pcd->arg_tests_encoding;
5428 pointer arglist = SCHEME_V->args; 5378 pointer arglist = SCHEME_V->args;
5429 5379
5430 do 5380 do
5431 { 5381 {
5432 pointer arg = car (arglist); 5382 pointer arg = car (arglist);
5433 5383
5434 j = (int) t[0]; 5384 j = t[0];
5435 5385
5386 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5436 if (j == TST_LIST[0]) 5387 if (j == TST_LIST[0])
5437 { 5388 {
5438 if (arg != NIL && !is_pair (arg)) 5389 if (!tst_is_list (SCHEME_A_ arg))
5439 break; 5390 break;
5440 } 5391 }
5441 else 5392 else
5442 { 5393 {
5443 if (!tests[j].fct (arg)) 5394 if (!tests[j - 1].fct (arg))
5444 break; 5395 break;
5445 } 5396 }
5446 5397
5447 if (t[1] != 0) /* last test is replicated as necessary */ 5398 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5448 t++; 5399 t++;
5449 5400
5450 arglist = cdr (arglist); 5401 arglist = cdr (arglist);
5451 i++; 5402 i++;
5452 } 5403 }
5453 while (i < n); 5404 while (i < n);
5454 5405
5455 if (i < n) 5406 if (i < n)
5456 { 5407 {
5457 ok = 0;
5458 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); 5408 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind);
5409 xError_1 (SCHEME_A_ msg, 0);
5410 continue;
5459 } 5411 }
5460 } 5412 }
5461 } 5413 }
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 } 5414 }
5471#endif 5415#endif
5472 5416
5473 ok_to_freely_gc (SCHEME_A); 5417 ok_to_freely_gc (SCHEME_A);
5474 5418
5419 static const dispatch_func dispatch_funcs[] = {
5420 opexe_0,
5421 opexe_1,
5422 opexe_2,
5423 opexe_3,
5424 opexe_4,
5425 opexe_5,
5426 opexe_6,
5427 };
5428
5475 if (ecb_expect_false (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL)) 5429 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5476 return; 5430 return;
5477 5431
5478 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5432 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5479 { 5433 {
5480 xwrstr ("No memory!\n"); 5434 xwrstr ("No memory!\n");
5504mk_proc (SCHEME_P_ enum scheme_opcodes op) 5458mk_proc (SCHEME_P_ enum scheme_opcodes op)
5505{ 5459{
5506 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5460 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5507 set_typeflag (y, (T_PROC | T_ATOM)); 5461 set_typeflag (y, (T_PROC | T_ATOM));
5508 ivalue_unchecked (y) = op; 5462 ivalue_unchecked (y) = op;
5509 set_num_integer (y);
5510 return y; 5463 return y;
5511} 5464}
5512 5465
5513/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5466/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5514static int 5467static int
5579 return OP_C0STREAM; /* cons-stream */ 5532 return OP_C0STREAM; /* cons-stream */
5580 } 5533 }
5581} 5534}
5582 5535
5583#if USE_MULTIPLICITY 5536#if USE_MULTIPLICITY
5584scheme * 5537ecb_cold scheme *
5585scheme_init_new () 5538scheme_init_new ()
5586{ 5539{
5587 scheme *sc = malloc (sizeof (scheme)); 5540 scheme *sc = malloc (sizeof (scheme));
5588 5541
5589 if (!scheme_init (SCHEME_A)) 5542 if (!scheme_init (SCHEME_A))
5594 else 5547 else
5595 return sc; 5548 return sc;
5596} 5549}
5597#endif 5550#endif
5598 5551
5599int 5552ecb_cold int
5600scheme_init (SCHEME_P) 5553scheme_init (SCHEME_P)
5601{ 5554{
5602 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5555 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5603 pointer x; 5556 pointer x;
5604 5557
5677 5630
5678 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) 5631 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
5679 assign_syntax (SCHEME_A_ syntax_names[i]); 5632 assign_syntax (SCHEME_A_ syntax_names[i]);
5680 } 5633 }
5681 5634
5635 // TODO: should iterate via strlen, to avoid n² complexity
5682 for (i = 0; i < n; i++) 5636 for (i = 0; i < n; i++)
5683 if (dispatch_table[i].name != 0) 5637 if (dispatch_table[i].builtin)
5684 assign_proc (SCHEME_A_ i, dispatch_table[i].name); 5638 assign_proc (SCHEME_A_ i, opname (i));
5685 5639
5686 /* initialization of global pointers to special symbols */ 5640 /* initialization of global pointers to special symbols */
5687 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 5641 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
5688 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 5642 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
5689 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 5643 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
5728scheme_set_external_data (SCHEME_P_ void *p) 5682scheme_set_external_data (SCHEME_P_ void *p)
5729{ 5683{
5730 SCHEME_V->ext_data = p; 5684 SCHEME_V->ext_data = p;
5731} 5685}
5732 5686
5733void 5687ecb_cold void
5734scheme_deinit (SCHEME_P) 5688scheme_deinit (SCHEME_P)
5735{ 5689{
5736 int i; 5690 int i;
5737 5691
5738#if SHOW_ERROR_LINE 5692#if SHOW_ERROR_LINE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines