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.24 by root, Fri Nov 27 02:12:08 2015 UTC vs.
Revision 1.33 by root, Sat Nov 28 10:59:14 2015 UTC

182# define FIRST_CELLSEGS 3 182# define FIRST_CELLSEGS 3
183#endif 183#endif
184 184
185enum scheme_types 185enum scheme_types
186{ 186{
187 T_INTEGER,
187 T_FREE, 188 T_REAL,
188 T_STRING, 189 T_STRING,
189 T_NUMBER,
190 T_SYMBOL, 190 T_SYMBOL,
191 T_PROC, 191 T_PROC,
192 T_PAIR, 192 T_PAIR, /* also used for free cells */
193 T_CLOSURE, 193 T_CLOSURE,
194 T_CONTINUATION, 194 T_CONTINUATION,
195 T_FOREIGN, 195 T_FOREIGN,
196 T_CHARACTER, 196 T_CHARACTER,
197 T_PORT, 197 T_PORT,
207#define T_SYNTAX 0x0010 207#define T_SYNTAX 0x0010
208#define T_IMMUTABLE 0x0020 208#define T_IMMUTABLE 0x0020
209#define T_ATOM 0x0040 /* only for gc */ 209#define T_ATOM 0x0040 /* only for gc */
210#define T_MARK 0x0080 /* only for gc */ 210#define T_MARK 0x0080 /* only for gc */
211 211
212/* num, for generic arithmetic */
213struct num
214{
215 IVALUE ivalue;
216#if USE_REAL
217 RVALUE rvalue;
218 char is_fixnum;
219#endif
220};
221
222#if USE_REAL
223# define num_is_fixnum(n) (n).is_fixnum
224# define num_set_fixnum(n,f) (n).is_fixnum = (f)
225# define num_ivalue(n) (n).ivalue
226# define num_rvalue(n) (n).rvalue
227# define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i)
228# define num_set_rvalue(n,r) (n).rvalue = (r)
229#else
230# define num_is_fixnum(n) 1
231# define num_set_fixnum(n,f) 0
232# define num_ivalue(n) (n).ivalue
233# define num_rvalue(n) (n).ivalue
234# define num_set_ivalue(n,i) (n).ivalue = (i)
235# define num_set_rvalue(n,r) (n).ivalue = (r)
236#endif
237
212enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; 238enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
213 239
214static num num_op (enum num_op op, num a, num b); 240static num num_op (enum num_op op, num a, num b);
215static num num_intdiv (num a, num b); 241static num num_intdiv (num a, num b);
216static num num_rem (num a, num b); 242static num num_rem (num a, num b);
219#if USE_MATH 245#if USE_MATH
220static double round_per_R5RS (double x); 246static double round_per_R5RS (double x);
221#endif 247#endif
222static int is_zero_rvalue (RVALUE x); 248static int is_zero_rvalue (RVALUE x);
223 249
224ecb_inline int
225num_is_integer (pointer p)
226{
227 return num_is_fixnum (p->object.number);
228}
229
230static num num_zero; 250static num num_zero;
231static num num_one; 251static num num_one;
232 252
233/* macros for cell operations */ 253/* macros for cell operations */
234#define typeflag(p) ((p)->flag + 0) 254#define typeflag(p) ((p)->flag + 0)
242} 262}
243 263
244#define strvalue(p) ((p)->object.string.svalue) 264#define strvalue(p) ((p)->object.string.svalue)
245#define strlength(p) ((p)->object.string.length) 265#define strlength(p) ((p)->object.string.length)
246 266
247INTERFACE int is_list (SCHEME_P_ pointer p);
248
249INTERFACE int 267INTERFACE int
250is_vector (pointer p) 268is_vector (pointer p)
251{ 269{
252 return type (p) == T_VECTOR; 270 return type (p) == T_VECTOR;
253} 271}
254 272
255#define vecvalue(p) ((p)->object.vector.vvalue) 273#define vecvalue(p) ((p)->object.vector.vvalue)
256#define veclength(p) ((p)->object.vector.length) 274#define veclength(p) ((p)->object.vector.length)
257INTERFACE void fill_vector (pointer vec, pointer obj); 275INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
258INTERFACE uint32_t vector_length (pointer vec);
259INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); 276INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
260INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); 277INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
261 278
262INTERFACE uint32_t 279INTERFACE int
263vector_length (pointer vec) 280is_integer (pointer p)
264{ 281{
265 return vec->object.vector.length; 282 return type (p) == T_INTEGER;
283}
284
285/* not the same as in scheme, where integers are (correctly :) reals */
286INTERFACE int
287is_real (pointer p)
288{
289 return type (p) == T_REAL;
266} 290}
267 291
268INTERFACE int 292INTERFACE int
269is_number (pointer p) 293is_number (pointer p)
270{ 294{
271 return type (p) == T_NUMBER; 295 return is_integer (p) || is_real (p);
272}
273
274INTERFACE int
275is_integer (pointer p)
276{
277 if (!is_number (p))
278 return 0;
279
280 if (num_is_integer (p) || ivalue (p) == rvalue (p))
281 return 1;
282
283 return 0;
284}
285
286INTERFACE int
287is_real (pointer p)
288{
289 return is_number (p) && !num_is_fixnum (p->object.number);
290} 296}
291 297
292INTERFACE int 298INTERFACE int
293is_character (pointer p) 299is_character (pointer p)
294{ 300{
299string_value (pointer p) 305string_value (pointer p)
300{ 306{
301 return strvalue (p); 307 return strvalue (p);
302} 308}
303 309
304ecb_inline num
305nvalue (pointer p)
306{
307 return (p)->object.number;
308}
309
310static IVALUE
311num_get_ivalue (const num n)
312{
313 return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n);
314}
315
316static RVALUE
317num_get_rvalue (const num n)
318{
319 return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n);
320}
321
322INTERFACE IVALUE
323ivalue (pointer p)
324{
325 return num_get_ivalue (p->object.number);
326}
327
328INTERFACE RVALUE
329rvalue (pointer p)
330{
331 return num_get_rvalue (p->object.number);
332}
333
334#define ivalue_unchecked(p) ((p)->object.number.value.ivalue) 310#define ivalue_unchecked(p) (p)->object.ivalue
311#define set_ivalue(p,v) (p)->object.ivalue = (v)
312
335#if USE_REAL 313#if USE_REAL
336# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 314#define rvalue_unchecked(p) (p)->object.rvalue
337# define set_num_integer(p) (p)->object.number.is_fixnum=1; 315#define set_rvalue(p,v) (p)->object.rvalue = (v)
338# define set_num_real(p) (p)->object.number.is_fixnum=0;
339#else 316#else
340# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 317#define rvalue_unchecked(p) (p)->object.ivalue
341# define set_num_integer(p) 0 318#define set_rvalue(p,v) (p)->object.ivalue = (v)
342# define set_num_real(p) 0
343#endif 319#endif
344 320
345INTERFACE long 321INTERFACE long
346charvalue (pointer p) 322charvalue (pointer p)
347{ 323{
452syntaxname (pointer p) 428syntaxname (pointer p)
453{ 429{
454 return strvalue (car (p)); 430 return strvalue (car (p));
455} 431}
456 432
457#define procnum(p) ivalue (p) 433#define procnum(p) ivalue_unchecked (p)
458static const char *procname (pointer x); 434static const char *procname (pointer x);
459 435
460INTERFACE int 436INTERFACE int
461is_closure (pointer p) 437is_closure (pointer p)
462{ 438{
523setimmutable (pointer p) 499setimmutable (pointer p)
524{ 500{
525#if USE_ERROR_CHECKING 501#if USE_ERROR_CHECKING
526 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 502 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
527#endif 503#endif
504}
505
506/* Result is:
507 proper list: length
508 circular list: -1
509 not even a pair: -2
510 dotted list: -2 minus length before dot
511*/
512INTERFACE int
513list_length (SCHEME_P_ pointer a)
514{
515 int i = 0;
516 pointer slow, fast;
517
518 slow = fast = a;
519
520 while (1)
521 {
522 if (fast == NIL)
523 return i;
524
525 if (!is_pair (fast))
526 return -2 - i;
527
528 fast = cdr (fast);
529 ++i;
530
531 if (fast == NIL)
532 return i;
533
534 if (!is_pair (fast))
535 return -2 - i;
536
537 ++i;
538 fast = cdr (fast);
539
540 /* Safe because we would have already returned if `fast'
541 encountered a non-pair. */
542 slow = cdr (slow);
543
544 if (fast == slow)
545 {
546 /* the fast pointer has looped back around and caught up
547 with the slow pointer, hence the structure is circular,
548 not of finite length, and therefore not a list */
549 return -1;
550 }
551 }
552}
553
554INTERFACE int
555is_list (SCHEME_P_ pointer a)
556{
557 return list_length (SCHEME_A_ a) >= 0;
528} 558}
529 559
530#if USE_CHAR_CLASSIFIERS 560#if USE_CHAR_CLASSIFIERS
531ecb_inline int 561ecb_inline int
532Cisalpha (int c) 562Cisalpha (int c)
676static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 706static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
677static void assign_syntax (SCHEME_P_ const char *name); 707static void assign_syntax (SCHEME_P_ const char *name);
678static int syntaxnum (pointer p); 708static int syntaxnum (pointer p);
679static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 709static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
680 710
711static IVALUE
712ivalue (pointer x)
713{
714 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
715}
716
717static RVALUE
718rvalue (pointer x)
719{
720 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
721}
722
723INTERFACE num
724nvalue (pointer x)
725{
726 num n;
727
728 num_set_fixnum (n, is_integer (x));
729
730 if (num_is_fixnum (n))
731 num_set_ivalue (n, ivalue_unchecked (x));
732 else
733 num_set_rvalue (n, rvalue_unchecked (x));
734
735 return n;
736}
737
681static num 738static num
682num_op (enum num_op op, num a, num b) 739num_op (enum num_op op, num a, num b)
683{ 740{
684 num ret; 741 num ret;
685 742
686 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));
687 744
688 if (num_is_fixnum (ret)) 745 if (num_is_fixnum (ret))
689 { 746 {
690 IVALUE av = num_get_ivalue (a);
691 IVALUE bv = num_get_ivalue (b);
692
693 switch (op) 747 switch (op)
694 { 748 {
695 case NUM_ADD: av += bv; break; 749 case NUM_ADD: a.ivalue += b.ivalue; break;
696 case NUM_SUB: av -= bv; break; 750 case NUM_SUB: a.ivalue -= b.ivalue; break;
697 case NUM_MUL: av *= bv; break; 751 case NUM_MUL: a.ivalue *= b.ivalue; break;
698 case NUM_INTDIV: av /= bv; break; 752 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
699 } 753 }
700 754
701 num_set_ivalue (ret, av); 755 num_set_ivalue (ret, a.ivalue);
702 } 756 }
757#if USE_REAL
703 else 758 else
704 { 759 {
705 RVALUE av = num_get_rvalue (a);
706 RVALUE bv = num_get_rvalue (b);
707
708 switch (op) 760 switch (op)
709 { 761 {
710 case NUM_ADD: av += bv; break; 762 case NUM_ADD: a.rvalue += b.rvalue; break;
711 case NUM_SUB: av -= bv; break; 763 case NUM_SUB: a.rvalue -= b.rvalue; break;
712 case NUM_MUL: av *= bv; break; 764 case NUM_MUL: a.rvalue *= b.rvalue; break;
713 case NUM_INTDIV: av /= bv; break; 765 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
714 } 766 }
715 767
716 num_set_rvalue (ret, av); 768 num_set_rvalue (ret, a.rvalue);
717 } 769 }
770#endif
718 771
719 return ret; 772 return ret;
720} 773}
721 774
722static num 775static num
723num_div (num a, num b) 776num_div (num a, num b)
724{ 777{
725 num ret; 778 num ret;
726 779
727 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); 780 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0);
728 781
729 if (num_is_fixnum (ret)) 782 if (num_is_fixnum (ret))
730 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 783 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
731 else 784 else
732 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 785 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
733 786
734 return ret; 787 return ret;
735} 788}
736 789
737static num 790static num
739{ 792{
740 num ret; 793 num ret;
741 long e1, e2, res; 794 long e1, e2, res;
742 795
743 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 796 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
744 e1 = num_get_ivalue (a); 797 e1 = num_ivalue (a);
745 e2 = num_get_ivalue (b); 798 e2 = num_ivalue (b);
746 res = e1 % e2; 799 res = e1 % e2;
747 800
748 /* remainder should have same sign as second operand */ 801 /* remainder should have same sign as second operand */
749 if (res > 0) 802 if (res > 0)
750 { 803 {
766{ 819{
767 num ret; 820 num ret;
768 long e1, e2, res; 821 long e1, e2, res;
769 822
770 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 823 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
771 e1 = num_get_ivalue (a); 824 e1 = num_ivalue (a);
772 e2 = num_get_ivalue (b); 825 e2 = num_ivalue (b);
773 res = e1 % e2; 826 res = e1 % e2;
774 827
775 /* modulo should have same sign as second operand */ 828 /* modulo should have same sign as second operand */
776 if (res * e2 < 0) 829 if (res * e2 < 0)
777 res += e2; 830 res += e2;
787 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 840 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
788 int ret; 841 int ret;
789 842
790 if (is_fixnum) 843 if (is_fixnum)
791 { 844 {
792 IVALUE av = num_get_ivalue (a); 845 IVALUE av = num_ivalue (a);
793 IVALUE bv = num_get_ivalue (b); 846 IVALUE bv = num_ivalue (b);
794 847
795 ret = av == bv ? 0 : av < bv ? -1 : +1; 848 ret = av == bv ? 0 : av < bv ? -1 : +1;
796 } 849 }
797 else 850 else
798 { 851 {
799 RVALUE av = num_get_rvalue (a); 852 RVALUE av = num_rvalue (a);
800 RVALUE bv = num_get_rvalue (b); 853 RVALUE bv = num_rvalue (b);
801 854
802 ret = av == bv ? 0 : av < bv ? -1 : +1; 855 ret = av == bv ? 0 : av < bv ? -1 : +1;
803 } 856 }
804 857
805 return ret; 858 return ret;
868 return k; 921 return k;
869 922
870 i = ++SCHEME_V->last_cell_seg; 923 i = ++SCHEME_V->last_cell_seg;
871 SCHEME_V->alloc_seg[i] = cp; 924 SCHEME_V->alloc_seg[i] = cp;
872 925
873 /* insert new segment in address order */
874 newp = (pointer)cp; 926 newp = (pointer)cp;
875 SCHEME_V->cell_seg[i] = newp; 927 SCHEME_V->cell_seg[i] = newp;
876 SCHEME_V->cell_segsize[i] = segsize; 928 SCHEME_V->cell_segsize[i] = segsize;
877
878 //TODO: insert, not swap
879 while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i])
880 {
881 p = SCHEME_V->cell_seg[i];
882 SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1];
883 SCHEME_V->cell_seg[i - 1] = p;
884
885 k = SCHEME_V->cell_segsize[i];
886 SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1];
887 SCHEME_V->cell_segsize[i - 1] = k;
888
889 --i;
890 }
891
892 SCHEME_V->fcells += segsize; 929 SCHEME_V->fcells += segsize;
893 last = newp + segsize - 1; 930 last = newp + segsize - 1;
894 931
895 for (p = newp; p <= last; p++) 932 for (p = newp; p <= last; p++)
896 { 933 {
897 set_typeflag (p, T_FREE); 934 set_typeflag (p, T_PAIR);
898 set_car (p, NIL); 935 set_car (p, NIL);
899 set_cdr (p, p + 1); 936 set_cdr (p, p + 1);
900 } 937 }
901 938
902 /* insert new cells in address order on free list */
903 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell)
904 {
905 set_cdr (last, SCHEME_V->free_cell); 939 set_cdr (last, SCHEME_V->free_cell);
906 SCHEME_V->free_cell = newp; 940 SCHEME_V->free_cell = newp;
907 }
908 else
909 {
910 p = SCHEME_V->free_cell;
911
912 while (cdr (p) != NIL && newp > cdr (p))
913 p = cdr (p);
914
915 set_cdr (last, cdr (p));
916 set_cdr (p, newp);
917 }
918 } 941 }
919 942
920 return n; 943 return n;
921} 944}
922 945
1001 /* Record it as a vector so that gc understands it. */ 1024 /* Record it as a vector so that gc understands it. */
1002 set_typeflag (v, T_VECTOR | T_ATOM); 1025 set_typeflag (v, T_VECTOR | T_ATOM);
1003 1026
1004 v->object.vector.vvalue = e; 1027 v->object.vector.vvalue = e;
1005 v->object.vector.length = len; 1028 v->object.vector.length = len;
1006 fill_vector (v, init); 1029 fill_vector (v, 0, init);
1007 push_recent_alloc (SCHEME_A_ v, NIL); 1030 push_recent_alloc (SCHEME_A_ v, NIL);
1008 1031
1009 return v; 1032 return v;
1010} 1033}
1011 1034
1058 1081
1059/* ========== oblist implementation ========== */ 1082/* ========== oblist implementation ========== */
1060 1083
1061#ifndef USE_OBJECT_LIST 1084#ifndef USE_OBJECT_LIST
1062 1085
1086static int
1063static int hash_fn (const char *key, int table_size); 1087hash_fn (const char *key, int table_size)
1088{
1089 const unsigned char *p = key;
1090 uint32_t hash = 2166136261;
1091
1092 while (*p)
1093 hash = (hash ^ *p++) * 16777619;
1094
1095 return hash % table_size;
1096}
1064 1097
1065static pointer 1098static pointer
1066oblist_initial_value (SCHEME_P) 1099oblist_initial_value (SCHEME_P)
1067{ 1100{
1068 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1101 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1077 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); 1110 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1078 set_typeflag (x, T_SYMBOL); 1111 set_typeflag (x, T_SYMBOL);
1079 setimmutable (car (x)); 1112 setimmutable (car (x));
1080 1113
1081 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1114 location = hash_fn (name, veclength (SCHEME_V->oblist));
1082 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); 1115 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1083 return x; 1116 return x;
1084} 1117}
1085 1118
1086ecb_inline pointer 1119ecb_inline pointer
1087oblist_find_by_name (SCHEME_P_ const char *name) 1120oblist_find_by_name (SCHEME_P_ const char *name)
1090 pointer x; 1123 pointer x;
1091 char *s; 1124 char *s;
1092 1125
1093 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1126 location = hash_fn (name, veclength (SCHEME_V->oblist));
1094 1127
1095 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1128 for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1096 { 1129 {
1097 s = symname (car (x)); 1130 s = symname (car (x));
1098 1131
1099 /* case-insensitive, per R5RS section 2 */ 1132 /* case-insensitive, per R5RS section 2 */
1100 if (stricmp (name, s) == 0) 1133 if (stricmp (name, s) == 0)
1110 int i; 1143 int i;
1111 pointer x; 1144 pointer x;
1112 pointer ob_list = NIL; 1145 pointer ob_list = NIL;
1113 1146
1114 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1147 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1115 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1148 for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1116 ob_list = cons (x, ob_list); 1149 ob_list = cons (x, ob_list);
1117 1150
1118 return ob_list; 1151 return ob_list;
1119} 1152}
1120 1153
1146 1179
1147/* returns the new symbol */ 1180/* returns the new symbol */
1148static pointer 1181static pointer
1149oblist_add_by_name (SCHEME_P_ const char *name) 1182oblist_add_by_name (SCHEME_P_ const char *name)
1150{ 1183{
1151 pointer x; 1184 pointer x = mk_string (SCHEME_A_ name);
1152
1153 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1154 set_typeflag (x, T_SYMBOL); 1185 set_typeflag (x, T_SYMBOL);
1155 setimmutable (car (x)); 1186 setimmutable (x);
1156 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1187 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1157 return x; 1188 return x;
1158} 1189}
1159 1190
1160static pointer 1191static pointer
1193mk_character (SCHEME_P_ int c) 1224mk_character (SCHEME_P_ int c)
1194{ 1225{
1195 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1226 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1196 1227
1197 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1228 set_typeflag (x, (T_CHARACTER | T_ATOM));
1198 ivalue_unchecked (x) = c & 0xff; 1229 set_ivalue (x, c & 0xff);
1199 set_num_integer (x); 1230
1200 return x; 1231 return x;
1201} 1232}
1202 1233
1203/* get number atom (integer) */ 1234/* get number atom (integer) */
1204INTERFACE pointer 1235INTERFACE pointer
1205mk_integer (SCHEME_P_ long num) 1236mk_integer (SCHEME_P_ long n)
1206{ 1237{
1207 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1238 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1208 1239
1209 set_typeflag (x, (T_NUMBER | T_ATOM)); 1240 set_typeflag (x, (T_INTEGER | T_ATOM));
1210 ivalue_unchecked (x) = num; 1241 set_ivalue (x, n);
1211 set_num_integer (x); 1242
1212 return x; 1243 return x;
1213} 1244}
1214 1245
1215INTERFACE pointer 1246INTERFACE pointer
1216mk_real (SCHEME_P_ RVALUE n) 1247mk_real (SCHEME_P_ RVALUE n)
1217{ 1248{
1249#if USE_REAL
1218 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1250 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1219 1251
1220 set_typeflag (x, (T_NUMBER | T_ATOM)); 1252 set_typeflag (x, (T_REAL | T_ATOM));
1221 rvalue_unchecked (x) = n; 1253 set_rvalue (x, n);
1222 set_num_real (x); 1254
1223 return x; 1255 return x;
1256#else
1257 return mk_integer (SCHEME_A_ n);
1258#endif
1224} 1259}
1225 1260
1226static pointer 1261static pointer
1227mk_number (SCHEME_P_ const num n) 1262mk_number (SCHEME_P_ const num n)
1228{ 1263{
1264#if USE_REAL
1229 if (num_is_fixnum (n)) 1265 return num_is_fixnum (n)
1266 ? mk_integer (SCHEME_A_ num_ivalue (n))
1267 : mk_real (SCHEME_A_ num_rvalue (n));
1268#else
1230 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1269 return mk_integer (SCHEME_A_ num_ivalue (n));
1231 else 1270#endif
1232 return mk_real (SCHEME_A_ num_get_rvalue (n));
1233} 1271}
1234 1272
1235/* allocate name to string area */ 1273/* allocate name to string area */
1236static char * 1274static char *
1237store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) 1275store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1296{ 1334{
1297 return get_vector_object (SCHEME_A_ len, NIL); 1335 return get_vector_object (SCHEME_A_ len, NIL);
1298} 1336}
1299 1337
1300INTERFACE void 1338INTERFACE void
1301fill_vector (pointer vec, pointer obj) 1339fill_vector (pointer vec, uint32_t start, pointer obj)
1302{ 1340{
1303 int i; 1341 int i;
1304 1342
1305 for (i = 0; i < vec->object.vector.length; i++) 1343 for (i = start; i < veclength (vec); i++)
1306 vecvalue (vec)[i] = obj; 1344 vecvalue (vec)[i] = obj;
1307} 1345}
1308 1346
1309INTERFACE pointer 1347INTERFACE pointer
1310vector_elem (pointer vec, uint32_t ielem) 1348vector_get (pointer vec, uint32_t ielem)
1311{ 1349{
1312 return vecvalue(vec)[ielem]; 1350 return vecvalue(vec)[ielem];
1313} 1351}
1314 1352
1315INTERFACE void 1353INTERFACE void
1316set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1354vector_set (pointer vec, uint32_t ielem, pointer a)
1317{ 1355{
1318 vecvalue(vec)[ielem] = a; 1356 vecvalue(vec)[ielem] = a;
1319} 1357}
1320 1358
1321/* get new symbol */ 1359/* get new symbol */
1413 } 1451 }
1414 else if ((c == 'e') || (c == 'E')) 1452 else if ((c == 'e') || (c == 'E'))
1415 { 1453 {
1416 if (!has_fp_exp) 1454 if (!has_fp_exp)
1417 { 1455 {
1418 has_dec_point = 1; /* decimal point illegal 1456 has_dec_point = 1; /* decimal point illegal from now on */
1419 from now on */
1420 p++; 1457 p++;
1421 1458
1422 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1459 if ((*p == '-') || (*p == '+') || isdigit (*p))
1423 continue; 1460 continue;
1424 } 1461 }
1512 1549
1513 if (ecb_expect_false (is_vector (p))) 1550 if (ecb_expect_false (is_vector (p)))
1514 { 1551 {
1515 int i; 1552 int i;
1516 1553
1517 for (i = 0; i < p->object.vector.length; i++) 1554 for (i = 0; i < veclength (p); i++)
1518 mark (vecvalue (p)[i]); 1555 mark (vecvalue (p)[i]);
1519 } 1556 }
1520 1557
1521 if (is_atom (p)) 1558 if (is_atom (p))
1522 goto E6; 1559 goto E6;
1620 if (is_mark (p)) 1657 if (is_mark (p))
1621 clrmark (p); 1658 clrmark (p);
1622 else 1659 else
1623 { 1660 {
1624 /* reclaim cell */ 1661 /* reclaim cell */
1625 if (typeflag (p) != T_FREE) 1662 if (typeflag (p) != T_PAIR)
1626 { 1663 {
1627 finalize_cell (SCHEME_A_ p); 1664 finalize_cell (SCHEME_A_ p);
1628 set_typeflag (p, T_FREE); 1665 set_typeflag (p, T_PAIR);
1629 set_car (p, NIL); 1666 set_car (p, NIL);
1630 } 1667 }
1631 1668
1632 ++SCHEME_V->fcells; 1669 ++SCHEME_V->fcells;
1633 set_cdr (p, SCHEME_V->free_cell); 1670 set_cdr (p, SCHEME_V->free_cell);
1635 } 1672 }
1636 } 1673 }
1637 } 1674 }
1638 1675
1639 if (SCHEME_V->gc_verbose) 1676 if (SCHEME_V->gc_verbose)
1677 {
1640 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1678 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1679 }
1641} 1680}
1642 1681
1643static void 1682static void
1644finalize_cell (SCHEME_P_ pointer a) 1683finalize_cell (SCHEME_P_ pointer a)
1645{ 1684{
2480 { 2519 {
2481 p = SCHEME_V->strbuff; 2520 p = SCHEME_V->strbuff;
2482 2521
2483 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2522 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2484 { 2523 {
2485 if (num_is_integer (l)) 2524 if (is_integer (l))
2486 xnum (p, ivalue_unchecked (l)); 2525 xnum (p, ivalue_unchecked (l));
2487#if USE_REAL 2526#if USE_REAL
2488 else 2527 else
2489 { 2528 {
2490 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2529 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2745 return 0; 2784 return 0;
2746 } 2785 }
2747 else if (is_number (a)) 2786 else if (is_number (a))
2748 { 2787 {
2749 if (is_number (b)) 2788 if (is_number (b))
2750 if (num_is_integer (a) == num_is_integer (b))
2751 return num_cmp (nvalue (a), nvalue (b)) == 0; 2789 return num_cmp (nvalue (a), nvalue (b)) == 0;
2752 2790
2753 return 0; 2791 return 0;
2754 } 2792 }
2755 else if (is_character (a)) 2793 else if (is_character (a))
2756 { 2794 {
2782/* () is #t in R5RS */ 2820/* () is #t in R5RS */
2783#define is_true(p) ((p) != S_F) 2821#define is_true(p) ((p) != S_F)
2784#define is_false(p) ((p) == S_F) 2822#define is_false(p) ((p) == S_F)
2785 2823
2786/* ========== Environment implementation ========== */ 2824/* ========== Environment implementation ========== */
2787
2788#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST)
2789
2790static int
2791hash_fn (const char *key, int table_size)
2792{
2793 const unsigned char *p = key;
2794 uint32_t hash = 2166136261;
2795
2796 while (*p)
2797 hash = (hash ^ *p++) * 16777619;
2798
2799 return hash % table_size;
2800}
2801#endif
2802 2825
2803#ifndef USE_ALIST_ENV 2826#ifndef USE_ALIST_ENV
2804 2827
2805/* 2828/*
2806 * In this implementation, each frame of the environment may be 2829 * In this implementation, each frame of the environment may be
2823 2846
2824 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2847 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2825 setenvironment (SCHEME_V->envir); 2848 setenvironment (SCHEME_V->envir);
2826} 2849}
2827 2850
2851static uint32_t
2852sym_hash (pointer sym, uint32_t size)
2853{
2854 uintptr_t ptr = (uintptr_t)sym;
2855
2856#if 0
2857 /* table size is prime, so why mix */
2858 ptr += ptr >> 32;
2859 ptr += ptr >> 16;
2860 ptr += ptr >> 8;
2861#endif
2862
2863 return ptr % size;
2864}
2865
2828ecb_inline void 2866ecb_inline void
2829new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2867new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2830{ 2868{
2831 pointer slot = immutable_cons (variable, value); 2869 pointer slot = immutable_cons (variable, value);
2832 2870
2833 if (is_vector (car (env))) 2871 if (is_vector (car (env)))
2834 { 2872 {
2835 int location = hash_fn (symname (variable), veclength (car (env))); 2873 int location = sym_hash (variable, veclength (car (env)));
2836
2837 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2874 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2838 } 2875 }
2839 else 2876 else
2840 set_car (env, immutable_cons (slot, car (env))); 2877 set_car (env, immutable_cons (slot, car (env)));
2841} 2878}
2842 2879
2843static pointer 2880static pointer
2844find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2881find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2845{ 2882{
2846 pointer x, y; 2883 pointer x, y;
2847 int location;
2848 2884
2849 for (x = env; x != NIL; x = cdr (x)) 2885 for (x = env; x != NIL; x = cdr (x))
2850 { 2886 {
2851 if (is_vector (car (x))) 2887 if (is_vector (car (x)))
2852 { 2888 {
2853 location = hash_fn (symname (hdl), veclength (car (x))); 2889 int location = sym_hash (hdl, veclength (car (x)));
2854 y = vector_elem (car (x), location); 2890 y = vector_get (car (x), location);
2855 } 2891 }
2856 else 2892 else
2857 y = car (x); 2893 y = car (x);
2858 2894
2859 for (; y != NIL; y = cdr (y)) 2895 for (; y != NIL; y = cdr (y))
2860 if (caar (y) == hdl) 2896 if (caar (y) == hdl)
2861 break; 2897 break;
2862 2898
2863 if (y != NIL) 2899 if (y != NIL)
2900 return car (y);
2901
2902 if (!all)
2864 break; 2903 break;
2865
2866 if (!all)
2867 return NIL;
2868 } 2904 }
2869
2870 if (x != NIL)
2871 return car (y);
2872 2905
2873 return NIL; 2906 return NIL;
2874} 2907}
2875 2908
2876#else /* USE_ALIST_ENV */ 2909#else /* USE_ALIST_ENV */
2898 for (y = car (x); y != NIL; y = cdr (y)) 2931 for (y = car (x); y != NIL; y = cdr (y))
2899 if (caar (y) == hdl) 2932 if (caar (y) == hdl)
2900 break; 2933 break;
2901 2934
2902 if (y != NIL) 2935 if (y != NIL)
2936 return car (y);
2903 break; 2937 break;
2904 2938
2905 if (!all) 2939 if (!all)
2906 return NIL; 2940 break;
2907 } 2941 }
2908
2909 if (x != NIL)
2910 return car (y);
2911 2942
2912 return NIL; 2943 return NIL;
2913} 2944}
2914 2945
2915#endif /* USE_ALIST_ENV else */ 2946#endif /* USE_ALIST_ENV else */
3132 int i = 0; 3163 int i = 0;
3133 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3164 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3134 3165
3135 while (cont != NIL) 3166 while (cont != NIL)
3136 { 3167 {
3137 frame->op = ivalue (car (cont)); cont = cdr (cont); 3168 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3138 frame->args = car (cont) ; cont = cdr (cont); 3169 frame->args = car (cont) ; cont = cdr (cont);
3139 frame->envir = car (cont) ; cont = cdr (cont); 3170 frame->envir = car (cont) ; cont = cdr (cont);
3140 frame->code = car (cont) ; cont = cdr (cont); 3171 frame->code = car (cont) ; cont = cdr (cont);
3141 3172
3142 ++frame; 3173 ++frame;
3143 ++i; 3174 ++i;
3144 } 3175 }
3145 3176
3174 SCHEME_V->value = a; 3205 SCHEME_V->value = a;
3175 3206
3176 if (dump == NIL) 3207 if (dump == NIL)
3177 return -1; 3208 return -1;
3178 3209
3179 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3210 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3180 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3211 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3181 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3212 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3182 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3213 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3183 3214
3184 SCHEME_V->dump = dump; 3215 SCHEME_V->dump = dump;
3185 3216
3186 return 0; 3217 return 0;
3187} 3218}
3393 3424
3394 case OP_TRACING: 3425 case OP_TRACING:
3395 { 3426 {
3396 int tr = SCHEME_V->tracing; 3427 int tr = SCHEME_V->tracing;
3397 3428
3398 SCHEME_V->tracing = ivalue (car (args)); 3429 SCHEME_V->tracing = ivalue_unchecked (car (args));
3399 s_return (mk_integer (SCHEME_A_ tr)); 3430 s_return (mk_integer (SCHEME_A_ tr));
3400 } 3431 }
3401 3432
3402#endif 3433#endif
3403 3434
3922{ 3953{
3923 pointer args = SCHEME_V->args; 3954 pointer args = SCHEME_V->args;
3924 pointer x = car (args); 3955 pointer x = car (args);
3925 num v; 3956 num v;
3926 3957
3927#if USE_MATH
3928 RVALUE dd;
3929#endif
3930
3931 switch (op) 3958 switch (op)
3932 { 3959 {
3933#if USE_MATH 3960#if USE_MATH
3934 case OP_INEX2EX: /* inexact->exact */ 3961 case OP_INEX2EX: /* inexact->exact */
3962 {
3935 if (num_is_integer (x)) 3963 if (is_integer (x))
3936 s_return (x); 3964 s_return (x);
3937 else if (modf (rvalue_unchecked (x), &dd) == 0) 3965
3966 RVALUE r = rvalue_unchecked (x);
3967
3968 if (r == (RVALUE)(IVALUE)r)
3938 s_return (mk_integer (SCHEME_A_ ivalue (x))); 3969 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3939 else 3970 else
3940 Error_1 ("inexact->exact: not integral:", x); 3971 Error_1 ("inexact->exact: not integral:", x);
3972 }
3941 3973
3942 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 3974 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
3943 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 3975 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))));
3944 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 3976 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
3945 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 3977 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3963 { 3995 {
3964 RVALUE result; 3996 RVALUE result;
3965 int real_result = 1; 3997 int real_result = 1;
3966 pointer y = cadr (args); 3998 pointer y = cadr (args);
3967 3999
3968 if (num_is_integer (x) && num_is_integer (y)) 4000 if (is_integer (x) && is_integer (y))
3969 real_result = 0; 4001 real_result = 0;
3970 4002
3971 /* This 'if' is an R5RS compatibility fix. */ 4003 /* This 'if' is an R5RS compatibility fix. */
3972 /* NOTE: Remove this 'if' fix for R6RS. */ 4004 /* NOTE: Remove this 'if' fix for R6RS. */
3973 if (rvalue (x) == 0 && rvalue (y) < 0) 4005 if (rvalue (x) == 0 && rvalue (y) < 0)
3979 /* If the test fails, result is too big for integer. */ 4011 /* If the test fails, result is too big for integer. */
3980 if (!real_result) 4012 if (!real_result)
3981 { 4013 {
3982 long result_as_long = result; 4014 long result_as_long = result;
3983 4015
3984 if (result != (RVALUE) result_as_long) 4016 if (result != result_as_long)
3985 real_result = 1; 4017 real_result = 1;
3986 } 4018 }
3987 4019
3988 if (real_result) 4020 if (real_result)
3989 s_return (mk_real (SCHEME_A_ result)); 4021 s_return (mk_real (SCHEME_A_ result));
3994 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4026 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
3995 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); 4027 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
3996 4028
3997 case OP_TRUNCATE: 4029 case OP_TRUNCATE:
3998 { 4030 {
3999 RVALUE rvalue_of_x; 4031 RVALUE n = rvalue (x);
4000
4001 rvalue_of_x = rvalue (x);
4002
4003 if (rvalue_of_x > 0)
4004 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); 4032 s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n)));
4005 else
4006 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
4007 } 4033 }
4008 4034
4009 case OP_ROUND: 4035 case OP_ROUND:
4010 if (num_is_integer (x)) 4036 if (is_integer (x))
4011 s_return (x); 4037 s_return (x);
4012 4038
4013 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4039 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4014#endif 4040#endif
4015 4041
4135 } 4161 }
4136 else 4162 else
4137 Error_0 ("set-cdr!: unable to alter immutable pair"); 4163 Error_0 ("set-cdr!: unable to alter immutable pair");
4138 4164
4139 case OP_CHAR2INT: /* char->integer */ 4165 case OP_CHAR2INT: /* char->integer */
4140 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4166 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4141 4167
4142 case OP_INT2CHAR: /* integer->char */ 4168 case OP_INT2CHAR: /* integer->char */
4143 s_return (mk_character (SCHEME_A_ ivalue (x))); 4169 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4144 4170
4145 case OP_CHARUPCASE: 4171 case OP_CHARUPCASE:
4146 { 4172 {
4147 unsigned char c = ivalue (x); 4173 unsigned char c = ivalue_unchecked (x);
4148 c = toupper (c); 4174 c = toupper (c);
4149 s_return (mk_character (SCHEME_A_ c)); 4175 s_return (mk_character (SCHEME_A_ c));
4150 } 4176 }
4151 4177
4152 case OP_CHARDNCASE: 4178 case OP_CHARDNCASE:
4153 { 4179 {
4154 unsigned char c = ivalue (x); 4180 unsigned char c = ivalue_unchecked (x);
4155 c = tolower (c); 4181 c = tolower (c);
4156 s_return (mk_character (SCHEME_A_ c)); 4182 s_return (mk_character (SCHEME_A_ c));
4157 } 4183 }
4158 4184
4159 case OP_STR2SYM: /* string->symbol */ 4185 case OP_STR2SYM: /* string->symbol */
4236 Error_1 ("atom->string: not an atom:", x); 4262 Error_1 ("atom->string: not an atom:", x);
4237 } 4263 }
4238 4264
4239 case OP_MKSTRING: /* make-string */ 4265 case OP_MKSTRING: /* make-string */
4240 { 4266 {
4241 int fill = ' '; 4267 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4242 int len;
4243
4244 len = ivalue (x); 4268 int len = ivalue_unchecked (x);
4245
4246 if (cdr (args) != NIL)
4247 fill = charvalue (cadr (args));
4248 4269
4249 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4270 s_return (mk_empty_string (SCHEME_A_ len, fill));
4250 } 4271 }
4251 4272
4252 case OP_STRLEN: /* string-length */ 4273 case OP_STRLEN: /* string-length */
4253 s_return (mk_integer (SCHEME_A_ strlength (x))); 4274 s_return (mk_integer (SCHEME_A_ strlength (x)));
4254 4275
4255 case OP_STRREF: /* string-ref */ 4276 case OP_STRREF: /* string-ref */
4256 { 4277 {
4257 char *str;
4258 int index;
4259
4260 str = strvalue (x); 4278 char *str = strvalue (x);
4261
4262 index = ivalue (cadr (args)); 4279 int index = ivalue_unchecked (cadr (args));
4263 4280
4264 if (index >= strlength (x)) 4281 if (index >= strlength (x))
4265 Error_1 ("string-ref: out of bounds:", cadr (args)); 4282 Error_1 ("string-ref: out of bounds:", cadr (args));
4266 4283
4267 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4284 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4268 } 4285 }
4269 4286
4270 case OP_STRSET: /* string-set! */ 4287 case OP_STRSET: /* string-set! */
4271 { 4288 {
4272 char *str; 4289 char *str = strvalue (x);
4273 int index; 4290 int index = ivalue_unchecked (cadr (args));
4274 int c; 4291 int c;
4275 4292
4276 if (is_immutable (x)) 4293 if (is_immutable (x))
4277 Error_1 ("string-set!: unable to alter immutable string:", x); 4294 Error_1 ("string-set!: unable to alter immutable string:", x);
4278
4279 str = strvalue (x);
4280
4281 index = ivalue (cadr (args));
4282 4295
4283 if (index >= strlength (x)) 4296 if (index >= strlength (x))
4284 Error_1 ("string-set!: out of bounds:", cadr (args)); 4297 Error_1 ("string-set!: out of bounds:", cadr (args));
4285 4298
4286 c = charvalue (caddr (args)); 4299 c = charvalue (caddr (args));
4309 s_return (newstr); 4322 s_return (newstr);
4310 } 4323 }
4311 4324
4312 case OP_SUBSTR: /* substring */ 4325 case OP_SUBSTR: /* substring */
4313 { 4326 {
4314 char *str; 4327 char *str = strvalue (x);
4315 int index0; 4328 int index0 = ivalue_unchecked (cadr (args));
4316 int index1; 4329 int index1;
4317 int len; 4330 int len;
4318 4331
4319 str = strvalue (x);
4320
4321 index0 = ivalue (cadr (args));
4322
4323 if (index0 > strlength (x)) 4332 if (index0 > strlength (x))
4324 Error_1 ("substring: start out of bounds:", cadr (args)); 4333 Error_1 ("substring: start out of bounds:", cadr (args));
4325 4334
4326 if (cddr (args) != NIL) 4335 if (cddr (args) != NIL)
4327 { 4336 {
4328 index1 = ivalue (caddr (args)); 4337 index1 = ivalue_unchecked (caddr (args));
4329 4338
4330 if (index1 > strlength (x) || index1 < index0) 4339 if (index1 > strlength (x) || index1 < index0)
4331 Error_1 ("substring: end out of bounds:", caddr (args)); 4340 Error_1 ("substring: end out of bounds:", caddr (args));
4332 } 4341 }
4333 else 4342 else
4356 if (SCHEME_V->no_memory) 4365 if (SCHEME_V->no_memory)
4357 s_return (S_SINK); 4366 s_return (S_SINK);
4358#endif 4367#endif
4359 4368
4360 for (x = args, i = 0; is_pair (x); x = cdr (x), i++) 4369 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4361 set_vector_elem (vec, i, car (x)); 4370 vector_set (vec, i, car (x));
4362 4371
4363 s_return (vec); 4372 s_return (vec);
4364 } 4373 }
4365 4374
4366 case OP_MKVECTOR: /* make-vector */ 4375 case OP_MKVECTOR: /* make-vector */
4367 { 4376 {
4368 pointer fill = NIL; 4377 pointer fill = NIL;
4369 int len;
4370 pointer vec; 4378 pointer vec;
4371
4372 len = ivalue (x); 4379 int len = ivalue_unchecked (x);
4373 4380
4374 if (cdr (args) != NIL) 4381 if (cdr (args) != NIL)
4375 fill = cadr (args); 4382 fill = cadr (args);
4376 4383
4377 vec = mk_vector (SCHEME_A_ len); 4384 vec = mk_vector (SCHEME_A_ len);
4380 if (SCHEME_V->no_memory) 4387 if (SCHEME_V->no_memory)
4381 s_return (S_SINK); 4388 s_return (S_SINK);
4382#endif 4389#endif
4383 4390
4384 if (fill != NIL) 4391 if (fill != NIL)
4385 fill_vector (vec, fill); 4392 fill_vector (vec, 0, fill);
4386 4393
4387 s_return (vec); 4394 s_return (vec);
4388 } 4395 }
4389 4396
4390 case OP_VECLEN: /* vector-length */ 4397 case OP_VECLEN: /* vector-length */
4391 s_return (mk_integer (SCHEME_A_ veclength (x))); 4398 s_return (mk_integer (SCHEME_A_ veclength (x)));
4392 4399
4393 case OP_VECREF: /* vector-ref */ 4400 case OP_VECREF: /* vector-ref */
4394 { 4401 {
4395 int index;
4396
4397 index = ivalue (cadr (args)); 4402 int index = ivalue_unchecked (cadr (args));
4398 4403
4399 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4404 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4400 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4405 Error_1 ("vector-ref: out of bounds:", cadr (args));
4401 4406
4402 s_return (vector_elem (x, index)); 4407 s_return (vector_get (x, index));
4403 } 4408 }
4404 4409
4405 case OP_VECSET: /* vector-set! */ 4410 case OP_VECSET: /* vector-set! */
4406 { 4411 {
4407 int index; 4412 int index = ivalue_unchecked (cadr (args));
4408 4413
4409 if (is_immutable (x)) 4414 if (is_immutable (x))
4410 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4415 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4411 4416
4412 index = ivalue (cadr (args));
4413
4414 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4417 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4415 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4418 Error_1 ("vector-set!: out of bounds:", cadr (args));
4416 4419
4417 set_vector_elem (x, index, caddr (args)); 4420 vector_set (x, index, caddr (args));
4418 s_return (x); 4421 s_return (x);
4419 } 4422 }
4420 } 4423 }
4421 4424
4422 if (USE_ERROR_CHECKING) abort (); 4425 if (USE_ERROR_CHECKING) abort ();
4423}
4424
4425INTERFACE int
4426is_list (SCHEME_P_ pointer a)
4427{
4428 return list_length (SCHEME_A_ a) >= 0;
4429}
4430
4431/* Result is:
4432 proper list: length
4433 circular list: -1
4434 not even a pair: -2
4435 dotted list: -2 minus length before dot
4436*/
4437INTERFACE int
4438list_length (SCHEME_P_ pointer a)
4439{
4440 int i = 0;
4441 pointer slow, fast;
4442
4443 slow = fast = a;
4444
4445 while (1)
4446 {
4447 if (fast == NIL)
4448 return i;
4449
4450 if (!is_pair (fast))
4451 return -2 - i;
4452
4453 fast = cdr (fast);
4454 ++i;
4455
4456 if (fast == NIL)
4457 return i;
4458
4459 if (!is_pair (fast))
4460 return -2 - i;
4461
4462 ++i;
4463 fast = cdr (fast);
4464
4465 /* Safe because we would have already returned if `fast'
4466 encountered a non-pair. */
4467 slow = cdr (slow);
4468
4469 if (fast == slow)
4470 {
4471 /* the fast pointer has looped back around and caught up
4472 with the slow pointer, hence the structure is circular,
4473 not of finite length, and therefore not a list */
4474 return -1;
4475 }
4476 }
4477} 4426}
4478 4427
4479static int 4428static int
4480opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4429opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4481{ 4430{
4527 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4476 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4528 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4477 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4529 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4478 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4530 4479
4531#if USE_CHAR_CLASSIFIERS 4480#if USE_CHAR_CLASSIFIERS
4532 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4481 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4533 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4482 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4534 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4483 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4535 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4484 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4536 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4485 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4537#endif 4486#endif
4538 4487
4539#if USE_PORTS 4488#if USE_PORTS
4540 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4489 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4541 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4490 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4742 4691
4743 case OP_NEWSEGMENT: /* new-segment */ 4692 case OP_NEWSEGMENT: /* new-segment */
4744 if (!is_pair (args) || !is_number (a)) 4693 if (!is_pair (args) || !is_number (a))
4745 Error_0 ("new-segment: argument must be a number"); 4694 Error_0 ("new-segment: argument must be a number");
4746 4695
4747 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 4696 alloc_cellseg (SCHEME_A_ ivalue (a));
4748 4697
4749 s_return (S_T); 4698 s_return (S_T);
4750 4699
4751 case OP_OBLIST: /* oblist */ 4700 case OP_OBLIST: /* oblist */
4752 s_return (oblist_all_symbols (SCHEME_A)); 4701 s_return (oblist_all_symbols (SCHEME_A));
5197 putstr (SCHEME_A_ ")"); 5146 putstr (SCHEME_A_ ")");
5198 s_return (S_T); 5147 s_return (S_T);
5199 } 5148 }
5200 else 5149 else
5201 { 5150 {
5202 pointer elem = vector_elem (vec, i); 5151 pointer elem = vector_get (vec, i);
5203 5152
5204 ivalue_unchecked (cdr (args)) = i + 1; 5153 ivalue_unchecked (cdr (args)) = i + 1;
5205 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5154 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5206 SCHEME_V->args = elem; 5155 SCHEME_V->args = elem;
5207 5156
5282/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5231/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5283typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); 5232typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5284 5233
5285typedef int (*test_predicate)(pointer); 5234typedef int (*test_predicate)(pointer);
5286static int 5235static int
5287is_any (pointer p) 5236tst_any (pointer p)
5288{ 5237{
5289 return 1; 5238 return 1;
5290} 5239}
5291 5240
5292static int 5241static int
5293is_nonneg (pointer p) 5242tst_inonneg (pointer p)
5294{ 5243{
5295 return ivalue (p) >= 0 && is_integer (p); 5244 return is_integer (p) && ivalue_unchecked (p) >= 0;
5296} 5245}
5297 5246
5298static int 5247static int
5299tst_is_list (pointer p) 5248tst_is_list (SCHEME_P_ pointer p)
5300{ 5249{
5301 return p == NIL || is_pair (p); 5250 return p == NIL || is_pair (p);
5302} 5251}
5303 5252
5304/* Correspond carefully with following defines! */ 5253/* Correspond carefully with following defines! */
5305static struct 5254static struct
5306{ 5255{
5307 test_predicate fct; 5256 test_predicate fct;
5308 const char *kind; 5257 const char *kind;
5309} tests[] = 5258} tests[] = {
5310{
5311 { is_any, 0 }, 5259 { tst_any , 0 },
5312 { is_string, "string" }, 5260 { is_string , "string" },
5313 { is_symbol, "symbol" }, 5261 { is_symbol , "symbol" },
5314 { is_port, "port" }, 5262 { is_port , "port" },
5315 { is_inport, "input port" }, 5263 { is_inport , "input port" },
5316 { is_outport, "output port" }, 5264 { is_outport , "output port" },
5317 { is_environment, "environment" }, 5265 { is_environment, "environment" },
5318 { is_pair, "pair" }, 5266 { is_pair , "pair" },
5319 { tst_is_list, "pair or '()" }, 5267 { 0 , "pair or '()" },
5320 { is_character, "character" }, 5268 { is_character , "character" },
5321 { is_vector, "vector" }, 5269 { is_vector , "vector" },
5322 { is_number, "number" }, 5270 { is_number , "number" },
5323 { is_integer, "integer" }, 5271 { is_integer , "integer" },
5324 { is_nonneg, "non-negative integer" } 5272 { tst_inonneg , "non-negative integer" }
5325}; 5273};
5326 5274
5327#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ 5275#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5328#define TST_ANY "\001" 5276#define TST_ANY "\001"
5329#define TST_STRING "\002" 5277#define TST_STRING "\002"
5370typedef struct 5318typedef struct
5371{ 5319{
5372 uint8_t func; 5320 uint8_t func;
5373 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ 5321 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5374 uint8_t builtin; 5322 uint8_t builtin;
5323#if USE_ERROR_CHECKING
5375 uint8_t min_arity; 5324 uint8_t min_arity;
5376 uint8_t max_arity; 5325 uint8_t max_arity;
5377 char arg_tests_encoding[3]; 5326 char arg_tests_encoding[3];
5327#endif
5378} op_code_info; 5328} op_code_info;
5379 5329
5380static const op_code_info dispatch_table[] = { 5330static const op_code_info dispatch_table[] = {
5331#if USE_ERROR_CHECKING
5381#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, 5332#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5333#else
5334#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5335#endif
5382#include "opdefines.h" 5336#include "opdefines.h"
5383#undef OP_DEF 5337#undef OP_DEF
5384 {0} 5338 {0}
5385}; 5339};
5386 5340
5428 { 5382 {
5429 pointer arg = car (arglist); 5383 pointer arg = car (arglist);
5430 5384
5431 j = t[0]; 5385 j = t[0];
5432 5386
5387 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5388 if (j == TST_LIST[0])
5389 {
5390 if (!tst_is_list (SCHEME_A_ arg))
5391 break;
5392 }
5393 else
5394 {
5433 if (!tests[j - 1].fct (arg)) 5395 if (!tests[j - 1].fct (arg))
5434 break; 5396 break;
5397 }
5435 5398
5436 if (t[1]) /* last test is replicated as necessary */ 5399 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5437 t++; 5400 t++;
5438 5401
5439 arglist = cdr (arglist); 5402 arglist = cdr (arglist);
5440 i++; 5403 i++;
5441 } 5404 }
5496mk_proc (SCHEME_P_ enum scheme_opcodes op) 5459mk_proc (SCHEME_P_ enum scheme_opcodes op)
5497{ 5460{
5498 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5461 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5499 set_typeflag (y, (T_PROC | T_ATOM)); 5462 set_typeflag (y, (T_PROC | T_ATOM));
5500 ivalue_unchecked (y) = op; 5463 ivalue_unchecked (y) = op;
5501 set_num_integer (y);
5502 return y; 5464 return y;
5503} 5465}
5504 5466
5505/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5467/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5506static int 5468static int

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines