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.38 by root, Sun Nov 29 13:47:15 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{
415} 406}
416 407
417INTERFACE char * 408INTERFACE char *
418symname (pointer p) 409symname (pointer p)
419{ 410{
420 return strvalue (car (p)); 411 return strvalue (p);
421} 412}
422 413
423#if USE_PLIST 414#if USE_PLIST
424SCHEME_EXPORT int 415SCHEME_EXPORT int
425hasprop (pointer p) 416hasprop (pointer p)
449} 440}
450 441
451INTERFACE char * 442INTERFACE char *
452syntaxname (pointer p) 443syntaxname (pointer p)
453{ 444{
454 return strvalue (car (p)); 445 return strvalue (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 set_typeflag (x, T_SYMBOL | T_ATOM);
1105 return x;
1106}
1107
1061#ifndef USE_OBJECT_LIST 1108#ifndef USE_OBJECT_LIST
1062 1109
1110static int
1063static int hash_fn (const char *key, int table_size); 1111hash_fn (const char *key, int table_size)
1112{
1113 const unsigned char *p = key;
1114 uint32_t hash = 2166136261;
1115
1116 while (*p)
1117 hash = (hash ^ *p++) * 16777619;
1118
1119 return hash % table_size;
1120}
1064 1121
1065static pointer 1122static pointer
1066oblist_initial_value (SCHEME_P) 1123oblist_initial_value (SCHEME_P)
1067{ 1124{
1068 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1125 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1070 1127
1071/* returns the new symbol */ 1128/* returns the new symbol */
1072static pointer 1129static pointer
1073oblist_add_by_name (SCHEME_P_ const char *name) 1130oblist_add_by_name (SCHEME_P_ const char *name)
1074{ 1131{
1075 int location; 1132 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)); 1133 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))); 1134 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1083 return x; 1135 return x;
1084} 1136}
1085 1137
1086ecb_inline pointer 1138ecb_inline pointer
1087oblist_find_by_name (SCHEME_P_ const char *name) 1139oblist_find_by_name (SCHEME_P_ const char *name)
1090 pointer x; 1142 pointer x;
1091 char *s; 1143 char *s;
1092 1144
1093 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1145 location = hash_fn (name, veclength (SCHEME_V->oblist));
1094 1146
1095 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1147 for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1096 { 1148 {
1097 s = symname (car (x)); 1149 s = symname (car (x));
1098 1150
1099 /* case-insensitive, per R5RS section 2 */ 1151 /* case-insensitive, per R5RS section 2 */
1100 if (stricmp (name, s) == 0) 1152 if (stricmp (name, s) == 0)
1110 int i; 1162 int i;
1111 pointer x; 1163 pointer x;
1112 pointer ob_list = NIL; 1164 pointer ob_list = NIL;
1113 1165
1114 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1166 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1115 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1167 for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1116 ob_list = cons (x, ob_list); 1168 ob_list = cons (x, ob_list);
1117 1169
1118 return ob_list; 1170 return ob_list;
1119} 1171}
1120 1172
1146 1198
1147/* returns the new symbol */ 1199/* returns the new symbol */
1148static pointer 1200static pointer
1149oblist_add_by_name (SCHEME_P_ const char *name) 1201oblist_add_by_name (SCHEME_P_ const char *name)
1150{ 1202{
1151 pointer x; 1203 pointer x = mk_string (SCHEME_A_ name);
1152
1153 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1154 set_typeflag (x, T_SYMBOL); 1204 set_typeflag (x, T_SYMBOL);
1155 setimmutable (car (x)); 1205 setimmutable (x);
1156 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1206 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1157 return x; 1207 return x;
1158} 1208}
1159 1209
1160static pointer 1210static pointer
1193mk_character (SCHEME_P_ int c) 1243mk_character (SCHEME_P_ int c)
1194{ 1244{
1195 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1245 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1196 1246
1197 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1247 set_typeflag (x, (T_CHARACTER | T_ATOM));
1198 ivalue_unchecked (x) = c & 0xff; 1248 set_ivalue (x, c & 0xff);
1199 set_num_integer (x); 1249
1200 return x; 1250 return x;
1201} 1251}
1202 1252
1203/* get number atom (integer) */ 1253/* get number atom (integer) */
1204INTERFACE pointer 1254INTERFACE pointer
1205mk_integer (SCHEME_P_ long num) 1255mk_integer (SCHEME_P_ long n)
1206{ 1256{
1207 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1257 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1208 1258
1209 set_typeflag (x, (T_NUMBER | T_ATOM)); 1259 set_typeflag (x, (T_INTEGER | T_ATOM));
1210 ivalue_unchecked (x) = num; 1260 set_ivalue (x, n);
1211 set_num_integer (x); 1261
1212 return x; 1262 return x;
1213} 1263}
1214 1264
1215INTERFACE pointer 1265INTERFACE pointer
1216mk_real (SCHEME_P_ RVALUE n) 1266mk_real (SCHEME_P_ RVALUE n)
1217{ 1267{
1268#if USE_REAL
1218 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1269 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1219 1270
1220 set_typeflag (x, (T_NUMBER | T_ATOM)); 1271 set_typeflag (x, (T_REAL | T_ATOM));
1221 rvalue_unchecked (x) = n; 1272 set_rvalue (x, n);
1222 set_num_real (x); 1273
1223 return x; 1274 return x;
1275#else
1276 return mk_integer (SCHEME_A_ n);
1277#endif
1224} 1278}
1225 1279
1226static pointer 1280static pointer
1227mk_number (SCHEME_P_ const num n) 1281mk_number (SCHEME_P_ const num n)
1228{ 1282{
1283#if USE_REAL
1229 if (num_is_fixnum (n)) 1284 return num_is_fixnum (n)
1285 ? mk_integer (SCHEME_A_ num_ivalue (n))
1286 : mk_real (SCHEME_A_ num_rvalue (n));
1287#else
1230 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1288 return mk_integer (SCHEME_A_ num_ivalue (n));
1231 else 1289#endif
1232 return mk_real (SCHEME_A_ num_get_rvalue (n));
1233} 1290}
1234 1291
1235/* allocate name to string area */ 1292/* allocate name to string area */
1236static char * 1293static char *
1237store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) 1294store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1243 SCHEME_V->no_memory = 1; 1300 SCHEME_V->no_memory = 1;
1244 return SCHEME_V->strbuff; 1301 return SCHEME_V->strbuff;
1245 } 1302 }
1246 1303
1247 if (str) 1304 if (str)
1248 { 1305 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 1306 else
1258 {
1259 memset (q, fill, len_str); 1307 memset (q, fill, len_str);
1308
1260 q[len_str] = 0; 1309 q[len_str] = 0;
1261 }
1262 1310
1263 return q; 1311 return q;
1264} 1312}
1265 1313
1266INTERFACE pointer 1314INTERFACE pointer
1280 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1328 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1281 1329
1282 set_typeflag (x, T_STRING | T_ATOM); 1330 set_typeflag (x, T_STRING | T_ATOM);
1283 strvalue (x) = store_string (SCHEME_A_ len, str, 0); 1331 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1284 strlength (x) = len; 1332 strlength (x) = len;
1333
1285 return x; 1334 return x;
1286} 1335}
1287 1336
1288INTERFACE pointer 1337INTERFACE pointer
1289mk_string (SCHEME_P_ const char *str) 1338mk_string (SCHEME_P_ const char *str)
1296{ 1345{
1297 return get_vector_object (SCHEME_A_ len, NIL); 1346 return get_vector_object (SCHEME_A_ len, NIL);
1298} 1347}
1299 1348
1300INTERFACE void 1349INTERFACE void
1301fill_vector (pointer vec, pointer obj) 1350fill_vector (pointer vec, uint32_t start, pointer obj)
1302{ 1351{
1303 int i; 1352 int i;
1304 1353
1305 for (i = 0; i < vec->object.vector.length; i++) 1354 for (i = start; i < veclength (vec); i++)
1306 vecvalue (vec)[i] = obj; 1355 vecvalue (vec)[i] = obj;
1307} 1356}
1308 1357
1358INTERFACE void
1359vector_resize (pointer vec, uint32_t newsize, pointer fill)
1360{
1361 uint32_t oldsize = veclength (vec);
1362 vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer));
1363 veclength (vec) = newsize;
1364 fill_vector (vec, oldsize, fill);
1365}
1366
1309INTERFACE pointer 1367INTERFACE pointer
1310vector_elem (pointer vec, uint32_t ielem) 1368vector_get (pointer vec, uint32_t ielem)
1311{ 1369{
1312 return vecvalue(vec)[ielem]; 1370 return vecvalue(vec)[ielem];
1313} 1371}
1314 1372
1315INTERFACE void 1373INTERFACE void
1316set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1374vector_set (pointer vec, uint32_t ielem, pointer a)
1317{ 1375{
1318 vecvalue(vec)[ielem] = a; 1376 vecvalue(vec)[ielem] = a;
1319} 1377}
1320 1378
1321/* get new symbol */ 1379/* get new symbol */
1333 1391
1334INTERFACE pointer 1392INTERFACE pointer
1335gensym (SCHEME_P) 1393gensym (SCHEME_P)
1336{ 1394{
1337 pointer x; 1395 pointer x;
1338
1339 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1340 {
1341 char name[40] = "gensym-"; 1396 char name[40] = "gensym-";
1342 xnum (name + 7, SCHEME_V->gensym_cnt); 1397 xnum (name + 7, SCHEME_V->gensym_cnt);
1343 1398
1344 /* first check oblist */ 1399 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} 1400}
1356 1401
1357/* make symbol or number atom from string */ 1402/* make symbol or number atom from string */
1358static pointer 1403static pointer
1359mk_atom (SCHEME_P_ char *q) 1404mk_atom (SCHEME_P_ char *q)
1413 } 1458 }
1414 else if ((c == 'e') || (c == 'E')) 1459 else if ((c == 'e') || (c == 'E'))
1415 { 1460 {
1416 if (!has_fp_exp) 1461 if (!has_fp_exp)
1417 { 1462 {
1418 has_dec_point = 1; /* decimal point illegal 1463 has_dec_point = 1; /* decimal point illegal from now on */
1419 from now on */
1420 p++; 1464 p++;
1421 1465
1422 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1466 if ((*p == '-') || (*p == '+') || isdigit (*p))
1423 continue; 1467 continue;
1424 } 1468 }
1512 1556
1513 if (ecb_expect_false (is_vector (p))) 1557 if (ecb_expect_false (is_vector (p)))
1514 { 1558 {
1515 int i; 1559 int i;
1516 1560
1517 for (i = 0; i < p->object.vector.length; i++) 1561 for (i = 0; i < veclength (p); i++)
1518 mark (vecvalue (p)[i]); 1562 mark (vecvalue (p)[i]);
1519 } 1563 }
1520 1564
1521 if (is_atom (p)) 1565 if (is_atom (p))
1522 goto E6; 1566 goto E6;
1620 if (is_mark (p)) 1664 if (is_mark (p))
1621 clrmark (p); 1665 clrmark (p);
1622 else 1666 else
1623 { 1667 {
1624 /* reclaim cell */ 1668 /* reclaim cell */
1625 if (typeflag (p) != T_FREE) 1669 if (typeflag (p) != T_PAIR)
1626 { 1670 {
1627 finalize_cell (SCHEME_A_ p); 1671 finalize_cell (SCHEME_A_ p);
1628 set_typeflag (p, T_FREE); 1672 set_typeflag (p, T_PAIR);
1629 set_car (p, NIL); 1673 set_car (p, NIL);
1630 } 1674 }
1631 1675
1632 ++SCHEME_V->fcells; 1676 ++SCHEME_V->fcells;
1633 set_cdr (p, SCHEME_V->free_cell); 1677 set_cdr (p, SCHEME_V->free_cell);
1635 } 1679 }
1636 } 1680 }
1637 } 1681 }
1638 1682
1639 if (SCHEME_V->gc_verbose) 1683 if (SCHEME_V->gc_verbose)
1684 {
1640 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1685 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1686 }
1641} 1687}
1642 1688
1643static void 1689static void
1644finalize_cell (SCHEME_P_ pointer a) 1690finalize_cell (SCHEME_P_ pointer a)
1645{ 1691{
2074#endif 2120#endif
2075} 2121}
2076 2122
2077/* read characters up to delimiter, but cater to character constants */ 2123/* read characters up to delimiter, but cater to character constants */
2078static char * 2124static char *
2079readstr_upto (SCHEME_P_ char *delim) 2125readstr_upto (SCHEME_P_ int skip, const char *delim)
2080{ 2126{
2081 char *p = SCHEME_V->strbuff; 2127 char *p = SCHEME_V->strbuff + skip;
2082 2128
2083 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2129 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2084 2130
2085 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2131 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2086 *p = 0; 2132 *p = 0;
2093 return SCHEME_V->strbuff; 2139 return SCHEME_V->strbuff;
2094} 2140}
2095 2141
2096/* read string expression "xxx...xxx" */ 2142/* read string expression "xxx...xxx" */
2097static pointer 2143static pointer
2098readstrexp (SCHEME_P) 2144readstrexp (SCHEME_P_ char delim)
2099{ 2145{
2100 char *p = SCHEME_V->strbuff; 2146 char *p = SCHEME_V->strbuff;
2101 int c; 2147 int c;
2102 int c1 = 0; 2148 int c1 = 0;
2103 enum
2104 { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; 2149 enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok;
2105 2150
2106 for (;;) 2151 for (;;)
2107 { 2152 {
2108 c = inchar (SCHEME_A); 2153 c = inchar (SCHEME_A);
2109 2154
2111 return S_F; 2156 return S_F;
2112 2157
2113 switch (state) 2158 switch (state)
2114 { 2159 {
2115 case st_ok: 2160 case st_ok:
2116 switch (c) 2161 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); 2162 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff);
2125 2163
2126 default: 2164 if (ecb_expect_false (c == '\\'))
2165 state = st_bsl;
2166 else
2127 *p++ = c; 2167 *p++ = c;
2128 break;
2129 }
2130 2168
2131 break; 2169 break;
2132 2170
2133 case st_bsl: 2171 case st_bsl:
2134 switch (c) 2172 switch (c)
2164 case 'r': 2202 case 'r':
2165 *p++ = '\r'; 2203 *p++ = '\r';
2166 state = st_ok; 2204 state = st_ok;
2167 break; 2205 break;
2168 2206
2169 case '"':
2170 *p++ = '"';
2171 state = st_ok;
2172 break;
2173
2174 default: 2207 default:
2175 *p++ = c; 2208 *p++ = c;
2176 state = st_ok; 2209 state = st_ok;
2177 break; 2210 break;
2178 } 2211 }
2179 2212
2180 break; 2213 break;
2181 2214
2182 case st_x1: 2215 case st_x1:
2183 case st_x2: 2216 case st_x2:
2184 c = toupper (c); 2217 c = tolower (c);
2185 2218
2186 if (c >= '0' && c <= 'F') 2219 if (c >= '0' && c <= '9')
2187 {
2188 if (c <= '9')
2189 c1 = (c1 << 4) + c - '0'; 2220 c1 = (c1 << 4) + c - '0';
2190 else 2221 else if (c >= 'a' && c <= 'f')
2191 c1 = (c1 << 4) + c - 'A' + 10; 2222 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 2223 else
2202 return S_F; 2224 return S_F;
2225
2226 if (state == st_x1)
2227 state = st_x2;
2228 else
2229 {
2230 *p++ = c1;
2231 state = st_ok;
2232 }
2203 2233
2204 break; 2234 break;
2205 2235
2206 case st_oct1: 2236 case st_oct1:
2207 case st_oct2: 2237 case st_oct2:
2211 backchar (SCHEME_A_ c); 2241 backchar (SCHEME_A_ c);
2212 state = st_ok; 2242 state = st_ok;
2213 } 2243 }
2214 else 2244 else
2215 { 2245 {
2216 if (state == st_oct2 && c1 >= 32) 2246 if (state == st_oct2 && c1 >= ' ')
2217 return S_F; 2247 return S_F;
2218 2248
2219 c1 = (c1 << 3) + (c - '0'); 2249 c1 = (c1 << 3) + (c - '0');
2220 2250
2221 if (state == st_oct1) 2251 if (state == st_oct1)
2226 state = st_ok; 2256 state = st_ok;
2227 } 2257 }
2228 } 2258 }
2229 2259
2230 break; 2260 break;
2231
2232 } 2261 }
2233 } 2262 }
2234} 2263}
2235 2264
2236/* check c is in chars */ 2265/* check c is in chars */
2237ecb_inline int 2266ecb_inline int
2238is_one_of (char *s, int c) 2267is_one_of (const char *s, int c)
2239{ 2268{
2240 if (c == EOF) 2269 if (c == EOF)
2241 return 1; 2270 return 1;
2242 2271
2243 return !!strchr (s, c); 2272 return !!strchr (s, c);
2299 2328
2300 if (is_one_of (" \n\t", c)) 2329 if (is_one_of (" \n\t", c))
2301 return TOK_DOT; 2330 return TOK_DOT;
2302 else 2331 else
2303 { 2332 {
2304 //TODO: ungetc twice in a row is not supported in C
2305 backchar (SCHEME_A_ c); 2333 backchar (SCHEME_A_ c);
2306 backchar (SCHEME_A_ '.');
2307 return TOK_ATOM; 2334 return TOK_DOTATOM;
2308 } 2335 }
2336
2337 case '|':
2338 return TOK_STRATOM;
2309 2339
2310 case '\'': 2340 case '\'':
2311 return TOK_QUOTE; 2341 return TOK_QUOTE;
2312 2342
2313 case ';': 2343 case ';':
2445 } 2475 }
2446 2476
2447 putcharacter (SCHEME_A_ '"'); 2477 putcharacter (SCHEME_A_ '"');
2448} 2478}
2449 2479
2450
2451/* print atoms */ 2480/* print atoms */
2452static void 2481static void
2453printatom (SCHEME_P_ pointer l, int f) 2482printatom (SCHEME_P_ pointer l, int f)
2454{ 2483{
2455 char *p; 2484 char *p;
2456 int len; 2485 int len;
2457 2486
2458 atom2str (SCHEME_A_ l, f, &p, &len); 2487 atom2str (SCHEME_A_ l, f, &p, &len);
2459 putchars (SCHEME_A_ p, len); 2488 putchars (SCHEME_A_ p, len);
2460} 2489}
2461
2462 2490
2463/* Uses internal buffer unless string pointer is already available */ 2491/* Uses internal buffer unless string pointer is already available */
2464static void 2492static void
2465atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2493atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2466{ 2494{
2480 { 2508 {
2481 p = SCHEME_V->strbuff; 2509 p = SCHEME_V->strbuff;
2482 2510
2483 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2511 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2484 { 2512 {
2485 if (num_is_integer (l)) 2513 if (is_integer (l))
2486 xnum (p, ivalue_unchecked (l)); 2514 xnum (p, ivalue_unchecked (l));
2487#if USE_REAL 2515#if USE_REAL
2488 else 2516 else
2489 { 2517 {
2490 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2518 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2627#endif 2655#endif
2628 } 2656 }
2629 else if (is_continuation (l)) 2657 else if (is_continuation (l))
2630 p = "#<CONTINUATION>"; 2658 p = "#<CONTINUATION>";
2631 else 2659 else
2660 {
2661#if USE_PRINTF
2662 p = SCHEME_V->strbuff;
2663 snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l));
2664#else
2632 p = "#<ERROR>"; 2665 p = "#<ERROR>";
2666#endif
2667 }
2633 2668
2634 *pp = p; 2669 *pp = p;
2635 *plen = strlen (p); 2670 *plen = strlen (p);
2636} 2671}
2637 2672
2745 return 0; 2780 return 0;
2746 } 2781 }
2747 else if (is_number (a)) 2782 else if (is_number (a))
2748 { 2783 {
2749 if (is_number (b)) 2784 if (is_number (b))
2750 if (num_is_integer (a) == num_is_integer (b))
2751 return num_cmp (nvalue (a), nvalue (b)) == 0; 2785 return num_cmp (nvalue (a), nvalue (b)) == 0;
2752 2786
2753 return 0; 2787 return 0;
2754 } 2788 }
2755 else if (is_character (a)) 2789 else if (is_character (a))
2756 { 2790 {
2782/* () is #t in R5RS */ 2816/* () is #t in R5RS */
2783#define is_true(p) ((p) != S_F) 2817#define is_true(p) ((p) != S_F)
2784#define is_false(p) ((p) == S_F) 2818#define is_false(p) ((p) == S_F)
2785 2819
2786/* ========== Environment implementation ========== */ 2820/* ========== 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 2821
2803#ifndef USE_ALIST_ENV 2822#ifndef USE_ALIST_ENV
2804 2823
2805/* 2824/*
2806 * In this implementation, each frame of the environment may be 2825 * In this implementation, each frame of the environment may be
2823 2842
2824 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2843 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2825 setenvironment (SCHEME_V->envir); 2844 setenvironment (SCHEME_V->envir);
2826} 2845}
2827 2846
2847static uint32_t
2848sym_hash (pointer sym, uint32_t size)
2849{
2850 uintptr_t ptr = (uintptr_t)sym;
2851
2852#if 0
2853 /* table size is prime, so why mix */
2854 ptr += ptr >> 32;
2855 ptr += ptr >> 16;
2856 ptr += ptr >> 8;
2857#endif
2858
2859 return ptr % size;
2860}
2861
2828ecb_inline void 2862ecb_inline void
2829new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2863new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2830{ 2864{
2831 pointer slot = immutable_cons (variable, value); 2865 pointer slot = immutable_cons (variable, value);
2832 2866
2833 if (is_vector (car (env))) 2867 if (is_vector (car (env)))
2834 { 2868 {
2835 int location = hash_fn (symname (variable), veclength (car (env))); 2869 int location = sym_hash (variable, veclength (car (env)));
2836
2837 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2870 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2838 } 2871 }
2839 else 2872 else
2840 set_car (env, immutable_cons (slot, car (env))); 2873 set_car (env, immutable_cons (slot, car (env)));
2841} 2874}
2842 2875
2843static pointer 2876static pointer
2844find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2877find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2845{ 2878{
2846 pointer x, y; 2879 pointer x, y;
2847 int location;
2848 2880
2849 for (x = env; x != NIL; x = cdr (x)) 2881 for (x = env; x != NIL; x = cdr (x))
2850 { 2882 {
2851 if (is_vector (car (x))) 2883 if (is_vector (car (x)))
2852 { 2884 {
2853 location = hash_fn (symname (hdl), veclength (car (x))); 2885 int location = sym_hash (hdl, veclength (car (x)));
2854 y = vector_elem (car (x), location); 2886 y = vector_get (car (x), location);
2855 } 2887 }
2856 else 2888 else
2857 y = car (x); 2889 y = car (x);
2858 2890
2859 for (; y != NIL; y = cdr (y)) 2891 for (; y != NIL; y = cdr (y))
2860 if (caar (y) == hdl) 2892 if (caar (y) == hdl)
2861 break; 2893 break;
2862 2894
2863 if (y != NIL) 2895 if (y != NIL)
2896 return car (y);
2897
2898 if (!all)
2864 break; 2899 break;
2865
2866 if (!all)
2867 return NIL;
2868 } 2900 }
2869
2870 if (x != NIL)
2871 return car (y);
2872 2901
2873 return NIL; 2902 return NIL;
2874} 2903}
2875 2904
2876#else /* USE_ALIST_ENV */ 2905#else /* USE_ALIST_ENV */
2898 for (y = car (x); y != NIL; y = cdr (y)) 2927 for (y = car (x); y != NIL; y = cdr (y))
2899 if (caar (y) == hdl) 2928 if (caar (y) == hdl)
2900 break; 2929 break;
2901 2930
2902 if (y != NIL) 2931 if (y != NIL)
2932 return car (y);
2903 break; 2933 break;
2904 2934
2905 if (!all) 2935 if (!all)
2906 return NIL; 2936 break;
2907 } 2937 }
2908
2909 if (x != NIL)
2910 return car (y);
2911 2938
2912 return NIL; 2939 return NIL;
2913} 2940}
2914 2941
2915#endif /* USE_ALIST_ENV else */ 2942#endif /* USE_ALIST_ENV else */
3132 int i = 0; 3159 int i = 0;
3133 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3160 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3134 3161
3135 while (cont != NIL) 3162 while (cont != NIL)
3136 { 3163 {
3137 frame->op = ivalue (car (cont)); cont = cdr (cont); 3164 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3138 frame->args = car (cont) ; cont = cdr (cont); 3165 frame->args = car (cont) ; cont = cdr (cont);
3139 frame->envir = car (cont) ; cont = cdr (cont); 3166 frame->envir = car (cont) ; cont = cdr (cont);
3140 frame->code = car (cont) ; cont = cdr (cont); 3167 frame->code = car (cont) ; cont = cdr (cont);
3141 3168
3142 ++frame; 3169 ++frame;
3143 ++i; 3170 ++i;
3144 } 3171 }
3145 3172
3174 SCHEME_V->value = a; 3201 SCHEME_V->value = a;
3175 3202
3176 if (dump == NIL) 3203 if (dump == NIL)
3177 return -1; 3204 return -1;
3178 3205
3179 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3206 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3180 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3207 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3181 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3208 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3182 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3209 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3183 3210
3184 SCHEME_V->dump = dump; 3211 SCHEME_V->dump = dump;
3185 3212
3186 return 0; 3213 return 0;
3187} 3214}
3356 } 3383 }
3357 else 3384 else
3358 s_return (SCHEME_V->code); 3385 s_return (SCHEME_V->code);
3359 3386
3360 case OP_E0ARGS: /* eval arguments */ 3387 case OP_E0ARGS: /* eval arguments */
3361 if (is_macro (SCHEME_V->value)) /* macro expansion */ 3388 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3362 { 3389 {
3363 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3390 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3364 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3391 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3365 SCHEME_V->code = SCHEME_V->value; 3392 SCHEME_V->code = SCHEME_V->value;
3366 s_goto (OP_APPLY); 3393 s_goto (OP_APPLY);
3393 3420
3394 case OP_TRACING: 3421 case OP_TRACING:
3395 { 3422 {
3396 int tr = SCHEME_V->tracing; 3423 int tr = SCHEME_V->tracing;
3397 3424
3398 SCHEME_V->tracing = ivalue (car (args)); 3425 SCHEME_V->tracing = ivalue_unchecked (car (args));
3399 s_return (mk_integer (SCHEME_A_ tr)); 3426 s_return (mk_integer (SCHEME_A_ tr));
3400 } 3427 }
3401 3428
3402#endif 3429#endif
3403 3430
3627 case OP_LET2: /* let */ 3654 case OP_LET2: /* let */
3628 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 3655 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3629 3656
3630 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args; 3657 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args;
3631 y != NIL; x = cdr (x), y = cdr (y)) 3658 y != NIL; x = cdr (x), y = cdr (y))
3659 { if (is_pair(caar(x))){debug(SCHEME_A_ 0, SCHEME_V->code);};//D
3660 pointer z = caar (x);
3661 if(is_pair(z)) z = car(z);//D
3662 new_slot_in_env (SCHEME_A_ z, car (y));
3663 }
3632 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3664 //new_slot_in_env (SCHEME_A_ caar (x), car (y));
3633 3665
3634 if (is_symbol (car (SCHEME_V->code))) /* named let */ 3666 if (is_symbol (car (SCHEME_V->code))) /* named let */
3635 { 3667 {
3636 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x)) 3668 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x))
3637 { 3669 {
3922{ 3954{
3923 pointer args = SCHEME_V->args; 3955 pointer args = SCHEME_V->args;
3924 pointer x = car (args); 3956 pointer x = car (args);
3925 num v; 3957 num v;
3926 3958
3927#if USE_MATH
3928 RVALUE dd;
3929#endif
3930
3931 switch (op) 3959 switch (op)
3932 { 3960 {
3933#if USE_MATH 3961#if USE_MATH
3934 case OP_INEX2EX: /* inexact->exact */ 3962 case OP_INEX2EX: /* inexact->exact */
3963 {
3935 if (num_is_integer (x)) 3964 if (is_integer (x))
3936 s_return (x); 3965 s_return (x);
3937 else if (modf (rvalue_unchecked (x), &dd) == 0) 3966
3967 RVALUE r = rvalue_unchecked (x);
3968
3969 if (r == (RVALUE)(IVALUE)r)
3938 s_return (mk_integer (SCHEME_A_ ivalue (x))); 3970 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3939 else 3971 else
3940 Error_1 ("inexact->exact: not integral:", x); 3972 Error_1 ("inexact->exact: not integral:", x);
3973 }
3941 3974
3942 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 3975 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)))); 3976 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)))); 3977 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)))); 3978 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3963 { 3996 {
3964 RVALUE result; 3997 RVALUE result;
3965 int real_result = 1; 3998 int real_result = 1;
3966 pointer y = cadr (args); 3999 pointer y = cadr (args);
3967 4000
3968 if (num_is_integer (x) && num_is_integer (y)) 4001 if (is_integer (x) && is_integer (y))
3969 real_result = 0; 4002 real_result = 0;
3970 4003
3971 /* This 'if' is an R5RS compatibility fix. */ 4004 /* This 'if' is an R5RS compatibility fix. */
3972 /* NOTE: Remove this 'if' fix for R6RS. */ 4005 /* NOTE: Remove this 'if' fix for R6RS. */
3973 if (rvalue (x) == 0 && rvalue (y) < 0) 4006 if (rvalue (x) == 0 && rvalue (y) < 0)
3979 /* If the test fails, result is too big for integer. */ 4012 /* If the test fails, result is too big for integer. */
3980 if (!real_result) 4013 if (!real_result)
3981 { 4014 {
3982 long result_as_long = result; 4015 long result_as_long = result;
3983 4016
3984 if (result != (RVALUE) result_as_long) 4017 if (result != result_as_long)
3985 real_result = 1; 4018 real_result = 1;
3986 } 4019 }
3987 4020
3988 if (real_result) 4021 if (real_result)
3989 s_return (mk_real (SCHEME_A_ result)); 4022 s_return (mk_real (SCHEME_A_ result));
3994 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4027 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)))); 4028 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
3996 4029
3997 case OP_TRUNCATE: 4030 case OP_TRUNCATE:
3998 { 4031 {
3999 RVALUE rvalue_of_x; 4032 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))); 4033 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 } 4034 }
4008 4035
4009 case OP_ROUND: 4036 case OP_ROUND:
4010 if (num_is_integer (x)) 4037 if (is_integer (x))
4011 s_return (x); 4038 s_return (x);
4012 4039
4013 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4040 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4014#endif 4041#endif
4015 4042
4135 } 4162 }
4136 else 4163 else
4137 Error_0 ("set-cdr!: unable to alter immutable pair"); 4164 Error_0 ("set-cdr!: unable to alter immutable pair");
4138 4165
4139 case OP_CHAR2INT: /* char->integer */ 4166 case OP_CHAR2INT: /* char->integer */
4140 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4167 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4141 4168
4142 case OP_INT2CHAR: /* integer->char */ 4169 case OP_INT2CHAR: /* integer->char */
4143 s_return (mk_character (SCHEME_A_ ivalue (x))); 4170 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4144 4171
4145 case OP_CHARUPCASE: 4172 case OP_CHARUPCASE:
4146 { 4173 {
4147 unsigned char c = ivalue (x); 4174 unsigned char c = ivalue_unchecked (x);
4148 c = toupper (c); 4175 c = toupper (c);
4149 s_return (mk_character (SCHEME_A_ c)); 4176 s_return (mk_character (SCHEME_A_ c));
4150 } 4177 }
4151 4178
4152 case OP_CHARDNCASE: 4179 case OP_CHARDNCASE:
4153 { 4180 {
4154 unsigned char c = ivalue (x); 4181 unsigned char c = ivalue_unchecked (x);
4155 c = tolower (c); 4182 c = tolower (c);
4156 s_return (mk_character (SCHEME_A_ c)); 4183 s_return (mk_character (SCHEME_A_ c));
4157 } 4184 }
4158 4185
4159 case OP_STR2SYM: /* string->symbol */ 4186 case OP_STR2SYM: /* string->symbol */
4236 Error_1 ("atom->string: not an atom:", x); 4263 Error_1 ("atom->string: not an atom:", x);
4237 } 4264 }
4238 4265
4239 case OP_MKSTRING: /* make-string */ 4266 case OP_MKSTRING: /* make-string */
4240 { 4267 {
4241 int fill = ' '; 4268 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4242 int len;
4243
4244 len = ivalue (x); 4269 int len = ivalue_unchecked (x);
4245
4246 if (cdr (args) != NIL)
4247 fill = charvalue (cadr (args));
4248 4270
4249 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4271 s_return (mk_empty_string (SCHEME_A_ len, fill));
4250 } 4272 }
4251 4273
4252 case OP_STRLEN: /* string-length */ 4274 case OP_STRLEN: /* string-length */
4253 s_return (mk_integer (SCHEME_A_ strlength (x))); 4275 s_return (mk_integer (SCHEME_A_ strlength (x)));
4254 4276
4255 case OP_STRREF: /* string-ref */ 4277 case OP_STRREF: /* string-ref */
4256 { 4278 {
4257 char *str;
4258 int index;
4259
4260 str = strvalue (x); 4279 char *str = strvalue (x);
4261
4262 index = ivalue (cadr (args)); 4280 int index = ivalue_unchecked (cadr (args));
4263 4281
4264 if (index >= strlength (x)) 4282 if (index >= strlength (x))
4265 Error_1 ("string-ref: out of bounds:", cadr (args)); 4283 Error_1 ("string-ref: out of bounds:", cadr (args));
4266 4284
4267 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4285 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4268 } 4286 }
4269 4287
4270 case OP_STRSET: /* string-set! */ 4288 case OP_STRSET: /* string-set! */
4271 { 4289 {
4272 char *str; 4290 char *str = strvalue (x);
4273 int index; 4291 int index = ivalue_unchecked (cadr (args));
4274 int c; 4292 int c;
4275 4293
4276 if (is_immutable (x)) 4294 if (is_immutable (x))
4277 Error_1 ("string-set!: unable to alter immutable string:", x); 4295 Error_1 ("string-set!: unable to alter immutable string:", x);
4278
4279 str = strvalue (x);
4280
4281 index = ivalue (cadr (args));
4282 4296
4283 if (index >= strlength (x)) 4297 if (index >= strlength (x))
4284 Error_1 ("string-set!: out of bounds:", cadr (args)); 4298 Error_1 ("string-set!: out of bounds:", cadr (args));
4285 4299
4286 c = charvalue (caddr (args)); 4300 c = charvalue (caddr (args));
4309 s_return (newstr); 4323 s_return (newstr);
4310 } 4324 }
4311 4325
4312 case OP_SUBSTR: /* substring */ 4326 case OP_SUBSTR: /* substring */
4313 { 4327 {
4314 char *str; 4328 char *str = strvalue (x);
4315 int index0; 4329 int index0 = ivalue_unchecked (cadr (args));
4316 int index1; 4330 int index1;
4317 int len; 4331 int len;
4318 4332
4319 str = strvalue (x);
4320
4321 index0 = ivalue (cadr (args));
4322
4323 if (index0 > strlength (x)) 4333 if (index0 > strlength (x))
4324 Error_1 ("substring: start out of bounds:", cadr (args)); 4334 Error_1 ("substring: start out of bounds:", cadr (args));
4325 4335
4326 if (cddr (args) != NIL) 4336 if (cddr (args) != NIL)
4327 { 4337 {
4328 index1 = ivalue (caddr (args)); 4338 index1 = ivalue_unchecked (caddr (args));
4329 4339
4330 if (index1 > strlength (x) || index1 < index0) 4340 if (index1 > strlength (x) || index1 < index0)
4331 Error_1 ("substring: end out of bounds:", caddr (args)); 4341 Error_1 ("substring: end out of bounds:", caddr (args));
4332 } 4342 }
4333 else 4343 else
4356 if (SCHEME_V->no_memory) 4366 if (SCHEME_V->no_memory)
4357 s_return (S_SINK); 4367 s_return (S_SINK);
4358#endif 4368#endif
4359 4369
4360 for (x = args, i = 0; is_pair (x); x = cdr (x), i++) 4370 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4361 set_vector_elem (vec, i, car (x)); 4371 vector_set (vec, i, car (x));
4362 4372
4363 s_return (vec); 4373 s_return (vec);
4364 } 4374 }
4365 4375
4366 case OP_MKVECTOR: /* make-vector */ 4376 case OP_MKVECTOR: /* make-vector */
4367 { 4377 {
4368 pointer fill = NIL; 4378 pointer fill = NIL;
4369 int len;
4370 pointer vec; 4379 pointer vec;
4371
4372 len = ivalue (x); 4380 int len = ivalue_unchecked (x);
4373 4381
4374 if (cdr (args) != NIL) 4382 if (cdr (args) != NIL)
4375 fill = cadr (args); 4383 fill = cadr (args);
4376 4384
4377 vec = mk_vector (SCHEME_A_ len); 4385 vec = mk_vector (SCHEME_A_ len);
4380 if (SCHEME_V->no_memory) 4388 if (SCHEME_V->no_memory)
4381 s_return (S_SINK); 4389 s_return (S_SINK);
4382#endif 4390#endif
4383 4391
4384 if (fill != NIL) 4392 if (fill != NIL)
4385 fill_vector (vec, fill); 4393 fill_vector (vec, 0, fill);
4386 4394
4387 s_return (vec); 4395 s_return (vec);
4388 } 4396 }
4389 4397
4390 case OP_VECLEN: /* vector-length */ 4398 case OP_VECLEN: /* vector-length */
4391 s_return (mk_integer (SCHEME_A_ veclength (x))); 4399 s_return (mk_integer (SCHEME_A_ veclength (x)));
4392 4400
4401 case OP_VECRESIZE:
4402 vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args));
4403 s_return (x);
4404
4393 case OP_VECREF: /* vector-ref */ 4405 case OP_VECREF: /* vector-ref */
4394 { 4406 {
4395 int index;
4396
4397 index = ivalue (cadr (args)); 4407 int index = ivalue_unchecked (cadr (args));
4398 4408
4399 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4409 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4400 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4410 Error_1 ("vector-ref: out of bounds:", cadr (args));
4401 4411
4402 s_return (vector_elem (x, index)); 4412 s_return (vector_get (x, index));
4403 } 4413 }
4404 4414
4405 case OP_VECSET: /* vector-set! */ 4415 case OP_VECSET: /* vector-set! */
4406 { 4416 {
4407 int index; 4417 int index = ivalue_unchecked (cadr (args));
4408 4418
4409 if (is_immutable (x)) 4419 if (is_immutable (x))
4410 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4420 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4411 4421
4412 index = ivalue (cadr (args));
4413
4414 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4422 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4415 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4423 Error_1 ("vector-set!: out of bounds:", cadr (args));
4416 4424
4417 set_vector_elem (x, index, caddr (args)); 4425 vector_set (x, index, caddr (args));
4418 s_return (x); 4426 s_return (x);
4419 } 4427 }
4420 } 4428 }
4421 4429
4422 if (USE_ERROR_CHECKING) abort (); 4430 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} 4431}
4478 4432
4479static int 4433static int
4480opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4434opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4481{ 4435{
4527 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4481 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4528 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4482 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4529 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4483 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4530 4484
4531#if USE_CHAR_CLASSIFIERS 4485#if USE_CHAR_CLASSIFIERS
4532 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4486 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4533 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4487 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4534 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4488 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4535 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4489 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4536 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4490 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4537#endif 4491#endif
4538 4492
4539#if USE_PORTS 4493#if USE_PORTS
4540 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4494 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4541 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4495 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4742 4696
4743 case OP_NEWSEGMENT: /* new-segment */ 4697 case OP_NEWSEGMENT: /* new-segment */
4744 if (!is_pair (args) || !is_number (a)) 4698 if (!is_pair (args) || !is_number (a))
4745 Error_0 ("new-segment: argument must be a number"); 4699 Error_0 ("new-segment: argument must be a number");
4746 4700
4747 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 4701 alloc_cellseg (SCHEME_A_ ivalue (a));
4748 4702
4749 s_return (S_T); 4703 s_return (S_T);
4750 4704
4751 case OP_OBLIST: /* oblist */ 4705 case OP_OBLIST: /* oblist */
4752 s_return (oblist_all_symbols (SCHEME_A)); 4706 s_return (oblist_all_symbols (SCHEME_A));
5010 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 4964 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
5011 SCHEME_V->tok = token (SCHEME_A); 4965 SCHEME_V->tok = token (SCHEME_A);
5012 s_goto (OP_RDSEXPR); 4966 s_goto (OP_RDSEXPR);
5013 4967
5014 case TOK_ATOM: 4968 case TOK_ATOM:
5015 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); 4969 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS)));
4970
4971 case TOK_DOTATOM:
4972 SCHEME_V->strbuff[0] = '.';
4973 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
4974
4975 case TOK_STRATOM:
4976 x = readstrexp (SCHEME_A_ '|');
4977 //TODO: haven't checked whether the garbage collector could interfere
4978 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5016 4979
5017 case TOK_DQUOTE: 4980 case TOK_DQUOTE:
5018 x = readstrexp (SCHEME_A); 4981 x = readstrexp (SCHEME_A_ '"');
5019 4982
5020 if (x == S_F) 4983 if (x == S_F)
5021 Error_0 ("Error reading string"); 4984 Error_0 ("Error reading string");
5022 4985
5023 setimmutable (x); 4986 setimmutable (x);
5035 s_goto (OP_EVAL); 4998 s_goto (OP_EVAL);
5036 } 4999 }
5037 } 5000 }
5038 5001
5039 case TOK_SHARP_CONST: 5002 case TOK_SHARP_CONST:
5040 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) 5003 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5041 Error_0 ("undefined sharp expression"); 5004 Error_0 ("undefined sharp expression");
5042 else 5005 else
5043 s_return (x); 5006 s_return (x);
5044 5007
5045 default: 5008 default:
5197 putstr (SCHEME_A_ ")"); 5160 putstr (SCHEME_A_ ")");
5198 s_return (S_T); 5161 s_return (S_T);
5199 } 5162 }
5200 else 5163 else
5201 { 5164 {
5202 pointer elem = vector_elem (vec, i); 5165 pointer elem = vector_get (vec, i);
5203 5166
5204 ivalue_unchecked (cdr (args)) = i + 1; 5167 ivalue_unchecked (cdr (args)) = i + 1;
5205 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5168 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5206 SCHEME_V->args = elem; 5169 SCHEME_V->args = elem;
5207 5170
5267 5230
5268 case OP_CLOSUREP: /* closure? */ 5231 case OP_CLOSUREP: /* closure? */
5269 /* 5232 /*
5270 * Note, macro object is also a closure. 5233 * Note, macro object is also a closure.
5271 * Therefore, (closure? <#MACRO>) ==> #t 5234 * Therefore, (closure? <#MACRO>) ==> #t
5235 * (schmorp) well, obviously not, fix? TODO
5272 */ 5236 */
5273 s_retbool (is_closure (a)); 5237 s_retbool (is_closure (a));
5274 5238
5275 case OP_MACROP: /* macro? */ 5239 case OP_MACROP: /* macro? */
5276 s_retbool (is_macro (a)); 5240 s_retbool (is_macro (a));
5282/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5246/* 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); 5247typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5284 5248
5285typedef int (*test_predicate)(pointer); 5249typedef int (*test_predicate)(pointer);
5286static int 5250static int
5287is_any (pointer p) 5251tst_any (pointer p)
5288{ 5252{
5289 return 1; 5253 return 1;
5290} 5254}
5291 5255
5292static int 5256static int
5293is_nonneg (pointer p) 5257tst_inonneg (pointer p)
5294{ 5258{
5295 return ivalue (p) >= 0 && is_integer (p); 5259 return is_integer (p) && ivalue_unchecked (p) >= 0;
5296} 5260}
5297 5261
5298static int 5262static int
5299tst_is_list (pointer p) 5263tst_is_list (SCHEME_P_ pointer p)
5300{ 5264{
5301 return p == NIL || is_pair (p); 5265 return p == NIL || is_pair (p);
5302} 5266}
5303 5267
5304/* Correspond carefully with following defines! */ 5268/* Correspond carefully with following defines! */
5305static struct 5269static struct
5306{ 5270{
5307 test_predicate fct; 5271 test_predicate fct;
5308 const char *kind; 5272 const char *kind;
5309} tests[] = 5273} tests[] = {
5310{
5311 { is_any, 0 }, 5274 { tst_any , 0 },
5312 { is_string, "string" }, 5275 { is_string , "string" },
5313 { is_symbol, "symbol" }, 5276 { is_symbol , "symbol" },
5314 { is_port, "port" }, 5277 { is_port , "port" },
5315 { is_inport, "input port" }, 5278 { is_inport , "input port" },
5316 { is_outport, "output port" }, 5279 { is_outport , "output port" },
5317 { is_environment, "environment" }, 5280 { is_environment, "environment" },
5318 { is_pair, "pair" }, 5281 { is_pair , "pair" },
5319 { tst_is_list, "pair or '()" }, 5282 { 0 , "pair or '()" },
5320 { is_character, "character" }, 5283 { is_character , "character" },
5321 { is_vector, "vector" }, 5284 { is_vector , "vector" },
5322 { is_number, "number" }, 5285 { is_number , "number" },
5323 { is_integer, "integer" }, 5286 { is_integer , "integer" },
5324 { is_nonneg, "non-negative integer" } 5287 { tst_inonneg , "non-negative integer" }
5325}; 5288};
5326 5289
5327#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ 5290#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5328#define TST_ANY "\001" 5291#define TST_ANY "\001"
5329#define TST_STRING "\002" 5292#define TST_STRING "\002"
5370typedef struct 5333typedef struct
5371{ 5334{
5372 uint8_t func; 5335 uint8_t func;
5373 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ 5336 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5374 uint8_t builtin; 5337 uint8_t builtin;
5338#if USE_ERROR_CHECKING
5375 uint8_t min_arity; 5339 uint8_t min_arity;
5376 uint8_t max_arity; 5340 uint8_t max_arity;
5377 char arg_tests_encoding[3]; 5341 char arg_tests_encoding[3];
5342#endif
5378} op_code_info; 5343} op_code_info;
5379 5344
5380static const op_code_info dispatch_table[] = { 5345static const op_code_info dispatch_table[] = {
5346#if USE_ERROR_CHECKING
5381#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, 5347#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5348#else
5349#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5350#endif
5382#include "opdefines.h" 5351#include "opdefines.h"
5383#undef OP_DEF 5352#undef OP_DEF
5384 {0} 5353 {0}
5385}; 5354};
5386 5355
5428 { 5397 {
5429 pointer arg = car (arglist); 5398 pointer arg = car (arglist);
5430 5399
5431 j = t[0]; 5400 j = t[0];
5432 5401
5402 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5403 if (j == TST_LIST[0])
5404 {
5405 if (!tst_is_list (SCHEME_A_ arg))
5406 break;
5407 }
5408 else
5409 {
5433 if (!tests[j - 1].fct (arg)) 5410 if (!tests[j - 1].fct (arg))
5434 break; 5411 break;
5412 }
5435 5413
5436 if (t[1]) /* last test is replicated as necessary */ 5414 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5437 t++; 5415 t++;
5438 5416
5439 arglist = cdr (arglist); 5417 arglist = cdr (arglist);
5440 i++; 5418 i++;
5441 } 5419 }
5496mk_proc (SCHEME_P_ enum scheme_opcodes op) 5474mk_proc (SCHEME_P_ enum scheme_opcodes op)
5497{ 5475{
5498 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5476 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5499 set_typeflag (y, (T_PROC | T_ATOM)); 5477 set_typeflag (y, (T_PROC | T_ATOM));
5500 ivalue_unchecked (y) = op; 5478 ivalue_unchecked (y) = op;
5501 set_num_integer (y);
5502 return y; 5479 return y;
5503} 5480}
5504 5481
5505/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5482/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5506static int 5483static int
5507syntaxnum (pointer p) 5484syntaxnum (pointer p)
5508{ 5485{
5509 const char *s = strvalue (car (p)); 5486 const char *s = strvalue (p);
5510 5487
5511 switch (strlength (car (p))) 5488 switch (strlength (p))
5512 { 5489 {
5513 case 2: 5490 case 2:
5514 if (s[0] == 'i') 5491 if (s[0] == 'i')
5515 return OP_IF0; /* if */ 5492 return OP_IF0; /* if */
5516 else 5493 else
5952# endif 5929# endif
5953 int fin; 5930 int fin;
5954 char *file_name = InitFile; 5931 char *file_name = InitFile;
5955 int retcode; 5932 int retcode;
5956 int isfile = 1; 5933 int isfile = 1;
5934 system ("ps v $PPID");//D
5957 5935
5958 if (argc == 2 && strcmp (argv[1], "-?") == 0) 5936 if (argc == 2 && strcmp (argv[1], "-?") == 0)
5959 { 5937 {
5960 xwrstr ("Usage: tinyscheme -?\n"); 5938 xwrstr ("Usage: tinyscheme -?\n");
5961 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 5939 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines