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.1 by root, Wed Nov 25 05:02:56 2015 UTC vs.
Revision 1.4 by root, Wed Nov 25 10:49:29 2015 UTC

26#endif 26#endif
27 27
28#include <sys/types.h> 28#include <sys/types.h>
29#include <sys/stat.h> 29#include <sys/stat.h>
30#include <fcntl.h> 30#include <fcntl.h>
31
32#include <string.h>
33#include <stdlib.h>
31 34
32#include <limits.h> 35#include <limits.h>
33#include <inttypes.h> 36#include <inttypes.h>
34#include <float.h> 37#include <float.h>
35//#include <ctype.h> 38//#include <ctype.h>
52 55
53#define BACKQUOTE '`' 56#define BACKQUOTE '`'
54#define DELIMITERS "()\";\f\t\v\n\r " 57#define DELIMITERS "()\";\f\t\v\n\r "
55 58
56#define NIL (&SCHEME_V->xNIL) //TODO: make this 0? 59#define NIL (&SCHEME_V->xNIL) //TODO: make this 0?
57#define S_T (&SCHEME_V->xT) 60#define S_T (&SCHEME_V->xT) //TODO: magic ptr value?
58#define S_F (&SCHEME_V->xF) 61#define S_F (&SCHEME_V->xF) //TODO: magic ptr value?
59#define S_SINK (&SCHEME_V->xsink) 62#define S_SINK (&SCHEME_V->xsink)
60#define S_EOF (&SCHEME_V->xEOF_OBJ) 63#define S_EOF (&SCHEME_V->xEOF_OBJ)
61
62/*
63 * Basic memory allocation units
64 */
65
66#define banner "TinyScheme 1.41-s"
67
68#include <string.h>
69#include <stdlib.h>
70 64
71#if !USE_MULTIPLICITY 65#if !USE_MULTIPLICITY
72static scheme sc; 66static scheme sc;
73#endif 67#endif
74 68
169# define FIRST_CELLSEGS 3 163# define FIRST_CELLSEGS 3
170#endif 164#endif
171 165
172enum scheme_types 166enum scheme_types
173{ 167{
174 T_NULL, 168 T_FREE,
175 T_STRING, 169 T_STRING,
176 T_NUMBER, 170 T_NUMBER,
177 T_SYMBOL, 171 T_SYMBOL,
178 T_PROC, 172 T_PROC,
179 T_PAIR, 173 T_PAIR,
184 T_PORT, 178 T_PORT,
185 T_VECTOR, 179 T_VECTOR,
186 T_MACRO, 180 T_MACRO,
187 T_PROMISE, 181 T_PROMISE,
188 T_ENVIRONMENT, 182 T_ENVIRONMENT,
183 /* one more... */
189 T_NUM_SYSTEM_TYPES 184 T_NUM_SYSTEM_TYPES
190}; 185};
191 186
192#define T_MASKTYPE 31 /* 0000000000011111 */ 187#define T_MASKTYPE 0x000f
193#define T_SYNTAX 4096 /* 0001000000000000 */ 188#define T_SYNTAX 0x0010
194#define T_IMMUTABLE 8192 /* 0010000000000000 */ 189#define T_IMMUTABLE 0x0020
195#define T_ATOM 16384 /* 0100000000000000 */ /* only for gc */ 190#define T_ATOM 0x0040 /* only for gc */
196#define CLRATOM 49151 /* 1011111111111111 */ /* only for gc */ 191#define T_MARK 0x0080 /* only for gc */
197#define MARK 32768 /* 1000000000000000 */
198#define UNMARK 32767 /* 0111111111111111 */
199
200 192
201static num num_add (num a, num b); 193static num num_add (num a, num b);
202static num num_mul (num a, num b); 194static num num_mul (num a, num b);
203static num num_div (num a, num b); 195static num num_div (num a, num b);
204static num num_intdiv (num a, num b); 196static num num_intdiv (num a, num b);
245{ 237{
246 return type (p) == T_VECTOR; 238 return type (p) == T_VECTOR;
247} 239}
248 240
249INTERFACE void fill_vector (pointer vec, pointer obj); 241INTERFACE void fill_vector (pointer vec, pointer obj);
242INTERFACE uint32_t vector_length (pointer vec);
250INTERFACE pointer vector_elem (pointer vec, int ielem); 243INTERFACE pointer vector_elem (pointer vec, uint32_t ielem);
251INTERFACE void set_vector_elem (pointer vec, int ielem, pointer a); 244INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a);
245
246INTERFACE uint32_t
247vector_length (pointer vec)
248{
249 return vec->object.vector.length;
250}
252 251
253INTERFACE INLINE int 252INTERFACE INLINE int
254is_number (pointer p) 253is_number (pointer p)
255{ 254{
256 return type (p) == T_NUMBER; 255 return type (p) == T_NUMBER;
290nvalue (pointer p) 289nvalue (pointer p)
291{ 290{
292 return (p)->object.number; 291 return (p)->object.number;
293} 292}
294 293
295INTERFACE long 294static IVALUE
295num_get_ivalue (const num n)
296{
297 return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n);
298}
299
300static RVALUE
301num_get_rvalue (const num n)
302{
303 return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n);
304}
305
306INTERFACE IVALUE
296ivalue (pointer p) 307ivalue (pointer p)
297{ 308{
298 return num_is_integer (p) ? num_ivalue ((p)->object.number) : (long) num_rvalue ((p)->object.number); 309 return num_get_ivalue (p->object.number);
299} 310}
300 311
301INTERFACE RVALUE 312INTERFACE RVALUE
302rvalue (pointer p) 313rvalue (pointer p)
303{ 314{
304 return num_is_integer (p) ? (RVALUE) num_ivalue ((p)->object.number) : num_rvalue ((p)->object.number); 315 return num_get_rvalue (p->object.number);
305} 316}
306 317
307#define ivalue_unchecked(p) ((p)->object.number.value.ivalue) 318#define ivalue_unchecked(p) ((p)->object.number.value.ivalue)
308#if USE_FLOAT 319#if USE_FLOAT
309# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 320# define rvalue_unchecked(p) ((p)->object.number.value.rvalue)
424syntaxname (pointer p) 435syntaxname (pointer p)
425{ 436{
426 return strvalue (car (p)); 437 return strvalue (car (p));
427} 438}
428 439
429#define procnum(p) ivalue(p) 440#define procnum(p) ivalue (p)
430static const char *procname (pointer x); 441static const char *procname (pointer x);
431 442
432INTERFACE INLINE int 443INTERFACE INLINE int
433is_closure (pointer p) 444is_closure (pointer p)
434{ 445{
475 return type (p) == T_ENVIRONMENT; 486 return type (p) == T_ENVIRONMENT;
476} 487}
477 488
478#define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT) 489#define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT)
479 490
491#define is_atom1(p) (TYPESET_ATOM & (1U << type (p)))
480#define is_atom(p) (typeflag (p) & T_ATOM) 492#define is_atom(p) (typeflag (p) & T_ATOM)
481#define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) 493#define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM)
482#define clratom(p) set_typeflag ((p), typeflag (p) & CLRATOM) 494#define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM)
483 495
484#define is_mark(p) (typeflag (p) & MARK) 496#define is_mark(p) (typeflag (p) & T_MARK)
485#define setmark(p) set_typeflag ((p), typeflag (p) | MARK) 497#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
486#define clrmark(p) set_typeflag ((p), typeflag (p) & UNMARK) 498#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
499
500#if 0
501static int
502is_atom(pointer p)
503{
504 if (!is_atom1(p) != !is_atom2(p))
505 printf ("atoms disagree %x\n", typeflag(p));
506
507 return is_atom2(p);
508}
509#endif
487 510
488INTERFACE INLINE int 511INTERFACE INLINE int
489is_immutable (pointer p) 512is_immutable (pointer p)
490{ 513{
491 return typeflag (p) & T_IMMUTABLE; 514 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
492} 515}
493 516
494INTERFACE INLINE void 517INTERFACE INLINE void
495setimmutable (pointer p) 518setimmutable (pointer p)
496{ 519{
520#if USE_ERROR_CHECKING
497 set_typeflag (p, T_IMMUTABLE); 521 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
522#endif
498} 523}
499 524
500#if USE_CHAR_CLASSIFIERS 525#if USE_CHAR_CLASSIFIERS
501static INLINE int 526static INLINE int
502Cisalpha (int c) 527Cisalpha (int c)
595static int file_interactive (SCHEME_P); 620static int file_interactive (SCHEME_P);
596static INLINE int is_one_of (char *s, int c); 621static INLINE int is_one_of (char *s, int c);
597static int alloc_cellseg (SCHEME_P_ int n); 622static int alloc_cellseg (SCHEME_P_ int n);
598static long binary_decode (const char *s); 623static long binary_decode (const char *s);
599static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); 624static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b);
600static pointer xget_cell (SCHEME_P_ pointer a, pointer b);
601static pointer reserve_cells (SCHEME_P_ int n);
602static pointer get_consecutive_cells (SCHEME_P_ int n);
603static pointer find_consecutive_cells (SCHEME_P_ int n);
604static void finalize_cell (SCHEME_P_ pointer a); 625static void finalize_cell (SCHEME_P_ pointer a);
605static int count_consecutive_cells (pointer x, int needed); 626static int count_consecutive_cells (pointer x, int needed);
606static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 627static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
607static pointer mk_number (SCHEME_P_ num n); 628static pointer mk_number (SCHEME_P_ const num n);
608static char *store_string (SCHEME_P_ int len, const char *str, char fill); 629static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
609static pointer mk_vector (SCHEME_P_ int len); 630static pointer mk_vector (SCHEME_P_ uint32_t len);
610static pointer mk_atom (SCHEME_P_ char *q); 631static pointer mk_atom (SCHEME_P_ char *q);
611static pointer mk_sharp_const (SCHEME_P_ char *name); 632static pointer mk_sharp_const (SCHEME_P_ char *name);
612 633
613#if USE_PORTS 634#if USE_PORTS
614static pointer mk_port (SCHEME_P_ port *p); 635static pointer mk_port (SCHEME_P_ port *p);
646static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); 667static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op);
647static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); 668static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op);
648static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); 669static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op);
649static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); 670static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op);
650static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 671static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
651static void assign_syntax (SCHEME_P_ char *name); 672static void assign_syntax (SCHEME_P_ const char *name);
652static int syntaxnum (pointer p); 673static int syntaxnum (pointer p);
653static void assign_proc (SCHEME_P_ enum scheme_opcodes, char *name); 674static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
654 675
655static num 676static num
656num_add (num a, num b) 677num_add (num a, num b)
657{ 678{
658 num ret; 679 num ret;
659 680
660 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 681 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
661 682
662 if (num_is_fixnum (ret)) 683 if (num_is_fixnum (ret))
663 num_set_ivalue (ret, a.value.ivalue + b.value.ivalue); 684 num_set_ivalue (ret, num_get_ivalue (a) + num_get_ivalue (b));
664 else 685 else
665 num_set_rvalue (ret, num_rvalue (a) + num_rvalue (b)); 686 num_set_rvalue (ret, num_get_rvalue (a) + num_get_rvalue (b));
666 687
667 return ret; 688 return ret;
668} 689}
669 690
670static num 691static num
673 num ret; 694 num ret;
674 695
675 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 696 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
676 697
677 if (num_is_fixnum (ret)) 698 if (num_is_fixnum (ret))
678 num_set_ivalue (ret, a.value.ivalue * b.value.ivalue); 699 num_set_ivalue (ret, num_get_ivalue (a) * num_get_ivalue (b));
679 else 700 else
680 num_set_rvalue (ret, num_rvalue (a) * num_rvalue (b)); 701 num_set_rvalue (ret, num_get_rvalue (a) * num_get_rvalue (b));
681 702
682 return ret; 703 return ret;
683} 704}
684 705
685static num 706static num
686num_div (num a, num b) 707num_div (num a, num b)
687{ 708{
688 num ret; 709 num ret;
689 710
690 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && a.value.ivalue % b.value.ivalue == 0); 711 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0);
691 712
692 if (num_is_fixnum (ret)) 713 if (num_is_fixnum (ret))
693 num_set_ivalue (ret, a.value.ivalue / b.value.ivalue); 714 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b));
694 else 715 else
695 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); 716 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b));
696 717
697 return ret; 718 return ret;
698} 719}
699 720
700static num 721static num
703 num ret; 724 num ret;
704 725
705 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 726 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
706 727
707 if (num_is_fixnum (ret)) 728 if (num_is_fixnum (ret))
708 num_set_ivalue (ret, a.value.ivalue / b.value.ivalue); 729 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b));
709 else 730 else
710 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); 731 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b));
711 732
712 return ret; 733 return ret;
713} 734}
714 735
715static num 736static num
718 num ret; 739 num ret;
719 740
720 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 741 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
721 742
722 if (num_is_fixnum (ret)) 743 if (num_is_fixnum (ret))
723 num_set_ivalue (ret, a.value.ivalue - b.value.ivalue); 744 num_set_ivalue (ret, num_get_ivalue (a) - num_get_ivalue (b));
724 else 745 else
725 num_set_rvalue (ret, num_rvalue (a) - num_rvalue (b)); 746 num_set_rvalue (ret, num_get_rvalue (a) - num_get_rvalue (b));
726 747
727 return ret; 748 return ret;
728} 749}
729 750
730static num 751static num
732{ 753{
733 num ret; 754 num ret;
734 long e1, e2, res; 755 long e1, e2, res;
735 756
736 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 757 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
737 e1 = num_ivalue (a); 758 e1 = num_get_ivalue (a);
738 e2 = num_ivalue (b); 759 e2 = num_get_ivalue (b);
739 res = e1 % e2; 760 res = e1 % e2;
740 761
741 /* remainder should have same sign as second operand */ 762 /* remainder should have same sign as second operand */
742 if (res > 0) 763 if (res > 0)
743 { 764 {
759{ 780{
760 num ret; 781 num ret;
761 long e1, e2, res; 782 long e1, e2, res;
762 783
763 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 784 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
764 e1 = num_ivalue (a); 785 e1 = num_get_ivalue (a);
765 e2 = num_ivalue (b); 786 e2 = num_get_ivalue (b);
766 res = e1 % e2; 787 res = e1 % e2;
767 788
768 /* modulo should have same sign as second operand */ 789 /* modulo should have same sign as second operand */
769 if (res * e2 < 0) 790 if (res * e2 < 0)
770 res += e2; 791 res += e2;
778{ 799{
779 int ret; 800 int ret;
780 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 801 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
781 802
782 if (is_fixnum) 803 if (is_fixnum)
783 ret = a.value.ivalue == b.value.ivalue; 804 ret = num_get_ivalue (a) == num_get_ivalue (b);
784 else 805 else
785 ret = num_rvalue (a) == num_rvalue (b); 806 ret = num_get_rvalue (a) == num_get_rvalue (b);
786 807
787 return ret; 808 return ret;
788} 809}
789 810
790 811
793{ 814{
794 int ret; 815 int ret;
795 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 816 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
796 817
797 if (is_fixnum) 818 if (is_fixnum)
798 ret = a.value.ivalue > b.value.ivalue; 819 ret = num_get_ivalue (a) > num_get_ivalue (b);
799 else 820 else
800 ret = num_rvalue (a) > num_rvalue (b); 821 ret = num_get_rvalue (a) > num_get_rvalue (b);
801 822
802 return ret; 823 return ret;
803} 824}
804 825
805static int 826static int
813{ 834{
814 int ret; 835 int ret;
815 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 836 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
816 837
817 if (is_fixnum) 838 if (is_fixnum)
818 ret = a.value.ivalue < b.value.ivalue; 839 ret = num_get_ivalue (a) < num_get_ivalue (b);
819 else 840 else
820 ret = num_rvalue (a) < num_rvalue (b); 841 ret = num_get_rvalue (a) < num_get_rvalue (b);
821 842
822 return ret; 843 return ret;
823} 844}
824 845
825static int 846static int
927 SCHEME_V->fcells += segsize; 948 SCHEME_V->fcells += segsize;
928 last = newp + segsize - 1; 949 last = newp + segsize - 1;
929 950
930 for (p = newp; p <= last; p++) 951 for (p = newp; p <= last; p++)
931 { 952 {
932 set_typeflag (p, 0); 953 set_typeflag (p, T_FREE);
954 set_car (p, NIL);
933 set_cdr (p, p + 1); 955 set_cdr (p, p + 1);
934 set_car (p, NIL);
935 } 956 }
936 957
937 /* insert new cells in address order on free list */ 958 /* insert new cells in address order on free list */
938 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) 959 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell)
939 { 960 {
953 } 974 }
954 975
955 return n; 976 return n;
956} 977}
957 978
958/* get new cell. parameter a, b is marked by gc. */ 979/* get new cell. parameter a, b is marked by gc. */
959static INLINE pointer 980static INLINE pointer
960get_cell_x (SCHEME_P_ pointer a, pointer b) 981get_cell_x (SCHEME_P_ pointer a, pointer b)
961{ 982{
962 if (SCHEME_V->free_cell == NIL) 983 if (SCHEME_V->free_cell == NIL)
963 { 984 {
991 --SCHEME_V->fcells; 1012 --SCHEME_V->fcells;
992 return x; 1013 return x;
993 } 1014 }
994} 1015}
995 1016
996/* make sure that there is a given number of cells free */
997static pointer
998reserve_cells (SCHEME_P_ int n)
999{
1000 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
1001 return NIL;
1002
1003 /* Are there enough cells available? */
1004 if (SCHEME_V->fcells < n)
1005 {
1006 /* If not, try gc'ing some */
1007 gc (SCHEME_A_ NIL, NIL);
1008
1009 if (SCHEME_V->fcells < n)
1010 {
1011 /* If there still aren't, try getting more heap */
1012 if (!alloc_cellseg (SCHEME_A_ 1) && USE_ERROR_CHECKING)
1013 {
1014 SCHEME_V->no_memory = 1;
1015 return NIL;
1016 }
1017 }
1018
1019 if (SCHEME_V->fcells < n && USE_ERROR_CHECKING)
1020 {
1021 /* If all fail, report failure */
1022 SCHEME_V->no_memory = 1;
1023 return NIL;
1024 }
1025 }
1026
1027 return S_T;
1028}
1029
1030static pointer
1031get_consecutive_cells (SCHEME_P_ int n)
1032{
1033 pointer x;
1034
1035 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
1036 return S_SINK;
1037
1038 /* Are there any cells available? */
1039 x = find_consecutive_cells (SCHEME_A_ n);
1040
1041 if (x != NIL)
1042 return x;
1043
1044 /* If not, try gc'ing some */
1045 gc (SCHEME_A_ NIL, NIL);
1046
1047 for (;;)
1048 {
1049 x = find_consecutive_cells (SCHEME_A_ n);
1050
1051 if (x != NIL)
1052 return x;
1053
1054 /* If there still aren't, try getting more heap */
1055 if (!alloc_cellseg (SCHEME_A_ 1))
1056 {
1057#if USE_ERROR_CHECKING
1058 SCHEME_V->no_memory = 1;
1059 return S_SINK;
1060#endif
1061 }
1062 }
1063}
1064
1065static int
1066count_consecutive_cells (pointer x, int needed)
1067{
1068 int n = 1;
1069
1070 while (cdr (x) == x + 1)
1071 {
1072 x = cdr (x);
1073 n++;
1074
1075 if (n > needed)
1076 return n;
1077 }
1078
1079 return n;
1080}
1081
1082static pointer
1083find_consecutive_cells (SCHEME_P_ int n)
1084{
1085 pointer *pp;
1086 int cnt;
1087
1088 pp = &SCHEME_V->free_cell;
1089
1090 while (*pp != NIL)
1091 {
1092 cnt = count_consecutive_cells (*pp, n);
1093
1094 if (cnt >= n)
1095 {
1096 pointer x = *pp;
1097
1098 *pp = cdr (*pp + n - 1);
1099 SCHEME_V->fcells -= n;
1100 return x;
1101 }
1102
1103 pp = &cdr (*pp + cnt - 1);
1104 }
1105
1106 return NIL;
1107}
1108
1109/* To retain recent allocs before interpreter knows about them - 1017/* To retain recent allocs before interpreter knows about them -
1110 Tehom */ 1018 Tehom */
1111 1019
1112static void 1020static void
1113push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 1021push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
1114{ 1022{
1115 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 1023 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
1116 1024
1117 set_typeflag (holder, T_PAIR | T_IMMUTABLE); 1025 set_typeflag (holder, T_PAIR);
1026 setimmutable (holder);
1118 set_car (holder, recent); 1027 set_car (holder, recent);
1119 set_cdr (holder, car (S_SINK)); 1028 set_cdr (holder, car (S_SINK));
1120 set_car (S_SINK, holder); 1029 set_car (S_SINK, holder);
1121} 1030}
1122 1031
1135 1044
1136 return cell; 1045 return cell;
1137} 1046}
1138 1047
1139static pointer 1048static pointer
1140get_vector_object (SCHEME_P_ int len, pointer init) 1049get_vector_object (SCHEME_P_ uint32_t len, pointer init)
1141{ 1050{
1142 pointer cells = get_consecutive_cells (SCHEME_A_ len / 2 + len % 2 + 1); 1051 pointer v = get_cell_x (SCHEME_A_ 0, 0);
1052 pointer *e = malloc (len * sizeof (pointer));
1143 1053
1144#if USE_ERROR_CHECKING 1054 if (!e && USE_ERROR_CHECKING)
1145 if (SCHEME_V->no_memory)
1146 return S_SINK; 1055 return S_SINK;
1147#endif
1148 1056
1149 /* Record it as a vector so that gc understands it. */ 1057 /* Record it as a vector so that gc understands it. */
1150 set_typeflag (cells, T_VECTOR | T_ATOM); 1058 set_typeflag (v, T_VECTOR | T_ATOM);
1151 ivalue_unchecked (cells) = len; 1059
1152 set_num_integer (cells); 1060 v->object.vector.vvalue = e;
1061 v->object.vector.length = len;
1153 fill_vector (cells, init); 1062 fill_vector (v, init);
1154 push_recent_alloc (SCHEME_A_ cells, NIL); 1063 push_recent_alloc (SCHEME_A_ v, NIL);
1155 1064
1156 return cells; 1065 return v;
1157} 1066}
1158 1067
1159static INLINE void 1068static INLINE void
1160ok_to_freely_gc (SCHEME_P) 1069ok_to_freely_gc (SCHEME_P)
1161{ 1070{
1164 1073
1165#if defined TSGRIND 1074#if defined TSGRIND
1166static void 1075static void
1167check_cell_alloced (pointer p, int expect_alloced) 1076check_cell_alloced (pointer p, int expect_alloced)
1168{ 1077{
1169 /* Can't use putstr(SCHEME_A_ str) because callers have no access to 1078 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */
1170 sc. */
1171 if (typeflag (p) & !expect_alloced) 1079 if (typeflag (p) & !expect_alloced)
1172 xwrstr ("Cell is already allocated!\n"); 1080 xwrstr ("Cell is already allocated!\n");
1173 1081
1174 if (!(typeflag (p)) & expect_alloced) 1082 if (!(typeflag (p)) & expect_alloced)
1175 xwrstr ("Cell is not allocated!\n"); 1083 xwrstr ("Cell is not allocated!\n");
1198 if (immutable) 1106 if (immutable)
1199 setimmutable (x); 1107 setimmutable (x);
1200 1108
1201 set_car (x, a); 1109 set_car (x, a);
1202 set_cdr (x, b); 1110 set_cdr (x, b);
1111
1203 return x; 1112 return x;
1204} 1113}
1205 1114
1206/* ========== oblist implementation ========== */ 1115/* ========== oblist implementation ========== */
1207 1116
1217 1126
1218/* returns the new symbol */ 1127/* returns the new symbol */
1219static pointer 1128static pointer
1220oblist_add_by_name (SCHEME_P_ const char *name) 1129oblist_add_by_name (SCHEME_P_ const char *name)
1221{ 1130{
1222 pointer x;
1223 int location; 1131 int location;
1224 1132
1225 x = immutable_cons (mk_string (SCHEME_A_ name), NIL); 1133 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1226 set_typeflag (x, T_SYMBOL); 1134 set_typeflag (x, T_SYMBOL);
1227 setimmutable (car (x)); 1135 setimmutable (car (x));
1228 1136
1229 location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); 1137 location = hash_fn (name, vector_length (SCHEME_V->oblist));
1230 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); 1138 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location)));
1231 return x; 1139 return x;
1232} 1140}
1233 1141
1234static INLINE pointer 1142static INLINE pointer
1236{ 1144{
1237 int location; 1145 int location;
1238 pointer x; 1146 pointer x;
1239 char *s; 1147 char *s;
1240 1148
1241 location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); 1149 location = hash_fn (name, vector_length (SCHEME_V->oblist));
1242 1150
1243 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1151 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1244 { 1152 {
1245 s = symname (car (x)); 1153 s = symname (car (x));
1246 1154
1257{ 1165{
1258 int i; 1166 int i;
1259 pointer x; 1167 pointer x;
1260 pointer ob_list = NIL; 1168 pointer ob_list = NIL;
1261 1169
1262 for (i = 0; i < ivalue_unchecked (SCHEME_V->oblist); i++) 1170 for (i = 0; i < vector_length (SCHEME_V->oblist); i++)
1263 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1171 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1264 ob_list = cons (x, ob_list); 1172 ob_list = cons (x, ob_list);
1265 1173
1266 return ob_list; 1174 return ob_list;
1267} 1175}
1341mk_character (SCHEME_P_ int c) 1249mk_character (SCHEME_P_ int c)
1342{ 1250{
1343 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1251 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1344 1252
1345 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1253 set_typeflag (x, (T_CHARACTER | T_ATOM));
1346 ivalue_unchecked (x) = c; 1254 ivalue_unchecked (x) = c & 0xff;
1347 set_num_integer (x); 1255 set_num_integer (x);
1348 return x; 1256 return x;
1349} 1257}
1350 1258
1351/* get number atom (integer) */ 1259/* get number atom (integer) */
1370 set_num_real (x); 1278 set_num_real (x);
1371 return x; 1279 return x;
1372} 1280}
1373 1281
1374static pointer 1282static pointer
1375mk_number (SCHEME_P_ num n) 1283mk_number (SCHEME_P_ const num n)
1376{ 1284{
1377 if (num_is_fixnum (n)) 1285 if (num_is_fixnum (n))
1378 {
1379 return mk_integer (SCHEME_A_ num_ivalue (n)); 1286 return mk_integer (SCHEME_A_ num_get_ivalue (n));
1380 }
1381 else 1287 else
1382 {
1383 return mk_real (SCHEME_A_ num_rvalue (n)); 1288 return mk_real (SCHEME_A_ num_get_rvalue (n));
1384 }
1385} 1289}
1386 1290
1387/* allocate name to string area */ 1291/* allocate name to string area */
1388static char * 1292static char *
1389store_string (SCHEME_P_ int len_str, const char *str, char fill) 1293store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1390{ 1294{
1391 char *q;
1392
1393 q = (char *)malloc (len_str + 1); 1295 char *q = malloc (len_str + 1);
1394 1296
1395 if (q == 0) 1297 if (q == 0 && USE_ERROR_CHECKING)
1396 { 1298 {
1397#if USE_ERROR_CHECKING
1398 SCHEME_V->no_memory = 1; 1299 SCHEME_V->no_memory = 1;
1399 return SCHEME_V->strbuff; 1300 return SCHEME_V->strbuff;
1400#endif
1401 } 1301 }
1402 1302
1403 if (str) 1303 if (str)
1404 { 1304 {
1405 int l = strlen (str); 1305 int l = strlen (str);
1406 1306
1407 if (l > len_str) 1307 if (l > len_str)
1408 l = len_str; 1308 l = len_str;
1409 1309
1410 memcpy (q, str, l + 1); 1310 memcpy (q, str, l);
1411 q[l + 1] = 0; 1311 q[l] = 0;
1412 } 1312 }
1413 else 1313 else
1414 { 1314 {
1415 memset (q, fill, len_str); 1315 memset (q, fill, len_str);
1416 q[len_str] = 0; 1316 q[len_str] = 0;
1425{ 1325{
1426 return mk_counted_string (SCHEME_A_ str, strlen (str)); 1326 return mk_counted_string (SCHEME_A_ str, strlen (str));
1427} 1327}
1428 1328
1429INTERFACE pointer 1329INTERFACE pointer
1430mk_counted_string (SCHEME_P_ const char *str, int len) 1330mk_counted_string (SCHEME_P_ const char *str, uint32_t len)
1431{ 1331{
1432 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1332 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1433 1333
1434 set_typeflag (x, T_STRING | T_ATOM); 1334 set_typeflag (x, T_STRING | T_ATOM);
1435 strvalue (x) = store_string (SCHEME_A_ len, str, 0); 1335 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1436 strlength (x) = len; 1336 strlength (x) = len;
1437 return x; 1337 return x;
1438} 1338}
1439 1339
1440INTERFACE pointer 1340INTERFACE pointer
1441mk_empty_string (SCHEME_P_ int len, char fill) 1341mk_empty_string (SCHEME_P_ uint32_t len, char fill)
1442{ 1342{
1443 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1343 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1444 1344
1445 set_typeflag (x, T_STRING | T_ATOM); 1345 set_typeflag (x, T_STRING | T_ATOM);
1446 strvalue (x) = store_string (SCHEME_A_ len, 0, fill); 1346 strvalue (x) = store_string (SCHEME_A_ len, 0, fill);
1447 strlength (x) = len; 1347 strlength (x) = len;
1448 return x; 1348 return x;
1449} 1349}
1450 1350
1451INTERFACE pointer 1351INTERFACE pointer
1452mk_vector (SCHEME_P_ int len) 1352mk_vector (SCHEME_P_ uint32_t len)
1453{ 1353{
1454 return get_vector_object (SCHEME_A_ len, NIL); 1354 return get_vector_object (SCHEME_A_ len, NIL);
1455} 1355}
1456 1356
1457INTERFACE void 1357INTERFACE void
1458fill_vector (pointer vec, pointer obj) 1358fill_vector (pointer vec, pointer obj)
1459{ 1359{
1460 int i; 1360 int i;
1461 int num = ivalue (vec) / 2 + ivalue (vec) % 2;
1462 1361
1463 for (i = 0; i < num; i++) 1362 for (i = 0; i < vec->object.vector.length; i++)
1464 { 1363 vec->object.vector.vvalue[i] = obj;
1465 set_typeflag (vec + 1 + i, T_PAIR);
1466 setimmutable (vec + 1 + i);
1467 set_car (vec + 1 + i, obj);
1468 set_cdr (vec + 1 + i, obj);
1469 }
1470} 1364}
1471 1365
1472INTERFACE pointer 1366INTERFACE pointer
1473vector_elem (pointer vec, int ielem) 1367vector_elem (pointer vec, uint32_t ielem)
1474{ 1368{
1475 int n = ielem / 2; 1369 return vec->object.vector.vvalue[ielem];
1476
1477 if (ielem % 2 == 0)
1478 return car (vec + 1 + n);
1479 else
1480 return cdr (vec + 1 + n);
1481} 1370}
1482 1371
1483INTERFACE void 1372INTERFACE void
1484set_vector_elem (pointer vec, int ielem, pointer a) 1373set_vector_elem (pointer vec, uint32_t ielem, pointer a)
1485{ 1374{
1486 int n = ielem / 2; 1375 vec->object.vector.vvalue[ielem] = a;
1487
1488 if (ielem % 2 == 0)
1489 set_car (vec + 1 + n, a);
1490 else
1491 set_cdr (vec + 1 + n, a);
1492} 1376}
1493 1377
1494/* get new symbol */ 1378/* get new symbol */
1495INTERFACE pointer 1379INTERFACE pointer
1496mk_symbol (SCHEME_P_ const char *name) 1380mk_symbol (SCHEME_P_ const char *name)
1497{ 1381{
1498 pointer x;
1499
1500 /* first check oblist */ 1382 /* first check oblist */
1501 x = oblist_find_by_name (SCHEME_A_ name); 1383 pointer x = oblist_find_by_name (SCHEME_A_ name);
1502 1384
1503 if (x != NIL) 1385 if (x == NIL)
1504 return x;
1505 else
1506 {
1507 x = oblist_add_by_name (SCHEME_A_ name); 1386 x = oblist_add_by_name (SCHEME_A_ name);
1387
1508 return x; 1388 return x;
1509 }
1510} 1389}
1511 1390
1512INTERFACE pointer 1391INTERFACE pointer
1513gensym (SCHEME_P) 1392gensym (SCHEME_P)
1514{ 1393{
1567 has_dec_point = 1; 1446 has_dec_point = 1;
1568 c = *p++; 1447 c = *p++;
1569 } 1448 }
1570 1449
1571 if (!isdigit (c)) 1450 if (!isdigit (c))
1572 {
1573 return mk_symbol (SCHEME_A_ strlwr (q)); 1451 return mk_symbol (SCHEME_A_ strlwr (q));
1574 }
1575 } 1452 }
1576 else if (c == '.') 1453 else if (c == '.')
1577 { 1454 {
1578 has_dec_point = 1; 1455 has_dec_point = 1;
1579 c = *p++; 1456 c = *p++;
1580 1457
1581 if (!isdigit (c)) 1458 if (!isdigit (c))
1582 {
1583 return mk_symbol (SCHEME_A_ strlwr (q)); 1459 return mk_symbol (SCHEME_A_ strlwr (q));
1584 }
1585 } 1460 }
1586 else if (!isdigit (c)) 1461 else if (!isdigit (c))
1587 {
1588 return mk_symbol (SCHEME_A_ strlwr (q)); 1462 return mk_symbol (SCHEME_A_ strlwr (q));
1589 }
1590 1463
1591 for (; (c = *p) != 0; ++p) 1464 for (; (c = *p) != 0; ++p)
1592 { 1465 {
1593 if (!isdigit (c)) 1466 if (!isdigit (c))
1594 { 1467 {
1607 has_dec_point = 1; /* decimal point illegal 1480 has_dec_point = 1; /* decimal point illegal
1608 from now on */ 1481 from now on */
1609 p++; 1482 p++;
1610 1483
1611 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1484 if ((*p == '-') || (*p == '+') || isdigit (*p))
1612 {
1613 continue; 1485 continue;
1614 }
1615 } 1486 }
1616 } 1487 }
1617 1488
1618 return mk_symbol (SCHEME_A_ strlwr (q)); 1489 return mk_symbol (SCHEME_A_ strlwr (q));
1619 } 1490 }
1707static void 1578static void
1708mark (pointer a) 1579mark (pointer a)
1709{ 1580{
1710 pointer t, q, p; 1581 pointer t, q, p;
1711 1582
1712 t = (pointer) 0; 1583 t = 0;
1713 p = a; 1584 p = a;
1714E2: 1585E2:
1715 setmark (p); 1586 setmark (p);
1716 printf ("mark %p %x\n", p, p->flag);//D
1717 1587
1718 if (is_vector (p)) 1588 if (is_vector (p))
1719 { 1589 {
1720 int i; 1590 int i;
1721 int num = ivalue_unchecked (p) / 2 + ivalue_unchecked (p) % 2;
1722 1591
1723 for (i = 0; i < num; i++) 1592 for (i = 0; i < p->object.vector.length; i++)
1724 { 1593 mark (p->object.vector.vvalue[i]);
1725 /* Vector cells will be treated like ordinary cells */
1726 mark (p + 1 + i);
1727 }
1728 } 1594 }
1729 1595
1730 if (is_atom (p)) 1596 if (is_atom (p))
1731 goto E6; 1597 goto E6;
1732 1598
1742 goto E2; 1608 goto E2;
1743 } 1609 }
1744 1610
1745E5: 1611E5:
1746 q = cdr (p); /* down cdr */ 1612 q = cdr (p); /* down cdr */
1747 printf ("mark+ %p\n", q, q->flag);//D
1748 1613
1749 if (q && !is_mark (q)) 1614 if (q && !is_mark (q))
1750 { 1615 {
1751 set_cdr (p, t); 1616 set_cdr (p, t);
1752 t = p; 1617 t = p;
1753 p = q; 1618 p = q;
1754 goto E2; 1619 goto E2;
1755 } 1620 }
1756 1621
1757E6: /* up. Undo the link switching from steps E4 and E5. */ 1622E6: /* up. Undo the link switching from steps E4 and E5. */
1758
1759 if (!t) 1623 if (!t)
1760 return; 1624 return;
1761 1625
1762 q = t; 1626 q = t;
1763 1627
1831 if (is_mark (p)) 1695 if (is_mark (p))
1832 clrmark (p); 1696 clrmark (p);
1833 else 1697 else
1834 { 1698 {
1835 /* reclaim cell */ 1699 /* reclaim cell */
1836 if (typeflag (p) != 0) 1700 if (typeflag (p) != T_FREE)
1837 { 1701 {
1838 finalize_cell (SCHEME_A_ p); 1702 finalize_cell (SCHEME_A_ p);
1839 set_typeflag (p, 0); 1703 set_typeflag (p, T_FREE);
1840 set_car (p, NIL); 1704 set_car (p, NIL);
1841 } 1705 }
1842 1706
1843 ++SCHEME_V->fcells; 1707 ++SCHEME_V->fcells;
1844 set_cdr (p, SCHEME_V->free_cell); 1708 set_cdr (p, SCHEME_V->free_cell);
1854static void 1718static void
1855finalize_cell (SCHEME_P_ pointer a) 1719finalize_cell (SCHEME_P_ pointer a)
1856{ 1720{
1857 if (is_string (a)) 1721 if (is_string (a))
1858 free (strvalue (a)); 1722 free (strvalue (a));
1723 else if (is_vector (a))
1724 free (a->object.vector.vvalue);
1859#if USE_PORTS 1725#if USE_PORTS
1860 else if (is_port (a)) 1726 else if (is_port (a))
1861 { 1727 {
1862 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) 1728 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit)
1863 port_close (SCHEME_A_ a, port_input | port_output); 1729 port_close (SCHEME_A_ a, port_input | port_output);
2289 char *p = SCHEME_V->strbuff; 2155 char *p = SCHEME_V->strbuff;
2290 2156
2291 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2157 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2292 2158
2293 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2159 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2294 {
2295 *p = 0; 2160 *p = 0;
2296 }
2297 else 2161 else
2298 { 2162 {
2299 backchar (SCHEME_A_ p[-1]); 2163 backchar (SCHEME_A_ p[-1]);
2300 *--p = '\0'; 2164 *--p = '\0';
2301 } 2165 }
2316 for (;;) 2180 for (;;)
2317 { 2181 {
2318 c = inchar (SCHEME_A); 2182 c = inchar (SCHEME_A);
2319 2183
2320 if (c == EOF || p - SCHEME_V->strbuff > sizeof (SCHEME_V->strbuff) - 1) 2184 if (c == EOF || p - SCHEME_V->strbuff > sizeof (SCHEME_V->strbuff) - 1)
2321 {
2322 return S_F; 2185 return S_F;
2323 }
2324 2186
2325 switch (state) 2187 switch (state)
2326 { 2188 {
2327 case st_ok: 2189 case st_ok:
2328 switch (c) 2190 switch (c)
2396 c = toupper (c); 2258 c = toupper (c);
2397 2259
2398 if (c >= '0' && c <= 'F') 2260 if (c >= '0' && c <= 'F')
2399 { 2261 {
2400 if (c <= '9') 2262 if (c <= '9')
2401 {
2402 c1 = (c1 << 4) + c - '0'; 2263 c1 = (c1 << 4) + c - '0';
2403 }
2404 else 2264 else
2405 {
2406 c1 = (c1 << 4) + c - 'A' + 10; 2265 c1 = (c1 << 4) + c - 'A' + 10;
2407 }
2408 2266
2409 if (state == st_x1) 2267 if (state == st_x1)
2410 {
2411 state = st_x2; 2268 state = st_x2;
2412 }
2413 else 2269 else
2414 { 2270 {
2415 *p++ = c1; 2271 *p++ = c1;
2416 state = st_ok; 2272 state = st_ok;
2417 } 2273 }
2418 } 2274 }
2419 else 2275 else
2420 {
2421 return S_F; 2276 return S_F;
2422 }
2423 2277
2424 break; 2278 break;
2425 2279
2426 case st_oct1: 2280 case st_oct1:
2427 case st_oct2: 2281 case st_oct2:
2458is_one_of (char *s, int c) 2312is_one_of (char *s, int c)
2459{ 2313{
2460 if (c == EOF) 2314 if (c == EOF)
2461 return 1; 2315 return 1;
2462 2316
2463 while (*s) 2317 return !!strchr (s, c);
2464 if (*s++ == c)
2465 return 1;
2466
2467 return 0;
2468} 2318}
2469 2319
2470/* skip white characters */ 2320/* skip white characters */
2471static INLINE int 2321static INLINE int
2472skipspace (SCHEME_P) 2322skipspace (SCHEME_P)
2477 { 2327 {
2478 c = inchar (SCHEME_A); 2328 c = inchar (SCHEME_A);
2479#if SHOW_ERROR_LINE 2329#if SHOW_ERROR_LINE
2480 if (c == '\n') 2330 if (c == '\n')
2481 curr_line++; 2331 curr_line++;
2482
2483#endif 2332#endif
2484 } 2333 }
2485 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2334 while (c == ' ' || c == '\n' || c == '\r' || c == '\t');
2486 2335
2487 /* record it */ 2336 /* record it */
2524 2373
2525 if (is_one_of (" \n\t", c)) 2374 if (is_one_of (" \n\t", c))
2526 return TOK_DOT; 2375 return TOK_DOT;
2527 else 2376 else
2528 { 2377 {
2378 //TODO: ungetc twice in a row is not supported in C
2529 backchar (SCHEME_A_ c); 2379 backchar (SCHEME_A_ c);
2530 backchar (SCHEME_A_ '.'); 2380 backchar (SCHEME_A_ '.');
2531 return TOK_ATOM; 2381 return TOK_ATOM;
2532 } 2382 }
2533 2383
2647 int d = *s / 16; 2497 int d = *s / 16;
2648 2498
2649 putcharacter (SCHEME_A_ 'x'); 2499 putcharacter (SCHEME_A_ 'x');
2650 2500
2651 if (d < 10) 2501 if (d < 10)
2652 {
2653 putcharacter (SCHEME_A_ d + '0'); 2502 putcharacter (SCHEME_A_ d + '0');
2654 }
2655 else 2503 else
2656 {
2657 putcharacter (SCHEME_A_ d - 10 + 'A'); 2504 putcharacter (SCHEME_A_ d - 10 + 'A');
2658 }
2659 2505
2660 d = *s % 16; 2506 d = *s % 16;
2661 2507
2662 if (d < 10) 2508 if (d < 10)
2663 {
2664 putcharacter (SCHEME_A_ d + '0'); 2509 putcharacter (SCHEME_A_ d + '0');
2665 }
2666 else 2510 else
2667 {
2668 putcharacter (SCHEME_A_ d - 10 + 'A'); 2511 putcharacter (SCHEME_A_ d - 10 + 'A');
2669 }
2670 } 2512 }
2671 } 2513 }
2672 } 2514 }
2673 else 2515 else
2674 {
2675 putcharacter (SCHEME_A_ * s); 2516 putcharacter (SCHEME_A_ * s);
2676 }
2677 2517
2678 s++; 2518 s++;
2679 } 2519 }
2680 2520
2681 putcharacter (SCHEME_A_ '"'); 2521 putcharacter (SCHEME_A_ '"');
2898list_star (SCHEME_P_ pointer d) 2738list_star (SCHEME_P_ pointer d)
2899{ 2739{
2900 pointer p, q; 2740 pointer p, q;
2901 2741
2902 if (cdr (d) == NIL) 2742 if (cdr (d) == NIL)
2903 {
2904 return car (d); 2743 return car (d);
2905 }
2906 2744
2907 p = cons (car (d), cdr (d)); 2745 p = cons (car (d), cdr (d));
2908 q = p; 2746 q = p;
2909 2747
2910 while (cdr (cdr (p)) != NIL) 2748 while (cdr (cdr (p)) != NIL)
2911 { 2749 {
2912 d = cons (car (p), cdr (p)); 2750 d = cons (car (p), cdr (p));
2913 2751
2914 if (cdr (cdr (p)) != NIL) 2752 if (cdr (cdr (p)) != NIL)
2915 {
2916 p = cdr (d); 2753 p = cdr (d);
2917 }
2918 } 2754 }
2919 2755
2920 set_cdr (p, car (cdr (p))); 2756 set_cdr (p, car (cdr (p)));
2921 return q; 2757 return q;
2922} 2758}
2936 2772
2937/* reverse list --- in-place */ 2773/* reverse list --- in-place */
2938static pointer 2774static pointer
2939reverse_in_place (SCHEME_P_ pointer term, pointer list) 2775reverse_in_place (SCHEME_P_ pointer term, pointer list)
2940{ 2776{
2941 pointer p = list, result = term, q; 2777 pointer result = term;
2778 pointer p = list;
2942 2779
2943 while (p != NIL) 2780 while (p != NIL)
2944 { 2781 {
2945 q = cdr (p); 2782 pointer q = cdr (p);
2946 set_cdr (p, result); 2783 set_cdr (p, result);
2947 result = p; 2784 result = p;
2948 p = q; 2785 p = q;
2949 } 2786 }
2950 2787
3025#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) 2862#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST)
3026 2863
3027static int 2864static int
3028hash_fn (const char *key, int table_size) 2865hash_fn (const char *key, int table_size)
3029{ 2866{
3030 unsigned int hashed = 0; 2867 const unsigned char *p = key;
3031 const char *c; 2868 uint32_t hash = 2166136261;
3032 int bits_per_int = sizeof (unsigned int) * 8;
3033 2869
3034 for (c = key; *c; c++) 2870 while (*p)
3035 { 2871 hash = (hash ^ *p++) * 16777619;
3036 /* letters have about 5 bits in them */
3037 hashed = (hashed << 5) | (hashed >> (bits_per_int - 5));
3038 hashed ^= *c;
3039 }
3040 2872
3041 return hashed % table_size; 2873 return hash % table_size;
3042} 2874}
3043#endif 2875#endif
3044 2876
3045#ifndef USE_ALIST_ENV 2877#ifndef USE_ALIST_ENV
3046 2878
3072{ 2904{
3073 pointer slot = immutable_cons (variable, value); 2905 pointer slot = immutable_cons (variable, value);
3074 2906
3075 if (is_vector (car (env))) 2907 if (is_vector (car (env)))
3076 { 2908 {
3077 int location = hash_fn (symname (variable), ivalue_unchecked (car (env))); 2909 int location = hash_fn (symname (variable), vector_length (car (env)));
3078 2910
3079 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2911 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location)));
3080 } 2912 }
3081 else 2913 else
3082 set_car (env, immutable_cons (slot, car (env))); 2914 set_car (env, immutable_cons (slot, car (env)));
3090 2922
3091 for (x = env; x != NIL; x = cdr (x)) 2923 for (x = env; x != NIL; x = cdr (x))
3092 { 2924 {
3093 if (is_vector (car (x))) 2925 if (is_vector (car (x)))
3094 { 2926 {
3095 location = hash_fn (symname (hdl), ivalue_unchecked (car (x))); 2927 location = hash_fn (symname (hdl), vector_length (car (x)));
3096 y = vector_elem (car (x), location); 2928 y = vector_elem (car (x), location);
3097 } 2929 }
3098 else 2930 else
3099 y = car (x); 2931 y = car (x);
3100 2932
3231 else 3063 else
3232 SCHEME_V->args = NIL; 3064 SCHEME_V->args = NIL;
3233 3065
3234 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); 3066 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args);
3235 setimmutable (car (SCHEME_V->args)); 3067 setimmutable (car (SCHEME_V->args));
3236 SCHEME_V->op = (int)OP_ERR0; 3068 SCHEME_V->op = OP_ERR0;
3237 return S_T; 3069 return S_T;
3238} 3070}
3239 3071
3240#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) 3072#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a)
3241#define Error_0(s) Error_1 (s, 0) 3073#define Error_0(s) Error_1 (s, 0)
3242 3074
3243/* Too small to turn into function */ 3075/* Too small to turn into function */
3244#define BEGIN do { 3076#define BEGIN do {
3245#define END } while (0) 3077#define END } while (0)
3246#define s_goto(a) BEGIN \ 3078#define s_goto(a) BEGIN \
3247 SCHEME_V->op = (int)(a); \ 3079 SCHEME_V->op = a; \
3248 return S_T; END 3080 return S_T; END
3249 3081
3250#define s_return(a) return xs_return(SCHEME_A_ a) 3082#define s_return(a) return xs_return (SCHEME_A_ a)
3251 3083
3252#ifndef USE_SCHEME_STACK 3084#ifndef USE_SCHEME_STACK
3253 3085
3254/* this structure holds all the interpreter's registers */ 3086/* this structure holds all the interpreter's registers */
3255struct dump_stack_frame 3087struct dump_stack_frame
3274 SCHEME_V->dump_size += STACK_GROWTH; 3106 SCHEME_V->dump_size += STACK_GROWTH;
3275 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); 3107 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size);
3276 } 3108 }
3277 3109
3278 next_frame = SCHEME_V->dump_base + nframes; 3110 next_frame = SCHEME_V->dump_base + nframes;
3111
3279 next_frame->op = op; 3112 next_frame->op = op;
3280 next_frame->args = args; 3113 next_frame->args = args;
3281 next_frame->envir = SCHEME_V->envir; 3114 next_frame->envir = SCHEME_V->envir;
3282 next_frame->code = code; 3115 next_frame->code = code;
3116
3283 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3117 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3284} 3118}
3285 3119
3286static pointer 3120static pointer
3287xs_return (SCHEME_P_ pointer a) 3121xs_return (SCHEME_P_ pointer a)
3292 SCHEME_V->value = a; 3126 SCHEME_V->value = a;
3293 3127
3294 if (nframes <= 0) 3128 if (nframes <= 0)
3295 return NIL; 3129 return NIL;
3296 3130
3297 nframes--;
3298 frame = SCHEME_V->dump_base + nframes; 3131 frame = &SCHEME_V->dump_base[--nframes];
3299 SCHEME_V->op = frame->op; 3132 SCHEME_V->op = frame->op;
3300 SCHEME_V->args = frame->args; 3133 SCHEME_V->args = frame->args;
3301 SCHEME_V->envir = frame->envir; 3134 SCHEME_V->envir = frame->envir;
3302 SCHEME_V->code = frame->code; 3135 SCHEME_V->code = frame->code;
3303 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3136 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3304 3137
3305 return S_T; 3138 return S_T;
3306} 3139}
3307 3140
3308static INLINE void 3141static INLINE void
3309dump_stack_reset (SCHEME_P) 3142dump_stack_reset (SCHEME_P)
3310{ 3143{
3311 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3144 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3312 SCHEME_V->dump = (pointer)0; 3145 SCHEME_V->dump = (pointer)+0;
3313} 3146}
3314 3147
3315static INLINE void 3148static INLINE void
3316dump_stack_initialize (SCHEME_P) 3149dump_stack_initialize (SCHEME_P)
3317{ 3150{
3318 SCHEME_V->dump_size = 0; 3151 SCHEME_V->dump_size = 0;
3319 SCHEME_V->dump_base = NULL; 3152 SCHEME_V->dump_base = 0;
3320 dump_stack_reset (SCHEME_A); 3153 dump_stack_reset (SCHEME_A);
3321} 3154}
3322 3155
3323static void 3156static void
3324dump_stack_free (SCHEME_P) 3157dump_stack_free (SCHEME_P)
3325{ 3158{
3326 free (SCHEME_V->dump_base); 3159 free (SCHEME_V->dump_base);
3327 SCHEME_V->dump_base = NULL; 3160 SCHEME_V->dump_base = 0;
3328 SCHEME_V->dump = (pointer)0; 3161 SCHEME_V->dump = (pointer)0;
3329 SCHEME_V->dump_size = 0; 3162 SCHEME_V->dump_size = 0;
3330} 3163}
3331 3164
3332static void 3165static void
3374 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3207 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3375 3208
3376 while (cont != NIL) 3209 while (cont != NIL)
3377 { 3210 {
3378 frame->op = ivalue (car (cont)); cont = cdr (cont); 3211 frame->op = ivalue (car (cont)); cont = cdr (cont);
3379 frame->args = car (cont) ; cont = cdr (cont); 3212 frame->args = car (cont) ; cont = cdr (cont);
3380 frame->envir = car (cont) ; cont = cdr (cont); 3213 frame->envir = car (cont) ; cont = cdr (cont);
3381 frame->code = car (cont) ; cont = cdr (cont); 3214 frame->code = car (cont) ; cont = cdr (cont);
3382 3215
3383 ++frame; 3216 ++frame;
3384 ++i; 3217 ++i;
3385 } 3218 }
3386 3219
3415 SCHEME_V->value = a; 3248 SCHEME_V->value = a;
3416 3249
3417 if (dump == NIL) 3250 if (dump == NIL)
3418 return NIL; 3251 return NIL;
3419 3252
3420 SCHEME_V->op = ivalue (car (dump)); 3253 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump);
3421 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3254 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3422 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3255 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3423 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3256 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3424 3257
3425 SCHEME_V->dump = dump; 3258 SCHEME_V->dump = dump;
3426 3259
3427 return S_T; 3260 return S_T;
3428} 3261}
3525 3358
3526 case OP_READ_INTERNAL: /* internal read */ 3359 case OP_READ_INTERNAL: /* internal read */
3527 SCHEME_V->tok = token (SCHEME_A); 3360 SCHEME_V->tok = token (SCHEME_A);
3528 3361
3529 if (SCHEME_V->tok == TOK_EOF) 3362 if (SCHEME_V->tok == TOK_EOF)
3530 {
3531 s_return (S_EOF); 3363 s_return (S_EOF);
3532 }
3533 3364
3534 s_goto (OP_RDSEXPR); 3365 s_goto (OP_RDSEXPR);
3535 3366
3536 case OP_GENSYM: 3367 case OP_GENSYM:
3537 s_return (gensym (SCHEME_A)); 3368 s_return (gensym (SCHEME_A));
3541 /* OP_VALUEPRINT is always pushed, because when changing from 3372 /* OP_VALUEPRINT is always pushed, because when changing from
3542 non-interactive to interactive mode, it needs to be 3373 non-interactive to interactive mode, it needs to be
3543 already on the stack */ 3374 already on the stack */
3544#if USE_TRACING 3375#if USE_TRACING
3545 if (SCHEME_V->tracing) 3376 if (SCHEME_V->tracing)
3546 {
3547 putstr (SCHEME_A_ "\nGives: "); 3377 putstr (SCHEME_A_ "\nGives: ");
3548 }
3549#endif 3378#endif
3550 3379
3551 if (file_interactive (SCHEME_A)) 3380 if (file_interactive (SCHEME_A))
3552 { 3381 {
3553 SCHEME_V->print_flag = 1; 3382 SCHEME_V->print_flag = 1;
3554 SCHEME_V->args = SCHEME_V->value; 3383 SCHEME_V->args = SCHEME_V->value;
3555 s_goto (OP_P0LIST); 3384 s_goto (OP_P0LIST);
3556 } 3385 }
3557 else 3386 else
3558 {
3559 s_return (SCHEME_V->value); 3387 s_return (SCHEME_V->value);
3560 }
3561 3388
3562 case OP_EVAL: /* main part of evaluation */ 3389 case OP_EVAL: /* main part of evaluation */
3563#if USE_TRACING 3390#if USE_TRACING
3564 if (SCHEME_V->tracing) 3391 if (SCHEME_V->tracing)
3565 { 3392 {
3569 putstr (SCHEME_A_ "\nEval: "); 3396 putstr (SCHEME_A_ "\nEval: ");
3570 s_goto (OP_P0LIST); 3397 s_goto (OP_P0LIST);
3571 } 3398 }
3572 3399
3573 /* fall through */ 3400 /* fall through */
3401
3574 case OP_REAL_EVAL: 3402 case OP_REAL_EVAL:
3575#endif 3403#endif
3576 if (is_symbol (SCHEME_V->code)) /* symbol */ 3404 if (is_symbol (SCHEME_V->code)) /* symbol */
3577 { 3405 {
3578 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); 3406 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1);
3654 putstr (SCHEME_A_ "\nApply to: "); 3482 putstr (SCHEME_A_ "\nApply to: ");
3655 s_goto (OP_P0LIST); 3483 s_goto (OP_P0LIST);
3656 } 3484 }
3657 3485
3658 /* fall through */ 3486 /* fall through */
3487
3659 case OP_REAL_APPLY: 3488 case OP_REAL_APPLY:
3660#endif 3489#endif
3661 if (is_proc (SCHEME_V->code)) 3490 if (is_proc (SCHEME_V->code))
3662 { 3491 {
3663 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3492 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3677 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); 3506 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code));
3678 3507
3679 for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) 3508 for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y))
3680 { 3509 {
3681 if (y == NIL) 3510 if (y == NIL)
3682 {
3683 Error_0 ("not enough arguments"); 3511 Error_0 ("not enough arguments");
3684 }
3685 else 3512 else
3686 {
3687 new_slot_in_env (SCHEME_A_ car (x), car (y)); 3513 new_slot_in_env (SCHEME_A_ car (x), car (y));
3688 }
3689 } 3514 }
3690 3515
3691 if (x == NIL) 3516 if (x == NIL)
3692 { 3517 {
3693
3694 /*-- 3518 /*--
3695 * if (y != NIL) { 3519 * if (y != NIL) {
3696 * Error_0("too many arguments"); 3520 * Error_0("too many arguments");
3697 * } 3521 * }
3698 */ 3522 */
3699 } 3523 }
3700 else if (is_symbol (x)) 3524 else if (is_symbol (x))
3701 new_slot_in_env (SCHEME_A_ x, y); 3525 new_slot_in_env (SCHEME_A_ x, y);
3702 else 3526 else
3703 {
3704 Error_1 ("syntax error in closure: not a symbol:", x); 3527 Error_1 ("syntax error in closure: not a symbol:", x);
3705 }
3706 3528
3707 SCHEME_V->code = cdr (closure_code (SCHEME_V->code)); 3529 SCHEME_V->code = cdr (closure_code (SCHEME_V->code));
3708 SCHEME_V->args = NIL; 3530 SCHEME_V->args = NIL;
3709 s_goto (OP_BEGIN); 3531 s_goto (OP_BEGIN);
3710 } 3532 }
3780 x = car (SCHEME_V->code); 3602 x = car (SCHEME_V->code);
3781 SCHEME_V->code = cadr (SCHEME_V->code); 3603 SCHEME_V->code = cadr (SCHEME_V->code);
3782 } 3604 }
3783 3605
3784 if (!is_symbol (x)) 3606 if (!is_symbol (x))
3785 {
3786 Error_0 ("variable is not a symbol"); 3607 Error_0 ("variable is not a symbol");
3787 }
3788 3608
3789 s_save (SCHEME_A_ OP_DEF1, NIL, x); 3609 s_save (SCHEME_A_ OP_DEF1, NIL, x);
3790 s_goto (OP_EVAL); 3610 s_goto (OP_EVAL);
3791 3611
3792 case OP_DEF1: /* define */ 3612 case OP_DEF1: /* define */
3793 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); 3613 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0);
3794 3614
3795 if (x != NIL) 3615 if (x != NIL)
3796 {
3797 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); 3616 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value);
3798 }
3799 else 3617 else
3800 {
3801 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); 3618 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value);
3802 }
3803 3619
3804 s_return (SCHEME_V->code); 3620 s_return (SCHEME_V->code);
3805 3621
3806 3622
3807 case OP_DEFP: /* defined? */ 3623 case OP_DEFP: /* defined? */
3808 x = SCHEME_V->envir; 3624 x = SCHEME_V->envir;
3809 3625
3810 if (cdr (SCHEME_V->args) != NIL) 3626 if (cdr (SCHEME_V->args) != NIL)
3811 {
3812 x = cadr (SCHEME_V->args); 3627 x = cadr (SCHEME_V->args);
3813 }
3814 3628
3815 s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); 3629 s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL);
3816 3630
3817 case OP_SET0: /* set! */ 3631 case OP_SET0: /* set! */
3818 if (is_immutable (car (SCHEME_V->code))) 3632 if (is_immutable (car (SCHEME_V->code)))
3829 { 3643 {
3830 set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); 3644 set_slot_in_env (SCHEME_A_ y, SCHEME_V->value);
3831 s_return (SCHEME_V->value); 3645 s_return (SCHEME_V->value);
3832 } 3646 }
3833 else 3647 else
3834 {
3835 Error_1 ("set!: unbound variable:", SCHEME_V->code); 3648 Error_1 ("set!: unbound variable:", SCHEME_V->code);
3836 }
3837 3649
3838 3650
3839 case OP_BEGIN: /* begin */ 3651 case OP_BEGIN: /* begin */
3840 if (!is_pair (SCHEME_V->code)) 3652 if (!is_pair (SCHEME_V->code))
3841 {
3842 s_return (SCHEME_V->code); 3653 s_return (SCHEME_V->code);
3843 }
3844 3654
3845 if (cdr (SCHEME_V->code) != NIL) 3655 if (cdr (SCHEME_V->code) != NIL)
3846 {
3847 s_save (SCHEME_A_ OP_BEGIN, NIL, cdr (SCHEME_V->code)); 3656 s_save (SCHEME_A_ OP_BEGIN, NIL, cdr (SCHEME_V->code));
3848 }
3849 3657
3850 SCHEME_V->code = car (SCHEME_V->code); 3658 SCHEME_V->code = car (SCHEME_V->code);
3851 s_goto (OP_EVAL); 3659 s_goto (OP_EVAL);
3852 3660
3853 case OP_IF0: /* if */ 3661 case OP_IF0: /* if */
3874 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3682 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args);
3875 3683
3876 if (is_pair (SCHEME_V->code)) /* continue */ 3684 if (is_pair (SCHEME_V->code)) /* continue */
3877 { 3685 {
3878 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3686 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3879 {
3880 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); 3687 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code));
3881 }
3882 3688
3883 s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); 3689 s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code));
3884 SCHEME_V->code = cadar (SCHEME_V->code); 3690 SCHEME_V->code = cadar (SCHEME_V->code);
3885 SCHEME_V->args = NIL; 3691 SCHEME_V->args = NIL;
3886 s_goto (OP_EVAL); 3692 s_goto (OP_EVAL);
3896 case OP_LET2: /* let */ 3702 case OP_LET2: /* let */
3897 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 3703 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3898 3704
3899 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; 3705 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args;
3900 y != NIL; x = cdr (x), y = cdr (y)) 3706 y != NIL; x = cdr (x), y = cdr (y))
3901 {
3902 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3707 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3903 }
3904 3708
3905 if (is_symbol (car (SCHEME_V->code))) /* named let */ 3709 if (is_symbol (car (SCHEME_V->code))) /* named let */
3906 { 3710 {
3907 for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) 3711 for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x))
3908 { 3712 {
3978 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3782 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args);
3979 3783
3980 if (is_pair (SCHEME_V->code)) /* continue */ 3784 if (is_pair (SCHEME_V->code)) /* continue */
3981 { 3785 {
3982 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3786 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3983 {
3984 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); 3787 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3985 }
3986 3788
3987 s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code)); 3789 s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code));
3988 SCHEME_V->code = cadar (SCHEME_V->code); 3790 SCHEME_V->code = cadar (SCHEME_V->code);
3989 SCHEME_V->args = NIL; 3791 SCHEME_V->args = NIL;
3990 s_goto (OP_EVAL); 3792 s_goto (OP_EVAL);
3997 s_goto (OP_LET2REC); 3799 s_goto (OP_LET2REC);
3998 } 3800 }
3999 3801
4000 case OP_LET2REC: /* letrec */ 3802 case OP_LET2REC: /* letrec */
4001 for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y)) 3803 for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y))
4002 {
4003 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3804 new_slot_in_env (SCHEME_A_ caar (x), car (y));
4004 }
4005 3805
4006 SCHEME_V->code = cdr (SCHEME_V->code); 3806 SCHEME_V->code = cdr (SCHEME_V->code);
4007 SCHEME_V->args = NIL; 3807 SCHEME_V->args = NIL;
4008 s_goto (OP_BEGIN); 3808 s_goto (OP_BEGIN);
4009 3809
4010 case OP_COND0: /* cond */ 3810 case OP_COND0: /* cond */
4011 if (!is_pair (SCHEME_V->code)) 3811 if (!is_pair (SCHEME_V->code))
4012 {
4013 Error_0 ("syntax error in cond"); 3812 Error_0 ("syntax error in cond");
4014 }
4015 3813
4016 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 3814 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
4017 SCHEME_V->code = caar (SCHEME_V->code); 3815 SCHEME_V->code = caar (SCHEME_V->code);
4018 s_goto (OP_EVAL); 3816 s_goto (OP_EVAL);
4019 3817
4020 case OP_COND1: /* cond */ 3818 case OP_COND1: /* cond */
4021 if (is_true (SCHEME_V->value)) 3819 if (is_true (SCHEME_V->value))
4022 { 3820 {
4023 if ((SCHEME_V->code = cdar (SCHEME_V->code)) == NIL) 3821 if ((SCHEME_V->code = cdar (SCHEME_V->code)) == NIL)
4024 {
4025 s_return (SCHEME_V->value); 3822 s_return (SCHEME_V->value);
4026 }
4027 3823
4028 if (car (SCHEME_V->code) == SCHEME_V->FEED_TO) 3824 if (car (SCHEME_V->code) == SCHEME_V->FEED_TO)
4029 { 3825 {
4030 if (!is_pair (cdr (SCHEME_V->code))) 3826 if (!is_pair (cdr (SCHEME_V->code)))
4031 {
4032 Error_0 ("syntax error in cond"); 3827 Error_0 ("syntax error in cond");
4033 }
4034 3828
4035 x = cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)); 3829 x = cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL));
4036 SCHEME_V->code = cons (cadr (SCHEME_V->code), cons (x, NIL)); 3830 SCHEME_V->code = cons (cadr (SCHEME_V->code), cons (x, NIL));
4037 s_goto (OP_EVAL); 3831 s_goto (OP_EVAL);
4038 } 3832 }
4040 s_goto (OP_BEGIN); 3834 s_goto (OP_BEGIN);
4041 } 3835 }
4042 else 3836 else
4043 { 3837 {
4044 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) 3838 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL)
4045 {
4046 s_return (NIL); 3839 s_return (NIL);
4047 }
4048 else 3840 else
4049 { 3841 {
4050 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 3842 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
4051 SCHEME_V->code = caar (SCHEME_V->code); 3843 SCHEME_V->code = caar (SCHEME_V->code);
4052 s_goto (OP_EVAL); 3844 s_goto (OP_EVAL);
4058 set_typeflag (x, T_PROMISE); 3850 set_typeflag (x, T_PROMISE);
4059 s_return (x); 3851 s_return (x);
4060 3852
4061 case OP_AND0: /* and */ 3853 case OP_AND0: /* and */
4062 if (SCHEME_V->code == NIL) 3854 if (SCHEME_V->code == NIL)
4063 {
4064 s_return (S_T); 3855 s_return (S_T);
4065 }
4066 3856
4067 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 3857 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
4068 SCHEME_V->code = car (SCHEME_V->code); 3858 SCHEME_V->code = car (SCHEME_V->code);
4069 s_goto (OP_EVAL); 3859 s_goto (OP_EVAL);
4070 3860
4071 case OP_AND1: /* and */ 3861 case OP_AND1: /* and */
4072 if (is_false (SCHEME_V->value)) 3862 if (is_false (SCHEME_V->value))
4073 {
4074 s_return (SCHEME_V->value); 3863 s_return (SCHEME_V->value);
4075 }
4076 else if (SCHEME_V->code == NIL) 3864 else if (SCHEME_V->code == NIL)
4077 {
4078 s_return (SCHEME_V->value); 3865 s_return (SCHEME_V->value);
4079 }
4080 else 3866 else
4081 { 3867 {
4082 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 3868 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
4083 SCHEME_V->code = car (SCHEME_V->code); 3869 SCHEME_V->code = car (SCHEME_V->code);
4084 s_goto (OP_EVAL); 3870 s_goto (OP_EVAL);
4085 } 3871 }
4086 3872
4087 case OP_OR0: /* or */ 3873 case OP_OR0: /* or */
4088 if (SCHEME_V->code == NIL) 3874 if (SCHEME_V->code == NIL)
4089 {
4090 s_return (S_F); 3875 s_return (S_F);
4091 }
4092 3876
4093 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 3877 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
4094 SCHEME_V->code = car (SCHEME_V->code); 3878 SCHEME_V->code = car (SCHEME_V->code);
4095 s_goto (OP_EVAL); 3879 s_goto (OP_EVAL);
4096 3880
4097 case OP_OR1: /* or */ 3881 case OP_OR1: /* or */
4098 if (is_true (SCHEME_V->value)) 3882 if (is_true (SCHEME_V->value))
4099 {
4100 s_return (SCHEME_V->value); 3883 s_return (SCHEME_V->value);
4101 }
4102 else if (SCHEME_V->code == NIL) 3884 else if (SCHEME_V->code == NIL)
4103 {
4104 s_return (SCHEME_V->value); 3885 s_return (SCHEME_V->value);
4105 }
4106 else 3886 else
4107 { 3887 {
4108 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 3888 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
4109 SCHEME_V->code = car (SCHEME_V->code); 3889 SCHEME_V->code = car (SCHEME_V->code);
4110 s_goto (OP_EVAL); 3890 s_goto (OP_EVAL);
4132 x = car (SCHEME_V->code); 3912 x = car (SCHEME_V->code);
4133 SCHEME_V->code = cadr (SCHEME_V->code); 3913 SCHEME_V->code = cadr (SCHEME_V->code);
4134 } 3914 }
4135 3915
4136 if (!is_symbol (x)) 3916 if (!is_symbol (x))
4137 {
4138 Error_0 ("variable is not a symbol"); 3917 Error_0 ("variable is not a symbol");
4139 }
4140 3918
4141 s_save (SCHEME_A_ OP_MACRO1, NIL, x); 3919 s_save (SCHEME_A_ OP_MACRO1, NIL, x);
4142 s_goto (OP_EVAL); 3920 s_goto (OP_EVAL);
4143 3921
4144 case OP_MACRO1: /* macro */ 3922 case OP_MACRO1: /* macro */
4145 set_typeflag (SCHEME_V->value, T_MACRO); 3923 set_typeflag (SCHEME_V->value, T_MACRO);
4146 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); 3924 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0);
4147 3925
4148 if (x != NIL) 3926 if (x != NIL)
4149 {
4150 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); 3927 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value);
4151 }
4152 else 3928 else
4153 {
4154 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); 3929 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value);
4155 }
4156 3930
4157 s_return (SCHEME_V->code); 3931 s_return (SCHEME_V->code);
4158 3932
4159 case OP_CASE0: /* case */ 3933 case OP_CASE0: /* case */
4160 s_save (SCHEME_A_ OP_CASE1, NIL, cdr (SCHEME_V->code)); 3934 s_save (SCHEME_A_ OP_CASE1, NIL, cdr (SCHEME_V->code));
4163 3937
4164 case OP_CASE1: /* case */ 3938 case OP_CASE1: /* case */
4165 for (x = SCHEME_V->code; x != NIL; x = cdr (x)) 3939 for (x = SCHEME_V->code; x != NIL; x = cdr (x))
4166 { 3940 {
4167 if (!is_pair (y = caar (x))) 3941 if (!is_pair (y = caar (x)))
4168 {
4169 break; 3942 break;
4170 }
4171 3943
4172 for (; y != NIL; y = cdr (y)) 3944 for (; y != NIL; y = cdr (y))
4173 { 3945 {
4174 if (eqv (car (y), SCHEME_V->value)) 3946 if (eqv (car (y), SCHEME_V->value))
4175 {
4176 break; 3947 break;
4177 }
4178 } 3948 }
4179 3949
4180 if (y != NIL) 3950 if (y != NIL)
4181 {
4182 break; 3951 break;
4183 }
4184 } 3952 }
4185 3953
4186 if (x != NIL) 3954 if (x != NIL)
4187 { 3955 {
4188 if (is_pair (caar (x))) 3956 if (is_pair (caar (x)))
4196 SCHEME_V->code = caar (x); 3964 SCHEME_V->code = caar (x);
4197 s_goto (OP_EVAL); 3965 s_goto (OP_EVAL);
4198 } 3966 }
4199 } 3967 }
4200 else 3968 else
4201 {
4202 s_return (NIL); 3969 s_return (NIL);
4203 }
4204 3970
4205 case OP_CASE2: /* case */ 3971 case OP_CASE2: /* case */
4206 if (is_true (SCHEME_V->value)) 3972 if (is_true (SCHEME_V->value))
4207 {
4208 s_goto (OP_BEGIN); 3973 s_goto (OP_BEGIN);
4209 }
4210 else 3974 else
4211 {
4212 s_return (NIL); 3975 s_return (NIL);
4213 }
4214 3976
4215 case OP_PAPPLY: /* apply */ 3977 case OP_PAPPLY: /* apply */
4216 SCHEME_V->code = car (SCHEME_V->args); 3978 SCHEME_V->code = car (SCHEME_V->args);
4217 SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); 3979 SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args));
4218 /*SCHEME_V->args = cadr(SCHEME_V->args); */ 3980 /*SCHEME_V->args = cadr(SCHEME_V->args); */
4219 s_goto (OP_APPLY); 3981 s_goto (OP_APPLY);
4220 3982
4221 case OP_PEVAL: /* eval */ 3983 case OP_PEVAL: /* eval */
4222 if (cdr (SCHEME_V->args) != NIL) 3984 if (cdr (SCHEME_V->args) != NIL)
4223 {
4224 SCHEME_V->envir = cadr (SCHEME_V->args); 3985 SCHEME_V->envir = cadr (SCHEME_V->args);
4225 }
4226 3986
4227 SCHEME_V->code = car (SCHEME_V->args); 3987 SCHEME_V->code = car (SCHEME_V->args);
4228 s_goto (OP_EVAL); 3988 s_goto (OP_EVAL);
4229 3989
4230 case OP_CONTINUATION: /* call-with-current-continuation */ 3990 case OP_CONTINUATION: /* call-with-current-continuation */
4252 4012
4253 case OP_INEX2EX: /* inexact->exact */ 4013 case OP_INEX2EX: /* inexact->exact */
4254 x = car (SCHEME_V->args); 4014 x = car (SCHEME_V->args);
4255 4015
4256 if (num_is_integer (x)) 4016 if (num_is_integer (x))
4257 {
4258 s_return (x); 4017 s_return (x);
4259 }
4260 else if (modf (rvalue_unchecked (x), &dd) == 0.0) 4018 else if (modf (rvalue_unchecked (x), &dd) == 0.0)
4261 {
4262 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4019 s_return (mk_integer (SCHEME_A_ ivalue (x)));
4263 }
4264 else 4020 else
4265 {
4266 Error_1 ("inexact->exact: not integral:", x); 4021 Error_1 ("inexact->exact: not integral:", x);
4267 }
4268 4022
4269 case OP_EXP: 4023 case OP_EXP:
4270 x = car (SCHEME_V->args); 4024 x = car (SCHEME_V->args);
4271 s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4025 s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
4272 4026
4296 4050
4297 case OP_ATAN: 4051 case OP_ATAN:
4298 x = car (SCHEME_V->args); 4052 x = car (SCHEME_V->args);
4299 4053
4300 if (cdr (SCHEME_V->args) == NIL) 4054 if (cdr (SCHEME_V->args) == NIL)
4301 {
4302 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 4055 s_return (mk_real (SCHEME_A_ atan (rvalue (x))));
4303 }
4304 else 4056 else
4305 { 4057 {
4306 pointer y = cadr (SCHEME_V->args); 4058 pointer y = cadr (SCHEME_V->args);
4307 4059
4308 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); 4060 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
4324 real_result = 0; 4076 real_result = 0;
4325 4077
4326 /* This 'if' is an R5RS compatibility fix. */ 4078 /* This 'if' is an R5RS compatibility fix. */
4327 /* NOTE: Remove this 'if' fix for R6RS. */ 4079 /* NOTE: Remove this 'if' fix for R6RS. */
4328 if (rvalue (x) == 0 && rvalue (y) < 0) 4080 if (rvalue (x) == 0 && rvalue (y) < 0)
4329 {
4330 result = 0.0; 4081 result = 0.0;
4331 }
4332 else 4082 else
4333 {
4334 result = pow (rvalue (x), rvalue (y)); 4083 result = pow (rvalue (x), rvalue (y));
4335 }
4336 4084
4337 /* Before returning integer result make sure we can. */ 4085 /* Before returning integer result make sure we can. */
4338 /* If the test fails, result is too big for integer. */ 4086 /* If the test fails, result is too big for integer. */
4339 if (!real_result) 4087 if (!real_result)
4340 { 4088 {
4343 if (result != (RVALUE) result_as_long) 4091 if (result != (RVALUE) result_as_long)
4344 real_result = 1; 4092 real_result = 1;
4345 } 4093 }
4346 4094
4347 if (real_result) 4095 if (real_result)
4348 {
4349 s_return (mk_real (SCHEME_A_ result)); 4096 s_return (mk_real (SCHEME_A_ result));
4350 }
4351 else 4097 else
4352 {
4353 s_return (mk_integer (SCHEME_A_ result)); 4098 s_return (mk_integer (SCHEME_A_ result));
4354 }
4355 } 4099 }
4356 4100
4357 case OP_FLOOR: 4101 case OP_FLOOR:
4358 x = car (SCHEME_V->args); 4102 x = car (SCHEME_V->args);
4359 s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4103 s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4368 4112
4369 x = car (SCHEME_V->args); 4113 x = car (SCHEME_V->args);
4370 rvalue_of_x = rvalue (x); 4114 rvalue_of_x = rvalue (x);
4371 4115
4372 if (rvalue_of_x > 0) 4116 if (rvalue_of_x > 0)
4373 {
4374 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); 4117 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x)));
4375 }
4376 else 4118 else
4377 {
4378 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); 4119 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
4379 }
4380 } 4120 }
4381 4121
4382 case OP_ROUND: 4122 case OP_ROUND:
4383 x = car (SCHEME_V->args); 4123 x = car (SCHEME_V->args);
4384 4124
4512 } 4252 }
4513 else 4253 else
4514 Error_0 ("set-cdr!: unable to alter immutable pair"); 4254 Error_0 ("set-cdr!: unable to alter immutable pair");
4515 4255
4516 case OP_CHAR2INT: /* char->integer */ 4256 case OP_CHAR2INT: /* char->integer */
4517 { 4257 s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args))));
4518 char c;
4519
4520 c = (char) ivalue (car (SCHEME_V->args));
4521 s_return (mk_integer (SCHEME_A_ (unsigned char) c));
4522 }
4523 4258
4524 case OP_INT2CHAR: /* integer->char */ 4259 case OP_INT2CHAR: /* integer->char */
4525 {
4526 unsigned char c;
4527
4528 c = (unsigned char) ivalue (car (SCHEME_V->args));
4529 s_return (mk_character (SCHEME_A_ (char) c)); 4260 s_return (mk_character (SCHEME_A_ ivalue (car (SCHEME_V->args))));
4530 }
4531 4261
4532 case OP_CHARUPCASE: 4262 case OP_CHARUPCASE:
4533 { 4263 {
4534 unsigned char c;
4535
4536 c = (unsigned char) ivalue (car (SCHEME_V->args)); 4264 unsigned char c = ivalue (car (SCHEME_V->args));
4537 c = toupper (c); 4265 c = toupper (c);
4538 s_return (mk_character (SCHEME_A_ (char) c)); 4266 s_return (mk_character (SCHEME_A_ c));
4539 } 4267 }
4540 4268
4541 case OP_CHARDNCASE: 4269 case OP_CHARDNCASE:
4542 { 4270 {
4543 unsigned char c;
4544
4545 c = (unsigned char) ivalue (car (SCHEME_V->args)); 4271 unsigned char c = ivalue (car (SCHEME_V->args));
4546 c = tolower (c); 4272 c = tolower (c);
4547 s_return (mk_character (SCHEME_A_ (char) c)); 4273 s_return (mk_character (SCHEME_A_ c));
4548 } 4274 }
4549 4275
4550 case OP_STR2SYM: /* string->symbol */ 4276 case OP_STR2SYM: /* string->symbol */
4551 s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); 4277 s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args))));
4552 4278
4564 if (pf == 16 || pf == 10 || pf == 8 || pf == 2) 4290 if (pf == 16 || pf == 10 || pf == 8 || pf == 2)
4565 { 4291 {
4566 /* base is OK */ 4292 /* base is OK */
4567 } 4293 }
4568 else 4294 else
4569 {
4570 pf = -1; 4295 pf = -1;
4571 }
4572 } 4296 }
4573 4297
4574 if (pf < 0) 4298 if (pf < 0)
4575 {
4576 Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); 4299 Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args));
4577 }
4578 else if (*s == '#') /* no use of base! */ 4300 else if (*s == '#') /* no use of base! */
4579 {
4580 s_return (mk_sharp_const (SCHEME_A_ s + 1)); 4301 s_return (mk_sharp_const (SCHEME_A_ s + 1));
4581 }
4582 else 4302 else
4583 { 4303 {
4584 if (pf == 0 || pf == 10) 4304 if (pf == 0 || pf == 10)
4585 {
4586 s_return (mk_atom (SCHEME_A_ s)); 4305 s_return (mk_atom (SCHEME_A_ s));
4587 }
4588 else 4306 else
4589 { 4307 {
4590 char *ep; 4308 char *ep;
4591 long iv = strtol (s, &ep, (int) pf); 4309 long iv = strtol (s, &ep, (int) pf);
4592 4310
4593 if (*ep == 0) 4311 if (*ep == 0)
4594 {
4595 s_return (mk_integer (SCHEME_A_ iv)); 4312 s_return (mk_integer (SCHEME_A_ iv));
4596 }
4597 else 4313 else
4598 {
4599 s_return (S_F); 4314 s_return (S_F);
4600 }
4601 } 4315 }
4602 } 4316 }
4603 } 4317 }
4604 4318
4605 case OP_SYM2STR: /* symbol->string */ 4319 case OP_SYM2STR: /* symbol->string */
4622 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) 4336 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2))
4623 { 4337 {
4624 /* base is OK */ 4338 /* base is OK */
4625 } 4339 }
4626 else 4340 else
4627 {
4628 pf = -1; 4341 pf = -1;
4629 }
4630 } 4342 }
4631 4343
4632 if (pf < 0) 4344 if (pf < 0)
4633 {
4634 Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); 4345 Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args));
4635 }
4636 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) 4346 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x))
4637 { 4347 {
4638 char *p; 4348 char *p;
4639 int len; 4349 int len;
4640 4350
4641 atom2str (SCHEME_A_ x, (int) pf, &p, &len); 4351 atom2str (SCHEME_A_ x, pf, &p, &len);
4642 s_return (mk_counted_string (SCHEME_A_ p, len)); 4352 s_return (mk_counted_string (SCHEME_A_ p, len));
4643 } 4353 }
4644 else 4354 else
4645 {
4646 Error_1 ("atom->string: not an atom:", x); 4355 Error_1 ("atom->string: not an atom:", x);
4647 }
4648 } 4356 }
4649 4357
4650 case OP_MKSTRING: /* make-string */ 4358 case OP_MKSTRING: /* make-string */
4651 { 4359 {
4652 int fill = ' '; 4360 int fill = ' ';
4653 int len; 4361 int len;
4654 4362
4655 len = ivalue (car (SCHEME_V->args)); 4363 len = ivalue (car (SCHEME_V->args));
4656 4364
4657 if (cdr (SCHEME_V->args) != NIL) 4365 if (cdr (SCHEME_V->args) != NIL)
4658 {
4659 fill = charvalue (cadr (SCHEME_V->args)); 4366 fill = charvalue (cadr (SCHEME_V->args));
4660 }
4661 4367
4662 s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); 4368 s_return (mk_empty_string (SCHEME_A_ len, (char) fill));
4663 } 4369 }
4664 4370
4665 case OP_STRLEN: /* string-length */ 4371 case OP_STRLEN: /* string-length */
4673 str = strvalue (car (SCHEME_V->args)); 4379 str = strvalue (car (SCHEME_V->args));
4674 4380
4675 index = ivalue (cadr (SCHEME_V->args)); 4381 index = ivalue (cadr (SCHEME_V->args));
4676 4382
4677 if (index >= strlength (car (SCHEME_V->args))) 4383 if (index >= strlength (car (SCHEME_V->args)))
4678 {
4679 Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); 4384 Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args));
4680 }
4681 4385
4682 s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); 4386 s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index]));
4683 } 4387 }
4684 4388
4685 case OP_STRSET: /* string-set! */ 4389 case OP_STRSET: /* string-set! */
4687 char *str; 4391 char *str;
4688 int index; 4392 int index;
4689 int c; 4393 int c;
4690 4394
4691 if (is_immutable (car (SCHEME_V->args))) 4395 if (is_immutable (car (SCHEME_V->args)))
4692 {
4693 Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); 4396 Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args));
4694 }
4695 4397
4696 str = strvalue (car (SCHEME_V->args)); 4398 str = strvalue (car (SCHEME_V->args));
4697 4399
4698 index = ivalue (cadr (SCHEME_V->args)); 4400 index = ivalue (cadr (SCHEME_V->args));
4699 4401
4700 if (index >= strlength (car (SCHEME_V->args))) 4402 if (index >= strlength (car (SCHEME_V->args)))
4701 {
4702 Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); 4403 Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args));
4703 }
4704 4404
4705 c = charvalue (caddr (SCHEME_V->args)); 4405 c = charvalue (caddr (SCHEME_V->args));
4706 4406
4707 str[index] = (char) c; 4407 str[index] = (char) c;
4708 s_return (car (SCHEME_V->args)); 4408 s_return (car (SCHEME_V->args));
4715 pointer newstr; 4415 pointer newstr;
4716 char *pos; 4416 char *pos;
4717 4417
4718 /* compute needed length for new string */ 4418 /* compute needed length for new string */
4719 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4419 for (x = SCHEME_V->args; x != NIL; x = cdr (x))
4720 {
4721 len += strlength (car (x)); 4420 len += strlength (car (x));
4722 }
4723 4421
4724 newstr = mk_empty_string (SCHEME_A_ len, ' '); 4422 newstr = mk_empty_string (SCHEME_A_ len, ' ');
4725 4423
4726 /* store the contents of the argument strings into the new string */ 4424 /* store the contents of the argument strings into the new string */
4727 for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x)) 4425 for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x))
4728 {
4729 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4426 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4730 }
4731 4427
4732 s_return (newstr); 4428 s_return (newstr);
4733 } 4429 }
4734 4430
4735 case OP_SUBSTR: /* substring */ 4431 case OP_SUBSTR: /* substring */
4742 str = strvalue (car (SCHEME_V->args)); 4438 str = strvalue (car (SCHEME_V->args));
4743 4439
4744 index0 = ivalue (cadr (SCHEME_V->args)); 4440 index0 = ivalue (cadr (SCHEME_V->args));
4745 4441
4746 if (index0 > strlength (car (SCHEME_V->args))) 4442 if (index0 > strlength (car (SCHEME_V->args)))
4747 {
4748 Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); 4443 Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args));
4749 }
4750 4444
4751 if (cddr (SCHEME_V->args) != NIL) 4445 if (cddr (SCHEME_V->args) != NIL)
4752 { 4446 {
4753 index1 = ivalue (caddr (SCHEME_V->args)); 4447 index1 = ivalue (caddr (SCHEME_V->args));
4754 4448
4755 if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) 4449 if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0)
4756 {
4757 Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); 4450 Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args));
4758 }
4759 } 4451 }
4760 else 4452 else
4761 {
4762 index1 = strlength (car (SCHEME_V->args)); 4453 index1 = strlength (car (SCHEME_V->args));
4763 }
4764 4454
4765 len = index1 - index0; 4455 len = index1 - index0;
4766 x = mk_empty_string (SCHEME_A_ len, ' '); 4456 x = mk_empty_string (SCHEME_A_ len, ' ');
4767 memcpy (strvalue (x), str + index0, len); 4457 memcpy (strvalue (x), str + index0, len);
4768 strvalue (x)[len] = 0; 4458 strvalue (x)[len] = 0;
4775 int i; 4465 int i;
4776 pointer vec; 4466 pointer vec;
4777 int len = list_length (SCHEME_A_ SCHEME_V->args); 4467 int len = list_length (SCHEME_A_ SCHEME_V->args);
4778 4468
4779 if (len < 0) 4469 if (len < 0)
4780 {
4781 Error_1 ("vector: not a proper list:", SCHEME_V->args); 4470 Error_1 ("vector: not a proper list:", SCHEME_V->args);
4782 }
4783 4471
4784 vec = mk_vector (SCHEME_A_ len); 4472 vec = mk_vector (SCHEME_A_ len);
4785 4473
4786#if USE_ERROR_CHECKING 4474#if USE_ERROR_CHECKING
4787 if (SCHEME_V->no_memory) 4475 if (SCHEME_V->no_memory)
4817 4505
4818 s_return (vec); 4506 s_return (vec);
4819 } 4507 }
4820 4508
4821 case OP_VECLEN: /* vector-length */ 4509 case OP_VECLEN: /* vector-length */
4822 s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4510 s_return (mk_integer (SCHEME_A_ vector_length (car (SCHEME_V->args))));
4823 4511
4824 case OP_VECREF: /* vector-ref */ 4512 case OP_VECREF: /* vector-ref */
4825 { 4513 {
4826 int index; 4514 int index;
4827 4515
4828 index = ivalue (cadr (SCHEME_V->args)); 4516 index = ivalue (cadr (SCHEME_V->args));
4829 4517
4830 if (index >= ivalue (car (SCHEME_V->args))) 4518 if (index >= vector_length (car (SCHEME_V->args)) && USE_ERROR_CHECKING)
4831 {
4832 Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); 4519 Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args));
4833 }
4834 4520
4835 s_return (vector_elem (car (SCHEME_V->args), index)); 4521 s_return (vector_elem (car (SCHEME_V->args), index));
4836 } 4522 }
4837 4523
4838 case OP_VECSET: /* vector-set! */ 4524 case OP_VECSET: /* vector-set! */
4839 { 4525 {
4840 int index; 4526 int index;
4841 4527
4842 if (is_immutable (car (SCHEME_V->args))) 4528 if (is_immutable (car (SCHEME_V->args)))
4843 {
4844 Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); 4529 Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args));
4845 }
4846 4530
4847 index = ivalue (cadr (SCHEME_V->args)); 4531 index = ivalue (cadr (SCHEME_V->args));
4848 4532
4849 if (index >= ivalue (car (SCHEME_V->args))) 4533 if (index >= vector_length (car (SCHEME_V->args)) && USE_ERROR_CHECKING)
4850 {
4851 Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); 4534 Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args));
4852 }
4853 4535
4854 set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); 4536 set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args));
4855 s_return (car (SCHEME_V->args)); 4537 s_return (car (SCHEME_V->args));
4856 } 4538 }
4857 } 4539 }
4967 x = cdr (x); 4649 x = cdr (x);
4968 4650
4969 for (; x != NIL; x = cdr (x)) 4651 for (; x != NIL; x = cdr (x))
4970 { 4652 {
4971 if (!comp_func (v, nvalue (car (x)))) 4653 if (!comp_func (v, nvalue (car (x))))
4972 {
4973 s_retbool (0); 4654 s_retbool (0);
4974 }
4975 4655
4976 v = nvalue (car (x)); 4656 v = nvalue (car (x));
4977 } 4657 }
4978 4658
4979 s_retbool (1); 4659 s_retbool (1);
5072 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); 4752 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code);
5073 SCHEME_V->args = NIL; 4753 SCHEME_V->args = NIL;
5074 s_goto (OP_APPLY); 4754 s_goto (OP_APPLY);
5075 } 4755 }
5076 else 4756 else
5077 {
5078 s_return (SCHEME_V->code); 4757 s_return (SCHEME_V->code);
5079 }
5080 4758
5081 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4759 case OP_SAVE_FORCED: /* Save forced value replacing promise */
5082 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); 4760 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell));
5083 s_return (SCHEME_V->value); 4761 s_return (SCHEME_V->value);
5084 4762
5148 else 4826 else
5149 { 4827 {
5150 putstr (SCHEME_A_ "\n"); 4828 putstr (SCHEME_A_ "\n");
5151 4829
5152 if (SCHEME_V->interactive_repl) 4830 if (SCHEME_V->interactive_repl)
5153 {
5154 s_goto (OP_T0LVL); 4831 s_goto (OP_T0LVL);
5155 }
5156 else 4832 else
5157 {
5158 return NIL; 4833 return NIL;
5159 }
5160 } 4834 }
5161 4835
5162 case OP_REVERSE: /* reverse */ 4836 case OP_REVERSE: /* reverse */
5163 s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); 4837 s_return (reverse (SCHEME_A_ car (SCHEME_V->args)));
5164 4838
5187 4861
5188#if USE_PLIST 4862#if USE_PLIST
5189 4863
5190 case OP_PUT: /* put */ 4864 case OP_PUT: /* put */
5191 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 4865 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args)))
5192 {
5193 Error_0 ("illegal use of put"); 4866 Error_0 ("illegal use of put");
5194 }
5195 4867
5196 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 4868 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x))
5197 { 4869 {
5198 if (caar (x) == y) 4870 if (caar (x) == y)
5199 {
5200 break; 4871 break;
5201 }
5202 } 4872 }
5203 4873
5204 if (x != NIL) 4874 if (x != NIL)
5205 cdar (x) = caddr (SCHEME_V->args); 4875 cdar (x) = caddr (SCHEME_V->args);
5206 else 4876 else
5283 } 4953 }
5284 4954
5285 p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); 4955 p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop);
5286 4956
5287 if (p == NIL) 4957 if (p == NIL)
5288 {
5289 s_return (S_F); 4958 s_return (S_F);
5290 }
5291 4959
5292 s_return (p); 4960 s_return (p);
5293 } 4961 }
5294 4962
5295# if USE_STRING_PORTS 4963# if USE_STRING_PORTS
5313 4981
5314 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4982 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)),
5315 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); 4983 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop);
5316 4984
5317 if (p == NIL) 4985 if (p == NIL)
5318 {
5319 s_return (S_F); 4986 s_return (S_F);
5320 }
5321 4987
5322 s_return (p); 4988 s_return (p);
5323 } 4989 }
5324 4990
5325 case OP_OPEN_OUTSTRING: /* open-output-string */ 4991 case OP_OPEN_OUTSTRING: /* open-output-string */
5329 if (car (SCHEME_V->args) == NIL) 4995 if (car (SCHEME_V->args) == NIL)
5330 { 4996 {
5331 p = port_from_scratch (SCHEME_A); 4997 p = port_from_scratch (SCHEME_A);
5332 4998
5333 if (p == NIL) 4999 if (p == NIL)
5334 {
5335 s_return (S_F); 5000 s_return (S_F);
5336 }
5337 } 5001 }
5338 else 5002 else
5339 { 5003 {
5340 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 5004 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)),
5341 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); 5005 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output);
5342 5006
5343 if (p == NIL) 5007 if (p == NIL)
5344 {
5345 s_return (S_F); 5008 s_return (S_F);
5346 }
5347 } 5009 }
5348 5010
5349 s_return (p); 5011 s_return (p);
5350 } 5012 }
5351 5013
5416 { 5078 {
5417 /* ========== reading part ========== */ 5079 /* ========== reading part ========== */
5418#if USE_PORTS 5080#if USE_PORTS
5419 case OP_READ: 5081 case OP_READ:
5420 if (!is_pair (SCHEME_V->args)) 5082 if (!is_pair (SCHEME_V->args))
5421 {
5422 s_goto (OP_READ_INTERNAL); 5083 s_goto (OP_READ_INTERNAL);
5423 }
5424 5084
5425 if (!is_inport (car (SCHEME_V->args))) 5085 if (!is_inport (car (SCHEME_V->args)))
5426 {
5427 Error_1 ("read: not an input port:", car (SCHEME_V->args)); 5086 Error_1 ("read: not an input port:", car (SCHEME_V->args));
5428 }
5429 5087
5430 if (car (SCHEME_V->args) == SCHEME_V->inport) 5088 if (car (SCHEME_V->args) == SCHEME_V->inport)
5431 {
5432 s_goto (OP_READ_INTERNAL); 5089 s_goto (OP_READ_INTERNAL);
5433 }
5434 5090
5435 x = SCHEME_V->inport; 5091 x = SCHEME_V->inport;
5436 SCHEME_V->inport = car (SCHEME_V->args); 5092 SCHEME_V->inport = car (SCHEME_V->args);
5437 x = cons (x, NIL); 5093 x = cons (x, NIL);
5438 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 5094 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5469 { 5125 {
5470 pointer p = SCHEME_V->inport; 5126 pointer p = SCHEME_V->inport;
5471 int res; 5127 int res;
5472 5128
5473 if (is_pair (SCHEME_V->args)) 5129 if (is_pair (SCHEME_V->args))
5474 {
5475 p = car (SCHEME_V->args); 5130 p = car (SCHEME_V->args);
5476 }
5477 5131
5478 res = p->object.port->kind & port_string; 5132 res = p->object.port->kind & port_string;
5479 5133
5480 s_retbool (res); 5134 s_retbool (res);
5481 } 5135 }
5496 s_return (S_EOF); 5150 s_return (S_EOF);
5497 /* NOTREACHED */ 5151 /* NOTREACHED */
5498 5152
5499 case TOK_VEC: 5153 case TOK_VEC:
5500 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); 5154 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL);
5501
5502 /* fall through */ 5155 /* fall through */
5156
5503 case TOK_LPAREN: 5157 case TOK_LPAREN:
5504 SCHEME_V->tok = token (SCHEME_A); 5158 SCHEME_V->tok = token (SCHEME_A);
5505 5159
5506 if (SCHEME_V->tok == TOK_RPAREN) 5160 if (SCHEME_V->tok == TOK_RPAREN)
5507 s_return (NIL); 5161 s_return (NIL);
5527 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); 5181 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL);
5528 SCHEME_V->tok = TOK_LPAREN; 5182 SCHEME_V->tok = TOK_LPAREN;
5529 s_goto (OP_RDSEXPR); 5183 s_goto (OP_RDSEXPR);
5530 } 5184 }
5531 else 5185 else
5532 {
5533 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); 5186 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL);
5534 }
5535 5187
5536 s_goto (OP_RDSEXPR); 5188 s_goto (OP_RDSEXPR);
5537 5189
5538 case TOK_COMMA: 5190 case TOK_COMMA:
5539 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); 5191 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL);
5581 } 5233 }
5582 5234
5583 break; 5235 break;
5584 5236
5585 case OP_RDLIST: 5237 case OP_RDLIST:
5586 {
5587 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 5238 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args);
5588 SCHEME_V->tok = token (SCHEME_A); 5239 SCHEME_V->tok = token (SCHEME_A);
5589 5240
5590 if (SCHEME_V->tok == TOK_EOF) 5241 switch (SCHEME_V->tok)
5242 {
5243 case TOK_EOF:
5591 s_return (S_EOF); 5244 s_return (S_EOF);
5592 else if (SCHEME_V->tok == TOK_RPAREN) 5245
5246 case TOK_RPAREN:
5593 { 5247 {
5594 int c = inchar (SCHEME_A); 5248 int c = inchar (SCHEME_A);
5595 5249
5596 if (c != '\n') 5250 if (c != '\n')
5597 backchar (SCHEME_A_ c); 5251 backchar (SCHEME_A_ c);
5598
5599#if SHOW_ERROR_LINE 5252#if SHOW_ERROR_LINE
5600 else if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) 5253 else if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file)
5601 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line++; 5254 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line++;
5602
5603#endif 5255#endif
5256
5604 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5257 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5605 s_return (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args)); 5258 s_return (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args));
5606 } 5259 }
5607 else if (SCHEME_V->tok == TOK_DOT) 5260
5608 { 5261 case TOK_DOT:
5609 s_save (SCHEME_A_ OP_RDDOT, SCHEME_V->args, NIL); 5262 s_save (SCHEME_A_ OP_RDDOT, SCHEME_V->args, NIL);
5610 SCHEME_V->tok = token (SCHEME_A); 5263 SCHEME_V->tok = token (SCHEME_A);
5611 s_goto (OP_RDSEXPR); 5264 s_goto (OP_RDSEXPR);
5612 } 5265
5613 else 5266 default:
5614 {
5615 s_save (SCHEME_A_ OP_RDLIST, SCHEME_V->args, NIL);; 5267 s_save (SCHEME_A_ OP_RDLIST, SCHEME_V->args, NIL);
5616 s_goto (OP_RDSEXPR); 5268 s_goto (OP_RDSEXPR);
5617 } 5269 }
5618 }
5619 5270
5620 case OP_RDDOT: 5271 case OP_RDDOT:
5621 if (token (SCHEME_A) != TOK_RPAREN) 5272 if (token (SCHEME_A) != TOK_RPAREN)
5622 Error_0 ("syntax error: illegal dot expression"); 5273 Error_0 ("syntax error: illegal dot expression");
5623 else 5274
5624 {
5625 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5275 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5626 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); 5276 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args));
5627 }
5628 5277
5629 case OP_RDQUOTE: 5278 case OP_RDQUOTE:
5630 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); 5279 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)));
5631 5280
5632 case OP_RDQQUOTE: 5281 case OP_RDQQUOTE:
5732 5381
5733 case OP_PVECFROM: 5382 case OP_PVECFROM:
5734 { 5383 {
5735 int i = ivalue_unchecked (cdr (SCHEME_V->args)); 5384 int i = ivalue_unchecked (cdr (SCHEME_V->args));
5736 pointer vec = car (SCHEME_V->args); 5385 pointer vec = car (SCHEME_V->args);
5737 int len = ivalue_unchecked (vec); 5386 int len = vector_length (vec);
5738 5387
5739 if (i == len) 5388 if (i == len)
5740 { 5389 {
5741 putstr (SCHEME_A_ ")"); 5390 putstr (SCHEME_A_ ")");
5742 s_return (S_T); 5391 s_return (S_T);
5898{ 5547{
5899 int n = procnum (x); 5548 int n = procnum (x);
5900 const char *name = dispatch_table[n].name; 5549 const char *name = dispatch_table[n].name;
5901 5550
5902 if (name == 0) 5551 if (name == 0)
5903 {
5904 name = "ILLEGAL!"; 5552 name = "ILLEGAL!";
5905 }
5906 5553
5907 return name; 5554 return name;
5908} 5555}
5909 5556
5910/* kernel of this interpreter */ 5557/* kernel of this interpreter */
5915 5562
5916 for (;;) 5563 for (;;)
5917 { 5564 {
5918 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5565 op_code_info *pcd = dispatch_table + SCHEME_V->op;
5919 5566
5567#if USE_ERROR_CHECKING
5920 if (pcd->name != 0) /* if built-in function, check arguments */ 5568 if (pcd->name) /* if built-in function, check arguments */
5921 { 5569 {
5570 int ok = 1;
5922 char msg[STRBUFFSIZE]; 5571 char msg[STRBUFFSIZE];
5923 int ok = 1;
5924 int n = list_length (SCHEME_A_ SCHEME_V->args); 5572 int n = list_length (SCHEME_A_ SCHEME_V->args);
5925 5573
5926#if USE_ERROR_CHECKING
5927 /* Check number of arguments */ 5574 /* Check number of arguments */
5928 if (n < pcd->min_arity) 5575 if (n < pcd->min_arity)
5929 { 5576 {
5930 ok = 0; 5577 ok = 0;
5931 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5578 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5936 { 5583 {
5937 ok = 0; 5584 ok = 0;
5938 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5585 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5939 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); 5586 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity);
5940 } 5587 }
5941#endif
5942 5588
5943 if (ok) 5589 if (ok)
5944 { 5590 {
5945 if (pcd->arg_tests_encoding != 0) 5591 if (pcd->arg_tests_encoding)
5946 { 5592 {
5947 int i = 0; 5593 int i = 0;
5948 int j; 5594 int j;
5949 const char *t = pcd->arg_tests_encoding; 5595 const char *t = pcd->arg_tests_encoding;
5950 pointer arglist = SCHEME_V->args; 5596 pointer arglist = SCHEME_V->args;
5965 if (!tests[j].fct (arg)) 5611 if (!tests[j].fct (arg))
5966 break; 5612 break;
5967 } 5613 }
5968 5614
5969 if (t[1] != 0) /* last test is replicated as necessary */ 5615 if (t[1] != 0) /* last test is replicated as necessary */
5970 {
5971 t++; 5616 t++;
5972 }
5973 5617
5974 arglist = cdr (arglist); 5618 arglist = cdr (arglist);
5975 i++; 5619 i++;
5976 } 5620 }
5977 while (i < n); 5621 while (i < n);
5978 5622
5979#if USE_ERROR_CHECKING
5980 if (i < n) 5623 if (i < n)
5981 { 5624 {
5982 ok = 0; 5625 ok = 0;
5983 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); 5626 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind);
5984 } 5627 }
5985#endif
5986 } 5628 }
5987 } 5629 }
5988 5630
5989 if (!ok) 5631 if (!ok)
5990 { 5632 {
5992 return; 5634 return;
5993 5635
5994 pcd = dispatch_table + SCHEME_V->op; 5636 pcd = dispatch_table + SCHEME_V->op;
5995 } 5637 }
5996 } 5638 }
5639#endif
5997 5640
5998 ok_to_freely_gc (SCHEME_A); 5641 ok_to_freely_gc (SCHEME_A);
5999 5642
6000 if (pcd->func (SCHEME_A_ (enum scheme_opcodes) SCHEME_V->op) == NIL) 5643 if (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL)
6001 return; 5644 return;
6002 5645
6003#if USE_ERROR_CHECKING 5646#if USE_ERROR_CHECKING
6004 if (SCHEME_V->no_memory) 5647 if (SCHEME_V->no_memory)
6005 { 5648 {
6011} 5654}
6012 5655
6013/* ========== Initialization of internal keywords ========== */ 5656/* ========== Initialization of internal keywords ========== */
6014 5657
6015static void 5658static void
6016assign_syntax (SCHEME_P_ char *name) 5659assign_syntax (SCHEME_P_ const char *name)
6017{ 5660{
6018 pointer x = oblist_add_by_name (SCHEME_A_ name); 5661 pointer x = oblist_add_by_name (SCHEME_A_ name);
6019 set_typeflag (x, typeflag (x) | T_SYNTAX); 5662 set_typeflag (x, typeflag (x) | T_SYNTAX);
6020} 5663}
6021 5664
6022static void 5665static void
6023assign_proc (SCHEME_P_ enum scheme_opcodes op, char *name) 5666assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
6024{ 5667{
6025 pointer x = mk_symbol (SCHEME_A_ name); 5668 pointer x = mk_symbol (SCHEME_A_ name);
6026 pointer y = mk_proc (SCHEME_A_ op); 5669 pointer y = mk_proc (SCHEME_A_ op);
6027 new_slot_in_env (SCHEME_A_ x, y); 5670 new_slot_in_env (SCHEME_A_ x, y);
6028} 5671}
6030static pointer 5673static pointer
6031mk_proc (SCHEME_P_ enum scheme_opcodes op) 5674mk_proc (SCHEME_P_ enum scheme_opcodes op)
6032{ 5675{
6033 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5676 pointer y = get_cell (SCHEME_A_ NIL, NIL);
6034 set_typeflag (y, (T_PROC | T_ATOM)); 5677 set_typeflag (y, (T_PROC | T_ATOM));
6035 ivalue_unchecked (y) = (long) op; 5678 ivalue_unchecked (y) = op;
6036 set_num_integer (y); 5679 set_num_integer (y);
6037 return y; 5680 return y;
6038} 5681}
6039 5682
6040/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5683/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
6158 } 5801 }
6159 5802
6160 SCHEME_V->gc_verbose = 0; 5803 SCHEME_V->gc_verbose = 0;
6161 dump_stack_initialize (SCHEME_A); 5804 dump_stack_initialize (SCHEME_A);
6162 SCHEME_V->code = NIL; 5805 SCHEME_V->code = NIL;
5806 SCHEME_V->args = NIL;
5807 SCHEME_V->envir = NIL;
6163 SCHEME_V->tracing = 0; 5808 SCHEME_V->tracing = 0;
6164 5809
6165 /* init NIL */ 5810 /* init NIL */
6166 set_typeflag (NIL, T_ATOM | MARK); 5811 set_typeflag (NIL, T_ATOM | T_MARK);
6167 set_car (NIL, NIL); 5812 set_car (NIL, NIL);
6168 set_cdr (NIL, NIL); 5813 set_cdr (NIL, NIL);
6169 /* init T */ 5814 /* init T */
6170 set_typeflag (S_T, T_ATOM | MARK); 5815 set_typeflag (S_T, T_ATOM | T_MARK);
6171 set_car (S_T, S_T); 5816 set_car (S_T, S_T);
6172 set_cdr (S_T, S_T); 5817 set_cdr (S_T, S_T);
6173 /* init F */ 5818 /* init F */
6174 set_typeflag (S_F, T_ATOM | MARK); 5819 set_typeflag (S_F, T_ATOM | T_MARK);
6175 set_car (S_F, S_F); 5820 set_car (S_F, S_F);
6176 set_cdr (S_F, S_F); 5821 set_cdr (S_F, S_F);
6177 /* init sink */ 5822 /* init sink */
6178 set_typeflag (S_SINK, T_PAIR | MARK); 5823 set_typeflag (S_SINK, T_PAIR | T_MARK);
6179 set_car (S_SINK, NIL); 5824 set_car (S_SINK, NIL);
6180 /* init c_nest */ 5825 /* init c_nest */
6181 SCHEME_V->c_nest = NIL; 5826 SCHEME_V->c_nest = NIL;
6182 5827
6183 SCHEME_V->oblist = oblist_initial_value (SCHEME_A); 5828 SCHEME_V->oblist = oblist_initial_value (SCHEME_A);
6186 SCHEME_V->global_env = SCHEME_V->envir; 5831 SCHEME_V->global_env = SCHEME_V->envir;
6187 /* init else */ 5832 /* init else */
6188 x = mk_symbol (SCHEME_A_ "else"); 5833 x = mk_symbol (SCHEME_A_ "else");
6189 new_slot_in_env (SCHEME_A_ x, S_T); 5834 new_slot_in_env (SCHEME_A_ x, S_T);
6190 5835
6191 assign_syntax (SCHEME_A_ "lambda"); 5836 {
6192 assign_syntax (SCHEME_A_ "quote"); 5837 static const char *syntax_names[] = {
6193 assign_syntax (SCHEME_A_ "define"); 5838 "lambda", "quote", "define", "if", "begin", "set!",
6194 assign_syntax (SCHEME_A_ "if"); 5839 "let", "let*", "letrec", "cond", "delay", "and",
6195 assign_syntax (SCHEME_A_ "begin"); 5840 "or", "cons-stream", "macro", "case"
6196 assign_syntax (SCHEME_A_ "set!"); 5841 };
6197 assign_syntax (SCHEME_A_ "let"); 5842
6198 assign_syntax (SCHEME_A_ "let*"); 5843 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
6199 assign_syntax (SCHEME_A_ "letrec");
6200 assign_syntax (SCHEME_A_ "cond");
6201 assign_syntax (SCHEME_A_ "delay");
6202 assign_syntax (SCHEME_A_ "and"); 5844 assign_syntax (SCHEME_A_ syntax_names[i]);
6203 assign_syntax (SCHEME_A_ "or"); 5845 }
6204 assign_syntax (SCHEME_A_ "cons-stream");
6205 assign_syntax (SCHEME_A_ "macro");
6206 assign_syntax (SCHEME_A_ "case");
6207 5846
6208 for (i = 0; i < n; i++) 5847 for (i = 0; i < n; i++)
6209 {
6210 if (dispatch_table[i].name != 0) 5848 if (dispatch_table[i].name != 0)
6211 {
6212 assign_proc (SCHEME_A_ (enum scheme_opcodes) i, dispatch_table[i].name); 5849 assign_proc (SCHEME_A_ i, dispatch_table[i].name);
6213 }
6214 }
6215 5850
6216 /* initialization of global pointers to special symbols */ 5851 /* initialization of global pointers to special symbols */
6217 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 5852 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
6218 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 5853 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
6219 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 5854 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
6385 pointer x; 6020 pointer x;
6386 6021
6387 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 6022 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
6388 6023
6389 if (x != NIL) 6024 if (x != NIL)
6390 {
6391 set_slot_in_env (SCHEME_A_ x, value); 6025 set_slot_in_env (SCHEME_A_ x, value);
6392 }
6393 else 6026 else
6394 {
6395 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 6027 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
6396 }
6397} 6028}
6398 6029
6399#if !STANDALONE 6030#if !STANDALONE
6031
6400void 6032void
6401scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 6033scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
6402{ 6034{
6403 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); 6035 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f));
6404} 6036}
6407scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 6039scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
6408{ 6040{
6409 int i; 6041 int i;
6410 6042
6411 for (i = 0; i < count; i++) 6043 for (i = 0; i < count; i++)
6412 {
6413 scheme_register_foreign_func (SCHEME_A_ list + i); 6044 scheme_register_foreign_func (SCHEME_A_ list + i);
6414 }
6415} 6045}
6416 6046
6417pointer 6047pointer
6418scheme_apply0 (SCHEME_P_ const char *procname) 6048scheme_apply0 (SCHEME_P_ const char *procname)
6419{ 6049{
6476 SCHEME_V->interactive_repl = old_repl; 6106 SCHEME_V->interactive_repl = old_repl;
6477 restore_from_C_call (SCHEME_A); 6107 restore_from_C_call (SCHEME_A);
6478 return SCHEME_V->value; 6108 return SCHEME_V->value;
6479} 6109}
6480 6110
6481
6482#endif 6111#endif
6483 6112
6484/* ========== Main ========== */ 6113/* ========== Main ========== */
6485 6114
6486#if STANDALONE 6115#if STANDALONE
6505main (int argc, char **argv) 6134main (int argc, char **argv)
6506{ 6135{
6507# endif 6136# endif
6508# if USE_MULTIPLICITY 6137# if USE_MULTIPLICITY
6509 scheme ssc; 6138 scheme ssc;
6510 scheme *const sc = &ssc; 6139 scheme *const SCHEME_V = &ssc;
6511# else 6140# else
6512# endif 6141# endif
6513 int fin; 6142 int fin;
6514 char *file_name = InitFile; 6143 char *file_name = InitFile;
6515 int retcode; 6144 int retcode;
6516 int isfile = 1; 6145 int isfile = 1;
6517
6518 if (argc == 1)
6519 xwrstr (banner);
6520 6146
6521 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6147 if (argc == 2 && strcmp (argv[1], "-?") == 0)
6522 { 6148 {
6523 xwrstr ("Usage: tinyscheme -?\n"); 6149 xwrstr ("Usage: tinyscheme -?\n");
6524 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6150 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n");
6547 if (access (file_name, 0) != 0) 6173 if (access (file_name, 0) != 0)
6548 { 6174 {
6549 char *p = getenv ("TINYSCHEMEINIT"); 6175 char *p = getenv ("TINYSCHEMEINIT");
6550 6176
6551 if (p != 0) 6177 if (p != 0)
6552 {
6553 file_name = p; 6178 file_name = p;
6554 }
6555 } 6179 }
6556#endif 6180#endif
6557 6181
6558 do 6182 do
6559 { 6183 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines