… | |
… | |
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 | INTERFACE INLINE int |
278 | INTERFACE int |
253 | is_vector (pointer p) |
279 | is_vector (pointer p) |
254 | { |
280 | { |
255 | return type (p) == T_VECTOR; |
281 | return type (p) == T_VECTOR; |
256 | } |
282 | } |
257 | |
283 | |
258 | #define vecvalue(p) ((p)->object.vector.vvalue) |
284 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
259 | #define veclength(p) ((p)->object.vector.length) |
285 | #define veclength(p) (CELL(p)->object.vector.length) |
260 | INTERFACE void fill_vector (pointer vec, pointer obj); |
286 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
261 | INTERFACE uint32_t vector_length (pointer vec); |
|
|
262 | INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); |
287 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
263 | INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); |
288 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
264 | |
289 | |
265 | INTERFACE uint32_t |
290 | INTERFACE int |
266 | vector_length (pointer vec) |
291 | is_integer (pointer p) |
267 | { |
292 | { |
268 | return vec->object.vector.length; |
293 | return type (p) == T_INTEGER; |
269 | } |
294 | } |
270 | |
295 | |
|
|
296 | /* not the same as in scheme, where integers are (correctly :) reals */ |
271 | INTERFACE INLINE int |
297 | INTERFACE int |
|
|
298 | is_real (pointer p) |
|
|
299 | { |
|
|
300 | return type (p) == T_REAL; |
|
|
301 | } |
|
|
302 | |
|
|
303 | INTERFACE int |
272 | is_number (pointer p) |
304 | is_number (pointer p) |
273 | { |
305 | { |
274 | return type (p) == T_NUMBER; |
306 | return is_integer (p) || is_real (p); |
275 | } |
307 | } |
276 | |
308 | |
277 | INTERFACE INLINE int |
309 | INTERFACE int |
278 | is_integer (pointer p) |
|
|
279 | { |
|
|
280 | if (!is_number (p)) |
|
|
281 | return 0; |
|
|
282 | |
|
|
283 | if (num_is_integer (p) || ivalue (p) == rvalue (p)) |
|
|
284 | return 1; |
|
|
285 | |
|
|
286 | return 0; |
|
|
287 | } |
|
|
288 | |
|
|
289 | INTERFACE INLINE int |
|
|
290 | is_real (pointer p) |
|
|
291 | { |
|
|
292 | return is_number (p) && !num_is_fixnum (p->object.number); |
|
|
293 | } |
|
|
294 | |
|
|
295 | INTERFACE INLINE int |
|
|
296 | is_character (pointer p) |
310 | is_character (pointer p) |
297 | { |
311 | { |
298 | return type (p) == T_CHARACTER; |
312 | return type (p) == T_CHARACTER; |
299 | } |
313 | } |
300 | |
314 | |
301 | INTERFACE INLINE char * |
315 | INTERFACE char * |
302 | string_value (pointer p) |
316 | string_value (pointer p) |
303 | { |
317 | { |
304 | return strvalue (p); |
318 | return strvalue (p); |
305 | } |
319 | } |
306 | |
320 | |
307 | INLINE num |
|
|
308 | nvalue (pointer p) |
|
|
309 | { |
|
|
310 | return (p)->object.number; |
|
|
311 | } |
|
|
312 | |
|
|
313 | static IVALUE |
|
|
314 | num_get_ivalue (const num n) |
|
|
315 | { |
|
|
316 | return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n); |
|
|
317 | } |
|
|
318 | |
|
|
319 | static RVALUE |
|
|
320 | num_get_rvalue (const num n) |
|
|
321 | { |
|
|
322 | return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n); |
|
|
323 | } |
|
|
324 | |
|
|
325 | INTERFACE IVALUE |
|
|
326 | ivalue (pointer p) |
|
|
327 | { |
|
|
328 | return num_get_ivalue (p->object.number); |
|
|
329 | } |
|
|
330 | |
|
|
331 | INTERFACE RVALUE |
|
|
332 | rvalue (pointer p) |
|
|
333 | { |
|
|
334 | return num_get_rvalue (p->object.number); |
|
|
335 | } |
|
|
336 | |
|
|
337 | #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 | |
338 | #if USE_REAL |
324 | #if USE_REAL |
339 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
325 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
340 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
326 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
341 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
|
|
342 | #else |
327 | #else |
343 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
328 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
344 | # define set_num_integer(p) 0 |
329 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
345 | # define set_num_real(p) 0 |
|
|
346 | #endif |
330 | #endif |
|
|
331 | |
347 | INTERFACE long |
332 | INTERFACE long |
348 | charvalue (pointer p) |
333 | charvalue (pointer p) |
349 | { |
334 | { |
350 | return ivalue_unchecked (p); |
335 | return ivalue_unchecked (p); |
351 | } |
336 | } |
352 | |
337 | |
|
|
338 | #define port(p) CELL(p)->object.port |
|
|
339 | #define set_port(p,v) port(p) = (v) |
353 | INTERFACE INLINE int |
340 | INTERFACE int |
354 | is_port (pointer p) |
341 | is_port (pointer p) |
355 | { |
342 | { |
356 | return type (p) == T_PORT; |
343 | return type (p) == T_PORT; |
357 | } |
344 | } |
358 | |
345 | |
359 | INTERFACE INLINE int |
346 | INTERFACE int |
360 | is_inport (pointer p) |
347 | is_inport (pointer p) |
361 | { |
348 | { |
362 | return is_port (p) && p->object.port->kind & port_input; |
349 | return is_port (p) && port (p)->kind & port_input; |
363 | } |
350 | } |
364 | |
351 | |
365 | INTERFACE INLINE int |
352 | INTERFACE int |
366 | is_outport (pointer p) |
353 | is_outport (pointer p) |
367 | { |
354 | { |
368 | return is_port (p) && p->object.port->kind & port_output; |
355 | return is_port (p) && port (p)->kind & port_output; |
369 | } |
356 | } |
370 | |
357 | |
371 | INTERFACE INLINE int |
358 | INTERFACE int |
372 | is_pair (pointer p) |
359 | is_pair (pointer p) |
373 | { |
360 | { |
374 | return type (p) == T_PAIR; |
361 | return type (p) == T_PAIR; |
375 | } |
362 | } |
376 | |
363 | |
377 | #define car(p) ((p)->object.cons.car + 0) |
364 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
378 | #define cdr(p) ((p)->object.cons.cdr + 0) |
365 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
379 | |
366 | |
380 | static pointer caar (pointer p) { return car (car (p)); } |
367 | static pointer caar (pointer p) { return car (car (p)); } |
381 | static pointer cadr (pointer p) { return car (cdr (p)); } |
368 | static pointer cadr (pointer p) { return car (cdr (p)); } |
382 | static pointer cdar (pointer p) { return cdr (car (p)); } |
369 | static pointer cdar (pointer p) { return cdr (car (p)); } |
383 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
370 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
… | |
… | |
387 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
374 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
388 | |
375 | |
389 | INTERFACE void |
376 | INTERFACE void |
390 | set_car (pointer p, pointer q) |
377 | set_car (pointer p, pointer q) |
391 | { |
378 | { |
392 | p->object.cons.car = q; |
379 | CELL(p)->object.cons.car = CELL (q); |
393 | } |
380 | } |
394 | |
381 | |
395 | INTERFACE void |
382 | INTERFACE void |
396 | set_cdr (pointer p, pointer q) |
383 | set_cdr (pointer p, pointer q) |
397 | { |
384 | { |
398 | p->object.cons.cdr = q; |
385 | CELL(p)->object.cons.cdr = CELL (q); |
399 | } |
386 | } |
400 | |
387 | |
401 | INTERFACE pointer |
388 | INTERFACE pointer |
402 | pair_car (pointer p) |
389 | pair_car (pointer p) |
403 | { |
390 | { |
… | |
… | |
408 | pair_cdr (pointer p) |
395 | pair_cdr (pointer p) |
409 | { |
396 | { |
410 | return cdr (p); |
397 | return cdr (p); |
411 | } |
398 | } |
412 | |
399 | |
413 | INTERFACE INLINE int |
400 | INTERFACE int |
414 | is_symbol (pointer p) |
401 | is_symbol (pointer p) |
415 | { |
402 | { |
416 | return type (p) == T_SYMBOL; |
403 | return type (p) == T_SYMBOL; |
417 | } |
404 | } |
418 | |
405 | |
419 | INTERFACE INLINE char * |
406 | INTERFACE char * |
420 | symname (pointer p) |
407 | symname (pointer p) |
421 | { |
408 | { |
422 | return strvalue (car (p)); |
409 | return strvalue (p); |
423 | } |
410 | } |
424 | |
411 | |
425 | #if USE_PLIST |
412 | #if USE_PLIST |
|
|
413 | #error plists are broken because symbols are no longer pairs |
|
|
414 | #define symprop(p) cdr(p) |
426 | SCHEME_EXPORT INLINE int |
415 | SCHEME_EXPORT int |
427 | hasprop (pointer p) |
416 | hasprop (pointer p) |
428 | { |
417 | { |
429 | return typeflag (p) & T_SYMBOL; |
418 | return typeflag (p) & T_SYMBOL; |
430 | } |
419 | } |
431 | |
|
|
432 | # define symprop(p) cdr(p) |
|
|
433 | #endif |
420 | #endif |
434 | |
421 | |
435 | INTERFACE INLINE int |
422 | INTERFACE int |
436 | is_syntax (pointer p) |
423 | is_syntax (pointer p) |
437 | { |
424 | { |
438 | return typeflag (p) & T_SYNTAX; |
425 | return typeflag (p) & T_SYNTAX; |
439 | } |
426 | } |
440 | |
427 | |
441 | INTERFACE INLINE int |
428 | INTERFACE int |
442 | is_proc (pointer p) |
429 | is_proc (pointer p) |
443 | { |
430 | { |
444 | return type (p) == T_PROC; |
431 | return type (p) == T_PROC; |
445 | } |
432 | } |
446 | |
433 | |
447 | INTERFACE INLINE int |
434 | INTERFACE int |
448 | is_foreign (pointer p) |
435 | is_foreign (pointer p) |
449 | { |
436 | { |
450 | return type (p) == T_FOREIGN; |
437 | return type (p) == T_FOREIGN; |
451 | } |
438 | } |
452 | |
439 | |
453 | INTERFACE INLINE char * |
440 | INTERFACE char * |
454 | syntaxname (pointer p) |
441 | syntaxname (pointer p) |
455 | { |
442 | { |
456 | return strvalue (car (p)); |
443 | return strvalue (p); |
457 | } |
444 | } |
458 | |
445 | |
459 | #define procnum(p) ivalue (p) |
446 | #define procnum(p) ivalue_unchecked (p) |
460 | static const char *procname (pointer x); |
447 | static const char *procname (pointer x); |
461 | |
448 | |
462 | INTERFACE INLINE int |
449 | INTERFACE int |
463 | is_closure (pointer p) |
450 | is_closure (pointer p) |
464 | { |
451 | { |
465 | return type (p) == T_CLOSURE; |
452 | return type (p) == T_CLOSURE; |
466 | } |
453 | } |
467 | |
454 | |
468 | INTERFACE INLINE int |
455 | INTERFACE int |
469 | is_macro (pointer p) |
456 | is_macro (pointer p) |
470 | { |
457 | { |
471 | return type (p) == T_MACRO; |
458 | return type (p) == T_MACRO; |
472 | } |
459 | } |
473 | |
460 | |
474 | INTERFACE INLINE pointer |
461 | INTERFACE pointer |
475 | closure_code (pointer p) |
462 | closure_code (pointer p) |
476 | { |
463 | { |
477 | return car (p); |
464 | return car (p); |
478 | } |
465 | } |
479 | |
466 | |
480 | INTERFACE INLINE pointer |
467 | INTERFACE pointer |
481 | closure_env (pointer p) |
468 | closure_env (pointer p) |
482 | { |
469 | { |
483 | return cdr (p); |
470 | return cdr (p); |
484 | } |
471 | } |
485 | |
472 | |
486 | INTERFACE INLINE int |
473 | INTERFACE int |
487 | is_continuation (pointer p) |
474 | is_continuation (pointer p) |
488 | { |
475 | { |
489 | return type (p) == T_CONTINUATION; |
476 | return type (p) == T_CONTINUATION; |
490 | } |
477 | } |
491 | |
478 | |
492 | #define cont_dump(p) cdr (p) |
479 | #define cont_dump(p) cdr (p) |
493 | #define set_cont_dump(p,v) set_cdr ((p), (v)) |
480 | #define set_cont_dump(p,v) set_cdr ((p), (v)) |
494 | |
481 | |
495 | /* To do: promise should be forced ONCE only */ |
482 | /* To do: promise should be forced ONCE only */ |
496 | INTERFACE INLINE int |
483 | INTERFACE int |
497 | is_promise (pointer p) |
484 | is_promise (pointer p) |
498 | { |
485 | { |
499 | return type (p) == T_PROMISE; |
486 | return type (p) == T_PROMISE; |
500 | } |
487 | } |
501 | |
488 | |
502 | INTERFACE INLINE int |
489 | INTERFACE int |
503 | is_environment (pointer p) |
490 | is_environment (pointer p) |
504 | { |
491 | { |
505 | return type (p) == T_ENVIRONMENT; |
492 | return type (p) == T_ENVIRONMENT; |
506 | } |
493 | } |
507 | |
494 | |
… | |
… | |
513 | |
500 | |
514 | #define is_mark(p) (typeflag (p) & T_MARK) |
501 | #define is_mark(p) (typeflag (p) & T_MARK) |
515 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
502 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
516 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
503 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
517 | |
504 | |
518 | INTERFACE INLINE int |
505 | INTERFACE int |
519 | is_immutable (pointer p) |
506 | is_immutable (pointer p) |
520 | { |
507 | { |
521 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
508 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
522 | } |
509 | } |
523 | |
510 | |
524 | INTERFACE INLINE void |
511 | INTERFACE void |
525 | setimmutable (pointer p) |
512 | setimmutable (pointer p) |
526 | { |
513 | { |
527 | #if USE_ERROR_CHECKING |
514 | #if USE_ERROR_CHECKING |
528 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
515 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
529 | #endif |
516 | #endif |
530 | } |
517 | } |
531 | |
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 | |
532 | #if USE_CHAR_CLASSIFIERS |
573 | #if USE_CHAR_CLASSIFIERS |
533 | static INLINE int |
574 | ecb_inline int |
534 | Cisalpha (int c) |
575 | Cisalpha (int c) |
535 | { |
576 | { |
536 | return isascii (c) && isalpha (c); |
577 | return isascii (c) && isalpha (c); |
537 | } |
578 | } |
538 | |
579 | |
539 | static INLINE int |
580 | ecb_inline int |
540 | Cisdigit (int c) |
581 | Cisdigit (int c) |
541 | { |
582 | { |
542 | return isascii (c) && isdigit (c); |
583 | return isascii (c) && isdigit (c); |
543 | } |
584 | } |
544 | |
585 | |
545 | static INLINE int |
586 | ecb_inline int |
546 | Cisspace (int c) |
587 | Cisspace (int c) |
547 | { |
588 | { |
548 | return isascii (c) && isspace (c); |
589 | return isascii (c) && isspace (c); |
549 | } |
590 | } |
550 | |
591 | |
551 | static INLINE int |
592 | ecb_inline int |
552 | Cisupper (int c) |
593 | Cisupper (int c) |
553 | { |
594 | { |
554 | return isascii (c) && isupper (c); |
595 | return isascii (c) && isupper (c); |
555 | } |
596 | } |
556 | |
597 | |
557 | static INLINE int |
598 | ecb_inline int |
558 | Cislower (int c) |
599 | Cislower (int c) |
559 | { |
600 | { |
560 | return isascii (c) && islower (c); |
601 | return isascii (c) && islower (c); |
561 | } |
602 | } |
562 | #endif |
603 | #endif |
… | |
… | |
623 | #endif |
664 | #endif |
624 | |
665 | |
625 | static int file_push (SCHEME_P_ const char *fname); |
666 | static int file_push (SCHEME_P_ const char *fname); |
626 | static void file_pop (SCHEME_P); |
667 | static void file_pop (SCHEME_P); |
627 | static int file_interactive (SCHEME_P); |
668 | static int file_interactive (SCHEME_P); |
628 | static INLINE int is_one_of (char *s, int c); |
669 | ecb_inline int is_one_of (const char *s, int c); |
629 | static int alloc_cellseg (SCHEME_P_ int n); |
670 | static int alloc_cellseg (SCHEME_P); |
630 | static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); |
671 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
631 | static void finalize_cell (SCHEME_P_ pointer a); |
672 | static void finalize_cell (SCHEME_P_ pointer a); |
632 | static int count_consecutive_cells (pointer x, int needed); |
673 | static int count_consecutive_cells (pointer x, int needed); |
633 | 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); |
634 | static pointer mk_number (SCHEME_P_ const num n); |
675 | static pointer mk_number (SCHEME_P_ const num n); |
635 | 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); |
… | |
… | |
650 | static void mark (pointer a); |
691 | static void mark (pointer a); |
651 | static void gc (SCHEME_P_ pointer a, pointer b); |
692 | static void gc (SCHEME_P_ pointer a, pointer b); |
652 | static int basic_inchar (port *pt); |
693 | static int basic_inchar (port *pt); |
653 | static int inchar (SCHEME_P); |
694 | static int inchar (SCHEME_P); |
654 | static void backchar (SCHEME_P_ int c); |
695 | static void backchar (SCHEME_P_ int c); |
655 | static char *readstr_upto (SCHEME_P_ char *delim); |
696 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
656 | static pointer readstrexp (SCHEME_P); |
697 | static pointer readstrexp (SCHEME_P_ char delim); |
657 | static INLINE int skipspace (SCHEME_P); |
698 | ecb_inline int skipspace (SCHEME_P); |
658 | static int token (SCHEME_P); |
699 | static int token (SCHEME_P); |
659 | static void printslashstring (SCHEME_P_ char *s, int len); |
700 | static void printslashstring (SCHEME_P_ char *s, int len); |
660 | 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); |
661 | static void printatom (SCHEME_P_ pointer l, int f); |
702 | static void printatom (SCHEME_P_ pointer l, int f); |
662 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
703 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
… | |
… | |
666 | static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); |
707 | static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); |
667 | static pointer revappend (SCHEME_P_ pointer a, pointer b); |
708 | static pointer revappend (SCHEME_P_ pointer a, pointer b); |
668 | static pointer ss_get_cont (SCHEME_P); |
709 | static pointer ss_get_cont (SCHEME_P); |
669 | static void ss_set_cont (SCHEME_P_ pointer cont); |
710 | static void ss_set_cont (SCHEME_P_ pointer cont); |
670 | static void dump_stack_mark (SCHEME_P); |
711 | static void dump_stack_mark (SCHEME_P); |
671 | static pointer opexe_0 (SCHEME_P_ enum scheme_opcodes op); |
712 | static int opexe_0 (SCHEME_P_ enum scheme_opcodes op); |
|
|
713 | static int opexe_1 (SCHEME_P_ enum scheme_opcodes op); |
672 | static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); |
714 | static int opexe_2 (SCHEME_P_ enum scheme_opcodes op); |
673 | static pointer opexe_r (SCHEME_P_ enum scheme_opcodes op); |
|
|
674 | static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); |
715 | static int opexe_3 (SCHEME_P_ enum scheme_opcodes op); |
675 | static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); |
716 | static int opexe_4 (SCHEME_P_ enum scheme_opcodes op); |
676 | static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); |
717 | static int opexe_5 (SCHEME_P_ enum scheme_opcodes op); |
677 | static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); |
718 | static int opexe_6 (SCHEME_P_ enum scheme_opcodes op); |
678 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
719 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
679 | static void assign_syntax (SCHEME_P_ const char *name); |
720 | static void assign_syntax (SCHEME_P_ const char *name); |
680 | static int syntaxnum (pointer p); |
721 | static int syntaxnum (pointer p); |
681 | 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); |
682 | |
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 | |
683 | static num |
751 | static num |
684 | num_op (enum num_op op, num a, num b) |
752 | num_op (enum num_op op, num a, num b) |
685 | { |
753 | { |
686 | num ret; |
754 | num ret; |
687 | |
755 | |
688 | 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)); |
689 | |
757 | |
690 | if (num_is_fixnum (ret)) |
758 | if (num_is_fixnum (ret)) |
691 | { |
759 | { |
692 | IVALUE av = num_get_ivalue (a); |
|
|
693 | IVALUE bv = num_get_ivalue (b); |
|
|
694 | |
|
|
695 | switch (op) |
760 | switch (op) |
696 | { |
761 | { |
697 | case NUM_ADD: av += bv; break; |
762 | case NUM_ADD: a.ivalue += b.ivalue; break; |
698 | case NUM_SUB: av -= bv; break; |
763 | case NUM_SUB: a.ivalue -= b.ivalue; break; |
699 | case NUM_MUL: av *= bv; break; |
764 | case NUM_MUL: a.ivalue *= b.ivalue; break; |
700 | case NUM_INTDIV: av /= bv; break; |
765 | case NUM_INTDIV: a.ivalue /= b.ivalue; break; |
701 | } |
766 | } |
702 | |
767 | |
703 | num_set_ivalue (ret, av); |
768 | num_set_ivalue (ret, a.ivalue); |
704 | } |
769 | } |
|
|
770 | #if USE_REAL |
705 | else |
771 | else |
706 | { |
772 | { |
707 | RVALUE av = num_get_rvalue (a); |
|
|
708 | RVALUE bv = num_get_rvalue (b); |
|
|
709 | |
|
|
710 | switch (op) |
773 | switch (op) |
711 | { |
774 | { |
712 | case NUM_ADD: av += bv; break; |
775 | case NUM_ADD: a.rvalue += b.rvalue; break; |
713 | case NUM_SUB: av -= bv; break; |
776 | case NUM_SUB: a.rvalue -= b.rvalue; break; |
714 | case NUM_MUL: av *= bv; break; |
777 | case NUM_MUL: a.rvalue *= b.rvalue; break; |
715 | case NUM_INTDIV: av /= bv; break; |
778 | case NUM_INTDIV: a.rvalue /= b.rvalue; break; |
716 | } |
779 | } |
717 | |
780 | |
718 | num_set_rvalue (ret, av); |
781 | num_set_rvalue (ret, a.rvalue); |
719 | } |
782 | } |
|
|
783 | #endif |
720 | |
784 | |
721 | return ret; |
785 | return ret; |
722 | } |
786 | } |
723 | |
787 | |
724 | static num |
788 | static num |
725 | num_div (num a, num b) |
789 | num_div (num a, num b) |
726 | { |
790 | { |
727 | num ret; |
791 | num ret; |
728 | |
792 | |
729 | 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); |
730 | |
794 | |
731 | if (num_is_fixnum (ret)) |
795 | if (num_is_fixnum (ret)) |
732 | num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); |
796 | num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b)); |
733 | else |
797 | else |
734 | num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); |
798 | num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); |
735 | |
799 | |
736 | return ret; |
800 | return ret; |
737 | } |
801 | } |
738 | |
802 | |
739 | static num |
803 | static num |
… | |
… | |
741 | { |
805 | { |
742 | num ret; |
806 | num ret; |
743 | long e1, e2, res; |
807 | long e1, e2, res; |
744 | |
808 | |
745 | 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)); |
746 | e1 = num_get_ivalue (a); |
810 | e1 = num_ivalue (a); |
747 | e2 = num_get_ivalue (b); |
811 | e2 = num_ivalue (b); |
748 | res = e1 % e2; |
812 | res = e1 % e2; |
749 | |
813 | |
750 | /* remainder should have same sign as second operand */ |
814 | /* remainder should have same sign as second operand */ |
751 | if (res > 0) |
815 | if (res > 0) |
752 | { |
816 | { |
… | |
… | |
768 | { |
832 | { |
769 | num ret; |
833 | num ret; |
770 | long e1, e2, res; |
834 | long e1, e2, res; |
771 | |
835 | |
772 | 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)); |
773 | e1 = num_get_ivalue (a); |
837 | e1 = num_ivalue (a); |
774 | e2 = num_get_ivalue (b); |
838 | e2 = num_ivalue (b); |
775 | res = e1 % e2; |
839 | res = e1 % e2; |
776 | |
840 | |
777 | /* modulo should have same sign as second operand */ |
841 | /* modulo should have same sign as second operand */ |
778 | if (res * e2 < 0) |
842 | if (res * e2 < 0) |
779 | res += e2; |
843 | res += e2; |
780 | |
844 | |
781 | num_set_ivalue (ret, res); |
845 | num_set_ivalue (ret, res); |
782 | return ret; |
846 | return ret; |
783 | } |
847 | } |
784 | |
848 | |
785 | /* this completely disrespects NaNs */ |
849 | /* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */ |
786 | static int |
850 | static int |
787 | num_cmp (num a, num b) |
851 | num_cmp (num a, num b) |
788 | { |
852 | { |
789 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
853 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
790 | int ret; |
854 | int ret; |
791 | |
855 | |
792 | if (is_fixnum) |
856 | if (is_fixnum) |
793 | { |
857 | { |
794 | IVALUE av = num_get_ivalue (a); |
858 | IVALUE av = num_ivalue (a); |
795 | IVALUE bv = num_get_ivalue (b); |
859 | IVALUE bv = num_ivalue (b); |
796 | |
860 | |
797 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
861 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
798 | } |
862 | } |
799 | else |
863 | else |
800 | { |
864 | { |
801 | RVALUE av = num_get_rvalue (a); |
865 | RVALUE av = num_rvalue (a); |
802 | RVALUE bv = num_get_rvalue (b); |
866 | RVALUE bv = num_rvalue (b); |
803 | |
867 | |
804 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
868 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
805 | } |
869 | } |
806 | |
870 | |
807 | return ret; |
871 | return ret; |
808 | } |
872 | } |
809 | |
|
|
810 | #if USE_MATH |
|
|
811 | |
|
|
812 | /* Round to nearest. Round to even if midway */ |
|
|
813 | static double |
|
|
814 | round_per_R5RS (double x) |
|
|
815 | { |
|
|
816 | double fl = floor (x); |
|
|
817 | double ce = ceil (x); |
|
|
818 | double dfl = x - fl; |
|
|
819 | double dce = ce - x; |
|
|
820 | |
|
|
821 | if (dfl > dce) |
|
|
822 | return ce; |
|
|
823 | else if (dfl < dce) |
|
|
824 | return fl; |
|
|
825 | else |
|
|
826 | { |
|
|
827 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
|
|
828 | return fl; |
|
|
829 | else |
|
|
830 | return ce; |
|
|
831 | } |
|
|
832 | } |
|
|
833 | #endif |
|
|
834 | |
873 | |
835 | static int |
874 | static int |
836 | is_zero_rvalue (RVALUE x) |
875 | is_zero_rvalue (RVALUE x) |
837 | { |
876 | { |
|
|
877 | return x == 0; |
|
|
878 | #if 0 |
838 | #if USE_REAL |
879 | #if USE_REAL |
839 | 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. */ |
840 | #else |
881 | #else |
841 | return x == 0; |
882 | return x == 0; |
842 | #endif |
883 | #endif |
|
|
884 | #endif |
843 | } |
885 | } |
844 | |
886 | |
845 | /* allocate new cell segment */ |
887 | /* allocate new cell segment */ |
846 | static int |
888 | static int |
847 | alloc_cellseg (SCHEME_P_ int n) |
889 | alloc_cellseg (SCHEME_P) |
848 | { |
890 | { |
849 | pointer newp; |
891 | struct cell *newp; |
850 | pointer last; |
892 | struct cell *last; |
851 | pointer p; |
893 | struct cell *p; |
852 | char *cp; |
894 | char *cp; |
853 | long i; |
895 | long i; |
854 | int k; |
896 | int k; |
855 | |
897 | |
856 | static int segsize = CELL_SEGSIZE >> 1; |
898 | static int segsize = CELL_SEGSIZE >> 1; |
857 | segsize <<= 1; |
899 | segsize <<= 1; |
858 | |
900 | |
859 | for (k = 0; k < n; k++) |
|
|
860 | { |
|
|
861 | if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1) |
|
|
862 | return k; |
|
|
863 | |
|
|
864 | cp = malloc (segsize * sizeof (struct cell)); |
901 | cp = malloc (segsize * sizeof (struct cell)); |
865 | |
902 | |
866 | if (!cp && USE_ERROR_CHECKING) |
903 | if (!cp && USE_ERROR_CHECKING) |
867 | return k; |
904 | return k; |
868 | |
905 | |
869 | i = ++SCHEME_V->last_cell_seg; |
906 | i = ++SCHEME_V->last_cell_seg; |
870 | SCHEME_V->alloc_seg[i] = cp; |
907 | SCHEME_V->alloc_seg[i] = cp; |
871 | |
908 | |
872 | /* insert new segment in address order */ |
909 | newp = (struct cell *)cp; |
873 | newp = (pointer)cp; |
|
|
874 | SCHEME_V->cell_seg[i] = newp; |
910 | SCHEME_V->cell_seg[i] = newp; |
875 | SCHEME_V->cell_segsize[i] = segsize; |
911 | SCHEME_V->cell_segsize[i] = segsize; |
876 | |
|
|
877 | //TODO: insert, not swap |
|
|
878 | while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i]) |
|
|
879 | { |
|
|
880 | p = SCHEME_V->cell_seg[i]; |
|
|
881 | SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1]; |
|
|
882 | SCHEME_V->cell_seg[i - 1] = p; |
|
|
883 | |
|
|
884 | k = SCHEME_V->cell_segsize[i]; |
|
|
885 | SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1]; |
|
|
886 | SCHEME_V->cell_segsize[i - 1] = k; |
|
|
887 | |
|
|
888 | --i; |
|
|
889 | } |
|
|
890 | |
|
|
891 | SCHEME_V->fcells += segsize; |
912 | SCHEME_V->fcells += segsize; |
892 | last = newp + segsize - 1; |
913 | last = newp + segsize - 1; |
893 | |
914 | |
894 | for (p = newp; p <= last; p++) |
915 | for (p = newp; p <= last; p++) |
895 | { |
916 | { |
|
|
917 | pointer cp = POINTER (p); |
896 | set_typeflag (p, T_FREE); |
918 | set_typeflag (cp, T_PAIR); |
897 | set_car (p, NIL); |
919 | set_car (cp, NIL); |
898 | set_cdr (p, p + 1); |
920 | set_cdr (cp, POINTER (p + 1)); |
899 | } |
921 | } |
900 | |
922 | |
901 | /* insert new cells in address order on free list */ |
|
|
902 | if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) |
|
|
903 | { |
|
|
904 | set_cdr (last, SCHEME_V->free_cell); |
923 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
905 | SCHEME_V->free_cell = newp; |
924 | SCHEME_V->free_cell = POINTER (newp); |
906 | } |
|
|
907 | else |
|
|
908 | { |
|
|
909 | p = SCHEME_V->free_cell; |
|
|
910 | |
925 | |
911 | while (cdr (p) != NIL && newp > cdr (p)) |
|
|
912 | p = cdr (p); |
|
|
913 | |
|
|
914 | set_cdr (last, cdr (p)); |
|
|
915 | set_cdr (p, newp); |
|
|
916 | } |
|
|
917 | } |
|
|
918 | |
|
|
919 | return n; |
926 | return 1; |
920 | } |
927 | } |
921 | |
928 | |
922 | /* get new cell. parameter a, b is marked by gc. */ |
929 | /* get new cell. parameter a, b is marked by gc. */ |
923 | static INLINE pointer |
930 | ecb_inline pointer |
924 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
931 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
925 | { |
932 | { |
926 | if (ecb_expect_false (SCHEME_V->free_cell == NIL)) |
933 | if (ecb_expect_false (SCHEME_V->free_cell == NIL)) |
927 | { |
934 | { |
928 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
935 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
929 | return S_SINK; |
936 | return S_SINK; |
930 | |
937 | |
931 | if (SCHEME_V->free_cell == NIL) |
938 | if (SCHEME_V->free_cell == NIL) |
932 | { |
939 | { |
933 | 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; |
934 | |
941 | |
935 | gc (SCHEME_A_ a, b); |
942 | gc (SCHEME_A_ a, b); |
936 | |
943 | |
937 | 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) |
938 | { |
945 | { |
939 | /* 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 */ |
940 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
947 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
941 | { |
948 | { |
942 | #if USE_ERROR_CHECKING |
949 | #if USE_ERROR_CHECKING |
943 | SCHEME_V->no_memory = 1; |
950 | SCHEME_V->no_memory = 1; |
944 | return S_SINK; |
951 | return S_SINK; |
945 | #endif |
952 | #endif |
… | |
… | |
957 | } |
964 | } |
958 | } |
965 | } |
959 | |
966 | |
960 | /* To retain recent allocs before interpreter knows about them - |
967 | /* To retain recent allocs before interpreter knows about them - |
961 | Tehom */ |
968 | Tehom */ |
962 | |
|
|
963 | static void |
969 | static void |
964 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
970 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
965 | { |
971 | { |
966 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
972 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
967 | |
973 | |
… | |
… | |
989 | } |
995 | } |
990 | |
996 | |
991 | static pointer |
997 | static pointer |
992 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
998 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
993 | { |
999 | { |
994 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1000 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
995 | pointer *e = malloc (len * sizeof (pointer)); |
1001 | pointer *e = malloc (len * sizeof (pointer)); |
996 | |
1002 | |
997 | if (!e && USE_ERROR_CHECKING) |
1003 | if (!e && USE_ERROR_CHECKING) |
998 | return S_SINK; |
1004 | return S_SINK; |
999 | |
1005 | |
1000 | /* Record it as a vector so that gc understands it. */ |
1006 | /* Record it as a vector so that gc understands it. */ |
1001 | set_typeflag (v, T_VECTOR | T_ATOM); |
1007 | set_typeflag (v, T_VECTOR | T_ATOM); |
1002 | |
1008 | |
1003 | v->object.vector.vvalue = e; |
1009 | CELL(v)->object.vector.vvalue = e; |
1004 | v->object.vector.length = len; |
1010 | CELL(v)->object.vector.length = len; |
1005 | fill_vector (v, init); |
1011 | fill_vector (v, 0, init); |
1006 | push_recent_alloc (SCHEME_A_ v, NIL); |
1012 | push_recent_alloc (SCHEME_A_ v, NIL); |
1007 | |
1013 | |
1008 | return v; |
1014 | return v; |
1009 | } |
1015 | } |
1010 | |
1016 | |
1011 | static INLINE void |
1017 | ecb_inline void |
1012 | ok_to_freely_gc (SCHEME_P) |
1018 | ok_to_freely_gc (SCHEME_P) |
1013 | { |
1019 | { |
1014 | set_car (S_SINK, NIL); |
1020 | set_car (S_SINK, NIL); |
1015 | } |
1021 | } |
1016 | |
1022 | |
… | |
… | |
1018 | static void |
1024 | static void |
1019 | check_cell_alloced (pointer p, int expect_alloced) |
1025 | check_cell_alloced (pointer p, int expect_alloced) |
1020 | { |
1026 | { |
1021 | /* 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. */ |
1022 | if (typeflag (p) & !expect_alloced) |
1028 | if (typeflag (p) & !expect_alloced) |
1023 | xwrstr ("Cell is already allocated!\n"); |
1029 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1024 | |
1030 | |
1025 | if (!(typeflag (p)) & expect_alloced) |
1031 | if (!(typeflag (p)) & expect_alloced) |
1026 | xwrstr ("Cell is not allocated!\n"); |
1032 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1027 | } |
1033 | } |
1028 | |
1034 | |
1029 | static void |
1035 | static void |
1030 | check_range_alloced (pointer p, int n, int expect_alloced) |
1036 | check_range_alloced (pointer p, int n, int expect_alloced) |
1031 | { |
1037 | { |
… | |
… | |
1053 | set_cdr (x, b); |
1059 | set_cdr (x, b); |
1054 | |
1060 | |
1055 | return x; |
1061 | return x; |
1056 | } |
1062 | } |
1057 | |
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 | |
1058 | /* ========== oblist implementation ========== */ |
1073 | /* ========== oblist implementation ========== */ |
1059 | |
1074 | |
1060 | #ifndef USE_OBJECT_LIST |
1075 | #ifndef USE_OBJECT_LIST |
1061 | |
1076 | |
|
|
1077 | static int |
1062 | 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 | } |
1063 | |
1088 | |
1064 | static pointer |
1089 | static pointer |
1065 | oblist_initial_value (SCHEME_P) |
1090 | oblist_initial_value (SCHEME_P) |
1066 | { |
1091 | { |
1067 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1092 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
… | |
… | |
1069 | |
1094 | |
1070 | /* returns the new symbol */ |
1095 | /* returns the new symbol */ |
1071 | static pointer |
1096 | static pointer |
1072 | oblist_add_by_name (SCHEME_P_ const char *name) |
1097 | oblist_add_by_name (SCHEME_P_ const char *name) |
1073 | { |
1098 | { |
1074 | int location; |
1099 | pointer x = generate_symbol (SCHEME_A_ name); |
1075 | |
|
|
1076 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1077 | set_typeflag (x, T_SYMBOL); |
|
|
1078 | setimmutable (car (x)); |
|
|
1079 | |
|
|
1080 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1100 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1081 | 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))); |
1082 | return x; |
1102 | return x; |
1083 | } |
1103 | } |
1084 | |
1104 | |
1085 | static INLINE pointer |
1105 | ecb_inline pointer |
1086 | oblist_find_by_name (SCHEME_P_ const char *name) |
1106 | oblist_find_by_name (SCHEME_P_ const char *name) |
1087 | { |
1107 | { |
1088 | int location; |
1108 | int location; |
1089 | pointer x; |
1109 | pointer x; |
1090 | char *s; |
1110 | char *s; |
1091 | |
1111 | |
1092 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1112 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1093 | |
1113 | |
1094 | 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)) |
1095 | { |
1115 | { |
1096 | s = symname (car (x)); |
1116 | s = symname (car (x)); |
1097 | |
1117 | |
1098 | /* case-insensitive, per R5RS section 2 */ |
1118 | /* case-insensitive, per R5RS section 2 */ |
1099 | if (stricmp (name, s) == 0) |
1119 | if (stricmp (name, s) == 0) |
… | |
… | |
1109 | int i; |
1129 | int i; |
1110 | pointer x; |
1130 | pointer x; |
1111 | pointer ob_list = NIL; |
1131 | pointer ob_list = NIL; |
1112 | |
1132 | |
1113 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1133 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1114 | 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)) |
1115 | ob_list = cons (x, ob_list); |
1135 | ob_list = cons (x, ob_list); |
1116 | |
1136 | |
1117 | return ob_list; |
1137 | return ob_list; |
1118 | } |
1138 | } |
1119 | |
1139 | |
… | |
… | |
1123 | oblist_initial_value (SCHEME_P) |
1143 | oblist_initial_value (SCHEME_P) |
1124 | { |
1144 | { |
1125 | return NIL; |
1145 | return NIL; |
1126 | } |
1146 | } |
1127 | |
1147 | |
1128 | static INLINE pointer |
1148 | ecb_inline pointer |
1129 | oblist_find_by_name (SCHEME_P_ const char *name) |
1149 | oblist_find_by_name (SCHEME_P_ const char *name) |
1130 | { |
1150 | { |
1131 | pointer x; |
1151 | pointer x; |
1132 | char *s; |
1152 | char *s; |
1133 | |
1153 | |
… | |
… | |
1145 | |
1165 | |
1146 | /* returns the new symbol */ |
1166 | /* returns the new symbol */ |
1147 | static pointer |
1167 | static pointer |
1148 | oblist_add_by_name (SCHEME_P_ const char *name) |
1168 | oblist_add_by_name (SCHEME_P_ const char *name) |
1149 | { |
1169 | { |
1150 | pointer x; |
1170 | pointer x = generate_symbol (SCHEME_A_ name); |
1151 | |
|
|
1152 | x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1153 | set_typeflag (x, T_SYMBOL); |
|
|
1154 | setimmutable (car (x)); |
|
|
1155 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1171 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1156 | return x; |
1172 | return x; |
1157 | } |
1173 | } |
1158 | |
1174 | |
1159 | static pointer |
1175 | static pointer |
… | |
… | |
1169 | mk_port (SCHEME_P_ port *p) |
1185 | mk_port (SCHEME_P_ port *p) |
1170 | { |
1186 | { |
1171 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1187 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1172 | |
1188 | |
1173 | set_typeflag (x, T_PORT | T_ATOM); |
1189 | set_typeflag (x, T_PORT | T_ATOM); |
1174 | x->object.port = p; |
1190 | set_port (x, p); |
1175 | |
1191 | |
1176 | return x; |
1192 | return x; |
1177 | } |
1193 | } |
1178 | #endif |
1194 | #endif |
1179 | |
1195 | |
1180 | pointer |
1196 | pointer |
1181 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1197 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1182 | { |
1198 | { |
1183 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1199 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1184 | |
1200 | |
1185 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1201 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1186 | x->object.ff = f; |
1202 | CELL(x)->object.ff = f; |
1187 | |
1203 | |
1188 | return x; |
1204 | return x; |
1189 | } |
1205 | } |
1190 | |
1206 | |
1191 | INTERFACE pointer |
1207 | INTERFACE pointer |
1192 | mk_character (SCHEME_P_ int c) |
1208 | mk_character (SCHEME_P_ int c) |
1193 | { |
1209 | { |
1194 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1210 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1195 | |
1211 | |
1196 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1212 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1197 | ivalue_unchecked (x) = c & 0xff; |
1213 | set_ivalue (x, c & 0xff); |
1198 | set_num_integer (x); |
1214 | |
1199 | return x; |
1215 | return x; |
1200 | } |
1216 | } |
1201 | |
1217 | |
1202 | /* get number atom (integer) */ |
1218 | /* get number atom (integer) */ |
1203 | INTERFACE pointer |
1219 | INTERFACE pointer |
1204 | mk_integer (SCHEME_P_ long num) |
1220 | mk_integer (SCHEME_P_ long n) |
1205 | { |
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 | { |
1206 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1232 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1207 | |
1233 | |
1208 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1234 | set_typeflag (x, T_INTEGER | T_ATOM); |
1209 | ivalue_unchecked (x) = num; |
1235 | setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */ |
1210 | set_num_integer (x); |
1236 | set_ivalue (x, n); |
|
|
1237 | |
|
|
1238 | *pp = x; |
|
|
1239 | } |
|
|
1240 | |
1211 | return x; |
1241 | return *pp; |
1212 | } |
1242 | } |
1213 | |
1243 | |
1214 | INTERFACE pointer |
1244 | INTERFACE pointer |
1215 | mk_real (SCHEME_P_ RVALUE n) |
1245 | mk_real (SCHEME_P_ RVALUE n) |
1216 | { |
1246 | { |
|
|
1247 | #if USE_REAL |
1217 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1248 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1218 | |
1249 | |
1219 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1250 | set_typeflag (x, T_REAL | T_ATOM); |
1220 | rvalue_unchecked (x) = n; |
1251 | set_rvalue (x, n); |
1221 | set_num_real (x); |
1252 | |
1222 | return x; |
1253 | return x; |
|
|
1254 | #else |
|
|
1255 | return mk_integer (SCHEME_A_ n); |
|
|
1256 | #endif |
1223 | } |
1257 | } |
1224 | |
1258 | |
1225 | static pointer |
1259 | static pointer |
1226 | mk_number (SCHEME_P_ const num n) |
1260 | mk_number (SCHEME_P_ const num n) |
1227 | { |
1261 | { |
|
|
1262 | #if USE_REAL |
1228 | 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 |
1229 | return mk_integer (SCHEME_A_ num_get_ivalue (n)); |
1267 | return mk_integer (SCHEME_A_ num_ivalue (n)); |
1230 | else |
1268 | #endif |
1231 | return mk_real (SCHEME_A_ num_get_rvalue (n)); |
|
|
1232 | } |
1269 | } |
1233 | |
1270 | |
1234 | /* allocate name to string area */ |
1271 | /* allocate name to string area */ |
1235 | static char * |
1272 | static char * |
1236 | 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) |
… | |
… | |
1242 | SCHEME_V->no_memory = 1; |
1279 | SCHEME_V->no_memory = 1; |
1243 | return SCHEME_V->strbuff; |
1280 | return SCHEME_V->strbuff; |
1244 | } |
1281 | } |
1245 | |
1282 | |
1246 | if (str) |
1283 | if (str) |
1247 | { |
1284 | memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */ |
1248 | int l = strlen (str); |
|
|
1249 | |
|
|
1250 | if (l > len_str) |
|
|
1251 | l = len_str; |
|
|
1252 | |
|
|
1253 | memcpy (q, str, l); |
|
|
1254 | q[l] = 0; |
|
|
1255 | } |
|
|
1256 | else |
1285 | else |
1257 | { |
|
|
1258 | memset (q, fill, len_str); |
1286 | memset (q, fill, len_str); |
|
|
1287 | |
1259 | q[len_str] = 0; |
1288 | q[len_str] = 0; |
1260 | } |
|
|
1261 | |
1289 | |
1262 | return q; |
1290 | return q; |
1263 | } |
1291 | } |
1264 | |
1292 | |
1265 | INTERFACE pointer |
1293 | INTERFACE pointer |
… | |
… | |
1279 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1307 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1280 | |
1308 | |
1281 | set_typeflag (x, T_STRING | T_ATOM); |
1309 | set_typeflag (x, T_STRING | T_ATOM); |
1282 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1310 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1283 | strlength (x) = len; |
1311 | strlength (x) = len; |
|
|
1312 | |
1284 | return x; |
1313 | return x; |
1285 | } |
1314 | } |
1286 | |
1315 | |
1287 | INTERFACE pointer |
1316 | INTERFACE pointer |
1288 | mk_string (SCHEME_P_ const char *str) |
1317 | mk_string (SCHEME_P_ const char *str) |
… | |
… | |
1295 | { |
1324 | { |
1296 | return get_vector_object (SCHEME_A_ len, NIL); |
1325 | return get_vector_object (SCHEME_A_ len, NIL); |
1297 | } |
1326 | } |
1298 | |
1327 | |
1299 | INTERFACE void |
1328 | INTERFACE void |
1300 | fill_vector (pointer vec, pointer obj) |
1329 | fill_vector (pointer vec, uint32_t start, pointer obj) |
1301 | { |
1330 | { |
1302 | int i; |
1331 | int i; |
1303 | |
1332 | |
1304 | for (i = 0; i < vec->object.vector.length; i++) |
1333 | for (i = start; i < veclength (vec); i++) |
1305 | vecvalue (vec)[i] = obj; |
1334 | vecvalue (vec)[i] = obj; |
1306 | } |
1335 | } |
1307 | |
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 | |
1308 | INTERFACE pointer |
1346 | INTERFACE pointer |
1309 | vector_elem (pointer vec, uint32_t ielem) |
1347 | vector_get (pointer vec, uint32_t ielem) |
1310 | { |
1348 | { |
1311 | return vecvalue(vec)[ielem]; |
1349 | return vecvalue(vec)[ielem]; |
1312 | } |
1350 | } |
1313 | |
1351 | |
1314 | INTERFACE void |
1352 | INTERFACE void |
1315 | set_vector_elem (pointer vec, uint32_t ielem, pointer a) |
1353 | vector_set (pointer vec, uint32_t ielem, pointer a) |
1316 | { |
1354 | { |
1317 | vecvalue(vec)[ielem] = a; |
1355 | vecvalue(vec)[ielem] = a; |
1318 | } |
1356 | } |
1319 | |
1357 | |
1320 | /* get new symbol */ |
1358 | /* get new symbol */ |
… | |
… | |
1332 | |
1370 | |
1333 | INTERFACE pointer |
1371 | INTERFACE pointer |
1334 | gensym (SCHEME_P) |
1372 | gensym (SCHEME_P) |
1335 | { |
1373 | { |
1336 | pointer x; |
1374 | pointer x; |
1337 | |
|
|
1338 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
|
|
1339 | { |
|
|
1340 | char name[40] = "gensym-"; |
1375 | char name[40] = "gensym-"; |
1341 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1376 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1342 | |
1377 | |
1343 | /* first check oblist */ |
1378 | return generate_symbol (SCHEME_A_ name); |
1344 | x = oblist_find_by_name (SCHEME_A_ name); |
1379 | } |
1345 | |
1380 | |
1346 | if (x == NIL) |
1381 | static int |
1347 | { |
1382 | is_gensym (SCHEME_P_ pointer x) |
1348 | x = oblist_add_by_name (SCHEME_A_ name); |
1383 | { |
1349 | return x; |
1384 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1350 | } |
|
|
1351 | } |
|
|
1352 | |
|
|
1353 | return NIL; |
|
|
1354 | } |
1385 | } |
1355 | |
1386 | |
1356 | /* make symbol or number atom from string */ |
1387 | /* make symbol or number atom from string */ |
1357 | static pointer |
1388 | static pointer |
1358 | mk_atom (SCHEME_P_ char *q) |
1389 | mk_atom (SCHEME_P_ char *q) |
… | |
… | |
1412 | } |
1443 | } |
1413 | else if ((c == 'e') || (c == 'E')) |
1444 | else if ((c == 'e') || (c == 'E')) |
1414 | { |
1445 | { |
1415 | if (!has_fp_exp) |
1446 | if (!has_fp_exp) |
1416 | { |
1447 | { |
1417 | has_dec_point = 1; /* decimal point illegal |
1448 | has_dec_point = 1; /* decimal point illegal from now on */ |
1418 | from now on */ |
|
|
1419 | p++; |
1449 | p++; |
1420 | |
1450 | |
1421 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1451 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1422 | continue; |
1452 | continue; |
1423 | } |
1453 | } |
… | |
… | |
1445 | return S_F; |
1475 | return S_F; |
1446 | else if (*name == '\\') /* #\w (character) */ |
1476 | else if (*name == '\\') /* #\w (character) */ |
1447 | { |
1477 | { |
1448 | int c; |
1478 | int c; |
1449 | |
1479 | |
|
|
1480 | // TODO: optimise |
1450 | if (stricmp (name + 1, "space") == 0) |
1481 | if (stricmp (name + 1, "space") == 0) |
1451 | c = ' '; |
1482 | c = ' '; |
1452 | else if (stricmp (name + 1, "newline") == 0) |
1483 | else if (stricmp (name + 1, "newline") == 0) |
1453 | c = '\n'; |
1484 | c = '\n'; |
1454 | else if (stricmp (name + 1, "return") == 0) |
1485 | else if (stricmp (name + 1, "return") == 0) |
1455 | c = '\r'; |
1486 | c = '\r'; |
1456 | else if (stricmp (name + 1, "tab") == 0) |
1487 | else if (stricmp (name + 1, "tab") == 0) |
1457 | c = '\t'; |
1488 | c = '\t'; |
|
|
1489 | else if (stricmp (name + 1, "alarm") == 0) |
|
|
1490 | c = 0x07; |
|
|
1491 | else if (stricmp (name + 1, "backspace") == 0) |
|
|
1492 | c = 0x08; |
|
|
1493 | else if (stricmp (name + 1, "escape") == 0) |
|
|
1494 | c = 0x1b; |
|
|
1495 | else if (stricmp (name + 1, "delete") == 0) |
|
|
1496 | c = 0x7f; |
|
|
1497 | else if (stricmp (name + 1, "null") == 0) |
|
|
1498 | c = 0; |
1458 | else if (name[1] == 'x' && name[2] != 0) |
1499 | else if (name[1] == 'x' && name[2] != 0) |
1459 | { |
1500 | { |
1460 | long c1 = strtol (name + 2, 0, 16); |
1501 | long c1 = strtol (name + 2, 0, 16); |
1461 | |
1502 | |
1462 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1503 | if (0 <= c1 && c1 <= UCHAR_MAX) |
… | |
… | |
1511 | |
1552 | |
1512 | if (ecb_expect_false (is_vector (p))) |
1553 | if (ecb_expect_false (is_vector (p))) |
1513 | { |
1554 | { |
1514 | int i; |
1555 | int i; |
1515 | |
1556 | |
1516 | for (i = 0; i < p->object.vector.length; i++) |
1557 | for (i = 0; i < veclength (p); i++) |
1517 | mark (vecvalue (p)[i]); |
1558 | mark (vecvalue (p)[i]); |
1518 | } |
1559 | } |
1519 | |
1560 | |
1520 | if (is_atom (p)) |
1561 | if (is_atom (p)) |
1521 | goto E6; |
1562 | goto E6; |
… | |
… | |
1568 | |
1609 | |
1569 | /* garbage collection. parameter a, b is marked. */ |
1610 | /* garbage collection. parameter a, b is marked. */ |
1570 | static void |
1611 | static void |
1571 | gc (SCHEME_P_ pointer a, pointer b) |
1612 | gc (SCHEME_P_ pointer a, pointer b) |
1572 | { |
1613 | { |
1573 | pointer p; |
|
|
1574 | int i; |
1614 | int i; |
1575 | |
1615 | |
1576 | if (SCHEME_V->gc_verbose) |
1616 | if (SCHEME_V->gc_verbose) |
1577 | putstr (SCHEME_A_ "gc..."); |
1617 | putstr (SCHEME_A_ "gc..."); |
1578 | |
1618 | |
… | |
… | |
1594 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1634 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1595 | mark (car (S_SINK)); |
1635 | mark (car (S_SINK)); |
1596 | /* Mark any older stuff above nested C calls */ |
1636 | /* Mark any older stuff above nested C calls */ |
1597 | mark (SCHEME_V->c_nest); |
1637 | mark (SCHEME_V->c_nest); |
1598 | |
1638 | |
|
|
1639 | #if USE_INTCACHE |
|
|
1640 | /* mark intcache */ |
|
|
1641 | for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i) |
|
|
1642 | if (SCHEME_V->intcache[i - INTCACHE_MIN]) |
|
|
1643 | mark (SCHEME_V->intcache[i - INTCACHE_MIN]); |
|
|
1644 | #endif |
|
|
1645 | |
1599 | /* mark variables a, b */ |
1646 | /* mark variables a, b */ |
1600 | mark (a); |
1647 | mark (a); |
1601 | mark (b); |
1648 | mark (b); |
1602 | |
1649 | |
1603 | /* garbage collect */ |
1650 | /* garbage collect */ |
1604 | clrmark (NIL); |
1651 | clrmark (NIL); |
1605 | SCHEME_V->fcells = 0; |
1652 | SCHEME_V->fcells = 0; |
1606 | SCHEME_V->free_cell = NIL; |
1653 | SCHEME_V->free_cell = NIL; |
1607 | |
1654 | |
1608 | /* free-list is kept sorted by address so as to maintain consecutive |
1655 | if (SCHEME_V->gc_verbose) |
1609 | ranges, if possible, for use with vectors. Here we scan the cells |
1656 | putstr (SCHEME_A_ "freeing..."); |
1610 | (which are also kept sorted by address) downwards to build the |
1657 | |
1611 | free-list in sorted order. |
1658 | uint32_t total = 0; |
1612 | */ |
1659 | |
|
|
1660 | /* Here we scan the cells to build the free-list. */ |
1613 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1661 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1614 | { |
1662 | { |
1615 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
1663 | struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1664 | struct cell *p; |
|
|
1665 | total += SCHEME_V->cell_segsize [i]; |
1616 | |
1666 | |
1617 | while (--p >= SCHEME_V->cell_seg[i]) |
1667 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
1618 | { |
1668 | { |
|
|
1669 | pointer c = POINTER (p); |
|
|
1670 | |
1619 | if (is_mark (p)) |
1671 | if (is_mark (c)) |
1620 | clrmark (p); |
1672 | clrmark (c); |
1621 | else |
1673 | else |
1622 | { |
1674 | { |
1623 | /* reclaim cell */ |
1675 | /* reclaim cell */ |
1624 | if (typeflag (p) != T_FREE) |
1676 | if (typeflag (c) != T_PAIR) |
1625 | { |
1677 | { |
1626 | finalize_cell (SCHEME_A_ p); |
1678 | finalize_cell (SCHEME_A_ c); |
1627 | set_typeflag (p, T_FREE); |
1679 | set_typeflag (c, T_PAIR); |
1628 | set_car (p, NIL); |
1680 | set_car (c, NIL); |
1629 | } |
1681 | } |
1630 | |
1682 | |
1631 | ++SCHEME_V->fcells; |
1683 | ++SCHEME_V->fcells; |
1632 | set_cdr (p, SCHEME_V->free_cell); |
1684 | set_cdr (c, SCHEME_V->free_cell); |
1633 | SCHEME_V->free_cell = p; |
1685 | SCHEME_V->free_cell = c; |
1634 | } |
1686 | } |
1635 | } |
1687 | } |
1636 | } |
1688 | } |
1637 | |
1689 | |
1638 | if (SCHEME_V->gc_verbose) |
1690 | if (SCHEME_V->gc_verbose) |
1639 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
1691 | { |
|
|
1692 | 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"); |
|
|
1693 | } |
1640 | } |
1694 | } |
1641 | |
1695 | |
1642 | static void |
1696 | static void |
1643 | finalize_cell (SCHEME_P_ pointer a) |
1697 | finalize_cell (SCHEME_P_ pointer a) |
1644 | { |
1698 | { |
1645 | /* TODO, fast bitmap check? */ |
1699 | /* TODO, fast bitmap check? */ |
1646 | if (is_string (a)) |
1700 | if (is_string (a) || is_symbol (a)) |
1647 | free (strvalue (a)); |
1701 | free (strvalue (a)); |
1648 | else if (is_vector (a)) |
1702 | else if (is_vector (a)) |
1649 | free (vecvalue (a)); |
1703 | free (vecvalue (a)); |
1650 | #if USE_PORTS |
1704 | #if USE_PORTS |
1651 | else if (is_port (a)) |
1705 | else if (is_port (a)) |
1652 | { |
1706 | { |
1653 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
1707 | if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) |
1654 | port_close (SCHEME_A_ a, port_input | port_output); |
1708 | port_close (SCHEME_A_ a, port_input | port_output); |
1655 | |
1709 | |
1656 | free (a->object.port); |
1710 | free (port (a)); |
1657 | } |
1711 | } |
1658 | #endif |
1712 | #endif |
1659 | } |
1713 | } |
1660 | |
1714 | |
1661 | /* ========== Routines for Reading ========== */ |
1715 | /* ========== Routines for Reading ========== */ |
… | |
… | |
1677 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1731 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1678 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1732 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1679 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1733 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1680 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1734 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1681 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1735 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1682 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1736 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1683 | |
1737 | |
1684 | #if SHOW_ERROR_LINE |
1738 | #if SHOW_ERROR_LINE |
1685 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1739 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1686 | |
1740 | |
1687 | if (fname) |
1741 | if (fname) |
… | |
… | |
1704 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1758 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1705 | #if USE_PORTS |
1759 | #if USE_PORTS |
1706 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1760 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1707 | #endif |
1761 | #endif |
1708 | SCHEME_V->file_i--; |
1762 | SCHEME_V->file_i--; |
1709 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1763 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1710 | } |
1764 | } |
1711 | } |
1765 | } |
1712 | |
1766 | |
1713 | static int |
1767 | static int |
1714 | file_interactive (SCHEME_P) |
1768 | file_interactive (SCHEME_P) |
1715 | { |
1769 | { |
1716 | #if USE_PORTS |
1770 | #if USE_PORTS |
1717 | return SCHEME_V->file_i == 0 |
1771 | return SCHEME_V->file_i == 0 |
1718 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1772 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1719 | && (SCHEME_V->inport->object.port->kind & port_file); |
1773 | && (port (SCHEME_V->inport)->kind & port_file); |
1720 | #else |
1774 | #else |
1721 | return 0; |
1775 | return 0; |
1722 | #endif |
1776 | #endif |
1723 | } |
1777 | } |
1724 | |
1778 | |
… | |
… | |
1858 | } |
1912 | } |
1859 | |
1913 | |
1860 | static void |
1914 | static void |
1861 | port_close (SCHEME_P_ pointer p, int flag) |
1915 | port_close (SCHEME_P_ pointer p, int flag) |
1862 | { |
1916 | { |
1863 | port *pt = p->object.port; |
1917 | port *pt = port (p); |
1864 | |
1918 | |
1865 | pt->kind &= ~flag; |
1919 | pt->kind &= ~flag; |
1866 | |
1920 | |
1867 | if ((pt->kind & (port_input | port_output)) == 0) |
1921 | if ((pt->kind & (port_input | port_output)) == 0) |
1868 | { |
1922 | { |
… | |
… | |
1889 | /* get new character from input file */ |
1943 | /* get new character from input file */ |
1890 | static int |
1944 | static int |
1891 | inchar (SCHEME_P) |
1945 | inchar (SCHEME_P) |
1892 | { |
1946 | { |
1893 | int c; |
1947 | int c; |
1894 | port *pt; |
1948 | port *pt = port (SCHEME_V->inport); |
1895 | |
|
|
1896 | pt = SCHEME_V->inport->object.port; |
|
|
1897 | |
1949 | |
1898 | if (pt->kind & port_saw_EOF) |
1950 | if (pt->kind & port_saw_EOF) |
1899 | return EOF; |
1951 | return EOF; |
1900 | |
1952 | |
1901 | c = basic_inchar (pt); |
1953 | c = basic_inchar (pt); |
… | |
… | |
1968 | port *pt; |
2020 | port *pt; |
1969 | |
2021 | |
1970 | if (c == EOF) |
2022 | if (c == EOF) |
1971 | return; |
2023 | return; |
1972 | |
2024 | |
1973 | pt = SCHEME_V->inport->object.port; |
2025 | pt = port (SCHEME_V->inport); |
1974 | pt->unget = c; |
2026 | pt->unget = c; |
1975 | #else |
2027 | #else |
1976 | if (c == EOF) |
2028 | if (c == EOF) |
1977 | return; |
2029 | return; |
1978 | |
2030 | |
… | |
… | |
2006 | |
2058 | |
2007 | INTERFACE void |
2059 | INTERFACE void |
2008 | putstr (SCHEME_P_ const char *s) |
2060 | putstr (SCHEME_P_ const char *s) |
2009 | { |
2061 | { |
2010 | #if USE_PORTS |
2062 | #if USE_PORTS |
2011 | port *pt = SCHEME_V->outport->object.port; |
2063 | port *pt = port (SCHEME_V->outport); |
2012 | |
2064 | |
2013 | if (pt->kind & port_file) |
2065 | if (pt->kind & port_file) |
2014 | write (pt->rep.stdio.file, s, strlen (s)); |
2066 | write (pt->rep.stdio.file, s, strlen (s)); |
2015 | else |
2067 | else |
2016 | for (; *s; s++) |
2068 | for (; *s; s++) |
… | |
… | |
2018 | *pt->rep.string.curr++ = *s; |
2070 | *pt->rep.string.curr++ = *s; |
2019 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2071 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2020 | *pt->rep.string.curr++ = *s; |
2072 | *pt->rep.string.curr++ = *s; |
2021 | |
2073 | |
2022 | #else |
2074 | #else |
2023 | xwrstr (s); |
2075 | write (pt->rep.stdio.file, s, strlen (s)); |
2024 | #endif |
2076 | #endif |
2025 | } |
2077 | } |
2026 | |
2078 | |
2027 | static void |
2079 | static void |
2028 | putchars (SCHEME_P_ const char *s, int len) |
2080 | putchars (SCHEME_P_ const char *s, int len) |
2029 | { |
2081 | { |
2030 | #if USE_PORTS |
2082 | #if USE_PORTS |
2031 | port *pt = SCHEME_V->outport->object.port; |
2083 | port *pt = port (SCHEME_V->outport); |
2032 | |
2084 | |
2033 | if (pt->kind & port_file) |
2085 | if (pt->kind & port_file) |
2034 | write (pt->rep.stdio.file, s, len); |
2086 | write (pt->rep.stdio.file, s, len); |
2035 | else |
2087 | else |
2036 | { |
2088 | { |
… | |
… | |
2050 | |
2102 | |
2051 | INTERFACE void |
2103 | INTERFACE void |
2052 | putcharacter (SCHEME_P_ int c) |
2104 | putcharacter (SCHEME_P_ int c) |
2053 | { |
2105 | { |
2054 | #if USE_PORTS |
2106 | #if USE_PORTS |
2055 | port *pt = SCHEME_V->outport->object.port; |
2107 | port *pt = port (SCHEME_V->outport); |
2056 | |
2108 | |
2057 | if (pt->kind & port_file) |
2109 | if (pt->kind & port_file) |
2058 | { |
2110 | { |
2059 | char cc = c; |
2111 | char cc = c; |
2060 | write (pt->rep.stdio.file, &cc, 1); |
2112 | write (pt->rep.stdio.file, &cc, 1); |
… | |
… | |
2073 | #endif |
2125 | #endif |
2074 | } |
2126 | } |
2075 | |
2127 | |
2076 | /* read characters up to delimiter, but cater to character constants */ |
2128 | /* read characters up to delimiter, but cater to character constants */ |
2077 | static char * |
2129 | static char * |
2078 | readstr_upto (SCHEME_P_ char *delim) |
2130 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2079 | { |
2131 | { |
2080 | char *p = SCHEME_V->strbuff; |
2132 | char *p = SCHEME_V->strbuff + skip; |
2081 | |
2133 | |
2082 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2134 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2083 | |
2135 | |
2084 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2136 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2085 | *p = 0; |
2137 | *p = 0; |
… | |
… | |
2092 | return SCHEME_V->strbuff; |
2144 | return SCHEME_V->strbuff; |
2093 | } |
2145 | } |
2094 | |
2146 | |
2095 | /* read string expression "xxx...xxx" */ |
2147 | /* read string expression "xxx...xxx" */ |
2096 | static pointer |
2148 | static pointer |
2097 | readstrexp (SCHEME_P) |
2149 | readstrexp (SCHEME_P_ char delim) |
2098 | { |
2150 | { |
2099 | char *p = SCHEME_V->strbuff; |
2151 | char *p = SCHEME_V->strbuff; |
2100 | int c; |
2152 | int c; |
2101 | int c1 = 0; |
2153 | int c1 = 0; |
2102 | enum |
|
|
2103 | { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2154 | enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2104 | |
2155 | |
2105 | for (;;) |
2156 | for (;;) |
2106 | { |
2157 | { |
2107 | c = inchar (SCHEME_A); |
2158 | c = inchar (SCHEME_A); |
2108 | |
2159 | |
… | |
… | |
2110 | return S_F; |
2161 | return S_F; |
2111 | |
2162 | |
2112 | switch (state) |
2163 | switch (state) |
2113 | { |
2164 | { |
2114 | case st_ok: |
2165 | case st_ok: |
2115 | switch (c) |
2166 | if (ecb_expect_false (c == delim)) |
2116 | { |
|
|
2117 | case '\\': |
|
|
2118 | state = st_bsl; |
|
|
2119 | break; |
|
|
2120 | |
|
|
2121 | case '"': |
|
|
2122 | *p = 0; |
|
|
2123 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2167 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2124 | |
2168 | |
2125 | default: |
2169 | if (ecb_expect_false (c == '\\')) |
|
|
2170 | state = st_bsl; |
|
|
2171 | else |
2126 | *p++ = c; |
2172 | *p++ = c; |
2127 | break; |
|
|
2128 | } |
|
|
2129 | |
2173 | |
2130 | break; |
2174 | break; |
2131 | |
2175 | |
2132 | case st_bsl: |
2176 | case st_bsl: |
2133 | switch (c) |
2177 | switch (c) |
… | |
… | |
2142 | case '7': |
2186 | case '7': |
2143 | state = st_oct1; |
2187 | state = st_oct1; |
2144 | c1 = c - '0'; |
2188 | c1 = c - '0'; |
2145 | break; |
2189 | break; |
2146 | |
2190 | |
|
|
2191 | case 'a': *p++ = '\a'; state = st_ok; break; |
|
|
2192 | case 'n': *p++ = '\n'; state = st_ok; break; |
|
|
2193 | case 'r': *p++ = '\r'; state = st_ok; break; |
|
|
2194 | case 't': *p++ = '\t'; state = st_ok; break; |
|
|
2195 | |
|
|
2196 | //TODO: \whitespace eol whitespace |
|
|
2197 | |
|
|
2198 | //TODO: x should end in ;, not two-digit hex |
2147 | case 'x': |
2199 | case 'x': |
2148 | case 'X': |
2200 | case 'X': |
2149 | state = st_x1; |
2201 | state = st_x1; |
2150 | c1 = 0; |
2202 | c1 = 0; |
2151 | break; |
2203 | break; |
2152 | |
2204 | |
2153 | case 'n': |
|
|
2154 | *p++ = '\n'; |
|
|
2155 | state = st_ok; |
|
|
2156 | break; |
|
|
2157 | |
|
|
2158 | case 't': |
|
|
2159 | *p++ = '\t'; |
|
|
2160 | state = st_ok; |
|
|
2161 | break; |
|
|
2162 | |
|
|
2163 | case 'r': |
|
|
2164 | *p++ = '\r'; |
|
|
2165 | state = st_ok; |
|
|
2166 | break; |
|
|
2167 | |
|
|
2168 | case '"': |
|
|
2169 | *p++ = '"'; |
|
|
2170 | state = st_ok; |
|
|
2171 | break; |
|
|
2172 | |
|
|
2173 | default: |
2205 | default: |
2174 | *p++ = c; |
2206 | *p++ = c; |
2175 | state = st_ok; |
2207 | state = st_ok; |
2176 | break; |
2208 | break; |
2177 | } |
2209 | } |
2178 | |
2210 | |
2179 | break; |
2211 | break; |
2180 | |
2212 | |
2181 | case st_x1: |
2213 | case st_x1: |
2182 | case st_x2: |
2214 | case st_x2: |
2183 | c = toupper (c); |
2215 | c = tolower (c); |
2184 | |
2216 | |
2185 | if (c >= '0' && c <= 'F') |
2217 | if (c >= '0' && c <= '9') |
2186 | { |
|
|
2187 | if (c <= '9') |
|
|
2188 | c1 = (c1 << 4) + c - '0'; |
2218 | c1 = (c1 << 4) + c - '0'; |
2189 | else |
2219 | else if (c >= 'a' && c <= 'f') |
2190 | c1 = (c1 << 4) + c - 'A' + 10; |
2220 | c1 = (c1 << 4) + c - 'a' + 10; |
2191 | |
|
|
2192 | if (state == st_x1) |
|
|
2193 | state = st_x2; |
|
|
2194 | else |
|
|
2195 | { |
|
|
2196 | *p++ = c1; |
|
|
2197 | state = st_ok; |
|
|
2198 | } |
|
|
2199 | } |
|
|
2200 | else |
2221 | else |
2201 | return S_F; |
2222 | return S_F; |
|
|
2223 | |
|
|
2224 | if (state == st_x1) |
|
|
2225 | state = st_x2; |
|
|
2226 | else |
|
|
2227 | { |
|
|
2228 | *p++ = c1; |
|
|
2229 | state = st_ok; |
|
|
2230 | } |
2202 | |
2231 | |
2203 | break; |
2232 | break; |
2204 | |
2233 | |
2205 | case st_oct1: |
2234 | case st_oct1: |
2206 | case st_oct2: |
2235 | case st_oct2: |
… | |
… | |
2210 | backchar (SCHEME_A_ c); |
2239 | backchar (SCHEME_A_ c); |
2211 | state = st_ok; |
2240 | state = st_ok; |
2212 | } |
2241 | } |
2213 | else |
2242 | else |
2214 | { |
2243 | { |
2215 | if (state == st_oct2 && c1 >= 32) |
2244 | if (state == st_oct2 && c1 >= ' ') |
2216 | return S_F; |
2245 | return S_F; |
2217 | |
2246 | |
2218 | c1 = (c1 << 3) + (c - '0'); |
2247 | c1 = (c1 << 3) + (c - '0'); |
2219 | |
2248 | |
2220 | if (state == st_oct1) |
2249 | if (state == st_oct1) |
… | |
… | |
2225 | state = st_ok; |
2254 | state = st_ok; |
2226 | } |
2255 | } |
2227 | } |
2256 | } |
2228 | |
2257 | |
2229 | break; |
2258 | break; |
2230 | |
|
|
2231 | } |
2259 | } |
2232 | } |
2260 | } |
2233 | } |
2261 | } |
2234 | |
2262 | |
2235 | /* check c is in chars */ |
2263 | /* check c is in chars */ |
2236 | static INLINE int |
2264 | ecb_inline int |
2237 | is_one_of (char *s, int c) |
2265 | is_one_of (const char *s, int c) |
2238 | { |
2266 | { |
2239 | if (c == EOF) |
|
|
2240 | return 1; |
|
|
2241 | |
|
|
2242 | return !!strchr (s, c); |
2267 | return c == EOF || !!strchr (s, c); |
2243 | } |
2268 | } |
2244 | |
2269 | |
2245 | /* skip white characters */ |
2270 | /* skip white characters */ |
2246 | static INLINE int |
2271 | ecb_inline int |
2247 | skipspace (SCHEME_P) |
2272 | skipspace (SCHEME_P) |
2248 | { |
2273 | { |
2249 | int c, curr_line = 0; |
2274 | int c, curr_line = 0; |
2250 | |
2275 | |
2251 | do |
2276 | do |
2252 | { |
2277 | { |
2253 | c = inchar (SCHEME_A); |
2278 | c = inchar (SCHEME_A); |
|
|
2279 | |
2254 | #if SHOW_ERROR_LINE |
2280 | #if SHOW_ERROR_LINE |
2255 | if (c == '\n') |
2281 | if (ecb_expect_false (c == '\n')) |
2256 | curr_line++; |
2282 | curr_line++; |
2257 | #endif |
2283 | #endif |
|
|
2284 | |
|
|
2285 | if (ecb_expect_false (c == EOF)) |
|
|
2286 | return c; |
2258 | } |
2287 | } |
2259 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2288 | while (is_one_of (WHITESPACE, c)); |
2260 | |
2289 | |
2261 | /* record it */ |
2290 | /* record it */ |
2262 | #if SHOW_ERROR_LINE |
2291 | #if SHOW_ERROR_LINE |
2263 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2292 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2264 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2293 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2265 | #endif |
2294 | #endif |
2266 | |
2295 | |
2267 | if (c != EOF) |
|
|
2268 | { |
|
|
2269 | backchar (SCHEME_A_ c); |
2296 | backchar (SCHEME_A_ c); |
2270 | return 1; |
2297 | return 1; |
2271 | } |
|
|
2272 | else |
|
|
2273 | return EOF; |
|
|
2274 | } |
2298 | } |
2275 | |
2299 | |
2276 | /* get token */ |
2300 | /* get token */ |
2277 | static int |
2301 | static int |
2278 | token (SCHEME_P) |
2302 | token (SCHEME_P) |
… | |
… | |
2294 | return TOK_RPAREN; |
2318 | return TOK_RPAREN; |
2295 | |
2319 | |
2296 | case '.': |
2320 | case '.': |
2297 | c = inchar (SCHEME_A); |
2321 | c = inchar (SCHEME_A); |
2298 | |
2322 | |
2299 | if (is_one_of (" \n\t", c)) |
2323 | if (is_one_of (WHITESPACE, c)) |
2300 | return TOK_DOT; |
2324 | return TOK_DOT; |
2301 | else |
2325 | else |
2302 | { |
2326 | { |
2303 | //TODO: ungetc twice in a row is not supported in C |
|
|
2304 | backchar (SCHEME_A_ c); |
2327 | backchar (SCHEME_A_ c); |
2305 | backchar (SCHEME_A_ '.'); |
|
|
2306 | return TOK_ATOM; |
2328 | return TOK_DOTATOM; |
2307 | } |
2329 | } |
|
|
2330 | |
|
|
2331 | case '|': |
|
|
2332 | return TOK_STRATOM; |
2308 | |
2333 | |
2309 | case '\'': |
2334 | case '\'': |
2310 | return TOK_QUOTE; |
2335 | return TOK_QUOTE; |
2311 | |
2336 | |
2312 | case ';': |
2337 | case ';': |
… | |
… | |
2444 | } |
2469 | } |
2445 | |
2470 | |
2446 | putcharacter (SCHEME_A_ '"'); |
2471 | putcharacter (SCHEME_A_ '"'); |
2447 | } |
2472 | } |
2448 | |
2473 | |
2449 | |
|
|
2450 | /* print atoms */ |
2474 | /* print atoms */ |
2451 | static void |
2475 | static void |
2452 | printatom (SCHEME_P_ pointer l, int f) |
2476 | printatom (SCHEME_P_ pointer l, int f) |
2453 | { |
2477 | { |
2454 | char *p; |
2478 | char *p; |
2455 | int len; |
2479 | int len; |
2456 | |
2480 | |
2457 | atom2str (SCHEME_A_ l, f, &p, &len); |
2481 | atom2str (SCHEME_A_ l, f, &p, &len); |
2458 | putchars (SCHEME_A_ p, len); |
2482 | putchars (SCHEME_A_ p, len); |
2459 | } |
2483 | } |
2460 | |
|
|
2461 | |
2484 | |
2462 | /* Uses internal buffer unless string pointer is already available */ |
2485 | /* Uses internal buffer unless string pointer is already available */ |
2463 | static void |
2486 | static void |
2464 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2487 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2465 | { |
2488 | { |
… | |
… | |
2479 | { |
2502 | { |
2480 | p = SCHEME_V->strbuff; |
2503 | p = SCHEME_V->strbuff; |
2481 | |
2504 | |
2482 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2505 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2483 | { |
2506 | { |
2484 | if (num_is_integer (l)) |
2507 | if (is_integer (l)) |
2485 | xnum (p, ivalue_unchecked (l)); |
2508 | xnum (p, ivalue_unchecked (l)); |
2486 | #if USE_REAL |
2509 | #if USE_REAL |
2487 | else |
2510 | else |
2488 | { |
2511 | { |
2489 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
2512 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
… | |
… | |
2626 | #endif |
2649 | #endif |
2627 | } |
2650 | } |
2628 | else if (is_continuation (l)) |
2651 | else if (is_continuation (l)) |
2629 | p = "#<CONTINUATION>"; |
2652 | p = "#<CONTINUATION>"; |
2630 | else |
2653 | else |
|
|
2654 | { |
|
|
2655 | #if USE_PRINTF |
|
|
2656 | p = SCHEME_V->strbuff; |
|
|
2657 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2658 | #else |
2631 | p = "#<ERROR>"; |
2659 | p = "#<ERROR>"; |
|
|
2660 | #endif |
|
|
2661 | } |
2632 | |
2662 | |
2633 | *pp = p; |
2663 | *pp = p; |
2634 | *plen = strlen (p); |
2664 | *plen = strlen (p); |
2635 | } |
2665 | } |
2636 | |
2666 | |
… | |
… | |
2744 | return 0; |
2774 | return 0; |
2745 | } |
2775 | } |
2746 | else if (is_number (a)) |
2776 | else if (is_number (a)) |
2747 | { |
2777 | { |
2748 | if (is_number (b)) |
2778 | if (is_number (b)) |
2749 | if (num_is_integer (a) == num_is_integer (b)) |
|
|
2750 | return num_cmp (nvalue (a), nvalue (b)) == 0; |
2779 | return num_cmp (nvalue (a), nvalue (b)) == 0; |
2751 | |
2780 | |
2752 | return 0; |
2781 | return 0; |
2753 | } |
2782 | } |
2754 | else if (is_character (a)) |
2783 | else if (is_character (a)) |
2755 | { |
2784 | { |
… | |
… | |
2781 | /* () is #t in R5RS */ |
2810 | /* () is #t in R5RS */ |
2782 | #define is_true(p) ((p) != S_F) |
2811 | #define is_true(p) ((p) != S_F) |
2783 | #define is_false(p) ((p) == S_F) |
2812 | #define is_false(p) ((p) == S_F) |
2784 | |
2813 | |
2785 | /* ========== Environment implementation ========== */ |
2814 | /* ========== Environment implementation ========== */ |
2786 | |
|
|
2787 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
|
|
2788 | |
|
|
2789 | static int |
|
|
2790 | hash_fn (const char *key, int table_size) |
|
|
2791 | { |
|
|
2792 | const unsigned char *p = key; |
|
|
2793 | uint32_t hash = 2166136261; |
|
|
2794 | |
|
|
2795 | while (*p) |
|
|
2796 | hash = (hash ^ *p++) * 16777619; |
|
|
2797 | |
|
|
2798 | return hash % table_size; |
|
|
2799 | } |
|
|
2800 | #endif |
|
|
2801 | |
2815 | |
2802 | #ifndef USE_ALIST_ENV |
2816 | #ifndef USE_ALIST_ENV |
2803 | |
2817 | |
2804 | /* |
2818 | /* |
2805 | * In this implementation, each frame of the environment may be |
2819 | * In this implementation, each frame of the environment may be |
… | |
… | |
2822 | |
2836 | |
2823 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2837 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2824 | setenvironment (SCHEME_V->envir); |
2838 | setenvironment (SCHEME_V->envir); |
2825 | } |
2839 | } |
2826 | |
2840 | |
2827 | static INLINE void |
2841 | static uint32_t |
|
|
2842 | sym_hash (pointer sym, uint32_t size) |
|
|
2843 | { |
|
|
2844 | uintptr_t ptr = (uintptr_t)sym; |
|
|
2845 | |
|
|
2846 | #if 0 |
|
|
2847 | /* table size is prime, so why mix */ |
|
|
2848 | ptr += ptr >> 32; |
|
|
2849 | ptr += ptr >> 16; |
|
|
2850 | ptr += ptr >> 8; |
|
|
2851 | #endif |
|
|
2852 | |
|
|
2853 | return ptr % size; |
|
|
2854 | } |
|
|
2855 | |
|
|
2856 | ecb_inline void |
2828 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2857 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2829 | { |
2858 | { |
2830 | pointer slot = immutable_cons (variable, value); |
2859 | pointer slot = immutable_cons (variable, value); |
2831 | |
2860 | |
2832 | if (is_vector (car (env))) |
2861 | if (is_vector (car (env))) |
2833 | { |
2862 | { |
2834 | int location = hash_fn (symname (variable), veclength (car (env))); |
2863 | int location = sym_hash (variable, veclength (car (env))); |
2835 | |
|
|
2836 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
2864 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2837 | } |
2865 | } |
2838 | else |
2866 | else |
2839 | set_car (env, immutable_cons (slot, car (env))); |
2867 | set_car (env, immutable_cons (slot, car (env))); |
2840 | } |
2868 | } |
2841 | |
2869 | |
2842 | static pointer |
2870 | static pointer |
2843 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2871 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2844 | { |
2872 | { |
2845 | pointer x, y; |
2873 | pointer x, y; |
2846 | int location; |
|
|
2847 | |
2874 | |
2848 | for (x = env; x != NIL; x = cdr (x)) |
2875 | for (x = env; x != NIL; x = cdr (x)) |
2849 | { |
2876 | { |
2850 | if (is_vector (car (x))) |
2877 | if (is_vector (car (x))) |
2851 | { |
2878 | { |
2852 | location = hash_fn (symname (hdl), veclength (car (x))); |
2879 | int location = sym_hash (hdl, veclength (car (x))); |
2853 | y = vector_elem (car (x), location); |
2880 | y = vector_get (car (x), location); |
2854 | } |
2881 | } |
2855 | else |
2882 | else |
2856 | y = car (x); |
2883 | y = car (x); |
2857 | |
2884 | |
2858 | for (; y != NIL; y = cdr (y)) |
2885 | for (; y != NIL; y = cdr (y)) |
2859 | if (caar (y) == hdl) |
2886 | if (caar (y) == hdl) |
2860 | break; |
2887 | break; |
2861 | |
2888 | |
2862 | if (y != NIL) |
2889 | if (y != NIL) |
|
|
2890 | return car (y); |
|
|
2891 | |
|
|
2892 | if (!all) |
2863 | break; |
2893 | break; |
2864 | |
|
|
2865 | if (!all) |
|
|
2866 | return NIL; |
|
|
2867 | } |
2894 | } |
2868 | |
|
|
2869 | if (x != NIL) |
|
|
2870 | return car (y); |
|
|
2871 | |
2895 | |
2872 | return NIL; |
2896 | return NIL; |
2873 | } |
2897 | } |
2874 | |
2898 | |
2875 | #else /* USE_ALIST_ENV */ |
2899 | #else /* USE_ALIST_ENV */ |
2876 | |
2900 | |
2877 | static INLINE void |
2901 | ecb_inline void |
2878 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2902 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2879 | { |
2903 | { |
2880 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2904 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2881 | setenvironment (SCHEME_V->envir); |
2905 | setenvironment (SCHEME_V->envir); |
2882 | } |
2906 | } |
2883 | |
2907 | |
2884 | static INLINE void |
2908 | ecb_inline void |
2885 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2909 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2886 | { |
2910 | { |
2887 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2911 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2888 | } |
2912 | } |
2889 | |
2913 | |
… | |
… | |
2897 | for (y = car (x); y != NIL; y = cdr (y)) |
2921 | for (y = car (x); y != NIL; y = cdr (y)) |
2898 | if (caar (y) == hdl) |
2922 | if (caar (y) == hdl) |
2899 | break; |
2923 | break; |
2900 | |
2924 | |
2901 | if (y != NIL) |
2925 | if (y != NIL) |
|
|
2926 | return car (y); |
2902 | break; |
2927 | break; |
2903 | |
2928 | |
2904 | if (!all) |
2929 | if (!all) |
2905 | return NIL; |
2930 | break; |
2906 | } |
2931 | } |
2907 | |
|
|
2908 | if (x != NIL) |
|
|
2909 | return car (y); |
|
|
2910 | |
2932 | |
2911 | return NIL; |
2933 | return NIL; |
2912 | } |
2934 | } |
2913 | |
2935 | |
2914 | #endif /* USE_ALIST_ENV else */ |
2936 | #endif /* USE_ALIST_ENV else */ |
2915 | |
2937 | |
2916 | static INLINE void |
2938 | ecb_inline void |
2917 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2939 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2918 | { |
2940 | { |
|
|
2941 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2919 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2942 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2920 | } |
2943 | } |
2921 | |
2944 | |
2922 | static INLINE void |
2945 | ecb_inline void |
2923 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2946 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2924 | { |
2947 | { |
2925 | set_cdr (slot, value); |
2948 | set_cdr (slot, value); |
2926 | } |
2949 | } |
2927 | |
2950 | |
2928 | static INLINE pointer |
2951 | ecb_inline pointer |
2929 | slot_value_in_env (pointer slot) |
2952 | slot_value_in_env (pointer slot) |
2930 | { |
2953 | { |
2931 | return cdr (slot); |
2954 | return cdr (slot); |
2932 | } |
2955 | } |
2933 | |
2956 | |
2934 | /* ========== Evaluation Cycle ========== */ |
2957 | /* ========== Evaluation Cycle ========== */ |
2935 | |
2958 | |
2936 | static pointer |
2959 | static int |
2937 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2960 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2938 | { |
2961 | { |
2939 | #if USE_ERROR_HOOK |
2962 | #if USE_ERROR_HOOK |
2940 | pointer x; |
2963 | pointer x; |
2941 | pointer hdl = SCHEME_V->ERROR_HOOK; |
2964 | pointer hdl = SCHEME_V->ERROR_HOOK; |
… | |
… | |
2976 | code = cons (mk_string (SCHEME_A_ s), code); |
2999 | code = cons (mk_string (SCHEME_A_ s), code); |
2977 | setimmutable (car (code)); |
3000 | setimmutable (car (code)); |
2978 | SCHEME_V->code = cons (slot_value_in_env (x), code); |
3001 | SCHEME_V->code = cons (slot_value_in_env (x), code); |
2979 | SCHEME_V->op = OP_EVAL; |
3002 | SCHEME_V->op = OP_EVAL; |
2980 | |
3003 | |
2981 | return S_T; |
3004 | return 0; |
2982 | } |
3005 | } |
2983 | #endif |
3006 | #endif |
2984 | |
3007 | |
2985 | if (a) |
3008 | if (a) |
2986 | SCHEME_V->args = cons (a, NIL); |
3009 | SCHEME_V->args = cons (a, NIL); |
… | |
… | |
2988 | SCHEME_V->args = NIL; |
3011 | SCHEME_V->args = NIL; |
2989 | |
3012 | |
2990 | SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); |
3013 | SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); |
2991 | setimmutable (car (SCHEME_V->args)); |
3014 | setimmutable (car (SCHEME_V->args)); |
2992 | SCHEME_V->op = OP_ERR0; |
3015 | SCHEME_V->op = OP_ERR0; |
|
|
3016 | |
2993 | return S_T; |
3017 | return 0; |
2994 | } |
3018 | } |
2995 | |
3019 | |
2996 | #define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) |
3020 | #define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) |
2997 | #define Error_0(s) Error_1 (s, 0) |
3021 | #define Error_0(s) Error_1 (s, 0) |
2998 | |
3022 | |
2999 | /* Too small to turn into function */ |
3023 | /* Too small to turn into function */ |
3000 | #define BEGIN do { |
3024 | #define BEGIN do { |
3001 | #define END } while (0) |
3025 | #define END } while (0) |
3002 | #define s_goto(a) BEGIN \ |
3026 | #define s_goto(a) BEGIN \ |
3003 | SCHEME_V->op = a; \ |
3027 | SCHEME_V->op = a; \ |
3004 | return S_T; END |
3028 | return 0; END |
3005 | |
3029 | |
3006 | #define s_return(a) return xs_return (SCHEME_A_ a) |
3030 | #define s_return(a) return xs_return (SCHEME_A_ a) |
3007 | |
3031 | |
3008 | #ifndef USE_SCHEME_STACK |
3032 | #ifndef USE_SCHEME_STACK |
3009 | |
3033 | |
… | |
… | |
3023 | { |
3047 | { |
3024 | int nframes = (uintptr_t)SCHEME_V->dump; |
3048 | int nframes = (uintptr_t)SCHEME_V->dump; |
3025 | struct dump_stack_frame *next_frame; |
3049 | struct dump_stack_frame *next_frame; |
3026 | |
3050 | |
3027 | /* enough room for the next frame? */ |
3051 | /* enough room for the next frame? */ |
3028 | if (nframes >= SCHEME_V->dump_size) |
3052 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3029 | { |
3053 | { |
3030 | SCHEME_V->dump_size += STACK_GROWTH; |
3054 | SCHEME_V->dump_size += STACK_GROWTH; |
3031 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3055 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3032 | } |
3056 | } |
3033 | |
3057 | |
… | |
… | |
3039 | next_frame->code = code; |
3063 | next_frame->code = code; |
3040 | |
3064 | |
3041 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3065 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3042 | } |
3066 | } |
3043 | |
3067 | |
3044 | static pointer |
3068 | static int |
3045 | xs_return (SCHEME_P_ pointer a) |
3069 | xs_return (SCHEME_P_ pointer a) |
3046 | { |
3070 | { |
3047 | int nframes = (uintptr_t)SCHEME_V->dump; |
3071 | int nframes = (uintptr_t)SCHEME_V->dump; |
3048 | struct dump_stack_frame *frame; |
3072 | struct dump_stack_frame *frame; |
3049 | |
3073 | |
3050 | SCHEME_V->value = a; |
3074 | SCHEME_V->value = a; |
3051 | |
3075 | |
3052 | if (nframes <= 0) |
3076 | if (nframes <= 0) |
3053 | return NIL; |
3077 | return -1; |
3054 | |
3078 | |
3055 | frame = &SCHEME_V->dump_base[--nframes]; |
3079 | frame = &SCHEME_V->dump_base[--nframes]; |
3056 | SCHEME_V->op = frame->op; |
3080 | SCHEME_V->op = frame->op; |
3057 | SCHEME_V->args = frame->args; |
3081 | SCHEME_V->args = frame->args; |
3058 | SCHEME_V->envir = frame->envir; |
3082 | SCHEME_V->envir = frame->envir; |
3059 | SCHEME_V->code = frame->code; |
3083 | SCHEME_V->code = frame->code; |
3060 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3084 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3061 | |
3085 | |
3062 | return S_T; |
3086 | return 0; |
3063 | } |
3087 | } |
3064 | |
3088 | |
3065 | static INLINE void |
3089 | ecb_inline void |
3066 | dump_stack_reset (SCHEME_P) |
3090 | dump_stack_reset (SCHEME_P) |
3067 | { |
3091 | { |
3068 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3092 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3069 | SCHEME_V->dump = (pointer)+0; |
3093 | SCHEME_V->dump = (pointer)+0; |
3070 | } |
3094 | } |
3071 | |
3095 | |
3072 | static INLINE void |
3096 | ecb_inline void |
3073 | dump_stack_initialize (SCHEME_P) |
3097 | dump_stack_initialize (SCHEME_P) |
3074 | { |
3098 | { |
3075 | SCHEME_V->dump_size = 0; |
3099 | SCHEME_V->dump_size = 0; |
3076 | SCHEME_V->dump_base = 0; |
3100 | SCHEME_V->dump_base = 0; |
3077 | dump_stack_reset (SCHEME_A); |
3101 | dump_stack_reset (SCHEME_A); |
… | |
… | |
3130 | int i = 0; |
3154 | int i = 0; |
3131 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3155 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3132 | |
3156 | |
3133 | while (cont != NIL) |
3157 | while (cont != NIL) |
3134 | { |
3158 | { |
3135 | frame->op = ivalue (car (cont)); cont = cdr (cont); |
3159 | frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont); |
3136 | frame->args = car (cont) ; cont = cdr (cont); |
3160 | frame->args = car (cont) ; cont = cdr (cont); |
3137 | frame->envir = car (cont) ; cont = cdr (cont); |
3161 | frame->envir = car (cont) ; cont = cdr (cont); |
3138 | frame->code = car (cont) ; cont = cdr (cont); |
3162 | frame->code = car (cont) ; cont = cdr (cont); |
3139 | |
3163 | |
3140 | ++frame; |
3164 | ++frame; |
3141 | ++i; |
3165 | ++i; |
3142 | } |
3166 | } |
3143 | |
3167 | |
3144 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3168 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3145 | } |
3169 | } |
3146 | |
3170 | |
3147 | #else |
3171 | #else |
3148 | |
3172 | |
3149 | static INLINE void |
3173 | ecb_inline void |
3150 | dump_stack_reset (SCHEME_P) |
3174 | dump_stack_reset (SCHEME_P) |
3151 | { |
3175 | { |
3152 | SCHEME_V->dump = NIL; |
3176 | SCHEME_V->dump = NIL; |
3153 | } |
3177 | } |
3154 | |
3178 | |
3155 | static INLINE void |
3179 | ecb_inline void |
3156 | dump_stack_initialize (SCHEME_P) |
3180 | dump_stack_initialize (SCHEME_P) |
3157 | { |
3181 | { |
3158 | dump_stack_reset (SCHEME_A); |
3182 | dump_stack_reset (SCHEME_A); |
3159 | } |
3183 | } |
3160 | |
3184 | |
… | |
… | |
3162 | dump_stack_free (SCHEME_P) |
3186 | dump_stack_free (SCHEME_P) |
3163 | { |
3187 | { |
3164 | SCHEME_V->dump = NIL; |
3188 | SCHEME_V->dump = NIL; |
3165 | } |
3189 | } |
3166 | |
3190 | |
3167 | static pointer |
3191 | static int |
3168 | xs_return (SCHEME_P_ pointer a) |
3192 | xs_return (SCHEME_P_ pointer a) |
3169 | { |
3193 | { |
3170 | pointer dump = SCHEME_V->dump; |
3194 | pointer dump = SCHEME_V->dump; |
3171 | |
3195 | |
3172 | SCHEME_V->value = a; |
3196 | SCHEME_V->value = a; |
3173 | |
3197 | |
3174 | if (dump == NIL) |
3198 | if (dump == NIL) |
3175 | return NIL; |
3199 | return -1; |
3176 | |
3200 | |
3177 | SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); |
3201 | SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump); |
3178 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3202 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3179 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3203 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3180 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3204 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3181 | |
3205 | |
3182 | SCHEME_V->dump = dump; |
3206 | SCHEME_V->dump = dump; |
3183 | |
3207 | |
3184 | return S_T; |
3208 | return 0; |
3185 | } |
3209 | } |
3186 | |
3210 | |
3187 | static void |
3211 | static void |
3188 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3212 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3189 | { |
3213 | { |
… | |
… | |
3214 | |
3238 | |
3215 | #endif |
3239 | #endif |
3216 | |
3240 | |
3217 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3241 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3218 | |
3242 | |
|
|
3243 | #if EXPERIMENT |
3219 | static pointer |
3244 | static int |
|
|
3245 | debug (SCHEME_P_ int indent, pointer x) |
|
|
3246 | { |
|
|
3247 | int c; |
|
|
3248 | |
|
|
3249 | if (is_syntax (x)) |
|
|
3250 | { |
|
|
3251 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3252 | return 8 + 8; |
|
|
3253 | } |
|
|
3254 | |
|
|
3255 | if (x == NIL) |
|
|
3256 | { |
|
|
3257 | printf ("%*sNIL\n", indent, ""); |
|
|
3258 | return 3; |
|
|
3259 | } |
|
|
3260 | |
|
|
3261 | switch (type (x)) |
|
|
3262 | { |
|
|
3263 | case T_INTEGER: |
|
|
3264 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3265 | return 32+8; |
|
|
3266 | |
|
|
3267 | case T_SYMBOL: |
|
|
3268 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3269 | return 24+8; |
|
|
3270 | |
|
|
3271 | case T_CLOSURE: |
|
|
3272 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3273 | debug (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3274 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3275 | |
|
|
3276 | case T_PAIR: |
|
|
3277 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3278 | c = debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3279 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3280 | return c + 1; |
|
|
3281 | |
|
|
3282 | case T_PORT: |
|
|
3283 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3284 | return 24+8; |
|
|
3285 | |
|
|
3286 | case T_VECTOR: |
|
|
3287 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3288 | return 24+8; |
|
|
3289 | |
|
|
3290 | case T_ENVIRONMENT: |
|
|
3291 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3292 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3293 | |
|
|
3294 | default: |
|
|
3295 | printf ("unhandled type %d\n", type (x)); |
|
|
3296 | break; |
|
|
3297 | } |
|
|
3298 | } |
|
|
3299 | #endif |
|
|
3300 | |
|
|
3301 | static int |
3220 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3302 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3221 | { |
3303 | { |
3222 | pointer args = SCHEME_V->args; |
3304 | pointer args = SCHEME_V->args; |
3223 | pointer x, y; |
3305 | pointer x, y; |
3224 | |
3306 | |
3225 | switch (op) |
3307 | switch (op) |
3226 | { |
3308 | { |
|
|
3309 | #if EXPERIMENT //D |
|
|
3310 | case OP_DEBUG: |
|
|
3311 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
|
|
3312 | printf ("\n"); |
|
|
3313 | s_return (S_T); |
|
|
3314 | #endif |
3227 | case OP_LOAD: /* load */ |
3315 | case OP_LOAD: /* load */ |
3228 | if (file_interactive (SCHEME_A)) |
3316 | if (file_interactive (SCHEME_A)) |
3229 | { |
3317 | { |
3230 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3318 | putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n"); |
3231 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3319 | //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3232 | } |
3320 | } |
3233 | |
3321 | |
3234 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3322 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3235 | Error_1 ("unable to open", car (args)); |
3323 | Error_1 ("unable to open", car (args)); |
3236 | else |
3324 | else |
… | |
… | |
3240 | } |
3328 | } |
3241 | |
3329 | |
3242 | case OP_T0LVL: /* top level */ |
3330 | case OP_T0LVL: /* top level */ |
3243 | |
3331 | |
3244 | /* If we reached the end of file, this loop is done. */ |
3332 | /* If we reached the end of file, this loop is done. */ |
3245 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3333 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3246 | { |
3334 | { |
3247 | if (SCHEME_V->file_i == 0) |
3335 | if (SCHEME_V->file_i == 0) |
3248 | { |
3336 | { |
3249 | SCHEME_V->args = NIL; |
3337 | SCHEME_V->args = NIL; |
3250 | s_goto (OP_QUIT); |
3338 | s_goto (OP_QUIT); |
… | |
… | |
3328 | #endif |
3416 | #endif |
3329 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3417 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3330 | { |
3418 | { |
3331 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3419 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3332 | |
3420 | |
3333 | if (x != NIL) |
3421 | if (x == NIL) |
3334 | s_return (slot_value_in_env (x)); |
|
|
3335 | else |
|
|
3336 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3422 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3423 | |
|
|
3424 | s_return (slot_value_in_env (x)); |
3337 | } |
3425 | } |
3338 | else if (is_pair (SCHEME_V->code)) |
3426 | else if (is_pair (SCHEME_V->code)) |
3339 | { |
3427 | { |
3340 | x = car (SCHEME_V->code); |
3428 | x = car (SCHEME_V->code); |
3341 | |
3429 | |
… | |
… | |
3354 | } |
3442 | } |
3355 | else |
3443 | else |
3356 | s_return (SCHEME_V->code); |
3444 | s_return (SCHEME_V->code); |
3357 | |
3445 | |
3358 | case OP_E0ARGS: /* eval arguments */ |
3446 | case OP_E0ARGS: /* eval arguments */ |
3359 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3447 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3360 | { |
3448 | { |
3361 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3449 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3362 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3450 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3363 | SCHEME_V->code = SCHEME_V->value; |
3451 | SCHEME_V->code = SCHEME_V->value; |
3364 | s_goto (OP_APPLY); |
3452 | s_goto (OP_APPLY); |
… | |
… | |
3391 | |
3479 | |
3392 | case OP_TRACING: |
3480 | case OP_TRACING: |
3393 | { |
3481 | { |
3394 | int tr = SCHEME_V->tracing; |
3482 | int tr = SCHEME_V->tracing; |
3395 | |
3483 | |
3396 | SCHEME_V->tracing = ivalue (car (args)); |
3484 | SCHEME_V->tracing = ivalue_unchecked (car (args)); |
3397 | s_return (mk_integer (SCHEME_A_ tr)); |
3485 | s_return (mk_integer (SCHEME_A_ tr)); |
3398 | } |
3486 | } |
3399 | |
3487 | |
3400 | #endif |
3488 | #endif |
3401 | |
3489 | |
… | |
… | |
3418 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3506 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3419 | else if (is_foreign (SCHEME_V->code)) |
3507 | else if (is_foreign (SCHEME_V->code)) |
3420 | { |
3508 | { |
3421 | /* Keep nested calls from GC'ing the arglist */ |
3509 | /* Keep nested calls from GC'ing the arglist */ |
3422 | push_recent_alloc (SCHEME_A_ args, NIL); |
3510 | push_recent_alloc (SCHEME_A_ args, NIL); |
3423 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3511 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3424 | |
3512 | |
3425 | s_return (x); |
3513 | s_return (x); |
3426 | } |
3514 | } |
3427 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3515 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3428 | { |
3516 | { |
… | |
… | |
3465 | |
3553 | |
3466 | case OP_DOMACRO: /* do macro */ |
3554 | case OP_DOMACRO: /* do macro */ |
3467 | SCHEME_V->code = SCHEME_V->value; |
3555 | SCHEME_V->code = SCHEME_V->value; |
3468 | s_goto (OP_EVAL); |
3556 | s_goto (OP_EVAL); |
3469 | |
3557 | |
3470 | #if 1 |
|
|
3471 | |
|
|
3472 | case OP_LAMBDA: /* lambda */ |
3558 | case OP_LAMBDA: /* lambda */ |
3473 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3559 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3474 | set SCHEME_V->value fall thru */ |
3560 | set SCHEME_V->value fall thru */ |
3475 | { |
3561 | { |
3476 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3562 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
… | |
… | |
3482 | SCHEME_V->code = slot_value_in_env (f); |
3568 | SCHEME_V->code = slot_value_in_env (f); |
3483 | s_goto (OP_APPLY); |
3569 | s_goto (OP_APPLY); |
3484 | } |
3570 | } |
3485 | |
3571 | |
3486 | SCHEME_V->value = SCHEME_V->code; |
3572 | SCHEME_V->value = SCHEME_V->code; |
3487 | /* Fallthru */ |
|
|
3488 | } |
3573 | } |
|
|
3574 | /* Fallthru */ |
3489 | |
3575 | |
3490 | case OP_LAMBDA1: |
3576 | case OP_LAMBDA1: |
3491 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3577 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3492 | |
|
|
3493 | #else |
|
|
3494 | |
|
|
3495 | case OP_LAMBDA: /* lambda */ |
|
|
3496 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
|
|
3497 | |
|
|
3498 | #endif |
|
|
3499 | |
3578 | |
3500 | case OP_MKCLOSURE: /* make-closure */ |
3579 | case OP_MKCLOSURE: /* make-closure */ |
3501 | x = car (args); |
3580 | x = car (args); |
3502 | |
3581 | |
3503 | if (car (x) == SCHEME_V->LAMBDA) |
3582 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
3910 | SCHEME_V->code = car (args); |
3989 | SCHEME_V->code = car (args); |
3911 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); |
3990 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); |
3912 | s_goto (OP_APPLY); |
3991 | s_goto (OP_APPLY); |
3913 | } |
3992 | } |
3914 | |
3993 | |
3915 | abort (); |
3994 | if (USE_ERROR_CHECKING) abort (); |
3916 | } |
3995 | } |
3917 | |
3996 | |
3918 | static pointer |
3997 | static int |
3919 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
3998 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
3920 | { |
3999 | { |
3921 | pointer args = SCHEME_V->args; |
4000 | pointer args = SCHEME_V->args; |
3922 | pointer x = car (args); |
4001 | pointer x = car (args); |
3923 | num v; |
4002 | num v; |
3924 | |
4003 | |
3925 | #if USE_MATH |
|
|
3926 | RVALUE dd; |
|
|
3927 | #endif |
|
|
3928 | |
|
|
3929 | switch (op) |
4004 | switch (op) |
3930 | { |
4005 | { |
3931 | #if USE_MATH |
4006 | #if USE_MATH |
3932 | case OP_INEX2EX: /* inexact->exact */ |
4007 | case OP_INEX2EX: /* inexact->exact */ |
3933 | if (num_is_integer (x)) |
4008 | if (!is_integer (x)) |
3934 | s_return (x); |
4009 | { |
3935 | else if (modf (rvalue_unchecked (x), &dd) == 0) |
4010 | RVALUE r = rvalue_unchecked (x); |
3936 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4011 | |
|
|
4012 | if (r == (RVALUE)(IVALUE)r) |
|
|
4013 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
3937 | else |
4014 | else |
3938 | Error_1 ("inexact->exact: not integral:", x); |
4015 | Error_1 ("inexact->exact: not integral:", x); |
|
|
4016 | } |
3939 | |
4017 | |
|
|
4018 | s_return (x); |
|
|
4019 | |
|
|
4020 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4021 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4022 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4023 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
|
|
4024 | |
|
|
4025 | case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
3940 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4026 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
3941 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
4027 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
|
|
4028 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
3942 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4029 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
3943 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4030 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
3944 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4031 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
3945 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4032 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
3946 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4033 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
3947 | |
4034 | |
3948 | case OP_ATAN: |
4035 | case OP_ATAN: |
|
|
4036 | s_return (mk_real (SCHEME_A_ |
3949 | if (cdr (args) == NIL) |
4037 | cdr (args) == NIL |
3950 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4038 | ? atan (rvalue (x)) |
3951 | else |
4039 | : atan2 (rvalue (x), rvalue (cadr (args))))); |
3952 | { |
|
|
3953 | pointer y = cadr (args); |
|
|
3954 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
|
|
3955 | } |
|
|
3956 | |
|
|
3957 | case OP_SQRT: |
|
|
3958 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
|
|
3959 | |
4040 | |
3960 | case OP_EXPT: |
4041 | case OP_EXPT: |
3961 | { |
4042 | { |
3962 | RVALUE result; |
4043 | RVALUE result; |
3963 | int real_result = 1; |
4044 | int real_result = 1; |
3964 | pointer y = cadr (args); |
4045 | pointer y = cadr (args); |
3965 | |
4046 | |
3966 | if (num_is_integer (x) && num_is_integer (y)) |
4047 | if (is_integer (x) && is_integer (y)) |
3967 | real_result = 0; |
4048 | real_result = 0; |
3968 | |
4049 | |
3969 | /* This 'if' is an R5RS compatibility fix. */ |
4050 | /* This 'if' is an R5RS compatibility fix. */ |
3970 | /* NOTE: Remove this 'if' fix for R6RS. */ |
4051 | /* NOTE: Remove this 'if' fix for R6RS. */ |
3971 | if (rvalue (x) == 0 && rvalue (y) < 0) |
4052 | if (rvalue (x) == 0 && rvalue (y) < 0) |
… | |
… | |
3977 | /* If the test fails, result is too big for integer. */ |
4058 | /* If the test fails, result is too big for integer. */ |
3978 | if (!real_result) |
4059 | if (!real_result) |
3979 | { |
4060 | { |
3980 | long result_as_long = result; |
4061 | long result_as_long = result; |
3981 | |
4062 | |
3982 | if (result != (RVALUE) result_as_long) |
4063 | if (result != result_as_long) |
3983 | real_result = 1; |
4064 | real_result = 1; |
3984 | } |
4065 | } |
3985 | |
4066 | |
3986 | if (real_result) |
4067 | if (real_result) |
3987 | s_return (mk_real (SCHEME_A_ result)); |
4068 | s_return (mk_real (SCHEME_A_ result)); |
3988 | else |
4069 | else |
3989 | s_return (mk_integer (SCHEME_A_ result)); |
4070 | s_return (mk_integer (SCHEME_A_ result)); |
3990 | } |
4071 | } |
3991 | |
|
|
3992 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
3993 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
3994 | |
|
|
3995 | case OP_TRUNCATE: |
|
|
3996 | { |
|
|
3997 | RVALUE rvalue_of_x; |
|
|
3998 | |
|
|
3999 | rvalue_of_x = rvalue (x); |
|
|
4000 | |
|
|
4001 | if (rvalue_of_x > 0) |
|
|
4002 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
|
|
4003 | else |
|
|
4004 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
|
|
4005 | } |
|
|
4006 | |
|
|
4007 | case OP_ROUND: |
|
|
4008 | if (num_is_integer (x)) |
|
|
4009 | s_return (x); |
|
|
4010 | |
|
|
4011 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
|
|
4012 | #endif |
4072 | #endif |
4013 | |
4073 | |
4014 | case OP_ADD: /* + */ |
4074 | case OP_ADD: /* + */ |
4015 | v = num_zero; |
4075 | v = num_zero; |
4016 | |
4076 | |
4017 | for (x = args; x != NIL; x = cdr (x)) |
4077 | for (x = args; x != NIL; x = cdr (x)) |
4018 | v = num_op ('+', v, nvalue (car (x))); |
4078 | v = num_op (NUM_ADD, v, nvalue (car (x))); |
4019 | |
4079 | |
4020 | s_return (mk_number (SCHEME_A_ v)); |
4080 | s_return (mk_number (SCHEME_A_ v)); |
4021 | |
4081 | |
4022 | case OP_MUL: /* * */ |
4082 | case OP_MUL: /* * */ |
4023 | v = num_one; |
4083 | v = num_one; |
4024 | |
4084 | |
4025 | for (x = args; x != NIL; x = cdr (x)) |
4085 | for (x = args; x != NIL; x = cdr (x)) |
4026 | v = num_op ('+', v, nvalue (car (x))); |
4086 | v = num_op (NUM_MUL, v, nvalue (car (x))); |
4027 | |
4087 | |
4028 | s_return (mk_number (SCHEME_A_ v)); |
4088 | s_return (mk_number (SCHEME_A_ v)); |
4029 | |
4089 | |
4030 | case OP_SUB: /* - */ |
4090 | case OP_SUB: /* - */ |
4031 | if (cdr (args) == NIL) |
4091 | if (cdr (args) == NIL) |
… | |
… | |
4038 | x = cdr (args); |
4098 | x = cdr (args); |
4039 | v = nvalue (car (args)); |
4099 | v = nvalue (car (args)); |
4040 | } |
4100 | } |
4041 | |
4101 | |
4042 | for (; x != NIL; x = cdr (x)) |
4102 | for (; x != NIL; x = cdr (x)) |
4043 | v = num_op ('+', v, nvalue (car (x))); |
4103 | v = num_op (NUM_SUB, v, nvalue (car (x))); |
4044 | |
4104 | |
4045 | s_return (mk_number (SCHEME_A_ v)); |
4105 | s_return (mk_number (SCHEME_A_ v)); |
4046 | |
4106 | |
4047 | case OP_DIV: /* / */ |
4107 | case OP_DIV: /* / */ |
4048 | if (cdr (args) == NIL) |
4108 | if (cdr (args) == NIL) |
… | |
… | |
4055 | x = cdr (args); |
4115 | x = cdr (args); |
4056 | v = nvalue (car (args)); |
4116 | v = nvalue (car (args)); |
4057 | } |
4117 | } |
4058 | |
4118 | |
4059 | for (; x != NIL; x = cdr (x)) |
4119 | for (; x != NIL; x = cdr (x)) |
4060 | { |
|
|
4061 | if (!is_zero_rvalue (rvalue (car (x)))) |
4120 | if (!is_zero_rvalue (rvalue (car (x)))) |
4062 | v = num_div (v, nvalue (car (x))); |
4121 | v = num_div (v, nvalue (car (x))); |
4063 | else |
4122 | else |
4064 | Error_0 ("/: division by zero"); |
4123 | Error_0 ("/: division by zero"); |
4065 | } |
|
|
4066 | |
4124 | |
4067 | s_return (mk_number (SCHEME_A_ v)); |
4125 | s_return (mk_number (SCHEME_A_ v)); |
4068 | |
4126 | |
4069 | case OP_INTDIV: /* quotient */ |
4127 | case OP_INTDIV: /* quotient */ |
4070 | if (cdr (args) == NIL) |
4128 | if (cdr (args) == NIL) |
… | |
… | |
4079 | } |
4137 | } |
4080 | |
4138 | |
4081 | for (; x != NIL; x = cdr (x)) |
4139 | for (; x != NIL; x = cdr (x)) |
4082 | { |
4140 | { |
4083 | if (ivalue (car (x)) != 0) |
4141 | if (ivalue (car (x)) != 0) |
4084 | v = num_op ('/', v, nvalue (car (x))); |
4142 | v = num_op (NUM_INTDIV, v, nvalue (car (x))); |
4085 | else |
4143 | else |
4086 | Error_0 ("quotient: division by zero"); |
4144 | Error_0 ("quotient: division by zero"); |
4087 | } |
4145 | } |
4088 | |
4146 | |
4089 | s_return (mk_number (SCHEME_A_ v)); |
4147 | s_return (mk_number (SCHEME_A_ v)); |
… | |
… | |
4106 | else |
4164 | else |
4107 | Error_0 ("modulo: division by zero"); |
4165 | Error_0 ("modulo: division by zero"); |
4108 | |
4166 | |
4109 | s_return (mk_number (SCHEME_A_ v)); |
4167 | s_return (mk_number (SCHEME_A_ v)); |
4110 | |
4168 | |
4111 | case OP_CAR: /* car */ |
4169 | /* the compiler will optimize this mess... */ |
4112 | s_return (caar (args)); |
4170 | case OP_CAR: op_car: s_return (car (x)); |
4113 | |
4171 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4114 | case OP_CDR: /* cdr */ |
4172 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4115 | s_return (cdar (args)); |
4173 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4174 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4175 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4176 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4177 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4178 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4179 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4180 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4181 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4182 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4183 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4184 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4185 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4186 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4187 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4188 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4189 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4190 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4191 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4192 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4193 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4194 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4195 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4196 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4197 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4198 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4199 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4116 | |
4200 | |
4117 | case OP_CONS: /* cons */ |
4201 | case OP_CONS: /* cons */ |
4118 | set_cdr (args, cadr (args)); |
4202 | set_cdr (args, cadr (args)); |
4119 | s_return (args); |
4203 | s_return (args); |
4120 | |
4204 | |
… | |
… | |
4135 | } |
4219 | } |
4136 | else |
4220 | else |
4137 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4221 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4138 | |
4222 | |
4139 | case OP_CHAR2INT: /* char->integer */ |
4223 | case OP_CHAR2INT: /* char->integer */ |
4140 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4224 | s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x))); |
4141 | |
4225 | |
4142 | case OP_INT2CHAR: /* integer->char */ |
4226 | case OP_INT2CHAR: /* integer->char */ |
4143 | s_return (mk_character (SCHEME_A_ ivalue (x))); |
4227 | s_return (mk_character (SCHEME_A_ ivalue_unchecked (x))); |
4144 | |
4228 | |
4145 | case OP_CHARUPCASE: |
4229 | case OP_CHARUPCASE: |
4146 | { |
4230 | { |
4147 | unsigned char c = ivalue (x); |
4231 | unsigned char c = ivalue_unchecked (x); |
4148 | c = toupper (c); |
4232 | c = toupper (c); |
4149 | s_return (mk_character (SCHEME_A_ c)); |
4233 | s_return (mk_character (SCHEME_A_ c)); |
4150 | } |
4234 | } |
4151 | |
4235 | |
4152 | case OP_CHARDNCASE: |
4236 | case OP_CHARDNCASE: |
4153 | { |
4237 | { |
4154 | unsigned char c = ivalue (x); |
4238 | unsigned char c = ivalue_unchecked (x); |
4155 | c = tolower (c); |
4239 | c = tolower (c); |
4156 | s_return (mk_character (SCHEME_A_ c)); |
4240 | s_return (mk_character (SCHEME_A_ c)); |
4157 | } |
4241 | } |
4158 | |
4242 | |
4159 | case OP_STR2SYM: /* string->symbol */ |
4243 | case OP_STR2SYM: /* string->symbol */ |
… | |
… | |
4236 | Error_1 ("atom->string: not an atom:", x); |
4320 | Error_1 ("atom->string: not an atom:", x); |
4237 | } |
4321 | } |
4238 | |
4322 | |
4239 | case OP_MKSTRING: /* make-string */ |
4323 | case OP_MKSTRING: /* make-string */ |
4240 | { |
4324 | { |
4241 | int fill = ' '; |
4325 | int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' '; |
4242 | int len; |
|
|
4243 | |
|
|
4244 | len = ivalue (x); |
4326 | int len = ivalue_unchecked (x); |
4245 | |
|
|
4246 | if (cdr (args) != NIL) |
|
|
4247 | fill = charvalue (cadr (args)); |
|
|
4248 | |
4327 | |
4249 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4328 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4250 | } |
4329 | } |
4251 | |
4330 | |
4252 | case OP_STRLEN: /* string-length */ |
4331 | case OP_STRLEN: /* string-length */ |
4253 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4332 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4254 | |
4333 | |
4255 | case OP_STRREF: /* string-ref */ |
4334 | case OP_STRREF: /* string-ref */ |
4256 | { |
4335 | { |
4257 | char *str; |
|
|
4258 | int index; |
|
|
4259 | |
|
|
4260 | str = strvalue (x); |
4336 | char *str = strvalue (x); |
4261 | |
|
|
4262 | index = ivalue (cadr (args)); |
4337 | int index = ivalue_unchecked (cadr (args)); |
4263 | |
4338 | |
4264 | if (index >= strlength (x)) |
4339 | if (index >= strlength (x)) |
4265 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4340 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4266 | |
4341 | |
4267 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4342 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4268 | } |
4343 | } |
4269 | |
4344 | |
4270 | case OP_STRSET: /* string-set! */ |
4345 | case OP_STRSET: /* string-set! */ |
4271 | { |
4346 | { |
4272 | char *str; |
4347 | char *str = strvalue (x); |
4273 | int index; |
4348 | int index = ivalue_unchecked (cadr (args)); |
4274 | int c; |
4349 | int c; |
4275 | |
4350 | |
4276 | if (is_immutable (x)) |
4351 | if (is_immutable (x)) |
4277 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4352 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4278 | |
|
|
4279 | str = strvalue (x); |
|
|
4280 | |
|
|
4281 | index = ivalue (cadr (args)); |
|
|
4282 | |
4353 | |
4283 | if (index >= strlength (x)) |
4354 | if (index >= strlength (x)) |
4284 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4355 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4285 | |
4356 | |
4286 | c = charvalue (caddr (args)); |
4357 | c = charvalue (caddr (args)); |
… | |
… | |
4307 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4378 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4308 | |
4379 | |
4309 | s_return (newstr); |
4380 | s_return (newstr); |
4310 | } |
4381 | } |
4311 | |
4382 | |
4312 | case OP_SUBSTR: /* substring */ |
4383 | case OP_STRING_COPY: /* substring/string-copy */ |
4313 | { |
4384 | { |
4314 | char *str; |
4385 | char *str = strvalue (x); |
4315 | int index0; |
4386 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4316 | int index1; |
4387 | int index1; |
4317 | int len; |
4388 | int len; |
4318 | |
4389 | |
4319 | str = strvalue (x); |
|
|
4320 | |
|
|
4321 | index0 = ivalue (cadr (args)); |
|
|
4322 | |
|
|
4323 | if (index0 > strlength (x)) |
4390 | if (index0 > strlength (x)) |
4324 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4391 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4325 | |
4392 | |
4326 | if (cddr (args) != NIL) |
4393 | if (cddr (args) != NIL) |
4327 | { |
4394 | { |
4328 | index1 = ivalue (caddr (args)); |
4395 | index1 = ivalue_unchecked (caddr (args)); |
4329 | |
4396 | |
4330 | if (index1 > strlength (x) || index1 < index0) |
4397 | if (index1 > strlength (x) || index1 < index0) |
4331 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4398 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4332 | } |
4399 | } |
4333 | else |
4400 | else |
4334 | index1 = strlength (x); |
4401 | index1 = strlength (x); |
4335 | |
4402 | |
4336 | len = index1 - index0; |
4403 | len = index1 - index0; |
4337 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4404 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4338 | memcpy (strvalue (x), str + index0, len); |
|
|
4339 | strvalue (x)[len] = 0; |
|
|
4340 | |
4405 | |
4341 | s_return (x); |
4406 | s_return (x); |
4342 | } |
4407 | } |
4343 | |
4408 | |
4344 | case OP_VECTOR: /* vector */ |
4409 | case OP_VECTOR: /* vector */ |
… | |
… | |
4356 | if (SCHEME_V->no_memory) |
4421 | if (SCHEME_V->no_memory) |
4357 | s_return (S_SINK); |
4422 | s_return (S_SINK); |
4358 | #endif |
4423 | #endif |
4359 | |
4424 | |
4360 | 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++) |
4361 | set_vector_elem (vec, i, car (x)); |
4426 | vector_set (vec, i, car (x)); |
4362 | |
4427 | |
4363 | s_return (vec); |
4428 | s_return (vec); |
4364 | } |
4429 | } |
4365 | |
4430 | |
4366 | case OP_MKVECTOR: /* make-vector */ |
4431 | case OP_MKVECTOR: /* make-vector */ |
4367 | { |
4432 | { |
4368 | pointer fill = NIL; |
4433 | pointer fill = NIL; |
4369 | int len; |
|
|
4370 | pointer vec; |
4434 | pointer vec; |
4371 | |
|
|
4372 | len = ivalue (x); |
4435 | int len = ivalue_unchecked (x); |
4373 | |
4436 | |
4374 | if (cdr (args) != NIL) |
4437 | if (cdr (args) != NIL) |
4375 | fill = cadr (args); |
4438 | fill = cadr (args); |
4376 | |
4439 | |
4377 | vec = mk_vector (SCHEME_A_ len); |
4440 | vec = mk_vector (SCHEME_A_ len); |
… | |
… | |
4380 | if (SCHEME_V->no_memory) |
4443 | if (SCHEME_V->no_memory) |
4381 | s_return (S_SINK); |
4444 | s_return (S_SINK); |
4382 | #endif |
4445 | #endif |
4383 | |
4446 | |
4384 | if (fill != NIL) |
4447 | if (fill != NIL) |
4385 | fill_vector (vec, fill); |
4448 | fill_vector (vec, 0, fill); |
4386 | |
4449 | |
4387 | s_return (vec); |
4450 | s_return (vec); |
4388 | } |
4451 | } |
4389 | |
4452 | |
4390 | case OP_VECLEN: /* vector-length */ |
4453 | case OP_VECLEN: /* vector-length */ |
4391 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4454 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4392 | |
4455 | |
|
|
4456 | case OP_VECRESIZE: |
|
|
4457 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4458 | s_return (x); |
|
|
4459 | |
4393 | case OP_VECREF: /* vector-ref */ |
4460 | case OP_VECREF: /* vector-ref */ |
4394 | { |
4461 | { |
4395 | int index; |
|
|
4396 | |
|
|
4397 | index = ivalue (cadr (args)); |
4462 | int index = ivalue_unchecked (cadr (args)); |
4398 | |
4463 | |
4399 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4464 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4400 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4465 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4401 | |
4466 | |
4402 | s_return (vector_elem (x, index)); |
4467 | s_return (vector_get (x, index)); |
4403 | } |
4468 | } |
4404 | |
4469 | |
4405 | case OP_VECSET: /* vector-set! */ |
4470 | case OP_VECSET: /* vector-set! */ |
4406 | { |
4471 | { |
4407 | int index; |
4472 | int index = ivalue_unchecked (cadr (args)); |
4408 | |
4473 | |
4409 | if (is_immutable (x)) |
4474 | if (is_immutable (x)) |
4410 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4475 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4411 | |
4476 | |
4412 | index = ivalue (cadr (args)); |
|
|
4413 | |
|
|
4414 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4477 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4415 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4478 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4416 | |
4479 | |
4417 | set_vector_elem (x, index, caddr (args)); |
4480 | vector_set (x, index, caddr (args)); |
4418 | s_return (x); |
4481 | s_return (x); |
4419 | } |
4482 | } |
4420 | } |
4483 | } |
4421 | |
4484 | |
4422 | return S_T; |
4485 | if (USE_ERROR_CHECKING) abort (); |
4423 | } |
4486 | } |
4424 | |
4487 | |
4425 | INTERFACE int |
|
|
4426 | is_list (SCHEME_P_ pointer a) |
|
|
4427 | { |
|
|
4428 | return list_length (SCHEME_A_ a) >= 0; |
|
|
4429 | } |
|
|
4430 | |
|
|
4431 | /* Result is: |
|
|
4432 | proper list: length |
|
|
4433 | circular list: -1 |
|
|
4434 | not even a pair: -2 |
|
|
4435 | dotted list: -2 minus length before dot |
|
|
4436 | */ |
|
|
4437 | INTERFACE int |
|
|
4438 | list_length (SCHEME_P_ pointer a) |
|
|
4439 | { |
|
|
4440 | int i = 0; |
|
|
4441 | pointer slow, fast; |
|
|
4442 | |
|
|
4443 | slow = fast = a; |
|
|
4444 | |
|
|
4445 | while (1) |
|
|
4446 | { |
|
|
4447 | if (fast == NIL) |
|
|
4448 | return i; |
|
|
4449 | |
|
|
4450 | if (!is_pair (fast)) |
|
|
4451 | return -2 - i; |
|
|
4452 | |
|
|
4453 | fast = cdr (fast); |
|
|
4454 | ++i; |
|
|
4455 | |
|
|
4456 | if (fast == NIL) |
|
|
4457 | return i; |
|
|
4458 | |
|
|
4459 | if (!is_pair (fast)) |
|
|
4460 | return -2 - i; |
|
|
4461 | |
|
|
4462 | ++i; |
|
|
4463 | fast = cdr (fast); |
|
|
4464 | |
|
|
4465 | /* Safe because we would have already returned if `fast' |
|
|
4466 | encountered a non-pair. */ |
|
|
4467 | slow = cdr (slow); |
|
|
4468 | |
|
|
4469 | if (fast == slow) |
|
|
4470 | { |
|
|
4471 | /* the fast pointer has looped back around and caught up |
|
|
4472 | with the slow pointer, hence the structure is circular, |
|
|
4473 | not of finite length, and therefore not a list */ |
|
|
4474 | return -1; |
|
|
4475 | } |
|
|
4476 | } |
|
|
4477 | } |
|
|
4478 | |
|
|
4479 | static pointer |
4488 | static int |
4480 | opexe_r (SCHEME_P_ enum scheme_opcodes op) |
4489 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4481 | { |
4490 | { |
4482 | pointer x = SCHEME_V->args; |
4491 | pointer x = SCHEME_V->args; |
4483 | |
4492 | |
4484 | for (;;) |
4493 | for (;;) |
4485 | { |
4494 | { |
… | |
… | |
4505 | } |
4514 | } |
4506 | |
4515 | |
4507 | s_return (S_T); |
4516 | s_return (S_T); |
4508 | } |
4517 | } |
4509 | |
4518 | |
4510 | static pointer |
4519 | static int |
4511 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4520 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4512 | { |
4521 | { |
4513 | pointer args = SCHEME_V->args; |
4522 | pointer args = SCHEME_V->args; |
4514 | pointer a = car (args); |
4523 | pointer a = car (args); |
4515 | pointer d = cdr (args); |
4524 | pointer d = cdr (args); |
4516 | int r; |
4525 | int r; |
4517 | |
4526 | |
4518 | switch (op) |
4527 | switch (op) |
4519 | { |
4528 | { |
4520 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4529 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4521 | 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; |
4522 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4531 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4523 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4532 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4524 | 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; |
4525 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4535 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4526 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4536 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4527 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4537 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4528 | 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 */ |
4529 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4539 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4530 | |
4540 | |
4531 | #if USE_CHAR_CLASSIFIERS |
4541 | #if USE_CHAR_CLASSIFIERS |
4532 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; |
4542 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4533 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; |
4543 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4534 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; |
4544 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4535 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; |
4545 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break; |
4536 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; |
4546 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break; |
4537 | #endif |
4547 | #endif |
4538 | |
4548 | |
4539 | #if USE_PORTS |
4549 | #if USE_PORTS |
4540 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4550 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4541 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
4551 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
… | |
… | |
4561 | } |
4571 | } |
4562 | |
4572 | |
4563 | s_retbool (r); |
4573 | s_retbool (r); |
4564 | } |
4574 | } |
4565 | |
4575 | |
4566 | static pointer |
4576 | static int |
4567 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4577 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4568 | { |
4578 | { |
4569 | pointer args = SCHEME_V->args; |
4579 | pointer args = SCHEME_V->args; |
4570 | pointer a = car (args); |
4580 | pointer a = car (args); |
4571 | pointer x, y; |
4581 | pointer x, y; |
… | |
… | |
4584 | } |
4594 | } |
4585 | else |
4595 | else |
4586 | s_return (SCHEME_V->code); |
4596 | s_return (SCHEME_V->code); |
4587 | |
4597 | |
4588 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4598 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4589 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4599 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4590 | s_return (SCHEME_V->value); |
4600 | s_return (SCHEME_V->value); |
4591 | |
4601 | |
4592 | #if USE_PORTS |
4602 | #if USE_PORTS |
4593 | |
4603 | |
4594 | case OP_WRITE: /* write */ |
4604 | case OP_WRITE: /* write */ |
… | |
… | |
4657 | putstr (SCHEME_A_ "\n"); |
4667 | putstr (SCHEME_A_ "\n"); |
4658 | |
4668 | |
4659 | if (SCHEME_V->interactive_repl) |
4669 | if (SCHEME_V->interactive_repl) |
4660 | s_goto (OP_T0LVL); |
4670 | s_goto (OP_T0LVL); |
4661 | else |
4671 | else |
4662 | return NIL; |
4672 | return -1; |
4663 | } |
4673 | } |
4664 | |
4674 | |
4665 | case OP_REVERSE: /* reverse */ |
4675 | case OP_REVERSE: /* reverse */ |
4666 | s_return (reverse (SCHEME_A_ a)); |
4676 | s_return (reverse (SCHEME_A_ a)); |
4667 | |
4677 | |
… | |
… | |
4724 | |
4734 | |
4725 | case OP_QUIT: /* quit */ |
4735 | case OP_QUIT: /* quit */ |
4726 | if (is_pair (args)) |
4736 | if (is_pair (args)) |
4727 | SCHEME_V->retcode = ivalue (a); |
4737 | SCHEME_V->retcode = ivalue (a); |
4728 | |
4738 | |
4729 | return NIL; |
4739 | return -1; |
4730 | |
4740 | |
4731 | case OP_GC: /* gc */ |
4741 | case OP_GC: /* gc */ |
4732 | gc (SCHEME_A_ NIL, NIL); |
4742 | gc (SCHEME_A_ NIL, NIL); |
4733 | s_return (S_T); |
4743 | s_return (S_T); |
4734 | |
4744 | |
… | |
… | |
4739 | SCHEME_V->gc_verbose = (a != S_F); |
4749 | SCHEME_V->gc_verbose = (a != S_F); |
4740 | s_retbool (was); |
4750 | s_retbool (was); |
4741 | } |
4751 | } |
4742 | |
4752 | |
4743 | case OP_NEWSEGMENT: /* new-segment */ |
4753 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4754 | #if 0 |
4744 | if (!is_pair (args) || !is_number (a)) |
4755 | if (!is_pair (args) || !is_number (a)) |
4745 | Error_0 ("new-segment: argument must be a number"); |
4756 | Error_0 ("new-segment: argument must be a number"); |
4746 | |
4757 | #endif |
4747 | alloc_cellseg (SCHEME_A_ (int)ivalue (a)); |
4758 | s_retbool (alloc_cellseg (SCHEME_A)); |
4748 | |
|
|
4749 | s_return (S_T); |
|
|
4750 | |
4759 | |
4751 | case OP_OBLIST: /* oblist */ |
4760 | case OP_OBLIST: /* oblist */ |
4752 | s_return (oblist_all_symbols (SCHEME_A)); |
4761 | s_return (oblist_all_symbols (SCHEME_A)); |
4753 | |
4762 | |
4754 | #if USE_PORTS |
4763 | #if USE_PORTS |
… | |
… | |
4781 | break; |
4790 | break; |
4782 | } |
4791 | } |
4783 | |
4792 | |
4784 | p = port_from_filename (SCHEME_A_ strvalue (a), prop); |
4793 | p = port_from_filename (SCHEME_A_ strvalue (a), prop); |
4785 | |
4794 | |
4786 | if (p == NIL) |
4795 | s_return (p == NIL ? S_F : p); |
4787 | s_return (S_F); |
|
|
4788 | |
|
|
4789 | s_return (p); |
|
|
4790 | } |
4796 | } |
4791 | |
4797 | |
4792 | # if USE_STRING_PORTS |
4798 | # if USE_STRING_PORTS |
4793 | |
4799 | |
4794 | case OP_OPEN_INSTRING: /* open-input-string */ |
4800 | case OP_OPEN_INSTRING: /* open-input-string */ |
… | |
… | |
4809 | } |
4815 | } |
4810 | |
4816 | |
4811 | p = port_from_string (SCHEME_A_ strvalue (a), |
4817 | p = port_from_string (SCHEME_A_ strvalue (a), |
4812 | strvalue (a) + strlength (a), prop); |
4818 | strvalue (a) + strlength (a), prop); |
4813 | |
4819 | |
4814 | if (p == NIL) |
4820 | s_return (p == NIL ? S_F : p); |
4815 | s_return (S_F); |
|
|
4816 | |
|
|
4817 | s_return (p); |
|
|
4818 | } |
4821 | } |
4819 | |
4822 | |
4820 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
4823 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
4821 | { |
4824 | { |
4822 | pointer p; |
4825 | pointer p; |
4823 | |
4826 | |
4824 | if (a == NIL) |
4827 | if (a == NIL) |
4825 | { |
|
|
4826 | p = port_from_scratch (SCHEME_A); |
4828 | p = port_from_scratch (SCHEME_A); |
4827 | |
|
|
4828 | if (p == NIL) |
|
|
4829 | s_return (S_F); |
|
|
4830 | } |
|
|
4831 | else |
4829 | else |
4832 | { |
|
|
4833 | p = port_from_string (SCHEME_A_ strvalue (a), |
4830 | p = port_from_string (SCHEME_A_ strvalue (a), |
4834 | strvalue (a) + strlength (a), port_output); |
4831 | strvalue (a) + strlength (a), port_output); |
4835 | |
4832 | |
4836 | if (p == NIL) |
4833 | s_return (p == NIL ? S_F : p); |
4837 | s_return (S_F); |
|
|
4838 | } |
|
|
4839 | |
|
|
4840 | s_return (p); |
|
|
4841 | } |
4834 | } |
4842 | |
4835 | |
4843 | case OP_GET_OUTSTRING: /* get-output-string */ |
4836 | case OP_GET_OUTSTRING: /* get-output-string */ |
4844 | { |
4837 | { |
4845 | port *p; |
4838 | port *p = port (a); |
4846 | |
4839 | |
4847 | if ((p = a->object.port)->kind & port_string) |
4840 | if (p->kind & port_string) |
4848 | { |
4841 | { |
4849 | off_t size; |
4842 | off_t size; |
4850 | char *str; |
4843 | char *str; |
4851 | |
4844 | |
4852 | size = p->rep.string.curr - p->rep.string.start + 1; |
4845 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4884 | case OP_CURR_ENV: /* current-environment */ |
4877 | case OP_CURR_ENV: /* current-environment */ |
4885 | s_return (SCHEME_V->envir); |
4878 | s_return (SCHEME_V->envir); |
4886 | |
4879 | |
4887 | } |
4880 | } |
4888 | |
4881 | |
4889 | abort (); |
4882 | if (USE_ERROR_CHECKING) abort (); |
4890 | } |
4883 | } |
4891 | |
4884 | |
4892 | static pointer |
4885 | static int |
4893 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4886 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4894 | { |
4887 | { |
4895 | pointer args = SCHEME_V->args; |
4888 | pointer args = SCHEME_V->args; |
4896 | pointer x; |
4889 | pointer x; |
4897 | |
4890 | |
… | |
… | |
4957 | int res; |
4950 | int res; |
4958 | |
4951 | |
4959 | if (is_pair (args)) |
4952 | if (is_pair (args)) |
4960 | p = car (args); |
4953 | p = car (args); |
4961 | |
4954 | |
4962 | res = p->object.port->kind & port_string; |
4955 | res = port (p)->kind & port_string; |
4963 | |
4956 | |
4964 | s_retbool (res); |
4957 | s_retbool (res); |
4965 | } |
4958 | } |
4966 | |
4959 | |
4967 | case OP_SET_INPORT: /* set-input-port */ |
4960 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
5026 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5019 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5027 | SCHEME_V->tok = token (SCHEME_A); |
5020 | SCHEME_V->tok = token (SCHEME_A); |
5028 | s_goto (OP_RDSEXPR); |
5021 | s_goto (OP_RDSEXPR); |
5029 | |
5022 | |
5030 | case TOK_ATOM: |
5023 | case TOK_ATOM: |
5031 | 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))); |
5032 | |
5034 | |
5033 | case TOK_DQUOTE: |
5035 | case TOK_DQUOTE: |
5034 | x = readstrexp (SCHEME_A); |
5036 | x = readstrexp (SCHEME_A_ '"'); |
5035 | |
5037 | |
5036 | if (x == S_F) |
5038 | if (x == S_F) |
5037 | Error_0 ("Error reading string"); |
5039 | Error_0 ("Error reading string"); |
5038 | |
5040 | |
5039 | setimmutable (x); |
5041 | setimmutable (x); |
… | |
… | |
5051 | s_goto (OP_EVAL); |
5053 | s_goto (OP_EVAL); |
5052 | } |
5054 | } |
5053 | } |
5055 | } |
5054 | |
5056 | |
5055 | case TOK_SHARP_CONST: |
5057 | case TOK_SHARP_CONST: |
5056 | 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) |
5057 | Error_0 ("undefined sharp expression"); |
5059 | Error_0 ("undefined sharp expression"); |
5058 | else |
5060 | else |
5059 | s_return (x); |
5061 | s_return (x); |
5060 | |
5062 | |
5061 | default: |
5063 | default: |
… | |
… | |
5213 | putstr (SCHEME_A_ ")"); |
5215 | putstr (SCHEME_A_ ")"); |
5214 | s_return (S_T); |
5216 | s_return (S_T); |
5215 | } |
5217 | } |
5216 | else |
5218 | else |
5217 | { |
5219 | { |
5218 | pointer elem = vector_elem (vec, i); |
5220 | pointer elem = vector_get (vec, i); |
5219 | |
5221 | |
5220 | ivalue_unchecked (cdr (args)) = i + 1; |
5222 | ivalue_unchecked (cdr (args)) = i + 1; |
5221 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5223 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5222 | SCHEME_V->args = elem; |
5224 | SCHEME_V->args = elem; |
5223 | |
5225 | |
… | |
… | |
5227 | s_goto (OP_P0LIST); |
5229 | s_goto (OP_P0LIST); |
5228 | } |
5230 | } |
5229 | } |
5231 | } |
5230 | } |
5232 | } |
5231 | |
5233 | |
5232 | abort (); |
5234 | if (USE_ERROR_CHECKING) abort (); |
5233 | } |
5235 | } |
5234 | |
5236 | |
5235 | static pointer |
5237 | static int |
5236 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5238 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5237 | { |
5239 | { |
5238 | pointer args = SCHEME_V->args; |
5240 | pointer args = SCHEME_V->args; |
5239 | pointer a = car (args); |
5241 | pointer a = car (args); |
5240 | pointer x, y; |
5242 | pointer x, y; |
… | |
… | |
5283 | |
5285 | |
5284 | case OP_CLOSUREP: /* closure? */ |
5286 | case OP_CLOSUREP: /* closure? */ |
5285 | /* |
5287 | /* |
5286 | * Note, macro object is also a closure. |
5288 | * Note, macro object is also a closure. |
5287 | * Therefore, (closure? <#MACRO>) ==> #t |
5289 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5290 | * (schmorp) well, obviously not, fix? TODO |
5288 | */ |
5291 | */ |
5289 | s_retbool (is_closure (a)); |
5292 | s_retbool (is_closure (a)); |
5290 | |
5293 | |
5291 | case OP_MACROP: /* macro? */ |
5294 | case OP_MACROP: /* macro? */ |
5292 | s_retbool (is_macro (a)); |
5295 | s_retbool (is_macro (a)); |
5293 | } |
5296 | } |
5294 | |
5297 | |
5295 | abort (); |
5298 | if (USE_ERROR_CHECKING) abort (); |
5296 | } |
5299 | } |
5297 | |
5300 | |
|
|
5301 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5298 | typedef pointer (*dispatch_func) (SCHEME_P_ enum scheme_opcodes); |
5302 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5299 | |
5303 | |
5300 | typedef int (*test_predicate) (pointer); |
5304 | typedef int (*test_predicate)(pointer); |
5301 | static int |
5305 | static int |
5302 | is_any (pointer p) |
5306 | tst_any (pointer p) |
5303 | { |
5307 | { |
5304 | return 1; |
5308 | return 1; |
5305 | } |
5309 | } |
5306 | |
5310 | |
5307 | static int |
5311 | static int |
5308 | is_nonneg (pointer p) |
5312 | tst_inonneg (pointer p) |
5309 | { |
5313 | { |
5310 | return ivalue (p) >= 0 && is_integer (p); |
5314 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
|
|
5315 | } |
|
|
5316 | |
|
|
5317 | static int |
|
|
5318 | tst_is_list (SCHEME_P_ pointer p) |
|
|
5319 | { |
|
|
5320 | return p == NIL || is_pair (p); |
5311 | } |
5321 | } |
5312 | |
5322 | |
5313 | /* Correspond carefully with following defines! */ |
5323 | /* Correspond carefully with following defines! */ |
5314 | static struct |
5324 | static struct |
5315 | { |
5325 | { |
5316 | test_predicate fct; |
5326 | test_predicate fct; |
5317 | const char *kind; |
5327 | const char *kind; |
5318 | } tests[] = |
5328 | } tests[] = { |
5319 | { |
5329 | { tst_any , 0 }, |
5320 | { 0, 0}, /* unused */ |
5330 | { is_string , "string" }, |
5321 | { is_any, 0}, |
5331 | { is_symbol , "symbol" }, |
5322 | { is_string, "string" }, |
5332 | { is_port , "port" }, |
5323 | { is_symbol, "symbol" }, |
|
|
5324 | { is_port, "port" }, |
|
|
5325 | { is_inport, "input port" }, |
5333 | { is_inport , "input port" }, |
5326 | { is_outport, "output port" }, |
5334 | { is_outport , "output port" }, |
5327 | { is_environment, "environment" }, |
5335 | { is_environment, "environment" }, |
5328 | { is_pair, "pair" }, |
5336 | { is_pair , "pair" }, |
5329 | { 0, "pair or '()" }, |
5337 | { 0 , "pair or '()" }, |
5330 | { is_character, "character" }, |
5338 | { is_character , "character" }, |
5331 | { is_vector, "vector" }, |
5339 | { is_vector , "vector" }, |
5332 | { is_number, "number" }, |
5340 | { is_number , "number" }, |
5333 | { is_integer, "integer" }, |
5341 | { is_integer , "integer" }, |
5334 | { is_nonneg, "non-negative integer" } |
5342 | { tst_inonneg , "non-negative integer" } |
5335 | }; |
5343 | }; |
5336 | |
5344 | |
5337 | #define TST_NONE 0 /* TST_NONE used for standard procedures, for internal ops, 0 is used */ |
5345 | #define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ |
5338 | #define TST_ANY "\001" |
5346 | #define TST_ANY "\001" |
5339 | #define TST_STRING "\002" |
5347 | #define TST_STRING "\002" |
5340 | #define TST_SYMBOL "\003" |
5348 | #define TST_SYMBOL "\003" |
5341 | #define TST_PORT "\004" |
5349 | #define TST_PORT "\004" |
5342 | #define TST_INPORT "\005" |
5350 | #define TST_INPORT "\005" |
… | |
… | |
5348 | #define TST_VECTOR "\013" |
5356 | #define TST_VECTOR "\013" |
5349 | #define TST_NUMBER "\014" |
5357 | #define TST_NUMBER "\014" |
5350 | #define TST_INTEGER "\015" |
5358 | #define TST_INTEGER "\015" |
5351 | #define TST_NATURAL "\016" |
5359 | #define TST_NATURAL "\016" |
5352 | |
5360 | |
|
|
5361 | #define INF_ARG 0xff |
|
|
5362 | #define UNNAMED_OP "" |
|
|
5363 | |
|
|
5364 | static const char opnames[] = |
|
|
5365 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
|
|
5366 | #include "opdefines.h" |
|
|
5367 | #undef OP_DEF |
|
|
5368 | ; |
|
|
5369 | |
|
|
5370 | static const char * |
|
|
5371 | opname (int idx) |
|
|
5372 | { |
|
|
5373 | const char *name = opnames; |
|
|
5374 | |
|
|
5375 | /* should do this at compile time, but would require external program, right? */ |
|
|
5376 | while (idx--) |
|
|
5377 | name += strlen (name) + 1; |
|
|
5378 | |
|
|
5379 | return *name ? name : "ILLEGAL"; |
|
|
5380 | } |
|
|
5381 | |
|
|
5382 | static const char * |
|
|
5383 | procname (pointer x) |
|
|
5384 | { |
|
|
5385 | return opname (procnum (x)); |
|
|
5386 | } |
|
|
5387 | |
5353 | typedef struct |
5388 | typedef struct |
5354 | { |
5389 | { |
5355 | dispatch_func func; |
5390 | uint8_t func; |
5356 | char *name; |
5391 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
|
|
5392 | uint8_t builtin; |
|
|
5393 | #if USE_ERROR_CHECKING |
5357 | int min_arity; |
5394 | uint8_t min_arity; |
5358 | int max_arity; |
5395 | uint8_t max_arity; |
5359 | char arg_tests_encoding[3]; |
5396 | char arg_tests_encoding[3]; |
|
|
5397 | #endif |
5360 | } op_code_info; |
5398 | } op_code_info; |
5361 | |
5399 | |
5362 | #define INF_ARG 0xffff |
|
|
5363 | |
|
|
5364 | static op_code_info dispatch_table[] = { |
5400 | static const op_code_info dispatch_table[] = { |
|
|
5401 | #if USE_ERROR_CHECKING |
5365 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { opexe_ ## func, name, 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 |
5366 | #include "opdefines.h" |
5406 | #include "opdefines.h" |
5367 | #undef OP_DEF |
5407 | #undef OP_DEF |
5368 | {0} |
5408 | {0} |
5369 | }; |
5409 | }; |
5370 | |
5410 | |
5371 | static const char * |
|
|
5372 | procname (pointer x) |
|
|
5373 | { |
|
|
5374 | int n = procnum (x); |
|
|
5375 | const char *name = dispatch_table[n].name; |
|
|
5376 | |
|
|
5377 | if (name == 0) |
|
|
5378 | name = "ILLEGAL!"; |
|
|
5379 | |
|
|
5380 | return name; |
|
|
5381 | } |
|
|
5382 | |
|
|
5383 | /* kernel of this interpreter */ |
5411 | /* kernel of this interpreter */ |
5384 | static void |
5412 | static void ecb_hot |
5385 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5413 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5386 | { |
5414 | { |
5387 | SCHEME_V->op = op; |
5415 | SCHEME_V->op = op; |
5388 | |
5416 | |
5389 | for (;;) |
5417 | for (;;) |
5390 | { |
5418 | { |
5391 | op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5419 | const op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5392 | |
5420 | |
5393 | #if USE_ERROR_CHECKING |
5421 | #if USE_ERROR_CHECKING |
5394 | if (pcd->name) /* if built-in function, check arguments */ |
5422 | if (pcd->builtin) /* if built-in function, check arguments */ |
5395 | { |
5423 | { |
5396 | int ok = 1; |
|
|
5397 | char msg[STRBUFFSIZE]; |
5424 | char msg[STRBUFFSIZE]; |
5398 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5425 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5399 | |
5426 | |
5400 | /* Check number of arguments */ |
5427 | /* Check number of arguments */ |
5401 | if (ecb_expect_false (n < pcd->min_arity)) |
5428 | if (ecb_expect_false (n < pcd->min_arity)) |
5402 | { |
5429 | { |
5403 | ok = 0; |
|
|
5404 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5430 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5405 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); |
5431 | opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); |
|
|
5432 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5433 | continue; |
5406 | } |
5434 | } |
5407 | else if (ecb_expect_false (n > pcd->max_arity)) |
5435 | else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG)) |
5408 | { |
5436 | { |
5409 | ok = 0; |
|
|
5410 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5437 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5411 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
5438 | opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
|
|
5439 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5440 | continue; |
5412 | } |
5441 | } |
5413 | |
5442 | else |
5414 | if (ecb_expect_false (ok)) |
|
|
5415 | { |
5443 | { |
5416 | if (*pcd->arg_tests_encoding) |
5444 | if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */ |
5417 | { |
5445 | { |
5418 | int i = 0; |
5446 | int i = 0; |
5419 | int j; |
5447 | int j; |
5420 | const char *t = pcd->arg_tests_encoding; |
5448 | const char *t = pcd->arg_tests_encoding; |
5421 | pointer arglist = SCHEME_V->args; |
5449 | pointer arglist = SCHEME_V->args; |
… | |
… | |
5424 | { |
5452 | { |
5425 | pointer arg = car (arglist); |
5453 | pointer arg = car (arglist); |
5426 | |
5454 | |
5427 | j = t[0]; |
5455 | j = t[0]; |
5428 | |
5456 | |
|
|
5457 | /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */ |
5429 | if (j == TST_LIST[0]) |
5458 | if (j == TST_LIST[0]) |
5430 | { |
5459 | { |
5431 | if (arg != NIL && !is_pair (arg)) |
5460 | if (!tst_is_list (SCHEME_A_ arg)) |
5432 | break; |
5461 | break; |
5433 | } |
5462 | } |
5434 | else |
5463 | else |
5435 | { |
5464 | { |
5436 | if (!tests[j].fct (arg)) |
5465 | if (!tests[j - 1].fct (arg)) |
5437 | break; |
5466 | break; |
5438 | } |
5467 | } |
5439 | |
5468 | |
5440 | 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 */ |
5441 | t++; |
5470 | t++; |
5442 | |
5471 | |
5443 | arglist = cdr (arglist); |
5472 | arglist = cdr (arglist); |
5444 | i++; |
5473 | i++; |
5445 | } |
5474 | } |
5446 | while (i < n); |
5475 | while (i < n); |
5447 | |
5476 | |
5448 | if (i < n) |
5477 | if (i < n) |
5449 | { |
5478 | { |
5450 | ok = 0; |
|
|
5451 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); |
5479 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind); |
|
|
5480 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5481 | continue; |
5452 | } |
5482 | } |
5453 | } |
5483 | } |
5454 | } |
5484 | } |
5455 | |
|
|
5456 | if (!ok) |
|
|
5457 | { |
|
|
5458 | if (xError_1 (SCHEME_A_ msg, 0) == NIL) |
|
|
5459 | return; |
|
|
5460 | |
|
|
5461 | pcd = dispatch_table + SCHEME_V->op; |
|
|
5462 | } |
|
|
5463 | } |
5485 | } |
5464 | #endif |
5486 | #endif |
5465 | |
5487 | |
5466 | ok_to_freely_gc (SCHEME_A); |
5488 | ok_to_freely_gc (SCHEME_A); |
5467 | |
5489 | |
|
|
5490 | static const dispatch_func dispatch_funcs[] = { |
|
|
5491 | opexe_0, |
|
|
5492 | opexe_1, |
|
|
5493 | opexe_2, |
|
|
5494 | opexe_3, |
|
|
5495 | opexe_4, |
|
|
5496 | opexe_5, |
|
|
5497 | opexe_6, |
|
|
5498 | }; |
|
|
5499 | |
5468 | if (ecb_expect_false (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL)) |
5500 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5469 | return; |
5501 | return; |
5470 | |
5502 | |
5471 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5503 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5472 | { |
5504 | { |
5473 | xwrstr ("No memory!\n"); |
5505 | putstr (SCHEME_A_ "No memory!\n"); |
5474 | return; |
5506 | return; |
5475 | } |
5507 | } |
5476 | } |
5508 | } |
5477 | } |
5509 | } |
5478 | |
5510 | |
… | |
… | |
5497 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5529 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5498 | { |
5530 | { |
5499 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5531 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5500 | set_typeflag (y, (T_PROC | T_ATOM)); |
5532 | set_typeflag (y, (T_PROC | T_ATOM)); |
5501 | ivalue_unchecked (y) = op; |
5533 | ivalue_unchecked (y) = op; |
5502 | set_num_integer (y); |
|
|
5503 | return y; |
5534 | return y; |
5504 | } |
5535 | } |
5505 | |
5536 | |
5506 | /* 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! */ |
5507 | static int |
5538 | static int |
5508 | syntaxnum (pointer p) |
5539 | syntaxnum (pointer p) |
5509 | { |
5540 | { |
5510 | const char *s = strvalue (car (p)); |
5541 | const char *s = strvalue (p); |
5511 | |
5542 | |
5512 | switch (strlength (car (p))) |
5543 | switch (strlength (p)) |
5513 | { |
5544 | { |
5514 | case 2: |
5545 | case 2: |
5515 | if (s[0] == 'i') |
5546 | if (s[0] == 'i') |
5516 | return OP_IF0; /* if */ |
5547 | return OP_IF0; /* if */ |
5517 | else |
5548 | else |
… | |
… | |
5572 | return OP_C0STREAM; /* cons-stream */ |
5603 | return OP_C0STREAM; /* cons-stream */ |
5573 | } |
5604 | } |
5574 | } |
5605 | } |
5575 | |
5606 | |
5576 | #if USE_MULTIPLICITY |
5607 | #if USE_MULTIPLICITY |
5577 | scheme * |
5608 | ecb_cold scheme * |
5578 | scheme_init_new () |
5609 | scheme_init_new () |
5579 | { |
5610 | { |
5580 | scheme *sc = malloc (sizeof (scheme)); |
5611 | scheme *sc = malloc (sizeof (scheme)); |
5581 | |
5612 | |
5582 | if (!scheme_init (SCHEME_A)) |
5613 | if (!scheme_init (SCHEME_A)) |
… | |
… | |
5587 | else |
5618 | else |
5588 | return sc; |
5619 | return sc; |
5589 | } |
5620 | } |
5590 | #endif |
5621 | #endif |
5591 | |
5622 | |
5592 | int |
5623 | ecb_cold int |
5593 | scheme_init (SCHEME_P) |
5624 | scheme_init (SCHEME_P) |
5594 | { |
5625 | { |
5595 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5626 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5596 | 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)); |
5597 | |
5634 | |
5598 | num_set_fixnum (num_zero, 1); |
5635 | num_set_fixnum (num_zero, 1); |
5599 | num_set_ivalue (num_zero, 0); |
5636 | num_set_ivalue (num_zero, 0); |
5600 | num_set_fixnum (num_one, 1); |
5637 | num_set_fixnum (num_one, 1); |
5601 | num_set_ivalue (num_one, 1); |
5638 | num_set_ivalue (num_one, 1); |
… | |
… | |
5613 | SCHEME_V->save_inport = NIL; |
5650 | SCHEME_V->save_inport = NIL; |
5614 | SCHEME_V->loadport = NIL; |
5651 | SCHEME_V->loadport = NIL; |
5615 | SCHEME_V->nesting = 0; |
5652 | SCHEME_V->nesting = 0; |
5616 | SCHEME_V->interactive_repl = 0; |
5653 | SCHEME_V->interactive_repl = 0; |
5617 | |
5654 | |
5618 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5655 | if (!alloc_cellseg (SCHEME_A)) |
5619 | { |
5656 | { |
5620 | #if USE_ERROR_CHECKING |
5657 | #if USE_ERROR_CHECKING |
5621 | SCHEME_V->no_memory = 1; |
5658 | SCHEME_V->no_memory = 1; |
5622 | return 0; |
5659 | return 0; |
5623 | #endif |
5660 | #endif |
… | |
… | |
5670 | |
5707 | |
5671 | for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) |
5708 | for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) |
5672 | assign_syntax (SCHEME_A_ syntax_names[i]); |
5709 | assign_syntax (SCHEME_A_ syntax_names[i]); |
5673 | } |
5710 | } |
5674 | |
5711 | |
|
|
5712 | // TODO: should iterate via strlen, to avoid n² complexity |
5675 | for (i = 0; i < n; i++) |
5713 | for (i = 0; i < n; i++) |
5676 | if (dispatch_table[i].name != 0) |
5714 | if (dispatch_table[i].builtin) |
5677 | assign_proc (SCHEME_A_ i, dispatch_table[i].name); |
5715 | assign_proc (SCHEME_A_ i, opname (i)); |
5678 | |
5716 | |
5679 | /* initialization of global pointers to special symbols */ |
5717 | /* initialization of global pointers to special symbols */ |
5680 | SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); |
5718 | SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); |
5681 | SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); |
5719 | SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); |
5682 | SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); |
5720 | SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); |
… | |
… | |
5721 | scheme_set_external_data (SCHEME_P_ void *p) |
5759 | scheme_set_external_data (SCHEME_P_ void *p) |
5722 | { |
5760 | { |
5723 | SCHEME_V->ext_data = p; |
5761 | SCHEME_V->ext_data = p; |
5724 | } |
5762 | } |
5725 | |
5763 | |
5726 | void |
5764 | ecb_cold void |
5727 | scheme_deinit (SCHEME_P) |
5765 | scheme_deinit (SCHEME_P) |
5728 | { |
5766 | { |
5729 | int i; |
5767 | int i; |
5730 | |
5768 | |
5731 | #if SHOW_ERROR_LINE |
5769 | #if SHOW_ERROR_LINE |
… | |
… | |
5952 | # endif |
5990 | # endif |
5953 | int fin; |
5991 | int fin; |
5954 | char *file_name = InitFile; |
5992 | char *file_name = InitFile; |
5955 | int retcode; |
5993 | int retcode; |
5956 | int isfile = 1; |
5994 | int isfile = 1; |
|
|
5995 | system ("ps v $PPID");//D |
5957 | |
5996 | |
5958 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5997 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5959 | { |
5998 | { |
5960 | xwrstr ("Usage: tinyscheme -?\n"); |
5999 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
5961 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6000 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
5962 | xwrstr ("followed by\n"); |
6001 | putstr (SCHEME_A_ "followed by\n"); |
5963 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6002 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
5964 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6003 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
5965 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6004 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
5966 | xwrstr ("Use - as filename for stdin.\n"); |
6005 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
5967 | return 1; |
6006 | return 1; |
5968 | } |
6007 | } |
5969 | |
6008 | |
5970 | if (!scheme_init (SCHEME_A)) |
6009 | if (!scheme_init (SCHEME_A)) |
5971 | { |
6010 | { |
5972 | xwrstr ("Could not initialize!\n"); |
6011 | putstr (SCHEME_A_ "Could not initialize!\n"); |
5973 | return 2; |
6012 | return 2; |
5974 | } |
6013 | } |
5975 | |
6014 | |
5976 | # if USE_PORTS |
6015 | # if USE_PORTS |
5977 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6016 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
6022 | fin = open (file_name, O_RDONLY); |
6061 | fin = open (file_name, O_RDONLY); |
6023 | #endif |
6062 | #endif |
6024 | |
6063 | |
6025 | if (isfile && fin < 0) |
6064 | if (isfile && fin < 0) |
6026 | { |
6065 | { |
6027 | 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"); |
6028 | } |
6067 | } |
6029 | else |
6068 | else |
6030 | { |
6069 | { |
6031 | if (isfile) |
6070 | if (isfile) |
6032 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6071 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
… | |
… | |
6036 | #if USE_PORTS |
6075 | #if USE_PORTS |
6037 | if (!isfile || fin != STDIN_FILENO) |
6076 | if (!isfile || fin != STDIN_FILENO) |
6038 | { |
6077 | { |
6039 | if (SCHEME_V->retcode != 0) |
6078 | if (SCHEME_V->retcode != 0) |
6040 | { |
6079 | { |
6041 | 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"); |
6042 | } |
6081 | } |
6043 | |
6082 | |
6044 | if (isfile) |
6083 | if (isfile) |
6045 | close (fin); |
6084 | close (fin); |
6046 | } |
6085 | } |