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.23 by root, Fri Nov 27 02:06:36 2015 UTC vs.
Revision 1.40 by root, Mon Nov 30 05:19:01 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,
59 TOK_SHARP_CONST, 74 TOK_SHARP_CONST,
60 TOK_VEC 75 TOK_VEC
61}; 76};
62 77
63#define BACKQUOTE '`' 78#define BACKQUOTE '`'
64#define DELIMITERS "()\";\f\t\v\n\r " 79#define WHITESPACE " \t\r\n\v\f"
80#define DELIMITERS "()\";" WHITESPACE
65 81
66#define NIL (&SCHEME_V->xNIL) //TODO: make this 0? 82#define NIL (&SCHEME_V->xNIL) //TODO: make this 0?
67#define S_T (&SCHEME_V->xT) //TODO: magic ptr value? 83#define S_T (&SCHEME_V->xT) //TODO: magic ptr value?
68#define S_F (&SCHEME_V->xF) //TODO: magic ptr value? 84#define S_F (&SCHEME_V->xF) //TODO: magic ptr value?
69#define S_SINK (&SCHEME_V->xsink) 85#define S_SINK (&SCHEME_V->xsink)
182# define FIRST_CELLSEGS 3 198# define FIRST_CELLSEGS 3
183#endif 199#endif
184 200
185enum scheme_types 201enum scheme_types
186{ 202{
203 T_INTEGER,
187 T_FREE, 204 T_REAL,
188 T_STRING, 205 T_STRING,
189 T_NUMBER,
190 T_SYMBOL, 206 T_SYMBOL,
191 T_PROC, 207 T_PROC,
192 T_PAIR, 208 T_PAIR, /* also used for free cells */
193 T_CLOSURE, 209 T_CLOSURE,
194 T_CONTINUATION, 210 T_CONTINUATION,
195 T_FOREIGN, 211 T_FOREIGN,
196 T_CHARACTER, 212 T_CHARACTER,
197 T_PORT, 213 T_PORT,
207#define T_SYNTAX 0x0010 223#define T_SYNTAX 0x0010
208#define T_IMMUTABLE 0x0020 224#define T_IMMUTABLE 0x0020
209#define T_ATOM 0x0040 /* only for gc */ 225#define T_ATOM 0x0040 /* only for gc */
210#define T_MARK 0x0080 /* only for gc */ 226#define T_MARK 0x0080 /* only for gc */
211 227
228/* num, for generic arithmetic */
229struct num
230{
231 IVALUE ivalue;
232#if USE_REAL
233 RVALUE rvalue;
234 char is_fixnum;
235#endif
236};
237
238#if USE_REAL
239# define num_is_fixnum(n) (n).is_fixnum
240# define num_set_fixnum(n,f) (n).is_fixnum = (f)
241# define num_ivalue(n) (n).ivalue
242# define num_rvalue(n) (n).rvalue
243# define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i)
244# define num_set_rvalue(n,r) (n).rvalue = (r)
245#else
246# define num_is_fixnum(n) 1
247# define num_set_fixnum(n,f) 0
248# define num_ivalue(n) (n).ivalue
249# define num_rvalue(n) (n).ivalue
250# define num_set_ivalue(n,i) (n).ivalue = (i)
251# define num_set_rvalue(n,r) (n).ivalue = (r)
252#endif
253
212enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; 254enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
213 255
214static num num_op (enum num_op op, num a, num b); 256static num num_op (enum num_op op, num a, num b);
215static num num_intdiv (num a, num b); 257static num num_intdiv (num a, num b);
216static num num_rem (num a, num b); 258static num num_rem (num a, num b);
219#if USE_MATH 261#if USE_MATH
220static double round_per_R5RS (double x); 262static double round_per_R5RS (double x);
221#endif 263#endif
222static int is_zero_rvalue (RVALUE x); 264static int is_zero_rvalue (RVALUE x);
223 265
224ecb_inline int
225num_is_integer (pointer p)
226{
227 return num_is_fixnum (p->object.number);
228}
229
230static num num_zero; 266static num num_zero;
231static num num_one; 267static num num_one;
232 268
233/* macros for cell operations */ 269/* macros for cell operations */
234#define typeflag(p) ((p)->flag + 0) 270#define typeflag(p) ((p)->flag + 0)
242} 278}
243 279
244#define strvalue(p) ((p)->object.string.svalue) 280#define strvalue(p) ((p)->object.string.svalue)
245#define strlength(p) ((p)->object.string.length) 281#define strlength(p) ((p)->object.string.length)
246 282
247INTERFACE int is_list (SCHEME_P_ pointer p);
248
249INTERFACE int 283INTERFACE int
250is_vector (pointer p) 284is_vector (pointer p)
251{ 285{
252 return type (p) == T_VECTOR; 286 return type (p) == T_VECTOR;
253} 287}
254 288
255#define vecvalue(p) ((p)->object.vector.vvalue) 289#define vecvalue(p) ((p)->object.vector.vvalue)
256#define veclength(p) ((p)->object.vector.length) 290#define veclength(p) ((p)->object.vector.length)
257INTERFACE void fill_vector (pointer vec, pointer obj); 291INTERFACE 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); 292INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
260INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); 293INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
261 294
262INTERFACE uint32_t 295INTERFACE int
263vector_length (pointer vec) 296is_integer (pointer p)
264{ 297{
265 return vec->object.vector.length; 298 return type (p) == T_INTEGER;
299}
300
301/* not the same as in scheme, where integers are (correctly :) reals */
302INTERFACE int
303is_real (pointer p)
304{
305 return type (p) == T_REAL;
266} 306}
267 307
268INTERFACE int 308INTERFACE int
269is_number (pointer p) 309is_number (pointer p)
270{ 310{
271 return type (p) == T_NUMBER; 311 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} 312}
291 313
292INTERFACE int 314INTERFACE int
293is_character (pointer p) 315is_character (pointer p)
294{ 316{
299string_value (pointer p) 321string_value (pointer p)
300{ 322{
301 return strvalue (p); 323 return strvalue (p);
302} 324}
303 325
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) 326#define ivalue_unchecked(p) (p)->object.ivalue
327#define set_ivalue(p,v) (p)->object.ivalue = (v)
328
335#if USE_REAL 329#if USE_REAL
336# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 330#define rvalue_unchecked(p) (p)->object.rvalue
337# define set_num_integer(p) (p)->object.number.is_fixnum=1; 331#define set_rvalue(p,v) (p)->object.rvalue = (v)
338# define set_num_real(p) (p)->object.number.is_fixnum=0;
339#else 332#else
340# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 333#define rvalue_unchecked(p) (p)->object.ivalue
341# define set_num_integer(p) 0 334#define set_rvalue(p,v) (p)->object.ivalue = (v)
342# define set_num_real(p) 0
343#endif 335#endif
344 336
345INTERFACE long 337INTERFACE long
346charvalue (pointer p) 338charvalue (pointer p)
347{ 339{
415} 407}
416 408
417INTERFACE char * 409INTERFACE char *
418symname (pointer p) 410symname (pointer p)
419{ 411{
420 return strvalue (car (p)); 412 return strvalue (p);
421} 413}
422 414
423#if USE_PLIST 415#if USE_PLIST
424SCHEME_EXPORT int 416SCHEME_EXPORT int
425hasprop (pointer p) 417hasprop (pointer p)
449} 441}
450 442
451INTERFACE char * 443INTERFACE char *
452syntaxname (pointer p) 444syntaxname (pointer p)
453{ 445{
454 return strvalue (car (p)); 446 return strvalue (p);
455} 447}
456 448
457#define procnum(p) ivalue (p) 449#define procnum(p) ivalue_unchecked (p)
458static const char *procname (pointer x); 450static const char *procname (pointer x);
459 451
460INTERFACE int 452INTERFACE int
461is_closure (pointer p) 453is_closure (pointer p)
462{ 454{
523setimmutable (pointer p) 515setimmutable (pointer p)
524{ 516{
525#if USE_ERROR_CHECKING 517#if USE_ERROR_CHECKING
526 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 518 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
527#endif 519#endif
520}
521
522/* Result is:
523 proper list: length
524 circular list: -1
525 not even a pair: -2
526 dotted list: -2 minus length before dot
527*/
528INTERFACE int
529list_length (SCHEME_P_ pointer a)
530{
531 int i = 0;
532 pointer slow, fast;
533
534 slow = fast = a;
535
536 while (1)
537 {
538 if (fast == NIL)
539 return i;
540
541 if (!is_pair (fast))
542 return -2 - i;
543
544 fast = cdr (fast);
545 ++i;
546
547 if (fast == NIL)
548 return i;
549
550 if (!is_pair (fast))
551 return -2 - i;
552
553 ++i;
554 fast = cdr (fast);
555
556 /* Safe because we would have already returned if `fast'
557 encountered a non-pair. */
558 slow = cdr (slow);
559
560 if (fast == slow)
561 {
562 /* the fast pointer has looped back around and caught up
563 with the slow pointer, hence the structure is circular,
564 not of finite length, and therefore not a list */
565 return -1;
566 }
567 }
568}
569
570INTERFACE int
571is_list (SCHEME_P_ pointer a)
572{
573 return list_length (SCHEME_A_ a) >= 0;
528} 574}
529 575
530#if USE_CHAR_CLASSIFIERS 576#if USE_CHAR_CLASSIFIERS
531ecb_inline int 577ecb_inline int
532Cisalpha (int c) 578Cisalpha (int c)
621#endif 667#endif
622 668
623static int file_push (SCHEME_P_ const char *fname); 669static int file_push (SCHEME_P_ const char *fname);
624static void file_pop (SCHEME_P); 670static void file_pop (SCHEME_P);
625static int file_interactive (SCHEME_P); 671static int file_interactive (SCHEME_P);
626ecb_inline int is_one_of (char *s, int c); 672ecb_inline int is_one_of (const char *s, int c);
627static int alloc_cellseg (SCHEME_P_ int n); 673static int alloc_cellseg (SCHEME_P_ int n);
628ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); 674ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
629static void finalize_cell (SCHEME_P_ pointer a); 675static void finalize_cell (SCHEME_P_ pointer a);
630static int count_consecutive_cells (pointer x, int needed); 676static int count_consecutive_cells (pointer x, int needed);
631static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 677static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
648static void mark (pointer a); 694static void mark (pointer a);
649static void gc (SCHEME_P_ pointer a, pointer b); 695static void gc (SCHEME_P_ pointer a, pointer b);
650static int basic_inchar (port *pt); 696static int basic_inchar (port *pt);
651static int inchar (SCHEME_P); 697static int inchar (SCHEME_P);
652static void backchar (SCHEME_P_ int c); 698static void backchar (SCHEME_P_ int c);
653static char *readstr_upto (SCHEME_P_ char *delim); 699static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
654static pointer readstrexp (SCHEME_P); 700static pointer readstrexp (SCHEME_P_ char delim);
655ecb_inline int skipspace (SCHEME_P); 701ecb_inline int skipspace (SCHEME_P);
656static int token (SCHEME_P); 702static int token (SCHEME_P);
657static void printslashstring (SCHEME_P_ char *s, int len); 703static void printslashstring (SCHEME_P_ char *s, int len);
658static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 704static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
659static void printatom (SCHEME_P_ pointer l, int f); 705static void printatom (SCHEME_P_ pointer l, int f);
676static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 722static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
677static void assign_syntax (SCHEME_P_ const char *name); 723static void assign_syntax (SCHEME_P_ const char *name);
678static int syntaxnum (pointer p); 724static int syntaxnum (pointer p);
679static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 725static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
680 726
727static IVALUE
728ivalue (pointer x)
729{
730 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
731}
732
733static RVALUE
734rvalue (pointer x)
735{
736 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
737}
738
739INTERFACE num
740nvalue (pointer x)
741{
742 num n;
743
744 num_set_fixnum (n, is_integer (x));
745
746 if (num_is_fixnum (n))
747 num_set_ivalue (n, ivalue_unchecked (x));
748 else
749 num_set_rvalue (n, rvalue_unchecked (x));
750
751 return n;
752}
753
681static num 754static num
682num_op (enum num_op op, num a, num b) 755num_op (enum num_op op, num a, num b)
683{ 756{
684 num ret; 757 num ret;
685 758
686 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 759 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
687 760
688 if (num_is_fixnum (ret)) 761 if (num_is_fixnum (ret))
689 { 762 {
690 IVALUE av = num_get_ivalue (a);
691 IVALUE bv = num_get_ivalue (b);
692
693 switch (op) 763 switch (op)
694 { 764 {
695 case NUM_ADD: av += bv; break; 765 case NUM_ADD: a.ivalue += b.ivalue; break;
696 case NUM_SUB: av -= bv; break; 766 case NUM_SUB: a.ivalue -= b.ivalue; break;
697 case NUM_MUL: av *= bv; break; 767 case NUM_MUL: a.ivalue *= b.ivalue; break;
698 case NUM_INTDIV: av /= bv; break; 768 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
699 } 769 }
700 770
701 num_set_ivalue (ret, av); 771 num_set_ivalue (ret, a.ivalue);
702 } 772 }
773#if USE_REAL
703 else 774 else
704 { 775 {
705 RVALUE av = num_get_rvalue (a);
706 RVALUE bv = num_get_rvalue (b);
707
708 switch (op) 776 switch (op)
709 { 777 {
710 case NUM_ADD: av += bv; break; 778 case NUM_ADD: a.rvalue += b.rvalue; break;
711 case NUM_SUB: av -= bv; break; 779 case NUM_SUB: a.rvalue -= b.rvalue; break;
712 case NUM_MUL: av *= bv; break; 780 case NUM_MUL: a.rvalue *= b.rvalue; break;
713 case NUM_INTDIV: av /= bv; break; 781 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
714 } 782 }
715 783
716 num_set_rvalue (ret, av); 784 num_set_rvalue (ret, a.rvalue);
717 } 785 }
786#endif
718 787
719 return ret; 788 return ret;
720} 789}
721 790
722static num 791static num
723num_div (num a, num b) 792num_div (num a, num b)
724{ 793{
725 num ret; 794 num ret;
726 795
727 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); 796 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0);
728 797
729 if (num_is_fixnum (ret)) 798 if (num_is_fixnum (ret))
730 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 799 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
731 else 800 else
732 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 801 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
733 802
734 return ret; 803 return ret;
735} 804}
736 805
737static num 806static num
739{ 808{
740 num ret; 809 num ret;
741 long e1, e2, res; 810 long e1, e2, res;
742 811
743 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 812 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
744 e1 = num_get_ivalue (a); 813 e1 = num_ivalue (a);
745 e2 = num_get_ivalue (b); 814 e2 = num_ivalue (b);
746 res = e1 % e2; 815 res = e1 % e2;
747 816
748 /* remainder should have same sign as second operand */ 817 /* remainder should have same sign as second operand */
749 if (res > 0) 818 if (res > 0)
750 { 819 {
766{ 835{
767 num ret; 836 num ret;
768 long e1, e2, res; 837 long e1, e2, res;
769 838
770 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 839 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
771 e1 = num_get_ivalue (a); 840 e1 = num_ivalue (a);
772 e2 = num_get_ivalue (b); 841 e2 = num_ivalue (b);
773 res = e1 % e2; 842 res = e1 % e2;
774 843
775 /* modulo should have same sign as second operand */ 844 /* modulo should have same sign as second operand */
776 if (res * e2 < 0) 845 if (res * e2 < 0)
777 res += e2; 846 res += e2;
787 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 856 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
788 int ret; 857 int ret;
789 858
790 if (is_fixnum) 859 if (is_fixnum)
791 { 860 {
792 IVALUE av = num_get_ivalue (a); 861 IVALUE av = num_ivalue (a);
793 IVALUE bv = num_get_ivalue (b); 862 IVALUE bv = num_ivalue (b);
794 863
795 ret = av == bv ? 0 : av < bv ? -1 : +1; 864 ret = av == bv ? 0 : av < bv ? -1 : +1;
796 } 865 }
797 else 866 else
798 { 867 {
799 RVALUE av = num_get_rvalue (a); 868 RVALUE av = num_rvalue (a);
800 RVALUE bv = num_get_rvalue (b); 869 RVALUE bv = num_rvalue (b);
801 870
802 ret = av == bv ? 0 : av < bv ? -1 : +1; 871 ret = av == bv ? 0 : av < bv ? -1 : +1;
803 } 872 }
804 873
805 return ret; 874 return ret;
868 return k; 937 return k;
869 938
870 i = ++SCHEME_V->last_cell_seg; 939 i = ++SCHEME_V->last_cell_seg;
871 SCHEME_V->alloc_seg[i] = cp; 940 SCHEME_V->alloc_seg[i] = cp;
872 941
873 /* insert new segment in address order */
874 newp = (pointer)cp; 942 newp = (pointer)cp;
875 SCHEME_V->cell_seg[i] = newp; 943 SCHEME_V->cell_seg[i] = newp;
876 SCHEME_V->cell_segsize[i] = segsize; 944 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; 945 SCHEME_V->fcells += segsize;
893 last = newp + segsize - 1; 946 last = newp + segsize - 1;
894 947
895 for (p = newp; p <= last; p++) 948 for (p = newp; p <= last; p++)
896 { 949 {
897 set_typeflag (p, T_FREE); 950 set_typeflag (p, T_PAIR);
898 set_car (p, NIL); 951 set_car (p, NIL);
899 set_cdr (p, p + 1); 952 set_cdr (p, p + 1);
900 } 953 }
901 954
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); 955 set_cdr (last, SCHEME_V->free_cell);
906 SCHEME_V->free_cell = newp; 956 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 } 957 }
919 958
920 return n; 959 return n;
921} 960}
922 961
1001 /* Record it as a vector so that gc understands it. */ 1040 /* Record it as a vector so that gc understands it. */
1002 set_typeflag (v, T_VECTOR | T_ATOM); 1041 set_typeflag (v, T_VECTOR | T_ATOM);
1003 1042
1004 v->object.vector.vvalue = e; 1043 v->object.vector.vvalue = e;
1005 v->object.vector.length = len; 1044 v->object.vector.length = len;
1006 fill_vector (v, init); 1045 fill_vector (v, 0, init);
1007 push_recent_alloc (SCHEME_A_ v, NIL); 1046 push_recent_alloc (SCHEME_A_ v, NIL);
1008 1047
1009 return v; 1048 return v;
1010} 1049}
1011 1050
1056 return x; 1095 return x;
1057} 1096}
1058 1097
1059/* ========== oblist implementation ========== */ 1098/* ========== oblist implementation ========== */
1060 1099
1100static pointer
1101generate_symbol (SCHEME_P_ const char *name)
1102{
1103 pointer x = mk_string (SCHEME_A_ name);
1104 setimmutable (x);
1105 set_typeflag (x, T_SYMBOL | T_ATOM);
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
1359INTERFACE void
1360vector_resize (pointer vec, uint32_t newsize, pointer fill)
1361{
1362 uint32_t oldsize = veclength (vec);
1363 vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer));
1364 veclength (vec) = newsize;
1365 fill_vector (vec, oldsize, fill);
1366}
1367
1309INTERFACE pointer 1368INTERFACE pointer
1310vector_elem (pointer vec, uint32_t ielem) 1369vector_get (pointer vec, uint32_t ielem)
1311{ 1370{
1312 return vecvalue(vec)[ielem]; 1371 return vecvalue(vec)[ielem];
1313} 1372}
1314 1373
1315INTERFACE void 1374INTERFACE void
1316set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1375vector_set (pointer vec, uint32_t ielem, pointer a)
1317{ 1376{
1318 vecvalue(vec)[ielem] = a; 1377 vecvalue(vec)[ielem] = a;
1319} 1378}
1320 1379
1321/* get new symbol */ 1380/* get new symbol */
1333 1392
1334INTERFACE pointer 1393INTERFACE pointer
1335gensym (SCHEME_P) 1394gensym (SCHEME_P)
1336{ 1395{
1337 pointer x; 1396 pointer x;
1338
1339 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1340 {
1341 char name[40] = "gensym-"; 1397 char name[40] = "gensym-";
1342 xnum (name + 7, SCHEME_V->gensym_cnt); 1398 xnum (name + 7, SCHEME_V->gensym_cnt);
1343 1399
1344 /* first check oblist */ 1400 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} 1401}
1356 1402
1357/* make symbol or number atom from string */ 1403/* make symbol or number atom from string */
1358static pointer 1404static pointer
1359mk_atom (SCHEME_P_ char *q) 1405mk_atom (SCHEME_P_ char *q)
1413 } 1459 }
1414 else if ((c == 'e') || (c == 'E')) 1460 else if ((c == 'e') || (c == 'E'))
1415 { 1461 {
1416 if (!has_fp_exp) 1462 if (!has_fp_exp)
1417 { 1463 {
1418 has_dec_point = 1; /* decimal point illegal 1464 has_dec_point = 1; /* decimal point illegal from now on */
1419 from now on */
1420 p++; 1465 p++;
1421 1466
1422 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1467 if ((*p == '-') || (*p == '+') || isdigit (*p))
1423 continue; 1468 continue;
1424 } 1469 }
1512 1557
1513 if (ecb_expect_false (is_vector (p))) 1558 if (ecb_expect_false (is_vector (p)))
1514 { 1559 {
1515 int i; 1560 int i;
1516 1561
1517 for (i = 0; i < p->object.vector.length; i++) 1562 for (i = 0; i < veclength (p); i++)
1518 mark (vecvalue (p)[i]); 1563 mark (vecvalue (p)[i]);
1519 } 1564 }
1520 1565
1521 if (is_atom (p)) 1566 if (is_atom (p))
1522 goto E6; 1567 goto E6;
1620 if (is_mark (p)) 1665 if (is_mark (p))
1621 clrmark (p); 1666 clrmark (p);
1622 else 1667 else
1623 { 1668 {
1624 /* reclaim cell */ 1669 /* reclaim cell */
1625 if (typeflag (p) != T_FREE) 1670 if (typeflag (p) != T_PAIR)
1626 { 1671 {
1627 finalize_cell (SCHEME_A_ p); 1672 finalize_cell (SCHEME_A_ p);
1628 set_typeflag (p, T_FREE); 1673 set_typeflag (p, T_PAIR);
1629 set_car (p, NIL); 1674 set_car (p, NIL);
1630 } 1675 }
1631 1676
1632 ++SCHEME_V->fcells; 1677 ++SCHEME_V->fcells;
1633 set_cdr (p, SCHEME_V->free_cell); 1678 set_cdr (p, SCHEME_V->free_cell);
1635 } 1680 }
1636 } 1681 }
1637 } 1682 }
1638 1683
1639 if (SCHEME_V->gc_verbose) 1684 if (SCHEME_V->gc_verbose)
1685 {
1640 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1686 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1687 }
1641} 1688}
1642 1689
1643static void 1690static void
1644finalize_cell (SCHEME_P_ pointer a) 1691finalize_cell (SCHEME_P_ pointer a)
1645{ 1692{
1646 /* TODO, fast bitmap check? */ 1693 /* TODO, fast bitmap check? */
1647 if (is_string (a)) 1694 if (is_string (a) || is_symbol (a))
1648 free (strvalue (a)); 1695 free (strvalue (a));
1649 else if (is_vector (a)) 1696 else if (is_vector (a))
1650 free (vecvalue (a)); 1697 free (vecvalue (a));
1651#if USE_PORTS 1698#if USE_PORTS
1652 else if (is_port (a)) 1699 else if (is_port (a))
2074#endif 2121#endif
2075} 2122}
2076 2123
2077/* read characters up to delimiter, but cater to character constants */ 2124/* read characters up to delimiter, but cater to character constants */
2078static char * 2125static char *
2079readstr_upto (SCHEME_P_ char *delim) 2126readstr_upto (SCHEME_P_ int skip, const char *delim)
2080{ 2127{
2081 char *p = SCHEME_V->strbuff; 2128 char *p = SCHEME_V->strbuff + skip;
2082 2129
2083 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2130 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2084 2131
2085 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2132 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2086 *p = 0; 2133 *p = 0;
2093 return SCHEME_V->strbuff; 2140 return SCHEME_V->strbuff;
2094} 2141}
2095 2142
2096/* read string expression "xxx...xxx" */ 2143/* read string expression "xxx...xxx" */
2097static pointer 2144static pointer
2098readstrexp (SCHEME_P) 2145readstrexp (SCHEME_P_ char delim)
2099{ 2146{
2100 char *p = SCHEME_V->strbuff; 2147 char *p = SCHEME_V->strbuff;
2101 int c; 2148 int c;
2102 int c1 = 0; 2149 int c1 = 0;
2103 enum
2104 { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; 2150 enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok;
2105 2151
2106 for (;;) 2152 for (;;)
2107 { 2153 {
2108 c = inchar (SCHEME_A); 2154 c = inchar (SCHEME_A);
2109 2155
2111 return S_F; 2157 return S_F;
2112 2158
2113 switch (state) 2159 switch (state)
2114 { 2160 {
2115 case st_ok: 2161 case st_ok:
2116 switch (c) 2162 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); 2163 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff);
2125 2164
2126 default: 2165 if (ecb_expect_false (c == '\\'))
2166 state = st_bsl;
2167 else
2127 *p++ = c; 2168 *p++ = c;
2128 break;
2129 }
2130 2169
2131 break; 2170 break;
2132 2171
2133 case st_bsl: 2172 case st_bsl:
2134 switch (c) 2173 switch (c)
2164 case 'r': 2203 case 'r':
2165 *p++ = '\r'; 2204 *p++ = '\r';
2166 state = st_ok; 2205 state = st_ok;
2167 break; 2206 break;
2168 2207
2169 case '"':
2170 *p++ = '"';
2171 state = st_ok;
2172 break;
2173
2174 default: 2208 default:
2175 *p++ = c; 2209 *p++ = c;
2176 state = st_ok; 2210 state = st_ok;
2177 break; 2211 break;
2178 } 2212 }
2179 2213
2180 break; 2214 break;
2181 2215
2182 case st_x1: 2216 case st_x1:
2183 case st_x2: 2217 case st_x2:
2184 c = toupper (c); 2218 c = tolower (c);
2185 2219
2186 if (c >= '0' && c <= 'F') 2220 if (c >= '0' && c <= '9')
2187 {
2188 if (c <= '9')
2189 c1 = (c1 << 4) + c - '0'; 2221 c1 = (c1 << 4) + c - '0';
2190 else 2222 else if (c >= 'a' && c <= 'f')
2191 c1 = (c1 << 4) + c - 'A' + 10; 2223 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 2224 else
2202 return S_F; 2225 return S_F;
2226
2227 if (state == st_x1)
2228 state = st_x2;
2229 else
2230 {
2231 *p++ = c1;
2232 state = st_ok;
2233 }
2203 2234
2204 break; 2235 break;
2205 2236
2206 case st_oct1: 2237 case st_oct1:
2207 case st_oct2: 2238 case st_oct2:
2211 backchar (SCHEME_A_ c); 2242 backchar (SCHEME_A_ c);
2212 state = st_ok; 2243 state = st_ok;
2213 } 2244 }
2214 else 2245 else
2215 { 2246 {
2216 if (state == st_oct2 && c1 >= 32) 2247 if (state == st_oct2 && c1 >= ' ')
2217 return S_F; 2248 return S_F;
2218 2249
2219 c1 = (c1 << 3) + (c - '0'); 2250 c1 = (c1 << 3) + (c - '0');
2220 2251
2221 if (state == st_oct1) 2252 if (state == st_oct1)
2226 state = st_ok; 2257 state = st_ok;
2227 } 2258 }
2228 } 2259 }
2229 2260
2230 break; 2261 break;
2231
2232 } 2262 }
2233 } 2263 }
2234} 2264}
2235 2265
2236/* check c is in chars */ 2266/* check c is in chars */
2237ecb_inline int 2267ecb_inline int
2238is_one_of (char *s, int c) 2268is_one_of (const char *s, int c)
2239{ 2269{
2240 if (c == EOF)
2241 return 1;
2242
2243 return !!strchr (s, c); 2270 return c == EOF || !!strchr (s, c);
2244} 2271}
2245 2272
2246/* skip white characters */ 2273/* skip white characters */
2247ecb_inline int 2274ecb_inline int
2248skipspace (SCHEME_P) 2275skipspace (SCHEME_P)
2255#if SHOW_ERROR_LINE 2282#if SHOW_ERROR_LINE
2256 if (c == '\n') 2283 if (c == '\n')
2257 curr_line++; 2284 curr_line++;
2258#endif 2285#endif
2259 } 2286 }
2260 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2287 while (is_one_of (WHITESPACE, c));
2261 2288
2262 /* record it */ 2289 /* record it */
2263#if SHOW_ERROR_LINE 2290#if SHOW_ERROR_LINE
2264 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) 2291 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file)
2265 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; 2292 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line;
2295 return TOK_RPAREN; 2322 return TOK_RPAREN;
2296 2323
2297 case '.': 2324 case '.':
2298 c = inchar (SCHEME_A); 2325 c = inchar (SCHEME_A);
2299 2326
2300 if (is_one_of (" \n\t", c)) 2327 if (is_one_of (WHITESPACE, c))
2301 return TOK_DOT; 2328 return TOK_DOT;
2302 else 2329 else
2303 { 2330 {
2304 //TODO: ungetc twice in a row is not supported in C
2305 backchar (SCHEME_A_ c); 2331 backchar (SCHEME_A_ c);
2306 backchar (SCHEME_A_ '.');
2307 return TOK_ATOM; 2332 return TOK_DOTATOM;
2308 } 2333 }
2334
2335 case '|':
2336 return TOK_STRATOM;
2309 2337
2310 case '\'': 2338 case '\'':
2311 return TOK_QUOTE; 2339 return TOK_QUOTE;
2312 2340
2313 case ';': 2341 case ';':
2445 } 2473 }
2446 2474
2447 putcharacter (SCHEME_A_ '"'); 2475 putcharacter (SCHEME_A_ '"');
2448} 2476}
2449 2477
2450
2451/* print atoms */ 2478/* print atoms */
2452static void 2479static void
2453printatom (SCHEME_P_ pointer l, int f) 2480printatom (SCHEME_P_ pointer l, int f)
2454{ 2481{
2455 char *p; 2482 char *p;
2456 int len; 2483 int len;
2457 2484
2458 atom2str (SCHEME_A_ l, f, &p, &len); 2485 atom2str (SCHEME_A_ l, f, &p, &len);
2459 putchars (SCHEME_A_ p, len); 2486 putchars (SCHEME_A_ p, len);
2460} 2487}
2461
2462 2488
2463/* Uses internal buffer unless string pointer is already available */ 2489/* Uses internal buffer unless string pointer is already available */
2464static void 2490static void
2465atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2491atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2466{ 2492{
2480 { 2506 {
2481 p = SCHEME_V->strbuff; 2507 p = SCHEME_V->strbuff;
2482 2508
2483 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2509 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2484 { 2510 {
2485 if (num_is_integer (l)) 2511 if (is_integer (l))
2486 xnum (p, ivalue_unchecked (l)); 2512 xnum (p, ivalue_unchecked (l));
2487#if USE_REAL 2513#if USE_REAL
2488 else 2514 else
2489 { 2515 {
2490 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2516 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2627#endif 2653#endif
2628 } 2654 }
2629 else if (is_continuation (l)) 2655 else if (is_continuation (l))
2630 p = "#<CONTINUATION>"; 2656 p = "#<CONTINUATION>";
2631 else 2657 else
2658 {
2659#if USE_PRINTF
2660 p = SCHEME_V->strbuff;
2661 snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l));
2662#else
2632 p = "#<ERROR>"; 2663 p = "#<ERROR>";
2664#endif
2665 }
2633 2666
2634 *pp = p; 2667 *pp = p;
2635 *plen = strlen (p); 2668 *plen = strlen (p);
2636} 2669}
2637 2670
2745 return 0; 2778 return 0;
2746 } 2779 }
2747 else if (is_number (a)) 2780 else if (is_number (a))
2748 { 2781 {
2749 if (is_number (b)) 2782 if (is_number (b))
2750 if (num_is_integer (a) == num_is_integer (b))
2751 return num_cmp (nvalue (a), nvalue (b)) == 0; 2783 return num_cmp (nvalue (a), nvalue (b)) == 0;
2752 2784
2753 return 0; 2785 return 0;
2754 } 2786 }
2755 else if (is_character (a)) 2787 else if (is_character (a))
2756 { 2788 {
2782/* () is #t in R5RS */ 2814/* () is #t in R5RS */
2783#define is_true(p) ((p) != S_F) 2815#define is_true(p) ((p) != S_F)
2784#define is_false(p) ((p) == S_F) 2816#define is_false(p) ((p) == S_F)
2785 2817
2786/* ========== Environment implementation ========== */ 2818/* ========== 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 2819
2803#ifndef USE_ALIST_ENV 2820#ifndef USE_ALIST_ENV
2804 2821
2805/* 2822/*
2806 * In this implementation, each frame of the environment may be 2823 * In this implementation, each frame of the environment may be
2823 2840
2824 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2841 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2825 setenvironment (SCHEME_V->envir); 2842 setenvironment (SCHEME_V->envir);
2826} 2843}
2827 2844
2845static uint32_t
2846sym_hash (pointer sym, uint32_t size)
2847{
2848 uintptr_t ptr = (uintptr_t)sym;
2849
2850#if 0
2851 /* table size is prime, so why mix */
2852 ptr += ptr >> 32;
2853 ptr += ptr >> 16;
2854 ptr += ptr >> 8;
2855#endif
2856
2857 return ptr % size;
2858}
2859
2828ecb_inline void 2860ecb_inline void
2829new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2861new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2830{ 2862{
2831 pointer slot = immutable_cons (variable, value); 2863 pointer slot = immutable_cons (variable, value);
2832 2864
2833 if (is_vector (car (env))) 2865 if (is_vector (car (env)))
2834 { 2866 {
2835 int location = hash_fn (symname (variable), veclength (car (env))); 2867 int location = sym_hash (variable, veclength (car (env)));
2836
2837 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2868 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2838 } 2869 }
2839 else 2870 else
2840 set_car (env, immutable_cons (slot, car (env))); 2871 set_car (env, immutable_cons (slot, car (env)));
2841} 2872}
2842 2873
2843static pointer 2874static pointer
2844find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2875find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2845{ 2876{
2846 pointer x, y; 2877 pointer x, y;
2847 int location;
2848 2878
2849 for (x = env; x != NIL; x = cdr (x)) 2879 for (x = env; x != NIL; x = cdr (x))
2850 { 2880 {
2851 if (is_vector (car (x))) 2881 if (is_vector (car (x)))
2852 { 2882 {
2853 location = hash_fn (symname (hdl), veclength (car (x))); 2883 int location = sym_hash (hdl, veclength (car (x)));
2854 y = vector_elem (car (x), location); 2884 y = vector_get (car (x), location);
2855 } 2885 }
2856 else 2886 else
2857 y = car (x); 2887 y = car (x);
2858 2888
2859 for (; y != NIL; y = cdr (y)) 2889 for (; y != NIL; y = cdr (y))
2860 if (caar (y) == hdl) 2890 if (caar (y) == hdl)
2861 break; 2891 break;
2862 2892
2863 if (y != NIL) 2893 if (y != NIL)
2894 return car (y);
2895
2896 if (!all)
2864 break; 2897 break;
2865
2866 if (!all)
2867 return NIL;
2868 } 2898 }
2869
2870 if (x != NIL)
2871 return car (y);
2872 2899
2873 return NIL; 2900 return NIL;
2874} 2901}
2875 2902
2876#else /* USE_ALIST_ENV */ 2903#else /* USE_ALIST_ENV */
2898 for (y = car (x); y != NIL; y = cdr (y)) 2925 for (y = car (x); y != NIL; y = cdr (y))
2899 if (caar (y) == hdl) 2926 if (caar (y) == hdl)
2900 break; 2927 break;
2901 2928
2902 if (y != NIL) 2929 if (y != NIL)
2930 return car (y);
2903 break; 2931 break;
2904 2932
2905 if (!all) 2933 if (!all)
2906 return NIL; 2934 break;
2907 } 2935 }
2908
2909 if (x != NIL)
2910 return car (y);
2911 2936
2912 return NIL; 2937 return NIL;
2913} 2938}
2914 2939
2915#endif /* USE_ALIST_ENV else */ 2940#endif /* USE_ALIST_ENV else */
2916 2941
2917ecb_inline void 2942ecb_inline void
2918new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2943new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2919{ 2944{
2945 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2920 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2946 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2921} 2947}
2922 2948
2923ecb_inline void 2949ecb_inline void
2924set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2950set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
3132 int i = 0; 3158 int i = 0;
3133 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3159 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3134 3160
3135 while (cont != NIL) 3161 while (cont != NIL)
3136 { 3162 {
3137 frame->op = ivalue (car (cont)); cont = cdr (cont); 3163 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3138 frame->args = car (cont) ; cont = cdr (cont); 3164 frame->args = car (cont) ; cont = cdr (cont);
3139 frame->envir = car (cont) ; cont = cdr (cont); 3165 frame->envir = car (cont) ; cont = cdr (cont);
3140 frame->code = car (cont) ; cont = cdr (cont); 3166 frame->code = car (cont) ; cont = cdr (cont);
3141 3167
3142 ++frame; 3168 ++frame;
3143 ++i; 3169 ++i;
3144 } 3170 }
3145 3171
3174 SCHEME_V->value = a; 3200 SCHEME_V->value = a;
3175 3201
3176 if (dump == NIL) 3202 if (dump == NIL)
3177 return -1; 3203 return -1;
3178 3204
3179 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3205 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3180 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3206 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3181 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3207 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3182 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3208 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3183 3209
3184 SCHEME_V->dump = dump; 3210 SCHEME_V->dump = dump;
3185 3211
3186 return 0; 3212 return 0;
3187} 3213}
3216 3242
3217#endif 3243#endif
3218 3244
3219#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3245#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3220 3246
3247#if 1
3248static int
3249debug (SCHEME_P_ int indent, pointer x)
3250{
3251 int c;
3252
3253 if (is_syntax (x))
3254 {
3255 printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x));
3256 return 8 + 8;
3257 }
3258
3259 if (x == NIL)
3260 {
3261 printf ("%*sNIL\n", indent, "");
3262 return 3;
3263 }
3264
3265 switch (type (x))
3266 {
3267 case T_INTEGER:
3268 printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x);
3269 return 32+8;
3270
3271 case T_SYMBOL:
3272 printf ("%*sS<%s>\n", indent, "", symname (x));
3273 return 24+8;
3274
3275 case T_CLOSURE:
3276 printf ("%*sS<%s>\n", indent, "", "closure");
3277 debug (SCHEME_A_ indent + 3, cdr(x));
3278 return 32 + debug (SCHEME_A_ indent + 3, car (x));
3279
3280 case T_PAIR:
3281 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3282 c = debug (SCHEME_A_ indent + 3, car (x));
3283 c += debug (SCHEME_A_ indent + 3, cdr (x));
3284 return c + 1;
3285
3286 case T_PORT:
3287 printf ("%*sS<%s>\n", indent, "", "port");
3288 return 24+8;
3289
3290 case T_VECTOR:
3291 printf ("%*sS<%s>\n", indent, "", "vector");
3292 return 24+8;
3293
3294 case T_ENVIRONMENT:
3295 printf ("%*sS<%s>\n", indent, "", "environment");
3296 return 0 + debug (SCHEME_A_ indent + 3, car (x));
3297
3298 default:
3299 printf ("unhandled type %d\n", type (x));
3300 break;
3301 }
3302}
3303#endif
3304
3221static int 3305static int
3222opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3306opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3223{ 3307{
3224 pointer args = SCHEME_V->args; 3308 pointer args = SCHEME_V->args;
3225 pointer x, y; 3309 pointer x, y;
3226 3310
3227 switch (op) 3311 switch (op)
3228 { 3312 {
3313#if 1 //D
3314 case OP_DEBUG:
3315 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8);
3316 printf ("\n");
3317 s_return (S_T);
3318#endif
3229 case OP_LOAD: /* load */ 3319 case OP_LOAD: /* load */
3230 if (file_interactive (SCHEME_A)) 3320 if (file_interactive (SCHEME_A))
3231 { 3321 {
3232 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); 3322 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n");
3233 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); 3323 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args)));
3356 } 3446 }
3357 else 3447 else
3358 s_return (SCHEME_V->code); 3448 s_return (SCHEME_V->code);
3359 3449
3360 case OP_E0ARGS: /* eval arguments */ 3450 case OP_E0ARGS: /* eval arguments */
3361 if (is_macro (SCHEME_V->value)) /* macro expansion */ 3451 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3362 { 3452 {
3363 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3453 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3364 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3454 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3365 SCHEME_V->code = SCHEME_V->value; 3455 SCHEME_V->code = SCHEME_V->value;
3366 s_goto (OP_APPLY); 3456 s_goto (OP_APPLY);
3393 3483
3394 case OP_TRACING: 3484 case OP_TRACING:
3395 { 3485 {
3396 int tr = SCHEME_V->tracing; 3486 int tr = SCHEME_V->tracing;
3397 3487
3398 SCHEME_V->tracing = ivalue (car (args)); 3488 SCHEME_V->tracing = ivalue_unchecked (car (args));
3399 s_return (mk_integer (SCHEME_A_ tr)); 3489 s_return (mk_integer (SCHEME_A_ tr));
3400 } 3490 }
3401 3491
3402#endif 3492#endif
3403 3493
3912 SCHEME_V->code = car (args); 4002 SCHEME_V->code = car (args);
3913 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); 4003 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
3914 s_goto (OP_APPLY); 4004 s_goto (OP_APPLY);
3915 } 4005 }
3916 4006
3917 abort (); 4007 if (USE_ERROR_CHECKING) abort ();
3918} 4008}
3919 4009
3920static int 4010static int
3921opexe_1 (SCHEME_P_ enum scheme_opcodes op) 4011opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3922{ 4012{
3923 pointer args = SCHEME_V->args; 4013 pointer args = SCHEME_V->args;
3924 pointer x = car (args); 4014 pointer x = car (args);
3925 num v; 4015 num v;
3926 4016
3927#if USE_MATH
3928 RVALUE dd;
3929#endif
3930
3931 switch (op) 4017 switch (op)
3932 { 4018 {
3933#if USE_MATH 4019#if USE_MATH
3934 case OP_INEX2EX: /* inexact->exact */ 4020 case OP_INEX2EX: /* inexact->exact */
4021 {
3935 if (num_is_integer (x)) 4022 if (is_integer (x))
3936 s_return (x); 4023 s_return (x);
3937 else if (modf (rvalue_unchecked (x), &dd) == 0) 4024
4025 RVALUE r = rvalue_unchecked (x);
4026
4027 if (r == (RVALUE)(IVALUE)r)
3938 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4028 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3939 else 4029 else
3940 Error_1 ("inexact->exact: not integral:", x); 4030 Error_1 ("inexact->exact: not integral:", x);
4031 }
3941 4032
3942 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4033 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)))); 4034 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)))); 4035 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)))); 4036 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3963 { 4054 {
3964 RVALUE result; 4055 RVALUE result;
3965 int real_result = 1; 4056 int real_result = 1;
3966 pointer y = cadr (args); 4057 pointer y = cadr (args);
3967 4058
3968 if (num_is_integer (x) && num_is_integer (y)) 4059 if (is_integer (x) && is_integer (y))
3969 real_result = 0; 4060 real_result = 0;
3970 4061
3971 /* This 'if' is an R5RS compatibility fix. */ 4062 /* This 'if' is an R5RS compatibility fix. */
3972 /* NOTE: Remove this 'if' fix for R6RS. */ 4063 /* NOTE: Remove this 'if' fix for R6RS. */
3973 if (rvalue (x) == 0 && rvalue (y) < 0) 4064 if (rvalue (x) == 0 && rvalue (y) < 0)
3979 /* If the test fails, result is too big for integer. */ 4070 /* If the test fails, result is too big for integer. */
3980 if (!real_result) 4071 if (!real_result)
3981 { 4072 {
3982 long result_as_long = result; 4073 long result_as_long = result;
3983 4074
3984 if (result != (RVALUE) result_as_long) 4075 if (result != result_as_long)
3985 real_result = 1; 4076 real_result = 1;
3986 } 4077 }
3987 4078
3988 if (real_result) 4079 if (real_result)
3989 s_return (mk_real (SCHEME_A_ result)); 4080 s_return (mk_real (SCHEME_A_ result));
3994 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4085 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)))); 4086 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
3996 4087
3997 case OP_TRUNCATE: 4088 case OP_TRUNCATE:
3998 { 4089 {
3999 RVALUE rvalue_of_x; 4090 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))); 4091 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 } 4092 }
4008 4093
4009 case OP_ROUND: 4094 case OP_ROUND:
4010 if (num_is_integer (x)) 4095 if (is_integer (x))
4011 s_return (x); 4096 s_return (x);
4012 4097
4013 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4098 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4014#endif 4099#endif
4015 4100
4135 } 4220 }
4136 else 4221 else
4137 Error_0 ("set-cdr!: unable to alter immutable pair"); 4222 Error_0 ("set-cdr!: unable to alter immutable pair");
4138 4223
4139 case OP_CHAR2INT: /* char->integer */ 4224 case OP_CHAR2INT: /* char->integer */
4140 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4225 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4141 4226
4142 case OP_INT2CHAR: /* integer->char */ 4227 case OP_INT2CHAR: /* integer->char */
4143 s_return (mk_character (SCHEME_A_ ivalue (x))); 4228 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4144 4229
4145 case OP_CHARUPCASE: 4230 case OP_CHARUPCASE:
4146 { 4231 {
4147 unsigned char c = ivalue (x); 4232 unsigned char c = ivalue_unchecked (x);
4148 c = toupper (c); 4233 c = toupper (c);
4149 s_return (mk_character (SCHEME_A_ c)); 4234 s_return (mk_character (SCHEME_A_ c));
4150 } 4235 }
4151 4236
4152 case OP_CHARDNCASE: 4237 case OP_CHARDNCASE:
4153 { 4238 {
4154 unsigned char c = ivalue (x); 4239 unsigned char c = ivalue_unchecked (x);
4155 c = tolower (c); 4240 c = tolower (c);
4156 s_return (mk_character (SCHEME_A_ c)); 4241 s_return (mk_character (SCHEME_A_ c));
4157 } 4242 }
4158 4243
4159 case OP_STR2SYM: /* string->symbol */ 4244 case OP_STR2SYM: /* string->symbol */
4236 Error_1 ("atom->string: not an atom:", x); 4321 Error_1 ("atom->string: not an atom:", x);
4237 } 4322 }
4238 4323
4239 case OP_MKSTRING: /* make-string */ 4324 case OP_MKSTRING: /* make-string */
4240 { 4325 {
4241 int fill = ' '; 4326 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4242 int len;
4243
4244 len = ivalue (x); 4327 int len = ivalue_unchecked (x);
4245
4246 if (cdr (args) != NIL)
4247 fill = charvalue (cadr (args));
4248 4328
4249 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4329 s_return (mk_empty_string (SCHEME_A_ len, fill));
4250 } 4330 }
4251 4331
4252 case OP_STRLEN: /* string-length */ 4332 case OP_STRLEN: /* string-length */
4253 s_return (mk_integer (SCHEME_A_ strlength (x))); 4333 s_return (mk_integer (SCHEME_A_ strlength (x)));
4254 4334
4255 case OP_STRREF: /* string-ref */ 4335 case OP_STRREF: /* string-ref */
4256 { 4336 {
4257 char *str;
4258 int index;
4259
4260 str = strvalue (x); 4337 char *str = strvalue (x);
4261
4262 index = ivalue (cadr (args)); 4338 int index = ivalue_unchecked (cadr (args));
4263 4339
4264 if (index >= strlength (x)) 4340 if (index >= strlength (x))
4265 Error_1 ("string-ref: out of bounds:", cadr (args)); 4341 Error_1 ("string-ref: out of bounds:", cadr (args));
4266 4342
4267 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4343 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4268 } 4344 }
4269 4345
4270 case OP_STRSET: /* string-set! */ 4346 case OP_STRSET: /* string-set! */
4271 { 4347 {
4272 char *str; 4348 char *str = strvalue (x);
4273 int index; 4349 int index = ivalue_unchecked (cadr (args));
4274 int c; 4350 int c;
4275 4351
4276 if (is_immutable (x)) 4352 if (is_immutable (x))
4277 Error_1 ("string-set!: unable to alter immutable string:", x); 4353 Error_1 ("string-set!: unable to alter immutable string:", x);
4278
4279 str = strvalue (x);
4280
4281 index = ivalue (cadr (args));
4282 4354
4283 if (index >= strlength (x)) 4355 if (index >= strlength (x))
4284 Error_1 ("string-set!: out of bounds:", cadr (args)); 4356 Error_1 ("string-set!: out of bounds:", cadr (args));
4285 4357
4286 c = charvalue (caddr (args)); 4358 c = charvalue (caddr (args));
4309 s_return (newstr); 4381 s_return (newstr);
4310 } 4382 }
4311 4383
4312 case OP_SUBSTR: /* substring */ 4384 case OP_SUBSTR: /* substring */
4313 { 4385 {
4314 char *str; 4386 char *str = strvalue (x);
4315 int index0; 4387 int index0 = ivalue_unchecked (cadr (args));
4316 int index1; 4388 int index1;
4317 int len; 4389 int len;
4318 4390
4319 str = strvalue (x);
4320
4321 index0 = ivalue (cadr (args));
4322
4323 if (index0 > strlength (x)) 4391 if (index0 > strlength (x))
4324 Error_1 ("substring: start out of bounds:", cadr (args)); 4392 Error_1 ("substring: start out of bounds:", cadr (args));
4325 4393
4326 if (cddr (args) != NIL) 4394 if (cddr (args) != NIL)
4327 { 4395 {
4328 index1 = ivalue (caddr (args)); 4396 index1 = ivalue_unchecked (caddr (args));
4329 4397
4330 if (index1 > strlength (x) || index1 < index0) 4398 if (index1 > strlength (x) || index1 < index0)
4331 Error_1 ("substring: end out of bounds:", caddr (args)); 4399 Error_1 ("substring: end out of bounds:", caddr (args));
4332 } 4400 }
4333 else 4401 else
4356 if (SCHEME_V->no_memory) 4424 if (SCHEME_V->no_memory)
4357 s_return (S_SINK); 4425 s_return (S_SINK);
4358#endif 4426#endif
4359 4427
4360 for (x = args, i = 0; is_pair (x); x = cdr (x), i++) 4428 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4361 set_vector_elem (vec, i, car (x)); 4429 vector_set (vec, i, car (x));
4362 4430
4363 s_return (vec); 4431 s_return (vec);
4364 } 4432 }
4365 4433
4366 case OP_MKVECTOR: /* make-vector */ 4434 case OP_MKVECTOR: /* make-vector */
4367 { 4435 {
4368 pointer fill = NIL; 4436 pointer fill = NIL;
4369 int len;
4370 pointer vec; 4437 pointer vec;
4371
4372 len = ivalue (x); 4438 int len = ivalue_unchecked (x);
4373 4439
4374 if (cdr (args) != NIL) 4440 if (cdr (args) != NIL)
4375 fill = cadr (args); 4441 fill = cadr (args);
4376 4442
4377 vec = mk_vector (SCHEME_A_ len); 4443 vec = mk_vector (SCHEME_A_ len);
4380 if (SCHEME_V->no_memory) 4446 if (SCHEME_V->no_memory)
4381 s_return (S_SINK); 4447 s_return (S_SINK);
4382#endif 4448#endif
4383 4449
4384 if (fill != NIL) 4450 if (fill != NIL)
4385 fill_vector (vec, fill); 4451 fill_vector (vec, 0, fill);
4386 4452
4387 s_return (vec); 4453 s_return (vec);
4388 } 4454 }
4389 4455
4390 case OP_VECLEN: /* vector-length */ 4456 case OP_VECLEN: /* vector-length */
4391 s_return (mk_integer (SCHEME_A_ veclength (x))); 4457 s_return (mk_integer (SCHEME_A_ veclength (x)));
4392 4458
4459 case OP_VECRESIZE:
4460 vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args));
4461 s_return (x);
4462
4393 case OP_VECREF: /* vector-ref */ 4463 case OP_VECREF: /* vector-ref */
4394 { 4464 {
4395 int index;
4396
4397 index = ivalue (cadr (args)); 4465 int index = ivalue_unchecked (cadr (args));
4398 4466
4399 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4467 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4400 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4468 Error_1 ("vector-ref: out of bounds:", cadr (args));
4401 4469
4402 s_return (vector_elem (x, index)); 4470 s_return (vector_get (x, index));
4403 } 4471 }
4404 4472
4405 case OP_VECSET: /* vector-set! */ 4473 case OP_VECSET: /* vector-set! */
4406 { 4474 {
4407 int index; 4475 int index = ivalue_unchecked (cadr (args));
4408 4476
4409 if (is_immutable (x)) 4477 if (is_immutable (x))
4410 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4478 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4411 4479
4412 index = ivalue (cadr (args));
4413
4414 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4480 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4415 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4481 Error_1 ("vector-set!: out of bounds:", cadr (args));
4416 4482
4417 set_vector_elem (x, index, caddr (args)); 4483 vector_set (x, index, caddr (args));
4418 s_return (x); 4484 s_return (x);
4419 } 4485 }
4420 } 4486 }
4421 4487
4422 abort (); 4488 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} 4489}
4478 4490
4479static int 4491static int
4480opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4492opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4481{ 4493{
4527 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4539 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4528 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4540 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4529 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4541 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4530 4542
4531#if USE_CHAR_CLASSIFIERS 4543#if USE_CHAR_CLASSIFIERS
4532 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4544 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4533 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4545 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4534 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4546 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4535 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4547 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4536 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4548 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4537#endif 4549#endif
4538 4550
4539#if USE_PORTS 4551#if USE_PORTS
4540 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4552 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4541 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4553 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4742 4754
4743 case OP_NEWSEGMENT: /* new-segment */ 4755 case OP_NEWSEGMENT: /* new-segment */
4744 if (!is_pair (args) || !is_number (a)) 4756 if (!is_pair (args) || !is_number (a))
4745 Error_0 ("new-segment: argument must be a number"); 4757 Error_0 ("new-segment: argument must be a number");
4746 4758
4747 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 4759 alloc_cellseg (SCHEME_A_ ivalue (a));
4748 4760
4749 s_return (S_T); 4761 s_return (S_T);
4750 4762
4751 case OP_OBLIST: /* oblist */ 4763 case OP_OBLIST: /* oblist */
4752 s_return (oblist_all_symbols (SCHEME_A)); 4764 s_return (oblist_all_symbols (SCHEME_A));
4868 case OP_CURR_ENV: /* current-environment */ 4880 case OP_CURR_ENV: /* current-environment */
4869 s_return (SCHEME_V->envir); 4881 s_return (SCHEME_V->envir);
4870 4882
4871 } 4883 }
4872 4884
4873 abort (); 4885 if (USE_ERROR_CHECKING) abort ();
4874} 4886}
4875 4887
4876static int 4888static int
4877opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4889opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4878{ 4890{
5010 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 5022 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
5011 SCHEME_V->tok = token (SCHEME_A); 5023 SCHEME_V->tok = token (SCHEME_A);
5012 s_goto (OP_RDSEXPR); 5024 s_goto (OP_RDSEXPR);
5013 5025
5014 case TOK_ATOM: 5026 case TOK_ATOM:
5015 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); 5027 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS)));
5028
5029 case TOK_DOTATOM:
5030 SCHEME_V->strbuff[0] = '.';
5031 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5032
5033 case TOK_STRATOM:
5034 x = readstrexp (SCHEME_A_ '|');
5035 //TODO: haven't checked whether the garbage collector could interfere
5036 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5016 5037
5017 case TOK_DQUOTE: 5038 case TOK_DQUOTE:
5018 x = readstrexp (SCHEME_A); 5039 x = readstrexp (SCHEME_A_ '"');
5019 5040
5020 if (x == S_F) 5041 if (x == S_F)
5021 Error_0 ("Error reading string"); 5042 Error_0 ("Error reading string");
5022 5043
5023 setimmutable (x); 5044 setimmutable (x);
5035 s_goto (OP_EVAL); 5056 s_goto (OP_EVAL);
5036 } 5057 }
5037 } 5058 }
5038 5059
5039 case TOK_SHARP_CONST: 5060 case TOK_SHARP_CONST:
5040 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) 5061 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5041 Error_0 ("undefined sharp expression"); 5062 Error_0 ("undefined sharp expression");
5042 else 5063 else
5043 s_return (x); 5064 s_return (x);
5044 5065
5045 default: 5066 default:
5197 putstr (SCHEME_A_ ")"); 5218 putstr (SCHEME_A_ ")");
5198 s_return (S_T); 5219 s_return (S_T);
5199 } 5220 }
5200 else 5221 else
5201 { 5222 {
5202 pointer elem = vector_elem (vec, i); 5223 pointer elem = vector_get (vec, i);
5203 5224
5204 ivalue_unchecked (cdr (args)) = i + 1; 5225 ivalue_unchecked (cdr (args)) = i + 1;
5205 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5226 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5206 SCHEME_V->args = elem; 5227 SCHEME_V->args = elem;
5207 5228
5211 s_goto (OP_P0LIST); 5232 s_goto (OP_P0LIST);
5212 } 5233 }
5213 } 5234 }
5214 } 5235 }
5215 5236
5216 abort (); 5237 if (USE_ERROR_CHECKING) abort ();
5217} 5238}
5218 5239
5219static int 5240static int
5220opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5241opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5221{ 5242{
5267 5288
5268 case OP_CLOSUREP: /* closure? */ 5289 case OP_CLOSUREP: /* closure? */
5269 /* 5290 /*
5270 * Note, macro object is also a closure. 5291 * Note, macro object is also a closure.
5271 * Therefore, (closure? <#MACRO>) ==> #t 5292 * Therefore, (closure? <#MACRO>) ==> #t
5293 * (schmorp) well, obviously not, fix? TODO
5272 */ 5294 */
5273 s_retbool (is_closure (a)); 5295 s_retbool (is_closure (a));
5274 5296
5275 case OP_MACROP: /* macro? */ 5297 case OP_MACROP: /* macro? */
5276 s_retbool (is_macro (a)); 5298 s_retbool (is_macro (a));
5277 } 5299 }
5278 5300
5279 abort (); 5301 if (USE_ERROR_CHECKING) abort ();
5280} 5302}
5281 5303
5282/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5304/* 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); 5305typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5284 5306
5285typedef int (*test_predicate)(pointer); 5307typedef int (*test_predicate)(pointer);
5286static int 5308static int
5287is_any (pointer p) 5309tst_any (pointer p)
5288{ 5310{
5289 return 1; 5311 return 1;
5290} 5312}
5291 5313
5292static int 5314static int
5293is_nonneg (pointer p) 5315tst_inonneg (pointer p)
5294{ 5316{
5295 return ivalue (p) >= 0 && is_integer (p); 5317 return is_integer (p) && ivalue_unchecked (p) >= 0;
5296} 5318}
5297 5319
5298static int 5320static int
5299tst_is_list (pointer p) 5321tst_is_list (SCHEME_P_ pointer p)
5300{ 5322{
5301 return p == NIL || is_pair (p); 5323 return p == NIL || is_pair (p);
5302} 5324}
5303 5325
5304/* Correspond carefully with following defines! */ 5326/* Correspond carefully with following defines! */
5305static struct 5327static struct
5306{ 5328{
5307 test_predicate fct; 5329 test_predicate fct;
5308 const char *kind; 5330 const char *kind;
5309} tests[] = 5331} tests[] = {
5310{
5311 { is_any, 0 }, 5332 { tst_any , 0 },
5312 { is_string, "string" }, 5333 { is_string , "string" },
5313 { is_symbol, "symbol" }, 5334 { is_symbol , "symbol" },
5314 { is_port, "port" }, 5335 { is_port , "port" },
5315 { is_inport, "input port" }, 5336 { is_inport , "input port" },
5316 { is_outport, "output port" }, 5337 { is_outport , "output port" },
5317 { is_environment, "environment" }, 5338 { is_environment, "environment" },
5318 { is_pair, "pair" }, 5339 { is_pair , "pair" },
5319 { tst_is_list, "pair or '()" }, 5340 { 0 , "pair or '()" },
5320 { is_character, "character" }, 5341 { is_character , "character" },
5321 { is_vector, "vector" }, 5342 { is_vector , "vector" },
5322 { is_number, "number" }, 5343 { is_number , "number" },
5323 { is_integer, "integer" }, 5344 { is_integer , "integer" },
5324 { is_nonneg, "non-negative integer" } 5345 { tst_inonneg , "non-negative integer" }
5325}; 5346};
5326 5347
5327#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ 5348#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5328#define TST_ANY "\001" 5349#define TST_ANY "\001"
5329#define TST_STRING "\002" 5350#define TST_STRING "\002"
5370typedef struct 5391typedef struct
5371{ 5392{
5372 uint8_t func; 5393 uint8_t func;
5373 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ 5394 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5374 uint8_t builtin; 5395 uint8_t builtin;
5396#if USE_ERROR_CHECKING
5375 uint8_t min_arity; 5397 uint8_t min_arity;
5376 uint8_t max_arity; 5398 uint8_t max_arity;
5377 char arg_tests_encoding[3]; 5399 char arg_tests_encoding[3];
5400#endif
5378} op_code_info; 5401} op_code_info;
5379 5402
5380static const op_code_info dispatch_table[] = { 5403static const op_code_info dispatch_table[] = {
5404#if USE_ERROR_CHECKING
5381#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, 5405#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5406#else
5407#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5408#endif
5382#include "opdefines.h" 5409#include "opdefines.h"
5383#undef OP_DEF 5410#undef OP_DEF
5384 {0} 5411 {0}
5385}; 5412};
5386 5413
5428 { 5455 {
5429 pointer arg = car (arglist); 5456 pointer arg = car (arglist);
5430 5457
5431 j = t[0]; 5458 j = t[0];
5432 5459
5460 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5461 if (j == TST_LIST[0])
5462 {
5463 if (!tst_is_list (SCHEME_A_ arg))
5464 break;
5465 }
5466 else
5467 {
5433 if (!tests[j - 1].fct (arg)) 5468 if (!tests[j - 1].fct (arg))
5434 break; 5469 break;
5470 }
5435 5471
5436 if (t[1]) /* last test is replicated as necessary */ 5472 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5437 t++; 5473 t++;
5438 5474
5439 arglist = cdr (arglist); 5475 arglist = cdr (arglist);
5440 i++; 5476 i++;
5441 } 5477 }
5496mk_proc (SCHEME_P_ enum scheme_opcodes op) 5532mk_proc (SCHEME_P_ enum scheme_opcodes op)
5497{ 5533{
5498 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5534 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5499 set_typeflag (y, (T_PROC | T_ATOM)); 5535 set_typeflag (y, (T_PROC | T_ATOM));
5500 ivalue_unchecked (y) = op; 5536 ivalue_unchecked (y) = op;
5501 set_num_integer (y);
5502 return y; 5537 return y;
5503} 5538}
5504 5539
5505/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5540/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5506static int 5541static int
5507syntaxnum (pointer p) 5542syntaxnum (pointer p)
5508{ 5543{
5509 const char *s = strvalue (car (p)); 5544 const char *s = strvalue (p);
5510 5545
5511 switch (strlength (car (p))) 5546 switch (strlength (p))
5512 { 5547 {
5513 case 2: 5548 case 2:
5514 if (s[0] == 'i') 5549 if (s[0] == 'i')
5515 return OP_IF0; /* if */ 5550 return OP_IF0; /* if */
5516 else 5551 else
5952# endif 5987# endif
5953 int fin; 5988 int fin;
5954 char *file_name = InitFile; 5989 char *file_name = InitFile;
5955 int retcode; 5990 int retcode;
5956 int isfile = 1; 5991 int isfile = 1;
5992 system ("ps v $PPID");//D
5957 5993
5958 if (argc == 2 && strcmp (argv[1], "-?") == 0) 5994 if (argc == 2 && strcmp (argv[1], "-?") == 0)
5959 { 5995 {
5960 xwrstr ("Usage: tinyscheme -?\n"); 5996 xwrstr ("Usage: tinyscheme -?\n");
5961 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 5997 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines