ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/microscheme/scheme.c
(Generate patch)

Comparing cvsroot/microscheme/scheme.c (file contents):
Revision 1.24 by root, Fri Nov 27 02:12:08 2015 UTC vs.
Revision 1.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);
219#if USE_MATH 260#if USE_MATH
220static double round_per_R5RS (double x); 261static double round_per_R5RS (double x);
221#endif 262#endif
222static int is_zero_rvalue (RVALUE x); 263static int is_zero_rvalue (RVALUE x);
223 264
224ecb_inline int
225num_is_integer (pointer p)
226{
227 return num_is_fixnum (p->object.number);
228}
229
230static num num_zero; 265static num num_zero;
231static num num_one; 266static num num_one;
232 267
233/* macros for cell operations */ 268/* macros for cell operations */
234#define typeflag(p) ((p)->flag + 0) 269#define typeflag(p) ((p)->flag + 0)
242} 277}
243 278
244#define strvalue(p) ((p)->object.string.svalue) 279#define strvalue(p) ((p)->object.string.svalue)
245#define strlength(p) ((p)->object.string.length) 280#define strlength(p) ((p)->object.string.length)
246 281
247INTERFACE int is_list (SCHEME_P_ pointer p);
248
249INTERFACE int 282INTERFACE int
250is_vector (pointer p) 283is_vector (pointer p)
251{ 284{
252 return type (p) == T_VECTOR; 285 return type (p) == T_VECTOR;
253} 286}
254 287
255#define vecvalue(p) ((p)->object.vector.vvalue) 288#define vecvalue(p) ((p)->object.vector.vvalue)
256#define veclength(p) ((p)->object.vector.length) 289#define veclength(p) ((p)->object.vector.length)
257INTERFACE void fill_vector (pointer vec, pointer obj); 290INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
258INTERFACE uint32_t vector_length (pointer vec);
259INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); 291INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
260INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); 292INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
261 293
262INTERFACE uint32_t 294INTERFACE int
263vector_length (pointer vec) 295is_integer (pointer p)
264{ 296{
265 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;
266} 305}
267 306
268INTERFACE int 307INTERFACE int
269is_number (pointer p) 308is_number (pointer p)
270{ 309{
271 return type (p) == T_NUMBER; 310 return is_integer (p) || is_real (p);
272}
273
274INTERFACE int
275is_integer (pointer p)
276{
277 if (!is_number (p))
278 return 0;
279
280 if (num_is_integer (p) || ivalue (p) == rvalue (p))
281 return 1;
282
283 return 0;
284}
285
286INTERFACE int
287is_real (pointer p)
288{
289 return is_number (p) && !num_is_fixnum (p->object.number);
290} 311}
291 312
292INTERFACE int 313INTERFACE int
293is_character (pointer p) 314is_character (pointer p)
294{ 315{
299string_value (pointer p) 320string_value (pointer p)
300{ 321{
301 return strvalue (p); 322 return strvalue (p);
302} 323}
303 324
304ecb_inline num
305nvalue (pointer p)
306{
307 return (p)->object.number;
308}
309
310static IVALUE
311num_get_ivalue (const num n)
312{
313 return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n);
314}
315
316static RVALUE
317num_get_rvalue (const num n)
318{
319 return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n);
320}
321
322INTERFACE IVALUE
323ivalue (pointer p)
324{
325 return num_get_ivalue (p->object.number);
326}
327
328INTERFACE RVALUE
329rvalue (pointer p)
330{
331 return num_get_rvalue (p->object.number);
332}
333
334#define ivalue_unchecked(p) ((p)->object.number.value.ivalue) 325#define ivalue_unchecked(p) (p)->object.ivalue
326#define set_ivalue(p,v) (p)->object.ivalue = (v)
327
335#if USE_REAL 328#if USE_REAL
336# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 329#define rvalue_unchecked(p) (p)->object.rvalue
337# define set_num_integer(p) (p)->object.number.is_fixnum=1; 330#define set_rvalue(p,v) (p)->object.rvalue = (v)
338# define set_num_real(p) (p)->object.number.is_fixnum=0;
339#else 331#else
340# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 332#define rvalue_unchecked(p) (p)->object.ivalue
341# define set_num_integer(p) 0 333#define set_rvalue(p,v) (p)->object.ivalue = (v)
342# define set_num_real(p) 0
343#endif 334#endif
344 335
345INTERFACE long 336INTERFACE long
346charvalue (pointer p) 337charvalue (pointer p)
347{ 338{
452syntaxname (pointer p) 443syntaxname (pointer p)
453{ 444{
454 return strvalue (car (p)); 445 return strvalue (car (p));
455} 446}
456 447
457#define procnum(p) ivalue (p) 448#define procnum(p) ivalue_unchecked (p)
458static const char *procname (pointer x); 449static const char *procname (pointer x);
459 450
460INTERFACE int 451INTERFACE int
461is_closure (pointer p) 452is_closure (pointer p)
462{ 453{
523setimmutable (pointer p) 514setimmutable (pointer p)
524{ 515{
525#if USE_ERROR_CHECKING 516#if USE_ERROR_CHECKING
526 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 517 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
527#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;
528} 573}
529 574
530#if USE_CHAR_CLASSIFIERS 575#if USE_CHAR_CLASSIFIERS
531ecb_inline int 576ecb_inline int
532Cisalpha (int c) 577Cisalpha (int c)
621#endif 666#endif
622 667
623static int file_push (SCHEME_P_ const char *fname); 668static int file_push (SCHEME_P_ const char *fname);
624static void file_pop (SCHEME_P); 669static void file_pop (SCHEME_P);
625static int file_interactive (SCHEME_P); 670static int file_interactive (SCHEME_P);
626ecb_inline int is_one_of (char *s, int c); 671ecb_inline int is_one_of (const char *s, int c);
627static int alloc_cellseg (SCHEME_P_ int n); 672static int alloc_cellseg (SCHEME_P_ int n);
628ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); 673ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
629static void finalize_cell (SCHEME_P_ pointer a); 674static void finalize_cell (SCHEME_P_ pointer a);
630static int count_consecutive_cells (pointer x, int needed); 675static int count_consecutive_cells (pointer x, int needed);
631static 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);
648static void mark (pointer a); 693static void mark (pointer a);
649static void gc (SCHEME_P_ pointer a, pointer b); 694static void gc (SCHEME_P_ pointer a, pointer b);
650static int basic_inchar (port *pt); 695static int basic_inchar (port *pt);
651static int inchar (SCHEME_P); 696static int inchar (SCHEME_P);
652static void backchar (SCHEME_P_ int c); 697static void backchar (SCHEME_P_ int c);
653static char *readstr_upto (SCHEME_P_ char *delim); 698static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
654static pointer readstrexp (SCHEME_P); 699static pointer readstrexp (SCHEME_P_ char delim);
655ecb_inline int skipspace (SCHEME_P); 700ecb_inline int skipspace (SCHEME_P);
656static int token (SCHEME_P); 701static int token (SCHEME_P);
657static void printslashstring (SCHEME_P_ char *s, int len); 702static void printslashstring (SCHEME_P_ char *s, int len);
658static 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);
659static void printatom (SCHEME_P_ pointer l, int f); 704static void printatom (SCHEME_P_ pointer l, int f);
676static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 721static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
677static void assign_syntax (SCHEME_P_ const char *name); 722static void assign_syntax (SCHEME_P_ const char *name);
678static int syntaxnum (pointer p); 723static int syntaxnum (pointer p);
679static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 724static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
680 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
681static num 753static num
682num_op (enum num_op op, num a, num b) 754num_op (enum num_op op, num a, num b)
683{ 755{
684 num ret; 756 num ret;
685 757
686 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));
687 759
688 if (num_is_fixnum (ret)) 760 if (num_is_fixnum (ret))
689 { 761 {
690 IVALUE av = num_get_ivalue (a);
691 IVALUE bv = num_get_ivalue (b);
692
693 switch (op) 762 switch (op)
694 { 763 {
695 case NUM_ADD: av += bv; break; 764 case NUM_ADD: a.ivalue += b.ivalue; break;
696 case NUM_SUB: av -= bv; break; 765 case NUM_SUB: a.ivalue -= b.ivalue; break;
697 case NUM_MUL: av *= bv; break; 766 case NUM_MUL: a.ivalue *= b.ivalue; break;
698 case NUM_INTDIV: av /= bv; break; 767 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
699 } 768 }
700 769
701 num_set_ivalue (ret, av); 770 num_set_ivalue (ret, a.ivalue);
702 } 771 }
772#if USE_REAL
703 else 773 else
704 { 774 {
705 RVALUE av = num_get_rvalue (a);
706 RVALUE bv = num_get_rvalue (b);
707
708 switch (op) 775 switch (op)
709 { 776 {
710 case NUM_ADD: av += bv; break; 777 case NUM_ADD: a.rvalue += b.rvalue; break;
711 case NUM_SUB: av -= bv; break; 778 case NUM_SUB: a.rvalue -= b.rvalue; break;
712 case NUM_MUL: av *= bv; break; 779 case NUM_MUL: a.rvalue *= b.rvalue; break;
713 case NUM_INTDIV: av /= bv; break; 780 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
714 } 781 }
715 782
716 num_set_rvalue (ret, av); 783 num_set_rvalue (ret, a.rvalue);
717 } 784 }
785#endif
718 786
719 return ret; 787 return ret;
720} 788}
721 789
722static num 790static num
723num_div (num a, num b) 791num_div (num a, num b)
724{ 792{
725 num ret; 793 num ret;
726 794
727 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);
728 796
729 if (num_is_fixnum (ret)) 797 if (num_is_fixnum (ret))
730 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 798 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
731 else 799 else
732 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 800 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
733 801
734 return ret; 802 return ret;
735} 803}
736 804
737static num 805static num
739{ 807{
740 num ret; 808 num ret;
741 long e1, e2, res; 809 long e1, e2, res;
742 810
743 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));
744 e1 = num_get_ivalue (a); 812 e1 = num_ivalue (a);
745 e2 = num_get_ivalue (b); 813 e2 = num_ivalue (b);
746 res = e1 % e2; 814 res = e1 % e2;
747 815
748 /* remainder should have same sign as second operand */ 816 /* remainder should have same sign as second operand */
749 if (res > 0) 817 if (res > 0)
750 { 818 {
766{ 834{
767 num ret; 835 num ret;
768 long e1, e2, res; 836 long e1, e2, res;
769 837
770 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));
771 e1 = num_get_ivalue (a); 839 e1 = num_ivalue (a);
772 e2 = num_get_ivalue (b); 840 e2 = num_ivalue (b);
773 res = e1 % e2; 841 res = e1 % e2;
774 842
775 /* modulo should have same sign as second operand */ 843 /* modulo should have same sign as second operand */
776 if (res * e2 < 0) 844 if (res * e2 < 0)
777 res += e2; 845 res += e2;
787 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 855 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
788 int ret; 856 int ret;
789 857
790 if (is_fixnum) 858 if (is_fixnum)
791 { 859 {
792 IVALUE av = num_get_ivalue (a); 860 IVALUE av = num_ivalue (a);
793 IVALUE bv = num_get_ivalue (b); 861 IVALUE bv = num_ivalue (b);
794 862
795 ret = av == bv ? 0 : av < bv ? -1 : +1; 863 ret = av == bv ? 0 : av < bv ? -1 : +1;
796 } 864 }
797 else 865 else
798 { 866 {
799 RVALUE av = num_get_rvalue (a); 867 RVALUE av = num_rvalue (a);
800 RVALUE bv = num_get_rvalue (b); 868 RVALUE bv = num_rvalue (b);
801 869
802 ret = av == bv ? 0 : av < bv ? -1 : +1; 870 ret = av == bv ? 0 : av < bv ? -1 : +1;
803 } 871 }
804 872
805 return ret; 873 return ret;
868 return k; 936 return k;
869 937
870 i = ++SCHEME_V->last_cell_seg; 938 i = ++SCHEME_V->last_cell_seg;
871 SCHEME_V->alloc_seg[i] = cp; 939 SCHEME_V->alloc_seg[i] = cp;
872 940
873 /* insert new segment in address order */
874 newp = (pointer)cp; 941 newp = (pointer)cp;
875 SCHEME_V->cell_seg[i] = newp; 942 SCHEME_V->cell_seg[i] = newp;
876 SCHEME_V->cell_segsize[i] = segsize; 943 SCHEME_V->cell_segsize[i] = segsize;
877
878 //TODO: insert, not swap
879 while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i])
880 {
881 p = SCHEME_V->cell_seg[i];
882 SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1];
883 SCHEME_V->cell_seg[i - 1] = p;
884
885 k = SCHEME_V->cell_segsize[i];
886 SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1];
887 SCHEME_V->cell_segsize[i - 1] = k;
888
889 --i;
890 }
891
892 SCHEME_V->fcells += segsize; 944 SCHEME_V->fcells += segsize;
893 last = newp + segsize - 1; 945 last = newp + segsize - 1;
894 946
895 for (p = newp; p <= last; p++) 947 for (p = newp; p <= last; p++)
896 { 948 {
897 set_typeflag (p, T_FREE); 949 set_typeflag (p, T_PAIR);
898 set_car (p, NIL); 950 set_car (p, NIL);
899 set_cdr (p, p + 1); 951 set_cdr (p, p + 1);
900 } 952 }
901 953
902 /* insert new cells in address order on free list */
903 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell)
904 {
905 set_cdr (last, SCHEME_V->free_cell); 954 set_cdr (last, SCHEME_V->free_cell);
906 SCHEME_V->free_cell = newp; 955 SCHEME_V->free_cell = newp;
907 }
908 else
909 {
910 p = SCHEME_V->free_cell;
911
912 while (cdr (p) != NIL && newp > cdr (p))
913 p = cdr (p);
914
915 set_cdr (last, cdr (p));
916 set_cdr (p, newp);
917 }
918 } 956 }
919 957
920 return n; 958 return n;
921} 959}
922 960
1001 /* Record it as a vector so that gc understands it. */ 1039 /* Record it as a vector so that gc understands it. */
1002 set_typeflag (v, T_VECTOR | T_ATOM); 1040 set_typeflag (v, T_VECTOR | T_ATOM);
1003 1041
1004 v->object.vector.vvalue = e; 1042 v->object.vector.vvalue = e;
1005 v->object.vector.length = len; 1043 v->object.vector.length = len;
1006 fill_vector (v, init); 1044 fill_vector (v, 0, init);
1007 push_recent_alloc (SCHEME_A_ v, NIL); 1045 push_recent_alloc (SCHEME_A_ v, NIL);
1008 1046
1009 return v; 1047 return v;
1010} 1048}
1011 1049
1056 return x; 1094 return x;
1057} 1095}
1058 1096
1059/* ========== oblist implementation ========== */ 1097/* ========== oblist implementation ========== */
1060 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
1061#ifndef USE_OBJECT_LIST 1109#ifndef USE_OBJECT_LIST
1062 1110
1111static int
1063static 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}
1064 1122
1065static pointer 1123static pointer
1066oblist_initial_value (SCHEME_P) 1124oblist_initial_value (SCHEME_P)
1067{ 1125{
1068 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1126 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1070 1128
1071/* returns the new symbol */ 1129/* returns the new symbol */
1072static pointer 1130static pointer
1073oblist_add_by_name (SCHEME_P_ const char *name) 1131oblist_add_by_name (SCHEME_P_ const char *name)
1074{ 1132{
1075 int location; 1133 pointer x = generate_symbol (SCHEME_A_ name);
1076
1077 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1078 set_typeflag (x, T_SYMBOL);
1079 setimmutable (car (x));
1080
1081 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1134 int location = hash_fn (name, veclength (SCHEME_V->oblist));
1082 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); 1135 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1083 return x; 1136 return x;
1084} 1137}
1085 1138
1086ecb_inline pointer 1139ecb_inline pointer
1087oblist_find_by_name (SCHEME_P_ const char *name) 1140oblist_find_by_name (SCHEME_P_ const char *name)
1090 pointer x; 1143 pointer x;
1091 char *s; 1144 char *s;
1092 1145
1093 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1146 location = hash_fn (name, veclength (SCHEME_V->oblist));
1094 1147
1095 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))
1096 { 1149 {
1097 s = symname (car (x)); 1150 s = symname (car (x));
1098 1151
1099 /* case-insensitive, per R5RS section 2 */ 1152 /* case-insensitive, per R5RS section 2 */
1100 if (stricmp (name, s) == 0) 1153 if (stricmp (name, s) == 0)
1110 int i; 1163 int i;
1111 pointer x; 1164 pointer x;
1112 pointer ob_list = NIL; 1165 pointer ob_list = NIL;
1113 1166
1114 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1167 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1115 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))
1116 ob_list = cons (x, ob_list); 1169 ob_list = cons (x, ob_list);
1117 1170
1118 return ob_list; 1171 return ob_list;
1119} 1172}
1120 1173
1146 1199
1147/* returns the new symbol */ 1200/* returns the new symbol */
1148static pointer 1201static pointer
1149oblist_add_by_name (SCHEME_P_ const char *name) 1202oblist_add_by_name (SCHEME_P_ const char *name)
1150{ 1203{
1151 pointer x; 1204 pointer x = mk_string (SCHEME_A_ name);
1152
1153 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1154 set_typeflag (x, T_SYMBOL); 1205 set_typeflag (x, T_SYMBOL);
1155 setimmutable (car (x)); 1206 setimmutable (x);
1156 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1207 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1157 return x; 1208 return x;
1158} 1209}
1159 1210
1160static pointer 1211static pointer
1193mk_character (SCHEME_P_ int c) 1244mk_character (SCHEME_P_ int c)
1194{ 1245{
1195 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1246 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1196 1247
1197 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1248 set_typeflag (x, (T_CHARACTER | T_ATOM));
1198 ivalue_unchecked (x) = c & 0xff; 1249 set_ivalue (x, c & 0xff);
1199 set_num_integer (x); 1250
1200 return x; 1251 return x;
1201} 1252}
1202 1253
1203/* get number atom (integer) */ 1254/* get number atom (integer) */
1204INTERFACE pointer 1255INTERFACE pointer
1205mk_integer (SCHEME_P_ long num) 1256mk_integer (SCHEME_P_ long n)
1206{ 1257{
1207 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1258 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1208 1259
1209 set_typeflag (x, (T_NUMBER | T_ATOM)); 1260 set_typeflag (x, (T_INTEGER | T_ATOM));
1210 ivalue_unchecked (x) = num; 1261 set_ivalue (x, n);
1211 set_num_integer (x); 1262
1212 return x; 1263 return x;
1213} 1264}
1214 1265
1215INTERFACE pointer 1266INTERFACE pointer
1216mk_real (SCHEME_P_ RVALUE n) 1267mk_real (SCHEME_P_ RVALUE n)
1217{ 1268{
1269#if USE_REAL
1218 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1270 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1219 1271
1220 set_typeflag (x, (T_NUMBER | T_ATOM)); 1272 set_typeflag (x, (T_REAL | T_ATOM));
1221 rvalue_unchecked (x) = n; 1273 set_rvalue (x, n);
1222 set_num_real (x); 1274
1223 return x; 1275 return x;
1276#else
1277 return mk_integer (SCHEME_A_ n);
1278#endif
1224} 1279}
1225 1280
1226static pointer 1281static pointer
1227mk_number (SCHEME_P_ const num n) 1282mk_number (SCHEME_P_ const num n)
1228{ 1283{
1284#if USE_REAL
1229 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
1230 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1289 return mk_integer (SCHEME_A_ num_ivalue (n));
1231 else 1290#endif
1232 return mk_real (SCHEME_A_ num_get_rvalue (n));
1233} 1291}
1234 1292
1235/* allocate name to string area */ 1293/* allocate name to string area */
1236static char * 1294static char *
1237store_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)
1243 SCHEME_V->no_memory = 1; 1301 SCHEME_V->no_memory = 1;
1244 return SCHEME_V->strbuff; 1302 return SCHEME_V->strbuff;
1245 } 1303 }
1246 1304
1247 if (str) 1305 if (str)
1248 { 1306 memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */
1249 int l = strlen (str);
1250
1251 if (l > len_str)
1252 l = len_str;
1253
1254 memcpy (q, str, l);
1255 q[l] = 0;
1256 }
1257 else 1307 else
1258 {
1259 memset (q, fill, len_str); 1308 memset (q, fill, len_str);
1309
1260 q[len_str] = 0; 1310 q[len_str] = 0;
1261 }
1262 1311
1263 return q; 1312 return q;
1264} 1313}
1265 1314
1266INTERFACE pointer 1315INTERFACE pointer
1280 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1329 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1281 1330
1282 set_typeflag (x, T_STRING | T_ATOM); 1331 set_typeflag (x, T_STRING | T_ATOM);
1283 strvalue (x) = store_string (SCHEME_A_ len, str, 0); 1332 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1284 strlength (x) = len; 1333 strlength (x) = len;
1334
1285 return x; 1335 return x;
1286} 1336}
1287 1337
1288INTERFACE pointer 1338INTERFACE pointer
1289mk_string (SCHEME_P_ const char *str) 1339mk_string (SCHEME_P_ const char *str)
1296{ 1346{
1297 return get_vector_object (SCHEME_A_ len, NIL); 1347 return get_vector_object (SCHEME_A_ len, NIL);
1298} 1348}
1299 1349
1300INTERFACE void 1350INTERFACE void
1301fill_vector (pointer vec, pointer obj) 1351fill_vector (pointer vec, uint32_t start, pointer obj)
1302{ 1352{
1303 int i; 1353 int i;
1304 1354
1305 for (i = 0; i < vec->object.vector.length; i++) 1355 for (i = start; i < veclength (vec); i++)
1306 vecvalue (vec)[i] = obj; 1356 vecvalue (vec)[i] = obj;
1307} 1357}
1308 1358
1309INTERFACE pointer 1359INTERFACE pointer
1310vector_elem (pointer vec, uint32_t ielem) 1360vector_get (pointer vec, uint32_t ielem)
1311{ 1361{
1312 return vecvalue(vec)[ielem]; 1362 return vecvalue(vec)[ielem];
1313} 1363}
1314 1364
1315INTERFACE void 1365INTERFACE void
1316set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1366vector_set (pointer vec, uint32_t ielem, pointer a)
1317{ 1367{
1318 vecvalue(vec)[ielem] = a; 1368 vecvalue(vec)[ielem] = a;
1319} 1369}
1320 1370
1321/* get new symbol */ 1371/* get new symbol */
1333 1383
1334INTERFACE pointer 1384INTERFACE pointer
1335gensym (SCHEME_P) 1385gensym (SCHEME_P)
1336{ 1386{
1337 pointer x; 1387 pointer x;
1338
1339 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1340 {
1341 char name[40] = "gensym-"; 1388 char name[40] = "gensym-";
1342 xnum (name + 7, SCHEME_V->gensym_cnt); 1389 xnum (name + 7, SCHEME_V->gensym_cnt);
1343 1390
1344 /* first check oblist */ 1391 return generate_symbol (SCHEME_A_ name);
1345 x = oblist_find_by_name (SCHEME_A_ name);
1346
1347 if (x == NIL)
1348 {
1349 x = oblist_add_by_name (SCHEME_A_ name);
1350 return x;
1351 }
1352 }
1353
1354 return NIL;
1355} 1392}
1356 1393
1357/* make symbol or number atom from string */ 1394/* make symbol or number atom from string */
1358static pointer 1395static pointer
1359mk_atom (SCHEME_P_ char *q) 1396mk_atom (SCHEME_P_ char *q)
1413 } 1450 }
1414 else if ((c == 'e') || (c == 'E')) 1451 else if ((c == 'e') || (c == 'E'))
1415 { 1452 {
1416 if (!has_fp_exp) 1453 if (!has_fp_exp)
1417 { 1454 {
1418 has_dec_point = 1; /* decimal point illegal 1455 has_dec_point = 1; /* decimal point illegal from now on */
1419 from now on */
1420 p++; 1456 p++;
1421 1457
1422 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1458 if ((*p == '-') || (*p == '+') || isdigit (*p))
1423 continue; 1459 continue;
1424 } 1460 }
1512 1548
1513 if (ecb_expect_false (is_vector (p))) 1549 if (ecb_expect_false (is_vector (p)))
1514 { 1550 {
1515 int i; 1551 int i;
1516 1552
1517 for (i = 0; i < p->object.vector.length; i++) 1553 for (i = 0; i < veclength (p); i++)
1518 mark (vecvalue (p)[i]); 1554 mark (vecvalue (p)[i]);
1519 } 1555 }
1520 1556
1521 if (is_atom (p)) 1557 if (is_atom (p))
1522 goto E6; 1558 goto E6;
1620 if (is_mark (p)) 1656 if (is_mark (p))
1621 clrmark (p); 1657 clrmark (p);
1622 else 1658 else
1623 { 1659 {
1624 /* reclaim cell */ 1660 /* reclaim cell */
1625 if (typeflag (p) != T_FREE) 1661 if (typeflag (p) != T_PAIR)
1626 { 1662 {
1627 finalize_cell (SCHEME_A_ p); 1663 finalize_cell (SCHEME_A_ p);
1628 set_typeflag (p, T_FREE); 1664 set_typeflag (p, T_PAIR);
1629 set_car (p, NIL); 1665 set_car (p, NIL);
1630 } 1666 }
1631 1667
1632 ++SCHEME_V->fcells; 1668 ++SCHEME_V->fcells;
1633 set_cdr (p, SCHEME_V->free_cell); 1669 set_cdr (p, SCHEME_V->free_cell);
1635 } 1671 }
1636 } 1672 }
1637 } 1673 }
1638 1674
1639 if (SCHEME_V->gc_verbose) 1675 if (SCHEME_V->gc_verbose)
1676 {
1640 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1677 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1678 }
1641} 1679}
1642 1680
1643static void 1681static void
1644finalize_cell (SCHEME_P_ pointer a) 1682finalize_cell (SCHEME_P_ pointer a)
1645{ 1683{
2074#endif 2112#endif
2075} 2113}
2076 2114
2077/* read characters up to delimiter, but cater to character constants */ 2115/* read characters up to delimiter, but cater to character constants */
2078static char * 2116static char *
2079readstr_upto (SCHEME_P_ char *delim) 2117readstr_upto (SCHEME_P_ int skip, const char *delim)
2080{ 2118{
2081 char *p = SCHEME_V->strbuff; 2119 char *p = SCHEME_V->strbuff + skip;
2082 2120
2083 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))));
2084 2122
2085 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2123 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2086 *p = 0; 2124 *p = 0;
2093 return SCHEME_V->strbuff; 2131 return SCHEME_V->strbuff;
2094} 2132}
2095 2133
2096/* read string expression "xxx...xxx" */ 2134/* read string expression "xxx...xxx" */
2097static pointer 2135static pointer
2098readstrexp (SCHEME_P) 2136readstrexp (SCHEME_P_ char delim)
2099{ 2137{
2100 char *p = SCHEME_V->strbuff; 2138 char *p = SCHEME_V->strbuff;
2101 int c; 2139 int c;
2102 int c1 = 0; 2140 int c1 = 0;
2103 enum
2104 { 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;
2105 2142
2106 for (;;) 2143 for (;;)
2107 { 2144 {
2108 c = inchar (SCHEME_A); 2145 c = inchar (SCHEME_A);
2109 2146
2111 return S_F; 2148 return S_F;
2112 2149
2113 switch (state) 2150 switch (state)
2114 { 2151 {
2115 case st_ok: 2152 case st_ok:
2116 switch (c) 2153 if (ecb_expect_false (c == delim))
2117 {
2118 case '\\':
2119 state = st_bsl;
2120 break;
2121
2122 case '"':
2123 *p = 0;
2124 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);
2125 2155
2126 default: 2156 if (ecb_expect_false (c == '\\'))
2157 state = st_bsl;
2158 else
2127 *p++ = c; 2159 *p++ = c;
2128 break;
2129 }
2130 2160
2131 break; 2161 break;
2132 2162
2133 case st_bsl: 2163 case st_bsl:
2134 switch (c) 2164 switch (c)
2164 case 'r': 2194 case 'r':
2165 *p++ = '\r'; 2195 *p++ = '\r';
2166 state = st_ok; 2196 state = st_ok;
2167 break; 2197 break;
2168 2198
2169 case '"':
2170 *p++ = '"';
2171 state = st_ok;
2172 break;
2173
2174 default: 2199 default:
2175 *p++ = c; 2200 *p++ = c;
2176 state = st_ok; 2201 state = st_ok;
2177 break; 2202 break;
2178 } 2203 }
2179 2204
2180 break; 2205 break;
2181 2206
2182 case st_x1: 2207 case st_x1:
2183 case st_x2: 2208 case st_x2:
2184 c = toupper (c); 2209 c = tolower (c);
2185 2210
2186 if (c >= '0' && c <= 'F') 2211 if (c >= '0' && c <= '9')
2187 {
2188 if (c <= '9')
2189 c1 = (c1 << 4) + c - '0'; 2212 c1 = (c1 << 4) + c - '0';
2190 else 2213 else if (c >= 'a' && c <= 'f')
2191 c1 = (c1 << 4) + c - 'A' + 10; 2214 c1 = (c1 << 4) + c - 'a' + 10;
2192
2193 if (state == st_x1)
2194 state = st_x2;
2195 else
2196 {
2197 *p++ = c1;
2198 state = st_ok;
2199 }
2200 }
2201 else 2215 else
2202 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 }
2203 2225
2204 break; 2226 break;
2205 2227
2206 case st_oct1: 2228 case st_oct1:
2207 case st_oct2: 2229 case st_oct2:
2211 backchar (SCHEME_A_ c); 2233 backchar (SCHEME_A_ c);
2212 state = st_ok; 2234 state = st_ok;
2213 } 2235 }
2214 else 2236 else
2215 { 2237 {
2216 if (state == st_oct2 && c1 >= 32) 2238 if (state == st_oct2 && c1 >= ' ')
2217 return S_F; 2239 return S_F;
2218 2240
2219 c1 = (c1 << 3) + (c - '0'); 2241 c1 = (c1 << 3) + (c - '0');
2220 2242
2221 if (state == st_oct1) 2243 if (state == st_oct1)
2226 state = st_ok; 2248 state = st_ok;
2227 } 2249 }
2228 } 2250 }
2229 2251
2230 break; 2252 break;
2231
2232 } 2253 }
2233 } 2254 }
2234} 2255}
2235 2256
2236/* check c is in chars */ 2257/* check c is in chars */
2237ecb_inline int 2258ecb_inline int
2238is_one_of (char *s, int c) 2259is_one_of (const char *s, int c)
2239{ 2260{
2240 if (c == EOF) 2261 if (c == EOF)
2241 return 1; 2262 return 1;
2242 2263
2243 return !!strchr (s, c); 2264 return !!strchr (s, c);
2299 2320
2300 if (is_one_of (" \n\t", c)) 2321 if (is_one_of (" \n\t", c))
2301 return TOK_DOT; 2322 return TOK_DOT;
2302 else 2323 else
2303 { 2324 {
2304 //TODO: ungetc twice in a row is not supported in C
2305 backchar (SCHEME_A_ c); 2325 backchar (SCHEME_A_ c);
2306 backchar (SCHEME_A_ '.');
2307 return TOK_ATOM; 2326 return TOK_DOTATOM;
2308 } 2327 }
2328
2329 case '|':
2330 return TOK_STRATOM;
2309 2331
2310 case '\'': 2332 case '\'':
2311 return TOK_QUOTE; 2333 return TOK_QUOTE;
2312 2334
2313 case ';': 2335 case ';':
2480 { 2502 {
2481 p = SCHEME_V->strbuff; 2503 p = SCHEME_V->strbuff;
2482 2504
2483 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2505 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2484 { 2506 {
2485 if (num_is_integer (l)) 2507 if (is_integer (l))
2486 xnum (p, ivalue_unchecked (l)); 2508 xnum (p, ivalue_unchecked (l));
2487#if USE_REAL 2509#if USE_REAL
2488 else 2510 else
2489 { 2511 {
2490 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2512 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2745 return 0; 2767 return 0;
2746 } 2768 }
2747 else if (is_number (a)) 2769 else if (is_number (a))
2748 { 2770 {
2749 if (is_number (b)) 2771 if (is_number (b))
2750 if (num_is_integer (a) == num_is_integer (b))
2751 return num_cmp (nvalue (a), nvalue (b)) == 0; 2772 return num_cmp (nvalue (a), nvalue (b)) == 0;
2752 2773
2753 return 0; 2774 return 0;
2754 } 2775 }
2755 else if (is_character (a)) 2776 else if (is_character (a))
2756 { 2777 {
2782/* () is #t in R5RS */ 2803/* () is #t in R5RS */
2783#define is_true(p) ((p) != S_F) 2804#define is_true(p) ((p) != S_F)
2784#define is_false(p) ((p) == S_F) 2805#define is_false(p) ((p) == S_F)
2785 2806
2786/* ========== Environment implementation ========== */ 2807/* ========== Environment implementation ========== */
2787
2788#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST)
2789
2790static int
2791hash_fn (const char *key, int table_size)
2792{
2793 const unsigned char *p = key;
2794 uint32_t hash = 2166136261;
2795
2796 while (*p)
2797 hash = (hash ^ *p++) * 16777619;
2798
2799 return hash % table_size;
2800}
2801#endif
2802 2808
2803#ifndef USE_ALIST_ENV 2809#ifndef USE_ALIST_ENV
2804 2810
2805/* 2811/*
2806 * In this implementation, each frame of the environment may be 2812 * In this implementation, each frame of the environment may be
2823 2829
2824 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2830 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2825 setenvironment (SCHEME_V->envir); 2831 setenvironment (SCHEME_V->envir);
2826} 2832}
2827 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
2828ecb_inline void 2849ecb_inline void
2829new_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)
2830{ 2851{
2831 pointer slot = immutable_cons (variable, value); 2852 pointer slot = immutable_cons (variable, value);
2832 2853
2833 if (is_vector (car (env))) 2854 if (is_vector (car (env)))
2834 { 2855 {
2835 int location = hash_fn (symname (variable), veclength (car (env))); 2856 int location = sym_hash (variable, veclength (car (env)));
2836
2837 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)));
2838 } 2858 }
2839 else 2859 else
2840 set_car (env, immutable_cons (slot, car (env))); 2860 set_car (env, immutable_cons (slot, car (env)));
2841} 2861}
2842 2862
2843static pointer 2863static pointer
2844find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2864find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2845{ 2865{
2846 pointer x, y; 2866 pointer x, y;
2847 int location;
2848 2867
2849 for (x = env; x != NIL; x = cdr (x)) 2868 for (x = env; x != NIL; x = cdr (x))
2850 { 2869 {
2851 if (is_vector (car (x))) 2870 if (is_vector (car (x)))
2852 { 2871 {
2853 location = hash_fn (symname (hdl), veclength (car (x))); 2872 int location = sym_hash (hdl, veclength (car (x)));
2854 y = vector_elem (car (x), location); 2873 y = vector_get (car (x), location);
2855 } 2874 }
2856 else 2875 else
2857 y = car (x); 2876 y = car (x);
2858 2877
2859 for (; y != NIL; y = cdr (y)) 2878 for (; y != NIL; y = cdr (y))
2860 if (caar (y) == hdl) 2879 if (caar (y) == hdl)
2861 break; 2880 break;
2862 2881
2863 if (y != NIL) 2882 if (y != NIL)
2883 return car (y);
2884
2885 if (!all)
2864 break; 2886 break;
2865
2866 if (!all)
2867 return NIL;
2868 } 2887 }
2869
2870 if (x != NIL)
2871 return car (y);
2872 2888
2873 return NIL; 2889 return NIL;
2874} 2890}
2875 2891
2876#else /* USE_ALIST_ENV */ 2892#else /* USE_ALIST_ENV */
2898 for (y = car (x); y != NIL; y = cdr (y)) 2914 for (y = car (x); y != NIL; y = cdr (y))
2899 if (caar (y) == hdl) 2915 if (caar (y) == hdl)
2900 break; 2916 break;
2901 2917
2902 if (y != NIL) 2918 if (y != NIL)
2919 return car (y);
2903 break; 2920 break;
2904 2921
2905 if (!all) 2922 if (!all)
2906 return NIL; 2923 break;
2907 } 2924 }
2908
2909 if (x != NIL)
2910 return car (y);
2911 2925
2912 return NIL; 2926 return NIL;
2913} 2927}
2914 2928
2915#endif /* USE_ALIST_ENV else */ 2929#endif /* USE_ALIST_ENV else */
3132 int i = 0; 3146 int i = 0;
3133 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3147 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3134 3148
3135 while (cont != NIL) 3149 while (cont != NIL)
3136 { 3150 {
3137 frame->op = ivalue (car (cont)); cont = cdr (cont); 3151 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3138 frame->args = car (cont) ; cont = cdr (cont); 3152 frame->args = car (cont) ; cont = cdr (cont);
3139 frame->envir = car (cont) ; cont = cdr (cont); 3153 frame->envir = car (cont) ; cont = cdr (cont);
3140 frame->code = car (cont) ; cont = cdr (cont); 3154 frame->code = car (cont) ; cont = cdr (cont);
3141 3155
3142 ++frame; 3156 ++frame;
3143 ++i; 3157 ++i;
3144 } 3158 }
3145 3159
3174 SCHEME_V->value = a; 3188 SCHEME_V->value = a;
3175 3189
3176 if (dump == NIL) 3190 if (dump == NIL)
3177 return -1; 3191 return -1;
3178 3192
3179 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3193 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3180 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3194 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3181 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3195 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3182 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3196 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3183 3197
3184 SCHEME_V->dump = dump; 3198 SCHEME_V->dump = dump;
3185 3199
3186 return 0; 3200 return 0;
3187} 3201}
3393 3407
3394 case OP_TRACING: 3408 case OP_TRACING:
3395 { 3409 {
3396 int tr = SCHEME_V->tracing; 3410 int tr = SCHEME_V->tracing;
3397 3411
3398 SCHEME_V->tracing = ivalue (car (args)); 3412 SCHEME_V->tracing = ivalue_unchecked (car (args));
3399 s_return (mk_integer (SCHEME_A_ tr)); 3413 s_return (mk_integer (SCHEME_A_ tr));
3400 } 3414 }
3401 3415
3402#endif 3416#endif
3403 3417
3922{ 3936{
3923 pointer args = SCHEME_V->args; 3937 pointer args = SCHEME_V->args;
3924 pointer x = car (args); 3938 pointer x = car (args);
3925 num v; 3939 num v;
3926 3940
3927#if USE_MATH
3928 RVALUE dd;
3929#endif
3930
3931 switch (op) 3941 switch (op)
3932 { 3942 {
3933#if USE_MATH 3943#if USE_MATH
3934 case OP_INEX2EX: /* inexact->exact */ 3944 case OP_INEX2EX: /* inexact->exact */
3945 {
3935 if (num_is_integer (x)) 3946 if (is_integer (x))
3936 s_return (x); 3947 s_return (x);
3937 else if (modf (rvalue_unchecked (x), &dd) == 0) 3948
3949 RVALUE r = rvalue_unchecked (x);
3950
3951 if (r == (RVALUE)(IVALUE)r)
3938 s_return (mk_integer (SCHEME_A_ ivalue (x))); 3952 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3939 else 3953 else
3940 Error_1 ("inexact->exact: not integral:", x); 3954 Error_1 ("inexact->exact: not integral:", x);
3955 }
3941 3956
3942 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))));
3943 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))));
3944 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))));
3945 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))));
3963 { 3978 {
3964 RVALUE result; 3979 RVALUE result;
3965 int real_result = 1; 3980 int real_result = 1;
3966 pointer y = cadr (args); 3981 pointer y = cadr (args);
3967 3982
3968 if (num_is_integer (x) && num_is_integer (y)) 3983 if (is_integer (x) && is_integer (y))
3969 real_result = 0; 3984 real_result = 0;
3970 3985
3971 /* This 'if' is an R5RS compatibility fix. */ 3986 /* This 'if' is an R5RS compatibility fix. */
3972 /* NOTE: Remove this 'if' fix for R6RS. */ 3987 /* NOTE: Remove this 'if' fix for R6RS. */
3973 if (rvalue (x) == 0 && rvalue (y) < 0) 3988 if (rvalue (x) == 0 && rvalue (y) < 0)
3979 /* If the test fails, result is too big for integer. */ 3994 /* If the test fails, result is too big for integer. */
3980 if (!real_result) 3995 if (!real_result)
3981 { 3996 {
3982 long result_as_long = result; 3997 long result_as_long = result;
3983 3998
3984 if (result != (RVALUE) result_as_long) 3999 if (result != result_as_long)
3985 real_result = 1; 4000 real_result = 1;
3986 } 4001 }
3987 4002
3988 if (real_result) 4003 if (real_result)
3989 s_return (mk_real (SCHEME_A_ result)); 4004 s_return (mk_real (SCHEME_A_ result));
3994 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))));
3995 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))));
3996 4011
3997 case OP_TRUNCATE: 4012 case OP_TRUNCATE:
3998 { 4013 {
3999 RVALUE rvalue_of_x; 4014 RVALUE n = rvalue (x);
4000
4001 rvalue_of_x = rvalue (x);
4002
4003 if (rvalue_of_x > 0)
4004 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); 4015 s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n)));
4005 else
4006 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
4007 } 4016 }
4008 4017
4009 case OP_ROUND: 4018 case OP_ROUND:
4010 if (num_is_integer (x)) 4019 if (is_integer (x))
4011 s_return (x); 4020 s_return (x);
4012 4021
4013 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4022 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4014#endif 4023#endif
4015 4024
4135 } 4144 }
4136 else 4145 else
4137 Error_0 ("set-cdr!: unable to alter immutable pair"); 4146 Error_0 ("set-cdr!: unable to alter immutable pair");
4138 4147
4139 case OP_CHAR2INT: /* char->integer */ 4148 case OP_CHAR2INT: /* char->integer */
4140 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4149 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4141 4150
4142 case OP_INT2CHAR: /* integer->char */ 4151 case OP_INT2CHAR: /* integer->char */
4143 s_return (mk_character (SCHEME_A_ ivalue (x))); 4152 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4144 4153
4145 case OP_CHARUPCASE: 4154 case OP_CHARUPCASE:
4146 { 4155 {
4147 unsigned char c = ivalue (x); 4156 unsigned char c = ivalue_unchecked (x);
4148 c = toupper (c); 4157 c = toupper (c);
4149 s_return (mk_character (SCHEME_A_ c)); 4158 s_return (mk_character (SCHEME_A_ c));
4150 } 4159 }
4151 4160
4152 case OP_CHARDNCASE: 4161 case OP_CHARDNCASE:
4153 { 4162 {
4154 unsigned char c = ivalue (x); 4163 unsigned char c = ivalue_unchecked (x);
4155 c = tolower (c); 4164 c = tolower (c);
4156 s_return (mk_character (SCHEME_A_ c)); 4165 s_return (mk_character (SCHEME_A_ c));
4157 } 4166 }
4158 4167
4159 case OP_STR2SYM: /* string->symbol */ 4168 case OP_STR2SYM: /* string->symbol */
4236 Error_1 ("atom->string: not an atom:", x); 4245 Error_1 ("atom->string: not an atom:", x);
4237 } 4246 }
4238 4247
4239 case OP_MKSTRING: /* make-string */ 4248 case OP_MKSTRING: /* make-string */
4240 { 4249 {
4241 int fill = ' '; 4250 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4242 int len;
4243
4244 len = ivalue (x); 4251 int len = ivalue_unchecked (x);
4245
4246 if (cdr (args) != NIL)
4247 fill = charvalue (cadr (args));
4248 4252
4249 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4253 s_return (mk_empty_string (SCHEME_A_ len, fill));
4250 } 4254 }
4251 4255
4252 case OP_STRLEN: /* string-length */ 4256 case OP_STRLEN: /* string-length */
4253 s_return (mk_integer (SCHEME_A_ strlength (x))); 4257 s_return (mk_integer (SCHEME_A_ strlength (x)));
4254 4258
4255 case OP_STRREF: /* string-ref */ 4259 case OP_STRREF: /* string-ref */
4256 { 4260 {
4257 char *str;
4258 int index;
4259
4260 str = strvalue (x); 4261 char *str = strvalue (x);
4261
4262 index = ivalue (cadr (args)); 4262 int index = ivalue_unchecked (cadr (args));
4263 4263
4264 if (index >= strlength (x)) 4264 if (index >= strlength (x))
4265 Error_1 ("string-ref: out of bounds:", cadr (args)); 4265 Error_1 ("string-ref: out of bounds:", cadr (args));
4266 4266
4267 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4267 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4268 } 4268 }
4269 4269
4270 case OP_STRSET: /* string-set! */ 4270 case OP_STRSET: /* string-set! */
4271 { 4271 {
4272 char *str; 4272 char *str = strvalue (x);
4273 int index; 4273 int index = ivalue_unchecked (cadr (args));
4274 int c; 4274 int c;
4275 4275
4276 if (is_immutable (x)) 4276 if (is_immutable (x))
4277 Error_1 ("string-set!: unable to alter immutable string:", x); 4277 Error_1 ("string-set!: unable to alter immutable string:", x);
4278
4279 str = strvalue (x);
4280
4281 index = ivalue (cadr (args));
4282 4278
4283 if (index >= strlength (x)) 4279 if (index >= strlength (x))
4284 Error_1 ("string-set!: out of bounds:", cadr (args)); 4280 Error_1 ("string-set!: out of bounds:", cadr (args));
4285 4281
4286 c = charvalue (caddr (args)); 4282 c = charvalue (caddr (args));
4309 s_return (newstr); 4305 s_return (newstr);
4310 } 4306 }
4311 4307
4312 case OP_SUBSTR: /* substring */ 4308 case OP_SUBSTR: /* substring */
4313 { 4309 {
4314 char *str; 4310 char *str = strvalue (x);
4315 int index0; 4311 int index0 = ivalue_unchecked (cadr (args));
4316 int index1; 4312 int index1;
4317 int len; 4313 int len;
4318 4314
4319 str = strvalue (x);
4320
4321 index0 = ivalue (cadr (args));
4322
4323 if (index0 > strlength (x)) 4315 if (index0 > strlength (x))
4324 Error_1 ("substring: start out of bounds:", cadr (args)); 4316 Error_1 ("substring: start out of bounds:", cadr (args));
4325 4317
4326 if (cddr (args) != NIL) 4318 if (cddr (args) != NIL)
4327 { 4319 {
4328 index1 = ivalue (caddr (args)); 4320 index1 = ivalue_unchecked (caddr (args));
4329 4321
4330 if (index1 > strlength (x) || index1 < index0) 4322 if (index1 > strlength (x) || index1 < index0)
4331 Error_1 ("substring: end out of bounds:", caddr (args)); 4323 Error_1 ("substring: end out of bounds:", caddr (args));
4332 } 4324 }
4333 else 4325 else
4356 if (SCHEME_V->no_memory) 4348 if (SCHEME_V->no_memory)
4357 s_return (S_SINK); 4349 s_return (S_SINK);
4358#endif 4350#endif
4359 4351
4360 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++)
4361 set_vector_elem (vec, i, car (x)); 4353 vector_set (vec, i, car (x));
4362 4354
4363 s_return (vec); 4355 s_return (vec);
4364 } 4356 }
4365 4357
4366 case OP_MKVECTOR: /* make-vector */ 4358 case OP_MKVECTOR: /* make-vector */
4367 { 4359 {
4368 pointer fill = NIL; 4360 pointer fill = NIL;
4369 int len;
4370 pointer vec; 4361 pointer vec;
4371
4372 len = ivalue (x); 4362 int len = ivalue_unchecked (x);
4373 4363
4374 if (cdr (args) != NIL) 4364 if (cdr (args) != NIL)
4375 fill = cadr (args); 4365 fill = cadr (args);
4376 4366
4377 vec = mk_vector (SCHEME_A_ len); 4367 vec = mk_vector (SCHEME_A_ len);
4380 if (SCHEME_V->no_memory) 4370 if (SCHEME_V->no_memory)
4381 s_return (S_SINK); 4371 s_return (S_SINK);
4382#endif 4372#endif
4383 4373
4384 if (fill != NIL) 4374 if (fill != NIL)
4385 fill_vector (vec, fill); 4375 fill_vector (vec, 0, fill);
4386 4376
4387 s_return (vec); 4377 s_return (vec);
4388 } 4378 }
4389 4379
4390 case OP_VECLEN: /* vector-length */ 4380 case OP_VECLEN: /* vector-length */
4391 s_return (mk_integer (SCHEME_A_ veclength (x))); 4381 s_return (mk_integer (SCHEME_A_ veclength (x)));
4392 4382
4393 case OP_VECREF: /* vector-ref */ 4383 case OP_VECREF: /* vector-ref */
4394 { 4384 {
4395 int index;
4396
4397 index = ivalue (cadr (args)); 4385 int index = ivalue_unchecked (cadr (args));
4398 4386
4399 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4387 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4400 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4388 Error_1 ("vector-ref: out of bounds:", cadr (args));
4401 4389
4402 s_return (vector_elem (x, index)); 4390 s_return (vector_get (x, index));
4403 } 4391 }
4404 4392
4405 case OP_VECSET: /* vector-set! */ 4393 case OP_VECSET: /* vector-set! */
4406 { 4394 {
4407 int index; 4395 int index = ivalue_unchecked (cadr (args));
4408 4396
4409 if (is_immutable (x)) 4397 if (is_immutable (x))
4410 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4398 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4411 4399
4412 index = ivalue (cadr (args));
4413
4414 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4400 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4415 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4401 Error_1 ("vector-set!: out of bounds:", cadr (args));
4416 4402
4417 set_vector_elem (x, index, caddr (args)); 4403 vector_set (x, index, caddr (args));
4418 s_return (x); 4404 s_return (x);
4419 } 4405 }
4420 } 4406 }
4421 4407
4422 if (USE_ERROR_CHECKING) abort (); 4408 if (USE_ERROR_CHECKING) abort ();
4423}
4424
4425INTERFACE int
4426is_list (SCHEME_P_ pointer a)
4427{
4428 return list_length (SCHEME_A_ a) >= 0;
4429}
4430
4431/* Result is:
4432 proper list: length
4433 circular list: -1
4434 not even a pair: -2
4435 dotted list: -2 minus length before dot
4436*/
4437INTERFACE int
4438list_length (SCHEME_P_ pointer a)
4439{
4440 int i = 0;
4441 pointer slow, fast;
4442
4443 slow = fast = a;
4444
4445 while (1)
4446 {
4447 if (fast == NIL)
4448 return i;
4449
4450 if (!is_pair (fast))
4451 return -2 - i;
4452
4453 fast = cdr (fast);
4454 ++i;
4455
4456 if (fast == NIL)
4457 return i;
4458
4459 if (!is_pair (fast))
4460 return -2 - i;
4461
4462 ++i;
4463 fast = cdr (fast);
4464
4465 /* Safe because we would have already returned if `fast'
4466 encountered a non-pair. */
4467 slow = cdr (slow);
4468
4469 if (fast == slow)
4470 {
4471 /* the fast pointer has looped back around and caught up
4472 with the slow pointer, hence the structure is circular,
4473 not of finite length, and therefore not a list */
4474 return -1;
4475 }
4476 }
4477} 4409}
4478 4410
4479static int 4411static int
4480opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4412opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4481{ 4413{
4527 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4459 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4528 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 */
4529 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4461 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4530 4462
4531#if USE_CHAR_CLASSIFIERS 4463#if USE_CHAR_CLASSIFIERS
4532 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4464 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4533 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4465 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4534 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4466 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4535 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4467 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4536 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4468 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4537#endif 4469#endif
4538 4470
4539#if USE_PORTS 4471#if USE_PORTS
4540 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4472 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4541 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4473 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4742 4674
4743 case OP_NEWSEGMENT: /* new-segment */ 4675 case OP_NEWSEGMENT: /* new-segment */
4744 if (!is_pair (args) || !is_number (a)) 4676 if (!is_pair (args) || !is_number (a))
4745 Error_0 ("new-segment: argument must be a number"); 4677 Error_0 ("new-segment: argument must be a number");
4746 4678
4747 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 4679 alloc_cellseg (SCHEME_A_ ivalue (a));
4748 4680
4749 s_return (S_T); 4681 s_return (S_T);
4750 4682
4751 case OP_OBLIST: /* oblist */ 4683 case OP_OBLIST: /* oblist */
4752 s_return (oblist_all_symbols (SCHEME_A)); 4684 s_return (oblist_all_symbols (SCHEME_A));
5010 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 4942 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
5011 SCHEME_V->tok = token (SCHEME_A); 4943 SCHEME_V->tok = token (SCHEME_A);
5012 s_goto (OP_RDSEXPR); 4944 s_goto (OP_RDSEXPR);
5013 4945
5014 case TOK_ATOM: 4946 case TOK_ATOM:
5015 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)));
5016 4952
5017 case TOK_DQUOTE: 4953 case TOK_DQUOTE:
5018 x = readstrexp (SCHEME_A); 4954 x = readstrexp (SCHEME_A_ '"');
5019 4955
5020 if (x == S_F) 4956 if (x == S_F)
5021 Error_0 ("Error reading string"); 4957 Error_0 ("Error reading string");
5022 4958
5023 setimmutable (x); 4959 setimmutable (x);
5035 s_goto (OP_EVAL); 4971 s_goto (OP_EVAL);
5036 } 4972 }
5037 } 4973 }
5038 4974
5039 case TOK_SHARP_CONST: 4975 case TOK_SHARP_CONST:
5040 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)
5041 Error_0 ("undefined sharp expression"); 4977 Error_0 ("undefined sharp expression");
5042 else 4978 else
5043 s_return (x); 4979 s_return (x);
5044 4980
5045 default: 4981 default:
5197 putstr (SCHEME_A_ ")"); 5133 putstr (SCHEME_A_ ")");
5198 s_return (S_T); 5134 s_return (S_T);
5199 } 5135 }
5200 else 5136 else
5201 { 5137 {
5202 pointer elem = vector_elem (vec, i); 5138 pointer elem = vector_get (vec, i);
5203 5139
5204 ivalue_unchecked (cdr (args)) = i + 1; 5140 ivalue_unchecked (cdr (args)) = i + 1;
5205 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5141 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5206 SCHEME_V->args = elem; 5142 SCHEME_V->args = elem;
5207 5143
5282/* 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 */
5283typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); 5219typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5284 5220
5285typedef int (*test_predicate)(pointer); 5221typedef int (*test_predicate)(pointer);
5286static int 5222static int
5287is_any (pointer p) 5223tst_any (pointer p)
5288{ 5224{
5289 return 1; 5225 return 1;
5290} 5226}
5291 5227
5292static int 5228static int
5293is_nonneg (pointer p) 5229tst_inonneg (pointer p)
5294{ 5230{
5295 return ivalue (p) >= 0 && is_integer (p); 5231 return is_integer (p) && ivalue_unchecked (p) >= 0;
5296} 5232}
5297 5233
5298static int 5234static int
5299tst_is_list (pointer p) 5235tst_is_list (SCHEME_P_ pointer p)
5300{ 5236{
5301 return p == NIL || is_pair (p); 5237 return p == NIL || is_pair (p);
5302} 5238}
5303 5239
5304/* Correspond carefully with following defines! */ 5240/* Correspond carefully with following defines! */
5305static struct 5241static struct
5306{ 5242{
5307 test_predicate fct; 5243 test_predicate fct;
5308 const char *kind; 5244 const char *kind;
5309} tests[] = 5245} tests[] = {
5310{
5311 { is_any, 0 }, 5246 { tst_any , 0 },
5312 { is_string, "string" }, 5247 { is_string , "string" },
5313 { is_symbol, "symbol" }, 5248 { is_symbol , "symbol" },
5314 { is_port, "port" }, 5249 { is_port , "port" },
5315 { is_inport, "input port" }, 5250 { is_inport , "input port" },
5316 { is_outport, "output port" }, 5251 { is_outport , "output port" },
5317 { is_environment, "environment" }, 5252 { is_environment, "environment" },
5318 { is_pair, "pair" }, 5253 { is_pair , "pair" },
5319 { tst_is_list, "pair or '()" }, 5254 { 0 , "pair or '()" },
5320 { is_character, "character" }, 5255 { is_character , "character" },
5321 { is_vector, "vector" }, 5256 { is_vector , "vector" },
5322 { is_number, "number" }, 5257 { is_number , "number" },
5323 { is_integer, "integer" }, 5258 { is_integer , "integer" },
5324 { is_nonneg, "non-negative integer" } 5259 { tst_inonneg , "non-negative integer" }
5325}; 5260};
5326 5261
5327#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 */
5328#define TST_ANY "\001" 5263#define TST_ANY "\001"
5329#define TST_STRING "\002" 5264#define TST_STRING "\002"
5370typedef struct 5305typedef struct
5371{ 5306{
5372 uint8_t func; 5307 uint8_t func;
5373 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ 5308 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5374 uint8_t builtin; 5309 uint8_t builtin;
5310#if USE_ERROR_CHECKING
5375 uint8_t min_arity; 5311 uint8_t min_arity;
5376 uint8_t max_arity; 5312 uint8_t max_arity;
5377 char arg_tests_encoding[3]; 5313 char arg_tests_encoding[3];
5314#endif
5378} op_code_info; 5315} op_code_info;
5379 5316
5380static const op_code_info dispatch_table[] = { 5317static const op_code_info dispatch_table[] = {
5318#if USE_ERROR_CHECKING
5381#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
5382#include "opdefines.h" 5323#include "opdefines.h"
5383#undef OP_DEF 5324#undef OP_DEF
5384 {0} 5325 {0}
5385}; 5326};
5386 5327
5428 { 5369 {
5429 pointer arg = car (arglist); 5370 pointer arg = car (arglist);
5430 5371
5431 j = t[0]; 5372 j = t[0];
5432 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 {
5433 if (!tests[j - 1].fct (arg)) 5382 if (!tests[j - 1].fct (arg))
5434 break; 5383 break;
5384 }
5435 5385
5436 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 */
5437 t++; 5387 t++;
5438 5388
5439 arglist = cdr (arglist); 5389 arglist = cdr (arglist);
5440 i++; 5390 i++;
5441 } 5391 }
5496mk_proc (SCHEME_P_ enum scheme_opcodes op) 5446mk_proc (SCHEME_P_ enum scheme_opcodes op)
5497{ 5447{
5498 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5448 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5499 set_typeflag (y, (T_PROC | T_ATOM)); 5449 set_typeflag (y, (T_PROC | T_ATOM));
5500 ivalue_unchecked (y) = op; 5450 ivalue_unchecked (y) = op;
5501 set_num_integer (y);
5502 return y; 5451 return y;
5503} 5452}
5504 5453
5505/* 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! */
5506static int 5455static int

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines