… | |
… | |
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 PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
21 | #define _GNU_SOURCE 1 |
22 | #include "malloc.c" |
22 | #define _POSIX_C_SOURCE 200201 |
|
|
23 | #define _XOPEN_SOURCE 600 |
|
|
24 | |
23 | |
25 | |
24 | #define SCHEME_SOURCE |
26 | #define SCHEME_SOURCE |
25 | #include "scheme-private.h" |
27 | #include "scheme-private.h" |
26 | #ifndef WIN32 |
28 | #ifndef WIN32 |
27 | # include <unistd.h> |
29 | # include <unistd.h> |
28 | #endif |
30 | #endif |
29 | #if USE_MATH |
31 | #if USE_MATH |
30 | # include <math.h> |
32 | # include <math.h> |
31 | #endif |
33 | #endif |
32 | |
34 | |
|
|
35 | #define ECB_NO_THREADS 1 |
|
|
36 | #include "ecb.h" |
|
|
37 | |
33 | #include <sys/types.h> |
38 | #include <sys/types.h> |
34 | #include <sys/stat.h> |
39 | #include <sys/stat.h> |
35 | #include <fcntl.h> |
40 | #include <fcntl.h> |
36 | |
41 | |
|
|
42 | #if !USE_ERROR_CHECKING |
|
|
43 | # define NDEBUG |
|
|
44 | #endif |
|
|
45 | |
|
|
46 | #include <assert.h> |
|
|
47 | #include <stdlib.h> |
37 | #include <string.h> |
48 | #include <string.h> |
38 | #include <stdlib.h> |
|
|
39 | |
49 | |
40 | #include <limits.h> |
50 | #include <limits.h> |
41 | #include <inttypes.h> |
51 | #include <inttypes.h> |
42 | #include <float.h> |
52 | #include <float.h> |
43 | //#include <ctype.h> |
53 | |
|
|
54 | #if !USE_SYSTEM_MALLOC |
|
|
55 | # define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
|
|
56 | # include "malloc.c" |
|
|
57 | # define malloc(n) tiny_malloc (n) |
|
|
58 | # define realloc(p,n) tiny_realloc (p, n) |
|
|
59 | # define free(p) tiny_free (p) |
|
|
60 | #endif |
|
|
61 | |
|
|
62 | #if '1' != '0' + 1 \ |
|
|
63 | || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ |
|
|
64 | || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ |
|
|
65 | || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ |
|
|
66 | || 'f' != 'a' + 5 |
|
|
67 | # error "execution character set digits not consecutive" |
|
|
68 | #endif |
44 | |
69 | |
45 | enum { |
70 | enum { |
46 | TOK_EOF, |
71 | TOK_EOF, |
47 | TOK_LPAREN, |
72 | TOK_LPAREN, |
48 | TOK_RPAREN, |
73 | TOK_RPAREN, |
49 | TOK_DOT, |
74 | TOK_DOT, |
50 | TOK_ATOM, |
75 | TOK_ATOM, |
|
|
76 | TOK_DOTATOM, /* atom name starting with '.' */ |
|
|
77 | TOK_STRATOM, /* atom name enclosed in | */ |
51 | TOK_QUOTE, |
78 | TOK_QUOTE, |
52 | TOK_DQUOTE, |
79 | TOK_DQUOTE, |
53 | TOK_BQUOTE, |
80 | TOK_BQUOTE, |
54 | TOK_COMMA, |
81 | TOK_COMMA, |
55 | TOK_ATMARK, |
82 | TOK_ATMARK, |
… | |
… | |
57 | TOK_SHARP_CONST, |
84 | TOK_SHARP_CONST, |
58 | TOK_VEC |
85 | TOK_VEC |
59 | }; |
86 | }; |
60 | |
87 | |
61 | #define BACKQUOTE '`' |
88 | #define BACKQUOTE '`' |
62 | #define DELIMITERS "()\";\f\t\v\n\r " |
89 | #define WHITESPACE " \t\r\n\v\f" |
|
|
90 | #define DELIMITERS "()\";" WHITESPACE |
63 | |
91 | |
64 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
92 | #define NIL POINTER (&SCHEME_V->xNIL) |
65 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
93 | #define S_T POINTER (&SCHEME_V->xT) |
66 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
94 | #define S_F POINTER (&SCHEME_V->xF) |
67 | #define S_SINK (&SCHEME_V->xsink) |
95 | #define S_SINK POINTER (&SCHEME_V->xsink) |
68 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
96 | #define S_EOF POINTER (&SCHEME_V->xEOF_OBJ) |
69 | |
|
|
70 | /* should use libecb */ |
|
|
71 | #if __GNUC__ >= 4 |
|
|
72 | # define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
73 | # define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
|
|
74 | # define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
|
|
75 | #else |
|
|
76 | # define ecb_expect_false(expr) !!(expr) |
|
|
77 | # define ecb_expect_true(expr) !!(expr) |
|
|
78 | #endif |
|
|
79 | |
97 | |
80 | #if !USE_MULTIPLICITY |
98 | #if !USE_MULTIPLICITY |
81 | static scheme sc; |
99 | static scheme sc; |
82 | #endif |
100 | #endif |
83 | |
101 | |
84 | static void |
102 | ecb_cold static void |
85 | xbase (char *s, long n, int base) |
103 | xbase (char *s, long n, int base) |
86 | { |
104 | { |
87 | if (n < 0) |
105 | if (n < 0) |
88 | { |
106 | { |
89 | *s++ = '-'; |
107 | *s++ = '-'; |
… | |
… | |
91 | } |
109 | } |
92 | |
110 | |
93 | char *p = s; |
111 | char *p = s; |
94 | |
112 | |
95 | do { |
113 | do { |
96 | *p++ = '0' + n % base; |
114 | *p++ = "0123456789abcdef"[n % base]; |
97 | n /= base; |
115 | n /= base; |
98 | } while (n); |
116 | } while (n); |
99 | |
117 | |
100 | *p-- = 0; |
118 | *p-- = 0; |
101 | |
119 | |
… | |
… | |
104 | char x = *s; *s = *p; *p = x; |
122 | char x = *s; *s = *p; *p = x; |
105 | --p; ++s; |
123 | --p; ++s; |
106 | } |
124 | } |
107 | } |
125 | } |
108 | |
126 | |
109 | static void |
127 | ecb_cold static void |
110 | xnum (char *s, long n) |
128 | xnum (char *s, long n) |
111 | { |
129 | { |
112 | xbase (s, n, 10); |
130 | xbase (s, n, 10); |
113 | } |
131 | } |
114 | |
132 | |
115 | static void |
133 | ecb_cold static void |
116 | xwrstr (const char *s) |
134 | putnum (SCHEME_P_ long n) |
117 | { |
|
|
118 | write (1, s, strlen (s)); |
|
|
119 | } |
|
|
120 | |
|
|
121 | static void |
|
|
122 | xwrnum (long n) |
|
|
123 | { |
135 | { |
124 | char buf[64]; |
136 | char buf[64]; |
125 | |
137 | |
126 | xnum (buf, n); |
138 | xnum (buf, n); |
127 | xwrstr (buf); |
139 | putstr (SCHEME_A_ buf); |
128 | } |
140 | } |
|
|
141 | |
|
|
142 | #if USE_CHAR_CLASSIFIERS |
|
|
143 | #include <ctype.h> |
|
|
144 | #else |
129 | |
145 | |
130 | static char |
146 | static char |
131 | xtoupper (char c) |
147 | xtoupper (char c) |
132 | { |
148 | { |
133 | if (c >= 'a' && c <= 'z') |
149 | if (c >= 'a' && c <= 'z') |
… | |
… | |
153 | |
169 | |
154 | #define toupper(c) xtoupper (c) |
170 | #define toupper(c) xtoupper (c) |
155 | #define tolower(c) xtolower (c) |
171 | #define tolower(c) xtolower (c) |
156 | #define isdigit(c) xisdigit (c) |
172 | #define isdigit(c) xisdigit (c) |
157 | |
173 | |
158 | #if USE_STRLWR |
174 | #endif |
|
|
175 | |
|
|
176 | #if USE_IGNORECASE |
159 | static const char * |
177 | ecb_cold static const char * |
160 | strlwr (char *s) |
178 | xstrlwr (char *s) |
161 | { |
179 | { |
162 | const char *p = s; |
180 | const char *p = s; |
163 | |
181 | |
164 | while (*s) |
182 | while (*s) |
165 | { |
183 | { |
… | |
… | |
167 | s++; |
185 | s++; |
168 | } |
186 | } |
169 | |
187 | |
170 | return p; |
188 | return p; |
171 | } |
189 | } |
172 | #endif |
|
|
173 | |
190 | |
|
|
191 | #define stricmp(a,b) strcasecmp (a, b) |
|
|
192 | #define strlwr(s) xstrlwr (s) |
|
|
193 | |
|
|
194 | #else |
174 | #define stricmp(a,b) strcmp (a, b) |
195 | # define stricmp(a,b) strcmp (a, b) |
175 | #define strlwr(s) (s) |
196 | # define strlwr(s) (s) |
|
|
197 | #endif |
176 | |
198 | |
177 | #ifndef prompt |
199 | #ifndef prompt |
178 | # define prompt "ts> " |
200 | # define prompt "ms> " |
179 | #endif |
201 | #endif |
180 | |
202 | |
181 | #ifndef InitFile |
203 | #ifndef InitFile |
182 | # define InitFile "init.scm" |
204 | # define InitFile "init.scm" |
183 | #endif |
205 | #endif |
184 | |
206 | |
185 | #ifndef FIRST_CELLSEGS |
|
|
186 | # define FIRST_CELLSEGS 3 |
|
|
187 | #endif |
|
|
188 | |
|
|
189 | enum scheme_types |
207 | enum scheme_types |
190 | { |
208 | { |
|
|
209 | T_INTEGER, |
|
|
210 | T_CHARACTER, |
191 | T_FREE, |
211 | T_REAL, |
192 | T_STRING, |
212 | T_STRING, |
193 | T_NUMBER, |
|
|
194 | T_SYMBOL, |
213 | T_SYMBOL, |
195 | T_PROC, |
214 | T_PROC, |
196 | T_PAIR, |
215 | T_PAIR, /* also used for free cells */ |
197 | T_CLOSURE, |
216 | T_CLOSURE, |
|
|
217 | T_BYTECODE, // temp |
|
|
218 | T_MACRO, |
198 | T_CONTINUATION, |
219 | T_CONTINUATION, |
199 | T_FOREIGN, |
220 | T_FOREIGN, |
200 | T_CHARACTER, |
|
|
201 | T_PORT, |
221 | T_PORT, |
202 | T_VECTOR, |
222 | T_VECTOR, |
203 | T_MACRO, |
|
|
204 | T_PROMISE, |
223 | T_PROMISE, |
205 | T_ENVIRONMENT, |
224 | T_ENVIRONMENT, |
206 | /* one more... */ |
225 | |
207 | T_NUM_SYSTEM_TYPES |
226 | T_NUM_SYSTEM_TYPES |
208 | }; |
227 | }; |
209 | |
228 | |
210 | #define T_MASKTYPE 0x000f |
229 | #define T_MASKTYPE 0x000f |
211 | #define T_SYNTAX 0x0010 |
230 | #define T_SYNTAX 0x0010 |
212 | #define T_IMMUTABLE 0x0020 |
231 | #define T_IMMUTABLE 0x0020 |
213 | #define T_ATOM 0x0040 /* only for gc */ |
232 | #define T_ATOM 0x0040 /* only for gc */ |
214 | #define T_MARK 0x0080 /* only for gc */ |
233 | #define T_MARK 0x0080 /* only for gc */ |
215 | |
234 | |
|
|
235 | /* num, for generic arithmetic */ |
|
|
236 | struct num |
|
|
237 | { |
|
|
238 | IVALUE ivalue; |
|
|
239 | #if USE_REAL |
|
|
240 | RVALUE rvalue; |
|
|
241 | char is_fixnum; |
|
|
242 | #endif |
|
|
243 | }; |
|
|
244 | |
|
|
245 | #if USE_REAL |
|
|
246 | # define num_is_fixnum(n) (n).is_fixnum |
|
|
247 | # define num_set_fixnum(n,f) (n).is_fixnum = (f) |
|
|
248 | # define num_ivalue(n) (n).ivalue |
|
|
249 | # define num_rvalue(n) (n).rvalue |
|
|
250 | # define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i) |
|
|
251 | # define num_set_rvalue(n,r) (n).rvalue = (r) |
|
|
252 | #else |
|
|
253 | # define num_is_fixnum(n) 1 |
|
|
254 | # define num_set_fixnum(n,f) 0 |
|
|
255 | # define num_ivalue(n) (n).ivalue |
|
|
256 | # define num_rvalue(n) (n).ivalue |
|
|
257 | # define num_set_ivalue(n,i) (n).ivalue = (i) |
|
|
258 | # define num_set_rvalue(n,r) (n).ivalue = (r) |
|
|
259 | #endif |
|
|
260 | |
216 | enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; |
261 | enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; |
217 | |
262 | |
218 | static num num_op (enum num_op op, num a, num b); |
263 | static num num_op (enum num_op op, num a, num b); |
219 | static num num_intdiv (num a, num b); |
264 | static num num_intdiv (num a, num b); |
220 | static num num_rem (num a, num b); |
265 | static num num_rem (num a, num b); |
221 | static num num_mod (num a, num b); |
266 | static num num_mod (num a, num b); |
222 | |
267 | |
223 | #if USE_MATH |
|
|
224 | static double round_per_R5RS (double x); |
|
|
225 | #endif |
|
|
226 | static int is_zero_rvalue (RVALUE x); |
268 | static int is_zero_rvalue (RVALUE x); |
227 | |
|
|
228 | static INLINE int |
|
|
229 | num_is_integer (pointer p) |
|
|
230 | { |
|
|
231 | return num_is_fixnum (p->object.number); |
|
|
232 | } |
|
|
233 | |
269 | |
234 | static num num_zero; |
270 | static num num_zero; |
235 | static num num_one; |
271 | static num num_one; |
236 | |
272 | |
|
|
273 | /* convert "pointer" to cell* / cell* to pointer */ |
|
|
274 | #define CELL(p) ((struct cell *)(p) + 0) |
|
|
275 | #define POINTER(c) ((void *)((c) - 0)) |
|
|
276 | |
237 | /* macros for cell operations */ |
277 | /* macros for cell operations */ |
238 | #define typeflag(p) ((p)->flag + 0) |
278 | #define typeflag(p) (CELL(p)->flag + 0) |
239 | #define set_typeflag(p,v) ((p)->flag = (v)) |
279 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
240 | #define type(p) (typeflag (p) & T_MASKTYPE) |
280 | #define type(p) (typeflag (p) & T_MASKTYPE) |
241 | |
281 | |
242 | INTERFACE INLINE int |
282 | INTERFACE int |
243 | is_string (pointer p) |
283 | is_string (pointer p) |
244 | { |
284 | { |
245 | return type (p) == T_STRING; |
285 | return type (p) == T_STRING; |
246 | } |
286 | } |
247 | |
287 | |
248 | #define strvalue(p) ((p)->object.string.svalue) |
288 | #define strvalue(p) (CELL(p)->object.string.svalue) |
249 | #define strlength(p) ((p)->object.string.length) |
289 | #define strlength(p) (CELL(p)->object.string.length) |
250 | |
290 | |
251 | INTERFACE int is_list (SCHEME_P_ pointer p); |
|
|
252 | |
|
|
253 | INTERFACE INLINE int |
291 | INTERFACE int |
254 | is_vector (pointer p) |
292 | is_vector (pointer p) |
255 | { |
293 | { |
256 | return type (p) == T_VECTOR; |
294 | return type (p) == T_VECTOR; |
257 | } |
295 | } |
258 | |
296 | |
259 | #define vecvalue(p) ((p)->object.vector.vvalue) |
297 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
260 | #define veclength(p) ((p)->object.vector.length) |
298 | #define veclength(p) (CELL(p)->object.vector.length) |
261 | INTERFACE void fill_vector (pointer vec, pointer obj); |
299 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
262 | INTERFACE uint32_t vector_length (pointer vec); |
|
|
263 | INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); |
300 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
264 | INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); |
301 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
265 | |
302 | |
266 | INTERFACE uint32_t |
303 | INTERFACE int |
267 | vector_length (pointer vec) |
304 | is_integer (pointer p) |
268 | { |
305 | { |
269 | return vec->object.vector.length; |
306 | return type (p) == T_INTEGER; |
270 | } |
307 | } |
271 | |
308 | |
|
|
309 | /* not the same as in scheme, where integers are (correctly :) reals */ |
272 | INTERFACE INLINE int |
310 | INTERFACE int |
|
|
311 | is_real (pointer p) |
|
|
312 | { |
|
|
313 | return type (p) == T_REAL; |
|
|
314 | } |
|
|
315 | |
|
|
316 | INTERFACE int |
273 | is_number (pointer p) |
317 | is_number (pointer p) |
274 | { |
318 | { |
275 | return type (p) == T_NUMBER; |
319 | return is_integer (p) || is_real (p); |
276 | } |
320 | } |
277 | |
321 | |
278 | INTERFACE INLINE int |
322 | INTERFACE int |
279 | is_integer (pointer p) |
|
|
280 | { |
|
|
281 | if (!is_number (p)) |
|
|
282 | return 0; |
|
|
283 | |
|
|
284 | if (num_is_integer (p) || ivalue (p) == rvalue (p)) |
|
|
285 | return 1; |
|
|
286 | |
|
|
287 | return 0; |
|
|
288 | } |
|
|
289 | |
|
|
290 | INTERFACE INLINE int |
|
|
291 | is_real (pointer p) |
|
|
292 | { |
|
|
293 | return is_number (p) && !num_is_fixnum (p->object.number); |
|
|
294 | } |
|
|
295 | |
|
|
296 | INTERFACE INLINE int |
|
|
297 | is_character (pointer p) |
323 | is_character (pointer p) |
298 | { |
324 | { |
299 | return type (p) == T_CHARACTER; |
325 | return type (p) == T_CHARACTER; |
300 | } |
326 | } |
301 | |
327 | |
302 | INTERFACE INLINE char * |
328 | INTERFACE char * |
303 | string_value (pointer p) |
329 | string_value (pointer p) |
304 | { |
330 | { |
305 | return strvalue (p); |
331 | return strvalue (p); |
306 | } |
332 | } |
307 | |
333 | |
308 | INLINE num |
|
|
309 | nvalue (pointer p) |
|
|
310 | { |
|
|
311 | return (p)->object.number; |
|
|
312 | } |
|
|
313 | |
|
|
314 | static IVALUE |
|
|
315 | num_get_ivalue (const num n) |
|
|
316 | { |
|
|
317 | return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n); |
|
|
318 | } |
|
|
319 | |
|
|
320 | static RVALUE |
|
|
321 | num_get_rvalue (const num n) |
|
|
322 | { |
|
|
323 | return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n); |
|
|
324 | } |
|
|
325 | |
|
|
326 | INTERFACE IVALUE |
|
|
327 | ivalue (pointer p) |
|
|
328 | { |
|
|
329 | return num_get_ivalue (p->object.number); |
|
|
330 | } |
|
|
331 | |
|
|
332 | INTERFACE RVALUE |
|
|
333 | rvalue (pointer p) |
|
|
334 | { |
|
|
335 | return num_get_rvalue (p->object.number); |
|
|
336 | } |
|
|
337 | |
|
|
338 | #define ivalue_unchecked(p) ((p)->object.number.value.ivalue) |
334 | #define ivalue_unchecked(p) CELL(p)->object.ivalue |
|
|
335 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
|
|
336 | |
339 | #if USE_REAL |
337 | #if USE_REAL |
340 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
338 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
341 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
339 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
342 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
|
|
343 | #else |
340 | #else |
344 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
341 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
345 | # define set_num_integer(p) 0 |
342 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
346 | # define set_num_real(p) 0 |
|
|
347 | #endif |
343 | #endif |
|
|
344 | |
348 | INTERFACE long |
345 | INTERFACE long |
349 | charvalue (pointer p) |
346 | charvalue (pointer p) |
350 | { |
347 | { |
351 | return ivalue_unchecked (p); |
348 | return ivalue_unchecked (p); |
352 | } |
349 | } |
353 | |
350 | |
|
|
351 | #define port(p) CELL(p)->object.port |
|
|
352 | #define set_port(p,v) port(p) = (v) |
354 | INTERFACE INLINE int |
353 | INTERFACE int |
355 | is_port (pointer p) |
354 | is_port (pointer p) |
356 | { |
355 | { |
357 | return type (p) == T_PORT; |
356 | return type (p) == T_PORT; |
358 | } |
357 | } |
359 | |
358 | |
360 | INTERFACE INLINE int |
359 | INTERFACE int |
361 | is_inport (pointer p) |
360 | is_inport (pointer p) |
362 | { |
361 | { |
363 | return is_port (p) && p->object.port->kind & port_input; |
362 | return is_port (p) && port (p)->kind & port_input; |
364 | } |
363 | } |
365 | |
364 | |
366 | INTERFACE INLINE int |
365 | INTERFACE int |
367 | is_outport (pointer p) |
366 | is_outport (pointer p) |
368 | { |
367 | { |
369 | return is_port (p) && p->object.port->kind & port_output; |
368 | return is_port (p) && port (p)->kind & port_output; |
370 | } |
369 | } |
371 | |
370 | |
372 | INTERFACE INLINE int |
371 | INTERFACE int |
373 | is_pair (pointer p) |
372 | is_pair (pointer p) |
374 | { |
373 | { |
375 | return type (p) == T_PAIR; |
374 | return type (p) == T_PAIR; |
376 | } |
375 | } |
377 | |
376 | |
378 | #define car(p) ((p)->object.cons.car + 0) |
377 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
379 | #define cdr(p) ((p)->object.cons.cdr + 0) |
378 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
380 | |
379 | |
381 | static pointer caar (pointer p) { return car (car (p)); } |
380 | static pointer caar (pointer p) { return car (car (p)); } |
382 | static pointer cadr (pointer p) { return car (cdr (p)); } |
381 | static pointer cadr (pointer p) { return car (cdr (p)); } |
383 | static pointer cdar (pointer p) { return cdr (car (p)); } |
382 | static pointer cdar (pointer p) { return cdr (car (p)); } |
384 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
383 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
… | |
… | |
388 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
387 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
389 | |
388 | |
390 | INTERFACE void |
389 | INTERFACE void |
391 | set_car (pointer p, pointer q) |
390 | set_car (pointer p, pointer q) |
392 | { |
391 | { |
393 | p->object.cons.car = q; |
392 | CELL(p)->object.cons.car = CELL (q); |
394 | } |
393 | } |
395 | |
394 | |
396 | INTERFACE void |
395 | INTERFACE void |
397 | set_cdr (pointer p, pointer q) |
396 | set_cdr (pointer p, pointer q) |
398 | { |
397 | { |
399 | p->object.cons.cdr = q; |
398 | CELL(p)->object.cons.cdr = CELL (q); |
400 | } |
399 | } |
401 | |
400 | |
402 | INTERFACE pointer |
401 | INTERFACE pointer |
403 | pair_car (pointer p) |
402 | pair_car (pointer p) |
404 | { |
403 | { |
… | |
… | |
409 | pair_cdr (pointer p) |
408 | pair_cdr (pointer p) |
410 | { |
409 | { |
411 | return cdr (p); |
410 | return cdr (p); |
412 | } |
411 | } |
413 | |
412 | |
414 | INTERFACE INLINE int |
413 | INTERFACE int |
415 | is_symbol (pointer p) |
414 | is_symbol (pointer p) |
416 | { |
415 | { |
417 | return type (p) == T_SYMBOL; |
416 | return type (p) == T_SYMBOL; |
418 | } |
417 | } |
419 | |
418 | |
420 | INTERFACE INLINE char * |
419 | INTERFACE char * |
421 | symname (pointer p) |
420 | symname (pointer p) |
422 | { |
421 | { |
423 | return strvalue (car (p)); |
422 | return strvalue (p); |
424 | } |
423 | } |
425 | |
424 | |
426 | #if USE_PLIST |
425 | #if USE_PLIST |
|
|
426 | #error plists are broken because symbols are no longer pairs |
|
|
427 | #define symprop(p) cdr(p) |
427 | SCHEME_EXPORT INLINE int |
428 | SCHEME_EXPORT int |
428 | hasprop (pointer p) |
429 | hasprop (pointer p) |
429 | { |
430 | { |
430 | return typeflag (p) & T_SYMBOL; |
431 | return typeflag (p) & T_SYMBOL; |
431 | } |
432 | } |
432 | |
|
|
433 | # define symprop(p) cdr(p) |
|
|
434 | #endif |
433 | #endif |
435 | |
434 | |
436 | INTERFACE INLINE int |
435 | INTERFACE int |
437 | is_syntax (pointer p) |
436 | is_syntax (pointer p) |
438 | { |
437 | { |
439 | return typeflag (p) & T_SYNTAX; |
438 | return typeflag (p) & T_SYNTAX; |
440 | } |
439 | } |
441 | |
440 | |
442 | INTERFACE INLINE int |
441 | INTERFACE int |
443 | is_proc (pointer p) |
442 | is_proc (pointer p) |
444 | { |
443 | { |
445 | return type (p) == T_PROC; |
444 | return type (p) == T_PROC; |
446 | } |
445 | } |
447 | |
446 | |
448 | INTERFACE INLINE int |
447 | INTERFACE int |
449 | is_foreign (pointer p) |
448 | is_foreign (pointer p) |
450 | { |
449 | { |
451 | return type (p) == T_FOREIGN; |
450 | return type (p) == T_FOREIGN; |
452 | } |
451 | } |
453 | |
452 | |
454 | INTERFACE INLINE char * |
453 | INTERFACE char * |
455 | syntaxname (pointer p) |
454 | syntaxname (pointer p) |
456 | { |
455 | { |
457 | return strvalue (car (p)); |
456 | return strvalue (p); |
458 | } |
457 | } |
459 | |
458 | |
460 | #define procnum(p) ivalue (p) |
459 | #define procnum(p) ivalue_unchecked (p) |
461 | static const char *procname (pointer x); |
460 | static const char *procname (pointer x); |
462 | |
461 | |
463 | INTERFACE INLINE int |
462 | INTERFACE int |
464 | is_closure (pointer p) |
463 | is_closure (pointer p) |
465 | { |
464 | { |
466 | return type (p) == T_CLOSURE; |
465 | return type (p) == T_CLOSURE; |
467 | } |
466 | } |
468 | |
467 | |
469 | INTERFACE INLINE int |
468 | INTERFACE int |
470 | is_macro (pointer p) |
469 | is_macro (pointer p) |
471 | { |
470 | { |
472 | return type (p) == T_MACRO; |
471 | return type (p) == T_MACRO; |
473 | } |
472 | } |
474 | |
473 | |
475 | INTERFACE INLINE pointer |
474 | INTERFACE pointer |
476 | closure_code (pointer p) |
475 | closure_code (pointer p) |
477 | { |
476 | { |
478 | return car (p); |
477 | return car (p); |
479 | } |
478 | } |
480 | |
479 | |
481 | INTERFACE INLINE pointer |
480 | INTERFACE pointer |
482 | closure_env (pointer p) |
481 | closure_env (pointer p) |
483 | { |
482 | { |
484 | return cdr (p); |
483 | return cdr (p); |
485 | } |
484 | } |
486 | |
485 | |
487 | INTERFACE INLINE int |
486 | INTERFACE int |
488 | is_continuation (pointer p) |
487 | is_continuation (pointer p) |
489 | { |
488 | { |
490 | return type (p) == T_CONTINUATION; |
489 | return type (p) == T_CONTINUATION; |
491 | } |
490 | } |
492 | |
491 | |
493 | #define cont_dump(p) cdr (p) |
492 | #define cont_dump(p) cdr (p) |
494 | #define set_cont_dump(p,v) set_cdr ((p), (v)) |
493 | #define set_cont_dump(p,v) set_cdr ((p), (v)) |
495 | |
494 | |
496 | /* To do: promise should be forced ONCE only */ |
495 | /* To do: promise should be forced ONCE only */ |
497 | INTERFACE INLINE int |
496 | INTERFACE int |
498 | is_promise (pointer p) |
497 | is_promise (pointer p) |
499 | { |
498 | { |
500 | return type (p) == T_PROMISE; |
499 | return type (p) == T_PROMISE; |
501 | } |
500 | } |
502 | |
501 | |
503 | INTERFACE INLINE int |
502 | INTERFACE int |
504 | is_environment (pointer p) |
503 | is_environment (pointer p) |
505 | { |
504 | { |
506 | return type (p) == T_ENVIRONMENT; |
505 | return type (p) == T_ENVIRONMENT; |
507 | } |
506 | } |
508 | |
507 | |
… | |
… | |
514 | |
513 | |
515 | #define is_mark(p) (typeflag (p) & T_MARK) |
514 | #define is_mark(p) (typeflag (p) & T_MARK) |
516 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
515 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
517 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
516 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
518 | |
517 | |
519 | INTERFACE INLINE int |
518 | INTERFACE int |
520 | is_immutable (pointer p) |
519 | is_immutable (pointer p) |
521 | { |
520 | { |
522 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
521 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
523 | } |
522 | } |
524 | |
523 | |
525 | INTERFACE INLINE void |
524 | INTERFACE void |
526 | setimmutable (pointer p) |
525 | setimmutable (pointer p) |
527 | { |
526 | { |
528 | #if USE_ERROR_CHECKING |
527 | #if USE_ERROR_CHECKING |
529 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
528 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
530 | #endif |
529 | #endif |
531 | } |
530 | } |
532 | |
531 | |
|
|
532 | /* Result is: |
|
|
533 | proper list: length |
|
|
534 | circular list: -1 |
|
|
535 | not even a pair: -2 |
|
|
536 | dotted list: -2 minus length before dot |
|
|
537 | */ |
|
|
538 | ecb_hot INTERFACE int |
|
|
539 | list_length (SCHEME_P_ pointer a) |
|
|
540 | { |
|
|
541 | int i = 0; |
|
|
542 | pointer slow, fast; |
|
|
543 | |
|
|
544 | slow = fast = a; |
|
|
545 | |
|
|
546 | while (1) |
|
|
547 | { |
|
|
548 | if (fast == NIL) |
|
|
549 | return i; |
|
|
550 | |
|
|
551 | if (!is_pair (fast)) |
|
|
552 | return -2 - i; |
|
|
553 | |
|
|
554 | fast = cdr (fast); |
|
|
555 | ++i; |
|
|
556 | |
|
|
557 | if (fast == NIL) |
|
|
558 | return i; |
|
|
559 | |
|
|
560 | if (!is_pair (fast)) |
|
|
561 | return -2 - i; |
|
|
562 | |
|
|
563 | ++i; |
|
|
564 | fast = cdr (fast); |
|
|
565 | |
|
|
566 | /* Safe because we would have already returned if `fast' |
|
|
567 | encountered a non-pair. */ |
|
|
568 | slow = cdr (slow); |
|
|
569 | |
|
|
570 | if (fast == slow) |
|
|
571 | { |
|
|
572 | /* the fast pointer has looped back around and caught up |
|
|
573 | with the slow pointer, hence the structure is circular, |
|
|
574 | not of finite length, and therefore not a list */ |
|
|
575 | return -1; |
|
|
576 | } |
|
|
577 | } |
|
|
578 | } |
|
|
579 | |
|
|
580 | INTERFACE int |
|
|
581 | is_list (SCHEME_P_ pointer a) |
|
|
582 | { |
|
|
583 | return list_length (SCHEME_A_ a) >= 0; |
|
|
584 | } |
|
|
585 | |
533 | #if USE_CHAR_CLASSIFIERS |
586 | #if USE_CHAR_CLASSIFIERS |
534 | static INLINE int |
587 | |
|
|
588 | ecb_inline int |
535 | Cisalpha (int c) |
589 | Cisalpha (int c) |
536 | { |
590 | { |
537 | return isascii (c) && isalpha (c); |
591 | return isascii (c) && isalpha (c); |
538 | } |
592 | } |
539 | |
593 | |
540 | static INLINE int |
594 | ecb_inline int |
541 | Cisdigit (int c) |
595 | Cisdigit (int c) |
542 | { |
596 | { |
543 | return isascii (c) && isdigit (c); |
597 | return isascii (c) && isdigit (c); |
544 | } |
598 | } |
545 | |
599 | |
546 | static INLINE int |
600 | ecb_inline int |
547 | Cisspace (int c) |
601 | Cisspace (int c) |
548 | { |
602 | { |
549 | return isascii (c) && isspace (c); |
603 | return isascii (c) && isspace (c); |
550 | } |
604 | } |
551 | |
605 | |
552 | static INLINE int |
606 | ecb_inline int |
553 | Cisupper (int c) |
607 | Cisupper (int c) |
554 | { |
608 | { |
555 | return isascii (c) && isupper (c); |
609 | return isascii (c) && isupper (c); |
556 | } |
610 | } |
557 | |
611 | |
558 | static INLINE int |
612 | ecb_inline int |
559 | Cislower (int c) |
613 | Cislower (int c) |
560 | { |
614 | { |
561 | return isascii (c) && islower (c); |
615 | return isascii (c) && islower (c); |
562 | } |
616 | } |
563 | #endif |
617 | #endif |
… | |
… | |
596 | "gs", |
650 | "gs", |
597 | "rs", |
651 | "rs", |
598 | "us" |
652 | "us" |
599 | }; |
653 | }; |
600 | |
654 | |
601 | static int |
655 | ecb_cold static int |
602 | is_ascii_name (const char *name, int *pc) |
656 | is_ascii_name (const char *name, int *pc) |
603 | { |
657 | { |
604 | int i; |
658 | int i; |
605 | |
659 | |
606 | for (i = 0; i < 32; i++) |
660 | for (i = 0; i < 32; i++) |
… | |
… | |
624 | #endif |
678 | #endif |
625 | |
679 | |
626 | static int file_push (SCHEME_P_ const char *fname); |
680 | static int file_push (SCHEME_P_ const char *fname); |
627 | static void file_pop (SCHEME_P); |
681 | static void file_pop (SCHEME_P); |
628 | static int file_interactive (SCHEME_P); |
682 | static int file_interactive (SCHEME_P); |
629 | static INLINE int is_one_of (char *s, int c); |
683 | ecb_inline int is_one_of (const char *s, int c); |
630 | static int alloc_cellseg (SCHEME_P_ int n); |
684 | static int alloc_cellseg (SCHEME_P); |
631 | static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); |
685 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
632 | static void finalize_cell (SCHEME_P_ pointer a); |
686 | static void finalize_cell (SCHEME_P_ pointer a); |
633 | static int count_consecutive_cells (pointer x, int needed); |
|
|
634 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
687 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
635 | static pointer mk_number (SCHEME_P_ const num n); |
688 | static pointer mk_number (SCHEME_P_ const num n); |
636 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
689 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
637 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
690 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
638 | static pointer mk_atom (SCHEME_P_ char *q); |
691 | static pointer mk_atom (SCHEME_P_ char *q); |
639 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
692 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
640 | |
693 | |
|
|
694 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
695 | |
641 | #if USE_PORTS |
696 | #if USE_PORTS |
642 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
643 | static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); |
697 | static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); |
644 | static pointer port_from_file (SCHEME_P_ int, int prop); |
698 | static pointer port_from_file (SCHEME_P_ int, int prop); |
645 | static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
699 | static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
646 | static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); |
700 | static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); |
647 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
701 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
648 | static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
702 | static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
649 | static void port_close (SCHEME_P_ pointer p, int flag); |
703 | static void port_close (SCHEME_P_ pointer p, int flag); |
650 | #endif |
704 | #endif |
|
|
705 | |
651 | static void mark (pointer a); |
706 | static void mark (pointer a); |
652 | static void gc (SCHEME_P_ pointer a, pointer b); |
707 | static void gc (SCHEME_P_ pointer a, pointer b); |
653 | static int basic_inchar (port *pt); |
708 | static int basic_inchar (port *pt); |
654 | static int inchar (SCHEME_P); |
709 | static int inchar (SCHEME_P); |
655 | static void backchar (SCHEME_P_ int c); |
710 | static void backchar (SCHEME_P_ int c); |
656 | static char *readstr_upto (SCHEME_P_ char *delim); |
711 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
657 | static pointer readstrexp (SCHEME_P); |
712 | static pointer readstrexp (SCHEME_P_ char delim); |
658 | static INLINE int skipspace (SCHEME_P); |
713 | static int skipspace (SCHEME_P); |
659 | static int token (SCHEME_P); |
714 | static int token (SCHEME_P); |
660 | static void printslashstring (SCHEME_P_ char *s, int len); |
715 | static void printslashstring (SCHEME_P_ char *s, int len); |
661 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
716 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
662 | static void printatom (SCHEME_P_ pointer l, int f); |
717 | static void printatom (SCHEME_P_ pointer l, int f); |
663 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
718 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
… | |
… | |
679 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
734 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
680 | static void assign_syntax (SCHEME_P_ const char *name); |
735 | static void assign_syntax (SCHEME_P_ const char *name); |
681 | static int syntaxnum (pointer p); |
736 | static int syntaxnum (pointer p); |
682 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
737 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
683 | |
738 | |
|
|
739 | static IVALUE |
|
|
740 | ivalue (pointer x) |
|
|
741 | { |
|
|
742 | return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x); |
|
|
743 | } |
|
|
744 | |
|
|
745 | static RVALUE |
|
|
746 | rvalue (pointer x) |
|
|
747 | { |
|
|
748 | return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x); |
|
|
749 | } |
|
|
750 | |
|
|
751 | INTERFACE num |
|
|
752 | nvalue (pointer x) |
|
|
753 | { |
|
|
754 | num n; |
|
|
755 | |
|
|
756 | num_set_fixnum (n, is_integer (x)); |
|
|
757 | |
|
|
758 | if (num_is_fixnum (n)) |
|
|
759 | num_set_ivalue (n, ivalue_unchecked (x)); |
|
|
760 | else |
|
|
761 | num_set_rvalue (n, rvalue_unchecked (x)); |
|
|
762 | |
|
|
763 | return n; |
|
|
764 | } |
|
|
765 | |
684 | static num |
766 | static num |
685 | num_op (enum num_op op, num a, num b) |
767 | num_op (enum num_op op, num a, num b) |
686 | { |
768 | { |
687 | num ret; |
769 | num ret; |
688 | |
770 | |
689 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
771 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
690 | |
772 | |
691 | if (num_is_fixnum (ret)) |
773 | if (num_is_fixnum (ret)) |
692 | { |
774 | { |
693 | IVALUE av = num_get_ivalue (a); |
|
|
694 | IVALUE bv = num_get_ivalue (b); |
|
|
695 | |
|
|
696 | switch (op) |
775 | switch (op) |
697 | { |
776 | { |
698 | case NUM_ADD: av += bv; break; |
777 | case NUM_ADD: a.ivalue += b.ivalue; break; |
699 | case NUM_SUB: av -= bv; break; |
778 | case NUM_SUB: a.ivalue -= b.ivalue; break; |
700 | case NUM_MUL: av *= bv; break; |
779 | case NUM_MUL: a.ivalue *= b.ivalue; break; |
701 | case NUM_INTDIV: av /= bv; break; |
780 | case NUM_INTDIV: a.ivalue /= b.ivalue; break; |
702 | } |
781 | } |
703 | |
782 | |
704 | num_set_ivalue (ret, av); |
783 | num_set_ivalue (ret, a.ivalue); |
705 | } |
784 | } |
|
|
785 | #if USE_REAL |
706 | else |
786 | else |
707 | { |
787 | { |
708 | RVALUE av = num_get_rvalue (a); |
|
|
709 | RVALUE bv = num_get_rvalue (b); |
|
|
710 | |
|
|
711 | switch (op) |
788 | switch (op) |
712 | { |
789 | { |
713 | case NUM_ADD: av += bv; break; |
790 | case NUM_ADD: a.rvalue += b.rvalue; break; |
714 | case NUM_SUB: av -= bv; break; |
791 | case NUM_SUB: a.rvalue -= b.rvalue; break; |
715 | case NUM_MUL: av *= bv; break; |
792 | case NUM_MUL: a.rvalue *= b.rvalue; break; |
716 | case NUM_INTDIV: av /= bv; break; |
793 | case NUM_INTDIV: a.rvalue /= b.rvalue; break; |
717 | } |
794 | } |
718 | |
795 | |
719 | num_set_rvalue (ret, av); |
796 | num_set_rvalue (ret, a.rvalue); |
720 | } |
797 | } |
|
|
798 | #endif |
721 | |
799 | |
722 | return ret; |
800 | return ret; |
723 | } |
801 | } |
724 | |
802 | |
725 | static num |
803 | static num |
726 | num_div (num a, num b) |
804 | num_div (num a, num b) |
727 | { |
805 | { |
728 | num ret; |
806 | num ret; |
729 | |
807 | |
730 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); |
808 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0); |
731 | |
809 | |
732 | if (num_is_fixnum (ret)) |
810 | if (num_is_fixnum (ret)) |
733 | num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); |
811 | num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b)); |
734 | else |
812 | else |
735 | num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); |
813 | num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); |
736 | |
814 | |
737 | return ret; |
815 | return ret; |
738 | } |
816 | } |
739 | |
817 | |
740 | static num |
818 | static num |
… | |
… | |
742 | { |
820 | { |
743 | num ret; |
821 | num ret; |
744 | long e1, e2, res; |
822 | long e1, e2, res; |
745 | |
823 | |
746 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
824 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
747 | e1 = num_get_ivalue (a); |
825 | e1 = num_ivalue (a); |
748 | e2 = num_get_ivalue (b); |
826 | e2 = num_ivalue (b); |
749 | res = e1 % e2; |
827 | res = e1 % e2; |
750 | |
828 | |
751 | /* remainder should have same sign as second operand */ |
829 | /* remainder should have same sign as second operand */ |
752 | if (res > 0) |
830 | if (res > 0) |
753 | { |
831 | { |
… | |
… | |
769 | { |
847 | { |
770 | num ret; |
848 | num ret; |
771 | long e1, e2, res; |
849 | long e1, e2, res; |
772 | |
850 | |
773 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
851 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
774 | e1 = num_get_ivalue (a); |
852 | e1 = num_ivalue (a); |
775 | e2 = num_get_ivalue (b); |
853 | e2 = num_ivalue (b); |
776 | res = e1 % e2; |
854 | res = e1 % e2; |
777 | |
855 | |
778 | /* modulo should have same sign as second operand */ |
856 | /* modulo should have same sign as second operand */ |
779 | if (res * e2 < 0) |
857 | if (res * e2 < 0) |
780 | res += e2; |
858 | res += e2; |
781 | |
859 | |
782 | num_set_ivalue (ret, res); |
860 | num_set_ivalue (ret, res); |
783 | return ret; |
861 | return ret; |
784 | } |
862 | } |
785 | |
863 | |
786 | /* this completely disrespects NaNs */ |
864 | /* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */ |
787 | static int |
865 | static int |
788 | num_cmp (num a, num b) |
866 | num_cmp (num a, num b) |
789 | { |
867 | { |
790 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
868 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
791 | int ret; |
869 | int ret; |
792 | |
870 | |
793 | if (is_fixnum) |
871 | if (is_fixnum) |
794 | { |
872 | { |
795 | IVALUE av = num_get_ivalue (a); |
873 | IVALUE av = num_ivalue (a); |
796 | IVALUE bv = num_get_ivalue (b); |
874 | IVALUE bv = num_ivalue (b); |
797 | |
875 | |
798 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
876 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
799 | } |
877 | } |
800 | else |
878 | else |
801 | { |
879 | { |
802 | RVALUE av = num_get_rvalue (a); |
880 | RVALUE av = num_rvalue (a); |
803 | RVALUE bv = num_get_rvalue (b); |
881 | RVALUE bv = num_rvalue (b); |
804 | |
882 | |
805 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
883 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
806 | } |
884 | } |
807 | |
885 | |
808 | return ret; |
886 | return ret; |
809 | } |
887 | } |
810 | |
|
|
811 | #if USE_MATH |
|
|
812 | |
|
|
813 | /* Round to nearest. Round to even if midway */ |
|
|
814 | static double |
|
|
815 | round_per_R5RS (double x) |
|
|
816 | { |
|
|
817 | double fl = floor (x); |
|
|
818 | double ce = ceil (x); |
|
|
819 | double dfl = x - fl; |
|
|
820 | double dce = ce - x; |
|
|
821 | |
|
|
822 | if (dfl > dce) |
|
|
823 | return ce; |
|
|
824 | else if (dfl < dce) |
|
|
825 | return fl; |
|
|
826 | else |
|
|
827 | { |
|
|
828 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
|
|
829 | return fl; |
|
|
830 | else |
|
|
831 | return ce; |
|
|
832 | } |
|
|
833 | } |
|
|
834 | #endif |
|
|
835 | |
888 | |
836 | static int |
889 | static int |
837 | is_zero_rvalue (RVALUE x) |
890 | is_zero_rvalue (RVALUE x) |
838 | { |
891 | { |
|
|
892 | return x == 0; |
|
|
893 | #if 0 |
839 | #if USE_REAL |
894 | #if USE_REAL |
840 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
895 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
841 | #else |
896 | #else |
842 | return x == 0; |
897 | return x == 0; |
843 | #endif |
898 | #endif |
|
|
899 | #endif |
844 | } |
900 | } |
845 | |
901 | |
846 | /* allocate new cell segment */ |
902 | /* allocate new cell segment */ |
847 | static int |
903 | ecb_cold static int |
848 | alloc_cellseg (SCHEME_P_ int n) |
904 | alloc_cellseg (SCHEME_P) |
849 | { |
905 | { |
850 | pointer newp; |
906 | struct cell *newp; |
851 | pointer last; |
907 | struct cell *last; |
852 | pointer p; |
908 | struct cell *p; |
853 | char *cp; |
909 | char *cp; |
854 | long i; |
910 | long i; |
855 | int k; |
911 | int k; |
856 | |
912 | |
857 | static int segsize = CELL_SEGSIZE >> 1; |
913 | static int segsize = CELL_SEGSIZE >> 1; |
858 | segsize <<= 1; |
914 | segsize <<= 1; |
859 | |
915 | |
860 | for (k = 0; k < n; k++) |
|
|
861 | { |
|
|
862 | if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1) |
|
|
863 | return k; |
|
|
864 | |
|
|
865 | cp = malloc (segsize * sizeof (struct cell)); |
916 | cp = malloc (segsize * sizeof (struct cell)); |
866 | |
917 | |
867 | if (!cp && USE_ERROR_CHECKING) |
918 | if (!cp && USE_ERROR_CHECKING) |
868 | return k; |
919 | return k; |
869 | |
920 | |
870 | i = ++SCHEME_V->last_cell_seg; |
921 | i = ++SCHEME_V->last_cell_seg; |
871 | SCHEME_V->alloc_seg[i] = cp; |
|
|
872 | |
922 | |
873 | /* insert new segment in address order */ |
923 | newp = (struct cell *)cp; |
874 | newp = (pointer)cp; |
|
|
875 | SCHEME_V->cell_seg[i] = newp; |
924 | SCHEME_V->cell_seg[i] = newp; |
876 | SCHEME_V->cell_segsize[i] = segsize; |
925 | SCHEME_V->cell_segsize[i] = segsize; |
877 | |
|
|
878 | //TODO: insert, not swap |
|
|
879 | while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i]) |
|
|
880 | { |
|
|
881 | p = SCHEME_V->cell_seg[i]; |
|
|
882 | SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1]; |
|
|
883 | SCHEME_V->cell_seg[i - 1] = p; |
|
|
884 | |
|
|
885 | k = SCHEME_V->cell_segsize[i]; |
|
|
886 | SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1]; |
|
|
887 | SCHEME_V->cell_segsize[i - 1] = k; |
|
|
888 | |
|
|
889 | --i; |
|
|
890 | } |
|
|
891 | |
|
|
892 | SCHEME_V->fcells += segsize; |
926 | SCHEME_V->fcells += segsize; |
893 | last = newp + segsize - 1; |
927 | last = newp + segsize - 1; |
894 | |
928 | |
895 | for (p = newp; p <= last; p++) |
929 | for (p = newp; p <= last; p++) |
896 | { |
930 | { |
|
|
931 | pointer cp = POINTER (p); |
897 | set_typeflag (p, T_FREE); |
932 | set_typeflag (cp, T_PAIR); |
898 | set_car (p, NIL); |
933 | set_car (cp, NIL); |
899 | set_cdr (p, p + 1); |
934 | set_cdr (cp, POINTER (p + 1)); |
900 | } |
935 | } |
901 | |
936 | |
902 | /* insert new cells in address order on free list */ |
|
|
903 | if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) |
|
|
904 | { |
|
|
905 | set_cdr (last, SCHEME_V->free_cell); |
937 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
906 | SCHEME_V->free_cell = newp; |
938 | SCHEME_V->free_cell = POINTER (newp); |
907 | } |
|
|
908 | else |
|
|
909 | { |
|
|
910 | p = SCHEME_V->free_cell; |
|
|
911 | |
939 | |
912 | while (cdr (p) != NIL && newp > cdr (p)) |
|
|
913 | p = cdr (p); |
|
|
914 | |
|
|
915 | set_cdr (last, cdr (p)); |
|
|
916 | set_cdr (p, newp); |
|
|
917 | } |
|
|
918 | } |
|
|
919 | |
|
|
920 | return n; |
940 | return 1; |
921 | } |
941 | } |
922 | |
942 | |
923 | /* get new cell. parameter a, b is marked by gc. */ |
943 | /* get new cell. parameter a, b is marked by gc. */ |
924 | static INLINE pointer |
944 | ecb_inline pointer |
925 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
945 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
926 | { |
946 | { |
927 | if (ecb_expect_false (SCHEME_V->free_cell == NIL)) |
947 | if (ecb_expect_false (SCHEME_V->free_cell == NIL)) |
928 | { |
948 | { |
929 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
949 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
930 | return S_SINK; |
950 | return S_SINK; |
931 | |
951 | |
932 | if (SCHEME_V->free_cell == NIL) |
952 | if (SCHEME_V->free_cell == NIL) |
933 | { |
953 | { |
934 | const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; |
954 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 2; |
935 | |
955 | |
936 | gc (SCHEME_A_ a, b); |
956 | gc (SCHEME_A_ a, b); |
937 | |
957 | |
938 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
958 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
939 | { |
959 | { |
940 | /* if only a few recovered, get more to avoid fruitless gc's */ |
960 | /* if only a few recovered, get more to avoid fruitless gc's */ |
941 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
961 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
942 | { |
962 | { |
943 | #if USE_ERROR_CHECKING |
963 | #if USE_ERROR_CHECKING |
944 | SCHEME_V->no_memory = 1; |
964 | SCHEME_V->no_memory = 1; |
945 | return S_SINK; |
965 | return S_SINK; |
946 | #endif |
966 | #endif |
… | |
… | |
958 | } |
978 | } |
959 | } |
979 | } |
960 | |
980 | |
961 | /* To retain recent allocs before interpreter knows about them - |
981 | /* To retain recent allocs before interpreter knows about them - |
962 | Tehom */ |
982 | Tehom */ |
963 | |
983 | ecb_hot static void |
964 | static void |
|
|
965 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
984 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
966 | { |
985 | { |
967 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
986 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
968 | |
987 | |
969 | set_typeflag (holder, T_PAIR); |
988 | set_typeflag (holder, T_PAIR); |
… | |
… | |
971 | set_car (holder, recent); |
990 | set_car (holder, recent); |
972 | set_cdr (holder, car (S_SINK)); |
991 | set_cdr (holder, car (S_SINK)); |
973 | set_car (S_SINK, holder); |
992 | set_car (S_SINK, holder); |
974 | } |
993 | } |
975 | |
994 | |
976 | static pointer |
995 | ecb_hot static pointer |
977 | get_cell (SCHEME_P_ pointer a, pointer b) |
996 | get_cell (SCHEME_P_ pointer a, pointer b) |
978 | { |
997 | { |
979 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
998 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
980 | |
999 | |
981 | /* For right now, include "a" and "b" in "cell" so that gc doesn't |
1000 | /* For right now, include "a" and "b" in "cell" so that gc doesn't |
… | |
… | |
990 | } |
1009 | } |
991 | |
1010 | |
992 | static pointer |
1011 | static pointer |
993 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1012 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
994 | { |
1013 | { |
995 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1014 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
996 | pointer *e = malloc (len * sizeof (pointer)); |
1015 | pointer *e = malloc (len * sizeof (pointer)); |
997 | |
1016 | |
998 | if (!e && USE_ERROR_CHECKING) |
1017 | if (!e && USE_ERROR_CHECKING) |
999 | return S_SINK; |
1018 | return S_SINK; |
1000 | |
1019 | |
1001 | /* Record it as a vector so that gc understands it. */ |
1020 | /* Record it as a vector so that gc understands it. */ |
1002 | set_typeflag (v, T_VECTOR | T_ATOM); |
1021 | set_typeflag (v, T_VECTOR | T_ATOM); |
1003 | |
1022 | |
1004 | v->object.vector.vvalue = e; |
1023 | CELL(v)->object.vector.vvalue = e; |
1005 | v->object.vector.length = len; |
1024 | CELL(v)->object.vector.length = len; |
1006 | fill_vector (v, init); |
1025 | fill_vector (v, 0, init); |
1007 | push_recent_alloc (SCHEME_A_ v, NIL); |
1026 | push_recent_alloc (SCHEME_A_ v, NIL); |
1008 | |
1027 | |
1009 | return v; |
1028 | return v; |
1010 | } |
1029 | } |
1011 | |
1030 | |
1012 | static INLINE void |
1031 | ecb_inline void |
1013 | ok_to_freely_gc (SCHEME_P) |
1032 | ok_to_freely_gc (SCHEME_P) |
1014 | { |
1033 | { |
1015 | set_car (S_SINK, NIL); |
1034 | set_car (S_SINK, NIL); |
1016 | } |
1035 | } |
1017 | |
1036 | |
… | |
… | |
1019 | static void |
1038 | static void |
1020 | check_cell_alloced (pointer p, int expect_alloced) |
1039 | check_cell_alloced (pointer p, int expect_alloced) |
1021 | { |
1040 | { |
1022 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1041 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1023 | if (typeflag (p) & !expect_alloced) |
1042 | if (typeflag (p) & !expect_alloced) |
1024 | xwrstr ("Cell is already allocated!\n"); |
1043 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1025 | |
1044 | |
1026 | if (!(typeflag (p)) & expect_alloced) |
1045 | if (!(typeflag (p)) & expect_alloced) |
1027 | xwrstr ("Cell is not allocated!\n"); |
1046 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1028 | } |
1047 | } |
1029 | |
1048 | |
1030 | static void |
1049 | static void |
1031 | check_range_alloced (pointer p, int n, int expect_alloced) |
1050 | check_range_alloced (pointer p, int n, int expect_alloced) |
1032 | { |
1051 | { |
… | |
… | |
1038 | #endif |
1057 | #endif |
1039 | |
1058 | |
1040 | /* Medium level cell allocation */ |
1059 | /* Medium level cell allocation */ |
1041 | |
1060 | |
1042 | /* get new cons cell */ |
1061 | /* get new cons cell */ |
1043 | pointer |
1062 | ecb_hot static pointer |
1044 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1063 | xcons (SCHEME_P_ pointer a, pointer b) |
1045 | { |
1064 | { |
1046 | pointer x = get_cell (SCHEME_A_ a, b); |
1065 | pointer x = get_cell (SCHEME_A_ a, b); |
1047 | |
1066 | |
1048 | set_typeflag (x, T_PAIR); |
1067 | set_typeflag (x, T_PAIR); |
1049 | |
|
|
1050 | if (immutable) |
|
|
1051 | setimmutable (x); |
|
|
1052 | |
1068 | |
1053 | set_car (x, a); |
1069 | set_car (x, a); |
1054 | set_cdr (x, b); |
1070 | set_cdr (x, b); |
1055 | |
1071 | |
1056 | return x; |
1072 | return x; |
1057 | } |
1073 | } |
1058 | |
1074 | |
|
|
1075 | ecb_hot static pointer |
|
|
1076 | ximmutable_cons (SCHEME_P_ pointer a, pointer b) |
|
|
1077 | { |
|
|
1078 | pointer x = xcons (SCHEME_A_ a, b); |
|
|
1079 | setimmutable (x); |
|
|
1080 | return x; |
|
|
1081 | } |
|
|
1082 | |
|
|
1083 | #define cons(a,b) xcons (SCHEME_A_ a, b) |
|
|
1084 | #define immutable_cons(a,b) ximmutable_cons (SCHEME_A_ a, b) |
|
|
1085 | |
|
|
1086 | ecb_cold static pointer |
|
|
1087 | generate_symbol (SCHEME_P_ const char *name) |
|
|
1088 | { |
|
|
1089 | pointer x = mk_string (SCHEME_A_ name); |
|
|
1090 | setimmutable (x); |
|
|
1091 | set_typeflag (x, T_SYMBOL | T_ATOM); |
|
|
1092 | return x; |
|
|
1093 | } |
|
|
1094 | |
1059 | /* ========== oblist implementation ========== */ |
1095 | /* ========== oblist implementation ========== */ |
1060 | |
1096 | |
1061 | #ifndef USE_OBJECT_LIST |
1097 | #ifndef USE_OBJECT_LIST |
1062 | |
1098 | |
|
|
1099 | static int |
1063 | static int hash_fn (const char *key, int table_size); |
1100 | hash_fn (const char *key, int table_size) |
|
|
1101 | { |
|
|
1102 | const unsigned char *p = (unsigned char *)key; |
|
|
1103 | uint32_t hash = 2166136261U; |
1064 | |
1104 | |
|
|
1105 | while (*p) |
|
|
1106 | hash = (hash ^ *p++) * 16777619; |
|
|
1107 | |
|
|
1108 | return hash % table_size; |
|
|
1109 | } |
|
|
1110 | |
1065 | static pointer |
1111 | ecb_cold static pointer |
1066 | oblist_initial_value (SCHEME_P) |
1112 | oblist_initial_value (SCHEME_P) |
1067 | { |
1113 | { |
1068 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1114 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1069 | } |
1115 | } |
1070 | |
1116 | |
1071 | /* returns the new symbol */ |
1117 | /* returns the new symbol */ |
1072 | static pointer |
1118 | ecb_cold static pointer |
1073 | oblist_add_by_name (SCHEME_P_ const char *name) |
1119 | oblist_add_by_name (SCHEME_P_ const char *name) |
1074 | { |
1120 | { |
1075 | int location; |
1121 | pointer x = generate_symbol (SCHEME_A_ name); |
1076 | |
|
|
1077 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1078 | set_typeflag (x, T_SYMBOL); |
|
|
1079 | setimmutable (car (x)); |
|
|
1080 | |
|
|
1081 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1122 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1082 | set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); |
1123 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1083 | return x; |
1124 | return x; |
1084 | } |
1125 | } |
1085 | |
1126 | |
1086 | static INLINE pointer |
1127 | ecb_cold static pointer |
1087 | oblist_find_by_name (SCHEME_P_ const char *name) |
1128 | oblist_find_by_name (SCHEME_P_ const char *name) |
1088 | { |
1129 | { |
1089 | int location; |
1130 | int location; |
1090 | pointer x; |
1131 | pointer x; |
1091 | char *s; |
1132 | char *s; |
1092 | |
1133 | |
1093 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1134 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1094 | |
1135 | |
1095 | for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1136 | for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1096 | { |
1137 | { |
1097 | s = symname (car (x)); |
1138 | s = symname (car (x)); |
1098 | |
1139 | |
1099 | /* case-insensitive, per R5RS section 2 */ |
1140 | /* case-insensitive, per R5RS section 2 */ |
1100 | if (stricmp (name, s) == 0) |
1141 | if (stricmp (name, s) == 0) |
… | |
… | |
1102 | } |
1143 | } |
1103 | |
1144 | |
1104 | return NIL; |
1145 | return NIL; |
1105 | } |
1146 | } |
1106 | |
1147 | |
1107 | static pointer |
1148 | ecb_cold static pointer |
1108 | oblist_all_symbols (SCHEME_P) |
1149 | oblist_all_symbols (SCHEME_P) |
1109 | { |
1150 | { |
1110 | int i; |
1151 | int i; |
1111 | pointer x; |
1152 | pointer x; |
1112 | pointer ob_list = NIL; |
1153 | pointer ob_list = NIL; |
1113 | |
1154 | |
1114 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1155 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1115 | for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1156 | for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1116 | ob_list = cons (x, ob_list); |
1157 | ob_list = cons (x, ob_list); |
1117 | |
1158 | |
1118 | return ob_list; |
1159 | return ob_list; |
1119 | } |
1160 | } |
1120 | |
1161 | |
1121 | #else |
1162 | #else |
1122 | |
1163 | |
1123 | static pointer |
1164 | ecb_cold static pointer |
1124 | oblist_initial_value (SCHEME_P) |
1165 | oblist_initial_value (SCHEME_P) |
1125 | { |
1166 | { |
1126 | return NIL; |
1167 | return NIL; |
1127 | } |
1168 | } |
1128 | |
1169 | |
1129 | static INLINE pointer |
1170 | ecb_cold static pointer |
1130 | oblist_find_by_name (SCHEME_P_ const char *name) |
1171 | oblist_find_by_name (SCHEME_P_ const char *name) |
1131 | { |
1172 | { |
1132 | pointer x; |
1173 | pointer x; |
1133 | char *s; |
1174 | char *s; |
1134 | |
1175 | |
… | |
… | |
1143 | |
1184 | |
1144 | return NIL; |
1185 | return NIL; |
1145 | } |
1186 | } |
1146 | |
1187 | |
1147 | /* returns the new symbol */ |
1188 | /* returns the new symbol */ |
1148 | static pointer |
1189 | ecb_cold static pointer |
1149 | oblist_add_by_name (SCHEME_P_ const char *name) |
1190 | oblist_add_by_name (SCHEME_P_ const char *name) |
1150 | { |
1191 | { |
1151 | pointer x; |
1192 | pointer x = generate_symbol (SCHEME_A_ name); |
1152 | |
|
|
1153 | x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1154 | set_typeflag (x, T_SYMBOL); |
|
|
1155 | setimmutable (car (x)); |
|
|
1156 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1193 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1157 | return x; |
1194 | return x; |
1158 | } |
1195 | } |
1159 | |
1196 | |
1160 | static pointer |
1197 | ecb_cold static pointer |
1161 | oblist_all_symbols (SCHEME_P) |
1198 | oblist_all_symbols (SCHEME_P) |
1162 | { |
1199 | { |
1163 | return SCHEME_V->oblist; |
1200 | return SCHEME_V->oblist; |
1164 | } |
1201 | } |
1165 | |
1202 | |
1166 | #endif |
1203 | #endif |
1167 | |
1204 | |
1168 | #if USE_PORTS |
|
|
1169 | static pointer |
1205 | ecb_cold static pointer |
1170 | mk_port (SCHEME_P_ port *p) |
1206 | mk_port (SCHEME_P_ port *p) |
1171 | { |
1207 | { |
1172 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1208 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1173 | |
1209 | |
1174 | set_typeflag (x, T_PORT | T_ATOM); |
1210 | set_typeflag (x, T_PORT | T_ATOM); |
1175 | x->object.port = p; |
1211 | set_port (x, p); |
1176 | |
1212 | |
1177 | return x; |
1213 | return x; |
1178 | } |
1214 | } |
1179 | #endif |
|
|
1180 | |
1215 | |
1181 | pointer |
1216 | ecb_cold pointer |
1182 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1217 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1183 | { |
1218 | { |
1184 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1219 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1185 | |
1220 | |
1186 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1221 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1187 | x->object.ff = f; |
1222 | CELL(x)->object.ff = f; |
1188 | |
1223 | |
1189 | return x; |
1224 | return x; |
1190 | } |
1225 | } |
1191 | |
1226 | |
1192 | INTERFACE pointer |
1227 | INTERFACE pointer |
1193 | mk_character (SCHEME_P_ int c) |
1228 | mk_character (SCHEME_P_ int c) |
1194 | { |
1229 | { |
1195 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1230 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1196 | |
1231 | |
1197 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1232 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1198 | ivalue_unchecked (x) = c & 0xff; |
1233 | set_ivalue (x, c & 0xff); |
1199 | set_num_integer (x); |
1234 | |
1200 | return x; |
1235 | return x; |
1201 | } |
1236 | } |
1202 | |
1237 | |
1203 | /* get number atom (integer) */ |
1238 | /* get number atom (integer) */ |
1204 | INTERFACE pointer |
1239 | INTERFACE pointer |
1205 | mk_integer (SCHEME_P_ long num) |
1240 | mk_integer (SCHEME_P_ long n) |
1206 | { |
1241 | { |
|
|
1242 | pointer p = 0; |
|
|
1243 | pointer *pp = &p; |
|
|
1244 | |
|
|
1245 | #if USE_INTCACHE |
|
|
1246 | if (n >= INTCACHE_MIN && n <= INTCACHE_MAX) |
|
|
1247 | pp = &SCHEME_V->intcache[n - INTCACHE_MIN]; |
|
|
1248 | #endif |
|
|
1249 | |
|
|
1250 | if (!*pp) |
|
|
1251 | { |
1207 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1252 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1208 | |
1253 | |
1209 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1254 | set_typeflag (x, T_INTEGER | T_ATOM); |
1210 | ivalue_unchecked (x) = num; |
1255 | setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */ |
1211 | set_num_integer (x); |
1256 | set_ivalue (x, n); |
|
|
1257 | |
|
|
1258 | *pp = x; |
|
|
1259 | } |
|
|
1260 | |
1212 | return x; |
1261 | return *pp; |
1213 | } |
1262 | } |
1214 | |
1263 | |
1215 | INTERFACE pointer |
1264 | INTERFACE pointer |
1216 | mk_real (SCHEME_P_ RVALUE n) |
1265 | mk_real (SCHEME_P_ RVALUE n) |
1217 | { |
1266 | { |
|
|
1267 | #if USE_REAL |
1218 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1268 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1219 | |
1269 | |
1220 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1270 | set_typeflag (x, T_REAL | T_ATOM); |
1221 | rvalue_unchecked (x) = n; |
1271 | set_rvalue (x, n); |
1222 | set_num_real (x); |
1272 | |
1223 | return x; |
1273 | return x; |
|
|
1274 | #else |
|
|
1275 | return mk_integer (SCHEME_A_ n); |
|
|
1276 | #endif |
1224 | } |
1277 | } |
1225 | |
1278 | |
1226 | static pointer |
1279 | static pointer |
1227 | mk_number (SCHEME_P_ const num n) |
1280 | mk_number (SCHEME_P_ const num n) |
1228 | { |
1281 | { |
|
|
1282 | #if USE_REAL |
1229 | if (num_is_fixnum (n)) |
1283 | return num_is_fixnum (n) |
|
|
1284 | ? mk_integer (SCHEME_A_ num_ivalue (n)) |
|
|
1285 | : mk_real (SCHEME_A_ num_rvalue (n)); |
|
|
1286 | #else |
1230 | return mk_integer (SCHEME_A_ num_get_ivalue (n)); |
1287 | return mk_integer (SCHEME_A_ num_ivalue (n)); |
1231 | else |
1288 | #endif |
1232 | return mk_real (SCHEME_A_ num_get_rvalue (n)); |
|
|
1233 | } |
1289 | } |
1234 | |
1290 | |
1235 | /* allocate name to string area */ |
1291 | /* allocate name to string area */ |
1236 | static char * |
1292 | static char * |
1237 | store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) |
1293 | store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) |
… | |
… | |
1243 | SCHEME_V->no_memory = 1; |
1299 | SCHEME_V->no_memory = 1; |
1244 | return SCHEME_V->strbuff; |
1300 | return SCHEME_V->strbuff; |
1245 | } |
1301 | } |
1246 | |
1302 | |
1247 | if (str) |
1303 | if (str) |
1248 | { |
1304 | memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */ |
1249 | int l = strlen (str); |
|
|
1250 | |
|
|
1251 | if (l > len_str) |
|
|
1252 | l = len_str; |
|
|
1253 | |
|
|
1254 | memcpy (q, str, l); |
|
|
1255 | q[l] = 0; |
|
|
1256 | } |
|
|
1257 | else |
1305 | else |
1258 | { |
|
|
1259 | memset (q, fill, len_str); |
1306 | memset (q, fill, len_str); |
|
|
1307 | |
1260 | q[len_str] = 0; |
1308 | q[len_str] = 0; |
1261 | } |
|
|
1262 | |
1309 | |
1263 | return q; |
1310 | return q; |
1264 | } |
1311 | } |
1265 | |
1312 | |
1266 | INTERFACE pointer |
1313 | INTERFACE pointer |
… | |
… | |
1280 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1327 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1281 | |
1328 | |
1282 | set_typeflag (x, T_STRING | T_ATOM); |
1329 | set_typeflag (x, T_STRING | T_ATOM); |
1283 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1330 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1284 | strlength (x) = len; |
1331 | strlength (x) = len; |
|
|
1332 | |
1285 | return x; |
1333 | return x; |
1286 | } |
1334 | } |
1287 | |
1335 | |
1288 | INTERFACE pointer |
1336 | INTERFACE pointer |
1289 | mk_string (SCHEME_P_ const char *str) |
1337 | mk_string (SCHEME_P_ const char *str) |
… | |
… | |
1296 | { |
1344 | { |
1297 | return get_vector_object (SCHEME_A_ len, NIL); |
1345 | return get_vector_object (SCHEME_A_ len, NIL); |
1298 | } |
1346 | } |
1299 | |
1347 | |
1300 | INTERFACE void |
1348 | INTERFACE void |
1301 | fill_vector (pointer vec, pointer obj) |
1349 | fill_vector (pointer vec, uint32_t start, pointer obj) |
1302 | { |
1350 | { |
1303 | int i; |
1351 | int i; |
1304 | |
1352 | |
1305 | for (i = 0; i < vec->object.vector.length; i++) |
1353 | for (i = start; i < veclength (vec); i++) |
1306 | vecvalue (vec)[i] = obj; |
1354 | vecvalue (vec)[i] = obj; |
1307 | } |
1355 | } |
1308 | |
1356 | |
|
|
1357 | INTERFACE void |
|
|
1358 | vector_resize (pointer vec, uint32_t newsize, pointer fill) |
|
|
1359 | { |
|
|
1360 | uint32_t oldsize = veclength (vec); |
|
|
1361 | vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer)); |
|
|
1362 | veclength (vec) = newsize; |
|
|
1363 | fill_vector (vec, oldsize, fill); |
|
|
1364 | } |
|
|
1365 | |
1309 | INTERFACE pointer |
1366 | INTERFACE pointer |
1310 | vector_elem (pointer vec, uint32_t ielem) |
1367 | vector_get (pointer vec, uint32_t ielem) |
1311 | { |
1368 | { |
1312 | return vecvalue(vec)[ielem]; |
1369 | return vecvalue(vec)[ielem]; |
1313 | } |
1370 | } |
1314 | |
1371 | |
1315 | INTERFACE void |
1372 | INTERFACE void |
1316 | set_vector_elem (pointer vec, uint32_t ielem, pointer a) |
1373 | vector_set (pointer vec, uint32_t ielem, pointer a) |
1317 | { |
1374 | { |
1318 | vecvalue(vec)[ielem] = a; |
1375 | vecvalue(vec)[ielem] = a; |
1319 | } |
1376 | } |
1320 | |
1377 | |
1321 | /* get new symbol */ |
1378 | /* get new symbol */ |
… | |
… | |
1329 | x = oblist_add_by_name (SCHEME_A_ name); |
1386 | x = oblist_add_by_name (SCHEME_A_ name); |
1330 | |
1387 | |
1331 | return x; |
1388 | return x; |
1332 | } |
1389 | } |
1333 | |
1390 | |
1334 | INTERFACE pointer |
1391 | ecb_cold INTERFACE pointer |
1335 | gensym (SCHEME_P) |
1392 | gensym (SCHEME_P) |
1336 | { |
1393 | { |
1337 | pointer x; |
1394 | pointer x; |
1338 | |
|
|
1339 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
|
|
1340 | { |
|
|
1341 | char name[40] = "gensym-"; |
1395 | char name[40] = "gensym-"; |
1342 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1396 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1343 | |
1397 | |
1344 | /* first check oblist */ |
1398 | return generate_symbol (SCHEME_A_ name); |
1345 | x = oblist_find_by_name (SCHEME_A_ name); |
1399 | } |
1346 | |
1400 | |
1347 | if (x == NIL) |
1401 | static int |
1348 | { |
1402 | is_gensym (SCHEME_P_ pointer x) |
1349 | x = oblist_add_by_name (SCHEME_A_ name); |
1403 | { |
1350 | return x; |
1404 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1351 | } |
|
|
1352 | } |
|
|
1353 | |
|
|
1354 | return NIL; |
|
|
1355 | } |
1405 | } |
1356 | |
1406 | |
1357 | /* make symbol or number atom from string */ |
1407 | /* make symbol or number atom from string */ |
1358 | static pointer |
1408 | ecb_cold static pointer |
1359 | mk_atom (SCHEME_P_ char *q) |
1409 | mk_atom (SCHEME_P_ char *q) |
1360 | { |
1410 | { |
1361 | char c, *p; |
1411 | char c, *p; |
1362 | int has_dec_point = 0; |
1412 | int has_dec_point = 0; |
1363 | int has_fp_exp = 0; |
1413 | int has_fp_exp = 0; |
… | |
… | |
1413 | } |
1463 | } |
1414 | else if ((c == 'e') || (c == 'E')) |
1464 | else if ((c == 'e') || (c == 'E')) |
1415 | { |
1465 | { |
1416 | if (!has_fp_exp) |
1466 | if (!has_fp_exp) |
1417 | { |
1467 | { |
1418 | has_dec_point = 1; /* decimal point illegal |
1468 | has_dec_point = 1; /* decimal point illegal from now on */ |
1419 | from now on */ |
|
|
1420 | p++; |
1469 | p++; |
1421 | |
1470 | |
1422 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1471 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1423 | continue; |
1472 | continue; |
1424 | } |
1473 | } |
… | |
… | |
1435 | |
1484 | |
1436 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1485 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1437 | } |
1486 | } |
1438 | |
1487 | |
1439 | /* make constant */ |
1488 | /* make constant */ |
1440 | static pointer |
1489 | ecb_cold static pointer |
1441 | mk_sharp_const (SCHEME_P_ char *name) |
1490 | mk_sharp_const (SCHEME_P_ char *name) |
1442 | { |
1491 | { |
1443 | if (!strcmp (name, "t")) |
1492 | if (!strcmp (name, "t")) |
1444 | return S_T; |
1493 | return S_T; |
1445 | else if (!strcmp (name, "f")) |
1494 | else if (!strcmp (name, "f")) |
1446 | return S_F; |
1495 | return S_F; |
1447 | else if (*name == '\\') /* #\w (character) */ |
1496 | else if (*name == '\\') /* #\w (character) */ |
1448 | { |
1497 | { |
1449 | int c; |
1498 | int c; |
1450 | |
1499 | |
|
|
1500 | // TODO: optimise |
1451 | if (stricmp (name + 1, "space") == 0) |
1501 | if (stricmp (name + 1, "space") == 0) |
1452 | c = ' '; |
1502 | c = ' '; |
1453 | else if (stricmp (name + 1, "newline") == 0) |
1503 | else if (stricmp (name + 1, "newline") == 0) |
1454 | c = '\n'; |
1504 | c = '\n'; |
1455 | else if (stricmp (name + 1, "return") == 0) |
1505 | else if (stricmp (name + 1, "return") == 0) |
1456 | c = '\r'; |
1506 | c = '\r'; |
1457 | else if (stricmp (name + 1, "tab") == 0) |
1507 | else if (stricmp (name + 1, "tab") == 0) |
1458 | c = '\t'; |
1508 | c = '\t'; |
|
|
1509 | else if (stricmp (name + 1, "alarm") == 0) |
|
|
1510 | c = 0x07; |
|
|
1511 | else if (stricmp (name + 1, "backspace") == 0) |
|
|
1512 | c = 0x08; |
|
|
1513 | else if (stricmp (name + 1, "escape") == 0) |
|
|
1514 | c = 0x1b; |
|
|
1515 | else if (stricmp (name + 1, "delete") == 0) |
|
|
1516 | c = 0x7f; |
|
|
1517 | else if (stricmp (name + 1, "null") == 0) |
|
|
1518 | c = 0; |
1459 | else if (name[1] == 'x' && name[2] != 0) |
1519 | else if (name[1] == 'x' && name[2] != 0) |
1460 | { |
1520 | { |
1461 | long c1 = strtol (name + 2, 0, 16); |
1521 | long c1 = strtol (name + 2, 0, 16); |
1462 | |
1522 | |
1463 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1523 | if (0 <= c1 && c1 <= UCHAR_MAX) |
… | |
… | |
1488 | return NIL; |
1548 | return NIL; |
1489 | } |
1549 | } |
1490 | } |
1550 | } |
1491 | |
1551 | |
1492 | /* ========== garbage collector ========== */ |
1552 | /* ========== garbage collector ========== */ |
|
|
1553 | |
|
|
1554 | static void |
|
|
1555 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1556 | { |
|
|
1557 | /* TODO, fast bitmap check? */ |
|
|
1558 | if (is_string (a) || is_symbol (a)) |
|
|
1559 | free (strvalue (a)); |
|
|
1560 | else if (is_vector (a)) |
|
|
1561 | free (vecvalue (a)); |
|
|
1562 | #if USE_PORTS |
|
|
1563 | else if (is_port (a)) |
|
|
1564 | { |
|
|
1565 | if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) |
|
|
1566 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1567 | |
|
|
1568 | free (port (a)); |
|
|
1569 | } |
|
|
1570 | #endif |
|
|
1571 | } |
1493 | |
1572 | |
1494 | /*-- |
1573 | /*-- |
1495 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1574 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1496 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1575 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1497 | * for marking. |
1576 | * for marking. |
1498 | * |
1577 | * |
1499 | * The exception is vectors - vectors are currently marked recursively, |
1578 | * The exception is vectors - vectors are currently marked recursively, |
1500 | * which is inherited form tinyscheme and could be fixed by having another |
1579 | * which is inherited form tinyscheme and could be fixed by having another |
1501 | * word of context in the vector |
1580 | * word of context in the vector |
1502 | */ |
1581 | */ |
1503 | static void |
1582 | ecb_hot static void |
1504 | mark (pointer a) |
1583 | mark (pointer a) |
1505 | { |
1584 | { |
1506 | pointer t, q, p; |
1585 | pointer t, q, p; |
1507 | |
1586 | |
1508 | t = 0; |
1587 | t = 0; |
… | |
… | |
1512 | |
1591 | |
1513 | if (ecb_expect_false (is_vector (p))) |
1592 | if (ecb_expect_false (is_vector (p))) |
1514 | { |
1593 | { |
1515 | int i; |
1594 | int i; |
1516 | |
1595 | |
1517 | for (i = 0; i < p->object.vector.length; i++) |
1596 | for (i = 0; i < veclength (p); i++) |
1518 | mark (vecvalue (p)[i]); |
1597 | mark (vecvalue (p)[i]); |
1519 | } |
1598 | } |
1520 | |
1599 | |
1521 | if (is_atom (p)) |
1600 | if (is_atom (p)) |
1522 | goto E6; |
1601 | goto E6; |
… | |
… | |
1565 | p = q; |
1644 | p = q; |
1566 | goto E6; |
1645 | goto E6; |
1567 | } |
1646 | } |
1568 | } |
1647 | } |
1569 | |
1648 | |
|
|
1649 | ecb_hot static void |
|
|
1650 | gc_free (SCHEME_P) |
|
|
1651 | { |
|
|
1652 | int i; |
|
|
1653 | uint32_t total = 0; |
|
|
1654 | |
|
|
1655 | /* Here we scan the cells to build the free-list. */ |
|
|
1656 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1657 | { |
|
|
1658 | struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1659 | struct cell *p; |
|
|
1660 | total += SCHEME_V->cell_segsize [i]; |
|
|
1661 | |
|
|
1662 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
|
|
1663 | { |
|
|
1664 | pointer c = POINTER (p); |
|
|
1665 | |
|
|
1666 | if (is_mark (c)) |
|
|
1667 | clrmark (c); |
|
|
1668 | else |
|
|
1669 | { |
|
|
1670 | /* reclaim cell */ |
|
|
1671 | if (typeflag (c) != T_PAIR) |
|
|
1672 | { |
|
|
1673 | finalize_cell (SCHEME_A_ c); |
|
|
1674 | set_typeflag (c, T_PAIR); |
|
|
1675 | set_car (c, NIL); |
|
|
1676 | } |
|
|
1677 | |
|
|
1678 | ++SCHEME_V->fcells; |
|
|
1679 | set_cdr (c, SCHEME_V->free_cell); |
|
|
1680 | SCHEME_V->free_cell = c; |
|
|
1681 | } |
|
|
1682 | } |
|
|
1683 | } |
|
|
1684 | |
|
|
1685 | if (SCHEME_V->gc_verbose) |
|
|
1686 | { |
|
|
1687 | putstr (SCHEME_A_ "done: "); putnum (SCHEME_A_ SCHEME_V->fcells); putstr (SCHEME_A_ " out of "); putnum (SCHEME_A_ total); putstr (SCHEME_A_ " cells were recovered.\n"); |
|
|
1688 | } |
|
|
1689 | } |
|
|
1690 | |
1570 | /* garbage collection. parameter a, b is marked. */ |
1691 | /* garbage collection. parameter a, b is marked. */ |
1571 | static void |
1692 | ecb_cold static void |
1572 | gc (SCHEME_P_ pointer a, pointer b) |
1693 | gc (SCHEME_P_ pointer a, pointer b) |
1573 | { |
1694 | { |
1574 | pointer p; |
|
|
1575 | int i; |
1695 | int i; |
1576 | |
1696 | |
1577 | if (SCHEME_V->gc_verbose) |
1697 | if (SCHEME_V->gc_verbose) |
1578 | putstr (SCHEME_A_ "gc..."); |
1698 | putstr (SCHEME_A_ "gc..."); |
1579 | |
1699 | |
… | |
… | |
1595 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1715 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1596 | mark (car (S_SINK)); |
1716 | mark (car (S_SINK)); |
1597 | /* Mark any older stuff above nested C calls */ |
1717 | /* Mark any older stuff above nested C calls */ |
1598 | mark (SCHEME_V->c_nest); |
1718 | mark (SCHEME_V->c_nest); |
1599 | |
1719 | |
|
|
1720 | #if USE_INTCACHE |
|
|
1721 | /* mark intcache */ |
|
|
1722 | for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i) |
|
|
1723 | if (SCHEME_V->intcache[i - INTCACHE_MIN]) |
|
|
1724 | mark (SCHEME_V->intcache[i - INTCACHE_MIN]); |
|
|
1725 | #endif |
|
|
1726 | |
1600 | /* mark variables a, b */ |
1727 | /* mark variables a, b */ |
1601 | mark (a); |
1728 | mark (a); |
1602 | mark (b); |
1729 | mark (b); |
1603 | |
1730 | |
1604 | /* garbage collect */ |
1731 | /* garbage collect */ |
1605 | clrmark (NIL); |
1732 | clrmark (NIL); |
1606 | SCHEME_V->fcells = 0; |
1733 | SCHEME_V->fcells = 0; |
1607 | SCHEME_V->free_cell = NIL; |
1734 | SCHEME_V->free_cell = NIL; |
1608 | |
1735 | |
1609 | /* free-list is kept sorted by address so as to maintain consecutive |
|
|
1610 | ranges, if possible, for use with vectors. Here we scan the cells |
|
|
1611 | (which are also kept sorted by address) downwards to build the |
|
|
1612 | free-list in sorted order. |
|
|
1613 | */ |
|
|
1614 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1615 | { |
|
|
1616 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1617 | |
|
|
1618 | while (--p >= SCHEME_V->cell_seg[i]) |
|
|
1619 | { |
|
|
1620 | if (is_mark (p)) |
|
|
1621 | clrmark (p); |
|
|
1622 | else |
|
|
1623 | { |
|
|
1624 | /* reclaim cell */ |
|
|
1625 | if (typeflag (p) != T_FREE) |
|
|
1626 | { |
|
|
1627 | finalize_cell (SCHEME_A_ p); |
|
|
1628 | set_typeflag (p, T_FREE); |
|
|
1629 | set_car (p, NIL); |
|
|
1630 | } |
|
|
1631 | |
|
|
1632 | ++SCHEME_V->fcells; |
|
|
1633 | set_cdr (p, SCHEME_V->free_cell); |
|
|
1634 | SCHEME_V->free_cell = p; |
|
|
1635 | } |
|
|
1636 | } |
|
|
1637 | } |
|
|
1638 | |
|
|
1639 | if (SCHEME_V->gc_verbose) |
1736 | if (SCHEME_V->gc_verbose) |
1640 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
1737 | putstr (SCHEME_A_ "freeing..."); |
1641 | } |
|
|
1642 | |
1738 | |
1643 | static void |
1739 | gc_free (SCHEME_A); |
1644 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1645 | { |
|
|
1646 | /* TODO, fast bitmap check? */ |
|
|
1647 | if (is_string (a)) |
|
|
1648 | free (strvalue (a)); |
|
|
1649 | else if (is_vector (a)) |
|
|
1650 | free (vecvalue (a)); |
|
|
1651 | #if USE_PORTS |
|
|
1652 | else if (is_port (a)) |
|
|
1653 | { |
|
|
1654 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
|
|
1655 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1656 | |
|
|
1657 | free (a->object.port); |
|
|
1658 | } |
|
|
1659 | #endif |
|
|
1660 | } |
1740 | } |
1661 | |
1741 | |
1662 | /* ========== Routines for Reading ========== */ |
1742 | /* ========== Routines for Reading ========== */ |
1663 | |
1743 | |
1664 | static int |
1744 | ecb_cold static int |
1665 | file_push (SCHEME_P_ const char *fname) |
1745 | file_push (SCHEME_P_ const char *fname) |
1666 | { |
1746 | { |
1667 | #if USE_PORTS |
|
|
1668 | int fin; |
1747 | int fin; |
1669 | |
1748 | |
1670 | if (SCHEME_V->file_i == MAXFIL - 1) |
1749 | if (SCHEME_V->file_i == MAXFIL - 1) |
1671 | return 0; |
1750 | return 0; |
1672 | |
1751 | |
… | |
… | |
1678 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1757 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1679 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1758 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1680 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1759 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1681 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1760 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1682 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1761 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1683 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1762 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1684 | |
1763 | |
1685 | #if SHOW_ERROR_LINE |
1764 | #if SHOW_ERROR_LINE |
1686 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1765 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1687 | |
1766 | |
1688 | if (fname) |
1767 | if (fname) |
1689 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0); |
1768 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0); |
1690 | #endif |
1769 | #endif |
1691 | } |
1770 | } |
1692 | |
1771 | |
1693 | return fin >= 0; |
1772 | return fin >= 0; |
1694 | |
|
|
1695 | #else |
|
|
1696 | return 1; |
|
|
1697 | #endif |
|
|
1698 | } |
1773 | } |
1699 | |
1774 | |
1700 | static void |
1775 | ecb_cold static void |
1701 | file_pop (SCHEME_P) |
1776 | file_pop (SCHEME_P) |
1702 | { |
1777 | { |
1703 | if (SCHEME_V->file_i != 0) |
1778 | if (SCHEME_V->file_i != 0) |
1704 | { |
1779 | { |
1705 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1780 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1706 | #if USE_PORTS |
1781 | #if USE_PORTS |
1707 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1782 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1708 | #endif |
1783 | #endif |
1709 | SCHEME_V->file_i--; |
1784 | SCHEME_V->file_i--; |
1710 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1785 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1711 | } |
1786 | } |
1712 | } |
1787 | } |
1713 | |
1788 | |
1714 | static int |
1789 | ecb_cold static int |
1715 | file_interactive (SCHEME_P) |
1790 | file_interactive (SCHEME_P) |
1716 | { |
1791 | { |
1717 | #if USE_PORTS |
1792 | #if USE_PORTS |
1718 | return SCHEME_V->file_i == 0 |
1793 | return SCHEME_V->file_i == 0 |
1719 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1794 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1720 | && (SCHEME_V->inport->object.port->kind & port_file); |
1795 | && (port (SCHEME_V->inport)->kind & port_file); |
1721 | #else |
1796 | #else |
1722 | return 0; |
1797 | return 0; |
1723 | #endif |
1798 | #endif |
1724 | } |
1799 | } |
1725 | |
1800 | |
1726 | #if USE_PORTS |
1801 | #if USE_PORTS |
1727 | static port * |
1802 | ecb_cold static port * |
1728 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1803 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1729 | { |
1804 | { |
1730 | int fd; |
1805 | int fd; |
1731 | int flags; |
1806 | int flags; |
1732 | char *rw; |
1807 | char *rw; |
… | |
… | |
1755 | # endif |
1830 | # endif |
1756 | |
1831 | |
1757 | return pt; |
1832 | return pt; |
1758 | } |
1833 | } |
1759 | |
1834 | |
1760 | static pointer |
1835 | ecb_cold static pointer |
1761 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1836 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1762 | { |
1837 | { |
1763 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1838 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1764 | |
1839 | |
1765 | if (!pt && USE_ERROR_CHECKING) |
1840 | if (!pt && USE_ERROR_CHECKING) |
1766 | return NIL; |
1841 | return NIL; |
1767 | |
1842 | |
1768 | return mk_port (SCHEME_A_ pt); |
1843 | return mk_port (SCHEME_A_ pt); |
1769 | } |
1844 | } |
1770 | |
1845 | |
1771 | static port * |
1846 | ecb_cold static port * |
1772 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1847 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1773 | { |
1848 | { |
1774 | port *pt = malloc (sizeof *pt); |
1849 | port *pt = malloc (sizeof *pt); |
1775 | |
1850 | |
1776 | if (!pt && USE_ERROR_CHECKING) |
1851 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1781 | pt->rep.stdio.file = f; |
1856 | pt->rep.stdio.file = f; |
1782 | pt->rep.stdio.closeit = 0; |
1857 | pt->rep.stdio.closeit = 0; |
1783 | return pt; |
1858 | return pt; |
1784 | } |
1859 | } |
1785 | |
1860 | |
1786 | static pointer |
1861 | ecb_cold static pointer |
1787 | port_from_file (SCHEME_P_ int f, int prop) |
1862 | port_from_file (SCHEME_P_ int f, int prop) |
1788 | { |
1863 | { |
1789 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1864 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1790 | |
1865 | |
1791 | if (!pt && USE_ERROR_CHECKING) |
1866 | if (!pt && USE_ERROR_CHECKING) |
1792 | return NIL; |
1867 | return NIL; |
1793 | |
1868 | |
1794 | return mk_port (SCHEME_A_ pt); |
1869 | return mk_port (SCHEME_A_ pt); |
1795 | } |
1870 | } |
1796 | |
1871 | |
1797 | static port * |
1872 | ecb_cold static port * |
1798 | port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1873 | port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1799 | { |
1874 | { |
1800 | port *pt = malloc (sizeof (port)); |
1875 | port *pt = malloc (sizeof (port)); |
1801 | |
1876 | |
1802 | if (!pt && USE_ERROR_CHECKING) |
1877 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1808 | pt->rep.string.curr = start; |
1883 | pt->rep.string.curr = start; |
1809 | pt->rep.string.past_the_end = past_the_end; |
1884 | pt->rep.string.past_the_end = past_the_end; |
1810 | return pt; |
1885 | return pt; |
1811 | } |
1886 | } |
1812 | |
1887 | |
1813 | static pointer |
1888 | ecb_cold static pointer |
1814 | port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1889 | port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1815 | { |
1890 | { |
1816 | port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); |
1891 | port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); |
1817 | |
1892 | |
1818 | if (!pt && USE_ERROR_CHECKING) |
1893 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1821 | return mk_port (SCHEME_A_ pt); |
1896 | return mk_port (SCHEME_A_ pt); |
1822 | } |
1897 | } |
1823 | |
1898 | |
1824 | # define BLOCK_SIZE 256 |
1899 | # define BLOCK_SIZE 256 |
1825 | |
1900 | |
1826 | static port * |
1901 | ecb_cold static port * |
1827 | port_rep_from_scratch (SCHEME_P) |
1902 | port_rep_from_scratch (SCHEME_P) |
1828 | { |
1903 | { |
1829 | char *start; |
1904 | char *start; |
1830 | port *pt = malloc (sizeof (port)); |
1905 | port *pt = malloc (sizeof (port)); |
1831 | |
1906 | |
… | |
… | |
1845 | pt->rep.string.curr = start; |
1920 | pt->rep.string.curr = start; |
1846 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1921 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1847 | return pt; |
1922 | return pt; |
1848 | } |
1923 | } |
1849 | |
1924 | |
1850 | static pointer |
1925 | ecb_cold static pointer |
1851 | port_from_scratch (SCHEME_P) |
1926 | port_from_scratch (SCHEME_P) |
1852 | { |
1927 | { |
1853 | port *pt = port_rep_from_scratch (SCHEME_A); |
1928 | port *pt = port_rep_from_scratch (SCHEME_A); |
1854 | |
1929 | |
1855 | if (!pt && USE_ERROR_CHECKING) |
1930 | if (!pt && USE_ERROR_CHECKING) |
1856 | return NIL; |
1931 | return NIL; |
1857 | |
1932 | |
1858 | return mk_port (SCHEME_A_ pt); |
1933 | return mk_port (SCHEME_A_ pt); |
1859 | } |
1934 | } |
1860 | |
1935 | |
1861 | static void |
1936 | ecb_cold static void |
1862 | port_close (SCHEME_P_ pointer p, int flag) |
1937 | port_close (SCHEME_P_ pointer p, int flag) |
1863 | { |
1938 | { |
1864 | port *pt = p->object.port; |
1939 | port *pt = port (p); |
1865 | |
1940 | |
1866 | pt->kind &= ~flag; |
1941 | pt->kind &= ~flag; |
1867 | |
1942 | |
1868 | if ((pt->kind & (port_input | port_output)) == 0) |
1943 | if ((pt->kind & (port_input | port_output)) == 0) |
1869 | { |
1944 | { |
… | |
… | |
1886 | } |
1961 | } |
1887 | } |
1962 | } |
1888 | #endif |
1963 | #endif |
1889 | |
1964 | |
1890 | /* get new character from input file */ |
1965 | /* get new character from input file */ |
1891 | static int |
1966 | ecb_cold static int |
1892 | inchar (SCHEME_P) |
1967 | inchar (SCHEME_P) |
1893 | { |
1968 | { |
1894 | int c; |
1969 | int c; |
1895 | port *pt; |
1970 | port *pt = port (SCHEME_V->inport); |
1896 | |
|
|
1897 | pt = SCHEME_V->inport->object.port; |
|
|
1898 | |
1971 | |
1899 | if (pt->kind & port_saw_EOF) |
1972 | if (pt->kind & port_saw_EOF) |
1900 | return EOF; |
1973 | return EOF; |
1901 | |
1974 | |
1902 | c = basic_inchar (pt); |
1975 | c = basic_inchar (pt); |
… | |
… | |
1912 | } |
1985 | } |
1913 | |
1986 | |
1914 | return c; |
1987 | return c; |
1915 | } |
1988 | } |
1916 | |
1989 | |
1917 | static int ungot = -1; |
1990 | ecb_cold static int |
1918 | |
|
|
1919 | static int |
|
|
1920 | basic_inchar (port *pt) |
1991 | basic_inchar (port *pt) |
1921 | { |
1992 | { |
1922 | #if USE_PORTS |
|
|
1923 | if (pt->unget != -1) |
1993 | if (pt->unget != -1) |
1924 | { |
1994 | { |
1925 | int r = pt->unget; |
1995 | int r = pt->unget; |
1926 | pt->unget = -1; |
1996 | pt->unget = -1; |
1927 | return r; |
1997 | return r; |
1928 | } |
1998 | } |
1929 | |
1999 | |
|
|
2000 | #if USE_PORTS |
1930 | if (pt->kind & port_file) |
2001 | if (pt->kind & port_file) |
1931 | { |
2002 | { |
1932 | char c; |
2003 | char c; |
1933 | |
2004 | |
1934 | if (!read (pt->rep.stdio.file, &c, 1)) |
2005 | if (!read (pt->rep.stdio.file, &c, 1)) |
… | |
… | |
1942 | return EOF; |
2013 | return EOF; |
1943 | else |
2014 | else |
1944 | return *pt->rep.string.curr++; |
2015 | return *pt->rep.string.curr++; |
1945 | } |
2016 | } |
1946 | #else |
2017 | #else |
1947 | if (ungot == -1) |
|
|
1948 | { |
|
|
1949 | char c; |
2018 | char c; |
1950 | if (!read (0, &c, 1)) |
2019 | |
|
|
2020 | if (!read (pt->rep.stdio.file, &c, 1)) |
1951 | return EOF; |
2021 | return EOF; |
1952 | |
2022 | |
1953 | ungot = c; |
|
|
1954 | } |
|
|
1955 | |
|
|
1956 | { |
|
|
1957 | int r = ungot; |
|
|
1958 | ungot = -1; |
|
|
1959 | return r; |
2023 | return c; |
1960 | } |
|
|
1961 | #endif |
2024 | #endif |
1962 | } |
2025 | } |
1963 | |
2026 | |
1964 | /* back character to input buffer */ |
2027 | /* back character to input buffer */ |
1965 | static void |
2028 | ecb_cold static void |
1966 | backchar (SCHEME_P_ int c) |
2029 | backchar (SCHEME_P_ int c) |
1967 | { |
2030 | { |
1968 | #if USE_PORTS |
2031 | port *pt = port (SCHEME_V->inport); |
1969 | port *pt; |
|
|
1970 | |
2032 | |
1971 | if (c == EOF) |
2033 | if (c == EOF) |
1972 | return; |
2034 | return; |
1973 | |
2035 | |
1974 | pt = SCHEME_V->inport->object.port; |
|
|
1975 | pt->unget = c; |
2036 | pt->unget = c; |
1976 | #else |
|
|
1977 | if (c == EOF) |
|
|
1978 | return; |
|
|
1979 | |
|
|
1980 | ungot = c; |
|
|
1981 | #endif |
|
|
1982 | } |
2037 | } |
1983 | |
2038 | |
1984 | #if USE_PORTS |
2039 | #if USE_PORTS |
1985 | static int |
2040 | ecb_cold static int |
1986 | realloc_port_string (SCHEME_P_ port *p) |
2041 | realloc_port_string (SCHEME_P_ port *p) |
1987 | { |
2042 | { |
1988 | char *start = p->rep.string.start; |
2043 | char *start = p->rep.string.start; |
1989 | size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; |
2044 | size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; |
1990 | char *str = malloc (new_size); |
2045 | char *str = malloc (new_size); |
… | |
… | |
2003 | else |
2058 | else |
2004 | return 0; |
2059 | return 0; |
2005 | } |
2060 | } |
2006 | #endif |
2061 | #endif |
2007 | |
2062 | |
2008 | INTERFACE void |
2063 | ecb_cold static void |
2009 | putstr (SCHEME_P_ const char *s) |
2064 | putchars (SCHEME_P_ const char *s, int len) |
2010 | { |
2065 | { |
|
|
2066 | port *pt = port (SCHEME_V->outport); |
|
|
2067 | |
2011 | #if USE_PORTS |
2068 | #if USE_PORTS |
2012 | port *pt = SCHEME_V->outport->object.port; |
|
|
2013 | |
|
|
2014 | if (pt->kind & port_file) |
|
|
2015 | write (pt->rep.stdio.file, s, strlen (s)); |
|
|
2016 | else |
|
|
2017 | for (; *s; s++) |
|
|
2018 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2019 | *pt->rep.string.curr++ = *s; |
|
|
2020 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2021 | *pt->rep.string.curr++ = *s; |
|
|
2022 | |
|
|
2023 | #else |
|
|
2024 | xwrstr (s); |
|
|
2025 | #endif |
|
|
2026 | } |
|
|
2027 | |
|
|
2028 | static void |
|
|
2029 | putchars (SCHEME_P_ const char *s, int len) |
|
|
2030 | { |
|
|
2031 | #if USE_PORTS |
|
|
2032 | port *pt = SCHEME_V->outport->object.port; |
|
|
2033 | |
|
|
2034 | if (pt->kind & port_file) |
2069 | if (pt->kind & port_file) |
2035 | write (pt->rep.stdio.file, s, len); |
2070 | write (pt->rep.stdio.file, s, len); |
2036 | else |
2071 | else |
2037 | { |
2072 | { |
2038 | for (; len; len--) |
2073 | for (; len; len--) |
… | |
… | |
2043 | *pt->rep.string.curr++ = *s++; |
2078 | *pt->rep.string.curr++ = *s++; |
2044 | } |
2079 | } |
2045 | } |
2080 | } |
2046 | |
2081 | |
2047 | #else |
2082 | #else |
2048 | write (1, s, len); |
2083 | write (1, s, len); // output not initialised |
2049 | #endif |
2084 | #endif |
|
|
2085 | } |
|
|
2086 | |
|
|
2087 | INTERFACE void |
|
|
2088 | putstr (SCHEME_P_ const char *s) |
|
|
2089 | { |
|
|
2090 | putchars (SCHEME_A_ s, strlen (s)); |
2050 | } |
2091 | } |
2051 | |
2092 | |
2052 | INTERFACE void |
2093 | INTERFACE void |
2053 | putcharacter (SCHEME_P_ int c) |
2094 | putcharacter (SCHEME_P_ int c) |
2054 | { |
2095 | { |
2055 | #if USE_PORTS |
|
|
2056 | port *pt = SCHEME_V->outport->object.port; |
|
|
2057 | |
|
|
2058 | if (pt->kind & port_file) |
|
|
2059 | { |
|
|
2060 | char cc = c; |
|
|
2061 | write (pt->rep.stdio.file, &cc, 1); |
|
|
2062 | } |
|
|
2063 | else |
|
|
2064 | { |
|
|
2065 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2066 | *pt->rep.string.curr++ = c; |
|
|
2067 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2068 | *pt->rep.string.curr++ = c; |
|
|
2069 | } |
|
|
2070 | |
|
|
2071 | #else |
|
|
2072 | char cc = c; |
2096 | char cc = c; |
2073 | write (1, &c, 1); |
2097 | |
2074 | #endif |
2098 | putchars (SCHEME_A_ &cc, 1); |
2075 | } |
2099 | } |
2076 | |
2100 | |
2077 | /* read characters up to delimiter, but cater to character constants */ |
2101 | /* read characters up to delimiter, but cater to character constants */ |
2078 | static char * |
2102 | ecb_cold static char * |
2079 | readstr_upto (SCHEME_P_ char *delim) |
2103 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2080 | { |
2104 | { |
2081 | char *p = SCHEME_V->strbuff; |
2105 | char *p = SCHEME_V->strbuff + skip; |
2082 | |
2106 | |
2083 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2107 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2084 | |
2108 | |
2085 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2109 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2086 | *p = 0; |
2110 | *p = 0; |
… | |
… | |
2092 | |
2116 | |
2093 | return SCHEME_V->strbuff; |
2117 | return SCHEME_V->strbuff; |
2094 | } |
2118 | } |
2095 | |
2119 | |
2096 | /* read string expression "xxx...xxx" */ |
2120 | /* read string expression "xxx...xxx" */ |
2097 | static pointer |
2121 | ecb_cold static pointer |
2098 | readstrexp (SCHEME_P) |
2122 | readstrexp (SCHEME_P_ char delim) |
2099 | { |
2123 | { |
2100 | char *p = SCHEME_V->strbuff; |
2124 | char *p = SCHEME_V->strbuff; |
2101 | int c; |
2125 | int c; |
2102 | int c1 = 0; |
2126 | int c1 = 0; |
2103 | enum |
|
|
2104 | { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2127 | enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2105 | |
2128 | |
2106 | for (;;) |
2129 | for (;;) |
2107 | { |
2130 | { |
2108 | c = inchar (SCHEME_A); |
2131 | c = inchar (SCHEME_A); |
2109 | |
2132 | |
… | |
… | |
2111 | return S_F; |
2134 | return S_F; |
2112 | |
2135 | |
2113 | switch (state) |
2136 | switch (state) |
2114 | { |
2137 | { |
2115 | case st_ok: |
2138 | case st_ok: |
2116 | switch (c) |
2139 | if (ecb_expect_false (c == delim)) |
2117 | { |
|
|
2118 | case '\\': |
|
|
2119 | state = st_bsl; |
|
|
2120 | break; |
|
|
2121 | |
|
|
2122 | case '"': |
|
|
2123 | *p = 0; |
|
|
2124 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2140 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2125 | |
2141 | |
2126 | default: |
2142 | if (ecb_expect_false (c == '\\')) |
|
|
2143 | state = st_bsl; |
|
|
2144 | else |
2127 | *p++ = c; |
2145 | *p++ = c; |
2128 | break; |
|
|
2129 | } |
|
|
2130 | |
2146 | |
2131 | break; |
2147 | break; |
2132 | |
2148 | |
2133 | case st_bsl: |
2149 | case st_bsl: |
2134 | switch (c) |
2150 | switch (c) |
… | |
… | |
2143 | case '7': |
2159 | case '7': |
2144 | state = st_oct1; |
2160 | state = st_oct1; |
2145 | c1 = c - '0'; |
2161 | c1 = c - '0'; |
2146 | break; |
2162 | break; |
2147 | |
2163 | |
|
|
2164 | case 'a': *p++ = '\a'; state = st_ok; break; |
|
|
2165 | case 'n': *p++ = '\n'; state = st_ok; break; |
|
|
2166 | case 'r': *p++ = '\r'; state = st_ok; break; |
|
|
2167 | case 't': *p++ = '\t'; state = st_ok; break; |
|
|
2168 | |
|
|
2169 | // this overshoots the minimum requirements of r7rs |
|
|
2170 | case ' ': |
|
|
2171 | case '\t': |
|
|
2172 | case '\r': |
|
|
2173 | case '\n': |
|
|
2174 | skipspace (SCHEME_A); |
|
|
2175 | state = st_ok; |
|
|
2176 | break; |
|
|
2177 | |
|
|
2178 | //TODO: x should end in ;, not two-digit hex |
2148 | case 'x': |
2179 | case 'x': |
2149 | case 'X': |
2180 | case 'X': |
2150 | state = st_x1; |
2181 | state = st_x1; |
2151 | c1 = 0; |
2182 | c1 = 0; |
2152 | break; |
2183 | break; |
2153 | |
2184 | |
2154 | case 'n': |
|
|
2155 | *p++ = '\n'; |
|
|
2156 | state = st_ok; |
|
|
2157 | break; |
|
|
2158 | |
|
|
2159 | case 't': |
|
|
2160 | *p++ = '\t'; |
|
|
2161 | state = st_ok; |
|
|
2162 | break; |
|
|
2163 | |
|
|
2164 | case 'r': |
|
|
2165 | *p++ = '\r'; |
|
|
2166 | state = st_ok; |
|
|
2167 | break; |
|
|
2168 | |
|
|
2169 | case '"': |
|
|
2170 | *p++ = '"'; |
|
|
2171 | state = st_ok; |
|
|
2172 | break; |
|
|
2173 | |
|
|
2174 | default: |
2185 | default: |
2175 | *p++ = c; |
2186 | *p++ = c; |
2176 | state = st_ok; |
2187 | state = st_ok; |
2177 | break; |
2188 | break; |
2178 | } |
2189 | } |
2179 | |
2190 | |
2180 | break; |
2191 | break; |
2181 | |
2192 | |
2182 | case st_x1: |
2193 | case st_x1: |
2183 | case st_x2: |
2194 | case st_x2: |
2184 | c = toupper (c); |
2195 | c = tolower (c); |
2185 | |
2196 | |
2186 | if (c >= '0' && c <= 'F') |
2197 | if (c >= '0' && c <= '9') |
2187 | { |
|
|
2188 | if (c <= '9') |
|
|
2189 | c1 = (c1 << 4) + c - '0'; |
2198 | c1 = (c1 << 4) + c - '0'; |
2190 | else |
2199 | else if (c >= 'a' && c <= 'f') |
2191 | c1 = (c1 << 4) + c - 'A' + 10; |
2200 | c1 = (c1 << 4) + c - 'a' + 10; |
2192 | |
|
|
2193 | if (state == st_x1) |
|
|
2194 | state = st_x2; |
|
|
2195 | else |
|
|
2196 | { |
|
|
2197 | *p++ = c1; |
|
|
2198 | state = st_ok; |
|
|
2199 | } |
|
|
2200 | } |
|
|
2201 | else |
2201 | else |
2202 | return S_F; |
2202 | return S_F; |
|
|
2203 | |
|
|
2204 | if (state == st_x1) |
|
|
2205 | state = st_x2; |
|
|
2206 | else |
|
|
2207 | { |
|
|
2208 | *p++ = c1; |
|
|
2209 | state = st_ok; |
|
|
2210 | } |
2203 | |
2211 | |
2204 | break; |
2212 | break; |
2205 | |
2213 | |
2206 | case st_oct1: |
2214 | case st_oct1: |
2207 | case st_oct2: |
2215 | case st_oct2: |
… | |
… | |
2211 | backchar (SCHEME_A_ c); |
2219 | backchar (SCHEME_A_ c); |
2212 | state = st_ok; |
2220 | state = st_ok; |
2213 | } |
2221 | } |
2214 | else |
2222 | else |
2215 | { |
2223 | { |
2216 | if (state == st_oct2 && c1 >= 32) |
2224 | if (state == st_oct2 && c1 >= ' ') |
2217 | return S_F; |
2225 | return S_F; |
2218 | |
2226 | |
2219 | c1 = (c1 << 3) + (c - '0'); |
2227 | c1 = (c1 << 3) + (c - '0'); |
2220 | |
2228 | |
2221 | if (state == st_oct1) |
2229 | if (state == st_oct1) |
… | |
… | |
2226 | state = st_ok; |
2234 | state = st_ok; |
2227 | } |
2235 | } |
2228 | } |
2236 | } |
2229 | |
2237 | |
2230 | break; |
2238 | break; |
2231 | |
|
|
2232 | } |
2239 | } |
2233 | } |
2240 | } |
2234 | } |
2241 | } |
2235 | |
2242 | |
2236 | /* check c is in chars */ |
2243 | /* check c is in chars */ |
2237 | static INLINE int |
2244 | ecb_cold int |
2238 | is_one_of (char *s, int c) |
2245 | is_one_of (const char *s, int c) |
2239 | { |
2246 | { |
2240 | if (c == EOF) |
|
|
2241 | return 1; |
|
|
2242 | |
|
|
2243 | return !!strchr (s, c); |
2247 | return c == EOF || !!strchr (s, c); |
2244 | } |
2248 | } |
2245 | |
2249 | |
2246 | /* skip white characters */ |
2250 | /* skip white characters */ |
2247 | static INLINE int |
2251 | ecb_cold int |
2248 | skipspace (SCHEME_P) |
2252 | skipspace (SCHEME_P) |
2249 | { |
2253 | { |
2250 | int c, curr_line = 0; |
2254 | int c, curr_line = 0; |
2251 | |
2255 | |
2252 | do |
2256 | do |
2253 | { |
2257 | { |
2254 | c = inchar (SCHEME_A); |
2258 | c = inchar (SCHEME_A); |
|
|
2259 | |
2255 | #if SHOW_ERROR_LINE |
2260 | #if SHOW_ERROR_LINE |
2256 | if (c == '\n') |
2261 | if (ecb_expect_false (c == '\n')) |
2257 | curr_line++; |
2262 | curr_line++; |
2258 | #endif |
2263 | #endif |
|
|
2264 | |
|
|
2265 | if (ecb_expect_false (c == EOF)) |
|
|
2266 | return c; |
2259 | } |
2267 | } |
2260 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2268 | while (is_one_of (WHITESPACE, c)); |
2261 | |
2269 | |
2262 | /* record it */ |
2270 | /* record it */ |
2263 | #if SHOW_ERROR_LINE |
2271 | #if SHOW_ERROR_LINE |
2264 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2272 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2265 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2273 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2266 | #endif |
2274 | #endif |
2267 | |
2275 | |
2268 | if (c != EOF) |
|
|
2269 | { |
|
|
2270 | backchar (SCHEME_A_ c); |
2276 | backchar (SCHEME_A_ c); |
2271 | return 1; |
2277 | return 1; |
2272 | } |
|
|
2273 | else |
|
|
2274 | return EOF; |
|
|
2275 | } |
2278 | } |
2276 | |
2279 | |
2277 | /* get token */ |
2280 | /* get token */ |
2278 | static int |
2281 | ecb_cold static int |
2279 | token (SCHEME_P) |
2282 | token (SCHEME_P) |
2280 | { |
2283 | { |
2281 | int c = skipspace (SCHEME_A); |
2284 | int c = skipspace (SCHEME_A); |
2282 | |
2285 | |
2283 | if (c == EOF) |
2286 | if (c == EOF) |
… | |
… | |
2295 | return TOK_RPAREN; |
2298 | return TOK_RPAREN; |
2296 | |
2299 | |
2297 | case '.': |
2300 | case '.': |
2298 | c = inchar (SCHEME_A); |
2301 | c = inchar (SCHEME_A); |
2299 | |
2302 | |
2300 | if (is_one_of (" \n\t", c)) |
2303 | if (is_one_of (WHITESPACE, c)) |
2301 | return TOK_DOT; |
2304 | return TOK_DOT; |
2302 | else |
2305 | else |
2303 | { |
2306 | { |
2304 | //TODO: ungetc twice in a row is not supported in C |
|
|
2305 | backchar (SCHEME_A_ c); |
2307 | backchar (SCHEME_A_ c); |
2306 | backchar (SCHEME_A_ '.'); |
|
|
2307 | return TOK_ATOM; |
2308 | return TOK_DOTATOM; |
2308 | } |
2309 | } |
|
|
2310 | |
|
|
2311 | case '|': |
|
|
2312 | return TOK_STRATOM; |
2309 | |
2313 | |
2310 | case '\'': |
2314 | case '\'': |
2311 | return TOK_QUOTE; |
2315 | return TOK_QUOTE; |
2312 | |
2316 | |
2313 | case ';': |
2317 | case ';': |
… | |
… | |
2380 | } |
2384 | } |
2381 | |
2385 | |
2382 | /* ========== Routines for Printing ========== */ |
2386 | /* ========== Routines for Printing ========== */ |
2383 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2387 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2384 | |
2388 | |
2385 | static void |
2389 | ecb_cold static void |
2386 | printslashstring (SCHEME_P_ char *p, int len) |
2390 | printslashstring (SCHEME_P_ char *p, int len) |
2387 | { |
2391 | { |
2388 | int i; |
2392 | int i; |
2389 | unsigned char *s = (unsigned char *) p; |
2393 | unsigned char *s = (unsigned char *) p; |
2390 | |
2394 | |
… | |
… | |
2445 | } |
2449 | } |
2446 | |
2450 | |
2447 | putcharacter (SCHEME_A_ '"'); |
2451 | putcharacter (SCHEME_A_ '"'); |
2448 | } |
2452 | } |
2449 | |
2453 | |
2450 | |
|
|
2451 | /* print atoms */ |
2454 | /* print atoms */ |
2452 | static void |
2455 | ecb_cold static void |
2453 | printatom (SCHEME_P_ pointer l, int f) |
2456 | printatom (SCHEME_P_ pointer l, int f) |
2454 | { |
2457 | { |
2455 | char *p; |
2458 | char *p; |
2456 | int len; |
2459 | int len; |
2457 | |
2460 | |
2458 | atom2str (SCHEME_A_ l, f, &p, &len); |
2461 | atom2str (SCHEME_A_ l, f, &p, &len); |
2459 | putchars (SCHEME_A_ p, len); |
2462 | putchars (SCHEME_A_ p, len); |
2460 | } |
2463 | } |
2461 | |
2464 | |
2462 | |
|
|
2463 | /* Uses internal buffer unless string pointer is already available */ |
2465 | /* Uses internal buffer unless string pointer is already available */ |
2464 | static void |
2466 | ecb_cold static void |
2465 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2467 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2466 | { |
2468 | { |
2467 | char *p; |
2469 | char *p; |
2468 | |
2470 | |
2469 | if (l == NIL) |
2471 | if (l == NIL) |
… | |
… | |
2480 | { |
2482 | { |
2481 | p = SCHEME_V->strbuff; |
2483 | p = SCHEME_V->strbuff; |
2482 | |
2484 | |
2483 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2485 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2484 | { |
2486 | { |
2485 | if (num_is_integer (l)) |
2487 | if (is_integer (l)) |
2486 | xnum (p, ivalue_unchecked (l)); |
2488 | xnum (p, ivalue_unchecked (l)); |
2487 | #if USE_REAL |
2489 | #if USE_REAL |
2488 | else |
2490 | else |
2489 | { |
2491 | { |
2490 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
2492 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
… | |
… | |
2627 | #endif |
2629 | #endif |
2628 | } |
2630 | } |
2629 | else if (is_continuation (l)) |
2631 | else if (is_continuation (l)) |
2630 | p = "#<CONTINUATION>"; |
2632 | p = "#<CONTINUATION>"; |
2631 | else |
2633 | else |
|
|
2634 | { |
|
|
2635 | #if USE_PRINTF |
|
|
2636 | p = SCHEME_V->strbuff; |
|
|
2637 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2638 | #else |
2632 | p = "#<ERROR>"; |
2639 | p = "#<ERROR>"; |
|
|
2640 | #endif |
|
|
2641 | } |
2633 | |
2642 | |
2634 | *pp = p; |
2643 | *pp = p; |
2635 | *plen = strlen (p); |
2644 | *plen = strlen (p); |
2636 | } |
2645 | } |
2637 | |
2646 | |
… | |
… | |
2669 | return car (d); |
2678 | return car (d); |
2670 | |
2679 | |
2671 | p = cons (car (d), cdr (d)); |
2680 | p = cons (car (d), cdr (d)); |
2672 | q = p; |
2681 | q = p; |
2673 | |
2682 | |
2674 | while (cdr (cdr (p)) != NIL) |
2683 | while (cddr (p) != NIL) |
2675 | { |
2684 | { |
2676 | d = cons (car (p), cdr (p)); |
2685 | d = cons (car (p), cdr (p)); |
2677 | |
2686 | |
2678 | if (cdr (cdr (p)) != NIL) |
2687 | if (cddr (p) != NIL) |
2679 | p = cdr (d); |
2688 | p = cdr (d); |
2680 | } |
2689 | } |
2681 | |
2690 | |
2682 | set_cdr (p, car (cdr (p))); |
2691 | set_cdr (p, cadr (p)); |
2683 | return q; |
2692 | return q; |
2684 | } |
2693 | } |
2685 | |
2694 | |
2686 | /* reverse list -- produce new list */ |
2695 | /* reverse list -- produce new list */ |
2687 | static pointer |
2696 | ecb_hot static pointer |
2688 | reverse (SCHEME_P_ pointer a) |
2697 | reverse (SCHEME_P_ pointer a) |
2689 | { |
2698 | { |
2690 | /* a must be checked by gc */ |
2699 | /* a must be checked by gc */ |
2691 | pointer p = NIL; |
2700 | pointer p = NIL; |
2692 | |
2701 | |
… | |
… | |
2695 | |
2704 | |
2696 | return p; |
2705 | return p; |
2697 | } |
2706 | } |
2698 | |
2707 | |
2699 | /* reverse list --- in-place */ |
2708 | /* reverse list --- in-place */ |
2700 | static pointer |
2709 | ecb_hot static pointer |
2701 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2710 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2702 | { |
2711 | { |
2703 | pointer result = term; |
2712 | pointer result = term; |
2704 | pointer p = list; |
2713 | pointer p = list; |
2705 | |
2714 | |
… | |
… | |
2713 | |
2722 | |
2714 | return result; |
2723 | return result; |
2715 | } |
2724 | } |
2716 | |
2725 | |
2717 | /* append list -- produce new list (in reverse order) */ |
2726 | /* append list -- produce new list (in reverse order) */ |
2718 | static pointer |
2727 | ecb_hot static pointer |
2719 | revappend (SCHEME_P_ pointer a, pointer b) |
2728 | revappend (SCHEME_P_ pointer a, pointer b) |
2720 | { |
2729 | { |
2721 | pointer result = a; |
2730 | pointer result = a; |
2722 | pointer p = b; |
2731 | pointer p = b; |
2723 | |
2732 | |
… | |
… | |
2732 | |
2741 | |
2733 | return S_F; /* signal an error */ |
2742 | return S_F; /* signal an error */ |
2734 | } |
2743 | } |
2735 | |
2744 | |
2736 | /* equivalence of atoms */ |
2745 | /* equivalence of atoms */ |
2737 | int |
2746 | ecb_hot int |
2738 | eqv (pointer a, pointer b) |
2747 | eqv (pointer a, pointer b) |
2739 | { |
2748 | { |
2740 | if (is_string (a)) |
2749 | if (is_string (a)) |
2741 | { |
2750 | { |
2742 | if (is_string (b)) |
2751 | if (is_string (b)) |
… | |
… | |
2745 | return 0; |
2754 | return 0; |
2746 | } |
2755 | } |
2747 | else if (is_number (a)) |
2756 | else if (is_number (a)) |
2748 | { |
2757 | { |
2749 | if (is_number (b)) |
2758 | if (is_number (b)) |
2750 | if (num_is_integer (a) == num_is_integer (b)) |
|
|
2751 | return num_cmp (nvalue (a), nvalue (b)) == 0; |
2759 | return num_cmp (nvalue (a), nvalue (b)) == 0; |
2752 | |
2760 | |
2753 | return 0; |
2761 | return 0; |
2754 | } |
2762 | } |
2755 | else if (is_character (a)) |
2763 | else if (is_character (a)) |
2756 | { |
2764 | { |
… | |
… | |
2782 | /* () is #t in R5RS */ |
2790 | /* () is #t in R5RS */ |
2783 | #define is_true(p) ((p) != S_F) |
2791 | #define is_true(p) ((p) != S_F) |
2784 | #define is_false(p) ((p) == S_F) |
2792 | #define is_false(p) ((p) == S_F) |
2785 | |
2793 | |
2786 | /* ========== Environment implementation ========== */ |
2794 | /* ========== Environment implementation ========== */ |
2787 | |
|
|
2788 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
|
|
2789 | |
|
|
2790 | static int |
|
|
2791 | hash_fn (const char *key, int table_size) |
|
|
2792 | { |
|
|
2793 | const unsigned char *p = key; |
|
|
2794 | uint32_t hash = 2166136261; |
|
|
2795 | |
|
|
2796 | while (*p) |
|
|
2797 | hash = (hash ^ *p++) * 16777619; |
|
|
2798 | |
|
|
2799 | return hash % table_size; |
|
|
2800 | } |
|
|
2801 | #endif |
|
|
2802 | |
2795 | |
2803 | #ifndef USE_ALIST_ENV |
2796 | #ifndef USE_ALIST_ENV |
2804 | |
2797 | |
2805 | /* |
2798 | /* |
2806 | * In this implementation, each frame of the environment may be |
2799 | * In this implementation, each frame of the environment may be |
… | |
… | |
2823 | |
2816 | |
2824 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2817 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2825 | setenvironment (SCHEME_V->envir); |
2818 | setenvironment (SCHEME_V->envir); |
2826 | } |
2819 | } |
2827 | |
2820 | |
2828 | static INLINE void |
2821 | static uint32_t |
|
|
2822 | sym_hash (pointer sym, uint32_t size) |
|
|
2823 | { |
|
|
2824 | uintptr_t ptr = (uintptr_t)sym; |
|
|
2825 | |
|
|
2826 | #if 0 |
|
|
2827 | /* table size is prime, so why mix */ |
|
|
2828 | ptr += ptr >> 32; |
|
|
2829 | ptr += ptr >> 16; |
|
|
2830 | ptr += ptr >> 8; |
|
|
2831 | #endif |
|
|
2832 | |
|
|
2833 | return ptr % size; |
|
|
2834 | } |
|
|
2835 | |
|
|
2836 | ecb_inline void |
2829 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2837 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2830 | { |
2838 | { |
2831 | pointer slot = immutable_cons (variable, value); |
2839 | pointer slot = immutable_cons (variable, value); |
2832 | |
2840 | |
2833 | if (is_vector (car (env))) |
2841 | if (is_vector (car (env))) |
2834 | { |
2842 | { |
2835 | int location = hash_fn (symname (variable), veclength (car (env))); |
2843 | int location = sym_hash (variable, veclength (car (env))); |
2836 | |
|
|
2837 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
2844 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2838 | } |
2845 | } |
2839 | else |
2846 | else |
2840 | set_car (env, immutable_cons (slot, car (env))); |
2847 | set_car (env, immutable_cons (slot, car (env))); |
2841 | } |
2848 | } |
2842 | |
2849 | |
2843 | static pointer |
2850 | ecb_hot static pointer |
2844 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2851 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2845 | { |
2852 | { |
2846 | pointer x, y; |
2853 | pointer x, y; |
2847 | int location; |
|
|
2848 | |
2854 | |
2849 | for (x = env; x != NIL; x = cdr (x)) |
2855 | for (x = env; x != NIL; x = cdr (x)) |
2850 | { |
2856 | { |
2851 | if (is_vector (car (x))) |
2857 | if (is_vector (car (x))) |
2852 | { |
2858 | { |
2853 | location = hash_fn (symname (hdl), veclength (car (x))); |
2859 | int location = sym_hash (hdl, veclength (car (x))); |
2854 | y = vector_elem (car (x), location); |
2860 | y = vector_get (car (x), location); |
2855 | } |
2861 | } |
2856 | else |
2862 | else |
2857 | y = car (x); |
2863 | y = car (x); |
2858 | |
2864 | |
2859 | for (; y != NIL; y = cdr (y)) |
2865 | for (; y != NIL; y = cdr (y)) |
2860 | if (caar (y) == hdl) |
2866 | if (caar (y) == hdl) |
2861 | break; |
2867 | break; |
2862 | |
2868 | |
2863 | if (y != NIL) |
2869 | if (y != NIL) |
|
|
2870 | return car (y); |
|
|
2871 | |
|
|
2872 | if (!all) |
2864 | break; |
2873 | break; |
2865 | |
|
|
2866 | if (!all) |
|
|
2867 | return NIL; |
|
|
2868 | } |
2874 | } |
2869 | |
|
|
2870 | if (x != NIL) |
|
|
2871 | return car (y); |
|
|
2872 | |
2875 | |
2873 | return NIL; |
2876 | return NIL; |
2874 | } |
2877 | } |
2875 | |
2878 | |
2876 | #else /* USE_ALIST_ENV */ |
2879 | #else /* USE_ALIST_ENV */ |
2877 | |
2880 | |
2878 | static INLINE void |
2881 | static void |
2879 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2882 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2880 | { |
2883 | { |
2881 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2884 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2882 | setenvironment (SCHEME_V->envir); |
2885 | setenvironment (SCHEME_V->envir); |
2883 | } |
2886 | } |
2884 | |
2887 | |
2885 | static INLINE void |
2888 | static void |
2886 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2889 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2887 | { |
2890 | { |
2888 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2891 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2889 | } |
2892 | } |
2890 | |
2893 | |
2891 | static pointer |
2894 | ecb_hot static pointer |
2892 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2895 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2893 | { |
2896 | { |
2894 | pointer x, y; |
2897 | pointer x, y; |
2895 | |
2898 | |
2896 | for (x = env; x != NIL; x = cdr (x)) |
2899 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2898 | for (y = car (x); y != NIL; y = cdr (y)) |
2901 | for (y = car (x); y != NIL; y = cdr (y)) |
2899 | if (caar (y) == hdl) |
2902 | if (caar (y) == hdl) |
2900 | break; |
2903 | break; |
2901 | |
2904 | |
2902 | if (y != NIL) |
2905 | if (y != NIL) |
|
|
2906 | return car (y); |
2903 | break; |
2907 | break; |
2904 | |
2908 | |
2905 | if (!all) |
2909 | if (!all) |
2906 | return NIL; |
2910 | break; |
2907 | } |
2911 | } |
2908 | |
|
|
2909 | if (x != NIL) |
|
|
2910 | return car (y); |
|
|
2911 | |
2912 | |
2912 | return NIL; |
2913 | return NIL; |
2913 | } |
2914 | } |
2914 | |
2915 | |
2915 | #endif /* USE_ALIST_ENV else */ |
2916 | #endif /* USE_ALIST_ENV else */ |
2916 | |
2917 | |
2917 | static INLINE void |
2918 | static void |
2918 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2919 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2919 | { |
2920 | { |
|
|
2921 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2920 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2922 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2921 | } |
2923 | } |
2922 | |
2924 | |
2923 | static INLINE void |
2925 | static void |
2924 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2926 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2925 | { |
2927 | { |
2926 | set_cdr (slot, value); |
2928 | set_cdr (slot, value); |
2927 | } |
2929 | } |
2928 | |
2930 | |
2929 | static INLINE pointer |
2931 | static pointer |
2930 | slot_value_in_env (pointer slot) |
2932 | slot_value_in_env (pointer slot) |
2931 | { |
2933 | { |
2932 | return cdr (slot); |
2934 | return cdr (slot); |
2933 | } |
2935 | } |
2934 | |
2936 | |
2935 | /* ========== Evaluation Cycle ========== */ |
2937 | /* ========== Evaluation Cycle ========== */ |
2936 | |
2938 | |
2937 | static int |
2939 | ecb_cold static int |
2938 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2940 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2939 | { |
2941 | { |
2940 | #if USE_ERROR_HOOK |
2942 | #if USE_ERROR_HOOK |
2941 | pointer x; |
2943 | pointer x; |
2942 | pointer hdl = SCHEME_V->ERROR_HOOK; |
2944 | pointer hdl = SCHEME_V->ERROR_HOOK; |
… | |
… | |
3018 | pointer code; |
3020 | pointer code; |
3019 | }; |
3021 | }; |
3020 | |
3022 | |
3021 | # define STACK_GROWTH 3 |
3023 | # define STACK_GROWTH 3 |
3022 | |
3024 | |
3023 | static void |
3025 | ecb_hot static void |
3024 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3026 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3025 | { |
3027 | { |
3026 | int nframes = (uintptr_t)SCHEME_V->dump; |
3028 | int nframes = (uintptr_t)SCHEME_V->dump; |
3027 | struct dump_stack_frame *next_frame; |
3029 | struct dump_stack_frame *next_frame; |
3028 | |
3030 | |
3029 | /* enough room for the next frame? */ |
3031 | /* enough room for the next frame? */ |
3030 | if (nframes >= SCHEME_V->dump_size) |
3032 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3031 | { |
3033 | { |
3032 | SCHEME_V->dump_size += STACK_GROWTH; |
3034 | SCHEME_V->dump_size += STACK_GROWTH; |
3033 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3035 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3034 | } |
3036 | } |
3035 | |
3037 | |
… | |
… | |
3041 | next_frame->code = code; |
3043 | next_frame->code = code; |
3042 | |
3044 | |
3043 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3045 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3044 | } |
3046 | } |
3045 | |
3047 | |
3046 | static int |
3048 | static ecb_hot int |
3047 | xs_return (SCHEME_P_ pointer a) |
3049 | xs_return (SCHEME_P_ pointer a) |
3048 | { |
3050 | { |
3049 | int nframes = (uintptr_t)SCHEME_V->dump; |
3051 | int nframes = (uintptr_t)SCHEME_V->dump; |
3050 | struct dump_stack_frame *frame; |
3052 | struct dump_stack_frame *frame; |
3051 | |
3053 | |
… | |
… | |
3062 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3064 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3063 | |
3065 | |
3064 | return 0; |
3066 | return 0; |
3065 | } |
3067 | } |
3066 | |
3068 | |
3067 | static INLINE void |
3069 | ecb_cold void |
3068 | dump_stack_reset (SCHEME_P) |
3070 | dump_stack_reset (SCHEME_P) |
3069 | { |
3071 | { |
3070 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3072 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3071 | SCHEME_V->dump = (pointer)+0; |
3073 | SCHEME_V->dump = (pointer)+0; |
3072 | } |
3074 | } |
3073 | |
3075 | |
3074 | static INLINE void |
3076 | ecb_cold void |
3075 | dump_stack_initialize (SCHEME_P) |
3077 | dump_stack_initialize (SCHEME_P) |
3076 | { |
3078 | { |
3077 | SCHEME_V->dump_size = 0; |
3079 | SCHEME_V->dump_size = 0; |
3078 | SCHEME_V->dump_base = 0; |
3080 | SCHEME_V->dump_base = 0; |
3079 | dump_stack_reset (SCHEME_A); |
3081 | dump_stack_reset (SCHEME_A); |
3080 | } |
3082 | } |
3081 | |
3083 | |
3082 | static void |
3084 | ecb_cold static void |
3083 | dump_stack_free (SCHEME_P) |
3085 | dump_stack_free (SCHEME_P) |
3084 | { |
3086 | { |
3085 | free (SCHEME_V->dump_base); |
3087 | free (SCHEME_V->dump_base); |
3086 | SCHEME_V->dump_base = 0; |
3088 | SCHEME_V->dump_base = 0; |
3087 | SCHEME_V->dump = (pointer)0; |
3089 | SCHEME_V->dump = (pointer)0; |
3088 | SCHEME_V->dump_size = 0; |
3090 | SCHEME_V->dump_size = 0; |
3089 | } |
3091 | } |
3090 | |
3092 | |
3091 | static void |
3093 | ecb_cold static void |
3092 | dump_stack_mark (SCHEME_P) |
3094 | dump_stack_mark (SCHEME_P) |
3093 | { |
3095 | { |
3094 | int nframes = (uintptr_t)SCHEME_V->dump; |
3096 | int nframes = (uintptr_t)SCHEME_V->dump; |
3095 | int i; |
3097 | int i; |
3096 | |
3098 | |
… | |
… | |
3102 | mark (frame->envir); |
3104 | mark (frame->envir); |
3103 | mark (frame->code); |
3105 | mark (frame->code); |
3104 | } |
3106 | } |
3105 | } |
3107 | } |
3106 | |
3108 | |
3107 | static pointer |
3109 | ecb_cold static pointer |
3108 | ss_get_cont (SCHEME_P) |
3110 | ss_get_cont (SCHEME_P) |
3109 | { |
3111 | { |
3110 | int nframes = (uintptr_t)SCHEME_V->dump; |
3112 | int nframes = (uintptr_t)SCHEME_V->dump; |
3111 | int i; |
3113 | int i; |
3112 | |
3114 | |
… | |
… | |
3124 | } |
3126 | } |
3125 | |
3127 | |
3126 | return cont; |
3128 | return cont; |
3127 | } |
3129 | } |
3128 | |
3130 | |
3129 | static void |
3131 | ecb_cold static void |
3130 | ss_set_cont (SCHEME_P_ pointer cont) |
3132 | ss_set_cont (SCHEME_P_ pointer cont) |
3131 | { |
3133 | { |
3132 | int i = 0; |
3134 | int i = 0; |
3133 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3135 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3134 | |
3136 | |
3135 | while (cont != NIL) |
3137 | while (cont != NIL) |
3136 | { |
3138 | { |
3137 | frame->op = ivalue (car (cont)); cont = cdr (cont); |
3139 | frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont); |
3138 | frame->args = car (cont) ; cont = cdr (cont); |
3140 | frame->args = car (cont) ; cont = cdr (cont); |
3139 | frame->envir = car (cont) ; cont = cdr (cont); |
3141 | frame->envir = car (cont) ; cont = cdr (cont); |
3140 | frame->code = car (cont) ; cont = cdr (cont); |
3142 | frame->code = car (cont) ; cont = cdr (cont); |
3141 | |
3143 | |
3142 | ++frame; |
3144 | ++frame; |
3143 | ++i; |
3145 | ++i; |
3144 | } |
3146 | } |
3145 | |
3147 | |
3146 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3148 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3147 | } |
3149 | } |
3148 | |
3150 | |
3149 | #else |
3151 | #else |
3150 | |
3152 | |
3151 | static INLINE void |
3153 | ecb_cold void |
3152 | dump_stack_reset (SCHEME_P) |
3154 | dump_stack_reset (SCHEME_P) |
3153 | { |
3155 | { |
3154 | SCHEME_V->dump = NIL; |
3156 | SCHEME_V->dump = NIL; |
3155 | } |
3157 | } |
3156 | |
3158 | |
3157 | static INLINE void |
3159 | ecb_cold void |
3158 | dump_stack_initialize (SCHEME_P) |
3160 | dump_stack_initialize (SCHEME_P) |
3159 | { |
3161 | { |
3160 | dump_stack_reset (SCHEME_A); |
3162 | dump_stack_reset (SCHEME_A); |
3161 | } |
3163 | } |
3162 | |
3164 | |
3163 | static void |
3165 | ecb_cold static void |
3164 | dump_stack_free (SCHEME_P) |
3166 | dump_stack_free (SCHEME_P) |
3165 | { |
3167 | { |
3166 | SCHEME_V->dump = NIL; |
3168 | SCHEME_V->dump = NIL; |
3167 | } |
3169 | } |
3168 | |
3170 | |
3169 | static int |
3171 | ecb_hot static int |
3170 | xs_return (SCHEME_P_ pointer a) |
3172 | xs_return (SCHEME_P_ pointer a) |
3171 | { |
3173 | { |
3172 | pointer dump = SCHEME_V->dump; |
3174 | pointer dump = SCHEME_V->dump; |
3173 | |
3175 | |
3174 | SCHEME_V->value = a; |
3176 | SCHEME_V->value = a; |
3175 | |
3177 | |
3176 | if (dump == NIL) |
3178 | if (dump == NIL) |
3177 | return -1; |
3179 | return -1; |
3178 | |
3180 | |
3179 | SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); |
3181 | SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump); |
3180 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3182 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3181 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3183 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3182 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3184 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3183 | |
3185 | |
3184 | SCHEME_V->dump = dump; |
3186 | SCHEME_V->dump = dump; |
3185 | |
3187 | |
3186 | return 0; |
3188 | return 0; |
3187 | } |
3189 | } |
3188 | |
3190 | |
3189 | static void |
3191 | ecb_hot static void |
3190 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3192 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3191 | { |
3193 | { |
3192 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3194 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3193 | cons (args, |
3195 | cons (args, |
3194 | cons (SCHEME_V->envir, |
3196 | cons (SCHEME_V->envir, |
3195 | cons (code, |
3197 | cons (code, |
3196 | SCHEME_V->dump)))); |
3198 | SCHEME_V->dump)))); |
3197 | } |
3199 | } |
3198 | |
3200 | |
|
|
3201 | ecb_cold static void |
|
|
3202 | dump_stack_mark (SCHEME_P) |
|
|
3203 | { |
|
|
3204 | mark (SCHEME_V->dump); |
|
|
3205 | } |
|
|
3206 | |
|
|
3207 | ecb_cold static pointer |
|
|
3208 | ss_get_cont (SCHEME_P) |
|
|
3209 | { |
|
|
3210 | return SCHEME_V->dump; |
|
|
3211 | } |
|
|
3212 | |
|
|
3213 | ecb_cold static void |
|
|
3214 | ss_set_cont (SCHEME_P_ pointer cont) |
|
|
3215 | { |
|
|
3216 | SCHEME_V->dump = cont; |
|
|
3217 | } |
|
|
3218 | |
|
|
3219 | #endif |
|
|
3220 | |
|
|
3221 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
|
|
3222 | |
|
|
3223 | #if EXPERIMENT |
|
|
3224 | |
|
|
3225 | typedef void *stream[1]; |
|
|
3226 | |
|
|
3227 | #define stream_init() { 0 } |
|
|
3228 | |
|
|
3229 | ecb_cold static void |
|
|
3230 | stream_put (void **s, uint8_t byte) |
|
|
3231 | { |
|
|
3232 | uint32_t *sp = *s; |
|
|
3233 | uint32_t size = sizeof (uint32_t) * 2; |
|
|
3234 | uint32_t offs = size; |
|
|
3235 | |
|
|
3236 | if (ecb_expect_true (sp)) |
|
|
3237 | { |
|
|
3238 | offs = sp[0]; |
|
|
3239 | size = sp[1]; |
|
|
3240 | } |
|
|
3241 | |
|
|
3242 | if (ecb_expect_false (offs == size)) |
|
|
3243 | { |
|
|
3244 | size *= 2; |
|
|
3245 | sp = realloc (sp, size); |
|
|
3246 | *s = sp; |
|
|
3247 | sp[1] = size; |
|
|
3248 | |
|
|
3249 | } |
|
|
3250 | |
|
|
3251 | ((uint8_t *)sp)[offs++] = byte; |
|
|
3252 | sp[0] = offs; |
|
|
3253 | } |
|
|
3254 | |
|
|
3255 | #define stream_data(s) ((char *)(s)[0] + sizeof (uint32_t) * 2) |
|
|
3256 | #define stream_size(s) (((uint32_t *)(s)[0])[0] - sizeof (uint32_t) * 2) |
|
|
3257 | #define stream_free(s) free (s[0]) |
|
|
3258 | |
|
|
3259 | // calculates a (preferably small) integer that makes it possible to find |
|
|
3260 | // the symbol again. if pointers were offsets into a memory area... until |
|
|
3261 | // then, we return segment number in the low bits, and offset in the high |
|
|
3262 | // bits |
|
|
3263 | static uint32_t |
|
|
3264 | symbol_id (SCHEME_P_ pointer sym) |
|
|
3265 | { |
|
|
3266 | struct cell *p = CELL (sym); |
|
|
3267 | int i; |
|
|
3268 | |
|
|
3269 | for (i = SCHEME_V->last_cell_seg; i >= 0; --i) |
|
|
3270 | if (SCHEME_V->cell_seg[i] <= p && p < SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize[i]) |
|
|
3271 | { |
|
|
3272 | printf ("seg %d ofs %d/%d\n",i,(p - SCHEME_V->cell_seg[i]),SCHEME_V->cell_segsize[i]);//D |
|
|
3273 | return i | ((p - SCHEME_V->cell_seg[i]) << CELL_NSEGMENT_LOG); |
|
|
3274 | } |
|
|
3275 | |
|
|
3276 | abort (); |
|
|
3277 | } |
|
|
3278 | |
3199 | static void |
3279 | static void |
3200 | dump_stack_mark (SCHEME_P) |
3280 | compile (SCHEME_P_ stream s, pointer x) |
3201 | { |
3281 | { |
3202 | mark (SCHEME_V->dump); |
3282 | if (x == NIL) |
3203 | } |
3283 | { |
|
|
3284 | stream_put (s, 0); |
|
|
3285 | return; |
|
|
3286 | } |
3204 | |
3287 | |
3205 | static pointer |
3288 | if (is_syntax (x)) |
3206 | ss_get_cont (SCHEME_P) |
3289 | { |
3207 | { |
3290 | stream_put (s, 1); |
3208 | return SCHEME_V->dump; |
3291 | stream_put (s, syntaxnum (x)); |
3209 | } |
3292 | return; |
|
|
3293 | } |
3210 | |
3294 | |
3211 | static void |
3295 | switch (type (x)) |
3212 | ss_set_cont (SCHEME_P_ pointer cont) |
3296 | { |
3213 | { |
3297 | case T_INTEGER: |
3214 | SCHEME_V->dump = cont; |
3298 | stream_put (s, 2); |
3215 | } |
3299 | stream_put (s, 0); |
|
|
3300 | stream_put (s, 0); |
|
|
3301 | stream_put (s, 0); |
|
|
3302 | stream_put (s, 0); |
|
|
3303 | return; |
3216 | |
3304 | |
3217 | #endif |
3305 | case T_SYMBOL: |
|
|
3306 | { |
|
|
3307 | uint32_t sym = symbol_id (SCHEME_A_ x); |
|
|
3308 | printf ("sym %x\n", sym);//D |
3218 | |
3309 | |
3219 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3310 | stream_put (s, 3); |
|
|
3311 | |
|
|
3312 | while (sym > 0x7f) |
|
|
3313 | { |
|
|
3314 | stream_put (s, sym | 0x80); |
|
|
3315 | sym >>= 8; |
|
|
3316 | } |
|
|
3317 | |
|
|
3318 | stream_put (s, sym); |
|
|
3319 | } |
|
|
3320 | return; |
|
|
3321 | |
|
|
3322 | case T_PAIR: |
|
|
3323 | stream_put (s, 4); |
|
|
3324 | while (x != NIL) |
|
|
3325 | { |
|
|
3326 | compile (SCHEME_A_ s, car (x)); |
|
|
3327 | x = cdr (x); |
|
|
3328 | } |
|
|
3329 | stream_put (s, 0xff); |
|
|
3330 | return; |
|
|
3331 | |
|
|
3332 | default: |
|
|
3333 | stream_put (s, 5); |
|
|
3334 | stream_put (s, type (x)); |
|
|
3335 | stream_put (s, 0); |
|
|
3336 | stream_put (s, 0); |
|
|
3337 | stream_put (s, 0); |
|
|
3338 | stream_put (s, 0); |
|
|
3339 | break; |
|
|
3340 | } |
|
|
3341 | } |
3220 | |
3342 | |
3221 | static int |
3343 | static int |
|
|
3344 | compile_closure (SCHEME_P_ pointer p) |
|
|
3345 | { |
|
|
3346 | stream s = stream_init (); |
|
|
3347 | |
|
|
3348 | printatom (SCHEME_A_ p, 1);//D |
|
|
3349 | compile (SCHEME_A_ s, car (p)); |
|
|
3350 | |
|
|
3351 | FILE *xxd = popen ("xxd", "we"); |
|
|
3352 | fwrite (stream_data (s), 1, stream_size (s), xxd); |
|
|
3353 | fclose (xxd); |
|
|
3354 | |
|
|
3355 | return stream_size (s); |
|
|
3356 | } |
|
|
3357 | |
|
|
3358 | static int |
|
|
3359 | dtree (SCHEME_P_ int indent, pointer x) |
|
|
3360 | { |
|
|
3361 | int c; |
|
|
3362 | |
|
|
3363 | if (is_syntax (x)) |
|
|
3364 | { |
|
|
3365 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3366 | return 8 + 8; |
|
|
3367 | } |
|
|
3368 | |
|
|
3369 | if (x == NIL) |
|
|
3370 | { |
|
|
3371 | printf ("%*sNIL\n", indent, ""); |
|
|
3372 | return 3; |
|
|
3373 | } |
|
|
3374 | |
|
|
3375 | switch (type (x)) |
|
|
3376 | { |
|
|
3377 | case T_INTEGER: |
|
|
3378 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3379 | return 32+8; |
|
|
3380 | |
|
|
3381 | case T_SYMBOL: |
|
|
3382 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3383 | return 24+8; |
|
|
3384 | |
|
|
3385 | case T_CLOSURE: |
|
|
3386 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3387 | dtree (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3388 | return 32 + dtree (SCHEME_A_ indent + 3, car (x)); |
|
|
3389 | |
|
|
3390 | case T_PAIR: |
|
|
3391 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3392 | c = dtree (SCHEME_A_ indent + 3, car (x)); |
|
|
3393 | c += dtree (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3394 | return c + 1; |
|
|
3395 | |
|
|
3396 | case T_PORT: |
|
|
3397 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3398 | return 24+8; |
|
|
3399 | |
|
|
3400 | case T_VECTOR: |
|
|
3401 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3402 | return 24+8; |
|
|
3403 | |
|
|
3404 | case T_ENVIRONMENT: |
|
|
3405 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3406 | return 0 + dtree (SCHEME_A_ indent + 3, car (x)); |
|
|
3407 | |
|
|
3408 | default: |
|
|
3409 | printf ("unhandled type %d\n", type (x)); |
|
|
3410 | break; |
|
|
3411 | } |
|
|
3412 | } |
|
|
3413 | #endif |
|
|
3414 | |
|
|
3415 | /* syntax, eval, core, ... */ |
|
|
3416 | ecb_hot static int |
3222 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3417 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3223 | { |
3418 | { |
3224 | pointer args = SCHEME_V->args; |
3419 | pointer args = SCHEME_V->args; |
3225 | pointer x, y; |
3420 | pointer x, y; |
3226 | |
3421 | |
3227 | switch (op) |
3422 | switch (op) |
3228 | { |
3423 | { |
|
|
3424 | #if EXPERIMENT //D |
|
|
3425 | case OP_DEBUG: |
|
|
3426 | { |
|
|
3427 | uint32_t len = compile_closure (SCHEME_A_ car (args)); |
|
|
3428 | printf ("len = %d\n", len); |
|
|
3429 | printf ("\n"); |
|
|
3430 | s_return (S_T); |
|
|
3431 | } |
|
|
3432 | #endif |
3229 | case OP_LOAD: /* load */ |
3433 | case OP_LOAD: /* load */ |
3230 | if (file_interactive (SCHEME_A)) |
3434 | if (file_interactive (SCHEME_A)) |
3231 | { |
3435 | { |
3232 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3436 | putstr (SCHEME_A_ "Loading "); |
3233 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3437 | putstr (SCHEME_A_ strvalue (car (args))); |
|
|
3438 | putcharacter (SCHEME_A_ '\n'); |
3234 | } |
3439 | } |
3235 | |
3440 | |
3236 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3441 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3237 | Error_1 ("unable to open", car (args)); |
3442 | Error_1 ("unable to open", car (args)); |
3238 | else |
3443 | |
3239 | { |
|
|
3240 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3444 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3241 | s_goto (OP_T0LVL); |
3445 | s_goto (OP_T0LVL); |
3242 | } |
|
|
3243 | |
3446 | |
3244 | case OP_T0LVL: /* top level */ |
3447 | case OP_T0LVL: /* top level */ |
3245 | |
3448 | |
3246 | /* If we reached the end of file, this loop is done. */ |
3449 | /* If we reached the end of file, this loop is done. */ |
3247 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3450 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3248 | { |
3451 | { |
3249 | if (SCHEME_V->file_i == 0) |
3452 | if (SCHEME_V->file_i == 0) |
3250 | { |
3453 | { |
3251 | SCHEME_V->args = NIL; |
3454 | SCHEME_V->args = NIL; |
3252 | s_goto (OP_QUIT); |
3455 | s_goto (OP_QUIT); |
… | |
… | |
3263 | /* If interactive, be nice to user. */ |
3466 | /* If interactive, be nice to user. */ |
3264 | if (file_interactive (SCHEME_A)) |
3467 | if (file_interactive (SCHEME_A)) |
3265 | { |
3468 | { |
3266 | SCHEME_V->envir = SCHEME_V->global_env; |
3469 | SCHEME_V->envir = SCHEME_V->global_env; |
3267 | dump_stack_reset (SCHEME_A); |
3470 | dump_stack_reset (SCHEME_A); |
3268 | putstr (SCHEME_A_ "\n"); |
3471 | putcharacter (SCHEME_A_ '\n'); |
|
|
3472 | #if EXPERIMENT |
|
|
3473 | system ("ps v $PPID"); |
|
|
3474 | #endif |
3269 | putstr (SCHEME_A_ prompt); |
3475 | putstr (SCHEME_A_ prompt); |
3270 | } |
3476 | } |
3271 | |
3477 | |
3272 | /* Set up another iteration of REPL */ |
3478 | /* Set up another iteration of REPL */ |
3273 | SCHEME_V->nesting = 0; |
3479 | SCHEME_V->nesting = 0; |
… | |
… | |
3308 | { |
3514 | { |
3309 | SCHEME_V->print_flag = 1; |
3515 | SCHEME_V->print_flag = 1; |
3310 | SCHEME_V->args = SCHEME_V->value; |
3516 | SCHEME_V->args = SCHEME_V->value; |
3311 | s_goto (OP_P0LIST); |
3517 | s_goto (OP_P0LIST); |
3312 | } |
3518 | } |
3313 | else |
3519 | |
3314 | s_return (SCHEME_V->value); |
3520 | s_return (SCHEME_V->value); |
3315 | |
3521 | |
3316 | case OP_EVAL: /* main part of evaluation */ |
3522 | case OP_EVAL: /* main part of evaluation */ |
3317 | #if USE_TRACING |
3523 | #if USE_TRACING |
3318 | if (SCHEME_V->tracing) |
3524 | if (SCHEME_V->tracing) |
3319 | { |
3525 | { |
… | |
… | |
3330 | #endif |
3536 | #endif |
3331 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3537 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3332 | { |
3538 | { |
3333 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3539 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3334 | |
3540 | |
3335 | if (x != NIL) |
3541 | if (x == NIL) |
3336 | s_return (slot_value_in_env (x)); |
|
|
3337 | else |
|
|
3338 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3542 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3543 | |
|
|
3544 | s_return (slot_value_in_env (x)); |
3339 | } |
3545 | } |
3340 | else if (is_pair (SCHEME_V->code)) |
3546 | else if (is_pair (SCHEME_V->code)) |
3341 | { |
3547 | { |
3342 | x = car (SCHEME_V->code); |
3548 | x = car (SCHEME_V->code); |
3343 | |
3549 | |
… | |
… | |
3352 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3558 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3353 | SCHEME_V->code = x; |
3559 | SCHEME_V->code = x; |
3354 | s_goto (OP_EVAL); |
3560 | s_goto (OP_EVAL); |
3355 | } |
3561 | } |
3356 | } |
3562 | } |
3357 | else |
3563 | |
3358 | s_return (SCHEME_V->code); |
3564 | s_return (SCHEME_V->code); |
3359 | |
3565 | |
3360 | case OP_E0ARGS: /* eval arguments */ |
3566 | case OP_E0ARGS: /* eval arguments */ |
3361 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3567 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3362 | { |
3568 | { |
3363 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3569 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3364 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3570 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3365 | SCHEME_V->code = SCHEME_V->value; |
3571 | SCHEME_V->code = SCHEME_V->value; |
3366 | s_goto (OP_APPLY); |
3572 | s_goto (OP_APPLY); |
… | |
… | |
3393 | |
3599 | |
3394 | case OP_TRACING: |
3600 | case OP_TRACING: |
3395 | { |
3601 | { |
3396 | int tr = SCHEME_V->tracing; |
3602 | int tr = SCHEME_V->tracing; |
3397 | |
3603 | |
3398 | SCHEME_V->tracing = ivalue (car (args)); |
3604 | SCHEME_V->tracing = ivalue_unchecked (car (args)); |
3399 | s_return (mk_integer (SCHEME_A_ tr)); |
3605 | s_return (mk_integer (SCHEME_A_ tr)); |
3400 | } |
3606 | } |
3401 | |
3607 | |
3402 | #endif |
3608 | #endif |
3403 | |
3609 | |
… | |
… | |
3420 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3626 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3421 | else if (is_foreign (SCHEME_V->code)) |
3627 | else if (is_foreign (SCHEME_V->code)) |
3422 | { |
3628 | { |
3423 | /* Keep nested calls from GC'ing the arglist */ |
3629 | /* Keep nested calls from GC'ing the arglist */ |
3424 | push_recent_alloc (SCHEME_A_ args, NIL); |
3630 | push_recent_alloc (SCHEME_A_ args, NIL); |
3425 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3631 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3426 | |
3632 | |
3427 | s_return (x); |
3633 | s_return (x); |
3428 | } |
3634 | } |
3429 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3635 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3430 | { |
3636 | { |
… | |
… | |
3467 | |
3673 | |
3468 | case OP_DOMACRO: /* do macro */ |
3674 | case OP_DOMACRO: /* do macro */ |
3469 | SCHEME_V->code = SCHEME_V->value; |
3675 | SCHEME_V->code = SCHEME_V->value; |
3470 | s_goto (OP_EVAL); |
3676 | s_goto (OP_EVAL); |
3471 | |
3677 | |
3472 | #if 1 |
|
|
3473 | |
|
|
3474 | case OP_LAMBDA: /* lambda */ |
3678 | case OP_LAMBDA: /* lambda */ |
3475 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3679 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3476 | set SCHEME_V->value fall thru */ |
3680 | set SCHEME_V->value fall thru */ |
3477 | { |
3681 | { |
3478 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3682 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
… | |
… | |
3484 | SCHEME_V->code = slot_value_in_env (f); |
3688 | SCHEME_V->code = slot_value_in_env (f); |
3485 | s_goto (OP_APPLY); |
3689 | s_goto (OP_APPLY); |
3486 | } |
3690 | } |
3487 | |
3691 | |
3488 | SCHEME_V->value = SCHEME_V->code; |
3692 | SCHEME_V->value = SCHEME_V->code; |
3489 | /* Fallthru */ |
|
|
3490 | } |
3693 | } |
|
|
3694 | /* Fallthru */ |
3491 | |
3695 | |
3492 | case OP_LAMBDA1: |
3696 | case OP_LAMBDA1: |
3493 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3697 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3494 | |
|
|
3495 | #else |
|
|
3496 | |
|
|
3497 | case OP_LAMBDA: /* lambda */ |
|
|
3498 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
|
|
3499 | |
|
|
3500 | #endif |
|
|
3501 | |
3698 | |
3502 | case OP_MKCLOSURE: /* make-closure */ |
3699 | case OP_MKCLOSURE: /* make-closure */ |
3503 | x = car (args); |
3700 | x = car (args); |
3504 | |
3701 | |
3505 | if (car (x) == SCHEME_V->LAMBDA) |
3702 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
3544 | else |
3741 | else |
3545 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3742 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3546 | |
3743 | |
3547 | s_return (SCHEME_V->code); |
3744 | s_return (SCHEME_V->code); |
3548 | |
3745 | |
3549 | |
|
|
3550 | case OP_DEFP: /* defined? */ |
3746 | case OP_DEFP: /* defined? */ |
3551 | x = SCHEME_V->envir; |
3747 | x = SCHEME_V->envir; |
3552 | |
3748 | |
3553 | if (cdr (args) != NIL) |
3749 | if (cdr (args) != NIL) |
3554 | x = cadr (args); |
3750 | x = cadr (args); |
… | |
… | |
3571 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3767 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3572 | s_return (SCHEME_V->value); |
3768 | s_return (SCHEME_V->value); |
3573 | } |
3769 | } |
3574 | else |
3770 | else |
3575 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3771 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3576 | |
|
|
3577 | |
3772 | |
3578 | case OP_BEGIN: /* begin */ |
3773 | case OP_BEGIN: /* begin */ |
3579 | if (!is_pair (SCHEME_V->code)) |
3774 | if (!is_pair (SCHEME_V->code)) |
3580 | s_return (SCHEME_V->code); |
3775 | s_return (SCHEME_V->code); |
3581 | |
3776 | |
… | |
… | |
3593 | case OP_IF1: /* if */ |
3788 | case OP_IF1: /* if */ |
3594 | if (is_true (SCHEME_V->value)) |
3789 | if (is_true (SCHEME_V->value)) |
3595 | SCHEME_V->code = car (SCHEME_V->code); |
3790 | SCHEME_V->code = car (SCHEME_V->code); |
3596 | else |
3791 | else |
3597 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
3792 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
|
|
3793 | |
3598 | s_goto (OP_EVAL); |
3794 | s_goto (OP_EVAL); |
3599 | |
3795 | |
3600 | case OP_LET0: /* let */ |
3796 | case OP_LET0: /* let */ |
3601 | SCHEME_V->args = NIL; |
3797 | SCHEME_V->args = NIL; |
3602 | SCHEME_V->value = SCHEME_V->code; |
3798 | SCHEME_V->value = SCHEME_V->code; |
… | |
… | |
3758 | } |
3954 | } |
3759 | else |
3955 | else |
3760 | { |
3956 | { |
3761 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3957 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3762 | s_return (NIL); |
3958 | s_return (NIL); |
3763 | else |
3959 | |
3764 | { |
|
|
3765 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3960 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3766 | SCHEME_V->code = caar (SCHEME_V->code); |
3961 | SCHEME_V->code = caar (SCHEME_V->code); |
3767 | s_goto (OP_EVAL); |
3962 | s_goto (OP_EVAL); |
3768 | } |
|
|
3769 | } |
3963 | } |
3770 | |
3964 | |
3771 | case OP_DELAY: /* delay */ |
3965 | case OP_DELAY: /* delay */ |
3772 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3966 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3773 | set_typeflag (x, T_PROMISE); |
3967 | set_typeflag (x, T_PROMISE); |
… | |
… | |
3784 | case OP_AND1: /* and */ |
3978 | case OP_AND1: /* and */ |
3785 | if (is_false (SCHEME_V->value)) |
3979 | if (is_false (SCHEME_V->value)) |
3786 | s_return (SCHEME_V->value); |
3980 | s_return (SCHEME_V->value); |
3787 | else if (SCHEME_V->code == NIL) |
3981 | else if (SCHEME_V->code == NIL) |
3788 | s_return (SCHEME_V->value); |
3982 | s_return (SCHEME_V->value); |
3789 | else |
3983 | |
3790 | { |
|
|
3791 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3984 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3792 | SCHEME_V->code = car (SCHEME_V->code); |
3985 | SCHEME_V->code = car (SCHEME_V->code); |
3793 | s_goto (OP_EVAL); |
3986 | s_goto (OP_EVAL); |
3794 | } |
|
|
3795 | |
3987 | |
3796 | case OP_OR0: /* or */ |
3988 | case OP_OR0: /* or */ |
3797 | if (SCHEME_V->code == NIL) |
3989 | if (SCHEME_V->code == NIL) |
3798 | s_return (S_F); |
3990 | s_return (S_F); |
3799 | |
3991 | |
… | |
… | |
3804 | case OP_OR1: /* or */ |
3996 | case OP_OR1: /* or */ |
3805 | if (is_true (SCHEME_V->value)) |
3997 | if (is_true (SCHEME_V->value)) |
3806 | s_return (SCHEME_V->value); |
3998 | s_return (SCHEME_V->value); |
3807 | else if (SCHEME_V->code == NIL) |
3999 | else if (SCHEME_V->code == NIL) |
3808 | s_return (SCHEME_V->value); |
4000 | s_return (SCHEME_V->value); |
3809 | else |
4001 | |
3810 | { |
|
|
3811 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
4002 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3812 | SCHEME_V->code = car (SCHEME_V->code); |
4003 | SCHEME_V->code = car (SCHEME_V->code); |
3813 | s_goto (OP_EVAL); |
4004 | s_goto (OP_EVAL); |
3814 | } |
|
|
3815 | |
4005 | |
3816 | case OP_C0STREAM: /* cons-stream */ |
4006 | case OP_C0STREAM: /* cons-stream */ |
3817 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
4007 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3818 | SCHEME_V->code = car (SCHEME_V->code); |
4008 | SCHEME_V->code = car (SCHEME_V->code); |
3819 | s_goto (OP_EVAL); |
4009 | s_goto (OP_EVAL); |
… | |
… | |
3884 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
4074 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
3885 | SCHEME_V->code = caar (x); |
4075 | SCHEME_V->code = caar (x); |
3886 | s_goto (OP_EVAL); |
4076 | s_goto (OP_EVAL); |
3887 | } |
4077 | } |
3888 | } |
4078 | } |
3889 | else |
4079 | |
3890 | s_return (NIL); |
4080 | s_return (NIL); |
3891 | |
4081 | |
3892 | case OP_CASE2: /* case */ |
4082 | case OP_CASE2: /* case */ |
3893 | if (is_true (SCHEME_V->value)) |
4083 | if (is_true (SCHEME_V->value)) |
3894 | s_goto (OP_BEGIN); |
4084 | s_goto (OP_BEGIN); |
3895 | else |
4085 | |
3896 | s_return (NIL); |
4086 | s_return (NIL); |
3897 | |
4087 | |
3898 | case OP_PAPPLY: /* apply */ |
4088 | case OP_PAPPLY: /* apply */ |
3899 | SCHEME_V->code = car (args); |
4089 | SCHEME_V->code = car (args); |
3900 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
4090 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
3901 | /*SCHEME_V->args = cadr(args); */ |
4091 | /*SCHEME_V->args = cadr(args); */ |
… | |
… | |
3912 | SCHEME_V->code = car (args); |
4102 | SCHEME_V->code = car (args); |
3913 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); |
4103 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); |
3914 | s_goto (OP_APPLY); |
4104 | s_goto (OP_APPLY); |
3915 | } |
4105 | } |
3916 | |
4106 | |
3917 | abort (); |
4107 | if (USE_ERROR_CHECKING) abort (); |
3918 | } |
4108 | } |
3919 | |
4109 | |
3920 | static int |
4110 | /* math, cxr */ |
|
|
4111 | ecb_hot static int |
3921 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4112 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
3922 | { |
4113 | { |
3923 | pointer args = SCHEME_V->args; |
4114 | pointer args = SCHEME_V->args; |
3924 | pointer x = car (args); |
4115 | pointer x = car (args); |
3925 | num v; |
4116 | num v; |
3926 | |
4117 | |
3927 | #if USE_MATH |
|
|
3928 | RVALUE dd; |
|
|
3929 | #endif |
|
|
3930 | |
|
|
3931 | switch (op) |
4118 | switch (op) |
3932 | { |
4119 | { |
3933 | #if USE_MATH |
4120 | #if USE_MATH |
3934 | case OP_INEX2EX: /* inexact->exact */ |
4121 | case OP_INEX2EX: /* inexact->exact */ |
3935 | if (num_is_integer (x)) |
4122 | if (!is_integer (x)) |
3936 | s_return (x); |
4123 | { |
3937 | else if (modf (rvalue_unchecked (x), &dd) == 0) |
4124 | RVALUE r = rvalue_unchecked (x); |
3938 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4125 | |
|
|
4126 | if (r == (RVALUE)(IVALUE)r) |
|
|
4127 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
3939 | else |
4128 | else |
3940 | Error_1 ("inexact->exact: not integral:", x); |
4129 | Error_1 ("inexact->exact: not integral:", x); |
|
|
4130 | } |
3941 | |
4131 | |
|
|
4132 | s_return (x); |
|
|
4133 | |
|
|
4134 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4135 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4136 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4137 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
|
|
4138 | |
|
|
4139 | case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
3942 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4140 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
3943 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
4141 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
|
|
4142 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
3944 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4143 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
3945 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4144 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
3946 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4145 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
3947 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4146 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
3948 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4147 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
3949 | |
4148 | |
3950 | case OP_ATAN: |
4149 | case OP_ATAN: |
|
|
4150 | s_return (mk_real (SCHEME_A_ |
3951 | if (cdr (args) == NIL) |
4151 | cdr (args) == NIL |
3952 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4152 | ? atan (rvalue (x)) |
3953 | else |
4153 | : atan2 (rvalue (x), rvalue (cadr (args))))); |
3954 | { |
|
|
3955 | pointer y = cadr (args); |
|
|
3956 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
|
|
3957 | } |
|
|
3958 | |
|
|
3959 | case OP_SQRT: |
|
|
3960 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
|
|
3961 | |
4154 | |
3962 | case OP_EXPT: |
4155 | case OP_EXPT: |
3963 | { |
4156 | { |
3964 | RVALUE result; |
4157 | RVALUE result; |
3965 | int real_result = 1; |
4158 | int real_result = 1; |
3966 | pointer y = cadr (args); |
4159 | pointer y = cadr (args); |
3967 | |
4160 | |
3968 | if (num_is_integer (x) && num_is_integer (y)) |
4161 | if (is_integer (x) && is_integer (y)) |
3969 | real_result = 0; |
4162 | real_result = 0; |
3970 | |
4163 | |
3971 | /* This 'if' is an R5RS compatibility fix. */ |
4164 | /* This 'if' is an R5RS compatibility fix. */ |
3972 | /* NOTE: Remove this 'if' fix for R6RS. */ |
4165 | /* NOTE: Remove this 'if' fix for R6RS. */ |
3973 | if (rvalue (x) == 0 && rvalue (y) < 0) |
4166 | if (rvalue (x) == 0 && rvalue (y) < 0) |
… | |
… | |
3979 | /* If the test fails, result is too big for integer. */ |
4172 | /* If the test fails, result is too big for integer. */ |
3980 | if (!real_result) |
4173 | if (!real_result) |
3981 | { |
4174 | { |
3982 | long result_as_long = result; |
4175 | long result_as_long = result; |
3983 | |
4176 | |
3984 | if (result != (RVALUE) result_as_long) |
4177 | if (result != result_as_long) |
3985 | real_result = 1; |
4178 | real_result = 1; |
3986 | } |
4179 | } |
3987 | |
4180 | |
3988 | if (real_result) |
4181 | if (real_result) |
3989 | s_return (mk_real (SCHEME_A_ result)); |
4182 | s_return (mk_real (SCHEME_A_ result)); |
3990 | else |
4183 | else |
3991 | s_return (mk_integer (SCHEME_A_ result)); |
4184 | s_return (mk_integer (SCHEME_A_ result)); |
3992 | } |
4185 | } |
3993 | |
|
|
3994 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
3995 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
3996 | |
|
|
3997 | case OP_TRUNCATE: |
|
|
3998 | { |
|
|
3999 | RVALUE rvalue_of_x; |
|
|
4000 | |
|
|
4001 | rvalue_of_x = rvalue (x); |
|
|
4002 | |
|
|
4003 | if (rvalue_of_x > 0) |
|
|
4004 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
|
|
4005 | else |
|
|
4006 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
|
|
4007 | } |
|
|
4008 | |
|
|
4009 | case OP_ROUND: |
|
|
4010 | if (num_is_integer (x)) |
|
|
4011 | s_return (x); |
|
|
4012 | |
|
|
4013 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
|
|
4014 | #endif |
4186 | #endif |
4015 | |
4187 | |
4016 | case OP_ADD: /* + */ |
4188 | case OP_ADD: /* + */ |
4017 | v = num_zero; |
4189 | v = num_zero; |
4018 | |
4190 | |
4019 | for (x = args; x != NIL; x = cdr (x)) |
4191 | for (x = args; x != NIL; x = cdr (x)) |
4020 | v = num_op ('+', v, nvalue (car (x))); |
4192 | v = num_op (NUM_ADD, v, nvalue (car (x))); |
4021 | |
4193 | |
4022 | s_return (mk_number (SCHEME_A_ v)); |
4194 | s_return (mk_number (SCHEME_A_ v)); |
4023 | |
4195 | |
4024 | case OP_MUL: /* * */ |
4196 | case OP_MUL: /* * */ |
4025 | v = num_one; |
4197 | v = num_one; |
4026 | |
4198 | |
4027 | for (x = args; x != NIL; x = cdr (x)) |
4199 | for (x = args; x != NIL; x = cdr (x)) |
4028 | v = num_op ('*', v, nvalue (car (x))); |
4200 | v = num_op (NUM_MUL, v, nvalue (car (x))); |
4029 | |
4201 | |
4030 | s_return (mk_number (SCHEME_A_ v)); |
4202 | s_return (mk_number (SCHEME_A_ v)); |
4031 | |
4203 | |
4032 | case OP_SUB: /* - */ |
4204 | case OP_SUB: /* - */ |
4033 | if (cdr (args) == NIL) |
4205 | if (cdr (args) == NIL) |
… | |
… | |
4040 | x = cdr (args); |
4212 | x = cdr (args); |
4041 | v = nvalue (car (args)); |
4213 | v = nvalue (car (args)); |
4042 | } |
4214 | } |
4043 | |
4215 | |
4044 | for (; x != NIL; x = cdr (x)) |
4216 | for (; x != NIL; x = cdr (x)) |
4045 | v = num_op ('-', v, nvalue (car (x))); |
4217 | v = num_op (NUM_SUB, v, nvalue (car (x))); |
4046 | |
4218 | |
4047 | s_return (mk_number (SCHEME_A_ v)); |
4219 | s_return (mk_number (SCHEME_A_ v)); |
4048 | |
4220 | |
4049 | case OP_DIV: /* / */ |
4221 | case OP_DIV: /* / */ |
4050 | if (cdr (args) == NIL) |
4222 | if (cdr (args) == NIL) |
… | |
… | |
4057 | x = cdr (args); |
4229 | x = cdr (args); |
4058 | v = nvalue (car (args)); |
4230 | v = nvalue (car (args)); |
4059 | } |
4231 | } |
4060 | |
4232 | |
4061 | for (; x != NIL; x = cdr (x)) |
4233 | for (; x != NIL; x = cdr (x)) |
4062 | { |
|
|
4063 | if (!is_zero_rvalue (rvalue (car (x)))) |
4234 | if (!is_zero_rvalue (rvalue (car (x)))) |
4064 | v = num_div (v, nvalue (car (x))); |
4235 | v = num_div (v, nvalue (car (x))); |
4065 | else |
4236 | else |
4066 | Error_0 ("/: division by zero"); |
4237 | Error_0 ("/: division by zero"); |
4067 | } |
|
|
4068 | |
4238 | |
4069 | s_return (mk_number (SCHEME_A_ v)); |
4239 | s_return (mk_number (SCHEME_A_ v)); |
4070 | |
4240 | |
4071 | case OP_INTDIV: /* quotient */ |
4241 | case OP_INTDIV: /* quotient */ |
4072 | if (cdr (args) == NIL) |
4242 | if (cdr (args) == NIL) |
… | |
… | |
4081 | } |
4251 | } |
4082 | |
4252 | |
4083 | for (; x != NIL; x = cdr (x)) |
4253 | for (; x != NIL; x = cdr (x)) |
4084 | { |
4254 | { |
4085 | if (ivalue (car (x)) != 0) |
4255 | if (ivalue (car (x)) != 0) |
4086 | v = num_op ('/', v, nvalue (car (x))); |
4256 | v = num_op (NUM_INTDIV, v, nvalue (car (x))); |
4087 | else |
4257 | else |
4088 | Error_0 ("quotient: division by zero"); |
4258 | Error_0 ("quotient: division by zero"); |
4089 | } |
4259 | } |
4090 | |
4260 | |
4091 | s_return (mk_number (SCHEME_A_ v)); |
4261 | s_return (mk_number (SCHEME_A_ v)); |
… | |
… | |
4108 | else |
4278 | else |
4109 | Error_0 ("modulo: division by zero"); |
4279 | Error_0 ("modulo: division by zero"); |
4110 | |
4280 | |
4111 | s_return (mk_number (SCHEME_A_ v)); |
4281 | s_return (mk_number (SCHEME_A_ v)); |
4112 | |
4282 | |
4113 | case OP_CAR: /* car */ |
4283 | /* the compiler will optimize this mess... */ |
4114 | s_return (caar (args)); |
4284 | case OP_CAR: op_car: s_return (car (x)); |
4115 | |
4285 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4116 | case OP_CDR: /* cdr */ |
4286 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4117 | s_return (cdar (args)); |
4287 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4288 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4289 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4290 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4291 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4292 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4293 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4294 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4295 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4296 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4297 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4298 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4299 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4300 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4301 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4302 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4303 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4304 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4305 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4306 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4307 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4308 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4309 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4310 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4311 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4312 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4313 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4118 | |
4314 | |
4119 | case OP_CONS: /* cons */ |
4315 | case OP_CONS: /* cons */ |
4120 | set_cdr (args, cadr (args)); |
4316 | set_cdr (args, cadr (args)); |
4121 | s_return (args); |
4317 | s_return (args); |
4122 | |
4318 | |
… | |
… | |
4137 | } |
4333 | } |
4138 | else |
4334 | else |
4139 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4335 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4140 | |
4336 | |
4141 | case OP_CHAR2INT: /* char->integer */ |
4337 | case OP_CHAR2INT: /* char->integer */ |
4142 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4338 | s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x))); |
4143 | |
4339 | |
4144 | case OP_INT2CHAR: /* integer->char */ |
4340 | case OP_INT2CHAR: /* integer->char */ |
4145 | s_return (mk_character (SCHEME_A_ ivalue (x))); |
4341 | s_return (mk_character (SCHEME_A_ ivalue_unchecked (x))); |
4146 | |
4342 | |
4147 | case OP_CHARUPCASE: |
4343 | case OP_CHARUPCASE: |
4148 | { |
4344 | { |
4149 | unsigned char c = ivalue (x); |
4345 | unsigned char c = ivalue_unchecked (x); |
4150 | c = toupper (c); |
4346 | c = toupper (c); |
4151 | s_return (mk_character (SCHEME_A_ c)); |
4347 | s_return (mk_character (SCHEME_A_ c)); |
4152 | } |
4348 | } |
4153 | |
4349 | |
4154 | case OP_CHARDNCASE: |
4350 | case OP_CHARDNCASE: |
4155 | { |
4351 | { |
4156 | unsigned char c = ivalue (x); |
4352 | unsigned char c = ivalue_unchecked (x); |
4157 | c = tolower (c); |
4353 | c = tolower (c); |
4158 | s_return (mk_character (SCHEME_A_ c)); |
4354 | s_return (mk_character (SCHEME_A_ c)); |
4159 | } |
4355 | } |
4160 | |
4356 | |
4161 | case OP_STR2SYM: /* string->symbol */ |
4357 | case OP_STR2SYM: /* string->symbol */ |
… | |
… | |
4238 | Error_1 ("atom->string: not an atom:", x); |
4434 | Error_1 ("atom->string: not an atom:", x); |
4239 | } |
4435 | } |
4240 | |
4436 | |
4241 | case OP_MKSTRING: /* make-string */ |
4437 | case OP_MKSTRING: /* make-string */ |
4242 | { |
4438 | { |
4243 | int fill = ' '; |
4439 | int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' '; |
4244 | int len; |
|
|
4245 | |
|
|
4246 | len = ivalue (x); |
4440 | int len = ivalue_unchecked (x); |
4247 | |
|
|
4248 | if (cdr (args) != NIL) |
|
|
4249 | fill = charvalue (cadr (args)); |
|
|
4250 | |
4441 | |
4251 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4442 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4252 | } |
4443 | } |
4253 | |
4444 | |
4254 | case OP_STRLEN: /* string-length */ |
4445 | case OP_STRLEN: /* string-length */ |
4255 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4446 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4256 | |
4447 | |
4257 | case OP_STRREF: /* string-ref */ |
4448 | case OP_STRREF: /* string-ref */ |
4258 | { |
4449 | { |
4259 | char *str; |
|
|
4260 | int index; |
|
|
4261 | |
|
|
4262 | str = strvalue (x); |
4450 | char *str = strvalue (x); |
4263 | |
|
|
4264 | index = ivalue (cadr (args)); |
4451 | int index = ivalue_unchecked (cadr (args)); |
4265 | |
4452 | |
4266 | if (index >= strlength (x)) |
4453 | if (index >= strlength (x)) |
4267 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4454 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4268 | |
4455 | |
4269 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4456 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4270 | } |
4457 | } |
4271 | |
4458 | |
4272 | case OP_STRSET: /* string-set! */ |
4459 | case OP_STRSET: /* string-set! */ |
4273 | { |
4460 | { |
4274 | char *str; |
4461 | char *str = strvalue (x); |
4275 | int index; |
4462 | int index = ivalue_unchecked (cadr (args)); |
4276 | int c; |
4463 | int c; |
4277 | |
4464 | |
4278 | if (is_immutable (x)) |
4465 | if (is_immutable (x)) |
4279 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4466 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4280 | |
|
|
4281 | str = strvalue (x); |
|
|
4282 | |
|
|
4283 | index = ivalue (cadr (args)); |
|
|
4284 | |
4467 | |
4285 | if (index >= strlength (x)) |
4468 | if (index >= strlength (x)) |
4286 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4469 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4287 | |
4470 | |
4288 | c = charvalue (caddr (args)); |
4471 | c = charvalue (caddr (args)); |
… | |
… | |
4309 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4492 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4310 | |
4493 | |
4311 | s_return (newstr); |
4494 | s_return (newstr); |
4312 | } |
4495 | } |
4313 | |
4496 | |
4314 | case OP_SUBSTR: /* substring */ |
4497 | case OP_STRING_COPY: /* substring/string-copy */ |
4315 | { |
4498 | { |
4316 | char *str; |
4499 | char *str = strvalue (x); |
4317 | int index0; |
4500 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4318 | int index1; |
4501 | int index1; |
4319 | int len; |
4502 | int len; |
4320 | |
4503 | |
4321 | str = strvalue (x); |
|
|
4322 | |
|
|
4323 | index0 = ivalue (cadr (args)); |
|
|
4324 | |
|
|
4325 | if (index0 > strlength (x)) |
4504 | if (index0 > strlength (x)) |
4326 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4505 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4327 | |
4506 | |
4328 | if (cddr (args) != NIL) |
4507 | if (cddr (args) != NIL) |
4329 | { |
4508 | { |
4330 | index1 = ivalue (caddr (args)); |
4509 | index1 = ivalue_unchecked (caddr (args)); |
4331 | |
4510 | |
4332 | if (index1 > strlength (x) || index1 < index0) |
4511 | if (index1 > strlength (x) || index1 < index0) |
4333 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4512 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4334 | } |
4513 | } |
4335 | else |
4514 | else |
4336 | index1 = strlength (x); |
4515 | index1 = strlength (x); |
4337 | |
4516 | |
4338 | len = index1 - index0; |
4517 | len = index1 - index0; |
4339 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4518 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4340 | memcpy (strvalue (x), str + index0, len); |
|
|
4341 | strvalue (x)[len] = 0; |
|
|
4342 | |
4519 | |
4343 | s_return (x); |
4520 | s_return (x); |
4344 | } |
4521 | } |
4345 | |
4522 | |
4346 | case OP_VECTOR: /* vector */ |
4523 | case OP_VECTOR: /* vector */ |
… | |
… | |
4358 | if (SCHEME_V->no_memory) |
4535 | if (SCHEME_V->no_memory) |
4359 | s_return (S_SINK); |
4536 | s_return (S_SINK); |
4360 | #endif |
4537 | #endif |
4361 | |
4538 | |
4362 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4539 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4363 | set_vector_elem (vec, i, car (x)); |
4540 | vector_set (vec, i, car (x)); |
4364 | |
4541 | |
4365 | s_return (vec); |
4542 | s_return (vec); |
4366 | } |
4543 | } |
4367 | |
4544 | |
4368 | case OP_MKVECTOR: /* make-vector */ |
4545 | case OP_MKVECTOR: /* make-vector */ |
4369 | { |
4546 | { |
4370 | pointer fill = NIL; |
4547 | pointer fill = NIL; |
4371 | int len; |
|
|
4372 | pointer vec; |
4548 | pointer vec; |
4373 | |
|
|
4374 | len = ivalue (x); |
4549 | int len = ivalue_unchecked (x); |
4375 | |
4550 | |
4376 | if (cdr (args) != NIL) |
4551 | if (cdr (args) != NIL) |
4377 | fill = cadr (args); |
4552 | fill = cadr (args); |
4378 | |
4553 | |
4379 | vec = mk_vector (SCHEME_A_ len); |
4554 | vec = mk_vector (SCHEME_A_ len); |
… | |
… | |
4382 | if (SCHEME_V->no_memory) |
4557 | if (SCHEME_V->no_memory) |
4383 | s_return (S_SINK); |
4558 | s_return (S_SINK); |
4384 | #endif |
4559 | #endif |
4385 | |
4560 | |
4386 | if (fill != NIL) |
4561 | if (fill != NIL) |
4387 | fill_vector (vec, fill); |
4562 | fill_vector (vec, 0, fill); |
4388 | |
4563 | |
4389 | s_return (vec); |
4564 | s_return (vec); |
4390 | } |
4565 | } |
4391 | |
4566 | |
4392 | case OP_VECLEN: /* vector-length */ |
4567 | case OP_VECLEN: /* vector-length */ |
4393 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4568 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4394 | |
4569 | |
|
|
4570 | case OP_VECRESIZE: |
|
|
4571 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4572 | s_return (x); |
|
|
4573 | |
4395 | case OP_VECREF: /* vector-ref */ |
4574 | case OP_VECREF: /* vector-ref */ |
4396 | { |
4575 | { |
4397 | int index; |
|
|
4398 | |
|
|
4399 | index = ivalue (cadr (args)); |
4576 | int index = ivalue_unchecked (cadr (args)); |
4400 | |
4577 | |
4401 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4578 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4402 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4579 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4403 | |
4580 | |
4404 | s_return (vector_elem (x, index)); |
4581 | s_return (vector_get (x, index)); |
4405 | } |
4582 | } |
4406 | |
4583 | |
4407 | case OP_VECSET: /* vector-set! */ |
4584 | case OP_VECSET: /* vector-set! */ |
4408 | { |
4585 | { |
4409 | int index; |
4586 | int index = ivalue_unchecked (cadr (args)); |
4410 | |
4587 | |
4411 | if (is_immutable (x)) |
4588 | if (is_immutable (x)) |
4412 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4589 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4413 | |
4590 | |
4414 | index = ivalue (cadr (args)); |
|
|
4415 | |
|
|
4416 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4591 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4417 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4592 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4418 | |
4593 | |
4419 | set_vector_elem (x, index, caddr (args)); |
4594 | vector_set (x, index, caddr (args)); |
4420 | s_return (x); |
4595 | s_return (x); |
4421 | } |
4596 | } |
4422 | } |
4597 | } |
4423 | |
4598 | |
4424 | abort (); |
4599 | if (USE_ERROR_CHECKING) abort (); |
4425 | } |
4600 | } |
4426 | |
4601 | |
4427 | INTERFACE int |
4602 | /* relational ops */ |
4428 | is_list (SCHEME_P_ pointer a) |
4603 | ecb_hot static int |
4429 | { |
|
|
4430 | return list_length (SCHEME_A_ a) >= 0; |
|
|
4431 | } |
|
|
4432 | |
|
|
4433 | /* Result is: |
|
|
4434 | proper list: length |
|
|
4435 | circular list: -1 |
|
|
4436 | not even a pair: -2 |
|
|
4437 | dotted list: -2 minus length before dot |
|
|
4438 | */ |
|
|
4439 | INTERFACE int |
|
|
4440 | list_length (SCHEME_P_ pointer a) |
|
|
4441 | { |
|
|
4442 | int i = 0; |
|
|
4443 | pointer slow, fast; |
|
|
4444 | |
|
|
4445 | slow = fast = a; |
|
|
4446 | |
|
|
4447 | while (1) |
|
|
4448 | { |
|
|
4449 | if (fast == NIL) |
|
|
4450 | return i; |
|
|
4451 | |
|
|
4452 | if (!is_pair (fast)) |
|
|
4453 | return -2 - i; |
|
|
4454 | |
|
|
4455 | fast = cdr (fast); |
|
|
4456 | ++i; |
|
|
4457 | |
|
|
4458 | if (fast == NIL) |
|
|
4459 | return i; |
|
|
4460 | |
|
|
4461 | if (!is_pair (fast)) |
|
|
4462 | return -2 - i; |
|
|
4463 | |
|
|
4464 | ++i; |
|
|
4465 | fast = cdr (fast); |
|
|
4466 | |
|
|
4467 | /* Safe because we would have already returned if `fast' |
|
|
4468 | encountered a non-pair. */ |
|
|
4469 | slow = cdr (slow); |
|
|
4470 | |
|
|
4471 | if (fast == slow) |
|
|
4472 | { |
|
|
4473 | /* the fast pointer has looped back around and caught up |
|
|
4474 | with the slow pointer, hence the structure is circular, |
|
|
4475 | not of finite length, and therefore not a list */ |
|
|
4476 | return -1; |
|
|
4477 | } |
|
|
4478 | } |
|
|
4479 | } |
|
|
4480 | |
|
|
4481 | static int |
|
|
4482 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4604 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4483 | { |
4605 | { |
4484 | pointer x = SCHEME_V->args; |
4606 | pointer x = SCHEME_V->args; |
4485 | |
4607 | |
4486 | for (;;) |
4608 | for (;;) |
… | |
… | |
4507 | } |
4629 | } |
4508 | |
4630 | |
4509 | s_return (S_T); |
4631 | s_return (S_T); |
4510 | } |
4632 | } |
4511 | |
4633 | |
4512 | static int |
4634 | /* predicates */ |
|
|
4635 | ecb_hot static int |
4513 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4636 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4514 | { |
4637 | { |
4515 | pointer args = SCHEME_V->args; |
4638 | pointer args = SCHEME_V->args; |
4516 | pointer a = car (args); |
4639 | pointer a = car (args); |
4517 | pointer d = cdr (args); |
4640 | pointer d = cdr (args); |
4518 | int r; |
4641 | int r; |
4519 | |
4642 | |
4520 | switch (op) |
4643 | switch (op) |
4521 | { |
4644 | { |
4522 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4645 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4523 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
4646 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break; |
4524 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4647 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4525 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4648 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4526 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
4649 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4650 | case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break; |
4527 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4651 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4528 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4652 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4529 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4653 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4530 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4654 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4531 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4655 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4532 | |
4656 | |
4533 | #if USE_CHAR_CLASSIFIERS |
4657 | #if USE_CHAR_CLASSIFIERS |
4534 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; |
4658 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4535 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; |
4659 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4536 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; |
4660 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4537 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; |
4661 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break; |
4538 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; |
4662 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break; |
4539 | #endif |
4663 | #endif |
4540 | |
4664 | |
4541 | #if USE_PORTS |
4665 | #if USE_PORTS |
4542 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4666 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4543 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
4667 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
… | |
… | |
4563 | } |
4687 | } |
4564 | |
4688 | |
4565 | s_retbool (r); |
4689 | s_retbool (r); |
4566 | } |
4690 | } |
4567 | |
4691 | |
4568 | static int |
4692 | /* promises, list ops, ports */ |
|
|
4693 | ecb_hot static int |
4569 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4694 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4570 | { |
4695 | { |
4571 | pointer args = SCHEME_V->args; |
4696 | pointer args = SCHEME_V->args; |
4572 | pointer a = car (args); |
4697 | pointer a = car (args); |
4573 | pointer x, y; |
4698 | pointer x, y; |
… | |
… | |
4586 | } |
4711 | } |
4587 | else |
4712 | else |
4588 | s_return (SCHEME_V->code); |
4713 | s_return (SCHEME_V->code); |
4589 | |
4714 | |
4590 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4715 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4591 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4716 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4592 | s_return (SCHEME_V->value); |
4717 | s_return (SCHEME_V->value); |
4593 | |
4718 | |
4594 | #if USE_PORTS |
4719 | #if USE_PORTS |
|
|
4720 | |
|
|
4721 | case OP_EOF_OBJECT: /* eof-object */ |
|
|
4722 | s_return (S_EOF); |
4595 | |
4723 | |
4596 | case OP_WRITE: /* write */ |
4724 | case OP_WRITE: /* write */ |
4597 | case OP_DISPLAY: /* display */ |
4725 | case OP_DISPLAY: /* display */ |
4598 | case OP_WRITE_CHAR: /* write-char */ |
4726 | case OP_WRITE_CHAR: /* write-char */ |
4599 | if (is_pair (cdr (SCHEME_V->args))) |
4727 | if (is_pair (cdr (SCHEME_V->args))) |
… | |
… | |
4613 | else |
4741 | else |
4614 | SCHEME_V->print_flag = 0; |
4742 | SCHEME_V->print_flag = 0; |
4615 | |
4743 | |
4616 | s_goto (OP_P0LIST); |
4744 | s_goto (OP_P0LIST); |
4617 | |
4745 | |
|
|
4746 | //TODO: move to scheme |
4618 | case OP_NEWLINE: /* newline */ |
4747 | case OP_NEWLINE: /* newline */ |
4619 | if (is_pair (args)) |
4748 | if (is_pair (args)) |
4620 | { |
4749 | { |
4621 | if (a != SCHEME_V->outport) |
4750 | if (a != SCHEME_V->outport) |
4622 | { |
4751 | { |
… | |
… | |
4624 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4753 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4625 | SCHEME_V->outport = a; |
4754 | SCHEME_V->outport = a; |
4626 | } |
4755 | } |
4627 | } |
4756 | } |
4628 | |
4757 | |
4629 | putstr (SCHEME_A_ "\n"); |
4758 | putcharacter (SCHEME_A_ '\n'); |
4630 | s_return (S_T); |
4759 | s_return (S_T); |
4631 | #endif |
4760 | #endif |
4632 | |
4761 | |
4633 | case OP_ERR0: /* error */ |
4762 | case OP_ERR0: /* error */ |
4634 | SCHEME_V->retcode = -1; |
4763 | SCHEME_V->retcode = -1; |
… | |
… | |
4643 | putstr (SCHEME_A_ strvalue (car (args))); |
4772 | putstr (SCHEME_A_ strvalue (car (args))); |
4644 | SCHEME_V->args = cdr (args); |
4773 | SCHEME_V->args = cdr (args); |
4645 | s_goto (OP_ERR1); |
4774 | s_goto (OP_ERR1); |
4646 | |
4775 | |
4647 | case OP_ERR1: /* error */ |
4776 | case OP_ERR1: /* error */ |
4648 | putstr (SCHEME_A_ " "); |
4777 | putcharacter (SCHEME_A_ ' '); |
4649 | |
4778 | |
4650 | if (args != NIL) |
4779 | if (args != NIL) |
4651 | { |
4780 | { |
4652 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4781 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4653 | SCHEME_V->args = a; |
4782 | SCHEME_V->args = a; |
4654 | SCHEME_V->print_flag = 1; |
4783 | SCHEME_V->print_flag = 1; |
4655 | s_goto (OP_P0LIST); |
4784 | s_goto (OP_P0LIST); |
4656 | } |
4785 | } |
4657 | else |
4786 | else |
4658 | { |
4787 | { |
4659 | putstr (SCHEME_A_ "\n"); |
4788 | putcharacter (SCHEME_A_ '\n'); |
4660 | |
4789 | |
4661 | if (SCHEME_V->interactive_repl) |
4790 | if (SCHEME_V->interactive_repl) |
4662 | s_goto (OP_T0LVL); |
4791 | s_goto (OP_T0LVL); |
4663 | else |
4792 | else |
4664 | return -1; |
4793 | return -1; |
… | |
… | |
4741 | SCHEME_V->gc_verbose = (a != S_F); |
4870 | SCHEME_V->gc_verbose = (a != S_F); |
4742 | s_retbool (was); |
4871 | s_retbool (was); |
4743 | } |
4872 | } |
4744 | |
4873 | |
4745 | case OP_NEWSEGMENT: /* new-segment */ |
4874 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4875 | #if 0 |
4746 | if (!is_pair (args) || !is_number (a)) |
4876 | if (!is_pair (args) || !is_number (a)) |
4747 | Error_0 ("new-segment: argument must be a number"); |
4877 | Error_0 ("new-segment: argument must be a number"); |
4748 | |
4878 | #endif |
4749 | alloc_cellseg (SCHEME_A_ (int)ivalue (a)); |
4879 | s_retbool (alloc_cellseg (SCHEME_A)); |
4750 | |
|
|
4751 | s_return (S_T); |
|
|
4752 | |
4880 | |
4753 | case OP_OBLIST: /* oblist */ |
4881 | case OP_OBLIST: /* oblist */ |
4754 | s_return (oblist_all_symbols (SCHEME_A)); |
4882 | s_return (oblist_all_symbols (SCHEME_A)); |
4755 | |
4883 | |
4756 | #if USE_PORTS |
4884 | #if USE_PORTS |
… | |
… | |
4783 | break; |
4911 | break; |
4784 | } |
4912 | } |
4785 | |
4913 | |
4786 | p = port_from_filename (SCHEME_A_ strvalue (a), prop); |
4914 | p = port_from_filename (SCHEME_A_ strvalue (a), prop); |
4787 | |
4915 | |
4788 | if (p == NIL) |
4916 | s_return (p == NIL ? S_F : p); |
4789 | s_return (S_F); |
|
|
4790 | |
|
|
4791 | s_return (p); |
|
|
4792 | } |
4917 | } |
4793 | |
4918 | |
4794 | # if USE_STRING_PORTS |
4919 | # if USE_STRING_PORTS |
4795 | |
4920 | |
4796 | case OP_OPEN_INSTRING: /* open-input-string */ |
4921 | case OP_OPEN_INSTRING: /* open-input-string */ |
… | |
… | |
4811 | } |
4936 | } |
4812 | |
4937 | |
4813 | p = port_from_string (SCHEME_A_ strvalue (a), |
4938 | p = port_from_string (SCHEME_A_ strvalue (a), |
4814 | strvalue (a) + strlength (a), prop); |
4939 | strvalue (a) + strlength (a), prop); |
4815 | |
4940 | |
4816 | if (p == NIL) |
4941 | s_return (p == NIL ? S_F : p); |
4817 | s_return (S_F); |
|
|
4818 | |
|
|
4819 | s_return (p); |
|
|
4820 | } |
4942 | } |
4821 | |
4943 | |
4822 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
4944 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
4823 | { |
4945 | { |
4824 | pointer p; |
4946 | pointer p; |
4825 | |
4947 | |
4826 | if (a == NIL) |
4948 | if (a == NIL) |
4827 | { |
|
|
4828 | p = port_from_scratch (SCHEME_A); |
4949 | p = port_from_scratch (SCHEME_A); |
4829 | |
|
|
4830 | if (p == NIL) |
|
|
4831 | s_return (S_F); |
|
|
4832 | } |
|
|
4833 | else |
4950 | else |
4834 | { |
|
|
4835 | p = port_from_string (SCHEME_A_ strvalue (a), |
4951 | p = port_from_string (SCHEME_A_ strvalue (a), |
4836 | strvalue (a) + strlength (a), port_output); |
4952 | strvalue (a) + strlength (a), port_output); |
4837 | |
4953 | |
4838 | if (p == NIL) |
4954 | s_return (p == NIL ? S_F : p); |
4839 | s_return (S_F); |
|
|
4840 | } |
|
|
4841 | |
|
|
4842 | s_return (p); |
|
|
4843 | } |
4955 | } |
4844 | |
4956 | |
4845 | case OP_GET_OUTSTRING: /* get-output-string */ |
4957 | case OP_GET_OUTSTRING: /* get-output-string */ |
4846 | { |
4958 | { |
4847 | port *p; |
4959 | port *p = port (a); |
4848 | |
4960 | |
4849 | if ((p = a->object.port)->kind & port_string) |
4961 | if (p->kind & port_string) |
4850 | { |
4962 | { |
4851 | off_t size; |
4963 | off_t size; |
4852 | char *str; |
4964 | char *str; |
4853 | |
4965 | |
4854 | size = p->rep.string.curr - p->rep.string.start + 1; |
4966 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4886 | case OP_CURR_ENV: /* current-environment */ |
4998 | case OP_CURR_ENV: /* current-environment */ |
4887 | s_return (SCHEME_V->envir); |
4999 | s_return (SCHEME_V->envir); |
4888 | |
5000 | |
4889 | } |
5001 | } |
4890 | |
5002 | |
4891 | abort (); |
5003 | if (USE_ERROR_CHECKING) abort (); |
4892 | } |
5004 | } |
4893 | |
5005 | |
4894 | static int |
5006 | /* reading */ |
|
|
5007 | ecb_cold static int |
4895 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
5008 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4896 | { |
5009 | { |
4897 | pointer args = SCHEME_V->args; |
5010 | pointer args = SCHEME_V->args; |
4898 | pointer x; |
5011 | pointer x; |
4899 | |
5012 | |
… | |
… | |
4959 | int res; |
5072 | int res; |
4960 | |
5073 | |
4961 | if (is_pair (args)) |
5074 | if (is_pair (args)) |
4962 | p = car (args); |
5075 | p = car (args); |
4963 | |
5076 | |
4964 | res = p->object.port->kind & port_string; |
5077 | res = port (p)->kind & port_string; |
4965 | |
5078 | |
4966 | s_retbool (res); |
5079 | s_retbool (res); |
4967 | } |
5080 | } |
4968 | |
5081 | |
4969 | case OP_SET_INPORT: /* set-input-port */ |
5082 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
5028 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5141 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5029 | SCHEME_V->tok = token (SCHEME_A); |
5142 | SCHEME_V->tok = token (SCHEME_A); |
5030 | s_goto (OP_RDSEXPR); |
5143 | s_goto (OP_RDSEXPR); |
5031 | |
5144 | |
5032 | case TOK_ATOM: |
5145 | case TOK_ATOM: |
5033 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); |
5146 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
|
|
5147 | |
|
|
5148 | case TOK_DOTATOM: |
|
|
5149 | SCHEME_V->strbuff[0] = '.'; |
|
|
5150 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
|
|
5151 | |
|
|
5152 | case TOK_STRATOM: |
|
|
5153 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5154 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5155 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
5034 | |
5156 | |
5035 | case TOK_DQUOTE: |
5157 | case TOK_DQUOTE: |
5036 | x = readstrexp (SCHEME_A); |
5158 | x = readstrexp (SCHEME_A_ '"'); |
5037 | |
5159 | |
5038 | if (x == S_F) |
5160 | if (x == S_F) |
5039 | Error_0 ("Error reading string"); |
5161 | Error_0 ("Error reading string"); |
5040 | |
5162 | |
5041 | setimmutable (x); |
5163 | setimmutable (x); |
… | |
… | |
5053 | s_goto (OP_EVAL); |
5175 | s_goto (OP_EVAL); |
5054 | } |
5176 | } |
5055 | } |
5177 | } |
5056 | |
5178 | |
5057 | case TOK_SHARP_CONST: |
5179 | case TOK_SHARP_CONST: |
5058 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) |
5180 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
5059 | Error_0 ("undefined sharp expression"); |
5181 | Error_0 ("undefined sharp expression"); |
5060 | else |
5182 | else |
5061 | s_return (x); |
5183 | s_return (x); |
5062 | |
5184 | |
5063 | default: |
5185 | default: |
… | |
… | |
5159 | pointer b = cdr (args); |
5281 | pointer b = cdr (args); |
5160 | int ok_abbr = ok_abbrev (b); |
5282 | int ok_abbr = ok_abbrev (b); |
5161 | SCHEME_V->args = car (b); |
5283 | SCHEME_V->args = car (b); |
5162 | |
5284 | |
5163 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5285 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5164 | putstr (SCHEME_A_ "'"); |
5286 | putcharacter (SCHEME_A_ '\''); |
5165 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5287 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5166 | putstr (SCHEME_A_ "`"); |
5288 | putcharacter (SCHEME_A_ '`'); |
5167 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5289 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5168 | putstr (SCHEME_A_ ","); |
5290 | putcharacter (SCHEME_A_ ','); |
5169 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5291 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5170 | putstr (SCHEME_A_ ",@"); |
5292 | putstr (SCHEME_A_ ",@"); |
5171 | else |
5293 | else |
5172 | { |
5294 | { |
5173 | putstr (SCHEME_A_ "("); |
5295 | putcharacter (SCHEME_A_ '('); |
5174 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5296 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5175 | SCHEME_V->args = a; |
5297 | SCHEME_V->args = a; |
5176 | } |
5298 | } |
5177 | |
5299 | |
5178 | s_goto (OP_P0LIST); |
5300 | s_goto (OP_P0LIST); |
… | |
… | |
5180 | |
5302 | |
5181 | case OP_P1LIST: |
5303 | case OP_P1LIST: |
5182 | if (is_pair (args)) |
5304 | if (is_pair (args)) |
5183 | { |
5305 | { |
5184 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5306 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5185 | putstr (SCHEME_A_ " "); |
5307 | putcharacter (SCHEME_A_ ' '); |
5186 | SCHEME_V->args = car (args); |
5308 | SCHEME_V->args = car (args); |
5187 | s_goto (OP_P0LIST); |
5309 | s_goto (OP_P0LIST); |
5188 | } |
5310 | } |
5189 | else if (is_vector (args)) |
5311 | else if (is_vector (args)) |
5190 | { |
5312 | { |
… | |
… | |
5198 | { |
5320 | { |
5199 | putstr (SCHEME_A_ " . "); |
5321 | putstr (SCHEME_A_ " . "); |
5200 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5322 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5201 | } |
5323 | } |
5202 | |
5324 | |
5203 | putstr (SCHEME_A_ ")"); |
5325 | putcharacter (SCHEME_A_ ')'); |
5204 | s_return (S_T); |
5326 | s_return (S_T); |
5205 | } |
5327 | } |
5206 | |
5328 | |
5207 | case OP_PVECFROM: |
5329 | case OP_PVECFROM: |
5208 | { |
5330 | { |
… | |
… | |
5210 | pointer vec = car (args); |
5332 | pointer vec = car (args); |
5211 | int len = veclength (vec); |
5333 | int len = veclength (vec); |
5212 | |
5334 | |
5213 | if (i == len) |
5335 | if (i == len) |
5214 | { |
5336 | { |
5215 | putstr (SCHEME_A_ ")"); |
5337 | putcharacter (SCHEME_A_ ')'); |
5216 | s_return (S_T); |
5338 | s_return (S_T); |
5217 | } |
5339 | } |
5218 | else |
5340 | else |
5219 | { |
5341 | { |
5220 | pointer elem = vector_elem (vec, i); |
5342 | pointer elem = vector_get (vec, i); |
5221 | |
5343 | |
5222 | ivalue_unchecked (cdr (args)) = i + 1; |
5344 | ivalue_unchecked (cdr (args)) = i + 1; |
5223 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5345 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5224 | SCHEME_V->args = elem; |
5346 | SCHEME_V->args = elem; |
5225 | |
5347 | |
5226 | if (i > 0) |
5348 | if (i > 0) |
5227 | putstr (SCHEME_A_ " "); |
5349 | putcharacter (SCHEME_A_ ' '); |
5228 | |
5350 | |
5229 | s_goto (OP_P0LIST); |
5351 | s_goto (OP_P0LIST); |
5230 | } |
5352 | } |
5231 | } |
5353 | } |
5232 | } |
5354 | } |
5233 | |
5355 | |
5234 | abort (); |
5356 | if (USE_ERROR_CHECKING) abort (); |
5235 | } |
5357 | } |
5236 | |
5358 | |
5237 | static int |
5359 | /* list ops */ |
|
|
5360 | ecb_hot static int |
5238 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5361 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5239 | { |
5362 | { |
5240 | pointer args = SCHEME_V->args; |
5363 | pointer args = SCHEME_V->args; |
5241 | pointer a = car (args); |
5364 | pointer a = car (args); |
5242 | pointer x, y; |
5365 | pointer x, y; |
… | |
… | |
5265 | break; |
5388 | break; |
5266 | } |
5389 | } |
5267 | |
5390 | |
5268 | if (is_pair (y)) |
5391 | if (is_pair (y)) |
5269 | s_return (car (y)); |
5392 | s_return (car (y)); |
5270 | else |
5393 | |
5271 | s_return (S_F); |
5394 | s_return (S_F); |
5272 | |
|
|
5273 | |
5395 | |
5274 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5396 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5275 | SCHEME_V->args = a; |
5397 | SCHEME_V->args = a; |
5276 | |
5398 | |
5277 | if (SCHEME_V->args == NIL) |
5399 | if (SCHEME_V->args == NIL) |
5278 | s_return (S_F); |
5400 | s_return (S_F); |
5279 | else if (is_closure (SCHEME_V->args)) |
5401 | else if (is_closure (SCHEME_V->args) || is_macro (SCHEME_V->args)) |
5280 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
5402 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
5281 | else if (is_macro (SCHEME_V->args)) |
5403 | |
5282 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
|
|
5283 | else |
|
|
5284 | s_return (S_F); |
5404 | s_return (S_F); |
5285 | |
5405 | |
5286 | case OP_CLOSUREP: /* closure? */ |
5406 | case OP_CLOSUREP: /* closure? */ |
5287 | /* |
5407 | /* |
5288 | * Note, macro object is also a closure. |
5408 | * Note, macro object is also a closure. |
5289 | * Therefore, (closure? <#MACRO>) ==> #t |
5409 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5410 | * (schmorp) well, obviously not, fix? TODO |
5290 | */ |
5411 | */ |
5291 | s_retbool (is_closure (a)); |
5412 | s_retbool (is_closure (a)); |
5292 | |
5413 | |
5293 | case OP_MACROP: /* macro? */ |
5414 | case OP_MACROP: /* macro? */ |
5294 | s_retbool (is_macro (a)); |
5415 | s_retbool (is_macro (a)); |
5295 | } |
5416 | } |
5296 | |
5417 | |
5297 | abort (); |
5418 | if (USE_ERROR_CHECKING) abort (); |
5298 | } |
5419 | } |
5299 | |
5420 | |
5300 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5421 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5301 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5422 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5302 | |
5423 | |
5303 | typedef int (*test_predicate)(pointer); |
5424 | typedef int (*test_predicate)(pointer); |
5304 | static int |
5425 | |
|
|
5426 | ecb_hot static int |
5305 | is_any (pointer p) |
5427 | tst_any (pointer p) |
5306 | { |
5428 | { |
5307 | return 1; |
5429 | return 1; |
5308 | } |
5430 | } |
5309 | |
5431 | |
5310 | static int |
5432 | ecb_hot static int |
5311 | is_nonneg (pointer p) |
5433 | tst_inonneg (pointer p) |
5312 | { |
5434 | { |
5313 | return ivalue (p) >= 0 && is_integer (p); |
5435 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5314 | } |
5436 | } |
5315 | |
5437 | |
5316 | static int |
5438 | ecb_hot static int |
5317 | tst_is_list (pointer p) |
5439 | tst_is_list (SCHEME_P_ pointer p) |
5318 | { |
5440 | { |
5319 | return p == NIL || is_pair (p); |
5441 | return p == NIL || is_pair (p); |
5320 | } |
5442 | } |
5321 | |
5443 | |
5322 | /* Correspond carefully with following defines! */ |
5444 | /* Correspond carefully with following defines! */ |
5323 | static struct |
5445 | static struct |
5324 | { |
5446 | { |
5325 | test_predicate fct; |
5447 | test_predicate fct; |
5326 | const char *kind; |
5448 | const char *kind; |
5327 | } tests[] = |
5449 | } tests[] = { |
5328 | { |
|
|
5329 | { is_any, 0 }, |
5450 | { tst_any , 0 }, |
5330 | { is_string, "string" }, |
5451 | { is_string , "string" }, |
5331 | { is_symbol, "symbol" }, |
5452 | { is_symbol , "symbol" }, |
5332 | { is_port, "port" }, |
5453 | { is_port , "port" }, |
5333 | { is_inport, "input port" }, |
5454 | { is_inport , "input port" }, |
5334 | { is_outport, "output port" }, |
5455 | { is_outport , "output port" }, |
5335 | { is_environment, "environment" }, |
5456 | { is_environment, "environment" }, |
5336 | { is_pair, "pair" }, |
5457 | { is_pair , "pair" }, |
5337 | { tst_is_list, "pair or '()" }, |
5458 | { 0 , "pair or '()" }, |
5338 | { is_character, "character" }, |
5459 | { is_character , "character" }, |
5339 | { is_vector, "vector" }, |
5460 | { is_vector , "vector" }, |
5340 | { is_number, "number" }, |
5461 | { is_number , "number" }, |
5341 | { is_integer, "integer" }, |
5462 | { is_integer , "integer" }, |
5342 | { is_nonneg, "non-negative integer" } |
5463 | { tst_inonneg , "non-negative integer" } |
5343 | }; |
5464 | }; |
5344 | |
5465 | |
5345 | #define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ |
5466 | #define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ |
5346 | #define TST_ANY "\001" |
5467 | #define TST_ANY "\001" |
5347 | #define TST_STRING "\002" |
5468 | #define TST_STRING "\002" |
… | |
… | |
5365 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5486 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5366 | #include "opdefines.h" |
5487 | #include "opdefines.h" |
5367 | #undef OP_DEF |
5488 | #undef OP_DEF |
5368 | ; |
5489 | ; |
5369 | |
5490 | |
5370 | static const char * |
5491 | ecb_cold static const char * |
5371 | opname (int idx) |
5492 | opname (int idx) |
5372 | { |
5493 | { |
5373 | const char *name = opnames; |
5494 | const char *name = opnames; |
5374 | |
5495 | |
5375 | /* should do this at compile time, but would require external program, right? */ |
5496 | /* should do this at compile time, but would require external program, right? */ |
… | |
… | |
5377 | name += strlen (name) + 1; |
5498 | name += strlen (name) + 1; |
5378 | |
5499 | |
5379 | return *name ? name : "ILLEGAL"; |
5500 | return *name ? name : "ILLEGAL"; |
5380 | } |
5501 | } |
5381 | |
5502 | |
5382 | static const char * |
5503 | ecb_cold static const char * |
5383 | procname (pointer x) |
5504 | procname (pointer x) |
5384 | { |
5505 | { |
5385 | return opname (procnum (x)); |
5506 | return opname (procnum (x)); |
5386 | } |
5507 | } |
5387 | |
5508 | |
5388 | typedef struct |
5509 | typedef struct |
5389 | { |
5510 | { |
5390 | uint8_t func; |
5511 | uint8_t func; |
5391 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
5512 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
5392 | uint8_t builtin; |
5513 | uint8_t builtin; |
|
|
5514 | #if USE_ERROR_CHECKING |
5393 | uint8_t min_arity; |
5515 | uint8_t min_arity; |
5394 | uint8_t max_arity; |
5516 | uint8_t max_arity; |
5395 | char arg_tests_encoding[3]; |
5517 | char arg_tests_encoding[3]; |
|
|
5518 | #endif |
5396 | } op_code_info; |
5519 | } op_code_info; |
5397 | |
5520 | |
5398 | static const op_code_info dispatch_table[] = { |
5521 | static const op_code_info dispatch_table[] = { |
|
|
5522 | #if USE_ERROR_CHECKING |
5399 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, |
5523 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, |
|
|
5524 | #else |
|
|
5525 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 }, |
|
|
5526 | #endif |
5400 | #include "opdefines.h" |
5527 | #include "opdefines.h" |
5401 | #undef OP_DEF |
5528 | #undef OP_DEF |
5402 | {0} |
5529 | {0} |
5403 | }; |
5530 | }; |
5404 | |
5531 | |
5405 | /* kernel of this interpreter */ |
5532 | /* kernel of this interpreter */ |
5406 | static void |
5533 | ecb_hot static void |
5407 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5534 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5408 | { |
5535 | { |
5409 | SCHEME_V->op = op; |
5536 | SCHEME_V->op = op; |
5410 | |
5537 | |
5411 | for (;;) |
5538 | for (;;) |
… | |
… | |
5446 | { |
5573 | { |
5447 | pointer arg = car (arglist); |
5574 | pointer arg = car (arglist); |
5448 | |
5575 | |
5449 | j = t[0]; |
5576 | j = t[0]; |
5450 | |
5577 | |
|
|
5578 | /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */ |
|
|
5579 | if (j == TST_LIST[0]) |
|
|
5580 | { |
|
|
5581 | if (!tst_is_list (SCHEME_A_ arg)) |
|
|
5582 | break; |
|
|
5583 | } |
|
|
5584 | else |
|
|
5585 | { |
5451 | if (!tests[j - 1].fct (arg)) |
5586 | if (!tests[j - 1].fct (arg)) |
5452 | break; |
5587 | break; |
|
|
5588 | } |
5453 | |
5589 | |
5454 | if (t[1]) /* last test is replicated as necessary */ |
5590 | if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */ |
5455 | t++; |
5591 | t++; |
5456 | |
5592 | |
5457 | arglist = cdr (arglist); |
5593 | arglist = cdr (arglist); |
5458 | i++; |
5594 | i++; |
5459 | } |
5595 | } |
… | |
… | |
5485 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5621 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5486 | return; |
5622 | return; |
5487 | |
5623 | |
5488 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5624 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5489 | { |
5625 | { |
5490 | xwrstr ("No memory!\n"); |
5626 | putstr (SCHEME_A_ "No memory!\n"); |
5491 | return; |
5627 | return; |
5492 | } |
5628 | } |
5493 | } |
5629 | } |
5494 | } |
5630 | } |
5495 | |
5631 | |
5496 | /* ========== Initialization of internal keywords ========== */ |
5632 | /* ========== Initialization of internal keywords ========== */ |
5497 | |
5633 | |
5498 | static void |
5634 | ecb_cold static void |
5499 | assign_syntax (SCHEME_P_ const char *name) |
5635 | assign_syntax (SCHEME_P_ const char *name) |
5500 | { |
5636 | { |
5501 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5637 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5502 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5638 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5503 | } |
5639 | } |
5504 | |
5640 | |
5505 | static void |
5641 | ecb_cold static void |
5506 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5642 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5507 | { |
5643 | { |
5508 | pointer x = mk_symbol (SCHEME_A_ name); |
5644 | pointer x = mk_symbol (SCHEME_A_ name); |
5509 | pointer y = mk_proc (SCHEME_A_ op); |
5645 | pointer y = mk_proc (SCHEME_A_ op); |
5510 | new_slot_in_env (SCHEME_A_ x, y); |
5646 | new_slot_in_env (SCHEME_A_ x, y); |
… | |
… | |
5514 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5650 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5515 | { |
5651 | { |
5516 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5652 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5517 | set_typeflag (y, (T_PROC | T_ATOM)); |
5653 | set_typeflag (y, (T_PROC | T_ATOM)); |
5518 | ivalue_unchecked (y) = op; |
5654 | ivalue_unchecked (y) = op; |
5519 | set_num_integer (y); |
|
|
5520 | return y; |
5655 | return y; |
5521 | } |
5656 | } |
5522 | |
5657 | |
5523 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5658 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5524 | static int |
5659 | ecb_hot static int |
5525 | syntaxnum (pointer p) |
5660 | syntaxnum (pointer p) |
5526 | { |
5661 | { |
5527 | const char *s = strvalue (car (p)); |
5662 | const char *s = strvalue (p); |
5528 | |
5663 | |
5529 | switch (strlength (car (p))) |
5664 | switch (strlength (p)) |
5530 | { |
5665 | { |
5531 | case 2: |
5666 | case 2: |
5532 | if (s[0] == 'i') |
5667 | if (s[0] == 'i') |
5533 | return OP_IF0; /* if */ |
5668 | return OP_IF0; /* if */ |
5534 | else |
5669 | else |
… | |
… | |
5589 | return OP_C0STREAM; /* cons-stream */ |
5724 | return OP_C0STREAM; /* cons-stream */ |
5590 | } |
5725 | } |
5591 | } |
5726 | } |
5592 | |
5727 | |
5593 | #if USE_MULTIPLICITY |
5728 | #if USE_MULTIPLICITY |
5594 | scheme * |
5729 | ecb_cold scheme * |
5595 | scheme_init_new () |
5730 | scheme_init_new () |
5596 | { |
5731 | { |
5597 | scheme *sc = malloc (sizeof (scheme)); |
5732 | scheme *sc = malloc (sizeof (scheme)); |
5598 | |
5733 | |
5599 | if (!scheme_init (SCHEME_A)) |
5734 | if (!scheme_init (SCHEME_A)) |
… | |
… | |
5604 | else |
5739 | else |
5605 | return sc; |
5740 | return sc; |
5606 | } |
5741 | } |
5607 | #endif |
5742 | #endif |
5608 | |
5743 | |
5609 | int |
5744 | ecb_cold int |
5610 | scheme_init (SCHEME_P) |
5745 | scheme_init (SCHEME_P) |
5611 | { |
5746 | { |
5612 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5747 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5613 | pointer x; |
5748 | pointer x; |
|
|
5749 | |
|
|
5750 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
5751 | * that memset 0 will also set pointers to 0, but memset does |
|
|
5752 | * of course not guarantee that. screw such systems. |
|
|
5753 | */ |
|
|
5754 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5614 | |
5755 | |
5615 | num_set_fixnum (num_zero, 1); |
5756 | num_set_fixnum (num_zero, 1); |
5616 | num_set_ivalue (num_zero, 0); |
5757 | num_set_ivalue (num_zero, 0); |
5617 | num_set_fixnum (num_one, 1); |
5758 | num_set_fixnum (num_one, 1); |
5618 | num_set_ivalue (num_one, 1); |
5759 | num_set_ivalue (num_one, 1); |
… | |
… | |
5630 | SCHEME_V->save_inport = NIL; |
5771 | SCHEME_V->save_inport = NIL; |
5631 | SCHEME_V->loadport = NIL; |
5772 | SCHEME_V->loadport = NIL; |
5632 | SCHEME_V->nesting = 0; |
5773 | SCHEME_V->nesting = 0; |
5633 | SCHEME_V->interactive_repl = 0; |
5774 | SCHEME_V->interactive_repl = 0; |
5634 | |
5775 | |
5635 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5776 | if (!alloc_cellseg (SCHEME_A)) |
5636 | { |
5777 | { |
5637 | #if USE_ERROR_CHECKING |
5778 | #if USE_ERROR_CHECKING |
5638 | SCHEME_V->no_memory = 1; |
5779 | SCHEME_V->no_memory = 1; |
5639 | return 0; |
5780 | return 0; |
5640 | #endif |
5781 | #endif |
5641 | } |
5782 | } |
5642 | |
5783 | |
5643 | SCHEME_V->gc_verbose = 0; |
5784 | SCHEME_V->gc_verbose = 0; |
5644 | dump_stack_initialize (SCHEME_A); |
5785 | dump_stack_initialize (SCHEME_A); |
5645 | SCHEME_V->code = NIL; |
5786 | SCHEME_V->code = NIL; |
5646 | SCHEME_V->args = NIL; |
5787 | SCHEME_V->args = NIL; |
5647 | SCHEME_V->envir = NIL; |
5788 | SCHEME_V->envir = NIL; |
|
|
5789 | SCHEME_V->value = NIL; |
5648 | SCHEME_V->tracing = 0; |
5790 | SCHEME_V->tracing = 0; |
5649 | |
5791 | |
5650 | /* init NIL */ |
5792 | /* init NIL */ |
5651 | set_typeflag (NIL, T_ATOM | T_MARK); |
5793 | set_typeflag (NIL, T_ATOM | T_MARK); |
5652 | set_car (NIL, NIL); |
5794 | set_car (NIL, NIL); |
… | |
… | |
5708 | |
5850 | |
5709 | return !SCHEME_V->no_memory; |
5851 | return !SCHEME_V->no_memory; |
5710 | } |
5852 | } |
5711 | |
5853 | |
5712 | #if USE_PORTS |
5854 | #if USE_PORTS |
5713 | void |
5855 | ecb_cold void |
5714 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5856 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5715 | { |
5857 | { |
5716 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5858 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5717 | } |
5859 | } |
5718 | |
5860 | |
5719 | void |
5861 | ecb_cold void |
5720 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5862 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5721 | { |
5863 | { |
5722 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5864 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5723 | } |
5865 | } |
5724 | |
5866 | |
5725 | void |
5867 | ecb_cold void |
5726 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5868 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5727 | { |
5869 | { |
5728 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5870 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5729 | } |
5871 | } |
5730 | |
5872 | |
5731 | void |
5873 | ecb_cold void |
5732 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5874 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5733 | { |
5875 | { |
5734 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5876 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5735 | } |
5877 | } |
5736 | #endif |
5878 | #endif |
5737 | |
5879 | |
5738 | void |
5880 | ecb_cold void |
5739 | scheme_set_external_data (SCHEME_P_ void *p) |
5881 | scheme_set_external_data (SCHEME_P_ void *p) |
5740 | { |
5882 | { |
5741 | SCHEME_V->ext_data = p; |
5883 | SCHEME_V->ext_data = p; |
5742 | } |
5884 | } |
5743 | |
5885 | |
5744 | void |
5886 | ecb_cold void |
5745 | scheme_deinit (SCHEME_P) |
5887 | scheme_deinit (SCHEME_P) |
5746 | { |
5888 | { |
5747 | int i; |
5889 | int i; |
5748 | |
5890 | |
5749 | #if SHOW_ERROR_LINE |
5891 | #if SHOW_ERROR_LINE |
… | |
… | |
5775 | SCHEME_V->loadport = NIL; |
5917 | SCHEME_V->loadport = NIL; |
5776 | SCHEME_V->gc_verbose = 0; |
5918 | SCHEME_V->gc_verbose = 0; |
5777 | gc (SCHEME_A_ NIL, NIL); |
5919 | gc (SCHEME_A_ NIL, NIL); |
5778 | |
5920 | |
5779 | for (i = 0; i <= SCHEME_V->last_cell_seg; i++) |
5921 | for (i = 0; i <= SCHEME_V->last_cell_seg; i++) |
5780 | free (SCHEME_V->alloc_seg[i]); |
5922 | free (SCHEME_V->cell_seg[i]); |
5781 | |
5923 | |
5782 | #if SHOW_ERROR_LINE |
5924 | #if SHOW_ERROR_LINE |
5783 | for (i = 0; i <= SCHEME_V->file_i; i++) |
5925 | for (i = 0; i <= SCHEME_V->file_i; i++) |
5784 | { |
|
|
5785 | if (SCHEME_V->load_stack[i].kind & port_file) |
5926 | if (SCHEME_V->load_stack[i].kind & port_file) |
5786 | { |
5927 | { |
5787 | fname = SCHEME_V->load_stack[i].rep.stdio.filename; |
5928 | fname = SCHEME_V->load_stack[i].rep.stdio.filename; |
5788 | |
5929 | |
5789 | if (fname) |
5930 | if (fname) |
5790 | free (fname); |
5931 | free (fname); |
5791 | } |
5932 | } |
5792 | } |
|
|
5793 | #endif |
5933 | #endif |
5794 | } |
5934 | } |
5795 | |
5935 | |
5796 | void |
5936 | ecb_cold void |
5797 | scheme_load_file (SCHEME_P_ int fin) |
5937 | scheme_load_file (SCHEME_P_ int fin) |
5798 | { |
5938 | { |
5799 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5939 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5800 | } |
5940 | } |
5801 | |
5941 | |
5802 | void |
5942 | ecb_cold void |
5803 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5943 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5804 | { |
5944 | { |
5805 | dump_stack_reset (SCHEME_A); |
5945 | dump_stack_reset (SCHEME_A); |
5806 | SCHEME_V->envir = SCHEME_V->global_env; |
5946 | SCHEME_V->envir = SCHEME_V->global_env; |
5807 | SCHEME_V->file_i = 0; |
5947 | SCHEME_V->file_i = 0; |
5808 | SCHEME_V->load_stack[0].unget = -1; |
5948 | SCHEME_V->load_stack[0].unget = -1; |
5809 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5949 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5810 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5950 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5811 | #if USE_PORTS |
|
|
5812 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5951 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5813 | #endif |
|
|
5814 | SCHEME_V->retcode = 0; |
5952 | SCHEME_V->retcode = 0; |
5815 | |
5953 | |
5816 | #if USE_PORTS |
|
|
5817 | if (fin == STDIN_FILENO) |
5954 | if (fin == STDIN_FILENO) |
5818 | SCHEME_V->interactive_repl = 1; |
5955 | SCHEME_V->interactive_repl = 1; |
5819 | #endif |
|
|
5820 | |
5956 | |
5821 | #if USE_PORTS |
5957 | #if USE_PORTS |
5822 | #if SHOW_ERROR_LINE |
5958 | #if SHOW_ERROR_LINE |
5823 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5959 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5824 | |
5960 | |
… | |
… | |
5828 | #endif |
5964 | #endif |
5829 | |
5965 | |
5830 | SCHEME_V->inport = SCHEME_V->loadport; |
5966 | SCHEME_V->inport = SCHEME_V->loadport; |
5831 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5967 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5832 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5968 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
|
|
5969 | |
5833 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5970 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5834 | |
5971 | |
5835 | if (SCHEME_V->retcode == 0) |
5972 | if (SCHEME_V->retcode == 0) |
5836 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5973 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5837 | } |
5974 | } |
5838 | |
5975 | |
5839 | void |
5976 | ecb_cold void |
5840 | scheme_load_string (SCHEME_P_ const char *cmd) |
5977 | scheme_load_string (SCHEME_P_ const char *cmd) |
5841 | { |
5978 | { |
|
|
5979 | #if USE_PORTs |
5842 | dump_stack_reset (SCHEME_A); |
5980 | dump_stack_reset (SCHEME_A); |
5843 | SCHEME_V->envir = SCHEME_V->global_env; |
5981 | SCHEME_V->envir = SCHEME_V->global_env; |
5844 | SCHEME_V->file_i = 0; |
5982 | SCHEME_V->file_i = 0; |
5845 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5983 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5846 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
5984 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
5847 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
5985 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
5848 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5986 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5849 | #if USE_PORTS |
|
|
5850 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5987 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5851 | #endif |
|
|
5852 | SCHEME_V->retcode = 0; |
5988 | SCHEME_V->retcode = 0; |
5853 | SCHEME_V->interactive_repl = 0; |
5989 | SCHEME_V->interactive_repl = 0; |
5854 | SCHEME_V->inport = SCHEME_V->loadport; |
5990 | SCHEME_V->inport = SCHEME_V->loadport; |
5855 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5991 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5856 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5992 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5857 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5993 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5858 | |
5994 | |
5859 | if (SCHEME_V->retcode == 0) |
5995 | if (SCHEME_V->retcode == 0) |
5860 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5996 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
|
|
5997 | #else |
|
|
5998 | abort (); |
|
|
5999 | #endif |
5861 | } |
6000 | } |
5862 | |
6001 | |
5863 | void |
6002 | ecb_cold void |
5864 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
6003 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5865 | { |
6004 | { |
5866 | pointer x; |
6005 | pointer x; |
5867 | |
6006 | |
5868 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
6007 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
… | |
… | |
5873 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
6012 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5874 | } |
6013 | } |
5875 | |
6014 | |
5876 | #if !STANDALONE |
6015 | #if !STANDALONE |
5877 | |
6016 | |
5878 | void |
6017 | ecb_cold void |
5879 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
6018 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5880 | { |
6019 | { |
5881 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
6020 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5882 | } |
6021 | } |
5883 | |
6022 | |
5884 | void |
6023 | ecb_cold void |
5885 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
6024 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5886 | { |
6025 | { |
5887 | int i; |
6026 | int i; |
5888 | |
6027 | |
5889 | for (i = 0; i < count; i++) |
6028 | for (i = 0; i < count; i++) |
5890 | scheme_register_foreign_func (SCHEME_A_ list + i); |
6029 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5891 | } |
6030 | } |
5892 | |
6031 | |
5893 | pointer |
6032 | ecb_cold pointer |
5894 | scheme_apply0 (SCHEME_P_ const char *procname) |
6033 | scheme_apply0 (SCHEME_P_ const char *procname) |
5895 | { |
6034 | { |
5896 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
6035 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
5897 | } |
6036 | } |
5898 | |
6037 | |
5899 | void |
6038 | ecb_cold void |
5900 | save_from_C_call (SCHEME_P) |
6039 | save_from_C_call (SCHEME_P) |
5901 | { |
6040 | { |
5902 | pointer saved_data = cons (car (S_SINK), |
6041 | pointer saved_data = cons (car (S_SINK), |
5903 | cons (SCHEME_V->envir, |
6042 | cons (SCHEME_V->envir, |
5904 | SCHEME_V->dump)); |
6043 | SCHEME_V->dump)); |
… | |
… | |
5908 | /* Truncate the dump stack so TS will return here when done, not |
6047 | /* Truncate the dump stack so TS will return here when done, not |
5909 | directly resume pre-C-call operations. */ |
6048 | directly resume pre-C-call operations. */ |
5910 | dump_stack_reset (SCHEME_A); |
6049 | dump_stack_reset (SCHEME_A); |
5911 | } |
6050 | } |
5912 | |
6051 | |
5913 | void |
6052 | ecb_cold void |
5914 | restore_from_C_call (SCHEME_P) |
6053 | restore_from_C_call (SCHEME_P) |
5915 | { |
6054 | { |
5916 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
6055 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5917 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
6056 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5918 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
6057 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5919 | /* Pop */ |
6058 | /* Pop */ |
5920 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
6059 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5921 | } |
6060 | } |
5922 | |
6061 | |
5923 | /* "func" and "args" are assumed to be already eval'ed. */ |
6062 | /* "func" and "args" are assumed to be already eval'ed. */ |
5924 | pointer |
6063 | ecb_cold pointer |
5925 | scheme_call (SCHEME_P_ pointer func, pointer args) |
6064 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5926 | { |
6065 | { |
5927 | int old_repl = SCHEME_V->interactive_repl; |
6066 | int old_repl = SCHEME_V->interactive_repl; |
5928 | |
6067 | |
5929 | SCHEME_V->interactive_repl = 0; |
6068 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5936 | SCHEME_V->interactive_repl = old_repl; |
6075 | SCHEME_V->interactive_repl = old_repl; |
5937 | restore_from_C_call (SCHEME_A); |
6076 | restore_from_C_call (SCHEME_A); |
5938 | return SCHEME_V->value; |
6077 | return SCHEME_V->value; |
5939 | } |
6078 | } |
5940 | |
6079 | |
5941 | pointer |
6080 | ecb_cold pointer |
5942 | scheme_eval (SCHEME_P_ pointer obj) |
6081 | scheme_eval (SCHEME_P_ pointer obj) |
5943 | { |
6082 | { |
5944 | int old_repl = SCHEME_V->interactive_repl; |
6083 | int old_repl = SCHEME_V->interactive_repl; |
5945 | |
6084 | |
5946 | SCHEME_V->interactive_repl = 0; |
6085 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5958 | |
6097 | |
5959 | /* ========== Main ========== */ |
6098 | /* ========== Main ========== */ |
5960 | |
6099 | |
5961 | #if STANDALONE |
6100 | #if STANDALONE |
5962 | |
6101 | |
5963 | int |
6102 | ecb_cold int |
5964 | main (int argc, char **argv) |
6103 | main (int argc, char **argv) |
5965 | { |
6104 | { |
5966 | # if USE_MULTIPLICITY |
6105 | # if USE_MULTIPLICITY |
5967 | scheme ssc; |
6106 | scheme ssc; |
5968 | scheme *const SCHEME_V = &ssc; |
6107 | scheme *const SCHEME_V = &ssc; |
… | |
… | |
5970 | # endif |
6109 | # endif |
5971 | int fin; |
6110 | int fin; |
5972 | char *file_name = InitFile; |
6111 | char *file_name = InitFile; |
5973 | int retcode; |
6112 | int retcode; |
5974 | int isfile = 1; |
6113 | int isfile = 1; |
|
|
6114 | #if EXPERIMENT |
|
|
6115 | system ("ps v $PPID"); |
|
|
6116 | #endif |
5975 | |
6117 | |
5976 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6118 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5977 | { |
6119 | { |
5978 | xwrstr ("Usage: tinyscheme -?\n"); |
6120 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
5979 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6121 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
5980 | xwrstr ("followed by\n"); |
6122 | putstr (SCHEME_A_ "followed by\n"); |
5981 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6123 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
5982 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6124 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
5983 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6125 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
5984 | xwrstr ("Use - as filename for stdin.\n"); |
6126 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
5985 | return 1; |
6127 | return 1; |
5986 | } |
6128 | } |
5987 | |
6129 | |
5988 | if (!scheme_init (SCHEME_A)) |
6130 | if (!scheme_init (SCHEME_A)) |
5989 | { |
6131 | { |
5990 | xwrstr ("Could not initialize!\n"); |
6132 | putstr (SCHEME_A_ "Could not initialize!\n"); |
5991 | return 2; |
6133 | return 2; |
5992 | } |
6134 | } |
5993 | |
6135 | |
5994 | # if USE_PORTS |
6136 | # if USE_PORTS |
5995 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6137 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
6008 | } |
6150 | } |
6009 | #endif |
6151 | #endif |
6010 | |
6152 | |
6011 | do |
6153 | do |
6012 | { |
6154 | { |
6013 | #if USE_PORTS |
|
|
6014 | if (strcmp (file_name, "-") == 0) |
6155 | if (strcmp (file_name, "-") == 0) |
6015 | fin = STDIN_FILENO; |
6156 | fin = STDIN_FILENO; |
6016 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
6157 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
6017 | { |
6158 | { |
6018 | pointer args = NIL; |
6159 | pointer args = NIL; |
… | |
… | |
6036 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
6177 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
6037 | |
6178 | |
6038 | } |
6179 | } |
6039 | else |
6180 | else |
6040 | fin = open (file_name, O_RDONLY); |
6181 | fin = open (file_name, O_RDONLY); |
6041 | #endif |
|
|
6042 | |
6182 | |
6043 | if (isfile && fin < 0) |
6183 | if (isfile && fin < 0) |
6044 | { |
6184 | { |
6045 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6185 | putstr (SCHEME_A_ "Could not open file "); |
|
|
6186 | putstr (SCHEME_A_ file_name); |
|
|
6187 | putcharacter (SCHEME_A_ '\n'); |
6046 | } |
6188 | } |
6047 | else |
6189 | else |
6048 | { |
6190 | { |
6049 | if (isfile) |
6191 | if (isfile) |
6050 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6192 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6051 | else |
6193 | else |
6052 | scheme_load_string (SCHEME_A_ file_name); |
6194 | scheme_load_string (SCHEME_A_ file_name); |
6053 | |
6195 | |
6054 | #if USE_PORTS |
|
|
6055 | if (!isfile || fin != STDIN_FILENO) |
6196 | if (!isfile || fin != STDIN_FILENO) |
6056 | { |
6197 | { |
6057 | if (SCHEME_V->retcode != 0) |
6198 | if (SCHEME_V->retcode != 0) |
6058 | { |
6199 | { |
6059 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6200 | putstr (SCHEME_A_ "Errors encountered reading "); |
|
|
6201 | putstr (SCHEME_A_ file_name); |
|
|
6202 | putcharacter (SCHEME_A_ '\n'); |
6060 | } |
6203 | } |
6061 | |
6204 | |
6062 | if (isfile) |
6205 | if (isfile) |
6063 | close (fin); |
6206 | close (fin); |
6064 | } |
6207 | } |
6065 | #endif |
|
|
6066 | } |
6208 | } |
6067 | |
6209 | |
6068 | file_name = *argv++; |
6210 | file_name = *argv++; |
6069 | } |
6211 | } |
6070 | while (file_name != 0); |
6212 | while (file_name != 0); |