… | |
… | |
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> |
28 | #endif |
30 | #endif |
29 | #if USE_MATH |
31 | #if USE_MATH |
30 | # include <math.h> |
32 | # include <math.h> |
31 | #endif |
33 | #endif |
32 | |
34 | |
|
|
35 | #define ECB_NO_THREADS 1 |
33 | #include "ecb.h" |
36 | #include "ecb.h" |
34 | |
37 | |
35 | #include <sys/types.h> |
38 | #include <sys/types.h> |
36 | #include <sys/stat.h> |
39 | #include <sys/stat.h> |
37 | #include <fcntl.h> |
40 | #include <fcntl.h> |
… | |
… | |
45 | #include <string.h> |
48 | #include <string.h> |
46 | |
49 | |
47 | #include <limits.h> |
50 | #include <limits.h> |
48 | #include <inttypes.h> |
51 | #include <inttypes.h> |
49 | #include <float.h> |
52 | #include <float.h> |
50 | //#include <ctype.h> |
53 | |
|
|
54 | #if !USE_SYSTEM_MALLOC |
|
|
55 | # define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
|
|
56 | # include "malloc.c" |
|
|
57 | # define malloc(n) tiny_malloc (n) |
|
|
58 | # define realloc(p,n) tiny_realloc (p, n) |
|
|
59 | # define free(p) tiny_free (p) |
|
|
60 | #endif |
51 | |
61 | |
52 | #if '1' != '0' + 1 \ |
62 | #if '1' != '0' + 1 \ |
53 | || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ |
63 | || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ |
54 | || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ |
64 | || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ |
55 | || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ |
65 | || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ |
… | |
… | |
77 | |
87 | |
78 | #define BACKQUOTE '`' |
88 | #define BACKQUOTE '`' |
79 | #define WHITESPACE " \t\r\n\v\f" |
89 | #define WHITESPACE " \t\r\n\v\f" |
80 | #define DELIMITERS "()\";" WHITESPACE |
90 | #define DELIMITERS "()\";" WHITESPACE |
81 | |
91 | |
82 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
92 | #define NIL POINTER (&SCHEME_V->xNIL) |
83 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
93 | #define S_T POINTER (&SCHEME_V->xT) |
84 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
94 | #define S_F POINTER (&SCHEME_V->xF) |
85 | #define S_SINK (&SCHEME_V->xsink) |
95 | #define S_SINK POINTER (&SCHEME_V->xsink) |
86 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
96 | #define S_EOF POINTER (&SCHEME_V->xEOF_OBJ) |
87 | |
97 | |
88 | #if !USE_MULTIPLICITY |
98 | #if !USE_MULTIPLICITY |
89 | static scheme sc; |
99 | static scheme sc; |
90 | #endif |
100 | #endif |
91 | |
101 | |
92 | static void |
102 | ecb_cold static void |
93 | xbase (char *s, long n, int base) |
103 | xbase (char *s, long n, int base) |
94 | { |
104 | { |
95 | if (n < 0) |
105 | if (n < 0) |
96 | { |
106 | { |
97 | *s++ = '-'; |
107 | *s++ = '-'; |
… | |
… | |
99 | } |
109 | } |
100 | |
110 | |
101 | char *p = s; |
111 | char *p = s; |
102 | |
112 | |
103 | do { |
113 | do { |
104 | *p++ = '0' + n % base; |
114 | *p++ = "0123456789abcdef"[n % base]; |
105 | n /= base; |
115 | n /= base; |
106 | } while (n); |
116 | } while (n); |
107 | |
117 | |
108 | *p-- = 0; |
118 | *p-- = 0; |
109 | |
119 | |
… | |
… | |
112 | char x = *s; *s = *p; *p = x; |
122 | char x = *s; *s = *p; *p = x; |
113 | --p; ++s; |
123 | --p; ++s; |
114 | } |
124 | } |
115 | } |
125 | } |
116 | |
126 | |
117 | static void |
127 | ecb_cold static void |
118 | xnum (char *s, long n) |
128 | xnum (char *s, long n) |
119 | { |
129 | { |
120 | xbase (s, n, 10); |
130 | xbase (s, n, 10); |
121 | } |
131 | } |
122 | |
132 | |
123 | static void |
133 | ecb_cold static void |
124 | xwrstr (const char *s) |
134 | putnum (SCHEME_P_ long n) |
125 | { |
|
|
126 | write (1, s, strlen (s)); |
|
|
127 | } |
|
|
128 | |
|
|
129 | static void |
|
|
130 | xwrnum (long n) |
|
|
131 | { |
135 | { |
132 | char buf[64]; |
136 | char buf[64]; |
133 | |
137 | |
134 | xnum (buf, n); |
138 | xnum (buf, n); |
135 | xwrstr (buf); |
139 | putstr (SCHEME_A_ buf); |
136 | } |
140 | } |
|
|
141 | |
|
|
142 | #if USE_CHAR_CLASSIFIERS |
|
|
143 | #include <ctype.h> |
|
|
144 | #else |
137 | |
145 | |
138 | static char |
146 | static char |
139 | xtoupper (char c) |
147 | xtoupper (char c) |
140 | { |
148 | { |
141 | if (c >= 'a' && c <= 'z') |
149 | if (c >= 'a' && c <= 'z') |
… | |
… | |
161 | |
169 | |
162 | #define toupper(c) xtoupper (c) |
170 | #define toupper(c) xtoupper (c) |
163 | #define tolower(c) xtolower (c) |
171 | #define tolower(c) xtolower (c) |
164 | #define isdigit(c) xisdigit (c) |
172 | #define isdigit(c) xisdigit (c) |
165 | |
173 | |
|
|
174 | #endif |
|
|
175 | |
166 | #if USE_IGNORECASE |
176 | #if USE_IGNORECASE |
167 | static const char * |
177 | ecb_cold static const char * |
168 | xstrlwr (char *s) |
178 | xstrlwr (char *s) |
169 | { |
179 | { |
170 | const char *p = s; |
180 | const char *p = s; |
171 | |
181 | |
172 | while (*s) |
182 | while (*s) |
… | |
… | |
185 | # define stricmp(a,b) strcmp (a, b) |
195 | # define stricmp(a,b) strcmp (a, b) |
186 | # define strlwr(s) (s) |
196 | # define strlwr(s) (s) |
187 | #endif |
197 | #endif |
188 | |
198 | |
189 | #ifndef prompt |
199 | #ifndef prompt |
190 | # define prompt "ts> " |
200 | # define prompt "ms> " |
191 | #endif |
201 | #endif |
192 | |
202 | |
193 | #ifndef InitFile |
203 | #ifndef InitFile |
194 | # define InitFile "init.scm" |
204 | # define InitFile "init.scm" |
195 | #endif |
205 | #endif |
196 | |
206 | |
197 | #ifndef FIRST_CELLSEGS |
|
|
198 | # define FIRST_CELLSEGS 3 |
|
|
199 | #endif |
|
|
200 | |
|
|
201 | enum scheme_types |
207 | enum scheme_types |
202 | { |
208 | { |
203 | T_INTEGER, |
209 | T_INTEGER, |
|
|
210 | T_CHARACTER, |
204 | T_REAL, |
211 | T_REAL, |
205 | T_STRING, |
212 | T_STRING, |
206 | T_SYMBOL, |
213 | T_SYMBOL, |
207 | T_PROC, |
214 | T_PROC, |
208 | T_PAIR, /* also used for free cells */ |
215 | T_PAIR, /* also used for free cells */ |
209 | T_CLOSURE, |
216 | T_CLOSURE, |
|
|
217 | T_BYTECODE, // temp |
|
|
218 | T_MACRO, |
210 | T_CONTINUATION, |
219 | T_CONTINUATION, |
211 | T_FOREIGN, |
220 | T_FOREIGN, |
212 | T_CHARACTER, |
|
|
213 | T_PORT, |
221 | T_PORT, |
214 | T_VECTOR, |
222 | T_VECTOR, |
215 | T_MACRO, |
|
|
216 | T_PROMISE, |
223 | T_PROMISE, |
217 | T_ENVIRONMENT, |
224 | T_ENVIRONMENT, |
218 | /* one more... */ |
225 | |
219 | T_NUM_SYSTEM_TYPES |
226 | T_NUM_SYSTEM_TYPES |
220 | }; |
227 | }; |
221 | |
228 | |
222 | #define T_MASKTYPE 0x000f |
229 | #define T_MASKTYPE 0x000f |
223 | #define T_SYNTAX 0x0010 |
230 | #define T_SYNTAX 0x0010 |
… | |
… | |
256 | static num num_op (enum num_op op, num a, num b); |
263 | static num num_op (enum num_op op, num a, num b); |
257 | static num num_intdiv (num a, num b); |
264 | static num num_intdiv (num a, num b); |
258 | static num num_rem (num a, num b); |
265 | static num num_rem (num a, num b); |
259 | static num num_mod (num a, num b); |
266 | static num num_mod (num a, num b); |
260 | |
267 | |
261 | #if USE_MATH |
|
|
262 | static double round_per_R5RS (double x); |
|
|
263 | #endif |
|
|
264 | static int is_zero_rvalue (RVALUE x); |
268 | static int is_zero_rvalue (RVALUE x); |
265 | |
269 | |
266 | static num num_zero; |
270 | static num num_zero; |
267 | static num num_one; |
271 | static num num_one; |
268 | |
272 | |
|
|
273 | /* convert "pointer" to cell* / cell* to pointer */ |
|
|
274 | #define CELL(p) ((struct cell *)(p) + 0) |
|
|
275 | #define POINTER(c) ((void *)((c) - 0)) |
|
|
276 | |
269 | /* macros for cell operations */ |
277 | /* macros for cell operations */ |
270 | #define typeflag(p) ((p)->flag + 0) |
278 | #define typeflag(p) (CELL(p)->flag + 0) |
271 | #define set_typeflag(p,v) ((p)->flag = (v)) |
279 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
272 | #define type(p) (typeflag (p) & T_MASKTYPE) |
280 | #define type(p) (typeflag (p) & T_MASKTYPE) |
273 | |
281 | |
274 | INTERFACE int |
282 | INTERFACE int |
275 | is_string (pointer p) |
283 | is_string (pointer p) |
276 | { |
284 | { |
277 | return type (p) == T_STRING; |
285 | return type (p) == T_STRING; |
278 | } |
286 | } |
279 | |
287 | |
280 | #define strvalue(p) ((p)->object.string.svalue) |
288 | #define strvalue(p) (CELL(p)->object.string.svalue) |
281 | #define strlength(p) ((p)->object.string.length) |
289 | #define strlength(p) (CELL(p)->object.string.length) |
282 | |
290 | |
283 | INTERFACE int |
291 | INTERFACE int |
284 | is_vector (pointer p) |
292 | is_vector (pointer p) |
285 | { |
293 | { |
286 | return type (p) == T_VECTOR; |
294 | return type (p) == T_VECTOR; |
287 | } |
295 | } |
288 | |
296 | |
289 | #define vecvalue(p) ((p)->object.vector.vvalue) |
297 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
290 | #define veclength(p) ((p)->object.vector.length) |
298 | #define veclength(p) (CELL(p)->object.vector.length) |
291 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
299 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
292 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
300 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
293 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
301 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
294 | |
302 | |
295 | INTERFACE int |
303 | INTERFACE int |
… | |
… | |
321 | string_value (pointer p) |
329 | string_value (pointer p) |
322 | { |
330 | { |
323 | return strvalue (p); |
331 | return strvalue (p); |
324 | } |
332 | } |
325 | |
333 | |
326 | #define ivalue_unchecked(p) (p)->object.ivalue |
334 | #define ivalue_unchecked(p) CELL(p)->object.ivalue |
327 | #define set_ivalue(p,v) (p)->object.ivalue = (v) |
335 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
328 | |
336 | |
329 | #if USE_REAL |
337 | #if USE_REAL |
330 | #define rvalue_unchecked(p) (p)->object.rvalue |
338 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
331 | #define set_rvalue(p,v) (p)->object.rvalue = (v) |
339 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
332 | #else |
340 | #else |
333 | #define rvalue_unchecked(p) (p)->object.ivalue |
341 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
334 | #define set_rvalue(p,v) (p)->object.ivalue = (v) |
342 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
335 | #endif |
343 | #endif |
336 | |
344 | |
337 | INTERFACE long |
345 | INTERFACE long |
338 | charvalue (pointer p) |
346 | charvalue (pointer p) |
339 | { |
347 | { |
340 | return ivalue_unchecked (p); |
348 | return ivalue_unchecked (p); |
341 | } |
349 | } |
342 | |
350 | |
|
|
351 | #define port(p) CELL(p)->object.port |
|
|
352 | #define set_port(p,v) port(p) = (v) |
343 | INTERFACE int |
353 | INTERFACE int |
344 | is_port (pointer p) |
354 | is_port (pointer p) |
345 | { |
355 | { |
346 | return type (p) == T_PORT; |
356 | return type (p) == T_PORT; |
347 | } |
357 | } |
348 | |
358 | |
349 | INTERFACE int |
359 | INTERFACE int |
350 | is_inport (pointer p) |
360 | is_inport (pointer p) |
351 | { |
361 | { |
352 | return is_port (p) && p->object.port->kind & port_input; |
362 | return is_port (p) && port (p)->kind & port_input; |
353 | } |
363 | } |
354 | |
364 | |
355 | INTERFACE int |
365 | INTERFACE int |
356 | is_outport (pointer p) |
366 | is_outport (pointer p) |
357 | { |
367 | { |
358 | return is_port (p) && p->object.port->kind & port_output; |
368 | return is_port (p) && port (p)->kind & port_output; |
359 | } |
369 | } |
360 | |
370 | |
361 | INTERFACE int |
371 | INTERFACE int |
362 | is_pair (pointer p) |
372 | is_pair (pointer p) |
363 | { |
373 | { |
364 | return type (p) == T_PAIR; |
374 | return type (p) == T_PAIR; |
365 | } |
375 | } |
366 | |
376 | |
367 | #define car(p) ((p)->object.cons.car + 0) |
377 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
368 | #define cdr(p) ((p)->object.cons.cdr + 0) |
378 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
369 | |
379 | |
370 | static pointer caar (pointer p) { return car (car (p)); } |
380 | static pointer caar (pointer p) { return car (car (p)); } |
371 | static pointer cadr (pointer p) { return car (cdr (p)); } |
381 | static pointer cadr (pointer p) { return car (cdr (p)); } |
372 | static pointer cdar (pointer p) { return cdr (car (p)); } |
382 | static pointer cdar (pointer p) { return cdr (car (p)); } |
373 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
383 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
… | |
… | |
377 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
387 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
378 | |
388 | |
379 | INTERFACE void |
389 | INTERFACE void |
380 | set_car (pointer p, pointer q) |
390 | set_car (pointer p, pointer q) |
381 | { |
391 | { |
382 | p->object.cons.car = q; |
392 | CELL(p)->object.cons.car = CELL (q); |
383 | } |
393 | } |
384 | |
394 | |
385 | INTERFACE void |
395 | INTERFACE void |
386 | set_cdr (pointer p, pointer q) |
396 | set_cdr (pointer p, pointer q) |
387 | { |
397 | { |
388 | p->object.cons.cdr = q; |
398 | CELL(p)->object.cons.cdr = CELL (q); |
389 | } |
399 | } |
390 | |
400 | |
391 | INTERFACE pointer |
401 | INTERFACE pointer |
392 | pair_car (pointer p) |
402 | pair_car (pointer p) |
393 | { |
403 | { |
… | |
… | |
411 | { |
421 | { |
412 | return strvalue (p); |
422 | return strvalue (p); |
413 | } |
423 | } |
414 | |
424 | |
415 | #if USE_PLIST |
425 | #if USE_PLIST |
|
|
426 | #error plists are broken because symbols are no longer pairs |
|
|
427 | #define symprop(p) cdr(p) |
416 | SCHEME_EXPORT int |
428 | SCHEME_EXPORT int |
417 | hasprop (pointer p) |
429 | hasprop (pointer p) |
418 | { |
430 | { |
419 | return typeflag (p) & T_SYMBOL; |
431 | return typeflag (p) & T_SYMBOL; |
420 | } |
432 | } |
421 | |
|
|
422 | # define symprop(p) cdr(p) |
|
|
423 | #endif |
433 | #endif |
424 | |
434 | |
425 | INTERFACE int |
435 | INTERFACE int |
426 | is_syntax (pointer p) |
436 | is_syntax (pointer p) |
427 | { |
437 | { |
… | |
… | |
523 | proper list: length |
533 | proper list: length |
524 | circular list: -1 |
534 | circular list: -1 |
525 | not even a pair: -2 |
535 | not even a pair: -2 |
526 | dotted list: -2 minus length before dot |
536 | dotted list: -2 minus length before dot |
527 | */ |
537 | */ |
528 | INTERFACE int |
538 | ecb_hot INTERFACE int |
529 | list_length (SCHEME_P_ pointer a) |
539 | list_length (SCHEME_P_ pointer a) |
530 | { |
540 | { |
531 | int i = 0; |
541 | int i = 0; |
532 | pointer slow, fast; |
542 | pointer slow, fast; |
533 | |
543 | |
… | |
… | |
572 | { |
582 | { |
573 | return list_length (SCHEME_A_ a) >= 0; |
583 | return list_length (SCHEME_A_ a) >= 0; |
574 | } |
584 | } |
575 | |
585 | |
576 | #if USE_CHAR_CLASSIFIERS |
586 | #if USE_CHAR_CLASSIFIERS |
|
|
587 | |
577 | ecb_inline int |
588 | ecb_inline int |
578 | Cisalpha (int c) |
589 | Cisalpha (int c) |
579 | { |
590 | { |
580 | return isascii (c) && isalpha (c); |
591 | return isascii (c) && isalpha (c); |
581 | } |
592 | } |
… | |
… | |
639 | "gs", |
650 | "gs", |
640 | "rs", |
651 | "rs", |
641 | "us" |
652 | "us" |
642 | }; |
653 | }; |
643 | |
654 | |
644 | static int |
655 | ecb_cold static int |
645 | is_ascii_name (const char *name, int *pc) |
656 | is_ascii_name (const char *name, int *pc) |
646 | { |
657 | { |
647 | int i; |
658 | int i; |
648 | |
659 | |
649 | for (i = 0; i < 32; i++) |
660 | for (i = 0; i < 32; i++) |
… | |
… | |
668 | |
679 | |
669 | static int file_push (SCHEME_P_ const char *fname); |
680 | static int file_push (SCHEME_P_ const char *fname); |
670 | static void file_pop (SCHEME_P); |
681 | static void file_pop (SCHEME_P); |
671 | static int file_interactive (SCHEME_P); |
682 | static int file_interactive (SCHEME_P); |
672 | ecb_inline int is_one_of (const char *s, int c); |
683 | ecb_inline int is_one_of (const char *s, int c); |
673 | static int alloc_cellseg (SCHEME_P_ int n); |
684 | static int alloc_cellseg (SCHEME_P); |
674 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
685 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
675 | static void finalize_cell (SCHEME_P_ pointer a); |
686 | static void finalize_cell (SCHEME_P_ pointer a); |
676 | static int count_consecutive_cells (pointer x, int needed); |
|
|
677 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
687 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
678 | static pointer mk_number (SCHEME_P_ const num n); |
688 | static pointer mk_number (SCHEME_P_ const num n); |
679 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
689 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
680 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
690 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
681 | static pointer mk_atom (SCHEME_P_ char *q); |
691 | static pointer mk_atom (SCHEME_P_ char *q); |
682 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
692 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
683 | |
693 | |
|
|
694 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
695 | |
684 | #if USE_PORTS |
696 | #if USE_PORTS |
685 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
686 | static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); |
697 | static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); |
687 | static pointer port_from_file (SCHEME_P_ int, int prop); |
698 | static pointer port_from_file (SCHEME_P_ int, int prop); |
688 | static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
699 | static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
689 | static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); |
700 | static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); |
690 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
701 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
691 | static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
702 | static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
692 | static void port_close (SCHEME_P_ pointer p, int flag); |
703 | static void port_close (SCHEME_P_ pointer p, int flag); |
693 | #endif |
704 | #endif |
|
|
705 | |
694 | static void mark (pointer a); |
706 | static void mark (pointer a); |
695 | static void gc (SCHEME_P_ pointer a, pointer b); |
707 | static void gc (SCHEME_P_ pointer a, pointer b); |
696 | static int basic_inchar (port *pt); |
708 | static int basic_inchar (port *pt); |
697 | static int inchar (SCHEME_P); |
709 | static int inchar (SCHEME_P); |
698 | static void backchar (SCHEME_P_ int c); |
710 | static void backchar (SCHEME_P_ int c); |
699 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
711 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
700 | static pointer readstrexp (SCHEME_P_ char delim); |
712 | static pointer readstrexp (SCHEME_P_ char delim); |
701 | ecb_inline int skipspace (SCHEME_P); |
713 | static int skipspace (SCHEME_P); |
702 | static int token (SCHEME_P); |
714 | static int token (SCHEME_P); |
703 | static void printslashstring (SCHEME_P_ char *s, int len); |
715 | static void printslashstring (SCHEME_P_ char *s, int len); |
704 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
716 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
705 | static void printatom (SCHEME_P_ pointer l, int f); |
717 | static void printatom (SCHEME_P_ pointer l, int f); |
706 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
718 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
… | |
… | |
872 | } |
884 | } |
873 | |
885 | |
874 | return ret; |
886 | return ret; |
875 | } |
887 | } |
876 | |
888 | |
877 | #if USE_MATH |
|
|
878 | |
|
|
879 | /* Round to nearest. Round to even if midway */ |
|
|
880 | static double |
|
|
881 | round_per_R5RS (double x) |
|
|
882 | { |
|
|
883 | double fl = floor (x); |
|
|
884 | double ce = ceil (x); |
|
|
885 | double dfl = x - fl; |
|
|
886 | double dce = ce - x; |
|
|
887 | |
|
|
888 | if (dfl > dce) |
|
|
889 | return ce; |
|
|
890 | else if (dfl < dce) |
|
|
891 | return fl; |
|
|
892 | else |
|
|
893 | { |
|
|
894 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
|
|
895 | return fl; |
|
|
896 | else |
|
|
897 | return ce; |
|
|
898 | } |
|
|
899 | } |
|
|
900 | #endif |
|
|
901 | |
|
|
902 | static int |
889 | static int |
903 | is_zero_rvalue (RVALUE x) |
890 | is_zero_rvalue (RVALUE x) |
904 | { |
891 | { |
905 | return x == 0; |
892 | return x == 0; |
906 | #if 0 |
893 | #if 0 |
… | |
… | |
911 | #endif |
898 | #endif |
912 | #endif |
899 | #endif |
913 | } |
900 | } |
914 | |
901 | |
915 | /* allocate new cell segment */ |
902 | /* allocate new cell segment */ |
916 | static int |
903 | ecb_cold static int |
917 | alloc_cellseg (SCHEME_P_ int n) |
904 | alloc_cellseg (SCHEME_P) |
918 | { |
905 | { |
919 | pointer newp; |
906 | struct cell *newp; |
920 | pointer last; |
907 | struct cell *last; |
921 | pointer p; |
908 | struct cell *p; |
922 | char *cp; |
909 | char *cp; |
923 | long i; |
910 | long i; |
924 | int k; |
911 | int k; |
925 | |
912 | |
926 | static int segsize = CELL_SEGSIZE >> 1; |
913 | static int segsize = CELL_SEGSIZE >> 1; |
927 | segsize <<= 1; |
914 | segsize <<= 1; |
928 | |
915 | |
929 | for (k = 0; k < n; k++) |
|
|
930 | { |
|
|
931 | if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1) |
|
|
932 | return k; |
|
|
933 | |
|
|
934 | cp = malloc (segsize * sizeof (struct cell)); |
916 | cp = malloc (segsize * sizeof (struct cell)); |
935 | |
917 | |
936 | if (!cp && USE_ERROR_CHECKING) |
918 | if (!cp && USE_ERROR_CHECKING) |
937 | return k; |
919 | return k; |
938 | |
920 | |
939 | i = ++SCHEME_V->last_cell_seg; |
921 | i = ++SCHEME_V->last_cell_seg; |
940 | SCHEME_V->alloc_seg[i] = cp; |
|
|
941 | |
922 | |
942 | newp = (pointer)cp; |
923 | newp = (struct cell *)cp; |
943 | SCHEME_V->cell_seg[i] = newp; |
924 | SCHEME_V->cell_seg[i] = newp; |
944 | SCHEME_V->cell_segsize[i] = segsize; |
925 | SCHEME_V->cell_segsize[i] = segsize; |
945 | SCHEME_V->fcells += segsize; |
926 | SCHEME_V->fcells += segsize; |
946 | last = newp + segsize - 1; |
927 | last = newp + segsize - 1; |
947 | |
928 | |
948 | for (p = newp; p <= last; p++) |
929 | for (p = newp; p <= last; p++) |
949 | { |
930 | { |
|
|
931 | pointer cp = POINTER (p); |
950 | set_typeflag (p, T_PAIR); |
932 | set_typeflag (cp, T_PAIR); |
951 | set_car (p, NIL); |
933 | set_car (cp, NIL); |
952 | set_cdr (p, p + 1); |
934 | set_cdr (cp, POINTER (p + 1)); |
953 | } |
935 | } |
954 | |
936 | |
955 | set_cdr (last, SCHEME_V->free_cell); |
937 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
956 | SCHEME_V->free_cell = newp; |
938 | SCHEME_V->free_cell = POINTER (newp); |
957 | } |
|
|
958 | |
939 | |
959 | return n; |
940 | return 1; |
960 | } |
941 | } |
961 | |
942 | |
962 | /* get new cell. parameter a, b is marked by gc. */ |
943 | /* get new cell. parameter a, b is marked by gc. */ |
963 | ecb_inline pointer |
944 | ecb_inline pointer |
964 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
945 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
… | |
… | |
968 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
949 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
969 | return S_SINK; |
950 | return S_SINK; |
970 | |
951 | |
971 | if (SCHEME_V->free_cell == NIL) |
952 | if (SCHEME_V->free_cell == NIL) |
972 | { |
953 | { |
973 | 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; |
974 | |
955 | |
975 | gc (SCHEME_A_ a, b); |
956 | gc (SCHEME_A_ a, b); |
976 | |
957 | |
977 | 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) |
978 | { |
959 | { |
979 | /* 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 */ |
980 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
961 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
981 | { |
962 | { |
982 | #if USE_ERROR_CHECKING |
963 | #if USE_ERROR_CHECKING |
983 | SCHEME_V->no_memory = 1; |
964 | SCHEME_V->no_memory = 1; |
984 | return S_SINK; |
965 | return S_SINK; |
985 | #endif |
966 | #endif |
… | |
… | |
997 | } |
978 | } |
998 | } |
979 | } |
999 | |
980 | |
1000 | /* To retain recent allocs before interpreter knows about them - |
981 | /* To retain recent allocs before interpreter knows about them - |
1001 | Tehom */ |
982 | Tehom */ |
1002 | |
983 | ecb_hot static void |
1003 | static void |
|
|
1004 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
984 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
1005 | { |
985 | { |
1006 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
986 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1007 | |
987 | |
1008 | set_typeflag (holder, T_PAIR); |
988 | set_typeflag (holder, T_PAIR); |
… | |
… | |
1010 | set_car (holder, recent); |
990 | set_car (holder, recent); |
1011 | set_cdr (holder, car (S_SINK)); |
991 | set_cdr (holder, car (S_SINK)); |
1012 | set_car (S_SINK, holder); |
992 | set_car (S_SINK, holder); |
1013 | } |
993 | } |
1014 | |
994 | |
1015 | static pointer |
995 | ecb_hot static pointer |
1016 | get_cell (SCHEME_P_ pointer a, pointer b) |
996 | get_cell (SCHEME_P_ pointer a, pointer b) |
1017 | { |
997 | { |
1018 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
998 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
1019 | |
999 | |
1020 | /* 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 |
… | |
… | |
1029 | } |
1009 | } |
1030 | |
1010 | |
1031 | static pointer |
1011 | static pointer |
1032 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1012 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1033 | { |
1013 | { |
1034 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1014 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
1035 | pointer *e = malloc (len * sizeof (pointer)); |
1015 | pointer *e = malloc (len * sizeof (pointer)); |
1036 | |
1016 | |
1037 | if (!e && USE_ERROR_CHECKING) |
1017 | if (!e && USE_ERROR_CHECKING) |
1038 | return S_SINK; |
1018 | return S_SINK; |
1039 | |
1019 | |
1040 | /* Record it as a vector so that gc understands it. */ |
1020 | /* Record it as a vector so that gc understands it. */ |
1041 | set_typeflag (v, T_VECTOR | T_ATOM); |
1021 | set_typeflag (v, T_VECTOR | T_ATOM); |
1042 | |
1022 | |
1043 | v->object.vector.vvalue = e; |
1023 | CELL(v)->object.vector.vvalue = e; |
1044 | v->object.vector.length = len; |
1024 | CELL(v)->object.vector.length = len; |
1045 | fill_vector (v, 0, init); |
1025 | fill_vector (v, 0, init); |
1046 | push_recent_alloc (SCHEME_A_ v, NIL); |
1026 | push_recent_alloc (SCHEME_A_ v, NIL); |
1047 | |
1027 | |
1048 | return v; |
1028 | return v; |
1049 | } |
1029 | } |
… | |
… | |
1058 | static void |
1038 | static void |
1059 | check_cell_alloced (pointer p, int expect_alloced) |
1039 | check_cell_alloced (pointer p, int expect_alloced) |
1060 | { |
1040 | { |
1061 | /* 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. */ |
1062 | if (typeflag (p) & !expect_alloced) |
1042 | if (typeflag (p) & !expect_alloced) |
1063 | xwrstr ("Cell is already allocated!\n"); |
1043 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1064 | |
1044 | |
1065 | if (!(typeflag (p)) & expect_alloced) |
1045 | if (!(typeflag (p)) & expect_alloced) |
1066 | xwrstr ("Cell is not allocated!\n"); |
1046 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1067 | } |
1047 | } |
1068 | |
1048 | |
1069 | static void |
1049 | static void |
1070 | check_range_alloced (pointer p, int n, int expect_alloced) |
1050 | check_range_alloced (pointer p, int n, int expect_alloced) |
1071 | { |
1051 | { |
… | |
… | |
1077 | #endif |
1057 | #endif |
1078 | |
1058 | |
1079 | /* Medium level cell allocation */ |
1059 | /* Medium level cell allocation */ |
1080 | |
1060 | |
1081 | /* get new cons cell */ |
1061 | /* get new cons cell */ |
1082 | pointer |
1062 | ecb_hot static pointer |
1083 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1063 | xcons (SCHEME_P_ pointer a, pointer b) |
1084 | { |
1064 | { |
1085 | pointer x = get_cell (SCHEME_A_ a, b); |
1065 | pointer x = get_cell (SCHEME_A_ a, b); |
1086 | |
1066 | |
1087 | set_typeflag (x, T_PAIR); |
1067 | set_typeflag (x, T_PAIR); |
1088 | |
|
|
1089 | if (immutable) |
|
|
1090 | setimmutable (x); |
|
|
1091 | |
1068 | |
1092 | set_car (x, a); |
1069 | set_car (x, a); |
1093 | set_cdr (x, b); |
1070 | set_cdr (x, b); |
1094 | |
1071 | |
1095 | return x; |
1072 | return x; |
1096 | } |
1073 | } |
1097 | |
1074 | |
1098 | /* ========== oblist implementation ========== */ |
|
|
1099 | |
|
|
1100 | static pointer |
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 |
1101 | generate_symbol (SCHEME_P_ const char *name) |
1087 | generate_symbol (SCHEME_P_ const char *name) |
1102 | { |
1088 | { |
1103 | pointer x = mk_string (SCHEME_A_ name); |
1089 | pointer x = mk_string (SCHEME_A_ name); |
1104 | setimmutable (x); |
1090 | setimmutable (x); |
1105 | set_typeflag (x, T_SYMBOL | T_ATOM); |
1091 | set_typeflag (x, T_SYMBOL | T_ATOM); |
1106 | return x; |
1092 | return x; |
1107 | } |
1093 | } |
1108 | |
1094 | |
|
|
1095 | /* ========== oblist implementation ========== */ |
|
|
1096 | |
1109 | #ifndef USE_OBJECT_LIST |
1097 | #ifndef USE_OBJECT_LIST |
1110 | |
1098 | |
1111 | static int |
1099 | static int |
1112 | hash_fn (const char *key, int table_size) |
1100 | hash_fn (const char *key, int table_size) |
1113 | { |
1101 | { |
1114 | const unsigned char *p = key; |
1102 | const unsigned char *p = (unsigned char *)key; |
1115 | uint32_t hash = 2166136261; |
1103 | uint32_t hash = 2166136261U; |
1116 | |
1104 | |
1117 | while (*p) |
1105 | while (*p) |
1118 | hash = (hash ^ *p++) * 16777619; |
1106 | hash = (hash ^ *p++) * 16777619; |
1119 | |
1107 | |
1120 | return hash % table_size; |
1108 | return hash % table_size; |
1121 | } |
1109 | } |
1122 | |
1110 | |
1123 | static pointer |
1111 | ecb_cold static pointer |
1124 | oblist_initial_value (SCHEME_P) |
1112 | oblist_initial_value (SCHEME_P) |
1125 | { |
1113 | { |
1126 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1114 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1127 | } |
1115 | } |
1128 | |
1116 | |
1129 | /* returns the new symbol */ |
1117 | /* returns the new symbol */ |
1130 | static pointer |
1118 | ecb_cold static pointer |
1131 | oblist_add_by_name (SCHEME_P_ const char *name) |
1119 | oblist_add_by_name (SCHEME_P_ const char *name) |
1132 | { |
1120 | { |
1133 | pointer x = generate_symbol (SCHEME_A_ name); |
1121 | pointer x = generate_symbol (SCHEME_A_ name); |
1134 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1122 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1135 | 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))); |
1136 | return x; |
1124 | return x; |
1137 | } |
1125 | } |
1138 | |
1126 | |
1139 | ecb_inline pointer |
1127 | ecb_cold static pointer |
1140 | oblist_find_by_name (SCHEME_P_ const char *name) |
1128 | oblist_find_by_name (SCHEME_P_ const char *name) |
1141 | { |
1129 | { |
1142 | int location; |
1130 | int location; |
1143 | pointer x; |
1131 | pointer x; |
1144 | char *s; |
1132 | char *s; |
… | |
… | |
1155 | } |
1143 | } |
1156 | |
1144 | |
1157 | return NIL; |
1145 | return NIL; |
1158 | } |
1146 | } |
1159 | |
1147 | |
1160 | static pointer |
1148 | ecb_cold static pointer |
1161 | oblist_all_symbols (SCHEME_P) |
1149 | oblist_all_symbols (SCHEME_P) |
1162 | { |
1150 | { |
1163 | int i; |
1151 | int i; |
1164 | pointer x; |
1152 | pointer x; |
1165 | pointer ob_list = NIL; |
1153 | pointer ob_list = NIL; |
… | |
… | |
1171 | return ob_list; |
1159 | return ob_list; |
1172 | } |
1160 | } |
1173 | |
1161 | |
1174 | #else |
1162 | #else |
1175 | |
1163 | |
1176 | static pointer |
1164 | ecb_cold static pointer |
1177 | oblist_initial_value (SCHEME_P) |
1165 | oblist_initial_value (SCHEME_P) |
1178 | { |
1166 | { |
1179 | return NIL; |
1167 | return NIL; |
1180 | } |
1168 | } |
1181 | |
1169 | |
1182 | ecb_inline pointer |
1170 | ecb_cold static pointer |
1183 | oblist_find_by_name (SCHEME_P_ const char *name) |
1171 | oblist_find_by_name (SCHEME_P_ const char *name) |
1184 | { |
1172 | { |
1185 | pointer x; |
1173 | pointer x; |
1186 | char *s; |
1174 | char *s; |
1187 | |
1175 | |
… | |
… | |
1196 | |
1184 | |
1197 | return NIL; |
1185 | return NIL; |
1198 | } |
1186 | } |
1199 | |
1187 | |
1200 | /* returns the new symbol */ |
1188 | /* returns the new symbol */ |
1201 | static pointer |
1189 | ecb_cold static pointer |
1202 | oblist_add_by_name (SCHEME_P_ const char *name) |
1190 | oblist_add_by_name (SCHEME_P_ const char *name) |
1203 | { |
1191 | { |
1204 | pointer x = mk_string (SCHEME_A_ name); |
1192 | pointer x = generate_symbol (SCHEME_A_ name); |
1205 | set_typeflag (x, T_SYMBOL); |
|
|
1206 | setimmutable (x); |
|
|
1207 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1193 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1208 | return x; |
1194 | return x; |
1209 | } |
1195 | } |
1210 | |
1196 | |
1211 | static pointer |
1197 | ecb_cold static pointer |
1212 | oblist_all_symbols (SCHEME_P) |
1198 | oblist_all_symbols (SCHEME_P) |
1213 | { |
1199 | { |
1214 | return SCHEME_V->oblist; |
1200 | return SCHEME_V->oblist; |
1215 | } |
1201 | } |
1216 | |
1202 | |
1217 | #endif |
1203 | #endif |
1218 | |
1204 | |
1219 | #if USE_PORTS |
|
|
1220 | static pointer |
1205 | ecb_cold static pointer |
1221 | mk_port (SCHEME_P_ port *p) |
1206 | mk_port (SCHEME_P_ port *p) |
1222 | { |
1207 | { |
1223 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1208 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1224 | |
1209 | |
1225 | set_typeflag (x, T_PORT | T_ATOM); |
1210 | set_typeflag (x, T_PORT | T_ATOM); |
1226 | x->object.port = p; |
1211 | set_port (x, p); |
1227 | |
1212 | |
1228 | return x; |
1213 | return x; |
1229 | } |
1214 | } |
1230 | #endif |
|
|
1231 | |
1215 | |
1232 | pointer |
1216 | ecb_cold pointer |
1233 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1217 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1234 | { |
1218 | { |
1235 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1219 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1236 | |
1220 | |
1237 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1221 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1238 | x->object.ff = f; |
1222 | CELL(x)->object.ff = f; |
1239 | |
1223 | |
1240 | return x; |
1224 | return x; |
1241 | } |
1225 | } |
1242 | |
1226 | |
1243 | INTERFACE pointer |
1227 | INTERFACE pointer |
1244 | mk_character (SCHEME_P_ int c) |
1228 | mk_character (SCHEME_P_ int c) |
1245 | { |
1229 | { |
1246 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1230 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1247 | |
1231 | |
1248 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1232 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1249 | set_ivalue (x, c & 0xff); |
1233 | set_ivalue (x, c & 0xff); |
1250 | |
1234 | |
1251 | return x; |
1235 | return x; |
1252 | } |
1236 | } |
1253 | |
1237 | |
1254 | /* get number atom (integer) */ |
1238 | /* get number atom (integer) */ |
1255 | INTERFACE pointer |
1239 | INTERFACE pointer |
1256 | mk_integer (SCHEME_P_ long n) |
1240 | mk_integer (SCHEME_P_ long n) |
1257 | { |
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 | { |
1258 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1252 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1259 | |
1253 | |
1260 | 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 */ |
1261 | set_ivalue (x, n); |
1256 | set_ivalue (x, n); |
1262 | |
1257 | |
|
|
1258 | *pp = x; |
|
|
1259 | } |
|
|
1260 | |
1263 | return x; |
1261 | return *pp; |
1264 | } |
1262 | } |
1265 | |
1263 | |
1266 | INTERFACE pointer |
1264 | INTERFACE pointer |
1267 | mk_real (SCHEME_P_ RVALUE n) |
1265 | mk_real (SCHEME_P_ RVALUE n) |
1268 | { |
1266 | { |
1269 | #if USE_REAL |
1267 | #if USE_REAL |
1270 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1268 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1271 | |
1269 | |
1272 | set_typeflag (x, (T_REAL | T_ATOM)); |
1270 | set_typeflag (x, T_REAL | T_ATOM); |
1273 | set_rvalue (x, n); |
1271 | set_rvalue (x, n); |
1274 | |
1272 | |
1275 | return x; |
1273 | return x; |
1276 | #else |
1274 | #else |
1277 | return mk_integer (SCHEME_A_ n); |
1275 | return mk_integer (SCHEME_A_ n); |
… | |
… | |
1388 | x = oblist_add_by_name (SCHEME_A_ name); |
1386 | x = oblist_add_by_name (SCHEME_A_ name); |
1389 | |
1387 | |
1390 | return x; |
1388 | return x; |
1391 | } |
1389 | } |
1392 | |
1390 | |
1393 | INTERFACE pointer |
1391 | ecb_cold INTERFACE pointer |
1394 | gensym (SCHEME_P) |
1392 | gensym (SCHEME_P) |
1395 | { |
1393 | { |
1396 | pointer x; |
1394 | pointer x; |
1397 | char name[40] = "gensym-"; |
1395 | char name[40] = "gensym-"; |
1398 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1396 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1399 | |
1397 | |
1400 | return generate_symbol (SCHEME_A_ name); |
1398 | return generate_symbol (SCHEME_A_ name); |
1401 | } |
1399 | } |
1402 | |
1400 | |
|
|
1401 | static int |
|
|
1402 | is_gensym (SCHEME_P_ pointer x) |
|
|
1403 | { |
|
|
1404 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
|
|
1405 | } |
|
|
1406 | |
1403 | /* make symbol or number atom from string */ |
1407 | /* make symbol or number atom from string */ |
1404 | static pointer |
1408 | ecb_cold static pointer |
1405 | mk_atom (SCHEME_P_ char *q) |
1409 | mk_atom (SCHEME_P_ char *q) |
1406 | { |
1410 | { |
1407 | char c, *p; |
1411 | char c, *p; |
1408 | int has_dec_point = 0; |
1412 | int has_dec_point = 0; |
1409 | int has_fp_exp = 0; |
1413 | int has_fp_exp = 0; |
… | |
… | |
1480 | |
1484 | |
1481 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1485 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1482 | } |
1486 | } |
1483 | |
1487 | |
1484 | /* make constant */ |
1488 | /* make constant */ |
1485 | static pointer |
1489 | ecb_cold static pointer |
1486 | mk_sharp_const (SCHEME_P_ char *name) |
1490 | mk_sharp_const (SCHEME_P_ char *name) |
1487 | { |
1491 | { |
1488 | if (!strcmp (name, "t")) |
1492 | if (!strcmp (name, "t")) |
1489 | return S_T; |
1493 | return S_T; |
1490 | else if (!strcmp (name, "f")) |
1494 | else if (!strcmp (name, "f")) |
1491 | return S_F; |
1495 | return S_F; |
1492 | else if (*name == '\\') /* #\w (character) */ |
1496 | else if (*name == '\\') /* #\w (character) */ |
1493 | { |
1497 | { |
1494 | int c; |
1498 | int c; |
1495 | |
1499 | |
|
|
1500 | // TODO: optimise |
1496 | if (stricmp (name + 1, "space") == 0) |
1501 | if (stricmp (name + 1, "space") == 0) |
1497 | c = ' '; |
1502 | c = ' '; |
1498 | else if (stricmp (name + 1, "newline") == 0) |
1503 | else if (stricmp (name + 1, "newline") == 0) |
1499 | c = '\n'; |
1504 | c = '\n'; |
1500 | else if (stricmp (name + 1, "return") == 0) |
1505 | else if (stricmp (name + 1, "return") == 0) |
1501 | c = '\r'; |
1506 | c = '\r'; |
1502 | else if (stricmp (name + 1, "tab") == 0) |
1507 | else if (stricmp (name + 1, "tab") == 0) |
1503 | 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; |
1504 | else if (name[1] == 'x' && name[2] != 0) |
1519 | else if (name[1] == 'x' && name[2] != 0) |
1505 | { |
1520 | { |
1506 | long c1 = strtol (name + 2, 0, 16); |
1521 | long c1 = strtol (name + 2, 0, 16); |
1507 | |
1522 | |
1508 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1523 | if (0 <= c1 && c1 <= UCHAR_MAX) |
… | |
… | |
1533 | return NIL; |
1548 | return NIL; |
1534 | } |
1549 | } |
1535 | } |
1550 | } |
1536 | |
1551 | |
1537 | /* ========== 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 | } |
1538 | |
1572 | |
1539 | /*-- |
1573 | /*-- |
1540 | * 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, |
1541 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1575 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1542 | * for marking. |
1576 | * for marking. |
1543 | * |
1577 | * |
1544 | * The exception is vectors - vectors are currently marked recursively, |
1578 | * The exception is vectors - vectors are currently marked recursively, |
1545 | * 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 |
1546 | * word of context in the vector |
1580 | * word of context in the vector |
1547 | */ |
1581 | */ |
1548 | static void |
1582 | ecb_hot static void |
1549 | mark (pointer a) |
1583 | mark (pointer a) |
1550 | { |
1584 | { |
1551 | pointer t, q, p; |
1585 | pointer t, q, p; |
1552 | |
1586 | |
1553 | t = 0; |
1587 | t = 0; |
… | |
… | |
1610 | p = q; |
1644 | p = q; |
1611 | goto E6; |
1645 | goto E6; |
1612 | } |
1646 | } |
1613 | } |
1647 | } |
1614 | |
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 | |
1615 | /* garbage collection. parameter a, b is marked. */ |
1691 | /* garbage collection. parameter a, b is marked. */ |
1616 | static void |
1692 | ecb_cold static void |
1617 | gc (SCHEME_P_ pointer a, pointer b) |
1693 | gc (SCHEME_P_ pointer a, pointer b) |
1618 | { |
1694 | { |
1619 | pointer p; |
|
|
1620 | int i; |
1695 | int i; |
1621 | |
1696 | |
1622 | if (SCHEME_V->gc_verbose) |
1697 | if (SCHEME_V->gc_verbose) |
1623 | putstr (SCHEME_A_ "gc..."); |
1698 | putstr (SCHEME_A_ "gc..."); |
1624 | |
1699 | |
… | |
… | |
1640 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1715 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1641 | mark (car (S_SINK)); |
1716 | mark (car (S_SINK)); |
1642 | /* Mark any older stuff above nested C calls */ |
1717 | /* Mark any older stuff above nested C calls */ |
1643 | mark (SCHEME_V->c_nest); |
1718 | mark (SCHEME_V->c_nest); |
1644 | |
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 | |
1645 | /* mark variables a, b */ |
1727 | /* mark variables a, b */ |
1646 | mark (a); |
1728 | mark (a); |
1647 | mark (b); |
1729 | mark (b); |
1648 | |
1730 | |
1649 | /* garbage collect */ |
1731 | /* garbage collect */ |
1650 | clrmark (NIL); |
1732 | clrmark (NIL); |
1651 | SCHEME_V->fcells = 0; |
1733 | SCHEME_V->fcells = 0; |
1652 | SCHEME_V->free_cell = NIL; |
1734 | SCHEME_V->free_cell = NIL; |
1653 | |
1735 | |
1654 | /* free-list is kept sorted by address so as to maintain consecutive |
|
|
1655 | ranges, if possible, for use with vectors. Here we scan the cells |
|
|
1656 | (which are also kept sorted by address) downwards to build the |
|
|
1657 | free-list in sorted order. |
|
|
1658 | */ |
|
|
1659 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1660 | { |
|
|
1661 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1662 | |
|
|
1663 | while (--p >= SCHEME_V->cell_seg[i]) |
|
|
1664 | { |
|
|
1665 | if (is_mark (p)) |
|
|
1666 | clrmark (p); |
|
|
1667 | else |
|
|
1668 | { |
|
|
1669 | /* reclaim cell */ |
|
|
1670 | if (typeflag (p) != T_PAIR) |
|
|
1671 | { |
|
|
1672 | finalize_cell (SCHEME_A_ p); |
|
|
1673 | set_typeflag (p, T_PAIR); |
|
|
1674 | set_car (p, NIL); |
|
|
1675 | } |
|
|
1676 | |
|
|
1677 | ++SCHEME_V->fcells; |
|
|
1678 | set_cdr (p, SCHEME_V->free_cell); |
|
|
1679 | SCHEME_V->free_cell = p; |
|
|
1680 | } |
|
|
1681 | } |
|
|
1682 | } |
|
|
1683 | |
|
|
1684 | if (SCHEME_V->gc_verbose) |
1736 | if (SCHEME_V->gc_verbose) |
1685 | { |
1737 | putstr (SCHEME_A_ "freeing..."); |
1686 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
|
|
1687 | } |
|
|
1688 | } |
|
|
1689 | |
1738 | |
1690 | static void |
1739 | gc_free (SCHEME_A); |
1691 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1692 | { |
|
|
1693 | /* TODO, fast bitmap check? */ |
|
|
1694 | if (is_string (a) || is_symbol (a)) |
|
|
1695 | free (strvalue (a)); |
|
|
1696 | else if (is_vector (a)) |
|
|
1697 | free (vecvalue (a)); |
|
|
1698 | #if USE_PORTS |
|
|
1699 | else if (is_port (a)) |
|
|
1700 | { |
|
|
1701 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
|
|
1702 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1703 | |
|
|
1704 | free (a->object.port); |
|
|
1705 | } |
|
|
1706 | #endif |
|
|
1707 | } |
1740 | } |
1708 | |
1741 | |
1709 | /* ========== Routines for Reading ========== */ |
1742 | /* ========== Routines for Reading ========== */ |
1710 | |
1743 | |
1711 | static int |
1744 | ecb_cold static int |
1712 | file_push (SCHEME_P_ const char *fname) |
1745 | file_push (SCHEME_P_ const char *fname) |
1713 | { |
1746 | { |
1714 | #if USE_PORTS |
|
|
1715 | int fin; |
1747 | int fin; |
1716 | |
1748 | |
1717 | if (SCHEME_V->file_i == MAXFIL - 1) |
1749 | if (SCHEME_V->file_i == MAXFIL - 1) |
1718 | return 0; |
1750 | return 0; |
1719 | |
1751 | |
… | |
… | |
1725 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1757 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1726 | 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; |
1727 | 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; |
1728 | 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; |
1729 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1761 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1730 | 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); |
1731 | |
1763 | |
1732 | #if SHOW_ERROR_LINE |
1764 | #if SHOW_ERROR_LINE |
1733 | 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; |
1734 | |
1766 | |
1735 | if (fname) |
1767 | if (fname) |
1736 | 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); |
1737 | #endif |
1769 | #endif |
1738 | } |
1770 | } |
1739 | |
1771 | |
1740 | return fin >= 0; |
1772 | return fin >= 0; |
1741 | |
|
|
1742 | #else |
|
|
1743 | return 1; |
|
|
1744 | #endif |
|
|
1745 | } |
1773 | } |
1746 | |
1774 | |
1747 | static void |
1775 | ecb_cold static void |
1748 | file_pop (SCHEME_P) |
1776 | file_pop (SCHEME_P) |
1749 | { |
1777 | { |
1750 | if (SCHEME_V->file_i != 0) |
1778 | if (SCHEME_V->file_i != 0) |
1751 | { |
1779 | { |
1752 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1780 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1753 | #if USE_PORTS |
1781 | #if USE_PORTS |
1754 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1782 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1755 | #endif |
1783 | #endif |
1756 | SCHEME_V->file_i--; |
1784 | SCHEME_V->file_i--; |
1757 | 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); |
1758 | } |
1786 | } |
1759 | } |
1787 | } |
1760 | |
1788 | |
1761 | static int |
1789 | ecb_cold static int |
1762 | file_interactive (SCHEME_P) |
1790 | file_interactive (SCHEME_P) |
1763 | { |
1791 | { |
1764 | #if USE_PORTS |
1792 | #if USE_PORTS |
1765 | return SCHEME_V->file_i == 0 |
1793 | return SCHEME_V->file_i == 0 |
1766 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1794 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1767 | && (SCHEME_V->inport->object.port->kind & port_file); |
1795 | && (port (SCHEME_V->inport)->kind & port_file); |
1768 | #else |
1796 | #else |
1769 | return 0; |
1797 | return 0; |
1770 | #endif |
1798 | #endif |
1771 | } |
1799 | } |
1772 | |
1800 | |
1773 | #if USE_PORTS |
1801 | #if USE_PORTS |
1774 | static port * |
1802 | ecb_cold static port * |
1775 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1803 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1776 | { |
1804 | { |
1777 | int fd; |
1805 | int fd; |
1778 | int flags; |
1806 | int flags; |
1779 | char *rw; |
1807 | char *rw; |
… | |
… | |
1802 | # endif |
1830 | # endif |
1803 | |
1831 | |
1804 | return pt; |
1832 | return pt; |
1805 | } |
1833 | } |
1806 | |
1834 | |
1807 | static pointer |
1835 | ecb_cold static pointer |
1808 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1836 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1809 | { |
1837 | { |
1810 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1838 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1811 | |
1839 | |
1812 | if (!pt && USE_ERROR_CHECKING) |
1840 | if (!pt && USE_ERROR_CHECKING) |
1813 | return NIL; |
1841 | return NIL; |
1814 | |
1842 | |
1815 | return mk_port (SCHEME_A_ pt); |
1843 | return mk_port (SCHEME_A_ pt); |
1816 | } |
1844 | } |
1817 | |
1845 | |
1818 | static port * |
1846 | ecb_cold static port * |
1819 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1847 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1820 | { |
1848 | { |
1821 | port *pt = malloc (sizeof *pt); |
1849 | port *pt = malloc (sizeof *pt); |
1822 | |
1850 | |
1823 | if (!pt && USE_ERROR_CHECKING) |
1851 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1828 | pt->rep.stdio.file = f; |
1856 | pt->rep.stdio.file = f; |
1829 | pt->rep.stdio.closeit = 0; |
1857 | pt->rep.stdio.closeit = 0; |
1830 | return pt; |
1858 | return pt; |
1831 | } |
1859 | } |
1832 | |
1860 | |
1833 | static pointer |
1861 | ecb_cold static pointer |
1834 | port_from_file (SCHEME_P_ int f, int prop) |
1862 | port_from_file (SCHEME_P_ int f, int prop) |
1835 | { |
1863 | { |
1836 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1864 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1837 | |
1865 | |
1838 | if (!pt && USE_ERROR_CHECKING) |
1866 | if (!pt && USE_ERROR_CHECKING) |
1839 | return NIL; |
1867 | return NIL; |
1840 | |
1868 | |
1841 | return mk_port (SCHEME_A_ pt); |
1869 | return mk_port (SCHEME_A_ pt); |
1842 | } |
1870 | } |
1843 | |
1871 | |
1844 | static port * |
1872 | ecb_cold static port * |
1845 | 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) |
1846 | { |
1874 | { |
1847 | port *pt = malloc (sizeof (port)); |
1875 | port *pt = malloc (sizeof (port)); |
1848 | |
1876 | |
1849 | if (!pt && USE_ERROR_CHECKING) |
1877 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1855 | pt->rep.string.curr = start; |
1883 | pt->rep.string.curr = start; |
1856 | pt->rep.string.past_the_end = past_the_end; |
1884 | pt->rep.string.past_the_end = past_the_end; |
1857 | return pt; |
1885 | return pt; |
1858 | } |
1886 | } |
1859 | |
1887 | |
1860 | static pointer |
1888 | ecb_cold static pointer |
1861 | 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) |
1862 | { |
1890 | { |
1863 | 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); |
1864 | |
1892 | |
1865 | if (!pt && USE_ERROR_CHECKING) |
1893 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1868 | return mk_port (SCHEME_A_ pt); |
1896 | return mk_port (SCHEME_A_ pt); |
1869 | } |
1897 | } |
1870 | |
1898 | |
1871 | # define BLOCK_SIZE 256 |
1899 | # define BLOCK_SIZE 256 |
1872 | |
1900 | |
1873 | static port * |
1901 | ecb_cold static port * |
1874 | port_rep_from_scratch (SCHEME_P) |
1902 | port_rep_from_scratch (SCHEME_P) |
1875 | { |
1903 | { |
1876 | char *start; |
1904 | char *start; |
1877 | port *pt = malloc (sizeof (port)); |
1905 | port *pt = malloc (sizeof (port)); |
1878 | |
1906 | |
… | |
… | |
1892 | pt->rep.string.curr = start; |
1920 | pt->rep.string.curr = start; |
1893 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1921 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1894 | return pt; |
1922 | return pt; |
1895 | } |
1923 | } |
1896 | |
1924 | |
1897 | static pointer |
1925 | ecb_cold static pointer |
1898 | port_from_scratch (SCHEME_P) |
1926 | port_from_scratch (SCHEME_P) |
1899 | { |
1927 | { |
1900 | port *pt = port_rep_from_scratch (SCHEME_A); |
1928 | port *pt = port_rep_from_scratch (SCHEME_A); |
1901 | |
1929 | |
1902 | if (!pt && USE_ERROR_CHECKING) |
1930 | if (!pt && USE_ERROR_CHECKING) |
1903 | return NIL; |
1931 | return NIL; |
1904 | |
1932 | |
1905 | return mk_port (SCHEME_A_ pt); |
1933 | return mk_port (SCHEME_A_ pt); |
1906 | } |
1934 | } |
1907 | |
1935 | |
1908 | static void |
1936 | ecb_cold static void |
1909 | port_close (SCHEME_P_ pointer p, int flag) |
1937 | port_close (SCHEME_P_ pointer p, int flag) |
1910 | { |
1938 | { |
1911 | port *pt = p->object.port; |
1939 | port *pt = port (p); |
1912 | |
1940 | |
1913 | pt->kind &= ~flag; |
1941 | pt->kind &= ~flag; |
1914 | |
1942 | |
1915 | if ((pt->kind & (port_input | port_output)) == 0) |
1943 | if ((pt->kind & (port_input | port_output)) == 0) |
1916 | { |
1944 | { |
… | |
… | |
1933 | } |
1961 | } |
1934 | } |
1962 | } |
1935 | #endif |
1963 | #endif |
1936 | |
1964 | |
1937 | /* get new character from input file */ |
1965 | /* get new character from input file */ |
1938 | static int |
1966 | ecb_cold static int |
1939 | inchar (SCHEME_P) |
1967 | inchar (SCHEME_P) |
1940 | { |
1968 | { |
1941 | int c; |
1969 | int c; |
1942 | port *pt; |
1970 | port *pt = port (SCHEME_V->inport); |
1943 | |
|
|
1944 | pt = SCHEME_V->inport->object.port; |
|
|
1945 | |
1971 | |
1946 | if (pt->kind & port_saw_EOF) |
1972 | if (pt->kind & port_saw_EOF) |
1947 | return EOF; |
1973 | return EOF; |
1948 | |
1974 | |
1949 | c = basic_inchar (pt); |
1975 | c = basic_inchar (pt); |
… | |
… | |
1959 | } |
1985 | } |
1960 | |
1986 | |
1961 | return c; |
1987 | return c; |
1962 | } |
1988 | } |
1963 | |
1989 | |
1964 | static int ungot = -1; |
1990 | ecb_cold static int |
1965 | |
|
|
1966 | static int |
|
|
1967 | basic_inchar (port *pt) |
1991 | basic_inchar (port *pt) |
1968 | { |
1992 | { |
1969 | #if USE_PORTS |
|
|
1970 | if (pt->unget != -1) |
1993 | if (pt->unget != -1) |
1971 | { |
1994 | { |
1972 | int r = pt->unget; |
1995 | int r = pt->unget; |
1973 | pt->unget = -1; |
1996 | pt->unget = -1; |
1974 | return r; |
1997 | return r; |
1975 | } |
1998 | } |
1976 | |
1999 | |
|
|
2000 | #if USE_PORTS |
1977 | if (pt->kind & port_file) |
2001 | if (pt->kind & port_file) |
1978 | { |
2002 | { |
1979 | char c; |
2003 | char c; |
1980 | |
2004 | |
1981 | if (!read (pt->rep.stdio.file, &c, 1)) |
2005 | if (!read (pt->rep.stdio.file, &c, 1)) |
… | |
… | |
1989 | return EOF; |
2013 | return EOF; |
1990 | else |
2014 | else |
1991 | return *pt->rep.string.curr++; |
2015 | return *pt->rep.string.curr++; |
1992 | } |
2016 | } |
1993 | #else |
2017 | #else |
1994 | if (ungot == -1) |
|
|
1995 | { |
|
|
1996 | char c; |
2018 | char c; |
1997 | if (!read (0, &c, 1)) |
2019 | |
|
|
2020 | if (!read (pt->rep.stdio.file, &c, 1)) |
1998 | return EOF; |
2021 | return EOF; |
1999 | |
2022 | |
2000 | ungot = c; |
|
|
2001 | } |
|
|
2002 | |
|
|
2003 | { |
|
|
2004 | int r = ungot; |
|
|
2005 | ungot = -1; |
|
|
2006 | return r; |
2023 | return c; |
2007 | } |
|
|
2008 | #endif |
2024 | #endif |
2009 | } |
2025 | } |
2010 | |
2026 | |
2011 | /* back character to input buffer */ |
2027 | /* back character to input buffer */ |
2012 | static void |
2028 | ecb_cold static void |
2013 | backchar (SCHEME_P_ int c) |
2029 | backchar (SCHEME_P_ int c) |
2014 | { |
2030 | { |
2015 | #if USE_PORTS |
2031 | port *pt = port (SCHEME_V->inport); |
2016 | port *pt; |
|
|
2017 | |
2032 | |
2018 | if (c == EOF) |
2033 | if (c == EOF) |
2019 | return; |
2034 | return; |
2020 | |
2035 | |
2021 | pt = SCHEME_V->inport->object.port; |
|
|
2022 | pt->unget = c; |
2036 | pt->unget = c; |
2023 | #else |
|
|
2024 | if (c == EOF) |
|
|
2025 | return; |
|
|
2026 | |
|
|
2027 | ungot = c; |
|
|
2028 | #endif |
|
|
2029 | } |
2037 | } |
2030 | |
2038 | |
2031 | #if USE_PORTS |
2039 | #if USE_PORTS |
2032 | static int |
2040 | ecb_cold static int |
2033 | realloc_port_string (SCHEME_P_ port *p) |
2041 | realloc_port_string (SCHEME_P_ port *p) |
2034 | { |
2042 | { |
2035 | char *start = p->rep.string.start; |
2043 | char *start = p->rep.string.start; |
2036 | 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; |
2037 | char *str = malloc (new_size); |
2045 | char *str = malloc (new_size); |
… | |
… | |
2050 | else |
2058 | else |
2051 | return 0; |
2059 | return 0; |
2052 | } |
2060 | } |
2053 | #endif |
2061 | #endif |
2054 | |
2062 | |
2055 | INTERFACE void |
2063 | ecb_cold static void |
2056 | putstr (SCHEME_P_ const char *s) |
2064 | putchars (SCHEME_P_ const char *s, int len) |
2057 | { |
2065 | { |
|
|
2066 | port *pt = port (SCHEME_V->outport); |
|
|
2067 | |
2058 | #if USE_PORTS |
2068 | #if USE_PORTS |
2059 | port *pt = SCHEME_V->outport->object.port; |
|
|
2060 | |
|
|
2061 | if (pt->kind & port_file) |
|
|
2062 | write (pt->rep.stdio.file, s, strlen (s)); |
|
|
2063 | else |
|
|
2064 | for (; *s; s++) |
|
|
2065 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2066 | *pt->rep.string.curr++ = *s; |
|
|
2067 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2068 | *pt->rep.string.curr++ = *s; |
|
|
2069 | |
|
|
2070 | #else |
|
|
2071 | xwrstr (s); |
|
|
2072 | #endif |
|
|
2073 | } |
|
|
2074 | |
|
|
2075 | static void |
|
|
2076 | putchars (SCHEME_P_ const char *s, int len) |
|
|
2077 | { |
|
|
2078 | #if USE_PORTS |
|
|
2079 | port *pt = SCHEME_V->outport->object.port; |
|
|
2080 | |
|
|
2081 | if (pt->kind & port_file) |
2069 | if (pt->kind & port_file) |
2082 | write (pt->rep.stdio.file, s, len); |
2070 | write (pt->rep.stdio.file, s, len); |
2083 | else |
2071 | else |
2084 | { |
2072 | { |
2085 | for (; len; len--) |
2073 | for (; len; len--) |
… | |
… | |
2090 | *pt->rep.string.curr++ = *s++; |
2078 | *pt->rep.string.curr++ = *s++; |
2091 | } |
2079 | } |
2092 | } |
2080 | } |
2093 | |
2081 | |
2094 | #else |
2082 | #else |
2095 | write (1, s, len); |
2083 | write (1, s, len); // output not initialised |
2096 | #endif |
2084 | #endif |
|
|
2085 | } |
|
|
2086 | |
|
|
2087 | INTERFACE void |
|
|
2088 | putstr (SCHEME_P_ const char *s) |
|
|
2089 | { |
|
|
2090 | putchars (SCHEME_A_ s, strlen (s)); |
2097 | } |
2091 | } |
2098 | |
2092 | |
2099 | INTERFACE void |
2093 | INTERFACE void |
2100 | putcharacter (SCHEME_P_ int c) |
2094 | putcharacter (SCHEME_P_ int c) |
2101 | { |
2095 | { |
2102 | #if USE_PORTS |
|
|
2103 | port *pt = SCHEME_V->outport->object.port; |
|
|
2104 | |
|
|
2105 | if (pt->kind & port_file) |
|
|
2106 | { |
|
|
2107 | char cc = c; |
|
|
2108 | write (pt->rep.stdio.file, &cc, 1); |
|
|
2109 | } |
|
|
2110 | else |
|
|
2111 | { |
|
|
2112 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2113 | *pt->rep.string.curr++ = c; |
|
|
2114 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2115 | *pt->rep.string.curr++ = c; |
|
|
2116 | } |
|
|
2117 | |
|
|
2118 | #else |
|
|
2119 | char cc = c; |
2096 | char cc = c; |
2120 | write (1, &c, 1); |
2097 | |
2121 | #endif |
2098 | putchars (SCHEME_A_ &cc, 1); |
2122 | } |
2099 | } |
2123 | |
2100 | |
2124 | /* read characters up to delimiter, but cater to character constants */ |
2101 | /* read characters up to delimiter, but cater to character constants */ |
2125 | static char * |
2102 | ecb_cold static char * |
2126 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2103 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2127 | { |
2104 | { |
2128 | char *p = SCHEME_V->strbuff + skip; |
2105 | char *p = SCHEME_V->strbuff + skip; |
2129 | |
2106 | |
2130 | 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)))); |
… | |
… | |
2139 | |
2116 | |
2140 | return SCHEME_V->strbuff; |
2117 | return SCHEME_V->strbuff; |
2141 | } |
2118 | } |
2142 | |
2119 | |
2143 | /* read string expression "xxx...xxx" */ |
2120 | /* read string expression "xxx...xxx" */ |
2144 | static pointer |
2121 | ecb_cold static pointer |
2145 | readstrexp (SCHEME_P_ char delim) |
2122 | readstrexp (SCHEME_P_ char delim) |
2146 | { |
2123 | { |
2147 | char *p = SCHEME_V->strbuff; |
2124 | char *p = SCHEME_V->strbuff; |
2148 | int c; |
2125 | int c; |
2149 | int c1 = 0; |
2126 | int c1 = 0; |
… | |
… | |
2182 | case '7': |
2159 | case '7': |
2183 | state = st_oct1; |
2160 | state = st_oct1; |
2184 | c1 = c - '0'; |
2161 | c1 = c - '0'; |
2185 | break; |
2162 | break; |
2186 | |
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 | // this overshoots the minimum requirements of r7rs |
|
|
2170 | case ' ': |
|
|
2171 | case '\t': |
|
|
2172 | case '\r': |
|
|
2173 | case '\n': |
|
|
2174 | skipspace (SCHEME_A); |
|
|
2175 | state = st_ok; |
|
|
2176 | break; |
|
|
2177 | |
|
|
2178 | //TODO: x should end in ;, not two-digit hex |
2187 | case 'x': |
2179 | case 'x': |
2188 | case 'X': |
2180 | case 'X': |
2189 | state = st_x1; |
2181 | state = st_x1; |
2190 | c1 = 0; |
2182 | c1 = 0; |
2191 | break; |
|
|
2192 | |
|
|
2193 | case 'n': |
|
|
2194 | *p++ = '\n'; |
|
|
2195 | state = st_ok; |
|
|
2196 | break; |
|
|
2197 | |
|
|
2198 | case 't': |
|
|
2199 | *p++ = '\t'; |
|
|
2200 | state = st_ok; |
|
|
2201 | break; |
|
|
2202 | |
|
|
2203 | case 'r': |
|
|
2204 | *p++ = '\r'; |
|
|
2205 | state = st_ok; |
|
|
2206 | break; |
2183 | break; |
2207 | |
2184 | |
2208 | default: |
2185 | default: |
2209 | *p++ = c; |
2186 | *p++ = c; |
2210 | state = st_ok; |
2187 | state = st_ok; |
… | |
… | |
2262 | } |
2239 | } |
2263 | } |
2240 | } |
2264 | } |
2241 | } |
2265 | |
2242 | |
2266 | /* check c is in chars */ |
2243 | /* check c is in chars */ |
2267 | ecb_inline int |
2244 | ecb_cold int |
2268 | is_one_of (const char *s, int c) |
2245 | is_one_of (const char *s, int c) |
2269 | { |
2246 | { |
2270 | return c == EOF || !!strchr (s, c); |
2247 | return c == EOF || !!strchr (s, c); |
2271 | } |
2248 | } |
2272 | |
2249 | |
2273 | /* skip white characters */ |
2250 | /* skip white characters */ |
2274 | ecb_inline int |
2251 | ecb_cold int |
2275 | skipspace (SCHEME_P) |
2252 | skipspace (SCHEME_P) |
2276 | { |
2253 | { |
2277 | int c, curr_line = 0; |
2254 | int c, curr_line = 0; |
2278 | |
2255 | |
2279 | do |
2256 | do |
2280 | { |
2257 | { |
2281 | c = inchar (SCHEME_A); |
2258 | c = inchar (SCHEME_A); |
|
|
2259 | |
2282 | #if SHOW_ERROR_LINE |
2260 | #if SHOW_ERROR_LINE |
2283 | if (c == '\n') |
2261 | if (ecb_expect_false (c == '\n')) |
2284 | curr_line++; |
2262 | curr_line++; |
2285 | #endif |
2263 | #endif |
|
|
2264 | |
|
|
2265 | if (ecb_expect_false (c == EOF)) |
|
|
2266 | return c; |
2286 | } |
2267 | } |
2287 | while (is_one_of (WHITESPACE, c)); |
2268 | while (is_one_of (WHITESPACE, c)); |
2288 | |
2269 | |
2289 | /* record it */ |
2270 | /* record it */ |
2290 | #if SHOW_ERROR_LINE |
2271 | #if SHOW_ERROR_LINE |
2291 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2272 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2292 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2273 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2293 | #endif |
2274 | #endif |
2294 | |
2275 | |
2295 | if (c != EOF) |
|
|
2296 | { |
|
|
2297 | backchar (SCHEME_A_ c); |
2276 | backchar (SCHEME_A_ c); |
2298 | return 1; |
2277 | return 1; |
2299 | } |
|
|
2300 | else |
|
|
2301 | return EOF; |
|
|
2302 | } |
2278 | } |
2303 | |
2279 | |
2304 | /* get token */ |
2280 | /* get token */ |
2305 | static int |
2281 | ecb_cold static int |
2306 | token (SCHEME_P) |
2282 | token (SCHEME_P) |
2307 | { |
2283 | { |
2308 | int c = skipspace (SCHEME_A); |
2284 | int c = skipspace (SCHEME_A); |
2309 | |
2285 | |
2310 | if (c == EOF) |
2286 | if (c == EOF) |
… | |
… | |
2408 | } |
2384 | } |
2409 | |
2385 | |
2410 | /* ========== Routines for Printing ========== */ |
2386 | /* ========== Routines for Printing ========== */ |
2411 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2387 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2412 | |
2388 | |
2413 | static void |
2389 | ecb_cold static void |
2414 | printslashstring (SCHEME_P_ char *p, int len) |
2390 | printslashstring (SCHEME_P_ char *p, int len) |
2415 | { |
2391 | { |
2416 | int i; |
2392 | int i; |
2417 | unsigned char *s = (unsigned char *) p; |
2393 | unsigned char *s = (unsigned char *) p; |
2418 | |
2394 | |
… | |
… | |
2474 | |
2450 | |
2475 | putcharacter (SCHEME_A_ '"'); |
2451 | putcharacter (SCHEME_A_ '"'); |
2476 | } |
2452 | } |
2477 | |
2453 | |
2478 | /* print atoms */ |
2454 | /* print atoms */ |
2479 | static void |
2455 | ecb_cold static void |
2480 | printatom (SCHEME_P_ pointer l, int f) |
2456 | printatom (SCHEME_P_ pointer l, int f) |
2481 | { |
2457 | { |
2482 | char *p; |
2458 | char *p; |
2483 | int len; |
2459 | int len; |
2484 | |
2460 | |
2485 | atom2str (SCHEME_A_ l, f, &p, &len); |
2461 | atom2str (SCHEME_A_ l, f, &p, &len); |
2486 | putchars (SCHEME_A_ p, len); |
2462 | putchars (SCHEME_A_ p, len); |
2487 | } |
2463 | } |
2488 | |
2464 | |
2489 | /* Uses internal buffer unless string pointer is already available */ |
2465 | /* Uses internal buffer unless string pointer is already available */ |
2490 | static void |
2466 | ecb_cold static void |
2491 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2467 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2492 | { |
2468 | { |
2493 | char *p; |
2469 | char *p; |
2494 | |
2470 | |
2495 | if (l == NIL) |
2471 | if (l == NIL) |
… | |
… | |
2702 | return car (d); |
2678 | return car (d); |
2703 | |
2679 | |
2704 | p = cons (car (d), cdr (d)); |
2680 | p = cons (car (d), cdr (d)); |
2705 | q = p; |
2681 | q = p; |
2706 | |
2682 | |
2707 | while (cdr (cdr (p)) != NIL) |
2683 | while (cddr (p) != NIL) |
2708 | { |
2684 | { |
2709 | d = cons (car (p), cdr (p)); |
2685 | d = cons (car (p), cdr (p)); |
2710 | |
2686 | |
2711 | if (cdr (cdr (p)) != NIL) |
2687 | if (cddr (p) != NIL) |
2712 | p = cdr (d); |
2688 | p = cdr (d); |
2713 | } |
2689 | } |
2714 | |
2690 | |
2715 | set_cdr (p, car (cdr (p))); |
2691 | set_cdr (p, cadr (p)); |
2716 | return q; |
2692 | return q; |
2717 | } |
2693 | } |
2718 | |
2694 | |
2719 | /* reverse list -- produce new list */ |
2695 | /* reverse list -- produce new list */ |
2720 | static pointer |
2696 | ecb_hot static pointer |
2721 | reverse (SCHEME_P_ pointer a) |
2697 | reverse (SCHEME_P_ pointer a) |
2722 | { |
2698 | { |
2723 | /* a must be checked by gc */ |
2699 | /* a must be checked by gc */ |
2724 | pointer p = NIL; |
2700 | pointer p = NIL; |
2725 | |
2701 | |
… | |
… | |
2728 | |
2704 | |
2729 | return p; |
2705 | return p; |
2730 | } |
2706 | } |
2731 | |
2707 | |
2732 | /* reverse list --- in-place */ |
2708 | /* reverse list --- in-place */ |
2733 | static pointer |
2709 | ecb_hot static pointer |
2734 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2710 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2735 | { |
2711 | { |
2736 | pointer result = term; |
2712 | pointer result = term; |
2737 | pointer p = list; |
2713 | pointer p = list; |
2738 | |
2714 | |
… | |
… | |
2746 | |
2722 | |
2747 | return result; |
2723 | return result; |
2748 | } |
2724 | } |
2749 | |
2725 | |
2750 | /* append list -- produce new list (in reverse order) */ |
2726 | /* append list -- produce new list (in reverse order) */ |
2751 | static pointer |
2727 | ecb_hot static pointer |
2752 | revappend (SCHEME_P_ pointer a, pointer b) |
2728 | revappend (SCHEME_P_ pointer a, pointer b) |
2753 | { |
2729 | { |
2754 | pointer result = a; |
2730 | pointer result = a; |
2755 | pointer p = b; |
2731 | pointer p = b; |
2756 | |
2732 | |
… | |
… | |
2765 | |
2741 | |
2766 | return S_F; /* signal an error */ |
2742 | return S_F; /* signal an error */ |
2767 | } |
2743 | } |
2768 | |
2744 | |
2769 | /* equivalence of atoms */ |
2745 | /* equivalence of atoms */ |
2770 | int |
2746 | ecb_hot int |
2771 | eqv (pointer a, pointer b) |
2747 | eqv (pointer a, pointer b) |
2772 | { |
2748 | { |
2773 | if (is_string (a)) |
2749 | if (is_string (a)) |
2774 | { |
2750 | { |
2775 | if (is_string (b)) |
2751 | if (is_string (b)) |
… | |
… | |
2869 | } |
2845 | } |
2870 | else |
2846 | else |
2871 | set_car (env, immutable_cons (slot, car (env))); |
2847 | set_car (env, immutable_cons (slot, car (env))); |
2872 | } |
2848 | } |
2873 | |
2849 | |
2874 | static pointer |
2850 | ecb_hot static pointer |
2875 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2851 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2876 | { |
2852 | { |
2877 | pointer x, y; |
2853 | pointer x, y; |
2878 | |
2854 | |
2879 | for (x = env; x != NIL; x = cdr (x)) |
2855 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2900 | return NIL; |
2876 | return NIL; |
2901 | } |
2877 | } |
2902 | |
2878 | |
2903 | #else /* USE_ALIST_ENV */ |
2879 | #else /* USE_ALIST_ENV */ |
2904 | |
2880 | |
2905 | ecb_inline void |
2881 | static void |
2906 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2882 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2907 | { |
2883 | { |
2908 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2884 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2909 | setenvironment (SCHEME_V->envir); |
2885 | setenvironment (SCHEME_V->envir); |
2910 | } |
2886 | } |
2911 | |
2887 | |
2912 | ecb_inline void |
2888 | static void |
2913 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2889 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2914 | { |
2890 | { |
2915 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2891 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2916 | } |
2892 | } |
2917 | |
2893 | |
2918 | static pointer |
2894 | ecb_hot static pointer |
2919 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2895 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2920 | { |
2896 | { |
2921 | pointer x, y; |
2897 | pointer x, y; |
2922 | |
2898 | |
2923 | for (x = env; x != NIL; x = cdr (x)) |
2899 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2937 | return NIL; |
2913 | return NIL; |
2938 | } |
2914 | } |
2939 | |
2915 | |
2940 | #endif /* USE_ALIST_ENV else */ |
2916 | #endif /* USE_ALIST_ENV else */ |
2941 | |
2917 | |
2942 | ecb_inline void |
2918 | static void |
2943 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2919 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2944 | { |
2920 | { |
2945 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2921 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2946 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2922 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2947 | } |
2923 | } |
2948 | |
2924 | |
2949 | ecb_inline void |
2925 | static void |
2950 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2926 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2951 | { |
2927 | { |
2952 | set_cdr (slot, value); |
2928 | set_cdr (slot, value); |
2953 | } |
2929 | } |
2954 | |
2930 | |
2955 | ecb_inline pointer |
2931 | static pointer |
2956 | slot_value_in_env (pointer slot) |
2932 | slot_value_in_env (pointer slot) |
2957 | { |
2933 | { |
2958 | return cdr (slot); |
2934 | return cdr (slot); |
2959 | } |
2935 | } |
2960 | |
2936 | |
2961 | /* ========== Evaluation Cycle ========== */ |
2937 | /* ========== Evaluation Cycle ========== */ |
2962 | |
2938 | |
2963 | static int |
2939 | ecb_cold static int |
2964 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2940 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2965 | { |
2941 | { |
2966 | #if USE_ERROR_HOOK |
2942 | #if USE_ERROR_HOOK |
2967 | pointer x; |
2943 | pointer x; |
2968 | pointer hdl = SCHEME_V->ERROR_HOOK; |
2944 | pointer hdl = SCHEME_V->ERROR_HOOK; |
… | |
… | |
3044 | pointer code; |
3020 | pointer code; |
3045 | }; |
3021 | }; |
3046 | |
3022 | |
3047 | # define STACK_GROWTH 3 |
3023 | # define STACK_GROWTH 3 |
3048 | |
3024 | |
3049 | static void |
3025 | ecb_hot static void |
3050 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3026 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3051 | { |
3027 | { |
3052 | int nframes = (uintptr_t)SCHEME_V->dump; |
3028 | int nframes = (uintptr_t)SCHEME_V->dump; |
3053 | struct dump_stack_frame *next_frame; |
3029 | struct dump_stack_frame *next_frame; |
3054 | |
3030 | |
3055 | /* enough room for the next frame? */ |
3031 | /* enough room for the next frame? */ |
3056 | if (nframes >= SCHEME_V->dump_size) |
3032 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3057 | { |
3033 | { |
3058 | SCHEME_V->dump_size += STACK_GROWTH; |
3034 | SCHEME_V->dump_size += STACK_GROWTH; |
3059 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3035 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3060 | } |
3036 | } |
3061 | |
3037 | |
… | |
… | |
3067 | next_frame->code = code; |
3043 | next_frame->code = code; |
3068 | |
3044 | |
3069 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3045 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3070 | } |
3046 | } |
3071 | |
3047 | |
3072 | static int |
3048 | static ecb_hot int |
3073 | xs_return (SCHEME_P_ pointer a) |
3049 | xs_return (SCHEME_P_ pointer a) |
3074 | { |
3050 | { |
3075 | int nframes = (uintptr_t)SCHEME_V->dump; |
3051 | int nframes = (uintptr_t)SCHEME_V->dump; |
3076 | struct dump_stack_frame *frame; |
3052 | struct dump_stack_frame *frame; |
3077 | |
3053 | |
… | |
… | |
3088 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3064 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3089 | |
3065 | |
3090 | return 0; |
3066 | return 0; |
3091 | } |
3067 | } |
3092 | |
3068 | |
3093 | ecb_inline void |
3069 | ecb_cold void |
3094 | dump_stack_reset (SCHEME_P) |
3070 | dump_stack_reset (SCHEME_P) |
3095 | { |
3071 | { |
3096 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3072 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3097 | SCHEME_V->dump = (pointer)+0; |
3073 | SCHEME_V->dump = (pointer)+0; |
3098 | } |
3074 | } |
3099 | |
3075 | |
3100 | ecb_inline void |
3076 | ecb_cold void |
3101 | dump_stack_initialize (SCHEME_P) |
3077 | dump_stack_initialize (SCHEME_P) |
3102 | { |
3078 | { |
3103 | SCHEME_V->dump_size = 0; |
3079 | SCHEME_V->dump_size = 0; |
3104 | SCHEME_V->dump_base = 0; |
3080 | SCHEME_V->dump_base = 0; |
3105 | dump_stack_reset (SCHEME_A); |
3081 | dump_stack_reset (SCHEME_A); |
3106 | } |
3082 | } |
3107 | |
3083 | |
3108 | static void |
3084 | ecb_cold static void |
3109 | dump_stack_free (SCHEME_P) |
3085 | dump_stack_free (SCHEME_P) |
3110 | { |
3086 | { |
3111 | free (SCHEME_V->dump_base); |
3087 | free (SCHEME_V->dump_base); |
3112 | SCHEME_V->dump_base = 0; |
3088 | SCHEME_V->dump_base = 0; |
3113 | SCHEME_V->dump = (pointer)0; |
3089 | SCHEME_V->dump = (pointer)0; |
3114 | SCHEME_V->dump_size = 0; |
3090 | SCHEME_V->dump_size = 0; |
3115 | } |
3091 | } |
3116 | |
3092 | |
3117 | static void |
3093 | ecb_cold static void |
3118 | dump_stack_mark (SCHEME_P) |
3094 | dump_stack_mark (SCHEME_P) |
3119 | { |
3095 | { |
3120 | int nframes = (uintptr_t)SCHEME_V->dump; |
3096 | int nframes = (uintptr_t)SCHEME_V->dump; |
3121 | int i; |
3097 | int i; |
3122 | |
3098 | |
… | |
… | |
3128 | mark (frame->envir); |
3104 | mark (frame->envir); |
3129 | mark (frame->code); |
3105 | mark (frame->code); |
3130 | } |
3106 | } |
3131 | } |
3107 | } |
3132 | |
3108 | |
3133 | static pointer |
3109 | ecb_cold static pointer |
3134 | ss_get_cont (SCHEME_P) |
3110 | ss_get_cont (SCHEME_P) |
3135 | { |
3111 | { |
3136 | int nframes = (uintptr_t)SCHEME_V->dump; |
3112 | int nframes = (uintptr_t)SCHEME_V->dump; |
3137 | int i; |
3113 | int i; |
3138 | |
3114 | |
… | |
… | |
3150 | } |
3126 | } |
3151 | |
3127 | |
3152 | return cont; |
3128 | return cont; |
3153 | } |
3129 | } |
3154 | |
3130 | |
3155 | static void |
3131 | ecb_cold static void |
3156 | ss_set_cont (SCHEME_P_ pointer cont) |
3132 | ss_set_cont (SCHEME_P_ pointer cont) |
3157 | { |
3133 | { |
3158 | int i = 0; |
3134 | int i = 0; |
3159 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3135 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3160 | |
3136 | |
… | |
… | |
3172 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3148 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3173 | } |
3149 | } |
3174 | |
3150 | |
3175 | #else |
3151 | #else |
3176 | |
3152 | |
3177 | ecb_inline void |
3153 | ecb_cold void |
3178 | dump_stack_reset (SCHEME_P) |
3154 | dump_stack_reset (SCHEME_P) |
3179 | { |
3155 | { |
3180 | SCHEME_V->dump = NIL; |
3156 | SCHEME_V->dump = NIL; |
3181 | } |
3157 | } |
3182 | |
3158 | |
3183 | ecb_inline void |
3159 | ecb_cold void |
3184 | dump_stack_initialize (SCHEME_P) |
3160 | dump_stack_initialize (SCHEME_P) |
3185 | { |
3161 | { |
3186 | dump_stack_reset (SCHEME_A); |
3162 | dump_stack_reset (SCHEME_A); |
3187 | } |
3163 | } |
3188 | |
3164 | |
3189 | static void |
3165 | ecb_cold static void |
3190 | dump_stack_free (SCHEME_P) |
3166 | dump_stack_free (SCHEME_P) |
3191 | { |
3167 | { |
3192 | SCHEME_V->dump = NIL; |
3168 | SCHEME_V->dump = NIL; |
3193 | } |
3169 | } |
3194 | |
3170 | |
3195 | static int |
3171 | ecb_hot static int |
3196 | xs_return (SCHEME_P_ pointer a) |
3172 | xs_return (SCHEME_P_ pointer a) |
3197 | { |
3173 | { |
3198 | pointer dump = SCHEME_V->dump; |
3174 | pointer dump = SCHEME_V->dump; |
3199 | |
3175 | |
3200 | SCHEME_V->value = a; |
3176 | SCHEME_V->value = a; |
… | |
… | |
3210 | SCHEME_V->dump = dump; |
3186 | SCHEME_V->dump = dump; |
3211 | |
3187 | |
3212 | return 0; |
3188 | return 0; |
3213 | } |
3189 | } |
3214 | |
3190 | |
3215 | static void |
3191 | ecb_hot static void |
3216 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3192 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3217 | { |
3193 | { |
3218 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3194 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3219 | cons (args, |
3195 | cons (args, |
3220 | cons (SCHEME_V->envir, |
3196 | cons (SCHEME_V->envir, |
3221 | cons (code, |
3197 | cons (code, |
3222 | SCHEME_V->dump)))); |
3198 | SCHEME_V->dump)))); |
3223 | } |
3199 | } |
3224 | |
3200 | |
|
|
3201 | ecb_cold static void |
|
|
3202 | dump_stack_mark (SCHEME_P) |
|
|
3203 | { |
|
|
3204 | mark (SCHEME_V->dump); |
|
|
3205 | } |
|
|
3206 | |
|
|
3207 | ecb_cold static pointer |
|
|
3208 | ss_get_cont (SCHEME_P) |
|
|
3209 | { |
|
|
3210 | return SCHEME_V->dump; |
|
|
3211 | } |
|
|
3212 | |
|
|
3213 | ecb_cold static void |
|
|
3214 | ss_set_cont (SCHEME_P_ pointer cont) |
|
|
3215 | { |
|
|
3216 | SCHEME_V->dump = cont; |
|
|
3217 | } |
|
|
3218 | |
|
|
3219 | #endif |
|
|
3220 | |
|
|
3221 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
|
|
3222 | |
|
|
3223 | #if EXPERIMENT |
|
|
3224 | |
|
|
3225 | typedef void *stream[1]; |
|
|
3226 | |
|
|
3227 | #define stream_init() { 0 } |
|
|
3228 | |
|
|
3229 | ecb_cold static void |
|
|
3230 | stream_put (void **s, uint8_t byte) |
|
|
3231 | { |
|
|
3232 | uint32_t *sp = *s; |
|
|
3233 | uint32_t size = sizeof (uint32_t) * 2; |
|
|
3234 | uint32_t offs = size; |
|
|
3235 | |
|
|
3236 | if (ecb_expect_true (sp)) |
|
|
3237 | { |
|
|
3238 | offs = sp[0]; |
|
|
3239 | size = sp[1]; |
|
|
3240 | } |
|
|
3241 | |
|
|
3242 | if (ecb_expect_false (offs == size)) |
|
|
3243 | { |
|
|
3244 | size *= 2; |
|
|
3245 | sp = realloc (sp, size); |
|
|
3246 | *s = sp; |
|
|
3247 | sp[1] = size; |
|
|
3248 | |
|
|
3249 | } |
|
|
3250 | |
|
|
3251 | ((uint8_t *)sp)[offs++] = byte; |
|
|
3252 | sp[0] = offs; |
|
|
3253 | } |
|
|
3254 | |
|
|
3255 | #define stream_data(s) ((char *)(s)[0] + sizeof (uint32_t) * 2) |
|
|
3256 | #define stream_size(s) (((uint32_t *)(s)[0])[0] - sizeof (uint32_t) * 2) |
|
|
3257 | #define stream_free(s) free (s[0]) |
|
|
3258 | |
|
|
3259 | // calculates a (preferably small) integer that makes it possible to find |
|
|
3260 | // the symbol again. if pointers were offsets into a memory area... until |
|
|
3261 | // then, we return segment number in the low bits, and offset in the high |
|
|
3262 | // bits |
|
|
3263 | static uint32_t |
|
|
3264 | symbol_id (SCHEME_P_ pointer sym) |
|
|
3265 | { |
|
|
3266 | struct cell *p = CELL (sym); |
|
|
3267 | int i; |
|
|
3268 | |
|
|
3269 | for (i = SCHEME_V->last_cell_seg; i >= 0; --i) |
|
|
3270 | if (SCHEME_V->cell_seg[i] <= p && p < SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize[i]) |
|
|
3271 | { |
|
|
3272 | printf ("seg %d ofs %d/%d\n",i,(p - SCHEME_V->cell_seg[i]),SCHEME_V->cell_segsize[i]);//D |
|
|
3273 | return i | ((p - SCHEME_V->cell_seg[i]) << CELL_NSEGMENT_LOG); |
|
|
3274 | } |
|
|
3275 | |
|
|
3276 | abort (); |
|
|
3277 | } |
|
|
3278 | |
3225 | static void |
3279 | static void |
3226 | dump_stack_mark (SCHEME_P) |
3280 | compile (SCHEME_P_ stream s, pointer x) |
3227 | { |
3281 | { |
3228 | mark (SCHEME_V->dump); |
3282 | if (x == NIL) |
3229 | } |
3283 | { |
|
|
3284 | stream_put (s, 0); |
|
|
3285 | return; |
|
|
3286 | } |
3230 | |
3287 | |
3231 | static pointer |
3288 | if (is_syntax (x)) |
3232 | ss_get_cont (SCHEME_P) |
3289 | { |
3233 | { |
3290 | stream_put (s, 1); |
3234 | return SCHEME_V->dump; |
3291 | stream_put (s, syntaxnum (x)); |
3235 | } |
3292 | return; |
|
|
3293 | } |
3236 | |
3294 | |
3237 | static void |
3295 | switch (type (x)) |
3238 | ss_set_cont (SCHEME_P_ pointer cont) |
3296 | { |
3239 | { |
3297 | case T_INTEGER: |
3240 | SCHEME_V->dump = cont; |
3298 | stream_put (s, 2); |
3241 | } |
3299 | stream_put (s, 0); |
|
|
3300 | stream_put (s, 0); |
|
|
3301 | stream_put (s, 0); |
|
|
3302 | stream_put (s, 0); |
|
|
3303 | return; |
3242 | |
3304 | |
3243 | #endif |
3305 | case T_SYMBOL: |
|
|
3306 | { |
|
|
3307 | uint32_t sym = symbol_id (SCHEME_A_ x); |
|
|
3308 | printf ("sym %x\n", sym);//D |
3244 | |
3309 | |
3245 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3310 | stream_put (s, 3); |
3246 | |
3311 | |
3247 | #if 1 |
3312 | while (sym > 0x7f) |
|
|
3313 | { |
|
|
3314 | stream_put (s, sym | 0x80); |
|
|
3315 | sym >>= 8; |
|
|
3316 | } |
|
|
3317 | |
|
|
3318 | stream_put (s, sym); |
|
|
3319 | } |
|
|
3320 | return; |
|
|
3321 | |
|
|
3322 | case T_PAIR: |
|
|
3323 | stream_put (s, 4); |
|
|
3324 | while (x != NIL) |
|
|
3325 | { |
|
|
3326 | compile (SCHEME_A_ s, car (x)); |
|
|
3327 | x = cdr (x); |
|
|
3328 | } |
|
|
3329 | stream_put (s, 0xff); |
|
|
3330 | return; |
|
|
3331 | |
|
|
3332 | default: |
|
|
3333 | stream_put (s, 5); |
|
|
3334 | stream_put (s, type (x)); |
|
|
3335 | stream_put (s, 0); |
|
|
3336 | stream_put (s, 0); |
|
|
3337 | stream_put (s, 0); |
|
|
3338 | stream_put (s, 0); |
|
|
3339 | break; |
|
|
3340 | } |
|
|
3341 | } |
|
|
3342 | |
3248 | static int |
3343 | static int |
|
|
3344 | compile_closure (SCHEME_P_ pointer p) |
|
|
3345 | { |
|
|
3346 | stream s = stream_init (); |
|
|
3347 | |
|
|
3348 | printatom (SCHEME_A_ p, 1);//D |
|
|
3349 | compile (SCHEME_A_ s, car (p)); |
|
|
3350 | |
|
|
3351 | FILE *xxd = popen ("xxd", "we"); |
|
|
3352 | fwrite (stream_data (s), 1, stream_size (s), xxd); |
|
|
3353 | fclose (xxd); |
|
|
3354 | |
|
|
3355 | return stream_size (s); |
|
|
3356 | } |
|
|
3357 | |
|
|
3358 | static int |
3249 | debug (SCHEME_P_ int indent, pointer x) |
3359 | dtree (SCHEME_P_ int indent, pointer x) |
3250 | { |
3360 | { |
3251 | int c; |
3361 | int c; |
3252 | |
3362 | |
3253 | if (is_syntax (x)) |
3363 | if (is_syntax (x)) |
3254 | { |
3364 | { |
… | |
… | |
3272 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
3382 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
3273 | return 24+8; |
3383 | return 24+8; |
3274 | |
3384 | |
3275 | case T_CLOSURE: |
3385 | case T_CLOSURE: |
3276 | printf ("%*sS<%s>\n", indent, "", "closure"); |
3386 | printf ("%*sS<%s>\n", indent, "", "closure"); |
3277 | debug (SCHEME_A_ indent + 3, cdr(x)); |
3387 | dtree (SCHEME_A_ indent + 3, cdr(x)); |
3278 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
3388 | return 32 + dtree (SCHEME_A_ indent + 3, car (x)); |
3279 | |
3389 | |
3280 | case T_PAIR: |
3390 | case T_PAIR: |
3281 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
3391 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
3282 | c = debug (SCHEME_A_ indent + 3, car (x)); |
3392 | c = dtree (SCHEME_A_ indent + 3, car (x)); |
3283 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
3393 | c += dtree (SCHEME_A_ indent + 3, cdr (x)); |
3284 | return c + 1; |
3394 | return c + 1; |
3285 | |
3395 | |
3286 | case T_PORT: |
3396 | case T_PORT: |
3287 | printf ("%*sS<%s>\n", indent, "", "port"); |
3397 | printf ("%*sS<%s>\n", indent, "", "port"); |
3288 | return 24+8; |
3398 | return 24+8; |
… | |
… | |
3291 | printf ("%*sS<%s>\n", indent, "", "vector"); |
3401 | printf ("%*sS<%s>\n", indent, "", "vector"); |
3292 | return 24+8; |
3402 | return 24+8; |
3293 | |
3403 | |
3294 | case T_ENVIRONMENT: |
3404 | case T_ENVIRONMENT: |
3295 | printf ("%*sS<%s>\n", indent, "", "environment"); |
3405 | printf ("%*sS<%s>\n", indent, "", "environment"); |
3296 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
3406 | return 0 + dtree (SCHEME_A_ indent + 3, car (x)); |
3297 | |
3407 | |
3298 | default: |
3408 | default: |
3299 | printf ("unhandled type %d\n", type (x)); |
3409 | printf ("unhandled type %d\n", type (x)); |
3300 | break; |
3410 | break; |
3301 | } |
3411 | } |
3302 | } |
3412 | } |
3303 | #endif |
3413 | #endif |
3304 | |
3414 | |
3305 | static int |
3415 | /* syntax, eval, core, ... */ |
|
|
3416 | ecb_hot static int |
3306 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3417 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3307 | { |
3418 | { |
3308 | pointer args = SCHEME_V->args; |
3419 | pointer args = SCHEME_V->args; |
3309 | pointer x, y; |
3420 | pointer x, y; |
3310 | |
3421 | |
3311 | switch (op) |
3422 | switch (op) |
3312 | { |
3423 | { |
3313 | #if 1 //D |
3424 | #if EXPERIMENT //D |
3314 | case OP_DEBUG: |
3425 | case OP_DEBUG: |
3315 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
3426 | { |
|
|
3427 | uint32_t len = compile_closure (SCHEME_A_ car (args)); |
|
|
3428 | printf ("len = %d\n", len); |
3316 | printf ("\n"); |
3429 | printf ("\n"); |
3317 | s_return (S_T); |
3430 | s_return (S_T); |
|
|
3431 | } |
3318 | #endif |
3432 | #endif |
3319 | case OP_LOAD: /* load */ |
3433 | case OP_LOAD: /* load */ |
3320 | if (file_interactive (SCHEME_A)) |
3434 | if (file_interactive (SCHEME_A)) |
3321 | { |
3435 | { |
3322 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3436 | putstr (SCHEME_A_ "Loading "); |
3323 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3437 | putstr (SCHEME_A_ strvalue (car (args))); |
|
|
3438 | putcharacter (SCHEME_A_ '\n'); |
3324 | } |
3439 | } |
3325 | |
3440 | |
3326 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3441 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3327 | Error_1 ("unable to open", car (args)); |
3442 | Error_1 ("unable to open", car (args)); |
3328 | else |
3443 | |
3329 | { |
|
|
3330 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3444 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3331 | s_goto (OP_T0LVL); |
3445 | s_goto (OP_T0LVL); |
3332 | } |
|
|
3333 | |
3446 | |
3334 | case OP_T0LVL: /* top level */ |
3447 | case OP_T0LVL: /* top level */ |
3335 | |
3448 | |
3336 | /* If we reached the end of file, this loop is done. */ |
3449 | /* If we reached the end of file, this loop is done. */ |
3337 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3450 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3338 | { |
3451 | { |
3339 | if (SCHEME_V->file_i == 0) |
3452 | if (SCHEME_V->file_i == 0) |
3340 | { |
3453 | { |
3341 | SCHEME_V->args = NIL; |
3454 | SCHEME_V->args = NIL; |
3342 | s_goto (OP_QUIT); |
3455 | s_goto (OP_QUIT); |
… | |
… | |
3353 | /* If interactive, be nice to user. */ |
3466 | /* If interactive, be nice to user. */ |
3354 | if (file_interactive (SCHEME_A)) |
3467 | if (file_interactive (SCHEME_A)) |
3355 | { |
3468 | { |
3356 | SCHEME_V->envir = SCHEME_V->global_env; |
3469 | SCHEME_V->envir = SCHEME_V->global_env; |
3357 | dump_stack_reset (SCHEME_A); |
3470 | dump_stack_reset (SCHEME_A); |
3358 | putstr (SCHEME_A_ "\n"); |
3471 | putcharacter (SCHEME_A_ '\n'); |
|
|
3472 | #if EXPERIMENT |
|
|
3473 | system ("ps v $PPID"); |
|
|
3474 | #endif |
3359 | putstr (SCHEME_A_ prompt); |
3475 | putstr (SCHEME_A_ prompt); |
3360 | } |
3476 | } |
3361 | |
3477 | |
3362 | /* Set up another iteration of REPL */ |
3478 | /* Set up another iteration of REPL */ |
3363 | SCHEME_V->nesting = 0; |
3479 | SCHEME_V->nesting = 0; |
… | |
… | |
3398 | { |
3514 | { |
3399 | SCHEME_V->print_flag = 1; |
3515 | SCHEME_V->print_flag = 1; |
3400 | SCHEME_V->args = SCHEME_V->value; |
3516 | SCHEME_V->args = SCHEME_V->value; |
3401 | s_goto (OP_P0LIST); |
3517 | s_goto (OP_P0LIST); |
3402 | } |
3518 | } |
3403 | else |
3519 | |
3404 | s_return (SCHEME_V->value); |
3520 | s_return (SCHEME_V->value); |
3405 | |
3521 | |
3406 | case OP_EVAL: /* main part of evaluation */ |
3522 | case OP_EVAL: /* main part of evaluation */ |
3407 | #if USE_TRACING |
3523 | #if USE_TRACING |
3408 | if (SCHEME_V->tracing) |
3524 | if (SCHEME_V->tracing) |
3409 | { |
3525 | { |
… | |
… | |
3420 | #endif |
3536 | #endif |
3421 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3537 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3422 | { |
3538 | { |
3423 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3539 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3424 | |
3540 | |
3425 | if (x != NIL) |
3541 | if (x == NIL) |
3426 | s_return (slot_value_in_env (x)); |
|
|
3427 | else |
|
|
3428 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3542 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3543 | |
|
|
3544 | s_return (slot_value_in_env (x)); |
3429 | } |
3545 | } |
3430 | else if (is_pair (SCHEME_V->code)) |
3546 | else if (is_pair (SCHEME_V->code)) |
3431 | { |
3547 | { |
3432 | x = car (SCHEME_V->code); |
3548 | x = car (SCHEME_V->code); |
3433 | |
3549 | |
… | |
… | |
3442 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3558 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3443 | SCHEME_V->code = x; |
3559 | SCHEME_V->code = x; |
3444 | s_goto (OP_EVAL); |
3560 | s_goto (OP_EVAL); |
3445 | } |
3561 | } |
3446 | } |
3562 | } |
3447 | else |
3563 | |
3448 | s_return (SCHEME_V->code); |
3564 | s_return (SCHEME_V->code); |
3449 | |
3565 | |
3450 | case OP_E0ARGS: /* eval arguments */ |
3566 | case OP_E0ARGS: /* eval arguments */ |
3451 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3567 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3452 | { |
3568 | { |
3453 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3569 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
… | |
… | |
3510 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3626 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3511 | else if (is_foreign (SCHEME_V->code)) |
3627 | else if (is_foreign (SCHEME_V->code)) |
3512 | { |
3628 | { |
3513 | /* Keep nested calls from GC'ing the arglist */ |
3629 | /* Keep nested calls from GC'ing the arglist */ |
3514 | push_recent_alloc (SCHEME_A_ args, NIL); |
3630 | push_recent_alloc (SCHEME_A_ args, NIL); |
3515 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3631 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3516 | |
3632 | |
3517 | s_return (x); |
3633 | s_return (x); |
3518 | } |
3634 | } |
3519 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3635 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3520 | { |
3636 | { |
… | |
… | |
3557 | |
3673 | |
3558 | case OP_DOMACRO: /* do macro */ |
3674 | case OP_DOMACRO: /* do macro */ |
3559 | SCHEME_V->code = SCHEME_V->value; |
3675 | SCHEME_V->code = SCHEME_V->value; |
3560 | s_goto (OP_EVAL); |
3676 | s_goto (OP_EVAL); |
3561 | |
3677 | |
3562 | #if 1 |
|
|
3563 | |
|
|
3564 | case OP_LAMBDA: /* lambda */ |
3678 | case OP_LAMBDA: /* lambda */ |
3565 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3679 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3566 | set SCHEME_V->value fall thru */ |
3680 | set SCHEME_V->value fall thru */ |
3567 | { |
3681 | { |
3568 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3682 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
… | |
… | |
3574 | SCHEME_V->code = slot_value_in_env (f); |
3688 | SCHEME_V->code = slot_value_in_env (f); |
3575 | s_goto (OP_APPLY); |
3689 | s_goto (OP_APPLY); |
3576 | } |
3690 | } |
3577 | |
3691 | |
3578 | SCHEME_V->value = SCHEME_V->code; |
3692 | SCHEME_V->value = SCHEME_V->code; |
3579 | /* Fallthru */ |
|
|
3580 | } |
3693 | } |
|
|
3694 | /* Fallthru */ |
3581 | |
3695 | |
3582 | case OP_LAMBDA1: |
3696 | case OP_LAMBDA1: |
3583 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3697 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3584 | |
|
|
3585 | #else |
|
|
3586 | |
|
|
3587 | case OP_LAMBDA: /* lambda */ |
|
|
3588 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
|
|
3589 | |
|
|
3590 | #endif |
|
|
3591 | |
3698 | |
3592 | case OP_MKCLOSURE: /* make-closure */ |
3699 | case OP_MKCLOSURE: /* make-closure */ |
3593 | x = car (args); |
3700 | x = car (args); |
3594 | |
3701 | |
3595 | if (car (x) == SCHEME_V->LAMBDA) |
3702 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
3634 | else |
3741 | else |
3635 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3742 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3636 | |
3743 | |
3637 | s_return (SCHEME_V->code); |
3744 | s_return (SCHEME_V->code); |
3638 | |
3745 | |
3639 | |
|
|
3640 | case OP_DEFP: /* defined? */ |
3746 | case OP_DEFP: /* defined? */ |
3641 | x = SCHEME_V->envir; |
3747 | x = SCHEME_V->envir; |
3642 | |
3748 | |
3643 | if (cdr (args) != NIL) |
3749 | if (cdr (args) != NIL) |
3644 | x = cadr (args); |
3750 | x = cadr (args); |
… | |
… | |
3661 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3767 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3662 | s_return (SCHEME_V->value); |
3768 | s_return (SCHEME_V->value); |
3663 | } |
3769 | } |
3664 | else |
3770 | else |
3665 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3771 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3666 | |
|
|
3667 | |
3772 | |
3668 | case OP_BEGIN: /* begin */ |
3773 | case OP_BEGIN: /* begin */ |
3669 | if (!is_pair (SCHEME_V->code)) |
3774 | if (!is_pair (SCHEME_V->code)) |
3670 | s_return (SCHEME_V->code); |
3775 | s_return (SCHEME_V->code); |
3671 | |
3776 | |
… | |
… | |
3683 | case OP_IF1: /* if */ |
3788 | case OP_IF1: /* if */ |
3684 | if (is_true (SCHEME_V->value)) |
3789 | if (is_true (SCHEME_V->value)) |
3685 | SCHEME_V->code = car (SCHEME_V->code); |
3790 | SCHEME_V->code = car (SCHEME_V->code); |
3686 | else |
3791 | else |
3687 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
3792 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
|
|
3793 | |
3688 | s_goto (OP_EVAL); |
3794 | s_goto (OP_EVAL); |
3689 | |
3795 | |
3690 | case OP_LET0: /* let */ |
3796 | case OP_LET0: /* let */ |
3691 | SCHEME_V->args = NIL; |
3797 | SCHEME_V->args = NIL; |
3692 | SCHEME_V->value = SCHEME_V->code; |
3798 | SCHEME_V->value = SCHEME_V->code; |
… | |
… | |
3848 | } |
3954 | } |
3849 | else |
3955 | else |
3850 | { |
3956 | { |
3851 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3957 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3852 | s_return (NIL); |
3958 | s_return (NIL); |
3853 | else |
3959 | |
3854 | { |
|
|
3855 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3960 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3856 | SCHEME_V->code = caar (SCHEME_V->code); |
3961 | SCHEME_V->code = caar (SCHEME_V->code); |
3857 | s_goto (OP_EVAL); |
3962 | s_goto (OP_EVAL); |
3858 | } |
|
|
3859 | } |
3963 | } |
3860 | |
3964 | |
3861 | case OP_DELAY: /* delay */ |
3965 | case OP_DELAY: /* delay */ |
3862 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3966 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3863 | set_typeflag (x, T_PROMISE); |
3967 | set_typeflag (x, T_PROMISE); |
… | |
… | |
3874 | case OP_AND1: /* and */ |
3978 | case OP_AND1: /* and */ |
3875 | if (is_false (SCHEME_V->value)) |
3979 | if (is_false (SCHEME_V->value)) |
3876 | s_return (SCHEME_V->value); |
3980 | s_return (SCHEME_V->value); |
3877 | else if (SCHEME_V->code == NIL) |
3981 | else if (SCHEME_V->code == NIL) |
3878 | s_return (SCHEME_V->value); |
3982 | s_return (SCHEME_V->value); |
3879 | else |
3983 | |
3880 | { |
|
|
3881 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3984 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3882 | SCHEME_V->code = car (SCHEME_V->code); |
3985 | SCHEME_V->code = car (SCHEME_V->code); |
3883 | s_goto (OP_EVAL); |
3986 | s_goto (OP_EVAL); |
3884 | } |
|
|
3885 | |
3987 | |
3886 | case OP_OR0: /* or */ |
3988 | case OP_OR0: /* or */ |
3887 | if (SCHEME_V->code == NIL) |
3989 | if (SCHEME_V->code == NIL) |
3888 | s_return (S_F); |
3990 | s_return (S_F); |
3889 | |
3991 | |
… | |
… | |
3894 | case OP_OR1: /* or */ |
3996 | case OP_OR1: /* or */ |
3895 | if (is_true (SCHEME_V->value)) |
3997 | if (is_true (SCHEME_V->value)) |
3896 | s_return (SCHEME_V->value); |
3998 | s_return (SCHEME_V->value); |
3897 | else if (SCHEME_V->code == NIL) |
3999 | else if (SCHEME_V->code == NIL) |
3898 | s_return (SCHEME_V->value); |
4000 | s_return (SCHEME_V->value); |
3899 | else |
4001 | |
3900 | { |
|
|
3901 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
4002 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3902 | SCHEME_V->code = car (SCHEME_V->code); |
4003 | SCHEME_V->code = car (SCHEME_V->code); |
3903 | s_goto (OP_EVAL); |
4004 | s_goto (OP_EVAL); |
3904 | } |
|
|
3905 | |
4005 | |
3906 | case OP_C0STREAM: /* cons-stream */ |
4006 | case OP_C0STREAM: /* cons-stream */ |
3907 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
4007 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3908 | SCHEME_V->code = car (SCHEME_V->code); |
4008 | SCHEME_V->code = car (SCHEME_V->code); |
3909 | s_goto (OP_EVAL); |
4009 | s_goto (OP_EVAL); |
… | |
… | |
3974 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
4074 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
3975 | SCHEME_V->code = caar (x); |
4075 | SCHEME_V->code = caar (x); |
3976 | s_goto (OP_EVAL); |
4076 | s_goto (OP_EVAL); |
3977 | } |
4077 | } |
3978 | } |
4078 | } |
3979 | else |
4079 | |
3980 | s_return (NIL); |
4080 | s_return (NIL); |
3981 | |
4081 | |
3982 | case OP_CASE2: /* case */ |
4082 | case OP_CASE2: /* case */ |
3983 | if (is_true (SCHEME_V->value)) |
4083 | if (is_true (SCHEME_V->value)) |
3984 | s_goto (OP_BEGIN); |
4084 | s_goto (OP_BEGIN); |
3985 | else |
4085 | |
3986 | s_return (NIL); |
4086 | s_return (NIL); |
3987 | |
4087 | |
3988 | case OP_PAPPLY: /* apply */ |
4088 | case OP_PAPPLY: /* apply */ |
3989 | SCHEME_V->code = car (args); |
4089 | SCHEME_V->code = car (args); |
3990 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
4090 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
3991 | /*SCHEME_V->args = cadr(args); */ |
4091 | /*SCHEME_V->args = cadr(args); */ |
… | |
… | |
4005 | } |
4105 | } |
4006 | |
4106 | |
4007 | if (USE_ERROR_CHECKING) abort (); |
4107 | if (USE_ERROR_CHECKING) abort (); |
4008 | } |
4108 | } |
4009 | |
4109 | |
4010 | static int |
4110 | /* math, cxr */ |
|
|
4111 | ecb_hot static int |
4011 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4112 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4012 | { |
4113 | { |
4013 | pointer args = SCHEME_V->args; |
4114 | pointer args = SCHEME_V->args; |
4014 | pointer x = car (args); |
4115 | pointer x = car (args); |
4015 | num v; |
4116 | num v; |
4016 | |
4117 | |
4017 | switch (op) |
4118 | switch (op) |
4018 | { |
4119 | { |
4019 | #if USE_MATH |
4120 | #if USE_MATH |
4020 | case OP_INEX2EX: /* inexact->exact */ |
4121 | case OP_INEX2EX: /* inexact->exact */ |
4021 | { |
|
|
4022 | if (is_integer (x)) |
4122 | if (!is_integer (x)) |
4023 | s_return (x); |
4123 | { |
4024 | |
|
|
4025 | RVALUE r = rvalue_unchecked (x); |
4124 | RVALUE r = rvalue_unchecked (x); |
4026 | |
4125 | |
4027 | if (r == (RVALUE)(IVALUE)r) |
4126 | if (r == (RVALUE)(IVALUE)r) |
4028 | s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); |
4127 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
4029 | else |
4128 | else |
4030 | Error_1 ("inexact->exact: not integral:", x); |
4129 | Error_1 ("inexact->exact: not integral:", x); |
4031 | } |
4130 | } |
4032 | |
4131 | |
|
|
4132 | s_return (x); |
|
|
4133 | |
|
|
4134 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4135 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4136 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4137 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
|
|
4138 | |
|
|
4139 | case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
4033 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4140 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4034 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
4141 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
|
|
4142 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
4035 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4143 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4036 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4144 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4037 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4145 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4038 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4146 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4039 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4147 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4040 | |
4148 | |
4041 | case OP_ATAN: |
4149 | case OP_ATAN: |
|
|
4150 | s_return (mk_real (SCHEME_A_ |
4042 | if (cdr (args) == NIL) |
4151 | cdr (args) == NIL |
4043 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4152 | ? atan (rvalue (x)) |
4044 | else |
4153 | : atan2 (rvalue (x), rvalue (cadr (args))))); |
4045 | { |
|
|
4046 | pointer y = cadr (args); |
|
|
4047 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
|
|
4048 | } |
|
|
4049 | |
|
|
4050 | case OP_SQRT: |
|
|
4051 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
|
|
4052 | |
4154 | |
4053 | case OP_EXPT: |
4155 | case OP_EXPT: |
4054 | { |
4156 | { |
4055 | RVALUE result; |
4157 | RVALUE result; |
4056 | int real_result = 1; |
4158 | int real_result = 1; |
… | |
… | |
4079 | if (real_result) |
4181 | if (real_result) |
4080 | s_return (mk_real (SCHEME_A_ result)); |
4182 | s_return (mk_real (SCHEME_A_ result)); |
4081 | else |
4183 | else |
4082 | s_return (mk_integer (SCHEME_A_ result)); |
4184 | s_return (mk_integer (SCHEME_A_ result)); |
4083 | } |
4185 | } |
4084 | |
|
|
4085 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4086 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4087 | |
|
|
4088 | case OP_TRUNCATE: |
|
|
4089 | { |
|
|
4090 | RVALUE n = rvalue (x); |
|
|
4091 | s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n))); |
|
|
4092 | } |
|
|
4093 | |
|
|
4094 | case OP_ROUND: |
|
|
4095 | if (is_integer (x)) |
|
|
4096 | s_return (x); |
|
|
4097 | |
|
|
4098 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
|
|
4099 | #endif |
4186 | #endif |
4100 | |
4187 | |
4101 | case OP_ADD: /* + */ |
4188 | case OP_ADD: /* + */ |
4102 | v = num_zero; |
4189 | v = num_zero; |
4103 | |
4190 | |
… | |
… | |
4191 | else |
4278 | else |
4192 | Error_0 ("modulo: division by zero"); |
4279 | Error_0 ("modulo: division by zero"); |
4193 | |
4280 | |
4194 | s_return (mk_number (SCHEME_A_ v)); |
4281 | s_return (mk_number (SCHEME_A_ v)); |
4195 | |
4282 | |
4196 | case OP_CAR: /* car */ |
4283 | /* the compiler will optimize this mess... */ |
4197 | s_return (caar (args)); |
4284 | case OP_CAR: op_car: s_return (car (x)); |
4198 | |
4285 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4199 | case OP_CDR: /* cdr */ |
4286 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4200 | s_return (cdar (args)); |
4287 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4288 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4289 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4290 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4291 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4292 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4293 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4294 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4295 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4296 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4297 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4298 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4299 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4300 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4301 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4302 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4303 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4304 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4305 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4306 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4307 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4308 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4309 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4310 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4311 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4312 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4313 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4201 | |
4314 | |
4202 | case OP_CONS: /* cons */ |
4315 | case OP_CONS: /* cons */ |
4203 | set_cdr (args, cadr (args)); |
4316 | set_cdr (args, cadr (args)); |
4204 | s_return (args); |
4317 | s_return (args); |
4205 | |
4318 | |
… | |
… | |
4379 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4492 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4380 | |
4493 | |
4381 | s_return (newstr); |
4494 | s_return (newstr); |
4382 | } |
4495 | } |
4383 | |
4496 | |
4384 | case OP_SUBSTR: /* substring */ |
4497 | case OP_STRING_COPY: /* substring/string-copy */ |
4385 | { |
4498 | { |
4386 | char *str = strvalue (x); |
4499 | char *str = strvalue (x); |
4387 | int index0 = ivalue_unchecked (cadr (args)); |
4500 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4388 | int index1; |
4501 | int index1; |
4389 | int len; |
4502 | int len; |
4390 | |
4503 | |
4391 | if (index0 > strlength (x)) |
4504 | if (index0 > strlength (x)) |
4392 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4505 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4393 | |
4506 | |
4394 | if (cddr (args) != NIL) |
4507 | if (cddr (args) != NIL) |
4395 | { |
4508 | { |
4396 | index1 = ivalue_unchecked (caddr (args)); |
4509 | index1 = ivalue_unchecked (caddr (args)); |
4397 | |
4510 | |
4398 | if (index1 > strlength (x) || index1 < index0) |
4511 | if (index1 > strlength (x) || index1 < index0) |
4399 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4512 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4400 | } |
4513 | } |
4401 | else |
4514 | else |
4402 | index1 = strlength (x); |
4515 | index1 = strlength (x); |
4403 | |
4516 | |
4404 | len = index1 - index0; |
4517 | len = index1 - index0; |
4405 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4518 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4406 | memcpy (strvalue (x), str + index0, len); |
|
|
4407 | strvalue (x)[len] = 0; |
|
|
4408 | |
4519 | |
4409 | s_return (x); |
4520 | s_return (x); |
4410 | } |
4521 | } |
4411 | |
4522 | |
4412 | case OP_VECTOR: /* vector */ |
4523 | case OP_VECTOR: /* vector */ |
… | |
… | |
4486 | } |
4597 | } |
4487 | |
4598 | |
4488 | if (USE_ERROR_CHECKING) abort (); |
4599 | if (USE_ERROR_CHECKING) abort (); |
4489 | } |
4600 | } |
4490 | |
4601 | |
4491 | static int |
4602 | /* relational ops */ |
|
|
4603 | ecb_hot static int |
4492 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4604 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4493 | { |
4605 | { |
4494 | pointer x = SCHEME_V->args; |
4606 | pointer x = SCHEME_V->args; |
4495 | |
4607 | |
4496 | for (;;) |
4608 | for (;;) |
… | |
… | |
4517 | } |
4629 | } |
4518 | |
4630 | |
4519 | s_return (S_T); |
4631 | s_return (S_T); |
4520 | } |
4632 | } |
4521 | |
4633 | |
4522 | static int |
4634 | /* predicates */ |
|
|
4635 | ecb_hot static int |
4523 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4636 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4524 | { |
4637 | { |
4525 | pointer args = SCHEME_V->args; |
4638 | pointer args = SCHEME_V->args; |
4526 | pointer a = car (args); |
4639 | pointer a = car (args); |
4527 | pointer d = cdr (args); |
4640 | pointer d = cdr (args); |
4528 | int r; |
4641 | int r; |
4529 | |
4642 | |
4530 | switch (op) |
4643 | switch (op) |
4531 | { |
4644 | { |
4532 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4645 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4533 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
4646 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break; |
4534 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4647 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4535 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4648 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4536 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
4649 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4650 | case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break; |
4537 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4651 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4538 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4652 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4539 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4653 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4540 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4654 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4541 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4655 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4542 | |
4656 | |
4543 | #if USE_CHAR_CLASSIFIERS |
4657 | #if USE_CHAR_CLASSIFIERS |
4544 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4658 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4545 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4659 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4546 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4660 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
… | |
… | |
4573 | } |
4687 | } |
4574 | |
4688 | |
4575 | s_retbool (r); |
4689 | s_retbool (r); |
4576 | } |
4690 | } |
4577 | |
4691 | |
4578 | static int |
4692 | /* promises, list ops, ports */ |
|
|
4693 | ecb_hot static int |
4579 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4694 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4580 | { |
4695 | { |
4581 | pointer args = SCHEME_V->args; |
4696 | pointer args = SCHEME_V->args; |
4582 | pointer a = car (args); |
4697 | pointer a = car (args); |
4583 | pointer x, y; |
4698 | pointer x, y; |
… | |
… | |
4596 | } |
4711 | } |
4597 | else |
4712 | else |
4598 | s_return (SCHEME_V->code); |
4713 | s_return (SCHEME_V->code); |
4599 | |
4714 | |
4600 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4715 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4601 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4716 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4602 | s_return (SCHEME_V->value); |
4717 | s_return (SCHEME_V->value); |
4603 | |
4718 | |
4604 | #if USE_PORTS |
4719 | #if USE_PORTS |
|
|
4720 | |
|
|
4721 | case OP_EOF_OBJECT: /* eof-object */ |
|
|
4722 | s_return (S_EOF); |
4605 | |
4723 | |
4606 | case OP_WRITE: /* write */ |
4724 | case OP_WRITE: /* write */ |
4607 | case OP_DISPLAY: /* display */ |
4725 | case OP_DISPLAY: /* display */ |
4608 | case OP_WRITE_CHAR: /* write-char */ |
4726 | case OP_WRITE_CHAR: /* write-char */ |
4609 | if (is_pair (cdr (SCHEME_V->args))) |
4727 | if (is_pair (cdr (SCHEME_V->args))) |
… | |
… | |
4623 | else |
4741 | else |
4624 | SCHEME_V->print_flag = 0; |
4742 | SCHEME_V->print_flag = 0; |
4625 | |
4743 | |
4626 | s_goto (OP_P0LIST); |
4744 | s_goto (OP_P0LIST); |
4627 | |
4745 | |
|
|
4746 | //TODO: move to scheme |
4628 | case OP_NEWLINE: /* newline */ |
4747 | case OP_NEWLINE: /* newline */ |
4629 | if (is_pair (args)) |
4748 | if (is_pair (args)) |
4630 | { |
4749 | { |
4631 | if (a != SCHEME_V->outport) |
4750 | if (a != SCHEME_V->outport) |
4632 | { |
4751 | { |
… | |
… | |
4634 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4753 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4635 | SCHEME_V->outport = a; |
4754 | SCHEME_V->outport = a; |
4636 | } |
4755 | } |
4637 | } |
4756 | } |
4638 | |
4757 | |
4639 | putstr (SCHEME_A_ "\n"); |
4758 | putcharacter (SCHEME_A_ '\n'); |
4640 | s_return (S_T); |
4759 | s_return (S_T); |
4641 | #endif |
4760 | #endif |
4642 | |
4761 | |
4643 | case OP_ERR0: /* error */ |
4762 | case OP_ERR0: /* error */ |
4644 | SCHEME_V->retcode = -1; |
4763 | SCHEME_V->retcode = -1; |
… | |
… | |
4653 | putstr (SCHEME_A_ strvalue (car (args))); |
4772 | putstr (SCHEME_A_ strvalue (car (args))); |
4654 | SCHEME_V->args = cdr (args); |
4773 | SCHEME_V->args = cdr (args); |
4655 | s_goto (OP_ERR1); |
4774 | s_goto (OP_ERR1); |
4656 | |
4775 | |
4657 | case OP_ERR1: /* error */ |
4776 | case OP_ERR1: /* error */ |
4658 | putstr (SCHEME_A_ " "); |
4777 | putcharacter (SCHEME_A_ ' '); |
4659 | |
4778 | |
4660 | if (args != NIL) |
4779 | if (args != NIL) |
4661 | { |
4780 | { |
4662 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4781 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4663 | SCHEME_V->args = a; |
4782 | SCHEME_V->args = a; |
4664 | SCHEME_V->print_flag = 1; |
4783 | SCHEME_V->print_flag = 1; |
4665 | s_goto (OP_P0LIST); |
4784 | s_goto (OP_P0LIST); |
4666 | } |
4785 | } |
4667 | else |
4786 | else |
4668 | { |
4787 | { |
4669 | putstr (SCHEME_A_ "\n"); |
4788 | putcharacter (SCHEME_A_ '\n'); |
4670 | |
4789 | |
4671 | if (SCHEME_V->interactive_repl) |
4790 | if (SCHEME_V->interactive_repl) |
4672 | s_goto (OP_T0LVL); |
4791 | s_goto (OP_T0LVL); |
4673 | else |
4792 | else |
4674 | return -1; |
4793 | return -1; |
… | |
… | |
4751 | SCHEME_V->gc_verbose = (a != S_F); |
4870 | SCHEME_V->gc_verbose = (a != S_F); |
4752 | s_retbool (was); |
4871 | s_retbool (was); |
4753 | } |
4872 | } |
4754 | |
4873 | |
4755 | case OP_NEWSEGMENT: /* new-segment */ |
4874 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4875 | #if 0 |
4756 | if (!is_pair (args) || !is_number (a)) |
4876 | if (!is_pair (args) || !is_number (a)) |
4757 | Error_0 ("new-segment: argument must be a number"); |
4877 | Error_0 ("new-segment: argument must be a number"); |
4758 | |
4878 | #endif |
4759 | alloc_cellseg (SCHEME_A_ ivalue (a)); |
4879 | s_retbool (alloc_cellseg (SCHEME_A)); |
4760 | |
|
|
4761 | s_return (S_T); |
|
|
4762 | |
4880 | |
4763 | case OP_OBLIST: /* oblist */ |
4881 | case OP_OBLIST: /* oblist */ |
4764 | s_return (oblist_all_symbols (SCHEME_A)); |
4882 | s_return (oblist_all_symbols (SCHEME_A)); |
4765 | |
4883 | |
4766 | #if USE_PORTS |
4884 | #if USE_PORTS |
… | |
… | |
4836 | s_return (p == NIL ? S_F : p); |
4954 | s_return (p == NIL ? S_F : p); |
4837 | } |
4955 | } |
4838 | |
4956 | |
4839 | case OP_GET_OUTSTRING: /* get-output-string */ |
4957 | case OP_GET_OUTSTRING: /* get-output-string */ |
4840 | { |
4958 | { |
4841 | port *p; |
4959 | port *p = port (a); |
4842 | |
4960 | |
4843 | if ((p = a->object.port)->kind & port_string) |
4961 | if (p->kind & port_string) |
4844 | { |
4962 | { |
4845 | off_t size; |
4963 | off_t size; |
4846 | char *str; |
4964 | char *str; |
4847 | |
4965 | |
4848 | size = p->rep.string.curr - p->rep.string.start + 1; |
4966 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4883 | } |
5001 | } |
4884 | |
5002 | |
4885 | if (USE_ERROR_CHECKING) abort (); |
5003 | if (USE_ERROR_CHECKING) abort (); |
4886 | } |
5004 | } |
4887 | |
5005 | |
4888 | static int |
5006 | /* reading */ |
|
|
5007 | ecb_cold static int |
4889 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
5008 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4890 | { |
5009 | { |
4891 | pointer args = SCHEME_V->args; |
5010 | pointer args = SCHEME_V->args; |
4892 | pointer x; |
5011 | pointer x; |
4893 | |
5012 | |
… | |
… | |
4953 | int res; |
5072 | int res; |
4954 | |
5073 | |
4955 | if (is_pair (args)) |
5074 | if (is_pair (args)) |
4956 | p = car (args); |
5075 | p = car (args); |
4957 | |
5076 | |
4958 | res = p->object.port->kind & port_string; |
5077 | res = port (p)->kind & port_string; |
4959 | |
5078 | |
4960 | s_retbool (res); |
5079 | s_retbool (res); |
4961 | } |
5080 | } |
4962 | |
5081 | |
4963 | case OP_SET_INPORT: /* set-input-port */ |
5082 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
5162 | pointer b = cdr (args); |
5281 | pointer b = cdr (args); |
5163 | int ok_abbr = ok_abbrev (b); |
5282 | int ok_abbr = ok_abbrev (b); |
5164 | SCHEME_V->args = car (b); |
5283 | SCHEME_V->args = car (b); |
5165 | |
5284 | |
5166 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5285 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5167 | putstr (SCHEME_A_ "'"); |
5286 | putcharacter (SCHEME_A_ '\''); |
5168 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5287 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5169 | putstr (SCHEME_A_ "`"); |
5288 | putcharacter (SCHEME_A_ '`'); |
5170 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5289 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5171 | putstr (SCHEME_A_ ","); |
5290 | putcharacter (SCHEME_A_ ','); |
5172 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5291 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5173 | putstr (SCHEME_A_ ",@"); |
5292 | putstr (SCHEME_A_ ",@"); |
5174 | else |
5293 | else |
5175 | { |
5294 | { |
5176 | putstr (SCHEME_A_ "("); |
5295 | putcharacter (SCHEME_A_ '('); |
5177 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5296 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5178 | SCHEME_V->args = a; |
5297 | SCHEME_V->args = a; |
5179 | } |
5298 | } |
5180 | |
5299 | |
5181 | s_goto (OP_P0LIST); |
5300 | s_goto (OP_P0LIST); |
… | |
… | |
5183 | |
5302 | |
5184 | case OP_P1LIST: |
5303 | case OP_P1LIST: |
5185 | if (is_pair (args)) |
5304 | if (is_pair (args)) |
5186 | { |
5305 | { |
5187 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5306 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5188 | putstr (SCHEME_A_ " "); |
5307 | putcharacter (SCHEME_A_ ' '); |
5189 | SCHEME_V->args = car (args); |
5308 | SCHEME_V->args = car (args); |
5190 | s_goto (OP_P0LIST); |
5309 | s_goto (OP_P0LIST); |
5191 | } |
5310 | } |
5192 | else if (is_vector (args)) |
5311 | else if (is_vector (args)) |
5193 | { |
5312 | { |
… | |
… | |
5201 | { |
5320 | { |
5202 | putstr (SCHEME_A_ " . "); |
5321 | putstr (SCHEME_A_ " . "); |
5203 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5322 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5204 | } |
5323 | } |
5205 | |
5324 | |
5206 | putstr (SCHEME_A_ ")"); |
5325 | putcharacter (SCHEME_A_ ')'); |
5207 | s_return (S_T); |
5326 | s_return (S_T); |
5208 | } |
5327 | } |
5209 | |
5328 | |
5210 | case OP_PVECFROM: |
5329 | case OP_PVECFROM: |
5211 | { |
5330 | { |
… | |
… | |
5213 | pointer vec = car (args); |
5332 | pointer vec = car (args); |
5214 | int len = veclength (vec); |
5333 | int len = veclength (vec); |
5215 | |
5334 | |
5216 | if (i == len) |
5335 | if (i == len) |
5217 | { |
5336 | { |
5218 | putstr (SCHEME_A_ ")"); |
5337 | putcharacter (SCHEME_A_ ')'); |
5219 | s_return (S_T); |
5338 | s_return (S_T); |
5220 | } |
5339 | } |
5221 | else |
5340 | else |
5222 | { |
5341 | { |
5223 | pointer elem = vector_get (vec, i); |
5342 | pointer elem = vector_get (vec, i); |
… | |
… | |
5225 | ivalue_unchecked (cdr (args)) = i + 1; |
5344 | ivalue_unchecked (cdr (args)) = i + 1; |
5226 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5345 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5227 | SCHEME_V->args = elem; |
5346 | SCHEME_V->args = elem; |
5228 | |
5347 | |
5229 | if (i > 0) |
5348 | if (i > 0) |
5230 | putstr (SCHEME_A_ " "); |
5349 | putcharacter (SCHEME_A_ ' '); |
5231 | |
5350 | |
5232 | s_goto (OP_P0LIST); |
5351 | s_goto (OP_P0LIST); |
5233 | } |
5352 | } |
5234 | } |
5353 | } |
5235 | } |
5354 | } |
5236 | |
5355 | |
5237 | if (USE_ERROR_CHECKING) abort (); |
5356 | if (USE_ERROR_CHECKING) abort (); |
5238 | } |
5357 | } |
5239 | |
5358 | |
5240 | static int |
5359 | /* list ops */ |
|
|
5360 | ecb_hot static int |
5241 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5361 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5242 | { |
5362 | { |
5243 | pointer args = SCHEME_V->args; |
5363 | pointer args = SCHEME_V->args; |
5244 | pointer a = car (args); |
5364 | pointer a = car (args); |
5245 | pointer x, y; |
5365 | pointer x, y; |
… | |
… | |
5268 | break; |
5388 | break; |
5269 | } |
5389 | } |
5270 | |
5390 | |
5271 | if (is_pair (y)) |
5391 | if (is_pair (y)) |
5272 | s_return (car (y)); |
5392 | s_return (car (y)); |
5273 | else |
5393 | |
5274 | s_return (S_F); |
5394 | s_return (S_F); |
5275 | |
|
|
5276 | |
5395 | |
5277 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5396 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5278 | SCHEME_V->args = a; |
5397 | SCHEME_V->args = a; |
5279 | |
5398 | |
5280 | if (SCHEME_V->args == NIL) |
5399 | if (SCHEME_V->args == NIL) |
5281 | s_return (S_F); |
5400 | s_return (S_F); |
5282 | else if (is_closure (SCHEME_V->args)) |
5401 | else if (is_closure (SCHEME_V->args) || is_macro (SCHEME_V->args)) |
5283 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
5402 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
5284 | else if (is_macro (SCHEME_V->args)) |
5403 | |
5285 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
|
|
5286 | else |
|
|
5287 | s_return (S_F); |
5404 | s_return (S_F); |
5288 | |
5405 | |
5289 | case OP_CLOSUREP: /* closure? */ |
5406 | case OP_CLOSUREP: /* closure? */ |
5290 | /* |
5407 | /* |
5291 | * Note, macro object is also a closure. |
5408 | * Note, macro object is also a closure. |
5292 | * Therefore, (closure? <#MACRO>) ==> #t |
5409 | * Therefore, (closure? <#MACRO>) ==> #t |
… | |
… | |
5303 | |
5420 | |
5304 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5421 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5305 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5422 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5306 | |
5423 | |
5307 | typedef int (*test_predicate)(pointer); |
5424 | typedef int (*test_predicate)(pointer); |
5308 | static int |
5425 | |
|
|
5426 | ecb_hot static int |
5309 | tst_any (pointer p) |
5427 | tst_any (pointer p) |
5310 | { |
5428 | { |
5311 | return 1; |
5429 | return 1; |
5312 | } |
5430 | } |
5313 | |
5431 | |
5314 | static int |
5432 | ecb_hot static int |
5315 | tst_inonneg (pointer p) |
5433 | tst_inonneg (pointer p) |
5316 | { |
5434 | { |
5317 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5435 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5318 | } |
5436 | } |
5319 | |
5437 | |
5320 | static int |
5438 | ecb_hot static int |
5321 | tst_is_list (SCHEME_P_ pointer p) |
5439 | tst_is_list (SCHEME_P_ pointer p) |
5322 | { |
5440 | { |
5323 | return p == NIL || is_pair (p); |
5441 | return p == NIL || is_pair (p); |
5324 | } |
5442 | } |
5325 | |
5443 | |
… | |
… | |
5368 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5486 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5369 | #include "opdefines.h" |
5487 | #include "opdefines.h" |
5370 | #undef OP_DEF |
5488 | #undef OP_DEF |
5371 | ; |
5489 | ; |
5372 | |
5490 | |
5373 | static const char * |
5491 | ecb_cold static const char * |
5374 | opname (int idx) |
5492 | opname (int idx) |
5375 | { |
5493 | { |
5376 | const char *name = opnames; |
5494 | const char *name = opnames; |
5377 | |
5495 | |
5378 | /* should do this at compile time, but would require external program, right? */ |
5496 | /* should do this at compile time, but would require external program, right? */ |
… | |
… | |
5380 | name += strlen (name) + 1; |
5498 | name += strlen (name) + 1; |
5381 | |
5499 | |
5382 | return *name ? name : "ILLEGAL"; |
5500 | return *name ? name : "ILLEGAL"; |
5383 | } |
5501 | } |
5384 | |
5502 | |
5385 | static const char * |
5503 | ecb_cold static const char * |
5386 | procname (pointer x) |
5504 | procname (pointer x) |
5387 | { |
5505 | { |
5388 | return opname (procnum (x)); |
5506 | return opname (procnum (x)); |
5389 | } |
5507 | } |
5390 | |
5508 | |
… | |
… | |
5410 | #undef OP_DEF |
5528 | #undef OP_DEF |
5411 | {0} |
5529 | {0} |
5412 | }; |
5530 | }; |
5413 | |
5531 | |
5414 | /* kernel of this interpreter */ |
5532 | /* kernel of this interpreter */ |
5415 | static void ecb_hot |
5533 | ecb_hot static void |
5416 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5534 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5417 | { |
5535 | { |
5418 | SCHEME_V->op = op; |
5536 | SCHEME_V->op = op; |
5419 | |
5537 | |
5420 | for (;;) |
5538 | for (;;) |
… | |
… | |
5503 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5621 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5504 | return; |
5622 | return; |
5505 | |
5623 | |
5506 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5624 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5507 | { |
5625 | { |
5508 | xwrstr ("No memory!\n"); |
5626 | putstr (SCHEME_A_ "No memory!\n"); |
5509 | return; |
5627 | return; |
5510 | } |
5628 | } |
5511 | } |
5629 | } |
5512 | } |
5630 | } |
5513 | |
5631 | |
5514 | /* ========== Initialization of internal keywords ========== */ |
5632 | /* ========== Initialization of internal keywords ========== */ |
5515 | |
5633 | |
5516 | static void |
5634 | ecb_cold static void |
5517 | assign_syntax (SCHEME_P_ const char *name) |
5635 | assign_syntax (SCHEME_P_ const char *name) |
5518 | { |
5636 | { |
5519 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5637 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5520 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5638 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5521 | } |
5639 | } |
5522 | |
5640 | |
5523 | static void |
5641 | ecb_cold static void |
5524 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5642 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5525 | { |
5643 | { |
5526 | pointer x = mk_symbol (SCHEME_A_ name); |
5644 | pointer x = mk_symbol (SCHEME_A_ name); |
5527 | pointer y = mk_proc (SCHEME_A_ op); |
5645 | pointer y = mk_proc (SCHEME_A_ op); |
5528 | new_slot_in_env (SCHEME_A_ x, y); |
5646 | new_slot_in_env (SCHEME_A_ x, y); |
… | |
… | |
5536 | ivalue_unchecked (y) = op; |
5654 | ivalue_unchecked (y) = op; |
5537 | return y; |
5655 | return y; |
5538 | } |
5656 | } |
5539 | |
5657 | |
5540 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5658 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5541 | static int |
5659 | ecb_hot static int |
5542 | syntaxnum (pointer p) |
5660 | syntaxnum (pointer p) |
5543 | { |
5661 | { |
5544 | const char *s = strvalue (p); |
5662 | const char *s = strvalue (p); |
5545 | |
5663 | |
5546 | switch (strlength (p)) |
5664 | switch (strlength (p)) |
… | |
… | |
5626 | ecb_cold int |
5744 | ecb_cold int |
5627 | scheme_init (SCHEME_P) |
5745 | scheme_init (SCHEME_P) |
5628 | { |
5746 | { |
5629 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5747 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5630 | pointer x; |
5748 | pointer x; |
|
|
5749 | |
|
|
5750 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
5751 | * that memset 0 will also set pointers to 0, but memset does |
|
|
5752 | * of course not guarantee that. screw such systems. |
|
|
5753 | */ |
|
|
5754 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5631 | |
5755 | |
5632 | num_set_fixnum (num_zero, 1); |
5756 | num_set_fixnum (num_zero, 1); |
5633 | num_set_ivalue (num_zero, 0); |
5757 | num_set_ivalue (num_zero, 0); |
5634 | num_set_fixnum (num_one, 1); |
5758 | num_set_fixnum (num_one, 1); |
5635 | num_set_ivalue (num_one, 1); |
5759 | num_set_ivalue (num_one, 1); |
… | |
… | |
5647 | SCHEME_V->save_inport = NIL; |
5771 | SCHEME_V->save_inport = NIL; |
5648 | SCHEME_V->loadport = NIL; |
5772 | SCHEME_V->loadport = NIL; |
5649 | SCHEME_V->nesting = 0; |
5773 | SCHEME_V->nesting = 0; |
5650 | SCHEME_V->interactive_repl = 0; |
5774 | SCHEME_V->interactive_repl = 0; |
5651 | |
5775 | |
5652 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5776 | if (!alloc_cellseg (SCHEME_A)) |
5653 | { |
5777 | { |
5654 | #if USE_ERROR_CHECKING |
5778 | #if USE_ERROR_CHECKING |
5655 | SCHEME_V->no_memory = 1; |
5779 | SCHEME_V->no_memory = 1; |
5656 | return 0; |
5780 | return 0; |
5657 | #endif |
5781 | #endif |
5658 | } |
5782 | } |
5659 | |
5783 | |
5660 | SCHEME_V->gc_verbose = 0; |
5784 | SCHEME_V->gc_verbose = 0; |
5661 | dump_stack_initialize (SCHEME_A); |
5785 | dump_stack_initialize (SCHEME_A); |
5662 | SCHEME_V->code = NIL; |
5786 | SCHEME_V->code = NIL; |
5663 | SCHEME_V->args = NIL; |
5787 | SCHEME_V->args = NIL; |
5664 | SCHEME_V->envir = NIL; |
5788 | SCHEME_V->envir = NIL; |
|
|
5789 | SCHEME_V->value = NIL; |
5665 | SCHEME_V->tracing = 0; |
5790 | SCHEME_V->tracing = 0; |
5666 | |
5791 | |
5667 | /* init NIL */ |
5792 | /* init NIL */ |
5668 | set_typeflag (NIL, T_ATOM | T_MARK); |
5793 | set_typeflag (NIL, T_ATOM | T_MARK); |
5669 | set_car (NIL, NIL); |
5794 | set_car (NIL, NIL); |
… | |
… | |
5725 | |
5850 | |
5726 | return !SCHEME_V->no_memory; |
5851 | return !SCHEME_V->no_memory; |
5727 | } |
5852 | } |
5728 | |
5853 | |
5729 | #if USE_PORTS |
5854 | #if USE_PORTS |
5730 | void |
5855 | ecb_cold void |
5731 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5856 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5732 | { |
5857 | { |
5733 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5858 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5734 | } |
5859 | } |
5735 | |
5860 | |
5736 | void |
5861 | ecb_cold void |
5737 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5862 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5738 | { |
5863 | { |
5739 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5864 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5740 | } |
5865 | } |
5741 | |
5866 | |
5742 | void |
5867 | ecb_cold void |
5743 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5868 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5744 | { |
5869 | { |
5745 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5870 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5746 | } |
5871 | } |
5747 | |
5872 | |
5748 | void |
5873 | ecb_cold void |
5749 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5874 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5750 | { |
5875 | { |
5751 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5876 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5752 | } |
5877 | } |
5753 | #endif |
5878 | #endif |
5754 | |
5879 | |
5755 | void |
5880 | ecb_cold void |
5756 | scheme_set_external_data (SCHEME_P_ void *p) |
5881 | scheme_set_external_data (SCHEME_P_ void *p) |
5757 | { |
5882 | { |
5758 | SCHEME_V->ext_data = p; |
5883 | SCHEME_V->ext_data = p; |
5759 | } |
5884 | } |
5760 | |
5885 | |
… | |
… | |
5792 | SCHEME_V->loadport = NIL; |
5917 | SCHEME_V->loadport = NIL; |
5793 | SCHEME_V->gc_verbose = 0; |
5918 | SCHEME_V->gc_verbose = 0; |
5794 | gc (SCHEME_A_ NIL, NIL); |
5919 | gc (SCHEME_A_ NIL, NIL); |
5795 | |
5920 | |
5796 | for (i = 0; i <= SCHEME_V->last_cell_seg; i++) |
5921 | for (i = 0; i <= SCHEME_V->last_cell_seg; i++) |
5797 | free (SCHEME_V->alloc_seg[i]); |
5922 | free (SCHEME_V->cell_seg[i]); |
5798 | |
5923 | |
5799 | #if SHOW_ERROR_LINE |
5924 | #if SHOW_ERROR_LINE |
5800 | for (i = 0; i <= SCHEME_V->file_i; i++) |
5925 | for (i = 0; i <= SCHEME_V->file_i; i++) |
5801 | { |
|
|
5802 | if (SCHEME_V->load_stack[i].kind & port_file) |
5926 | if (SCHEME_V->load_stack[i].kind & port_file) |
5803 | { |
5927 | { |
5804 | fname = SCHEME_V->load_stack[i].rep.stdio.filename; |
5928 | fname = SCHEME_V->load_stack[i].rep.stdio.filename; |
5805 | |
5929 | |
5806 | if (fname) |
5930 | if (fname) |
5807 | free (fname); |
5931 | free (fname); |
5808 | } |
5932 | } |
5809 | } |
|
|
5810 | #endif |
5933 | #endif |
5811 | } |
5934 | } |
5812 | |
5935 | |
5813 | void |
5936 | ecb_cold void |
5814 | scheme_load_file (SCHEME_P_ int fin) |
5937 | scheme_load_file (SCHEME_P_ int fin) |
5815 | { |
5938 | { |
5816 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5939 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5817 | } |
5940 | } |
5818 | |
5941 | |
5819 | void |
5942 | ecb_cold void |
5820 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5943 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5821 | { |
5944 | { |
5822 | dump_stack_reset (SCHEME_A); |
5945 | dump_stack_reset (SCHEME_A); |
5823 | SCHEME_V->envir = SCHEME_V->global_env; |
5946 | SCHEME_V->envir = SCHEME_V->global_env; |
5824 | SCHEME_V->file_i = 0; |
5947 | SCHEME_V->file_i = 0; |
5825 | SCHEME_V->load_stack[0].unget = -1; |
5948 | SCHEME_V->load_stack[0].unget = -1; |
5826 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5949 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5827 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5950 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5828 | #if USE_PORTS |
|
|
5829 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5951 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5830 | #endif |
|
|
5831 | SCHEME_V->retcode = 0; |
5952 | SCHEME_V->retcode = 0; |
5832 | |
5953 | |
5833 | #if USE_PORTS |
|
|
5834 | if (fin == STDIN_FILENO) |
5954 | if (fin == STDIN_FILENO) |
5835 | SCHEME_V->interactive_repl = 1; |
5955 | SCHEME_V->interactive_repl = 1; |
5836 | #endif |
|
|
5837 | |
5956 | |
5838 | #if USE_PORTS |
5957 | #if USE_PORTS |
5839 | #if SHOW_ERROR_LINE |
5958 | #if SHOW_ERROR_LINE |
5840 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5959 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5841 | |
5960 | |
… | |
… | |
5845 | #endif |
5964 | #endif |
5846 | |
5965 | |
5847 | SCHEME_V->inport = SCHEME_V->loadport; |
5966 | SCHEME_V->inport = SCHEME_V->loadport; |
5848 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5967 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5849 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5968 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
|
|
5969 | |
5850 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5970 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5851 | |
5971 | |
5852 | if (SCHEME_V->retcode == 0) |
5972 | if (SCHEME_V->retcode == 0) |
5853 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5973 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5854 | } |
5974 | } |
5855 | |
5975 | |
5856 | void |
5976 | ecb_cold void |
5857 | scheme_load_string (SCHEME_P_ const char *cmd) |
5977 | scheme_load_string (SCHEME_P_ const char *cmd) |
5858 | { |
5978 | { |
|
|
5979 | #if USE_PORTs |
5859 | dump_stack_reset (SCHEME_A); |
5980 | dump_stack_reset (SCHEME_A); |
5860 | SCHEME_V->envir = SCHEME_V->global_env; |
5981 | SCHEME_V->envir = SCHEME_V->global_env; |
5861 | SCHEME_V->file_i = 0; |
5982 | SCHEME_V->file_i = 0; |
5862 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5983 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5863 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
5984 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
5864 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
5985 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
5865 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5986 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5866 | #if USE_PORTS |
|
|
5867 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5987 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5868 | #endif |
|
|
5869 | SCHEME_V->retcode = 0; |
5988 | SCHEME_V->retcode = 0; |
5870 | SCHEME_V->interactive_repl = 0; |
5989 | SCHEME_V->interactive_repl = 0; |
5871 | SCHEME_V->inport = SCHEME_V->loadport; |
5990 | SCHEME_V->inport = SCHEME_V->loadport; |
5872 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5991 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5873 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5992 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5874 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5993 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5875 | |
5994 | |
5876 | if (SCHEME_V->retcode == 0) |
5995 | if (SCHEME_V->retcode == 0) |
5877 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5996 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
|
|
5997 | #else |
|
|
5998 | abort (); |
|
|
5999 | #endif |
5878 | } |
6000 | } |
5879 | |
6001 | |
5880 | void |
6002 | ecb_cold void |
5881 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
6003 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5882 | { |
6004 | { |
5883 | pointer x; |
6005 | pointer x; |
5884 | |
6006 | |
5885 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
6007 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
… | |
… | |
5890 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
6012 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5891 | } |
6013 | } |
5892 | |
6014 | |
5893 | #if !STANDALONE |
6015 | #if !STANDALONE |
5894 | |
6016 | |
5895 | void |
6017 | ecb_cold void |
5896 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
6018 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5897 | { |
6019 | { |
5898 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
6020 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5899 | } |
6021 | } |
5900 | |
6022 | |
5901 | void |
6023 | ecb_cold void |
5902 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
6024 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5903 | { |
6025 | { |
5904 | int i; |
6026 | int i; |
5905 | |
6027 | |
5906 | for (i = 0; i < count; i++) |
6028 | for (i = 0; i < count; i++) |
5907 | scheme_register_foreign_func (SCHEME_A_ list + i); |
6029 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5908 | } |
6030 | } |
5909 | |
6031 | |
5910 | pointer |
6032 | ecb_cold pointer |
5911 | scheme_apply0 (SCHEME_P_ const char *procname) |
6033 | scheme_apply0 (SCHEME_P_ const char *procname) |
5912 | { |
6034 | { |
5913 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
6035 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
5914 | } |
6036 | } |
5915 | |
6037 | |
5916 | void |
6038 | ecb_cold void |
5917 | save_from_C_call (SCHEME_P) |
6039 | save_from_C_call (SCHEME_P) |
5918 | { |
6040 | { |
5919 | pointer saved_data = cons (car (S_SINK), |
6041 | pointer saved_data = cons (car (S_SINK), |
5920 | cons (SCHEME_V->envir, |
6042 | cons (SCHEME_V->envir, |
5921 | SCHEME_V->dump)); |
6043 | SCHEME_V->dump)); |
… | |
… | |
5925 | /* Truncate the dump stack so TS will return here when done, not |
6047 | /* Truncate the dump stack so TS will return here when done, not |
5926 | directly resume pre-C-call operations. */ |
6048 | directly resume pre-C-call operations. */ |
5927 | dump_stack_reset (SCHEME_A); |
6049 | dump_stack_reset (SCHEME_A); |
5928 | } |
6050 | } |
5929 | |
6051 | |
5930 | void |
6052 | ecb_cold void |
5931 | restore_from_C_call (SCHEME_P) |
6053 | restore_from_C_call (SCHEME_P) |
5932 | { |
6054 | { |
5933 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
6055 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5934 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
6056 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5935 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
6057 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5936 | /* Pop */ |
6058 | /* Pop */ |
5937 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
6059 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5938 | } |
6060 | } |
5939 | |
6061 | |
5940 | /* "func" and "args" are assumed to be already eval'ed. */ |
6062 | /* "func" and "args" are assumed to be already eval'ed. */ |
5941 | pointer |
6063 | ecb_cold pointer |
5942 | scheme_call (SCHEME_P_ pointer func, pointer args) |
6064 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5943 | { |
6065 | { |
5944 | int old_repl = SCHEME_V->interactive_repl; |
6066 | int old_repl = SCHEME_V->interactive_repl; |
5945 | |
6067 | |
5946 | SCHEME_V->interactive_repl = 0; |
6068 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5953 | SCHEME_V->interactive_repl = old_repl; |
6075 | SCHEME_V->interactive_repl = old_repl; |
5954 | restore_from_C_call (SCHEME_A); |
6076 | restore_from_C_call (SCHEME_A); |
5955 | return SCHEME_V->value; |
6077 | return SCHEME_V->value; |
5956 | } |
6078 | } |
5957 | |
6079 | |
5958 | pointer |
6080 | ecb_cold pointer |
5959 | scheme_eval (SCHEME_P_ pointer obj) |
6081 | scheme_eval (SCHEME_P_ pointer obj) |
5960 | { |
6082 | { |
5961 | int old_repl = SCHEME_V->interactive_repl; |
6083 | int old_repl = SCHEME_V->interactive_repl; |
5962 | |
6084 | |
5963 | SCHEME_V->interactive_repl = 0; |
6085 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5975 | |
6097 | |
5976 | /* ========== Main ========== */ |
6098 | /* ========== Main ========== */ |
5977 | |
6099 | |
5978 | #if STANDALONE |
6100 | #if STANDALONE |
5979 | |
6101 | |
5980 | int |
6102 | ecb_cold int |
5981 | main (int argc, char **argv) |
6103 | main (int argc, char **argv) |
5982 | { |
6104 | { |
5983 | # if USE_MULTIPLICITY |
6105 | # if USE_MULTIPLICITY |
5984 | scheme ssc; |
6106 | scheme ssc; |
5985 | scheme *const SCHEME_V = &ssc; |
6107 | scheme *const SCHEME_V = &ssc; |
… | |
… | |
5987 | # endif |
6109 | # endif |
5988 | int fin; |
6110 | int fin; |
5989 | char *file_name = InitFile; |
6111 | char *file_name = InitFile; |
5990 | int retcode; |
6112 | int retcode; |
5991 | int isfile = 1; |
6113 | int isfile = 1; |
|
|
6114 | #if EXPERIMENT |
5992 | system ("ps v $PPID");//D |
6115 | system ("ps v $PPID"); |
|
|
6116 | #endif |
5993 | |
6117 | |
5994 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6118 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5995 | { |
6119 | { |
5996 | xwrstr ("Usage: tinyscheme -?\n"); |
6120 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
5997 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6121 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
5998 | xwrstr ("followed by\n"); |
6122 | putstr (SCHEME_A_ "followed by\n"); |
5999 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6123 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
6000 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6124 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6001 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6125 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
6002 | xwrstr ("Use - as filename for stdin.\n"); |
6126 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
6003 | return 1; |
6127 | return 1; |
6004 | } |
6128 | } |
6005 | |
6129 | |
6006 | if (!scheme_init (SCHEME_A)) |
6130 | if (!scheme_init (SCHEME_A)) |
6007 | { |
6131 | { |
6008 | xwrstr ("Could not initialize!\n"); |
6132 | putstr (SCHEME_A_ "Could not initialize!\n"); |
6009 | return 2; |
6133 | return 2; |
6010 | } |
6134 | } |
6011 | |
6135 | |
6012 | # if USE_PORTS |
6136 | # if USE_PORTS |
6013 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6137 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
6026 | } |
6150 | } |
6027 | #endif |
6151 | #endif |
6028 | |
6152 | |
6029 | do |
6153 | do |
6030 | { |
6154 | { |
6031 | #if USE_PORTS |
|
|
6032 | if (strcmp (file_name, "-") == 0) |
6155 | if (strcmp (file_name, "-") == 0) |
6033 | fin = STDIN_FILENO; |
6156 | fin = STDIN_FILENO; |
6034 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
6157 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
6035 | { |
6158 | { |
6036 | pointer args = NIL; |
6159 | pointer args = NIL; |
… | |
… | |
6054 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
6177 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
6055 | |
6178 | |
6056 | } |
6179 | } |
6057 | else |
6180 | else |
6058 | fin = open (file_name, O_RDONLY); |
6181 | fin = open (file_name, O_RDONLY); |
6059 | #endif |
|
|
6060 | |
6182 | |
6061 | if (isfile && fin < 0) |
6183 | if (isfile && fin < 0) |
6062 | { |
6184 | { |
6063 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6185 | putstr (SCHEME_A_ "Could not open file "); |
|
|
6186 | putstr (SCHEME_A_ file_name); |
|
|
6187 | putcharacter (SCHEME_A_ '\n'); |
6064 | } |
6188 | } |
6065 | else |
6189 | else |
6066 | { |
6190 | { |
6067 | if (isfile) |
6191 | if (isfile) |
6068 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6192 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6069 | else |
6193 | else |
6070 | scheme_load_string (SCHEME_A_ file_name); |
6194 | scheme_load_string (SCHEME_A_ file_name); |
6071 | |
6195 | |
6072 | #if USE_PORTS |
|
|
6073 | if (!isfile || fin != STDIN_FILENO) |
6196 | if (!isfile || fin != STDIN_FILENO) |
6074 | { |
6197 | { |
6075 | if (SCHEME_V->retcode != 0) |
6198 | if (SCHEME_V->retcode != 0) |
6076 | { |
6199 | { |
6077 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6200 | putstr (SCHEME_A_ "Errors encountered reading "); |
|
|
6201 | putstr (SCHEME_A_ file_name); |
|
|
6202 | putcharacter (SCHEME_A_ '\n'); |
6078 | } |
6203 | } |
6079 | |
6204 | |
6080 | if (isfile) |
6205 | if (isfile) |
6081 | close (fin); |
6206 | close (fin); |
6082 | } |
6207 | } |
6083 | #endif |
|
|
6084 | } |
6208 | } |
6085 | |
6209 | |
6086 | file_name = *argv++; |
6210 | file_name = *argv++; |
6087 | } |
6211 | } |
6088 | while (file_name != 0); |
6212 | while (file_name != 0); |