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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines