… | |
… | |
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> |
… | |
… | |
34 | |
36 | |
35 | #include <sys/types.h> |
37 | #include <sys/types.h> |
36 | #include <sys/stat.h> |
38 | #include <sys/stat.h> |
37 | #include <fcntl.h> |
39 | #include <fcntl.h> |
38 | |
40 | |
|
|
41 | #if !USE_ERROR_CHECKING |
|
|
42 | # define NDEBUG |
|
|
43 | #endif |
|
|
44 | |
|
|
45 | #include <assert.h> |
|
|
46 | #include <stdlib.h> |
39 | #include <string.h> |
47 | #include <string.h> |
40 | #include <stdlib.h> |
|
|
41 | |
48 | |
42 | #include <limits.h> |
49 | #include <limits.h> |
43 | #include <inttypes.h> |
50 | #include <inttypes.h> |
44 | #include <float.h> |
51 | #include <float.h> |
45 | //#include <ctype.h> |
52 | |
|
|
53 | #if !USE_SYSTEM_MALLOC |
|
|
54 | # define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
|
|
55 | # include "malloc.c" |
|
|
56 | # define malloc(n) tiny_malloc (n) |
|
|
57 | # define realloc(p,n) tiny_realloc (p, n) |
|
|
58 | # define free(p) tiny_free (p) |
|
|
59 | #endif |
|
|
60 | |
|
|
61 | #if '1' != '0' + 1 \ |
|
|
62 | || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ |
|
|
63 | || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ |
|
|
64 | || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ |
|
|
65 | || 'f' != 'a' + 5 |
|
|
66 | # error "execution character set digits not consecutive" |
|
|
67 | #endif |
46 | |
68 | |
47 | enum { |
69 | enum { |
48 | TOK_EOF, |
70 | TOK_EOF, |
49 | TOK_LPAREN, |
71 | TOK_LPAREN, |
50 | TOK_RPAREN, |
72 | TOK_RPAREN, |
51 | TOK_DOT, |
73 | TOK_DOT, |
52 | TOK_ATOM, |
74 | TOK_ATOM, |
|
|
75 | TOK_DOTATOM, /* atom name starting with '.' */ |
|
|
76 | TOK_STRATOM, /* atom name enclosed in | */ |
53 | TOK_QUOTE, |
77 | TOK_QUOTE, |
54 | TOK_DQUOTE, |
78 | TOK_DQUOTE, |
55 | TOK_BQUOTE, |
79 | TOK_BQUOTE, |
56 | TOK_COMMA, |
80 | TOK_COMMA, |
57 | TOK_ATMARK, |
81 | TOK_ATMARK, |
… | |
… | |
59 | TOK_SHARP_CONST, |
83 | TOK_SHARP_CONST, |
60 | TOK_VEC |
84 | TOK_VEC |
61 | }; |
85 | }; |
62 | |
86 | |
63 | #define BACKQUOTE '`' |
87 | #define BACKQUOTE '`' |
64 | #define DELIMITERS "()\";\f\t\v\n\r " |
88 | #define WHITESPACE " \t\r\n\v\f" |
|
|
89 | #define DELIMITERS "()\";" WHITESPACE |
65 | |
90 | |
66 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
91 | #define NIL POINTER (&SCHEME_V->xNIL) |
67 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
92 | #define S_T POINTER (&SCHEME_V->xT) |
68 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
93 | #define S_F POINTER (&SCHEME_V->xF) |
69 | #define S_SINK (&SCHEME_V->xsink) |
94 | #define S_SINK POINTER (&SCHEME_V->xsink) |
70 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
95 | #define S_EOF POINTER (&SCHEME_V->xEOF_OBJ) |
71 | |
96 | |
72 | #if !USE_MULTIPLICITY |
97 | #if !USE_MULTIPLICITY |
73 | static scheme sc; |
98 | static scheme sc; |
74 | #endif |
99 | #endif |
75 | |
100 | |
76 | static void |
101 | ecb_cold static void |
77 | xbase (char *s, long n, int base) |
102 | xbase (char *s, long n, int base) |
78 | { |
103 | { |
79 | if (n < 0) |
104 | if (n < 0) |
80 | { |
105 | { |
81 | *s++ = '-'; |
106 | *s++ = '-'; |
… | |
… | |
83 | } |
108 | } |
84 | |
109 | |
85 | char *p = s; |
110 | char *p = s; |
86 | |
111 | |
87 | do { |
112 | do { |
88 | *p++ = '0' + n % base; |
113 | *p++ = "0123456789abcdef"[n % base]; |
89 | n /= base; |
114 | n /= base; |
90 | } while (n); |
115 | } while (n); |
91 | |
116 | |
92 | *p-- = 0; |
117 | *p-- = 0; |
93 | |
118 | |
… | |
… | |
96 | char x = *s; *s = *p; *p = x; |
121 | char x = *s; *s = *p; *p = x; |
97 | --p; ++s; |
122 | --p; ++s; |
98 | } |
123 | } |
99 | } |
124 | } |
100 | |
125 | |
101 | static void |
126 | ecb_cold static void |
102 | xnum (char *s, long n) |
127 | xnum (char *s, long n) |
103 | { |
128 | { |
104 | xbase (s, n, 10); |
129 | xbase (s, n, 10); |
105 | } |
130 | } |
106 | |
131 | |
107 | static void |
132 | ecb_cold static void |
108 | xwrstr (const char *s) |
133 | putnum (SCHEME_P_ long n) |
109 | { |
|
|
110 | write (1, s, strlen (s)); |
|
|
111 | } |
|
|
112 | |
|
|
113 | static void |
|
|
114 | xwrnum (long n) |
|
|
115 | { |
134 | { |
116 | char buf[64]; |
135 | char buf[64]; |
117 | |
136 | |
118 | xnum (buf, n); |
137 | xnum (buf, n); |
119 | xwrstr (buf); |
138 | putstr (SCHEME_A_ buf); |
120 | } |
139 | } |
|
|
140 | |
|
|
141 | #if USE_CHAR_CLASSIFIERS |
|
|
142 | #include <ctype.h> |
|
|
143 | #else |
121 | |
144 | |
122 | static char |
145 | static char |
123 | xtoupper (char c) |
146 | xtoupper (char c) |
124 | { |
147 | { |
125 | if (c >= 'a' && c <= 'z') |
148 | if (c >= 'a' && c <= 'z') |
… | |
… | |
145 | |
168 | |
146 | #define toupper(c) xtoupper (c) |
169 | #define toupper(c) xtoupper (c) |
147 | #define tolower(c) xtolower (c) |
170 | #define tolower(c) xtolower (c) |
148 | #define isdigit(c) xisdigit (c) |
171 | #define isdigit(c) xisdigit (c) |
149 | |
172 | |
|
|
173 | #endif |
|
|
174 | |
150 | #if USE_IGNORECASE |
175 | #if USE_IGNORECASE |
151 | static const char * |
176 | ecb_cold static const char * |
152 | xstrlwr (char *s) |
177 | xstrlwr (char *s) |
153 | { |
178 | { |
154 | const char *p = s; |
179 | const char *p = s; |
155 | |
180 | |
156 | while (*s) |
181 | while (*s) |
… | |
… | |
176 | |
201 | |
177 | #ifndef InitFile |
202 | #ifndef InitFile |
178 | # define InitFile "init.scm" |
203 | # define InitFile "init.scm" |
179 | #endif |
204 | #endif |
180 | |
205 | |
181 | #ifndef FIRST_CELLSEGS |
|
|
182 | # define FIRST_CELLSEGS 3 |
|
|
183 | #endif |
|
|
184 | |
|
|
185 | enum scheme_types |
206 | enum scheme_types |
186 | { |
207 | { |
|
|
208 | T_INTEGER, |
|
|
209 | T_CHARACTER, |
187 | T_FREE, |
210 | T_REAL, |
188 | T_STRING, |
211 | T_STRING, |
189 | T_NUMBER, |
|
|
190 | T_SYMBOL, |
212 | T_SYMBOL, |
191 | T_PROC, |
213 | T_PROC, |
192 | T_PAIR, |
214 | T_PAIR, /* also used for free cells */ |
193 | T_CLOSURE, |
215 | T_CLOSURE, |
|
|
216 | T_BYTECODE, // temp |
|
|
217 | T_MACRO, |
194 | T_CONTINUATION, |
218 | T_CONTINUATION, |
195 | T_FOREIGN, |
219 | T_FOREIGN, |
196 | T_CHARACTER, |
|
|
197 | T_PORT, |
220 | T_PORT, |
198 | T_VECTOR, |
221 | T_VECTOR, |
199 | T_MACRO, |
|
|
200 | T_PROMISE, |
222 | T_PROMISE, |
201 | T_ENVIRONMENT, |
223 | T_ENVIRONMENT, |
202 | /* one more... */ |
224 | |
203 | T_NUM_SYSTEM_TYPES |
225 | T_NUM_SYSTEM_TYPES |
204 | }; |
226 | }; |
205 | |
227 | |
206 | #define T_MASKTYPE 0x000f |
228 | #define T_MASKTYPE 0x000f |
207 | #define T_SYNTAX 0x0010 |
229 | #define T_SYNTAX 0x0010 |
208 | #define T_IMMUTABLE 0x0020 |
230 | #define T_IMMUTABLE 0x0020 |
209 | #define T_ATOM 0x0040 /* only for gc */ |
231 | #define T_ATOM 0x0040 /* only for gc */ |
210 | #define T_MARK 0x0080 /* only for gc */ |
232 | #define T_MARK 0x0080 /* only for gc */ |
211 | |
233 | |
|
|
234 | /* num, for generic arithmetic */ |
|
|
235 | struct num |
|
|
236 | { |
|
|
237 | IVALUE ivalue; |
|
|
238 | #if USE_REAL |
|
|
239 | RVALUE rvalue; |
|
|
240 | char is_fixnum; |
|
|
241 | #endif |
|
|
242 | }; |
|
|
243 | |
|
|
244 | #if USE_REAL |
|
|
245 | # define num_is_fixnum(n) (n).is_fixnum |
|
|
246 | # define num_set_fixnum(n,f) (n).is_fixnum = (f) |
|
|
247 | # define num_ivalue(n) (n).ivalue |
|
|
248 | # define num_rvalue(n) (n).rvalue |
|
|
249 | # define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i) |
|
|
250 | # define num_set_rvalue(n,r) (n).rvalue = (r) |
|
|
251 | #else |
|
|
252 | # define num_is_fixnum(n) 1 |
|
|
253 | # define num_set_fixnum(n,f) 0 |
|
|
254 | # define num_ivalue(n) (n).ivalue |
|
|
255 | # define num_rvalue(n) (n).ivalue |
|
|
256 | # define num_set_ivalue(n,i) (n).ivalue = (i) |
|
|
257 | # define num_set_rvalue(n,r) (n).ivalue = (r) |
|
|
258 | #endif |
|
|
259 | |
212 | enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; |
260 | enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; |
213 | |
261 | |
214 | static num num_op (enum num_op op, num a, num b); |
262 | static num num_op (enum num_op op, num a, num b); |
215 | static num num_intdiv (num a, num b); |
263 | static num num_intdiv (num a, num b); |
216 | static num num_rem (num a, num b); |
264 | static num num_rem (num a, num b); |
217 | static num num_mod (num a, num b); |
265 | static num num_mod (num a, num b); |
218 | |
266 | |
219 | #if USE_MATH |
|
|
220 | static double round_per_R5RS (double x); |
|
|
221 | #endif |
|
|
222 | static int is_zero_rvalue (RVALUE x); |
267 | static int is_zero_rvalue (RVALUE x); |
223 | |
268 | |
224 | static num num_zero; |
269 | static num num_zero; |
225 | static num num_one; |
270 | static num num_one; |
226 | |
271 | |
|
|
272 | /* convert "pointer" to cell* / cell* to pointer */ |
|
|
273 | #define CELL(p) ((struct cell *)(p) + 0) |
|
|
274 | #define POINTER(c) ((void *)((c) - 0)) |
|
|
275 | |
227 | /* macros for cell operations */ |
276 | /* macros for cell operations */ |
228 | #define typeflag(p) ((p)->flag + 0) |
277 | #define typeflag(p) (CELL(p)->flag + 0) |
229 | #define set_typeflag(p,v) ((p)->flag = (v)) |
278 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
230 | #define type(p) (typeflag (p) & T_MASKTYPE) |
279 | #define type(p) (typeflag (p) & T_MASKTYPE) |
231 | |
280 | |
232 | INTERFACE int |
281 | INTERFACE int |
233 | is_string (pointer p) |
282 | is_string (pointer p) |
234 | { |
283 | { |
235 | return type (p) == T_STRING; |
284 | return type (p) == T_STRING; |
236 | } |
285 | } |
237 | |
286 | |
238 | #define strvalue(p) ((p)->object.string.svalue) |
287 | #define strvalue(p) (CELL(p)->object.string.svalue) |
239 | #define strlength(p) ((p)->object.string.length) |
288 | #define strlength(p) (CELL(p)->object.string.length) |
240 | |
|
|
241 | INTERFACE int is_list (SCHEME_P_ pointer p); |
|
|
242 | |
289 | |
243 | INTERFACE int |
290 | INTERFACE int |
244 | is_vector (pointer p) |
291 | is_vector (pointer p) |
245 | { |
292 | { |
246 | return type (p) == T_VECTOR; |
293 | return type (p) == T_VECTOR; |
247 | } |
294 | } |
248 | |
295 | |
249 | #define vecvalue(p) ((p)->object.vector.vvalue) |
296 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
250 | #define veclength(p) ((p)->object.vector.length) |
297 | #define veclength(p) (CELL(p)->object.vector.length) |
251 | INTERFACE void fill_vector (pointer vec, pointer obj); |
298 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
252 | INTERFACE uint32_t vector_length (pointer vec); |
|
|
253 | INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); |
299 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
254 | INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); |
300 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
255 | |
301 | |
256 | INTERFACE uint32_t |
302 | INTERFACE int |
257 | vector_length (pointer vec) |
303 | is_integer (pointer p) |
258 | { |
304 | { |
259 | return vec->object.vector.length; |
305 | return type (p) == T_INTEGER; |
|
|
306 | } |
|
|
307 | |
|
|
308 | /* not the same as in scheme, where integers are (correctly :) reals */ |
|
|
309 | INTERFACE int |
|
|
310 | is_real (pointer p) |
|
|
311 | { |
|
|
312 | return type (p) == T_REAL; |
260 | } |
313 | } |
261 | |
314 | |
262 | INTERFACE int |
315 | INTERFACE int |
263 | is_number (pointer p) |
316 | is_number (pointer p) |
264 | { |
317 | { |
265 | return type (p) == T_NUMBER; |
318 | return is_integer (p) || is_real (p); |
266 | } |
|
|
267 | |
|
|
268 | INTERFACE int |
|
|
269 | is_integer (pointer p) |
|
|
270 | { |
|
|
271 | return is_number (p) && num_is_fixnum (p->object.number); |
|
|
272 | } |
|
|
273 | |
|
|
274 | INTERFACE int |
|
|
275 | is_real (pointer p) |
|
|
276 | { |
|
|
277 | return is_number (p) && !num_is_fixnum (p->object.number); |
|
|
278 | } |
319 | } |
279 | |
320 | |
280 | INTERFACE int |
321 | INTERFACE int |
281 | is_character (pointer p) |
322 | is_character (pointer p) |
282 | { |
323 | { |
… | |
… | |
287 | string_value (pointer p) |
328 | string_value (pointer p) |
288 | { |
329 | { |
289 | return strvalue (p); |
330 | return strvalue (p); |
290 | } |
331 | } |
291 | |
332 | |
292 | ecb_inline num |
|
|
293 | nvalue (pointer p) |
|
|
294 | { |
|
|
295 | return (p)->object.number; |
|
|
296 | } |
|
|
297 | |
|
|
298 | static IVALUE |
|
|
299 | num_get_ivalue (const num n) |
|
|
300 | { |
|
|
301 | return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n); |
|
|
302 | } |
|
|
303 | |
|
|
304 | static RVALUE |
|
|
305 | num_get_rvalue (const num n) |
|
|
306 | { |
|
|
307 | return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n); |
|
|
308 | } |
|
|
309 | |
|
|
310 | INTERFACE IVALUE |
|
|
311 | ivalue (pointer p) |
|
|
312 | { |
|
|
313 | return num_get_ivalue (p->object.number); |
|
|
314 | } |
|
|
315 | |
|
|
316 | INTERFACE RVALUE |
|
|
317 | rvalue (pointer p) |
|
|
318 | { |
|
|
319 | return num_get_rvalue (p->object.number); |
|
|
320 | } |
|
|
321 | |
|
|
322 | #define ivalue_unchecked(p) ((p)->object.number.value.ivalue) |
333 | #define ivalue_unchecked(p) CELL(p)->object.ivalue |
|
|
334 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
|
|
335 | |
323 | #if USE_REAL |
336 | #if USE_REAL |
324 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
337 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
325 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
338 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
326 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
|
|
327 | #else |
339 | #else |
328 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
340 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
329 | # define set_num_integer(p) 0 |
341 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
330 | # define set_num_real(p) 0 |
|
|
331 | #endif |
342 | #endif |
332 | |
343 | |
333 | INTERFACE long |
344 | INTERFACE long |
334 | charvalue (pointer p) |
345 | charvalue (pointer p) |
335 | { |
346 | { |
336 | return ivalue_unchecked (p); |
347 | return ivalue_unchecked (p); |
337 | } |
348 | } |
338 | |
349 | |
|
|
350 | #define port(p) CELL(p)->object.port |
|
|
351 | #define set_port(p,v) port(p) = (v) |
339 | INTERFACE int |
352 | INTERFACE int |
340 | is_port (pointer p) |
353 | is_port (pointer p) |
341 | { |
354 | { |
342 | return type (p) == T_PORT; |
355 | return type (p) == T_PORT; |
343 | } |
356 | } |
344 | |
357 | |
345 | INTERFACE int |
358 | INTERFACE int |
346 | is_inport (pointer p) |
359 | is_inport (pointer p) |
347 | { |
360 | { |
348 | return is_port (p) && p->object.port->kind & port_input; |
361 | return is_port (p) && port (p)->kind & port_input; |
349 | } |
362 | } |
350 | |
363 | |
351 | INTERFACE int |
364 | INTERFACE int |
352 | is_outport (pointer p) |
365 | is_outport (pointer p) |
353 | { |
366 | { |
354 | return is_port (p) && p->object.port->kind & port_output; |
367 | return is_port (p) && port (p)->kind & port_output; |
355 | } |
368 | } |
356 | |
369 | |
357 | INTERFACE int |
370 | INTERFACE int |
358 | is_pair (pointer p) |
371 | is_pair (pointer p) |
359 | { |
372 | { |
360 | return type (p) == T_PAIR; |
373 | return type (p) == T_PAIR; |
361 | } |
374 | } |
362 | |
375 | |
363 | #define car(p) ((p)->object.cons.car + 0) |
376 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
364 | #define cdr(p) ((p)->object.cons.cdr + 0) |
377 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
365 | |
378 | |
366 | static pointer caar (pointer p) { return car (car (p)); } |
379 | static pointer caar (pointer p) { return car (car (p)); } |
367 | static pointer cadr (pointer p) { return car (cdr (p)); } |
380 | static pointer cadr (pointer p) { return car (cdr (p)); } |
368 | static pointer cdar (pointer p) { return cdr (car (p)); } |
381 | static pointer cdar (pointer p) { return cdr (car (p)); } |
369 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
382 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
… | |
… | |
373 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
386 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
374 | |
387 | |
375 | INTERFACE void |
388 | INTERFACE void |
376 | set_car (pointer p, pointer q) |
389 | set_car (pointer p, pointer q) |
377 | { |
390 | { |
378 | p->object.cons.car = q; |
391 | CELL(p)->object.cons.car = CELL (q); |
379 | } |
392 | } |
380 | |
393 | |
381 | INTERFACE void |
394 | INTERFACE void |
382 | set_cdr (pointer p, pointer q) |
395 | set_cdr (pointer p, pointer q) |
383 | { |
396 | { |
384 | p->object.cons.cdr = q; |
397 | CELL(p)->object.cons.cdr = CELL (q); |
385 | } |
398 | } |
386 | |
399 | |
387 | INTERFACE pointer |
400 | INTERFACE pointer |
388 | pair_car (pointer p) |
401 | pair_car (pointer p) |
389 | { |
402 | { |
… | |
… | |
403 | } |
416 | } |
404 | |
417 | |
405 | INTERFACE char * |
418 | INTERFACE char * |
406 | symname (pointer p) |
419 | symname (pointer p) |
407 | { |
420 | { |
408 | return strvalue (car (p)); |
421 | return strvalue (p); |
409 | } |
422 | } |
410 | |
423 | |
411 | #if USE_PLIST |
424 | #if USE_PLIST |
|
|
425 | #error plists are broken because symbols are no longer pairs |
|
|
426 | #define symprop(p) cdr(p) |
412 | SCHEME_EXPORT int |
427 | SCHEME_EXPORT int |
413 | hasprop (pointer p) |
428 | hasprop (pointer p) |
414 | { |
429 | { |
415 | return typeflag (p) & T_SYMBOL; |
430 | return typeflag (p) & T_SYMBOL; |
416 | } |
431 | } |
417 | |
|
|
418 | # define symprop(p) cdr(p) |
|
|
419 | #endif |
432 | #endif |
420 | |
433 | |
421 | INTERFACE int |
434 | INTERFACE int |
422 | is_syntax (pointer p) |
435 | is_syntax (pointer p) |
423 | { |
436 | { |
… | |
… | |
437 | } |
450 | } |
438 | |
451 | |
439 | INTERFACE char * |
452 | INTERFACE char * |
440 | syntaxname (pointer p) |
453 | syntaxname (pointer p) |
441 | { |
454 | { |
442 | return strvalue (car (p)); |
455 | return strvalue (p); |
443 | } |
456 | } |
444 | |
457 | |
445 | #define procnum(p) ivalue (p) |
458 | #define procnum(p) ivalue_unchecked (p) |
446 | static const char *procname (pointer x); |
459 | static const char *procname (pointer x); |
447 | |
460 | |
448 | INTERFACE int |
461 | INTERFACE int |
449 | is_closure (pointer p) |
462 | is_closure (pointer p) |
450 | { |
463 | { |
… | |
… | |
513 | #if USE_ERROR_CHECKING |
526 | #if USE_ERROR_CHECKING |
514 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
527 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
515 | #endif |
528 | #endif |
516 | } |
529 | } |
517 | |
530 | |
|
|
531 | /* Result is: |
|
|
532 | proper list: length |
|
|
533 | circular list: -1 |
|
|
534 | not even a pair: -2 |
|
|
535 | dotted list: -2 minus length before dot |
|
|
536 | */ |
|
|
537 | ecb_hot INTERFACE int |
|
|
538 | list_length (SCHEME_P_ pointer a) |
|
|
539 | { |
|
|
540 | int i = 0; |
|
|
541 | pointer slow, fast; |
|
|
542 | |
|
|
543 | slow = fast = a; |
|
|
544 | |
|
|
545 | while (1) |
|
|
546 | { |
|
|
547 | if (fast == NIL) |
|
|
548 | return i; |
|
|
549 | |
|
|
550 | if (!is_pair (fast)) |
|
|
551 | return -2 - i; |
|
|
552 | |
|
|
553 | fast = cdr (fast); |
|
|
554 | ++i; |
|
|
555 | |
|
|
556 | if (fast == NIL) |
|
|
557 | return i; |
|
|
558 | |
|
|
559 | if (!is_pair (fast)) |
|
|
560 | return -2 - i; |
|
|
561 | |
|
|
562 | ++i; |
|
|
563 | fast = cdr (fast); |
|
|
564 | |
|
|
565 | /* Safe because we would have already returned if `fast' |
|
|
566 | encountered a non-pair. */ |
|
|
567 | slow = cdr (slow); |
|
|
568 | |
|
|
569 | if (fast == slow) |
|
|
570 | { |
|
|
571 | /* the fast pointer has looped back around and caught up |
|
|
572 | with the slow pointer, hence the structure is circular, |
|
|
573 | not of finite length, and therefore not a list */ |
|
|
574 | return -1; |
|
|
575 | } |
|
|
576 | } |
|
|
577 | } |
|
|
578 | |
|
|
579 | INTERFACE int |
|
|
580 | is_list (SCHEME_P_ pointer a) |
|
|
581 | { |
|
|
582 | return list_length (SCHEME_A_ a) >= 0; |
|
|
583 | } |
|
|
584 | |
518 | #if USE_CHAR_CLASSIFIERS |
585 | #if USE_CHAR_CLASSIFIERS |
|
|
586 | |
519 | ecb_inline int |
587 | ecb_inline int |
520 | Cisalpha (int c) |
588 | Cisalpha (int c) |
521 | { |
589 | { |
522 | return isascii (c) && isalpha (c); |
590 | return isascii (c) && isalpha (c); |
523 | } |
591 | } |
… | |
… | |
581 | "gs", |
649 | "gs", |
582 | "rs", |
650 | "rs", |
583 | "us" |
651 | "us" |
584 | }; |
652 | }; |
585 | |
653 | |
586 | static int |
654 | ecb_cold static int |
587 | is_ascii_name (const char *name, int *pc) |
655 | is_ascii_name (const char *name, int *pc) |
588 | { |
656 | { |
589 | int i; |
657 | int i; |
590 | |
658 | |
591 | for (i = 0; i < 32; i++) |
659 | for (i = 0; i < 32; i++) |
… | |
… | |
609 | #endif |
677 | #endif |
610 | |
678 | |
611 | static int file_push (SCHEME_P_ const char *fname); |
679 | static int file_push (SCHEME_P_ const char *fname); |
612 | static void file_pop (SCHEME_P); |
680 | static void file_pop (SCHEME_P); |
613 | static int file_interactive (SCHEME_P); |
681 | static int file_interactive (SCHEME_P); |
614 | ecb_inline int is_one_of (char *s, int c); |
682 | ecb_inline int is_one_of (const char *s, int c); |
615 | static int alloc_cellseg (SCHEME_P_ int n); |
683 | static int alloc_cellseg (SCHEME_P); |
616 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
684 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
617 | static void finalize_cell (SCHEME_P_ pointer a); |
685 | static void finalize_cell (SCHEME_P_ pointer a); |
618 | static int count_consecutive_cells (pointer x, int needed); |
|
|
619 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
686 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
620 | static pointer mk_number (SCHEME_P_ const num n); |
687 | static pointer mk_number (SCHEME_P_ const num n); |
621 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
688 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
622 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
689 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
623 | static pointer mk_atom (SCHEME_P_ char *q); |
690 | static pointer mk_atom (SCHEME_P_ char *q); |
624 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
691 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
625 | |
692 | |
|
|
693 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
694 | |
626 | #if USE_PORTS |
695 | #if USE_PORTS |
627 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
628 | static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); |
696 | static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); |
629 | static pointer port_from_file (SCHEME_P_ int, int prop); |
697 | static pointer port_from_file (SCHEME_P_ int, int prop); |
630 | static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
698 | static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
631 | static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); |
699 | static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); |
632 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
700 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
633 | static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
701 | static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
634 | static void port_close (SCHEME_P_ pointer p, int flag); |
702 | static void port_close (SCHEME_P_ pointer p, int flag); |
635 | #endif |
703 | #endif |
|
|
704 | |
636 | static void mark (pointer a); |
705 | static void mark (pointer a); |
637 | static void gc (SCHEME_P_ pointer a, pointer b); |
706 | static void gc (SCHEME_P_ pointer a, pointer b); |
638 | static int basic_inchar (port *pt); |
707 | static int basic_inchar (port *pt); |
639 | static int inchar (SCHEME_P); |
708 | static int inchar (SCHEME_P); |
640 | static void backchar (SCHEME_P_ int c); |
709 | static void backchar (SCHEME_P_ int c); |
641 | static char *readstr_upto (SCHEME_P_ char *delim); |
710 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
642 | static pointer readstrexp (SCHEME_P); |
711 | static pointer readstrexp (SCHEME_P_ char delim); |
643 | ecb_inline int skipspace (SCHEME_P); |
712 | ecb_inline int skipspace (SCHEME_P); |
644 | static int token (SCHEME_P); |
713 | static int token (SCHEME_P); |
645 | static void printslashstring (SCHEME_P_ char *s, int len); |
714 | static void printslashstring (SCHEME_P_ char *s, int len); |
646 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
715 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
647 | static void printatom (SCHEME_P_ pointer l, int f); |
716 | static void printatom (SCHEME_P_ pointer l, int f); |
… | |
… | |
664 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
733 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
665 | static void assign_syntax (SCHEME_P_ const char *name); |
734 | static void assign_syntax (SCHEME_P_ const char *name); |
666 | static int syntaxnum (pointer p); |
735 | static int syntaxnum (pointer p); |
667 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
736 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
668 | |
737 | |
|
|
738 | static IVALUE |
|
|
739 | ivalue (pointer x) |
|
|
740 | { |
|
|
741 | return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x); |
|
|
742 | } |
|
|
743 | |
|
|
744 | static RVALUE |
|
|
745 | rvalue (pointer x) |
|
|
746 | { |
|
|
747 | return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x); |
|
|
748 | } |
|
|
749 | |
|
|
750 | INTERFACE num |
|
|
751 | nvalue (pointer x) |
|
|
752 | { |
|
|
753 | num n; |
|
|
754 | |
|
|
755 | num_set_fixnum (n, is_integer (x)); |
|
|
756 | |
|
|
757 | if (num_is_fixnum (n)) |
|
|
758 | num_set_ivalue (n, ivalue_unchecked (x)); |
|
|
759 | else |
|
|
760 | num_set_rvalue (n, rvalue_unchecked (x)); |
|
|
761 | |
|
|
762 | return n; |
|
|
763 | } |
|
|
764 | |
669 | static num |
765 | static num |
670 | num_op (enum num_op op, num a, num b) |
766 | num_op (enum num_op op, num a, num b) |
671 | { |
767 | { |
672 | num ret; |
768 | num ret; |
673 | |
769 | |
674 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
770 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
675 | |
771 | |
676 | if (num_is_fixnum (ret)) |
772 | if (num_is_fixnum (ret)) |
677 | { |
773 | { |
678 | IVALUE av = num_get_ivalue (a); |
|
|
679 | IVALUE bv = num_get_ivalue (b); |
|
|
680 | |
|
|
681 | switch (op) |
774 | switch (op) |
682 | { |
775 | { |
683 | case NUM_ADD: av += bv; break; |
776 | case NUM_ADD: a.ivalue += b.ivalue; break; |
684 | case NUM_SUB: av -= bv; break; |
777 | case NUM_SUB: a.ivalue -= b.ivalue; break; |
685 | case NUM_MUL: av *= bv; break; |
778 | case NUM_MUL: a.ivalue *= b.ivalue; break; |
686 | case NUM_INTDIV: av /= bv; break; |
779 | case NUM_INTDIV: a.ivalue /= b.ivalue; break; |
687 | } |
780 | } |
688 | |
781 | |
689 | num_set_ivalue (ret, av); |
782 | num_set_ivalue (ret, a.ivalue); |
690 | } |
783 | } |
|
|
784 | #if USE_REAL |
691 | else |
785 | else |
692 | { |
786 | { |
693 | RVALUE av = num_get_rvalue (a); |
|
|
694 | RVALUE bv = num_get_rvalue (b); |
|
|
695 | |
|
|
696 | switch (op) |
787 | switch (op) |
697 | { |
788 | { |
698 | case NUM_ADD: av += bv; break; |
789 | case NUM_ADD: a.rvalue += b.rvalue; break; |
699 | case NUM_SUB: av -= bv; break; |
790 | case NUM_SUB: a.rvalue -= b.rvalue; break; |
700 | case NUM_MUL: av *= bv; break; |
791 | case NUM_MUL: a.rvalue *= b.rvalue; break; |
701 | case NUM_INTDIV: av /= bv; break; |
792 | case NUM_INTDIV: a.rvalue /= b.rvalue; break; |
702 | } |
793 | } |
703 | |
794 | |
704 | num_set_rvalue (ret, av); |
795 | num_set_rvalue (ret, a.rvalue); |
705 | } |
796 | } |
|
|
797 | #endif |
706 | |
798 | |
707 | return ret; |
799 | return ret; |
708 | } |
800 | } |
709 | |
801 | |
710 | static num |
802 | static num |
711 | num_div (num a, num b) |
803 | num_div (num a, num b) |
712 | { |
804 | { |
713 | num ret; |
805 | num ret; |
714 | |
806 | |
715 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); |
807 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0); |
716 | |
808 | |
717 | if (num_is_fixnum (ret)) |
809 | if (num_is_fixnum (ret)) |
718 | num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); |
810 | num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b)); |
719 | else |
811 | else |
720 | num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); |
812 | num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); |
721 | |
813 | |
722 | return ret; |
814 | return ret; |
723 | } |
815 | } |
724 | |
816 | |
725 | static num |
817 | static num |
… | |
… | |
727 | { |
819 | { |
728 | num ret; |
820 | num ret; |
729 | long e1, e2, res; |
821 | long e1, e2, res; |
730 | |
822 | |
731 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
823 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
732 | e1 = num_get_ivalue (a); |
824 | e1 = num_ivalue (a); |
733 | e2 = num_get_ivalue (b); |
825 | e2 = num_ivalue (b); |
734 | res = e1 % e2; |
826 | res = e1 % e2; |
735 | |
827 | |
736 | /* remainder should have same sign as second operand */ |
828 | /* remainder should have same sign as second operand */ |
737 | if (res > 0) |
829 | if (res > 0) |
738 | { |
830 | { |
… | |
… | |
754 | { |
846 | { |
755 | num ret; |
847 | num ret; |
756 | long e1, e2, res; |
848 | long e1, e2, res; |
757 | |
849 | |
758 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
850 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
759 | e1 = num_get_ivalue (a); |
851 | e1 = num_ivalue (a); |
760 | e2 = num_get_ivalue (b); |
852 | e2 = num_ivalue (b); |
761 | res = e1 % e2; |
853 | res = e1 % e2; |
762 | |
854 | |
763 | /* modulo should have same sign as second operand */ |
855 | /* modulo should have same sign as second operand */ |
764 | if (res * e2 < 0) |
856 | if (res * e2 < 0) |
765 | res += e2; |
857 | res += e2; |
… | |
… | |
775 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
867 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
776 | int ret; |
868 | int ret; |
777 | |
869 | |
778 | if (is_fixnum) |
870 | if (is_fixnum) |
779 | { |
871 | { |
780 | IVALUE av = num_get_ivalue (a); |
872 | IVALUE av = num_ivalue (a); |
781 | IVALUE bv = num_get_ivalue (b); |
873 | IVALUE bv = num_ivalue (b); |
782 | |
874 | |
783 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
875 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
784 | } |
876 | } |
785 | else |
877 | else |
786 | { |
878 | { |
787 | RVALUE av = num_get_rvalue (a); |
879 | RVALUE av = num_rvalue (a); |
788 | RVALUE bv = num_get_rvalue (b); |
880 | RVALUE bv = num_rvalue (b); |
789 | |
881 | |
790 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
882 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
791 | } |
883 | } |
792 | |
884 | |
793 | return ret; |
885 | return ret; |
794 | } |
886 | } |
795 | |
|
|
796 | #if USE_MATH |
|
|
797 | |
|
|
798 | /* Round to nearest. Round to even if midway */ |
|
|
799 | static double |
|
|
800 | round_per_R5RS (double x) |
|
|
801 | { |
|
|
802 | double fl = floor (x); |
|
|
803 | double ce = ceil (x); |
|
|
804 | double dfl = x - fl; |
|
|
805 | double dce = ce - x; |
|
|
806 | |
|
|
807 | if (dfl > dce) |
|
|
808 | return ce; |
|
|
809 | else if (dfl < dce) |
|
|
810 | return fl; |
|
|
811 | else |
|
|
812 | { |
|
|
813 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
|
|
814 | return fl; |
|
|
815 | else |
|
|
816 | return ce; |
|
|
817 | } |
|
|
818 | } |
|
|
819 | #endif |
|
|
820 | |
887 | |
821 | static int |
888 | static int |
822 | is_zero_rvalue (RVALUE x) |
889 | is_zero_rvalue (RVALUE x) |
823 | { |
890 | { |
824 | return x == 0; |
891 | return x == 0; |
… | |
… | |
830 | #endif |
897 | #endif |
831 | #endif |
898 | #endif |
832 | } |
899 | } |
833 | |
900 | |
834 | /* allocate new cell segment */ |
901 | /* allocate new cell segment */ |
835 | static int |
902 | ecb_cold static int |
836 | alloc_cellseg (SCHEME_P_ int n) |
903 | alloc_cellseg (SCHEME_P) |
837 | { |
904 | { |
838 | pointer newp; |
905 | struct cell *newp; |
839 | pointer last; |
906 | struct cell *last; |
840 | pointer p; |
907 | struct cell *p; |
841 | char *cp; |
908 | char *cp; |
842 | long i; |
909 | long i; |
843 | int k; |
910 | int k; |
844 | |
911 | |
845 | static int segsize = CELL_SEGSIZE >> 1; |
912 | static int segsize = CELL_SEGSIZE >> 1; |
846 | segsize <<= 1; |
913 | segsize <<= 1; |
847 | |
914 | |
848 | for (k = 0; k < n; k++) |
|
|
849 | { |
|
|
850 | if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1) |
|
|
851 | return k; |
|
|
852 | |
|
|
853 | cp = malloc (segsize * sizeof (struct cell)); |
915 | cp = malloc (segsize * sizeof (struct cell)); |
854 | |
916 | |
855 | if (!cp && USE_ERROR_CHECKING) |
917 | if (!cp && USE_ERROR_CHECKING) |
856 | return k; |
918 | return k; |
857 | |
919 | |
858 | i = ++SCHEME_V->last_cell_seg; |
920 | i = ++SCHEME_V->last_cell_seg; |
859 | SCHEME_V->alloc_seg[i] = cp; |
921 | SCHEME_V->alloc_seg[i] = cp; |
860 | |
922 | |
861 | /* insert new segment in address order */ |
923 | newp = (struct cell *)cp; |
862 | newp = (pointer)cp; |
|
|
863 | SCHEME_V->cell_seg[i] = newp; |
924 | SCHEME_V->cell_seg[i] = newp; |
864 | SCHEME_V->cell_segsize[i] = segsize; |
925 | SCHEME_V->cell_segsize[i] = segsize; |
865 | |
|
|
866 | //TODO: insert, not swap |
|
|
867 | while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i]) |
|
|
868 | { |
|
|
869 | p = SCHEME_V->cell_seg[i]; |
|
|
870 | SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1]; |
|
|
871 | SCHEME_V->cell_seg[i - 1] = p; |
|
|
872 | |
|
|
873 | k = SCHEME_V->cell_segsize[i]; |
|
|
874 | SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1]; |
|
|
875 | SCHEME_V->cell_segsize[i - 1] = k; |
|
|
876 | |
|
|
877 | --i; |
|
|
878 | } |
|
|
879 | |
|
|
880 | SCHEME_V->fcells += segsize; |
926 | SCHEME_V->fcells += segsize; |
881 | last = newp + segsize - 1; |
927 | last = newp + segsize - 1; |
882 | |
928 | |
883 | for (p = newp; p <= last; p++) |
929 | for (p = newp; p <= last; p++) |
884 | { |
930 | { |
|
|
931 | pointer cp = POINTER (p); |
885 | set_typeflag (p, T_FREE); |
932 | set_typeflag (cp, T_PAIR); |
886 | set_car (p, NIL); |
933 | set_car (cp, NIL); |
887 | set_cdr (p, p + 1); |
934 | set_cdr (cp, POINTER (p + 1)); |
888 | } |
935 | } |
889 | |
936 | |
890 | /* insert new cells in address order on free list */ |
|
|
891 | if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) |
|
|
892 | { |
|
|
893 | set_cdr (last, SCHEME_V->free_cell); |
937 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
894 | SCHEME_V->free_cell = newp; |
938 | SCHEME_V->free_cell = POINTER (newp); |
895 | } |
|
|
896 | else |
|
|
897 | { |
|
|
898 | p = SCHEME_V->free_cell; |
|
|
899 | |
939 | |
900 | while (cdr (p) != NIL && newp > cdr (p)) |
|
|
901 | p = cdr (p); |
|
|
902 | |
|
|
903 | set_cdr (last, cdr (p)); |
|
|
904 | set_cdr (p, newp); |
|
|
905 | } |
|
|
906 | } |
|
|
907 | |
|
|
908 | return n; |
940 | return 1; |
909 | } |
941 | } |
910 | |
942 | |
911 | /* get new cell. parameter a, b is marked by gc. */ |
943 | /* get new cell. parameter a, b is marked by gc. */ |
912 | ecb_inline pointer |
944 | ecb_inline pointer |
913 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
945 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
… | |
… | |
917 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
949 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
918 | return S_SINK; |
950 | return S_SINK; |
919 | |
951 | |
920 | if (SCHEME_V->free_cell == NIL) |
952 | if (SCHEME_V->free_cell == NIL) |
921 | { |
953 | { |
922 | 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; |
923 | |
955 | |
924 | gc (SCHEME_A_ a, b); |
956 | gc (SCHEME_A_ a, b); |
925 | |
957 | |
926 | 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) |
927 | { |
959 | { |
928 | /* 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 */ |
929 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
961 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
930 | { |
962 | { |
931 | #if USE_ERROR_CHECKING |
963 | #if USE_ERROR_CHECKING |
932 | SCHEME_V->no_memory = 1; |
964 | SCHEME_V->no_memory = 1; |
933 | return S_SINK; |
965 | return S_SINK; |
934 | #endif |
966 | #endif |
… | |
… | |
946 | } |
978 | } |
947 | } |
979 | } |
948 | |
980 | |
949 | /* To retain recent allocs before interpreter knows about them - |
981 | /* To retain recent allocs before interpreter knows about them - |
950 | Tehom */ |
982 | Tehom */ |
951 | |
983 | ecb_hot static void |
952 | static void |
|
|
953 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
984 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
954 | { |
985 | { |
955 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
986 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
956 | |
987 | |
957 | set_typeflag (holder, T_PAIR); |
988 | set_typeflag (holder, T_PAIR); |
… | |
… | |
959 | set_car (holder, recent); |
990 | set_car (holder, recent); |
960 | set_cdr (holder, car (S_SINK)); |
991 | set_cdr (holder, car (S_SINK)); |
961 | set_car (S_SINK, holder); |
992 | set_car (S_SINK, holder); |
962 | } |
993 | } |
963 | |
994 | |
964 | static pointer |
995 | ecb_hot static pointer |
965 | get_cell (SCHEME_P_ pointer a, pointer b) |
996 | get_cell (SCHEME_P_ pointer a, pointer b) |
966 | { |
997 | { |
967 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
998 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
968 | |
999 | |
969 | /* 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 |
… | |
… | |
978 | } |
1009 | } |
979 | |
1010 | |
980 | static pointer |
1011 | static pointer |
981 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1012 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
982 | { |
1013 | { |
983 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1014 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
984 | pointer *e = malloc (len * sizeof (pointer)); |
1015 | pointer *e = malloc (len * sizeof (pointer)); |
985 | |
1016 | |
986 | if (!e && USE_ERROR_CHECKING) |
1017 | if (!e && USE_ERROR_CHECKING) |
987 | return S_SINK; |
1018 | return S_SINK; |
988 | |
1019 | |
989 | /* Record it as a vector so that gc understands it. */ |
1020 | /* Record it as a vector so that gc understands it. */ |
990 | set_typeflag (v, T_VECTOR | T_ATOM); |
1021 | set_typeflag (v, T_VECTOR | T_ATOM); |
991 | |
1022 | |
992 | v->object.vector.vvalue = e; |
1023 | CELL(v)->object.vector.vvalue = e; |
993 | v->object.vector.length = len; |
1024 | CELL(v)->object.vector.length = len; |
994 | fill_vector (v, init); |
1025 | fill_vector (v, 0, init); |
995 | push_recent_alloc (SCHEME_A_ v, NIL); |
1026 | push_recent_alloc (SCHEME_A_ v, NIL); |
996 | |
1027 | |
997 | return v; |
1028 | return v; |
998 | } |
1029 | } |
999 | |
1030 | |
… | |
… | |
1007 | static void |
1038 | static void |
1008 | check_cell_alloced (pointer p, int expect_alloced) |
1039 | check_cell_alloced (pointer p, int expect_alloced) |
1009 | { |
1040 | { |
1010 | /* 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. */ |
1011 | if (typeflag (p) & !expect_alloced) |
1042 | if (typeflag (p) & !expect_alloced) |
1012 | xwrstr ("Cell is already allocated!\n"); |
1043 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1013 | |
1044 | |
1014 | if (!(typeflag (p)) & expect_alloced) |
1045 | if (!(typeflag (p)) & expect_alloced) |
1015 | xwrstr ("Cell is not allocated!\n"); |
1046 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1016 | } |
1047 | } |
1017 | |
1048 | |
1018 | static void |
1049 | static void |
1019 | check_range_alloced (pointer p, int n, int expect_alloced) |
1050 | check_range_alloced (pointer p, int n, int expect_alloced) |
1020 | { |
1051 | { |
… | |
… | |
1026 | #endif |
1057 | #endif |
1027 | |
1058 | |
1028 | /* Medium level cell allocation */ |
1059 | /* Medium level cell allocation */ |
1029 | |
1060 | |
1030 | /* get new cons cell */ |
1061 | /* get new cons cell */ |
1031 | pointer |
1062 | ecb_hot static pointer |
1032 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1063 | xcons (SCHEME_P_ pointer a, pointer b) |
1033 | { |
1064 | { |
1034 | pointer x = get_cell (SCHEME_A_ a, b); |
1065 | pointer x = get_cell (SCHEME_A_ a, b); |
1035 | |
1066 | |
1036 | set_typeflag (x, T_PAIR); |
1067 | set_typeflag (x, T_PAIR); |
1037 | |
|
|
1038 | if (immutable) |
|
|
1039 | setimmutable (x); |
|
|
1040 | |
1068 | |
1041 | set_car (x, a); |
1069 | set_car (x, a); |
1042 | set_cdr (x, b); |
1070 | set_cdr (x, b); |
1043 | |
1071 | |
1044 | return x; |
1072 | return x; |
1045 | } |
1073 | } |
1046 | |
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 | |
1047 | /* ========== oblist implementation ========== */ |
1095 | /* ========== oblist implementation ========== */ |
1048 | |
1096 | |
1049 | #ifndef USE_OBJECT_LIST |
1097 | #ifndef USE_OBJECT_LIST |
1050 | |
1098 | |
|
|
1099 | static int |
1051 | 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 = 2166136261; |
1052 | |
1104 | |
|
|
1105 | while (*p) |
|
|
1106 | hash = (hash ^ *p++) * 16777619; |
|
|
1107 | |
|
|
1108 | return hash % table_size; |
|
|
1109 | } |
|
|
1110 | |
1053 | static pointer |
1111 | ecb_cold static pointer |
1054 | oblist_initial_value (SCHEME_P) |
1112 | oblist_initial_value (SCHEME_P) |
1055 | { |
1113 | { |
1056 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1114 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1057 | } |
1115 | } |
1058 | |
1116 | |
1059 | /* returns the new symbol */ |
1117 | /* returns the new symbol */ |
1060 | static pointer |
1118 | ecb_cold static pointer |
1061 | oblist_add_by_name (SCHEME_P_ const char *name) |
1119 | oblist_add_by_name (SCHEME_P_ const char *name) |
1062 | { |
1120 | { |
1063 | int location; |
1121 | pointer x = generate_symbol (SCHEME_A_ name); |
1064 | |
|
|
1065 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1066 | set_typeflag (x, T_SYMBOL); |
|
|
1067 | setimmutable (car (x)); |
|
|
1068 | |
|
|
1069 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1122 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1070 | 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))); |
1071 | return x; |
1124 | return x; |
1072 | } |
1125 | } |
1073 | |
1126 | |
1074 | ecb_inline pointer |
1127 | ecb_cold static pointer |
1075 | oblist_find_by_name (SCHEME_P_ const char *name) |
1128 | oblist_find_by_name (SCHEME_P_ const char *name) |
1076 | { |
1129 | { |
1077 | int location; |
1130 | int location; |
1078 | pointer x; |
1131 | pointer x; |
1079 | char *s; |
1132 | char *s; |
1080 | |
1133 | |
1081 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1134 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1082 | |
1135 | |
1083 | 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)) |
1084 | { |
1137 | { |
1085 | s = symname (car (x)); |
1138 | s = symname (car (x)); |
1086 | |
1139 | |
1087 | /* case-insensitive, per R5RS section 2 */ |
1140 | /* case-insensitive, per R5RS section 2 */ |
1088 | if (stricmp (name, s) == 0) |
1141 | if (stricmp (name, s) == 0) |
… | |
… | |
1090 | } |
1143 | } |
1091 | |
1144 | |
1092 | return NIL; |
1145 | return NIL; |
1093 | } |
1146 | } |
1094 | |
1147 | |
1095 | static pointer |
1148 | ecb_cold static pointer |
1096 | oblist_all_symbols (SCHEME_P) |
1149 | oblist_all_symbols (SCHEME_P) |
1097 | { |
1150 | { |
1098 | int i; |
1151 | int i; |
1099 | pointer x; |
1152 | pointer x; |
1100 | pointer ob_list = NIL; |
1153 | pointer ob_list = NIL; |
1101 | |
1154 | |
1102 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1155 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1103 | 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)) |
1104 | ob_list = cons (x, ob_list); |
1157 | ob_list = cons (x, ob_list); |
1105 | |
1158 | |
1106 | return ob_list; |
1159 | return ob_list; |
1107 | } |
1160 | } |
1108 | |
1161 | |
1109 | #else |
1162 | #else |
1110 | |
1163 | |
1111 | static pointer |
1164 | ecb_cold static pointer |
1112 | oblist_initial_value (SCHEME_P) |
1165 | oblist_initial_value (SCHEME_P) |
1113 | { |
1166 | { |
1114 | return NIL; |
1167 | return NIL; |
1115 | } |
1168 | } |
1116 | |
1169 | |
1117 | ecb_inline pointer |
1170 | ecb_cold static pointer |
1118 | oblist_find_by_name (SCHEME_P_ const char *name) |
1171 | oblist_find_by_name (SCHEME_P_ const char *name) |
1119 | { |
1172 | { |
1120 | pointer x; |
1173 | pointer x; |
1121 | char *s; |
1174 | char *s; |
1122 | |
1175 | |
… | |
… | |
1131 | |
1184 | |
1132 | return NIL; |
1185 | return NIL; |
1133 | } |
1186 | } |
1134 | |
1187 | |
1135 | /* returns the new symbol */ |
1188 | /* returns the new symbol */ |
1136 | static pointer |
1189 | ecb_cold static pointer |
1137 | oblist_add_by_name (SCHEME_P_ const char *name) |
1190 | oblist_add_by_name (SCHEME_P_ const char *name) |
1138 | { |
1191 | { |
1139 | pointer x; |
1192 | pointer x = generate_symbol (SCHEME_A_ name); |
1140 | |
|
|
1141 | x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1142 | set_typeflag (x, T_SYMBOL); |
|
|
1143 | setimmutable (car (x)); |
|
|
1144 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1193 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1145 | return x; |
1194 | return x; |
1146 | } |
1195 | } |
1147 | |
1196 | |
1148 | static pointer |
1197 | ecb_cold static pointer |
1149 | oblist_all_symbols (SCHEME_P) |
1198 | oblist_all_symbols (SCHEME_P) |
1150 | { |
1199 | { |
1151 | return SCHEME_V->oblist; |
1200 | return SCHEME_V->oblist; |
1152 | } |
1201 | } |
1153 | |
1202 | |
1154 | #endif |
1203 | #endif |
1155 | |
1204 | |
1156 | #if USE_PORTS |
|
|
1157 | static pointer |
1205 | ecb_cold static pointer |
1158 | mk_port (SCHEME_P_ port *p) |
1206 | mk_port (SCHEME_P_ port *p) |
1159 | { |
1207 | { |
1160 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1208 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1161 | |
1209 | |
1162 | set_typeflag (x, T_PORT | T_ATOM); |
1210 | set_typeflag (x, T_PORT | T_ATOM); |
1163 | x->object.port = p; |
1211 | set_port (x, p); |
1164 | |
1212 | |
1165 | return x; |
1213 | return x; |
1166 | } |
1214 | } |
1167 | #endif |
|
|
1168 | |
1215 | |
1169 | pointer |
1216 | ecb_cold pointer |
1170 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1217 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1171 | { |
1218 | { |
1172 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1219 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1173 | |
1220 | |
1174 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1221 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1175 | x->object.ff = f; |
1222 | CELL(x)->object.ff = f; |
1176 | |
1223 | |
1177 | return x; |
1224 | return x; |
1178 | } |
1225 | } |
1179 | |
1226 | |
1180 | INTERFACE pointer |
1227 | INTERFACE pointer |
1181 | mk_character (SCHEME_P_ int c) |
1228 | mk_character (SCHEME_P_ int c) |
1182 | { |
1229 | { |
1183 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1230 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1184 | |
1231 | |
1185 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1232 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1186 | ivalue_unchecked (x) = c & 0xff; |
1233 | set_ivalue (x, c & 0xff); |
1187 | set_num_integer (x); |
1234 | |
1188 | return x; |
1235 | return x; |
1189 | } |
1236 | } |
1190 | |
1237 | |
1191 | /* get number atom (integer) */ |
1238 | /* get number atom (integer) */ |
1192 | INTERFACE pointer |
1239 | INTERFACE pointer |
1193 | mk_integer (SCHEME_P_ long num) |
1240 | mk_integer (SCHEME_P_ long n) |
1194 | { |
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 | { |
1195 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1252 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1196 | |
1253 | |
1197 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1254 | set_typeflag (x, T_INTEGER | T_ATOM); |
1198 | ivalue_unchecked (x) = num; |
1255 | setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */ |
1199 | set_num_integer (x); |
1256 | set_ivalue (x, n); |
|
|
1257 | |
|
|
1258 | *pp = x; |
|
|
1259 | } |
|
|
1260 | |
1200 | return x; |
1261 | return *pp; |
1201 | } |
1262 | } |
1202 | |
1263 | |
1203 | INTERFACE pointer |
1264 | INTERFACE pointer |
1204 | mk_real (SCHEME_P_ RVALUE n) |
1265 | mk_real (SCHEME_P_ RVALUE n) |
1205 | { |
1266 | { |
|
|
1267 | #if USE_REAL |
1206 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1268 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1207 | |
1269 | |
1208 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1270 | set_typeflag (x, T_REAL | T_ATOM); |
1209 | rvalue_unchecked (x) = n; |
1271 | set_rvalue (x, n); |
1210 | set_num_real (x); |
1272 | |
1211 | return x; |
1273 | return x; |
|
|
1274 | #else |
|
|
1275 | return mk_integer (SCHEME_A_ n); |
|
|
1276 | #endif |
1212 | } |
1277 | } |
1213 | |
1278 | |
1214 | static pointer |
1279 | static pointer |
1215 | mk_number (SCHEME_P_ const num n) |
1280 | mk_number (SCHEME_P_ const num n) |
1216 | { |
1281 | { |
|
|
1282 | #if USE_REAL |
1217 | 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 |
1218 | return mk_integer (SCHEME_A_ num_get_ivalue (n)); |
1287 | return mk_integer (SCHEME_A_ num_ivalue (n)); |
1219 | else |
1288 | #endif |
1220 | return mk_real (SCHEME_A_ num_get_rvalue (n)); |
|
|
1221 | } |
1289 | } |
1222 | |
1290 | |
1223 | /* allocate name to string area */ |
1291 | /* allocate name to string area */ |
1224 | static char * |
1292 | static char * |
1225 | 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) |
… | |
… | |
1231 | SCHEME_V->no_memory = 1; |
1299 | SCHEME_V->no_memory = 1; |
1232 | return SCHEME_V->strbuff; |
1300 | return SCHEME_V->strbuff; |
1233 | } |
1301 | } |
1234 | |
1302 | |
1235 | if (str) |
1303 | if (str) |
1236 | { |
1304 | memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */ |
1237 | int l = strlen (str); |
|
|
1238 | |
|
|
1239 | if (l > len_str) |
|
|
1240 | l = len_str; |
|
|
1241 | |
|
|
1242 | memcpy (q, str, l); |
|
|
1243 | q[l] = 0; |
|
|
1244 | } |
|
|
1245 | else |
1305 | else |
1246 | { |
|
|
1247 | memset (q, fill, len_str); |
1306 | memset (q, fill, len_str); |
|
|
1307 | |
1248 | q[len_str] = 0; |
1308 | q[len_str] = 0; |
1249 | } |
|
|
1250 | |
1309 | |
1251 | return q; |
1310 | return q; |
1252 | } |
1311 | } |
1253 | |
1312 | |
1254 | INTERFACE pointer |
1313 | INTERFACE pointer |
… | |
… | |
1268 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1327 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1269 | |
1328 | |
1270 | set_typeflag (x, T_STRING | T_ATOM); |
1329 | set_typeflag (x, T_STRING | T_ATOM); |
1271 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1330 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1272 | strlength (x) = len; |
1331 | strlength (x) = len; |
|
|
1332 | |
1273 | return x; |
1333 | return x; |
1274 | } |
1334 | } |
1275 | |
1335 | |
1276 | INTERFACE pointer |
1336 | INTERFACE pointer |
1277 | mk_string (SCHEME_P_ const char *str) |
1337 | mk_string (SCHEME_P_ const char *str) |
… | |
… | |
1284 | { |
1344 | { |
1285 | return get_vector_object (SCHEME_A_ len, NIL); |
1345 | return get_vector_object (SCHEME_A_ len, NIL); |
1286 | } |
1346 | } |
1287 | |
1347 | |
1288 | INTERFACE void |
1348 | INTERFACE void |
1289 | fill_vector (pointer vec, pointer obj) |
1349 | fill_vector (pointer vec, uint32_t start, pointer obj) |
1290 | { |
1350 | { |
1291 | int i; |
1351 | int i; |
1292 | |
1352 | |
1293 | for (i = 0; i < vec->object.vector.length; i++) |
1353 | for (i = start; i < veclength (vec); i++) |
1294 | vecvalue (vec)[i] = obj; |
1354 | vecvalue (vec)[i] = obj; |
1295 | } |
1355 | } |
1296 | |
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 | |
1297 | INTERFACE pointer |
1366 | INTERFACE pointer |
1298 | vector_elem (pointer vec, uint32_t ielem) |
1367 | vector_get (pointer vec, uint32_t ielem) |
1299 | { |
1368 | { |
1300 | return vecvalue(vec)[ielem]; |
1369 | return vecvalue(vec)[ielem]; |
1301 | } |
1370 | } |
1302 | |
1371 | |
1303 | INTERFACE void |
1372 | INTERFACE void |
1304 | set_vector_elem (pointer vec, uint32_t ielem, pointer a) |
1373 | vector_set (pointer vec, uint32_t ielem, pointer a) |
1305 | { |
1374 | { |
1306 | vecvalue(vec)[ielem] = a; |
1375 | vecvalue(vec)[ielem] = a; |
1307 | } |
1376 | } |
1308 | |
1377 | |
1309 | /* get new symbol */ |
1378 | /* get new symbol */ |
… | |
… | |
1317 | x = oblist_add_by_name (SCHEME_A_ name); |
1386 | x = oblist_add_by_name (SCHEME_A_ name); |
1318 | |
1387 | |
1319 | return x; |
1388 | return x; |
1320 | } |
1389 | } |
1321 | |
1390 | |
1322 | INTERFACE pointer |
1391 | ecb_cold INTERFACE pointer |
1323 | gensym (SCHEME_P) |
1392 | gensym (SCHEME_P) |
1324 | { |
1393 | { |
1325 | pointer x; |
1394 | pointer x; |
1326 | |
|
|
1327 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
|
|
1328 | { |
|
|
1329 | char name[40] = "gensym-"; |
1395 | char name[40] = "gensym-"; |
1330 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1396 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1331 | |
1397 | |
1332 | /* first check oblist */ |
1398 | return generate_symbol (SCHEME_A_ name); |
1333 | x = oblist_find_by_name (SCHEME_A_ name); |
1399 | } |
1334 | |
1400 | |
1335 | if (x == NIL) |
1401 | static int |
1336 | { |
1402 | is_gensym (SCHEME_P_ pointer x) |
1337 | x = oblist_add_by_name (SCHEME_A_ name); |
1403 | { |
1338 | return x; |
1404 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1339 | } |
|
|
1340 | } |
|
|
1341 | |
|
|
1342 | return NIL; |
|
|
1343 | } |
1405 | } |
1344 | |
1406 | |
1345 | /* make symbol or number atom from string */ |
1407 | /* make symbol or number atom from string */ |
1346 | static pointer |
1408 | ecb_cold static pointer |
1347 | mk_atom (SCHEME_P_ char *q) |
1409 | mk_atom (SCHEME_P_ char *q) |
1348 | { |
1410 | { |
1349 | char c, *p; |
1411 | char c, *p; |
1350 | int has_dec_point = 0; |
1412 | int has_dec_point = 0; |
1351 | int has_fp_exp = 0; |
1413 | int has_fp_exp = 0; |
… | |
… | |
1401 | } |
1463 | } |
1402 | else if ((c == 'e') || (c == 'E')) |
1464 | else if ((c == 'e') || (c == 'E')) |
1403 | { |
1465 | { |
1404 | if (!has_fp_exp) |
1466 | if (!has_fp_exp) |
1405 | { |
1467 | { |
1406 | has_dec_point = 1; /* decimal point illegal |
1468 | has_dec_point = 1; /* decimal point illegal from now on */ |
1407 | from now on */ |
|
|
1408 | p++; |
1469 | p++; |
1409 | |
1470 | |
1410 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1471 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1411 | continue; |
1472 | continue; |
1412 | } |
1473 | } |
… | |
… | |
1423 | |
1484 | |
1424 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1485 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1425 | } |
1486 | } |
1426 | |
1487 | |
1427 | /* make constant */ |
1488 | /* make constant */ |
1428 | static pointer |
1489 | ecb_cold static pointer |
1429 | mk_sharp_const (SCHEME_P_ char *name) |
1490 | mk_sharp_const (SCHEME_P_ char *name) |
1430 | { |
1491 | { |
1431 | if (!strcmp (name, "t")) |
1492 | if (!strcmp (name, "t")) |
1432 | return S_T; |
1493 | return S_T; |
1433 | else if (!strcmp (name, "f")) |
1494 | else if (!strcmp (name, "f")) |
1434 | return S_F; |
1495 | return S_F; |
1435 | else if (*name == '\\') /* #\w (character) */ |
1496 | else if (*name == '\\') /* #\w (character) */ |
1436 | { |
1497 | { |
1437 | int c; |
1498 | int c; |
1438 | |
1499 | |
|
|
1500 | // TODO: optimise |
1439 | if (stricmp (name + 1, "space") == 0) |
1501 | if (stricmp (name + 1, "space") == 0) |
1440 | c = ' '; |
1502 | c = ' '; |
1441 | else if (stricmp (name + 1, "newline") == 0) |
1503 | else if (stricmp (name + 1, "newline") == 0) |
1442 | c = '\n'; |
1504 | c = '\n'; |
1443 | else if (stricmp (name + 1, "return") == 0) |
1505 | else if (stricmp (name + 1, "return") == 0) |
1444 | c = '\r'; |
1506 | c = '\r'; |
1445 | else if (stricmp (name + 1, "tab") == 0) |
1507 | else if (stricmp (name + 1, "tab") == 0) |
1446 | 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; |
1447 | else if (name[1] == 'x' && name[2] != 0) |
1519 | else if (name[1] == 'x' && name[2] != 0) |
1448 | { |
1520 | { |
1449 | long c1 = strtol (name + 2, 0, 16); |
1521 | long c1 = strtol (name + 2, 0, 16); |
1450 | |
1522 | |
1451 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1523 | if (0 <= c1 && c1 <= UCHAR_MAX) |
… | |
… | |
1476 | return NIL; |
1548 | return NIL; |
1477 | } |
1549 | } |
1478 | } |
1550 | } |
1479 | |
1551 | |
1480 | /* ========== 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 | } |
1481 | |
1572 | |
1482 | /*-- |
1573 | /*-- |
1483 | * 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, |
1484 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1575 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1485 | * for marking. |
1576 | * for marking. |
1486 | * |
1577 | * |
1487 | * The exception is vectors - vectors are currently marked recursively, |
1578 | * The exception is vectors - vectors are currently marked recursively, |
1488 | * 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 |
1489 | * word of context in the vector |
1580 | * word of context in the vector |
1490 | */ |
1581 | */ |
1491 | static void |
1582 | ecb_hot static void |
1492 | mark (pointer a) |
1583 | mark (pointer a) |
1493 | { |
1584 | { |
1494 | pointer t, q, p; |
1585 | pointer t, q, p; |
1495 | |
1586 | |
1496 | t = 0; |
1587 | t = 0; |
… | |
… | |
1500 | |
1591 | |
1501 | if (ecb_expect_false (is_vector (p))) |
1592 | if (ecb_expect_false (is_vector (p))) |
1502 | { |
1593 | { |
1503 | int i; |
1594 | int i; |
1504 | |
1595 | |
1505 | for (i = 0; i < p->object.vector.length; i++) |
1596 | for (i = 0; i < veclength (p); i++) |
1506 | mark (vecvalue (p)[i]); |
1597 | mark (vecvalue (p)[i]); |
1507 | } |
1598 | } |
1508 | |
1599 | |
1509 | if (is_atom (p)) |
1600 | if (is_atom (p)) |
1510 | goto E6; |
1601 | goto E6; |
… | |
… | |
1553 | p = q; |
1644 | p = q; |
1554 | goto E6; |
1645 | goto E6; |
1555 | } |
1646 | } |
1556 | } |
1647 | } |
1557 | |
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 | |
1558 | /* garbage collection. parameter a, b is marked. */ |
1691 | /* garbage collection. parameter a, b is marked. */ |
1559 | static void |
1692 | ecb_cold static void |
1560 | gc (SCHEME_P_ pointer a, pointer b) |
1693 | gc (SCHEME_P_ pointer a, pointer b) |
1561 | { |
1694 | { |
1562 | pointer p; |
|
|
1563 | int i; |
1695 | int i; |
1564 | |
1696 | |
1565 | if (SCHEME_V->gc_verbose) |
1697 | if (SCHEME_V->gc_verbose) |
1566 | putstr (SCHEME_A_ "gc..."); |
1698 | putstr (SCHEME_A_ "gc..."); |
1567 | |
1699 | |
… | |
… | |
1583 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1715 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1584 | mark (car (S_SINK)); |
1716 | mark (car (S_SINK)); |
1585 | /* Mark any older stuff above nested C calls */ |
1717 | /* Mark any older stuff above nested C calls */ |
1586 | mark (SCHEME_V->c_nest); |
1718 | mark (SCHEME_V->c_nest); |
1587 | |
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 | |
1588 | /* mark variables a, b */ |
1727 | /* mark variables a, b */ |
1589 | mark (a); |
1728 | mark (a); |
1590 | mark (b); |
1729 | mark (b); |
1591 | |
1730 | |
1592 | /* garbage collect */ |
1731 | /* garbage collect */ |
1593 | clrmark (NIL); |
1732 | clrmark (NIL); |
1594 | SCHEME_V->fcells = 0; |
1733 | SCHEME_V->fcells = 0; |
1595 | SCHEME_V->free_cell = NIL; |
1734 | SCHEME_V->free_cell = NIL; |
1596 | |
1735 | |
1597 | /* free-list is kept sorted by address so as to maintain consecutive |
|
|
1598 | ranges, if possible, for use with vectors. Here we scan the cells |
|
|
1599 | (which are also kept sorted by address) downwards to build the |
|
|
1600 | free-list in sorted order. |
|
|
1601 | */ |
|
|
1602 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1603 | { |
|
|
1604 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1605 | |
|
|
1606 | while (--p >= SCHEME_V->cell_seg[i]) |
|
|
1607 | { |
|
|
1608 | if (is_mark (p)) |
|
|
1609 | clrmark (p); |
|
|
1610 | else |
|
|
1611 | { |
|
|
1612 | /* reclaim cell */ |
|
|
1613 | if (typeflag (p) != T_FREE) |
|
|
1614 | { |
|
|
1615 | finalize_cell (SCHEME_A_ p); |
|
|
1616 | set_typeflag (p, T_FREE); |
|
|
1617 | set_car (p, NIL); |
|
|
1618 | } |
|
|
1619 | |
|
|
1620 | ++SCHEME_V->fcells; |
|
|
1621 | set_cdr (p, SCHEME_V->free_cell); |
|
|
1622 | SCHEME_V->free_cell = p; |
|
|
1623 | } |
|
|
1624 | } |
|
|
1625 | } |
|
|
1626 | |
|
|
1627 | if (SCHEME_V->gc_verbose) |
1736 | if (SCHEME_V->gc_verbose) |
1628 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
1737 | putstr (SCHEME_A_ "freeing..."); |
1629 | } |
|
|
1630 | |
1738 | |
1631 | static void |
1739 | gc_free (SCHEME_A); |
1632 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1633 | { |
|
|
1634 | /* TODO, fast bitmap check? */ |
|
|
1635 | if (is_string (a)) |
|
|
1636 | free (strvalue (a)); |
|
|
1637 | else if (is_vector (a)) |
|
|
1638 | free (vecvalue (a)); |
|
|
1639 | #if USE_PORTS |
|
|
1640 | else if (is_port (a)) |
|
|
1641 | { |
|
|
1642 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
|
|
1643 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1644 | |
|
|
1645 | free (a->object.port); |
|
|
1646 | } |
|
|
1647 | #endif |
|
|
1648 | } |
1740 | } |
1649 | |
1741 | |
1650 | /* ========== Routines for Reading ========== */ |
1742 | /* ========== Routines for Reading ========== */ |
1651 | |
1743 | |
1652 | static int |
1744 | ecb_cold static int |
1653 | file_push (SCHEME_P_ const char *fname) |
1745 | file_push (SCHEME_P_ const char *fname) |
1654 | { |
1746 | { |
1655 | #if USE_PORTS |
|
|
1656 | int fin; |
1747 | int fin; |
1657 | |
1748 | |
1658 | if (SCHEME_V->file_i == MAXFIL - 1) |
1749 | if (SCHEME_V->file_i == MAXFIL - 1) |
1659 | return 0; |
1750 | return 0; |
1660 | |
1751 | |
… | |
… | |
1666 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1757 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1667 | 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; |
1668 | 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; |
1669 | 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; |
1670 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1761 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1671 | 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); |
1672 | |
1763 | |
1673 | #if SHOW_ERROR_LINE |
1764 | #if SHOW_ERROR_LINE |
1674 | 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; |
1675 | |
1766 | |
1676 | if (fname) |
1767 | if (fname) |
1677 | 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); |
1678 | #endif |
1769 | #endif |
1679 | } |
1770 | } |
1680 | |
1771 | |
1681 | return fin >= 0; |
1772 | return fin >= 0; |
1682 | |
|
|
1683 | #else |
|
|
1684 | return 1; |
|
|
1685 | #endif |
|
|
1686 | } |
1773 | } |
1687 | |
1774 | |
1688 | static void |
1775 | ecb_cold static void |
1689 | file_pop (SCHEME_P) |
1776 | file_pop (SCHEME_P) |
1690 | { |
1777 | { |
1691 | if (SCHEME_V->file_i != 0) |
1778 | if (SCHEME_V->file_i != 0) |
1692 | { |
1779 | { |
1693 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1780 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1694 | #if USE_PORTS |
1781 | #if USE_PORTS |
1695 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1782 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1696 | #endif |
1783 | #endif |
1697 | SCHEME_V->file_i--; |
1784 | SCHEME_V->file_i--; |
1698 | 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); |
1699 | } |
1786 | } |
1700 | } |
1787 | } |
1701 | |
1788 | |
1702 | static int |
1789 | ecb_cold static int |
1703 | file_interactive (SCHEME_P) |
1790 | file_interactive (SCHEME_P) |
1704 | { |
1791 | { |
1705 | #if USE_PORTS |
1792 | #if USE_PORTS |
1706 | return SCHEME_V->file_i == 0 |
1793 | return SCHEME_V->file_i == 0 |
1707 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1794 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1708 | && (SCHEME_V->inport->object.port->kind & port_file); |
1795 | && (port (SCHEME_V->inport)->kind & port_file); |
1709 | #else |
1796 | #else |
1710 | return 0; |
1797 | return 0; |
1711 | #endif |
1798 | #endif |
1712 | } |
1799 | } |
1713 | |
1800 | |
1714 | #if USE_PORTS |
1801 | #if USE_PORTS |
1715 | static port * |
1802 | ecb_cold static port * |
1716 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1803 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1717 | { |
1804 | { |
1718 | int fd; |
1805 | int fd; |
1719 | int flags; |
1806 | int flags; |
1720 | char *rw; |
1807 | char *rw; |
… | |
… | |
1743 | # endif |
1830 | # endif |
1744 | |
1831 | |
1745 | return pt; |
1832 | return pt; |
1746 | } |
1833 | } |
1747 | |
1834 | |
1748 | static pointer |
1835 | ecb_cold static pointer |
1749 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1836 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1750 | { |
1837 | { |
1751 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1838 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1752 | |
1839 | |
1753 | if (!pt && USE_ERROR_CHECKING) |
1840 | if (!pt && USE_ERROR_CHECKING) |
1754 | return NIL; |
1841 | return NIL; |
1755 | |
1842 | |
1756 | return mk_port (SCHEME_A_ pt); |
1843 | return mk_port (SCHEME_A_ pt); |
1757 | } |
1844 | } |
1758 | |
1845 | |
1759 | static port * |
1846 | ecb_cold static port * |
1760 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1847 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1761 | { |
1848 | { |
1762 | port *pt = malloc (sizeof *pt); |
1849 | port *pt = malloc (sizeof *pt); |
1763 | |
1850 | |
1764 | if (!pt && USE_ERROR_CHECKING) |
1851 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1769 | pt->rep.stdio.file = f; |
1856 | pt->rep.stdio.file = f; |
1770 | pt->rep.stdio.closeit = 0; |
1857 | pt->rep.stdio.closeit = 0; |
1771 | return pt; |
1858 | return pt; |
1772 | } |
1859 | } |
1773 | |
1860 | |
1774 | static pointer |
1861 | ecb_cold static pointer |
1775 | port_from_file (SCHEME_P_ int f, int prop) |
1862 | port_from_file (SCHEME_P_ int f, int prop) |
1776 | { |
1863 | { |
1777 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1864 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1778 | |
1865 | |
1779 | if (!pt && USE_ERROR_CHECKING) |
1866 | if (!pt && USE_ERROR_CHECKING) |
1780 | return NIL; |
1867 | return NIL; |
1781 | |
1868 | |
1782 | return mk_port (SCHEME_A_ pt); |
1869 | return mk_port (SCHEME_A_ pt); |
1783 | } |
1870 | } |
1784 | |
1871 | |
1785 | static port * |
1872 | ecb_cold static port * |
1786 | 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) |
1787 | { |
1874 | { |
1788 | port *pt = malloc (sizeof (port)); |
1875 | port *pt = malloc (sizeof (port)); |
1789 | |
1876 | |
1790 | if (!pt && USE_ERROR_CHECKING) |
1877 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1796 | pt->rep.string.curr = start; |
1883 | pt->rep.string.curr = start; |
1797 | pt->rep.string.past_the_end = past_the_end; |
1884 | pt->rep.string.past_the_end = past_the_end; |
1798 | return pt; |
1885 | return pt; |
1799 | } |
1886 | } |
1800 | |
1887 | |
1801 | static pointer |
1888 | ecb_cold static pointer |
1802 | 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) |
1803 | { |
1890 | { |
1804 | 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); |
1805 | |
1892 | |
1806 | if (!pt && USE_ERROR_CHECKING) |
1893 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1809 | return mk_port (SCHEME_A_ pt); |
1896 | return mk_port (SCHEME_A_ pt); |
1810 | } |
1897 | } |
1811 | |
1898 | |
1812 | # define BLOCK_SIZE 256 |
1899 | # define BLOCK_SIZE 256 |
1813 | |
1900 | |
1814 | static port * |
1901 | ecb_cold static port * |
1815 | port_rep_from_scratch (SCHEME_P) |
1902 | port_rep_from_scratch (SCHEME_P) |
1816 | { |
1903 | { |
1817 | char *start; |
1904 | char *start; |
1818 | port *pt = malloc (sizeof (port)); |
1905 | port *pt = malloc (sizeof (port)); |
1819 | |
1906 | |
… | |
… | |
1833 | pt->rep.string.curr = start; |
1920 | pt->rep.string.curr = start; |
1834 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1921 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1835 | return pt; |
1922 | return pt; |
1836 | } |
1923 | } |
1837 | |
1924 | |
1838 | static pointer |
1925 | ecb_cold static pointer |
1839 | port_from_scratch (SCHEME_P) |
1926 | port_from_scratch (SCHEME_P) |
1840 | { |
1927 | { |
1841 | port *pt = port_rep_from_scratch (SCHEME_A); |
1928 | port *pt = port_rep_from_scratch (SCHEME_A); |
1842 | |
1929 | |
1843 | if (!pt && USE_ERROR_CHECKING) |
1930 | if (!pt && USE_ERROR_CHECKING) |
1844 | return NIL; |
1931 | return NIL; |
1845 | |
1932 | |
1846 | return mk_port (SCHEME_A_ pt); |
1933 | return mk_port (SCHEME_A_ pt); |
1847 | } |
1934 | } |
1848 | |
1935 | |
1849 | static void |
1936 | ecb_cold static void |
1850 | port_close (SCHEME_P_ pointer p, int flag) |
1937 | port_close (SCHEME_P_ pointer p, int flag) |
1851 | { |
1938 | { |
1852 | port *pt = p->object.port; |
1939 | port *pt = port (p); |
1853 | |
1940 | |
1854 | pt->kind &= ~flag; |
1941 | pt->kind &= ~flag; |
1855 | |
1942 | |
1856 | if ((pt->kind & (port_input | port_output)) == 0) |
1943 | if ((pt->kind & (port_input | port_output)) == 0) |
1857 | { |
1944 | { |
… | |
… | |
1874 | } |
1961 | } |
1875 | } |
1962 | } |
1876 | #endif |
1963 | #endif |
1877 | |
1964 | |
1878 | /* get new character from input file */ |
1965 | /* get new character from input file */ |
1879 | static int |
1966 | ecb_cold static int |
1880 | inchar (SCHEME_P) |
1967 | inchar (SCHEME_P) |
1881 | { |
1968 | { |
1882 | int c; |
1969 | int c; |
1883 | port *pt; |
1970 | port *pt = port (SCHEME_V->inport); |
1884 | |
|
|
1885 | pt = SCHEME_V->inport->object.port; |
|
|
1886 | |
1971 | |
1887 | if (pt->kind & port_saw_EOF) |
1972 | if (pt->kind & port_saw_EOF) |
1888 | return EOF; |
1973 | return EOF; |
1889 | |
1974 | |
1890 | c = basic_inchar (pt); |
1975 | c = basic_inchar (pt); |
… | |
… | |
1900 | } |
1985 | } |
1901 | |
1986 | |
1902 | return c; |
1987 | return c; |
1903 | } |
1988 | } |
1904 | |
1989 | |
1905 | static int ungot = -1; |
1990 | ecb_cold static int |
1906 | |
|
|
1907 | static int |
|
|
1908 | basic_inchar (port *pt) |
1991 | basic_inchar (port *pt) |
1909 | { |
1992 | { |
1910 | #if USE_PORTS |
|
|
1911 | if (pt->unget != -1) |
1993 | if (pt->unget != -1) |
1912 | { |
1994 | { |
1913 | int r = pt->unget; |
1995 | int r = pt->unget; |
1914 | pt->unget = -1; |
1996 | pt->unget = -1; |
1915 | return r; |
1997 | return r; |
1916 | } |
1998 | } |
1917 | |
1999 | |
|
|
2000 | #if USE_PORTS |
1918 | if (pt->kind & port_file) |
2001 | if (pt->kind & port_file) |
1919 | { |
2002 | { |
1920 | char c; |
2003 | char c; |
1921 | |
2004 | |
1922 | if (!read (pt->rep.stdio.file, &c, 1)) |
2005 | if (!read (pt->rep.stdio.file, &c, 1)) |
… | |
… | |
1930 | return EOF; |
2013 | return EOF; |
1931 | else |
2014 | else |
1932 | return *pt->rep.string.curr++; |
2015 | return *pt->rep.string.curr++; |
1933 | } |
2016 | } |
1934 | #else |
2017 | #else |
1935 | if (ungot == -1) |
|
|
1936 | { |
|
|
1937 | char c; |
2018 | char c; |
1938 | if (!read (0, &c, 1)) |
2019 | |
|
|
2020 | if (!read (pt->rep.stdio.file, &c, 1)) |
1939 | return EOF; |
2021 | return EOF; |
1940 | |
2022 | |
1941 | ungot = c; |
|
|
1942 | } |
|
|
1943 | |
|
|
1944 | { |
|
|
1945 | int r = ungot; |
|
|
1946 | ungot = -1; |
|
|
1947 | return r; |
2023 | return c; |
1948 | } |
|
|
1949 | #endif |
2024 | #endif |
1950 | } |
2025 | } |
1951 | |
2026 | |
1952 | /* back character to input buffer */ |
2027 | /* back character to input buffer */ |
1953 | static void |
2028 | ecb_cold static void |
1954 | backchar (SCHEME_P_ int c) |
2029 | backchar (SCHEME_P_ int c) |
1955 | { |
2030 | { |
1956 | #if USE_PORTS |
2031 | port *pt = port (SCHEME_V->inport); |
1957 | port *pt; |
|
|
1958 | |
2032 | |
1959 | if (c == EOF) |
2033 | if (c == EOF) |
1960 | return; |
2034 | return; |
1961 | |
2035 | |
1962 | pt = SCHEME_V->inport->object.port; |
|
|
1963 | pt->unget = c; |
2036 | pt->unget = c; |
1964 | #else |
|
|
1965 | if (c == EOF) |
|
|
1966 | return; |
|
|
1967 | |
|
|
1968 | ungot = c; |
|
|
1969 | #endif |
|
|
1970 | } |
2037 | } |
1971 | |
2038 | |
1972 | #if USE_PORTS |
2039 | #if USE_PORTS |
1973 | static int |
2040 | ecb_cold static int |
1974 | realloc_port_string (SCHEME_P_ port *p) |
2041 | realloc_port_string (SCHEME_P_ port *p) |
1975 | { |
2042 | { |
1976 | char *start = p->rep.string.start; |
2043 | char *start = p->rep.string.start; |
1977 | 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; |
1978 | char *str = malloc (new_size); |
2045 | char *str = malloc (new_size); |
… | |
… | |
1991 | else |
2058 | else |
1992 | return 0; |
2059 | return 0; |
1993 | } |
2060 | } |
1994 | #endif |
2061 | #endif |
1995 | |
2062 | |
1996 | INTERFACE void |
2063 | ecb_cold static void |
1997 | putstr (SCHEME_P_ const char *s) |
2064 | putchars (SCHEME_P_ const char *s, int len) |
1998 | { |
2065 | { |
|
|
2066 | port *pt = port (SCHEME_V->outport); |
|
|
2067 | |
1999 | #if USE_PORTS |
2068 | #if USE_PORTS |
2000 | port *pt = SCHEME_V->outport->object.port; |
|
|
2001 | |
|
|
2002 | if (pt->kind & port_file) |
|
|
2003 | write (pt->rep.stdio.file, s, strlen (s)); |
|
|
2004 | else |
|
|
2005 | for (; *s; s++) |
|
|
2006 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2007 | *pt->rep.string.curr++ = *s; |
|
|
2008 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2009 | *pt->rep.string.curr++ = *s; |
|
|
2010 | |
|
|
2011 | #else |
|
|
2012 | xwrstr (s); |
|
|
2013 | #endif |
|
|
2014 | } |
|
|
2015 | |
|
|
2016 | static void |
|
|
2017 | putchars (SCHEME_P_ const char *s, int len) |
|
|
2018 | { |
|
|
2019 | #if USE_PORTS |
|
|
2020 | port *pt = SCHEME_V->outport->object.port; |
|
|
2021 | |
|
|
2022 | if (pt->kind & port_file) |
2069 | if (pt->kind & port_file) |
2023 | write (pt->rep.stdio.file, s, len); |
2070 | write (pt->rep.stdio.file, s, len); |
2024 | else |
2071 | else |
2025 | { |
2072 | { |
2026 | for (; len; len--) |
2073 | for (; len; len--) |
… | |
… | |
2031 | *pt->rep.string.curr++ = *s++; |
2078 | *pt->rep.string.curr++ = *s++; |
2032 | } |
2079 | } |
2033 | } |
2080 | } |
2034 | |
2081 | |
2035 | #else |
2082 | #else |
2036 | write (1, s, len); |
2083 | write (1, s, len); // output not initialised |
2037 | #endif |
2084 | #endif |
|
|
2085 | } |
|
|
2086 | |
|
|
2087 | INTERFACE void |
|
|
2088 | putstr (SCHEME_P_ const char *s) |
|
|
2089 | { |
|
|
2090 | putchars (SCHEME_A_ s, strlen (s)); |
2038 | } |
2091 | } |
2039 | |
2092 | |
2040 | INTERFACE void |
2093 | INTERFACE void |
2041 | putcharacter (SCHEME_P_ int c) |
2094 | putcharacter (SCHEME_P_ int c) |
2042 | { |
2095 | { |
2043 | #if USE_PORTS |
|
|
2044 | port *pt = SCHEME_V->outport->object.port; |
|
|
2045 | |
|
|
2046 | if (pt->kind & port_file) |
|
|
2047 | { |
|
|
2048 | char cc = c; |
|
|
2049 | write (pt->rep.stdio.file, &cc, 1); |
|
|
2050 | } |
|
|
2051 | else |
|
|
2052 | { |
|
|
2053 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2054 | *pt->rep.string.curr++ = c; |
|
|
2055 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2056 | *pt->rep.string.curr++ = c; |
|
|
2057 | } |
|
|
2058 | |
|
|
2059 | #else |
|
|
2060 | char cc = c; |
2096 | char cc = c; |
2061 | write (1, &c, 1); |
2097 | |
2062 | #endif |
2098 | putchars (SCHEME_A_ &cc, 1); |
2063 | } |
2099 | } |
2064 | |
2100 | |
2065 | /* read characters up to delimiter, but cater to character constants */ |
2101 | /* read characters up to delimiter, but cater to character constants */ |
2066 | static char * |
2102 | ecb_cold static char * |
2067 | readstr_upto (SCHEME_P_ char *delim) |
2103 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2068 | { |
2104 | { |
2069 | char *p = SCHEME_V->strbuff; |
2105 | char *p = SCHEME_V->strbuff + skip; |
2070 | |
2106 | |
2071 | 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)))); |
2072 | |
2108 | |
2073 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2109 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2074 | *p = 0; |
2110 | *p = 0; |
… | |
… | |
2080 | |
2116 | |
2081 | return SCHEME_V->strbuff; |
2117 | return SCHEME_V->strbuff; |
2082 | } |
2118 | } |
2083 | |
2119 | |
2084 | /* read string expression "xxx...xxx" */ |
2120 | /* read string expression "xxx...xxx" */ |
2085 | static pointer |
2121 | ecb_cold static pointer |
2086 | readstrexp (SCHEME_P) |
2122 | readstrexp (SCHEME_P_ char delim) |
2087 | { |
2123 | { |
2088 | char *p = SCHEME_V->strbuff; |
2124 | char *p = SCHEME_V->strbuff; |
2089 | int c; |
2125 | int c; |
2090 | int c1 = 0; |
2126 | int c1 = 0; |
2091 | enum |
|
|
2092 | { 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; |
2093 | |
2128 | |
2094 | for (;;) |
2129 | for (;;) |
2095 | { |
2130 | { |
2096 | c = inchar (SCHEME_A); |
2131 | c = inchar (SCHEME_A); |
2097 | |
2132 | |
… | |
… | |
2099 | return S_F; |
2134 | return S_F; |
2100 | |
2135 | |
2101 | switch (state) |
2136 | switch (state) |
2102 | { |
2137 | { |
2103 | case st_ok: |
2138 | case st_ok: |
2104 | switch (c) |
2139 | if (ecb_expect_false (c == delim)) |
2105 | { |
|
|
2106 | case '\\': |
|
|
2107 | state = st_bsl; |
|
|
2108 | break; |
|
|
2109 | |
|
|
2110 | case '"': |
|
|
2111 | *p = 0; |
|
|
2112 | 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); |
2113 | |
2141 | |
2114 | default: |
2142 | if (ecb_expect_false (c == '\\')) |
|
|
2143 | state = st_bsl; |
|
|
2144 | else |
2115 | *p++ = c; |
2145 | *p++ = c; |
2116 | break; |
|
|
2117 | } |
|
|
2118 | |
2146 | |
2119 | break; |
2147 | break; |
2120 | |
2148 | |
2121 | case st_bsl: |
2149 | case st_bsl: |
2122 | switch (c) |
2150 | switch (c) |
… | |
… | |
2131 | case '7': |
2159 | case '7': |
2132 | state = st_oct1; |
2160 | state = st_oct1; |
2133 | c1 = c - '0'; |
2161 | c1 = c - '0'; |
2134 | break; |
2162 | break; |
2135 | |
2163 | |
|
|
2164 | case 'a': *p++ = '\a'; state = st_ok; break; |
|
|
2165 | case 'n': *p++ = '\n'; state = st_ok; break; |
|
|
2166 | case 'r': *p++ = '\r'; state = st_ok; break; |
|
|
2167 | case 't': *p++ = '\t'; state = st_ok; break; |
|
|
2168 | |
|
|
2169 | case '\\': |
|
|
2170 | skipspace (SCHEME_A); |
|
|
2171 | break; |
|
|
2172 | |
|
|
2173 | //TODO: x should end in ;, not two-digit hex |
2136 | case 'x': |
2174 | case 'x': |
2137 | case 'X': |
2175 | case 'X': |
2138 | state = st_x1; |
2176 | state = st_x1; |
2139 | c1 = 0; |
2177 | c1 = 0; |
2140 | break; |
2178 | break; |
2141 | |
2179 | |
2142 | case 'n': |
|
|
2143 | *p++ = '\n'; |
|
|
2144 | state = st_ok; |
|
|
2145 | break; |
|
|
2146 | |
|
|
2147 | case 't': |
|
|
2148 | *p++ = '\t'; |
|
|
2149 | state = st_ok; |
|
|
2150 | break; |
|
|
2151 | |
|
|
2152 | case 'r': |
|
|
2153 | *p++ = '\r'; |
|
|
2154 | state = st_ok; |
|
|
2155 | break; |
|
|
2156 | |
|
|
2157 | case '"': |
|
|
2158 | *p++ = '"'; |
|
|
2159 | state = st_ok; |
|
|
2160 | break; |
|
|
2161 | |
|
|
2162 | default: |
2180 | default: |
2163 | *p++ = c; |
2181 | *p++ = c; |
2164 | state = st_ok; |
2182 | state = st_ok; |
2165 | break; |
2183 | break; |
2166 | } |
2184 | } |
2167 | |
2185 | |
2168 | break; |
2186 | break; |
2169 | |
2187 | |
2170 | case st_x1: |
2188 | case st_x1: |
2171 | case st_x2: |
2189 | case st_x2: |
2172 | c = toupper (c); |
2190 | c = tolower (c); |
2173 | |
2191 | |
2174 | if (c >= '0' && c <= 'F') |
2192 | if (c >= '0' && c <= '9') |
2175 | { |
|
|
2176 | if (c <= '9') |
|
|
2177 | c1 = (c1 << 4) + c - '0'; |
2193 | c1 = (c1 << 4) + c - '0'; |
2178 | else |
2194 | else if (c >= 'a' && c <= 'f') |
2179 | c1 = (c1 << 4) + c - 'A' + 10; |
2195 | c1 = (c1 << 4) + c - 'a' + 10; |
2180 | |
|
|
2181 | if (state == st_x1) |
|
|
2182 | state = st_x2; |
|
|
2183 | else |
|
|
2184 | { |
|
|
2185 | *p++ = c1; |
|
|
2186 | state = st_ok; |
|
|
2187 | } |
|
|
2188 | } |
|
|
2189 | else |
2196 | else |
2190 | return S_F; |
2197 | return S_F; |
|
|
2198 | |
|
|
2199 | if (state == st_x1) |
|
|
2200 | state = st_x2; |
|
|
2201 | else |
|
|
2202 | { |
|
|
2203 | *p++ = c1; |
|
|
2204 | state = st_ok; |
|
|
2205 | } |
2191 | |
2206 | |
2192 | break; |
2207 | break; |
2193 | |
2208 | |
2194 | case st_oct1: |
2209 | case st_oct1: |
2195 | case st_oct2: |
2210 | case st_oct2: |
… | |
… | |
2199 | backchar (SCHEME_A_ c); |
2214 | backchar (SCHEME_A_ c); |
2200 | state = st_ok; |
2215 | state = st_ok; |
2201 | } |
2216 | } |
2202 | else |
2217 | else |
2203 | { |
2218 | { |
2204 | if (state == st_oct2 && c1 >= 32) |
2219 | if (state == st_oct2 && c1 >= ' ') |
2205 | return S_F; |
2220 | return S_F; |
2206 | |
2221 | |
2207 | c1 = (c1 << 3) + (c - '0'); |
2222 | c1 = (c1 << 3) + (c - '0'); |
2208 | |
2223 | |
2209 | if (state == st_oct1) |
2224 | if (state == st_oct1) |
… | |
… | |
2214 | state = st_ok; |
2229 | state = st_ok; |
2215 | } |
2230 | } |
2216 | } |
2231 | } |
2217 | |
2232 | |
2218 | break; |
2233 | break; |
2219 | |
|
|
2220 | } |
2234 | } |
2221 | } |
2235 | } |
2222 | } |
2236 | } |
2223 | |
2237 | |
2224 | /* check c is in chars */ |
2238 | /* check c is in chars */ |
2225 | ecb_inline int |
2239 | ecb_cold int |
2226 | is_one_of (char *s, int c) |
2240 | is_one_of (const char *s, int c) |
2227 | { |
2241 | { |
2228 | if (c == EOF) |
|
|
2229 | return 1; |
|
|
2230 | |
|
|
2231 | return !!strchr (s, c); |
2242 | return c == EOF || !!strchr (s, c); |
2232 | } |
2243 | } |
2233 | |
2244 | |
2234 | /* skip white characters */ |
2245 | /* skip white characters */ |
2235 | ecb_inline int |
2246 | ecb_cold int |
2236 | skipspace (SCHEME_P) |
2247 | skipspace (SCHEME_P) |
2237 | { |
2248 | { |
2238 | int c, curr_line = 0; |
2249 | int c, curr_line = 0; |
2239 | |
2250 | |
2240 | do |
2251 | do |
2241 | { |
2252 | { |
2242 | c = inchar (SCHEME_A); |
2253 | c = inchar (SCHEME_A); |
|
|
2254 | |
2243 | #if SHOW_ERROR_LINE |
2255 | #if SHOW_ERROR_LINE |
2244 | if (c == '\n') |
2256 | if (ecb_expect_false (c == '\n')) |
2245 | curr_line++; |
2257 | curr_line++; |
2246 | #endif |
2258 | #endif |
|
|
2259 | |
|
|
2260 | if (ecb_expect_false (c == EOF)) |
|
|
2261 | return c; |
2247 | } |
2262 | } |
2248 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2263 | while (is_one_of (WHITESPACE, c)); |
2249 | |
2264 | |
2250 | /* record it */ |
2265 | /* record it */ |
2251 | #if SHOW_ERROR_LINE |
2266 | #if SHOW_ERROR_LINE |
2252 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2267 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2253 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2268 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2254 | #endif |
2269 | #endif |
2255 | |
2270 | |
2256 | if (c != EOF) |
|
|
2257 | { |
|
|
2258 | backchar (SCHEME_A_ c); |
2271 | backchar (SCHEME_A_ c); |
2259 | return 1; |
2272 | return 1; |
2260 | } |
|
|
2261 | else |
|
|
2262 | return EOF; |
|
|
2263 | } |
2273 | } |
2264 | |
2274 | |
2265 | /* get token */ |
2275 | /* get token */ |
2266 | static int |
2276 | ecb_cold static int |
2267 | token (SCHEME_P) |
2277 | token (SCHEME_P) |
2268 | { |
2278 | { |
2269 | int c = skipspace (SCHEME_A); |
2279 | int c = skipspace (SCHEME_A); |
2270 | |
2280 | |
2271 | if (c == EOF) |
2281 | if (c == EOF) |
… | |
… | |
2283 | return TOK_RPAREN; |
2293 | return TOK_RPAREN; |
2284 | |
2294 | |
2285 | case '.': |
2295 | case '.': |
2286 | c = inchar (SCHEME_A); |
2296 | c = inchar (SCHEME_A); |
2287 | |
2297 | |
2288 | if (is_one_of (" \n\t", c)) |
2298 | if (is_one_of (WHITESPACE, c)) |
2289 | return TOK_DOT; |
2299 | return TOK_DOT; |
2290 | else |
2300 | else |
2291 | { |
2301 | { |
2292 | //TODO: ungetc twice in a row is not supported in C |
|
|
2293 | backchar (SCHEME_A_ c); |
2302 | backchar (SCHEME_A_ c); |
2294 | backchar (SCHEME_A_ '.'); |
|
|
2295 | return TOK_ATOM; |
2303 | return TOK_DOTATOM; |
2296 | } |
2304 | } |
|
|
2305 | |
|
|
2306 | case '|': |
|
|
2307 | return TOK_STRATOM; |
2297 | |
2308 | |
2298 | case '\'': |
2309 | case '\'': |
2299 | return TOK_QUOTE; |
2310 | return TOK_QUOTE; |
2300 | |
2311 | |
2301 | case ';': |
2312 | case ';': |
… | |
… | |
2368 | } |
2379 | } |
2369 | |
2380 | |
2370 | /* ========== Routines for Printing ========== */ |
2381 | /* ========== Routines for Printing ========== */ |
2371 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2382 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2372 | |
2383 | |
2373 | static void |
2384 | ecb_cold static void |
2374 | printslashstring (SCHEME_P_ char *p, int len) |
2385 | printslashstring (SCHEME_P_ char *p, int len) |
2375 | { |
2386 | { |
2376 | int i; |
2387 | int i; |
2377 | unsigned char *s = (unsigned char *) p; |
2388 | unsigned char *s = (unsigned char *) p; |
2378 | |
2389 | |
… | |
… | |
2433 | } |
2444 | } |
2434 | |
2445 | |
2435 | putcharacter (SCHEME_A_ '"'); |
2446 | putcharacter (SCHEME_A_ '"'); |
2436 | } |
2447 | } |
2437 | |
2448 | |
2438 | |
|
|
2439 | /* print atoms */ |
2449 | /* print atoms */ |
2440 | static void |
2450 | ecb_cold static void |
2441 | printatom (SCHEME_P_ pointer l, int f) |
2451 | printatom (SCHEME_P_ pointer l, int f) |
2442 | { |
2452 | { |
2443 | char *p; |
2453 | char *p; |
2444 | int len; |
2454 | int len; |
2445 | |
2455 | |
2446 | atom2str (SCHEME_A_ l, f, &p, &len); |
2456 | atom2str (SCHEME_A_ l, f, &p, &len); |
2447 | putchars (SCHEME_A_ p, len); |
2457 | putchars (SCHEME_A_ p, len); |
2448 | } |
2458 | } |
2449 | |
2459 | |
2450 | |
|
|
2451 | /* Uses internal buffer unless string pointer is already available */ |
2460 | /* Uses internal buffer unless string pointer is already available */ |
2452 | static void |
2461 | ecb_cold static void |
2453 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2462 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2454 | { |
2463 | { |
2455 | char *p; |
2464 | char *p; |
2456 | |
2465 | |
2457 | if (l == NIL) |
2466 | if (l == NIL) |
… | |
… | |
2615 | #endif |
2624 | #endif |
2616 | } |
2625 | } |
2617 | else if (is_continuation (l)) |
2626 | else if (is_continuation (l)) |
2618 | p = "#<CONTINUATION>"; |
2627 | p = "#<CONTINUATION>"; |
2619 | else |
2628 | else |
|
|
2629 | { |
|
|
2630 | #if USE_PRINTF |
|
|
2631 | p = SCHEME_V->strbuff; |
|
|
2632 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2633 | #else |
2620 | p = "#<ERROR>"; |
2634 | p = "#<ERROR>"; |
|
|
2635 | #endif |
|
|
2636 | } |
2621 | |
2637 | |
2622 | *pp = p; |
2638 | *pp = p; |
2623 | *plen = strlen (p); |
2639 | *plen = strlen (p); |
2624 | } |
2640 | } |
2625 | |
2641 | |
… | |
… | |
2657 | return car (d); |
2673 | return car (d); |
2658 | |
2674 | |
2659 | p = cons (car (d), cdr (d)); |
2675 | p = cons (car (d), cdr (d)); |
2660 | q = p; |
2676 | q = p; |
2661 | |
2677 | |
2662 | while (cdr (cdr (p)) != NIL) |
2678 | while (cddr (p) != NIL) |
2663 | { |
2679 | { |
2664 | d = cons (car (p), cdr (p)); |
2680 | d = cons (car (p), cdr (p)); |
2665 | |
2681 | |
2666 | if (cdr (cdr (p)) != NIL) |
2682 | if (cddr (p) != NIL) |
2667 | p = cdr (d); |
2683 | p = cdr (d); |
2668 | } |
2684 | } |
2669 | |
2685 | |
2670 | set_cdr (p, car (cdr (p))); |
2686 | set_cdr (p, cadr (p)); |
2671 | return q; |
2687 | return q; |
2672 | } |
2688 | } |
2673 | |
2689 | |
2674 | /* reverse list -- produce new list */ |
2690 | /* reverse list -- produce new list */ |
2675 | static pointer |
2691 | ecb_hot static pointer |
2676 | reverse (SCHEME_P_ pointer a) |
2692 | reverse (SCHEME_P_ pointer a) |
2677 | { |
2693 | { |
2678 | /* a must be checked by gc */ |
2694 | /* a must be checked by gc */ |
2679 | pointer p = NIL; |
2695 | pointer p = NIL; |
2680 | |
2696 | |
… | |
… | |
2683 | |
2699 | |
2684 | return p; |
2700 | return p; |
2685 | } |
2701 | } |
2686 | |
2702 | |
2687 | /* reverse list --- in-place */ |
2703 | /* reverse list --- in-place */ |
2688 | static pointer |
2704 | ecb_hot static pointer |
2689 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2705 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2690 | { |
2706 | { |
2691 | pointer result = term; |
2707 | pointer result = term; |
2692 | pointer p = list; |
2708 | pointer p = list; |
2693 | |
2709 | |
… | |
… | |
2701 | |
2717 | |
2702 | return result; |
2718 | return result; |
2703 | } |
2719 | } |
2704 | |
2720 | |
2705 | /* append list -- produce new list (in reverse order) */ |
2721 | /* append list -- produce new list (in reverse order) */ |
2706 | static pointer |
2722 | ecb_hot static pointer |
2707 | revappend (SCHEME_P_ pointer a, pointer b) |
2723 | revappend (SCHEME_P_ pointer a, pointer b) |
2708 | { |
2724 | { |
2709 | pointer result = a; |
2725 | pointer result = a; |
2710 | pointer p = b; |
2726 | pointer p = b; |
2711 | |
2727 | |
… | |
… | |
2720 | |
2736 | |
2721 | return S_F; /* signal an error */ |
2737 | return S_F; /* signal an error */ |
2722 | } |
2738 | } |
2723 | |
2739 | |
2724 | /* equivalence of atoms */ |
2740 | /* equivalence of atoms */ |
2725 | int |
2741 | ecb_hot int |
2726 | eqv (pointer a, pointer b) |
2742 | eqv (pointer a, pointer b) |
2727 | { |
2743 | { |
2728 | if (is_string (a)) |
2744 | if (is_string (a)) |
2729 | { |
2745 | { |
2730 | if (is_string (b)) |
2746 | if (is_string (b)) |
… | |
… | |
2769 | /* () is #t in R5RS */ |
2785 | /* () is #t in R5RS */ |
2770 | #define is_true(p) ((p) != S_F) |
2786 | #define is_true(p) ((p) != S_F) |
2771 | #define is_false(p) ((p) == S_F) |
2787 | #define is_false(p) ((p) == S_F) |
2772 | |
2788 | |
2773 | /* ========== Environment implementation ========== */ |
2789 | /* ========== Environment implementation ========== */ |
2774 | |
|
|
2775 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
|
|
2776 | |
|
|
2777 | static int |
|
|
2778 | hash_fn (const char *key, int table_size) |
|
|
2779 | { |
|
|
2780 | const unsigned char *p = key; |
|
|
2781 | uint32_t hash = 2166136261; |
|
|
2782 | |
|
|
2783 | while (*p) |
|
|
2784 | hash = (hash ^ *p++) * 16777619; |
|
|
2785 | |
|
|
2786 | return hash % table_size; |
|
|
2787 | } |
|
|
2788 | #endif |
|
|
2789 | |
2790 | |
2790 | #ifndef USE_ALIST_ENV |
2791 | #ifndef USE_ALIST_ENV |
2791 | |
2792 | |
2792 | /* |
2793 | /* |
2793 | * In this implementation, each frame of the environment may be |
2794 | * In this implementation, each frame of the environment may be |
… | |
… | |
2810 | |
2811 | |
2811 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2812 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2812 | setenvironment (SCHEME_V->envir); |
2813 | setenvironment (SCHEME_V->envir); |
2813 | } |
2814 | } |
2814 | |
2815 | |
|
|
2816 | static uint32_t |
|
|
2817 | sym_hash (pointer sym, uint32_t size) |
|
|
2818 | { |
|
|
2819 | uintptr_t ptr = (uintptr_t)sym; |
|
|
2820 | |
|
|
2821 | #if 0 |
|
|
2822 | /* table size is prime, so why mix */ |
|
|
2823 | ptr += ptr >> 32; |
|
|
2824 | ptr += ptr >> 16; |
|
|
2825 | ptr += ptr >> 8; |
|
|
2826 | #endif |
|
|
2827 | |
|
|
2828 | return ptr % size; |
|
|
2829 | } |
|
|
2830 | |
2815 | ecb_inline void |
2831 | ecb_inline void |
2816 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2832 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2817 | { |
2833 | { |
2818 | pointer slot = immutable_cons (variable, value); |
2834 | pointer slot = immutable_cons (variable, value); |
2819 | |
2835 | |
2820 | if (is_vector (car (env))) |
2836 | if (is_vector (car (env))) |
2821 | { |
2837 | { |
2822 | int location = hash_fn (symname (variable), veclength (car (env))); |
2838 | int location = sym_hash (variable, veclength (car (env))); |
2823 | |
|
|
2824 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
2839 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2825 | } |
2840 | } |
2826 | else |
2841 | else |
2827 | set_car (env, immutable_cons (slot, car (env))); |
2842 | set_car (env, immutable_cons (slot, car (env))); |
2828 | } |
2843 | } |
2829 | |
2844 | |
2830 | static pointer |
2845 | ecb_hot static pointer |
2831 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2846 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2832 | { |
2847 | { |
2833 | pointer x, y; |
2848 | pointer x, y; |
2834 | int location; |
|
|
2835 | |
2849 | |
2836 | for (x = env; x != NIL; x = cdr (x)) |
2850 | for (x = env; x != NIL; x = cdr (x)) |
2837 | { |
2851 | { |
2838 | if (is_vector (car (x))) |
2852 | if (is_vector (car (x))) |
2839 | { |
2853 | { |
2840 | location = hash_fn (symname (hdl), veclength (car (x))); |
2854 | int location = sym_hash (hdl, veclength (car (x))); |
2841 | y = vector_elem (car (x), location); |
2855 | y = vector_get (car (x), location); |
2842 | } |
2856 | } |
2843 | else |
2857 | else |
2844 | y = car (x); |
2858 | y = car (x); |
2845 | |
2859 | |
2846 | for (; y != NIL; y = cdr (y)) |
2860 | for (; y != NIL; y = cdr (y)) |
2847 | if (caar (y) == hdl) |
2861 | if (caar (y) == hdl) |
2848 | break; |
2862 | break; |
2849 | |
2863 | |
2850 | if (y != NIL) |
2864 | if (y != NIL) |
|
|
2865 | return car (y); |
|
|
2866 | |
|
|
2867 | if (!all) |
2851 | break; |
2868 | break; |
2852 | |
|
|
2853 | if (!all) |
|
|
2854 | return NIL; |
|
|
2855 | } |
2869 | } |
2856 | |
|
|
2857 | if (x != NIL) |
|
|
2858 | return car (y); |
|
|
2859 | |
2870 | |
2860 | return NIL; |
2871 | return NIL; |
2861 | } |
2872 | } |
2862 | |
2873 | |
2863 | #else /* USE_ALIST_ENV */ |
2874 | #else /* USE_ALIST_ENV */ |
2864 | |
2875 | |
2865 | ecb_inline void |
2876 | static void |
2866 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2877 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2867 | { |
2878 | { |
2868 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2879 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2869 | setenvironment (SCHEME_V->envir); |
2880 | setenvironment (SCHEME_V->envir); |
2870 | } |
2881 | } |
2871 | |
2882 | |
2872 | ecb_inline void |
2883 | static void |
2873 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2884 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2874 | { |
2885 | { |
2875 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2886 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2876 | } |
2887 | } |
2877 | |
2888 | |
2878 | static pointer |
2889 | ecb_hot static pointer |
2879 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2890 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2880 | { |
2891 | { |
2881 | pointer x, y; |
2892 | pointer x, y; |
2882 | |
2893 | |
2883 | for (x = env; x != NIL; x = cdr (x)) |
2894 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2885 | for (y = car (x); y != NIL; y = cdr (y)) |
2896 | for (y = car (x); y != NIL; y = cdr (y)) |
2886 | if (caar (y) == hdl) |
2897 | if (caar (y) == hdl) |
2887 | break; |
2898 | break; |
2888 | |
2899 | |
2889 | if (y != NIL) |
2900 | if (y != NIL) |
|
|
2901 | return car (y); |
2890 | break; |
2902 | break; |
2891 | |
2903 | |
2892 | if (!all) |
2904 | if (!all) |
2893 | return NIL; |
2905 | break; |
2894 | } |
2906 | } |
2895 | |
|
|
2896 | if (x != NIL) |
|
|
2897 | return car (y); |
|
|
2898 | |
2907 | |
2899 | return NIL; |
2908 | return NIL; |
2900 | } |
2909 | } |
2901 | |
2910 | |
2902 | #endif /* USE_ALIST_ENV else */ |
2911 | #endif /* USE_ALIST_ENV else */ |
2903 | |
2912 | |
2904 | ecb_inline void |
2913 | static void |
2905 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2914 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2906 | { |
2915 | { |
|
|
2916 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2907 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2917 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2908 | } |
2918 | } |
2909 | |
2919 | |
2910 | ecb_inline void |
2920 | static void |
2911 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2921 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2912 | { |
2922 | { |
2913 | set_cdr (slot, value); |
2923 | set_cdr (slot, value); |
2914 | } |
2924 | } |
2915 | |
2925 | |
2916 | ecb_inline pointer |
2926 | static pointer |
2917 | slot_value_in_env (pointer slot) |
2927 | slot_value_in_env (pointer slot) |
2918 | { |
2928 | { |
2919 | return cdr (slot); |
2929 | return cdr (slot); |
2920 | } |
2930 | } |
2921 | |
2931 | |
2922 | /* ========== Evaluation Cycle ========== */ |
2932 | /* ========== Evaluation Cycle ========== */ |
2923 | |
2933 | |
2924 | static int |
2934 | ecb_cold static int |
2925 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2935 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2926 | { |
2936 | { |
2927 | #if USE_ERROR_HOOK |
2937 | #if USE_ERROR_HOOK |
2928 | pointer x; |
2938 | pointer x; |
2929 | pointer hdl = SCHEME_V->ERROR_HOOK; |
2939 | pointer hdl = SCHEME_V->ERROR_HOOK; |
… | |
… | |
3005 | pointer code; |
3015 | pointer code; |
3006 | }; |
3016 | }; |
3007 | |
3017 | |
3008 | # define STACK_GROWTH 3 |
3018 | # define STACK_GROWTH 3 |
3009 | |
3019 | |
3010 | static void |
3020 | ecb_hot static void |
3011 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3021 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3012 | { |
3022 | { |
3013 | int nframes = (uintptr_t)SCHEME_V->dump; |
3023 | int nframes = (uintptr_t)SCHEME_V->dump; |
3014 | struct dump_stack_frame *next_frame; |
3024 | struct dump_stack_frame *next_frame; |
3015 | |
3025 | |
3016 | /* enough room for the next frame? */ |
3026 | /* enough room for the next frame? */ |
3017 | if (nframes >= SCHEME_V->dump_size) |
3027 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3018 | { |
3028 | { |
3019 | SCHEME_V->dump_size += STACK_GROWTH; |
3029 | SCHEME_V->dump_size += STACK_GROWTH; |
3020 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3030 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3021 | } |
3031 | } |
3022 | |
3032 | |
… | |
… | |
3028 | next_frame->code = code; |
3038 | next_frame->code = code; |
3029 | |
3039 | |
3030 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3040 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3031 | } |
3041 | } |
3032 | |
3042 | |
3033 | static int |
3043 | static ecb_hot int |
3034 | xs_return (SCHEME_P_ pointer a) |
3044 | xs_return (SCHEME_P_ pointer a) |
3035 | { |
3045 | { |
3036 | int nframes = (uintptr_t)SCHEME_V->dump; |
3046 | int nframes = (uintptr_t)SCHEME_V->dump; |
3037 | struct dump_stack_frame *frame; |
3047 | struct dump_stack_frame *frame; |
3038 | |
3048 | |
… | |
… | |
3049 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3059 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3050 | |
3060 | |
3051 | return 0; |
3061 | return 0; |
3052 | } |
3062 | } |
3053 | |
3063 | |
3054 | ecb_inline void |
3064 | ecb_cold void |
3055 | dump_stack_reset (SCHEME_P) |
3065 | dump_stack_reset (SCHEME_P) |
3056 | { |
3066 | { |
3057 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3067 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3058 | SCHEME_V->dump = (pointer)+0; |
3068 | SCHEME_V->dump = (pointer)+0; |
3059 | } |
3069 | } |
3060 | |
3070 | |
3061 | ecb_inline void |
3071 | ecb_cold void |
3062 | dump_stack_initialize (SCHEME_P) |
3072 | dump_stack_initialize (SCHEME_P) |
3063 | { |
3073 | { |
3064 | SCHEME_V->dump_size = 0; |
3074 | SCHEME_V->dump_size = 0; |
3065 | SCHEME_V->dump_base = 0; |
3075 | SCHEME_V->dump_base = 0; |
3066 | dump_stack_reset (SCHEME_A); |
3076 | dump_stack_reset (SCHEME_A); |
3067 | } |
3077 | } |
3068 | |
3078 | |
3069 | static void |
3079 | ecb_cold static void |
3070 | dump_stack_free (SCHEME_P) |
3080 | dump_stack_free (SCHEME_P) |
3071 | { |
3081 | { |
3072 | free (SCHEME_V->dump_base); |
3082 | free (SCHEME_V->dump_base); |
3073 | SCHEME_V->dump_base = 0; |
3083 | SCHEME_V->dump_base = 0; |
3074 | SCHEME_V->dump = (pointer)0; |
3084 | SCHEME_V->dump = (pointer)0; |
3075 | SCHEME_V->dump_size = 0; |
3085 | SCHEME_V->dump_size = 0; |
3076 | } |
3086 | } |
3077 | |
3087 | |
3078 | static void |
3088 | ecb_cold static void |
3079 | dump_stack_mark (SCHEME_P) |
3089 | dump_stack_mark (SCHEME_P) |
3080 | { |
3090 | { |
3081 | int nframes = (uintptr_t)SCHEME_V->dump; |
3091 | int nframes = (uintptr_t)SCHEME_V->dump; |
3082 | int i; |
3092 | int i; |
3083 | |
3093 | |
… | |
… | |
3089 | mark (frame->envir); |
3099 | mark (frame->envir); |
3090 | mark (frame->code); |
3100 | mark (frame->code); |
3091 | } |
3101 | } |
3092 | } |
3102 | } |
3093 | |
3103 | |
3094 | static pointer |
3104 | ecb_cold static pointer |
3095 | ss_get_cont (SCHEME_P) |
3105 | ss_get_cont (SCHEME_P) |
3096 | { |
3106 | { |
3097 | int nframes = (uintptr_t)SCHEME_V->dump; |
3107 | int nframes = (uintptr_t)SCHEME_V->dump; |
3098 | int i; |
3108 | int i; |
3099 | |
3109 | |
… | |
… | |
3111 | } |
3121 | } |
3112 | |
3122 | |
3113 | return cont; |
3123 | return cont; |
3114 | } |
3124 | } |
3115 | |
3125 | |
3116 | static void |
3126 | ecb_cold static void |
3117 | ss_set_cont (SCHEME_P_ pointer cont) |
3127 | ss_set_cont (SCHEME_P_ pointer cont) |
3118 | { |
3128 | { |
3119 | int i = 0; |
3129 | int i = 0; |
3120 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3130 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3121 | |
3131 | |
3122 | while (cont != NIL) |
3132 | while (cont != NIL) |
3123 | { |
3133 | { |
3124 | frame->op = ivalue (car (cont)); cont = cdr (cont); |
3134 | frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont); |
3125 | frame->args = car (cont) ; cont = cdr (cont); |
3135 | frame->args = car (cont) ; cont = cdr (cont); |
3126 | frame->envir = car (cont) ; cont = cdr (cont); |
3136 | frame->envir = car (cont) ; cont = cdr (cont); |
3127 | frame->code = car (cont) ; cont = cdr (cont); |
3137 | frame->code = car (cont) ; cont = cdr (cont); |
3128 | |
3138 | |
3129 | ++frame; |
3139 | ++frame; |
3130 | ++i; |
3140 | ++i; |
3131 | } |
3141 | } |
3132 | |
3142 | |
3133 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3143 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3134 | } |
3144 | } |
3135 | |
3145 | |
3136 | #else |
3146 | #else |
3137 | |
3147 | |
3138 | ecb_inline void |
3148 | ecb_cold void |
3139 | dump_stack_reset (SCHEME_P) |
3149 | dump_stack_reset (SCHEME_P) |
3140 | { |
3150 | { |
3141 | SCHEME_V->dump = NIL; |
3151 | SCHEME_V->dump = NIL; |
3142 | } |
3152 | } |
3143 | |
3153 | |
3144 | ecb_inline void |
3154 | ecb_cold void |
3145 | dump_stack_initialize (SCHEME_P) |
3155 | dump_stack_initialize (SCHEME_P) |
3146 | { |
3156 | { |
3147 | dump_stack_reset (SCHEME_A); |
3157 | dump_stack_reset (SCHEME_A); |
3148 | } |
3158 | } |
3149 | |
3159 | |
3150 | static void |
3160 | ecb_cold static void |
3151 | dump_stack_free (SCHEME_P) |
3161 | dump_stack_free (SCHEME_P) |
3152 | { |
3162 | { |
3153 | SCHEME_V->dump = NIL; |
3163 | SCHEME_V->dump = NIL; |
3154 | } |
3164 | } |
3155 | |
3165 | |
3156 | static int |
3166 | ecb_hot static int |
3157 | xs_return (SCHEME_P_ pointer a) |
3167 | xs_return (SCHEME_P_ pointer a) |
3158 | { |
3168 | { |
3159 | pointer dump = SCHEME_V->dump; |
3169 | pointer dump = SCHEME_V->dump; |
3160 | |
3170 | |
3161 | SCHEME_V->value = a; |
3171 | SCHEME_V->value = a; |
3162 | |
3172 | |
3163 | if (dump == NIL) |
3173 | if (dump == NIL) |
3164 | return -1; |
3174 | return -1; |
3165 | |
3175 | |
3166 | SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); |
3176 | SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump); |
3167 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3177 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3168 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3178 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3169 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3179 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3170 | |
3180 | |
3171 | SCHEME_V->dump = dump; |
3181 | SCHEME_V->dump = dump; |
3172 | |
3182 | |
3173 | return 0; |
3183 | return 0; |
3174 | } |
3184 | } |
3175 | |
3185 | |
3176 | static void |
3186 | ecb_hot static void |
3177 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3187 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3178 | { |
3188 | { |
3179 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3189 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3180 | cons (args, |
3190 | cons (args, |
3181 | cons (SCHEME_V->envir, |
3191 | cons (SCHEME_V->envir, |
3182 | cons (code, |
3192 | cons (code, |
3183 | SCHEME_V->dump)))); |
3193 | SCHEME_V->dump)))); |
3184 | } |
3194 | } |
3185 | |
3195 | |
|
|
3196 | ecb_cold static void |
|
|
3197 | dump_stack_mark (SCHEME_P) |
|
|
3198 | { |
|
|
3199 | mark (SCHEME_V->dump); |
|
|
3200 | } |
|
|
3201 | |
|
|
3202 | ecb_cold static pointer |
|
|
3203 | ss_get_cont (SCHEME_P) |
|
|
3204 | { |
|
|
3205 | return SCHEME_V->dump; |
|
|
3206 | } |
|
|
3207 | |
|
|
3208 | ecb_cold static void |
|
|
3209 | ss_set_cont (SCHEME_P_ pointer cont) |
|
|
3210 | { |
|
|
3211 | SCHEME_V->dump = cont; |
|
|
3212 | } |
|
|
3213 | |
|
|
3214 | #endif |
|
|
3215 | |
|
|
3216 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
|
|
3217 | |
|
|
3218 | #if EXPERIMENT |
|
|
3219 | |
|
|
3220 | typedef void *stream[1]; |
|
|
3221 | |
|
|
3222 | #define stream_init() { 0 } |
|
|
3223 | |
|
|
3224 | ecb_cold static void |
|
|
3225 | stream_put (void **s, uint8_t byte) |
|
|
3226 | { |
|
|
3227 | uint32_t *sp = *s; |
|
|
3228 | uint32_t size = sizeof (uint32_t) * 2; |
|
|
3229 | uint32_t offs = size; |
|
|
3230 | |
|
|
3231 | if (ecb_expect_true (sp)) |
|
|
3232 | { |
|
|
3233 | offs = sp[0]; |
|
|
3234 | size = sp[1]; |
|
|
3235 | } |
|
|
3236 | |
|
|
3237 | if (ecb_expect_false (offs == size)) |
|
|
3238 | { |
|
|
3239 | size *= 2; |
|
|
3240 | sp = realloc (sp, size); |
|
|
3241 | *s = sp; |
|
|
3242 | sp[1] = size; |
|
|
3243 | |
|
|
3244 | } |
|
|
3245 | |
|
|
3246 | ((uint8_t *)sp)[offs++] = byte; |
|
|
3247 | sp[0] = offs; |
|
|
3248 | } |
|
|
3249 | |
|
|
3250 | #define stream_data(s) ((char *)(s)[0] + sizeof (uint32_t) * 2) |
|
|
3251 | #define stream_size(s) (((uint32_t *)(s)[0])[0] - sizeof (uint32_t) * 2) |
|
|
3252 | #define stream_free(s) free (s[0]) |
|
|
3253 | |
|
|
3254 | // calculates a (preferably small) integer that makes it possible to find |
|
|
3255 | // the symbol again. if pointers were offsets into a memory area... until |
|
|
3256 | // then, we return segment number in the low bits, and offset in the high |
|
|
3257 | // bits |
|
|
3258 | static uint32_t |
|
|
3259 | symbol_id (SCHEME_P_ pointer sym) |
|
|
3260 | { |
|
|
3261 | struct cell *p = CELL (sym); |
|
|
3262 | int i; |
|
|
3263 | |
|
|
3264 | for (i = SCHEME_V->last_cell_seg; i >= 0; --i) |
|
|
3265 | if (SCHEME_V->cell_seg[i] <= p && p < SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize[i]) |
|
|
3266 | { |
|
|
3267 | printf ("seg %d ofs %d/%d\n",i,(p - SCHEME_V->cell_seg[i]),SCHEME_V->cell_segsize[i]);//D |
|
|
3268 | return i | ((p - SCHEME_V->cell_seg[i]) << CELL_NSEGMENT_LOG); |
|
|
3269 | } |
|
|
3270 | |
|
|
3271 | abort (); |
|
|
3272 | } |
|
|
3273 | |
3186 | static void |
3274 | static void |
3187 | dump_stack_mark (SCHEME_P) |
3275 | compile (SCHEME_P_ stream s, pointer x) |
3188 | { |
3276 | { |
3189 | mark (SCHEME_V->dump); |
3277 | if (x == NIL) |
3190 | } |
3278 | { |
|
|
3279 | stream_put (s, 0); |
|
|
3280 | return; |
|
|
3281 | } |
3191 | |
3282 | |
3192 | static pointer |
3283 | if (is_syntax (x)) |
3193 | ss_get_cont (SCHEME_P) |
3284 | { |
3194 | { |
3285 | stream_put (s, 1); |
3195 | return SCHEME_V->dump; |
3286 | stream_put (s, syntaxnum (x)); |
3196 | } |
3287 | return; |
|
|
3288 | } |
3197 | |
3289 | |
3198 | static void |
3290 | switch (type (x)) |
3199 | ss_set_cont (SCHEME_P_ pointer cont) |
3291 | { |
3200 | { |
3292 | case T_INTEGER: |
3201 | SCHEME_V->dump = cont; |
3293 | stream_put (s, 2); |
3202 | } |
3294 | stream_put (s, 0); |
|
|
3295 | stream_put (s, 0); |
|
|
3296 | stream_put (s, 0); |
|
|
3297 | stream_put (s, 0); |
|
|
3298 | return; |
3203 | |
3299 | |
3204 | #endif |
3300 | case T_SYMBOL: |
|
|
3301 | { |
|
|
3302 | uint32_t sym = symbol_id (SCHEME_A_ x); |
|
|
3303 | printf ("sym %x\n", sym);//D |
3205 | |
3304 | |
3206 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3305 | stream_put (s, 3); |
|
|
3306 | |
|
|
3307 | while (sym > 0x7f) |
|
|
3308 | { |
|
|
3309 | stream_put (s, sym | 0x80); |
|
|
3310 | sym >>= 8; |
|
|
3311 | } |
|
|
3312 | |
|
|
3313 | stream_put (s, sym); |
|
|
3314 | } |
|
|
3315 | return; |
|
|
3316 | |
|
|
3317 | case T_PAIR: |
|
|
3318 | stream_put (s, 4); |
|
|
3319 | while (x != NIL) |
|
|
3320 | { |
|
|
3321 | compile (SCHEME_A_ s, car (x)); |
|
|
3322 | x = cdr (x); |
|
|
3323 | } |
|
|
3324 | stream_put (s, 0xff); |
|
|
3325 | return; |
|
|
3326 | |
|
|
3327 | default: |
|
|
3328 | stream_put (s, 5); |
|
|
3329 | stream_put (s, type (x)); |
|
|
3330 | stream_put (s, 0); |
|
|
3331 | stream_put (s, 0); |
|
|
3332 | stream_put (s, 0); |
|
|
3333 | stream_put (s, 0); |
|
|
3334 | break; |
|
|
3335 | } |
|
|
3336 | } |
3207 | |
3337 | |
3208 | static int |
3338 | static int |
|
|
3339 | compile_closure (SCHEME_P_ pointer p) |
|
|
3340 | { |
|
|
3341 | stream s = stream_init (); |
|
|
3342 | |
|
|
3343 | printatom (SCHEME_A_ p, 1);//D |
|
|
3344 | compile (SCHEME_A_ s, car (p)); |
|
|
3345 | |
|
|
3346 | FILE *xxd = popen ("xxd", "we"); |
|
|
3347 | fwrite (stream_data (s), 1, stream_size (s), xxd); |
|
|
3348 | fclose (xxd); |
|
|
3349 | |
|
|
3350 | return stream_size (s); |
|
|
3351 | } |
|
|
3352 | |
|
|
3353 | static int |
|
|
3354 | dtree (SCHEME_P_ int indent, pointer x) |
|
|
3355 | { |
|
|
3356 | int c; |
|
|
3357 | |
|
|
3358 | if (is_syntax (x)) |
|
|
3359 | { |
|
|
3360 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3361 | return 8 + 8; |
|
|
3362 | } |
|
|
3363 | |
|
|
3364 | if (x == NIL) |
|
|
3365 | { |
|
|
3366 | printf ("%*sNIL\n", indent, ""); |
|
|
3367 | return 3; |
|
|
3368 | } |
|
|
3369 | |
|
|
3370 | switch (type (x)) |
|
|
3371 | { |
|
|
3372 | case T_INTEGER: |
|
|
3373 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3374 | return 32+8; |
|
|
3375 | |
|
|
3376 | case T_SYMBOL: |
|
|
3377 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3378 | return 24+8; |
|
|
3379 | |
|
|
3380 | case T_CLOSURE: |
|
|
3381 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3382 | dtree (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3383 | return 32 + dtree (SCHEME_A_ indent + 3, car (x)); |
|
|
3384 | |
|
|
3385 | case T_PAIR: |
|
|
3386 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3387 | c = dtree (SCHEME_A_ indent + 3, car (x)); |
|
|
3388 | c += dtree (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3389 | return c + 1; |
|
|
3390 | |
|
|
3391 | case T_PORT: |
|
|
3392 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3393 | return 24+8; |
|
|
3394 | |
|
|
3395 | case T_VECTOR: |
|
|
3396 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3397 | return 24+8; |
|
|
3398 | |
|
|
3399 | case T_ENVIRONMENT: |
|
|
3400 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3401 | return 0 + dtree (SCHEME_A_ indent + 3, car (x)); |
|
|
3402 | |
|
|
3403 | default: |
|
|
3404 | printf ("unhandled type %d\n", type (x)); |
|
|
3405 | break; |
|
|
3406 | } |
|
|
3407 | } |
|
|
3408 | #endif |
|
|
3409 | |
|
|
3410 | /* syntax, eval, core, ... */ |
|
|
3411 | ecb_hot static int |
3209 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3412 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3210 | { |
3413 | { |
3211 | pointer args = SCHEME_V->args; |
3414 | pointer args = SCHEME_V->args; |
3212 | pointer x, y; |
3415 | pointer x, y; |
3213 | |
3416 | |
3214 | switch (op) |
3417 | switch (op) |
3215 | { |
3418 | { |
|
|
3419 | #if EXPERIMENT //D |
|
|
3420 | case OP_DEBUG: |
|
|
3421 | { |
|
|
3422 | uint32_t len = compile_closure (SCHEME_A_ car (args)); |
|
|
3423 | printf ("len = %d\n", len); |
|
|
3424 | printf ("\n"); |
|
|
3425 | s_return (S_T); |
|
|
3426 | } |
|
|
3427 | #endif |
3216 | case OP_LOAD: /* load */ |
3428 | case OP_LOAD: /* load */ |
3217 | if (file_interactive (SCHEME_A)) |
3429 | if (file_interactive (SCHEME_A)) |
3218 | { |
3430 | { |
3219 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3431 | putstr (SCHEME_A_ "Loading "); |
3220 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3432 | putstr (SCHEME_A_ strvalue (car (args))); |
|
|
3433 | putcharacter (SCHEME_A_ '\n'); |
3221 | } |
3434 | } |
3222 | |
3435 | |
3223 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3436 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3224 | Error_1 ("unable to open", car (args)); |
3437 | Error_1 ("unable to open", car (args)); |
3225 | else |
3438 | |
3226 | { |
|
|
3227 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3439 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3228 | s_goto (OP_T0LVL); |
3440 | s_goto (OP_T0LVL); |
3229 | } |
|
|
3230 | |
3441 | |
3231 | case OP_T0LVL: /* top level */ |
3442 | case OP_T0LVL: /* top level */ |
3232 | |
3443 | |
3233 | /* If we reached the end of file, this loop is done. */ |
3444 | /* If we reached the end of file, this loop is done. */ |
3234 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3445 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3235 | { |
3446 | { |
3236 | if (SCHEME_V->file_i == 0) |
3447 | if (SCHEME_V->file_i == 0) |
3237 | { |
3448 | { |
3238 | SCHEME_V->args = NIL; |
3449 | SCHEME_V->args = NIL; |
3239 | s_goto (OP_QUIT); |
3450 | s_goto (OP_QUIT); |
… | |
… | |
3250 | /* If interactive, be nice to user. */ |
3461 | /* If interactive, be nice to user. */ |
3251 | if (file_interactive (SCHEME_A)) |
3462 | if (file_interactive (SCHEME_A)) |
3252 | { |
3463 | { |
3253 | SCHEME_V->envir = SCHEME_V->global_env; |
3464 | SCHEME_V->envir = SCHEME_V->global_env; |
3254 | dump_stack_reset (SCHEME_A); |
3465 | dump_stack_reset (SCHEME_A); |
3255 | putstr (SCHEME_A_ "\n"); |
3466 | putcharacter (SCHEME_A_ '\n'); |
3256 | putstr (SCHEME_A_ prompt); |
3467 | putstr (SCHEME_A_ prompt); |
3257 | } |
3468 | } |
3258 | |
3469 | |
3259 | /* Set up another iteration of REPL */ |
3470 | /* Set up another iteration of REPL */ |
3260 | SCHEME_V->nesting = 0; |
3471 | SCHEME_V->nesting = 0; |
… | |
… | |
3295 | { |
3506 | { |
3296 | SCHEME_V->print_flag = 1; |
3507 | SCHEME_V->print_flag = 1; |
3297 | SCHEME_V->args = SCHEME_V->value; |
3508 | SCHEME_V->args = SCHEME_V->value; |
3298 | s_goto (OP_P0LIST); |
3509 | s_goto (OP_P0LIST); |
3299 | } |
3510 | } |
3300 | else |
3511 | |
3301 | s_return (SCHEME_V->value); |
3512 | s_return (SCHEME_V->value); |
3302 | |
3513 | |
3303 | case OP_EVAL: /* main part of evaluation */ |
3514 | case OP_EVAL: /* main part of evaluation */ |
3304 | #if USE_TRACING |
3515 | #if USE_TRACING |
3305 | if (SCHEME_V->tracing) |
3516 | if (SCHEME_V->tracing) |
3306 | { |
3517 | { |
… | |
… | |
3317 | #endif |
3528 | #endif |
3318 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3529 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3319 | { |
3530 | { |
3320 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3531 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3321 | |
3532 | |
3322 | if (x != NIL) |
3533 | if (x == NIL) |
3323 | s_return (slot_value_in_env (x)); |
|
|
3324 | else |
|
|
3325 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3534 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3535 | |
|
|
3536 | s_return (slot_value_in_env (x)); |
3326 | } |
3537 | } |
3327 | else if (is_pair (SCHEME_V->code)) |
3538 | else if (is_pair (SCHEME_V->code)) |
3328 | { |
3539 | { |
3329 | x = car (SCHEME_V->code); |
3540 | x = car (SCHEME_V->code); |
3330 | |
3541 | |
… | |
… | |
3339 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3550 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3340 | SCHEME_V->code = x; |
3551 | SCHEME_V->code = x; |
3341 | s_goto (OP_EVAL); |
3552 | s_goto (OP_EVAL); |
3342 | } |
3553 | } |
3343 | } |
3554 | } |
3344 | else |
3555 | |
3345 | s_return (SCHEME_V->code); |
3556 | s_return (SCHEME_V->code); |
3346 | |
3557 | |
3347 | case OP_E0ARGS: /* eval arguments */ |
3558 | case OP_E0ARGS: /* eval arguments */ |
3348 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3559 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3349 | { |
3560 | { |
3350 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3561 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3351 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3562 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3352 | SCHEME_V->code = SCHEME_V->value; |
3563 | SCHEME_V->code = SCHEME_V->value; |
3353 | s_goto (OP_APPLY); |
3564 | s_goto (OP_APPLY); |
… | |
… | |
3380 | |
3591 | |
3381 | case OP_TRACING: |
3592 | case OP_TRACING: |
3382 | { |
3593 | { |
3383 | int tr = SCHEME_V->tracing; |
3594 | int tr = SCHEME_V->tracing; |
3384 | |
3595 | |
3385 | SCHEME_V->tracing = ivalue (car (args)); |
3596 | SCHEME_V->tracing = ivalue_unchecked (car (args)); |
3386 | s_return (mk_integer (SCHEME_A_ tr)); |
3597 | s_return (mk_integer (SCHEME_A_ tr)); |
3387 | } |
3598 | } |
3388 | |
3599 | |
3389 | #endif |
3600 | #endif |
3390 | |
3601 | |
… | |
… | |
3407 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3618 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3408 | else if (is_foreign (SCHEME_V->code)) |
3619 | else if (is_foreign (SCHEME_V->code)) |
3409 | { |
3620 | { |
3410 | /* Keep nested calls from GC'ing the arglist */ |
3621 | /* Keep nested calls from GC'ing the arglist */ |
3411 | push_recent_alloc (SCHEME_A_ args, NIL); |
3622 | push_recent_alloc (SCHEME_A_ args, NIL); |
3412 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3623 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3413 | |
3624 | |
3414 | s_return (x); |
3625 | s_return (x); |
3415 | } |
3626 | } |
3416 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3627 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3417 | { |
3628 | { |
… | |
… | |
3454 | |
3665 | |
3455 | case OP_DOMACRO: /* do macro */ |
3666 | case OP_DOMACRO: /* do macro */ |
3456 | SCHEME_V->code = SCHEME_V->value; |
3667 | SCHEME_V->code = SCHEME_V->value; |
3457 | s_goto (OP_EVAL); |
3668 | s_goto (OP_EVAL); |
3458 | |
3669 | |
3459 | #if 1 |
|
|
3460 | |
|
|
3461 | case OP_LAMBDA: /* lambda */ |
3670 | case OP_LAMBDA: /* lambda */ |
3462 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3671 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3463 | set SCHEME_V->value fall thru */ |
3672 | set SCHEME_V->value fall thru */ |
3464 | { |
3673 | { |
3465 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3674 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
… | |
… | |
3471 | SCHEME_V->code = slot_value_in_env (f); |
3680 | SCHEME_V->code = slot_value_in_env (f); |
3472 | s_goto (OP_APPLY); |
3681 | s_goto (OP_APPLY); |
3473 | } |
3682 | } |
3474 | |
3683 | |
3475 | SCHEME_V->value = SCHEME_V->code; |
3684 | SCHEME_V->value = SCHEME_V->code; |
3476 | /* Fallthru */ |
|
|
3477 | } |
3685 | } |
|
|
3686 | /* Fallthru */ |
3478 | |
3687 | |
3479 | case OP_LAMBDA1: |
3688 | case OP_LAMBDA1: |
3480 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3689 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3481 | |
|
|
3482 | #else |
|
|
3483 | |
|
|
3484 | case OP_LAMBDA: /* lambda */ |
|
|
3485 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
|
|
3486 | |
|
|
3487 | #endif |
|
|
3488 | |
3690 | |
3489 | case OP_MKCLOSURE: /* make-closure */ |
3691 | case OP_MKCLOSURE: /* make-closure */ |
3490 | x = car (args); |
3692 | x = car (args); |
3491 | |
3693 | |
3492 | if (car (x) == SCHEME_V->LAMBDA) |
3694 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
3531 | else |
3733 | else |
3532 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3734 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3533 | |
3735 | |
3534 | s_return (SCHEME_V->code); |
3736 | s_return (SCHEME_V->code); |
3535 | |
3737 | |
3536 | |
|
|
3537 | case OP_DEFP: /* defined? */ |
3738 | case OP_DEFP: /* defined? */ |
3538 | x = SCHEME_V->envir; |
3739 | x = SCHEME_V->envir; |
3539 | |
3740 | |
3540 | if (cdr (args) != NIL) |
3741 | if (cdr (args) != NIL) |
3541 | x = cadr (args); |
3742 | x = cadr (args); |
… | |
… | |
3558 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3759 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3559 | s_return (SCHEME_V->value); |
3760 | s_return (SCHEME_V->value); |
3560 | } |
3761 | } |
3561 | else |
3762 | else |
3562 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3763 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3563 | |
|
|
3564 | |
3764 | |
3565 | case OP_BEGIN: /* begin */ |
3765 | case OP_BEGIN: /* begin */ |
3566 | if (!is_pair (SCHEME_V->code)) |
3766 | if (!is_pair (SCHEME_V->code)) |
3567 | s_return (SCHEME_V->code); |
3767 | s_return (SCHEME_V->code); |
3568 | |
3768 | |
… | |
… | |
3580 | case OP_IF1: /* if */ |
3780 | case OP_IF1: /* if */ |
3581 | if (is_true (SCHEME_V->value)) |
3781 | if (is_true (SCHEME_V->value)) |
3582 | SCHEME_V->code = car (SCHEME_V->code); |
3782 | SCHEME_V->code = car (SCHEME_V->code); |
3583 | else |
3783 | else |
3584 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
3784 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
|
|
3785 | |
3585 | s_goto (OP_EVAL); |
3786 | s_goto (OP_EVAL); |
3586 | |
3787 | |
3587 | case OP_LET0: /* let */ |
3788 | case OP_LET0: /* let */ |
3588 | SCHEME_V->args = NIL; |
3789 | SCHEME_V->args = NIL; |
3589 | SCHEME_V->value = SCHEME_V->code; |
3790 | SCHEME_V->value = SCHEME_V->code; |
… | |
… | |
3745 | } |
3946 | } |
3746 | else |
3947 | else |
3747 | { |
3948 | { |
3748 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3949 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3749 | s_return (NIL); |
3950 | s_return (NIL); |
3750 | else |
3951 | |
3751 | { |
|
|
3752 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3952 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3753 | SCHEME_V->code = caar (SCHEME_V->code); |
3953 | SCHEME_V->code = caar (SCHEME_V->code); |
3754 | s_goto (OP_EVAL); |
3954 | s_goto (OP_EVAL); |
3755 | } |
|
|
3756 | } |
3955 | } |
3757 | |
3956 | |
3758 | case OP_DELAY: /* delay */ |
3957 | case OP_DELAY: /* delay */ |
3759 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3958 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3760 | set_typeflag (x, T_PROMISE); |
3959 | set_typeflag (x, T_PROMISE); |
… | |
… | |
3771 | case OP_AND1: /* and */ |
3970 | case OP_AND1: /* and */ |
3772 | if (is_false (SCHEME_V->value)) |
3971 | if (is_false (SCHEME_V->value)) |
3773 | s_return (SCHEME_V->value); |
3972 | s_return (SCHEME_V->value); |
3774 | else if (SCHEME_V->code == NIL) |
3973 | else if (SCHEME_V->code == NIL) |
3775 | s_return (SCHEME_V->value); |
3974 | s_return (SCHEME_V->value); |
3776 | else |
3975 | |
3777 | { |
|
|
3778 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3976 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3779 | SCHEME_V->code = car (SCHEME_V->code); |
3977 | SCHEME_V->code = car (SCHEME_V->code); |
3780 | s_goto (OP_EVAL); |
3978 | s_goto (OP_EVAL); |
3781 | } |
|
|
3782 | |
3979 | |
3783 | case OP_OR0: /* or */ |
3980 | case OP_OR0: /* or */ |
3784 | if (SCHEME_V->code == NIL) |
3981 | if (SCHEME_V->code == NIL) |
3785 | s_return (S_F); |
3982 | s_return (S_F); |
3786 | |
3983 | |
… | |
… | |
3791 | case OP_OR1: /* or */ |
3988 | case OP_OR1: /* or */ |
3792 | if (is_true (SCHEME_V->value)) |
3989 | if (is_true (SCHEME_V->value)) |
3793 | s_return (SCHEME_V->value); |
3990 | s_return (SCHEME_V->value); |
3794 | else if (SCHEME_V->code == NIL) |
3991 | else if (SCHEME_V->code == NIL) |
3795 | s_return (SCHEME_V->value); |
3992 | s_return (SCHEME_V->value); |
3796 | else |
3993 | |
3797 | { |
|
|
3798 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3994 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3799 | SCHEME_V->code = car (SCHEME_V->code); |
3995 | SCHEME_V->code = car (SCHEME_V->code); |
3800 | s_goto (OP_EVAL); |
3996 | s_goto (OP_EVAL); |
3801 | } |
|
|
3802 | |
3997 | |
3803 | case OP_C0STREAM: /* cons-stream */ |
3998 | case OP_C0STREAM: /* cons-stream */ |
3804 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3999 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3805 | SCHEME_V->code = car (SCHEME_V->code); |
4000 | SCHEME_V->code = car (SCHEME_V->code); |
3806 | s_goto (OP_EVAL); |
4001 | s_goto (OP_EVAL); |
… | |
… | |
3871 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
4066 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
3872 | SCHEME_V->code = caar (x); |
4067 | SCHEME_V->code = caar (x); |
3873 | s_goto (OP_EVAL); |
4068 | s_goto (OP_EVAL); |
3874 | } |
4069 | } |
3875 | } |
4070 | } |
3876 | else |
4071 | |
3877 | s_return (NIL); |
4072 | s_return (NIL); |
3878 | |
4073 | |
3879 | case OP_CASE2: /* case */ |
4074 | case OP_CASE2: /* case */ |
3880 | if (is_true (SCHEME_V->value)) |
4075 | if (is_true (SCHEME_V->value)) |
3881 | s_goto (OP_BEGIN); |
4076 | s_goto (OP_BEGIN); |
3882 | else |
4077 | |
3883 | s_return (NIL); |
4078 | s_return (NIL); |
3884 | |
4079 | |
3885 | case OP_PAPPLY: /* apply */ |
4080 | case OP_PAPPLY: /* apply */ |
3886 | SCHEME_V->code = car (args); |
4081 | SCHEME_V->code = car (args); |
3887 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
4082 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
3888 | /*SCHEME_V->args = cadr(args); */ |
4083 | /*SCHEME_V->args = cadr(args); */ |
… | |
… | |
3902 | } |
4097 | } |
3903 | |
4098 | |
3904 | if (USE_ERROR_CHECKING) abort (); |
4099 | if (USE_ERROR_CHECKING) abort (); |
3905 | } |
4100 | } |
3906 | |
4101 | |
3907 | static int |
4102 | /* math, cxr */ |
|
|
4103 | ecb_hot static int |
3908 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4104 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
3909 | { |
4105 | { |
3910 | pointer args = SCHEME_V->args; |
4106 | pointer args = SCHEME_V->args; |
3911 | pointer x = car (args); |
4107 | pointer x = car (args); |
3912 | num v; |
4108 | num v; |
3913 | |
4109 | |
3914 | #if USE_MATH |
|
|
3915 | RVALUE dd; |
|
|
3916 | #endif |
|
|
3917 | |
|
|
3918 | switch (op) |
4110 | switch (op) |
3919 | { |
4111 | { |
3920 | #if USE_MATH |
4112 | #if USE_MATH |
3921 | case OP_INEX2EX: /* inexact->exact */ |
4113 | case OP_INEX2EX: /* inexact->exact */ |
3922 | if (is_integer (x)) |
4114 | if (!is_integer (x)) |
3923 | s_return (x); |
4115 | { |
3924 | else if (modf (rvalue_unchecked (x), &dd) == 0) |
4116 | RVALUE r = rvalue_unchecked (x); |
3925 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4117 | |
|
|
4118 | if (r == (RVALUE)(IVALUE)r) |
|
|
4119 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
3926 | else |
4120 | else |
3927 | Error_1 ("inexact->exact: not integral:", x); |
4121 | Error_1 ("inexact->exact: not integral:", x); |
|
|
4122 | } |
3928 | |
4123 | |
|
|
4124 | s_return (x); |
|
|
4125 | |
|
|
4126 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4127 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4128 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4129 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
|
|
4130 | |
|
|
4131 | case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
3929 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4132 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
3930 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
4133 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
|
|
4134 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
3931 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4135 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
3932 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4136 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
3933 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4137 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
3934 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4138 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
3935 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4139 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
3936 | |
4140 | |
3937 | case OP_ATAN: |
4141 | case OP_ATAN: |
|
|
4142 | s_return (mk_real (SCHEME_A_ |
3938 | if (cdr (args) == NIL) |
4143 | cdr (args) == NIL |
3939 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4144 | ? atan (rvalue (x)) |
3940 | else |
4145 | : atan2 (rvalue (x), rvalue (cadr (args))))); |
3941 | { |
|
|
3942 | pointer y = cadr (args); |
|
|
3943 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
|
|
3944 | } |
|
|
3945 | |
|
|
3946 | case OP_SQRT: |
|
|
3947 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
|
|
3948 | |
4146 | |
3949 | case OP_EXPT: |
4147 | case OP_EXPT: |
3950 | { |
4148 | { |
3951 | RVALUE result; |
4149 | RVALUE result; |
3952 | int real_result = 1; |
4150 | int real_result = 1; |
… | |
… | |
3966 | /* If the test fails, result is too big for integer. */ |
4164 | /* If the test fails, result is too big for integer. */ |
3967 | if (!real_result) |
4165 | if (!real_result) |
3968 | { |
4166 | { |
3969 | long result_as_long = result; |
4167 | long result_as_long = result; |
3970 | |
4168 | |
3971 | if (result != (RVALUE) result_as_long) |
4169 | if (result != result_as_long) |
3972 | real_result = 1; |
4170 | real_result = 1; |
3973 | } |
4171 | } |
3974 | |
4172 | |
3975 | if (real_result) |
4173 | if (real_result) |
3976 | s_return (mk_real (SCHEME_A_ result)); |
4174 | s_return (mk_real (SCHEME_A_ result)); |
3977 | else |
4175 | else |
3978 | s_return (mk_integer (SCHEME_A_ result)); |
4176 | s_return (mk_integer (SCHEME_A_ result)); |
3979 | } |
4177 | } |
3980 | |
|
|
3981 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
3982 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
3983 | |
|
|
3984 | case OP_TRUNCATE: |
|
|
3985 | { |
|
|
3986 | RVALUE rvalue_of_x; |
|
|
3987 | |
|
|
3988 | rvalue_of_x = rvalue (x); |
|
|
3989 | |
|
|
3990 | if (rvalue_of_x > 0) |
|
|
3991 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
|
|
3992 | else |
|
|
3993 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
|
|
3994 | } |
|
|
3995 | |
|
|
3996 | case OP_ROUND: |
|
|
3997 | if (num_is_integer (x)) |
|
|
3998 | s_return (x); |
|
|
3999 | |
|
|
4000 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
|
|
4001 | #endif |
4178 | #endif |
4002 | |
4179 | |
4003 | case OP_ADD: /* + */ |
4180 | case OP_ADD: /* + */ |
4004 | v = num_zero; |
4181 | v = num_zero; |
4005 | |
4182 | |
… | |
… | |
4093 | else |
4270 | else |
4094 | Error_0 ("modulo: division by zero"); |
4271 | Error_0 ("modulo: division by zero"); |
4095 | |
4272 | |
4096 | s_return (mk_number (SCHEME_A_ v)); |
4273 | s_return (mk_number (SCHEME_A_ v)); |
4097 | |
4274 | |
4098 | case OP_CAR: /* car */ |
4275 | /* the compiler will optimize this mess... */ |
4099 | s_return (caar (args)); |
4276 | case OP_CAR: op_car: s_return (car (x)); |
4100 | |
4277 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4101 | case OP_CDR: /* cdr */ |
4278 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4102 | s_return (cdar (args)); |
4279 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4280 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4281 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4282 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4283 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4284 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4285 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4286 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4287 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4288 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4289 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4290 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4291 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4292 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4293 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4294 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4295 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4296 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4297 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4298 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4299 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4300 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4301 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4302 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4303 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4304 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4305 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4103 | |
4306 | |
4104 | case OP_CONS: /* cons */ |
4307 | case OP_CONS: /* cons */ |
4105 | set_cdr (args, cadr (args)); |
4308 | set_cdr (args, cadr (args)); |
4106 | s_return (args); |
4309 | s_return (args); |
4107 | |
4310 | |
… | |
… | |
4122 | } |
4325 | } |
4123 | else |
4326 | else |
4124 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4327 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4125 | |
4328 | |
4126 | case OP_CHAR2INT: /* char->integer */ |
4329 | case OP_CHAR2INT: /* char->integer */ |
4127 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4330 | s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x))); |
4128 | |
4331 | |
4129 | case OP_INT2CHAR: /* integer->char */ |
4332 | case OP_INT2CHAR: /* integer->char */ |
4130 | s_return (mk_character (SCHEME_A_ ivalue (x))); |
4333 | s_return (mk_character (SCHEME_A_ ivalue_unchecked (x))); |
4131 | |
4334 | |
4132 | case OP_CHARUPCASE: |
4335 | case OP_CHARUPCASE: |
4133 | { |
4336 | { |
4134 | unsigned char c = ivalue (x); |
4337 | unsigned char c = ivalue_unchecked (x); |
4135 | c = toupper (c); |
4338 | c = toupper (c); |
4136 | s_return (mk_character (SCHEME_A_ c)); |
4339 | s_return (mk_character (SCHEME_A_ c)); |
4137 | } |
4340 | } |
4138 | |
4341 | |
4139 | case OP_CHARDNCASE: |
4342 | case OP_CHARDNCASE: |
4140 | { |
4343 | { |
4141 | unsigned char c = ivalue (x); |
4344 | unsigned char c = ivalue_unchecked (x); |
4142 | c = tolower (c); |
4345 | c = tolower (c); |
4143 | s_return (mk_character (SCHEME_A_ c)); |
4346 | s_return (mk_character (SCHEME_A_ c)); |
4144 | } |
4347 | } |
4145 | |
4348 | |
4146 | case OP_STR2SYM: /* string->symbol */ |
4349 | case OP_STR2SYM: /* string->symbol */ |
… | |
… | |
4223 | Error_1 ("atom->string: not an atom:", x); |
4426 | Error_1 ("atom->string: not an atom:", x); |
4224 | } |
4427 | } |
4225 | |
4428 | |
4226 | case OP_MKSTRING: /* make-string */ |
4429 | case OP_MKSTRING: /* make-string */ |
4227 | { |
4430 | { |
4228 | int fill = ' '; |
4431 | int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' '; |
4229 | int len; |
|
|
4230 | |
|
|
4231 | len = ivalue (x); |
4432 | int len = ivalue_unchecked (x); |
4232 | |
|
|
4233 | if (cdr (args) != NIL) |
|
|
4234 | fill = charvalue (cadr (args)); |
|
|
4235 | |
4433 | |
4236 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4434 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4237 | } |
4435 | } |
4238 | |
4436 | |
4239 | case OP_STRLEN: /* string-length */ |
4437 | case OP_STRLEN: /* string-length */ |
4240 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4438 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4241 | |
4439 | |
4242 | case OP_STRREF: /* string-ref */ |
4440 | case OP_STRREF: /* string-ref */ |
4243 | { |
4441 | { |
4244 | char *str; |
|
|
4245 | int index; |
|
|
4246 | |
|
|
4247 | str = strvalue (x); |
4442 | char *str = strvalue (x); |
4248 | |
|
|
4249 | index = ivalue (cadr (args)); |
4443 | int index = ivalue_unchecked (cadr (args)); |
4250 | |
4444 | |
4251 | if (index >= strlength (x)) |
4445 | if (index >= strlength (x)) |
4252 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4446 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4253 | |
4447 | |
4254 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4448 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4255 | } |
4449 | } |
4256 | |
4450 | |
4257 | case OP_STRSET: /* string-set! */ |
4451 | case OP_STRSET: /* string-set! */ |
4258 | { |
4452 | { |
4259 | char *str; |
4453 | char *str = strvalue (x); |
4260 | int index; |
4454 | int index = ivalue_unchecked (cadr (args)); |
4261 | int c; |
4455 | int c; |
4262 | |
4456 | |
4263 | if (is_immutable (x)) |
4457 | if (is_immutable (x)) |
4264 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4458 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4265 | |
|
|
4266 | str = strvalue (x); |
|
|
4267 | |
|
|
4268 | index = ivalue (cadr (args)); |
|
|
4269 | |
4459 | |
4270 | if (index >= strlength (x)) |
4460 | if (index >= strlength (x)) |
4271 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4461 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4272 | |
4462 | |
4273 | c = charvalue (caddr (args)); |
4463 | c = charvalue (caddr (args)); |
… | |
… | |
4294 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4484 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4295 | |
4485 | |
4296 | s_return (newstr); |
4486 | s_return (newstr); |
4297 | } |
4487 | } |
4298 | |
4488 | |
4299 | case OP_SUBSTR: /* substring */ |
4489 | case OP_STRING_COPY: /* substring/string-copy */ |
4300 | { |
4490 | { |
4301 | char *str; |
4491 | char *str = strvalue (x); |
4302 | int index0; |
4492 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4303 | int index1; |
4493 | int index1; |
4304 | int len; |
4494 | int len; |
4305 | |
4495 | |
4306 | str = strvalue (x); |
|
|
4307 | |
|
|
4308 | index0 = ivalue (cadr (args)); |
|
|
4309 | |
|
|
4310 | if (index0 > strlength (x)) |
4496 | if (index0 > strlength (x)) |
4311 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4497 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4312 | |
4498 | |
4313 | if (cddr (args) != NIL) |
4499 | if (cddr (args) != NIL) |
4314 | { |
4500 | { |
4315 | index1 = ivalue (caddr (args)); |
4501 | index1 = ivalue_unchecked (caddr (args)); |
4316 | |
4502 | |
4317 | if (index1 > strlength (x) || index1 < index0) |
4503 | if (index1 > strlength (x) || index1 < index0) |
4318 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4504 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4319 | } |
4505 | } |
4320 | else |
4506 | else |
4321 | index1 = strlength (x); |
4507 | index1 = strlength (x); |
4322 | |
4508 | |
4323 | len = index1 - index0; |
4509 | len = index1 - index0; |
4324 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4510 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4325 | memcpy (strvalue (x), str + index0, len); |
|
|
4326 | strvalue (x)[len] = 0; |
|
|
4327 | |
4511 | |
4328 | s_return (x); |
4512 | s_return (x); |
4329 | } |
4513 | } |
4330 | |
4514 | |
4331 | case OP_VECTOR: /* vector */ |
4515 | case OP_VECTOR: /* vector */ |
… | |
… | |
4343 | if (SCHEME_V->no_memory) |
4527 | if (SCHEME_V->no_memory) |
4344 | s_return (S_SINK); |
4528 | s_return (S_SINK); |
4345 | #endif |
4529 | #endif |
4346 | |
4530 | |
4347 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4531 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4348 | set_vector_elem (vec, i, car (x)); |
4532 | vector_set (vec, i, car (x)); |
4349 | |
4533 | |
4350 | s_return (vec); |
4534 | s_return (vec); |
4351 | } |
4535 | } |
4352 | |
4536 | |
4353 | case OP_MKVECTOR: /* make-vector */ |
4537 | case OP_MKVECTOR: /* make-vector */ |
4354 | { |
4538 | { |
4355 | pointer fill = NIL; |
4539 | pointer fill = NIL; |
4356 | int len; |
|
|
4357 | pointer vec; |
4540 | pointer vec; |
4358 | |
|
|
4359 | len = ivalue (x); |
4541 | int len = ivalue_unchecked (x); |
4360 | |
4542 | |
4361 | if (cdr (args) != NIL) |
4543 | if (cdr (args) != NIL) |
4362 | fill = cadr (args); |
4544 | fill = cadr (args); |
4363 | |
4545 | |
4364 | vec = mk_vector (SCHEME_A_ len); |
4546 | vec = mk_vector (SCHEME_A_ len); |
… | |
… | |
4367 | if (SCHEME_V->no_memory) |
4549 | if (SCHEME_V->no_memory) |
4368 | s_return (S_SINK); |
4550 | s_return (S_SINK); |
4369 | #endif |
4551 | #endif |
4370 | |
4552 | |
4371 | if (fill != NIL) |
4553 | if (fill != NIL) |
4372 | fill_vector (vec, fill); |
4554 | fill_vector (vec, 0, fill); |
4373 | |
4555 | |
4374 | s_return (vec); |
4556 | s_return (vec); |
4375 | } |
4557 | } |
4376 | |
4558 | |
4377 | case OP_VECLEN: /* vector-length */ |
4559 | case OP_VECLEN: /* vector-length */ |
4378 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4560 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4379 | |
4561 | |
|
|
4562 | case OP_VECRESIZE: |
|
|
4563 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4564 | s_return (x); |
|
|
4565 | |
4380 | case OP_VECREF: /* vector-ref */ |
4566 | case OP_VECREF: /* vector-ref */ |
4381 | { |
4567 | { |
4382 | int index; |
|
|
4383 | |
|
|
4384 | index = ivalue (cadr (args)); |
4568 | int index = ivalue_unchecked (cadr (args)); |
4385 | |
4569 | |
4386 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4570 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4387 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4571 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4388 | |
4572 | |
4389 | s_return (vector_elem (x, index)); |
4573 | s_return (vector_get (x, index)); |
4390 | } |
4574 | } |
4391 | |
4575 | |
4392 | case OP_VECSET: /* vector-set! */ |
4576 | case OP_VECSET: /* vector-set! */ |
4393 | { |
4577 | { |
4394 | int index; |
4578 | int index = ivalue_unchecked (cadr (args)); |
4395 | |
4579 | |
4396 | if (is_immutable (x)) |
4580 | if (is_immutable (x)) |
4397 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4581 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4398 | |
4582 | |
4399 | index = ivalue (cadr (args)); |
|
|
4400 | |
|
|
4401 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4583 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4402 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4584 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4403 | |
4585 | |
4404 | set_vector_elem (x, index, caddr (args)); |
4586 | vector_set (x, index, caddr (args)); |
4405 | s_return (x); |
4587 | s_return (x); |
4406 | } |
4588 | } |
4407 | } |
4589 | } |
4408 | |
4590 | |
4409 | if (USE_ERROR_CHECKING) abort (); |
4591 | if (USE_ERROR_CHECKING) abort (); |
4410 | } |
4592 | } |
4411 | |
4593 | |
4412 | INTERFACE int |
4594 | /* relational ops */ |
4413 | is_list (SCHEME_P_ pointer a) |
4595 | ecb_hot static int |
4414 | { |
|
|
4415 | return list_length (SCHEME_A_ a) >= 0; |
|
|
4416 | } |
|
|
4417 | |
|
|
4418 | /* Result is: |
|
|
4419 | proper list: length |
|
|
4420 | circular list: -1 |
|
|
4421 | not even a pair: -2 |
|
|
4422 | dotted list: -2 minus length before dot |
|
|
4423 | */ |
|
|
4424 | INTERFACE int |
|
|
4425 | list_length (SCHEME_P_ pointer a) |
|
|
4426 | { |
|
|
4427 | int i = 0; |
|
|
4428 | pointer slow, fast; |
|
|
4429 | |
|
|
4430 | slow = fast = a; |
|
|
4431 | |
|
|
4432 | while (1) |
|
|
4433 | { |
|
|
4434 | if (fast == NIL) |
|
|
4435 | return i; |
|
|
4436 | |
|
|
4437 | if (!is_pair (fast)) |
|
|
4438 | return -2 - i; |
|
|
4439 | |
|
|
4440 | fast = cdr (fast); |
|
|
4441 | ++i; |
|
|
4442 | |
|
|
4443 | if (fast == NIL) |
|
|
4444 | return i; |
|
|
4445 | |
|
|
4446 | if (!is_pair (fast)) |
|
|
4447 | return -2 - i; |
|
|
4448 | |
|
|
4449 | ++i; |
|
|
4450 | fast = cdr (fast); |
|
|
4451 | |
|
|
4452 | /* Safe because we would have already returned if `fast' |
|
|
4453 | encountered a non-pair. */ |
|
|
4454 | slow = cdr (slow); |
|
|
4455 | |
|
|
4456 | if (fast == slow) |
|
|
4457 | { |
|
|
4458 | /* the fast pointer has looped back around and caught up |
|
|
4459 | with the slow pointer, hence the structure is circular, |
|
|
4460 | not of finite length, and therefore not a list */ |
|
|
4461 | return -1; |
|
|
4462 | } |
|
|
4463 | } |
|
|
4464 | } |
|
|
4465 | |
|
|
4466 | static int |
|
|
4467 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4596 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4468 | { |
4597 | { |
4469 | pointer x = SCHEME_V->args; |
4598 | pointer x = SCHEME_V->args; |
4470 | |
4599 | |
4471 | for (;;) |
4600 | for (;;) |
… | |
… | |
4492 | } |
4621 | } |
4493 | |
4622 | |
4494 | s_return (S_T); |
4623 | s_return (S_T); |
4495 | } |
4624 | } |
4496 | |
4625 | |
4497 | static int |
4626 | /* predicates */ |
|
|
4627 | ecb_hot static int |
4498 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4628 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4499 | { |
4629 | { |
4500 | pointer args = SCHEME_V->args; |
4630 | pointer args = SCHEME_V->args; |
4501 | pointer a = car (args); |
4631 | pointer a = car (args); |
4502 | pointer d = cdr (args); |
4632 | pointer d = cdr (args); |
4503 | int r; |
4633 | int r; |
4504 | |
4634 | |
4505 | switch (op) |
4635 | switch (op) |
4506 | { |
4636 | { |
4507 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4637 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4508 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
4638 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break; |
4509 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4639 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4510 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4640 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4511 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
4641 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4642 | case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break; |
4512 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4643 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4513 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4644 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4514 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4645 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4515 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4646 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4516 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4647 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4517 | |
4648 | |
4518 | #if USE_CHAR_CLASSIFIERS |
4649 | #if USE_CHAR_CLASSIFIERS |
4519 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; |
4650 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4520 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; |
4651 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4521 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; |
4652 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4522 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; |
4653 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break; |
4523 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; |
4654 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break; |
4524 | #endif |
4655 | #endif |
4525 | |
4656 | |
4526 | #if USE_PORTS |
4657 | #if USE_PORTS |
4527 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4658 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4528 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
4659 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
… | |
… | |
4548 | } |
4679 | } |
4549 | |
4680 | |
4550 | s_retbool (r); |
4681 | s_retbool (r); |
4551 | } |
4682 | } |
4552 | |
4683 | |
4553 | static int |
4684 | /* promises, list ops, ports */ |
|
|
4685 | ecb_hot static int |
4554 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4686 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4555 | { |
4687 | { |
4556 | pointer args = SCHEME_V->args; |
4688 | pointer args = SCHEME_V->args; |
4557 | pointer a = car (args); |
4689 | pointer a = car (args); |
4558 | pointer x, y; |
4690 | pointer x, y; |
… | |
… | |
4571 | } |
4703 | } |
4572 | else |
4704 | else |
4573 | s_return (SCHEME_V->code); |
4705 | s_return (SCHEME_V->code); |
4574 | |
4706 | |
4575 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4707 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4576 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4708 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4577 | s_return (SCHEME_V->value); |
4709 | s_return (SCHEME_V->value); |
4578 | |
4710 | |
4579 | #if USE_PORTS |
4711 | #if USE_PORTS |
|
|
4712 | |
|
|
4713 | case OP_EOF_OBJECT: /* eof-object */ |
|
|
4714 | s_return (S_EOF); |
4580 | |
4715 | |
4581 | case OP_WRITE: /* write */ |
4716 | case OP_WRITE: /* write */ |
4582 | case OP_DISPLAY: /* display */ |
4717 | case OP_DISPLAY: /* display */ |
4583 | case OP_WRITE_CHAR: /* write-char */ |
4718 | case OP_WRITE_CHAR: /* write-char */ |
4584 | if (is_pair (cdr (SCHEME_V->args))) |
4719 | if (is_pair (cdr (SCHEME_V->args))) |
… | |
… | |
4598 | else |
4733 | else |
4599 | SCHEME_V->print_flag = 0; |
4734 | SCHEME_V->print_flag = 0; |
4600 | |
4735 | |
4601 | s_goto (OP_P0LIST); |
4736 | s_goto (OP_P0LIST); |
4602 | |
4737 | |
|
|
4738 | //TODO: move to scheme |
4603 | case OP_NEWLINE: /* newline */ |
4739 | case OP_NEWLINE: /* newline */ |
4604 | if (is_pair (args)) |
4740 | if (is_pair (args)) |
4605 | { |
4741 | { |
4606 | if (a != SCHEME_V->outport) |
4742 | if (a != SCHEME_V->outport) |
4607 | { |
4743 | { |
… | |
… | |
4609 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4745 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4610 | SCHEME_V->outport = a; |
4746 | SCHEME_V->outport = a; |
4611 | } |
4747 | } |
4612 | } |
4748 | } |
4613 | |
4749 | |
4614 | putstr (SCHEME_A_ "\n"); |
4750 | putcharacter (SCHEME_A_ '\n'); |
4615 | s_return (S_T); |
4751 | s_return (S_T); |
4616 | #endif |
4752 | #endif |
4617 | |
4753 | |
4618 | case OP_ERR0: /* error */ |
4754 | case OP_ERR0: /* error */ |
4619 | SCHEME_V->retcode = -1; |
4755 | SCHEME_V->retcode = -1; |
… | |
… | |
4628 | putstr (SCHEME_A_ strvalue (car (args))); |
4764 | putstr (SCHEME_A_ strvalue (car (args))); |
4629 | SCHEME_V->args = cdr (args); |
4765 | SCHEME_V->args = cdr (args); |
4630 | s_goto (OP_ERR1); |
4766 | s_goto (OP_ERR1); |
4631 | |
4767 | |
4632 | case OP_ERR1: /* error */ |
4768 | case OP_ERR1: /* error */ |
4633 | putstr (SCHEME_A_ " "); |
4769 | putcharacter (SCHEME_A_ ' '); |
4634 | |
4770 | |
4635 | if (args != NIL) |
4771 | if (args != NIL) |
4636 | { |
4772 | { |
4637 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4773 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4638 | SCHEME_V->args = a; |
4774 | SCHEME_V->args = a; |
4639 | SCHEME_V->print_flag = 1; |
4775 | SCHEME_V->print_flag = 1; |
4640 | s_goto (OP_P0LIST); |
4776 | s_goto (OP_P0LIST); |
4641 | } |
4777 | } |
4642 | else |
4778 | else |
4643 | { |
4779 | { |
4644 | putstr (SCHEME_A_ "\n"); |
4780 | putcharacter (SCHEME_A_ '\n'); |
4645 | |
4781 | |
4646 | if (SCHEME_V->interactive_repl) |
4782 | if (SCHEME_V->interactive_repl) |
4647 | s_goto (OP_T0LVL); |
4783 | s_goto (OP_T0LVL); |
4648 | else |
4784 | else |
4649 | return -1; |
4785 | return -1; |
… | |
… | |
4726 | SCHEME_V->gc_verbose = (a != S_F); |
4862 | SCHEME_V->gc_verbose = (a != S_F); |
4727 | s_retbool (was); |
4863 | s_retbool (was); |
4728 | } |
4864 | } |
4729 | |
4865 | |
4730 | case OP_NEWSEGMENT: /* new-segment */ |
4866 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4867 | #if 0 |
4731 | if (!is_pair (args) || !is_number (a)) |
4868 | if (!is_pair (args) || !is_number (a)) |
4732 | Error_0 ("new-segment: argument must be a number"); |
4869 | Error_0 ("new-segment: argument must be a number"); |
4733 | |
4870 | #endif |
4734 | alloc_cellseg (SCHEME_A_ (int)ivalue (a)); |
4871 | s_retbool (alloc_cellseg (SCHEME_A)); |
4735 | |
|
|
4736 | s_return (S_T); |
|
|
4737 | |
4872 | |
4738 | case OP_OBLIST: /* oblist */ |
4873 | case OP_OBLIST: /* oblist */ |
4739 | s_return (oblist_all_symbols (SCHEME_A)); |
4874 | s_return (oblist_all_symbols (SCHEME_A)); |
4740 | |
4875 | |
4741 | #if USE_PORTS |
4876 | #if USE_PORTS |
… | |
… | |
4811 | s_return (p == NIL ? S_F : p); |
4946 | s_return (p == NIL ? S_F : p); |
4812 | } |
4947 | } |
4813 | |
4948 | |
4814 | case OP_GET_OUTSTRING: /* get-output-string */ |
4949 | case OP_GET_OUTSTRING: /* get-output-string */ |
4815 | { |
4950 | { |
4816 | port *p; |
4951 | port *p = port (a); |
4817 | |
4952 | |
4818 | if ((p = a->object.port)->kind & port_string) |
4953 | if (p->kind & port_string) |
4819 | { |
4954 | { |
4820 | off_t size; |
4955 | off_t size; |
4821 | char *str; |
4956 | char *str; |
4822 | |
4957 | |
4823 | size = p->rep.string.curr - p->rep.string.start + 1; |
4958 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4858 | } |
4993 | } |
4859 | |
4994 | |
4860 | if (USE_ERROR_CHECKING) abort (); |
4995 | if (USE_ERROR_CHECKING) abort (); |
4861 | } |
4996 | } |
4862 | |
4997 | |
4863 | static int |
4998 | /* reading */ |
|
|
4999 | ecb_cold static int |
4864 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
5000 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4865 | { |
5001 | { |
4866 | pointer args = SCHEME_V->args; |
5002 | pointer args = SCHEME_V->args; |
4867 | pointer x; |
5003 | pointer x; |
4868 | |
5004 | |
… | |
… | |
4928 | int res; |
5064 | int res; |
4929 | |
5065 | |
4930 | if (is_pair (args)) |
5066 | if (is_pair (args)) |
4931 | p = car (args); |
5067 | p = car (args); |
4932 | |
5068 | |
4933 | res = p->object.port->kind & port_string; |
5069 | res = port (p)->kind & port_string; |
4934 | |
5070 | |
4935 | s_retbool (res); |
5071 | s_retbool (res); |
4936 | } |
5072 | } |
4937 | |
5073 | |
4938 | case OP_SET_INPORT: /* set-input-port */ |
5074 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
4997 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5133 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
4998 | SCHEME_V->tok = token (SCHEME_A); |
5134 | SCHEME_V->tok = token (SCHEME_A); |
4999 | s_goto (OP_RDSEXPR); |
5135 | s_goto (OP_RDSEXPR); |
5000 | |
5136 | |
5001 | case TOK_ATOM: |
5137 | case TOK_ATOM: |
5002 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); |
5138 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
|
|
5139 | |
|
|
5140 | case TOK_DOTATOM: |
|
|
5141 | SCHEME_V->strbuff[0] = '.'; |
|
|
5142 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
|
|
5143 | |
|
|
5144 | case TOK_STRATOM: |
|
|
5145 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5146 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5147 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
5003 | |
5148 | |
5004 | case TOK_DQUOTE: |
5149 | case TOK_DQUOTE: |
5005 | x = readstrexp (SCHEME_A); |
5150 | x = readstrexp (SCHEME_A_ '"'); |
5006 | |
5151 | |
5007 | if (x == S_F) |
5152 | if (x == S_F) |
5008 | Error_0 ("Error reading string"); |
5153 | Error_0 ("Error reading string"); |
5009 | |
5154 | |
5010 | setimmutable (x); |
5155 | setimmutable (x); |
… | |
… | |
5022 | s_goto (OP_EVAL); |
5167 | s_goto (OP_EVAL); |
5023 | } |
5168 | } |
5024 | } |
5169 | } |
5025 | |
5170 | |
5026 | case TOK_SHARP_CONST: |
5171 | case TOK_SHARP_CONST: |
5027 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) |
5172 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
5028 | Error_0 ("undefined sharp expression"); |
5173 | Error_0 ("undefined sharp expression"); |
5029 | else |
5174 | else |
5030 | s_return (x); |
5175 | s_return (x); |
5031 | |
5176 | |
5032 | default: |
5177 | default: |
… | |
… | |
5128 | pointer b = cdr (args); |
5273 | pointer b = cdr (args); |
5129 | int ok_abbr = ok_abbrev (b); |
5274 | int ok_abbr = ok_abbrev (b); |
5130 | SCHEME_V->args = car (b); |
5275 | SCHEME_V->args = car (b); |
5131 | |
5276 | |
5132 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5277 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5133 | putstr (SCHEME_A_ "'"); |
5278 | putcharacter (SCHEME_A_ '\''); |
5134 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5279 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5135 | putstr (SCHEME_A_ "`"); |
5280 | putcharacter (SCHEME_A_ '`'); |
5136 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5281 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5137 | putstr (SCHEME_A_ ","); |
5282 | putcharacter (SCHEME_A_ ','); |
5138 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5283 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5139 | putstr (SCHEME_A_ ",@"); |
5284 | putstr (SCHEME_A_ ",@"); |
5140 | else |
5285 | else |
5141 | { |
5286 | { |
5142 | putstr (SCHEME_A_ "("); |
5287 | putcharacter (SCHEME_A_ '('); |
5143 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5288 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5144 | SCHEME_V->args = a; |
5289 | SCHEME_V->args = a; |
5145 | } |
5290 | } |
5146 | |
5291 | |
5147 | s_goto (OP_P0LIST); |
5292 | s_goto (OP_P0LIST); |
… | |
… | |
5149 | |
5294 | |
5150 | case OP_P1LIST: |
5295 | case OP_P1LIST: |
5151 | if (is_pair (args)) |
5296 | if (is_pair (args)) |
5152 | { |
5297 | { |
5153 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5298 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5154 | putstr (SCHEME_A_ " "); |
5299 | putcharacter (SCHEME_A_ ' '); |
5155 | SCHEME_V->args = car (args); |
5300 | SCHEME_V->args = car (args); |
5156 | s_goto (OP_P0LIST); |
5301 | s_goto (OP_P0LIST); |
5157 | } |
5302 | } |
5158 | else if (is_vector (args)) |
5303 | else if (is_vector (args)) |
5159 | { |
5304 | { |
… | |
… | |
5167 | { |
5312 | { |
5168 | putstr (SCHEME_A_ " . "); |
5313 | putstr (SCHEME_A_ " . "); |
5169 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5314 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5170 | } |
5315 | } |
5171 | |
5316 | |
5172 | putstr (SCHEME_A_ ")"); |
5317 | putcharacter (SCHEME_A_ ')'); |
5173 | s_return (S_T); |
5318 | s_return (S_T); |
5174 | } |
5319 | } |
5175 | |
5320 | |
5176 | case OP_PVECFROM: |
5321 | case OP_PVECFROM: |
5177 | { |
5322 | { |
… | |
… | |
5179 | pointer vec = car (args); |
5324 | pointer vec = car (args); |
5180 | int len = veclength (vec); |
5325 | int len = veclength (vec); |
5181 | |
5326 | |
5182 | if (i == len) |
5327 | if (i == len) |
5183 | { |
5328 | { |
5184 | putstr (SCHEME_A_ ")"); |
5329 | putcharacter (SCHEME_A_ ')'); |
5185 | s_return (S_T); |
5330 | s_return (S_T); |
5186 | } |
5331 | } |
5187 | else |
5332 | else |
5188 | { |
5333 | { |
5189 | pointer elem = vector_elem (vec, i); |
5334 | pointer elem = vector_get (vec, i); |
5190 | |
5335 | |
5191 | ivalue_unchecked (cdr (args)) = i + 1; |
5336 | ivalue_unchecked (cdr (args)) = i + 1; |
5192 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5337 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5193 | SCHEME_V->args = elem; |
5338 | SCHEME_V->args = elem; |
5194 | |
5339 | |
5195 | if (i > 0) |
5340 | if (i > 0) |
5196 | putstr (SCHEME_A_ " "); |
5341 | putcharacter (SCHEME_A_ ' '); |
5197 | |
5342 | |
5198 | s_goto (OP_P0LIST); |
5343 | s_goto (OP_P0LIST); |
5199 | } |
5344 | } |
5200 | } |
5345 | } |
5201 | } |
5346 | } |
5202 | |
5347 | |
5203 | if (USE_ERROR_CHECKING) abort (); |
5348 | if (USE_ERROR_CHECKING) abort (); |
5204 | } |
5349 | } |
5205 | |
5350 | |
5206 | static int |
5351 | /* list ops */ |
|
|
5352 | ecb_hot static int |
5207 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5353 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5208 | { |
5354 | { |
5209 | pointer args = SCHEME_V->args; |
5355 | pointer args = SCHEME_V->args; |
5210 | pointer a = car (args); |
5356 | pointer a = car (args); |
5211 | pointer x, y; |
5357 | pointer x, y; |
… | |
… | |
5254 | |
5400 | |
5255 | case OP_CLOSUREP: /* closure? */ |
5401 | case OP_CLOSUREP: /* closure? */ |
5256 | /* |
5402 | /* |
5257 | * Note, macro object is also a closure. |
5403 | * Note, macro object is also a closure. |
5258 | * Therefore, (closure? <#MACRO>) ==> #t |
5404 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5405 | * (schmorp) well, obviously not, fix? TODO |
5259 | */ |
5406 | */ |
5260 | s_retbool (is_closure (a)); |
5407 | s_retbool (is_closure (a)); |
5261 | |
5408 | |
5262 | case OP_MACROP: /* macro? */ |
5409 | case OP_MACROP: /* macro? */ |
5263 | s_retbool (is_macro (a)); |
5410 | s_retbool (is_macro (a)); |
… | |
… | |
5268 | |
5415 | |
5269 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5416 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5270 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5417 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5271 | |
5418 | |
5272 | typedef int (*test_predicate)(pointer); |
5419 | typedef int (*test_predicate)(pointer); |
5273 | static int |
5420 | |
|
|
5421 | ecb_hot static int |
5274 | is_any (pointer p) |
5422 | tst_any (pointer p) |
5275 | { |
5423 | { |
5276 | return 1; |
5424 | return 1; |
5277 | } |
5425 | } |
5278 | |
5426 | |
5279 | static int |
5427 | ecb_hot static int |
5280 | is_nonneg (pointer p) |
5428 | tst_inonneg (pointer p) |
5281 | { |
5429 | { |
5282 | return ivalue (p) >= 0 && is_integer (p); |
5430 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5283 | } |
5431 | } |
5284 | |
5432 | |
5285 | static int |
5433 | ecb_hot static int |
5286 | tst_is_list (pointer p) |
5434 | tst_is_list (SCHEME_P_ pointer p) |
5287 | { |
5435 | { |
5288 | return p == NIL || is_pair (p); |
5436 | return p == NIL || is_pair (p); |
5289 | } |
5437 | } |
5290 | |
5438 | |
5291 | /* Correspond carefully with following defines! */ |
5439 | /* Correspond carefully with following defines! */ |
5292 | static struct |
5440 | static struct |
5293 | { |
5441 | { |
5294 | test_predicate fct; |
5442 | test_predicate fct; |
5295 | const char *kind; |
5443 | const char *kind; |
5296 | } tests[] = |
5444 | } tests[] = { |
5297 | { |
|
|
5298 | { is_any, 0 }, |
5445 | { tst_any , 0 }, |
5299 | { is_string, "string" }, |
5446 | { is_string , "string" }, |
5300 | { is_symbol, "symbol" }, |
5447 | { is_symbol , "symbol" }, |
5301 | { is_port, "port" }, |
5448 | { is_port , "port" }, |
5302 | { is_inport, "input port" }, |
5449 | { is_inport , "input port" }, |
5303 | { is_outport, "output port" }, |
5450 | { is_outport , "output port" }, |
5304 | { is_environment, "environment" }, |
5451 | { is_environment, "environment" }, |
5305 | { is_pair, "pair" }, |
5452 | { is_pair , "pair" }, |
5306 | { tst_is_list, "pair or '()" }, |
5453 | { 0 , "pair or '()" }, |
5307 | { is_character, "character" }, |
5454 | { is_character , "character" }, |
5308 | { is_vector, "vector" }, |
5455 | { is_vector , "vector" }, |
5309 | { is_number, "number" }, |
5456 | { is_number , "number" }, |
5310 | { is_integer, "integer" }, |
5457 | { is_integer , "integer" }, |
5311 | { is_nonneg, "non-negative integer" } |
5458 | { tst_inonneg , "non-negative integer" } |
5312 | }; |
5459 | }; |
5313 | |
5460 | |
5314 | #define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ |
5461 | #define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ |
5315 | #define TST_ANY "\001" |
5462 | #define TST_ANY "\001" |
5316 | #define TST_STRING "\002" |
5463 | #define TST_STRING "\002" |
… | |
… | |
5334 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5481 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5335 | #include "opdefines.h" |
5482 | #include "opdefines.h" |
5336 | #undef OP_DEF |
5483 | #undef OP_DEF |
5337 | ; |
5484 | ; |
5338 | |
5485 | |
5339 | static const char * |
5486 | ecb_cold static const char * |
5340 | opname (int idx) |
5487 | opname (int idx) |
5341 | { |
5488 | { |
5342 | const char *name = opnames; |
5489 | const char *name = opnames; |
5343 | |
5490 | |
5344 | /* should do this at compile time, but would require external program, right? */ |
5491 | /* should do this at compile time, but would require external program, right? */ |
… | |
… | |
5346 | name += strlen (name) + 1; |
5493 | name += strlen (name) + 1; |
5347 | |
5494 | |
5348 | return *name ? name : "ILLEGAL"; |
5495 | return *name ? name : "ILLEGAL"; |
5349 | } |
5496 | } |
5350 | |
5497 | |
5351 | static const char * |
5498 | ecb_cold static const char * |
5352 | procname (pointer x) |
5499 | procname (pointer x) |
5353 | { |
5500 | { |
5354 | return opname (procnum (x)); |
5501 | return opname (procnum (x)); |
5355 | } |
5502 | } |
5356 | |
5503 | |
5357 | typedef struct |
5504 | typedef struct |
5358 | { |
5505 | { |
5359 | uint8_t func; |
5506 | uint8_t func; |
5360 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
5507 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
5361 | uint8_t builtin; |
5508 | uint8_t builtin; |
|
|
5509 | #if USE_ERROR_CHECKING |
5362 | uint8_t min_arity; |
5510 | uint8_t min_arity; |
5363 | uint8_t max_arity; |
5511 | uint8_t max_arity; |
5364 | char arg_tests_encoding[3]; |
5512 | char arg_tests_encoding[3]; |
|
|
5513 | #endif |
5365 | } op_code_info; |
5514 | } op_code_info; |
5366 | |
5515 | |
5367 | static const op_code_info dispatch_table[] = { |
5516 | static const op_code_info dispatch_table[] = { |
|
|
5517 | #if USE_ERROR_CHECKING |
5368 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, |
5518 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, |
|
|
5519 | #else |
|
|
5520 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 }, |
|
|
5521 | #endif |
5369 | #include "opdefines.h" |
5522 | #include "opdefines.h" |
5370 | #undef OP_DEF |
5523 | #undef OP_DEF |
5371 | {0} |
5524 | {0} |
5372 | }; |
5525 | }; |
5373 | |
5526 | |
5374 | /* kernel of this interpreter */ |
5527 | /* kernel of this interpreter */ |
5375 | static void ecb_hot |
5528 | ecb_hot static void |
5376 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5529 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5377 | { |
5530 | { |
5378 | SCHEME_V->op = op; |
5531 | SCHEME_V->op = op; |
5379 | |
5532 | |
5380 | for (;;) |
5533 | for (;;) |
… | |
… | |
5415 | { |
5568 | { |
5416 | pointer arg = car (arglist); |
5569 | pointer arg = car (arglist); |
5417 | |
5570 | |
5418 | j = t[0]; |
5571 | j = t[0]; |
5419 | |
5572 | |
|
|
5573 | /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */ |
|
|
5574 | if (j == TST_LIST[0]) |
|
|
5575 | { |
|
|
5576 | if (!tst_is_list (SCHEME_A_ arg)) |
|
|
5577 | break; |
|
|
5578 | } |
|
|
5579 | else |
|
|
5580 | { |
5420 | if (!tests[j - 1].fct (arg)) |
5581 | if (!tests[j - 1].fct (arg)) |
5421 | break; |
5582 | break; |
|
|
5583 | } |
5422 | |
5584 | |
5423 | if (t[1]) /* last test is replicated as necessary */ |
5585 | if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */ |
5424 | t++; |
5586 | t++; |
5425 | |
5587 | |
5426 | arglist = cdr (arglist); |
5588 | arglist = cdr (arglist); |
5427 | i++; |
5589 | i++; |
5428 | } |
5590 | } |
… | |
… | |
5454 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5616 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5455 | return; |
5617 | return; |
5456 | |
5618 | |
5457 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5619 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5458 | { |
5620 | { |
5459 | xwrstr ("No memory!\n"); |
5621 | putstr (SCHEME_A_ "No memory!\n"); |
5460 | return; |
5622 | return; |
5461 | } |
5623 | } |
5462 | } |
5624 | } |
5463 | } |
5625 | } |
5464 | |
5626 | |
5465 | /* ========== Initialization of internal keywords ========== */ |
5627 | /* ========== Initialization of internal keywords ========== */ |
5466 | |
5628 | |
5467 | static void |
5629 | ecb_cold static void |
5468 | assign_syntax (SCHEME_P_ const char *name) |
5630 | assign_syntax (SCHEME_P_ const char *name) |
5469 | { |
5631 | { |
5470 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5632 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5471 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5633 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5472 | } |
5634 | } |
5473 | |
5635 | |
5474 | static void |
5636 | ecb_cold static void |
5475 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5637 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5476 | { |
5638 | { |
5477 | pointer x = mk_symbol (SCHEME_A_ name); |
5639 | pointer x = mk_symbol (SCHEME_A_ name); |
5478 | pointer y = mk_proc (SCHEME_A_ op); |
5640 | pointer y = mk_proc (SCHEME_A_ op); |
5479 | new_slot_in_env (SCHEME_A_ x, y); |
5641 | new_slot_in_env (SCHEME_A_ x, y); |
… | |
… | |
5483 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5645 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5484 | { |
5646 | { |
5485 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5647 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5486 | set_typeflag (y, (T_PROC | T_ATOM)); |
5648 | set_typeflag (y, (T_PROC | T_ATOM)); |
5487 | ivalue_unchecked (y) = op; |
5649 | ivalue_unchecked (y) = op; |
5488 | set_num_integer (y); |
|
|
5489 | return y; |
5650 | return y; |
5490 | } |
5651 | } |
5491 | |
5652 | |
5492 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5653 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5493 | static int |
5654 | ecb_hot static int |
5494 | syntaxnum (pointer p) |
5655 | syntaxnum (pointer p) |
5495 | { |
5656 | { |
5496 | const char *s = strvalue (car (p)); |
5657 | const char *s = strvalue (p); |
5497 | |
5658 | |
5498 | switch (strlength (car (p))) |
5659 | switch (strlength (p)) |
5499 | { |
5660 | { |
5500 | case 2: |
5661 | case 2: |
5501 | if (s[0] == 'i') |
5662 | if (s[0] == 'i') |
5502 | return OP_IF0; /* if */ |
5663 | return OP_IF0; /* if */ |
5503 | else |
5664 | else |
… | |
… | |
5578 | ecb_cold int |
5739 | ecb_cold int |
5579 | scheme_init (SCHEME_P) |
5740 | scheme_init (SCHEME_P) |
5580 | { |
5741 | { |
5581 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5742 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5582 | pointer x; |
5743 | pointer x; |
|
|
5744 | |
|
|
5745 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
5746 | * that memset 0 will also set pointers to 0, but memset does |
|
|
5747 | * of course not guarantee that. screw such systems. |
|
|
5748 | */ |
|
|
5749 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5583 | |
5750 | |
5584 | num_set_fixnum (num_zero, 1); |
5751 | num_set_fixnum (num_zero, 1); |
5585 | num_set_ivalue (num_zero, 0); |
5752 | num_set_ivalue (num_zero, 0); |
5586 | num_set_fixnum (num_one, 1); |
5753 | num_set_fixnum (num_one, 1); |
5587 | num_set_ivalue (num_one, 1); |
5754 | num_set_ivalue (num_one, 1); |
… | |
… | |
5599 | SCHEME_V->save_inport = NIL; |
5766 | SCHEME_V->save_inport = NIL; |
5600 | SCHEME_V->loadport = NIL; |
5767 | SCHEME_V->loadport = NIL; |
5601 | SCHEME_V->nesting = 0; |
5768 | SCHEME_V->nesting = 0; |
5602 | SCHEME_V->interactive_repl = 0; |
5769 | SCHEME_V->interactive_repl = 0; |
5603 | |
5770 | |
5604 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5771 | if (!alloc_cellseg (SCHEME_A)) |
5605 | { |
5772 | { |
5606 | #if USE_ERROR_CHECKING |
5773 | #if USE_ERROR_CHECKING |
5607 | SCHEME_V->no_memory = 1; |
5774 | SCHEME_V->no_memory = 1; |
5608 | return 0; |
5775 | return 0; |
5609 | #endif |
5776 | #endif |
5610 | } |
5777 | } |
5611 | |
5778 | |
5612 | SCHEME_V->gc_verbose = 0; |
5779 | SCHEME_V->gc_verbose = 0; |
5613 | dump_stack_initialize (SCHEME_A); |
5780 | dump_stack_initialize (SCHEME_A); |
5614 | SCHEME_V->code = NIL; |
5781 | SCHEME_V->code = NIL; |
5615 | SCHEME_V->args = NIL; |
5782 | SCHEME_V->args = NIL; |
5616 | SCHEME_V->envir = NIL; |
5783 | SCHEME_V->envir = NIL; |
|
|
5784 | SCHEME_V->value = NIL; |
5617 | SCHEME_V->tracing = 0; |
5785 | SCHEME_V->tracing = 0; |
5618 | |
5786 | |
5619 | /* init NIL */ |
5787 | /* init NIL */ |
5620 | set_typeflag (NIL, T_ATOM | T_MARK); |
5788 | set_typeflag (NIL, T_ATOM | T_MARK); |
5621 | set_car (NIL, NIL); |
5789 | set_car (NIL, NIL); |
… | |
… | |
5677 | |
5845 | |
5678 | return !SCHEME_V->no_memory; |
5846 | return !SCHEME_V->no_memory; |
5679 | } |
5847 | } |
5680 | |
5848 | |
5681 | #if USE_PORTS |
5849 | #if USE_PORTS |
5682 | void |
5850 | ecb_cold void |
5683 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5851 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5684 | { |
5852 | { |
5685 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5853 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5686 | } |
5854 | } |
5687 | |
5855 | |
5688 | void |
5856 | ecb_cold void |
5689 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5857 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5690 | { |
5858 | { |
5691 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5859 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5692 | } |
5860 | } |
5693 | |
5861 | |
5694 | void |
5862 | ecb_cold void |
5695 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5863 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5696 | { |
5864 | { |
5697 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5865 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5698 | } |
5866 | } |
5699 | |
5867 | |
5700 | void |
5868 | ecb_cold void |
5701 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5869 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5702 | { |
5870 | { |
5703 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5871 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5704 | } |
5872 | } |
5705 | #endif |
5873 | #endif |
5706 | |
5874 | |
5707 | void |
5875 | ecb_cold void |
5708 | scheme_set_external_data (SCHEME_P_ void *p) |
5876 | scheme_set_external_data (SCHEME_P_ void *p) |
5709 | { |
5877 | { |
5710 | SCHEME_V->ext_data = p; |
5878 | SCHEME_V->ext_data = p; |
5711 | } |
5879 | } |
5712 | |
5880 | |
… | |
… | |
5760 | } |
5928 | } |
5761 | } |
5929 | } |
5762 | #endif |
5930 | #endif |
5763 | } |
5931 | } |
5764 | |
5932 | |
5765 | void |
5933 | ecb_cold void |
5766 | scheme_load_file (SCHEME_P_ int fin) |
5934 | scheme_load_file (SCHEME_P_ int fin) |
5767 | { |
5935 | { |
5768 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5936 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5769 | } |
5937 | } |
5770 | |
5938 | |
5771 | void |
5939 | ecb_cold void |
5772 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5940 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5773 | { |
5941 | { |
5774 | dump_stack_reset (SCHEME_A); |
5942 | dump_stack_reset (SCHEME_A); |
5775 | SCHEME_V->envir = SCHEME_V->global_env; |
5943 | SCHEME_V->envir = SCHEME_V->global_env; |
5776 | SCHEME_V->file_i = 0; |
5944 | SCHEME_V->file_i = 0; |
5777 | SCHEME_V->load_stack[0].unget = -1; |
5945 | SCHEME_V->load_stack[0].unget = -1; |
5778 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5946 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5779 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5947 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5780 | #if USE_PORTS |
|
|
5781 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5948 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5782 | #endif |
|
|
5783 | SCHEME_V->retcode = 0; |
5949 | SCHEME_V->retcode = 0; |
5784 | |
5950 | |
5785 | #if USE_PORTS |
|
|
5786 | if (fin == STDIN_FILENO) |
5951 | if (fin == STDIN_FILENO) |
5787 | SCHEME_V->interactive_repl = 1; |
5952 | SCHEME_V->interactive_repl = 1; |
5788 | #endif |
|
|
5789 | |
5953 | |
5790 | #if USE_PORTS |
5954 | #if USE_PORTS |
5791 | #if SHOW_ERROR_LINE |
5955 | #if SHOW_ERROR_LINE |
5792 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5956 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5793 | |
5957 | |
… | |
… | |
5797 | #endif |
5961 | #endif |
5798 | |
5962 | |
5799 | SCHEME_V->inport = SCHEME_V->loadport; |
5963 | SCHEME_V->inport = SCHEME_V->loadport; |
5800 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5964 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5801 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5965 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
|
|
5966 | |
5802 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5967 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5803 | |
5968 | |
5804 | if (SCHEME_V->retcode == 0) |
5969 | if (SCHEME_V->retcode == 0) |
5805 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5970 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5806 | } |
5971 | } |
5807 | |
5972 | |
5808 | void |
5973 | ecb_cold void |
5809 | scheme_load_string (SCHEME_P_ const char *cmd) |
5974 | scheme_load_string (SCHEME_P_ const char *cmd) |
5810 | { |
5975 | { |
|
|
5976 | #if USE_PORTs |
5811 | dump_stack_reset (SCHEME_A); |
5977 | dump_stack_reset (SCHEME_A); |
5812 | SCHEME_V->envir = SCHEME_V->global_env; |
5978 | SCHEME_V->envir = SCHEME_V->global_env; |
5813 | SCHEME_V->file_i = 0; |
5979 | SCHEME_V->file_i = 0; |
5814 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5980 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5815 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
5981 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
5816 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
5982 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
5817 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5983 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5818 | #if USE_PORTS |
|
|
5819 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5984 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5820 | #endif |
|
|
5821 | SCHEME_V->retcode = 0; |
5985 | SCHEME_V->retcode = 0; |
5822 | SCHEME_V->interactive_repl = 0; |
5986 | SCHEME_V->interactive_repl = 0; |
5823 | SCHEME_V->inport = SCHEME_V->loadport; |
5987 | SCHEME_V->inport = SCHEME_V->loadport; |
5824 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5988 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5825 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5989 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5826 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5990 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5827 | |
5991 | |
5828 | if (SCHEME_V->retcode == 0) |
5992 | if (SCHEME_V->retcode == 0) |
5829 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5993 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
|
|
5994 | #else |
|
|
5995 | abort (); |
|
|
5996 | #endif |
5830 | } |
5997 | } |
5831 | |
5998 | |
5832 | void |
5999 | ecb_cold void |
5833 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
6000 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5834 | { |
6001 | { |
5835 | pointer x; |
6002 | pointer x; |
5836 | |
6003 | |
5837 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
6004 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
… | |
… | |
5842 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
6009 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5843 | } |
6010 | } |
5844 | |
6011 | |
5845 | #if !STANDALONE |
6012 | #if !STANDALONE |
5846 | |
6013 | |
5847 | void |
6014 | ecb_cold void |
5848 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
6015 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5849 | { |
6016 | { |
5850 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
6017 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5851 | } |
6018 | } |
5852 | |
6019 | |
5853 | void |
6020 | ecb_cold void |
5854 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
6021 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5855 | { |
6022 | { |
5856 | int i; |
6023 | int i; |
5857 | |
6024 | |
5858 | for (i = 0; i < count; i++) |
6025 | for (i = 0; i < count; i++) |
5859 | scheme_register_foreign_func (SCHEME_A_ list + i); |
6026 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5860 | } |
6027 | } |
5861 | |
6028 | |
5862 | pointer |
6029 | ecb_cold pointer |
5863 | scheme_apply0 (SCHEME_P_ const char *procname) |
6030 | scheme_apply0 (SCHEME_P_ const char *procname) |
5864 | { |
6031 | { |
5865 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
6032 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
5866 | } |
6033 | } |
5867 | |
6034 | |
5868 | void |
6035 | ecb_cold void |
5869 | save_from_C_call (SCHEME_P) |
6036 | save_from_C_call (SCHEME_P) |
5870 | { |
6037 | { |
5871 | pointer saved_data = cons (car (S_SINK), |
6038 | pointer saved_data = cons (car (S_SINK), |
5872 | cons (SCHEME_V->envir, |
6039 | cons (SCHEME_V->envir, |
5873 | SCHEME_V->dump)); |
6040 | SCHEME_V->dump)); |
… | |
… | |
5877 | /* Truncate the dump stack so TS will return here when done, not |
6044 | /* Truncate the dump stack so TS will return here when done, not |
5878 | directly resume pre-C-call operations. */ |
6045 | directly resume pre-C-call operations. */ |
5879 | dump_stack_reset (SCHEME_A); |
6046 | dump_stack_reset (SCHEME_A); |
5880 | } |
6047 | } |
5881 | |
6048 | |
5882 | void |
6049 | ecb_cold void |
5883 | restore_from_C_call (SCHEME_P) |
6050 | restore_from_C_call (SCHEME_P) |
5884 | { |
6051 | { |
5885 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
6052 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5886 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
6053 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5887 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
6054 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5888 | /* Pop */ |
6055 | /* Pop */ |
5889 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
6056 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5890 | } |
6057 | } |
5891 | |
6058 | |
5892 | /* "func" and "args" are assumed to be already eval'ed. */ |
6059 | /* "func" and "args" are assumed to be already eval'ed. */ |
5893 | pointer |
6060 | ecb_cold pointer |
5894 | scheme_call (SCHEME_P_ pointer func, pointer args) |
6061 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5895 | { |
6062 | { |
5896 | int old_repl = SCHEME_V->interactive_repl; |
6063 | int old_repl = SCHEME_V->interactive_repl; |
5897 | |
6064 | |
5898 | SCHEME_V->interactive_repl = 0; |
6065 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5905 | SCHEME_V->interactive_repl = old_repl; |
6072 | SCHEME_V->interactive_repl = old_repl; |
5906 | restore_from_C_call (SCHEME_A); |
6073 | restore_from_C_call (SCHEME_A); |
5907 | return SCHEME_V->value; |
6074 | return SCHEME_V->value; |
5908 | } |
6075 | } |
5909 | |
6076 | |
5910 | pointer |
6077 | ecb_cold pointer |
5911 | scheme_eval (SCHEME_P_ pointer obj) |
6078 | scheme_eval (SCHEME_P_ pointer obj) |
5912 | { |
6079 | { |
5913 | int old_repl = SCHEME_V->interactive_repl; |
6080 | int old_repl = SCHEME_V->interactive_repl; |
5914 | |
6081 | |
5915 | SCHEME_V->interactive_repl = 0; |
6082 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5927 | |
6094 | |
5928 | /* ========== Main ========== */ |
6095 | /* ========== Main ========== */ |
5929 | |
6096 | |
5930 | #if STANDALONE |
6097 | #if STANDALONE |
5931 | |
6098 | |
5932 | int |
6099 | ecb_cold int |
5933 | main (int argc, char **argv) |
6100 | main (int argc, char **argv) |
5934 | { |
6101 | { |
5935 | # if USE_MULTIPLICITY |
6102 | # if USE_MULTIPLICITY |
5936 | scheme ssc; |
6103 | scheme ssc; |
5937 | scheme *const SCHEME_V = &ssc; |
6104 | scheme *const SCHEME_V = &ssc; |
… | |
… | |
5939 | # endif |
6106 | # endif |
5940 | int fin; |
6107 | int fin; |
5941 | char *file_name = InitFile; |
6108 | char *file_name = InitFile; |
5942 | int retcode; |
6109 | int retcode; |
5943 | int isfile = 1; |
6110 | int isfile = 1; |
|
|
6111 | system ("ps v $PPID");//D |
5944 | |
6112 | |
5945 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6113 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5946 | { |
6114 | { |
5947 | xwrstr ("Usage: tinyscheme -?\n"); |
6115 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
5948 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6116 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
5949 | xwrstr ("followed by\n"); |
6117 | putstr (SCHEME_A_ "followed by\n"); |
5950 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6118 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
5951 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6119 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
5952 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6120 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
5953 | xwrstr ("Use - as filename for stdin.\n"); |
6121 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
5954 | return 1; |
6122 | return 1; |
5955 | } |
6123 | } |
5956 | |
6124 | |
5957 | if (!scheme_init (SCHEME_A)) |
6125 | if (!scheme_init (SCHEME_A)) |
5958 | { |
6126 | { |
5959 | xwrstr ("Could not initialize!\n"); |
6127 | putstr (SCHEME_A_ "Could not initialize!\n"); |
5960 | return 2; |
6128 | return 2; |
5961 | } |
6129 | } |
5962 | |
6130 | |
5963 | # if USE_PORTS |
6131 | # if USE_PORTS |
5964 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6132 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
5977 | } |
6145 | } |
5978 | #endif |
6146 | #endif |
5979 | |
6147 | |
5980 | do |
6148 | do |
5981 | { |
6149 | { |
5982 | #if USE_PORTS |
|
|
5983 | if (strcmp (file_name, "-") == 0) |
6150 | if (strcmp (file_name, "-") == 0) |
5984 | fin = STDIN_FILENO; |
6151 | fin = STDIN_FILENO; |
5985 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
6152 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
5986 | { |
6153 | { |
5987 | pointer args = NIL; |
6154 | pointer args = NIL; |
… | |
… | |
6005 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
6172 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
6006 | |
6173 | |
6007 | } |
6174 | } |
6008 | else |
6175 | else |
6009 | fin = open (file_name, O_RDONLY); |
6176 | fin = open (file_name, O_RDONLY); |
6010 | #endif |
|
|
6011 | |
6177 | |
6012 | if (isfile && fin < 0) |
6178 | if (isfile && fin < 0) |
6013 | { |
6179 | { |
6014 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6180 | putstr (SCHEME_A_ "Could not open file "); |
|
|
6181 | putstr (SCHEME_A_ file_name); |
|
|
6182 | putcharacter (SCHEME_A_ '\n'); |
6015 | } |
6183 | } |
6016 | else |
6184 | else |
6017 | { |
6185 | { |
6018 | if (isfile) |
6186 | if (isfile) |
6019 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6187 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6020 | else |
6188 | else |
6021 | scheme_load_string (SCHEME_A_ file_name); |
6189 | scheme_load_string (SCHEME_A_ file_name); |
6022 | |
6190 | |
6023 | #if USE_PORTS |
|
|
6024 | if (!isfile || fin != STDIN_FILENO) |
6191 | if (!isfile || fin != STDIN_FILENO) |
6025 | { |
6192 | { |
6026 | if (SCHEME_V->retcode != 0) |
6193 | if (SCHEME_V->retcode != 0) |
6027 | { |
6194 | { |
6028 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6195 | putstr (SCHEME_A_ "Errors encountered reading "); |
|
|
6196 | putstr (SCHEME_A_ file_name); |
|
|
6197 | putcharacter (SCHEME_A_ '\n'); |
6029 | } |
6198 | } |
6030 | |
6199 | |
6031 | if (isfile) |
6200 | if (isfile) |
6032 | close (fin); |
6201 | close (fin); |
6033 | } |
6202 | } |
6034 | #endif |
|
|
6035 | } |
6203 | } |
6036 | |
6204 | |
6037 | file_name = *argv++; |
6205 | file_name = *argv++; |
6038 | } |
6206 | } |
6039 | while (file_name != 0); |
6207 | while (file_name != 0); |