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.59 by root, Tue Dec 1 07:13:25 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)
200 T_STRING, 211 T_STRING,
201 T_SYMBOL, 212 T_SYMBOL,
202 T_PROC, 213 T_PROC,
203 T_PAIR, /* also used for free cells */ 214 T_PAIR, /* also used for free cells */
204 T_CLOSURE, 215 T_CLOSURE,
216 T_BYTECODE, // temp
205 T_MACRO, 217 T_MACRO,
206 T_CONTINUATION, 218 T_CONTINUATION,
207 T_FOREIGN, 219 T_FOREIGN,
208 T_PORT, 220 T_PORT,
209 T_VECTOR, 221 T_VECTOR,
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
520 proper list: length 532 proper list: length
521 circular list: -1 533 circular list: -1
522 not even a pair: -2 534 not even a pair: -2
523 dotted list: -2 minus length before dot 535 dotted list: -2 minus length before dot
524*/ 536*/
525INTERFACE int 537ecb_hot INTERFACE int
526list_length (SCHEME_P_ pointer a) 538list_length (SCHEME_P_ pointer a)
527{ 539{
528 int i = 0; 540 int i = 0;
529 pointer slow, fast; 541 pointer slow, fast;
530 542
569{ 581{
570 return list_length (SCHEME_A_ a) >= 0; 582 return list_length (SCHEME_A_ a) >= 0;
571} 583}
572 584
573#if USE_CHAR_CLASSIFIERS 585#if USE_CHAR_CLASSIFIERS
586
574ecb_inline int 587ecb_inline int
575Cisalpha (int c) 588Cisalpha (int c)
576{ 589{
577 return isascii (c) && isalpha (c); 590 return isascii (c) && isalpha (c);
578} 591}
636 "gs", 649 "gs",
637 "rs", 650 "rs",
638 "us" 651 "us"
639}; 652};
640 653
641static int 654ecb_cold static int
642is_ascii_name (const char *name, int *pc) 655is_ascii_name (const char *name, int *pc)
643{ 656{
644 int i; 657 int i;
645 658
646 for (i = 0; i < 32; i++) 659 for (i = 0; i < 32; i++)
668static int file_interactive (SCHEME_P); 681static int file_interactive (SCHEME_P);
669ecb_inline int is_one_of (const char *s, int c); 682ecb_inline int is_one_of (const char *s, int c);
670static int alloc_cellseg (SCHEME_P); 683static int alloc_cellseg (SCHEME_P);
671ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); 684ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
672static void finalize_cell (SCHEME_P_ pointer a); 685static void finalize_cell (SCHEME_P_ pointer a);
673static int count_consecutive_cells (pointer x, int needed);
674static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 686static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
675static pointer mk_number (SCHEME_P_ const num n); 687static pointer mk_number (SCHEME_P_ const num n);
676static 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);
677static pointer mk_vector (SCHEME_P_ uint32_t len); 689static pointer mk_vector (SCHEME_P_ uint32_t len);
678static pointer mk_atom (SCHEME_P_ char *q); 690static pointer mk_atom (SCHEME_P_ char *q);
679static pointer mk_sharp_const (SCHEME_P_ char *name); 691static pointer mk_sharp_const (SCHEME_P_ char *name);
680 692
693static pointer mk_port (SCHEME_P_ port *p);
694
681#if USE_PORTS 695#if USE_PORTS
682static pointer mk_port (SCHEME_P_ port *p);
683static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); 696static pointer port_from_filename (SCHEME_P_ const char *fn, int prop);
684static pointer port_from_file (SCHEME_P_ int, int prop); 697static pointer port_from_file (SCHEME_P_ int, int prop);
685static 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);
686static 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);
687static port *port_rep_from_file (SCHEME_P_ int, int prop); 700static port *port_rep_from_file (SCHEME_P_ int, int prop);
688static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); 701static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop);
689static void port_close (SCHEME_P_ pointer p, int flag); 702static void port_close (SCHEME_P_ pointer p, int flag);
690#endif 703#endif
704
691static void mark (pointer a); 705static void mark (pointer a);
692static void gc (SCHEME_P_ pointer a, pointer b); 706static void gc (SCHEME_P_ pointer a, pointer b);
693static int basic_inchar (port *pt); 707static int basic_inchar (port *pt);
694static int inchar (SCHEME_P); 708static int inchar (SCHEME_P);
695static void backchar (SCHEME_P_ int c); 709static void backchar (SCHEME_P_ int c);
883#endif 897#endif
884#endif 898#endif
885} 899}
886 900
887/* allocate new cell segment */ 901/* allocate new cell segment */
888static int 902ecb_cold static int
889alloc_cellseg (SCHEME_P) 903alloc_cellseg (SCHEME_P)
890{ 904{
891 struct cell *newp; 905 struct cell *newp;
892 struct cell *last; 906 struct cell *last;
893 struct cell *p; 907 struct cell *p;
935 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 949 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
936 return S_SINK; 950 return S_SINK;
937 951
938 if (SCHEME_V->free_cell == NIL) 952 if (SCHEME_V->free_cell == NIL)
939 { 953 {
940 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;
941 955
942 gc (SCHEME_A_ a, b); 956 gc (SCHEME_A_ a, b);
943 957
944 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)
945 { 959 {
964 } 978 }
965} 979}
966 980
967/* To retain recent allocs before interpreter knows about them - 981/* To retain recent allocs before interpreter knows about them -
968 Tehom */ 982 Tehom */
969static void 983ecb_hot static void
970push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 984push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
971{ 985{
972 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 986 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
973 987
974 set_typeflag (holder, T_PAIR); 988 set_typeflag (holder, T_PAIR);
976 set_car (holder, recent); 990 set_car (holder, recent);
977 set_cdr (holder, car (S_SINK)); 991 set_cdr (holder, car (S_SINK));
978 set_car (S_SINK, holder); 992 set_car (S_SINK, holder);
979} 993}
980 994
981static pointer 995ecb_hot static pointer
982get_cell (SCHEME_P_ pointer a, pointer b) 996get_cell (SCHEME_P_ pointer a, pointer b)
983{ 997{
984 pointer cell = get_cell_x (SCHEME_A_ a, b); 998 pointer cell = get_cell_x (SCHEME_A_ a, b);
985 999
986 /* 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
1043#endif 1057#endif
1044 1058
1045/* Medium level cell allocation */ 1059/* Medium level cell allocation */
1046 1060
1047/* get new cons cell */ 1061/* get new cons cell */
1048pointer 1062ecb_hot static pointer
1049xcons (SCHEME_P_ pointer a, pointer b, int immutable) 1063xcons (SCHEME_P_ pointer a, pointer b)
1050{ 1064{
1051 pointer x = get_cell (SCHEME_A_ a, b); 1065 pointer x = get_cell (SCHEME_A_ a, b);
1052 1066
1053 set_typeflag (x, T_PAIR); 1067 set_typeflag (x, T_PAIR);
1054
1055 if (immutable)
1056 setimmutable (x);
1057 1068
1058 set_car (x, a); 1069 set_car (x, a);
1059 set_cdr (x, b); 1070 set_cdr (x, b);
1060 1071
1061 return x; 1072 return x;
1062} 1073}
1063 1074
1064static 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
1065generate_symbol (SCHEME_P_ const char *name) 1087generate_symbol (SCHEME_P_ const char *name)
1066{ 1088{
1067 pointer x = mk_string (SCHEME_A_ name); 1089 pointer x = mk_string (SCHEME_A_ name);
1068 setimmutable (x); 1090 setimmutable (x);
1069 set_typeflag (x, T_SYMBOL | T_ATOM); 1091 set_typeflag (x, T_SYMBOL | T_ATOM);
1075#ifndef USE_OBJECT_LIST 1097#ifndef USE_OBJECT_LIST
1076 1098
1077static int 1099static int
1078hash_fn (const char *key, int table_size) 1100hash_fn (const char *key, int table_size)
1079{ 1101{
1080 const unsigned char *p = key; 1102 const unsigned char *p = (unsigned char *)key;
1081 uint32_t hash = 2166136261; 1103 uint32_t hash = 2166136261;
1082 1104
1083 while (*p) 1105 while (*p)
1084 hash = (hash ^ *p++) * 16777619; 1106 hash = (hash ^ *p++) * 16777619;
1085 1107
1086 return hash % table_size; 1108 return hash % table_size;
1087} 1109}
1088 1110
1089static pointer 1111ecb_cold static pointer
1090oblist_initial_value (SCHEME_P) 1112oblist_initial_value (SCHEME_P)
1091{ 1113{
1092 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1114 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1093} 1115}
1094 1116
1095/* returns the new symbol */ 1117/* returns the new symbol */
1096static pointer 1118ecb_cold static pointer
1097oblist_add_by_name (SCHEME_P_ const char *name) 1119oblist_add_by_name (SCHEME_P_ const char *name)
1098{ 1120{
1099 pointer x = generate_symbol (SCHEME_A_ name); 1121 pointer x = generate_symbol (SCHEME_A_ name);
1100 int location = hash_fn (name, veclength (SCHEME_V->oblist)); 1122 int location = hash_fn (name, veclength (SCHEME_V->oblist));
1101 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); 1123 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1102 return x; 1124 return x;
1103} 1125}
1104 1126
1105ecb_inline pointer 1127ecb_cold static pointer
1106oblist_find_by_name (SCHEME_P_ const char *name) 1128oblist_find_by_name (SCHEME_P_ const char *name)
1107{ 1129{
1108 int location; 1130 int location;
1109 pointer x; 1131 pointer x;
1110 char *s; 1132 char *s;
1121 } 1143 }
1122 1144
1123 return NIL; 1145 return NIL;
1124} 1146}
1125 1147
1126static pointer 1148ecb_cold static pointer
1127oblist_all_symbols (SCHEME_P) 1149oblist_all_symbols (SCHEME_P)
1128{ 1150{
1129 int i; 1151 int i;
1130 pointer x; 1152 pointer x;
1131 pointer ob_list = NIL; 1153 pointer ob_list = NIL;
1137 return ob_list; 1159 return ob_list;
1138} 1160}
1139 1161
1140#else 1162#else
1141 1163
1142static pointer 1164ecb_cold static pointer
1143oblist_initial_value (SCHEME_P) 1165oblist_initial_value (SCHEME_P)
1144{ 1166{
1145 return NIL; 1167 return NIL;
1146} 1168}
1147 1169
1148ecb_inline pointer 1170ecb_cold static pointer
1149oblist_find_by_name (SCHEME_P_ const char *name) 1171oblist_find_by_name (SCHEME_P_ const char *name)
1150{ 1172{
1151 pointer x; 1173 pointer x;
1152 char *s; 1174 char *s;
1153 1175
1162 1184
1163 return NIL; 1185 return NIL;
1164} 1186}
1165 1187
1166/* returns the new symbol */ 1188/* returns the new symbol */
1167static pointer 1189ecb_cold static pointer
1168oblist_add_by_name (SCHEME_P_ const char *name) 1190oblist_add_by_name (SCHEME_P_ const char *name)
1169{ 1191{
1170 pointer x = generate_symbol (SCHEME_A_ name); 1192 pointer x = generate_symbol (SCHEME_A_ name);
1171 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1193 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1172 return x; 1194 return x;
1173} 1195}
1174 1196
1175static pointer 1197ecb_cold static pointer
1176oblist_all_symbols (SCHEME_P) 1198oblist_all_symbols (SCHEME_P)
1177{ 1199{
1178 return SCHEME_V->oblist; 1200 return SCHEME_V->oblist;
1179} 1201}
1180 1202
1181#endif 1203#endif
1182 1204
1183#if USE_PORTS
1184static pointer 1205ecb_cold static pointer
1185mk_port (SCHEME_P_ port *p) 1206mk_port (SCHEME_P_ port *p)
1186{ 1207{
1187 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1208 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1188 1209
1189 set_typeflag (x, T_PORT | T_ATOM); 1210 set_typeflag (x, T_PORT | T_ATOM);
1190 set_port (x, p); 1211 set_port (x, p);
1191 1212
1192 return x; 1213 return x;
1193} 1214}
1194#endif
1195 1215
1196pointer 1216ecb_cold pointer
1197mk_foreign_func (SCHEME_P_ foreign_func f) 1217mk_foreign_func (SCHEME_P_ foreign_func f)
1198{ 1218{
1199 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1219 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1200 1220
1201 set_typeflag (x, T_FOREIGN | T_ATOM); 1221 set_typeflag (x, T_FOREIGN | T_ATOM);
1366 x = oblist_add_by_name (SCHEME_A_ name); 1386 x = oblist_add_by_name (SCHEME_A_ name);
1367 1387
1368 return x; 1388 return x;
1369} 1389}
1370 1390
1371INTERFACE pointer 1391ecb_cold INTERFACE pointer
1372gensym (SCHEME_P) 1392gensym (SCHEME_P)
1373{ 1393{
1374 pointer x; 1394 pointer x;
1375 char name[40] = "gensym-"; 1395 char name[40] = "gensym-";
1376 xnum (name + 7, ++SCHEME_V->gensym_cnt); 1396 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1383{ 1403{
1384 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;
1385} 1405}
1386 1406
1387/* make symbol or number atom from string */ 1407/* make symbol or number atom from string */
1388static pointer 1408ecb_cold static pointer
1389mk_atom (SCHEME_P_ char *q) 1409mk_atom (SCHEME_P_ char *q)
1390{ 1410{
1391 char c, *p; 1411 char c, *p;
1392 int has_dec_point = 0; 1412 int has_dec_point = 0;
1393 int has_fp_exp = 0; 1413 int has_fp_exp = 0;
1464 1484
1465 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1485 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1466} 1486}
1467 1487
1468/* make constant */ 1488/* make constant */
1469static pointer 1489ecb_cold static pointer
1470mk_sharp_const (SCHEME_P_ char *name) 1490mk_sharp_const (SCHEME_P_ char *name)
1471{ 1491{
1472 if (!strcmp (name, "t")) 1492 if (!strcmp (name, "t"))
1473 return S_T; 1493 return S_T;
1474 else if (!strcmp (name, "f")) 1494 else if (!strcmp (name, "f"))
1529 } 1549 }
1530} 1550}
1531 1551
1532/* ========== garbage collector ========== */ 1552/* ========== garbage collector ========== */
1533 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}
1572
1534/*-- 1573/*--
1535 * 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,
1536 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1575 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1537 * for marking. 1576 * for marking.
1538 * 1577 *
1539 * The exception is vectors - vectors are currently marked recursively, 1578 * The exception is vectors - vectors are currently marked recursively,
1540 * 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
1541 * word of context in the vector 1580 * word of context in the vector
1542 */ 1581 */
1543static void 1582ecb_hot static void
1544mark (pointer a) 1583mark (pointer a)
1545{ 1584{
1546 pointer t, q, p; 1585 pointer t, q, p;
1547 1586
1548 t = 0; 1587 t = 0;
1605 p = q; 1644 p = q;
1606 goto E6; 1645 goto E6;
1607 } 1646 }
1608} 1647}
1609 1648
1610/* garbage collection. parameter a, b is marked. */ 1649ecb_hot static void
1611static void 1650gc_free (SCHEME_P)
1612gc (SCHEME_P_ pointer a, pointer b)
1613{ 1651{
1614 int i; 1652 int i;
1615
1616 if (SCHEME_V->gc_verbose)
1617 putstr (SCHEME_A_ "gc...");
1618
1619 /* mark system globals */
1620 mark (SCHEME_V->oblist);
1621 mark (SCHEME_V->global_env);
1622
1623 /* mark current registers */
1624 mark (SCHEME_V->args);
1625 mark (SCHEME_V->envir);
1626 mark (SCHEME_V->code);
1627 dump_stack_mark (SCHEME_A);
1628 mark (SCHEME_V->value);
1629 mark (SCHEME_V->inport);
1630 mark (SCHEME_V->save_inport);
1631 mark (SCHEME_V->outport);
1632 mark (SCHEME_V->loadport);
1633
1634 /* Mark recent objects the interpreter doesn't know about yet. */
1635 mark (car (S_SINK));
1636 /* Mark any older stuff above nested C calls */
1637 mark (SCHEME_V->c_nest);
1638
1639#if USE_INTCACHE
1640 /* mark intcache */
1641 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1642 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1643 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1644#endif
1645
1646 /* mark variables a, b */
1647 mark (a);
1648 mark (b);
1649
1650 /* garbage collect */
1651 clrmark (NIL);
1652 SCHEME_V->fcells = 0;
1653 SCHEME_V->free_cell = NIL;
1654
1655 if (SCHEME_V->gc_verbose)
1656 putstr (SCHEME_A_ "freeing...");
1657
1658 uint32_t total = 0; 1653 uint32_t total = 0;
1659 1654
1660 /* Here we scan the cells to build the free-list. */ 1655 /* Here we scan the cells to build the free-list. */
1661 for (i = SCHEME_V->last_cell_seg; i >= 0; i--) 1656 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1662 { 1657 {
1691 { 1686 {
1692 putstr (SCHEME_A_ "done: "); putnum (SCHEME_A_ SCHEME_V->fcells); putstr (SCHEME_A_ " out of "); putnum (SCHEME_A_ total); putstr (SCHEME_A_ " cells were recovered.\n"); 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");
1693 } 1688 }
1694} 1689}
1695 1690
1696static void 1691/* garbage collection. parameter a, b is marked. */
1697finalize_cell (SCHEME_P_ pointer a) 1692ecb_cold static void
1693gc (SCHEME_P_ pointer a, pointer b)
1698{ 1694{
1699 /* TODO, fast bitmap check? */ 1695 int i;
1700 if (is_string (a) || is_symbol (a))
1701 free (strvalue (a));
1702 else if (is_vector (a))
1703 free (vecvalue (a));
1704#if USE_PORTS
1705 else if (is_port (a))
1706 {
1707 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit)
1708 port_close (SCHEME_A_ a, port_input | port_output);
1709 1696
1710 free (port (a)); 1697 if (SCHEME_V->gc_verbose)
1711 } 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]);
1712#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);
1713} 1740}
1714 1741
1715/* ========== Routines for Reading ========== */ 1742/* ========== Routines for Reading ========== */
1716 1743
1717static int 1744ecb_cold static int
1718file_push (SCHEME_P_ const char *fname) 1745file_push (SCHEME_P_ const char *fname)
1719{ 1746{
1720#if USE_PORTS
1721 int fin; 1747 int fin;
1722 1748
1723 if (SCHEME_V->file_i == MAXFIL - 1) 1749 if (SCHEME_V->file_i == MAXFIL - 1)
1724 return 0; 1750 return 0;
1725 1751
1742 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);
1743#endif 1769#endif
1744 } 1770 }
1745 1771
1746 return fin >= 0; 1772 return fin >= 0;
1747
1748#else
1749 return 1;
1750#endif
1751} 1773}
1752 1774
1753static void 1775ecb_cold static void
1754file_pop (SCHEME_P) 1776file_pop (SCHEME_P)
1755{ 1777{
1756 if (SCHEME_V->file_i != 0) 1778 if (SCHEME_V->file_i != 0)
1757 { 1779 {
1758 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1780 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1762 SCHEME_V->file_i--; 1784 SCHEME_V->file_i--;
1763 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);
1764 } 1786 }
1765} 1787}
1766 1788
1767static int 1789ecb_cold static int
1768file_interactive (SCHEME_P) 1790file_interactive (SCHEME_P)
1769{ 1791{
1770#if USE_PORTS 1792#if USE_PORTS
1771 return SCHEME_V->file_i == 0 1793 return SCHEME_V->file_i == 0
1772 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1794 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1775 return 0; 1797 return 0;
1776#endif 1798#endif
1777} 1799}
1778 1800
1779#if USE_PORTS 1801#if USE_PORTS
1780static port * 1802ecb_cold static port *
1781port_rep_from_filename (SCHEME_P_ const char *fn, int prop) 1803port_rep_from_filename (SCHEME_P_ const char *fn, int prop)
1782{ 1804{
1783 int fd; 1805 int fd;
1784 int flags; 1806 int flags;
1785 char *rw; 1807 char *rw;
1808# endif 1830# endif
1809 1831
1810 return pt; 1832 return pt;
1811} 1833}
1812 1834
1813static pointer 1835ecb_cold static pointer
1814port_from_filename (SCHEME_P_ const char *fn, int prop) 1836port_from_filename (SCHEME_P_ const char *fn, int prop)
1815{ 1837{
1816 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); 1838 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop);
1817 1839
1818 if (!pt && USE_ERROR_CHECKING) 1840 if (!pt && USE_ERROR_CHECKING)
1819 return NIL; 1841 return NIL;
1820 1842
1821 return mk_port (SCHEME_A_ pt); 1843 return mk_port (SCHEME_A_ pt);
1822} 1844}
1823 1845
1824static port * 1846ecb_cold static port *
1825port_rep_from_file (SCHEME_P_ int f, int prop) 1847port_rep_from_file (SCHEME_P_ int f, int prop)
1826{ 1848{
1827 port *pt = malloc (sizeof *pt); 1849 port *pt = malloc (sizeof *pt);
1828 1850
1829 if (!pt && USE_ERROR_CHECKING) 1851 if (!pt && USE_ERROR_CHECKING)
1834 pt->rep.stdio.file = f; 1856 pt->rep.stdio.file = f;
1835 pt->rep.stdio.closeit = 0; 1857 pt->rep.stdio.closeit = 0;
1836 return pt; 1858 return pt;
1837} 1859}
1838 1860
1839static pointer 1861ecb_cold static pointer
1840port_from_file (SCHEME_P_ int f, int prop) 1862port_from_file (SCHEME_P_ int f, int prop)
1841{ 1863{
1842 port *pt = port_rep_from_file (SCHEME_A_ f, prop); 1864 port *pt = port_rep_from_file (SCHEME_A_ f, prop);
1843 1865
1844 if (!pt && USE_ERROR_CHECKING) 1866 if (!pt && USE_ERROR_CHECKING)
1845 return NIL; 1867 return NIL;
1846 1868
1847 return mk_port (SCHEME_A_ pt); 1869 return mk_port (SCHEME_A_ pt);
1848} 1870}
1849 1871
1850static port * 1872ecb_cold static port *
1851port_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)
1852{ 1874{
1853 port *pt = malloc (sizeof (port)); 1875 port *pt = malloc (sizeof (port));
1854 1876
1855 if (!pt && USE_ERROR_CHECKING) 1877 if (!pt && USE_ERROR_CHECKING)
1861 pt->rep.string.curr = start; 1883 pt->rep.string.curr = start;
1862 pt->rep.string.past_the_end = past_the_end; 1884 pt->rep.string.past_the_end = past_the_end;
1863 return pt; 1885 return pt;
1864} 1886}
1865 1887
1866static pointer 1888ecb_cold static pointer
1867port_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)
1868{ 1890{
1869 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);
1870 1892
1871 if (!pt && USE_ERROR_CHECKING) 1893 if (!pt && USE_ERROR_CHECKING)
1874 return mk_port (SCHEME_A_ pt); 1896 return mk_port (SCHEME_A_ pt);
1875} 1897}
1876 1898
1877# define BLOCK_SIZE 256 1899# define BLOCK_SIZE 256
1878 1900
1879static port * 1901ecb_cold static port *
1880port_rep_from_scratch (SCHEME_P) 1902port_rep_from_scratch (SCHEME_P)
1881{ 1903{
1882 char *start; 1904 char *start;
1883 port *pt = malloc (sizeof (port)); 1905 port *pt = malloc (sizeof (port));
1884 1906
1898 pt->rep.string.curr = start; 1920 pt->rep.string.curr = start;
1899 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; 1921 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1;
1900 return pt; 1922 return pt;
1901} 1923}
1902 1924
1903static pointer 1925ecb_cold static pointer
1904port_from_scratch (SCHEME_P) 1926port_from_scratch (SCHEME_P)
1905{ 1927{
1906 port *pt = port_rep_from_scratch (SCHEME_A); 1928 port *pt = port_rep_from_scratch (SCHEME_A);
1907 1929
1908 if (!pt && USE_ERROR_CHECKING) 1930 if (!pt && USE_ERROR_CHECKING)
1909 return NIL; 1931 return NIL;
1910 1932
1911 return mk_port (SCHEME_A_ pt); 1933 return mk_port (SCHEME_A_ pt);
1912} 1934}
1913 1935
1914static void 1936ecb_cold static void
1915port_close (SCHEME_P_ pointer p, int flag) 1937port_close (SCHEME_P_ pointer p, int flag)
1916{ 1938{
1917 port *pt = port (p); 1939 port *pt = port (p);
1918 1940
1919 pt->kind &= ~flag; 1941 pt->kind &= ~flag;
1939 } 1961 }
1940} 1962}
1941#endif 1963#endif
1942 1964
1943/* get new character from input file */ 1965/* get new character from input file */
1944static int 1966ecb_cold static int
1945inchar (SCHEME_P) 1967inchar (SCHEME_P)
1946{ 1968{
1947 int c; 1969 int c;
1948 port *pt = port (SCHEME_V->inport); 1970 port *pt = port (SCHEME_V->inport);
1949 1971
1963 } 1985 }
1964 1986
1965 return c; 1987 return c;
1966} 1988}
1967 1989
1968static int ungot = -1; 1990ecb_cold static int
1969
1970static int
1971basic_inchar (port *pt) 1991basic_inchar (port *pt)
1972{ 1992{
1973#if USE_PORTS
1974 if (pt->unget != -1) 1993 if (pt->unget != -1)
1975 { 1994 {
1976 int r = pt->unget; 1995 int r = pt->unget;
1977 pt->unget = -1; 1996 pt->unget = -1;
1978 return r; 1997 return r;
1979 } 1998 }
1980 1999
2000#if USE_PORTS
1981 if (pt->kind & port_file) 2001 if (pt->kind & port_file)
1982 { 2002 {
1983 char c; 2003 char c;
1984 2004
1985 if (!read (pt->rep.stdio.file, &c, 1)) 2005 if (!read (pt->rep.stdio.file, &c, 1))
1993 return EOF; 2013 return EOF;
1994 else 2014 else
1995 return *pt->rep.string.curr++; 2015 return *pt->rep.string.curr++;
1996 } 2016 }
1997#else 2017#else
1998 if (ungot == -1)
1999 {
2000 char c; 2018 char c;
2001 if (!read (0, &c, 1)) 2019
2020 if (!read (pt->rep.stdio.file, &c, 1))
2002 return EOF; 2021 return EOF;
2003 2022
2004 ungot = c;
2005 }
2006
2007 {
2008 int r = ungot;
2009 ungot = -1;
2010 return r; 2023 return c;
2011 }
2012#endif 2024#endif
2013} 2025}
2014 2026
2015/* back character to input buffer */ 2027/* back character to input buffer */
2016static void 2028ecb_cold static void
2017backchar (SCHEME_P_ int c) 2029backchar (SCHEME_P_ int c)
2018{ 2030{
2019#if USE_PORTS 2031 port *pt = port (SCHEME_V->inport);
2020 port *pt;
2021 2032
2022 if (c == EOF) 2033 if (c == EOF)
2023 return; 2034 return;
2024 2035
2025 pt = port (SCHEME_V->inport);
2026 pt->unget = c; 2036 pt->unget = c;
2027#else
2028 if (c == EOF)
2029 return;
2030
2031 ungot = c;
2032#endif
2033} 2037}
2034 2038
2035#if USE_PORTS 2039#if USE_PORTS
2036static int 2040ecb_cold static int
2037realloc_port_string (SCHEME_P_ port *p) 2041realloc_port_string (SCHEME_P_ port *p)
2038{ 2042{
2039 char *start = p->rep.string.start; 2043 char *start = p->rep.string.start;
2040 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;
2041 char *str = malloc (new_size); 2045 char *str = malloc (new_size);
2054 else 2058 else
2055 return 0; 2059 return 0;
2056} 2060}
2057#endif 2061#endif
2058 2062
2059INTERFACE void 2063ecb_cold static void
2060putstr (SCHEME_P_ const char *s) 2064putchars (SCHEME_P_ const char *s, int len)
2061{ 2065{
2066 port *pt = port (SCHEME_V->outport);
2067
2062#if USE_PORTS 2068#if USE_PORTS
2063 port *pt = port (SCHEME_V->outport);
2064
2065 if (pt->kind & port_file)
2066 write (pt->rep.stdio.file, s, strlen (s));
2067 else
2068 for (; *s; s++)
2069 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2070 *pt->rep.string.curr++ = *s;
2071 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2072 *pt->rep.string.curr++ = *s;
2073
2074#else
2075 write (pt->rep.stdio.file, s, strlen (s));
2076#endif
2077}
2078
2079static void
2080putchars (SCHEME_P_ const char *s, int len)
2081{
2082#if USE_PORTS
2083 port *pt = port (SCHEME_V->outport);
2084
2085 if (pt->kind & port_file) 2069 if (pt->kind & port_file)
2086 write (pt->rep.stdio.file, s, len); 2070 write (pt->rep.stdio.file, s, len);
2087 else 2071 else
2088 { 2072 {
2089 for (; len; len--) 2073 for (; len; len--)
2094 *pt->rep.string.curr++ = *s++; 2078 *pt->rep.string.curr++ = *s++;
2095 } 2079 }
2096 } 2080 }
2097 2081
2098#else 2082#else
2099 write (1, s, len); 2083 write (1, s, len); // output not initialised
2100#endif 2084#endif
2085}
2086
2087INTERFACE void
2088putstr (SCHEME_P_ const char *s)
2089{
2090 putchars (SCHEME_A_ s, strlen (s));
2101} 2091}
2102 2092
2103INTERFACE void 2093INTERFACE void
2104putcharacter (SCHEME_P_ int c) 2094putcharacter (SCHEME_P_ int c)
2105{ 2095{
2106#if USE_PORTS
2107 port *pt = port (SCHEME_V->outport);
2108
2109 if (pt->kind & port_file)
2110 {
2111 char cc = c;
2112 write (pt->rep.stdio.file, &cc, 1);
2113 }
2114 else
2115 {
2116 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2117 *pt->rep.string.curr++ = c;
2118 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2119 *pt->rep.string.curr++ = c;
2120 }
2121
2122#else
2123 char cc = c; 2096 char cc = c;
2124 write (1, &c, 1); 2097
2125#endif 2098 putchars (SCHEME_A_ &cc, 1);
2126} 2099}
2127 2100
2128/* read characters up to delimiter, but cater to character constants */ 2101/* read characters up to delimiter, but cater to character constants */
2129static char * 2102ecb_cold static char *
2130readstr_upto (SCHEME_P_ int skip, const char *delim) 2103readstr_upto (SCHEME_P_ int skip, const char *delim)
2131{ 2104{
2132 char *p = SCHEME_V->strbuff + skip; 2105 char *p = SCHEME_V->strbuff + skip;
2133 2106
2134 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))));
2143 2116
2144 return SCHEME_V->strbuff; 2117 return SCHEME_V->strbuff;
2145} 2118}
2146 2119
2147/* read string expression "xxx...xxx" */ 2120/* read string expression "xxx...xxx" */
2148static pointer 2121ecb_cold static pointer
2149readstrexp (SCHEME_P_ char delim) 2122readstrexp (SCHEME_P_ char delim)
2150{ 2123{
2151 char *p = SCHEME_V->strbuff; 2124 char *p = SCHEME_V->strbuff;
2152 int c; 2125 int c;
2153 int c1 = 0; 2126 int c1 = 0;
2191 case 'a': *p++ = '\a'; state = st_ok; break; 2164 case 'a': *p++ = '\a'; state = st_ok; break;
2192 case 'n': *p++ = '\n'; state = st_ok; break; 2165 case 'n': *p++ = '\n'; state = st_ok; break;
2193 case 'r': *p++ = '\r'; state = st_ok; break; 2166 case 'r': *p++ = '\r'; state = st_ok; break;
2194 case 't': *p++ = '\t'; state = st_ok; break; 2167 case 't': *p++ = '\t'; state = st_ok; break;
2195 2168
2196 //TODO: \whitespace eol whitespace 2169 case '\\':
2170 skipspace (SCHEME_A);
2171 break;
2197 2172
2198 //TODO: x should end in ;, not two-digit hex 2173 //TODO: x should end in ;, not two-digit hex
2199 case 'x': 2174 case 'x':
2200 case 'X': 2175 case 'X':
2201 state = st_x1; 2176 state = st_x1;
2259 } 2234 }
2260 } 2235 }
2261} 2236}
2262 2237
2263/* check c is in chars */ 2238/* check c is in chars */
2264ecb_inline int 2239ecb_cold int
2265is_one_of (const char *s, int c) 2240is_one_of (const char *s, int c)
2266{ 2241{
2267 return c == EOF || !!strchr (s, c); 2242 return c == EOF || !!strchr (s, c);
2268} 2243}
2269 2244
2270/* skip white characters */ 2245/* skip white characters */
2271ecb_inline int 2246ecb_cold int
2272skipspace (SCHEME_P) 2247skipspace (SCHEME_P)
2273{ 2248{
2274 int c, curr_line = 0; 2249 int c, curr_line = 0;
2275 2250
2276 do 2251 do
2296 backchar (SCHEME_A_ c); 2271 backchar (SCHEME_A_ c);
2297 return 1; 2272 return 1;
2298} 2273}
2299 2274
2300/* get token */ 2275/* get token */
2301static int 2276ecb_cold static int
2302token (SCHEME_P) 2277token (SCHEME_P)
2303{ 2278{
2304 int c = skipspace (SCHEME_A); 2279 int c = skipspace (SCHEME_A);
2305 2280
2306 if (c == EOF) 2281 if (c == EOF)
2404} 2379}
2405 2380
2406/* ========== Routines for Printing ========== */ 2381/* ========== Routines for Printing ========== */
2407#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) 2382#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL)
2408 2383
2409static void 2384ecb_cold static void
2410printslashstring (SCHEME_P_ char *p, int len) 2385printslashstring (SCHEME_P_ char *p, int len)
2411{ 2386{
2412 int i; 2387 int i;
2413 unsigned char *s = (unsigned char *) p; 2388 unsigned char *s = (unsigned char *) p;
2414 2389
2470 2445
2471 putcharacter (SCHEME_A_ '"'); 2446 putcharacter (SCHEME_A_ '"');
2472} 2447}
2473 2448
2474/* print atoms */ 2449/* print atoms */
2475static void 2450ecb_cold static void
2476printatom (SCHEME_P_ pointer l, int f) 2451printatom (SCHEME_P_ pointer l, int f)
2477{ 2452{
2478 char *p; 2453 char *p;
2479 int len; 2454 int len;
2480 2455
2481 atom2str (SCHEME_A_ l, f, &p, &len); 2456 atom2str (SCHEME_A_ l, f, &p, &len);
2482 putchars (SCHEME_A_ p, len); 2457 putchars (SCHEME_A_ p, len);
2483} 2458}
2484 2459
2485/* Uses internal buffer unless string pointer is already available */ 2460/* Uses internal buffer unless string pointer is already available */
2486static void 2461ecb_cold static void
2487atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2462atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2488{ 2463{
2489 char *p; 2464 char *p;
2490 2465
2491 if (l == NIL) 2466 if (l == NIL)
2698 return car (d); 2673 return car (d);
2699 2674
2700 p = cons (car (d), cdr (d)); 2675 p = cons (car (d), cdr (d));
2701 q = p; 2676 q = p;
2702 2677
2703 while (cdr (cdr (p)) != NIL) 2678 while (cddr (p) != NIL)
2704 { 2679 {
2705 d = cons (car (p), cdr (p)); 2680 d = cons (car (p), cdr (p));
2706 2681
2707 if (cdr (cdr (p)) != NIL) 2682 if (cddr (p) != NIL)
2708 p = cdr (d); 2683 p = cdr (d);
2709 } 2684 }
2710 2685
2711 set_cdr (p, car (cdr (p))); 2686 set_cdr (p, cadr (p));
2712 return q; 2687 return q;
2713} 2688}
2714 2689
2715/* reverse list -- produce new list */ 2690/* reverse list -- produce new list */
2716static pointer 2691ecb_hot static pointer
2717reverse (SCHEME_P_ pointer a) 2692reverse (SCHEME_P_ pointer a)
2718{ 2693{
2719 /* a must be checked by gc */ 2694 /* a must be checked by gc */
2720 pointer p = NIL; 2695 pointer p = NIL;
2721 2696
2724 2699
2725 return p; 2700 return p;
2726} 2701}
2727 2702
2728/* reverse list --- in-place */ 2703/* reverse list --- in-place */
2729static pointer 2704ecb_hot static pointer
2730reverse_in_place (SCHEME_P_ pointer term, pointer list) 2705reverse_in_place (SCHEME_P_ pointer term, pointer list)
2731{ 2706{
2732 pointer result = term; 2707 pointer result = term;
2733 pointer p = list; 2708 pointer p = list;
2734 2709
2742 2717
2743 return result; 2718 return result;
2744} 2719}
2745 2720
2746/* append list -- produce new list (in reverse order) */ 2721/* append list -- produce new list (in reverse order) */
2747static pointer 2722ecb_hot static pointer
2748revappend (SCHEME_P_ pointer a, pointer b) 2723revappend (SCHEME_P_ pointer a, pointer b)
2749{ 2724{
2750 pointer result = a; 2725 pointer result = a;
2751 pointer p = b; 2726 pointer p = b;
2752 2727
2761 2736
2762 return S_F; /* signal an error */ 2737 return S_F; /* signal an error */
2763} 2738}
2764 2739
2765/* equivalence of atoms */ 2740/* equivalence of atoms */
2766int 2741ecb_hot int
2767eqv (pointer a, pointer b) 2742eqv (pointer a, pointer b)
2768{ 2743{
2769 if (is_string (a)) 2744 if (is_string (a))
2770 { 2745 {
2771 if (is_string (b)) 2746 if (is_string (b))
2865 } 2840 }
2866 else 2841 else
2867 set_car (env, immutable_cons (slot, car (env))); 2842 set_car (env, immutable_cons (slot, car (env)));
2868} 2843}
2869 2844
2870static pointer 2845ecb_hot static pointer
2871find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2846find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2872{ 2847{
2873 pointer x, y; 2848 pointer x, y;
2874 2849
2875 for (x = env; x != NIL; x = cdr (x)) 2850 for (x = env; x != NIL; x = cdr (x))
2896 return NIL; 2871 return NIL;
2897} 2872}
2898 2873
2899#else /* USE_ALIST_ENV */ 2874#else /* USE_ALIST_ENV */
2900 2875
2901ecb_inline void 2876static void
2902new_frame_in_env (SCHEME_P_ pointer old_env) 2877new_frame_in_env (SCHEME_P_ pointer old_env)
2903{ 2878{
2904 SCHEME_V->envir = immutable_cons (NIL, old_env); 2879 SCHEME_V->envir = immutable_cons (NIL, old_env);
2905 setenvironment (SCHEME_V->envir); 2880 setenvironment (SCHEME_V->envir);
2906} 2881}
2907 2882
2908ecb_inline void 2883static void
2909new_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)
2910{ 2885{
2911 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2886 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2912} 2887}
2913 2888
2914static pointer 2889ecb_hot static pointer
2915find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2890find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2916{ 2891{
2917 pointer x, y; 2892 pointer x, y;
2918 2893
2919 for (x = env; x != NIL; x = cdr (x)) 2894 for (x = env; x != NIL; x = cdr (x))
2933 return NIL; 2908 return NIL;
2934} 2909}
2935 2910
2936#endif /* USE_ALIST_ENV else */ 2911#endif /* USE_ALIST_ENV else */
2937 2912
2938ecb_inline void 2913static void
2939new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2914new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2940{ 2915{
2941 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 2916 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2942 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);
2943} 2918}
2944 2919
2945ecb_inline void 2920static void
2946set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2921set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2947{ 2922{
2948 set_cdr (slot, value); 2923 set_cdr (slot, value);
2949} 2924}
2950 2925
2951ecb_inline pointer 2926static pointer
2952slot_value_in_env (pointer slot) 2927slot_value_in_env (pointer slot)
2953{ 2928{
2954 return cdr (slot); 2929 return cdr (slot);
2955} 2930}
2956 2931
2957/* ========== Evaluation Cycle ========== */ 2932/* ========== Evaluation Cycle ========== */
2958 2933
2959static int 2934ecb_cold static int
2960xError_1 (SCHEME_P_ const char *s, pointer a) 2935xError_1 (SCHEME_P_ const char *s, pointer a)
2961{ 2936{
2962#if USE_ERROR_HOOK 2937#if USE_ERROR_HOOK
2963 pointer x; 2938 pointer x;
2964 pointer hdl = SCHEME_V->ERROR_HOOK; 2939 pointer hdl = SCHEME_V->ERROR_HOOK;
3040 pointer code; 3015 pointer code;
3041}; 3016};
3042 3017
3043# define STACK_GROWTH 3 3018# define STACK_GROWTH 3
3044 3019
3045static void 3020ecb_hot static void
3046s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3021s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3047{ 3022{
3048 int nframes = (uintptr_t)SCHEME_V->dump; 3023 int nframes = (uintptr_t)SCHEME_V->dump;
3049 struct dump_stack_frame *next_frame; 3024 struct dump_stack_frame *next_frame;
3050 3025
3063 next_frame->code = code; 3038 next_frame->code = code;
3064 3039
3065 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3040 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3066} 3041}
3067 3042
3068static int 3043static ecb_hot int
3069xs_return (SCHEME_P_ pointer a) 3044xs_return (SCHEME_P_ pointer a)
3070{ 3045{
3071 int nframes = (uintptr_t)SCHEME_V->dump; 3046 int nframes = (uintptr_t)SCHEME_V->dump;
3072 struct dump_stack_frame *frame; 3047 struct dump_stack_frame *frame;
3073 3048
3084 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3059 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3085 3060
3086 return 0; 3061 return 0;
3087} 3062}
3088 3063
3089ecb_inline void 3064ecb_cold void
3090dump_stack_reset (SCHEME_P) 3065dump_stack_reset (SCHEME_P)
3091{ 3066{
3092 /* 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 */
3093 SCHEME_V->dump = (pointer)+0; 3068 SCHEME_V->dump = (pointer)+0;
3094} 3069}
3095 3070
3096ecb_inline void 3071ecb_cold void
3097dump_stack_initialize (SCHEME_P) 3072dump_stack_initialize (SCHEME_P)
3098{ 3073{
3099 SCHEME_V->dump_size = 0; 3074 SCHEME_V->dump_size = 0;
3100 SCHEME_V->dump_base = 0; 3075 SCHEME_V->dump_base = 0;
3101 dump_stack_reset (SCHEME_A); 3076 dump_stack_reset (SCHEME_A);
3102} 3077}
3103 3078
3104static void 3079ecb_cold static void
3105dump_stack_free (SCHEME_P) 3080dump_stack_free (SCHEME_P)
3106{ 3081{
3107 free (SCHEME_V->dump_base); 3082 free (SCHEME_V->dump_base);
3108 SCHEME_V->dump_base = 0; 3083 SCHEME_V->dump_base = 0;
3109 SCHEME_V->dump = (pointer)0; 3084 SCHEME_V->dump = (pointer)0;
3110 SCHEME_V->dump_size = 0; 3085 SCHEME_V->dump_size = 0;
3111} 3086}
3112 3087
3113static void 3088ecb_cold static void
3114dump_stack_mark (SCHEME_P) 3089dump_stack_mark (SCHEME_P)
3115{ 3090{
3116 int nframes = (uintptr_t)SCHEME_V->dump; 3091 int nframes = (uintptr_t)SCHEME_V->dump;
3117 int i; 3092 int i;
3118 3093
3124 mark (frame->envir); 3099 mark (frame->envir);
3125 mark (frame->code); 3100 mark (frame->code);
3126 } 3101 }
3127} 3102}
3128 3103
3129static pointer 3104ecb_cold static pointer
3130ss_get_cont (SCHEME_P) 3105ss_get_cont (SCHEME_P)
3131{ 3106{
3132 int nframes = (uintptr_t)SCHEME_V->dump; 3107 int nframes = (uintptr_t)SCHEME_V->dump;
3133 int i; 3108 int i;
3134 3109
3146 } 3121 }
3147 3122
3148 return cont; 3123 return cont;
3149} 3124}
3150 3125
3151static void 3126ecb_cold static void
3152ss_set_cont (SCHEME_P_ pointer cont) 3127ss_set_cont (SCHEME_P_ pointer cont)
3153{ 3128{
3154 int i = 0; 3129 int i = 0;
3155 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3130 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3156 3131
3168 SCHEME_V->dump = (pointer)(uintptr_t)i; 3143 SCHEME_V->dump = (pointer)(uintptr_t)i;
3169} 3144}
3170 3145
3171#else 3146#else
3172 3147
3173ecb_inline void 3148ecb_cold void
3174dump_stack_reset (SCHEME_P) 3149dump_stack_reset (SCHEME_P)
3175{ 3150{
3176 SCHEME_V->dump = NIL; 3151 SCHEME_V->dump = NIL;
3177} 3152}
3178 3153
3179ecb_inline void 3154ecb_cold void
3180dump_stack_initialize (SCHEME_P) 3155dump_stack_initialize (SCHEME_P)
3181{ 3156{
3182 dump_stack_reset (SCHEME_A); 3157 dump_stack_reset (SCHEME_A);
3183} 3158}
3184 3159
3185static void 3160ecb_cold static void
3186dump_stack_free (SCHEME_P) 3161dump_stack_free (SCHEME_P)
3187{ 3162{
3188 SCHEME_V->dump = NIL; 3163 SCHEME_V->dump = NIL;
3189} 3164}
3190 3165
3191static int 3166ecb_hot static int
3192xs_return (SCHEME_P_ pointer a) 3167xs_return (SCHEME_P_ pointer a)
3193{ 3168{
3194 pointer dump = SCHEME_V->dump; 3169 pointer dump = SCHEME_V->dump;
3195 3170
3196 SCHEME_V->value = a; 3171 SCHEME_V->value = a;
3206 SCHEME_V->dump = dump; 3181 SCHEME_V->dump = dump;
3207 3182
3208 return 0; 3183 return 0;
3209} 3184}
3210 3185
3211static void 3186ecb_hot static void
3212s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3187s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3213{ 3188{
3214 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), 3189 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op),
3215 cons (args, 3190 cons (args,
3216 cons (SCHEME_V->envir, 3191 cons (SCHEME_V->envir,
3217 cons (code, 3192 cons (code,
3218 SCHEME_V->dump)))); 3193 SCHEME_V->dump))));
3219} 3194}
3220 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
3221static void 3274static void
3222dump_stack_mark (SCHEME_P) 3275compile (SCHEME_P_ stream s, pointer x)
3223{ 3276{
3224 mark (SCHEME_V->dump); 3277 if (x == NIL)
3225} 3278 {
3279 stream_put (s, 0);
3280 return;
3281 }
3226 3282
3227static pointer 3283 if (is_syntax (x))
3228ss_get_cont (SCHEME_P) 3284 {
3229{ 3285 stream_put (s, 1);
3230 return SCHEME_V->dump; 3286 stream_put (s, syntaxnum (x));
3231} 3287 return;
3288 }
3232 3289
3233static void 3290 switch (type (x))
3234ss_set_cont (SCHEME_P_ pointer cont) 3291 {
3235{ 3292 case T_INTEGER:
3236 SCHEME_V->dump = cont; 3293 stream_put (s, 2);
3237} 3294 stream_put (s, 0);
3295 stream_put (s, 0);
3296 stream_put (s, 0);
3297 stream_put (s, 0);
3298 return;
3238 3299
3239#endif 3300 case T_SYMBOL:
3301 {
3302 uint32_t sym = symbol_id (SCHEME_A_ x);
3303 printf ("sym %x\n", sym);//D
3240 3304
3241#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3305 stream_put (s, 3);
3242 3306
3243#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
3244static 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
3245debug (SCHEME_P_ int indent, pointer x) 3354dtree (SCHEME_P_ int indent, pointer x)
3246{ 3355{
3247 int c; 3356 int c;
3248 3357
3249 if (is_syntax (x)) 3358 if (is_syntax (x))
3250 { 3359 {
3268 printf ("%*sS<%s>\n", indent, "", symname (x)); 3377 printf ("%*sS<%s>\n", indent, "", symname (x));
3269 return 24+8; 3378 return 24+8;
3270 3379
3271 case T_CLOSURE: 3380 case T_CLOSURE:
3272 printf ("%*sS<%s>\n", indent, "", "closure"); 3381 printf ("%*sS<%s>\n", indent, "", "closure");
3273 debug (SCHEME_A_ indent + 3, cdr(x)); 3382 dtree (SCHEME_A_ indent + 3, cdr(x));
3274 return 32 + debug (SCHEME_A_ indent + 3, car (x)); 3383 return 32 + dtree (SCHEME_A_ indent + 3, car (x));
3275 3384
3276 case T_PAIR: 3385 case T_PAIR:
3277 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); 3386 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3278 c = debug (SCHEME_A_ indent + 3, car (x)); 3387 c = dtree (SCHEME_A_ indent + 3, car (x));
3279 c += debug (SCHEME_A_ indent + 3, cdr (x)); 3388 c += dtree (SCHEME_A_ indent + 3, cdr (x));
3280 return c + 1; 3389 return c + 1;
3281 3390
3282 case T_PORT: 3391 case T_PORT:
3283 printf ("%*sS<%s>\n", indent, "", "port"); 3392 printf ("%*sS<%s>\n", indent, "", "port");
3284 return 24+8; 3393 return 24+8;
3287 printf ("%*sS<%s>\n", indent, "", "vector"); 3396 printf ("%*sS<%s>\n", indent, "", "vector");
3288 return 24+8; 3397 return 24+8;
3289 3398
3290 case T_ENVIRONMENT: 3399 case T_ENVIRONMENT:
3291 printf ("%*sS<%s>\n", indent, "", "environment"); 3400 printf ("%*sS<%s>\n", indent, "", "environment");
3292 return 0 + debug (SCHEME_A_ indent + 3, car (x)); 3401 return 0 + dtree (SCHEME_A_ indent + 3, car (x));
3293 3402
3294 default: 3403 default:
3295 printf ("unhandled type %d\n", type (x)); 3404 printf ("unhandled type %d\n", type (x));
3296 break; 3405 break;
3297 } 3406 }
3298} 3407}
3299#endif 3408#endif
3300 3409
3301static int 3410/* syntax, eval, core, ... */
3411ecb_hot static int
3302opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3412opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3303{ 3413{
3304 pointer args = SCHEME_V->args; 3414 pointer args = SCHEME_V->args;
3305 pointer x, y; 3415 pointer x, y;
3306 3416
3307 switch (op) 3417 switch (op)
3308 { 3418 {
3309#if EXPERIMENT //D 3419#if EXPERIMENT //D
3310 case OP_DEBUG: 3420 case OP_DEBUG:
3311 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);
3312 printf ("\n"); 3424 printf ("\n");
3313 s_return (S_T); 3425 s_return (S_T);
3426 }
3314#endif 3427#endif
3315 case OP_LOAD: /* load */ 3428 case OP_LOAD: /* load */
3316 if (file_interactive (SCHEME_A)) 3429 if (file_interactive (SCHEME_A))
3317 { 3430 {
3318 putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n"); 3431 putstr (SCHEME_A_ "Loading ");
3319 //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');
3320 } 3434 }
3321 3435
3322 if (!file_push (SCHEME_A_ strvalue (car (args)))) 3436 if (!file_push (SCHEME_A_ strvalue (car (args))))
3323 Error_1 ("unable to open", car (args)); 3437 Error_1 ("unable to open", car (args));
3324 else 3438
3325 {
3326 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3439 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3327 s_goto (OP_T0LVL); 3440 s_goto (OP_T0LVL);
3328 }
3329 3441
3330 case OP_T0LVL: /* top level */ 3442 case OP_T0LVL: /* top level */
3331 3443
3332 /* If we reached the end of file, this loop is done. */ 3444 /* If we reached the end of file, this loop is done. */
3333 if (port (SCHEME_V->loadport)->kind & port_saw_EOF) 3445 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3349 /* If interactive, be nice to user. */ 3461 /* If interactive, be nice to user. */
3350 if (file_interactive (SCHEME_A)) 3462 if (file_interactive (SCHEME_A))
3351 { 3463 {
3352 SCHEME_V->envir = SCHEME_V->global_env; 3464 SCHEME_V->envir = SCHEME_V->global_env;
3353 dump_stack_reset (SCHEME_A); 3465 dump_stack_reset (SCHEME_A);
3354 putstr (SCHEME_A_ "\n"); 3466 putcharacter (SCHEME_A_ '\n');
3355 putstr (SCHEME_A_ prompt); 3467 putstr (SCHEME_A_ prompt);
3356 } 3468 }
3357 3469
3358 /* Set up another iteration of REPL */ 3470 /* Set up another iteration of REPL */
3359 SCHEME_V->nesting = 0; 3471 SCHEME_V->nesting = 0;
3394 { 3506 {
3395 SCHEME_V->print_flag = 1; 3507 SCHEME_V->print_flag = 1;
3396 SCHEME_V->args = SCHEME_V->value; 3508 SCHEME_V->args = SCHEME_V->value;
3397 s_goto (OP_P0LIST); 3509 s_goto (OP_P0LIST);
3398 } 3510 }
3399 else 3511
3400 s_return (SCHEME_V->value); 3512 s_return (SCHEME_V->value);
3401 3513
3402 case OP_EVAL: /* main part of evaluation */ 3514 case OP_EVAL: /* main part of evaluation */
3403#if USE_TRACING 3515#if USE_TRACING
3404 if (SCHEME_V->tracing) 3516 if (SCHEME_V->tracing)
3405 { 3517 {
3438 /* 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)); */
3439 SCHEME_V->code = x; 3551 SCHEME_V->code = x;
3440 s_goto (OP_EVAL); 3552 s_goto (OP_EVAL);
3441 } 3553 }
3442 } 3554 }
3443 else 3555
3444 s_return (SCHEME_V->code); 3556 s_return (SCHEME_V->code);
3445 3557
3446 case OP_E0ARGS: /* eval arguments */ 3558 case OP_E0ARGS: /* eval arguments */
3447 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ 3559 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3448 { 3560 {
3449 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3561 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3621 else 3733 else
3622 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);
3623 3735
3624 s_return (SCHEME_V->code); 3736 s_return (SCHEME_V->code);
3625 3737
3626
3627 case OP_DEFP: /* defined? */ 3738 case OP_DEFP: /* defined? */
3628 x = SCHEME_V->envir; 3739 x = SCHEME_V->envir;
3629 3740
3630 if (cdr (args) != NIL) 3741 if (cdr (args) != NIL)
3631 x = cadr (args); 3742 x = cadr (args);
3648 set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); 3759 set_slot_in_env (SCHEME_A_ y, SCHEME_V->value);
3649 s_return (SCHEME_V->value); 3760 s_return (SCHEME_V->value);
3650 } 3761 }
3651 else 3762 else
3652 Error_1 ("set!: unbound variable:", SCHEME_V->code); 3763 Error_1 ("set!: unbound variable:", SCHEME_V->code);
3653
3654 3764
3655 case OP_BEGIN: /* begin */ 3765 case OP_BEGIN: /* begin */
3656 if (!is_pair (SCHEME_V->code)) 3766 if (!is_pair (SCHEME_V->code))
3657 s_return (SCHEME_V->code); 3767 s_return (SCHEME_V->code);
3658 3768
3670 case OP_IF1: /* if */ 3780 case OP_IF1: /* if */
3671 if (is_true (SCHEME_V->value)) 3781 if (is_true (SCHEME_V->value))
3672 SCHEME_V->code = car (SCHEME_V->code); 3782 SCHEME_V->code = car (SCHEME_V->code);
3673 else 3783 else
3674 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
3675 s_goto (OP_EVAL); 3786 s_goto (OP_EVAL);
3676 3787
3677 case OP_LET0: /* let */ 3788 case OP_LET0: /* let */
3678 SCHEME_V->args = NIL; 3789 SCHEME_V->args = NIL;
3679 SCHEME_V->value = SCHEME_V->code; 3790 SCHEME_V->value = SCHEME_V->code;
3835 } 3946 }
3836 else 3947 else
3837 { 3948 {
3838 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) 3949 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL)
3839 s_return (NIL); 3950 s_return (NIL);
3840 else 3951
3841 {
3842 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 3952 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
3843 SCHEME_V->code = caar (SCHEME_V->code); 3953 SCHEME_V->code = caar (SCHEME_V->code);
3844 s_goto (OP_EVAL); 3954 s_goto (OP_EVAL);
3845 }
3846 } 3955 }
3847 3956
3848 case OP_DELAY: /* delay */ 3957 case OP_DELAY: /* delay */
3849 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);
3850 set_typeflag (x, T_PROMISE); 3959 set_typeflag (x, T_PROMISE);
3861 case OP_AND1: /* and */ 3970 case OP_AND1: /* and */
3862 if (is_false (SCHEME_V->value)) 3971 if (is_false (SCHEME_V->value))
3863 s_return (SCHEME_V->value); 3972 s_return (SCHEME_V->value);
3864 else if (SCHEME_V->code == NIL) 3973 else if (SCHEME_V->code == NIL)
3865 s_return (SCHEME_V->value); 3974 s_return (SCHEME_V->value);
3866 else 3975
3867 {
3868 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 3976 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
3869 SCHEME_V->code = car (SCHEME_V->code); 3977 SCHEME_V->code = car (SCHEME_V->code);
3870 s_goto (OP_EVAL); 3978 s_goto (OP_EVAL);
3871 }
3872 3979
3873 case OP_OR0: /* or */ 3980 case OP_OR0: /* or */
3874 if (SCHEME_V->code == NIL) 3981 if (SCHEME_V->code == NIL)
3875 s_return (S_F); 3982 s_return (S_F);
3876 3983
3881 case OP_OR1: /* or */ 3988 case OP_OR1: /* or */
3882 if (is_true (SCHEME_V->value)) 3989 if (is_true (SCHEME_V->value))
3883 s_return (SCHEME_V->value); 3990 s_return (SCHEME_V->value);
3884 else if (SCHEME_V->code == NIL) 3991 else if (SCHEME_V->code == NIL)
3885 s_return (SCHEME_V->value); 3992 s_return (SCHEME_V->value);
3886 else 3993
3887 {
3888 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 3994 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
3889 SCHEME_V->code = car (SCHEME_V->code); 3995 SCHEME_V->code = car (SCHEME_V->code);
3890 s_goto (OP_EVAL); 3996 s_goto (OP_EVAL);
3891 }
3892 3997
3893 case OP_C0STREAM: /* cons-stream */ 3998 case OP_C0STREAM: /* cons-stream */
3894 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 3999 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3895 SCHEME_V->code = car (SCHEME_V->code); 4000 SCHEME_V->code = car (SCHEME_V->code);
3896 s_goto (OP_EVAL); 4001 s_goto (OP_EVAL);
3961 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); 4066 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x));
3962 SCHEME_V->code = caar (x); 4067 SCHEME_V->code = caar (x);
3963 s_goto (OP_EVAL); 4068 s_goto (OP_EVAL);
3964 } 4069 }
3965 } 4070 }
3966 else 4071
3967 s_return (NIL); 4072 s_return (NIL);
3968 4073
3969 case OP_CASE2: /* case */ 4074 case OP_CASE2: /* case */
3970 if (is_true (SCHEME_V->value)) 4075 if (is_true (SCHEME_V->value))
3971 s_goto (OP_BEGIN); 4076 s_goto (OP_BEGIN);
3972 else 4077
3973 s_return (NIL); 4078 s_return (NIL);
3974 4079
3975 case OP_PAPPLY: /* apply */ 4080 case OP_PAPPLY: /* apply */
3976 SCHEME_V->code = car (args); 4081 SCHEME_V->code = car (args);
3977 SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); 4082 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
3978 /*SCHEME_V->args = cadr(args); */ 4083 /*SCHEME_V->args = cadr(args); */
3992 } 4097 }
3993 4098
3994 if (USE_ERROR_CHECKING) abort (); 4099 if (USE_ERROR_CHECKING) abort ();
3995} 4100}
3996 4101
3997static int 4102/* math, cxr */
4103ecb_hot static int
3998opexe_1 (SCHEME_P_ enum scheme_opcodes op) 4104opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3999{ 4105{
4000 pointer args = SCHEME_V->args; 4106 pointer args = SCHEME_V->args;
4001 pointer x = car (args); 4107 pointer x = car (args);
4002 num v; 4108 num v;
4483 } 4589 }
4484 4590
4485 if (USE_ERROR_CHECKING) abort (); 4591 if (USE_ERROR_CHECKING) abort ();
4486} 4592}
4487 4593
4488static int 4594/* relational ops */
4595ecb_hot static int
4489opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4596opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4490{ 4597{
4491 pointer x = SCHEME_V->args; 4598 pointer x = SCHEME_V->args;
4492 4599
4493 for (;;) 4600 for (;;)
4514 } 4621 }
4515 4622
4516 s_return (S_T); 4623 s_return (S_T);
4517} 4624}
4518 4625
4519static int 4626/* predicates */
4627ecb_hot static int
4520opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4628opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4521{ 4629{
4522 pointer args = SCHEME_V->args; 4630 pointer args = SCHEME_V->args;
4523 pointer a = car (args); 4631 pointer a = car (args);
4524 pointer d = cdr (args); 4632 pointer d = cdr (args);
4571 } 4679 }
4572 4680
4573 s_retbool (r); 4681 s_retbool (r);
4574} 4682}
4575 4683
4576static int 4684/* promises, list ops, ports */
4685ecb_hot static int
4577opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4686opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4578{ 4687{
4579 pointer args = SCHEME_V->args; 4688 pointer args = SCHEME_V->args;
4580 pointer a = car (args); 4689 pointer a = car (args);
4581 pointer x, y; 4690 pointer x, y;
4624 else 4733 else
4625 SCHEME_V->print_flag = 0; 4734 SCHEME_V->print_flag = 0;
4626 4735
4627 s_goto (OP_P0LIST); 4736 s_goto (OP_P0LIST);
4628 4737
4738 //TODO: move to scheme
4629 case OP_NEWLINE: /* newline */ 4739 case OP_NEWLINE: /* newline */
4630 if (is_pair (args)) 4740 if (is_pair (args))
4631 { 4741 {
4632 if (a != SCHEME_V->outport) 4742 if (a != SCHEME_V->outport)
4633 { 4743 {
4635 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4745 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4636 SCHEME_V->outport = a; 4746 SCHEME_V->outport = a;
4637 } 4747 }
4638 } 4748 }
4639 4749
4640 putstr (SCHEME_A_ "\n"); 4750 putcharacter (SCHEME_A_ '\n');
4641 s_return (S_T); 4751 s_return (S_T);
4642#endif 4752#endif
4643 4753
4644 case OP_ERR0: /* error */ 4754 case OP_ERR0: /* error */
4645 SCHEME_V->retcode = -1; 4755 SCHEME_V->retcode = -1;
4654 putstr (SCHEME_A_ strvalue (car (args))); 4764 putstr (SCHEME_A_ strvalue (car (args)));
4655 SCHEME_V->args = cdr (args); 4765 SCHEME_V->args = cdr (args);
4656 s_goto (OP_ERR1); 4766 s_goto (OP_ERR1);
4657 4767
4658 case OP_ERR1: /* error */ 4768 case OP_ERR1: /* error */
4659 putstr (SCHEME_A_ " "); 4769 putcharacter (SCHEME_A_ ' ');
4660 4770
4661 if (args != NIL) 4771 if (args != NIL)
4662 { 4772 {
4663 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); 4773 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4664 SCHEME_V->args = a; 4774 SCHEME_V->args = a;
4665 SCHEME_V->print_flag = 1; 4775 SCHEME_V->print_flag = 1;
4666 s_goto (OP_P0LIST); 4776 s_goto (OP_P0LIST);
4667 } 4777 }
4668 else 4778 else
4669 { 4779 {
4670 putstr (SCHEME_A_ "\n"); 4780 putcharacter (SCHEME_A_ '\n');
4671 4781
4672 if (SCHEME_V->interactive_repl) 4782 if (SCHEME_V->interactive_repl)
4673 s_goto (OP_T0LVL); 4783 s_goto (OP_T0LVL);
4674 else 4784 else
4675 return -1; 4785 return -1;
4883 } 4993 }
4884 4994
4885 if (USE_ERROR_CHECKING) abort (); 4995 if (USE_ERROR_CHECKING) abort ();
4886} 4996}
4887 4997
4888static int 4998/* reading */
4999ecb_cold static int
4889opexe_5 (SCHEME_P_ enum scheme_opcodes op) 5000opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4890{ 5001{
4891 pointer args = SCHEME_V->args; 5002 pointer args = SCHEME_V->args;
4892 pointer x; 5003 pointer x;
4893 5004
5162 pointer b = cdr (args); 5273 pointer b = cdr (args);
5163 int ok_abbr = ok_abbrev (b); 5274 int ok_abbr = ok_abbrev (b);
5164 SCHEME_V->args = car (b); 5275 SCHEME_V->args = car (b);
5165 5276
5166 if (a == SCHEME_V->QUOTE && ok_abbr) 5277 if (a == SCHEME_V->QUOTE && ok_abbr)
5167 putstr (SCHEME_A_ "'"); 5278 putcharacter (SCHEME_A_ '\'');
5168 else if (a == SCHEME_V->QQUOTE && ok_abbr) 5279 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5169 putstr (SCHEME_A_ "`"); 5280 putcharacter (SCHEME_A_ '`');
5170 else if (a == SCHEME_V->UNQUOTE && ok_abbr) 5281 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5171 putstr (SCHEME_A_ ","); 5282 putcharacter (SCHEME_A_ ',');
5172 else if (a == SCHEME_V->UNQUOTESP && ok_abbr) 5283 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5173 putstr (SCHEME_A_ ",@"); 5284 putstr (SCHEME_A_ ",@");
5174 else 5285 else
5175 { 5286 {
5176 putstr (SCHEME_A_ "("); 5287 putcharacter (SCHEME_A_ '(');
5177 s_save (SCHEME_A_ OP_P1LIST, b, NIL); 5288 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5178 SCHEME_V->args = a; 5289 SCHEME_V->args = a;
5179 } 5290 }
5180 5291
5181 s_goto (OP_P0LIST); 5292 s_goto (OP_P0LIST);
5183 5294
5184 case OP_P1LIST: 5295 case OP_P1LIST:
5185 if (is_pair (args)) 5296 if (is_pair (args))
5186 { 5297 {
5187 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); 5298 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5188 putstr (SCHEME_A_ " "); 5299 putcharacter (SCHEME_A_ ' ');
5189 SCHEME_V->args = car (args); 5300 SCHEME_V->args = car (args);
5190 s_goto (OP_P0LIST); 5301 s_goto (OP_P0LIST);
5191 } 5302 }
5192 else if (is_vector (args)) 5303 else if (is_vector (args))
5193 { 5304 {
5201 { 5312 {
5202 putstr (SCHEME_A_ " . "); 5313 putstr (SCHEME_A_ " . ");
5203 printatom (SCHEME_A_ args, SCHEME_V->print_flag); 5314 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5204 } 5315 }
5205 5316
5206 putstr (SCHEME_A_ ")"); 5317 putcharacter (SCHEME_A_ ')');
5207 s_return (S_T); 5318 s_return (S_T);
5208 } 5319 }
5209 5320
5210 case OP_PVECFROM: 5321 case OP_PVECFROM:
5211 { 5322 {
5213 pointer vec = car (args); 5324 pointer vec = car (args);
5214 int len = veclength (vec); 5325 int len = veclength (vec);
5215 5326
5216 if (i == len) 5327 if (i == len)
5217 { 5328 {
5218 putstr (SCHEME_A_ ")"); 5329 putcharacter (SCHEME_A_ ')');
5219 s_return (S_T); 5330 s_return (S_T);
5220 } 5331 }
5221 else 5332 else
5222 { 5333 {
5223 pointer elem = vector_get (vec, i); 5334 pointer elem = vector_get (vec, i);
5225 ivalue_unchecked (cdr (args)) = i + 1; 5336 ivalue_unchecked (cdr (args)) = i + 1;
5226 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5337 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5227 SCHEME_V->args = elem; 5338 SCHEME_V->args = elem;
5228 5339
5229 if (i > 0) 5340 if (i > 0)
5230 putstr (SCHEME_A_ " "); 5341 putcharacter (SCHEME_A_ ' ');
5231 5342
5232 s_goto (OP_P0LIST); 5343 s_goto (OP_P0LIST);
5233 } 5344 }
5234 } 5345 }
5235 } 5346 }
5236 5347
5237 if (USE_ERROR_CHECKING) abort (); 5348 if (USE_ERROR_CHECKING) abort ();
5238} 5349}
5239 5350
5240static int 5351/* list ops */
5352ecb_hot static int
5241opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5353opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5242{ 5354{
5243 pointer args = SCHEME_V->args; 5355 pointer args = SCHEME_V->args;
5244 pointer a = car (args); 5356 pointer a = car (args);
5245 pointer x, y; 5357 pointer x, y;
5303 5415
5304/* 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 */
5305typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); 5417typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5306 5418
5307typedef int (*test_predicate)(pointer); 5419typedef int (*test_predicate)(pointer);
5308static int 5420
5421ecb_hot static int
5309tst_any (pointer p) 5422tst_any (pointer p)
5310{ 5423{
5311 return 1; 5424 return 1;
5312} 5425}
5313 5426
5314static int 5427ecb_hot static int
5315tst_inonneg (pointer p) 5428tst_inonneg (pointer p)
5316{ 5429{
5317 return is_integer (p) && ivalue_unchecked (p) >= 0; 5430 return is_integer (p) && ivalue_unchecked (p) >= 0;
5318} 5431}
5319 5432
5320static int 5433ecb_hot static int
5321tst_is_list (SCHEME_P_ pointer p) 5434tst_is_list (SCHEME_P_ pointer p)
5322{ 5435{
5323 return p == NIL || is_pair (p); 5436 return p == NIL || is_pair (p);
5324} 5437}
5325 5438
5368#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" 5481#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5369#include "opdefines.h" 5482#include "opdefines.h"
5370#undef OP_DEF 5483#undef OP_DEF
5371; 5484;
5372 5485
5373static const char * 5486ecb_cold static const char *
5374opname (int idx) 5487opname (int idx)
5375{ 5488{
5376 const char *name = opnames; 5489 const char *name = opnames;
5377 5490
5378 /* 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? */
5380 name += strlen (name) + 1; 5493 name += strlen (name) + 1;
5381 5494
5382 return *name ? name : "ILLEGAL"; 5495 return *name ? name : "ILLEGAL";
5383} 5496}
5384 5497
5385static const char * 5498ecb_cold static const char *
5386procname (pointer x) 5499procname (pointer x)
5387{ 5500{
5388 return opname (procnum (x)); 5501 return opname (procnum (x));
5389} 5502}
5390 5503
5410#undef OP_DEF 5523#undef OP_DEF
5411 {0} 5524 {0}
5412}; 5525};
5413 5526
5414/* kernel of this interpreter */ 5527/* kernel of this interpreter */
5415static void ecb_hot 5528ecb_hot static void
5416Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5529Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5417{ 5530{
5418 SCHEME_V->op = op; 5531 SCHEME_V->op = op;
5419 5532
5420 for (;;) 5533 for (;;)
5511 } 5624 }
5512} 5625}
5513 5626
5514/* ========== Initialization of internal keywords ========== */ 5627/* ========== Initialization of internal keywords ========== */
5515 5628
5516static void 5629ecb_cold static void
5517assign_syntax (SCHEME_P_ const char *name) 5630assign_syntax (SCHEME_P_ const char *name)
5518{ 5631{
5519 pointer x = oblist_add_by_name (SCHEME_A_ name); 5632 pointer x = oblist_add_by_name (SCHEME_A_ name);
5520 set_typeflag (x, typeflag (x) | T_SYNTAX); 5633 set_typeflag (x, typeflag (x) | T_SYNTAX);
5521} 5634}
5522 5635
5523static void 5636ecb_cold static void
5524assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) 5637assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
5525{ 5638{
5526 pointer x = mk_symbol (SCHEME_A_ name); 5639 pointer x = mk_symbol (SCHEME_A_ name);
5527 pointer y = mk_proc (SCHEME_A_ op); 5640 pointer y = mk_proc (SCHEME_A_ op);
5528 new_slot_in_env (SCHEME_A_ x, y); 5641 new_slot_in_env (SCHEME_A_ x, y);
5536 ivalue_unchecked (y) = op; 5649 ivalue_unchecked (y) = op;
5537 return y; 5650 return y;
5538} 5651}
5539 5652
5540/* 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! */
5541static int 5654ecb_hot static int
5542syntaxnum (pointer p) 5655syntaxnum (pointer p)
5543{ 5656{
5544 const char *s = strvalue (p); 5657 const char *s = strvalue (p);
5545 5658
5546 switch (strlength (p)) 5659 switch (strlength (p))
5663#endif 5776#endif
5664 } 5777 }
5665 5778
5666 SCHEME_V->gc_verbose = 0; 5779 SCHEME_V->gc_verbose = 0;
5667 dump_stack_initialize (SCHEME_A); 5780 dump_stack_initialize (SCHEME_A);
5668 SCHEME_V->code = NIL; 5781 SCHEME_V->code = NIL;
5669 SCHEME_V->args = NIL; 5782 SCHEME_V->args = NIL;
5670 SCHEME_V->envir = NIL; 5783 SCHEME_V->envir = NIL;
5784 SCHEME_V->value = NIL;
5671 SCHEME_V->tracing = 0; 5785 SCHEME_V->tracing = 0;
5672 5786
5673 /* init NIL */ 5787 /* init NIL */
5674 set_typeflag (NIL, T_ATOM | T_MARK); 5788 set_typeflag (NIL, T_ATOM | T_MARK);
5675 set_car (NIL, NIL); 5789 set_car (NIL, NIL);
5731 5845
5732 return !SCHEME_V->no_memory; 5846 return !SCHEME_V->no_memory;
5733} 5847}
5734 5848
5735#if USE_PORTS 5849#if USE_PORTS
5736void 5850ecb_cold void
5737scheme_set_input_port_file (SCHEME_P_ int fin) 5851scheme_set_input_port_file (SCHEME_P_ int fin)
5738{ 5852{
5739 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); 5853 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input);
5740} 5854}
5741 5855
5742void 5856ecb_cold void
5743scheme_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)
5744{ 5858{
5745 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);
5746} 5860}
5747 5861
5748void 5862ecb_cold void
5749scheme_set_output_port_file (SCHEME_P_ int fout) 5863scheme_set_output_port_file (SCHEME_P_ int fout)
5750{ 5864{
5751 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); 5865 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output);
5752} 5866}
5753 5867
5754void 5868ecb_cold void
5755scheme_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)
5756{ 5870{
5757 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);
5758} 5872}
5759#endif 5873#endif
5760 5874
5761void 5875ecb_cold void
5762scheme_set_external_data (SCHEME_P_ void *p) 5876scheme_set_external_data (SCHEME_P_ void *p)
5763{ 5877{
5764 SCHEME_V->ext_data = p; 5878 SCHEME_V->ext_data = p;
5765} 5879}
5766 5880
5814 } 5928 }
5815 } 5929 }
5816#endif 5930#endif
5817} 5931}
5818 5932
5819void 5933ecb_cold void
5820scheme_load_file (SCHEME_P_ int fin) 5934scheme_load_file (SCHEME_P_ int fin)
5821{ 5935{
5822 scheme_load_named_file (SCHEME_A_ fin, 0); 5936 scheme_load_named_file (SCHEME_A_ fin, 0);
5823} 5937}
5824 5938
5825void 5939ecb_cold void
5826scheme_load_named_file (SCHEME_P_ int fin, const char *filename) 5940scheme_load_named_file (SCHEME_P_ int fin, const char *filename)
5827{ 5941{
5828 dump_stack_reset (SCHEME_A); 5942 dump_stack_reset (SCHEME_A);
5829 SCHEME_V->envir = SCHEME_V->global_env; 5943 SCHEME_V->envir = SCHEME_V->global_env;
5830 SCHEME_V->file_i = 0; 5944 SCHEME_V->file_i = 0;
5831 SCHEME_V->load_stack[0].unget = -1; 5945 SCHEME_V->load_stack[0].unget = -1;
5832 SCHEME_V->load_stack[0].kind = port_input | port_file; 5946 SCHEME_V->load_stack[0].kind = port_input | port_file;
5833 SCHEME_V->load_stack[0].rep.stdio.file = fin; 5947 SCHEME_V->load_stack[0].rep.stdio.file = fin;
5834#if USE_PORTS
5835 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 5948 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5836#endif
5837 SCHEME_V->retcode = 0; 5949 SCHEME_V->retcode = 0;
5838 5950
5839#if USE_PORTS
5840 if (fin == STDIN_FILENO) 5951 if (fin == STDIN_FILENO)
5841 SCHEME_V->interactive_repl = 1; 5952 SCHEME_V->interactive_repl = 1;
5842#endif
5843 5953
5844#if USE_PORTS 5954#if USE_PORTS
5845#if SHOW_ERROR_LINE 5955#if SHOW_ERROR_LINE
5846 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; 5956 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0;
5847 5957
5851#endif 5961#endif
5852 5962
5853 SCHEME_V->inport = SCHEME_V->loadport; 5963 SCHEME_V->inport = SCHEME_V->loadport;
5854 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 5964 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5855 Eval_Cycle (SCHEME_A_ OP_T0LVL); 5965 Eval_Cycle (SCHEME_A_ OP_T0LVL);
5966
5856 set_typeflag (SCHEME_V->loadport, T_ATOM); 5967 set_typeflag (SCHEME_V->loadport, T_ATOM);
5857 5968
5858 if (SCHEME_V->retcode == 0) 5969 if (SCHEME_V->retcode == 0)
5859 SCHEME_V->retcode = SCHEME_V->nesting != 0; 5970 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5860} 5971}
5861 5972
5862void 5973ecb_cold void
5863scheme_load_string (SCHEME_P_ const char *cmd) 5974scheme_load_string (SCHEME_P_ const char *cmd)
5864{ 5975{
5976#if USE_PORTs
5865 dump_stack_reset (SCHEME_A); 5977 dump_stack_reset (SCHEME_A);
5866 SCHEME_V->envir = SCHEME_V->global_env; 5978 SCHEME_V->envir = SCHEME_V->global_env;
5867 SCHEME_V->file_i = 0; 5979 SCHEME_V->file_i = 0;
5868 SCHEME_V->load_stack[0].kind = port_input | port_string; 5980 SCHEME_V->load_stack[0].kind = port_input | port_string;
5869 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ 5981 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */
5870 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); 5982 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd);
5871 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; 5983 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
5872#if USE_PORTS
5873 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 5984 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5874#endif
5875 SCHEME_V->retcode = 0; 5985 SCHEME_V->retcode = 0;
5876 SCHEME_V->interactive_repl = 0; 5986 SCHEME_V->interactive_repl = 0;
5877 SCHEME_V->inport = SCHEME_V->loadport; 5987 SCHEME_V->inport = SCHEME_V->loadport;
5878 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 5988 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5879 Eval_Cycle (SCHEME_A_ OP_T0LVL); 5989 Eval_Cycle (SCHEME_A_ OP_T0LVL);
5880 set_typeflag (SCHEME_V->loadport, T_ATOM); 5990 set_typeflag (SCHEME_V->loadport, T_ATOM);
5881 5991
5882 if (SCHEME_V->retcode == 0) 5992 if (SCHEME_V->retcode == 0)
5883 SCHEME_V->retcode = SCHEME_V->nesting != 0; 5993 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5994#else
5995 abort ();
5996#endif
5884} 5997}
5885 5998
5886void 5999ecb_cold void
5887scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) 6000scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value)
5888{ 6001{
5889 pointer x; 6002 pointer x;
5890 6003
5891 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 6004 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
5896 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 6009 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
5897} 6010}
5898 6011
5899#if !STANDALONE 6012#if !STANDALONE
5900 6013
5901void 6014ecb_cold void
5902scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 6015scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
5903{ 6016{
5904 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));
5905} 6018}
5906 6019
5907void 6020ecb_cold void
5908scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 6021scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
5909{ 6022{
5910 int i; 6023 int i;
5911 6024
5912 for (i = 0; i < count; i++) 6025 for (i = 0; i < count; i++)
5913 scheme_register_foreign_func (SCHEME_A_ list + i); 6026 scheme_register_foreign_func (SCHEME_A_ list + i);
5914} 6027}
5915 6028
5916pointer 6029ecb_cold pointer
5917scheme_apply0 (SCHEME_P_ const char *procname) 6030scheme_apply0 (SCHEME_P_ const char *procname)
5918{ 6031{
5919 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));
5920} 6033}
5921 6034
5922void 6035ecb_cold void
5923save_from_C_call (SCHEME_P) 6036save_from_C_call (SCHEME_P)
5924{ 6037{
5925 pointer saved_data = cons (car (S_SINK), 6038 pointer saved_data = cons (car (S_SINK),
5926 cons (SCHEME_V->envir, 6039 cons (SCHEME_V->envir,
5927 SCHEME_V->dump)); 6040 SCHEME_V->dump));
5931 /* 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
5932 directly resume pre-C-call operations. */ 6045 directly resume pre-C-call operations. */
5933 dump_stack_reset (SCHEME_A); 6046 dump_stack_reset (SCHEME_A);
5934} 6047}
5935 6048
5936void 6049ecb_cold void
5937restore_from_C_call (SCHEME_P) 6050restore_from_C_call (SCHEME_P)
5938{ 6051{
5939 set_car (S_SINK, caar (SCHEME_V->c_nest)); 6052 set_car (S_SINK, caar (SCHEME_V->c_nest));
5940 SCHEME_V->envir = cadar (SCHEME_V->c_nest); 6053 SCHEME_V->envir = cadar (SCHEME_V->c_nest);
5941 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); 6054 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest));
5942 /* Pop */ 6055 /* Pop */
5943 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); 6056 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest);
5944} 6057}
5945 6058
5946/* "func" and "args" are assumed to be already eval'ed. */ 6059/* "func" and "args" are assumed to be already eval'ed. */
5947pointer 6060ecb_cold pointer
5948scheme_call (SCHEME_P_ pointer func, pointer args) 6061scheme_call (SCHEME_P_ pointer func, pointer args)
5949{ 6062{
5950 int old_repl = SCHEME_V->interactive_repl; 6063 int old_repl = SCHEME_V->interactive_repl;
5951 6064
5952 SCHEME_V->interactive_repl = 0; 6065 SCHEME_V->interactive_repl = 0;
5959 SCHEME_V->interactive_repl = old_repl; 6072 SCHEME_V->interactive_repl = old_repl;
5960 restore_from_C_call (SCHEME_A); 6073 restore_from_C_call (SCHEME_A);
5961 return SCHEME_V->value; 6074 return SCHEME_V->value;
5962} 6075}
5963 6076
5964pointer 6077ecb_cold pointer
5965scheme_eval (SCHEME_P_ pointer obj) 6078scheme_eval (SCHEME_P_ pointer obj)
5966{ 6079{
5967 int old_repl = SCHEME_V->interactive_repl; 6080 int old_repl = SCHEME_V->interactive_repl;
5968 6081
5969 SCHEME_V->interactive_repl = 0; 6082 SCHEME_V->interactive_repl = 0;
5981 6094
5982/* ========== Main ========== */ 6095/* ========== Main ========== */
5983 6096
5984#if STANDALONE 6097#if STANDALONE
5985 6098
5986int 6099ecb_cold int
5987main (int argc, char **argv) 6100main (int argc, char **argv)
5988{ 6101{
5989# if USE_MULTIPLICITY 6102# if USE_MULTIPLICITY
5990 scheme ssc; 6103 scheme ssc;
5991 scheme *const SCHEME_V = &ssc; 6104 scheme *const SCHEME_V = &ssc;
6032 } 6145 }
6033#endif 6146#endif
6034 6147
6035 do 6148 do
6036 { 6149 {
6037#if USE_PORTS
6038 if (strcmp (file_name, "-") == 0) 6150 if (strcmp (file_name, "-") == 0)
6039 fin = STDIN_FILENO; 6151 fin = STDIN_FILENO;
6040 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)
6041 { 6153 {
6042 pointer args = NIL; 6154 pointer args = NIL;
6060 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);
6061 6173
6062 } 6174 }
6063 else 6175 else
6064 fin = open (file_name, O_RDONLY); 6176 fin = open (file_name, O_RDONLY);
6065#endif
6066 6177
6067 if (isfile && fin < 0) 6178 if (isfile && fin < 0)
6068 { 6179 {
6069 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');
6070 } 6183 }
6071 else 6184 else
6072 { 6185 {
6073 if (isfile) 6186 if (isfile)
6074 scheme_load_named_file (SCHEME_A_ fin, file_name); 6187 scheme_load_named_file (SCHEME_A_ fin, file_name);
6075 else 6188 else
6076 scheme_load_string (SCHEME_A_ file_name); 6189 scheme_load_string (SCHEME_A_ file_name);
6077 6190
6078#if USE_PORTS
6079 if (!isfile || fin != STDIN_FILENO) 6191 if (!isfile || fin != STDIN_FILENO)
6080 { 6192 {
6081 if (SCHEME_V->retcode != 0) 6193 if (SCHEME_V->retcode != 0)
6082 { 6194 {
6083 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');
6084 } 6198 }
6085 6199
6086 if (isfile) 6200 if (isfile)
6087 close (fin); 6201 close (fin);
6088 } 6202 }
6089#endif
6090 } 6203 }
6091 6204
6092 file_name = *argv++; 6205 file_name = *argv++;
6093 } 6206 }
6094 while (file_name != 0); 6207 while (file_name != 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines