… | |
… | |
16 | * (MINISCM) This is a revised and modified version by Akira KIDA. |
16 | * (MINISCM) This is a revised and modified version by Akira KIDA. |
17 | * (MINISCM) current version is 0.85k4 (15 May 1994) |
17 | * (MINISCM) current version is 0.85k4 (15 May 1994) |
18 | * |
18 | * |
19 | */ |
19 | */ |
20 | |
20 | |
21 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
21 | #define _GNU_SOURCE 1 |
22 | #include "malloc.c" |
22 | #define _POSIX_C_SOURCE 200201 |
|
|
23 | #define _XOPEN_SOURCE 600 |
|
|
24 | |
23 | |
25 | |
24 | #define SCHEME_SOURCE |
26 | #define SCHEME_SOURCE |
25 | #include "scheme-private.h" |
27 | #include "scheme-private.h" |
26 | #ifndef WIN32 |
28 | #ifndef WIN32 |
27 | # include <unistd.h> |
29 | # include <unistd.h> |
… | |
… | |
34 | |
36 | |
35 | #include <sys/types.h> |
37 | #include <sys/types.h> |
36 | #include <sys/stat.h> |
38 | #include <sys/stat.h> |
37 | #include <fcntl.h> |
39 | #include <fcntl.h> |
38 | |
40 | |
|
|
41 | #if !USE_ERROR_CHECKING |
|
|
42 | # define NDEBUG |
|
|
43 | #endif |
|
|
44 | |
|
|
45 | #include <assert.h> |
|
|
46 | #include <stdlib.h> |
39 | #include <string.h> |
47 | #include <string.h> |
40 | #include <stdlib.h> |
|
|
41 | |
48 | |
42 | #include <limits.h> |
49 | #include <limits.h> |
43 | #include <inttypes.h> |
50 | #include <inttypes.h> |
44 | #include <float.h> |
51 | #include <float.h> |
45 | //#include <ctype.h> |
52 | |
|
|
53 | #if !USE_SYSTEM_MALLOC |
|
|
54 | # define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
|
|
55 | # include "malloc.c" |
|
|
56 | # define malloc(n) tiny_malloc (n) |
|
|
57 | # define realloc(p,n) tiny_realloc (p, n) |
|
|
58 | # define free(p) tiny_free (p) |
|
|
59 | #endif |
|
|
60 | |
|
|
61 | #if '1' != '0' + 1 \ |
|
|
62 | || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ |
|
|
63 | || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ |
|
|
64 | || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ |
|
|
65 | || 'f' != 'a' + 5 |
|
|
66 | # error "execution character set digits not consecutive" |
|
|
67 | #endif |
46 | |
68 | |
47 | enum { |
69 | enum { |
48 | TOK_EOF, |
70 | TOK_EOF, |
49 | TOK_LPAREN, |
71 | TOK_LPAREN, |
50 | TOK_RPAREN, |
72 | TOK_RPAREN, |
51 | TOK_DOT, |
73 | TOK_DOT, |
52 | TOK_ATOM, |
74 | TOK_ATOM, |
|
|
75 | TOK_DOTATOM, /* atom name starting with '.' */ |
|
|
76 | TOK_STRATOM, /* atom name enclosed in | */ |
53 | TOK_QUOTE, |
77 | TOK_QUOTE, |
54 | TOK_DQUOTE, |
78 | TOK_DQUOTE, |
55 | TOK_BQUOTE, |
79 | TOK_BQUOTE, |
56 | TOK_COMMA, |
80 | TOK_COMMA, |
57 | TOK_ATMARK, |
81 | TOK_ATMARK, |
… | |
… | |
59 | TOK_SHARP_CONST, |
83 | TOK_SHARP_CONST, |
60 | TOK_VEC |
84 | TOK_VEC |
61 | }; |
85 | }; |
62 | |
86 | |
63 | #define BACKQUOTE '`' |
87 | #define BACKQUOTE '`' |
64 | #define DELIMITERS "()\";\f\t\v\n\r " |
88 | #define WHITESPACE " \t\r\n\v\f" |
|
|
89 | #define DELIMITERS "()\";" WHITESPACE |
65 | |
90 | |
66 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
91 | #define NIL POINTER (&SCHEME_V->xNIL) |
67 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
92 | #define S_T POINTER (&SCHEME_V->xT) |
68 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
93 | #define S_F POINTER (&SCHEME_V->xF) |
69 | #define S_SINK (&SCHEME_V->xsink) |
94 | #define S_SINK POINTER (&SCHEME_V->xsink) |
70 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
95 | #define S_EOF POINTER (&SCHEME_V->xEOF_OBJ) |
71 | |
96 | |
72 | #if !USE_MULTIPLICITY |
97 | #if !USE_MULTIPLICITY |
73 | static scheme sc; |
98 | static scheme sc; |
74 | #endif |
99 | #endif |
75 | |
100 | |
76 | static void |
101 | ecb_cold static void |
77 | xbase (char *s, long n, int base) |
102 | xbase (char *s, long n, int base) |
78 | { |
103 | { |
79 | if (n < 0) |
104 | if (n < 0) |
80 | { |
105 | { |
81 | *s++ = '-'; |
106 | *s++ = '-'; |
… | |
… | |
83 | } |
108 | } |
84 | |
109 | |
85 | char *p = s; |
110 | char *p = s; |
86 | |
111 | |
87 | do { |
112 | do { |
88 | *p++ = '0' + n % base; |
113 | *p++ = "0123456789abcdef"[n % base]; |
89 | n /= base; |
114 | n /= base; |
90 | } while (n); |
115 | } while (n); |
91 | |
116 | |
92 | *p-- = 0; |
117 | *p-- = 0; |
93 | |
118 | |
… | |
… | |
96 | char x = *s; *s = *p; *p = x; |
121 | char x = *s; *s = *p; *p = x; |
97 | --p; ++s; |
122 | --p; ++s; |
98 | } |
123 | } |
99 | } |
124 | } |
100 | |
125 | |
101 | static void |
126 | ecb_cold static void |
102 | xnum (char *s, long n) |
127 | xnum (char *s, long n) |
103 | { |
128 | { |
104 | xbase (s, n, 10); |
129 | xbase (s, n, 10); |
105 | } |
130 | } |
106 | |
131 | |
107 | static void |
132 | ecb_cold static void |
108 | xwrstr (const char *s) |
133 | putnum (SCHEME_P_ long n) |
109 | { |
|
|
110 | write (1, s, strlen (s)); |
|
|
111 | } |
|
|
112 | |
|
|
113 | static void |
|
|
114 | xwrnum (long n) |
|
|
115 | { |
134 | { |
116 | char buf[64]; |
135 | char buf[64]; |
117 | |
136 | |
118 | xnum (buf, n); |
137 | xnum (buf, n); |
119 | xwrstr (buf); |
138 | putstr (SCHEME_A_ buf); |
120 | } |
139 | } |
|
|
140 | |
|
|
141 | #if USE_CHAR_CLASSIFIERS |
|
|
142 | #include <ctype.h> |
|
|
143 | #else |
121 | |
144 | |
122 | static char |
145 | static char |
123 | xtoupper (char c) |
146 | xtoupper (char c) |
124 | { |
147 | { |
125 | if (c >= 'a' && c <= 'z') |
148 | if (c >= 'a' && c <= 'z') |
… | |
… | |
145 | |
168 | |
146 | #define toupper(c) xtoupper (c) |
169 | #define toupper(c) xtoupper (c) |
147 | #define tolower(c) xtolower (c) |
170 | #define tolower(c) xtolower (c) |
148 | #define isdigit(c) xisdigit (c) |
171 | #define isdigit(c) xisdigit (c) |
149 | |
172 | |
|
|
173 | #endif |
|
|
174 | |
150 | #if USE_IGNORECASE |
175 | #if USE_IGNORECASE |
151 | static const char * |
176 | ecb_cold static const char * |
152 | xstrlwr (char *s) |
177 | xstrlwr (char *s) |
153 | { |
178 | { |
154 | const char *p = s; |
179 | const char *p = s; |
155 | |
180 | |
156 | while (*s) |
181 | while (*s) |
… | |
… | |
176 | |
201 | |
177 | #ifndef InitFile |
202 | #ifndef InitFile |
178 | # define InitFile "init.scm" |
203 | # define InitFile "init.scm" |
179 | #endif |
204 | #endif |
180 | |
205 | |
181 | #ifndef FIRST_CELLSEGS |
|
|
182 | # define FIRST_CELLSEGS 3 |
|
|
183 | #endif |
|
|
184 | |
|
|
185 | enum scheme_types |
206 | enum scheme_types |
186 | { |
207 | { |
187 | T_INTEGER, |
208 | T_INTEGER, |
|
|
209 | T_CHARACTER, |
188 | T_REAL, |
210 | T_REAL, |
189 | T_STRING, |
211 | T_STRING, |
190 | T_SYMBOL, |
212 | T_SYMBOL, |
191 | T_PROC, |
213 | T_PROC, |
192 | T_PAIR, /* also used for free cells */ |
214 | T_PAIR, /* also used for free cells */ |
193 | T_CLOSURE, |
215 | T_CLOSURE, |
|
|
216 | T_BYTECODE, // temp |
|
|
217 | T_MACRO, |
194 | T_CONTINUATION, |
218 | T_CONTINUATION, |
195 | T_FOREIGN, |
219 | T_FOREIGN, |
196 | T_CHARACTER, |
|
|
197 | T_PORT, |
220 | T_PORT, |
198 | T_VECTOR, |
221 | T_VECTOR, |
199 | T_MACRO, |
|
|
200 | T_PROMISE, |
222 | T_PROMISE, |
201 | T_ENVIRONMENT, |
223 | T_ENVIRONMENT, |
202 | /* one more... */ |
224 | |
203 | T_NUM_SYSTEM_TYPES |
225 | T_NUM_SYSTEM_TYPES |
204 | }; |
226 | }; |
205 | |
227 | |
206 | #define T_MASKTYPE 0x000f |
228 | #define T_MASKTYPE 0x000f |
207 | #define T_SYNTAX 0x0010 |
229 | #define T_SYNTAX 0x0010 |
… | |
… | |
240 | static num num_op (enum num_op op, num a, num b); |
262 | static num num_op (enum num_op op, num a, num b); |
241 | static num num_intdiv (num a, num b); |
263 | static num num_intdiv (num a, num b); |
242 | static num num_rem (num a, num b); |
264 | static num num_rem (num a, num b); |
243 | static num num_mod (num a, num b); |
265 | static num num_mod (num a, num b); |
244 | |
266 | |
245 | #if USE_MATH |
|
|
246 | static double round_per_R5RS (double x); |
|
|
247 | #endif |
|
|
248 | static int is_zero_rvalue (RVALUE x); |
267 | static int is_zero_rvalue (RVALUE x); |
249 | |
268 | |
250 | static num num_zero; |
269 | static num num_zero; |
251 | static num num_one; |
270 | static num num_one; |
252 | |
271 | |
|
|
272 | /* convert "pointer" to cell* / cell* to pointer */ |
|
|
273 | #define CELL(p) ((struct cell *)(p) + 0) |
|
|
274 | #define POINTER(c) ((void *)((c) - 0)) |
|
|
275 | |
253 | /* macros for cell operations */ |
276 | /* macros for cell operations */ |
254 | #define typeflag(p) ((p)->flag + 0) |
277 | #define typeflag(p) (CELL(p)->flag + 0) |
255 | #define set_typeflag(p,v) ((p)->flag = (v)) |
278 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
256 | #define type(p) (typeflag (p) & T_MASKTYPE) |
279 | #define type(p) (typeflag (p) & T_MASKTYPE) |
257 | |
280 | |
258 | INTERFACE int |
281 | INTERFACE int |
259 | is_string (pointer p) |
282 | is_string (pointer p) |
260 | { |
283 | { |
261 | return type (p) == T_STRING; |
284 | return type (p) == T_STRING; |
262 | } |
285 | } |
263 | |
286 | |
264 | #define strvalue(p) ((p)->object.string.svalue) |
287 | #define strvalue(p) (CELL(p)->object.string.svalue) |
265 | #define strlength(p) ((p)->object.string.length) |
288 | #define strlength(p) (CELL(p)->object.string.length) |
266 | |
289 | |
267 | INTERFACE int |
290 | INTERFACE int |
268 | is_vector (pointer p) |
291 | is_vector (pointer p) |
269 | { |
292 | { |
270 | return type (p) == T_VECTOR; |
293 | return type (p) == T_VECTOR; |
271 | } |
294 | } |
272 | |
295 | |
273 | #define vecvalue(p) ((p)->object.vector.vvalue) |
296 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
274 | #define veclength(p) ((p)->object.vector.length) |
297 | #define veclength(p) (CELL(p)->object.vector.length) |
275 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
298 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
276 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
299 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
277 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
300 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
278 | |
301 | |
279 | INTERFACE int |
302 | INTERFACE int |
… | |
… | |
305 | string_value (pointer p) |
328 | string_value (pointer p) |
306 | { |
329 | { |
307 | return strvalue (p); |
330 | return strvalue (p); |
308 | } |
331 | } |
309 | |
332 | |
310 | #define ivalue_unchecked(p) (p)->object.ivalue |
333 | #define ivalue_unchecked(p) CELL(p)->object.ivalue |
311 | #define set_ivalue(p,v) (p)->object.ivalue = (v) |
334 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
312 | |
335 | |
313 | #if USE_REAL |
336 | #if USE_REAL |
314 | #define rvalue_unchecked(p) (p)->object.rvalue |
337 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
315 | #define set_rvalue(p,v) (p)->object.rvalue = (v) |
338 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
316 | #else |
339 | #else |
317 | #define rvalue_unchecked(p) (p)->object.ivalue |
340 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
318 | #define set_rvalue(p,v) (p)->object.ivalue = (v) |
341 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
319 | #endif |
342 | #endif |
320 | |
343 | |
321 | INTERFACE long |
344 | INTERFACE long |
322 | charvalue (pointer p) |
345 | charvalue (pointer p) |
323 | { |
346 | { |
324 | return ivalue_unchecked (p); |
347 | return ivalue_unchecked (p); |
325 | } |
348 | } |
326 | |
349 | |
|
|
350 | #define port(p) CELL(p)->object.port |
|
|
351 | #define set_port(p,v) port(p) = (v) |
327 | INTERFACE int |
352 | INTERFACE int |
328 | is_port (pointer p) |
353 | is_port (pointer p) |
329 | { |
354 | { |
330 | return type (p) == T_PORT; |
355 | return type (p) == T_PORT; |
331 | } |
356 | } |
332 | |
357 | |
333 | INTERFACE int |
358 | INTERFACE int |
334 | is_inport (pointer p) |
359 | is_inport (pointer p) |
335 | { |
360 | { |
336 | return is_port (p) && p->object.port->kind & port_input; |
361 | return is_port (p) && port (p)->kind & port_input; |
337 | } |
362 | } |
338 | |
363 | |
339 | INTERFACE int |
364 | INTERFACE int |
340 | is_outport (pointer p) |
365 | is_outport (pointer p) |
341 | { |
366 | { |
342 | return is_port (p) && p->object.port->kind & port_output; |
367 | return is_port (p) && port (p)->kind & port_output; |
343 | } |
368 | } |
344 | |
369 | |
345 | INTERFACE int |
370 | INTERFACE int |
346 | is_pair (pointer p) |
371 | is_pair (pointer p) |
347 | { |
372 | { |
348 | return type (p) == T_PAIR; |
373 | return type (p) == T_PAIR; |
349 | } |
374 | } |
350 | |
375 | |
351 | #define car(p) ((p)->object.cons.car + 0) |
376 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
352 | #define cdr(p) ((p)->object.cons.cdr + 0) |
377 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
353 | |
378 | |
354 | static pointer caar (pointer p) { return car (car (p)); } |
379 | static pointer caar (pointer p) { return car (car (p)); } |
355 | static pointer cadr (pointer p) { return car (cdr (p)); } |
380 | static pointer cadr (pointer p) { return car (cdr (p)); } |
356 | static pointer cdar (pointer p) { return cdr (car (p)); } |
381 | static pointer cdar (pointer p) { return cdr (car (p)); } |
357 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
382 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
… | |
… | |
361 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
386 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
362 | |
387 | |
363 | INTERFACE void |
388 | INTERFACE void |
364 | set_car (pointer p, pointer q) |
389 | set_car (pointer p, pointer q) |
365 | { |
390 | { |
366 | p->object.cons.car = q; |
391 | CELL(p)->object.cons.car = CELL (q); |
367 | } |
392 | } |
368 | |
393 | |
369 | INTERFACE void |
394 | INTERFACE void |
370 | set_cdr (pointer p, pointer q) |
395 | set_cdr (pointer p, pointer q) |
371 | { |
396 | { |
372 | p->object.cons.cdr = q; |
397 | CELL(p)->object.cons.cdr = CELL (q); |
373 | } |
398 | } |
374 | |
399 | |
375 | INTERFACE pointer |
400 | INTERFACE pointer |
376 | pair_car (pointer p) |
401 | pair_car (pointer p) |
377 | { |
402 | { |
… | |
… | |
391 | } |
416 | } |
392 | |
417 | |
393 | INTERFACE char * |
418 | INTERFACE char * |
394 | symname (pointer p) |
419 | symname (pointer p) |
395 | { |
420 | { |
396 | return strvalue (car (p)); |
421 | return strvalue (p); |
397 | } |
422 | } |
398 | |
423 | |
399 | #if USE_PLIST |
424 | #if USE_PLIST |
|
|
425 | #error plists are broken because symbols are no longer pairs |
|
|
426 | #define symprop(p) cdr(p) |
400 | SCHEME_EXPORT int |
427 | SCHEME_EXPORT int |
401 | hasprop (pointer p) |
428 | hasprop (pointer p) |
402 | { |
429 | { |
403 | return typeflag (p) & T_SYMBOL; |
430 | return typeflag (p) & T_SYMBOL; |
404 | } |
431 | } |
405 | |
|
|
406 | # define symprop(p) cdr(p) |
|
|
407 | #endif |
432 | #endif |
408 | |
433 | |
409 | INTERFACE int |
434 | INTERFACE int |
410 | is_syntax (pointer p) |
435 | is_syntax (pointer p) |
411 | { |
436 | { |
… | |
… | |
425 | } |
450 | } |
426 | |
451 | |
427 | INTERFACE char * |
452 | INTERFACE char * |
428 | syntaxname (pointer p) |
453 | syntaxname (pointer p) |
429 | { |
454 | { |
430 | return strvalue (car (p)); |
455 | return strvalue (p); |
431 | } |
456 | } |
432 | |
457 | |
433 | #define procnum(p) ivalue_unchecked (p) |
458 | #define procnum(p) ivalue_unchecked (p) |
434 | static const char *procname (pointer x); |
459 | static const char *procname (pointer x); |
435 | |
460 | |
… | |
… | |
507 | proper list: length |
532 | proper list: length |
508 | circular list: -1 |
533 | circular list: -1 |
509 | not even a pair: -2 |
534 | not even a pair: -2 |
510 | dotted list: -2 minus length before dot |
535 | dotted list: -2 minus length before dot |
511 | */ |
536 | */ |
512 | INTERFACE int |
537 | ecb_hot INTERFACE int |
513 | list_length (SCHEME_P_ pointer a) |
538 | list_length (SCHEME_P_ pointer a) |
514 | { |
539 | { |
515 | int i = 0; |
540 | int i = 0; |
516 | pointer slow, fast; |
541 | pointer slow, fast; |
517 | |
542 | |
… | |
… | |
556 | { |
581 | { |
557 | return list_length (SCHEME_A_ a) >= 0; |
582 | return list_length (SCHEME_A_ a) >= 0; |
558 | } |
583 | } |
559 | |
584 | |
560 | #if USE_CHAR_CLASSIFIERS |
585 | #if USE_CHAR_CLASSIFIERS |
|
|
586 | |
561 | ecb_inline int |
587 | ecb_inline int |
562 | Cisalpha (int c) |
588 | Cisalpha (int c) |
563 | { |
589 | { |
564 | return isascii (c) && isalpha (c); |
590 | return isascii (c) && isalpha (c); |
565 | } |
591 | } |
… | |
… | |
623 | "gs", |
649 | "gs", |
624 | "rs", |
650 | "rs", |
625 | "us" |
651 | "us" |
626 | }; |
652 | }; |
627 | |
653 | |
628 | static int |
654 | ecb_cold static int |
629 | is_ascii_name (const char *name, int *pc) |
655 | is_ascii_name (const char *name, int *pc) |
630 | { |
656 | { |
631 | int i; |
657 | int i; |
632 | |
658 | |
633 | for (i = 0; i < 32; i++) |
659 | for (i = 0; i < 32; i++) |
… | |
… | |
651 | #endif |
677 | #endif |
652 | |
678 | |
653 | static int file_push (SCHEME_P_ const char *fname); |
679 | static int file_push (SCHEME_P_ const char *fname); |
654 | static void file_pop (SCHEME_P); |
680 | static void file_pop (SCHEME_P); |
655 | static int file_interactive (SCHEME_P); |
681 | static int file_interactive (SCHEME_P); |
656 | ecb_inline int is_one_of (char *s, int c); |
682 | ecb_inline int is_one_of (const char *s, int c); |
657 | static int alloc_cellseg (SCHEME_P_ int n); |
683 | static int alloc_cellseg (SCHEME_P); |
658 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
684 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
659 | static void finalize_cell (SCHEME_P_ pointer a); |
685 | static void finalize_cell (SCHEME_P_ pointer a); |
660 | static int count_consecutive_cells (pointer x, int needed); |
|
|
661 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
686 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
662 | static pointer mk_number (SCHEME_P_ const num n); |
687 | static pointer mk_number (SCHEME_P_ const num n); |
663 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
688 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
664 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
689 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
665 | static pointer mk_atom (SCHEME_P_ char *q); |
690 | static pointer mk_atom (SCHEME_P_ char *q); |
666 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
691 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
667 | |
692 | |
|
|
693 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
694 | |
668 | #if USE_PORTS |
695 | #if USE_PORTS |
669 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
670 | static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); |
696 | static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); |
671 | static pointer port_from_file (SCHEME_P_ int, int prop); |
697 | static pointer port_from_file (SCHEME_P_ int, int prop); |
672 | static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
698 | static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
673 | static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); |
699 | static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); |
674 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
700 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
675 | static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
701 | static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
676 | static void port_close (SCHEME_P_ pointer p, int flag); |
702 | static void port_close (SCHEME_P_ pointer p, int flag); |
677 | #endif |
703 | #endif |
|
|
704 | |
678 | static void mark (pointer a); |
705 | static void mark (pointer a); |
679 | static void gc (SCHEME_P_ pointer a, pointer b); |
706 | static void gc (SCHEME_P_ pointer a, pointer b); |
680 | static int basic_inchar (port *pt); |
707 | static int basic_inchar (port *pt); |
681 | static int inchar (SCHEME_P); |
708 | static int inchar (SCHEME_P); |
682 | static void backchar (SCHEME_P_ int c); |
709 | static void backchar (SCHEME_P_ int c); |
683 | static char *readstr_upto (SCHEME_P_ char *delim); |
710 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
684 | static pointer readstrexp (SCHEME_P); |
711 | static pointer readstrexp (SCHEME_P_ char delim); |
685 | ecb_inline int skipspace (SCHEME_P); |
712 | ecb_inline int skipspace (SCHEME_P); |
686 | static int token (SCHEME_P); |
713 | static int token (SCHEME_P); |
687 | static void printslashstring (SCHEME_P_ char *s, int len); |
714 | static void printslashstring (SCHEME_P_ char *s, int len); |
688 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
715 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
689 | static void printatom (SCHEME_P_ pointer l, int f); |
716 | static void printatom (SCHEME_P_ pointer l, int f); |
… | |
… | |
856 | } |
883 | } |
857 | |
884 | |
858 | return ret; |
885 | return ret; |
859 | } |
886 | } |
860 | |
887 | |
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 |
888 | static int |
887 | is_zero_rvalue (RVALUE x) |
889 | is_zero_rvalue (RVALUE x) |
888 | { |
890 | { |
889 | return x == 0; |
891 | return x == 0; |
890 | #if 0 |
892 | #if 0 |
… | |
… | |
895 | #endif |
897 | #endif |
896 | #endif |
898 | #endif |
897 | } |
899 | } |
898 | |
900 | |
899 | /* allocate new cell segment */ |
901 | /* allocate new cell segment */ |
900 | static int |
902 | ecb_cold static int |
901 | alloc_cellseg (SCHEME_P_ int n) |
903 | alloc_cellseg (SCHEME_P) |
902 | { |
904 | { |
903 | pointer newp; |
905 | struct cell *newp; |
904 | pointer last; |
906 | struct cell *last; |
905 | pointer p; |
907 | struct cell *p; |
906 | char *cp; |
908 | char *cp; |
907 | long i; |
909 | long i; |
908 | int k; |
910 | int k; |
909 | |
911 | |
910 | static int segsize = CELL_SEGSIZE >> 1; |
912 | static int segsize = CELL_SEGSIZE >> 1; |
911 | segsize <<= 1; |
913 | segsize <<= 1; |
912 | |
914 | |
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)); |
915 | cp = malloc (segsize * sizeof (struct cell)); |
919 | |
916 | |
920 | if (!cp && USE_ERROR_CHECKING) |
917 | if (!cp && USE_ERROR_CHECKING) |
921 | return k; |
918 | return k; |
922 | |
919 | |
923 | i = ++SCHEME_V->last_cell_seg; |
920 | i = ++SCHEME_V->last_cell_seg; |
924 | SCHEME_V->alloc_seg[i] = cp; |
921 | SCHEME_V->alloc_seg[i] = cp; |
925 | |
922 | |
926 | newp = (pointer)cp; |
923 | newp = (struct cell *)cp; |
927 | SCHEME_V->cell_seg[i] = newp; |
924 | SCHEME_V->cell_seg[i] = newp; |
928 | SCHEME_V->cell_segsize[i] = segsize; |
925 | SCHEME_V->cell_segsize[i] = segsize; |
929 | SCHEME_V->fcells += segsize; |
926 | SCHEME_V->fcells += segsize; |
930 | last = newp + segsize - 1; |
927 | last = newp + segsize - 1; |
931 | |
928 | |
932 | for (p = newp; p <= last; p++) |
929 | for (p = newp; p <= last; p++) |
933 | { |
930 | { |
|
|
931 | pointer cp = POINTER (p); |
934 | set_typeflag (p, T_PAIR); |
932 | set_typeflag (cp, T_PAIR); |
935 | set_car (p, NIL); |
933 | set_car (cp, NIL); |
936 | set_cdr (p, p + 1); |
934 | set_cdr (cp, POINTER (p + 1)); |
937 | } |
935 | } |
938 | |
936 | |
939 | set_cdr (last, SCHEME_V->free_cell); |
937 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
940 | SCHEME_V->free_cell = newp; |
938 | SCHEME_V->free_cell = POINTER (newp); |
941 | } |
|
|
942 | |
939 | |
943 | return n; |
940 | return 1; |
944 | } |
941 | } |
945 | |
942 | |
946 | /* get new cell. parameter a, b is marked by gc. */ |
943 | /* get new cell. parameter a, b is marked by gc. */ |
947 | ecb_inline pointer |
944 | ecb_inline pointer |
948 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
945 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
… | |
… | |
952 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
949 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
953 | return S_SINK; |
950 | return S_SINK; |
954 | |
951 | |
955 | if (SCHEME_V->free_cell == NIL) |
952 | if (SCHEME_V->free_cell == NIL) |
956 | { |
953 | { |
957 | const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; |
954 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 2; |
958 | |
955 | |
959 | gc (SCHEME_A_ a, b); |
956 | gc (SCHEME_A_ a, b); |
960 | |
957 | |
961 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
958 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
962 | { |
959 | { |
963 | /* if only a few recovered, get more to avoid fruitless gc's */ |
960 | /* if only a few recovered, get more to avoid fruitless gc's */ |
964 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
961 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
965 | { |
962 | { |
966 | #if USE_ERROR_CHECKING |
963 | #if USE_ERROR_CHECKING |
967 | SCHEME_V->no_memory = 1; |
964 | SCHEME_V->no_memory = 1; |
968 | return S_SINK; |
965 | return S_SINK; |
969 | #endif |
966 | #endif |
… | |
… | |
981 | } |
978 | } |
982 | } |
979 | } |
983 | |
980 | |
984 | /* To retain recent allocs before interpreter knows about them - |
981 | /* To retain recent allocs before interpreter knows about them - |
985 | Tehom */ |
982 | Tehom */ |
986 | |
983 | ecb_hot static void |
987 | static void |
|
|
988 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
984 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
989 | { |
985 | { |
990 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
986 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
991 | |
987 | |
992 | set_typeflag (holder, T_PAIR); |
988 | set_typeflag (holder, T_PAIR); |
… | |
… | |
994 | set_car (holder, recent); |
990 | set_car (holder, recent); |
995 | set_cdr (holder, car (S_SINK)); |
991 | set_cdr (holder, car (S_SINK)); |
996 | set_car (S_SINK, holder); |
992 | set_car (S_SINK, holder); |
997 | } |
993 | } |
998 | |
994 | |
999 | static pointer |
995 | ecb_hot static pointer |
1000 | get_cell (SCHEME_P_ pointer a, pointer b) |
996 | get_cell (SCHEME_P_ pointer a, pointer b) |
1001 | { |
997 | { |
1002 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
998 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
1003 | |
999 | |
1004 | /* For right now, include "a" and "b" in "cell" so that gc doesn't |
1000 | /* For right now, include "a" and "b" in "cell" so that gc doesn't |
… | |
… | |
1013 | } |
1009 | } |
1014 | |
1010 | |
1015 | static pointer |
1011 | static pointer |
1016 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1012 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1017 | { |
1013 | { |
1018 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1014 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
1019 | pointer *e = malloc (len * sizeof (pointer)); |
1015 | pointer *e = malloc (len * sizeof (pointer)); |
1020 | |
1016 | |
1021 | if (!e && USE_ERROR_CHECKING) |
1017 | if (!e && USE_ERROR_CHECKING) |
1022 | return S_SINK; |
1018 | return S_SINK; |
1023 | |
1019 | |
1024 | /* Record it as a vector so that gc understands it. */ |
1020 | /* Record it as a vector so that gc understands it. */ |
1025 | set_typeflag (v, T_VECTOR | T_ATOM); |
1021 | set_typeflag (v, T_VECTOR | T_ATOM); |
1026 | |
1022 | |
1027 | v->object.vector.vvalue = e; |
1023 | CELL(v)->object.vector.vvalue = e; |
1028 | v->object.vector.length = len; |
1024 | CELL(v)->object.vector.length = len; |
1029 | fill_vector (v, 0, init); |
1025 | fill_vector (v, 0, init); |
1030 | push_recent_alloc (SCHEME_A_ v, NIL); |
1026 | push_recent_alloc (SCHEME_A_ v, NIL); |
1031 | |
1027 | |
1032 | return v; |
1028 | return v; |
1033 | } |
1029 | } |
… | |
… | |
1042 | static void |
1038 | static void |
1043 | check_cell_alloced (pointer p, int expect_alloced) |
1039 | check_cell_alloced (pointer p, int expect_alloced) |
1044 | { |
1040 | { |
1045 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1041 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1046 | if (typeflag (p) & !expect_alloced) |
1042 | if (typeflag (p) & !expect_alloced) |
1047 | xwrstr ("Cell is already allocated!\n"); |
1043 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1048 | |
1044 | |
1049 | if (!(typeflag (p)) & expect_alloced) |
1045 | if (!(typeflag (p)) & expect_alloced) |
1050 | xwrstr ("Cell is not allocated!\n"); |
1046 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1051 | } |
1047 | } |
1052 | |
1048 | |
1053 | static void |
1049 | static void |
1054 | check_range_alloced (pointer p, int n, int expect_alloced) |
1050 | check_range_alloced (pointer p, int n, int expect_alloced) |
1055 | { |
1051 | { |
… | |
… | |
1061 | #endif |
1057 | #endif |
1062 | |
1058 | |
1063 | /* Medium level cell allocation */ |
1059 | /* Medium level cell allocation */ |
1064 | |
1060 | |
1065 | /* get new cons cell */ |
1061 | /* get new cons cell */ |
1066 | pointer |
1062 | ecb_hot static pointer |
1067 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1063 | xcons (SCHEME_P_ pointer a, pointer b) |
1068 | { |
1064 | { |
1069 | pointer x = get_cell (SCHEME_A_ a, b); |
1065 | pointer x = get_cell (SCHEME_A_ a, b); |
1070 | |
1066 | |
1071 | set_typeflag (x, T_PAIR); |
1067 | set_typeflag (x, T_PAIR); |
1072 | |
|
|
1073 | if (immutable) |
|
|
1074 | setimmutable (x); |
|
|
1075 | |
1068 | |
1076 | set_car (x, a); |
1069 | set_car (x, a); |
1077 | set_cdr (x, b); |
1070 | set_cdr (x, b); |
1078 | |
1071 | |
1079 | return x; |
1072 | return x; |
1080 | } |
1073 | } |
1081 | |
1074 | |
|
|
1075 | ecb_hot static pointer |
|
|
1076 | ximmutable_cons (SCHEME_P_ pointer a, pointer b) |
|
|
1077 | { |
|
|
1078 | pointer x = xcons (SCHEME_A_ a, b); |
|
|
1079 | setimmutable (x); |
|
|
1080 | return x; |
|
|
1081 | } |
|
|
1082 | |
|
|
1083 | #define cons(a,b) xcons (SCHEME_A_ a, b) |
|
|
1084 | #define immutable_cons(a,b) ximmutable_cons (SCHEME_A_ a, b) |
|
|
1085 | |
|
|
1086 | ecb_cold static pointer |
|
|
1087 | generate_symbol (SCHEME_P_ const char *name) |
|
|
1088 | { |
|
|
1089 | pointer x = mk_string (SCHEME_A_ name); |
|
|
1090 | setimmutable (x); |
|
|
1091 | set_typeflag (x, T_SYMBOL | T_ATOM); |
|
|
1092 | return x; |
|
|
1093 | } |
|
|
1094 | |
1082 | /* ========== oblist implementation ========== */ |
1095 | /* ========== oblist implementation ========== */ |
1083 | |
1096 | |
1084 | #ifndef USE_OBJECT_LIST |
1097 | #ifndef USE_OBJECT_LIST |
1085 | |
1098 | |
|
|
1099 | static int |
1086 | static int hash_fn (const char *key, int table_size); |
1100 | hash_fn (const char *key, int table_size) |
|
|
1101 | { |
|
|
1102 | const unsigned char *p = (unsigned char *)key; |
|
|
1103 | uint32_t hash = 2166136261; |
1087 | |
1104 | |
|
|
1105 | while (*p) |
|
|
1106 | hash = (hash ^ *p++) * 16777619; |
|
|
1107 | |
|
|
1108 | return hash % table_size; |
|
|
1109 | } |
|
|
1110 | |
1088 | static pointer |
1111 | ecb_cold static pointer |
1089 | oblist_initial_value (SCHEME_P) |
1112 | oblist_initial_value (SCHEME_P) |
1090 | { |
1113 | { |
1091 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1114 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1092 | } |
1115 | } |
1093 | |
1116 | |
1094 | /* returns the new symbol */ |
1117 | /* returns the new symbol */ |
1095 | static pointer |
1118 | ecb_cold static pointer |
1096 | oblist_add_by_name (SCHEME_P_ const char *name) |
1119 | oblist_add_by_name (SCHEME_P_ const char *name) |
1097 | { |
1120 | { |
1098 | int location; |
1121 | 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)); |
1122 | 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))); |
1123 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1106 | return x; |
1124 | return x; |
1107 | } |
1125 | } |
1108 | |
1126 | |
1109 | ecb_inline pointer |
1127 | ecb_cold static pointer |
1110 | oblist_find_by_name (SCHEME_P_ const char *name) |
1128 | oblist_find_by_name (SCHEME_P_ const char *name) |
1111 | { |
1129 | { |
1112 | int location; |
1130 | int location; |
1113 | pointer x; |
1131 | pointer x; |
1114 | char *s; |
1132 | char *s; |
… | |
… | |
1125 | } |
1143 | } |
1126 | |
1144 | |
1127 | return NIL; |
1145 | return NIL; |
1128 | } |
1146 | } |
1129 | |
1147 | |
1130 | static pointer |
1148 | ecb_cold static pointer |
1131 | oblist_all_symbols (SCHEME_P) |
1149 | oblist_all_symbols (SCHEME_P) |
1132 | { |
1150 | { |
1133 | int i; |
1151 | int i; |
1134 | pointer x; |
1152 | pointer x; |
1135 | pointer ob_list = NIL; |
1153 | pointer ob_list = NIL; |
… | |
… | |
1141 | return ob_list; |
1159 | return ob_list; |
1142 | } |
1160 | } |
1143 | |
1161 | |
1144 | #else |
1162 | #else |
1145 | |
1163 | |
1146 | static pointer |
1164 | ecb_cold static pointer |
1147 | oblist_initial_value (SCHEME_P) |
1165 | oblist_initial_value (SCHEME_P) |
1148 | { |
1166 | { |
1149 | return NIL; |
1167 | return NIL; |
1150 | } |
1168 | } |
1151 | |
1169 | |
1152 | ecb_inline pointer |
1170 | ecb_cold static pointer |
1153 | oblist_find_by_name (SCHEME_P_ const char *name) |
1171 | oblist_find_by_name (SCHEME_P_ const char *name) |
1154 | { |
1172 | { |
1155 | pointer x; |
1173 | pointer x; |
1156 | char *s; |
1174 | char *s; |
1157 | |
1175 | |
… | |
… | |
1166 | |
1184 | |
1167 | return NIL; |
1185 | return NIL; |
1168 | } |
1186 | } |
1169 | |
1187 | |
1170 | /* returns the new symbol */ |
1188 | /* returns the new symbol */ |
1171 | static pointer |
1189 | ecb_cold static pointer |
1172 | oblist_add_by_name (SCHEME_P_ const char *name) |
1190 | oblist_add_by_name (SCHEME_P_ const char *name) |
1173 | { |
1191 | { |
1174 | pointer x; |
1192 | 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); |
1193 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1180 | return x; |
1194 | return x; |
1181 | } |
1195 | } |
1182 | |
1196 | |
1183 | static pointer |
1197 | ecb_cold static pointer |
1184 | oblist_all_symbols (SCHEME_P) |
1198 | oblist_all_symbols (SCHEME_P) |
1185 | { |
1199 | { |
1186 | return SCHEME_V->oblist; |
1200 | return SCHEME_V->oblist; |
1187 | } |
1201 | } |
1188 | |
1202 | |
1189 | #endif |
1203 | #endif |
1190 | |
1204 | |
1191 | #if USE_PORTS |
|
|
1192 | static pointer |
1205 | ecb_cold static pointer |
1193 | mk_port (SCHEME_P_ port *p) |
1206 | mk_port (SCHEME_P_ port *p) |
1194 | { |
1207 | { |
1195 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1208 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1196 | |
1209 | |
1197 | set_typeflag (x, T_PORT | T_ATOM); |
1210 | set_typeflag (x, T_PORT | T_ATOM); |
1198 | x->object.port = p; |
1211 | set_port (x, p); |
1199 | |
1212 | |
1200 | return x; |
1213 | return x; |
1201 | } |
1214 | } |
1202 | #endif |
|
|
1203 | |
1215 | |
1204 | pointer |
1216 | ecb_cold pointer |
1205 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1217 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1206 | { |
1218 | { |
1207 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1219 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1208 | |
1220 | |
1209 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1221 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1210 | x->object.ff = f; |
1222 | CELL(x)->object.ff = f; |
1211 | |
1223 | |
1212 | return x; |
1224 | return x; |
1213 | } |
1225 | } |
1214 | |
1226 | |
1215 | INTERFACE pointer |
1227 | INTERFACE pointer |
1216 | mk_character (SCHEME_P_ int c) |
1228 | mk_character (SCHEME_P_ int c) |
1217 | { |
1229 | { |
1218 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1230 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1219 | |
1231 | |
1220 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1232 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1221 | set_ivalue (x, c & 0xff); |
1233 | set_ivalue (x, c & 0xff); |
1222 | |
1234 | |
1223 | return x; |
1235 | return x; |
1224 | } |
1236 | } |
1225 | |
1237 | |
1226 | /* get number atom (integer) */ |
1238 | /* get number atom (integer) */ |
1227 | INTERFACE pointer |
1239 | INTERFACE pointer |
1228 | mk_integer (SCHEME_P_ long n) |
1240 | mk_integer (SCHEME_P_ long n) |
1229 | { |
1241 | { |
|
|
1242 | pointer p = 0; |
|
|
1243 | pointer *pp = &p; |
|
|
1244 | |
|
|
1245 | #if USE_INTCACHE |
|
|
1246 | if (n >= INTCACHE_MIN && n <= INTCACHE_MAX) |
|
|
1247 | pp = &SCHEME_V->intcache[n - INTCACHE_MIN]; |
|
|
1248 | #endif |
|
|
1249 | |
|
|
1250 | if (!*pp) |
|
|
1251 | { |
1230 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1252 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1231 | |
1253 | |
1232 | set_typeflag (x, (T_INTEGER | T_ATOM)); |
1254 | set_typeflag (x, T_INTEGER | T_ATOM); |
|
|
1255 | setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */ |
1233 | set_ivalue (x, n); |
1256 | set_ivalue (x, n); |
1234 | |
1257 | |
|
|
1258 | *pp = x; |
|
|
1259 | } |
|
|
1260 | |
1235 | return x; |
1261 | return *pp; |
1236 | } |
1262 | } |
1237 | |
1263 | |
1238 | INTERFACE pointer |
1264 | INTERFACE pointer |
1239 | mk_real (SCHEME_P_ RVALUE n) |
1265 | mk_real (SCHEME_P_ RVALUE n) |
1240 | { |
1266 | { |
1241 | #if USE_REAL |
1267 | #if USE_REAL |
1242 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1268 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1243 | |
1269 | |
1244 | set_typeflag (x, (T_REAL | T_ATOM)); |
1270 | set_typeflag (x, T_REAL | T_ATOM); |
1245 | set_rvalue (x, n); |
1271 | set_rvalue (x, n); |
1246 | |
1272 | |
1247 | return x; |
1273 | return x; |
1248 | #else |
1274 | #else |
1249 | return mk_integer (SCHEME_A_ n); |
1275 | return mk_integer (SCHEME_A_ n); |
… | |
… | |
1273 | SCHEME_V->no_memory = 1; |
1299 | SCHEME_V->no_memory = 1; |
1274 | return SCHEME_V->strbuff; |
1300 | return SCHEME_V->strbuff; |
1275 | } |
1301 | } |
1276 | |
1302 | |
1277 | if (str) |
1303 | if (str) |
1278 | { |
1304 | 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 |
1305 | else |
1288 | { |
|
|
1289 | memset (q, fill, len_str); |
1306 | memset (q, fill, len_str); |
|
|
1307 | |
1290 | q[len_str] = 0; |
1308 | q[len_str] = 0; |
1291 | } |
|
|
1292 | |
1309 | |
1293 | return q; |
1310 | return q; |
1294 | } |
1311 | } |
1295 | |
1312 | |
1296 | INTERFACE pointer |
1313 | INTERFACE pointer |
… | |
… | |
1310 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1327 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1311 | |
1328 | |
1312 | set_typeflag (x, T_STRING | T_ATOM); |
1329 | set_typeflag (x, T_STRING | T_ATOM); |
1313 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1330 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1314 | strlength (x) = len; |
1331 | strlength (x) = len; |
|
|
1332 | |
1315 | return x; |
1333 | return x; |
1316 | } |
1334 | } |
1317 | |
1335 | |
1318 | INTERFACE pointer |
1336 | INTERFACE pointer |
1319 | mk_string (SCHEME_P_ const char *str) |
1337 | mk_string (SCHEME_P_ const char *str) |
… | |
… | |
1332 | { |
1350 | { |
1333 | int i; |
1351 | int i; |
1334 | |
1352 | |
1335 | for (i = start; i < veclength (vec); i++) |
1353 | for (i = start; i < veclength (vec); i++) |
1336 | vecvalue (vec)[i] = obj; |
1354 | vecvalue (vec)[i] = obj; |
|
|
1355 | } |
|
|
1356 | |
|
|
1357 | INTERFACE void |
|
|
1358 | vector_resize (pointer vec, uint32_t newsize, pointer fill) |
|
|
1359 | { |
|
|
1360 | uint32_t oldsize = veclength (vec); |
|
|
1361 | vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer)); |
|
|
1362 | veclength (vec) = newsize; |
|
|
1363 | fill_vector (vec, oldsize, fill); |
1337 | } |
1364 | } |
1338 | |
1365 | |
1339 | INTERFACE pointer |
1366 | INTERFACE pointer |
1340 | vector_get (pointer vec, uint32_t ielem) |
1367 | vector_get (pointer vec, uint32_t ielem) |
1341 | { |
1368 | { |
… | |
… | |
1359 | x = oblist_add_by_name (SCHEME_A_ name); |
1386 | x = oblist_add_by_name (SCHEME_A_ name); |
1360 | |
1387 | |
1361 | return x; |
1388 | return x; |
1362 | } |
1389 | } |
1363 | |
1390 | |
1364 | INTERFACE pointer |
1391 | ecb_cold INTERFACE pointer |
1365 | gensym (SCHEME_P) |
1392 | gensym (SCHEME_P) |
1366 | { |
1393 | { |
1367 | pointer x; |
1394 | pointer x; |
1368 | |
|
|
1369 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
|
|
1370 | { |
|
|
1371 | char name[40] = "gensym-"; |
1395 | char name[40] = "gensym-"; |
1372 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1396 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1373 | |
1397 | |
1374 | /* first check oblist */ |
1398 | return generate_symbol (SCHEME_A_ name); |
1375 | x = oblist_find_by_name (SCHEME_A_ name); |
1399 | } |
1376 | |
1400 | |
1377 | if (x == NIL) |
1401 | static int |
1378 | { |
1402 | is_gensym (SCHEME_P_ pointer x) |
1379 | x = oblist_add_by_name (SCHEME_A_ name); |
1403 | { |
1380 | return x; |
1404 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1381 | } |
|
|
1382 | } |
|
|
1383 | |
|
|
1384 | return NIL; |
|
|
1385 | } |
1405 | } |
1386 | |
1406 | |
1387 | /* make symbol or number atom from string */ |
1407 | /* make symbol or number atom from string */ |
1388 | static pointer |
1408 | ecb_cold static pointer |
1389 | mk_atom (SCHEME_P_ char *q) |
1409 | mk_atom (SCHEME_P_ char *q) |
1390 | { |
1410 | { |
1391 | char c, *p; |
1411 | char c, *p; |
1392 | int has_dec_point = 0; |
1412 | int has_dec_point = 0; |
1393 | int has_fp_exp = 0; |
1413 | int has_fp_exp = 0; |
… | |
… | |
1464 | |
1484 | |
1465 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1485 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1466 | } |
1486 | } |
1467 | |
1487 | |
1468 | /* make constant */ |
1488 | /* make constant */ |
1469 | static pointer |
1489 | ecb_cold static pointer |
1470 | mk_sharp_const (SCHEME_P_ char *name) |
1490 | mk_sharp_const (SCHEME_P_ char *name) |
1471 | { |
1491 | { |
1472 | if (!strcmp (name, "t")) |
1492 | if (!strcmp (name, "t")) |
1473 | return S_T; |
1493 | return S_T; |
1474 | else if (!strcmp (name, "f")) |
1494 | else if (!strcmp (name, "f")) |
1475 | return S_F; |
1495 | return S_F; |
1476 | else if (*name == '\\') /* #\w (character) */ |
1496 | else if (*name == '\\') /* #\w (character) */ |
1477 | { |
1497 | { |
1478 | int c; |
1498 | int c; |
1479 | |
1499 | |
|
|
1500 | // TODO: optimise |
1480 | if (stricmp (name + 1, "space") == 0) |
1501 | if (stricmp (name + 1, "space") == 0) |
1481 | c = ' '; |
1502 | c = ' '; |
1482 | else if (stricmp (name + 1, "newline") == 0) |
1503 | else if (stricmp (name + 1, "newline") == 0) |
1483 | c = '\n'; |
1504 | c = '\n'; |
1484 | else if (stricmp (name + 1, "return") == 0) |
1505 | else if (stricmp (name + 1, "return") == 0) |
1485 | c = '\r'; |
1506 | c = '\r'; |
1486 | else if (stricmp (name + 1, "tab") == 0) |
1507 | else if (stricmp (name + 1, "tab") == 0) |
1487 | c = '\t'; |
1508 | c = '\t'; |
|
|
1509 | else if (stricmp (name + 1, "alarm") == 0) |
|
|
1510 | c = 0x07; |
|
|
1511 | else if (stricmp (name + 1, "backspace") == 0) |
|
|
1512 | c = 0x08; |
|
|
1513 | else if (stricmp (name + 1, "escape") == 0) |
|
|
1514 | c = 0x1b; |
|
|
1515 | else if (stricmp (name + 1, "delete") == 0) |
|
|
1516 | c = 0x7f; |
|
|
1517 | else if (stricmp (name + 1, "null") == 0) |
|
|
1518 | c = 0; |
1488 | else if (name[1] == 'x' && name[2] != 0) |
1519 | else if (name[1] == 'x' && name[2] != 0) |
1489 | { |
1520 | { |
1490 | long c1 = strtol (name + 2, 0, 16); |
1521 | long c1 = strtol (name + 2, 0, 16); |
1491 | |
1522 | |
1492 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1523 | if (0 <= c1 && c1 <= UCHAR_MAX) |
… | |
… | |
1517 | return NIL; |
1548 | return NIL; |
1518 | } |
1549 | } |
1519 | } |
1550 | } |
1520 | |
1551 | |
1521 | /* ========== garbage collector ========== */ |
1552 | /* ========== garbage collector ========== */ |
|
|
1553 | |
|
|
1554 | static void |
|
|
1555 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1556 | { |
|
|
1557 | /* TODO, fast bitmap check? */ |
|
|
1558 | if (is_string (a) || is_symbol (a)) |
|
|
1559 | free (strvalue (a)); |
|
|
1560 | else if (is_vector (a)) |
|
|
1561 | free (vecvalue (a)); |
|
|
1562 | #if USE_PORTS |
|
|
1563 | else if (is_port (a)) |
|
|
1564 | { |
|
|
1565 | if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) |
|
|
1566 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1567 | |
|
|
1568 | free (port (a)); |
|
|
1569 | } |
|
|
1570 | #endif |
|
|
1571 | } |
1522 | |
1572 | |
1523 | /*-- |
1573 | /*-- |
1524 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1574 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1525 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1575 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1526 | * for marking. |
1576 | * for marking. |
1527 | * |
1577 | * |
1528 | * The exception is vectors - vectors are currently marked recursively, |
1578 | * The exception is vectors - vectors are currently marked recursively, |
1529 | * which is inherited form tinyscheme and could be fixed by having another |
1579 | * which is inherited form tinyscheme and could be fixed by having another |
1530 | * word of context in the vector |
1580 | * word of context in the vector |
1531 | */ |
1581 | */ |
1532 | static void |
1582 | ecb_hot static void |
1533 | mark (pointer a) |
1583 | mark (pointer a) |
1534 | { |
1584 | { |
1535 | pointer t, q, p; |
1585 | pointer t, q, p; |
1536 | |
1586 | |
1537 | t = 0; |
1587 | t = 0; |
… | |
… | |
1594 | p = q; |
1644 | p = q; |
1595 | goto E6; |
1645 | goto E6; |
1596 | } |
1646 | } |
1597 | } |
1647 | } |
1598 | |
1648 | |
|
|
1649 | ecb_hot static void |
|
|
1650 | gc_free (SCHEME_P) |
|
|
1651 | { |
|
|
1652 | int i; |
|
|
1653 | uint32_t total = 0; |
|
|
1654 | |
|
|
1655 | /* Here we scan the cells to build the free-list. */ |
|
|
1656 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1657 | { |
|
|
1658 | struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1659 | struct cell *p; |
|
|
1660 | total += SCHEME_V->cell_segsize [i]; |
|
|
1661 | |
|
|
1662 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
|
|
1663 | { |
|
|
1664 | pointer c = POINTER (p); |
|
|
1665 | |
|
|
1666 | if (is_mark (c)) |
|
|
1667 | clrmark (c); |
|
|
1668 | else |
|
|
1669 | { |
|
|
1670 | /* reclaim cell */ |
|
|
1671 | if (typeflag (c) != T_PAIR) |
|
|
1672 | { |
|
|
1673 | finalize_cell (SCHEME_A_ c); |
|
|
1674 | set_typeflag (c, T_PAIR); |
|
|
1675 | set_car (c, NIL); |
|
|
1676 | } |
|
|
1677 | |
|
|
1678 | ++SCHEME_V->fcells; |
|
|
1679 | set_cdr (c, SCHEME_V->free_cell); |
|
|
1680 | SCHEME_V->free_cell = c; |
|
|
1681 | } |
|
|
1682 | } |
|
|
1683 | } |
|
|
1684 | |
|
|
1685 | if (SCHEME_V->gc_verbose) |
|
|
1686 | { |
|
|
1687 | 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"); |
|
|
1688 | } |
|
|
1689 | } |
|
|
1690 | |
1599 | /* garbage collection. parameter a, b is marked. */ |
1691 | /* garbage collection. parameter a, b is marked. */ |
1600 | static void |
1692 | ecb_cold static void |
1601 | gc (SCHEME_P_ pointer a, pointer b) |
1693 | gc (SCHEME_P_ pointer a, pointer b) |
1602 | { |
1694 | { |
1603 | pointer p; |
|
|
1604 | int i; |
1695 | int i; |
1605 | |
1696 | |
1606 | if (SCHEME_V->gc_verbose) |
1697 | if (SCHEME_V->gc_verbose) |
1607 | putstr (SCHEME_A_ "gc..."); |
1698 | putstr (SCHEME_A_ "gc..."); |
1608 | |
1699 | |
… | |
… | |
1624 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1715 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1625 | mark (car (S_SINK)); |
1716 | mark (car (S_SINK)); |
1626 | /* Mark any older stuff above nested C calls */ |
1717 | /* Mark any older stuff above nested C calls */ |
1627 | mark (SCHEME_V->c_nest); |
1718 | mark (SCHEME_V->c_nest); |
1628 | |
1719 | |
|
|
1720 | #if USE_INTCACHE |
|
|
1721 | /* mark intcache */ |
|
|
1722 | for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i) |
|
|
1723 | if (SCHEME_V->intcache[i - INTCACHE_MIN]) |
|
|
1724 | mark (SCHEME_V->intcache[i - INTCACHE_MIN]); |
|
|
1725 | #endif |
|
|
1726 | |
1629 | /* mark variables a, b */ |
1727 | /* mark variables a, b */ |
1630 | mark (a); |
1728 | mark (a); |
1631 | mark (b); |
1729 | mark (b); |
1632 | |
1730 | |
1633 | /* garbage collect */ |
1731 | /* garbage collect */ |
1634 | clrmark (NIL); |
1732 | clrmark (NIL); |
1635 | SCHEME_V->fcells = 0; |
1733 | SCHEME_V->fcells = 0; |
1636 | SCHEME_V->free_cell = NIL; |
1734 | SCHEME_V->free_cell = NIL; |
1637 | |
1735 | |
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) |
1736 | if (SCHEME_V->gc_verbose) |
1669 | { |
1737 | putstr (SCHEME_A_ "freeing..."); |
1670 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
|
|
1671 | } |
|
|
1672 | } |
|
|
1673 | |
1738 | |
1674 | static void |
1739 | 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 | } |
1740 | } |
1692 | |
1741 | |
1693 | /* ========== Routines for Reading ========== */ |
1742 | /* ========== Routines for Reading ========== */ |
1694 | |
1743 | |
1695 | static int |
1744 | ecb_cold static int |
1696 | file_push (SCHEME_P_ const char *fname) |
1745 | file_push (SCHEME_P_ const char *fname) |
1697 | { |
1746 | { |
1698 | #if USE_PORTS |
|
|
1699 | int fin; |
1747 | int fin; |
1700 | |
1748 | |
1701 | if (SCHEME_V->file_i == MAXFIL - 1) |
1749 | if (SCHEME_V->file_i == MAXFIL - 1) |
1702 | return 0; |
1750 | return 0; |
1703 | |
1751 | |
… | |
… | |
1709 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1757 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1710 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1758 | 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; |
1759 | 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; |
1760 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1713 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1761 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1714 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1762 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1715 | |
1763 | |
1716 | #if SHOW_ERROR_LINE |
1764 | #if SHOW_ERROR_LINE |
1717 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1765 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1718 | |
1766 | |
1719 | if (fname) |
1767 | if (fname) |
1720 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0); |
1768 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0); |
1721 | #endif |
1769 | #endif |
1722 | } |
1770 | } |
1723 | |
1771 | |
1724 | return fin >= 0; |
1772 | return fin >= 0; |
1725 | |
|
|
1726 | #else |
|
|
1727 | return 1; |
|
|
1728 | #endif |
|
|
1729 | } |
1773 | } |
1730 | |
1774 | |
1731 | static void |
1775 | ecb_cold static void |
1732 | file_pop (SCHEME_P) |
1776 | file_pop (SCHEME_P) |
1733 | { |
1777 | { |
1734 | if (SCHEME_V->file_i != 0) |
1778 | if (SCHEME_V->file_i != 0) |
1735 | { |
1779 | { |
1736 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1780 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1737 | #if USE_PORTS |
1781 | #if USE_PORTS |
1738 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1782 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1739 | #endif |
1783 | #endif |
1740 | SCHEME_V->file_i--; |
1784 | SCHEME_V->file_i--; |
1741 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1785 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1742 | } |
1786 | } |
1743 | } |
1787 | } |
1744 | |
1788 | |
1745 | static int |
1789 | ecb_cold static int |
1746 | file_interactive (SCHEME_P) |
1790 | file_interactive (SCHEME_P) |
1747 | { |
1791 | { |
1748 | #if USE_PORTS |
1792 | #if USE_PORTS |
1749 | return SCHEME_V->file_i == 0 |
1793 | return SCHEME_V->file_i == 0 |
1750 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1794 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1751 | && (SCHEME_V->inport->object.port->kind & port_file); |
1795 | && (port (SCHEME_V->inport)->kind & port_file); |
1752 | #else |
1796 | #else |
1753 | return 0; |
1797 | return 0; |
1754 | #endif |
1798 | #endif |
1755 | } |
1799 | } |
1756 | |
1800 | |
1757 | #if USE_PORTS |
1801 | #if USE_PORTS |
1758 | static port * |
1802 | ecb_cold static port * |
1759 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1803 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1760 | { |
1804 | { |
1761 | int fd; |
1805 | int fd; |
1762 | int flags; |
1806 | int flags; |
1763 | char *rw; |
1807 | char *rw; |
… | |
… | |
1786 | # endif |
1830 | # endif |
1787 | |
1831 | |
1788 | return pt; |
1832 | return pt; |
1789 | } |
1833 | } |
1790 | |
1834 | |
1791 | static pointer |
1835 | ecb_cold static pointer |
1792 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1836 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1793 | { |
1837 | { |
1794 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1838 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1795 | |
1839 | |
1796 | if (!pt && USE_ERROR_CHECKING) |
1840 | if (!pt && USE_ERROR_CHECKING) |
1797 | return NIL; |
1841 | return NIL; |
1798 | |
1842 | |
1799 | return mk_port (SCHEME_A_ pt); |
1843 | return mk_port (SCHEME_A_ pt); |
1800 | } |
1844 | } |
1801 | |
1845 | |
1802 | static port * |
1846 | ecb_cold static port * |
1803 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1847 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1804 | { |
1848 | { |
1805 | port *pt = malloc (sizeof *pt); |
1849 | port *pt = malloc (sizeof *pt); |
1806 | |
1850 | |
1807 | if (!pt && USE_ERROR_CHECKING) |
1851 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1812 | pt->rep.stdio.file = f; |
1856 | pt->rep.stdio.file = f; |
1813 | pt->rep.stdio.closeit = 0; |
1857 | pt->rep.stdio.closeit = 0; |
1814 | return pt; |
1858 | return pt; |
1815 | } |
1859 | } |
1816 | |
1860 | |
1817 | static pointer |
1861 | ecb_cold static pointer |
1818 | port_from_file (SCHEME_P_ int f, int prop) |
1862 | port_from_file (SCHEME_P_ int f, int prop) |
1819 | { |
1863 | { |
1820 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1864 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1821 | |
1865 | |
1822 | if (!pt && USE_ERROR_CHECKING) |
1866 | if (!pt && USE_ERROR_CHECKING) |
1823 | return NIL; |
1867 | return NIL; |
1824 | |
1868 | |
1825 | return mk_port (SCHEME_A_ pt); |
1869 | return mk_port (SCHEME_A_ pt); |
1826 | } |
1870 | } |
1827 | |
1871 | |
1828 | static port * |
1872 | ecb_cold static port * |
1829 | port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1873 | port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1830 | { |
1874 | { |
1831 | port *pt = malloc (sizeof (port)); |
1875 | port *pt = malloc (sizeof (port)); |
1832 | |
1876 | |
1833 | if (!pt && USE_ERROR_CHECKING) |
1877 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1839 | pt->rep.string.curr = start; |
1883 | pt->rep.string.curr = start; |
1840 | pt->rep.string.past_the_end = past_the_end; |
1884 | pt->rep.string.past_the_end = past_the_end; |
1841 | return pt; |
1885 | return pt; |
1842 | } |
1886 | } |
1843 | |
1887 | |
1844 | static pointer |
1888 | ecb_cold static pointer |
1845 | port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1889 | port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1846 | { |
1890 | { |
1847 | port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); |
1891 | port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); |
1848 | |
1892 | |
1849 | if (!pt && USE_ERROR_CHECKING) |
1893 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1852 | return mk_port (SCHEME_A_ pt); |
1896 | return mk_port (SCHEME_A_ pt); |
1853 | } |
1897 | } |
1854 | |
1898 | |
1855 | # define BLOCK_SIZE 256 |
1899 | # define BLOCK_SIZE 256 |
1856 | |
1900 | |
1857 | static port * |
1901 | ecb_cold static port * |
1858 | port_rep_from_scratch (SCHEME_P) |
1902 | port_rep_from_scratch (SCHEME_P) |
1859 | { |
1903 | { |
1860 | char *start; |
1904 | char *start; |
1861 | port *pt = malloc (sizeof (port)); |
1905 | port *pt = malloc (sizeof (port)); |
1862 | |
1906 | |
… | |
… | |
1876 | pt->rep.string.curr = start; |
1920 | pt->rep.string.curr = start; |
1877 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1921 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1878 | return pt; |
1922 | return pt; |
1879 | } |
1923 | } |
1880 | |
1924 | |
1881 | static pointer |
1925 | ecb_cold static pointer |
1882 | port_from_scratch (SCHEME_P) |
1926 | port_from_scratch (SCHEME_P) |
1883 | { |
1927 | { |
1884 | port *pt = port_rep_from_scratch (SCHEME_A); |
1928 | port *pt = port_rep_from_scratch (SCHEME_A); |
1885 | |
1929 | |
1886 | if (!pt && USE_ERROR_CHECKING) |
1930 | if (!pt && USE_ERROR_CHECKING) |
1887 | return NIL; |
1931 | return NIL; |
1888 | |
1932 | |
1889 | return mk_port (SCHEME_A_ pt); |
1933 | return mk_port (SCHEME_A_ pt); |
1890 | } |
1934 | } |
1891 | |
1935 | |
1892 | static void |
1936 | ecb_cold static void |
1893 | port_close (SCHEME_P_ pointer p, int flag) |
1937 | port_close (SCHEME_P_ pointer p, int flag) |
1894 | { |
1938 | { |
1895 | port *pt = p->object.port; |
1939 | port *pt = port (p); |
1896 | |
1940 | |
1897 | pt->kind &= ~flag; |
1941 | pt->kind &= ~flag; |
1898 | |
1942 | |
1899 | if ((pt->kind & (port_input | port_output)) == 0) |
1943 | if ((pt->kind & (port_input | port_output)) == 0) |
1900 | { |
1944 | { |
… | |
… | |
1917 | } |
1961 | } |
1918 | } |
1962 | } |
1919 | #endif |
1963 | #endif |
1920 | |
1964 | |
1921 | /* get new character from input file */ |
1965 | /* get new character from input file */ |
1922 | static int |
1966 | ecb_cold static int |
1923 | inchar (SCHEME_P) |
1967 | inchar (SCHEME_P) |
1924 | { |
1968 | { |
1925 | int c; |
1969 | int c; |
1926 | port *pt; |
1970 | port *pt = port (SCHEME_V->inport); |
1927 | |
|
|
1928 | pt = SCHEME_V->inport->object.port; |
|
|
1929 | |
1971 | |
1930 | if (pt->kind & port_saw_EOF) |
1972 | if (pt->kind & port_saw_EOF) |
1931 | return EOF; |
1973 | return EOF; |
1932 | |
1974 | |
1933 | c = basic_inchar (pt); |
1975 | c = basic_inchar (pt); |
… | |
… | |
1943 | } |
1985 | } |
1944 | |
1986 | |
1945 | return c; |
1987 | return c; |
1946 | } |
1988 | } |
1947 | |
1989 | |
1948 | static int ungot = -1; |
1990 | ecb_cold static int |
1949 | |
|
|
1950 | static int |
|
|
1951 | basic_inchar (port *pt) |
1991 | basic_inchar (port *pt) |
1952 | { |
1992 | { |
1953 | #if USE_PORTS |
|
|
1954 | if (pt->unget != -1) |
1993 | if (pt->unget != -1) |
1955 | { |
1994 | { |
1956 | int r = pt->unget; |
1995 | int r = pt->unget; |
1957 | pt->unget = -1; |
1996 | pt->unget = -1; |
1958 | return r; |
1997 | return r; |
1959 | } |
1998 | } |
1960 | |
1999 | |
|
|
2000 | #if USE_PORTS |
1961 | if (pt->kind & port_file) |
2001 | if (pt->kind & port_file) |
1962 | { |
2002 | { |
1963 | char c; |
2003 | char c; |
1964 | |
2004 | |
1965 | if (!read (pt->rep.stdio.file, &c, 1)) |
2005 | if (!read (pt->rep.stdio.file, &c, 1)) |
… | |
… | |
1973 | return EOF; |
2013 | return EOF; |
1974 | else |
2014 | else |
1975 | return *pt->rep.string.curr++; |
2015 | return *pt->rep.string.curr++; |
1976 | } |
2016 | } |
1977 | #else |
2017 | #else |
1978 | if (ungot == -1) |
|
|
1979 | { |
|
|
1980 | char c; |
2018 | char c; |
1981 | if (!read (0, &c, 1)) |
2019 | |
|
|
2020 | if (!read (pt->rep.stdio.file, &c, 1)) |
1982 | return EOF; |
2021 | return EOF; |
1983 | |
2022 | |
1984 | ungot = c; |
|
|
1985 | } |
|
|
1986 | |
|
|
1987 | { |
|
|
1988 | int r = ungot; |
|
|
1989 | ungot = -1; |
|
|
1990 | return r; |
2023 | return c; |
1991 | } |
|
|
1992 | #endif |
2024 | #endif |
1993 | } |
2025 | } |
1994 | |
2026 | |
1995 | /* back character to input buffer */ |
2027 | /* back character to input buffer */ |
1996 | static void |
2028 | ecb_cold static void |
1997 | backchar (SCHEME_P_ int c) |
2029 | backchar (SCHEME_P_ int c) |
1998 | { |
2030 | { |
1999 | #if USE_PORTS |
2031 | port *pt = port (SCHEME_V->inport); |
2000 | port *pt; |
|
|
2001 | |
2032 | |
2002 | if (c == EOF) |
2033 | if (c == EOF) |
2003 | return; |
2034 | return; |
2004 | |
2035 | |
2005 | pt = SCHEME_V->inport->object.port; |
|
|
2006 | pt->unget = c; |
2036 | pt->unget = c; |
2007 | #else |
|
|
2008 | if (c == EOF) |
|
|
2009 | return; |
|
|
2010 | |
|
|
2011 | ungot = c; |
|
|
2012 | #endif |
|
|
2013 | } |
2037 | } |
2014 | |
2038 | |
2015 | #if USE_PORTS |
2039 | #if USE_PORTS |
2016 | static int |
2040 | ecb_cold static int |
2017 | realloc_port_string (SCHEME_P_ port *p) |
2041 | realloc_port_string (SCHEME_P_ port *p) |
2018 | { |
2042 | { |
2019 | char *start = p->rep.string.start; |
2043 | char *start = p->rep.string.start; |
2020 | size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; |
2044 | size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; |
2021 | char *str = malloc (new_size); |
2045 | char *str = malloc (new_size); |
… | |
… | |
2034 | else |
2058 | else |
2035 | return 0; |
2059 | return 0; |
2036 | } |
2060 | } |
2037 | #endif |
2061 | #endif |
2038 | |
2062 | |
2039 | INTERFACE void |
2063 | ecb_cold static void |
2040 | putstr (SCHEME_P_ const char *s) |
2064 | putchars (SCHEME_P_ const char *s, int len) |
2041 | { |
2065 | { |
|
|
2066 | port *pt = port (SCHEME_V->outport); |
|
|
2067 | |
2042 | #if USE_PORTS |
2068 | #if USE_PORTS |
2043 | port *pt = SCHEME_V->outport->object.port; |
|
|
2044 | |
|
|
2045 | if (pt->kind & port_file) |
|
|
2046 | write (pt->rep.stdio.file, s, strlen (s)); |
|
|
2047 | else |
|
|
2048 | for (; *s; s++) |
|
|
2049 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2050 | *pt->rep.string.curr++ = *s; |
|
|
2051 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2052 | *pt->rep.string.curr++ = *s; |
|
|
2053 | |
|
|
2054 | #else |
|
|
2055 | xwrstr (s); |
|
|
2056 | #endif |
|
|
2057 | } |
|
|
2058 | |
|
|
2059 | static void |
|
|
2060 | putchars (SCHEME_P_ const char *s, int len) |
|
|
2061 | { |
|
|
2062 | #if USE_PORTS |
|
|
2063 | port *pt = SCHEME_V->outport->object.port; |
|
|
2064 | |
|
|
2065 | if (pt->kind & port_file) |
2069 | if (pt->kind & port_file) |
2066 | write (pt->rep.stdio.file, s, len); |
2070 | write (pt->rep.stdio.file, s, len); |
2067 | else |
2071 | else |
2068 | { |
2072 | { |
2069 | for (; len; len--) |
2073 | for (; len; len--) |
… | |
… | |
2074 | *pt->rep.string.curr++ = *s++; |
2078 | *pt->rep.string.curr++ = *s++; |
2075 | } |
2079 | } |
2076 | } |
2080 | } |
2077 | |
2081 | |
2078 | #else |
2082 | #else |
2079 | write (1, s, len); |
2083 | write (1, s, len); // output not initialised |
2080 | #endif |
2084 | #endif |
|
|
2085 | } |
|
|
2086 | |
|
|
2087 | INTERFACE void |
|
|
2088 | putstr (SCHEME_P_ const char *s) |
|
|
2089 | { |
|
|
2090 | putchars (SCHEME_A_ s, strlen (s)); |
2081 | } |
2091 | } |
2082 | |
2092 | |
2083 | INTERFACE void |
2093 | INTERFACE void |
2084 | putcharacter (SCHEME_P_ int c) |
2094 | putcharacter (SCHEME_P_ int c) |
2085 | { |
2095 | { |
2086 | #if USE_PORTS |
|
|
2087 | port *pt = SCHEME_V->outport->object.port; |
|
|
2088 | |
|
|
2089 | if (pt->kind & port_file) |
|
|
2090 | { |
|
|
2091 | char cc = c; |
|
|
2092 | write (pt->rep.stdio.file, &cc, 1); |
|
|
2093 | } |
|
|
2094 | else |
|
|
2095 | { |
|
|
2096 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2097 | *pt->rep.string.curr++ = c; |
|
|
2098 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2099 | *pt->rep.string.curr++ = c; |
|
|
2100 | } |
|
|
2101 | |
|
|
2102 | #else |
|
|
2103 | char cc = c; |
2096 | char cc = c; |
2104 | write (1, &c, 1); |
2097 | |
2105 | #endif |
2098 | putchars (SCHEME_A_ &cc, 1); |
2106 | } |
2099 | } |
2107 | |
2100 | |
2108 | /* read characters up to delimiter, but cater to character constants */ |
2101 | /* read characters up to delimiter, but cater to character constants */ |
2109 | static char * |
2102 | ecb_cold static char * |
2110 | readstr_upto (SCHEME_P_ char *delim) |
2103 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2111 | { |
2104 | { |
2112 | char *p = SCHEME_V->strbuff; |
2105 | char *p = SCHEME_V->strbuff + skip; |
2113 | |
2106 | |
2114 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2107 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2115 | |
2108 | |
2116 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2109 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2117 | *p = 0; |
2110 | *p = 0; |
… | |
… | |
2123 | |
2116 | |
2124 | return SCHEME_V->strbuff; |
2117 | return SCHEME_V->strbuff; |
2125 | } |
2118 | } |
2126 | |
2119 | |
2127 | /* read string expression "xxx...xxx" */ |
2120 | /* read string expression "xxx...xxx" */ |
2128 | static pointer |
2121 | ecb_cold static pointer |
2129 | readstrexp (SCHEME_P) |
2122 | readstrexp (SCHEME_P_ char delim) |
2130 | { |
2123 | { |
2131 | char *p = SCHEME_V->strbuff; |
2124 | char *p = SCHEME_V->strbuff; |
2132 | int c; |
2125 | int c; |
2133 | int c1 = 0; |
2126 | int c1 = 0; |
2134 | enum |
|
|
2135 | { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2127 | enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2136 | |
2128 | |
2137 | for (;;) |
2129 | for (;;) |
2138 | { |
2130 | { |
2139 | c = inchar (SCHEME_A); |
2131 | c = inchar (SCHEME_A); |
2140 | |
2132 | |
… | |
… | |
2142 | return S_F; |
2134 | return S_F; |
2143 | |
2135 | |
2144 | switch (state) |
2136 | switch (state) |
2145 | { |
2137 | { |
2146 | case st_ok: |
2138 | case st_ok: |
2147 | switch (c) |
2139 | 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); |
2140 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2156 | |
2141 | |
2157 | default: |
2142 | if (ecb_expect_false (c == '\\')) |
|
|
2143 | state = st_bsl; |
|
|
2144 | else |
2158 | *p++ = c; |
2145 | *p++ = c; |
2159 | break; |
|
|
2160 | } |
|
|
2161 | |
2146 | |
2162 | break; |
2147 | break; |
2163 | |
2148 | |
2164 | case st_bsl: |
2149 | case st_bsl: |
2165 | switch (c) |
2150 | switch (c) |
… | |
… | |
2174 | case '7': |
2159 | case '7': |
2175 | state = st_oct1; |
2160 | state = st_oct1; |
2176 | c1 = c - '0'; |
2161 | c1 = c - '0'; |
2177 | break; |
2162 | break; |
2178 | |
2163 | |
|
|
2164 | case 'a': *p++ = '\a'; state = st_ok; break; |
|
|
2165 | case 'n': *p++ = '\n'; state = st_ok; break; |
|
|
2166 | case 'r': *p++ = '\r'; state = st_ok; break; |
|
|
2167 | case 't': *p++ = '\t'; state = st_ok; break; |
|
|
2168 | |
|
|
2169 | case '\\': |
|
|
2170 | skipspace (SCHEME_A); |
|
|
2171 | break; |
|
|
2172 | |
|
|
2173 | //TODO: x should end in ;, not two-digit hex |
2179 | case 'x': |
2174 | case 'x': |
2180 | case 'X': |
2175 | case 'X': |
2181 | state = st_x1; |
2176 | state = st_x1; |
2182 | c1 = 0; |
2177 | c1 = 0; |
2183 | break; |
2178 | break; |
2184 | |
2179 | |
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: |
2180 | default: |
2206 | *p++ = c; |
2181 | *p++ = c; |
2207 | state = st_ok; |
2182 | state = st_ok; |
2208 | break; |
2183 | break; |
2209 | } |
2184 | } |
2210 | |
2185 | |
2211 | break; |
2186 | break; |
2212 | |
2187 | |
2213 | case st_x1: |
2188 | case st_x1: |
2214 | case st_x2: |
2189 | case st_x2: |
2215 | c = toupper (c); |
2190 | c = tolower (c); |
2216 | |
2191 | |
2217 | if (c >= '0' && c <= 'F') |
2192 | if (c >= '0' && c <= '9') |
2218 | { |
|
|
2219 | if (c <= '9') |
|
|
2220 | c1 = (c1 << 4) + c - '0'; |
2193 | c1 = (c1 << 4) + c - '0'; |
2221 | else |
2194 | else if (c >= 'a' && c <= 'f') |
2222 | c1 = (c1 << 4) + c - 'A' + 10; |
2195 | 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 |
2196 | else |
2233 | return S_F; |
2197 | return S_F; |
|
|
2198 | |
|
|
2199 | if (state == st_x1) |
|
|
2200 | state = st_x2; |
|
|
2201 | else |
|
|
2202 | { |
|
|
2203 | *p++ = c1; |
|
|
2204 | state = st_ok; |
|
|
2205 | } |
2234 | |
2206 | |
2235 | break; |
2207 | break; |
2236 | |
2208 | |
2237 | case st_oct1: |
2209 | case st_oct1: |
2238 | case st_oct2: |
2210 | case st_oct2: |
… | |
… | |
2242 | backchar (SCHEME_A_ c); |
2214 | backchar (SCHEME_A_ c); |
2243 | state = st_ok; |
2215 | state = st_ok; |
2244 | } |
2216 | } |
2245 | else |
2217 | else |
2246 | { |
2218 | { |
2247 | if (state == st_oct2 && c1 >= 32) |
2219 | if (state == st_oct2 && c1 >= ' ') |
2248 | return S_F; |
2220 | return S_F; |
2249 | |
2221 | |
2250 | c1 = (c1 << 3) + (c - '0'); |
2222 | c1 = (c1 << 3) + (c - '0'); |
2251 | |
2223 | |
2252 | if (state == st_oct1) |
2224 | if (state == st_oct1) |
… | |
… | |
2257 | state = st_ok; |
2229 | state = st_ok; |
2258 | } |
2230 | } |
2259 | } |
2231 | } |
2260 | |
2232 | |
2261 | break; |
2233 | break; |
2262 | |
|
|
2263 | } |
2234 | } |
2264 | } |
2235 | } |
2265 | } |
2236 | } |
2266 | |
2237 | |
2267 | /* check c is in chars */ |
2238 | /* check c is in chars */ |
2268 | ecb_inline int |
2239 | ecb_cold int |
2269 | is_one_of (char *s, int c) |
2240 | is_one_of (const char *s, int c) |
2270 | { |
2241 | { |
2271 | if (c == EOF) |
|
|
2272 | return 1; |
|
|
2273 | |
|
|
2274 | return !!strchr (s, c); |
2242 | return c == EOF || !!strchr (s, c); |
2275 | } |
2243 | } |
2276 | |
2244 | |
2277 | /* skip white characters */ |
2245 | /* skip white characters */ |
2278 | ecb_inline int |
2246 | ecb_cold int |
2279 | skipspace (SCHEME_P) |
2247 | skipspace (SCHEME_P) |
2280 | { |
2248 | { |
2281 | int c, curr_line = 0; |
2249 | int c, curr_line = 0; |
2282 | |
2250 | |
2283 | do |
2251 | do |
2284 | { |
2252 | { |
2285 | c = inchar (SCHEME_A); |
2253 | c = inchar (SCHEME_A); |
|
|
2254 | |
2286 | #if SHOW_ERROR_LINE |
2255 | #if SHOW_ERROR_LINE |
2287 | if (c == '\n') |
2256 | if (ecb_expect_false (c == '\n')) |
2288 | curr_line++; |
2257 | curr_line++; |
2289 | #endif |
2258 | #endif |
|
|
2259 | |
|
|
2260 | if (ecb_expect_false (c == EOF)) |
|
|
2261 | return c; |
2290 | } |
2262 | } |
2291 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2263 | while (is_one_of (WHITESPACE, c)); |
2292 | |
2264 | |
2293 | /* record it */ |
2265 | /* record it */ |
2294 | #if SHOW_ERROR_LINE |
2266 | #if SHOW_ERROR_LINE |
2295 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2267 | 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; |
2268 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2297 | #endif |
2269 | #endif |
2298 | |
2270 | |
2299 | if (c != EOF) |
|
|
2300 | { |
|
|
2301 | backchar (SCHEME_A_ c); |
2271 | backchar (SCHEME_A_ c); |
2302 | return 1; |
2272 | return 1; |
2303 | } |
|
|
2304 | else |
|
|
2305 | return EOF; |
|
|
2306 | } |
2273 | } |
2307 | |
2274 | |
2308 | /* get token */ |
2275 | /* get token */ |
2309 | static int |
2276 | ecb_cold static int |
2310 | token (SCHEME_P) |
2277 | token (SCHEME_P) |
2311 | { |
2278 | { |
2312 | int c = skipspace (SCHEME_A); |
2279 | int c = skipspace (SCHEME_A); |
2313 | |
2280 | |
2314 | if (c == EOF) |
2281 | if (c == EOF) |
… | |
… | |
2326 | return TOK_RPAREN; |
2293 | return TOK_RPAREN; |
2327 | |
2294 | |
2328 | case '.': |
2295 | case '.': |
2329 | c = inchar (SCHEME_A); |
2296 | c = inchar (SCHEME_A); |
2330 | |
2297 | |
2331 | if (is_one_of (" \n\t", c)) |
2298 | if (is_one_of (WHITESPACE, c)) |
2332 | return TOK_DOT; |
2299 | return TOK_DOT; |
2333 | else |
2300 | else |
2334 | { |
2301 | { |
2335 | //TODO: ungetc twice in a row is not supported in C |
|
|
2336 | backchar (SCHEME_A_ c); |
2302 | backchar (SCHEME_A_ c); |
2337 | backchar (SCHEME_A_ '.'); |
|
|
2338 | return TOK_ATOM; |
2303 | return TOK_DOTATOM; |
2339 | } |
2304 | } |
|
|
2305 | |
|
|
2306 | case '|': |
|
|
2307 | return TOK_STRATOM; |
2340 | |
2308 | |
2341 | case '\'': |
2309 | case '\'': |
2342 | return TOK_QUOTE; |
2310 | return TOK_QUOTE; |
2343 | |
2311 | |
2344 | case ';': |
2312 | case ';': |
… | |
… | |
2411 | } |
2379 | } |
2412 | |
2380 | |
2413 | /* ========== Routines for Printing ========== */ |
2381 | /* ========== Routines for Printing ========== */ |
2414 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2382 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2415 | |
2383 | |
2416 | static void |
2384 | ecb_cold static void |
2417 | printslashstring (SCHEME_P_ char *p, int len) |
2385 | printslashstring (SCHEME_P_ char *p, int len) |
2418 | { |
2386 | { |
2419 | int i; |
2387 | int i; |
2420 | unsigned char *s = (unsigned char *) p; |
2388 | unsigned char *s = (unsigned char *) p; |
2421 | |
2389 | |
… | |
… | |
2476 | } |
2444 | } |
2477 | |
2445 | |
2478 | putcharacter (SCHEME_A_ '"'); |
2446 | putcharacter (SCHEME_A_ '"'); |
2479 | } |
2447 | } |
2480 | |
2448 | |
2481 | |
|
|
2482 | /* print atoms */ |
2449 | /* print atoms */ |
2483 | static void |
2450 | ecb_cold static void |
2484 | printatom (SCHEME_P_ pointer l, int f) |
2451 | printatom (SCHEME_P_ pointer l, int f) |
2485 | { |
2452 | { |
2486 | char *p; |
2453 | char *p; |
2487 | int len; |
2454 | int len; |
2488 | |
2455 | |
2489 | atom2str (SCHEME_A_ l, f, &p, &len); |
2456 | atom2str (SCHEME_A_ l, f, &p, &len); |
2490 | putchars (SCHEME_A_ p, len); |
2457 | putchars (SCHEME_A_ p, len); |
2491 | } |
2458 | } |
2492 | |
2459 | |
2493 | |
|
|
2494 | /* Uses internal buffer unless string pointer is already available */ |
2460 | /* Uses internal buffer unless string pointer is already available */ |
2495 | static void |
2461 | ecb_cold static void |
2496 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2462 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2497 | { |
2463 | { |
2498 | char *p; |
2464 | char *p; |
2499 | |
2465 | |
2500 | if (l == NIL) |
2466 | if (l == NIL) |
… | |
… | |
2658 | #endif |
2624 | #endif |
2659 | } |
2625 | } |
2660 | else if (is_continuation (l)) |
2626 | else if (is_continuation (l)) |
2661 | p = "#<CONTINUATION>"; |
2627 | p = "#<CONTINUATION>"; |
2662 | else |
2628 | else |
|
|
2629 | { |
|
|
2630 | #if USE_PRINTF |
|
|
2631 | p = SCHEME_V->strbuff; |
|
|
2632 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2633 | #else |
2663 | p = "#<ERROR>"; |
2634 | p = "#<ERROR>"; |
|
|
2635 | #endif |
|
|
2636 | } |
2664 | |
2637 | |
2665 | *pp = p; |
2638 | *pp = p; |
2666 | *plen = strlen (p); |
2639 | *plen = strlen (p); |
2667 | } |
2640 | } |
2668 | |
2641 | |
… | |
… | |
2700 | return car (d); |
2673 | return car (d); |
2701 | |
2674 | |
2702 | p = cons (car (d), cdr (d)); |
2675 | p = cons (car (d), cdr (d)); |
2703 | q = p; |
2676 | q = p; |
2704 | |
2677 | |
2705 | while (cdr (cdr (p)) != NIL) |
2678 | while (cddr (p) != NIL) |
2706 | { |
2679 | { |
2707 | d = cons (car (p), cdr (p)); |
2680 | d = cons (car (p), cdr (p)); |
2708 | |
2681 | |
2709 | if (cdr (cdr (p)) != NIL) |
2682 | if (cddr (p) != NIL) |
2710 | p = cdr (d); |
2683 | p = cdr (d); |
2711 | } |
2684 | } |
2712 | |
2685 | |
2713 | set_cdr (p, car (cdr (p))); |
2686 | set_cdr (p, cadr (p)); |
2714 | return q; |
2687 | return q; |
2715 | } |
2688 | } |
2716 | |
2689 | |
2717 | /* reverse list -- produce new list */ |
2690 | /* reverse list -- produce new list */ |
2718 | static pointer |
2691 | ecb_hot static pointer |
2719 | reverse (SCHEME_P_ pointer a) |
2692 | reverse (SCHEME_P_ pointer a) |
2720 | { |
2693 | { |
2721 | /* a must be checked by gc */ |
2694 | /* a must be checked by gc */ |
2722 | pointer p = NIL; |
2695 | pointer p = NIL; |
2723 | |
2696 | |
… | |
… | |
2726 | |
2699 | |
2727 | return p; |
2700 | return p; |
2728 | } |
2701 | } |
2729 | |
2702 | |
2730 | /* reverse list --- in-place */ |
2703 | /* reverse list --- in-place */ |
2731 | static pointer |
2704 | ecb_hot static pointer |
2732 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2705 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2733 | { |
2706 | { |
2734 | pointer result = term; |
2707 | pointer result = term; |
2735 | pointer p = list; |
2708 | pointer p = list; |
2736 | |
2709 | |
… | |
… | |
2744 | |
2717 | |
2745 | return result; |
2718 | return result; |
2746 | } |
2719 | } |
2747 | |
2720 | |
2748 | /* append list -- produce new list (in reverse order) */ |
2721 | /* append list -- produce new list (in reverse order) */ |
2749 | static pointer |
2722 | ecb_hot static pointer |
2750 | revappend (SCHEME_P_ pointer a, pointer b) |
2723 | revappend (SCHEME_P_ pointer a, pointer b) |
2751 | { |
2724 | { |
2752 | pointer result = a; |
2725 | pointer result = a; |
2753 | pointer p = b; |
2726 | pointer p = b; |
2754 | |
2727 | |
… | |
… | |
2763 | |
2736 | |
2764 | return S_F; /* signal an error */ |
2737 | return S_F; /* signal an error */ |
2765 | } |
2738 | } |
2766 | |
2739 | |
2767 | /* equivalence of atoms */ |
2740 | /* equivalence of atoms */ |
2768 | int |
2741 | ecb_hot int |
2769 | eqv (pointer a, pointer b) |
2742 | eqv (pointer a, pointer b) |
2770 | { |
2743 | { |
2771 | if (is_string (a)) |
2744 | if (is_string (a)) |
2772 | { |
2745 | { |
2773 | if (is_string (b)) |
2746 | if (is_string (b)) |
… | |
… | |
2812 | /* () is #t in R5RS */ |
2785 | /* () is #t in R5RS */ |
2813 | #define is_true(p) ((p) != S_F) |
2786 | #define is_true(p) ((p) != S_F) |
2814 | #define is_false(p) ((p) == S_F) |
2787 | #define is_false(p) ((p) == S_F) |
2815 | |
2788 | |
2816 | /* ========== Environment implementation ========== */ |
2789 | /* ========== 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 | |
2790 | |
2833 | #ifndef USE_ALIST_ENV |
2791 | #ifndef USE_ALIST_ENV |
2834 | |
2792 | |
2835 | /* |
2793 | /* |
2836 | * In this implementation, each frame of the environment may be |
2794 | * In this implementation, each frame of the environment may be |
… | |
… | |
2853 | |
2811 | |
2854 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2812 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2855 | setenvironment (SCHEME_V->envir); |
2813 | setenvironment (SCHEME_V->envir); |
2856 | } |
2814 | } |
2857 | |
2815 | |
|
|
2816 | static uint32_t |
|
|
2817 | sym_hash (pointer sym, uint32_t size) |
|
|
2818 | { |
|
|
2819 | uintptr_t ptr = (uintptr_t)sym; |
|
|
2820 | |
|
|
2821 | #if 0 |
|
|
2822 | /* table size is prime, so why mix */ |
|
|
2823 | ptr += ptr >> 32; |
|
|
2824 | ptr += ptr >> 16; |
|
|
2825 | ptr += ptr >> 8; |
|
|
2826 | #endif |
|
|
2827 | |
|
|
2828 | return ptr % size; |
|
|
2829 | } |
|
|
2830 | |
2858 | ecb_inline void |
2831 | ecb_inline void |
2859 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2832 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2860 | { |
2833 | { |
2861 | pointer slot = immutable_cons (variable, value); |
2834 | pointer slot = immutable_cons (variable, value); |
2862 | |
2835 | |
2863 | if (is_vector (car (env))) |
2836 | if (is_vector (car (env))) |
2864 | { |
2837 | { |
2865 | int location = hash_fn (symname (variable), veclength (car (env))); |
2838 | int location = sym_hash (variable, veclength (car (env))); |
2866 | |
|
|
2867 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2839 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2868 | } |
2840 | } |
2869 | else |
2841 | else |
2870 | set_car (env, immutable_cons (slot, car (env))); |
2842 | set_car (env, immutable_cons (slot, car (env))); |
2871 | } |
2843 | } |
2872 | |
2844 | |
2873 | static pointer |
2845 | ecb_hot static pointer |
2874 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2846 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2875 | { |
2847 | { |
2876 | pointer x, y; |
2848 | pointer x, y; |
2877 | int location; |
|
|
2878 | |
2849 | |
2879 | for (x = env; x != NIL; x = cdr (x)) |
2850 | for (x = env; x != NIL; x = cdr (x)) |
2880 | { |
2851 | { |
2881 | if (is_vector (car (x))) |
2852 | if (is_vector (car (x))) |
2882 | { |
2853 | { |
2883 | location = hash_fn (symname (hdl), veclength (car (x))); |
2854 | int location = sym_hash (hdl, veclength (car (x))); |
2884 | y = vector_get (car (x), location); |
2855 | y = vector_get (car (x), location); |
2885 | } |
2856 | } |
2886 | else |
2857 | else |
2887 | y = car (x); |
2858 | y = car (x); |
2888 | |
2859 | |
… | |
… | |
2900 | return NIL; |
2871 | return NIL; |
2901 | } |
2872 | } |
2902 | |
2873 | |
2903 | #else /* USE_ALIST_ENV */ |
2874 | #else /* USE_ALIST_ENV */ |
2904 | |
2875 | |
2905 | ecb_inline void |
2876 | static void |
2906 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2877 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2907 | { |
2878 | { |
2908 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2879 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2909 | setenvironment (SCHEME_V->envir); |
2880 | setenvironment (SCHEME_V->envir); |
2910 | } |
2881 | } |
2911 | |
2882 | |
2912 | ecb_inline void |
2883 | static void |
2913 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2884 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2914 | { |
2885 | { |
2915 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2886 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2916 | } |
2887 | } |
2917 | |
2888 | |
2918 | static pointer |
2889 | ecb_hot static pointer |
2919 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2890 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2920 | { |
2891 | { |
2921 | pointer x, y; |
2892 | pointer x, y; |
2922 | |
2893 | |
2923 | for (x = env; x != NIL; x = cdr (x)) |
2894 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2925 | for (y = car (x); y != NIL; y = cdr (y)) |
2896 | for (y = car (x); y != NIL; y = cdr (y)) |
2926 | if (caar (y) == hdl) |
2897 | if (caar (y) == hdl) |
2927 | break; |
2898 | break; |
2928 | |
2899 | |
2929 | if (y != NIL) |
2900 | if (y != NIL) |
|
|
2901 | return car (y); |
2930 | break; |
2902 | break; |
2931 | |
2903 | |
2932 | if (!all) |
2904 | if (!all) |
2933 | return NIL; |
2905 | break; |
2934 | } |
2906 | } |
2935 | |
|
|
2936 | if (x != NIL) |
|
|
2937 | return car (y); |
|
|
2938 | |
2907 | |
2939 | return NIL; |
2908 | return NIL; |
2940 | } |
2909 | } |
2941 | |
2910 | |
2942 | #endif /* USE_ALIST_ENV else */ |
2911 | #endif /* USE_ALIST_ENV else */ |
2943 | |
2912 | |
2944 | ecb_inline void |
2913 | static void |
2945 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2914 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2946 | { |
2915 | { |
|
|
2916 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2947 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2917 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2948 | } |
2918 | } |
2949 | |
2919 | |
2950 | ecb_inline void |
2920 | static void |
2951 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2921 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2952 | { |
2922 | { |
2953 | set_cdr (slot, value); |
2923 | set_cdr (slot, value); |
2954 | } |
2924 | } |
2955 | |
2925 | |
2956 | ecb_inline pointer |
2926 | static pointer |
2957 | slot_value_in_env (pointer slot) |
2927 | slot_value_in_env (pointer slot) |
2958 | { |
2928 | { |
2959 | return cdr (slot); |
2929 | return cdr (slot); |
2960 | } |
2930 | } |
2961 | |
2931 | |
2962 | /* ========== Evaluation Cycle ========== */ |
2932 | /* ========== Evaluation Cycle ========== */ |
2963 | |
2933 | |
2964 | static int |
2934 | ecb_cold static int |
2965 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2935 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2966 | { |
2936 | { |
2967 | #if USE_ERROR_HOOK |
2937 | #if USE_ERROR_HOOK |
2968 | pointer x; |
2938 | pointer x; |
2969 | pointer hdl = SCHEME_V->ERROR_HOOK; |
2939 | pointer hdl = SCHEME_V->ERROR_HOOK; |
… | |
… | |
3045 | pointer code; |
3015 | pointer code; |
3046 | }; |
3016 | }; |
3047 | |
3017 | |
3048 | # define STACK_GROWTH 3 |
3018 | # define STACK_GROWTH 3 |
3049 | |
3019 | |
3050 | static void |
3020 | ecb_hot static void |
3051 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3021 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3052 | { |
3022 | { |
3053 | int nframes = (uintptr_t)SCHEME_V->dump; |
3023 | int nframes = (uintptr_t)SCHEME_V->dump; |
3054 | struct dump_stack_frame *next_frame; |
3024 | struct dump_stack_frame *next_frame; |
3055 | |
3025 | |
3056 | /* enough room for the next frame? */ |
3026 | /* enough room for the next frame? */ |
3057 | if (nframes >= SCHEME_V->dump_size) |
3027 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3058 | { |
3028 | { |
3059 | SCHEME_V->dump_size += STACK_GROWTH; |
3029 | 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); |
3030 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3061 | } |
3031 | } |
3062 | |
3032 | |
… | |
… | |
3068 | next_frame->code = code; |
3038 | next_frame->code = code; |
3069 | |
3039 | |
3070 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3040 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3071 | } |
3041 | } |
3072 | |
3042 | |
3073 | static int |
3043 | static ecb_hot int |
3074 | xs_return (SCHEME_P_ pointer a) |
3044 | xs_return (SCHEME_P_ pointer a) |
3075 | { |
3045 | { |
3076 | int nframes = (uintptr_t)SCHEME_V->dump; |
3046 | int nframes = (uintptr_t)SCHEME_V->dump; |
3077 | struct dump_stack_frame *frame; |
3047 | struct dump_stack_frame *frame; |
3078 | |
3048 | |
… | |
… | |
3089 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3059 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3090 | |
3060 | |
3091 | return 0; |
3061 | return 0; |
3092 | } |
3062 | } |
3093 | |
3063 | |
3094 | ecb_inline void |
3064 | ecb_cold void |
3095 | dump_stack_reset (SCHEME_P) |
3065 | dump_stack_reset (SCHEME_P) |
3096 | { |
3066 | { |
3097 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3067 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3098 | SCHEME_V->dump = (pointer)+0; |
3068 | SCHEME_V->dump = (pointer)+0; |
3099 | } |
3069 | } |
3100 | |
3070 | |
3101 | ecb_inline void |
3071 | ecb_cold void |
3102 | dump_stack_initialize (SCHEME_P) |
3072 | dump_stack_initialize (SCHEME_P) |
3103 | { |
3073 | { |
3104 | SCHEME_V->dump_size = 0; |
3074 | SCHEME_V->dump_size = 0; |
3105 | SCHEME_V->dump_base = 0; |
3075 | SCHEME_V->dump_base = 0; |
3106 | dump_stack_reset (SCHEME_A); |
3076 | dump_stack_reset (SCHEME_A); |
3107 | } |
3077 | } |
3108 | |
3078 | |
3109 | static void |
3079 | ecb_cold static void |
3110 | dump_stack_free (SCHEME_P) |
3080 | dump_stack_free (SCHEME_P) |
3111 | { |
3081 | { |
3112 | free (SCHEME_V->dump_base); |
3082 | free (SCHEME_V->dump_base); |
3113 | SCHEME_V->dump_base = 0; |
3083 | SCHEME_V->dump_base = 0; |
3114 | SCHEME_V->dump = (pointer)0; |
3084 | SCHEME_V->dump = (pointer)0; |
3115 | SCHEME_V->dump_size = 0; |
3085 | SCHEME_V->dump_size = 0; |
3116 | } |
3086 | } |
3117 | |
3087 | |
3118 | static void |
3088 | ecb_cold static void |
3119 | dump_stack_mark (SCHEME_P) |
3089 | dump_stack_mark (SCHEME_P) |
3120 | { |
3090 | { |
3121 | int nframes = (uintptr_t)SCHEME_V->dump; |
3091 | int nframes = (uintptr_t)SCHEME_V->dump; |
3122 | int i; |
3092 | int i; |
3123 | |
3093 | |
… | |
… | |
3129 | mark (frame->envir); |
3099 | mark (frame->envir); |
3130 | mark (frame->code); |
3100 | mark (frame->code); |
3131 | } |
3101 | } |
3132 | } |
3102 | } |
3133 | |
3103 | |
3134 | static pointer |
3104 | ecb_cold static pointer |
3135 | ss_get_cont (SCHEME_P) |
3105 | ss_get_cont (SCHEME_P) |
3136 | { |
3106 | { |
3137 | int nframes = (uintptr_t)SCHEME_V->dump; |
3107 | int nframes = (uintptr_t)SCHEME_V->dump; |
3138 | int i; |
3108 | int i; |
3139 | |
3109 | |
… | |
… | |
3151 | } |
3121 | } |
3152 | |
3122 | |
3153 | return cont; |
3123 | return cont; |
3154 | } |
3124 | } |
3155 | |
3125 | |
3156 | static void |
3126 | ecb_cold static void |
3157 | ss_set_cont (SCHEME_P_ pointer cont) |
3127 | ss_set_cont (SCHEME_P_ pointer cont) |
3158 | { |
3128 | { |
3159 | int i = 0; |
3129 | int i = 0; |
3160 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3130 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3161 | |
3131 | |
… | |
… | |
3173 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3143 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3174 | } |
3144 | } |
3175 | |
3145 | |
3176 | #else |
3146 | #else |
3177 | |
3147 | |
3178 | ecb_inline void |
3148 | ecb_cold void |
3179 | dump_stack_reset (SCHEME_P) |
3149 | dump_stack_reset (SCHEME_P) |
3180 | { |
3150 | { |
3181 | SCHEME_V->dump = NIL; |
3151 | SCHEME_V->dump = NIL; |
3182 | } |
3152 | } |
3183 | |
3153 | |
3184 | ecb_inline void |
3154 | ecb_cold void |
3185 | dump_stack_initialize (SCHEME_P) |
3155 | dump_stack_initialize (SCHEME_P) |
3186 | { |
3156 | { |
3187 | dump_stack_reset (SCHEME_A); |
3157 | dump_stack_reset (SCHEME_A); |
3188 | } |
3158 | } |
3189 | |
3159 | |
3190 | static void |
3160 | ecb_cold static void |
3191 | dump_stack_free (SCHEME_P) |
3161 | dump_stack_free (SCHEME_P) |
3192 | { |
3162 | { |
3193 | SCHEME_V->dump = NIL; |
3163 | SCHEME_V->dump = NIL; |
3194 | } |
3164 | } |
3195 | |
3165 | |
3196 | static int |
3166 | ecb_hot static int |
3197 | xs_return (SCHEME_P_ pointer a) |
3167 | xs_return (SCHEME_P_ pointer a) |
3198 | { |
3168 | { |
3199 | pointer dump = SCHEME_V->dump; |
3169 | pointer dump = SCHEME_V->dump; |
3200 | |
3170 | |
3201 | SCHEME_V->value = a; |
3171 | SCHEME_V->value = a; |
… | |
… | |
3211 | SCHEME_V->dump = dump; |
3181 | SCHEME_V->dump = dump; |
3212 | |
3182 | |
3213 | return 0; |
3183 | return 0; |
3214 | } |
3184 | } |
3215 | |
3185 | |
3216 | static void |
3186 | ecb_hot static void |
3217 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3187 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3218 | { |
3188 | { |
3219 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3189 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3220 | cons (args, |
3190 | cons (args, |
3221 | cons (SCHEME_V->envir, |
3191 | cons (SCHEME_V->envir, |
3222 | cons (code, |
3192 | cons (code, |
3223 | SCHEME_V->dump)))); |
3193 | SCHEME_V->dump)))); |
3224 | } |
3194 | } |
3225 | |
3195 | |
|
|
3196 | ecb_cold static void |
|
|
3197 | dump_stack_mark (SCHEME_P) |
|
|
3198 | { |
|
|
3199 | mark (SCHEME_V->dump); |
|
|
3200 | } |
|
|
3201 | |
|
|
3202 | ecb_cold static pointer |
|
|
3203 | ss_get_cont (SCHEME_P) |
|
|
3204 | { |
|
|
3205 | return SCHEME_V->dump; |
|
|
3206 | } |
|
|
3207 | |
|
|
3208 | ecb_cold static void |
|
|
3209 | ss_set_cont (SCHEME_P_ pointer cont) |
|
|
3210 | { |
|
|
3211 | SCHEME_V->dump = cont; |
|
|
3212 | } |
|
|
3213 | |
|
|
3214 | #endif |
|
|
3215 | |
|
|
3216 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
|
|
3217 | |
|
|
3218 | #if EXPERIMENT |
|
|
3219 | |
|
|
3220 | typedef void *stream[1]; |
|
|
3221 | |
|
|
3222 | #define stream_init() { 0 } |
|
|
3223 | |
|
|
3224 | ecb_cold static void |
|
|
3225 | stream_put (void **s, uint8_t byte) |
|
|
3226 | { |
|
|
3227 | uint32_t *sp = *s; |
|
|
3228 | uint32_t size = sizeof (uint32_t) * 2; |
|
|
3229 | uint32_t offs = size; |
|
|
3230 | |
|
|
3231 | if (ecb_expect_true (sp)) |
|
|
3232 | { |
|
|
3233 | offs = sp[0]; |
|
|
3234 | size = sp[1]; |
|
|
3235 | } |
|
|
3236 | |
|
|
3237 | if (ecb_expect_false (offs == size)) |
|
|
3238 | { |
|
|
3239 | size *= 2; |
|
|
3240 | sp = realloc (sp, size); |
|
|
3241 | *s = sp; |
|
|
3242 | sp[1] = size; |
|
|
3243 | |
|
|
3244 | } |
|
|
3245 | |
|
|
3246 | ((uint8_t *)sp)[offs++] = byte; |
|
|
3247 | sp[0] = offs; |
|
|
3248 | } |
|
|
3249 | |
|
|
3250 | #define stream_data(s) ((char *)(s)[0] + sizeof (uint32_t) * 2) |
|
|
3251 | #define stream_size(s) (((uint32_t *)(s)[0])[0] - sizeof (uint32_t) * 2) |
|
|
3252 | #define stream_free(s) free (s[0]) |
|
|
3253 | |
|
|
3254 | // calculates a (preferably small) integer that makes it possible to find |
|
|
3255 | // the symbol again. if pointers were offsets into a memory area... until |
|
|
3256 | // then, we return segment number in the low bits, and offset in the high |
|
|
3257 | // bits |
|
|
3258 | static uint32_t |
|
|
3259 | symbol_id (SCHEME_P_ pointer sym) |
|
|
3260 | { |
|
|
3261 | struct cell *p = CELL (sym); |
|
|
3262 | int i; |
|
|
3263 | |
|
|
3264 | for (i = SCHEME_V->last_cell_seg; i >= 0; --i) |
|
|
3265 | if (SCHEME_V->cell_seg[i] <= p && p < SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize[i]) |
|
|
3266 | { |
|
|
3267 | printf ("seg %d ofs %d/%d\n",i,(p - SCHEME_V->cell_seg[i]),SCHEME_V->cell_segsize[i]);//D |
|
|
3268 | return i | ((p - SCHEME_V->cell_seg[i]) << CELL_NSEGMENT_LOG); |
|
|
3269 | } |
|
|
3270 | |
|
|
3271 | abort (); |
|
|
3272 | } |
|
|
3273 | |
3226 | static void |
3274 | static void |
3227 | dump_stack_mark (SCHEME_P) |
3275 | compile (SCHEME_P_ stream s, pointer x) |
3228 | { |
3276 | { |
3229 | mark (SCHEME_V->dump); |
3277 | if (x == NIL) |
3230 | } |
3278 | { |
|
|
3279 | stream_put (s, 0); |
|
|
3280 | return; |
|
|
3281 | } |
3231 | |
3282 | |
3232 | static pointer |
3283 | if (is_syntax (x)) |
3233 | ss_get_cont (SCHEME_P) |
3284 | { |
3234 | { |
3285 | stream_put (s, 1); |
3235 | return SCHEME_V->dump; |
3286 | stream_put (s, syntaxnum (x)); |
3236 | } |
3287 | return; |
|
|
3288 | } |
3237 | |
3289 | |
3238 | static void |
3290 | switch (type (x)) |
3239 | ss_set_cont (SCHEME_P_ pointer cont) |
3291 | { |
3240 | { |
3292 | case T_INTEGER: |
3241 | SCHEME_V->dump = cont; |
3293 | stream_put (s, 2); |
3242 | } |
3294 | stream_put (s, 0); |
|
|
3295 | stream_put (s, 0); |
|
|
3296 | stream_put (s, 0); |
|
|
3297 | stream_put (s, 0); |
|
|
3298 | return; |
3243 | |
3299 | |
3244 | #endif |
3300 | case T_SYMBOL: |
|
|
3301 | { |
|
|
3302 | uint32_t sym = symbol_id (SCHEME_A_ x); |
|
|
3303 | printf ("sym %x\n", sym);//D |
3245 | |
3304 | |
3246 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3305 | stream_put (s, 3); |
|
|
3306 | |
|
|
3307 | while (sym > 0x7f) |
|
|
3308 | { |
|
|
3309 | stream_put (s, sym | 0x80); |
|
|
3310 | sym >>= 8; |
|
|
3311 | } |
|
|
3312 | |
|
|
3313 | stream_put (s, sym); |
|
|
3314 | } |
|
|
3315 | return; |
|
|
3316 | |
|
|
3317 | case T_PAIR: |
|
|
3318 | stream_put (s, 4); |
|
|
3319 | while (x != NIL) |
|
|
3320 | { |
|
|
3321 | compile (SCHEME_A_ s, car (x)); |
|
|
3322 | x = cdr (x); |
|
|
3323 | } |
|
|
3324 | stream_put (s, 0xff); |
|
|
3325 | return; |
|
|
3326 | |
|
|
3327 | default: |
|
|
3328 | stream_put (s, 5); |
|
|
3329 | stream_put (s, type (x)); |
|
|
3330 | stream_put (s, 0); |
|
|
3331 | stream_put (s, 0); |
|
|
3332 | stream_put (s, 0); |
|
|
3333 | stream_put (s, 0); |
|
|
3334 | break; |
|
|
3335 | } |
|
|
3336 | } |
3247 | |
3337 | |
3248 | static int |
3338 | static int |
|
|
3339 | compile_closure (SCHEME_P_ pointer p) |
|
|
3340 | { |
|
|
3341 | stream s = stream_init (); |
|
|
3342 | |
|
|
3343 | printatom (SCHEME_A_ p, 1);//D |
|
|
3344 | compile (SCHEME_A_ s, car (p)); |
|
|
3345 | |
|
|
3346 | FILE *xxd = popen ("xxd", "we"); |
|
|
3347 | fwrite (stream_data (s), 1, stream_size (s), xxd); |
|
|
3348 | fclose (xxd); |
|
|
3349 | |
|
|
3350 | return stream_size (s); |
|
|
3351 | } |
|
|
3352 | |
|
|
3353 | static int |
|
|
3354 | dtree (SCHEME_P_ int indent, pointer x) |
|
|
3355 | { |
|
|
3356 | int c; |
|
|
3357 | |
|
|
3358 | if (is_syntax (x)) |
|
|
3359 | { |
|
|
3360 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3361 | return 8 + 8; |
|
|
3362 | } |
|
|
3363 | |
|
|
3364 | if (x == NIL) |
|
|
3365 | { |
|
|
3366 | printf ("%*sNIL\n", indent, ""); |
|
|
3367 | return 3; |
|
|
3368 | } |
|
|
3369 | |
|
|
3370 | switch (type (x)) |
|
|
3371 | { |
|
|
3372 | case T_INTEGER: |
|
|
3373 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3374 | return 32+8; |
|
|
3375 | |
|
|
3376 | case T_SYMBOL: |
|
|
3377 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3378 | return 24+8; |
|
|
3379 | |
|
|
3380 | case T_CLOSURE: |
|
|
3381 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3382 | dtree (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3383 | return 32 + dtree (SCHEME_A_ indent + 3, car (x)); |
|
|
3384 | |
|
|
3385 | case T_PAIR: |
|
|
3386 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3387 | c = dtree (SCHEME_A_ indent + 3, car (x)); |
|
|
3388 | c += dtree (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3389 | return c + 1; |
|
|
3390 | |
|
|
3391 | case T_PORT: |
|
|
3392 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3393 | return 24+8; |
|
|
3394 | |
|
|
3395 | case T_VECTOR: |
|
|
3396 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3397 | return 24+8; |
|
|
3398 | |
|
|
3399 | case T_ENVIRONMENT: |
|
|
3400 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3401 | return 0 + dtree (SCHEME_A_ indent + 3, car (x)); |
|
|
3402 | |
|
|
3403 | default: |
|
|
3404 | printf ("unhandled type %d\n", type (x)); |
|
|
3405 | break; |
|
|
3406 | } |
|
|
3407 | } |
|
|
3408 | #endif |
|
|
3409 | |
|
|
3410 | /* syntax, eval, core, ... */ |
|
|
3411 | ecb_hot static int |
3249 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3412 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3250 | { |
3413 | { |
3251 | pointer args = SCHEME_V->args; |
3414 | pointer args = SCHEME_V->args; |
3252 | pointer x, y; |
3415 | pointer x, y; |
3253 | |
3416 | |
3254 | switch (op) |
3417 | switch (op) |
3255 | { |
3418 | { |
|
|
3419 | #if EXPERIMENT //D |
|
|
3420 | case OP_DEBUG: |
|
|
3421 | { |
|
|
3422 | uint32_t len = compile_closure (SCHEME_A_ car (args)); |
|
|
3423 | printf ("len = %d\n", len); |
|
|
3424 | printf ("\n"); |
|
|
3425 | s_return (S_T); |
|
|
3426 | } |
|
|
3427 | #endif |
3256 | case OP_LOAD: /* load */ |
3428 | case OP_LOAD: /* load */ |
3257 | if (file_interactive (SCHEME_A)) |
3429 | if (file_interactive (SCHEME_A)) |
3258 | { |
3430 | { |
3259 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3431 | putstr (SCHEME_A_ "Loading "); |
3260 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3432 | putstr (SCHEME_A_ strvalue (car (args))); |
|
|
3433 | putcharacter (SCHEME_A_ '\n'); |
3261 | } |
3434 | } |
3262 | |
3435 | |
3263 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3436 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3264 | Error_1 ("unable to open", car (args)); |
3437 | Error_1 ("unable to open", car (args)); |
3265 | else |
3438 | |
3266 | { |
|
|
3267 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3439 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3268 | s_goto (OP_T0LVL); |
3440 | s_goto (OP_T0LVL); |
3269 | } |
|
|
3270 | |
3441 | |
3271 | case OP_T0LVL: /* top level */ |
3442 | case OP_T0LVL: /* top level */ |
3272 | |
3443 | |
3273 | /* If we reached the end of file, this loop is done. */ |
3444 | /* If we reached the end of file, this loop is done. */ |
3274 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3445 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3275 | { |
3446 | { |
3276 | if (SCHEME_V->file_i == 0) |
3447 | if (SCHEME_V->file_i == 0) |
3277 | { |
3448 | { |
3278 | SCHEME_V->args = NIL; |
3449 | SCHEME_V->args = NIL; |
3279 | s_goto (OP_QUIT); |
3450 | s_goto (OP_QUIT); |
… | |
… | |
3290 | /* If interactive, be nice to user. */ |
3461 | /* If interactive, be nice to user. */ |
3291 | if (file_interactive (SCHEME_A)) |
3462 | if (file_interactive (SCHEME_A)) |
3292 | { |
3463 | { |
3293 | SCHEME_V->envir = SCHEME_V->global_env; |
3464 | SCHEME_V->envir = SCHEME_V->global_env; |
3294 | dump_stack_reset (SCHEME_A); |
3465 | dump_stack_reset (SCHEME_A); |
3295 | putstr (SCHEME_A_ "\n"); |
3466 | putcharacter (SCHEME_A_ '\n'); |
3296 | putstr (SCHEME_A_ prompt); |
3467 | putstr (SCHEME_A_ prompt); |
3297 | } |
3468 | } |
3298 | |
3469 | |
3299 | /* Set up another iteration of REPL */ |
3470 | /* Set up another iteration of REPL */ |
3300 | SCHEME_V->nesting = 0; |
3471 | SCHEME_V->nesting = 0; |
… | |
… | |
3335 | { |
3506 | { |
3336 | SCHEME_V->print_flag = 1; |
3507 | SCHEME_V->print_flag = 1; |
3337 | SCHEME_V->args = SCHEME_V->value; |
3508 | SCHEME_V->args = SCHEME_V->value; |
3338 | s_goto (OP_P0LIST); |
3509 | s_goto (OP_P0LIST); |
3339 | } |
3510 | } |
3340 | else |
3511 | |
3341 | s_return (SCHEME_V->value); |
3512 | s_return (SCHEME_V->value); |
3342 | |
3513 | |
3343 | case OP_EVAL: /* main part of evaluation */ |
3514 | case OP_EVAL: /* main part of evaluation */ |
3344 | #if USE_TRACING |
3515 | #if USE_TRACING |
3345 | if (SCHEME_V->tracing) |
3516 | if (SCHEME_V->tracing) |
3346 | { |
3517 | { |
… | |
… | |
3357 | #endif |
3528 | #endif |
3358 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3529 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3359 | { |
3530 | { |
3360 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3531 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3361 | |
3532 | |
3362 | if (x != NIL) |
3533 | if (x == NIL) |
3363 | s_return (slot_value_in_env (x)); |
|
|
3364 | else |
|
|
3365 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3534 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3535 | |
|
|
3536 | s_return (slot_value_in_env (x)); |
3366 | } |
3537 | } |
3367 | else if (is_pair (SCHEME_V->code)) |
3538 | else if (is_pair (SCHEME_V->code)) |
3368 | { |
3539 | { |
3369 | x = car (SCHEME_V->code); |
3540 | x = car (SCHEME_V->code); |
3370 | |
3541 | |
… | |
… | |
3379 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3550 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3380 | SCHEME_V->code = x; |
3551 | SCHEME_V->code = x; |
3381 | s_goto (OP_EVAL); |
3552 | s_goto (OP_EVAL); |
3382 | } |
3553 | } |
3383 | } |
3554 | } |
3384 | else |
3555 | |
3385 | s_return (SCHEME_V->code); |
3556 | s_return (SCHEME_V->code); |
3386 | |
3557 | |
3387 | case OP_E0ARGS: /* eval arguments */ |
3558 | case OP_E0ARGS: /* eval arguments */ |
3388 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3559 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3389 | { |
3560 | { |
3390 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3561 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3391 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3562 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3392 | SCHEME_V->code = SCHEME_V->value; |
3563 | SCHEME_V->code = SCHEME_V->value; |
3393 | s_goto (OP_APPLY); |
3564 | s_goto (OP_APPLY); |
… | |
… | |
3447 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3618 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3448 | else if (is_foreign (SCHEME_V->code)) |
3619 | else if (is_foreign (SCHEME_V->code)) |
3449 | { |
3620 | { |
3450 | /* Keep nested calls from GC'ing the arglist */ |
3621 | /* Keep nested calls from GC'ing the arglist */ |
3451 | push_recent_alloc (SCHEME_A_ args, NIL); |
3622 | push_recent_alloc (SCHEME_A_ args, NIL); |
3452 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3623 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3453 | |
3624 | |
3454 | s_return (x); |
3625 | s_return (x); |
3455 | } |
3626 | } |
3456 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3627 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3457 | { |
3628 | { |
… | |
… | |
3494 | |
3665 | |
3495 | case OP_DOMACRO: /* do macro */ |
3666 | case OP_DOMACRO: /* do macro */ |
3496 | SCHEME_V->code = SCHEME_V->value; |
3667 | SCHEME_V->code = SCHEME_V->value; |
3497 | s_goto (OP_EVAL); |
3668 | s_goto (OP_EVAL); |
3498 | |
3669 | |
3499 | #if 1 |
|
|
3500 | |
|
|
3501 | case OP_LAMBDA: /* lambda */ |
3670 | case OP_LAMBDA: /* lambda */ |
3502 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3671 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3503 | set SCHEME_V->value fall thru */ |
3672 | set SCHEME_V->value fall thru */ |
3504 | { |
3673 | { |
3505 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3674 | 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); |
3680 | SCHEME_V->code = slot_value_in_env (f); |
3512 | s_goto (OP_APPLY); |
3681 | s_goto (OP_APPLY); |
3513 | } |
3682 | } |
3514 | |
3683 | |
3515 | SCHEME_V->value = SCHEME_V->code; |
3684 | SCHEME_V->value = SCHEME_V->code; |
3516 | /* Fallthru */ |
|
|
3517 | } |
3685 | } |
|
|
3686 | /* Fallthru */ |
3518 | |
3687 | |
3519 | case OP_LAMBDA1: |
3688 | case OP_LAMBDA1: |
3520 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3689 | 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 | |
3690 | |
3529 | case OP_MKCLOSURE: /* make-closure */ |
3691 | case OP_MKCLOSURE: /* make-closure */ |
3530 | x = car (args); |
3692 | x = car (args); |
3531 | |
3693 | |
3532 | if (car (x) == SCHEME_V->LAMBDA) |
3694 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
3571 | else |
3733 | else |
3572 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3734 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3573 | |
3735 | |
3574 | s_return (SCHEME_V->code); |
3736 | s_return (SCHEME_V->code); |
3575 | |
3737 | |
3576 | |
|
|
3577 | case OP_DEFP: /* defined? */ |
3738 | case OP_DEFP: /* defined? */ |
3578 | x = SCHEME_V->envir; |
3739 | x = SCHEME_V->envir; |
3579 | |
3740 | |
3580 | if (cdr (args) != NIL) |
3741 | if (cdr (args) != NIL) |
3581 | x = cadr (args); |
3742 | x = cadr (args); |
… | |
… | |
3598 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3759 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3599 | s_return (SCHEME_V->value); |
3760 | s_return (SCHEME_V->value); |
3600 | } |
3761 | } |
3601 | else |
3762 | else |
3602 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3763 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3603 | |
|
|
3604 | |
3764 | |
3605 | case OP_BEGIN: /* begin */ |
3765 | case OP_BEGIN: /* begin */ |
3606 | if (!is_pair (SCHEME_V->code)) |
3766 | if (!is_pair (SCHEME_V->code)) |
3607 | s_return (SCHEME_V->code); |
3767 | s_return (SCHEME_V->code); |
3608 | |
3768 | |
… | |
… | |
3620 | case OP_IF1: /* if */ |
3780 | case OP_IF1: /* if */ |
3621 | if (is_true (SCHEME_V->value)) |
3781 | if (is_true (SCHEME_V->value)) |
3622 | SCHEME_V->code = car (SCHEME_V->code); |
3782 | SCHEME_V->code = car (SCHEME_V->code); |
3623 | else |
3783 | else |
3624 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
3784 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
|
|
3785 | |
3625 | s_goto (OP_EVAL); |
3786 | s_goto (OP_EVAL); |
3626 | |
3787 | |
3627 | case OP_LET0: /* let */ |
3788 | case OP_LET0: /* let */ |
3628 | SCHEME_V->args = NIL; |
3789 | SCHEME_V->args = NIL; |
3629 | SCHEME_V->value = SCHEME_V->code; |
3790 | SCHEME_V->value = SCHEME_V->code; |
… | |
… | |
3785 | } |
3946 | } |
3786 | else |
3947 | else |
3787 | { |
3948 | { |
3788 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3949 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3789 | s_return (NIL); |
3950 | s_return (NIL); |
3790 | else |
3951 | |
3791 | { |
|
|
3792 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3952 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3793 | SCHEME_V->code = caar (SCHEME_V->code); |
3953 | SCHEME_V->code = caar (SCHEME_V->code); |
3794 | s_goto (OP_EVAL); |
3954 | s_goto (OP_EVAL); |
3795 | } |
|
|
3796 | } |
3955 | } |
3797 | |
3956 | |
3798 | case OP_DELAY: /* delay */ |
3957 | case OP_DELAY: /* delay */ |
3799 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3958 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3800 | set_typeflag (x, T_PROMISE); |
3959 | set_typeflag (x, T_PROMISE); |
… | |
… | |
3811 | case OP_AND1: /* and */ |
3970 | case OP_AND1: /* and */ |
3812 | if (is_false (SCHEME_V->value)) |
3971 | if (is_false (SCHEME_V->value)) |
3813 | s_return (SCHEME_V->value); |
3972 | s_return (SCHEME_V->value); |
3814 | else if (SCHEME_V->code == NIL) |
3973 | else if (SCHEME_V->code == NIL) |
3815 | s_return (SCHEME_V->value); |
3974 | s_return (SCHEME_V->value); |
3816 | else |
3975 | |
3817 | { |
|
|
3818 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3976 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3819 | SCHEME_V->code = car (SCHEME_V->code); |
3977 | SCHEME_V->code = car (SCHEME_V->code); |
3820 | s_goto (OP_EVAL); |
3978 | s_goto (OP_EVAL); |
3821 | } |
|
|
3822 | |
3979 | |
3823 | case OP_OR0: /* or */ |
3980 | case OP_OR0: /* or */ |
3824 | if (SCHEME_V->code == NIL) |
3981 | if (SCHEME_V->code == NIL) |
3825 | s_return (S_F); |
3982 | s_return (S_F); |
3826 | |
3983 | |
… | |
… | |
3831 | case OP_OR1: /* or */ |
3988 | case OP_OR1: /* or */ |
3832 | if (is_true (SCHEME_V->value)) |
3989 | if (is_true (SCHEME_V->value)) |
3833 | s_return (SCHEME_V->value); |
3990 | s_return (SCHEME_V->value); |
3834 | else if (SCHEME_V->code == NIL) |
3991 | else if (SCHEME_V->code == NIL) |
3835 | s_return (SCHEME_V->value); |
3992 | s_return (SCHEME_V->value); |
3836 | else |
3993 | |
3837 | { |
|
|
3838 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3994 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3839 | SCHEME_V->code = car (SCHEME_V->code); |
3995 | SCHEME_V->code = car (SCHEME_V->code); |
3840 | s_goto (OP_EVAL); |
3996 | s_goto (OP_EVAL); |
3841 | } |
|
|
3842 | |
3997 | |
3843 | case OP_C0STREAM: /* cons-stream */ |
3998 | case OP_C0STREAM: /* cons-stream */ |
3844 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3999 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3845 | SCHEME_V->code = car (SCHEME_V->code); |
4000 | SCHEME_V->code = car (SCHEME_V->code); |
3846 | s_goto (OP_EVAL); |
4001 | s_goto (OP_EVAL); |
… | |
… | |
3911 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
4066 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
3912 | SCHEME_V->code = caar (x); |
4067 | SCHEME_V->code = caar (x); |
3913 | s_goto (OP_EVAL); |
4068 | s_goto (OP_EVAL); |
3914 | } |
4069 | } |
3915 | } |
4070 | } |
3916 | else |
4071 | |
3917 | s_return (NIL); |
4072 | s_return (NIL); |
3918 | |
4073 | |
3919 | case OP_CASE2: /* case */ |
4074 | case OP_CASE2: /* case */ |
3920 | if (is_true (SCHEME_V->value)) |
4075 | if (is_true (SCHEME_V->value)) |
3921 | s_goto (OP_BEGIN); |
4076 | s_goto (OP_BEGIN); |
3922 | else |
4077 | |
3923 | s_return (NIL); |
4078 | s_return (NIL); |
3924 | |
4079 | |
3925 | case OP_PAPPLY: /* apply */ |
4080 | case OP_PAPPLY: /* apply */ |
3926 | SCHEME_V->code = car (args); |
4081 | SCHEME_V->code = car (args); |
3927 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
4082 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
3928 | /*SCHEME_V->args = cadr(args); */ |
4083 | /*SCHEME_V->args = cadr(args); */ |
… | |
… | |
3942 | } |
4097 | } |
3943 | |
4098 | |
3944 | if (USE_ERROR_CHECKING) abort (); |
4099 | if (USE_ERROR_CHECKING) abort (); |
3945 | } |
4100 | } |
3946 | |
4101 | |
3947 | static int |
4102 | /* math, cxr */ |
|
|
4103 | ecb_hot static int |
3948 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4104 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
3949 | { |
4105 | { |
3950 | pointer args = SCHEME_V->args; |
4106 | pointer args = SCHEME_V->args; |
3951 | pointer x = car (args); |
4107 | pointer x = car (args); |
3952 | num v; |
4108 | num v; |
3953 | |
4109 | |
3954 | switch (op) |
4110 | switch (op) |
3955 | { |
4111 | { |
3956 | #if USE_MATH |
4112 | #if USE_MATH |
3957 | case OP_INEX2EX: /* inexact->exact */ |
4113 | case OP_INEX2EX: /* inexact->exact */ |
3958 | { |
|
|
3959 | if (is_integer (x)) |
4114 | if (!is_integer (x)) |
3960 | s_return (x); |
4115 | { |
3961 | |
|
|
3962 | RVALUE r = rvalue_unchecked (x); |
4116 | RVALUE r = rvalue_unchecked (x); |
3963 | |
4117 | |
3964 | if (r == (RVALUE)(IVALUE)r) |
4118 | if (r == (RVALUE)(IVALUE)r) |
3965 | s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); |
4119 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
3966 | else |
4120 | else |
3967 | Error_1 ("inexact->exact: not integral:", x); |
4121 | Error_1 ("inexact->exact: not integral:", x); |
3968 | } |
4122 | } |
3969 | |
4123 | |
|
|
4124 | s_return (x); |
|
|
4125 | |
|
|
4126 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4127 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4128 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4129 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
|
|
4130 | |
|
|
4131 | 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)))); |
4132 | 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)))); |
4133 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
|
|
4134 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
3972 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4135 | 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)))); |
4136 | 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)))); |
4137 | 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)))); |
4138 | 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)))); |
4139 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
3977 | |
4140 | |
3978 | case OP_ATAN: |
4141 | case OP_ATAN: |
|
|
4142 | s_return (mk_real (SCHEME_A_ |
3979 | if (cdr (args) == NIL) |
4143 | cdr (args) == NIL |
3980 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4144 | ? atan (rvalue (x)) |
3981 | else |
4145 | : 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 | |
4146 | |
3990 | case OP_EXPT: |
4147 | case OP_EXPT: |
3991 | { |
4148 | { |
3992 | RVALUE result; |
4149 | RVALUE result; |
3993 | int real_result = 1; |
4150 | int real_result = 1; |
… | |
… | |
4016 | if (real_result) |
4173 | if (real_result) |
4017 | s_return (mk_real (SCHEME_A_ result)); |
4174 | s_return (mk_real (SCHEME_A_ result)); |
4018 | else |
4175 | else |
4019 | s_return (mk_integer (SCHEME_A_ result)); |
4176 | s_return (mk_integer (SCHEME_A_ result)); |
4020 | } |
4177 | } |
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 |
4178 | #endif |
4037 | |
4179 | |
4038 | case OP_ADD: /* + */ |
4180 | case OP_ADD: /* + */ |
4039 | v = num_zero; |
4181 | v = num_zero; |
4040 | |
4182 | |
… | |
… | |
4128 | else |
4270 | else |
4129 | Error_0 ("modulo: division by zero"); |
4271 | Error_0 ("modulo: division by zero"); |
4130 | |
4272 | |
4131 | s_return (mk_number (SCHEME_A_ v)); |
4273 | s_return (mk_number (SCHEME_A_ v)); |
4132 | |
4274 | |
4133 | case OP_CAR: /* car */ |
4275 | /* the compiler will optimize this mess... */ |
4134 | s_return (caar (args)); |
4276 | case OP_CAR: op_car: s_return (car (x)); |
4135 | |
4277 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4136 | case OP_CDR: /* cdr */ |
4278 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4137 | s_return (cdar (args)); |
4279 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4280 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4281 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4282 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4283 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4284 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4285 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4286 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4287 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4288 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4289 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4290 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4291 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4292 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4293 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4294 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4295 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4296 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4297 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4298 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4299 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4300 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4301 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4302 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4303 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4304 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4305 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4138 | |
4306 | |
4139 | case OP_CONS: /* cons */ |
4307 | case OP_CONS: /* cons */ |
4140 | set_cdr (args, cadr (args)); |
4308 | set_cdr (args, cadr (args)); |
4141 | s_return (args); |
4309 | s_return (args); |
4142 | |
4310 | |
… | |
… | |
4316 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4484 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4317 | |
4485 | |
4318 | s_return (newstr); |
4486 | s_return (newstr); |
4319 | } |
4487 | } |
4320 | |
4488 | |
4321 | case OP_SUBSTR: /* substring */ |
4489 | case OP_STRING_COPY: /* substring/string-copy */ |
4322 | { |
4490 | { |
4323 | char *str = strvalue (x); |
4491 | char *str = strvalue (x); |
4324 | int index0 = ivalue_unchecked (cadr (args)); |
4492 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4325 | int index1; |
4493 | int index1; |
4326 | int len; |
4494 | int len; |
4327 | |
4495 | |
4328 | if (index0 > strlength (x)) |
4496 | if (index0 > strlength (x)) |
4329 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4497 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4330 | |
4498 | |
4331 | if (cddr (args) != NIL) |
4499 | if (cddr (args) != NIL) |
4332 | { |
4500 | { |
4333 | index1 = ivalue_unchecked (caddr (args)); |
4501 | index1 = ivalue_unchecked (caddr (args)); |
4334 | |
4502 | |
4335 | if (index1 > strlength (x) || index1 < index0) |
4503 | if (index1 > strlength (x) || index1 < index0) |
4336 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4504 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4337 | } |
4505 | } |
4338 | else |
4506 | else |
4339 | index1 = strlength (x); |
4507 | index1 = strlength (x); |
4340 | |
4508 | |
4341 | len = index1 - index0; |
4509 | len = index1 - index0; |
4342 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4510 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4343 | memcpy (strvalue (x), str + index0, len); |
|
|
4344 | strvalue (x)[len] = 0; |
|
|
4345 | |
4511 | |
4346 | s_return (x); |
4512 | s_return (x); |
4347 | } |
4513 | } |
4348 | |
4514 | |
4349 | case OP_VECTOR: /* vector */ |
4515 | case OP_VECTOR: /* vector */ |
… | |
… | |
4391 | } |
4557 | } |
4392 | |
4558 | |
4393 | case OP_VECLEN: /* vector-length */ |
4559 | case OP_VECLEN: /* vector-length */ |
4394 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4560 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4395 | |
4561 | |
|
|
4562 | case OP_VECRESIZE: |
|
|
4563 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4564 | s_return (x); |
|
|
4565 | |
4396 | case OP_VECREF: /* vector-ref */ |
4566 | case OP_VECREF: /* vector-ref */ |
4397 | { |
4567 | { |
4398 | int index = ivalue_unchecked (cadr (args)); |
4568 | int index = ivalue_unchecked (cadr (args)); |
4399 | |
4569 | |
4400 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4570 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
… | |
… | |
4419 | } |
4589 | } |
4420 | |
4590 | |
4421 | if (USE_ERROR_CHECKING) abort (); |
4591 | if (USE_ERROR_CHECKING) abort (); |
4422 | } |
4592 | } |
4423 | |
4593 | |
4424 | static int |
4594 | /* relational ops */ |
|
|
4595 | ecb_hot static int |
4425 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4596 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4426 | { |
4597 | { |
4427 | pointer x = SCHEME_V->args; |
4598 | pointer x = SCHEME_V->args; |
4428 | |
4599 | |
4429 | for (;;) |
4600 | for (;;) |
… | |
… | |
4450 | } |
4621 | } |
4451 | |
4622 | |
4452 | s_return (S_T); |
4623 | s_return (S_T); |
4453 | } |
4624 | } |
4454 | |
4625 | |
4455 | static int |
4626 | /* predicates */ |
|
|
4627 | ecb_hot static int |
4456 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4628 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4457 | { |
4629 | { |
4458 | pointer args = SCHEME_V->args; |
4630 | pointer args = SCHEME_V->args; |
4459 | pointer a = car (args); |
4631 | pointer a = car (args); |
4460 | pointer d = cdr (args); |
4632 | pointer d = cdr (args); |
4461 | int r; |
4633 | int r; |
4462 | |
4634 | |
4463 | switch (op) |
4635 | switch (op) |
4464 | { |
4636 | { |
4465 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4637 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4466 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
4638 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break; |
4467 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4639 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4468 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4640 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4469 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
4641 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4642 | case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break; |
4470 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4643 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4471 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4644 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4472 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4645 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4473 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4646 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4474 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4647 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4475 | |
4648 | |
4476 | #if USE_CHAR_CLASSIFIERS |
4649 | #if USE_CHAR_CLASSIFIERS |
4477 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4650 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4478 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4651 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4479 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4652 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
… | |
… | |
4506 | } |
4679 | } |
4507 | |
4680 | |
4508 | s_retbool (r); |
4681 | s_retbool (r); |
4509 | } |
4682 | } |
4510 | |
4683 | |
4511 | static int |
4684 | /* promises, list ops, ports */ |
|
|
4685 | ecb_hot static int |
4512 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4686 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4513 | { |
4687 | { |
4514 | pointer args = SCHEME_V->args; |
4688 | pointer args = SCHEME_V->args; |
4515 | pointer a = car (args); |
4689 | pointer a = car (args); |
4516 | pointer x, y; |
4690 | pointer x, y; |
… | |
… | |
4529 | } |
4703 | } |
4530 | else |
4704 | else |
4531 | s_return (SCHEME_V->code); |
4705 | s_return (SCHEME_V->code); |
4532 | |
4706 | |
4533 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4707 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4534 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4708 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4535 | s_return (SCHEME_V->value); |
4709 | s_return (SCHEME_V->value); |
4536 | |
4710 | |
4537 | #if USE_PORTS |
4711 | #if USE_PORTS |
|
|
4712 | |
|
|
4713 | case OP_EOF_OBJECT: /* eof-object */ |
|
|
4714 | s_return (S_EOF); |
4538 | |
4715 | |
4539 | case OP_WRITE: /* write */ |
4716 | case OP_WRITE: /* write */ |
4540 | case OP_DISPLAY: /* display */ |
4717 | case OP_DISPLAY: /* display */ |
4541 | case OP_WRITE_CHAR: /* write-char */ |
4718 | case OP_WRITE_CHAR: /* write-char */ |
4542 | if (is_pair (cdr (SCHEME_V->args))) |
4719 | if (is_pair (cdr (SCHEME_V->args))) |
… | |
… | |
4556 | else |
4733 | else |
4557 | SCHEME_V->print_flag = 0; |
4734 | SCHEME_V->print_flag = 0; |
4558 | |
4735 | |
4559 | s_goto (OP_P0LIST); |
4736 | s_goto (OP_P0LIST); |
4560 | |
4737 | |
|
|
4738 | //TODO: move to scheme |
4561 | case OP_NEWLINE: /* newline */ |
4739 | case OP_NEWLINE: /* newline */ |
4562 | if (is_pair (args)) |
4740 | if (is_pair (args)) |
4563 | { |
4741 | { |
4564 | if (a != SCHEME_V->outport) |
4742 | if (a != SCHEME_V->outport) |
4565 | { |
4743 | { |
… | |
… | |
4567 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4745 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4568 | SCHEME_V->outport = a; |
4746 | SCHEME_V->outport = a; |
4569 | } |
4747 | } |
4570 | } |
4748 | } |
4571 | |
4749 | |
4572 | putstr (SCHEME_A_ "\n"); |
4750 | putcharacter (SCHEME_A_ '\n'); |
4573 | s_return (S_T); |
4751 | s_return (S_T); |
4574 | #endif |
4752 | #endif |
4575 | |
4753 | |
4576 | case OP_ERR0: /* error */ |
4754 | case OP_ERR0: /* error */ |
4577 | SCHEME_V->retcode = -1; |
4755 | SCHEME_V->retcode = -1; |
… | |
… | |
4586 | putstr (SCHEME_A_ strvalue (car (args))); |
4764 | putstr (SCHEME_A_ strvalue (car (args))); |
4587 | SCHEME_V->args = cdr (args); |
4765 | SCHEME_V->args = cdr (args); |
4588 | s_goto (OP_ERR1); |
4766 | s_goto (OP_ERR1); |
4589 | |
4767 | |
4590 | case OP_ERR1: /* error */ |
4768 | case OP_ERR1: /* error */ |
4591 | putstr (SCHEME_A_ " "); |
4769 | putcharacter (SCHEME_A_ ' '); |
4592 | |
4770 | |
4593 | if (args != NIL) |
4771 | if (args != NIL) |
4594 | { |
4772 | { |
4595 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4773 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4596 | SCHEME_V->args = a; |
4774 | SCHEME_V->args = a; |
4597 | SCHEME_V->print_flag = 1; |
4775 | SCHEME_V->print_flag = 1; |
4598 | s_goto (OP_P0LIST); |
4776 | s_goto (OP_P0LIST); |
4599 | } |
4777 | } |
4600 | else |
4778 | else |
4601 | { |
4779 | { |
4602 | putstr (SCHEME_A_ "\n"); |
4780 | putcharacter (SCHEME_A_ '\n'); |
4603 | |
4781 | |
4604 | if (SCHEME_V->interactive_repl) |
4782 | if (SCHEME_V->interactive_repl) |
4605 | s_goto (OP_T0LVL); |
4783 | s_goto (OP_T0LVL); |
4606 | else |
4784 | else |
4607 | return -1; |
4785 | return -1; |
… | |
… | |
4684 | SCHEME_V->gc_verbose = (a != S_F); |
4862 | SCHEME_V->gc_verbose = (a != S_F); |
4685 | s_retbool (was); |
4863 | s_retbool (was); |
4686 | } |
4864 | } |
4687 | |
4865 | |
4688 | case OP_NEWSEGMENT: /* new-segment */ |
4866 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4867 | #if 0 |
4689 | if (!is_pair (args) || !is_number (a)) |
4868 | if (!is_pair (args) || !is_number (a)) |
4690 | Error_0 ("new-segment: argument must be a number"); |
4869 | Error_0 ("new-segment: argument must be a number"); |
4691 | |
4870 | #endif |
4692 | alloc_cellseg (SCHEME_A_ ivalue (a)); |
4871 | s_retbool (alloc_cellseg (SCHEME_A)); |
4693 | |
|
|
4694 | s_return (S_T); |
|
|
4695 | |
4872 | |
4696 | case OP_OBLIST: /* oblist */ |
4873 | case OP_OBLIST: /* oblist */ |
4697 | s_return (oblist_all_symbols (SCHEME_A)); |
4874 | s_return (oblist_all_symbols (SCHEME_A)); |
4698 | |
4875 | |
4699 | #if USE_PORTS |
4876 | #if USE_PORTS |
… | |
… | |
4769 | s_return (p == NIL ? S_F : p); |
4946 | s_return (p == NIL ? S_F : p); |
4770 | } |
4947 | } |
4771 | |
4948 | |
4772 | case OP_GET_OUTSTRING: /* get-output-string */ |
4949 | case OP_GET_OUTSTRING: /* get-output-string */ |
4773 | { |
4950 | { |
4774 | port *p; |
4951 | port *p = port (a); |
4775 | |
4952 | |
4776 | if ((p = a->object.port)->kind & port_string) |
4953 | if (p->kind & port_string) |
4777 | { |
4954 | { |
4778 | off_t size; |
4955 | off_t size; |
4779 | char *str; |
4956 | char *str; |
4780 | |
4957 | |
4781 | size = p->rep.string.curr - p->rep.string.start + 1; |
4958 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4816 | } |
4993 | } |
4817 | |
4994 | |
4818 | if (USE_ERROR_CHECKING) abort (); |
4995 | if (USE_ERROR_CHECKING) abort (); |
4819 | } |
4996 | } |
4820 | |
4997 | |
4821 | static int |
4998 | /* reading */ |
|
|
4999 | ecb_cold static int |
4822 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
5000 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4823 | { |
5001 | { |
4824 | pointer args = SCHEME_V->args; |
5002 | pointer args = SCHEME_V->args; |
4825 | pointer x; |
5003 | pointer x; |
4826 | |
5004 | |
… | |
… | |
4886 | int res; |
5064 | int res; |
4887 | |
5065 | |
4888 | if (is_pair (args)) |
5066 | if (is_pair (args)) |
4889 | p = car (args); |
5067 | p = car (args); |
4890 | |
5068 | |
4891 | res = p->object.port->kind & port_string; |
5069 | res = port (p)->kind & port_string; |
4892 | |
5070 | |
4893 | s_retbool (res); |
5071 | s_retbool (res); |
4894 | } |
5072 | } |
4895 | |
5073 | |
4896 | case OP_SET_INPORT: /* set-input-port */ |
5074 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
4955 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5133 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
4956 | SCHEME_V->tok = token (SCHEME_A); |
5134 | SCHEME_V->tok = token (SCHEME_A); |
4957 | s_goto (OP_RDSEXPR); |
5135 | s_goto (OP_RDSEXPR); |
4958 | |
5136 | |
4959 | case TOK_ATOM: |
5137 | case TOK_ATOM: |
4960 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); |
5138 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
|
|
5139 | |
|
|
5140 | case TOK_DOTATOM: |
|
|
5141 | SCHEME_V->strbuff[0] = '.'; |
|
|
5142 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
|
|
5143 | |
|
|
5144 | case TOK_STRATOM: |
|
|
5145 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5146 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5147 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
4961 | |
5148 | |
4962 | case TOK_DQUOTE: |
5149 | case TOK_DQUOTE: |
4963 | x = readstrexp (SCHEME_A); |
5150 | x = readstrexp (SCHEME_A_ '"'); |
4964 | |
5151 | |
4965 | if (x == S_F) |
5152 | if (x == S_F) |
4966 | Error_0 ("Error reading string"); |
5153 | Error_0 ("Error reading string"); |
4967 | |
5154 | |
4968 | setimmutable (x); |
5155 | setimmutable (x); |
… | |
… | |
4980 | s_goto (OP_EVAL); |
5167 | s_goto (OP_EVAL); |
4981 | } |
5168 | } |
4982 | } |
5169 | } |
4983 | |
5170 | |
4984 | case TOK_SHARP_CONST: |
5171 | case TOK_SHARP_CONST: |
4985 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) |
5172 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
4986 | Error_0 ("undefined sharp expression"); |
5173 | Error_0 ("undefined sharp expression"); |
4987 | else |
5174 | else |
4988 | s_return (x); |
5175 | s_return (x); |
4989 | |
5176 | |
4990 | default: |
5177 | default: |
… | |
… | |
5086 | pointer b = cdr (args); |
5273 | pointer b = cdr (args); |
5087 | int ok_abbr = ok_abbrev (b); |
5274 | int ok_abbr = ok_abbrev (b); |
5088 | SCHEME_V->args = car (b); |
5275 | SCHEME_V->args = car (b); |
5089 | |
5276 | |
5090 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5277 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5091 | putstr (SCHEME_A_ "'"); |
5278 | putcharacter (SCHEME_A_ '\''); |
5092 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5279 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5093 | putstr (SCHEME_A_ "`"); |
5280 | putcharacter (SCHEME_A_ '`'); |
5094 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5281 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5095 | putstr (SCHEME_A_ ","); |
5282 | putcharacter (SCHEME_A_ ','); |
5096 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5283 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5097 | putstr (SCHEME_A_ ",@"); |
5284 | putstr (SCHEME_A_ ",@"); |
5098 | else |
5285 | else |
5099 | { |
5286 | { |
5100 | putstr (SCHEME_A_ "("); |
5287 | putcharacter (SCHEME_A_ '('); |
5101 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5288 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5102 | SCHEME_V->args = a; |
5289 | SCHEME_V->args = a; |
5103 | } |
5290 | } |
5104 | |
5291 | |
5105 | s_goto (OP_P0LIST); |
5292 | s_goto (OP_P0LIST); |
… | |
… | |
5107 | |
5294 | |
5108 | case OP_P1LIST: |
5295 | case OP_P1LIST: |
5109 | if (is_pair (args)) |
5296 | if (is_pair (args)) |
5110 | { |
5297 | { |
5111 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5298 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5112 | putstr (SCHEME_A_ " "); |
5299 | putcharacter (SCHEME_A_ ' '); |
5113 | SCHEME_V->args = car (args); |
5300 | SCHEME_V->args = car (args); |
5114 | s_goto (OP_P0LIST); |
5301 | s_goto (OP_P0LIST); |
5115 | } |
5302 | } |
5116 | else if (is_vector (args)) |
5303 | else if (is_vector (args)) |
5117 | { |
5304 | { |
… | |
… | |
5125 | { |
5312 | { |
5126 | putstr (SCHEME_A_ " . "); |
5313 | putstr (SCHEME_A_ " . "); |
5127 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5314 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5128 | } |
5315 | } |
5129 | |
5316 | |
5130 | putstr (SCHEME_A_ ")"); |
5317 | putcharacter (SCHEME_A_ ')'); |
5131 | s_return (S_T); |
5318 | s_return (S_T); |
5132 | } |
5319 | } |
5133 | |
5320 | |
5134 | case OP_PVECFROM: |
5321 | case OP_PVECFROM: |
5135 | { |
5322 | { |
… | |
… | |
5137 | pointer vec = car (args); |
5324 | pointer vec = car (args); |
5138 | int len = veclength (vec); |
5325 | int len = veclength (vec); |
5139 | |
5326 | |
5140 | if (i == len) |
5327 | if (i == len) |
5141 | { |
5328 | { |
5142 | putstr (SCHEME_A_ ")"); |
5329 | putcharacter (SCHEME_A_ ')'); |
5143 | s_return (S_T); |
5330 | s_return (S_T); |
5144 | } |
5331 | } |
5145 | else |
5332 | else |
5146 | { |
5333 | { |
5147 | pointer elem = vector_get (vec, i); |
5334 | pointer elem = vector_get (vec, i); |
… | |
… | |
5149 | ivalue_unchecked (cdr (args)) = i + 1; |
5336 | ivalue_unchecked (cdr (args)) = i + 1; |
5150 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5337 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5151 | SCHEME_V->args = elem; |
5338 | SCHEME_V->args = elem; |
5152 | |
5339 | |
5153 | if (i > 0) |
5340 | if (i > 0) |
5154 | putstr (SCHEME_A_ " "); |
5341 | putcharacter (SCHEME_A_ ' '); |
5155 | |
5342 | |
5156 | s_goto (OP_P0LIST); |
5343 | s_goto (OP_P0LIST); |
5157 | } |
5344 | } |
5158 | } |
5345 | } |
5159 | } |
5346 | } |
5160 | |
5347 | |
5161 | if (USE_ERROR_CHECKING) abort (); |
5348 | if (USE_ERROR_CHECKING) abort (); |
5162 | } |
5349 | } |
5163 | |
5350 | |
5164 | static int |
5351 | /* list ops */ |
|
|
5352 | ecb_hot static int |
5165 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5353 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5166 | { |
5354 | { |
5167 | pointer args = SCHEME_V->args; |
5355 | pointer args = SCHEME_V->args; |
5168 | pointer a = car (args); |
5356 | pointer a = car (args); |
5169 | pointer x, y; |
5357 | pointer x, y; |
… | |
… | |
5212 | |
5400 | |
5213 | case OP_CLOSUREP: /* closure? */ |
5401 | case OP_CLOSUREP: /* closure? */ |
5214 | /* |
5402 | /* |
5215 | * Note, macro object is also a closure. |
5403 | * Note, macro object is also a closure. |
5216 | * Therefore, (closure? <#MACRO>) ==> #t |
5404 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5405 | * (schmorp) well, obviously not, fix? TODO |
5217 | */ |
5406 | */ |
5218 | s_retbool (is_closure (a)); |
5407 | s_retbool (is_closure (a)); |
5219 | |
5408 | |
5220 | case OP_MACROP: /* macro? */ |
5409 | case OP_MACROP: /* macro? */ |
5221 | s_retbool (is_macro (a)); |
5410 | s_retbool (is_macro (a)); |
… | |
… | |
5226 | |
5415 | |
5227 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5416 | /* 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); |
5417 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5229 | |
5418 | |
5230 | typedef int (*test_predicate)(pointer); |
5419 | typedef int (*test_predicate)(pointer); |
5231 | static int |
5420 | |
|
|
5421 | ecb_hot static int |
5232 | tst_any (pointer p) |
5422 | tst_any (pointer p) |
5233 | { |
5423 | { |
5234 | return 1; |
5424 | return 1; |
5235 | } |
5425 | } |
5236 | |
5426 | |
5237 | static int |
5427 | ecb_hot static int |
5238 | tst_inonneg (pointer p) |
5428 | tst_inonneg (pointer p) |
5239 | { |
5429 | { |
5240 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5430 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5241 | } |
5431 | } |
5242 | |
5432 | |
5243 | static int |
5433 | ecb_hot static int |
5244 | tst_is_list (SCHEME_P_ pointer p) |
5434 | tst_is_list (SCHEME_P_ pointer p) |
5245 | { |
5435 | { |
5246 | return p == NIL || is_pair (p); |
5436 | return p == NIL || is_pair (p); |
5247 | } |
5437 | } |
5248 | |
5438 | |
… | |
… | |
5291 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5481 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5292 | #include "opdefines.h" |
5482 | #include "opdefines.h" |
5293 | #undef OP_DEF |
5483 | #undef OP_DEF |
5294 | ; |
5484 | ; |
5295 | |
5485 | |
5296 | static const char * |
5486 | ecb_cold static const char * |
5297 | opname (int idx) |
5487 | opname (int idx) |
5298 | { |
5488 | { |
5299 | const char *name = opnames; |
5489 | const char *name = opnames; |
5300 | |
5490 | |
5301 | /* should do this at compile time, but would require external program, right? */ |
5491 | /* should do this at compile time, but would require external program, right? */ |
… | |
… | |
5303 | name += strlen (name) + 1; |
5493 | name += strlen (name) + 1; |
5304 | |
5494 | |
5305 | return *name ? name : "ILLEGAL"; |
5495 | return *name ? name : "ILLEGAL"; |
5306 | } |
5496 | } |
5307 | |
5497 | |
5308 | static const char * |
5498 | ecb_cold static const char * |
5309 | procname (pointer x) |
5499 | procname (pointer x) |
5310 | { |
5500 | { |
5311 | return opname (procnum (x)); |
5501 | return opname (procnum (x)); |
5312 | } |
5502 | } |
5313 | |
5503 | |
… | |
… | |
5333 | #undef OP_DEF |
5523 | #undef OP_DEF |
5334 | {0} |
5524 | {0} |
5335 | }; |
5525 | }; |
5336 | |
5526 | |
5337 | /* kernel of this interpreter */ |
5527 | /* kernel of this interpreter */ |
5338 | static void ecb_hot |
5528 | ecb_hot static void |
5339 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5529 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5340 | { |
5530 | { |
5341 | SCHEME_V->op = op; |
5531 | SCHEME_V->op = op; |
5342 | |
5532 | |
5343 | for (;;) |
5533 | for (;;) |
… | |
… | |
5426 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5616 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5427 | return; |
5617 | return; |
5428 | |
5618 | |
5429 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5619 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5430 | { |
5620 | { |
5431 | xwrstr ("No memory!\n"); |
5621 | putstr (SCHEME_A_ "No memory!\n"); |
5432 | return; |
5622 | return; |
5433 | } |
5623 | } |
5434 | } |
5624 | } |
5435 | } |
5625 | } |
5436 | |
5626 | |
5437 | /* ========== Initialization of internal keywords ========== */ |
5627 | /* ========== Initialization of internal keywords ========== */ |
5438 | |
5628 | |
5439 | static void |
5629 | ecb_cold static void |
5440 | assign_syntax (SCHEME_P_ const char *name) |
5630 | assign_syntax (SCHEME_P_ const char *name) |
5441 | { |
5631 | { |
5442 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5632 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5443 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5633 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5444 | } |
5634 | } |
5445 | |
5635 | |
5446 | static void |
5636 | ecb_cold static void |
5447 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5637 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5448 | { |
5638 | { |
5449 | pointer x = mk_symbol (SCHEME_A_ name); |
5639 | pointer x = mk_symbol (SCHEME_A_ name); |
5450 | pointer y = mk_proc (SCHEME_A_ op); |
5640 | pointer y = mk_proc (SCHEME_A_ op); |
5451 | new_slot_in_env (SCHEME_A_ x, y); |
5641 | new_slot_in_env (SCHEME_A_ x, y); |
… | |
… | |
5459 | ivalue_unchecked (y) = op; |
5649 | ivalue_unchecked (y) = op; |
5460 | return y; |
5650 | return y; |
5461 | } |
5651 | } |
5462 | |
5652 | |
5463 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5653 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5464 | static int |
5654 | ecb_hot static int |
5465 | syntaxnum (pointer p) |
5655 | syntaxnum (pointer p) |
5466 | { |
5656 | { |
5467 | const char *s = strvalue (car (p)); |
5657 | const char *s = strvalue (p); |
5468 | |
5658 | |
5469 | switch (strlength (car (p))) |
5659 | switch (strlength (p)) |
5470 | { |
5660 | { |
5471 | case 2: |
5661 | case 2: |
5472 | if (s[0] == 'i') |
5662 | if (s[0] == 'i') |
5473 | return OP_IF0; /* if */ |
5663 | return OP_IF0; /* if */ |
5474 | else |
5664 | else |
… | |
… | |
5549 | ecb_cold int |
5739 | ecb_cold int |
5550 | scheme_init (SCHEME_P) |
5740 | scheme_init (SCHEME_P) |
5551 | { |
5741 | { |
5552 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5742 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5553 | pointer x; |
5743 | pointer x; |
|
|
5744 | |
|
|
5745 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
5746 | * that memset 0 will also set pointers to 0, but memset does |
|
|
5747 | * of course not guarantee that. screw such systems. |
|
|
5748 | */ |
|
|
5749 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5554 | |
5750 | |
5555 | num_set_fixnum (num_zero, 1); |
5751 | num_set_fixnum (num_zero, 1); |
5556 | num_set_ivalue (num_zero, 0); |
5752 | num_set_ivalue (num_zero, 0); |
5557 | num_set_fixnum (num_one, 1); |
5753 | num_set_fixnum (num_one, 1); |
5558 | num_set_ivalue (num_one, 1); |
5754 | num_set_ivalue (num_one, 1); |
… | |
… | |
5570 | SCHEME_V->save_inport = NIL; |
5766 | SCHEME_V->save_inport = NIL; |
5571 | SCHEME_V->loadport = NIL; |
5767 | SCHEME_V->loadport = NIL; |
5572 | SCHEME_V->nesting = 0; |
5768 | SCHEME_V->nesting = 0; |
5573 | SCHEME_V->interactive_repl = 0; |
5769 | SCHEME_V->interactive_repl = 0; |
5574 | |
5770 | |
5575 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5771 | if (!alloc_cellseg (SCHEME_A)) |
5576 | { |
5772 | { |
5577 | #if USE_ERROR_CHECKING |
5773 | #if USE_ERROR_CHECKING |
5578 | SCHEME_V->no_memory = 1; |
5774 | SCHEME_V->no_memory = 1; |
5579 | return 0; |
5775 | return 0; |
5580 | #endif |
5776 | #endif |
5581 | } |
5777 | } |
5582 | |
5778 | |
5583 | SCHEME_V->gc_verbose = 0; |
5779 | SCHEME_V->gc_verbose = 0; |
5584 | dump_stack_initialize (SCHEME_A); |
5780 | dump_stack_initialize (SCHEME_A); |
5585 | SCHEME_V->code = NIL; |
5781 | SCHEME_V->code = NIL; |
5586 | SCHEME_V->args = NIL; |
5782 | SCHEME_V->args = NIL; |
5587 | SCHEME_V->envir = NIL; |
5783 | SCHEME_V->envir = NIL; |
|
|
5784 | SCHEME_V->value = NIL; |
5588 | SCHEME_V->tracing = 0; |
5785 | SCHEME_V->tracing = 0; |
5589 | |
5786 | |
5590 | /* init NIL */ |
5787 | /* init NIL */ |
5591 | set_typeflag (NIL, T_ATOM | T_MARK); |
5788 | set_typeflag (NIL, T_ATOM | T_MARK); |
5592 | set_car (NIL, NIL); |
5789 | set_car (NIL, NIL); |
… | |
… | |
5648 | |
5845 | |
5649 | return !SCHEME_V->no_memory; |
5846 | return !SCHEME_V->no_memory; |
5650 | } |
5847 | } |
5651 | |
5848 | |
5652 | #if USE_PORTS |
5849 | #if USE_PORTS |
5653 | void |
5850 | ecb_cold void |
5654 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5851 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5655 | { |
5852 | { |
5656 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5853 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5657 | } |
5854 | } |
5658 | |
5855 | |
5659 | void |
5856 | ecb_cold void |
5660 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5857 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5661 | { |
5858 | { |
5662 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5859 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5663 | } |
5860 | } |
5664 | |
5861 | |
5665 | void |
5862 | ecb_cold void |
5666 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5863 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5667 | { |
5864 | { |
5668 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5865 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5669 | } |
5866 | } |
5670 | |
5867 | |
5671 | void |
5868 | ecb_cold void |
5672 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5869 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5673 | { |
5870 | { |
5674 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5871 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5675 | } |
5872 | } |
5676 | #endif |
5873 | #endif |
5677 | |
5874 | |
5678 | void |
5875 | ecb_cold void |
5679 | scheme_set_external_data (SCHEME_P_ void *p) |
5876 | scheme_set_external_data (SCHEME_P_ void *p) |
5680 | { |
5877 | { |
5681 | SCHEME_V->ext_data = p; |
5878 | SCHEME_V->ext_data = p; |
5682 | } |
5879 | } |
5683 | |
5880 | |
… | |
… | |
5731 | } |
5928 | } |
5732 | } |
5929 | } |
5733 | #endif |
5930 | #endif |
5734 | } |
5931 | } |
5735 | |
5932 | |
5736 | void |
5933 | ecb_cold void |
5737 | scheme_load_file (SCHEME_P_ int fin) |
5934 | scheme_load_file (SCHEME_P_ int fin) |
5738 | { |
5935 | { |
5739 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5936 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5740 | } |
5937 | } |
5741 | |
5938 | |
5742 | void |
5939 | ecb_cold void |
5743 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5940 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5744 | { |
5941 | { |
5745 | dump_stack_reset (SCHEME_A); |
5942 | dump_stack_reset (SCHEME_A); |
5746 | SCHEME_V->envir = SCHEME_V->global_env; |
5943 | SCHEME_V->envir = SCHEME_V->global_env; |
5747 | SCHEME_V->file_i = 0; |
5944 | SCHEME_V->file_i = 0; |
5748 | SCHEME_V->load_stack[0].unget = -1; |
5945 | SCHEME_V->load_stack[0].unget = -1; |
5749 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5946 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5750 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5947 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5751 | #if USE_PORTS |
|
|
5752 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5948 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5753 | #endif |
|
|
5754 | SCHEME_V->retcode = 0; |
5949 | SCHEME_V->retcode = 0; |
5755 | |
5950 | |
5756 | #if USE_PORTS |
|
|
5757 | if (fin == STDIN_FILENO) |
5951 | if (fin == STDIN_FILENO) |
5758 | SCHEME_V->interactive_repl = 1; |
5952 | SCHEME_V->interactive_repl = 1; |
5759 | #endif |
|
|
5760 | |
5953 | |
5761 | #if USE_PORTS |
5954 | #if USE_PORTS |
5762 | #if SHOW_ERROR_LINE |
5955 | #if SHOW_ERROR_LINE |
5763 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5956 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5764 | |
5957 | |
… | |
… | |
5768 | #endif |
5961 | #endif |
5769 | |
5962 | |
5770 | SCHEME_V->inport = SCHEME_V->loadport; |
5963 | SCHEME_V->inport = SCHEME_V->loadport; |
5771 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5964 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5772 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5965 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
|
|
5966 | |
5773 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5967 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5774 | |
5968 | |
5775 | if (SCHEME_V->retcode == 0) |
5969 | if (SCHEME_V->retcode == 0) |
5776 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5970 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5777 | } |
5971 | } |
5778 | |
5972 | |
5779 | void |
5973 | ecb_cold void |
5780 | scheme_load_string (SCHEME_P_ const char *cmd) |
5974 | scheme_load_string (SCHEME_P_ const char *cmd) |
5781 | { |
5975 | { |
|
|
5976 | #if USE_PORTs |
5782 | dump_stack_reset (SCHEME_A); |
5977 | dump_stack_reset (SCHEME_A); |
5783 | SCHEME_V->envir = SCHEME_V->global_env; |
5978 | SCHEME_V->envir = SCHEME_V->global_env; |
5784 | SCHEME_V->file_i = 0; |
5979 | SCHEME_V->file_i = 0; |
5785 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5980 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5786 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
5981 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
5787 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
5982 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
5788 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5983 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5789 | #if USE_PORTS |
|
|
5790 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5984 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5791 | #endif |
|
|
5792 | SCHEME_V->retcode = 0; |
5985 | SCHEME_V->retcode = 0; |
5793 | SCHEME_V->interactive_repl = 0; |
5986 | SCHEME_V->interactive_repl = 0; |
5794 | SCHEME_V->inport = SCHEME_V->loadport; |
5987 | SCHEME_V->inport = SCHEME_V->loadport; |
5795 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5988 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5796 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5989 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5797 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5990 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5798 | |
5991 | |
5799 | if (SCHEME_V->retcode == 0) |
5992 | if (SCHEME_V->retcode == 0) |
5800 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5993 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
|
|
5994 | #else |
|
|
5995 | abort (); |
|
|
5996 | #endif |
5801 | } |
5997 | } |
5802 | |
5998 | |
5803 | void |
5999 | ecb_cold void |
5804 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
6000 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5805 | { |
6001 | { |
5806 | pointer x; |
6002 | pointer x; |
5807 | |
6003 | |
5808 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
6004 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
… | |
… | |
5813 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
6009 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5814 | } |
6010 | } |
5815 | |
6011 | |
5816 | #if !STANDALONE |
6012 | #if !STANDALONE |
5817 | |
6013 | |
5818 | void |
6014 | ecb_cold void |
5819 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
6015 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5820 | { |
6016 | { |
5821 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
6017 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5822 | } |
6018 | } |
5823 | |
6019 | |
5824 | void |
6020 | ecb_cold void |
5825 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
6021 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5826 | { |
6022 | { |
5827 | int i; |
6023 | int i; |
5828 | |
6024 | |
5829 | for (i = 0; i < count; i++) |
6025 | for (i = 0; i < count; i++) |
5830 | scheme_register_foreign_func (SCHEME_A_ list + i); |
6026 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5831 | } |
6027 | } |
5832 | |
6028 | |
5833 | pointer |
6029 | ecb_cold pointer |
5834 | scheme_apply0 (SCHEME_P_ const char *procname) |
6030 | scheme_apply0 (SCHEME_P_ const char *procname) |
5835 | { |
6031 | { |
5836 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
6032 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
5837 | } |
6033 | } |
5838 | |
6034 | |
5839 | void |
6035 | ecb_cold void |
5840 | save_from_C_call (SCHEME_P) |
6036 | save_from_C_call (SCHEME_P) |
5841 | { |
6037 | { |
5842 | pointer saved_data = cons (car (S_SINK), |
6038 | pointer saved_data = cons (car (S_SINK), |
5843 | cons (SCHEME_V->envir, |
6039 | cons (SCHEME_V->envir, |
5844 | SCHEME_V->dump)); |
6040 | SCHEME_V->dump)); |
… | |
… | |
5848 | /* Truncate the dump stack so TS will return here when done, not |
6044 | /* Truncate the dump stack so TS will return here when done, not |
5849 | directly resume pre-C-call operations. */ |
6045 | directly resume pre-C-call operations. */ |
5850 | dump_stack_reset (SCHEME_A); |
6046 | dump_stack_reset (SCHEME_A); |
5851 | } |
6047 | } |
5852 | |
6048 | |
5853 | void |
6049 | ecb_cold void |
5854 | restore_from_C_call (SCHEME_P) |
6050 | restore_from_C_call (SCHEME_P) |
5855 | { |
6051 | { |
5856 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
6052 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5857 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
6053 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5858 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
6054 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5859 | /* Pop */ |
6055 | /* Pop */ |
5860 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
6056 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5861 | } |
6057 | } |
5862 | |
6058 | |
5863 | /* "func" and "args" are assumed to be already eval'ed. */ |
6059 | /* "func" and "args" are assumed to be already eval'ed. */ |
5864 | pointer |
6060 | ecb_cold pointer |
5865 | scheme_call (SCHEME_P_ pointer func, pointer args) |
6061 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5866 | { |
6062 | { |
5867 | int old_repl = SCHEME_V->interactive_repl; |
6063 | int old_repl = SCHEME_V->interactive_repl; |
5868 | |
6064 | |
5869 | SCHEME_V->interactive_repl = 0; |
6065 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5876 | SCHEME_V->interactive_repl = old_repl; |
6072 | SCHEME_V->interactive_repl = old_repl; |
5877 | restore_from_C_call (SCHEME_A); |
6073 | restore_from_C_call (SCHEME_A); |
5878 | return SCHEME_V->value; |
6074 | return SCHEME_V->value; |
5879 | } |
6075 | } |
5880 | |
6076 | |
5881 | pointer |
6077 | ecb_cold pointer |
5882 | scheme_eval (SCHEME_P_ pointer obj) |
6078 | scheme_eval (SCHEME_P_ pointer obj) |
5883 | { |
6079 | { |
5884 | int old_repl = SCHEME_V->interactive_repl; |
6080 | int old_repl = SCHEME_V->interactive_repl; |
5885 | |
6081 | |
5886 | SCHEME_V->interactive_repl = 0; |
6082 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5898 | |
6094 | |
5899 | /* ========== Main ========== */ |
6095 | /* ========== Main ========== */ |
5900 | |
6096 | |
5901 | #if STANDALONE |
6097 | #if STANDALONE |
5902 | |
6098 | |
5903 | int |
6099 | ecb_cold int |
5904 | main (int argc, char **argv) |
6100 | main (int argc, char **argv) |
5905 | { |
6101 | { |
5906 | # if USE_MULTIPLICITY |
6102 | # if USE_MULTIPLICITY |
5907 | scheme ssc; |
6103 | scheme ssc; |
5908 | scheme *const SCHEME_V = &ssc; |
6104 | scheme *const SCHEME_V = &ssc; |
… | |
… | |
5910 | # endif |
6106 | # endif |
5911 | int fin; |
6107 | int fin; |
5912 | char *file_name = InitFile; |
6108 | char *file_name = InitFile; |
5913 | int retcode; |
6109 | int retcode; |
5914 | int isfile = 1; |
6110 | int isfile = 1; |
|
|
6111 | system ("ps v $PPID");//D |
5915 | |
6112 | |
5916 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6113 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5917 | { |
6114 | { |
5918 | xwrstr ("Usage: tinyscheme -?\n"); |
6115 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
5919 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6116 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
5920 | xwrstr ("followed by\n"); |
6117 | putstr (SCHEME_A_ "followed by\n"); |
5921 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6118 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
5922 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6119 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
5923 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6120 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
5924 | xwrstr ("Use - as filename for stdin.\n"); |
6121 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
5925 | return 1; |
6122 | return 1; |
5926 | } |
6123 | } |
5927 | |
6124 | |
5928 | if (!scheme_init (SCHEME_A)) |
6125 | if (!scheme_init (SCHEME_A)) |
5929 | { |
6126 | { |
5930 | xwrstr ("Could not initialize!\n"); |
6127 | putstr (SCHEME_A_ "Could not initialize!\n"); |
5931 | return 2; |
6128 | return 2; |
5932 | } |
6129 | } |
5933 | |
6130 | |
5934 | # if USE_PORTS |
6131 | # if USE_PORTS |
5935 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6132 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
5948 | } |
6145 | } |
5949 | #endif |
6146 | #endif |
5950 | |
6147 | |
5951 | do |
6148 | do |
5952 | { |
6149 | { |
5953 | #if USE_PORTS |
|
|
5954 | if (strcmp (file_name, "-") == 0) |
6150 | if (strcmp (file_name, "-") == 0) |
5955 | fin = STDIN_FILENO; |
6151 | fin = STDIN_FILENO; |
5956 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
6152 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
5957 | { |
6153 | { |
5958 | pointer args = NIL; |
6154 | pointer args = NIL; |
… | |
… | |
5976 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
6172 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
5977 | |
6173 | |
5978 | } |
6174 | } |
5979 | else |
6175 | else |
5980 | fin = open (file_name, O_RDONLY); |
6176 | fin = open (file_name, O_RDONLY); |
5981 | #endif |
|
|
5982 | |
6177 | |
5983 | if (isfile && fin < 0) |
6178 | if (isfile && fin < 0) |
5984 | { |
6179 | { |
5985 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6180 | putstr (SCHEME_A_ "Could not open file "); |
|
|
6181 | putstr (SCHEME_A_ file_name); |
|
|
6182 | putcharacter (SCHEME_A_ '\n'); |
5986 | } |
6183 | } |
5987 | else |
6184 | else |
5988 | { |
6185 | { |
5989 | if (isfile) |
6186 | if (isfile) |
5990 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6187 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
5991 | else |
6188 | else |
5992 | scheme_load_string (SCHEME_A_ file_name); |
6189 | scheme_load_string (SCHEME_A_ file_name); |
5993 | |
6190 | |
5994 | #if USE_PORTS |
|
|
5995 | if (!isfile || fin != STDIN_FILENO) |
6191 | if (!isfile || fin != STDIN_FILENO) |
5996 | { |
6192 | { |
5997 | if (SCHEME_V->retcode != 0) |
6193 | if (SCHEME_V->retcode != 0) |
5998 | { |
6194 | { |
5999 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6195 | putstr (SCHEME_A_ "Errors encountered reading "); |
|
|
6196 | putstr (SCHEME_A_ file_name); |
|
|
6197 | putcharacter (SCHEME_A_ '\n'); |
6000 | } |
6198 | } |
6001 | |
6199 | |
6002 | if (isfile) |
6200 | if (isfile) |
6003 | close (fin); |
6201 | close (fin); |
6004 | } |
6202 | } |
6005 | #endif |
|
|
6006 | } |
6203 | } |
6007 | |
6204 | |
6008 | file_name = *argv++; |
6205 | file_name = *argv++; |
6009 | } |
6206 | } |
6010 | while (file_name != 0); |
6207 | while (file_name != 0); |