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.53 by root, Tue Dec 1 02:21:49 2015 UTC vs.
Revision 1.64 by root, Wed Dec 2 17:01:18 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines