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

Comparing microscheme/scheme.c (file contents):
Revision 1.12 by root, Thu Nov 26 07:30:25 2015 UTC vs.
Revision 1.33 by root, Sat Nov 28 10:59:14 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#endif
76
77#if !USE_MULTIPLICITY 72#if !USE_MULTIPLICITY
78static scheme sc; 73static scheme sc;
79#endif 74#endif
80 75
81static void 76static void
150 145
151#define toupper(c) xtoupper (c) 146#define toupper(c) xtoupper (c)
152#define tolower(c) xtolower (c) 147#define tolower(c) xtolower (c)
153#define isdigit(c) xisdigit (c) 148#define isdigit(c) xisdigit (c)
154 149
155#if USE_STRLWR 150#if USE_IGNORECASE
156static const char * 151static const char *
157strlwr (char *s) 152xstrlwr (char *s)
158{ 153{
159 const char *p = s; 154 const char *p = s;
160 155
161 while (*s) 156 while (*s)
162 { 157 {
164 s++; 159 s++;
165 } 160 }
166 161
167 return p; 162 return p;
168} 163}
169#endif
170 164
165#define stricmp(a,b) strcasecmp (a, b)
166#define strlwr(s) xstrlwr (s)
167
168#else
171#define stricmp(a,b) strcmp (a, b) 169# define stricmp(a,b) strcmp (a, b)
172#define strlwr(s) (s) 170# define strlwr(s) (s)
171#endif
173 172
174#ifndef prompt 173#ifndef prompt
175# define prompt "ts> " 174# define prompt "ts> "
176#endif 175#endif
177 176
183# define FIRST_CELLSEGS 3 182# define FIRST_CELLSEGS 3
184#endif 183#endif
185 184
186enum scheme_types 185enum scheme_types
187{ 186{
187 T_INTEGER,
188 T_FREE, 188 T_REAL,
189 T_STRING, 189 T_STRING,
190 T_NUMBER,
191 T_SYMBOL, 190 T_SYMBOL,
192 T_PROC, 191 T_PROC,
193 T_PAIR, 192 T_PAIR, /* also used for free cells */
194 T_CLOSURE, 193 T_CLOSURE,
195 T_CONTINUATION, 194 T_CONTINUATION,
196 T_FOREIGN, 195 T_FOREIGN,
197 T_CHARACTER, 196 T_CHARACTER,
198 T_PORT, 197 T_PORT,
208#define T_SYNTAX 0x0010 207#define T_SYNTAX 0x0010
209#define T_IMMUTABLE 0x0020 208#define T_IMMUTABLE 0x0020
210#define T_ATOM 0x0040 /* only for gc */ 209#define T_ATOM 0x0040 /* only for gc */
211#define T_MARK 0x0080 /* only for gc */ 210#define T_MARK 0x0080 /* only for gc */
212 211
213static num num_add (num a, num b); 212/* num, for generic arithmetic */
214static num num_mul (num a, num b); 213struct num
215static num num_div (num a, num b); 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
238enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
239
240static num num_op (enum num_op op, num a, num b);
216static num num_intdiv (num a, num b); 241static num num_intdiv (num a, num b);
217static num num_sub (num a, num b);
218static num num_rem (num a, num b); 242static num num_rem (num a, num b);
219static num num_mod (num a, num b); 243static num num_mod (num a, num b);
220static int num_eq (num a, num b);
221static int num_gt (num a, num b);
222static int num_ge (num a, num b);
223static int num_lt (num a, num b);
224static int num_le (num a, num b);
225 244
226#if USE_MATH 245#if USE_MATH
227static double round_per_R5RS (double x); 246static double round_per_R5RS (double x);
228#endif 247#endif
229static int is_zero_rvalue (RVALUE x); 248static int is_zero_rvalue (RVALUE x);
230
231static INLINE int
232num_is_integer (pointer p)
233{
234 return num_is_fixnum (p->object.number);
235}
236 249
237static num num_zero; 250static num num_zero;
238static num num_one; 251static num num_one;
239 252
240/* macros for cell operations */ 253/* macros for cell operations */
241#define typeflag(p) ((p)->flag + 0) 254#define typeflag(p) ((p)->flag + 0)
242#define set_typeflag(p,v) ((p)->flag = (v)) 255#define set_typeflag(p,v) ((p)->flag = (v))
243#define type(p) (typeflag (p) & T_MASKTYPE) 256#define type(p) (typeflag (p) & T_MASKTYPE)
244 257
245INTERFACE INLINE int 258INTERFACE int
246is_string (pointer p) 259is_string (pointer p)
247{ 260{
248 return type (p) == T_STRING; 261 return type (p) == T_STRING;
249} 262}
250 263
251#define strvalue(p) ((p)->object.string.svalue) 264#define strvalue(p) ((p)->object.string.svalue)
252#define strlength(p) ((p)->object.string.length) 265#define strlength(p) ((p)->object.string.length)
253 266
254INTERFACE int is_list (SCHEME_P_ pointer p);
255INTERFACE INLINE int 267INTERFACE int
256is_vector (pointer p) 268is_vector (pointer p)
257{ 269{
258 return type (p) == T_VECTOR; 270 return type (p) == T_VECTOR;
259} 271}
260 272
261#define vecvalue(p) ((p)->object.vector.vvalue) 273#define vecvalue(p) ((p)->object.vector.vvalue)
262#define veclength(p) ((p)->object.vector.length) 274#define veclength(p) ((p)->object.vector.length)
263INTERFACE void fill_vector (pointer vec, pointer obj); 275INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
264INTERFACE uint32_t vector_length (pointer vec);
265INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); 276INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
266INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); 277INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
267 278
268INTERFACE uint32_t 279INTERFACE int
269vector_length (pointer vec) 280is_integer (pointer p)
270{ 281{
271 return vec->object.vector.length; 282 return type (p) == T_INTEGER;
272} 283}
273 284
285/* not the same as in scheme, where integers are (correctly :) reals */
274INTERFACE INLINE int 286INTERFACE int
287is_real (pointer p)
288{
289 return type (p) == T_REAL;
290}
291
292INTERFACE int
275is_number (pointer p) 293is_number (pointer p)
276{ 294{
277 return type (p) == T_NUMBER; 295 return is_integer (p) || is_real (p);
278} 296}
279 297
280INTERFACE INLINE int 298INTERFACE int
281is_integer (pointer p)
282{
283 if (!is_number (p))
284 return 0;
285
286 if (num_is_integer (p) || ivalue (p) == rvalue (p))
287 return 1;
288
289 return 0;
290}
291
292INTERFACE INLINE int
293is_real (pointer p)
294{
295 return is_number (p) && !num_is_fixnum (p->object.number);
296}
297
298INTERFACE INLINE int
299is_character (pointer p) 299is_character (pointer p)
300{ 300{
301 return type (p) == T_CHARACTER; 301 return type (p) == T_CHARACTER;
302} 302}
303 303
304INTERFACE INLINE char * 304INTERFACE char *
305string_value (pointer p) 305string_value (pointer p)
306{ 306{
307 return strvalue (p); 307 return strvalue (p);
308} 308}
309 309
310INLINE num
311nvalue (pointer p)
312{
313 return (p)->object.number;
314}
315
316static IVALUE
317num_get_ivalue (const num n)
318{
319 return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n);
320}
321
322static RVALUE
323num_get_rvalue (const num n)
324{
325 return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n);
326}
327
328INTERFACE IVALUE
329ivalue (pointer p)
330{
331 return num_get_ivalue (p->object.number);
332}
333
334INTERFACE RVALUE
335rvalue (pointer p)
336{
337 return num_get_rvalue (p->object.number);
338}
339
340#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
341#if USE_REAL 313#if USE_REAL
342# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 314#define rvalue_unchecked(p) (p)->object.rvalue
343# define set_num_integer(p) (p)->object.number.is_fixnum=1; 315#define set_rvalue(p,v) (p)->object.rvalue = (v)
344# define set_num_real(p) (p)->object.number.is_fixnum=0;
345#else 316#else
346# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 317#define rvalue_unchecked(p) (p)->object.ivalue
347# define set_num_integer(p) 0 318#define set_rvalue(p,v) (p)->object.ivalue = (v)
348# define set_num_real(p) 0
349#endif 319#endif
320
350INTERFACE long 321INTERFACE long
351charvalue (pointer p) 322charvalue (pointer p)
352{ 323{
353 return ivalue_unchecked (p); 324 return ivalue_unchecked (p);
354} 325}
355 326
356INTERFACE INLINE int 327INTERFACE int
357is_port (pointer p) 328is_port (pointer p)
358{ 329{
359 return type (p) == T_PORT; 330 return type (p) == T_PORT;
360} 331}
361 332
362INTERFACE INLINE int 333INTERFACE int
363is_inport (pointer p) 334is_inport (pointer p)
364{ 335{
365 return is_port (p) && p->object.port->kind & port_input; 336 return is_port (p) && p->object.port->kind & port_input;
366} 337}
367 338
368INTERFACE INLINE int 339INTERFACE int
369is_outport (pointer p) 340is_outport (pointer p)
370{ 341{
371 return is_port (p) && p->object.port->kind & port_output; 342 return is_port (p) && p->object.port->kind & port_output;
372} 343}
373 344
374INTERFACE INLINE int 345INTERFACE int
375is_pair (pointer p) 346is_pair (pointer p)
376{ 347{
377 return type (p) == T_PAIR; 348 return type (p) == T_PAIR;
378} 349}
379 350
411pair_cdr (pointer p) 382pair_cdr (pointer p)
412{ 383{
413 return cdr (p); 384 return cdr (p);
414} 385}
415 386
416INTERFACE INLINE int 387INTERFACE int
417is_symbol (pointer p) 388is_symbol (pointer p)
418{ 389{
419 return type (p) == T_SYMBOL; 390 return type (p) == T_SYMBOL;
420} 391}
421 392
422INTERFACE INLINE char * 393INTERFACE char *
423symname (pointer p) 394symname (pointer p)
424{ 395{
425 return strvalue (car (p)); 396 return strvalue (car (p));
426} 397}
427 398
428#if USE_PLIST 399#if USE_PLIST
429SCHEME_EXPORT INLINE int 400SCHEME_EXPORT int
430hasprop (pointer p) 401hasprop (pointer p)
431{ 402{
432 return typeflag (p) & T_SYMBOL; 403 return typeflag (p) & T_SYMBOL;
433} 404}
434 405
435# define symprop(p) cdr(p) 406# define symprop(p) cdr(p)
436#endif 407#endif
437 408
438INTERFACE INLINE int 409INTERFACE int
439is_syntax (pointer p) 410is_syntax (pointer p)
440{ 411{
441 return typeflag (p) & T_SYNTAX; 412 return typeflag (p) & T_SYNTAX;
442} 413}
443 414
444INTERFACE INLINE int 415INTERFACE int
445is_proc (pointer p) 416is_proc (pointer p)
446{ 417{
447 return type (p) == T_PROC; 418 return type (p) == T_PROC;
448} 419}
449 420
450INTERFACE INLINE int 421INTERFACE int
451is_foreign (pointer p) 422is_foreign (pointer p)
452{ 423{
453 return type (p) == T_FOREIGN; 424 return type (p) == T_FOREIGN;
454} 425}
455 426
456INTERFACE INLINE char * 427INTERFACE char *
457syntaxname (pointer p) 428syntaxname (pointer p)
458{ 429{
459 return strvalue (car (p)); 430 return strvalue (car (p));
460} 431}
461 432
462#define procnum(p) ivalue (p) 433#define procnum(p) ivalue_unchecked (p)
463static const char *procname (pointer x); 434static const char *procname (pointer x);
464 435
465INTERFACE INLINE int 436INTERFACE int
466is_closure (pointer p) 437is_closure (pointer p)
467{ 438{
468 return type (p) == T_CLOSURE; 439 return type (p) == T_CLOSURE;
469} 440}
470 441
471INTERFACE INLINE int 442INTERFACE int
472is_macro (pointer p) 443is_macro (pointer p)
473{ 444{
474 return type (p) == T_MACRO; 445 return type (p) == T_MACRO;
475} 446}
476 447
477INTERFACE INLINE pointer 448INTERFACE pointer
478closure_code (pointer p) 449closure_code (pointer p)
479{ 450{
480 return car (p); 451 return car (p);
481} 452}
482 453
483INTERFACE INLINE pointer 454INTERFACE pointer
484closure_env (pointer p) 455closure_env (pointer p)
485{ 456{
486 return cdr (p); 457 return cdr (p);
487} 458}
488 459
489INTERFACE INLINE int 460INTERFACE int
490is_continuation (pointer p) 461is_continuation (pointer p)
491{ 462{
492 return type (p) == T_CONTINUATION; 463 return type (p) == T_CONTINUATION;
493} 464}
494 465
495#define cont_dump(p) cdr (p) 466#define cont_dump(p) cdr (p)
496#define set_cont_dump(p,v) set_cdr ((p), (v)) 467#define set_cont_dump(p,v) set_cdr ((p), (v))
497 468
498/* To do: promise should be forced ONCE only */ 469/* To do: promise should be forced ONCE only */
499INTERFACE INLINE int 470INTERFACE int
500is_promise (pointer p) 471is_promise (pointer p)
501{ 472{
502 return type (p) == T_PROMISE; 473 return type (p) == T_PROMISE;
503} 474}
504 475
505INTERFACE INLINE int 476INTERFACE int
506is_environment (pointer p) 477is_environment (pointer p)
507{ 478{
508 return type (p) == T_ENVIRONMENT; 479 return type (p) == T_ENVIRONMENT;
509} 480}
510 481
516 487
517#define is_mark(p) (typeflag (p) & T_MARK) 488#define is_mark(p) (typeflag (p) & T_MARK)
518#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) 489#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
519#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) 490#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
520 491
521INTERFACE INLINE int 492INTERFACE int
522is_immutable (pointer p) 493is_immutable (pointer p)
523{ 494{
524 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; 495 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
525} 496}
526 497
527INTERFACE INLINE void 498INTERFACE void
528setimmutable (pointer p) 499setimmutable (pointer p)
529{ 500{
530#if USE_ERROR_CHECKING 501#if USE_ERROR_CHECKING
531 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 502 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
532#endif 503#endif
533} 504}
534 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
535#if USE_CHAR_CLASSIFIERS 560#if USE_CHAR_CLASSIFIERS
536static INLINE int 561ecb_inline int
537Cisalpha (int c) 562Cisalpha (int c)
538{ 563{
539 return isascii (c) && isalpha (c); 564 return isascii (c) && isalpha (c);
540} 565}
541 566
542static INLINE int 567ecb_inline int
543Cisdigit (int c) 568Cisdigit (int c)
544{ 569{
545 return isascii (c) && isdigit (c); 570 return isascii (c) && isdigit (c);
546} 571}
547 572
548static INLINE int 573ecb_inline int
549Cisspace (int c) 574Cisspace (int c)
550{ 575{
551 return isascii (c) && isspace (c); 576 return isascii (c) && isspace (c);
552} 577}
553 578
554static INLINE int 579ecb_inline int
555Cisupper (int c) 580Cisupper (int c)
556{ 581{
557 return isascii (c) && isupper (c); 582 return isascii (c) && isupper (c);
558} 583}
559 584
560static INLINE int 585ecb_inline int
561Cislower (int c) 586Cislower (int c)
562{ 587{
563 return isascii (c) && islower (c); 588 return isascii (c) && islower (c);
564} 589}
565#endif 590#endif
626#endif 651#endif
627 652
628static int file_push (SCHEME_P_ const char *fname); 653static int file_push (SCHEME_P_ const char *fname);
629static void file_pop (SCHEME_P); 654static void file_pop (SCHEME_P);
630static int file_interactive (SCHEME_P); 655static int file_interactive (SCHEME_P);
631static INLINE int is_one_of (char *s, int c); 656ecb_inline int is_one_of (char *s, int c);
632static int alloc_cellseg (SCHEME_P_ int n); 657static int alloc_cellseg (SCHEME_P_ int n);
633static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); 658ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
634static void finalize_cell (SCHEME_P_ pointer a); 659static void finalize_cell (SCHEME_P_ pointer a);
635static int count_consecutive_cells (pointer x, int needed); 660static int count_consecutive_cells (pointer x, int needed);
636static 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);
637static pointer mk_number (SCHEME_P_ const num n); 662static pointer mk_number (SCHEME_P_ const num n);
638static 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);
655static int basic_inchar (port *pt); 680static int basic_inchar (port *pt);
656static int inchar (SCHEME_P); 681static int inchar (SCHEME_P);
657static void backchar (SCHEME_P_ int c); 682static void backchar (SCHEME_P_ int c);
658static char *readstr_upto (SCHEME_P_ char *delim); 683static char *readstr_upto (SCHEME_P_ char *delim);
659static pointer readstrexp (SCHEME_P); 684static pointer readstrexp (SCHEME_P);
660static INLINE int skipspace (SCHEME_P); 685ecb_inline int skipspace (SCHEME_P);
661static int token (SCHEME_P); 686static int token (SCHEME_P);
662static void printslashstring (SCHEME_P_ char *s, int len); 687static void printslashstring (SCHEME_P_ char *s, int len);
663static 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);
664static void printatom (SCHEME_P_ pointer l, int f); 689static void printatom (SCHEME_P_ pointer l, int f);
665static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 690static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
669static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); 694static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list);
670static pointer revappend (SCHEME_P_ pointer a, pointer b); 695static pointer revappend (SCHEME_P_ pointer a, pointer b);
671static pointer ss_get_cont (SCHEME_P); 696static pointer ss_get_cont (SCHEME_P);
672static void ss_set_cont (SCHEME_P_ pointer cont); 697static void ss_set_cont (SCHEME_P_ pointer cont);
673static void dump_stack_mark (SCHEME_P); 698static void dump_stack_mark (SCHEME_P);
674static 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);
675static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); 701static int opexe_2 (SCHEME_P_ enum scheme_opcodes op);
676static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); 702static int opexe_3 (SCHEME_P_ enum scheme_opcodes op);
677static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); 703static int opexe_4 (SCHEME_P_ enum scheme_opcodes op);
678static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); 704static int opexe_5 (SCHEME_P_ enum scheme_opcodes op);
679static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); 705static int opexe_6 (SCHEME_P_ enum scheme_opcodes op);
680static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 706static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
681static void assign_syntax (SCHEME_P_ const char *name); 707static void assign_syntax (SCHEME_P_ const char *name);
682static int syntaxnum (pointer p); 708static int syntaxnum (pointer p);
683static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 709static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
684 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
685static num 738static num
686num_add (num a, num b) 739num_op (enum num_op op, num a, num b)
687{ 740{
688 num ret; 741 num ret;
689 742
690 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));
691 744
692 if (num_is_fixnum (ret)) 745 if (num_is_fixnum (ret))
693 num_set_ivalue (ret, num_get_ivalue (a) + num_get_ivalue (b)); 746 {
747 switch (op)
748 {
749 case NUM_ADD: a.ivalue += b.ivalue; break;
750 case NUM_SUB: a.ivalue -= b.ivalue; break;
751 case NUM_MUL: a.ivalue *= b.ivalue; break;
752 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
753 }
754
755 num_set_ivalue (ret, a.ivalue);
756 }
757#if USE_REAL
694 else 758 else
695 num_set_rvalue (ret, num_get_rvalue (a) + num_get_rvalue (b)); 759 {
760 switch (op)
761 {
762 case NUM_ADD: a.rvalue += b.rvalue; break;
763 case NUM_SUB: a.rvalue -= b.rvalue; break;
764 case NUM_MUL: a.rvalue *= b.rvalue; break;
765 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
766 }
696 767
697 return ret; 768 num_set_rvalue (ret, a.rvalue);
698} 769 }
699 770#endif
700static num
701num_mul (num a, num b)
702{
703 num ret;
704
705 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
706
707 if (num_is_fixnum (ret))
708 num_set_ivalue (ret, num_get_ivalue (a) * num_get_ivalue (b));
709 else
710 num_set_rvalue (ret, num_get_rvalue (a) * num_get_rvalue (b));
711 771
712 return ret; 772 return ret;
713} 773}
714 774
715static num 775static num
716num_div (num a, num b) 776num_div (num a, num b)
717{ 777{
718 num ret; 778 num ret;
719 779
720 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);
721 781
722 if (num_is_fixnum (ret)) 782 if (num_is_fixnum (ret))
723 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 783 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
724 else 784 else
725 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 785 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
726
727 return ret;
728}
729
730static num
731num_intdiv (num a, num b)
732{
733 num ret;
734
735 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
736
737 if (num_is_fixnum (ret))
738 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b));
739 else
740 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b));
741
742 return ret;
743}
744
745static num
746num_sub (num a, num b)
747{
748 num ret;
749
750 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
751
752 if (num_is_fixnum (ret))
753 num_set_ivalue (ret, num_get_ivalue (a) - num_get_ivalue (b));
754 else
755 num_set_rvalue (ret, num_get_rvalue (a) - num_get_rvalue (b));
756 786
757 return ret; 787 return ret;
758} 788}
759 789
760static num 790static num
762{ 792{
763 num ret; 793 num ret;
764 long e1, e2, res; 794 long e1, e2, res;
765 795
766 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));
767 e1 = num_get_ivalue (a); 797 e1 = num_ivalue (a);
768 e2 = num_get_ivalue (b); 798 e2 = num_ivalue (b);
769 res = e1 % e2; 799 res = e1 % e2;
770 800
771 /* remainder should have same sign as second operand */ 801 /* remainder should have same sign as second operand */
772 if (res > 0) 802 if (res > 0)
773 { 803 {
789{ 819{
790 num ret; 820 num ret;
791 long e1, e2, res; 821 long e1, e2, res;
792 822
793 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));
794 e1 = num_get_ivalue (a); 824 e1 = num_ivalue (a);
795 e2 = num_get_ivalue (b); 825 e2 = num_ivalue (b);
796 res = e1 % e2; 826 res = e1 % e2;
797 827
798 /* modulo should have same sign as second operand */ 828 /* modulo should have same sign as second operand */
799 if (res * e2 < 0) 829 if (res * e2 < 0)
800 res += e2; 830 res += e2;
801 831
802 num_set_ivalue (ret, res); 832 num_set_ivalue (ret, res);
803 return ret; 833 return ret;
804} 834}
805 835
836/* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */
806static int 837static int
807num_eq (num a, num b) 838num_cmp (num a, num b)
808{ 839{
840 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
809 int ret; 841 int ret;
810 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
811 842
812 if (is_fixnum) 843 if (is_fixnum)
813 ret = num_get_ivalue (a) == num_get_ivalue (b); 844 {
845 IVALUE av = num_ivalue (a);
846 IVALUE bv = num_ivalue (b);
847
848 ret = av == bv ? 0 : av < bv ? -1 : +1;
849 }
814 else 850 else
815 ret = num_get_rvalue (a) == num_get_rvalue (b); 851 {
852 RVALUE av = num_rvalue (a);
853 RVALUE bv = num_rvalue (b);
854
855 ret = av == bv ? 0 : av < bv ? -1 : +1;
856 }
816 857
817 return ret; 858 return ret;
818}
819
820
821static int
822num_gt (num a, num b)
823{
824 int ret;
825 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
826
827 if (is_fixnum)
828 ret = num_get_ivalue (a) > num_get_ivalue (b);
829 else
830 ret = num_get_rvalue (a) > num_get_rvalue (b);
831
832 return ret;
833}
834
835static int
836num_ge (num a, num b)
837{
838 return !num_lt (a, b);
839}
840
841static int
842num_lt (num a, num b)
843{
844 int ret;
845 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
846
847 if (is_fixnum)
848 ret = num_get_ivalue (a) < num_get_ivalue (b);
849 else
850 ret = num_get_rvalue (a) < num_get_rvalue (b);
851
852 return ret;
853}
854
855static int
856num_le (num a, num b)
857{
858 return !num_gt (a, b);
859} 859}
860 860
861#if USE_MATH 861#if USE_MATH
862 862
863/* Round to nearest. Round to even if midway */ 863/* Round to nearest. Round to even if midway */
873 return ce; 873 return ce;
874 else if (dfl < dce) 874 else if (dfl < dce)
875 return fl; 875 return fl;
876 else 876 else
877 { 877 {
878 if (fmod (fl, 2.0) == 0.0) /* I imagine this holds */ 878 if (fmod (fl, 2) == 0) /* I imagine this holds */
879 return fl; 879 return fl;
880 else 880 else
881 return ce; 881 return ce;
882 } 882 }
883} 883}
884#endif 884#endif
885 885
886static int 886static int
887is_zero_rvalue (RVALUE x) 887is_zero_rvalue (RVALUE x)
888{ 888{
889 return x == 0;
890#if 0
889#if USE_REAL 891#if USE_REAL
890 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. */
891#else 893#else
892 return x == 0; 894 return x == 0;
895#endif
893#endif 896#endif
894} 897}
895 898
896/* allocate new cell segment */ 899/* allocate new cell segment */
897static int 900static int
918 return k; 921 return k;
919 922
920 i = ++SCHEME_V->last_cell_seg; 923 i = ++SCHEME_V->last_cell_seg;
921 SCHEME_V->alloc_seg[i] = cp; 924 SCHEME_V->alloc_seg[i] = cp;
922 925
923 /* insert new segment in address order */
924 newp = (pointer)cp; 926 newp = (pointer)cp;
925 SCHEME_V->cell_seg[i] = newp; 927 SCHEME_V->cell_seg[i] = newp;
926 SCHEME_V->cell_segsize[i] = segsize; 928 SCHEME_V->cell_segsize[i] = segsize;
927
928 //TODO: insert, not swap
929 while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i])
930 {
931 p = SCHEME_V->cell_seg[i];
932 SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1];
933 SCHEME_V->cell_seg[i - 1] = p;
934
935 k = SCHEME_V->cell_segsize[i];
936 SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1];
937 SCHEME_V->cell_segsize[i - 1] = k;
938
939 --i;
940 }
941
942 SCHEME_V->fcells += segsize; 929 SCHEME_V->fcells += segsize;
943 last = newp + segsize - 1; 930 last = newp + segsize - 1;
944 931
945 for (p = newp; p <= last; p++) 932 for (p = newp; p <= last; p++)
946 { 933 {
947 set_typeflag (p, T_FREE); 934 set_typeflag (p, T_PAIR);
948 set_car (p, NIL); 935 set_car (p, NIL);
949 set_cdr (p, p + 1); 936 set_cdr (p, p + 1);
950 } 937 }
951 938
952 /* insert new cells in address order on free list */
953 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell)
954 {
955 set_cdr (last, SCHEME_V->free_cell); 939 set_cdr (last, SCHEME_V->free_cell);
956 SCHEME_V->free_cell = newp; 940 SCHEME_V->free_cell = newp;
957 }
958 else
959 {
960 p = SCHEME_V->free_cell;
961
962 while (cdr (p) != NIL && newp > cdr (p))
963 p = cdr (p);
964
965 set_cdr (last, cdr (p));
966 set_cdr (p, newp);
967 }
968 } 941 }
969 942
970 return n; 943 return n;
971} 944}
972 945
973/* get new cell. parameter a, b is marked by gc. */ 946/* get new cell. parameter a, b is marked by gc. */
974static INLINE pointer 947ecb_inline pointer
975get_cell_x (SCHEME_P_ pointer a, pointer b) 948get_cell_x (SCHEME_P_ pointer a, pointer b)
976{ 949{
977 if (ecb_expect_false (SCHEME_V->free_cell == NIL)) 950 if (ecb_expect_false (SCHEME_V->free_cell == NIL))
978 { 951 {
979 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 952 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
1051 /* Record it as a vector so that gc understands it. */ 1024 /* Record it as a vector so that gc understands it. */
1052 set_typeflag (v, T_VECTOR | T_ATOM); 1025 set_typeflag (v, T_VECTOR | T_ATOM);
1053 1026
1054 v->object.vector.vvalue = e; 1027 v->object.vector.vvalue = e;
1055 v->object.vector.length = len; 1028 v->object.vector.length = len;
1056 fill_vector (v, init); 1029 fill_vector (v, 0, init);
1057 push_recent_alloc (SCHEME_A_ v, NIL); 1030 push_recent_alloc (SCHEME_A_ v, NIL);
1058 1031
1059 return v; 1032 return v;
1060} 1033}
1061 1034
1062static INLINE void 1035ecb_inline void
1063ok_to_freely_gc (SCHEME_P) 1036ok_to_freely_gc (SCHEME_P)
1064{ 1037{
1065 set_car (S_SINK, NIL); 1038 set_car (S_SINK, NIL);
1066} 1039}
1067 1040
1108 1081
1109/* ========== oblist implementation ========== */ 1082/* ========== oblist implementation ========== */
1110 1083
1111#ifndef USE_OBJECT_LIST 1084#ifndef USE_OBJECT_LIST
1112 1085
1086static int
1113static int hash_fn (const char *key, int table_size); 1087hash_fn (const char *key, int table_size)
1088{
1089 const unsigned char *p = key;
1090 uint32_t hash = 2166136261;
1091
1092 while (*p)
1093 hash = (hash ^ *p++) * 16777619;
1094
1095 return hash % table_size;
1096}
1114 1097
1115static pointer 1098static pointer
1116oblist_initial_value (SCHEME_P) 1099oblist_initial_value (SCHEME_P)
1117{ 1100{
1118 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1101 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1127 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); 1110 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1128 set_typeflag (x, T_SYMBOL); 1111 set_typeflag (x, T_SYMBOL);
1129 setimmutable (car (x)); 1112 setimmutable (car (x));
1130 1113
1131 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1114 location = hash_fn (name, veclength (SCHEME_V->oblist));
1132 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); 1115 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1133 return x; 1116 return x;
1134} 1117}
1135 1118
1136static INLINE pointer 1119ecb_inline pointer
1137oblist_find_by_name (SCHEME_P_ const char *name) 1120oblist_find_by_name (SCHEME_P_ const char *name)
1138{ 1121{
1139 int location; 1122 int location;
1140 pointer x; 1123 pointer x;
1141 char *s; 1124 char *s;
1142 1125
1143 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1126 location = hash_fn (name, veclength (SCHEME_V->oblist));
1144 1127
1145 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1128 for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1146 { 1129 {
1147 s = symname (car (x)); 1130 s = symname (car (x));
1148 1131
1149 /* case-insensitive, per R5RS section 2 */ 1132 /* case-insensitive, per R5RS section 2 */
1150 if (stricmp (name, s) == 0) 1133 if (stricmp (name, s) == 0)
1160 int i; 1143 int i;
1161 pointer x; 1144 pointer x;
1162 pointer ob_list = NIL; 1145 pointer ob_list = NIL;
1163 1146
1164 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1147 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1165 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1148 for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1166 ob_list = cons (x, ob_list); 1149 ob_list = cons (x, ob_list);
1167 1150
1168 return ob_list; 1151 return ob_list;
1169} 1152}
1170 1153
1174oblist_initial_value (SCHEME_P) 1157oblist_initial_value (SCHEME_P)
1175{ 1158{
1176 return NIL; 1159 return NIL;
1177} 1160}
1178 1161
1179static INLINE pointer 1162ecb_inline pointer
1180oblist_find_by_name (SCHEME_P_ const char *name) 1163oblist_find_by_name (SCHEME_P_ const char *name)
1181{ 1164{
1182 pointer x; 1165 pointer x;
1183 char *s; 1166 char *s;
1184 1167
1196 1179
1197/* returns the new symbol */ 1180/* returns the new symbol */
1198static pointer 1181static pointer
1199oblist_add_by_name (SCHEME_P_ const char *name) 1182oblist_add_by_name (SCHEME_P_ const char *name)
1200{ 1183{
1201 pointer x; 1184 pointer x = mk_string (SCHEME_A_ name);
1202
1203 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1204 set_typeflag (x, T_SYMBOL); 1185 set_typeflag (x, T_SYMBOL);
1205 setimmutable (car (x)); 1186 setimmutable (x);
1206 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1187 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1207 return x; 1188 return x;
1208} 1189}
1209 1190
1210static pointer 1191static pointer
1243mk_character (SCHEME_P_ int c) 1224mk_character (SCHEME_P_ int c)
1244{ 1225{
1245 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1226 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1246 1227
1247 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1228 set_typeflag (x, (T_CHARACTER | T_ATOM));
1248 ivalue_unchecked (x) = c & 0xff; 1229 set_ivalue (x, c & 0xff);
1249 set_num_integer (x); 1230
1250 return x; 1231 return x;
1251} 1232}
1252 1233
1253/* get number atom (integer) */ 1234/* get number atom (integer) */
1254INTERFACE pointer 1235INTERFACE pointer
1255mk_integer (SCHEME_P_ long num) 1236mk_integer (SCHEME_P_ long n)
1256{ 1237{
1257 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1238 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1258 1239
1259 set_typeflag (x, (T_NUMBER | T_ATOM)); 1240 set_typeflag (x, (T_INTEGER | T_ATOM));
1260 ivalue_unchecked (x) = num; 1241 set_ivalue (x, n);
1261 set_num_integer (x); 1242
1262 return x; 1243 return x;
1263} 1244}
1264 1245
1265INTERFACE pointer 1246INTERFACE pointer
1266mk_real (SCHEME_P_ RVALUE n) 1247mk_real (SCHEME_P_ RVALUE n)
1267{ 1248{
1249#if USE_REAL
1268 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1250 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1269 1251
1270 set_typeflag (x, (T_NUMBER | T_ATOM)); 1252 set_typeflag (x, (T_REAL | T_ATOM));
1271 rvalue_unchecked (x) = n; 1253 set_rvalue (x, n);
1272 set_num_real (x); 1254
1273 return x; 1255 return x;
1256#else
1257 return mk_integer (SCHEME_A_ n);
1258#endif
1274} 1259}
1275 1260
1276static pointer 1261static pointer
1277mk_number (SCHEME_P_ const num n) 1262mk_number (SCHEME_P_ const num n)
1278{ 1263{
1264#if USE_REAL
1279 if (num_is_fixnum (n)) 1265 return num_is_fixnum (n)
1266 ? mk_integer (SCHEME_A_ num_ivalue (n))
1267 : mk_real (SCHEME_A_ num_rvalue (n));
1268#else
1280 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1269 return mk_integer (SCHEME_A_ num_ivalue (n));
1281 else 1270#endif
1282 return mk_real (SCHEME_A_ num_get_rvalue (n));
1283} 1271}
1284 1272
1285/* allocate name to string area */ 1273/* allocate name to string area */
1286static char * 1274static char *
1287store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) 1275store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1346{ 1334{
1347 return get_vector_object (SCHEME_A_ len, NIL); 1335 return get_vector_object (SCHEME_A_ len, NIL);
1348} 1336}
1349 1337
1350INTERFACE void 1338INTERFACE void
1351fill_vector (pointer vec, pointer obj) 1339fill_vector (pointer vec, uint32_t start, pointer obj)
1352{ 1340{
1353 int i; 1341 int i;
1354 1342
1355 for (i = 0; i < vec->object.vector.length; i++) 1343 for (i = start; i < veclength (vec); i++)
1356 vecvalue (vec)[i] = obj; 1344 vecvalue (vec)[i] = obj;
1357} 1345}
1358 1346
1359INTERFACE pointer 1347INTERFACE pointer
1360vector_elem (pointer vec, uint32_t ielem) 1348vector_get (pointer vec, uint32_t ielem)
1361{ 1349{
1362 return vecvalue(vec)[ielem]; 1350 return vecvalue(vec)[ielem];
1363} 1351}
1364 1352
1365INTERFACE void 1353INTERFACE void
1366set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1354vector_set (pointer vec, uint32_t ielem, pointer a)
1367{ 1355{
1368 vecvalue(vec)[ielem] = a; 1356 vecvalue(vec)[ielem] = a;
1369} 1357}
1370 1358
1371/* get new symbol */ 1359/* get new symbol */
1463 } 1451 }
1464 else if ((c == 'e') || (c == 'E')) 1452 else if ((c == 'e') || (c == 'E'))
1465 { 1453 {
1466 if (!has_fp_exp) 1454 if (!has_fp_exp)
1467 { 1455 {
1468 has_dec_point = 1; /* decimal point illegal 1456 has_dec_point = 1; /* decimal point illegal from now on */
1469 from now on */
1470 p++; 1457 p++;
1471 1458
1472 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1459 if ((*p == '-') || (*p == '+') || isdigit (*p))
1473 continue; 1460 continue;
1474 } 1461 }
1562 1549
1563 if (ecb_expect_false (is_vector (p))) 1550 if (ecb_expect_false (is_vector (p)))
1564 { 1551 {
1565 int i; 1552 int i;
1566 1553
1567 for (i = 0; i < p->object.vector.length; i++) 1554 for (i = 0; i < veclength (p); i++)
1568 mark (vecvalue (p)[i]); 1555 mark (vecvalue (p)[i]);
1569 } 1556 }
1570 1557
1571 if (is_atom (p)) 1558 if (is_atom (p))
1572 goto E6; 1559 goto E6;
1670 if (is_mark (p)) 1657 if (is_mark (p))
1671 clrmark (p); 1658 clrmark (p);
1672 else 1659 else
1673 { 1660 {
1674 /* reclaim cell */ 1661 /* reclaim cell */
1675 if (typeflag (p) != T_FREE) 1662 if (typeflag (p) != T_PAIR)
1676 { 1663 {
1677 finalize_cell (SCHEME_A_ p); 1664 finalize_cell (SCHEME_A_ p);
1678 set_typeflag (p, T_FREE); 1665 set_typeflag (p, T_PAIR);
1679 set_car (p, NIL); 1666 set_car (p, NIL);
1680 } 1667 }
1681 1668
1682 ++SCHEME_V->fcells; 1669 ++SCHEME_V->fcells;
1683 set_cdr (p, SCHEME_V->free_cell); 1670 set_cdr (p, SCHEME_V->free_cell);
1685 } 1672 }
1686 } 1673 }
1687 } 1674 }
1688 1675
1689 if (SCHEME_V->gc_verbose) 1676 if (SCHEME_V->gc_verbose)
1677 {
1690 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1678 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1679 }
1691} 1680}
1692 1681
1693static void 1682static void
1694finalize_cell (SCHEME_P_ pointer a) 1683finalize_cell (SCHEME_P_ pointer a)
1695{ 1684{
2282 } 2271 }
2283 } 2272 }
2284} 2273}
2285 2274
2286/* check c is in chars */ 2275/* check c is in chars */
2287static INLINE int 2276ecb_inline int
2288is_one_of (char *s, int c) 2277is_one_of (char *s, int c)
2289{ 2278{
2290 if (c == EOF) 2279 if (c == EOF)
2291 return 1; 2280 return 1;
2292 2281
2293 return !!strchr (s, c); 2282 return !!strchr (s, c);
2294} 2283}
2295 2284
2296/* skip white characters */ 2285/* skip white characters */
2297static INLINE int 2286ecb_inline int
2298skipspace (SCHEME_P) 2287skipspace (SCHEME_P)
2299{ 2288{
2300 int c, curr_line = 0; 2289 int c, curr_line = 0;
2301 2290
2302 do 2291 do
2530 { 2519 {
2531 p = SCHEME_V->strbuff; 2520 p = SCHEME_V->strbuff;
2532 2521
2533 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2522 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2534 { 2523 {
2535 if (num_is_integer (l)) 2524 if (is_integer (l))
2536 xnum (p, ivalue_unchecked (l)); 2525 xnum (p, ivalue_unchecked (l));
2537#if USE_REAL 2526#if USE_REAL
2538 else 2527 else
2539 { 2528 {
2540 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2529 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2795 return 0; 2784 return 0;
2796 } 2785 }
2797 else if (is_number (a)) 2786 else if (is_number (a))
2798 { 2787 {
2799 if (is_number (b)) 2788 if (is_number (b))
2800 if (num_is_integer (a) == num_is_integer (b))
2801 return num_eq (nvalue (a), nvalue (b)); 2789 return num_cmp (nvalue (a), nvalue (b)) == 0;
2802 2790
2803 return 0; 2791 return 0;
2804 } 2792 }
2805 else if (is_character (a)) 2793 else if (is_character (a))
2806 { 2794 {
2832/* () is #t in R5RS */ 2820/* () is #t in R5RS */
2833#define is_true(p) ((p) != S_F) 2821#define is_true(p) ((p) != S_F)
2834#define is_false(p) ((p) == S_F) 2822#define is_false(p) ((p) == S_F)
2835 2823
2836/* ========== Environment implementation ========== */ 2824/* ========== Environment implementation ========== */
2837
2838#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST)
2839
2840static int
2841hash_fn (const char *key, int table_size)
2842{
2843 const unsigned char *p = key;
2844 uint32_t hash = 2166136261;
2845
2846 while (*p)
2847 hash = (hash ^ *p++) * 16777619;
2848
2849 return hash % table_size;
2850}
2851#endif
2852 2825
2853#ifndef USE_ALIST_ENV 2826#ifndef USE_ALIST_ENV
2854 2827
2855/* 2828/*
2856 * In this implementation, each frame of the environment may be 2829 * In this implementation, each frame of the environment may be
2873 2846
2874 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2847 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2875 setenvironment (SCHEME_V->envir); 2848 setenvironment (SCHEME_V->envir);
2876} 2849}
2877 2850
2878static INLINE void 2851static uint32_t
2852sym_hash (pointer sym, uint32_t size)
2853{
2854 uintptr_t ptr = (uintptr_t)sym;
2855
2856#if 0
2857 /* table size is prime, so why mix */
2858 ptr += ptr >> 32;
2859 ptr += ptr >> 16;
2860 ptr += ptr >> 8;
2861#endif
2862
2863 return ptr % size;
2864}
2865
2866ecb_inline void
2879new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2867new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2880{ 2868{
2881 pointer slot = immutable_cons (variable, value); 2869 pointer slot = immutable_cons (variable, value);
2882 2870
2883 if (is_vector (car (env))) 2871 if (is_vector (car (env)))
2884 { 2872 {
2885 int location = hash_fn (symname (variable), veclength (car (env))); 2873 int location = sym_hash (variable, veclength (car (env)));
2886
2887 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2874 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2888 } 2875 }
2889 else 2876 else
2890 set_car (env, immutable_cons (slot, car (env))); 2877 set_car (env, immutable_cons (slot, car (env)));
2891} 2878}
2892 2879
2893static pointer 2880static pointer
2894find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2881find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2895{ 2882{
2896 pointer x, y; 2883 pointer x, y;
2897 int location;
2898 2884
2899 for (x = env; x != NIL; x = cdr (x)) 2885 for (x = env; x != NIL; x = cdr (x))
2900 { 2886 {
2901 if (is_vector (car (x))) 2887 if (is_vector (car (x)))
2902 { 2888 {
2903 location = hash_fn (symname (hdl), veclength (car (x))); 2889 int location = sym_hash (hdl, veclength (car (x)));
2904 y = vector_elem (car (x), location); 2890 y = vector_get (car (x), location);
2905 } 2891 }
2906 else 2892 else
2907 y = car (x); 2893 y = car (x);
2908 2894
2909 for (; y != NIL; y = cdr (y)) 2895 for (; y != NIL; y = cdr (y))
2910 if (caar (y) == hdl) 2896 if (caar (y) == hdl)
2911 break; 2897 break;
2912 2898
2913 if (y != NIL) 2899 if (y != NIL)
2900 return car (y);
2901
2902 if (!all)
2914 break; 2903 break;
2915
2916 if (!all)
2917 return NIL;
2918 } 2904 }
2919
2920 if (x != NIL)
2921 return car (y);
2922 2905
2923 return NIL; 2906 return NIL;
2924} 2907}
2925 2908
2926#else /* USE_ALIST_ENV */ 2909#else /* USE_ALIST_ENV */
2927 2910
2928static INLINE void 2911ecb_inline void
2929new_frame_in_env (SCHEME_P_ pointer old_env) 2912new_frame_in_env (SCHEME_P_ pointer old_env)
2930{ 2913{
2931 SCHEME_V->envir = immutable_cons (NIL, old_env); 2914 SCHEME_V->envir = immutable_cons (NIL, old_env);
2932 setenvironment (SCHEME_V->envir); 2915 setenvironment (SCHEME_V->envir);
2933} 2916}
2934 2917
2935static INLINE void 2918ecb_inline void
2936new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2919new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2937{ 2920{
2938 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2921 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2939} 2922}
2940 2923
2948 for (y = car (x); y != NIL; y = cdr (y)) 2931 for (y = car (x); y != NIL; y = cdr (y))
2949 if (caar (y) == hdl) 2932 if (caar (y) == hdl)
2950 break; 2933 break;
2951 2934
2952 if (y != NIL) 2935 if (y != NIL)
2936 return car (y);
2953 break; 2937 break;
2954 2938
2955 if (!all) 2939 if (!all)
2956 return NIL; 2940 break;
2957 } 2941 }
2958
2959 if (x != NIL)
2960 return car (y);
2961 2942
2962 return NIL; 2943 return NIL;
2963} 2944}
2964 2945
2965#endif /* USE_ALIST_ENV else */ 2946#endif /* USE_ALIST_ENV else */
2966 2947
2967static INLINE void 2948ecb_inline void
2968new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2949new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2969{ 2950{
2970 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2951 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2971} 2952}
2972 2953
2973static INLINE void 2954ecb_inline void
2974set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2955set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2975{ 2956{
2976 set_cdr (slot, value); 2957 set_cdr (slot, value);
2977} 2958}
2978 2959
2979static INLINE pointer 2960ecb_inline pointer
2980slot_value_in_env (pointer slot) 2961slot_value_in_env (pointer slot)
2981{ 2962{
2982 return cdr (slot); 2963 return cdr (slot);
2983} 2964}
2984 2965
2985/* ========== Evaluation Cycle ========== */ 2966/* ========== Evaluation Cycle ========== */
2986 2967
2987static pointer 2968static int
2988xError_1 (SCHEME_P_ const char *s, pointer a) 2969xError_1 (SCHEME_P_ const char *s, pointer a)
2989{ 2970{
2990#if USE_ERROR_HOOK 2971#if USE_ERROR_HOOK
2991 pointer x; 2972 pointer x;
2992 pointer hdl = SCHEME_V->ERROR_HOOK; 2973 pointer hdl = SCHEME_V->ERROR_HOOK;
3027 code = cons (mk_string (SCHEME_A_ s), code); 3008 code = cons (mk_string (SCHEME_A_ s), code);
3028 setimmutable (car (code)); 3009 setimmutable (car (code));
3029 SCHEME_V->code = cons (slot_value_in_env (x), code); 3010 SCHEME_V->code = cons (slot_value_in_env (x), code);
3030 SCHEME_V->op = OP_EVAL; 3011 SCHEME_V->op = OP_EVAL;
3031 3012
3032 return S_T; 3013 return 0;
3033 } 3014 }
3034#endif 3015#endif
3035 3016
3036 if (a) 3017 if (a)
3037 SCHEME_V->args = cons (a, NIL); 3018 SCHEME_V->args = cons (a, NIL);
3039 SCHEME_V->args = NIL; 3020 SCHEME_V->args = NIL;
3040 3021
3041 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); 3022 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args);
3042 setimmutable (car (SCHEME_V->args)); 3023 setimmutable (car (SCHEME_V->args));
3043 SCHEME_V->op = OP_ERR0; 3024 SCHEME_V->op = OP_ERR0;
3025
3044 return S_T; 3026 return 0;
3045} 3027}
3046 3028
3047#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) 3029#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a)
3048#define Error_0(s) Error_1 (s, 0) 3030#define Error_0(s) Error_1 (s, 0)
3049 3031
3050/* Too small to turn into function */ 3032/* Too small to turn into function */
3051#define BEGIN do { 3033#define BEGIN do {
3052#define END } while (0) 3034#define END } while (0)
3053#define s_goto(a) BEGIN \ 3035#define s_goto(a) BEGIN \
3054 SCHEME_V->op = a; \ 3036 SCHEME_V->op = a; \
3055 return S_T; END 3037 return 0; END
3056 3038
3057#define s_return(a) return xs_return (SCHEME_A_ a) 3039#define s_return(a) return xs_return (SCHEME_A_ a)
3058 3040
3059#ifndef USE_SCHEME_STACK 3041#ifndef USE_SCHEME_STACK
3060 3042
3085 next_frame = SCHEME_V->dump_base + nframes; 3067 next_frame = SCHEME_V->dump_base + nframes;
3086 3068
3087 next_frame->op = op; 3069 next_frame->op = op;
3088 next_frame->args = args; 3070 next_frame->args = args;
3089 next_frame->envir = SCHEME_V->envir; 3071 next_frame->envir = SCHEME_V->envir;
3090 next_frame->code = code; 3072 next_frame->code = code;
3091 3073
3092 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3074 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3093} 3075}
3094 3076
3095static pointer 3077static int
3096xs_return (SCHEME_P_ pointer a) 3078xs_return (SCHEME_P_ pointer a)
3097{ 3079{
3098 int nframes = (uintptr_t)SCHEME_V->dump; 3080 int nframes = (uintptr_t)SCHEME_V->dump;
3099 struct dump_stack_frame *frame; 3081 struct dump_stack_frame *frame;
3100 3082
3101 SCHEME_V->value = a; 3083 SCHEME_V->value = a;
3102 3084
3103 if (nframes <= 0) 3085 if (nframes <= 0)
3104 return NIL; 3086 return -1;
3105 3087
3106 frame = &SCHEME_V->dump_base[--nframes]; 3088 frame = &SCHEME_V->dump_base[--nframes];
3107 SCHEME_V->op = frame->op; 3089 SCHEME_V->op = frame->op;
3108 SCHEME_V->args = frame->args; 3090 SCHEME_V->args = frame->args;
3109 SCHEME_V->envir = frame->envir; 3091 SCHEME_V->envir = frame->envir;
3110 SCHEME_V->code = frame->code; 3092 SCHEME_V->code = frame->code;
3111 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3093 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3112 3094
3113 return S_T; 3095 return 0;
3114} 3096}
3115 3097
3116static INLINE void 3098ecb_inline void
3117dump_stack_reset (SCHEME_P) 3099dump_stack_reset (SCHEME_P)
3118{ 3100{
3119 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3101 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3120 SCHEME_V->dump = (pointer)+0; 3102 SCHEME_V->dump = (pointer)+0;
3121} 3103}
3122 3104
3123static INLINE void 3105ecb_inline void
3124dump_stack_initialize (SCHEME_P) 3106dump_stack_initialize (SCHEME_P)
3125{ 3107{
3126 SCHEME_V->dump_size = 0; 3108 SCHEME_V->dump_size = 0;
3127 SCHEME_V->dump_base = 0; 3109 SCHEME_V->dump_base = 0;
3128 dump_stack_reset (SCHEME_A); 3110 dump_stack_reset (SCHEME_A);
3181 int i = 0; 3163 int i = 0;
3182 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3164 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3183 3165
3184 while (cont != NIL) 3166 while (cont != NIL)
3185 { 3167 {
3186 frame->op = ivalue (car (cont)); cont = cdr (cont); 3168 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3187 frame->args = car (cont) ; cont = cdr (cont); 3169 frame->args = car (cont) ; cont = cdr (cont);
3188 frame->envir = car (cont) ; cont = cdr (cont); 3170 frame->envir = car (cont) ; cont = cdr (cont);
3189 frame->code = car (cont) ; cont = cdr (cont); 3171 frame->code = car (cont) ; cont = cdr (cont);
3190 3172
3191 ++frame; 3173 ++frame;
3192 ++i; 3174 ++i;
3193 } 3175 }
3194 3176
3195 SCHEME_V->dump = (pointer)(uintptr_t)i; 3177 SCHEME_V->dump = (pointer)(uintptr_t)i;
3196} 3178}
3197 3179
3198#else 3180#else
3199 3181
3200static INLINE void 3182ecb_inline void
3201dump_stack_reset (SCHEME_P) 3183dump_stack_reset (SCHEME_P)
3202{ 3184{
3203 SCHEME_V->dump = NIL; 3185 SCHEME_V->dump = NIL;
3204} 3186}
3205 3187
3206static INLINE void 3188ecb_inline void
3207dump_stack_initialize (SCHEME_P) 3189dump_stack_initialize (SCHEME_P)
3208{ 3190{
3209 dump_stack_reset (SCHEME_A); 3191 dump_stack_reset (SCHEME_A);
3210} 3192}
3211 3193
3213dump_stack_free (SCHEME_P) 3195dump_stack_free (SCHEME_P)
3214{ 3196{
3215 SCHEME_V->dump = NIL; 3197 SCHEME_V->dump = NIL;
3216} 3198}
3217 3199
3218static pointer 3200static int
3219xs_return (SCHEME_P_ pointer a) 3201xs_return (SCHEME_P_ pointer a)
3220{ 3202{
3221 pointer dump = SCHEME_V->dump; 3203 pointer dump = SCHEME_V->dump;
3222 3204
3223 SCHEME_V->value = a; 3205 SCHEME_V->value = a;
3224 3206
3225 if (dump == NIL) 3207 if (dump == NIL)
3226 return NIL; 3208 return -1;
3227 3209
3228 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3210 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3229 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3211 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3230 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3212 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3231 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3213 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3232 3214
3233 SCHEME_V->dump = dump; 3215 SCHEME_V->dump = dump;
3234 3216
3235 return S_T; 3217 return 0;
3236} 3218}
3237 3219
3238static void 3220static void
3239s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3221s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3240{ 3222{
3265 3247
3266#endif 3248#endif
3267 3249
3268#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3250#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3269 3251
3270static pointer 3252static int
3271opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3253opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3272{ 3254{
3255 pointer args = SCHEME_V->args;
3273 pointer x, y; 3256 pointer x, y;
3274 3257
3275 switch (op) 3258 switch (op)
3276 { 3259 {
3277 case OP_LOAD: /* load */ 3260 case OP_LOAD: /* load */
3278 if (file_interactive (SCHEME_A)) 3261 if (file_interactive (SCHEME_A))
3279 { 3262 {
3280 xwrstr ("Loading "); xwrstr (strvalue (car (SCHEME_V->args))); xwrstr ("\n"); 3263 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n");
3281 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (SCHEME_V->args))); 3264 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args)));
3282 } 3265 }
3283 3266
3284 if (!file_push (SCHEME_A_ strvalue (car (SCHEME_V->args)))) 3267 if (!file_push (SCHEME_A_ strvalue (car (args))))
3285 Error_1 ("unable to open", car (SCHEME_V->args)); 3268 Error_1 ("unable to open", car (args));
3286 else 3269 else
3287 { 3270 {
3288 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3271 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3289 s_goto (OP_T0LVL); 3272 s_goto (OP_T0LVL);
3290 } 3273 }
3364 case OP_EVAL: /* main part of evaluation */ 3347 case OP_EVAL: /* main part of evaluation */
3365#if USE_TRACING 3348#if USE_TRACING
3366 if (SCHEME_V->tracing) 3349 if (SCHEME_V->tracing)
3367 { 3350 {
3368 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ 3351 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */
3369 s_save (SCHEME_A_ OP_REAL_EVAL, SCHEME_V->args, SCHEME_V->code); 3352 s_save (SCHEME_A_ OP_REAL_EVAL, args, SCHEME_V->code);
3370 SCHEME_V->args = SCHEME_V->code; 3353 SCHEME_V->args = SCHEME_V->code;
3371 putstr (SCHEME_A_ "\nEval: "); 3354 putstr (SCHEME_A_ "\nEval: ");
3372 s_goto (OP_P0LIST); 3355 s_goto (OP_P0LIST);
3373 } 3356 }
3374 3357
3418 SCHEME_V->code = cdr (SCHEME_V->code); 3401 SCHEME_V->code = cdr (SCHEME_V->code);
3419 s_goto (OP_E1ARGS); 3402 s_goto (OP_E1ARGS);
3420 } 3403 }
3421 3404
3422 case OP_E1ARGS: /* eval arguments */ 3405 case OP_E1ARGS: /* eval arguments */
3423 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3406 args = cons (SCHEME_V->value, args);
3424 3407
3425 if (is_pair (SCHEME_V->code)) /* continue */ 3408 if (is_pair (SCHEME_V->code)) /* continue */
3426 { 3409 {
3427 s_save (SCHEME_A_ OP_E1ARGS, SCHEME_V->args, cdr (SCHEME_V->code)); 3410 s_save (SCHEME_A_ OP_E1ARGS, args, cdr (SCHEME_V->code));
3428 SCHEME_V->code = car (SCHEME_V->code); 3411 SCHEME_V->code = car (SCHEME_V->code);
3429 SCHEME_V->args = NIL; 3412 SCHEME_V->args = NIL;
3430 s_goto (OP_EVAL); 3413 s_goto (OP_EVAL);
3431 } 3414 }
3432 else /* end */ 3415 else /* end */
3433 { 3416 {
3434 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3417 args = reverse_in_place (SCHEME_A_ NIL, args);
3435 SCHEME_V->code = car (SCHEME_V->args); 3418 SCHEME_V->code = car (args);
3436 SCHEME_V->args = cdr (SCHEME_V->args); 3419 SCHEME_V->args = cdr (args);
3437 s_goto (OP_APPLY); 3420 s_goto (OP_APPLY);
3438 } 3421 }
3439 3422
3440#if USE_TRACING 3423#if USE_TRACING
3441 3424
3442 case OP_TRACING: 3425 case OP_TRACING:
3443 { 3426 {
3444 int tr = SCHEME_V->tracing; 3427 int tr = SCHEME_V->tracing;
3445 3428
3446 SCHEME_V->tracing = ivalue (car (SCHEME_V->args)); 3429 SCHEME_V->tracing = ivalue_unchecked (car (args));
3447 s_return (mk_integer (SCHEME_A_ tr)); 3430 s_return (mk_integer (SCHEME_A_ tr));
3448 } 3431 }
3449 3432
3450#endif 3433#endif
3451 3434
3452 case OP_APPLY: /* apply 'code' to 'args' */ 3435 case OP_APPLY: /* apply 'code' to 'args' */
3453#if USE_TRACING 3436#if USE_TRACING
3454 if (SCHEME_V->tracing) 3437 if (SCHEME_V->tracing)
3455 { 3438 {
3456 s_save (SCHEME_A_ OP_REAL_APPLY, SCHEME_V->args, SCHEME_V->code); 3439 s_save (SCHEME_A_ OP_REAL_APPLY, args, SCHEME_V->code);
3457 SCHEME_V->print_flag = 1; 3440 SCHEME_V->print_flag = 1;
3458 /* SCHEME_V->args=cons(SCHEME_V->code,SCHEME_V->args); */ 3441 /* args=cons(SCHEME_V->code,args); */
3459 putstr (SCHEME_A_ "\nApply to: "); 3442 putstr (SCHEME_A_ "\nApply to: ");
3460 s_goto (OP_P0LIST); 3443 s_goto (OP_P0LIST);
3461 } 3444 }
3462 3445
3463 /* fall through */ 3446 /* fall through */
3464 3447
3465 case OP_REAL_APPLY: 3448 case OP_REAL_APPLY:
3466#endif 3449#endif
3467 if (is_proc (SCHEME_V->code)) 3450 if (is_proc (SCHEME_V->code))
3468 {
3469 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3451 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3470 }
3471 else if (is_foreign (SCHEME_V->code)) 3452 else if (is_foreign (SCHEME_V->code))
3472 { 3453 {
3473 /* Keep nested calls from GC'ing the arglist */ 3454 /* Keep nested calls from GC'ing the arglist */
3474 push_recent_alloc (SCHEME_A_ SCHEME_V->args, NIL); 3455 push_recent_alloc (SCHEME_A_ args, NIL);
3475 x = SCHEME_V->code->object.ff (SCHEME_A_ SCHEME_V->args); 3456 x = SCHEME_V->code->object.ff (SCHEME_A_ args);
3476 3457
3477 s_return (x); 3458 s_return (x);
3478 } 3459 }
3479 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ 3460 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */
3480 { 3461 {
3481 /* Should not accept promise */ 3462 /* Should not accept promise */
3482 /* make environment */ 3463 /* make environment */
3483 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); 3464 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code));
3484 3465
3485 for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) 3466 for (x = car (closure_code (SCHEME_V->code)), y = args; is_pair (x); x = cdr (x), y = cdr (y))
3486 { 3467 {
3487 if (y == NIL) 3468 if (y == NIL)
3488 Error_0 ("not enough arguments"); 3469 Error_0 ("not enough arguments");
3489 else 3470 else
3490 new_slot_in_env (SCHEME_A_ car (x), car (y)); 3471 new_slot_in_env (SCHEME_A_ car (x), car (y));
3508 s_goto (OP_BEGIN); 3489 s_goto (OP_BEGIN);
3509 } 3490 }
3510 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ 3491 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */
3511 { 3492 {
3512 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); 3493 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code));
3513 s_return (SCHEME_V->args != NIL ? car (SCHEME_V->args) : NIL); 3494 s_return (args != NIL ? car (args) : NIL);
3514 } 3495 }
3515 else 3496 else
3516 Error_0 ("illegal function"); 3497 Error_0 ("illegal function");
3517 3498
3518 case OP_DOMACRO: /* do macro */ 3499 case OP_DOMACRO: /* do macro */
3527 { 3508 {
3528 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); 3509 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1);
3529 3510
3530 if (f != NIL) 3511 if (f != NIL)
3531 { 3512 {
3532 s_save (SCHEME_A_ OP_LAMBDA1, SCHEME_V->args, SCHEME_V->code); 3513 s_save (SCHEME_A_ OP_LAMBDA1, args, SCHEME_V->code);
3533 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3514 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3534 SCHEME_V->code = slot_value_in_env (f); 3515 SCHEME_V->code = slot_value_in_env (f);
3535 s_goto (OP_APPLY); 3516 s_goto (OP_APPLY);
3536 } 3517 }
3537 3518
3548 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); 3529 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir));
3549 3530
3550#endif 3531#endif
3551 3532
3552 case OP_MKCLOSURE: /* make-closure */ 3533 case OP_MKCLOSURE: /* make-closure */
3553 x = car (SCHEME_V->args); 3534 x = car (args);
3554 3535
3555 if (car (x) == SCHEME_V->LAMBDA) 3536 if (car (x) == SCHEME_V->LAMBDA)
3556 x = cdr (x); 3537 x = cdr (x);
3557 3538
3558 if (cdr (SCHEME_V->args) == NIL) 3539 if (cdr (args) == NIL)
3559 y = SCHEME_V->envir; 3540 y = SCHEME_V->envir;
3560 else 3541 else
3561 y = cadr (SCHEME_V->args); 3542 y = cadr (args);
3562 3543
3563 s_return (mk_closure (SCHEME_A_ x, y)); 3544 s_return (mk_closure (SCHEME_A_ x, y));
3564 3545
3565 case OP_QUOTE: /* quote */ 3546 case OP_QUOTE: /* quote */
3566 s_return (car (SCHEME_V->code)); 3547 s_return (car (SCHEME_V->code));
3598 3579
3599 3580
3600 case OP_DEFP: /* defined? */ 3581 case OP_DEFP: /* defined? */
3601 x = SCHEME_V->envir; 3582 x = SCHEME_V->envir;
3602 3583
3603 if (cdr (SCHEME_V->args) != NIL) 3584 if (cdr (args) != NIL)
3604 x = cadr (SCHEME_V->args); 3585 x = cadr (args);
3605 3586
3606 s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); 3587 s_retbool (find_slot_in_env (SCHEME_A_ x, car (args), 1) != NIL);
3607 3588
3608 case OP_SET0: /* set! */ 3589 case OP_SET0: /* set! */
3609 if (is_immutable (car (SCHEME_V->code))) 3590 if (is_immutable (car (SCHEME_V->code)))
3610 Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code)); 3591 Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code));
3611 3592
3642 3623
3643 case OP_IF1: /* if */ 3624 case OP_IF1: /* if */
3644 if (is_true (SCHEME_V->value)) 3625 if (is_true (SCHEME_V->value))
3645 SCHEME_V->code = car (SCHEME_V->code); 3626 SCHEME_V->code = car (SCHEME_V->code);
3646 else 3627 else
3647 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because 3628 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
3648
3649 * car(NIL) = NIL */
3650 s_goto (OP_EVAL); 3629 s_goto (OP_EVAL);
3651 3630
3652 case OP_LET0: /* let */ 3631 case OP_LET0: /* let */
3653 SCHEME_V->args = NIL; 3632 SCHEME_V->args = NIL;
3654 SCHEME_V->value = SCHEME_V->code; 3633 SCHEME_V->value = SCHEME_V->code;
3655 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); 3634 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code);
3656 s_goto (OP_LET1); 3635 s_goto (OP_LET1);
3657 3636
3658 case OP_LET1: /* let (calculate parameters) */ 3637 case OP_LET1: /* let (calculate parameters) */
3659 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3638 args = cons (SCHEME_V->value, args);
3660 3639
3661 if (is_pair (SCHEME_V->code)) /* continue */ 3640 if (is_pair (SCHEME_V->code)) /* continue */
3662 { 3641 {
3663 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3642 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3664 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); 3643 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code));
3665 3644
3666 s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); 3645 s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code));
3667 SCHEME_V->code = cadar (SCHEME_V->code); 3646 SCHEME_V->code = cadar (SCHEME_V->code);
3668 SCHEME_V->args = NIL; 3647 SCHEME_V->args = NIL;
3669 s_goto (OP_EVAL); 3648 s_goto (OP_EVAL);
3670 } 3649 }
3671 else /* end */ 3650 else /* end */
3672 { 3651 {
3673 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3652 args = reverse_in_place (SCHEME_A_ NIL, args);
3674 SCHEME_V->code = car (SCHEME_V->args); 3653 SCHEME_V->code = car (args);
3675 SCHEME_V->args = cdr (SCHEME_V->args); 3654 SCHEME_V->args = cdr (args);
3676 s_goto (OP_LET2); 3655 s_goto (OP_LET2);
3677 } 3656 }
3678 3657
3679 case OP_LET2: /* let */ 3658 case OP_LET2: /* let */
3680 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 3659 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3681 3660
3682 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; 3661 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args;
3683 y != NIL; x = cdr (x), y = cdr (y)) 3662 y != NIL; x = cdr (x), y = cdr (y))
3684 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3663 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3685 3664
3686 if (is_symbol (car (SCHEME_V->code))) /* named let */ 3665 if (is_symbol (car (SCHEME_V->code))) /* named let */
3687 { 3666 {
3688 for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) 3667 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x))
3689 { 3668 {
3690 if (!is_pair (x)) 3669 if (!is_pair (x))
3691 Error_1 ("Bad syntax of binding in let :", x); 3670 Error_1 ("Bad syntax of binding in let :", x);
3692 3671
3693 if (!is_list (SCHEME_A_ car (x))) 3672 if (!is_list (SCHEME_A_ car (x)))
3694 Error_1 ("Bad syntax of binding in let :", car (x)); 3673 Error_1 ("Bad syntax of binding in let :", car (x));
3695 3674
3696 SCHEME_V->args = cons (caar (x), SCHEME_V->args); 3675 args = cons (caar (x), args);
3697 } 3676 }
3698 3677
3699 x =
3700 mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args), cddr (SCHEME_V->code)), 3678 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)),
3701 SCHEME_V->envir); 3679 SCHEME_V->envir);
3702 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); 3680 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x);
3703 SCHEME_V->code = cddr (SCHEME_V->code); 3681 SCHEME_V->code = cddr (SCHEME_V->code);
3704 SCHEME_V->args = NIL;
3705 } 3682 }
3706 else 3683 else
3707 { 3684 {
3708 SCHEME_V->code = cdr (SCHEME_V->code); 3685 SCHEME_V->code = cdr (SCHEME_V->code);
3686 }
3687
3709 SCHEME_V->args = NIL; 3688 SCHEME_V->args = NIL;
3710 }
3711
3712 s_goto (OP_BEGIN); 3689 s_goto (OP_BEGIN);
3713 3690
3714 case OP_LET0AST: /* let* */ 3691 case OP_LET0AST: /* let* */
3715 if (car (SCHEME_V->code) == NIL) 3692 if (car (SCHEME_V->code) == NIL)
3716 { 3693 {
3734 new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value); 3711 new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value);
3735 SCHEME_V->code = cdr (SCHEME_V->code); 3712 SCHEME_V->code = cdr (SCHEME_V->code);
3736 3713
3737 if (is_pair (SCHEME_V->code)) /* continue */ 3714 if (is_pair (SCHEME_V->code)) /* continue */
3738 { 3715 {
3739 s_save (SCHEME_A_ OP_LET2AST, SCHEME_V->args, SCHEME_V->code); 3716 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code);
3740 SCHEME_V->code = cadar (SCHEME_V->code); 3717 SCHEME_V->code = cadar (SCHEME_V->code);
3741 SCHEME_V->args = NIL; 3718 SCHEME_V->args = NIL;
3742 s_goto (OP_EVAL); 3719 s_goto (OP_EVAL);
3743 } 3720 }
3744 else /* end */ 3721 else /* end */
3745 { 3722 {
3746 SCHEME_V->code = SCHEME_V->args; 3723 SCHEME_V->code = args;
3747 SCHEME_V->args = NIL; 3724 SCHEME_V->args = NIL;
3748 s_goto (OP_BEGIN); 3725 s_goto (OP_BEGIN);
3749 } 3726 }
3750 3727
3751 case OP_LET0REC: /* letrec */ 3728 case OP_LET0REC: /* letrec */
3754 SCHEME_V->value = SCHEME_V->code; 3731 SCHEME_V->value = SCHEME_V->code;
3755 SCHEME_V->code = car (SCHEME_V->code); 3732 SCHEME_V->code = car (SCHEME_V->code);
3756 s_goto (OP_LET1REC); 3733 s_goto (OP_LET1REC);
3757 3734
3758 case OP_LET1REC: /* letrec (calculate parameters) */ 3735 case OP_LET1REC: /* letrec (calculate parameters) */
3759 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3736 args = cons (SCHEME_V->value, args);
3760 3737
3761 if (is_pair (SCHEME_V->code)) /* continue */ 3738 if (is_pair (SCHEME_V->code)) /* continue */
3762 { 3739 {
3763 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3740 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3764 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); 3741 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3765 3742
3766 s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code)); 3743 s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code));
3767 SCHEME_V->code = cadar (SCHEME_V->code); 3744 SCHEME_V->code = cadar (SCHEME_V->code);
3768 SCHEME_V->args = NIL; 3745 SCHEME_V->args = NIL;
3769 s_goto (OP_EVAL); 3746 s_goto (OP_EVAL);
3770 } 3747 }
3771 else /* end */ 3748 else /* end */
3772 { 3749 {
3773 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3750 args = reverse_in_place (SCHEME_A_ NIL, args);
3774 SCHEME_V->code = car (SCHEME_V->args); 3751 SCHEME_V->code = car (args);
3775 SCHEME_V->args = cdr (SCHEME_V->args); 3752 SCHEME_V->args = cdr (args);
3776 s_goto (OP_LET2REC); 3753 s_goto (OP_LET2REC);
3777 } 3754 }
3778 3755
3779 case OP_LET2REC: /* letrec */ 3756 case OP_LET2REC: /* letrec */
3780 for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y)) 3757 for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y))
3781 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3758 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3782 3759
3783 SCHEME_V->code = cdr (SCHEME_V->code); 3760 SCHEME_V->code = cdr (SCHEME_V->code);
3784 SCHEME_V->args = NIL; 3761 SCHEME_V->args = NIL;
3785 s_goto (OP_BEGIN); 3762 s_goto (OP_BEGIN);
3871 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 3848 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3872 SCHEME_V->code = car (SCHEME_V->code); 3849 SCHEME_V->code = car (SCHEME_V->code);
3873 s_goto (OP_EVAL); 3850 s_goto (OP_EVAL);
3874 3851
3875 case OP_C1STREAM: /* cons-stream */ 3852 case OP_C1STREAM: /* cons-stream */
3876 SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register SCHEME_V->args for gc */ 3853 SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register args for gc */
3877 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); 3854 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir);
3878 set_typeflag (x, T_PROMISE); 3855 set_typeflag (x, T_PROMISE);
3879 s_return (cons (SCHEME_V->args, x)); 3856 s_return (cons (args, x));
3880 3857
3881 case OP_MACRO0: /* macro */ 3858 case OP_MACRO0: /* macro */
3882 if (is_pair (car (SCHEME_V->code))) 3859 if (is_pair (car (SCHEME_V->code)))
3883 { 3860 {
3884 x = caar (SCHEME_V->code); 3861 x = caar (SCHEME_V->code);
3917 { 3894 {
3918 if (!is_pair (y = caar (x))) 3895 if (!is_pair (y = caar (x)))
3919 break; 3896 break;
3920 3897
3921 for (; y != NIL; y = cdr (y)) 3898 for (; y != NIL; y = cdr (y))
3922 {
3923 if (eqv (car (y), SCHEME_V->value)) 3899 if (eqv (car (y), SCHEME_V->value))
3924 break; 3900 break;
3925 }
3926 3901
3927 if (y != NIL) 3902 if (y != NIL)
3928 break; 3903 break;
3929 } 3904 }
3930 3905
3950 s_goto (OP_BEGIN); 3925 s_goto (OP_BEGIN);
3951 else 3926 else
3952 s_return (NIL); 3927 s_return (NIL);
3953 3928
3954 case OP_PAPPLY: /* apply */ 3929 case OP_PAPPLY: /* apply */
3955 SCHEME_V->code = car (SCHEME_V->args); 3930 SCHEME_V->code = car (args);
3956 SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); 3931 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
3957 /*SCHEME_V->args = cadr(SCHEME_V->args); */ 3932 /*SCHEME_V->args = cadr(args); */
3958 s_goto (OP_APPLY); 3933 s_goto (OP_APPLY);
3959 3934
3960 case OP_PEVAL: /* eval */ 3935 case OP_PEVAL: /* eval */
3961 if (cdr (SCHEME_V->args) != NIL) 3936 if (cdr (args) != NIL)
3962 SCHEME_V->envir = cadr (SCHEME_V->args); 3937 SCHEME_V->envir = cadr (args);
3963 3938
3964 SCHEME_V->code = car (SCHEME_V->args); 3939 SCHEME_V->code = car (args);
3965 s_goto (OP_EVAL); 3940 s_goto (OP_EVAL);
3966 3941
3967 case OP_CONTINUATION: /* call-with-current-continuation */ 3942 case OP_CONTINUATION: /* call-with-current-continuation */
3968 SCHEME_V->code = car (SCHEME_V->args); 3943 SCHEME_V->code = car (args);
3969 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); 3944 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
3970 s_goto (OP_APPLY); 3945 s_goto (OP_APPLY);
3971 } 3946 }
3972 3947
3973 return S_T; 3948 if (USE_ERROR_CHECKING) abort ();
3974} 3949}
3975 3950
3976static pointer 3951static int
3977opexe_2 (SCHEME_P_ enum scheme_opcodes op) 3952opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3978{ 3953{
3979 pointer x; 3954 pointer args = SCHEME_V->args;
3955 pointer x = car (args);
3980 num v; 3956 num v;
3981 3957
3958 switch (op)
3959 {
3982#if USE_MATH 3960#if USE_MATH
3983 RVALUE dd;
3984#endif
3985
3986 switch (op)
3987 {
3988#if USE_MATH
3989
3990 case OP_INEX2EX: /* inexact->exact */ 3961 case OP_INEX2EX: /* inexact->exact */
3991 x = car (SCHEME_V->args); 3962 {
3992
3993 if (num_is_integer (x)) 3963 if (is_integer (x))
3994 s_return (x); 3964 s_return (x);
3995 else if (modf (rvalue_unchecked (x), &dd) == 0.0) 3965
3966 RVALUE r = rvalue_unchecked (x);
3967
3968 if (r == (RVALUE)(IVALUE)r)
3996 s_return (mk_integer (SCHEME_A_ ivalue (x))); 3969 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3997 else 3970 else
3998 Error_1 ("inexact->exact: not integral:", x); 3971 Error_1 ("inexact->exact: not integral:", x);
3972 }
3999 3973
4000 case OP_EXP:
4001 x = car (SCHEME_V->args);
4002 s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 3974 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
4003
4004 case OP_LOG:
4005 x = car (SCHEME_V->args);
4006 s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 3975 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))));
4007
4008 case OP_SIN:
4009 x = car (SCHEME_V->args);
4010 s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 3976 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
4011
4012 case OP_COS:
4013 x = car (SCHEME_V->args);
4014 s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 3977 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
4015
4016 case OP_TAN:
4017 x = car (SCHEME_V->args);
4018 s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); 3978 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x))));
4019
4020 case OP_ASIN:
4021 x = car (SCHEME_V->args);
4022 s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); 3979 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x))));
4023
4024 case OP_ACOS:
4025 x = car (SCHEME_V->args);
4026 s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); 3980 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x))));
4027 3981
4028 case OP_ATAN: 3982 case OP_ATAN:
4029 x = car (SCHEME_V->args);
4030
4031 if (cdr (SCHEME_V->args) == NIL) 3983 if (cdr (args) == NIL)
4032 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 3984 s_return (mk_real (SCHEME_A_ atan (rvalue (x))));
4033 else 3985 else
4034 { 3986 {
4035 pointer y = cadr (SCHEME_V->args); 3987 pointer y = cadr (args);
4036
4037 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); 3988 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
4038 } 3989 }
4039 3990
4040 case OP_SQRT: 3991 case OP_SQRT:
4041 x = car (SCHEME_V->args);
4042 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); 3992 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4043 3993
4044 case OP_EXPT: 3994 case OP_EXPT:
4045 { 3995 {
4046 RVALUE result; 3996 RVALUE result;
4047 int real_result = 1; 3997 int real_result = 1;
4048 pointer y = cadr (SCHEME_V->args); 3998 pointer y = cadr (args);
4049 3999
4050 x = car (SCHEME_V->args);
4051
4052 if (num_is_integer (x) && num_is_integer (y)) 4000 if (is_integer (x) && is_integer (y))
4053 real_result = 0; 4001 real_result = 0;
4054 4002
4055 /* This 'if' is an R5RS compatibility fix. */ 4003 /* This 'if' is an R5RS compatibility fix. */
4056 /* NOTE: Remove this 'if' fix for R6RS. */ 4004 /* NOTE: Remove this 'if' fix for R6RS. */
4057 if (rvalue (x) == 0 && rvalue (y) < 0) 4005 if (rvalue (x) == 0 && rvalue (y) < 0)
4058 result = 0.0; 4006 result = 0;
4059 else 4007 else
4060 result = pow (rvalue (x), rvalue (y)); 4008 result = pow (rvalue (x), rvalue (y));
4061 4009
4062 /* Before returning integer result make sure we can. */ 4010 /* Before returning integer result make sure we can. */
4063 /* If the test fails, result is too big for integer. */ 4011 /* If the test fails, result is too big for integer. */
4064 if (!real_result) 4012 if (!real_result)
4065 { 4013 {
4066 long result_as_long = (long) result; 4014 long result_as_long = result;
4067 4015
4068 if (result != (RVALUE) result_as_long) 4016 if (result != result_as_long)
4069 real_result = 1; 4017 real_result = 1;
4070 } 4018 }
4071 4019
4072 if (real_result) 4020 if (real_result)
4073 s_return (mk_real (SCHEME_A_ result)); 4021 s_return (mk_real (SCHEME_A_ result));
4074 else 4022 else
4075 s_return (mk_integer (SCHEME_A_ result)); 4023 s_return (mk_integer (SCHEME_A_ result));
4076 } 4024 }
4077 4025
4078 case OP_FLOOR:
4079 x = car (SCHEME_V->args);
4080 s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4026 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4081
4082 case OP_CEILING:
4083 x = car (SCHEME_V->args);
4084 s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); 4027 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4085 4028
4086 case OP_TRUNCATE: 4029 case OP_TRUNCATE:
4087 { 4030 {
4088 RVALUE rvalue_of_x; 4031 RVALUE n = rvalue (x);
4089
4090 x = car (SCHEME_V->args);
4091 rvalue_of_x = rvalue (x);
4092
4093 if (rvalue_of_x > 0)
4094 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); 4032 s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n)));
4095 else
4096 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
4097 } 4033 }
4098 4034
4099 case OP_ROUND: 4035 case OP_ROUND:
4100 x = car (SCHEME_V->args);
4101
4102 if (num_is_integer (x)) 4036 if (is_integer (x))
4103 s_return (x); 4037 s_return (x);
4104 4038
4105 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4039 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4106#endif 4040#endif
4107 4041
4108 case OP_ADD: /* + */ 4042 case OP_ADD: /* + */
4109 v = num_zero; 4043 v = num_zero;
4110 4044
4111 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4045 for (x = args; x != NIL; x = cdr (x))
4112 v = num_add (v, nvalue (car (x))); 4046 v = num_op (NUM_ADD, v, nvalue (car (x)));
4113 4047
4114 s_return (mk_number (SCHEME_A_ v)); 4048 s_return (mk_number (SCHEME_A_ v));
4115 4049
4116 case OP_MUL: /* * */ 4050 case OP_MUL: /* * */
4117 v = num_one; 4051 v = num_one;
4118 4052
4119 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4053 for (x = args; x != NIL; x = cdr (x))
4120 v = num_mul (v, nvalue (car (x))); 4054 v = num_op (NUM_MUL, v, nvalue (car (x)));
4121 4055
4122 s_return (mk_number (SCHEME_A_ v)); 4056 s_return (mk_number (SCHEME_A_ v));
4123 4057
4124 case OP_SUB: /* - */ 4058 case OP_SUB: /* - */
4125 if (cdr (SCHEME_V->args) == NIL) 4059 if (cdr (args) == NIL)
4126 { 4060 {
4127 x = SCHEME_V->args; 4061 x = args;
4128 v = num_zero; 4062 v = num_zero;
4129 } 4063 }
4130 else 4064 else
4131 { 4065 {
4132 x = cdr (SCHEME_V->args); 4066 x = cdr (args);
4133 v = nvalue (car (SCHEME_V->args)); 4067 v = nvalue (car (args));
4134 } 4068 }
4135 4069
4136 for (; x != NIL; x = cdr (x)) 4070 for (; x != NIL; x = cdr (x))
4137 v = num_sub (v, nvalue (car (x))); 4071 v = num_op (NUM_SUB, v, nvalue (car (x)));
4138 4072
4139 s_return (mk_number (SCHEME_A_ v)); 4073 s_return (mk_number (SCHEME_A_ v));
4140 4074
4141 case OP_DIV: /* / */ 4075 case OP_DIV: /* / */
4142 if (cdr (SCHEME_V->args) == NIL) 4076 if (cdr (args) == NIL)
4143 { 4077 {
4144 x = SCHEME_V->args; 4078 x = args;
4145 v = num_one; 4079 v = num_one;
4146 } 4080 }
4147 else 4081 else
4148 { 4082 {
4149 x = cdr (SCHEME_V->args); 4083 x = cdr (args);
4150 v = nvalue (car (SCHEME_V->args)); 4084 v = nvalue (car (args));
4151 } 4085 }
4152 4086
4153 for (; x != NIL; x = cdr (x)) 4087 for (; x != NIL; x = cdr (x))
4154 {
4155 if (!is_zero_rvalue (rvalue (car (x)))) 4088 if (!is_zero_rvalue (rvalue (car (x))))
4156 v = num_div (v, nvalue (car (x))); 4089 v = num_div (v, nvalue (car (x)));
4157 else 4090 else
4158 Error_0 ("/: division by zero"); 4091 Error_0 ("/: division by zero");
4159 }
4160 4092
4161 s_return (mk_number (SCHEME_A_ v)); 4093 s_return (mk_number (SCHEME_A_ v));
4162 4094
4163 case OP_INTDIV: /* quotient */ 4095 case OP_INTDIV: /* quotient */
4164 if (cdr (SCHEME_V->args) == NIL) 4096 if (cdr (args) == NIL)
4165 { 4097 {
4166 x = SCHEME_V->args; 4098 x = args;
4167 v = num_one; 4099 v = num_one;
4168 } 4100 }
4169 else 4101 else
4170 { 4102 {
4171 x = cdr (SCHEME_V->args); 4103 x = cdr (args);
4172 v = nvalue (car (SCHEME_V->args)); 4104 v = nvalue (car (args));
4173 } 4105 }
4174 4106
4175 for (; x != NIL; x = cdr (x)) 4107 for (; x != NIL; x = cdr (x))
4176 { 4108 {
4177 if (ivalue (car (x)) != 0) 4109 if (ivalue (car (x)) != 0)
4178 v = num_intdiv (v, nvalue (car (x))); 4110 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4179 else 4111 else
4180 Error_0 ("quotient: division by zero"); 4112 Error_0 ("quotient: division by zero");
4181 } 4113 }
4182 4114
4183 s_return (mk_number (SCHEME_A_ v)); 4115 s_return (mk_number (SCHEME_A_ v));
4184 4116
4185 case OP_REM: /* remainder */ 4117 case OP_REM: /* remainder */
4186 v = nvalue (car (SCHEME_V->args)); 4118 v = nvalue (x);
4187 4119
4188 if (ivalue (cadr (SCHEME_V->args)) != 0) 4120 if (ivalue (cadr (args)) != 0)
4189 v = num_rem (v, nvalue (cadr (SCHEME_V->args))); 4121 v = num_rem (v, nvalue (cadr (args)));
4190 else 4122 else
4191 Error_0 ("remainder: division by zero"); 4123 Error_0 ("remainder: division by zero");
4192 4124
4193 s_return (mk_number (SCHEME_A_ v)); 4125 s_return (mk_number (SCHEME_A_ v));
4194 4126
4195 case OP_MOD: /* modulo */ 4127 case OP_MOD: /* modulo */
4196 v = nvalue (car (SCHEME_V->args)); 4128 v = nvalue (x);
4197 4129
4198 if (ivalue (cadr (SCHEME_V->args)) != 0) 4130 if (ivalue (cadr (args)) != 0)
4199 v = num_mod (v, nvalue (cadr (SCHEME_V->args))); 4131 v = num_mod (v, nvalue (cadr (args)));
4200 else 4132 else
4201 Error_0 ("modulo: division by zero"); 4133 Error_0 ("modulo: division by zero");
4202 4134
4203 s_return (mk_number (SCHEME_A_ v)); 4135 s_return (mk_number (SCHEME_A_ v));
4204 4136
4205 case OP_CAR: /* car */ 4137 case OP_CAR: /* car */
4206 s_return (caar (SCHEME_V->args)); 4138 s_return (caar (args));
4207 4139
4208 case OP_CDR: /* cdr */ 4140 case OP_CDR: /* cdr */
4209 s_return (cdar (SCHEME_V->args)); 4141 s_return (cdar (args));
4210 4142
4211 case OP_CONS: /* cons */ 4143 case OP_CONS: /* cons */
4212 set_cdr (SCHEME_V->args, cadr (SCHEME_V->args)); 4144 set_cdr (args, cadr (args));
4213 s_return (SCHEME_V->args); 4145 s_return (args);
4214 4146
4215 case OP_SETCAR: /* set-car! */ 4147 case OP_SETCAR: /* set-car! */
4216 if (!is_immutable (car (SCHEME_V->args))) 4148 if (!is_immutable (x))
4217 { 4149 {
4218 set_car (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4150 set_car (x, cadr (args));
4219 s_return (car (SCHEME_V->args)); 4151 s_return (car (args));
4220 } 4152 }
4221 else 4153 else
4222 Error_0 ("set-car!: unable to alter immutable pair"); 4154 Error_0 ("set-car!: unable to alter immutable pair");
4223 4155
4224 case OP_SETCDR: /* set-cdr! */ 4156 case OP_SETCDR: /* set-cdr! */
4225 if (!is_immutable (car (SCHEME_V->args))) 4157 if (!is_immutable (x))
4226 { 4158 {
4227 set_cdr (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4159 set_cdr (x, cadr (args));
4228 s_return (car (SCHEME_V->args)); 4160 s_return (car (args));
4229 } 4161 }
4230 else 4162 else
4231 Error_0 ("set-cdr!: unable to alter immutable pair"); 4163 Error_0 ("set-cdr!: unable to alter immutable pair");
4232 4164
4233 case OP_CHAR2INT: /* char->integer */ 4165 case OP_CHAR2INT: /* char->integer */
4234 s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4166 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4235 4167
4236 case OP_INT2CHAR: /* integer->char */ 4168 case OP_INT2CHAR: /* integer->char */
4237 s_return (mk_character (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4169 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4238 4170
4239 case OP_CHARUPCASE: 4171 case OP_CHARUPCASE:
4240 { 4172 {
4241 unsigned char c = ivalue (car (SCHEME_V->args)); 4173 unsigned char c = ivalue_unchecked (x);
4242 c = toupper (c); 4174 c = toupper (c);
4243 s_return (mk_character (SCHEME_A_ c)); 4175 s_return (mk_character (SCHEME_A_ c));
4244 } 4176 }
4245 4177
4246 case OP_CHARDNCASE: 4178 case OP_CHARDNCASE:
4247 { 4179 {
4248 unsigned char c = ivalue (car (SCHEME_V->args)); 4180 unsigned char c = ivalue_unchecked (x);
4249 c = tolower (c); 4181 c = tolower (c);
4250 s_return (mk_character (SCHEME_A_ c)); 4182 s_return (mk_character (SCHEME_A_ c));
4251 } 4183 }
4252 4184
4253 case OP_STR2SYM: /* string->symbol */ 4185 case OP_STR2SYM: /* string->symbol */
4254 s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); 4186 s_return (mk_symbol (SCHEME_A_ strvalue (x)));
4255 4187
4256 case OP_STR2ATOM: /* string->atom */ 4188 case OP_STR2ATOM: /* string->atom */
4257 { 4189 {
4258 char *s = strvalue (car (SCHEME_V->args)); 4190 char *s = strvalue (x);
4259 long pf = 0; 4191 long pf = 0;
4260 4192
4261 if (cdr (SCHEME_V->args) != NIL) 4193 if (cdr (args) != NIL)
4262 { 4194 {
4263 /* we know cadr(SCHEME_V->args) is a natural number */ 4195 /* we know cadr(args) is a natural number */
4264 /* see if it is 2, 8, 10, or 16, or error */ 4196 /* see if it is 2, 8, 10, or 16, or error */
4265 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4197 pf = ivalue_unchecked (cadr (args));
4266 4198
4267 if (pf == 16 || pf == 10 || pf == 8 || pf == 2) 4199 if (pf == 16 || pf == 10 || pf == 8 || pf == 2)
4268 { 4200 {
4269 /* base is OK */ 4201 /* base is OK */
4270 } 4202 }
4271 else 4203 else
4272 pf = -1; 4204 pf = -1;
4273 } 4205 }
4274 4206
4275 if (pf < 0) 4207 if (pf < 0)
4276 Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); 4208 Error_1 ("string->atom: bad base:", cadr (args));
4277 else if (*s == '#') /* no use of base! */ 4209 else if (*s == '#') /* no use of base! */
4278 s_return (mk_sharp_const (SCHEME_A_ s + 1)); 4210 s_return (mk_sharp_const (SCHEME_A_ s + 1));
4279 else 4211 else
4280 { 4212 {
4281 if (pf == 0 || pf == 10) 4213 if (pf == 0 || pf == 10)
4292 } 4224 }
4293 } 4225 }
4294 } 4226 }
4295 4227
4296 case OP_SYM2STR: /* symbol->string */ 4228 case OP_SYM2STR: /* symbol->string */
4297 x = mk_string (SCHEME_A_ symname (car (SCHEME_V->args))); 4229 x = mk_string (SCHEME_A_ symname (x));
4298 setimmutable (x); 4230 setimmutable (x);
4299 s_return (x); 4231 s_return (x);
4300 4232
4301 case OP_ATOM2STR: /* atom->string */ 4233 case OP_ATOM2STR: /* atom->string */
4302 { 4234 {
4303 long pf = 0; 4235 long pf = 0;
4304 4236
4305 x = car (SCHEME_V->args);
4306
4307 if (cdr (SCHEME_V->args) != NIL) 4237 if (cdr (args) != NIL)
4308 { 4238 {
4309 /* we know cadr(SCHEME_V->args) is a natural number */ 4239 /* we know cadr(args) is a natural number */
4310 /* see if it is 2, 8, 10, or 16, or error */ 4240 /* see if it is 2, 8, 10, or 16, or error */
4311 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4241 pf = ivalue_unchecked (cadr (args));
4312 4242
4313 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) 4243 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2))
4314 { 4244 {
4315 /* base is OK */ 4245 /* base is OK */
4316 } 4246 }
4317 else 4247 else
4318 pf = -1; 4248 pf = -1;
4319 } 4249 }
4320 4250
4321 if (pf < 0) 4251 if (pf < 0)
4322 Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); 4252 Error_1 ("atom->string: bad base:", cadr (args));
4323 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) 4253 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x))
4324 { 4254 {
4325 char *p; 4255 char *p;
4326 int len; 4256 int len;
4327 4257
4332 Error_1 ("atom->string: not an atom:", x); 4262 Error_1 ("atom->string: not an atom:", x);
4333 } 4263 }
4334 4264
4335 case OP_MKSTRING: /* make-string */ 4265 case OP_MKSTRING: /* make-string */
4336 { 4266 {
4337 int fill = ' '; 4267 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4338 int len; 4268 int len = ivalue_unchecked (x);
4339 4269
4340 len = ivalue (car (SCHEME_V->args));
4341
4342 if (cdr (SCHEME_V->args) != NIL)
4343 fill = charvalue (cadr (SCHEME_V->args));
4344
4345 s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); 4270 s_return (mk_empty_string (SCHEME_A_ len, fill));
4346 } 4271 }
4347 4272
4348 case OP_STRLEN: /* string-length */ 4273 case OP_STRLEN: /* string-length */
4349 s_return (mk_integer (SCHEME_A_ strlength (car (SCHEME_V->args)))); 4274 s_return (mk_integer (SCHEME_A_ strlength (x)));
4350 4275
4351 case OP_STRREF: /* string-ref */ 4276 case OP_STRREF: /* string-ref */
4352 { 4277 {
4353 char *str; 4278 char *str = strvalue (x);
4354 int index;
4355
4356 str = strvalue (car (SCHEME_V->args));
4357
4358 index = ivalue (cadr (SCHEME_V->args)); 4279 int index = ivalue_unchecked (cadr (args));
4359 4280
4360 if (index >= strlength (car (SCHEME_V->args))) 4281 if (index >= strlength (x))
4361 Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); 4282 Error_1 ("string-ref: out of bounds:", cadr (args));
4362 4283
4363 s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); 4284 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4364 } 4285 }
4365 4286
4366 case OP_STRSET: /* string-set! */ 4287 case OP_STRSET: /* string-set! */
4367 { 4288 {
4368 char *str; 4289 char *str = strvalue (x);
4369 int index; 4290 int index = ivalue_unchecked (cadr (args));
4370 int c; 4291 int c;
4371 4292
4372 if (is_immutable (car (SCHEME_V->args))) 4293 if (is_immutable (x))
4373 Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); 4294 Error_1 ("string-set!: unable to alter immutable string:", x);
4374 4295
4375 str = strvalue (car (SCHEME_V->args));
4376
4377 index = ivalue (cadr (SCHEME_V->args));
4378
4379 if (index >= strlength (car (SCHEME_V->args))) 4296 if (index >= strlength (x))
4380 Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); 4297 Error_1 ("string-set!: out of bounds:", cadr (args));
4381 4298
4382 c = charvalue (caddr (SCHEME_V->args)); 4299 c = charvalue (caddr (args));
4383 4300
4384 str[index] = (char) c; 4301 str[index] = c;
4385 s_return (car (SCHEME_V->args)); 4302 s_return (car (args));
4386 } 4303 }
4387 4304
4388 case OP_STRAPPEND: /* string-append */ 4305 case OP_STRAPPEND: /* string-append */
4389 { 4306 {
4390 /* in 1.29 string-append was in Scheme in init.scm but was too slow */ 4307 /* in 1.29 string-append was in Scheme in init.scm but was too slow */
4391 int len = 0; 4308 int len = 0;
4392 pointer newstr; 4309 pointer newstr;
4393 char *pos; 4310 char *pos;
4394 4311
4395 /* compute needed length for new string */ 4312 /* compute needed length for new string */
4396 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4313 for (x = args; x != NIL; x = cdr (x))
4397 len += strlength (car (x)); 4314 len += strlength (car (x));
4398 4315
4399 newstr = mk_empty_string (SCHEME_A_ len, ' '); 4316 newstr = mk_empty_string (SCHEME_A_ len, ' ');
4400 4317
4401 /* store the contents of the argument strings into the new string */ 4318 /* store the contents of the argument strings into the new string */
4402 for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x)) 4319 for (pos = strvalue (newstr), x = args; x != NIL; pos += strlength (car (x)), x = cdr (x))
4403 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4320 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4404 4321
4405 s_return (newstr); 4322 s_return (newstr);
4406 } 4323 }
4407 4324
4408 case OP_SUBSTR: /* substring */ 4325 case OP_SUBSTR: /* substring */
4409 { 4326 {
4410 char *str; 4327 char *str = strvalue (x);
4411 int index0; 4328 int index0 = ivalue_unchecked (cadr (args));
4412 int index1; 4329 int index1;
4413 int len; 4330 int len;
4414 4331
4415 str = strvalue (car (SCHEME_V->args));
4416
4417 index0 = ivalue (cadr (SCHEME_V->args));
4418
4419 if (index0 > strlength (car (SCHEME_V->args))) 4332 if (index0 > strlength (x))
4420 Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); 4333 Error_1 ("substring: start out of bounds:", cadr (args));
4421 4334
4422 if (cddr (SCHEME_V->args) != NIL) 4335 if (cddr (args) != NIL)
4423 { 4336 {
4424 index1 = ivalue (caddr (SCHEME_V->args)); 4337 index1 = ivalue_unchecked (caddr (args));
4425 4338
4426 if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) 4339 if (index1 > strlength (x) || index1 < index0)
4427 Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); 4340 Error_1 ("substring: end out of bounds:", caddr (args));
4428 } 4341 }
4429 else 4342 else
4430 index1 = strlength (car (SCHEME_V->args)); 4343 index1 = strlength (x);
4431 4344
4432 len = index1 - index0; 4345 len = index1 - index0;
4433 x = mk_empty_string (SCHEME_A_ len, ' '); 4346 x = mk_empty_string (SCHEME_A_ len, ' ');
4434 memcpy (strvalue (x), str + index0, len); 4347 memcpy (strvalue (x), str + index0, len);
4435 strvalue (x)[len] = 0; 4348 strvalue (x)[len] = 0;
4439 4352
4440 case OP_VECTOR: /* vector */ 4353 case OP_VECTOR: /* vector */
4441 { 4354 {
4442 int i; 4355 int i;
4443 pointer vec; 4356 pointer vec;
4444 int len = list_length (SCHEME_A_ SCHEME_V->args); 4357 int len = list_length (SCHEME_A_ args);
4445 4358
4446 if (len < 0) 4359 if (len < 0)
4447 Error_1 ("vector: not a proper list:", SCHEME_V->args); 4360 Error_1 ("vector: not a proper list:", args);
4448 4361
4449 vec = mk_vector (SCHEME_A_ len); 4362 vec = mk_vector (SCHEME_A_ len);
4450 4363
4451#if USE_ERROR_CHECKING 4364#if USE_ERROR_CHECKING
4452 if (SCHEME_V->no_memory) 4365 if (SCHEME_V->no_memory)
4453 s_return (S_SINK); 4366 s_return (S_SINK);
4454#endif 4367#endif
4455 4368
4456 for (x = SCHEME_V->args, i = 0; is_pair (x); x = cdr (x), i++) 4369 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4457 set_vector_elem (vec, i, car (x)); 4370 vector_set (vec, i, car (x));
4458 4371
4459 s_return (vec); 4372 s_return (vec);
4460 } 4373 }
4461 4374
4462 case OP_MKVECTOR: /* make-vector */ 4375 case OP_MKVECTOR: /* make-vector */
4463 { 4376 {
4464 pointer fill = NIL; 4377 pointer fill = NIL;
4465 int len;
4466 pointer vec; 4378 pointer vec;
4379 int len = ivalue_unchecked (x);
4467 4380
4468 len = ivalue (car (SCHEME_V->args));
4469
4470 if (cdr (SCHEME_V->args) != NIL) 4381 if (cdr (args) != NIL)
4471 fill = cadr (SCHEME_V->args); 4382 fill = cadr (args);
4472 4383
4473 vec = mk_vector (SCHEME_A_ len); 4384 vec = mk_vector (SCHEME_A_ len);
4474 4385
4475#if USE_ERROR_CHECKING 4386#if USE_ERROR_CHECKING
4476 if (SCHEME_V->no_memory) 4387 if (SCHEME_V->no_memory)
4477 s_return (S_SINK); 4388 s_return (S_SINK);
4478#endif 4389#endif
4479 4390
4480 if (fill != NIL) 4391 if (fill != NIL)
4481 fill_vector (vec, fill); 4392 fill_vector (vec, 0, fill);
4482 4393
4483 s_return (vec); 4394 s_return (vec);
4484 } 4395 }
4485 4396
4486 case OP_VECLEN: /* vector-length */ 4397 case OP_VECLEN: /* vector-length */
4487 s_return (mk_integer (SCHEME_A_ veclength (car (SCHEME_V->args)))); 4398 s_return (mk_integer (SCHEME_A_ veclength (x)));
4488 4399
4489 case OP_VECREF: /* vector-ref */ 4400 case OP_VECREF: /* vector-ref */
4490 { 4401 {
4491 int index;
4492
4493 index = ivalue (cadr (SCHEME_V->args)); 4402 int index = ivalue_unchecked (cadr (args));
4494 4403
4495 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) 4404 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4496 Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); 4405 Error_1 ("vector-ref: out of bounds:", cadr (args));
4497 4406
4498 s_return (vector_elem (car (SCHEME_V->args), index)); 4407 s_return (vector_get (x, index));
4499 } 4408 }
4500 4409
4501 case OP_VECSET: /* vector-set! */ 4410 case OP_VECSET: /* vector-set! */
4502 { 4411 {
4503 int index; 4412 int index = ivalue_unchecked (cadr (args));
4504 4413
4505 if (is_immutable (car (SCHEME_V->args))) 4414 if (is_immutable (x))
4506 Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); 4415 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4507 4416
4508 index = ivalue (cadr (SCHEME_V->args));
4509
4510 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) 4417 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4511 Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); 4418 Error_1 ("vector-set!: out of bounds:", cadr (args));
4512 4419
4513 set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); 4420 vector_set (x, index, caddr (args));
4514 s_return (car (SCHEME_V->args)); 4421 s_return (x);
4515 } 4422 }
4516 } 4423 }
4517 4424
4518 return S_T; 4425 if (USE_ERROR_CHECKING) abort ();
4519} 4426}
4520 4427
4521INTERFACE int 4428static int
4522is_list (SCHEME_P_ pointer a) 4429opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4523{ 4430{
4524 return list_length (SCHEME_A_ a) >= 0; 4431 pointer x = SCHEME_V->args;
4525}
4526 4432
4527/* Result is: 4433 for (;;)
4528 proper list: length
4529 circular list: -1
4530 not even a pair: -2
4531 dotted list: -2 minus length before dot
4532*/
4533INTERFACE int
4534list_length (SCHEME_P_ pointer a)
4535{
4536 int i = 0;
4537 pointer slow, fast;
4538
4539 slow = fast = a;
4540
4541 while (1)
4542 { 4434 {
4435 num v = nvalue (car (x));
4436 x = cdr (x);
4437
4543 if (fast == NIL) 4438 if (x == NIL)
4544 return i; 4439 break;
4545 4440
4546 if (!is_pair (fast)) 4441 int r = num_cmp (v, nvalue (car (x)));
4547 return -2 - i;
4548 4442
4549 fast = cdr (fast); 4443 switch (op)
4550 ++i;
4551
4552 if (fast == NIL)
4553 return i;
4554
4555 if (!is_pair (fast))
4556 return -2 - i;
4557
4558 ++i;
4559 fast = cdr (fast);
4560
4561 /* Safe because we would have already returned if `fast'
4562 encountered a non-pair. */
4563 slow = cdr (slow);
4564
4565 if (fast == slow)
4566 { 4444 {
4567 /* the fast pointer has looped back around and caught up 4445 case OP_NUMEQ: r = r == 0; break;
4568 with the slow pointer, hence the structure is circular, 4446 case OP_LESS: r = r < 0; break;
4569 not of finite length, and therefore not a list */ 4447 case OP_GRE: r = r > 0; break;
4570 return -1; 4448 case OP_LEQ: r = r <= 0; break;
4449 case OP_GEQ: r = r >= 0; break;
4571 } 4450 }
4572 }
4573}
4574 4451
4452 if (!r)
4453 s_return (S_F);
4454 }
4455
4456 s_return (S_T);
4457}
4458
4575static pointer 4459static int
4576opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4460opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4577{ 4461{
4578 pointer x; 4462 pointer args = SCHEME_V->args;
4579 num v; 4463 pointer a = car (args);
4580 int (*comp_func) (num, num); 4464 pointer d = cdr (args);
4465 int r;
4581 4466
4582 switch (op) 4467 switch (op)
4583 { 4468 {
4584 case OP_NOT: /* not */ 4469 case OP_NOT: /* not */ r = is_false (a) ; break;
4585 s_retbool (is_false (car (SCHEME_V->args))); 4470 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break;
4471 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break;
4472 case OP_NULLP: /* null? */ r = a == NIL ; break;
4473 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break;
4474 case OP_NUMBERP: /* number? */ r = is_number (a) ; break;
4475 case OP_STRINGP: /* string? */ r = is_string (a) ; break;
4476 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4477 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4478 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4586 4479
4587 case OP_BOOLP: /* boolean? */
4588 s_retbool (car (SCHEME_V->args) == S_F || car (SCHEME_V->args) == S_T);
4589
4590 case OP_EOFOBJP: /* boolean? */
4591 s_retbool (car (SCHEME_V->args) == S_EOF);
4592
4593 case OP_NULLP: /* null? */
4594 s_retbool (car (SCHEME_V->args) == NIL);
4595
4596 case OP_NUMEQ: /* = */
4597 case OP_LESS: /* < */
4598 case OP_GRE: /* > */
4599 case OP_LEQ: /* <= */
4600 case OP_GEQ: /* >= */
4601 switch (op)
4602 {
4603 case OP_NUMEQ:
4604 comp_func = num_eq;
4605 break;
4606
4607 case OP_LESS:
4608 comp_func = num_lt;
4609 break;
4610
4611 case OP_GRE:
4612 comp_func = num_gt;
4613 break;
4614
4615 case OP_LEQ:
4616 comp_func = num_le;
4617 break;
4618
4619 case OP_GEQ:
4620 comp_func = num_ge;
4621 break;
4622 }
4623
4624 x = SCHEME_V->args;
4625 v = nvalue (car (x));
4626 x = cdr (x);
4627
4628 for (; x != NIL; x = cdr (x))
4629 {
4630 if (!comp_func (v, nvalue (car (x))))
4631 s_retbool (0);
4632
4633 v = nvalue (car (x));
4634 }
4635
4636 s_retbool (1);
4637
4638 case OP_SYMBOLP: /* symbol? */
4639 s_retbool (is_symbol (car (SCHEME_V->args)));
4640
4641 case OP_NUMBERP: /* number? */
4642 s_retbool (is_number (car (SCHEME_V->args)));
4643
4644 case OP_STRINGP: /* string? */
4645 s_retbool (is_string (car (SCHEME_V->args)));
4646
4647 case OP_INTEGERP: /* integer? */
4648 s_retbool (is_integer (car (SCHEME_V->args)));
4649
4650 case OP_REALP: /* real? */
4651 s_retbool (is_number (car (SCHEME_V->args))); /* All numbers are real */
4652
4653 case OP_CHARP: /* char? */
4654 s_retbool (is_character (car (SCHEME_V->args)));
4655#if USE_CHAR_CLASSIFIERS 4480#if USE_CHAR_CLASSIFIERS
4656 4481 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4657 case OP_CHARAP: /* char-alphabetic? */ 4482 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4658 s_retbool (Cisalpha (ivalue (car (SCHEME_V->args)))); 4483 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4659 4484 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4660 case OP_CHARNP: /* char-numeric? */ 4485 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4661 s_retbool (Cisdigit (ivalue (car (SCHEME_V->args))));
4662
4663 case OP_CHARWP: /* char-whitespace? */
4664 s_retbool (Cisspace (ivalue (car (SCHEME_V->args))));
4665
4666 case OP_CHARUP: /* char-upper-case? */
4667 s_retbool (Cisupper (ivalue (car (SCHEME_V->args))));
4668
4669 case OP_CHARLP: /* char-lower-case? */
4670 s_retbool (Cislower (ivalue (car (SCHEME_V->args))));
4671#endif 4486#endif
4487
4672#if USE_PORTS 4488#if USE_PORTS
4673 4489 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4674 case OP_PORTP: /* port? */
4675 s_retbool (is_port (car (SCHEME_V->args)));
4676
4677 case OP_INPORTP: /* input-port? */ 4490 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4678 s_retbool (is_inport (car (SCHEME_V->args)));
4679
4680 case OP_OUTPORTP: /* output-port? */ 4491 case OP_OUTPORTP: /* output-port? */ r = is_outport (a); break;
4681 s_retbool (is_outport (car (SCHEME_V->args)));
4682#endif 4492#endif
4683 4493
4684 case OP_PROCP: /* procedure? */ 4494 case OP_PROCP: /* procedure? */
4685 4495
4686 /*-- 4496 /*--
4687 * continuation should be procedure by the example 4497 * continuation should be procedure by the example
4688 * (call-with-current-continuation procedure?) ==> #t 4498 * (call-with-current-continuation procedure?) ==> #t
4689 * in R^3 report sec. 6.9 4499 * in R^3 report sec. 6.9
4690 */ 4500 */
4691 s_retbool (is_proc (car (SCHEME_V->args)) || is_closure (car (SCHEME_V->args)) 4501 r = is_proc (a) || is_closure (a) || is_continuation (a) || is_foreign (a);
4692 || is_continuation (car (SCHEME_V->args)) || is_foreign (car (SCHEME_V->args))); 4502 break;
4693 4503
4694 case OP_PAIRP: /* pair? */ 4504 case OP_PAIRP: /* pair? */ r = is_pair (a) ; break;
4695 s_retbool (is_pair (car (SCHEME_V->args))); 4505 case OP_LISTP: /* list? */ r = list_length (SCHEME_A_ a) >= 0; break;
4696 4506 case OP_ENVP: /* environment? */ r = is_environment (a) ; break;
4697 case OP_LISTP: /* list? */ 4507 case OP_VECTORP: /* vector? */ r = is_vector (a) ; break;
4698 s_retbool (list_length (SCHEME_A_ car (SCHEME_V->args)) >= 0); 4508 case OP_EQ: /* eq? */ r = a == cadr (args) ; break;
4699 4509 case OP_EQV: /* eqv? */ r = eqv (a, cadr (args)) ; break;
4700 case OP_ENVP: /* environment? */
4701 s_retbool (is_environment (car (SCHEME_V->args)));
4702
4703 case OP_VECTORP: /* vector? */
4704 s_retbool (is_vector (car (SCHEME_V->args)));
4705
4706 case OP_EQ: /* eq? */
4707 s_retbool (car (SCHEME_V->args) == cadr (SCHEME_V->args));
4708
4709 case OP_EQV: /* eqv? */
4710 s_retbool (eqv (car (SCHEME_V->args), cadr (SCHEME_V->args)));
4711 } 4510 }
4712 4511
4713 return S_T; 4512 s_retbool (r);
4714} 4513}
4715 4514
4716static pointer 4515static int
4717opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4516opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4718{ 4517{
4518 pointer args = SCHEME_V->args;
4519 pointer a = car (args);
4719 pointer x, y; 4520 pointer x, y;
4720 4521
4721 switch (op) 4522 switch (op)
4722 { 4523 {
4723 case OP_FORCE: /* force */ 4524 case OP_FORCE: /* force */
4724 SCHEME_V->code = car (SCHEME_V->args); 4525 SCHEME_V->code = a;
4725 4526
4726 if (is_promise (SCHEME_V->code)) 4527 if (is_promise (SCHEME_V->code))
4727 { 4528 {
4728 /* Should change type to closure here */ 4529 /* Should change type to closure here */
4729 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); 4530 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code);
4750 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4551 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4751 SCHEME_V->outport = cadr (SCHEME_V->args); 4552 SCHEME_V->outport = cadr (SCHEME_V->args);
4752 } 4553 }
4753 } 4554 }
4754 4555
4755 SCHEME_V->args = car (SCHEME_V->args); 4556 SCHEME_V->args = a;
4756 4557
4757 if (op == OP_WRITE) 4558 if (op == OP_WRITE)
4758 SCHEME_V->print_flag = 1; 4559 SCHEME_V->print_flag = 1;
4759 else 4560 else
4760 SCHEME_V->print_flag = 0; 4561 SCHEME_V->print_flag = 0;
4761 4562
4762 s_goto (OP_P0LIST); 4563 s_goto (OP_P0LIST);
4763 4564
4764 case OP_NEWLINE: /* newline */ 4565 case OP_NEWLINE: /* newline */
4765 if (is_pair (SCHEME_V->args)) 4566 if (is_pair (args))
4766 { 4567 {
4767 if (car (SCHEME_V->args) != SCHEME_V->outport) 4568 if (a != SCHEME_V->outport)
4768 { 4569 {
4769 x = cons (SCHEME_V->outport, NIL); 4570 x = cons (SCHEME_V->outport, NIL);
4770 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4571 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4771 SCHEME_V->outport = car (SCHEME_V->args); 4572 SCHEME_V->outport = a;
4772 } 4573 }
4773 } 4574 }
4774 4575
4775 putstr (SCHEME_A_ "\n"); 4576 putstr (SCHEME_A_ "\n");
4776 s_return (S_T); 4577 s_return (S_T);
4777#endif 4578#endif
4778 4579
4779 case OP_ERR0: /* error */ 4580 case OP_ERR0: /* error */
4780 SCHEME_V->retcode = -1; 4581 SCHEME_V->retcode = -1;
4781 4582
4782 if (!is_string (car (SCHEME_V->args))) 4583 if (!is_string (a))
4783 { 4584 {
4784 SCHEME_V->args = cons (mk_string (SCHEME_A_ " -- "), SCHEME_V->args); 4585 args = cons (mk_string (SCHEME_A_ " -- "), args);
4785 setimmutable (car (SCHEME_V->args)); 4586 setimmutable (car (args));
4786 } 4587 }
4787 4588
4788 putstr (SCHEME_A_ "Error: "); 4589 putstr (SCHEME_A_ "Error: ");
4789 putstr (SCHEME_A_ strvalue (car (SCHEME_V->args))); 4590 putstr (SCHEME_A_ strvalue (car (args)));
4790 SCHEME_V->args = cdr (SCHEME_V->args); 4591 SCHEME_V->args = cdr (args);
4791 s_goto (OP_ERR1); 4592 s_goto (OP_ERR1);
4792 4593
4793 case OP_ERR1: /* error */ 4594 case OP_ERR1: /* error */
4794 putstr (SCHEME_A_ " "); 4595 putstr (SCHEME_A_ " ");
4795 4596
4796 if (SCHEME_V->args != NIL) 4597 if (args != NIL)
4797 { 4598 {
4798 s_save (SCHEME_A_ OP_ERR1, cdr (SCHEME_V->args), NIL); 4599 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4799 SCHEME_V->args = car (SCHEME_V->args); 4600 SCHEME_V->args = a;
4800 SCHEME_V->print_flag = 1; 4601 SCHEME_V->print_flag = 1;
4801 s_goto (OP_P0LIST); 4602 s_goto (OP_P0LIST);
4802 } 4603 }
4803 else 4604 else
4804 { 4605 {
4805 putstr (SCHEME_A_ "\n"); 4606 putstr (SCHEME_A_ "\n");
4806 4607
4807 if (SCHEME_V->interactive_repl) 4608 if (SCHEME_V->interactive_repl)
4808 s_goto (OP_T0LVL); 4609 s_goto (OP_T0LVL);
4809 else 4610 else
4810 return NIL; 4611 return -1;
4811 } 4612 }
4812 4613
4813 case OP_REVERSE: /* reverse */ 4614 case OP_REVERSE: /* reverse */
4814 s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); 4615 s_return (reverse (SCHEME_A_ a));
4815 4616
4816 case OP_LIST_STAR: /* list* */ 4617 case OP_LIST_STAR: /* list* */
4817 s_return (list_star (SCHEME_A_ SCHEME_V->args)); 4618 s_return (list_star (SCHEME_A_ SCHEME_V->args));
4818 4619
4819 case OP_APPEND: /* append */ 4620 case OP_APPEND: /* append */
4820 x = NIL; 4621 x = NIL;
4821 y = SCHEME_V->args; 4622 y = args;
4822 4623
4823 if (y == x) 4624 if (y == x)
4824 s_return (x); 4625 s_return (x);
4825 4626
4826 /* cdr() in the while condition is not a typo. If car() */ 4627 /* cdr() in the while condition is not a typo. If car() */
4837 s_return (reverse_in_place (SCHEME_A_ car (y), x)); 4638 s_return (reverse_in_place (SCHEME_A_ car (y), x));
4838 4639
4839#if USE_PLIST 4640#if USE_PLIST
4840 4641
4841 case OP_PUT: /* put */ 4642 case OP_PUT: /* put */
4842 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 4643 if (!hasprop (a) || !hasprop (cadr (args)))
4843 Error_0 ("illegal use of put"); 4644 Error_0 ("illegal use of put");
4844 4645
4845 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 4646 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
4846 { 4647 {
4847 if (caar (x) == y) 4648 if (caar (x) == y)
4848 break; 4649 break;
4849 } 4650 }
4850 4651
4851 if (x != NIL) 4652 if (x != NIL)
4852 cdar (x) = caddr (SCHEME_V->args); 4653 cdar (x) = caddr (args);
4853 else 4654 else
4854 symprop (car (SCHEME_V->args)) = cons (cons (y, caddr (SCHEME_V->args)), symprop (car (SCHEME_V->args))); 4655 symprop (a) = cons (cons (y, caddr (args)), symprop (a));
4855 4656
4856 s_return (S_T); 4657 s_return (S_T);
4857 4658
4858 case OP_GET: /* get */ 4659 case OP_GET: /* get */
4859 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 4660 if (!hasprop (a) || !hasprop (cadr (args)))
4860 Error_0 ("illegal use of get"); 4661 Error_0 ("illegal use of get");
4861 4662
4862 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 4663 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
4863 if (caar (x) == y) 4664 if (caar (x) == y)
4864 break; 4665 break;
4865 4666
4866 if (x != NIL) 4667 if (x != NIL)
4867 s_return (cdar (x)); 4668 s_return (cdar (x));
4869 s_return (NIL); 4670 s_return (NIL);
4870 4671
4871#endif /* USE_PLIST */ 4672#endif /* USE_PLIST */
4872 4673
4873 case OP_QUIT: /* quit */ 4674 case OP_QUIT: /* quit */
4874 if (is_pair (SCHEME_V->args)) 4675 if (is_pair (args))
4875 SCHEME_V->retcode = ivalue (car (SCHEME_V->args)); 4676 SCHEME_V->retcode = ivalue (a);
4876 4677
4877 return NIL; 4678 return -1;
4878 4679
4879 case OP_GC: /* gc */ 4680 case OP_GC: /* gc */
4880 gc (SCHEME_A_ NIL, NIL); 4681 gc (SCHEME_A_ NIL, NIL);
4881 s_return (S_T); 4682 s_return (S_T);
4882 4683
4883 case OP_GCVERB: /* gc-verbose */ 4684 case OP_GCVERB: /* gc-verbose */
4884 { 4685 {
4885 int was = SCHEME_V->gc_verbose; 4686 int was = SCHEME_V->gc_verbose;
4886 4687
4887 SCHEME_V->gc_verbose = (car (SCHEME_V->args) != S_F); 4688 SCHEME_V->gc_verbose = (a != S_F);
4888 s_retbool (was); 4689 s_retbool (was);
4889 } 4690 }
4890 4691
4891 case OP_NEWSEGMENT: /* new-segment */ 4692 case OP_NEWSEGMENT: /* new-segment */
4892 if (!is_pair (SCHEME_V->args) || !is_number (car (SCHEME_V->args))) 4693 if (!is_pair (args) || !is_number (a))
4893 Error_0 ("new-segment: argument must be a number"); 4694 Error_0 ("new-segment: argument must be a number");
4894 4695
4895 alloc_cellseg (SCHEME_A_ (int)ivalue (car (SCHEME_V->args))); 4696 alloc_cellseg (SCHEME_A_ ivalue (a));
4896 4697
4897 s_return (S_T); 4698 s_return (S_T);
4898 4699
4899 case OP_OBLIST: /* oblist */ 4700 case OP_OBLIST: /* oblist */
4900 s_return (oblist_all_symbols (SCHEME_A)); 4701 s_return (oblist_all_symbols (SCHEME_A));
4927 case OP_OPEN_INOUTFILE: 4728 case OP_OPEN_INOUTFILE:
4928 prop = port_input | port_output; 4729 prop = port_input | port_output;
4929 break; 4730 break;
4930 } 4731 }
4931 4732
4932 p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); 4733 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4933 4734
4934 if (p == NIL) 4735 s_return (p == NIL ? S_F : p);
4935 s_return (S_F);
4936
4937 s_return (p);
4938 } 4736 }
4939 4737
4940# if USE_STRING_PORTS 4738# if USE_STRING_PORTS
4941 4739
4942 case OP_OPEN_INSTRING: /* open-input-string */ 4740 case OP_OPEN_INSTRING: /* open-input-string */
4954 case OP_OPEN_INOUTSTRING: 4752 case OP_OPEN_INOUTSTRING:
4955 prop = port_input | port_output; 4753 prop = port_input | port_output;
4956 break; 4754 break;
4957 } 4755 }
4958 4756
4959 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4757 p = port_from_string (SCHEME_A_ strvalue (a),
4960 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); 4758 strvalue (a) + strlength (a), prop);
4961 4759
4962 if (p == NIL) 4760 s_return (p == NIL ? S_F : p);
4963 s_return (S_F);
4964
4965 s_return (p);
4966 } 4761 }
4967 4762
4968 case OP_OPEN_OUTSTRING: /* open-output-string */ 4763 case OP_OPEN_OUTSTRING: /* open-output-string */
4969 { 4764 {
4970 pointer p; 4765 pointer p;
4971 4766
4972 if (car (SCHEME_V->args) == NIL) 4767 if (a == NIL)
4973 {
4974 p = port_from_scratch (SCHEME_A); 4768 p = port_from_scratch (SCHEME_A);
4975
4976 if (p == NIL)
4977 s_return (S_F);
4978 }
4979 else 4769 else
4980 {
4981 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4770 p = port_from_string (SCHEME_A_ strvalue (a),
4982 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); 4771 strvalue (a) + strlength (a), port_output);
4983 4772
4984 if (p == NIL) 4773 s_return (p == NIL ? S_F : p);
4985 s_return (S_F);
4986 }
4987
4988 s_return (p);
4989 } 4774 }
4990 4775
4991 case OP_GET_OUTSTRING: /* get-output-string */ 4776 case OP_GET_OUTSTRING: /* get-output-string */
4992 { 4777 {
4993 port *p; 4778 port *p;
4994 4779
4995 if ((p = car (SCHEME_V->args)->object.port)->kind & port_string) 4780 if ((p = a->object.port)->kind & port_string)
4996 { 4781 {
4997 off_t size; 4782 off_t size;
4998 char *str; 4783 char *str;
4999 4784
5000 size = p->rep.string.curr - p->rep.string.start + 1; 4785 size = p->rep.string.curr - p->rep.string.start + 1;
5016 } 4801 }
5017 4802
5018# endif 4803# endif
5019 4804
5020 case OP_CLOSE_INPORT: /* close-input-port */ 4805 case OP_CLOSE_INPORT: /* close-input-port */
5021 port_close (SCHEME_A_ car (SCHEME_V->args), port_input); 4806 port_close (SCHEME_A_ a, port_input);
5022 s_return (S_T); 4807 s_return (S_T);
5023 4808
5024 case OP_CLOSE_OUTPORT: /* close-output-port */ 4809 case OP_CLOSE_OUTPORT: /* close-output-port */
5025 port_close (SCHEME_A_ car (SCHEME_V->args), port_output); 4810 port_close (SCHEME_A_ a, port_output);
5026 s_return (S_T); 4811 s_return (S_T);
5027#endif 4812#endif
5028 4813
5029 case OP_INT_ENV: /* interaction-environment */ 4814 case OP_INT_ENV: /* interaction-environment */
5030 s_return (SCHEME_V->global_env); 4815 s_return (SCHEME_V->global_env);
5032 case OP_CURR_ENV: /* current-environment */ 4817 case OP_CURR_ENV: /* current-environment */
5033 s_return (SCHEME_V->envir); 4818 s_return (SCHEME_V->envir);
5034 4819
5035 } 4820 }
5036 4821
5037 return S_T; 4822 if (USE_ERROR_CHECKING) abort ();
5038} 4823}
5039 4824
5040static pointer 4825static int
5041opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4826opexe_5 (SCHEME_P_ enum scheme_opcodes op)
5042{ 4827{
4828 pointer args = SCHEME_V->args;
5043 pointer x; 4829 pointer x;
5044 4830
5045 if (SCHEME_V->nesting != 0) 4831 if (SCHEME_V->nesting != 0)
5046 { 4832 {
5047 int n = SCHEME_V->nesting; 4833 int n = SCHEME_V->nesting;
5054 switch (op) 4840 switch (op)
5055 { 4841 {
5056 /* ========== reading part ========== */ 4842 /* ========== reading part ========== */
5057#if USE_PORTS 4843#if USE_PORTS
5058 case OP_READ: 4844 case OP_READ:
5059 if (!is_pair (SCHEME_V->args)) 4845 if (!is_pair (args))
5060 s_goto (OP_READ_INTERNAL); 4846 s_goto (OP_READ_INTERNAL);
5061 4847
5062 if (!is_inport (car (SCHEME_V->args))) 4848 if (!is_inport (car (args)))
5063 Error_1 ("read: not an input port:", car (SCHEME_V->args)); 4849 Error_1 ("read: not an input port:", car (args));
5064 4850
5065 if (car (SCHEME_V->args) == SCHEME_V->inport) 4851 if (car (args) == SCHEME_V->inport)
5066 s_goto (OP_READ_INTERNAL); 4852 s_goto (OP_READ_INTERNAL);
5067 4853
5068 x = SCHEME_V->inport; 4854 x = SCHEME_V->inport;
5069 SCHEME_V->inport = car (SCHEME_V->args); 4855 SCHEME_V->inport = car (args);
5070 x = cons (x, NIL); 4856 x = cons (x, NIL);
5071 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4857 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5072 s_goto (OP_READ_INTERNAL); 4858 s_goto (OP_READ_INTERNAL);
5073 4859
5074 case OP_READ_CHAR: /* read-char */ 4860 case OP_READ_CHAR: /* read-char */
5075 case OP_PEEK_CHAR: /* peek-char */ 4861 case OP_PEEK_CHAR: /* peek-char */
5076 { 4862 {
5077 int c; 4863 int c;
5078 4864
5079 if (is_pair (SCHEME_V->args)) 4865 if (is_pair (args))
5080 { 4866 {
5081 if (car (SCHEME_V->args) != SCHEME_V->inport) 4867 if (car (args) != SCHEME_V->inport)
5082 { 4868 {
5083 x = SCHEME_V->inport; 4869 x = SCHEME_V->inport;
5084 x = cons (x, NIL); 4870 x = cons (x, NIL);
5085 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4871 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5086 SCHEME_V->inport = car (SCHEME_V->args); 4872 SCHEME_V->inport = car (args);
5087 } 4873 }
5088 } 4874 }
5089 4875
5090 c = inchar (SCHEME_A); 4876 c = inchar (SCHEME_A);
5091 4877
5101 case OP_CHAR_READY: /* char-ready? */ 4887 case OP_CHAR_READY: /* char-ready? */
5102 { 4888 {
5103 pointer p = SCHEME_V->inport; 4889 pointer p = SCHEME_V->inport;
5104 int res; 4890 int res;
5105 4891
5106 if (is_pair (SCHEME_V->args)) 4892 if (is_pair (args))
5107 p = car (SCHEME_V->args); 4893 p = car (args);
5108 4894
5109 res = p->object.port->kind & port_string; 4895 res = p->object.port->kind & port_string;
5110 4896
5111 s_retbool (res); 4897 s_retbool (res);
5112 } 4898 }
5113 4899
5114 case OP_SET_INPORT: /* set-input-port */ 4900 case OP_SET_INPORT: /* set-input-port */
5115 SCHEME_V->inport = car (SCHEME_V->args); 4901 SCHEME_V->inport = car (args);
5116 s_return (SCHEME_V->value); 4902 s_return (SCHEME_V->value);
5117 4903
5118 case OP_SET_OUTPORT: /* set-output-port */ 4904 case OP_SET_OUTPORT: /* set-output-port */
5119 SCHEME_V->outport = car (SCHEME_V->args); 4905 SCHEME_V->outport = car (args);
5120 s_return (SCHEME_V->value); 4906 s_return (SCHEME_V->value);
5121#endif 4907#endif
5122 4908
5123 case OP_RDSEXPR: 4909 case OP_RDSEXPR:
5124 switch (SCHEME_V->tok) 4910 switch (SCHEME_V->tok)
5210 } 4996 }
5211 4997
5212 break; 4998 break;
5213 4999
5214 case OP_RDLIST: 5000 case OP_RDLIST:
5215 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 5001 SCHEME_V->args = cons (SCHEME_V->value, args);
5216 SCHEME_V->tok = token (SCHEME_A); 5002 SCHEME_V->tok = token (SCHEME_A);
5217 5003
5218 switch (SCHEME_V->tok) 5004 switch (SCHEME_V->tok)
5219 { 5005 {
5220 case TOK_EOF: 5006 case TOK_EOF:
5248 case OP_RDDOT: 5034 case OP_RDDOT:
5249 if (token (SCHEME_A) != TOK_RPAREN) 5035 if (token (SCHEME_A) != TOK_RPAREN)
5250 Error_0 ("syntax error: illegal dot expression"); 5036 Error_0 ("syntax error: illegal dot expression");
5251 5037
5252 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5038 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5253 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); 5039 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, args));
5254 5040
5255 case OP_RDQUOTE: 5041 case OP_RDQUOTE:
5256 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); 5042 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)));
5257 5043
5258 case OP_RDQQUOTE: 5044 case OP_RDQQUOTE:
5280 SCHEME_V->args = SCHEME_V->value; 5066 SCHEME_V->args = SCHEME_V->value;
5281 s_goto (OP_VECTOR); 5067 s_goto (OP_VECTOR);
5282 5068
5283 /* ========== printing part ========== */ 5069 /* ========== printing part ========== */
5284 case OP_P0LIST: 5070 case OP_P0LIST:
5285 if (is_vector (SCHEME_V->args)) 5071 if (is_vector (args))
5286 { 5072 {
5287 putstr (SCHEME_A_ "#("); 5073 putstr (SCHEME_A_ "#(");
5288 SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); 5074 SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0));
5289 s_goto (OP_PVECFROM); 5075 s_goto (OP_PVECFROM);
5290 } 5076 }
5291 else if (is_environment (SCHEME_V->args)) 5077 else if (is_environment (args))
5292 { 5078 {
5293 putstr (SCHEME_A_ "#<ENVIRONMENT>"); 5079 putstr (SCHEME_A_ "#<ENVIRONMENT>");
5294 s_return (S_T); 5080 s_return (S_T);
5295 } 5081 }
5296 else if (!is_pair (SCHEME_V->args)) 5082 else if (!is_pair (args))
5297 { 5083 {
5298 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5084 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5299 s_return (S_T); 5085 s_return (S_T);
5300 } 5086 }
5301 else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5087 else
5302 { 5088 {
5089 pointer a = car (args);
5090 pointer b = cdr (args);
5091 int ok_abbr = ok_abbrev (b);
5092 SCHEME_V->args = car (b);
5093
5094 if (a == SCHEME_V->QUOTE && ok_abbr)
5303 putstr (SCHEME_A_ "'"); 5095 putstr (SCHEME_A_ "'");
5304 SCHEME_V->args = cadr (SCHEME_V->args); 5096 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5097 putstr (SCHEME_A_ "`");
5098 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5099 putstr (SCHEME_A_ ",");
5100 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5101 putstr (SCHEME_A_ ",@");
5102 else
5103 {
5104 putstr (SCHEME_A_ "(");
5105 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5106 SCHEME_V->args = a;
5107 }
5108
5305 s_goto (OP_P0LIST); 5109 s_goto (OP_P0LIST);
5306 } 5110 }
5307 else if (car (SCHEME_V->args) == SCHEME_V->QQUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5111
5112 case OP_P1LIST:
5113 if (is_pair (args))
5308 { 5114 {
5115 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5309 putstr (SCHEME_A_ "`"); 5116 putstr (SCHEME_A_ " ");
5310 SCHEME_V->args = cadr (SCHEME_V->args); 5117 SCHEME_V->args = car (args);
5311 s_goto (OP_P0LIST); 5118 s_goto (OP_P0LIST);
5312 } 5119 }
5313 else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTE && ok_abbrev (cdr (SCHEME_V->args)))
5314 {
5315 putstr (SCHEME_A_ ",");
5316 SCHEME_V->args = cadr (SCHEME_V->args);
5317 s_goto (OP_P0LIST);
5318 }
5319 else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTESP && ok_abbrev (cdr (SCHEME_V->args)))
5320 {
5321 putstr (SCHEME_A_ ",@");
5322 SCHEME_V->args = cadr (SCHEME_V->args);
5323 s_goto (OP_P0LIST);
5324 }
5325 else
5326 {
5327 putstr (SCHEME_A_ "(");
5328 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL);
5329 SCHEME_V->args = car (SCHEME_V->args);
5330 s_goto (OP_P0LIST);
5331 }
5332
5333 case OP_P1LIST:
5334 if (is_pair (SCHEME_V->args))
5335 {
5336 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL);
5337 putstr (SCHEME_A_ " ");
5338 SCHEME_V->args = car (SCHEME_V->args);
5339 s_goto (OP_P0LIST);
5340 }
5341 else if (is_vector (SCHEME_V->args)) 5120 else if (is_vector (args))
5342 { 5121 {
5343 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); 5122 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL);
5344 putstr (SCHEME_A_ " . "); 5123 putstr (SCHEME_A_ " . ");
5345 s_goto (OP_P0LIST); 5124 s_goto (OP_P0LIST);
5346 } 5125 }
5347 else 5126 else
5348 { 5127 {
5349 if (SCHEME_V->args != NIL) 5128 if (args != NIL)
5350 { 5129 {
5351 putstr (SCHEME_A_ " . "); 5130 putstr (SCHEME_A_ " . ");
5352 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5131 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5353 } 5132 }
5354 5133
5355 putstr (SCHEME_A_ ")"); 5134 putstr (SCHEME_A_ ")");
5356 s_return (S_T); 5135 s_return (S_T);
5357 } 5136 }
5358 5137
5359 case OP_PVECFROM: 5138 case OP_PVECFROM:
5360 { 5139 {
5361 int i = ivalue_unchecked (cdr (SCHEME_V->args)); 5140 int i = ivalue_unchecked (cdr (args));
5362 pointer vec = car (SCHEME_V->args); 5141 pointer vec = car (args);
5363 int len = veclength (vec); 5142 int len = veclength (vec);
5364 5143
5365 if (i == len) 5144 if (i == len)
5366 { 5145 {
5367 putstr (SCHEME_A_ ")"); 5146 putstr (SCHEME_A_ ")");
5368 s_return (S_T); 5147 s_return (S_T);
5369 } 5148 }
5370 else 5149 else
5371 { 5150 {
5372 pointer elem = vector_elem (vec, i); 5151 pointer elem = vector_get (vec, i);
5373 5152
5374 ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; 5153 ivalue_unchecked (cdr (args)) = i + 1;
5375 s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); 5154 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5376 SCHEME_V->args = elem; 5155 SCHEME_V->args = elem;
5377 5156
5378 if (i > 0) 5157 if (i > 0)
5379 putstr (SCHEME_A_ " "); 5158 putstr (SCHEME_A_ " ");
5380 5159
5381 s_goto (OP_P0LIST); 5160 s_goto (OP_P0LIST);
5382 } 5161 }
5383 } 5162 }
5384 } 5163 }
5385 5164
5386 return S_T; 5165 if (USE_ERROR_CHECKING) abort ();
5387} 5166}
5388 5167
5389static pointer 5168static int
5390opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5169opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5391{ 5170{
5171 pointer args = SCHEME_V->args;
5172 pointer a = car (args);
5392 pointer x, y; 5173 pointer x, y;
5393 5174
5394 switch (op) 5175 switch (op)
5395 { 5176 {
5396 case OP_LIST_LENGTH: /* length *//* a.k */ 5177 case OP_LIST_LENGTH: /* length *//* a.k */
5397 { 5178 {
5398 long v = list_length (SCHEME_A_ car (SCHEME_V->args)); 5179 long v = list_length (SCHEME_A_ a);
5399 5180
5400 if (v < 0) 5181 if (v < 0)
5401 Error_1 ("length: not a list:", car (SCHEME_V->args)); 5182 Error_1 ("length: not a list:", a);
5402 5183
5403 s_return (mk_integer (SCHEME_A_ v)); 5184 s_return (mk_integer (SCHEME_A_ v));
5404 } 5185 }
5405 5186
5406 case OP_ASSQ: /* assq *//* a.k */ 5187 case OP_ASSQ: /* assq *//* a.k */
5407 x = car (SCHEME_V->args); 5188 x = a;
5408 5189
5409 for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) 5190 for (y = cadr (args); is_pair (y); y = cdr (y))
5410 { 5191 {
5411 if (!is_pair (car (y))) 5192 if (!is_pair (car (y)))
5412 Error_0 ("unable to handle non pair element"); 5193 Error_0 ("unable to handle non pair element");
5413 5194
5414 if (x == caar (y)) 5195 if (x == caar (y))
5420 else 5201 else
5421 s_return (S_F); 5202 s_return (S_F);
5422 5203
5423 5204
5424 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5205 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5425 SCHEME_V->args = car (SCHEME_V->args); 5206 SCHEME_V->args = a;
5426 5207
5427 if (SCHEME_V->args == NIL) 5208 if (SCHEME_V->args == NIL)
5428 s_return (S_F); 5209 s_return (S_F);
5429 else if (is_closure (SCHEME_V->args)) 5210 else if (is_closure (SCHEME_V->args))
5430 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5211 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5436 case OP_CLOSUREP: /* closure? */ 5217 case OP_CLOSUREP: /* closure? */
5437 /* 5218 /*
5438 * Note, macro object is also a closure. 5219 * Note, macro object is also a closure.
5439 * Therefore, (closure? <#MACRO>) ==> #t 5220 * Therefore, (closure? <#MACRO>) ==> #t
5440 */ 5221 */
5441 s_retbool (is_closure (car (SCHEME_V->args))); 5222 s_retbool (is_closure (a));
5442 5223
5443 case OP_MACROP: /* macro? */ 5224 case OP_MACROP: /* macro? */
5444 s_retbool (is_macro (car (SCHEME_V->args))); 5225 s_retbool (is_macro (a));
5445 } 5226 }
5446 5227
5447 return S_T; /* NOTREACHED */ 5228 if (USE_ERROR_CHECKING) abort ();
5448} 5229}
5449 5230
5231/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5450typedef pointer (*dispatch_func) (SCHEME_P_ enum scheme_opcodes); 5232typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5451 5233
5452typedef int (*test_predicate) (pointer); 5234typedef int (*test_predicate)(pointer);
5453static int 5235static int
5454is_any (pointer p) 5236tst_any (pointer p)
5455{ 5237{
5456 return 1; 5238 return 1;
5457} 5239}
5458 5240
5459static int 5241static int
5460is_nonneg (pointer p) 5242tst_inonneg (pointer p)
5461{ 5243{
5462 return ivalue (p) >= 0 && is_integer (p); 5244 return is_integer (p) && ivalue_unchecked (p) >= 0;
5245}
5246
5247static int
5248tst_is_list (SCHEME_P_ pointer p)
5249{
5250 return p == NIL || is_pair (p);
5463} 5251}
5464 5252
5465/* Correspond carefully with following defines! */ 5253/* Correspond carefully with following defines! */
5466static struct 5254static struct
5467{ 5255{
5468 test_predicate fct; 5256 test_predicate fct;
5469 const char *kind; 5257 const char *kind;
5470} tests[] = 5258} tests[] = {
5471{ 5259 { tst_any , 0 },
5472 { 0, 0}, /* unused */ 5260 { is_string , "string" },
5473 { is_any, 0}, 5261 { is_symbol , "symbol" },
5474 { is_string, "string" }, 5262 { is_port , "port" },
5475 { is_symbol, "symbol" },
5476 { is_port, "port" },
5477 { is_inport, "input port" }, 5263 { is_inport , "input port" },
5478 { is_outport, "output port" }, 5264 { is_outport , "output port" },
5479 { is_environment, "environment" }, 5265 { is_environment, "environment" },
5480 { is_pair, "pair" }, 5266 { is_pair , "pair" },
5481 { 0, "pair or '()" }, 5267 { 0 , "pair or '()" },
5482 { is_character, "character" }, 5268 { is_character , "character" },
5483 { is_vector, "vector" }, 5269 { is_vector , "vector" },
5484 { is_number, "number" }, 5270 { is_number , "number" },
5485 { is_integer, "integer" }, 5271 { is_integer , "integer" },
5486 { is_nonneg, "non-negative integer" } 5272 { tst_inonneg , "non-negative integer" }
5487}; 5273};
5488 5274
5489#define TST_NONE 0 5275#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5490#define TST_ANY "\001" 5276#define TST_ANY "\001"
5491#define TST_STRING "\002" 5277#define TST_STRING "\002"
5492#define TST_SYMBOL "\003" 5278#define TST_SYMBOL "\003"
5493#define TST_PORT "\004" 5279#define TST_PORT "\004"
5494#define TST_INPORT "\005" 5280#define TST_INPORT "\005"
5495#define TST_OUTPORT "\006" 5281#define TST_OUTPORT "\006"
5496#define TST_ENVIRONMENT "\007" 5282#define TST_ENVIRONMENT "\007"
5497#define TST_PAIR "\010" 5283#define TST_PAIR "\010"
5498#define TST_LIST "\011" 5284#define TST_LIST "\011"
5499#define TST_CHAR "\012" 5285#define TST_CHAR "\012"
5500#define TST_VECTOR "\013" 5286#define TST_VECTOR "\013"
5501#define TST_NUMBER "\014" 5287#define TST_NUMBER "\014"
5502#define TST_INTEGER "\015" 5288#define TST_INTEGER "\015"
5503#define TST_NATURAL "\016" 5289#define TST_NATURAL "\016"
5290
5291#define INF_ARG 0xff
5292#define UNNAMED_OP ""
5293
5294static const char opnames[] =
5295#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5296#include "opdefines.h"
5297#undef OP_DEF
5298;
5299
5300static const char *
5301opname (int idx)
5302{
5303 const char *name = opnames;
5304
5305 /* should do this at compile time, but would require external program, right? */
5306 while (idx--)
5307 name += strlen (name) + 1;
5308
5309 return *name ? name : "ILLEGAL";
5310}
5311
5312static const char *
5313procname (pointer x)
5314{
5315 return opname (procnum (x));
5316}
5504 5317
5505typedef struct 5318typedef struct
5506{ 5319{
5507 dispatch_func func; 5320 uint8_t func;
5508 char *name; 5321 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5322 uint8_t builtin;
5323#if USE_ERROR_CHECKING
5509 int min_arity; 5324 uint8_t min_arity;
5510 int max_arity; 5325 uint8_t max_arity;
5511 char *arg_tests_encoding; 5326 char arg_tests_encoding[3];
5327#endif
5512} op_code_info; 5328} op_code_info;
5513 5329
5514#define INF_ARG 0xffff
5515
5516static op_code_info dispatch_table[] = { 5330static const op_code_info dispatch_table[] = {
5517#define OP_DEF(A,B,C,D,E,OP) {A,B,C,D,E}, 5331#if USE_ERROR_CHECKING
5332#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5333#else
5334#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5335#endif
5518#include "opdefines.h" 5336#include "opdefines.h"
5337#undef OP_DEF
5519 {0} 5338 {0}
5520}; 5339};
5521 5340
5522static const char *
5523procname (pointer x)
5524{
5525 int n = procnum (x);
5526 const char *name = dispatch_table[n].name;
5527
5528 if (name == 0)
5529 name = "ILLEGAL!";
5530
5531 return name;
5532}
5533
5534/* kernel of this interpreter */ 5341/* kernel of this interpreter */
5535static void 5342static void ecb_hot
5536Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5343Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5537{ 5344{
5538 SCHEME_V->op = op; 5345 SCHEME_V->op = op;
5539 5346
5540 for (;;) 5347 for (;;)
5541 { 5348 {
5542 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5349 const op_code_info *pcd = dispatch_table + SCHEME_V->op;
5543 5350
5544#if USE_ERROR_CHECKING 5351#if USE_ERROR_CHECKING
5545 if (pcd->name) /* if built-in function, check arguments */ 5352 if (pcd->builtin) /* if built-in function, check arguments */
5546 { 5353 {
5547 int ok = 1;
5548 char msg[STRBUFFSIZE]; 5354 char msg[STRBUFFSIZE];
5549 int n = list_length (SCHEME_A_ SCHEME_V->args); 5355 int n = list_length (SCHEME_A_ SCHEME_V->args);
5550 5356
5551 /* Check number of arguments */ 5357 /* Check number of arguments */
5552 if (ecb_expect_false (n < pcd->min_arity)) 5358 if (ecb_expect_false (n < pcd->min_arity))
5553 { 5359 {
5554 ok = 0;
5555 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5360 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5556 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); 5361 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity);
5362 xError_1 (SCHEME_A_ msg, 0);
5363 continue;
5557 } 5364 }
5558 else if (ecb_excpect_false (n > pcd->max_arity)) 5365 else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG))
5559 { 5366 {
5560 ok = 0;
5561 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5367 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5562 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); 5368 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity);
5369 xError_1 (SCHEME_A_ msg, 0);
5370 continue;
5563 } 5371 }
5564 5372 else
5565 if (ecb_expect_false (ok))
5566 { 5373 {
5567 if (pcd->arg_tests_encoding) 5374 if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */
5568 { 5375 {
5569 int i = 0; 5376 int i = 0;
5570 int j; 5377 int j;
5571 const char *t = pcd->arg_tests_encoding; 5378 const char *t = pcd->arg_tests_encoding;
5572 pointer arglist = SCHEME_V->args; 5379 pointer arglist = SCHEME_V->args;
5573 5380
5574 do 5381 do
5575 { 5382 {
5576 pointer arg = car (arglist); 5383 pointer arg = car (arglist);
5577 5384
5578 j = (int) t[0]; 5385 j = t[0];
5579 5386
5387 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5580 if (j == TST_LIST[0]) 5388 if (j == TST_LIST[0])
5581 { 5389 {
5582 if (arg != NIL && !is_pair (arg)) 5390 if (!tst_is_list (SCHEME_A_ arg))
5583 break; 5391 break;
5584 } 5392 }
5585 else 5393 else
5586 { 5394 {
5587 if (!tests[j].fct (arg)) 5395 if (!tests[j - 1].fct (arg))
5588 break; 5396 break;
5589 } 5397 }
5590 5398
5591 if (t[1] != 0) /* last test is replicated as necessary */ 5399 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5592 t++; 5400 t++;
5593 5401
5594 arglist = cdr (arglist); 5402 arglist = cdr (arglist);
5595 i++; 5403 i++;
5596 } 5404 }
5597 while (i < n); 5405 while (i < n);
5598 5406
5599 if (i < n) 5407 if (i < n)
5600 { 5408 {
5601 ok = 0;
5602 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); 5409 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind);
5410 xError_1 (SCHEME_A_ msg, 0);
5411 continue;
5603 } 5412 }
5604 } 5413 }
5605 } 5414 }
5606
5607 if (!ok)
5608 {
5609 if (xError_1 (SCHEME_A_ msg, 0) == NIL)
5610 return;
5611
5612 pcd = dispatch_table + SCHEME_V->op;
5613 }
5614 } 5415 }
5615#endif 5416#endif
5616 5417
5617 ok_to_freely_gc (SCHEME_A); 5418 ok_to_freely_gc (SCHEME_A);
5618 5419
5420 static const dispatch_func dispatch_funcs[] = {
5421 opexe_0,
5422 opexe_1,
5423 opexe_2,
5424 opexe_3,
5425 opexe_4,
5426 opexe_5,
5427 opexe_6,
5428 };
5429
5619 if (ecb_expect_false (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL)) 5430 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5620 return; 5431 return;
5621 5432
5622 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5433 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5623 { 5434 {
5624 xwrstr ("No memory!\n"); 5435 xwrstr ("No memory!\n");
5648mk_proc (SCHEME_P_ enum scheme_opcodes op) 5459mk_proc (SCHEME_P_ enum scheme_opcodes op)
5649{ 5460{
5650 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5461 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5651 set_typeflag (y, (T_PROC | T_ATOM)); 5462 set_typeflag (y, (T_PROC | T_ATOM));
5652 ivalue_unchecked (y) = op; 5463 ivalue_unchecked (y) = op;
5653 set_num_integer (y);
5654 return y; 5464 return y;
5655} 5465}
5656 5466
5657/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5467/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5658static int 5468static int
5723 return OP_C0STREAM; /* cons-stream */ 5533 return OP_C0STREAM; /* cons-stream */
5724 } 5534 }
5725} 5535}
5726 5536
5727#if USE_MULTIPLICITY 5537#if USE_MULTIPLICITY
5728scheme * 5538ecb_cold scheme *
5729scheme_init_new () 5539scheme_init_new ()
5730{ 5540{
5731 scheme *sc = malloc (sizeof (scheme)); 5541 scheme *sc = malloc (sizeof (scheme));
5732 5542
5733 if (!scheme_init (SCHEME_A)) 5543 if (!scheme_init (SCHEME_A))
5738 else 5548 else
5739 return sc; 5549 return sc;
5740} 5550}
5741#endif 5551#endif
5742 5552
5743int 5553ecb_cold int
5744scheme_init (SCHEME_P) 5554scheme_init (SCHEME_P)
5745{ 5555{
5746 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5556 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5747 pointer x; 5557 pointer x;
5748 5558
5821 5631
5822 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) 5632 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
5823 assign_syntax (SCHEME_A_ syntax_names[i]); 5633 assign_syntax (SCHEME_A_ syntax_names[i]);
5824 } 5634 }
5825 5635
5636 // TODO: should iterate via strlen, to avoid n² complexity
5826 for (i = 0; i < n; i++) 5637 for (i = 0; i < n; i++)
5827 if (dispatch_table[i].name != 0) 5638 if (dispatch_table[i].builtin)
5828 assign_proc (SCHEME_A_ i, dispatch_table[i].name); 5639 assign_proc (SCHEME_A_ i, opname (i));
5829 5640
5830 /* initialization of global pointers to special symbols */ 5641 /* initialization of global pointers to special symbols */
5831 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 5642 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
5832 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 5643 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
5833 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 5644 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
5872scheme_set_external_data (SCHEME_P_ void *p) 5683scheme_set_external_data (SCHEME_P_ void *p)
5873{ 5684{
5874 SCHEME_V->ext_data = p; 5685 SCHEME_V->ext_data = p;
5875} 5686}
5876 5687
5877void 5688ecb_cold void
5878scheme_deinit (SCHEME_P) 5689scheme_deinit (SCHEME_P)
5879{ 5690{
5880 int i; 5691 int i;
5881 5692
5882#if SHOW_ERROR_LINE 5693#if SHOW_ERROR_LINE
5974{ 5785{
5975 dump_stack_reset (SCHEME_A); 5786 dump_stack_reset (SCHEME_A);
5976 SCHEME_V->envir = SCHEME_V->global_env; 5787 SCHEME_V->envir = SCHEME_V->global_env;
5977 SCHEME_V->file_i = 0; 5788 SCHEME_V->file_i = 0;
5978 SCHEME_V->load_stack[0].kind = port_input | port_string; 5789 SCHEME_V->load_stack[0].kind = port_input | port_string;
5979 SCHEME_V->load_stack[0].rep.string.start = (char *) cmd; /* This func respects const */ 5790 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */
5980 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *) cmd + strlen (cmd); 5791 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd);
5981 SCHEME_V->load_stack[0].rep.string.curr = (char *) cmd; 5792 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
5982#if USE_PORTS 5793#if USE_PORTS
5983 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 5794 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5984#endif 5795#endif
5985 SCHEME_V->retcode = 0; 5796 SCHEME_V->retcode = 0;
5986 SCHEME_V->interactive_repl = 0; 5797 SCHEME_V->interactive_repl = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines