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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines