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.25 by root, Fri Nov 27 04:37:26 2015 UTC vs.
Revision 1.28 by root, Sat Nov 28 08:09:04 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);
236} 262}
237 263
238#define strvalue(p) ((p)->object.string.svalue) 264#define strvalue(p) ((p)->object.string.svalue)
239#define strlength(p) ((p)->object.string.length) 265#define strlength(p) ((p)->object.string.length)
240 266
241INTERFACE int is_list (SCHEME_P_ pointer p);
242
243INTERFACE int 267INTERFACE int
244is_vector (pointer p) 268is_vector (pointer p)
245{ 269{
246 return type (p) == T_VECTOR; 270 return type (p) == T_VECTOR;
247} 271}
248 272
249#define vecvalue(p) ((p)->object.vector.vvalue) 273#define vecvalue(p) ((p)->object.vector.vvalue)
250#define veclength(p) ((p)->object.vector.length) 274#define veclength(p) ((p)->object.vector.length)
251INTERFACE void fill_vector (pointer vec, pointer obj); 275INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
252INTERFACE uint32_t vector_length (pointer vec);
253INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); 276INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
254INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); 277INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
255 278
256INTERFACE uint32_t 279INTERFACE int
257vector_length (pointer vec) 280is_integer (pointer p)
258{ 281{
259 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;
260} 290}
261 291
262INTERFACE int 292INTERFACE int
263is_number (pointer p) 293is_number (pointer p)
264{ 294{
265 return type (p) == T_NUMBER; 295 return is_integer (p) || is_real (p);
266}
267
268INTERFACE int
269is_integer (pointer p)
270{
271 return is_number (p) && num_is_fixnum (p->object.number);
272}
273
274INTERFACE int
275is_real (pointer p)
276{
277 return is_number (p) && !num_is_fixnum (p->object.number);
278} 296}
279 297
280INTERFACE int 298INTERFACE int
281is_character (pointer p) 299is_character (pointer p)
282{ 300{
287string_value (pointer p) 305string_value (pointer p)
288{ 306{
289 return strvalue (p); 307 return strvalue (p);
290} 308}
291 309
292ecb_inline num
293nvalue (pointer p)
294{
295 return (p)->object.number;
296}
297
298static IVALUE
299num_get_ivalue (const num n)
300{
301 return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n);
302}
303
304static RVALUE
305num_get_rvalue (const num n)
306{
307 return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n);
308}
309
310INTERFACE IVALUE
311ivalue (pointer p)
312{
313 return num_get_ivalue (p->object.number);
314}
315
316INTERFACE RVALUE
317rvalue (pointer p)
318{
319 return num_get_rvalue (p->object.number);
320}
321
322#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
323#if USE_REAL 313#if USE_REAL
324# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 314#define rvalue_unchecked(p) (p)->object.rvalue
325# define set_num_integer(p) (p)->object.number.is_fixnum=1; 315#define set_rvalue(p,v) (p)->object.rvalue = (v)
326# define set_num_real(p) (p)->object.number.is_fixnum=0;
327#else 316#else
328# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 317#define rvalue_unchecked(p) (p)->object.ivalue
329# define set_num_integer(p) 0 318#define set_rvalue(p,v) (p)->object.ivalue = (v)
330# define set_num_real(p) 0
331#endif 319#endif
332 320
333INTERFACE long 321INTERFACE long
334charvalue (pointer p) 322charvalue (pointer p)
335{ 323{
440syntaxname (pointer p) 428syntaxname (pointer p)
441{ 429{
442 return strvalue (car (p)); 430 return strvalue (car (p));
443} 431}
444 432
445#define procnum(p) ivalue (p) 433#define procnum(p) ivalue_unchecked (p)
446static const char *procname (pointer x); 434static const char *procname (pointer x);
447 435
448INTERFACE int 436INTERFACE int
449is_closure (pointer p) 437is_closure (pointer p)
450{ 438{
511setimmutable (pointer p) 499setimmutable (pointer p)
512{ 500{
513#if USE_ERROR_CHECKING 501#if USE_ERROR_CHECKING
514 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 502 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
515#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;
516} 558}
517 559
518#if USE_CHAR_CLASSIFIERS 560#if USE_CHAR_CLASSIFIERS
519ecb_inline int 561ecb_inline int
520Cisalpha (int c) 562Cisalpha (int c)
664static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 706static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
665static void assign_syntax (SCHEME_P_ const char *name); 707static void assign_syntax (SCHEME_P_ const char *name);
666static int syntaxnum (pointer p); 708static int syntaxnum (pointer p);
667static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 709static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
668 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
669static num 738static num
670num_op (enum num_op op, num a, num b) 739num_op (enum num_op op, num a, num b)
671{ 740{
672 num ret; 741 num ret;
673 742
674 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));
675 744
676 if (num_is_fixnum (ret)) 745 if (num_is_fixnum (ret))
677 { 746 {
678 IVALUE av = num_get_ivalue (a);
679 IVALUE bv = num_get_ivalue (b);
680
681 switch (op) 747 switch (op)
682 { 748 {
683 case NUM_ADD: av += bv; break; 749 case NUM_ADD: a.ivalue += b.ivalue; break;
684 case NUM_SUB: av -= bv; break; 750 case NUM_SUB: a.ivalue -= b.ivalue; break;
685 case NUM_MUL: av *= bv; break; 751 case NUM_MUL: a.ivalue *= b.ivalue; break;
686 case NUM_INTDIV: av /= bv; break; 752 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
687 } 753 }
688 754
689 num_set_ivalue (ret, av); 755 num_set_ivalue (ret, a.ivalue);
690 } 756 }
757#if USE_REAL
691 else 758 else
692 { 759 {
693 RVALUE av = num_get_rvalue (a);
694 RVALUE bv = num_get_rvalue (b);
695
696 switch (op) 760 switch (op)
697 { 761 {
698 case NUM_ADD: av += bv; break; 762 case NUM_ADD: a.rvalue += b.rvalue; break;
699 case NUM_SUB: av -= bv; break; 763 case NUM_SUB: a.rvalue -= b.rvalue; break;
700 case NUM_MUL: av *= bv; break; 764 case NUM_MUL: a.rvalue *= b.rvalue; break;
701 case NUM_INTDIV: av /= bv; break; 765 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
702 } 766 }
703 767
704 num_set_rvalue (ret, av); 768 num_set_rvalue (ret, a.rvalue);
705 } 769 }
770#endif
706 771
707 return ret; 772 return ret;
708} 773}
709 774
710static num 775static num
711num_div (num a, num b) 776num_div (num a, num b)
712{ 777{
713 num ret; 778 num ret;
714 779
715 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);
716 781
717 if (num_is_fixnum (ret)) 782 if (num_is_fixnum (ret))
718 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 783 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
719 else 784 else
720 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 785 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
721 786
722 return ret; 787 return ret;
723} 788}
724 789
725static num 790static num
727{ 792{
728 num ret; 793 num ret;
729 long e1, e2, res; 794 long e1, e2, res;
730 795
731 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));
732 e1 = num_get_ivalue (a); 797 e1 = num_ivalue (a);
733 e2 = num_get_ivalue (b); 798 e2 = num_ivalue (b);
734 res = e1 % e2; 799 res = e1 % e2;
735 800
736 /* remainder should have same sign as second operand */ 801 /* remainder should have same sign as second operand */
737 if (res > 0) 802 if (res > 0)
738 { 803 {
754{ 819{
755 num ret; 820 num ret;
756 long e1, e2, res; 821 long e1, e2, res;
757 822
758 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));
759 e1 = num_get_ivalue (a); 824 e1 = num_ivalue (a);
760 e2 = num_get_ivalue (b); 825 e2 = num_ivalue (b);
761 res = e1 % e2; 826 res = e1 % e2;
762 827
763 /* modulo should have same sign as second operand */ 828 /* modulo should have same sign as second operand */
764 if (res * e2 < 0) 829 if (res * e2 < 0)
765 res += e2; 830 res += e2;
775 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 840 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
776 int ret; 841 int ret;
777 842
778 if (is_fixnum) 843 if (is_fixnum)
779 { 844 {
780 IVALUE av = num_get_ivalue (a); 845 IVALUE av = num_ivalue (a);
781 IVALUE bv = num_get_ivalue (b); 846 IVALUE bv = num_ivalue (b);
782 847
783 ret = av == bv ? 0 : av < bv ? -1 : +1; 848 ret = av == bv ? 0 : av < bv ? -1 : +1;
784 } 849 }
785 else 850 else
786 { 851 {
787 RVALUE av = num_get_rvalue (a); 852 RVALUE av = num_rvalue (a);
788 RVALUE bv = num_get_rvalue (b); 853 RVALUE bv = num_rvalue (b);
789 854
790 ret = av == bv ? 0 : av < bv ? -1 : +1; 855 ret = av == bv ? 0 : av < bv ? -1 : +1;
791 } 856 }
792 857
793 return ret; 858 return ret;
856 return k; 921 return k;
857 922
858 i = ++SCHEME_V->last_cell_seg; 923 i = ++SCHEME_V->last_cell_seg;
859 SCHEME_V->alloc_seg[i] = cp; 924 SCHEME_V->alloc_seg[i] = cp;
860 925
861 /* insert new segment in address order */
862 newp = (pointer)cp; 926 newp = (pointer)cp;
863 SCHEME_V->cell_seg[i] = newp; 927 SCHEME_V->cell_seg[i] = newp;
864 SCHEME_V->cell_segsize[i] = segsize; 928 SCHEME_V->cell_segsize[i] = segsize;
865
866 //TODO: insert, not swap
867 while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i])
868 {
869 p = SCHEME_V->cell_seg[i];
870 SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1];
871 SCHEME_V->cell_seg[i - 1] = p;
872
873 k = SCHEME_V->cell_segsize[i];
874 SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1];
875 SCHEME_V->cell_segsize[i - 1] = k;
876
877 --i;
878 }
879
880 SCHEME_V->fcells += segsize; 929 SCHEME_V->fcells += segsize;
881 last = newp + segsize - 1; 930 last = newp + segsize - 1;
882 931
883 for (p = newp; p <= last; p++) 932 for (p = newp; p <= last; p++)
884 { 933 {
885 set_typeflag (p, T_FREE); 934 set_typeflag (p, T_PAIR);
886 set_car (p, NIL); 935 set_car (p, NIL);
887 set_cdr (p, p + 1); 936 set_cdr (p, p + 1);
888 } 937 }
889 938
890 /* insert new cells in address order on free list */
891 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell)
892 {
893 set_cdr (last, SCHEME_V->free_cell); 939 set_cdr (last, SCHEME_V->free_cell);
894 SCHEME_V->free_cell = newp; 940 SCHEME_V->free_cell = newp;
895 }
896 else
897 {
898 p = SCHEME_V->free_cell;
899
900 while (cdr (p) != NIL && newp > cdr (p))
901 p = cdr (p);
902
903 set_cdr (last, cdr (p));
904 set_cdr (p, newp);
905 }
906 } 941 }
907 942
908 return n; 943 return n;
909} 944}
910 945
989 /* Record it as a vector so that gc understands it. */ 1024 /* Record it as a vector so that gc understands it. */
990 set_typeflag (v, T_VECTOR | T_ATOM); 1025 set_typeflag (v, T_VECTOR | T_ATOM);
991 1026
992 v->object.vector.vvalue = e; 1027 v->object.vector.vvalue = e;
993 v->object.vector.length = len; 1028 v->object.vector.length = len;
994 fill_vector (v, init); 1029 fill_vector (v, 0, init);
995 push_recent_alloc (SCHEME_A_ v, NIL); 1030 push_recent_alloc (SCHEME_A_ v, NIL);
996 1031
997 return v; 1032 return v;
998} 1033}
999 1034
1065 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); 1100 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1066 set_typeflag (x, T_SYMBOL); 1101 set_typeflag (x, T_SYMBOL);
1067 setimmutable (car (x)); 1102 setimmutable (car (x));
1068 1103
1069 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1104 location = hash_fn (name, veclength (SCHEME_V->oblist));
1070 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); 1105 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1071 return x; 1106 return x;
1072} 1107}
1073 1108
1074ecb_inline pointer 1109ecb_inline pointer
1075oblist_find_by_name (SCHEME_P_ const char *name) 1110oblist_find_by_name (SCHEME_P_ const char *name)
1078 pointer x; 1113 pointer x;
1079 char *s; 1114 char *s;
1080 1115
1081 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1116 location = hash_fn (name, veclength (SCHEME_V->oblist));
1082 1117
1083 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1118 for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1084 { 1119 {
1085 s = symname (car (x)); 1120 s = symname (car (x));
1086 1121
1087 /* case-insensitive, per R5RS section 2 */ 1122 /* case-insensitive, per R5RS section 2 */
1088 if (stricmp (name, s) == 0) 1123 if (stricmp (name, s) == 0)
1098 int i; 1133 int i;
1099 pointer x; 1134 pointer x;
1100 pointer ob_list = NIL; 1135 pointer ob_list = NIL;
1101 1136
1102 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1137 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1103 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1138 for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1104 ob_list = cons (x, ob_list); 1139 ob_list = cons (x, ob_list);
1105 1140
1106 return ob_list; 1141 return ob_list;
1107} 1142}
1108 1143
1181mk_character (SCHEME_P_ int c) 1216mk_character (SCHEME_P_ int c)
1182{ 1217{
1183 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1218 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1184 1219
1185 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1220 set_typeflag (x, (T_CHARACTER | T_ATOM));
1186 ivalue_unchecked (x) = c & 0xff; 1221 set_ivalue (x, c & 0xff);
1187 set_num_integer (x); 1222
1188 return x; 1223 return x;
1189} 1224}
1190 1225
1191/* get number atom (integer) */ 1226/* get number atom (integer) */
1192INTERFACE pointer 1227INTERFACE pointer
1193mk_integer (SCHEME_P_ long num) 1228mk_integer (SCHEME_P_ long n)
1194{ 1229{
1195 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1230 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1196 1231
1197 set_typeflag (x, (T_NUMBER | T_ATOM)); 1232 set_typeflag (x, (T_INTEGER | T_ATOM));
1198 ivalue_unchecked (x) = num; 1233 set_ivalue (x, n);
1199 set_num_integer (x); 1234
1200 return x; 1235 return x;
1201} 1236}
1202 1237
1203INTERFACE pointer 1238INTERFACE pointer
1204mk_real (SCHEME_P_ RVALUE n) 1239mk_real (SCHEME_P_ RVALUE n)
1205{ 1240{
1241#if USE_REAL
1206 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1242 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1207 1243
1208 set_typeflag (x, (T_NUMBER | T_ATOM)); 1244 set_typeflag (x, (T_REAL | T_ATOM));
1209 rvalue_unchecked (x) = n; 1245 set_rvalue (x, n);
1210 set_num_real (x); 1246
1211 return x; 1247 return x;
1248#else
1249 return mk_integer (SCHEME_A_ n);
1250#endif
1212} 1251}
1213 1252
1214static pointer 1253static pointer
1215mk_number (SCHEME_P_ const num n) 1254mk_number (SCHEME_P_ const num n)
1216{ 1255{
1256#if USE_REAL
1217 if (num_is_fixnum (n)) 1257 return num_is_fixnum (n)
1258 ? mk_integer (SCHEME_A_ num_ivalue (n))
1259 : mk_real (SCHEME_A_ num_rvalue (n));
1260#else
1218 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1261 return mk_integer (SCHEME_A_ num_ivalue (n));
1219 else 1262#endif
1220 return mk_real (SCHEME_A_ num_get_rvalue (n));
1221} 1263}
1222 1264
1223/* allocate name to string area */ 1265/* allocate name to string area */
1224static char * 1266static char *
1225store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) 1267store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1284{ 1326{
1285 return get_vector_object (SCHEME_A_ len, NIL); 1327 return get_vector_object (SCHEME_A_ len, NIL);
1286} 1328}
1287 1329
1288INTERFACE void 1330INTERFACE void
1289fill_vector (pointer vec, pointer obj) 1331fill_vector (pointer vec, uint32_t start, pointer obj)
1290{ 1332{
1291 int i; 1333 int i;
1292 1334
1293 for (i = 0; i < vec->object.vector.length; i++) 1335 for (i = start; i < veclength (vec); i++)
1294 vecvalue (vec)[i] = obj; 1336 vecvalue (vec)[i] = obj;
1295} 1337}
1296 1338
1297INTERFACE pointer 1339INTERFACE pointer
1298vector_elem (pointer vec, uint32_t ielem) 1340vector_get (pointer vec, uint32_t ielem)
1299{ 1341{
1300 return vecvalue(vec)[ielem]; 1342 return vecvalue(vec)[ielem];
1301} 1343}
1302 1344
1303INTERFACE void 1345INTERFACE void
1304set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1346vector_set (pointer vec, uint32_t ielem, pointer a)
1305{ 1347{
1306 vecvalue(vec)[ielem] = a; 1348 vecvalue(vec)[ielem] = a;
1307} 1349}
1308 1350
1309/* get new symbol */ 1351/* get new symbol */
1401 } 1443 }
1402 else if ((c == 'e') || (c == 'E')) 1444 else if ((c == 'e') || (c == 'E'))
1403 { 1445 {
1404 if (!has_fp_exp) 1446 if (!has_fp_exp)
1405 { 1447 {
1406 has_dec_point = 1; /* decimal point illegal 1448 has_dec_point = 1; /* decimal point illegal from now on */
1407 from now on */
1408 p++; 1449 p++;
1409 1450
1410 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1451 if ((*p == '-') || (*p == '+') || isdigit (*p))
1411 continue; 1452 continue;
1412 } 1453 }
1500 1541
1501 if (ecb_expect_false (is_vector (p))) 1542 if (ecb_expect_false (is_vector (p)))
1502 { 1543 {
1503 int i; 1544 int i;
1504 1545
1505 for (i = 0; i < p->object.vector.length; i++) 1546 for (i = 0; i < veclength (p); i++)
1506 mark (vecvalue (p)[i]); 1547 mark (vecvalue (p)[i]);
1507 } 1548 }
1508 1549
1509 if (is_atom (p)) 1550 if (is_atom (p))
1510 goto E6; 1551 goto E6;
1608 if (is_mark (p)) 1649 if (is_mark (p))
1609 clrmark (p); 1650 clrmark (p);
1610 else 1651 else
1611 { 1652 {
1612 /* reclaim cell */ 1653 /* reclaim cell */
1613 if (typeflag (p) != T_FREE) 1654 if (typeflag (p) != T_PAIR)
1614 { 1655 {
1615 finalize_cell (SCHEME_A_ p); 1656 finalize_cell (SCHEME_A_ p);
1616 set_typeflag (p, T_FREE); 1657 set_typeflag (p, T_PAIR);
1617 set_car (p, NIL); 1658 set_car (p, NIL);
1618 } 1659 }
1619 1660
1620 ++SCHEME_V->fcells; 1661 ++SCHEME_V->fcells;
1621 set_cdr (p, SCHEME_V->free_cell); 1662 set_cdr (p, SCHEME_V->free_cell);
1623 } 1664 }
1624 } 1665 }
1625 } 1666 }
1626 1667
1627 if (SCHEME_V->gc_verbose) 1668 if (SCHEME_V->gc_verbose)
1669 {
1628 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1670 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1671 }
1629} 1672}
1630 1673
1631static void 1674static void
1632finalize_cell (SCHEME_P_ pointer a) 1675finalize_cell (SCHEME_P_ pointer a)
1633{ 1676{
2819 2862
2820 if (is_vector (car (env))) 2863 if (is_vector (car (env)))
2821 { 2864 {
2822 int location = hash_fn (symname (variable), veclength (car (env))); 2865 int location = hash_fn (symname (variable), veclength (car (env)));
2823 2866
2824 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2867 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2825 } 2868 }
2826 else 2869 else
2827 set_car (env, immutable_cons (slot, car (env))); 2870 set_car (env, immutable_cons (slot, car (env)));
2828} 2871}
2829 2872
2836 for (x = env; x != NIL; x = cdr (x)) 2879 for (x = env; x != NIL; x = cdr (x))
2837 { 2880 {
2838 if (is_vector (car (x))) 2881 if (is_vector (car (x)))
2839 { 2882 {
2840 location = hash_fn (symname (hdl), veclength (car (x))); 2883 location = hash_fn (symname (hdl), veclength (car (x)));
2841 y = vector_elem (car (x), location); 2884 y = vector_get (car (x), location);
2842 } 2885 }
2843 else 2886 else
2844 y = car (x); 2887 y = car (x);
2845 2888
2846 for (; y != NIL; y = cdr (y)) 2889 for (; y != NIL; y = cdr (y))
3119 int i = 0; 3162 int i = 0;
3120 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3163 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3121 3164
3122 while (cont != NIL) 3165 while (cont != NIL)
3123 { 3166 {
3124 frame->op = ivalue (car (cont)); cont = cdr (cont); 3167 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3125 frame->args = car (cont) ; cont = cdr (cont); 3168 frame->args = car (cont) ; cont = cdr (cont);
3126 frame->envir = car (cont) ; cont = cdr (cont); 3169 frame->envir = car (cont) ; cont = cdr (cont);
3127 frame->code = car (cont) ; cont = cdr (cont); 3170 frame->code = car (cont) ; cont = cdr (cont);
3128 3171
3129 ++frame; 3172 ++frame;
3130 ++i; 3173 ++i;
3131 } 3174 }
3132 3175
3161 SCHEME_V->value = a; 3204 SCHEME_V->value = a;
3162 3205
3163 if (dump == NIL) 3206 if (dump == NIL)
3164 return -1; 3207 return -1;
3165 3208
3166 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3209 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3167 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3210 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3168 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3211 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3169 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3212 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3170 3213
3171 SCHEME_V->dump = dump; 3214 SCHEME_V->dump = dump;
3172 3215
3173 return 0; 3216 return 0;
3174} 3217}
3380 3423
3381 case OP_TRACING: 3424 case OP_TRACING:
3382 { 3425 {
3383 int tr = SCHEME_V->tracing; 3426 int tr = SCHEME_V->tracing;
3384 3427
3385 SCHEME_V->tracing = ivalue (car (args)); 3428 SCHEME_V->tracing = ivalue_unchecked (car (args));
3386 s_return (mk_integer (SCHEME_A_ tr)); 3429 s_return (mk_integer (SCHEME_A_ tr));
3387 } 3430 }
3388 3431
3389#endif 3432#endif
3390 3433
3909{ 3952{
3910 pointer args = SCHEME_V->args; 3953 pointer args = SCHEME_V->args;
3911 pointer x = car (args); 3954 pointer x = car (args);
3912 num v; 3955 num v;
3913 3956
3914#if USE_MATH
3915 RVALUE dd;
3916#endif
3917
3918 switch (op) 3957 switch (op)
3919 { 3958 {
3920#if USE_MATH 3959#if USE_MATH
3921 case OP_INEX2EX: /* inexact->exact */ 3960 case OP_INEX2EX: /* inexact->exact */
3961 {
3922 if (is_integer (x)) 3962 if (is_integer (x))
3923 s_return (x); 3963 s_return (x);
3924 else if (modf (rvalue_unchecked (x), &dd) == 0) 3964
3965 RVALUE r = rvalue_unchecked (x);
3966
3967 if (r == (RVALUE)(IVALUE)r)
3925 s_return (mk_integer (SCHEME_A_ ivalue (x))); 3968 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3926 else 3969 else
3927 Error_1 ("inexact->exact: not integral:", x); 3970 Error_1 ("inexact->exact: not integral:", x);
3971 }
3928 3972
3929 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 3973 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
3930 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 3974 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))));
3931 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 3975 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
3932 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 3976 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3966 /* If the test fails, result is too big for integer. */ 4010 /* If the test fails, result is too big for integer. */
3967 if (!real_result) 4011 if (!real_result)
3968 { 4012 {
3969 long result_as_long = result; 4013 long result_as_long = result;
3970 4014
3971 if (result != (RVALUE) result_as_long) 4015 if (result != result_as_long)
3972 real_result = 1; 4016 real_result = 1;
3973 } 4017 }
3974 4018
3975 if (real_result) 4019 if (real_result)
3976 s_return (mk_real (SCHEME_A_ result)); 4020 s_return (mk_real (SCHEME_A_ result));
3981 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4025 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
3982 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); 4026 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
3983 4027
3984 case OP_TRUNCATE: 4028 case OP_TRUNCATE:
3985 { 4029 {
3986 RVALUE rvalue_of_x; 4030 RVALUE n = rvalue (x);
3987
3988 rvalue_of_x = rvalue (x);
3989
3990 if (rvalue_of_x > 0)
3991 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); 4031 s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n)));
3992 else
3993 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
3994 } 4032 }
3995 4033
3996 case OP_ROUND: 4034 case OP_ROUND:
3997 if (num_is_integer (x)) 4035 if (is_integer (x))
3998 s_return (x); 4036 s_return (x);
3999 4037
4000 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4038 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4001#endif 4039#endif
4002 4040
4122 } 4160 }
4123 else 4161 else
4124 Error_0 ("set-cdr!: unable to alter immutable pair"); 4162 Error_0 ("set-cdr!: unable to alter immutable pair");
4125 4163
4126 case OP_CHAR2INT: /* char->integer */ 4164 case OP_CHAR2INT: /* char->integer */
4127 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4165 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4128 4166
4129 case OP_INT2CHAR: /* integer->char */ 4167 case OP_INT2CHAR: /* integer->char */
4130 s_return (mk_character (SCHEME_A_ ivalue (x))); 4168 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4131 4169
4132 case OP_CHARUPCASE: 4170 case OP_CHARUPCASE:
4133 { 4171 {
4134 unsigned char c = ivalue (x); 4172 unsigned char c = ivalue_unchecked (x);
4135 c = toupper (c); 4173 c = toupper (c);
4136 s_return (mk_character (SCHEME_A_ c)); 4174 s_return (mk_character (SCHEME_A_ c));
4137 } 4175 }
4138 4176
4139 case OP_CHARDNCASE: 4177 case OP_CHARDNCASE:
4140 { 4178 {
4141 unsigned char c = ivalue (x); 4179 unsigned char c = ivalue_unchecked (x);
4142 c = tolower (c); 4180 c = tolower (c);
4143 s_return (mk_character (SCHEME_A_ c)); 4181 s_return (mk_character (SCHEME_A_ c));
4144 } 4182 }
4145 4183
4146 case OP_STR2SYM: /* string->symbol */ 4184 case OP_STR2SYM: /* string->symbol */
4223 Error_1 ("atom->string: not an atom:", x); 4261 Error_1 ("atom->string: not an atom:", x);
4224 } 4262 }
4225 4263
4226 case OP_MKSTRING: /* make-string */ 4264 case OP_MKSTRING: /* make-string */
4227 { 4265 {
4228 int fill = ' '; 4266 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4229 int len;
4230
4231 len = ivalue (x); 4267 int len = ivalue_unchecked (x);
4232
4233 if (cdr (args) != NIL)
4234 fill = charvalue (cadr (args));
4235 4268
4236 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4269 s_return (mk_empty_string (SCHEME_A_ len, fill));
4237 } 4270 }
4238 4271
4239 case OP_STRLEN: /* string-length */ 4272 case OP_STRLEN: /* string-length */
4240 s_return (mk_integer (SCHEME_A_ strlength (x))); 4273 s_return (mk_integer (SCHEME_A_ strlength (x)));
4241 4274
4242 case OP_STRREF: /* string-ref */ 4275 case OP_STRREF: /* string-ref */
4243 { 4276 {
4244 char *str;
4245 int index;
4246
4247 str = strvalue (x); 4277 char *str = strvalue (x);
4248
4249 index = ivalue (cadr (args)); 4278 int index = ivalue_unchecked (cadr (args));
4250 4279
4251 if (index >= strlength (x)) 4280 if (index >= strlength (x))
4252 Error_1 ("string-ref: out of bounds:", cadr (args)); 4281 Error_1 ("string-ref: out of bounds:", cadr (args));
4253 4282
4254 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4283 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4255 } 4284 }
4256 4285
4257 case OP_STRSET: /* string-set! */ 4286 case OP_STRSET: /* string-set! */
4258 { 4287 {
4259 char *str; 4288 char *str = strvalue (x);
4260 int index; 4289 int index = ivalue_unchecked (cadr (args));
4261 int c; 4290 int c;
4262 4291
4263 if (is_immutable (x)) 4292 if (is_immutable (x))
4264 Error_1 ("string-set!: unable to alter immutable string:", x); 4293 Error_1 ("string-set!: unable to alter immutable string:", x);
4265
4266 str = strvalue (x);
4267
4268 index = ivalue (cadr (args));
4269 4294
4270 if (index >= strlength (x)) 4295 if (index >= strlength (x))
4271 Error_1 ("string-set!: out of bounds:", cadr (args)); 4296 Error_1 ("string-set!: out of bounds:", cadr (args));
4272 4297
4273 c = charvalue (caddr (args)); 4298 c = charvalue (caddr (args));
4296 s_return (newstr); 4321 s_return (newstr);
4297 } 4322 }
4298 4323
4299 case OP_SUBSTR: /* substring */ 4324 case OP_SUBSTR: /* substring */
4300 { 4325 {
4301 char *str; 4326 char *str = strvalue (x);
4302 int index0; 4327 int index0 = ivalue_unchecked (cadr (args));
4303 int index1; 4328 int index1;
4304 int len; 4329 int len;
4305 4330
4306 str = strvalue (x);
4307
4308 index0 = ivalue (cadr (args));
4309
4310 if (index0 > strlength (x)) 4331 if (index0 > strlength (x))
4311 Error_1 ("substring: start out of bounds:", cadr (args)); 4332 Error_1 ("substring: start out of bounds:", cadr (args));
4312 4333
4313 if (cddr (args) != NIL) 4334 if (cddr (args) != NIL)
4314 { 4335 {
4315 index1 = ivalue (caddr (args)); 4336 index1 = ivalue_unchecked (caddr (args));
4316 4337
4317 if (index1 > strlength (x) || index1 < index0) 4338 if (index1 > strlength (x) || index1 < index0)
4318 Error_1 ("substring: end out of bounds:", caddr (args)); 4339 Error_1 ("substring: end out of bounds:", caddr (args));
4319 } 4340 }
4320 else 4341 else
4343 if (SCHEME_V->no_memory) 4364 if (SCHEME_V->no_memory)
4344 s_return (S_SINK); 4365 s_return (S_SINK);
4345#endif 4366#endif
4346 4367
4347 for (x = args, i = 0; is_pair (x); x = cdr (x), i++) 4368 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4348 set_vector_elem (vec, i, car (x)); 4369 vector_set (vec, i, car (x));
4349 4370
4350 s_return (vec); 4371 s_return (vec);
4351 } 4372 }
4352 4373
4353 case OP_MKVECTOR: /* make-vector */ 4374 case OP_MKVECTOR: /* make-vector */
4354 { 4375 {
4355 pointer fill = NIL; 4376 pointer fill = NIL;
4356 int len;
4357 pointer vec; 4377 pointer vec;
4358
4359 len = ivalue (x); 4378 int len = ivalue_unchecked (x);
4360 4379
4361 if (cdr (args) != NIL) 4380 if (cdr (args) != NIL)
4362 fill = cadr (args); 4381 fill = cadr (args);
4363 4382
4364 vec = mk_vector (SCHEME_A_ len); 4383 vec = mk_vector (SCHEME_A_ len);
4367 if (SCHEME_V->no_memory) 4386 if (SCHEME_V->no_memory)
4368 s_return (S_SINK); 4387 s_return (S_SINK);
4369#endif 4388#endif
4370 4389
4371 if (fill != NIL) 4390 if (fill != NIL)
4372 fill_vector (vec, fill); 4391 fill_vector (vec, 0, fill);
4373 4392
4374 s_return (vec); 4393 s_return (vec);
4375 } 4394 }
4376 4395
4377 case OP_VECLEN: /* vector-length */ 4396 case OP_VECLEN: /* vector-length */
4378 s_return (mk_integer (SCHEME_A_ veclength (x))); 4397 s_return (mk_integer (SCHEME_A_ veclength (x)));
4379 4398
4380 case OP_VECREF: /* vector-ref */ 4399 case OP_VECREF: /* vector-ref */
4381 { 4400 {
4382 int index;
4383
4384 index = ivalue (cadr (args)); 4401 int index = ivalue_unchecked (cadr (args));
4385 4402
4386 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4403 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4387 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4404 Error_1 ("vector-ref: out of bounds:", cadr (args));
4388 4405
4389 s_return (vector_elem (x, index)); 4406 s_return (vector_get (x, index));
4390 } 4407 }
4391 4408
4392 case OP_VECSET: /* vector-set! */ 4409 case OP_VECSET: /* vector-set! */
4393 { 4410 {
4394 int index; 4411 int index = ivalue_unchecked (cadr (args));
4395 4412
4396 if (is_immutable (x)) 4413 if (is_immutable (x))
4397 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4414 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4398 4415
4399 index = ivalue (cadr (args));
4400
4401 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4416 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4402 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4417 Error_1 ("vector-set!: out of bounds:", cadr (args));
4403 4418
4404 set_vector_elem (x, index, caddr (args)); 4419 vector_set (x, index, caddr (args));
4405 s_return (x); 4420 s_return (x);
4406 } 4421 }
4407 } 4422 }
4408 4423
4409 if (USE_ERROR_CHECKING) abort (); 4424 if (USE_ERROR_CHECKING) abort ();
4410}
4411
4412INTERFACE int
4413is_list (SCHEME_P_ pointer a)
4414{
4415 return list_length (SCHEME_A_ a) >= 0;
4416}
4417
4418/* Result is:
4419 proper list: length
4420 circular list: -1
4421 not even a pair: -2
4422 dotted list: -2 minus length before dot
4423*/
4424INTERFACE int
4425list_length (SCHEME_P_ pointer a)
4426{
4427 int i = 0;
4428 pointer slow, fast;
4429
4430 slow = fast = a;
4431
4432 while (1)
4433 {
4434 if (fast == NIL)
4435 return i;
4436
4437 if (!is_pair (fast))
4438 return -2 - i;
4439
4440 fast = cdr (fast);
4441 ++i;
4442
4443 if (fast == NIL)
4444 return i;
4445
4446 if (!is_pair (fast))
4447 return -2 - i;
4448
4449 ++i;
4450 fast = cdr (fast);
4451
4452 /* Safe because we would have already returned if `fast'
4453 encountered a non-pair. */
4454 slow = cdr (slow);
4455
4456 if (fast == slow)
4457 {
4458 /* the fast pointer has looped back around and caught up
4459 with the slow pointer, hence the structure is circular,
4460 not of finite length, and therefore not a list */
4461 return -1;
4462 }
4463 }
4464} 4425}
4465 4426
4466static int 4427static int
4467opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4428opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4468{ 4429{
4514 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4475 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4515 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4476 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4516 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4477 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4517 4478
4518#if USE_CHAR_CLASSIFIERS 4479#if USE_CHAR_CLASSIFIERS
4519 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4480 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4520 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4481 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4521 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4482 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4522 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4483 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4523 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4484 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4524#endif 4485#endif
4525 4486
4526#if USE_PORTS 4487#if USE_PORTS
4527 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4488 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4528 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4489 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4729 4690
4730 case OP_NEWSEGMENT: /* new-segment */ 4691 case OP_NEWSEGMENT: /* new-segment */
4731 if (!is_pair (args) || !is_number (a)) 4692 if (!is_pair (args) || !is_number (a))
4732 Error_0 ("new-segment: argument must be a number"); 4693 Error_0 ("new-segment: argument must be a number");
4733 4694
4734 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 4695 alloc_cellseg (SCHEME_A_ ivalue (a));
4735 4696
4736 s_return (S_T); 4697 s_return (S_T);
4737 4698
4738 case OP_OBLIST: /* oblist */ 4699 case OP_OBLIST: /* oblist */
4739 s_return (oblist_all_symbols (SCHEME_A)); 4700 s_return (oblist_all_symbols (SCHEME_A));
5184 putstr (SCHEME_A_ ")"); 5145 putstr (SCHEME_A_ ")");
5185 s_return (S_T); 5146 s_return (S_T);
5186 } 5147 }
5187 else 5148 else
5188 { 5149 {
5189 pointer elem = vector_elem (vec, i); 5150 pointer elem = vector_get (vec, i);
5190 5151
5191 ivalue_unchecked (cdr (args)) = i + 1; 5152 ivalue_unchecked (cdr (args)) = i + 1;
5192 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5153 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5193 SCHEME_V->args = elem; 5154 SCHEME_V->args = elem;
5194 5155
5269/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5230/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5270typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); 5231typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5271 5232
5272typedef int (*test_predicate)(pointer); 5233typedef int (*test_predicate)(pointer);
5273static int 5234static int
5274is_any (pointer p) 5235tst_any (pointer p)
5275{ 5236{
5276 return 1; 5237 return 1;
5277} 5238}
5278 5239
5279static int 5240static int
5280is_nonneg (pointer p) 5241tst_inonneg (pointer p)
5281{ 5242{
5282 return ivalue (p) >= 0 && is_integer (p); 5243 return is_integer (p) && ivalue_unchecked (p) >= 0;
5283} 5244}
5284 5245
5285static int 5246static int
5286tst_is_list (pointer p) 5247tst_is_list (SCHEME_P_ pointer p)
5287{ 5248{
5288 return p == NIL || is_pair (p); 5249 return p == NIL || is_pair (p);
5289} 5250}
5290 5251
5291/* Correspond carefully with following defines! */ 5252/* Correspond carefully with following defines! */
5292static struct 5253static struct
5293{ 5254{
5294 test_predicate fct; 5255 test_predicate fct;
5295 const char *kind; 5256 const char *kind;
5296} tests[] = 5257} tests[] = {
5297{
5298 { is_any, 0 }, 5258 { tst_any , 0 },
5299 { is_string, "string" }, 5259 { is_string , "string" },
5300 { is_symbol, "symbol" }, 5260 { is_symbol , "symbol" },
5301 { is_port, "port" }, 5261 { is_port , "port" },
5302 { is_inport, "input port" }, 5262 { is_inport , "input port" },
5303 { is_outport, "output port" }, 5263 { is_outport , "output port" },
5304 { is_environment, "environment" }, 5264 { is_environment, "environment" },
5305 { is_pair, "pair" }, 5265 { is_pair , "pair" },
5306 { tst_is_list, "pair or '()" }, 5266 { 0 , "pair or '()" },
5307 { is_character, "character" }, 5267 { is_character , "character" },
5308 { is_vector, "vector" }, 5268 { is_vector , "vector" },
5309 { is_number, "number" }, 5269 { is_number , "number" },
5310 { is_integer, "integer" }, 5270 { is_integer , "integer" },
5311 { is_nonneg, "non-negative integer" } 5271 { tst_inonneg , "non-negative integer" }
5312}; 5272};
5313 5273
5314#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ 5274#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5315#define TST_ANY "\001" 5275#define TST_ANY "\001"
5316#define TST_STRING "\002" 5276#define TST_STRING "\002"
5357typedef struct 5317typedef struct
5358{ 5318{
5359 uint8_t func; 5319 uint8_t func;
5360 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ 5320 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5361 uint8_t builtin; 5321 uint8_t builtin;
5322#if USE_ERROR_CHECKING
5362 uint8_t min_arity; 5323 uint8_t min_arity;
5363 uint8_t max_arity; 5324 uint8_t max_arity;
5364 char arg_tests_encoding[3]; 5325 char arg_tests_encoding[3];
5326#endif
5365} op_code_info; 5327} op_code_info;
5366 5328
5367static const op_code_info dispatch_table[] = { 5329static const op_code_info dispatch_table[] = {
5330#if USE_ERROR_CHECKING
5368#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, 5331#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5332#else
5333#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5334#endif
5369#include "opdefines.h" 5335#include "opdefines.h"
5370#undef OP_DEF 5336#undef OP_DEF
5371 {0} 5337 {0}
5372}; 5338};
5373 5339
5415 { 5381 {
5416 pointer arg = car (arglist); 5382 pointer arg = car (arglist);
5417 5383
5418 j = t[0]; 5384 j = t[0];
5419 5385
5386 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5387 if (j == TST_LIST[0])
5388 {
5389 if (!tst_is_list (SCHEME_A_ arg))
5390 break;
5391 }
5392 else
5393 {
5420 if (!tests[j - 1].fct (arg)) 5394 if (!tests[j - 1].fct (arg))
5421 break; 5395 break;
5396 }
5422 5397
5423 if (t[1]) /* last test is replicated as necessary */ 5398 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5424 t++; 5399 t++;
5425 5400
5426 arglist = cdr (arglist); 5401 arglist = cdr (arglist);
5427 i++; 5402 i++;
5428 } 5403 }
5483mk_proc (SCHEME_P_ enum scheme_opcodes op) 5458mk_proc (SCHEME_P_ enum scheme_opcodes op)
5484{ 5459{
5485 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5460 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5486 set_typeflag (y, (T_PROC | T_ATOM)); 5461 set_typeflag (y, (T_PROC | T_ATOM));
5487 ivalue_unchecked (y) = op; 5462 ivalue_unchecked (y) = op;
5488 set_num_integer (y);
5489 return y; 5463 return y;
5490} 5464}
5491 5465
5492/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5466/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5493static int 5467static int

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines