… | |
… | |
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> |
… | |
… | |
45 | #include <string.h> |
47 | #include <string.h> |
46 | |
48 | |
47 | #include <limits.h> |
49 | #include <limits.h> |
48 | #include <inttypes.h> |
50 | #include <inttypes.h> |
49 | #include <float.h> |
51 | #include <float.h> |
50 | //#include <ctype.h> |
52 | |
|
|
53 | #if !USE_SYSTEM_MALLOC |
|
|
54 | # define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
|
|
55 | # include "malloc.c" |
|
|
56 | # define malloc(n) tiny_malloc (n) |
|
|
57 | # define realloc(p,n) tiny_realloc (p, n) |
|
|
58 | # define free(p) tiny_free (p) |
|
|
59 | #endif |
51 | |
60 | |
52 | #if '1' != '0' + 1 \ |
61 | #if '1' != '0' + 1 \ |
53 | || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ |
62 | || '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 \ |
63 | || '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 \ |
64 | || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ |
… | |
… | |
74 | TOK_SHARP_CONST, |
83 | TOK_SHARP_CONST, |
75 | TOK_VEC |
84 | TOK_VEC |
76 | }; |
85 | }; |
77 | |
86 | |
78 | #define BACKQUOTE '`' |
87 | #define BACKQUOTE '`' |
79 | #define DELIMITERS "()\";\f\t\v\n\r " |
88 | #define WHITESPACE " \t\r\n\v\f" |
|
|
89 | #define DELIMITERS "()\";" WHITESPACE |
80 | |
90 | |
81 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
91 | #define NIL POINTER (&SCHEME_V->xNIL) |
82 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
92 | #define S_T POINTER (&SCHEME_V->xT) |
83 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
93 | #define S_F POINTER (&SCHEME_V->xF) |
84 | #define S_SINK (&SCHEME_V->xsink) |
94 | #define S_SINK POINTER (&SCHEME_V->xsink) |
85 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
95 | #define S_EOF POINTER (&SCHEME_V->xEOF_OBJ) |
86 | |
96 | |
87 | #if !USE_MULTIPLICITY |
97 | #if !USE_MULTIPLICITY |
88 | static scheme sc; |
98 | static scheme sc; |
89 | #endif |
99 | #endif |
90 | |
100 | |
91 | static void |
101 | ecb_cold static void |
92 | xbase (char *s, long n, int base) |
102 | xbase (char *s, long n, int base) |
93 | { |
103 | { |
94 | if (n < 0) |
104 | if (n < 0) |
95 | { |
105 | { |
96 | *s++ = '-'; |
106 | *s++ = '-'; |
… | |
… | |
98 | } |
108 | } |
99 | |
109 | |
100 | char *p = s; |
110 | char *p = s; |
101 | |
111 | |
102 | do { |
112 | do { |
103 | *p++ = '0' + n % base; |
113 | *p++ = "0123456789abcdef"[n % base]; |
104 | n /= base; |
114 | n /= base; |
105 | } while (n); |
115 | } while (n); |
106 | |
116 | |
107 | *p-- = 0; |
117 | *p-- = 0; |
108 | |
118 | |
… | |
… | |
111 | char x = *s; *s = *p; *p = x; |
121 | char x = *s; *s = *p; *p = x; |
112 | --p; ++s; |
122 | --p; ++s; |
113 | } |
123 | } |
114 | } |
124 | } |
115 | |
125 | |
116 | static void |
126 | ecb_cold static void |
117 | xnum (char *s, long n) |
127 | xnum (char *s, long n) |
118 | { |
128 | { |
119 | xbase (s, n, 10); |
129 | xbase (s, n, 10); |
120 | } |
130 | } |
121 | |
131 | |
122 | static void |
132 | ecb_cold static void |
123 | xwrstr (const char *s) |
133 | putnum (SCHEME_P_ long n) |
124 | { |
|
|
125 | write (1, s, strlen (s)); |
|
|
126 | } |
|
|
127 | |
|
|
128 | static void |
|
|
129 | xwrnum (long n) |
|
|
130 | { |
134 | { |
131 | char buf[64]; |
135 | char buf[64]; |
132 | |
136 | |
133 | xnum (buf, n); |
137 | xnum (buf, n); |
134 | xwrstr (buf); |
138 | putstr (SCHEME_A_ buf); |
135 | } |
139 | } |
|
|
140 | |
|
|
141 | #if USE_CHAR_CLASSIFIERS |
|
|
142 | #include <ctype.h> |
|
|
143 | #else |
136 | |
144 | |
137 | static char |
145 | static char |
138 | xtoupper (char c) |
146 | xtoupper (char c) |
139 | { |
147 | { |
140 | if (c >= 'a' && c <= 'z') |
148 | if (c >= 'a' && c <= 'z') |
… | |
… | |
160 | |
168 | |
161 | #define toupper(c) xtoupper (c) |
169 | #define toupper(c) xtoupper (c) |
162 | #define tolower(c) xtolower (c) |
170 | #define tolower(c) xtolower (c) |
163 | #define isdigit(c) xisdigit (c) |
171 | #define isdigit(c) xisdigit (c) |
164 | |
172 | |
|
|
173 | #endif |
|
|
174 | |
165 | #if USE_IGNORECASE |
175 | #if USE_IGNORECASE |
166 | static const char * |
176 | ecb_cold static const char * |
167 | xstrlwr (char *s) |
177 | xstrlwr (char *s) |
168 | { |
178 | { |
169 | const char *p = s; |
179 | const char *p = s; |
170 | |
180 | |
171 | while (*s) |
181 | while (*s) |
… | |
… | |
191 | |
201 | |
192 | #ifndef InitFile |
202 | #ifndef InitFile |
193 | # define InitFile "init.scm" |
203 | # define InitFile "init.scm" |
194 | #endif |
204 | #endif |
195 | |
205 | |
196 | #ifndef FIRST_CELLSEGS |
|
|
197 | # define FIRST_CELLSEGS 3 |
|
|
198 | #endif |
|
|
199 | |
|
|
200 | enum scheme_types |
206 | enum scheme_types |
201 | { |
207 | { |
202 | T_INTEGER, |
208 | T_INTEGER, |
|
|
209 | T_CHARACTER, |
203 | T_REAL, |
210 | T_REAL, |
204 | T_STRING, |
211 | T_STRING, |
205 | T_SYMBOL, |
212 | T_SYMBOL, |
206 | T_PROC, |
213 | T_PROC, |
207 | T_PAIR, /* also used for free cells */ |
214 | T_PAIR, /* also used for free cells */ |
208 | T_CLOSURE, |
215 | T_CLOSURE, |
|
|
216 | T_BYTECODE, // temp |
|
|
217 | T_MACRO, |
209 | T_CONTINUATION, |
218 | T_CONTINUATION, |
210 | T_FOREIGN, |
219 | T_FOREIGN, |
211 | T_CHARACTER, |
|
|
212 | T_PORT, |
220 | T_PORT, |
213 | T_VECTOR, |
221 | T_VECTOR, |
214 | T_MACRO, |
|
|
215 | T_PROMISE, |
222 | T_PROMISE, |
216 | T_ENVIRONMENT, |
223 | T_ENVIRONMENT, |
217 | /* one more... */ |
224 | |
218 | T_NUM_SYSTEM_TYPES |
225 | T_NUM_SYSTEM_TYPES |
219 | }; |
226 | }; |
220 | |
227 | |
221 | #define T_MASKTYPE 0x000f |
228 | #define T_MASKTYPE 0x000f |
222 | #define T_SYNTAX 0x0010 |
229 | #define T_SYNTAX 0x0010 |
… | |
… | |
255 | static num num_op (enum num_op op, num a, num b); |
262 | static num num_op (enum num_op op, num a, num b); |
256 | static num num_intdiv (num a, num b); |
263 | static num num_intdiv (num a, num b); |
257 | static num num_rem (num a, num b); |
264 | static num num_rem (num a, num b); |
258 | static num num_mod (num a, num b); |
265 | static num num_mod (num a, num b); |
259 | |
266 | |
260 | #if USE_MATH |
|
|
261 | static double round_per_R5RS (double x); |
|
|
262 | #endif |
|
|
263 | static int is_zero_rvalue (RVALUE x); |
267 | static int is_zero_rvalue (RVALUE x); |
264 | |
268 | |
265 | static num num_zero; |
269 | static num num_zero; |
266 | static num num_one; |
270 | static num num_one; |
267 | |
271 | |
|
|
272 | /* convert "pointer" to cell* / cell* to pointer */ |
|
|
273 | #define CELL(p) ((struct cell *)(p) + 0) |
|
|
274 | #define POINTER(c) ((void *)((c) - 0)) |
|
|
275 | |
268 | /* macros for cell operations */ |
276 | /* macros for cell operations */ |
269 | #define typeflag(p) ((p)->flag + 0) |
277 | #define typeflag(p) (CELL(p)->flag + 0) |
270 | #define set_typeflag(p,v) ((p)->flag = (v)) |
278 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
271 | #define type(p) (typeflag (p) & T_MASKTYPE) |
279 | #define type(p) (typeflag (p) & T_MASKTYPE) |
272 | |
280 | |
273 | INTERFACE int |
281 | INTERFACE int |
274 | is_string (pointer p) |
282 | is_string (pointer p) |
275 | { |
283 | { |
276 | return type (p) == T_STRING; |
284 | return type (p) == T_STRING; |
277 | } |
285 | } |
278 | |
286 | |
279 | #define strvalue(p) ((p)->object.string.svalue) |
287 | #define strvalue(p) (CELL(p)->object.string.svalue) |
280 | #define strlength(p) ((p)->object.string.length) |
288 | #define strlength(p) (CELL(p)->object.string.length) |
281 | |
289 | |
282 | INTERFACE int |
290 | INTERFACE int |
283 | is_vector (pointer p) |
291 | is_vector (pointer p) |
284 | { |
292 | { |
285 | return type (p) == T_VECTOR; |
293 | return type (p) == T_VECTOR; |
286 | } |
294 | } |
287 | |
295 | |
288 | #define vecvalue(p) ((p)->object.vector.vvalue) |
296 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
289 | #define veclength(p) ((p)->object.vector.length) |
297 | #define veclength(p) (CELL(p)->object.vector.length) |
290 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
298 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
291 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
299 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
292 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
300 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
293 | |
301 | |
294 | INTERFACE int |
302 | INTERFACE int |
… | |
… | |
320 | string_value (pointer p) |
328 | string_value (pointer p) |
321 | { |
329 | { |
322 | return strvalue (p); |
330 | return strvalue (p); |
323 | } |
331 | } |
324 | |
332 | |
325 | #define ivalue_unchecked(p) (p)->object.ivalue |
333 | #define ivalue_unchecked(p) CELL(p)->object.ivalue |
326 | #define set_ivalue(p,v) (p)->object.ivalue = (v) |
334 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
327 | |
335 | |
328 | #if USE_REAL |
336 | #if USE_REAL |
329 | #define rvalue_unchecked(p) (p)->object.rvalue |
337 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
330 | #define set_rvalue(p,v) (p)->object.rvalue = (v) |
338 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
331 | #else |
339 | #else |
332 | #define rvalue_unchecked(p) (p)->object.ivalue |
340 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
333 | #define set_rvalue(p,v) (p)->object.ivalue = (v) |
341 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
334 | #endif |
342 | #endif |
335 | |
343 | |
336 | INTERFACE long |
344 | INTERFACE long |
337 | charvalue (pointer p) |
345 | charvalue (pointer p) |
338 | { |
346 | { |
339 | return ivalue_unchecked (p); |
347 | return ivalue_unchecked (p); |
340 | } |
348 | } |
341 | |
349 | |
|
|
350 | #define port(p) CELL(p)->object.port |
|
|
351 | #define set_port(p,v) port(p) = (v) |
342 | INTERFACE int |
352 | INTERFACE int |
343 | is_port (pointer p) |
353 | is_port (pointer p) |
344 | { |
354 | { |
345 | return type (p) == T_PORT; |
355 | return type (p) == T_PORT; |
346 | } |
356 | } |
347 | |
357 | |
348 | INTERFACE int |
358 | INTERFACE int |
349 | is_inport (pointer p) |
359 | is_inport (pointer p) |
350 | { |
360 | { |
351 | return is_port (p) && p->object.port->kind & port_input; |
361 | return is_port (p) && port (p)->kind & port_input; |
352 | } |
362 | } |
353 | |
363 | |
354 | INTERFACE int |
364 | INTERFACE int |
355 | is_outport (pointer p) |
365 | is_outport (pointer p) |
356 | { |
366 | { |
357 | return is_port (p) && p->object.port->kind & port_output; |
367 | return is_port (p) && port (p)->kind & port_output; |
358 | } |
368 | } |
359 | |
369 | |
360 | INTERFACE int |
370 | INTERFACE int |
361 | is_pair (pointer p) |
371 | is_pair (pointer p) |
362 | { |
372 | { |
363 | return type (p) == T_PAIR; |
373 | return type (p) == T_PAIR; |
364 | } |
374 | } |
365 | |
375 | |
366 | #define car(p) ((p)->object.cons.car + 0) |
376 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
367 | #define cdr(p) ((p)->object.cons.cdr + 0) |
377 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
368 | |
378 | |
369 | static pointer caar (pointer p) { return car (car (p)); } |
379 | static pointer caar (pointer p) { return car (car (p)); } |
370 | static pointer cadr (pointer p) { return car (cdr (p)); } |
380 | static pointer cadr (pointer p) { return car (cdr (p)); } |
371 | static pointer cdar (pointer p) { return cdr (car (p)); } |
381 | static pointer cdar (pointer p) { return cdr (car (p)); } |
372 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
382 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
… | |
… | |
376 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
386 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
377 | |
387 | |
378 | INTERFACE void |
388 | INTERFACE void |
379 | set_car (pointer p, pointer q) |
389 | set_car (pointer p, pointer q) |
380 | { |
390 | { |
381 | p->object.cons.car = q; |
391 | CELL(p)->object.cons.car = CELL (q); |
382 | } |
392 | } |
383 | |
393 | |
384 | INTERFACE void |
394 | INTERFACE void |
385 | set_cdr (pointer p, pointer q) |
395 | set_cdr (pointer p, pointer q) |
386 | { |
396 | { |
387 | p->object.cons.cdr = q; |
397 | CELL(p)->object.cons.cdr = CELL (q); |
388 | } |
398 | } |
389 | |
399 | |
390 | INTERFACE pointer |
400 | INTERFACE pointer |
391 | pair_car (pointer p) |
401 | pair_car (pointer p) |
392 | { |
402 | { |
… | |
… | |
406 | } |
416 | } |
407 | |
417 | |
408 | INTERFACE char * |
418 | INTERFACE char * |
409 | symname (pointer p) |
419 | symname (pointer p) |
410 | { |
420 | { |
411 | return strvalue (car (p)); |
421 | return strvalue (p); |
412 | } |
422 | } |
413 | |
423 | |
414 | #if USE_PLIST |
424 | #if USE_PLIST |
|
|
425 | #error plists are broken because symbols are no longer pairs |
|
|
426 | #define symprop(p) cdr(p) |
415 | SCHEME_EXPORT int |
427 | SCHEME_EXPORT int |
416 | hasprop (pointer p) |
428 | hasprop (pointer p) |
417 | { |
429 | { |
418 | return typeflag (p) & T_SYMBOL; |
430 | return typeflag (p) & T_SYMBOL; |
419 | } |
431 | } |
420 | |
|
|
421 | # define symprop(p) cdr(p) |
|
|
422 | #endif |
432 | #endif |
423 | |
433 | |
424 | INTERFACE int |
434 | INTERFACE int |
425 | is_syntax (pointer p) |
435 | is_syntax (pointer p) |
426 | { |
436 | { |
… | |
… | |
440 | } |
450 | } |
441 | |
451 | |
442 | INTERFACE char * |
452 | INTERFACE char * |
443 | syntaxname (pointer p) |
453 | syntaxname (pointer p) |
444 | { |
454 | { |
445 | return strvalue (car (p)); |
455 | return strvalue (p); |
446 | } |
456 | } |
447 | |
457 | |
448 | #define procnum(p) ivalue_unchecked (p) |
458 | #define procnum(p) ivalue_unchecked (p) |
449 | static const char *procname (pointer x); |
459 | static const char *procname (pointer x); |
450 | |
460 | |
… | |
… | |
522 | proper list: length |
532 | proper list: length |
523 | circular list: -1 |
533 | circular list: -1 |
524 | not even a pair: -2 |
534 | not even a pair: -2 |
525 | dotted list: -2 minus length before dot |
535 | dotted list: -2 minus length before dot |
526 | */ |
536 | */ |
527 | INTERFACE int |
537 | ecb_hot INTERFACE int |
528 | list_length (SCHEME_P_ pointer a) |
538 | list_length (SCHEME_P_ pointer a) |
529 | { |
539 | { |
530 | int i = 0; |
540 | int i = 0; |
531 | pointer slow, fast; |
541 | pointer slow, fast; |
532 | |
542 | |
… | |
… | |
571 | { |
581 | { |
572 | return list_length (SCHEME_A_ a) >= 0; |
582 | return list_length (SCHEME_A_ a) >= 0; |
573 | } |
583 | } |
574 | |
584 | |
575 | #if USE_CHAR_CLASSIFIERS |
585 | #if USE_CHAR_CLASSIFIERS |
|
|
586 | |
576 | ecb_inline int |
587 | ecb_inline int |
577 | Cisalpha (int c) |
588 | Cisalpha (int c) |
578 | { |
589 | { |
579 | return isascii (c) && isalpha (c); |
590 | return isascii (c) && isalpha (c); |
580 | } |
591 | } |
… | |
… | |
638 | "gs", |
649 | "gs", |
639 | "rs", |
650 | "rs", |
640 | "us" |
651 | "us" |
641 | }; |
652 | }; |
642 | |
653 | |
643 | static int |
654 | ecb_cold static int |
644 | is_ascii_name (const char *name, int *pc) |
655 | is_ascii_name (const char *name, int *pc) |
645 | { |
656 | { |
646 | int i; |
657 | int i; |
647 | |
658 | |
648 | for (i = 0; i < 32; i++) |
659 | for (i = 0; i < 32; i++) |
… | |
… | |
667 | |
678 | |
668 | static int file_push (SCHEME_P_ const char *fname); |
679 | static int file_push (SCHEME_P_ const char *fname); |
669 | static void file_pop (SCHEME_P); |
680 | static void file_pop (SCHEME_P); |
670 | static int file_interactive (SCHEME_P); |
681 | static int file_interactive (SCHEME_P); |
671 | ecb_inline int is_one_of (const char *s, int c); |
682 | ecb_inline int is_one_of (const char *s, int c); |
672 | static int alloc_cellseg (SCHEME_P_ int n); |
683 | static int alloc_cellseg (SCHEME_P); |
673 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
684 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
674 | static void finalize_cell (SCHEME_P_ pointer a); |
685 | static void finalize_cell (SCHEME_P_ pointer a); |
675 | static int count_consecutive_cells (pointer x, int needed); |
|
|
676 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
686 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
677 | static pointer mk_number (SCHEME_P_ const num n); |
687 | static pointer mk_number (SCHEME_P_ const num n); |
678 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
688 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
679 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
689 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
680 | static pointer mk_atom (SCHEME_P_ char *q); |
690 | static pointer mk_atom (SCHEME_P_ char *q); |
681 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
691 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
682 | |
692 | |
|
|
693 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
694 | |
683 | #if USE_PORTS |
695 | #if USE_PORTS |
684 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
685 | static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); |
696 | static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); |
686 | static pointer port_from_file (SCHEME_P_ int, int prop); |
697 | static pointer port_from_file (SCHEME_P_ int, int prop); |
687 | static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
698 | static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
688 | static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); |
699 | static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); |
689 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
700 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
690 | static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
701 | static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
691 | static void port_close (SCHEME_P_ pointer p, int flag); |
702 | static void port_close (SCHEME_P_ pointer p, int flag); |
692 | #endif |
703 | #endif |
|
|
704 | |
693 | static void mark (pointer a); |
705 | static void mark (pointer a); |
694 | static void gc (SCHEME_P_ pointer a, pointer b); |
706 | static void gc (SCHEME_P_ pointer a, pointer b); |
695 | static int basic_inchar (port *pt); |
707 | static int basic_inchar (port *pt); |
696 | static int inchar (SCHEME_P); |
708 | static int inchar (SCHEME_P); |
697 | static void backchar (SCHEME_P_ int c); |
709 | static void backchar (SCHEME_P_ int c); |
… | |
… | |
871 | } |
883 | } |
872 | |
884 | |
873 | return ret; |
885 | return ret; |
874 | } |
886 | } |
875 | |
887 | |
876 | #if USE_MATH |
|
|
877 | |
|
|
878 | /* Round to nearest. Round to even if midway */ |
|
|
879 | static double |
|
|
880 | round_per_R5RS (double x) |
|
|
881 | { |
|
|
882 | double fl = floor (x); |
|
|
883 | double ce = ceil (x); |
|
|
884 | double dfl = x - fl; |
|
|
885 | double dce = ce - x; |
|
|
886 | |
|
|
887 | if (dfl > dce) |
|
|
888 | return ce; |
|
|
889 | else if (dfl < dce) |
|
|
890 | return fl; |
|
|
891 | else |
|
|
892 | { |
|
|
893 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
|
|
894 | return fl; |
|
|
895 | else |
|
|
896 | return ce; |
|
|
897 | } |
|
|
898 | } |
|
|
899 | #endif |
|
|
900 | |
|
|
901 | static int |
888 | static int |
902 | is_zero_rvalue (RVALUE x) |
889 | is_zero_rvalue (RVALUE x) |
903 | { |
890 | { |
904 | return x == 0; |
891 | return x == 0; |
905 | #if 0 |
892 | #if 0 |
… | |
… | |
910 | #endif |
897 | #endif |
911 | #endif |
898 | #endif |
912 | } |
899 | } |
913 | |
900 | |
914 | /* allocate new cell segment */ |
901 | /* allocate new cell segment */ |
915 | static int |
902 | ecb_cold static int |
916 | alloc_cellseg (SCHEME_P_ int n) |
903 | alloc_cellseg (SCHEME_P) |
917 | { |
904 | { |
918 | pointer newp; |
905 | struct cell *newp; |
919 | pointer last; |
906 | struct cell *last; |
920 | pointer p; |
907 | struct cell *p; |
921 | char *cp; |
908 | char *cp; |
922 | long i; |
909 | long i; |
923 | int k; |
910 | int k; |
924 | |
911 | |
925 | static int segsize = CELL_SEGSIZE >> 1; |
912 | static int segsize = CELL_SEGSIZE >> 1; |
926 | segsize <<= 1; |
913 | segsize <<= 1; |
927 | |
914 | |
928 | for (k = 0; k < n; k++) |
|
|
929 | { |
|
|
930 | if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1) |
|
|
931 | return k; |
|
|
932 | |
|
|
933 | cp = malloc (segsize * sizeof (struct cell)); |
915 | cp = malloc (segsize * sizeof (struct cell)); |
934 | |
916 | |
935 | if (!cp && USE_ERROR_CHECKING) |
917 | if (!cp && USE_ERROR_CHECKING) |
936 | return k; |
918 | return k; |
937 | |
919 | |
938 | i = ++SCHEME_V->last_cell_seg; |
920 | i = ++SCHEME_V->last_cell_seg; |
939 | SCHEME_V->alloc_seg[i] = cp; |
921 | SCHEME_V->alloc_seg[i] = cp; |
940 | |
922 | |
941 | newp = (pointer)cp; |
923 | newp = (struct cell *)cp; |
942 | SCHEME_V->cell_seg[i] = newp; |
924 | SCHEME_V->cell_seg[i] = newp; |
943 | SCHEME_V->cell_segsize[i] = segsize; |
925 | SCHEME_V->cell_segsize[i] = segsize; |
944 | SCHEME_V->fcells += segsize; |
926 | SCHEME_V->fcells += segsize; |
945 | last = newp + segsize - 1; |
927 | last = newp + segsize - 1; |
946 | |
928 | |
947 | for (p = newp; p <= last; p++) |
929 | for (p = newp; p <= last; p++) |
948 | { |
930 | { |
|
|
931 | pointer cp = POINTER (p); |
949 | set_typeflag (p, T_PAIR); |
932 | set_typeflag (cp, T_PAIR); |
950 | set_car (p, NIL); |
933 | set_car (cp, NIL); |
951 | set_cdr (p, p + 1); |
934 | set_cdr (cp, POINTER (p + 1)); |
952 | } |
935 | } |
953 | |
936 | |
954 | set_cdr (last, SCHEME_V->free_cell); |
937 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
955 | SCHEME_V->free_cell = newp; |
938 | SCHEME_V->free_cell = POINTER (newp); |
956 | } |
|
|
957 | |
939 | |
958 | return n; |
940 | return 1; |
959 | } |
941 | } |
960 | |
942 | |
961 | /* get new cell. parameter a, b is marked by gc. */ |
943 | /* get new cell. parameter a, b is marked by gc. */ |
962 | ecb_inline pointer |
944 | ecb_inline pointer |
963 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
945 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
… | |
… | |
967 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
949 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
968 | return S_SINK; |
950 | return S_SINK; |
969 | |
951 | |
970 | if (SCHEME_V->free_cell == NIL) |
952 | if (SCHEME_V->free_cell == NIL) |
971 | { |
953 | { |
972 | 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; |
973 | |
955 | |
974 | gc (SCHEME_A_ a, b); |
956 | gc (SCHEME_A_ a, b); |
975 | |
957 | |
976 | 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) |
977 | { |
959 | { |
978 | /* 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 */ |
979 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
961 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
980 | { |
962 | { |
981 | #if USE_ERROR_CHECKING |
963 | #if USE_ERROR_CHECKING |
982 | SCHEME_V->no_memory = 1; |
964 | SCHEME_V->no_memory = 1; |
983 | return S_SINK; |
965 | return S_SINK; |
984 | #endif |
966 | #endif |
… | |
… | |
996 | } |
978 | } |
997 | } |
979 | } |
998 | |
980 | |
999 | /* To retain recent allocs before interpreter knows about them - |
981 | /* To retain recent allocs before interpreter knows about them - |
1000 | Tehom */ |
982 | Tehom */ |
1001 | |
983 | ecb_hot static void |
1002 | static void |
|
|
1003 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
984 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
1004 | { |
985 | { |
1005 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
986 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1006 | |
987 | |
1007 | set_typeflag (holder, T_PAIR); |
988 | set_typeflag (holder, T_PAIR); |
… | |
… | |
1009 | set_car (holder, recent); |
990 | set_car (holder, recent); |
1010 | set_cdr (holder, car (S_SINK)); |
991 | set_cdr (holder, car (S_SINK)); |
1011 | set_car (S_SINK, holder); |
992 | set_car (S_SINK, holder); |
1012 | } |
993 | } |
1013 | |
994 | |
1014 | static pointer |
995 | ecb_hot static pointer |
1015 | get_cell (SCHEME_P_ pointer a, pointer b) |
996 | get_cell (SCHEME_P_ pointer a, pointer b) |
1016 | { |
997 | { |
1017 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
998 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
1018 | |
999 | |
1019 | /* 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 |
… | |
… | |
1028 | } |
1009 | } |
1029 | |
1010 | |
1030 | static pointer |
1011 | static pointer |
1031 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1012 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1032 | { |
1013 | { |
1033 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1014 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
1034 | pointer *e = malloc (len * sizeof (pointer)); |
1015 | pointer *e = malloc (len * sizeof (pointer)); |
1035 | |
1016 | |
1036 | if (!e && USE_ERROR_CHECKING) |
1017 | if (!e && USE_ERROR_CHECKING) |
1037 | return S_SINK; |
1018 | return S_SINK; |
1038 | |
1019 | |
1039 | /* Record it as a vector so that gc understands it. */ |
1020 | /* Record it as a vector so that gc understands it. */ |
1040 | set_typeflag (v, T_VECTOR | T_ATOM); |
1021 | set_typeflag (v, T_VECTOR | T_ATOM); |
1041 | |
1022 | |
1042 | v->object.vector.vvalue = e; |
1023 | CELL(v)->object.vector.vvalue = e; |
1043 | v->object.vector.length = len; |
1024 | CELL(v)->object.vector.length = len; |
1044 | fill_vector (v, 0, init); |
1025 | fill_vector (v, 0, init); |
1045 | push_recent_alloc (SCHEME_A_ v, NIL); |
1026 | push_recent_alloc (SCHEME_A_ v, NIL); |
1046 | |
1027 | |
1047 | return v; |
1028 | return v; |
1048 | } |
1029 | } |
… | |
… | |
1057 | static void |
1038 | static void |
1058 | check_cell_alloced (pointer p, int expect_alloced) |
1039 | check_cell_alloced (pointer p, int expect_alloced) |
1059 | { |
1040 | { |
1060 | /* 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. */ |
1061 | if (typeflag (p) & !expect_alloced) |
1042 | if (typeflag (p) & !expect_alloced) |
1062 | xwrstr ("Cell is already allocated!\n"); |
1043 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1063 | |
1044 | |
1064 | if (!(typeflag (p)) & expect_alloced) |
1045 | if (!(typeflag (p)) & expect_alloced) |
1065 | xwrstr ("Cell is not allocated!\n"); |
1046 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1066 | } |
1047 | } |
1067 | |
1048 | |
1068 | static void |
1049 | static void |
1069 | check_range_alloced (pointer p, int n, int expect_alloced) |
1050 | check_range_alloced (pointer p, int n, int expect_alloced) |
1070 | { |
1051 | { |
… | |
… | |
1076 | #endif |
1057 | #endif |
1077 | |
1058 | |
1078 | /* Medium level cell allocation */ |
1059 | /* Medium level cell allocation */ |
1079 | |
1060 | |
1080 | /* get new cons cell */ |
1061 | /* get new cons cell */ |
1081 | pointer |
1062 | ecb_hot static pointer |
1082 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1063 | xcons (SCHEME_P_ pointer a, pointer b) |
1083 | { |
1064 | { |
1084 | pointer x = get_cell (SCHEME_A_ a, b); |
1065 | pointer x = get_cell (SCHEME_A_ a, b); |
1085 | |
1066 | |
1086 | set_typeflag (x, T_PAIR); |
1067 | set_typeflag (x, T_PAIR); |
1087 | |
|
|
1088 | if (immutable) |
|
|
1089 | setimmutable (x); |
|
|
1090 | |
1068 | |
1091 | set_car (x, a); |
1069 | set_car (x, a); |
1092 | set_cdr (x, b); |
1070 | set_cdr (x, b); |
1093 | |
1071 | |
1094 | return x; |
1072 | return x; |
1095 | } |
1073 | } |
1096 | |
1074 | |
1097 | /* ========== oblist implementation ========== */ |
|
|
1098 | |
|
|
1099 | 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 |
1100 | generate_symbol (SCHEME_P_ const char *name) |
1087 | generate_symbol (SCHEME_P_ const char *name) |
1101 | { |
1088 | { |
1102 | pointer x = mk_string (SCHEME_A_ name); |
1089 | pointer x = mk_string (SCHEME_A_ name); |
1103 | setimmutable (x); |
1090 | setimmutable (x); |
1104 | x = immutable_cons (x, NIL); |
|
|
1105 | set_typeflag (x, T_SYMBOL); |
1091 | set_typeflag (x, T_SYMBOL | T_ATOM); |
1106 | return x; |
1092 | return x; |
1107 | } |
1093 | } |
|
|
1094 | |
|
|
1095 | /* ========== oblist implementation ========== */ |
1108 | |
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 = 2166136261; |
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); |
… | |
… | |
1354 | |
1352 | |
1355 | for (i = start; i < veclength (vec); i++) |
1353 | for (i = start; i < veclength (vec); i++) |
1356 | vecvalue (vec)[i] = obj; |
1354 | vecvalue (vec)[i] = obj; |
1357 | } |
1355 | } |
1358 | |
1356 | |
|
|
1357 | INTERFACE void |
|
|
1358 | vector_resize (pointer vec, uint32_t newsize, pointer fill) |
|
|
1359 | { |
|
|
1360 | uint32_t oldsize = veclength (vec); |
|
|
1361 | vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer)); |
|
|
1362 | veclength (vec) = newsize; |
|
|
1363 | fill_vector (vec, oldsize, fill); |
|
|
1364 | } |
|
|
1365 | |
1359 | INTERFACE pointer |
1366 | INTERFACE pointer |
1360 | vector_get (pointer vec, uint32_t ielem) |
1367 | vector_get (pointer vec, uint32_t ielem) |
1361 | { |
1368 | { |
1362 | return vecvalue(vec)[ielem]; |
1369 | return vecvalue(vec)[ielem]; |
1363 | } |
1370 | } |
… | |
… | |
1379 | x = oblist_add_by_name (SCHEME_A_ name); |
1386 | x = oblist_add_by_name (SCHEME_A_ name); |
1380 | |
1387 | |
1381 | return x; |
1388 | return x; |
1382 | } |
1389 | } |
1383 | |
1390 | |
1384 | INTERFACE pointer |
1391 | ecb_cold INTERFACE pointer |
1385 | gensym (SCHEME_P) |
1392 | gensym (SCHEME_P) |
1386 | { |
1393 | { |
1387 | pointer x; |
1394 | pointer x; |
1388 | char name[40] = "gensym-"; |
1395 | char name[40] = "gensym-"; |
1389 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1396 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1390 | |
1397 | |
1391 | return generate_symbol (SCHEME_A_ name); |
1398 | return generate_symbol (SCHEME_A_ name); |
1392 | } |
1399 | } |
1393 | |
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 | |
1394 | /* make symbol or number atom from string */ |
1407 | /* make symbol or number atom from string */ |
1395 | static pointer |
1408 | ecb_cold static pointer |
1396 | mk_atom (SCHEME_P_ char *q) |
1409 | mk_atom (SCHEME_P_ char *q) |
1397 | { |
1410 | { |
1398 | char c, *p; |
1411 | char c, *p; |
1399 | int has_dec_point = 0; |
1412 | int has_dec_point = 0; |
1400 | int has_fp_exp = 0; |
1413 | int has_fp_exp = 0; |
… | |
… | |
1471 | |
1484 | |
1472 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1485 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1473 | } |
1486 | } |
1474 | |
1487 | |
1475 | /* make constant */ |
1488 | /* make constant */ |
1476 | static pointer |
1489 | ecb_cold static pointer |
1477 | mk_sharp_const (SCHEME_P_ char *name) |
1490 | mk_sharp_const (SCHEME_P_ char *name) |
1478 | { |
1491 | { |
1479 | if (!strcmp (name, "t")) |
1492 | if (!strcmp (name, "t")) |
1480 | return S_T; |
1493 | return S_T; |
1481 | else if (!strcmp (name, "f")) |
1494 | else if (!strcmp (name, "f")) |
1482 | return S_F; |
1495 | return S_F; |
1483 | else if (*name == '\\') /* #\w (character) */ |
1496 | else if (*name == '\\') /* #\w (character) */ |
1484 | { |
1497 | { |
1485 | int c; |
1498 | int c; |
1486 | |
1499 | |
|
|
1500 | // TODO: optimise |
1487 | if (stricmp (name + 1, "space") == 0) |
1501 | if (stricmp (name + 1, "space") == 0) |
1488 | c = ' '; |
1502 | c = ' '; |
1489 | else if (stricmp (name + 1, "newline") == 0) |
1503 | else if (stricmp (name + 1, "newline") == 0) |
1490 | c = '\n'; |
1504 | c = '\n'; |
1491 | else if (stricmp (name + 1, "return") == 0) |
1505 | else if (stricmp (name + 1, "return") == 0) |
1492 | c = '\r'; |
1506 | c = '\r'; |
1493 | else if (stricmp (name + 1, "tab") == 0) |
1507 | else if (stricmp (name + 1, "tab") == 0) |
1494 | 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; |
1495 | else if (name[1] == 'x' && name[2] != 0) |
1519 | else if (name[1] == 'x' && name[2] != 0) |
1496 | { |
1520 | { |
1497 | long c1 = strtol (name + 2, 0, 16); |
1521 | long c1 = strtol (name + 2, 0, 16); |
1498 | |
1522 | |
1499 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1523 | if (0 <= c1 && c1 <= UCHAR_MAX) |
… | |
… | |
1524 | return NIL; |
1548 | return NIL; |
1525 | } |
1549 | } |
1526 | } |
1550 | } |
1527 | |
1551 | |
1528 | /* ========== 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 | } |
1529 | |
1572 | |
1530 | /*-- |
1573 | /*-- |
1531 | * 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, |
1532 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1575 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1533 | * for marking. |
1576 | * for marking. |
1534 | * |
1577 | * |
1535 | * The exception is vectors - vectors are currently marked recursively, |
1578 | * The exception is vectors - vectors are currently marked recursively, |
1536 | * 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 |
1537 | * word of context in the vector |
1580 | * word of context in the vector |
1538 | */ |
1581 | */ |
1539 | static void |
1582 | ecb_hot static void |
1540 | mark (pointer a) |
1583 | mark (pointer a) |
1541 | { |
1584 | { |
1542 | pointer t, q, p; |
1585 | pointer t, q, p; |
1543 | |
1586 | |
1544 | t = 0; |
1587 | t = 0; |
… | |
… | |
1601 | p = q; |
1644 | p = q; |
1602 | goto E6; |
1645 | goto E6; |
1603 | } |
1646 | } |
1604 | } |
1647 | } |
1605 | |
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 | |
1606 | /* garbage collection. parameter a, b is marked. */ |
1691 | /* garbage collection. parameter a, b is marked. */ |
1607 | static void |
1692 | ecb_cold static void |
1608 | gc (SCHEME_P_ pointer a, pointer b) |
1693 | gc (SCHEME_P_ pointer a, pointer b) |
1609 | { |
1694 | { |
1610 | pointer p; |
|
|
1611 | int i; |
1695 | int i; |
1612 | |
1696 | |
1613 | if (SCHEME_V->gc_verbose) |
1697 | if (SCHEME_V->gc_verbose) |
1614 | putstr (SCHEME_A_ "gc..."); |
1698 | putstr (SCHEME_A_ "gc..."); |
1615 | |
1699 | |
… | |
… | |
1631 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1715 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1632 | mark (car (S_SINK)); |
1716 | mark (car (S_SINK)); |
1633 | /* Mark any older stuff above nested C calls */ |
1717 | /* Mark any older stuff above nested C calls */ |
1634 | mark (SCHEME_V->c_nest); |
1718 | mark (SCHEME_V->c_nest); |
1635 | |
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 | |
1636 | /* mark variables a, b */ |
1727 | /* mark variables a, b */ |
1637 | mark (a); |
1728 | mark (a); |
1638 | mark (b); |
1729 | mark (b); |
1639 | |
1730 | |
1640 | /* garbage collect */ |
1731 | /* garbage collect */ |
1641 | clrmark (NIL); |
1732 | clrmark (NIL); |
1642 | SCHEME_V->fcells = 0; |
1733 | SCHEME_V->fcells = 0; |
1643 | SCHEME_V->free_cell = NIL; |
1734 | SCHEME_V->free_cell = NIL; |
1644 | |
1735 | |
1645 | /* free-list is kept sorted by address so as to maintain consecutive |
|
|
1646 | ranges, if possible, for use with vectors. Here we scan the cells |
|
|
1647 | (which are also kept sorted by address) downwards to build the |
|
|
1648 | free-list in sorted order. |
|
|
1649 | */ |
|
|
1650 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1651 | { |
|
|
1652 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1653 | |
|
|
1654 | while (--p >= SCHEME_V->cell_seg[i]) |
|
|
1655 | { |
|
|
1656 | if (is_mark (p)) |
|
|
1657 | clrmark (p); |
|
|
1658 | else |
|
|
1659 | { |
|
|
1660 | /* reclaim cell */ |
|
|
1661 | if (typeflag (p) != T_PAIR) |
|
|
1662 | { |
|
|
1663 | finalize_cell (SCHEME_A_ p); |
|
|
1664 | set_typeflag (p, T_PAIR); |
|
|
1665 | set_car (p, NIL); |
|
|
1666 | } |
|
|
1667 | |
|
|
1668 | ++SCHEME_V->fcells; |
|
|
1669 | set_cdr (p, SCHEME_V->free_cell); |
|
|
1670 | SCHEME_V->free_cell = p; |
|
|
1671 | } |
|
|
1672 | } |
|
|
1673 | } |
|
|
1674 | |
|
|
1675 | if (SCHEME_V->gc_verbose) |
1736 | if (SCHEME_V->gc_verbose) |
1676 | { |
1737 | putstr (SCHEME_A_ "freeing..."); |
1677 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
|
|
1678 | } |
|
|
1679 | } |
|
|
1680 | |
1738 | |
1681 | static void |
1739 | gc_free (SCHEME_A); |
1682 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1683 | { |
|
|
1684 | /* TODO, fast bitmap check? */ |
|
|
1685 | if (is_string (a)) |
|
|
1686 | free (strvalue (a)); |
|
|
1687 | else if (is_vector (a)) |
|
|
1688 | free (vecvalue (a)); |
|
|
1689 | #if USE_PORTS |
|
|
1690 | else if (is_port (a)) |
|
|
1691 | { |
|
|
1692 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
|
|
1693 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1694 | |
|
|
1695 | free (a->object.port); |
|
|
1696 | } |
|
|
1697 | #endif |
|
|
1698 | } |
1740 | } |
1699 | |
1741 | |
1700 | /* ========== Routines for Reading ========== */ |
1742 | /* ========== Routines for Reading ========== */ |
1701 | |
1743 | |
1702 | static int |
1744 | ecb_cold static int |
1703 | file_push (SCHEME_P_ const char *fname) |
1745 | file_push (SCHEME_P_ const char *fname) |
1704 | { |
1746 | { |
1705 | #if USE_PORTS |
|
|
1706 | int fin; |
1747 | int fin; |
1707 | |
1748 | |
1708 | if (SCHEME_V->file_i == MAXFIL - 1) |
1749 | if (SCHEME_V->file_i == MAXFIL - 1) |
1709 | return 0; |
1750 | return 0; |
1710 | |
1751 | |
… | |
… | |
1716 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1757 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1717 | 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; |
1718 | 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; |
1719 | 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; |
1720 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1761 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1721 | 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); |
1722 | |
1763 | |
1723 | #if SHOW_ERROR_LINE |
1764 | #if SHOW_ERROR_LINE |
1724 | 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; |
1725 | |
1766 | |
1726 | if (fname) |
1767 | if (fname) |
1727 | 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); |
1728 | #endif |
1769 | #endif |
1729 | } |
1770 | } |
1730 | |
1771 | |
1731 | return fin >= 0; |
1772 | return fin >= 0; |
1732 | |
|
|
1733 | #else |
|
|
1734 | return 1; |
|
|
1735 | #endif |
|
|
1736 | } |
1773 | } |
1737 | |
1774 | |
1738 | static void |
1775 | ecb_cold static void |
1739 | file_pop (SCHEME_P) |
1776 | file_pop (SCHEME_P) |
1740 | { |
1777 | { |
1741 | if (SCHEME_V->file_i != 0) |
1778 | if (SCHEME_V->file_i != 0) |
1742 | { |
1779 | { |
1743 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1780 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1744 | #if USE_PORTS |
1781 | #if USE_PORTS |
1745 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1782 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1746 | #endif |
1783 | #endif |
1747 | SCHEME_V->file_i--; |
1784 | SCHEME_V->file_i--; |
1748 | 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); |
1749 | } |
1786 | } |
1750 | } |
1787 | } |
1751 | |
1788 | |
1752 | static int |
1789 | ecb_cold static int |
1753 | file_interactive (SCHEME_P) |
1790 | file_interactive (SCHEME_P) |
1754 | { |
1791 | { |
1755 | #if USE_PORTS |
1792 | #if USE_PORTS |
1756 | return SCHEME_V->file_i == 0 |
1793 | return SCHEME_V->file_i == 0 |
1757 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1794 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1758 | && (SCHEME_V->inport->object.port->kind & port_file); |
1795 | && (port (SCHEME_V->inport)->kind & port_file); |
1759 | #else |
1796 | #else |
1760 | return 0; |
1797 | return 0; |
1761 | #endif |
1798 | #endif |
1762 | } |
1799 | } |
1763 | |
1800 | |
1764 | #if USE_PORTS |
1801 | #if USE_PORTS |
1765 | static port * |
1802 | ecb_cold static port * |
1766 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1803 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1767 | { |
1804 | { |
1768 | int fd; |
1805 | int fd; |
1769 | int flags; |
1806 | int flags; |
1770 | char *rw; |
1807 | char *rw; |
… | |
… | |
1793 | # endif |
1830 | # endif |
1794 | |
1831 | |
1795 | return pt; |
1832 | return pt; |
1796 | } |
1833 | } |
1797 | |
1834 | |
1798 | static pointer |
1835 | ecb_cold static pointer |
1799 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1836 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1800 | { |
1837 | { |
1801 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1838 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1802 | |
1839 | |
1803 | if (!pt && USE_ERROR_CHECKING) |
1840 | if (!pt && USE_ERROR_CHECKING) |
1804 | return NIL; |
1841 | return NIL; |
1805 | |
1842 | |
1806 | return mk_port (SCHEME_A_ pt); |
1843 | return mk_port (SCHEME_A_ pt); |
1807 | } |
1844 | } |
1808 | |
1845 | |
1809 | static port * |
1846 | ecb_cold static port * |
1810 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1847 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1811 | { |
1848 | { |
1812 | port *pt = malloc (sizeof *pt); |
1849 | port *pt = malloc (sizeof *pt); |
1813 | |
1850 | |
1814 | if (!pt && USE_ERROR_CHECKING) |
1851 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1819 | pt->rep.stdio.file = f; |
1856 | pt->rep.stdio.file = f; |
1820 | pt->rep.stdio.closeit = 0; |
1857 | pt->rep.stdio.closeit = 0; |
1821 | return pt; |
1858 | return pt; |
1822 | } |
1859 | } |
1823 | |
1860 | |
1824 | static pointer |
1861 | ecb_cold static pointer |
1825 | port_from_file (SCHEME_P_ int f, int prop) |
1862 | port_from_file (SCHEME_P_ int f, int prop) |
1826 | { |
1863 | { |
1827 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1864 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1828 | |
1865 | |
1829 | if (!pt && USE_ERROR_CHECKING) |
1866 | if (!pt && USE_ERROR_CHECKING) |
1830 | return NIL; |
1867 | return NIL; |
1831 | |
1868 | |
1832 | return mk_port (SCHEME_A_ pt); |
1869 | return mk_port (SCHEME_A_ pt); |
1833 | } |
1870 | } |
1834 | |
1871 | |
1835 | static port * |
1872 | ecb_cold static port * |
1836 | 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) |
1837 | { |
1874 | { |
1838 | port *pt = malloc (sizeof (port)); |
1875 | port *pt = malloc (sizeof (port)); |
1839 | |
1876 | |
1840 | if (!pt && USE_ERROR_CHECKING) |
1877 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1846 | pt->rep.string.curr = start; |
1883 | pt->rep.string.curr = start; |
1847 | pt->rep.string.past_the_end = past_the_end; |
1884 | pt->rep.string.past_the_end = past_the_end; |
1848 | return pt; |
1885 | return pt; |
1849 | } |
1886 | } |
1850 | |
1887 | |
1851 | static pointer |
1888 | ecb_cold static pointer |
1852 | 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) |
1853 | { |
1890 | { |
1854 | 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); |
1855 | |
1892 | |
1856 | if (!pt && USE_ERROR_CHECKING) |
1893 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1859 | return mk_port (SCHEME_A_ pt); |
1896 | return mk_port (SCHEME_A_ pt); |
1860 | } |
1897 | } |
1861 | |
1898 | |
1862 | # define BLOCK_SIZE 256 |
1899 | # define BLOCK_SIZE 256 |
1863 | |
1900 | |
1864 | static port * |
1901 | ecb_cold static port * |
1865 | port_rep_from_scratch (SCHEME_P) |
1902 | port_rep_from_scratch (SCHEME_P) |
1866 | { |
1903 | { |
1867 | char *start; |
1904 | char *start; |
1868 | port *pt = malloc (sizeof (port)); |
1905 | port *pt = malloc (sizeof (port)); |
1869 | |
1906 | |
… | |
… | |
1883 | pt->rep.string.curr = start; |
1920 | pt->rep.string.curr = start; |
1884 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1921 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1885 | return pt; |
1922 | return pt; |
1886 | } |
1923 | } |
1887 | |
1924 | |
1888 | static pointer |
1925 | ecb_cold static pointer |
1889 | port_from_scratch (SCHEME_P) |
1926 | port_from_scratch (SCHEME_P) |
1890 | { |
1927 | { |
1891 | port *pt = port_rep_from_scratch (SCHEME_A); |
1928 | port *pt = port_rep_from_scratch (SCHEME_A); |
1892 | |
1929 | |
1893 | if (!pt && USE_ERROR_CHECKING) |
1930 | if (!pt && USE_ERROR_CHECKING) |
1894 | return NIL; |
1931 | return NIL; |
1895 | |
1932 | |
1896 | return mk_port (SCHEME_A_ pt); |
1933 | return mk_port (SCHEME_A_ pt); |
1897 | } |
1934 | } |
1898 | |
1935 | |
1899 | static void |
1936 | ecb_cold static void |
1900 | port_close (SCHEME_P_ pointer p, int flag) |
1937 | port_close (SCHEME_P_ pointer p, int flag) |
1901 | { |
1938 | { |
1902 | port *pt = p->object.port; |
1939 | port *pt = port (p); |
1903 | |
1940 | |
1904 | pt->kind &= ~flag; |
1941 | pt->kind &= ~flag; |
1905 | |
1942 | |
1906 | if ((pt->kind & (port_input | port_output)) == 0) |
1943 | if ((pt->kind & (port_input | port_output)) == 0) |
1907 | { |
1944 | { |
… | |
… | |
1924 | } |
1961 | } |
1925 | } |
1962 | } |
1926 | #endif |
1963 | #endif |
1927 | |
1964 | |
1928 | /* get new character from input file */ |
1965 | /* get new character from input file */ |
1929 | static int |
1966 | ecb_cold static int |
1930 | inchar (SCHEME_P) |
1967 | inchar (SCHEME_P) |
1931 | { |
1968 | { |
1932 | int c; |
1969 | int c; |
1933 | port *pt; |
1970 | port *pt = port (SCHEME_V->inport); |
1934 | |
|
|
1935 | pt = SCHEME_V->inport->object.port; |
|
|
1936 | |
1971 | |
1937 | if (pt->kind & port_saw_EOF) |
1972 | if (pt->kind & port_saw_EOF) |
1938 | return EOF; |
1973 | return EOF; |
1939 | |
1974 | |
1940 | c = basic_inchar (pt); |
1975 | c = basic_inchar (pt); |
… | |
… | |
1950 | } |
1985 | } |
1951 | |
1986 | |
1952 | return c; |
1987 | return c; |
1953 | } |
1988 | } |
1954 | |
1989 | |
1955 | static int ungot = -1; |
1990 | ecb_cold static int |
1956 | |
|
|
1957 | static int |
|
|
1958 | basic_inchar (port *pt) |
1991 | basic_inchar (port *pt) |
1959 | { |
1992 | { |
1960 | #if USE_PORTS |
|
|
1961 | if (pt->unget != -1) |
1993 | if (pt->unget != -1) |
1962 | { |
1994 | { |
1963 | int r = pt->unget; |
1995 | int r = pt->unget; |
1964 | pt->unget = -1; |
1996 | pt->unget = -1; |
1965 | return r; |
1997 | return r; |
1966 | } |
1998 | } |
1967 | |
1999 | |
|
|
2000 | #if USE_PORTS |
1968 | if (pt->kind & port_file) |
2001 | if (pt->kind & port_file) |
1969 | { |
2002 | { |
1970 | char c; |
2003 | char c; |
1971 | |
2004 | |
1972 | if (!read (pt->rep.stdio.file, &c, 1)) |
2005 | if (!read (pt->rep.stdio.file, &c, 1)) |
… | |
… | |
1980 | return EOF; |
2013 | return EOF; |
1981 | else |
2014 | else |
1982 | return *pt->rep.string.curr++; |
2015 | return *pt->rep.string.curr++; |
1983 | } |
2016 | } |
1984 | #else |
2017 | #else |
1985 | if (ungot == -1) |
|
|
1986 | { |
|
|
1987 | char c; |
2018 | char c; |
1988 | if (!read (0, &c, 1)) |
2019 | |
|
|
2020 | if (!read (pt->rep.stdio.file, &c, 1)) |
1989 | return EOF; |
2021 | return EOF; |
1990 | |
2022 | |
1991 | ungot = c; |
|
|
1992 | } |
|
|
1993 | |
|
|
1994 | { |
|
|
1995 | int r = ungot; |
|
|
1996 | ungot = -1; |
|
|
1997 | return r; |
2023 | return c; |
1998 | } |
|
|
1999 | #endif |
2024 | #endif |
2000 | } |
2025 | } |
2001 | |
2026 | |
2002 | /* back character to input buffer */ |
2027 | /* back character to input buffer */ |
2003 | static void |
2028 | ecb_cold static void |
2004 | backchar (SCHEME_P_ int c) |
2029 | backchar (SCHEME_P_ int c) |
2005 | { |
2030 | { |
2006 | #if USE_PORTS |
2031 | port *pt = port (SCHEME_V->inport); |
2007 | port *pt; |
|
|
2008 | |
2032 | |
2009 | if (c == EOF) |
2033 | if (c == EOF) |
2010 | return; |
2034 | return; |
2011 | |
2035 | |
2012 | pt = SCHEME_V->inport->object.port; |
|
|
2013 | pt->unget = c; |
2036 | pt->unget = c; |
2014 | #else |
|
|
2015 | if (c == EOF) |
|
|
2016 | return; |
|
|
2017 | |
|
|
2018 | ungot = c; |
|
|
2019 | #endif |
|
|
2020 | } |
2037 | } |
2021 | |
2038 | |
2022 | #if USE_PORTS |
2039 | #if USE_PORTS |
2023 | static int |
2040 | ecb_cold static int |
2024 | realloc_port_string (SCHEME_P_ port *p) |
2041 | realloc_port_string (SCHEME_P_ port *p) |
2025 | { |
2042 | { |
2026 | char *start = p->rep.string.start; |
2043 | char *start = p->rep.string.start; |
2027 | 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; |
2028 | char *str = malloc (new_size); |
2045 | char *str = malloc (new_size); |
… | |
… | |
2041 | else |
2058 | else |
2042 | return 0; |
2059 | return 0; |
2043 | } |
2060 | } |
2044 | #endif |
2061 | #endif |
2045 | |
2062 | |
2046 | INTERFACE void |
2063 | ecb_cold static void |
2047 | putstr (SCHEME_P_ const char *s) |
2064 | putchars (SCHEME_P_ const char *s, int len) |
2048 | { |
2065 | { |
|
|
2066 | port *pt = port (SCHEME_V->outport); |
|
|
2067 | |
2049 | #if USE_PORTS |
2068 | #if USE_PORTS |
2050 | port *pt = SCHEME_V->outport->object.port; |
|
|
2051 | |
|
|
2052 | if (pt->kind & port_file) |
|
|
2053 | write (pt->rep.stdio.file, s, strlen (s)); |
|
|
2054 | else |
|
|
2055 | for (; *s; s++) |
|
|
2056 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2057 | *pt->rep.string.curr++ = *s; |
|
|
2058 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2059 | *pt->rep.string.curr++ = *s; |
|
|
2060 | |
|
|
2061 | #else |
|
|
2062 | xwrstr (s); |
|
|
2063 | #endif |
|
|
2064 | } |
|
|
2065 | |
|
|
2066 | static void |
|
|
2067 | putchars (SCHEME_P_ const char *s, int len) |
|
|
2068 | { |
|
|
2069 | #if USE_PORTS |
|
|
2070 | port *pt = SCHEME_V->outport->object.port; |
|
|
2071 | |
|
|
2072 | if (pt->kind & port_file) |
2069 | if (pt->kind & port_file) |
2073 | write (pt->rep.stdio.file, s, len); |
2070 | write (pt->rep.stdio.file, s, len); |
2074 | else |
2071 | else |
2075 | { |
2072 | { |
2076 | for (; len; len--) |
2073 | for (; len; len--) |
… | |
… | |
2081 | *pt->rep.string.curr++ = *s++; |
2078 | *pt->rep.string.curr++ = *s++; |
2082 | } |
2079 | } |
2083 | } |
2080 | } |
2084 | |
2081 | |
2085 | #else |
2082 | #else |
2086 | write (1, s, len); |
2083 | write (1, s, len); // output not initialised |
2087 | #endif |
2084 | #endif |
|
|
2085 | } |
|
|
2086 | |
|
|
2087 | INTERFACE void |
|
|
2088 | putstr (SCHEME_P_ const char *s) |
|
|
2089 | { |
|
|
2090 | putchars (SCHEME_A_ s, strlen (s)); |
2088 | } |
2091 | } |
2089 | |
2092 | |
2090 | INTERFACE void |
2093 | INTERFACE void |
2091 | putcharacter (SCHEME_P_ int c) |
2094 | putcharacter (SCHEME_P_ int c) |
2092 | { |
2095 | { |
2093 | #if USE_PORTS |
|
|
2094 | port *pt = SCHEME_V->outport->object.port; |
|
|
2095 | |
|
|
2096 | if (pt->kind & port_file) |
|
|
2097 | { |
|
|
2098 | char cc = c; |
|
|
2099 | write (pt->rep.stdio.file, &cc, 1); |
|
|
2100 | } |
|
|
2101 | else |
|
|
2102 | { |
|
|
2103 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2104 | *pt->rep.string.curr++ = c; |
|
|
2105 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2106 | *pt->rep.string.curr++ = c; |
|
|
2107 | } |
|
|
2108 | |
|
|
2109 | #else |
|
|
2110 | char cc = c; |
2096 | char cc = c; |
2111 | write (1, &c, 1); |
2097 | |
2112 | #endif |
2098 | putchars (SCHEME_A_ &cc, 1); |
2113 | } |
2099 | } |
2114 | |
2100 | |
2115 | /* read characters up to delimiter, but cater to character constants */ |
2101 | /* read characters up to delimiter, but cater to character constants */ |
2116 | static char * |
2102 | ecb_cold static char * |
2117 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2103 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2118 | { |
2104 | { |
2119 | char *p = SCHEME_V->strbuff + skip; |
2105 | char *p = SCHEME_V->strbuff + skip; |
2120 | |
2106 | |
2121 | 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)))); |
… | |
… | |
2130 | |
2116 | |
2131 | return SCHEME_V->strbuff; |
2117 | return SCHEME_V->strbuff; |
2132 | } |
2118 | } |
2133 | |
2119 | |
2134 | /* read string expression "xxx...xxx" */ |
2120 | /* read string expression "xxx...xxx" */ |
2135 | static pointer |
2121 | ecb_cold static pointer |
2136 | readstrexp (SCHEME_P_ char delim) |
2122 | readstrexp (SCHEME_P_ char delim) |
2137 | { |
2123 | { |
2138 | char *p = SCHEME_V->strbuff; |
2124 | char *p = SCHEME_V->strbuff; |
2139 | int c; |
2125 | int c; |
2140 | int c1 = 0; |
2126 | int c1 = 0; |
… | |
… | |
2173 | case '7': |
2159 | case '7': |
2174 | state = st_oct1; |
2160 | state = st_oct1; |
2175 | c1 = c - '0'; |
2161 | c1 = c - '0'; |
2176 | break; |
2162 | break; |
2177 | |
2163 | |
|
|
2164 | case 'a': *p++ = '\a'; state = st_ok; break; |
|
|
2165 | case 'n': *p++ = '\n'; state = st_ok; break; |
|
|
2166 | case 'r': *p++ = '\r'; state = st_ok; break; |
|
|
2167 | case 't': *p++ = '\t'; state = st_ok; break; |
|
|
2168 | |
|
|
2169 | case '\\': |
|
|
2170 | skipspace (SCHEME_A); |
|
|
2171 | break; |
|
|
2172 | |
|
|
2173 | //TODO: x should end in ;, not two-digit hex |
2178 | case 'x': |
2174 | case 'x': |
2179 | case 'X': |
2175 | case 'X': |
2180 | state = st_x1; |
2176 | state = st_x1; |
2181 | c1 = 0; |
2177 | c1 = 0; |
2182 | break; |
|
|
2183 | |
|
|
2184 | case 'n': |
|
|
2185 | *p++ = '\n'; |
|
|
2186 | state = st_ok; |
|
|
2187 | break; |
|
|
2188 | |
|
|
2189 | case 't': |
|
|
2190 | *p++ = '\t'; |
|
|
2191 | state = st_ok; |
|
|
2192 | break; |
|
|
2193 | |
|
|
2194 | case 'r': |
|
|
2195 | *p++ = '\r'; |
|
|
2196 | state = st_ok; |
|
|
2197 | break; |
2178 | break; |
2198 | |
2179 | |
2199 | default: |
2180 | default: |
2200 | *p++ = c; |
2181 | *p++ = c; |
2201 | state = st_ok; |
2182 | state = st_ok; |
… | |
… | |
2253 | } |
2234 | } |
2254 | } |
2235 | } |
2255 | } |
2236 | } |
2256 | |
2237 | |
2257 | /* check c is in chars */ |
2238 | /* check c is in chars */ |
2258 | ecb_inline int |
2239 | ecb_cold int |
2259 | is_one_of (const char *s, int c) |
2240 | is_one_of (const char *s, int c) |
2260 | { |
2241 | { |
2261 | if (c == EOF) |
|
|
2262 | return 1; |
|
|
2263 | |
|
|
2264 | return !!strchr (s, c); |
2242 | return c == EOF || !!strchr (s, c); |
2265 | } |
2243 | } |
2266 | |
2244 | |
2267 | /* skip white characters */ |
2245 | /* skip white characters */ |
2268 | ecb_inline int |
2246 | ecb_cold int |
2269 | skipspace (SCHEME_P) |
2247 | skipspace (SCHEME_P) |
2270 | { |
2248 | { |
2271 | int c, curr_line = 0; |
2249 | int c, curr_line = 0; |
2272 | |
2250 | |
2273 | do |
2251 | do |
2274 | { |
2252 | { |
2275 | c = inchar (SCHEME_A); |
2253 | c = inchar (SCHEME_A); |
|
|
2254 | |
2276 | #if SHOW_ERROR_LINE |
2255 | #if SHOW_ERROR_LINE |
2277 | if (c == '\n') |
2256 | if (ecb_expect_false (c == '\n')) |
2278 | curr_line++; |
2257 | curr_line++; |
2279 | #endif |
2258 | #endif |
|
|
2259 | |
|
|
2260 | if (ecb_expect_false (c == EOF)) |
|
|
2261 | return c; |
2280 | } |
2262 | } |
2281 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2263 | while (is_one_of (WHITESPACE, c)); |
2282 | |
2264 | |
2283 | /* record it */ |
2265 | /* record it */ |
2284 | #if SHOW_ERROR_LINE |
2266 | #if SHOW_ERROR_LINE |
2285 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2267 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2286 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2268 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2287 | #endif |
2269 | #endif |
2288 | |
2270 | |
2289 | if (c != EOF) |
|
|
2290 | { |
|
|
2291 | backchar (SCHEME_A_ c); |
2271 | backchar (SCHEME_A_ c); |
2292 | return 1; |
2272 | return 1; |
2293 | } |
|
|
2294 | else |
|
|
2295 | return EOF; |
|
|
2296 | } |
2273 | } |
2297 | |
2274 | |
2298 | /* get token */ |
2275 | /* get token */ |
2299 | static int |
2276 | ecb_cold static int |
2300 | token (SCHEME_P) |
2277 | token (SCHEME_P) |
2301 | { |
2278 | { |
2302 | int c = skipspace (SCHEME_A); |
2279 | int c = skipspace (SCHEME_A); |
2303 | |
2280 | |
2304 | if (c == EOF) |
2281 | if (c == EOF) |
… | |
… | |
2316 | return TOK_RPAREN; |
2293 | return TOK_RPAREN; |
2317 | |
2294 | |
2318 | case '.': |
2295 | case '.': |
2319 | c = inchar (SCHEME_A); |
2296 | c = inchar (SCHEME_A); |
2320 | |
2297 | |
2321 | if (is_one_of (" \n\t", c)) |
2298 | if (is_one_of (WHITESPACE, c)) |
2322 | return TOK_DOT; |
2299 | return TOK_DOT; |
2323 | else |
2300 | else |
2324 | { |
2301 | { |
2325 | backchar (SCHEME_A_ c); |
2302 | backchar (SCHEME_A_ c); |
2326 | return TOK_DOTATOM; |
2303 | return TOK_DOTATOM; |
… | |
… | |
2402 | } |
2379 | } |
2403 | |
2380 | |
2404 | /* ========== Routines for Printing ========== */ |
2381 | /* ========== Routines for Printing ========== */ |
2405 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2382 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2406 | |
2383 | |
2407 | static void |
2384 | ecb_cold static void |
2408 | printslashstring (SCHEME_P_ char *p, int len) |
2385 | printslashstring (SCHEME_P_ char *p, int len) |
2409 | { |
2386 | { |
2410 | int i; |
2387 | int i; |
2411 | unsigned char *s = (unsigned char *) p; |
2388 | unsigned char *s = (unsigned char *) p; |
2412 | |
2389 | |
… | |
… | |
2467 | } |
2444 | } |
2468 | |
2445 | |
2469 | putcharacter (SCHEME_A_ '"'); |
2446 | putcharacter (SCHEME_A_ '"'); |
2470 | } |
2447 | } |
2471 | |
2448 | |
2472 | |
|
|
2473 | /* print atoms */ |
2449 | /* print atoms */ |
2474 | static void |
2450 | ecb_cold static void |
2475 | printatom (SCHEME_P_ pointer l, int f) |
2451 | printatom (SCHEME_P_ pointer l, int f) |
2476 | { |
2452 | { |
2477 | char *p; |
2453 | char *p; |
2478 | int len; |
2454 | int len; |
2479 | |
2455 | |
2480 | atom2str (SCHEME_A_ l, f, &p, &len); |
2456 | atom2str (SCHEME_A_ l, f, &p, &len); |
2481 | putchars (SCHEME_A_ p, len); |
2457 | putchars (SCHEME_A_ p, len); |
2482 | } |
2458 | } |
2483 | |
2459 | |
2484 | |
|
|
2485 | /* Uses internal buffer unless string pointer is already available */ |
2460 | /* Uses internal buffer unless string pointer is already available */ |
2486 | static void |
2461 | ecb_cold static void |
2487 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2462 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2488 | { |
2463 | { |
2489 | char *p; |
2464 | char *p; |
2490 | |
2465 | |
2491 | if (l == NIL) |
2466 | if (l == NIL) |
… | |
… | |
2649 | #endif |
2624 | #endif |
2650 | } |
2625 | } |
2651 | else if (is_continuation (l)) |
2626 | else if (is_continuation (l)) |
2652 | p = "#<CONTINUATION>"; |
2627 | p = "#<CONTINUATION>"; |
2653 | else |
2628 | else |
|
|
2629 | { |
|
|
2630 | #if USE_PRINTF |
|
|
2631 | p = SCHEME_V->strbuff; |
|
|
2632 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2633 | #else |
2654 | p = "#<ERROR>"; |
2634 | p = "#<ERROR>"; |
|
|
2635 | #endif |
|
|
2636 | } |
2655 | |
2637 | |
2656 | *pp = p; |
2638 | *pp = p; |
2657 | *plen = strlen (p); |
2639 | *plen = strlen (p); |
2658 | } |
2640 | } |
2659 | |
2641 | |
… | |
… | |
2691 | return car (d); |
2673 | return car (d); |
2692 | |
2674 | |
2693 | p = cons (car (d), cdr (d)); |
2675 | p = cons (car (d), cdr (d)); |
2694 | q = p; |
2676 | q = p; |
2695 | |
2677 | |
2696 | while (cdr (cdr (p)) != NIL) |
2678 | while (cddr (p) != NIL) |
2697 | { |
2679 | { |
2698 | d = cons (car (p), cdr (p)); |
2680 | d = cons (car (p), cdr (p)); |
2699 | |
2681 | |
2700 | if (cdr (cdr (p)) != NIL) |
2682 | if (cddr (p) != NIL) |
2701 | p = cdr (d); |
2683 | p = cdr (d); |
2702 | } |
2684 | } |
2703 | |
2685 | |
2704 | set_cdr (p, car (cdr (p))); |
2686 | set_cdr (p, cadr (p)); |
2705 | return q; |
2687 | return q; |
2706 | } |
2688 | } |
2707 | |
2689 | |
2708 | /* reverse list -- produce new list */ |
2690 | /* reverse list -- produce new list */ |
2709 | static pointer |
2691 | ecb_hot static pointer |
2710 | reverse (SCHEME_P_ pointer a) |
2692 | reverse (SCHEME_P_ pointer a) |
2711 | { |
2693 | { |
2712 | /* a must be checked by gc */ |
2694 | /* a must be checked by gc */ |
2713 | pointer p = NIL; |
2695 | pointer p = NIL; |
2714 | |
2696 | |
… | |
… | |
2717 | |
2699 | |
2718 | return p; |
2700 | return p; |
2719 | } |
2701 | } |
2720 | |
2702 | |
2721 | /* reverse list --- in-place */ |
2703 | /* reverse list --- in-place */ |
2722 | static pointer |
2704 | ecb_hot static pointer |
2723 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2705 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2724 | { |
2706 | { |
2725 | pointer result = term; |
2707 | pointer result = term; |
2726 | pointer p = list; |
2708 | pointer p = list; |
2727 | |
2709 | |
… | |
… | |
2735 | |
2717 | |
2736 | return result; |
2718 | return result; |
2737 | } |
2719 | } |
2738 | |
2720 | |
2739 | /* append list -- produce new list (in reverse order) */ |
2721 | /* append list -- produce new list (in reverse order) */ |
2740 | static pointer |
2722 | ecb_hot static pointer |
2741 | revappend (SCHEME_P_ pointer a, pointer b) |
2723 | revappend (SCHEME_P_ pointer a, pointer b) |
2742 | { |
2724 | { |
2743 | pointer result = a; |
2725 | pointer result = a; |
2744 | pointer p = b; |
2726 | pointer p = b; |
2745 | |
2727 | |
… | |
… | |
2754 | |
2736 | |
2755 | return S_F; /* signal an error */ |
2737 | return S_F; /* signal an error */ |
2756 | } |
2738 | } |
2757 | |
2739 | |
2758 | /* equivalence of atoms */ |
2740 | /* equivalence of atoms */ |
2759 | int |
2741 | ecb_hot int |
2760 | eqv (pointer a, pointer b) |
2742 | eqv (pointer a, pointer b) |
2761 | { |
2743 | { |
2762 | if (is_string (a)) |
2744 | if (is_string (a)) |
2763 | { |
2745 | { |
2764 | if (is_string (b)) |
2746 | if (is_string (b)) |
… | |
… | |
2858 | } |
2840 | } |
2859 | else |
2841 | else |
2860 | set_car (env, immutable_cons (slot, car (env))); |
2842 | set_car (env, immutable_cons (slot, car (env))); |
2861 | } |
2843 | } |
2862 | |
2844 | |
2863 | static pointer |
2845 | ecb_hot static pointer |
2864 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2846 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2865 | { |
2847 | { |
2866 | pointer x, y; |
2848 | pointer x, y; |
2867 | |
2849 | |
2868 | for (x = env; x != NIL; x = cdr (x)) |
2850 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2889 | return NIL; |
2871 | return NIL; |
2890 | } |
2872 | } |
2891 | |
2873 | |
2892 | #else /* USE_ALIST_ENV */ |
2874 | #else /* USE_ALIST_ENV */ |
2893 | |
2875 | |
2894 | ecb_inline void |
2876 | static void |
2895 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2877 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2896 | { |
2878 | { |
2897 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2879 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2898 | setenvironment (SCHEME_V->envir); |
2880 | setenvironment (SCHEME_V->envir); |
2899 | } |
2881 | } |
2900 | |
2882 | |
2901 | ecb_inline void |
2883 | static void |
2902 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2884 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2903 | { |
2885 | { |
2904 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2886 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2905 | } |
2887 | } |
2906 | |
2888 | |
2907 | static pointer |
2889 | ecb_hot static pointer |
2908 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2890 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2909 | { |
2891 | { |
2910 | pointer x, y; |
2892 | pointer x, y; |
2911 | |
2893 | |
2912 | for (x = env; x != NIL; x = cdr (x)) |
2894 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2926 | return NIL; |
2908 | return NIL; |
2927 | } |
2909 | } |
2928 | |
2910 | |
2929 | #endif /* USE_ALIST_ENV else */ |
2911 | #endif /* USE_ALIST_ENV else */ |
2930 | |
2912 | |
2931 | ecb_inline void |
2913 | static void |
2932 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2914 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2933 | { |
2915 | { |
|
|
2916 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2934 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2917 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2935 | } |
2918 | } |
2936 | |
2919 | |
2937 | ecb_inline void |
2920 | static void |
2938 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2921 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2939 | { |
2922 | { |
2940 | set_cdr (slot, value); |
2923 | set_cdr (slot, value); |
2941 | } |
2924 | } |
2942 | |
2925 | |
2943 | ecb_inline pointer |
2926 | static pointer |
2944 | slot_value_in_env (pointer slot) |
2927 | slot_value_in_env (pointer slot) |
2945 | { |
2928 | { |
2946 | return cdr (slot); |
2929 | return cdr (slot); |
2947 | } |
2930 | } |
2948 | |
2931 | |
2949 | /* ========== Evaluation Cycle ========== */ |
2932 | /* ========== Evaluation Cycle ========== */ |
2950 | |
2933 | |
2951 | static int |
2934 | ecb_cold static int |
2952 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2935 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2953 | { |
2936 | { |
2954 | #if USE_ERROR_HOOK |
2937 | #if USE_ERROR_HOOK |
2955 | pointer x; |
2938 | pointer x; |
2956 | pointer hdl = SCHEME_V->ERROR_HOOK; |
2939 | pointer hdl = SCHEME_V->ERROR_HOOK; |
… | |
… | |
3032 | pointer code; |
3015 | pointer code; |
3033 | }; |
3016 | }; |
3034 | |
3017 | |
3035 | # define STACK_GROWTH 3 |
3018 | # define STACK_GROWTH 3 |
3036 | |
3019 | |
3037 | static void |
3020 | ecb_hot static void |
3038 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3021 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3039 | { |
3022 | { |
3040 | int nframes = (uintptr_t)SCHEME_V->dump; |
3023 | int nframes = (uintptr_t)SCHEME_V->dump; |
3041 | struct dump_stack_frame *next_frame; |
3024 | struct dump_stack_frame *next_frame; |
3042 | |
3025 | |
3043 | /* enough room for the next frame? */ |
3026 | /* enough room for the next frame? */ |
3044 | if (nframes >= SCHEME_V->dump_size) |
3027 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3045 | { |
3028 | { |
3046 | SCHEME_V->dump_size += STACK_GROWTH; |
3029 | SCHEME_V->dump_size += STACK_GROWTH; |
3047 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3030 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3048 | } |
3031 | } |
3049 | |
3032 | |
… | |
… | |
3055 | next_frame->code = code; |
3038 | next_frame->code = code; |
3056 | |
3039 | |
3057 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3040 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3058 | } |
3041 | } |
3059 | |
3042 | |
3060 | static int |
3043 | static ecb_hot int |
3061 | xs_return (SCHEME_P_ pointer a) |
3044 | xs_return (SCHEME_P_ pointer a) |
3062 | { |
3045 | { |
3063 | int nframes = (uintptr_t)SCHEME_V->dump; |
3046 | int nframes = (uintptr_t)SCHEME_V->dump; |
3064 | struct dump_stack_frame *frame; |
3047 | struct dump_stack_frame *frame; |
3065 | |
3048 | |
… | |
… | |
3076 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3059 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3077 | |
3060 | |
3078 | return 0; |
3061 | return 0; |
3079 | } |
3062 | } |
3080 | |
3063 | |
3081 | ecb_inline void |
3064 | ecb_cold void |
3082 | dump_stack_reset (SCHEME_P) |
3065 | dump_stack_reset (SCHEME_P) |
3083 | { |
3066 | { |
3084 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3067 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3085 | SCHEME_V->dump = (pointer)+0; |
3068 | SCHEME_V->dump = (pointer)+0; |
3086 | } |
3069 | } |
3087 | |
3070 | |
3088 | ecb_inline void |
3071 | ecb_cold void |
3089 | dump_stack_initialize (SCHEME_P) |
3072 | dump_stack_initialize (SCHEME_P) |
3090 | { |
3073 | { |
3091 | SCHEME_V->dump_size = 0; |
3074 | SCHEME_V->dump_size = 0; |
3092 | SCHEME_V->dump_base = 0; |
3075 | SCHEME_V->dump_base = 0; |
3093 | dump_stack_reset (SCHEME_A); |
3076 | dump_stack_reset (SCHEME_A); |
3094 | } |
3077 | } |
3095 | |
3078 | |
3096 | static void |
3079 | ecb_cold static void |
3097 | dump_stack_free (SCHEME_P) |
3080 | dump_stack_free (SCHEME_P) |
3098 | { |
3081 | { |
3099 | free (SCHEME_V->dump_base); |
3082 | free (SCHEME_V->dump_base); |
3100 | SCHEME_V->dump_base = 0; |
3083 | SCHEME_V->dump_base = 0; |
3101 | SCHEME_V->dump = (pointer)0; |
3084 | SCHEME_V->dump = (pointer)0; |
3102 | SCHEME_V->dump_size = 0; |
3085 | SCHEME_V->dump_size = 0; |
3103 | } |
3086 | } |
3104 | |
3087 | |
3105 | static void |
3088 | ecb_cold static void |
3106 | dump_stack_mark (SCHEME_P) |
3089 | dump_stack_mark (SCHEME_P) |
3107 | { |
3090 | { |
3108 | int nframes = (uintptr_t)SCHEME_V->dump; |
3091 | int nframes = (uintptr_t)SCHEME_V->dump; |
3109 | int i; |
3092 | int i; |
3110 | |
3093 | |
… | |
… | |
3116 | mark (frame->envir); |
3099 | mark (frame->envir); |
3117 | mark (frame->code); |
3100 | mark (frame->code); |
3118 | } |
3101 | } |
3119 | } |
3102 | } |
3120 | |
3103 | |
3121 | static pointer |
3104 | ecb_cold static pointer |
3122 | ss_get_cont (SCHEME_P) |
3105 | ss_get_cont (SCHEME_P) |
3123 | { |
3106 | { |
3124 | int nframes = (uintptr_t)SCHEME_V->dump; |
3107 | int nframes = (uintptr_t)SCHEME_V->dump; |
3125 | int i; |
3108 | int i; |
3126 | |
3109 | |
… | |
… | |
3138 | } |
3121 | } |
3139 | |
3122 | |
3140 | return cont; |
3123 | return cont; |
3141 | } |
3124 | } |
3142 | |
3125 | |
3143 | static void |
3126 | ecb_cold static void |
3144 | ss_set_cont (SCHEME_P_ pointer cont) |
3127 | ss_set_cont (SCHEME_P_ pointer cont) |
3145 | { |
3128 | { |
3146 | int i = 0; |
3129 | int i = 0; |
3147 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3130 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3148 | |
3131 | |
… | |
… | |
3160 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3143 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3161 | } |
3144 | } |
3162 | |
3145 | |
3163 | #else |
3146 | #else |
3164 | |
3147 | |
3165 | ecb_inline void |
3148 | ecb_cold void |
3166 | dump_stack_reset (SCHEME_P) |
3149 | dump_stack_reset (SCHEME_P) |
3167 | { |
3150 | { |
3168 | SCHEME_V->dump = NIL; |
3151 | SCHEME_V->dump = NIL; |
3169 | } |
3152 | } |
3170 | |
3153 | |
3171 | ecb_inline void |
3154 | ecb_cold void |
3172 | dump_stack_initialize (SCHEME_P) |
3155 | dump_stack_initialize (SCHEME_P) |
3173 | { |
3156 | { |
3174 | dump_stack_reset (SCHEME_A); |
3157 | dump_stack_reset (SCHEME_A); |
3175 | } |
3158 | } |
3176 | |
3159 | |
3177 | static void |
3160 | ecb_cold static void |
3178 | dump_stack_free (SCHEME_P) |
3161 | dump_stack_free (SCHEME_P) |
3179 | { |
3162 | { |
3180 | SCHEME_V->dump = NIL; |
3163 | SCHEME_V->dump = NIL; |
3181 | } |
3164 | } |
3182 | |
3165 | |
3183 | static int |
3166 | ecb_hot static int |
3184 | xs_return (SCHEME_P_ pointer a) |
3167 | xs_return (SCHEME_P_ pointer a) |
3185 | { |
3168 | { |
3186 | pointer dump = SCHEME_V->dump; |
3169 | pointer dump = SCHEME_V->dump; |
3187 | |
3170 | |
3188 | SCHEME_V->value = a; |
3171 | SCHEME_V->value = a; |
… | |
… | |
3198 | SCHEME_V->dump = dump; |
3181 | SCHEME_V->dump = dump; |
3199 | |
3182 | |
3200 | return 0; |
3183 | return 0; |
3201 | } |
3184 | } |
3202 | |
3185 | |
3203 | static void |
3186 | ecb_hot static void |
3204 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3187 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3205 | { |
3188 | { |
3206 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3189 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3207 | cons (args, |
3190 | cons (args, |
3208 | cons (SCHEME_V->envir, |
3191 | cons (SCHEME_V->envir, |
3209 | cons (code, |
3192 | cons (code, |
3210 | SCHEME_V->dump)))); |
3193 | SCHEME_V->dump)))); |
3211 | } |
3194 | } |
3212 | |
3195 | |
|
|
3196 | ecb_cold static void |
|
|
3197 | dump_stack_mark (SCHEME_P) |
|
|
3198 | { |
|
|
3199 | mark (SCHEME_V->dump); |
|
|
3200 | } |
|
|
3201 | |
|
|
3202 | ecb_cold static pointer |
|
|
3203 | ss_get_cont (SCHEME_P) |
|
|
3204 | { |
|
|
3205 | return SCHEME_V->dump; |
|
|
3206 | } |
|
|
3207 | |
|
|
3208 | ecb_cold static void |
|
|
3209 | ss_set_cont (SCHEME_P_ pointer cont) |
|
|
3210 | { |
|
|
3211 | SCHEME_V->dump = cont; |
|
|
3212 | } |
|
|
3213 | |
|
|
3214 | #endif |
|
|
3215 | |
|
|
3216 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
|
|
3217 | |
|
|
3218 | #if EXPERIMENT |
|
|
3219 | |
|
|
3220 | typedef void *stream[1]; |
|
|
3221 | |
|
|
3222 | #define stream_init() { 0 } |
|
|
3223 | |
|
|
3224 | ecb_cold static void |
|
|
3225 | stream_put (void **s, uint8_t byte) |
|
|
3226 | { |
|
|
3227 | uint32_t *sp = *s; |
|
|
3228 | uint32_t size = sizeof (uint32_t) * 2; |
|
|
3229 | uint32_t offs = size; |
|
|
3230 | |
|
|
3231 | if (ecb_expect_true (sp)) |
|
|
3232 | { |
|
|
3233 | offs = sp[0]; |
|
|
3234 | size = sp[1]; |
|
|
3235 | } |
|
|
3236 | |
|
|
3237 | if (ecb_expect_false (offs == size)) |
|
|
3238 | { |
|
|
3239 | size *= 2; |
|
|
3240 | sp = realloc (sp, size); |
|
|
3241 | *s = sp; |
|
|
3242 | sp[1] = size; |
|
|
3243 | |
|
|
3244 | } |
|
|
3245 | |
|
|
3246 | ((uint8_t *)sp)[offs++] = byte; |
|
|
3247 | sp[0] = offs; |
|
|
3248 | } |
|
|
3249 | |
|
|
3250 | #define stream_data(s) ((char *)(s)[0] + sizeof (uint32_t) * 2) |
|
|
3251 | #define stream_size(s) (((uint32_t *)(s)[0])[0] - sizeof (uint32_t) * 2) |
|
|
3252 | #define stream_free(s) free (s[0]) |
|
|
3253 | |
|
|
3254 | // calculates a (preferably small) integer that makes it possible to find |
|
|
3255 | // the symbol again. if pointers were offsets into a memory area... until |
|
|
3256 | // then, we return segment number in the low bits, and offset in the high |
|
|
3257 | // bits |
|
|
3258 | static uint32_t |
|
|
3259 | symbol_id (SCHEME_P_ pointer sym) |
|
|
3260 | { |
|
|
3261 | struct cell *p = CELL (sym); |
|
|
3262 | int i; |
|
|
3263 | |
|
|
3264 | for (i = SCHEME_V->last_cell_seg; i >= 0; --i) |
|
|
3265 | if (SCHEME_V->cell_seg[i] <= p && p < SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize[i]) |
|
|
3266 | { |
|
|
3267 | printf ("seg %d ofs %d/%d\n",i,(p - SCHEME_V->cell_seg[i]),SCHEME_V->cell_segsize[i]);//D |
|
|
3268 | return i | ((p - SCHEME_V->cell_seg[i]) << CELL_NSEGMENT_LOG); |
|
|
3269 | } |
|
|
3270 | |
|
|
3271 | abort (); |
|
|
3272 | } |
|
|
3273 | |
3213 | static void |
3274 | static void |
3214 | dump_stack_mark (SCHEME_P) |
3275 | compile (SCHEME_P_ stream s, pointer x) |
3215 | { |
3276 | { |
3216 | mark (SCHEME_V->dump); |
3277 | if (x == NIL) |
3217 | } |
3278 | { |
|
|
3279 | stream_put (s, 0); |
|
|
3280 | return; |
|
|
3281 | } |
3218 | |
3282 | |
3219 | static pointer |
3283 | if (is_syntax (x)) |
3220 | ss_get_cont (SCHEME_P) |
3284 | { |
3221 | { |
3285 | stream_put (s, 1); |
3222 | return SCHEME_V->dump; |
3286 | stream_put (s, syntaxnum (x)); |
3223 | } |
3287 | return; |
|
|
3288 | } |
3224 | |
3289 | |
3225 | static void |
3290 | switch (type (x)) |
3226 | ss_set_cont (SCHEME_P_ pointer cont) |
3291 | { |
3227 | { |
3292 | case T_INTEGER: |
3228 | SCHEME_V->dump = cont; |
3293 | stream_put (s, 2); |
3229 | } |
3294 | stream_put (s, 0); |
|
|
3295 | stream_put (s, 0); |
|
|
3296 | stream_put (s, 0); |
|
|
3297 | stream_put (s, 0); |
|
|
3298 | return; |
3230 | |
3299 | |
3231 | #endif |
3300 | case T_SYMBOL: |
|
|
3301 | { |
|
|
3302 | uint32_t sym = symbol_id (SCHEME_A_ x); |
|
|
3303 | printf ("sym %x\n", sym);//D |
3232 | |
3304 | |
3233 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3305 | stream_put (s, 3); |
|
|
3306 | |
|
|
3307 | while (sym > 0x7f) |
|
|
3308 | { |
|
|
3309 | stream_put (s, sym | 0x80); |
|
|
3310 | sym >>= 8; |
|
|
3311 | } |
|
|
3312 | |
|
|
3313 | stream_put (s, sym); |
|
|
3314 | } |
|
|
3315 | return; |
|
|
3316 | |
|
|
3317 | case T_PAIR: |
|
|
3318 | stream_put (s, 4); |
|
|
3319 | while (x != NIL) |
|
|
3320 | { |
|
|
3321 | compile (SCHEME_A_ s, car (x)); |
|
|
3322 | x = cdr (x); |
|
|
3323 | } |
|
|
3324 | stream_put (s, 0xff); |
|
|
3325 | return; |
|
|
3326 | |
|
|
3327 | default: |
|
|
3328 | stream_put (s, 5); |
|
|
3329 | stream_put (s, type (x)); |
|
|
3330 | stream_put (s, 0); |
|
|
3331 | stream_put (s, 0); |
|
|
3332 | stream_put (s, 0); |
|
|
3333 | stream_put (s, 0); |
|
|
3334 | break; |
|
|
3335 | } |
|
|
3336 | } |
3234 | |
3337 | |
3235 | static int |
3338 | static int |
|
|
3339 | compile_closure (SCHEME_P_ pointer p) |
|
|
3340 | { |
|
|
3341 | stream s = stream_init (); |
|
|
3342 | |
|
|
3343 | printatom (SCHEME_A_ p, 1);//D |
|
|
3344 | compile (SCHEME_A_ s, car (p)); |
|
|
3345 | |
|
|
3346 | FILE *xxd = popen ("xxd", "we"); |
|
|
3347 | fwrite (stream_data (s), 1, stream_size (s), xxd); |
|
|
3348 | fclose (xxd); |
|
|
3349 | |
|
|
3350 | return stream_size (s); |
|
|
3351 | } |
|
|
3352 | |
|
|
3353 | static int |
|
|
3354 | dtree (SCHEME_P_ int indent, pointer x) |
|
|
3355 | { |
|
|
3356 | int c; |
|
|
3357 | |
|
|
3358 | if (is_syntax (x)) |
|
|
3359 | { |
|
|
3360 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3361 | return 8 + 8; |
|
|
3362 | } |
|
|
3363 | |
|
|
3364 | if (x == NIL) |
|
|
3365 | { |
|
|
3366 | printf ("%*sNIL\n", indent, ""); |
|
|
3367 | return 3; |
|
|
3368 | } |
|
|
3369 | |
|
|
3370 | switch (type (x)) |
|
|
3371 | { |
|
|
3372 | case T_INTEGER: |
|
|
3373 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3374 | return 32+8; |
|
|
3375 | |
|
|
3376 | case T_SYMBOL: |
|
|
3377 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3378 | return 24+8; |
|
|
3379 | |
|
|
3380 | case T_CLOSURE: |
|
|
3381 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3382 | dtree (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3383 | return 32 + dtree (SCHEME_A_ indent + 3, car (x)); |
|
|
3384 | |
|
|
3385 | case T_PAIR: |
|
|
3386 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3387 | c = dtree (SCHEME_A_ indent + 3, car (x)); |
|
|
3388 | c += dtree (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3389 | return c + 1; |
|
|
3390 | |
|
|
3391 | case T_PORT: |
|
|
3392 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3393 | return 24+8; |
|
|
3394 | |
|
|
3395 | case T_VECTOR: |
|
|
3396 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3397 | return 24+8; |
|
|
3398 | |
|
|
3399 | case T_ENVIRONMENT: |
|
|
3400 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3401 | return 0 + dtree (SCHEME_A_ indent + 3, car (x)); |
|
|
3402 | |
|
|
3403 | default: |
|
|
3404 | printf ("unhandled type %d\n", type (x)); |
|
|
3405 | break; |
|
|
3406 | } |
|
|
3407 | } |
|
|
3408 | #endif |
|
|
3409 | |
|
|
3410 | /* syntax, eval, core, ... */ |
|
|
3411 | ecb_hot static int |
3236 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3412 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3237 | { |
3413 | { |
3238 | pointer args = SCHEME_V->args; |
3414 | pointer args = SCHEME_V->args; |
3239 | pointer x, y; |
3415 | pointer x, y; |
3240 | |
3416 | |
3241 | switch (op) |
3417 | switch (op) |
3242 | { |
3418 | { |
|
|
3419 | #if EXPERIMENT //D |
|
|
3420 | case OP_DEBUG: |
|
|
3421 | { |
|
|
3422 | uint32_t len = compile_closure (SCHEME_A_ car (args)); |
|
|
3423 | printf ("len = %d\n", len); |
|
|
3424 | printf ("\n"); |
|
|
3425 | s_return (S_T); |
|
|
3426 | } |
|
|
3427 | #endif |
3243 | case OP_LOAD: /* load */ |
3428 | case OP_LOAD: /* load */ |
3244 | if (file_interactive (SCHEME_A)) |
3429 | if (file_interactive (SCHEME_A)) |
3245 | { |
3430 | { |
3246 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3431 | putstr (SCHEME_A_ "Loading "); |
3247 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3432 | putstr (SCHEME_A_ strvalue (car (args))); |
|
|
3433 | putcharacter (SCHEME_A_ '\n'); |
3248 | } |
3434 | } |
3249 | |
3435 | |
3250 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3436 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3251 | Error_1 ("unable to open", car (args)); |
3437 | Error_1 ("unable to open", car (args)); |
3252 | else |
3438 | |
3253 | { |
|
|
3254 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3439 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3255 | s_goto (OP_T0LVL); |
3440 | s_goto (OP_T0LVL); |
3256 | } |
|
|
3257 | |
3441 | |
3258 | case OP_T0LVL: /* top level */ |
3442 | case OP_T0LVL: /* top level */ |
3259 | |
3443 | |
3260 | /* If we reached the end of file, this loop is done. */ |
3444 | /* If we reached the end of file, this loop is done. */ |
3261 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3445 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3262 | { |
3446 | { |
3263 | if (SCHEME_V->file_i == 0) |
3447 | if (SCHEME_V->file_i == 0) |
3264 | { |
3448 | { |
3265 | SCHEME_V->args = NIL; |
3449 | SCHEME_V->args = NIL; |
3266 | s_goto (OP_QUIT); |
3450 | s_goto (OP_QUIT); |
… | |
… | |
3277 | /* If interactive, be nice to user. */ |
3461 | /* If interactive, be nice to user. */ |
3278 | if (file_interactive (SCHEME_A)) |
3462 | if (file_interactive (SCHEME_A)) |
3279 | { |
3463 | { |
3280 | SCHEME_V->envir = SCHEME_V->global_env; |
3464 | SCHEME_V->envir = SCHEME_V->global_env; |
3281 | dump_stack_reset (SCHEME_A); |
3465 | dump_stack_reset (SCHEME_A); |
3282 | putstr (SCHEME_A_ "\n"); |
3466 | putcharacter (SCHEME_A_ '\n'); |
3283 | putstr (SCHEME_A_ prompt); |
3467 | putstr (SCHEME_A_ prompt); |
3284 | } |
3468 | } |
3285 | |
3469 | |
3286 | /* Set up another iteration of REPL */ |
3470 | /* Set up another iteration of REPL */ |
3287 | SCHEME_V->nesting = 0; |
3471 | SCHEME_V->nesting = 0; |
… | |
… | |
3322 | { |
3506 | { |
3323 | SCHEME_V->print_flag = 1; |
3507 | SCHEME_V->print_flag = 1; |
3324 | SCHEME_V->args = SCHEME_V->value; |
3508 | SCHEME_V->args = SCHEME_V->value; |
3325 | s_goto (OP_P0LIST); |
3509 | s_goto (OP_P0LIST); |
3326 | } |
3510 | } |
3327 | else |
3511 | |
3328 | s_return (SCHEME_V->value); |
3512 | s_return (SCHEME_V->value); |
3329 | |
3513 | |
3330 | case OP_EVAL: /* main part of evaluation */ |
3514 | case OP_EVAL: /* main part of evaluation */ |
3331 | #if USE_TRACING |
3515 | #if USE_TRACING |
3332 | if (SCHEME_V->tracing) |
3516 | if (SCHEME_V->tracing) |
3333 | { |
3517 | { |
… | |
… | |
3344 | #endif |
3528 | #endif |
3345 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3529 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3346 | { |
3530 | { |
3347 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3531 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3348 | |
3532 | |
3349 | if (x != NIL) |
3533 | if (x == NIL) |
3350 | s_return (slot_value_in_env (x)); |
|
|
3351 | else |
|
|
3352 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3534 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3535 | |
|
|
3536 | s_return (slot_value_in_env (x)); |
3353 | } |
3537 | } |
3354 | else if (is_pair (SCHEME_V->code)) |
3538 | else if (is_pair (SCHEME_V->code)) |
3355 | { |
3539 | { |
3356 | x = car (SCHEME_V->code); |
3540 | x = car (SCHEME_V->code); |
3357 | |
3541 | |
… | |
… | |
3366 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3550 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3367 | SCHEME_V->code = x; |
3551 | SCHEME_V->code = x; |
3368 | s_goto (OP_EVAL); |
3552 | s_goto (OP_EVAL); |
3369 | } |
3553 | } |
3370 | } |
3554 | } |
3371 | else |
3555 | |
3372 | s_return (SCHEME_V->code); |
3556 | s_return (SCHEME_V->code); |
3373 | |
3557 | |
3374 | case OP_E0ARGS: /* eval arguments */ |
3558 | case OP_E0ARGS: /* eval arguments */ |
3375 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3559 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3376 | { |
3560 | { |
3377 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3561 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3378 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3562 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3379 | SCHEME_V->code = SCHEME_V->value; |
3563 | SCHEME_V->code = SCHEME_V->value; |
3380 | s_goto (OP_APPLY); |
3564 | s_goto (OP_APPLY); |
… | |
… | |
3434 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3618 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3435 | else if (is_foreign (SCHEME_V->code)) |
3619 | else if (is_foreign (SCHEME_V->code)) |
3436 | { |
3620 | { |
3437 | /* Keep nested calls from GC'ing the arglist */ |
3621 | /* Keep nested calls from GC'ing the arglist */ |
3438 | push_recent_alloc (SCHEME_A_ args, NIL); |
3622 | push_recent_alloc (SCHEME_A_ args, NIL); |
3439 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3623 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3440 | |
3624 | |
3441 | s_return (x); |
3625 | s_return (x); |
3442 | } |
3626 | } |
3443 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3627 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3444 | { |
3628 | { |
… | |
… | |
3481 | |
3665 | |
3482 | case OP_DOMACRO: /* do macro */ |
3666 | case OP_DOMACRO: /* do macro */ |
3483 | SCHEME_V->code = SCHEME_V->value; |
3667 | SCHEME_V->code = SCHEME_V->value; |
3484 | s_goto (OP_EVAL); |
3668 | s_goto (OP_EVAL); |
3485 | |
3669 | |
3486 | #if 1 |
|
|
3487 | |
|
|
3488 | case OP_LAMBDA: /* lambda */ |
3670 | case OP_LAMBDA: /* lambda */ |
3489 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3671 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3490 | set SCHEME_V->value fall thru */ |
3672 | set SCHEME_V->value fall thru */ |
3491 | { |
3673 | { |
3492 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3674 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
… | |
… | |
3498 | SCHEME_V->code = slot_value_in_env (f); |
3680 | SCHEME_V->code = slot_value_in_env (f); |
3499 | s_goto (OP_APPLY); |
3681 | s_goto (OP_APPLY); |
3500 | } |
3682 | } |
3501 | |
3683 | |
3502 | SCHEME_V->value = SCHEME_V->code; |
3684 | SCHEME_V->value = SCHEME_V->code; |
3503 | /* Fallthru */ |
|
|
3504 | } |
3685 | } |
|
|
3686 | /* Fallthru */ |
3505 | |
3687 | |
3506 | case OP_LAMBDA1: |
3688 | case OP_LAMBDA1: |
3507 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3689 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3508 | |
|
|
3509 | #else |
|
|
3510 | |
|
|
3511 | case OP_LAMBDA: /* lambda */ |
|
|
3512 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
|
|
3513 | |
|
|
3514 | #endif |
|
|
3515 | |
3690 | |
3516 | case OP_MKCLOSURE: /* make-closure */ |
3691 | case OP_MKCLOSURE: /* make-closure */ |
3517 | x = car (args); |
3692 | x = car (args); |
3518 | |
3693 | |
3519 | if (car (x) == SCHEME_V->LAMBDA) |
3694 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
3558 | else |
3733 | else |
3559 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3734 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3560 | |
3735 | |
3561 | s_return (SCHEME_V->code); |
3736 | s_return (SCHEME_V->code); |
3562 | |
3737 | |
3563 | |
|
|
3564 | case OP_DEFP: /* defined? */ |
3738 | case OP_DEFP: /* defined? */ |
3565 | x = SCHEME_V->envir; |
3739 | x = SCHEME_V->envir; |
3566 | |
3740 | |
3567 | if (cdr (args) != NIL) |
3741 | if (cdr (args) != NIL) |
3568 | x = cadr (args); |
3742 | x = cadr (args); |
… | |
… | |
3585 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3759 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3586 | s_return (SCHEME_V->value); |
3760 | s_return (SCHEME_V->value); |
3587 | } |
3761 | } |
3588 | else |
3762 | else |
3589 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3763 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3590 | |
|
|
3591 | |
3764 | |
3592 | case OP_BEGIN: /* begin */ |
3765 | case OP_BEGIN: /* begin */ |
3593 | if (!is_pair (SCHEME_V->code)) |
3766 | if (!is_pair (SCHEME_V->code)) |
3594 | s_return (SCHEME_V->code); |
3767 | s_return (SCHEME_V->code); |
3595 | |
3768 | |
… | |
… | |
3607 | case OP_IF1: /* if */ |
3780 | case OP_IF1: /* if */ |
3608 | if (is_true (SCHEME_V->value)) |
3781 | if (is_true (SCHEME_V->value)) |
3609 | SCHEME_V->code = car (SCHEME_V->code); |
3782 | SCHEME_V->code = car (SCHEME_V->code); |
3610 | else |
3783 | else |
3611 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
3784 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
|
|
3785 | |
3612 | s_goto (OP_EVAL); |
3786 | s_goto (OP_EVAL); |
3613 | |
3787 | |
3614 | case OP_LET0: /* let */ |
3788 | case OP_LET0: /* let */ |
3615 | SCHEME_V->args = NIL; |
3789 | SCHEME_V->args = NIL; |
3616 | SCHEME_V->value = SCHEME_V->code; |
3790 | SCHEME_V->value = SCHEME_V->code; |
… | |
… | |
3772 | } |
3946 | } |
3773 | else |
3947 | else |
3774 | { |
3948 | { |
3775 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3949 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3776 | s_return (NIL); |
3950 | s_return (NIL); |
3777 | else |
3951 | |
3778 | { |
|
|
3779 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3952 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3780 | SCHEME_V->code = caar (SCHEME_V->code); |
3953 | SCHEME_V->code = caar (SCHEME_V->code); |
3781 | s_goto (OP_EVAL); |
3954 | s_goto (OP_EVAL); |
3782 | } |
|
|
3783 | } |
3955 | } |
3784 | |
3956 | |
3785 | case OP_DELAY: /* delay */ |
3957 | case OP_DELAY: /* delay */ |
3786 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3958 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3787 | set_typeflag (x, T_PROMISE); |
3959 | set_typeflag (x, T_PROMISE); |
… | |
… | |
3798 | case OP_AND1: /* and */ |
3970 | case OP_AND1: /* and */ |
3799 | if (is_false (SCHEME_V->value)) |
3971 | if (is_false (SCHEME_V->value)) |
3800 | s_return (SCHEME_V->value); |
3972 | s_return (SCHEME_V->value); |
3801 | else if (SCHEME_V->code == NIL) |
3973 | else if (SCHEME_V->code == NIL) |
3802 | s_return (SCHEME_V->value); |
3974 | s_return (SCHEME_V->value); |
3803 | else |
3975 | |
3804 | { |
|
|
3805 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3976 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3806 | SCHEME_V->code = car (SCHEME_V->code); |
3977 | SCHEME_V->code = car (SCHEME_V->code); |
3807 | s_goto (OP_EVAL); |
3978 | s_goto (OP_EVAL); |
3808 | } |
|
|
3809 | |
3979 | |
3810 | case OP_OR0: /* or */ |
3980 | case OP_OR0: /* or */ |
3811 | if (SCHEME_V->code == NIL) |
3981 | if (SCHEME_V->code == NIL) |
3812 | s_return (S_F); |
3982 | s_return (S_F); |
3813 | |
3983 | |
… | |
… | |
3818 | case OP_OR1: /* or */ |
3988 | case OP_OR1: /* or */ |
3819 | if (is_true (SCHEME_V->value)) |
3989 | if (is_true (SCHEME_V->value)) |
3820 | s_return (SCHEME_V->value); |
3990 | s_return (SCHEME_V->value); |
3821 | else if (SCHEME_V->code == NIL) |
3991 | else if (SCHEME_V->code == NIL) |
3822 | s_return (SCHEME_V->value); |
3992 | s_return (SCHEME_V->value); |
3823 | else |
3993 | |
3824 | { |
|
|
3825 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3994 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3826 | SCHEME_V->code = car (SCHEME_V->code); |
3995 | SCHEME_V->code = car (SCHEME_V->code); |
3827 | s_goto (OP_EVAL); |
3996 | s_goto (OP_EVAL); |
3828 | } |
|
|
3829 | |
3997 | |
3830 | case OP_C0STREAM: /* cons-stream */ |
3998 | case OP_C0STREAM: /* cons-stream */ |
3831 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3999 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3832 | SCHEME_V->code = car (SCHEME_V->code); |
4000 | SCHEME_V->code = car (SCHEME_V->code); |
3833 | s_goto (OP_EVAL); |
4001 | s_goto (OP_EVAL); |
… | |
… | |
3898 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
4066 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
3899 | SCHEME_V->code = caar (x); |
4067 | SCHEME_V->code = caar (x); |
3900 | s_goto (OP_EVAL); |
4068 | s_goto (OP_EVAL); |
3901 | } |
4069 | } |
3902 | } |
4070 | } |
3903 | else |
4071 | |
3904 | s_return (NIL); |
4072 | s_return (NIL); |
3905 | |
4073 | |
3906 | case OP_CASE2: /* case */ |
4074 | case OP_CASE2: /* case */ |
3907 | if (is_true (SCHEME_V->value)) |
4075 | if (is_true (SCHEME_V->value)) |
3908 | s_goto (OP_BEGIN); |
4076 | s_goto (OP_BEGIN); |
3909 | else |
4077 | |
3910 | s_return (NIL); |
4078 | s_return (NIL); |
3911 | |
4079 | |
3912 | case OP_PAPPLY: /* apply */ |
4080 | case OP_PAPPLY: /* apply */ |
3913 | SCHEME_V->code = car (args); |
4081 | SCHEME_V->code = car (args); |
3914 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
4082 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
3915 | /*SCHEME_V->args = cadr(args); */ |
4083 | /*SCHEME_V->args = cadr(args); */ |
… | |
… | |
3929 | } |
4097 | } |
3930 | |
4098 | |
3931 | if (USE_ERROR_CHECKING) abort (); |
4099 | if (USE_ERROR_CHECKING) abort (); |
3932 | } |
4100 | } |
3933 | |
4101 | |
3934 | static int |
4102 | /* math, cxr */ |
|
|
4103 | ecb_hot static int |
3935 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4104 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
3936 | { |
4105 | { |
3937 | pointer args = SCHEME_V->args; |
4106 | pointer args = SCHEME_V->args; |
3938 | pointer x = car (args); |
4107 | pointer x = car (args); |
3939 | num v; |
4108 | num v; |
3940 | |
4109 | |
3941 | switch (op) |
4110 | switch (op) |
3942 | { |
4111 | { |
3943 | #if USE_MATH |
4112 | #if USE_MATH |
3944 | case OP_INEX2EX: /* inexact->exact */ |
4113 | case OP_INEX2EX: /* inexact->exact */ |
3945 | { |
|
|
3946 | if (is_integer (x)) |
4114 | if (!is_integer (x)) |
3947 | s_return (x); |
4115 | { |
3948 | |
|
|
3949 | RVALUE r = rvalue_unchecked (x); |
4116 | RVALUE r = rvalue_unchecked (x); |
3950 | |
4117 | |
3951 | if (r == (RVALUE)(IVALUE)r) |
4118 | if (r == (RVALUE)(IVALUE)r) |
3952 | s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); |
4119 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
3953 | else |
4120 | else |
3954 | Error_1 ("inexact->exact: not integral:", x); |
4121 | Error_1 ("inexact->exact: not integral:", x); |
3955 | } |
4122 | } |
3956 | |
4123 | |
|
|
4124 | s_return (x); |
|
|
4125 | |
|
|
4126 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4127 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4128 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4129 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
|
|
4130 | |
|
|
4131 | case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
3957 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4132 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
3958 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
4133 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
|
|
4134 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
3959 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4135 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
3960 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4136 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
3961 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4137 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
3962 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4138 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
3963 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4139 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
3964 | |
4140 | |
3965 | case OP_ATAN: |
4141 | case OP_ATAN: |
|
|
4142 | s_return (mk_real (SCHEME_A_ |
3966 | if (cdr (args) == NIL) |
4143 | cdr (args) == NIL |
3967 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4144 | ? atan (rvalue (x)) |
3968 | else |
4145 | : atan2 (rvalue (x), rvalue (cadr (args))))); |
3969 | { |
|
|
3970 | pointer y = cadr (args); |
|
|
3971 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
|
|
3972 | } |
|
|
3973 | |
|
|
3974 | case OP_SQRT: |
|
|
3975 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
|
|
3976 | |
4146 | |
3977 | case OP_EXPT: |
4147 | case OP_EXPT: |
3978 | { |
4148 | { |
3979 | RVALUE result; |
4149 | RVALUE result; |
3980 | int real_result = 1; |
4150 | int real_result = 1; |
… | |
… | |
4003 | if (real_result) |
4173 | if (real_result) |
4004 | s_return (mk_real (SCHEME_A_ result)); |
4174 | s_return (mk_real (SCHEME_A_ result)); |
4005 | else |
4175 | else |
4006 | s_return (mk_integer (SCHEME_A_ result)); |
4176 | s_return (mk_integer (SCHEME_A_ result)); |
4007 | } |
4177 | } |
4008 | |
|
|
4009 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4010 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4011 | |
|
|
4012 | case OP_TRUNCATE: |
|
|
4013 | { |
|
|
4014 | RVALUE n = rvalue (x); |
|
|
4015 | s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n))); |
|
|
4016 | } |
|
|
4017 | |
|
|
4018 | case OP_ROUND: |
|
|
4019 | if (is_integer (x)) |
|
|
4020 | s_return (x); |
|
|
4021 | |
|
|
4022 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
|
|
4023 | #endif |
4178 | #endif |
4024 | |
4179 | |
4025 | case OP_ADD: /* + */ |
4180 | case OP_ADD: /* + */ |
4026 | v = num_zero; |
4181 | v = num_zero; |
4027 | |
4182 | |
… | |
… | |
4115 | else |
4270 | else |
4116 | Error_0 ("modulo: division by zero"); |
4271 | Error_0 ("modulo: division by zero"); |
4117 | |
4272 | |
4118 | s_return (mk_number (SCHEME_A_ v)); |
4273 | s_return (mk_number (SCHEME_A_ v)); |
4119 | |
4274 | |
4120 | case OP_CAR: /* car */ |
4275 | /* the compiler will optimize this mess... */ |
4121 | s_return (caar (args)); |
4276 | case OP_CAR: op_car: s_return (car (x)); |
4122 | |
4277 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4123 | case OP_CDR: /* cdr */ |
4278 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4124 | s_return (cdar (args)); |
4279 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4280 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4281 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4282 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4283 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4284 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4285 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4286 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4287 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4288 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4289 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4290 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4291 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4292 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4293 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4294 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4295 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4296 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4297 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4298 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4299 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4300 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4301 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4302 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4303 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4304 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4305 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4125 | |
4306 | |
4126 | case OP_CONS: /* cons */ |
4307 | case OP_CONS: /* cons */ |
4127 | set_cdr (args, cadr (args)); |
4308 | set_cdr (args, cadr (args)); |
4128 | s_return (args); |
4309 | s_return (args); |
4129 | |
4310 | |
… | |
… | |
4303 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4484 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4304 | |
4485 | |
4305 | s_return (newstr); |
4486 | s_return (newstr); |
4306 | } |
4487 | } |
4307 | |
4488 | |
4308 | case OP_SUBSTR: /* substring */ |
4489 | case OP_STRING_COPY: /* substring/string-copy */ |
4309 | { |
4490 | { |
4310 | char *str = strvalue (x); |
4491 | char *str = strvalue (x); |
4311 | int index0 = ivalue_unchecked (cadr (args)); |
4492 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4312 | int index1; |
4493 | int index1; |
4313 | int len; |
4494 | int len; |
4314 | |
4495 | |
4315 | if (index0 > strlength (x)) |
4496 | if (index0 > strlength (x)) |
4316 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4497 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4317 | |
4498 | |
4318 | if (cddr (args) != NIL) |
4499 | if (cddr (args) != NIL) |
4319 | { |
4500 | { |
4320 | index1 = ivalue_unchecked (caddr (args)); |
4501 | index1 = ivalue_unchecked (caddr (args)); |
4321 | |
4502 | |
4322 | if (index1 > strlength (x) || index1 < index0) |
4503 | if (index1 > strlength (x) || index1 < index0) |
4323 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4504 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4324 | } |
4505 | } |
4325 | else |
4506 | else |
4326 | index1 = strlength (x); |
4507 | index1 = strlength (x); |
4327 | |
4508 | |
4328 | len = index1 - index0; |
4509 | len = index1 - index0; |
4329 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4510 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4330 | memcpy (strvalue (x), str + index0, len); |
|
|
4331 | strvalue (x)[len] = 0; |
|
|
4332 | |
4511 | |
4333 | s_return (x); |
4512 | s_return (x); |
4334 | } |
4513 | } |
4335 | |
4514 | |
4336 | case OP_VECTOR: /* vector */ |
4515 | case OP_VECTOR: /* vector */ |
… | |
… | |
4378 | } |
4557 | } |
4379 | |
4558 | |
4380 | case OP_VECLEN: /* vector-length */ |
4559 | case OP_VECLEN: /* vector-length */ |
4381 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4560 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4382 | |
4561 | |
|
|
4562 | case OP_VECRESIZE: |
|
|
4563 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4564 | s_return (x); |
|
|
4565 | |
4383 | case OP_VECREF: /* vector-ref */ |
4566 | case OP_VECREF: /* vector-ref */ |
4384 | { |
4567 | { |
4385 | int index = ivalue_unchecked (cadr (args)); |
4568 | int index = ivalue_unchecked (cadr (args)); |
4386 | |
4569 | |
4387 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4570 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
… | |
… | |
4406 | } |
4589 | } |
4407 | |
4590 | |
4408 | if (USE_ERROR_CHECKING) abort (); |
4591 | if (USE_ERROR_CHECKING) abort (); |
4409 | } |
4592 | } |
4410 | |
4593 | |
4411 | static int |
4594 | /* relational ops */ |
|
|
4595 | ecb_hot static int |
4412 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4596 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4413 | { |
4597 | { |
4414 | pointer x = SCHEME_V->args; |
4598 | pointer x = SCHEME_V->args; |
4415 | |
4599 | |
4416 | for (;;) |
4600 | for (;;) |
… | |
… | |
4437 | } |
4621 | } |
4438 | |
4622 | |
4439 | s_return (S_T); |
4623 | s_return (S_T); |
4440 | } |
4624 | } |
4441 | |
4625 | |
4442 | static int |
4626 | /* predicates */ |
|
|
4627 | ecb_hot static int |
4443 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4628 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4444 | { |
4629 | { |
4445 | pointer args = SCHEME_V->args; |
4630 | pointer args = SCHEME_V->args; |
4446 | pointer a = car (args); |
4631 | pointer a = car (args); |
4447 | pointer d = cdr (args); |
4632 | pointer d = cdr (args); |
4448 | int r; |
4633 | int r; |
4449 | |
4634 | |
4450 | switch (op) |
4635 | switch (op) |
4451 | { |
4636 | { |
4452 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4637 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4453 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
4638 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break; |
4454 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4639 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4455 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4640 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4456 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
4641 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4642 | case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break; |
4457 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4643 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4458 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4644 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4459 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4645 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4460 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4646 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4461 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4647 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4462 | |
4648 | |
4463 | #if USE_CHAR_CLASSIFIERS |
4649 | #if USE_CHAR_CLASSIFIERS |
4464 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4650 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4465 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4651 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4466 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4652 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
… | |
… | |
4493 | } |
4679 | } |
4494 | |
4680 | |
4495 | s_retbool (r); |
4681 | s_retbool (r); |
4496 | } |
4682 | } |
4497 | |
4683 | |
4498 | static int |
4684 | /* promises, list ops, ports */ |
|
|
4685 | ecb_hot static int |
4499 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4686 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4500 | { |
4687 | { |
4501 | pointer args = SCHEME_V->args; |
4688 | pointer args = SCHEME_V->args; |
4502 | pointer a = car (args); |
4689 | pointer a = car (args); |
4503 | pointer x, y; |
4690 | pointer x, y; |
… | |
… | |
4516 | } |
4703 | } |
4517 | else |
4704 | else |
4518 | s_return (SCHEME_V->code); |
4705 | s_return (SCHEME_V->code); |
4519 | |
4706 | |
4520 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4707 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4521 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4708 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4522 | s_return (SCHEME_V->value); |
4709 | s_return (SCHEME_V->value); |
4523 | |
4710 | |
4524 | #if USE_PORTS |
4711 | #if USE_PORTS |
|
|
4712 | |
|
|
4713 | case OP_EOF_OBJECT: /* eof-object */ |
|
|
4714 | s_return (S_EOF); |
4525 | |
4715 | |
4526 | case OP_WRITE: /* write */ |
4716 | case OP_WRITE: /* write */ |
4527 | case OP_DISPLAY: /* display */ |
4717 | case OP_DISPLAY: /* display */ |
4528 | case OP_WRITE_CHAR: /* write-char */ |
4718 | case OP_WRITE_CHAR: /* write-char */ |
4529 | if (is_pair (cdr (SCHEME_V->args))) |
4719 | if (is_pair (cdr (SCHEME_V->args))) |
… | |
… | |
4543 | else |
4733 | else |
4544 | SCHEME_V->print_flag = 0; |
4734 | SCHEME_V->print_flag = 0; |
4545 | |
4735 | |
4546 | s_goto (OP_P0LIST); |
4736 | s_goto (OP_P0LIST); |
4547 | |
4737 | |
|
|
4738 | //TODO: move to scheme |
4548 | case OP_NEWLINE: /* newline */ |
4739 | case OP_NEWLINE: /* newline */ |
4549 | if (is_pair (args)) |
4740 | if (is_pair (args)) |
4550 | { |
4741 | { |
4551 | if (a != SCHEME_V->outport) |
4742 | if (a != SCHEME_V->outport) |
4552 | { |
4743 | { |
… | |
… | |
4554 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4745 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4555 | SCHEME_V->outport = a; |
4746 | SCHEME_V->outport = a; |
4556 | } |
4747 | } |
4557 | } |
4748 | } |
4558 | |
4749 | |
4559 | putstr (SCHEME_A_ "\n"); |
4750 | putcharacter (SCHEME_A_ '\n'); |
4560 | s_return (S_T); |
4751 | s_return (S_T); |
4561 | #endif |
4752 | #endif |
4562 | |
4753 | |
4563 | case OP_ERR0: /* error */ |
4754 | case OP_ERR0: /* error */ |
4564 | SCHEME_V->retcode = -1; |
4755 | SCHEME_V->retcode = -1; |
… | |
… | |
4573 | putstr (SCHEME_A_ strvalue (car (args))); |
4764 | putstr (SCHEME_A_ strvalue (car (args))); |
4574 | SCHEME_V->args = cdr (args); |
4765 | SCHEME_V->args = cdr (args); |
4575 | s_goto (OP_ERR1); |
4766 | s_goto (OP_ERR1); |
4576 | |
4767 | |
4577 | case OP_ERR1: /* error */ |
4768 | case OP_ERR1: /* error */ |
4578 | putstr (SCHEME_A_ " "); |
4769 | putcharacter (SCHEME_A_ ' '); |
4579 | |
4770 | |
4580 | if (args != NIL) |
4771 | if (args != NIL) |
4581 | { |
4772 | { |
4582 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4773 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4583 | SCHEME_V->args = a; |
4774 | SCHEME_V->args = a; |
4584 | SCHEME_V->print_flag = 1; |
4775 | SCHEME_V->print_flag = 1; |
4585 | s_goto (OP_P0LIST); |
4776 | s_goto (OP_P0LIST); |
4586 | } |
4777 | } |
4587 | else |
4778 | else |
4588 | { |
4779 | { |
4589 | putstr (SCHEME_A_ "\n"); |
4780 | putcharacter (SCHEME_A_ '\n'); |
4590 | |
4781 | |
4591 | if (SCHEME_V->interactive_repl) |
4782 | if (SCHEME_V->interactive_repl) |
4592 | s_goto (OP_T0LVL); |
4783 | s_goto (OP_T0LVL); |
4593 | else |
4784 | else |
4594 | return -1; |
4785 | return -1; |
… | |
… | |
4671 | SCHEME_V->gc_verbose = (a != S_F); |
4862 | SCHEME_V->gc_verbose = (a != S_F); |
4672 | s_retbool (was); |
4863 | s_retbool (was); |
4673 | } |
4864 | } |
4674 | |
4865 | |
4675 | case OP_NEWSEGMENT: /* new-segment */ |
4866 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4867 | #if 0 |
4676 | if (!is_pair (args) || !is_number (a)) |
4868 | if (!is_pair (args) || !is_number (a)) |
4677 | Error_0 ("new-segment: argument must be a number"); |
4869 | Error_0 ("new-segment: argument must be a number"); |
4678 | |
4870 | #endif |
4679 | alloc_cellseg (SCHEME_A_ ivalue (a)); |
4871 | s_retbool (alloc_cellseg (SCHEME_A)); |
4680 | |
|
|
4681 | s_return (S_T); |
|
|
4682 | |
4872 | |
4683 | case OP_OBLIST: /* oblist */ |
4873 | case OP_OBLIST: /* oblist */ |
4684 | s_return (oblist_all_symbols (SCHEME_A)); |
4874 | s_return (oblist_all_symbols (SCHEME_A)); |
4685 | |
4875 | |
4686 | #if USE_PORTS |
4876 | #if USE_PORTS |
… | |
… | |
4756 | s_return (p == NIL ? S_F : p); |
4946 | s_return (p == NIL ? S_F : p); |
4757 | } |
4947 | } |
4758 | |
4948 | |
4759 | case OP_GET_OUTSTRING: /* get-output-string */ |
4949 | case OP_GET_OUTSTRING: /* get-output-string */ |
4760 | { |
4950 | { |
4761 | port *p; |
4951 | port *p = port (a); |
4762 | |
4952 | |
4763 | if ((p = a->object.port)->kind & port_string) |
4953 | if (p->kind & port_string) |
4764 | { |
4954 | { |
4765 | off_t size; |
4955 | off_t size; |
4766 | char *str; |
4956 | char *str; |
4767 | |
4957 | |
4768 | size = p->rep.string.curr - p->rep.string.start + 1; |
4958 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4803 | } |
4993 | } |
4804 | |
4994 | |
4805 | if (USE_ERROR_CHECKING) abort (); |
4995 | if (USE_ERROR_CHECKING) abort (); |
4806 | } |
4996 | } |
4807 | |
4997 | |
4808 | static int |
4998 | /* reading */ |
|
|
4999 | ecb_cold static int |
4809 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
5000 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4810 | { |
5001 | { |
4811 | pointer args = SCHEME_V->args; |
5002 | pointer args = SCHEME_V->args; |
4812 | pointer x; |
5003 | pointer x; |
4813 | |
5004 | |
… | |
… | |
4873 | int res; |
5064 | int res; |
4874 | |
5065 | |
4875 | if (is_pair (args)) |
5066 | if (is_pair (args)) |
4876 | p = car (args); |
5067 | p = car (args); |
4877 | |
5068 | |
4878 | res = p->object.port->kind & port_string; |
5069 | res = port (p)->kind & port_string; |
4879 | |
5070 | |
4880 | s_retbool (res); |
5071 | s_retbool (res); |
4881 | } |
5072 | } |
4882 | |
5073 | |
4883 | case OP_SET_INPORT: /* set-input-port */ |
5074 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
4948 | |
5139 | |
4949 | case TOK_DOTATOM: |
5140 | case TOK_DOTATOM: |
4950 | SCHEME_V->strbuff[0] = '.'; |
5141 | SCHEME_V->strbuff[0] = '.'; |
4951 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
5142 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
4952 | |
5143 | |
|
|
5144 | case TOK_STRATOM: |
|
|
5145 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5146 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5147 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
|
|
5148 | |
4953 | case TOK_DQUOTE: |
5149 | case TOK_DQUOTE: |
4954 | x = readstrexp (SCHEME_A_ '"'); |
5150 | x = readstrexp (SCHEME_A_ '"'); |
4955 | |
5151 | |
4956 | if (x == S_F) |
5152 | if (x == S_F) |
4957 | Error_0 ("Error reading string"); |
5153 | Error_0 ("Error reading string"); |
… | |
… | |
5077 | pointer b = cdr (args); |
5273 | pointer b = cdr (args); |
5078 | int ok_abbr = ok_abbrev (b); |
5274 | int ok_abbr = ok_abbrev (b); |
5079 | SCHEME_V->args = car (b); |
5275 | SCHEME_V->args = car (b); |
5080 | |
5276 | |
5081 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5277 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5082 | putstr (SCHEME_A_ "'"); |
5278 | putcharacter (SCHEME_A_ '\''); |
5083 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5279 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5084 | putstr (SCHEME_A_ "`"); |
5280 | putcharacter (SCHEME_A_ '`'); |
5085 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5281 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5086 | putstr (SCHEME_A_ ","); |
5282 | putcharacter (SCHEME_A_ ','); |
5087 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5283 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5088 | putstr (SCHEME_A_ ",@"); |
5284 | putstr (SCHEME_A_ ",@"); |
5089 | else |
5285 | else |
5090 | { |
5286 | { |
5091 | putstr (SCHEME_A_ "("); |
5287 | putcharacter (SCHEME_A_ '('); |
5092 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5288 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5093 | SCHEME_V->args = a; |
5289 | SCHEME_V->args = a; |
5094 | } |
5290 | } |
5095 | |
5291 | |
5096 | s_goto (OP_P0LIST); |
5292 | s_goto (OP_P0LIST); |
… | |
… | |
5098 | |
5294 | |
5099 | case OP_P1LIST: |
5295 | case OP_P1LIST: |
5100 | if (is_pair (args)) |
5296 | if (is_pair (args)) |
5101 | { |
5297 | { |
5102 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5298 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5103 | putstr (SCHEME_A_ " "); |
5299 | putcharacter (SCHEME_A_ ' '); |
5104 | SCHEME_V->args = car (args); |
5300 | SCHEME_V->args = car (args); |
5105 | s_goto (OP_P0LIST); |
5301 | s_goto (OP_P0LIST); |
5106 | } |
5302 | } |
5107 | else if (is_vector (args)) |
5303 | else if (is_vector (args)) |
5108 | { |
5304 | { |
… | |
… | |
5116 | { |
5312 | { |
5117 | putstr (SCHEME_A_ " . "); |
5313 | putstr (SCHEME_A_ " . "); |
5118 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5314 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5119 | } |
5315 | } |
5120 | |
5316 | |
5121 | putstr (SCHEME_A_ ")"); |
5317 | putcharacter (SCHEME_A_ ')'); |
5122 | s_return (S_T); |
5318 | s_return (S_T); |
5123 | } |
5319 | } |
5124 | |
5320 | |
5125 | case OP_PVECFROM: |
5321 | case OP_PVECFROM: |
5126 | { |
5322 | { |
… | |
… | |
5128 | pointer vec = car (args); |
5324 | pointer vec = car (args); |
5129 | int len = veclength (vec); |
5325 | int len = veclength (vec); |
5130 | |
5326 | |
5131 | if (i == len) |
5327 | if (i == len) |
5132 | { |
5328 | { |
5133 | putstr (SCHEME_A_ ")"); |
5329 | putcharacter (SCHEME_A_ ')'); |
5134 | s_return (S_T); |
5330 | s_return (S_T); |
5135 | } |
5331 | } |
5136 | else |
5332 | else |
5137 | { |
5333 | { |
5138 | pointer elem = vector_get (vec, i); |
5334 | pointer elem = vector_get (vec, i); |
… | |
… | |
5140 | ivalue_unchecked (cdr (args)) = i + 1; |
5336 | ivalue_unchecked (cdr (args)) = i + 1; |
5141 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5337 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5142 | SCHEME_V->args = elem; |
5338 | SCHEME_V->args = elem; |
5143 | |
5339 | |
5144 | if (i > 0) |
5340 | if (i > 0) |
5145 | putstr (SCHEME_A_ " "); |
5341 | putcharacter (SCHEME_A_ ' '); |
5146 | |
5342 | |
5147 | s_goto (OP_P0LIST); |
5343 | s_goto (OP_P0LIST); |
5148 | } |
5344 | } |
5149 | } |
5345 | } |
5150 | } |
5346 | } |
5151 | |
5347 | |
5152 | if (USE_ERROR_CHECKING) abort (); |
5348 | if (USE_ERROR_CHECKING) abort (); |
5153 | } |
5349 | } |
5154 | |
5350 | |
5155 | static int |
5351 | /* list ops */ |
|
|
5352 | ecb_hot static int |
5156 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5353 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5157 | { |
5354 | { |
5158 | pointer args = SCHEME_V->args; |
5355 | pointer args = SCHEME_V->args; |
5159 | pointer a = car (args); |
5356 | pointer a = car (args); |
5160 | pointer x, y; |
5357 | pointer x, y; |
… | |
… | |
5203 | |
5400 | |
5204 | case OP_CLOSUREP: /* closure? */ |
5401 | case OP_CLOSUREP: /* closure? */ |
5205 | /* |
5402 | /* |
5206 | * Note, macro object is also a closure. |
5403 | * Note, macro object is also a closure. |
5207 | * Therefore, (closure? <#MACRO>) ==> #t |
5404 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5405 | * (schmorp) well, obviously not, fix? TODO |
5208 | */ |
5406 | */ |
5209 | s_retbool (is_closure (a)); |
5407 | s_retbool (is_closure (a)); |
5210 | |
5408 | |
5211 | case OP_MACROP: /* macro? */ |
5409 | case OP_MACROP: /* macro? */ |
5212 | s_retbool (is_macro (a)); |
5410 | s_retbool (is_macro (a)); |
… | |
… | |
5217 | |
5415 | |
5218 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5416 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5219 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5417 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5220 | |
5418 | |
5221 | typedef int (*test_predicate)(pointer); |
5419 | typedef int (*test_predicate)(pointer); |
5222 | static int |
5420 | |
|
|
5421 | ecb_hot static int |
5223 | tst_any (pointer p) |
5422 | tst_any (pointer p) |
5224 | { |
5423 | { |
5225 | return 1; |
5424 | return 1; |
5226 | } |
5425 | } |
5227 | |
5426 | |
5228 | static int |
5427 | ecb_hot static int |
5229 | tst_inonneg (pointer p) |
5428 | tst_inonneg (pointer p) |
5230 | { |
5429 | { |
5231 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5430 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5232 | } |
5431 | } |
5233 | |
5432 | |
5234 | static int |
5433 | ecb_hot static int |
5235 | tst_is_list (SCHEME_P_ pointer p) |
5434 | tst_is_list (SCHEME_P_ pointer p) |
5236 | { |
5435 | { |
5237 | return p == NIL || is_pair (p); |
5436 | return p == NIL || is_pair (p); |
5238 | } |
5437 | } |
5239 | |
5438 | |
… | |
… | |
5282 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5481 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5283 | #include "opdefines.h" |
5482 | #include "opdefines.h" |
5284 | #undef OP_DEF |
5483 | #undef OP_DEF |
5285 | ; |
5484 | ; |
5286 | |
5485 | |
5287 | static const char * |
5486 | ecb_cold static const char * |
5288 | opname (int idx) |
5487 | opname (int idx) |
5289 | { |
5488 | { |
5290 | const char *name = opnames; |
5489 | const char *name = opnames; |
5291 | |
5490 | |
5292 | /* should do this at compile time, but would require external program, right? */ |
5491 | /* should do this at compile time, but would require external program, right? */ |
… | |
… | |
5294 | name += strlen (name) + 1; |
5493 | name += strlen (name) + 1; |
5295 | |
5494 | |
5296 | return *name ? name : "ILLEGAL"; |
5495 | return *name ? name : "ILLEGAL"; |
5297 | } |
5496 | } |
5298 | |
5497 | |
5299 | static const char * |
5498 | ecb_cold static const char * |
5300 | procname (pointer x) |
5499 | procname (pointer x) |
5301 | { |
5500 | { |
5302 | return opname (procnum (x)); |
5501 | return opname (procnum (x)); |
5303 | } |
5502 | } |
5304 | |
5503 | |
… | |
… | |
5324 | #undef OP_DEF |
5523 | #undef OP_DEF |
5325 | {0} |
5524 | {0} |
5326 | }; |
5525 | }; |
5327 | |
5526 | |
5328 | /* kernel of this interpreter */ |
5527 | /* kernel of this interpreter */ |
5329 | static void ecb_hot |
5528 | ecb_hot static void |
5330 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5529 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5331 | { |
5530 | { |
5332 | SCHEME_V->op = op; |
5531 | SCHEME_V->op = op; |
5333 | |
5532 | |
5334 | for (;;) |
5533 | for (;;) |
… | |
… | |
5417 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5616 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5418 | return; |
5617 | return; |
5419 | |
5618 | |
5420 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5619 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5421 | { |
5620 | { |
5422 | xwrstr ("No memory!\n"); |
5621 | putstr (SCHEME_A_ "No memory!\n"); |
5423 | return; |
5622 | return; |
5424 | } |
5623 | } |
5425 | } |
5624 | } |
5426 | } |
5625 | } |
5427 | |
5626 | |
5428 | /* ========== Initialization of internal keywords ========== */ |
5627 | /* ========== Initialization of internal keywords ========== */ |
5429 | |
5628 | |
5430 | static void |
5629 | ecb_cold static void |
5431 | assign_syntax (SCHEME_P_ const char *name) |
5630 | assign_syntax (SCHEME_P_ const char *name) |
5432 | { |
5631 | { |
5433 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5632 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5434 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5633 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5435 | } |
5634 | } |
5436 | |
5635 | |
5437 | static void |
5636 | ecb_cold static void |
5438 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5637 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5439 | { |
5638 | { |
5440 | pointer x = mk_symbol (SCHEME_A_ name); |
5639 | pointer x = mk_symbol (SCHEME_A_ name); |
5441 | pointer y = mk_proc (SCHEME_A_ op); |
5640 | pointer y = mk_proc (SCHEME_A_ op); |
5442 | new_slot_in_env (SCHEME_A_ x, y); |
5641 | new_slot_in_env (SCHEME_A_ x, y); |
… | |
… | |
5450 | ivalue_unchecked (y) = op; |
5649 | ivalue_unchecked (y) = op; |
5451 | return y; |
5650 | return y; |
5452 | } |
5651 | } |
5453 | |
5652 | |
5454 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5653 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5455 | static int |
5654 | ecb_hot static int |
5456 | syntaxnum (pointer p) |
5655 | syntaxnum (pointer p) |
5457 | { |
5656 | { |
5458 | const char *s = strvalue (car (p)); |
5657 | const char *s = strvalue (p); |
5459 | |
5658 | |
5460 | switch (strlength (car (p))) |
5659 | switch (strlength (p)) |
5461 | { |
5660 | { |
5462 | case 2: |
5661 | case 2: |
5463 | if (s[0] == 'i') |
5662 | if (s[0] == 'i') |
5464 | return OP_IF0; /* if */ |
5663 | return OP_IF0; /* if */ |
5465 | else |
5664 | else |
… | |
… | |
5540 | ecb_cold int |
5739 | ecb_cold int |
5541 | scheme_init (SCHEME_P) |
5740 | scheme_init (SCHEME_P) |
5542 | { |
5741 | { |
5543 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5742 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5544 | pointer x; |
5743 | pointer x; |
|
|
5744 | |
|
|
5745 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
5746 | * that memset 0 will also set pointers to 0, but memset does |
|
|
5747 | * of course not guarantee that. screw such systems. |
|
|
5748 | */ |
|
|
5749 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5545 | |
5750 | |
5546 | num_set_fixnum (num_zero, 1); |
5751 | num_set_fixnum (num_zero, 1); |
5547 | num_set_ivalue (num_zero, 0); |
5752 | num_set_ivalue (num_zero, 0); |
5548 | num_set_fixnum (num_one, 1); |
5753 | num_set_fixnum (num_one, 1); |
5549 | num_set_ivalue (num_one, 1); |
5754 | num_set_ivalue (num_one, 1); |
… | |
… | |
5561 | SCHEME_V->save_inport = NIL; |
5766 | SCHEME_V->save_inport = NIL; |
5562 | SCHEME_V->loadport = NIL; |
5767 | SCHEME_V->loadport = NIL; |
5563 | SCHEME_V->nesting = 0; |
5768 | SCHEME_V->nesting = 0; |
5564 | SCHEME_V->interactive_repl = 0; |
5769 | SCHEME_V->interactive_repl = 0; |
5565 | |
5770 | |
5566 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5771 | if (!alloc_cellseg (SCHEME_A)) |
5567 | { |
5772 | { |
5568 | #if USE_ERROR_CHECKING |
5773 | #if USE_ERROR_CHECKING |
5569 | SCHEME_V->no_memory = 1; |
5774 | SCHEME_V->no_memory = 1; |
5570 | return 0; |
5775 | return 0; |
5571 | #endif |
5776 | #endif |
5572 | } |
5777 | } |
5573 | |
5778 | |
5574 | SCHEME_V->gc_verbose = 0; |
5779 | SCHEME_V->gc_verbose = 0; |
5575 | dump_stack_initialize (SCHEME_A); |
5780 | dump_stack_initialize (SCHEME_A); |
5576 | SCHEME_V->code = NIL; |
5781 | SCHEME_V->code = NIL; |
5577 | SCHEME_V->args = NIL; |
5782 | SCHEME_V->args = NIL; |
5578 | SCHEME_V->envir = NIL; |
5783 | SCHEME_V->envir = NIL; |
|
|
5784 | SCHEME_V->value = NIL; |
5579 | SCHEME_V->tracing = 0; |
5785 | SCHEME_V->tracing = 0; |
5580 | |
5786 | |
5581 | /* init NIL */ |
5787 | /* init NIL */ |
5582 | set_typeflag (NIL, T_ATOM | T_MARK); |
5788 | set_typeflag (NIL, T_ATOM | T_MARK); |
5583 | set_car (NIL, NIL); |
5789 | set_car (NIL, NIL); |
… | |
… | |
5639 | |
5845 | |
5640 | return !SCHEME_V->no_memory; |
5846 | return !SCHEME_V->no_memory; |
5641 | } |
5847 | } |
5642 | |
5848 | |
5643 | #if USE_PORTS |
5849 | #if USE_PORTS |
5644 | void |
5850 | ecb_cold void |
5645 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5851 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5646 | { |
5852 | { |
5647 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5853 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5648 | } |
5854 | } |
5649 | |
5855 | |
5650 | void |
5856 | ecb_cold void |
5651 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5857 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5652 | { |
5858 | { |
5653 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5859 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5654 | } |
5860 | } |
5655 | |
5861 | |
5656 | void |
5862 | ecb_cold void |
5657 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5863 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5658 | { |
5864 | { |
5659 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5865 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5660 | } |
5866 | } |
5661 | |
5867 | |
5662 | void |
5868 | ecb_cold void |
5663 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5869 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5664 | { |
5870 | { |
5665 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5871 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5666 | } |
5872 | } |
5667 | #endif |
5873 | #endif |
5668 | |
5874 | |
5669 | void |
5875 | ecb_cold void |
5670 | scheme_set_external_data (SCHEME_P_ void *p) |
5876 | scheme_set_external_data (SCHEME_P_ void *p) |
5671 | { |
5877 | { |
5672 | SCHEME_V->ext_data = p; |
5878 | SCHEME_V->ext_data = p; |
5673 | } |
5879 | } |
5674 | |
5880 | |
… | |
… | |
5722 | } |
5928 | } |
5723 | } |
5929 | } |
5724 | #endif |
5930 | #endif |
5725 | } |
5931 | } |
5726 | |
5932 | |
5727 | void |
5933 | ecb_cold void |
5728 | scheme_load_file (SCHEME_P_ int fin) |
5934 | scheme_load_file (SCHEME_P_ int fin) |
5729 | { |
5935 | { |
5730 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5936 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5731 | } |
5937 | } |
5732 | |
5938 | |
5733 | void |
5939 | ecb_cold void |
5734 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5940 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5735 | { |
5941 | { |
5736 | dump_stack_reset (SCHEME_A); |
5942 | dump_stack_reset (SCHEME_A); |
5737 | SCHEME_V->envir = SCHEME_V->global_env; |
5943 | SCHEME_V->envir = SCHEME_V->global_env; |
5738 | SCHEME_V->file_i = 0; |
5944 | SCHEME_V->file_i = 0; |
5739 | SCHEME_V->load_stack[0].unget = -1; |
5945 | SCHEME_V->load_stack[0].unget = -1; |
5740 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5946 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5741 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5947 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5742 | #if USE_PORTS |
|
|
5743 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5948 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5744 | #endif |
|
|
5745 | SCHEME_V->retcode = 0; |
5949 | SCHEME_V->retcode = 0; |
5746 | |
5950 | |
5747 | #if USE_PORTS |
|
|
5748 | if (fin == STDIN_FILENO) |
5951 | if (fin == STDIN_FILENO) |
5749 | SCHEME_V->interactive_repl = 1; |
5952 | SCHEME_V->interactive_repl = 1; |
5750 | #endif |
|
|
5751 | |
5953 | |
5752 | #if USE_PORTS |
5954 | #if USE_PORTS |
5753 | #if SHOW_ERROR_LINE |
5955 | #if SHOW_ERROR_LINE |
5754 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5956 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5755 | |
5957 | |
… | |
… | |
5759 | #endif |
5961 | #endif |
5760 | |
5962 | |
5761 | SCHEME_V->inport = SCHEME_V->loadport; |
5963 | SCHEME_V->inport = SCHEME_V->loadport; |
5762 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5964 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5763 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5965 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
|
|
5966 | |
5764 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5967 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5765 | |
5968 | |
5766 | if (SCHEME_V->retcode == 0) |
5969 | if (SCHEME_V->retcode == 0) |
5767 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5970 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5768 | } |
5971 | } |
5769 | |
5972 | |
5770 | void |
5973 | ecb_cold void |
5771 | scheme_load_string (SCHEME_P_ const char *cmd) |
5974 | scheme_load_string (SCHEME_P_ const char *cmd) |
5772 | { |
5975 | { |
|
|
5976 | #if USE_PORTs |
5773 | dump_stack_reset (SCHEME_A); |
5977 | dump_stack_reset (SCHEME_A); |
5774 | SCHEME_V->envir = SCHEME_V->global_env; |
5978 | SCHEME_V->envir = SCHEME_V->global_env; |
5775 | SCHEME_V->file_i = 0; |
5979 | SCHEME_V->file_i = 0; |
5776 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5980 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5777 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
5981 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
5778 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
5982 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
5779 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5983 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5780 | #if USE_PORTS |
|
|
5781 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5984 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5782 | #endif |
|
|
5783 | SCHEME_V->retcode = 0; |
5985 | SCHEME_V->retcode = 0; |
5784 | SCHEME_V->interactive_repl = 0; |
5986 | SCHEME_V->interactive_repl = 0; |
5785 | SCHEME_V->inport = SCHEME_V->loadport; |
5987 | SCHEME_V->inport = SCHEME_V->loadport; |
5786 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5988 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5787 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5989 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5788 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5990 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5789 | |
5991 | |
5790 | if (SCHEME_V->retcode == 0) |
5992 | if (SCHEME_V->retcode == 0) |
5791 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5993 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
|
|
5994 | #else |
|
|
5995 | abort (); |
|
|
5996 | #endif |
5792 | } |
5997 | } |
5793 | |
5998 | |
5794 | void |
5999 | ecb_cold void |
5795 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
6000 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5796 | { |
6001 | { |
5797 | pointer x; |
6002 | pointer x; |
5798 | |
6003 | |
5799 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
6004 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
… | |
… | |
5804 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
6009 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5805 | } |
6010 | } |
5806 | |
6011 | |
5807 | #if !STANDALONE |
6012 | #if !STANDALONE |
5808 | |
6013 | |
5809 | void |
6014 | ecb_cold void |
5810 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
6015 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5811 | { |
6016 | { |
5812 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
6017 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5813 | } |
6018 | } |
5814 | |
6019 | |
5815 | void |
6020 | ecb_cold void |
5816 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
6021 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5817 | { |
6022 | { |
5818 | int i; |
6023 | int i; |
5819 | |
6024 | |
5820 | for (i = 0; i < count; i++) |
6025 | for (i = 0; i < count; i++) |
5821 | scheme_register_foreign_func (SCHEME_A_ list + i); |
6026 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5822 | } |
6027 | } |
5823 | |
6028 | |
5824 | pointer |
6029 | ecb_cold pointer |
5825 | scheme_apply0 (SCHEME_P_ const char *procname) |
6030 | scheme_apply0 (SCHEME_P_ const char *procname) |
5826 | { |
6031 | { |
5827 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
6032 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
5828 | } |
6033 | } |
5829 | |
6034 | |
5830 | void |
6035 | ecb_cold void |
5831 | save_from_C_call (SCHEME_P) |
6036 | save_from_C_call (SCHEME_P) |
5832 | { |
6037 | { |
5833 | pointer saved_data = cons (car (S_SINK), |
6038 | pointer saved_data = cons (car (S_SINK), |
5834 | cons (SCHEME_V->envir, |
6039 | cons (SCHEME_V->envir, |
5835 | SCHEME_V->dump)); |
6040 | SCHEME_V->dump)); |
… | |
… | |
5839 | /* Truncate the dump stack so TS will return here when done, not |
6044 | /* Truncate the dump stack so TS will return here when done, not |
5840 | directly resume pre-C-call operations. */ |
6045 | directly resume pre-C-call operations. */ |
5841 | dump_stack_reset (SCHEME_A); |
6046 | dump_stack_reset (SCHEME_A); |
5842 | } |
6047 | } |
5843 | |
6048 | |
5844 | void |
6049 | ecb_cold void |
5845 | restore_from_C_call (SCHEME_P) |
6050 | restore_from_C_call (SCHEME_P) |
5846 | { |
6051 | { |
5847 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
6052 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5848 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
6053 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5849 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
6054 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5850 | /* Pop */ |
6055 | /* Pop */ |
5851 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
6056 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5852 | } |
6057 | } |
5853 | |
6058 | |
5854 | /* "func" and "args" are assumed to be already eval'ed. */ |
6059 | /* "func" and "args" are assumed to be already eval'ed. */ |
5855 | pointer |
6060 | ecb_cold pointer |
5856 | scheme_call (SCHEME_P_ pointer func, pointer args) |
6061 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5857 | { |
6062 | { |
5858 | int old_repl = SCHEME_V->interactive_repl; |
6063 | int old_repl = SCHEME_V->interactive_repl; |
5859 | |
6064 | |
5860 | SCHEME_V->interactive_repl = 0; |
6065 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5867 | SCHEME_V->interactive_repl = old_repl; |
6072 | SCHEME_V->interactive_repl = old_repl; |
5868 | restore_from_C_call (SCHEME_A); |
6073 | restore_from_C_call (SCHEME_A); |
5869 | return SCHEME_V->value; |
6074 | return SCHEME_V->value; |
5870 | } |
6075 | } |
5871 | |
6076 | |
5872 | pointer |
6077 | ecb_cold pointer |
5873 | scheme_eval (SCHEME_P_ pointer obj) |
6078 | scheme_eval (SCHEME_P_ pointer obj) |
5874 | { |
6079 | { |
5875 | int old_repl = SCHEME_V->interactive_repl; |
6080 | int old_repl = SCHEME_V->interactive_repl; |
5876 | |
6081 | |
5877 | SCHEME_V->interactive_repl = 0; |
6082 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5889 | |
6094 | |
5890 | /* ========== Main ========== */ |
6095 | /* ========== Main ========== */ |
5891 | |
6096 | |
5892 | #if STANDALONE |
6097 | #if STANDALONE |
5893 | |
6098 | |
5894 | int |
6099 | ecb_cold int |
5895 | main (int argc, char **argv) |
6100 | main (int argc, char **argv) |
5896 | { |
6101 | { |
5897 | # if USE_MULTIPLICITY |
6102 | # if USE_MULTIPLICITY |
5898 | scheme ssc; |
6103 | scheme ssc; |
5899 | scheme *const SCHEME_V = &ssc; |
6104 | scheme *const SCHEME_V = &ssc; |
… | |
… | |
5901 | # endif |
6106 | # endif |
5902 | int fin; |
6107 | int fin; |
5903 | char *file_name = InitFile; |
6108 | char *file_name = InitFile; |
5904 | int retcode; |
6109 | int retcode; |
5905 | int isfile = 1; |
6110 | int isfile = 1; |
|
|
6111 | system ("ps v $PPID");//D |
5906 | |
6112 | |
5907 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6113 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5908 | { |
6114 | { |
5909 | xwrstr ("Usage: tinyscheme -?\n"); |
6115 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
5910 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6116 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
5911 | xwrstr ("followed by\n"); |
6117 | putstr (SCHEME_A_ "followed by\n"); |
5912 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6118 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
5913 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6119 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
5914 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6120 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
5915 | xwrstr ("Use - as filename for stdin.\n"); |
6121 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
5916 | return 1; |
6122 | return 1; |
5917 | } |
6123 | } |
5918 | |
6124 | |
5919 | if (!scheme_init (SCHEME_A)) |
6125 | if (!scheme_init (SCHEME_A)) |
5920 | { |
6126 | { |
5921 | xwrstr ("Could not initialize!\n"); |
6127 | putstr (SCHEME_A_ "Could not initialize!\n"); |
5922 | return 2; |
6128 | return 2; |
5923 | } |
6129 | } |
5924 | |
6130 | |
5925 | # if USE_PORTS |
6131 | # if USE_PORTS |
5926 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6132 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
5939 | } |
6145 | } |
5940 | #endif |
6146 | #endif |
5941 | |
6147 | |
5942 | do |
6148 | do |
5943 | { |
6149 | { |
5944 | #if USE_PORTS |
|
|
5945 | if (strcmp (file_name, "-") == 0) |
6150 | if (strcmp (file_name, "-") == 0) |
5946 | fin = STDIN_FILENO; |
6151 | fin = STDIN_FILENO; |
5947 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
6152 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
5948 | { |
6153 | { |
5949 | pointer args = NIL; |
6154 | pointer args = NIL; |
… | |
… | |
5967 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
6172 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
5968 | |
6173 | |
5969 | } |
6174 | } |
5970 | else |
6175 | else |
5971 | fin = open (file_name, O_RDONLY); |
6176 | fin = open (file_name, O_RDONLY); |
5972 | #endif |
|
|
5973 | |
6177 | |
5974 | if (isfile && fin < 0) |
6178 | if (isfile && fin < 0) |
5975 | { |
6179 | { |
5976 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6180 | putstr (SCHEME_A_ "Could not open file "); |
|
|
6181 | putstr (SCHEME_A_ file_name); |
|
|
6182 | putcharacter (SCHEME_A_ '\n'); |
5977 | } |
6183 | } |
5978 | else |
6184 | else |
5979 | { |
6185 | { |
5980 | if (isfile) |
6186 | if (isfile) |
5981 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6187 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
5982 | else |
6188 | else |
5983 | scheme_load_string (SCHEME_A_ file_name); |
6189 | scheme_load_string (SCHEME_A_ file_name); |
5984 | |
6190 | |
5985 | #if USE_PORTS |
|
|
5986 | if (!isfile || fin != STDIN_FILENO) |
6191 | if (!isfile || fin != STDIN_FILENO) |
5987 | { |
6192 | { |
5988 | if (SCHEME_V->retcode != 0) |
6193 | if (SCHEME_V->retcode != 0) |
5989 | { |
6194 | { |
5990 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6195 | putstr (SCHEME_A_ "Errors encountered reading "); |
|
|
6196 | putstr (SCHEME_A_ file_name); |
|
|
6197 | putcharacter (SCHEME_A_ '\n'); |
5991 | } |
6198 | } |
5992 | |
6199 | |
5993 | if (isfile) |
6200 | if (isfile) |
5994 | close (fin); |
6201 | close (fin); |
5995 | } |
6202 | } |
5996 | #endif |
|
|
5997 | } |
6203 | } |
5998 | |
6204 | |
5999 | file_name = *argv++; |
6205 | file_name = *argv++; |
6000 | } |
6206 | } |
6001 | while (file_name != 0); |
6207 | while (file_name != 0); |