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.51 by root, Tue Dec 1 01:54:27 2015 UTC vs.
Revision 1.65 by root, Wed Dec 2 17:01:51 2015 UTC

16 * (MINISCM) This is a revised and modified version by Akira KIDA. 16 * (MINISCM) This is a revised and modified version by Akira KIDA.
17 * (MINISCM) current version is 0.85k4 (15 May 1994) 17 * (MINISCM) current version is 0.85k4 (15 May 1994)
18 * 18 *
19 */ 19 */
20 20
21#define EXPERIMENT 1 21#define _POSIX_C_SOURCE 200201
22 22#define _XOPEN_SOURCE 600
23#define PAGE_SIZE 4096 /* does not work on sparc/alpha */ 23#define _GNU_SOURCE 1 /* for malloc mremap */
24#include "malloc.c"
25 24
26#define SCHEME_SOURCE 25#define SCHEME_SOURCE
27#include "scheme-private.h" 26#include "scheme-private.h"
28#ifndef WIN32 27#ifndef WIN32
29# include <unistd.h> 28# include <unistd.h>
30#endif 29#endif
31#if USE_MATH 30#if USE_MATH
32# include <math.h> 31# include <math.h>
33#endif 32#endif
34 33
34#define ECB_NO_THREADS 1
35#include "ecb.h" 35#include "ecb.h"
36 36
37#include <sys/types.h> 37#include <sys/types.h>
38#include <sys/stat.h> 38#include <sys/stat.h>
39#include <fcntl.h> 39#include <fcntl.h>
47#include <string.h> 47#include <string.h>
48 48
49#include <limits.h> 49#include <limits.h>
50#include <inttypes.h> 50#include <inttypes.h>
51#include <float.h> 51#include <float.h>
52//#include <ctype.h> 52
53#if !USE_SYSTEM_MALLOC
54# define PAGE_SIZE 4096 /* does not work on sparc/alpha */
55# include "malloc.c"
56# define malloc(n) tiny_malloc (n)
57# define realloc(p,n) tiny_realloc (p, n)
58# define free(p) tiny_free (p)
59#endif
53 60
54#if '1' != '0' + 1 \ 61#if '1' != '0' + 1 \
55 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ 62 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \
56 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ 63 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \
57 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ 64 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \
89 96
90#if !USE_MULTIPLICITY 97#if !USE_MULTIPLICITY
91static scheme sc; 98static scheme sc;
92#endif 99#endif
93 100
94static void 101ecb_cold static void
95xbase (char *s, long n, int base) 102xbase (char *s, long n, int base)
96{ 103{
97 if (n < 0) 104 if (n < 0)
98 { 105 {
99 *s++ = '-'; 106 *s++ = '-';
101 } 108 }
102 109
103 char *p = s; 110 char *p = s;
104 111
105 do { 112 do {
106 *p++ = '0' + n % base; 113 *p++ = "0123456789abcdef"[n % base];
107 n /= base; 114 n /= base;
108 } while (n); 115 } while (n);
109 116
110 *p-- = 0; 117 *p-- = 0;
111 118
114 char x = *s; *s = *p; *p = x; 121 char x = *s; *s = *p; *p = x;
115 --p; ++s; 122 --p; ++s;
116 } 123 }
117} 124}
118 125
119static void 126ecb_cold static void
120xnum (char *s, long n) 127xnum (char *s, long n)
121{ 128{
122 xbase (s, n, 10); 129 xbase (s, n, 10);
123} 130}
124 131
125static void 132ecb_cold static void
126xwrstr (const char *s) 133putnum (SCHEME_P_ long n)
127{
128 write (1, s, strlen (s));
129}
130
131static void
132xwrnum (long n)
133{ 134{
134 char buf[64]; 135 char buf[64];
135 136
136 xnum (buf, n); 137 xnum (buf, n);
137 xwrstr (buf); 138 putstr (SCHEME_A_ buf);
138} 139}
140
141#if USE_CHAR_CLASSIFIERS
142#include <ctype.h>
143#else
139 144
140static char 145static char
141xtoupper (char c) 146xtoupper (char c)
142{ 147{
143 if (c >= 'a' && c <= 'z') 148 if (c >= 'a' && c <= 'z')
163 168
164#define toupper(c) xtoupper (c) 169#define toupper(c) xtoupper (c)
165#define tolower(c) xtolower (c) 170#define tolower(c) xtolower (c)
166#define isdigit(c) xisdigit (c) 171#define isdigit(c) xisdigit (c)
167 172
173#endif
174
168#if USE_IGNORECASE 175#if USE_IGNORECASE
169static const char * 176ecb_cold static const char *
170xstrlwr (char *s) 177xstrlwr (char *s)
171{ 178{
172 const char *p = s; 179 const char *p = s;
173 180
174 while (*s) 181 while (*s)
187# define stricmp(a,b) strcmp (a, b) 194# define stricmp(a,b) strcmp (a, b)
188# define strlwr(s) (s) 195# define strlwr(s) (s)
189#endif 196#endif
190 197
191#ifndef prompt 198#ifndef prompt
192# define prompt "ts> " 199# define prompt "ms> "
193#endif 200#endif
194 201
195#ifndef InitFile 202#ifndef InitFile
196# define InitFile "init.scm" 203# define InitFile "init.scm"
197#endif 204#endif
198 205
199enum scheme_types 206enum scheme_types
200{ 207{
201 T_INTEGER, 208 T_INTEGER,
209 T_CHARACTER,
202 T_REAL, 210 T_REAL,
203 T_STRING, 211 T_STRING,
204 T_SYMBOL, 212 T_SYMBOL,
205 T_PROC, 213 T_PROC,
206 T_PAIR, /* also used for free cells */ 214 T_PAIR, /* also used for free cells */
207 T_CLOSURE, 215 T_CLOSURE,
216 T_BYTECODE, // temp
217 T_MACRO,
208 T_CONTINUATION, 218 T_CONTINUATION,
209 T_FOREIGN, 219 T_FOREIGN,
210 T_CHARACTER,
211 T_PORT, 220 T_PORT,
212 T_VECTOR, 221 T_VECTOR,
213 T_MACRO,
214 T_PROMISE, 222 T_PROMISE,
215 T_ENVIRONMENT, 223 T_ENVIRONMENT,
216 /* one more... */ 224
217 T_NUM_SYSTEM_TYPES 225 T_NUM_SYSTEM_TYPES
218}; 226};
219 227
220#define T_MASKTYPE 0x000f 228#define T_MASKTYPE 0x000f
221#define T_SYNTAX 0x0010 229#define T_SYNTAX 0x0010
254static num num_op (enum num_op op, num a, num b); 262static num num_op (enum num_op op, num a, num b);
255static num num_intdiv (num a, num b); 263static num num_intdiv (num a, num b);
256static num num_rem (num a, num b); 264static num num_rem (num a, num b);
257static num num_mod (num a, num b); 265static num num_mod (num a, num b);
258 266
259#if USE_MATH
260static double round_per_R5RS (double x);
261#endif
262static int is_zero_rvalue (RVALUE x); 267static int is_zero_rvalue (RVALUE x);
263 268
264static num num_zero; 269static num num_zero;
265static num num_one; 270static num num_one;
266 271
378 383
379static pointer cadar (pointer p) { return car (cdr (car (p))); } 384static pointer cadar (pointer p) { return car (cdr (car (p))); }
380static pointer caddr (pointer p) { return car (cdr (cdr (p))); } 385static pointer caddr (pointer p) { return car (cdr (cdr (p))); }
381static pointer cdaar (pointer p) { return cdr (car (car (p))); } 386static pointer cdaar (pointer p) { return cdr (car (car (p))); }
382 387
388static pointer cadddr (pointer p) { return car (car (car (cdr (p)))); }
389
383INTERFACE void 390INTERFACE void
384set_car (pointer p, pointer q) 391set_car (pointer p, pointer q)
385{ 392{
386 CELL(p)->object.cons.car = CELL (q); 393 CELL(p)->object.cons.car = CELL (q);
387} 394}
527 proper list: length 534 proper list: length
528 circular list: -1 535 circular list: -1
529 not even a pair: -2 536 not even a pair: -2
530 dotted list: -2 minus length before dot 537 dotted list: -2 minus length before dot
531*/ 538*/
532INTERFACE int 539ecb_hot INTERFACE int
533list_length (SCHEME_P_ pointer a) 540list_length (SCHEME_P_ pointer a)
534{ 541{
535 int i = 0; 542 int i = 0;
536 pointer slow, fast; 543 pointer slow, fast;
537 544
576{ 583{
577 return list_length (SCHEME_A_ a) >= 0; 584 return list_length (SCHEME_A_ a) >= 0;
578} 585}
579 586
580#if USE_CHAR_CLASSIFIERS 587#if USE_CHAR_CLASSIFIERS
588
581ecb_inline int 589ecb_inline int
582Cisalpha (int c) 590Cisalpha (int c)
583{ 591{
584 return isascii (c) && isalpha (c); 592 return isascii (c) && isalpha (c);
585} 593}
643 "gs", 651 "gs",
644 "rs", 652 "rs",
645 "us" 653 "us"
646}; 654};
647 655
648static int 656ecb_cold static int
649is_ascii_name (const char *name, int *pc) 657is_ascii_name (const char *name, int *pc)
650{ 658{
651 int i; 659 int i;
652 660
653 for (i = 0; i < 32; i++) 661 for (i = 0; i < 32; i++)
675static int file_interactive (SCHEME_P); 683static int file_interactive (SCHEME_P);
676ecb_inline int is_one_of (const char *s, int c); 684ecb_inline int is_one_of (const char *s, int c);
677static int alloc_cellseg (SCHEME_P); 685static int alloc_cellseg (SCHEME_P);
678ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); 686ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
679static void finalize_cell (SCHEME_P_ pointer a); 687static void finalize_cell (SCHEME_P_ pointer a);
680static int count_consecutive_cells (pointer x, int needed);
681static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 688static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
682static pointer mk_number (SCHEME_P_ const num n); 689static pointer mk_number (SCHEME_P_ const num n);
683static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); 690static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
684static pointer mk_vector (SCHEME_P_ uint32_t len); 691static pointer mk_vector (SCHEME_P_ uint32_t len);
685static pointer mk_atom (SCHEME_P_ char *q); 692static pointer mk_atom (SCHEME_P_ char *q);
686static pointer mk_sharp_const (SCHEME_P_ char *name); 693static pointer mk_sharp_const (SCHEME_P_ char *name);
687 694
695static pointer mk_port (SCHEME_P_ port *p);
696
688#if USE_PORTS 697#if USE_PORTS
689static pointer mk_port (SCHEME_P_ port *p);
690static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); 698static pointer port_from_filename (SCHEME_P_ const char *fn, int prop);
691static pointer port_from_file (SCHEME_P_ int, int prop); 699static pointer port_from_file (SCHEME_P_ int, int prop);
692static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); 700static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop);
693static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); 701static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop);
694static port *port_rep_from_file (SCHEME_P_ int, int prop); 702static port *port_rep_from_file (SCHEME_P_ int, int prop);
695static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); 703static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop);
696static void port_close (SCHEME_P_ pointer p, int flag); 704static void port_close (SCHEME_P_ pointer p, int flag);
697#endif 705#endif
706
698static void mark (pointer a); 707static void mark (pointer a);
699static void gc (SCHEME_P_ pointer a, pointer b); 708static void gc (SCHEME_P_ pointer a, pointer b);
700static int basic_inchar (port *pt); 709static int basic_inchar (port *pt);
701static int inchar (SCHEME_P); 710static int inchar (SCHEME_P);
702static void backchar (SCHEME_P_ int c); 711static void backchar (SCHEME_P_ int c);
703static char *readstr_upto (SCHEME_P_ int skip, const char *delim); 712static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
704static pointer readstrexp (SCHEME_P_ char delim); 713static pointer readstrexp (SCHEME_P_ char delim);
705ecb_inline int skipspace (SCHEME_P); 714static int skipspace (SCHEME_P);
706static int token (SCHEME_P); 715static int token (SCHEME_P);
707static void printslashstring (SCHEME_P_ char *s, int len); 716static void printslashstring (SCHEME_P_ char *s, int len);
708static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 717static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
709static void printatom (SCHEME_P_ pointer l, int f); 718static void printatom (SCHEME_P_ pointer l, int f);
710static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 719static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
876 } 885 }
877 886
878 return ret; 887 return ret;
879} 888}
880 889
881#if USE_MATH
882
883/* Round to nearest. Round to even if midway */
884static double
885round_per_R5RS (double x)
886{
887 double fl = floor (x);
888 double ce = ceil (x);
889 double dfl = x - fl;
890 double dce = ce - x;
891
892 if (dfl > dce)
893 return ce;
894 else if (dfl < dce)
895 return fl;
896 else
897 {
898 if (fmod (fl, 2) == 0) /* I imagine this holds */
899 return fl;
900 else
901 return ce;
902 }
903}
904#endif
905
906static int 890static int
907is_zero_rvalue (RVALUE x) 891is_zero_rvalue (RVALUE x)
908{ 892{
909 return x == 0; 893 return x == 0;
910#if 0 894#if 0
915#endif 899#endif
916#endif 900#endif
917} 901}
918 902
919/* allocate new cell segment */ 903/* allocate new cell segment */
920static int 904ecb_cold static int
921alloc_cellseg (SCHEME_P) 905alloc_cellseg (SCHEME_P)
922{ 906{
923 struct cell *newp; 907 struct cell *newp;
924 struct cell *last; 908 struct cell *last;
925 struct cell *p; 909 struct cell *p;
934 918
935 if (!cp && USE_ERROR_CHECKING) 919 if (!cp && USE_ERROR_CHECKING)
936 return k; 920 return k;
937 921
938 i = ++SCHEME_V->last_cell_seg; 922 i = ++SCHEME_V->last_cell_seg;
939 SCHEME_V->alloc_seg[i] = cp;
940 923
941 newp = (struct cell *)cp; 924 newp = (struct cell *)cp;
942 SCHEME_V->cell_seg[i] = newp; 925 SCHEME_V->cell_seg[i] = newp;
943 SCHEME_V->cell_segsize[i] = segsize; 926 SCHEME_V->cell_segsize[i] = segsize;
944 SCHEME_V->fcells += segsize; 927 SCHEME_V->fcells += segsize;
967 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 950 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
968 return S_SINK; 951 return S_SINK;
969 952
970 if (SCHEME_V->free_cell == NIL) 953 if (SCHEME_V->free_cell == NIL)
971 { 954 {
972 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1; 955 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 2;
973 956
974 gc (SCHEME_A_ a, b); 957 gc (SCHEME_A_ a, b);
975 958
976 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) 959 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL)
977 { 960 {
996 } 979 }
997} 980}
998 981
999/* To retain recent allocs before interpreter knows about them - 982/* To retain recent allocs before interpreter knows about them -
1000 Tehom */ 983 Tehom */
1001static void 984ecb_hot static void
1002push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 985push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
1003{ 986{
1004 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 987 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
1005 988
1006 set_typeflag (holder, T_PAIR); 989 set_typeflag (holder, T_PAIR);
1008 set_car (holder, recent); 991 set_car (holder, recent);
1009 set_cdr (holder, car (S_SINK)); 992 set_cdr (holder, car (S_SINK));
1010 set_car (S_SINK, holder); 993 set_car (S_SINK, holder);
1011} 994}
1012 995
1013static pointer 996ecb_hot static pointer
1014get_cell (SCHEME_P_ pointer a, pointer b) 997get_cell (SCHEME_P_ pointer a, pointer b)
1015{ 998{
1016 pointer cell = get_cell_x (SCHEME_A_ a, b); 999 pointer cell = get_cell_x (SCHEME_A_ a, b);
1017 1000
1018 /* For right now, include "a" and "b" in "cell" so that gc doesn't 1001 /* For right now, include "a" and "b" in "cell" so that gc doesn't
1056static void 1039static void
1057check_cell_alloced (pointer p, int expect_alloced) 1040check_cell_alloced (pointer p, int expect_alloced)
1058{ 1041{
1059 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ 1042 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */
1060 if (typeflag (p) & !expect_alloced) 1043 if (typeflag (p) & !expect_alloced)
1061 xwrstr ("Cell is already allocated!\n"); 1044 putstr (SCHEME_A_ "Cell is already allocated!\n");
1062 1045
1063 if (!(typeflag (p)) & expect_alloced) 1046 if (!(typeflag (p)) & expect_alloced)
1064 xwrstr ("Cell is not allocated!\n"); 1047 putstr (SCHEME_A_ "Cell is not allocated!\n");
1065} 1048}
1066 1049
1067static void 1050static void
1068check_range_alloced (pointer p, int n, int expect_alloced) 1051check_range_alloced (pointer p, int n, int expect_alloced)
1069{ 1052{
1075#endif 1058#endif
1076 1059
1077/* Medium level cell allocation */ 1060/* Medium level cell allocation */
1078 1061
1079/* get new cons cell */ 1062/* get new cons cell */
1080pointer 1063ecb_hot static pointer
1081xcons (SCHEME_P_ pointer a, pointer b, int immutable) 1064xcons (SCHEME_P_ pointer a, pointer b)
1082{ 1065{
1083 pointer x = get_cell (SCHEME_A_ a, b); 1066 pointer x = get_cell (SCHEME_A_ a, b);
1084 1067
1085 set_typeflag (x, T_PAIR); 1068 set_typeflag (x, T_PAIR);
1086
1087 if (immutable)
1088 setimmutable (x);
1089 1069
1090 set_car (x, a); 1070 set_car (x, a);
1091 set_cdr (x, b); 1071 set_cdr (x, b);
1092 1072
1093 return x; 1073 return x;
1094} 1074}
1095 1075
1096static pointer 1076ecb_hot static pointer
1077ximmutable_cons (SCHEME_P_ pointer a, pointer b)
1078{
1079 pointer x = xcons (SCHEME_A_ a, b);
1080 setimmutable (x);
1081 return x;
1082}
1083
1084#define cons(a,b) xcons (SCHEME_A_ a, b)
1085#define immutable_cons(a,b) ximmutable_cons (SCHEME_A_ a, b)
1086
1087ecb_cold static pointer
1097generate_symbol (SCHEME_P_ const char *name) 1088generate_symbol (SCHEME_P_ const char *name)
1098{ 1089{
1099 pointer x = mk_string (SCHEME_A_ name); 1090 pointer x = mk_string (SCHEME_A_ name);
1100 setimmutable (x); 1091 setimmutable (x);
1101 set_typeflag (x, T_SYMBOL | T_ATOM); 1092 set_typeflag (x, T_SYMBOL | T_ATOM);
1107#ifndef USE_OBJECT_LIST 1098#ifndef USE_OBJECT_LIST
1108 1099
1109static int 1100static int
1110hash_fn (const char *key, int table_size) 1101hash_fn (const char *key, int table_size)
1111{ 1102{
1112 const unsigned char *p = key; 1103 const unsigned char *p = (unsigned char *)key;
1113 uint32_t hash = 2166136261; 1104 uint32_t hash = 2166136261U;
1114 1105
1115 while (*p) 1106 while (*p)
1116 hash = (hash ^ *p++) * 16777619; 1107 hash = (hash ^ *p++) * 16777619;
1117 1108
1118 return hash % table_size; 1109 return hash % table_size;
1119} 1110}
1120 1111
1121static pointer 1112ecb_cold static pointer
1122oblist_initial_value (SCHEME_P) 1113oblist_initial_value (SCHEME_P)
1123{ 1114{
1124 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1115 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1125} 1116}
1126 1117
1127/* returns the new symbol */ 1118/* returns the new symbol */
1128static pointer 1119ecb_cold static pointer
1129oblist_add_by_name (SCHEME_P_ const char *name) 1120oblist_add_by_name (SCHEME_P_ const char *name)
1130{ 1121{
1131 pointer x = generate_symbol (SCHEME_A_ name); 1122 pointer x = generate_symbol (SCHEME_A_ name);
1132 int location = hash_fn (name, veclength (SCHEME_V->oblist)); 1123 int location = hash_fn (name, veclength (SCHEME_V->oblist));
1133 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); 1124 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1134 return x; 1125 return x;
1135} 1126}
1136 1127
1137ecb_inline pointer 1128ecb_cold static pointer
1138oblist_find_by_name (SCHEME_P_ const char *name) 1129oblist_find_by_name (SCHEME_P_ const char *name)
1139{ 1130{
1140 int location; 1131 int location;
1141 pointer x; 1132 pointer x;
1142 char *s; 1133 char *s;
1153 } 1144 }
1154 1145
1155 return NIL; 1146 return NIL;
1156} 1147}
1157 1148
1158static pointer 1149ecb_cold static pointer
1159oblist_all_symbols (SCHEME_P) 1150oblist_all_symbols (SCHEME_P)
1160{ 1151{
1161 int i; 1152 int i;
1162 pointer x; 1153 pointer x;
1163 pointer ob_list = NIL; 1154 pointer ob_list = NIL;
1169 return ob_list; 1160 return ob_list;
1170} 1161}
1171 1162
1172#else 1163#else
1173 1164
1174static pointer 1165ecb_cold static pointer
1175oblist_initial_value (SCHEME_P) 1166oblist_initial_value (SCHEME_P)
1176{ 1167{
1177 return NIL; 1168 return NIL;
1178} 1169}
1179 1170
1180ecb_inline pointer 1171ecb_cold static pointer
1181oblist_find_by_name (SCHEME_P_ const char *name) 1172oblist_find_by_name (SCHEME_P_ const char *name)
1182{ 1173{
1183 pointer x; 1174 pointer x;
1184 char *s; 1175 char *s;
1185 1176
1194 1185
1195 return NIL; 1186 return NIL;
1196} 1187}
1197 1188
1198/* returns the new symbol */ 1189/* returns the new symbol */
1199static pointer 1190ecb_cold static pointer
1200oblist_add_by_name (SCHEME_P_ const char *name) 1191oblist_add_by_name (SCHEME_P_ const char *name)
1201{ 1192{
1202 pointer x = generate_symbol (SCHEME_A_ name); 1193 pointer x = generate_symbol (SCHEME_A_ name);
1203 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1194 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1204 return x; 1195 return x;
1205} 1196}
1206 1197
1207static pointer 1198ecb_cold static pointer
1208oblist_all_symbols (SCHEME_P) 1199oblist_all_symbols (SCHEME_P)
1209{ 1200{
1210 return SCHEME_V->oblist; 1201 return SCHEME_V->oblist;
1211} 1202}
1212 1203
1213#endif 1204#endif
1214 1205
1215#if USE_PORTS
1216static pointer 1206ecb_cold static pointer
1217mk_port (SCHEME_P_ port *p) 1207mk_port (SCHEME_P_ port *p)
1218{ 1208{
1219 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1209 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1220 1210
1221 set_typeflag (x, T_PORT | T_ATOM); 1211 set_typeflag (x, T_PORT | T_ATOM);
1222 set_port (x, p); 1212 set_port (x, p);
1223 1213
1224 return x; 1214 return x;
1225} 1215}
1226#endif
1227 1216
1228pointer 1217ecb_cold pointer
1229mk_foreign_func (SCHEME_P_ foreign_func f) 1218mk_foreign_func (SCHEME_P_ foreign_func f)
1230{ 1219{
1231 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1220 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1232 1221
1233 set_typeflag (x, T_FOREIGN | T_ATOM); 1222 set_typeflag (x, T_FOREIGN | T_ATOM);
1398 x = oblist_add_by_name (SCHEME_A_ name); 1387 x = oblist_add_by_name (SCHEME_A_ name);
1399 1388
1400 return x; 1389 return x;
1401} 1390}
1402 1391
1403INTERFACE pointer 1392ecb_cold INTERFACE pointer
1404gensym (SCHEME_P) 1393gensym (SCHEME_P)
1405{ 1394{
1406 pointer x; 1395 pointer x;
1407 char name[40] = "gensym-"; 1396 char name[40] = "gensym-";
1408 xnum (name + 7, ++SCHEME_V->gensym_cnt); 1397 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1415{ 1404{
1416 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; 1405 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1417} 1406}
1418 1407
1419/* make symbol or number atom from string */ 1408/* make symbol or number atom from string */
1420static pointer 1409ecb_cold static pointer
1421mk_atom (SCHEME_P_ char *q) 1410mk_atom (SCHEME_P_ char *q)
1422{ 1411{
1423 char c, *p; 1412 char c, *p;
1424 int has_dec_point = 0; 1413 int has_dec_point = 0;
1425 int has_fp_exp = 0; 1414 int has_fp_exp = 0;
1496 1485
1497 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1486 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1498} 1487}
1499 1488
1500/* make constant */ 1489/* make constant */
1501static pointer 1490ecb_cold static pointer
1502mk_sharp_const (SCHEME_P_ char *name) 1491mk_sharp_const (SCHEME_P_ char *name)
1503{ 1492{
1504 if (!strcmp (name, "t")) 1493 if (!strcmp (name, "t"))
1505 return S_T; 1494 return S_T;
1506 else if (!strcmp (name, "f")) 1495 else if (!strcmp (name, "f"))
1507 return S_F; 1496 return S_F;
1508 else if (*name == '\\') /* #\w (character) */ 1497 else if (*name == '\\') /* #\w (character) */
1509 { 1498 {
1510 int c; 1499 int c;
1511 1500
1501 // TODO: optimise
1512 if (stricmp (name + 1, "space") == 0) 1502 if (stricmp (name + 1, "space") == 0)
1513 c = ' '; 1503 c = ' ';
1514 else if (stricmp (name + 1, "newline") == 0) 1504 else if (stricmp (name + 1, "newline") == 0)
1515 c = '\n'; 1505 c = '\n';
1516 else if (stricmp (name + 1, "return") == 0) 1506 else if (stricmp (name + 1, "return") == 0)
1517 c = '\r'; 1507 c = '\r';
1518 else if (stricmp (name + 1, "tab") == 0) 1508 else if (stricmp (name + 1, "tab") == 0)
1519 c = '\t'; 1509 c = '\t';
1510 else if (stricmp (name + 1, "alarm") == 0)
1511 c = 0x07;
1512 else if (stricmp (name + 1, "backspace") == 0)
1513 c = 0x08;
1514 else if (stricmp (name + 1, "escape") == 0)
1515 c = 0x1b;
1516 else if (stricmp (name + 1, "delete") == 0)
1517 c = 0x7f;
1518 else if (stricmp (name + 1, "null") == 0)
1519 c = 0;
1520 else if (name[1] == 'x' && name[2] != 0) 1520 else if (name[1] == 'x' && name[2] != 0)
1521 { 1521 {
1522 long c1 = strtol (name + 2, 0, 16); 1522 long c1 = strtol (name + 2, 0, 16);
1523 1523
1524 if (0 <= c1 && c1 <= UCHAR_MAX) 1524 if (0 <= c1 && c1 <= UCHAR_MAX)
1549 return NIL; 1549 return NIL;
1550 } 1550 }
1551} 1551}
1552 1552
1553/* ========== garbage collector ========== */ 1553/* ========== garbage collector ========== */
1554
1555static void
1556finalize_cell (SCHEME_P_ pointer a)
1557{
1558 /* TODO, fast bitmap check? */
1559 if (is_string (a) || is_symbol (a))
1560 free (strvalue (a));
1561 else if (is_vector (a))
1562 free (vecvalue (a));
1563#if USE_PORTS
1564 else if (is_port (a))
1565 {
1566 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit)
1567 port_close (SCHEME_A_ a, port_input | port_output);
1568
1569 free (port (a));
1570 }
1571#endif
1572}
1554 1573
1555/*-- 1574/*--
1556 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, 1575 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1,
1557 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1576 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1558 * for marking. 1577 * for marking.
1559 * 1578 *
1560 * The exception is vectors - vectors are currently marked recursively, 1579 * The exception is vectors - vectors are currently marked recursively,
1561 * which is inherited form tinyscheme and could be fixed by having another 1580 * which is inherited form tinyscheme and could be fixed by having another
1562 * word of context in the vector 1581 * word of context in the vector
1563 */ 1582 */
1564static void 1583ecb_hot static void
1565mark (pointer a) 1584mark (pointer a)
1566{ 1585{
1567 pointer t, q, p; 1586 pointer t, q, p;
1568 1587
1569 t = 0; 1588 t = 0;
1626 p = q; 1645 p = q;
1627 goto E6; 1646 goto E6;
1628 } 1647 }
1629} 1648}
1630 1649
1631/* garbage collection. parameter a, b is marked. */ 1650ecb_hot static void
1632static void 1651gc_free (SCHEME_P)
1633gc (SCHEME_P_ pointer a, pointer b)
1634{ 1652{
1635 int i; 1653 int i;
1636
1637 if (SCHEME_V->gc_verbose)
1638 putstr (SCHEME_A_ "gc...");
1639
1640 /* mark system globals */
1641 mark (SCHEME_V->oblist);
1642 mark (SCHEME_V->global_env);
1643
1644 /* mark current registers */
1645 mark (SCHEME_V->args);
1646 mark (SCHEME_V->envir);
1647 mark (SCHEME_V->code);
1648 dump_stack_mark (SCHEME_A);
1649 mark (SCHEME_V->value);
1650 mark (SCHEME_V->inport);
1651 mark (SCHEME_V->save_inport);
1652 mark (SCHEME_V->outport);
1653 mark (SCHEME_V->loadport);
1654
1655 /* Mark recent objects the interpreter doesn't know about yet. */
1656 mark (car (S_SINK));
1657 /* Mark any older stuff above nested C calls */
1658 mark (SCHEME_V->c_nest);
1659
1660#if USE_INTCACHE
1661 /* mark intcache */
1662 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1663 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1664 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1665#endif
1666
1667 /* mark variables a, b */
1668 mark (a);
1669 mark (b);
1670
1671 /* garbage collect */
1672 clrmark (NIL);
1673 SCHEME_V->fcells = 0;
1674 SCHEME_V->free_cell = NIL;
1675
1676 if (SCHEME_V->gc_verbose)
1677 xwrstr ("freeing...");
1678
1679 uint32_t total = 0; 1654 uint32_t total = 0;
1680 1655
1681 /* Here we scan the cells to build the free-list. */ 1656 /* Here we scan the cells to build the free-list. */
1682 for (i = SCHEME_V->last_cell_seg; i >= 0; i--) 1657 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1683 { 1658 {
1708 } 1683 }
1709 } 1684 }
1710 1685
1711 if (SCHEME_V->gc_verbose) 1686 if (SCHEME_V->gc_verbose)
1712 { 1687 {
1713 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" out of "); xwrnum (total); xwrstr (" cells were recovered.\n"); 1688 putstr (SCHEME_A_ "done: "); putnum (SCHEME_A_ SCHEME_V->fcells); putstr (SCHEME_A_ " out of "); putnum (SCHEME_A_ total); putstr (SCHEME_A_ " cells were recovered.\n");
1714 }
1715}
1716
1717static void
1718finalize_cell (SCHEME_P_ pointer a)
1719{
1720 /* TODO, fast bitmap check? */
1721 if (is_string (a) || is_symbol (a))
1722 free (strvalue (a));
1723 else if (is_vector (a))
1724 free (vecvalue (a));
1725#if USE_PORTS
1726 else if (is_port (a))
1727 { 1689 }
1728 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) 1690}
1729 port_close (SCHEME_A_ a, port_input | port_output);
1730 1691
1731 free (port (a)); 1692/* garbage collection. parameter a, b is marked. */
1732 } 1693ecb_cold static void
1694gc (SCHEME_P_ pointer a, pointer b)
1695{
1696 int i;
1697
1698 if (SCHEME_V->gc_verbose)
1699 putstr (SCHEME_A_ "gc...");
1700
1701 /* mark system globals */
1702 mark (SCHEME_V->oblist);
1703 mark (SCHEME_V->global_env);
1704
1705 /* mark current registers */
1706 mark (SCHEME_V->args);
1707 mark (SCHEME_V->envir);
1708 mark (SCHEME_V->code);
1709 dump_stack_mark (SCHEME_A);
1710 mark (SCHEME_V->value);
1711 mark (SCHEME_V->inport);
1712 mark (SCHEME_V->save_inport);
1713 mark (SCHEME_V->outport);
1714 mark (SCHEME_V->loadport);
1715
1716 /* Mark recent objects the interpreter doesn't know about yet. */
1717 mark (car (S_SINK));
1718 /* Mark any older stuff above nested C calls */
1719 mark (SCHEME_V->c_nest);
1720
1721#if USE_INTCACHE
1722 /* mark intcache */
1723 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1724 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1725 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1733#endif 1726#endif
1727
1728 /* mark variables a, b */
1729 mark (a);
1730 mark (b);
1731
1732 /* garbage collect */
1733 clrmark (NIL);
1734 SCHEME_V->fcells = 0;
1735 SCHEME_V->free_cell = NIL;
1736
1737 if (SCHEME_V->gc_verbose)
1738 putstr (SCHEME_A_ "freeing...");
1739
1740 gc_free (SCHEME_A);
1734} 1741}
1735 1742
1736/* ========== Routines for Reading ========== */ 1743/* ========== Routines for Reading ========== */
1737 1744
1738static int 1745ecb_cold static int
1739file_push (SCHEME_P_ const char *fname) 1746file_push (SCHEME_P_ const char *fname)
1740{ 1747{
1741#if USE_PORTS
1742 int fin; 1748 int fin;
1743 1749
1744 if (SCHEME_V->file_i == MAXFIL - 1) 1750 if (SCHEME_V->file_i == MAXFIL - 1)
1745 return 0; 1751 return 0;
1746 1752
1763 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0); 1769 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0);
1764#endif 1770#endif
1765 } 1771 }
1766 1772
1767 return fin >= 0; 1773 return fin >= 0;
1768
1769#else
1770 return 1;
1771#endif
1772} 1774}
1773 1775
1774static void 1776ecb_cold static void
1775file_pop (SCHEME_P) 1777file_pop (SCHEME_P)
1776{ 1778{
1777 if (SCHEME_V->file_i != 0) 1779 if (SCHEME_V->file_i != 0)
1778 { 1780 {
1779 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1781 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1783 SCHEME_V->file_i--; 1785 SCHEME_V->file_i--;
1784 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); 1786 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1785 } 1787 }
1786} 1788}
1787 1789
1788static int 1790ecb_cold static int
1789file_interactive (SCHEME_P) 1791file_interactive (SCHEME_P)
1790{ 1792{
1791#if USE_PORTS 1793#if USE_PORTS
1792 return SCHEME_V->file_i == 0 1794 return SCHEME_V->file_i == 0
1793 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1795 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1796 return 0; 1798 return 0;
1797#endif 1799#endif
1798} 1800}
1799 1801
1800#if USE_PORTS 1802#if USE_PORTS
1801static port * 1803ecb_cold static port *
1802port_rep_from_filename (SCHEME_P_ const char *fn, int prop) 1804port_rep_from_filename (SCHEME_P_ const char *fn, int prop)
1803{ 1805{
1804 int fd; 1806 int fd;
1805 int flags; 1807 int flags;
1806 char *rw; 1808 char *rw;
1829# endif 1831# endif
1830 1832
1831 return pt; 1833 return pt;
1832} 1834}
1833 1835
1834static pointer 1836ecb_cold static pointer
1835port_from_filename (SCHEME_P_ const char *fn, int prop) 1837port_from_filename (SCHEME_P_ const char *fn, int prop)
1836{ 1838{
1837 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); 1839 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop);
1838 1840
1839 if (!pt && USE_ERROR_CHECKING) 1841 if (!pt && USE_ERROR_CHECKING)
1840 return NIL; 1842 return NIL;
1841 1843
1842 return mk_port (SCHEME_A_ pt); 1844 return mk_port (SCHEME_A_ pt);
1843} 1845}
1844 1846
1845static port * 1847ecb_cold static port *
1846port_rep_from_file (SCHEME_P_ int f, int prop) 1848port_rep_from_file (SCHEME_P_ int f, int prop)
1847{ 1849{
1848 port *pt = malloc (sizeof *pt); 1850 port *pt = malloc (sizeof *pt);
1849 1851
1850 if (!pt && USE_ERROR_CHECKING) 1852 if (!pt && USE_ERROR_CHECKING)
1855 pt->rep.stdio.file = f; 1857 pt->rep.stdio.file = f;
1856 pt->rep.stdio.closeit = 0; 1858 pt->rep.stdio.closeit = 0;
1857 return pt; 1859 return pt;
1858} 1860}
1859 1861
1860static pointer 1862ecb_cold static pointer
1861port_from_file (SCHEME_P_ int f, int prop) 1863port_from_file (SCHEME_P_ int f, int prop)
1862{ 1864{
1863 port *pt = port_rep_from_file (SCHEME_A_ f, prop); 1865 port *pt = port_rep_from_file (SCHEME_A_ f, prop);
1864 1866
1865 if (!pt && USE_ERROR_CHECKING) 1867 if (!pt && USE_ERROR_CHECKING)
1866 return NIL; 1868 return NIL;
1867 1869
1868 return mk_port (SCHEME_A_ pt); 1870 return mk_port (SCHEME_A_ pt);
1869} 1871}
1870 1872
1871static port * 1873ecb_cold static port *
1872port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1874port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
1873{ 1875{
1874 port *pt = malloc (sizeof (port)); 1876 port *pt = malloc (sizeof (port));
1875 1877
1876 if (!pt && USE_ERROR_CHECKING) 1878 if (!pt && USE_ERROR_CHECKING)
1882 pt->rep.string.curr = start; 1884 pt->rep.string.curr = start;
1883 pt->rep.string.past_the_end = past_the_end; 1885 pt->rep.string.past_the_end = past_the_end;
1884 return pt; 1886 return pt;
1885} 1887}
1886 1888
1887static pointer 1889ecb_cold static pointer
1888port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1890port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
1889{ 1891{
1890 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); 1892 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop);
1891 1893
1892 if (!pt && USE_ERROR_CHECKING) 1894 if (!pt && USE_ERROR_CHECKING)
1895 return mk_port (SCHEME_A_ pt); 1897 return mk_port (SCHEME_A_ pt);
1896} 1898}
1897 1899
1898# define BLOCK_SIZE 256 1900# define BLOCK_SIZE 256
1899 1901
1900static port * 1902ecb_cold static port *
1901port_rep_from_scratch (SCHEME_P) 1903port_rep_from_scratch (SCHEME_P)
1902{ 1904{
1903 char *start; 1905 char *start;
1904 port *pt = malloc (sizeof (port)); 1906 port *pt = malloc (sizeof (port));
1905 1907
1919 pt->rep.string.curr = start; 1921 pt->rep.string.curr = start;
1920 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; 1922 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1;
1921 return pt; 1923 return pt;
1922} 1924}
1923 1925
1924static pointer 1926ecb_cold static pointer
1925port_from_scratch (SCHEME_P) 1927port_from_scratch (SCHEME_P)
1926{ 1928{
1927 port *pt = port_rep_from_scratch (SCHEME_A); 1929 port *pt = port_rep_from_scratch (SCHEME_A);
1928 1930
1929 if (!pt && USE_ERROR_CHECKING) 1931 if (!pt && USE_ERROR_CHECKING)
1930 return NIL; 1932 return NIL;
1931 1933
1932 return mk_port (SCHEME_A_ pt); 1934 return mk_port (SCHEME_A_ pt);
1933} 1935}
1934 1936
1935static void 1937ecb_cold static void
1936port_close (SCHEME_P_ pointer p, int flag) 1938port_close (SCHEME_P_ pointer p, int flag)
1937{ 1939{
1938 port *pt = port (p); 1940 port *pt = port (p);
1939 1941
1940 pt->kind &= ~flag; 1942 pt->kind &= ~flag;
1960 } 1962 }
1961} 1963}
1962#endif 1964#endif
1963 1965
1964/* get new character from input file */ 1966/* get new character from input file */
1965static int 1967ecb_cold static int
1966inchar (SCHEME_P) 1968inchar (SCHEME_P)
1967{ 1969{
1968 int c; 1970 int c;
1969 port *pt = port (SCHEME_V->inport); 1971 port *pt = port (SCHEME_V->inport);
1970 1972
1984 } 1986 }
1985 1987
1986 return c; 1988 return c;
1987} 1989}
1988 1990
1989static int ungot = -1; 1991ecb_cold static int
1990
1991static int
1992basic_inchar (port *pt) 1992basic_inchar (port *pt)
1993{ 1993{
1994#if USE_PORTS
1995 if (pt->unget != -1) 1994 if (pt->unget != -1)
1996 { 1995 {
1997 int r = pt->unget; 1996 int r = pt->unget;
1998 pt->unget = -1; 1997 pt->unget = -1;
1999 return r; 1998 return r;
2000 } 1999 }
2001 2000
2001#if USE_PORTS
2002 if (pt->kind & port_file) 2002 if (pt->kind & port_file)
2003 { 2003 {
2004 char c; 2004 char c;
2005 2005
2006 if (!read (pt->rep.stdio.file, &c, 1)) 2006 if (!read (pt->rep.stdio.file, &c, 1))
2014 return EOF; 2014 return EOF;
2015 else 2015 else
2016 return *pt->rep.string.curr++; 2016 return *pt->rep.string.curr++;
2017 } 2017 }
2018#else 2018#else
2019 if (ungot == -1)
2020 {
2021 char c; 2019 char c;
2022 if (!read (0, &c, 1)) 2020
2021 if (!read (pt->rep.stdio.file, &c, 1))
2023 return EOF; 2022 return EOF;
2024 2023
2025 ungot = c;
2026 }
2027
2028 {
2029 int r = ungot;
2030 ungot = -1;
2031 return r; 2024 return c;
2032 }
2033#endif 2025#endif
2034} 2026}
2035 2027
2036/* back character to input buffer */ 2028/* back character to input buffer */
2037static void 2029ecb_cold static void
2038backchar (SCHEME_P_ int c) 2030backchar (SCHEME_P_ int c)
2039{ 2031{
2040#if USE_PORTS 2032 port *pt = port (SCHEME_V->inport);
2041 port *pt;
2042 2033
2043 if (c == EOF) 2034 if (c == EOF)
2044 return; 2035 return;
2045 2036
2046 pt = port (SCHEME_V->inport);
2047 pt->unget = c; 2037 pt->unget = c;
2048#else
2049 if (c == EOF)
2050 return;
2051
2052 ungot = c;
2053#endif
2054} 2038}
2055 2039
2056#if USE_PORTS 2040#if USE_PORTS
2057static int 2041ecb_cold static int
2058realloc_port_string (SCHEME_P_ port *p) 2042realloc_port_string (SCHEME_P_ port *p)
2059{ 2043{
2060 char *start = p->rep.string.start; 2044 char *start = p->rep.string.start;
2061 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; 2045 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE;
2062 char *str = malloc (new_size); 2046 char *str = malloc (new_size);
2075 else 2059 else
2076 return 0; 2060 return 0;
2077} 2061}
2078#endif 2062#endif
2079 2063
2080INTERFACE void 2064ecb_cold static void
2081putstr (SCHEME_P_ const char *s) 2065putchars (SCHEME_P_ const char *s, int len)
2082{ 2066{
2067 port *pt = port (SCHEME_V->outport);
2068
2083#if USE_PORTS 2069#if USE_PORTS
2084 port *pt = port (SCHEME_V->outport);
2085
2086 if (pt->kind & port_file)
2087 write (pt->rep.stdio.file, s, strlen (s));
2088 else
2089 for (; *s; s++)
2090 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2091 *pt->rep.string.curr++ = *s;
2092 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2093 *pt->rep.string.curr++ = *s;
2094
2095#else
2096 xwrstr (s);
2097#endif
2098}
2099
2100static void
2101putchars (SCHEME_P_ const char *s, int len)
2102{
2103#if USE_PORTS
2104 port *pt = port (SCHEME_V->outport);
2105
2106 if (pt->kind & port_file) 2070 if (pt->kind & port_file)
2107 write (pt->rep.stdio.file, s, len); 2071 write (pt->rep.stdio.file, s, len);
2108 else 2072 else
2109 { 2073 {
2110 for (; len; len--) 2074 for (; len; len--)
2115 *pt->rep.string.curr++ = *s++; 2079 *pt->rep.string.curr++ = *s++;
2116 } 2080 }
2117 } 2081 }
2118 2082
2119#else 2083#else
2120 write (1, s, len); 2084 write (1, s, len); // output not initialised
2121#endif 2085#endif
2086}
2087
2088INTERFACE void
2089putstr (SCHEME_P_ const char *s)
2090{
2091 putchars (SCHEME_A_ s, strlen (s));
2122} 2092}
2123 2093
2124INTERFACE void 2094INTERFACE void
2125putcharacter (SCHEME_P_ int c) 2095putcharacter (SCHEME_P_ int c)
2126{ 2096{
2127#if USE_PORTS
2128 port *pt = port (SCHEME_V->outport);
2129
2130 if (pt->kind & port_file)
2131 {
2132 char cc = c;
2133 write (pt->rep.stdio.file, &cc, 1);
2134 }
2135 else
2136 {
2137 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2138 *pt->rep.string.curr++ = c;
2139 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2140 *pt->rep.string.curr++ = c;
2141 }
2142
2143#else
2144 char cc = c; 2097 char cc = c;
2145 write (1, &c, 1); 2098
2146#endif 2099 putchars (SCHEME_A_ &cc, 1);
2147} 2100}
2148 2101
2149/* read characters up to delimiter, but cater to character constants */ 2102/* read characters up to delimiter, but cater to character constants */
2150static char * 2103ecb_cold static char *
2151readstr_upto (SCHEME_P_ int skip, const char *delim) 2104readstr_upto (SCHEME_P_ int skip, const char *delim)
2152{ 2105{
2153 char *p = SCHEME_V->strbuff + skip; 2106 char *p = SCHEME_V->strbuff + skip;
2154 2107
2155 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2108 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2164 2117
2165 return SCHEME_V->strbuff; 2118 return SCHEME_V->strbuff;
2166} 2119}
2167 2120
2168/* read string expression "xxx...xxx" */ 2121/* read string expression "xxx...xxx" */
2169static pointer 2122ecb_cold static pointer
2170readstrexp (SCHEME_P_ char delim) 2123readstrexp (SCHEME_P_ char delim)
2171{ 2124{
2172 char *p = SCHEME_V->strbuff; 2125 char *p = SCHEME_V->strbuff;
2173 int c; 2126 int c;
2174 int c1 = 0; 2127 int c1 = 0;
2207 case '7': 2160 case '7':
2208 state = st_oct1; 2161 state = st_oct1;
2209 c1 = c - '0'; 2162 c1 = c - '0';
2210 break; 2163 break;
2211 2164
2165 case 'a': *p++ = '\a'; state = st_ok; break;
2166 case 'n': *p++ = '\n'; state = st_ok; break;
2167 case 'r': *p++ = '\r'; state = st_ok; break;
2168 case 't': *p++ = '\t'; state = st_ok; break;
2169
2170 // this overshoots the minimum requirements of r7rs
2171 case ' ':
2172 case '\t':
2173 case '\r':
2174 case '\n':
2175 skipspace (SCHEME_A);
2176 state = st_ok;
2177 break;
2178
2179 //TODO: x should end in ;, not two-digit hex
2212 case 'x': 2180 case 'x':
2213 case 'X': 2181 case 'X':
2214 state = st_x1; 2182 state = st_x1;
2215 c1 = 0; 2183 c1 = 0;
2216 break;
2217
2218 case 'n':
2219 *p++ = '\n';
2220 state = st_ok;
2221 break;
2222
2223 case 't':
2224 *p++ = '\t';
2225 state = st_ok;
2226 break;
2227
2228 case 'r':
2229 *p++ = '\r';
2230 state = st_ok;
2231 break; 2184 break;
2232 2185
2233 default: 2186 default:
2234 *p++ = c; 2187 *p++ = c;
2235 state = st_ok; 2188 state = st_ok;
2287 } 2240 }
2288 } 2241 }
2289} 2242}
2290 2243
2291/* check c is in chars */ 2244/* check c is in chars */
2292ecb_inline int 2245ecb_cold int
2293is_one_of (const char *s, int c) 2246is_one_of (const char *s, int c)
2294{ 2247{
2295 return c == EOF || !!strchr (s, c); 2248 return c == EOF || !!strchr (s, c);
2296} 2249}
2297 2250
2298/* skip white characters */ 2251/* skip white characters */
2299ecb_inline int 2252ecb_cold int
2300skipspace (SCHEME_P) 2253skipspace (SCHEME_P)
2301{ 2254{
2302 int c, curr_line = 0; 2255 int c, curr_line = 0;
2303 2256
2304 do 2257 do
2324 backchar (SCHEME_A_ c); 2277 backchar (SCHEME_A_ c);
2325 return 1; 2278 return 1;
2326} 2279}
2327 2280
2328/* get token */ 2281/* get token */
2329static int 2282ecb_cold static int
2330token (SCHEME_P) 2283token (SCHEME_P)
2331{ 2284{
2332 int c = skipspace (SCHEME_A); 2285 int c = skipspace (SCHEME_A);
2333 2286
2334 if (c == EOF) 2287 if (c == EOF)
2432} 2385}
2433 2386
2434/* ========== Routines for Printing ========== */ 2387/* ========== Routines for Printing ========== */
2435#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) 2388#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL)
2436 2389
2437static void 2390ecb_cold static void
2438printslashstring (SCHEME_P_ char *p, int len) 2391printslashstring (SCHEME_P_ char *p, int len)
2439{ 2392{
2440 int i; 2393 int i;
2441 unsigned char *s = (unsigned char *) p; 2394 unsigned char *s = (unsigned char *) p;
2442 2395
2498 2451
2499 putcharacter (SCHEME_A_ '"'); 2452 putcharacter (SCHEME_A_ '"');
2500} 2453}
2501 2454
2502/* print atoms */ 2455/* print atoms */
2503static void 2456ecb_cold static void
2504printatom (SCHEME_P_ pointer l, int f) 2457printatom (SCHEME_P_ pointer l, int f)
2505{ 2458{
2506 char *p; 2459 char *p;
2507 int len; 2460 int len;
2508 2461
2509 atom2str (SCHEME_A_ l, f, &p, &len); 2462 atom2str (SCHEME_A_ l, f, &p, &len);
2510 putchars (SCHEME_A_ p, len); 2463 putchars (SCHEME_A_ p, len);
2511} 2464}
2512 2465
2513/* Uses internal buffer unless string pointer is already available */ 2466/* Uses internal buffer unless string pointer is already available */
2514static void 2467ecb_cold static void
2515atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2468atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2516{ 2469{
2517 char *p; 2470 char *p;
2518 2471
2519 if (l == NIL) 2472 if (l == NIL)
2726 return car (d); 2679 return car (d);
2727 2680
2728 p = cons (car (d), cdr (d)); 2681 p = cons (car (d), cdr (d));
2729 q = p; 2682 q = p;
2730 2683
2731 while (cdr (cdr (p)) != NIL) 2684 while (cddr (p) != NIL)
2732 { 2685 {
2733 d = cons (car (p), cdr (p)); 2686 d = cons (car (p), cdr (p));
2734 2687
2735 if (cdr (cdr (p)) != NIL) 2688 if (cddr (p) != NIL)
2736 p = cdr (d); 2689 p = cdr (d);
2737 } 2690 }
2738 2691
2739 set_cdr (p, car (cdr (p))); 2692 set_cdr (p, cadr (p));
2740 return q; 2693 return q;
2741} 2694}
2742 2695
2743/* reverse list -- produce new list */ 2696/* reverse list -- produce new list */
2744static pointer 2697ecb_hot static pointer
2745reverse (SCHEME_P_ pointer a) 2698reverse (SCHEME_P_ pointer a)
2746{ 2699{
2747 /* a must be checked by gc */ 2700 /* a must be checked by gc */
2748 pointer p = NIL; 2701 pointer p = NIL;
2749 2702
2752 2705
2753 return p; 2706 return p;
2754} 2707}
2755 2708
2756/* reverse list --- in-place */ 2709/* reverse list --- in-place */
2757static pointer 2710ecb_hot static pointer
2758reverse_in_place (SCHEME_P_ pointer term, pointer list) 2711reverse_in_place (SCHEME_P_ pointer term, pointer list)
2759{ 2712{
2760 pointer result = term; 2713 pointer result = term;
2761 pointer p = list; 2714 pointer p = list;
2762 2715
2770 2723
2771 return result; 2724 return result;
2772} 2725}
2773 2726
2774/* append list -- produce new list (in reverse order) */ 2727/* append list -- produce new list (in reverse order) */
2775static pointer 2728ecb_hot static pointer
2776revappend (SCHEME_P_ pointer a, pointer b) 2729revappend (SCHEME_P_ pointer a, pointer b)
2777{ 2730{
2778 pointer result = a; 2731 pointer result = a;
2779 pointer p = b; 2732 pointer p = b;
2780 2733
2789 2742
2790 return S_F; /* signal an error */ 2743 return S_F; /* signal an error */
2791} 2744}
2792 2745
2793/* equivalence of atoms */ 2746/* equivalence of atoms */
2794int 2747ecb_hot int
2795eqv (pointer a, pointer b) 2748eqv (pointer a, pointer b)
2796{ 2749{
2797 if (is_string (a)) 2750 if (is_string (a))
2798 { 2751 {
2799 if (is_string (b)) 2752 if (is_string (b))
2893 } 2846 }
2894 else 2847 else
2895 set_car (env, immutable_cons (slot, car (env))); 2848 set_car (env, immutable_cons (slot, car (env)));
2896} 2849}
2897 2850
2898static pointer 2851ecb_hot static pointer
2899find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2852find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2900{ 2853{
2901 pointer x, y; 2854 pointer x, y;
2902 2855
2903 for (x = env; x != NIL; x = cdr (x)) 2856 for (x = env; x != NIL; x = cdr (x))
2924 return NIL; 2877 return NIL;
2925} 2878}
2926 2879
2927#else /* USE_ALIST_ENV */ 2880#else /* USE_ALIST_ENV */
2928 2881
2929ecb_inline void 2882static void
2930new_frame_in_env (SCHEME_P_ pointer old_env) 2883new_frame_in_env (SCHEME_P_ pointer old_env)
2931{ 2884{
2932 SCHEME_V->envir = immutable_cons (NIL, old_env); 2885 SCHEME_V->envir = immutable_cons (NIL, old_env);
2933 setenvironment (SCHEME_V->envir); 2886 setenvironment (SCHEME_V->envir);
2934} 2887}
2935 2888
2936ecb_inline void 2889static void
2937new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2890new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2938{ 2891{
2939 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2892 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2940} 2893}
2941 2894
2942static pointer 2895ecb_hot static pointer
2943find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2896find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2944{ 2897{
2945 pointer x, y; 2898 pointer x, y;
2946 2899
2947 for (x = env; x != NIL; x = cdr (x)) 2900 for (x = env; x != NIL; x = cdr (x))
2961 return NIL; 2914 return NIL;
2962} 2915}
2963 2916
2964#endif /* USE_ALIST_ENV else */ 2917#endif /* USE_ALIST_ENV else */
2965 2918
2966ecb_inline void 2919static void
2967new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2920new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2968{ 2921{
2969 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 2922 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2970 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2923 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2971} 2924}
2972 2925
2973ecb_inline void 2926static void
2974set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2927set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2975{ 2928{
2976 set_cdr (slot, value); 2929 set_cdr (slot, value);
2977} 2930}
2978 2931
2979ecb_inline pointer 2932static pointer
2980slot_value_in_env (pointer slot) 2933slot_value_in_env (pointer slot)
2981{ 2934{
2982 return cdr (slot); 2935 return cdr (slot);
2983} 2936}
2984 2937
2985/* ========== Evaluation Cycle ========== */ 2938/* ========== Evaluation Cycle ========== */
2986 2939
2987static int 2940ecb_cold static int
2988xError_1 (SCHEME_P_ const char *s, pointer a) 2941xError_1 (SCHEME_P_ const char *s, pointer a)
2989{ 2942{
2990#if USE_ERROR_HOOK
2991 pointer x;
2992 pointer hdl = SCHEME_V->ERROR_HOOK;
2993#endif
2994
2995#if USE_PRINTF 2943#if USE_PRINTF
2996#if SHOW_ERROR_LINE 2944#if SHOW_ERROR_LINE
2997 char sbuf[STRBUFFSIZE]; 2945 char sbuf[STRBUFFSIZE];
2998 2946
2999 /* make sure error is not in REPL */ 2947 /* make sure error is not in REPL */
3014 } 2962 }
3015#endif 2963#endif
3016#endif 2964#endif
3017 2965
3018#if USE_ERROR_HOOK 2966#if USE_ERROR_HOOK
3019 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); 2967 pointer x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->ERROR_HOOK, 1);
3020 2968
3021 if (x != NIL) 2969 if (x != NIL)
3022 { 2970 {
3023 pointer code = a 2971 pointer code = a
3024 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) 2972 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL)
3068 pointer code; 3016 pointer code;
3069}; 3017};
3070 3018
3071# define STACK_GROWTH 3 3019# define STACK_GROWTH 3
3072 3020
3073static void 3021ecb_hot static void
3074s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3022s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3075{ 3023{
3076 int nframes = (uintptr_t)SCHEME_V->dump; 3024 int nframes = (uintptr_t)SCHEME_V->dump;
3077 struct dump_stack_frame *next_frame; 3025 struct dump_stack_frame *next_frame;
3078 3026
3091 next_frame->code = code; 3039 next_frame->code = code;
3092 3040
3093 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3041 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3094} 3042}
3095 3043
3096static int 3044static ecb_hot int
3097xs_return (SCHEME_P_ pointer a) 3045xs_return (SCHEME_P_ pointer a)
3098{ 3046{
3099 int nframes = (uintptr_t)SCHEME_V->dump; 3047 int nframes = (uintptr_t)SCHEME_V->dump;
3100 struct dump_stack_frame *frame; 3048 struct dump_stack_frame *frame;
3101 3049
3112 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3060 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3113 3061
3114 return 0; 3062 return 0;
3115} 3063}
3116 3064
3117ecb_inline void 3065ecb_cold void
3118dump_stack_reset (SCHEME_P) 3066dump_stack_reset (SCHEME_P)
3119{ 3067{
3120 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3068 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3121 SCHEME_V->dump = (pointer)+0; 3069 SCHEME_V->dump = (pointer)+0;
3122} 3070}
3123 3071
3124ecb_inline void 3072ecb_cold void
3125dump_stack_initialize (SCHEME_P) 3073dump_stack_initialize (SCHEME_P)
3126{ 3074{
3127 SCHEME_V->dump_size = 0; 3075 SCHEME_V->dump_size = 0;
3128 SCHEME_V->dump_base = 0; 3076 SCHEME_V->dump_base = 0;
3129 dump_stack_reset (SCHEME_A); 3077 dump_stack_reset (SCHEME_A);
3130} 3078}
3131 3079
3132static void 3080ecb_cold static void
3133dump_stack_free (SCHEME_P) 3081dump_stack_free (SCHEME_P)
3134{ 3082{
3135 free (SCHEME_V->dump_base); 3083 free (SCHEME_V->dump_base);
3136 SCHEME_V->dump_base = 0; 3084 SCHEME_V->dump_base = 0;
3137 SCHEME_V->dump = (pointer)0; 3085 SCHEME_V->dump = (pointer)0;
3138 SCHEME_V->dump_size = 0; 3086 SCHEME_V->dump_size = 0;
3139} 3087}
3140 3088
3141static void 3089ecb_cold static void
3142dump_stack_mark (SCHEME_P) 3090dump_stack_mark (SCHEME_P)
3143{ 3091{
3144 int nframes = (uintptr_t)SCHEME_V->dump; 3092 int nframes = (uintptr_t)SCHEME_V->dump;
3145 int i; 3093 int i;
3146 3094
3152 mark (frame->envir); 3100 mark (frame->envir);
3153 mark (frame->code); 3101 mark (frame->code);
3154 } 3102 }
3155} 3103}
3156 3104
3157static pointer 3105ecb_cold static pointer
3158ss_get_cont (SCHEME_P) 3106ss_get_cont (SCHEME_P)
3159{ 3107{
3160 int nframes = (uintptr_t)SCHEME_V->dump; 3108 int nframes = (uintptr_t)SCHEME_V->dump;
3161 int i; 3109 int i;
3162 3110
3174 } 3122 }
3175 3123
3176 return cont; 3124 return cont;
3177} 3125}
3178 3126
3179static void 3127ecb_cold static void
3180ss_set_cont (SCHEME_P_ pointer cont) 3128ss_set_cont (SCHEME_P_ pointer cont)
3181{ 3129{
3182 int i = 0; 3130 int i = 0;
3183 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3131 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3184 3132
3196 SCHEME_V->dump = (pointer)(uintptr_t)i; 3144 SCHEME_V->dump = (pointer)(uintptr_t)i;
3197} 3145}
3198 3146
3199#else 3147#else
3200 3148
3201ecb_inline void 3149ecb_cold void
3202dump_stack_reset (SCHEME_P) 3150dump_stack_reset (SCHEME_P)
3203{ 3151{
3204 SCHEME_V->dump = NIL; 3152 SCHEME_V->dump = NIL;
3205} 3153}
3206 3154
3207ecb_inline void 3155ecb_cold void
3208dump_stack_initialize (SCHEME_P) 3156dump_stack_initialize (SCHEME_P)
3209{ 3157{
3210 dump_stack_reset (SCHEME_A); 3158 dump_stack_reset (SCHEME_A);
3211} 3159}
3212 3160
3213static void 3161ecb_cold static void
3214dump_stack_free (SCHEME_P) 3162dump_stack_free (SCHEME_P)
3215{ 3163{
3216 SCHEME_V->dump = NIL; 3164 SCHEME_V->dump = NIL;
3217} 3165}
3218 3166
3219static int 3167ecb_hot static int
3220xs_return (SCHEME_P_ pointer a) 3168xs_return (SCHEME_P_ pointer a)
3221{ 3169{
3222 pointer dump = SCHEME_V->dump; 3170 pointer dump = SCHEME_V->dump;
3223 3171
3224 SCHEME_V->value = a; 3172 SCHEME_V->value = a;
3234 SCHEME_V->dump = dump; 3182 SCHEME_V->dump = dump;
3235 3183
3236 return 0; 3184 return 0;
3237} 3185}
3238 3186
3239static void 3187ecb_hot static void
3240s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3188s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3241{ 3189{
3242 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), 3190 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op),
3243 cons (args, 3191 cons (args,
3244 cons (SCHEME_V->envir, 3192 cons (SCHEME_V->envir,
3245 cons (code, 3193 cons (code,
3246 SCHEME_V->dump)))); 3194 SCHEME_V->dump))));
3247} 3195}
3248 3196
3249static void 3197ecb_cold static void
3250dump_stack_mark (SCHEME_P) 3198dump_stack_mark (SCHEME_P)
3251{ 3199{
3252 mark (SCHEME_V->dump); 3200 mark (SCHEME_V->dump);
3253} 3201}
3254 3202
3255static pointer 3203ecb_cold static pointer
3256ss_get_cont (SCHEME_P) 3204ss_get_cont (SCHEME_P)
3257{ 3205{
3258 return SCHEME_V->dump; 3206 return SCHEME_V->dump;
3259} 3207}
3260 3208
3261static void 3209ecb_cold static void
3262ss_set_cont (SCHEME_P_ pointer cont) 3210ss_set_cont (SCHEME_P_ pointer cont)
3263{ 3211{
3264 SCHEME_V->dump = cont; 3212 SCHEME_V->dump = cont;
3265} 3213}
3266 3214
3267#endif 3215#endif
3268 3216
3269#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3217#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3270 3218
3271#if EXPERIMENT 3219#if EXPERIMENT
3220
3272static int 3221static int
3273debug (SCHEME_P_ int indent, pointer x) 3222dtree (SCHEME_P_ int indent, pointer x)
3274{ 3223{
3275 int c; 3224 int c;
3276 3225
3277 if (is_syntax (x)) 3226 if (is_syntax (x))
3278 { 3227 {
3296 printf ("%*sS<%s>\n", indent, "", symname (x)); 3245 printf ("%*sS<%s>\n", indent, "", symname (x));
3297 return 24+8; 3246 return 24+8;
3298 3247
3299 case T_CLOSURE: 3248 case T_CLOSURE:
3300 printf ("%*sS<%s>\n", indent, "", "closure"); 3249 printf ("%*sS<%s>\n", indent, "", "closure");
3301 debug (SCHEME_A_ indent + 3, cdr(x)); 3250 dtree (SCHEME_A_ indent + 3, cdr(x));
3302 return 32 + debug (SCHEME_A_ indent + 3, car (x)); 3251 return 32 + dtree (SCHEME_A_ indent + 3, car (x));
3303 3252
3304 case T_PAIR: 3253 case T_PAIR:
3305 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); 3254 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3306 c = debug (SCHEME_A_ indent + 3, car (x)); 3255 c = dtree (SCHEME_A_ indent + 3, car (x));
3307 c += debug (SCHEME_A_ indent + 3, cdr (x)); 3256 c += dtree (SCHEME_A_ indent + 3, cdr (x));
3308 return c + 1; 3257 return c + 1;
3309 3258
3310 case T_PORT: 3259 case T_PORT:
3311 printf ("%*sS<%s>\n", indent, "", "port"); 3260 printf ("%*sS<%s>\n", indent, "", "port");
3312 return 24+8; 3261 return 24+8;
3315 printf ("%*sS<%s>\n", indent, "", "vector"); 3264 printf ("%*sS<%s>\n", indent, "", "vector");
3316 return 24+8; 3265 return 24+8;
3317 3266
3318 case T_ENVIRONMENT: 3267 case T_ENVIRONMENT:
3319 printf ("%*sS<%s>\n", indent, "", "environment"); 3268 printf ("%*sS<%s>\n", indent, "", "environment");
3320 return 0 + debug (SCHEME_A_ indent + 3, car (x)); 3269 return 0 + dtree (SCHEME_A_ indent + 3, car (x));
3321 3270
3322 default: 3271 default:
3323 printf ("unhandled type %d\n", type (x)); 3272 printf ("unhandled type %d\n", type (x));
3324 break; 3273 break;
3325 } 3274 }
3326} 3275}
3327#endif
3328 3276
3277#define DUMP(t) do { printf ("DUMP %s:%d\n", __FILE__, __LINE__); dtree (SCHEME_A_ 0, (t)); } while (0)
3278
3279typedef void *stream[1];
3280
3281#define stream_init() { 0 }
3282#define stream_data(s) ((char *)(s)[0] + sizeof (uint32_t) * 2)
3283#define stream_size(s) (((uint32_t *)(s)[0])[0] - sizeof (uint32_t) * 2)
3284#define stream_free(s) free (s[0])
3285
3286ecb_cold static void
3287stream_put (stream s, uint8_t byte)
3288{
3289 uint32_t *sp = *s;
3290 uint32_t size = sizeof (uint32_t) * 2;
3291 uint32_t offs = size;
3292
3293 if (ecb_expect_true (sp))
3294 {
3295 offs = sp[0];
3296 size = sp[1];
3297 }
3298
3299 if (ecb_expect_false (offs == size))
3300 {
3301 size *= 2;
3302 sp = realloc (sp, size);
3303 *s = sp;
3304 sp[1] = size;
3305
3306 }
3307
3308 ((uint8_t *)sp)[offs++] = byte;
3309 sp[0] = offs;
3310}
3311
3312ecb_cold static void
3313stream_put_v (stream s, uint32_t v)
3314{
3315 while (v > 0x7f)
3316 {
3317 stream_put (s, v | 0x80);
3318 v >>= 7;
3319 }
3320
3321 stream_put (s, v);
3322}
3323
3324ecb_cold static void
3325stream_put_tv (stream s, int bop, uint32_t v)
3326{
3327 printf ("put tv %d %d\n", bop, v);//D
3328 stream_put (s, bop);
3329 stream_put_v (s, v);
3330}
3331
3332ecb_cold static void
3333stream_put_stream (stream s, stream o)
3334{
3335 uint32_t i;
3336
3337 for (i = 0; i < stream_size (o); ++i)
3338 stream_put (s, stream_data (o)[i]);
3339
3340 stream_free (o);
3341}
3342
3343// calculates a (preferably small) integer that makes it possible to find
3344// the symbol again. if pointers were offsets into a memory area... until
3345// then, we return segment number in the low bits, and offset in the high
3346// bits.
3347// also, this function must never return 0.
3348ecb_cold static uint32_t
3349symbol_id (SCHEME_P_ pointer sym)
3350{
3351 struct cell *p = CELL (sym);
3352 int i;
3353
3354 for (i = SCHEME_V->last_cell_seg; i >= 0; --i)
3355 if (SCHEME_V->cell_seg[i] <= p && p < SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize[i])
3356 return i | ((p - SCHEME_V->cell_seg[i]) << CELL_NSEGMENT_LOG);
3357
3358 abort ();
3359}
3360
3361ecb_cold static uint32_t
3362cell_id (SCHEME_P_ pointer p)
3363{
3364 return symbol_id (SCHEME_A_ p);
3365}
3366
3367enum byteop
3368{
3369 BOP_NIL,
3370 BOP_SYNTAX,
3371 BOP_INTEGER,
3372 BOP_SYMBOL,
3373 BOP_LIST_BEG,
3374 BOP_LIST_END,
3375 BOP_BIFT, // branch if true
3376 BOP_BIFF, // branch if false
3377 BOP_BIFNE, // branch if not eqv?
3378 BOP_BRA, // "short" branch
3379 BOP_JMP, // "long" jump
3380 BOP_DATUM,
3381 BOP_LET,
3382 BOP_LETAST,
3383 BOP_LETREC,
3384 BOP_DEFINE,
3385 BOP_MACRO,
3386 BOP_SET,
3387 BOP_BEGIN,
3388 BOP_LAMBDA,
3389};
3390
3391ecb_cold static void compile_expr (SCHEME_P_ stream s, pointer x);
3392
3393ecb_cold static void
3394compile_list (SCHEME_P_ stream s, pointer x)
3395{
3396 for (; x != NIL; x = cdr (x))
3397 compile_expr (SCHEME_A_ s, car (x));
3398}
3399
3329static int 3400static void
3401compile_if (SCHEME_P_ stream s, pointer cond, pointer ift, pointer iff)
3402{
3403 //TODO: borked
3404 stream sift = stream_init (); compile_expr (SCHEME_A_ sift, ift);
3405
3406 stream_put (s, BOP_BIFF);
3407 compile_expr (SCHEME_A_ s, cond);
3408 stream_put_v (s, stream_size (sift));
3409 stream_put_stream (s, sift);
3410
3411 if (iff != NIL)
3412 {
3413 stream siff = stream_init (); compile_expr (SCHEME_A_ siff, iff);
3414 stream_put_tv (s, BOP_BRA, stream_size (siff));
3415 stream_put_stream (s, siff);
3416 }
3417}
3418
3419typedef uint32_t stream_fixup;
3420
3421static stream_fixup
3422stream_put_fixup (stream s)
3423{
3424 stream_put (s, 0);
3425 stream_put (s, 0);
3426
3427 return stream_size (s);
3428}
3429
3430static void
3431stream_fix_fixup (stream s, stream_fixup fixup, uint32_t target)
3432{
3433 target -= fixup;
3434 assert (target < (1 << 14));
3435 stream_data (s)[fixup - 2] = target | 0x80;
3436 stream_data (s)[fixup - 1] = target >> 7;
3437}
3438
3439static void
3440compile_and_or (SCHEME_P_ stream s, int and, pointer x)
3441{
3442 if (cdr (x) == NIL)
3443 compile_expr (SCHEME_A_ s, car (x));
3444 else
3445 {
3446 stream_put (s, and ? BOP_BIFF : BOP_BIFT);
3447 compile_expr (SCHEME_A_ s, car (x));
3448 stream_fixup end = stream_put_fixup (s);
3449
3450 compile_and_or (SCHEME_A_ s, and, cdr (x));
3451 stream_fix_fixup (s, end, stream_size (s));
3452 }
3453}
3454
3455ecb_cold static void
3456compile_expr (SCHEME_P_ stream s, pointer x)
3457{
3458 if (x == NIL)
3459 {
3460 stream_put (s, BOP_NIL);
3461 return;
3462 }
3463
3464 if (is_pair (x))
3465 {
3466 pointer head = car (x);
3467
3468 if (is_syntax (head))
3469 {
3470 x = cdr (x);
3471
3472 switch (syntaxnum (head))
3473 {
3474 case OP_IF0: /* if */
3475 compile_if (SCHEME_A_ s, car (x), cadr (x), caddr (x));
3476 break;
3477
3478 case OP_OR0: /* or */
3479 compile_and_or (SCHEME_A_ s, 0, x);
3480 break;
3481
3482 case OP_AND0: /* and */
3483 compile_and_or (SCHEME_A_ s, 1, x);
3484 break;
3485
3486 case OP_CASE0: /* case */
3487 abort ();
3488 break;
3489
3490 case OP_COND0: /* cond */
3491 abort ();
3492 break;
3493
3494 case OP_LET0: /* let */
3495 case OP_LET0AST: /* let* */
3496 case OP_LET0REC: /* letrec */
3497 switch (syntaxnum (head))
3498 {
3499 case OP_LET0: stream_put (s, BOP_LET ); break;
3500 case OP_LET0AST: stream_put (s, BOP_LETAST); break;
3501 case OP_LET0REC: stream_put (s, BOP_LETREC); break;
3502 }
3503
3504 {
3505 pointer bindings = car (x);
3506 pointer body = cadr (x);
3507
3508 for (x = bindings; x != NIL; x = cdr (x))
3509 {
3510 pointer init = NIL;
3511 pointer var = car (x);
3512
3513 if (is_pair (var))
3514 {
3515 init = cdr (var);
3516 var = car (var);
3517 }
3518
3519 stream_put_v (s, symbol_id (SCHEME_A_ var));
3520 compile_expr (SCHEME_A_ s, init);
3521 }
3522
3523 stream_put_v (s, 0);
3524 compile_expr (SCHEME_A_ s, body);
3525 }
3526 break;
3527
3528 case OP_DEF0: /* define */
3529 case OP_MACRO0: /* macro */
3530 stream_put (s, syntaxnum (head) == OP_DEF0 ? BOP_DEFINE : BOP_MACRO);
3531 stream_put_v (s, cell_id (SCHEME_A_ car (x)));
3532 compile_expr (SCHEME_A_ s, cadr (x));
3533 break;
3534
3535 case OP_SET0: /* set! */
3536 stream_put (s, BOP_SET);
3537 stream_put_v (s, symbol_id (SCHEME_A_ car (x)));
3538 compile_expr (SCHEME_A_ s, cadr (x));
3539 break;
3540
3541 case OP_BEGIN: /* begin */
3542 stream_put (s, BOP_BEGIN);
3543 compile_list (SCHEME_A_ s, x);
3544 return;
3545
3546 case OP_DELAY: /* delay */
3547 abort ();
3548 break;
3549
3550 case OP_QUOTE: /* quote */
3551 stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x));
3552 break;
3553
3554 case OP_LAMBDA: /* lambda */
3555 {
3556 pointer formals = car (x);
3557 pointer body = cadr (x);
3558
3559 stream_put (s, BOP_LAMBDA);
3560
3561 for (; is_pair (formals); formals = cdr (formals))
3562 stream_put_v (s, symbol_id (SCHEME_A_ car (formals)));
3563
3564 stream_put_v (s, 0);
3565 stream_put_v (s, formals == NIL ? 0 : symbol_id (SCHEME_A_ formals));
3566
3567 compile_expr (SCHEME_A_ s, body);
3568 }
3569 break;
3570
3571 case OP_C0STREAM:/* cons-stream */
3572 abort ();
3573 break;
3574 }
3575
3576 return;
3577 }
3578
3579 pointer m = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, head, 1);
3580
3581 if (m != NIL)
3582 {
3583 m = slot_value_in_env (m);
3584
3585 if (is_macro (m))
3586 {
3587 s_save (SCHEME_A_ OP_DEBUG2, SCHEME_V->args, SCHEME_V->code);
3588 SCHEME_V->code = m;
3589 SCHEME_V->args = cons (x, NIL);
3590 Eval_Cycle (SCHEME_A_ OP_APPLY);
3591 x = SCHEME_V->value;
3592 compile_expr (SCHEME_A_ s, SCHEME_V->value);
3593 return;
3594 }
3595 }
3596 }
3597
3598 switch (type (x))
3599 {
3600 case T_INTEGER:
3601 {
3602 IVALUE iv = ivalue_unchecked (x);
3603 iv = iv < 0 ? ((uint32_t)-iv << 1) | 1 : (uint32_t)iv << 1;
3604 stream_put_tv (s, BOP_INTEGER, iv);
3605 }
3606 return;
3607
3608 case T_SYMBOL:
3609 stream_put_tv (s, BOP_SYMBOL, symbol_id (SCHEME_A_ x));
3610 return;
3611
3612 case T_PAIR:
3613 stream_put (s, BOP_LIST_BEG);
3614
3615 for (; x != NIL; x = cdr (x))
3616 compile_expr (SCHEME_A_ s, car (x));
3617
3618 stream_put (s, BOP_LIST_END);
3619 return;
3620
3621 default:
3622 stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x));
3623 break;
3624 }
3625}
3626
3627ecb_cold static int
3628compile_closure (SCHEME_P_ pointer p)
3629{
3630 stream s = stream_init ();
3631
3632 compile_list (SCHEME_A_ s, cdar (p));
3633
3634 FILE *xxd = popen ("xxd", "we");
3635 fwrite (stream_data (s), 1, stream_size (s), xxd);
3636 fclose (xxd);
3637
3638 return stream_size (s);
3639}
3640
3641#endif
3642
3643/* syntax, eval, core, ... */
3644ecb_hot static int
3330opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3645opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3331{ 3646{
3332 pointer args = SCHEME_V->args; 3647 pointer args = SCHEME_V->args;
3333 pointer x, y; 3648 pointer x, y;
3334 3649
3335 switch (op) 3650 switch (op)
3336 { 3651 {
3337#if EXPERIMENT //D 3652#if EXPERIMENT //D
3338 case OP_DEBUG: 3653 case OP_DEBUG:
3339 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); 3654 {
3655 uint32_t len = compile_closure (SCHEME_A_ car (args));
3656 printf ("len = %d\n", len);
3340 printf ("\n"); 3657 printf ("\n");
3341 s_return (S_T); 3658 s_return (S_T);
3659 }
3660
3661 case OP_DEBUG2:
3662 return -1;
3342#endif 3663#endif
3664
3343 case OP_LOAD: /* load */ 3665 case OP_LOAD: /* load */
3344 if (file_interactive (SCHEME_A)) 3666 if (file_interactive (SCHEME_A))
3345 { 3667 {
3346 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); 3668 putstr (SCHEME_A_ "Loading ");
3347 //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args))); 3669 putstr (SCHEME_A_ strvalue (car (args)));
3670 putcharacter (SCHEME_A_ '\n');
3348 } 3671 }
3349 3672
3350 if (!file_push (SCHEME_A_ strvalue (car (args)))) 3673 if (!file_push (SCHEME_A_ strvalue (car (args))))
3351 Error_1 ("unable to open", car (args)); 3674 Error_1 ("unable to open", car (args));
3352 else 3675
3353 {
3354 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3676 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3355 s_goto (OP_T0LVL); 3677 s_goto (OP_T0LVL);
3356 }
3357 3678
3358 case OP_T0LVL: /* top level */ 3679 case OP_T0LVL: /* top level */
3359 3680
3360 /* If we reached the end of file, this loop is done. */ 3681 /* If we reached the end of file, this loop is done. */
3361 if (port (SCHEME_V->loadport)->kind & port_saw_EOF) 3682 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3377 /* If interactive, be nice to user. */ 3698 /* If interactive, be nice to user. */
3378 if (file_interactive (SCHEME_A)) 3699 if (file_interactive (SCHEME_A))
3379 { 3700 {
3380 SCHEME_V->envir = SCHEME_V->global_env; 3701 SCHEME_V->envir = SCHEME_V->global_env;
3381 dump_stack_reset (SCHEME_A); 3702 dump_stack_reset (SCHEME_A);
3382 putstr (SCHEME_A_ "\n"); 3703 putcharacter (SCHEME_A_ '\n');
3704#if EXPERIMENT
3705 system ("ps v $PPID");
3706#endif
3383 putstr (SCHEME_A_ prompt); 3707 putstr (SCHEME_A_ prompt);
3384 } 3708 }
3385 3709
3386 /* Set up another iteration of REPL */ 3710 /* Set up another iteration of REPL */
3387 SCHEME_V->nesting = 0; 3711 SCHEME_V->nesting = 0;
3422 { 3746 {
3423 SCHEME_V->print_flag = 1; 3747 SCHEME_V->print_flag = 1;
3424 SCHEME_V->args = SCHEME_V->value; 3748 SCHEME_V->args = SCHEME_V->value;
3425 s_goto (OP_P0LIST); 3749 s_goto (OP_P0LIST);
3426 } 3750 }
3427 else 3751
3428 s_return (SCHEME_V->value); 3752 s_return (SCHEME_V->value);
3429 3753
3430 case OP_EVAL: /* main part of evaluation */ 3754 case OP_EVAL: /* main part of evaluation */
3431#if USE_TRACING 3755#if USE_TRACING
3432 if (SCHEME_V->tracing) 3756 if (SCHEME_V->tracing)
3433 { 3757 {
3466 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ 3790 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */
3467 SCHEME_V->code = x; 3791 SCHEME_V->code = x;
3468 s_goto (OP_EVAL); 3792 s_goto (OP_EVAL);
3469 } 3793 }
3470 } 3794 }
3471 else 3795
3472 s_return (SCHEME_V->code); 3796 s_return (SCHEME_V->code);
3473 3797
3474 case OP_E0ARGS: /* eval arguments */ 3798 case OP_E0ARGS: /* eval arguments */
3475 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ 3799 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3476 { 3800 {
3477 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3801 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3478 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3802 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3479 SCHEME_V->code = SCHEME_V->value; 3803 SCHEME_V->code = SCHEME_V->value;
3480 s_goto (OP_APPLY); 3804 s_goto (OP_APPLY);
3481 } 3805 }
3482 else 3806
3483 {
3484 SCHEME_V->code = cdr (SCHEME_V->code); 3807 SCHEME_V->code = cdr (SCHEME_V->code);
3485 s_goto (OP_E1ARGS); 3808 s_goto (OP_E1ARGS);
3486 }
3487 3809
3488 case OP_E1ARGS: /* eval arguments */ 3810 case OP_E1ARGS: /* eval arguments */
3489 args = cons (SCHEME_V->value, args); 3811 args = cons (SCHEME_V->value, args);
3490 3812
3491 if (is_pair (SCHEME_V->code)) /* continue */ 3813 if (is_pair (SCHEME_V->code)) /* continue */
3502 SCHEME_V->args = cdr (args); 3824 SCHEME_V->args = cdr (args);
3503 s_goto (OP_APPLY); 3825 s_goto (OP_APPLY);
3504 } 3826 }
3505 3827
3506#if USE_TRACING 3828#if USE_TRACING
3507
3508 case OP_TRACING: 3829 case OP_TRACING:
3509 { 3830 {
3510 int tr = SCHEME_V->tracing; 3831 int tr = SCHEME_V->tracing;
3511 3832
3512 SCHEME_V->tracing = ivalue_unchecked (car (args)); 3833 SCHEME_V->tracing = ivalue_unchecked (car (args));
3513 s_return (mk_integer (SCHEME_A_ tr)); 3834 s_return (mk_integer (SCHEME_A_ tr));
3514 } 3835 }
3515
3516#endif 3836#endif
3517 3837
3518 case OP_APPLY: /* apply 'code' to 'args' */ 3838 case OP_APPLY: /* apply 'code' to 'args' */
3519#if USE_TRACING 3839#if USE_TRACING
3520 if (SCHEME_V->tracing) 3840 if (SCHEME_V->tracing)
3574 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ 3894 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */
3575 { 3895 {
3576 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); 3896 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code));
3577 s_return (args != NIL ? car (args) : NIL); 3897 s_return (args != NIL ? car (args) : NIL);
3578 } 3898 }
3579 else 3899
3580 Error_0 ("illegal function"); 3900 Error_0 ("illegal function");
3581 3901
3582 case OP_DOMACRO: /* do macro */ 3902 case OP_DOMACRO: /* do macro */
3583 SCHEME_V->code = SCHEME_V->value; 3903 SCHEME_V->code = SCHEME_V->value;
3584 s_goto (OP_EVAL); 3904 s_goto (OP_EVAL);
3585 3905
3649 else 3969 else
3650 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); 3970 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value);
3651 3971
3652 s_return (SCHEME_V->code); 3972 s_return (SCHEME_V->code);
3653 3973
3654
3655 case OP_DEFP: /* defined? */ 3974 case OP_DEFP: /* defined? */
3656 x = SCHEME_V->envir; 3975 x = SCHEME_V->envir;
3657 3976
3658 if (cdr (args) != NIL) 3977 if (cdr (args) != NIL)
3659 x = cadr (args); 3978 x = cadr (args);
3677 s_return (SCHEME_V->value); 3996 s_return (SCHEME_V->value);
3678 } 3997 }
3679 else 3998 else
3680 Error_1 ("set!: unbound variable:", SCHEME_V->code); 3999 Error_1 ("set!: unbound variable:", SCHEME_V->code);
3681 4000
3682
3683 case OP_BEGIN: /* begin */ 4001 case OP_BEGIN: /* begin */
3684 if (!is_pair (SCHEME_V->code)) 4002 if (!is_pair (SCHEME_V->code))
3685 s_return (SCHEME_V->code); 4003 s_return (SCHEME_V->code);
3686 4004
3687 if (cdr (SCHEME_V->code) != NIL) 4005 if (cdr (SCHEME_V->code) != NIL)
3698 case OP_IF1: /* if */ 4016 case OP_IF1: /* if */
3699 if (is_true (SCHEME_V->value)) 4017 if (is_true (SCHEME_V->value))
3700 SCHEME_V->code = car (SCHEME_V->code); 4018 SCHEME_V->code = car (SCHEME_V->code);
3701 else 4019 else
3702 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ 4020 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
4021
3703 s_goto (OP_EVAL); 4022 s_goto (OP_EVAL);
3704 4023
3705 case OP_LET0: /* let */ 4024 case OP_LET0: /* let */
3706 SCHEME_V->args = NIL; 4025 SCHEME_V->args = NIL;
3707 SCHEME_V->value = SCHEME_V->code; 4026 SCHEME_V->value = SCHEME_V->code;
3708 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); 4027 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code);
3709 s_goto (OP_LET1); 4028 s_goto (OP_LET1);
3710 4029
3711 case OP_LET1: /* let (calculate parameters) */ 4030 case OP_LET1: /* let (calculate parameters) */
4031 case OP_LET1REC: /* letrec (calculate parameters) */
3712 args = cons (SCHEME_V->value, args); 4032 args = cons (SCHEME_V->value, args);
3713 4033
3714 if (is_pair (SCHEME_V->code)) /* continue */ 4034 if (is_pair (SCHEME_V->code)) /* continue */
3715 { 4035 {
3716 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 4036 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3717 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); 4037 Error_1 ("Bad syntax of binding spec in let/letrec:", car (SCHEME_V->code));
3718 4038
3719 s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code)); 4039 s_save (SCHEME_A_ op, args, cdr (SCHEME_V->code));
3720 SCHEME_V->code = cadar (SCHEME_V->code); 4040 SCHEME_V->code = cadar (SCHEME_V->code);
3721 SCHEME_V->args = NIL; 4041 SCHEME_V->args = NIL;
3722 s_goto (OP_EVAL); 4042 s_goto (OP_EVAL);
3723 } 4043 }
3724 else /* end */ 4044
3725 { 4045 /* end */
3726 args = reverse_in_place (SCHEME_A_ NIL, args); 4046 args = reverse_in_place (SCHEME_A_ NIL, args);
3727 SCHEME_V->code = car (args); 4047 SCHEME_V->code = car (args);
3728 SCHEME_V->args = cdr (args); 4048 SCHEME_V->args = cdr (args);
3729 s_goto (OP_LET2); 4049 s_goto (op == OP_LET1 ? OP_LET2 : OP_LET2REC);
3730 }
3731 4050
3732 case OP_LET2: /* let */ 4051 case OP_LET2: /* let */
3733 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4052 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3734 4053
3735 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args; 4054 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args;
3739 if (is_symbol (car (SCHEME_V->code))) /* named let */ 4058 if (is_symbol (car (SCHEME_V->code))) /* named let */
3740 { 4059 {
3741 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x)) 4060 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x))
3742 { 4061 {
3743 if (!is_pair (x)) 4062 if (!is_pair (x))
3744 Error_1 ("Bad syntax of binding in let :", x); 4063 Error_1 ("Bad syntax of binding in let:", x);
3745 4064
3746 if (!is_list (SCHEME_A_ car (x))) 4065 if (!is_list (SCHEME_A_ car (x)))
3747 Error_1 ("Bad syntax of binding in let :", car (x)); 4066 Error_1 ("Bad syntax of binding in let:", car (x));
3748 4067
3749 args = cons (caar (x), args); 4068 args = cons (caar (x), args);
3750 } 4069 }
3751 4070
3752 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)), 4071 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)),
3769 SCHEME_V->code = cdr (SCHEME_V->code); 4088 SCHEME_V->code = cdr (SCHEME_V->code);
3770 s_goto (OP_BEGIN); 4089 s_goto (OP_BEGIN);
3771 } 4090 }
3772 4091
3773 if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code))) 4092 if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code)))
3774 Error_1 ("Bad syntax of binding spec in let* :", car (SCHEME_V->code)); 4093 Error_1 ("Bad syntax of binding spec in let*:", car (SCHEME_V->code));
3775 4094
3776 s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code)); 4095 s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code));
3777 SCHEME_V->code = car (cdaar (SCHEME_V->code)); 4096 SCHEME_V->code = car (cdaar (SCHEME_V->code));
3778 s_goto (OP_EVAL); 4097 s_goto (OP_EVAL);
3779 4098
3790 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); 4109 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code);
3791 SCHEME_V->code = cadar (SCHEME_V->code); 4110 SCHEME_V->code = cadar (SCHEME_V->code);
3792 SCHEME_V->args = NIL; 4111 SCHEME_V->args = NIL;
3793 s_goto (OP_EVAL); 4112 s_goto (OP_EVAL);
3794 } 4113 }
3795 else /* end */ 4114
4115 /* end */
3796 { 4116
3797 SCHEME_V->code = args; 4117 SCHEME_V->code = args;
3798 SCHEME_V->args = NIL; 4118 SCHEME_V->args = NIL;
3799 s_goto (OP_BEGIN); 4119 s_goto (OP_BEGIN);
3800 }
3801 4120
3802 case OP_LET0REC: /* letrec */ 4121 case OP_LET0REC: /* letrec */
3803 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4122 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3804 SCHEME_V->args = NIL; 4123 SCHEME_V->args = NIL;
3805 SCHEME_V->value = SCHEME_V->code; 4124 SCHEME_V->value = SCHEME_V->code;
3806 SCHEME_V->code = car (SCHEME_V->code); 4125 SCHEME_V->code = car (SCHEME_V->code);
3807 s_goto (OP_LET1REC); 4126 s_goto (OP_LET1REC);
3808 4127
3809 case OP_LET1REC: /* letrec (calculate parameters) */ 4128 /* OP_LET1REC handled by OP_LET1 */
3810 args = cons (SCHEME_V->value, args);
3811
3812 if (is_pair (SCHEME_V->code)) /* continue */
3813 {
3814 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3815 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3816
3817 s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code));
3818 SCHEME_V->code = cadar (SCHEME_V->code);
3819 SCHEME_V->args = NIL;
3820 s_goto (OP_EVAL);
3821 }
3822 else /* end */
3823 {
3824 args = reverse_in_place (SCHEME_A_ NIL, args);
3825 SCHEME_V->code = car (args);
3826 SCHEME_V->args = cdr (args);
3827 s_goto (OP_LET2REC);
3828 }
3829 4129
3830 case OP_LET2REC: /* letrec */ 4130 case OP_LET2REC: /* letrec */
3831 for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y)) 4131 for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y))
3832 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 4132 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3833 4133
3863 } 4163 }
3864 else 4164 else
3865 { 4165 {
3866 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) 4166 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL)
3867 s_return (NIL); 4167 s_return (NIL);
3868 else 4168
3869 {
3870 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 4169 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
3871 SCHEME_V->code = caar (SCHEME_V->code); 4170 SCHEME_V->code = caar (SCHEME_V->code);
3872 s_goto (OP_EVAL); 4171 s_goto (OP_EVAL);
3873 }
3874 } 4172 }
3875 4173
3876 case OP_DELAY: /* delay */ 4174 case OP_DELAY: /* delay */
3877 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); 4175 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir);
3878 set_typeflag (x, T_PROMISE); 4176 set_typeflag (x, T_PROMISE);
3889 case OP_AND1: /* and */ 4187 case OP_AND1: /* and */
3890 if (is_false (SCHEME_V->value)) 4188 if (is_false (SCHEME_V->value))
3891 s_return (SCHEME_V->value); 4189 s_return (SCHEME_V->value);
3892 else if (SCHEME_V->code == NIL) 4190 else if (SCHEME_V->code == NIL)
3893 s_return (SCHEME_V->value); 4191 s_return (SCHEME_V->value);
3894 else 4192
3895 {
3896 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 4193 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
3897 SCHEME_V->code = car (SCHEME_V->code); 4194 SCHEME_V->code = car (SCHEME_V->code);
3898 s_goto (OP_EVAL); 4195 s_goto (OP_EVAL);
3899 }
3900 4196
3901 case OP_OR0: /* or */ 4197 case OP_OR0: /* or */
3902 if (SCHEME_V->code == NIL) 4198 if (SCHEME_V->code == NIL)
3903 s_return (S_F); 4199 s_return (S_F);
3904 4200
3909 case OP_OR1: /* or */ 4205 case OP_OR1: /* or */
3910 if (is_true (SCHEME_V->value)) 4206 if (is_true (SCHEME_V->value))
3911 s_return (SCHEME_V->value); 4207 s_return (SCHEME_V->value);
3912 else if (SCHEME_V->code == NIL) 4208 else if (SCHEME_V->code == NIL)
3913 s_return (SCHEME_V->value); 4209 s_return (SCHEME_V->value);
3914 else 4210
3915 {
3916 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 4211 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
3917 SCHEME_V->code = car (SCHEME_V->code); 4212 SCHEME_V->code = car (SCHEME_V->code);
3918 s_goto (OP_EVAL); 4213 s_goto (OP_EVAL);
3919 }
3920 4214
3921 case OP_C0STREAM: /* cons-stream */ 4215 case OP_C0STREAM: /* cons-stream */
3922 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 4216 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3923 SCHEME_V->code = car (SCHEME_V->code); 4217 SCHEME_V->code = car (SCHEME_V->code);
3924 s_goto (OP_EVAL); 4218 s_goto (OP_EVAL);
3989 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); 4283 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x));
3990 SCHEME_V->code = caar (x); 4284 SCHEME_V->code = caar (x);
3991 s_goto (OP_EVAL); 4285 s_goto (OP_EVAL);
3992 } 4286 }
3993 } 4287 }
3994 else 4288
3995 s_return (NIL); 4289 s_return (NIL);
3996 4290
3997 case OP_CASE2: /* case */ 4291 case OP_CASE2: /* case */
3998 if (is_true (SCHEME_V->value)) 4292 if (is_true (SCHEME_V->value))
3999 s_goto (OP_BEGIN); 4293 s_goto (OP_BEGIN);
4000 else 4294
4001 s_return (NIL); 4295 s_return (NIL);
4002 4296
4003 case OP_PAPPLY: /* apply */ 4297 case OP_PAPPLY: /* apply */
4004 SCHEME_V->code = car (args); 4298 SCHEME_V->code = car (args);
4005 SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); 4299 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
4006 /*SCHEME_V->args = cadr(args); */ 4300 /*SCHEME_V->args = cadr(args); */
4020 } 4314 }
4021 4315
4022 if (USE_ERROR_CHECKING) abort (); 4316 if (USE_ERROR_CHECKING) abort ();
4023} 4317}
4024 4318
4025static int 4319/* math, cxr */
4320ecb_hot static int
4026opexe_1 (SCHEME_P_ enum scheme_opcodes op) 4321opexe_1 (SCHEME_P_ enum scheme_opcodes op)
4027{ 4322{
4028 pointer args = SCHEME_V->args; 4323 pointer args = SCHEME_V->args;
4029 pointer x = car (args); 4324 pointer x = car (args);
4030 num v; 4325 num v;
4031 4326
4032 switch (op) 4327 switch (op)
4033 { 4328 {
4034#if USE_MATH 4329#if USE_MATH
4035 case OP_INEX2EX: /* inexact->exact */ 4330 case OP_INEX2EX: /* inexact->exact */
4036 {
4037 if (is_integer (x)) 4331 if (!is_integer (x))
4038 s_return (x); 4332 {
4039
4040 RVALUE r = rvalue_unchecked (x); 4333 RVALUE r = rvalue_unchecked (x);
4041 4334
4042 if (r == (RVALUE)(IVALUE)r) 4335 if (r == (RVALUE)(IVALUE)r)
4043 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); 4336 x = mk_integer (SCHEME_A_ rvalue_unchecked (x));
4044 else 4337 else
4045 Error_1 ("inexact->exact: not integral:", x); 4338 Error_1 ("inexact->exact: not integral:", x);
4046 } 4339 }
4047 4340
4341 s_return (x);
4342
4343 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4344 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4345 case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x))));
4346 case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x))));
4347
4348 case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4048 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4349 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
4049 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 4350 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))
4351 / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args))))));
4050 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 4352 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
4051 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 4353 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
4052 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); 4354 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x))));
4053 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); 4355 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x))));
4054 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); 4356 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x))));
4055 4357
4056 case OP_ATAN: 4358 case OP_ATAN:
4359 s_return (mk_real (SCHEME_A_
4057 if (cdr (args) == NIL) 4360 cdr (args) == NIL
4058 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 4361 ? atan (rvalue (x))
4059 else 4362 : atan2 (rvalue (x), rvalue (cadr (args)))));
4060 {
4061 pointer y = cadr (args);
4062 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
4063 }
4064
4065 case OP_SQRT:
4066 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4067 4363
4068 case OP_EXPT: 4364 case OP_EXPT:
4069 { 4365 {
4070 RVALUE result; 4366 RVALUE result;
4071 int real_result = 1; 4367 int real_result = 1;
4094 if (real_result) 4390 if (real_result)
4095 s_return (mk_real (SCHEME_A_ result)); 4391 s_return (mk_real (SCHEME_A_ result));
4096 else 4392 else
4097 s_return (mk_integer (SCHEME_A_ result)); 4393 s_return (mk_integer (SCHEME_A_ result));
4098 } 4394 }
4099
4100 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4101 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4102
4103 case OP_TRUNCATE:
4104 {
4105 RVALUE n = rvalue (x);
4106 s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n)));
4107 }
4108
4109 case OP_ROUND:
4110 if (is_integer (x))
4111 s_return (x);
4112
4113 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4114#endif 4395#endif
4115 4396
4116 case OP_ADD: /* + */ 4397 case OP_ADD: /* + */
4117 v = num_zero; 4398 v = num_zero;
4118 4399
4420 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4701 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4421 4702
4422 s_return (newstr); 4703 s_return (newstr);
4423 } 4704 }
4424 4705
4425 case OP_SUBSTR: /* substring */ 4706 case OP_STRING_COPY: /* substring/string-copy */
4426 { 4707 {
4427 char *str = strvalue (x); 4708 char *str = strvalue (x);
4428 int index0 = ivalue_unchecked (cadr (args)); 4709 int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args));
4429 int index1; 4710 int index1;
4430 int len; 4711 int len;
4431 4712
4432 if (index0 > strlength (x)) 4713 if (index0 > strlength (x))
4433 Error_1 ("substring: start out of bounds:", cadr (args)); 4714 Error_1 ("string->copy: start out of bounds:", cadr (args));
4434 4715
4435 if (cddr (args) != NIL) 4716 if (cddr (args) != NIL)
4436 { 4717 {
4437 index1 = ivalue_unchecked (caddr (args)); 4718 index1 = ivalue_unchecked (caddr (args));
4438 4719
4439 if (index1 > strlength (x) || index1 < index0) 4720 if (index1 > strlength (x) || index1 < index0)
4440 Error_1 ("substring: end out of bounds:", caddr (args)); 4721 Error_1 ("string->copy: end out of bounds:", caddr (args));
4441 } 4722 }
4442 else 4723 else
4443 index1 = strlength (x); 4724 index1 = strlength (x);
4444 4725
4445 len = index1 - index0; 4726 len = index1 - index0;
4446 x = mk_empty_string (SCHEME_A_ len, ' '); 4727 x = mk_counted_string (SCHEME_A_ str + index0, len);
4447 memcpy (strvalue (x), str + index0, len);
4448 strvalue (x)[len] = 0;
4449 4728
4450 s_return (x); 4729 s_return (x);
4451 } 4730 }
4452 4731
4453 case OP_VECTOR: /* vector */ 4732 case OP_VECTOR: /* vector */
4527 } 4806 }
4528 4807
4529 if (USE_ERROR_CHECKING) abort (); 4808 if (USE_ERROR_CHECKING) abort ();
4530} 4809}
4531 4810
4532static int 4811/* relational ops */
4812ecb_hot static int
4533opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4813opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4534{ 4814{
4535 pointer x = SCHEME_V->args; 4815 pointer x = SCHEME_V->args;
4536 4816
4537 for (;;) 4817 for (;;)
4558 } 4838 }
4559 4839
4560 s_return (S_T); 4840 s_return (S_T);
4561} 4841}
4562 4842
4563static int 4843/* predicates */
4844ecb_hot static int
4564opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4845opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4565{ 4846{
4566 pointer args = SCHEME_V->args; 4847 pointer args = SCHEME_V->args;
4567 pointer a = car (args); 4848 pointer a = car (args);
4568 pointer d = cdr (args); 4849 pointer d = cdr (args);
4615 } 4896 }
4616 4897
4617 s_retbool (r); 4898 s_retbool (r);
4618} 4899}
4619 4900
4620static int 4901/* promises, list ops, ports */
4902ecb_hot static int
4621opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4903opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4622{ 4904{
4623 pointer args = SCHEME_V->args; 4905 pointer args = SCHEME_V->args;
4624 pointer a = car (args); 4906 pointer a = car (args);
4625 pointer x, y; 4907 pointer x, y;
4642 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4924 case OP_SAVE_FORCED: /* Save forced value replacing promise */
4643 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); 4925 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
4644 s_return (SCHEME_V->value); 4926 s_return (SCHEME_V->value);
4645 4927
4646#if USE_PORTS 4928#if USE_PORTS
4929
4930 case OP_EOF_OBJECT: /* eof-object */
4931 s_return (S_EOF);
4647 4932
4648 case OP_WRITE: /* write */ 4933 case OP_WRITE: /* write */
4649 case OP_DISPLAY: /* display */ 4934 case OP_DISPLAY: /* display */
4650 case OP_WRITE_CHAR: /* write-char */ 4935 case OP_WRITE_CHAR: /* write-char */
4651 if (is_pair (cdr (SCHEME_V->args))) 4936 if (is_pair (cdr (SCHEME_V->args)))
4665 else 4950 else
4666 SCHEME_V->print_flag = 0; 4951 SCHEME_V->print_flag = 0;
4667 4952
4668 s_goto (OP_P0LIST); 4953 s_goto (OP_P0LIST);
4669 4954
4955 //TODO: move to scheme
4670 case OP_NEWLINE: /* newline */ 4956 case OP_NEWLINE: /* newline */
4671 if (is_pair (args)) 4957 if (is_pair (args))
4672 { 4958 {
4673 if (a != SCHEME_V->outport) 4959 if (a != SCHEME_V->outport)
4674 { 4960 {
4676 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4962 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4677 SCHEME_V->outport = a; 4963 SCHEME_V->outport = a;
4678 } 4964 }
4679 } 4965 }
4680 4966
4681 putstr (SCHEME_A_ "\n"); 4967 putcharacter (SCHEME_A_ '\n');
4682 s_return (S_T); 4968 s_return (S_T);
4683#endif 4969#endif
4684 4970
4685 case OP_ERR0: /* error */ 4971 case OP_ERR0: /* error */
4686 SCHEME_V->retcode = -1; 4972 SCHEME_V->retcode = -1;
4695 putstr (SCHEME_A_ strvalue (car (args))); 4981 putstr (SCHEME_A_ strvalue (car (args)));
4696 SCHEME_V->args = cdr (args); 4982 SCHEME_V->args = cdr (args);
4697 s_goto (OP_ERR1); 4983 s_goto (OP_ERR1);
4698 4984
4699 case OP_ERR1: /* error */ 4985 case OP_ERR1: /* error */
4700 putstr (SCHEME_A_ " "); 4986 putcharacter (SCHEME_A_ ' ');
4701 4987
4702 if (args != NIL) 4988 if (args != NIL)
4703 { 4989 {
4704 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); 4990 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4705 SCHEME_V->args = a; 4991 SCHEME_V->args = a;
4706 SCHEME_V->print_flag = 1; 4992 SCHEME_V->print_flag = 1;
4707 s_goto (OP_P0LIST); 4993 s_goto (OP_P0LIST);
4708 } 4994 }
4709 else 4995 else
4710 { 4996 {
4711 putstr (SCHEME_A_ "\n"); 4997 putcharacter (SCHEME_A_ '\n');
4712 4998
4713 if (SCHEME_V->interactive_repl) 4999 if (SCHEME_V->interactive_repl)
4714 s_goto (OP_T0LVL); 5000 s_goto (OP_T0LVL);
4715 else 5001 else
4716 return -1; 5002 return -1;
4924 } 5210 }
4925 5211
4926 if (USE_ERROR_CHECKING) abort (); 5212 if (USE_ERROR_CHECKING) abort ();
4927} 5213}
4928 5214
4929static int 5215/* reading */
5216ecb_cold static int
4930opexe_5 (SCHEME_P_ enum scheme_opcodes op) 5217opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4931{ 5218{
4932 pointer args = SCHEME_V->args; 5219 pointer args = SCHEME_V->args;
4933 pointer x; 5220 pointer x;
4934 5221
5013 case OP_RDSEXPR: 5300 case OP_RDSEXPR:
5014 switch (SCHEME_V->tok) 5301 switch (SCHEME_V->tok)
5015 { 5302 {
5016 case TOK_EOF: 5303 case TOK_EOF:
5017 s_return (S_EOF); 5304 s_return (S_EOF);
5018 /* NOTREACHED */
5019 5305
5020 case TOK_VEC: 5306 case TOK_VEC:
5021 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); 5307 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL);
5022 /* fall through */ 5308 /* fall through */
5023 5309
5026 5312
5027 if (SCHEME_V->tok == TOK_RPAREN) 5313 if (SCHEME_V->tok == TOK_RPAREN)
5028 s_return (NIL); 5314 s_return (NIL);
5029 else if (SCHEME_V->tok == TOK_DOT) 5315 else if (SCHEME_V->tok == TOK_DOT)
5030 Error_0 ("syntax error: illegal dot expression"); 5316 Error_0 ("syntax error: illegal dot expression");
5031 else 5317
5032 {
5033 SCHEME_V->nesting_stack[SCHEME_V->file_i]++; 5318 SCHEME_V->nesting_stack[SCHEME_V->file_i]++;
5034 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL); 5319 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL);
5035 s_goto (OP_RDSEXPR); 5320 s_goto (OP_RDSEXPR);
5036 }
5037 5321
5038 case TOK_QUOTE: 5322 case TOK_QUOTE:
5039 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL); 5323 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL);
5040 SCHEME_V->tok = token (SCHEME_A); 5324 SCHEME_V->tok = token (SCHEME_A);
5041 s_goto (OP_RDSEXPR); 5325 s_goto (OP_RDSEXPR);
5047 { 5331 {
5048 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); 5332 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL);
5049 SCHEME_V->tok = TOK_LPAREN; 5333 SCHEME_V->tok = TOK_LPAREN;
5050 s_goto (OP_RDSEXPR); 5334 s_goto (OP_RDSEXPR);
5051 } 5335 }
5052 else 5336
5053 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); 5337 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL);
5054
5055 s_goto (OP_RDSEXPR); 5338 s_goto (OP_RDSEXPR);
5056 5339
5057 case TOK_COMMA: 5340 case TOK_COMMA:
5058 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); 5341 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL);
5059 SCHEME_V->tok = token (SCHEME_A); 5342 SCHEME_V->tok = token (SCHEME_A);
5070 case TOK_DOTATOM: 5353 case TOK_DOTATOM:
5071 SCHEME_V->strbuff[0] = '.'; 5354 SCHEME_V->strbuff[0] = '.';
5072 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); 5355 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5073 5356
5074 case TOK_STRATOM: 5357 case TOK_STRATOM:
5358 //TODO: haven't checked whether the garbage collector could interfere and free x
5359 gc (SCHEME_A_ NIL, NIL); //TODO: superheavyhanded
5075 x = readstrexp (SCHEME_A_ '|'); 5360 x = readstrexp (SCHEME_A_ '|');
5076 //TODO: haven't checked whether the garbage collector could interfere
5077 s_return (mk_atom (SCHEME_A_ strvalue (x))); 5361 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5078 5362
5079 case TOK_DQUOTE: 5363 case TOK_DQUOTE:
5080 x = readstrexp (SCHEME_A_ '"'); 5364 x = readstrexp (SCHEME_A_ '"');
5081 5365
5089 { 5373 {
5090 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1); 5374 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1);
5091 5375
5092 if (f == NIL) 5376 if (f == NIL)
5093 Error_0 ("undefined sharp expression"); 5377 Error_0 ("undefined sharp expression");
5094 else 5378
5095 {
5096 SCHEME_V->code = cons (slot_value_in_env (f), NIL); 5379 SCHEME_V->code = cons (slot_value_in_env (f), NIL);
5097 s_goto (OP_EVAL); 5380 s_goto (OP_EVAL);
5098 }
5099 } 5381 }
5100 5382
5101 case TOK_SHARP_CONST: 5383 case TOK_SHARP_CONST:
5102 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) 5384 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5103 Error_0 ("undefined sharp expression"); 5385 Error_0 ("undefined sharp expression");
5104 else 5386
5105 s_return (x); 5387 s_return (x);
5106 5388
5107 default: 5389 default:
5108 Error_0 ("syntax error: illegal token"); 5390 Error_0 ("syntax error: illegal token");
5109 } 5391 }
5110 5392
5203 pointer b = cdr (args); 5485 pointer b = cdr (args);
5204 int ok_abbr = ok_abbrev (b); 5486 int ok_abbr = ok_abbrev (b);
5205 SCHEME_V->args = car (b); 5487 SCHEME_V->args = car (b);
5206 5488
5207 if (a == SCHEME_V->QUOTE && ok_abbr) 5489 if (a == SCHEME_V->QUOTE && ok_abbr)
5208 putstr (SCHEME_A_ "'"); 5490 putcharacter (SCHEME_A_ '\'');
5209 else if (a == SCHEME_V->QQUOTE && ok_abbr) 5491 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5210 putstr (SCHEME_A_ "`"); 5492 putcharacter (SCHEME_A_ '`');
5211 else if (a == SCHEME_V->UNQUOTE && ok_abbr) 5493 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5212 putstr (SCHEME_A_ ","); 5494 putcharacter (SCHEME_A_ ',');
5213 else if (a == SCHEME_V->UNQUOTESP && ok_abbr) 5495 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5214 putstr (SCHEME_A_ ",@"); 5496 putstr (SCHEME_A_ ",@");
5215 else 5497 else
5216 { 5498 {
5217 putstr (SCHEME_A_ "("); 5499 putcharacter (SCHEME_A_ '(');
5218 s_save (SCHEME_A_ OP_P1LIST, b, NIL); 5500 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5219 SCHEME_V->args = a; 5501 SCHEME_V->args = a;
5220 } 5502 }
5221 5503
5222 s_goto (OP_P0LIST); 5504 s_goto (OP_P0LIST);
5224 5506
5225 case OP_P1LIST: 5507 case OP_P1LIST:
5226 if (is_pair (args)) 5508 if (is_pair (args))
5227 { 5509 {
5228 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); 5510 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5229 putstr (SCHEME_A_ " "); 5511 putcharacter (SCHEME_A_ ' ');
5230 SCHEME_V->args = car (args); 5512 SCHEME_V->args = car (args);
5231 s_goto (OP_P0LIST); 5513 s_goto (OP_P0LIST);
5232 } 5514 }
5233 else if (is_vector (args)) 5515 else if (is_vector (args))
5234 { 5516 {
5242 { 5524 {
5243 putstr (SCHEME_A_ " . "); 5525 putstr (SCHEME_A_ " . ");
5244 printatom (SCHEME_A_ args, SCHEME_V->print_flag); 5526 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5245 } 5527 }
5246 5528
5247 putstr (SCHEME_A_ ")"); 5529 putcharacter (SCHEME_A_ ')');
5248 s_return (S_T); 5530 s_return (S_T);
5249 } 5531 }
5250 5532
5251 case OP_PVECFROM: 5533 case OP_PVECFROM:
5252 { 5534 {
5254 pointer vec = car (args); 5536 pointer vec = car (args);
5255 int len = veclength (vec); 5537 int len = veclength (vec);
5256 5538
5257 if (i == len) 5539 if (i == len)
5258 { 5540 {
5259 putstr (SCHEME_A_ ")"); 5541 putcharacter (SCHEME_A_ ')');
5260 s_return (S_T); 5542 s_return (S_T);
5261 } 5543 }
5262 else 5544 else
5263 { 5545 {
5264 pointer elem = vector_get (vec, i); 5546 pointer elem = vector_get (vec, i);
5266 ivalue_unchecked (cdr (args)) = i + 1; 5548 ivalue_unchecked (cdr (args)) = i + 1;
5267 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5549 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5268 SCHEME_V->args = elem; 5550 SCHEME_V->args = elem;
5269 5551
5270 if (i > 0) 5552 if (i > 0)
5271 putstr (SCHEME_A_ " "); 5553 putcharacter (SCHEME_A_ ' ');
5272 5554
5273 s_goto (OP_P0LIST); 5555 s_goto (OP_P0LIST);
5274 } 5556 }
5275 } 5557 }
5276 } 5558 }
5277 5559
5278 if (USE_ERROR_CHECKING) abort (); 5560 if (USE_ERROR_CHECKING) abort ();
5279} 5561}
5280 5562
5281static int 5563/* list ops */
5564ecb_hot static int
5282opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5565opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5283{ 5566{
5284 pointer args = SCHEME_V->args; 5567 pointer args = SCHEME_V->args;
5285 pointer a = car (args); 5568 pointer a = car (args);
5286 pointer x, y; 5569 pointer x, y;
5309 break; 5592 break;
5310 } 5593 }
5311 5594
5312 if (is_pair (y)) 5595 if (is_pair (y))
5313 s_return (car (y)); 5596 s_return (car (y));
5314 else 5597
5315 s_return (S_F); 5598 s_return (S_F);
5316
5317 5599
5318 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5600 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5319 SCHEME_V->args = a; 5601 SCHEME_V->args = a;
5320 5602
5321 if (SCHEME_V->args == NIL) 5603 if (SCHEME_V->args == NIL)
5322 s_return (S_F); 5604 s_return (S_F);
5323 else if (is_closure (SCHEME_V->args)) 5605 else if (is_closure (SCHEME_V->args) || is_macro (SCHEME_V->args))
5324 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5606 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5325 else if (is_macro (SCHEME_V->args)) 5607
5326 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5327 else
5328 s_return (S_F); 5608 s_return (S_F);
5329 5609
5330 case OP_CLOSUREP: /* closure? */ 5610 case OP_CLOSUREP: /* closure? */
5331 /* 5611 /*
5332 * Note, macro object is also a closure. 5612 * Note, macro object is also a closure.
5333 * Therefore, (closure? <#MACRO>) ==> #t 5613 * Therefore, (closure? <#MACRO>) ==> #t
5344 5624
5345/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5625/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5346typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); 5626typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5347 5627
5348typedef int (*test_predicate)(pointer); 5628typedef int (*test_predicate)(pointer);
5349static int 5629
5630ecb_hot static int
5350tst_any (pointer p) 5631tst_any (pointer p)
5351{ 5632{
5352 return 1; 5633 return 1;
5353} 5634}
5354 5635
5355static int 5636ecb_hot static int
5356tst_inonneg (pointer p) 5637tst_inonneg (pointer p)
5357{ 5638{
5358 return is_integer (p) && ivalue_unchecked (p) >= 0; 5639 return is_integer (p) && ivalue_unchecked (p) >= 0;
5359} 5640}
5360 5641
5361static int 5642ecb_hot static int
5362tst_is_list (SCHEME_P_ pointer p) 5643tst_is_list (SCHEME_P_ pointer p)
5363{ 5644{
5364 return p == NIL || is_pair (p); 5645 return p == NIL || is_pair (p);
5365} 5646}
5366 5647
5409#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" 5690#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5410#include "opdefines.h" 5691#include "opdefines.h"
5411#undef OP_DEF 5692#undef OP_DEF
5412; 5693;
5413 5694
5414static const char * 5695ecb_cold static const char *
5415opname (int idx) 5696opname (int idx)
5416{ 5697{
5417 const char *name = opnames; 5698 const char *name = opnames;
5418 5699
5419 /* should do this at compile time, but would require external program, right? */ 5700 /* should do this at compile time, but would require external program, right? */
5421 name += strlen (name) + 1; 5702 name += strlen (name) + 1;
5422 5703
5423 return *name ? name : "ILLEGAL"; 5704 return *name ? name : "ILLEGAL";
5424} 5705}
5425 5706
5426static const char * 5707ecb_cold static const char *
5427procname (pointer x) 5708procname (pointer x)
5428{ 5709{
5429 return opname (procnum (x)); 5710 return opname (procnum (x));
5430} 5711}
5431 5712
5451#undef OP_DEF 5732#undef OP_DEF
5452 {0} 5733 {0}
5453}; 5734};
5454 5735
5455/* kernel of this interpreter */ 5736/* kernel of this interpreter */
5456static void ecb_hot 5737ecb_hot static void
5457Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5738Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5458{ 5739{
5459 SCHEME_V->op = op; 5740 SCHEME_V->op = op;
5460 5741
5461 for (;;) 5742 for (;;)
5544 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) 5825 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5545 return; 5826 return;
5546 5827
5547 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5828 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5548 { 5829 {
5549 xwrstr ("No memory!\n"); 5830 putstr (SCHEME_A_ "No memory!\n");
5550 return; 5831 return;
5551 } 5832 }
5552 } 5833 }
5553} 5834}
5554 5835
5555/* ========== Initialization of internal keywords ========== */ 5836/* ========== Initialization of internal keywords ========== */
5556 5837
5557static void 5838ecb_cold static void
5558assign_syntax (SCHEME_P_ const char *name) 5839assign_syntax (SCHEME_P_ const char *name)
5559{ 5840{
5560 pointer x = oblist_add_by_name (SCHEME_A_ name); 5841 pointer x = oblist_add_by_name (SCHEME_A_ name);
5561 set_typeflag (x, typeflag (x) | T_SYNTAX); 5842 set_typeflag (x, typeflag (x) | T_SYNTAX);
5562} 5843}
5563 5844
5564static void 5845ecb_cold static void
5565assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) 5846assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
5566{ 5847{
5567 pointer x = mk_symbol (SCHEME_A_ name); 5848 pointer x = mk_symbol (SCHEME_A_ name);
5568 pointer y = mk_proc (SCHEME_A_ op); 5849 pointer y = mk_proc (SCHEME_A_ op);
5569 new_slot_in_env (SCHEME_A_ x, y); 5850 new_slot_in_env (SCHEME_A_ x, y);
5577 ivalue_unchecked (y) = op; 5858 ivalue_unchecked (y) = op;
5578 return y; 5859 return y;
5579} 5860}
5580 5861
5581/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5862/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5582static int 5863ecb_hot static int
5583syntaxnum (pointer p) 5864syntaxnum (pointer p)
5584{ 5865{
5585 const char *s = strvalue (p); 5866 const char *s = strvalue (p);
5586 5867
5587 switch (strlength (p)) 5868 switch (strlength (p))
5704#endif 5985#endif
5705 } 5986 }
5706 5987
5707 SCHEME_V->gc_verbose = 0; 5988 SCHEME_V->gc_verbose = 0;
5708 dump_stack_initialize (SCHEME_A); 5989 dump_stack_initialize (SCHEME_A);
5709 SCHEME_V->code = NIL; 5990 SCHEME_V->code = NIL;
5710 SCHEME_V->args = NIL; 5991 SCHEME_V->args = NIL;
5711 SCHEME_V->envir = NIL; 5992 SCHEME_V->envir = NIL;
5993 SCHEME_V->value = NIL;
5712 SCHEME_V->tracing = 0; 5994 SCHEME_V->tracing = 0;
5713 5995
5714 /* init NIL */ 5996 /* init NIL */
5715 set_typeflag (NIL, T_ATOM | T_MARK); 5997 set_typeflag (NIL, T_ATOM | T_MARK);
5716 set_car (NIL, NIL); 5998 set_car (NIL, NIL);
5772 6054
5773 return !SCHEME_V->no_memory; 6055 return !SCHEME_V->no_memory;
5774} 6056}
5775 6057
5776#if USE_PORTS 6058#if USE_PORTS
5777void 6059ecb_cold void
5778scheme_set_input_port_file (SCHEME_P_ int fin) 6060scheme_set_input_port_file (SCHEME_P_ int fin)
5779{ 6061{
5780 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); 6062 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input);
5781} 6063}
5782 6064
5783void 6065ecb_cold void
5784scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) 6066scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end)
5785{ 6067{
5786 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); 6068 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input);
5787} 6069}
5788 6070
5789void 6071ecb_cold void
5790scheme_set_output_port_file (SCHEME_P_ int fout) 6072scheme_set_output_port_file (SCHEME_P_ int fout)
5791{ 6073{
5792 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); 6074 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output);
5793} 6075}
5794 6076
5795void 6077ecb_cold void
5796scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) 6078scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end)
5797{ 6079{
5798 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); 6080 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output);
5799} 6081}
5800#endif 6082#endif
5801 6083
5802void 6084ecb_cold void
5803scheme_set_external_data (SCHEME_P_ void *p) 6085scheme_set_external_data (SCHEME_P_ void *p)
5804{ 6086{
5805 SCHEME_V->ext_data = p; 6087 SCHEME_V->ext_data = p;
5806} 6088}
5807 6089
5839 SCHEME_V->loadport = NIL; 6121 SCHEME_V->loadport = NIL;
5840 SCHEME_V->gc_verbose = 0; 6122 SCHEME_V->gc_verbose = 0;
5841 gc (SCHEME_A_ NIL, NIL); 6123 gc (SCHEME_A_ NIL, NIL);
5842 6124
5843 for (i = 0; i <= SCHEME_V->last_cell_seg; i++) 6125 for (i = 0; i <= SCHEME_V->last_cell_seg; i++)
5844 free (SCHEME_V->alloc_seg[i]); 6126 free (SCHEME_V->cell_seg[i]);
5845 6127
5846#if SHOW_ERROR_LINE 6128#if SHOW_ERROR_LINE
5847 for (i = 0; i <= SCHEME_V->file_i; i++) 6129 for (i = 0; i <= SCHEME_V->file_i; i++)
5848 {
5849 if (SCHEME_V->load_stack[i].kind & port_file) 6130 if (SCHEME_V->load_stack[i].kind & port_file)
5850 { 6131 {
5851 fname = SCHEME_V->load_stack[i].rep.stdio.filename; 6132 fname = SCHEME_V->load_stack[i].rep.stdio.filename;
5852 6133
5853 if (fname) 6134 if (fname)
5854 free (fname); 6135 free (fname);
5855 } 6136 }
5856 }
5857#endif 6137#endif
5858} 6138}
5859 6139
5860void 6140ecb_cold void
5861scheme_load_file (SCHEME_P_ int fin) 6141scheme_load_file (SCHEME_P_ int fin)
5862{ 6142{
5863 scheme_load_named_file (SCHEME_A_ fin, 0); 6143 scheme_load_named_file (SCHEME_A_ fin, 0);
5864} 6144}
5865 6145
5866void 6146ecb_cold void
5867scheme_load_named_file (SCHEME_P_ int fin, const char *filename) 6147scheme_load_named_file (SCHEME_P_ int fin, const char *filename)
5868{ 6148{
5869 dump_stack_reset (SCHEME_A); 6149 dump_stack_reset (SCHEME_A);
5870 SCHEME_V->envir = SCHEME_V->global_env; 6150 SCHEME_V->envir = SCHEME_V->global_env;
5871 SCHEME_V->file_i = 0; 6151 SCHEME_V->file_i = 0;
5872 SCHEME_V->load_stack[0].unget = -1; 6152 SCHEME_V->load_stack[0].unget = -1;
5873 SCHEME_V->load_stack[0].kind = port_input | port_file; 6153 SCHEME_V->load_stack[0].kind = port_input | port_file;
5874 SCHEME_V->load_stack[0].rep.stdio.file = fin; 6154 SCHEME_V->load_stack[0].rep.stdio.file = fin;
5875#if USE_PORTS
5876 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6155 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5877#endif
5878 SCHEME_V->retcode = 0; 6156 SCHEME_V->retcode = 0;
5879 6157
5880#if USE_PORTS
5881 if (fin == STDIN_FILENO) 6158 if (fin == STDIN_FILENO)
5882 SCHEME_V->interactive_repl = 1; 6159 SCHEME_V->interactive_repl = 1;
5883#endif
5884 6160
5885#if USE_PORTS 6161#if USE_PORTS
5886#if SHOW_ERROR_LINE 6162#if SHOW_ERROR_LINE
5887 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; 6163 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0;
5888 6164
5892#endif 6168#endif
5893 6169
5894 SCHEME_V->inport = SCHEME_V->loadport; 6170 SCHEME_V->inport = SCHEME_V->loadport;
5895 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6171 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5896 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6172 Eval_Cycle (SCHEME_A_ OP_T0LVL);
6173
5897 set_typeflag (SCHEME_V->loadport, T_ATOM); 6174 set_typeflag (SCHEME_V->loadport, T_ATOM);
5898 6175
5899 if (SCHEME_V->retcode == 0) 6176 if (SCHEME_V->retcode == 0)
5900 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6177 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5901} 6178}
5902 6179
5903void 6180ecb_cold void
5904scheme_load_string (SCHEME_P_ const char *cmd) 6181scheme_load_string (SCHEME_P_ const char *cmd)
5905{ 6182{
6183#if USE_PORTs
5906 dump_stack_reset (SCHEME_A); 6184 dump_stack_reset (SCHEME_A);
5907 SCHEME_V->envir = SCHEME_V->global_env; 6185 SCHEME_V->envir = SCHEME_V->global_env;
5908 SCHEME_V->file_i = 0; 6186 SCHEME_V->file_i = 0;
5909 SCHEME_V->load_stack[0].kind = port_input | port_string; 6187 SCHEME_V->load_stack[0].kind = port_input | port_string;
5910 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ 6188 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */
5911 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); 6189 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd);
5912 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; 6190 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
5913#if USE_PORTS
5914 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6191 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5915#endif
5916 SCHEME_V->retcode = 0; 6192 SCHEME_V->retcode = 0;
5917 SCHEME_V->interactive_repl = 0; 6193 SCHEME_V->interactive_repl = 0;
5918 SCHEME_V->inport = SCHEME_V->loadport; 6194 SCHEME_V->inport = SCHEME_V->loadport;
5919 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6195 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5920 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6196 Eval_Cycle (SCHEME_A_ OP_T0LVL);
5921 set_typeflag (SCHEME_V->loadport, T_ATOM); 6197 set_typeflag (SCHEME_V->loadport, T_ATOM);
5922 6198
5923 if (SCHEME_V->retcode == 0) 6199 if (SCHEME_V->retcode == 0)
5924 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6200 SCHEME_V->retcode = SCHEME_V->nesting != 0;
6201#else
6202 abort ();
6203#endif
5925} 6204}
5926 6205
5927void 6206ecb_cold void
5928scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) 6207scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value)
5929{ 6208{
5930 pointer x; 6209 pointer x;
5931 6210
5932 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 6211 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
5937 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 6216 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
5938} 6217}
5939 6218
5940#if !STANDALONE 6219#if !STANDALONE
5941 6220
5942void 6221ecb_cold void
5943scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 6222scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
5944{ 6223{
5945 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); 6224 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f));
5946} 6225}
5947 6226
5948void 6227ecb_cold void
5949scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 6228scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
5950{ 6229{
5951 int i; 6230 int i;
5952 6231
5953 for (i = 0; i < count; i++) 6232 for (i = 0; i < count; i++)
5954 scheme_register_foreign_func (SCHEME_A_ list + i); 6233 scheme_register_foreign_func (SCHEME_A_ list + i);
5955} 6234}
5956 6235
5957pointer 6236ecb_cold pointer
5958scheme_apply0 (SCHEME_P_ const char *procname) 6237scheme_apply0 (SCHEME_P_ const char *procname)
5959{ 6238{
5960 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); 6239 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL));
5961} 6240}
5962 6241
5963void 6242ecb_cold void
5964save_from_C_call (SCHEME_P) 6243save_from_C_call (SCHEME_P)
5965{ 6244{
5966 pointer saved_data = cons (car (S_SINK), 6245 pointer saved_data = cons (car (S_SINK),
5967 cons (SCHEME_V->envir, 6246 cons (SCHEME_V->envir,
5968 SCHEME_V->dump)); 6247 SCHEME_V->dump));
5972 /* Truncate the dump stack so TS will return here when done, not 6251 /* Truncate the dump stack so TS will return here when done, not
5973 directly resume pre-C-call operations. */ 6252 directly resume pre-C-call operations. */
5974 dump_stack_reset (SCHEME_A); 6253 dump_stack_reset (SCHEME_A);
5975} 6254}
5976 6255
5977void 6256ecb_cold void
5978restore_from_C_call (SCHEME_P) 6257restore_from_C_call (SCHEME_P)
5979{ 6258{
5980 set_car (S_SINK, caar (SCHEME_V->c_nest)); 6259 set_car (S_SINK, caar (SCHEME_V->c_nest));
5981 SCHEME_V->envir = cadar (SCHEME_V->c_nest); 6260 SCHEME_V->envir = cadar (SCHEME_V->c_nest);
5982 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); 6261 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest));
5983 /* Pop */ 6262 /* Pop */
5984 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); 6263 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest);
5985} 6264}
5986 6265
5987/* "func" and "args" are assumed to be already eval'ed. */ 6266/* "func" and "args" are assumed to be already eval'ed. */
5988pointer 6267ecb_cold pointer
5989scheme_call (SCHEME_P_ pointer func, pointer args) 6268scheme_call (SCHEME_P_ pointer func, pointer args)
5990{ 6269{
5991 int old_repl = SCHEME_V->interactive_repl; 6270 int old_repl = SCHEME_V->interactive_repl;
5992 6271
5993 SCHEME_V->interactive_repl = 0; 6272 SCHEME_V->interactive_repl = 0;
6000 SCHEME_V->interactive_repl = old_repl; 6279 SCHEME_V->interactive_repl = old_repl;
6001 restore_from_C_call (SCHEME_A); 6280 restore_from_C_call (SCHEME_A);
6002 return SCHEME_V->value; 6281 return SCHEME_V->value;
6003} 6282}
6004 6283
6005pointer 6284ecb_cold pointer
6006scheme_eval (SCHEME_P_ pointer obj) 6285scheme_eval (SCHEME_P_ pointer obj)
6007{ 6286{
6008 int old_repl = SCHEME_V->interactive_repl; 6287 int old_repl = SCHEME_V->interactive_repl;
6009 6288
6010 SCHEME_V->interactive_repl = 0; 6289 SCHEME_V->interactive_repl = 0;
6022 6301
6023/* ========== Main ========== */ 6302/* ========== Main ========== */
6024 6303
6025#if STANDALONE 6304#if STANDALONE
6026 6305
6027int 6306ecb_cold int
6028main (int argc, char **argv) 6307main (int argc, char **argv)
6029{ 6308{
6030# if USE_MULTIPLICITY 6309# if USE_MULTIPLICITY
6031 scheme ssc; 6310 scheme ssc;
6032 scheme *const SCHEME_V = &ssc; 6311 scheme *const SCHEME_V = &ssc;
6034# endif 6313# endif
6035 int fin; 6314 int fin;
6036 char *file_name = InitFile; 6315 char *file_name = InitFile;
6037 int retcode; 6316 int retcode;
6038 int isfile = 1; 6317 int isfile = 1;
6318#if EXPERIMENT
6039 system ("ps v $PPID");//D 6319 system ("ps v $PPID");
6320#endif
6040 6321
6041 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6322 if (argc == 2 && strcmp (argv[1], "-?") == 0)
6042 { 6323 {
6043 xwrstr ("Usage: tinyscheme -?\n"); 6324 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
6044 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6325 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
6045 xwrstr ("followed by\n"); 6326 putstr (SCHEME_A_ "followed by\n");
6046 xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); 6327 putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n");
6047 xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); 6328 putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n");
6048 xwrstr ("assuming that the executable is named tinyscheme.\n"); 6329 putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n");
6049 xwrstr ("Use - as filename for stdin.\n"); 6330 putstr (SCHEME_A_ "Use - as filename for stdin.\n");
6050 return 1; 6331 return 1;
6051 } 6332 }
6052 6333
6053 if (!scheme_init (SCHEME_A)) 6334 if (!scheme_init (SCHEME_A))
6054 { 6335 {
6055 xwrstr ("Could not initialize!\n"); 6336 putstr (SCHEME_A_ "Could not initialize!\n");
6056 return 2; 6337 return 2;
6057 } 6338 }
6058 6339
6059# if USE_PORTS 6340# if USE_PORTS
6060 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); 6341 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO);
6073 } 6354 }
6074#endif 6355#endif
6075 6356
6076 do 6357 do
6077 { 6358 {
6078#if USE_PORTS
6079 if (strcmp (file_name, "-") == 0) 6359 if (strcmp (file_name, "-") == 0)
6080 fin = STDIN_FILENO; 6360 fin = STDIN_FILENO;
6081 else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) 6361 else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0)
6082 { 6362 {
6083 pointer args = NIL; 6363 pointer args = NIL;
6101 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); 6381 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args);
6102 6382
6103 } 6383 }
6104 else 6384 else
6105 fin = open (file_name, O_RDONLY); 6385 fin = open (file_name, O_RDONLY);
6106#endif
6107 6386
6108 if (isfile && fin < 0) 6387 if (isfile && fin < 0)
6109 { 6388 {
6110 xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); 6389 putstr (SCHEME_A_ "Could not open file ");
6390 putstr (SCHEME_A_ file_name);
6391 putcharacter (SCHEME_A_ '\n');
6111 } 6392 }
6112 else 6393 else
6113 { 6394 {
6114 if (isfile) 6395 if (isfile)
6115 scheme_load_named_file (SCHEME_A_ fin, file_name); 6396 scheme_load_named_file (SCHEME_A_ fin, file_name);
6116 else 6397 else
6117 scheme_load_string (SCHEME_A_ file_name); 6398 scheme_load_string (SCHEME_A_ file_name);
6118 6399
6119#if USE_PORTS
6120 if (!isfile || fin != STDIN_FILENO) 6400 if (!isfile || fin != STDIN_FILENO)
6121 { 6401 {
6122 if (SCHEME_V->retcode != 0) 6402 if (SCHEME_V->retcode != 0)
6123 { 6403 {
6124 xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); 6404 putstr (SCHEME_A_ "Errors encountered reading ");
6405 putstr (SCHEME_A_ file_name);
6406 putcharacter (SCHEME_A_ '\n');
6125 } 6407 }
6126 6408
6127 if (isfile) 6409 if (isfile)
6128 close (fin); 6410 close (fin);
6129 } 6411 }
6130#endif
6131 } 6412 }
6132 6413
6133 file_name = *argv++; 6414 file_name = *argv++;
6134 } 6415 }
6135 while (file_name != 0); 6416 while (file_name != 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines