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.37 by root, Sun Nov 29 05:09:24 2015 UTC

34 34
35#include <sys/types.h> 35#include <sys/types.h>
36#include <sys/stat.h> 36#include <sys/stat.h>
37#include <fcntl.h> 37#include <fcntl.h>
38 38
39#if !USE_ERROR_CHECKING
40# define NDEBUG
41#endif
42
43#include <assert.h>
44#include <stdlib.h>
39#include <string.h> 45#include <string.h>
40#include <stdlib.h>
41 46
42#include <limits.h> 47#include <limits.h>
43#include <inttypes.h> 48#include <inttypes.h>
44#include <float.h> 49#include <float.h>
45//#include <ctype.h> 50//#include <ctype.h>
51
52#if '1' != '0' + 1 \
53 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \
54 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \
55 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \
56 || 'f' != 'a' + 5
57# error "execution character set digits not consecutive"
58#endif
46 59
47enum { 60enum {
48 TOK_EOF, 61 TOK_EOF,
49 TOK_LPAREN, 62 TOK_LPAREN,
50 TOK_RPAREN, 63 TOK_RPAREN,
51 TOK_DOT, 64 TOK_DOT,
52 TOK_ATOM, 65 TOK_ATOM,
66 TOK_DOTATOM, /* atom name starting with '.' */
67 TOK_STRATOM, /* atom name enclosed in | */
53 TOK_QUOTE, 68 TOK_QUOTE,
54 TOK_DQUOTE, 69 TOK_DQUOTE,
55 TOK_BQUOTE, 70 TOK_BQUOTE,
56 TOK_COMMA, 71 TOK_COMMA,
57 TOK_ATMARK, 72 TOK_ATMARK,
182# define FIRST_CELLSEGS 3 197# define FIRST_CELLSEGS 3
183#endif 198#endif
184 199
185enum scheme_types 200enum scheme_types
186{ 201{
202 T_INTEGER,
187 T_FREE, 203 T_REAL,
188 T_STRING, 204 T_STRING,
189 T_NUMBER,
190 T_SYMBOL, 205 T_SYMBOL,
191 T_PROC, 206 T_PROC,
192 T_PAIR, 207 T_PAIR, /* also used for free cells */
193 T_CLOSURE, 208 T_CLOSURE,
194 T_CONTINUATION, 209 T_CONTINUATION,
195 T_FOREIGN, 210 T_FOREIGN,
196 T_CHARACTER, 211 T_CHARACTER,
197 T_PORT, 212 T_PORT,
207#define T_SYNTAX 0x0010 222#define T_SYNTAX 0x0010
208#define T_IMMUTABLE 0x0020 223#define T_IMMUTABLE 0x0020
209#define T_ATOM 0x0040 /* only for gc */ 224#define T_ATOM 0x0040 /* only for gc */
210#define T_MARK 0x0080 /* only for gc */ 225#define T_MARK 0x0080 /* only for gc */
211 226
227/* num, for generic arithmetic */
228struct num
229{
230 IVALUE ivalue;
231#if USE_REAL
232 RVALUE rvalue;
233 char is_fixnum;
234#endif
235};
236
237#if USE_REAL
238# define num_is_fixnum(n) (n).is_fixnum
239# define num_set_fixnum(n,f) (n).is_fixnum = (f)
240# define num_ivalue(n) (n).ivalue
241# define num_rvalue(n) (n).rvalue
242# define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i)
243# define num_set_rvalue(n,r) (n).rvalue = (r)
244#else
245# define num_is_fixnum(n) 1
246# define num_set_fixnum(n,f) 0
247# define num_ivalue(n) (n).ivalue
248# define num_rvalue(n) (n).ivalue
249# define num_set_ivalue(n,i) (n).ivalue = (i)
250# define num_set_rvalue(n,r) (n).ivalue = (r)
251#endif
252
212enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; 253enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
213 254
214static num num_op (enum num_op op, num a, num b); 255static num num_op (enum num_op op, num a, num b);
215static num num_intdiv (num a, num b); 256static num num_intdiv (num a, num b);
216static num num_rem (num a, num b); 257static num num_rem (num a, num b);
236} 277}
237 278
238#define strvalue(p) ((p)->object.string.svalue) 279#define strvalue(p) ((p)->object.string.svalue)
239#define strlength(p) ((p)->object.string.length) 280#define strlength(p) ((p)->object.string.length)
240 281
241INTERFACE int is_list (SCHEME_P_ pointer p);
242
243INTERFACE int 282INTERFACE int
244is_vector (pointer p) 283is_vector (pointer p)
245{ 284{
246 return type (p) == T_VECTOR; 285 return type (p) == T_VECTOR;
247} 286}
248 287
249#define vecvalue(p) ((p)->object.vector.vvalue) 288#define vecvalue(p) ((p)->object.vector.vvalue)
250#define veclength(p) ((p)->object.vector.length) 289#define veclength(p) ((p)->object.vector.length)
251INTERFACE void fill_vector (pointer vec, pointer obj); 290INTERFACE 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); 291INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
254INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); 292INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
255 293
256INTERFACE uint32_t 294INTERFACE int
257vector_length (pointer vec) 295is_integer (pointer p)
258{ 296{
259 return vec->object.vector.length; 297 return type (p) == T_INTEGER;
298}
299
300/* not the same as in scheme, where integers are (correctly :) reals */
301INTERFACE int
302is_real (pointer p)
303{
304 return type (p) == T_REAL;
260} 305}
261 306
262INTERFACE int 307INTERFACE int
263is_number (pointer p) 308is_number (pointer p)
264{ 309{
265 return type (p) == T_NUMBER; 310 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} 311}
279 312
280INTERFACE int 313INTERFACE int
281is_character (pointer p) 314is_character (pointer p)
282{ 315{
287string_value (pointer p) 320string_value (pointer p)
288{ 321{
289 return strvalue (p); 322 return strvalue (p);
290} 323}
291 324
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) 325#define ivalue_unchecked(p) (p)->object.ivalue
326#define set_ivalue(p,v) (p)->object.ivalue = (v)
327
323#if USE_REAL 328#if USE_REAL
324# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 329#define rvalue_unchecked(p) (p)->object.rvalue
325# define set_num_integer(p) (p)->object.number.is_fixnum=1; 330#define set_rvalue(p,v) (p)->object.rvalue = (v)
326# define set_num_real(p) (p)->object.number.is_fixnum=0;
327#else 331#else
328# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 332#define rvalue_unchecked(p) (p)->object.ivalue
329# define set_num_integer(p) 0 333#define set_rvalue(p,v) (p)->object.ivalue = (v)
330# define set_num_real(p) 0
331#endif 334#endif
332 335
333INTERFACE long 336INTERFACE long
334charvalue (pointer p) 337charvalue (pointer p)
335{ 338{
440syntaxname (pointer p) 443syntaxname (pointer p)
441{ 444{
442 return strvalue (car (p)); 445 return strvalue (car (p));
443} 446}
444 447
445#define procnum(p) ivalue (p) 448#define procnum(p) ivalue_unchecked (p)
446static const char *procname (pointer x); 449static const char *procname (pointer x);
447 450
448INTERFACE int 451INTERFACE int
449is_closure (pointer p) 452is_closure (pointer p)
450{ 453{
511setimmutable (pointer p) 514setimmutable (pointer p)
512{ 515{
513#if USE_ERROR_CHECKING 516#if USE_ERROR_CHECKING
514 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 517 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
515#endif 518#endif
519}
520
521/* Result is:
522 proper list: length
523 circular list: -1
524 not even a pair: -2
525 dotted list: -2 minus length before dot
526*/
527INTERFACE int
528list_length (SCHEME_P_ pointer a)
529{
530 int i = 0;
531 pointer slow, fast;
532
533 slow = fast = a;
534
535 while (1)
536 {
537 if (fast == NIL)
538 return i;
539
540 if (!is_pair (fast))
541 return -2 - i;
542
543 fast = cdr (fast);
544 ++i;
545
546 if (fast == NIL)
547 return i;
548
549 if (!is_pair (fast))
550 return -2 - i;
551
552 ++i;
553 fast = cdr (fast);
554
555 /* Safe because we would have already returned if `fast'
556 encountered a non-pair. */
557 slow = cdr (slow);
558
559 if (fast == slow)
560 {
561 /* the fast pointer has looped back around and caught up
562 with the slow pointer, hence the structure is circular,
563 not of finite length, and therefore not a list */
564 return -1;
565 }
566 }
567}
568
569INTERFACE int
570is_list (SCHEME_P_ pointer a)
571{
572 return list_length (SCHEME_A_ a) >= 0;
516} 573}
517 574
518#if USE_CHAR_CLASSIFIERS 575#if USE_CHAR_CLASSIFIERS
519ecb_inline int 576ecb_inline int
520Cisalpha (int c) 577Cisalpha (int c)
609#endif 666#endif
610 667
611static int file_push (SCHEME_P_ const char *fname); 668static int file_push (SCHEME_P_ const char *fname);
612static void file_pop (SCHEME_P); 669static void file_pop (SCHEME_P);
613static int file_interactive (SCHEME_P); 670static int file_interactive (SCHEME_P);
614ecb_inline int is_one_of (char *s, int c); 671ecb_inline int is_one_of (const char *s, int c);
615static int alloc_cellseg (SCHEME_P_ int n); 672static int alloc_cellseg (SCHEME_P_ int n);
616ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); 673ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
617static void finalize_cell (SCHEME_P_ pointer a); 674static void finalize_cell (SCHEME_P_ pointer a);
618static int count_consecutive_cells (pointer x, int needed); 675static int count_consecutive_cells (pointer x, int needed);
619static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 676static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
636static void mark (pointer a); 693static void mark (pointer a);
637static void gc (SCHEME_P_ pointer a, pointer b); 694static void gc (SCHEME_P_ pointer a, pointer b);
638static int basic_inchar (port *pt); 695static int basic_inchar (port *pt);
639static int inchar (SCHEME_P); 696static int inchar (SCHEME_P);
640static void backchar (SCHEME_P_ int c); 697static void backchar (SCHEME_P_ int c);
641static char *readstr_upto (SCHEME_P_ char *delim); 698static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
642static pointer readstrexp (SCHEME_P); 699static pointer readstrexp (SCHEME_P_ char delim);
643ecb_inline int skipspace (SCHEME_P); 700ecb_inline int skipspace (SCHEME_P);
644static int token (SCHEME_P); 701static int token (SCHEME_P);
645static void printslashstring (SCHEME_P_ char *s, int len); 702static void printslashstring (SCHEME_P_ char *s, int len);
646static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 703static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
647static void printatom (SCHEME_P_ pointer l, int f); 704static void printatom (SCHEME_P_ pointer l, int f);
664static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 721static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
665static void assign_syntax (SCHEME_P_ const char *name); 722static void assign_syntax (SCHEME_P_ const char *name);
666static int syntaxnum (pointer p); 723static int syntaxnum (pointer p);
667static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 724static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
668 725
726static IVALUE
727ivalue (pointer x)
728{
729 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
730}
731
732static RVALUE
733rvalue (pointer x)
734{
735 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
736}
737
738INTERFACE num
739nvalue (pointer x)
740{
741 num n;
742
743 num_set_fixnum (n, is_integer (x));
744
745 if (num_is_fixnum (n))
746 num_set_ivalue (n, ivalue_unchecked (x));
747 else
748 num_set_rvalue (n, rvalue_unchecked (x));
749
750 return n;
751}
752
669static num 753static num
670num_op (enum num_op op, num a, num b) 754num_op (enum num_op op, num a, num b)
671{ 755{
672 num ret; 756 num ret;
673 757
674 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 758 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
675 759
676 if (num_is_fixnum (ret)) 760 if (num_is_fixnum (ret))
677 { 761 {
678 IVALUE av = num_get_ivalue (a);
679 IVALUE bv = num_get_ivalue (b);
680
681 switch (op) 762 switch (op)
682 { 763 {
683 case NUM_ADD: av += bv; break; 764 case NUM_ADD: a.ivalue += b.ivalue; break;
684 case NUM_SUB: av -= bv; break; 765 case NUM_SUB: a.ivalue -= b.ivalue; break;
685 case NUM_MUL: av *= bv; break; 766 case NUM_MUL: a.ivalue *= b.ivalue; break;
686 case NUM_INTDIV: av /= bv; break; 767 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
687 } 768 }
688 769
689 num_set_ivalue (ret, av); 770 num_set_ivalue (ret, a.ivalue);
690 } 771 }
772#if USE_REAL
691 else 773 else
692 { 774 {
693 RVALUE av = num_get_rvalue (a);
694 RVALUE bv = num_get_rvalue (b);
695
696 switch (op) 775 switch (op)
697 { 776 {
698 case NUM_ADD: av += bv; break; 777 case NUM_ADD: a.rvalue += b.rvalue; break;
699 case NUM_SUB: av -= bv; break; 778 case NUM_SUB: a.rvalue -= b.rvalue; break;
700 case NUM_MUL: av *= bv; break; 779 case NUM_MUL: a.rvalue *= b.rvalue; break;
701 case NUM_INTDIV: av /= bv; break; 780 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
702 } 781 }
703 782
704 num_set_rvalue (ret, av); 783 num_set_rvalue (ret, a.rvalue);
705 } 784 }
785#endif
706 786
707 return ret; 787 return ret;
708} 788}
709 789
710static num 790static num
711num_div (num a, num b) 791num_div (num a, num b)
712{ 792{
713 num ret; 793 num ret;
714 794
715 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); 795 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0);
716 796
717 if (num_is_fixnum (ret)) 797 if (num_is_fixnum (ret))
718 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 798 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
719 else 799 else
720 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 800 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
721 801
722 return ret; 802 return ret;
723} 803}
724 804
725static num 805static num
727{ 807{
728 num ret; 808 num ret;
729 long e1, e2, res; 809 long e1, e2, res;
730 810
731 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 811 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
732 e1 = num_get_ivalue (a); 812 e1 = num_ivalue (a);
733 e2 = num_get_ivalue (b); 813 e2 = num_ivalue (b);
734 res = e1 % e2; 814 res = e1 % e2;
735 815
736 /* remainder should have same sign as second operand */ 816 /* remainder should have same sign as second operand */
737 if (res > 0) 817 if (res > 0)
738 { 818 {
754{ 834{
755 num ret; 835 num ret;
756 long e1, e2, res; 836 long e1, e2, res;
757 837
758 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 838 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
759 e1 = num_get_ivalue (a); 839 e1 = num_ivalue (a);
760 e2 = num_get_ivalue (b); 840 e2 = num_ivalue (b);
761 res = e1 % e2; 841 res = e1 % e2;
762 842
763 /* modulo should have same sign as second operand */ 843 /* modulo should have same sign as second operand */
764 if (res * e2 < 0) 844 if (res * e2 < 0)
765 res += e2; 845 res += e2;
775 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 855 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
776 int ret; 856 int ret;
777 857
778 if (is_fixnum) 858 if (is_fixnum)
779 { 859 {
780 IVALUE av = num_get_ivalue (a); 860 IVALUE av = num_ivalue (a);
781 IVALUE bv = num_get_ivalue (b); 861 IVALUE bv = num_ivalue (b);
782 862
783 ret = av == bv ? 0 : av < bv ? -1 : +1; 863 ret = av == bv ? 0 : av < bv ? -1 : +1;
784 } 864 }
785 else 865 else
786 { 866 {
787 RVALUE av = num_get_rvalue (a); 867 RVALUE av = num_rvalue (a);
788 RVALUE bv = num_get_rvalue (b); 868 RVALUE bv = num_rvalue (b);
789 869
790 ret = av == bv ? 0 : av < bv ? -1 : +1; 870 ret = av == bv ? 0 : av < bv ? -1 : +1;
791 } 871 }
792 872
793 return ret; 873 return ret;
856 return k; 936 return k;
857 937
858 i = ++SCHEME_V->last_cell_seg; 938 i = ++SCHEME_V->last_cell_seg;
859 SCHEME_V->alloc_seg[i] = cp; 939 SCHEME_V->alloc_seg[i] = cp;
860 940
861 /* insert new segment in address order */
862 newp = (pointer)cp; 941 newp = (pointer)cp;
863 SCHEME_V->cell_seg[i] = newp; 942 SCHEME_V->cell_seg[i] = newp;
864 SCHEME_V->cell_segsize[i] = segsize; 943 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; 944 SCHEME_V->fcells += segsize;
881 last = newp + segsize - 1; 945 last = newp + segsize - 1;
882 946
883 for (p = newp; p <= last; p++) 947 for (p = newp; p <= last; p++)
884 { 948 {
885 set_typeflag (p, T_FREE); 949 set_typeflag (p, T_PAIR);
886 set_car (p, NIL); 950 set_car (p, NIL);
887 set_cdr (p, p + 1); 951 set_cdr (p, p + 1);
888 } 952 }
889 953
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); 954 set_cdr (last, SCHEME_V->free_cell);
894 SCHEME_V->free_cell = newp; 955 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 } 956 }
907 957
908 return n; 958 return n;
909} 959}
910 960
989 /* Record it as a vector so that gc understands it. */ 1039 /* Record it as a vector so that gc understands it. */
990 set_typeflag (v, T_VECTOR | T_ATOM); 1040 set_typeflag (v, T_VECTOR | T_ATOM);
991 1041
992 v->object.vector.vvalue = e; 1042 v->object.vector.vvalue = e;
993 v->object.vector.length = len; 1043 v->object.vector.length = len;
994 fill_vector (v, init); 1044 fill_vector (v, 0, init);
995 push_recent_alloc (SCHEME_A_ v, NIL); 1045 push_recent_alloc (SCHEME_A_ v, NIL);
996 1046
997 return v; 1047 return v;
998} 1048}
999 1049
1044 return x; 1094 return x;
1045} 1095}
1046 1096
1047/* ========== oblist implementation ========== */ 1097/* ========== oblist implementation ========== */
1048 1098
1099static pointer
1100generate_symbol (SCHEME_P_ const char *name)
1101{
1102 pointer x = mk_string (SCHEME_A_ name);
1103 setimmutable (x);
1104 x = immutable_cons (x, NIL);
1105 set_typeflag (x, T_SYMBOL);
1106 return x;
1107}
1108
1049#ifndef USE_OBJECT_LIST 1109#ifndef USE_OBJECT_LIST
1050 1110
1111static int
1051static int hash_fn (const char *key, int table_size); 1112hash_fn (const char *key, int table_size)
1113{
1114 const unsigned char *p = key;
1115 uint32_t hash = 2166136261;
1116
1117 while (*p)
1118 hash = (hash ^ *p++) * 16777619;
1119
1120 return hash % table_size;
1121}
1052 1122
1053static pointer 1123static pointer
1054oblist_initial_value (SCHEME_P) 1124oblist_initial_value (SCHEME_P)
1055{ 1125{
1056 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1126 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1058 1128
1059/* returns the new symbol */ 1129/* returns the new symbol */
1060static pointer 1130static pointer
1061oblist_add_by_name (SCHEME_P_ const char *name) 1131oblist_add_by_name (SCHEME_P_ const char *name)
1062{ 1132{
1063 int location; 1133 pointer x = generate_symbol (SCHEME_A_ name);
1064
1065 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1066 set_typeflag (x, T_SYMBOL);
1067 setimmutable (car (x));
1068
1069 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1134 int 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))); 1135 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1071 return x; 1136 return x;
1072} 1137}
1073 1138
1074ecb_inline pointer 1139ecb_inline pointer
1075oblist_find_by_name (SCHEME_P_ const char *name) 1140oblist_find_by_name (SCHEME_P_ const char *name)
1078 pointer x; 1143 pointer x;
1079 char *s; 1144 char *s;
1080 1145
1081 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1146 location = hash_fn (name, veclength (SCHEME_V->oblist));
1082 1147
1083 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1148 for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1084 { 1149 {
1085 s = symname (car (x)); 1150 s = symname (car (x));
1086 1151
1087 /* case-insensitive, per R5RS section 2 */ 1152 /* case-insensitive, per R5RS section 2 */
1088 if (stricmp (name, s) == 0) 1153 if (stricmp (name, s) == 0)
1098 int i; 1163 int i;
1099 pointer x; 1164 pointer x;
1100 pointer ob_list = NIL; 1165 pointer ob_list = NIL;
1101 1166
1102 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1167 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1103 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1168 for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1104 ob_list = cons (x, ob_list); 1169 ob_list = cons (x, ob_list);
1105 1170
1106 return ob_list; 1171 return ob_list;
1107} 1172}
1108 1173
1134 1199
1135/* returns the new symbol */ 1200/* returns the new symbol */
1136static pointer 1201static pointer
1137oblist_add_by_name (SCHEME_P_ const char *name) 1202oblist_add_by_name (SCHEME_P_ const char *name)
1138{ 1203{
1139 pointer x; 1204 pointer x = mk_string (SCHEME_A_ name);
1140
1141 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1142 set_typeflag (x, T_SYMBOL); 1205 set_typeflag (x, T_SYMBOL);
1143 setimmutable (car (x)); 1206 setimmutable (x);
1144 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1207 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1145 return x; 1208 return x;
1146} 1209}
1147 1210
1148static pointer 1211static pointer
1181mk_character (SCHEME_P_ int c) 1244mk_character (SCHEME_P_ int c)
1182{ 1245{
1183 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1246 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1184 1247
1185 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1248 set_typeflag (x, (T_CHARACTER | T_ATOM));
1186 ivalue_unchecked (x) = c & 0xff; 1249 set_ivalue (x, c & 0xff);
1187 set_num_integer (x); 1250
1188 return x; 1251 return x;
1189} 1252}
1190 1253
1191/* get number atom (integer) */ 1254/* get number atom (integer) */
1192INTERFACE pointer 1255INTERFACE pointer
1193mk_integer (SCHEME_P_ long num) 1256mk_integer (SCHEME_P_ long n)
1194{ 1257{
1195 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1258 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1196 1259
1197 set_typeflag (x, (T_NUMBER | T_ATOM)); 1260 set_typeflag (x, (T_INTEGER | T_ATOM));
1198 ivalue_unchecked (x) = num; 1261 set_ivalue (x, n);
1199 set_num_integer (x); 1262
1200 return x; 1263 return x;
1201} 1264}
1202 1265
1203INTERFACE pointer 1266INTERFACE pointer
1204mk_real (SCHEME_P_ RVALUE n) 1267mk_real (SCHEME_P_ RVALUE n)
1205{ 1268{
1269#if USE_REAL
1206 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1270 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1207 1271
1208 set_typeflag (x, (T_NUMBER | T_ATOM)); 1272 set_typeflag (x, (T_REAL | T_ATOM));
1209 rvalue_unchecked (x) = n; 1273 set_rvalue (x, n);
1210 set_num_real (x); 1274
1211 return x; 1275 return x;
1276#else
1277 return mk_integer (SCHEME_A_ n);
1278#endif
1212} 1279}
1213 1280
1214static pointer 1281static pointer
1215mk_number (SCHEME_P_ const num n) 1282mk_number (SCHEME_P_ const num n)
1216{ 1283{
1284#if USE_REAL
1217 if (num_is_fixnum (n)) 1285 return num_is_fixnum (n)
1286 ? mk_integer (SCHEME_A_ num_ivalue (n))
1287 : mk_real (SCHEME_A_ num_rvalue (n));
1288#else
1218 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1289 return mk_integer (SCHEME_A_ num_ivalue (n));
1219 else 1290#endif
1220 return mk_real (SCHEME_A_ num_get_rvalue (n));
1221} 1291}
1222 1292
1223/* allocate name to string area */ 1293/* allocate name to string area */
1224static char * 1294static char *
1225store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) 1295store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1231 SCHEME_V->no_memory = 1; 1301 SCHEME_V->no_memory = 1;
1232 return SCHEME_V->strbuff; 1302 return SCHEME_V->strbuff;
1233 } 1303 }
1234 1304
1235 if (str) 1305 if (str)
1236 { 1306 memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */
1237 int l = strlen (str);
1238
1239 if (l > len_str)
1240 l = len_str;
1241
1242 memcpy (q, str, l);
1243 q[l] = 0;
1244 }
1245 else 1307 else
1246 {
1247 memset (q, fill, len_str); 1308 memset (q, fill, len_str);
1309
1248 q[len_str] = 0; 1310 q[len_str] = 0;
1249 }
1250 1311
1251 return q; 1312 return q;
1252} 1313}
1253 1314
1254INTERFACE pointer 1315INTERFACE pointer
1268 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1329 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1269 1330
1270 set_typeflag (x, T_STRING | T_ATOM); 1331 set_typeflag (x, T_STRING | T_ATOM);
1271 strvalue (x) = store_string (SCHEME_A_ len, str, 0); 1332 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1272 strlength (x) = len; 1333 strlength (x) = len;
1334
1273 return x; 1335 return x;
1274} 1336}
1275 1337
1276INTERFACE pointer 1338INTERFACE pointer
1277mk_string (SCHEME_P_ const char *str) 1339mk_string (SCHEME_P_ const char *str)
1284{ 1346{
1285 return get_vector_object (SCHEME_A_ len, NIL); 1347 return get_vector_object (SCHEME_A_ len, NIL);
1286} 1348}
1287 1349
1288INTERFACE void 1350INTERFACE void
1289fill_vector (pointer vec, pointer obj) 1351fill_vector (pointer vec, uint32_t start, pointer obj)
1290{ 1352{
1291 int i; 1353 int i;
1292 1354
1293 for (i = 0; i < vec->object.vector.length; i++) 1355 for (i = start; i < veclength (vec); i++)
1294 vecvalue (vec)[i] = obj; 1356 vecvalue (vec)[i] = obj;
1295} 1357}
1296 1358
1359INTERFACE void
1360vector_resize (pointer vec, uint32_t newsize, pointer fill)
1361{
1362 uint32_t oldsize = veclength (vec);
1363 vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer));
1364 veclength (vec) = newsize;
1365 fill_vector (vec, oldsize, fill);
1366}
1367
1297INTERFACE pointer 1368INTERFACE pointer
1298vector_elem (pointer vec, uint32_t ielem) 1369vector_get (pointer vec, uint32_t ielem)
1299{ 1370{
1300 return vecvalue(vec)[ielem]; 1371 return vecvalue(vec)[ielem];
1301} 1372}
1302 1373
1303INTERFACE void 1374INTERFACE void
1304set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1375vector_set (pointer vec, uint32_t ielem, pointer a)
1305{ 1376{
1306 vecvalue(vec)[ielem] = a; 1377 vecvalue(vec)[ielem] = a;
1307} 1378}
1308 1379
1309/* get new symbol */ 1380/* get new symbol */
1321 1392
1322INTERFACE pointer 1393INTERFACE pointer
1323gensym (SCHEME_P) 1394gensym (SCHEME_P)
1324{ 1395{
1325 pointer x; 1396 pointer x;
1326
1327 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1328 {
1329 char name[40] = "gensym-"; 1397 char name[40] = "gensym-";
1330 xnum (name + 7, SCHEME_V->gensym_cnt); 1398 xnum (name + 7, SCHEME_V->gensym_cnt);
1331 1399
1332 /* first check oblist */ 1400 return generate_symbol (SCHEME_A_ name);
1333 x = oblist_find_by_name (SCHEME_A_ name);
1334
1335 if (x == NIL)
1336 {
1337 x = oblist_add_by_name (SCHEME_A_ name);
1338 return x;
1339 }
1340 }
1341
1342 return NIL;
1343} 1401}
1344 1402
1345/* make symbol or number atom from string */ 1403/* make symbol or number atom from string */
1346static pointer 1404static pointer
1347mk_atom (SCHEME_P_ char *q) 1405mk_atom (SCHEME_P_ char *q)
1401 } 1459 }
1402 else if ((c == 'e') || (c == 'E')) 1460 else if ((c == 'e') || (c == 'E'))
1403 { 1461 {
1404 if (!has_fp_exp) 1462 if (!has_fp_exp)
1405 { 1463 {
1406 has_dec_point = 1; /* decimal point illegal 1464 has_dec_point = 1; /* decimal point illegal from now on */
1407 from now on */
1408 p++; 1465 p++;
1409 1466
1410 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1467 if ((*p == '-') || (*p == '+') || isdigit (*p))
1411 continue; 1468 continue;
1412 } 1469 }
1500 1557
1501 if (ecb_expect_false (is_vector (p))) 1558 if (ecb_expect_false (is_vector (p)))
1502 { 1559 {
1503 int i; 1560 int i;
1504 1561
1505 for (i = 0; i < p->object.vector.length; i++) 1562 for (i = 0; i < veclength (p); i++)
1506 mark (vecvalue (p)[i]); 1563 mark (vecvalue (p)[i]);
1507 } 1564 }
1508 1565
1509 if (is_atom (p)) 1566 if (is_atom (p))
1510 goto E6; 1567 goto E6;
1608 if (is_mark (p)) 1665 if (is_mark (p))
1609 clrmark (p); 1666 clrmark (p);
1610 else 1667 else
1611 { 1668 {
1612 /* reclaim cell */ 1669 /* reclaim cell */
1613 if (typeflag (p) != T_FREE) 1670 if (typeflag (p) != T_PAIR)
1614 { 1671 {
1615 finalize_cell (SCHEME_A_ p); 1672 finalize_cell (SCHEME_A_ p);
1616 set_typeflag (p, T_FREE); 1673 set_typeflag (p, T_PAIR);
1617 set_car (p, NIL); 1674 set_car (p, NIL);
1618 } 1675 }
1619 1676
1620 ++SCHEME_V->fcells; 1677 ++SCHEME_V->fcells;
1621 set_cdr (p, SCHEME_V->free_cell); 1678 set_cdr (p, SCHEME_V->free_cell);
1623 } 1680 }
1624 } 1681 }
1625 } 1682 }
1626 1683
1627 if (SCHEME_V->gc_verbose) 1684 if (SCHEME_V->gc_verbose)
1685 {
1628 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1686 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1687 }
1629} 1688}
1630 1689
1631static void 1690static void
1632finalize_cell (SCHEME_P_ pointer a) 1691finalize_cell (SCHEME_P_ pointer a)
1633{ 1692{
2062#endif 2121#endif
2063} 2122}
2064 2123
2065/* read characters up to delimiter, but cater to character constants */ 2124/* read characters up to delimiter, but cater to character constants */
2066static char * 2125static char *
2067readstr_upto (SCHEME_P_ char *delim) 2126readstr_upto (SCHEME_P_ int skip, const char *delim)
2068{ 2127{
2069 char *p = SCHEME_V->strbuff; 2128 char *p = SCHEME_V->strbuff + skip;
2070 2129
2071 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2130 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2072 2131
2073 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2132 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2074 *p = 0; 2133 *p = 0;
2081 return SCHEME_V->strbuff; 2140 return SCHEME_V->strbuff;
2082} 2141}
2083 2142
2084/* read string expression "xxx...xxx" */ 2143/* read string expression "xxx...xxx" */
2085static pointer 2144static pointer
2086readstrexp (SCHEME_P) 2145readstrexp (SCHEME_P_ char delim)
2087{ 2146{
2088 char *p = SCHEME_V->strbuff; 2147 char *p = SCHEME_V->strbuff;
2089 int c; 2148 int c;
2090 int c1 = 0; 2149 int c1 = 0;
2091 enum
2092 { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; 2150 enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok;
2093 2151
2094 for (;;) 2152 for (;;)
2095 { 2153 {
2096 c = inchar (SCHEME_A); 2154 c = inchar (SCHEME_A);
2097 2155
2099 return S_F; 2157 return S_F;
2100 2158
2101 switch (state) 2159 switch (state)
2102 { 2160 {
2103 case st_ok: 2161 case st_ok:
2104 switch (c) 2162 if (ecb_expect_false (c == delim))
2105 {
2106 case '\\':
2107 state = st_bsl;
2108 break;
2109
2110 case '"':
2111 *p = 0;
2112 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); 2163 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff);
2113 2164
2114 default: 2165 if (ecb_expect_false (c == '\\'))
2166 state = st_bsl;
2167 else
2115 *p++ = c; 2168 *p++ = c;
2116 break;
2117 }
2118 2169
2119 break; 2170 break;
2120 2171
2121 case st_bsl: 2172 case st_bsl:
2122 switch (c) 2173 switch (c)
2152 case 'r': 2203 case 'r':
2153 *p++ = '\r'; 2204 *p++ = '\r';
2154 state = st_ok; 2205 state = st_ok;
2155 break; 2206 break;
2156 2207
2157 case '"':
2158 *p++ = '"';
2159 state = st_ok;
2160 break;
2161
2162 default: 2208 default:
2163 *p++ = c; 2209 *p++ = c;
2164 state = st_ok; 2210 state = st_ok;
2165 break; 2211 break;
2166 } 2212 }
2167 2213
2168 break; 2214 break;
2169 2215
2170 case st_x1: 2216 case st_x1:
2171 case st_x2: 2217 case st_x2:
2172 c = toupper (c); 2218 c = tolower (c);
2173 2219
2174 if (c >= '0' && c <= 'F') 2220 if (c >= '0' && c <= '9')
2175 {
2176 if (c <= '9')
2177 c1 = (c1 << 4) + c - '0'; 2221 c1 = (c1 << 4) + c - '0';
2178 else 2222 else if (c >= 'a' && c <= 'f')
2179 c1 = (c1 << 4) + c - 'A' + 10; 2223 c1 = (c1 << 4) + c - 'a' + 10;
2180
2181 if (state == st_x1)
2182 state = st_x2;
2183 else
2184 {
2185 *p++ = c1;
2186 state = st_ok;
2187 }
2188 }
2189 else 2224 else
2190 return S_F; 2225 return S_F;
2226
2227 if (state == st_x1)
2228 state = st_x2;
2229 else
2230 {
2231 *p++ = c1;
2232 state = st_ok;
2233 }
2191 2234
2192 break; 2235 break;
2193 2236
2194 case st_oct1: 2237 case st_oct1:
2195 case st_oct2: 2238 case st_oct2:
2199 backchar (SCHEME_A_ c); 2242 backchar (SCHEME_A_ c);
2200 state = st_ok; 2243 state = st_ok;
2201 } 2244 }
2202 else 2245 else
2203 { 2246 {
2204 if (state == st_oct2 && c1 >= 32) 2247 if (state == st_oct2 && c1 >= ' ')
2205 return S_F; 2248 return S_F;
2206 2249
2207 c1 = (c1 << 3) + (c - '0'); 2250 c1 = (c1 << 3) + (c - '0');
2208 2251
2209 if (state == st_oct1) 2252 if (state == st_oct1)
2214 state = st_ok; 2257 state = st_ok;
2215 } 2258 }
2216 } 2259 }
2217 2260
2218 break; 2261 break;
2219
2220 } 2262 }
2221 } 2263 }
2222} 2264}
2223 2265
2224/* check c is in chars */ 2266/* check c is in chars */
2225ecb_inline int 2267ecb_inline int
2226is_one_of (char *s, int c) 2268is_one_of (const char *s, int c)
2227{ 2269{
2228 if (c == EOF) 2270 if (c == EOF)
2229 return 1; 2271 return 1;
2230 2272
2231 return !!strchr (s, c); 2273 return !!strchr (s, c);
2287 2329
2288 if (is_one_of (" \n\t", c)) 2330 if (is_one_of (" \n\t", c))
2289 return TOK_DOT; 2331 return TOK_DOT;
2290 else 2332 else
2291 { 2333 {
2292 //TODO: ungetc twice in a row is not supported in C
2293 backchar (SCHEME_A_ c); 2334 backchar (SCHEME_A_ c);
2294 backchar (SCHEME_A_ '.');
2295 return TOK_ATOM; 2335 return TOK_DOTATOM;
2296 } 2336 }
2337
2338 case '|':
2339 return TOK_STRATOM;
2297 2340
2298 case '\'': 2341 case '\'':
2299 return TOK_QUOTE; 2342 return TOK_QUOTE;
2300 2343
2301 case ';': 2344 case ';':
2770#define is_true(p) ((p) != S_F) 2813#define is_true(p) ((p) != S_F)
2771#define is_false(p) ((p) == S_F) 2814#define is_false(p) ((p) == S_F)
2772 2815
2773/* ========== Environment implementation ========== */ 2816/* ========== Environment implementation ========== */
2774 2817
2775#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST)
2776
2777static int
2778hash_fn (const char *key, int table_size)
2779{
2780 const unsigned char *p = key;
2781 uint32_t hash = 2166136261;
2782
2783 while (*p)
2784 hash = (hash ^ *p++) * 16777619;
2785
2786 return hash % table_size;
2787}
2788#endif
2789
2790#ifndef USE_ALIST_ENV 2818#ifndef USE_ALIST_ENV
2791 2819
2792/* 2820/*
2793 * In this implementation, each frame of the environment may be 2821 * In this implementation, each frame of the environment may be
2794 * a hash table: a vector of alists hashed by variable name. 2822 * a hash table: a vector of alists hashed by variable name.
2810 2838
2811 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2839 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2812 setenvironment (SCHEME_V->envir); 2840 setenvironment (SCHEME_V->envir);
2813} 2841}
2814 2842
2843static uint32_t
2844sym_hash (pointer sym, uint32_t size)
2845{
2846 uintptr_t ptr = (uintptr_t)sym;
2847
2848#if 0
2849 /* table size is prime, so why mix */
2850 ptr += ptr >> 32;
2851 ptr += ptr >> 16;
2852 ptr += ptr >> 8;
2853#endif
2854
2855 return ptr % size;
2856}
2857
2815ecb_inline void 2858ecb_inline void
2816new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2859new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2817{ 2860{
2818 pointer slot = immutable_cons (variable, value); 2861 pointer slot = immutable_cons (variable, value);
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 = sym_hash (variable, veclength (car (env)));
2823
2824 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2866 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2825 } 2867 }
2826 else 2868 else
2827 set_car (env, immutable_cons (slot, car (env))); 2869 set_car (env, immutable_cons (slot, car (env)));
2828} 2870}
2829 2871
2830static pointer 2872static pointer
2831find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2873find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2832{ 2874{
2833 pointer x, y; 2875 pointer x, y;
2834 int location;
2835 2876
2836 for (x = env; x != NIL; x = cdr (x)) 2877 for (x = env; x != NIL; x = cdr (x))
2837 { 2878 {
2838 if (is_vector (car (x))) 2879 if (is_vector (car (x)))
2839 { 2880 {
2840 location = hash_fn (symname (hdl), veclength (car (x))); 2881 int location = sym_hash (hdl, veclength (car (x)));
2841 y = vector_elem (car (x), location); 2882 y = vector_get (car (x), location);
2842 } 2883 }
2843 else 2884 else
2844 y = car (x); 2885 y = car (x);
2845 2886
2846 for (; y != NIL; y = cdr (y)) 2887 for (; y != NIL; y = cdr (y))
2847 if (caar (y) == hdl) 2888 if (caar (y) == hdl)
2848 break; 2889 break;
2849 2890
2850 if (y != NIL) 2891 if (y != NIL)
2892 return car (y);
2893
2894 if (!all)
2851 break; 2895 break;
2852
2853 if (!all)
2854 return NIL;
2855 } 2896 }
2856
2857 if (x != NIL)
2858 return car (y);
2859 2897
2860 return NIL; 2898 return NIL;
2861} 2899}
2862 2900
2863#else /* USE_ALIST_ENV */ 2901#else /* USE_ALIST_ENV */
2885 for (y = car (x); y != NIL; y = cdr (y)) 2923 for (y = car (x); y != NIL; y = cdr (y))
2886 if (caar (y) == hdl) 2924 if (caar (y) == hdl)
2887 break; 2925 break;
2888 2926
2889 if (y != NIL) 2927 if (y != NIL)
2928 return car (y);
2890 break; 2929 break;
2891 2930
2892 if (!all) 2931 if (!all)
2893 return NIL; 2932 break;
2894 } 2933 }
2895
2896 if (x != NIL)
2897 return car (y);
2898 2934
2899 return NIL; 2935 return NIL;
2900} 2936}
2901 2937
2902#endif /* USE_ALIST_ENV else */ 2938#endif /* USE_ALIST_ENV else */
3119 int i = 0; 3155 int i = 0;
3120 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3156 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3121 3157
3122 while (cont != NIL) 3158 while (cont != NIL)
3123 { 3159 {
3124 frame->op = ivalue (car (cont)); cont = cdr (cont); 3160 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3125 frame->args = car (cont) ; cont = cdr (cont); 3161 frame->args = car (cont) ; cont = cdr (cont);
3126 frame->envir = car (cont) ; cont = cdr (cont); 3162 frame->envir = car (cont) ; cont = cdr (cont);
3127 frame->code = car (cont) ; cont = cdr (cont); 3163 frame->code = car (cont) ; cont = cdr (cont);
3128 3164
3129 ++frame; 3165 ++frame;
3130 ++i; 3166 ++i;
3131 } 3167 }
3132 3168
3161 SCHEME_V->value = a; 3197 SCHEME_V->value = a;
3162 3198
3163 if (dump == NIL) 3199 if (dump == NIL)
3164 return -1; 3200 return -1;
3165 3201
3166 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3202 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3167 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3203 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3168 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3204 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3169 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3205 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3170 3206
3171 SCHEME_V->dump = dump; 3207 SCHEME_V->dump = dump;
3172 3208
3173 return 0; 3209 return 0;
3174} 3210}
3380 3416
3381 case OP_TRACING: 3417 case OP_TRACING:
3382 { 3418 {
3383 int tr = SCHEME_V->tracing; 3419 int tr = SCHEME_V->tracing;
3384 3420
3385 SCHEME_V->tracing = ivalue (car (args)); 3421 SCHEME_V->tracing = ivalue_unchecked (car (args));
3386 s_return (mk_integer (SCHEME_A_ tr)); 3422 s_return (mk_integer (SCHEME_A_ tr));
3387 } 3423 }
3388 3424
3389#endif 3425#endif
3390 3426
3909{ 3945{
3910 pointer args = SCHEME_V->args; 3946 pointer args = SCHEME_V->args;
3911 pointer x = car (args); 3947 pointer x = car (args);
3912 num v; 3948 num v;
3913 3949
3914#if USE_MATH
3915 RVALUE dd;
3916#endif
3917
3918 switch (op) 3950 switch (op)
3919 { 3951 {
3920#if USE_MATH 3952#if USE_MATH
3921 case OP_INEX2EX: /* inexact->exact */ 3953 case OP_INEX2EX: /* inexact->exact */
3954 {
3922 if (is_integer (x)) 3955 if (is_integer (x))
3923 s_return (x); 3956 s_return (x);
3924 else if (modf (rvalue_unchecked (x), &dd) == 0) 3957
3958 RVALUE r = rvalue_unchecked (x);
3959
3960 if (r == (RVALUE)(IVALUE)r)
3925 s_return (mk_integer (SCHEME_A_ ivalue (x))); 3961 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3926 else 3962 else
3927 Error_1 ("inexact->exact: not integral:", x); 3963 Error_1 ("inexact->exact: not integral:", x);
3964 }
3928 3965
3929 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 3966 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)))); 3967 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)))); 3968 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)))); 3969 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3966 /* If the test fails, result is too big for integer. */ 4003 /* If the test fails, result is too big for integer. */
3967 if (!real_result) 4004 if (!real_result)
3968 { 4005 {
3969 long result_as_long = result; 4006 long result_as_long = result;
3970 4007
3971 if (result != (RVALUE) result_as_long) 4008 if (result != result_as_long)
3972 real_result = 1; 4009 real_result = 1;
3973 } 4010 }
3974 4011
3975 if (real_result) 4012 if (real_result)
3976 s_return (mk_real (SCHEME_A_ result)); 4013 s_return (mk_real (SCHEME_A_ result));
3981 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4018 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)))); 4019 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
3983 4020
3984 case OP_TRUNCATE: 4021 case OP_TRUNCATE:
3985 { 4022 {
3986 RVALUE rvalue_of_x; 4023 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))); 4024 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 } 4025 }
3995 4026
3996 case OP_ROUND: 4027 case OP_ROUND:
3997 if (num_is_integer (x)) 4028 if (is_integer (x))
3998 s_return (x); 4029 s_return (x);
3999 4030
4000 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4031 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4001#endif 4032#endif
4002 4033
4122 } 4153 }
4123 else 4154 else
4124 Error_0 ("set-cdr!: unable to alter immutable pair"); 4155 Error_0 ("set-cdr!: unable to alter immutable pair");
4125 4156
4126 case OP_CHAR2INT: /* char->integer */ 4157 case OP_CHAR2INT: /* char->integer */
4127 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4158 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4128 4159
4129 case OP_INT2CHAR: /* integer->char */ 4160 case OP_INT2CHAR: /* integer->char */
4130 s_return (mk_character (SCHEME_A_ ivalue (x))); 4161 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4131 4162
4132 case OP_CHARUPCASE: 4163 case OP_CHARUPCASE:
4133 { 4164 {
4134 unsigned char c = ivalue (x); 4165 unsigned char c = ivalue_unchecked (x);
4135 c = toupper (c); 4166 c = toupper (c);
4136 s_return (mk_character (SCHEME_A_ c)); 4167 s_return (mk_character (SCHEME_A_ c));
4137 } 4168 }
4138 4169
4139 case OP_CHARDNCASE: 4170 case OP_CHARDNCASE:
4140 { 4171 {
4141 unsigned char c = ivalue (x); 4172 unsigned char c = ivalue_unchecked (x);
4142 c = tolower (c); 4173 c = tolower (c);
4143 s_return (mk_character (SCHEME_A_ c)); 4174 s_return (mk_character (SCHEME_A_ c));
4144 } 4175 }
4145 4176
4146 case OP_STR2SYM: /* string->symbol */ 4177 case OP_STR2SYM: /* string->symbol */
4223 Error_1 ("atom->string: not an atom:", x); 4254 Error_1 ("atom->string: not an atom:", x);
4224 } 4255 }
4225 4256
4226 case OP_MKSTRING: /* make-string */ 4257 case OP_MKSTRING: /* make-string */
4227 { 4258 {
4228 int fill = ' '; 4259 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4229 int len;
4230
4231 len = ivalue (x); 4260 int len = ivalue_unchecked (x);
4232
4233 if (cdr (args) != NIL)
4234 fill = charvalue (cadr (args));
4235 4261
4236 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4262 s_return (mk_empty_string (SCHEME_A_ len, fill));
4237 } 4263 }
4238 4264
4239 case OP_STRLEN: /* string-length */ 4265 case OP_STRLEN: /* string-length */
4240 s_return (mk_integer (SCHEME_A_ strlength (x))); 4266 s_return (mk_integer (SCHEME_A_ strlength (x)));
4241 4267
4242 case OP_STRREF: /* string-ref */ 4268 case OP_STRREF: /* string-ref */
4243 { 4269 {
4244 char *str;
4245 int index;
4246
4247 str = strvalue (x); 4270 char *str = strvalue (x);
4248
4249 index = ivalue (cadr (args)); 4271 int index = ivalue_unchecked (cadr (args));
4250 4272
4251 if (index >= strlength (x)) 4273 if (index >= strlength (x))
4252 Error_1 ("string-ref: out of bounds:", cadr (args)); 4274 Error_1 ("string-ref: out of bounds:", cadr (args));
4253 4275
4254 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4276 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4255 } 4277 }
4256 4278
4257 case OP_STRSET: /* string-set! */ 4279 case OP_STRSET: /* string-set! */
4258 { 4280 {
4259 char *str; 4281 char *str = strvalue (x);
4260 int index; 4282 int index = ivalue_unchecked (cadr (args));
4261 int c; 4283 int c;
4262 4284
4263 if (is_immutable (x)) 4285 if (is_immutable (x))
4264 Error_1 ("string-set!: unable to alter immutable string:", x); 4286 Error_1 ("string-set!: unable to alter immutable string:", x);
4265
4266 str = strvalue (x);
4267
4268 index = ivalue (cadr (args));
4269 4287
4270 if (index >= strlength (x)) 4288 if (index >= strlength (x))
4271 Error_1 ("string-set!: out of bounds:", cadr (args)); 4289 Error_1 ("string-set!: out of bounds:", cadr (args));
4272 4290
4273 c = charvalue (caddr (args)); 4291 c = charvalue (caddr (args));
4296 s_return (newstr); 4314 s_return (newstr);
4297 } 4315 }
4298 4316
4299 case OP_SUBSTR: /* substring */ 4317 case OP_SUBSTR: /* substring */
4300 { 4318 {
4301 char *str; 4319 char *str = strvalue (x);
4302 int index0; 4320 int index0 = ivalue_unchecked (cadr (args));
4303 int index1; 4321 int index1;
4304 int len; 4322 int len;
4305 4323
4306 str = strvalue (x);
4307
4308 index0 = ivalue (cadr (args));
4309
4310 if (index0 > strlength (x)) 4324 if (index0 > strlength (x))
4311 Error_1 ("substring: start out of bounds:", cadr (args)); 4325 Error_1 ("substring: start out of bounds:", cadr (args));
4312 4326
4313 if (cddr (args) != NIL) 4327 if (cddr (args) != NIL)
4314 { 4328 {
4315 index1 = ivalue (caddr (args)); 4329 index1 = ivalue_unchecked (caddr (args));
4316 4330
4317 if (index1 > strlength (x) || index1 < index0) 4331 if (index1 > strlength (x) || index1 < index0)
4318 Error_1 ("substring: end out of bounds:", caddr (args)); 4332 Error_1 ("substring: end out of bounds:", caddr (args));
4319 } 4333 }
4320 else 4334 else
4343 if (SCHEME_V->no_memory) 4357 if (SCHEME_V->no_memory)
4344 s_return (S_SINK); 4358 s_return (S_SINK);
4345#endif 4359#endif
4346 4360
4347 for (x = args, i = 0; is_pair (x); x = cdr (x), i++) 4361 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4348 set_vector_elem (vec, i, car (x)); 4362 vector_set (vec, i, car (x));
4349 4363
4350 s_return (vec); 4364 s_return (vec);
4351 } 4365 }
4352 4366
4353 case OP_MKVECTOR: /* make-vector */ 4367 case OP_MKVECTOR: /* make-vector */
4354 { 4368 {
4355 pointer fill = NIL; 4369 pointer fill = NIL;
4356 int len;
4357 pointer vec; 4370 pointer vec;
4358
4359 len = ivalue (x); 4371 int len = ivalue_unchecked (x);
4360 4372
4361 if (cdr (args) != NIL) 4373 if (cdr (args) != NIL)
4362 fill = cadr (args); 4374 fill = cadr (args);
4363 4375
4364 vec = mk_vector (SCHEME_A_ len); 4376 vec = mk_vector (SCHEME_A_ len);
4367 if (SCHEME_V->no_memory) 4379 if (SCHEME_V->no_memory)
4368 s_return (S_SINK); 4380 s_return (S_SINK);
4369#endif 4381#endif
4370 4382
4371 if (fill != NIL) 4383 if (fill != NIL)
4372 fill_vector (vec, fill); 4384 fill_vector (vec, 0, fill);
4373 4385
4374 s_return (vec); 4386 s_return (vec);
4375 } 4387 }
4376 4388
4377 case OP_VECLEN: /* vector-length */ 4389 case OP_VECLEN: /* vector-length */
4378 s_return (mk_integer (SCHEME_A_ veclength (x))); 4390 s_return (mk_integer (SCHEME_A_ veclength (x)));
4379 4391
4392 case OP_VECRESIZE:
4393 vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args));
4394 s_return (x);
4395
4380 case OP_VECREF: /* vector-ref */ 4396 case OP_VECREF: /* vector-ref */
4381 { 4397 {
4382 int index;
4383
4384 index = ivalue (cadr (args)); 4398 int index = ivalue_unchecked (cadr (args));
4385 4399
4386 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4400 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4387 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4401 Error_1 ("vector-ref: out of bounds:", cadr (args));
4388 4402
4389 s_return (vector_elem (x, index)); 4403 s_return (vector_get (x, index));
4390 } 4404 }
4391 4405
4392 case OP_VECSET: /* vector-set! */ 4406 case OP_VECSET: /* vector-set! */
4393 { 4407 {
4394 int index; 4408 int index = ivalue_unchecked (cadr (args));
4395 4409
4396 if (is_immutable (x)) 4410 if (is_immutable (x))
4397 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4411 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4398 4412
4399 index = ivalue (cadr (args));
4400
4401 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4413 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4402 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4414 Error_1 ("vector-set!: out of bounds:", cadr (args));
4403 4415
4404 set_vector_elem (x, index, caddr (args)); 4416 vector_set (x, index, caddr (args));
4405 s_return (x); 4417 s_return (x);
4406 } 4418 }
4407 } 4419 }
4408 4420
4409 if (USE_ERROR_CHECKING) abort (); 4421 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} 4422}
4465 4423
4466static int 4424static int
4467opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4425opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4468{ 4426{
4514 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4472 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4515 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4473 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4516 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4474 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4517 4475
4518#if USE_CHAR_CLASSIFIERS 4476#if USE_CHAR_CLASSIFIERS
4519 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4477 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4520 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4478 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4521 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4479 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4522 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4480 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4523 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4481 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4524#endif 4482#endif
4525 4483
4526#if USE_PORTS 4484#if USE_PORTS
4527 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4485 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4528 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4486 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4729 4687
4730 case OP_NEWSEGMENT: /* new-segment */ 4688 case OP_NEWSEGMENT: /* new-segment */
4731 if (!is_pair (args) || !is_number (a)) 4689 if (!is_pair (args) || !is_number (a))
4732 Error_0 ("new-segment: argument must be a number"); 4690 Error_0 ("new-segment: argument must be a number");
4733 4691
4734 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 4692 alloc_cellseg (SCHEME_A_ ivalue (a));
4735 4693
4736 s_return (S_T); 4694 s_return (S_T);
4737 4695
4738 case OP_OBLIST: /* oblist */ 4696 case OP_OBLIST: /* oblist */
4739 s_return (oblist_all_symbols (SCHEME_A)); 4697 s_return (oblist_all_symbols (SCHEME_A));
4997 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 4955 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
4998 SCHEME_V->tok = token (SCHEME_A); 4956 SCHEME_V->tok = token (SCHEME_A);
4999 s_goto (OP_RDSEXPR); 4957 s_goto (OP_RDSEXPR);
5000 4958
5001 case TOK_ATOM: 4959 case TOK_ATOM:
5002 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); 4960 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS)));
4961
4962 case TOK_DOTATOM:
4963 SCHEME_V->strbuff[0] = '.';
4964 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
4965
4966 case TOK_STRATOM:
4967 x = readstrexp (SCHEME_A_ '|');
4968 //TODO: haven't checked whether the garbage collector could interfere
4969 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5003 4970
5004 case TOK_DQUOTE: 4971 case TOK_DQUOTE:
5005 x = readstrexp (SCHEME_A); 4972 x = readstrexp (SCHEME_A_ '"');
5006 4973
5007 if (x == S_F) 4974 if (x == S_F)
5008 Error_0 ("Error reading string"); 4975 Error_0 ("Error reading string");
5009 4976
5010 setimmutable (x); 4977 setimmutable (x);
5022 s_goto (OP_EVAL); 4989 s_goto (OP_EVAL);
5023 } 4990 }
5024 } 4991 }
5025 4992
5026 case TOK_SHARP_CONST: 4993 case TOK_SHARP_CONST:
5027 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) 4994 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5028 Error_0 ("undefined sharp expression"); 4995 Error_0 ("undefined sharp expression");
5029 else 4996 else
5030 s_return (x); 4997 s_return (x);
5031 4998
5032 default: 4999 default:
5184 putstr (SCHEME_A_ ")"); 5151 putstr (SCHEME_A_ ")");
5185 s_return (S_T); 5152 s_return (S_T);
5186 } 5153 }
5187 else 5154 else
5188 { 5155 {
5189 pointer elem = vector_elem (vec, i); 5156 pointer elem = vector_get (vec, i);
5190 5157
5191 ivalue_unchecked (cdr (args)) = i + 1; 5158 ivalue_unchecked (cdr (args)) = i + 1;
5192 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5159 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5193 SCHEME_V->args = elem; 5160 SCHEME_V->args = elem;
5194 5161
5269/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5236/* 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); 5237typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5271 5238
5272typedef int (*test_predicate)(pointer); 5239typedef int (*test_predicate)(pointer);
5273static int 5240static int
5274is_any (pointer p) 5241tst_any (pointer p)
5275{ 5242{
5276 return 1; 5243 return 1;
5277} 5244}
5278 5245
5279static int 5246static int
5280is_nonneg (pointer p) 5247tst_inonneg (pointer p)
5281{ 5248{
5282 return ivalue (p) >= 0 && is_integer (p); 5249 return is_integer (p) && ivalue_unchecked (p) >= 0;
5283} 5250}
5284 5251
5285static int 5252static int
5286tst_is_list (pointer p) 5253tst_is_list (SCHEME_P_ pointer p)
5287{ 5254{
5288 return p == NIL || is_pair (p); 5255 return p == NIL || is_pair (p);
5289} 5256}
5290 5257
5291/* Correspond carefully with following defines! */ 5258/* Correspond carefully with following defines! */
5292static struct 5259static struct
5293{ 5260{
5294 test_predicate fct; 5261 test_predicate fct;
5295 const char *kind; 5262 const char *kind;
5296} tests[] = 5263} tests[] = {
5297{
5298 { is_any, 0 }, 5264 { tst_any , 0 },
5299 { is_string, "string" }, 5265 { is_string , "string" },
5300 { is_symbol, "symbol" }, 5266 { is_symbol , "symbol" },
5301 { is_port, "port" }, 5267 { is_port , "port" },
5302 { is_inport, "input port" }, 5268 { is_inport , "input port" },
5303 { is_outport, "output port" }, 5269 { is_outport , "output port" },
5304 { is_environment, "environment" }, 5270 { is_environment, "environment" },
5305 { is_pair, "pair" }, 5271 { is_pair , "pair" },
5306 { tst_is_list, "pair or '()" }, 5272 { 0 , "pair or '()" },
5307 { is_character, "character" }, 5273 { is_character , "character" },
5308 { is_vector, "vector" }, 5274 { is_vector , "vector" },
5309 { is_number, "number" }, 5275 { is_number , "number" },
5310 { is_integer, "integer" }, 5276 { is_integer , "integer" },
5311 { is_nonneg, "non-negative integer" } 5277 { tst_inonneg , "non-negative integer" }
5312}; 5278};
5313 5279
5314#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ 5280#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5315#define TST_ANY "\001" 5281#define TST_ANY "\001"
5316#define TST_STRING "\002" 5282#define TST_STRING "\002"
5357typedef struct 5323typedef struct
5358{ 5324{
5359 uint8_t func; 5325 uint8_t func;
5360 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ 5326 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5361 uint8_t builtin; 5327 uint8_t builtin;
5328#if USE_ERROR_CHECKING
5362 uint8_t min_arity; 5329 uint8_t min_arity;
5363 uint8_t max_arity; 5330 uint8_t max_arity;
5364 char arg_tests_encoding[3]; 5331 char arg_tests_encoding[3];
5332#endif
5365} op_code_info; 5333} op_code_info;
5366 5334
5367static const op_code_info dispatch_table[] = { 5335static const op_code_info dispatch_table[] = {
5336#if USE_ERROR_CHECKING
5368#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, 5337#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5338#else
5339#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5340#endif
5369#include "opdefines.h" 5341#include "opdefines.h"
5370#undef OP_DEF 5342#undef OP_DEF
5371 {0} 5343 {0}
5372}; 5344};
5373 5345
5415 { 5387 {
5416 pointer arg = car (arglist); 5388 pointer arg = car (arglist);
5417 5389
5418 j = t[0]; 5390 j = t[0];
5419 5391
5392 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5393 if (j == TST_LIST[0])
5394 {
5395 if (!tst_is_list (SCHEME_A_ arg))
5396 break;
5397 }
5398 else
5399 {
5420 if (!tests[j - 1].fct (arg)) 5400 if (!tests[j - 1].fct (arg))
5421 break; 5401 break;
5402 }
5422 5403
5423 if (t[1]) /* last test is replicated as necessary */ 5404 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5424 t++; 5405 t++;
5425 5406
5426 arglist = cdr (arglist); 5407 arglist = cdr (arglist);
5427 i++; 5408 i++;
5428 } 5409 }
5483mk_proc (SCHEME_P_ enum scheme_opcodes op) 5464mk_proc (SCHEME_P_ enum scheme_opcodes op)
5484{ 5465{
5485 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5466 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5486 set_typeflag (y, (T_PROC | T_ATOM)); 5467 set_typeflag (y, (T_PROC | T_ATOM));
5487 ivalue_unchecked (y) = op; 5468 ivalue_unchecked (y) = op;
5488 set_num_integer (y);
5489 return y; 5469 return y;
5490} 5470}
5491 5471
5492/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5472/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5493static int 5473static int

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines