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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines