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