… | |
… | |
16 | * (MINISCM) This is a revised and modified version by Akira KIDA. |
16 | * (MINISCM) This is a revised and modified version by Akira KIDA. |
17 | * (MINISCM) current version is 0.85k4 (15 May 1994) |
17 | * (MINISCM) current version is 0.85k4 (15 May 1994) |
18 | * |
18 | * |
19 | */ |
19 | */ |
20 | |
20 | |
|
|
21 | #define EXPERIMENT 1 |
|
|
22 | |
|
|
23 | #if 1 |
21 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
24 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
22 | #include "malloc.c" |
25 | #include "malloc.c" |
|
|
26 | #endif |
23 | |
27 | |
24 | #define SCHEME_SOURCE |
28 | #define SCHEME_SOURCE |
25 | #include "scheme-private.h" |
29 | #include "scheme-private.h" |
26 | #ifndef WIN32 |
30 | #ifndef WIN32 |
27 | # include <unistd.h> |
31 | # include <unistd.h> |
28 | #endif |
32 | #endif |
29 | #if USE_MATH |
33 | #if USE_MATH |
30 | # include <math.h> |
34 | # include <math.h> |
31 | #endif |
35 | #endif |
32 | |
36 | |
|
|
37 | #include "ecb.h" |
|
|
38 | |
33 | #include <sys/types.h> |
39 | #include <sys/types.h> |
34 | #include <sys/stat.h> |
40 | #include <sys/stat.h> |
35 | #include <fcntl.h> |
41 | #include <fcntl.h> |
36 | |
42 | |
|
|
43 | #if !USE_ERROR_CHECKING |
|
|
44 | # define NDEBUG |
|
|
45 | #endif |
|
|
46 | |
|
|
47 | #include <assert.h> |
|
|
48 | #include <stdlib.h> |
37 | #include <string.h> |
49 | #include <string.h> |
38 | #include <stdlib.h> |
|
|
39 | |
50 | |
40 | #include <limits.h> |
51 | #include <limits.h> |
41 | #include <inttypes.h> |
52 | #include <inttypes.h> |
42 | #include <float.h> |
53 | #include <float.h> |
43 | //#include <ctype.h> |
54 | //#include <ctype.h> |
|
|
55 | |
|
|
56 | #if '1' != '0' + 1 \ |
|
|
57 | || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ |
|
|
58 | || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ |
|
|
59 | || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ |
|
|
60 | || 'f' != 'a' + 5 |
|
|
61 | # error "execution character set digits not consecutive" |
|
|
62 | #endif |
44 | |
63 | |
45 | enum { |
64 | enum { |
46 | TOK_EOF, |
65 | TOK_EOF, |
47 | TOK_LPAREN, |
66 | TOK_LPAREN, |
48 | TOK_RPAREN, |
67 | TOK_RPAREN, |
49 | TOK_DOT, |
68 | TOK_DOT, |
50 | TOK_ATOM, |
69 | TOK_ATOM, |
|
|
70 | TOK_DOTATOM, /* atom name starting with '.' */ |
|
|
71 | TOK_STRATOM, /* atom name enclosed in | */ |
51 | TOK_QUOTE, |
72 | TOK_QUOTE, |
52 | TOK_DQUOTE, |
73 | TOK_DQUOTE, |
53 | TOK_BQUOTE, |
74 | TOK_BQUOTE, |
54 | TOK_COMMA, |
75 | TOK_COMMA, |
55 | TOK_ATMARK, |
76 | TOK_ATMARK, |
… | |
… | |
57 | TOK_SHARP_CONST, |
78 | TOK_SHARP_CONST, |
58 | TOK_VEC |
79 | TOK_VEC |
59 | }; |
80 | }; |
60 | |
81 | |
61 | #define BACKQUOTE '`' |
82 | #define BACKQUOTE '`' |
62 | #define DELIMITERS "()\";\f\t\v\n\r " |
83 | #define WHITESPACE " \t\r\n\v\f" |
|
|
84 | #define DELIMITERS "()\";" WHITESPACE |
63 | |
85 | |
64 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
86 | #define NIL POINTER (&SCHEME_V->xNIL) |
65 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
87 | #define S_T POINTER (&SCHEME_V->xT) |
66 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
88 | #define S_F POINTER (&SCHEME_V->xF) |
67 | #define S_SINK (&SCHEME_V->xsink) |
89 | #define S_SINK POINTER (&SCHEME_V->xsink) |
68 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
90 | #define S_EOF POINTER (&SCHEME_V->xEOF_OBJ) |
69 | |
|
|
70 | /* should use libecb */ |
|
|
71 | #if __GNUC__ >= 4 |
|
|
72 | # define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
73 | # define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
|
|
74 | # define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
|
|
75 | #else |
|
|
76 | # define ecb_expect_false(expr) !!(expr) |
|
|
77 | # define ecb_expect_true(expr) !!(expr) |
|
|
78 | #endif |
|
|
79 | |
91 | |
80 | #if !USE_MULTIPLICITY |
92 | #if !USE_MULTIPLICITY |
81 | static scheme sc; |
93 | static scheme sc; |
82 | #endif |
94 | #endif |
83 | |
95 | |
… | |
… | |
91 | } |
103 | } |
92 | |
104 | |
93 | char *p = s; |
105 | char *p = s; |
94 | |
106 | |
95 | do { |
107 | do { |
96 | *p++ = '0' + n % base; |
108 | *p++ = "0123456789abcdef"[n % base]; |
97 | n /= base; |
109 | n /= base; |
98 | } while (n); |
110 | } while (n); |
99 | |
111 | |
100 | *p-- = 0; |
112 | *p-- = 0; |
101 | |
113 | |
… | |
… | |
111 | { |
123 | { |
112 | xbase (s, n, 10); |
124 | xbase (s, n, 10); |
113 | } |
125 | } |
114 | |
126 | |
115 | static void |
127 | static void |
116 | xwrstr (const char *s) |
128 | putnum (SCHEME_P_ long n) |
117 | { |
|
|
118 | write (1, s, strlen (s)); |
|
|
119 | } |
|
|
120 | |
|
|
121 | static void |
|
|
122 | xwrnum (long n) |
|
|
123 | { |
129 | { |
124 | char buf[64]; |
130 | char buf[64]; |
125 | |
131 | |
126 | xnum (buf, n); |
132 | xnum (buf, n); |
127 | xwrstr (buf); |
133 | putstr (SCHEME_A_ buf); |
128 | } |
134 | } |
129 | |
135 | |
130 | static char |
136 | static char |
131 | xtoupper (char c) |
137 | xtoupper (char c) |
132 | { |
138 | { |
… | |
… | |
153 | |
159 | |
154 | #define toupper(c) xtoupper (c) |
160 | #define toupper(c) xtoupper (c) |
155 | #define tolower(c) xtolower (c) |
161 | #define tolower(c) xtolower (c) |
156 | #define isdigit(c) xisdigit (c) |
162 | #define isdigit(c) xisdigit (c) |
157 | |
163 | |
158 | #if USE_STRLWR |
164 | #if USE_IGNORECASE |
159 | static const char * |
165 | static const char * |
160 | strlwr (char *s) |
166 | xstrlwr (char *s) |
161 | { |
167 | { |
162 | const char *p = s; |
168 | const char *p = s; |
163 | |
169 | |
164 | while (*s) |
170 | while (*s) |
165 | { |
171 | { |
… | |
… | |
167 | s++; |
173 | s++; |
168 | } |
174 | } |
169 | |
175 | |
170 | return p; |
176 | return p; |
171 | } |
177 | } |
172 | #endif |
|
|
173 | |
178 | |
|
|
179 | #define stricmp(a,b) strcasecmp (a, b) |
|
|
180 | #define strlwr(s) xstrlwr (s) |
|
|
181 | |
|
|
182 | #else |
174 | #define stricmp(a,b) strcmp (a, b) |
183 | # define stricmp(a,b) strcmp (a, b) |
175 | #define strlwr(s) (s) |
184 | # define strlwr(s) (s) |
|
|
185 | #endif |
176 | |
186 | |
177 | #ifndef prompt |
187 | #ifndef prompt |
178 | # define prompt "ts> " |
188 | # define prompt "ts> " |
179 | #endif |
189 | #endif |
180 | |
190 | |
181 | #ifndef InitFile |
191 | #ifndef InitFile |
182 | # define InitFile "init.scm" |
192 | # define InitFile "init.scm" |
183 | #endif |
193 | #endif |
184 | |
194 | |
185 | #ifndef FIRST_CELLSEGS |
|
|
186 | # define FIRST_CELLSEGS 3 |
|
|
187 | #endif |
|
|
188 | |
|
|
189 | enum scheme_types |
195 | enum scheme_types |
190 | { |
196 | { |
|
|
197 | T_INTEGER, |
191 | T_FREE, |
198 | T_REAL, |
192 | T_STRING, |
199 | T_STRING, |
193 | T_NUMBER, |
|
|
194 | T_SYMBOL, |
200 | T_SYMBOL, |
195 | T_PROC, |
201 | T_PROC, |
196 | T_PAIR, |
202 | T_PAIR, /* also used for free cells */ |
197 | T_CLOSURE, |
203 | T_CLOSURE, |
198 | T_CONTINUATION, |
204 | T_CONTINUATION, |
199 | T_FOREIGN, |
205 | T_FOREIGN, |
200 | T_CHARACTER, |
206 | T_CHARACTER, |
201 | T_PORT, |
207 | T_PORT, |
… | |
… | |
211 | #define T_SYNTAX 0x0010 |
217 | #define T_SYNTAX 0x0010 |
212 | #define T_IMMUTABLE 0x0020 |
218 | #define T_IMMUTABLE 0x0020 |
213 | #define T_ATOM 0x0040 /* only for gc */ |
219 | #define T_ATOM 0x0040 /* only for gc */ |
214 | #define T_MARK 0x0080 /* only for gc */ |
220 | #define T_MARK 0x0080 /* only for gc */ |
215 | |
221 | |
|
|
222 | /* num, for generic arithmetic */ |
|
|
223 | struct num |
|
|
224 | { |
|
|
225 | IVALUE ivalue; |
|
|
226 | #if USE_REAL |
|
|
227 | RVALUE rvalue; |
|
|
228 | char is_fixnum; |
|
|
229 | #endif |
|
|
230 | }; |
|
|
231 | |
|
|
232 | #if USE_REAL |
|
|
233 | # define num_is_fixnum(n) (n).is_fixnum |
|
|
234 | # define num_set_fixnum(n,f) (n).is_fixnum = (f) |
|
|
235 | # define num_ivalue(n) (n).ivalue |
|
|
236 | # define num_rvalue(n) (n).rvalue |
|
|
237 | # define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i) |
|
|
238 | # define num_set_rvalue(n,r) (n).rvalue = (r) |
|
|
239 | #else |
|
|
240 | # define num_is_fixnum(n) 1 |
|
|
241 | # define num_set_fixnum(n,f) 0 |
|
|
242 | # define num_ivalue(n) (n).ivalue |
|
|
243 | # define num_rvalue(n) (n).ivalue |
|
|
244 | # define num_set_ivalue(n,i) (n).ivalue = (i) |
|
|
245 | # define num_set_rvalue(n,r) (n).ivalue = (r) |
|
|
246 | #endif |
|
|
247 | |
216 | enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; |
248 | enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; |
217 | |
249 | |
218 | static num num_op (enum num_op op, num a, num b); |
250 | static num num_op (enum num_op op, num a, num b); |
219 | static num num_intdiv (num a, num b); |
251 | static num num_intdiv (num a, num b); |
220 | static num num_rem (num a, num b); |
252 | static num num_rem (num a, num b); |
221 | static num num_mod (num a, num b); |
253 | static num num_mod (num a, num b); |
222 | |
254 | |
223 | #if USE_MATH |
|
|
224 | static double round_per_R5RS (double x); |
|
|
225 | #endif |
|
|
226 | static int is_zero_rvalue (RVALUE x); |
255 | static int is_zero_rvalue (RVALUE x); |
227 | |
|
|
228 | static INLINE int |
|
|
229 | num_is_integer (pointer p) |
|
|
230 | { |
|
|
231 | return num_is_fixnum (p->object.number); |
|
|
232 | } |
|
|
233 | |
256 | |
234 | static num num_zero; |
257 | static num num_zero; |
235 | static num num_one; |
258 | static num num_one; |
236 | |
259 | |
|
|
260 | /* convert "pointer" to cell* / cell* to pointer */ |
|
|
261 | #define CELL(p) ((struct cell *)(p) + 0) |
|
|
262 | #define POINTER(c) ((void *)((c) - 0)) |
|
|
263 | |
237 | /* macros for cell operations */ |
264 | /* macros for cell operations */ |
238 | #define typeflag(p) ((p)->flag + 0) |
265 | #define typeflag(p) (CELL(p)->flag + 0) |
239 | #define set_typeflag(p,v) ((p)->flag = (v)) |
266 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
240 | #define type(p) (typeflag (p) & T_MASKTYPE) |
267 | #define type(p) (typeflag (p) & T_MASKTYPE) |
241 | |
268 | |
242 | INTERFACE INLINE int |
269 | INTERFACE int |
243 | is_string (pointer p) |
270 | is_string (pointer p) |
244 | { |
271 | { |
245 | return type (p) == T_STRING; |
272 | return type (p) == T_STRING; |
246 | } |
273 | } |
247 | |
274 | |
248 | #define strvalue(p) ((p)->object.string.svalue) |
275 | #define strvalue(p) (CELL(p)->object.string.svalue) |
249 | #define strlength(p) ((p)->object.string.length) |
276 | #define strlength(p) (CELL(p)->object.string.length) |
250 | |
277 | |
251 | INTERFACE int is_list (SCHEME_P_ pointer p); |
|
|
252 | |
|
|
253 | INTERFACE INLINE int |
278 | INTERFACE int |
254 | is_vector (pointer p) |
279 | is_vector (pointer p) |
255 | { |
280 | { |
256 | return type (p) == T_VECTOR; |
281 | return type (p) == T_VECTOR; |
257 | } |
282 | } |
258 | |
283 | |
259 | #define vecvalue(p) ((p)->object.vector.vvalue) |
284 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
260 | #define veclength(p) ((p)->object.vector.length) |
285 | #define veclength(p) (CELL(p)->object.vector.length) |
261 | INTERFACE void fill_vector (pointer vec, pointer obj); |
286 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
262 | INTERFACE uint32_t vector_length (pointer vec); |
|
|
263 | INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); |
287 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
264 | INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); |
288 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
265 | |
289 | |
266 | INTERFACE uint32_t |
290 | INTERFACE int |
267 | vector_length (pointer vec) |
291 | is_integer (pointer p) |
268 | { |
292 | { |
269 | return vec->object.vector.length; |
293 | return type (p) == T_INTEGER; |
270 | } |
294 | } |
271 | |
295 | |
|
|
296 | /* not the same as in scheme, where integers are (correctly :) reals */ |
272 | INTERFACE INLINE int |
297 | INTERFACE int |
|
|
298 | is_real (pointer p) |
|
|
299 | { |
|
|
300 | return type (p) == T_REAL; |
|
|
301 | } |
|
|
302 | |
|
|
303 | INTERFACE int |
273 | is_number (pointer p) |
304 | is_number (pointer p) |
274 | { |
305 | { |
275 | return type (p) == T_NUMBER; |
306 | return is_integer (p) || is_real (p); |
276 | } |
307 | } |
277 | |
308 | |
278 | INTERFACE INLINE int |
309 | INTERFACE int |
279 | is_integer (pointer p) |
|
|
280 | { |
|
|
281 | if (!is_number (p)) |
|
|
282 | return 0; |
|
|
283 | |
|
|
284 | if (num_is_integer (p) || ivalue (p) == rvalue (p)) |
|
|
285 | return 1; |
|
|
286 | |
|
|
287 | return 0; |
|
|
288 | } |
|
|
289 | |
|
|
290 | INTERFACE INLINE int |
|
|
291 | is_real (pointer p) |
|
|
292 | { |
|
|
293 | return is_number (p) && !num_is_fixnum (p->object.number); |
|
|
294 | } |
|
|
295 | |
|
|
296 | INTERFACE INLINE int |
|
|
297 | is_character (pointer p) |
310 | is_character (pointer p) |
298 | { |
311 | { |
299 | return type (p) == T_CHARACTER; |
312 | return type (p) == T_CHARACTER; |
300 | } |
313 | } |
301 | |
314 | |
302 | INTERFACE INLINE char * |
315 | INTERFACE char * |
303 | string_value (pointer p) |
316 | string_value (pointer p) |
304 | { |
317 | { |
305 | return strvalue (p); |
318 | return strvalue (p); |
306 | } |
319 | } |
307 | |
320 | |
308 | INLINE num |
|
|
309 | nvalue (pointer p) |
|
|
310 | { |
|
|
311 | return (p)->object.number; |
|
|
312 | } |
|
|
313 | |
|
|
314 | static IVALUE |
|
|
315 | num_get_ivalue (const num n) |
|
|
316 | { |
|
|
317 | return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n); |
|
|
318 | } |
|
|
319 | |
|
|
320 | static RVALUE |
|
|
321 | num_get_rvalue (const num n) |
|
|
322 | { |
|
|
323 | return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n); |
|
|
324 | } |
|
|
325 | |
|
|
326 | INTERFACE IVALUE |
|
|
327 | ivalue (pointer p) |
|
|
328 | { |
|
|
329 | return num_get_ivalue (p->object.number); |
|
|
330 | } |
|
|
331 | |
|
|
332 | INTERFACE RVALUE |
|
|
333 | rvalue (pointer p) |
|
|
334 | { |
|
|
335 | return num_get_rvalue (p->object.number); |
|
|
336 | } |
|
|
337 | |
|
|
338 | #define ivalue_unchecked(p) ((p)->object.number.value.ivalue) |
321 | #define ivalue_unchecked(p) CELL(p)->object.ivalue |
|
|
322 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
|
|
323 | |
339 | #if USE_REAL |
324 | #if USE_REAL |
340 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
325 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
341 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
326 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
342 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
|
|
343 | #else |
327 | #else |
344 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
328 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
345 | # define set_num_integer(p) 0 |
329 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
346 | # define set_num_real(p) 0 |
|
|
347 | #endif |
330 | #endif |
|
|
331 | |
348 | INTERFACE long |
332 | INTERFACE long |
349 | charvalue (pointer p) |
333 | charvalue (pointer p) |
350 | { |
334 | { |
351 | return ivalue_unchecked (p); |
335 | return ivalue_unchecked (p); |
352 | } |
336 | } |
353 | |
337 | |
|
|
338 | #define port(p) CELL(p)->object.port |
|
|
339 | #define set_port(p,v) port(p) = (v) |
354 | INTERFACE INLINE int |
340 | INTERFACE int |
355 | is_port (pointer p) |
341 | is_port (pointer p) |
356 | { |
342 | { |
357 | return type (p) == T_PORT; |
343 | return type (p) == T_PORT; |
358 | } |
344 | } |
359 | |
345 | |
360 | INTERFACE INLINE int |
346 | INTERFACE int |
361 | is_inport (pointer p) |
347 | is_inport (pointer p) |
362 | { |
348 | { |
363 | return is_port (p) && p->object.port->kind & port_input; |
349 | return is_port (p) && port (p)->kind & port_input; |
364 | } |
350 | } |
365 | |
351 | |
366 | INTERFACE INLINE int |
352 | INTERFACE int |
367 | is_outport (pointer p) |
353 | is_outport (pointer p) |
368 | { |
354 | { |
369 | return is_port (p) && p->object.port->kind & port_output; |
355 | return is_port (p) && port (p)->kind & port_output; |
370 | } |
356 | } |
371 | |
357 | |
372 | INTERFACE INLINE int |
358 | INTERFACE int |
373 | is_pair (pointer p) |
359 | is_pair (pointer p) |
374 | { |
360 | { |
375 | return type (p) == T_PAIR; |
361 | return type (p) == T_PAIR; |
376 | } |
362 | } |
377 | |
363 | |
378 | #define car(p) ((p)->object.cons.car + 0) |
364 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
379 | #define cdr(p) ((p)->object.cons.cdr + 0) |
365 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
380 | |
366 | |
381 | static pointer caar (pointer p) { return car (car (p)); } |
367 | static pointer caar (pointer p) { return car (car (p)); } |
382 | static pointer cadr (pointer p) { return car (cdr (p)); } |
368 | static pointer cadr (pointer p) { return car (cdr (p)); } |
383 | static pointer cdar (pointer p) { return cdr (car (p)); } |
369 | static pointer cdar (pointer p) { return cdr (car (p)); } |
384 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
370 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
… | |
… | |
388 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
374 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
389 | |
375 | |
390 | INTERFACE void |
376 | INTERFACE void |
391 | set_car (pointer p, pointer q) |
377 | set_car (pointer p, pointer q) |
392 | { |
378 | { |
393 | p->object.cons.car = q; |
379 | CELL(p)->object.cons.car = CELL (q); |
394 | } |
380 | } |
395 | |
381 | |
396 | INTERFACE void |
382 | INTERFACE void |
397 | set_cdr (pointer p, pointer q) |
383 | set_cdr (pointer p, pointer q) |
398 | { |
384 | { |
399 | p->object.cons.cdr = q; |
385 | CELL(p)->object.cons.cdr = CELL (q); |
400 | } |
386 | } |
401 | |
387 | |
402 | INTERFACE pointer |
388 | INTERFACE pointer |
403 | pair_car (pointer p) |
389 | pair_car (pointer p) |
404 | { |
390 | { |
… | |
… | |
409 | pair_cdr (pointer p) |
395 | pair_cdr (pointer p) |
410 | { |
396 | { |
411 | return cdr (p); |
397 | return cdr (p); |
412 | } |
398 | } |
413 | |
399 | |
414 | INTERFACE INLINE int |
400 | INTERFACE int |
415 | is_symbol (pointer p) |
401 | is_symbol (pointer p) |
416 | { |
402 | { |
417 | return type (p) == T_SYMBOL; |
403 | return type (p) == T_SYMBOL; |
418 | } |
404 | } |
419 | |
405 | |
420 | INTERFACE INLINE char * |
406 | INTERFACE char * |
421 | symname (pointer p) |
407 | symname (pointer p) |
422 | { |
408 | { |
423 | return strvalue (car (p)); |
409 | return strvalue (p); |
424 | } |
410 | } |
425 | |
411 | |
426 | #if USE_PLIST |
412 | #if USE_PLIST |
|
|
413 | #error plists are broken because symbols are no longer pairs |
|
|
414 | #define symprop(p) cdr(p) |
427 | SCHEME_EXPORT INLINE int |
415 | SCHEME_EXPORT int |
428 | hasprop (pointer p) |
416 | hasprop (pointer p) |
429 | { |
417 | { |
430 | return typeflag (p) & T_SYMBOL; |
418 | return typeflag (p) & T_SYMBOL; |
431 | } |
419 | } |
432 | |
|
|
433 | # define symprop(p) cdr(p) |
|
|
434 | #endif |
420 | #endif |
435 | |
421 | |
436 | INTERFACE INLINE int |
422 | INTERFACE int |
437 | is_syntax (pointer p) |
423 | is_syntax (pointer p) |
438 | { |
424 | { |
439 | return typeflag (p) & T_SYNTAX; |
425 | return typeflag (p) & T_SYNTAX; |
440 | } |
426 | } |
441 | |
427 | |
442 | INTERFACE INLINE int |
428 | INTERFACE int |
443 | is_proc (pointer p) |
429 | is_proc (pointer p) |
444 | { |
430 | { |
445 | return type (p) == T_PROC; |
431 | return type (p) == T_PROC; |
446 | } |
432 | } |
447 | |
433 | |
448 | INTERFACE INLINE int |
434 | INTERFACE int |
449 | is_foreign (pointer p) |
435 | is_foreign (pointer p) |
450 | { |
436 | { |
451 | return type (p) == T_FOREIGN; |
437 | return type (p) == T_FOREIGN; |
452 | } |
438 | } |
453 | |
439 | |
454 | INTERFACE INLINE char * |
440 | INTERFACE char * |
455 | syntaxname (pointer p) |
441 | syntaxname (pointer p) |
456 | { |
442 | { |
457 | return strvalue (car (p)); |
443 | return strvalue (p); |
458 | } |
444 | } |
459 | |
445 | |
460 | #define procnum(p) ivalue (p) |
446 | #define procnum(p) ivalue_unchecked (p) |
461 | static const char *procname (pointer x); |
447 | static const char *procname (pointer x); |
462 | |
448 | |
463 | INTERFACE INLINE int |
449 | INTERFACE int |
464 | is_closure (pointer p) |
450 | is_closure (pointer p) |
465 | { |
451 | { |
466 | return type (p) == T_CLOSURE; |
452 | return type (p) == T_CLOSURE; |
467 | } |
453 | } |
468 | |
454 | |
469 | INTERFACE INLINE int |
455 | INTERFACE int |
470 | is_macro (pointer p) |
456 | is_macro (pointer p) |
471 | { |
457 | { |
472 | return type (p) == T_MACRO; |
458 | return type (p) == T_MACRO; |
473 | } |
459 | } |
474 | |
460 | |
475 | INTERFACE INLINE pointer |
461 | INTERFACE pointer |
476 | closure_code (pointer p) |
462 | closure_code (pointer p) |
477 | { |
463 | { |
478 | return car (p); |
464 | return car (p); |
479 | } |
465 | } |
480 | |
466 | |
481 | INTERFACE INLINE pointer |
467 | INTERFACE pointer |
482 | closure_env (pointer p) |
468 | closure_env (pointer p) |
483 | { |
469 | { |
484 | return cdr (p); |
470 | return cdr (p); |
485 | } |
471 | } |
486 | |
472 | |
487 | INTERFACE INLINE int |
473 | INTERFACE int |
488 | is_continuation (pointer p) |
474 | is_continuation (pointer p) |
489 | { |
475 | { |
490 | return type (p) == T_CONTINUATION; |
476 | return type (p) == T_CONTINUATION; |
491 | } |
477 | } |
492 | |
478 | |
493 | #define cont_dump(p) cdr (p) |
479 | #define cont_dump(p) cdr (p) |
494 | #define set_cont_dump(p,v) set_cdr ((p), (v)) |
480 | #define set_cont_dump(p,v) set_cdr ((p), (v)) |
495 | |
481 | |
496 | /* To do: promise should be forced ONCE only */ |
482 | /* To do: promise should be forced ONCE only */ |
497 | INTERFACE INLINE int |
483 | INTERFACE int |
498 | is_promise (pointer p) |
484 | is_promise (pointer p) |
499 | { |
485 | { |
500 | return type (p) == T_PROMISE; |
486 | return type (p) == T_PROMISE; |
501 | } |
487 | } |
502 | |
488 | |
503 | INTERFACE INLINE int |
489 | INTERFACE int |
504 | is_environment (pointer p) |
490 | is_environment (pointer p) |
505 | { |
491 | { |
506 | return type (p) == T_ENVIRONMENT; |
492 | return type (p) == T_ENVIRONMENT; |
507 | } |
493 | } |
508 | |
494 | |
… | |
… | |
514 | |
500 | |
515 | #define is_mark(p) (typeflag (p) & T_MARK) |
501 | #define is_mark(p) (typeflag (p) & T_MARK) |
516 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
502 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
517 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
503 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
518 | |
504 | |
519 | INTERFACE INLINE int |
505 | INTERFACE int |
520 | is_immutable (pointer p) |
506 | is_immutable (pointer p) |
521 | { |
507 | { |
522 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
508 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
523 | } |
509 | } |
524 | |
510 | |
525 | INTERFACE INLINE void |
511 | INTERFACE void |
526 | setimmutable (pointer p) |
512 | setimmutable (pointer p) |
527 | { |
513 | { |
528 | #if USE_ERROR_CHECKING |
514 | #if USE_ERROR_CHECKING |
529 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
515 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
530 | #endif |
516 | #endif |
531 | } |
517 | } |
532 | |
518 | |
|
|
519 | /* Result is: |
|
|
520 | proper list: length |
|
|
521 | circular list: -1 |
|
|
522 | not even a pair: -2 |
|
|
523 | dotted list: -2 minus length before dot |
|
|
524 | */ |
|
|
525 | INTERFACE int |
|
|
526 | list_length (SCHEME_P_ pointer a) |
|
|
527 | { |
|
|
528 | int i = 0; |
|
|
529 | pointer slow, fast; |
|
|
530 | |
|
|
531 | slow = fast = a; |
|
|
532 | |
|
|
533 | while (1) |
|
|
534 | { |
|
|
535 | if (fast == NIL) |
|
|
536 | return i; |
|
|
537 | |
|
|
538 | if (!is_pair (fast)) |
|
|
539 | return -2 - i; |
|
|
540 | |
|
|
541 | fast = cdr (fast); |
|
|
542 | ++i; |
|
|
543 | |
|
|
544 | if (fast == NIL) |
|
|
545 | return i; |
|
|
546 | |
|
|
547 | if (!is_pair (fast)) |
|
|
548 | return -2 - i; |
|
|
549 | |
|
|
550 | ++i; |
|
|
551 | fast = cdr (fast); |
|
|
552 | |
|
|
553 | /* Safe because we would have already returned if `fast' |
|
|
554 | encountered a non-pair. */ |
|
|
555 | slow = cdr (slow); |
|
|
556 | |
|
|
557 | if (fast == slow) |
|
|
558 | { |
|
|
559 | /* the fast pointer has looped back around and caught up |
|
|
560 | with the slow pointer, hence the structure is circular, |
|
|
561 | not of finite length, and therefore not a list */ |
|
|
562 | return -1; |
|
|
563 | } |
|
|
564 | } |
|
|
565 | } |
|
|
566 | |
|
|
567 | INTERFACE int |
|
|
568 | is_list (SCHEME_P_ pointer a) |
|
|
569 | { |
|
|
570 | return list_length (SCHEME_A_ a) >= 0; |
|
|
571 | } |
|
|
572 | |
533 | #if USE_CHAR_CLASSIFIERS |
573 | #if USE_CHAR_CLASSIFIERS |
534 | static INLINE int |
574 | ecb_inline int |
535 | Cisalpha (int c) |
575 | Cisalpha (int c) |
536 | { |
576 | { |
537 | return isascii (c) && isalpha (c); |
577 | return isascii (c) && isalpha (c); |
538 | } |
578 | } |
539 | |
579 | |
540 | static INLINE int |
580 | ecb_inline int |
541 | Cisdigit (int c) |
581 | Cisdigit (int c) |
542 | { |
582 | { |
543 | return isascii (c) && isdigit (c); |
583 | return isascii (c) && isdigit (c); |
544 | } |
584 | } |
545 | |
585 | |
546 | static INLINE int |
586 | ecb_inline int |
547 | Cisspace (int c) |
587 | Cisspace (int c) |
548 | { |
588 | { |
549 | return isascii (c) && isspace (c); |
589 | return isascii (c) && isspace (c); |
550 | } |
590 | } |
551 | |
591 | |
552 | static INLINE int |
592 | ecb_inline int |
553 | Cisupper (int c) |
593 | Cisupper (int c) |
554 | { |
594 | { |
555 | return isascii (c) && isupper (c); |
595 | return isascii (c) && isupper (c); |
556 | } |
596 | } |
557 | |
597 | |
558 | static INLINE int |
598 | ecb_inline int |
559 | Cislower (int c) |
599 | Cislower (int c) |
560 | { |
600 | { |
561 | return isascii (c) && islower (c); |
601 | return isascii (c) && islower (c); |
562 | } |
602 | } |
563 | #endif |
603 | #endif |
… | |
… | |
624 | #endif |
664 | #endif |
625 | |
665 | |
626 | static int file_push (SCHEME_P_ const char *fname); |
666 | static int file_push (SCHEME_P_ const char *fname); |
627 | static void file_pop (SCHEME_P); |
667 | static void file_pop (SCHEME_P); |
628 | static int file_interactive (SCHEME_P); |
668 | static int file_interactive (SCHEME_P); |
629 | static INLINE int is_one_of (char *s, int c); |
669 | ecb_inline int is_one_of (const char *s, int c); |
630 | static int alloc_cellseg (SCHEME_P_ int n); |
670 | static int alloc_cellseg (SCHEME_P); |
631 | static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); |
671 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
632 | static void finalize_cell (SCHEME_P_ pointer a); |
672 | static void finalize_cell (SCHEME_P_ pointer a); |
633 | static int count_consecutive_cells (pointer x, int needed); |
673 | static int count_consecutive_cells (pointer x, int needed); |
634 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
674 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
635 | static pointer mk_number (SCHEME_P_ const num n); |
675 | static pointer mk_number (SCHEME_P_ const num n); |
636 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
676 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
… | |
… | |
651 | static void mark (pointer a); |
691 | static void mark (pointer a); |
652 | static void gc (SCHEME_P_ pointer a, pointer b); |
692 | static void gc (SCHEME_P_ pointer a, pointer b); |
653 | static int basic_inchar (port *pt); |
693 | static int basic_inchar (port *pt); |
654 | static int inchar (SCHEME_P); |
694 | static int inchar (SCHEME_P); |
655 | static void backchar (SCHEME_P_ int c); |
695 | static void backchar (SCHEME_P_ int c); |
656 | static char *readstr_upto (SCHEME_P_ char *delim); |
696 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
657 | static pointer readstrexp (SCHEME_P); |
697 | static pointer readstrexp (SCHEME_P_ char delim); |
658 | static INLINE int skipspace (SCHEME_P); |
698 | ecb_inline int skipspace (SCHEME_P); |
659 | static int token (SCHEME_P); |
699 | static int token (SCHEME_P); |
660 | static void printslashstring (SCHEME_P_ char *s, int len); |
700 | static void printslashstring (SCHEME_P_ char *s, int len); |
661 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
701 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
662 | static void printatom (SCHEME_P_ pointer l, int f); |
702 | static void printatom (SCHEME_P_ pointer l, int f); |
663 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
703 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
… | |
… | |
679 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
719 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
680 | static void assign_syntax (SCHEME_P_ const char *name); |
720 | static void assign_syntax (SCHEME_P_ const char *name); |
681 | static int syntaxnum (pointer p); |
721 | static int syntaxnum (pointer p); |
682 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
722 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
683 | |
723 | |
|
|
724 | static IVALUE |
|
|
725 | ivalue (pointer x) |
|
|
726 | { |
|
|
727 | return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x); |
|
|
728 | } |
|
|
729 | |
|
|
730 | static RVALUE |
|
|
731 | rvalue (pointer x) |
|
|
732 | { |
|
|
733 | return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x); |
|
|
734 | } |
|
|
735 | |
|
|
736 | INTERFACE num |
|
|
737 | nvalue (pointer x) |
|
|
738 | { |
|
|
739 | num n; |
|
|
740 | |
|
|
741 | num_set_fixnum (n, is_integer (x)); |
|
|
742 | |
|
|
743 | if (num_is_fixnum (n)) |
|
|
744 | num_set_ivalue (n, ivalue_unchecked (x)); |
|
|
745 | else |
|
|
746 | num_set_rvalue (n, rvalue_unchecked (x)); |
|
|
747 | |
|
|
748 | return n; |
|
|
749 | } |
|
|
750 | |
684 | static num |
751 | static num |
685 | num_op (enum num_op op, num a, num b) |
752 | num_op (enum num_op op, num a, num b) |
686 | { |
753 | { |
687 | num ret; |
754 | num ret; |
688 | |
755 | |
689 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
756 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
690 | |
757 | |
691 | if (num_is_fixnum (ret)) |
758 | if (num_is_fixnum (ret)) |
692 | { |
759 | { |
693 | IVALUE av = num_get_ivalue (a); |
|
|
694 | IVALUE bv = num_get_ivalue (b); |
|
|
695 | |
|
|
696 | switch (op) |
760 | switch (op) |
697 | { |
761 | { |
698 | case NUM_ADD: av += bv; break; |
762 | case NUM_ADD: a.ivalue += b.ivalue; break; |
699 | case NUM_SUB: av -= bv; break; |
763 | case NUM_SUB: a.ivalue -= b.ivalue; break; |
700 | case NUM_MUL: av *= bv; break; |
764 | case NUM_MUL: a.ivalue *= b.ivalue; break; |
701 | case NUM_INTDIV: av /= bv; break; |
765 | case NUM_INTDIV: a.ivalue /= b.ivalue; break; |
702 | } |
766 | } |
703 | |
767 | |
704 | num_set_ivalue (ret, av); |
768 | num_set_ivalue (ret, a.ivalue); |
705 | } |
769 | } |
|
|
770 | #if USE_REAL |
706 | else |
771 | else |
707 | { |
772 | { |
708 | RVALUE av = num_get_rvalue (a); |
|
|
709 | RVALUE bv = num_get_rvalue (b); |
|
|
710 | |
|
|
711 | switch (op) |
773 | switch (op) |
712 | { |
774 | { |
713 | case NUM_ADD: av += bv; break; |
775 | case NUM_ADD: a.rvalue += b.rvalue; break; |
714 | case NUM_SUB: av -= bv; break; |
776 | case NUM_SUB: a.rvalue -= b.rvalue; break; |
715 | case NUM_MUL: av *= bv; break; |
777 | case NUM_MUL: a.rvalue *= b.rvalue; break; |
716 | case NUM_INTDIV: av /= bv; break; |
778 | case NUM_INTDIV: a.rvalue /= b.rvalue; break; |
717 | } |
779 | } |
718 | |
780 | |
719 | num_set_rvalue (ret, av); |
781 | num_set_rvalue (ret, a.rvalue); |
720 | } |
782 | } |
|
|
783 | #endif |
721 | |
784 | |
722 | return ret; |
785 | return ret; |
723 | } |
786 | } |
724 | |
787 | |
725 | static num |
788 | static num |
726 | num_div (num a, num b) |
789 | num_div (num a, num b) |
727 | { |
790 | { |
728 | num ret; |
791 | num ret; |
729 | |
792 | |
730 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); |
793 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0); |
731 | |
794 | |
732 | if (num_is_fixnum (ret)) |
795 | if (num_is_fixnum (ret)) |
733 | num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); |
796 | num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b)); |
734 | else |
797 | else |
735 | num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); |
798 | num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); |
736 | |
799 | |
737 | return ret; |
800 | return ret; |
738 | } |
801 | } |
739 | |
802 | |
740 | static num |
803 | static num |
… | |
… | |
742 | { |
805 | { |
743 | num ret; |
806 | num ret; |
744 | long e1, e2, res; |
807 | long e1, e2, res; |
745 | |
808 | |
746 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
809 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
747 | e1 = num_get_ivalue (a); |
810 | e1 = num_ivalue (a); |
748 | e2 = num_get_ivalue (b); |
811 | e2 = num_ivalue (b); |
749 | res = e1 % e2; |
812 | res = e1 % e2; |
750 | |
813 | |
751 | /* remainder should have same sign as second operand */ |
814 | /* remainder should have same sign as second operand */ |
752 | if (res > 0) |
815 | if (res > 0) |
753 | { |
816 | { |
… | |
… | |
769 | { |
832 | { |
770 | num ret; |
833 | num ret; |
771 | long e1, e2, res; |
834 | long e1, e2, res; |
772 | |
835 | |
773 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
836 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
774 | e1 = num_get_ivalue (a); |
837 | e1 = num_ivalue (a); |
775 | e2 = num_get_ivalue (b); |
838 | e2 = num_ivalue (b); |
776 | res = e1 % e2; |
839 | res = e1 % e2; |
777 | |
840 | |
778 | /* modulo should have same sign as second operand */ |
841 | /* modulo should have same sign as second operand */ |
779 | if (res * e2 < 0) |
842 | if (res * e2 < 0) |
780 | res += e2; |
843 | res += e2; |
781 | |
844 | |
782 | num_set_ivalue (ret, res); |
845 | num_set_ivalue (ret, res); |
783 | return ret; |
846 | return ret; |
784 | } |
847 | } |
785 | |
848 | |
786 | /* this completely disrespects NaNs */ |
849 | /* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */ |
787 | static int |
850 | static int |
788 | num_cmp (num a, num b) |
851 | num_cmp (num a, num b) |
789 | { |
852 | { |
790 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
853 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
791 | int ret; |
854 | int ret; |
792 | |
855 | |
793 | if (is_fixnum) |
856 | if (is_fixnum) |
794 | { |
857 | { |
795 | IVALUE av = num_get_ivalue (a); |
858 | IVALUE av = num_ivalue (a); |
796 | IVALUE bv = num_get_ivalue (b); |
859 | IVALUE bv = num_ivalue (b); |
797 | |
860 | |
798 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
861 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
799 | } |
862 | } |
800 | else |
863 | else |
801 | { |
864 | { |
802 | RVALUE av = num_get_rvalue (a); |
865 | RVALUE av = num_rvalue (a); |
803 | RVALUE bv = num_get_rvalue (b); |
866 | RVALUE bv = num_rvalue (b); |
804 | |
867 | |
805 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
868 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
806 | } |
869 | } |
807 | |
870 | |
808 | return ret; |
871 | return ret; |
809 | } |
872 | } |
810 | |
|
|
811 | #if USE_MATH |
|
|
812 | |
|
|
813 | /* Round to nearest. Round to even if midway */ |
|
|
814 | static double |
|
|
815 | round_per_R5RS (double x) |
|
|
816 | { |
|
|
817 | double fl = floor (x); |
|
|
818 | double ce = ceil (x); |
|
|
819 | double dfl = x - fl; |
|
|
820 | double dce = ce - x; |
|
|
821 | |
|
|
822 | if (dfl > dce) |
|
|
823 | return ce; |
|
|
824 | else if (dfl < dce) |
|
|
825 | return fl; |
|
|
826 | else |
|
|
827 | { |
|
|
828 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
|
|
829 | return fl; |
|
|
830 | else |
|
|
831 | return ce; |
|
|
832 | } |
|
|
833 | } |
|
|
834 | #endif |
|
|
835 | |
873 | |
836 | static int |
874 | static int |
837 | is_zero_rvalue (RVALUE x) |
875 | is_zero_rvalue (RVALUE x) |
838 | { |
876 | { |
|
|
877 | return x == 0; |
|
|
878 | #if 0 |
839 | #if USE_REAL |
879 | #if USE_REAL |
840 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
880 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
841 | #else |
881 | #else |
842 | return x == 0; |
882 | return x == 0; |
843 | #endif |
883 | #endif |
|
|
884 | #endif |
844 | } |
885 | } |
845 | |
886 | |
846 | /* allocate new cell segment */ |
887 | /* allocate new cell segment */ |
847 | static int |
888 | static int |
848 | alloc_cellseg (SCHEME_P_ int n) |
889 | alloc_cellseg (SCHEME_P) |
849 | { |
890 | { |
850 | pointer newp; |
891 | struct cell *newp; |
851 | pointer last; |
892 | struct cell *last; |
852 | pointer p; |
893 | struct cell *p; |
853 | char *cp; |
894 | char *cp; |
854 | long i; |
895 | long i; |
855 | int k; |
896 | int k; |
856 | |
897 | |
857 | static int segsize = CELL_SEGSIZE >> 1; |
898 | static int segsize = CELL_SEGSIZE >> 1; |
858 | segsize <<= 1; |
899 | segsize <<= 1; |
859 | |
900 | |
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)); |
901 | cp = malloc (segsize * sizeof (struct cell)); |
866 | |
902 | |
867 | if (!cp && USE_ERROR_CHECKING) |
903 | if (!cp && USE_ERROR_CHECKING) |
868 | return k; |
904 | return k; |
869 | |
905 | |
870 | i = ++SCHEME_V->last_cell_seg; |
906 | i = ++SCHEME_V->last_cell_seg; |
871 | SCHEME_V->alloc_seg[i] = cp; |
907 | SCHEME_V->alloc_seg[i] = cp; |
872 | |
908 | |
873 | /* insert new segment in address order */ |
909 | newp = (struct cell *)cp; |
874 | newp = (pointer)cp; |
|
|
875 | SCHEME_V->cell_seg[i] = newp; |
910 | SCHEME_V->cell_seg[i] = newp; |
876 | SCHEME_V->cell_segsize[i] = segsize; |
911 | 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; |
912 | SCHEME_V->fcells += segsize; |
893 | last = newp + segsize - 1; |
913 | last = newp + segsize - 1; |
894 | |
914 | |
895 | for (p = newp; p <= last; p++) |
915 | for (p = newp; p <= last; p++) |
896 | { |
916 | { |
|
|
917 | pointer cp = POINTER (p); |
897 | set_typeflag (p, T_FREE); |
918 | set_typeflag (cp, T_PAIR); |
898 | set_car (p, NIL); |
919 | set_car (cp, NIL); |
899 | set_cdr (p, p + 1); |
920 | set_cdr (cp, POINTER (p + 1)); |
900 | } |
921 | } |
901 | |
922 | |
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); |
923 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
906 | SCHEME_V->free_cell = newp; |
924 | SCHEME_V->free_cell = POINTER (newp); |
907 | } |
|
|
908 | else |
|
|
909 | { |
|
|
910 | p = SCHEME_V->free_cell; |
|
|
911 | |
925 | |
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; |
926 | return 1; |
921 | } |
927 | } |
922 | |
928 | |
923 | /* get new cell. parameter a, b is marked by gc. */ |
929 | /* get new cell. parameter a, b is marked by gc. */ |
924 | static INLINE pointer |
930 | ecb_inline pointer |
925 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
931 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
926 | { |
932 | { |
927 | if (ecb_expect_false (SCHEME_V->free_cell == NIL)) |
933 | if (ecb_expect_false (SCHEME_V->free_cell == NIL)) |
928 | { |
934 | { |
929 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
935 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
930 | return S_SINK; |
936 | return S_SINK; |
931 | |
937 | |
932 | if (SCHEME_V->free_cell == NIL) |
938 | if (SCHEME_V->free_cell == NIL) |
933 | { |
939 | { |
934 | const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; |
940 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1; |
935 | |
941 | |
936 | gc (SCHEME_A_ a, b); |
942 | gc (SCHEME_A_ a, b); |
937 | |
943 | |
938 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
944 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
939 | { |
945 | { |
940 | /* if only a few recovered, get more to avoid fruitless gc's */ |
946 | /* if only a few recovered, get more to avoid fruitless gc's */ |
941 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
947 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
942 | { |
948 | { |
943 | #if USE_ERROR_CHECKING |
949 | #if USE_ERROR_CHECKING |
944 | SCHEME_V->no_memory = 1; |
950 | SCHEME_V->no_memory = 1; |
945 | return S_SINK; |
951 | return S_SINK; |
946 | #endif |
952 | #endif |
… | |
… | |
958 | } |
964 | } |
959 | } |
965 | } |
960 | |
966 | |
961 | /* To retain recent allocs before interpreter knows about them - |
967 | /* To retain recent allocs before interpreter knows about them - |
962 | Tehom */ |
968 | Tehom */ |
963 | |
|
|
964 | static void |
969 | static void |
965 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
970 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
966 | { |
971 | { |
967 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
972 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
968 | |
973 | |
… | |
… | |
990 | } |
995 | } |
991 | |
996 | |
992 | static pointer |
997 | static pointer |
993 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
998 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
994 | { |
999 | { |
995 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1000 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
996 | pointer *e = malloc (len * sizeof (pointer)); |
1001 | pointer *e = malloc (len * sizeof (pointer)); |
997 | |
1002 | |
998 | if (!e && USE_ERROR_CHECKING) |
1003 | if (!e && USE_ERROR_CHECKING) |
999 | return S_SINK; |
1004 | return S_SINK; |
1000 | |
1005 | |
1001 | /* Record it as a vector so that gc understands it. */ |
1006 | /* Record it as a vector so that gc understands it. */ |
1002 | set_typeflag (v, T_VECTOR | T_ATOM); |
1007 | set_typeflag (v, T_VECTOR | T_ATOM); |
1003 | |
1008 | |
1004 | v->object.vector.vvalue = e; |
1009 | CELL(v)->object.vector.vvalue = e; |
1005 | v->object.vector.length = len; |
1010 | CELL(v)->object.vector.length = len; |
1006 | fill_vector (v, init); |
1011 | fill_vector (v, 0, init); |
1007 | push_recent_alloc (SCHEME_A_ v, NIL); |
1012 | push_recent_alloc (SCHEME_A_ v, NIL); |
1008 | |
1013 | |
1009 | return v; |
1014 | return v; |
1010 | } |
1015 | } |
1011 | |
1016 | |
1012 | static INLINE void |
1017 | ecb_inline void |
1013 | ok_to_freely_gc (SCHEME_P) |
1018 | ok_to_freely_gc (SCHEME_P) |
1014 | { |
1019 | { |
1015 | set_car (S_SINK, NIL); |
1020 | set_car (S_SINK, NIL); |
1016 | } |
1021 | } |
1017 | |
1022 | |
… | |
… | |
1019 | static void |
1024 | static void |
1020 | check_cell_alloced (pointer p, int expect_alloced) |
1025 | check_cell_alloced (pointer p, int expect_alloced) |
1021 | { |
1026 | { |
1022 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1027 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1023 | if (typeflag (p) & !expect_alloced) |
1028 | if (typeflag (p) & !expect_alloced) |
1024 | xwrstr ("Cell is already allocated!\n"); |
1029 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1025 | |
1030 | |
1026 | if (!(typeflag (p)) & expect_alloced) |
1031 | if (!(typeflag (p)) & expect_alloced) |
1027 | xwrstr ("Cell is not allocated!\n"); |
1032 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1028 | } |
1033 | } |
1029 | |
1034 | |
1030 | static void |
1035 | static void |
1031 | check_range_alloced (pointer p, int n, int expect_alloced) |
1036 | check_range_alloced (pointer p, int n, int expect_alloced) |
1032 | { |
1037 | { |
… | |
… | |
1054 | set_cdr (x, b); |
1059 | set_cdr (x, b); |
1055 | |
1060 | |
1056 | return x; |
1061 | return x; |
1057 | } |
1062 | } |
1058 | |
1063 | |
|
|
1064 | static pointer |
|
|
1065 | generate_symbol (SCHEME_P_ const char *name) |
|
|
1066 | { |
|
|
1067 | pointer x = mk_string (SCHEME_A_ name); |
|
|
1068 | setimmutable (x); |
|
|
1069 | set_typeflag (x, T_SYMBOL | T_ATOM); |
|
|
1070 | return x; |
|
|
1071 | } |
|
|
1072 | |
1059 | /* ========== oblist implementation ========== */ |
1073 | /* ========== oblist implementation ========== */ |
1060 | |
1074 | |
1061 | #ifndef USE_OBJECT_LIST |
1075 | #ifndef USE_OBJECT_LIST |
1062 | |
1076 | |
|
|
1077 | static int |
1063 | static int hash_fn (const char *key, int table_size); |
1078 | hash_fn (const char *key, int table_size) |
|
|
1079 | { |
|
|
1080 | const unsigned char *p = key; |
|
|
1081 | uint32_t hash = 2166136261; |
|
|
1082 | |
|
|
1083 | while (*p) |
|
|
1084 | hash = (hash ^ *p++) * 16777619; |
|
|
1085 | |
|
|
1086 | return hash % table_size; |
|
|
1087 | } |
1064 | |
1088 | |
1065 | static pointer |
1089 | static pointer |
1066 | oblist_initial_value (SCHEME_P) |
1090 | oblist_initial_value (SCHEME_P) |
1067 | { |
1091 | { |
1068 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1092 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
… | |
… | |
1070 | |
1094 | |
1071 | /* returns the new symbol */ |
1095 | /* returns the new symbol */ |
1072 | static pointer |
1096 | static pointer |
1073 | oblist_add_by_name (SCHEME_P_ const char *name) |
1097 | oblist_add_by_name (SCHEME_P_ const char *name) |
1074 | { |
1098 | { |
1075 | int location; |
1099 | 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)); |
1100 | 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))); |
1101 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1083 | return x; |
1102 | return x; |
1084 | } |
1103 | } |
1085 | |
1104 | |
1086 | static INLINE pointer |
1105 | ecb_inline pointer |
1087 | oblist_find_by_name (SCHEME_P_ const char *name) |
1106 | oblist_find_by_name (SCHEME_P_ const char *name) |
1088 | { |
1107 | { |
1089 | int location; |
1108 | int location; |
1090 | pointer x; |
1109 | pointer x; |
1091 | char *s; |
1110 | char *s; |
1092 | |
1111 | |
1093 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1112 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1094 | |
1113 | |
1095 | for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1114 | for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1096 | { |
1115 | { |
1097 | s = symname (car (x)); |
1116 | s = symname (car (x)); |
1098 | |
1117 | |
1099 | /* case-insensitive, per R5RS section 2 */ |
1118 | /* case-insensitive, per R5RS section 2 */ |
1100 | if (stricmp (name, s) == 0) |
1119 | if (stricmp (name, s) == 0) |
… | |
… | |
1110 | int i; |
1129 | int i; |
1111 | pointer x; |
1130 | pointer x; |
1112 | pointer ob_list = NIL; |
1131 | pointer ob_list = NIL; |
1113 | |
1132 | |
1114 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1133 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1115 | for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1134 | for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1116 | ob_list = cons (x, ob_list); |
1135 | ob_list = cons (x, ob_list); |
1117 | |
1136 | |
1118 | return ob_list; |
1137 | return ob_list; |
1119 | } |
1138 | } |
1120 | |
1139 | |
… | |
… | |
1124 | oblist_initial_value (SCHEME_P) |
1143 | oblist_initial_value (SCHEME_P) |
1125 | { |
1144 | { |
1126 | return NIL; |
1145 | return NIL; |
1127 | } |
1146 | } |
1128 | |
1147 | |
1129 | static INLINE pointer |
1148 | ecb_inline pointer |
1130 | oblist_find_by_name (SCHEME_P_ const char *name) |
1149 | oblist_find_by_name (SCHEME_P_ const char *name) |
1131 | { |
1150 | { |
1132 | pointer x; |
1151 | pointer x; |
1133 | char *s; |
1152 | char *s; |
1134 | |
1153 | |
… | |
… | |
1146 | |
1165 | |
1147 | /* returns the new symbol */ |
1166 | /* returns the new symbol */ |
1148 | static pointer |
1167 | static pointer |
1149 | oblist_add_by_name (SCHEME_P_ const char *name) |
1168 | oblist_add_by_name (SCHEME_P_ const char *name) |
1150 | { |
1169 | { |
1151 | pointer x; |
1170 | 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); |
1171 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1157 | return x; |
1172 | return x; |
1158 | } |
1173 | } |
1159 | |
1174 | |
1160 | static pointer |
1175 | static pointer |
… | |
… | |
1170 | mk_port (SCHEME_P_ port *p) |
1185 | mk_port (SCHEME_P_ port *p) |
1171 | { |
1186 | { |
1172 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1187 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1173 | |
1188 | |
1174 | set_typeflag (x, T_PORT | T_ATOM); |
1189 | set_typeflag (x, T_PORT | T_ATOM); |
1175 | x->object.port = p; |
1190 | set_port (x, p); |
1176 | |
1191 | |
1177 | return x; |
1192 | return x; |
1178 | } |
1193 | } |
1179 | #endif |
1194 | #endif |
1180 | |
1195 | |
1181 | pointer |
1196 | pointer |
1182 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1197 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1183 | { |
1198 | { |
1184 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1199 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1185 | |
1200 | |
1186 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1201 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1187 | x->object.ff = f; |
1202 | CELL(x)->object.ff = f; |
1188 | |
1203 | |
1189 | return x; |
1204 | return x; |
1190 | } |
1205 | } |
1191 | |
1206 | |
1192 | INTERFACE pointer |
1207 | INTERFACE pointer |
1193 | mk_character (SCHEME_P_ int c) |
1208 | mk_character (SCHEME_P_ int c) |
1194 | { |
1209 | { |
1195 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1210 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1196 | |
1211 | |
1197 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1212 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1198 | ivalue_unchecked (x) = c & 0xff; |
1213 | set_ivalue (x, c & 0xff); |
1199 | set_num_integer (x); |
1214 | |
1200 | return x; |
1215 | return x; |
1201 | } |
1216 | } |
1202 | |
1217 | |
1203 | /* get number atom (integer) */ |
1218 | /* get number atom (integer) */ |
1204 | INTERFACE pointer |
1219 | INTERFACE pointer |
1205 | mk_integer (SCHEME_P_ long num) |
1220 | mk_integer (SCHEME_P_ long n) |
1206 | { |
1221 | { |
|
|
1222 | pointer p = 0; |
|
|
1223 | pointer *pp = &p; |
|
|
1224 | |
|
|
1225 | #if USE_INTCACHE |
|
|
1226 | if (n >= INTCACHE_MIN && n <= INTCACHE_MAX) |
|
|
1227 | pp = &SCHEME_V->intcache[n - INTCACHE_MIN]; |
|
|
1228 | #endif |
|
|
1229 | |
|
|
1230 | if (!*pp) |
|
|
1231 | { |
1207 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1232 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1208 | |
1233 | |
1209 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1234 | set_typeflag (x, T_INTEGER | T_ATOM); |
1210 | ivalue_unchecked (x) = num; |
1235 | setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */ |
1211 | set_num_integer (x); |
1236 | set_ivalue (x, n); |
|
|
1237 | |
|
|
1238 | *pp = x; |
|
|
1239 | } |
|
|
1240 | |
1212 | return x; |
1241 | return *pp; |
1213 | } |
1242 | } |
1214 | |
1243 | |
1215 | INTERFACE pointer |
1244 | INTERFACE pointer |
1216 | mk_real (SCHEME_P_ RVALUE n) |
1245 | mk_real (SCHEME_P_ RVALUE n) |
1217 | { |
1246 | { |
|
|
1247 | #if USE_REAL |
1218 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1248 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1219 | |
1249 | |
1220 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1250 | set_typeflag (x, T_REAL | T_ATOM); |
1221 | rvalue_unchecked (x) = n; |
1251 | set_rvalue (x, n); |
1222 | set_num_real (x); |
1252 | |
1223 | return x; |
1253 | return x; |
|
|
1254 | #else |
|
|
1255 | return mk_integer (SCHEME_A_ n); |
|
|
1256 | #endif |
1224 | } |
1257 | } |
1225 | |
1258 | |
1226 | static pointer |
1259 | static pointer |
1227 | mk_number (SCHEME_P_ const num n) |
1260 | mk_number (SCHEME_P_ const num n) |
1228 | { |
1261 | { |
|
|
1262 | #if USE_REAL |
1229 | if (num_is_fixnum (n)) |
1263 | return num_is_fixnum (n) |
|
|
1264 | ? mk_integer (SCHEME_A_ num_ivalue (n)) |
|
|
1265 | : mk_real (SCHEME_A_ num_rvalue (n)); |
|
|
1266 | #else |
1230 | return mk_integer (SCHEME_A_ num_get_ivalue (n)); |
1267 | return mk_integer (SCHEME_A_ num_ivalue (n)); |
1231 | else |
1268 | #endif |
1232 | return mk_real (SCHEME_A_ num_get_rvalue (n)); |
|
|
1233 | } |
1269 | } |
1234 | |
1270 | |
1235 | /* allocate name to string area */ |
1271 | /* allocate name to string area */ |
1236 | static char * |
1272 | static char * |
1237 | store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) |
1273 | store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) |
… | |
… | |
1243 | SCHEME_V->no_memory = 1; |
1279 | SCHEME_V->no_memory = 1; |
1244 | return SCHEME_V->strbuff; |
1280 | return SCHEME_V->strbuff; |
1245 | } |
1281 | } |
1246 | |
1282 | |
1247 | if (str) |
1283 | if (str) |
1248 | { |
1284 | 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 |
1285 | else |
1258 | { |
|
|
1259 | memset (q, fill, len_str); |
1286 | memset (q, fill, len_str); |
|
|
1287 | |
1260 | q[len_str] = 0; |
1288 | q[len_str] = 0; |
1261 | } |
|
|
1262 | |
1289 | |
1263 | return q; |
1290 | return q; |
1264 | } |
1291 | } |
1265 | |
1292 | |
1266 | INTERFACE pointer |
1293 | INTERFACE pointer |
… | |
… | |
1280 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1307 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1281 | |
1308 | |
1282 | set_typeflag (x, T_STRING | T_ATOM); |
1309 | set_typeflag (x, T_STRING | T_ATOM); |
1283 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1310 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1284 | strlength (x) = len; |
1311 | strlength (x) = len; |
|
|
1312 | |
1285 | return x; |
1313 | return x; |
1286 | } |
1314 | } |
1287 | |
1315 | |
1288 | INTERFACE pointer |
1316 | INTERFACE pointer |
1289 | mk_string (SCHEME_P_ const char *str) |
1317 | mk_string (SCHEME_P_ const char *str) |
… | |
… | |
1296 | { |
1324 | { |
1297 | return get_vector_object (SCHEME_A_ len, NIL); |
1325 | return get_vector_object (SCHEME_A_ len, NIL); |
1298 | } |
1326 | } |
1299 | |
1327 | |
1300 | INTERFACE void |
1328 | INTERFACE void |
1301 | fill_vector (pointer vec, pointer obj) |
1329 | fill_vector (pointer vec, uint32_t start, pointer obj) |
1302 | { |
1330 | { |
1303 | int i; |
1331 | int i; |
1304 | |
1332 | |
1305 | for (i = 0; i < vec->object.vector.length; i++) |
1333 | for (i = start; i < veclength (vec); i++) |
1306 | vecvalue (vec)[i] = obj; |
1334 | vecvalue (vec)[i] = obj; |
1307 | } |
1335 | } |
1308 | |
1336 | |
|
|
1337 | INTERFACE void |
|
|
1338 | vector_resize (pointer vec, uint32_t newsize, pointer fill) |
|
|
1339 | { |
|
|
1340 | uint32_t oldsize = veclength (vec); |
|
|
1341 | vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer)); |
|
|
1342 | veclength (vec) = newsize; |
|
|
1343 | fill_vector (vec, oldsize, fill); |
|
|
1344 | } |
|
|
1345 | |
1309 | INTERFACE pointer |
1346 | INTERFACE pointer |
1310 | vector_elem (pointer vec, uint32_t ielem) |
1347 | vector_get (pointer vec, uint32_t ielem) |
1311 | { |
1348 | { |
1312 | return vecvalue(vec)[ielem]; |
1349 | return vecvalue(vec)[ielem]; |
1313 | } |
1350 | } |
1314 | |
1351 | |
1315 | INTERFACE void |
1352 | INTERFACE void |
1316 | set_vector_elem (pointer vec, uint32_t ielem, pointer a) |
1353 | vector_set (pointer vec, uint32_t ielem, pointer a) |
1317 | { |
1354 | { |
1318 | vecvalue(vec)[ielem] = a; |
1355 | vecvalue(vec)[ielem] = a; |
1319 | } |
1356 | } |
1320 | |
1357 | |
1321 | /* get new symbol */ |
1358 | /* get new symbol */ |
… | |
… | |
1333 | |
1370 | |
1334 | INTERFACE pointer |
1371 | INTERFACE pointer |
1335 | gensym (SCHEME_P) |
1372 | gensym (SCHEME_P) |
1336 | { |
1373 | { |
1337 | pointer x; |
1374 | pointer x; |
1338 | |
|
|
1339 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
|
|
1340 | { |
|
|
1341 | char name[40] = "gensym-"; |
1375 | char name[40] = "gensym-"; |
1342 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1376 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1343 | |
1377 | |
1344 | /* first check oblist */ |
1378 | return generate_symbol (SCHEME_A_ name); |
1345 | x = oblist_find_by_name (SCHEME_A_ name); |
1379 | } |
1346 | |
1380 | |
1347 | if (x == NIL) |
1381 | static int |
1348 | { |
1382 | is_gensym (SCHEME_P_ pointer x) |
1349 | x = oblist_add_by_name (SCHEME_A_ name); |
1383 | { |
1350 | return x; |
1384 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1351 | } |
|
|
1352 | } |
|
|
1353 | |
|
|
1354 | return NIL; |
|
|
1355 | } |
1385 | } |
1356 | |
1386 | |
1357 | /* make symbol or number atom from string */ |
1387 | /* make symbol or number atom from string */ |
1358 | static pointer |
1388 | static pointer |
1359 | mk_atom (SCHEME_P_ char *q) |
1389 | mk_atom (SCHEME_P_ char *q) |
… | |
… | |
1413 | } |
1443 | } |
1414 | else if ((c == 'e') || (c == 'E')) |
1444 | else if ((c == 'e') || (c == 'E')) |
1415 | { |
1445 | { |
1416 | if (!has_fp_exp) |
1446 | if (!has_fp_exp) |
1417 | { |
1447 | { |
1418 | has_dec_point = 1; /* decimal point illegal |
1448 | has_dec_point = 1; /* decimal point illegal from now on */ |
1419 | from now on */ |
|
|
1420 | p++; |
1449 | p++; |
1421 | |
1450 | |
1422 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1451 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1423 | continue; |
1452 | continue; |
1424 | } |
1453 | } |
… | |
… | |
1512 | |
1541 | |
1513 | if (ecb_expect_false (is_vector (p))) |
1542 | if (ecb_expect_false (is_vector (p))) |
1514 | { |
1543 | { |
1515 | int i; |
1544 | int i; |
1516 | |
1545 | |
1517 | for (i = 0; i < p->object.vector.length; i++) |
1546 | for (i = 0; i < veclength (p); i++) |
1518 | mark (vecvalue (p)[i]); |
1547 | mark (vecvalue (p)[i]); |
1519 | } |
1548 | } |
1520 | |
1549 | |
1521 | if (is_atom (p)) |
1550 | if (is_atom (p)) |
1522 | goto E6; |
1551 | goto E6; |
… | |
… | |
1569 | |
1598 | |
1570 | /* garbage collection. parameter a, b is marked. */ |
1599 | /* garbage collection. parameter a, b is marked. */ |
1571 | static void |
1600 | static void |
1572 | gc (SCHEME_P_ pointer a, pointer b) |
1601 | gc (SCHEME_P_ pointer a, pointer b) |
1573 | { |
1602 | { |
1574 | pointer p; |
|
|
1575 | int i; |
1603 | int i; |
1576 | |
1604 | |
1577 | if (SCHEME_V->gc_verbose) |
1605 | if (SCHEME_V->gc_verbose) |
1578 | putstr (SCHEME_A_ "gc..."); |
1606 | putstr (SCHEME_A_ "gc..."); |
1579 | |
1607 | |
… | |
… | |
1595 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1623 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1596 | mark (car (S_SINK)); |
1624 | mark (car (S_SINK)); |
1597 | /* Mark any older stuff above nested C calls */ |
1625 | /* Mark any older stuff above nested C calls */ |
1598 | mark (SCHEME_V->c_nest); |
1626 | mark (SCHEME_V->c_nest); |
1599 | |
1627 | |
|
|
1628 | #if USE_INTCACHE |
|
|
1629 | /* mark intcache */ |
|
|
1630 | for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i) |
|
|
1631 | if (SCHEME_V->intcache[i - INTCACHE_MIN]) |
|
|
1632 | mark (SCHEME_V->intcache[i - INTCACHE_MIN]); |
|
|
1633 | #endif |
|
|
1634 | |
1600 | /* mark variables a, b */ |
1635 | /* mark variables a, b */ |
1601 | mark (a); |
1636 | mark (a); |
1602 | mark (b); |
1637 | mark (b); |
1603 | |
1638 | |
1604 | /* garbage collect */ |
1639 | /* garbage collect */ |
1605 | clrmark (NIL); |
1640 | clrmark (NIL); |
1606 | SCHEME_V->fcells = 0; |
1641 | SCHEME_V->fcells = 0; |
1607 | SCHEME_V->free_cell = NIL; |
1642 | SCHEME_V->free_cell = NIL; |
1608 | |
1643 | |
1609 | /* free-list is kept sorted by address so as to maintain consecutive |
1644 | if (SCHEME_V->gc_verbose) |
1610 | ranges, if possible, for use with vectors. Here we scan the cells |
1645 | putstr (SCHEME_A_ "freeing..."); |
1611 | (which are also kept sorted by address) downwards to build the |
1646 | |
1612 | free-list in sorted order. |
1647 | uint32_t total = 0; |
1613 | */ |
1648 | |
|
|
1649 | /* Here we scan the cells to build the free-list. */ |
1614 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1650 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1615 | { |
1651 | { |
1616 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
1652 | struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1653 | struct cell *p; |
|
|
1654 | total += SCHEME_V->cell_segsize [i]; |
1617 | |
1655 | |
1618 | while (--p >= SCHEME_V->cell_seg[i]) |
1656 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
1619 | { |
1657 | { |
|
|
1658 | pointer c = POINTER (p); |
|
|
1659 | |
1620 | if (is_mark (p)) |
1660 | if (is_mark (c)) |
1621 | clrmark (p); |
1661 | clrmark (c); |
1622 | else |
1662 | else |
1623 | { |
1663 | { |
1624 | /* reclaim cell */ |
1664 | /* reclaim cell */ |
1625 | if (typeflag (p) != T_FREE) |
1665 | if (typeflag (c) != T_PAIR) |
1626 | { |
1666 | { |
1627 | finalize_cell (SCHEME_A_ p); |
1667 | finalize_cell (SCHEME_A_ c); |
1628 | set_typeflag (p, T_FREE); |
1668 | set_typeflag (c, T_PAIR); |
1629 | set_car (p, NIL); |
1669 | set_car (c, NIL); |
1630 | } |
1670 | } |
1631 | |
1671 | |
1632 | ++SCHEME_V->fcells; |
1672 | ++SCHEME_V->fcells; |
1633 | set_cdr (p, SCHEME_V->free_cell); |
1673 | set_cdr (c, SCHEME_V->free_cell); |
1634 | SCHEME_V->free_cell = p; |
1674 | SCHEME_V->free_cell = c; |
1635 | } |
1675 | } |
1636 | } |
1676 | } |
1637 | } |
1677 | } |
1638 | |
1678 | |
1639 | if (SCHEME_V->gc_verbose) |
1679 | if (SCHEME_V->gc_verbose) |
1640 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
1680 | { |
|
|
1681 | 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"); |
|
|
1682 | } |
1641 | } |
1683 | } |
1642 | |
1684 | |
1643 | static void |
1685 | static void |
1644 | finalize_cell (SCHEME_P_ pointer a) |
1686 | finalize_cell (SCHEME_P_ pointer a) |
1645 | { |
1687 | { |
1646 | /* TODO, fast bitmap check? */ |
1688 | /* TODO, fast bitmap check? */ |
1647 | if (is_string (a)) |
1689 | if (is_string (a) || is_symbol (a)) |
1648 | free (strvalue (a)); |
1690 | free (strvalue (a)); |
1649 | else if (is_vector (a)) |
1691 | else if (is_vector (a)) |
1650 | free (vecvalue (a)); |
1692 | free (vecvalue (a)); |
1651 | #if USE_PORTS |
1693 | #if USE_PORTS |
1652 | else if (is_port (a)) |
1694 | else if (is_port (a)) |
1653 | { |
1695 | { |
1654 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
1696 | if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) |
1655 | port_close (SCHEME_A_ a, port_input | port_output); |
1697 | port_close (SCHEME_A_ a, port_input | port_output); |
1656 | |
1698 | |
1657 | free (a->object.port); |
1699 | free (port (a)); |
1658 | } |
1700 | } |
1659 | #endif |
1701 | #endif |
1660 | } |
1702 | } |
1661 | |
1703 | |
1662 | /* ========== Routines for Reading ========== */ |
1704 | /* ========== Routines for Reading ========== */ |
… | |
… | |
1678 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1720 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1679 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1721 | 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; |
1722 | 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; |
1723 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1682 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1724 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1683 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1725 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1684 | |
1726 | |
1685 | #if SHOW_ERROR_LINE |
1727 | #if SHOW_ERROR_LINE |
1686 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1728 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1687 | |
1729 | |
1688 | if (fname) |
1730 | if (fname) |
… | |
… | |
1705 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1747 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1706 | #if USE_PORTS |
1748 | #if USE_PORTS |
1707 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1749 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1708 | #endif |
1750 | #endif |
1709 | SCHEME_V->file_i--; |
1751 | SCHEME_V->file_i--; |
1710 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1752 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1711 | } |
1753 | } |
1712 | } |
1754 | } |
1713 | |
1755 | |
1714 | static int |
1756 | static int |
1715 | file_interactive (SCHEME_P) |
1757 | file_interactive (SCHEME_P) |
1716 | { |
1758 | { |
1717 | #if USE_PORTS |
1759 | #if USE_PORTS |
1718 | return SCHEME_V->file_i == 0 |
1760 | return SCHEME_V->file_i == 0 |
1719 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1761 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1720 | && (SCHEME_V->inport->object.port->kind & port_file); |
1762 | && (port (SCHEME_V->inport)->kind & port_file); |
1721 | #else |
1763 | #else |
1722 | return 0; |
1764 | return 0; |
1723 | #endif |
1765 | #endif |
1724 | } |
1766 | } |
1725 | |
1767 | |
… | |
… | |
1859 | } |
1901 | } |
1860 | |
1902 | |
1861 | static void |
1903 | static void |
1862 | port_close (SCHEME_P_ pointer p, int flag) |
1904 | port_close (SCHEME_P_ pointer p, int flag) |
1863 | { |
1905 | { |
1864 | port *pt = p->object.port; |
1906 | port *pt = port (p); |
1865 | |
1907 | |
1866 | pt->kind &= ~flag; |
1908 | pt->kind &= ~flag; |
1867 | |
1909 | |
1868 | if ((pt->kind & (port_input | port_output)) == 0) |
1910 | if ((pt->kind & (port_input | port_output)) == 0) |
1869 | { |
1911 | { |
… | |
… | |
1890 | /* get new character from input file */ |
1932 | /* get new character from input file */ |
1891 | static int |
1933 | static int |
1892 | inchar (SCHEME_P) |
1934 | inchar (SCHEME_P) |
1893 | { |
1935 | { |
1894 | int c; |
1936 | int c; |
1895 | port *pt; |
1937 | port *pt = port (SCHEME_V->inport); |
1896 | |
|
|
1897 | pt = SCHEME_V->inport->object.port; |
|
|
1898 | |
1938 | |
1899 | if (pt->kind & port_saw_EOF) |
1939 | if (pt->kind & port_saw_EOF) |
1900 | return EOF; |
1940 | return EOF; |
1901 | |
1941 | |
1902 | c = basic_inchar (pt); |
1942 | c = basic_inchar (pt); |
… | |
… | |
1969 | port *pt; |
2009 | port *pt; |
1970 | |
2010 | |
1971 | if (c == EOF) |
2011 | if (c == EOF) |
1972 | return; |
2012 | return; |
1973 | |
2013 | |
1974 | pt = SCHEME_V->inport->object.port; |
2014 | pt = port (SCHEME_V->inport); |
1975 | pt->unget = c; |
2015 | pt->unget = c; |
1976 | #else |
2016 | #else |
1977 | if (c == EOF) |
2017 | if (c == EOF) |
1978 | return; |
2018 | return; |
1979 | |
2019 | |
… | |
… | |
2007 | |
2047 | |
2008 | INTERFACE void |
2048 | INTERFACE void |
2009 | putstr (SCHEME_P_ const char *s) |
2049 | putstr (SCHEME_P_ const char *s) |
2010 | { |
2050 | { |
2011 | #if USE_PORTS |
2051 | #if USE_PORTS |
2012 | port *pt = SCHEME_V->outport->object.port; |
2052 | port *pt = port (SCHEME_V->outport); |
2013 | |
2053 | |
2014 | if (pt->kind & port_file) |
2054 | if (pt->kind & port_file) |
2015 | write (pt->rep.stdio.file, s, strlen (s)); |
2055 | write (pt->rep.stdio.file, s, strlen (s)); |
2016 | else |
2056 | else |
2017 | for (; *s; s++) |
2057 | for (; *s; s++) |
… | |
… | |
2019 | *pt->rep.string.curr++ = *s; |
2059 | *pt->rep.string.curr++ = *s; |
2020 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2060 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2021 | *pt->rep.string.curr++ = *s; |
2061 | *pt->rep.string.curr++ = *s; |
2022 | |
2062 | |
2023 | #else |
2063 | #else |
2024 | xwrstr (s); |
2064 | write (pt->rep.stdio.file, s, strlen (s)); |
2025 | #endif |
2065 | #endif |
2026 | } |
2066 | } |
2027 | |
2067 | |
2028 | static void |
2068 | static void |
2029 | putchars (SCHEME_P_ const char *s, int len) |
2069 | putchars (SCHEME_P_ const char *s, int len) |
2030 | { |
2070 | { |
2031 | #if USE_PORTS |
2071 | #if USE_PORTS |
2032 | port *pt = SCHEME_V->outport->object.port; |
2072 | port *pt = port (SCHEME_V->outport); |
2033 | |
2073 | |
2034 | if (pt->kind & port_file) |
2074 | if (pt->kind & port_file) |
2035 | write (pt->rep.stdio.file, s, len); |
2075 | write (pt->rep.stdio.file, s, len); |
2036 | else |
2076 | else |
2037 | { |
2077 | { |
… | |
… | |
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 |
2095 | #if USE_PORTS |
2056 | port *pt = SCHEME_V->outport->object.port; |
2096 | port *pt = port (SCHEME_V->outport); |
2057 | |
2097 | |
2058 | if (pt->kind & port_file) |
2098 | if (pt->kind & port_file) |
2059 | { |
2099 | { |
2060 | char cc = c; |
2100 | char cc = c; |
2061 | write (pt->rep.stdio.file, &cc, 1); |
2101 | write (pt->rep.stdio.file, &cc, 1); |
… | |
… | |
2074 | #endif |
2114 | #endif |
2075 | } |
2115 | } |
2076 | |
2116 | |
2077 | /* read characters up to delimiter, but cater to character constants */ |
2117 | /* read characters up to delimiter, but cater to character constants */ |
2078 | static char * |
2118 | static char * |
2079 | readstr_upto (SCHEME_P_ char *delim) |
2119 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2080 | { |
2120 | { |
2081 | char *p = SCHEME_V->strbuff; |
2121 | char *p = SCHEME_V->strbuff + skip; |
2082 | |
2122 | |
2083 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2123 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2084 | |
2124 | |
2085 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2125 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2086 | *p = 0; |
2126 | *p = 0; |
… | |
… | |
2093 | return SCHEME_V->strbuff; |
2133 | return SCHEME_V->strbuff; |
2094 | } |
2134 | } |
2095 | |
2135 | |
2096 | /* read string expression "xxx...xxx" */ |
2136 | /* read string expression "xxx...xxx" */ |
2097 | static pointer |
2137 | static pointer |
2098 | readstrexp (SCHEME_P) |
2138 | readstrexp (SCHEME_P_ char delim) |
2099 | { |
2139 | { |
2100 | char *p = SCHEME_V->strbuff; |
2140 | char *p = SCHEME_V->strbuff; |
2101 | int c; |
2141 | int c; |
2102 | int c1 = 0; |
2142 | int c1 = 0; |
2103 | enum |
|
|
2104 | { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2143 | enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2105 | |
2144 | |
2106 | for (;;) |
2145 | for (;;) |
2107 | { |
2146 | { |
2108 | c = inchar (SCHEME_A); |
2147 | c = inchar (SCHEME_A); |
2109 | |
2148 | |
… | |
… | |
2111 | return S_F; |
2150 | return S_F; |
2112 | |
2151 | |
2113 | switch (state) |
2152 | switch (state) |
2114 | { |
2153 | { |
2115 | case st_ok: |
2154 | case st_ok: |
2116 | switch (c) |
2155 | 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); |
2156 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2125 | |
2157 | |
2126 | default: |
2158 | if (ecb_expect_false (c == '\\')) |
|
|
2159 | state = st_bsl; |
|
|
2160 | else |
2127 | *p++ = c; |
2161 | *p++ = c; |
2128 | break; |
|
|
2129 | } |
|
|
2130 | |
2162 | |
2131 | break; |
2163 | break; |
2132 | |
2164 | |
2133 | case st_bsl: |
2165 | case st_bsl: |
2134 | switch (c) |
2166 | switch (c) |
… | |
… | |
2164 | case 'r': |
2196 | case 'r': |
2165 | *p++ = '\r'; |
2197 | *p++ = '\r'; |
2166 | state = st_ok; |
2198 | state = st_ok; |
2167 | break; |
2199 | break; |
2168 | |
2200 | |
2169 | case '"': |
|
|
2170 | *p++ = '"'; |
|
|
2171 | state = st_ok; |
|
|
2172 | break; |
|
|
2173 | |
|
|
2174 | default: |
2201 | default: |
2175 | *p++ = c; |
2202 | *p++ = c; |
2176 | state = st_ok; |
2203 | state = st_ok; |
2177 | break; |
2204 | break; |
2178 | } |
2205 | } |
2179 | |
2206 | |
2180 | break; |
2207 | break; |
2181 | |
2208 | |
2182 | case st_x1: |
2209 | case st_x1: |
2183 | case st_x2: |
2210 | case st_x2: |
2184 | c = toupper (c); |
2211 | c = tolower (c); |
2185 | |
2212 | |
2186 | if (c >= '0' && c <= 'F') |
2213 | if (c >= '0' && c <= '9') |
2187 | { |
|
|
2188 | if (c <= '9') |
|
|
2189 | c1 = (c1 << 4) + c - '0'; |
2214 | c1 = (c1 << 4) + c - '0'; |
2190 | else |
2215 | else if (c >= 'a' && c <= 'f') |
2191 | c1 = (c1 << 4) + c - 'A' + 10; |
2216 | 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 |
2217 | else |
2202 | return S_F; |
2218 | return S_F; |
|
|
2219 | |
|
|
2220 | if (state == st_x1) |
|
|
2221 | state = st_x2; |
|
|
2222 | else |
|
|
2223 | { |
|
|
2224 | *p++ = c1; |
|
|
2225 | state = st_ok; |
|
|
2226 | } |
2203 | |
2227 | |
2204 | break; |
2228 | break; |
2205 | |
2229 | |
2206 | case st_oct1: |
2230 | case st_oct1: |
2207 | case st_oct2: |
2231 | case st_oct2: |
… | |
… | |
2211 | backchar (SCHEME_A_ c); |
2235 | backchar (SCHEME_A_ c); |
2212 | state = st_ok; |
2236 | state = st_ok; |
2213 | } |
2237 | } |
2214 | else |
2238 | else |
2215 | { |
2239 | { |
2216 | if (state == st_oct2 && c1 >= 32) |
2240 | if (state == st_oct2 && c1 >= ' ') |
2217 | return S_F; |
2241 | return S_F; |
2218 | |
2242 | |
2219 | c1 = (c1 << 3) + (c - '0'); |
2243 | c1 = (c1 << 3) + (c - '0'); |
2220 | |
2244 | |
2221 | if (state == st_oct1) |
2245 | if (state == st_oct1) |
… | |
… | |
2226 | state = st_ok; |
2250 | state = st_ok; |
2227 | } |
2251 | } |
2228 | } |
2252 | } |
2229 | |
2253 | |
2230 | break; |
2254 | break; |
2231 | |
|
|
2232 | } |
2255 | } |
2233 | } |
2256 | } |
2234 | } |
2257 | } |
2235 | |
2258 | |
2236 | /* check c is in chars */ |
2259 | /* check c is in chars */ |
2237 | static INLINE int |
2260 | ecb_inline int |
2238 | is_one_of (char *s, int c) |
2261 | is_one_of (const char *s, int c) |
2239 | { |
2262 | { |
2240 | if (c == EOF) |
|
|
2241 | return 1; |
|
|
2242 | |
|
|
2243 | return !!strchr (s, c); |
2263 | return c == EOF || !!strchr (s, c); |
2244 | } |
2264 | } |
2245 | |
2265 | |
2246 | /* skip white characters */ |
2266 | /* skip white characters */ |
2247 | static INLINE int |
2267 | ecb_inline int |
2248 | skipspace (SCHEME_P) |
2268 | skipspace (SCHEME_P) |
2249 | { |
2269 | { |
2250 | int c, curr_line = 0; |
2270 | int c, curr_line = 0; |
2251 | |
2271 | |
2252 | do |
2272 | do |
2253 | { |
2273 | { |
2254 | c = inchar (SCHEME_A); |
2274 | c = inchar (SCHEME_A); |
|
|
2275 | |
2255 | #if SHOW_ERROR_LINE |
2276 | #if SHOW_ERROR_LINE |
2256 | if (c == '\n') |
2277 | if (ecb_expect_false (c == '\n')) |
2257 | curr_line++; |
2278 | curr_line++; |
2258 | #endif |
2279 | #endif |
|
|
2280 | |
|
|
2281 | if (ecb_expect_false (c == EOF)) |
|
|
2282 | return c; |
2259 | } |
2283 | } |
2260 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2284 | while (is_one_of (WHITESPACE, c)); |
2261 | |
2285 | |
2262 | /* record it */ |
2286 | /* record it */ |
2263 | #if SHOW_ERROR_LINE |
2287 | #if SHOW_ERROR_LINE |
2264 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2288 | 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; |
2289 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2266 | #endif |
2290 | #endif |
2267 | |
2291 | |
2268 | if (c != EOF) |
|
|
2269 | { |
|
|
2270 | backchar (SCHEME_A_ c); |
2292 | backchar (SCHEME_A_ c); |
2271 | return 1; |
2293 | return 1; |
2272 | } |
|
|
2273 | else |
|
|
2274 | return EOF; |
|
|
2275 | } |
2294 | } |
2276 | |
2295 | |
2277 | /* get token */ |
2296 | /* get token */ |
2278 | static int |
2297 | static int |
2279 | token (SCHEME_P) |
2298 | token (SCHEME_P) |
… | |
… | |
2295 | return TOK_RPAREN; |
2314 | return TOK_RPAREN; |
2296 | |
2315 | |
2297 | case '.': |
2316 | case '.': |
2298 | c = inchar (SCHEME_A); |
2317 | c = inchar (SCHEME_A); |
2299 | |
2318 | |
2300 | if (is_one_of (" \n\t", c)) |
2319 | if (is_one_of (WHITESPACE, c)) |
2301 | return TOK_DOT; |
2320 | return TOK_DOT; |
2302 | else |
2321 | else |
2303 | { |
2322 | { |
2304 | //TODO: ungetc twice in a row is not supported in C |
|
|
2305 | backchar (SCHEME_A_ c); |
2323 | backchar (SCHEME_A_ c); |
2306 | backchar (SCHEME_A_ '.'); |
|
|
2307 | return TOK_ATOM; |
2324 | return TOK_DOTATOM; |
2308 | } |
2325 | } |
|
|
2326 | |
|
|
2327 | case '|': |
|
|
2328 | return TOK_STRATOM; |
2309 | |
2329 | |
2310 | case '\'': |
2330 | case '\'': |
2311 | return TOK_QUOTE; |
2331 | return TOK_QUOTE; |
2312 | |
2332 | |
2313 | case ';': |
2333 | case ';': |
… | |
… | |
2445 | } |
2465 | } |
2446 | |
2466 | |
2447 | putcharacter (SCHEME_A_ '"'); |
2467 | putcharacter (SCHEME_A_ '"'); |
2448 | } |
2468 | } |
2449 | |
2469 | |
2450 | |
|
|
2451 | /* print atoms */ |
2470 | /* print atoms */ |
2452 | static void |
2471 | static void |
2453 | printatom (SCHEME_P_ pointer l, int f) |
2472 | printatom (SCHEME_P_ pointer l, int f) |
2454 | { |
2473 | { |
2455 | char *p; |
2474 | char *p; |
2456 | int len; |
2475 | int len; |
2457 | |
2476 | |
2458 | atom2str (SCHEME_A_ l, f, &p, &len); |
2477 | atom2str (SCHEME_A_ l, f, &p, &len); |
2459 | putchars (SCHEME_A_ p, len); |
2478 | putchars (SCHEME_A_ p, len); |
2460 | } |
2479 | } |
2461 | |
|
|
2462 | |
2480 | |
2463 | /* Uses internal buffer unless string pointer is already available */ |
2481 | /* Uses internal buffer unless string pointer is already available */ |
2464 | static void |
2482 | static void |
2465 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2483 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2466 | { |
2484 | { |
… | |
… | |
2480 | { |
2498 | { |
2481 | p = SCHEME_V->strbuff; |
2499 | p = SCHEME_V->strbuff; |
2482 | |
2500 | |
2483 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2501 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2484 | { |
2502 | { |
2485 | if (num_is_integer (l)) |
2503 | if (is_integer (l)) |
2486 | xnum (p, ivalue_unchecked (l)); |
2504 | xnum (p, ivalue_unchecked (l)); |
2487 | #if USE_REAL |
2505 | #if USE_REAL |
2488 | else |
2506 | else |
2489 | { |
2507 | { |
2490 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
2508 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
… | |
… | |
2627 | #endif |
2645 | #endif |
2628 | } |
2646 | } |
2629 | else if (is_continuation (l)) |
2647 | else if (is_continuation (l)) |
2630 | p = "#<CONTINUATION>"; |
2648 | p = "#<CONTINUATION>"; |
2631 | else |
2649 | else |
|
|
2650 | { |
|
|
2651 | #if USE_PRINTF |
|
|
2652 | p = SCHEME_V->strbuff; |
|
|
2653 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2654 | #else |
2632 | p = "#<ERROR>"; |
2655 | p = "#<ERROR>"; |
|
|
2656 | #endif |
|
|
2657 | } |
2633 | |
2658 | |
2634 | *pp = p; |
2659 | *pp = p; |
2635 | *plen = strlen (p); |
2660 | *plen = strlen (p); |
2636 | } |
2661 | } |
2637 | |
2662 | |
… | |
… | |
2745 | return 0; |
2770 | return 0; |
2746 | } |
2771 | } |
2747 | else if (is_number (a)) |
2772 | else if (is_number (a)) |
2748 | { |
2773 | { |
2749 | if (is_number (b)) |
2774 | if (is_number (b)) |
2750 | if (num_is_integer (a) == num_is_integer (b)) |
|
|
2751 | return num_cmp (nvalue (a), nvalue (b)) == 0; |
2775 | return num_cmp (nvalue (a), nvalue (b)) == 0; |
2752 | |
2776 | |
2753 | return 0; |
2777 | return 0; |
2754 | } |
2778 | } |
2755 | else if (is_character (a)) |
2779 | else if (is_character (a)) |
2756 | { |
2780 | { |
… | |
… | |
2782 | /* () is #t in R5RS */ |
2806 | /* () is #t in R5RS */ |
2783 | #define is_true(p) ((p) != S_F) |
2807 | #define is_true(p) ((p) != S_F) |
2784 | #define is_false(p) ((p) == S_F) |
2808 | #define is_false(p) ((p) == S_F) |
2785 | |
2809 | |
2786 | /* ========== Environment implementation ========== */ |
2810 | /* ========== 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 | |
2811 | |
2803 | #ifndef USE_ALIST_ENV |
2812 | #ifndef USE_ALIST_ENV |
2804 | |
2813 | |
2805 | /* |
2814 | /* |
2806 | * In this implementation, each frame of the environment may be |
2815 | * In this implementation, each frame of the environment may be |
… | |
… | |
2823 | |
2832 | |
2824 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2833 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2825 | setenvironment (SCHEME_V->envir); |
2834 | setenvironment (SCHEME_V->envir); |
2826 | } |
2835 | } |
2827 | |
2836 | |
2828 | static INLINE void |
2837 | static uint32_t |
|
|
2838 | sym_hash (pointer sym, uint32_t size) |
|
|
2839 | { |
|
|
2840 | uintptr_t ptr = (uintptr_t)sym; |
|
|
2841 | |
|
|
2842 | #if 0 |
|
|
2843 | /* table size is prime, so why mix */ |
|
|
2844 | ptr += ptr >> 32; |
|
|
2845 | ptr += ptr >> 16; |
|
|
2846 | ptr += ptr >> 8; |
|
|
2847 | #endif |
|
|
2848 | |
|
|
2849 | return ptr % size; |
|
|
2850 | } |
|
|
2851 | |
|
|
2852 | ecb_inline void |
2829 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2853 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2830 | { |
2854 | { |
2831 | pointer slot = immutable_cons (variable, value); |
2855 | pointer slot = immutable_cons (variable, value); |
2832 | |
2856 | |
2833 | if (is_vector (car (env))) |
2857 | if (is_vector (car (env))) |
2834 | { |
2858 | { |
2835 | int location = hash_fn (symname (variable), veclength (car (env))); |
2859 | int location = sym_hash (variable, veclength (car (env))); |
2836 | |
|
|
2837 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
2860 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2838 | } |
2861 | } |
2839 | else |
2862 | else |
2840 | set_car (env, immutable_cons (slot, car (env))); |
2863 | set_car (env, immutable_cons (slot, car (env))); |
2841 | } |
2864 | } |
2842 | |
2865 | |
2843 | static pointer |
2866 | static pointer |
2844 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2867 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2845 | { |
2868 | { |
2846 | pointer x, y; |
2869 | pointer x, y; |
2847 | int location; |
|
|
2848 | |
2870 | |
2849 | for (x = env; x != NIL; x = cdr (x)) |
2871 | for (x = env; x != NIL; x = cdr (x)) |
2850 | { |
2872 | { |
2851 | if (is_vector (car (x))) |
2873 | if (is_vector (car (x))) |
2852 | { |
2874 | { |
2853 | location = hash_fn (symname (hdl), veclength (car (x))); |
2875 | int location = sym_hash (hdl, veclength (car (x))); |
2854 | y = vector_elem (car (x), location); |
2876 | y = vector_get (car (x), location); |
2855 | } |
2877 | } |
2856 | else |
2878 | else |
2857 | y = car (x); |
2879 | y = car (x); |
2858 | |
2880 | |
2859 | for (; y != NIL; y = cdr (y)) |
2881 | for (; y != NIL; y = cdr (y)) |
2860 | if (caar (y) == hdl) |
2882 | if (caar (y) == hdl) |
2861 | break; |
2883 | break; |
2862 | |
2884 | |
2863 | if (y != NIL) |
2885 | if (y != NIL) |
|
|
2886 | return car (y); |
|
|
2887 | |
|
|
2888 | if (!all) |
2864 | break; |
2889 | break; |
2865 | |
|
|
2866 | if (!all) |
|
|
2867 | return NIL; |
|
|
2868 | } |
2890 | } |
2869 | |
|
|
2870 | if (x != NIL) |
|
|
2871 | return car (y); |
|
|
2872 | |
2891 | |
2873 | return NIL; |
2892 | return NIL; |
2874 | } |
2893 | } |
2875 | |
2894 | |
2876 | #else /* USE_ALIST_ENV */ |
2895 | #else /* USE_ALIST_ENV */ |
2877 | |
2896 | |
2878 | static INLINE void |
2897 | ecb_inline void |
2879 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2898 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2880 | { |
2899 | { |
2881 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2900 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2882 | setenvironment (SCHEME_V->envir); |
2901 | setenvironment (SCHEME_V->envir); |
2883 | } |
2902 | } |
2884 | |
2903 | |
2885 | static INLINE void |
2904 | ecb_inline void |
2886 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2905 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2887 | { |
2906 | { |
2888 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2907 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2889 | } |
2908 | } |
2890 | |
2909 | |
… | |
… | |
2898 | for (y = car (x); y != NIL; y = cdr (y)) |
2917 | for (y = car (x); y != NIL; y = cdr (y)) |
2899 | if (caar (y) == hdl) |
2918 | if (caar (y) == hdl) |
2900 | break; |
2919 | break; |
2901 | |
2920 | |
2902 | if (y != NIL) |
2921 | if (y != NIL) |
|
|
2922 | return car (y); |
2903 | break; |
2923 | break; |
2904 | |
2924 | |
2905 | if (!all) |
2925 | if (!all) |
2906 | return NIL; |
2926 | break; |
2907 | } |
2927 | } |
2908 | |
|
|
2909 | if (x != NIL) |
|
|
2910 | return car (y); |
|
|
2911 | |
2928 | |
2912 | return NIL; |
2929 | return NIL; |
2913 | } |
2930 | } |
2914 | |
2931 | |
2915 | #endif /* USE_ALIST_ENV else */ |
2932 | #endif /* USE_ALIST_ENV else */ |
2916 | |
2933 | |
2917 | static INLINE void |
2934 | ecb_inline void |
2918 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2935 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2919 | { |
2936 | { |
|
|
2937 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2920 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2938 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2921 | } |
2939 | } |
2922 | |
2940 | |
2923 | static INLINE void |
2941 | ecb_inline void |
2924 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2942 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2925 | { |
2943 | { |
2926 | set_cdr (slot, value); |
2944 | set_cdr (slot, value); |
2927 | } |
2945 | } |
2928 | |
2946 | |
2929 | static INLINE pointer |
2947 | ecb_inline pointer |
2930 | slot_value_in_env (pointer slot) |
2948 | slot_value_in_env (pointer slot) |
2931 | { |
2949 | { |
2932 | return cdr (slot); |
2950 | return cdr (slot); |
2933 | } |
2951 | } |
2934 | |
2952 | |
… | |
… | |
3025 | { |
3043 | { |
3026 | int nframes = (uintptr_t)SCHEME_V->dump; |
3044 | int nframes = (uintptr_t)SCHEME_V->dump; |
3027 | struct dump_stack_frame *next_frame; |
3045 | struct dump_stack_frame *next_frame; |
3028 | |
3046 | |
3029 | /* enough room for the next frame? */ |
3047 | /* enough room for the next frame? */ |
3030 | if (nframes >= SCHEME_V->dump_size) |
3048 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3031 | { |
3049 | { |
3032 | SCHEME_V->dump_size += STACK_GROWTH; |
3050 | 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); |
3051 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3034 | } |
3052 | } |
3035 | |
3053 | |
… | |
… | |
3062 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3080 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3063 | |
3081 | |
3064 | return 0; |
3082 | return 0; |
3065 | } |
3083 | } |
3066 | |
3084 | |
3067 | static INLINE void |
3085 | ecb_inline void |
3068 | dump_stack_reset (SCHEME_P) |
3086 | dump_stack_reset (SCHEME_P) |
3069 | { |
3087 | { |
3070 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3088 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3071 | SCHEME_V->dump = (pointer)+0; |
3089 | SCHEME_V->dump = (pointer)+0; |
3072 | } |
3090 | } |
3073 | |
3091 | |
3074 | static INLINE void |
3092 | ecb_inline void |
3075 | dump_stack_initialize (SCHEME_P) |
3093 | dump_stack_initialize (SCHEME_P) |
3076 | { |
3094 | { |
3077 | SCHEME_V->dump_size = 0; |
3095 | SCHEME_V->dump_size = 0; |
3078 | SCHEME_V->dump_base = 0; |
3096 | SCHEME_V->dump_base = 0; |
3079 | dump_stack_reset (SCHEME_A); |
3097 | dump_stack_reset (SCHEME_A); |
… | |
… | |
3132 | int i = 0; |
3150 | int i = 0; |
3133 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3151 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3134 | |
3152 | |
3135 | while (cont != NIL) |
3153 | while (cont != NIL) |
3136 | { |
3154 | { |
3137 | frame->op = ivalue (car (cont)); cont = cdr (cont); |
3155 | frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont); |
3138 | frame->args = car (cont) ; cont = cdr (cont); |
3156 | frame->args = car (cont) ; cont = cdr (cont); |
3139 | frame->envir = car (cont) ; cont = cdr (cont); |
3157 | frame->envir = car (cont) ; cont = cdr (cont); |
3140 | frame->code = car (cont) ; cont = cdr (cont); |
3158 | frame->code = car (cont) ; cont = cdr (cont); |
3141 | |
3159 | |
3142 | ++frame; |
3160 | ++frame; |
3143 | ++i; |
3161 | ++i; |
3144 | } |
3162 | } |
3145 | |
3163 | |
3146 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3164 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3147 | } |
3165 | } |
3148 | |
3166 | |
3149 | #else |
3167 | #else |
3150 | |
3168 | |
3151 | static INLINE void |
3169 | ecb_inline void |
3152 | dump_stack_reset (SCHEME_P) |
3170 | dump_stack_reset (SCHEME_P) |
3153 | { |
3171 | { |
3154 | SCHEME_V->dump = NIL; |
3172 | SCHEME_V->dump = NIL; |
3155 | } |
3173 | } |
3156 | |
3174 | |
3157 | static INLINE void |
3175 | ecb_inline void |
3158 | dump_stack_initialize (SCHEME_P) |
3176 | dump_stack_initialize (SCHEME_P) |
3159 | { |
3177 | { |
3160 | dump_stack_reset (SCHEME_A); |
3178 | dump_stack_reset (SCHEME_A); |
3161 | } |
3179 | } |
3162 | |
3180 | |
… | |
… | |
3174 | SCHEME_V->value = a; |
3192 | SCHEME_V->value = a; |
3175 | |
3193 | |
3176 | if (dump == NIL) |
3194 | if (dump == NIL) |
3177 | return -1; |
3195 | return -1; |
3178 | |
3196 | |
3179 | SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); |
3197 | SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump); |
3180 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3198 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3181 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3199 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3182 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3200 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3183 | |
3201 | |
3184 | SCHEME_V->dump = dump; |
3202 | SCHEME_V->dump = dump; |
3185 | |
3203 | |
3186 | return 0; |
3204 | return 0; |
3187 | } |
3205 | } |
… | |
… | |
3216 | |
3234 | |
3217 | #endif |
3235 | #endif |
3218 | |
3236 | |
3219 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3237 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3220 | |
3238 | |
|
|
3239 | #if EXPERIMENT |
|
|
3240 | static int |
|
|
3241 | debug (SCHEME_P_ int indent, pointer x) |
|
|
3242 | { |
|
|
3243 | int c; |
|
|
3244 | |
|
|
3245 | if (is_syntax (x)) |
|
|
3246 | { |
|
|
3247 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3248 | return 8 + 8; |
|
|
3249 | } |
|
|
3250 | |
|
|
3251 | if (x == NIL) |
|
|
3252 | { |
|
|
3253 | printf ("%*sNIL\n", indent, ""); |
|
|
3254 | return 3; |
|
|
3255 | } |
|
|
3256 | |
|
|
3257 | switch (type (x)) |
|
|
3258 | { |
|
|
3259 | case T_INTEGER: |
|
|
3260 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3261 | return 32+8; |
|
|
3262 | |
|
|
3263 | case T_SYMBOL: |
|
|
3264 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3265 | return 24+8; |
|
|
3266 | |
|
|
3267 | case T_CLOSURE: |
|
|
3268 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3269 | debug (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3270 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3271 | |
|
|
3272 | case T_PAIR: |
|
|
3273 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3274 | c = debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3275 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3276 | return c + 1; |
|
|
3277 | |
|
|
3278 | case T_PORT: |
|
|
3279 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3280 | return 24+8; |
|
|
3281 | |
|
|
3282 | case T_VECTOR: |
|
|
3283 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3284 | return 24+8; |
|
|
3285 | |
|
|
3286 | case T_ENVIRONMENT: |
|
|
3287 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3288 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3289 | |
|
|
3290 | default: |
|
|
3291 | printf ("unhandled type %d\n", type (x)); |
|
|
3292 | break; |
|
|
3293 | } |
|
|
3294 | } |
|
|
3295 | #endif |
|
|
3296 | |
3221 | static int |
3297 | static int |
3222 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3298 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3223 | { |
3299 | { |
3224 | pointer args = SCHEME_V->args; |
3300 | pointer args = SCHEME_V->args; |
3225 | pointer x, y; |
3301 | pointer x, y; |
3226 | |
3302 | |
3227 | switch (op) |
3303 | switch (op) |
3228 | { |
3304 | { |
|
|
3305 | #if EXPERIMENT //D |
|
|
3306 | case OP_DEBUG: |
|
|
3307 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
|
|
3308 | printf ("\n"); |
|
|
3309 | s_return (S_T); |
|
|
3310 | #endif |
3229 | case OP_LOAD: /* load */ |
3311 | case OP_LOAD: /* load */ |
3230 | if (file_interactive (SCHEME_A)) |
3312 | if (file_interactive (SCHEME_A)) |
3231 | { |
3313 | { |
3232 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3314 | putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n"); |
3233 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3315 | //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3234 | } |
3316 | } |
3235 | |
3317 | |
3236 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3318 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3237 | Error_1 ("unable to open", car (args)); |
3319 | Error_1 ("unable to open", car (args)); |
3238 | else |
3320 | else |
… | |
… | |
3242 | } |
3324 | } |
3243 | |
3325 | |
3244 | case OP_T0LVL: /* top level */ |
3326 | case OP_T0LVL: /* top level */ |
3245 | |
3327 | |
3246 | /* If we reached the end of file, this loop is done. */ |
3328 | /* If we reached the end of file, this loop is done. */ |
3247 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3329 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3248 | { |
3330 | { |
3249 | if (SCHEME_V->file_i == 0) |
3331 | if (SCHEME_V->file_i == 0) |
3250 | { |
3332 | { |
3251 | SCHEME_V->args = NIL; |
3333 | SCHEME_V->args = NIL; |
3252 | s_goto (OP_QUIT); |
3334 | s_goto (OP_QUIT); |
… | |
… | |
3330 | #endif |
3412 | #endif |
3331 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3413 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3332 | { |
3414 | { |
3333 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3415 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3334 | |
3416 | |
3335 | if (x != NIL) |
3417 | if (x == NIL) |
3336 | s_return (slot_value_in_env (x)); |
|
|
3337 | else |
|
|
3338 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3418 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3419 | |
|
|
3420 | s_return (slot_value_in_env (x)); |
3339 | } |
3421 | } |
3340 | else if (is_pair (SCHEME_V->code)) |
3422 | else if (is_pair (SCHEME_V->code)) |
3341 | { |
3423 | { |
3342 | x = car (SCHEME_V->code); |
3424 | x = car (SCHEME_V->code); |
3343 | |
3425 | |
… | |
… | |
3356 | } |
3438 | } |
3357 | else |
3439 | else |
3358 | s_return (SCHEME_V->code); |
3440 | s_return (SCHEME_V->code); |
3359 | |
3441 | |
3360 | case OP_E0ARGS: /* eval arguments */ |
3442 | case OP_E0ARGS: /* eval arguments */ |
3361 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3443 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3362 | { |
3444 | { |
3363 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3445 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3364 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3446 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3365 | SCHEME_V->code = SCHEME_V->value; |
3447 | SCHEME_V->code = SCHEME_V->value; |
3366 | s_goto (OP_APPLY); |
3448 | s_goto (OP_APPLY); |
… | |
… | |
3393 | |
3475 | |
3394 | case OP_TRACING: |
3476 | case OP_TRACING: |
3395 | { |
3477 | { |
3396 | int tr = SCHEME_V->tracing; |
3478 | int tr = SCHEME_V->tracing; |
3397 | |
3479 | |
3398 | SCHEME_V->tracing = ivalue (car (args)); |
3480 | SCHEME_V->tracing = ivalue_unchecked (car (args)); |
3399 | s_return (mk_integer (SCHEME_A_ tr)); |
3481 | s_return (mk_integer (SCHEME_A_ tr)); |
3400 | } |
3482 | } |
3401 | |
3483 | |
3402 | #endif |
3484 | #endif |
3403 | |
3485 | |
… | |
… | |
3420 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3502 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3421 | else if (is_foreign (SCHEME_V->code)) |
3503 | else if (is_foreign (SCHEME_V->code)) |
3422 | { |
3504 | { |
3423 | /* Keep nested calls from GC'ing the arglist */ |
3505 | /* Keep nested calls from GC'ing the arglist */ |
3424 | push_recent_alloc (SCHEME_A_ args, NIL); |
3506 | push_recent_alloc (SCHEME_A_ args, NIL); |
3425 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3507 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3426 | |
3508 | |
3427 | s_return (x); |
3509 | s_return (x); |
3428 | } |
3510 | } |
3429 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3511 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3430 | { |
3512 | { |
… | |
… | |
3467 | |
3549 | |
3468 | case OP_DOMACRO: /* do macro */ |
3550 | case OP_DOMACRO: /* do macro */ |
3469 | SCHEME_V->code = SCHEME_V->value; |
3551 | SCHEME_V->code = SCHEME_V->value; |
3470 | s_goto (OP_EVAL); |
3552 | s_goto (OP_EVAL); |
3471 | |
3553 | |
3472 | #if 1 |
|
|
3473 | |
|
|
3474 | case OP_LAMBDA: /* lambda */ |
3554 | case OP_LAMBDA: /* lambda */ |
3475 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3555 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3476 | set SCHEME_V->value fall thru */ |
3556 | set SCHEME_V->value fall thru */ |
3477 | { |
3557 | { |
3478 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3558 | 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); |
3564 | SCHEME_V->code = slot_value_in_env (f); |
3485 | s_goto (OP_APPLY); |
3565 | s_goto (OP_APPLY); |
3486 | } |
3566 | } |
3487 | |
3567 | |
3488 | SCHEME_V->value = SCHEME_V->code; |
3568 | SCHEME_V->value = SCHEME_V->code; |
3489 | /* Fallthru */ |
|
|
3490 | } |
3569 | } |
|
|
3570 | /* Fallthru */ |
3491 | |
3571 | |
3492 | case OP_LAMBDA1: |
3572 | case OP_LAMBDA1: |
3493 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3573 | 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 | |
3574 | |
3502 | case OP_MKCLOSURE: /* make-closure */ |
3575 | case OP_MKCLOSURE: /* make-closure */ |
3503 | x = car (args); |
3576 | x = car (args); |
3504 | |
3577 | |
3505 | if (car (x) == SCHEME_V->LAMBDA) |
3578 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
3912 | SCHEME_V->code = car (args); |
3985 | SCHEME_V->code = car (args); |
3913 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); |
3986 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); |
3914 | s_goto (OP_APPLY); |
3987 | s_goto (OP_APPLY); |
3915 | } |
3988 | } |
3916 | |
3989 | |
3917 | abort (); |
3990 | if (USE_ERROR_CHECKING) abort (); |
3918 | } |
3991 | } |
3919 | |
3992 | |
3920 | static int |
3993 | static int |
3921 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
3994 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
3922 | { |
3995 | { |
3923 | pointer args = SCHEME_V->args; |
3996 | pointer args = SCHEME_V->args; |
3924 | pointer x = car (args); |
3997 | pointer x = car (args); |
3925 | num v; |
3998 | num v; |
3926 | |
3999 | |
3927 | #if USE_MATH |
|
|
3928 | RVALUE dd; |
|
|
3929 | #endif |
|
|
3930 | |
|
|
3931 | switch (op) |
4000 | switch (op) |
3932 | { |
4001 | { |
3933 | #if USE_MATH |
4002 | #if USE_MATH |
3934 | case OP_INEX2EX: /* inexact->exact */ |
4003 | case OP_INEX2EX: /* inexact->exact */ |
3935 | if (num_is_integer (x)) |
4004 | if (!is_integer (x)) |
3936 | s_return (x); |
4005 | { |
3937 | else if (modf (rvalue_unchecked (x), &dd) == 0) |
4006 | RVALUE r = rvalue_unchecked (x); |
3938 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4007 | |
|
|
4008 | if (r == (RVALUE)(IVALUE)r) |
|
|
4009 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
3939 | else |
4010 | else |
3940 | Error_1 ("inexact->exact: not integral:", x); |
4011 | Error_1 ("inexact->exact: not integral:", x); |
|
|
4012 | } |
|
|
4013 | |
|
|
4014 | s_return (x); |
3941 | |
4015 | |
3942 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4016 | 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)))); |
4017 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
|
|
4018 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
3944 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4019 | 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)))); |
4020 | 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)))); |
4021 | 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)))); |
4022 | 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)))); |
4023 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
3949 | |
4024 | |
3950 | case OP_ATAN: |
4025 | case OP_ATAN: |
|
|
4026 | s_return (mk_real (SCHEME_A_ |
3951 | if (cdr (args) == NIL) |
4027 | cdr (args) == NIL |
3952 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4028 | ? atan (rvalue (x)) |
3953 | else |
4029 | : 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 | |
4030 | |
3959 | case OP_SQRT: |
4031 | case OP_SQRT: |
3960 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
4032 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
3961 | |
4033 | |
3962 | case OP_EXPT: |
4034 | case OP_EXPT: |
3963 | { |
4035 | { |
3964 | RVALUE result; |
4036 | RVALUE result; |
3965 | int real_result = 1; |
4037 | int real_result = 1; |
3966 | pointer y = cadr (args); |
4038 | pointer y = cadr (args); |
3967 | |
4039 | |
3968 | if (num_is_integer (x) && num_is_integer (y)) |
4040 | if (is_integer (x) && is_integer (y)) |
3969 | real_result = 0; |
4041 | real_result = 0; |
3970 | |
4042 | |
3971 | /* This 'if' is an R5RS compatibility fix. */ |
4043 | /* This 'if' is an R5RS compatibility fix. */ |
3972 | /* NOTE: Remove this 'if' fix for R6RS. */ |
4044 | /* NOTE: Remove this 'if' fix for R6RS. */ |
3973 | if (rvalue (x) == 0 && rvalue (y) < 0) |
4045 | if (rvalue (x) == 0 && rvalue (y) < 0) |
… | |
… | |
3979 | /* If the test fails, result is too big for integer. */ |
4051 | /* If the test fails, result is too big for integer. */ |
3980 | if (!real_result) |
4052 | if (!real_result) |
3981 | { |
4053 | { |
3982 | long result_as_long = result; |
4054 | long result_as_long = result; |
3983 | |
4055 | |
3984 | if (result != (RVALUE) result_as_long) |
4056 | if (result != result_as_long) |
3985 | real_result = 1; |
4057 | real_result = 1; |
3986 | } |
4058 | } |
3987 | |
4059 | |
3988 | if (real_result) |
4060 | if (real_result) |
3989 | s_return (mk_real (SCHEME_A_ result)); |
4061 | s_return (mk_real (SCHEME_A_ result)); |
3990 | else |
4062 | else |
3991 | s_return (mk_integer (SCHEME_A_ result)); |
4063 | s_return (mk_integer (SCHEME_A_ result)); |
3992 | } |
4064 | } |
3993 | |
4065 | |
3994 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
4066 | 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)))); |
4067 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
3996 | |
4068 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
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))); |
4069 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (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 |
4070 | #endif |
4015 | |
4071 | |
4016 | case OP_ADD: /* + */ |
4072 | case OP_ADD: /* + */ |
4017 | v = num_zero; |
4073 | v = num_zero; |
4018 | |
4074 | |
4019 | for (x = args; x != NIL; x = cdr (x)) |
4075 | for (x = args; x != NIL; x = cdr (x)) |
4020 | v = num_op ('+', v, nvalue (car (x))); |
4076 | v = num_op (NUM_ADD, v, nvalue (car (x))); |
4021 | |
4077 | |
4022 | s_return (mk_number (SCHEME_A_ v)); |
4078 | s_return (mk_number (SCHEME_A_ v)); |
4023 | |
4079 | |
4024 | case OP_MUL: /* * */ |
4080 | case OP_MUL: /* * */ |
4025 | v = num_one; |
4081 | v = num_one; |
4026 | |
4082 | |
4027 | for (x = args; x != NIL; x = cdr (x)) |
4083 | for (x = args; x != NIL; x = cdr (x)) |
4028 | v = num_op ('*', v, nvalue (car (x))); |
4084 | v = num_op (NUM_MUL, v, nvalue (car (x))); |
4029 | |
4085 | |
4030 | s_return (mk_number (SCHEME_A_ v)); |
4086 | s_return (mk_number (SCHEME_A_ v)); |
4031 | |
4087 | |
4032 | case OP_SUB: /* - */ |
4088 | case OP_SUB: /* - */ |
4033 | if (cdr (args) == NIL) |
4089 | if (cdr (args) == NIL) |
… | |
… | |
4040 | x = cdr (args); |
4096 | x = cdr (args); |
4041 | v = nvalue (car (args)); |
4097 | v = nvalue (car (args)); |
4042 | } |
4098 | } |
4043 | |
4099 | |
4044 | for (; x != NIL; x = cdr (x)) |
4100 | for (; x != NIL; x = cdr (x)) |
4045 | v = num_op ('-', v, nvalue (car (x))); |
4101 | v = num_op (NUM_SUB, v, nvalue (car (x))); |
4046 | |
4102 | |
4047 | s_return (mk_number (SCHEME_A_ v)); |
4103 | s_return (mk_number (SCHEME_A_ v)); |
4048 | |
4104 | |
4049 | case OP_DIV: /* / */ |
4105 | case OP_DIV: /* / */ |
4050 | if (cdr (args) == NIL) |
4106 | if (cdr (args) == NIL) |
… | |
… | |
4057 | x = cdr (args); |
4113 | x = cdr (args); |
4058 | v = nvalue (car (args)); |
4114 | v = nvalue (car (args)); |
4059 | } |
4115 | } |
4060 | |
4116 | |
4061 | for (; x != NIL; x = cdr (x)) |
4117 | for (; x != NIL; x = cdr (x)) |
4062 | { |
|
|
4063 | if (!is_zero_rvalue (rvalue (car (x)))) |
4118 | if (!is_zero_rvalue (rvalue (car (x)))) |
4064 | v = num_div (v, nvalue (car (x))); |
4119 | v = num_div (v, nvalue (car (x))); |
4065 | else |
4120 | else |
4066 | Error_0 ("/: division by zero"); |
4121 | Error_0 ("/: division by zero"); |
4067 | } |
|
|
4068 | |
4122 | |
4069 | s_return (mk_number (SCHEME_A_ v)); |
4123 | s_return (mk_number (SCHEME_A_ v)); |
4070 | |
4124 | |
4071 | case OP_INTDIV: /* quotient */ |
4125 | case OP_INTDIV: /* quotient */ |
4072 | if (cdr (args) == NIL) |
4126 | if (cdr (args) == NIL) |
… | |
… | |
4081 | } |
4135 | } |
4082 | |
4136 | |
4083 | for (; x != NIL; x = cdr (x)) |
4137 | for (; x != NIL; x = cdr (x)) |
4084 | { |
4138 | { |
4085 | if (ivalue (car (x)) != 0) |
4139 | if (ivalue (car (x)) != 0) |
4086 | v = num_op ('/', v, nvalue (car (x))); |
4140 | v = num_op (NUM_INTDIV, v, nvalue (car (x))); |
4087 | else |
4141 | else |
4088 | Error_0 ("quotient: division by zero"); |
4142 | Error_0 ("quotient: division by zero"); |
4089 | } |
4143 | } |
4090 | |
4144 | |
4091 | s_return (mk_number (SCHEME_A_ v)); |
4145 | s_return (mk_number (SCHEME_A_ v)); |
… | |
… | |
4108 | else |
4162 | else |
4109 | Error_0 ("modulo: division by zero"); |
4163 | Error_0 ("modulo: division by zero"); |
4110 | |
4164 | |
4111 | s_return (mk_number (SCHEME_A_ v)); |
4165 | s_return (mk_number (SCHEME_A_ v)); |
4112 | |
4166 | |
4113 | case OP_CAR: /* car */ |
4167 | /* the compiler will optimize this mess... */ |
4114 | s_return (caar (args)); |
4168 | case OP_CAR: op_car: s_return (car (x)); |
4115 | |
4169 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4116 | case OP_CDR: /* cdr */ |
4170 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4117 | s_return (cdar (args)); |
4171 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4172 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4173 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4174 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4175 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4176 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4177 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4178 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4179 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4180 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4181 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4182 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4183 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4184 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4185 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4186 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4187 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4188 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4189 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4190 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4191 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4192 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4193 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4194 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4195 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4196 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4197 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4118 | |
4198 | |
4119 | case OP_CONS: /* cons */ |
4199 | case OP_CONS: /* cons */ |
4120 | set_cdr (args, cadr (args)); |
4200 | set_cdr (args, cadr (args)); |
4121 | s_return (args); |
4201 | s_return (args); |
4122 | |
4202 | |
… | |
… | |
4137 | } |
4217 | } |
4138 | else |
4218 | else |
4139 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4219 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4140 | |
4220 | |
4141 | case OP_CHAR2INT: /* char->integer */ |
4221 | case OP_CHAR2INT: /* char->integer */ |
4142 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4222 | s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x))); |
4143 | |
4223 | |
4144 | case OP_INT2CHAR: /* integer->char */ |
4224 | case OP_INT2CHAR: /* integer->char */ |
4145 | s_return (mk_character (SCHEME_A_ ivalue (x))); |
4225 | s_return (mk_character (SCHEME_A_ ivalue_unchecked (x))); |
4146 | |
4226 | |
4147 | case OP_CHARUPCASE: |
4227 | case OP_CHARUPCASE: |
4148 | { |
4228 | { |
4149 | unsigned char c = ivalue (x); |
4229 | unsigned char c = ivalue_unchecked (x); |
4150 | c = toupper (c); |
4230 | c = toupper (c); |
4151 | s_return (mk_character (SCHEME_A_ c)); |
4231 | s_return (mk_character (SCHEME_A_ c)); |
4152 | } |
4232 | } |
4153 | |
4233 | |
4154 | case OP_CHARDNCASE: |
4234 | case OP_CHARDNCASE: |
4155 | { |
4235 | { |
4156 | unsigned char c = ivalue (x); |
4236 | unsigned char c = ivalue_unchecked (x); |
4157 | c = tolower (c); |
4237 | c = tolower (c); |
4158 | s_return (mk_character (SCHEME_A_ c)); |
4238 | s_return (mk_character (SCHEME_A_ c)); |
4159 | } |
4239 | } |
4160 | |
4240 | |
4161 | case OP_STR2SYM: /* string->symbol */ |
4241 | case OP_STR2SYM: /* string->symbol */ |
… | |
… | |
4238 | Error_1 ("atom->string: not an atom:", x); |
4318 | Error_1 ("atom->string: not an atom:", x); |
4239 | } |
4319 | } |
4240 | |
4320 | |
4241 | case OP_MKSTRING: /* make-string */ |
4321 | case OP_MKSTRING: /* make-string */ |
4242 | { |
4322 | { |
4243 | int fill = ' '; |
4323 | int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' '; |
4244 | int len; |
|
|
4245 | |
|
|
4246 | len = ivalue (x); |
4324 | int len = ivalue_unchecked (x); |
4247 | |
|
|
4248 | if (cdr (args) != NIL) |
|
|
4249 | fill = charvalue (cadr (args)); |
|
|
4250 | |
4325 | |
4251 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4326 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4252 | } |
4327 | } |
4253 | |
4328 | |
4254 | case OP_STRLEN: /* string-length */ |
4329 | case OP_STRLEN: /* string-length */ |
4255 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4330 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4256 | |
4331 | |
4257 | case OP_STRREF: /* string-ref */ |
4332 | case OP_STRREF: /* string-ref */ |
4258 | { |
4333 | { |
4259 | char *str; |
|
|
4260 | int index; |
|
|
4261 | |
|
|
4262 | str = strvalue (x); |
4334 | char *str = strvalue (x); |
4263 | |
|
|
4264 | index = ivalue (cadr (args)); |
4335 | int index = ivalue_unchecked (cadr (args)); |
4265 | |
4336 | |
4266 | if (index >= strlength (x)) |
4337 | if (index >= strlength (x)) |
4267 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4338 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4268 | |
4339 | |
4269 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4340 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4270 | } |
4341 | } |
4271 | |
4342 | |
4272 | case OP_STRSET: /* string-set! */ |
4343 | case OP_STRSET: /* string-set! */ |
4273 | { |
4344 | { |
4274 | char *str; |
4345 | char *str = strvalue (x); |
4275 | int index; |
4346 | int index = ivalue_unchecked (cadr (args)); |
4276 | int c; |
4347 | int c; |
4277 | |
4348 | |
4278 | if (is_immutable (x)) |
4349 | if (is_immutable (x)) |
4279 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4350 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4280 | |
|
|
4281 | str = strvalue (x); |
|
|
4282 | |
|
|
4283 | index = ivalue (cadr (args)); |
|
|
4284 | |
4351 | |
4285 | if (index >= strlength (x)) |
4352 | if (index >= strlength (x)) |
4286 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4353 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4287 | |
4354 | |
4288 | c = charvalue (caddr (args)); |
4355 | c = charvalue (caddr (args)); |
… | |
… | |
4311 | s_return (newstr); |
4378 | s_return (newstr); |
4312 | } |
4379 | } |
4313 | |
4380 | |
4314 | case OP_SUBSTR: /* substring */ |
4381 | case OP_SUBSTR: /* substring */ |
4315 | { |
4382 | { |
4316 | char *str; |
4383 | char *str = strvalue (x); |
4317 | int index0; |
4384 | int index0 = ivalue_unchecked (cadr (args)); |
4318 | int index1; |
4385 | int index1; |
4319 | int len; |
4386 | int len; |
4320 | |
4387 | |
4321 | str = strvalue (x); |
|
|
4322 | |
|
|
4323 | index0 = ivalue (cadr (args)); |
|
|
4324 | |
|
|
4325 | if (index0 > strlength (x)) |
4388 | if (index0 > strlength (x)) |
4326 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4389 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4327 | |
4390 | |
4328 | if (cddr (args) != NIL) |
4391 | if (cddr (args) != NIL) |
4329 | { |
4392 | { |
4330 | index1 = ivalue (caddr (args)); |
4393 | index1 = ivalue_unchecked (caddr (args)); |
4331 | |
4394 | |
4332 | if (index1 > strlength (x) || index1 < index0) |
4395 | if (index1 > strlength (x) || index1 < index0) |
4333 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4396 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4334 | } |
4397 | } |
4335 | else |
4398 | else |
… | |
… | |
4358 | if (SCHEME_V->no_memory) |
4421 | if (SCHEME_V->no_memory) |
4359 | s_return (S_SINK); |
4422 | s_return (S_SINK); |
4360 | #endif |
4423 | #endif |
4361 | |
4424 | |
4362 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4425 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4363 | set_vector_elem (vec, i, car (x)); |
4426 | vector_set (vec, i, car (x)); |
4364 | |
4427 | |
4365 | s_return (vec); |
4428 | s_return (vec); |
4366 | } |
4429 | } |
4367 | |
4430 | |
4368 | case OP_MKVECTOR: /* make-vector */ |
4431 | case OP_MKVECTOR: /* make-vector */ |
4369 | { |
4432 | { |
4370 | pointer fill = NIL; |
4433 | pointer fill = NIL; |
4371 | int len; |
|
|
4372 | pointer vec; |
4434 | pointer vec; |
4373 | |
|
|
4374 | len = ivalue (x); |
4435 | int len = ivalue_unchecked (x); |
4375 | |
4436 | |
4376 | if (cdr (args) != NIL) |
4437 | if (cdr (args) != NIL) |
4377 | fill = cadr (args); |
4438 | fill = cadr (args); |
4378 | |
4439 | |
4379 | vec = mk_vector (SCHEME_A_ len); |
4440 | vec = mk_vector (SCHEME_A_ len); |
… | |
… | |
4382 | if (SCHEME_V->no_memory) |
4443 | if (SCHEME_V->no_memory) |
4383 | s_return (S_SINK); |
4444 | s_return (S_SINK); |
4384 | #endif |
4445 | #endif |
4385 | |
4446 | |
4386 | if (fill != NIL) |
4447 | if (fill != NIL) |
4387 | fill_vector (vec, fill); |
4448 | fill_vector (vec, 0, fill); |
4388 | |
4449 | |
4389 | s_return (vec); |
4450 | s_return (vec); |
4390 | } |
4451 | } |
4391 | |
4452 | |
4392 | case OP_VECLEN: /* vector-length */ |
4453 | case OP_VECLEN: /* vector-length */ |
4393 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4454 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4394 | |
4455 | |
|
|
4456 | case OP_VECRESIZE: |
|
|
4457 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4458 | s_return (x); |
|
|
4459 | |
4395 | case OP_VECREF: /* vector-ref */ |
4460 | case OP_VECREF: /* vector-ref */ |
4396 | { |
4461 | { |
4397 | int index; |
|
|
4398 | |
|
|
4399 | index = ivalue (cadr (args)); |
4462 | int index = ivalue_unchecked (cadr (args)); |
4400 | |
4463 | |
4401 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4464 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4402 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4465 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4403 | |
4466 | |
4404 | s_return (vector_elem (x, index)); |
4467 | s_return (vector_get (x, index)); |
4405 | } |
4468 | } |
4406 | |
4469 | |
4407 | case OP_VECSET: /* vector-set! */ |
4470 | case OP_VECSET: /* vector-set! */ |
4408 | { |
4471 | { |
4409 | int index; |
4472 | int index = ivalue_unchecked (cadr (args)); |
4410 | |
4473 | |
4411 | if (is_immutable (x)) |
4474 | if (is_immutable (x)) |
4412 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4475 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4413 | |
4476 | |
4414 | index = ivalue (cadr (args)); |
|
|
4415 | |
|
|
4416 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4477 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4417 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4478 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4418 | |
4479 | |
4419 | set_vector_elem (x, index, caddr (args)); |
4480 | vector_set (x, index, caddr (args)); |
4420 | s_return (x); |
4481 | s_return (x); |
4421 | } |
4482 | } |
4422 | } |
4483 | } |
4423 | |
4484 | |
4424 | abort (); |
4485 | if (USE_ERROR_CHECKING) abort (); |
4425 | } |
|
|
4426 | |
|
|
4427 | INTERFACE int |
|
|
4428 | is_list (SCHEME_P_ pointer a) |
|
|
4429 | { |
|
|
4430 | return list_length (SCHEME_A_ a) >= 0; |
|
|
4431 | } |
|
|
4432 | |
|
|
4433 | /* Result is: |
|
|
4434 | proper list: length |
|
|
4435 | circular list: -1 |
|
|
4436 | not even a pair: -2 |
|
|
4437 | dotted list: -2 minus length before dot |
|
|
4438 | */ |
|
|
4439 | INTERFACE int |
|
|
4440 | list_length (SCHEME_P_ pointer a) |
|
|
4441 | { |
|
|
4442 | int i = 0; |
|
|
4443 | pointer slow, fast; |
|
|
4444 | |
|
|
4445 | slow = fast = a; |
|
|
4446 | |
|
|
4447 | while (1) |
|
|
4448 | { |
|
|
4449 | if (fast == NIL) |
|
|
4450 | return i; |
|
|
4451 | |
|
|
4452 | if (!is_pair (fast)) |
|
|
4453 | return -2 - i; |
|
|
4454 | |
|
|
4455 | fast = cdr (fast); |
|
|
4456 | ++i; |
|
|
4457 | |
|
|
4458 | if (fast == NIL) |
|
|
4459 | return i; |
|
|
4460 | |
|
|
4461 | if (!is_pair (fast)) |
|
|
4462 | return -2 - i; |
|
|
4463 | |
|
|
4464 | ++i; |
|
|
4465 | fast = cdr (fast); |
|
|
4466 | |
|
|
4467 | /* Safe because we would have already returned if `fast' |
|
|
4468 | encountered a non-pair. */ |
|
|
4469 | slow = cdr (slow); |
|
|
4470 | |
|
|
4471 | if (fast == slow) |
|
|
4472 | { |
|
|
4473 | /* the fast pointer has looped back around and caught up |
|
|
4474 | with the slow pointer, hence the structure is circular, |
|
|
4475 | not of finite length, and therefore not a list */ |
|
|
4476 | return -1; |
|
|
4477 | } |
|
|
4478 | } |
|
|
4479 | } |
4486 | } |
4480 | |
4487 | |
4481 | static int |
4488 | static int |
4482 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4489 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4483 | { |
4490 | { |
… | |
… | |
4517 | pointer d = cdr (args); |
4524 | pointer d = cdr (args); |
4518 | int r; |
4525 | int r; |
4519 | |
4526 | |
4520 | switch (op) |
4527 | switch (op) |
4521 | { |
4528 | { |
4522 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4529 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4523 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
4530 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break; |
4524 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4531 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4525 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4532 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4526 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
4533 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4534 | case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break; |
4527 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4535 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4528 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4536 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4529 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4537 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4530 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4538 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4531 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4539 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4532 | |
4540 | |
4533 | #if USE_CHAR_CLASSIFIERS |
4541 | #if USE_CHAR_CLASSIFIERS |
4534 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; |
4542 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4535 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; |
4543 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4536 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; |
4544 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4537 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; |
4545 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break; |
4538 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; |
4546 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break; |
4539 | #endif |
4547 | #endif |
4540 | |
4548 | |
4541 | #if USE_PORTS |
4549 | #if USE_PORTS |
4542 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4550 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4543 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
4551 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
… | |
… | |
4586 | } |
4594 | } |
4587 | else |
4595 | else |
4588 | s_return (SCHEME_V->code); |
4596 | s_return (SCHEME_V->code); |
4589 | |
4597 | |
4590 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4598 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4591 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4599 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4592 | s_return (SCHEME_V->value); |
4600 | s_return (SCHEME_V->value); |
4593 | |
4601 | |
4594 | #if USE_PORTS |
4602 | #if USE_PORTS |
4595 | |
4603 | |
4596 | case OP_WRITE: /* write */ |
4604 | case OP_WRITE: /* write */ |
… | |
… | |
4741 | SCHEME_V->gc_verbose = (a != S_F); |
4749 | SCHEME_V->gc_verbose = (a != S_F); |
4742 | s_retbool (was); |
4750 | s_retbool (was); |
4743 | } |
4751 | } |
4744 | |
4752 | |
4745 | case OP_NEWSEGMENT: /* new-segment */ |
4753 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4754 | #if 0 |
4746 | if (!is_pair (args) || !is_number (a)) |
4755 | if (!is_pair (args) || !is_number (a)) |
4747 | Error_0 ("new-segment: argument must be a number"); |
4756 | Error_0 ("new-segment: argument must be a number"); |
4748 | |
4757 | #endif |
4749 | alloc_cellseg (SCHEME_A_ (int)ivalue (a)); |
4758 | s_retbool (alloc_cellseg (SCHEME_A)); |
4750 | |
|
|
4751 | s_return (S_T); |
|
|
4752 | |
4759 | |
4753 | case OP_OBLIST: /* oblist */ |
4760 | case OP_OBLIST: /* oblist */ |
4754 | s_return (oblist_all_symbols (SCHEME_A)); |
4761 | s_return (oblist_all_symbols (SCHEME_A)); |
4755 | |
4762 | |
4756 | #if USE_PORTS |
4763 | #if USE_PORTS |
… | |
… | |
4783 | break; |
4790 | break; |
4784 | } |
4791 | } |
4785 | |
4792 | |
4786 | p = port_from_filename (SCHEME_A_ strvalue (a), prop); |
4793 | p = port_from_filename (SCHEME_A_ strvalue (a), prop); |
4787 | |
4794 | |
4788 | if (p == NIL) |
4795 | s_return (p == NIL ? S_F : p); |
4789 | s_return (S_F); |
|
|
4790 | |
|
|
4791 | s_return (p); |
|
|
4792 | } |
4796 | } |
4793 | |
4797 | |
4794 | # if USE_STRING_PORTS |
4798 | # if USE_STRING_PORTS |
4795 | |
4799 | |
4796 | case OP_OPEN_INSTRING: /* open-input-string */ |
4800 | case OP_OPEN_INSTRING: /* open-input-string */ |
… | |
… | |
4811 | } |
4815 | } |
4812 | |
4816 | |
4813 | p = port_from_string (SCHEME_A_ strvalue (a), |
4817 | p = port_from_string (SCHEME_A_ strvalue (a), |
4814 | strvalue (a) + strlength (a), prop); |
4818 | strvalue (a) + strlength (a), prop); |
4815 | |
4819 | |
4816 | if (p == NIL) |
4820 | s_return (p == NIL ? S_F : p); |
4817 | s_return (S_F); |
|
|
4818 | |
|
|
4819 | s_return (p); |
|
|
4820 | } |
4821 | } |
4821 | |
4822 | |
4822 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
4823 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
4823 | { |
4824 | { |
4824 | pointer p; |
4825 | pointer p; |
4825 | |
4826 | |
4826 | if (a == NIL) |
4827 | if (a == NIL) |
4827 | { |
|
|
4828 | p = port_from_scratch (SCHEME_A); |
4828 | p = port_from_scratch (SCHEME_A); |
4829 | |
|
|
4830 | if (p == NIL) |
|
|
4831 | s_return (S_F); |
|
|
4832 | } |
|
|
4833 | else |
4829 | else |
4834 | { |
|
|
4835 | p = port_from_string (SCHEME_A_ strvalue (a), |
4830 | p = port_from_string (SCHEME_A_ strvalue (a), |
4836 | strvalue (a) + strlength (a), port_output); |
4831 | strvalue (a) + strlength (a), port_output); |
4837 | |
4832 | |
4838 | if (p == NIL) |
4833 | s_return (p == NIL ? S_F : p); |
4839 | s_return (S_F); |
|
|
4840 | } |
|
|
4841 | |
|
|
4842 | s_return (p); |
|
|
4843 | } |
4834 | } |
4844 | |
4835 | |
4845 | case OP_GET_OUTSTRING: /* get-output-string */ |
4836 | case OP_GET_OUTSTRING: /* get-output-string */ |
4846 | { |
4837 | { |
4847 | port *p; |
4838 | port *p = port (a); |
4848 | |
4839 | |
4849 | if ((p = a->object.port)->kind & port_string) |
4840 | if (p->kind & port_string) |
4850 | { |
4841 | { |
4851 | off_t size; |
4842 | off_t size; |
4852 | char *str; |
4843 | char *str; |
4853 | |
4844 | |
4854 | size = p->rep.string.curr - p->rep.string.start + 1; |
4845 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4886 | case OP_CURR_ENV: /* current-environment */ |
4877 | case OP_CURR_ENV: /* current-environment */ |
4887 | s_return (SCHEME_V->envir); |
4878 | s_return (SCHEME_V->envir); |
4888 | |
4879 | |
4889 | } |
4880 | } |
4890 | |
4881 | |
4891 | abort (); |
4882 | if (USE_ERROR_CHECKING) abort (); |
4892 | } |
4883 | } |
4893 | |
4884 | |
4894 | static int |
4885 | static int |
4895 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4886 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4896 | { |
4887 | { |
… | |
… | |
4959 | int res; |
4950 | int res; |
4960 | |
4951 | |
4961 | if (is_pair (args)) |
4952 | if (is_pair (args)) |
4962 | p = car (args); |
4953 | p = car (args); |
4963 | |
4954 | |
4964 | res = p->object.port->kind & port_string; |
4955 | res = port (p)->kind & port_string; |
4965 | |
4956 | |
4966 | s_retbool (res); |
4957 | s_retbool (res); |
4967 | } |
4958 | } |
4968 | |
4959 | |
4969 | case OP_SET_INPORT: /* set-input-port */ |
4960 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
5028 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5019 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5029 | SCHEME_V->tok = token (SCHEME_A); |
5020 | SCHEME_V->tok = token (SCHEME_A); |
5030 | s_goto (OP_RDSEXPR); |
5021 | s_goto (OP_RDSEXPR); |
5031 | |
5022 | |
5032 | case TOK_ATOM: |
5023 | case TOK_ATOM: |
5033 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); |
5024 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
|
|
5025 | |
|
|
5026 | case TOK_DOTATOM: |
|
|
5027 | SCHEME_V->strbuff[0] = '.'; |
|
|
5028 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
|
|
5029 | |
|
|
5030 | case TOK_STRATOM: |
|
|
5031 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5032 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5033 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
5034 | |
5034 | |
5035 | case TOK_DQUOTE: |
5035 | case TOK_DQUOTE: |
5036 | x = readstrexp (SCHEME_A); |
5036 | x = readstrexp (SCHEME_A_ '"'); |
5037 | |
5037 | |
5038 | if (x == S_F) |
5038 | if (x == S_F) |
5039 | Error_0 ("Error reading string"); |
5039 | Error_0 ("Error reading string"); |
5040 | |
5040 | |
5041 | setimmutable (x); |
5041 | setimmutable (x); |
… | |
… | |
5053 | s_goto (OP_EVAL); |
5053 | s_goto (OP_EVAL); |
5054 | } |
5054 | } |
5055 | } |
5055 | } |
5056 | |
5056 | |
5057 | case TOK_SHARP_CONST: |
5057 | case TOK_SHARP_CONST: |
5058 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) |
5058 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
5059 | Error_0 ("undefined sharp expression"); |
5059 | Error_0 ("undefined sharp expression"); |
5060 | else |
5060 | else |
5061 | s_return (x); |
5061 | s_return (x); |
5062 | |
5062 | |
5063 | default: |
5063 | default: |
… | |
… | |
5215 | putstr (SCHEME_A_ ")"); |
5215 | putstr (SCHEME_A_ ")"); |
5216 | s_return (S_T); |
5216 | s_return (S_T); |
5217 | } |
5217 | } |
5218 | else |
5218 | else |
5219 | { |
5219 | { |
5220 | pointer elem = vector_elem (vec, i); |
5220 | pointer elem = vector_get (vec, i); |
5221 | |
5221 | |
5222 | ivalue_unchecked (cdr (args)) = i + 1; |
5222 | ivalue_unchecked (cdr (args)) = i + 1; |
5223 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5223 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5224 | SCHEME_V->args = elem; |
5224 | SCHEME_V->args = elem; |
5225 | |
5225 | |
… | |
… | |
5229 | s_goto (OP_P0LIST); |
5229 | s_goto (OP_P0LIST); |
5230 | } |
5230 | } |
5231 | } |
5231 | } |
5232 | } |
5232 | } |
5233 | |
5233 | |
5234 | abort (); |
5234 | if (USE_ERROR_CHECKING) abort (); |
5235 | } |
5235 | } |
5236 | |
5236 | |
5237 | static int |
5237 | static int |
5238 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5238 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5239 | { |
5239 | { |
… | |
… | |
5285 | |
5285 | |
5286 | case OP_CLOSUREP: /* closure? */ |
5286 | case OP_CLOSUREP: /* closure? */ |
5287 | /* |
5287 | /* |
5288 | * Note, macro object is also a closure. |
5288 | * Note, macro object is also a closure. |
5289 | * Therefore, (closure? <#MACRO>) ==> #t |
5289 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5290 | * (schmorp) well, obviously not, fix? TODO |
5290 | */ |
5291 | */ |
5291 | s_retbool (is_closure (a)); |
5292 | s_retbool (is_closure (a)); |
5292 | |
5293 | |
5293 | case OP_MACROP: /* macro? */ |
5294 | case OP_MACROP: /* macro? */ |
5294 | s_retbool (is_macro (a)); |
5295 | s_retbool (is_macro (a)); |
5295 | } |
5296 | } |
5296 | |
5297 | |
5297 | abort (); |
5298 | if (USE_ERROR_CHECKING) abort (); |
5298 | } |
5299 | } |
5299 | |
5300 | |
5300 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5301 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5301 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5302 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5302 | |
5303 | |
5303 | typedef int (*test_predicate)(pointer); |
5304 | typedef int (*test_predicate)(pointer); |
5304 | static int |
5305 | static int |
5305 | is_any (pointer p) |
5306 | tst_any (pointer p) |
5306 | { |
5307 | { |
5307 | return 1; |
5308 | return 1; |
5308 | } |
5309 | } |
5309 | |
5310 | |
5310 | static int |
5311 | static int |
5311 | is_nonneg (pointer p) |
5312 | tst_inonneg (pointer p) |
5312 | { |
5313 | { |
5313 | return ivalue (p) >= 0 && is_integer (p); |
5314 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5314 | } |
5315 | } |
5315 | |
5316 | |
5316 | static int |
5317 | static int |
5317 | tst_is_list (pointer p) |
5318 | tst_is_list (SCHEME_P_ pointer p) |
5318 | { |
5319 | { |
5319 | return p == NIL || is_pair (p); |
5320 | return p == NIL || is_pair (p); |
5320 | } |
5321 | } |
5321 | |
5322 | |
5322 | /* Correspond carefully with following defines! */ |
5323 | /* Correspond carefully with following defines! */ |
5323 | static struct |
5324 | static struct |
5324 | { |
5325 | { |
5325 | test_predicate fct; |
5326 | test_predicate fct; |
5326 | const char *kind; |
5327 | const char *kind; |
5327 | } tests[] = |
5328 | } tests[] = { |
5328 | { |
|
|
5329 | { is_any, 0 }, |
5329 | { tst_any , 0 }, |
5330 | { is_string, "string" }, |
5330 | { is_string , "string" }, |
5331 | { is_symbol, "symbol" }, |
5331 | { is_symbol , "symbol" }, |
5332 | { is_port, "port" }, |
5332 | { is_port , "port" }, |
5333 | { is_inport, "input port" }, |
5333 | { is_inport , "input port" }, |
5334 | { is_outport, "output port" }, |
5334 | { is_outport , "output port" }, |
5335 | { is_environment, "environment" }, |
5335 | { is_environment, "environment" }, |
5336 | { is_pair, "pair" }, |
5336 | { is_pair , "pair" }, |
5337 | { tst_is_list, "pair or '()" }, |
5337 | { 0 , "pair or '()" }, |
5338 | { is_character, "character" }, |
5338 | { is_character , "character" }, |
5339 | { is_vector, "vector" }, |
5339 | { is_vector , "vector" }, |
5340 | { is_number, "number" }, |
5340 | { is_number , "number" }, |
5341 | { is_integer, "integer" }, |
5341 | { is_integer , "integer" }, |
5342 | { is_nonneg, "non-negative integer" } |
5342 | { tst_inonneg , "non-negative integer" } |
5343 | }; |
5343 | }; |
5344 | |
5344 | |
5345 | #define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ |
5345 | #define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ |
5346 | #define TST_ANY "\001" |
5346 | #define TST_ANY "\001" |
5347 | #define TST_STRING "\002" |
5347 | #define TST_STRING "\002" |
… | |
… | |
5388 | typedef struct |
5388 | typedef struct |
5389 | { |
5389 | { |
5390 | uint8_t func; |
5390 | uint8_t func; |
5391 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
5391 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
5392 | uint8_t builtin; |
5392 | uint8_t builtin; |
|
|
5393 | #if USE_ERROR_CHECKING |
5393 | uint8_t min_arity; |
5394 | uint8_t min_arity; |
5394 | uint8_t max_arity; |
5395 | uint8_t max_arity; |
5395 | char arg_tests_encoding[3]; |
5396 | char arg_tests_encoding[3]; |
|
|
5397 | #endif |
5396 | } op_code_info; |
5398 | } op_code_info; |
5397 | |
5399 | |
5398 | static const op_code_info dispatch_table[] = { |
5400 | static const op_code_info dispatch_table[] = { |
|
|
5401 | #if USE_ERROR_CHECKING |
5399 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, |
5402 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, |
|
|
5403 | #else |
|
|
5404 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 }, |
|
|
5405 | #endif |
5400 | #include "opdefines.h" |
5406 | #include "opdefines.h" |
5401 | #undef OP_DEF |
5407 | #undef OP_DEF |
5402 | {0} |
5408 | {0} |
5403 | }; |
5409 | }; |
5404 | |
5410 | |
5405 | /* kernel of this interpreter */ |
5411 | /* kernel of this interpreter */ |
5406 | static void |
5412 | static void ecb_hot |
5407 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5413 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5408 | { |
5414 | { |
5409 | SCHEME_V->op = op; |
5415 | SCHEME_V->op = op; |
5410 | |
5416 | |
5411 | for (;;) |
5417 | for (;;) |
… | |
… | |
5446 | { |
5452 | { |
5447 | pointer arg = car (arglist); |
5453 | pointer arg = car (arglist); |
5448 | |
5454 | |
5449 | j = t[0]; |
5455 | j = t[0]; |
5450 | |
5456 | |
|
|
5457 | /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */ |
|
|
5458 | if (j == TST_LIST[0]) |
|
|
5459 | { |
|
|
5460 | if (!tst_is_list (SCHEME_A_ arg)) |
|
|
5461 | break; |
|
|
5462 | } |
|
|
5463 | else |
|
|
5464 | { |
5451 | if (!tests[j - 1].fct (arg)) |
5465 | if (!tests[j - 1].fct (arg)) |
5452 | break; |
5466 | break; |
|
|
5467 | } |
5453 | |
5468 | |
5454 | if (t[1]) /* last test is replicated as necessary */ |
5469 | if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */ |
5455 | t++; |
5470 | t++; |
5456 | |
5471 | |
5457 | arglist = cdr (arglist); |
5472 | arglist = cdr (arglist); |
5458 | i++; |
5473 | i++; |
5459 | } |
5474 | } |
… | |
… | |
5485 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5500 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5486 | return; |
5501 | return; |
5487 | |
5502 | |
5488 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5503 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5489 | { |
5504 | { |
5490 | xwrstr ("No memory!\n"); |
5505 | putstr (SCHEME_A_ "No memory!\n"); |
5491 | return; |
5506 | return; |
5492 | } |
5507 | } |
5493 | } |
5508 | } |
5494 | } |
5509 | } |
5495 | |
5510 | |
… | |
… | |
5514 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5529 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5515 | { |
5530 | { |
5516 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5531 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5517 | set_typeflag (y, (T_PROC | T_ATOM)); |
5532 | set_typeflag (y, (T_PROC | T_ATOM)); |
5518 | ivalue_unchecked (y) = op; |
5533 | ivalue_unchecked (y) = op; |
5519 | set_num_integer (y); |
|
|
5520 | return y; |
5534 | return y; |
5521 | } |
5535 | } |
5522 | |
5536 | |
5523 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5537 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5524 | static int |
5538 | static int |
5525 | syntaxnum (pointer p) |
5539 | syntaxnum (pointer p) |
5526 | { |
5540 | { |
5527 | const char *s = strvalue (car (p)); |
5541 | const char *s = strvalue (p); |
5528 | |
5542 | |
5529 | switch (strlength (car (p))) |
5543 | switch (strlength (p)) |
5530 | { |
5544 | { |
5531 | case 2: |
5545 | case 2: |
5532 | if (s[0] == 'i') |
5546 | if (s[0] == 'i') |
5533 | return OP_IF0; /* if */ |
5547 | return OP_IF0; /* if */ |
5534 | else |
5548 | else |
… | |
… | |
5589 | return OP_C0STREAM; /* cons-stream */ |
5603 | return OP_C0STREAM; /* cons-stream */ |
5590 | } |
5604 | } |
5591 | } |
5605 | } |
5592 | |
5606 | |
5593 | #if USE_MULTIPLICITY |
5607 | #if USE_MULTIPLICITY |
5594 | scheme * |
5608 | ecb_cold scheme * |
5595 | scheme_init_new () |
5609 | scheme_init_new () |
5596 | { |
5610 | { |
5597 | scheme *sc = malloc (sizeof (scheme)); |
5611 | scheme *sc = malloc (sizeof (scheme)); |
5598 | |
5612 | |
5599 | if (!scheme_init (SCHEME_A)) |
5613 | if (!scheme_init (SCHEME_A)) |
… | |
… | |
5604 | else |
5618 | else |
5605 | return sc; |
5619 | return sc; |
5606 | } |
5620 | } |
5607 | #endif |
5621 | #endif |
5608 | |
5622 | |
5609 | int |
5623 | ecb_cold int |
5610 | scheme_init (SCHEME_P) |
5624 | scheme_init (SCHEME_P) |
5611 | { |
5625 | { |
5612 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5626 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5613 | pointer x; |
5627 | pointer x; |
|
|
5628 | |
|
|
5629 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
5630 | * that memset 0 will also set pointers to 0, but memset does |
|
|
5631 | * of course not guarantee that. screw such systems. |
|
|
5632 | */ |
|
|
5633 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5614 | |
5634 | |
5615 | num_set_fixnum (num_zero, 1); |
5635 | num_set_fixnum (num_zero, 1); |
5616 | num_set_ivalue (num_zero, 0); |
5636 | num_set_ivalue (num_zero, 0); |
5617 | num_set_fixnum (num_one, 1); |
5637 | num_set_fixnum (num_one, 1); |
5618 | num_set_ivalue (num_one, 1); |
5638 | num_set_ivalue (num_one, 1); |
… | |
… | |
5630 | SCHEME_V->save_inport = NIL; |
5650 | SCHEME_V->save_inport = NIL; |
5631 | SCHEME_V->loadport = NIL; |
5651 | SCHEME_V->loadport = NIL; |
5632 | SCHEME_V->nesting = 0; |
5652 | SCHEME_V->nesting = 0; |
5633 | SCHEME_V->interactive_repl = 0; |
5653 | SCHEME_V->interactive_repl = 0; |
5634 | |
5654 | |
5635 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5655 | if (!alloc_cellseg (SCHEME_A)) |
5636 | { |
5656 | { |
5637 | #if USE_ERROR_CHECKING |
5657 | #if USE_ERROR_CHECKING |
5638 | SCHEME_V->no_memory = 1; |
5658 | SCHEME_V->no_memory = 1; |
5639 | return 0; |
5659 | return 0; |
5640 | #endif |
5660 | #endif |
… | |
… | |
5739 | scheme_set_external_data (SCHEME_P_ void *p) |
5759 | scheme_set_external_data (SCHEME_P_ void *p) |
5740 | { |
5760 | { |
5741 | SCHEME_V->ext_data = p; |
5761 | SCHEME_V->ext_data = p; |
5742 | } |
5762 | } |
5743 | |
5763 | |
5744 | void |
5764 | ecb_cold void |
5745 | scheme_deinit (SCHEME_P) |
5765 | scheme_deinit (SCHEME_P) |
5746 | { |
5766 | { |
5747 | int i; |
5767 | int i; |
5748 | |
5768 | |
5749 | #if SHOW_ERROR_LINE |
5769 | #if SHOW_ERROR_LINE |
… | |
… | |
5970 | # endif |
5990 | # endif |
5971 | int fin; |
5991 | int fin; |
5972 | char *file_name = InitFile; |
5992 | char *file_name = InitFile; |
5973 | int retcode; |
5993 | int retcode; |
5974 | int isfile = 1; |
5994 | int isfile = 1; |
|
|
5995 | system ("ps v $PPID");//D |
5975 | |
5996 | |
5976 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5997 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5977 | { |
5998 | { |
5978 | xwrstr ("Usage: tinyscheme -?\n"); |
5999 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
5979 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6000 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
5980 | xwrstr ("followed by\n"); |
6001 | putstr (SCHEME_A_ "followed by\n"); |
5981 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6002 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
5982 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6003 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
5983 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6004 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
5984 | xwrstr ("Use - as filename for stdin.\n"); |
6005 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
5985 | return 1; |
6006 | return 1; |
5986 | } |
6007 | } |
5987 | |
6008 | |
5988 | if (!scheme_init (SCHEME_A)) |
6009 | if (!scheme_init (SCHEME_A)) |
5989 | { |
6010 | { |
5990 | xwrstr ("Could not initialize!\n"); |
6011 | putstr (SCHEME_A_ "Could not initialize!\n"); |
5991 | return 2; |
6012 | return 2; |
5992 | } |
6013 | } |
5993 | |
6014 | |
5994 | # if USE_PORTS |
6015 | # if USE_PORTS |
5995 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6016 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
6040 | fin = open (file_name, O_RDONLY); |
6061 | fin = open (file_name, O_RDONLY); |
6041 | #endif |
6062 | #endif |
6042 | |
6063 | |
6043 | if (isfile && fin < 0) |
6064 | if (isfile && fin < 0) |
6044 | { |
6065 | { |
6045 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6066 | putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
6046 | } |
6067 | } |
6047 | else |
6068 | else |
6048 | { |
6069 | { |
6049 | if (isfile) |
6070 | if (isfile) |
6050 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6071 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
… | |
… | |
6054 | #if USE_PORTS |
6075 | #if USE_PORTS |
6055 | if (!isfile || fin != STDIN_FILENO) |
6076 | if (!isfile || fin != STDIN_FILENO) |
6056 | { |
6077 | { |
6057 | if (SCHEME_V->retcode != 0) |
6078 | if (SCHEME_V->retcode != 0) |
6058 | { |
6079 | { |
6059 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6080 | putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
6060 | } |
6081 | } |
6061 | |
6082 | |
6062 | if (isfile) |
6083 | if (isfile) |
6063 | close (fin); |
6084 | close (fin); |
6064 | } |
6085 | } |