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.55 by root, Tue Dec 1 03:03:11 2015 UTC vs.
Revision 1.64 by root, Wed Dec 2 17:01:18 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 _GNU_SOURCE 1
22#define _POSIX_C_SOURCE 200201
23#define _XOPEN_SOURCE 600
22 24
23#if 1
24#define PAGE_SIZE 4096 /* does not work on sparc/alpha */
25#include "malloc.c"
26#endif
27 25
28#define SCHEME_SOURCE 26#define SCHEME_SOURCE
29#include "scheme-private.h" 27#include "scheme-private.h"
30#ifndef WIN32 28#ifndef WIN32
31# include <unistd.h> 29# include <unistd.h>
32#endif 30#endif
33#if USE_MATH 31#if USE_MATH
34# include <math.h> 32# include <math.h>
35#endif 33#endif
36 34
35#define ECB_NO_THREADS 1
37#include "ecb.h" 36#include "ecb.h"
38 37
39#include <sys/types.h> 38#include <sys/types.h>
40#include <sys/stat.h> 39#include <sys/stat.h>
41#include <fcntl.h> 40#include <fcntl.h>
49#include <string.h> 48#include <string.h>
50 49
51#include <limits.h> 50#include <limits.h>
52#include <inttypes.h> 51#include <inttypes.h>
53#include <float.h> 52#include <float.h>
54//#include <ctype.h> 53
54#if !USE_SYSTEM_MALLOC
55# define PAGE_SIZE 4096 /* does not work on sparc/alpha */
56# include "malloc.c"
57# define malloc(n) tiny_malloc (n)
58# define realloc(p,n) tiny_realloc (p, n)
59# define free(p) tiny_free (p)
60#endif
55 61
56#if '1' != '0' + 1 \ 62#if '1' != '0' + 1 \
57 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ 63 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \
58 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ 64 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \
59 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ 65 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \
91 97
92#if !USE_MULTIPLICITY 98#if !USE_MULTIPLICITY
93static scheme sc; 99static scheme sc;
94#endif 100#endif
95 101
96static void 102ecb_cold static void
97xbase (char *s, long n, int base) 103xbase (char *s, long n, int base)
98{ 104{
99 if (n < 0) 105 if (n < 0)
100 { 106 {
101 *s++ = '-'; 107 *s++ = '-';
116 char x = *s; *s = *p; *p = x; 122 char x = *s; *s = *p; *p = x;
117 --p; ++s; 123 --p; ++s;
118 } 124 }
119} 125}
120 126
121static void 127ecb_cold static void
122xnum (char *s, long n) 128xnum (char *s, long n)
123{ 129{
124 xbase (s, n, 10); 130 xbase (s, n, 10);
125} 131}
126 132
127static void 133ecb_cold static void
128putnum (SCHEME_P_ long n) 134putnum (SCHEME_P_ long n)
129{ 135{
130 char buf[64]; 136 char buf[64];
131 137
132 xnum (buf, n); 138 xnum (buf, n);
133 putstr (SCHEME_A_ buf); 139 putstr (SCHEME_A_ buf);
134} 140}
141
142#if USE_CHAR_CLASSIFIERS
143#include <ctype.h>
144#else
135 145
136static char 146static char
137xtoupper (char c) 147xtoupper (char c)
138{ 148{
139 if (c >= 'a' && c <= 'z') 149 if (c >= 'a' && c <= 'z')
159 169
160#define toupper(c) xtoupper (c) 170#define toupper(c) xtoupper (c)
161#define tolower(c) xtolower (c) 171#define tolower(c) xtolower (c)
162#define isdigit(c) xisdigit (c) 172#define isdigit(c) xisdigit (c)
163 173
174#endif
175
164#if USE_IGNORECASE 176#if USE_IGNORECASE
165static const char * 177ecb_cold static const char *
166xstrlwr (char *s) 178xstrlwr (char *s)
167{ 179{
168 const char *p = s; 180 const char *p = s;
169 181
170 while (*s) 182 while (*s)
183# define stricmp(a,b) strcmp (a, b) 195# define stricmp(a,b) strcmp (a, b)
184# define strlwr(s) (s) 196# define strlwr(s) (s)
185#endif 197#endif
186 198
187#ifndef prompt 199#ifndef prompt
188# define prompt "ts> " 200# define prompt "ms> "
189#endif 201#endif
190 202
191#ifndef InitFile 203#ifndef InitFile
192# define InitFile "init.scm" 204# define InitFile "init.scm"
193#endif 205#endif
194 206
195enum scheme_types 207enum scheme_types
196{ 208{
197 T_INTEGER, 209 T_INTEGER,
210 T_CHARACTER,
198 T_REAL, 211 T_REAL,
199 T_STRING, 212 T_STRING,
200 T_SYMBOL, 213 T_SYMBOL,
201 T_PROC, 214 T_PROC,
202 T_PAIR, /* also used for free cells */ 215 T_PAIR, /* also used for free cells */
203 T_CLOSURE, 216 T_CLOSURE,
217 T_BYTECODE, // temp
218 T_MACRO,
204 T_CONTINUATION, 219 T_CONTINUATION,
205 T_FOREIGN, 220 T_FOREIGN,
206 T_CHARACTER,
207 T_PORT, 221 T_PORT,
208 T_VECTOR, 222 T_VECTOR,
209 T_MACRO,
210 T_PROMISE, 223 T_PROMISE,
211 T_ENVIRONMENT, 224 T_ENVIRONMENT,
212 /* one more... */ 225
213 T_NUM_SYSTEM_TYPES 226 T_NUM_SYSTEM_TYPES
214}; 227};
215 228
216#define T_MASKTYPE 0x000f 229#define T_MASKTYPE 0x000f
217#define T_SYNTAX 0x0010 230#define T_SYNTAX 0x0010
371 384
372static pointer cadar (pointer p) { return car (cdr (car (p))); } 385static pointer cadar (pointer p) { return car (cdr (car (p))); }
373static pointer caddr (pointer p) { return car (cdr (cdr (p))); } 386static pointer caddr (pointer p) { return car (cdr (cdr (p))); }
374static pointer cdaar (pointer p) { return cdr (car (car (p))); } 387static pointer cdaar (pointer p) { return cdr (car (car (p))); }
375 388
389static pointer cadddr (pointer p) { return car (car (car (cdr (p)))); }
390
376INTERFACE void 391INTERFACE void
377set_car (pointer p, pointer q) 392set_car (pointer p, pointer q)
378{ 393{
379 CELL(p)->object.cons.car = CELL (q); 394 CELL(p)->object.cons.car = CELL (q);
380} 395}
520 proper list: length 535 proper list: length
521 circular list: -1 536 circular list: -1
522 not even a pair: -2 537 not even a pair: -2
523 dotted list: -2 minus length before dot 538 dotted list: -2 minus length before dot
524*/ 539*/
525INTERFACE int 540ecb_hot INTERFACE int
526list_length (SCHEME_P_ pointer a) 541list_length (SCHEME_P_ pointer a)
527{ 542{
528 int i = 0; 543 int i = 0;
529 pointer slow, fast; 544 pointer slow, fast;
530 545
569{ 584{
570 return list_length (SCHEME_A_ a) >= 0; 585 return list_length (SCHEME_A_ a) >= 0;
571} 586}
572 587
573#if USE_CHAR_CLASSIFIERS 588#if USE_CHAR_CLASSIFIERS
589
574ecb_inline int 590ecb_inline int
575Cisalpha (int c) 591Cisalpha (int c)
576{ 592{
577 return isascii (c) && isalpha (c); 593 return isascii (c) && isalpha (c);
578} 594}
636 "gs", 652 "gs",
637 "rs", 653 "rs",
638 "us" 654 "us"
639}; 655};
640 656
641static int 657ecb_cold static int
642is_ascii_name (const char *name, int *pc) 658is_ascii_name (const char *name, int *pc)
643{ 659{
644 int i; 660 int i;
645 661
646 for (i = 0; i < 32; i++) 662 for (i = 0; i < 32; i++)
668static int file_interactive (SCHEME_P); 684static int file_interactive (SCHEME_P);
669ecb_inline int is_one_of (const char *s, int c); 685ecb_inline int is_one_of (const char *s, int c);
670static int alloc_cellseg (SCHEME_P); 686static int alloc_cellseg (SCHEME_P);
671ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); 687ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
672static void finalize_cell (SCHEME_P_ pointer a); 688static void finalize_cell (SCHEME_P_ pointer a);
673static int count_consecutive_cells (pointer x, int needed);
674static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 689static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
675static pointer mk_number (SCHEME_P_ const num n); 690static pointer mk_number (SCHEME_P_ const num n);
676static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); 691static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
677static pointer mk_vector (SCHEME_P_ uint32_t len); 692static pointer mk_vector (SCHEME_P_ uint32_t len);
678static pointer mk_atom (SCHEME_P_ char *q); 693static pointer mk_atom (SCHEME_P_ char *q);
679static pointer mk_sharp_const (SCHEME_P_ char *name); 694static pointer mk_sharp_const (SCHEME_P_ char *name);
680 695
696static pointer mk_port (SCHEME_P_ port *p);
697
681#if USE_PORTS 698#if USE_PORTS
682static pointer mk_port (SCHEME_P_ port *p);
683static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); 699static pointer port_from_filename (SCHEME_P_ const char *fn, int prop);
684static pointer port_from_file (SCHEME_P_ int, int prop); 700static pointer port_from_file (SCHEME_P_ int, int prop);
685static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); 701static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop);
686static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); 702static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop);
687static port *port_rep_from_file (SCHEME_P_ int, int prop); 703static port *port_rep_from_file (SCHEME_P_ int, int prop);
688static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); 704static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop);
689static void port_close (SCHEME_P_ pointer p, int flag); 705static void port_close (SCHEME_P_ pointer p, int flag);
690#endif 706#endif
707
691static void mark (pointer a); 708static void mark (pointer a);
692static void gc (SCHEME_P_ pointer a, pointer b); 709static void gc (SCHEME_P_ pointer a, pointer b);
693static int basic_inchar (port *pt); 710static int basic_inchar (port *pt);
694static int inchar (SCHEME_P); 711static int inchar (SCHEME_P);
695static void backchar (SCHEME_P_ int c); 712static void backchar (SCHEME_P_ int c);
696static char *readstr_upto (SCHEME_P_ int skip, const char *delim); 713static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
697static pointer readstrexp (SCHEME_P_ char delim); 714static pointer readstrexp (SCHEME_P_ char delim);
698ecb_inline int skipspace (SCHEME_P); 715static int skipspace (SCHEME_P);
699static int token (SCHEME_P); 716static int token (SCHEME_P);
700static void printslashstring (SCHEME_P_ char *s, int len); 717static void printslashstring (SCHEME_P_ char *s, int len);
701static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 718static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
702static void printatom (SCHEME_P_ pointer l, int f); 719static void printatom (SCHEME_P_ pointer l, int f);
703static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 720static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
883#endif 900#endif
884#endif 901#endif
885} 902}
886 903
887/* allocate new cell segment */ 904/* allocate new cell segment */
888static int 905ecb_cold static int
889alloc_cellseg (SCHEME_P) 906alloc_cellseg (SCHEME_P)
890{ 907{
891 struct cell *newp; 908 struct cell *newp;
892 struct cell *last; 909 struct cell *last;
893 struct cell *p; 910 struct cell *p;
902 919
903 if (!cp && USE_ERROR_CHECKING) 920 if (!cp && USE_ERROR_CHECKING)
904 return k; 921 return k;
905 922
906 i = ++SCHEME_V->last_cell_seg; 923 i = ++SCHEME_V->last_cell_seg;
907 SCHEME_V->alloc_seg[i] = cp;
908 924
909 newp = (struct cell *)cp; 925 newp = (struct cell *)cp;
910 SCHEME_V->cell_seg[i] = newp; 926 SCHEME_V->cell_seg[i] = newp;
911 SCHEME_V->cell_segsize[i] = segsize; 927 SCHEME_V->cell_segsize[i] = segsize;
912 SCHEME_V->fcells += segsize; 928 SCHEME_V->fcells += segsize;
935 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 951 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
936 return S_SINK; 952 return S_SINK;
937 953
938 if (SCHEME_V->free_cell == NIL) 954 if (SCHEME_V->free_cell == NIL)
939 { 955 {
940 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1; 956 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 2;
941 957
942 gc (SCHEME_A_ a, b); 958 gc (SCHEME_A_ a, b);
943 959
944 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) 960 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL)
945 { 961 {
964 } 980 }
965} 981}
966 982
967/* To retain recent allocs before interpreter knows about them - 983/* To retain recent allocs before interpreter knows about them -
968 Tehom */ 984 Tehom */
969static void 985ecb_hot static void
970push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 986push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
971{ 987{
972 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 988 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
973 989
974 set_typeflag (holder, T_PAIR); 990 set_typeflag (holder, T_PAIR);
976 set_car (holder, recent); 992 set_car (holder, recent);
977 set_cdr (holder, car (S_SINK)); 993 set_cdr (holder, car (S_SINK));
978 set_car (S_SINK, holder); 994 set_car (S_SINK, holder);
979} 995}
980 996
981static pointer 997ecb_hot static pointer
982get_cell (SCHEME_P_ pointer a, pointer b) 998get_cell (SCHEME_P_ pointer a, pointer b)
983{ 999{
984 pointer cell = get_cell_x (SCHEME_A_ a, b); 1000 pointer cell = get_cell_x (SCHEME_A_ a, b);
985 1001
986 /* For right now, include "a" and "b" in "cell" so that gc doesn't 1002 /* For right now, include "a" and "b" in "cell" so that gc doesn't
1043#endif 1059#endif
1044 1060
1045/* Medium level cell allocation */ 1061/* Medium level cell allocation */
1046 1062
1047/* get new cons cell */ 1063/* get new cons cell */
1048pointer 1064ecb_hot static pointer
1049xcons (SCHEME_P_ pointer a, pointer b, int immutable) 1065xcons (SCHEME_P_ pointer a, pointer b)
1050{ 1066{
1051 pointer x = get_cell (SCHEME_A_ a, b); 1067 pointer x = get_cell (SCHEME_A_ a, b);
1052 1068
1053 set_typeflag (x, T_PAIR); 1069 set_typeflag (x, T_PAIR);
1054
1055 if (immutable)
1056 setimmutable (x);
1057 1070
1058 set_car (x, a); 1071 set_car (x, a);
1059 set_cdr (x, b); 1072 set_cdr (x, b);
1060 1073
1061 return x; 1074 return x;
1062} 1075}
1063 1076
1064static pointer 1077ecb_hot static pointer
1078ximmutable_cons (SCHEME_P_ pointer a, pointer b)
1079{
1080 pointer x = xcons (SCHEME_A_ a, b);
1081 setimmutable (x);
1082 return x;
1083}
1084
1085#define cons(a,b) xcons (SCHEME_A_ a, b)
1086#define immutable_cons(a,b) ximmutable_cons (SCHEME_A_ a, b)
1087
1088ecb_cold static pointer
1065generate_symbol (SCHEME_P_ const char *name) 1089generate_symbol (SCHEME_P_ const char *name)
1066{ 1090{
1067 pointer x = mk_string (SCHEME_A_ name); 1091 pointer x = mk_string (SCHEME_A_ name);
1068 setimmutable (x); 1092 setimmutable (x);
1069 set_typeflag (x, T_SYMBOL | T_ATOM); 1093 set_typeflag (x, T_SYMBOL | T_ATOM);
1075#ifndef USE_OBJECT_LIST 1099#ifndef USE_OBJECT_LIST
1076 1100
1077static int 1101static int
1078hash_fn (const char *key, int table_size) 1102hash_fn (const char *key, int table_size)
1079{ 1103{
1080 const unsigned char *p = key; 1104 const unsigned char *p = (unsigned char *)key;
1081 uint32_t hash = 2166136261; 1105 uint32_t hash = 2166136261U;
1082 1106
1083 while (*p) 1107 while (*p)
1084 hash = (hash ^ *p++) * 16777619; 1108 hash = (hash ^ *p++) * 16777619;
1085 1109
1086 return hash % table_size; 1110 return hash % table_size;
1087} 1111}
1088 1112
1089static pointer 1113ecb_cold static pointer
1090oblist_initial_value (SCHEME_P) 1114oblist_initial_value (SCHEME_P)
1091{ 1115{
1092 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1116 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1093} 1117}
1094 1118
1095/* returns the new symbol */ 1119/* returns the new symbol */
1096static pointer 1120ecb_cold static pointer
1097oblist_add_by_name (SCHEME_P_ const char *name) 1121oblist_add_by_name (SCHEME_P_ const char *name)
1098{ 1122{
1099 pointer x = generate_symbol (SCHEME_A_ name); 1123 pointer x = generate_symbol (SCHEME_A_ name);
1100 int location = hash_fn (name, veclength (SCHEME_V->oblist)); 1124 int location = hash_fn (name, veclength (SCHEME_V->oblist));
1101 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); 1125 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1102 return x; 1126 return x;
1103} 1127}
1104 1128
1105ecb_inline pointer 1129ecb_cold static pointer
1106oblist_find_by_name (SCHEME_P_ const char *name) 1130oblist_find_by_name (SCHEME_P_ const char *name)
1107{ 1131{
1108 int location; 1132 int location;
1109 pointer x; 1133 pointer x;
1110 char *s; 1134 char *s;
1121 } 1145 }
1122 1146
1123 return NIL; 1147 return NIL;
1124} 1148}
1125 1149
1126static pointer 1150ecb_cold static pointer
1127oblist_all_symbols (SCHEME_P) 1151oblist_all_symbols (SCHEME_P)
1128{ 1152{
1129 int i; 1153 int i;
1130 pointer x; 1154 pointer x;
1131 pointer ob_list = NIL; 1155 pointer ob_list = NIL;
1137 return ob_list; 1161 return ob_list;
1138} 1162}
1139 1163
1140#else 1164#else
1141 1165
1142static pointer 1166ecb_cold static pointer
1143oblist_initial_value (SCHEME_P) 1167oblist_initial_value (SCHEME_P)
1144{ 1168{
1145 return NIL; 1169 return NIL;
1146} 1170}
1147 1171
1148ecb_inline pointer 1172ecb_cold static pointer
1149oblist_find_by_name (SCHEME_P_ const char *name) 1173oblist_find_by_name (SCHEME_P_ const char *name)
1150{ 1174{
1151 pointer x; 1175 pointer x;
1152 char *s; 1176 char *s;
1153 1177
1162 1186
1163 return NIL; 1187 return NIL;
1164} 1188}
1165 1189
1166/* returns the new symbol */ 1190/* returns the new symbol */
1167static pointer 1191ecb_cold static pointer
1168oblist_add_by_name (SCHEME_P_ const char *name) 1192oblist_add_by_name (SCHEME_P_ const char *name)
1169{ 1193{
1170 pointer x = generate_symbol (SCHEME_A_ name); 1194 pointer x = generate_symbol (SCHEME_A_ name);
1171 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1195 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1172 return x; 1196 return x;
1173} 1197}
1174 1198
1175static pointer 1199ecb_cold static pointer
1176oblist_all_symbols (SCHEME_P) 1200oblist_all_symbols (SCHEME_P)
1177{ 1201{
1178 return SCHEME_V->oblist; 1202 return SCHEME_V->oblist;
1179} 1203}
1180 1204
1181#endif 1205#endif
1182 1206
1183#if USE_PORTS
1184static pointer 1207ecb_cold static pointer
1185mk_port (SCHEME_P_ port *p) 1208mk_port (SCHEME_P_ port *p)
1186{ 1209{
1187 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1210 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1188 1211
1189 set_typeflag (x, T_PORT | T_ATOM); 1212 set_typeflag (x, T_PORT | T_ATOM);
1190 set_port (x, p); 1213 set_port (x, p);
1191 1214
1192 return x; 1215 return x;
1193} 1216}
1194#endif
1195 1217
1196pointer 1218ecb_cold pointer
1197mk_foreign_func (SCHEME_P_ foreign_func f) 1219mk_foreign_func (SCHEME_P_ foreign_func f)
1198{ 1220{
1199 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1221 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1200 1222
1201 set_typeflag (x, T_FOREIGN | T_ATOM); 1223 set_typeflag (x, T_FOREIGN | T_ATOM);
1366 x = oblist_add_by_name (SCHEME_A_ name); 1388 x = oblist_add_by_name (SCHEME_A_ name);
1367 1389
1368 return x; 1390 return x;
1369} 1391}
1370 1392
1371INTERFACE pointer 1393ecb_cold INTERFACE pointer
1372gensym (SCHEME_P) 1394gensym (SCHEME_P)
1373{ 1395{
1374 pointer x; 1396 pointer x;
1375 char name[40] = "gensym-"; 1397 char name[40] = "gensym-";
1376 xnum (name + 7, ++SCHEME_V->gensym_cnt); 1398 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1383{ 1405{
1384 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; 1406 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1385} 1407}
1386 1408
1387/* make symbol or number atom from string */ 1409/* make symbol or number atom from string */
1388static pointer 1410ecb_cold static pointer
1389mk_atom (SCHEME_P_ char *q) 1411mk_atom (SCHEME_P_ char *q)
1390{ 1412{
1391 char c, *p; 1413 char c, *p;
1392 int has_dec_point = 0; 1414 int has_dec_point = 0;
1393 int has_fp_exp = 0; 1415 int has_fp_exp = 0;
1464 1486
1465 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1487 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1466} 1488}
1467 1489
1468/* make constant */ 1490/* make constant */
1469static pointer 1491ecb_cold static pointer
1470mk_sharp_const (SCHEME_P_ char *name) 1492mk_sharp_const (SCHEME_P_ char *name)
1471{ 1493{
1472 if (!strcmp (name, "t")) 1494 if (!strcmp (name, "t"))
1473 return S_T; 1495 return S_T;
1474 else if (!strcmp (name, "f")) 1496 else if (!strcmp (name, "f"))
1475 return S_F; 1497 return S_F;
1476 else if (*name == '\\') /* #\w (character) */ 1498 else if (*name == '\\') /* #\w (character) */
1477 { 1499 {
1478 int c; 1500 int c;
1479 1501
1502 // TODO: optimise
1480 if (stricmp (name + 1, "space") == 0) 1503 if (stricmp (name + 1, "space") == 0)
1481 c = ' '; 1504 c = ' ';
1482 else if (stricmp (name + 1, "newline") == 0) 1505 else if (stricmp (name + 1, "newline") == 0)
1483 c = '\n'; 1506 c = '\n';
1484 else if (stricmp (name + 1, "return") == 0) 1507 else if (stricmp (name + 1, "return") == 0)
1485 c = '\r'; 1508 c = '\r';
1486 else if (stricmp (name + 1, "tab") == 0) 1509 else if (stricmp (name + 1, "tab") == 0)
1487 c = '\t'; 1510 c = '\t';
1511 else if (stricmp (name + 1, "alarm") == 0)
1512 c = 0x07;
1513 else if (stricmp (name + 1, "backspace") == 0)
1514 c = 0x08;
1515 else if (stricmp (name + 1, "escape") == 0)
1516 c = 0x1b;
1517 else if (stricmp (name + 1, "delete") == 0)
1518 c = 0x7f;
1519 else if (stricmp (name + 1, "null") == 0)
1520 c = 0;
1488 else if (name[1] == 'x' && name[2] != 0) 1521 else if (name[1] == 'x' && name[2] != 0)
1489 { 1522 {
1490 long c1 = strtol (name + 2, 0, 16); 1523 long c1 = strtol (name + 2, 0, 16);
1491 1524
1492 if (0 <= c1 && c1 <= UCHAR_MAX) 1525 if (0 <= c1 && c1 <= UCHAR_MAX)
1517 return NIL; 1550 return NIL;
1518 } 1551 }
1519} 1552}
1520 1553
1521/* ========== garbage collector ========== */ 1554/* ========== garbage collector ========== */
1555
1556static void
1557finalize_cell (SCHEME_P_ pointer a)
1558{
1559 /* TODO, fast bitmap check? */
1560 if (is_string (a) || is_symbol (a))
1561 free (strvalue (a));
1562 else if (is_vector (a))
1563 free (vecvalue (a));
1564#if USE_PORTS
1565 else if (is_port (a))
1566 {
1567 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit)
1568 port_close (SCHEME_A_ a, port_input | port_output);
1569
1570 free (port (a));
1571 }
1572#endif
1573}
1522 1574
1523/*-- 1575/*--
1524 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, 1576 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1,
1525 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1577 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1526 * for marking. 1578 * for marking.
1527 * 1579 *
1528 * The exception is vectors - vectors are currently marked recursively, 1580 * The exception is vectors - vectors are currently marked recursively,
1529 * which is inherited form tinyscheme and could be fixed by having another 1581 * which is inherited form tinyscheme and could be fixed by having another
1530 * word of context in the vector 1582 * word of context in the vector
1531 */ 1583 */
1532static void 1584ecb_hot static void
1533mark (pointer a) 1585mark (pointer a)
1534{ 1586{
1535 pointer t, q, p; 1587 pointer t, q, p;
1536 1588
1537 t = 0; 1589 t = 0;
1594 p = q; 1646 p = q;
1595 goto E6; 1647 goto E6;
1596 } 1648 }
1597} 1649}
1598 1650
1599/* garbage collection. parameter a, b is marked. */ 1651ecb_hot static void
1600static void 1652gc_free (SCHEME_P)
1601gc (SCHEME_P_ pointer a, pointer b)
1602{ 1653{
1603 int i; 1654 int i;
1604
1605 if (SCHEME_V->gc_verbose)
1606 putstr (SCHEME_A_ "gc...");
1607
1608 /* mark system globals */
1609 mark (SCHEME_V->oblist);
1610 mark (SCHEME_V->global_env);
1611
1612 /* mark current registers */
1613 mark (SCHEME_V->args);
1614 mark (SCHEME_V->envir);
1615 mark (SCHEME_V->code);
1616 dump_stack_mark (SCHEME_A);
1617 mark (SCHEME_V->value);
1618 mark (SCHEME_V->inport);
1619 mark (SCHEME_V->save_inport);
1620 mark (SCHEME_V->outport);
1621 mark (SCHEME_V->loadport);
1622
1623 /* Mark recent objects the interpreter doesn't know about yet. */
1624 mark (car (S_SINK));
1625 /* Mark any older stuff above nested C calls */
1626 mark (SCHEME_V->c_nest);
1627
1628#if USE_INTCACHE
1629 /* mark intcache */
1630 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1631 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1632 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1633#endif
1634
1635 /* mark variables a, b */
1636 mark (a);
1637 mark (b);
1638
1639 /* garbage collect */
1640 clrmark (NIL);
1641 SCHEME_V->fcells = 0;
1642 SCHEME_V->free_cell = NIL;
1643
1644 if (SCHEME_V->gc_verbose)
1645 putstr (SCHEME_A_ "freeing...");
1646
1647 uint32_t total = 0; 1655 uint32_t total = 0;
1648 1656
1649 /* Here we scan the cells to build the free-list. */ 1657 /* Here we scan the cells to build the free-list. */
1650 for (i = SCHEME_V->last_cell_seg; i >= 0; i--) 1658 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1651 { 1659 {
1680 { 1688 {
1681 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"); 1689 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");
1682 } 1690 }
1683} 1691}
1684 1692
1685static void 1693/* garbage collection. parameter a, b is marked. */
1686finalize_cell (SCHEME_P_ pointer a) 1694ecb_cold static void
1695gc (SCHEME_P_ pointer a, pointer b)
1687{ 1696{
1688 /* TODO, fast bitmap check? */ 1697 int i;
1689 if (is_string (a) || is_symbol (a))
1690 free (strvalue (a));
1691 else if (is_vector (a))
1692 free (vecvalue (a));
1693#if USE_PORTS
1694 else if (is_port (a))
1695 {
1696 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit)
1697 port_close (SCHEME_A_ a, port_input | port_output);
1698 1698
1699 free (port (a)); 1699 if (SCHEME_V->gc_verbose)
1700 } 1700 putstr (SCHEME_A_ "gc...");
1701
1702 /* mark system globals */
1703 mark (SCHEME_V->oblist);
1704 mark (SCHEME_V->global_env);
1705
1706 /* mark current registers */
1707 mark (SCHEME_V->args);
1708 mark (SCHEME_V->envir);
1709 mark (SCHEME_V->code);
1710 dump_stack_mark (SCHEME_A);
1711 mark (SCHEME_V->value);
1712 mark (SCHEME_V->inport);
1713 mark (SCHEME_V->save_inport);
1714 mark (SCHEME_V->outport);
1715 mark (SCHEME_V->loadport);
1716
1717 /* Mark recent objects the interpreter doesn't know about yet. */
1718 mark (car (S_SINK));
1719 /* Mark any older stuff above nested C calls */
1720 mark (SCHEME_V->c_nest);
1721
1722#if USE_INTCACHE
1723 /* mark intcache */
1724 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1725 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1726 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1701#endif 1727#endif
1728
1729 /* mark variables a, b */
1730 mark (a);
1731 mark (b);
1732
1733 /* garbage collect */
1734 clrmark (NIL);
1735 SCHEME_V->fcells = 0;
1736 SCHEME_V->free_cell = NIL;
1737
1738 if (SCHEME_V->gc_verbose)
1739 putstr (SCHEME_A_ "freeing...");
1740
1741 gc_free (SCHEME_A);
1702} 1742}
1703 1743
1704/* ========== Routines for Reading ========== */ 1744/* ========== Routines for Reading ========== */
1705 1745
1706static int 1746ecb_cold static int
1707file_push (SCHEME_P_ const char *fname) 1747file_push (SCHEME_P_ const char *fname)
1708{ 1748{
1709#if USE_PORTS
1710 int fin; 1749 int fin;
1711 1750
1712 if (SCHEME_V->file_i == MAXFIL - 1) 1751 if (SCHEME_V->file_i == MAXFIL - 1)
1713 return 0; 1752 return 0;
1714 1753
1731 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0); 1770 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0);
1732#endif 1771#endif
1733 } 1772 }
1734 1773
1735 return fin >= 0; 1774 return fin >= 0;
1736
1737#else
1738 return 1;
1739#endif
1740} 1775}
1741 1776
1742static void 1777ecb_cold static void
1743file_pop (SCHEME_P) 1778file_pop (SCHEME_P)
1744{ 1779{
1745 if (SCHEME_V->file_i != 0) 1780 if (SCHEME_V->file_i != 0)
1746 { 1781 {
1747 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1782 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1751 SCHEME_V->file_i--; 1786 SCHEME_V->file_i--;
1752 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); 1787 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1753 } 1788 }
1754} 1789}
1755 1790
1756static int 1791ecb_cold static int
1757file_interactive (SCHEME_P) 1792file_interactive (SCHEME_P)
1758{ 1793{
1759#if USE_PORTS 1794#if USE_PORTS
1760 return SCHEME_V->file_i == 0 1795 return SCHEME_V->file_i == 0
1761 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1796 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1764 return 0; 1799 return 0;
1765#endif 1800#endif
1766} 1801}
1767 1802
1768#if USE_PORTS 1803#if USE_PORTS
1769static port * 1804ecb_cold static port *
1770port_rep_from_filename (SCHEME_P_ const char *fn, int prop) 1805port_rep_from_filename (SCHEME_P_ const char *fn, int prop)
1771{ 1806{
1772 int fd; 1807 int fd;
1773 int flags; 1808 int flags;
1774 char *rw; 1809 char *rw;
1797# endif 1832# endif
1798 1833
1799 return pt; 1834 return pt;
1800} 1835}
1801 1836
1802static pointer 1837ecb_cold static pointer
1803port_from_filename (SCHEME_P_ const char *fn, int prop) 1838port_from_filename (SCHEME_P_ const char *fn, int prop)
1804{ 1839{
1805 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); 1840 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop);
1806 1841
1807 if (!pt && USE_ERROR_CHECKING) 1842 if (!pt && USE_ERROR_CHECKING)
1808 return NIL; 1843 return NIL;
1809 1844
1810 return mk_port (SCHEME_A_ pt); 1845 return mk_port (SCHEME_A_ pt);
1811} 1846}
1812 1847
1813static port * 1848ecb_cold static port *
1814port_rep_from_file (SCHEME_P_ int f, int prop) 1849port_rep_from_file (SCHEME_P_ int f, int prop)
1815{ 1850{
1816 port *pt = malloc (sizeof *pt); 1851 port *pt = malloc (sizeof *pt);
1817 1852
1818 if (!pt && USE_ERROR_CHECKING) 1853 if (!pt && USE_ERROR_CHECKING)
1823 pt->rep.stdio.file = f; 1858 pt->rep.stdio.file = f;
1824 pt->rep.stdio.closeit = 0; 1859 pt->rep.stdio.closeit = 0;
1825 return pt; 1860 return pt;
1826} 1861}
1827 1862
1828static pointer 1863ecb_cold static pointer
1829port_from_file (SCHEME_P_ int f, int prop) 1864port_from_file (SCHEME_P_ int f, int prop)
1830{ 1865{
1831 port *pt = port_rep_from_file (SCHEME_A_ f, prop); 1866 port *pt = port_rep_from_file (SCHEME_A_ f, prop);
1832 1867
1833 if (!pt && USE_ERROR_CHECKING) 1868 if (!pt && USE_ERROR_CHECKING)
1834 return NIL; 1869 return NIL;
1835 1870
1836 return mk_port (SCHEME_A_ pt); 1871 return mk_port (SCHEME_A_ pt);
1837} 1872}
1838 1873
1839static port * 1874ecb_cold static port *
1840port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1875port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
1841{ 1876{
1842 port *pt = malloc (sizeof (port)); 1877 port *pt = malloc (sizeof (port));
1843 1878
1844 if (!pt && USE_ERROR_CHECKING) 1879 if (!pt && USE_ERROR_CHECKING)
1850 pt->rep.string.curr = start; 1885 pt->rep.string.curr = start;
1851 pt->rep.string.past_the_end = past_the_end; 1886 pt->rep.string.past_the_end = past_the_end;
1852 return pt; 1887 return pt;
1853} 1888}
1854 1889
1855static pointer 1890ecb_cold static pointer
1856port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1891port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
1857{ 1892{
1858 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); 1893 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop);
1859 1894
1860 if (!pt && USE_ERROR_CHECKING) 1895 if (!pt && USE_ERROR_CHECKING)
1863 return mk_port (SCHEME_A_ pt); 1898 return mk_port (SCHEME_A_ pt);
1864} 1899}
1865 1900
1866# define BLOCK_SIZE 256 1901# define BLOCK_SIZE 256
1867 1902
1868static port * 1903ecb_cold static port *
1869port_rep_from_scratch (SCHEME_P) 1904port_rep_from_scratch (SCHEME_P)
1870{ 1905{
1871 char *start; 1906 char *start;
1872 port *pt = malloc (sizeof (port)); 1907 port *pt = malloc (sizeof (port));
1873 1908
1887 pt->rep.string.curr = start; 1922 pt->rep.string.curr = start;
1888 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; 1923 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1;
1889 return pt; 1924 return pt;
1890} 1925}
1891 1926
1892static pointer 1927ecb_cold static pointer
1893port_from_scratch (SCHEME_P) 1928port_from_scratch (SCHEME_P)
1894{ 1929{
1895 port *pt = port_rep_from_scratch (SCHEME_A); 1930 port *pt = port_rep_from_scratch (SCHEME_A);
1896 1931
1897 if (!pt && USE_ERROR_CHECKING) 1932 if (!pt && USE_ERROR_CHECKING)
1898 return NIL; 1933 return NIL;
1899 1934
1900 return mk_port (SCHEME_A_ pt); 1935 return mk_port (SCHEME_A_ pt);
1901} 1936}
1902 1937
1903static void 1938ecb_cold static void
1904port_close (SCHEME_P_ pointer p, int flag) 1939port_close (SCHEME_P_ pointer p, int flag)
1905{ 1940{
1906 port *pt = port (p); 1941 port *pt = port (p);
1907 1942
1908 pt->kind &= ~flag; 1943 pt->kind &= ~flag;
1928 } 1963 }
1929} 1964}
1930#endif 1965#endif
1931 1966
1932/* get new character from input file */ 1967/* get new character from input file */
1933static int 1968ecb_cold static int
1934inchar (SCHEME_P) 1969inchar (SCHEME_P)
1935{ 1970{
1936 int c; 1971 int c;
1937 port *pt = port (SCHEME_V->inport); 1972 port *pt = port (SCHEME_V->inport);
1938 1973
1952 } 1987 }
1953 1988
1954 return c; 1989 return c;
1955} 1990}
1956 1991
1957static int ungot = -1; 1992ecb_cold static int
1958
1959static int
1960basic_inchar (port *pt) 1993basic_inchar (port *pt)
1961{ 1994{
1962#if USE_PORTS
1963 if (pt->unget != -1) 1995 if (pt->unget != -1)
1964 { 1996 {
1965 int r = pt->unget; 1997 int r = pt->unget;
1966 pt->unget = -1; 1998 pt->unget = -1;
1967 return r; 1999 return r;
1968 } 2000 }
1969 2001
2002#if USE_PORTS
1970 if (pt->kind & port_file) 2003 if (pt->kind & port_file)
1971 { 2004 {
1972 char c; 2005 char c;
1973 2006
1974 if (!read (pt->rep.stdio.file, &c, 1)) 2007 if (!read (pt->rep.stdio.file, &c, 1))
1982 return EOF; 2015 return EOF;
1983 else 2016 else
1984 return *pt->rep.string.curr++; 2017 return *pt->rep.string.curr++;
1985 } 2018 }
1986#else 2019#else
1987 if (ungot == -1)
1988 {
1989 char c; 2020 char c;
1990 if (!read (0, &c, 1)) 2021
2022 if (!read (pt->rep.stdio.file, &c, 1))
1991 return EOF; 2023 return EOF;
1992 2024
1993 ungot = c;
1994 }
1995
1996 {
1997 int r = ungot;
1998 ungot = -1;
1999 return r; 2025 return c;
2000 }
2001#endif 2026#endif
2002} 2027}
2003 2028
2004/* back character to input buffer */ 2029/* back character to input buffer */
2005static void 2030ecb_cold static void
2006backchar (SCHEME_P_ int c) 2031backchar (SCHEME_P_ int c)
2007{ 2032{
2008#if USE_PORTS 2033 port *pt = port (SCHEME_V->inport);
2009 port *pt;
2010 2034
2011 if (c == EOF) 2035 if (c == EOF)
2012 return; 2036 return;
2013 2037
2014 pt = port (SCHEME_V->inport);
2015 pt->unget = c; 2038 pt->unget = c;
2016#else
2017 if (c == EOF)
2018 return;
2019
2020 ungot = c;
2021#endif
2022} 2039}
2023 2040
2024#if USE_PORTS 2041#if USE_PORTS
2025static int 2042ecb_cold static int
2026realloc_port_string (SCHEME_P_ port *p) 2043realloc_port_string (SCHEME_P_ port *p)
2027{ 2044{
2028 char *start = p->rep.string.start; 2045 char *start = p->rep.string.start;
2029 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; 2046 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE;
2030 char *str = malloc (new_size); 2047 char *str = malloc (new_size);
2043 else 2060 else
2044 return 0; 2061 return 0;
2045} 2062}
2046#endif 2063#endif
2047 2064
2048INTERFACE void 2065ecb_cold static void
2049putstr (SCHEME_P_ const char *s) 2066putchars (SCHEME_P_ const char *s, int len)
2050{ 2067{
2068 port *pt = port (SCHEME_V->outport);
2069
2051#if USE_PORTS 2070#if USE_PORTS
2052 port *pt = port (SCHEME_V->outport);
2053
2054 if (pt->kind & port_file)
2055 write (pt->rep.stdio.file, s, strlen (s));
2056 else
2057 for (; *s; s++)
2058 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2059 *pt->rep.string.curr++ = *s;
2060 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2061 *pt->rep.string.curr++ = *s;
2062
2063#else
2064 write (pt->rep.stdio.file, s, strlen (s));
2065#endif
2066}
2067
2068static void
2069putchars (SCHEME_P_ const char *s, int len)
2070{
2071#if USE_PORTS
2072 port *pt = port (SCHEME_V->outport);
2073
2074 if (pt->kind & port_file) 2071 if (pt->kind & port_file)
2075 write (pt->rep.stdio.file, s, len); 2072 write (pt->rep.stdio.file, s, len);
2076 else 2073 else
2077 { 2074 {
2078 for (; len; len--) 2075 for (; len; len--)
2083 *pt->rep.string.curr++ = *s++; 2080 *pt->rep.string.curr++ = *s++;
2084 } 2081 }
2085 } 2082 }
2086 2083
2087#else 2084#else
2088 write (1, s, len); 2085 write (1, s, len); // output not initialised
2089#endif 2086#endif
2087}
2088
2089INTERFACE void
2090putstr (SCHEME_P_ const char *s)
2091{
2092 putchars (SCHEME_A_ s, strlen (s));
2090} 2093}
2091 2094
2092INTERFACE void 2095INTERFACE void
2093putcharacter (SCHEME_P_ int c) 2096putcharacter (SCHEME_P_ int c)
2094{ 2097{
2095#if USE_PORTS
2096 port *pt = port (SCHEME_V->outport);
2097
2098 if (pt->kind & port_file)
2099 {
2100 char cc = c;
2101 write (pt->rep.stdio.file, &cc, 1);
2102 }
2103 else
2104 {
2105 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2106 *pt->rep.string.curr++ = c;
2107 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2108 *pt->rep.string.curr++ = c;
2109 }
2110
2111#else
2112 char cc = c; 2098 char cc = c;
2113 write (1, &c, 1); 2099
2114#endif 2100 putchars (SCHEME_A_ &cc, 1);
2115} 2101}
2116 2102
2117/* read characters up to delimiter, but cater to character constants */ 2103/* read characters up to delimiter, but cater to character constants */
2118static char * 2104ecb_cold static char *
2119readstr_upto (SCHEME_P_ int skip, const char *delim) 2105readstr_upto (SCHEME_P_ int skip, const char *delim)
2120{ 2106{
2121 char *p = SCHEME_V->strbuff + skip; 2107 char *p = SCHEME_V->strbuff + skip;
2122 2108
2123 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2109 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2132 2118
2133 return SCHEME_V->strbuff; 2119 return SCHEME_V->strbuff;
2134} 2120}
2135 2121
2136/* read string expression "xxx...xxx" */ 2122/* read string expression "xxx...xxx" */
2137static pointer 2123ecb_cold static pointer
2138readstrexp (SCHEME_P_ char delim) 2124readstrexp (SCHEME_P_ char delim)
2139{ 2125{
2140 char *p = SCHEME_V->strbuff; 2126 char *p = SCHEME_V->strbuff;
2141 int c; 2127 int c;
2142 int c1 = 0; 2128 int c1 = 0;
2175 case '7': 2161 case '7':
2176 state = st_oct1; 2162 state = st_oct1;
2177 c1 = c - '0'; 2163 c1 = c - '0';
2178 break; 2164 break;
2179 2165
2166 case 'a': *p++ = '\a'; state = st_ok; break;
2167 case 'n': *p++ = '\n'; state = st_ok; break;
2168 case 'r': *p++ = '\r'; state = st_ok; break;
2169 case 't': *p++ = '\t'; state = st_ok; break;
2170
2171 // this overshoots the minimum requirements of r7rs
2172 case ' ':
2173 case '\t':
2174 case '\r':
2175 case '\n':
2176 skipspace (SCHEME_A);
2177 state = st_ok;
2178 break;
2179
2180 //TODO: x should end in ;, not two-digit hex
2180 case 'x': 2181 case 'x':
2181 case 'X': 2182 case 'X':
2182 state = st_x1; 2183 state = st_x1;
2183 c1 = 0; 2184 c1 = 0;
2184 break;
2185
2186 case 'n':
2187 *p++ = '\n';
2188 state = st_ok;
2189 break;
2190
2191 case 't':
2192 *p++ = '\t';
2193 state = st_ok;
2194 break;
2195
2196 case 'r':
2197 *p++ = '\r';
2198 state = st_ok;
2199 break; 2185 break;
2200 2186
2201 default: 2187 default:
2202 *p++ = c; 2188 *p++ = c;
2203 state = st_ok; 2189 state = st_ok;
2255 } 2241 }
2256 } 2242 }
2257} 2243}
2258 2244
2259/* check c is in chars */ 2245/* check c is in chars */
2260ecb_inline int 2246ecb_cold int
2261is_one_of (const char *s, int c) 2247is_one_of (const char *s, int c)
2262{ 2248{
2263 return c == EOF || !!strchr (s, c); 2249 return c == EOF || !!strchr (s, c);
2264} 2250}
2265 2251
2266/* skip white characters */ 2252/* skip white characters */
2267ecb_inline int 2253ecb_cold int
2268skipspace (SCHEME_P) 2254skipspace (SCHEME_P)
2269{ 2255{
2270 int c, curr_line = 0; 2256 int c, curr_line = 0;
2271 2257
2272 do 2258 do
2292 backchar (SCHEME_A_ c); 2278 backchar (SCHEME_A_ c);
2293 return 1; 2279 return 1;
2294} 2280}
2295 2281
2296/* get token */ 2282/* get token */
2297static int 2283ecb_cold static int
2298token (SCHEME_P) 2284token (SCHEME_P)
2299{ 2285{
2300 int c = skipspace (SCHEME_A); 2286 int c = skipspace (SCHEME_A);
2301 2287
2302 if (c == EOF) 2288 if (c == EOF)
2400} 2386}
2401 2387
2402/* ========== Routines for Printing ========== */ 2388/* ========== Routines for Printing ========== */
2403#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) 2389#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL)
2404 2390
2405static void 2391ecb_cold static void
2406printslashstring (SCHEME_P_ char *p, int len) 2392printslashstring (SCHEME_P_ char *p, int len)
2407{ 2393{
2408 int i; 2394 int i;
2409 unsigned char *s = (unsigned char *) p; 2395 unsigned char *s = (unsigned char *) p;
2410 2396
2466 2452
2467 putcharacter (SCHEME_A_ '"'); 2453 putcharacter (SCHEME_A_ '"');
2468} 2454}
2469 2455
2470/* print atoms */ 2456/* print atoms */
2471static void 2457ecb_cold static void
2472printatom (SCHEME_P_ pointer l, int f) 2458printatom (SCHEME_P_ pointer l, int f)
2473{ 2459{
2474 char *p; 2460 char *p;
2475 int len; 2461 int len;
2476 2462
2477 atom2str (SCHEME_A_ l, f, &p, &len); 2463 atom2str (SCHEME_A_ l, f, &p, &len);
2478 putchars (SCHEME_A_ p, len); 2464 putchars (SCHEME_A_ p, len);
2479} 2465}
2480 2466
2481/* Uses internal buffer unless string pointer is already available */ 2467/* Uses internal buffer unless string pointer is already available */
2482static void 2468ecb_cold static void
2483atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2469atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2484{ 2470{
2485 char *p; 2471 char *p;
2486 2472
2487 if (l == NIL) 2473 if (l == NIL)
2694 return car (d); 2680 return car (d);
2695 2681
2696 p = cons (car (d), cdr (d)); 2682 p = cons (car (d), cdr (d));
2697 q = p; 2683 q = p;
2698 2684
2699 while (cdr (cdr (p)) != NIL) 2685 while (cddr (p) != NIL)
2700 { 2686 {
2701 d = cons (car (p), cdr (p)); 2687 d = cons (car (p), cdr (p));
2702 2688
2703 if (cdr (cdr (p)) != NIL) 2689 if (cddr (p) != NIL)
2704 p = cdr (d); 2690 p = cdr (d);
2705 } 2691 }
2706 2692
2707 set_cdr (p, car (cdr (p))); 2693 set_cdr (p, cadr (p));
2708 return q; 2694 return q;
2709} 2695}
2710 2696
2711/* reverse list -- produce new list */ 2697/* reverse list -- produce new list */
2712static pointer 2698ecb_hot static pointer
2713reverse (SCHEME_P_ pointer a) 2699reverse (SCHEME_P_ pointer a)
2714{ 2700{
2715 /* a must be checked by gc */ 2701 /* a must be checked by gc */
2716 pointer p = NIL; 2702 pointer p = NIL;
2717 2703
2720 2706
2721 return p; 2707 return p;
2722} 2708}
2723 2709
2724/* reverse list --- in-place */ 2710/* reverse list --- in-place */
2725static pointer 2711ecb_hot static pointer
2726reverse_in_place (SCHEME_P_ pointer term, pointer list) 2712reverse_in_place (SCHEME_P_ pointer term, pointer list)
2727{ 2713{
2728 pointer result = term; 2714 pointer result = term;
2729 pointer p = list; 2715 pointer p = list;
2730 2716
2738 2724
2739 return result; 2725 return result;
2740} 2726}
2741 2727
2742/* append list -- produce new list (in reverse order) */ 2728/* append list -- produce new list (in reverse order) */
2743static pointer 2729ecb_hot static pointer
2744revappend (SCHEME_P_ pointer a, pointer b) 2730revappend (SCHEME_P_ pointer a, pointer b)
2745{ 2731{
2746 pointer result = a; 2732 pointer result = a;
2747 pointer p = b; 2733 pointer p = b;
2748 2734
2757 2743
2758 return S_F; /* signal an error */ 2744 return S_F; /* signal an error */
2759} 2745}
2760 2746
2761/* equivalence of atoms */ 2747/* equivalence of atoms */
2762int 2748ecb_hot int
2763eqv (pointer a, pointer b) 2749eqv (pointer a, pointer b)
2764{ 2750{
2765 if (is_string (a)) 2751 if (is_string (a))
2766 { 2752 {
2767 if (is_string (b)) 2753 if (is_string (b))
2861 } 2847 }
2862 else 2848 else
2863 set_car (env, immutable_cons (slot, car (env))); 2849 set_car (env, immutable_cons (slot, car (env)));
2864} 2850}
2865 2851
2866static pointer 2852ecb_hot static pointer
2867find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2853find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2868{ 2854{
2869 pointer x, y; 2855 pointer x, y;
2870 2856
2871 for (x = env; x != NIL; x = cdr (x)) 2857 for (x = env; x != NIL; x = cdr (x))
2892 return NIL; 2878 return NIL;
2893} 2879}
2894 2880
2895#else /* USE_ALIST_ENV */ 2881#else /* USE_ALIST_ENV */
2896 2882
2897ecb_inline void 2883static void
2898new_frame_in_env (SCHEME_P_ pointer old_env) 2884new_frame_in_env (SCHEME_P_ pointer old_env)
2899{ 2885{
2900 SCHEME_V->envir = immutable_cons (NIL, old_env); 2886 SCHEME_V->envir = immutable_cons (NIL, old_env);
2901 setenvironment (SCHEME_V->envir); 2887 setenvironment (SCHEME_V->envir);
2902} 2888}
2903 2889
2904ecb_inline void 2890static void
2905new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2891new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2906{ 2892{
2907 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2893 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2908} 2894}
2909 2895
2910static pointer 2896ecb_hot static pointer
2911find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2897find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2912{ 2898{
2913 pointer x, y; 2899 pointer x, y;
2914 2900
2915 for (x = env; x != NIL; x = cdr (x)) 2901 for (x = env; x != NIL; x = cdr (x))
2929 return NIL; 2915 return NIL;
2930} 2916}
2931 2917
2932#endif /* USE_ALIST_ENV else */ 2918#endif /* USE_ALIST_ENV else */
2933 2919
2934ecb_inline void 2920static void
2935new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2921new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2936{ 2922{
2937 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 2923 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2938 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2924 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2939} 2925}
2940 2926
2941ecb_inline void 2927static void
2942set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2928set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2943{ 2929{
2944 set_cdr (slot, value); 2930 set_cdr (slot, value);
2945} 2931}
2946 2932
2947ecb_inline pointer 2933static pointer
2948slot_value_in_env (pointer slot) 2934slot_value_in_env (pointer slot)
2949{ 2935{
2950 return cdr (slot); 2936 return cdr (slot);
2951} 2937}
2952 2938
2953/* ========== Evaluation Cycle ========== */ 2939/* ========== Evaluation Cycle ========== */
2954 2940
2955static int 2941ecb_cold static int
2956xError_1 (SCHEME_P_ const char *s, pointer a) 2942xError_1 (SCHEME_P_ const char *s, pointer a)
2957{ 2943{
2958#if USE_ERROR_HOOK
2959 pointer x;
2960 pointer hdl = SCHEME_V->ERROR_HOOK;
2961#endif
2962
2963#if USE_PRINTF 2944#if USE_PRINTF
2964#if SHOW_ERROR_LINE 2945#if SHOW_ERROR_LINE
2965 char sbuf[STRBUFFSIZE]; 2946 char sbuf[STRBUFFSIZE];
2966 2947
2967 /* make sure error is not in REPL */ 2948 /* make sure error is not in REPL */
2982 } 2963 }
2983#endif 2964#endif
2984#endif 2965#endif
2985 2966
2986#if USE_ERROR_HOOK 2967#if USE_ERROR_HOOK
2987 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); 2968 pointer x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->ERROR_HOOK, 1);
2988 2969
2989 if (x != NIL) 2970 if (x != NIL)
2990 { 2971 {
2991 pointer code = a 2972 pointer code = a
2992 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) 2973 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL)
3036 pointer code; 3017 pointer code;
3037}; 3018};
3038 3019
3039# define STACK_GROWTH 3 3020# define STACK_GROWTH 3
3040 3021
3041static void 3022ecb_hot static void
3042s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3023s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3043{ 3024{
3044 int nframes = (uintptr_t)SCHEME_V->dump; 3025 int nframes = (uintptr_t)SCHEME_V->dump;
3045 struct dump_stack_frame *next_frame; 3026 struct dump_stack_frame *next_frame;
3046 3027
3059 next_frame->code = code; 3040 next_frame->code = code;
3060 3041
3061 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3042 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3062} 3043}
3063 3044
3064static int 3045static ecb_hot int
3065xs_return (SCHEME_P_ pointer a) 3046xs_return (SCHEME_P_ pointer a)
3066{ 3047{
3067 int nframes = (uintptr_t)SCHEME_V->dump; 3048 int nframes = (uintptr_t)SCHEME_V->dump;
3068 struct dump_stack_frame *frame; 3049 struct dump_stack_frame *frame;
3069 3050
3080 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3061 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3081 3062
3082 return 0; 3063 return 0;
3083} 3064}
3084 3065
3085ecb_inline void 3066ecb_cold void
3086dump_stack_reset (SCHEME_P) 3067dump_stack_reset (SCHEME_P)
3087{ 3068{
3088 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3069 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3089 SCHEME_V->dump = (pointer)+0; 3070 SCHEME_V->dump = (pointer)+0;
3090} 3071}
3091 3072
3092ecb_inline void 3073ecb_cold void
3093dump_stack_initialize (SCHEME_P) 3074dump_stack_initialize (SCHEME_P)
3094{ 3075{
3095 SCHEME_V->dump_size = 0; 3076 SCHEME_V->dump_size = 0;
3096 SCHEME_V->dump_base = 0; 3077 SCHEME_V->dump_base = 0;
3097 dump_stack_reset (SCHEME_A); 3078 dump_stack_reset (SCHEME_A);
3098} 3079}
3099 3080
3100static void 3081ecb_cold static void
3101dump_stack_free (SCHEME_P) 3082dump_stack_free (SCHEME_P)
3102{ 3083{
3103 free (SCHEME_V->dump_base); 3084 free (SCHEME_V->dump_base);
3104 SCHEME_V->dump_base = 0; 3085 SCHEME_V->dump_base = 0;
3105 SCHEME_V->dump = (pointer)0; 3086 SCHEME_V->dump = (pointer)0;
3106 SCHEME_V->dump_size = 0; 3087 SCHEME_V->dump_size = 0;
3107} 3088}
3108 3089
3109static void 3090ecb_cold static void
3110dump_stack_mark (SCHEME_P) 3091dump_stack_mark (SCHEME_P)
3111{ 3092{
3112 int nframes = (uintptr_t)SCHEME_V->dump; 3093 int nframes = (uintptr_t)SCHEME_V->dump;
3113 int i; 3094 int i;
3114 3095
3120 mark (frame->envir); 3101 mark (frame->envir);
3121 mark (frame->code); 3102 mark (frame->code);
3122 } 3103 }
3123} 3104}
3124 3105
3125static pointer 3106ecb_cold static pointer
3126ss_get_cont (SCHEME_P) 3107ss_get_cont (SCHEME_P)
3127{ 3108{
3128 int nframes = (uintptr_t)SCHEME_V->dump; 3109 int nframes = (uintptr_t)SCHEME_V->dump;
3129 int i; 3110 int i;
3130 3111
3142 } 3123 }
3143 3124
3144 return cont; 3125 return cont;
3145} 3126}
3146 3127
3147static void 3128ecb_cold static void
3148ss_set_cont (SCHEME_P_ pointer cont) 3129ss_set_cont (SCHEME_P_ pointer cont)
3149{ 3130{
3150 int i = 0; 3131 int i = 0;
3151 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3132 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3152 3133
3164 SCHEME_V->dump = (pointer)(uintptr_t)i; 3145 SCHEME_V->dump = (pointer)(uintptr_t)i;
3165} 3146}
3166 3147
3167#else 3148#else
3168 3149
3169ecb_inline void 3150ecb_cold void
3170dump_stack_reset (SCHEME_P) 3151dump_stack_reset (SCHEME_P)
3171{ 3152{
3172 SCHEME_V->dump = NIL; 3153 SCHEME_V->dump = NIL;
3173} 3154}
3174 3155
3175ecb_inline void 3156ecb_cold void
3176dump_stack_initialize (SCHEME_P) 3157dump_stack_initialize (SCHEME_P)
3177{ 3158{
3178 dump_stack_reset (SCHEME_A); 3159 dump_stack_reset (SCHEME_A);
3179} 3160}
3180 3161
3181static void 3162ecb_cold static void
3182dump_stack_free (SCHEME_P) 3163dump_stack_free (SCHEME_P)
3183{ 3164{
3184 SCHEME_V->dump = NIL; 3165 SCHEME_V->dump = NIL;
3185} 3166}
3186 3167
3187static int 3168ecb_hot static int
3188xs_return (SCHEME_P_ pointer a) 3169xs_return (SCHEME_P_ pointer a)
3189{ 3170{
3190 pointer dump = SCHEME_V->dump; 3171 pointer dump = SCHEME_V->dump;
3191 3172
3192 SCHEME_V->value = a; 3173 SCHEME_V->value = a;
3202 SCHEME_V->dump = dump; 3183 SCHEME_V->dump = dump;
3203 3184
3204 return 0; 3185 return 0;
3205} 3186}
3206 3187
3207static void 3188ecb_hot static void
3208s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3189s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3209{ 3190{
3210 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), 3191 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op),
3211 cons (args, 3192 cons (args,
3212 cons (SCHEME_V->envir, 3193 cons (SCHEME_V->envir,
3213 cons (code, 3194 cons (code,
3214 SCHEME_V->dump)))); 3195 SCHEME_V->dump))));
3215} 3196}
3216 3197
3217static void 3198ecb_cold static void
3218dump_stack_mark (SCHEME_P) 3199dump_stack_mark (SCHEME_P)
3219{ 3200{
3220 mark (SCHEME_V->dump); 3201 mark (SCHEME_V->dump);
3221} 3202}
3222 3203
3223static pointer 3204ecb_cold static pointer
3224ss_get_cont (SCHEME_P) 3205ss_get_cont (SCHEME_P)
3225{ 3206{
3226 return SCHEME_V->dump; 3207 return SCHEME_V->dump;
3227} 3208}
3228 3209
3229static void 3210ecb_cold static void
3230ss_set_cont (SCHEME_P_ pointer cont) 3211ss_set_cont (SCHEME_P_ pointer cont)
3231{ 3212{
3232 SCHEME_V->dump = cont; 3213 SCHEME_V->dump = cont;
3233} 3214}
3234 3215
3235#endif 3216#endif
3236 3217
3237#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3218#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3238 3219
3239#if EXPERIMENT 3220#if EXPERIMENT
3221
3240static int 3222static int
3241debug (SCHEME_P_ int indent, pointer x) 3223dtree (SCHEME_P_ int indent, pointer x)
3242{ 3224{
3243 int c; 3225 int c;
3244 3226
3245 if (is_syntax (x)) 3227 if (is_syntax (x))
3246 { 3228 {
3264 printf ("%*sS<%s>\n", indent, "", symname (x)); 3246 printf ("%*sS<%s>\n", indent, "", symname (x));
3265 return 24+8; 3247 return 24+8;
3266 3248
3267 case T_CLOSURE: 3249 case T_CLOSURE:
3268 printf ("%*sS<%s>\n", indent, "", "closure"); 3250 printf ("%*sS<%s>\n", indent, "", "closure");
3269 debug (SCHEME_A_ indent + 3, cdr(x)); 3251 dtree (SCHEME_A_ indent + 3, cdr(x));
3270 return 32 + debug (SCHEME_A_ indent + 3, car (x)); 3252 return 32 + dtree (SCHEME_A_ indent + 3, car (x));
3271 3253
3272 case T_PAIR: 3254 case T_PAIR:
3273 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); 3255 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3274 c = debug (SCHEME_A_ indent + 3, car (x)); 3256 c = dtree (SCHEME_A_ indent + 3, car (x));
3275 c += debug (SCHEME_A_ indent + 3, cdr (x)); 3257 c += dtree (SCHEME_A_ indent + 3, cdr (x));
3276 return c + 1; 3258 return c + 1;
3277 3259
3278 case T_PORT: 3260 case T_PORT:
3279 printf ("%*sS<%s>\n", indent, "", "port"); 3261 printf ("%*sS<%s>\n", indent, "", "port");
3280 return 24+8; 3262 return 24+8;
3283 printf ("%*sS<%s>\n", indent, "", "vector"); 3265 printf ("%*sS<%s>\n", indent, "", "vector");
3284 return 24+8; 3266 return 24+8;
3285 3267
3286 case T_ENVIRONMENT: 3268 case T_ENVIRONMENT:
3287 printf ("%*sS<%s>\n", indent, "", "environment"); 3269 printf ("%*sS<%s>\n", indent, "", "environment");
3288 return 0 + debug (SCHEME_A_ indent + 3, car (x)); 3270 return 0 + dtree (SCHEME_A_ indent + 3, car (x));
3289 3271
3290 default: 3272 default:
3291 printf ("unhandled type %d\n", type (x)); 3273 printf ("unhandled type %d\n", type (x));
3292 break; 3274 break;
3293 } 3275 }
3294} 3276}
3295#endif
3296 3277
3278#define DUMP(t) do { printf ("DUMP %s:%d\n", __FILE__, __LINE__); dtree (SCHEME_A_ 0, (t)); } while (0)
3279
3280typedef void *stream[1];
3281
3282#define stream_init() { 0 }
3283#define stream_data(s) ((char *)(s)[0] + sizeof (uint32_t) * 2)
3284#define stream_size(s) (((uint32_t *)(s)[0])[0] - sizeof (uint32_t) * 2)
3285#define stream_free(s) free (s[0])
3286
3287ecb_cold static void
3288stream_put (stream s, uint8_t byte)
3289{
3290 uint32_t *sp = *s;
3291 uint32_t size = sizeof (uint32_t) * 2;
3292 uint32_t offs = size;
3293
3294 if (ecb_expect_true (sp))
3295 {
3296 offs = sp[0];
3297 size = sp[1];
3298 }
3299
3300 if (ecb_expect_false (offs == size))
3301 {
3302 size *= 2;
3303 sp = realloc (sp, size);
3304 *s = sp;
3305 sp[1] = size;
3306
3307 }
3308
3309 ((uint8_t *)sp)[offs++] = byte;
3310 sp[0] = offs;
3311}
3312
3313ecb_cold static void
3314stream_put_v (stream s, uint32_t v)
3315{
3316 while (v > 0x7f)
3317 {
3318 stream_put (s, v | 0x80);
3319 v >>= 7;
3320 }
3321
3322 stream_put (s, v);
3323}
3324
3325ecb_cold static void
3326stream_put_tv (stream s, int bop, uint32_t v)
3327{
3328 printf ("put tv %d %d\n", bop, v);//D
3329 stream_put (s, bop);
3330 stream_put_v (s, v);
3331}
3332
3333ecb_cold static void
3334stream_put_stream (stream s, stream o)
3335{
3336 uint32_t i;
3337
3338 for (i = 0; i < stream_size (o); ++i)
3339 stream_put (s, stream_data (o)[i]);
3340
3341 stream_free (o);
3342}
3343
3344// calculates a (preferably small) integer that makes it possible to find
3345// the symbol again. if pointers were offsets into a memory area... until
3346// then, we return segment number in the low bits, and offset in the high
3347// bits.
3348// also, this function must never return 0.
3349ecb_cold static uint32_t
3350symbol_id (SCHEME_P_ pointer sym)
3351{
3352 struct cell *p = CELL (sym);
3353 int i;
3354
3355 for (i = SCHEME_V->last_cell_seg; i >= 0; --i)
3356 if (SCHEME_V->cell_seg[i] <= p && p < SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize[i])
3357 return i | ((p - SCHEME_V->cell_seg[i]) << CELL_NSEGMENT_LOG);
3358
3359 abort ();
3360}
3361
3362ecb_cold static uint32_t
3363cell_id (SCHEME_P_ pointer p)
3364{
3365 return symbol_id (SCHEME_A_ p);
3366}
3367
3368enum byteop
3369{
3370 BOP_NIL,
3371 BOP_SYNTAX,
3372 BOP_INTEGER,
3373 BOP_SYMBOL,
3374 BOP_LIST_BEG,
3375 BOP_LIST_END,
3376 BOP_BIFT, // branch if true
3377 BOP_BIFF, // branch if false
3378 BOP_BIFNE, // branch if not eqv?
3379 BOP_BRA, // "short" branch
3380 BOP_JMP, // "long" jump
3381 BOP_DATUM,
3382 BOP_LET,
3383 BOP_LETAST,
3384 BOP_LETREC,
3385 BOP_DEFINE,
3386 BOP_MACRO,
3387 BOP_SET,
3388 BOP_BEGIN,
3389 BOP_LAMBDA,
3390};
3391
3392ecb_cold static void compile_expr (SCHEME_P_ stream s, pointer x);
3393
3394ecb_cold static void
3395compile_list (SCHEME_P_ stream s, pointer x)
3396{
3397 for (; x != NIL; x = cdr (x))
3398 compile_expr (SCHEME_A_ s, car (x));
3399}
3400
3297static int 3401static void
3402compile_if (SCHEME_P_ stream s, pointer cond, pointer ift, pointer iff)
3403{
3404 //TODO: borked
3405 stream sift = stream_init (); compile_expr (SCHEME_A_ sift, ift);
3406
3407 stream_put (s, BOP_BIFF);
3408 compile_expr (SCHEME_A_ s, cond);
3409 stream_put_v (s, stream_size (sift));
3410 stream_put_stream (s, sift);
3411
3412 if (iff != NIL)
3413 {
3414 stream siff = stream_init (); compile_expr (SCHEME_A_ siff, iff);
3415 stream_put_tv (s, BOP_BRA, stream_size (siff));
3416 stream_put_stream (s, siff);
3417 }
3418}
3419
3420typedef uint32_t stream_fixup;
3421
3422static stream_fixup
3423stream_put_fixup (stream s)
3424{
3425 stream_put (s, 0);
3426 stream_put (s, 0);
3427
3428 return stream_size (s);
3429}
3430
3431static void
3432stream_fix_fixup (stream s, stream_fixup fixup, uint32_t target)
3433{
3434 target -= fixup;
3435 assert (target < (1 << 14));
3436 stream_data (s)[fixup - 2] = target | 0x80;
3437 stream_data (s)[fixup - 1] = target >> 7;
3438}
3439
3440static void
3441compile_and_or (SCHEME_P_ stream s, int and, pointer x)
3442{
3443 if (cdr (x) == NIL)
3444 compile_expr (SCHEME_A_ s, car (x));
3445 else
3446 {
3447 stream_put (s, and ? BOP_BIFF : BOP_BIFT);
3448 compile_expr (SCHEME_A_ s, car (x));
3449 stream_fixup end = stream_put_fixup (s);
3450
3451 compile_and_or (SCHEME_A_ s, and, cdr (x));
3452 stream_fix_fixup (s, end, stream_size (s));
3453 }
3454}
3455
3456ecb_cold static void
3457compile_expr (SCHEME_P_ stream s, pointer x)
3458{
3459 if (x == NIL)
3460 {
3461 stream_put (s, BOP_NIL);
3462 return;
3463 }
3464
3465 if (is_pair (x))
3466 {
3467 pointer head = car (x);
3468
3469 if (is_syntax (head))
3470 {
3471 x = cdr (x);
3472
3473 switch (syntaxnum (head))
3474 {
3475 case OP_IF0: /* if */
3476 compile_if (SCHEME_A_ s, car (x), cadr (x), caddr (x));
3477 break;
3478
3479 case OP_OR0: /* or */
3480 compile_and_or (SCHEME_A_ s, 0, x);
3481 break;
3482
3483 case OP_AND0: /* and */
3484 compile_and_or (SCHEME_A_ s, 1, x);
3485 break;
3486
3487 case OP_CASE0: /* case */
3488 abort ();
3489 break;
3490
3491 case OP_COND0: /* cond */
3492 abort ();
3493 break;
3494
3495 case OP_LET0: /* let */
3496 case OP_LET0AST: /* let* */
3497 case OP_LET0REC: /* letrec */
3498 switch (syntaxnum (head))
3499 {
3500 case OP_LET0: stream_put (s, BOP_LET ); break;
3501 case OP_LET0AST: stream_put (s, BOP_LETAST); break;
3502 case OP_LET0REC: stream_put (s, BOP_LETREC); break;
3503 }
3504
3505 {
3506 pointer bindings = car (x);
3507 pointer body = cadr (x);
3508
3509 for (x = bindings; x != NIL; x = cdr (x))
3510 {
3511 pointer init = NIL;
3512 pointer var = car (x);
3513
3514 if (is_pair (var))
3515 {
3516 init = cdr (var);
3517 var = car (var);
3518 }
3519
3520 stream_put_v (s, symbol_id (SCHEME_A_ var));
3521 compile_expr (SCHEME_A_ s, init);
3522 }
3523
3524 stream_put_v (s, 0);
3525 compile_expr (SCHEME_A_ s, body);
3526 }
3527 break;
3528
3529 case OP_DEF0: /* define */
3530 case OP_MACRO0: /* macro */
3531 stream_put (s, syntaxnum (head) == OP_DEF0 ? BOP_DEFINE : BOP_MACRO);
3532 stream_put_v (s, cell_id (SCHEME_A_ car (x)));
3533 compile_expr (SCHEME_A_ s, cadr (x));
3534 break;
3535
3536 case OP_SET0: /* set! */
3537 stream_put (s, BOP_SET);
3538 stream_put_v (s, symbol_id (SCHEME_A_ car (x)));
3539 compile_expr (SCHEME_A_ s, cadr (x));
3540 break;
3541
3542 case OP_BEGIN: /* begin */
3543 stream_put (s, BOP_BEGIN);
3544 compile_list (SCHEME_A_ s, x);
3545 return;
3546
3547 case OP_DELAY: /* delay */
3548 abort ();
3549 break;
3550
3551 case OP_QUOTE: /* quote */
3552 stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x));
3553 break;
3554
3555 case OP_LAMBDA: /* lambda */
3556 {
3557 pointer formals = car (x);
3558 pointer body = cadr (x);
3559
3560 stream_put (s, BOP_LAMBDA);
3561
3562 for (; is_pair (formals); formals = cdr (formals))
3563 stream_put_v (s, symbol_id (SCHEME_A_ car (formals)));
3564
3565 stream_put_v (s, 0);
3566 stream_put_v (s, formals == NIL ? 0 : symbol_id (SCHEME_A_ formals));
3567
3568 compile_expr (SCHEME_A_ s, body);
3569 }
3570 break;
3571
3572 case OP_C0STREAM:/* cons-stream */
3573 abort ();
3574 break;
3575 }
3576
3577 return;
3578 }
3579
3580 pointer m = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, head, 1);
3581
3582 if (m != NIL)
3583 {
3584 m = slot_value_in_env (m);
3585
3586 if (is_macro (m))
3587 {
3588 s_save (SCHEME_A_ OP_DEBUG2, SCHEME_V->args, SCHEME_V->code);
3589 SCHEME_V->code = m;
3590 SCHEME_V->args = cons (x, NIL);
3591 Eval_Cycle (SCHEME_A_ OP_APPLY);
3592 x = SCHEME_V->value;
3593 compile_expr (SCHEME_A_ s, SCHEME_V->value);
3594 return;
3595 }
3596 }
3597 }
3598
3599 switch (type (x))
3600 {
3601 case T_INTEGER:
3602 {
3603 IVALUE iv = ivalue_unchecked (x);
3604 iv = iv < 0 ? ((uint32_t)-iv << 1) | 1 : (uint32_t)iv << 1;
3605 stream_put_tv (s, BOP_INTEGER, iv);
3606 }
3607 return;
3608
3609 case T_SYMBOL:
3610 stream_put_tv (s, BOP_SYMBOL, symbol_id (SCHEME_A_ x));
3611 return;
3612
3613 case T_PAIR:
3614 stream_put (s, BOP_LIST_BEG);
3615
3616 for (; x != NIL; x = cdr (x))
3617 compile_expr (SCHEME_A_ s, car (x));
3618
3619 stream_put (s, BOP_LIST_END);
3620 return;
3621
3622 default:
3623 stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x));
3624 break;
3625 }
3626}
3627
3628ecb_cold static int
3629compile_closure (SCHEME_P_ pointer p)
3630{
3631 stream s = stream_init ();
3632
3633 compile_list (SCHEME_A_ s, cdar (p));
3634
3635 FILE *xxd = popen ("xxd", "we");
3636 fwrite (stream_data (s), 1, stream_size (s), xxd);
3637 fclose (xxd);
3638
3639 return stream_size (s);
3640}
3641
3642#endif
3643
3644/* syntax, eval, core, ... */
3645ecb_hot static int
3298opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3646opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3299{ 3647{
3300 pointer args = SCHEME_V->args; 3648 pointer args = SCHEME_V->args;
3301 pointer x, y; 3649 pointer x, y;
3302 3650
3303 switch (op) 3651 switch (op)
3304 { 3652 {
3305#if EXPERIMENT //D 3653#if EXPERIMENT //D
3306 case OP_DEBUG: 3654 case OP_DEBUG:
3307 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); 3655 {
3656 uint32_t len = compile_closure (SCHEME_A_ car (args));
3657 printf ("len = %d\n", len);
3308 printf ("\n"); 3658 printf ("\n");
3309 s_return (S_T); 3659 s_return (S_T);
3660 }
3661
3662 case OP_DEBUG2:
3663 return -1;
3310#endif 3664#endif
3665
3311 case OP_LOAD: /* load */ 3666 case OP_LOAD: /* load */
3312 if (file_interactive (SCHEME_A)) 3667 if (file_interactive (SCHEME_A))
3313 { 3668 {
3314 putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n"); 3669 putstr (SCHEME_A_ "Loading ");
3315 //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args))); 3670 putstr (SCHEME_A_ strvalue (car (args)));
3671 putcharacter (SCHEME_A_ '\n');
3316 } 3672 }
3317 3673
3318 if (!file_push (SCHEME_A_ strvalue (car (args)))) 3674 if (!file_push (SCHEME_A_ strvalue (car (args))))
3319 Error_1 ("unable to open", car (args)); 3675 Error_1 ("unable to open", car (args));
3320 else 3676
3321 {
3322 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3677 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3323 s_goto (OP_T0LVL); 3678 s_goto (OP_T0LVL);
3324 }
3325 3679
3326 case OP_T0LVL: /* top level */ 3680 case OP_T0LVL: /* top level */
3327 3681
3328 /* If we reached the end of file, this loop is done. */ 3682 /* If we reached the end of file, this loop is done. */
3329 if (port (SCHEME_V->loadport)->kind & port_saw_EOF) 3683 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3345 /* If interactive, be nice to user. */ 3699 /* If interactive, be nice to user. */
3346 if (file_interactive (SCHEME_A)) 3700 if (file_interactive (SCHEME_A))
3347 { 3701 {
3348 SCHEME_V->envir = SCHEME_V->global_env; 3702 SCHEME_V->envir = SCHEME_V->global_env;
3349 dump_stack_reset (SCHEME_A); 3703 dump_stack_reset (SCHEME_A);
3350 putstr (SCHEME_A_ "\n"); 3704 putcharacter (SCHEME_A_ '\n');
3705#if EXPERIMENT
3706 system ("ps v $PPID");
3707#endif
3351 putstr (SCHEME_A_ prompt); 3708 putstr (SCHEME_A_ prompt);
3352 } 3709 }
3353 3710
3354 /* Set up another iteration of REPL */ 3711 /* Set up another iteration of REPL */
3355 SCHEME_V->nesting = 0; 3712 SCHEME_V->nesting = 0;
3390 { 3747 {
3391 SCHEME_V->print_flag = 1; 3748 SCHEME_V->print_flag = 1;
3392 SCHEME_V->args = SCHEME_V->value; 3749 SCHEME_V->args = SCHEME_V->value;
3393 s_goto (OP_P0LIST); 3750 s_goto (OP_P0LIST);
3394 } 3751 }
3395 else 3752
3396 s_return (SCHEME_V->value); 3753 s_return (SCHEME_V->value);
3397 3754
3398 case OP_EVAL: /* main part of evaluation */ 3755 case OP_EVAL: /* main part of evaluation */
3399#if USE_TRACING 3756#if USE_TRACING
3400 if (SCHEME_V->tracing) 3757 if (SCHEME_V->tracing)
3401 { 3758 {
3434 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ 3791 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */
3435 SCHEME_V->code = x; 3792 SCHEME_V->code = x;
3436 s_goto (OP_EVAL); 3793 s_goto (OP_EVAL);
3437 } 3794 }
3438 } 3795 }
3439 else 3796
3440 s_return (SCHEME_V->code); 3797 s_return (SCHEME_V->code);
3441 3798
3442 case OP_E0ARGS: /* eval arguments */ 3799 case OP_E0ARGS: /* eval arguments */
3443 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ 3800 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3444 { 3801 {
3445 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3802 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3446 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3803 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3447 SCHEME_V->code = SCHEME_V->value; 3804 SCHEME_V->code = SCHEME_V->value;
3448 s_goto (OP_APPLY); 3805 s_goto (OP_APPLY);
3449 } 3806 }
3450 else 3807
3451 {
3452 SCHEME_V->code = cdr (SCHEME_V->code); 3808 SCHEME_V->code = cdr (SCHEME_V->code);
3453 s_goto (OP_E1ARGS); 3809 s_goto (OP_E1ARGS);
3454 }
3455 3810
3456 case OP_E1ARGS: /* eval arguments */ 3811 case OP_E1ARGS: /* eval arguments */
3457 args = cons (SCHEME_V->value, args); 3812 args = cons (SCHEME_V->value, args);
3458 3813
3459 if (is_pair (SCHEME_V->code)) /* continue */ 3814 if (is_pair (SCHEME_V->code)) /* continue */
3470 SCHEME_V->args = cdr (args); 3825 SCHEME_V->args = cdr (args);
3471 s_goto (OP_APPLY); 3826 s_goto (OP_APPLY);
3472 } 3827 }
3473 3828
3474#if USE_TRACING 3829#if USE_TRACING
3475
3476 case OP_TRACING: 3830 case OP_TRACING:
3477 { 3831 {
3478 int tr = SCHEME_V->tracing; 3832 int tr = SCHEME_V->tracing;
3479 3833
3480 SCHEME_V->tracing = ivalue_unchecked (car (args)); 3834 SCHEME_V->tracing = ivalue_unchecked (car (args));
3481 s_return (mk_integer (SCHEME_A_ tr)); 3835 s_return (mk_integer (SCHEME_A_ tr));
3482 } 3836 }
3483
3484#endif 3837#endif
3485 3838
3486 case OP_APPLY: /* apply 'code' to 'args' */ 3839 case OP_APPLY: /* apply 'code' to 'args' */
3487#if USE_TRACING 3840#if USE_TRACING
3488 if (SCHEME_V->tracing) 3841 if (SCHEME_V->tracing)
3542 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ 3895 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */
3543 { 3896 {
3544 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); 3897 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code));
3545 s_return (args != NIL ? car (args) : NIL); 3898 s_return (args != NIL ? car (args) : NIL);
3546 } 3899 }
3547 else 3900
3548 Error_0 ("illegal function"); 3901 Error_0 ("illegal function");
3549 3902
3550 case OP_DOMACRO: /* do macro */ 3903 case OP_DOMACRO: /* do macro */
3551 SCHEME_V->code = SCHEME_V->value; 3904 SCHEME_V->code = SCHEME_V->value;
3552 s_goto (OP_EVAL); 3905 s_goto (OP_EVAL);
3553 3906
3617 else 3970 else
3618 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); 3971 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value);
3619 3972
3620 s_return (SCHEME_V->code); 3973 s_return (SCHEME_V->code);
3621 3974
3622
3623 case OP_DEFP: /* defined? */ 3975 case OP_DEFP: /* defined? */
3624 x = SCHEME_V->envir; 3976 x = SCHEME_V->envir;
3625 3977
3626 if (cdr (args) != NIL) 3978 if (cdr (args) != NIL)
3627 x = cadr (args); 3979 x = cadr (args);
3645 s_return (SCHEME_V->value); 3997 s_return (SCHEME_V->value);
3646 } 3998 }
3647 else 3999 else
3648 Error_1 ("set!: unbound variable:", SCHEME_V->code); 4000 Error_1 ("set!: unbound variable:", SCHEME_V->code);
3649 4001
3650
3651 case OP_BEGIN: /* begin */ 4002 case OP_BEGIN: /* begin */
3652 if (!is_pair (SCHEME_V->code)) 4003 if (!is_pair (SCHEME_V->code))
3653 s_return (SCHEME_V->code); 4004 s_return (SCHEME_V->code);
3654 4005
3655 if (cdr (SCHEME_V->code) != NIL) 4006 if (cdr (SCHEME_V->code) != NIL)
3666 case OP_IF1: /* if */ 4017 case OP_IF1: /* if */
3667 if (is_true (SCHEME_V->value)) 4018 if (is_true (SCHEME_V->value))
3668 SCHEME_V->code = car (SCHEME_V->code); 4019 SCHEME_V->code = car (SCHEME_V->code);
3669 else 4020 else
3670 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ 4021 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
4022
3671 s_goto (OP_EVAL); 4023 s_goto (OP_EVAL);
3672 4024
3673 case OP_LET0: /* let */ 4025 case OP_LET0: /* let */
3674 SCHEME_V->args = NIL; 4026 SCHEME_V->args = NIL;
3675 SCHEME_V->value = SCHEME_V->code; 4027 SCHEME_V->value = SCHEME_V->code;
3676 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); 4028 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code);
3677 s_goto (OP_LET1); 4029 s_goto (OP_LET1);
3678 4030
3679 case OP_LET1: /* let (calculate parameters) */ 4031 case OP_LET1: /* let (calculate parameters) */
4032 case OP_LET1REC: /* letrec (calculate parameters) */
3680 args = cons (SCHEME_V->value, args); 4033 args = cons (SCHEME_V->value, args);
3681 4034
3682 if (is_pair (SCHEME_V->code)) /* continue */ 4035 if (is_pair (SCHEME_V->code)) /* continue */
3683 { 4036 {
3684 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 4037 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3685 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); 4038 Error_1 ("Bad syntax of binding spec in let/letrec:", car (SCHEME_V->code));
3686 4039
3687 s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code)); 4040 s_save (SCHEME_A_ op, args, cdr (SCHEME_V->code));
3688 SCHEME_V->code = cadar (SCHEME_V->code); 4041 SCHEME_V->code = cadar (SCHEME_V->code);
3689 SCHEME_V->args = NIL; 4042 SCHEME_V->args = NIL;
3690 s_goto (OP_EVAL); 4043 s_goto (OP_EVAL);
3691 } 4044 }
3692 else /* end */ 4045
3693 { 4046 /* end */
3694 args = reverse_in_place (SCHEME_A_ NIL, args); 4047 args = reverse_in_place (SCHEME_A_ NIL, args);
3695 SCHEME_V->code = car (args); 4048 SCHEME_V->code = car (args);
3696 SCHEME_V->args = cdr (args); 4049 SCHEME_V->args = cdr (args);
3697 s_goto (OP_LET2); 4050 s_goto (op == OP_LET1 ? OP_LET2 : OP_LET2REC);
3698 }
3699 4051
3700 case OP_LET2: /* let */ 4052 case OP_LET2: /* let */
3701 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4053 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3702 4054
3703 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args; 4055 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args;
3707 if (is_symbol (car (SCHEME_V->code))) /* named let */ 4059 if (is_symbol (car (SCHEME_V->code))) /* named let */
3708 { 4060 {
3709 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x)) 4061 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x))
3710 { 4062 {
3711 if (!is_pair (x)) 4063 if (!is_pair (x))
3712 Error_1 ("Bad syntax of binding in let :", x); 4064 Error_1 ("Bad syntax of binding in let:", x);
3713 4065
3714 if (!is_list (SCHEME_A_ car (x))) 4066 if (!is_list (SCHEME_A_ car (x)))
3715 Error_1 ("Bad syntax of binding in let :", car (x)); 4067 Error_1 ("Bad syntax of binding in let:", car (x));
3716 4068
3717 args = cons (caar (x), args); 4069 args = cons (caar (x), args);
3718 } 4070 }
3719 4071
3720 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)), 4072 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)),
3737 SCHEME_V->code = cdr (SCHEME_V->code); 4089 SCHEME_V->code = cdr (SCHEME_V->code);
3738 s_goto (OP_BEGIN); 4090 s_goto (OP_BEGIN);
3739 } 4091 }
3740 4092
3741 if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code))) 4093 if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code)))
3742 Error_1 ("Bad syntax of binding spec in let* :", car (SCHEME_V->code)); 4094 Error_1 ("Bad syntax of binding spec in let*:", car (SCHEME_V->code));
3743 4095
3744 s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code)); 4096 s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code));
3745 SCHEME_V->code = car (cdaar (SCHEME_V->code)); 4097 SCHEME_V->code = car (cdaar (SCHEME_V->code));
3746 s_goto (OP_EVAL); 4098 s_goto (OP_EVAL);
3747 4099
3758 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); 4110 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code);
3759 SCHEME_V->code = cadar (SCHEME_V->code); 4111 SCHEME_V->code = cadar (SCHEME_V->code);
3760 SCHEME_V->args = NIL; 4112 SCHEME_V->args = NIL;
3761 s_goto (OP_EVAL); 4113 s_goto (OP_EVAL);
3762 } 4114 }
3763 else /* end */ 4115
4116 /* end */
3764 { 4117
3765 SCHEME_V->code = args; 4118 SCHEME_V->code = args;
3766 SCHEME_V->args = NIL; 4119 SCHEME_V->args = NIL;
3767 s_goto (OP_BEGIN); 4120 s_goto (OP_BEGIN);
3768 }
3769 4121
3770 case OP_LET0REC: /* letrec */ 4122 case OP_LET0REC: /* letrec */
3771 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4123 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3772 SCHEME_V->args = NIL; 4124 SCHEME_V->args = NIL;
3773 SCHEME_V->value = SCHEME_V->code; 4125 SCHEME_V->value = SCHEME_V->code;
3774 SCHEME_V->code = car (SCHEME_V->code); 4126 SCHEME_V->code = car (SCHEME_V->code);
3775 s_goto (OP_LET1REC); 4127 s_goto (OP_LET1REC);
3776 4128
3777 case OP_LET1REC: /* letrec (calculate parameters) */ 4129 /* OP_LET1REC handled by OP_LET1 */
3778 args = cons (SCHEME_V->value, args);
3779
3780 if (is_pair (SCHEME_V->code)) /* continue */
3781 {
3782 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3783 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3784
3785 s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code));
3786 SCHEME_V->code = cadar (SCHEME_V->code);
3787 SCHEME_V->args = NIL;
3788 s_goto (OP_EVAL);
3789 }
3790 else /* end */
3791 {
3792 args = reverse_in_place (SCHEME_A_ NIL, args);
3793 SCHEME_V->code = car (args);
3794 SCHEME_V->args = cdr (args);
3795 s_goto (OP_LET2REC);
3796 }
3797 4130
3798 case OP_LET2REC: /* letrec */ 4131 case OP_LET2REC: /* letrec */
3799 for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y)) 4132 for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y))
3800 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 4133 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3801 4134
3831 } 4164 }
3832 else 4165 else
3833 { 4166 {
3834 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) 4167 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL)
3835 s_return (NIL); 4168 s_return (NIL);
3836 else 4169
3837 {
3838 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 4170 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
3839 SCHEME_V->code = caar (SCHEME_V->code); 4171 SCHEME_V->code = caar (SCHEME_V->code);
3840 s_goto (OP_EVAL); 4172 s_goto (OP_EVAL);
3841 }
3842 } 4173 }
3843 4174
3844 case OP_DELAY: /* delay */ 4175 case OP_DELAY: /* delay */
3845 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); 4176 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir);
3846 set_typeflag (x, T_PROMISE); 4177 set_typeflag (x, T_PROMISE);
3857 case OP_AND1: /* and */ 4188 case OP_AND1: /* and */
3858 if (is_false (SCHEME_V->value)) 4189 if (is_false (SCHEME_V->value))
3859 s_return (SCHEME_V->value); 4190 s_return (SCHEME_V->value);
3860 else if (SCHEME_V->code == NIL) 4191 else if (SCHEME_V->code == NIL)
3861 s_return (SCHEME_V->value); 4192 s_return (SCHEME_V->value);
3862 else 4193
3863 {
3864 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 4194 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
3865 SCHEME_V->code = car (SCHEME_V->code); 4195 SCHEME_V->code = car (SCHEME_V->code);
3866 s_goto (OP_EVAL); 4196 s_goto (OP_EVAL);
3867 }
3868 4197
3869 case OP_OR0: /* or */ 4198 case OP_OR0: /* or */
3870 if (SCHEME_V->code == NIL) 4199 if (SCHEME_V->code == NIL)
3871 s_return (S_F); 4200 s_return (S_F);
3872 4201
3877 case OP_OR1: /* or */ 4206 case OP_OR1: /* or */
3878 if (is_true (SCHEME_V->value)) 4207 if (is_true (SCHEME_V->value))
3879 s_return (SCHEME_V->value); 4208 s_return (SCHEME_V->value);
3880 else if (SCHEME_V->code == NIL) 4209 else if (SCHEME_V->code == NIL)
3881 s_return (SCHEME_V->value); 4210 s_return (SCHEME_V->value);
3882 else 4211
3883 {
3884 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 4212 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
3885 SCHEME_V->code = car (SCHEME_V->code); 4213 SCHEME_V->code = car (SCHEME_V->code);
3886 s_goto (OP_EVAL); 4214 s_goto (OP_EVAL);
3887 }
3888 4215
3889 case OP_C0STREAM: /* cons-stream */ 4216 case OP_C0STREAM: /* cons-stream */
3890 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 4217 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3891 SCHEME_V->code = car (SCHEME_V->code); 4218 SCHEME_V->code = car (SCHEME_V->code);
3892 s_goto (OP_EVAL); 4219 s_goto (OP_EVAL);
3957 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); 4284 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x));
3958 SCHEME_V->code = caar (x); 4285 SCHEME_V->code = caar (x);
3959 s_goto (OP_EVAL); 4286 s_goto (OP_EVAL);
3960 } 4287 }
3961 } 4288 }
3962 else 4289
3963 s_return (NIL); 4290 s_return (NIL);
3964 4291
3965 case OP_CASE2: /* case */ 4292 case OP_CASE2: /* case */
3966 if (is_true (SCHEME_V->value)) 4293 if (is_true (SCHEME_V->value))
3967 s_goto (OP_BEGIN); 4294 s_goto (OP_BEGIN);
3968 else 4295
3969 s_return (NIL); 4296 s_return (NIL);
3970 4297
3971 case OP_PAPPLY: /* apply */ 4298 case OP_PAPPLY: /* apply */
3972 SCHEME_V->code = car (args); 4299 SCHEME_V->code = car (args);
3973 SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); 4300 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
3974 /*SCHEME_V->args = cadr(args); */ 4301 /*SCHEME_V->args = cadr(args); */
3988 } 4315 }
3989 4316
3990 if (USE_ERROR_CHECKING) abort (); 4317 if (USE_ERROR_CHECKING) abort ();
3991} 4318}
3992 4319
3993static int 4320/* math, cxr */
4321ecb_hot static int
3994opexe_1 (SCHEME_P_ enum scheme_opcodes op) 4322opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3995{ 4323{
3996 pointer args = SCHEME_V->args; 4324 pointer args = SCHEME_V->args;
3997 pointer x = car (args); 4325 pointer x = car (args);
3998 num v; 4326 num v;
4011 Error_1 ("inexact->exact: not integral:", x); 4339 Error_1 ("inexact->exact: not integral:", x);
4012 } 4340 }
4013 4341
4014 s_return (x); 4342 s_return (x);
4015 4343
4344 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4345 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4346 case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x))));
4347 case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x))));
4348
4349 case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4016 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4350 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
4017 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) 4351 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))
4018 / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); 4352 / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args))))));
4019 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 4353 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
4020 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 4354 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
4026 s_return (mk_real (SCHEME_A_ 4360 s_return (mk_real (SCHEME_A_
4027 cdr (args) == NIL 4361 cdr (args) == NIL
4028 ? atan (rvalue (x)) 4362 ? atan (rvalue (x))
4029 : atan2 (rvalue (x), rvalue (cadr (args))))); 4363 : atan2 (rvalue (x), rvalue (cadr (args)))));
4030 4364
4031 case OP_SQRT:
4032 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4033
4034 case OP_EXPT: 4365 case OP_EXPT:
4035 { 4366 {
4036 RVALUE result; 4367 RVALUE result;
4037 int real_result = 1; 4368 int real_result = 1;
4038 pointer y = cadr (args); 4369 pointer y = cadr (args);
4060 if (real_result) 4391 if (real_result)
4061 s_return (mk_real (SCHEME_A_ result)); 4392 s_return (mk_real (SCHEME_A_ result));
4062 else 4393 else
4063 s_return (mk_integer (SCHEME_A_ result)); 4394 s_return (mk_integer (SCHEME_A_ result));
4064 } 4395 }
4065
4066 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4067 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4068 case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x))));
4069 case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x))));
4070#endif 4396#endif
4071 4397
4072 case OP_ADD: /* + */ 4398 case OP_ADD: /* + */
4073 v = num_zero; 4399 v = num_zero;
4074 4400
4376 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4702 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4377 4703
4378 s_return (newstr); 4704 s_return (newstr);
4379 } 4705 }
4380 4706
4381 case OP_SUBSTR: /* substring */ 4707 case OP_STRING_COPY: /* substring/string-copy */
4382 { 4708 {
4383 char *str = strvalue (x); 4709 char *str = strvalue (x);
4384 int index0 = ivalue_unchecked (cadr (args)); 4710 int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args));
4385 int index1; 4711 int index1;
4386 int len; 4712 int len;
4387 4713
4388 if (index0 > strlength (x)) 4714 if (index0 > strlength (x))
4389 Error_1 ("substring: start out of bounds:", cadr (args)); 4715 Error_1 ("string->copy: start out of bounds:", cadr (args));
4390 4716
4391 if (cddr (args) != NIL) 4717 if (cddr (args) != NIL)
4392 { 4718 {
4393 index1 = ivalue_unchecked (caddr (args)); 4719 index1 = ivalue_unchecked (caddr (args));
4394 4720
4395 if (index1 > strlength (x) || index1 < index0) 4721 if (index1 > strlength (x) || index1 < index0)
4396 Error_1 ("substring: end out of bounds:", caddr (args)); 4722 Error_1 ("string->copy: end out of bounds:", caddr (args));
4397 } 4723 }
4398 else 4724 else
4399 index1 = strlength (x); 4725 index1 = strlength (x);
4400 4726
4401 len = index1 - index0; 4727 len = index1 - index0;
4402 x = mk_empty_string (SCHEME_A_ len, ' '); 4728 x = mk_counted_string (SCHEME_A_ str + index0, len);
4403 memcpy (strvalue (x), str + index0, len);
4404 strvalue (x)[len] = 0;
4405 4729
4406 s_return (x); 4730 s_return (x);
4407 } 4731 }
4408 4732
4409 case OP_VECTOR: /* vector */ 4733 case OP_VECTOR: /* vector */
4483 } 4807 }
4484 4808
4485 if (USE_ERROR_CHECKING) abort (); 4809 if (USE_ERROR_CHECKING) abort ();
4486} 4810}
4487 4811
4488static int 4812/* relational ops */
4813ecb_hot static int
4489opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4814opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4490{ 4815{
4491 pointer x = SCHEME_V->args; 4816 pointer x = SCHEME_V->args;
4492 4817
4493 for (;;) 4818 for (;;)
4514 } 4839 }
4515 4840
4516 s_return (S_T); 4841 s_return (S_T);
4517} 4842}
4518 4843
4519static int 4844/* predicates */
4845ecb_hot static int
4520opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4846opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4521{ 4847{
4522 pointer args = SCHEME_V->args; 4848 pointer args = SCHEME_V->args;
4523 pointer a = car (args); 4849 pointer a = car (args);
4524 pointer d = cdr (args); 4850 pointer d = cdr (args);
4571 } 4897 }
4572 4898
4573 s_retbool (r); 4899 s_retbool (r);
4574} 4900}
4575 4901
4576static int 4902/* promises, list ops, ports */
4903ecb_hot static int
4577opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4904opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4578{ 4905{
4579 pointer args = SCHEME_V->args; 4906 pointer args = SCHEME_V->args;
4580 pointer a = car (args); 4907 pointer a = car (args);
4581 pointer x, y; 4908 pointer x, y;
4598 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4925 case OP_SAVE_FORCED: /* Save forced value replacing promise */
4599 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); 4926 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
4600 s_return (SCHEME_V->value); 4927 s_return (SCHEME_V->value);
4601 4928
4602#if USE_PORTS 4929#if USE_PORTS
4930
4931 case OP_EOF_OBJECT: /* eof-object */
4932 s_return (S_EOF);
4603 4933
4604 case OP_WRITE: /* write */ 4934 case OP_WRITE: /* write */
4605 case OP_DISPLAY: /* display */ 4935 case OP_DISPLAY: /* display */
4606 case OP_WRITE_CHAR: /* write-char */ 4936 case OP_WRITE_CHAR: /* write-char */
4607 if (is_pair (cdr (SCHEME_V->args))) 4937 if (is_pair (cdr (SCHEME_V->args)))
4621 else 4951 else
4622 SCHEME_V->print_flag = 0; 4952 SCHEME_V->print_flag = 0;
4623 4953
4624 s_goto (OP_P0LIST); 4954 s_goto (OP_P0LIST);
4625 4955
4956 //TODO: move to scheme
4626 case OP_NEWLINE: /* newline */ 4957 case OP_NEWLINE: /* newline */
4627 if (is_pair (args)) 4958 if (is_pair (args))
4628 { 4959 {
4629 if (a != SCHEME_V->outport) 4960 if (a != SCHEME_V->outport)
4630 { 4961 {
4632 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4963 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4633 SCHEME_V->outport = a; 4964 SCHEME_V->outport = a;
4634 } 4965 }
4635 } 4966 }
4636 4967
4637 putstr (SCHEME_A_ "\n"); 4968 putcharacter (SCHEME_A_ '\n');
4638 s_return (S_T); 4969 s_return (S_T);
4639#endif 4970#endif
4640 4971
4641 case OP_ERR0: /* error */ 4972 case OP_ERR0: /* error */
4642 SCHEME_V->retcode = -1; 4973 SCHEME_V->retcode = -1;
4651 putstr (SCHEME_A_ strvalue (car (args))); 4982 putstr (SCHEME_A_ strvalue (car (args)));
4652 SCHEME_V->args = cdr (args); 4983 SCHEME_V->args = cdr (args);
4653 s_goto (OP_ERR1); 4984 s_goto (OP_ERR1);
4654 4985
4655 case OP_ERR1: /* error */ 4986 case OP_ERR1: /* error */
4656 putstr (SCHEME_A_ " "); 4987 putcharacter (SCHEME_A_ ' ');
4657 4988
4658 if (args != NIL) 4989 if (args != NIL)
4659 { 4990 {
4660 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); 4991 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4661 SCHEME_V->args = a; 4992 SCHEME_V->args = a;
4662 SCHEME_V->print_flag = 1; 4993 SCHEME_V->print_flag = 1;
4663 s_goto (OP_P0LIST); 4994 s_goto (OP_P0LIST);
4664 } 4995 }
4665 else 4996 else
4666 { 4997 {
4667 putstr (SCHEME_A_ "\n"); 4998 putcharacter (SCHEME_A_ '\n');
4668 4999
4669 if (SCHEME_V->interactive_repl) 5000 if (SCHEME_V->interactive_repl)
4670 s_goto (OP_T0LVL); 5001 s_goto (OP_T0LVL);
4671 else 5002 else
4672 return -1; 5003 return -1;
4880 } 5211 }
4881 5212
4882 if (USE_ERROR_CHECKING) abort (); 5213 if (USE_ERROR_CHECKING) abort ();
4883} 5214}
4884 5215
4885static int 5216/* reading */
5217ecb_cold static int
4886opexe_5 (SCHEME_P_ enum scheme_opcodes op) 5218opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4887{ 5219{
4888 pointer args = SCHEME_V->args; 5220 pointer args = SCHEME_V->args;
4889 pointer x; 5221 pointer x;
4890 5222
4969 case OP_RDSEXPR: 5301 case OP_RDSEXPR:
4970 switch (SCHEME_V->tok) 5302 switch (SCHEME_V->tok)
4971 { 5303 {
4972 case TOK_EOF: 5304 case TOK_EOF:
4973 s_return (S_EOF); 5305 s_return (S_EOF);
4974 /* NOTREACHED */
4975 5306
4976 case TOK_VEC: 5307 case TOK_VEC:
4977 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); 5308 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL);
4978 /* fall through */ 5309 /* fall through */
4979 5310
4982 5313
4983 if (SCHEME_V->tok == TOK_RPAREN) 5314 if (SCHEME_V->tok == TOK_RPAREN)
4984 s_return (NIL); 5315 s_return (NIL);
4985 else if (SCHEME_V->tok == TOK_DOT) 5316 else if (SCHEME_V->tok == TOK_DOT)
4986 Error_0 ("syntax error: illegal dot expression"); 5317 Error_0 ("syntax error: illegal dot expression");
4987 else 5318
4988 {
4989 SCHEME_V->nesting_stack[SCHEME_V->file_i]++; 5319 SCHEME_V->nesting_stack[SCHEME_V->file_i]++;
4990 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL); 5320 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL);
4991 s_goto (OP_RDSEXPR); 5321 s_goto (OP_RDSEXPR);
4992 }
4993 5322
4994 case TOK_QUOTE: 5323 case TOK_QUOTE:
4995 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL); 5324 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL);
4996 SCHEME_V->tok = token (SCHEME_A); 5325 SCHEME_V->tok = token (SCHEME_A);
4997 s_goto (OP_RDSEXPR); 5326 s_goto (OP_RDSEXPR);
5003 { 5332 {
5004 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); 5333 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL);
5005 SCHEME_V->tok = TOK_LPAREN; 5334 SCHEME_V->tok = TOK_LPAREN;
5006 s_goto (OP_RDSEXPR); 5335 s_goto (OP_RDSEXPR);
5007 } 5336 }
5008 else 5337
5009 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); 5338 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL);
5010
5011 s_goto (OP_RDSEXPR); 5339 s_goto (OP_RDSEXPR);
5012 5340
5013 case TOK_COMMA: 5341 case TOK_COMMA:
5014 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); 5342 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL);
5015 SCHEME_V->tok = token (SCHEME_A); 5343 SCHEME_V->tok = token (SCHEME_A);
5026 case TOK_DOTATOM: 5354 case TOK_DOTATOM:
5027 SCHEME_V->strbuff[0] = '.'; 5355 SCHEME_V->strbuff[0] = '.';
5028 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); 5356 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5029 5357
5030 case TOK_STRATOM: 5358 case TOK_STRATOM:
5359 //TODO: haven't checked whether the garbage collector could interfere and free x
5360 gc (SCHEME_A_ NIL, NIL); //TODO: superheavyhanded
5031 x = readstrexp (SCHEME_A_ '|'); 5361 x = readstrexp (SCHEME_A_ '|');
5032 //TODO: haven't checked whether the garbage collector could interfere
5033 s_return (mk_atom (SCHEME_A_ strvalue (x))); 5362 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5034 5363
5035 case TOK_DQUOTE: 5364 case TOK_DQUOTE:
5036 x = readstrexp (SCHEME_A_ '"'); 5365 x = readstrexp (SCHEME_A_ '"');
5037 5366
5045 { 5374 {
5046 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1); 5375 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1);
5047 5376
5048 if (f == NIL) 5377 if (f == NIL)
5049 Error_0 ("undefined sharp expression"); 5378 Error_0 ("undefined sharp expression");
5050 else 5379
5051 {
5052 SCHEME_V->code = cons (slot_value_in_env (f), NIL); 5380 SCHEME_V->code = cons (slot_value_in_env (f), NIL);
5053 s_goto (OP_EVAL); 5381 s_goto (OP_EVAL);
5054 }
5055 } 5382 }
5056 5383
5057 case TOK_SHARP_CONST: 5384 case TOK_SHARP_CONST:
5058 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) 5385 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5059 Error_0 ("undefined sharp expression"); 5386 Error_0 ("undefined sharp expression");
5060 else 5387
5061 s_return (x); 5388 s_return (x);
5062 5389
5063 default: 5390 default:
5064 Error_0 ("syntax error: illegal token"); 5391 Error_0 ("syntax error: illegal token");
5065 } 5392 }
5066 5393
5159 pointer b = cdr (args); 5486 pointer b = cdr (args);
5160 int ok_abbr = ok_abbrev (b); 5487 int ok_abbr = ok_abbrev (b);
5161 SCHEME_V->args = car (b); 5488 SCHEME_V->args = car (b);
5162 5489
5163 if (a == SCHEME_V->QUOTE && ok_abbr) 5490 if (a == SCHEME_V->QUOTE && ok_abbr)
5164 putstr (SCHEME_A_ "'"); 5491 putcharacter (SCHEME_A_ '\'');
5165 else if (a == SCHEME_V->QQUOTE && ok_abbr) 5492 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5166 putstr (SCHEME_A_ "`"); 5493 putcharacter (SCHEME_A_ '`');
5167 else if (a == SCHEME_V->UNQUOTE && ok_abbr) 5494 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5168 putstr (SCHEME_A_ ","); 5495 putcharacter (SCHEME_A_ ',');
5169 else if (a == SCHEME_V->UNQUOTESP && ok_abbr) 5496 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5170 putstr (SCHEME_A_ ",@"); 5497 putstr (SCHEME_A_ ",@");
5171 else 5498 else
5172 { 5499 {
5173 putstr (SCHEME_A_ "("); 5500 putcharacter (SCHEME_A_ '(');
5174 s_save (SCHEME_A_ OP_P1LIST, b, NIL); 5501 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5175 SCHEME_V->args = a; 5502 SCHEME_V->args = a;
5176 } 5503 }
5177 5504
5178 s_goto (OP_P0LIST); 5505 s_goto (OP_P0LIST);
5180 5507
5181 case OP_P1LIST: 5508 case OP_P1LIST:
5182 if (is_pair (args)) 5509 if (is_pair (args))
5183 { 5510 {
5184 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); 5511 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5185 putstr (SCHEME_A_ " "); 5512 putcharacter (SCHEME_A_ ' ');
5186 SCHEME_V->args = car (args); 5513 SCHEME_V->args = car (args);
5187 s_goto (OP_P0LIST); 5514 s_goto (OP_P0LIST);
5188 } 5515 }
5189 else if (is_vector (args)) 5516 else if (is_vector (args))
5190 { 5517 {
5198 { 5525 {
5199 putstr (SCHEME_A_ " . "); 5526 putstr (SCHEME_A_ " . ");
5200 printatom (SCHEME_A_ args, SCHEME_V->print_flag); 5527 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5201 } 5528 }
5202 5529
5203 putstr (SCHEME_A_ ")"); 5530 putcharacter (SCHEME_A_ ')');
5204 s_return (S_T); 5531 s_return (S_T);
5205 } 5532 }
5206 5533
5207 case OP_PVECFROM: 5534 case OP_PVECFROM:
5208 { 5535 {
5210 pointer vec = car (args); 5537 pointer vec = car (args);
5211 int len = veclength (vec); 5538 int len = veclength (vec);
5212 5539
5213 if (i == len) 5540 if (i == len)
5214 { 5541 {
5215 putstr (SCHEME_A_ ")"); 5542 putcharacter (SCHEME_A_ ')');
5216 s_return (S_T); 5543 s_return (S_T);
5217 } 5544 }
5218 else 5545 else
5219 { 5546 {
5220 pointer elem = vector_get (vec, i); 5547 pointer elem = vector_get (vec, i);
5222 ivalue_unchecked (cdr (args)) = i + 1; 5549 ivalue_unchecked (cdr (args)) = i + 1;
5223 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5550 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5224 SCHEME_V->args = elem; 5551 SCHEME_V->args = elem;
5225 5552
5226 if (i > 0) 5553 if (i > 0)
5227 putstr (SCHEME_A_ " "); 5554 putcharacter (SCHEME_A_ ' ');
5228 5555
5229 s_goto (OP_P0LIST); 5556 s_goto (OP_P0LIST);
5230 } 5557 }
5231 } 5558 }
5232 } 5559 }
5233 5560
5234 if (USE_ERROR_CHECKING) abort (); 5561 if (USE_ERROR_CHECKING) abort ();
5235} 5562}
5236 5563
5237static int 5564/* list ops */
5565ecb_hot static int
5238opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5566opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5239{ 5567{
5240 pointer args = SCHEME_V->args; 5568 pointer args = SCHEME_V->args;
5241 pointer a = car (args); 5569 pointer a = car (args);
5242 pointer x, y; 5570 pointer x, y;
5265 break; 5593 break;
5266 } 5594 }
5267 5595
5268 if (is_pair (y)) 5596 if (is_pair (y))
5269 s_return (car (y)); 5597 s_return (car (y));
5270 else 5598
5271 s_return (S_F); 5599 s_return (S_F);
5272
5273 5600
5274 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5601 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5275 SCHEME_V->args = a; 5602 SCHEME_V->args = a;
5276 5603
5277 if (SCHEME_V->args == NIL) 5604 if (SCHEME_V->args == NIL)
5278 s_return (S_F); 5605 s_return (S_F);
5279 else if (is_closure (SCHEME_V->args)) 5606 else if (is_closure (SCHEME_V->args) || is_macro (SCHEME_V->args))
5280 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5607 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5281 else if (is_macro (SCHEME_V->args)) 5608
5282 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5283 else
5284 s_return (S_F); 5609 s_return (S_F);
5285 5610
5286 case OP_CLOSUREP: /* closure? */ 5611 case OP_CLOSUREP: /* closure? */
5287 /* 5612 /*
5288 * Note, macro object is also a closure. 5613 * Note, macro object is also a closure.
5289 * Therefore, (closure? <#MACRO>) ==> #t 5614 * Therefore, (closure? <#MACRO>) ==> #t
5300 5625
5301/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5626/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5302typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); 5627typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5303 5628
5304typedef int (*test_predicate)(pointer); 5629typedef int (*test_predicate)(pointer);
5305static int 5630
5631ecb_hot static int
5306tst_any (pointer p) 5632tst_any (pointer p)
5307{ 5633{
5308 return 1; 5634 return 1;
5309} 5635}
5310 5636
5311static int 5637ecb_hot static int
5312tst_inonneg (pointer p) 5638tst_inonneg (pointer p)
5313{ 5639{
5314 return is_integer (p) && ivalue_unchecked (p) >= 0; 5640 return is_integer (p) && ivalue_unchecked (p) >= 0;
5315} 5641}
5316 5642
5317static int 5643ecb_hot static int
5318tst_is_list (SCHEME_P_ pointer p) 5644tst_is_list (SCHEME_P_ pointer p)
5319{ 5645{
5320 return p == NIL || is_pair (p); 5646 return p == NIL || is_pair (p);
5321} 5647}
5322 5648
5365#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" 5691#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5366#include "opdefines.h" 5692#include "opdefines.h"
5367#undef OP_DEF 5693#undef OP_DEF
5368; 5694;
5369 5695
5370static const char * 5696ecb_cold static const char *
5371opname (int idx) 5697opname (int idx)
5372{ 5698{
5373 const char *name = opnames; 5699 const char *name = opnames;
5374 5700
5375 /* should do this at compile time, but would require external program, right? */ 5701 /* should do this at compile time, but would require external program, right? */
5377 name += strlen (name) + 1; 5703 name += strlen (name) + 1;
5378 5704
5379 return *name ? name : "ILLEGAL"; 5705 return *name ? name : "ILLEGAL";
5380} 5706}
5381 5707
5382static const char * 5708ecb_cold static const char *
5383procname (pointer x) 5709procname (pointer x)
5384{ 5710{
5385 return opname (procnum (x)); 5711 return opname (procnum (x));
5386} 5712}
5387 5713
5407#undef OP_DEF 5733#undef OP_DEF
5408 {0} 5734 {0}
5409}; 5735};
5410 5736
5411/* kernel of this interpreter */ 5737/* kernel of this interpreter */
5412static void ecb_hot 5738ecb_hot static void
5413Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5739Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5414{ 5740{
5415 SCHEME_V->op = op; 5741 SCHEME_V->op = op;
5416 5742
5417 for (;;) 5743 for (;;)
5508 } 5834 }
5509} 5835}
5510 5836
5511/* ========== Initialization of internal keywords ========== */ 5837/* ========== Initialization of internal keywords ========== */
5512 5838
5513static void 5839ecb_cold static void
5514assign_syntax (SCHEME_P_ const char *name) 5840assign_syntax (SCHEME_P_ const char *name)
5515{ 5841{
5516 pointer x = oblist_add_by_name (SCHEME_A_ name); 5842 pointer x = oblist_add_by_name (SCHEME_A_ name);
5517 set_typeflag (x, typeflag (x) | T_SYNTAX); 5843 set_typeflag (x, typeflag (x) | T_SYNTAX);
5518} 5844}
5519 5845
5520static void 5846ecb_cold static void
5521assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) 5847assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
5522{ 5848{
5523 pointer x = mk_symbol (SCHEME_A_ name); 5849 pointer x = mk_symbol (SCHEME_A_ name);
5524 pointer y = mk_proc (SCHEME_A_ op); 5850 pointer y = mk_proc (SCHEME_A_ op);
5525 new_slot_in_env (SCHEME_A_ x, y); 5851 new_slot_in_env (SCHEME_A_ x, y);
5533 ivalue_unchecked (y) = op; 5859 ivalue_unchecked (y) = op;
5534 return y; 5860 return y;
5535} 5861}
5536 5862
5537/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5863/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5538static int 5864ecb_hot static int
5539syntaxnum (pointer p) 5865syntaxnum (pointer p)
5540{ 5866{
5541 const char *s = strvalue (p); 5867 const char *s = strvalue (p);
5542 5868
5543 switch (strlength (p)) 5869 switch (strlength (p))
5660#endif 5986#endif
5661 } 5987 }
5662 5988
5663 SCHEME_V->gc_verbose = 0; 5989 SCHEME_V->gc_verbose = 0;
5664 dump_stack_initialize (SCHEME_A); 5990 dump_stack_initialize (SCHEME_A);
5665 SCHEME_V->code = NIL; 5991 SCHEME_V->code = NIL;
5666 SCHEME_V->args = NIL; 5992 SCHEME_V->args = NIL;
5667 SCHEME_V->envir = NIL; 5993 SCHEME_V->envir = NIL;
5994 SCHEME_V->value = NIL;
5668 SCHEME_V->tracing = 0; 5995 SCHEME_V->tracing = 0;
5669 5996
5670 /* init NIL */ 5997 /* init NIL */
5671 set_typeflag (NIL, T_ATOM | T_MARK); 5998 set_typeflag (NIL, T_ATOM | T_MARK);
5672 set_car (NIL, NIL); 5999 set_car (NIL, NIL);
5728 6055
5729 return !SCHEME_V->no_memory; 6056 return !SCHEME_V->no_memory;
5730} 6057}
5731 6058
5732#if USE_PORTS 6059#if USE_PORTS
5733void 6060ecb_cold void
5734scheme_set_input_port_file (SCHEME_P_ int fin) 6061scheme_set_input_port_file (SCHEME_P_ int fin)
5735{ 6062{
5736 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); 6063 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input);
5737} 6064}
5738 6065
5739void 6066ecb_cold void
5740scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) 6067scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end)
5741{ 6068{
5742 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); 6069 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input);
5743} 6070}
5744 6071
5745void 6072ecb_cold void
5746scheme_set_output_port_file (SCHEME_P_ int fout) 6073scheme_set_output_port_file (SCHEME_P_ int fout)
5747{ 6074{
5748 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); 6075 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output);
5749} 6076}
5750 6077
5751void 6078ecb_cold void
5752scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) 6079scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end)
5753{ 6080{
5754 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); 6081 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output);
5755} 6082}
5756#endif 6083#endif
5757 6084
5758void 6085ecb_cold void
5759scheme_set_external_data (SCHEME_P_ void *p) 6086scheme_set_external_data (SCHEME_P_ void *p)
5760{ 6087{
5761 SCHEME_V->ext_data = p; 6088 SCHEME_V->ext_data = p;
5762} 6089}
5763 6090
5795 SCHEME_V->loadport = NIL; 6122 SCHEME_V->loadport = NIL;
5796 SCHEME_V->gc_verbose = 0; 6123 SCHEME_V->gc_verbose = 0;
5797 gc (SCHEME_A_ NIL, NIL); 6124 gc (SCHEME_A_ NIL, NIL);
5798 6125
5799 for (i = 0; i <= SCHEME_V->last_cell_seg; i++) 6126 for (i = 0; i <= SCHEME_V->last_cell_seg; i++)
5800 free (SCHEME_V->alloc_seg[i]); 6127 free (SCHEME_V->cell_seg[i]);
5801 6128
5802#if SHOW_ERROR_LINE 6129#if SHOW_ERROR_LINE
5803 for (i = 0; i <= SCHEME_V->file_i; i++) 6130 for (i = 0; i <= SCHEME_V->file_i; i++)
5804 {
5805 if (SCHEME_V->load_stack[i].kind & port_file) 6131 if (SCHEME_V->load_stack[i].kind & port_file)
5806 { 6132 {
5807 fname = SCHEME_V->load_stack[i].rep.stdio.filename; 6133 fname = SCHEME_V->load_stack[i].rep.stdio.filename;
5808 6134
5809 if (fname) 6135 if (fname)
5810 free (fname); 6136 free (fname);
5811 } 6137 }
5812 }
5813#endif 6138#endif
5814} 6139}
5815 6140
5816void 6141ecb_cold void
5817scheme_load_file (SCHEME_P_ int fin) 6142scheme_load_file (SCHEME_P_ int fin)
5818{ 6143{
5819 scheme_load_named_file (SCHEME_A_ fin, 0); 6144 scheme_load_named_file (SCHEME_A_ fin, 0);
5820} 6145}
5821 6146
5822void 6147ecb_cold void
5823scheme_load_named_file (SCHEME_P_ int fin, const char *filename) 6148scheme_load_named_file (SCHEME_P_ int fin, const char *filename)
5824{ 6149{
5825 dump_stack_reset (SCHEME_A); 6150 dump_stack_reset (SCHEME_A);
5826 SCHEME_V->envir = SCHEME_V->global_env; 6151 SCHEME_V->envir = SCHEME_V->global_env;
5827 SCHEME_V->file_i = 0; 6152 SCHEME_V->file_i = 0;
5828 SCHEME_V->load_stack[0].unget = -1; 6153 SCHEME_V->load_stack[0].unget = -1;
5829 SCHEME_V->load_stack[0].kind = port_input | port_file; 6154 SCHEME_V->load_stack[0].kind = port_input | port_file;
5830 SCHEME_V->load_stack[0].rep.stdio.file = fin; 6155 SCHEME_V->load_stack[0].rep.stdio.file = fin;
5831#if USE_PORTS
5832 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6156 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5833#endif
5834 SCHEME_V->retcode = 0; 6157 SCHEME_V->retcode = 0;
5835 6158
5836#if USE_PORTS
5837 if (fin == STDIN_FILENO) 6159 if (fin == STDIN_FILENO)
5838 SCHEME_V->interactive_repl = 1; 6160 SCHEME_V->interactive_repl = 1;
5839#endif
5840 6161
5841#if USE_PORTS 6162#if USE_PORTS
5842#if SHOW_ERROR_LINE 6163#if SHOW_ERROR_LINE
5843 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; 6164 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0;
5844 6165
5848#endif 6169#endif
5849 6170
5850 SCHEME_V->inport = SCHEME_V->loadport; 6171 SCHEME_V->inport = SCHEME_V->loadport;
5851 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6172 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5852 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6173 Eval_Cycle (SCHEME_A_ OP_T0LVL);
6174
5853 set_typeflag (SCHEME_V->loadport, T_ATOM); 6175 set_typeflag (SCHEME_V->loadport, T_ATOM);
5854 6176
5855 if (SCHEME_V->retcode == 0) 6177 if (SCHEME_V->retcode == 0)
5856 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6178 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5857} 6179}
5858 6180
5859void 6181ecb_cold void
5860scheme_load_string (SCHEME_P_ const char *cmd) 6182scheme_load_string (SCHEME_P_ const char *cmd)
5861{ 6183{
6184#if USE_PORTs
5862 dump_stack_reset (SCHEME_A); 6185 dump_stack_reset (SCHEME_A);
5863 SCHEME_V->envir = SCHEME_V->global_env; 6186 SCHEME_V->envir = SCHEME_V->global_env;
5864 SCHEME_V->file_i = 0; 6187 SCHEME_V->file_i = 0;
5865 SCHEME_V->load_stack[0].kind = port_input | port_string; 6188 SCHEME_V->load_stack[0].kind = port_input | port_string;
5866 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ 6189 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */
5867 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); 6190 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd);
5868 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; 6191 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
5869#if USE_PORTS
5870 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6192 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5871#endif
5872 SCHEME_V->retcode = 0; 6193 SCHEME_V->retcode = 0;
5873 SCHEME_V->interactive_repl = 0; 6194 SCHEME_V->interactive_repl = 0;
5874 SCHEME_V->inport = SCHEME_V->loadport; 6195 SCHEME_V->inport = SCHEME_V->loadport;
5875 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6196 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5876 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6197 Eval_Cycle (SCHEME_A_ OP_T0LVL);
5877 set_typeflag (SCHEME_V->loadport, T_ATOM); 6198 set_typeflag (SCHEME_V->loadport, T_ATOM);
5878 6199
5879 if (SCHEME_V->retcode == 0) 6200 if (SCHEME_V->retcode == 0)
5880 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6201 SCHEME_V->retcode = SCHEME_V->nesting != 0;
6202#else
6203 abort ();
6204#endif
5881} 6205}
5882 6206
5883void 6207ecb_cold void
5884scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) 6208scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value)
5885{ 6209{
5886 pointer x; 6210 pointer x;
5887 6211
5888 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 6212 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
5893 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 6217 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
5894} 6218}
5895 6219
5896#if !STANDALONE 6220#if !STANDALONE
5897 6221
5898void 6222ecb_cold void
5899scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 6223scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
5900{ 6224{
5901 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); 6225 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f));
5902} 6226}
5903 6227
5904void 6228ecb_cold void
5905scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 6229scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
5906{ 6230{
5907 int i; 6231 int i;
5908 6232
5909 for (i = 0; i < count; i++) 6233 for (i = 0; i < count; i++)
5910 scheme_register_foreign_func (SCHEME_A_ list + i); 6234 scheme_register_foreign_func (SCHEME_A_ list + i);
5911} 6235}
5912 6236
5913pointer 6237ecb_cold pointer
5914scheme_apply0 (SCHEME_P_ const char *procname) 6238scheme_apply0 (SCHEME_P_ const char *procname)
5915{ 6239{
5916 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); 6240 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL));
5917} 6241}
5918 6242
5919void 6243ecb_cold void
5920save_from_C_call (SCHEME_P) 6244save_from_C_call (SCHEME_P)
5921{ 6245{
5922 pointer saved_data = cons (car (S_SINK), 6246 pointer saved_data = cons (car (S_SINK),
5923 cons (SCHEME_V->envir, 6247 cons (SCHEME_V->envir,
5924 SCHEME_V->dump)); 6248 SCHEME_V->dump));
5928 /* Truncate the dump stack so TS will return here when done, not 6252 /* Truncate the dump stack so TS will return here when done, not
5929 directly resume pre-C-call operations. */ 6253 directly resume pre-C-call operations. */
5930 dump_stack_reset (SCHEME_A); 6254 dump_stack_reset (SCHEME_A);
5931} 6255}
5932 6256
5933void 6257ecb_cold void
5934restore_from_C_call (SCHEME_P) 6258restore_from_C_call (SCHEME_P)
5935{ 6259{
5936 set_car (S_SINK, caar (SCHEME_V->c_nest)); 6260 set_car (S_SINK, caar (SCHEME_V->c_nest));
5937 SCHEME_V->envir = cadar (SCHEME_V->c_nest); 6261 SCHEME_V->envir = cadar (SCHEME_V->c_nest);
5938 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); 6262 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest));
5939 /* Pop */ 6263 /* Pop */
5940 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); 6264 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest);
5941} 6265}
5942 6266
5943/* "func" and "args" are assumed to be already eval'ed. */ 6267/* "func" and "args" are assumed to be already eval'ed. */
5944pointer 6268ecb_cold pointer
5945scheme_call (SCHEME_P_ pointer func, pointer args) 6269scheme_call (SCHEME_P_ pointer func, pointer args)
5946{ 6270{
5947 int old_repl = SCHEME_V->interactive_repl; 6271 int old_repl = SCHEME_V->interactive_repl;
5948 6272
5949 SCHEME_V->interactive_repl = 0; 6273 SCHEME_V->interactive_repl = 0;
5956 SCHEME_V->interactive_repl = old_repl; 6280 SCHEME_V->interactive_repl = old_repl;
5957 restore_from_C_call (SCHEME_A); 6281 restore_from_C_call (SCHEME_A);
5958 return SCHEME_V->value; 6282 return SCHEME_V->value;
5959} 6283}
5960 6284
5961pointer 6285ecb_cold pointer
5962scheme_eval (SCHEME_P_ pointer obj) 6286scheme_eval (SCHEME_P_ pointer obj)
5963{ 6287{
5964 int old_repl = SCHEME_V->interactive_repl; 6288 int old_repl = SCHEME_V->interactive_repl;
5965 6289
5966 SCHEME_V->interactive_repl = 0; 6290 SCHEME_V->interactive_repl = 0;
5978 6302
5979/* ========== Main ========== */ 6303/* ========== Main ========== */
5980 6304
5981#if STANDALONE 6305#if STANDALONE
5982 6306
5983int 6307ecb_cold int
5984main (int argc, char **argv) 6308main (int argc, char **argv)
5985{ 6309{
5986# if USE_MULTIPLICITY 6310# if USE_MULTIPLICITY
5987 scheme ssc; 6311 scheme ssc;
5988 scheme *const SCHEME_V = &ssc; 6312 scheme *const SCHEME_V = &ssc;
5990# endif 6314# endif
5991 int fin; 6315 int fin;
5992 char *file_name = InitFile; 6316 char *file_name = InitFile;
5993 int retcode; 6317 int retcode;
5994 int isfile = 1; 6318 int isfile = 1;
6319#if EXPERIMENT
5995 system ("ps v $PPID");//D 6320 system ("ps v $PPID");
6321#endif
5996 6322
5997 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6323 if (argc == 2 && strcmp (argv[1], "-?") == 0)
5998 { 6324 {
5999 putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); 6325 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
6000 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); 6326 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
6029 } 6355 }
6030#endif 6356#endif
6031 6357
6032 do 6358 do
6033 { 6359 {
6034#if USE_PORTS
6035 if (strcmp (file_name, "-") == 0) 6360 if (strcmp (file_name, "-") == 0)
6036 fin = STDIN_FILENO; 6361 fin = STDIN_FILENO;
6037 else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) 6362 else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0)
6038 { 6363 {
6039 pointer args = NIL; 6364 pointer args = NIL;
6057 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); 6382 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args);
6058 6383
6059 } 6384 }
6060 else 6385 else
6061 fin = open (file_name, O_RDONLY); 6386 fin = open (file_name, O_RDONLY);
6062#endif
6063 6387
6064 if (isfile && fin < 0) 6388 if (isfile && fin < 0)
6065 { 6389 {
6066 putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); 6390 putstr (SCHEME_A_ "Could not open file ");
6391 putstr (SCHEME_A_ file_name);
6392 putcharacter (SCHEME_A_ '\n');
6067 } 6393 }
6068 else 6394 else
6069 { 6395 {
6070 if (isfile) 6396 if (isfile)
6071 scheme_load_named_file (SCHEME_A_ fin, file_name); 6397 scheme_load_named_file (SCHEME_A_ fin, file_name);
6072 else 6398 else
6073 scheme_load_string (SCHEME_A_ file_name); 6399 scheme_load_string (SCHEME_A_ file_name);
6074 6400
6075#if USE_PORTS
6076 if (!isfile || fin != STDIN_FILENO) 6401 if (!isfile || fin != STDIN_FILENO)
6077 { 6402 {
6078 if (SCHEME_V->retcode != 0) 6403 if (SCHEME_V->retcode != 0)
6079 { 6404 {
6080 putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); 6405 putstr (SCHEME_A_ "Errors encountered reading ");
6406 putstr (SCHEME_A_ file_name);
6407 putcharacter (SCHEME_A_ '\n');
6081 } 6408 }
6082 6409
6083 if (isfile) 6410 if (isfile)
6084 close (fin); 6411 close (fin);
6085 } 6412 }
6086#endif
6087 } 6413 }
6088 6414
6089 file_name = *argv++; 6415 file_name = *argv++;
6090 } 6416 }
6091 while (file_name != 0); 6417 while (file_name != 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines