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.35 by root, Sun Nov 29 00:02:21 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
1297INTERFACE pointer 1359INTERFACE pointer
1298vector_elem (pointer vec, uint32_t ielem) 1360vector_get (pointer vec, uint32_t ielem)
1299{ 1361{
1300 return vecvalue(vec)[ielem]; 1362 return vecvalue(vec)[ielem];
1301} 1363}
1302 1364
1303INTERFACE void 1365INTERFACE void
1304set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1366vector_set (pointer vec, uint32_t ielem, pointer a)
1305{ 1367{
1306 vecvalue(vec)[ielem] = a; 1368 vecvalue(vec)[ielem] = a;
1307} 1369}
1308 1370
1309/* get new symbol */ 1371/* get new symbol */
1321 1383
1322INTERFACE pointer 1384INTERFACE pointer
1323gensym (SCHEME_P) 1385gensym (SCHEME_P)
1324{ 1386{
1325 pointer x; 1387 pointer x;
1326
1327 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1328 {
1329 char name[40] = "gensym-"; 1388 char name[40] = "gensym-";
1330 xnum (name + 7, SCHEME_V->gensym_cnt); 1389 xnum (name + 7, SCHEME_V->gensym_cnt);
1331 1390
1332 /* first check oblist */ 1391 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} 1392}
1344 1393
1345/* make symbol or number atom from string */ 1394/* make symbol or number atom from string */
1346static pointer 1395static pointer
1347mk_atom (SCHEME_P_ char *q) 1396mk_atom (SCHEME_P_ char *q)
1401 } 1450 }
1402 else if ((c == 'e') || (c == 'E')) 1451 else if ((c == 'e') || (c == 'E'))
1403 { 1452 {
1404 if (!has_fp_exp) 1453 if (!has_fp_exp)
1405 { 1454 {
1406 has_dec_point = 1; /* decimal point illegal 1455 has_dec_point = 1; /* decimal point illegal from now on */
1407 from now on */
1408 p++; 1456 p++;
1409 1457
1410 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1458 if ((*p == '-') || (*p == '+') || isdigit (*p))
1411 continue; 1459 continue;
1412 } 1460 }
1500 1548
1501 if (ecb_expect_false (is_vector (p))) 1549 if (ecb_expect_false (is_vector (p)))
1502 { 1550 {
1503 int i; 1551 int i;
1504 1552
1505 for (i = 0; i < p->object.vector.length; i++) 1553 for (i = 0; i < veclength (p); i++)
1506 mark (vecvalue (p)[i]); 1554 mark (vecvalue (p)[i]);
1507 } 1555 }
1508 1556
1509 if (is_atom (p)) 1557 if (is_atom (p))
1510 goto E6; 1558 goto E6;
1608 if (is_mark (p)) 1656 if (is_mark (p))
1609 clrmark (p); 1657 clrmark (p);
1610 else 1658 else
1611 { 1659 {
1612 /* reclaim cell */ 1660 /* reclaim cell */
1613 if (typeflag (p) != T_FREE) 1661 if (typeflag (p) != T_PAIR)
1614 { 1662 {
1615 finalize_cell (SCHEME_A_ p); 1663 finalize_cell (SCHEME_A_ p);
1616 set_typeflag (p, T_FREE); 1664 set_typeflag (p, T_PAIR);
1617 set_car (p, NIL); 1665 set_car (p, NIL);
1618 } 1666 }
1619 1667
1620 ++SCHEME_V->fcells; 1668 ++SCHEME_V->fcells;
1621 set_cdr (p, SCHEME_V->free_cell); 1669 set_cdr (p, SCHEME_V->free_cell);
1623 } 1671 }
1624 } 1672 }
1625 } 1673 }
1626 1674
1627 if (SCHEME_V->gc_verbose) 1675 if (SCHEME_V->gc_verbose)
1676 {
1628 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1677 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1678 }
1629} 1679}
1630 1680
1631static void 1681static void
1632finalize_cell (SCHEME_P_ pointer a) 1682finalize_cell (SCHEME_P_ pointer a)
1633{ 1683{
2062#endif 2112#endif
2063} 2113}
2064 2114
2065/* read characters up to delimiter, but cater to character constants */ 2115/* read characters up to delimiter, but cater to character constants */
2066static char * 2116static char *
2067readstr_upto (SCHEME_P_ char *delim) 2117readstr_upto (SCHEME_P_ int skip, const char *delim)
2068{ 2118{
2069 char *p = SCHEME_V->strbuff; 2119 char *p = SCHEME_V->strbuff + skip;
2070 2120
2071 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2121 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2072 2122
2073 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2123 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2074 *p = 0; 2124 *p = 0;
2081 return SCHEME_V->strbuff; 2131 return SCHEME_V->strbuff;
2082} 2132}
2083 2133
2084/* read string expression "xxx...xxx" */ 2134/* read string expression "xxx...xxx" */
2085static pointer 2135static pointer
2086readstrexp (SCHEME_P) 2136readstrexp (SCHEME_P_ char delim)
2087{ 2137{
2088 char *p = SCHEME_V->strbuff; 2138 char *p = SCHEME_V->strbuff;
2089 int c; 2139 int c;
2090 int c1 = 0; 2140 int c1 = 0;
2091 enum
2092 { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; 2141 enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok;
2093 2142
2094 for (;;) 2143 for (;;)
2095 { 2144 {
2096 c = inchar (SCHEME_A); 2145 c = inchar (SCHEME_A);
2097 2146
2099 return S_F; 2148 return S_F;
2100 2149
2101 switch (state) 2150 switch (state)
2102 { 2151 {
2103 case st_ok: 2152 case st_ok:
2104 switch (c) 2153 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); 2154 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff);
2113 2155
2114 default: 2156 if (ecb_expect_false (c == '\\'))
2157 state = st_bsl;
2158 else
2115 *p++ = c; 2159 *p++ = c;
2116 break;
2117 }
2118 2160
2119 break; 2161 break;
2120 2162
2121 case st_bsl: 2163 case st_bsl:
2122 switch (c) 2164 switch (c)
2152 case 'r': 2194 case 'r':
2153 *p++ = '\r'; 2195 *p++ = '\r';
2154 state = st_ok; 2196 state = st_ok;
2155 break; 2197 break;
2156 2198
2157 case '"':
2158 *p++ = '"';
2159 state = st_ok;
2160 break;
2161
2162 default: 2199 default:
2163 *p++ = c; 2200 *p++ = c;
2164 state = st_ok; 2201 state = st_ok;
2165 break; 2202 break;
2166 } 2203 }
2167 2204
2168 break; 2205 break;
2169 2206
2170 case st_x1: 2207 case st_x1:
2171 case st_x2: 2208 case st_x2:
2172 c = toupper (c); 2209 c = tolower (c);
2173 2210
2174 if (c >= '0' && c <= 'F') 2211 if (c >= '0' && c <= '9')
2175 {
2176 if (c <= '9')
2177 c1 = (c1 << 4) + c - '0'; 2212 c1 = (c1 << 4) + c - '0';
2178 else 2213 else if (c >= 'a' && c <= 'f')
2179 c1 = (c1 << 4) + c - 'A' + 10; 2214 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 2215 else
2190 return S_F; 2216 return S_F;
2217
2218 if (state == st_x1)
2219 state = st_x2;
2220 else
2221 {
2222 *p++ = c1;
2223 state = st_ok;
2224 }
2191 2225
2192 break; 2226 break;
2193 2227
2194 case st_oct1: 2228 case st_oct1:
2195 case st_oct2: 2229 case st_oct2:
2199 backchar (SCHEME_A_ c); 2233 backchar (SCHEME_A_ c);
2200 state = st_ok; 2234 state = st_ok;
2201 } 2235 }
2202 else 2236 else
2203 { 2237 {
2204 if (state == st_oct2 && c1 >= 32) 2238 if (state == st_oct2 && c1 >= ' ')
2205 return S_F; 2239 return S_F;
2206 2240
2207 c1 = (c1 << 3) + (c - '0'); 2241 c1 = (c1 << 3) + (c - '0');
2208 2242
2209 if (state == st_oct1) 2243 if (state == st_oct1)
2214 state = st_ok; 2248 state = st_ok;
2215 } 2249 }
2216 } 2250 }
2217 2251
2218 break; 2252 break;
2219
2220 } 2253 }
2221 } 2254 }
2222} 2255}
2223 2256
2224/* check c is in chars */ 2257/* check c is in chars */
2225ecb_inline int 2258ecb_inline int
2226is_one_of (char *s, int c) 2259is_one_of (const char *s, int c)
2227{ 2260{
2228 if (c == EOF) 2261 if (c == EOF)
2229 return 1; 2262 return 1;
2230 2263
2231 return !!strchr (s, c); 2264 return !!strchr (s, c);
2287 2320
2288 if (is_one_of (" \n\t", c)) 2321 if (is_one_of (" \n\t", c))
2289 return TOK_DOT; 2322 return TOK_DOT;
2290 else 2323 else
2291 { 2324 {
2292 //TODO: ungetc twice in a row is not supported in C
2293 backchar (SCHEME_A_ c); 2325 backchar (SCHEME_A_ c);
2294 backchar (SCHEME_A_ '.');
2295 return TOK_ATOM; 2326 return TOK_DOTATOM;
2296 } 2327 }
2328
2329 case '|':
2330 return TOK_STRATOM;
2297 2331
2298 case '\'': 2332 case '\'':
2299 return TOK_QUOTE; 2333 return TOK_QUOTE;
2300 2334
2301 case ';': 2335 case ';':
2770#define is_true(p) ((p) != S_F) 2804#define is_true(p) ((p) != S_F)
2771#define is_false(p) ((p) == S_F) 2805#define is_false(p) ((p) == S_F)
2772 2806
2773/* ========== Environment implementation ========== */ 2807/* ========== Environment implementation ========== */
2774 2808
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 2809#ifndef USE_ALIST_ENV
2791 2810
2792/* 2811/*
2793 * In this implementation, each frame of the environment may be 2812 * In this implementation, each frame of the environment may be
2794 * a hash table: a vector of alists hashed by variable name. 2813 * a hash table: a vector of alists hashed by variable name.
2810 2829
2811 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2830 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2812 setenvironment (SCHEME_V->envir); 2831 setenvironment (SCHEME_V->envir);
2813} 2832}
2814 2833
2834static uint32_t
2835sym_hash (pointer sym, uint32_t size)
2836{
2837 uintptr_t ptr = (uintptr_t)sym;
2838
2839#if 0
2840 /* table size is prime, so why mix */
2841 ptr += ptr >> 32;
2842 ptr += ptr >> 16;
2843 ptr += ptr >> 8;
2844#endif
2845
2846 return ptr % size;
2847}
2848
2815ecb_inline void 2849ecb_inline void
2816new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2850new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2817{ 2851{
2818 pointer slot = immutable_cons (variable, value); 2852 pointer slot = immutable_cons (variable, value);
2819 2853
2820 if (is_vector (car (env))) 2854 if (is_vector (car (env)))
2821 { 2855 {
2822 int location = hash_fn (symname (variable), veclength (car (env))); 2856 int location = sym_hash (variable, veclength (car (env)));
2823
2824 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2857 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2825 } 2858 }
2826 else 2859 else
2827 set_car (env, immutable_cons (slot, car (env))); 2860 set_car (env, immutable_cons (slot, car (env)));
2828} 2861}
2829 2862
2830static pointer 2863static pointer
2831find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2864find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2832{ 2865{
2833 pointer x, y; 2866 pointer x, y;
2834 int location;
2835 2867
2836 for (x = env; x != NIL; x = cdr (x)) 2868 for (x = env; x != NIL; x = cdr (x))
2837 { 2869 {
2838 if (is_vector (car (x))) 2870 if (is_vector (car (x)))
2839 { 2871 {
2840 location = hash_fn (symname (hdl), veclength (car (x))); 2872 int location = sym_hash (hdl, veclength (car (x)));
2841 y = vector_elem (car (x), location); 2873 y = vector_get (car (x), location);
2842 } 2874 }
2843 else 2875 else
2844 y = car (x); 2876 y = car (x);
2845 2877
2846 for (; y != NIL; y = cdr (y)) 2878 for (; y != NIL; y = cdr (y))
2847 if (caar (y) == hdl) 2879 if (caar (y) == hdl)
2848 break; 2880 break;
2849 2881
2850 if (y != NIL) 2882 if (y != NIL)
2883 return car (y);
2884
2885 if (!all)
2851 break; 2886 break;
2852
2853 if (!all)
2854 return NIL;
2855 } 2887 }
2856
2857 if (x != NIL)
2858 return car (y);
2859 2888
2860 return NIL; 2889 return NIL;
2861} 2890}
2862 2891
2863#else /* USE_ALIST_ENV */ 2892#else /* USE_ALIST_ENV */
2885 for (y = car (x); y != NIL; y = cdr (y)) 2914 for (y = car (x); y != NIL; y = cdr (y))
2886 if (caar (y) == hdl) 2915 if (caar (y) == hdl)
2887 break; 2916 break;
2888 2917
2889 if (y != NIL) 2918 if (y != NIL)
2919 return car (y);
2890 break; 2920 break;
2891 2921
2892 if (!all) 2922 if (!all)
2893 return NIL; 2923 break;
2894 } 2924 }
2895
2896 if (x != NIL)
2897 return car (y);
2898 2925
2899 return NIL; 2926 return NIL;
2900} 2927}
2901 2928
2902#endif /* USE_ALIST_ENV else */ 2929#endif /* USE_ALIST_ENV else */
3119 int i = 0; 3146 int i = 0;
3120 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3147 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3121 3148
3122 while (cont != NIL) 3149 while (cont != NIL)
3123 { 3150 {
3124 frame->op = ivalue (car (cont)); cont = cdr (cont); 3151 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3125 frame->args = car (cont) ; cont = cdr (cont); 3152 frame->args = car (cont) ; cont = cdr (cont);
3126 frame->envir = car (cont) ; cont = cdr (cont); 3153 frame->envir = car (cont) ; cont = cdr (cont);
3127 frame->code = car (cont) ; cont = cdr (cont); 3154 frame->code = car (cont) ; cont = cdr (cont);
3128 3155
3129 ++frame; 3156 ++frame;
3130 ++i; 3157 ++i;
3131 } 3158 }
3132 3159
3161 SCHEME_V->value = a; 3188 SCHEME_V->value = a;
3162 3189
3163 if (dump == NIL) 3190 if (dump == NIL)
3164 return -1; 3191 return -1;
3165 3192
3166 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3193 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3167 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3194 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3168 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3195 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3169 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3196 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3170 3197
3171 SCHEME_V->dump = dump; 3198 SCHEME_V->dump = dump;
3172 3199
3173 return 0; 3200 return 0;
3174} 3201}
3380 3407
3381 case OP_TRACING: 3408 case OP_TRACING:
3382 { 3409 {
3383 int tr = SCHEME_V->tracing; 3410 int tr = SCHEME_V->tracing;
3384 3411
3385 SCHEME_V->tracing = ivalue (car (args)); 3412 SCHEME_V->tracing = ivalue_unchecked (car (args));
3386 s_return (mk_integer (SCHEME_A_ tr)); 3413 s_return (mk_integer (SCHEME_A_ tr));
3387 } 3414 }
3388 3415
3389#endif 3416#endif
3390 3417
3909{ 3936{
3910 pointer args = SCHEME_V->args; 3937 pointer args = SCHEME_V->args;
3911 pointer x = car (args); 3938 pointer x = car (args);
3912 num v; 3939 num v;
3913 3940
3914#if USE_MATH
3915 RVALUE dd;
3916#endif
3917
3918 switch (op) 3941 switch (op)
3919 { 3942 {
3920#if USE_MATH 3943#if USE_MATH
3921 case OP_INEX2EX: /* inexact->exact */ 3944 case OP_INEX2EX: /* inexact->exact */
3945 {
3922 if (is_integer (x)) 3946 if (is_integer (x))
3923 s_return (x); 3947 s_return (x);
3924 else if (modf (rvalue_unchecked (x), &dd) == 0) 3948
3949 RVALUE r = rvalue_unchecked (x);
3950
3951 if (r == (RVALUE)(IVALUE)r)
3925 s_return (mk_integer (SCHEME_A_ ivalue (x))); 3952 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3926 else 3953 else
3927 Error_1 ("inexact->exact: not integral:", x); 3954 Error_1 ("inexact->exact: not integral:", x);
3955 }
3928 3956
3929 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 3957 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)))); 3958 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)))); 3959 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)))); 3960 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3966 /* If the test fails, result is too big for integer. */ 3994 /* If the test fails, result is too big for integer. */
3967 if (!real_result) 3995 if (!real_result)
3968 { 3996 {
3969 long result_as_long = result; 3997 long result_as_long = result;
3970 3998
3971 if (result != (RVALUE) result_as_long) 3999 if (result != result_as_long)
3972 real_result = 1; 4000 real_result = 1;
3973 } 4001 }
3974 4002
3975 if (real_result) 4003 if (real_result)
3976 s_return (mk_real (SCHEME_A_ result)); 4004 s_return (mk_real (SCHEME_A_ result));
3981 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4009 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)))); 4010 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
3983 4011
3984 case OP_TRUNCATE: 4012 case OP_TRUNCATE:
3985 { 4013 {
3986 RVALUE rvalue_of_x; 4014 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))); 4015 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 } 4016 }
3995 4017
3996 case OP_ROUND: 4018 case OP_ROUND:
3997 if (num_is_integer (x)) 4019 if (is_integer (x))
3998 s_return (x); 4020 s_return (x);
3999 4021
4000 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4022 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4001#endif 4023#endif
4002 4024
4122 } 4144 }
4123 else 4145 else
4124 Error_0 ("set-cdr!: unable to alter immutable pair"); 4146 Error_0 ("set-cdr!: unable to alter immutable pair");
4125 4147
4126 case OP_CHAR2INT: /* char->integer */ 4148 case OP_CHAR2INT: /* char->integer */
4127 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4149 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4128 4150
4129 case OP_INT2CHAR: /* integer->char */ 4151 case OP_INT2CHAR: /* integer->char */
4130 s_return (mk_character (SCHEME_A_ ivalue (x))); 4152 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4131 4153
4132 case OP_CHARUPCASE: 4154 case OP_CHARUPCASE:
4133 { 4155 {
4134 unsigned char c = ivalue (x); 4156 unsigned char c = ivalue_unchecked (x);
4135 c = toupper (c); 4157 c = toupper (c);
4136 s_return (mk_character (SCHEME_A_ c)); 4158 s_return (mk_character (SCHEME_A_ c));
4137 } 4159 }
4138 4160
4139 case OP_CHARDNCASE: 4161 case OP_CHARDNCASE:
4140 { 4162 {
4141 unsigned char c = ivalue (x); 4163 unsigned char c = ivalue_unchecked (x);
4142 c = tolower (c); 4164 c = tolower (c);
4143 s_return (mk_character (SCHEME_A_ c)); 4165 s_return (mk_character (SCHEME_A_ c));
4144 } 4166 }
4145 4167
4146 case OP_STR2SYM: /* string->symbol */ 4168 case OP_STR2SYM: /* string->symbol */
4223 Error_1 ("atom->string: not an atom:", x); 4245 Error_1 ("atom->string: not an atom:", x);
4224 } 4246 }
4225 4247
4226 case OP_MKSTRING: /* make-string */ 4248 case OP_MKSTRING: /* make-string */
4227 { 4249 {
4228 int fill = ' '; 4250 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4229 int len;
4230
4231 len = ivalue (x); 4251 int len = ivalue_unchecked (x);
4232
4233 if (cdr (args) != NIL)
4234 fill = charvalue (cadr (args));
4235 4252
4236 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4253 s_return (mk_empty_string (SCHEME_A_ len, fill));
4237 } 4254 }
4238 4255
4239 case OP_STRLEN: /* string-length */ 4256 case OP_STRLEN: /* string-length */
4240 s_return (mk_integer (SCHEME_A_ strlength (x))); 4257 s_return (mk_integer (SCHEME_A_ strlength (x)));
4241 4258
4242 case OP_STRREF: /* string-ref */ 4259 case OP_STRREF: /* string-ref */
4243 { 4260 {
4244 char *str;
4245 int index;
4246
4247 str = strvalue (x); 4261 char *str = strvalue (x);
4248
4249 index = ivalue (cadr (args)); 4262 int index = ivalue_unchecked (cadr (args));
4250 4263
4251 if (index >= strlength (x)) 4264 if (index >= strlength (x))
4252 Error_1 ("string-ref: out of bounds:", cadr (args)); 4265 Error_1 ("string-ref: out of bounds:", cadr (args));
4253 4266
4254 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4267 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4255 } 4268 }
4256 4269
4257 case OP_STRSET: /* string-set! */ 4270 case OP_STRSET: /* string-set! */
4258 { 4271 {
4259 char *str; 4272 char *str = strvalue (x);
4260 int index; 4273 int index = ivalue_unchecked (cadr (args));
4261 int c; 4274 int c;
4262 4275
4263 if (is_immutable (x)) 4276 if (is_immutable (x))
4264 Error_1 ("string-set!: unable to alter immutable string:", x); 4277 Error_1 ("string-set!: unable to alter immutable string:", x);
4265
4266 str = strvalue (x);
4267
4268 index = ivalue (cadr (args));
4269 4278
4270 if (index >= strlength (x)) 4279 if (index >= strlength (x))
4271 Error_1 ("string-set!: out of bounds:", cadr (args)); 4280 Error_1 ("string-set!: out of bounds:", cadr (args));
4272 4281
4273 c = charvalue (caddr (args)); 4282 c = charvalue (caddr (args));
4296 s_return (newstr); 4305 s_return (newstr);
4297 } 4306 }
4298 4307
4299 case OP_SUBSTR: /* substring */ 4308 case OP_SUBSTR: /* substring */
4300 { 4309 {
4301 char *str; 4310 char *str = strvalue (x);
4302 int index0; 4311 int index0 = ivalue_unchecked (cadr (args));
4303 int index1; 4312 int index1;
4304 int len; 4313 int len;
4305 4314
4306 str = strvalue (x);
4307
4308 index0 = ivalue (cadr (args));
4309
4310 if (index0 > strlength (x)) 4315 if (index0 > strlength (x))
4311 Error_1 ("substring: start out of bounds:", cadr (args)); 4316 Error_1 ("substring: start out of bounds:", cadr (args));
4312 4317
4313 if (cddr (args) != NIL) 4318 if (cddr (args) != NIL)
4314 { 4319 {
4315 index1 = ivalue (caddr (args)); 4320 index1 = ivalue_unchecked (caddr (args));
4316 4321
4317 if (index1 > strlength (x) || index1 < index0) 4322 if (index1 > strlength (x) || index1 < index0)
4318 Error_1 ("substring: end out of bounds:", caddr (args)); 4323 Error_1 ("substring: end out of bounds:", caddr (args));
4319 } 4324 }
4320 else 4325 else
4343 if (SCHEME_V->no_memory) 4348 if (SCHEME_V->no_memory)
4344 s_return (S_SINK); 4349 s_return (S_SINK);
4345#endif 4350#endif
4346 4351
4347 for (x = args, i = 0; is_pair (x); x = cdr (x), i++) 4352 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4348 set_vector_elem (vec, i, car (x)); 4353 vector_set (vec, i, car (x));
4349 4354
4350 s_return (vec); 4355 s_return (vec);
4351 } 4356 }
4352 4357
4353 case OP_MKVECTOR: /* make-vector */ 4358 case OP_MKVECTOR: /* make-vector */
4354 { 4359 {
4355 pointer fill = NIL; 4360 pointer fill = NIL;
4356 int len;
4357 pointer vec; 4361 pointer vec;
4358
4359 len = ivalue (x); 4362 int len = ivalue_unchecked (x);
4360 4363
4361 if (cdr (args) != NIL) 4364 if (cdr (args) != NIL)
4362 fill = cadr (args); 4365 fill = cadr (args);
4363 4366
4364 vec = mk_vector (SCHEME_A_ len); 4367 vec = mk_vector (SCHEME_A_ len);
4367 if (SCHEME_V->no_memory) 4370 if (SCHEME_V->no_memory)
4368 s_return (S_SINK); 4371 s_return (S_SINK);
4369#endif 4372#endif
4370 4373
4371 if (fill != NIL) 4374 if (fill != NIL)
4372 fill_vector (vec, fill); 4375 fill_vector (vec, 0, fill);
4373 4376
4374 s_return (vec); 4377 s_return (vec);
4375 } 4378 }
4376 4379
4377 case OP_VECLEN: /* vector-length */ 4380 case OP_VECLEN: /* vector-length */
4378 s_return (mk_integer (SCHEME_A_ veclength (x))); 4381 s_return (mk_integer (SCHEME_A_ veclength (x)));
4379 4382
4380 case OP_VECREF: /* vector-ref */ 4383 case OP_VECREF: /* vector-ref */
4381 { 4384 {
4382 int index;
4383
4384 index = ivalue (cadr (args)); 4385 int index = ivalue_unchecked (cadr (args));
4385 4386
4386 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4387 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4387 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4388 Error_1 ("vector-ref: out of bounds:", cadr (args));
4388 4389
4389 s_return (vector_elem (x, index)); 4390 s_return (vector_get (x, index));
4390 } 4391 }
4391 4392
4392 case OP_VECSET: /* vector-set! */ 4393 case OP_VECSET: /* vector-set! */
4393 { 4394 {
4394 int index; 4395 int index = ivalue_unchecked (cadr (args));
4395 4396
4396 if (is_immutable (x)) 4397 if (is_immutable (x))
4397 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4398 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4398 4399
4399 index = ivalue (cadr (args));
4400
4401 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4400 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4402 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4401 Error_1 ("vector-set!: out of bounds:", cadr (args));
4403 4402
4404 set_vector_elem (x, index, caddr (args)); 4403 vector_set (x, index, caddr (args));
4405 s_return (x); 4404 s_return (x);
4406 } 4405 }
4407 } 4406 }
4408 4407
4409 if (USE_ERROR_CHECKING) abort (); 4408 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} 4409}
4465 4410
4466static int 4411static int
4467opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4412opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4468{ 4413{
4514 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4459 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4515 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4460 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4516 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4461 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4517 4462
4518#if USE_CHAR_CLASSIFIERS 4463#if USE_CHAR_CLASSIFIERS
4519 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4464 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4520 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4465 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4521 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4466 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4522 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4467 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4523 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4468 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4524#endif 4469#endif
4525 4470
4526#if USE_PORTS 4471#if USE_PORTS
4527 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4472 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4528 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4473 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4729 4674
4730 case OP_NEWSEGMENT: /* new-segment */ 4675 case OP_NEWSEGMENT: /* new-segment */
4731 if (!is_pair (args) || !is_number (a)) 4676 if (!is_pair (args) || !is_number (a))
4732 Error_0 ("new-segment: argument must be a number"); 4677 Error_0 ("new-segment: argument must be a number");
4733 4678
4734 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 4679 alloc_cellseg (SCHEME_A_ ivalue (a));
4735 4680
4736 s_return (S_T); 4681 s_return (S_T);
4737 4682
4738 case OP_OBLIST: /* oblist */ 4683 case OP_OBLIST: /* oblist */
4739 s_return (oblist_all_symbols (SCHEME_A)); 4684 s_return (oblist_all_symbols (SCHEME_A));
4997 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 4942 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
4998 SCHEME_V->tok = token (SCHEME_A); 4943 SCHEME_V->tok = token (SCHEME_A);
4999 s_goto (OP_RDSEXPR); 4944 s_goto (OP_RDSEXPR);
5000 4945
5001 case TOK_ATOM: 4946 case TOK_ATOM:
5002 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); 4947 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS)));
4948
4949 case TOK_DOTATOM:
4950 SCHEME_V->strbuff[0] = '.';
4951 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5003 4952
5004 case TOK_DQUOTE: 4953 case TOK_DQUOTE:
5005 x = readstrexp (SCHEME_A); 4954 x = readstrexp (SCHEME_A_ '"');
5006 4955
5007 if (x == S_F) 4956 if (x == S_F)
5008 Error_0 ("Error reading string"); 4957 Error_0 ("Error reading string");
5009 4958
5010 setimmutable (x); 4959 setimmutable (x);
5022 s_goto (OP_EVAL); 4971 s_goto (OP_EVAL);
5023 } 4972 }
5024 } 4973 }
5025 4974
5026 case TOK_SHARP_CONST: 4975 case TOK_SHARP_CONST:
5027 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) 4976 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5028 Error_0 ("undefined sharp expression"); 4977 Error_0 ("undefined sharp expression");
5029 else 4978 else
5030 s_return (x); 4979 s_return (x);
5031 4980
5032 default: 4981 default:
5184 putstr (SCHEME_A_ ")"); 5133 putstr (SCHEME_A_ ")");
5185 s_return (S_T); 5134 s_return (S_T);
5186 } 5135 }
5187 else 5136 else
5188 { 5137 {
5189 pointer elem = vector_elem (vec, i); 5138 pointer elem = vector_get (vec, i);
5190 5139
5191 ivalue_unchecked (cdr (args)) = i + 1; 5140 ivalue_unchecked (cdr (args)) = i + 1;
5192 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5141 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5193 SCHEME_V->args = elem; 5142 SCHEME_V->args = elem;
5194 5143
5269/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5218/* 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); 5219typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5271 5220
5272typedef int (*test_predicate)(pointer); 5221typedef int (*test_predicate)(pointer);
5273static int 5222static int
5274is_any (pointer p) 5223tst_any (pointer p)
5275{ 5224{
5276 return 1; 5225 return 1;
5277} 5226}
5278 5227
5279static int 5228static int
5280is_nonneg (pointer p) 5229tst_inonneg (pointer p)
5281{ 5230{
5282 return ivalue (p) >= 0 && is_integer (p); 5231 return is_integer (p) && ivalue_unchecked (p) >= 0;
5283} 5232}
5284 5233
5285static int 5234static int
5286tst_is_list (pointer p) 5235tst_is_list (SCHEME_P_ pointer p)
5287{ 5236{
5288 return p == NIL || is_pair (p); 5237 return p == NIL || is_pair (p);
5289} 5238}
5290 5239
5291/* Correspond carefully with following defines! */ 5240/* Correspond carefully with following defines! */
5292static struct 5241static struct
5293{ 5242{
5294 test_predicate fct; 5243 test_predicate fct;
5295 const char *kind; 5244 const char *kind;
5296} tests[] = 5245} tests[] = {
5297{
5298 { is_any, 0 }, 5246 { tst_any , 0 },
5299 { is_string, "string" }, 5247 { is_string , "string" },
5300 { is_symbol, "symbol" }, 5248 { is_symbol , "symbol" },
5301 { is_port, "port" }, 5249 { is_port , "port" },
5302 { is_inport, "input port" }, 5250 { is_inport , "input port" },
5303 { is_outport, "output port" }, 5251 { is_outport , "output port" },
5304 { is_environment, "environment" }, 5252 { is_environment, "environment" },
5305 { is_pair, "pair" }, 5253 { is_pair , "pair" },
5306 { tst_is_list, "pair or '()" }, 5254 { 0 , "pair or '()" },
5307 { is_character, "character" }, 5255 { is_character , "character" },
5308 { is_vector, "vector" }, 5256 { is_vector , "vector" },
5309 { is_number, "number" }, 5257 { is_number , "number" },
5310 { is_integer, "integer" }, 5258 { is_integer , "integer" },
5311 { is_nonneg, "non-negative integer" } 5259 { tst_inonneg , "non-negative integer" }
5312}; 5260};
5313 5261
5314#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ 5262#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5315#define TST_ANY "\001" 5263#define TST_ANY "\001"
5316#define TST_STRING "\002" 5264#define TST_STRING "\002"
5357typedef struct 5305typedef struct
5358{ 5306{
5359 uint8_t func; 5307 uint8_t func;
5360 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ 5308 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5361 uint8_t builtin; 5309 uint8_t builtin;
5310#if USE_ERROR_CHECKING
5362 uint8_t min_arity; 5311 uint8_t min_arity;
5363 uint8_t max_arity; 5312 uint8_t max_arity;
5364 char arg_tests_encoding[3]; 5313 char arg_tests_encoding[3];
5314#endif
5365} op_code_info; 5315} op_code_info;
5366 5316
5367static const op_code_info dispatch_table[] = { 5317static const op_code_info dispatch_table[] = {
5318#if USE_ERROR_CHECKING
5368#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, 5319#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5320#else
5321#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5322#endif
5369#include "opdefines.h" 5323#include "opdefines.h"
5370#undef OP_DEF 5324#undef OP_DEF
5371 {0} 5325 {0}
5372}; 5326};
5373 5327
5415 { 5369 {
5416 pointer arg = car (arglist); 5370 pointer arg = car (arglist);
5417 5371
5418 j = t[0]; 5372 j = t[0];
5419 5373
5374 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5375 if (j == TST_LIST[0])
5376 {
5377 if (!tst_is_list (SCHEME_A_ arg))
5378 break;
5379 }
5380 else
5381 {
5420 if (!tests[j - 1].fct (arg)) 5382 if (!tests[j - 1].fct (arg))
5421 break; 5383 break;
5384 }
5422 5385
5423 if (t[1]) /* last test is replicated as necessary */ 5386 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5424 t++; 5387 t++;
5425 5388
5426 arglist = cdr (arglist); 5389 arglist = cdr (arglist);
5427 i++; 5390 i++;
5428 } 5391 }
5483mk_proc (SCHEME_P_ enum scheme_opcodes op) 5446mk_proc (SCHEME_P_ enum scheme_opcodes op)
5484{ 5447{
5485 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5448 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5486 set_typeflag (y, (T_PROC | T_ATOM)); 5449 set_typeflag (y, (T_PROC | T_ATOM));
5487 ivalue_unchecked (y) = op; 5450 ivalue_unchecked (y) = op;
5488 set_num_integer (y);
5489 return y; 5451 return y;
5490} 5452}
5491 5453
5492/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5454/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5493static int 5455static int

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines