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.1 by root, Wed Nov 25 05:02:56 2015 UTC vs.
Revision 1.8 by root, Wed Nov 25 22:36:25 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.
26#endif 31#endif
27 32
28#include <sys/types.h> 33#include <sys/types.h>
29#include <sys/stat.h> 34#include <sys/stat.h>
30#include <fcntl.h> 35#include <fcntl.h>
36
37#include <string.h>
38#include <stdlib.h>
31 39
32#include <limits.h> 40#include <limits.h>
33#include <inttypes.h> 41#include <inttypes.h>
34#include <float.h> 42#include <float.h>
35//#include <ctype.h> 43//#include <ctype.h>
52 60
53#define BACKQUOTE '`' 61#define BACKQUOTE '`'
54#define DELIMITERS "()\";\f\t\v\n\r " 62#define DELIMITERS "()\";\f\t\v\n\r "
55 63
56#define NIL (&SCHEME_V->xNIL) //TODO: make this 0? 64#define NIL (&SCHEME_V->xNIL) //TODO: make this 0?
57#define S_T (&SCHEME_V->xT) 65#define S_T (&SCHEME_V->xT) //TODO: magic ptr value?
58#define S_F (&SCHEME_V->xF) 66#define S_F (&SCHEME_V->xF) //TODO: magic ptr value?
59#define S_SINK (&SCHEME_V->xsink) 67#define S_SINK (&SCHEME_V->xsink)
60#define S_EOF (&SCHEME_V->xEOF_OBJ) 68#define S_EOF (&SCHEME_V->xEOF_OBJ)
61
62/*
63 * Basic memory allocation units
64 */
65
66#define banner "TinyScheme 1.41-s"
67
68#include <string.h>
69#include <stdlib.h>
70 69
71#if !USE_MULTIPLICITY 70#if !USE_MULTIPLICITY
72static scheme sc; 71static scheme sc;
73#endif 72#endif
74 73
134 c += 'a' - 'A'; 133 c += 'a' - 'A';
135 134
136 return c; 135 return c;
137} 136}
138 137
138static int
139xisdigit (char c)
140{
141 return c >= '0' && c <= '9';
142}
143
144#define toupper(c) xtoupper (c)
145#define tolower(c) xtolower (c)
146#define isdigit(c) xisdigit (c)
147
139#if USE_STRLWR 148#if USE_STRLWR
140static const char * 149static const char *
141strlwr (char *s) 150strlwr (char *s)
142{ 151{
143 const char *p = s; 152 const char *p = s;
152} 161}
153#endif 162#endif
154 163
155#define stricmp(a,b) strcmp (a, b) 164#define stricmp(a,b) strcmp (a, b)
156#define strlwr(s) (s) 165#define strlwr(s) (s)
157#define toupper(c) xtoupper(c)
158#define tolower(c) xtolower(c)
159 166
160#ifndef prompt 167#ifndef prompt
161# define prompt "ts> " 168# define prompt "ts> "
162#endif 169#endif
163 170
169# define FIRST_CELLSEGS 3 176# define FIRST_CELLSEGS 3
170#endif 177#endif
171 178
172enum scheme_types 179enum scheme_types
173{ 180{
174 T_NULL, 181 T_FREE,
175 T_STRING, 182 T_STRING,
176 T_NUMBER, 183 T_NUMBER,
177 T_SYMBOL, 184 T_SYMBOL,
178 T_PROC, 185 T_PROC,
179 T_PAIR, 186 T_PAIR,
184 T_PORT, 191 T_PORT,
185 T_VECTOR, 192 T_VECTOR,
186 T_MACRO, 193 T_MACRO,
187 T_PROMISE, 194 T_PROMISE,
188 T_ENVIRONMENT, 195 T_ENVIRONMENT,
196 /* one more... */
189 T_NUM_SYSTEM_TYPES 197 T_NUM_SYSTEM_TYPES
190}; 198};
191 199
192#define T_MASKTYPE 31 /* 0000000000011111 */ 200#define T_MASKTYPE 0x000f
193#define T_SYNTAX 4096 /* 0001000000000000 */ 201#define T_SYNTAX 0x0010
194#define T_IMMUTABLE 8192 /* 0010000000000000 */ 202#define T_IMMUTABLE 0x0020
195#define T_ATOM 16384 /* 0100000000000000 */ /* only for gc */ 203#define T_ATOM 0x0040 /* only for gc */
196#define CLRATOM 49151 /* 1011111111111111 */ /* only for gc */ 204#define T_MARK 0x0080 /* only for gc */
197#define MARK 32768 /* 1000000000000000 */
198#define UNMARK 32767 /* 0111111111111111 */
199
200 205
201static num num_add (num a, num b); 206static num num_add (num a, num b);
202static num num_mul (num a, num b); 207static num num_mul (num a, num b);
203static num num_div (num a, num b); 208static num num_div (num a, num b);
204static num num_intdiv (num a, num b); 209static num num_intdiv (num a, num b);
244is_vector (pointer p) 249is_vector (pointer p)
245{ 250{
246 return type (p) == T_VECTOR; 251 return type (p) == T_VECTOR;
247} 252}
248 253
254#define vecvalue(p) ((p)->object.vector.vvalue)
255#define veclength(p) ((p)->object.vector.length)
249INTERFACE void fill_vector (pointer vec, pointer obj); 256INTERFACE void fill_vector (pointer vec, pointer obj);
257INTERFACE uint32_t vector_length (pointer vec);
250INTERFACE pointer vector_elem (pointer vec, int ielem); 258INTERFACE pointer vector_elem (pointer vec, uint32_t ielem);
251INTERFACE void set_vector_elem (pointer vec, int ielem, pointer a); 259INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a);
260
261INTERFACE uint32_t
262vector_length (pointer vec)
263{
264 return vec->object.vector.length;
265}
252 266
253INTERFACE INLINE int 267INTERFACE INLINE int
254is_number (pointer p) 268is_number (pointer p)
255{ 269{
256 return type (p) == T_NUMBER; 270 return type (p) == T_NUMBER;
290nvalue (pointer p) 304nvalue (pointer p)
291{ 305{
292 return (p)->object.number; 306 return (p)->object.number;
293} 307}
294 308
295INTERFACE long 309static IVALUE
310num_get_ivalue (const num n)
311{
312 return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n);
313}
314
315static RVALUE
316num_get_rvalue (const num n)
317{
318 return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n);
319}
320
321INTERFACE IVALUE
296ivalue (pointer p) 322ivalue (pointer p)
297{ 323{
298 return num_is_integer (p) ? num_ivalue ((p)->object.number) : (long) num_rvalue ((p)->object.number); 324 return num_get_ivalue (p->object.number);
299} 325}
300 326
301INTERFACE RVALUE 327INTERFACE RVALUE
302rvalue (pointer p) 328rvalue (pointer p)
303{ 329{
304 return num_is_integer (p) ? (RVALUE) num_ivalue ((p)->object.number) : num_rvalue ((p)->object.number); 330 return num_get_rvalue (p->object.number);
305} 331}
306 332
307#define ivalue_unchecked(p) ((p)->object.number.value.ivalue) 333#define ivalue_unchecked(p) ((p)->object.number.value.ivalue)
308#if USE_FLOAT 334#if USE_REAL
309# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 335# define rvalue_unchecked(p) ((p)->object.number.value.rvalue)
310# define set_num_integer(p) (p)->object.number.is_fixnum=1; 336# define set_num_integer(p) (p)->object.number.is_fixnum=1;
311# define set_num_real(p) (p)->object.number.is_fixnum=0; 337# define set_num_real(p) (p)->object.number.is_fixnum=0;
312#else 338#else
313# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 339# define rvalue_unchecked(p) ((p)->object.number.value.ivalue)
424syntaxname (pointer p) 450syntaxname (pointer p)
425{ 451{
426 return strvalue (car (p)); 452 return strvalue (car (p));
427} 453}
428 454
429#define procnum(p) ivalue(p) 455#define procnum(p) ivalue (p)
430static const char *procname (pointer x); 456static const char *procname (pointer x);
431 457
432INTERFACE INLINE int 458INTERFACE INLINE int
433is_closure (pointer p) 459is_closure (pointer p)
434{ 460{
477 503
478#define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT) 504#define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT)
479 505
480#define is_atom(p) (typeflag (p) & T_ATOM) 506#define is_atom(p) (typeflag (p) & T_ATOM)
481#define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) 507#define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM)
482#define clratom(p) set_typeflag ((p), typeflag (p) & CLRATOM) 508#define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM)
483 509
484#define is_mark(p) (typeflag (p) & MARK) 510#define is_mark(p) (typeflag (p) & T_MARK)
485#define setmark(p) set_typeflag ((p), typeflag (p) | MARK) 511#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
486#define clrmark(p) set_typeflag ((p), typeflag (p) & UNMARK) 512#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
487 513
488INTERFACE INLINE int 514INTERFACE INLINE int
489is_immutable (pointer p) 515is_immutable (pointer p)
490{ 516{
491 return typeflag (p) & T_IMMUTABLE; 517 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
492} 518}
493 519
494INTERFACE INLINE void 520INTERFACE INLINE void
495setimmutable (pointer p) 521setimmutable (pointer p)
496{ 522{
523#if USE_ERROR_CHECKING
497 set_typeflag (p, T_IMMUTABLE); 524 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
525#endif
498} 526}
499 527
500#if USE_CHAR_CLASSIFIERS 528#if USE_CHAR_CLASSIFIERS
501static INLINE int 529static INLINE int
502Cisalpha (int c) 530Cisalpha (int c)
593static int file_push (SCHEME_P_ const char *fname); 621static int file_push (SCHEME_P_ const char *fname);
594static void file_pop (SCHEME_P); 622static void file_pop (SCHEME_P);
595static int file_interactive (SCHEME_P); 623static int file_interactive (SCHEME_P);
596static INLINE int is_one_of (char *s, int c); 624static INLINE int is_one_of (char *s, int c);
597static int alloc_cellseg (SCHEME_P_ int n); 625static int alloc_cellseg (SCHEME_P_ int n);
598static long binary_decode (const char *s);
599static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); 626static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b);
600static pointer xget_cell (SCHEME_P_ pointer a, pointer b);
601static pointer reserve_cells (SCHEME_P_ int n);
602static pointer get_consecutive_cells (SCHEME_P_ int n);
603static pointer find_consecutive_cells (SCHEME_P_ int n);
604static void finalize_cell (SCHEME_P_ pointer a); 627static void finalize_cell (SCHEME_P_ pointer a);
605static int count_consecutive_cells (pointer x, int needed); 628static int count_consecutive_cells (pointer x, int needed);
606static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 629static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
607static pointer mk_number (SCHEME_P_ num n); 630static pointer mk_number (SCHEME_P_ const num n);
608static char *store_string (SCHEME_P_ int len, const char *str, char fill); 631static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
609static pointer mk_vector (SCHEME_P_ int len); 632static pointer mk_vector (SCHEME_P_ uint32_t len);
610static pointer mk_atom (SCHEME_P_ char *q); 633static pointer mk_atom (SCHEME_P_ char *q);
611static pointer mk_sharp_const (SCHEME_P_ char *name); 634static pointer mk_sharp_const (SCHEME_P_ char *name);
612 635
613#if USE_PORTS 636#if USE_PORTS
614static pointer mk_port (SCHEME_P_ port *p); 637static pointer mk_port (SCHEME_P_ port *p);
646static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); 669static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op);
647static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); 670static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op);
648static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); 671static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op);
649static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); 672static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op);
650static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 673static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
651static void assign_syntax (SCHEME_P_ char *name); 674static void assign_syntax (SCHEME_P_ const char *name);
652static int syntaxnum (pointer p); 675static int syntaxnum (pointer p);
653static void assign_proc (SCHEME_P_ enum scheme_opcodes, char *name); 676static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
654 677
655static num 678static num
656num_add (num a, num b) 679num_add (num a, num b)
657{ 680{
658 num ret; 681 num ret;
659 682
660 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 683 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
661 684
662 if (num_is_fixnum (ret)) 685 if (num_is_fixnum (ret))
663 num_set_ivalue (ret, a.value.ivalue + b.value.ivalue); 686 num_set_ivalue (ret, num_get_ivalue (a) + num_get_ivalue (b));
664 else 687 else
665 num_set_rvalue (ret, num_rvalue (a) + num_rvalue (b)); 688 num_set_rvalue (ret, num_get_rvalue (a) + num_get_rvalue (b));
666 689
667 return ret; 690 return ret;
668} 691}
669 692
670static num 693static num
673 num ret; 696 num ret;
674 697
675 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 698 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
676 699
677 if (num_is_fixnum (ret)) 700 if (num_is_fixnum (ret))
678 num_set_ivalue (ret, a.value.ivalue * b.value.ivalue); 701 num_set_ivalue (ret, num_get_ivalue (a) * num_get_ivalue (b));
679 else 702 else
680 num_set_rvalue (ret, num_rvalue (a) * num_rvalue (b)); 703 num_set_rvalue (ret, num_get_rvalue (a) * num_get_rvalue (b));
681 704
682 return ret; 705 return ret;
683} 706}
684 707
685static num 708static num
686num_div (num a, num b) 709num_div (num a, num b)
687{ 710{
688 num ret; 711 num ret;
689 712
690 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && a.value.ivalue % b.value.ivalue == 0); 713 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0);
691 714
692 if (num_is_fixnum (ret)) 715 if (num_is_fixnum (ret))
693 num_set_ivalue (ret, a.value.ivalue / b.value.ivalue); 716 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b));
694 else 717 else
695 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); 718 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b));
696 719
697 return ret; 720 return ret;
698} 721}
699 722
700static num 723static num
703 num ret; 726 num ret;
704 727
705 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 728 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
706 729
707 if (num_is_fixnum (ret)) 730 if (num_is_fixnum (ret))
708 num_set_ivalue (ret, a.value.ivalue / b.value.ivalue); 731 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b));
709 else 732 else
710 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); 733 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b));
711 734
712 return ret; 735 return ret;
713} 736}
714 737
715static num 738static num
718 num ret; 741 num ret;
719 742
720 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 743 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
721 744
722 if (num_is_fixnum (ret)) 745 if (num_is_fixnum (ret))
723 num_set_ivalue (ret, a.value.ivalue - b.value.ivalue); 746 num_set_ivalue (ret, num_get_ivalue (a) - num_get_ivalue (b));
724 else 747 else
725 num_set_rvalue (ret, num_rvalue (a) - num_rvalue (b)); 748 num_set_rvalue (ret, num_get_rvalue (a) - num_get_rvalue (b));
726 749
727 return ret; 750 return ret;
728} 751}
729 752
730static num 753static num
732{ 755{
733 num ret; 756 num ret;
734 long e1, e2, res; 757 long e1, e2, res;
735 758
736 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 759 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
737 e1 = num_ivalue (a); 760 e1 = num_get_ivalue (a);
738 e2 = num_ivalue (b); 761 e2 = num_get_ivalue (b);
739 res = e1 % e2; 762 res = e1 % e2;
740 763
741 /* remainder should have same sign as second operand */ 764 /* remainder should have same sign as second operand */
742 if (res > 0) 765 if (res > 0)
743 { 766 {
759{ 782{
760 num ret; 783 num ret;
761 long e1, e2, res; 784 long e1, e2, res;
762 785
763 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 786 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
764 e1 = num_ivalue (a); 787 e1 = num_get_ivalue (a);
765 e2 = num_ivalue (b); 788 e2 = num_get_ivalue (b);
766 res = e1 % e2; 789 res = e1 % e2;
767 790
768 /* modulo should have same sign as second operand */ 791 /* modulo should have same sign as second operand */
769 if (res * e2 < 0) 792 if (res * e2 < 0)
770 res += e2; 793 res += e2;
778{ 801{
779 int ret; 802 int ret;
780 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 803 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
781 804
782 if (is_fixnum) 805 if (is_fixnum)
783 ret = a.value.ivalue == b.value.ivalue; 806 ret = num_get_ivalue (a) == num_get_ivalue (b);
784 else 807 else
785 ret = num_rvalue (a) == num_rvalue (b); 808 ret = num_get_rvalue (a) == num_get_rvalue (b);
786 809
787 return ret; 810 return ret;
788} 811}
789 812
790 813
793{ 816{
794 int ret; 817 int ret;
795 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 818 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
796 819
797 if (is_fixnum) 820 if (is_fixnum)
798 ret = a.value.ivalue > b.value.ivalue; 821 ret = num_get_ivalue (a) > num_get_ivalue (b);
799 else 822 else
800 ret = num_rvalue (a) > num_rvalue (b); 823 ret = num_get_rvalue (a) > num_get_rvalue (b);
801 824
802 return ret; 825 return ret;
803} 826}
804 827
805static int 828static int
813{ 836{
814 int ret; 837 int ret;
815 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 838 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
816 839
817 if (is_fixnum) 840 if (is_fixnum)
818 ret = a.value.ivalue < b.value.ivalue; 841 ret = num_get_ivalue (a) < num_get_ivalue (b);
819 else 842 else
820 ret = num_rvalue (a) < num_rvalue (b); 843 ret = num_get_rvalue (a) < num_get_rvalue (b);
821 844
822 return ret; 845 return ret;
823} 846}
824 847
825static int 848static int
854#endif 877#endif
855 878
856static int 879static int
857is_zero_rvalue (RVALUE x) 880is_zero_rvalue (RVALUE x)
858{ 881{
859#if USE_FLOAT 882#if USE_REAL
860 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ 883 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */
861#else 884#else
862 return x == 0; 885 return x == 0;
863#endif 886#endif
864}
865
866static long
867binary_decode (const char *s)
868{
869 long x = 0;
870
871 while (*s != 0 && (*s == '1' || *s == '0'))
872 {
873 x <<= 1;
874 x += *s - '0';
875 s++;
876 }
877
878 return x;
879} 887}
880 888
881/* allocate new cell segment */ 889/* allocate new cell segment */
882static int 890static int
883alloc_cellseg (SCHEME_P_ int n) 891alloc_cellseg (SCHEME_P_ int n)
927 SCHEME_V->fcells += segsize; 935 SCHEME_V->fcells += segsize;
928 last = newp + segsize - 1; 936 last = newp + segsize - 1;
929 937
930 for (p = newp; p <= last; p++) 938 for (p = newp; p <= last; p++)
931 { 939 {
932 set_typeflag (p, 0); 940 set_typeflag (p, T_FREE);
941 set_car (p, NIL);
933 set_cdr (p, p + 1); 942 set_cdr (p, p + 1);
934 set_car (p, NIL);
935 } 943 }
936 944
937 /* insert new cells in address order on free list */ 945 /* insert new cells in address order on free list */
938 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) 946 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell)
939 { 947 {
953 } 961 }
954 962
955 return n; 963 return n;
956} 964}
957 965
958/* get new cell. parameter a, b is marked by gc. */ 966/* get new cell. parameter a, b is marked by gc. */
959static INLINE pointer 967static INLINE pointer
960get_cell_x (SCHEME_P_ pointer a, pointer b) 968get_cell_x (SCHEME_P_ pointer a, pointer b)
961{ 969{
962 if (SCHEME_V->free_cell == NIL) 970 if (SCHEME_V->free_cell == NIL)
963 { 971 {
991 --SCHEME_V->fcells; 999 --SCHEME_V->fcells;
992 return x; 1000 return x;
993 } 1001 }
994} 1002}
995 1003
996/* make sure that there is a given number of cells free */
997static pointer
998reserve_cells (SCHEME_P_ int n)
999{
1000 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
1001 return NIL;
1002
1003 /* Are there enough cells available? */
1004 if (SCHEME_V->fcells < n)
1005 {
1006 /* If not, try gc'ing some */
1007 gc (SCHEME_A_ NIL, NIL);
1008
1009 if (SCHEME_V->fcells < n)
1010 {
1011 /* If there still aren't, try getting more heap */
1012 if (!alloc_cellseg (SCHEME_A_ 1) && USE_ERROR_CHECKING)
1013 {
1014 SCHEME_V->no_memory = 1;
1015 return NIL;
1016 }
1017 }
1018
1019 if (SCHEME_V->fcells < n && USE_ERROR_CHECKING)
1020 {
1021 /* If all fail, report failure */
1022 SCHEME_V->no_memory = 1;
1023 return NIL;
1024 }
1025 }
1026
1027 return S_T;
1028}
1029
1030static pointer
1031get_consecutive_cells (SCHEME_P_ int n)
1032{
1033 pointer x;
1034
1035 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
1036 return S_SINK;
1037
1038 /* Are there any cells available? */
1039 x = find_consecutive_cells (SCHEME_A_ n);
1040
1041 if (x != NIL)
1042 return x;
1043
1044 /* If not, try gc'ing some */
1045 gc (SCHEME_A_ NIL, NIL);
1046
1047 for (;;)
1048 {
1049 x = find_consecutive_cells (SCHEME_A_ n);
1050
1051 if (x != NIL)
1052 return x;
1053
1054 /* If there still aren't, try getting more heap */
1055 if (!alloc_cellseg (SCHEME_A_ 1))
1056 {
1057#if USE_ERROR_CHECKING
1058 SCHEME_V->no_memory = 1;
1059 return S_SINK;
1060#endif
1061 }
1062 }
1063}
1064
1065static int
1066count_consecutive_cells (pointer x, int needed)
1067{
1068 int n = 1;
1069
1070 while (cdr (x) == x + 1)
1071 {
1072 x = cdr (x);
1073 n++;
1074
1075 if (n > needed)
1076 return n;
1077 }
1078
1079 return n;
1080}
1081
1082static pointer
1083find_consecutive_cells (SCHEME_P_ int n)
1084{
1085 pointer *pp;
1086 int cnt;
1087
1088 pp = &SCHEME_V->free_cell;
1089
1090 while (*pp != NIL)
1091 {
1092 cnt = count_consecutive_cells (*pp, n);
1093
1094 if (cnt >= n)
1095 {
1096 pointer x = *pp;
1097
1098 *pp = cdr (*pp + n - 1);
1099 SCHEME_V->fcells -= n;
1100 return x;
1101 }
1102
1103 pp = &cdr (*pp + cnt - 1);
1104 }
1105
1106 return NIL;
1107}
1108
1109/* To retain recent allocs before interpreter knows about them - 1004/* To retain recent allocs before interpreter knows about them -
1110 Tehom */ 1005 Tehom */
1111 1006
1112static void 1007static void
1113push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 1008push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
1114{ 1009{
1115 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 1010 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
1116 1011
1117 set_typeflag (holder, T_PAIR | T_IMMUTABLE); 1012 set_typeflag (holder, T_PAIR);
1013 setimmutable (holder);
1118 set_car (holder, recent); 1014 set_car (holder, recent);
1119 set_cdr (holder, car (S_SINK)); 1015 set_cdr (holder, car (S_SINK));
1120 set_car (S_SINK, holder); 1016 set_car (S_SINK, holder);
1121} 1017}
1122 1018
1135 1031
1136 return cell; 1032 return cell;
1137} 1033}
1138 1034
1139static pointer 1035static pointer
1140get_vector_object (SCHEME_P_ int len, pointer init) 1036get_vector_object (SCHEME_P_ uint32_t len, pointer init)
1141{ 1037{
1142 pointer cells = get_consecutive_cells (SCHEME_A_ len / 2 + len % 2 + 1); 1038 pointer v = get_cell_x (SCHEME_A_ 0, 0);
1039 pointer *e = malloc (len * sizeof (pointer));
1143 1040
1144#if USE_ERROR_CHECKING 1041 if (!e && USE_ERROR_CHECKING)
1145 if (SCHEME_V->no_memory)
1146 return S_SINK; 1042 return S_SINK;
1147#endif
1148 1043
1149 /* Record it as a vector so that gc understands it. */ 1044 /* Record it as a vector so that gc understands it. */
1150 set_typeflag (cells, T_VECTOR | T_ATOM); 1045 set_typeflag (v, T_VECTOR | T_ATOM);
1151 ivalue_unchecked (cells) = len; 1046
1152 set_num_integer (cells); 1047 v->object.vector.vvalue = e;
1048 v->object.vector.length = len;
1153 fill_vector (cells, init); 1049 fill_vector (v, init);
1154 push_recent_alloc (SCHEME_A_ cells, NIL); 1050 push_recent_alloc (SCHEME_A_ v, NIL);
1155 1051
1156 return cells; 1052 return v;
1157} 1053}
1158 1054
1159static INLINE void 1055static INLINE void
1160ok_to_freely_gc (SCHEME_P) 1056ok_to_freely_gc (SCHEME_P)
1161{ 1057{
1164 1060
1165#if defined TSGRIND 1061#if defined TSGRIND
1166static void 1062static void
1167check_cell_alloced (pointer p, int expect_alloced) 1063check_cell_alloced (pointer p, int expect_alloced)
1168{ 1064{
1169 /* Can't use putstr(SCHEME_A_ str) because callers have no access to 1065 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */
1170 sc. */
1171 if (typeflag (p) & !expect_alloced) 1066 if (typeflag (p) & !expect_alloced)
1172 xwrstr ("Cell is already allocated!\n"); 1067 xwrstr ("Cell is already allocated!\n");
1173 1068
1174 if (!(typeflag (p)) & expect_alloced) 1069 if (!(typeflag (p)) & expect_alloced)
1175 xwrstr ("Cell is not allocated!\n"); 1070 xwrstr ("Cell is not allocated!\n");
1198 if (immutable) 1093 if (immutable)
1199 setimmutable (x); 1094 setimmutable (x);
1200 1095
1201 set_car (x, a); 1096 set_car (x, a);
1202 set_cdr (x, b); 1097 set_cdr (x, b);
1098
1203 return x; 1099 return x;
1204} 1100}
1205 1101
1206/* ========== oblist implementation ========== */ 1102/* ========== oblist implementation ========== */
1207 1103
1217 1113
1218/* returns the new symbol */ 1114/* returns the new symbol */
1219static pointer 1115static pointer
1220oblist_add_by_name (SCHEME_P_ const char *name) 1116oblist_add_by_name (SCHEME_P_ const char *name)
1221{ 1117{
1222 pointer x;
1223 int location; 1118 int location;
1224 1119
1225 x = immutable_cons (mk_string (SCHEME_A_ name), NIL); 1120 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1226 set_typeflag (x, T_SYMBOL); 1121 set_typeflag (x, T_SYMBOL);
1227 setimmutable (car (x)); 1122 setimmutable (car (x));
1228 1123
1229 location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); 1124 location = hash_fn (name, veclength (SCHEME_V->oblist));
1230 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); 1125 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location)));
1231 return x; 1126 return x;
1232} 1127}
1233 1128
1234static INLINE pointer 1129static INLINE pointer
1236{ 1131{
1237 int location; 1132 int location;
1238 pointer x; 1133 pointer x;
1239 char *s; 1134 char *s;
1240 1135
1241 location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); 1136 location = hash_fn (name, veclength (SCHEME_V->oblist));
1242 1137
1243 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1138 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1244 { 1139 {
1245 s = symname (car (x)); 1140 s = symname (car (x));
1246 1141
1257{ 1152{
1258 int i; 1153 int i;
1259 pointer x; 1154 pointer x;
1260 pointer ob_list = NIL; 1155 pointer ob_list = NIL;
1261 1156
1262 for (i = 0; i < ivalue_unchecked (SCHEME_V->oblist); i++) 1157 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1263 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1158 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1264 ob_list = cons (x, ob_list); 1159 ob_list = cons (x, ob_list);
1265 1160
1266 return ob_list; 1161 return ob_list;
1267} 1162}
1341mk_character (SCHEME_P_ int c) 1236mk_character (SCHEME_P_ int c)
1342{ 1237{
1343 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1238 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1344 1239
1345 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1240 set_typeflag (x, (T_CHARACTER | T_ATOM));
1346 ivalue_unchecked (x) = c; 1241 ivalue_unchecked (x) = c & 0xff;
1347 set_num_integer (x); 1242 set_num_integer (x);
1348 return x; 1243 return x;
1349} 1244}
1350 1245
1351/* get number atom (integer) */ 1246/* get number atom (integer) */
1370 set_num_real (x); 1265 set_num_real (x);
1371 return x; 1266 return x;
1372} 1267}
1373 1268
1374static pointer 1269static pointer
1375mk_number (SCHEME_P_ num n) 1270mk_number (SCHEME_P_ const num n)
1376{ 1271{
1377 if (num_is_fixnum (n)) 1272 if (num_is_fixnum (n))
1378 {
1379 return mk_integer (SCHEME_A_ num_ivalue (n)); 1273 return mk_integer (SCHEME_A_ num_get_ivalue (n));
1380 }
1381 else 1274 else
1382 {
1383 return mk_real (SCHEME_A_ num_rvalue (n)); 1275 return mk_real (SCHEME_A_ num_get_rvalue (n));
1384 }
1385} 1276}
1386 1277
1387/* allocate name to string area */ 1278/* allocate name to string area */
1388static char * 1279static char *
1389store_string (SCHEME_P_ int len_str, const char *str, char fill) 1280store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1390{ 1281{
1391 char *q;
1392
1393 q = (char *)malloc (len_str + 1); 1282 char *q = malloc (len_str + 1);
1394 1283
1395 if (q == 0) 1284 if (q == 0 && USE_ERROR_CHECKING)
1396 { 1285 {
1397#if USE_ERROR_CHECKING
1398 SCHEME_V->no_memory = 1; 1286 SCHEME_V->no_memory = 1;
1399 return SCHEME_V->strbuff; 1287 return SCHEME_V->strbuff;
1400#endif
1401 } 1288 }
1402 1289
1403 if (str) 1290 if (str)
1404 { 1291 {
1405 int l = strlen (str); 1292 int l = strlen (str);
1406 1293
1407 if (l > len_str) 1294 if (l > len_str)
1408 l = len_str; 1295 l = len_str;
1409 1296
1410 memcpy (q, str, l + 1); 1297 memcpy (q, str, l);
1411 q[l + 1] = 0; 1298 q[l] = 0;
1412 } 1299 }
1413 else 1300 else
1414 { 1301 {
1415 memset (q, fill, len_str); 1302 memset (q, fill, len_str);
1416 q[len_str] = 0; 1303 q[len_str] = 0;
1417 } 1304 }
1418 1305
1419 return q; 1306 return q;
1420} 1307}
1421 1308
1422/* get new string */
1423INTERFACE pointer 1309INTERFACE pointer
1424mk_string (SCHEME_P_ const char *str) 1310mk_empty_string (SCHEME_P_ uint32_t len, char fill)
1425{ 1311{
1426 return mk_counted_string (SCHEME_A_ str, strlen (str)); 1312 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1313
1314 set_typeflag (x, T_STRING | T_ATOM);
1315 strvalue (x) = store_string (SCHEME_A_ len, 0, fill);
1316 strlength (x) = len;
1317 return x;
1427} 1318}
1428 1319
1429INTERFACE pointer 1320INTERFACE pointer
1430mk_counted_string (SCHEME_P_ const char *str, int len) 1321mk_counted_string (SCHEME_P_ const char *str, uint32_t len)
1431{ 1322{
1432 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1323 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1433 1324
1434 set_typeflag (x, T_STRING | T_ATOM); 1325 set_typeflag (x, T_STRING | T_ATOM);
1435 strvalue (x) = store_string (SCHEME_A_ len, str, 0); 1326 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1436 strlength (x) = len; 1327 strlength (x) = len;
1437 return x; 1328 return x;
1438} 1329}
1439 1330
1440INTERFACE pointer 1331INTERFACE pointer
1441mk_empty_string (SCHEME_P_ int len, char fill) 1332mk_string (SCHEME_P_ const char *str)
1442{ 1333{
1443 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1334 return mk_counted_string (SCHEME_A_ str, strlen (str));
1444
1445 set_typeflag (x, T_STRING | T_ATOM);
1446 strvalue (x) = store_string (SCHEME_A_ len, 0, fill);
1447 strlength (x) = len;
1448 return x;
1449} 1335}
1450 1336
1451INTERFACE pointer 1337INTERFACE pointer
1452mk_vector (SCHEME_P_ int len) 1338mk_vector (SCHEME_P_ uint32_t len)
1453{ 1339{
1454 return get_vector_object (SCHEME_A_ len, NIL); 1340 return get_vector_object (SCHEME_A_ len, NIL);
1455} 1341}
1456 1342
1457INTERFACE void 1343INTERFACE void
1458fill_vector (pointer vec, pointer obj) 1344fill_vector (pointer vec, pointer obj)
1459{ 1345{
1460 int i; 1346 int i;
1461 int num = ivalue (vec) / 2 + ivalue (vec) % 2;
1462 1347
1463 for (i = 0; i < num; i++) 1348 for (i = 0; i < vec->object.vector.length; i++)
1464 { 1349 vecvalue (vec)[i] = obj;
1465 set_typeflag (vec + 1 + i, T_PAIR);
1466 setimmutable (vec + 1 + i);
1467 set_car (vec + 1 + i, obj);
1468 set_cdr (vec + 1 + i, obj);
1469 }
1470} 1350}
1471 1351
1472INTERFACE pointer 1352INTERFACE pointer
1473vector_elem (pointer vec, int ielem) 1353vector_elem (pointer vec, uint32_t ielem)
1474{ 1354{
1475 int n = ielem / 2; 1355 return vecvalue(vec)[ielem];
1476
1477 if (ielem % 2 == 0)
1478 return car (vec + 1 + n);
1479 else
1480 return cdr (vec + 1 + n);
1481} 1356}
1482 1357
1483INTERFACE void 1358INTERFACE void
1484set_vector_elem (pointer vec, int ielem, pointer a) 1359set_vector_elem (pointer vec, uint32_t ielem, pointer a)
1485{ 1360{
1486 int n = ielem / 2; 1361 vecvalue(vec)[ielem] = a;
1487
1488 if (ielem % 2 == 0)
1489 set_car (vec + 1 + n, a);
1490 else
1491 set_cdr (vec + 1 + n, a);
1492} 1362}
1493 1363
1494/* get new symbol */ 1364/* get new symbol */
1495INTERFACE pointer 1365INTERFACE pointer
1496mk_symbol (SCHEME_P_ const char *name) 1366mk_symbol (SCHEME_P_ const char *name)
1497{ 1367{
1498 pointer x;
1499
1500 /* first check oblist */ 1368 /* first check oblist */
1501 x = oblist_find_by_name (SCHEME_A_ name); 1369 pointer x = oblist_find_by_name (SCHEME_A_ name);
1502 1370
1503 if (x != NIL) 1371 if (x == NIL)
1504 return x;
1505 else
1506 {
1507 x = oblist_add_by_name (SCHEME_A_ name); 1372 x = oblist_add_by_name (SCHEME_A_ name);
1373
1508 return x; 1374 return x;
1509 }
1510} 1375}
1511 1376
1512INTERFACE pointer 1377INTERFACE pointer
1513gensym (SCHEME_P) 1378gensym (SCHEME_P)
1514{ 1379{
1515 pointer x; 1380 pointer x;
1516 char name[40];
1517 1381
1518 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) 1382 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1519 { 1383 {
1520 strcpy (name, "gensym-"); 1384 char name[40] = "gensym-";
1521 xnum (name + 7, SCHEME_V->gensym_cnt); 1385 xnum (name + 7, SCHEME_V->gensym_cnt);
1522 1386
1523 /* first check oblist */ 1387 /* first check oblist */
1524 x = oblist_find_by_name (SCHEME_A_ name); 1388 x = oblist_find_by_name (SCHEME_A_ name);
1525 1389
1526 if (x != NIL) 1390 if (x == NIL)
1527 continue;
1528 else
1529 { 1391 {
1530 x = oblist_add_by_name (SCHEME_A_ name); 1392 x = oblist_add_by_name (SCHEME_A_ name);
1531 return x; 1393 return x;
1532 } 1394 }
1533 } 1395 }
1542 char c, *p; 1404 char c, *p;
1543 int has_dec_point = 0; 1405 int has_dec_point = 0;
1544 int has_fp_exp = 0; 1406 int has_fp_exp = 0;
1545 1407
1546#if USE_COLON_HOOK 1408#if USE_COLON_HOOK
1547
1548 if ((p = strstr (q, "::")) != 0) 1409 if ((p = strstr (q, "::")) != 0)
1549 { 1410 {
1550 *p = 0; 1411 *p = 0;
1551 return cons (SCHEME_V->COLON_HOOK, 1412 return cons (SCHEME_V->COLON_HOOK,
1552 cons (cons (SCHEME_V->QUOTE, 1413 cons (cons (SCHEME_V->QUOTE,
1553 cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL))); 1414 cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL)));
1554 } 1415 }
1555
1556#endif 1416#endif
1557 1417
1558 p = q; 1418 p = q;
1559 c = *p++; 1419 c = *p++;
1560 1420
1567 has_dec_point = 1; 1427 has_dec_point = 1;
1568 c = *p++; 1428 c = *p++;
1569 } 1429 }
1570 1430
1571 if (!isdigit (c)) 1431 if (!isdigit (c))
1572 {
1573 return mk_symbol (SCHEME_A_ strlwr (q)); 1432 return mk_symbol (SCHEME_A_ strlwr (q));
1574 }
1575 } 1433 }
1576 else if (c == '.') 1434 else if (c == '.')
1577 { 1435 {
1578 has_dec_point = 1; 1436 has_dec_point = 1;
1579 c = *p++; 1437 c = *p++;
1580 1438
1581 if (!isdigit (c)) 1439 if (!isdigit (c))
1582 {
1583 return mk_symbol (SCHEME_A_ strlwr (q)); 1440 return mk_symbol (SCHEME_A_ strlwr (q));
1584 }
1585 } 1441 }
1586 else if (!isdigit (c)) 1442 else if (!isdigit (c))
1587 {
1588 return mk_symbol (SCHEME_A_ strlwr (q)); 1443 return mk_symbol (SCHEME_A_ strlwr (q));
1589 }
1590 1444
1591 for (; (c = *p) != 0; ++p) 1445 for (; (c = *p) != 0; ++p)
1592 { 1446 {
1593 if (!isdigit (c)) 1447 if (!isdigit (c))
1594 { 1448 {
1607 has_dec_point = 1; /* decimal point illegal 1461 has_dec_point = 1; /* decimal point illegal
1608 from now on */ 1462 from now on */
1609 p++; 1463 p++;
1610 1464
1611 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1465 if ((*p == '-') || (*p == '+') || isdigit (*p))
1612 {
1613 continue; 1466 continue;
1614 }
1615 } 1467 }
1616 } 1468 }
1617 1469
1618 return mk_symbol (SCHEME_A_ strlwr (q)); 1470 return mk_symbol (SCHEME_A_ strlwr (q));
1619 } 1471 }
1620 } 1472 }
1621 1473
1622#if USE_FLOAT 1474#if USE_REAL
1623 if (has_dec_point) 1475 if (has_dec_point)
1624 return mk_real (SCHEME_A_ atof (q)); 1476 return mk_real (SCHEME_A_ atof (q));
1625#endif 1477#endif
1626 1478
1627 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1479 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1636 1488
1637 if (!strcmp (name, "t")) 1489 if (!strcmp (name, "t"))
1638 return S_T; 1490 return S_T;
1639 else if (!strcmp (name, "f")) 1491 else if (!strcmp (name, "f"))
1640 return S_F; 1492 return S_F;
1641 else if (*name == 'o') /* #o (octal) */
1642 {
1643 x = strtol (name + 1, 0, 8);
1644 return mk_integer (SCHEME_A_ x);
1645 }
1646 else if (*name == 'd') /* #d (decimal) */
1647 {
1648 x = strtol (name + 1, 0, 10);
1649 return mk_integer (SCHEME_A_ x);
1650 }
1651 else if (*name == 'x') /* #x (hex) */
1652 {
1653 x = strtol (name + 1, 0, 16);
1654 return mk_integer (SCHEME_A_ x);
1655 }
1656 else if (*name == 'b') /* #b (binary) */
1657 {
1658 x = binary_decode (name + 1);
1659 return mk_integer (SCHEME_A_ x);
1660 }
1661 else if (*name == '\\') /* #\w (character) */ 1493 else if (*name == '\\') /* #\w (character) */
1662 { 1494 {
1663 int c = 0; 1495 int c = 0;
1664 1496
1665 if (stricmp (name + 1, "space") == 0) 1497 if (stricmp (name + 1, "space") == 0)
1692 return NIL; 1524 return NIL;
1693 1525
1694 return mk_character (SCHEME_A_ c); 1526 return mk_character (SCHEME_A_ c);
1695 } 1527 }
1696 else 1528 else
1529 {
1530 /* identify base by string index */
1531 const char baseidx[17] = "ffbf" "ffff" "ofdf" "ffff" "x";
1532 char *base = strchr (baseidx, *name);
1533
1534 if (base)
1535 return mk_integer (SCHEME_A_ strtol (name + 1, 0, base - baseidx));
1536
1697 return NIL; 1537 return NIL;
1538 }
1698} 1539}
1699 1540
1700/* ========== garbage collector ========== */ 1541/* ========== garbage collector ========== */
1701 1542
1702/*-- 1543/*--
1707static void 1548static void
1708mark (pointer a) 1549mark (pointer a)
1709{ 1550{
1710 pointer t, q, p; 1551 pointer t, q, p;
1711 1552
1712 t = (pointer) 0; 1553 t = 0;
1713 p = a; 1554 p = a;
1714E2: 1555E2:
1715 setmark (p); 1556 setmark (p);
1716 printf ("mark %p %x\n", p, p->flag);//D
1717 1557
1718 if (is_vector (p)) 1558 if (is_vector (p))
1719 { 1559 {
1720 int i; 1560 int i;
1721 int num = ivalue_unchecked (p) / 2 + ivalue_unchecked (p) % 2;
1722 1561
1723 for (i = 0; i < num; i++) 1562 for (i = 0; i < p->object.vector.length; i++)
1724 { 1563 mark (vecvalue (p)[i]);
1725 /* Vector cells will be treated like ordinary cells */
1726 mark (p + 1 + i);
1727 }
1728 } 1564 }
1729 1565
1730 if (is_atom (p)) 1566 if (is_atom (p))
1731 goto E6; 1567 goto E6;
1732 1568
1742 goto E2; 1578 goto E2;
1743 } 1579 }
1744 1580
1745E5: 1581E5:
1746 q = cdr (p); /* down cdr */ 1582 q = cdr (p); /* down cdr */
1747 printf ("mark+ %p\n", q, q->flag);//D
1748 1583
1749 if (q && !is_mark (q)) 1584 if (q && !is_mark (q))
1750 { 1585 {
1751 set_cdr (p, t); 1586 set_cdr (p, t);
1752 t = p; 1587 t = p;
1753 p = q; 1588 p = q;
1754 goto E2; 1589 goto E2;
1755 } 1590 }
1756 1591
1757E6: /* up. Undo the link switching from steps E4 and E5. */ 1592E6: /* up. Undo the link switching from steps E4 and E5. */
1758
1759 if (!t) 1593 if (!t)
1760 return; 1594 return;
1761 1595
1762 q = t; 1596 q = t;
1763 1597
1831 if (is_mark (p)) 1665 if (is_mark (p))
1832 clrmark (p); 1666 clrmark (p);
1833 else 1667 else
1834 { 1668 {
1835 /* reclaim cell */ 1669 /* reclaim cell */
1836 if (typeflag (p) != 0) 1670 if (typeflag (p) != T_FREE)
1837 { 1671 {
1838 finalize_cell (SCHEME_A_ p); 1672 finalize_cell (SCHEME_A_ p);
1839 set_typeflag (p, 0); 1673 set_typeflag (p, T_FREE);
1840 set_car (p, NIL); 1674 set_car (p, NIL);
1841 } 1675 }
1842 1676
1843 ++SCHEME_V->fcells; 1677 ++SCHEME_V->fcells;
1844 set_cdr (p, SCHEME_V->free_cell); 1678 set_cdr (p, SCHEME_V->free_cell);
1854static void 1688static void
1855finalize_cell (SCHEME_P_ pointer a) 1689finalize_cell (SCHEME_P_ pointer a)
1856{ 1690{
1857 if (is_string (a)) 1691 if (is_string (a))
1858 free (strvalue (a)); 1692 free (strvalue (a));
1693 else if (is_vector (a))
1694 free (vecvalue (a));
1859#if USE_PORTS 1695#if USE_PORTS
1860 else if (is_port (a)) 1696 else if (is_port (a))
1861 { 1697 {
1862 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) 1698 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit)
1863 port_close (SCHEME_A_ a, port_input | port_output); 1699 port_close (SCHEME_A_ a, port_input | port_output);
2289 char *p = SCHEME_V->strbuff; 2125 char *p = SCHEME_V->strbuff;
2290 2126
2291 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2127 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2292 2128
2293 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2129 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2294 {
2295 *p = 0; 2130 *p = 0;
2296 }
2297 else 2131 else
2298 { 2132 {
2299 backchar (SCHEME_A_ p[-1]); 2133 backchar (SCHEME_A_ p[-1]);
2300 *--p = '\0'; 2134 *--p = '\0';
2301 } 2135 }
2316 for (;;) 2150 for (;;)
2317 { 2151 {
2318 c = inchar (SCHEME_A); 2152 c = inchar (SCHEME_A);
2319 2153
2320 if (c == EOF || p - SCHEME_V->strbuff > sizeof (SCHEME_V->strbuff) - 1) 2154 if (c == EOF || p - SCHEME_V->strbuff > sizeof (SCHEME_V->strbuff) - 1)
2321 {
2322 return S_F; 2155 return S_F;
2323 }
2324 2156
2325 switch (state) 2157 switch (state)
2326 { 2158 {
2327 case st_ok: 2159 case st_ok:
2328 switch (c) 2160 switch (c)
2396 c = toupper (c); 2228 c = toupper (c);
2397 2229
2398 if (c >= '0' && c <= 'F') 2230 if (c >= '0' && c <= 'F')
2399 { 2231 {
2400 if (c <= '9') 2232 if (c <= '9')
2401 {
2402 c1 = (c1 << 4) + c - '0'; 2233 c1 = (c1 << 4) + c - '0';
2403 }
2404 else 2234 else
2405 {
2406 c1 = (c1 << 4) + c - 'A' + 10; 2235 c1 = (c1 << 4) + c - 'A' + 10;
2407 }
2408 2236
2409 if (state == st_x1) 2237 if (state == st_x1)
2410 {
2411 state = st_x2; 2238 state = st_x2;
2412 }
2413 else 2239 else
2414 { 2240 {
2415 *p++ = c1; 2241 *p++ = c1;
2416 state = st_ok; 2242 state = st_ok;
2417 } 2243 }
2418 } 2244 }
2419 else 2245 else
2420 {
2421 return S_F; 2246 return S_F;
2422 }
2423 2247
2424 break; 2248 break;
2425 2249
2426 case st_oct1: 2250 case st_oct1:
2427 case st_oct2: 2251 case st_oct2:
2458is_one_of (char *s, int c) 2282is_one_of (char *s, int c)
2459{ 2283{
2460 if (c == EOF) 2284 if (c == EOF)
2461 return 1; 2285 return 1;
2462 2286
2463 while (*s) 2287 return !!strchr (s, c);
2464 if (*s++ == c)
2465 return 1;
2466
2467 return 0;
2468} 2288}
2469 2289
2470/* skip white characters */ 2290/* skip white characters */
2471static INLINE int 2291static INLINE int
2472skipspace (SCHEME_P) 2292skipspace (SCHEME_P)
2477 { 2297 {
2478 c = inchar (SCHEME_A); 2298 c = inchar (SCHEME_A);
2479#if SHOW_ERROR_LINE 2299#if SHOW_ERROR_LINE
2480 if (c == '\n') 2300 if (c == '\n')
2481 curr_line++; 2301 curr_line++;
2482
2483#endif 2302#endif
2484 } 2303 }
2485 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2304 while (c == ' ' || c == '\n' || c == '\r' || c == '\t');
2486 2305
2487 /* record it */ 2306 /* record it */
2524 2343
2525 if (is_one_of (" \n\t", c)) 2344 if (is_one_of (" \n\t", c))
2526 return TOK_DOT; 2345 return TOK_DOT;
2527 else 2346 else
2528 { 2347 {
2348 //TODO: ungetc twice in a row is not supported in C
2529 backchar (SCHEME_A_ c); 2349 backchar (SCHEME_A_ c);
2530 backchar (SCHEME_A_ '.'); 2350 backchar (SCHEME_A_ '.');
2531 return TOK_ATOM; 2351 return TOK_ATOM;
2532 } 2352 }
2533 2353
2647 int d = *s / 16; 2467 int d = *s / 16;
2648 2468
2649 putcharacter (SCHEME_A_ 'x'); 2469 putcharacter (SCHEME_A_ 'x');
2650 2470
2651 if (d < 10) 2471 if (d < 10)
2652 {
2653 putcharacter (SCHEME_A_ d + '0'); 2472 putcharacter (SCHEME_A_ d + '0');
2654 }
2655 else 2473 else
2656 {
2657 putcharacter (SCHEME_A_ d - 10 + 'A'); 2474 putcharacter (SCHEME_A_ d - 10 + 'A');
2658 }
2659 2475
2660 d = *s % 16; 2476 d = *s % 16;
2661 2477
2662 if (d < 10) 2478 if (d < 10)
2663 {
2664 putcharacter (SCHEME_A_ d + '0'); 2479 putcharacter (SCHEME_A_ d + '0');
2665 }
2666 else 2480 else
2667 {
2668 putcharacter (SCHEME_A_ d - 10 + 'A'); 2481 putcharacter (SCHEME_A_ d - 10 + 'A');
2669 }
2670 } 2482 }
2671 } 2483 }
2672 } 2484 }
2673 else 2485 else
2674 {
2675 putcharacter (SCHEME_A_ * s); 2486 putcharacter (SCHEME_A_ * s);
2676 }
2677 2487
2678 s++; 2488 s++;
2679 } 2489 }
2680 2490
2681 putcharacter (SCHEME_A_ '"'); 2491 putcharacter (SCHEME_A_ '"');
2716 2526
2717 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2527 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2718 { 2528 {
2719 if (num_is_integer (l)) 2529 if (num_is_integer (l))
2720 xnum (p, ivalue_unchecked (l)); 2530 xnum (p, ivalue_unchecked (l));
2721#if USE_FLOAT 2531#if USE_REAL
2722 else 2532 else
2723 { 2533 {
2724 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2534 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2725 /* r5rs says there must be a '.' (unless 'e'?) */ 2535 /* r5rs says there must be a '.' (unless 'e'?) */
2726 f = strcspn (p, ".e"); 2536 f = strcspn (p, ".e");
2898list_star (SCHEME_P_ pointer d) 2708list_star (SCHEME_P_ pointer d)
2899{ 2709{
2900 pointer p, q; 2710 pointer p, q;
2901 2711
2902 if (cdr (d) == NIL) 2712 if (cdr (d) == NIL)
2903 {
2904 return car (d); 2713 return car (d);
2905 }
2906 2714
2907 p = cons (car (d), cdr (d)); 2715 p = cons (car (d), cdr (d));
2908 q = p; 2716 q = p;
2909 2717
2910 while (cdr (cdr (p)) != NIL) 2718 while (cdr (cdr (p)) != NIL)
2911 { 2719 {
2912 d = cons (car (p), cdr (p)); 2720 d = cons (car (p), cdr (p));
2913 2721
2914 if (cdr (cdr (p)) != NIL) 2722 if (cdr (cdr (p)) != NIL)
2915 {
2916 p = cdr (d); 2723 p = cdr (d);
2917 }
2918 } 2724 }
2919 2725
2920 set_cdr (p, car (cdr (p))); 2726 set_cdr (p, car (cdr (p)));
2921 return q; 2727 return q;
2922} 2728}
2936 2742
2937/* reverse list --- in-place */ 2743/* reverse list --- in-place */
2938static pointer 2744static pointer
2939reverse_in_place (SCHEME_P_ pointer term, pointer list) 2745reverse_in_place (SCHEME_P_ pointer term, pointer list)
2940{ 2746{
2941 pointer p = list, result = term, q; 2747 pointer result = term;
2748 pointer p = list;
2942 2749
2943 while (p != NIL) 2750 while (p != NIL)
2944 { 2751 {
2945 q = cdr (p); 2752 pointer q = cdr (p);
2946 set_cdr (p, result); 2753 set_cdr (p, result);
2947 result = p; 2754 result = p;
2948 p = q; 2755 p = q;
2949 } 2756 }
2950 2757
3025#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) 2832#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST)
3026 2833
3027static int 2834static int
3028hash_fn (const char *key, int table_size) 2835hash_fn (const char *key, int table_size)
3029{ 2836{
3030 unsigned int hashed = 0; 2837 const unsigned char *p = key;
3031 const char *c; 2838 uint32_t hash = 2166136261;
3032 int bits_per_int = sizeof (unsigned int) * 8;
3033 2839
3034 for (c = key; *c; c++) 2840 while (*p)
3035 { 2841 hash = (hash ^ *p++) * 16777619;
3036 /* letters have about 5 bits in them */
3037 hashed = (hashed << 5) | (hashed >> (bits_per_int - 5));
3038 hashed ^= *c;
3039 }
3040 2842
3041 return hashed % table_size; 2843 return hash % table_size;
3042} 2844}
3043#endif 2845#endif
3044 2846
3045#ifndef USE_ALIST_ENV 2847#ifndef USE_ALIST_ENV
3046 2848
3072{ 2874{
3073 pointer slot = immutable_cons (variable, value); 2875 pointer slot = immutable_cons (variable, value);
3074 2876
3075 if (is_vector (car (env))) 2877 if (is_vector (car (env)))
3076 { 2878 {
3077 int location = hash_fn (symname (variable), ivalue_unchecked (car (env))); 2879 int location = hash_fn (symname (variable), veclength (car (env)));
3078 2880
3079 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2881 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location)));
3080 } 2882 }
3081 else 2883 else
3082 set_car (env, immutable_cons (slot, car (env))); 2884 set_car (env, immutable_cons (slot, car (env)));
3090 2892
3091 for (x = env; x != NIL; x = cdr (x)) 2893 for (x = env; x != NIL; x = cdr (x))
3092 { 2894 {
3093 if (is_vector (car (x))) 2895 if (is_vector (car (x)))
3094 { 2896 {
3095 location = hash_fn (symname (hdl), ivalue_unchecked (car (x))); 2897 location = hash_fn (symname (hdl), veclength (car (x)));
3096 y = vector_elem (car (x), location); 2898 y = vector_elem (car (x), location);
3097 } 2899 }
3098 else 2900 else
3099 y = car (x); 2901 y = car (x);
3100 2902
3210#if USE_ERROR_HOOK 3012#if USE_ERROR_HOOK
3211 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); 3013 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1);
3212 3014
3213 if (x != NIL) 3015 if (x != NIL)
3214 { 3016 {
3215 if (a) 3017 pointer code = a
3216 SCHEME_V->code = cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL); 3018 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL)
3217 else 3019 : NIL;
3218 SCHEME_V->code = NIL;
3219 3020
3220 SCHEME_V->code = cons (mk_string (SCHEME_A_ s), SCHEME_V->code); 3021 code = cons (mk_string (SCHEME_A_ s), code);
3221 setimmutable (car (SCHEME_V->code)); 3022 setimmutable (car (code));
3222 SCHEME_V->code = cons (slot_value_in_env (x), SCHEME_V->code); 3023 SCHEME_V->code = cons (slot_value_in_env (x), code);
3223 SCHEME_V->op = OP_EVAL; 3024 SCHEME_V->op = OP_EVAL;
3224 3025
3225 return S_T; 3026 return S_T;
3226 } 3027 }
3227#endif 3028#endif
3231 else 3032 else
3232 SCHEME_V->args = NIL; 3033 SCHEME_V->args = NIL;
3233 3034
3234 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); 3035 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args);
3235 setimmutable (car (SCHEME_V->args)); 3036 setimmutable (car (SCHEME_V->args));
3236 SCHEME_V->op = (int)OP_ERR0; 3037 SCHEME_V->op = OP_ERR0;
3237 return S_T; 3038 return S_T;
3238} 3039}
3239 3040
3240#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) 3041#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a)
3241#define Error_0(s) Error_1 (s, 0) 3042#define Error_0(s) Error_1 (s, 0)
3242 3043
3243/* Too small to turn into function */ 3044/* Too small to turn into function */
3244#define BEGIN do { 3045#define BEGIN do {
3245#define END } while (0) 3046#define END } while (0)
3246#define s_goto(a) BEGIN \ 3047#define s_goto(a) BEGIN \
3247 SCHEME_V->op = (int)(a); \ 3048 SCHEME_V->op = a; \
3248 return S_T; END 3049 return S_T; END
3249 3050
3250#define s_return(a) return xs_return(SCHEME_A_ a) 3051#define s_return(a) return xs_return (SCHEME_A_ a)
3251 3052
3252#ifndef USE_SCHEME_STACK 3053#ifndef USE_SCHEME_STACK
3253 3054
3254/* this structure holds all the interpreter's registers */ 3055/* this structure holds all the interpreter's registers */
3255struct dump_stack_frame 3056struct dump_stack_frame
3274 SCHEME_V->dump_size += STACK_GROWTH; 3075 SCHEME_V->dump_size += STACK_GROWTH;
3275 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); 3076 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size);
3276 } 3077 }
3277 3078
3278 next_frame = SCHEME_V->dump_base + nframes; 3079 next_frame = SCHEME_V->dump_base + nframes;
3080
3279 next_frame->op = op; 3081 next_frame->op = op;
3280 next_frame->args = args; 3082 next_frame->args = args;
3281 next_frame->envir = SCHEME_V->envir; 3083 next_frame->envir = SCHEME_V->envir;
3282 next_frame->code = code; 3084 next_frame->code = code;
3085
3283 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3086 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3284} 3087}
3285 3088
3286static pointer 3089static pointer
3287xs_return (SCHEME_P_ pointer a) 3090xs_return (SCHEME_P_ pointer a)
3292 SCHEME_V->value = a; 3095 SCHEME_V->value = a;
3293 3096
3294 if (nframes <= 0) 3097 if (nframes <= 0)
3295 return NIL; 3098 return NIL;
3296 3099
3297 nframes--;
3298 frame = SCHEME_V->dump_base + nframes; 3100 frame = &SCHEME_V->dump_base[--nframes];
3299 SCHEME_V->op = frame->op; 3101 SCHEME_V->op = frame->op;
3300 SCHEME_V->args = frame->args; 3102 SCHEME_V->args = frame->args;
3301 SCHEME_V->envir = frame->envir; 3103 SCHEME_V->envir = frame->envir;
3302 SCHEME_V->code = frame->code; 3104 SCHEME_V->code = frame->code;
3303 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3105 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3304 3106
3305 return S_T; 3107 return S_T;
3306} 3108}
3307 3109
3308static INLINE void 3110static INLINE void
3309dump_stack_reset (SCHEME_P) 3111dump_stack_reset (SCHEME_P)
3310{ 3112{
3311 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3113 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3312 SCHEME_V->dump = (pointer)0; 3114 SCHEME_V->dump = (pointer)+0;
3313} 3115}
3314 3116
3315static INLINE void 3117static INLINE void
3316dump_stack_initialize (SCHEME_P) 3118dump_stack_initialize (SCHEME_P)
3317{ 3119{
3318 SCHEME_V->dump_size = 0; 3120 SCHEME_V->dump_size = 0;
3319 SCHEME_V->dump_base = NULL; 3121 SCHEME_V->dump_base = 0;
3320 dump_stack_reset (SCHEME_A); 3122 dump_stack_reset (SCHEME_A);
3321} 3123}
3322 3124
3323static void 3125static void
3324dump_stack_free (SCHEME_P) 3126dump_stack_free (SCHEME_P)
3325{ 3127{
3326 free (SCHEME_V->dump_base); 3128 free (SCHEME_V->dump_base);
3327 SCHEME_V->dump_base = NULL; 3129 SCHEME_V->dump_base = 0;
3328 SCHEME_V->dump = (pointer)0; 3130 SCHEME_V->dump = (pointer)0;
3329 SCHEME_V->dump_size = 0; 3131 SCHEME_V->dump_size = 0;
3330} 3132}
3331 3133
3332static void 3134static void
3374 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3176 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3375 3177
3376 while (cont != NIL) 3178 while (cont != NIL)
3377 { 3179 {
3378 frame->op = ivalue (car (cont)); cont = cdr (cont); 3180 frame->op = ivalue (car (cont)); cont = cdr (cont);
3379 frame->args = car (cont) ; cont = cdr (cont); 3181 frame->args = car (cont) ; cont = cdr (cont);
3380 frame->envir = car (cont) ; cont = cdr (cont); 3182 frame->envir = car (cont) ; cont = cdr (cont);
3381 frame->code = car (cont) ; cont = cdr (cont); 3183 frame->code = car (cont) ; cont = cdr (cont);
3382 3184
3383 ++frame; 3185 ++frame;
3384 ++i; 3186 ++i;
3385 } 3187 }
3386 3188
3415 SCHEME_V->value = a; 3217 SCHEME_V->value = a;
3416 3218
3417 if (dump == NIL) 3219 if (dump == NIL)
3418 return NIL; 3220 return NIL;
3419 3221
3420 SCHEME_V->op = ivalue (car (dump)); 3222 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump);
3421 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3223 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3422 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3224 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3423 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3225 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3424 3226
3425 SCHEME_V->dump = dump; 3227 SCHEME_V->dump = dump;
3426 3228
3427 return S_T; 3229 return S_T;
3428} 3230}
3517 s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL); 3319 s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL);
3518 s_save (SCHEME_A_ OP_T1LVL, NIL, NIL); 3320 s_save (SCHEME_A_ OP_T1LVL, NIL, NIL);
3519 s_goto (OP_READ_INTERNAL); 3321 s_goto (OP_READ_INTERNAL);
3520 3322
3521 case OP_T1LVL: /* top level */ 3323 case OP_T1LVL: /* top level */
3522 SCHEME_V->code = SCHEME_V->value; 3324 SCHEME_V->code = SCHEME_V->value;
3523 SCHEME_V->inport = SCHEME_V->save_inport; 3325 SCHEME_V->inport = SCHEME_V->save_inport;
3524 s_goto (OP_EVAL); 3326 s_goto (OP_EVAL);
3525 3327
3526 case OP_READ_INTERNAL: /* internal read */ 3328 case OP_READ_INTERNAL: /* internal read */
3527 SCHEME_V->tok = token (SCHEME_A); 3329 SCHEME_V->tok = token (SCHEME_A);
3528 3330
3529 if (SCHEME_V->tok == TOK_EOF) 3331 if (SCHEME_V->tok == TOK_EOF)
3530 {
3531 s_return (S_EOF); 3332 s_return (S_EOF);
3532 }
3533 3333
3534 s_goto (OP_RDSEXPR); 3334 s_goto (OP_RDSEXPR);
3535 3335
3536 case OP_GENSYM: 3336 case OP_GENSYM:
3537 s_return (gensym (SCHEME_A)); 3337 s_return (gensym (SCHEME_A));
3541 /* OP_VALUEPRINT is always pushed, because when changing from 3341 /* OP_VALUEPRINT is always pushed, because when changing from
3542 non-interactive to interactive mode, it needs to be 3342 non-interactive to interactive mode, it needs to be
3543 already on the stack */ 3343 already on the stack */
3544#if USE_TRACING 3344#if USE_TRACING
3545 if (SCHEME_V->tracing) 3345 if (SCHEME_V->tracing)
3546 {
3547 putstr (SCHEME_A_ "\nGives: "); 3346 putstr (SCHEME_A_ "\nGives: ");
3548 }
3549#endif 3347#endif
3550 3348
3551 if (file_interactive (SCHEME_A)) 3349 if (file_interactive (SCHEME_A))
3552 { 3350 {
3553 SCHEME_V->print_flag = 1; 3351 SCHEME_V->print_flag = 1;
3554 SCHEME_V->args = SCHEME_V->value; 3352 SCHEME_V->args = SCHEME_V->value;
3555 s_goto (OP_P0LIST); 3353 s_goto (OP_P0LIST);
3556 } 3354 }
3557 else 3355 else
3558 {
3559 s_return (SCHEME_V->value); 3356 s_return (SCHEME_V->value);
3560 }
3561 3357
3562 case OP_EVAL: /* main part of evaluation */ 3358 case OP_EVAL: /* main part of evaluation */
3563#if USE_TRACING 3359#if USE_TRACING
3564 if (SCHEME_V->tracing) 3360 if (SCHEME_V->tracing)
3565 { 3361 {
3569 putstr (SCHEME_A_ "\nEval: "); 3365 putstr (SCHEME_A_ "\nEval: ");
3570 s_goto (OP_P0LIST); 3366 s_goto (OP_P0LIST);
3571 } 3367 }
3572 3368
3573 /* fall through */ 3369 /* fall through */
3370
3574 case OP_REAL_EVAL: 3371 case OP_REAL_EVAL:
3575#endif 3372#endif
3576 if (is_symbol (SCHEME_V->code)) /* symbol */ 3373 if (is_symbol (SCHEME_V->code)) /* symbol */
3577 { 3374 {
3578 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); 3375 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1);
3582 else 3379 else
3583 Error_1 ("eval: unbound variable:", SCHEME_V->code); 3380 Error_1 ("eval: unbound variable:", SCHEME_V->code);
3584 } 3381 }
3585 else if (is_pair (SCHEME_V->code)) 3382 else if (is_pair (SCHEME_V->code))
3586 { 3383 {
3384 x = car (SCHEME_V->code);
3385
3587 if (is_syntax (x = car (SCHEME_V->code))) /* SYNTAX */ 3386 if (is_syntax (x)) /* SYNTAX */
3588 { 3387 {
3589 SCHEME_V->code = cdr (SCHEME_V->code); 3388 SCHEME_V->code = cdr (SCHEME_V->code);
3590 s_goto (syntaxnum (x)); 3389 s_goto (syntaxnum (x));
3591 } 3390 }
3592 else /* first, eval top element and eval arguments */ 3391 else /* first, eval top element and eval arguments */
3593 { 3392 {
3594 s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code); 3393 s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code);
3595 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ 3394 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */
3596 SCHEME_V->code = car (SCHEME_V->code); 3395 SCHEME_V->code = x;
3597 s_goto (OP_EVAL); 3396 s_goto (OP_EVAL);
3598 } 3397 }
3599 } 3398 }
3600 else 3399 else
3601 s_return (SCHEME_V->code); 3400 s_return (SCHEME_V->code);
3654 putstr (SCHEME_A_ "\nApply to: "); 3453 putstr (SCHEME_A_ "\nApply to: ");
3655 s_goto (OP_P0LIST); 3454 s_goto (OP_P0LIST);
3656 } 3455 }
3657 3456
3658 /* fall through */ 3457 /* fall through */
3458
3659 case OP_REAL_APPLY: 3459 case OP_REAL_APPLY:
3660#endif 3460#endif
3661 if (is_proc (SCHEME_V->code)) 3461 if (is_proc (SCHEME_V->code))
3662 { 3462 {
3663 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3463 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3677 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); 3477 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code));
3678 3478
3679 for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) 3479 for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y))
3680 { 3480 {
3681 if (y == NIL) 3481 if (y == NIL)
3682 {
3683 Error_0 ("not enough arguments"); 3482 Error_0 ("not enough arguments");
3684 }
3685 else 3483 else
3686 {
3687 new_slot_in_env (SCHEME_A_ car (x), car (y)); 3484 new_slot_in_env (SCHEME_A_ car (x), car (y));
3688 }
3689 } 3485 }
3690 3486
3691 if (x == NIL) 3487 if (x == NIL)
3692 { 3488 {
3693
3694 /*-- 3489 /*--
3695 * if (y != NIL) { 3490 * if (y != NIL) {
3696 * Error_0("too many arguments"); 3491 * Error_0("too many arguments");
3697 * } 3492 * }
3698 */ 3493 */
3699 } 3494 }
3700 else if (is_symbol (x)) 3495 else if (is_symbol (x))
3701 new_slot_in_env (SCHEME_A_ x, y); 3496 new_slot_in_env (SCHEME_A_ x, y);
3702 else 3497 else
3703 {
3704 Error_1 ("syntax error in closure: not a symbol:", x); 3498 Error_1 ("syntax error in closure: not a symbol:", x);
3705 }
3706 3499
3707 SCHEME_V->code = cdr (closure_code (SCHEME_V->code)); 3500 SCHEME_V->code = cdr (closure_code (SCHEME_V->code));
3708 SCHEME_V->args = NIL; 3501 SCHEME_V->args = NIL;
3709 s_goto (OP_BEGIN); 3502 s_goto (OP_BEGIN);
3710 } 3503 }
3780 x = car (SCHEME_V->code); 3573 x = car (SCHEME_V->code);
3781 SCHEME_V->code = cadr (SCHEME_V->code); 3574 SCHEME_V->code = cadr (SCHEME_V->code);
3782 } 3575 }
3783 3576
3784 if (!is_symbol (x)) 3577 if (!is_symbol (x))
3785 {
3786 Error_0 ("variable is not a symbol"); 3578 Error_0 ("variable is not a symbol");
3787 }
3788 3579
3789 s_save (SCHEME_A_ OP_DEF1, NIL, x); 3580 s_save (SCHEME_A_ OP_DEF1, NIL, x);
3790 s_goto (OP_EVAL); 3581 s_goto (OP_EVAL);
3791 3582
3792 case OP_DEF1: /* define */ 3583 case OP_DEF1: /* define */
3793 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); 3584 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0);
3794 3585
3795 if (x != NIL) 3586 if (x != NIL)
3796 {
3797 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); 3587 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value);
3798 }
3799 else 3588 else
3800 {
3801 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); 3589 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value);
3802 }
3803 3590
3804 s_return (SCHEME_V->code); 3591 s_return (SCHEME_V->code);
3805 3592
3806 3593
3807 case OP_DEFP: /* defined? */ 3594 case OP_DEFP: /* defined? */
3808 x = SCHEME_V->envir; 3595 x = SCHEME_V->envir;
3809 3596
3810 if (cdr (SCHEME_V->args) != NIL) 3597 if (cdr (SCHEME_V->args) != NIL)
3811 {
3812 x = cadr (SCHEME_V->args); 3598 x = cadr (SCHEME_V->args);
3813 }
3814 3599
3815 s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); 3600 s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL);
3816 3601
3817 case OP_SET0: /* set! */ 3602 case OP_SET0: /* set! */
3818 if (is_immutable (car (SCHEME_V->code))) 3603 if (is_immutable (car (SCHEME_V->code)))
3829 { 3614 {
3830 set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); 3615 set_slot_in_env (SCHEME_A_ y, SCHEME_V->value);
3831 s_return (SCHEME_V->value); 3616 s_return (SCHEME_V->value);
3832 } 3617 }
3833 else 3618 else
3834 {
3835 Error_1 ("set!: unbound variable:", SCHEME_V->code); 3619 Error_1 ("set!: unbound variable:", SCHEME_V->code);
3836 }
3837 3620
3838 3621
3839 case OP_BEGIN: /* begin */ 3622 case OP_BEGIN: /* begin */
3840 if (!is_pair (SCHEME_V->code)) 3623 if (!is_pair (SCHEME_V->code))
3841 {
3842 s_return (SCHEME_V->code); 3624 s_return (SCHEME_V->code);
3843 }
3844 3625
3845 if (cdr (SCHEME_V->code) != NIL) 3626 if (cdr (SCHEME_V->code) != NIL)
3846 {
3847 s_save (SCHEME_A_ OP_BEGIN, NIL, cdr (SCHEME_V->code)); 3627 s_save (SCHEME_A_ OP_BEGIN, NIL, cdr (SCHEME_V->code));
3848 }
3849 3628
3850 SCHEME_V->code = car (SCHEME_V->code); 3629 SCHEME_V->code = car (SCHEME_V->code);
3851 s_goto (OP_EVAL); 3630 s_goto (OP_EVAL);
3852 3631
3853 case OP_IF0: /* if */ 3632 case OP_IF0: /* if */
3874 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3653 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args);
3875 3654
3876 if (is_pair (SCHEME_V->code)) /* continue */ 3655 if (is_pair (SCHEME_V->code)) /* continue */
3877 { 3656 {
3878 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3657 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3879 {
3880 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); 3658 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code));
3881 }
3882 3659
3883 s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); 3660 s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code));
3884 SCHEME_V->code = cadar (SCHEME_V->code); 3661 SCHEME_V->code = cadar (SCHEME_V->code);
3885 SCHEME_V->args = NIL; 3662 SCHEME_V->args = NIL;
3886 s_goto (OP_EVAL); 3663 s_goto (OP_EVAL);
3896 case OP_LET2: /* let */ 3673 case OP_LET2: /* let */
3897 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 3674 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3898 3675
3899 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; 3676 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args;
3900 y != NIL; x = cdr (x), y = cdr (y)) 3677 y != NIL; x = cdr (x), y = cdr (y))
3901 {
3902 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3678 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3903 }
3904 3679
3905 if (is_symbol (car (SCHEME_V->code))) /* named let */ 3680 if (is_symbol (car (SCHEME_V->code))) /* named let */
3906 { 3681 {
3907 for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) 3682 for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x))
3908 { 3683 {
3978 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3753 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args);
3979 3754
3980 if (is_pair (SCHEME_V->code)) /* continue */ 3755 if (is_pair (SCHEME_V->code)) /* continue */
3981 { 3756 {
3982 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3757 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3983 {
3984 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); 3758 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3985 }
3986 3759
3987 s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code)); 3760 s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code));
3988 SCHEME_V->code = cadar (SCHEME_V->code); 3761 SCHEME_V->code = cadar (SCHEME_V->code);
3989 SCHEME_V->args = NIL; 3762 SCHEME_V->args = NIL;
3990 s_goto (OP_EVAL); 3763 s_goto (OP_EVAL);
3997 s_goto (OP_LET2REC); 3770 s_goto (OP_LET2REC);
3998 } 3771 }
3999 3772
4000 case OP_LET2REC: /* letrec */ 3773 case OP_LET2REC: /* letrec */
4001 for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y)) 3774 for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y))
4002 {
4003 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3775 new_slot_in_env (SCHEME_A_ caar (x), car (y));
4004 }
4005 3776
4006 SCHEME_V->code = cdr (SCHEME_V->code); 3777 SCHEME_V->code = cdr (SCHEME_V->code);
4007 SCHEME_V->args = NIL; 3778 SCHEME_V->args = NIL;
4008 s_goto (OP_BEGIN); 3779 s_goto (OP_BEGIN);
4009 3780
4010 case OP_COND0: /* cond */ 3781 case OP_COND0: /* cond */
4011 if (!is_pair (SCHEME_V->code)) 3782 if (!is_pair (SCHEME_V->code))
4012 {
4013 Error_0 ("syntax error in cond"); 3783 Error_0 ("syntax error in cond");
4014 }
4015 3784
4016 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 3785 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
4017 SCHEME_V->code = caar (SCHEME_V->code); 3786 SCHEME_V->code = caar (SCHEME_V->code);
4018 s_goto (OP_EVAL); 3787 s_goto (OP_EVAL);
4019 3788
4020 case OP_COND1: /* cond */ 3789 case OP_COND1: /* cond */
4021 if (is_true (SCHEME_V->value)) 3790 if (is_true (SCHEME_V->value))
4022 { 3791 {
4023 if ((SCHEME_V->code = cdar (SCHEME_V->code)) == NIL) 3792 if ((SCHEME_V->code = cdar (SCHEME_V->code)) == NIL)
4024 {
4025 s_return (SCHEME_V->value); 3793 s_return (SCHEME_V->value);
4026 }
4027 3794
4028 if (car (SCHEME_V->code) == SCHEME_V->FEED_TO) 3795 if (car (SCHEME_V->code) == SCHEME_V->FEED_TO)
4029 { 3796 {
4030 if (!is_pair (cdr (SCHEME_V->code))) 3797 if (!is_pair (cdr (SCHEME_V->code)))
4031 {
4032 Error_0 ("syntax error in cond"); 3798 Error_0 ("syntax error in cond");
4033 }
4034 3799
4035 x = cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)); 3800 x = cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL));
4036 SCHEME_V->code = cons (cadr (SCHEME_V->code), cons (x, NIL)); 3801 SCHEME_V->code = cons (cadr (SCHEME_V->code), cons (x, NIL));
4037 s_goto (OP_EVAL); 3802 s_goto (OP_EVAL);
4038 } 3803 }
4040 s_goto (OP_BEGIN); 3805 s_goto (OP_BEGIN);
4041 } 3806 }
4042 else 3807 else
4043 { 3808 {
4044 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) 3809 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL)
4045 {
4046 s_return (NIL); 3810 s_return (NIL);
4047 }
4048 else 3811 else
4049 { 3812 {
4050 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 3813 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
4051 SCHEME_V->code = caar (SCHEME_V->code); 3814 SCHEME_V->code = caar (SCHEME_V->code);
4052 s_goto (OP_EVAL); 3815 s_goto (OP_EVAL);
4058 set_typeflag (x, T_PROMISE); 3821 set_typeflag (x, T_PROMISE);
4059 s_return (x); 3822 s_return (x);
4060 3823
4061 case OP_AND0: /* and */ 3824 case OP_AND0: /* and */
4062 if (SCHEME_V->code == NIL) 3825 if (SCHEME_V->code == NIL)
4063 {
4064 s_return (S_T); 3826 s_return (S_T);
4065 }
4066 3827
4067 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 3828 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
4068 SCHEME_V->code = car (SCHEME_V->code); 3829 SCHEME_V->code = car (SCHEME_V->code);
4069 s_goto (OP_EVAL); 3830 s_goto (OP_EVAL);
4070 3831
4071 case OP_AND1: /* and */ 3832 case OP_AND1: /* and */
4072 if (is_false (SCHEME_V->value)) 3833 if (is_false (SCHEME_V->value))
4073 {
4074 s_return (SCHEME_V->value); 3834 s_return (SCHEME_V->value);
4075 }
4076 else if (SCHEME_V->code == NIL) 3835 else if (SCHEME_V->code == NIL)
4077 {
4078 s_return (SCHEME_V->value); 3836 s_return (SCHEME_V->value);
4079 }
4080 else 3837 else
4081 { 3838 {
4082 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 3839 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
4083 SCHEME_V->code = car (SCHEME_V->code); 3840 SCHEME_V->code = car (SCHEME_V->code);
4084 s_goto (OP_EVAL); 3841 s_goto (OP_EVAL);
4085 } 3842 }
4086 3843
4087 case OP_OR0: /* or */ 3844 case OP_OR0: /* or */
4088 if (SCHEME_V->code == NIL) 3845 if (SCHEME_V->code == NIL)
4089 {
4090 s_return (S_F); 3846 s_return (S_F);
4091 }
4092 3847
4093 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 3848 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
4094 SCHEME_V->code = car (SCHEME_V->code); 3849 SCHEME_V->code = car (SCHEME_V->code);
4095 s_goto (OP_EVAL); 3850 s_goto (OP_EVAL);
4096 3851
4097 case OP_OR1: /* or */ 3852 case OP_OR1: /* or */
4098 if (is_true (SCHEME_V->value)) 3853 if (is_true (SCHEME_V->value))
4099 {
4100 s_return (SCHEME_V->value); 3854 s_return (SCHEME_V->value);
4101 }
4102 else if (SCHEME_V->code == NIL) 3855 else if (SCHEME_V->code == NIL)
4103 {
4104 s_return (SCHEME_V->value); 3856 s_return (SCHEME_V->value);
4105 }
4106 else 3857 else
4107 { 3858 {
4108 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 3859 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
4109 SCHEME_V->code = car (SCHEME_V->code); 3860 SCHEME_V->code = car (SCHEME_V->code);
4110 s_goto (OP_EVAL); 3861 s_goto (OP_EVAL);
4132 x = car (SCHEME_V->code); 3883 x = car (SCHEME_V->code);
4133 SCHEME_V->code = cadr (SCHEME_V->code); 3884 SCHEME_V->code = cadr (SCHEME_V->code);
4134 } 3885 }
4135 3886
4136 if (!is_symbol (x)) 3887 if (!is_symbol (x))
4137 {
4138 Error_0 ("variable is not a symbol"); 3888 Error_0 ("variable is not a symbol");
4139 }
4140 3889
4141 s_save (SCHEME_A_ OP_MACRO1, NIL, x); 3890 s_save (SCHEME_A_ OP_MACRO1, NIL, x);
4142 s_goto (OP_EVAL); 3891 s_goto (OP_EVAL);
4143 3892
4144 case OP_MACRO1: /* macro */ 3893 case OP_MACRO1: /* macro */
4145 set_typeflag (SCHEME_V->value, T_MACRO); 3894 set_typeflag (SCHEME_V->value, T_MACRO);
4146 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); 3895 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0);
4147 3896
4148 if (x != NIL) 3897 if (x != NIL)
4149 {
4150 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); 3898 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value);
4151 }
4152 else 3899 else
4153 {
4154 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); 3900 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value);
4155 }
4156 3901
4157 s_return (SCHEME_V->code); 3902 s_return (SCHEME_V->code);
4158 3903
4159 case OP_CASE0: /* case */ 3904 case OP_CASE0: /* case */
4160 s_save (SCHEME_A_ OP_CASE1, NIL, cdr (SCHEME_V->code)); 3905 s_save (SCHEME_A_ OP_CASE1, NIL, cdr (SCHEME_V->code));
4163 3908
4164 case OP_CASE1: /* case */ 3909 case OP_CASE1: /* case */
4165 for (x = SCHEME_V->code; x != NIL; x = cdr (x)) 3910 for (x = SCHEME_V->code; x != NIL; x = cdr (x))
4166 { 3911 {
4167 if (!is_pair (y = caar (x))) 3912 if (!is_pair (y = caar (x)))
4168 {
4169 break; 3913 break;
4170 }
4171 3914
4172 for (; y != NIL; y = cdr (y)) 3915 for (; y != NIL; y = cdr (y))
4173 { 3916 {
4174 if (eqv (car (y), SCHEME_V->value)) 3917 if (eqv (car (y), SCHEME_V->value))
4175 {
4176 break; 3918 break;
4177 }
4178 } 3919 }
4179 3920
4180 if (y != NIL) 3921 if (y != NIL)
4181 {
4182 break; 3922 break;
4183 }
4184 } 3923 }
4185 3924
4186 if (x != NIL) 3925 if (x != NIL)
4187 { 3926 {
4188 if (is_pair (caar (x))) 3927 if (is_pair (caar (x)))
4196 SCHEME_V->code = caar (x); 3935 SCHEME_V->code = caar (x);
4197 s_goto (OP_EVAL); 3936 s_goto (OP_EVAL);
4198 } 3937 }
4199 } 3938 }
4200 else 3939 else
4201 {
4202 s_return (NIL); 3940 s_return (NIL);
4203 }
4204 3941
4205 case OP_CASE2: /* case */ 3942 case OP_CASE2: /* case */
4206 if (is_true (SCHEME_V->value)) 3943 if (is_true (SCHEME_V->value))
4207 {
4208 s_goto (OP_BEGIN); 3944 s_goto (OP_BEGIN);
4209 }
4210 else 3945 else
4211 {
4212 s_return (NIL); 3946 s_return (NIL);
4213 }
4214 3947
4215 case OP_PAPPLY: /* apply */ 3948 case OP_PAPPLY: /* apply */
4216 SCHEME_V->code = car (SCHEME_V->args); 3949 SCHEME_V->code = car (SCHEME_V->args);
4217 SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); 3950 SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args));
4218 /*SCHEME_V->args = cadr(SCHEME_V->args); */ 3951 /*SCHEME_V->args = cadr(SCHEME_V->args); */
4219 s_goto (OP_APPLY); 3952 s_goto (OP_APPLY);
4220 3953
4221 case OP_PEVAL: /* eval */ 3954 case OP_PEVAL: /* eval */
4222 if (cdr (SCHEME_V->args) != NIL) 3955 if (cdr (SCHEME_V->args) != NIL)
4223 {
4224 SCHEME_V->envir = cadr (SCHEME_V->args); 3956 SCHEME_V->envir = cadr (SCHEME_V->args);
4225 }
4226 3957
4227 SCHEME_V->code = car (SCHEME_V->args); 3958 SCHEME_V->code = car (SCHEME_V->args);
4228 s_goto (OP_EVAL); 3959 s_goto (OP_EVAL);
4229 3960
4230 case OP_CONTINUATION: /* call-with-current-continuation */ 3961 case OP_CONTINUATION: /* call-with-current-continuation */
4231 SCHEME_V->code = car (SCHEME_V->args); 3962 SCHEME_V->code = car (SCHEME_V->args);
4232 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_V)), NIL); 3963 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
4233 s_goto (OP_APPLY); 3964 s_goto (OP_APPLY);
4234 } 3965 }
4235 3966
4236 return S_T; 3967 return S_T;
4237} 3968}
4252 3983
4253 case OP_INEX2EX: /* inexact->exact */ 3984 case OP_INEX2EX: /* inexact->exact */
4254 x = car (SCHEME_V->args); 3985 x = car (SCHEME_V->args);
4255 3986
4256 if (num_is_integer (x)) 3987 if (num_is_integer (x))
4257 {
4258 s_return (x); 3988 s_return (x);
4259 }
4260 else if (modf (rvalue_unchecked (x), &dd) == 0.0) 3989 else if (modf (rvalue_unchecked (x), &dd) == 0.0)
4261 {
4262 s_return (mk_integer (SCHEME_A_ ivalue (x))); 3990 s_return (mk_integer (SCHEME_A_ ivalue (x)));
4263 }
4264 else 3991 else
4265 {
4266 Error_1 ("inexact->exact: not integral:", x); 3992 Error_1 ("inexact->exact: not integral:", x);
4267 }
4268 3993
4269 case OP_EXP: 3994 case OP_EXP:
4270 x = car (SCHEME_V->args); 3995 x = car (SCHEME_V->args);
4271 s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 3996 s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
4272 3997
4296 4021
4297 case OP_ATAN: 4022 case OP_ATAN:
4298 x = car (SCHEME_V->args); 4023 x = car (SCHEME_V->args);
4299 4024
4300 if (cdr (SCHEME_V->args) == NIL) 4025 if (cdr (SCHEME_V->args) == NIL)
4301 {
4302 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 4026 s_return (mk_real (SCHEME_A_ atan (rvalue (x))));
4303 }
4304 else 4027 else
4305 { 4028 {
4306 pointer y = cadr (SCHEME_V->args); 4029 pointer y = cadr (SCHEME_V->args);
4307 4030
4308 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); 4031 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
4324 real_result = 0; 4047 real_result = 0;
4325 4048
4326 /* This 'if' is an R5RS compatibility fix. */ 4049 /* This 'if' is an R5RS compatibility fix. */
4327 /* NOTE: Remove this 'if' fix for R6RS. */ 4050 /* NOTE: Remove this 'if' fix for R6RS. */
4328 if (rvalue (x) == 0 && rvalue (y) < 0) 4051 if (rvalue (x) == 0 && rvalue (y) < 0)
4329 {
4330 result = 0.0; 4052 result = 0.0;
4331 }
4332 else 4053 else
4333 {
4334 result = pow (rvalue (x), rvalue (y)); 4054 result = pow (rvalue (x), rvalue (y));
4335 }
4336 4055
4337 /* Before returning integer result make sure we can. */ 4056 /* Before returning integer result make sure we can. */
4338 /* If the test fails, result is too big for integer. */ 4057 /* If the test fails, result is too big for integer. */
4339 if (!real_result) 4058 if (!real_result)
4340 { 4059 {
4343 if (result != (RVALUE) result_as_long) 4062 if (result != (RVALUE) result_as_long)
4344 real_result = 1; 4063 real_result = 1;
4345 } 4064 }
4346 4065
4347 if (real_result) 4066 if (real_result)
4348 {
4349 s_return (mk_real (SCHEME_A_ result)); 4067 s_return (mk_real (SCHEME_A_ result));
4350 }
4351 else 4068 else
4352 {
4353 s_return (mk_integer (SCHEME_A_ result)); 4069 s_return (mk_integer (SCHEME_A_ result));
4354 }
4355 } 4070 }
4356 4071
4357 case OP_FLOOR: 4072 case OP_FLOOR:
4358 x = car (SCHEME_V->args); 4073 x = car (SCHEME_V->args);
4359 s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4074 s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4368 4083
4369 x = car (SCHEME_V->args); 4084 x = car (SCHEME_V->args);
4370 rvalue_of_x = rvalue (x); 4085 rvalue_of_x = rvalue (x);
4371 4086
4372 if (rvalue_of_x > 0) 4087 if (rvalue_of_x > 0)
4373 {
4374 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); 4088 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x)));
4375 }
4376 else 4089 else
4377 {
4378 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); 4090 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
4379 }
4380 } 4091 }
4381 4092
4382 case OP_ROUND: 4093 case OP_ROUND:
4383 x = car (SCHEME_V->args); 4094 x = car (SCHEME_V->args);
4384 4095
4512 } 4223 }
4513 else 4224 else
4514 Error_0 ("set-cdr!: unable to alter immutable pair"); 4225 Error_0 ("set-cdr!: unable to alter immutable pair");
4515 4226
4516 case OP_CHAR2INT: /* char->integer */ 4227 case OP_CHAR2INT: /* char->integer */
4517 { 4228 s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args))));
4518 char c;
4519
4520 c = (char) ivalue (car (SCHEME_V->args));
4521 s_return (mk_integer (SCHEME_A_ (unsigned char) c));
4522 }
4523 4229
4524 case OP_INT2CHAR: /* integer->char */ 4230 case OP_INT2CHAR: /* integer->char */
4525 {
4526 unsigned char c;
4527
4528 c = (unsigned char) ivalue (car (SCHEME_V->args));
4529 s_return (mk_character (SCHEME_A_ (char) c)); 4231 s_return (mk_character (SCHEME_A_ ivalue (car (SCHEME_V->args))));
4530 }
4531 4232
4532 case OP_CHARUPCASE: 4233 case OP_CHARUPCASE:
4533 { 4234 {
4534 unsigned char c;
4535
4536 c = (unsigned char) ivalue (car (SCHEME_V->args)); 4235 unsigned char c = ivalue (car (SCHEME_V->args));
4537 c = toupper (c); 4236 c = toupper (c);
4538 s_return (mk_character (SCHEME_A_ (char) c)); 4237 s_return (mk_character (SCHEME_A_ c));
4539 } 4238 }
4540 4239
4541 case OP_CHARDNCASE: 4240 case OP_CHARDNCASE:
4542 { 4241 {
4543 unsigned char c;
4544
4545 c = (unsigned char) ivalue (car (SCHEME_V->args)); 4242 unsigned char c = ivalue (car (SCHEME_V->args));
4546 c = tolower (c); 4243 c = tolower (c);
4547 s_return (mk_character (SCHEME_A_ (char) c)); 4244 s_return (mk_character (SCHEME_A_ c));
4548 } 4245 }
4549 4246
4550 case OP_STR2SYM: /* string->symbol */ 4247 case OP_STR2SYM: /* string->symbol */
4551 s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); 4248 s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args))));
4552 4249
4564 if (pf == 16 || pf == 10 || pf == 8 || pf == 2) 4261 if (pf == 16 || pf == 10 || pf == 8 || pf == 2)
4565 { 4262 {
4566 /* base is OK */ 4263 /* base is OK */
4567 } 4264 }
4568 else 4265 else
4569 {
4570 pf = -1; 4266 pf = -1;
4571 }
4572 } 4267 }
4573 4268
4574 if (pf < 0) 4269 if (pf < 0)
4575 {
4576 Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); 4270 Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args));
4577 }
4578 else if (*s == '#') /* no use of base! */ 4271 else if (*s == '#') /* no use of base! */
4579 {
4580 s_return (mk_sharp_const (SCHEME_A_ s + 1)); 4272 s_return (mk_sharp_const (SCHEME_A_ s + 1));
4581 }
4582 else 4273 else
4583 { 4274 {
4584 if (pf == 0 || pf == 10) 4275 if (pf == 0 || pf == 10)
4585 {
4586 s_return (mk_atom (SCHEME_A_ s)); 4276 s_return (mk_atom (SCHEME_A_ s));
4587 }
4588 else 4277 else
4589 { 4278 {
4590 char *ep; 4279 char *ep;
4591 long iv = strtol (s, &ep, (int) pf); 4280 long iv = strtol (s, &ep, (int) pf);
4592 4281
4593 if (*ep == 0) 4282 if (*ep == 0)
4594 {
4595 s_return (mk_integer (SCHEME_A_ iv)); 4283 s_return (mk_integer (SCHEME_A_ iv));
4596 }
4597 else 4284 else
4598 {
4599 s_return (S_F); 4285 s_return (S_F);
4600 }
4601 } 4286 }
4602 } 4287 }
4603 } 4288 }
4604 4289
4605 case OP_SYM2STR: /* symbol->string */ 4290 case OP_SYM2STR: /* symbol->string */
4622 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) 4307 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2))
4623 { 4308 {
4624 /* base is OK */ 4309 /* base is OK */
4625 } 4310 }
4626 else 4311 else
4627 {
4628 pf = -1; 4312 pf = -1;
4629 }
4630 } 4313 }
4631 4314
4632 if (pf < 0) 4315 if (pf < 0)
4633 {
4634 Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); 4316 Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args));
4635 }
4636 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) 4317 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x))
4637 { 4318 {
4638 char *p; 4319 char *p;
4639 int len; 4320 int len;
4640 4321
4641 atom2str (SCHEME_A_ x, (int) pf, &p, &len); 4322 atom2str (SCHEME_A_ x, pf, &p, &len);
4642 s_return (mk_counted_string (SCHEME_A_ p, len)); 4323 s_return (mk_counted_string (SCHEME_A_ p, len));
4643 } 4324 }
4644 else 4325 else
4645 {
4646 Error_1 ("atom->string: not an atom:", x); 4326 Error_1 ("atom->string: not an atom:", x);
4647 }
4648 } 4327 }
4649 4328
4650 case OP_MKSTRING: /* make-string */ 4329 case OP_MKSTRING: /* make-string */
4651 { 4330 {
4652 int fill = ' '; 4331 int fill = ' ';
4653 int len; 4332 int len;
4654 4333
4655 len = ivalue (car (SCHEME_V->args)); 4334 len = ivalue (car (SCHEME_V->args));
4656 4335
4657 if (cdr (SCHEME_V->args) != NIL) 4336 if (cdr (SCHEME_V->args) != NIL)
4658 {
4659 fill = charvalue (cadr (SCHEME_V->args)); 4337 fill = charvalue (cadr (SCHEME_V->args));
4660 }
4661 4338
4662 s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); 4339 s_return (mk_empty_string (SCHEME_A_ len, (char) fill));
4663 } 4340 }
4664 4341
4665 case OP_STRLEN: /* string-length */ 4342 case OP_STRLEN: /* string-length */
4673 str = strvalue (car (SCHEME_V->args)); 4350 str = strvalue (car (SCHEME_V->args));
4674 4351
4675 index = ivalue (cadr (SCHEME_V->args)); 4352 index = ivalue (cadr (SCHEME_V->args));
4676 4353
4677 if (index >= strlength (car (SCHEME_V->args))) 4354 if (index >= strlength (car (SCHEME_V->args)))
4678 {
4679 Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); 4355 Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args));
4680 }
4681 4356
4682 s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); 4357 s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index]));
4683 } 4358 }
4684 4359
4685 case OP_STRSET: /* string-set! */ 4360 case OP_STRSET: /* string-set! */
4687 char *str; 4362 char *str;
4688 int index; 4363 int index;
4689 int c; 4364 int c;
4690 4365
4691 if (is_immutable (car (SCHEME_V->args))) 4366 if (is_immutable (car (SCHEME_V->args)))
4692 {
4693 Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); 4367 Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args));
4694 }
4695 4368
4696 str = strvalue (car (SCHEME_V->args)); 4369 str = strvalue (car (SCHEME_V->args));
4697 4370
4698 index = ivalue (cadr (SCHEME_V->args)); 4371 index = ivalue (cadr (SCHEME_V->args));
4699 4372
4700 if (index >= strlength (car (SCHEME_V->args))) 4373 if (index >= strlength (car (SCHEME_V->args)))
4701 {
4702 Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); 4374 Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args));
4703 }
4704 4375
4705 c = charvalue (caddr (SCHEME_V->args)); 4376 c = charvalue (caddr (SCHEME_V->args));
4706 4377
4707 str[index] = (char) c; 4378 str[index] = (char) c;
4708 s_return (car (SCHEME_V->args)); 4379 s_return (car (SCHEME_V->args));
4715 pointer newstr; 4386 pointer newstr;
4716 char *pos; 4387 char *pos;
4717 4388
4718 /* compute needed length for new string */ 4389 /* compute needed length for new string */
4719 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4390 for (x = SCHEME_V->args; x != NIL; x = cdr (x))
4720 {
4721 len += strlength (car (x)); 4391 len += strlength (car (x));
4722 }
4723 4392
4724 newstr = mk_empty_string (SCHEME_A_ len, ' '); 4393 newstr = mk_empty_string (SCHEME_A_ len, ' ');
4725 4394
4726 /* store the contents of the argument strings into the new string */ 4395 /* store the contents of the argument strings into the new string */
4727 for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x)) 4396 for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x))
4728 {
4729 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4397 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4730 }
4731 4398
4732 s_return (newstr); 4399 s_return (newstr);
4733 } 4400 }
4734 4401
4735 case OP_SUBSTR: /* substring */ 4402 case OP_SUBSTR: /* substring */
4742 str = strvalue (car (SCHEME_V->args)); 4409 str = strvalue (car (SCHEME_V->args));
4743 4410
4744 index0 = ivalue (cadr (SCHEME_V->args)); 4411 index0 = ivalue (cadr (SCHEME_V->args));
4745 4412
4746 if (index0 > strlength (car (SCHEME_V->args))) 4413 if (index0 > strlength (car (SCHEME_V->args)))
4747 {
4748 Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); 4414 Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args));
4749 }
4750 4415
4751 if (cddr (SCHEME_V->args) != NIL) 4416 if (cddr (SCHEME_V->args) != NIL)
4752 { 4417 {
4753 index1 = ivalue (caddr (SCHEME_V->args)); 4418 index1 = ivalue (caddr (SCHEME_V->args));
4754 4419
4755 if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) 4420 if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0)
4756 {
4757 Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); 4421 Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args));
4758 }
4759 } 4422 }
4760 else 4423 else
4761 {
4762 index1 = strlength (car (SCHEME_V->args)); 4424 index1 = strlength (car (SCHEME_V->args));
4763 }
4764 4425
4765 len = index1 - index0; 4426 len = index1 - index0;
4766 x = mk_empty_string (SCHEME_A_ len, ' '); 4427 x = mk_empty_string (SCHEME_A_ len, ' ');
4767 memcpy (strvalue (x), str + index0, len); 4428 memcpy (strvalue (x), str + index0, len);
4768 strvalue (x)[len] = 0; 4429 strvalue (x)[len] = 0;
4775 int i; 4436 int i;
4776 pointer vec; 4437 pointer vec;
4777 int len = list_length (SCHEME_A_ SCHEME_V->args); 4438 int len = list_length (SCHEME_A_ SCHEME_V->args);
4778 4439
4779 if (len < 0) 4440 if (len < 0)
4780 {
4781 Error_1 ("vector: not a proper list:", SCHEME_V->args); 4441 Error_1 ("vector: not a proper list:", SCHEME_V->args);
4782 }
4783 4442
4784 vec = mk_vector (SCHEME_A_ len); 4443 vec = mk_vector (SCHEME_A_ len);
4785 4444
4786#if USE_ERROR_CHECKING 4445#if USE_ERROR_CHECKING
4787 if (SCHEME_V->no_memory) 4446 if (SCHEME_V->no_memory)
4817 4476
4818 s_return (vec); 4477 s_return (vec);
4819 } 4478 }
4820 4479
4821 case OP_VECLEN: /* vector-length */ 4480 case OP_VECLEN: /* vector-length */
4822 s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4481 s_return (mk_integer (SCHEME_A_ veclength (car (SCHEME_V->args))));
4823 4482
4824 case OP_VECREF: /* vector-ref */ 4483 case OP_VECREF: /* vector-ref */
4825 { 4484 {
4826 int index; 4485 int index;
4827 4486
4828 index = ivalue (cadr (SCHEME_V->args)); 4487 index = ivalue (cadr (SCHEME_V->args));
4829 4488
4830 if (index >= ivalue (car (SCHEME_V->args))) 4489 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING)
4831 {
4832 Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); 4490 Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args));
4833 }
4834 4491
4835 s_return (vector_elem (car (SCHEME_V->args), index)); 4492 s_return (vector_elem (car (SCHEME_V->args), index));
4836 } 4493 }
4837 4494
4838 case OP_VECSET: /* vector-set! */ 4495 case OP_VECSET: /* vector-set! */
4839 { 4496 {
4840 int index; 4497 int index;
4841 4498
4842 if (is_immutable (car (SCHEME_V->args))) 4499 if (is_immutable (car (SCHEME_V->args)))
4843 {
4844 Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); 4500 Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args));
4845 }
4846 4501
4847 index = ivalue (cadr (SCHEME_V->args)); 4502 index = ivalue (cadr (SCHEME_V->args));
4848 4503
4849 if (index >= ivalue (car (SCHEME_V->args))) 4504 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING)
4850 {
4851 Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); 4505 Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args));
4852 }
4853 4506
4854 set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); 4507 set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args));
4855 s_return (car (SCHEME_V->args)); 4508 s_return (car (SCHEME_V->args));
4856 } 4509 }
4857 } 4510 }
4967 x = cdr (x); 4620 x = cdr (x);
4968 4621
4969 for (; x != NIL; x = cdr (x)) 4622 for (; x != NIL; x = cdr (x))
4970 { 4623 {
4971 if (!comp_func (v, nvalue (car (x)))) 4624 if (!comp_func (v, nvalue (car (x))))
4972 {
4973 s_retbool (0); 4625 s_retbool (0);
4974 }
4975 4626
4976 v = nvalue (car (x)); 4627 v = nvalue (car (x));
4977 } 4628 }
4978 4629
4979 s_retbool (1); 4630 s_retbool (1);
5072 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); 4723 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code);
5073 SCHEME_V->args = NIL; 4724 SCHEME_V->args = NIL;
5074 s_goto (OP_APPLY); 4725 s_goto (OP_APPLY);
5075 } 4726 }
5076 else 4727 else
5077 {
5078 s_return (SCHEME_V->code); 4728 s_return (SCHEME_V->code);
5079 }
5080 4729
5081 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4730 case OP_SAVE_FORCED: /* Save forced value replacing promise */
5082 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); 4731 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell));
5083 s_return (SCHEME_V->value); 4732 s_return (SCHEME_V->value);
5084 4733
5148 else 4797 else
5149 { 4798 {
5150 putstr (SCHEME_A_ "\n"); 4799 putstr (SCHEME_A_ "\n");
5151 4800
5152 if (SCHEME_V->interactive_repl) 4801 if (SCHEME_V->interactive_repl)
5153 {
5154 s_goto (OP_T0LVL); 4802 s_goto (OP_T0LVL);
5155 }
5156 else 4803 else
5157 {
5158 return NIL; 4804 return NIL;
5159 }
5160 } 4805 }
5161 4806
5162 case OP_REVERSE: /* reverse */ 4807 case OP_REVERSE: /* reverse */
5163 s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); 4808 s_return (reverse (SCHEME_A_ car (SCHEME_V->args)));
5164 4809
5187 4832
5188#if USE_PLIST 4833#if USE_PLIST
5189 4834
5190 case OP_PUT: /* put */ 4835 case OP_PUT: /* put */
5191 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 4836 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args)))
5192 {
5193 Error_0 ("illegal use of put"); 4837 Error_0 ("illegal use of put");
5194 }
5195 4838
5196 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 4839 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x))
5197 { 4840 {
5198 if (caar (x) == y) 4841 if (caar (x) == y)
5199 {
5200 break; 4842 break;
5201 }
5202 } 4843 }
5203 4844
5204 if (x != NIL) 4845 if (x != NIL)
5205 cdar (x) = caddr (SCHEME_V->args); 4846 cdar (x) = caddr (SCHEME_V->args);
5206 else 4847 else
5283 } 4924 }
5284 4925
5285 p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); 4926 p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop);
5286 4927
5287 if (p == NIL) 4928 if (p == NIL)
5288 {
5289 s_return (S_F); 4929 s_return (S_F);
5290 }
5291 4930
5292 s_return (p); 4931 s_return (p);
5293 } 4932 }
5294 4933
5295# if USE_STRING_PORTS 4934# if USE_STRING_PORTS
5313 4952
5314 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4953 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)),
5315 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); 4954 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop);
5316 4955
5317 if (p == NIL) 4956 if (p == NIL)
5318 {
5319 s_return (S_F); 4957 s_return (S_F);
5320 }
5321 4958
5322 s_return (p); 4959 s_return (p);
5323 } 4960 }
5324 4961
5325 case OP_OPEN_OUTSTRING: /* open-output-string */ 4962 case OP_OPEN_OUTSTRING: /* open-output-string */
5329 if (car (SCHEME_V->args) == NIL) 4966 if (car (SCHEME_V->args) == NIL)
5330 { 4967 {
5331 p = port_from_scratch (SCHEME_A); 4968 p = port_from_scratch (SCHEME_A);
5332 4969
5333 if (p == NIL) 4970 if (p == NIL)
5334 {
5335 s_return (S_F); 4971 s_return (S_F);
5336 }
5337 } 4972 }
5338 else 4973 else
5339 { 4974 {
5340 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4975 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)),
5341 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); 4976 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output);
5342 4977
5343 if (p == NIL) 4978 if (p == NIL)
5344 {
5345 s_return (S_F); 4979 s_return (S_F);
5346 }
5347 } 4980 }
5348 4981
5349 s_return (p); 4982 s_return (p);
5350 } 4983 }
5351 4984
5416 { 5049 {
5417 /* ========== reading part ========== */ 5050 /* ========== reading part ========== */
5418#if USE_PORTS 5051#if USE_PORTS
5419 case OP_READ: 5052 case OP_READ:
5420 if (!is_pair (SCHEME_V->args)) 5053 if (!is_pair (SCHEME_V->args))
5421 {
5422 s_goto (OP_READ_INTERNAL); 5054 s_goto (OP_READ_INTERNAL);
5423 }
5424 5055
5425 if (!is_inport (car (SCHEME_V->args))) 5056 if (!is_inport (car (SCHEME_V->args)))
5426 {
5427 Error_1 ("read: not an input port:", car (SCHEME_V->args)); 5057 Error_1 ("read: not an input port:", car (SCHEME_V->args));
5428 }
5429 5058
5430 if (car (SCHEME_V->args) == SCHEME_V->inport) 5059 if (car (SCHEME_V->args) == SCHEME_V->inport)
5431 {
5432 s_goto (OP_READ_INTERNAL); 5060 s_goto (OP_READ_INTERNAL);
5433 }
5434 5061
5435 x = SCHEME_V->inport; 5062 x = SCHEME_V->inport;
5436 SCHEME_V->inport = car (SCHEME_V->args); 5063 SCHEME_V->inport = car (SCHEME_V->args);
5437 x = cons (x, NIL); 5064 x = cons (x, NIL);
5438 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 5065 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5469 { 5096 {
5470 pointer p = SCHEME_V->inport; 5097 pointer p = SCHEME_V->inport;
5471 int res; 5098 int res;
5472 5099
5473 if (is_pair (SCHEME_V->args)) 5100 if (is_pair (SCHEME_V->args))
5474 {
5475 p = car (SCHEME_V->args); 5101 p = car (SCHEME_V->args);
5476 }
5477 5102
5478 res = p->object.port->kind & port_string; 5103 res = p->object.port->kind & port_string;
5479 5104
5480 s_retbool (res); 5105 s_retbool (res);
5481 } 5106 }
5496 s_return (S_EOF); 5121 s_return (S_EOF);
5497 /* NOTREACHED */ 5122 /* NOTREACHED */
5498 5123
5499 case TOK_VEC: 5124 case TOK_VEC:
5500 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); 5125 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL);
5501
5502 /* fall through */ 5126 /* fall through */
5127
5503 case TOK_LPAREN: 5128 case TOK_LPAREN:
5504 SCHEME_V->tok = token (SCHEME_A); 5129 SCHEME_V->tok = token (SCHEME_A);
5505 5130
5506 if (SCHEME_V->tok == TOK_RPAREN) 5131 if (SCHEME_V->tok == TOK_RPAREN)
5507 s_return (NIL); 5132 s_return (NIL);
5527 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); 5152 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL);
5528 SCHEME_V->tok = TOK_LPAREN; 5153 SCHEME_V->tok = TOK_LPAREN;
5529 s_goto (OP_RDSEXPR); 5154 s_goto (OP_RDSEXPR);
5530 } 5155 }
5531 else 5156 else
5532 {
5533 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); 5157 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL);
5534 }
5535 5158
5536 s_goto (OP_RDSEXPR); 5159 s_goto (OP_RDSEXPR);
5537 5160
5538 case TOK_COMMA: 5161 case TOK_COMMA:
5539 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); 5162 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL);
5581 } 5204 }
5582 5205
5583 break; 5206 break;
5584 5207
5585 case OP_RDLIST: 5208 case OP_RDLIST:
5586 {
5587 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 5209 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args);
5588 SCHEME_V->tok = token (SCHEME_A); 5210 SCHEME_V->tok = token (SCHEME_A);
5589 5211
5590 if (SCHEME_V->tok == TOK_EOF) 5212 switch (SCHEME_V->tok)
5213 {
5214 case TOK_EOF:
5591 s_return (S_EOF); 5215 s_return (S_EOF);
5592 else if (SCHEME_V->tok == TOK_RPAREN) 5216
5217 case TOK_RPAREN:
5593 { 5218 {
5594 int c = inchar (SCHEME_A); 5219 int c = inchar (SCHEME_A);
5595 5220
5596 if (c != '\n') 5221 if (c != '\n')
5597 backchar (SCHEME_A_ c); 5222 backchar (SCHEME_A_ c);
5598
5599#if SHOW_ERROR_LINE 5223#if SHOW_ERROR_LINE
5600 else if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) 5224 else if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file)
5601 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line++; 5225 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line++;
5602
5603#endif 5226#endif
5227
5604 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5228 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5605 s_return (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args)); 5229 s_return (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args));
5606 } 5230 }
5607 else if (SCHEME_V->tok == TOK_DOT) 5231
5608 { 5232 case TOK_DOT:
5609 s_save (SCHEME_A_ OP_RDDOT, SCHEME_V->args, NIL); 5233 s_save (SCHEME_A_ OP_RDDOT, SCHEME_V->args, NIL);
5610 SCHEME_V->tok = token (SCHEME_A); 5234 SCHEME_V->tok = token (SCHEME_A);
5611 s_goto (OP_RDSEXPR); 5235 s_goto (OP_RDSEXPR);
5612 } 5236
5613 else 5237 default:
5614 {
5615 s_save (SCHEME_A_ OP_RDLIST, SCHEME_V->args, NIL);; 5238 s_save (SCHEME_A_ OP_RDLIST, SCHEME_V->args, NIL);
5616 s_goto (OP_RDSEXPR); 5239 s_goto (OP_RDSEXPR);
5617 } 5240 }
5618 }
5619 5241
5620 case OP_RDDOT: 5242 case OP_RDDOT:
5621 if (token (SCHEME_A) != TOK_RPAREN) 5243 if (token (SCHEME_A) != TOK_RPAREN)
5622 Error_0 ("syntax error: illegal dot expression"); 5244 Error_0 ("syntax error: illegal dot expression");
5623 else 5245
5624 {
5625 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5246 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5626 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); 5247 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args));
5627 }
5628 5248
5629 case OP_RDQUOTE: 5249 case OP_RDQUOTE:
5630 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); 5250 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)));
5631 5251
5632 case OP_RDQQUOTE: 5252 case OP_RDQQUOTE:
5732 5352
5733 case OP_PVECFROM: 5353 case OP_PVECFROM:
5734 { 5354 {
5735 int i = ivalue_unchecked (cdr (SCHEME_V->args)); 5355 int i = ivalue_unchecked (cdr (SCHEME_V->args));
5736 pointer vec = car (SCHEME_V->args); 5356 pointer vec = car (SCHEME_V->args);
5737 int len = ivalue_unchecked (vec); 5357 int len = veclength (vec);
5738 5358
5739 if (i == len) 5359 if (i == len)
5740 { 5360 {
5741 putstr (SCHEME_A_ ")"); 5361 putstr (SCHEME_A_ ")");
5742 s_return (S_T); 5362 s_return (S_T);
5898{ 5518{
5899 int n = procnum (x); 5519 int n = procnum (x);
5900 const char *name = dispatch_table[n].name; 5520 const char *name = dispatch_table[n].name;
5901 5521
5902 if (name == 0) 5522 if (name == 0)
5903 {
5904 name = "ILLEGAL!"; 5523 name = "ILLEGAL!";
5905 }
5906 5524
5907 return name; 5525 return name;
5908} 5526}
5909 5527
5910/* kernel of this interpreter */ 5528/* kernel of this interpreter */
5915 5533
5916 for (;;) 5534 for (;;)
5917 { 5535 {
5918 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5536 op_code_info *pcd = dispatch_table + SCHEME_V->op;
5919 5537
5538#if USE_ERROR_CHECKING
5920 if (pcd->name != 0) /* if built-in function, check arguments */ 5539 if (pcd->name) /* if built-in function, check arguments */
5921 { 5540 {
5541 int ok = 1;
5922 char msg[STRBUFFSIZE]; 5542 char msg[STRBUFFSIZE];
5923 int ok = 1;
5924 int n = list_length (SCHEME_A_ SCHEME_V->args); 5543 int n = list_length (SCHEME_A_ SCHEME_V->args);
5925 5544
5926#if USE_ERROR_CHECKING
5927 /* Check number of arguments */ 5545 /* Check number of arguments */
5928 if (n < pcd->min_arity) 5546 if (n < pcd->min_arity)
5929 { 5547 {
5930 ok = 0; 5548 ok = 0;
5931 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5549 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5936 { 5554 {
5937 ok = 0; 5555 ok = 0;
5938 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5556 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5939 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); 5557 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity);
5940 } 5558 }
5941#endif
5942 5559
5943 if (ok) 5560 if (ok)
5944 { 5561 {
5945 if (pcd->arg_tests_encoding != 0) 5562 if (pcd->arg_tests_encoding)
5946 { 5563 {
5947 int i = 0; 5564 int i = 0;
5948 int j; 5565 int j;
5949 const char *t = pcd->arg_tests_encoding; 5566 const char *t = pcd->arg_tests_encoding;
5950 pointer arglist = SCHEME_V->args; 5567 pointer arglist = SCHEME_V->args;
5965 if (!tests[j].fct (arg)) 5582 if (!tests[j].fct (arg))
5966 break; 5583 break;
5967 } 5584 }
5968 5585
5969 if (t[1] != 0) /* last test is replicated as necessary */ 5586 if (t[1] != 0) /* last test is replicated as necessary */
5970 {
5971 t++; 5587 t++;
5972 }
5973 5588
5974 arglist = cdr (arglist); 5589 arglist = cdr (arglist);
5975 i++; 5590 i++;
5976 } 5591 }
5977 while (i < n); 5592 while (i < n);
5978 5593
5979#if USE_ERROR_CHECKING
5980 if (i < n) 5594 if (i < n)
5981 { 5595 {
5982 ok = 0; 5596 ok = 0;
5983 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); 5597 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind);
5984 } 5598 }
5985#endif
5986 } 5599 }
5987 } 5600 }
5988 5601
5989 if (!ok) 5602 if (!ok)
5990 { 5603 {
5992 return; 5605 return;
5993 5606
5994 pcd = dispatch_table + SCHEME_V->op; 5607 pcd = dispatch_table + SCHEME_V->op;
5995 } 5608 }
5996 } 5609 }
5610#endif
5997 5611
5998 ok_to_freely_gc (SCHEME_A); 5612 ok_to_freely_gc (SCHEME_A);
5999 5613
6000 if (pcd->func (SCHEME_A_ (enum scheme_opcodes) SCHEME_V->op) == NIL) 5614 if (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL)
6001 return; 5615 return;
6002 5616
6003#if USE_ERROR_CHECKING 5617 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
6004 if (SCHEME_V->no_memory)
6005 { 5618 {
6006 xwrstr ("No memory!\n"); 5619 xwrstr ("No memory!\n");
6007 return; 5620 return;
6008 } 5621 }
6009#endif
6010 } 5622 }
6011} 5623}
6012 5624
6013/* ========== Initialization of internal keywords ========== */ 5625/* ========== Initialization of internal keywords ========== */
6014 5626
6015static void 5627static void
6016assign_syntax (SCHEME_P_ char *name) 5628assign_syntax (SCHEME_P_ const char *name)
6017{ 5629{
6018 pointer x = oblist_add_by_name (SCHEME_A_ name); 5630 pointer x = oblist_add_by_name (SCHEME_A_ name);
6019 set_typeflag (x, typeflag (x) | T_SYNTAX); 5631 set_typeflag (x, typeflag (x) | T_SYNTAX);
6020} 5632}
6021 5633
6022static void 5634static void
6023assign_proc (SCHEME_P_ enum scheme_opcodes op, char *name) 5635assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
6024{ 5636{
6025 pointer x = mk_symbol (SCHEME_A_ name); 5637 pointer x = mk_symbol (SCHEME_A_ name);
6026 pointer y = mk_proc (SCHEME_A_ op); 5638 pointer y = mk_proc (SCHEME_A_ op);
6027 new_slot_in_env (SCHEME_A_ x, y); 5639 new_slot_in_env (SCHEME_A_ x, y);
6028} 5640}
6030static pointer 5642static pointer
6031mk_proc (SCHEME_P_ enum scheme_opcodes op) 5643mk_proc (SCHEME_P_ enum scheme_opcodes op)
6032{ 5644{
6033 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5645 pointer y = get_cell (SCHEME_A_ NIL, NIL);
6034 set_typeflag (y, (T_PROC | T_ATOM)); 5646 set_typeflag (y, (T_PROC | T_ATOM));
6035 ivalue_unchecked (y) = (long) op; 5647 ivalue_unchecked (y) = op;
6036 set_num_integer (y); 5648 set_num_integer (y);
6037 return y; 5649 return y;
6038} 5650}
6039 5651
6040/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5652/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
6158 } 5770 }
6159 5771
6160 SCHEME_V->gc_verbose = 0; 5772 SCHEME_V->gc_verbose = 0;
6161 dump_stack_initialize (SCHEME_A); 5773 dump_stack_initialize (SCHEME_A);
6162 SCHEME_V->code = NIL; 5774 SCHEME_V->code = NIL;
5775 SCHEME_V->args = NIL;
5776 SCHEME_V->envir = NIL;
6163 SCHEME_V->tracing = 0; 5777 SCHEME_V->tracing = 0;
6164 5778
6165 /* init NIL */ 5779 /* init NIL */
6166 set_typeflag (NIL, T_ATOM | MARK); 5780 set_typeflag (NIL, T_ATOM | T_MARK);
6167 set_car (NIL, NIL); 5781 set_car (NIL, NIL);
6168 set_cdr (NIL, NIL); 5782 set_cdr (NIL, NIL);
6169 /* init T */ 5783 /* init T */
6170 set_typeflag (S_T, T_ATOM | MARK); 5784 set_typeflag (S_T, T_ATOM | T_MARK);
6171 set_car (S_T, S_T); 5785 set_car (S_T, S_T);
6172 set_cdr (S_T, S_T); 5786 set_cdr (S_T, S_T);
6173 /* init F */ 5787 /* init F */
6174 set_typeflag (S_F, T_ATOM | MARK); 5788 set_typeflag (S_F, T_ATOM | T_MARK);
6175 set_car (S_F, S_F); 5789 set_car (S_F, S_F);
6176 set_cdr (S_F, S_F); 5790 set_cdr (S_F, S_F);
5791 /* init EOF_OBJ */
5792 set_typeflag (S_EOF, T_ATOM | T_MARK);
5793 set_car (S_EOF, S_EOF);
5794 set_cdr (S_EOF, S_EOF);
6177 /* init sink */ 5795 /* init sink */
6178 set_typeflag (S_SINK, T_PAIR | MARK); 5796 set_typeflag (S_SINK, T_PAIR | T_MARK);
6179 set_car (S_SINK, NIL); 5797 set_car (S_SINK, NIL);
5798
6180 /* init c_nest */ 5799 /* init c_nest */
6181 SCHEME_V->c_nest = NIL; 5800 SCHEME_V->c_nest = NIL;
6182 5801
6183 SCHEME_V->oblist = oblist_initial_value (SCHEME_A); 5802 SCHEME_V->oblist = oblist_initial_value (SCHEME_A);
6184 /* init global_env */ 5803 /* init global_env */
6186 SCHEME_V->global_env = SCHEME_V->envir; 5805 SCHEME_V->global_env = SCHEME_V->envir;
6187 /* init else */ 5806 /* init else */
6188 x = mk_symbol (SCHEME_A_ "else"); 5807 x = mk_symbol (SCHEME_A_ "else");
6189 new_slot_in_env (SCHEME_A_ x, S_T); 5808 new_slot_in_env (SCHEME_A_ x, S_T);
6190 5809
6191 assign_syntax (SCHEME_A_ "lambda"); 5810 {
6192 assign_syntax (SCHEME_A_ "quote"); 5811 static const char *syntax_names[] = {
6193 assign_syntax (SCHEME_A_ "define"); 5812 "lambda", "quote", "define", "if", "begin", "set!",
6194 assign_syntax (SCHEME_A_ "if"); 5813 "let", "let*", "letrec", "cond", "delay", "and",
6195 assign_syntax (SCHEME_A_ "begin"); 5814 "or", "cons-stream", "macro", "case"
6196 assign_syntax (SCHEME_A_ "set!"); 5815 };
6197 assign_syntax (SCHEME_A_ "let"); 5816
6198 assign_syntax (SCHEME_A_ "let*"); 5817 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
6199 assign_syntax (SCHEME_A_ "letrec");
6200 assign_syntax (SCHEME_A_ "cond");
6201 assign_syntax (SCHEME_A_ "delay");
6202 assign_syntax (SCHEME_A_ "and"); 5818 assign_syntax (SCHEME_A_ syntax_names[i]);
6203 assign_syntax (SCHEME_A_ "or"); 5819 }
6204 assign_syntax (SCHEME_A_ "cons-stream");
6205 assign_syntax (SCHEME_A_ "macro");
6206 assign_syntax (SCHEME_A_ "case");
6207 5820
6208 for (i = 0; i < n; i++) 5821 for (i = 0; i < n; i++)
6209 {
6210 if (dispatch_table[i].name != 0) 5822 if (dispatch_table[i].name != 0)
6211 {
6212 assign_proc (SCHEME_A_ (enum scheme_opcodes) i, dispatch_table[i].name); 5823 assign_proc (SCHEME_A_ i, dispatch_table[i].name);
6213 }
6214 }
6215 5824
6216 /* initialization of global pointers to special symbols */ 5825 /* initialization of global pointers to special symbols */
6217 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 5826 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
6218 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 5827 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
6219 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 5828 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
6220 SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote"); 5829 SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote");
6221 SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing"); 5830 SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing");
6222 SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>"); 5831 SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>");
6223 SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*"); 5832 SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*");
6224 SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*"); 5833 SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*");
6225 SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*"); 5834 SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*");
6226 SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*"); 5835 SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*");
6227 5836
6228 return !SCHEME_V->no_memory; 5837 return !SCHEME_V->no_memory;
6229} 5838}
6230 5839
6385 pointer x; 5994 pointer x;
6386 5995
6387 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 5996 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
6388 5997
6389 if (x != NIL) 5998 if (x != NIL)
6390 {
6391 set_slot_in_env (SCHEME_A_ x, value); 5999 set_slot_in_env (SCHEME_A_ x, value);
6392 }
6393 else 6000 else
6394 {
6395 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 6001 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
6396 }
6397} 6002}
6398 6003
6399#if !STANDALONE 6004#if !STANDALONE
6005
6400void 6006void
6401scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 6007scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
6402{ 6008{
6403 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); 6009 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f));
6404} 6010}
6407scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 6013scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
6408{ 6014{
6409 int i; 6015 int i;
6410 6016
6411 for (i = 0; i < count; i++) 6017 for (i = 0; i < count; i++)
6412 {
6413 scheme_register_foreign_func (SCHEME_A_ list + i); 6018 scheme_register_foreign_func (SCHEME_A_ list + i);
6414 }
6415} 6019}
6416 6020
6417pointer 6021pointer
6418scheme_apply0 (SCHEME_P_ const char *procname) 6022scheme_apply0 (SCHEME_P_ const char *procname)
6419{ 6023{
6476 SCHEME_V->interactive_repl = old_repl; 6080 SCHEME_V->interactive_repl = old_repl;
6477 restore_from_C_call (SCHEME_A); 6081 restore_from_C_call (SCHEME_A);
6478 return SCHEME_V->value; 6082 return SCHEME_V->value;
6479} 6083}
6480 6084
6481
6482#endif 6085#endif
6483 6086
6484/* ========== Main ========== */ 6087/* ========== Main ========== */
6485 6088
6486#if STANDALONE 6089#if STANDALONE
6487 6090
6488# if defined(__APPLE__) && !defined (OSX)
6489int
6490main ()
6491{
6492 extern MacTS_main (int argc, char **argv);
6493 char **argv;
6494 int argc = ccommand (&argv);
6495
6496 MacTS_main (argc, argv);
6497 return 0;
6498}
6499
6500int
6501MacTS_main (int argc, char **argv)
6502{
6503# else
6504int 6091int
6505main (int argc, char **argv) 6092main (int argc, char **argv)
6506{ 6093{
6507# endif
6508# if USE_MULTIPLICITY 6094# if USE_MULTIPLICITY
6509 scheme ssc; 6095 scheme ssc;
6510 scheme *const sc = &ssc; 6096 scheme *const SCHEME_V = &ssc;
6511# else 6097# else
6512# endif 6098# endif
6513 int fin; 6099 int fin;
6514 char *file_name = InitFile; 6100 char *file_name = InitFile;
6515 int retcode; 6101 int retcode;
6516 int isfile = 1; 6102 int isfile = 1;
6517
6518 if (argc == 1)
6519 xwrstr (banner);
6520 6103
6521 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6104 if (argc == 2 && strcmp (argv[1], "-?") == 0)
6522 { 6105 {
6523 xwrstr ("Usage: tinyscheme -?\n"); 6106 xwrstr ("Usage: tinyscheme -?\n");
6524 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6107 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n");
6547 if (access (file_name, 0) != 0) 6130 if (access (file_name, 0) != 0)
6548 { 6131 {
6549 char *p = getenv ("TINYSCHEMEINIT"); 6132 char *p = getenv ("TINYSCHEMEINIT");
6550 6133
6551 if (p != 0) 6134 if (p != 0)
6552 {
6553 file_name = p; 6135 file_name = p;
6554 }
6555 } 6136 }
6556#endif 6137#endif
6557 6138
6558 do 6139 do
6559 { 6140 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines