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.58 by root, Tue Dec 1 05:12:33 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"))
1529 } 1550 }
1530} 1551}
1531 1552
1532/* ========== garbage collector ========== */ 1553/* ========== garbage collector ========== */
1533 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}
1573
1534/*-- 1574/*--
1535 * 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,
1536 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1576 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1537 * for marking. 1577 * for marking.
1538 * 1578 *
1539 * The exception is vectors - vectors are currently marked recursively, 1579 * The exception is vectors - vectors are currently marked recursively,
1540 * 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
1541 * word of context in the vector 1581 * word of context in the vector
1542 */ 1582 */
1543static void 1583ecb_hot static void
1544mark (pointer a) 1584mark (pointer a)
1545{ 1585{
1546 pointer t, q, p; 1586 pointer t, q, p;
1547 1587
1548 t = 0; 1588 t = 0;
1605 p = q; 1645 p = q;
1606 goto E6; 1646 goto E6;
1607 } 1647 }
1608} 1648}
1609 1649
1610/* garbage collection. parameter a, b is marked. */ 1650ecb_hot static void
1611static void 1651gc_free (SCHEME_P)
1612gc (SCHEME_P_ pointer a, pointer b)
1613{ 1652{
1614 int i; 1653 int i;
1615
1616 if (SCHEME_V->gc_verbose)
1617 putstr (SCHEME_A_ "gc...");
1618
1619 /* mark system globals */
1620 mark (SCHEME_V->oblist);
1621 mark (SCHEME_V->global_env);
1622
1623 /* mark current registers */
1624 mark (SCHEME_V->args);
1625 mark (SCHEME_V->envir);
1626 mark (SCHEME_V->code);
1627 dump_stack_mark (SCHEME_A);
1628 mark (SCHEME_V->value);
1629 mark (SCHEME_V->inport);
1630 mark (SCHEME_V->save_inport);
1631 mark (SCHEME_V->outport);
1632 mark (SCHEME_V->loadport);
1633
1634 /* Mark recent objects the interpreter doesn't know about yet. */
1635 mark (car (S_SINK));
1636 /* Mark any older stuff above nested C calls */
1637 mark (SCHEME_V->c_nest);
1638
1639#if USE_INTCACHE
1640 /* mark intcache */
1641 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1642 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1643 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1644#endif
1645
1646 /* mark variables a, b */
1647 mark (a);
1648 mark (b);
1649
1650 /* garbage collect */
1651 clrmark (NIL);
1652 SCHEME_V->fcells = 0;
1653 SCHEME_V->free_cell = NIL;
1654
1655 if (SCHEME_V->gc_verbose)
1656 putstr (SCHEME_A_ "freeing...");
1657
1658 uint32_t total = 0; 1654 uint32_t total = 0;
1659 1655
1660 /* Here we scan the cells to build the free-list. */ 1656 /* Here we scan the cells to build the free-list. */
1661 for (i = SCHEME_V->last_cell_seg; i >= 0; i--) 1657 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1662 { 1658 {
1691 { 1687 {
1692 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");
1693 } 1689 }
1694} 1690}
1695 1691
1696static void 1692/* garbage collection. parameter a, b is marked. */
1697finalize_cell (SCHEME_P_ pointer a) 1693ecb_cold static void
1694gc (SCHEME_P_ pointer a, pointer b)
1698{ 1695{
1699 /* TODO, fast bitmap check? */ 1696 int i;
1700 if (is_string (a) || is_symbol (a))
1701 free (strvalue (a));
1702 else if (is_vector (a))
1703 free (vecvalue (a));
1704#if USE_PORTS
1705 else if (is_port (a))
1706 {
1707 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit)
1708 port_close (SCHEME_A_ a, port_input | port_output);
1709 1697
1710 free (port (a)); 1698 if (SCHEME_V->gc_verbose)
1711 } 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]);
1712#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);
1713} 1741}
1714 1742
1715/* ========== Routines for Reading ========== */ 1743/* ========== Routines for Reading ========== */
1716 1744
1717static int 1745ecb_cold static int
1718file_push (SCHEME_P_ const char *fname) 1746file_push (SCHEME_P_ const char *fname)
1719{ 1747{
1720#if USE_PORTS
1721 int fin; 1748 int fin;
1722 1749
1723 if (SCHEME_V->file_i == MAXFIL - 1) 1750 if (SCHEME_V->file_i == MAXFIL - 1)
1724 return 0; 1751 return 0;
1725 1752
1742 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);
1743#endif 1770#endif
1744 } 1771 }
1745 1772
1746 return fin >= 0; 1773 return fin >= 0;
1747
1748#else
1749 return 1;
1750#endif
1751} 1774}
1752 1775
1753static void 1776ecb_cold static void
1754file_pop (SCHEME_P) 1777file_pop (SCHEME_P)
1755{ 1778{
1756 if (SCHEME_V->file_i != 0) 1779 if (SCHEME_V->file_i != 0)
1757 { 1780 {
1758 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1781 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1762 SCHEME_V->file_i--; 1785 SCHEME_V->file_i--;
1763 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);
1764 } 1787 }
1765} 1788}
1766 1789
1767static int 1790ecb_cold static int
1768file_interactive (SCHEME_P) 1791file_interactive (SCHEME_P)
1769{ 1792{
1770#if USE_PORTS 1793#if USE_PORTS
1771 return SCHEME_V->file_i == 0 1794 return SCHEME_V->file_i == 0
1772 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1795 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1775 return 0; 1798 return 0;
1776#endif 1799#endif
1777} 1800}
1778 1801
1779#if USE_PORTS 1802#if USE_PORTS
1780static port * 1803ecb_cold static port *
1781port_rep_from_filename (SCHEME_P_ const char *fn, int prop) 1804port_rep_from_filename (SCHEME_P_ const char *fn, int prop)
1782{ 1805{
1783 int fd; 1806 int fd;
1784 int flags; 1807 int flags;
1785 char *rw; 1808 char *rw;
1808# endif 1831# endif
1809 1832
1810 return pt; 1833 return pt;
1811} 1834}
1812 1835
1813static pointer 1836ecb_cold static pointer
1814port_from_filename (SCHEME_P_ const char *fn, int prop) 1837port_from_filename (SCHEME_P_ const char *fn, int prop)
1815{ 1838{
1816 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); 1839 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop);
1817 1840
1818 if (!pt && USE_ERROR_CHECKING) 1841 if (!pt && USE_ERROR_CHECKING)
1819 return NIL; 1842 return NIL;
1820 1843
1821 return mk_port (SCHEME_A_ pt); 1844 return mk_port (SCHEME_A_ pt);
1822} 1845}
1823 1846
1824static port * 1847ecb_cold static port *
1825port_rep_from_file (SCHEME_P_ int f, int prop) 1848port_rep_from_file (SCHEME_P_ int f, int prop)
1826{ 1849{
1827 port *pt = malloc (sizeof *pt); 1850 port *pt = malloc (sizeof *pt);
1828 1851
1829 if (!pt && USE_ERROR_CHECKING) 1852 if (!pt && USE_ERROR_CHECKING)
1834 pt->rep.stdio.file = f; 1857 pt->rep.stdio.file = f;
1835 pt->rep.stdio.closeit = 0; 1858 pt->rep.stdio.closeit = 0;
1836 return pt; 1859 return pt;
1837} 1860}
1838 1861
1839static pointer 1862ecb_cold static pointer
1840port_from_file (SCHEME_P_ int f, int prop) 1863port_from_file (SCHEME_P_ int f, int prop)
1841{ 1864{
1842 port *pt = port_rep_from_file (SCHEME_A_ f, prop); 1865 port *pt = port_rep_from_file (SCHEME_A_ f, prop);
1843 1866
1844 if (!pt && USE_ERROR_CHECKING) 1867 if (!pt && USE_ERROR_CHECKING)
1845 return NIL; 1868 return NIL;
1846 1869
1847 return mk_port (SCHEME_A_ pt); 1870 return mk_port (SCHEME_A_ pt);
1848} 1871}
1849 1872
1850static port * 1873ecb_cold static port *
1851port_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)
1852{ 1875{
1853 port *pt = malloc (sizeof (port)); 1876 port *pt = malloc (sizeof (port));
1854 1877
1855 if (!pt && USE_ERROR_CHECKING) 1878 if (!pt && USE_ERROR_CHECKING)
1861 pt->rep.string.curr = start; 1884 pt->rep.string.curr = start;
1862 pt->rep.string.past_the_end = past_the_end; 1885 pt->rep.string.past_the_end = past_the_end;
1863 return pt; 1886 return pt;
1864} 1887}
1865 1888
1866static pointer 1889ecb_cold static pointer
1867port_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)
1868{ 1891{
1869 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);
1870 1893
1871 if (!pt && USE_ERROR_CHECKING) 1894 if (!pt && USE_ERROR_CHECKING)
1874 return mk_port (SCHEME_A_ pt); 1897 return mk_port (SCHEME_A_ pt);
1875} 1898}
1876 1899
1877# define BLOCK_SIZE 256 1900# define BLOCK_SIZE 256
1878 1901
1879static port * 1902ecb_cold static port *
1880port_rep_from_scratch (SCHEME_P) 1903port_rep_from_scratch (SCHEME_P)
1881{ 1904{
1882 char *start; 1905 char *start;
1883 port *pt = malloc (sizeof (port)); 1906 port *pt = malloc (sizeof (port));
1884 1907
1898 pt->rep.string.curr = start; 1921 pt->rep.string.curr = start;
1899 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; 1922 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1;
1900 return pt; 1923 return pt;
1901} 1924}
1902 1925
1903static pointer 1926ecb_cold static pointer
1904port_from_scratch (SCHEME_P) 1927port_from_scratch (SCHEME_P)
1905{ 1928{
1906 port *pt = port_rep_from_scratch (SCHEME_A); 1929 port *pt = port_rep_from_scratch (SCHEME_A);
1907 1930
1908 if (!pt && USE_ERROR_CHECKING) 1931 if (!pt && USE_ERROR_CHECKING)
1909 return NIL; 1932 return NIL;
1910 1933
1911 return mk_port (SCHEME_A_ pt); 1934 return mk_port (SCHEME_A_ pt);
1912} 1935}
1913 1936
1914static void 1937ecb_cold static void
1915port_close (SCHEME_P_ pointer p, int flag) 1938port_close (SCHEME_P_ pointer p, int flag)
1916{ 1939{
1917 port *pt = port (p); 1940 port *pt = port (p);
1918 1941
1919 pt->kind &= ~flag; 1942 pt->kind &= ~flag;
1939 } 1962 }
1940} 1963}
1941#endif 1964#endif
1942 1965
1943/* get new character from input file */ 1966/* get new character from input file */
1944static int 1967ecb_cold static int
1945inchar (SCHEME_P) 1968inchar (SCHEME_P)
1946{ 1969{
1947 int c; 1970 int c;
1948 port *pt = port (SCHEME_V->inport); 1971 port *pt = port (SCHEME_V->inport);
1949 1972
1963 } 1986 }
1964 1987
1965 return c; 1988 return c;
1966} 1989}
1967 1990
1968static int ungot = -1; 1991ecb_cold static int
1969
1970static int
1971basic_inchar (port *pt) 1992basic_inchar (port *pt)
1972{ 1993{
1973#if USE_PORTS
1974 if (pt->unget != -1) 1994 if (pt->unget != -1)
1975 { 1995 {
1976 int r = pt->unget; 1996 int r = pt->unget;
1977 pt->unget = -1; 1997 pt->unget = -1;
1978 return r; 1998 return r;
1979 } 1999 }
1980 2000
2001#if USE_PORTS
1981 if (pt->kind & port_file) 2002 if (pt->kind & port_file)
1982 { 2003 {
1983 char c; 2004 char c;
1984 2005
1985 if (!read (pt->rep.stdio.file, &c, 1)) 2006 if (!read (pt->rep.stdio.file, &c, 1))
1993 return EOF; 2014 return EOF;
1994 else 2015 else
1995 return *pt->rep.string.curr++; 2016 return *pt->rep.string.curr++;
1996 } 2017 }
1997#else 2018#else
1998 if (ungot == -1)
1999 {
2000 char c; 2019 char c;
2001 if (!read (0, &c, 1)) 2020
2021 if (!read (pt->rep.stdio.file, &c, 1))
2002 return EOF; 2022 return EOF;
2003 2023
2004 ungot = c;
2005 }
2006
2007 {
2008 int r = ungot;
2009 ungot = -1;
2010 return r; 2024 return c;
2011 }
2012#endif 2025#endif
2013} 2026}
2014 2027
2015/* back character to input buffer */ 2028/* back character to input buffer */
2016static void 2029ecb_cold static void
2017backchar (SCHEME_P_ int c) 2030backchar (SCHEME_P_ int c)
2018{ 2031{
2019#if USE_PORTS 2032 port *pt = port (SCHEME_V->inport);
2020 port *pt;
2021 2033
2022 if (c == EOF) 2034 if (c == EOF)
2023 return; 2035 return;
2024 2036
2025 pt = port (SCHEME_V->inport);
2026 pt->unget = c; 2037 pt->unget = c;
2027#else
2028 if (c == EOF)
2029 return;
2030
2031 ungot = c;
2032#endif
2033} 2038}
2034 2039
2035#if USE_PORTS 2040#if USE_PORTS
2036static int 2041ecb_cold static int
2037realloc_port_string (SCHEME_P_ port *p) 2042realloc_port_string (SCHEME_P_ port *p)
2038{ 2043{
2039 char *start = p->rep.string.start; 2044 char *start = p->rep.string.start;
2040 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;
2041 char *str = malloc (new_size); 2046 char *str = malloc (new_size);
2054 else 2059 else
2055 return 0; 2060 return 0;
2056} 2061}
2057#endif 2062#endif
2058 2063
2059INTERFACE void 2064ecb_cold static void
2060putstr (SCHEME_P_ const char *s) 2065putchars (SCHEME_P_ const char *s, int len)
2061{ 2066{
2067 port *pt = port (SCHEME_V->outport);
2068
2062#if USE_PORTS 2069#if USE_PORTS
2063 port *pt = port (SCHEME_V->outport);
2064
2065 if (pt->kind & port_file)
2066 write (pt->rep.stdio.file, s, strlen (s));
2067 else
2068 for (; *s; s++)
2069 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2070 *pt->rep.string.curr++ = *s;
2071 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2072 *pt->rep.string.curr++ = *s;
2073
2074#else
2075 write (pt->rep.stdio.file, s, strlen (s));
2076#endif
2077}
2078
2079static void
2080putchars (SCHEME_P_ const char *s, int len)
2081{
2082#if USE_PORTS
2083 port *pt = port (SCHEME_V->outport);
2084
2085 if (pt->kind & port_file) 2070 if (pt->kind & port_file)
2086 write (pt->rep.stdio.file, s, len); 2071 write (pt->rep.stdio.file, s, len);
2087 else 2072 else
2088 { 2073 {
2089 for (; len; len--) 2074 for (; len; len--)
2094 *pt->rep.string.curr++ = *s++; 2079 *pt->rep.string.curr++ = *s++;
2095 } 2080 }
2096 } 2081 }
2097 2082
2098#else 2083#else
2099 write (1, s, len); 2084 write (1, s, len); // output not initialised
2100#endif 2085#endif
2086}
2087
2088INTERFACE void
2089putstr (SCHEME_P_ const char *s)
2090{
2091 putchars (SCHEME_A_ s, strlen (s));
2101} 2092}
2102 2093
2103INTERFACE void 2094INTERFACE void
2104putcharacter (SCHEME_P_ int c) 2095putcharacter (SCHEME_P_ int c)
2105{ 2096{
2106#if USE_PORTS
2107 port *pt = port (SCHEME_V->outport);
2108
2109 if (pt->kind & port_file)
2110 {
2111 char cc = c;
2112 write (pt->rep.stdio.file, &cc, 1);
2113 }
2114 else
2115 {
2116 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2117 *pt->rep.string.curr++ = c;
2118 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2119 *pt->rep.string.curr++ = c;
2120 }
2121
2122#else
2123 char cc = c; 2097 char cc = c;
2124 write (1, &c, 1); 2098
2125#endif 2099 putchars (SCHEME_A_ &cc, 1);
2126} 2100}
2127 2101
2128/* read characters up to delimiter, but cater to character constants */ 2102/* read characters up to delimiter, but cater to character constants */
2129static char * 2103ecb_cold static char *
2130readstr_upto (SCHEME_P_ int skip, const char *delim) 2104readstr_upto (SCHEME_P_ int skip, const char *delim)
2131{ 2105{
2132 char *p = SCHEME_V->strbuff + skip; 2106 char *p = SCHEME_V->strbuff + skip;
2133 2107
2134 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))));
2143 2117
2144 return SCHEME_V->strbuff; 2118 return SCHEME_V->strbuff;
2145} 2119}
2146 2120
2147/* read string expression "xxx...xxx" */ 2121/* read string expression "xxx...xxx" */
2148static pointer 2122ecb_cold static pointer
2149readstrexp (SCHEME_P_ char delim) 2123readstrexp (SCHEME_P_ char delim)
2150{ 2124{
2151 char *p = SCHEME_V->strbuff; 2125 char *p = SCHEME_V->strbuff;
2152 int c; 2126 int c;
2153 int c1 = 0; 2127 int c1 = 0;
2191 case 'a': *p++ = '\a'; state = st_ok; break; 2165 case 'a': *p++ = '\a'; state = st_ok; break;
2192 case 'n': *p++ = '\n'; state = st_ok; break; 2166 case 'n': *p++ = '\n'; state = st_ok; break;
2193 case 'r': *p++ = '\r'; state = st_ok; break; 2167 case 'r': *p++ = '\r'; state = st_ok; break;
2194 case 't': *p++ = '\t'; state = st_ok; break; 2168 case 't': *p++ = '\t'; state = st_ok; break;
2195 2169
2196 //TODO: \whitespace eol whitespace 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;
2197 2178
2198 //TODO: x should end in ;, not two-digit hex 2179 //TODO: x should end in ;, not two-digit hex
2199 case 'x': 2180 case 'x':
2200 case 'X': 2181 case 'X':
2201 state = st_x1; 2182 state = st_x1;
2259 } 2240 }
2260 } 2241 }
2261} 2242}
2262 2243
2263/* check c is in chars */ 2244/* check c is in chars */
2264ecb_inline int 2245ecb_cold int
2265is_one_of (const char *s, int c) 2246is_one_of (const char *s, int c)
2266{ 2247{
2267 return c == EOF || !!strchr (s, c); 2248 return c == EOF || !!strchr (s, c);
2268} 2249}
2269 2250
2270/* skip white characters */ 2251/* skip white characters */
2271ecb_inline int 2252ecb_cold int
2272skipspace (SCHEME_P) 2253skipspace (SCHEME_P)
2273{ 2254{
2274 int c, curr_line = 0; 2255 int c, curr_line = 0;
2275 2256
2276 do 2257 do
2296 backchar (SCHEME_A_ c); 2277 backchar (SCHEME_A_ c);
2297 return 1; 2278 return 1;
2298} 2279}
2299 2280
2300/* get token */ 2281/* get token */
2301static int 2282ecb_cold static int
2302token (SCHEME_P) 2283token (SCHEME_P)
2303{ 2284{
2304 int c = skipspace (SCHEME_A); 2285 int c = skipspace (SCHEME_A);
2305 2286
2306 if (c == EOF) 2287 if (c == EOF)
2404} 2385}
2405 2386
2406/* ========== Routines for Printing ========== */ 2387/* ========== Routines for Printing ========== */
2407#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) 2388#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL)
2408 2389
2409static void 2390ecb_cold static void
2410printslashstring (SCHEME_P_ char *p, int len) 2391printslashstring (SCHEME_P_ char *p, int len)
2411{ 2392{
2412 int i; 2393 int i;
2413 unsigned char *s = (unsigned char *) p; 2394 unsigned char *s = (unsigned char *) p;
2414 2395
2470 2451
2471 putcharacter (SCHEME_A_ '"'); 2452 putcharacter (SCHEME_A_ '"');
2472} 2453}
2473 2454
2474/* print atoms */ 2455/* print atoms */
2475static void 2456ecb_cold static void
2476printatom (SCHEME_P_ pointer l, int f) 2457printatom (SCHEME_P_ pointer l, int f)
2477{ 2458{
2478 char *p; 2459 char *p;
2479 int len; 2460 int len;
2480 2461
2481 atom2str (SCHEME_A_ l, f, &p, &len); 2462 atom2str (SCHEME_A_ l, f, &p, &len);
2482 putchars (SCHEME_A_ p, len); 2463 putchars (SCHEME_A_ p, len);
2483} 2464}
2484 2465
2485/* Uses internal buffer unless string pointer is already available */ 2466/* Uses internal buffer unless string pointer is already available */
2486static void 2467ecb_cold static void
2487atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2468atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2488{ 2469{
2489 char *p; 2470 char *p;
2490 2471
2491 if (l == NIL) 2472 if (l == NIL)
2698 return car (d); 2679 return car (d);
2699 2680
2700 p = cons (car (d), cdr (d)); 2681 p = cons (car (d), cdr (d));
2701 q = p; 2682 q = p;
2702 2683
2703 while (cdr (cdr (p)) != NIL) 2684 while (cddr (p) != NIL)
2704 { 2685 {
2705 d = cons (car (p), cdr (p)); 2686 d = cons (car (p), cdr (p));
2706 2687
2707 if (cdr (cdr (p)) != NIL) 2688 if (cddr (p) != NIL)
2708 p = cdr (d); 2689 p = cdr (d);
2709 } 2690 }
2710 2691
2711 set_cdr (p, car (cdr (p))); 2692 set_cdr (p, cadr (p));
2712 return q; 2693 return q;
2713} 2694}
2714 2695
2715/* reverse list -- produce new list */ 2696/* reverse list -- produce new list */
2716static pointer 2697ecb_hot static pointer
2717reverse (SCHEME_P_ pointer a) 2698reverse (SCHEME_P_ pointer a)
2718{ 2699{
2719 /* a must be checked by gc */ 2700 /* a must be checked by gc */
2720 pointer p = NIL; 2701 pointer p = NIL;
2721 2702
2724 2705
2725 return p; 2706 return p;
2726} 2707}
2727 2708
2728/* reverse list --- in-place */ 2709/* reverse list --- in-place */
2729static pointer 2710ecb_hot static pointer
2730reverse_in_place (SCHEME_P_ pointer term, pointer list) 2711reverse_in_place (SCHEME_P_ pointer term, pointer list)
2731{ 2712{
2732 pointer result = term; 2713 pointer result = term;
2733 pointer p = list; 2714 pointer p = list;
2734 2715
2742 2723
2743 return result; 2724 return result;
2744} 2725}
2745 2726
2746/* append list -- produce new list (in reverse order) */ 2727/* append list -- produce new list (in reverse order) */
2747static pointer 2728ecb_hot static pointer
2748revappend (SCHEME_P_ pointer a, pointer b) 2729revappend (SCHEME_P_ pointer a, pointer b)
2749{ 2730{
2750 pointer result = a; 2731 pointer result = a;
2751 pointer p = b; 2732 pointer p = b;
2752 2733
2761 2742
2762 return S_F; /* signal an error */ 2743 return S_F; /* signal an error */
2763} 2744}
2764 2745
2765/* equivalence of atoms */ 2746/* equivalence of atoms */
2766int 2747ecb_hot int
2767eqv (pointer a, pointer b) 2748eqv (pointer a, pointer b)
2768{ 2749{
2769 if (is_string (a)) 2750 if (is_string (a))
2770 { 2751 {
2771 if (is_string (b)) 2752 if (is_string (b))
2865 } 2846 }
2866 else 2847 else
2867 set_car (env, immutable_cons (slot, car (env))); 2848 set_car (env, immutable_cons (slot, car (env)));
2868} 2849}
2869 2850
2870static pointer 2851ecb_hot static pointer
2871find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2852find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2872{ 2853{
2873 pointer x, y; 2854 pointer x, y;
2874 2855
2875 for (x = env; x != NIL; x = cdr (x)) 2856 for (x = env; x != NIL; x = cdr (x))
2896 return NIL; 2877 return NIL;
2897} 2878}
2898 2879
2899#else /* USE_ALIST_ENV */ 2880#else /* USE_ALIST_ENV */
2900 2881
2901ecb_inline void 2882static void
2902new_frame_in_env (SCHEME_P_ pointer old_env) 2883new_frame_in_env (SCHEME_P_ pointer old_env)
2903{ 2884{
2904 SCHEME_V->envir = immutable_cons (NIL, old_env); 2885 SCHEME_V->envir = immutable_cons (NIL, old_env);
2905 setenvironment (SCHEME_V->envir); 2886 setenvironment (SCHEME_V->envir);
2906} 2887}
2907 2888
2908ecb_inline void 2889static void
2909new_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)
2910{ 2891{
2911 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2892 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2912} 2893}
2913 2894
2914static pointer 2895ecb_hot static pointer
2915find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2896find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2916{ 2897{
2917 pointer x, y; 2898 pointer x, y;
2918 2899
2919 for (x = env; x != NIL; x = cdr (x)) 2900 for (x = env; x != NIL; x = cdr (x))
2933 return NIL; 2914 return NIL;
2934} 2915}
2935 2916
2936#endif /* USE_ALIST_ENV else */ 2917#endif /* USE_ALIST_ENV else */
2937 2918
2938ecb_inline void 2919static void
2939new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2920new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2940{ 2921{
2941 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 2922 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2942 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);
2943} 2924}
2944 2925
2945ecb_inline void 2926static void
2946set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2927set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2947{ 2928{
2948 set_cdr (slot, value); 2929 set_cdr (slot, value);
2949} 2930}
2950 2931
2951ecb_inline pointer 2932static pointer
2952slot_value_in_env (pointer slot) 2933slot_value_in_env (pointer slot)
2953{ 2934{
2954 return cdr (slot); 2935 return cdr (slot);
2955} 2936}
2956 2937
2957/* ========== Evaluation Cycle ========== */ 2938/* ========== Evaluation Cycle ========== */
2958 2939
2959static int 2940ecb_cold static int
2960xError_1 (SCHEME_P_ const char *s, pointer a) 2941xError_1 (SCHEME_P_ const char *s, pointer a)
2961{ 2942{
2962#if USE_ERROR_HOOK
2963 pointer x;
2964 pointer hdl = SCHEME_V->ERROR_HOOK;
2965#endif
2966
2967#if USE_PRINTF 2943#if USE_PRINTF
2968#if SHOW_ERROR_LINE 2944#if SHOW_ERROR_LINE
2969 char sbuf[STRBUFFSIZE]; 2945 char sbuf[STRBUFFSIZE];
2970 2946
2971 /* make sure error is not in REPL */ 2947 /* make sure error is not in REPL */
2986 } 2962 }
2987#endif 2963#endif
2988#endif 2964#endif
2989 2965
2990#if USE_ERROR_HOOK 2966#if USE_ERROR_HOOK
2991 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);
2992 2968
2993 if (x != NIL) 2969 if (x != NIL)
2994 { 2970 {
2995 pointer code = a 2971 pointer code = a
2996 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) 2972 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL)
3040 pointer code; 3016 pointer code;
3041}; 3017};
3042 3018
3043# define STACK_GROWTH 3 3019# define STACK_GROWTH 3
3044 3020
3045static void 3021ecb_hot static void
3046s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3022s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3047{ 3023{
3048 int nframes = (uintptr_t)SCHEME_V->dump; 3024 int nframes = (uintptr_t)SCHEME_V->dump;
3049 struct dump_stack_frame *next_frame; 3025 struct dump_stack_frame *next_frame;
3050 3026
3063 next_frame->code = code; 3039 next_frame->code = code;
3064 3040
3065 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3041 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3066} 3042}
3067 3043
3068static int 3044static ecb_hot int
3069xs_return (SCHEME_P_ pointer a) 3045xs_return (SCHEME_P_ pointer a)
3070{ 3046{
3071 int nframes = (uintptr_t)SCHEME_V->dump; 3047 int nframes = (uintptr_t)SCHEME_V->dump;
3072 struct dump_stack_frame *frame; 3048 struct dump_stack_frame *frame;
3073 3049
3084 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3060 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3085 3061
3086 return 0; 3062 return 0;
3087} 3063}
3088 3064
3089ecb_inline void 3065ecb_cold void
3090dump_stack_reset (SCHEME_P) 3066dump_stack_reset (SCHEME_P)
3091{ 3067{
3092 /* 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 */
3093 SCHEME_V->dump = (pointer)+0; 3069 SCHEME_V->dump = (pointer)+0;
3094} 3070}
3095 3071
3096ecb_inline void 3072ecb_cold void
3097dump_stack_initialize (SCHEME_P) 3073dump_stack_initialize (SCHEME_P)
3098{ 3074{
3099 SCHEME_V->dump_size = 0; 3075 SCHEME_V->dump_size = 0;
3100 SCHEME_V->dump_base = 0; 3076 SCHEME_V->dump_base = 0;
3101 dump_stack_reset (SCHEME_A); 3077 dump_stack_reset (SCHEME_A);
3102} 3078}
3103 3079
3104static void 3080ecb_cold static void
3105dump_stack_free (SCHEME_P) 3081dump_stack_free (SCHEME_P)
3106{ 3082{
3107 free (SCHEME_V->dump_base); 3083 free (SCHEME_V->dump_base);
3108 SCHEME_V->dump_base = 0; 3084 SCHEME_V->dump_base = 0;
3109 SCHEME_V->dump = (pointer)0; 3085 SCHEME_V->dump = (pointer)0;
3110 SCHEME_V->dump_size = 0; 3086 SCHEME_V->dump_size = 0;
3111} 3087}
3112 3088
3113static void 3089ecb_cold static void
3114dump_stack_mark (SCHEME_P) 3090dump_stack_mark (SCHEME_P)
3115{ 3091{
3116 int nframes = (uintptr_t)SCHEME_V->dump; 3092 int nframes = (uintptr_t)SCHEME_V->dump;
3117 int i; 3093 int i;
3118 3094
3124 mark (frame->envir); 3100 mark (frame->envir);
3125 mark (frame->code); 3101 mark (frame->code);
3126 } 3102 }
3127} 3103}
3128 3104
3129static pointer 3105ecb_cold static pointer
3130ss_get_cont (SCHEME_P) 3106ss_get_cont (SCHEME_P)
3131{ 3107{
3132 int nframes = (uintptr_t)SCHEME_V->dump; 3108 int nframes = (uintptr_t)SCHEME_V->dump;
3133 int i; 3109 int i;
3134 3110
3146 } 3122 }
3147 3123
3148 return cont; 3124 return cont;
3149} 3125}
3150 3126
3151static void 3127ecb_cold static void
3152ss_set_cont (SCHEME_P_ pointer cont) 3128ss_set_cont (SCHEME_P_ pointer cont)
3153{ 3129{
3154 int i = 0; 3130 int i = 0;
3155 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3131 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3156 3132
3168 SCHEME_V->dump = (pointer)(uintptr_t)i; 3144 SCHEME_V->dump = (pointer)(uintptr_t)i;
3169} 3145}
3170 3146
3171#else 3147#else
3172 3148
3173ecb_inline void 3149ecb_cold void
3174dump_stack_reset (SCHEME_P) 3150dump_stack_reset (SCHEME_P)
3175{ 3151{
3176 SCHEME_V->dump = NIL; 3152 SCHEME_V->dump = NIL;
3177} 3153}
3178 3154
3179ecb_inline void 3155ecb_cold void
3180dump_stack_initialize (SCHEME_P) 3156dump_stack_initialize (SCHEME_P)
3181{ 3157{
3182 dump_stack_reset (SCHEME_A); 3158 dump_stack_reset (SCHEME_A);
3183} 3159}
3184 3160
3185static void 3161ecb_cold static void
3186dump_stack_free (SCHEME_P) 3162dump_stack_free (SCHEME_P)
3187{ 3163{
3188 SCHEME_V->dump = NIL; 3164 SCHEME_V->dump = NIL;
3189} 3165}
3190 3166
3191static int 3167ecb_hot static int
3192xs_return (SCHEME_P_ pointer a) 3168xs_return (SCHEME_P_ pointer a)
3193{ 3169{
3194 pointer dump = SCHEME_V->dump; 3170 pointer dump = SCHEME_V->dump;
3195 3171
3196 SCHEME_V->value = a; 3172 SCHEME_V->value = a;
3206 SCHEME_V->dump = dump; 3182 SCHEME_V->dump = dump;
3207 3183
3208 return 0; 3184 return 0;
3209} 3185}
3210 3186
3211static void 3187ecb_hot static void
3212s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3188s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3213{ 3189{
3214 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), 3190 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op),
3215 cons (args, 3191 cons (args,
3216 cons (SCHEME_V->envir, 3192 cons (SCHEME_V->envir,
3217 cons (code, 3193 cons (code,
3218 SCHEME_V->dump)))); 3194 SCHEME_V->dump))));
3219} 3195}
3220 3196
3221static void 3197ecb_cold static void
3222dump_stack_mark (SCHEME_P) 3198dump_stack_mark (SCHEME_P)
3223{ 3199{
3224 mark (SCHEME_V->dump); 3200 mark (SCHEME_V->dump);
3225} 3201}
3226 3202
3227static pointer 3203ecb_cold static pointer
3228ss_get_cont (SCHEME_P) 3204ss_get_cont (SCHEME_P)
3229{ 3205{
3230 return SCHEME_V->dump; 3206 return SCHEME_V->dump;
3231} 3207}
3232 3208
3233static void 3209ecb_cold static void
3234ss_set_cont (SCHEME_P_ pointer cont) 3210ss_set_cont (SCHEME_P_ pointer cont)
3235{ 3211{
3236 SCHEME_V->dump = cont; 3212 SCHEME_V->dump = cont;
3237} 3213}
3238 3214
3239#endif 3215#endif
3240 3216
3241#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3217#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3242 3218
3243#if EXPERIMENT 3219#if EXPERIMENT
3220
3244static int 3221static int
3245debug (SCHEME_P_ int indent, pointer x) 3222dtree (SCHEME_P_ int indent, pointer x)
3246{ 3223{
3247 int c; 3224 int c;
3248 3225
3249 if (is_syntax (x)) 3226 if (is_syntax (x))
3250 { 3227 {
3268 printf ("%*sS<%s>\n", indent, "", symname (x)); 3245 printf ("%*sS<%s>\n", indent, "", symname (x));
3269 return 24+8; 3246 return 24+8;
3270 3247
3271 case T_CLOSURE: 3248 case T_CLOSURE:
3272 printf ("%*sS<%s>\n", indent, "", "closure"); 3249 printf ("%*sS<%s>\n", indent, "", "closure");
3273 debug (SCHEME_A_ indent + 3, cdr(x)); 3250 dtree (SCHEME_A_ indent + 3, cdr(x));
3274 return 32 + debug (SCHEME_A_ indent + 3, car (x)); 3251 return 32 + dtree (SCHEME_A_ indent + 3, car (x));
3275 3252
3276 case T_PAIR: 3253 case T_PAIR:
3277 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); 3254 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3278 c = debug (SCHEME_A_ indent + 3, car (x)); 3255 c = dtree (SCHEME_A_ indent + 3, car (x));
3279 c += debug (SCHEME_A_ indent + 3, cdr (x)); 3256 c += dtree (SCHEME_A_ indent + 3, cdr (x));
3280 return c + 1; 3257 return c + 1;
3281 3258
3282 case T_PORT: 3259 case T_PORT:
3283 printf ("%*sS<%s>\n", indent, "", "port"); 3260 printf ("%*sS<%s>\n", indent, "", "port");
3284 return 24+8; 3261 return 24+8;
3287 printf ("%*sS<%s>\n", indent, "", "vector"); 3264 printf ("%*sS<%s>\n", indent, "", "vector");
3288 return 24+8; 3265 return 24+8;
3289 3266
3290 case T_ENVIRONMENT: 3267 case T_ENVIRONMENT:
3291 printf ("%*sS<%s>\n", indent, "", "environment"); 3268 printf ("%*sS<%s>\n", indent, "", "environment");
3292 return 0 + debug (SCHEME_A_ indent + 3, car (x)); 3269 return 0 + dtree (SCHEME_A_ indent + 3, car (x));
3293 3270
3294 default: 3271 default:
3295 printf ("unhandled type %d\n", type (x)); 3272 printf ("unhandled type %d\n", type (x));
3296 break; 3273 break;
3297 } 3274 }
3298} 3275}
3299#endif
3300 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
3301static 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
3302opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3645opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3303{ 3646{
3304 pointer args = SCHEME_V->args; 3647 pointer args = SCHEME_V->args;
3305 pointer x, y; 3648 pointer x, y;
3306 3649
3307 switch (op) 3650 switch (op)
3308 { 3651 {
3309#if EXPERIMENT //D 3652#if EXPERIMENT //D
3310 case OP_DEBUG: 3653 case OP_DEBUG:
3311 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);
3312 printf ("\n"); 3657 printf ("\n");
3313 s_return (S_T); 3658 s_return (S_T);
3659 }
3660
3661 case OP_DEBUG2:
3662 return -1;
3314#endif 3663#endif
3664
3315 case OP_LOAD: /* load */ 3665 case OP_LOAD: /* load */
3316 if (file_interactive (SCHEME_A)) 3666 if (file_interactive (SCHEME_A))
3317 { 3667 {
3318 putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n"); 3668 putstr (SCHEME_A_ "Loading ");
3319 //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');
3320 } 3671 }
3321 3672
3322 if (!file_push (SCHEME_A_ strvalue (car (args)))) 3673 if (!file_push (SCHEME_A_ strvalue (car (args))))
3323 Error_1 ("unable to open", car (args)); 3674 Error_1 ("unable to open", car (args));
3324 else 3675
3325 {
3326 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3676 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3327 s_goto (OP_T0LVL); 3677 s_goto (OP_T0LVL);
3328 }
3329 3678
3330 case OP_T0LVL: /* top level */ 3679 case OP_T0LVL: /* top level */
3331 3680
3332 /* If we reached the end of file, this loop is done. */ 3681 /* If we reached the end of file, this loop is done. */
3333 if (port (SCHEME_V->loadport)->kind & port_saw_EOF) 3682 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3349 /* If interactive, be nice to user. */ 3698 /* If interactive, be nice to user. */
3350 if (file_interactive (SCHEME_A)) 3699 if (file_interactive (SCHEME_A))
3351 { 3700 {
3352 SCHEME_V->envir = SCHEME_V->global_env; 3701 SCHEME_V->envir = SCHEME_V->global_env;
3353 dump_stack_reset (SCHEME_A); 3702 dump_stack_reset (SCHEME_A);
3354 putstr (SCHEME_A_ "\n"); 3703 putcharacter (SCHEME_A_ '\n');
3704#if EXPERIMENT
3705 system ("ps v $PPID");
3706#endif
3355 putstr (SCHEME_A_ prompt); 3707 putstr (SCHEME_A_ prompt);
3356 } 3708 }
3357 3709
3358 /* Set up another iteration of REPL */ 3710 /* Set up another iteration of REPL */
3359 SCHEME_V->nesting = 0; 3711 SCHEME_V->nesting = 0;
3394 { 3746 {
3395 SCHEME_V->print_flag = 1; 3747 SCHEME_V->print_flag = 1;
3396 SCHEME_V->args = SCHEME_V->value; 3748 SCHEME_V->args = SCHEME_V->value;
3397 s_goto (OP_P0LIST); 3749 s_goto (OP_P0LIST);
3398 } 3750 }
3399 else 3751
3400 s_return (SCHEME_V->value); 3752 s_return (SCHEME_V->value);
3401 3753
3402 case OP_EVAL: /* main part of evaluation */ 3754 case OP_EVAL: /* main part of evaluation */
3403#if USE_TRACING 3755#if USE_TRACING
3404 if (SCHEME_V->tracing) 3756 if (SCHEME_V->tracing)
3405 { 3757 {
3438 /* 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)); */
3439 SCHEME_V->code = x; 3791 SCHEME_V->code = x;
3440 s_goto (OP_EVAL); 3792 s_goto (OP_EVAL);
3441 } 3793 }
3442 } 3794 }
3443 else 3795
3444 s_return (SCHEME_V->code); 3796 s_return (SCHEME_V->code);
3445 3797
3446 case OP_E0ARGS: /* eval arguments */ 3798 case OP_E0ARGS: /* eval arguments */
3447 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ 3799 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3448 { 3800 {
3449 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3801 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3450 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3802 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3451 SCHEME_V->code = SCHEME_V->value; 3803 SCHEME_V->code = SCHEME_V->value;
3452 s_goto (OP_APPLY); 3804 s_goto (OP_APPLY);
3453 } 3805 }
3454 else 3806
3455 {
3456 SCHEME_V->code = cdr (SCHEME_V->code); 3807 SCHEME_V->code = cdr (SCHEME_V->code);
3457 s_goto (OP_E1ARGS); 3808 s_goto (OP_E1ARGS);
3458 }
3459 3809
3460 case OP_E1ARGS: /* eval arguments */ 3810 case OP_E1ARGS: /* eval arguments */
3461 args = cons (SCHEME_V->value, args); 3811 args = cons (SCHEME_V->value, args);
3462 3812
3463 if (is_pair (SCHEME_V->code)) /* continue */ 3813 if (is_pair (SCHEME_V->code)) /* continue */
3474 SCHEME_V->args = cdr (args); 3824 SCHEME_V->args = cdr (args);
3475 s_goto (OP_APPLY); 3825 s_goto (OP_APPLY);
3476 } 3826 }
3477 3827
3478#if USE_TRACING 3828#if USE_TRACING
3479
3480 case OP_TRACING: 3829 case OP_TRACING:
3481 { 3830 {
3482 int tr = SCHEME_V->tracing; 3831 int tr = SCHEME_V->tracing;
3483 3832
3484 SCHEME_V->tracing = ivalue_unchecked (car (args)); 3833 SCHEME_V->tracing = ivalue_unchecked (car (args));
3485 s_return (mk_integer (SCHEME_A_ tr)); 3834 s_return (mk_integer (SCHEME_A_ tr));
3486 } 3835 }
3487
3488#endif 3836#endif
3489 3837
3490 case OP_APPLY: /* apply 'code' to 'args' */ 3838 case OP_APPLY: /* apply 'code' to 'args' */
3491#if USE_TRACING 3839#if USE_TRACING
3492 if (SCHEME_V->tracing) 3840 if (SCHEME_V->tracing)
3546 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ 3894 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */
3547 { 3895 {
3548 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); 3896 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code));
3549 s_return (args != NIL ? car (args) : NIL); 3897 s_return (args != NIL ? car (args) : NIL);
3550 } 3898 }
3551 else 3899
3552 Error_0 ("illegal function"); 3900 Error_0 ("illegal function");
3553 3901
3554 case OP_DOMACRO: /* do macro */ 3902 case OP_DOMACRO: /* do macro */
3555 SCHEME_V->code = SCHEME_V->value; 3903 SCHEME_V->code = SCHEME_V->value;
3556 s_goto (OP_EVAL); 3904 s_goto (OP_EVAL);
3557 3905
3621 else 3969 else
3622 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);
3623 3971
3624 s_return (SCHEME_V->code); 3972 s_return (SCHEME_V->code);
3625 3973
3626
3627 case OP_DEFP: /* defined? */ 3974 case OP_DEFP: /* defined? */
3628 x = SCHEME_V->envir; 3975 x = SCHEME_V->envir;
3629 3976
3630 if (cdr (args) != NIL) 3977 if (cdr (args) != NIL)
3631 x = cadr (args); 3978 x = cadr (args);
3649 s_return (SCHEME_V->value); 3996 s_return (SCHEME_V->value);
3650 } 3997 }
3651 else 3998 else
3652 Error_1 ("set!: unbound variable:", SCHEME_V->code); 3999 Error_1 ("set!: unbound variable:", SCHEME_V->code);
3653 4000
3654
3655 case OP_BEGIN: /* begin */ 4001 case OP_BEGIN: /* begin */
3656 if (!is_pair (SCHEME_V->code)) 4002 if (!is_pair (SCHEME_V->code))
3657 s_return (SCHEME_V->code); 4003 s_return (SCHEME_V->code);
3658 4004
3659 if (cdr (SCHEME_V->code) != NIL) 4005 if (cdr (SCHEME_V->code) != NIL)
3670 case OP_IF1: /* if */ 4016 case OP_IF1: /* if */
3671 if (is_true (SCHEME_V->value)) 4017 if (is_true (SCHEME_V->value))
3672 SCHEME_V->code = car (SCHEME_V->code); 4018 SCHEME_V->code = car (SCHEME_V->code);
3673 else 4019 else
3674 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
3675 s_goto (OP_EVAL); 4022 s_goto (OP_EVAL);
3676 4023
3677 case OP_LET0: /* let */ 4024 case OP_LET0: /* let */
3678 SCHEME_V->args = NIL; 4025 SCHEME_V->args = NIL;
3679 SCHEME_V->value = SCHEME_V->code; 4026 SCHEME_V->value = SCHEME_V->code;
3680 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);
3681 s_goto (OP_LET1); 4028 s_goto (OP_LET1);
3682 4029
3683 case OP_LET1: /* let (calculate parameters) */ 4030 case OP_LET1: /* let (calculate parameters) */
4031 case OP_LET1REC: /* letrec (calculate parameters) */
3684 args = cons (SCHEME_V->value, args); 4032 args = cons (SCHEME_V->value, args);
3685 4033
3686 if (is_pair (SCHEME_V->code)) /* continue */ 4034 if (is_pair (SCHEME_V->code)) /* continue */
3687 { 4035 {
3688 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)))
3689 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));
3690 4038
3691 s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code)); 4039 s_save (SCHEME_A_ op, args, cdr (SCHEME_V->code));
3692 SCHEME_V->code = cadar (SCHEME_V->code); 4040 SCHEME_V->code = cadar (SCHEME_V->code);
3693 SCHEME_V->args = NIL; 4041 SCHEME_V->args = NIL;
3694 s_goto (OP_EVAL); 4042 s_goto (OP_EVAL);
3695 } 4043 }
3696 else /* end */ 4044
3697 { 4045 /* end */
3698 args = reverse_in_place (SCHEME_A_ NIL, args); 4046 args = reverse_in_place (SCHEME_A_ NIL, args);
3699 SCHEME_V->code = car (args); 4047 SCHEME_V->code = car (args);
3700 SCHEME_V->args = cdr (args); 4048 SCHEME_V->args = cdr (args);
3701 s_goto (OP_LET2); 4049 s_goto (op == OP_LET1 ? OP_LET2 : OP_LET2REC);
3702 }
3703 4050
3704 case OP_LET2: /* let */ 4051 case OP_LET2: /* let */
3705 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4052 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3706 4053
3707 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;
3711 if (is_symbol (car (SCHEME_V->code))) /* named let */ 4058 if (is_symbol (car (SCHEME_V->code))) /* named let */
3712 { 4059 {
3713 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))
3714 { 4061 {
3715 if (!is_pair (x)) 4062 if (!is_pair (x))
3716 Error_1 ("Bad syntax of binding in let :", x); 4063 Error_1 ("Bad syntax of binding in let:", x);
3717 4064
3718 if (!is_list (SCHEME_A_ car (x))) 4065 if (!is_list (SCHEME_A_ car (x)))
3719 Error_1 ("Bad syntax of binding in let :", car (x)); 4066 Error_1 ("Bad syntax of binding in let:", car (x));
3720 4067
3721 args = cons (caar (x), args); 4068 args = cons (caar (x), args);
3722 } 4069 }
3723 4070
3724 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)),
3741 SCHEME_V->code = cdr (SCHEME_V->code); 4088 SCHEME_V->code = cdr (SCHEME_V->code);
3742 s_goto (OP_BEGIN); 4089 s_goto (OP_BEGIN);
3743 } 4090 }
3744 4091
3745 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)))
3746 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));
3747 4094
3748 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));
3749 SCHEME_V->code = car (cdaar (SCHEME_V->code)); 4096 SCHEME_V->code = car (cdaar (SCHEME_V->code));
3750 s_goto (OP_EVAL); 4097 s_goto (OP_EVAL);
3751 4098
3762 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); 4109 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code);
3763 SCHEME_V->code = cadar (SCHEME_V->code); 4110 SCHEME_V->code = cadar (SCHEME_V->code);
3764 SCHEME_V->args = NIL; 4111 SCHEME_V->args = NIL;
3765 s_goto (OP_EVAL); 4112 s_goto (OP_EVAL);
3766 } 4113 }
3767 else /* end */ 4114
4115 /* end */
3768 { 4116
3769 SCHEME_V->code = args; 4117 SCHEME_V->code = args;
3770 SCHEME_V->args = NIL; 4118 SCHEME_V->args = NIL;
3771 s_goto (OP_BEGIN); 4119 s_goto (OP_BEGIN);
3772 }
3773 4120
3774 case OP_LET0REC: /* letrec */ 4121 case OP_LET0REC: /* letrec */
3775 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4122 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3776 SCHEME_V->args = NIL; 4123 SCHEME_V->args = NIL;
3777 SCHEME_V->value = SCHEME_V->code; 4124 SCHEME_V->value = SCHEME_V->code;
3778 SCHEME_V->code = car (SCHEME_V->code); 4125 SCHEME_V->code = car (SCHEME_V->code);
3779 s_goto (OP_LET1REC); 4126 s_goto (OP_LET1REC);
3780 4127
3781 case OP_LET1REC: /* letrec (calculate parameters) */ 4128 /* OP_LET1REC handled by OP_LET1 */
3782 args = cons (SCHEME_V->value, args);
3783
3784 if (is_pair (SCHEME_V->code)) /* continue */
3785 {
3786 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3787 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3788
3789 s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code));
3790 SCHEME_V->code = cadar (SCHEME_V->code);
3791 SCHEME_V->args = NIL;
3792 s_goto (OP_EVAL);
3793 }
3794 else /* end */
3795 {
3796 args = reverse_in_place (SCHEME_A_ NIL, args);
3797 SCHEME_V->code = car (args);
3798 SCHEME_V->args = cdr (args);
3799 s_goto (OP_LET2REC);
3800 }
3801 4129
3802 case OP_LET2REC: /* letrec */ 4130 case OP_LET2REC: /* letrec */
3803 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))
3804 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 4132 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3805 4133
3835 } 4163 }
3836 else 4164 else
3837 { 4165 {
3838 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) 4166 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL)
3839 s_return (NIL); 4167 s_return (NIL);
3840 else 4168
3841 {
3842 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 4169 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
3843 SCHEME_V->code = caar (SCHEME_V->code); 4170 SCHEME_V->code = caar (SCHEME_V->code);
3844 s_goto (OP_EVAL); 4171 s_goto (OP_EVAL);
3845 }
3846 } 4172 }
3847 4173
3848 case OP_DELAY: /* delay */ 4174 case OP_DELAY: /* delay */
3849 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);
3850 set_typeflag (x, T_PROMISE); 4176 set_typeflag (x, T_PROMISE);
3861 case OP_AND1: /* and */ 4187 case OP_AND1: /* and */
3862 if (is_false (SCHEME_V->value)) 4188 if (is_false (SCHEME_V->value))
3863 s_return (SCHEME_V->value); 4189 s_return (SCHEME_V->value);
3864 else if (SCHEME_V->code == NIL) 4190 else if (SCHEME_V->code == NIL)
3865 s_return (SCHEME_V->value); 4191 s_return (SCHEME_V->value);
3866 else 4192
3867 {
3868 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 4193 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
3869 SCHEME_V->code = car (SCHEME_V->code); 4194 SCHEME_V->code = car (SCHEME_V->code);
3870 s_goto (OP_EVAL); 4195 s_goto (OP_EVAL);
3871 }
3872 4196
3873 case OP_OR0: /* or */ 4197 case OP_OR0: /* or */
3874 if (SCHEME_V->code == NIL) 4198 if (SCHEME_V->code == NIL)
3875 s_return (S_F); 4199 s_return (S_F);
3876 4200
3881 case OP_OR1: /* or */ 4205 case OP_OR1: /* or */
3882 if (is_true (SCHEME_V->value)) 4206 if (is_true (SCHEME_V->value))
3883 s_return (SCHEME_V->value); 4207 s_return (SCHEME_V->value);
3884 else if (SCHEME_V->code == NIL) 4208 else if (SCHEME_V->code == NIL)
3885 s_return (SCHEME_V->value); 4209 s_return (SCHEME_V->value);
3886 else 4210
3887 {
3888 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 4211 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
3889 SCHEME_V->code = car (SCHEME_V->code); 4212 SCHEME_V->code = car (SCHEME_V->code);
3890 s_goto (OP_EVAL); 4213 s_goto (OP_EVAL);
3891 }
3892 4214
3893 case OP_C0STREAM: /* cons-stream */ 4215 case OP_C0STREAM: /* cons-stream */
3894 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 4216 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3895 SCHEME_V->code = car (SCHEME_V->code); 4217 SCHEME_V->code = car (SCHEME_V->code);
3896 s_goto (OP_EVAL); 4218 s_goto (OP_EVAL);
3961 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); 4283 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x));
3962 SCHEME_V->code = caar (x); 4284 SCHEME_V->code = caar (x);
3963 s_goto (OP_EVAL); 4285 s_goto (OP_EVAL);
3964 } 4286 }
3965 } 4287 }
3966 else 4288
3967 s_return (NIL); 4289 s_return (NIL);
3968 4290
3969 case OP_CASE2: /* case */ 4291 case OP_CASE2: /* case */
3970 if (is_true (SCHEME_V->value)) 4292 if (is_true (SCHEME_V->value))
3971 s_goto (OP_BEGIN); 4293 s_goto (OP_BEGIN);
3972 else 4294
3973 s_return (NIL); 4295 s_return (NIL);
3974 4296
3975 case OP_PAPPLY: /* apply */ 4297 case OP_PAPPLY: /* apply */
3976 SCHEME_V->code = car (args); 4298 SCHEME_V->code = car (args);
3977 SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); 4299 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
3978 /*SCHEME_V->args = cadr(args); */ 4300 /*SCHEME_V->args = cadr(args); */
3992 } 4314 }
3993 4315
3994 if (USE_ERROR_CHECKING) abort (); 4316 if (USE_ERROR_CHECKING) abort ();
3995} 4317}
3996 4318
3997static int 4319/* math, cxr */
4320ecb_hot static int
3998opexe_1 (SCHEME_P_ enum scheme_opcodes op) 4321opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3999{ 4322{
4000 pointer args = SCHEME_V->args; 4323 pointer args = SCHEME_V->args;
4001 pointer x = car (args); 4324 pointer x = car (args);
4002 num v; 4325 num v;
4483 } 4806 }
4484 4807
4485 if (USE_ERROR_CHECKING) abort (); 4808 if (USE_ERROR_CHECKING) abort ();
4486} 4809}
4487 4810
4488static int 4811/* relational ops */
4812ecb_hot static int
4489opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4813opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4490{ 4814{
4491 pointer x = SCHEME_V->args; 4815 pointer x = SCHEME_V->args;
4492 4816
4493 for (;;) 4817 for (;;)
4514 } 4838 }
4515 4839
4516 s_return (S_T); 4840 s_return (S_T);
4517} 4841}
4518 4842
4519static int 4843/* predicates */
4844ecb_hot static int
4520opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4845opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4521{ 4846{
4522 pointer args = SCHEME_V->args; 4847 pointer args = SCHEME_V->args;
4523 pointer a = car (args); 4848 pointer a = car (args);
4524 pointer d = cdr (args); 4849 pointer d = cdr (args);
4571 } 4896 }
4572 4897
4573 s_retbool (r); 4898 s_retbool (r);
4574} 4899}
4575 4900
4576static int 4901/* promises, list ops, ports */
4902ecb_hot static int
4577opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4903opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4578{ 4904{
4579 pointer args = SCHEME_V->args; 4905 pointer args = SCHEME_V->args;
4580 pointer a = car (args); 4906 pointer a = car (args);
4581 pointer x, y; 4907 pointer x, y;
4624 else 4950 else
4625 SCHEME_V->print_flag = 0; 4951 SCHEME_V->print_flag = 0;
4626 4952
4627 s_goto (OP_P0LIST); 4953 s_goto (OP_P0LIST);
4628 4954
4955 //TODO: move to scheme
4629 case OP_NEWLINE: /* newline */ 4956 case OP_NEWLINE: /* newline */
4630 if (is_pair (args)) 4957 if (is_pair (args))
4631 { 4958 {
4632 if (a != SCHEME_V->outport) 4959 if (a != SCHEME_V->outport)
4633 { 4960 {
4635 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4962 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4636 SCHEME_V->outport = a; 4963 SCHEME_V->outport = a;
4637 } 4964 }
4638 } 4965 }
4639 4966
4640 putstr (SCHEME_A_ "\n"); 4967 putcharacter (SCHEME_A_ '\n');
4641 s_return (S_T); 4968 s_return (S_T);
4642#endif 4969#endif
4643 4970
4644 case OP_ERR0: /* error */ 4971 case OP_ERR0: /* error */
4645 SCHEME_V->retcode = -1; 4972 SCHEME_V->retcode = -1;
4654 putstr (SCHEME_A_ strvalue (car (args))); 4981 putstr (SCHEME_A_ strvalue (car (args)));
4655 SCHEME_V->args = cdr (args); 4982 SCHEME_V->args = cdr (args);
4656 s_goto (OP_ERR1); 4983 s_goto (OP_ERR1);
4657 4984
4658 case OP_ERR1: /* error */ 4985 case OP_ERR1: /* error */
4659 putstr (SCHEME_A_ " "); 4986 putcharacter (SCHEME_A_ ' ');
4660 4987
4661 if (args != NIL) 4988 if (args != NIL)
4662 { 4989 {
4663 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); 4990 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4664 SCHEME_V->args = a; 4991 SCHEME_V->args = a;
4665 SCHEME_V->print_flag = 1; 4992 SCHEME_V->print_flag = 1;
4666 s_goto (OP_P0LIST); 4993 s_goto (OP_P0LIST);
4667 } 4994 }
4668 else 4995 else
4669 { 4996 {
4670 putstr (SCHEME_A_ "\n"); 4997 putcharacter (SCHEME_A_ '\n');
4671 4998
4672 if (SCHEME_V->interactive_repl) 4999 if (SCHEME_V->interactive_repl)
4673 s_goto (OP_T0LVL); 5000 s_goto (OP_T0LVL);
4674 else 5001 else
4675 return -1; 5002 return -1;
4883 } 5210 }
4884 5211
4885 if (USE_ERROR_CHECKING) abort (); 5212 if (USE_ERROR_CHECKING) abort ();
4886} 5213}
4887 5214
4888static int 5215/* reading */
5216ecb_cold static int
4889opexe_5 (SCHEME_P_ enum scheme_opcodes op) 5217opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4890{ 5218{
4891 pointer args = SCHEME_V->args; 5219 pointer args = SCHEME_V->args;
4892 pointer x; 5220 pointer x;
4893 5221
4972 case OP_RDSEXPR: 5300 case OP_RDSEXPR:
4973 switch (SCHEME_V->tok) 5301 switch (SCHEME_V->tok)
4974 { 5302 {
4975 case TOK_EOF: 5303 case TOK_EOF:
4976 s_return (S_EOF); 5304 s_return (S_EOF);
4977 /* NOTREACHED */
4978 5305
4979 case TOK_VEC: 5306 case TOK_VEC:
4980 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); 5307 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL);
4981 /* fall through */ 5308 /* fall through */
4982 5309
4985 5312
4986 if (SCHEME_V->tok == TOK_RPAREN) 5313 if (SCHEME_V->tok == TOK_RPAREN)
4987 s_return (NIL); 5314 s_return (NIL);
4988 else if (SCHEME_V->tok == TOK_DOT) 5315 else if (SCHEME_V->tok == TOK_DOT)
4989 Error_0 ("syntax error: illegal dot expression"); 5316 Error_0 ("syntax error: illegal dot expression");
4990 else 5317
4991 {
4992 SCHEME_V->nesting_stack[SCHEME_V->file_i]++; 5318 SCHEME_V->nesting_stack[SCHEME_V->file_i]++;
4993 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL); 5319 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL);
4994 s_goto (OP_RDSEXPR); 5320 s_goto (OP_RDSEXPR);
4995 }
4996 5321
4997 case TOK_QUOTE: 5322 case TOK_QUOTE:
4998 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL); 5323 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL);
4999 SCHEME_V->tok = token (SCHEME_A); 5324 SCHEME_V->tok = token (SCHEME_A);
5000 s_goto (OP_RDSEXPR); 5325 s_goto (OP_RDSEXPR);
5006 { 5331 {
5007 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); 5332 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL);
5008 SCHEME_V->tok = TOK_LPAREN; 5333 SCHEME_V->tok = TOK_LPAREN;
5009 s_goto (OP_RDSEXPR); 5334 s_goto (OP_RDSEXPR);
5010 } 5335 }
5011 else 5336
5012 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); 5337 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL);
5013
5014 s_goto (OP_RDSEXPR); 5338 s_goto (OP_RDSEXPR);
5015 5339
5016 case TOK_COMMA: 5340 case TOK_COMMA:
5017 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); 5341 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL);
5018 SCHEME_V->tok = token (SCHEME_A); 5342 SCHEME_V->tok = token (SCHEME_A);
5029 case TOK_DOTATOM: 5353 case TOK_DOTATOM:
5030 SCHEME_V->strbuff[0] = '.'; 5354 SCHEME_V->strbuff[0] = '.';
5031 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)));
5032 5356
5033 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
5034 x = readstrexp (SCHEME_A_ '|'); 5360 x = readstrexp (SCHEME_A_ '|');
5035 //TODO: haven't checked whether the garbage collector could interfere
5036 s_return (mk_atom (SCHEME_A_ strvalue (x))); 5361 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5037 5362
5038 case TOK_DQUOTE: 5363 case TOK_DQUOTE:
5039 x = readstrexp (SCHEME_A_ '"'); 5364 x = readstrexp (SCHEME_A_ '"');
5040 5365
5048 { 5373 {
5049 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);
5050 5375
5051 if (f == NIL) 5376 if (f == NIL)
5052 Error_0 ("undefined sharp expression"); 5377 Error_0 ("undefined sharp expression");
5053 else 5378
5054 {
5055 SCHEME_V->code = cons (slot_value_in_env (f), NIL); 5379 SCHEME_V->code = cons (slot_value_in_env (f), NIL);
5056 s_goto (OP_EVAL); 5380 s_goto (OP_EVAL);
5057 }
5058 } 5381 }
5059 5382
5060 case TOK_SHARP_CONST: 5383 case TOK_SHARP_CONST:
5061 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)
5062 Error_0 ("undefined sharp expression"); 5385 Error_0 ("undefined sharp expression");
5063 else 5386
5064 s_return (x); 5387 s_return (x);
5065 5388
5066 default: 5389 default:
5067 Error_0 ("syntax error: illegal token"); 5390 Error_0 ("syntax error: illegal token");
5068 } 5391 }
5069 5392
5162 pointer b = cdr (args); 5485 pointer b = cdr (args);
5163 int ok_abbr = ok_abbrev (b); 5486 int ok_abbr = ok_abbrev (b);
5164 SCHEME_V->args = car (b); 5487 SCHEME_V->args = car (b);
5165 5488
5166 if (a == SCHEME_V->QUOTE && ok_abbr) 5489 if (a == SCHEME_V->QUOTE && ok_abbr)
5167 putstr (SCHEME_A_ "'"); 5490 putcharacter (SCHEME_A_ '\'');
5168 else if (a == SCHEME_V->QQUOTE && ok_abbr) 5491 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5169 putstr (SCHEME_A_ "`"); 5492 putcharacter (SCHEME_A_ '`');
5170 else if (a == SCHEME_V->UNQUOTE && ok_abbr) 5493 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5171 putstr (SCHEME_A_ ","); 5494 putcharacter (SCHEME_A_ ',');
5172 else if (a == SCHEME_V->UNQUOTESP && ok_abbr) 5495 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5173 putstr (SCHEME_A_ ",@"); 5496 putstr (SCHEME_A_ ",@");
5174 else 5497 else
5175 { 5498 {
5176 putstr (SCHEME_A_ "("); 5499 putcharacter (SCHEME_A_ '(');
5177 s_save (SCHEME_A_ OP_P1LIST, b, NIL); 5500 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5178 SCHEME_V->args = a; 5501 SCHEME_V->args = a;
5179 } 5502 }
5180 5503
5181 s_goto (OP_P0LIST); 5504 s_goto (OP_P0LIST);
5183 5506
5184 case OP_P1LIST: 5507 case OP_P1LIST:
5185 if (is_pair (args)) 5508 if (is_pair (args))
5186 { 5509 {
5187 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); 5510 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5188 putstr (SCHEME_A_ " "); 5511 putcharacter (SCHEME_A_ ' ');
5189 SCHEME_V->args = car (args); 5512 SCHEME_V->args = car (args);
5190 s_goto (OP_P0LIST); 5513 s_goto (OP_P0LIST);
5191 } 5514 }
5192 else if (is_vector (args)) 5515 else if (is_vector (args))
5193 { 5516 {
5201 { 5524 {
5202 putstr (SCHEME_A_ " . "); 5525 putstr (SCHEME_A_ " . ");
5203 printatom (SCHEME_A_ args, SCHEME_V->print_flag); 5526 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5204 } 5527 }
5205 5528
5206 putstr (SCHEME_A_ ")"); 5529 putcharacter (SCHEME_A_ ')');
5207 s_return (S_T); 5530 s_return (S_T);
5208 } 5531 }
5209 5532
5210 case OP_PVECFROM: 5533 case OP_PVECFROM:
5211 { 5534 {
5213 pointer vec = car (args); 5536 pointer vec = car (args);
5214 int len = veclength (vec); 5537 int len = veclength (vec);
5215 5538
5216 if (i == len) 5539 if (i == len)
5217 { 5540 {
5218 putstr (SCHEME_A_ ")"); 5541 putcharacter (SCHEME_A_ ')');
5219 s_return (S_T); 5542 s_return (S_T);
5220 } 5543 }
5221 else 5544 else
5222 { 5545 {
5223 pointer elem = vector_get (vec, i); 5546 pointer elem = vector_get (vec, i);
5225 ivalue_unchecked (cdr (args)) = i + 1; 5548 ivalue_unchecked (cdr (args)) = i + 1;
5226 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5549 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5227 SCHEME_V->args = elem; 5550 SCHEME_V->args = elem;
5228 5551
5229 if (i > 0) 5552 if (i > 0)
5230 putstr (SCHEME_A_ " "); 5553 putcharacter (SCHEME_A_ ' ');
5231 5554
5232 s_goto (OP_P0LIST); 5555 s_goto (OP_P0LIST);
5233 } 5556 }
5234 } 5557 }
5235 } 5558 }
5236 5559
5237 if (USE_ERROR_CHECKING) abort (); 5560 if (USE_ERROR_CHECKING) abort ();
5238} 5561}
5239 5562
5240static int 5563/* list ops */
5564ecb_hot static int
5241opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5565opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5242{ 5566{
5243 pointer args = SCHEME_V->args; 5567 pointer args = SCHEME_V->args;
5244 pointer a = car (args); 5568 pointer a = car (args);
5245 pointer x, y; 5569 pointer x, y;
5268 break; 5592 break;
5269 } 5593 }
5270 5594
5271 if (is_pair (y)) 5595 if (is_pair (y))
5272 s_return (car (y)); 5596 s_return (car (y));
5273 else 5597
5274 s_return (S_F); 5598 s_return (S_F);
5275
5276 5599
5277 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5600 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5278 SCHEME_V->args = a; 5601 SCHEME_V->args = a;
5279 5602
5280 if (SCHEME_V->args == NIL) 5603 if (SCHEME_V->args == NIL)
5281 s_return (S_F); 5604 s_return (S_F);
5282 else if (is_closure (SCHEME_V->args)) 5605 else if (is_closure (SCHEME_V->args) || is_macro (SCHEME_V->args))
5283 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5606 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5284 else if (is_macro (SCHEME_V->args)) 5607
5285 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5286 else
5287 s_return (S_F); 5608 s_return (S_F);
5288 5609
5289 case OP_CLOSUREP: /* closure? */ 5610 case OP_CLOSUREP: /* closure? */
5290 /* 5611 /*
5291 * Note, macro object is also a closure. 5612 * Note, macro object is also a closure.
5292 * Therefore, (closure? <#MACRO>) ==> #t 5613 * Therefore, (closure? <#MACRO>) ==> #t
5303 5624
5304/* 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 */
5305typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); 5626typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5306 5627
5307typedef int (*test_predicate)(pointer); 5628typedef int (*test_predicate)(pointer);
5308static int 5629
5630ecb_hot static int
5309tst_any (pointer p) 5631tst_any (pointer p)
5310{ 5632{
5311 return 1; 5633 return 1;
5312} 5634}
5313 5635
5314static int 5636ecb_hot static int
5315tst_inonneg (pointer p) 5637tst_inonneg (pointer p)
5316{ 5638{
5317 return is_integer (p) && ivalue_unchecked (p) >= 0; 5639 return is_integer (p) && ivalue_unchecked (p) >= 0;
5318} 5640}
5319 5641
5320static int 5642ecb_hot static int
5321tst_is_list (SCHEME_P_ pointer p) 5643tst_is_list (SCHEME_P_ pointer p)
5322{ 5644{
5323 return p == NIL || is_pair (p); 5645 return p == NIL || is_pair (p);
5324} 5646}
5325 5647
5368#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" 5690#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5369#include "opdefines.h" 5691#include "opdefines.h"
5370#undef OP_DEF 5692#undef OP_DEF
5371; 5693;
5372 5694
5373static const char * 5695ecb_cold static const char *
5374opname (int idx) 5696opname (int idx)
5375{ 5697{
5376 const char *name = opnames; 5698 const char *name = opnames;
5377 5699
5378 /* 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? */
5380 name += strlen (name) + 1; 5702 name += strlen (name) + 1;
5381 5703
5382 return *name ? name : "ILLEGAL"; 5704 return *name ? name : "ILLEGAL";
5383} 5705}
5384 5706
5385static const char * 5707ecb_cold static const char *
5386procname (pointer x) 5708procname (pointer x)
5387{ 5709{
5388 return opname (procnum (x)); 5710 return opname (procnum (x));
5389} 5711}
5390 5712
5410#undef OP_DEF 5732#undef OP_DEF
5411 {0} 5733 {0}
5412}; 5734};
5413 5735
5414/* kernel of this interpreter */ 5736/* kernel of this interpreter */
5415static void ecb_hot 5737ecb_hot static void
5416Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5738Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5417{ 5739{
5418 SCHEME_V->op = op; 5740 SCHEME_V->op = op;
5419 5741
5420 for (;;) 5742 for (;;)
5511 } 5833 }
5512} 5834}
5513 5835
5514/* ========== Initialization of internal keywords ========== */ 5836/* ========== Initialization of internal keywords ========== */
5515 5837
5516static void 5838ecb_cold static void
5517assign_syntax (SCHEME_P_ const char *name) 5839assign_syntax (SCHEME_P_ const char *name)
5518{ 5840{
5519 pointer x = oblist_add_by_name (SCHEME_A_ name); 5841 pointer x = oblist_add_by_name (SCHEME_A_ name);
5520 set_typeflag (x, typeflag (x) | T_SYNTAX); 5842 set_typeflag (x, typeflag (x) | T_SYNTAX);
5521} 5843}
5522 5844
5523static void 5845ecb_cold static void
5524assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) 5846assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
5525{ 5847{
5526 pointer x = mk_symbol (SCHEME_A_ name); 5848 pointer x = mk_symbol (SCHEME_A_ name);
5527 pointer y = mk_proc (SCHEME_A_ op); 5849 pointer y = mk_proc (SCHEME_A_ op);
5528 new_slot_in_env (SCHEME_A_ x, y); 5850 new_slot_in_env (SCHEME_A_ x, y);
5536 ivalue_unchecked (y) = op; 5858 ivalue_unchecked (y) = op;
5537 return y; 5859 return y;
5538} 5860}
5539 5861
5540/* 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! */
5541static int 5863ecb_hot static int
5542syntaxnum (pointer p) 5864syntaxnum (pointer p)
5543{ 5865{
5544 const char *s = strvalue (p); 5866 const char *s = strvalue (p);
5545 5867
5546 switch (strlength (p)) 5868 switch (strlength (p))
5663#endif 5985#endif
5664 } 5986 }
5665 5987
5666 SCHEME_V->gc_verbose = 0; 5988 SCHEME_V->gc_verbose = 0;
5667 dump_stack_initialize (SCHEME_A); 5989 dump_stack_initialize (SCHEME_A);
5668 SCHEME_V->code = NIL; 5990 SCHEME_V->code = NIL;
5669 SCHEME_V->args = NIL; 5991 SCHEME_V->args = NIL;
5670 SCHEME_V->envir = NIL; 5992 SCHEME_V->envir = NIL;
5993 SCHEME_V->value = NIL;
5671 SCHEME_V->tracing = 0; 5994 SCHEME_V->tracing = 0;
5672 5995
5673 /* init NIL */ 5996 /* init NIL */
5674 set_typeflag (NIL, T_ATOM | T_MARK); 5997 set_typeflag (NIL, T_ATOM | T_MARK);
5675 set_car (NIL, NIL); 5998 set_car (NIL, NIL);
5731 6054
5732 return !SCHEME_V->no_memory; 6055 return !SCHEME_V->no_memory;
5733} 6056}
5734 6057
5735#if USE_PORTS 6058#if USE_PORTS
5736void 6059ecb_cold void
5737scheme_set_input_port_file (SCHEME_P_ int fin) 6060scheme_set_input_port_file (SCHEME_P_ int fin)
5738{ 6061{
5739 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); 6062 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input);
5740} 6063}
5741 6064
5742void 6065ecb_cold void
5743scheme_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)
5744{ 6067{
5745 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);
5746} 6069}
5747 6070
5748void 6071ecb_cold void
5749scheme_set_output_port_file (SCHEME_P_ int fout) 6072scheme_set_output_port_file (SCHEME_P_ int fout)
5750{ 6073{
5751 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); 6074 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output);
5752} 6075}
5753 6076
5754void 6077ecb_cold void
5755scheme_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)
5756{ 6079{
5757 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);
5758} 6081}
5759#endif 6082#endif
5760 6083
5761void 6084ecb_cold void
5762scheme_set_external_data (SCHEME_P_ void *p) 6085scheme_set_external_data (SCHEME_P_ void *p)
5763{ 6086{
5764 SCHEME_V->ext_data = p; 6087 SCHEME_V->ext_data = p;
5765} 6088}
5766 6089
5798 SCHEME_V->loadport = NIL; 6121 SCHEME_V->loadport = NIL;
5799 SCHEME_V->gc_verbose = 0; 6122 SCHEME_V->gc_verbose = 0;
5800 gc (SCHEME_A_ NIL, NIL); 6123 gc (SCHEME_A_ NIL, NIL);
5801 6124
5802 for (i = 0; i <= SCHEME_V->last_cell_seg; i++) 6125 for (i = 0; i <= SCHEME_V->last_cell_seg; i++)
5803 free (SCHEME_V->alloc_seg[i]); 6126 free (SCHEME_V->cell_seg[i]);
5804 6127
5805#if SHOW_ERROR_LINE 6128#if SHOW_ERROR_LINE
5806 for (i = 0; i <= SCHEME_V->file_i; i++) 6129 for (i = 0; i <= SCHEME_V->file_i; i++)
5807 {
5808 if (SCHEME_V->load_stack[i].kind & port_file) 6130 if (SCHEME_V->load_stack[i].kind & port_file)
5809 { 6131 {
5810 fname = SCHEME_V->load_stack[i].rep.stdio.filename; 6132 fname = SCHEME_V->load_stack[i].rep.stdio.filename;
5811 6133
5812 if (fname) 6134 if (fname)
5813 free (fname); 6135 free (fname);
5814 } 6136 }
5815 }
5816#endif 6137#endif
5817} 6138}
5818 6139
5819void 6140ecb_cold void
5820scheme_load_file (SCHEME_P_ int fin) 6141scheme_load_file (SCHEME_P_ int fin)
5821{ 6142{
5822 scheme_load_named_file (SCHEME_A_ fin, 0); 6143 scheme_load_named_file (SCHEME_A_ fin, 0);
5823} 6144}
5824 6145
5825void 6146ecb_cold void
5826scheme_load_named_file (SCHEME_P_ int fin, const char *filename) 6147scheme_load_named_file (SCHEME_P_ int fin, const char *filename)
5827{ 6148{
5828 dump_stack_reset (SCHEME_A); 6149 dump_stack_reset (SCHEME_A);
5829 SCHEME_V->envir = SCHEME_V->global_env; 6150 SCHEME_V->envir = SCHEME_V->global_env;
5830 SCHEME_V->file_i = 0; 6151 SCHEME_V->file_i = 0;
5831 SCHEME_V->load_stack[0].unget = -1; 6152 SCHEME_V->load_stack[0].unget = -1;
5832 SCHEME_V->load_stack[0].kind = port_input | port_file; 6153 SCHEME_V->load_stack[0].kind = port_input | port_file;
5833 SCHEME_V->load_stack[0].rep.stdio.file = fin; 6154 SCHEME_V->load_stack[0].rep.stdio.file = fin;
5834#if USE_PORTS
5835 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6155 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5836#endif
5837 SCHEME_V->retcode = 0; 6156 SCHEME_V->retcode = 0;
5838 6157
5839#if USE_PORTS
5840 if (fin == STDIN_FILENO) 6158 if (fin == STDIN_FILENO)
5841 SCHEME_V->interactive_repl = 1; 6159 SCHEME_V->interactive_repl = 1;
5842#endif
5843 6160
5844#if USE_PORTS 6161#if USE_PORTS
5845#if SHOW_ERROR_LINE 6162#if SHOW_ERROR_LINE
5846 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; 6163 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0;
5847 6164
5851#endif 6168#endif
5852 6169
5853 SCHEME_V->inport = SCHEME_V->loadport; 6170 SCHEME_V->inport = SCHEME_V->loadport;
5854 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6171 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5855 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6172 Eval_Cycle (SCHEME_A_ OP_T0LVL);
6173
5856 set_typeflag (SCHEME_V->loadport, T_ATOM); 6174 set_typeflag (SCHEME_V->loadport, T_ATOM);
5857 6175
5858 if (SCHEME_V->retcode == 0) 6176 if (SCHEME_V->retcode == 0)
5859 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6177 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5860} 6178}
5861 6179
5862void 6180ecb_cold void
5863scheme_load_string (SCHEME_P_ const char *cmd) 6181scheme_load_string (SCHEME_P_ const char *cmd)
5864{ 6182{
6183#if USE_PORTs
5865 dump_stack_reset (SCHEME_A); 6184 dump_stack_reset (SCHEME_A);
5866 SCHEME_V->envir = SCHEME_V->global_env; 6185 SCHEME_V->envir = SCHEME_V->global_env;
5867 SCHEME_V->file_i = 0; 6186 SCHEME_V->file_i = 0;
5868 SCHEME_V->load_stack[0].kind = port_input | port_string; 6187 SCHEME_V->load_stack[0].kind = port_input | port_string;
5869 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 */
5870 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);
5871 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; 6190 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
5872#if USE_PORTS
5873 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6191 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5874#endif
5875 SCHEME_V->retcode = 0; 6192 SCHEME_V->retcode = 0;
5876 SCHEME_V->interactive_repl = 0; 6193 SCHEME_V->interactive_repl = 0;
5877 SCHEME_V->inport = SCHEME_V->loadport; 6194 SCHEME_V->inport = SCHEME_V->loadport;
5878 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6195 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5879 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6196 Eval_Cycle (SCHEME_A_ OP_T0LVL);
5880 set_typeflag (SCHEME_V->loadport, T_ATOM); 6197 set_typeflag (SCHEME_V->loadport, T_ATOM);
5881 6198
5882 if (SCHEME_V->retcode == 0) 6199 if (SCHEME_V->retcode == 0)
5883 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6200 SCHEME_V->retcode = SCHEME_V->nesting != 0;
6201#else
6202 abort ();
6203#endif
5884} 6204}
5885 6205
5886void 6206ecb_cold void
5887scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) 6207scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value)
5888{ 6208{
5889 pointer x; 6209 pointer x;
5890 6210
5891 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 6211 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
5896 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 6216 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
5897} 6217}
5898 6218
5899#if !STANDALONE 6219#if !STANDALONE
5900 6220
5901void 6221ecb_cold void
5902scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 6222scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
5903{ 6223{
5904 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));
5905} 6225}
5906 6226
5907void 6227ecb_cold void
5908scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 6228scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
5909{ 6229{
5910 int i; 6230 int i;
5911 6231
5912 for (i = 0; i < count; i++) 6232 for (i = 0; i < count; i++)
5913 scheme_register_foreign_func (SCHEME_A_ list + i); 6233 scheme_register_foreign_func (SCHEME_A_ list + i);
5914} 6234}
5915 6235
5916pointer 6236ecb_cold pointer
5917scheme_apply0 (SCHEME_P_ const char *procname) 6237scheme_apply0 (SCHEME_P_ const char *procname)
5918{ 6238{
5919 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));
5920} 6240}
5921 6241
5922void 6242ecb_cold void
5923save_from_C_call (SCHEME_P) 6243save_from_C_call (SCHEME_P)
5924{ 6244{
5925 pointer saved_data = cons (car (S_SINK), 6245 pointer saved_data = cons (car (S_SINK),
5926 cons (SCHEME_V->envir, 6246 cons (SCHEME_V->envir,
5927 SCHEME_V->dump)); 6247 SCHEME_V->dump));
5931 /* 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
5932 directly resume pre-C-call operations. */ 6252 directly resume pre-C-call operations. */
5933 dump_stack_reset (SCHEME_A); 6253 dump_stack_reset (SCHEME_A);
5934} 6254}
5935 6255
5936void 6256ecb_cold void
5937restore_from_C_call (SCHEME_P) 6257restore_from_C_call (SCHEME_P)
5938{ 6258{
5939 set_car (S_SINK, caar (SCHEME_V->c_nest)); 6259 set_car (S_SINK, caar (SCHEME_V->c_nest));
5940 SCHEME_V->envir = cadar (SCHEME_V->c_nest); 6260 SCHEME_V->envir = cadar (SCHEME_V->c_nest);
5941 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); 6261 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest));
5942 /* Pop */ 6262 /* Pop */
5943 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); 6263 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest);
5944} 6264}
5945 6265
5946/* "func" and "args" are assumed to be already eval'ed. */ 6266/* "func" and "args" are assumed to be already eval'ed. */
5947pointer 6267ecb_cold pointer
5948scheme_call (SCHEME_P_ pointer func, pointer args) 6268scheme_call (SCHEME_P_ pointer func, pointer args)
5949{ 6269{
5950 int old_repl = SCHEME_V->interactive_repl; 6270 int old_repl = SCHEME_V->interactive_repl;
5951 6271
5952 SCHEME_V->interactive_repl = 0; 6272 SCHEME_V->interactive_repl = 0;
5959 SCHEME_V->interactive_repl = old_repl; 6279 SCHEME_V->interactive_repl = old_repl;
5960 restore_from_C_call (SCHEME_A); 6280 restore_from_C_call (SCHEME_A);
5961 return SCHEME_V->value; 6281 return SCHEME_V->value;
5962} 6282}
5963 6283
5964pointer 6284ecb_cold pointer
5965scheme_eval (SCHEME_P_ pointer obj) 6285scheme_eval (SCHEME_P_ pointer obj)
5966{ 6286{
5967 int old_repl = SCHEME_V->interactive_repl; 6287 int old_repl = SCHEME_V->interactive_repl;
5968 6288
5969 SCHEME_V->interactive_repl = 0; 6289 SCHEME_V->interactive_repl = 0;
5981 6301
5982/* ========== Main ========== */ 6302/* ========== Main ========== */
5983 6303
5984#if STANDALONE 6304#if STANDALONE
5985 6305
5986int 6306ecb_cold int
5987main (int argc, char **argv) 6307main (int argc, char **argv)
5988{ 6308{
5989# if USE_MULTIPLICITY 6309# if USE_MULTIPLICITY
5990 scheme ssc; 6310 scheme ssc;
5991 scheme *const SCHEME_V = &ssc; 6311 scheme *const SCHEME_V = &ssc;
5993# endif 6313# endif
5994 int fin; 6314 int fin;
5995 char *file_name = InitFile; 6315 char *file_name = InitFile;
5996 int retcode; 6316 int retcode;
5997 int isfile = 1; 6317 int isfile = 1;
6318#if EXPERIMENT
5998 system ("ps v $PPID");//D 6319 system ("ps v $PPID");
6320#endif
5999 6321
6000 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6322 if (argc == 2 && strcmp (argv[1], "-?") == 0)
6001 { 6323 {
6002 putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); 6324 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
6003 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); 6325 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
6032 } 6354 }
6033#endif 6355#endif
6034 6356
6035 do 6357 do
6036 { 6358 {
6037#if USE_PORTS
6038 if (strcmp (file_name, "-") == 0) 6359 if (strcmp (file_name, "-") == 0)
6039 fin = STDIN_FILENO; 6360 fin = STDIN_FILENO;
6040 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)
6041 { 6362 {
6042 pointer args = NIL; 6363 pointer args = NIL;
6060 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);
6061 6382
6062 } 6383 }
6063 else 6384 else
6064 fin = open (file_name, O_RDONLY); 6385 fin = open (file_name, O_RDONLY);
6065#endif
6066 6386
6067 if (isfile && fin < 0) 6387 if (isfile && fin < 0)
6068 { 6388 {
6069 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');
6070 } 6392 }
6071 else 6393 else
6072 { 6394 {
6073 if (isfile) 6395 if (isfile)
6074 scheme_load_named_file (SCHEME_A_ fin, file_name); 6396 scheme_load_named_file (SCHEME_A_ fin, file_name);
6075 else 6397 else
6076 scheme_load_string (SCHEME_A_ file_name); 6398 scheme_load_string (SCHEME_A_ file_name);
6077 6399
6078#if USE_PORTS
6079 if (!isfile || fin != STDIN_FILENO) 6400 if (!isfile || fin != STDIN_FILENO)
6080 { 6401 {
6081 if (SCHEME_V->retcode != 0) 6402 if (SCHEME_V->retcode != 0)
6082 { 6403 {
6083 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');
6084 } 6407 }
6085 6408
6086 if (isfile) 6409 if (isfile)
6087 close (fin); 6410 close (fin);
6088 } 6411 }
6089#endif
6090 } 6412 }
6091 6413
6092 file_name = *argv++; 6414 file_name = *argv++;
6093 } 6415 }
6094 while (file_name != 0); 6416 while (file_name != 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines