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

Comparing cvsroot/microscheme/scheme.c (file contents):
Revision 1.6 by root, Wed Nov 25 10:57:17 2015 UTC vs.
Revision 1.24 by root, Fri Nov 27 02:12:08 2015 UTC

1 1/*
2/* T I N Y S C H E M E 1 . 4 1 2 * µscheme
3 *
4 * Copyright (C) 2015 Marc Alexander Lehmann <uscheme@schmorp.de>
5 * do as you want with this, attribution appreciated.
6 *
7 * Based opn tinyscheme-1.41 (original credits follow)
3 * Dimitrios Souflis (dsouflis@acm.org) 8 * Dimitrios Souflis (dsouflis@acm.org)
4 * Based on MiniScheme (original credits follow) 9 * Based on MiniScheme (original credits follow)
5 * (MINISCM) coded by Atsushi Moriwaki (11/5/1989) 10 * (MINISCM) coded by Atsushi Moriwaki (11/5/1989)
6 * (MINISCM) E-MAIL : moriwaki@kurims.kurims.kyoto-u.ac.jp 11 * (MINISCM) E-MAIL : moriwaki@kurims.kurims.kyoto-u.ac.jp
7 * (MINISCM) This version has been modified by R.C. Secrist. 12 * (MINISCM) This version has been modified by R.C. Secrist.
22# include <unistd.h> 27# include <unistd.h>
23#endif 28#endif
24#if USE_MATH 29#if USE_MATH
25# include <math.h> 30# include <math.h>
26#endif 31#endif
32
33#include "ecb.h"
27 34
28#include <sys/types.h> 35#include <sys/types.h>
29#include <sys/stat.h> 36#include <sys/stat.h>
30#include <fcntl.h> 37#include <fcntl.h>
31 38
128 c += 'a' - 'A'; 135 c += 'a' - 'A';
129 136
130 return c; 137 return c;
131} 138}
132 139
133#if USE_STRLWR 140static int
141xisdigit (char c)
142{
143 return c >= '0' && c <= '9';
144}
145
146#define toupper(c) xtoupper (c)
147#define tolower(c) xtolower (c)
148#define isdigit(c) xisdigit (c)
149
150#if USE_IGNORECASE
134static const char * 151static const char *
135strlwr (char *s) 152xstrlwr (char *s)
136{ 153{
137 const char *p = s; 154 const char *p = s;
138 155
139 while (*s) 156 while (*s)
140 { 157 {
142 s++; 159 s++;
143 } 160 }
144 161
145 return p; 162 return p;
146} 163}
147#endif
148 164
165#define stricmp(a,b) strcasecmp (a, b)
166#define strlwr(s) xstrlwr (s)
167
168#else
149#define stricmp(a,b) strcmp (a, b) 169# define stricmp(a,b) strcmp (a, b)
150#define strlwr(s) (s) 170# define strlwr(s) (s)
151#define toupper(c) xtoupper(c) 171#endif
152#define tolower(c) xtolower(c)
153 172
154#ifndef prompt 173#ifndef prompt
155# define prompt "ts> " 174# define prompt "ts> "
156#endif 175#endif
157 176
188#define T_SYNTAX 0x0010 207#define T_SYNTAX 0x0010
189#define T_IMMUTABLE 0x0020 208#define T_IMMUTABLE 0x0020
190#define T_ATOM 0x0040 /* only for gc */ 209#define T_ATOM 0x0040 /* only for gc */
191#define T_MARK 0x0080 /* only for gc */ 210#define T_MARK 0x0080 /* only for gc */
192 211
193static num num_add (num a, num b); 212enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
194static num num_mul (num a, num b); 213
195static num num_div (num a, num b); 214static num num_op (enum num_op op, num a, num b);
196static num num_intdiv (num a, num b); 215static num num_intdiv (num a, num b);
197static num num_sub (num a, num b);
198static num num_rem (num a, num b); 216static num num_rem (num a, num b);
199static num num_mod (num a, num b); 217static num num_mod (num a, num b);
200static int num_eq (num a, num b);
201static int num_gt (num a, num b);
202static int num_ge (num a, num b);
203static int num_lt (num a, num b);
204static int num_le (num a, num b);
205 218
206#if USE_MATH 219#if USE_MATH
207static double round_per_R5RS (double x); 220static double round_per_R5RS (double x);
208#endif 221#endif
209static int is_zero_rvalue (RVALUE x); 222static int is_zero_rvalue (RVALUE x);
210 223
211static INLINE int 224ecb_inline int
212num_is_integer (pointer p) 225num_is_integer (pointer p)
213{ 226{
214 return num_is_fixnum (p->object.number); 227 return num_is_fixnum (p->object.number);
215} 228}
216 229
220/* macros for cell operations */ 233/* macros for cell operations */
221#define typeflag(p) ((p)->flag + 0) 234#define typeflag(p) ((p)->flag + 0)
222#define set_typeflag(p,v) ((p)->flag = (v)) 235#define set_typeflag(p,v) ((p)->flag = (v))
223#define type(p) (typeflag (p) & T_MASKTYPE) 236#define type(p) (typeflag (p) & T_MASKTYPE)
224 237
225INTERFACE INLINE int 238INTERFACE int
226is_string (pointer p) 239is_string (pointer p)
227{ 240{
228 return type (p) == T_STRING; 241 return type (p) == T_STRING;
229} 242}
230 243
231#define strvalue(p) ((p)->object.string.svalue) 244#define strvalue(p) ((p)->object.string.svalue)
232#define strlength(p) ((p)->object.string.length) 245#define strlength(p) ((p)->object.string.length)
233 246
234INTERFACE int is_list (SCHEME_P_ pointer p); 247INTERFACE int is_list (SCHEME_P_ pointer p);
248
235INTERFACE INLINE int 249INTERFACE int
236is_vector (pointer p) 250is_vector (pointer p)
237{ 251{
238 return type (p) == T_VECTOR; 252 return type (p) == T_VECTOR;
239} 253}
240 254
255#define vecvalue(p) ((p)->object.vector.vvalue)
256#define veclength(p) ((p)->object.vector.length)
241INTERFACE void fill_vector (pointer vec, pointer obj); 257INTERFACE void fill_vector (pointer vec, pointer obj);
242INTERFACE uint32_t vector_length (pointer vec); 258INTERFACE uint32_t vector_length (pointer vec);
243INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); 259INTERFACE pointer vector_elem (pointer vec, uint32_t ielem);
244INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); 260INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a);
245 261
247vector_length (pointer vec) 263vector_length (pointer vec)
248{ 264{
249 return vec->object.vector.length; 265 return vec->object.vector.length;
250} 266}
251 267
252INTERFACE INLINE int 268INTERFACE int
253is_number (pointer p) 269is_number (pointer p)
254{ 270{
255 return type (p) == T_NUMBER; 271 return type (p) == T_NUMBER;
256} 272}
257 273
258INTERFACE INLINE int 274INTERFACE int
259is_integer (pointer p) 275is_integer (pointer p)
260{ 276{
261 if (!is_number (p)) 277 if (!is_number (p))
262 return 0; 278 return 0;
263 279
265 return 1; 281 return 1;
266 282
267 return 0; 283 return 0;
268} 284}
269 285
270INTERFACE INLINE int 286INTERFACE int
271is_real (pointer p) 287is_real (pointer p)
272{ 288{
273 return is_number (p) && !num_is_fixnum (p->object.number); 289 return is_number (p) && !num_is_fixnum (p->object.number);
274} 290}
275 291
276INTERFACE INLINE int 292INTERFACE int
277is_character (pointer p) 293is_character (pointer p)
278{ 294{
279 return type (p) == T_CHARACTER; 295 return type (p) == T_CHARACTER;
280} 296}
281 297
282INTERFACE INLINE char * 298INTERFACE char *
283string_value (pointer p) 299string_value (pointer p)
284{ 300{
285 return strvalue (p); 301 return strvalue (p);
286} 302}
287 303
288INLINE num 304ecb_inline num
289nvalue (pointer p) 305nvalue (pointer p)
290{ 306{
291 return (p)->object.number; 307 return (p)->object.number;
292} 308}
293 309
314{ 330{
315 return num_get_rvalue (p->object.number); 331 return num_get_rvalue (p->object.number);
316} 332}
317 333
318#define ivalue_unchecked(p) ((p)->object.number.value.ivalue) 334#define ivalue_unchecked(p) ((p)->object.number.value.ivalue)
319#if USE_FLOAT 335#if USE_REAL
320# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 336# define rvalue_unchecked(p) ((p)->object.number.value.rvalue)
321# define set_num_integer(p) (p)->object.number.is_fixnum=1; 337# define set_num_integer(p) (p)->object.number.is_fixnum=1;
322# define set_num_real(p) (p)->object.number.is_fixnum=0; 338# define set_num_real(p) (p)->object.number.is_fixnum=0;
323#else 339#else
324# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 340# define rvalue_unchecked(p) ((p)->object.number.value.ivalue)
325# define set_num_integer(p) 0 341# define set_num_integer(p) 0
326# define set_num_real(p) 0 342# define set_num_real(p) 0
327#endif 343#endif
344
328INTERFACE long 345INTERFACE long
329charvalue (pointer p) 346charvalue (pointer p)
330{ 347{
331 return ivalue_unchecked (p); 348 return ivalue_unchecked (p);
332} 349}
333 350
334INTERFACE INLINE int 351INTERFACE int
335is_port (pointer p) 352is_port (pointer p)
336{ 353{
337 return type (p) == T_PORT; 354 return type (p) == T_PORT;
338} 355}
339 356
340INTERFACE INLINE int 357INTERFACE int
341is_inport (pointer p) 358is_inport (pointer p)
342{ 359{
343 return is_port (p) && p->object.port->kind & port_input; 360 return is_port (p) && p->object.port->kind & port_input;
344} 361}
345 362
346INTERFACE INLINE int 363INTERFACE int
347is_outport (pointer p) 364is_outport (pointer p)
348{ 365{
349 return is_port (p) && p->object.port->kind & port_output; 366 return is_port (p) && p->object.port->kind & port_output;
350} 367}
351 368
352INTERFACE INLINE int 369INTERFACE int
353is_pair (pointer p) 370is_pair (pointer p)
354{ 371{
355 return type (p) == T_PAIR; 372 return type (p) == T_PAIR;
356} 373}
357 374
358#define car(p) ((p)->object.cons.car + 0) 375#define car(p) ((p)->object.cons.car + 0)
359#define cdr(p) ((p)->object.cons.cdr) /* find_consecutive_cells uses &cdr */ 376#define cdr(p) ((p)->object.cons.cdr + 0)
360 377
361#define caar(p) car (car (p)) 378static pointer caar (pointer p) { return car (car (p)); }
362#define cadr(p) car (cdr (p)) 379static pointer cadr (pointer p) { return car (cdr (p)); }
363#define cdar(p) cdr (car (p)) 380static pointer cdar (pointer p) { return cdr (car (p)); }
364#define cddr(p) cdr (cdr (p)) 381static pointer cddr (pointer p) { return cdr (cdr (p)); }
365 382
366#define cadar(p) car (cdr (car (p))) 383static pointer cadar (pointer p) { return car (cdr (car (p))); }
367#define caddr(p) car (cdr (cdr (p))) 384static pointer caddr (pointer p) { return car (cdr (cdr (p))); }
368#define cdaar(p) cdr (car (car (p))) 385static pointer cdaar (pointer p) { return cdr (car (car (p))); }
369 386
370INTERFACE void 387INTERFACE void
371set_car (pointer p, pointer q) 388set_car (pointer p, pointer q)
372{ 389{
373 p->object.cons.car = q; 390 p->object.cons.car = q;
389pair_cdr (pointer p) 406pair_cdr (pointer p)
390{ 407{
391 return cdr (p); 408 return cdr (p);
392} 409}
393 410
394INTERFACE INLINE int 411INTERFACE int
395is_symbol (pointer p) 412is_symbol (pointer p)
396{ 413{
397 return type (p) == T_SYMBOL; 414 return type (p) == T_SYMBOL;
398} 415}
399 416
400INTERFACE INLINE char * 417INTERFACE char *
401symname (pointer p) 418symname (pointer p)
402{ 419{
403 return strvalue (car (p)); 420 return strvalue (car (p));
404} 421}
405 422
406#if USE_PLIST 423#if USE_PLIST
407SCHEME_EXPORT INLINE int 424SCHEME_EXPORT int
408hasprop (pointer p) 425hasprop (pointer p)
409{ 426{
410 return typeflag (p) & T_SYMBOL; 427 return typeflag (p) & T_SYMBOL;
411} 428}
412 429
413# define symprop(p) cdr(p) 430# define symprop(p) cdr(p)
414#endif 431#endif
415 432
416INTERFACE INLINE int 433INTERFACE int
417is_syntax (pointer p) 434is_syntax (pointer p)
418{ 435{
419 return typeflag (p) & T_SYNTAX; 436 return typeflag (p) & T_SYNTAX;
420} 437}
421 438
422INTERFACE INLINE int 439INTERFACE int
423is_proc (pointer p) 440is_proc (pointer p)
424{ 441{
425 return type (p) == T_PROC; 442 return type (p) == T_PROC;
426} 443}
427 444
428INTERFACE INLINE int 445INTERFACE int
429is_foreign (pointer p) 446is_foreign (pointer p)
430{ 447{
431 return type (p) == T_FOREIGN; 448 return type (p) == T_FOREIGN;
432} 449}
433 450
434INTERFACE INLINE char * 451INTERFACE char *
435syntaxname (pointer p) 452syntaxname (pointer p)
436{ 453{
437 return strvalue (car (p)); 454 return strvalue (car (p));
438} 455}
439 456
440#define procnum(p) ivalue (p) 457#define procnum(p) ivalue (p)
441static const char *procname (pointer x); 458static const char *procname (pointer x);
442 459
443INTERFACE INLINE int 460INTERFACE int
444is_closure (pointer p) 461is_closure (pointer p)
445{ 462{
446 return type (p) == T_CLOSURE; 463 return type (p) == T_CLOSURE;
447} 464}
448 465
449INTERFACE INLINE int 466INTERFACE int
450is_macro (pointer p) 467is_macro (pointer p)
451{ 468{
452 return type (p) == T_MACRO; 469 return type (p) == T_MACRO;
453} 470}
454 471
455INTERFACE INLINE pointer 472INTERFACE pointer
456closure_code (pointer p) 473closure_code (pointer p)
457{ 474{
458 return car (p); 475 return car (p);
459} 476}
460 477
461INTERFACE INLINE pointer 478INTERFACE pointer
462closure_env (pointer p) 479closure_env (pointer p)
463{ 480{
464 return cdr (p); 481 return cdr (p);
465} 482}
466 483
467INTERFACE INLINE int 484INTERFACE int
468is_continuation (pointer p) 485is_continuation (pointer p)
469{ 486{
470 return type (p) == T_CONTINUATION; 487 return type (p) == T_CONTINUATION;
471} 488}
472 489
473#define cont_dump(p) cdr (p) 490#define cont_dump(p) cdr (p)
474#define set_cont_dump(p,v) set_cdr ((p), (v)) 491#define set_cont_dump(p,v) set_cdr ((p), (v))
475 492
476/* To do: promise should be forced ONCE only */ 493/* To do: promise should be forced ONCE only */
477INTERFACE INLINE int 494INTERFACE int
478is_promise (pointer p) 495is_promise (pointer p)
479{ 496{
480 return type (p) == T_PROMISE; 497 return type (p) == T_PROMISE;
481} 498}
482 499
483INTERFACE INLINE int 500INTERFACE int
484is_environment (pointer p) 501is_environment (pointer p)
485{ 502{
486 return type (p) == T_ENVIRONMENT; 503 return type (p) == T_ENVIRONMENT;
487} 504}
488 505
489#define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT) 506#define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT)
490 507
491#define is_atom1(p) (TYPESET_ATOM & (1U << type (p)))
492#define is_atom(p) (typeflag (p) & T_ATOM) 508#define is_atom(p) (typeflag (p) & T_ATOM)
493#define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) 509#define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM)
494#define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM) 510#define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM)
495 511
496#define is_mark(p) (typeflag (p) & T_MARK) 512#define is_mark(p) (typeflag (p) & T_MARK)
497#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) 513#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
498#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) 514#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
499 515
500#if 0
501static int
502is_atom(pointer p)
503{
504 if (!is_atom1(p) != !is_atom2(p))
505 printf ("atoms disagree %x\n", typeflag(p));
506
507 return is_atom2(p);
508}
509#endif
510
511INTERFACE INLINE int 516INTERFACE int
512is_immutable (pointer p) 517is_immutable (pointer p)
513{ 518{
514 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; 519 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
515} 520}
516 521
517INTERFACE INLINE void 522INTERFACE void
518setimmutable (pointer p) 523setimmutable (pointer p)
519{ 524{
520#if USE_ERROR_CHECKING 525#if USE_ERROR_CHECKING
521 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 526 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
522#endif 527#endif
523} 528}
524 529
525#if USE_CHAR_CLASSIFIERS 530#if USE_CHAR_CLASSIFIERS
526static INLINE int 531ecb_inline int
527Cisalpha (int c) 532Cisalpha (int c)
528{ 533{
529 return isascii (c) && isalpha (c); 534 return isascii (c) && isalpha (c);
530} 535}
531 536
532static INLINE int 537ecb_inline int
533Cisdigit (int c) 538Cisdigit (int c)
534{ 539{
535 return isascii (c) && isdigit (c); 540 return isascii (c) && isdigit (c);
536} 541}
537 542
538static INLINE int 543ecb_inline int
539Cisspace (int c) 544Cisspace (int c)
540{ 545{
541 return isascii (c) && isspace (c); 546 return isascii (c) && isspace (c);
542} 547}
543 548
544static INLINE int 549ecb_inline int
545Cisupper (int c) 550Cisupper (int c)
546{ 551{
547 return isascii (c) && isupper (c); 552 return isascii (c) && isupper (c);
548} 553}
549 554
550static INLINE int 555ecb_inline int
551Cislower (int c) 556Cislower (int c)
552{ 557{
553 return isascii (c) && islower (c); 558 return isascii (c) && islower (c);
554} 559}
555#endif 560#endif
616#endif 621#endif
617 622
618static int file_push (SCHEME_P_ const char *fname); 623static int file_push (SCHEME_P_ const char *fname);
619static void file_pop (SCHEME_P); 624static void file_pop (SCHEME_P);
620static int file_interactive (SCHEME_P); 625static int file_interactive (SCHEME_P);
621static INLINE int is_one_of (char *s, int c); 626ecb_inline int is_one_of (char *s, int c);
622static int alloc_cellseg (SCHEME_P_ int n); 627static int alloc_cellseg (SCHEME_P_ int n);
623static long binary_decode (const char *s);
624static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); 628ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
625static void finalize_cell (SCHEME_P_ pointer a); 629static void finalize_cell (SCHEME_P_ pointer a);
626static int count_consecutive_cells (pointer x, int needed); 630static int count_consecutive_cells (pointer x, int needed);
627static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 631static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
628static pointer mk_number (SCHEME_P_ const num n); 632static pointer mk_number (SCHEME_P_ const num n);
629static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); 633static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
646static int basic_inchar (port *pt); 650static int basic_inchar (port *pt);
647static int inchar (SCHEME_P); 651static int inchar (SCHEME_P);
648static void backchar (SCHEME_P_ int c); 652static void backchar (SCHEME_P_ int c);
649static char *readstr_upto (SCHEME_P_ char *delim); 653static char *readstr_upto (SCHEME_P_ char *delim);
650static pointer readstrexp (SCHEME_P); 654static pointer readstrexp (SCHEME_P);
651static INLINE int skipspace (SCHEME_P); 655ecb_inline int skipspace (SCHEME_P);
652static int token (SCHEME_P); 656static int token (SCHEME_P);
653static void printslashstring (SCHEME_P_ char *s, int len); 657static void printslashstring (SCHEME_P_ char *s, int len);
654static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 658static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
655static void printatom (SCHEME_P_ pointer l, int f); 659static void printatom (SCHEME_P_ pointer l, int f);
656static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 660static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
660static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); 664static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list);
661static pointer revappend (SCHEME_P_ pointer a, pointer b); 665static pointer revappend (SCHEME_P_ pointer a, pointer b);
662static pointer ss_get_cont (SCHEME_P); 666static pointer ss_get_cont (SCHEME_P);
663static void ss_set_cont (SCHEME_P_ pointer cont); 667static void ss_set_cont (SCHEME_P_ pointer cont);
664static void dump_stack_mark (SCHEME_P); 668static void dump_stack_mark (SCHEME_P);
665static pointer opexe_0 (SCHEME_P_ enum scheme_opcodes op); 669static int opexe_0 (SCHEME_P_ enum scheme_opcodes op);
670static int opexe_1 (SCHEME_P_ enum scheme_opcodes op);
666static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); 671static int opexe_2 (SCHEME_P_ enum scheme_opcodes op);
667static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); 672static int opexe_3 (SCHEME_P_ enum scheme_opcodes op);
668static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); 673static int opexe_4 (SCHEME_P_ enum scheme_opcodes op);
669static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); 674static int opexe_5 (SCHEME_P_ enum scheme_opcodes op);
670static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); 675static int opexe_6 (SCHEME_P_ enum scheme_opcodes op);
671static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 676static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
672static void assign_syntax (SCHEME_P_ const char *name); 677static void assign_syntax (SCHEME_P_ const char *name);
673static int syntaxnum (pointer p); 678static int syntaxnum (pointer p);
674static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 679static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
675 680
676static num 681static num
677num_add (num a, num b) 682num_op (enum num_op op, num a, num b)
678{ 683{
679 num ret; 684 num ret;
680 685
681 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 686 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
682 687
683 if (num_is_fixnum (ret)) 688 if (num_is_fixnum (ret))
684 num_set_ivalue (ret, num_get_ivalue (a) + num_get_ivalue (b)); 689 {
690 IVALUE av = num_get_ivalue (a);
691 IVALUE bv = num_get_ivalue (b);
692
693 switch (op)
694 {
695 case NUM_ADD: av += bv; break;
696 case NUM_SUB: av -= bv; break;
697 case NUM_MUL: av *= bv; break;
698 case NUM_INTDIV: av /= bv; break;
699 }
700
701 num_set_ivalue (ret, av);
702 }
685 else 703 else
686 num_set_rvalue (ret, num_get_rvalue (a) + num_get_rvalue (b)); 704 {
705 RVALUE av = num_get_rvalue (a);
706 RVALUE bv = num_get_rvalue (b);
687 707
688 return ret; 708 switch (op)
689} 709 {
710 case NUM_ADD: av += bv; break;
711 case NUM_SUB: av -= bv; break;
712 case NUM_MUL: av *= bv; break;
713 case NUM_INTDIV: av /= bv; break;
714 }
690 715
691static num 716 num_set_rvalue (ret, av);
692num_mul (num a, num b) 717 }
693{
694 num ret;
695
696 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
697
698 if (num_is_fixnum (ret))
699 num_set_ivalue (ret, num_get_ivalue (a) * num_get_ivalue (b));
700 else
701 num_set_rvalue (ret, num_get_rvalue (a) * num_get_rvalue (b));
702 718
703 return ret; 719 return ret;
704} 720}
705 721
706static num 722static num
717 733
718 return ret; 734 return ret;
719} 735}
720 736
721static num 737static num
722num_intdiv (num a, num b)
723{
724 num ret;
725
726 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
727
728 if (num_is_fixnum (ret))
729 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b));
730 else
731 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b));
732
733 return ret;
734}
735
736static num
737num_sub (num a, num b)
738{
739 num ret;
740
741 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
742
743 if (num_is_fixnum (ret))
744 num_set_ivalue (ret, num_get_ivalue (a) - num_get_ivalue (b));
745 else
746 num_set_rvalue (ret, num_get_rvalue (a) - num_get_rvalue (b));
747
748 return ret;
749}
750
751static num
752num_rem (num a, num b) 738num_rem (num a, num b)
753{ 739{
754 num ret; 740 num ret;
755 long e1, e2, res; 741 long e1, e2, res;
756 742
792 778
793 num_set_ivalue (ret, res); 779 num_set_ivalue (ret, res);
794 return ret; 780 return ret;
795} 781}
796 782
783/* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */
797static int 784static int
798num_eq (num a, num b) 785num_cmp (num a, num b)
799{ 786{
787 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
800 int ret; 788 int ret;
801 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
802 789
803 if (is_fixnum) 790 if (is_fixnum)
804 ret = num_get_ivalue (a) == num_get_ivalue (b); 791 {
792 IVALUE av = num_get_ivalue (a);
793 IVALUE bv = num_get_ivalue (b);
794
795 ret = av == bv ? 0 : av < bv ? -1 : +1;
796 }
805 else 797 else
806 ret = num_get_rvalue (a) == num_get_rvalue (b); 798 {
799 RVALUE av = num_get_rvalue (a);
800 RVALUE bv = num_get_rvalue (b);
801
802 ret = av == bv ? 0 : av < bv ? -1 : +1;
803 }
807 804
808 return ret; 805 return ret;
809}
810
811
812static int
813num_gt (num a, num b)
814{
815 int ret;
816 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
817
818 if (is_fixnum)
819 ret = num_get_ivalue (a) > num_get_ivalue (b);
820 else
821 ret = num_get_rvalue (a) > num_get_rvalue (b);
822
823 return ret;
824}
825
826static int
827num_ge (num a, num b)
828{
829 return !num_lt (a, b);
830}
831
832static int
833num_lt (num a, num b)
834{
835 int ret;
836 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
837
838 if (is_fixnum)
839 ret = num_get_ivalue (a) < num_get_ivalue (b);
840 else
841 ret = num_get_rvalue (a) < num_get_rvalue (b);
842
843 return ret;
844}
845
846static int
847num_le (num a, num b)
848{
849 return !num_gt (a, b);
850} 806}
851 807
852#if USE_MATH 808#if USE_MATH
853 809
854/* Round to nearest. Round to even if midway */ 810/* Round to nearest. Round to even if midway */
864 return ce; 820 return ce;
865 else if (dfl < dce) 821 else if (dfl < dce)
866 return fl; 822 return fl;
867 else 823 else
868 { 824 {
869 if (fmod (fl, 2.0) == 0.0) /* I imagine this holds */ 825 if (fmod (fl, 2) == 0) /* I imagine this holds */
870 return fl; 826 return fl;
871 else 827 else
872 return ce; 828 return ce;
873 } 829 }
874} 830}
875#endif 831#endif
876 832
877static int 833static int
878is_zero_rvalue (RVALUE x) 834is_zero_rvalue (RVALUE x)
879{ 835{
880#if USE_FLOAT 836 return x == 0;
837#if 0
838#if USE_REAL
881 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ 839 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */
882#else 840#else
883 return x == 0; 841 return x == 0;
884#endif 842#endif
885} 843#endif
886
887static long
888binary_decode (const char *s)
889{
890 long x = 0;
891
892 while (*s != 0 && (*s == '1' || *s == '0'))
893 {
894 x <<= 1;
895 x += *s - '0';
896 s++;
897 }
898
899 return x;
900} 844}
901 845
902/* allocate new cell segment */ 846/* allocate new cell segment */
903static int 847static int
904alloc_cellseg (SCHEME_P_ int n) 848alloc_cellseg (SCHEME_P_ int n)
975 919
976 return n; 920 return n;
977} 921}
978 922
979/* get new cell. parameter a, b is marked by gc. */ 923/* get new cell. parameter a, b is marked by gc. */
980static INLINE pointer 924ecb_inline pointer
981get_cell_x (SCHEME_P_ pointer a, pointer b) 925get_cell_x (SCHEME_P_ pointer a, pointer b)
982{ 926{
983 if (SCHEME_V->free_cell == NIL) 927 if (ecb_expect_false (SCHEME_V->free_cell == NIL))
984 { 928 {
985 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 929 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
986 return S_SINK; 930 return S_SINK;
987 931
988 if (SCHEME_V->free_cell == NIL) 932 if (SCHEME_V->free_cell == NIL)
1063 push_recent_alloc (SCHEME_A_ v, NIL); 1007 push_recent_alloc (SCHEME_A_ v, NIL);
1064 1008
1065 return v; 1009 return v;
1066} 1010}
1067 1011
1068static INLINE void 1012ecb_inline void
1069ok_to_freely_gc (SCHEME_P) 1013ok_to_freely_gc (SCHEME_P)
1070{ 1014{
1071 set_car (S_SINK, NIL); 1015 set_car (S_SINK, NIL);
1072} 1016}
1073 1017
1132 1076
1133 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); 1077 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1134 set_typeflag (x, T_SYMBOL); 1078 set_typeflag (x, T_SYMBOL);
1135 setimmutable (car (x)); 1079 setimmutable (car (x));
1136 1080
1137 location = hash_fn (name, vector_length (SCHEME_V->oblist)); 1081 location = hash_fn (name, veclength (SCHEME_V->oblist));
1138 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); 1082 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location)));
1139 return x; 1083 return x;
1140} 1084}
1141 1085
1142static INLINE pointer 1086ecb_inline pointer
1143oblist_find_by_name (SCHEME_P_ const char *name) 1087oblist_find_by_name (SCHEME_P_ const char *name)
1144{ 1088{
1145 int location; 1089 int location;
1146 pointer x; 1090 pointer x;
1147 char *s; 1091 char *s;
1148 1092
1149 location = hash_fn (name, vector_length (SCHEME_V->oblist)); 1093 location = hash_fn (name, veclength (SCHEME_V->oblist));
1150 1094
1151 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1095 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1152 { 1096 {
1153 s = symname (car (x)); 1097 s = symname (car (x));
1154 1098
1165{ 1109{
1166 int i; 1110 int i;
1167 pointer x; 1111 pointer x;
1168 pointer ob_list = NIL; 1112 pointer ob_list = NIL;
1169 1113
1170 for (i = 0; i < vector_length (SCHEME_V->oblist); i++) 1114 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1171 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1115 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1172 ob_list = cons (x, ob_list); 1116 ob_list = cons (x, ob_list);
1173 1117
1174 return ob_list; 1118 return ob_list;
1175} 1119}
1180oblist_initial_value (SCHEME_P) 1124oblist_initial_value (SCHEME_P)
1181{ 1125{
1182 return NIL; 1126 return NIL;
1183} 1127}
1184 1128
1185static INLINE pointer 1129ecb_inline pointer
1186oblist_find_by_name (SCHEME_P_ const char *name) 1130oblist_find_by_name (SCHEME_P_ const char *name)
1187{ 1131{
1188 pointer x; 1132 pointer x;
1189 char *s; 1133 char *s;
1190 1134
1317 } 1261 }
1318 1262
1319 return q; 1263 return q;
1320} 1264}
1321 1265
1322/* get new string */
1323INTERFACE pointer 1266INTERFACE pointer
1324mk_string (SCHEME_P_ const char *str) 1267mk_empty_string (SCHEME_P_ uint32_t len, char fill)
1325{ 1268{
1326 return mk_counted_string (SCHEME_A_ str, strlen (str)); 1269 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1270
1271 set_typeflag (x, T_STRING | T_ATOM);
1272 strvalue (x) = store_string (SCHEME_A_ len, 0, fill);
1273 strlength (x) = len;
1274 return x;
1327} 1275}
1328 1276
1329INTERFACE pointer 1277INTERFACE pointer
1330mk_counted_string (SCHEME_P_ const char *str, uint32_t len) 1278mk_counted_string (SCHEME_P_ const char *str, uint32_t len)
1331{ 1279{
1336 strlength (x) = len; 1284 strlength (x) = len;
1337 return x; 1285 return x;
1338} 1286}
1339 1287
1340INTERFACE pointer 1288INTERFACE pointer
1341mk_empty_string (SCHEME_P_ uint32_t len, char fill) 1289mk_string (SCHEME_P_ const char *str)
1342{ 1290{
1343 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1291 return mk_counted_string (SCHEME_A_ str, strlen (str));
1344
1345 set_typeflag (x, T_STRING | T_ATOM);
1346 strvalue (x) = store_string (SCHEME_A_ len, 0, fill);
1347 strlength (x) = len;
1348 return x;
1349} 1292}
1350 1293
1351INTERFACE pointer 1294INTERFACE pointer
1352mk_vector (SCHEME_P_ uint32_t len) 1295mk_vector (SCHEME_P_ uint32_t len)
1353{ 1296{
1358fill_vector (pointer vec, pointer obj) 1301fill_vector (pointer vec, pointer obj)
1359{ 1302{
1360 int i; 1303 int i;
1361 1304
1362 for (i = 0; i < vec->object.vector.length; i++) 1305 for (i = 0; i < vec->object.vector.length; i++)
1363 vec->object.vector.vvalue[i] = obj; 1306 vecvalue (vec)[i] = obj;
1364} 1307}
1365 1308
1366INTERFACE pointer 1309INTERFACE pointer
1367vector_elem (pointer vec, uint32_t ielem) 1310vector_elem (pointer vec, uint32_t ielem)
1368{ 1311{
1369 return vec->object.vector.vvalue[ielem]; 1312 return vecvalue(vec)[ielem];
1370} 1313}
1371 1314
1372INTERFACE void 1315INTERFACE void
1373set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1316set_vector_elem (pointer vec, uint32_t ielem, pointer a)
1374{ 1317{
1375 vec->object.vector.vvalue[ielem] = a; 1318 vecvalue(vec)[ielem] = a;
1376} 1319}
1377 1320
1378/* get new symbol */ 1321/* get new symbol */
1379INTERFACE pointer 1322INTERFACE pointer
1380mk_symbol (SCHEME_P_ const char *name) 1323mk_symbol (SCHEME_P_ const char *name)
1390 1333
1391INTERFACE pointer 1334INTERFACE pointer
1392gensym (SCHEME_P) 1335gensym (SCHEME_P)
1393{ 1336{
1394 pointer x; 1337 pointer x;
1395 char name[40];
1396 1338
1397 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) 1339 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1398 { 1340 {
1399 strcpy (name, "gensym-"); 1341 char name[40] = "gensym-";
1400 xnum (name + 7, SCHEME_V->gensym_cnt); 1342 xnum (name + 7, SCHEME_V->gensym_cnt);
1401 1343
1402 /* first check oblist */ 1344 /* first check oblist */
1403 x = oblist_find_by_name (SCHEME_A_ name); 1345 x = oblist_find_by_name (SCHEME_A_ name);
1404 1346
1405 if (x != NIL) 1347 if (x == NIL)
1406 continue;
1407 else
1408 { 1348 {
1409 x = oblist_add_by_name (SCHEME_A_ name); 1349 x = oblist_add_by_name (SCHEME_A_ name);
1410 return x; 1350 return x;
1411 } 1351 }
1412 } 1352 }
1421 char c, *p; 1361 char c, *p;
1422 int has_dec_point = 0; 1362 int has_dec_point = 0;
1423 int has_fp_exp = 0; 1363 int has_fp_exp = 0;
1424 1364
1425#if USE_COLON_HOOK 1365#if USE_COLON_HOOK
1426
1427 if ((p = strstr (q, "::")) != 0) 1366 if ((p = strstr (q, "::")) != 0)
1428 { 1367 {
1429 *p = 0; 1368 *p = 0;
1430 return cons (SCHEME_V->COLON_HOOK, 1369 return cons (SCHEME_V->COLON_HOOK,
1431 cons (cons (SCHEME_V->QUOTE, 1370 cons (cons (SCHEME_V->QUOTE,
1432 cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL))); 1371 cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL)));
1433 } 1372 }
1434
1435#endif 1373#endif
1436 1374
1437 p = q; 1375 p = q;
1438 c = *p++; 1376 c = *p++;
1439 1377
1488 1426
1489 return mk_symbol (SCHEME_A_ strlwr (q)); 1427 return mk_symbol (SCHEME_A_ strlwr (q));
1490 } 1428 }
1491 } 1429 }
1492 1430
1493#if USE_FLOAT 1431#if USE_REAL
1494 if (has_dec_point) 1432 if (has_dec_point)
1495 return mk_real (SCHEME_A_ atof (q)); 1433 return mk_real (SCHEME_A_ atof (q));
1496#endif 1434#endif
1497 1435
1498 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1436 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1500 1438
1501/* make constant */ 1439/* make constant */
1502static pointer 1440static pointer
1503mk_sharp_const (SCHEME_P_ char *name) 1441mk_sharp_const (SCHEME_P_ char *name)
1504{ 1442{
1505 long x;
1506 char tmp[STRBUFFSIZE];
1507
1508 if (!strcmp (name, "t")) 1443 if (!strcmp (name, "t"))
1509 return S_T; 1444 return S_T;
1510 else if (!strcmp (name, "f")) 1445 else if (!strcmp (name, "f"))
1511 return S_F; 1446 return S_F;
1512 else if (*name == 'o') /* #o (octal) */
1513 {
1514 x = strtol (name + 1, 0, 8);
1515 return mk_integer (SCHEME_A_ x);
1516 }
1517 else if (*name == 'd') /* #d (decimal) */
1518 {
1519 x = strtol (name + 1, 0, 10);
1520 return mk_integer (SCHEME_A_ x);
1521 }
1522 else if (*name == 'x') /* #x (hex) */
1523 {
1524 x = strtol (name + 1, 0, 16);
1525 return mk_integer (SCHEME_A_ x);
1526 }
1527 else if (*name == 'b') /* #b (binary) */
1528 {
1529 x = binary_decode (name + 1);
1530 return mk_integer (SCHEME_A_ x);
1531 }
1532 else if (*name == '\\') /* #\w (character) */ 1447 else if (*name == '\\') /* #\w (character) */
1533 { 1448 {
1534 int c = 0; 1449 int c;
1535 1450
1536 if (stricmp (name + 1, "space") == 0) 1451 if (stricmp (name + 1, "space") == 0)
1537 c = ' '; 1452 c = ' ';
1538 else if (stricmp (name + 1, "newline") == 0) 1453 else if (stricmp (name + 1, "newline") == 0)
1539 c = '\n'; 1454 c = '\n';
1541 c = '\r'; 1456 c = '\r';
1542 else if (stricmp (name + 1, "tab") == 0) 1457 else if (stricmp (name + 1, "tab") == 0)
1543 c = '\t'; 1458 c = '\t';
1544 else if (name[1] == 'x' && name[2] != 0) 1459 else if (name[1] == 'x' && name[2] != 0)
1545 { 1460 {
1546 int c1 = strtol (name + 2, 0, 16); 1461 long c1 = strtol (name + 2, 0, 16);
1547 1462
1548 if (c1 <= UCHAR_MAX) 1463 if (0 <= c1 && c1 <= UCHAR_MAX)
1549 c = c1; 1464 c = c1;
1550 else 1465 else
1551 return NIL; 1466 return NIL;
1552 1467 }
1553#if USE_ASCII_NAMES 1468#if USE_ASCII_NAMES
1554 }
1555 else if (is_ascii_name (name + 1, &c)) 1469 else if (is_ascii_name (name + 1, &c))
1556 {
1557 /* nothing */ 1470 /* nothing */;
1558#endif 1471#endif
1559 }
1560 else if (name[2] == 0) 1472 else if (name[2] == 0)
1561 c = name[1]; 1473 c = name[1];
1562 else 1474 else
1563 return NIL; 1475 return NIL;
1564 1476
1565 return mk_character (SCHEME_A_ c); 1477 return mk_character (SCHEME_A_ c);
1566 } 1478 }
1567 else 1479 else
1480 {
1481 /* identify base by string index */
1482 const char baseidx[17] = "ffbf" "ffff" "ofdf" "ffff" "x";
1483 char *base = strchr (baseidx, *name);
1484
1485 if (base)
1486 return mk_integer (SCHEME_A_ strtol (name + 1, 0, base - baseidx));
1487
1568 return NIL; 1488 return NIL;
1489 }
1569} 1490}
1570 1491
1571/* ========== garbage collector ========== */ 1492/* ========== garbage collector ========== */
1572 1493
1573/*-- 1494/*--
1574 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, 1495 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1,
1575 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1496 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1576 * for marking. 1497 * for marking.
1498 *
1499 * The exception is vectors - vectors are currently marked recursively,
1500 * which is inherited form tinyscheme and could be fixed by having another
1501 * word of context in the vector
1577 */ 1502 */
1578static void 1503static void
1579mark (pointer a) 1504mark (pointer a)
1580{ 1505{
1581 pointer t, q, p; 1506 pointer t, q, p;
1583 t = 0; 1508 t = 0;
1584 p = a; 1509 p = a;
1585E2: 1510E2:
1586 setmark (p); 1511 setmark (p);
1587 1512
1588 if (is_vector (p)) 1513 if (ecb_expect_false (is_vector (p)))
1589 { 1514 {
1590 int i; 1515 int i;
1591 1516
1592 for (i = 0; i < p->object.vector.length; i++) 1517 for (i = 0; i < p->object.vector.length; i++)
1593 mark (p->object.vector.vvalue[i]); 1518 mark (vecvalue (p)[i]);
1594 } 1519 }
1595 1520
1596 if (is_atom (p)) 1521 if (is_atom (p))
1597 goto E6; 1522 goto E6;
1598 1523
1716} 1641}
1717 1642
1718static void 1643static void
1719finalize_cell (SCHEME_P_ pointer a) 1644finalize_cell (SCHEME_P_ pointer a)
1720{ 1645{
1646 /* TODO, fast bitmap check? */
1721 if (is_string (a)) 1647 if (is_string (a))
1722 free (strvalue (a)); 1648 free (strvalue (a));
1723 else if (is_vector (a)) 1649 else if (is_vector (a))
1724 free (a->object.vector.vvalue); 1650 free (vecvalue (a));
1725#if USE_PORTS 1651#if USE_PORTS
1726 else if (is_port (a)) 1652 else if (is_port (a))
1727 { 1653 {
1728 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) 1654 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit)
1729 port_close (SCHEME_A_ a, port_input | port_output); 1655 port_close (SCHEME_A_ a, port_input | port_output);
2306 } 2232 }
2307 } 2233 }
2308} 2234}
2309 2235
2310/* check c is in chars */ 2236/* check c is in chars */
2311static INLINE int 2237ecb_inline int
2312is_one_of (char *s, int c) 2238is_one_of (char *s, int c)
2313{ 2239{
2314 if (c == EOF) 2240 if (c == EOF)
2315 return 1; 2241 return 1;
2316 2242
2317 return !!strchr (s, c); 2243 return !!strchr (s, c);
2318} 2244}
2319 2245
2320/* skip white characters */ 2246/* skip white characters */
2321static INLINE int 2247ecb_inline int
2322skipspace (SCHEME_P) 2248skipspace (SCHEME_P)
2323{ 2249{
2324 int c, curr_line = 0; 2250 int c, curr_line = 0;
2325 2251
2326 do 2252 do
2556 2482
2557 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2483 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2558 { 2484 {
2559 if (num_is_integer (l)) 2485 if (num_is_integer (l))
2560 xnum (p, ivalue_unchecked (l)); 2486 xnum (p, ivalue_unchecked (l));
2561#if USE_FLOAT 2487#if USE_REAL
2562 else 2488 else
2563 { 2489 {
2564 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2490 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2565 /* r5rs says there must be a '.' (unless 'e'?) */ 2491 /* r5rs says there must be a '.' (unless 'e'?) */
2566 f = strcspn (p, ".e"); 2492 f = strcspn (p, ".e");
2820 } 2746 }
2821 else if (is_number (a)) 2747 else if (is_number (a))
2822 { 2748 {
2823 if (is_number (b)) 2749 if (is_number (b))
2824 if (num_is_integer (a) == num_is_integer (b)) 2750 if (num_is_integer (a) == num_is_integer (b))
2825 return num_eq (nvalue (a), nvalue (b)); 2751 return num_cmp (nvalue (a), nvalue (b)) == 0;
2826 2752
2827 return 0; 2753 return 0;
2828 } 2754 }
2829 else if (is_character (a)) 2755 else if (is_character (a))
2830 { 2756 {
2897 2823
2898 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2824 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2899 setenvironment (SCHEME_V->envir); 2825 setenvironment (SCHEME_V->envir);
2900} 2826}
2901 2827
2902static INLINE void 2828ecb_inline void
2903new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2829new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2904{ 2830{
2905 pointer slot = immutable_cons (variable, value); 2831 pointer slot = immutable_cons (variable, value);
2906 2832
2907 if (is_vector (car (env))) 2833 if (is_vector (car (env)))
2908 { 2834 {
2909 int location = hash_fn (symname (variable), vector_length (car (env))); 2835 int location = hash_fn (symname (variable), veclength (car (env)));
2910 2836
2911 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2837 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location)));
2912 } 2838 }
2913 else 2839 else
2914 set_car (env, immutable_cons (slot, car (env))); 2840 set_car (env, immutable_cons (slot, car (env)));
2922 2848
2923 for (x = env; x != NIL; x = cdr (x)) 2849 for (x = env; x != NIL; x = cdr (x))
2924 { 2850 {
2925 if (is_vector (car (x))) 2851 if (is_vector (car (x)))
2926 { 2852 {
2927 location = hash_fn (symname (hdl), vector_length (car (x))); 2853 location = hash_fn (symname (hdl), veclength (car (x)));
2928 y = vector_elem (car (x), location); 2854 y = vector_elem (car (x), location);
2929 } 2855 }
2930 else 2856 else
2931 y = car (x); 2857 y = car (x);
2932 2858
2947 return NIL; 2873 return NIL;
2948} 2874}
2949 2875
2950#else /* USE_ALIST_ENV */ 2876#else /* USE_ALIST_ENV */
2951 2877
2952static INLINE void 2878ecb_inline void
2953new_frame_in_env (SCHEME_P_ pointer old_env) 2879new_frame_in_env (SCHEME_P_ pointer old_env)
2954{ 2880{
2955 SCHEME_V->envir = immutable_cons (NIL, old_env); 2881 SCHEME_V->envir = immutable_cons (NIL, old_env);
2956 setenvironment (SCHEME_V->envir); 2882 setenvironment (SCHEME_V->envir);
2957} 2883}
2958 2884
2959static INLINE void 2885ecb_inline void
2960new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2886new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2961{ 2887{
2962 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2888 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2963} 2889}
2964 2890
2986 return NIL; 2912 return NIL;
2987} 2913}
2988 2914
2989#endif /* USE_ALIST_ENV else */ 2915#endif /* USE_ALIST_ENV else */
2990 2916
2991static INLINE void 2917ecb_inline void
2992new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2918new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2993{ 2919{
2994 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2920 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2995} 2921}
2996 2922
2997static INLINE void 2923ecb_inline void
2998set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2924set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2999{ 2925{
3000 set_cdr (slot, value); 2926 set_cdr (slot, value);
3001} 2927}
3002 2928
3003static INLINE pointer 2929ecb_inline pointer
3004slot_value_in_env (pointer slot) 2930slot_value_in_env (pointer slot)
3005{ 2931{
3006 return cdr (slot); 2932 return cdr (slot);
3007} 2933}
3008 2934
3009/* ========== Evaluation Cycle ========== */ 2935/* ========== Evaluation Cycle ========== */
3010 2936
3011static pointer 2937static int
3012xError_1 (SCHEME_P_ const char *s, pointer a) 2938xError_1 (SCHEME_P_ const char *s, pointer a)
3013{ 2939{
3014#if USE_ERROR_HOOK 2940#if USE_ERROR_HOOK
3015 pointer x; 2941 pointer x;
3016 pointer hdl = SCHEME_V->ERROR_HOOK; 2942 pointer hdl = SCHEME_V->ERROR_HOOK;
3042#if USE_ERROR_HOOK 2968#if USE_ERROR_HOOK
3043 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); 2969 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1);
3044 2970
3045 if (x != NIL) 2971 if (x != NIL)
3046 { 2972 {
3047 if (a) 2973 pointer code = a
3048 SCHEME_V->code = cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL); 2974 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL)
3049 else 2975 : NIL;
3050 SCHEME_V->code = NIL;
3051 2976
3052 SCHEME_V->code = cons (mk_string (SCHEME_A_ s), SCHEME_V->code); 2977 code = cons (mk_string (SCHEME_A_ s), code);
3053 setimmutable (car (SCHEME_V->code)); 2978 setimmutable (car (code));
3054 SCHEME_V->code = cons (slot_value_in_env (x), SCHEME_V->code); 2979 SCHEME_V->code = cons (slot_value_in_env (x), code);
3055 SCHEME_V->op = OP_EVAL; 2980 SCHEME_V->op = OP_EVAL;
3056 2981
3057 return S_T; 2982 return 0;
3058 } 2983 }
3059#endif 2984#endif
3060 2985
3061 if (a) 2986 if (a)
3062 SCHEME_V->args = cons (a, NIL); 2987 SCHEME_V->args = cons (a, NIL);
3064 SCHEME_V->args = NIL; 2989 SCHEME_V->args = NIL;
3065 2990
3066 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); 2991 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args);
3067 setimmutable (car (SCHEME_V->args)); 2992 setimmutable (car (SCHEME_V->args));
3068 SCHEME_V->op = OP_ERR0; 2993 SCHEME_V->op = OP_ERR0;
2994
3069 return S_T; 2995 return 0;
3070} 2996}
3071 2997
3072#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) 2998#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a)
3073#define Error_0(s) Error_1 (s, 0) 2999#define Error_0(s) Error_1 (s, 0)
3074 3000
3075/* Too small to turn into function */ 3001/* Too small to turn into function */
3076#define BEGIN do { 3002#define BEGIN do {
3077#define END } while (0) 3003#define END } while (0)
3078#define s_goto(a) BEGIN \ 3004#define s_goto(a) BEGIN \
3079 SCHEME_V->op = a; \ 3005 SCHEME_V->op = a; \
3080 return S_T; END 3006 return 0; END
3081 3007
3082#define s_return(a) return xs_return (SCHEME_A_ a) 3008#define s_return(a) return xs_return (SCHEME_A_ a)
3083 3009
3084#ifndef USE_SCHEME_STACK 3010#ifndef USE_SCHEME_STACK
3085 3011
3110 next_frame = SCHEME_V->dump_base + nframes; 3036 next_frame = SCHEME_V->dump_base + nframes;
3111 3037
3112 next_frame->op = op; 3038 next_frame->op = op;
3113 next_frame->args = args; 3039 next_frame->args = args;
3114 next_frame->envir = SCHEME_V->envir; 3040 next_frame->envir = SCHEME_V->envir;
3115 next_frame->code = code; 3041 next_frame->code = code;
3116 3042
3117 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3043 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3118} 3044}
3119 3045
3120static pointer 3046static int
3121xs_return (SCHEME_P_ pointer a) 3047xs_return (SCHEME_P_ pointer a)
3122{ 3048{
3123 int nframes = (uintptr_t)SCHEME_V->dump; 3049 int nframes = (uintptr_t)SCHEME_V->dump;
3124 struct dump_stack_frame *frame; 3050 struct dump_stack_frame *frame;
3125 3051
3126 SCHEME_V->value = a; 3052 SCHEME_V->value = a;
3127 3053
3128 if (nframes <= 0) 3054 if (nframes <= 0)
3129 return NIL; 3055 return -1;
3130 3056
3131 frame = &SCHEME_V->dump_base[--nframes]; 3057 frame = &SCHEME_V->dump_base[--nframes];
3132 SCHEME_V->op = frame->op; 3058 SCHEME_V->op = frame->op;
3133 SCHEME_V->args = frame->args; 3059 SCHEME_V->args = frame->args;
3134 SCHEME_V->envir = frame->envir; 3060 SCHEME_V->envir = frame->envir;
3135 SCHEME_V->code = frame->code; 3061 SCHEME_V->code = frame->code;
3136 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3062 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3137 3063
3138 return S_T; 3064 return 0;
3139} 3065}
3140 3066
3141static INLINE void 3067ecb_inline void
3142dump_stack_reset (SCHEME_P) 3068dump_stack_reset (SCHEME_P)
3143{ 3069{
3144 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3070 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3145 SCHEME_V->dump = (pointer)+0; 3071 SCHEME_V->dump = (pointer)+0;
3146} 3072}
3147 3073
3148static INLINE void 3074ecb_inline void
3149dump_stack_initialize (SCHEME_P) 3075dump_stack_initialize (SCHEME_P)
3150{ 3076{
3151 SCHEME_V->dump_size = 0; 3077 SCHEME_V->dump_size = 0;
3152 SCHEME_V->dump_base = 0; 3078 SCHEME_V->dump_base = 0;
3153 dump_stack_reset (SCHEME_A); 3079 dump_stack_reset (SCHEME_A);
3220 SCHEME_V->dump = (pointer)(uintptr_t)i; 3146 SCHEME_V->dump = (pointer)(uintptr_t)i;
3221} 3147}
3222 3148
3223#else 3149#else
3224 3150
3225static INLINE void 3151ecb_inline void
3226dump_stack_reset (SCHEME_P) 3152dump_stack_reset (SCHEME_P)
3227{ 3153{
3228 SCHEME_V->dump = NIL; 3154 SCHEME_V->dump = NIL;
3229} 3155}
3230 3156
3231static INLINE void 3157ecb_inline void
3232dump_stack_initialize (SCHEME_P) 3158dump_stack_initialize (SCHEME_P)
3233{ 3159{
3234 dump_stack_reset (SCHEME_A); 3160 dump_stack_reset (SCHEME_A);
3235} 3161}
3236 3162
3238dump_stack_free (SCHEME_P) 3164dump_stack_free (SCHEME_P)
3239{ 3165{
3240 SCHEME_V->dump = NIL; 3166 SCHEME_V->dump = NIL;
3241} 3167}
3242 3168
3243static pointer 3169static int
3244xs_return (SCHEME_P_ pointer a) 3170xs_return (SCHEME_P_ pointer a)
3245{ 3171{
3246 pointer dump = SCHEME_V->dump; 3172 pointer dump = SCHEME_V->dump;
3247 3173
3248 SCHEME_V->value = a; 3174 SCHEME_V->value = a;
3249 3175
3250 if (dump == NIL) 3176 if (dump == NIL)
3251 return NIL; 3177 return -1;
3252 3178
3253 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3179 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump);
3254 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3180 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3255 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3181 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3256 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3182 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3257 3183
3258 SCHEME_V->dump = dump; 3184 SCHEME_V->dump = dump;
3259 3185
3260 return S_T; 3186 return 0;
3261} 3187}
3262 3188
3263static void 3189static void
3264s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3190s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3265{ 3191{
3290 3216
3291#endif 3217#endif
3292 3218
3293#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3219#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3294 3220
3295static pointer 3221static int
3296opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3222opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3297{ 3223{
3224 pointer args = SCHEME_V->args;
3298 pointer x, y; 3225 pointer x, y;
3299 3226
3300 switch (op) 3227 switch (op)
3301 { 3228 {
3302 case OP_LOAD: /* load */ 3229 case OP_LOAD: /* load */
3303 if (file_interactive (SCHEME_A)) 3230 if (file_interactive (SCHEME_A))
3304 { 3231 {
3305 xwrstr ("Loading "); xwrstr (strvalue (car (SCHEME_V->args))); xwrstr ("\n"); 3232 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n");
3306 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (SCHEME_V->args))); 3233 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args)));
3307 } 3234 }
3308 3235
3309 if (!file_push (SCHEME_A_ strvalue (car (SCHEME_V->args)))) 3236 if (!file_push (SCHEME_A_ strvalue (car (args))))
3310 Error_1 ("unable to open", car (SCHEME_V->args)); 3237 Error_1 ("unable to open", car (args));
3311 else 3238 else
3312 { 3239 {
3313 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3240 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3314 s_goto (OP_T0LVL); 3241 s_goto (OP_T0LVL);
3315 } 3242 }
3350 s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL); 3277 s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL);
3351 s_save (SCHEME_A_ OP_T1LVL, NIL, NIL); 3278 s_save (SCHEME_A_ OP_T1LVL, NIL, NIL);
3352 s_goto (OP_READ_INTERNAL); 3279 s_goto (OP_READ_INTERNAL);
3353 3280
3354 case OP_T1LVL: /* top level */ 3281 case OP_T1LVL: /* top level */
3355 SCHEME_V->code = SCHEME_V->value; 3282 SCHEME_V->code = SCHEME_V->value;
3356 SCHEME_V->inport = SCHEME_V->save_inport; 3283 SCHEME_V->inport = SCHEME_V->save_inport;
3357 s_goto (OP_EVAL); 3284 s_goto (OP_EVAL);
3358 3285
3359 case OP_READ_INTERNAL: /* internal read */ 3286 case OP_READ_INTERNAL: /* internal read */
3360 SCHEME_V->tok = token (SCHEME_A); 3287 SCHEME_V->tok = token (SCHEME_A);
3389 case OP_EVAL: /* main part of evaluation */ 3316 case OP_EVAL: /* main part of evaluation */
3390#if USE_TRACING 3317#if USE_TRACING
3391 if (SCHEME_V->tracing) 3318 if (SCHEME_V->tracing)
3392 { 3319 {
3393 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ 3320 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */
3394 s_save (SCHEME_A_ OP_REAL_EVAL, SCHEME_V->args, SCHEME_V->code); 3321 s_save (SCHEME_A_ OP_REAL_EVAL, args, SCHEME_V->code);
3395 SCHEME_V->args = SCHEME_V->code; 3322 SCHEME_V->args = SCHEME_V->code;
3396 putstr (SCHEME_A_ "\nEval: "); 3323 putstr (SCHEME_A_ "\nEval: ");
3397 s_goto (OP_P0LIST); 3324 s_goto (OP_P0LIST);
3398 } 3325 }
3399 3326
3410 else 3337 else
3411 Error_1 ("eval: unbound variable:", SCHEME_V->code); 3338 Error_1 ("eval: unbound variable:", SCHEME_V->code);
3412 } 3339 }
3413 else if (is_pair (SCHEME_V->code)) 3340 else if (is_pair (SCHEME_V->code))
3414 { 3341 {
3342 x = car (SCHEME_V->code);
3343
3415 if (is_syntax (x = car (SCHEME_V->code))) /* SYNTAX */ 3344 if (is_syntax (x)) /* SYNTAX */
3416 { 3345 {
3417 SCHEME_V->code = cdr (SCHEME_V->code); 3346 SCHEME_V->code = cdr (SCHEME_V->code);
3418 s_goto (syntaxnum (x)); 3347 s_goto (syntaxnum (x));
3419 } 3348 }
3420 else /* first, eval top element and eval arguments */ 3349 else /* first, eval top element and eval arguments */
3421 { 3350 {
3422 s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code); 3351 s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code);
3423 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ 3352 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */
3424 SCHEME_V->code = car (SCHEME_V->code); 3353 SCHEME_V->code = x;
3425 s_goto (OP_EVAL); 3354 s_goto (OP_EVAL);
3426 } 3355 }
3427 } 3356 }
3428 else 3357 else
3429 s_return (SCHEME_V->code); 3358 s_return (SCHEME_V->code);
3441 SCHEME_V->code = cdr (SCHEME_V->code); 3370 SCHEME_V->code = cdr (SCHEME_V->code);
3442 s_goto (OP_E1ARGS); 3371 s_goto (OP_E1ARGS);
3443 } 3372 }
3444 3373
3445 case OP_E1ARGS: /* eval arguments */ 3374 case OP_E1ARGS: /* eval arguments */
3446 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3375 args = cons (SCHEME_V->value, args);
3447 3376
3448 if (is_pair (SCHEME_V->code)) /* continue */ 3377 if (is_pair (SCHEME_V->code)) /* continue */
3449 { 3378 {
3450 s_save (SCHEME_A_ OP_E1ARGS, SCHEME_V->args, cdr (SCHEME_V->code)); 3379 s_save (SCHEME_A_ OP_E1ARGS, args, cdr (SCHEME_V->code));
3451 SCHEME_V->code = car (SCHEME_V->code); 3380 SCHEME_V->code = car (SCHEME_V->code);
3452 SCHEME_V->args = NIL; 3381 SCHEME_V->args = NIL;
3453 s_goto (OP_EVAL); 3382 s_goto (OP_EVAL);
3454 } 3383 }
3455 else /* end */ 3384 else /* end */
3456 { 3385 {
3457 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3386 args = reverse_in_place (SCHEME_A_ NIL, args);
3458 SCHEME_V->code = car (SCHEME_V->args); 3387 SCHEME_V->code = car (args);
3459 SCHEME_V->args = cdr (SCHEME_V->args); 3388 SCHEME_V->args = cdr (args);
3460 s_goto (OP_APPLY); 3389 s_goto (OP_APPLY);
3461 } 3390 }
3462 3391
3463#if USE_TRACING 3392#if USE_TRACING
3464 3393
3465 case OP_TRACING: 3394 case OP_TRACING:
3466 { 3395 {
3467 int tr = SCHEME_V->tracing; 3396 int tr = SCHEME_V->tracing;
3468 3397
3469 SCHEME_V->tracing = ivalue (car (SCHEME_V->args)); 3398 SCHEME_V->tracing = ivalue (car (args));
3470 s_return (mk_integer (SCHEME_A_ tr)); 3399 s_return (mk_integer (SCHEME_A_ tr));
3471 } 3400 }
3472 3401
3473#endif 3402#endif
3474 3403
3475 case OP_APPLY: /* apply 'code' to 'args' */ 3404 case OP_APPLY: /* apply 'code' to 'args' */
3476#if USE_TRACING 3405#if USE_TRACING
3477 if (SCHEME_V->tracing) 3406 if (SCHEME_V->tracing)
3478 { 3407 {
3479 s_save (SCHEME_A_ OP_REAL_APPLY, SCHEME_V->args, SCHEME_V->code); 3408 s_save (SCHEME_A_ OP_REAL_APPLY, args, SCHEME_V->code);
3480 SCHEME_V->print_flag = 1; 3409 SCHEME_V->print_flag = 1;
3481 /* SCHEME_V->args=cons(SCHEME_V->code,SCHEME_V->args); */ 3410 /* args=cons(SCHEME_V->code,args); */
3482 putstr (SCHEME_A_ "\nApply to: "); 3411 putstr (SCHEME_A_ "\nApply to: ");
3483 s_goto (OP_P0LIST); 3412 s_goto (OP_P0LIST);
3484 } 3413 }
3485 3414
3486 /* fall through */ 3415 /* fall through */
3487 3416
3488 case OP_REAL_APPLY: 3417 case OP_REAL_APPLY:
3489#endif 3418#endif
3490 if (is_proc (SCHEME_V->code)) 3419 if (is_proc (SCHEME_V->code))
3491 {
3492 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3420 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3493 }
3494 else if (is_foreign (SCHEME_V->code)) 3421 else if (is_foreign (SCHEME_V->code))
3495 { 3422 {
3496 /* Keep nested calls from GC'ing the arglist */ 3423 /* Keep nested calls from GC'ing the arglist */
3497 push_recent_alloc (SCHEME_A_ SCHEME_V->args, NIL); 3424 push_recent_alloc (SCHEME_A_ args, NIL);
3498 x = SCHEME_V->code->object.ff (SCHEME_A_ SCHEME_V->args); 3425 x = SCHEME_V->code->object.ff (SCHEME_A_ args);
3499 3426
3500 s_return (x); 3427 s_return (x);
3501 } 3428 }
3502 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ 3429 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */
3503 { 3430 {
3504 /* Should not accept promise */ 3431 /* Should not accept promise */
3505 /* make environment */ 3432 /* make environment */
3506 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); 3433 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code));
3507 3434
3508 for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) 3435 for (x = car (closure_code (SCHEME_V->code)), y = args; is_pair (x); x = cdr (x), y = cdr (y))
3509 { 3436 {
3510 if (y == NIL) 3437 if (y == NIL)
3511 Error_0 ("not enough arguments"); 3438 Error_0 ("not enough arguments");
3512 else 3439 else
3513 new_slot_in_env (SCHEME_A_ car (x), car (y)); 3440 new_slot_in_env (SCHEME_A_ car (x), car (y));
3531 s_goto (OP_BEGIN); 3458 s_goto (OP_BEGIN);
3532 } 3459 }
3533 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ 3460 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */
3534 { 3461 {
3535 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); 3462 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code));
3536 s_return (SCHEME_V->args != NIL ? car (SCHEME_V->args) : NIL); 3463 s_return (args != NIL ? car (args) : NIL);
3537 } 3464 }
3538 else 3465 else
3539 Error_0 ("illegal function"); 3466 Error_0 ("illegal function");
3540 3467
3541 case OP_DOMACRO: /* do macro */ 3468 case OP_DOMACRO: /* do macro */
3550 { 3477 {
3551 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); 3478 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1);
3552 3479
3553 if (f != NIL) 3480 if (f != NIL)
3554 { 3481 {
3555 s_save (SCHEME_A_ OP_LAMBDA1, SCHEME_V->args, SCHEME_V->code); 3482 s_save (SCHEME_A_ OP_LAMBDA1, args, SCHEME_V->code);
3556 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3483 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3557 SCHEME_V->code = slot_value_in_env (f); 3484 SCHEME_V->code = slot_value_in_env (f);
3558 s_goto (OP_APPLY); 3485 s_goto (OP_APPLY);
3559 } 3486 }
3560 3487
3571 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); 3498 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir));
3572 3499
3573#endif 3500#endif
3574 3501
3575 case OP_MKCLOSURE: /* make-closure */ 3502 case OP_MKCLOSURE: /* make-closure */
3576 x = car (SCHEME_V->args); 3503 x = car (args);
3577 3504
3578 if (car (x) == SCHEME_V->LAMBDA) 3505 if (car (x) == SCHEME_V->LAMBDA)
3579 x = cdr (x); 3506 x = cdr (x);
3580 3507
3581 if (cdr (SCHEME_V->args) == NIL) 3508 if (cdr (args) == NIL)
3582 y = SCHEME_V->envir; 3509 y = SCHEME_V->envir;
3583 else 3510 else
3584 y = cadr (SCHEME_V->args); 3511 y = cadr (args);
3585 3512
3586 s_return (mk_closure (SCHEME_A_ x, y)); 3513 s_return (mk_closure (SCHEME_A_ x, y));
3587 3514
3588 case OP_QUOTE: /* quote */ 3515 case OP_QUOTE: /* quote */
3589 s_return (car (SCHEME_V->code)); 3516 s_return (car (SCHEME_V->code));
3621 3548
3622 3549
3623 case OP_DEFP: /* defined? */ 3550 case OP_DEFP: /* defined? */
3624 x = SCHEME_V->envir; 3551 x = SCHEME_V->envir;
3625 3552
3626 if (cdr (SCHEME_V->args) != NIL) 3553 if (cdr (args) != NIL)
3627 x = cadr (SCHEME_V->args); 3554 x = cadr (args);
3628 3555
3629 s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); 3556 s_retbool (find_slot_in_env (SCHEME_A_ x, car (args), 1) != NIL);
3630 3557
3631 case OP_SET0: /* set! */ 3558 case OP_SET0: /* set! */
3632 if (is_immutable (car (SCHEME_V->code))) 3559 if (is_immutable (car (SCHEME_V->code)))
3633 Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code)); 3560 Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code));
3634 3561
3665 3592
3666 case OP_IF1: /* if */ 3593 case OP_IF1: /* if */
3667 if (is_true (SCHEME_V->value)) 3594 if (is_true (SCHEME_V->value))
3668 SCHEME_V->code = car (SCHEME_V->code); 3595 SCHEME_V->code = car (SCHEME_V->code);
3669 else 3596 else
3670 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because 3597 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
3671
3672 * car(NIL) = NIL */
3673 s_goto (OP_EVAL); 3598 s_goto (OP_EVAL);
3674 3599
3675 case OP_LET0: /* let */ 3600 case OP_LET0: /* let */
3676 SCHEME_V->args = NIL; 3601 SCHEME_V->args = NIL;
3677 SCHEME_V->value = SCHEME_V->code; 3602 SCHEME_V->value = SCHEME_V->code;
3678 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); 3603 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code);
3679 s_goto (OP_LET1); 3604 s_goto (OP_LET1);
3680 3605
3681 case OP_LET1: /* let (calculate parameters) */ 3606 case OP_LET1: /* let (calculate parameters) */
3682 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3607 args = cons (SCHEME_V->value, args);
3683 3608
3684 if (is_pair (SCHEME_V->code)) /* continue */ 3609 if (is_pair (SCHEME_V->code)) /* continue */
3685 { 3610 {
3686 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3611 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3687 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); 3612 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code));
3688 3613
3689 s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); 3614 s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code));
3690 SCHEME_V->code = cadar (SCHEME_V->code); 3615 SCHEME_V->code = cadar (SCHEME_V->code);
3691 SCHEME_V->args = NIL; 3616 SCHEME_V->args = NIL;
3692 s_goto (OP_EVAL); 3617 s_goto (OP_EVAL);
3693 } 3618 }
3694 else /* end */ 3619 else /* end */
3695 { 3620 {
3696 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3621 args = reverse_in_place (SCHEME_A_ NIL, args);
3697 SCHEME_V->code = car (SCHEME_V->args); 3622 SCHEME_V->code = car (args);
3698 SCHEME_V->args = cdr (SCHEME_V->args); 3623 SCHEME_V->args = cdr (args);
3699 s_goto (OP_LET2); 3624 s_goto (OP_LET2);
3700 } 3625 }
3701 3626
3702 case OP_LET2: /* let */ 3627 case OP_LET2: /* let */
3703 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 3628 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3704 3629
3705 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; 3630 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args;
3706 y != NIL; x = cdr (x), y = cdr (y)) 3631 y != NIL; x = cdr (x), y = cdr (y))
3707 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3632 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3708 3633
3709 if (is_symbol (car (SCHEME_V->code))) /* named let */ 3634 if (is_symbol (car (SCHEME_V->code))) /* named let */
3710 { 3635 {
3711 for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) 3636 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x))
3712 { 3637 {
3713 if (!is_pair (x)) 3638 if (!is_pair (x))
3714 Error_1 ("Bad syntax of binding in let :", x); 3639 Error_1 ("Bad syntax of binding in let :", x);
3715 3640
3716 if (!is_list (SCHEME_A_ car (x))) 3641 if (!is_list (SCHEME_A_ car (x)))
3717 Error_1 ("Bad syntax of binding in let :", car (x)); 3642 Error_1 ("Bad syntax of binding in let :", car (x));
3718 3643
3719 SCHEME_V->args = cons (caar (x), SCHEME_V->args); 3644 args = cons (caar (x), args);
3720 } 3645 }
3721 3646
3722 x =
3723 mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args), cddr (SCHEME_V->code)), 3647 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)),
3724 SCHEME_V->envir); 3648 SCHEME_V->envir);
3725 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); 3649 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x);
3726 SCHEME_V->code = cddr (SCHEME_V->code); 3650 SCHEME_V->code = cddr (SCHEME_V->code);
3727 SCHEME_V->args = NIL;
3728 } 3651 }
3729 else 3652 else
3730 { 3653 {
3731 SCHEME_V->code = cdr (SCHEME_V->code); 3654 SCHEME_V->code = cdr (SCHEME_V->code);
3655 }
3656
3732 SCHEME_V->args = NIL; 3657 SCHEME_V->args = NIL;
3733 }
3734
3735 s_goto (OP_BEGIN); 3658 s_goto (OP_BEGIN);
3736 3659
3737 case OP_LET0AST: /* let* */ 3660 case OP_LET0AST: /* let* */
3738 if (car (SCHEME_V->code) == NIL) 3661 if (car (SCHEME_V->code) == NIL)
3739 { 3662 {
3757 new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value); 3680 new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value);
3758 SCHEME_V->code = cdr (SCHEME_V->code); 3681 SCHEME_V->code = cdr (SCHEME_V->code);
3759 3682
3760 if (is_pair (SCHEME_V->code)) /* continue */ 3683 if (is_pair (SCHEME_V->code)) /* continue */
3761 { 3684 {
3762 s_save (SCHEME_A_ OP_LET2AST, SCHEME_V->args, SCHEME_V->code); 3685 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code);
3763 SCHEME_V->code = cadar (SCHEME_V->code); 3686 SCHEME_V->code = cadar (SCHEME_V->code);
3764 SCHEME_V->args = NIL; 3687 SCHEME_V->args = NIL;
3765 s_goto (OP_EVAL); 3688 s_goto (OP_EVAL);
3766 } 3689 }
3767 else /* end */ 3690 else /* end */
3768 { 3691 {
3769 SCHEME_V->code = SCHEME_V->args; 3692 SCHEME_V->code = args;
3770 SCHEME_V->args = NIL; 3693 SCHEME_V->args = NIL;
3771 s_goto (OP_BEGIN); 3694 s_goto (OP_BEGIN);
3772 } 3695 }
3773 3696
3774 case OP_LET0REC: /* letrec */ 3697 case OP_LET0REC: /* letrec */
3777 SCHEME_V->value = SCHEME_V->code; 3700 SCHEME_V->value = SCHEME_V->code;
3778 SCHEME_V->code = car (SCHEME_V->code); 3701 SCHEME_V->code = car (SCHEME_V->code);
3779 s_goto (OP_LET1REC); 3702 s_goto (OP_LET1REC);
3780 3703
3781 case OP_LET1REC: /* letrec (calculate parameters) */ 3704 case OP_LET1REC: /* letrec (calculate parameters) */
3782 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3705 args = cons (SCHEME_V->value, args);
3783 3706
3784 if (is_pair (SCHEME_V->code)) /* continue */ 3707 if (is_pair (SCHEME_V->code)) /* continue */
3785 { 3708 {
3786 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3709 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3787 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); 3710 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3788 3711
3789 s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code)); 3712 s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code));
3790 SCHEME_V->code = cadar (SCHEME_V->code); 3713 SCHEME_V->code = cadar (SCHEME_V->code);
3791 SCHEME_V->args = NIL; 3714 SCHEME_V->args = NIL;
3792 s_goto (OP_EVAL); 3715 s_goto (OP_EVAL);
3793 } 3716 }
3794 else /* end */ 3717 else /* end */
3795 { 3718 {
3796 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3719 args = reverse_in_place (SCHEME_A_ NIL, args);
3797 SCHEME_V->code = car (SCHEME_V->args); 3720 SCHEME_V->code = car (args);
3798 SCHEME_V->args = cdr (SCHEME_V->args); 3721 SCHEME_V->args = cdr (args);
3799 s_goto (OP_LET2REC); 3722 s_goto (OP_LET2REC);
3800 } 3723 }
3801 3724
3802 case OP_LET2REC: /* letrec */ 3725 case OP_LET2REC: /* letrec */
3803 for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y)) 3726 for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y))
3804 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3727 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3805 3728
3806 SCHEME_V->code = cdr (SCHEME_V->code); 3729 SCHEME_V->code = cdr (SCHEME_V->code);
3807 SCHEME_V->args = NIL; 3730 SCHEME_V->args = NIL;
3808 s_goto (OP_BEGIN); 3731 s_goto (OP_BEGIN);
3894 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 3817 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3895 SCHEME_V->code = car (SCHEME_V->code); 3818 SCHEME_V->code = car (SCHEME_V->code);
3896 s_goto (OP_EVAL); 3819 s_goto (OP_EVAL);
3897 3820
3898 case OP_C1STREAM: /* cons-stream */ 3821 case OP_C1STREAM: /* cons-stream */
3899 SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register SCHEME_V->args for gc */ 3822 SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register args for gc */
3900 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); 3823 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir);
3901 set_typeflag (x, T_PROMISE); 3824 set_typeflag (x, T_PROMISE);
3902 s_return (cons (SCHEME_V->args, x)); 3825 s_return (cons (args, x));
3903 3826
3904 case OP_MACRO0: /* macro */ 3827 case OP_MACRO0: /* macro */
3905 if (is_pair (car (SCHEME_V->code))) 3828 if (is_pair (car (SCHEME_V->code)))
3906 { 3829 {
3907 x = caar (SCHEME_V->code); 3830 x = caar (SCHEME_V->code);
3940 { 3863 {
3941 if (!is_pair (y = caar (x))) 3864 if (!is_pair (y = caar (x)))
3942 break; 3865 break;
3943 3866
3944 for (; y != NIL; y = cdr (y)) 3867 for (; y != NIL; y = cdr (y))
3945 {
3946 if (eqv (car (y), SCHEME_V->value)) 3868 if (eqv (car (y), SCHEME_V->value))
3947 break; 3869 break;
3948 }
3949 3870
3950 if (y != NIL) 3871 if (y != NIL)
3951 break; 3872 break;
3952 } 3873 }
3953 3874
3973 s_goto (OP_BEGIN); 3894 s_goto (OP_BEGIN);
3974 else 3895 else
3975 s_return (NIL); 3896 s_return (NIL);
3976 3897
3977 case OP_PAPPLY: /* apply */ 3898 case OP_PAPPLY: /* apply */
3978 SCHEME_V->code = car (SCHEME_V->args); 3899 SCHEME_V->code = car (args);
3979 SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); 3900 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
3980 /*SCHEME_V->args = cadr(SCHEME_V->args); */ 3901 /*SCHEME_V->args = cadr(args); */
3981 s_goto (OP_APPLY); 3902 s_goto (OP_APPLY);
3982 3903
3983 case OP_PEVAL: /* eval */ 3904 case OP_PEVAL: /* eval */
3984 if (cdr (SCHEME_V->args) != NIL) 3905 if (cdr (args) != NIL)
3985 SCHEME_V->envir = cadr (SCHEME_V->args); 3906 SCHEME_V->envir = cadr (args);
3986 3907
3987 SCHEME_V->code = car (SCHEME_V->args); 3908 SCHEME_V->code = car (args);
3988 s_goto (OP_EVAL); 3909 s_goto (OP_EVAL);
3989 3910
3990 case OP_CONTINUATION: /* call-with-current-continuation */ 3911 case OP_CONTINUATION: /* call-with-current-continuation */
3991 SCHEME_V->code = car (SCHEME_V->args); 3912 SCHEME_V->code = car (args);
3992 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_V)), NIL); 3913 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
3993 s_goto (OP_APPLY); 3914 s_goto (OP_APPLY);
3994 } 3915 }
3995 3916
3996 return S_T; 3917 if (USE_ERROR_CHECKING) abort ();
3997} 3918}
3998 3919
3999static pointer 3920static int
4000opexe_2 (SCHEME_P_ enum scheme_opcodes op) 3921opexe_1 (SCHEME_P_ enum scheme_opcodes op)
4001{ 3922{
4002 pointer x; 3923 pointer args = SCHEME_V->args;
3924 pointer x = car (args);
4003 num v; 3925 num v;
4004 3926
4005#if USE_MATH 3927#if USE_MATH
4006 RVALUE dd; 3928 RVALUE dd;
4007#endif 3929#endif
4008 3930
4009 switch (op) 3931 switch (op)
4010 { 3932 {
4011#if USE_MATH 3933#if USE_MATH
4012
4013 case OP_INEX2EX: /* inexact->exact */ 3934 case OP_INEX2EX: /* inexact->exact */
4014 x = car (SCHEME_V->args);
4015
4016 if (num_is_integer (x)) 3935 if (num_is_integer (x))
4017 s_return (x); 3936 s_return (x);
4018 else if (modf (rvalue_unchecked (x), &dd) == 0.0) 3937 else if (modf (rvalue_unchecked (x), &dd) == 0)
4019 s_return (mk_integer (SCHEME_A_ ivalue (x))); 3938 s_return (mk_integer (SCHEME_A_ ivalue (x)));
4020 else 3939 else
4021 Error_1 ("inexact->exact: not integral:", x); 3940 Error_1 ("inexact->exact: not integral:", x);
4022 3941
4023 case OP_EXP:
4024 x = car (SCHEME_V->args);
4025 s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 3942 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
4026
4027 case OP_LOG:
4028 x = car (SCHEME_V->args);
4029 s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 3943 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))));
4030
4031 case OP_SIN:
4032 x = car (SCHEME_V->args);
4033 s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 3944 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
4034
4035 case OP_COS:
4036 x = car (SCHEME_V->args);
4037 s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 3945 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
4038
4039 case OP_TAN:
4040 x = car (SCHEME_V->args);
4041 s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); 3946 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x))));
4042
4043 case OP_ASIN:
4044 x = car (SCHEME_V->args);
4045 s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); 3947 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x))));
4046
4047 case OP_ACOS:
4048 x = car (SCHEME_V->args);
4049 s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); 3948 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x))));
4050 3949
4051 case OP_ATAN: 3950 case OP_ATAN:
4052 x = car (SCHEME_V->args);
4053
4054 if (cdr (SCHEME_V->args) == NIL) 3951 if (cdr (args) == NIL)
4055 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 3952 s_return (mk_real (SCHEME_A_ atan (rvalue (x))));
4056 else 3953 else
4057 { 3954 {
4058 pointer y = cadr (SCHEME_V->args); 3955 pointer y = cadr (args);
4059
4060 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); 3956 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
4061 } 3957 }
4062 3958
4063 case OP_SQRT: 3959 case OP_SQRT:
4064 x = car (SCHEME_V->args);
4065 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); 3960 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4066 3961
4067 case OP_EXPT: 3962 case OP_EXPT:
4068 { 3963 {
4069 RVALUE result; 3964 RVALUE result;
4070 int real_result = 1; 3965 int real_result = 1;
4071 pointer y = cadr (SCHEME_V->args); 3966 pointer y = cadr (args);
4072
4073 x = car (SCHEME_V->args);
4074 3967
4075 if (num_is_integer (x) && num_is_integer (y)) 3968 if (num_is_integer (x) && num_is_integer (y))
4076 real_result = 0; 3969 real_result = 0;
4077 3970
4078 /* This 'if' is an R5RS compatibility fix. */ 3971 /* This 'if' is an R5RS compatibility fix. */
4079 /* NOTE: Remove this 'if' fix for R6RS. */ 3972 /* NOTE: Remove this 'if' fix for R6RS. */
4080 if (rvalue (x) == 0 && rvalue (y) < 0) 3973 if (rvalue (x) == 0 && rvalue (y) < 0)
4081 result = 0.0; 3974 result = 0;
4082 else 3975 else
4083 result = pow (rvalue (x), rvalue (y)); 3976 result = pow (rvalue (x), rvalue (y));
4084 3977
4085 /* Before returning integer result make sure we can. */ 3978 /* Before returning integer result make sure we can. */
4086 /* If the test fails, result is too big for integer. */ 3979 /* If the test fails, result is too big for integer. */
4087 if (!real_result) 3980 if (!real_result)
4088 { 3981 {
4089 long result_as_long = (long) result; 3982 long result_as_long = result;
4090 3983
4091 if (result != (RVALUE) result_as_long) 3984 if (result != (RVALUE) result_as_long)
4092 real_result = 1; 3985 real_result = 1;
4093 } 3986 }
4094 3987
4096 s_return (mk_real (SCHEME_A_ result)); 3989 s_return (mk_real (SCHEME_A_ result));
4097 else 3990 else
4098 s_return (mk_integer (SCHEME_A_ result)); 3991 s_return (mk_integer (SCHEME_A_ result));
4099 } 3992 }
4100 3993
4101 case OP_FLOOR:
4102 x = car (SCHEME_V->args);
4103 s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 3994 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4104
4105 case OP_CEILING:
4106 x = car (SCHEME_V->args);
4107 s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); 3995 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4108 3996
4109 case OP_TRUNCATE: 3997 case OP_TRUNCATE:
4110 { 3998 {
4111 RVALUE rvalue_of_x; 3999 RVALUE rvalue_of_x;
4112 4000
4113 x = car (SCHEME_V->args);
4114 rvalue_of_x = rvalue (x); 4001 rvalue_of_x = rvalue (x);
4115 4002
4116 if (rvalue_of_x > 0) 4003 if (rvalue_of_x > 0)
4117 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); 4004 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x)));
4118 else 4005 else
4119 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); 4006 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
4120 } 4007 }
4121 4008
4122 case OP_ROUND: 4009 case OP_ROUND:
4123 x = car (SCHEME_V->args);
4124
4125 if (num_is_integer (x)) 4010 if (num_is_integer (x))
4126 s_return (x); 4011 s_return (x);
4127 4012
4128 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4013 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4129#endif 4014#endif
4130 4015
4131 case OP_ADD: /* + */ 4016 case OP_ADD: /* + */
4132 v = num_zero; 4017 v = num_zero;
4133 4018
4134 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4019 for (x = args; x != NIL; x = cdr (x))
4135 v = num_add (v, nvalue (car (x))); 4020 v = num_op (NUM_ADD, v, nvalue (car (x)));
4136 4021
4137 s_return (mk_number (SCHEME_A_ v)); 4022 s_return (mk_number (SCHEME_A_ v));
4138 4023
4139 case OP_MUL: /* * */ 4024 case OP_MUL: /* * */
4140 v = num_one; 4025 v = num_one;
4141 4026
4142 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4027 for (x = args; x != NIL; x = cdr (x))
4143 v = num_mul (v, nvalue (car (x))); 4028 v = num_op (NUM_MUL, v, nvalue (car (x)));
4144 4029
4145 s_return (mk_number (SCHEME_A_ v)); 4030 s_return (mk_number (SCHEME_A_ v));
4146 4031
4147 case OP_SUB: /* - */ 4032 case OP_SUB: /* - */
4148 if (cdr (SCHEME_V->args) == NIL) 4033 if (cdr (args) == NIL)
4149 { 4034 {
4150 x = SCHEME_V->args; 4035 x = args;
4151 v = num_zero; 4036 v = num_zero;
4152 } 4037 }
4153 else 4038 else
4154 { 4039 {
4155 x = cdr (SCHEME_V->args); 4040 x = cdr (args);
4156 v = nvalue (car (SCHEME_V->args)); 4041 v = nvalue (car (args));
4157 } 4042 }
4158 4043
4159 for (; x != NIL; x = cdr (x)) 4044 for (; x != NIL; x = cdr (x))
4160 v = num_sub (v, nvalue (car (x))); 4045 v = num_op (NUM_SUB, v, nvalue (car (x)));
4161 4046
4162 s_return (mk_number (SCHEME_A_ v)); 4047 s_return (mk_number (SCHEME_A_ v));
4163 4048
4164 case OP_DIV: /* / */ 4049 case OP_DIV: /* / */
4165 if (cdr (SCHEME_V->args) == NIL) 4050 if (cdr (args) == NIL)
4166 { 4051 {
4167 x = SCHEME_V->args; 4052 x = args;
4168 v = num_one; 4053 v = num_one;
4169 } 4054 }
4170 else 4055 else
4171 { 4056 {
4172 x = cdr (SCHEME_V->args); 4057 x = cdr (args);
4173 v = nvalue (car (SCHEME_V->args)); 4058 v = nvalue (car (args));
4174 } 4059 }
4175 4060
4176 for (; x != NIL; x = cdr (x)) 4061 for (; x != NIL; x = cdr (x))
4177 {
4178 if (!is_zero_rvalue (rvalue (car (x)))) 4062 if (!is_zero_rvalue (rvalue (car (x))))
4179 v = num_div (v, nvalue (car (x))); 4063 v = num_div (v, nvalue (car (x)));
4180 else 4064 else
4181 Error_0 ("/: division by zero"); 4065 Error_0 ("/: division by zero");
4182 }
4183 4066
4184 s_return (mk_number (SCHEME_A_ v)); 4067 s_return (mk_number (SCHEME_A_ v));
4185 4068
4186 case OP_INTDIV: /* quotient */ 4069 case OP_INTDIV: /* quotient */
4187 if (cdr (SCHEME_V->args) == NIL) 4070 if (cdr (args) == NIL)
4188 { 4071 {
4189 x = SCHEME_V->args; 4072 x = args;
4190 v = num_one; 4073 v = num_one;
4191 } 4074 }
4192 else 4075 else
4193 { 4076 {
4194 x = cdr (SCHEME_V->args); 4077 x = cdr (args);
4195 v = nvalue (car (SCHEME_V->args)); 4078 v = nvalue (car (args));
4196 } 4079 }
4197 4080
4198 for (; x != NIL; x = cdr (x)) 4081 for (; x != NIL; x = cdr (x))
4199 { 4082 {
4200 if (ivalue (car (x)) != 0) 4083 if (ivalue (car (x)) != 0)
4201 v = num_intdiv (v, nvalue (car (x))); 4084 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4202 else 4085 else
4203 Error_0 ("quotient: division by zero"); 4086 Error_0 ("quotient: division by zero");
4204 } 4087 }
4205 4088
4206 s_return (mk_number (SCHEME_A_ v)); 4089 s_return (mk_number (SCHEME_A_ v));
4207 4090
4208 case OP_REM: /* remainder */ 4091 case OP_REM: /* remainder */
4209 v = nvalue (car (SCHEME_V->args)); 4092 v = nvalue (x);
4210 4093
4211 if (ivalue (cadr (SCHEME_V->args)) != 0) 4094 if (ivalue (cadr (args)) != 0)
4212 v = num_rem (v, nvalue (cadr (SCHEME_V->args))); 4095 v = num_rem (v, nvalue (cadr (args)));
4213 else 4096 else
4214 Error_0 ("remainder: division by zero"); 4097 Error_0 ("remainder: division by zero");
4215 4098
4216 s_return (mk_number (SCHEME_A_ v)); 4099 s_return (mk_number (SCHEME_A_ v));
4217 4100
4218 case OP_MOD: /* modulo */ 4101 case OP_MOD: /* modulo */
4219 v = nvalue (car (SCHEME_V->args)); 4102 v = nvalue (x);
4220 4103
4221 if (ivalue (cadr (SCHEME_V->args)) != 0) 4104 if (ivalue (cadr (args)) != 0)
4222 v = num_mod (v, nvalue (cadr (SCHEME_V->args))); 4105 v = num_mod (v, nvalue (cadr (args)));
4223 else 4106 else
4224 Error_0 ("modulo: division by zero"); 4107 Error_0 ("modulo: division by zero");
4225 4108
4226 s_return (mk_number (SCHEME_A_ v)); 4109 s_return (mk_number (SCHEME_A_ v));
4227 4110
4228 case OP_CAR: /* car */ 4111 case OP_CAR: /* car */
4229 s_return (caar (SCHEME_V->args)); 4112 s_return (caar (args));
4230 4113
4231 case OP_CDR: /* cdr */ 4114 case OP_CDR: /* cdr */
4232 s_return (cdar (SCHEME_V->args)); 4115 s_return (cdar (args));
4233 4116
4234 case OP_CONS: /* cons */ 4117 case OP_CONS: /* cons */
4235 set_cdr (SCHEME_V->args, cadr (SCHEME_V->args)); 4118 set_cdr (args, cadr (args));
4236 s_return (SCHEME_V->args); 4119 s_return (args);
4237 4120
4238 case OP_SETCAR: /* set-car! */ 4121 case OP_SETCAR: /* set-car! */
4239 if (!is_immutable (car (SCHEME_V->args))) 4122 if (!is_immutable (x))
4240 { 4123 {
4241 set_car (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4124 set_car (x, cadr (args));
4242 s_return (car (SCHEME_V->args)); 4125 s_return (car (args));
4243 } 4126 }
4244 else 4127 else
4245 Error_0 ("set-car!: unable to alter immutable pair"); 4128 Error_0 ("set-car!: unable to alter immutable pair");
4246 4129
4247 case OP_SETCDR: /* set-cdr! */ 4130 case OP_SETCDR: /* set-cdr! */
4248 if (!is_immutable (car (SCHEME_V->args))) 4131 if (!is_immutable (x))
4249 { 4132 {
4250 set_cdr (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4133 set_cdr (x, cadr (args));
4251 s_return (car (SCHEME_V->args)); 4134 s_return (car (args));
4252 } 4135 }
4253 else 4136 else
4254 Error_0 ("set-cdr!: unable to alter immutable pair"); 4137 Error_0 ("set-cdr!: unable to alter immutable pair");
4255 4138
4256 case OP_CHAR2INT: /* char->integer */ 4139 case OP_CHAR2INT: /* char->integer */
4257 s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4140 s_return (mk_integer (SCHEME_A_ ivalue (x)));
4258 4141
4259 case OP_INT2CHAR: /* integer->char */ 4142 case OP_INT2CHAR: /* integer->char */
4260 s_return (mk_character (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4143 s_return (mk_character (SCHEME_A_ ivalue (x)));
4261 4144
4262 case OP_CHARUPCASE: 4145 case OP_CHARUPCASE:
4263 { 4146 {
4264 unsigned char c = ivalue (car (SCHEME_V->args)); 4147 unsigned char c = ivalue (x);
4265 c = toupper (c); 4148 c = toupper (c);
4266 s_return (mk_character (SCHEME_A_ c)); 4149 s_return (mk_character (SCHEME_A_ c));
4267 } 4150 }
4268 4151
4269 case OP_CHARDNCASE: 4152 case OP_CHARDNCASE:
4270 { 4153 {
4271 unsigned char c = ivalue (car (SCHEME_V->args)); 4154 unsigned char c = ivalue (x);
4272 c = tolower (c); 4155 c = tolower (c);
4273 s_return (mk_character (SCHEME_A_ c)); 4156 s_return (mk_character (SCHEME_A_ c));
4274 } 4157 }
4275 4158
4276 case OP_STR2SYM: /* string->symbol */ 4159 case OP_STR2SYM: /* string->symbol */
4277 s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); 4160 s_return (mk_symbol (SCHEME_A_ strvalue (x)));
4278 4161
4279 case OP_STR2ATOM: /* string->atom */ 4162 case OP_STR2ATOM: /* string->atom */
4280 { 4163 {
4281 char *s = strvalue (car (SCHEME_V->args)); 4164 char *s = strvalue (x);
4282 long pf = 0; 4165 long pf = 0;
4283 4166
4284 if (cdr (SCHEME_V->args) != NIL) 4167 if (cdr (args) != NIL)
4285 { 4168 {
4286 /* we know cadr(SCHEME_V->args) is a natural number */ 4169 /* we know cadr(args) is a natural number */
4287 /* see if it is 2, 8, 10, or 16, or error */ 4170 /* see if it is 2, 8, 10, or 16, or error */
4288 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4171 pf = ivalue_unchecked (cadr (args));
4289 4172
4290 if (pf == 16 || pf == 10 || pf == 8 || pf == 2) 4173 if (pf == 16 || pf == 10 || pf == 8 || pf == 2)
4291 { 4174 {
4292 /* base is OK */ 4175 /* base is OK */
4293 } 4176 }
4294 else 4177 else
4295 pf = -1; 4178 pf = -1;
4296 } 4179 }
4297 4180
4298 if (pf < 0) 4181 if (pf < 0)
4299 Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); 4182 Error_1 ("string->atom: bad base:", cadr (args));
4300 else if (*s == '#') /* no use of base! */ 4183 else if (*s == '#') /* no use of base! */
4301 s_return (mk_sharp_const (SCHEME_A_ s + 1)); 4184 s_return (mk_sharp_const (SCHEME_A_ s + 1));
4302 else 4185 else
4303 { 4186 {
4304 if (pf == 0 || pf == 10) 4187 if (pf == 0 || pf == 10)
4315 } 4198 }
4316 } 4199 }
4317 } 4200 }
4318 4201
4319 case OP_SYM2STR: /* symbol->string */ 4202 case OP_SYM2STR: /* symbol->string */
4320 x = mk_string (SCHEME_A_ symname (car (SCHEME_V->args))); 4203 x = mk_string (SCHEME_A_ symname (x));
4321 setimmutable (x); 4204 setimmutable (x);
4322 s_return (x); 4205 s_return (x);
4323 4206
4324 case OP_ATOM2STR: /* atom->string */ 4207 case OP_ATOM2STR: /* atom->string */
4325 { 4208 {
4326 long pf = 0; 4209 long pf = 0;
4327 4210
4328 x = car (SCHEME_V->args);
4329
4330 if (cdr (SCHEME_V->args) != NIL) 4211 if (cdr (args) != NIL)
4331 { 4212 {
4332 /* we know cadr(SCHEME_V->args) is a natural number */ 4213 /* we know cadr(args) is a natural number */
4333 /* see if it is 2, 8, 10, or 16, or error */ 4214 /* see if it is 2, 8, 10, or 16, or error */
4334 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4215 pf = ivalue_unchecked (cadr (args));
4335 4216
4336 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) 4217 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2))
4337 { 4218 {
4338 /* base is OK */ 4219 /* base is OK */
4339 } 4220 }
4340 else 4221 else
4341 pf = -1; 4222 pf = -1;
4342 } 4223 }
4343 4224
4344 if (pf < 0) 4225 if (pf < 0)
4345 Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); 4226 Error_1 ("atom->string: bad base:", cadr (args));
4346 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) 4227 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x))
4347 { 4228 {
4348 char *p; 4229 char *p;
4349 int len; 4230 int len;
4350 4231
4358 case OP_MKSTRING: /* make-string */ 4239 case OP_MKSTRING: /* make-string */
4359 { 4240 {
4360 int fill = ' '; 4241 int fill = ' ';
4361 int len; 4242 int len;
4362 4243
4363 len = ivalue (car (SCHEME_V->args)); 4244 len = ivalue (x);
4364 4245
4365 if (cdr (SCHEME_V->args) != NIL) 4246 if (cdr (args) != NIL)
4366 fill = charvalue (cadr (SCHEME_V->args)); 4247 fill = charvalue (cadr (args));
4367 4248
4368 s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); 4249 s_return (mk_empty_string (SCHEME_A_ len, fill));
4369 } 4250 }
4370 4251
4371 case OP_STRLEN: /* string-length */ 4252 case OP_STRLEN: /* string-length */
4372 s_return (mk_integer (SCHEME_A_ strlength (car (SCHEME_V->args)))); 4253 s_return (mk_integer (SCHEME_A_ strlength (x)));
4373 4254
4374 case OP_STRREF: /* string-ref */ 4255 case OP_STRREF: /* string-ref */
4375 { 4256 {
4376 char *str; 4257 char *str;
4377 int index; 4258 int index;
4378 4259
4379 str = strvalue (car (SCHEME_V->args)); 4260 str = strvalue (x);
4380 4261
4381 index = ivalue (cadr (SCHEME_V->args)); 4262 index = ivalue (cadr (args));
4382 4263
4383 if (index >= strlength (car (SCHEME_V->args))) 4264 if (index >= strlength (x))
4384 Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); 4265 Error_1 ("string-ref: out of bounds:", cadr (args));
4385 4266
4386 s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); 4267 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4387 } 4268 }
4388 4269
4389 case OP_STRSET: /* string-set! */ 4270 case OP_STRSET: /* string-set! */
4390 { 4271 {
4391 char *str; 4272 char *str;
4392 int index; 4273 int index;
4393 int c; 4274 int c;
4394 4275
4395 if (is_immutable (car (SCHEME_V->args))) 4276 if (is_immutable (x))
4396 Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); 4277 Error_1 ("string-set!: unable to alter immutable string:", x);
4397 4278
4398 str = strvalue (car (SCHEME_V->args)); 4279 str = strvalue (x);
4399 4280
4400 index = ivalue (cadr (SCHEME_V->args)); 4281 index = ivalue (cadr (args));
4401 4282
4402 if (index >= strlength (car (SCHEME_V->args))) 4283 if (index >= strlength (x))
4403 Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); 4284 Error_1 ("string-set!: out of bounds:", cadr (args));
4404 4285
4405 c = charvalue (caddr (SCHEME_V->args)); 4286 c = charvalue (caddr (args));
4406 4287
4407 str[index] = (char) c; 4288 str[index] = c;
4408 s_return (car (SCHEME_V->args)); 4289 s_return (car (args));
4409 } 4290 }
4410 4291
4411 case OP_STRAPPEND: /* string-append */ 4292 case OP_STRAPPEND: /* string-append */
4412 { 4293 {
4413 /* in 1.29 string-append was in Scheme in init.scm but was too slow */ 4294 /* in 1.29 string-append was in Scheme in init.scm but was too slow */
4414 int len = 0; 4295 int len = 0;
4415 pointer newstr; 4296 pointer newstr;
4416 char *pos; 4297 char *pos;
4417 4298
4418 /* compute needed length for new string */ 4299 /* compute needed length for new string */
4419 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4300 for (x = args; x != NIL; x = cdr (x))
4420 len += strlength (car (x)); 4301 len += strlength (car (x));
4421 4302
4422 newstr = mk_empty_string (SCHEME_A_ len, ' '); 4303 newstr = mk_empty_string (SCHEME_A_ len, ' ');
4423 4304
4424 /* store the contents of the argument strings into the new string */ 4305 /* store the contents of the argument strings into the new string */
4425 for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x)) 4306 for (pos = strvalue (newstr), x = args; x != NIL; pos += strlength (car (x)), x = cdr (x))
4426 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4307 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4427 4308
4428 s_return (newstr); 4309 s_return (newstr);
4429 } 4310 }
4430 4311
4433 char *str; 4314 char *str;
4434 int index0; 4315 int index0;
4435 int index1; 4316 int index1;
4436 int len; 4317 int len;
4437 4318
4438 str = strvalue (car (SCHEME_V->args)); 4319 str = strvalue (x);
4439 4320
4440 index0 = ivalue (cadr (SCHEME_V->args)); 4321 index0 = ivalue (cadr (args));
4441 4322
4442 if (index0 > strlength (car (SCHEME_V->args))) 4323 if (index0 > strlength (x))
4443 Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); 4324 Error_1 ("substring: start out of bounds:", cadr (args));
4444 4325
4445 if (cddr (SCHEME_V->args) != NIL) 4326 if (cddr (args) != NIL)
4446 { 4327 {
4447 index1 = ivalue (caddr (SCHEME_V->args)); 4328 index1 = ivalue (caddr (args));
4448 4329
4449 if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) 4330 if (index1 > strlength (x) || index1 < index0)
4450 Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); 4331 Error_1 ("substring: end out of bounds:", caddr (args));
4451 } 4332 }
4452 else 4333 else
4453 index1 = strlength (car (SCHEME_V->args)); 4334 index1 = strlength (x);
4454 4335
4455 len = index1 - index0; 4336 len = index1 - index0;
4456 x = mk_empty_string (SCHEME_A_ len, ' '); 4337 x = mk_empty_string (SCHEME_A_ len, ' ');
4457 memcpy (strvalue (x), str + index0, len); 4338 memcpy (strvalue (x), str + index0, len);
4458 strvalue (x)[len] = 0; 4339 strvalue (x)[len] = 0;
4462 4343
4463 case OP_VECTOR: /* vector */ 4344 case OP_VECTOR: /* vector */
4464 { 4345 {
4465 int i; 4346 int i;
4466 pointer vec; 4347 pointer vec;
4467 int len = list_length (SCHEME_A_ SCHEME_V->args); 4348 int len = list_length (SCHEME_A_ args);
4468 4349
4469 if (len < 0) 4350 if (len < 0)
4470 Error_1 ("vector: not a proper list:", SCHEME_V->args); 4351 Error_1 ("vector: not a proper list:", args);
4471 4352
4472 vec = mk_vector (SCHEME_A_ len); 4353 vec = mk_vector (SCHEME_A_ len);
4473 4354
4474#if USE_ERROR_CHECKING 4355#if USE_ERROR_CHECKING
4475 if (SCHEME_V->no_memory) 4356 if (SCHEME_V->no_memory)
4476 s_return (S_SINK); 4357 s_return (S_SINK);
4477#endif 4358#endif
4478 4359
4479 for (x = SCHEME_V->args, i = 0; is_pair (x); x = cdr (x), i++) 4360 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4480 set_vector_elem (vec, i, car (x)); 4361 set_vector_elem (vec, i, car (x));
4481 4362
4482 s_return (vec); 4363 s_return (vec);
4483 } 4364 }
4484 4365
4486 { 4367 {
4487 pointer fill = NIL; 4368 pointer fill = NIL;
4488 int len; 4369 int len;
4489 pointer vec; 4370 pointer vec;
4490 4371
4491 len = ivalue (car (SCHEME_V->args)); 4372 len = ivalue (x);
4492 4373
4493 if (cdr (SCHEME_V->args) != NIL) 4374 if (cdr (args) != NIL)
4494 fill = cadr (SCHEME_V->args); 4375 fill = cadr (args);
4495 4376
4496 vec = mk_vector (SCHEME_A_ len); 4377 vec = mk_vector (SCHEME_A_ len);
4497 4378
4498#if USE_ERROR_CHECKING 4379#if USE_ERROR_CHECKING
4499 if (SCHEME_V->no_memory) 4380 if (SCHEME_V->no_memory)
4505 4386
4506 s_return (vec); 4387 s_return (vec);
4507 } 4388 }
4508 4389
4509 case OP_VECLEN: /* vector-length */ 4390 case OP_VECLEN: /* vector-length */
4510 s_return (mk_integer (SCHEME_A_ vector_length (car (SCHEME_V->args)))); 4391 s_return (mk_integer (SCHEME_A_ veclength (x)));
4511 4392
4512 case OP_VECREF: /* vector-ref */ 4393 case OP_VECREF: /* vector-ref */
4513 { 4394 {
4514 int index; 4395 int index;
4515 4396
4516 index = ivalue (cadr (SCHEME_V->args)); 4397 index = ivalue (cadr (args));
4517 4398
4518 if (index >= vector_length (car (SCHEME_V->args)) && USE_ERROR_CHECKING) 4399 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4519 Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); 4400 Error_1 ("vector-ref: out of bounds:", cadr (args));
4520 4401
4521 s_return (vector_elem (car (SCHEME_V->args), index)); 4402 s_return (vector_elem (x, index));
4522 } 4403 }
4523 4404
4524 case OP_VECSET: /* vector-set! */ 4405 case OP_VECSET: /* vector-set! */
4525 { 4406 {
4526 int index; 4407 int index;
4527 4408
4528 if (is_immutable (car (SCHEME_V->args))) 4409 if (is_immutable (x))
4529 Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); 4410 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4530 4411
4531 index = ivalue (cadr (SCHEME_V->args)); 4412 index = ivalue (cadr (args));
4532 4413
4533 if (index >= vector_length (car (SCHEME_V->args)) && USE_ERROR_CHECKING) 4414 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4534 Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); 4415 Error_1 ("vector-set!: out of bounds:", cadr (args));
4535 4416
4536 set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); 4417 set_vector_elem (x, index, caddr (args));
4537 s_return (car (SCHEME_V->args)); 4418 s_return (x);
4538 } 4419 }
4539 } 4420 }
4540 4421
4541 return S_T; 4422 if (USE_ERROR_CHECKING) abort ();
4542} 4423}
4543 4424
4544INTERFACE int 4425INTERFACE int
4545is_list (SCHEME_P_ pointer a) 4426is_list (SCHEME_P_ pointer a)
4546{ 4427{
4593 return -1; 4474 return -1;
4594 } 4475 }
4595 } 4476 }
4596} 4477}
4597 4478
4598static pointer 4479static int
4480opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4481{
4482 pointer x = SCHEME_V->args;
4483
4484 for (;;)
4485 {
4486 num v = nvalue (car (x));
4487 x = cdr (x);
4488
4489 if (x == NIL)
4490 break;
4491
4492 int r = num_cmp (v, nvalue (car (x)));
4493
4494 switch (op)
4495 {
4496 case OP_NUMEQ: r = r == 0; break;
4497 case OP_LESS: r = r < 0; break;
4498 case OP_GRE: r = r > 0; break;
4499 case OP_LEQ: r = r <= 0; break;
4500 case OP_GEQ: r = r >= 0; break;
4501 }
4502
4503 if (!r)
4504 s_return (S_F);
4505 }
4506
4507 s_return (S_T);
4508}
4509
4510static int
4599opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4511opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4600{ 4512{
4601 pointer x; 4513 pointer args = SCHEME_V->args;
4602 num v; 4514 pointer a = car (args);
4603 int (*comp_func) (num, num); 4515 pointer d = cdr (args);
4516 int r;
4604 4517
4605 switch (op) 4518 switch (op)
4606 { 4519 {
4607 case OP_NOT: /* not */ 4520 case OP_NOT: /* not */ r = is_false (a) ; break;
4608 s_retbool (is_false (car (SCHEME_V->args))); 4521 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break;
4522 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break;
4523 case OP_NULLP: /* null? */ r = a == NIL ; break;
4524 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break;
4525 case OP_NUMBERP: /* number? */ r = is_number (a) ; break;
4526 case OP_STRINGP: /* string? */ r = is_string (a) ; break;
4527 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4528 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4529 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4609 4530
4610 case OP_BOOLP: /* boolean? */
4611 s_retbool (car (SCHEME_V->args) == S_F || car (SCHEME_V->args) == S_T);
4612
4613 case OP_EOFOBJP: /* boolean? */
4614 s_retbool (car (SCHEME_V->args) == S_EOF);
4615
4616 case OP_NULLP: /* null? */
4617 s_retbool (car (SCHEME_V->args) == NIL);
4618
4619 case OP_NUMEQ: /* = */
4620 case OP_LESS: /* < */
4621 case OP_GRE: /* > */
4622 case OP_LEQ: /* <= */
4623 case OP_GEQ: /* >= */
4624 switch (op)
4625 {
4626 case OP_NUMEQ:
4627 comp_func = num_eq;
4628 break;
4629
4630 case OP_LESS:
4631 comp_func = num_lt;
4632 break;
4633
4634 case OP_GRE:
4635 comp_func = num_gt;
4636 break;
4637
4638 case OP_LEQ:
4639 comp_func = num_le;
4640 break;
4641
4642 case OP_GEQ:
4643 comp_func = num_ge;
4644 break;
4645 }
4646
4647 x = SCHEME_V->args;
4648 v = nvalue (car (x));
4649 x = cdr (x);
4650
4651 for (; x != NIL; x = cdr (x))
4652 {
4653 if (!comp_func (v, nvalue (car (x))))
4654 s_retbool (0);
4655
4656 v = nvalue (car (x));
4657 }
4658
4659 s_retbool (1);
4660
4661 case OP_SYMBOLP: /* symbol? */
4662 s_retbool (is_symbol (car (SCHEME_V->args)));
4663
4664 case OP_NUMBERP: /* number? */
4665 s_retbool (is_number (car (SCHEME_V->args)));
4666
4667 case OP_STRINGP: /* string? */
4668 s_retbool (is_string (car (SCHEME_V->args)));
4669
4670 case OP_INTEGERP: /* integer? */
4671 s_retbool (is_integer (car (SCHEME_V->args)));
4672
4673 case OP_REALP: /* real? */
4674 s_retbool (is_number (car (SCHEME_V->args))); /* All numbers are real */
4675
4676 case OP_CHARP: /* char? */
4677 s_retbool (is_character (car (SCHEME_V->args)));
4678#if USE_CHAR_CLASSIFIERS 4531#if USE_CHAR_CLASSIFIERS
4679
4680 case OP_CHARAP: /* char-alphabetic? */ 4532 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break;
4681 s_retbool (Cisalpha (ivalue (car (SCHEME_V->args)))); 4533 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break;
4682
4683 case OP_CHARNP: /* char-numeric? */
4684 s_retbool (Cisdigit (ivalue (car (SCHEME_V->args))));
4685
4686 case OP_CHARWP: /* char-whitespace? */ 4534 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break;
4687 s_retbool (Cisspace (ivalue (car (SCHEME_V->args))));
4688
4689 case OP_CHARUP: /* char-upper-case? */ 4535 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break;
4690 s_retbool (Cisupper (ivalue (car (SCHEME_V->args))));
4691
4692 case OP_CHARLP: /* char-lower-case? */ 4536 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break;
4693 s_retbool (Cislower (ivalue (car (SCHEME_V->args))));
4694#endif 4537#endif
4538
4695#if USE_PORTS 4539#if USE_PORTS
4696 4540 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4697 case OP_PORTP: /* port? */
4698 s_retbool (is_port (car (SCHEME_V->args)));
4699
4700 case OP_INPORTP: /* input-port? */ 4541 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4701 s_retbool (is_inport (car (SCHEME_V->args)));
4702
4703 case OP_OUTPORTP: /* output-port? */ 4542 case OP_OUTPORTP: /* output-port? */ r = is_outport (a); break;
4704 s_retbool (is_outport (car (SCHEME_V->args)));
4705#endif 4543#endif
4706 4544
4707 case OP_PROCP: /* procedure? */ 4545 case OP_PROCP: /* procedure? */
4708 4546
4709 /*-- 4547 /*--
4710 * continuation should be procedure by the example 4548 * continuation should be procedure by the example
4711 * (call-with-current-continuation procedure?) ==> #t 4549 * (call-with-current-continuation procedure?) ==> #t
4712 * in R^3 report sec. 6.9 4550 * in R^3 report sec. 6.9
4713 */ 4551 */
4714 s_retbool (is_proc (car (SCHEME_V->args)) || is_closure (car (SCHEME_V->args)) 4552 r = is_proc (a) || is_closure (a) || is_continuation (a) || is_foreign (a);
4715 || is_continuation (car (SCHEME_V->args)) || is_foreign (car (SCHEME_V->args))); 4553 break;
4716 4554
4717 case OP_PAIRP: /* pair? */ 4555 case OP_PAIRP: /* pair? */ r = is_pair (a) ; break;
4718 s_retbool (is_pair (car (SCHEME_V->args))); 4556 case OP_LISTP: /* list? */ r = list_length (SCHEME_A_ a) >= 0; break;
4719 4557 case OP_ENVP: /* environment? */ r = is_environment (a) ; break;
4720 case OP_LISTP: /* list? */ 4558 case OP_VECTORP: /* vector? */ r = is_vector (a) ; break;
4721 s_retbool (list_length (SCHEME_A_ car (SCHEME_V->args)) >= 0); 4559 case OP_EQ: /* eq? */ r = a == cadr (args) ; break;
4722 4560 case OP_EQV: /* eqv? */ r = eqv (a, cadr (args)) ; break;
4723 case OP_ENVP: /* environment? */
4724 s_retbool (is_environment (car (SCHEME_V->args)));
4725
4726 case OP_VECTORP: /* vector? */
4727 s_retbool (is_vector (car (SCHEME_V->args)));
4728
4729 case OP_EQ: /* eq? */
4730 s_retbool (car (SCHEME_V->args) == cadr (SCHEME_V->args));
4731
4732 case OP_EQV: /* eqv? */
4733 s_retbool (eqv (car (SCHEME_V->args), cadr (SCHEME_V->args)));
4734 } 4561 }
4735 4562
4736 return S_T; 4563 s_retbool (r);
4737} 4564}
4738 4565
4739static pointer 4566static int
4740opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4567opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4741{ 4568{
4569 pointer args = SCHEME_V->args;
4570 pointer a = car (args);
4742 pointer x, y; 4571 pointer x, y;
4743 4572
4744 switch (op) 4573 switch (op)
4745 { 4574 {
4746 case OP_FORCE: /* force */ 4575 case OP_FORCE: /* force */
4747 SCHEME_V->code = car (SCHEME_V->args); 4576 SCHEME_V->code = a;
4748 4577
4749 if (is_promise (SCHEME_V->code)) 4578 if (is_promise (SCHEME_V->code))
4750 { 4579 {
4751 /* Should change type to closure here */ 4580 /* Should change type to closure here */
4752 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); 4581 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code);
4773 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4602 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4774 SCHEME_V->outport = cadr (SCHEME_V->args); 4603 SCHEME_V->outport = cadr (SCHEME_V->args);
4775 } 4604 }
4776 } 4605 }
4777 4606
4778 SCHEME_V->args = car (SCHEME_V->args); 4607 SCHEME_V->args = a;
4779 4608
4780 if (op == OP_WRITE) 4609 if (op == OP_WRITE)
4781 SCHEME_V->print_flag = 1; 4610 SCHEME_V->print_flag = 1;
4782 else 4611 else
4783 SCHEME_V->print_flag = 0; 4612 SCHEME_V->print_flag = 0;
4784 4613
4785 s_goto (OP_P0LIST); 4614 s_goto (OP_P0LIST);
4786 4615
4787 case OP_NEWLINE: /* newline */ 4616 case OP_NEWLINE: /* newline */
4788 if (is_pair (SCHEME_V->args)) 4617 if (is_pair (args))
4789 { 4618 {
4790 if (car (SCHEME_V->args) != SCHEME_V->outport) 4619 if (a != SCHEME_V->outport)
4791 { 4620 {
4792 x = cons (SCHEME_V->outport, NIL); 4621 x = cons (SCHEME_V->outport, NIL);
4793 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4622 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4794 SCHEME_V->outport = car (SCHEME_V->args); 4623 SCHEME_V->outport = a;
4795 } 4624 }
4796 } 4625 }
4797 4626
4798 putstr (SCHEME_A_ "\n"); 4627 putstr (SCHEME_A_ "\n");
4799 s_return (S_T); 4628 s_return (S_T);
4800#endif 4629#endif
4801 4630
4802 case OP_ERR0: /* error */ 4631 case OP_ERR0: /* error */
4803 SCHEME_V->retcode = -1; 4632 SCHEME_V->retcode = -1;
4804 4633
4805 if (!is_string (car (SCHEME_V->args))) 4634 if (!is_string (a))
4806 { 4635 {
4807 SCHEME_V->args = cons (mk_string (SCHEME_A_ " -- "), SCHEME_V->args); 4636 args = cons (mk_string (SCHEME_A_ " -- "), args);
4808 setimmutable (car (SCHEME_V->args)); 4637 setimmutable (car (args));
4809 } 4638 }
4810 4639
4811 putstr (SCHEME_A_ "Error: "); 4640 putstr (SCHEME_A_ "Error: ");
4812 putstr (SCHEME_A_ strvalue (car (SCHEME_V->args))); 4641 putstr (SCHEME_A_ strvalue (car (args)));
4813 SCHEME_V->args = cdr (SCHEME_V->args); 4642 SCHEME_V->args = cdr (args);
4814 s_goto (OP_ERR1); 4643 s_goto (OP_ERR1);
4815 4644
4816 case OP_ERR1: /* error */ 4645 case OP_ERR1: /* error */
4817 putstr (SCHEME_A_ " "); 4646 putstr (SCHEME_A_ " ");
4818 4647
4819 if (SCHEME_V->args != NIL) 4648 if (args != NIL)
4820 { 4649 {
4821 s_save (SCHEME_A_ OP_ERR1, cdr (SCHEME_V->args), NIL); 4650 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4822 SCHEME_V->args = car (SCHEME_V->args); 4651 SCHEME_V->args = a;
4823 SCHEME_V->print_flag = 1; 4652 SCHEME_V->print_flag = 1;
4824 s_goto (OP_P0LIST); 4653 s_goto (OP_P0LIST);
4825 } 4654 }
4826 else 4655 else
4827 { 4656 {
4828 putstr (SCHEME_A_ "\n"); 4657 putstr (SCHEME_A_ "\n");
4829 4658
4830 if (SCHEME_V->interactive_repl) 4659 if (SCHEME_V->interactive_repl)
4831 s_goto (OP_T0LVL); 4660 s_goto (OP_T0LVL);
4832 else 4661 else
4833 return NIL; 4662 return -1;
4834 } 4663 }
4835 4664
4836 case OP_REVERSE: /* reverse */ 4665 case OP_REVERSE: /* reverse */
4837 s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); 4666 s_return (reverse (SCHEME_A_ a));
4838 4667
4839 case OP_LIST_STAR: /* list* */ 4668 case OP_LIST_STAR: /* list* */
4840 s_return (list_star (SCHEME_A_ SCHEME_V->args)); 4669 s_return (list_star (SCHEME_A_ SCHEME_V->args));
4841 4670
4842 case OP_APPEND: /* append */ 4671 case OP_APPEND: /* append */
4843 x = NIL; 4672 x = NIL;
4844 y = SCHEME_V->args; 4673 y = args;
4845 4674
4846 if (y == x) 4675 if (y == x)
4847 s_return (x); 4676 s_return (x);
4848 4677
4849 /* cdr() in the while condition is not a typo. If car() */ 4678 /* cdr() in the while condition is not a typo. If car() */
4860 s_return (reverse_in_place (SCHEME_A_ car (y), x)); 4689 s_return (reverse_in_place (SCHEME_A_ car (y), x));
4861 4690
4862#if USE_PLIST 4691#if USE_PLIST
4863 4692
4864 case OP_PUT: /* put */ 4693 case OP_PUT: /* put */
4865 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 4694 if (!hasprop (a) || !hasprop (cadr (args)))
4866 Error_0 ("illegal use of put"); 4695 Error_0 ("illegal use of put");
4867 4696
4868 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 4697 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
4869 { 4698 {
4870 if (caar (x) == y) 4699 if (caar (x) == y)
4871 break; 4700 break;
4872 } 4701 }
4873 4702
4874 if (x != NIL) 4703 if (x != NIL)
4875 cdar (x) = caddr (SCHEME_V->args); 4704 cdar (x) = caddr (args);
4876 else 4705 else
4877 symprop (car (SCHEME_V->args)) = cons (cons (y, caddr (SCHEME_V->args)), symprop (car (SCHEME_V->args))); 4706 symprop (a) = cons (cons (y, caddr (args)), symprop (a));
4878 4707
4879 s_return (S_T); 4708 s_return (S_T);
4880 4709
4881 case OP_GET: /* get */ 4710 case OP_GET: /* get */
4882 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 4711 if (!hasprop (a) || !hasprop (cadr (args)))
4883 Error_0 ("illegal use of get"); 4712 Error_0 ("illegal use of get");
4884 4713
4885 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 4714 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
4886 if (caar (x) == y) 4715 if (caar (x) == y)
4887 break; 4716 break;
4888 4717
4889 if (x != NIL) 4718 if (x != NIL)
4890 s_return (cdar (x)); 4719 s_return (cdar (x));
4892 s_return (NIL); 4721 s_return (NIL);
4893 4722
4894#endif /* USE_PLIST */ 4723#endif /* USE_PLIST */
4895 4724
4896 case OP_QUIT: /* quit */ 4725 case OP_QUIT: /* quit */
4897 if (is_pair (SCHEME_V->args)) 4726 if (is_pair (args))
4898 SCHEME_V->retcode = ivalue (car (SCHEME_V->args)); 4727 SCHEME_V->retcode = ivalue (a);
4899 4728
4900 return NIL; 4729 return -1;
4901 4730
4902 case OP_GC: /* gc */ 4731 case OP_GC: /* gc */
4903 gc (SCHEME_A_ NIL, NIL); 4732 gc (SCHEME_A_ NIL, NIL);
4904 s_return (S_T); 4733 s_return (S_T);
4905 4734
4906 case OP_GCVERB: /* gc-verbose */ 4735 case OP_GCVERB: /* gc-verbose */
4907 { 4736 {
4908 int was = SCHEME_V->gc_verbose; 4737 int was = SCHEME_V->gc_verbose;
4909 4738
4910 SCHEME_V->gc_verbose = (car (SCHEME_V->args) != S_F); 4739 SCHEME_V->gc_verbose = (a != S_F);
4911 s_retbool (was); 4740 s_retbool (was);
4912 } 4741 }
4913 4742
4914 case OP_NEWSEGMENT: /* new-segment */ 4743 case OP_NEWSEGMENT: /* new-segment */
4915 if (!is_pair (SCHEME_V->args) || !is_number (car (SCHEME_V->args))) 4744 if (!is_pair (args) || !is_number (a))
4916 Error_0 ("new-segment: argument must be a number"); 4745 Error_0 ("new-segment: argument must be a number");
4917 4746
4918 alloc_cellseg (SCHEME_A_ (int)ivalue (car (SCHEME_V->args))); 4747 alloc_cellseg (SCHEME_A_ (int)ivalue (a));
4919 4748
4920 s_return (S_T); 4749 s_return (S_T);
4921 4750
4922 case OP_OBLIST: /* oblist */ 4751 case OP_OBLIST: /* oblist */
4923 s_return (oblist_all_symbols (SCHEME_A)); 4752 s_return (oblist_all_symbols (SCHEME_A));
4950 case OP_OPEN_INOUTFILE: 4779 case OP_OPEN_INOUTFILE:
4951 prop = port_input | port_output; 4780 prop = port_input | port_output;
4952 break; 4781 break;
4953 } 4782 }
4954 4783
4955 p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); 4784 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4956 4785
4957 if (p == NIL) 4786 s_return (p == NIL ? S_F : p);
4958 s_return (S_F);
4959
4960 s_return (p);
4961 } 4787 }
4962 4788
4963# if USE_STRING_PORTS 4789# if USE_STRING_PORTS
4964 4790
4965 case OP_OPEN_INSTRING: /* open-input-string */ 4791 case OP_OPEN_INSTRING: /* open-input-string */
4977 case OP_OPEN_INOUTSTRING: 4803 case OP_OPEN_INOUTSTRING:
4978 prop = port_input | port_output; 4804 prop = port_input | port_output;
4979 break; 4805 break;
4980 } 4806 }
4981 4807
4982 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4808 p = port_from_string (SCHEME_A_ strvalue (a),
4983 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); 4809 strvalue (a) + strlength (a), prop);
4984 4810
4985 if (p == NIL) 4811 s_return (p == NIL ? S_F : p);
4986 s_return (S_F);
4987
4988 s_return (p);
4989 } 4812 }
4990 4813
4991 case OP_OPEN_OUTSTRING: /* open-output-string */ 4814 case OP_OPEN_OUTSTRING: /* open-output-string */
4992 { 4815 {
4993 pointer p; 4816 pointer p;
4994 4817
4995 if (car (SCHEME_V->args) == NIL) 4818 if (a == NIL)
4996 {
4997 p = port_from_scratch (SCHEME_A); 4819 p = port_from_scratch (SCHEME_A);
4998
4999 if (p == NIL)
5000 s_return (S_F);
5001 }
5002 else 4820 else
5003 {
5004 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4821 p = port_from_string (SCHEME_A_ strvalue (a),
5005 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); 4822 strvalue (a) + strlength (a), port_output);
5006 4823
5007 if (p == NIL) 4824 s_return (p == NIL ? S_F : p);
5008 s_return (S_F);
5009 }
5010
5011 s_return (p);
5012 } 4825 }
5013 4826
5014 case OP_GET_OUTSTRING: /* get-output-string */ 4827 case OP_GET_OUTSTRING: /* get-output-string */
5015 { 4828 {
5016 port *p; 4829 port *p;
5017 4830
5018 if ((p = car (SCHEME_V->args)->object.port)->kind & port_string) 4831 if ((p = a->object.port)->kind & port_string)
5019 { 4832 {
5020 off_t size; 4833 off_t size;
5021 char *str; 4834 char *str;
5022 4835
5023 size = p->rep.string.curr - p->rep.string.start + 1; 4836 size = p->rep.string.curr - p->rep.string.start + 1;
5039 } 4852 }
5040 4853
5041# endif 4854# endif
5042 4855
5043 case OP_CLOSE_INPORT: /* close-input-port */ 4856 case OP_CLOSE_INPORT: /* close-input-port */
5044 port_close (SCHEME_A_ car (SCHEME_V->args), port_input); 4857 port_close (SCHEME_A_ a, port_input);
5045 s_return (S_T); 4858 s_return (S_T);
5046 4859
5047 case OP_CLOSE_OUTPORT: /* close-output-port */ 4860 case OP_CLOSE_OUTPORT: /* close-output-port */
5048 port_close (SCHEME_A_ car (SCHEME_V->args), port_output); 4861 port_close (SCHEME_A_ a, port_output);
5049 s_return (S_T); 4862 s_return (S_T);
5050#endif 4863#endif
5051 4864
5052 case OP_INT_ENV: /* interaction-environment */ 4865 case OP_INT_ENV: /* interaction-environment */
5053 s_return (SCHEME_V->global_env); 4866 s_return (SCHEME_V->global_env);
5055 case OP_CURR_ENV: /* current-environment */ 4868 case OP_CURR_ENV: /* current-environment */
5056 s_return (SCHEME_V->envir); 4869 s_return (SCHEME_V->envir);
5057 4870
5058 } 4871 }
5059 4872
5060 return S_T; 4873 if (USE_ERROR_CHECKING) abort ();
5061} 4874}
5062 4875
5063static pointer 4876static int
5064opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4877opexe_5 (SCHEME_P_ enum scheme_opcodes op)
5065{ 4878{
4879 pointer args = SCHEME_V->args;
5066 pointer x; 4880 pointer x;
5067 4881
5068 if (SCHEME_V->nesting != 0) 4882 if (SCHEME_V->nesting != 0)
5069 { 4883 {
5070 int n = SCHEME_V->nesting; 4884 int n = SCHEME_V->nesting;
5077 switch (op) 4891 switch (op)
5078 { 4892 {
5079 /* ========== reading part ========== */ 4893 /* ========== reading part ========== */
5080#if USE_PORTS 4894#if USE_PORTS
5081 case OP_READ: 4895 case OP_READ:
5082 if (!is_pair (SCHEME_V->args)) 4896 if (!is_pair (args))
5083 s_goto (OP_READ_INTERNAL); 4897 s_goto (OP_READ_INTERNAL);
5084 4898
5085 if (!is_inport (car (SCHEME_V->args))) 4899 if (!is_inport (car (args)))
5086 Error_1 ("read: not an input port:", car (SCHEME_V->args)); 4900 Error_1 ("read: not an input port:", car (args));
5087 4901
5088 if (car (SCHEME_V->args) == SCHEME_V->inport) 4902 if (car (args) == SCHEME_V->inport)
5089 s_goto (OP_READ_INTERNAL); 4903 s_goto (OP_READ_INTERNAL);
5090 4904
5091 x = SCHEME_V->inport; 4905 x = SCHEME_V->inport;
5092 SCHEME_V->inport = car (SCHEME_V->args); 4906 SCHEME_V->inport = car (args);
5093 x = cons (x, NIL); 4907 x = cons (x, NIL);
5094 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4908 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5095 s_goto (OP_READ_INTERNAL); 4909 s_goto (OP_READ_INTERNAL);
5096 4910
5097 case OP_READ_CHAR: /* read-char */ 4911 case OP_READ_CHAR: /* read-char */
5098 case OP_PEEK_CHAR: /* peek-char */ 4912 case OP_PEEK_CHAR: /* peek-char */
5099 { 4913 {
5100 int c; 4914 int c;
5101 4915
5102 if (is_pair (SCHEME_V->args)) 4916 if (is_pair (args))
5103 { 4917 {
5104 if (car (SCHEME_V->args) != SCHEME_V->inport) 4918 if (car (args) != SCHEME_V->inport)
5105 { 4919 {
5106 x = SCHEME_V->inport; 4920 x = SCHEME_V->inport;
5107 x = cons (x, NIL); 4921 x = cons (x, NIL);
5108 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4922 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5109 SCHEME_V->inport = car (SCHEME_V->args); 4923 SCHEME_V->inport = car (args);
5110 } 4924 }
5111 } 4925 }
5112 4926
5113 c = inchar (SCHEME_A); 4927 c = inchar (SCHEME_A);
5114 4928
5124 case OP_CHAR_READY: /* char-ready? */ 4938 case OP_CHAR_READY: /* char-ready? */
5125 { 4939 {
5126 pointer p = SCHEME_V->inport; 4940 pointer p = SCHEME_V->inport;
5127 int res; 4941 int res;
5128 4942
5129 if (is_pair (SCHEME_V->args)) 4943 if (is_pair (args))
5130 p = car (SCHEME_V->args); 4944 p = car (args);
5131 4945
5132 res = p->object.port->kind & port_string; 4946 res = p->object.port->kind & port_string;
5133 4947
5134 s_retbool (res); 4948 s_retbool (res);
5135 } 4949 }
5136 4950
5137 case OP_SET_INPORT: /* set-input-port */ 4951 case OP_SET_INPORT: /* set-input-port */
5138 SCHEME_V->inport = car (SCHEME_V->args); 4952 SCHEME_V->inport = car (args);
5139 s_return (SCHEME_V->value); 4953 s_return (SCHEME_V->value);
5140 4954
5141 case OP_SET_OUTPORT: /* set-output-port */ 4955 case OP_SET_OUTPORT: /* set-output-port */
5142 SCHEME_V->outport = car (SCHEME_V->args); 4956 SCHEME_V->outport = car (args);
5143 s_return (SCHEME_V->value); 4957 s_return (SCHEME_V->value);
5144#endif 4958#endif
5145 4959
5146 case OP_RDSEXPR: 4960 case OP_RDSEXPR:
5147 switch (SCHEME_V->tok) 4961 switch (SCHEME_V->tok)
5233 } 5047 }
5234 5048
5235 break; 5049 break;
5236 5050
5237 case OP_RDLIST: 5051 case OP_RDLIST:
5238 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 5052 SCHEME_V->args = cons (SCHEME_V->value, args);
5239 SCHEME_V->tok = token (SCHEME_A); 5053 SCHEME_V->tok = token (SCHEME_A);
5240 5054
5241 switch (SCHEME_V->tok) 5055 switch (SCHEME_V->tok)
5242 { 5056 {
5243 case TOK_EOF: 5057 case TOK_EOF:
5271 case OP_RDDOT: 5085 case OP_RDDOT:
5272 if (token (SCHEME_A) != TOK_RPAREN) 5086 if (token (SCHEME_A) != TOK_RPAREN)
5273 Error_0 ("syntax error: illegal dot expression"); 5087 Error_0 ("syntax error: illegal dot expression");
5274 5088
5275 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5089 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5276 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); 5090 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, args));
5277 5091
5278 case OP_RDQUOTE: 5092 case OP_RDQUOTE:
5279 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); 5093 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)));
5280 5094
5281 case OP_RDQQUOTE: 5095 case OP_RDQQUOTE:
5303 SCHEME_V->args = SCHEME_V->value; 5117 SCHEME_V->args = SCHEME_V->value;
5304 s_goto (OP_VECTOR); 5118 s_goto (OP_VECTOR);
5305 5119
5306 /* ========== printing part ========== */ 5120 /* ========== printing part ========== */
5307 case OP_P0LIST: 5121 case OP_P0LIST:
5308 if (is_vector (SCHEME_V->args)) 5122 if (is_vector (args))
5309 { 5123 {
5310 putstr (SCHEME_A_ "#("); 5124 putstr (SCHEME_A_ "#(");
5311 SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); 5125 SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0));
5312 s_goto (OP_PVECFROM); 5126 s_goto (OP_PVECFROM);
5313 } 5127 }
5314 else if (is_environment (SCHEME_V->args)) 5128 else if (is_environment (args))
5315 { 5129 {
5316 putstr (SCHEME_A_ "#<ENVIRONMENT>"); 5130 putstr (SCHEME_A_ "#<ENVIRONMENT>");
5317 s_return (S_T); 5131 s_return (S_T);
5318 } 5132 }
5319 else if (!is_pair (SCHEME_V->args)) 5133 else if (!is_pair (args))
5320 { 5134 {
5321 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5135 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5322 s_return (S_T); 5136 s_return (S_T);
5323 } 5137 }
5324 else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5138 else
5325 { 5139 {
5140 pointer a = car (args);
5141 pointer b = cdr (args);
5142 int ok_abbr = ok_abbrev (b);
5143 SCHEME_V->args = car (b);
5144
5145 if (a == SCHEME_V->QUOTE && ok_abbr)
5326 putstr (SCHEME_A_ "'"); 5146 putstr (SCHEME_A_ "'");
5327 SCHEME_V->args = cadr (SCHEME_V->args); 5147 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5148 putstr (SCHEME_A_ "`");
5149 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5150 putstr (SCHEME_A_ ",");
5151 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5152 putstr (SCHEME_A_ ",@");
5153 else
5154 {
5155 putstr (SCHEME_A_ "(");
5156 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5157 SCHEME_V->args = a;
5158 }
5159
5328 s_goto (OP_P0LIST); 5160 s_goto (OP_P0LIST);
5329 } 5161 }
5330 else if (car (SCHEME_V->args) == SCHEME_V->QQUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5162
5163 case OP_P1LIST:
5164 if (is_pair (args))
5331 { 5165 {
5166 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5332 putstr (SCHEME_A_ "`"); 5167 putstr (SCHEME_A_ " ");
5333 SCHEME_V->args = cadr (SCHEME_V->args); 5168 SCHEME_V->args = car (args);
5334 s_goto (OP_P0LIST); 5169 s_goto (OP_P0LIST);
5335 } 5170 }
5336 else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTE && ok_abbrev (cdr (SCHEME_V->args)))
5337 {
5338 putstr (SCHEME_A_ ",");
5339 SCHEME_V->args = cadr (SCHEME_V->args);
5340 s_goto (OP_P0LIST);
5341 }
5342 else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTESP && ok_abbrev (cdr (SCHEME_V->args)))
5343 {
5344 putstr (SCHEME_A_ ",@");
5345 SCHEME_V->args = cadr (SCHEME_V->args);
5346 s_goto (OP_P0LIST);
5347 }
5348 else
5349 {
5350 putstr (SCHEME_A_ "(");
5351 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL);
5352 SCHEME_V->args = car (SCHEME_V->args);
5353 s_goto (OP_P0LIST);
5354 }
5355
5356 case OP_P1LIST:
5357 if (is_pair (SCHEME_V->args))
5358 {
5359 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL);
5360 putstr (SCHEME_A_ " ");
5361 SCHEME_V->args = car (SCHEME_V->args);
5362 s_goto (OP_P0LIST);
5363 }
5364 else if (is_vector (SCHEME_V->args)) 5171 else if (is_vector (args))
5365 { 5172 {
5366 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); 5173 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL);
5367 putstr (SCHEME_A_ " . "); 5174 putstr (SCHEME_A_ " . ");
5368 s_goto (OP_P0LIST); 5175 s_goto (OP_P0LIST);
5369 } 5176 }
5370 else 5177 else
5371 { 5178 {
5372 if (SCHEME_V->args != NIL) 5179 if (args != NIL)
5373 { 5180 {
5374 putstr (SCHEME_A_ " . "); 5181 putstr (SCHEME_A_ " . ");
5375 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5182 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5376 } 5183 }
5377 5184
5378 putstr (SCHEME_A_ ")"); 5185 putstr (SCHEME_A_ ")");
5379 s_return (S_T); 5186 s_return (S_T);
5380 } 5187 }
5381 5188
5382 case OP_PVECFROM: 5189 case OP_PVECFROM:
5383 { 5190 {
5384 int i = ivalue_unchecked (cdr (SCHEME_V->args)); 5191 int i = ivalue_unchecked (cdr (args));
5385 pointer vec = car (SCHEME_V->args); 5192 pointer vec = car (args);
5386 int len = vector_length (vec); 5193 int len = veclength (vec);
5387 5194
5388 if (i == len) 5195 if (i == len)
5389 { 5196 {
5390 putstr (SCHEME_A_ ")"); 5197 putstr (SCHEME_A_ ")");
5391 s_return (S_T); 5198 s_return (S_T);
5392 } 5199 }
5393 else 5200 else
5394 { 5201 {
5395 pointer elem = vector_elem (vec, i); 5202 pointer elem = vector_elem (vec, i);
5396 5203
5397 ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; 5204 ivalue_unchecked (cdr (args)) = i + 1;
5398 s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); 5205 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5399 SCHEME_V->args = elem; 5206 SCHEME_V->args = elem;
5400 5207
5401 if (i > 0) 5208 if (i > 0)
5402 putstr (SCHEME_A_ " "); 5209 putstr (SCHEME_A_ " ");
5403 5210
5404 s_goto (OP_P0LIST); 5211 s_goto (OP_P0LIST);
5405 } 5212 }
5406 } 5213 }
5407 } 5214 }
5408 5215
5409 return S_T; 5216 if (USE_ERROR_CHECKING) abort ();
5410} 5217}
5411 5218
5412static pointer 5219static int
5413opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5220opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5414{ 5221{
5222 pointer args = SCHEME_V->args;
5223 pointer a = car (args);
5415 pointer x, y; 5224 pointer x, y;
5416 5225
5417 switch (op) 5226 switch (op)
5418 { 5227 {
5419 case OP_LIST_LENGTH: /* length *//* a.k */ 5228 case OP_LIST_LENGTH: /* length *//* a.k */
5420 { 5229 {
5421 long v = list_length (SCHEME_A_ car (SCHEME_V->args)); 5230 long v = list_length (SCHEME_A_ a);
5422 5231
5423 if (v < 0) 5232 if (v < 0)
5424 Error_1 ("length: not a list:", car (SCHEME_V->args)); 5233 Error_1 ("length: not a list:", a);
5425 5234
5426 s_return (mk_integer (SCHEME_A_ v)); 5235 s_return (mk_integer (SCHEME_A_ v));
5427 } 5236 }
5428 5237
5429 case OP_ASSQ: /* assq *//* a.k */ 5238 case OP_ASSQ: /* assq *//* a.k */
5430 x = car (SCHEME_V->args); 5239 x = a;
5431 5240
5432 for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) 5241 for (y = cadr (args); is_pair (y); y = cdr (y))
5433 { 5242 {
5434 if (!is_pair (car (y))) 5243 if (!is_pair (car (y)))
5435 Error_0 ("unable to handle non pair element"); 5244 Error_0 ("unable to handle non pair element");
5436 5245
5437 if (x == caar (y)) 5246 if (x == caar (y))
5443 else 5252 else
5444 s_return (S_F); 5253 s_return (S_F);
5445 5254
5446 5255
5447 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5256 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5448 SCHEME_V->args = car (SCHEME_V->args); 5257 SCHEME_V->args = a;
5449 5258
5450 if (SCHEME_V->args == NIL) 5259 if (SCHEME_V->args == NIL)
5451 s_return (S_F); 5260 s_return (S_F);
5452 else if (is_closure (SCHEME_V->args)) 5261 else if (is_closure (SCHEME_V->args))
5453 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5262 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5459 case OP_CLOSUREP: /* closure? */ 5268 case OP_CLOSUREP: /* closure? */
5460 /* 5269 /*
5461 * Note, macro object is also a closure. 5270 * Note, macro object is also a closure.
5462 * Therefore, (closure? <#MACRO>) ==> #t 5271 * Therefore, (closure? <#MACRO>) ==> #t
5463 */ 5272 */
5464 s_retbool (is_closure (car (SCHEME_V->args))); 5273 s_retbool (is_closure (a));
5465 5274
5466 case OP_MACROP: /* macro? */ 5275 case OP_MACROP: /* macro? */
5467 s_retbool (is_macro (car (SCHEME_V->args))); 5276 s_retbool (is_macro (a));
5468 } 5277 }
5469 5278
5470 return S_T; /* NOTREACHED */ 5279 if (USE_ERROR_CHECKING) abort ();
5471} 5280}
5472 5281
5282/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5473typedef pointer (*dispatch_func) (SCHEME_P_ enum scheme_opcodes); 5283typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5474 5284
5475typedef int (*test_predicate) (pointer); 5285typedef int (*test_predicate)(pointer);
5476static int 5286static int
5477is_any (pointer p) 5287is_any (pointer p)
5478{ 5288{
5479 return 1; 5289 return 1;
5480} 5290}
5481 5291
5482static int 5292static int
5483is_nonneg (pointer p) 5293is_nonneg (pointer p)
5484{ 5294{
5485 return ivalue (p) >= 0 && is_integer (p); 5295 return ivalue (p) >= 0 && is_integer (p);
5296}
5297
5298static int
5299tst_is_list (pointer p)
5300{
5301 return p == NIL || is_pair (p);
5486} 5302}
5487 5303
5488/* Correspond carefully with following defines! */ 5304/* Correspond carefully with following defines! */
5489static struct 5305static struct
5490{ 5306{
5491 test_predicate fct; 5307 test_predicate fct;
5492 const char *kind; 5308 const char *kind;
5493} tests[] = 5309} tests[] =
5494{ 5310{
5495 { 0, 0}, /* unused */ 5311 { is_any, 0 },
5496 { is_any, 0}, 5312 { is_string, "string" },
5497 { is_string, "string" }, 5313 { is_symbol, "symbol" },
5498 { is_symbol, "symbol" }, 5314 { is_port, "port" },
5499 { is_port, "port" },
5500 { is_inport, "input port" }, 5315 { is_inport, "input port" },
5501 { is_outport, "output port" }, 5316 { is_outport, "output port" },
5502 { is_environment, "environment" }, 5317 { is_environment, "environment" },
5503 { is_pair, "pair" }, 5318 { is_pair, "pair" },
5504 { 0, "pair or '()" }, 5319 { tst_is_list, "pair or '()" },
5505 { is_character, "character" }, 5320 { is_character, "character" },
5506 { is_vector, "vector" }, 5321 { is_vector, "vector" },
5507 { is_number, "number" }, 5322 { is_number, "number" },
5508 { is_integer, "integer" }, 5323 { is_integer, "integer" },
5509 { is_nonneg, "non-negative integer" } 5324 { is_nonneg, "non-negative integer" }
5510}; 5325};
5511 5326
5512#define TST_NONE 0 5327#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5513#define TST_ANY "\001" 5328#define TST_ANY "\001"
5514#define TST_STRING "\002" 5329#define TST_STRING "\002"
5515#define TST_SYMBOL "\003" 5330#define TST_SYMBOL "\003"
5516#define TST_PORT "\004" 5331#define TST_PORT "\004"
5517#define TST_INPORT "\005" 5332#define TST_INPORT "\005"
5518#define TST_OUTPORT "\006" 5333#define TST_OUTPORT "\006"
5519#define TST_ENVIRONMENT "\007" 5334#define TST_ENVIRONMENT "\007"
5520#define TST_PAIR "\010" 5335#define TST_PAIR "\010"
5521#define TST_LIST "\011" 5336#define TST_LIST "\011"
5522#define TST_CHAR "\012" 5337#define TST_CHAR "\012"
5523#define TST_VECTOR "\013" 5338#define TST_VECTOR "\013"
5524#define TST_NUMBER "\014" 5339#define TST_NUMBER "\014"
5525#define TST_INTEGER "\015" 5340#define TST_INTEGER "\015"
5526#define TST_NATURAL "\016" 5341#define TST_NATURAL "\016"
5342
5343#define INF_ARG 0xff
5344#define UNNAMED_OP ""
5345
5346static const char opnames[] =
5347#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5348#include "opdefines.h"
5349#undef OP_DEF
5350;
5351
5352static const char *
5353opname (int idx)
5354{
5355 const char *name = opnames;
5356
5357 /* should do this at compile time, but would require external program, right? */
5358 while (idx--)
5359 name += strlen (name) + 1;
5360
5361 return *name ? name : "ILLEGAL";
5362}
5363
5364static const char *
5365procname (pointer x)
5366{
5367 return opname (procnum (x));
5368}
5527 5369
5528typedef struct 5370typedef struct
5529{ 5371{
5530 dispatch_func func; 5372 uint8_t func;
5531 char *name; 5373 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5374 uint8_t builtin;
5532 int min_arity; 5375 uint8_t min_arity;
5533 int max_arity; 5376 uint8_t max_arity;
5534 char *arg_tests_encoding; 5377 char arg_tests_encoding[3];
5535} op_code_info; 5378} op_code_info;
5536 5379
5537#define INF_ARG 0xffff
5538
5539static op_code_info dispatch_table[] = { 5380static const op_code_info dispatch_table[] = {
5540#define OP_DEF(A,B,C,D,E,OP) {A,B,C,D,E}, 5381#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5541#include "opdefines.h" 5382#include "opdefines.h"
5383#undef OP_DEF
5542 {0} 5384 {0}
5543}; 5385};
5544 5386
5545static const char *
5546procname (pointer x)
5547{
5548 int n = procnum (x);
5549 const char *name = dispatch_table[n].name;
5550
5551 if (name == 0)
5552 name = "ILLEGAL!";
5553
5554 return name;
5555}
5556
5557/* kernel of this interpreter */ 5387/* kernel of this interpreter */
5558static void 5388static void ecb_hot
5559Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5389Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5560{ 5390{
5561 SCHEME_V->op = op; 5391 SCHEME_V->op = op;
5562 5392
5563 for (;;) 5393 for (;;)
5564 { 5394 {
5565 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5395 const op_code_info *pcd = dispatch_table + SCHEME_V->op;
5566 5396
5567#if USE_ERROR_CHECKING 5397#if USE_ERROR_CHECKING
5568 if (pcd->name) /* if built-in function, check arguments */ 5398 if (pcd->builtin) /* if built-in function, check arguments */
5569 { 5399 {
5570 int ok = 1;
5571 char msg[STRBUFFSIZE]; 5400 char msg[STRBUFFSIZE];
5572 int n = list_length (SCHEME_A_ SCHEME_V->args); 5401 int n = list_length (SCHEME_A_ SCHEME_V->args);
5573 5402
5574 /* Check number of arguments */ 5403 /* Check number of arguments */
5575 if (n < pcd->min_arity) 5404 if (ecb_expect_false (n < pcd->min_arity))
5576 { 5405 {
5577 ok = 0;
5578 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5406 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5579 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); 5407 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity);
5408 xError_1 (SCHEME_A_ msg, 0);
5409 continue;
5580 } 5410 }
5581 5411 else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG))
5582 if (ok && n > pcd->max_arity)
5583 { 5412 {
5584 ok = 0;
5585 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5413 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5586 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); 5414 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity);
5415 xError_1 (SCHEME_A_ msg, 0);
5416 continue;
5587 } 5417 }
5588 5418 else
5589 if (ok)
5590 { 5419 {
5591 if (pcd->arg_tests_encoding) 5420 if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */
5592 { 5421 {
5593 int i = 0; 5422 int i = 0;
5594 int j; 5423 int j;
5595 const char *t = pcd->arg_tests_encoding; 5424 const char *t = pcd->arg_tests_encoding;
5596 pointer arglist = SCHEME_V->args; 5425 pointer arglist = SCHEME_V->args;
5597 5426
5598 do 5427 do
5599 { 5428 {
5600 pointer arg = car (arglist); 5429 pointer arg = car (arglist);
5601 5430
5602 j = (int) t[0]; 5431 j = t[0];
5603 5432
5604 if (j == TST_LIST[0]) 5433 if (!tests[j - 1].fct (arg))
5605 {
5606 if (arg != NIL && !is_pair (arg))
5607 break; 5434 break;
5608 }
5609 else
5610 {
5611 if (!tests[j].fct (arg))
5612 break;
5613 }
5614 5435
5615 if (t[1] != 0) /* last test is replicated as necessary */ 5436 if (t[1]) /* last test is replicated as necessary */
5616 t++; 5437 t++;
5617 5438
5618 arglist = cdr (arglist); 5439 arglist = cdr (arglist);
5619 i++; 5440 i++;
5620 } 5441 }
5621 while (i < n); 5442 while (i < n);
5622 5443
5623 if (i < n) 5444 if (i < n)
5624 { 5445 {
5625 ok = 0;
5626 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); 5446 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind);
5447 xError_1 (SCHEME_A_ msg, 0);
5448 continue;
5627 } 5449 }
5628 } 5450 }
5629 } 5451 }
5630
5631 if (!ok)
5632 {
5633 if (xError_1 (SCHEME_A_ msg, 0) == NIL)
5634 return;
5635
5636 pcd = dispatch_table + SCHEME_V->op;
5637 }
5638 } 5452 }
5639#endif 5453#endif
5640 5454
5641 ok_to_freely_gc (SCHEME_A); 5455 ok_to_freely_gc (SCHEME_A);
5642 5456
5643 if (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL) 5457 static const dispatch_func dispatch_funcs[] = {
5458 opexe_0,
5459 opexe_1,
5460 opexe_2,
5461 opexe_3,
5462 opexe_4,
5463 opexe_5,
5464 opexe_6,
5465 };
5466
5467 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5644 return; 5468 return;
5645 5469
5646 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5470 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5647 { 5471 {
5648 xwrstr ("No memory!\n"); 5472 xwrstr ("No memory!\n");
5706 5530
5707 case 'd': 5531 case 'd':
5708 return OP_COND0; /* cond */ 5532 return OP_COND0; /* cond */
5709 5533
5710 case '*': 5534 case '*':
5711 return OP_LET0AST; /* let* */ 5535 return OP_LET0AST;/* let* */
5712 5536
5713 default: 5537 default:
5714 return OP_SET0; /* set! */ 5538 return OP_SET0; /* set! */
5715 } 5539 }
5716 5540
5738 5562
5739 case 'f': 5563 case 'f':
5740 return OP_DEF0; /* define */ 5564 return OP_DEF0; /* define */
5741 5565
5742 default: 5566 default:
5743 return OP_LET0REC; /* letrec */ 5567 return OP_LET0REC;/* letrec */
5744 } 5568 }
5745 5569
5746 default: 5570 default:
5747 return OP_C0STREAM; /* cons-stream */ 5571 return OP_C0STREAM; /* cons-stream */
5748 } 5572 }
5749} 5573}
5750 5574
5751#if USE_MULTIPLICITY 5575#if USE_MULTIPLICITY
5752scheme * 5576ecb_cold scheme *
5753scheme_init_new () 5577scheme_init_new ()
5754{ 5578{
5755 scheme *sc = malloc (sizeof (scheme)); 5579 scheme *sc = malloc (sizeof (scheme));
5756 5580
5757 if (!scheme_init (SCHEME_A)) 5581 if (!scheme_init (SCHEME_A))
5762 else 5586 else
5763 return sc; 5587 return sc;
5764} 5588}
5765#endif 5589#endif
5766 5590
5767int 5591ecb_cold int
5768scheme_init (SCHEME_P) 5592scheme_init (SCHEME_P)
5769{ 5593{
5770 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5594 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5771 pointer x; 5595 pointer x;
5772 5596
5815 set_cdr (S_T, S_T); 5639 set_cdr (S_T, S_T);
5816 /* init F */ 5640 /* init F */
5817 set_typeflag (S_F, T_ATOM | T_MARK); 5641 set_typeflag (S_F, T_ATOM | T_MARK);
5818 set_car (S_F, S_F); 5642 set_car (S_F, S_F);
5819 set_cdr (S_F, S_F); 5643 set_cdr (S_F, S_F);
5644 /* init EOF_OBJ */
5645 set_typeflag (S_EOF, T_ATOM | T_MARK);
5646 set_car (S_EOF, S_EOF);
5647 set_cdr (S_EOF, S_EOF);
5820 /* init sink */ 5648 /* init sink */
5821 set_typeflag (S_SINK, T_PAIR | T_MARK); 5649 set_typeflag (S_SINK, T_PAIR | T_MARK);
5822 set_car (S_SINK, NIL); 5650 set_car (S_SINK, NIL);
5651
5823 /* init c_nest */ 5652 /* init c_nest */
5824 SCHEME_V->c_nest = NIL; 5653 SCHEME_V->c_nest = NIL;
5825 5654
5826 SCHEME_V->oblist = oblist_initial_value (SCHEME_A); 5655 SCHEME_V->oblist = oblist_initial_value (SCHEME_A);
5827 /* init global_env */ 5656 /* init global_env */
5840 5669
5841 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) 5670 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
5842 assign_syntax (SCHEME_A_ syntax_names[i]); 5671 assign_syntax (SCHEME_A_ syntax_names[i]);
5843 } 5672 }
5844 5673
5674 // TODO: should iterate via strlen, to avoid n² complexity
5845 for (i = 0; i < n; i++) 5675 for (i = 0; i < n; i++)
5846 if (dispatch_table[i].name != 0) 5676 if (dispatch_table[i].builtin)
5847 assign_proc (SCHEME_A_ i, dispatch_table[i].name); 5677 assign_proc (SCHEME_A_ i, opname (i));
5848 5678
5849 /* initialization of global pointers to special symbols */ 5679 /* initialization of global pointers to special symbols */
5850 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 5680 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
5851 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 5681 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
5852 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 5682 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
5891scheme_set_external_data (SCHEME_P_ void *p) 5721scheme_set_external_data (SCHEME_P_ void *p)
5892{ 5722{
5893 SCHEME_V->ext_data = p; 5723 SCHEME_V->ext_data = p;
5894} 5724}
5895 5725
5896void 5726ecb_cold void
5897scheme_deinit (SCHEME_P) 5727scheme_deinit (SCHEME_P)
5898{ 5728{
5899 int i; 5729 int i;
5900 5730
5901#if SHOW_ERROR_LINE 5731#if SHOW_ERROR_LINE
5993{ 5823{
5994 dump_stack_reset (SCHEME_A); 5824 dump_stack_reset (SCHEME_A);
5995 SCHEME_V->envir = SCHEME_V->global_env; 5825 SCHEME_V->envir = SCHEME_V->global_env;
5996 SCHEME_V->file_i = 0; 5826 SCHEME_V->file_i = 0;
5997 SCHEME_V->load_stack[0].kind = port_input | port_string; 5827 SCHEME_V->load_stack[0].kind = port_input | port_string;
5998 SCHEME_V->load_stack[0].rep.string.start = (char *) cmd; /* This func respects const */ 5828 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */
5999 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *) cmd + strlen (cmd); 5829 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd);
6000 SCHEME_V->load_stack[0].rep.string.curr = (char *) cmd; 5830 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
6001#if USE_PORTS 5831#if USE_PORTS
6002 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 5832 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
6003#endif 5833#endif
6004 SCHEME_V->retcode = 0; 5834 SCHEME_V->retcode = 0;
6005 SCHEME_V->interactive_repl = 0; 5835 SCHEME_V->interactive_repl = 0;
6110 5940
6111/* ========== Main ========== */ 5941/* ========== Main ========== */
6112 5942
6113#if STANDALONE 5943#if STANDALONE
6114 5944
6115# if defined(__APPLE__) && !defined (OSX)
6116int
6117main ()
6118{
6119 extern MacTS_main (int argc, char **argv);
6120 char **argv;
6121 int argc = ccommand (&argv);
6122
6123 MacTS_main (argc, argv);
6124 return 0;
6125}
6126
6127int
6128MacTS_main (int argc, char **argv)
6129{
6130# else
6131int 5945int
6132main (int argc, char **argv) 5946main (int argc, char **argv)
6133{ 5947{
6134# endif
6135# if USE_MULTIPLICITY 5948# if USE_MULTIPLICITY
6136 scheme ssc; 5949 scheme ssc;
6137 scheme *const SCHEME_V = &ssc; 5950 scheme *const SCHEME_V = &ssc;
6138# else 5951# else
6139# endif 5952# endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines