… | |
… | |
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> |
… | |
… | |
34 | |
38 | |
35 | #include <sys/types.h> |
39 | #include <sys/types.h> |
36 | #include <sys/stat.h> |
40 | #include <sys/stat.h> |
37 | #include <fcntl.h> |
41 | #include <fcntl.h> |
38 | |
42 | |
|
|
43 | #if !USE_ERROR_CHECKING |
|
|
44 | # define NDEBUG |
|
|
45 | #endif |
|
|
46 | |
|
|
47 | #include <assert.h> |
|
|
48 | #include <stdlib.h> |
39 | #include <string.h> |
49 | #include <string.h> |
40 | #include <stdlib.h> |
|
|
41 | |
50 | |
42 | #include <limits.h> |
51 | #include <limits.h> |
43 | #include <inttypes.h> |
52 | #include <inttypes.h> |
44 | #include <float.h> |
53 | #include <float.h> |
45 | //#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 |
46 | |
63 | |
47 | enum { |
64 | enum { |
48 | TOK_EOF, |
65 | TOK_EOF, |
49 | TOK_LPAREN, |
66 | TOK_LPAREN, |
50 | TOK_RPAREN, |
67 | TOK_RPAREN, |
51 | TOK_DOT, |
68 | TOK_DOT, |
52 | TOK_ATOM, |
69 | TOK_ATOM, |
|
|
70 | TOK_DOTATOM, /* atom name starting with '.' */ |
|
|
71 | TOK_STRATOM, /* atom name enclosed in | */ |
53 | TOK_QUOTE, |
72 | TOK_QUOTE, |
54 | TOK_DQUOTE, |
73 | TOK_DQUOTE, |
55 | TOK_BQUOTE, |
74 | TOK_BQUOTE, |
56 | TOK_COMMA, |
75 | TOK_COMMA, |
57 | TOK_ATMARK, |
76 | TOK_ATMARK, |
… | |
… | |
59 | TOK_SHARP_CONST, |
78 | TOK_SHARP_CONST, |
60 | TOK_VEC |
79 | TOK_VEC |
61 | }; |
80 | }; |
62 | |
81 | |
63 | #define BACKQUOTE '`' |
82 | #define BACKQUOTE '`' |
64 | #define DELIMITERS "()\";\f\t\v\n\r " |
83 | #define WHITESPACE " \t\r\n\v\f" |
|
|
84 | #define DELIMITERS "()\";" WHITESPACE |
65 | |
85 | |
66 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
86 | #define NIL POINTER (&SCHEME_V->xNIL) |
67 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
87 | #define S_T POINTER (&SCHEME_V->xT) |
68 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
88 | #define S_F POINTER (&SCHEME_V->xF) |
69 | #define S_SINK (&SCHEME_V->xsink) |
89 | #define S_SINK POINTER (&SCHEME_V->xsink) |
70 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
90 | #define S_EOF POINTER (&SCHEME_V->xEOF_OBJ) |
71 | |
91 | |
72 | #if !USE_MULTIPLICITY |
92 | #if !USE_MULTIPLICITY |
73 | static scheme sc; |
93 | static scheme sc; |
74 | #endif |
94 | #endif |
75 | |
95 | |
76 | static void |
96 | ecb_cold static void |
77 | xbase (char *s, long n, int base) |
97 | xbase (char *s, long n, int base) |
78 | { |
98 | { |
79 | if (n < 0) |
99 | if (n < 0) |
80 | { |
100 | { |
81 | *s++ = '-'; |
101 | *s++ = '-'; |
… | |
… | |
83 | } |
103 | } |
84 | |
104 | |
85 | char *p = s; |
105 | char *p = s; |
86 | |
106 | |
87 | do { |
107 | do { |
88 | *p++ = '0' + n % base; |
108 | *p++ = "0123456789abcdef"[n % base]; |
89 | n /= base; |
109 | n /= base; |
90 | } while (n); |
110 | } while (n); |
91 | |
111 | |
92 | *p-- = 0; |
112 | *p-- = 0; |
93 | |
113 | |
… | |
… | |
96 | char x = *s; *s = *p; *p = x; |
116 | char x = *s; *s = *p; *p = x; |
97 | --p; ++s; |
117 | --p; ++s; |
98 | } |
118 | } |
99 | } |
119 | } |
100 | |
120 | |
101 | static void |
121 | ecb_cold static void |
102 | xnum (char *s, long n) |
122 | xnum (char *s, long n) |
103 | { |
123 | { |
104 | xbase (s, n, 10); |
124 | xbase (s, n, 10); |
105 | } |
125 | } |
106 | |
126 | |
107 | static void |
127 | ecb_cold static void |
108 | xwrstr (const char *s) |
128 | putnum (SCHEME_P_ long n) |
109 | { |
|
|
110 | write (1, s, strlen (s)); |
|
|
111 | } |
|
|
112 | |
|
|
113 | static void |
|
|
114 | xwrnum (long n) |
|
|
115 | { |
129 | { |
116 | char buf[64]; |
130 | char buf[64]; |
117 | |
131 | |
118 | xnum (buf, n); |
132 | xnum (buf, n); |
119 | xwrstr (buf); |
133 | putstr (SCHEME_A_ buf); |
120 | } |
134 | } |
121 | |
135 | |
122 | static char |
136 | ecb_cold static char |
123 | xtoupper (char c) |
137 | xtoupper (char c) |
124 | { |
138 | { |
125 | if (c >= 'a' && c <= 'z') |
139 | if (c >= 'a' && c <= 'z') |
126 | c -= 'a' - 'A'; |
140 | c -= 'a' - 'A'; |
127 | |
141 | |
128 | return c; |
142 | return c; |
129 | } |
143 | } |
130 | |
144 | |
131 | static char |
145 | ecb_cold static char |
132 | xtolower (char c) |
146 | xtolower (char c) |
133 | { |
147 | { |
134 | if (c >= 'A' && c <= 'Z') |
148 | if (c >= 'A' && c <= 'Z') |
135 | c += 'a' - 'A'; |
149 | c += 'a' - 'A'; |
136 | |
150 | |
137 | return c; |
151 | return c; |
138 | } |
152 | } |
139 | |
153 | |
140 | static int |
154 | ecb_cold static int |
141 | xisdigit (char c) |
155 | xisdigit (char c) |
142 | { |
156 | { |
143 | return c >= '0' && c <= '9'; |
157 | return c >= '0' && c <= '9'; |
144 | } |
158 | } |
145 | |
159 | |
146 | #define toupper(c) xtoupper (c) |
160 | #define toupper(c) xtoupper (c) |
147 | #define tolower(c) xtolower (c) |
161 | #define tolower(c) xtolower (c) |
148 | #define isdigit(c) xisdigit (c) |
162 | #define isdigit(c) xisdigit (c) |
149 | |
163 | |
150 | #if USE_IGNORECASE |
164 | #if USE_IGNORECASE |
151 | static const char * |
165 | ecb_cold static const char * |
152 | xstrlwr (char *s) |
166 | xstrlwr (char *s) |
153 | { |
167 | { |
154 | const char *p = s; |
168 | const char *p = s; |
155 | |
169 | |
156 | while (*s) |
170 | while (*s) |
… | |
… | |
176 | |
190 | |
177 | #ifndef InitFile |
191 | #ifndef InitFile |
178 | # define InitFile "init.scm" |
192 | # define InitFile "init.scm" |
179 | #endif |
193 | #endif |
180 | |
194 | |
181 | #ifndef FIRST_CELLSEGS |
|
|
182 | # define FIRST_CELLSEGS 3 |
|
|
183 | #endif |
|
|
184 | |
|
|
185 | enum scheme_types |
195 | enum scheme_types |
186 | { |
196 | { |
187 | T_INTEGER, |
197 | T_INTEGER, |
|
|
198 | T_CHARACTER, |
188 | T_REAL, |
199 | T_REAL, |
189 | T_STRING, |
200 | T_STRING, |
190 | T_SYMBOL, |
201 | T_SYMBOL, |
191 | T_PROC, |
202 | T_PROC, |
192 | T_PAIR, /* also used for free cells */ |
203 | T_PAIR, /* also used for free cells */ |
193 | T_CLOSURE, |
204 | T_CLOSURE, |
|
|
205 | T_MACRO, |
194 | T_CONTINUATION, |
206 | T_CONTINUATION, |
195 | T_FOREIGN, |
207 | T_FOREIGN, |
196 | T_CHARACTER, |
|
|
197 | T_PORT, |
208 | T_PORT, |
198 | T_VECTOR, |
209 | T_VECTOR, |
199 | T_MACRO, |
|
|
200 | T_PROMISE, |
210 | T_PROMISE, |
201 | T_ENVIRONMENT, |
211 | T_ENVIRONMENT, |
202 | /* one more... */ |
212 | /* one more... */ |
203 | T_NUM_SYSTEM_TYPES |
213 | T_NUM_SYSTEM_TYPES |
204 | }; |
214 | }; |
… | |
… | |
240 | 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); |
241 | static num num_intdiv (num a, num b); |
251 | static num num_intdiv (num a, num b); |
242 | static num num_rem (num a, num b); |
252 | static num num_rem (num a, num b); |
243 | static num num_mod (num a, num b); |
253 | static num num_mod (num a, num b); |
244 | |
254 | |
245 | #if USE_MATH |
|
|
246 | static double round_per_R5RS (double x); |
|
|
247 | #endif |
|
|
248 | static int is_zero_rvalue (RVALUE x); |
255 | static int is_zero_rvalue (RVALUE x); |
249 | |
256 | |
250 | static num num_zero; |
257 | static num num_zero; |
251 | static num num_one; |
258 | static num num_one; |
252 | |
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 | |
253 | /* macros for cell operations */ |
264 | /* macros for cell operations */ |
254 | #define typeflag(p) ((p)->flag + 0) |
265 | #define typeflag(p) (CELL(p)->flag + 0) |
255 | #define set_typeflag(p,v) ((p)->flag = (v)) |
266 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
256 | #define type(p) (typeflag (p) & T_MASKTYPE) |
267 | #define type(p) (typeflag (p) & T_MASKTYPE) |
257 | |
268 | |
258 | INTERFACE int |
269 | INTERFACE int |
259 | is_string (pointer p) |
270 | is_string (pointer p) |
260 | { |
271 | { |
261 | return type (p) == T_STRING; |
272 | return type (p) == T_STRING; |
262 | } |
273 | } |
263 | |
274 | |
264 | #define strvalue(p) ((p)->object.string.svalue) |
275 | #define strvalue(p) (CELL(p)->object.string.svalue) |
265 | #define strlength(p) ((p)->object.string.length) |
276 | #define strlength(p) (CELL(p)->object.string.length) |
266 | |
277 | |
267 | INTERFACE int |
278 | INTERFACE int |
268 | is_vector (pointer p) |
279 | is_vector (pointer p) |
269 | { |
280 | { |
270 | return type (p) == T_VECTOR; |
281 | return type (p) == T_VECTOR; |
271 | } |
282 | } |
272 | |
283 | |
273 | #define vecvalue(p) ((p)->object.vector.vvalue) |
284 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
274 | #define veclength(p) ((p)->object.vector.length) |
285 | #define veclength(p) (CELL(p)->object.vector.length) |
275 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
286 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
276 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
287 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
277 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
288 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
278 | |
289 | |
279 | INTERFACE int |
290 | INTERFACE int |
… | |
… | |
305 | string_value (pointer p) |
316 | string_value (pointer p) |
306 | { |
317 | { |
307 | return strvalue (p); |
318 | return strvalue (p); |
308 | } |
319 | } |
309 | |
320 | |
310 | #define ivalue_unchecked(p) (p)->object.ivalue |
321 | #define ivalue_unchecked(p) CELL(p)->object.ivalue |
311 | #define set_ivalue(p,v) (p)->object.ivalue = (v) |
322 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
312 | |
323 | |
313 | #if USE_REAL |
324 | #if USE_REAL |
314 | #define rvalue_unchecked(p) (p)->object.rvalue |
325 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
315 | #define set_rvalue(p,v) (p)->object.rvalue = (v) |
326 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
316 | #else |
327 | #else |
317 | #define rvalue_unchecked(p) (p)->object.ivalue |
328 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
318 | #define set_rvalue(p,v) (p)->object.ivalue = (v) |
329 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
319 | #endif |
330 | #endif |
320 | |
331 | |
321 | INTERFACE long |
332 | INTERFACE long |
322 | charvalue (pointer p) |
333 | charvalue (pointer p) |
323 | { |
334 | { |
324 | return ivalue_unchecked (p); |
335 | return ivalue_unchecked (p); |
325 | } |
336 | } |
326 | |
337 | |
|
|
338 | #define port(p) CELL(p)->object.port |
|
|
339 | #define set_port(p,v) port(p) = (v) |
327 | INTERFACE int |
340 | INTERFACE int |
328 | is_port (pointer p) |
341 | is_port (pointer p) |
329 | { |
342 | { |
330 | return type (p) == T_PORT; |
343 | return type (p) == T_PORT; |
331 | } |
344 | } |
332 | |
345 | |
333 | INTERFACE int |
346 | INTERFACE int |
334 | is_inport (pointer p) |
347 | is_inport (pointer p) |
335 | { |
348 | { |
336 | return is_port (p) && p->object.port->kind & port_input; |
349 | return is_port (p) && port (p)->kind & port_input; |
337 | } |
350 | } |
338 | |
351 | |
339 | INTERFACE int |
352 | INTERFACE int |
340 | is_outport (pointer p) |
353 | is_outport (pointer p) |
341 | { |
354 | { |
342 | return is_port (p) && p->object.port->kind & port_output; |
355 | return is_port (p) && port (p)->kind & port_output; |
343 | } |
356 | } |
344 | |
357 | |
345 | INTERFACE int |
358 | INTERFACE int |
346 | is_pair (pointer p) |
359 | is_pair (pointer p) |
347 | { |
360 | { |
348 | return type (p) == T_PAIR; |
361 | return type (p) == T_PAIR; |
349 | } |
362 | } |
350 | |
363 | |
351 | #define car(p) ((p)->object.cons.car + 0) |
364 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
352 | #define cdr(p) ((p)->object.cons.cdr + 0) |
365 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
353 | |
366 | |
354 | static pointer caar (pointer p) { return car (car (p)); } |
367 | static pointer caar (pointer p) { return car (car (p)); } |
355 | static pointer cadr (pointer p) { return car (cdr (p)); } |
368 | static pointer cadr (pointer p) { return car (cdr (p)); } |
356 | static pointer cdar (pointer p) { return cdr (car (p)); } |
369 | static pointer cdar (pointer p) { return cdr (car (p)); } |
357 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
370 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
… | |
… | |
361 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
374 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
362 | |
375 | |
363 | INTERFACE void |
376 | INTERFACE void |
364 | set_car (pointer p, pointer q) |
377 | set_car (pointer p, pointer q) |
365 | { |
378 | { |
366 | p->object.cons.car = q; |
379 | CELL(p)->object.cons.car = CELL (q); |
367 | } |
380 | } |
368 | |
381 | |
369 | INTERFACE void |
382 | INTERFACE void |
370 | set_cdr (pointer p, pointer q) |
383 | set_cdr (pointer p, pointer q) |
371 | { |
384 | { |
372 | p->object.cons.cdr = q; |
385 | CELL(p)->object.cons.cdr = CELL (q); |
373 | } |
386 | } |
374 | |
387 | |
375 | INTERFACE pointer |
388 | INTERFACE pointer |
376 | pair_car (pointer p) |
389 | pair_car (pointer p) |
377 | { |
390 | { |
… | |
… | |
391 | } |
404 | } |
392 | |
405 | |
393 | INTERFACE char * |
406 | INTERFACE char * |
394 | symname (pointer p) |
407 | symname (pointer p) |
395 | { |
408 | { |
396 | return strvalue (car (p)); |
409 | return strvalue (p); |
397 | } |
410 | } |
398 | |
411 | |
399 | #if USE_PLIST |
412 | #if USE_PLIST |
|
|
413 | #error plists are broken because symbols are no longer pairs |
|
|
414 | #define symprop(p) cdr(p) |
400 | SCHEME_EXPORT int |
415 | SCHEME_EXPORT int |
401 | hasprop (pointer p) |
416 | hasprop (pointer p) |
402 | { |
417 | { |
403 | return typeflag (p) & T_SYMBOL; |
418 | return typeflag (p) & T_SYMBOL; |
404 | } |
419 | } |
405 | |
|
|
406 | # define symprop(p) cdr(p) |
|
|
407 | #endif |
420 | #endif |
408 | |
421 | |
409 | INTERFACE int |
422 | INTERFACE int |
410 | is_syntax (pointer p) |
423 | is_syntax (pointer p) |
411 | { |
424 | { |
… | |
… | |
425 | } |
438 | } |
426 | |
439 | |
427 | INTERFACE char * |
440 | INTERFACE char * |
428 | syntaxname (pointer p) |
441 | syntaxname (pointer p) |
429 | { |
442 | { |
430 | return strvalue (car (p)); |
443 | return strvalue (p); |
431 | } |
444 | } |
432 | |
445 | |
433 | #define procnum(p) ivalue_unchecked (p) |
446 | #define procnum(p) ivalue_unchecked (p) |
434 | static const char *procname (pointer x); |
447 | static const char *procname (pointer x); |
435 | |
448 | |
… | |
… | |
507 | proper list: length |
520 | proper list: length |
508 | circular list: -1 |
521 | circular list: -1 |
509 | not even a pair: -2 |
522 | not even a pair: -2 |
510 | dotted list: -2 minus length before dot |
523 | dotted list: -2 minus length before dot |
511 | */ |
524 | */ |
512 | INTERFACE int |
525 | ecb_hot INTERFACE int |
513 | list_length (SCHEME_P_ pointer a) |
526 | list_length (SCHEME_P_ pointer a) |
514 | { |
527 | { |
515 | int i = 0; |
528 | int i = 0; |
516 | pointer slow, fast; |
529 | pointer slow, fast; |
517 | |
530 | |
… | |
… | |
623 | "gs", |
636 | "gs", |
624 | "rs", |
637 | "rs", |
625 | "us" |
638 | "us" |
626 | }; |
639 | }; |
627 | |
640 | |
628 | static int |
641 | ecb_cold static int |
629 | is_ascii_name (const char *name, int *pc) |
642 | is_ascii_name (const char *name, int *pc) |
630 | { |
643 | { |
631 | int i; |
644 | int i; |
632 | |
645 | |
633 | for (i = 0; i < 32; i++) |
646 | for (i = 0; i < 32; i++) |
… | |
… | |
651 | #endif |
664 | #endif |
652 | |
665 | |
653 | static int file_push (SCHEME_P_ const char *fname); |
666 | static int file_push (SCHEME_P_ const char *fname); |
654 | static void file_pop (SCHEME_P); |
667 | static void file_pop (SCHEME_P); |
655 | static int file_interactive (SCHEME_P); |
668 | static int file_interactive (SCHEME_P); |
656 | ecb_inline int is_one_of (char *s, int c); |
669 | ecb_inline int is_one_of (const char *s, int c); |
657 | static int alloc_cellseg (SCHEME_P_ int n); |
670 | static int alloc_cellseg (SCHEME_P); |
658 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
671 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
659 | static void finalize_cell (SCHEME_P_ pointer a); |
672 | static void finalize_cell (SCHEME_P_ pointer a); |
660 | static int count_consecutive_cells (pointer x, int needed); |
673 | static int count_consecutive_cells (pointer x, int needed); |
661 | 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); |
662 | static pointer mk_number (SCHEME_P_ const num n); |
675 | static pointer mk_number (SCHEME_P_ const num n); |
… | |
… | |
678 | static void mark (pointer a); |
691 | static void mark (pointer a); |
679 | static void gc (SCHEME_P_ pointer a, pointer b); |
692 | static void gc (SCHEME_P_ pointer a, pointer b); |
680 | static int basic_inchar (port *pt); |
693 | static int basic_inchar (port *pt); |
681 | static int inchar (SCHEME_P); |
694 | static int inchar (SCHEME_P); |
682 | static void backchar (SCHEME_P_ int c); |
695 | static void backchar (SCHEME_P_ int c); |
683 | static char *readstr_upto (SCHEME_P_ char *delim); |
696 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
684 | static pointer readstrexp (SCHEME_P); |
697 | static pointer readstrexp (SCHEME_P_ char delim); |
685 | ecb_inline int skipspace (SCHEME_P); |
698 | ecb_inline int skipspace (SCHEME_P); |
686 | static int token (SCHEME_P); |
699 | static int token (SCHEME_P); |
687 | static void printslashstring (SCHEME_P_ char *s, int len); |
700 | static void printslashstring (SCHEME_P_ char *s, int len); |
688 | 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); |
689 | static void printatom (SCHEME_P_ pointer l, int f); |
702 | static void printatom (SCHEME_P_ pointer l, int f); |
… | |
… | |
856 | } |
869 | } |
857 | |
870 | |
858 | return ret; |
871 | return ret; |
859 | } |
872 | } |
860 | |
873 | |
861 | #if USE_MATH |
|
|
862 | |
|
|
863 | /* Round to nearest. Round to even if midway */ |
|
|
864 | static double |
|
|
865 | round_per_R5RS (double x) |
|
|
866 | { |
|
|
867 | double fl = floor (x); |
|
|
868 | double ce = ceil (x); |
|
|
869 | double dfl = x - fl; |
|
|
870 | double dce = ce - x; |
|
|
871 | |
|
|
872 | if (dfl > dce) |
|
|
873 | return ce; |
|
|
874 | else if (dfl < dce) |
|
|
875 | return fl; |
|
|
876 | else |
|
|
877 | { |
|
|
878 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
|
|
879 | return fl; |
|
|
880 | else |
|
|
881 | return ce; |
|
|
882 | } |
|
|
883 | } |
|
|
884 | #endif |
|
|
885 | |
|
|
886 | static int |
874 | static int |
887 | is_zero_rvalue (RVALUE x) |
875 | is_zero_rvalue (RVALUE x) |
888 | { |
876 | { |
889 | return x == 0; |
877 | return x == 0; |
890 | #if 0 |
878 | #if 0 |
… | |
… | |
895 | #endif |
883 | #endif |
896 | #endif |
884 | #endif |
897 | } |
885 | } |
898 | |
886 | |
899 | /* allocate new cell segment */ |
887 | /* allocate new cell segment */ |
900 | static int |
888 | ecb_cold static int |
901 | alloc_cellseg (SCHEME_P_ int n) |
889 | alloc_cellseg (SCHEME_P) |
902 | { |
890 | { |
903 | pointer newp; |
891 | struct cell *newp; |
904 | pointer last; |
892 | struct cell *last; |
905 | pointer p; |
893 | struct cell *p; |
906 | char *cp; |
894 | char *cp; |
907 | long i; |
895 | long i; |
908 | int k; |
896 | int k; |
909 | |
897 | |
910 | static int segsize = CELL_SEGSIZE >> 1; |
898 | static int segsize = CELL_SEGSIZE >> 1; |
911 | segsize <<= 1; |
899 | segsize <<= 1; |
912 | |
900 | |
913 | for (k = 0; k < n; k++) |
|
|
914 | { |
|
|
915 | if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1) |
|
|
916 | return k; |
|
|
917 | |
|
|
918 | cp = malloc (segsize * sizeof (struct cell)); |
901 | cp = malloc (segsize * sizeof (struct cell)); |
919 | |
902 | |
920 | if (!cp && USE_ERROR_CHECKING) |
903 | if (!cp && USE_ERROR_CHECKING) |
921 | return k; |
904 | return k; |
922 | |
905 | |
923 | i = ++SCHEME_V->last_cell_seg; |
906 | i = ++SCHEME_V->last_cell_seg; |
924 | SCHEME_V->alloc_seg[i] = cp; |
907 | SCHEME_V->alloc_seg[i] = cp; |
925 | |
908 | |
926 | newp = (pointer)cp; |
909 | newp = (struct cell *)cp; |
927 | SCHEME_V->cell_seg[i] = newp; |
910 | SCHEME_V->cell_seg[i] = newp; |
928 | SCHEME_V->cell_segsize[i] = segsize; |
911 | SCHEME_V->cell_segsize[i] = segsize; |
929 | SCHEME_V->fcells += segsize; |
912 | SCHEME_V->fcells += segsize; |
930 | last = newp + segsize - 1; |
913 | last = newp + segsize - 1; |
931 | |
914 | |
932 | for (p = newp; p <= last; p++) |
915 | for (p = newp; p <= last; p++) |
933 | { |
916 | { |
|
|
917 | pointer cp = POINTER (p); |
934 | set_typeflag (p, T_PAIR); |
918 | set_typeflag (cp, T_PAIR); |
935 | set_car (p, NIL); |
919 | set_car (cp, NIL); |
936 | set_cdr (p, p + 1); |
920 | set_cdr (cp, POINTER (p + 1)); |
937 | } |
921 | } |
938 | |
922 | |
939 | set_cdr (last, SCHEME_V->free_cell); |
923 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
940 | SCHEME_V->free_cell = newp; |
924 | SCHEME_V->free_cell = POINTER (newp); |
941 | } |
|
|
942 | |
925 | |
943 | return n; |
926 | return 1; |
944 | } |
927 | } |
945 | |
928 | |
946 | /* get new cell. parameter a, b is marked by gc. */ |
929 | /* get new cell. parameter a, b is marked by gc. */ |
947 | ecb_inline pointer |
930 | ecb_inline pointer |
948 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
931 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
… | |
… | |
952 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
935 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
953 | return S_SINK; |
936 | return S_SINK; |
954 | |
937 | |
955 | if (SCHEME_V->free_cell == NIL) |
938 | if (SCHEME_V->free_cell == NIL) |
956 | { |
939 | { |
957 | 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; |
958 | |
941 | |
959 | gc (SCHEME_A_ a, b); |
942 | gc (SCHEME_A_ a, b); |
960 | |
943 | |
961 | 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) |
962 | { |
945 | { |
963 | /* 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 */ |
964 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
947 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
965 | { |
948 | { |
966 | #if USE_ERROR_CHECKING |
949 | #if USE_ERROR_CHECKING |
967 | SCHEME_V->no_memory = 1; |
950 | SCHEME_V->no_memory = 1; |
968 | return S_SINK; |
951 | return S_SINK; |
969 | #endif |
952 | #endif |
… | |
… | |
981 | } |
964 | } |
982 | } |
965 | } |
983 | |
966 | |
984 | /* To retain recent allocs before interpreter knows about them - |
967 | /* To retain recent allocs before interpreter knows about them - |
985 | Tehom */ |
968 | Tehom */ |
986 | |
969 | ecb_hot static void |
987 | static void |
|
|
988 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
970 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
989 | { |
971 | { |
990 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
972 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
991 | |
973 | |
992 | set_typeflag (holder, T_PAIR); |
974 | set_typeflag (holder, T_PAIR); |
… | |
… | |
994 | set_car (holder, recent); |
976 | set_car (holder, recent); |
995 | set_cdr (holder, car (S_SINK)); |
977 | set_cdr (holder, car (S_SINK)); |
996 | set_car (S_SINK, holder); |
978 | set_car (S_SINK, holder); |
997 | } |
979 | } |
998 | |
980 | |
999 | static pointer |
981 | ecb_hot static pointer |
1000 | get_cell (SCHEME_P_ pointer a, pointer b) |
982 | get_cell (SCHEME_P_ pointer a, pointer b) |
1001 | { |
983 | { |
1002 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
984 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
1003 | |
985 | |
1004 | /* For right now, include "a" and "b" in "cell" so that gc doesn't |
986 | /* For right now, include "a" and "b" in "cell" so that gc doesn't |
… | |
… | |
1013 | } |
995 | } |
1014 | |
996 | |
1015 | static pointer |
997 | static pointer |
1016 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
998 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1017 | { |
999 | { |
1018 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1000 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
1019 | pointer *e = malloc (len * sizeof (pointer)); |
1001 | pointer *e = malloc (len * sizeof (pointer)); |
1020 | |
1002 | |
1021 | if (!e && USE_ERROR_CHECKING) |
1003 | if (!e && USE_ERROR_CHECKING) |
1022 | return S_SINK; |
1004 | return S_SINK; |
1023 | |
1005 | |
1024 | /* Record it as a vector so that gc understands it. */ |
1006 | /* Record it as a vector so that gc understands it. */ |
1025 | set_typeflag (v, T_VECTOR | T_ATOM); |
1007 | set_typeflag (v, T_VECTOR | T_ATOM); |
1026 | |
1008 | |
1027 | v->object.vector.vvalue = e; |
1009 | CELL(v)->object.vector.vvalue = e; |
1028 | v->object.vector.length = len; |
1010 | CELL(v)->object.vector.length = len; |
1029 | fill_vector (v, 0, init); |
1011 | fill_vector (v, 0, init); |
1030 | push_recent_alloc (SCHEME_A_ v, NIL); |
1012 | push_recent_alloc (SCHEME_A_ v, NIL); |
1031 | |
1013 | |
1032 | return v; |
1014 | return v; |
1033 | } |
1015 | } |
… | |
… | |
1042 | static void |
1024 | static void |
1043 | check_cell_alloced (pointer p, int expect_alloced) |
1025 | check_cell_alloced (pointer p, int expect_alloced) |
1044 | { |
1026 | { |
1045 | /* 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. */ |
1046 | if (typeflag (p) & !expect_alloced) |
1028 | if (typeflag (p) & !expect_alloced) |
1047 | xwrstr ("Cell is already allocated!\n"); |
1029 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1048 | |
1030 | |
1049 | if (!(typeflag (p)) & expect_alloced) |
1031 | if (!(typeflag (p)) & expect_alloced) |
1050 | xwrstr ("Cell is not allocated!\n"); |
1032 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1051 | } |
1033 | } |
1052 | |
1034 | |
1053 | static void |
1035 | static void |
1054 | check_range_alloced (pointer p, int n, int expect_alloced) |
1036 | check_range_alloced (pointer p, int n, int expect_alloced) |
1055 | { |
1037 | { |
… | |
… | |
1061 | #endif |
1043 | #endif |
1062 | |
1044 | |
1063 | /* Medium level cell allocation */ |
1045 | /* Medium level cell allocation */ |
1064 | |
1046 | |
1065 | /* get new cons cell */ |
1047 | /* get new cons cell */ |
1066 | pointer |
1048 | ecb_hot pointer |
1067 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1049 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1068 | { |
1050 | { |
1069 | pointer x = get_cell (SCHEME_A_ a, b); |
1051 | pointer x = get_cell (SCHEME_A_ a, b); |
1070 | |
1052 | |
1071 | set_typeflag (x, T_PAIR); |
1053 | set_typeflag (x, T_PAIR); |
… | |
… | |
1077 | set_cdr (x, b); |
1059 | set_cdr (x, b); |
1078 | |
1060 | |
1079 | return x; |
1061 | return x; |
1080 | } |
1062 | } |
1081 | |
1063 | |
|
|
1064 | ecb_cold 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 | |
1082 | /* ========== oblist implementation ========== */ |
1073 | /* ========== oblist implementation ========== */ |
1083 | |
1074 | |
1084 | #ifndef USE_OBJECT_LIST |
1075 | #ifndef USE_OBJECT_LIST |
1085 | |
1076 | |
|
|
1077 | static int |
1086 | 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; |
1087 | |
1082 | |
|
|
1083 | while (*p) |
|
|
1084 | hash = (hash ^ *p++) * 16777619; |
|
|
1085 | |
|
|
1086 | return hash % table_size; |
|
|
1087 | } |
|
|
1088 | |
1088 | static pointer |
1089 | ecb_cold static pointer |
1089 | oblist_initial_value (SCHEME_P) |
1090 | oblist_initial_value (SCHEME_P) |
1090 | { |
1091 | { |
1091 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1092 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1092 | } |
1093 | } |
1093 | |
1094 | |
1094 | /* returns the new symbol */ |
1095 | /* returns the new symbol */ |
1095 | static pointer |
1096 | ecb_cold static pointer |
1096 | oblist_add_by_name (SCHEME_P_ const char *name) |
1097 | oblist_add_by_name (SCHEME_P_ const char *name) |
1097 | { |
1098 | { |
1098 | int location; |
1099 | pointer x = generate_symbol (SCHEME_A_ name); |
1099 | |
|
|
1100 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1101 | set_typeflag (x, T_SYMBOL); |
|
|
1102 | setimmutable (car (x)); |
|
|
1103 | |
|
|
1104 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1100 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1105 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1101 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1106 | return x; |
1102 | return x; |
1107 | } |
1103 | } |
1108 | |
1104 | |
1109 | ecb_inline pointer |
1105 | ecb_cold static pointer |
1110 | oblist_find_by_name (SCHEME_P_ const char *name) |
1106 | oblist_find_by_name (SCHEME_P_ const char *name) |
1111 | { |
1107 | { |
1112 | int location; |
1108 | int location; |
1113 | pointer x; |
1109 | pointer x; |
1114 | char *s; |
1110 | char *s; |
… | |
… | |
1125 | } |
1121 | } |
1126 | |
1122 | |
1127 | return NIL; |
1123 | return NIL; |
1128 | } |
1124 | } |
1129 | |
1125 | |
1130 | static pointer |
1126 | ecb_cold static pointer |
1131 | oblist_all_symbols (SCHEME_P) |
1127 | oblist_all_symbols (SCHEME_P) |
1132 | { |
1128 | { |
1133 | int i; |
1129 | int i; |
1134 | pointer x; |
1130 | pointer x; |
1135 | pointer ob_list = NIL; |
1131 | pointer ob_list = NIL; |
… | |
… | |
1141 | return ob_list; |
1137 | return ob_list; |
1142 | } |
1138 | } |
1143 | |
1139 | |
1144 | #else |
1140 | #else |
1145 | |
1141 | |
1146 | static pointer |
1142 | ecb_cold static pointer |
1147 | oblist_initial_value (SCHEME_P) |
1143 | oblist_initial_value (SCHEME_P) |
1148 | { |
1144 | { |
1149 | return NIL; |
1145 | return NIL; |
1150 | } |
1146 | } |
1151 | |
1147 | |
1152 | ecb_inline pointer |
1148 | ecb_cold static pointer |
1153 | oblist_find_by_name (SCHEME_P_ const char *name) |
1149 | oblist_find_by_name (SCHEME_P_ const char *name) |
1154 | { |
1150 | { |
1155 | pointer x; |
1151 | pointer x; |
1156 | char *s; |
1152 | char *s; |
1157 | |
1153 | |
… | |
… | |
1166 | |
1162 | |
1167 | return NIL; |
1163 | return NIL; |
1168 | } |
1164 | } |
1169 | |
1165 | |
1170 | /* returns the new symbol */ |
1166 | /* returns the new symbol */ |
1171 | static pointer |
1167 | ecb_cold static pointer |
1172 | oblist_add_by_name (SCHEME_P_ const char *name) |
1168 | oblist_add_by_name (SCHEME_P_ const char *name) |
1173 | { |
1169 | { |
1174 | pointer x; |
1170 | pointer x = generate_symbol (SCHEME_A_ name); |
1175 | |
|
|
1176 | x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1177 | set_typeflag (x, T_SYMBOL); |
|
|
1178 | setimmutable (car (x)); |
|
|
1179 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1171 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1180 | return x; |
1172 | return x; |
1181 | } |
1173 | } |
1182 | |
1174 | |
1183 | static pointer |
1175 | ecb_cold static pointer |
1184 | oblist_all_symbols (SCHEME_P) |
1176 | oblist_all_symbols (SCHEME_P) |
1185 | { |
1177 | { |
1186 | return SCHEME_V->oblist; |
1178 | return SCHEME_V->oblist; |
1187 | } |
1179 | } |
1188 | |
1180 | |
1189 | #endif |
1181 | #endif |
1190 | |
1182 | |
1191 | #if USE_PORTS |
1183 | #if USE_PORTS |
1192 | static pointer |
1184 | ecb_cold static pointer |
1193 | mk_port (SCHEME_P_ port *p) |
1185 | mk_port (SCHEME_P_ port *p) |
1194 | { |
1186 | { |
1195 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1187 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1196 | |
1188 | |
1197 | set_typeflag (x, T_PORT | T_ATOM); |
1189 | set_typeflag (x, T_PORT | T_ATOM); |
1198 | x->object.port = p; |
1190 | set_port (x, p); |
1199 | |
1191 | |
1200 | return x; |
1192 | return x; |
1201 | } |
1193 | } |
1202 | #endif |
1194 | #endif |
1203 | |
1195 | |
1204 | pointer |
1196 | ecb_cold pointer |
1205 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1197 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1206 | { |
1198 | { |
1207 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1199 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1208 | |
1200 | |
1209 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1201 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1210 | x->object.ff = f; |
1202 | CELL(x)->object.ff = f; |
1211 | |
1203 | |
1212 | return x; |
1204 | return x; |
1213 | } |
1205 | } |
1214 | |
1206 | |
1215 | INTERFACE pointer |
1207 | INTERFACE pointer |
1216 | mk_character (SCHEME_P_ int c) |
1208 | mk_character (SCHEME_P_ int c) |
1217 | { |
1209 | { |
1218 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1210 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1219 | |
1211 | |
1220 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1212 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1221 | set_ivalue (x, c & 0xff); |
1213 | set_ivalue (x, c & 0xff); |
1222 | |
1214 | |
1223 | return x; |
1215 | return x; |
1224 | } |
1216 | } |
1225 | |
1217 | |
1226 | /* get number atom (integer) */ |
1218 | /* get number atom (integer) */ |
1227 | INTERFACE pointer |
1219 | INTERFACE pointer |
1228 | mk_integer (SCHEME_P_ long n) |
1220 | mk_integer (SCHEME_P_ long n) |
1229 | { |
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 | { |
1230 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1232 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1231 | |
1233 | |
1232 | set_typeflag (x, (T_INTEGER | T_ATOM)); |
1234 | set_typeflag (x, T_INTEGER | T_ATOM); |
|
|
1235 | setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */ |
1233 | set_ivalue (x, n); |
1236 | set_ivalue (x, n); |
1234 | |
1237 | |
|
|
1238 | *pp = x; |
|
|
1239 | } |
|
|
1240 | |
1235 | return x; |
1241 | return *pp; |
1236 | } |
1242 | } |
1237 | |
1243 | |
1238 | INTERFACE pointer |
1244 | INTERFACE pointer |
1239 | mk_real (SCHEME_P_ RVALUE n) |
1245 | mk_real (SCHEME_P_ RVALUE n) |
1240 | { |
1246 | { |
1241 | #if USE_REAL |
1247 | #if USE_REAL |
1242 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1248 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1243 | |
1249 | |
1244 | set_typeflag (x, (T_REAL | T_ATOM)); |
1250 | set_typeflag (x, T_REAL | T_ATOM); |
1245 | set_rvalue (x, n); |
1251 | set_rvalue (x, n); |
1246 | |
1252 | |
1247 | return x; |
1253 | return x; |
1248 | #else |
1254 | #else |
1249 | return mk_integer (SCHEME_A_ n); |
1255 | return mk_integer (SCHEME_A_ n); |
… | |
… | |
1273 | SCHEME_V->no_memory = 1; |
1279 | SCHEME_V->no_memory = 1; |
1274 | return SCHEME_V->strbuff; |
1280 | return SCHEME_V->strbuff; |
1275 | } |
1281 | } |
1276 | |
1282 | |
1277 | if (str) |
1283 | if (str) |
1278 | { |
1284 | memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */ |
1279 | int l = strlen (str); |
|
|
1280 | |
|
|
1281 | if (l > len_str) |
|
|
1282 | l = len_str; |
|
|
1283 | |
|
|
1284 | memcpy (q, str, l); |
|
|
1285 | q[l] = 0; |
|
|
1286 | } |
|
|
1287 | else |
1285 | else |
1288 | { |
|
|
1289 | memset (q, fill, len_str); |
1286 | memset (q, fill, len_str); |
|
|
1287 | |
1290 | q[len_str] = 0; |
1288 | q[len_str] = 0; |
1291 | } |
|
|
1292 | |
1289 | |
1293 | return q; |
1290 | return q; |
1294 | } |
1291 | } |
1295 | |
1292 | |
1296 | INTERFACE pointer |
1293 | INTERFACE pointer |
… | |
… | |
1310 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1307 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1311 | |
1308 | |
1312 | set_typeflag (x, T_STRING | T_ATOM); |
1309 | set_typeflag (x, T_STRING | T_ATOM); |
1313 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1310 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1314 | strlength (x) = len; |
1311 | strlength (x) = len; |
|
|
1312 | |
1315 | return x; |
1313 | return x; |
1316 | } |
1314 | } |
1317 | |
1315 | |
1318 | INTERFACE pointer |
1316 | INTERFACE pointer |
1319 | mk_string (SCHEME_P_ const char *str) |
1317 | mk_string (SCHEME_P_ const char *str) |
… | |
… | |
1332 | { |
1330 | { |
1333 | int i; |
1331 | int i; |
1334 | |
1332 | |
1335 | for (i = start; i < veclength (vec); i++) |
1333 | for (i = start; i < veclength (vec); i++) |
1336 | vecvalue (vec)[i] = obj; |
1334 | vecvalue (vec)[i] = obj; |
|
|
1335 | } |
|
|
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); |
1337 | } |
1344 | } |
1338 | |
1345 | |
1339 | INTERFACE pointer |
1346 | INTERFACE pointer |
1340 | vector_get (pointer vec, uint32_t ielem) |
1347 | vector_get (pointer vec, uint32_t ielem) |
1341 | { |
1348 | { |
… | |
… | |
1359 | x = oblist_add_by_name (SCHEME_A_ name); |
1366 | x = oblist_add_by_name (SCHEME_A_ name); |
1360 | |
1367 | |
1361 | return x; |
1368 | return x; |
1362 | } |
1369 | } |
1363 | |
1370 | |
1364 | INTERFACE pointer |
1371 | ecb_cold INTERFACE pointer |
1365 | gensym (SCHEME_P) |
1372 | gensym (SCHEME_P) |
1366 | { |
1373 | { |
1367 | pointer x; |
1374 | pointer x; |
1368 | |
|
|
1369 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
|
|
1370 | { |
|
|
1371 | char name[40] = "gensym-"; |
1375 | char name[40] = "gensym-"; |
1372 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1376 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1373 | |
1377 | |
1374 | /* first check oblist */ |
1378 | return generate_symbol (SCHEME_A_ name); |
1375 | x = oblist_find_by_name (SCHEME_A_ name); |
1379 | } |
1376 | |
1380 | |
1377 | if (x == NIL) |
1381 | static int |
1378 | { |
1382 | is_gensym (SCHEME_P_ pointer x) |
1379 | x = oblist_add_by_name (SCHEME_A_ name); |
1383 | { |
1380 | return x; |
1384 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1381 | } |
|
|
1382 | } |
|
|
1383 | |
|
|
1384 | return NIL; |
|
|
1385 | } |
1385 | } |
1386 | |
1386 | |
1387 | /* make symbol or number atom from string */ |
1387 | /* make symbol or number atom from string */ |
1388 | static pointer |
1388 | ecb_cold static pointer |
1389 | mk_atom (SCHEME_P_ char *q) |
1389 | mk_atom (SCHEME_P_ char *q) |
1390 | { |
1390 | { |
1391 | char c, *p; |
1391 | char c, *p; |
1392 | int has_dec_point = 0; |
1392 | int has_dec_point = 0; |
1393 | int has_fp_exp = 0; |
1393 | int has_fp_exp = 0; |
… | |
… | |
1464 | |
1464 | |
1465 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1465 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1466 | } |
1466 | } |
1467 | |
1467 | |
1468 | /* make constant */ |
1468 | /* make constant */ |
1469 | static pointer |
1469 | ecb_cold static pointer |
1470 | mk_sharp_const (SCHEME_P_ char *name) |
1470 | mk_sharp_const (SCHEME_P_ char *name) |
1471 | { |
1471 | { |
1472 | if (!strcmp (name, "t")) |
1472 | if (!strcmp (name, "t")) |
1473 | return S_T; |
1473 | return S_T; |
1474 | else if (!strcmp (name, "f")) |
1474 | else if (!strcmp (name, "f")) |
1475 | return S_F; |
1475 | return S_F; |
1476 | else if (*name == '\\') /* #\w (character) */ |
1476 | else if (*name == '\\') /* #\w (character) */ |
1477 | { |
1477 | { |
1478 | int c; |
1478 | int c; |
1479 | |
1479 | |
|
|
1480 | // TODO: optimise |
1480 | if (stricmp (name + 1, "space") == 0) |
1481 | if (stricmp (name + 1, "space") == 0) |
1481 | c = ' '; |
1482 | c = ' '; |
1482 | else if (stricmp (name + 1, "newline") == 0) |
1483 | else if (stricmp (name + 1, "newline") == 0) |
1483 | c = '\n'; |
1484 | c = '\n'; |
1484 | else if (stricmp (name + 1, "return") == 0) |
1485 | else if (stricmp (name + 1, "return") == 0) |
1485 | c = '\r'; |
1486 | c = '\r'; |
1486 | else if (stricmp (name + 1, "tab") == 0) |
1487 | else if (stricmp (name + 1, "tab") == 0) |
1487 | 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; |
1488 | else if (name[1] == 'x' && name[2] != 0) |
1499 | else if (name[1] == 'x' && name[2] != 0) |
1489 | { |
1500 | { |
1490 | long c1 = strtol (name + 2, 0, 16); |
1501 | long c1 = strtol (name + 2, 0, 16); |
1491 | |
1502 | |
1492 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1503 | if (0 <= c1 && c1 <= UCHAR_MAX) |
… | |
… | |
1517 | return NIL; |
1528 | return NIL; |
1518 | } |
1529 | } |
1519 | } |
1530 | } |
1520 | |
1531 | |
1521 | /* ========== garbage collector ========== */ |
1532 | /* ========== garbage collector ========== */ |
|
|
1533 | |
|
|
1534 | static void |
|
|
1535 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1536 | { |
|
|
1537 | /* TODO, fast bitmap check? */ |
|
|
1538 | if (is_string (a) || is_symbol (a)) |
|
|
1539 | free (strvalue (a)); |
|
|
1540 | else if (is_vector (a)) |
|
|
1541 | free (vecvalue (a)); |
|
|
1542 | #if USE_PORTS |
|
|
1543 | else if (is_port (a)) |
|
|
1544 | { |
|
|
1545 | if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) |
|
|
1546 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1547 | |
|
|
1548 | free (port (a)); |
|
|
1549 | } |
|
|
1550 | #endif |
|
|
1551 | } |
1522 | |
1552 | |
1523 | /*-- |
1553 | /*-- |
1524 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1554 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1525 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1555 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1526 | * for marking. |
1556 | * for marking. |
1527 | * |
1557 | * |
1528 | * The exception is vectors - vectors are currently marked recursively, |
1558 | * The exception is vectors - vectors are currently marked recursively, |
1529 | * which is inherited form tinyscheme and could be fixed by having another |
1559 | * which is inherited form tinyscheme and could be fixed by having another |
1530 | * word of context in the vector |
1560 | * word of context in the vector |
1531 | */ |
1561 | */ |
1532 | static void |
1562 | ecb_hot static void |
1533 | mark (pointer a) |
1563 | mark (pointer a) |
1534 | { |
1564 | { |
1535 | pointer t, q, p; |
1565 | pointer t, q, p; |
1536 | |
1566 | |
1537 | t = 0; |
1567 | t = 0; |
… | |
… | |
1594 | p = q; |
1624 | p = q; |
1595 | goto E6; |
1625 | goto E6; |
1596 | } |
1626 | } |
1597 | } |
1627 | } |
1598 | |
1628 | |
|
|
1629 | ecb_hot static void |
|
|
1630 | gc_free (SCHEME_P) |
|
|
1631 | { |
|
|
1632 | int i; |
|
|
1633 | uint32_t total = 0; |
|
|
1634 | |
|
|
1635 | /* Here we scan the cells to build the free-list. */ |
|
|
1636 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1637 | { |
|
|
1638 | struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1639 | struct cell *p; |
|
|
1640 | total += SCHEME_V->cell_segsize [i]; |
|
|
1641 | |
|
|
1642 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
|
|
1643 | { |
|
|
1644 | pointer c = POINTER (p); |
|
|
1645 | |
|
|
1646 | if (is_mark (c)) |
|
|
1647 | clrmark (c); |
|
|
1648 | else |
|
|
1649 | { |
|
|
1650 | /* reclaim cell */ |
|
|
1651 | if (typeflag (c) != T_PAIR) |
|
|
1652 | { |
|
|
1653 | finalize_cell (SCHEME_A_ c); |
|
|
1654 | set_typeflag (c, T_PAIR); |
|
|
1655 | set_car (c, NIL); |
|
|
1656 | } |
|
|
1657 | |
|
|
1658 | ++SCHEME_V->fcells; |
|
|
1659 | set_cdr (c, SCHEME_V->free_cell); |
|
|
1660 | SCHEME_V->free_cell = c; |
|
|
1661 | } |
|
|
1662 | } |
|
|
1663 | } |
|
|
1664 | |
|
|
1665 | if (SCHEME_V->gc_verbose) |
|
|
1666 | { |
|
|
1667 | 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"); |
|
|
1668 | } |
|
|
1669 | } |
|
|
1670 | |
1599 | /* garbage collection. parameter a, b is marked. */ |
1671 | /* garbage collection. parameter a, b is marked. */ |
1600 | static void |
1672 | ecb_cold static void |
1601 | gc (SCHEME_P_ pointer a, pointer b) |
1673 | gc (SCHEME_P_ pointer a, pointer b) |
1602 | { |
1674 | { |
1603 | pointer p; |
|
|
1604 | int i; |
1675 | int i; |
1605 | |
1676 | |
1606 | if (SCHEME_V->gc_verbose) |
1677 | if (SCHEME_V->gc_verbose) |
1607 | putstr (SCHEME_A_ "gc..."); |
1678 | putstr (SCHEME_A_ "gc..."); |
1608 | |
1679 | |
… | |
… | |
1624 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1695 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1625 | mark (car (S_SINK)); |
1696 | mark (car (S_SINK)); |
1626 | /* Mark any older stuff above nested C calls */ |
1697 | /* Mark any older stuff above nested C calls */ |
1627 | mark (SCHEME_V->c_nest); |
1698 | mark (SCHEME_V->c_nest); |
1628 | |
1699 | |
|
|
1700 | #if USE_INTCACHE |
|
|
1701 | /* mark intcache */ |
|
|
1702 | for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i) |
|
|
1703 | if (SCHEME_V->intcache[i - INTCACHE_MIN]) |
|
|
1704 | mark (SCHEME_V->intcache[i - INTCACHE_MIN]); |
|
|
1705 | #endif |
|
|
1706 | |
1629 | /* mark variables a, b */ |
1707 | /* mark variables a, b */ |
1630 | mark (a); |
1708 | mark (a); |
1631 | mark (b); |
1709 | mark (b); |
1632 | |
1710 | |
1633 | /* garbage collect */ |
1711 | /* garbage collect */ |
1634 | clrmark (NIL); |
1712 | clrmark (NIL); |
1635 | SCHEME_V->fcells = 0; |
1713 | SCHEME_V->fcells = 0; |
1636 | SCHEME_V->free_cell = NIL; |
1714 | SCHEME_V->free_cell = NIL; |
1637 | |
1715 | |
1638 | /* free-list is kept sorted by address so as to maintain consecutive |
|
|
1639 | ranges, if possible, for use with vectors. Here we scan the cells |
|
|
1640 | (which are also kept sorted by address) downwards to build the |
|
|
1641 | free-list in sorted order. |
|
|
1642 | */ |
|
|
1643 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1644 | { |
|
|
1645 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1646 | |
|
|
1647 | while (--p >= SCHEME_V->cell_seg[i]) |
|
|
1648 | { |
|
|
1649 | if (is_mark (p)) |
|
|
1650 | clrmark (p); |
|
|
1651 | else |
|
|
1652 | { |
|
|
1653 | /* reclaim cell */ |
|
|
1654 | if (typeflag (p) != T_PAIR) |
|
|
1655 | { |
|
|
1656 | finalize_cell (SCHEME_A_ p); |
|
|
1657 | set_typeflag (p, T_PAIR); |
|
|
1658 | set_car (p, NIL); |
|
|
1659 | } |
|
|
1660 | |
|
|
1661 | ++SCHEME_V->fcells; |
|
|
1662 | set_cdr (p, SCHEME_V->free_cell); |
|
|
1663 | SCHEME_V->free_cell = p; |
|
|
1664 | } |
|
|
1665 | } |
|
|
1666 | } |
|
|
1667 | |
|
|
1668 | if (SCHEME_V->gc_verbose) |
1716 | if (SCHEME_V->gc_verbose) |
1669 | { |
1717 | putstr (SCHEME_A_ "freeing..."); |
1670 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
|
|
1671 | } |
|
|
1672 | } |
|
|
1673 | |
1718 | |
1674 | static void |
1719 | gc_free (SCHEME_A); |
1675 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1676 | { |
|
|
1677 | /* TODO, fast bitmap check? */ |
|
|
1678 | if (is_string (a)) |
|
|
1679 | free (strvalue (a)); |
|
|
1680 | else if (is_vector (a)) |
|
|
1681 | free (vecvalue (a)); |
|
|
1682 | #if USE_PORTS |
|
|
1683 | else if (is_port (a)) |
|
|
1684 | { |
|
|
1685 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
|
|
1686 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1687 | |
|
|
1688 | free (a->object.port); |
|
|
1689 | } |
|
|
1690 | #endif |
|
|
1691 | } |
1720 | } |
1692 | |
1721 | |
1693 | /* ========== Routines for Reading ========== */ |
1722 | /* ========== Routines for Reading ========== */ |
1694 | |
1723 | |
1695 | static int |
1724 | ecb_cold static int |
1696 | file_push (SCHEME_P_ const char *fname) |
1725 | file_push (SCHEME_P_ const char *fname) |
1697 | { |
1726 | { |
1698 | #if USE_PORTS |
1727 | #if USE_PORTS |
1699 | int fin; |
1728 | int fin; |
1700 | |
1729 | |
… | |
… | |
1709 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1738 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1710 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1739 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1711 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1740 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1712 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1741 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1713 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1742 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1714 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1743 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1715 | |
1744 | |
1716 | #if SHOW_ERROR_LINE |
1745 | #if SHOW_ERROR_LINE |
1717 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1746 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1718 | |
1747 | |
1719 | if (fname) |
1748 | if (fname) |
… | |
… | |
1726 | #else |
1755 | #else |
1727 | return 1; |
1756 | return 1; |
1728 | #endif |
1757 | #endif |
1729 | } |
1758 | } |
1730 | |
1759 | |
1731 | static void |
1760 | ecb_cold static void |
1732 | file_pop (SCHEME_P) |
1761 | file_pop (SCHEME_P) |
1733 | { |
1762 | { |
1734 | if (SCHEME_V->file_i != 0) |
1763 | if (SCHEME_V->file_i != 0) |
1735 | { |
1764 | { |
1736 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1765 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1737 | #if USE_PORTS |
1766 | #if USE_PORTS |
1738 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1767 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1739 | #endif |
1768 | #endif |
1740 | SCHEME_V->file_i--; |
1769 | SCHEME_V->file_i--; |
1741 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1770 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1742 | } |
1771 | } |
1743 | } |
1772 | } |
1744 | |
1773 | |
1745 | static int |
1774 | ecb_cold static int |
1746 | file_interactive (SCHEME_P) |
1775 | file_interactive (SCHEME_P) |
1747 | { |
1776 | { |
1748 | #if USE_PORTS |
1777 | #if USE_PORTS |
1749 | return SCHEME_V->file_i == 0 |
1778 | return SCHEME_V->file_i == 0 |
1750 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1779 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1751 | && (SCHEME_V->inport->object.port->kind & port_file); |
1780 | && (port (SCHEME_V->inport)->kind & port_file); |
1752 | #else |
1781 | #else |
1753 | return 0; |
1782 | return 0; |
1754 | #endif |
1783 | #endif |
1755 | } |
1784 | } |
1756 | |
1785 | |
1757 | #if USE_PORTS |
1786 | #if USE_PORTS |
1758 | static port * |
1787 | ecb_cold static port * |
1759 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1788 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1760 | { |
1789 | { |
1761 | int fd; |
1790 | int fd; |
1762 | int flags; |
1791 | int flags; |
1763 | char *rw; |
1792 | char *rw; |
… | |
… | |
1786 | # endif |
1815 | # endif |
1787 | |
1816 | |
1788 | return pt; |
1817 | return pt; |
1789 | } |
1818 | } |
1790 | |
1819 | |
1791 | static pointer |
1820 | ecb_cold static pointer |
1792 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1821 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1793 | { |
1822 | { |
1794 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1823 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1795 | |
1824 | |
1796 | if (!pt && USE_ERROR_CHECKING) |
1825 | if (!pt && USE_ERROR_CHECKING) |
1797 | return NIL; |
1826 | return NIL; |
1798 | |
1827 | |
1799 | return mk_port (SCHEME_A_ pt); |
1828 | return mk_port (SCHEME_A_ pt); |
1800 | } |
1829 | } |
1801 | |
1830 | |
1802 | static port * |
1831 | ecb_cold static port * |
1803 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1832 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1804 | { |
1833 | { |
1805 | port *pt = malloc (sizeof *pt); |
1834 | port *pt = malloc (sizeof *pt); |
1806 | |
1835 | |
1807 | if (!pt && USE_ERROR_CHECKING) |
1836 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1812 | pt->rep.stdio.file = f; |
1841 | pt->rep.stdio.file = f; |
1813 | pt->rep.stdio.closeit = 0; |
1842 | pt->rep.stdio.closeit = 0; |
1814 | return pt; |
1843 | return pt; |
1815 | } |
1844 | } |
1816 | |
1845 | |
1817 | static pointer |
1846 | ecb_cold static pointer |
1818 | port_from_file (SCHEME_P_ int f, int prop) |
1847 | port_from_file (SCHEME_P_ int f, int prop) |
1819 | { |
1848 | { |
1820 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1849 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1821 | |
1850 | |
1822 | if (!pt && USE_ERROR_CHECKING) |
1851 | if (!pt && USE_ERROR_CHECKING) |
1823 | return NIL; |
1852 | return NIL; |
1824 | |
1853 | |
1825 | return mk_port (SCHEME_A_ pt); |
1854 | return mk_port (SCHEME_A_ pt); |
1826 | } |
1855 | } |
1827 | |
1856 | |
1828 | static port * |
1857 | ecb_cold static port * |
1829 | port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1858 | port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1830 | { |
1859 | { |
1831 | port *pt = malloc (sizeof (port)); |
1860 | port *pt = malloc (sizeof (port)); |
1832 | |
1861 | |
1833 | if (!pt && USE_ERROR_CHECKING) |
1862 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1839 | pt->rep.string.curr = start; |
1868 | pt->rep.string.curr = start; |
1840 | pt->rep.string.past_the_end = past_the_end; |
1869 | pt->rep.string.past_the_end = past_the_end; |
1841 | return pt; |
1870 | return pt; |
1842 | } |
1871 | } |
1843 | |
1872 | |
1844 | static pointer |
1873 | ecb_cold static pointer |
1845 | port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1874 | port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1846 | { |
1875 | { |
1847 | port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); |
1876 | port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); |
1848 | |
1877 | |
1849 | if (!pt && USE_ERROR_CHECKING) |
1878 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1852 | return mk_port (SCHEME_A_ pt); |
1881 | return mk_port (SCHEME_A_ pt); |
1853 | } |
1882 | } |
1854 | |
1883 | |
1855 | # define BLOCK_SIZE 256 |
1884 | # define BLOCK_SIZE 256 |
1856 | |
1885 | |
1857 | static port * |
1886 | ecb_cold static port * |
1858 | port_rep_from_scratch (SCHEME_P) |
1887 | port_rep_from_scratch (SCHEME_P) |
1859 | { |
1888 | { |
1860 | char *start; |
1889 | char *start; |
1861 | port *pt = malloc (sizeof (port)); |
1890 | port *pt = malloc (sizeof (port)); |
1862 | |
1891 | |
… | |
… | |
1876 | pt->rep.string.curr = start; |
1905 | pt->rep.string.curr = start; |
1877 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1906 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1878 | return pt; |
1907 | return pt; |
1879 | } |
1908 | } |
1880 | |
1909 | |
1881 | static pointer |
1910 | ecb_cold static pointer |
1882 | port_from_scratch (SCHEME_P) |
1911 | port_from_scratch (SCHEME_P) |
1883 | { |
1912 | { |
1884 | port *pt = port_rep_from_scratch (SCHEME_A); |
1913 | port *pt = port_rep_from_scratch (SCHEME_A); |
1885 | |
1914 | |
1886 | if (!pt && USE_ERROR_CHECKING) |
1915 | if (!pt && USE_ERROR_CHECKING) |
1887 | return NIL; |
1916 | return NIL; |
1888 | |
1917 | |
1889 | return mk_port (SCHEME_A_ pt); |
1918 | return mk_port (SCHEME_A_ pt); |
1890 | } |
1919 | } |
1891 | |
1920 | |
1892 | static void |
1921 | ecb_cold static void |
1893 | port_close (SCHEME_P_ pointer p, int flag) |
1922 | port_close (SCHEME_P_ pointer p, int flag) |
1894 | { |
1923 | { |
1895 | port *pt = p->object.port; |
1924 | port *pt = port (p); |
1896 | |
1925 | |
1897 | pt->kind &= ~flag; |
1926 | pt->kind &= ~flag; |
1898 | |
1927 | |
1899 | if ((pt->kind & (port_input | port_output)) == 0) |
1928 | if ((pt->kind & (port_input | port_output)) == 0) |
1900 | { |
1929 | { |
… | |
… | |
1921 | /* get new character from input file */ |
1950 | /* get new character from input file */ |
1922 | static int |
1951 | static int |
1923 | inchar (SCHEME_P) |
1952 | inchar (SCHEME_P) |
1924 | { |
1953 | { |
1925 | int c; |
1954 | int c; |
1926 | port *pt; |
1955 | port *pt = port (SCHEME_V->inport); |
1927 | |
|
|
1928 | pt = SCHEME_V->inport->object.port; |
|
|
1929 | |
1956 | |
1930 | if (pt->kind & port_saw_EOF) |
1957 | if (pt->kind & port_saw_EOF) |
1931 | return EOF; |
1958 | return EOF; |
1932 | |
1959 | |
1933 | c = basic_inchar (pt); |
1960 | c = basic_inchar (pt); |
… | |
… | |
2000 | port *pt; |
2027 | port *pt; |
2001 | |
2028 | |
2002 | if (c == EOF) |
2029 | if (c == EOF) |
2003 | return; |
2030 | return; |
2004 | |
2031 | |
2005 | pt = SCHEME_V->inport->object.port; |
2032 | pt = port (SCHEME_V->inport); |
2006 | pt->unget = c; |
2033 | pt->unget = c; |
2007 | #else |
2034 | #else |
2008 | if (c == EOF) |
2035 | if (c == EOF) |
2009 | return; |
2036 | return; |
2010 | |
2037 | |
2011 | ungot = c; |
2038 | ungot = c; |
2012 | #endif |
2039 | #endif |
2013 | } |
2040 | } |
2014 | |
2041 | |
2015 | #if USE_PORTS |
2042 | #if USE_PORTS |
2016 | static int |
2043 | ecb_cold static int |
2017 | realloc_port_string (SCHEME_P_ port *p) |
2044 | realloc_port_string (SCHEME_P_ port *p) |
2018 | { |
2045 | { |
2019 | char *start = p->rep.string.start; |
2046 | char *start = p->rep.string.start; |
2020 | size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; |
2047 | size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; |
2021 | char *str = malloc (new_size); |
2048 | char *str = malloc (new_size); |
… | |
… | |
2034 | else |
2061 | else |
2035 | return 0; |
2062 | return 0; |
2036 | } |
2063 | } |
2037 | #endif |
2064 | #endif |
2038 | |
2065 | |
2039 | INTERFACE void |
2066 | ecb_cold INTERFACE void |
2040 | putstr (SCHEME_P_ const char *s) |
2067 | putstr (SCHEME_P_ const char *s) |
2041 | { |
2068 | { |
2042 | #if USE_PORTS |
2069 | #if USE_PORTS |
2043 | port *pt = SCHEME_V->outport->object.port; |
2070 | port *pt = port (SCHEME_V->outport); |
2044 | |
2071 | |
2045 | if (pt->kind & port_file) |
2072 | if (pt->kind & port_file) |
2046 | write (pt->rep.stdio.file, s, strlen (s)); |
2073 | write (pt->rep.stdio.file, s, strlen (s)); |
2047 | else |
2074 | else |
2048 | for (; *s; s++) |
2075 | for (; *s; s++) |
… | |
… | |
2050 | *pt->rep.string.curr++ = *s; |
2077 | *pt->rep.string.curr++ = *s; |
2051 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2078 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2052 | *pt->rep.string.curr++ = *s; |
2079 | *pt->rep.string.curr++ = *s; |
2053 | |
2080 | |
2054 | #else |
2081 | #else |
2055 | xwrstr (s); |
2082 | write (pt->rep.stdio.file, s, strlen (s)); |
2056 | #endif |
2083 | #endif |
2057 | } |
2084 | } |
2058 | |
2085 | |
2059 | static void |
2086 | ecb_cold static void |
2060 | putchars (SCHEME_P_ const char *s, int len) |
2087 | putchars (SCHEME_P_ const char *s, int len) |
2061 | { |
2088 | { |
2062 | #if USE_PORTS |
2089 | #if USE_PORTS |
2063 | port *pt = SCHEME_V->outport->object.port; |
2090 | port *pt = port (SCHEME_V->outport); |
2064 | |
2091 | |
2065 | if (pt->kind & port_file) |
2092 | if (pt->kind & port_file) |
2066 | write (pt->rep.stdio.file, s, len); |
2093 | write (pt->rep.stdio.file, s, len); |
2067 | else |
2094 | else |
2068 | { |
2095 | { |
… | |
… | |
2078 | #else |
2105 | #else |
2079 | write (1, s, len); |
2106 | write (1, s, len); |
2080 | #endif |
2107 | #endif |
2081 | } |
2108 | } |
2082 | |
2109 | |
2083 | INTERFACE void |
2110 | ecb_cold INTERFACE void |
2084 | putcharacter (SCHEME_P_ int c) |
2111 | putcharacter (SCHEME_P_ int c) |
2085 | { |
2112 | { |
2086 | #if USE_PORTS |
2113 | #if USE_PORTS |
2087 | port *pt = SCHEME_V->outport->object.port; |
2114 | port *pt = port (SCHEME_V->outport); |
2088 | |
2115 | |
2089 | if (pt->kind & port_file) |
2116 | if (pt->kind & port_file) |
2090 | { |
2117 | { |
2091 | char cc = c; |
2118 | char cc = c; |
2092 | write (pt->rep.stdio.file, &cc, 1); |
2119 | write (pt->rep.stdio.file, &cc, 1); |
… | |
… | |
2104 | write (1, &c, 1); |
2131 | write (1, &c, 1); |
2105 | #endif |
2132 | #endif |
2106 | } |
2133 | } |
2107 | |
2134 | |
2108 | /* read characters up to delimiter, but cater to character constants */ |
2135 | /* read characters up to delimiter, but cater to character constants */ |
2109 | static char * |
2136 | ecb_cold static char * |
2110 | readstr_upto (SCHEME_P_ char *delim) |
2137 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2111 | { |
2138 | { |
2112 | char *p = SCHEME_V->strbuff; |
2139 | char *p = SCHEME_V->strbuff + skip; |
2113 | |
2140 | |
2114 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2141 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2115 | |
2142 | |
2116 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2143 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2117 | *p = 0; |
2144 | *p = 0; |
… | |
… | |
2123 | |
2150 | |
2124 | return SCHEME_V->strbuff; |
2151 | return SCHEME_V->strbuff; |
2125 | } |
2152 | } |
2126 | |
2153 | |
2127 | /* read string expression "xxx...xxx" */ |
2154 | /* read string expression "xxx...xxx" */ |
2128 | static pointer |
2155 | ecb_cold static pointer |
2129 | readstrexp (SCHEME_P) |
2156 | readstrexp (SCHEME_P_ char delim) |
2130 | { |
2157 | { |
2131 | char *p = SCHEME_V->strbuff; |
2158 | char *p = SCHEME_V->strbuff; |
2132 | int c; |
2159 | int c; |
2133 | int c1 = 0; |
2160 | int c1 = 0; |
2134 | enum |
|
|
2135 | { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2161 | enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2136 | |
2162 | |
2137 | for (;;) |
2163 | for (;;) |
2138 | { |
2164 | { |
2139 | c = inchar (SCHEME_A); |
2165 | c = inchar (SCHEME_A); |
2140 | |
2166 | |
… | |
… | |
2142 | return S_F; |
2168 | return S_F; |
2143 | |
2169 | |
2144 | switch (state) |
2170 | switch (state) |
2145 | { |
2171 | { |
2146 | case st_ok: |
2172 | case st_ok: |
2147 | switch (c) |
2173 | if (ecb_expect_false (c == delim)) |
2148 | { |
|
|
2149 | case '\\': |
|
|
2150 | state = st_bsl; |
|
|
2151 | break; |
|
|
2152 | |
|
|
2153 | case '"': |
|
|
2154 | *p = 0; |
|
|
2155 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2174 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2156 | |
2175 | |
2157 | default: |
2176 | if (ecb_expect_false (c == '\\')) |
|
|
2177 | state = st_bsl; |
|
|
2178 | else |
2158 | *p++ = c; |
2179 | *p++ = c; |
2159 | break; |
|
|
2160 | } |
|
|
2161 | |
2180 | |
2162 | break; |
2181 | break; |
2163 | |
2182 | |
2164 | case st_bsl: |
2183 | case st_bsl: |
2165 | switch (c) |
2184 | switch (c) |
… | |
… | |
2174 | case '7': |
2193 | case '7': |
2175 | state = st_oct1; |
2194 | state = st_oct1; |
2176 | c1 = c - '0'; |
2195 | c1 = c - '0'; |
2177 | break; |
2196 | break; |
2178 | |
2197 | |
|
|
2198 | case 'a': *p++ = '\a'; state = st_ok; break; |
|
|
2199 | case 'n': *p++ = '\n'; state = st_ok; break; |
|
|
2200 | case 'r': *p++ = '\r'; state = st_ok; break; |
|
|
2201 | case 't': *p++ = '\t'; state = st_ok; break; |
|
|
2202 | |
|
|
2203 | //TODO: \whitespace eol whitespace |
|
|
2204 | |
|
|
2205 | //TODO: x should end in ;, not two-digit hex |
2179 | case 'x': |
2206 | case 'x': |
2180 | case 'X': |
2207 | case 'X': |
2181 | state = st_x1; |
2208 | state = st_x1; |
2182 | c1 = 0; |
2209 | c1 = 0; |
2183 | break; |
2210 | break; |
2184 | |
2211 | |
2185 | case 'n': |
|
|
2186 | *p++ = '\n'; |
|
|
2187 | state = st_ok; |
|
|
2188 | break; |
|
|
2189 | |
|
|
2190 | case 't': |
|
|
2191 | *p++ = '\t'; |
|
|
2192 | state = st_ok; |
|
|
2193 | break; |
|
|
2194 | |
|
|
2195 | case 'r': |
|
|
2196 | *p++ = '\r'; |
|
|
2197 | state = st_ok; |
|
|
2198 | break; |
|
|
2199 | |
|
|
2200 | case '"': |
|
|
2201 | *p++ = '"'; |
|
|
2202 | state = st_ok; |
|
|
2203 | break; |
|
|
2204 | |
|
|
2205 | default: |
2212 | default: |
2206 | *p++ = c; |
2213 | *p++ = c; |
2207 | state = st_ok; |
2214 | state = st_ok; |
2208 | break; |
2215 | break; |
2209 | } |
2216 | } |
2210 | |
2217 | |
2211 | break; |
2218 | break; |
2212 | |
2219 | |
2213 | case st_x1: |
2220 | case st_x1: |
2214 | case st_x2: |
2221 | case st_x2: |
2215 | c = toupper (c); |
2222 | c = tolower (c); |
2216 | |
2223 | |
2217 | if (c >= '0' && c <= 'F') |
2224 | if (c >= '0' && c <= '9') |
2218 | { |
|
|
2219 | if (c <= '9') |
|
|
2220 | c1 = (c1 << 4) + c - '0'; |
2225 | c1 = (c1 << 4) + c - '0'; |
2221 | else |
2226 | else if (c >= 'a' && c <= 'f') |
2222 | c1 = (c1 << 4) + c - 'A' + 10; |
2227 | c1 = (c1 << 4) + c - 'a' + 10; |
2223 | |
|
|
2224 | if (state == st_x1) |
|
|
2225 | state = st_x2; |
|
|
2226 | else |
|
|
2227 | { |
|
|
2228 | *p++ = c1; |
|
|
2229 | state = st_ok; |
|
|
2230 | } |
|
|
2231 | } |
|
|
2232 | else |
2228 | else |
2233 | return S_F; |
2229 | return S_F; |
|
|
2230 | |
|
|
2231 | if (state == st_x1) |
|
|
2232 | state = st_x2; |
|
|
2233 | else |
|
|
2234 | { |
|
|
2235 | *p++ = c1; |
|
|
2236 | state = st_ok; |
|
|
2237 | } |
2234 | |
2238 | |
2235 | break; |
2239 | break; |
2236 | |
2240 | |
2237 | case st_oct1: |
2241 | case st_oct1: |
2238 | case st_oct2: |
2242 | case st_oct2: |
… | |
… | |
2242 | backchar (SCHEME_A_ c); |
2246 | backchar (SCHEME_A_ c); |
2243 | state = st_ok; |
2247 | state = st_ok; |
2244 | } |
2248 | } |
2245 | else |
2249 | else |
2246 | { |
2250 | { |
2247 | if (state == st_oct2 && c1 >= 32) |
2251 | if (state == st_oct2 && c1 >= ' ') |
2248 | return S_F; |
2252 | return S_F; |
2249 | |
2253 | |
2250 | c1 = (c1 << 3) + (c - '0'); |
2254 | c1 = (c1 << 3) + (c - '0'); |
2251 | |
2255 | |
2252 | if (state == st_oct1) |
2256 | if (state == st_oct1) |
… | |
… | |
2257 | state = st_ok; |
2261 | state = st_ok; |
2258 | } |
2262 | } |
2259 | } |
2263 | } |
2260 | |
2264 | |
2261 | break; |
2265 | break; |
2262 | |
|
|
2263 | } |
2266 | } |
2264 | } |
2267 | } |
2265 | } |
2268 | } |
2266 | |
2269 | |
2267 | /* check c is in chars */ |
2270 | /* check c is in chars */ |
2268 | ecb_inline int |
2271 | ecb_cold int |
2269 | is_one_of (char *s, int c) |
2272 | is_one_of (const char *s, int c) |
2270 | { |
2273 | { |
2271 | if (c == EOF) |
|
|
2272 | return 1; |
|
|
2273 | |
|
|
2274 | return !!strchr (s, c); |
2274 | return c == EOF || !!strchr (s, c); |
2275 | } |
2275 | } |
2276 | |
2276 | |
2277 | /* skip white characters */ |
2277 | /* skip white characters */ |
2278 | ecb_inline int |
2278 | ecb_cold int |
2279 | skipspace (SCHEME_P) |
2279 | skipspace (SCHEME_P) |
2280 | { |
2280 | { |
2281 | int c, curr_line = 0; |
2281 | int c, curr_line = 0; |
2282 | |
2282 | |
2283 | do |
2283 | do |
2284 | { |
2284 | { |
2285 | c = inchar (SCHEME_A); |
2285 | c = inchar (SCHEME_A); |
|
|
2286 | |
2286 | #if SHOW_ERROR_LINE |
2287 | #if SHOW_ERROR_LINE |
2287 | if (c == '\n') |
2288 | if (ecb_expect_false (c == '\n')) |
2288 | curr_line++; |
2289 | curr_line++; |
2289 | #endif |
2290 | #endif |
|
|
2291 | |
|
|
2292 | if (ecb_expect_false (c == EOF)) |
|
|
2293 | return c; |
2290 | } |
2294 | } |
2291 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2295 | while (is_one_of (WHITESPACE, c)); |
2292 | |
2296 | |
2293 | /* record it */ |
2297 | /* record it */ |
2294 | #if SHOW_ERROR_LINE |
2298 | #if SHOW_ERROR_LINE |
2295 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2299 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2296 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2300 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2297 | #endif |
2301 | #endif |
2298 | |
2302 | |
2299 | if (c != EOF) |
|
|
2300 | { |
|
|
2301 | backchar (SCHEME_A_ c); |
2303 | backchar (SCHEME_A_ c); |
2302 | return 1; |
2304 | return 1; |
2303 | } |
|
|
2304 | else |
|
|
2305 | return EOF; |
|
|
2306 | } |
2305 | } |
2307 | |
2306 | |
2308 | /* get token */ |
2307 | /* get token */ |
2309 | static int |
2308 | ecb_cold static int |
2310 | token (SCHEME_P) |
2309 | token (SCHEME_P) |
2311 | { |
2310 | { |
2312 | int c = skipspace (SCHEME_A); |
2311 | int c = skipspace (SCHEME_A); |
2313 | |
2312 | |
2314 | if (c == EOF) |
2313 | if (c == EOF) |
… | |
… | |
2326 | return TOK_RPAREN; |
2325 | return TOK_RPAREN; |
2327 | |
2326 | |
2328 | case '.': |
2327 | case '.': |
2329 | c = inchar (SCHEME_A); |
2328 | c = inchar (SCHEME_A); |
2330 | |
2329 | |
2331 | if (is_one_of (" \n\t", c)) |
2330 | if (is_one_of (WHITESPACE, c)) |
2332 | return TOK_DOT; |
2331 | return TOK_DOT; |
2333 | else |
2332 | else |
2334 | { |
2333 | { |
2335 | //TODO: ungetc twice in a row is not supported in C |
|
|
2336 | backchar (SCHEME_A_ c); |
2334 | backchar (SCHEME_A_ c); |
2337 | backchar (SCHEME_A_ '.'); |
|
|
2338 | return TOK_ATOM; |
2335 | return TOK_DOTATOM; |
2339 | } |
2336 | } |
|
|
2337 | |
|
|
2338 | case '|': |
|
|
2339 | return TOK_STRATOM; |
2340 | |
2340 | |
2341 | case '\'': |
2341 | case '\'': |
2342 | return TOK_QUOTE; |
2342 | return TOK_QUOTE; |
2343 | |
2343 | |
2344 | case ';': |
2344 | case ';': |
… | |
… | |
2411 | } |
2411 | } |
2412 | |
2412 | |
2413 | /* ========== Routines for Printing ========== */ |
2413 | /* ========== Routines for Printing ========== */ |
2414 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2414 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2415 | |
2415 | |
2416 | static void |
2416 | ecb_cold static void |
2417 | printslashstring (SCHEME_P_ char *p, int len) |
2417 | printslashstring (SCHEME_P_ char *p, int len) |
2418 | { |
2418 | { |
2419 | int i; |
2419 | int i; |
2420 | unsigned char *s = (unsigned char *) p; |
2420 | unsigned char *s = (unsigned char *) p; |
2421 | |
2421 | |
… | |
… | |
2476 | } |
2476 | } |
2477 | |
2477 | |
2478 | putcharacter (SCHEME_A_ '"'); |
2478 | putcharacter (SCHEME_A_ '"'); |
2479 | } |
2479 | } |
2480 | |
2480 | |
2481 | |
|
|
2482 | /* print atoms */ |
2481 | /* print atoms */ |
2483 | static void |
2482 | ecb_cold static void |
2484 | printatom (SCHEME_P_ pointer l, int f) |
2483 | printatom (SCHEME_P_ pointer l, int f) |
2485 | { |
2484 | { |
2486 | char *p; |
2485 | char *p; |
2487 | int len; |
2486 | int len; |
2488 | |
2487 | |
2489 | atom2str (SCHEME_A_ l, f, &p, &len); |
2488 | atom2str (SCHEME_A_ l, f, &p, &len); |
2490 | putchars (SCHEME_A_ p, len); |
2489 | putchars (SCHEME_A_ p, len); |
2491 | } |
2490 | } |
2492 | |
2491 | |
2493 | |
|
|
2494 | /* Uses internal buffer unless string pointer is already available */ |
2492 | /* Uses internal buffer unless string pointer is already available */ |
2495 | static void |
2493 | ecb_cold static void |
2496 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2494 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2497 | { |
2495 | { |
2498 | char *p; |
2496 | char *p; |
2499 | |
2497 | |
2500 | if (l == NIL) |
2498 | if (l == NIL) |
… | |
… | |
2658 | #endif |
2656 | #endif |
2659 | } |
2657 | } |
2660 | else if (is_continuation (l)) |
2658 | else if (is_continuation (l)) |
2661 | p = "#<CONTINUATION>"; |
2659 | p = "#<CONTINUATION>"; |
2662 | else |
2660 | else |
|
|
2661 | { |
|
|
2662 | #if USE_PRINTF |
|
|
2663 | p = SCHEME_V->strbuff; |
|
|
2664 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2665 | #else |
2663 | p = "#<ERROR>"; |
2666 | p = "#<ERROR>"; |
|
|
2667 | #endif |
|
|
2668 | } |
2664 | |
2669 | |
2665 | *pp = p; |
2670 | *pp = p; |
2666 | *plen = strlen (p); |
2671 | *plen = strlen (p); |
2667 | } |
2672 | } |
2668 | |
2673 | |
… | |
… | |
2700 | return car (d); |
2705 | return car (d); |
2701 | |
2706 | |
2702 | p = cons (car (d), cdr (d)); |
2707 | p = cons (car (d), cdr (d)); |
2703 | q = p; |
2708 | q = p; |
2704 | |
2709 | |
2705 | while (cdr (cdr (p)) != NIL) |
2710 | while (cddr (p) != NIL) |
2706 | { |
2711 | { |
2707 | d = cons (car (p), cdr (p)); |
2712 | d = cons (car (p), cdr (p)); |
2708 | |
2713 | |
2709 | if (cdr (cdr (p)) != NIL) |
2714 | if (cddr (p) != NIL) |
2710 | p = cdr (d); |
2715 | p = cdr (d); |
2711 | } |
2716 | } |
2712 | |
2717 | |
2713 | set_cdr (p, car (cdr (p))); |
2718 | set_cdr (p, cadr (p)); |
2714 | return q; |
2719 | return q; |
2715 | } |
2720 | } |
2716 | |
2721 | |
2717 | /* reverse list -- produce new list */ |
2722 | /* reverse list -- produce new list */ |
2718 | static pointer |
2723 | ecb_hot static pointer |
2719 | reverse (SCHEME_P_ pointer a) |
2724 | reverse (SCHEME_P_ pointer a) |
2720 | { |
2725 | { |
2721 | /* a must be checked by gc */ |
2726 | /* a must be checked by gc */ |
2722 | pointer p = NIL; |
2727 | pointer p = NIL; |
2723 | |
2728 | |
… | |
… | |
2726 | |
2731 | |
2727 | return p; |
2732 | return p; |
2728 | } |
2733 | } |
2729 | |
2734 | |
2730 | /* reverse list --- in-place */ |
2735 | /* reverse list --- in-place */ |
2731 | static pointer |
2736 | ecb_hot static pointer |
2732 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2737 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2733 | { |
2738 | { |
2734 | pointer result = term; |
2739 | pointer result = term; |
2735 | pointer p = list; |
2740 | pointer p = list; |
2736 | |
2741 | |
… | |
… | |
2744 | |
2749 | |
2745 | return result; |
2750 | return result; |
2746 | } |
2751 | } |
2747 | |
2752 | |
2748 | /* append list -- produce new list (in reverse order) */ |
2753 | /* append list -- produce new list (in reverse order) */ |
2749 | static pointer |
2754 | ecb_hot static pointer |
2750 | revappend (SCHEME_P_ pointer a, pointer b) |
2755 | revappend (SCHEME_P_ pointer a, pointer b) |
2751 | { |
2756 | { |
2752 | pointer result = a; |
2757 | pointer result = a; |
2753 | pointer p = b; |
2758 | pointer p = b; |
2754 | |
2759 | |
… | |
… | |
2763 | |
2768 | |
2764 | return S_F; /* signal an error */ |
2769 | return S_F; /* signal an error */ |
2765 | } |
2770 | } |
2766 | |
2771 | |
2767 | /* equivalence of atoms */ |
2772 | /* equivalence of atoms */ |
2768 | int |
2773 | ecb_hot int |
2769 | eqv (pointer a, pointer b) |
2774 | eqv (pointer a, pointer b) |
2770 | { |
2775 | { |
2771 | if (is_string (a)) |
2776 | if (is_string (a)) |
2772 | { |
2777 | { |
2773 | if (is_string (b)) |
2778 | if (is_string (b)) |
… | |
… | |
2812 | /* () is #t in R5RS */ |
2817 | /* () is #t in R5RS */ |
2813 | #define is_true(p) ((p) != S_F) |
2818 | #define is_true(p) ((p) != S_F) |
2814 | #define is_false(p) ((p) == S_F) |
2819 | #define is_false(p) ((p) == S_F) |
2815 | |
2820 | |
2816 | /* ========== Environment implementation ========== */ |
2821 | /* ========== Environment implementation ========== */ |
2817 | |
|
|
2818 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
|
|
2819 | |
|
|
2820 | static int |
|
|
2821 | hash_fn (const char *key, int table_size) |
|
|
2822 | { |
|
|
2823 | const unsigned char *p = key; |
|
|
2824 | uint32_t hash = 2166136261; |
|
|
2825 | |
|
|
2826 | while (*p) |
|
|
2827 | hash = (hash ^ *p++) * 16777619; |
|
|
2828 | |
|
|
2829 | return hash % table_size; |
|
|
2830 | } |
|
|
2831 | #endif |
|
|
2832 | |
2822 | |
2833 | #ifndef USE_ALIST_ENV |
2823 | #ifndef USE_ALIST_ENV |
2834 | |
2824 | |
2835 | /* |
2825 | /* |
2836 | * In this implementation, each frame of the environment may be |
2826 | * In this implementation, each frame of the environment may be |
… | |
… | |
2853 | |
2843 | |
2854 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2844 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2855 | setenvironment (SCHEME_V->envir); |
2845 | setenvironment (SCHEME_V->envir); |
2856 | } |
2846 | } |
2857 | |
2847 | |
|
|
2848 | static uint32_t |
|
|
2849 | sym_hash (pointer sym, uint32_t size) |
|
|
2850 | { |
|
|
2851 | uintptr_t ptr = (uintptr_t)sym; |
|
|
2852 | |
|
|
2853 | #if 0 |
|
|
2854 | /* table size is prime, so why mix */ |
|
|
2855 | ptr += ptr >> 32; |
|
|
2856 | ptr += ptr >> 16; |
|
|
2857 | ptr += ptr >> 8; |
|
|
2858 | #endif |
|
|
2859 | |
|
|
2860 | return ptr % size; |
|
|
2861 | } |
|
|
2862 | |
2858 | ecb_inline void |
2863 | ecb_inline void |
2859 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2864 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2860 | { |
2865 | { |
2861 | pointer slot = immutable_cons (variable, value); |
2866 | pointer slot = immutable_cons (variable, value); |
2862 | |
2867 | |
2863 | if (is_vector (car (env))) |
2868 | if (is_vector (car (env))) |
2864 | { |
2869 | { |
2865 | int location = hash_fn (symname (variable), veclength (car (env))); |
2870 | int location = sym_hash (variable, veclength (car (env))); |
2866 | |
|
|
2867 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2871 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2868 | } |
2872 | } |
2869 | else |
2873 | else |
2870 | set_car (env, immutable_cons (slot, car (env))); |
2874 | set_car (env, immutable_cons (slot, car (env))); |
2871 | } |
2875 | } |
2872 | |
2876 | |
2873 | static pointer |
2877 | ecb_hot static pointer |
2874 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2878 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2875 | { |
2879 | { |
2876 | pointer x, y; |
2880 | pointer x, y; |
2877 | int location; |
|
|
2878 | |
2881 | |
2879 | for (x = env; x != NIL; x = cdr (x)) |
2882 | for (x = env; x != NIL; x = cdr (x)) |
2880 | { |
2883 | { |
2881 | if (is_vector (car (x))) |
2884 | if (is_vector (car (x))) |
2882 | { |
2885 | { |
2883 | location = hash_fn (symname (hdl), veclength (car (x))); |
2886 | int location = sym_hash (hdl, veclength (car (x))); |
2884 | y = vector_get (car (x), location); |
2887 | y = vector_get (car (x), location); |
2885 | } |
2888 | } |
2886 | else |
2889 | else |
2887 | y = car (x); |
2890 | y = car (x); |
2888 | |
2891 | |
… | |
… | |
2900 | return NIL; |
2903 | return NIL; |
2901 | } |
2904 | } |
2902 | |
2905 | |
2903 | #else /* USE_ALIST_ENV */ |
2906 | #else /* USE_ALIST_ENV */ |
2904 | |
2907 | |
2905 | ecb_inline void |
2908 | static void |
2906 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2909 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2907 | { |
2910 | { |
2908 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2911 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2909 | setenvironment (SCHEME_V->envir); |
2912 | setenvironment (SCHEME_V->envir); |
2910 | } |
2913 | } |
2911 | |
2914 | |
2912 | ecb_inline void |
2915 | static void |
2913 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2916 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2914 | { |
2917 | { |
2915 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2918 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2916 | } |
2919 | } |
2917 | |
2920 | |
2918 | static pointer |
2921 | ecb_hot static pointer |
2919 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2922 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2920 | { |
2923 | { |
2921 | pointer x, y; |
2924 | pointer x, y; |
2922 | |
2925 | |
2923 | for (x = env; x != NIL; x = cdr (x)) |
2926 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2925 | for (y = car (x); y != NIL; y = cdr (y)) |
2928 | for (y = car (x); y != NIL; y = cdr (y)) |
2926 | if (caar (y) == hdl) |
2929 | if (caar (y) == hdl) |
2927 | break; |
2930 | break; |
2928 | |
2931 | |
2929 | if (y != NIL) |
2932 | if (y != NIL) |
|
|
2933 | return car (y); |
2930 | break; |
2934 | break; |
2931 | |
2935 | |
2932 | if (!all) |
2936 | if (!all) |
2933 | return NIL; |
2937 | break; |
2934 | } |
2938 | } |
2935 | |
|
|
2936 | if (x != NIL) |
|
|
2937 | return car (y); |
|
|
2938 | |
2939 | |
2939 | return NIL; |
2940 | return NIL; |
2940 | } |
2941 | } |
2941 | |
2942 | |
2942 | #endif /* USE_ALIST_ENV else */ |
2943 | #endif /* USE_ALIST_ENV else */ |
2943 | |
2944 | |
2944 | ecb_inline void |
2945 | static void |
2945 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2946 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2946 | { |
2947 | { |
|
|
2948 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2947 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2949 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2948 | } |
2950 | } |
2949 | |
2951 | |
2950 | ecb_inline void |
2952 | static void |
2951 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2953 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2952 | { |
2954 | { |
2953 | set_cdr (slot, value); |
2955 | set_cdr (slot, value); |
2954 | } |
2956 | } |
2955 | |
2957 | |
2956 | ecb_inline pointer |
2958 | static pointer |
2957 | slot_value_in_env (pointer slot) |
2959 | slot_value_in_env (pointer slot) |
2958 | { |
2960 | { |
2959 | return cdr (slot); |
2961 | return cdr (slot); |
2960 | } |
2962 | } |
2961 | |
2963 | |
2962 | /* ========== Evaluation Cycle ========== */ |
2964 | /* ========== Evaluation Cycle ========== */ |
2963 | |
2965 | |
2964 | static int |
2966 | ecb_cold static int |
2965 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2967 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2966 | { |
2968 | { |
2967 | #if USE_ERROR_HOOK |
2969 | #if USE_ERROR_HOOK |
2968 | pointer x; |
2970 | pointer x; |
2969 | pointer hdl = SCHEME_V->ERROR_HOOK; |
2971 | pointer hdl = SCHEME_V->ERROR_HOOK; |
… | |
… | |
3045 | pointer code; |
3047 | pointer code; |
3046 | }; |
3048 | }; |
3047 | |
3049 | |
3048 | # define STACK_GROWTH 3 |
3050 | # define STACK_GROWTH 3 |
3049 | |
3051 | |
3050 | static void |
3052 | ecb_hot static void |
3051 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3053 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3052 | { |
3054 | { |
3053 | int nframes = (uintptr_t)SCHEME_V->dump; |
3055 | int nframes = (uintptr_t)SCHEME_V->dump; |
3054 | struct dump_stack_frame *next_frame; |
3056 | struct dump_stack_frame *next_frame; |
3055 | |
3057 | |
3056 | /* enough room for the next frame? */ |
3058 | /* enough room for the next frame? */ |
3057 | if (nframes >= SCHEME_V->dump_size) |
3059 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3058 | { |
3060 | { |
3059 | SCHEME_V->dump_size += STACK_GROWTH; |
3061 | SCHEME_V->dump_size += STACK_GROWTH; |
3060 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3062 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3061 | } |
3063 | } |
3062 | |
3064 | |
… | |
… | |
3068 | next_frame->code = code; |
3070 | next_frame->code = code; |
3069 | |
3071 | |
3070 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3072 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3071 | } |
3073 | } |
3072 | |
3074 | |
3073 | static int |
3075 | static ecb_hot int |
3074 | xs_return (SCHEME_P_ pointer a) |
3076 | xs_return (SCHEME_P_ pointer a) |
3075 | { |
3077 | { |
3076 | int nframes = (uintptr_t)SCHEME_V->dump; |
3078 | int nframes = (uintptr_t)SCHEME_V->dump; |
3077 | struct dump_stack_frame *frame; |
3079 | struct dump_stack_frame *frame; |
3078 | |
3080 | |
… | |
… | |
3089 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3091 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3090 | |
3092 | |
3091 | return 0; |
3093 | return 0; |
3092 | } |
3094 | } |
3093 | |
3095 | |
3094 | ecb_inline void |
3096 | ecb_cold void |
3095 | dump_stack_reset (SCHEME_P) |
3097 | dump_stack_reset (SCHEME_P) |
3096 | { |
3098 | { |
3097 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3099 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3098 | SCHEME_V->dump = (pointer)+0; |
3100 | SCHEME_V->dump = (pointer)+0; |
3099 | } |
3101 | } |
3100 | |
3102 | |
3101 | ecb_inline void |
3103 | ecb_cold void |
3102 | dump_stack_initialize (SCHEME_P) |
3104 | dump_stack_initialize (SCHEME_P) |
3103 | { |
3105 | { |
3104 | SCHEME_V->dump_size = 0; |
3106 | SCHEME_V->dump_size = 0; |
3105 | SCHEME_V->dump_base = 0; |
3107 | SCHEME_V->dump_base = 0; |
3106 | dump_stack_reset (SCHEME_A); |
3108 | dump_stack_reset (SCHEME_A); |
3107 | } |
3109 | } |
3108 | |
3110 | |
3109 | static void |
3111 | ecb_cold static void |
3110 | dump_stack_free (SCHEME_P) |
3112 | dump_stack_free (SCHEME_P) |
3111 | { |
3113 | { |
3112 | free (SCHEME_V->dump_base); |
3114 | free (SCHEME_V->dump_base); |
3113 | SCHEME_V->dump_base = 0; |
3115 | SCHEME_V->dump_base = 0; |
3114 | SCHEME_V->dump = (pointer)0; |
3116 | SCHEME_V->dump = (pointer)0; |
3115 | SCHEME_V->dump_size = 0; |
3117 | SCHEME_V->dump_size = 0; |
3116 | } |
3118 | } |
3117 | |
3119 | |
3118 | static void |
3120 | ecb_cold static void |
3119 | dump_stack_mark (SCHEME_P) |
3121 | dump_stack_mark (SCHEME_P) |
3120 | { |
3122 | { |
3121 | int nframes = (uintptr_t)SCHEME_V->dump; |
3123 | int nframes = (uintptr_t)SCHEME_V->dump; |
3122 | int i; |
3124 | int i; |
3123 | |
3125 | |
… | |
… | |
3129 | mark (frame->envir); |
3131 | mark (frame->envir); |
3130 | mark (frame->code); |
3132 | mark (frame->code); |
3131 | } |
3133 | } |
3132 | } |
3134 | } |
3133 | |
3135 | |
3134 | static pointer |
3136 | ecb_cold static pointer |
3135 | ss_get_cont (SCHEME_P) |
3137 | ss_get_cont (SCHEME_P) |
3136 | { |
3138 | { |
3137 | int nframes = (uintptr_t)SCHEME_V->dump; |
3139 | int nframes = (uintptr_t)SCHEME_V->dump; |
3138 | int i; |
3140 | int i; |
3139 | |
3141 | |
… | |
… | |
3151 | } |
3153 | } |
3152 | |
3154 | |
3153 | return cont; |
3155 | return cont; |
3154 | } |
3156 | } |
3155 | |
3157 | |
3156 | static void |
3158 | ecb_cold static void |
3157 | ss_set_cont (SCHEME_P_ pointer cont) |
3159 | ss_set_cont (SCHEME_P_ pointer cont) |
3158 | { |
3160 | { |
3159 | int i = 0; |
3161 | int i = 0; |
3160 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3162 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3161 | |
3163 | |
… | |
… | |
3173 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3175 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3174 | } |
3176 | } |
3175 | |
3177 | |
3176 | #else |
3178 | #else |
3177 | |
3179 | |
3178 | ecb_inline void |
3180 | ecb_cold void |
3179 | dump_stack_reset (SCHEME_P) |
3181 | dump_stack_reset (SCHEME_P) |
3180 | { |
3182 | { |
3181 | SCHEME_V->dump = NIL; |
3183 | SCHEME_V->dump = NIL; |
3182 | } |
3184 | } |
3183 | |
3185 | |
3184 | ecb_inline void |
3186 | ecb_cold void |
3185 | dump_stack_initialize (SCHEME_P) |
3187 | dump_stack_initialize (SCHEME_P) |
3186 | { |
3188 | { |
3187 | dump_stack_reset (SCHEME_A); |
3189 | dump_stack_reset (SCHEME_A); |
3188 | } |
3190 | } |
3189 | |
3191 | |
3190 | static void |
3192 | ecb_cold static void |
3191 | dump_stack_free (SCHEME_P) |
3193 | dump_stack_free (SCHEME_P) |
3192 | { |
3194 | { |
3193 | SCHEME_V->dump = NIL; |
3195 | SCHEME_V->dump = NIL; |
3194 | } |
3196 | } |
3195 | |
3197 | |
3196 | static int |
3198 | ecb_hot static int |
3197 | xs_return (SCHEME_P_ pointer a) |
3199 | xs_return (SCHEME_P_ pointer a) |
3198 | { |
3200 | { |
3199 | pointer dump = SCHEME_V->dump; |
3201 | pointer dump = SCHEME_V->dump; |
3200 | |
3202 | |
3201 | SCHEME_V->value = a; |
3203 | SCHEME_V->value = a; |
… | |
… | |
3211 | SCHEME_V->dump = dump; |
3213 | SCHEME_V->dump = dump; |
3212 | |
3214 | |
3213 | return 0; |
3215 | return 0; |
3214 | } |
3216 | } |
3215 | |
3217 | |
3216 | static void |
3218 | ecb_hot static void |
3217 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3219 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3218 | { |
3220 | { |
3219 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3221 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3220 | cons (args, |
3222 | cons (args, |
3221 | cons (SCHEME_V->envir, |
3223 | cons (SCHEME_V->envir, |
3222 | cons (code, |
3224 | cons (code, |
3223 | SCHEME_V->dump)))); |
3225 | SCHEME_V->dump)))); |
3224 | } |
3226 | } |
3225 | |
3227 | |
3226 | static void |
3228 | ecb_cold static void |
3227 | dump_stack_mark (SCHEME_P) |
3229 | dump_stack_mark (SCHEME_P) |
3228 | { |
3230 | { |
3229 | mark (SCHEME_V->dump); |
3231 | mark (SCHEME_V->dump); |
3230 | } |
3232 | } |
3231 | |
3233 | |
3232 | static pointer |
3234 | ecb_cold static pointer |
3233 | ss_get_cont (SCHEME_P) |
3235 | ss_get_cont (SCHEME_P) |
3234 | { |
3236 | { |
3235 | return SCHEME_V->dump; |
3237 | return SCHEME_V->dump; |
3236 | } |
3238 | } |
3237 | |
3239 | |
3238 | static void |
3240 | ecb_cold static void |
3239 | ss_set_cont (SCHEME_P_ pointer cont) |
3241 | ss_set_cont (SCHEME_P_ pointer cont) |
3240 | { |
3242 | { |
3241 | SCHEME_V->dump = cont; |
3243 | SCHEME_V->dump = cont; |
3242 | } |
3244 | } |
3243 | |
3245 | |
3244 | #endif |
3246 | #endif |
3245 | |
3247 | |
3246 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3248 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3247 | |
3249 | |
|
|
3250 | #if EXPERIMENT |
3248 | static int |
3251 | static int |
|
|
3252 | debug (SCHEME_P_ int indent, pointer x) |
|
|
3253 | { |
|
|
3254 | int c; |
|
|
3255 | |
|
|
3256 | if (is_syntax (x)) |
|
|
3257 | { |
|
|
3258 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3259 | return 8 + 8; |
|
|
3260 | } |
|
|
3261 | |
|
|
3262 | if (x == NIL) |
|
|
3263 | { |
|
|
3264 | printf ("%*sNIL\n", indent, ""); |
|
|
3265 | return 3; |
|
|
3266 | } |
|
|
3267 | |
|
|
3268 | switch (type (x)) |
|
|
3269 | { |
|
|
3270 | case T_INTEGER: |
|
|
3271 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3272 | return 32+8; |
|
|
3273 | |
|
|
3274 | case T_SYMBOL: |
|
|
3275 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3276 | return 24+8; |
|
|
3277 | |
|
|
3278 | case T_CLOSURE: |
|
|
3279 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3280 | debug (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3281 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3282 | |
|
|
3283 | case T_PAIR: |
|
|
3284 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3285 | c = debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3286 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3287 | return c + 1; |
|
|
3288 | |
|
|
3289 | case T_PORT: |
|
|
3290 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3291 | return 24+8; |
|
|
3292 | |
|
|
3293 | case T_VECTOR: |
|
|
3294 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3295 | return 24+8; |
|
|
3296 | |
|
|
3297 | case T_ENVIRONMENT: |
|
|
3298 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3299 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3300 | |
|
|
3301 | default: |
|
|
3302 | printf ("unhandled type %d\n", type (x)); |
|
|
3303 | break; |
|
|
3304 | } |
|
|
3305 | } |
|
|
3306 | #endif |
|
|
3307 | |
|
|
3308 | /* syntax, eval, core, ... */ |
|
|
3309 | ecb_hot static int |
3249 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3310 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3250 | { |
3311 | { |
3251 | pointer args = SCHEME_V->args; |
3312 | pointer args = SCHEME_V->args; |
3252 | pointer x, y; |
3313 | pointer x, y; |
3253 | |
3314 | |
3254 | switch (op) |
3315 | switch (op) |
3255 | { |
3316 | { |
|
|
3317 | #if EXPERIMENT //D |
|
|
3318 | case OP_DEBUG: |
|
|
3319 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
|
|
3320 | printf ("\n"); |
|
|
3321 | s_return (S_T); |
|
|
3322 | #endif |
3256 | case OP_LOAD: /* load */ |
3323 | case OP_LOAD: /* load */ |
3257 | if (file_interactive (SCHEME_A)) |
3324 | if (file_interactive (SCHEME_A)) |
3258 | { |
3325 | { |
3259 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3326 | putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n"); |
3260 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3327 | //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3261 | } |
3328 | } |
3262 | |
3329 | |
3263 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3330 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3264 | Error_1 ("unable to open", car (args)); |
3331 | Error_1 ("unable to open", car (args)); |
3265 | else |
3332 | else |
… | |
… | |
3269 | } |
3336 | } |
3270 | |
3337 | |
3271 | case OP_T0LVL: /* top level */ |
3338 | case OP_T0LVL: /* top level */ |
3272 | |
3339 | |
3273 | /* If we reached the end of file, this loop is done. */ |
3340 | /* If we reached the end of file, this loop is done. */ |
3274 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3341 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3275 | { |
3342 | { |
3276 | if (SCHEME_V->file_i == 0) |
3343 | if (SCHEME_V->file_i == 0) |
3277 | { |
3344 | { |
3278 | SCHEME_V->args = NIL; |
3345 | SCHEME_V->args = NIL; |
3279 | s_goto (OP_QUIT); |
3346 | s_goto (OP_QUIT); |
… | |
… | |
3357 | #endif |
3424 | #endif |
3358 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3425 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3359 | { |
3426 | { |
3360 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3427 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3361 | |
3428 | |
3362 | if (x != NIL) |
3429 | if (x == NIL) |
3363 | s_return (slot_value_in_env (x)); |
|
|
3364 | else |
|
|
3365 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3430 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3431 | |
|
|
3432 | s_return (slot_value_in_env (x)); |
3366 | } |
3433 | } |
3367 | else if (is_pair (SCHEME_V->code)) |
3434 | else if (is_pair (SCHEME_V->code)) |
3368 | { |
3435 | { |
3369 | x = car (SCHEME_V->code); |
3436 | x = car (SCHEME_V->code); |
3370 | |
3437 | |
… | |
… | |
3383 | } |
3450 | } |
3384 | else |
3451 | else |
3385 | s_return (SCHEME_V->code); |
3452 | s_return (SCHEME_V->code); |
3386 | |
3453 | |
3387 | case OP_E0ARGS: /* eval arguments */ |
3454 | case OP_E0ARGS: /* eval arguments */ |
3388 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3455 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3389 | { |
3456 | { |
3390 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3457 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3391 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3458 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3392 | SCHEME_V->code = SCHEME_V->value; |
3459 | SCHEME_V->code = SCHEME_V->value; |
3393 | s_goto (OP_APPLY); |
3460 | s_goto (OP_APPLY); |
… | |
… | |
3447 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3514 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3448 | else if (is_foreign (SCHEME_V->code)) |
3515 | else if (is_foreign (SCHEME_V->code)) |
3449 | { |
3516 | { |
3450 | /* Keep nested calls from GC'ing the arglist */ |
3517 | /* Keep nested calls from GC'ing the arglist */ |
3451 | push_recent_alloc (SCHEME_A_ args, NIL); |
3518 | push_recent_alloc (SCHEME_A_ args, NIL); |
3452 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3519 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3453 | |
3520 | |
3454 | s_return (x); |
3521 | s_return (x); |
3455 | } |
3522 | } |
3456 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3523 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3457 | { |
3524 | { |
… | |
… | |
3494 | |
3561 | |
3495 | case OP_DOMACRO: /* do macro */ |
3562 | case OP_DOMACRO: /* do macro */ |
3496 | SCHEME_V->code = SCHEME_V->value; |
3563 | SCHEME_V->code = SCHEME_V->value; |
3497 | s_goto (OP_EVAL); |
3564 | s_goto (OP_EVAL); |
3498 | |
3565 | |
3499 | #if 1 |
|
|
3500 | |
|
|
3501 | case OP_LAMBDA: /* lambda */ |
3566 | case OP_LAMBDA: /* lambda */ |
3502 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3567 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3503 | set SCHEME_V->value fall thru */ |
3568 | set SCHEME_V->value fall thru */ |
3504 | { |
3569 | { |
3505 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3570 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
… | |
… | |
3511 | SCHEME_V->code = slot_value_in_env (f); |
3576 | SCHEME_V->code = slot_value_in_env (f); |
3512 | s_goto (OP_APPLY); |
3577 | s_goto (OP_APPLY); |
3513 | } |
3578 | } |
3514 | |
3579 | |
3515 | SCHEME_V->value = SCHEME_V->code; |
3580 | SCHEME_V->value = SCHEME_V->code; |
3516 | /* Fallthru */ |
|
|
3517 | } |
3581 | } |
|
|
3582 | /* Fallthru */ |
3518 | |
3583 | |
3519 | case OP_LAMBDA1: |
3584 | case OP_LAMBDA1: |
3520 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3585 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3521 | |
|
|
3522 | #else |
|
|
3523 | |
|
|
3524 | case OP_LAMBDA: /* lambda */ |
|
|
3525 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
|
|
3526 | |
|
|
3527 | #endif |
|
|
3528 | |
3586 | |
3529 | case OP_MKCLOSURE: /* make-closure */ |
3587 | case OP_MKCLOSURE: /* make-closure */ |
3530 | x = car (args); |
3588 | x = car (args); |
3531 | |
3589 | |
3532 | if (car (x) == SCHEME_V->LAMBDA) |
3590 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
3942 | } |
4000 | } |
3943 | |
4001 | |
3944 | if (USE_ERROR_CHECKING) abort (); |
4002 | if (USE_ERROR_CHECKING) abort (); |
3945 | } |
4003 | } |
3946 | |
4004 | |
3947 | static int |
4005 | /* math, cxr */ |
|
|
4006 | ecb_hot static int |
3948 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4007 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
3949 | { |
4008 | { |
3950 | pointer args = SCHEME_V->args; |
4009 | pointer args = SCHEME_V->args; |
3951 | pointer x = car (args); |
4010 | pointer x = car (args); |
3952 | num v; |
4011 | num v; |
3953 | |
4012 | |
3954 | switch (op) |
4013 | switch (op) |
3955 | { |
4014 | { |
3956 | #if USE_MATH |
4015 | #if USE_MATH |
3957 | case OP_INEX2EX: /* inexact->exact */ |
4016 | case OP_INEX2EX: /* inexact->exact */ |
3958 | { |
|
|
3959 | if (is_integer (x)) |
4017 | if (!is_integer (x)) |
3960 | s_return (x); |
4018 | { |
3961 | |
|
|
3962 | RVALUE r = rvalue_unchecked (x); |
4019 | RVALUE r = rvalue_unchecked (x); |
3963 | |
4020 | |
3964 | if (r == (RVALUE)(IVALUE)r) |
4021 | if (r == (RVALUE)(IVALUE)r) |
3965 | s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); |
4022 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
3966 | else |
4023 | else |
3967 | Error_1 ("inexact->exact: not integral:", x); |
4024 | Error_1 ("inexact->exact: not integral:", x); |
3968 | } |
4025 | } |
3969 | |
4026 | |
|
|
4027 | s_return (x); |
|
|
4028 | |
|
|
4029 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4030 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4031 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4032 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
|
|
4033 | |
|
|
4034 | case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
3970 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4035 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
3971 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
4036 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
|
|
4037 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
3972 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4038 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
3973 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4039 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
3974 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4040 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
3975 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4041 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
3976 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4042 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
3977 | |
4043 | |
3978 | case OP_ATAN: |
4044 | case OP_ATAN: |
|
|
4045 | s_return (mk_real (SCHEME_A_ |
3979 | if (cdr (args) == NIL) |
4046 | cdr (args) == NIL |
3980 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4047 | ? atan (rvalue (x)) |
3981 | else |
4048 | : atan2 (rvalue (x), rvalue (cadr (args))))); |
3982 | { |
|
|
3983 | pointer y = cadr (args); |
|
|
3984 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
|
|
3985 | } |
|
|
3986 | |
|
|
3987 | case OP_SQRT: |
|
|
3988 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
|
|
3989 | |
4049 | |
3990 | case OP_EXPT: |
4050 | case OP_EXPT: |
3991 | { |
4051 | { |
3992 | RVALUE result; |
4052 | RVALUE result; |
3993 | int real_result = 1; |
4053 | int real_result = 1; |
… | |
… | |
4016 | if (real_result) |
4076 | if (real_result) |
4017 | s_return (mk_real (SCHEME_A_ result)); |
4077 | s_return (mk_real (SCHEME_A_ result)); |
4018 | else |
4078 | else |
4019 | s_return (mk_integer (SCHEME_A_ result)); |
4079 | s_return (mk_integer (SCHEME_A_ result)); |
4020 | } |
4080 | } |
4021 | |
|
|
4022 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4023 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4024 | |
|
|
4025 | case OP_TRUNCATE: |
|
|
4026 | { |
|
|
4027 | RVALUE n = rvalue (x); |
|
|
4028 | s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n))); |
|
|
4029 | } |
|
|
4030 | |
|
|
4031 | case OP_ROUND: |
|
|
4032 | if (is_integer (x)) |
|
|
4033 | s_return (x); |
|
|
4034 | |
|
|
4035 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
|
|
4036 | #endif |
4081 | #endif |
4037 | |
4082 | |
4038 | case OP_ADD: /* + */ |
4083 | case OP_ADD: /* + */ |
4039 | v = num_zero; |
4084 | v = num_zero; |
4040 | |
4085 | |
… | |
… | |
4128 | else |
4173 | else |
4129 | Error_0 ("modulo: division by zero"); |
4174 | Error_0 ("modulo: division by zero"); |
4130 | |
4175 | |
4131 | s_return (mk_number (SCHEME_A_ v)); |
4176 | s_return (mk_number (SCHEME_A_ v)); |
4132 | |
4177 | |
4133 | case OP_CAR: /* car */ |
4178 | /* the compiler will optimize this mess... */ |
4134 | s_return (caar (args)); |
4179 | case OP_CAR: op_car: s_return (car (x)); |
4135 | |
4180 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4136 | case OP_CDR: /* cdr */ |
4181 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4137 | s_return (cdar (args)); |
4182 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4183 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4184 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4185 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4186 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4187 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4188 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4189 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4190 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4191 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4192 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4193 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4194 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4195 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4196 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4197 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4198 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4199 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4200 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4201 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4202 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4203 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4204 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4205 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4206 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4207 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4208 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4138 | |
4209 | |
4139 | case OP_CONS: /* cons */ |
4210 | case OP_CONS: /* cons */ |
4140 | set_cdr (args, cadr (args)); |
4211 | set_cdr (args, cadr (args)); |
4141 | s_return (args); |
4212 | s_return (args); |
4142 | |
4213 | |
… | |
… | |
4316 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4387 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4317 | |
4388 | |
4318 | s_return (newstr); |
4389 | s_return (newstr); |
4319 | } |
4390 | } |
4320 | |
4391 | |
4321 | case OP_SUBSTR: /* substring */ |
4392 | case OP_STRING_COPY: /* substring/string-copy */ |
4322 | { |
4393 | { |
4323 | char *str = strvalue (x); |
4394 | char *str = strvalue (x); |
4324 | int index0 = ivalue_unchecked (cadr (args)); |
4395 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4325 | int index1; |
4396 | int index1; |
4326 | int len; |
4397 | int len; |
4327 | |
4398 | |
4328 | if (index0 > strlength (x)) |
4399 | if (index0 > strlength (x)) |
4329 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4400 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4330 | |
4401 | |
4331 | if (cddr (args) != NIL) |
4402 | if (cddr (args) != NIL) |
4332 | { |
4403 | { |
4333 | index1 = ivalue_unchecked (caddr (args)); |
4404 | index1 = ivalue_unchecked (caddr (args)); |
4334 | |
4405 | |
4335 | if (index1 > strlength (x) || index1 < index0) |
4406 | if (index1 > strlength (x) || index1 < index0) |
4336 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4407 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4337 | } |
4408 | } |
4338 | else |
4409 | else |
4339 | index1 = strlength (x); |
4410 | index1 = strlength (x); |
4340 | |
4411 | |
4341 | len = index1 - index0; |
4412 | len = index1 - index0; |
4342 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4413 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4343 | memcpy (strvalue (x), str + index0, len); |
|
|
4344 | strvalue (x)[len] = 0; |
|
|
4345 | |
4414 | |
4346 | s_return (x); |
4415 | s_return (x); |
4347 | } |
4416 | } |
4348 | |
4417 | |
4349 | case OP_VECTOR: /* vector */ |
4418 | case OP_VECTOR: /* vector */ |
… | |
… | |
4391 | } |
4460 | } |
4392 | |
4461 | |
4393 | case OP_VECLEN: /* vector-length */ |
4462 | case OP_VECLEN: /* vector-length */ |
4394 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4463 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4395 | |
4464 | |
|
|
4465 | case OP_VECRESIZE: |
|
|
4466 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4467 | s_return (x); |
|
|
4468 | |
4396 | case OP_VECREF: /* vector-ref */ |
4469 | case OP_VECREF: /* vector-ref */ |
4397 | { |
4470 | { |
4398 | int index = ivalue_unchecked (cadr (args)); |
4471 | int index = ivalue_unchecked (cadr (args)); |
4399 | |
4472 | |
4400 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4473 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
… | |
… | |
4419 | } |
4492 | } |
4420 | |
4493 | |
4421 | if (USE_ERROR_CHECKING) abort (); |
4494 | if (USE_ERROR_CHECKING) abort (); |
4422 | } |
4495 | } |
4423 | |
4496 | |
4424 | static int |
4497 | /* relational ops */ |
|
|
4498 | ecb_hot static int |
4425 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4499 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4426 | { |
4500 | { |
4427 | pointer x = SCHEME_V->args; |
4501 | pointer x = SCHEME_V->args; |
4428 | |
4502 | |
4429 | for (;;) |
4503 | for (;;) |
… | |
… | |
4450 | } |
4524 | } |
4451 | |
4525 | |
4452 | s_return (S_T); |
4526 | s_return (S_T); |
4453 | } |
4527 | } |
4454 | |
4528 | |
4455 | static int |
4529 | /* predicates */ |
|
|
4530 | ecb_hot static int |
4456 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4531 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4457 | { |
4532 | { |
4458 | pointer args = SCHEME_V->args; |
4533 | pointer args = SCHEME_V->args; |
4459 | pointer a = car (args); |
4534 | pointer a = car (args); |
4460 | pointer d = cdr (args); |
4535 | pointer d = cdr (args); |
4461 | int r; |
4536 | int r; |
4462 | |
4537 | |
4463 | switch (op) |
4538 | switch (op) |
4464 | { |
4539 | { |
4465 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4540 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4466 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
4541 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break; |
4467 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4542 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4468 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4543 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4469 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
4544 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4545 | case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break; |
4470 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4546 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4471 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4547 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4472 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4548 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4473 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4549 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4474 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4550 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4475 | |
4551 | |
4476 | #if USE_CHAR_CLASSIFIERS |
4552 | #if USE_CHAR_CLASSIFIERS |
4477 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4553 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4478 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4554 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4479 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4555 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
… | |
… | |
4506 | } |
4582 | } |
4507 | |
4583 | |
4508 | s_retbool (r); |
4584 | s_retbool (r); |
4509 | } |
4585 | } |
4510 | |
4586 | |
4511 | static int |
4587 | /* promises, list ops, ports */ |
|
|
4588 | ecb_hot static int |
4512 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4589 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4513 | { |
4590 | { |
4514 | pointer args = SCHEME_V->args; |
4591 | pointer args = SCHEME_V->args; |
4515 | pointer a = car (args); |
4592 | pointer a = car (args); |
4516 | pointer x, y; |
4593 | pointer x, y; |
… | |
… | |
4529 | } |
4606 | } |
4530 | else |
4607 | else |
4531 | s_return (SCHEME_V->code); |
4608 | s_return (SCHEME_V->code); |
4532 | |
4609 | |
4533 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4610 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4534 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4611 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4535 | s_return (SCHEME_V->value); |
4612 | s_return (SCHEME_V->value); |
4536 | |
4613 | |
4537 | #if USE_PORTS |
4614 | #if USE_PORTS |
|
|
4615 | |
|
|
4616 | case OP_EOF_OBJECT: /* eof-object */ |
|
|
4617 | s_return (S_EOF); |
4538 | |
4618 | |
4539 | case OP_WRITE: /* write */ |
4619 | case OP_WRITE: /* write */ |
4540 | case OP_DISPLAY: /* display */ |
4620 | case OP_DISPLAY: /* display */ |
4541 | case OP_WRITE_CHAR: /* write-char */ |
4621 | case OP_WRITE_CHAR: /* write-char */ |
4542 | if (is_pair (cdr (SCHEME_V->args))) |
4622 | if (is_pair (cdr (SCHEME_V->args))) |
… | |
… | |
4684 | SCHEME_V->gc_verbose = (a != S_F); |
4764 | SCHEME_V->gc_verbose = (a != S_F); |
4685 | s_retbool (was); |
4765 | s_retbool (was); |
4686 | } |
4766 | } |
4687 | |
4767 | |
4688 | case OP_NEWSEGMENT: /* new-segment */ |
4768 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4769 | #if 0 |
4689 | if (!is_pair (args) || !is_number (a)) |
4770 | if (!is_pair (args) || !is_number (a)) |
4690 | Error_0 ("new-segment: argument must be a number"); |
4771 | Error_0 ("new-segment: argument must be a number"); |
4691 | |
4772 | #endif |
4692 | alloc_cellseg (SCHEME_A_ ivalue (a)); |
4773 | s_retbool (alloc_cellseg (SCHEME_A)); |
4693 | |
|
|
4694 | s_return (S_T); |
|
|
4695 | |
4774 | |
4696 | case OP_OBLIST: /* oblist */ |
4775 | case OP_OBLIST: /* oblist */ |
4697 | s_return (oblist_all_symbols (SCHEME_A)); |
4776 | s_return (oblist_all_symbols (SCHEME_A)); |
4698 | |
4777 | |
4699 | #if USE_PORTS |
4778 | #if USE_PORTS |
… | |
… | |
4769 | s_return (p == NIL ? S_F : p); |
4848 | s_return (p == NIL ? S_F : p); |
4770 | } |
4849 | } |
4771 | |
4850 | |
4772 | case OP_GET_OUTSTRING: /* get-output-string */ |
4851 | case OP_GET_OUTSTRING: /* get-output-string */ |
4773 | { |
4852 | { |
4774 | port *p; |
4853 | port *p = port (a); |
4775 | |
4854 | |
4776 | if ((p = a->object.port)->kind & port_string) |
4855 | if (p->kind & port_string) |
4777 | { |
4856 | { |
4778 | off_t size; |
4857 | off_t size; |
4779 | char *str; |
4858 | char *str; |
4780 | |
4859 | |
4781 | size = p->rep.string.curr - p->rep.string.start + 1; |
4860 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4816 | } |
4895 | } |
4817 | |
4896 | |
4818 | if (USE_ERROR_CHECKING) abort (); |
4897 | if (USE_ERROR_CHECKING) abort (); |
4819 | } |
4898 | } |
4820 | |
4899 | |
4821 | static int |
4900 | /* reading */ |
|
|
4901 | ecb_cold static int |
4822 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4902 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4823 | { |
4903 | { |
4824 | pointer args = SCHEME_V->args; |
4904 | pointer args = SCHEME_V->args; |
4825 | pointer x; |
4905 | pointer x; |
4826 | |
4906 | |
… | |
… | |
4886 | int res; |
4966 | int res; |
4887 | |
4967 | |
4888 | if (is_pair (args)) |
4968 | if (is_pair (args)) |
4889 | p = car (args); |
4969 | p = car (args); |
4890 | |
4970 | |
4891 | res = p->object.port->kind & port_string; |
4971 | res = port (p)->kind & port_string; |
4892 | |
4972 | |
4893 | s_retbool (res); |
4973 | s_retbool (res); |
4894 | } |
4974 | } |
4895 | |
4975 | |
4896 | case OP_SET_INPORT: /* set-input-port */ |
4976 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
4955 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5035 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
4956 | SCHEME_V->tok = token (SCHEME_A); |
5036 | SCHEME_V->tok = token (SCHEME_A); |
4957 | s_goto (OP_RDSEXPR); |
5037 | s_goto (OP_RDSEXPR); |
4958 | |
5038 | |
4959 | case TOK_ATOM: |
5039 | case TOK_ATOM: |
4960 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); |
5040 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
|
|
5041 | |
|
|
5042 | case TOK_DOTATOM: |
|
|
5043 | SCHEME_V->strbuff[0] = '.'; |
|
|
5044 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
|
|
5045 | |
|
|
5046 | case TOK_STRATOM: |
|
|
5047 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5048 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5049 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
4961 | |
5050 | |
4962 | case TOK_DQUOTE: |
5051 | case TOK_DQUOTE: |
4963 | x = readstrexp (SCHEME_A); |
5052 | x = readstrexp (SCHEME_A_ '"'); |
4964 | |
5053 | |
4965 | if (x == S_F) |
5054 | if (x == S_F) |
4966 | Error_0 ("Error reading string"); |
5055 | Error_0 ("Error reading string"); |
4967 | |
5056 | |
4968 | setimmutable (x); |
5057 | setimmutable (x); |
… | |
… | |
4980 | s_goto (OP_EVAL); |
5069 | s_goto (OP_EVAL); |
4981 | } |
5070 | } |
4982 | } |
5071 | } |
4983 | |
5072 | |
4984 | case TOK_SHARP_CONST: |
5073 | case TOK_SHARP_CONST: |
4985 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) |
5074 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
4986 | Error_0 ("undefined sharp expression"); |
5075 | Error_0 ("undefined sharp expression"); |
4987 | else |
5076 | else |
4988 | s_return (x); |
5077 | s_return (x); |
4989 | |
5078 | |
4990 | default: |
5079 | default: |
… | |
… | |
5159 | } |
5248 | } |
5160 | |
5249 | |
5161 | if (USE_ERROR_CHECKING) abort (); |
5250 | if (USE_ERROR_CHECKING) abort (); |
5162 | } |
5251 | } |
5163 | |
5252 | |
5164 | static int |
5253 | /* list ops */ |
|
|
5254 | ecb_hot static int |
5165 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5255 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5166 | { |
5256 | { |
5167 | pointer args = SCHEME_V->args; |
5257 | pointer args = SCHEME_V->args; |
5168 | pointer a = car (args); |
5258 | pointer a = car (args); |
5169 | pointer x, y; |
5259 | pointer x, y; |
… | |
… | |
5212 | |
5302 | |
5213 | case OP_CLOSUREP: /* closure? */ |
5303 | case OP_CLOSUREP: /* closure? */ |
5214 | /* |
5304 | /* |
5215 | * Note, macro object is also a closure. |
5305 | * Note, macro object is also a closure. |
5216 | * Therefore, (closure? <#MACRO>) ==> #t |
5306 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5307 | * (schmorp) well, obviously not, fix? TODO |
5217 | */ |
5308 | */ |
5218 | s_retbool (is_closure (a)); |
5309 | s_retbool (is_closure (a)); |
5219 | |
5310 | |
5220 | case OP_MACROP: /* macro? */ |
5311 | case OP_MACROP: /* macro? */ |
5221 | s_retbool (is_macro (a)); |
5312 | s_retbool (is_macro (a)); |
… | |
… | |
5226 | |
5317 | |
5227 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5318 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5228 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5319 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5229 | |
5320 | |
5230 | typedef int (*test_predicate)(pointer); |
5321 | typedef int (*test_predicate)(pointer); |
5231 | static int |
5322 | |
|
|
5323 | ecb_hot static int |
5232 | tst_any (pointer p) |
5324 | tst_any (pointer p) |
5233 | { |
5325 | { |
5234 | return 1; |
5326 | return 1; |
5235 | } |
5327 | } |
5236 | |
5328 | |
5237 | static int |
5329 | ecb_hot static int |
5238 | tst_inonneg (pointer p) |
5330 | tst_inonneg (pointer p) |
5239 | { |
5331 | { |
5240 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5332 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5241 | } |
5333 | } |
5242 | |
5334 | |
5243 | static int |
5335 | ecb_hot static int |
5244 | tst_is_list (SCHEME_P_ pointer p) |
5336 | tst_is_list (SCHEME_P_ pointer p) |
5245 | { |
5337 | { |
5246 | return p == NIL || is_pair (p); |
5338 | return p == NIL || is_pair (p); |
5247 | } |
5339 | } |
5248 | |
5340 | |
… | |
… | |
5291 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5383 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5292 | #include "opdefines.h" |
5384 | #include "opdefines.h" |
5293 | #undef OP_DEF |
5385 | #undef OP_DEF |
5294 | ; |
5386 | ; |
5295 | |
5387 | |
5296 | static const char * |
5388 | ecb_cold static const char * |
5297 | opname (int idx) |
5389 | opname (int idx) |
5298 | { |
5390 | { |
5299 | const char *name = opnames; |
5391 | const char *name = opnames; |
5300 | |
5392 | |
5301 | /* should do this at compile time, but would require external program, right? */ |
5393 | /* should do this at compile time, but would require external program, right? */ |
… | |
… | |
5303 | name += strlen (name) + 1; |
5395 | name += strlen (name) + 1; |
5304 | |
5396 | |
5305 | return *name ? name : "ILLEGAL"; |
5397 | return *name ? name : "ILLEGAL"; |
5306 | } |
5398 | } |
5307 | |
5399 | |
5308 | static const char * |
5400 | ecb_cold static const char * |
5309 | procname (pointer x) |
5401 | procname (pointer x) |
5310 | { |
5402 | { |
5311 | return opname (procnum (x)); |
5403 | return opname (procnum (x)); |
5312 | } |
5404 | } |
5313 | |
5405 | |
… | |
… | |
5333 | #undef OP_DEF |
5425 | #undef OP_DEF |
5334 | {0} |
5426 | {0} |
5335 | }; |
5427 | }; |
5336 | |
5428 | |
5337 | /* kernel of this interpreter */ |
5429 | /* kernel of this interpreter */ |
5338 | static void ecb_hot |
5430 | ecb_hot static void |
5339 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5431 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5340 | { |
5432 | { |
5341 | SCHEME_V->op = op; |
5433 | SCHEME_V->op = op; |
5342 | |
5434 | |
5343 | for (;;) |
5435 | for (;;) |
… | |
… | |
5426 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5518 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5427 | return; |
5519 | return; |
5428 | |
5520 | |
5429 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5521 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5430 | { |
5522 | { |
5431 | xwrstr ("No memory!\n"); |
5523 | putstr (SCHEME_A_ "No memory!\n"); |
5432 | return; |
5524 | return; |
5433 | } |
5525 | } |
5434 | } |
5526 | } |
5435 | } |
5527 | } |
5436 | |
5528 | |
5437 | /* ========== Initialization of internal keywords ========== */ |
5529 | /* ========== Initialization of internal keywords ========== */ |
5438 | |
5530 | |
5439 | static void |
5531 | ecb_cold static void |
5440 | assign_syntax (SCHEME_P_ const char *name) |
5532 | assign_syntax (SCHEME_P_ const char *name) |
5441 | { |
5533 | { |
5442 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5534 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5443 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5535 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5444 | } |
5536 | } |
5445 | |
5537 | |
5446 | static void |
5538 | ecb_cold static void |
5447 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5539 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5448 | { |
5540 | { |
5449 | pointer x = mk_symbol (SCHEME_A_ name); |
5541 | pointer x = mk_symbol (SCHEME_A_ name); |
5450 | pointer y = mk_proc (SCHEME_A_ op); |
5542 | pointer y = mk_proc (SCHEME_A_ op); |
5451 | new_slot_in_env (SCHEME_A_ x, y); |
5543 | new_slot_in_env (SCHEME_A_ x, y); |
… | |
… | |
5459 | ivalue_unchecked (y) = op; |
5551 | ivalue_unchecked (y) = op; |
5460 | return y; |
5552 | return y; |
5461 | } |
5553 | } |
5462 | |
5554 | |
5463 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5555 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5464 | static int |
5556 | ecb_hot static int |
5465 | syntaxnum (pointer p) |
5557 | syntaxnum (pointer p) |
5466 | { |
5558 | { |
5467 | const char *s = strvalue (car (p)); |
5559 | const char *s = strvalue (p); |
5468 | |
5560 | |
5469 | switch (strlength (car (p))) |
5561 | switch (strlength (p)) |
5470 | { |
5562 | { |
5471 | case 2: |
5563 | case 2: |
5472 | if (s[0] == 'i') |
5564 | if (s[0] == 'i') |
5473 | return OP_IF0; /* if */ |
5565 | return OP_IF0; /* if */ |
5474 | else |
5566 | else |
… | |
… | |
5549 | ecb_cold int |
5641 | ecb_cold int |
5550 | scheme_init (SCHEME_P) |
5642 | scheme_init (SCHEME_P) |
5551 | { |
5643 | { |
5552 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5644 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5553 | pointer x; |
5645 | pointer x; |
|
|
5646 | |
|
|
5647 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
5648 | * that memset 0 will also set pointers to 0, but memset does |
|
|
5649 | * of course not guarantee that. screw such systems. |
|
|
5650 | */ |
|
|
5651 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5554 | |
5652 | |
5555 | num_set_fixnum (num_zero, 1); |
5653 | num_set_fixnum (num_zero, 1); |
5556 | num_set_ivalue (num_zero, 0); |
5654 | num_set_ivalue (num_zero, 0); |
5557 | num_set_fixnum (num_one, 1); |
5655 | num_set_fixnum (num_one, 1); |
5558 | num_set_ivalue (num_one, 1); |
5656 | num_set_ivalue (num_one, 1); |
… | |
… | |
5570 | SCHEME_V->save_inport = NIL; |
5668 | SCHEME_V->save_inport = NIL; |
5571 | SCHEME_V->loadport = NIL; |
5669 | SCHEME_V->loadport = NIL; |
5572 | SCHEME_V->nesting = 0; |
5670 | SCHEME_V->nesting = 0; |
5573 | SCHEME_V->interactive_repl = 0; |
5671 | SCHEME_V->interactive_repl = 0; |
5574 | |
5672 | |
5575 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5673 | if (!alloc_cellseg (SCHEME_A)) |
5576 | { |
5674 | { |
5577 | #if USE_ERROR_CHECKING |
5675 | #if USE_ERROR_CHECKING |
5578 | SCHEME_V->no_memory = 1; |
5676 | SCHEME_V->no_memory = 1; |
5579 | return 0; |
5677 | return 0; |
5580 | #endif |
5678 | #endif |
… | |
… | |
5648 | |
5746 | |
5649 | return !SCHEME_V->no_memory; |
5747 | return !SCHEME_V->no_memory; |
5650 | } |
5748 | } |
5651 | |
5749 | |
5652 | #if USE_PORTS |
5750 | #if USE_PORTS |
5653 | void |
5751 | ecb_cold void |
5654 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5752 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5655 | { |
5753 | { |
5656 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5754 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5657 | } |
5755 | } |
5658 | |
5756 | |
5659 | void |
5757 | ecb_cold void |
5660 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5758 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5661 | { |
5759 | { |
5662 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5760 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5663 | } |
5761 | } |
5664 | |
5762 | |
5665 | void |
5763 | ecb_cold void |
5666 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5764 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5667 | { |
5765 | { |
5668 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5766 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5669 | } |
5767 | } |
5670 | |
5768 | |
5671 | void |
5769 | ecb_cold void |
5672 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5770 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5673 | { |
5771 | { |
5674 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5772 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5675 | } |
5773 | } |
5676 | #endif |
5774 | #endif |
5677 | |
5775 | |
5678 | void |
5776 | ecb_cold void |
5679 | scheme_set_external_data (SCHEME_P_ void *p) |
5777 | scheme_set_external_data (SCHEME_P_ void *p) |
5680 | { |
5778 | { |
5681 | SCHEME_V->ext_data = p; |
5779 | SCHEME_V->ext_data = p; |
5682 | } |
5780 | } |
5683 | |
5781 | |
… | |
… | |
5731 | } |
5829 | } |
5732 | } |
5830 | } |
5733 | #endif |
5831 | #endif |
5734 | } |
5832 | } |
5735 | |
5833 | |
5736 | void |
5834 | ecb_cold void |
5737 | scheme_load_file (SCHEME_P_ int fin) |
5835 | scheme_load_file (SCHEME_P_ int fin) |
5738 | { |
5836 | { |
5739 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5837 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5740 | } |
5838 | } |
5741 | |
5839 | |
5742 | void |
5840 | ecb_cold void |
5743 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5841 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5744 | { |
5842 | { |
5745 | dump_stack_reset (SCHEME_A); |
5843 | dump_stack_reset (SCHEME_A); |
5746 | SCHEME_V->envir = SCHEME_V->global_env; |
5844 | SCHEME_V->envir = SCHEME_V->global_env; |
5747 | SCHEME_V->file_i = 0; |
5845 | SCHEME_V->file_i = 0; |
… | |
… | |
5774 | |
5872 | |
5775 | if (SCHEME_V->retcode == 0) |
5873 | if (SCHEME_V->retcode == 0) |
5776 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5874 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5777 | } |
5875 | } |
5778 | |
5876 | |
5779 | void |
5877 | ecb_cold void |
5780 | scheme_load_string (SCHEME_P_ const char *cmd) |
5878 | scheme_load_string (SCHEME_P_ const char *cmd) |
5781 | { |
5879 | { |
5782 | dump_stack_reset (SCHEME_A); |
5880 | dump_stack_reset (SCHEME_A); |
5783 | SCHEME_V->envir = SCHEME_V->global_env; |
5881 | SCHEME_V->envir = SCHEME_V->global_env; |
5784 | SCHEME_V->file_i = 0; |
5882 | SCHEME_V->file_i = 0; |
… | |
… | |
5798 | |
5896 | |
5799 | if (SCHEME_V->retcode == 0) |
5897 | if (SCHEME_V->retcode == 0) |
5800 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5898 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5801 | } |
5899 | } |
5802 | |
5900 | |
5803 | void |
5901 | ecb_cold void |
5804 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5902 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5805 | { |
5903 | { |
5806 | pointer x; |
5904 | pointer x; |
5807 | |
5905 | |
5808 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
5906 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
… | |
… | |
5813 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5911 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5814 | } |
5912 | } |
5815 | |
5913 | |
5816 | #if !STANDALONE |
5914 | #if !STANDALONE |
5817 | |
5915 | |
5818 | void |
5916 | ecb_cold void |
5819 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5917 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5820 | { |
5918 | { |
5821 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5919 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5822 | } |
5920 | } |
5823 | |
5921 | |
5824 | void |
5922 | ecb_cold void |
5825 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5923 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5826 | { |
5924 | { |
5827 | int i; |
5925 | int i; |
5828 | |
5926 | |
5829 | for (i = 0; i < count; i++) |
5927 | for (i = 0; i < count; i++) |
5830 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5928 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5831 | } |
5929 | } |
5832 | |
5930 | |
5833 | pointer |
5931 | ecb_cold pointer |
5834 | scheme_apply0 (SCHEME_P_ const char *procname) |
5932 | scheme_apply0 (SCHEME_P_ const char *procname) |
5835 | { |
5933 | { |
5836 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
5934 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
5837 | } |
5935 | } |
5838 | |
5936 | |
5839 | void |
5937 | ecb_cold void |
5840 | save_from_C_call (SCHEME_P) |
5938 | save_from_C_call (SCHEME_P) |
5841 | { |
5939 | { |
5842 | pointer saved_data = cons (car (S_SINK), |
5940 | pointer saved_data = cons (car (S_SINK), |
5843 | cons (SCHEME_V->envir, |
5941 | cons (SCHEME_V->envir, |
5844 | SCHEME_V->dump)); |
5942 | SCHEME_V->dump)); |
… | |
… | |
5848 | /* Truncate the dump stack so TS will return here when done, not |
5946 | /* Truncate the dump stack so TS will return here when done, not |
5849 | directly resume pre-C-call operations. */ |
5947 | directly resume pre-C-call operations. */ |
5850 | dump_stack_reset (SCHEME_A); |
5948 | dump_stack_reset (SCHEME_A); |
5851 | } |
5949 | } |
5852 | |
5950 | |
5853 | void |
5951 | ecb_cold void |
5854 | restore_from_C_call (SCHEME_P) |
5952 | restore_from_C_call (SCHEME_P) |
5855 | { |
5953 | { |
5856 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5954 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5857 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5955 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5858 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5956 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5859 | /* Pop */ |
5957 | /* Pop */ |
5860 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5958 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5861 | } |
5959 | } |
5862 | |
5960 | |
5863 | /* "func" and "args" are assumed to be already eval'ed. */ |
5961 | /* "func" and "args" are assumed to be already eval'ed. */ |
5864 | pointer |
5962 | ecb_cold pointer |
5865 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5963 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5866 | { |
5964 | { |
5867 | int old_repl = SCHEME_V->interactive_repl; |
5965 | int old_repl = SCHEME_V->interactive_repl; |
5868 | |
5966 | |
5869 | SCHEME_V->interactive_repl = 0; |
5967 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5876 | SCHEME_V->interactive_repl = old_repl; |
5974 | SCHEME_V->interactive_repl = old_repl; |
5877 | restore_from_C_call (SCHEME_A); |
5975 | restore_from_C_call (SCHEME_A); |
5878 | return SCHEME_V->value; |
5976 | return SCHEME_V->value; |
5879 | } |
5977 | } |
5880 | |
5978 | |
5881 | pointer |
5979 | ecb_cold pointer |
5882 | scheme_eval (SCHEME_P_ pointer obj) |
5980 | scheme_eval (SCHEME_P_ pointer obj) |
5883 | { |
5981 | { |
5884 | int old_repl = SCHEME_V->interactive_repl; |
5982 | int old_repl = SCHEME_V->interactive_repl; |
5885 | |
5983 | |
5886 | SCHEME_V->interactive_repl = 0; |
5984 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5898 | |
5996 | |
5899 | /* ========== Main ========== */ |
5997 | /* ========== Main ========== */ |
5900 | |
5998 | |
5901 | #if STANDALONE |
5999 | #if STANDALONE |
5902 | |
6000 | |
5903 | int |
6001 | ecb_cold int |
5904 | main (int argc, char **argv) |
6002 | main (int argc, char **argv) |
5905 | { |
6003 | { |
5906 | # if USE_MULTIPLICITY |
6004 | # if USE_MULTIPLICITY |
5907 | scheme ssc; |
6005 | scheme ssc; |
5908 | scheme *const SCHEME_V = &ssc; |
6006 | scheme *const SCHEME_V = &ssc; |
… | |
… | |
5910 | # endif |
6008 | # endif |
5911 | int fin; |
6009 | int fin; |
5912 | char *file_name = InitFile; |
6010 | char *file_name = InitFile; |
5913 | int retcode; |
6011 | int retcode; |
5914 | int isfile = 1; |
6012 | int isfile = 1; |
|
|
6013 | system ("ps v $PPID");//D |
5915 | |
6014 | |
5916 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6015 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5917 | { |
6016 | { |
5918 | xwrstr ("Usage: tinyscheme -?\n"); |
6017 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
5919 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6018 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
5920 | xwrstr ("followed by\n"); |
6019 | putstr (SCHEME_A_ "followed by\n"); |
5921 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6020 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
5922 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6021 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
5923 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6022 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
5924 | xwrstr ("Use - as filename for stdin.\n"); |
6023 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
5925 | return 1; |
6024 | return 1; |
5926 | } |
6025 | } |
5927 | |
6026 | |
5928 | if (!scheme_init (SCHEME_A)) |
6027 | if (!scheme_init (SCHEME_A)) |
5929 | { |
6028 | { |
5930 | xwrstr ("Could not initialize!\n"); |
6029 | putstr (SCHEME_A_ "Could not initialize!\n"); |
5931 | return 2; |
6030 | return 2; |
5932 | } |
6031 | } |
5933 | |
6032 | |
5934 | # if USE_PORTS |
6033 | # if USE_PORTS |
5935 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6034 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
5980 | fin = open (file_name, O_RDONLY); |
6079 | fin = open (file_name, O_RDONLY); |
5981 | #endif |
6080 | #endif |
5982 | |
6081 | |
5983 | if (isfile && fin < 0) |
6082 | if (isfile && fin < 0) |
5984 | { |
6083 | { |
5985 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6084 | putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
5986 | } |
6085 | } |
5987 | else |
6086 | else |
5988 | { |
6087 | { |
5989 | if (isfile) |
6088 | if (isfile) |
5990 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6089 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
… | |
… | |
5994 | #if USE_PORTS |
6093 | #if USE_PORTS |
5995 | if (!isfile || fin != STDIN_FILENO) |
6094 | if (!isfile || fin != STDIN_FILENO) |
5996 | { |
6095 | { |
5997 | if (SCHEME_V->retcode != 0) |
6096 | if (SCHEME_V->retcode != 0) |
5998 | { |
6097 | { |
5999 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6098 | putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
6000 | } |
6099 | } |
6001 | |
6100 | |
6002 | if (isfile) |
6101 | if (isfile) |
6003 | close (fin); |
6102 | close (fin); |
6004 | } |
6103 | } |