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.61 by root, Wed Dec 2 07:43:46 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>
49#include <string.h> 47#include <string.h>
50 48
51#include <limits.h> 49#include <limits.h>
52#include <inttypes.h> 50#include <inttypes.h>
53#include <float.h> 51#include <float.h>
54//#include <ctype.h> 52
53#if !USE_SYSTEM_MALLOC
54# define PAGE_SIZE 4096 /* does not work on sparc/alpha */
55# include "malloc.c"
56# define malloc(n) tiny_malloc (n)
57# define realloc(p,n) tiny_realloc (p, n)
58# define free(p) tiny_free (p)
59#endif
55 60
56#if '1' != '0' + 1 \ 61#if '1' != '0' + 1 \
57 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ 62 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \
58 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ 63 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \
59 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ 64 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \
91 96
92#if !USE_MULTIPLICITY 97#if !USE_MULTIPLICITY
93static scheme sc; 98static scheme sc;
94#endif 99#endif
95 100
96static void 101ecb_cold static void
97xbase (char *s, long n, int base) 102xbase (char *s, long n, int base)
98{ 103{
99 if (n < 0) 104 if (n < 0)
100 { 105 {
101 *s++ = '-'; 106 *s++ = '-';
116 char x = *s; *s = *p; *p = x; 121 char x = *s; *s = *p; *p = x;
117 --p; ++s; 122 --p; ++s;
118 } 123 }
119} 124}
120 125
121static void 126ecb_cold static void
122xnum (char *s, long n) 127xnum (char *s, long n)
123{ 128{
124 xbase (s, n, 10); 129 xbase (s, n, 10);
125} 130}
126 131
127static void 132ecb_cold static void
128putnum (SCHEME_P_ long n) 133putnum (SCHEME_P_ long n)
129{ 134{
130 char buf[64]; 135 char buf[64];
131 136
132 xnum (buf, n); 137 xnum (buf, n);
133 putstr (SCHEME_A_ buf); 138 putstr (SCHEME_A_ buf);
134} 139}
140
141#if USE_CHAR_CLASSIFIERS
142#include <ctype.h>
143#else
135 144
136static char 145static char
137xtoupper (char c) 146xtoupper (char c)
138{ 147{
139 if (c >= 'a' && c <= 'z') 148 if (c >= 'a' && c <= 'z')
159 168
160#define toupper(c) xtoupper (c) 169#define toupper(c) xtoupper (c)
161#define tolower(c) xtolower (c) 170#define tolower(c) xtolower (c)
162#define isdigit(c) xisdigit (c) 171#define isdigit(c) xisdigit (c)
163 172
173#endif
174
164#if USE_IGNORECASE 175#if USE_IGNORECASE
165static const char * 176ecb_cold static const char *
166xstrlwr (char *s) 177xstrlwr (char *s)
167{ 178{
168 const char *p = s; 179 const char *p = s;
169 180
170 while (*s) 181 while (*s)
193#endif 204#endif
194 205
195enum scheme_types 206enum scheme_types
196{ 207{
197 T_INTEGER, 208 T_INTEGER,
209 T_CHARACTER,
198 T_REAL, 210 T_REAL,
199 T_STRING, 211 T_STRING,
200 T_SYMBOL, 212 T_SYMBOL,
201 T_PROC, 213 T_PROC,
202 T_PAIR, /* also used for free cells */ 214 T_PAIR, /* also used for free cells */
203 T_CLOSURE, 215 T_CLOSURE,
216 T_BYTECODE, // temp
217 T_MACRO,
204 T_CONTINUATION, 218 T_CONTINUATION,
205 T_FOREIGN, 219 T_FOREIGN,
206 T_CHARACTER,
207 T_PORT, 220 T_PORT,
208 T_VECTOR, 221 T_VECTOR,
209 T_MACRO,
210 T_PROMISE, 222 T_PROMISE,
211 T_ENVIRONMENT, 223 T_ENVIRONMENT,
212 /* one more... */ 224
213 T_NUM_SYSTEM_TYPES 225 T_NUM_SYSTEM_TYPES
214}; 226};
215 227
216#define T_MASKTYPE 0x000f 228#define T_MASKTYPE 0x000f
217#define T_SYNTAX 0x0010 229#define T_SYNTAX 0x0010
250static num num_op (enum num_op op, num a, num b); 262static num num_op (enum num_op op, num a, num b);
251static num num_intdiv (num a, num b); 263static num num_intdiv (num a, num b);
252static num num_rem (num a, num b); 264static num num_rem (num a, num b);
253static num num_mod (num a, num b); 265static num num_mod (num a, num b);
254 266
255#if USE_MATH
256static double round_per_R5RS (double x);
257#endif
258static int is_zero_rvalue (RVALUE x); 267static int is_zero_rvalue (RVALUE x);
259 268
260static num num_zero; 269static num num_zero;
261static num num_one; 270static num num_one;
262 271
523 proper list: length 532 proper list: length
524 circular list: -1 533 circular list: -1
525 not even a pair: -2 534 not even a pair: -2
526 dotted list: -2 minus length before dot 535 dotted list: -2 minus length before dot
527*/ 536*/
528INTERFACE int 537ecb_hot INTERFACE int
529list_length (SCHEME_P_ pointer a) 538list_length (SCHEME_P_ pointer a)
530{ 539{
531 int i = 0; 540 int i = 0;
532 pointer slow, fast; 541 pointer slow, fast;
533 542
572{ 581{
573 return list_length (SCHEME_A_ a) >= 0; 582 return list_length (SCHEME_A_ a) >= 0;
574} 583}
575 584
576#if USE_CHAR_CLASSIFIERS 585#if USE_CHAR_CLASSIFIERS
586
577ecb_inline int 587ecb_inline int
578Cisalpha (int c) 588Cisalpha (int c)
579{ 589{
580 return isascii (c) && isalpha (c); 590 return isascii (c) && isalpha (c);
581} 591}
639 "gs", 649 "gs",
640 "rs", 650 "rs",
641 "us" 651 "us"
642}; 652};
643 653
644static int 654ecb_cold static int
645is_ascii_name (const char *name, int *pc) 655is_ascii_name (const char *name, int *pc)
646{ 656{
647 int i; 657 int i;
648 658
649 for (i = 0; i < 32; i++) 659 for (i = 0; i < 32; i++)
671static int file_interactive (SCHEME_P); 681static int file_interactive (SCHEME_P);
672ecb_inline int is_one_of (const char *s, int c); 682ecb_inline int is_one_of (const char *s, int c);
673static int alloc_cellseg (SCHEME_P); 683static int alloc_cellseg (SCHEME_P);
674ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); 684ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
675static void finalize_cell (SCHEME_P_ pointer a); 685static 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); 686static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
678static pointer mk_number (SCHEME_P_ const num n); 687static pointer mk_number (SCHEME_P_ const num n);
679static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); 688static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
680static pointer mk_vector (SCHEME_P_ uint32_t len); 689static pointer mk_vector (SCHEME_P_ uint32_t len);
681static pointer mk_atom (SCHEME_P_ char *q); 690static pointer mk_atom (SCHEME_P_ char *q);
682static pointer mk_sharp_const (SCHEME_P_ char *name); 691static pointer mk_sharp_const (SCHEME_P_ char *name);
683 692
693static pointer mk_port (SCHEME_P_ port *p);
694
684#if USE_PORTS 695#if USE_PORTS
685static pointer mk_port (SCHEME_P_ port *p);
686static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); 696static pointer port_from_filename (SCHEME_P_ const char *fn, int prop);
687static pointer port_from_file (SCHEME_P_ int, int prop); 697static pointer port_from_file (SCHEME_P_ int, int prop);
688static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); 698static 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); 699static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop);
690static port *port_rep_from_file (SCHEME_P_ int, int prop); 700static 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); 701static 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); 702static void port_close (SCHEME_P_ pointer p, int flag);
693#endif 703#endif
704
694static void mark (pointer a); 705static void mark (pointer a);
695static void gc (SCHEME_P_ pointer a, pointer b); 706static void gc (SCHEME_P_ pointer a, pointer b);
696static int basic_inchar (port *pt); 707static int basic_inchar (port *pt);
697static int inchar (SCHEME_P); 708static int inchar (SCHEME_P);
698static void backchar (SCHEME_P_ int c); 709static void backchar (SCHEME_P_ int c);
872 } 883 }
873 884
874 return ret; 885 return ret;
875} 886}
876 887
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 888static int
903is_zero_rvalue (RVALUE x) 889is_zero_rvalue (RVALUE x)
904{ 890{
905 return x == 0; 891 return x == 0;
906#if 0 892#if 0
911#endif 897#endif
912#endif 898#endif
913} 899}
914 900
915/* allocate new cell segment */ 901/* allocate new cell segment */
916static int 902ecb_cold static int
917alloc_cellseg (SCHEME_P) 903alloc_cellseg (SCHEME_P)
918{ 904{
919 struct cell *newp; 905 struct cell *newp;
920 struct cell *last; 906 struct cell *last;
921 struct cell *p; 907 struct cell *p;
963 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 949 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
964 return S_SINK; 950 return S_SINK;
965 951
966 if (SCHEME_V->free_cell == NIL) 952 if (SCHEME_V->free_cell == NIL)
967 { 953 {
968 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1; 954 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 2;
969 955
970 gc (SCHEME_A_ a, b); 956 gc (SCHEME_A_ a, b);
971 957
972 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) 958 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL)
973 { 959 {
992 } 978 }
993} 979}
994 980
995/* To retain recent allocs before interpreter knows about them - 981/* To retain recent allocs before interpreter knows about them -
996 Tehom */ 982 Tehom */
997static void 983ecb_hot static void
998push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 984push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
999{ 985{
1000 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 986 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
1001 987
1002 set_typeflag (holder, T_PAIR); 988 set_typeflag (holder, T_PAIR);
1004 set_car (holder, recent); 990 set_car (holder, recent);
1005 set_cdr (holder, car (S_SINK)); 991 set_cdr (holder, car (S_SINK));
1006 set_car (S_SINK, holder); 992 set_car (S_SINK, holder);
1007} 993}
1008 994
1009static pointer 995ecb_hot static pointer
1010get_cell (SCHEME_P_ pointer a, pointer b) 996get_cell (SCHEME_P_ pointer a, pointer b)
1011{ 997{
1012 pointer cell = get_cell_x (SCHEME_A_ a, b); 998 pointer cell = get_cell_x (SCHEME_A_ a, b);
1013 999
1014 /* For right now, include "a" and "b" in "cell" so that gc doesn't 1000 /* For right now, include "a" and "b" in "cell" so that gc doesn't
1071#endif 1057#endif
1072 1058
1073/* Medium level cell allocation */ 1059/* Medium level cell allocation */
1074 1060
1075/* get new cons cell */ 1061/* get new cons cell */
1076pointer 1062ecb_hot static pointer
1077xcons (SCHEME_P_ pointer a, pointer b, int immutable) 1063xcons (SCHEME_P_ pointer a, pointer b)
1078{ 1064{
1079 pointer x = get_cell (SCHEME_A_ a, b); 1065 pointer x = get_cell (SCHEME_A_ a, b);
1080 1066
1081 set_typeflag (x, T_PAIR); 1067 set_typeflag (x, T_PAIR);
1082
1083 if (immutable)
1084 setimmutable (x);
1085 1068
1086 set_car (x, a); 1069 set_car (x, a);
1087 set_cdr (x, b); 1070 set_cdr (x, b);
1088 1071
1089 return x; 1072 return x;
1090} 1073}
1091 1074
1092static pointer 1075ecb_hot static pointer
1076ximmutable_cons (SCHEME_P_ pointer a, pointer b)
1077{
1078 pointer x = xcons (SCHEME_A_ a, b);
1079 setimmutable (x);
1080 return x;
1081}
1082
1083#define cons(a,b) xcons (SCHEME_A_ a, b)
1084#define immutable_cons(a,b) ximmutable_cons (SCHEME_A_ a, b)
1085
1086ecb_cold static pointer
1093generate_symbol (SCHEME_P_ const char *name) 1087generate_symbol (SCHEME_P_ const char *name)
1094{ 1088{
1095 pointer x = mk_string (SCHEME_A_ name); 1089 pointer x = mk_string (SCHEME_A_ name);
1096 setimmutable (x); 1090 setimmutable (x);
1097 set_typeflag (x, T_SYMBOL | T_ATOM); 1091 set_typeflag (x, T_SYMBOL | T_ATOM);
1103#ifndef USE_OBJECT_LIST 1097#ifndef USE_OBJECT_LIST
1104 1098
1105static int 1099static int
1106hash_fn (const char *key, int table_size) 1100hash_fn (const char *key, int table_size)
1107{ 1101{
1108 const unsigned char *p = key; 1102 const unsigned char *p = (unsigned char *)key;
1109 uint32_t hash = 2166136261; 1103 uint32_t hash = 2166136261;
1110 1104
1111 while (*p) 1105 while (*p)
1112 hash = (hash ^ *p++) * 16777619; 1106 hash = (hash ^ *p++) * 16777619;
1113 1107
1114 return hash % table_size; 1108 return hash % table_size;
1115} 1109}
1116 1110
1117static pointer 1111ecb_cold static pointer
1118oblist_initial_value (SCHEME_P) 1112oblist_initial_value (SCHEME_P)
1119{ 1113{
1120 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1114 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1121} 1115}
1122 1116
1123/* returns the new symbol */ 1117/* returns the new symbol */
1124static pointer 1118ecb_cold static pointer
1125oblist_add_by_name (SCHEME_P_ const char *name) 1119oblist_add_by_name (SCHEME_P_ const char *name)
1126{ 1120{
1127 pointer x = generate_symbol (SCHEME_A_ name); 1121 pointer x = generate_symbol (SCHEME_A_ name);
1128 int location = hash_fn (name, veclength (SCHEME_V->oblist)); 1122 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))); 1123 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1130 return x; 1124 return x;
1131} 1125}
1132 1126
1133ecb_inline pointer 1127ecb_cold static pointer
1134oblist_find_by_name (SCHEME_P_ const char *name) 1128oblist_find_by_name (SCHEME_P_ const char *name)
1135{ 1129{
1136 int location; 1130 int location;
1137 pointer x; 1131 pointer x;
1138 char *s; 1132 char *s;
1149 } 1143 }
1150 1144
1151 return NIL; 1145 return NIL;
1152} 1146}
1153 1147
1154static pointer 1148ecb_cold static pointer
1155oblist_all_symbols (SCHEME_P) 1149oblist_all_symbols (SCHEME_P)
1156{ 1150{
1157 int i; 1151 int i;
1158 pointer x; 1152 pointer x;
1159 pointer ob_list = NIL; 1153 pointer ob_list = NIL;
1165 return ob_list; 1159 return ob_list;
1166} 1160}
1167 1161
1168#else 1162#else
1169 1163
1170static pointer 1164ecb_cold static pointer
1171oblist_initial_value (SCHEME_P) 1165oblist_initial_value (SCHEME_P)
1172{ 1166{
1173 return NIL; 1167 return NIL;
1174} 1168}
1175 1169
1176ecb_inline pointer 1170ecb_cold static pointer
1177oblist_find_by_name (SCHEME_P_ const char *name) 1171oblist_find_by_name (SCHEME_P_ const char *name)
1178{ 1172{
1179 pointer x; 1173 pointer x;
1180 char *s; 1174 char *s;
1181 1175
1190 1184
1191 return NIL; 1185 return NIL;
1192} 1186}
1193 1187
1194/* returns the new symbol */ 1188/* returns the new symbol */
1195static pointer 1189ecb_cold static pointer
1196oblist_add_by_name (SCHEME_P_ const char *name) 1190oblist_add_by_name (SCHEME_P_ const char *name)
1197{ 1191{
1198 pointer x = generate_symbol (SCHEME_A_ name); 1192 pointer x = generate_symbol (SCHEME_A_ name);
1199 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1193 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1200 return x; 1194 return x;
1201} 1195}
1202 1196
1203static pointer 1197ecb_cold static pointer
1204oblist_all_symbols (SCHEME_P) 1198oblist_all_symbols (SCHEME_P)
1205{ 1199{
1206 return SCHEME_V->oblist; 1200 return SCHEME_V->oblist;
1207} 1201}
1208 1202
1209#endif 1203#endif
1210 1204
1211#if USE_PORTS
1212static pointer 1205ecb_cold static pointer
1213mk_port (SCHEME_P_ port *p) 1206mk_port (SCHEME_P_ port *p)
1214{ 1207{
1215 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1208 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1216 1209
1217 set_typeflag (x, T_PORT | T_ATOM); 1210 set_typeflag (x, T_PORT | T_ATOM);
1218 set_port (x, p); 1211 set_port (x, p);
1219 1212
1220 return x; 1213 return x;
1221} 1214}
1222#endif
1223 1215
1224pointer 1216ecb_cold pointer
1225mk_foreign_func (SCHEME_P_ foreign_func f) 1217mk_foreign_func (SCHEME_P_ foreign_func f)
1226{ 1218{
1227 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1219 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1228 1220
1229 set_typeflag (x, T_FOREIGN | T_ATOM); 1221 set_typeflag (x, T_FOREIGN | T_ATOM);
1394 x = oblist_add_by_name (SCHEME_A_ name); 1386 x = oblist_add_by_name (SCHEME_A_ name);
1395 1387
1396 return x; 1388 return x;
1397} 1389}
1398 1390
1399INTERFACE pointer 1391ecb_cold INTERFACE pointer
1400gensym (SCHEME_P) 1392gensym (SCHEME_P)
1401{ 1393{
1402 pointer x; 1394 pointer x;
1403 char name[40] = "gensym-"; 1395 char name[40] = "gensym-";
1404 xnum (name + 7, ++SCHEME_V->gensym_cnt); 1396 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1411{ 1403{
1412 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; 1404 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1413} 1405}
1414 1406
1415/* make symbol or number atom from string */ 1407/* make symbol or number atom from string */
1416static pointer 1408ecb_cold static pointer
1417mk_atom (SCHEME_P_ char *q) 1409mk_atom (SCHEME_P_ char *q)
1418{ 1410{
1419 char c, *p; 1411 char c, *p;
1420 int has_dec_point = 0; 1412 int has_dec_point = 0;
1421 int has_fp_exp = 0; 1413 int has_fp_exp = 0;
1492 1484
1493 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1485 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1494} 1486}
1495 1487
1496/* make constant */ 1488/* make constant */
1497static pointer 1489ecb_cold static pointer
1498mk_sharp_const (SCHEME_P_ char *name) 1490mk_sharp_const (SCHEME_P_ char *name)
1499{ 1491{
1500 if (!strcmp (name, "t")) 1492 if (!strcmp (name, "t"))
1501 return S_T; 1493 return S_T;
1502 else if (!strcmp (name, "f")) 1494 else if (!strcmp (name, "f"))
1503 return S_F; 1495 return S_F;
1504 else if (*name == '\\') /* #\w (character) */ 1496 else if (*name == '\\') /* #\w (character) */
1505 { 1497 {
1506 int c; 1498 int c;
1507 1499
1500 // TODO: optimise
1508 if (stricmp (name + 1, "space") == 0) 1501 if (stricmp (name + 1, "space") == 0)
1509 c = ' '; 1502 c = ' ';
1510 else if (stricmp (name + 1, "newline") == 0) 1503 else if (stricmp (name + 1, "newline") == 0)
1511 c = '\n'; 1504 c = '\n';
1512 else if (stricmp (name + 1, "return") == 0) 1505 else if (stricmp (name + 1, "return") == 0)
1513 c = '\r'; 1506 c = '\r';
1514 else if (stricmp (name + 1, "tab") == 0) 1507 else if (stricmp (name + 1, "tab") == 0)
1515 c = '\t'; 1508 c = '\t';
1509 else if (stricmp (name + 1, "alarm") == 0)
1510 c = 0x07;
1511 else if (stricmp (name + 1, "backspace") == 0)
1512 c = 0x08;
1513 else if (stricmp (name + 1, "escape") == 0)
1514 c = 0x1b;
1515 else if (stricmp (name + 1, "delete") == 0)
1516 c = 0x7f;
1517 else if (stricmp (name + 1, "null") == 0)
1518 c = 0;
1516 else if (name[1] == 'x' && name[2] != 0) 1519 else if (name[1] == 'x' && name[2] != 0)
1517 { 1520 {
1518 long c1 = strtol (name + 2, 0, 16); 1521 long c1 = strtol (name + 2, 0, 16);
1519 1522
1520 if (0 <= c1 && c1 <= UCHAR_MAX) 1523 if (0 <= c1 && c1 <= UCHAR_MAX)
1545 return NIL; 1548 return NIL;
1546 } 1549 }
1547} 1550}
1548 1551
1549/* ========== garbage collector ========== */ 1552/* ========== garbage collector ========== */
1553
1554static void
1555finalize_cell (SCHEME_P_ pointer a)
1556{
1557 /* TODO, fast bitmap check? */
1558 if (is_string (a) || is_symbol (a))
1559 free (strvalue (a));
1560 else if (is_vector (a))
1561 free (vecvalue (a));
1562#if USE_PORTS
1563 else if (is_port (a))
1564 {
1565 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit)
1566 port_close (SCHEME_A_ a, port_input | port_output);
1567
1568 free (port (a));
1569 }
1570#endif
1571}
1550 1572
1551/*-- 1573/*--
1552 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, 1574 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1,
1553 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1575 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1554 * for marking. 1576 * for marking.
1555 * 1577 *
1556 * The exception is vectors - vectors are currently marked recursively, 1578 * The exception is vectors - vectors are currently marked recursively,
1557 * which is inherited form tinyscheme and could be fixed by having another 1579 * which is inherited form tinyscheme and could be fixed by having another
1558 * word of context in the vector 1580 * word of context in the vector
1559 */ 1581 */
1560static void 1582ecb_hot static void
1561mark (pointer a) 1583mark (pointer a)
1562{ 1584{
1563 pointer t, q, p; 1585 pointer t, q, p;
1564 1586
1565 t = 0; 1587 t = 0;
1622 p = q; 1644 p = q;
1623 goto E6; 1645 goto E6;
1624 } 1646 }
1625} 1647}
1626 1648
1627/* garbage collection. parameter a, b is marked. */ 1649ecb_hot static void
1628static void 1650gc_free (SCHEME_P)
1629gc (SCHEME_P_ pointer a, pointer b)
1630{ 1651{
1631 int i; 1652 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; 1653 uint32_t total = 0;
1676 1654
1677 /* Here we scan the cells to build the free-list. */ 1655 /* Here we scan the cells to build the free-list. */
1678 for (i = SCHEME_V->last_cell_seg; i >= 0; i--) 1656 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1679 { 1657 {
1708 { 1686 {
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"); 1687 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 } 1688 }
1711} 1689}
1712 1690
1713static void 1691/* garbage collection. parameter a, b is marked. */
1714finalize_cell (SCHEME_P_ pointer a) 1692ecb_cold static void
1693gc (SCHEME_P_ pointer a, pointer b)
1715{ 1694{
1716 /* TODO, fast bitmap check? */ 1695 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 1696
1727 free (port (a)); 1697 if (SCHEME_V->gc_verbose)
1728 } 1698 putstr (SCHEME_A_ "gc...");
1699
1700 /* mark system globals */
1701 mark (SCHEME_V->oblist);
1702 mark (SCHEME_V->global_env);
1703
1704 /* mark current registers */
1705 mark (SCHEME_V->args);
1706 mark (SCHEME_V->envir);
1707 mark (SCHEME_V->code);
1708 dump_stack_mark (SCHEME_A);
1709 mark (SCHEME_V->value);
1710 mark (SCHEME_V->inport);
1711 mark (SCHEME_V->save_inport);
1712 mark (SCHEME_V->outport);
1713 mark (SCHEME_V->loadport);
1714
1715 /* Mark recent objects the interpreter doesn't know about yet. */
1716 mark (car (S_SINK));
1717 /* Mark any older stuff above nested C calls */
1718 mark (SCHEME_V->c_nest);
1719
1720#if USE_INTCACHE
1721 /* mark intcache */
1722 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1723 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1724 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1729#endif 1725#endif
1726
1727 /* mark variables a, b */
1728 mark (a);
1729 mark (b);
1730
1731 /* garbage collect */
1732 clrmark (NIL);
1733 SCHEME_V->fcells = 0;
1734 SCHEME_V->free_cell = NIL;
1735
1736 if (SCHEME_V->gc_verbose)
1737 putstr (SCHEME_A_ "freeing...");
1738
1739 gc_free (SCHEME_A);
1730} 1740}
1731 1741
1732/* ========== Routines for Reading ========== */ 1742/* ========== Routines for Reading ========== */
1733 1743
1734static int 1744ecb_cold static int
1735file_push (SCHEME_P_ const char *fname) 1745file_push (SCHEME_P_ const char *fname)
1736{ 1746{
1737#if USE_PORTS
1738 int fin; 1747 int fin;
1739 1748
1740 if (SCHEME_V->file_i == MAXFIL - 1) 1749 if (SCHEME_V->file_i == MAXFIL - 1)
1741 return 0; 1750 return 0;
1742 1751
1759 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0); 1768 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0);
1760#endif 1769#endif
1761 } 1770 }
1762 1771
1763 return fin >= 0; 1772 return fin >= 0;
1764
1765#else
1766 return 1;
1767#endif
1768} 1773}
1769 1774
1770static void 1775ecb_cold static void
1771file_pop (SCHEME_P) 1776file_pop (SCHEME_P)
1772{ 1777{
1773 if (SCHEME_V->file_i != 0) 1778 if (SCHEME_V->file_i != 0)
1774 { 1779 {
1775 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1780 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1779 SCHEME_V->file_i--; 1784 SCHEME_V->file_i--;
1780 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); 1785 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1781 } 1786 }
1782} 1787}
1783 1788
1784static int 1789ecb_cold static int
1785file_interactive (SCHEME_P) 1790file_interactive (SCHEME_P)
1786{ 1791{
1787#if USE_PORTS 1792#if USE_PORTS
1788 return SCHEME_V->file_i == 0 1793 return SCHEME_V->file_i == 0
1789 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1794 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1792 return 0; 1797 return 0;
1793#endif 1798#endif
1794} 1799}
1795 1800
1796#if USE_PORTS 1801#if USE_PORTS
1797static port * 1802ecb_cold static port *
1798port_rep_from_filename (SCHEME_P_ const char *fn, int prop) 1803port_rep_from_filename (SCHEME_P_ const char *fn, int prop)
1799{ 1804{
1800 int fd; 1805 int fd;
1801 int flags; 1806 int flags;
1802 char *rw; 1807 char *rw;
1825# endif 1830# endif
1826 1831
1827 return pt; 1832 return pt;
1828} 1833}
1829 1834
1830static pointer 1835ecb_cold static pointer
1831port_from_filename (SCHEME_P_ const char *fn, int prop) 1836port_from_filename (SCHEME_P_ const char *fn, int prop)
1832{ 1837{
1833 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); 1838 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop);
1834 1839
1835 if (!pt && USE_ERROR_CHECKING) 1840 if (!pt && USE_ERROR_CHECKING)
1836 return NIL; 1841 return NIL;
1837 1842
1838 return mk_port (SCHEME_A_ pt); 1843 return mk_port (SCHEME_A_ pt);
1839} 1844}
1840 1845
1841static port * 1846ecb_cold static port *
1842port_rep_from_file (SCHEME_P_ int f, int prop) 1847port_rep_from_file (SCHEME_P_ int f, int prop)
1843{ 1848{
1844 port *pt = malloc (sizeof *pt); 1849 port *pt = malloc (sizeof *pt);
1845 1850
1846 if (!pt && USE_ERROR_CHECKING) 1851 if (!pt && USE_ERROR_CHECKING)
1851 pt->rep.stdio.file = f; 1856 pt->rep.stdio.file = f;
1852 pt->rep.stdio.closeit = 0; 1857 pt->rep.stdio.closeit = 0;
1853 return pt; 1858 return pt;
1854} 1859}
1855 1860
1856static pointer 1861ecb_cold static pointer
1857port_from_file (SCHEME_P_ int f, int prop) 1862port_from_file (SCHEME_P_ int f, int prop)
1858{ 1863{
1859 port *pt = port_rep_from_file (SCHEME_A_ f, prop); 1864 port *pt = port_rep_from_file (SCHEME_A_ f, prop);
1860 1865
1861 if (!pt && USE_ERROR_CHECKING) 1866 if (!pt && USE_ERROR_CHECKING)
1862 return NIL; 1867 return NIL;
1863 1868
1864 return mk_port (SCHEME_A_ pt); 1869 return mk_port (SCHEME_A_ pt);
1865} 1870}
1866 1871
1867static port * 1872ecb_cold static port *
1868port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1873port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
1869{ 1874{
1870 port *pt = malloc (sizeof (port)); 1875 port *pt = malloc (sizeof (port));
1871 1876
1872 if (!pt && USE_ERROR_CHECKING) 1877 if (!pt && USE_ERROR_CHECKING)
1878 pt->rep.string.curr = start; 1883 pt->rep.string.curr = start;
1879 pt->rep.string.past_the_end = past_the_end; 1884 pt->rep.string.past_the_end = past_the_end;
1880 return pt; 1885 return pt;
1881} 1886}
1882 1887
1883static pointer 1888ecb_cold static pointer
1884port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1889port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
1885{ 1890{
1886 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); 1891 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop);
1887 1892
1888 if (!pt && USE_ERROR_CHECKING) 1893 if (!pt && USE_ERROR_CHECKING)
1891 return mk_port (SCHEME_A_ pt); 1896 return mk_port (SCHEME_A_ pt);
1892} 1897}
1893 1898
1894# define BLOCK_SIZE 256 1899# define BLOCK_SIZE 256
1895 1900
1896static port * 1901ecb_cold static port *
1897port_rep_from_scratch (SCHEME_P) 1902port_rep_from_scratch (SCHEME_P)
1898{ 1903{
1899 char *start; 1904 char *start;
1900 port *pt = malloc (sizeof (port)); 1905 port *pt = malloc (sizeof (port));
1901 1906
1915 pt->rep.string.curr = start; 1920 pt->rep.string.curr = start;
1916 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; 1921 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1;
1917 return pt; 1922 return pt;
1918} 1923}
1919 1924
1920static pointer 1925ecb_cold static pointer
1921port_from_scratch (SCHEME_P) 1926port_from_scratch (SCHEME_P)
1922{ 1927{
1923 port *pt = port_rep_from_scratch (SCHEME_A); 1928 port *pt = port_rep_from_scratch (SCHEME_A);
1924 1929
1925 if (!pt && USE_ERROR_CHECKING) 1930 if (!pt && USE_ERROR_CHECKING)
1926 return NIL; 1931 return NIL;
1927 1932
1928 return mk_port (SCHEME_A_ pt); 1933 return mk_port (SCHEME_A_ pt);
1929} 1934}
1930 1935
1931static void 1936ecb_cold static void
1932port_close (SCHEME_P_ pointer p, int flag) 1937port_close (SCHEME_P_ pointer p, int flag)
1933{ 1938{
1934 port *pt = port (p); 1939 port *pt = port (p);
1935 1940
1936 pt->kind &= ~flag; 1941 pt->kind &= ~flag;
1956 } 1961 }
1957} 1962}
1958#endif 1963#endif
1959 1964
1960/* get new character from input file */ 1965/* get new character from input file */
1961static int 1966ecb_cold static int
1962inchar (SCHEME_P) 1967inchar (SCHEME_P)
1963{ 1968{
1964 int c; 1969 int c;
1965 port *pt = port (SCHEME_V->inport); 1970 port *pt = port (SCHEME_V->inport);
1966 1971
1980 } 1985 }
1981 1986
1982 return c; 1987 return c;
1983} 1988}
1984 1989
1985static int ungot = -1; 1990ecb_cold static int
1986
1987static int
1988basic_inchar (port *pt) 1991basic_inchar (port *pt)
1989{ 1992{
1990#if USE_PORTS
1991 if (pt->unget != -1) 1993 if (pt->unget != -1)
1992 { 1994 {
1993 int r = pt->unget; 1995 int r = pt->unget;
1994 pt->unget = -1; 1996 pt->unget = -1;
1995 return r; 1997 return r;
1996 } 1998 }
1997 1999
2000#if USE_PORTS
1998 if (pt->kind & port_file) 2001 if (pt->kind & port_file)
1999 { 2002 {
2000 char c; 2003 char c;
2001 2004
2002 if (!read (pt->rep.stdio.file, &c, 1)) 2005 if (!read (pt->rep.stdio.file, &c, 1))
2010 return EOF; 2013 return EOF;
2011 else 2014 else
2012 return *pt->rep.string.curr++; 2015 return *pt->rep.string.curr++;
2013 } 2016 }
2014#else 2017#else
2015 if (ungot == -1)
2016 {
2017 char c; 2018 char c;
2018 if (!read (0, &c, 1)) 2019
2020 if (!read (pt->rep.stdio.file, &c, 1))
2019 return EOF; 2021 return EOF;
2020 2022
2021 ungot = c;
2022 }
2023
2024 {
2025 int r = ungot;
2026 ungot = -1;
2027 return r; 2023 return c;
2028 }
2029#endif 2024#endif
2030} 2025}
2031 2026
2032/* back character to input buffer */ 2027/* back character to input buffer */
2033static void 2028ecb_cold static void
2034backchar (SCHEME_P_ int c) 2029backchar (SCHEME_P_ int c)
2035{ 2030{
2036#if USE_PORTS 2031 port *pt = port (SCHEME_V->inport);
2037 port *pt;
2038 2032
2039 if (c == EOF) 2033 if (c == EOF)
2040 return; 2034 return;
2041 2035
2042 pt = port (SCHEME_V->inport);
2043 pt->unget = c; 2036 pt->unget = c;
2044#else
2045 if (c == EOF)
2046 return;
2047
2048 ungot = c;
2049#endif
2050} 2037}
2051 2038
2052#if USE_PORTS 2039#if USE_PORTS
2053static int 2040ecb_cold static int
2054realloc_port_string (SCHEME_P_ port *p) 2041realloc_port_string (SCHEME_P_ port *p)
2055{ 2042{
2056 char *start = p->rep.string.start; 2043 char *start = p->rep.string.start;
2057 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; 2044 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE;
2058 char *str = malloc (new_size); 2045 char *str = malloc (new_size);
2071 else 2058 else
2072 return 0; 2059 return 0;
2073} 2060}
2074#endif 2061#endif
2075 2062
2076INTERFACE void 2063ecb_cold static void
2077putstr (SCHEME_P_ const char *s) 2064putchars (SCHEME_P_ const char *s, int len)
2078{ 2065{
2066 port *pt = port (SCHEME_V->outport);
2067
2079#if USE_PORTS 2068#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) 2069 if (pt->kind & port_file)
2103 write (pt->rep.stdio.file, s, len); 2070 write (pt->rep.stdio.file, s, len);
2104 else 2071 else
2105 { 2072 {
2106 for (; len; len--) 2073 for (; len; len--)
2111 *pt->rep.string.curr++ = *s++; 2078 *pt->rep.string.curr++ = *s++;
2112 } 2079 }
2113 } 2080 }
2114 2081
2115#else 2082#else
2116 write (1, s, len); 2083 write (1, s, len); // output not initialised
2117#endif 2084#endif
2085}
2086
2087INTERFACE void
2088putstr (SCHEME_P_ const char *s)
2089{
2090 putchars (SCHEME_A_ s, strlen (s));
2118} 2091}
2119 2092
2120INTERFACE void 2093INTERFACE void
2121putcharacter (SCHEME_P_ int c) 2094putcharacter (SCHEME_P_ int c)
2122{ 2095{
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; 2096 char cc = c;
2141 write (1, &c, 1); 2097
2142#endif 2098 putchars (SCHEME_A_ &cc, 1);
2143} 2099}
2144 2100
2145/* read characters up to delimiter, but cater to character constants */ 2101/* read characters up to delimiter, but cater to character constants */
2146static char * 2102ecb_cold static char *
2147readstr_upto (SCHEME_P_ int skip, const char *delim) 2103readstr_upto (SCHEME_P_ int skip, const char *delim)
2148{ 2104{
2149 char *p = SCHEME_V->strbuff + skip; 2105 char *p = SCHEME_V->strbuff + skip;
2150 2106
2151 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2107 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2160 2116
2161 return SCHEME_V->strbuff; 2117 return SCHEME_V->strbuff;
2162} 2118}
2163 2119
2164/* read string expression "xxx...xxx" */ 2120/* read string expression "xxx...xxx" */
2165static pointer 2121ecb_cold static pointer
2166readstrexp (SCHEME_P_ char delim) 2122readstrexp (SCHEME_P_ char delim)
2167{ 2123{
2168 char *p = SCHEME_V->strbuff; 2124 char *p = SCHEME_V->strbuff;
2169 int c; 2125 int c;
2170 int c1 = 0; 2126 int c1 = 0;
2203 case '7': 2159 case '7':
2204 state = st_oct1; 2160 state = st_oct1;
2205 c1 = c - '0'; 2161 c1 = c - '0';
2206 break; 2162 break;
2207 2163
2164 case 'a': *p++ = '\a'; state = st_ok; break;
2165 case 'n': *p++ = '\n'; state = st_ok; break;
2166 case 'r': *p++ = '\r'; state = st_ok; break;
2167 case 't': *p++ = '\t'; state = st_ok; break;
2168
2169 case '\\':
2170 skipspace (SCHEME_A);
2171 break;
2172
2173 //TODO: x should end in ;, not two-digit hex
2208 case 'x': 2174 case 'x':
2209 case 'X': 2175 case 'X':
2210 state = st_x1; 2176 state = st_x1;
2211 c1 = 0; 2177 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; 2178 break;
2228 2179
2229 default: 2180 default:
2230 *p++ = c; 2181 *p++ = c;
2231 state = st_ok; 2182 state = st_ok;
2283 } 2234 }
2284 } 2235 }
2285} 2236}
2286 2237
2287/* check c is in chars */ 2238/* check c is in chars */
2288ecb_inline int 2239ecb_cold int
2289is_one_of (const char *s, int c) 2240is_one_of (const char *s, int c)
2290{ 2241{
2291 return c == EOF || !!strchr (s, c); 2242 return c == EOF || !!strchr (s, c);
2292} 2243}
2293 2244
2294/* skip white characters */ 2245/* skip white characters */
2295ecb_inline int 2246ecb_cold int
2296skipspace (SCHEME_P) 2247skipspace (SCHEME_P)
2297{ 2248{
2298 int c, curr_line = 0; 2249 int c, curr_line = 0;
2299 2250
2300 do 2251 do
2320 backchar (SCHEME_A_ c); 2271 backchar (SCHEME_A_ c);
2321 return 1; 2272 return 1;
2322} 2273}
2323 2274
2324/* get token */ 2275/* get token */
2325static int 2276ecb_cold static int
2326token (SCHEME_P) 2277token (SCHEME_P)
2327{ 2278{
2328 int c = skipspace (SCHEME_A); 2279 int c = skipspace (SCHEME_A);
2329 2280
2330 if (c == EOF) 2281 if (c == EOF)
2428} 2379}
2429 2380
2430/* ========== Routines for Printing ========== */ 2381/* ========== Routines for Printing ========== */
2431#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) 2382#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL)
2432 2383
2433static void 2384ecb_cold static void
2434printslashstring (SCHEME_P_ char *p, int len) 2385printslashstring (SCHEME_P_ char *p, int len)
2435{ 2386{
2436 int i; 2387 int i;
2437 unsigned char *s = (unsigned char *) p; 2388 unsigned char *s = (unsigned char *) p;
2438 2389
2494 2445
2495 putcharacter (SCHEME_A_ '"'); 2446 putcharacter (SCHEME_A_ '"');
2496} 2447}
2497 2448
2498/* print atoms */ 2449/* print atoms */
2499static void 2450ecb_cold static void
2500printatom (SCHEME_P_ pointer l, int f) 2451printatom (SCHEME_P_ pointer l, int f)
2501{ 2452{
2502 char *p; 2453 char *p;
2503 int len; 2454 int len;
2504 2455
2505 atom2str (SCHEME_A_ l, f, &p, &len); 2456 atom2str (SCHEME_A_ l, f, &p, &len);
2506 putchars (SCHEME_A_ p, len); 2457 putchars (SCHEME_A_ p, len);
2507} 2458}
2508 2459
2509/* Uses internal buffer unless string pointer is already available */ 2460/* Uses internal buffer unless string pointer is already available */
2510static void 2461ecb_cold static void
2511atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2462atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2512{ 2463{
2513 char *p; 2464 char *p;
2514 2465
2515 if (l == NIL) 2466 if (l == NIL)
2722 return car (d); 2673 return car (d);
2723 2674
2724 p = cons (car (d), cdr (d)); 2675 p = cons (car (d), cdr (d));
2725 q = p; 2676 q = p;
2726 2677
2727 while (cdr (cdr (p)) != NIL) 2678 while (cddr (p) != NIL)
2728 { 2679 {
2729 d = cons (car (p), cdr (p)); 2680 d = cons (car (p), cdr (p));
2730 2681
2731 if (cdr (cdr (p)) != NIL) 2682 if (cddr (p) != NIL)
2732 p = cdr (d); 2683 p = cdr (d);
2733 } 2684 }
2734 2685
2735 set_cdr (p, car (cdr (p))); 2686 set_cdr (p, cadr (p));
2736 return q; 2687 return q;
2737} 2688}
2738 2689
2739/* reverse list -- produce new list */ 2690/* reverse list -- produce new list */
2740static pointer 2691ecb_hot static pointer
2741reverse (SCHEME_P_ pointer a) 2692reverse (SCHEME_P_ pointer a)
2742{ 2693{
2743 /* a must be checked by gc */ 2694 /* a must be checked by gc */
2744 pointer p = NIL; 2695 pointer p = NIL;
2745 2696
2748 2699
2749 return p; 2700 return p;
2750} 2701}
2751 2702
2752/* reverse list --- in-place */ 2703/* reverse list --- in-place */
2753static pointer 2704ecb_hot static pointer
2754reverse_in_place (SCHEME_P_ pointer term, pointer list) 2705reverse_in_place (SCHEME_P_ pointer term, pointer list)
2755{ 2706{
2756 pointer result = term; 2707 pointer result = term;
2757 pointer p = list; 2708 pointer p = list;
2758 2709
2766 2717
2767 return result; 2718 return result;
2768} 2719}
2769 2720
2770/* append list -- produce new list (in reverse order) */ 2721/* append list -- produce new list (in reverse order) */
2771static pointer 2722ecb_hot static pointer
2772revappend (SCHEME_P_ pointer a, pointer b) 2723revappend (SCHEME_P_ pointer a, pointer b)
2773{ 2724{
2774 pointer result = a; 2725 pointer result = a;
2775 pointer p = b; 2726 pointer p = b;
2776 2727
2785 2736
2786 return S_F; /* signal an error */ 2737 return S_F; /* signal an error */
2787} 2738}
2788 2739
2789/* equivalence of atoms */ 2740/* equivalence of atoms */
2790int 2741ecb_hot int
2791eqv (pointer a, pointer b) 2742eqv (pointer a, pointer b)
2792{ 2743{
2793 if (is_string (a)) 2744 if (is_string (a))
2794 { 2745 {
2795 if (is_string (b)) 2746 if (is_string (b))
2889 } 2840 }
2890 else 2841 else
2891 set_car (env, immutable_cons (slot, car (env))); 2842 set_car (env, immutable_cons (slot, car (env)));
2892} 2843}
2893 2844
2894static pointer 2845ecb_hot static pointer
2895find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2846find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2896{ 2847{
2897 pointer x, y; 2848 pointer x, y;
2898 2849
2899 for (x = env; x != NIL; x = cdr (x)) 2850 for (x = env; x != NIL; x = cdr (x))
2920 return NIL; 2871 return NIL;
2921} 2872}
2922 2873
2923#else /* USE_ALIST_ENV */ 2874#else /* USE_ALIST_ENV */
2924 2875
2925ecb_inline void 2876static void
2926new_frame_in_env (SCHEME_P_ pointer old_env) 2877new_frame_in_env (SCHEME_P_ pointer old_env)
2927{ 2878{
2928 SCHEME_V->envir = immutable_cons (NIL, old_env); 2879 SCHEME_V->envir = immutable_cons (NIL, old_env);
2929 setenvironment (SCHEME_V->envir); 2880 setenvironment (SCHEME_V->envir);
2930} 2881}
2931 2882
2932ecb_inline void 2883static void
2933new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2884new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2934{ 2885{
2935 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2886 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2936} 2887}
2937 2888
2938static pointer 2889ecb_hot static pointer
2939find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2890find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2940{ 2891{
2941 pointer x, y; 2892 pointer x, y;
2942 2893
2943 for (x = env; x != NIL; x = cdr (x)) 2894 for (x = env; x != NIL; x = cdr (x))
2957 return NIL; 2908 return NIL;
2958} 2909}
2959 2910
2960#endif /* USE_ALIST_ENV else */ 2911#endif /* USE_ALIST_ENV else */
2961 2912
2962ecb_inline void 2913static void
2963new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2914new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2964{ 2915{
2965 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 2916 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2966 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2917 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2967} 2918}
2968 2919
2969ecb_inline void 2920static void
2970set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2921set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2971{ 2922{
2972 set_cdr (slot, value); 2923 set_cdr (slot, value);
2973} 2924}
2974 2925
2975ecb_inline pointer 2926static pointer
2976slot_value_in_env (pointer slot) 2927slot_value_in_env (pointer slot)
2977{ 2928{
2978 return cdr (slot); 2929 return cdr (slot);
2979} 2930}
2980 2931
2981/* ========== Evaluation Cycle ========== */ 2932/* ========== Evaluation Cycle ========== */
2982 2933
2983static int 2934ecb_cold static int
2984xError_1 (SCHEME_P_ const char *s, pointer a) 2935xError_1 (SCHEME_P_ const char *s, pointer a)
2985{ 2936{
2986#if USE_ERROR_HOOK 2937#if USE_ERROR_HOOK
2987 pointer x; 2938 pointer x;
2988 pointer hdl = SCHEME_V->ERROR_HOOK; 2939 pointer hdl = SCHEME_V->ERROR_HOOK;
3064 pointer code; 3015 pointer code;
3065}; 3016};
3066 3017
3067# define STACK_GROWTH 3 3018# define STACK_GROWTH 3
3068 3019
3069static void 3020ecb_hot static void
3070s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3021s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3071{ 3022{
3072 int nframes = (uintptr_t)SCHEME_V->dump; 3023 int nframes = (uintptr_t)SCHEME_V->dump;
3073 struct dump_stack_frame *next_frame; 3024 struct dump_stack_frame *next_frame;
3074 3025
3087 next_frame->code = code; 3038 next_frame->code = code;
3088 3039
3089 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3040 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3090} 3041}
3091 3042
3092static int 3043static ecb_hot int
3093xs_return (SCHEME_P_ pointer a) 3044xs_return (SCHEME_P_ pointer a)
3094{ 3045{
3095 int nframes = (uintptr_t)SCHEME_V->dump; 3046 int nframes = (uintptr_t)SCHEME_V->dump;
3096 struct dump_stack_frame *frame; 3047 struct dump_stack_frame *frame;
3097 3048
3108 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3059 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3109 3060
3110 return 0; 3061 return 0;
3111} 3062}
3112 3063
3113ecb_inline void 3064ecb_cold void
3114dump_stack_reset (SCHEME_P) 3065dump_stack_reset (SCHEME_P)
3115{ 3066{
3116 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3067 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3117 SCHEME_V->dump = (pointer)+0; 3068 SCHEME_V->dump = (pointer)+0;
3118} 3069}
3119 3070
3120ecb_inline void 3071ecb_cold void
3121dump_stack_initialize (SCHEME_P) 3072dump_stack_initialize (SCHEME_P)
3122{ 3073{
3123 SCHEME_V->dump_size = 0; 3074 SCHEME_V->dump_size = 0;
3124 SCHEME_V->dump_base = 0; 3075 SCHEME_V->dump_base = 0;
3125 dump_stack_reset (SCHEME_A); 3076 dump_stack_reset (SCHEME_A);
3126} 3077}
3127 3078
3128static void 3079ecb_cold static void
3129dump_stack_free (SCHEME_P) 3080dump_stack_free (SCHEME_P)
3130{ 3081{
3131 free (SCHEME_V->dump_base); 3082 free (SCHEME_V->dump_base);
3132 SCHEME_V->dump_base = 0; 3083 SCHEME_V->dump_base = 0;
3133 SCHEME_V->dump = (pointer)0; 3084 SCHEME_V->dump = (pointer)0;
3134 SCHEME_V->dump_size = 0; 3085 SCHEME_V->dump_size = 0;
3135} 3086}
3136 3087
3137static void 3088ecb_cold static void
3138dump_stack_mark (SCHEME_P) 3089dump_stack_mark (SCHEME_P)
3139{ 3090{
3140 int nframes = (uintptr_t)SCHEME_V->dump; 3091 int nframes = (uintptr_t)SCHEME_V->dump;
3141 int i; 3092 int i;
3142 3093
3148 mark (frame->envir); 3099 mark (frame->envir);
3149 mark (frame->code); 3100 mark (frame->code);
3150 } 3101 }
3151} 3102}
3152 3103
3153static pointer 3104ecb_cold static pointer
3154ss_get_cont (SCHEME_P) 3105ss_get_cont (SCHEME_P)
3155{ 3106{
3156 int nframes = (uintptr_t)SCHEME_V->dump; 3107 int nframes = (uintptr_t)SCHEME_V->dump;
3157 int i; 3108 int i;
3158 3109
3170 } 3121 }
3171 3122
3172 return cont; 3123 return cont;
3173} 3124}
3174 3125
3175static void 3126ecb_cold static void
3176ss_set_cont (SCHEME_P_ pointer cont) 3127ss_set_cont (SCHEME_P_ pointer cont)
3177{ 3128{
3178 int i = 0; 3129 int i = 0;
3179 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3130 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3180 3131
3192 SCHEME_V->dump = (pointer)(uintptr_t)i; 3143 SCHEME_V->dump = (pointer)(uintptr_t)i;
3193} 3144}
3194 3145
3195#else 3146#else
3196 3147
3197ecb_inline void 3148ecb_cold void
3198dump_stack_reset (SCHEME_P) 3149dump_stack_reset (SCHEME_P)
3199{ 3150{
3200 SCHEME_V->dump = NIL; 3151 SCHEME_V->dump = NIL;
3201} 3152}
3202 3153
3203ecb_inline void 3154ecb_cold void
3204dump_stack_initialize (SCHEME_P) 3155dump_stack_initialize (SCHEME_P)
3205{ 3156{
3206 dump_stack_reset (SCHEME_A); 3157 dump_stack_reset (SCHEME_A);
3207} 3158}
3208 3159
3209static void 3160ecb_cold static void
3210dump_stack_free (SCHEME_P) 3161dump_stack_free (SCHEME_P)
3211{ 3162{
3212 SCHEME_V->dump = NIL; 3163 SCHEME_V->dump = NIL;
3213} 3164}
3214 3165
3215static int 3166ecb_hot static int
3216xs_return (SCHEME_P_ pointer a) 3167xs_return (SCHEME_P_ pointer a)
3217{ 3168{
3218 pointer dump = SCHEME_V->dump; 3169 pointer dump = SCHEME_V->dump;
3219 3170
3220 SCHEME_V->value = a; 3171 SCHEME_V->value = a;
3230 SCHEME_V->dump = dump; 3181 SCHEME_V->dump = dump;
3231 3182
3232 return 0; 3183 return 0;
3233} 3184}
3234 3185
3235static void 3186ecb_hot static void
3236s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3187s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3237{ 3188{
3238 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), 3189 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op),
3239 cons (args, 3190 cons (args,
3240 cons (SCHEME_V->envir, 3191 cons (SCHEME_V->envir,
3241 cons (code, 3192 cons (code,
3242 SCHEME_V->dump)))); 3193 SCHEME_V->dump))));
3243} 3194}
3244 3195
3196ecb_cold static void
3197dump_stack_mark (SCHEME_P)
3198{
3199 mark (SCHEME_V->dump);
3200}
3201
3202ecb_cold static pointer
3203ss_get_cont (SCHEME_P)
3204{
3205 return SCHEME_V->dump;
3206}
3207
3208ecb_cold static void
3209ss_set_cont (SCHEME_P_ pointer cont)
3210{
3211 SCHEME_V->dump = cont;
3212}
3213
3214#endif
3215
3216#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3217
3218#if EXPERIMENT
3219
3220typedef void *stream[1];
3221
3222#define stream_init() { 0 }
3223
3224ecb_cold static void
3225stream_put (void **s, uint8_t byte)
3226{
3227 uint32_t *sp = *s;
3228 uint32_t size = sizeof (uint32_t) * 2;
3229 uint32_t offs = size;
3230
3231 if (ecb_expect_true (sp))
3232 {
3233 offs = sp[0];
3234 size = sp[1];
3235 }
3236
3237 if (ecb_expect_false (offs == size))
3238 {
3239 size *= 2;
3240 sp = realloc (sp, size);
3241 *s = sp;
3242 sp[1] = size;
3243
3244 }
3245
3246 ((uint8_t *)sp)[offs++] = byte;
3247 sp[0] = offs;
3248}
3249
3250#define stream_data(s) ((char *)(s)[0] + sizeof (uint32_t) * 2)
3251#define stream_size(s) (((uint32_t *)(s)[0])[0] - sizeof (uint32_t) * 2)
3252#define stream_free(s) free (s[0])
3253
3254// calculates a (preferably small) integer that makes it possible to find
3255// the symbol again. if pointers were offsets into a memory area... until
3256// then, we return segment number in the low bits, and offset in the high
3257// bits
3258static uint32_t
3259symbol_id (SCHEME_P_ pointer sym)
3260{
3261 struct cell *p = CELL (sym);
3262 int i;
3263
3264 for (i = SCHEME_V->last_cell_seg; i >= 0; --i)
3265 if (SCHEME_V->cell_seg[i] <= p && p < SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize[i])
3266 {
3267 printf ("seg %d ofs %d/%d\n",i,(p - SCHEME_V->cell_seg[i]),SCHEME_V->cell_segsize[i]);//D
3268 return i | ((p - SCHEME_V->cell_seg[i]) << CELL_NSEGMENT_LOG);
3269 }
3270
3271 abort ();
3272}
3273
3245static void 3274static void
3246dump_stack_mark (SCHEME_P) 3275compile (SCHEME_P_ stream s, pointer x)
3247{ 3276{
3248 mark (SCHEME_V->dump); 3277 if (x == NIL)
3249} 3278 {
3279 stream_put (s, 0);
3280 return;
3281 }
3250 3282
3251static pointer 3283 if (is_syntax (x))
3252ss_get_cont (SCHEME_P) 3284 {
3253{ 3285 stream_put (s, 1);
3254 return SCHEME_V->dump; 3286 stream_put (s, syntaxnum (x));
3255} 3287 return;
3288 }
3256 3289
3257static void 3290 switch (type (x))
3258ss_set_cont (SCHEME_P_ pointer cont) 3291 {
3259{ 3292 case T_INTEGER:
3260 SCHEME_V->dump = cont; 3293 stream_put (s, 2);
3261} 3294 stream_put (s, 0);
3295 stream_put (s, 0);
3296 stream_put (s, 0);
3297 stream_put (s, 0);
3298 return;
3262 3299
3263#endif 3300 case T_SYMBOL:
3301 {
3302 uint32_t sym = symbol_id (SCHEME_A_ x);
3303 printf ("sym %x\n", sym);//D
3264 3304
3265#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3305 stream_put (s, 3);
3266 3306
3267#if EXPERIMENT 3307 while (sym > 0x7f)
3308 {
3309 stream_put (s, sym | 0x80);
3310 sym >>= 8;
3311 }
3312
3313 stream_put (s, sym);
3314 }
3315 return;
3316
3317 case T_PAIR:
3318 stream_put (s, 4);
3319 while (x != NIL)
3320 {
3321 compile (SCHEME_A_ s, car (x));
3322 x = cdr (x);
3323 }
3324 stream_put (s, 0xff);
3325 return;
3326
3327 default:
3328 stream_put (s, 5);
3329 stream_put (s, type (x));
3330 stream_put (s, 0);
3331 stream_put (s, 0);
3332 stream_put (s, 0);
3333 stream_put (s, 0);
3334 break;
3335 }
3336}
3337
3268static int 3338static int
3339compile_closure (SCHEME_P_ pointer p)
3340{
3341 stream s = stream_init ();
3342
3343 printatom (SCHEME_A_ p, 1);//D
3344 compile (SCHEME_A_ s, car (p));
3345
3346 FILE *xxd = popen ("xxd", "we");
3347 fwrite (stream_data (s), 1, stream_size (s), xxd);
3348 fclose (xxd);
3349
3350 return stream_size (s);
3351}
3352
3353static int
3269debug (SCHEME_P_ int indent, pointer x) 3354dtree (SCHEME_P_ int indent, pointer x)
3270{ 3355{
3271 int c; 3356 int c;
3272 3357
3273 if (is_syntax (x)) 3358 if (is_syntax (x))
3274 { 3359 {
3292 printf ("%*sS<%s>\n", indent, "", symname (x)); 3377 printf ("%*sS<%s>\n", indent, "", symname (x));
3293 return 24+8; 3378 return 24+8;
3294 3379
3295 case T_CLOSURE: 3380 case T_CLOSURE:
3296 printf ("%*sS<%s>\n", indent, "", "closure"); 3381 printf ("%*sS<%s>\n", indent, "", "closure");
3297 debug (SCHEME_A_ indent + 3, cdr(x)); 3382 dtree (SCHEME_A_ indent + 3, cdr(x));
3298 return 32 + debug (SCHEME_A_ indent + 3, car (x)); 3383 return 32 + dtree (SCHEME_A_ indent + 3, car (x));
3299 3384
3300 case T_PAIR: 3385 case T_PAIR:
3301 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); 3386 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3302 c = debug (SCHEME_A_ indent + 3, car (x)); 3387 c = dtree (SCHEME_A_ indent + 3, car (x));
3303 c += debug (SCHEME_A_ indent + 3, cdr (x)); 3388 c += dtree (SCHEME_A_ indent + 3, cdr (x));
3304 return c + 1; 3389 return c + 1;
3305 3390
3306 case T_PORT: 3391 case T_PORT:
3307 printf ("%*sS<%s>\n", indent, "", "port"); 3392 printf ("%*sS<%s>\n", indent, "", "port");
3308 return 24+8; 3393 return 24+8;
3311 printf ("%*sS<%s>\n", indent, "", "vector"); 3396 printf ("%*sS<%s>\n", indent, "", "vector");
3312 return 24+8; 3397 return 24+8;
3313 3398
3314 case T_ENVIRONMENT: 3399 case T_ENVIRONMENT:
3315 printf ("%*sS<%s>\n", indent, "", "environment"); 3400 printf ("%*sS<%s>\n", indent, "", "environment");
3316 return 0 + debug (SCHEME_A_ indent + 3, car (x)); 3401 return 0 + dtree (SCHEME_A_ indent + 3, car (x));
3317 3402
3318 default: 3403 default:
3319 printf ("unhandled type %d\n", type (x)); 3404 printf ("unhandled type %d\n", type (x));
3320 break; 3405 break;
3321 } 3406 }
3322} 3407}
3323#endif 3408#endif
3324 3409
3325static int 3410/* syntax, eval, core, ... */
3411ecb_hot static int
3326opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3412opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3327{ 3413{
3328 pointer args = SCHEME_V->args; 3414 pointer args = SCHEME_V->args;
3329 pointer x, y; 3415 pointer x, y;
3330 3416
3331 switch (op) 3417 switch (op)
3332 { 3418 {
3333#if EXPERIMENT //D 3419#if EXPERIMENT //D
3334 case OP_DEBUG: 3420 case OP_DEBUG:
3335 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); 3421 {
3422 uint32_t len = compile_closure (SCHEME_A_ car (args));
3423 printf ("len = %d\n", len);
3336 printf ("\n"); 3424 printf ("\n");
3337 s_return (S_T); 3425 s_return (S_T);
3426 }
3338#endif 3427#endif
3339 case OP_LOAD: /* load */ 3428 case OP_LOAD: /* load */
3340 if (file_interactive (SCHEME_A)) 3429 if (file_interactive (SCHEME_A))
3341 { 3430 {
3342 putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n"); 3431 putstr (SCHEME_A_ "Loading ");
3343 //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args))); 3432 putstr (SCHEME_A_ strvalue (car (args)));
3433 putcharacter (SCHEME_A_ '\n');
3344 } 3434 }
3345 3435
3346 if (!file_push (SCHEME_A_ strvalue (car (args)))) 3436 if (!file_push (SCHEME_A_ strvalue (car (args))))
3347 Error_1 ("unable to open", car (args)); 3437 Error_1 ("unable to open", car (args));
3348 else 3438
3349 {
3350 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3439 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3351 s_goto (OP_T0LVL); 3440 s_goto (OP_T0LVL);
3352 }
3353 3441
3354 case OP_T0LVL: /* top level */ 3442 case OP_T0LVL: /* top level */
3355 3443
3356 /* If we reached the end of file, this loop is done. */ 3444 /* If we reached the end of file, this loop is done. */
3357 if (port (SCHEME_V->loadport)->kind & port_saw_EOF) 3445 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3373 /* If interactive, be nice to user. */ 3461 /* If interactive, be nice to user. */
3374 if (file_interactive (SCHEME_A)) 3462 if (file_interactive (SCHEME_A))
3375 { 3463 {
3376 SCHEME_V->envir = SCHEME_V->global_env; 3464 SCHEME_V->envir = SCHEME_V->global_env;
3377 dump_stack_reset (SCHEME_A); 3465 dump_stack_reset (SCHEME_A);
3378 putstr (SCHEME_A_ "\n"); 3466 putcharacter (SCHEME_A_ '\n');
3379 putstr (SCHEME_A_ prompt); 3467 putstr (SCHEME_A_ prompt);
3380 } 3468 }
3381 3469
3382 /* Set up another iteration of REPL */ 3470 /* Set up another iteration of REPL */
3383 SCHEME_V->nesting = 0; 3471 SCHEME_V->nesting = 0;
3418 { 3506 {
3419 SCHEME_V->print_flag = 1; 3507 SCHEME_V->print_flag = 1;
3420 SCHEME_V->args = SCHEME_V->value; 3508 SCHEME_V->args = SCHEME_V->value;
3421 s_goto (OP_P0LIST); 3509 s_goto (OP_P0LIST);
3422 } 3510 }
3423 else 3511
3424 s_return (SCHEME_V->value); 3512 s_return (SCHEME_V->value);
3425 3513
3426 case OP_EVAL: /* main part of evaluation */ 3514 case OP_EVAL: /* main part of evaluation */
3427#if USE_TRACING 3515#if USE_TRACING
3428 if (SCHEME_V->tracing) 3516 if (SCHEME_V->tracing)
3429 { 3517 {
3462 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ 3550 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */
3463 SCHEME_V->code = x; 3551 SCHEME_V->code = x;
3464 s_goto (OP_EVAL); 3552 s_goto (OP_EVAL);
3465 } 3553 }
3466 } 3554 }
3467 else 3555
3468 s_return (SCHEME_V->code); 3556 s_return (SCHEME_V->code);
3469 3557
3470 case OP_E0ARGS: /* eval arguments */ 3558 case OP_E0ARGS: /* eval arguments */
3471 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ 3559 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3472 { 3560 {
3473 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3561 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3645 else 3733 else
3646 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); 3734 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value);
3647 3735
3648 s_return (SCHEME_V->code); 3736 s_return (SCHEME_V->code);
3649 3737
3650
3651 case OP_DEFP: /* defined? */ 3738 case OP_DEFP: /* defined? */
3652 x = SCHEME_V->envir; 3739 x = SCHEME_V->envir;
3653 3740
3654 if (cdr (args) != NIL) 3741 if (cdr (args) != NIL)
3655 x = cadr (args); 3742 x = cadr (args);
3672 set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); 3759 set_slot_in_env (SCHEME_A_ y, SCHEME_V->value);
3673 s_return (SCHEME_V->value); 3760 s_return (SCHEME_V->value);
3674 } 3761 }
3675 else 3762 else
3676 Error_1 ("set!: unbound variable:", SCHEME_V->code); 3763 Error_1 ("set!: unbound variable:", SCHEME_V->code);
3677
3678 3764
3679 case OP_BEGIN: /* begin */ 3765 case OP_BEGIN: /* begin */
3680 if (!is_pair (SCHEME_V->code)) 3766 if (!is_pair (SCHEME_V->code))
3681 s_return (SCHEME_V->code); 3767 s_return (SCHEME_V->code);
3682 3768
3694 case OP_IF1: /* if */ 3780 case OP_IF1: /* if */
3695 if (is_true (SCHEME_V->value)) 3781 if (is_true (SCHEME_V->value))
3696 SCHEME_V->code = car (SCHEME_V->code); 3782 SCHEME_V->code = car (SCHEME_V->code);
3697 else 3783 else
3698 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ 3784 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
3785
3699 s_goto (OP_EVAL); 3786 s_goto (OP_EVAL);
3700 3787
3701 case OP_LET0: /* let */ 3788 case OP_LET0: /* let */
3702 SCHEME_V->args = NIL; 3789 SCHEME_V->args = NIL;
3703 SCHEME_V->value = SCHEME_V->code; 3790 SCHEME_V->value = SCHEME_V->code;
3859 } 3946 }
3860 else 3947 else
3861 { 3948 {
3862 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) 3949 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL)
3863 s_return (NIL); 3950 s_return (NIL);
3864 else 3951
3865 {
3866 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 3952 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
3867 SCHEME_V->code = caar (SCHEME_V->code); 3953 SCHEME_V->code = caar (SCHEME_V->code);
3868 s_goto (OP_EVAL); 3954 s_goto (OP_EVAL);
3869 }
3870 } 3955 }
3871 3956
3872 case OP_DELAY: /* delay */ 3957 case OP_DELAY: /* delay */
3873 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); 3958 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir);
3874 set_typeflag (x, T_PROMISE); 3959 set_typeflag (x, T_PROMISE);
3885 case OP_AND1: /* and */ 3970 case OP_AND1: /* and */
3886 if (is_false (SCHEME_V->value)) 3971 if (is_false (SCHEME_V->value))
3887 s_return (SCHEME_V->value); 3972 s_return (SCHEME_V->value);
3888 else if (SCHEME_V->code == NIL) 3973 else if (SCHEME_V->code == NIL)
3889 s_return (SCHEME_V->value); 3974 s_return (SCHEME_V->value);
3890 else 3975
3891 {
3892 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 3976 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
3893 SCHEME_V->code = car (SCHEME_V->code); 3977 SCHEME_V->code = car (SCHEME_V->code);
3894 s_goto (OP_EVAL); 3978 s_goto (OP_EVAL);
3895 }
3896 3979
3897 case OP_OR0: /* or */ 3980 case OP_OR0: /* or */
3898 if (SCHEME_V->code == NIL) 3981 if (SCHEME_V->code == NIL)
3899 s_return (S_F); 3982 s_return (S_F);
3900 3983
3905 case OP_OR1: /* or */ 3988 case OP_OR1: /* or */
3906 if (is_true (SCHEME_V->value)) 3989 if (is_true (SCHEME_V->value))
3907 s_return (SCHEME_V->value); 3990 s_return (SCHEME_V->value);
3908 else if (SCHEME_V->code == NIL) 3991 else if (SCHEME_V->code == NIL)
3909 s_return (SCHEME_V->value); 3992 s_return (SCHEME_V->value);
3910 else 3993
3911 {
3912 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 3994 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
3913 SCHEME_V->code = car (SCHEME_V->code); 3995 SCHEME_V->code = car (SCHEME_V->code);
3914 s_goto (OP_EVAL); 3996 s_goto (OP_EVAL);
3915 }
3916 3997
3917 case OP_C0STREAM: /* cons-stream */ 3998 case OP_C0STREAM: /* cons-stream */
3918 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 3999 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3919 SCHEME_V->code = car (SCHEME_V->code); 4000 SCHEME_V->code = car (SCHEME_V->code);
3920 s_goto (OP_EVAL); 4001 s_goto (OP_EVAL);
3985 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); 4066 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x));
3986 SCHEME_V->code = caar (x); 4067 SCHEME_V->code = caar (x);
3987 s_goto (OP_EVAL); 4068 s_goto (OP_EVAL);
3988 } 4069 }
3989 } 4070 }
3990 else 4071
3991 s_return (NIL); 4072 s_return (NIL);
3992 4073
3993 case OP_CASE2: /* case */ 4074 case OP_CASE2: /* case */
3994 if (is_true (SCHEME_V->value)) 4075 if (is_true (SCHEME_V->value))
3995 s_goto (OP_BEGIN); 4076 s_goto (OP_BEGIN);
3996 else 4077
3997 s_return (NIL); 4078 s_return (NIL);
3998 4079
3999 case OP_PAPPLY: /* apply */ 4080 case OP_PAPPLY: /* apply */
4000 SCHEME_V->code = car (args); 4081 SCHEME_V->code = car (args);
4001 SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); 4082 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
4002 /*SCHEME_V->args = cadr(args); */ 4083 /*SCHEME_V->args = cadr(args); */
4016 } 4097 }
4017 4098
4018 if (USE_ERROR_CHECKING) abort (); 4099 if (USE_ERROR_CHECKING) abort ();
4019} 4100}
4020 4101
4021static int 4102/* math, cxr */
4103ecb_hot static int
4022opexe_1 (SCHEME_P_ enum scheme_opcodes op) 4104opexe_1 (SCHEME_P_ enum scheme_opcodes op)
4023{ 4105{
4024 pointer args = SCHEME_V->args; 4106 pointer args = SCHEME_V->args;
4025 pointer x = car (args); 4107 pointer x = car (args);
4026 num v; 4108 num v;
4027 4109
4028 switch (op) 4110 switch (op)
4029 { 4111 {
4030#if USE_MATH 4112#if USE_MATH
4031 case OP_INEX2EX: /* inexact->exact */ 4113 case OP_INEX2EX: /* inexact->exact */
4032 {
4033 if (is_integer (x)) 4114 if (!is_integer (x))
4034 s_return (x); 4115 {
4035
4036 RVALUE r = rvalue_unchecked (x); 4116 RVALUE r = rvalue_unchecked (x);
4037 4117
4038 if (r == (RVALUE)(IVALUE)r) 4118 if (r == (RVALUE)(IVALUE)r)
4039 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); 4119 x = mk_integer (SCHEME_A_ rvalue_unchecked (x));
4040 else 4120 else
4041 Error_1 ("inexact->exact: not integral:", x); 4121 Error_1 ("inexact->exact: not integral:", x);
4042 } 4122 }
4043 4123
4124 s_return (x);
4125
4126 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4127 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4128 case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x))));
4129 case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x))));
4130
4131 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)))); 4132 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)))); 4133 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))
4134 / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args))))));
4046 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 4135 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)))); 4136 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)))); 4137 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)))); 4138 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)))); 4139 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x))));
4051 4140
4052 case OP_ATAN: 4141 case OP_ATAN:
4142 s_return (mk_real (SCHEME_A_
4053 if (cdr (args) == NIL) 4143 cdr (args) == NIL
4054 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 4144 ? atan (rvalue (x))
4055 else 4145 : 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 4146
4064 case OP_EXPT: 4147 case OP_EXPT:
4065 { 4148 {
4066 RVALUE result; 4149 RVALUE result;
4067 int real_result = 1; 4150 int real_result = 1;
4090 if (real_result) 4173 if (real_result)
4091 s_return (mk_real (SCHEME_A_ result)); 4174 s_return (mk_real (SCHEME_A_ result));
4092 else 4175 else
4093 s_return (mk_integer (SCHEME_A_ result)); 4176 s_return (mk_integer (SCHEME_A_ result));
4094 } 4177 }
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 4178#endif
4111 4179
4112 case OP_ADD: /* + */ 4180 case OP_ADD: /* + */
4113 v = num_zero; 4181 v = num_zero;
4114 4182
4416 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4484 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4417 4485
4418 s_return (newstr); 4486 s_return (newstr);
4419 } 4487 }
4420 4488
4421 case OP_SUBSTR: /* substring */ 4489 case OP_STRING_COPY: /* substring/string-copy */
4422 { 4490 {
4423 char *str = strvalue (x); 4491 char *str = strvalue (x);
4424 int index0 = ivalue_unchecked (cadr (args)); 4492 int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args));
4425 int index1; 4493 int index1;
4426 int len; 4494 int len;
4427 4495
4428 if (index0 > strlength (x)) 4496 if (index0 > strlength (x))
4429 Error_1 ("substring: start out of bounds:", cadr (args)); 4497 Error_1 ("string->copy: start out of bounds:", cadr (args));
4430 4498
4431 if (cddr (args) != NIL) 4499 if (cddr (args) != NIL)
4432 { 4500 {
4433 index1 = ivalue_unchecked (caddr (args)); 4501 index1 = ivalue_unchecked (caddr (args));
4434 4502
4435 if (index1 > strlength (x) || index1 < index0) 4503 if (index1 > strlength (x) || index1 < index0)
4436 Error_1 ("substring: end out of bounds:", caddr (args)); 4504 Error_1 ("string->copy: end out of bounds:", caddr (args));
4437 } 4505 }
4438 else 4506 else
4439 index1 = strlength (x); 4507 index1 = strlength (x);
4440 4508
4441 len = index1 - index0; 4509 len = index1 - index0;
4442 x = mk_empty_string (SCHEME_A_ len, ' '); 4510 x = mk_counted_string (SCHEME_A_ str + index0, len);
4443 memcpy (strvalue (x), str + index0, len);
4444 strvalue (x)[len] = 0;
4445 4511
4446 s_return (x); 4512 s_return (x);
4447 } 4513 }
4448 4514
4449 case OP_VECTOR: /* vector */ 4515 case OP_VECTOR: /* vector */
4523 } 4589 }
4524 4590
4525 if (USE_ERROR_CHECKING) abort (); 4591 if (USE_ERROR_CHECKING) abort ();
4526} 4592}
4527 4593
4528static int 4594/* relational ops */
4595ecb_hot static int
4529opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4596opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4530{ 4597{
4531 pointer x = SCHEME_V->args; 4598 pointer x = SCHEME_V->args;
4532 4599
4533 for (;;) 4600 for (;;)
4554 } 4621 }
4555 4622
4556 s_return (S_T); 4623 s_return (S_T);
4557} 4624}
4558 4625
4559static int 4626/* predicates */
4627ecb_hot static int
4560opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4628opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4561{ 4629{
4562 pointer args = SCHEME_V->args; 4630 pointer args = SCHEME_V->args;
4563 pointer a = car (args); 4631 pointer a = car (args);
4564 pointer d = cdr (args); 4632 pointer d = cdr (args);
4611 } 4679 }
4612 4680
4613 s_retbool (r); 4681 s_retbool (r);
4614} 4682}
4615 4683
4616static int 4684/* promises, list ops, ports */
4685ecb_hot static int
4617opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4686opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4618{ 4687{
4619 pointer args = SCHEME_V->args; 4688 pointer args = SCHEME_V->args;
4620 pointer a = car (args); 4689 pointer a = car (args);
4621 pointer x, y; 4690 pointer x, y;
4638 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4707 case OP_SAVE_FORCED: /* Save forced value replacing promise */
4639 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); 4708 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
4640 s_return (SCHEME_V->value); 4709 s_return (SCHEME_V->value);
4641 4710
4642#if USE_PORTS 4711#if USE_PORTS
4712
4713 case OP_EOF_OBJECT: /* eof-object */
4714 s_return (S_EOF);
4643 4715
4644 case OP_WRITE: /* write */ 4716 case OP_WRITE: /* write */
4645 case OP_DISPLAY: /* display */ 4717 case OP_DISPLAY: /* display */
4646 case OP_WRITE_CHAR: /* write-char */ 4718 case OP_WRITE_CHAR: /* write-char */
4647 if (is_pair (cdr (SCHEME_V->args))) 4719 if (is_pair (cdr (SCHEME_V->args)))
4661 else 4733 else
4662 SCHEME_V->print_flag = 0; 4734 SCHEME_V->print_flag = 0;
4663 4735
4664 s_goto (OP_P0LIST); 4736 s_goto (OP_P0LIST);
4665 4737
4738 //TODO: move to scheme
4666 case OP_NEWLINE: /* newline */ 4739 case OP_NEWLINE: /* newline */
4667 if (is_pair (args)) 4740 if (is_pair (args))
4668 { 4741 {
4669 if (a != SCHEME_V->outport) 4742 if (a != SCHEME_V->outport)
4670 { 4743 {
4672 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4745 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4673 SCHEME_V->outport = a; 4746 SCHEME_V->outport = a;
4674 } 4747 }
4675 } 4748 }
4676 4749
4677 putstr (SCHEME_A_ "\n"); 4750 putcharacter (SCHEME_A_ '\n');
4678 s_return (S_T); 4751 s_return (S_T);
4679#endif 4752#endif
4680 4753
4681 case OP_ERR0: /* error */ 4754 case OP_ERR0: /* error */
4682 SCHEME_V->retcode = -1; 4755 SCHEME_V->retcode = -1;
4691 putstr (SCHEME_A_ strvalue (car (args))); 4764 putstr (SCHEME_A_ strvalue (car (args)));
4692 SCHEME_V->args = cdr (args); 4765 SCHEME_V->args = cdr (args);
4693 s_goto (OP_ERR1); 4766 s_goto (OP_ERR1);
4694 4767
4695 case OP_ERR1: /* error */ 4768 case OP_ERR1: /* error */
4696 putstr (SCHEME_A_ " "); 4769 putcharacter (SCHEME_A_ ' ');
4697 4770
4698 if (args != NIL) 4771 if (args != NIL)
4699 { 4772 {
4700 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); 4773 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4701 SCHEME_V->args = a; 4774 SCHEME_V->args = a;
4702 SCHEME_V->print_flag = 1; 4775 SCHEME_V->print_flag = 1;
4703 s_goto (OP_P0LIST); 4776 s_goto (OP_P0LIST);
4704 } 4777 }
4705 else 4778 else
4706 { 4779 {
4707 putstr (SCHEME_A_ "\n"); 4780 putcharacter (SCHEME_A_ '\n');
4708 4781
4709 if (SCHEME_V->interactive_repl) 4782 if (SCHEME_V->interactive_repl)
4710 s_goto (OP_T0LVL); 4783 s_goto (OP_T0LVL);
4711 else 4784 else
4712 return -1; 4785 return -1;
4920 } 4993 }
4921 4994
4922 if (USE_ERROR_CHECKING) abort (); 4995 if (USE_ERROR_CHECKING) abort ();
4923} 4996}
4924 4997
4925static int 4998/* reading */
4999ecb_cold static int
4926opexe_5 (SCHEME_P_ enum scheme_opcodes op) 5000opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4927{ 5001{
4928 pointer args = SCHEME_V->args; 5002 pointer args = SCHEME_V->args;
4929 pointer x; 5003 pointer x;
4930 5004
5199 pointer b = cdr (args); 5273 pointer b = cdr (args);
5200 int ok_abbr = ok_abbrev (b); 5274 int ok_abbr = ok_abbrev (b);
5201 SCHEME_V->args = car (b); 5275 SCHEME_V->args = car (b);
5202 5276
5203 if (a == SCHEME_V->QUOTE && ok_abbr) 5277 if (a == SCHEME_V->QUOTE && ok_abbr)
5204 putstr (SCHEME_A_ "'"); 5278 putcharacter (SCHEME_A_ '\'');
5205 else if (a == SCHEME_V->QQUOTE && ok_abbr) 5279 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5206 putstr (SCHEME_A_ "`"); 5280 putcharacter (SCHEME_A_ '`');
5207 else if (a == SCHEME_V->UNQUOTE && ok_abbr) 5281 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5208 putstr (SCHEME_A_ ","); 5282 putcharacter (SCHEME_A_ ',');
5209 else if (a == SCHEME_V->UNQUOTESP && ok_abbr) 5283 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5210 putstr (SCHEME_A_ ",@"); 5284 putstr (SCHEME_A_ ",@");
5211 else 5285 else
5212 { 5286 {
5213 putstr (SCHEME_A_ "("); 5287 putcharacter (SCHEME_A_ '(');
5214 s_save (SCHEME_A_ OP_P1LIST, b, NIL); 5288 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5215 SCHEME_V->args = a; 5289 SCHEME_V->args = a;
5216 } 5290 }
5217 5291
5218 s_goto (OP_P0LIST); 5292 s_goto (OP_P0LIST);
5220 5294
5221 case OP_P1LIST: 5295 case OP_P1LIST:
5222 if (is_pair (args)) 5296 if (is_pair (args))
5223 { 5297 {
5224 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); 5298 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5225 putstr (SCHEME_A_ " "); 5299 putcharacter (SCHEME_A_ ' ');
5226 SCHEME_V->args = car (args); 5300 SCHEME_V->args = car (args);
5227 s_goto (OP_P0LIST); 5301 s_goto (OP_P0LIST);
5228 } 5302 }
5229 else if (is_vector (args)) 5303 else if (is_vector (args))
5230 { 5304 {
5238 { 5312 {
5239 putstr (SCHEME_A_ " . "); 5313 putstr (SCHEME_A_ " . ");
5240 printatom (SCHEME_A_ args, SCHEME_V->print_flag); 5314 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5241 } 5315 }
5242 5316
5243 putstr (SCHEME_A_ ")"); 5317 putcharacter (SCHEME_A_ ')');
5244 s_return (S_T); 5318 s_return (S_T);
5245 } 5319 }
5246 5320
5247 case OP_PVECFROM: 5321 case OP_PVECFROM:
5248 { 5322 {
5250 pointer vec = car (args); 5324 pointer vec = car (args);
5251 int len = veclength (vec); 5325 int len = veclength (vec);
5252 5326
5253 if (i == len) 5327 if (i == len)
5254 { 5328 {
5255 putstr (SCHEME_A_ ")"); 5329 putcharacter (SCHEME_A_ ')');
5256 s_return (S_T); 5330 s_return (S_T);
5257 } 5331 }
5258 else 5332 else
5259 { 5333 {
5260 pointer elem = vector_get (vec, i); 5334 pointer elem = vector_get (vec, i);
5262 ivalue_unchecked (cdr (args)) = i + 1; 5336 ivalue_unchecked (cdr (args)) = i + 1;
5263 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5337 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5264 SCHEME_V->args = elem; 5338 SCHEME_V->args = elem;
5265 5339
5266 if (i > 0) 5340 if (i > 0)
5267 putstr (SCHEME_A_ " "); 5341 putcharacter (SCHEME_A_ ' ');
5268 5342
5269 s_goto (OP_P0LIST); 5343 s_goto (OP_P0LIST);
5270 } 5344 }
5271 } 5345 }
5272 } 5346 }
5273 5347
5274 if (USE_ERROR_CHECKING) abort (); 5348 if (USE_ERROR_CHECKING) abort ();
5275} 5349}
5276 5350
5277static int 5351/* list ops */
5352ecb_hot static int
5278opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5353opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5279{ 5354{
5280 pointer args = SCHEME_V->args; 5355 pointer args = SCHEME_V->args;
5281 pointer a = car (args); 5356 pointer a = car (args);
5282 pointer x, y; 5357 pointer x, y;
5340 5415
5341/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5416/* 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); 5417typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5343 5418
5344typedef int (*test_predicate)(pointer); 5419typedef int (*test_predicate)(pointer);
5345static int 5420
5421ecb_hot static int
5346tst_any (pointer p) 5422tst_any (pointer p)
5347{ 5423{
5348 return 1; 5424 return 1;
5349} 5425}
5350 5426
5351static int 5427ecb_hot static int
5352tst_inonneg (pointer p) 5428tst_inonneg (pointer p)
5353{ 5429{
5354 return is_integer (p) && ivalue_unchecked (p) >= 0; 5430 return is_integer (p) && ivalue_unchecked (p) >= 0;
5355} 5431}
5356 5432
5357static int 5433ecb_hot static int
5358tst_is_list (SCHEME_P_ pointer p) 5434tst_is_list (SCHEME_P_ pointer p)
5359{ 5435{
5360 return p == NIL || is_pair (p); 5436 return p == NIL || is_pair (p);
5361} 5437}
5362 5438
5405#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" 5481#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5406#include "opdefines.h" 5482#include "opdefines.h"
5407#undef OP_DEF 5483#undef OP_DEF
5408; 5484;
5409 5485
5410static const char * 5486ecb_cold static const char *
5411opname (int idx) 5487opname (int idx)
5412{ 5488{
5413 const char *name = opnames; 5489 const char *name = opnames;
5414 5490
5415 /* should do this at compile time, but would require external program, right? */ 5491 /* should do this at compile time, but would require external program, right? */
5417 name += strlen (name) + 1; 5493 name += strlen (name) + 1;
5418 5494
5419 return *name ? name : "ILLEGAL"; 5495 return *name ? name : "ILLEGAL";
5420} 5496}
5421 5497
5422static const char * 5498ecb_cold static const char *
5423procname (pointer x) 5499procname (pointer x)
5424{ 5500{
5425 return opname (procnum (x)); 5501 return opname (procnum (x));
5426} 5502}
5427 5503
5447#undef OP_DEF 5523#undef OP_DEF
5448 {0} 5524 {0}
5449}; 5525};
5450 5526
5451/* kernel of this interpreter */ 5527/* kernel of this interpreter */
5452static void ecb_hot 5528ecb_hot static void
5453Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5529Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5454{ 5530{
5455 SCHEME_V->op = op; 5531 SCHEME_V->op = op;
5456 5532
5457 for (;;) 5533 for (;;)
5548 } 5624 }
5549} 5625}
5550 5626
5551/* ========== Initialization of internal keywords ========== */ 5627/* ========== Initialization of internal keywords ========== */
5552 5628
5553static void 5629ecb_cold static void
5554assign_syntax (SCHEME_P_ const char *name) 5630assign_syntax (SCHEME_P_ const char *name)
5555{ 5631{
5556 pointer x = oblist_add_by_name (SCHEME_A_ name); 5632 pointer x = oblist_add_by_name (SCHEME_A_ name);
5557 set_typeflag (x, typeflag (x) | T_SYNTAX); 5633 set_typeflag (x, typeflag (x) | T_SYNTAX);
5558} 5634}
5559 5635
5560static void 5636ecb_cold static void
5561assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) 5637assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
5562{ 5638{
5563 pointer x = mk_symbol (SCHEME_A_ name); 5639 pointer x = mk_symbol (SCHEME_A_ name);
5564 pointer y = mk_proc (SCHEME_A_ op); 5640 pointer y = mk_proc (SCHEME_A_ op);
5565 new_slot_in_env (SCHEME_A_ x, y); 5641 new_slot_in_env (SCHEME_A_ x, y);
5573 ivalue_unchecked (y) = op; 5649 ivalue_unchecked (y) = op;
5574 return y; 5650 return y;
5575} 5651}
5576 5652
5577/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5653/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5578static int 5654ecb_hot static int
5579syntaxnum (pointer p) 5655syntaxnum (pointer p)
5580{ 5656{
5581 const char *s = strvalue (p); 5657 const char *s = strvalue (p);
5582 5658
5583 switch (strlength (p)) 5659 switch (strlength (p))
5700#endif 5776#endif
5701 } 5777 }
5702 5778
5703 SCHEME_V->gc_verbose = 0; 5779 SCHEME_V->gc_verbose = 0;
5704 dump_stack_initialize (SCHEME_A); 5780 dump_stack_initialize (SCHEME_A);
5705 SCHEME_V->code = NIL; 5781 SCHEME_V->code = NIL;
5706 SCHEME_V->args = NIL; 5782 SCHEME_V->args = NIL;
5707 SCHEME_V->envir = NIL; 5783 SCHEME_V->envir = NIL;
5784 SCHEME_V->value = NIL;
5708 SCHEME_V->tracing = 0; 5785 SCHEME_V->tracing = 0;
5709 5786
5710 /* init NIL */ 5787 /* init NIL */
5711 set_typeflag (NIL, T_ATOM | T_MARK); 5788 set_typeflag (NIL, T_ATOM | T_MARK);
5712 set_car (NIL, NIL); 5789 set_car (NIL, NIL);
5768 5845
5769 return !SCHEME_V->no_memory; 5846 return !SCHEME_V->no_memory;
5770} 5847}
5771 5848
5772#if USE_PORTS 5849#if USE_PORTS
5773void 5850ecb_cold void
5774scheme_set_input_port_file (SCHEME_P_ int fin) 5851scheme_set_input_port_file (SCHEME_P_ int fin)
5775{ 5852{
5776 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); 5853 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input);
5777} 5854}
5778 5855
5779void 5856ecb_cold void
5780scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) 5857scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end)
5781{ 5858{
5782 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); 5859 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input);
5783} 5860}
5784 5861
5785void 5862ecb_cold void
5786scheme_set_output_port_file (SCHEME_P_ int fout) 5863scheme_set_output_port_file (SCHEME_P_ int fout)
5787{ 5864{
5788 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); 5865 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output);
5789} 5866}
5790 5867
5791void 5868ecb_cold void
5792scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) 5869scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end)
5793{ 5870{
5794 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); 5871 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output);
5795} 5872}
5796#endif 5873#endif
5797 5874
5798void 5875ecb_cold void
5799scheme_set_external_data (SCHEME_P_ void *p) 5876scheme_set_external_data (SCHEME_P_ void *p)
5800{ 5877{
5801 SCHEME_V->ext_data = p; 5878 SCHEME_V->ext_data = p;
5802} 5879}
5803 5880
5851 } 5928 }
5852 } 5929 }
5853#endif 5930#endif
5854} 5931}
5855 5932
5856void 5933ecb_cold void
5857scheme_load_file (SCHEME_P_ int fin) 5934scheme_load_file (SCHEME_P_ int fin)
5858{ 5935{
5859 scheme_load_named_file (SCHEME_A_ fin, 0); 5936 scheme_load_named_file (SCHEME_A_ fin, 0);
5860} 5937}
5861 5938
5862void 5939ecb_cold void
5863scheme_load_named_file (SCHEME_P_ int fin, const char *filename) 5940scheme_load_named_file (SCHEME_P_ int fin, const char *filename)
5864{ 5941{
5865 dump_stack_reset (SCHEME_A); 5942 dump_stack_reset (SCHEME_A);
5866 SCHEME_V->envir = SCHEME_V->global_env; 5943 SCHEME_V->envir = SCHEME_V->global_env;
5867 SCHEME_V->file_i = 0; 5944 SCHEME_V->file_i = 0;
5868 SCHEME_V->load_stack[0].unget = -1; 5945 SCHEME_V->load_stack[0].unget = -1;
5869 SCHEME_V->load_stack[0].kind = port_input | port_file; 5946 SCHEME_V->load_stack[0].kind = port_input | port_file;
5870 SCHEME_V->load_stack[0].rep.stdio.file = fin; 5947 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); 5948 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5873#endif
5874 SCHEME_V->retcode = 0; 5949 SCHEME_V->retcode = 0;
5875 5950
5876#if USE_PORTS
5877 if (fin == STDIN_FILENO) 5951 if (fin == STDIN_FILENO)
5878 SCHEME_V->interactive_repl = 1; 5952 SCHEME_V->interactive_repl = 1;
5879#endif
5880 5953
5881#if USE_PORTS 5954#if USE_PORTS
5882#if SHOW_ERROR_LINE 5955#if SHOW_ERROR_LINE
5883 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; 5956 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0;
5884 5957
5888#endif 5961#endif
5889 5962
5890 SCHEME_V->inport = SCHEME_V->loadport; 5963 SCHEME_V->inport = SCHEME_V->loadport;
5891 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 5964 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5892 Eval_Cycle (SCHEME_A_ OP_T0LVL); 5965 Eval_Cycle (SCHEME_A_ OP_T0LVL);
5966
5893 set_typeflag (SCHEME_V->loadport, T_ATOM); 5967 set_typeflag (SCHEME_V->loadport, T_ATOM);
5894 5968
5895 if (SCHEME_V->retcode == 0) 5969 if (SCHEME_V->retcode == 0)
5896 SCHEME_V->retcode = SCHEME_V->nesting != 0; 5970 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5897} 5971}
5898 5972
5899void 5973ecb_cold void
5900scheme_load_string (SCHEME_P_ const char *cmd) 5974scheme_load_string (SCHEME_P_ const char *cmd)
5901{ 5975{
5976#if USE_PORTs
5902 dump_stack_reset (SCHEME_A); 5977 dump_stack_reset (SCHEME_A);
5903 SCHEME_V->envir = SCHEME_V->global_env; 5978 SCHEME_V->envir = SCHEME_V->global_env;
5904 SCHEME_V->file_i = 0; 5979 SCHEME_V->file_i = 0;
5905 SCHEME_V->load_stack[0].kind = port_input | port_string; 5980 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 */ 5981 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); 5982 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; 5983 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); 5984 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5911#endif
5912 SCHEME_V->retcode = 0; 5985 SCHEME_V->retcode = 0;
5913 SCHEME_V->interactive_repl = 0; 5986 SCHEME_V->interactive_repl = 0;
5914 SCHEME_V->inport = SCHEME_V->loadport; 5987 SCHEME_V->inport = SCHEME_V->loadport;
5915 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 5988 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5916 Eval_Cycle (SCHEME_A_ OP_T0LVL); 5989 Eval_Cycle (SCHEME_A_ OP_T0LVL);
5917 set_typeflag (SCHEME_V->loadport, T_ATOM); 5990 set_typeflag (SCHEME_V->loadport, T_ATOM);
5918 5991
5919 if (SCHEME_V->retcode == 0) 5992 if (SCHEME_V->retcode == 0)
5920 SCHEME_V->retcode = SCHEME_V->nesting != 0; 5993 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5994#else
5995 abort ();
5996#endif
5921} 5997}
5922 5998
5923void 5999ecb_cold void
5924scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) 6000scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value)
5925{ 6001{
5926 pointer x; 6002 pointer x;
5927 6003
5928 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 6004 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
5933 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 6009 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
5934} 6010}
5935 6011
5936#if !STANDALONE 6012#if !STANDALONE
5937 6013
5938void 6014ecb_cold void
5939scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 6015scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
5940{ 6016{
5941 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); 6017 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f));
5942} 6018}
5943 6019
5944void 6020ecb_cold void
5945scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 6021scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
5946{ 6022{
5947 int i; 6023 int i;
5948 6024
5949 for (i = 0; i < count; i++) 6025 for (i = 0; i < count; i++)
5950 scheme_register_foreign_func (SCHEME_A_ list + i); 6026 scheme_register_foreign_func (SCHEME_A_ list + i);
5951} 6027}
5952 6028
5953pointer 6029ecb_cold pointer
5954scheme_apply0 (SCHEME_P_ const char *procname) 6030scheme_apply0 (SCHEME_P_ const char *procname)
5955{ 6031{
5956 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); 6032 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL));
5957} 6033}
5958 6034
5959void 6035ecb_cold void
5960save_from_C_call (SCHEME_P) 6036save_from_C_call (SCHEME_P)
5961{ 6037{
5962 pointer saved_data = cons (car (S_SINK), 6038 pointer saved_data = cons (car (S_SINK),
5963 cons (SCHEME_V->envir, 6039 cons (SCHEME_V->envir,
5964 SCHEME_V->dump)); 6040 SCHEME_V->dump));
5968 /* Truncate the dump stack so TS will return here when done, not 6044 /* Truncate the dump stack so TS will return here when done, not
5969 directly resume pre-C-call operations. */ 6045 directly resume pre-C-call operations. */
5970 dump_stack_reset (SCHEME_A); 6046 dump_stack_reset (SCHEME_A);
5971} 6047}
5972 6048
5973void 6049ecb_cold void
5974restore_from_C_call (SCHEME_P) 6050restore_from_C_call (SCHEME_P)
5975{ 6051{
5976 set_car (S_SINK, caar (SCHEME_V->c_nest)); 6052 set_car (S_SINK, caar (SCHEME_V->c_nest));
5977 SCHEME_V->envir = cadar (SCHEME_V->c_nest); 6053 SCHEME_V->envir = cadar (SCHEME_V->c_nest);
5978 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); 6054 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest));
5979 /* Pop */ 6055 /* Pop */
5980 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); 6056 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest);
5981} 6057}
5982 6058
5983/* "func" and "args" are assumed to be already eval'ed. */ 6059/* "func" and "args" are assumed to be already eval'ed. */
5984pointer 6060ecb_cold pointer
5985scheme_call (SCHEME_P_ pointer func, pointer args) 6061scheme_call (SCHEME_P_ pointer func, pointer args)
5986{ 6062{
5987 int old_repl = SCHEME_V->interactive_repl; 6063 int old_repl = SCHEME_V->interactive_repl;
5988 6064
5989 SCHEME_V->interactive_repl = 0; 6065 SCHEME_V->interactive_repl = 0;
5996 SCHEME_V->interactive_repl = old_repl; 6072 SCHEME_V->interactive_repl = old_repl;
5997 restore_from_C_call (SCHEME_A); 6073 restore_from_C_call (SCHEME_A);
5998 return SCHEME_V->value; 6074 return SCHEME_V->value;
5999} 6075}
6000 6076
6001pointer 6077ecb_cold pointer
6002scheme_eval (SCHEME_P_ pointer obj) 6078scheme_eval (SCHEME_P_ pointer obj)
6003{ 6079{
6004 int old_repl = SCHEME_V->interactive_repl; 6080 int old_repl = SCHEME_V->interactive_repl;
6005 6081
6006 SCHEME_V->interactive_repl = 0; 6082 SCHEME_V->interactive_repl = 0;
6018 6094
6019/* ========== Main ========== */ 6095/* ========== Main ========== */
6020 6096
6021#if STANDALONE 6097#if STANDALONE
6022 6098
6023int 6099ecb_cold int
6024main (int argc, char **argv) 6100main (int argc, char **argv)
6025{ 6101{
6026# if USE_MULTIPLICITY 6102# if USE_MULTIPLICITY
6027 scheme ssc; 6103 scheme ssc;
6028 scheme *const SCHEME_V = &ssc; 6104 scheme *const SCHEME_V = &ssc;
6069 } 6145 }
6070#endif 6146#endif
6071 6147
6072 do 6148 do
6073 { 6149 {
6074#if USE_PORTS
6075 if (strcmp (file_name, "-") == 0) 6150 if (strcmp (file_name, "-") == 0)
6076 fin = STDIN_FILENO; 6151 fin = STDIN_FILENO;
6077 else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) 6152 else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0)
6078 { 6153 {
6079 pointer args = NIL; 6154 pointer args = NIL;
6097 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); 6172 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args);
6098 6173
6099 } 6174 }
6100 else 6175 else
6101 fin = open (file_name, O_RDONLY); 6176 fin = open (file_name, O_RDONLY);
6102#endif
6103 6177
6104 if (isfile && fin < 0) 6178 if (isfile && fin < 0)
6105 { 6179 {
6106 putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); 6180 putstr (SCHEME_A_ "Could not open file ");
6181 putstr (SCHEME_A_ file_name);
6182 putcharacter (SCHEME_A_ '\n');
6107 } 6183 }
6108 else 6184 else
6109 { 6185 {
6110 if (isfile) 6186 if (isfile)
6111 scheme_load_named_file (SCHEME_A_ fin, file_name); 6187 scheme_load_named_file (SCHEME_A_ fin, file_name);
6112 else 6188 else
6113 scheme_load_string (SCHEME_A_ file_name); 6189 scheme_load_string (SCHEME_A_ file_name);
6114 6190
6115#if USE_PORTS
6116 if (!isfile || fin != STDIN_FILENO) 6191 if (!isfile || fin != STDIN_FILENO)
6117 { 6192 {
6118 if (SCHEME_V->retcode != 0) 6193 if (SCHEME_V->retcode != 0)
6119 { 6194 {
6120 putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); 6195 putstr (SCHEME_A_ "Errors encountered reading ");
6196 putstr (SCHEME_A_ file_name);
6197 putcharacter (SCHEME_A_ '\n');
6121 } 6198 }
6122 6199
6123 if (isfile) 6200 if (isfile)
6124 close (fin); 6201 close (fin);
6125 } 6202 }
6126#endif
6127 } 6203 }
6128 6204
6129 file_name = *argv++; 6205 file_name = *argv++;
6130 } 6206 }
6131 while (file_name != 0); 6207 while (file_name != 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines