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