… | |
… | |
16 | * (MINISCM) This is a revised and modified version by Akira KIDA. |
16 | * (MINISCM) This is a revised and modified version by Akira KIDA. |
17 | * (MINISCM) current version is 0.85k4 (15 May 1994) |
17 | * (MINISCM) current version is 0.85k4 (15 May 1994) |
18 | * |
18 | * |
19 | */ |
19 | */ |
20 | |
20 | |
21 | #define EXPERIMENT 1 |
21 | #define _GNU_SOURCE 1 |
|
|
22 | #define _POSIX_C_SOURCE 200201 |
|
|
23 | #define _XOPEN_SOURCE 600 |
22 | |
24 | |
23 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
|
|
24 | #include "malloc.c" |
|
|
25 | |
25 | |
26 | #define SCHEME_SOURCE |
26 | #define SCHEME_SOURCE |
27 | #include "scheme-private.h" |
27 | #include "scheme-private.h" |
28 | #ifndef WIN32 |
28 | #ifndef WIN32 |
29 | # include <unistd.h> |
29 | # include <unistd.h> |
… | |
… | |
47 | #include <string.h> |
47 | #include <string.h> |
48 | |
48 | |
49 | #include <limits.h> |
49 | #include <limits.h> |
50 | #include <inttypes.h> |
50 | #include <inttypes.h> |
51 | #include <float.h> |
51 | #include <float.h> |
52 | //#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 |
53 | |
60 | |
54 | #if '1' != '0' + 1 \ |
61 | #if '1' != '0' + 1 \ |
55 | || '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 \ |
56 | || '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 \ |
57 | || '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 \ |
… | |
… | |
79 | |
86 | |
80 | #define BACKQUOTE '`' |
87 | #define BACKQUOTE '`' |
81 | #define WHITESPACE " \t\r\n\v\f" |
88 | #define WHITESPACE " \t\r\n\v\f" |
82 | #define DELIMITERS "()\";" WHITESPACE |
89 | #define DELIMITERS "()\";" WHITESPACE |
83 | |
90 | |
84 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
91 | #define NIL POINTER (&SCHEME_V->xNIL) |
85 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
92 | #define S_T POINTER (&SCHEME_V->xT) |
86 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
93 | #define S_F POINTER (&SCHEME_V->xF) |
87 | #define S_SINK (&SCHEME_V->xsink) |
94 | #define S_SINK POINTER (&SCHEME_V->xsink) |
88 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
95 | #define S_EOF POINTER (&SCHEME_V->xEOF_OBJ) |
89 | |
96 | |
90 | #if !USE_MULTIPLICITY |
97 | #if !USE_MULTIPLICITY |
91 | static scheme sc; |
98 | static scheme sc; |
92 | #endif |
99 | #endif |
93 | |
100 | |
94 | static void |
101 | ecb_cold static void |
95 | xbase (char *s, long n, int base) |
102 | xbase (char *s, long n, int base) |
96 | { |
103 | { |
97 | if (n < 0) |
104 | if (n < 0) |
98 | { |
105 | { |
99 | *s++ = '-'; |
106 | *s++ = '-'; |
… | |
… | |
101 | } |
108 | } |
102 | |
109 | |
103 | char *p = s; |
110 | char *p = s; |
104 | |
111 | |
105 | do { |
112 | do { |
106 | *p++ = '0' + n % base; |
113 | *p++ = "0123456789abcdef"[n % base]; |
107 | n /= base; |
114 | n /= base; |
108 | } while (n); |
115 | } while (n); |
109 | |
116 | |
110 | *p-- = 0; |
117 | *p-- = 0; |
111 | |
118 | |
… | |
… | |
114 | char x = *s; *s = *p; *p = x; |
121 | char x = *s; *s = *p; *p = x; |
115 | --p; ++s; |
122 | --p; ++s; |
116 | } |
123 | } |
117 | } |
124 | } |
118 | |
125 | |
119 | static void |
126 | ecb_cold static void |
120 | xnum (char *s, long n) |
127 | xnum (char *s, long n) |
121 | { |
128 | { |
122 | xbase (s, n, 10); |
129 | xbase (s, n, 10); |
123 | } |
130 | } |
124 | |
131 | |
125 | static void |
132 | ecb_cold static void |
126 | xwrstr (const char *s) |
133 | putnum (SCHEME_P_ long n) |
127 | { |
|
|
128 | write (1, s, strlen (s)); |
|
|
129 | } |
|
|
130 | |
|
|
131 | static void |
|
|
132 | xwrnum (long n) |
|
|
133 | { |
134 | { |
134 | char buf[64]; |
135 | char buf[64]; |
135 | |
136 | |
136 | xnum (buf, n); |
137 | xnum (buf, n); |
137 | xwrstr (buf); |
138 | putstr (SCHEME_A_ buf); |
138 | } |
139 | } |
|
|
140 | |
|
|
141 | #if USE_CHAR_CLASSIFIERS |
|
|
142 | #include <ctype.h> |
|
|
143 | #else |
139 | |
144 | |
140 | static char |
145 | static char |
141 | xtoupper (char c) |
146 | xtoupper (char c) |
142 | { |
147 | { |
143 | if (c >= 'a' && c <= 'z') |
148 | if (c >= 'a' && c <= 'z') |
… | |
… | |
163 | |
168 | |
164 | #define toupper(c) xtoupper (c) |
169 | #define toupper(c) xtoupper (c) |
165 | #define tolower(c) xtolower (c) |
170 | #define tolower(c) xtolower (c) |
166 | #define isdigit(c) xisdigit (c) |
171 | #define isdigit(c) xisdigit (c) |
167 | |
172 | |
|
|
173 | #endif |
|
|
174 | |
168 | #if USE_IGNORECASE |
175 | #if USE_IGNORECASE |
169 | static const char * |
176 | ecb_cold static const char * |
170 | xstrlwr (char *s) |
177 | xstrlwr (char *s) |
171 | { |
178 | { |
172 | const char *p = s; |
179 | const char *p = s; |
173 | |
180 | |
174 | while (*s) |
181 | while (*s) |
… | |
… | |
194 | |
201 | |
195 | #ifndef InitFile |
202 | #ifndef InitFile |
196 | # define InitFile "init.scm" |
203 | # define InitFile "init.scm" |
197 | #endif |
204 | #endif |
198 | |
205 | |
199 | #ifndef FIRST_CELLSEGS |
|
|
200 | # define FIRST_CELLSEGS 3 |
|
|
201 | #endif |
|
|
202 | |
|
|
203 | enum scheme_types |
206 | enum scheme_types |
204 | { |
207 | { |
205 | T_INTEGER, |
208 | T_INTEGER, |
|
|
209 | T_CHARACTER, |
206 | T_REAL, |
210 | T_REAL, |
207 | T_STRING, |
211 | T_STRING, |
208 | T_SYMBOL, |
212 | T_SYMBOL, |
209 | T_PROC, |
213 | T_PROC, |
210 | T_PAIR, /* also used for free cells */ |
214 | T_PAIR, /* also used for free cells */ |
211 | T_CLOSURE, |
215 | T_CLOSURE, |
|
|
216 | T_BYTECODE, // temp |
|
|
217 | T_MACRO, |
212 | T_CONTINUATION, |
218 | T_CONTINUATION, |
213 | T_FOREIGN, |
219 | T_FOREIGN, |
214 | T_CHARACTER, |
|
|
215 | T_PORT, |
220 | T_PORT, |
216 | T_VECTOR, |
221 | T_VECTOR, |
217 | T_MACRO, |
|
|
218 | T_PROMISE, |
222 | T_PROMISE, |
219 | T_ENVIRONMENT, |
223 | T_ENVIRONMENT, |
220 | /* one more... */ |
224 | |
221 | T_NUM_SYSTEM_TYPES |
225 | T_NUM_SYSTEM_TYPES |
222 | }; |
226 | }; |
223 | |
227 | |
224 | #define T_MASKTYPE 0x000f |
228 | #define T_MASKTYPE 0x000f |
225 | #define T_SYNTAX 0x0010 |
229 | #define T_SYNTAX 0x0010 |
… | |
… | |
258 | 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); |
259 | static num num_intdiv (num a, num b); |
263 | static num num_intdiv (num a, num b); |
260 | static num num_rem (num a, num b); |
264 | static num num_rem (num a, num b); |
261 | static num num_mod (num a, num b); |
265 | static num num_mod (num a, num b); |
262 | |
266 | |
263 | #if USE_MATH |
|
|
264 | static double round_per_R5RS (double x); |
|
|
265 | #endif |
|
|
266 | static int is_zero_rvalue (RVALUE x); |
267 | static int is_zero_rvalue (RVALUE x); |
267 | |
268 | |
268 | static num num_zero; |
269 | static num num_zero; |
269 | static num num_one; |
270 | static num num_one; |
270 | |
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 | |
271 | /* macros for cell operations */ |
276 | /* macros for cell operations */ |
272 | #define typeflag(p) ((p)->flag + 0) |
277 | #define typeflag(p) (CELL(p)->flag + 0) |
273 | #define set_typeflag(p,v) ((p)->flag = (v)) |
278 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
274 | #define type(p) (typeflag (p) & T_MASKTYPE) |
279 | #define type(p) (typeflag (p) & T_MASKTYPE) |
275 | |
280 | |
276 | INTERFACE int |
281 | INTERFACE int |
277 | is_string (pointer p) |
282 | is_string (pointer p) |
278 | { |
283 | { |
279 | return type (p) == T_STRING; |
284 | return type (p) == T_STRING; |
280 | } |
285 | } |
281 | |
286 | |
282 | #define strvalue(p) ((p)->object.string.svalue) |
287 | #define strvalue(p) (CELL(p)->object.string.svalue) |
283 | #define strlength(p) ((p)->object.string.length) |
288 | #define strlength(p) (CELL(p)->object.string.length) |
284 | |
289 | |
285 | INTERFACE int |
290 | INTERFACE int |
286 | is_vector (pointer p) |
291 | is_vector (pointer p) |
287 | { |
292 | { |
288 | return type (p) == T_VECTOR; |
293 | return type (p) == T_VECTOR; |
289 | } |
294 | } |
290 | |
295 | |
291 | #define vecvalue(p) ((p)->object.vector.vvalue) |
296 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
292 | #define veclength(p) ((p)->object.vector.length) |
297 | #define veclength(p) (CELL(p)->object.vector.length) |
293 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
298 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
294 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
299 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
295 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
300 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
296 | |
301 | |
297 | INTERFACE int |
302 | INTERFACE int |
… | |
… | |
323 | string_value (pointer p) |
328 | string_value (pointer p) |
324 | { |
329 | { |
325 | return strvalue (p); |
330 | return strvalue (p); |
326 | } |
331 | } |
327 | |
332 | |
328 | #define ivalue_unchecked(p) (p)->object.ivalue |
333 | #define ivalue_unchecked(p) CELL(p)->object.ivalue |
329 | #define set_ivalue(p,v) (p)->object.ivalue = (v) |
334 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
330 | |
335 | |
331 | #if USE_REAL |
336 | #if USE_REAL |
332 | #define rvalue_unchecked(p) (p)->object.rvalue |
337 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
333 | #define set_rvalue(p,v) (p)->object.rvalue = (v) |
338 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
334 | #else |
339 | #else |
335 | #define rvalue_unchecked(p) (p)->object.ivalue |
340 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
336 | #define set_rvalue(p,v) (p)->object.ivalue = (v) |
341 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
337 | #endif |
342 | #endif |
338 | |
343 | |
339 | INTERFACE long |
344 | INTERFACE long |
340 | charvalue (pointer p) |
345 | charvalue (pointer p) |
341 | { |
346 | { |
342 | return ivalue_unchecked (p); |
347 | return ivalue_unchecked (p); |
343 | } |
348 | } |
344 | |
349 | |
|
|
350 | #define port(p) CELL(p)->object.port |
|
|
351 | #define set_port(p,v) port(p) = (v) |
345 | INTERFACE int |
352 | INTERFACE int |
346 | is_port (pointer p) |
353 | is_port (pointer p) |
347 | { |
354 | { |
348 | return type (p) == T_PORT; |
355 | return type (p) == T_PORT; |
349 | } |
356 | } |
350 | |
357 | |
351 | INTERFACE int |
358 | INTERFACE int |
352 | is_inport (pointer p) |
359 | is_inport (pointer p) |
353 | { |
360 | { |
354 | return is_port (p) && p->object.port->kind & port_input; |
361 | return is_port (p) && port (p)->kind & port_input; |
355 | } |
362 | } |
356 | |
363 | |
357 | INTERFACE int |
364 | INTERFACE int |
358 | is_outport (pointer p) |
365 | is_outport (pointer p) |
359 | { |
366 | { |
360 | return is_port (p) && p->object.port->kind & port_output; |
367 | return is_port (p) && port (p)->kind & port_output; |
361 | } |
368 | } |
362 | |
369 | |
363 | INTERFACE int |
370 | INTERFACE int |
364 | is_pair (pointer p) |
371 | is_pair (pointer p) |
365 | { |
372 | { |
366 | return type (p) == T_PAIR; |
373 | return type (p) == T_PAIR; |
367 | } |
374 | } |
368 | |
375 | |
369 | #define car(p) ((p)->object.cons.car + 0) |
376 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
370 | #define cdr(p) ((p)->object.cons.cdr + 0) |
377 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
371 | |
378 | |
372 | static pointer caar (pointer p) { return car (car (p)); } |
379 | static pointer caar (pointer p) { return car (car (p)); } |
373 | static pointer cadr (pointer p) { return car (cdr (p)); } |
380 | static pointer cadr (pointer p) { return car (cdr (p)); } |
374 | static pointer cdar (pointer p) { return cdr (car (p)); } |
381 | static pointer cdar (pointer p) { return cdr (car (p)); } |
375 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
382 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
… | |
… | |
379 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
386 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
380 | |
387 | |
381 | INTERFACE void |
388 | INTERFACE void |
382 | set_car (pointer p, pointer q) |
389 | set_car (pointer p, pointer q) |
383 | { |
390 | { |
384 | p->object.cons.car = q; |
391 | CELL(p)->object.cons.car = CELL (q); |
385 | } |
392 | } |
386 | |
393 | |
387 | INTERFACE void |
394 | INTERFACE void |
388 | set_cdr (pointer p, pointer q) |
395 | set_cdr (pointer p, pointer q) |
389 | { |
396 | { |
390 | p->object.cons.cdr = q; |
397 | CELL(p)->object.cons.cdr = CELL (q); |
391 | } |
398 | } |
392 | |
399 | |
393 | INTERFACE pointer |
400 | INTERFACE pointer |
394 | pair_car (pointer p) |
401 | pair_car (pointer p) |
395 | { |
402 | { |
… | |
… | |
525 | proper list: length |
532 | proper list: length |
526 | circular list: -1 |
533 | circular list: -1 |
527 | not even a pair: -2 |
534 | not even a pair: -2 |
528 | dotted list: -2 minus length before dot |
535 | dotted list: -2 minus length before dot |
529 | */ |
536 | */ |
530 | INTERFACE int |
537 | ecb_hot INTERFACE int |
531 | list_length (SCHEME_P_ pointer a) |
538 | list_length (SCHEME_P_ pointer a) |
532 | { |
539 | { |
533 | int i = 0; |
540 | int i = 0; |
534 | pointer slow, fast; |
541 | pointer slow, fast; |
535 | |
542 | |
… | |
… | |
574 | { |
581 | { |
575 | return list_length (SCHEME_A_ a) >= 0; |
582 | return list_length (SCHEME_A_ a) >= 0; |
576 | } |
583 | } |
577 | |
584 | |
578 | #if USE_CHAR_CLASSIFIERS |
585 | #if USE_CHAR_CLASSIFIERS |
|
|
586 | |
579 | ecb_inline int |
587 | ecb_inline int |
580 | Cisalpha (int c) |
588 | Cisalpha (int c) |
581 | { |
589 | { |
582 | return isascii (c) && isalpha (c); |
590 | return isascii (c) && isalpha (c); |
583 | } |
591 | } |
… | |
… | |
641 | "gs", |
649 | "gs", |
642 | "rs", |
650 | "rs", |
643 | "us" |
651 | "us" |
644 | }; |
652 | }; |
645 | |
653 | |
646 | static int |
654 | ecb_cold static int |
647 | is_ascii_name (const char *name, int *pc) |
655 | is_ascii_name (const char *name, int *pc) |
648 | { |
656 | { |
649 | int i; |
657 | int i; |
650 | |
658 | |
651 | for (i = 0; i < 32; i++) |
659 | for (i = 0; i < 32; i++) |
… | |
… | |
670 | |
678 | |
671 | static int file_push (SCHEME_P_ const char *fname); |
679 | static int file_push (SCHEME_P_ const char *fname); |
672 | static void file_pop (SCHEME_P); |
680 | static void file_pop (SCHEME_P); |
673 | static int file_interactive (SCHEME_P); |
681 | static int file_interactive (SCHEME_P); |
674 | ecb_inline int is_one_of (const char *s, int c); |
682 | ecb_inline int is_one_of (const char *s, int c); |
675 | static int alloc_cellseg (SCHEME_P_ int n); |
683 | static int alloc_cellseg (SCHEME_P); |
676 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
684 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
677 | static void finalize_cell (SCHEME_P_ pointer a); |
685 | static void finalize_cell (SCHEME_P_ pointer a); |
678 | static int count_consecutive_cells (pointer x, int needed); |
|
|
679 | 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); |
680 | static pointer mk_number (SCHEME_P_ const num n); |
687 | static pointer mk_number (SCHEME_P_ const num n); |
681 | 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); |
682 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
689 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
683 | static pointer mk_atom (SCHEME_P_ char *q); |
690 | static pointer mk_atom (SCHEME_P_ char *q); |
684 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
691 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
685 | |
692 | |
|
|
693 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
694 | |
686 | #if USE_PORTS |
695 | #if USE_PORTS |
687 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
688 | 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); |
689 | static pointer port_from_file (SCHEME_P_ int, int prop); |
697 | static pointer port_from_file (SCHEME_P_ int, int prop); |
690 | 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); |
691 | 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); |
692 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
700 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
693 | 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); |
694 | static void port_close (SCHEME_P_ pointer p, int flag); |
702 | static void port_close (SCHEME_P_ pointer p, int flag); |
695 | #endif |
703 | #endif |
|
|
704 | |
696 | static void mark (pointer a); |
705 | static void mark (pointer a); |
697 | static void gc (SCHEME_P_ pointer a, pointer b); |
706 | static void gc (SCHEME_P_ pointer a, pointer b); |
698 | static int basic_inchar (port *pt); |
707 | static int basic_inchar (port *pt); |
699 | static int inchar (SCHEME_P); |
708 | static int inchar (SCHEME_P); |
700 | static void backchar (SCHEME_P_ int c); |
709 | static void backchar (SCHEME_P_ int c); |
… | |
… | |
874 | } |
883 | } |
875 | |
884 | |
876 | return ret; |
885 | return ret; |
877 | } |
886 | } |
878 | |
887 | |
879 | #if USE_MATH |
|
|
880 | |
|
|
881 | /* Round to nearest. Round to even if midway */ |
|
|
882 | static double |
|
|
883 | round_per_R5RS (double x) |
|
|
884 | { |
|
|
885 | double fl = floor (x); |
|
|
886 | double ce = ceil (x); |
|
|
887 | double dfl = x - fl; |
|
|
888 | double dce = ce - x; |
|
|
889 | |
|
|
890 | if (dfl > dce) |
|
|
891 | return ce; |
|
|
892 | else if (dfl < dce) |
|
|
893 | return fl; |
|
|
894 | else |
|
|
895 | { |
|
|
896 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
|
|
897 | return fl; |
|
|
898 | else |
|
|
899 | return ce; |
|
|
900 | } |
|
|
901 | } |
|
|
902 | #endif |
|
|
903 | |
|
|
904 | static int |
888 | static int |
905 | is_zero_rvalue (RVALUE x) |
889 | is_zero_rvalue (RVALUE x) |
906 | { |
890 | { |
907 | return x == 0; |
891 | return x == 0; |
908 | #if 0 |
892 | #if 0 |
… | |
… | |
913 | #endif |
897 | #endif |
914 | #endif |
898 | #endif |
915 | } |
899 | } |
916 | |
900 | |
917 | /* allocate new cell segment */ |
901 | /* allocate new cell segment */ |
918 | static int |
902 | ecb_cold static int |
919 | alloc_cellseg (SCHEME_P_ int n) |
903 | alloc_cellseg (SCHEME_P) |
920 | { |
904 | { |
921 | pointer newp; |
905 | struct cell *newp; |
922 | pointer last; |
906 | struct cell *last; |
923 | pointer p; |
907 | struct cell *p; |
924 | char *cp; |
908 | char *cp; |
925 | long i; |
909 | long i; |
926 | int k; |
910 | int k; |
927 | |
911 | |
928 | static int segsize = CELL_SEGSIZE >> 1; |
912 | static int segsize = CELL_SEGSIZE >> 1; |
929 | segsize <<= 1; |
913 | segsize <<= 1; |
930 | |
914 | |
931 | for (k = 0; k < n; k++) |
|
|
932 | { |
|
|
933 | if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1) |
|
|
934 | return k; |
|
|
935 | |
|
|
936 | cp = malloc (segsize * sizeof (struct cell)); |
915 | cp = malloc (segsize * sizeof (struct cell)); |
937 | |
916 | |
938 | if (!cp && USE_ERROR_CHECKING) |
917 | if (!cp && USE_ERROR_CHECKING) |
939 | return k; |
918 | return k; |
940 | |
919 | |
941 | i = ++SCHEME_V->last_cell_seg; |
920 | i = ++SCHEME_V->last_cell_seg; |
942 | SCHEME_V->alloc_seg[i] = cp; |
921 | SCHEME_V->alloc_seg[i] = cp; |
943 | |
922 | |
944 | newp = (pointer)cp; |
923 | newp = (struct cell *)cp; |
945 | SCHEME_V->cell_seg[i] = newp; |
924 | SCHEME_V->cell_seg[i] = newp; |
946 | SCHEME_V->cell_segsize[i] = segsize; |
925 | SCHEME_V->cell_segsize[i] = segsize; |
947 | SCHEME_V->fcells += segsize; |
926 | SCHEME_V->fcells += segsize; |
948 | last = newp + segsize - 1; |
927 | last = newp + segsize - 1; |
949 | |
928 | |
950 | for (p = newp; p <= last; p++) |
929 | for (p = newp; p <= last; p++) |
951 | { |
930 | { |
|
|
931 | pointer cp = POINTER (p); |
952 | set_typeflag (p, T_PAIR); |
932 | set_typeflag (cp, T_PAIR); |
953 | set_car (p, NIL); |
933 | set_car (cp, NIL); |
954 | set_cdr (p, p + 1); |
934 | set_cdr (cp, POINTER (p + 1)); |
955 | } |
935 | } |
956 | |
936 | |
957 | set_cdr (last, SCHEME_V->free_cell); |
937 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
958 | SCHEME_V->free_cell = newp; |
938 | SCHEME_V->free_cell = POINTER (newp); |
959 | } |
|
|
960 | |
939 | |
961 | return n; |
940 | return 1; |
962 | } |
941 | } |
963 | |
942 | |
964 | /* get new cell. parameter a, b is marked by gc. */ |
943 | /* get new cell. parameter a, b is marked by gc. */ |
965 | ecb_inline pointer |
944 | ecb_inline pointer |
966 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
945 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
… | |
… | |
970 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
949 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
971 | return S_SINK; |
950 | return S_SINK; |
972 | |
951 | |
973 | if (SCHEME_V->free_cell == NIL) |
952 | if (SCHEME_V->free_cell == NIL) |
974 | { |
953 | { |
975 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1; |
954 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 2; |
976 | |
955 | |
977 | gc (SCHEME_A_ a, b); |
956 | gc (SCHEME_A_ a, b); |
978 | |
957 | |
979 | 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) |
980 | { |
959 | { |
981 | /* 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 */ |
982 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
961 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
983 | { |
962 | { |
984 | #if USE_ERROR_CHECKING |
963 | #if USE_ERROR_CHECKING |
985 | SCHEME_V->no_memory = 1; |
964 | SCHEME_V->no_memory = 1; |
986 | return S_SINK; |
965 | return S_SINK; |
987 | #endif |
966 | #endif |
… | |
… | |
999 | } |
978 | } |
1000 | } |
979 | } |
1001 | |
980 | |
1002 | /* To retain recent allocs before interpreter knows about them - |
981 | /* To retain recent allocs before interpreter knows about them - |
1003 | Tehom */ |
982 | Tehom */ |
1004 | |
983 | ecb_hot static void |
1005 | static void |
|
|
1006 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
984 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
1007 | { |
985 | { |
1008 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
986 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1009 | |
987 | |
1010 | set_typeflag (holder, T_PAIR); |
988 | set_typeflag (holder, T_PAIR); |
… | |
… | |
1012 | set_car (holder, recent); |
990 | set_car (holder, recent); |
1013 | set_cdr (holder, car (S_SINK)); |
991 | set_cdr (holder, car (S_SINK)); |
1014 | set_car (S_SINK, holder); |
992 | set_car (S_SINK, holder); |
1015 | } |
993 | } |
1016 | |
994 | |
1017 | static pointer |
995 | ecb_hot static pointer |
1018 | get_cell (SCHEME_P_ pointer a, pointer b) |
996 | get_cell (SCHEME_P_ pointer a, pointer b) |
1019 | { |
997 | { |
1020 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
998 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
1021 | |
999 | |
1022 | /* 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 |
… | |
… | |
1040 | return S_SINK; |
1018 | return S_SINK; |
1041 | |
1019 | |
1042 | /* Record it as a vector so that gc understands it. */ |
1020 | /* Record it as a vector so that gc understands it. */ |
1043 | set_typeflag (v, T_VECTOR | T_ATOM); |
1021 | set_typeflag (v, T_VECTOR | T_ATOM); |
1044 | |
1022 | |
1045 | v->object.vector.vvalue = e; |
1023 | CELL(v)->object.vector.vvalue = e; |
1046 | v->object.vector.length = len; |
1024 | CELL(v)->object.vector.length = len; |
1047 | fill_vector (v, 0, init); |
1025 | fill_vector (v, 0, init); |
1048 | push_recent_alloc (SCHEME_A_ v, NIL); |
1026 | push_recent_alloc (SCHEME_A_ v, NIL); |
1049 | |
1027 | |
1050 | return v; |
1028 | return v; |
1051 | } |
1029 | } |
… | |
… | |
1060 | static void |
1038 | static void |
1061 | check_cell_alloced (pointer p, int expect_alloced) |
1039 | check_cell_alloced (pointer p, int expect_alloced) |
1062 | { |
1040 | { |
1063 | /* 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. */ |
1064 | if (typeflag (p) & !expect_alloced) |
1042 | if (typeflag (p) & !expect_alloced) |
1065 | xwrstr ("Cell is already allocated!\n"); |
1043 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1066 | |
1044 | |
1067 | if (!(typeflag (p)) & expect_alloced) |
1045 | if (!(typeflag (p)) & expect_alloced) |
1068 | xwrstr ("Cell is not allocated!\n"); |
1046 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1069 | } |
1047 | } |
1070 | |
1048 | |
1071 | static void |
1049 | static void |
1072 | check_range_alloced (pointer p, int n, int expect_alloced) |
1050 | check_range_alloced (pointer p, int n, int expect_alloced) |
1073 | { |
1051 | { |
… | |
… | |
1079 | #endif |
1057 | #endif |
1080 | |
1058 | |
1081 | /* Medium level cell allocation */ |
1059 | /* Medium level cell allocation */ |
1082 | |
1060 | |
1083 | /* get new cons cell */ |
1061 | /* get new cons cell */ |
1084 | pointer |
1062 | ecb_hot static pointer |
1085 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1063 | xcons (SCHEME_P_ pointer a, pointer b) |
1086 | { |
1064 | { |
1087 | pointer x = get_cell (SCHEME_A_ a, b); |
1065 | pointer x = get_cell (SCHEME_A_ a, b); |
1088 | |
1066 | |
1089 | set_typeflag (x, T_PAIR); |
1067 | set_typeflag (x, T_PAIR); |
1090 | |
|
|
1091 | if (immutable) |
|
|
1092 | setimmutable (x); |
|
|
1093 | |
1068 | |
1094 | set_car (x, a); |
1069 | set_car (x, a); |
1095 | set_cdr (x, b); |
1070 | set_cdr (x, b); |
1096 | |
1071 | |
1097 | return x; |
1072 | return x; |
1098 | } |
1073 | } |
1099 | |
1074 | |
1100 | static pointer |
1075 | ecb_hot static pointer |
|
|
1076 | ximmutable_cons (SCHEME_P_ pointer a, pointer b) |
|
|
1077 | { |
|
|
1078 | pointer x = xcons (SCHEME_A_ a, b); |
|
|
1079 | setimmutable (x); |
|
|
1080 | return x; |
|
|
1081 | } |
|
|
1082 | |
|
|
1083 | #define cons(a,b) xcons (SCHEME_A_ a, b) |
|
|
1084 | #define immutable_cons(a,b) ximmutable_cons (SCHEME_A_ a, b) |
|
|
1085 | |
|
|
1086 | ecb_cold static pointer |
1101 | generate_symbol (SCHEME_P_ const char *name) |
1087 | generate_symbol (SCHEME_P_ const char *name) |
1102 | { |
1088 | { |
1103 | pointer x = mk_string (SCHEME_A_ name); |
1089 | pointer x = mk_string (SCHEME_A_ name); |
1104 | setimmutable (x); |
1090 | setimmutable (x); |
1105 | set_typeflag (x, T_SYMBOL | T_ATOM); |
1091 | set_typeflag (x, T_SYMBOL | T_ATOM); |
… | |
… | |
1111 | #ifndef USE_OBJECT_LIST |
1097 | #ifndef USE_OBJECT_LIST |
1112 | |
1098 | |
1113 | static int |
1099 | static int |
1114 | hash_fn (const char *key, int table_size) |
1100 | hash_fn (const char *key, int table_size) |
1115 | { |
1101 | { |
1116 | const unsigned char *p = key; |
1102 | const unsigned char *p = (unsigned char *)key; |
1117 | uint32_t hash = 2166136261; |
1103 | uint32_t hash = 2166136261; |
1118 | |
1104 | |
1119 | while (*p) |
1105 | while (*p) |
1120 | hash = (hash ^ *p++) * 16777619; |
1106 | hash = (hash ^ *p++) * 16777619; |
1121 | |
1107 | |
1122 | return hash % table_size; |
1108 | return hash % table_size; |
1123 | } |
1109 | } |
1124 | |
1110 | |
1125 | static pointer |
1111 | ecb_cold static pointer |
1126 | oblist_initial_value (SCHEME_P) |
1112 | oblist_initial_value (SCHEME_P) |
1127 | { |
1113 | { |
1128 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1114 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1129 | } |
1115 | } |
1130 | |
1116 | |
1131 | /* returns the new symbol */ |
1117 | /* returns the new symbol */ |
1132 | static pointer |
1118 | ecb_cold static pointer |
1133 | oblist_add_by_name (SCHEME_P_ const char *name) |
1119 | oblist_add_by_name (SCHEME_P_ const char *name) |
1134 | { |
1120 | { |
1135 | pointer x = generate_symbol (SCHEME_A_ name); |
1121 | pointer x = generate_symbol (SCHEME_A_ name); |
1136 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1122 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1137 | 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))); |
1138 | return x; |
1124 | return x; |
1139 | } |
1125 | } |
1140 | |
1126 | |
1141 | ecb_inline pointer |
1127 | ecb_cold static pointer |
1142 | oblist_find_by_name (SCHEME_P_ const char *name) |
1128 | oblist_find_by_name (SCHEME_P_ const char *name) |
1143 | { |
1129 | { |
1144 | int location; |
1130 | int location; |
1145 | pointer x; |
1131 | pointer x; |
1146 | char *s; |
1132 | char *s; |
… | |
… | |
1157 | } |
1143 | } |
1158 | |
1144 | |
1159 | return NIL; |
1145 | return NIL; |
1160 | } |
1146 | } |
1161 | |
1147 | |
1162 | static pointer |
1148 | ecb_cold static pointer |
1163 | oblist_all_symbols (SCHEME_P) |
1149 | oblist_all_symbols (SCHEME_P) |
1164 | { |
1150 | { |
1165 | int i; |
1151 | int i; |
1166 | pointer x; |
1152 | pointer x; |
1167 | pointer ob_list = NIL; |
1153 | pointer ob_list = NIL; |
… | |
… | |
1173 | return ob_list; |
1159 | return ob_list; |
1174 | } |
1160 | } |
1175 | |
1161 | |
1176 | #else |
1162 | #else |
1177 | |
1163 | |
1178 | static pointer |
1164 | ecb_cold static pointer |
1179 | oblist_initial_value (SCHEME_P) |
1165 | oblist_initial_value (SCHEME_P) |
1180 | { |
1166 | { |
1181 | return NIL; |
1167 | return NIL; |
1182 | } |
1168 | } |
1183 | |
1169 | |
1184 | ecb_inline pointer |
1170 | ecb_cold static pointer |
1185 | oblist_find_by_name (SCHEME_P_ const char *name) |
1171 | oblist_find_by_name (SCHEME_P_ const char *name) |
1186 | { |
1172 | { |
1187 | pointer x; |
1173 | pointer x; |
1188 | char *s; |
1174 | char *s; |
1189 | |
1175 | |
… | |
… | |
1198 | |
1184 | |
1199 | return NIL; |
1185 | return NIL; |
1200 | } |
1186 | } |
1201 | |
1187 | |
1202 | /* returns the new symbol */ |
1188 | /* returns the new symbol */ |
1203 | static pointer |
1189 | ecb_cold static pointer |
1204 | oblist_add_by_name (SCHEME_P_ const char *name) |
1190 | oblist_add_by_name (SCHEME_P_ const char *name) |
1205 | { |
1191 | { |
1206 | pointer x = generate_symbol (SCHEME_A_ name); |
1192 | pointer x = generate_symbol (SCHEME_A_ name); |
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 |
… | |
… | |
1402 | x = oblist_add_by_name (SCHEME_A_ name); |
1386 | x = oblist_add_by_name (SCHEME_A_ name); |
1403 | |
1387 | |
1404 | return x; |
1388 | return x; |
1405 | } |
1389 | } |
1406 | |
1390 | |
1407 | INTERFACE pointer |
1391 | ecb_cold INTERFACE pointer |
1408 | gensym (SCHEME_P) |
1392 | gensym (SCHEME_P) |
1409 | { |
1393 | { |
1410 | pointer x; |
1394 | pointer x; |
1411 | char name[40] = "gensym-"; |
1395 | char name[40] = "gensym-"; |
1412 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1396 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
… | |
… | |
1419 | { |
1403 | { |
1420 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1404 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1421 | } |
1405 | } |
1422 | |
1406 | |
1423 | /* make symbol or number atom from string */ |
1407 | /* make symbol or number atom from string */ |
1424 | static pointer |
1408 | ecb_cold static pointer |
1425 | mk_atom (SCHEME_P_ char *q) |
1409 | mk_atom (SCHEME_P_ char *q) |
1426 | { |
1410 | { |
1427 | char c, *p; |
1411 | char c, *p; |
1428 | int has_dec_point = 0; |
1412 | int has_dec_point = 0; |
1429 | int has_fp_exp = 0; |
1413 | int has_fp_exp = 0; |
… | |
… | |
1500 | |
1484 | |
1501 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1485 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1502 | } |
1486 | } |
1503 | |
1487 | |
1504 | /* make constant */ |
1488 | /* make constant */ |
1505 | static pointer |
1489 | ecb_cold static pointer |
1506 | mk_sharp_const (SCHEME_P_ char *name) |
1490 | mk_sharp_const (SCHEME_P_ char *name) |
1507 | { |
1491 | { |
1508 | if (!strcmp (name, "t")) |
1492 | if (!strcmp (name, "t")) |
1509 | return S_T; |
1493 | return S_T; |
1510 | else if (!strcmp (name, "f")) |
1494 | else if (!strcmp (name, "f")) |
1511 | return S_F; |
1495 | return S_F; |
1512 | else if (*name == '\\') /* #\w (character) */ |
1496 | else if (*name == '\\') /* #\w (character) */ |
1513 | { |
1497 | { |
1514 | int c; |
1498 | int c; |
1515 | |
1499 | |
|
|
1500 | // TODO: optimise |
1516 | if (stricmp (name + 1, "space") == 0) |
1501 | if (stricmp (name + 1, "space") == 0) |
1517 | c = ' '; |
1502 | c = ' '; |
1518 | else if (stricmp (name + 1, "newline") == 0) |
1503 | else if (stricmp (name + 1, "newline") == 0) |
1519 | c = '\n'; |
1504 | c = '\n'; |
1520 | else if (stricmp (name + 1, "return") == 0) |
1505 | else if (stricmp (name + 1, "return") == 0) |
1521 | c = '\r'; |
1506 | c = '\r'; |
1522 | else if (stricmp (name + 1, "tab") == 0) |
1507 | else if (stricmp (name + 1, "tab") == 0) |
1523 | 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; |
1524 | else if (name[1] == 'x' && name[2] != 0) |
1519 | else if (name[1] == 'x' && name[2] != 0) |
1525 | { |
1520 | { |
1526 | long c1 = strtol (name + 2, 0, 16); |
1521 | long c1 = strtol (name + 2, 0, 16); |
1527 | |
1522 | |
1528 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1523 | if (0 <= c1 && c1 <= UCHAR_MAX) |
… | |
… | |
1553 | return NIL; |
1548 | return NIL; |
1554 | } |
1549 | } |
1555 | } |
1550 | } |
1556 | |
1551 | |
1557 | /* ========== 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 | } |
1558 | |
1572 | |
1559 | /*-- |
1573 | /*-- |
1560 | * 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, |
1561 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1575 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1562 | * for marking. |
1576 | * for marking. |
1563 | * |
1577 | * |
1564 | * The exception is vectors - vectors are currently marked recursively, |
1578 | * The exception is vectors - vectors are currently marked recursively, |
1565 | * 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 |
1566 | * word of context in the vector |
1580 | * word of context in the vector |
1567 | */ |
1581 | */ |
1568 | static void |
1582 | ecb_hot static void |
1569 | mark (pointer a) |
1583 | mark (pointer a) |
1570 | { |
1584 | { |
1571 | pointer t, q, p; |
1585 | pointer t, q, p; |
1572 | |
1586 | |
1573 | t = 0; |
1587 | t = 0; |
… | |
… | |
1630 | p = q; |
1644 | p = q; |
1631 | goto E6; |
1645 | goto E6; |
1632 | } |
1646 | } |
1633 | } |
1647 | } |
1634 | |
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 | |
1635 | /* garbage collection. parameter a, b is marked. */ |
1691 | /* garbage collection. parameter a, b is marked. */ |
1636 | static void |
1692 | ecb_cold static void |
1637 | gc (SCHEME_P_ pointer a, pointer b) |
1693 | gc (SCHEME_P_ pointer a, pointer b) |
1638 | { |
1694 | { |
1639 | pointer p; |
|
|
1640 | int i; |
1695 | int i; |
1641 | |
1696 | |
1642 | if (SCHEME_V->gc_verbose) |
1697 | if (SCHEME_V->gc_verbose) |
1643 | putstr (SCHEME_A_ "gc..."); |
1698 | putstr (SCHEME_A_ "gc..."); |
1644 | |
1699 | |
… | |
… | |
1677 | clrmark (NIL); |
1732 | clrmark (NIL); |
1678 | SCHEME_V->fcells = 0; |
1733 | SCHEME_V->fcells = 0; |
1679 | SCHEME_V->free_cell = NIL; |
1734 | SCHEME_V->free_cell = NIL; |
1680 | |
1735 | |
1681 | if (SCHEME_V->gc_verbose) |
1736 | if (SCHEME_V->gc_verbose) |
1682 | xwrstr ("freeing..."); |
1737 | putstr (SCHEME_A_ "freeing..."); |
1683 | |
1738 | |
1684 | uint32_t total = 0; |
1739 | gc_free (SCHEME_A); |
1685 | |
|
|
1686 | /* Here we scan the cells to build the free-list. */ |
|
|
1687 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1688 | { |
|
|
1689 | pointer end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1690 | total += SCHEME_V->cell_segsize [i]; |
|
|
1691 | |
|
|
1692 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
|
|
1693 | { |
|
|
1694 | if (is_mark (p)) |
|
|
1695 | clrmark (p); |
|
|
1696 | else |
|
|
1697 | { |
|
|
1698 | /* reclaim cell */ |
|
|
1699 | if (typeflag (p) != T_PAIR) |
|
|
1700 | { |
|
|
1701 | finalize_cell (SCHEME_A_ p); |
|
|
1702 | set_typeflag (p, T_PAIR); |
|
|
1703 | set_car (p, NIL); |
|
|
1704 | } |
|
|
1705 | |
|
|
1706 | ++SCHEME_V->fcells; |
|
|
1707 | set_cdr (p, SCHEME_V->free_cell); |
|
|
1708 | SCHEME_V->free_cell = p; |
|
|
1709 | } |
|
|
1710 | } |
|
|
1711 | } |
|
|
1712 | |
|
|
1713 | if (SCHEME_V->gc_verbose) |
|
|
1714 | { |
|
|
1715 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" out of "); xwrnum (total); xwrstr (" cells were recovered.\n"); |
|
|
1716 | } |
|
|
1717 | } |
|
|
1718 | |
|
|
1719 | static void |
|
|
1720 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1721 | { |
|
|
1722 | /* TODO, fast bitmap check? */ |
|
|
1723 | if (is_string (a) || is_symbol (a)) |
|
|
1724 | free (strvalue (a)); |
|
|
1725 | else if (is_vector (a)) |
|
|
1726 | free (vecvalue (a)); |
|
|
1727 | #if USE_PORTS |
|
|
1728 | else if (is_port (a)) |
|
|
1729 | { |
|
|
1730 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
|
|
1731 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1732 | |
|
|
1733 | free (a->object.port); |
|
|
1734 | } |
|
|
1735 | #endif |
|
|
1736 | } |
1740 | } |
1737 | |
1741 | |
1738 | /* ========== Routines for Reading ========== */ |
1742 | /* ========== Routines for Reading ========== */ |
1739 | |
1743 | |
1740 | static int |
1744 | ecb_cold static int |
1741 | file_push (SCHEME_P_ const char *fname) |
1745 | file_push (SCHEME_P_ const char *fname) |
1742 | { |
1746 | { |
1743 | #if USE_PORTS |
|
|
1744 | int fin; |
1747 | int fin; |
1745 | |
1748 | |
1746 | if (SCHEME_V->file_i == MAXFIL - 1) |
1749 | if (SCHEME_V->file_i == MAXFIL - 1) |
1747 | return 0; |
1750 | return 0; |
1748 | |
1751 | |
… | |
… | |
1754 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1757 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1755 | 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; |
1756 | 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; |
1757 | 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; |
1758 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1761 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1759 | 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); |
1760 | |
1763 | |
1761 | #if SHOW_ERROR_LINE |
1764 | #if SHOW_ERROR_LINE |
1762 | 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; |
1763 | |
1766 | |
1764 | if (fname) |
1767 | if (fname) |
1765 | 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); |
1766 | #endif |
1769 | #endif |
1767 | } |
1770 | } |
1768 | |
1771 | |
1769 | return fin >= 0; |
1772 | return fin >= 0; |
1770 | |
|
|
1771 | #else |
|
|
1772 | return 1; |
|
|
1773 | #endif |
|
|
1774 | } |
1773 | } |
1775 | |
1774 | |
1776 | static void |
1775 | ecb_cold static void |
1777 | file_pop (SCHEME_P) |
1776 | file_pop (SCHEME_P) |
1778 | { |
1777 | { |
1779 | if (SCHEME_V->file_i != 0) |
1778 | if (SCHEME_V->file_i != 0) |
1780 | { |
1779 | { |
1781 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1780 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1782 | #if USE_PORTS |
1781 | #if USE_PORTS |
1783 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1782 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1784 | #endif |
1783 | #endif |
1785 | SCHEME_V->file_i--; |
1784 | SCHEME_V->file_i--; |
1786 | 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); |
1787 | } |
1786 | } |
1788 | } |
1787 | } |
1789 | |
1788 | |
1790 | static int |
1789 | ecb_cold static int |
1791 | file_interactive (SCHEME_P) |
1790 | file_interactive (SCHEME_P) |
1792 | { |
1791 | { |
1793 | #if USE_PORTS |
1792 | #if USE_PORTS |
1794 | return SCHEME_V->file_i == 0 |
1793 | return SCHEME_V->file_i == 0 |
1795 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1794 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1796 | && (SCHEME_V->inport->object.port->kind & port_file); |
1795 | && (port (SCHEME_V->inport)->kind & port_file); |
1797 | #else |
1796 | #else |
1798 | return 0; |
1797 | return 0; |
1799 | #endif |
1798 | #endif |
1800 | } |
1799 | } |
1801 | |
1800 | |
1802 | #if USE_PORTS |
1801 | #if USE_PORTS |
1803 | static port * |
1802 | ecb_cold static port * |
1804 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1803 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1805 | { |
1804 | { |
1806 | int fd; |
1805 | int fd; |
1807 | int flags; |
1806 | int flags; |
1808 | char *rw; |
1807 | char *rw; |
… | |
… | |
1831 | # endif |
1830 | # endif |
1832 | |
1831 | |
1833 | return pt; |
1832 | return pt; |
1834 | } |
1833 | } |
1835 | |
1834 | |
1836 | static pointer |
1835 | ecb_cold static pointer |
1837 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1836 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1838 | { |
1837 | { |
1839 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1838 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1840 | |
1839 | |
1841 | if (!pt && USE_ERROR_CHECKING) |
1840 | if (!pt && USE_ERROR_CHECKING) |
1842 | return NIL; |
1841 | return NIL; |
1843 | |
1842 | |
1844 | return mk_port (SCHEME_A_ pt); |
1843 | return mk_port (SCHEME_A_ pt); |
1845 | } |
1844 | } |
1846 | |
1845 | |
1847 | static port * |
1846 | ecb_cold static port * |
1848 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1847 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1849 | { |
1848 | { |
1850 | port *pt = malloc (sizeof *pt); |
1849 | port *pt = malloc (sizeof *pt); |
1851 | |
1850 | |
1852 | if (!pt && USE_ERROR_CHECKING) |
1851 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1857 | pt->rep.stdio.file = f; |
1856 | pt->rep.stdio.file = f; |
1858 | pt->rep.stdio.closeit = 0; |
1857 | pt->rep.stdio.closeit = 0; |
1859 | return pt; |
1858 | return pt; |
1860 | } |
1859 | } |
1861 | |
1860 | |
1862 | static pointer |
1861 | ecb_cold static pointer |
1863 | port_from_file (SCHEME_P_ int f, int prop) |
1862 | port_from_file (SCHEME_P_ int f, int prop) |
1864 | { |
1863 | { |
1865 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1864 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1866 | |
1865 | |
1867 | if (!pt && USE_ERROR_CHECKING) |
1866 | if (!pt && USE_ERROR_CHECKING) |
1868 | return NIL; |
1867 | return NIL; |
1869 | |
1868 | |
1870 | return mk_port (SCHEME_A_ pt); |
1869 | return mk_port (SCHEME_A_ pt); |
1871 | } |
1870 | } |
1872 | |
1871 | |
1873 | static port * |
1872 | ecb_cold static port * |
1874 | 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) |
1875 | { |
1874 | { |
1876 | port *pt = malloc (sizeof (port)); |
1875 | port *pt = malloc (sizeof (port)); |
1877 | |
1876 | |
1878 | if (!pt && USE_ERROR_CHECKING) |
1877 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1884 | pt->rep.string.curr = start; |
1883 | pt->rep.string.curr = start; |
1885 | pt->rep.string.past_the_end = past_the_end; |
1884 | pt->rep.string.past_the_end = past_the_end; |
1886 | return pt; |
1885 | return pt; |
1887 | } |
1886 | } |
1888 | |
1887 | |
1889 | static pointer |
1888 | ecb_cold static pointer |
1890 | 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) |
1891 | { |
1890 | { |
1892 | 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); |
1893 | |
1892 | |
1894 | if (!pt && USE_ERROR_CHECKING) |
1893 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1897 | return mk_port (SCHEME_A_ pt); |
1896 | return mk_port (SCHEME_A_ pt); |
1898 | } |
1897 | } |
1899 | |
1898 | |
1900 | # define BLOCK_SIZE 256 |
1899 | # define BLOCK_SIZE 256 |
1901 | |
1900 | |
1902 | static port * |
1901 | ecb_cold static port * |
1903 | port_rep_from_scratch (SCHEME_P) |
1902 | port_rep_from_scratch (SCHEME_P) |
1904 | { |
1903 | { |
1905 | char *start; |
1904 | char *start; |
1906 | port *pt = malloc (sizeof (port)); |
1905 | port *pt = malloc (sizeof (port)); |
1907 | |
1906 | |
… | |
… | |
1921 | pt->rep.string.curr = start; |
1920 | pt->rep.string.curr = start; |
1922 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1921 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1923 | return pt; |
1922 | return pt; |
1924 | } |
1923 | } |
1925 | |
1924 | |
1926 | static pointer |
1925 | ecb_cold static pointer |
1927 | port_from_scratch (SCHEME_P) |
1926 | port_from_scratch (SCHEME_P) |
1928 | { |
1927 | { |
1929 | port *pt = port_rep_from_scratch (SCHEME_A); |
1928 | port *pt = port_rep_from_scratch (SCHEME_A); |
1930 | |
1929 | |
1931 | if (!pt && USE_ERROR_CHECKING) |
1930 | if (!pt && USE_ERROR_CHECKING) |
1932 | return NIL; |
1931 | return NIL; |
1933 | |
1932 | |
1934 | return mk_port (SCHEME_A_ pt); |
1933 | return mk_port (SCHEME_A_ pt); |
1935 | } |
1934 | } |
1936 | |
1935 | |
1937 | static void |
1936 | ecb_cold static void |
1938 | port_close (SCHEME_P_ pointer p, int flag) |
1937 | port_close (SCHEME_P_ pointer p, int flag) |
1939 | { |
1938 | { |
1940 | port *pt = p->object.port; |
1939 | port *pt = port (p); |
1941 | |
1940 | |
1942 | pt->kind &= ~flag; |
1941 | pt->kind &= ~flag; |
1943 | |
1942 | |
1944 | if ((pt->kind & (port_input | port_output)) == 0) |
1943 | if ((pt->kind & (port_input | port_output)) == 0) |
1945 | { |
1944 | { |
… | |
… | |
1962 | } |
1961 | } |
1963 | } |
1962 | } |
1964 | #endif |
1963 | #endif |
1965 | |
1964 | |
1966 | /* get new character from input file */ |
1965 | /* get new character from input file */ |
1967 | static int |
1966 | ecb_cold static int |
1968 | inchar (SCHEME_P) |
1967 | inchar (SCHEME_P) |
1969 | { |
1968 | { |
1970 | int c; |
1969 | int c; |
1971 | port *pt; |
1970 | port *pt = port (SCHEME_V->inport); |
1972 | |
|
|
1973 | pt = SCHEME_V->inport->object.port; |
|
|
1974 | |
1971 | |
1975 | if (pt->kind & port_saw_EOF) |
1972 | if (pt->kind & port_saw_EOF) |
1976 | return EOF; |
1973 | return EOF; |
1977 | |
1974 | |
1978 | c = basic_inchar (pt); |
1975 | c = basic_inchar (pt); |
… | |
… | |
1988 | } |
1985 | } |
1989 | |
1986 | |
1990 | return c; |
1987 | return c; |
1991 | } |
1988 | } |
1992 | |
1989 | |
1993 | static int ungot = -1; |
1990 | ecb_cold static int |
1994 | |
|
|
1995 | static int |
|
|
1996 | basic_inchar (port *pt) |
1991 | basic_inchar (port *pt) |
1997 | { |
1992 | { |
1998 | #if USE_PORTS |
|
|
1999 | if (pt->unget != -1) |
1993 | if (pt->unget != -1) |
2000 | { |
1994 | { |
2001 | int r = pt->unget; |
1995 | int r = pt->unget; |
2002 | pt->unget = -1; |
1996 | pt->unget = -1; |
2003 | return r; |
1997 | return r; |
2004 | } |
1998 | } |
2005 | |
1999 | |
|
|
2000 | #if USE_PORTS |
2006 | if (pt->kind & port_file) |
2001 | if (pt->kind & port_file) |
2007 | { |
2002 | { |
2008 | char c; |
2003 | char c; |
2009 | |
2004 | |
2010 | if (!read (pt->rep.stdio.file, &c, 1)) |
2005 | if (!read (pt->rep.stdio.file, &c, 1)) |
… | |
… | |
2018 | return EOF; |
2013 | return EOF; |
2019 | else |
2014 | else |
2020 | return *pt->rep.string.curr++; |
2015 | return *pt->rep.string.curr++; |
2021 | } |
2016 | } |
2022 | #else |
2017 | #else |
2023 | if (ungot == -1) |
|
|
2024 | { |
|
|
2025 | char c; |
2018 | char c; |
2026 | if (!read (0, &c, 1)) |
2019 | |
|
|
2020 | if (!read (pt->rep.stdio.file, &c, 1)) |
2027 | return EOF; |
2021 | return EOF; |
2028 | |
2022 | |
2029 | ungot = c; |
|
|
2030 | } |
|
|
2031 | |
|
|
2032 | { |
|
|
2033 | int r = ungot; |
|
|
2034 | ungot = -1; |
|
|
2035 | return r; |
2023 | return c; |
2036 | } |
|
|
2037 | #endif |
2024 | #endif |
2038 | } |
2025 | } |
2039 | |
2026 | |
2040 | /* back character to input buffer */ |
2027 | /* back character to input buffer */ |
2041 | static void |
2028 | ecb_cold static void |
2042 | backchar (SCHEME_P_ int c) |
2029 | backchar (SCHEME_P_ int c) |
2043 | { |
2030 | { |
2044 | #if USE_PORTS |
2031 | port *pt = port (SCHEME_V->inport); |
2045 | port *pt; |
|
|
2046 | |
2032 | |
2047 | if (c == EOF) |
2033 | if (c == EOF) |
2048 | return; |
2034 | return; |
2049 | |
2035 | |
2050 | pt = SCHEME_V->inport->object.port; |
|
|
2051 | pt->unget = c; |
2036 | pt->unget = c; |
2052 | #else |
|
|
2053 | if (c == EOF) |
|
|
2054 | return; |
|
|
2055 | |
|
|
2056 | ungot = c; |
|
|
2057 | #endif |
|
|
2058 | } |
2037 | } |
2059 | |
2038 | |
2060 | #if USE_PORTS |
2039 | #if USE_PORTS |
2061 | static int |
2040 | ecb_cold static int |
2062 | realloc_port_string (SCHEME_P_ port *p) |
2041 | realloc_port_string (SCHEME_P_ port *p) |
2063 | { |
2042 | { |
2064 | char *start = p->rep.string.start; |
2043 | char *start = p->rep.string.start; |
2065 | 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; |
2066 | char *str = malloc (new_size); |
2045 | char *str = malloc (new_size); |
… | |
… | |
2079 | else |
2058 | else |
2080 | return 0; |
2059 | return 0; |
2081 | } |
2060 | } |
2082 | #endif |
2061 | #endif |
2083 | |
2062 | |
2084 | INTERFACE void |
2063 | ecb_cold static void |
2085 | putstr (SCHEME_P_ const char *s) |
2064 | putchars (SCHEME_P_ const char *s, int len) |
2086 | { |
2065 | { |
|
|
2066 | port *pt = port (SCHEME_V->outport); |
|
|
2067 | |
2087 | #if USE_PORTS |
2068 | #if USE_PORTS |
2088 | port *pt = SCHEME_V->outport->object.port; |
|
|
2089 | |
|
|
2090 | if (pt->kind & port_file) |
|
|
2091 | write (pt->rep.stdio.file, s, strlen (s)); |
|
|
2092 | else |
|
|
2093 | for (; *s; s++) |
|
|
2094 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2095 | *pt->rep.string.curr++ = *s; |
|
|
2096 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2097 | *pt->rep.string.curr++ = *s; |
|
|
2098 | |
|
|
2099 | #else |
|
|
2100 | xwrstr (s); |
|
|
2101 | #endif |
|
|
2102 | } |
|
|
2103 | |
|
|
2104 | static void |
|
|
2105 | putchars (SCHEME_P_ const char *s, int len) |
|
|
2106 | { |
|
|
2107 | #if USE_PORTS |
|
|
2108 | port *pt = SCHEME_V->outport->object.port; |
|
|
2109 | |
|
|
2110 | if (pt->kind & port_file) |
2069 | if (pt->kind & port_file) |
2111 | write (pt->rep.stdio.file, s, len); |
2070 | write (pt->rep.stdio.file, s, len); |
2112 | else |
2071 | else |
2113 | { |
2072 | { |
2114 | for (; len; len--) |
2073 | for (; len; len--) |
… | |
… | |
2119 | *pt->rep.string.curr++ = *s++; |
2078 | *pt->rep.string.curr++ = *s++; |
2120 | } |
2079 | } |
2121 | } |
2080 | } |
2122 | |
2081 | |
2123 | #else |
2082 | #else |
2124 | write (1, s, len); |
2083 | write (1, s, len); // output not initialised |
2125 | #endif |
2084 | #endif |
|
|
2085 | } |
|
|
2086 | |
|
|
2087 | INTERFACE void |
|
|
2088 | putstr (SCHEME_P_ const char *s) |
|
|
2089 | { |
|
|
2090 | putchars (SCHEME_A_ s, strlen (s)); |
2126 | } |
2091 | } |
2127 | |
2092 | |
2128 | INTERFACE void |
2093 | INTERFACE void |
2129 | putcharacter (SCHEME_P_ int c) |
2094 | putcharacter (SCHEME_P_ int c) |
2130 | { |
2095 | { |
2131 | #if USE_PORTS |
|
|
2132 | port *pt = SCHEME_V->outport->object.port; |
|
|
2133 | |
|
|
2134 | if (pt->kind & port_file) |
|
|
2135 | { |
|
|
2136 | char cc = c; |
|
|
2137 | write (pt->rep.stdio.file, &cc, 1); |
|
|
2138 | } |
|
|
2139 | else |
|
|
2140 | { |
|
|
2141 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2142 | *pt->rep.string.curr++ = c; |
|
|
2143 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2144 | *pt->rep.string.curr++ = c; |
|
|
2145 | } |
|
|
2146 | |
|
|
2147 | #else |
|
|
2148 | char cc = c; |
2096 | char cc = c; |
2149 | write (1, &c, 1); |
2097 | |
2150 | #endif |
2098 | putchars (SCHEME_A_ &cc, 1); |
2151 | } |
2099 | } |
2152 | |
2100 | |
2153 | /* read characters up to delimiter, but cater to character constants */ |
2101 | /* read characters up to delimiter, but cater to character constants */ |
2154 | static char * |
2102 | ecb_cold static char * |
2155 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2103 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2156 | { |
2104 | { |
2157 | char *p = SCHEME_V->strbuff + skip; |
2105 | char *p = SCHEME_V->strbuff + skip; |
2158 | |
2106 | |
2159 | 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)))); |
… | |
… | |
2168 | |
2116 | |
2169 | return SCHEME_V->strbuff; |
2117 | return SCHEME_V->strbuff; |
2170 | } |
2118 | } |
2171 | |
2119 | |
2172 | /* read string expression "xxx...xxx" */ |
2120 | /* read string expression "xxx...xxx" */ |
2173 | static pointer |
2121 | ecb_cold static pointer |
2174 | readstrexp (SCHEME_P_ char delim) |
2122 | readstrexp (SCHEME_P_ char delim) |
2175 | { |
2123 | { |
2176 | char *p = SCHEME_V->strbuff; |
2124 | char *p = SCHEME_V->strbuff; |
2177 | int c; |
2125 | int c; |
2178 | int c1 = 0; |
2126 | int c1 = 0; |
… | |
… | |
2211 | case '7': |
2159 | case '7': |
2212 | state = st_oct1; |
2160 | state = st_oct1; |
2213 | c1 = c - '0'; |
2161 | c1 = c - '0'; |
2214 | break; |
2162 | break; |
2215 | |
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 |
2216 | case 'x': |
2174 | case 'x': |
2217 | case 'X': |
2175 | case 'X': |
2218 | state = st_x1; |
2176 | state = st_x1; |
2219 | c1 = 0; |
2177 | c1 = 0; |
2220 | break; |
|
|
2221 | |
|
|
2222 | case 'n': |
|
|
2223 | *p++ = '\n'; |
|
|
2224 | state = st_ok; |
|
|
2225 | break; |
|
|
2226 | |
|
|
2227 | case 't': |
|
|
2228 | *p++ = '\t'; |
|
|
2229 | state = st_ok; |
|
|
2230 | break; |
|
|
2231 | |
|
|
2232 | case 'r': |
|
|
2233 | *p++ = '\r'; |
|
|
2234 | state = st_ok; |
|
|
2235 | break; |
2178 | break; |
2236 | |
2179 | |
2237 | default: |
2180 | default: |
2238 | *p++ = c; |
2181 | *p++ = c; |
2239 | state = st_ok; |
2182 | state = st_ok; |
… | |
… | |
2291 | } |
2234 | } |
2292 | } |
2235 | } |
2293 | } |
2236 | } |
2294 | |
2237 | |
2295 | /* check c is in chars */ |
2238 | /* check c is in chars */ |
2296 | ecb_inline int |
2239 | ecb_cold int |
2297 | is_one_of (const char *s, int c) |
2240 | is_one_of (const char *s, int c) |
2298 | { |
2241 | { |
2299 | return c == EOF || !!strchr (s, c); |
2242 | return c == EOF || !!strchr (s, c); |
2300 | } |
2243 | } |
2301 | |
2244 | |
2302 | /* skip white characters */ |
2245 | /* skip white characters */ |
2303 | ecb_inline int |
2246 | ecb_cold int |
2304 | skipspace (SCHEME_P) |
2247 | skipspace (SCHEME_P) |
2305 | { |
2248 | { |
2306 | int c, curr_line = 0; |
2249 | int c, curr_line = 0; |
2307 | |
2250 | |
2308 | do |
2251 | do |
… | |
… | |
2328 | backchar (SCHEME_A_ c); |
2271 | backchar (SCHEME_A_ c); |
2329 | return 1; |
2272 | return 1; |
2330 | } |
2273 | } |
2331 | |
2274 | |
2332 | /* get token */ |
2275 | /* get token */ |
2333 | static int |
2276 | ecb_cold static int |
2334 | token (SCHEME_P) |
2277 | token (SCHEME_P) |
2335 | { |
2278 | { |
2336 | int c = skipspace (SCHEME_A); |
2279 | int c = skipspace (SCHEME_A); |
2337 | |
2280 | |
2338 | if (c == EOF) |
2281 | if (c == EOF) |
… | |
… | |
2436 | } |
2379 | } |
2437 | |
2380 | |
2438 | /* ========== Routines for Printing ========== */ |
2381 | /* ========== Routines for Printing ========== */ |
2439 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2382 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2440 | |
2383 | |
2441 | static void |
2384 | ecb_cold static void |
2442 | printslashstring (SCHEME_P_ char *p, int len) |
2385 | printslashstring (SCHEME_P_ char *p, int len) |
2443 | { |
2386 | { |
2444 | int i; |
2387 | int i; |
2445 | unsigned char *s = (unsigned char *) p; |
2388 | unsigned char *s = (unsigned char *) p; |
2446 | |
2389 | |
… | |
… | |
2502 | |
2445 | |
2503 | putcharacter (SCHEME_A_ '"'); |
2446 | putcharacter (SCHEME_A_ '"'); |
2504 | } |
2447 | } |
2505 | |
2448 | |
2506 | /* print atoms */ |
2449 | /* print atoms */ |
2507 | static void |
2450 | ecb_cold static void |
2508 | printatom (SCHEME_P_ pointer l, int f) |
2451 | printatom (SCHEME_P_ pointer l, int f) |
2509 | { |
2452 | { |
2510 | char *p; |
2453 | char *p; |
2511 | int len; |
2454 | int len; |
2512 | |
2455 | |
2513 | atom2str (SCHEME_A_ l, f, &p, &len); |
2456 | atom2str (SCHEME_A_ l, f, &p, &len); |
2514 | putchars (SCHEME_A_ p, len); |
2457 | putchars (SCHEME_A_ p, len); |
2515 | } |
2458 | } |
2516 | |
2459 | |
2517 | /* Uses internal buffer unless string pointer is already available */ |
2460 | /* Uses internal buffer unless string pointer is already available */ |
2518 | static void |
2461 | ecb_cold static void |
2519 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2462 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2520 | { |
2463 | { |
2521 | char *p; |
2464 | char *p; |
2522 | |
2465 | |
2523 | if (l == NIL) |
2466 | if (l == NIL) |
… | |
… | |
2730 | return car (d); |
2673 | return car (d); |
2731 | |
2674 | |
2732 | p = cons (car (d), cdr (d)); |
2675 | p = cons (car (d), cdr (d)); |
2733 | q = p; |
2676 | q = p; |
2734 | |
2677 | |
2735 | while (cdr (cdr (p)) != NIL) |
2678 | while (cddr (p) != NIL) |
2736 | { |
2679 | { |
2737 | d = cons (car (p), cdr (p)); |
2680 | d = cons (car (p), cdr (p)); |
2738 | |
2681 | |
2739 | if (cdr (cdr (p)) != NIL) |
2682 | if (cddr (p) != NIL) |
2740 | p = cdr (d); |
2683 | p = cdr (d); |
2741 | } |
2684 | } |
2742 | |
2685 | |
2743 | set_cdr (p, car (cdr (p))); |
2686 | set_cdr (p, cadr (p)); |
2744 | return q; |
2687 | return q; |
2745 | } |
2688 | } |
2746 | |
2689 | |
2747 | /* reverse list -- produce new list */ |
2690 | /* reverse list -- produce new list */ |
2748 | static pointer |
2691 | ecb_hot static pointer |
2749 | reverse (SCHEME_P_ pointer a) |
2692 | reverse (SCHEME_P_ pointer a) |
2750 | { |
2693 | { |
2751 | /* a must be checked by gc */ |
2694 | /* a must be checked by gc */ |
2752 | pointer p = NIL; |
2695 | pointer p = NIL; |
2753 | |
2696 | |
… | |
… | |
2756 | |
2699 | |
2757 | return p; |
2700 | return p; |
2758 | } |
2701 | } |
2759 | |
2702 | |
2760 | /* reverse list --- in-place */ |
2703 | /* reverse list --- in-place */ |
2761 | static pointer |
2704 | ecb_hot static pointer |
2762 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2705 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2763 | { |
2706 | { |
2764 | pointer result = term; |
2707 | pointer result = term; |
2765 | pointer p = list; |
2708 | pointer p = list; |
2766 | |
2709 | |
… | |
… | |
2774 | |
2717 | |
2775 | return result; |
2718 | return result; |
2776 | } |
2719 | } |
2777 | |
2720 | |
2778 | /* append list -- produce new list (in reverse order) */ |
2721 | /* append list -- produce new list (in reverse order) */ |
2779 | static pointer |
2722 | ecb_hot static pointer |
2780 | revappend (SCHEME_P_ pointer a, pointer b) |
2723 | revappend (SCHEME_P_ pointer a, pointer b) |
2781 | { |
2724 | { |
2782 | pointer result = a; |
2725 | pointer result = a; |
2783 | pointer p = b; |
2726 | pointer p = b; |
2784 | |
2727 | |
… | |
… | |
2793 | |
2736 | |
2794 | return S_F; /* signal an error */ |
2737 | return S_F; /* signal an error */ |
2795 | } |
2738 | } |
2796 | |
2739 | |
2797 | /* equivalence of atoms */ |
2740 | /* equivalence of atoms */ |
2798 | int |
2741 | ecb_hot int |
2799 | eqv (pointer a, pointer b) |
2742 | eqv (pointer a, pointer b) |
2800 | { |
2743 | { |
2801 | if (is_string (a)) |
2744 | if (is_string (a)) |
2802 | { |
2745 | { |
2803 | if (is_string (b)) |
2746 | if (is_string (b)) |
… | |
… | |
2897 | } |
2840 | } |
2898 | else |
2841 | else |
2899 | set_car (env, immutable_cons (slot, car (env))); |
2842 | set_car (env, immutable_cons (slot, car (env))); |
2900 | } |
2843 | } |
2901 | |
2844 | |
2902 | static pointer |
2845 | ecb_hot static pointer |
2903 | 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) |
2904 | { |
2847 | { |
2905 | pointer x, y; |
2848 | pointer x, y; |
2906 | |
2849 | |
2907 | for (x = env; x != NIL; x = cdr (x)) |
2850 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2928 | return NIL; |
2871 | return NIL; |
2929 | } |
2872 | } |
2930 | |
2873 | |
2931 | #else /* USE_ALIST_ENV */ |
2874 | #else /* USE_ALIST_ENV */ |
2932 | |
2875 | |
2933 | ecb_inline void |
2876 | static void |
2934 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2877 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2935 | { |
2878 | { |
2936 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2879 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2937 | setenvironment (SCHEME_V->envir); |
2880 | setenvironment (SCHEME_V->envir); |
2938 | } |
2881 | } |
2939 | |
2882 | |
2940 | ecb_inline void |
2883 | static void |
2941 | 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) |
2942 | { |
2885 | { |
2943 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2886 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2944 | } |
2887 | } |
2945 | |
2888 | |
2946 | static pointer |
2889 | ecb_hot static pointer |
2947 | 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) |
2948 | { |
2891 | { |
2949 | pointer x, y; |
2892 | pointer x, y; |
2950 | |
2893 | |
2951 | for (x = env; x != NIL; x = cdr (x)) |
2894 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2965 | return NIL; |
2908 | return NIL; |
2966 | } |
2909 | } |
2967 | |
2910 | |
2968 | #endif /* USE_ALIST_ENV else */ |
2911 | #endif /* USE_ALIST_ENV else */ |
2969 | |
2912 | |
2970 | ecb_inline void |
2913 | static void |
2971 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2914 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2972 | { |
2915 | { |
2973 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2916 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2974 | 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); |
2975 | } |
2918 | } |
2976 | |
2919 | |
2977 | ecb_inline void |
2920 | static void |
2978 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2921 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2979 | { |
2922 | { |
2980 | set_cdr (slot, value); |
2923 | set_cdr (slot, value); |
2981 | } |
2924 | } |
2982 | |
2925 | |
2983 | ecb_inline pointer |
2926 | static pointer |
2984 | slot_value_in_env (pointer slot) |
2927 | slot_value_in_env (pointer slot) |
2985 | { |
2928 | { |
2986 | return cdr (slot); |
2929 | return cdr (slot); |
2987 | } |
2930 | } |
2988 | |
2931 | |
2989 | /* ========== Evaluation Cycle ========== */ |
2932 | /* ========== Evaluation Cycle ========== */ |
2990 | |
2933 | |
2991 | static int |
2934 | ecb_cold static int |
2992 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2935 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2993 | { |
2936 | { |
2994 | #if USE_ERROR_HOOK |
2937 | #if USE_ERROR_HOOK |
2995 | pointer x; |
2938 | pointer x; |
2996 | pointer hdl = SCHEME_V->ERROR_HOOK; |
2939 | pointer hdl = SCHEME_V->ERROR_HOOK; |
… | |
… | |
3072 | pointer code; |
3015 | pointer code; |
3073 | }; |
3016 | }; |
3074 | |
3017 | |
3075 | # define STACK_GROWTH 3 |
3018 | # define STACK_GROWTH 3 |
3076 | |
3019 | |
3077 | static void |
3020 | ecb_hot static void |
3078 | 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) |
3079 | { |
3022 | { |
3080 | int nframes = (uintptr_t)SCHEME_V->dump; |
3023 | int nframes = (uintptr_t)SCHEME_V->dump; |
3081 | struct dump_stack_frame *next_frame; |
3024 | struct dump_stack_frame *next_frame; |
3082 | |
3025 | |
3083 | /* enough room for the next frame? */ |
3026 | /* enough room for the next frame? */ |
3084 | if (nframes >= SCHEME_V->dump_size) |
3027 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3085 | { |
3028 | { |
3086 | SCHEME_V->dump_size += STACK_GROWTH; |
3029 | SCHEME_V->dump_size += STACK_GROWTH; |
3087 | 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); |
3088 | } |
3031 | } |
3089 | |
3032 | |
… | |
… | |
3095 | next_frame->code = code; |
3038 | next_frame->code = code; |
3096 | |
3039 | |
3097 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3040 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3098 | } |
3041 | } |
3099 | |
3042 | |
3100 | static int |
3043 | static ecb_hot int |
3101 | xs_return (SCHEME_P_ pointer a) |
3044 | xs_return (SCHEME_P_ pointer a) |
3102 | { |
3045 | { |
3103 | int nframes = (uintptr_t)SCHEME_V->dump; |
3046 | int nframes = (uintptr_t)SCHEME_V->dump; |
3104 | struct dump_stack_frame *frame; |
3047 | struct dump_stack_frame *frame; |
3105 | |
3048 | |
… | |
… | |
3116 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3059 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3117 | |
3060 | |
3118 | return 0; |
3061 | return 0; |
3119 | } |
3062 | } |
3120 | |
3063 | |
3121 | ecb_inline void |
3064 | ecb_cold void |
3122 | dump_stack_reset (SCHEME_P) |
3065 | dump_stack_reset (SCHEME_P) |
3123 | { |
3066 | { |
3124 | /* 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 */ |
3125 | SCHEME_V->dump = (pointer)+0; |
3068 | SCHEME_V->dump = (pointer)+0; |
3126 | } |
3069 | } |
3127 | |
3070 | |
3128 | ecb_inline void |
3071 | ecb_cold void |
3129 | dump_stack_initialize (SCHEME_P) |
3072 | dump_stack_initialize (SCHEME_P) |
3130 | { |
3073 | { |
3131 | SCHEME_V->dump_size = 0; |
3074 | SCHEME_V->dump_size = 0; |
3132 | SCHEME_V->dump_base = 0; |
3075 | SCHEME_V->dump_base = 0; |
3133 | dump_stack_reset (SCHEME_A); |
3076 | dump_stack_reset (SCHEME_A); |
3134 | } |
3077 | } |
3135 | |
3078 | |
3136 | static void |
3079 | ecb_cold static void |
3137 | dump_stack_free (SCHEME_P) |
3080 | dump_stack_free (SCHEME_P) |
3138 | { |
3081 | { |
3139 | free (SCHEME_V->dump_base); |
3082 | free (SCHEME_V->dump_base); |
3140 | SCHEME_V->dump_base = 0; |
3083 | SCHEME_V->dump_base = 0; |
3141 | SCHEME_V->dump = (pointer)0; |
3084 | SCHEME_V->dump = (pointer)0; |
3142 | SCHEME_V->dump_size = 0; |
3085 | SCHEME_V->dump_size = 0; |
3143 | } |
3086 | } |
3144 | |
3087 | |
3145 | static void |
3088 | ecb_cold static void |
3146 | dump_stack_mark (SCHEME_P) |
3089 | dump_stack_mark (SCHEME_P) |
3147 | { |
3090 | { |
3148 | int nframes = (uintptr_t)SCHEME_V->dump; |
3091 | int nframes = (uintptr_t)SCHEME_V->dump; |
3149 | int i; |
3092 | int i; |
3150 | |
3093 | |
… | |
… | |
3156 | mark (frame->envir); |
3099 | mark (frame->envir); |
3157 | mark (frame->code); |
3100 | mark (frame->code); |
3158 | } |
3101 | } |
3159 | } |
3102 | } |
3160 | |
3103 | |
3161 | static pointer |
3104 | ecb_cold static pointer |
3162 | ss_get_cont (SCHEME_P) |
3105 | ss_get_cont (SCHEME_P) |
3163 | { |
3106 | { |
3164 | int nframes = (uintptr_t)SCHEME_V->dump; |
3107 | int nframes = (uintptr_t)SCHEME_V->dump; |
3165 | int i; |
3108 | int i; |
3166 | |
3109 | |
… | |
… | |
3178 | } |
3121 | } |
3179 | |
3122 | |
3180 | return cont; |
3123 | return cont; |
3181 | } |
3124 | } |
3182 | |
3125 | |
3183 | static void |
3126 | ecb_cold static void |
3184 | ss_set_cont (SCHEME_P_ pointer cont) |
3127 | ss_set_cont (SCHEME_P_ pointer cont) |
3185 | { |
3128 | { |
3186 | int i = 0; |
3129 | int i = 0; |
3187 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3130 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3188 | |
3131 | |
… | |
… | |
3200 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3143 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3201 | } |
3144 | } |
3202 | |
3145 | |
3203 | #else |
3146 | #else |
3204 | |
3147 | |
3205 | ecb_inline void |
3148 | ecb_cold void |
3206 | dump_stack_reset (SCHEME_P) |
3149 | dump_stack_reset (SCHEME_P) |
3207 | { |
3150 | { |
3208 | SCHEME_V->dump = NIL; |
3151 | SCHEME_V->dump = NIL; |
3209 | } |
3152 | } |
3210 | |
3153 | |
3211 | ecb_inline void |
3154 | ecb_cold void |
3212 | dump_stack_initialize (SCHEME_P) |
3155 | dump_stack_initialize (SCHEME_P) |
3213 | { |
3156 | { |
3214 | dump_stack_reset (SCHEME_A); |
3157 | dump_stack_reset (SCHEME_A); |
3215 | } |
3158 | } |
3216 | |
3159 | |
3217 | static void |
3160 | ecb_cold static void |
3218 | dump_stack_free (SCHEME_P) |
3161 | dump_stack_free (SCHEME_P) |
3219 | { |
3162 | { |
3220 | SCHEME_V->dump = NIL; |
3163 | SCHEME_V->dump = NIL; |
3221 | } |
3164 | } |
3222 | |
3165 | |
3223 | static int |
3166 | ecb_hot static int |
3224 | xs_return (SCHEME_P_ pointer a) |
3167 | xs_return (SCHEME_P_ pointer a) |
3225 | { |
3168 | { |
3226 | pointer dump = SCHEME_V->dump; |
3169 | pointer dump = SCHEME_V->dump; |
3227 | |
3170 | |
3228 | SCHEME_V->value = a; |
3171 | SCHEME_V->value = a; |
… | |
… | |
3238 | SCHEME_V->dump = dump; |
3181 | SCHEME_V->dump = dump; |
3239 | |
3182 | |
3240 | return 0; |
3183 | return 0; |
3241 | } |
3184 | } |
3242 | |
3185 | |
3243 | static void |
3186 | ecb_hot static void |
3244 | 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) |
3245 | { |
3188 | { |
3246 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3189 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3247 | cons (args, |
3190 | cons (args, |
3248 | cons (SCHEME_V->envir, |
3191 | cons (SCHEME_V->envir, |
3249 | cons (code, |
3192 | cons (code, |
3250 | SCHEME_V->dump)))); |
3193 | SCHEME_V->dump)))); |
3251 | } |
3194 | } |
3252 | |
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 | |
3253 | static void |
3274 | static void |
3254 | dump_stack_mark (SCHEME_P) |
3275 | compile (SCHEME_P_ stream s, pointer x) |
3255 | { |
3276 | { |
3256 | mark (SCHEME_V->dump); |
3277 | if (x == NIL) |
3257 | } |
3278 | { |
|
|
3279 | stream_put (s, 0); |
|
|
3280 | return; |
|
|
3281 | } |
3258 | |
3282 | |
3259 | static pointer |
3283 | if (is_syntax (x)) |
3260 | ss_get_cont (SCHEME_P) |
3284 | { |
3261 | { |
3285 | stream_put (s, 1); |
3262 | return SCHEME_V->dump; |
3286 | stream_put (s, syntaxnum (x)); |
3263 | } |
3287 | return; |
|
|
3288 | } |
3264 | |
3289 | |
3265 | static void |
3290 | switch (type (x)) |
3266 | ss_set_cont (SCHEME_P_ pointer cont) |
3291 | { |
3267 | { |
3292 | case T_INTEGER: |
3268 | SCHEME_V->dump = cont; |
3293 | stream_put (s, 2); |
3269 | } |
3294 | stream_put (s, 0); |
|
|
3295 | stream_put (s, 0); |
|
|
3296 | stream_put (s, 0); |
|
|
3297 | stream_put (s, 0); |
|
|
3298 | return; |
3270 | |
3299 | |
3271 | #endif |
3300 | case T_SYMBOL: |
|
|
3301 | { |
|
|
3302 | uint32_t sym = symbol_id (SCHEME_A_ x); |
|
|
3303 | printf ("sym %x\n", sym);//D |
3272 | |
3304 | |
3273 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3305 | stream_put (s, 3); |
3274 | |
3306 | |
3275 | #if EXPERIMENT |
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 | } |
|
|
3337 | |
3276 | 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 |
3277 | debug (SCHEME_P_ int indent, pointer x) |
3354 | dtree (SCHEME_P_ int indent, pointer x) |
3278 | { |
3355 | { |
3279 | int c; |
3356 | int c; |
3280 | |
3357 | |
3281 | if (is_syntax (x)) |
3358 | if (is_syntax (x)) |
3282 | { |
3359 | { |
… | |
… | |
3300 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
3377 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
3301 | return 24+8; |
3378 | return 24+8; |
3302 | |
3379 | |
3303 | case T_CLOSURE: |
3380 | case T_CLOSURE: |
3304 | printf ("%*sS<%s>\n", indent, "", "closure"); |
3381 | printf ("%*sS<%s>\n", indent, "", "closure"); |
3305 | debug (SCHEME_A_ indent + 3, cdr(x)); |
3382 | dtree (SCHEME_A_ indent + 3, cdr(x)); |
3306 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
3383 | return 32 + dtree (SCHEME_A_ indent + 3, car (x)); |
3307 | |
3384 | |
3308 | case T_PAIR: |
3385 | case T_PAIR: |
3309 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
3386 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
3310 | c = debug (SCHEME_A_ indent + 3, car (x)); |
3387 | c = dtree (SCHEME_A_ indent + 3, car (x)); |
3311 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
3388 | c += dtree (SCHEME_A_ indent + 3, cdr (x)); |
3312 | return c + 1; |
3389 | return c + 1; |
3313 | |
3390 | |
3314 | case T_PORT: |
3391 | case T_PORT: |
3315 | printf ("%*sS<%s>\n", indent, "", "port"); |
3392 | printf ("%*sS<%s>\n", indent, "", "port"); |
3316 | return 24+8; |
3393 | return 24+8; |
… | |
… | |
3319 | printf ("%*sS<%s>\n", indent, "", "vector"); |
3396 | printf ("%*sS<%s>\n", indent, "", "vector"); |
3320 | return 24+8; |
3397 | return 24+8; |
3321 | |
3398 | |
3322 | case T_ENVIRONMENT: |
3399 | case T_ENVIRONMENT: |
3323 | printf ("%*sS<%s>\n", indent, "", "environment"); |
3400 | printf ("%*sS<%s>\n", indent, "", "environment"); |
3324 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
3401 | return 0 + dtree (SCHEME_A_ indent + 3, car (x)); |
3325 | |
3402 | |
3326 | default: |
3403 | default: |
3327 | printf ("unhandled type %d\n", type (x)); |
3404 | printf ("unhandled type %d\n", type (x)); |
3328 | break; |
3405 | break; |
3329 | } |
3406 | } |
3330 | } |
3407 | } |
3331 | #endif |
3408 | #endif |
3332 | |
3409 | |
3333 | static int |
3410 | /* syntax, eval, core, ... */ |
|
|
3411 | ecb_hot static int |
3334 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3412 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3335 | { |
3413 | { |
3336 | pointer args = SCHEME_V->args; |
3414 | pointer args = SCHEME_V->args; |
3337 | pointer x, y; |
3415 | pointer x, y; |
3338 | |
3416 | |
3339 | switch (op) |
3417 | switch (op) |
3340 | { |
3418 | { |
3341 | #if EXPERIMENT //D |
3419 | #if EXPERIMENT //D |
3342 | case OP_DEBUG: |
3420 | case OP_DEBUG: |
3343 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
3421 | { |
|
|
3422 | uint32_t len = compile_closure (SCHEME_A_ car (args)); |
|
|
3423 | printf ("len = %d\n", len); |
3344 | printf ("\n"); |
3424 | printf ("\n"); |
3345 | s_return (S_T); |
3425 | s_return (S_T); |
|
|
3426 | } |
3346 | #endif |
3427 | #endif |
3347 | case OP_LOAD: /* load */ |
3428 | case OP_LOAD: /* load */ |
3348 | if (file_interactive (SCHEME_A)) |
3429 | if (file_interactive (SCHEME_A)) |
3349 | { |
3430 | { |
3350 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3431 | putstr (SCHEME_A_ "Loading "); |
3351 | //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'); |
3352 | } |
3434 | } |
3353 | |
3435 | |
3354 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3436 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3355 | Error_1 ("unable to open", car (args)); |
3437 | Error_1 ("unable to open", car (args)); |
3356 | else |
3438 | |
3357 | { |
|
|
3358 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3439 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3359 | s_goto (OP_T0LVL); |
3440 | s_goto (OP_T0LVL); |
3360 | } |
|
|
3361 | |
3441 | |
3362 | case OP_T0LVL: /* top level */ |
3442 | case OP_T0LVL: /* top level */ |
3363 | |
3443 | |
3364 | /* If we reached the end of file, this loop is done. */ |
3444 | /* If we reached the end of file, this loop is done. */ |
3365 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3445 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3366 | { |
3446 | { |
3367 | if (SCHEME_V->file_i == 0) |
3447 | if (SCHEME_V->file_i == 0) |
3368 | { |
3448 | { |
3369 | SCHEME_V->args = NIL; |
3449 | SCHEME_V->args = NIL; |
3370 | s_goto (OP_QUIT); |
3450 | s_goto (OP_QUIT); |
… | |
… | |
3381 | /* If interactive, be nice to user. */ |
3461 | /* If interactive, be nice to user. */ |
3382 | if (file_interactive (SCHEME_A)) |
3462 | if (file_interactive (SCHEME_A)) |
3383 | { |
3463 | { |
3384 | SCHEME_V->envir = SCHEME_V->global_env; |
3464 | SCHEME_V->envir = SCHEME_V->global_env; |
3385 | dump_stack_reset (SCHEME_A); |
3465 | dump_stack_reset (SCHEME_A); |
3386 | putstr (SCHEME_A_ "\n"); |
3466 | putcharacter (SCHEME_A_ '\n'); |
3387 | putstr (SCHEME_A_ prompt); |
3467 | putstr (SCHEME_A_ prompt); |
3388 | } |
3468 | } |
3389 | |
3469 | |
3390 | /* Set up another iteration of REPL */ |
3470 | /* Set up another iteration of REPL */ |
3391 | SCHEME_V->nesting = 0; |
3471 | SCHEME_V->nesting = 0; |
… | |
… | |
3426 | { |
3506 | { |
3427 | SCHEME_V->print_flag = 1; |
3507 | SCHEME_V->print_flag = 1; |
3428 | SCHEME_V->args = SCHEME_V->value; |
3508 | SCHEME_V->args = SCHEME_V->value; |
3429 | s_goto (OP_P0LIST); |
3509 | s_goto (OP_P0LIST); |
3430 | } |
3510 | } |
3431 | else |
3511 | |
3432 | s_return (SCHEME_V->value); |
3512 | s_return (SCHEME_V->value); |
3433 | |
3513 | |
3434 | case OP_EVAL: /* main part of evaluation */ |
3514 | case OP_EVAL: /* main part of evaluation */ |
3435 | #if USE_TRACING |
3515 | #if USE_TRACING |
3436 | if (SCHEME_V->tracing) |
3516 | if (SCHEME_V->tracing) |
3437 | { |
3517 | { |
… | |
… | |
3448 | #endif |
3528 | #endif |
3449 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3529 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3450 | { |
3530 | { |
3451 | 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); |
3452 | |
3532 | |
3453 | if (x != NIL) |
3533 | if (x == NIL) |
3454 | s_return (slot_value_in_env (x)); |
|
|
3455 | else |
|
|
3456 | 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)); |
3457 | } |
3537 | } |
3458 | else if (is_pair (SCHEME_V->code)) |
3538 | else if (is_pair (SCHEME_V->code)) |
3459 | { |
3539 | { |
3460 | x = car (SCHEME_V->code); |
3540 | x = car (SCHEME_V->code); |
3461 | |
3541 | |
… | |
… | |
3470 | /* 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)); */ |
3471 | SCHEME_V->code = x; |
3551 | SCHEME_V->code = x; |
3472 | s_goto (OP_EVAL); |
3552 | s_goto (OP_EVAL); |
3473 | } |
3553 | } |
3474 | } |
3554 | } |
3475 | else |
3555 | |
3476 | s_return (SCHEME_V->code); |
3556 | s_return (SCHEME_V->code); |
3477 | |
3557 | |
3478 | case OP_E0ARGS: /* eval arguments */ |
3558 | case OP_E0ARGS: /* eval arguments */ |
3479 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3559 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3480 | { |
3560 | { |
3481 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3561 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
… | |
… | |
3538 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3618 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3539 | else if (is_foreign (SCHEME_V->code)) |
3619 | else if (is_foreign (SCHEME_V->code)) |
3540 | { |
3620 | { |
3541 | /* Keep nested calls from GC'ing the arglist */ |
3621 | /* Keep nested calls from GC'ing the arglist */ |
3542 | push_recent_alloc (SCHEME_A_ args, NIL); |
3622 | push_recent_alloc (SCHEME_A_ args, NIL); |
3543 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3623 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3544 | |
3624 | |
3545 | s_return (x); |
3625 | s_return (x); |
3546 | } |
3626 | } |
3547 | 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 */ |
3548 | { |
3628 | { |
… | |
… | |
3653 | else |
3733 | else |
3654 | 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); |
3655 | |
3735 | |
3656 | s_return (SCHEME_V->code); |
3736 | s_return (SCHEME_V->code); |
3657 | |
3737 | |
3658 | |
|
|
3659 | case OP_DEFP: /* defined? */ |
3738 | case OP_DEFP: /* defined? */ |
3660 | x = SCHEME_V->envir; |
3739 | x = SCHEME_V->envir; |
3661 | |
3740 | |
3662 | if (cdr (args) != NIL) |
3741 | if (cdr (args) != NIL) |
3663 | x = cadr (args); |
3742 | x = cadr (args); |
… | |
… | |
3680 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3759 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3681 | s_return (SCHEME_V->value); |
3760 | s_return (SCHEME_V->value); |
3682 | } |
3761 | } |
3683 | else |
3762 | else |
3684 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3763 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3685 | |
|
|
3686 | |
3764 | |
3687 | case OP_BEGIN: /* begin */ |
3765 | case OP_BEGIN: /* begin */ |
3688 | if (!is_pair (SCHEME_V->code)) |
3766 | if (!is_pair (SCHEME_V->code)) |
3689 | s_return (SCHEME_V->code); |
3767 | s_return (SCHEME_V->code); |
3690 | |
3768 | |
… | |
… | |
3702 | case OP_IF1: /* if */ |
3780 | case OP_IF1: /* if */ |
3703 | if (is_true (SCHEME_V->value)) |
3781 | if (is_true (SCHEME_V->value)) |
3704 | SCHEME_V->code = car (SCHEME_V->code); |
3782 | SCHEME_V->code = car (SCHEME_V->code); |
3705 | else |
3783 | else |
3706 | 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 | |
3707 | s_goto (OP_EVAL); |
3786 | s_goto (OP_EVAL); |
3708 | |
3787 | |
3709 | case OP_LET0: /* let */ |
3788 | case OP_LET0: /* let */ |
3710 | SCHEME_V->args = NIL; |
3789 | SCHEME_V->args = NIL; |
3711 | SCHEME_V->value = SCHEME_V->code; |
3790 | SCHEME_V->value = SCHEME_V->code; |
… | |
… | |
3867 | } |
3946 | } |
3868 | else |
3947 | else |
3869 | { |
3948 | { |
3870 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3949 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3871 | s_return (NIL); |
3950 | s_return (NIL); |
3872 | else |
3951 | |
3873 | { |
|
|
3874 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3952 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3875 | SCHEME_V->code = caar (SCHEME_V->code); |
3953 | SCHEME_V->code = caar (SCHEME_V->code); |
3876 | s_goto (OP_EVAL); |
3954 | s_goto (OP_EVAL); |
3877 | } |
|
|
3878 | } |
3955 | } |
3879 | |
3956 | |
3880 | case OP_DELAY: /* delay */ |
3957 | case OP_DELAY: /* delay */ |
3881 | 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); |
3882 | set_typeflag (x, T_PROMISE); |
3959 | set_typeflag (x, T_PROMISE); |
… | |
… | |
3893 | case OP_AND1: /* and */ |
3970 | case OP_AND1: /* and */ |
3894 | if (is_false (SCHEME_V->value)) |
3971 | if (is_false (SCHEME_V->value)) |
3895 | s_return (SCHEME_V->value); |
3972 | s_return (SCHEME_V->value); |
3896 | else if (SCHEME_V->code == NIL) |
3973 | else if (SCHEME_V->code == NIL) |
3897 | s_return (SCHEME_V->value); |
3974 | s_return (SCHEME_V->value); |
3898 | else |
3975 | |
3899 | { |
|
|
3900 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3976 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3901 | SCHEME_V->code = car (SCHEME_V->code); |
3977 | SCHEME_V->code = car (SCHEME_V->code); |
3902 | s_goto (OP_EVAL); |
3978 | s_goto (OP_EVAL); |
3903 | } |
|
|
3904 | |
3979 | |
3905 | case OP_OR0: /* or */ |
3980 | case OP_OR0: /* or */ |
3906 | if (SCHEME_V->code == NIL) |
3981 | if (SCHEME_V->code == NIL) |
3907 | s_return (S_F); |
3982 | s_return (S_F); |
3908 | |
3983 | |
… | |
… | |
3913 | case OP_OR1: /* or */ |
3988 | case OP_OR1: /* or */ |
3914 | if (is_true (SCHEME_V->value)) |
3989 | if (is_true (SCHEME_V->value)) |
3915 | s_return (SCHEME_V->value); |
3990 | s_return (SCHEME_V->value); |
3916 | else if (SCHEME_V->code == NIL) |
3991 | else if (SCHEME_V->code == NIL) |
3917 | s_return (SCHEME_V->value); |
3992 | s_return (SCHEME_V->value); |
3918 | else |
3993 | |
3919 | { |
|
|
3920 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3994 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3921 | SCHEME_V->code = car (SCHEME_V->code); |
3995 | SCHEME_V->code = car (SCHEME_V->code); |
3922 | s_goto (OP_EVAL); |
3996 | s_goto (OP_EVAL); |
3923 | } |
|
|
3924 | |
3997 | |
3925 | case OP_C0STREAM: /* cons-stream */ |
3998 | case OP_C0STREAM: /* cons-stream */ |
3926 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3999 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3927 | SCHEME_V->code = car (SCHEME_V->code); |
4000 | SCHEME_V->code = car (SCHEME_V->code); |
3928 | s_goto (OP_EVAL); |
4001 | s_goto (OP_EVAL); |
… | |
… | |
3993 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
4066 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
3994 | SCHEME_V->code = caar (x); |
4067 | SCHEME_V->code = caar (x); |
3995 | s_goto (OP_EVAL); |
4068 | s_goto (OP_EVAL); |
3996 | } |
4069 | } |
3997 | } |
4070 | } |
3998 | else |
4071 | |
3999 | s_return (NIL); |
4072 | s_return (NIL); |
4000 | |
4073 | |
4001 | case OP_CASE2: /* case */ |
4074 | case OP_CASE2: /* case */ |
4002 | if (is_true (SCHEME_V->value)) |
4075 | if (is_true (SCHEME_V->value)) |
4003 | s_goto (OP_BEGIN); |
4076 | s_goto (OP_BEGIN); |
4004 | else |
4077 | |
4005 | s_return (NIL); |
4078 | s_return (NIL); |
4006 | |
4079 | |
4007 | case OP_PAPPLY: /* apply */ |
4080 | case OP_PAPPLY: /* apply */ |
4008 | SCHEME_V->code = car (args); |
4081 | SCHEME_V->code = car (args); |
4009 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
4082 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
4010 | /*SCHEME_V->args = cadr(args); */ |
4083 | /*SCHEME_V->args = cadr(args); */ |
… | |
… | |
4024 | } |
4097 | } |
4025 | |
4098 | |
4026 | if (USE_ERROR_CHECKING) abort (); |
4099 | if (USE_ERROR_CHECKING) abort (); |
4027 | } |
4100 | } |
4028 | |
4101 | |
4029 | static int |
4102 | /* math, cxr */ |
|
|
4103 | ecb_hot static int |
4030 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4104 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4031 | { |
4105 | { |
4032 | pointer args = SCHEME_V->args; |
4106 | pointer args = SCHEME_V->args; |
4033 | pointer x = car (args); |
4107 | pointer x = car (args); |
4034 | num v; |
4108 | num v; |
4035 | |
4109 | |
4036 | switch (op) |
4110 | switch (op) |
4037 | { |
4111 | { |
4038 | #if USE_MATH |
4112 | #if USE_MATH |
4039 | case OP_INEX2EX: /* inexact->exact */ |
4113 | case OP_INEX2EX: /* inexact->exact */ |
4040 | { |
|
|
4041 | if (is_integer (x)) |
4114 | if (!is_integer (x)) |
4042 | s_return (x); |
4115 | { |
4043 | |
|
|
4044 | RVALUE r = rvalue_unchecked (x); |
4116 | RVALUE r = rvalue_unchecked (x); |
4045 | |
4117 | |
4046 | if (r == (RVALUE)(IVALUE)r) |
4118 | if (r == (RVALUE)(IVALUE)r) |
4047 | s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); |
4119 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
4048 | else |
4120 | else |
4049 | Error_1 ("inexact->exact: not integral:", x); |
4121 | Error_1 ("inexact->exact: not integral:", x); |
4050 | } |
4122 | } |
4051 | |
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)))); |
4052 | 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)))); |
4053 | 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)))))); |
4054 | 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)))); |
4055 | 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)))); |
4056 | 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)))); |
4057 | 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)))); |
4058 | 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)))); |
4059 | |
4140 | |
4060 | case OP_ATAN: |
4141 | case OP_ATAN: |
|
|
4142 | s_return (mk_real (SCHEME_A_ |
4061 | if (cdr (args) == NIL) |
4143 | cdr (args) == NIL |
4062 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4144 | ? atan (rvalue (x)) |
4063 | else |
4145 | : atan2 (rvalue (x), rvalue (cadr (args))))); |
4064 | { |
|
|
4065 | pointer y = cadr (args); |
|
|
4066 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
|
|
4067 | } |
|
|
4068 | |
|
|
4069 | case OP_SQRT: |
|
|
4070 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
|
|
4071 | |
4146 | |
4072 | case OP_EXPT: |
4147 | case OP_EXPT: |
4073 | { |
4148 | { |
4074 | RVALUE result; |
4149 | RVALUE result; |
4075 | int real_result = 1; |
4150 | int real_result = 1; |
… | |
… | |
4098 | if (real_result) |
4173 | if (real_result) |
4099 | s_return (mk_real (SCHEME_A_ result)); |
4174 | s_return (mk_real (SCHEME_A_ result)); |
4100 | else |
4175 | else |
4101 | s_return (mk_integer (SCHEME_A_ result)); |
4176 | s_return (mk_integer (SCHEME_A_ result)); |
4102 | } |
4177 | } |
4103 | |
|
|
4104 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4105 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4106 | |
|
|
4107 | case OP_TRUNCATE: |
|
|
4108 | { |
|
|
4109 | RVALUE n = rvalue (x); |
|
|
4110 | s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n))); |
|
|
4111 | } |
|
|
4112 | |
|
|
4113 | case OP_ROUND: |
|
|
4114 | if (is_integer (x)) |
|
|
4115 | s_return (x); |
|
|
4116 | |
|
|
4117 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
|
|
4118 | #endif |
4178 | #endif |
4119 | |
4179 | |
4120 | case OP_ADD: /* + */ |
4180 | case OP_ADD: /* + */ |
4121 | v = num_zero; |
4181 | v = num_zero; |
4122 | |
4182 | |
… | |
… | |
4424 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4484 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4425 | |
4485 | |
4426 | s_return (newstr); |
4486 | s_return (newstr); |
4427 | } |
4487 | } |
4428 | |
4488 | |
4429 | case OP_SUBSTR: /* substring */ |
4489 | case OP_STRING_COPY: /* substring/string-copy */ |
4430 | { |
4490 | { |
4431 | char *str = strvalue (x); |
4491 | char *str = strvalue (x); |
4432 | int index0 = ivalue_unchecked (cadr (args)); |
4492 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4433 | int index1; |
4493 | int index1; |
4434 | int len; |
4494 | int len; |
4435 | |
4495 | |
4436 | if (index0 > strlength (x)) |
4496 | if (index0 > strlength (x)) |
4437 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4497 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4438 | |
4498 | |
4439 | if (cddr (args) != NIL) |
4499 | if (cddr (args) != NIL) |
4440 | { |
4500 | { |
4441 | index1 = ivalue_unchecked (caddr (args)); |
4501 | index1 = ivalue_unchecked (caddr (args)); |
4442 | |
4502 | |
4443 | if (index1 > strlength (x) || index1 < index0) |
4503 | if (index1 > strlength (x) || index1 < index0) |
4444 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4504 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4445 | } |
4505 | } |
4446 | else |
4506 | else |
4447 | index1 = strlength (x); |
4507 | index1 = strlength (x); |
4448 | |
4508 | |
4449 | len = index1 - index0; |
4509 | len = index1 - index0; |
4450 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4510 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4451 | memcpy (strvalue (x), str + index0, len); |
|
|
4452 | strvalue (x)[len] = 0; |
|
|
4453 | |
4511 | |
4454 | s_return (x); |
4512 | s_return (x); |
4455 | } |
4513 | } |
4456 | |
4514 | |
4457 | case OP_VECTOR: /* vector */ |
4515 | case OP_VECTOR: /* vector */ |
… | |
… | |
4531 | } |
4589 | } |
4532 | |
4590 | |
4533 | if (USE_ERROR_CHECKING) abort (); |
4591 | if (USE_ERROR_CHECKING) abort (); |
4534 | } |
4592 | } |
4535 | |
4593 | |
4536 | static int |
4594 | /* relational ops */ |
|
|
4595 | ecb_hot static int |
4537 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4596 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4538 | { |
4597 | { |
4539 | pointer x = SCHEME_V->args; |
4598 | pointer x = SCHEME_V->args; |
4540 | |
4599 | |
4541 | for (;;) |
4600 | for (;;) |
… | |
… | |
4562 | } |
4621 | } |
4563 | |
4622 | |
4564 | s_return (S_T); |
4623 | s_return (S_T); |
4565 | } |
4624 | } |
4566 | |
4625 | |
4567 | static int |
4626 | /* predicates */ |
|
|
4627 | ecb_hot static int |
4568 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4628 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4569 | { |
4629 | { |
4570 | pointer args = SCHEME_V->args; |
4630 | pointer args = SCHEME_V->args; |
4571 | pointer a = car (args); |
4631 | pointer a = car (args); |
4572 | pointer d = cdr (args); |
4632 | pointer d = cdr (args); |
… | |
… | |
4619 | } |
4679 | } |
4620 | |
4680 | |
4621 | s_retbool (r); |
4681 | s_retbool (r); |
4622 | } |
4682 | } |
4623 | |
4683 | |
4624 | static int |
4684 | /* promises, list ops, ports */ |
|
|
4685 | ecb_hot static int |
4625 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4686 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4626 | { |
4687 | { |
4627 | pointer args = SCHEME_V->args; |
4688 | pointer args = SCHEME_V->args; |
4628 | pointer a = car (args); |
4689 | pointer a = car (args); |
4629 | pointer x, y; |
4690 | pointer x, y; |
… | |
… | |
4642 | } |
4703 | } |
4643 | else |
4704 | else |
4644 | s_return (SCHEME_V->code); |
4705 | s_return (SCHEME_V->code); |
4645 | |
4706 | |
4646 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4707 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4647 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4708 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4648 | s_return (SCHEME_V->value); |
4709 | s_return (SCHEME_V->value); |
4649 | |
4710 | |
4650 | #if USE_PORTS |
4711 | #if USE_PORTS |
|
|
4712 | |
|
|
4713 | case OP_EOF_OBJECT: /* eof-object */ |
|
|
4714 | s_return (S_EOF); |
4651 | |
4715 | |
4652 | case OP_WRITE: /* write */ |
4716 | case OP_WRITE: /* write */ |
4653 | case OP_DISPLAY: /* display */ |
4717 | case OP_DISPLAY: /* display */ |
4654 | case OP_WRITE_CHAR: /* write-char */ |
4718 | case OP_WRITE_CHAR: /* write-char */ |
4655 | if (is_pair (cdr (SCHEME_V->args))) |
4719 | if (is_pair (cdr (SCHEME_V->args))) |
… | |
… | |
4669 | else |
4733 | else |
4670 | SCHEME_V->print_flag = 0; |
4734 | SCHEME_V->print_flag = 0; |
4671 | |
4735 | |
4672 | s_goto (OP_P0LIST); |
4736 | s_goto (OP_P0LIST); |
4673 | |
4737 | |
|
|
4738 | //TODO: move to scheme |
4674 | case OP_NEWLINE: /* newline */ |
4739 | case OP_NEWLINE: /* newline */ |
4675 | if (is_pair (args)) |
4740 | if (is_pair (args)) |
4676 | { |
4741 | { |
4677 | if (a != SCHEME_V->outport) |
4742 | if (a != SCHEME_V->outport) |
4678 | { |
4743 | { |
… | |
… | |
4680 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4745 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4681 | SCHEME_V->outport = a; |
4746 | SCHEME_V->outport = a; |
4682 | } |
4747 | } |
4683 | } |
4748 | } |
4684 | |
4749 | |
4685 | putstr (SCHEME_A_ "\n"); |
4750 | putcharacter (SCHEME_A_ '\n'); |
4686 | s_return (S_T); |
4751 | s_return (S_T); |
4687 | #endif |
4752 | #endif |
4688 | |
4753 | |
4689 | case OP_ERR0: /* error */ |
4754 | case OP_ERR0: /* error */ |
4690 | SCHEME_V->retcode = -1; |
4755 | SCHEME_V->retcode = -1; |
… | |
… | |
4699 | putstr (SCHEME_A_ strvalue (car (args))); |
4764 | putstr (SCHEME_A_ strvalue (car (args))); |
4700 | SCHEME_V->args = cdr (args); |
4765 | SCHEME_V->args = cdr (args); |
4701 | s_goto (OP_ERR1); |
4766 | s_goto (OP_ERR1); |
4702 | |
4767 | |
4703 | case OP_ERR1: /* error */ |
4768 | case OP_ERR1: /* error */ |
4704 | putstr (SCHEME_A_ " "); |
4769 | putcharacter (SCHEME_A_ ' '); |
4705 | |
4770 | |
4706 | if (args != NIL) |
4771 | if (args != NIL) |
4707 | { |
4772 | { |
4708 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4773 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4709 | SCHEME_V->args = a; |
4774 | SCHEME_V->args = a; |
4710 | SCHEME_V->print_flag = 1; |
4775 | SCHEME_V->print_flag = 1; |
4711 | s_goto (OP_P0LIST); |
4776 | s_goto (OP_P0LIST); |
4712 | } |
4777 | } |
4713 | else |
4778 | else |
4714 | { |
4779 | { |
4715 | putstr (SCHEME_A_ "\n"); |
4780 | putcharacter (SCHEME_A_ '\n'); |
4716 | |
4781 | |
4717 | if (SCHEME_V->interactive_repl) |
4782 | if (SCHEME_V->interactive_repl) |
4718 | s_goto (OP_T0LVL); |
4783 | s_goto (OP_T0LVL); |
4719 | else |
4784 | else |
4720 | return -1; |
4785 | return -1; |
… | |
… | |
4797 | SCHEME_V->gc_verbose = (a != S_F); |
4862 | SCHEME_V->gc_verbose = (a != S_F); |
4798 | s_retbool (was); |
4863 | s_retbool (was); |
4799 | } |
4864 | } |
4800 | |
4865 | |
4801 | case OP_NEWSEGMENT: /* new-segment */ |
4866 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4867 | #if 0 |
4802 | if (!is_pair (args) || !is_number (a)) |
4868 | if (!is_pair (args) || !is_number (a)) |
4803 | Error_0 ("new-segment: argument must be a number"); |
4869 | Error_0 ("new-segment: argument must be a number"); |
4804 | |
4870 | #endif |
4805 | alloc_cellseg (SCHEME_A_ ivalue (a)); |
4871 | s_retbool (alloc_cellseg (SCHEME_A)); |
4806 | |
|
|
4807 | s_return (S_T); |
|
|
4808 | |
4872 | |
4809 | case OP_OBLIST: /* oblist */ |
4873 | case OP_OBLIST: /* oblist */ |
4810 | s_return (oblist_all_symbols (SCHEME_A)); |
4874 | s_return (oblist_all_symbols (SCHEME_A)); |
4811 | |
4875 | |
4812 | #if USE_PORTS |
4876 | #if USE_PORTS |
… | |
… | |
4882 | s_return (p == NIL ? S_F : p); |
4946 | s_return (p == NIL ? S_F : p); |
4883 | } |
4947 | } |
4884 | |
4948 | |
4885 | case OP_GET_OUTSTRING: /* get-output-string */ |
4949 | case OP_GET_OUTSTRING: /* get-output-string */ |
4886 | { |
4950 | { |
4887 | port *p; |
4951 | port *p = port (a); |
4888 | |
4952 | |
4889 | if ((p = a->object.port)->kind & port_string) |
4953 | if (p->kind & port_string) |
4890 | { |
4954 | { |
4891 | off_t size; |
4955 | off_t size; |
4892 | char *str; |
4956 | char *str; |
4893 | |
4957 | |
4894 | size = p->rep.string.curr - p->rep.string.start + 1; |
4958 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4929 | } |
4993 | } |
4930 | |
4994 | |
4931 | if (USE_ERROR_CHECKING) abort (); |
4995 | if (USE_ERROR_CHECKING) abort (); |
4932 | } |
4996 | } |
4933 | |
4997 | |
4934 | static int |
4998 | /* reading */ |
|
|
4999 | ecb_cold static int |
4935 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
5000 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4936 | { |
5001 | { |
4937 | pointer args = SCHEME_V->args; |
5002 | pointer args = SCHEME_V->args; |
4938 | pointer x; |
5003 | pointer x; |
4939 | |
5004 | |
… | |
… | |
4999 | int res; |
5064 | int res; |
5000 | |
5065 | |
5001 | if (is_pair (args)) |
5066 | if (is_pair (args)) |
5002 | p = car (args); |
5067 | p = car (args); |
5003 | |
5068 | |
5004 | res = p->object.port->kind & port_string; |
5069 | res = port (p)->kind & port_string; |
5005 | |
5070 | |
5006 | s_retbool (res); |
5071 | s_retbool (res); |
5007 | } |
5072 | } |
5008 | |
5073 | |
5009 | case OP_SET_INPORT: /* set-input-port */ |
5074 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
5208 | pointer b = cdr (args); |
5273 | pointer b = cdr (args); |
5209 | int ok_abbr = ok_abbrev (b); |
5274 | int ok_abbr = ok_abbrev (b); |
5210 | SCHEME_V->args = car (b); |
5275 | SCHEME_V->args = car (b); |
5211 | |
5276 | |
5212 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5277 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5213 | putstr (SCHEME_A_ "'"); |
5278 | putcharacter (SCHEME_A_ '\''); |
5214 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5279 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5215 | putstr (SCHEME_A_ "`"); |
5280 | putcharacter (SCHEME_A_ '`'); |
5216 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5281 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5217 | putstr (SCHEME_A_ ","); |
5282 | putcharacter (SCHEME_A_ ','); |
5218 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5283 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5219 | putstr (SCHEME_A_ ",@"); |
5284 | putstr (SCHEME_A_ ",@"); |
5220 | else |
5285 | else |
5221 | { |
5286 | { |
5222 | putstr (SCHEME_A_ "("); |
5287 | putcharacter (SCHEME_A_ '('); |
5223 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5288 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5224 | SCHEME_V->args = a; |
5289 | SCHEME_V->args = a; |
5225 | } |
5290 | } |
5226 | |
5291 | |
5227 | s_goto (OP_P0LIST); |
5292 | s_goto (OP_P0LIST); |
… | |
… | |
5229 | |
5294 | |
5230 | case OP_P1LIST: |
5295 | case OP_P1LIST: |
5231 | if (is_pair (args)) |
5296 | if (is_pair (args)) |
5232 | { |
5297 | { |
5233 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5298 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5234 | putstr (SCHEME_A_ " "); |
5299 | putcharacter (SCHEME_A_ ' '); |
5235 | SCHEME_V->args = car (args); |
5300 | SCHEME_V->args = car (args); |
5236 | s_goto (OP_P0LIST); |
5301 | s_goto (OP_P0LIST); |
5237 | } |
5302 | } |
5238 | else if (is_vector (args)) |
5303 | else if (is_vector (args)) |
5239 | { |
5304 | { |
… | |
… | |
5247 | { |
5312 | { |
5248 | putstr (SCHEME_A_ " . "); |
5313 | putstr (SCHEME_A_ " . "); |
5249 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5314 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5250 | } |
5315 | } |
5251 | |
5316 | |
5252 | putstr (SCHEME_A_ ")"); |
5317 | putcharacter (SCHEME_A_ ')'); |
5253 | s_return (S_T); |
5318 | s_return (S_T); |
5254 | } |
5319 | } |
5255 | |
5320 | |
5256 | case OP_PVECFROM: |
5321 | case OP_PVECFROM: |
5257 | { |
5322 | { |
… | |
… | |
5259 | pointer vec = car (args); |
5324 | pointer vec = car (args); |
5260 | int len = veclength (vec); |
5325 | int len = veclength (vec); |
5261 | |
5326 | |
5262 | if (i == len) |
5327 | if (i == len) |
5263 | { |
5328 | { |
5264 | putstr (SCHEME_A_ ")"); |
5329 | putcharacter (SCHEME_A_ ')'); |
5265 | s_return (S_T); |
5330 | s_return (S_T); |
5266 | } |
5331 | } |
5267 | else |
5332 | else |
5268 | { |
5333 | { |
5269 | pointer elem = vector_get (vec, i); |
5334 | pointer elem = vector_get (vec, i); |
… | |
… | |
5271 | ivalue_unchecked (cdr (args)) = i + 1; |
5336 | ivalue_unchecked (cdr (args)) = i + 1; |
5272 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5337 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5273 | SCHEME_V->args = elem; |
5338 | SCHEME_V->args = elem; |
5274 | |
5339 | |
5275 | if (i > 0) |
5340 | if (i > 0) |
5276 | putstr (SCHEME_A_ " "); |
5341 | putcharacter (SCHEME_A_ ' '); |
5277 | |
5342 | |
5278 | s_goto (OP_P0LIST); |
5343 | s_goto (OP_P0LIST); |
5279 | } |
5344 | } |
5280 | } |
5345 | } |
5281 | } |
5346 | } |
5282 | |
5347 | |
5283 | if (USE_ERROR_CHECKING) abort (); |
5348 | if (USE_ERROR_CHECKING) abort (); |
5284 | } |
5349 | } |
5285 | |
5350 | |
5286 | static int |
5351 | /* list ops */ |
|
|
5352 | ecb_hot static int |
5287 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5353 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5288 | { |
5354 | { |
5289 | pointer args = SCHEME_V->args; |
5355 | pointer args = SCHEME_V->args; |
5290 | pointer a = car (args); |
5356 | pointer a = car (args); |
5291 | pointer x, y; |
5357 | pointer x, y; |
… | |
… | |
5349 | |
5415 | |
5350 | /* 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 */ |
5351 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5417 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5352 | |
5418 | |
5353 | typedef int (*test_predicate)(pointer); |
5419 | typedef int (*test_predicate)(pointer); |
5354 | static int |
5420 | |
|
|
5421 | ecb_hot static int |
5355 | tst_any (pointer p) |
5422 | tst_any (pointer p) |
5356 | { |
5423 | { |
5357 | return 1; |
5424 | return 1; |
5358 | } |
5425 | } |
5359 | |
5426 | |
5360 | static int |
5427 | ecb_hot static int |
5361 | tst_inonneg (pointer p) |
5428 | tst_inonneg (pointer p) |
5362 | { |
5429 | { |
5363 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5430 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5364 | } |
5431 | } |
5365 | |
5432 | |
5366 | static int |
5433 | ecb_hot static int |
5367 | tst_is_list (SCHEME_P_ pointer p) |
5434 | tst_is_list (SCHEME_P_ pointer p) |
5368 | { |
5435 | { |
5369 | return p == NIL || is_pair (p); |
5436 | return p == NIL || is_pair (p); |
5370 | } |
5437 | } |
5371 | |
5438 | |
… | |
… | |
5414 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5481 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5415 | #include "opdefines.h" |
5482 | #include "opdefines.h" |
5416 | #undef OP_DEF |
5483 | #undef OP_DEF |
5417 | ; |
5484 | ; |
5418 | |
5485 | |
5419 | static const char * |
5486 | ecb_cold static const char * |
5420 | opname (int idx) |
5487 | opname (int idx) |
5421 | { |
5488 | { |
5422 | const char *name = opnames; |
5489 | const char *name = opnames; |
5423 | |
5490 | |
5424 | /* 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? */ |
… | |
… | |
5426 | name += strlen (name) + 1; |
5493 | name += strlen (name) + 1; |
5427 | |
5494 | |
5428 | return *name ? name : "ILLEGAL"; |
5495 | return *name ? name : "ILLEGAL"; |
5429 | } |
5496 | } |
5430 | |
5497 | |
5431 | static const char * |
5498 | ecb_cold static const char * |
5432 | procname (pointer x) |
5499 | procname (pointer x) |
5433 | { |
5500 | { |
5434 | return opname (procnum (x)); |
5501 | return opname (procnum (x)); |
5435 | } |
5502 | } |
5436 | |
5503 | |
… | |
… | |
5456 | #undef OP_DEF |
5523 | #undef OP_DEF |
5457 | {0} |
5524 | {0} |
5458 | }; |
5525 | }; |
5459 | |
5526 | |
5460 | /* kernel of this interpreter */ |
5527 | /* kernel of this interpreter */ |
5461 | static void ecb_hot |
5528 | ecb_hot static void |
5462 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5529 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5463 | { |
5530 | { |
5464 | SCHEME_V->op = op; |
5531 | SCHEME_V->op = op; |
5465 | |
5532 | |
5466 | for (;;) |
5533 | for (;;) |
… | |
… | |
5549 | 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)) |
5550 | return; |
5617 | return; |
5551 | |
5618 | |
5552 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5619 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5553 | { |
5620 | { |
5554 | xwrstr ("No memory!\n"); |
5621 | putstr (SCHEME_A_ "No memory!\n"); |
5555 | return; |
5622 | return; |
5556 | } |
5623 | } |
5557 | } |
5624 | } |
5558 | } |
5625 | } |
5559 | |
5626 | |
5560 | /* ========== Initialization of internal keywords ========== */ |
5627 | /* ========== Initialization of internal keywords ========== */ |
5561 | |
5628 | |
5562 | static void |
5629 | ecb_cold static void |
5563 | assign_syntax (SCHEME_P_ const char *name) |
5630 | assign_syntax (SCHEME_P_ const char *name) |
5564 | { |
5631 | { |
5565 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5632 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5566 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5633 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5567 | } |
5634 | } |
5568 | |
5635 | |
5569 | static void |
5636 | ecb_cold static void |
5570 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5637 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5571 | { |
5638 | { |
5572 | pointer x = mk_symbol (SCHEME_A_ name); |
5639 | pointer x = mk_symbol (SCHEME_A_ name); |
5573 | pointer y = mk_proc (SCHEME_A_ op); |
5640 | pointer y = mk_proc (SCHEME_A_ op); |
5574 | new_slot_in_env (SCHEME_A_ x, y); |
5641 | new_slot_in_env (SCHEME_A_ x, y); |
… | |
… | |
5582 | ivalue_unchecked (y) = op; |
5649 | ivalue_unchecked (y) = op; |
5583 | return y; |
5650 | return y; |
5584 | } |
5651 | } |
5585 | |
5652 | |
5586 | /* 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! */ |
5587 | static int |
5654 | ecb_hot static int |
5588 | syntaxnum (pointer p) |
5655 | syntaxnum (pointer p) |
5589 | { |
5656 | { |
5590 | const char *s = strvalue (p); |
5657 | const char *s = strvalue (p); |
5591 | |
5658 | |
5592 | switch (strlength (p)) |
5659 | switch (strlength (p)) |
… | |
… | |
5699 | SCHEME_V->save_inport = NIL; |
5766 | SCHEME_V->save_inport = NIL; |
5700 | SCHEME_V->loadport = NIL; |
5767 | SCHEME_V->loadport = NIL; |
5701 | SCHEME_V->nesting = 0; |
5768 | SCHEME_V->nesting = 0; |
5702 | SCHEME_V->interactive_repl = 0; |
5769 | SCHEME_V->interactive_repl = 0; |
5703 | |
5770 | |
5704 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5771 | if (!alloc_cellseg (SCHEME_A)) |
5705 | { |
5772 | { |
5706 | #if USE_ERROR_CHECKING |
5773 | #if USE_ERROR_CHECKING |
5707 | SCHEME_V->no_memory = 1; |
5774 | SCHEME_V->no_memory = 1; |
5708 | return 0; |
5775 | return 0; |
5709 | #endif |
5776 | #endif |
5710 | } |
5777 | } |
5711 | |
5778 | |
5712 | SCHEME_V->gc_verbose = 0; |
5779 | SCHEME_V->gc_verbose = 0; |
5713 | dump_stack_initialize (SCHEME_A); |
5780 | dump_stack_initialize (SCHEME_A); |
5714 | SCHEME_V->code = NIL; |
5781 | SCHEME_V->code = NIL; |
5715 | SCHEME_V->args = NIL; |
5782 | SCHEME_V->args = NIL; |
5716 | SCHEME_V->envir = NIL; |
5783 | SCHEME_V->envir = NIL; |
|
|
5784 | SCHEME_V->value = NIL; |
5717 | SCHEME_V->tracing = 0; |
5785 | SCHEME_V->tracing = 0; |
5718 | |
5786 | |
5719 | /* init NIL */ |
5787 | /* init NIL */ |
5720 | set_typeflag (NIL, T_ATOM | T_MARK); |
5788 | set_typeflag (NIL, T_ATOM | T_MARK); |
5721 | set_car (NIL, NIL); |
5789 | set_car (NIL, NIL); |
… | |
… | |
5777 | |
5845 | |
5778 | return !SCHEME_V->no_memory; |
5846 | return !SCHEME_V->no_memory; |
5779 | } |
5847 | } |
5780 | |
5848 | |
5781 | #if USE_PORTS |
5849 | #if USE_PORTS |
5782 | void |
5850 | ecb_cold void |
5783 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5851 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5784 | { |
5852 | { |
5785 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5853 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5786 | } |
5854 | } |
5787 | |
5855 | |
5788 | void |
5856 | ecb_cold void |
5789 | 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) |
5790 | { |
5858 | { |
5791 | 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); |
5792 | } |
5860 | } |
5793 | |
5861 | |
5794 | void |
5862 | ecb_cold void |
5795 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5863 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5796 | { |
5864 | { |
5797 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5865 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5798 | } |
5866 | } |
5799 | |
5867 | |
5800 | void |
5868 | ecb_cold void |
5801 | 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) |
5802 | { |
5870 | { |
5803 | 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); |
5804 | } |
5872 | } |
5805 | #endif |
5873 | #endif |
5806 | |
5874 | |
5807 | void |
5875 | ecb_cold void |
5808 | scheme_set_external_data (SCHEME_P_ void *p) |
5876 | scheme_set_external_data (SCHEME_P_ void *p) |
5809 | { |
5877 | { |
5810 | SCHEME_V->ext_data = p; |
5878 | SCHEME_V->ext_data = p; |
5811 | } |
5879 | } |
5812 | |
5880 | |
… | |
… | |
5860 | } |
5928 | } |
5861 | } |
5929 | } |
5862 | #endif |
5930 | #endif |
5863 | } |
5931 | } |
5864 | |
5932 | |
5865 | void |
5933 | ecb_cold void |
5866 | scheme_load_file (SCHEME_P_ int fin) |
5934 | scheme_load_file (SCHEME_P_ int fin) |
5867 | { |
5935 | { |
5868 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5936 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5869 | } |
5937 | } |
5870 | |
5938 | |
5871 | void |
5939 | ecb_cold void |
5872 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5940 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5873 | { |
5941 | { |
5874 | dump_stack_reset (SCHEME_A); |
5942 | dump_stack_reset (SCHEME_A); |
5875 | SCHEME_V->envir = SCHEME_V->global_env; |
5943 | SCHEME_V->envir = SCHEME_V->global_env; |
5876 | SCHEME_V->file_i = 0; |
5944 | SCHEME_V->file_i = 0; |
5877 | SCHEME_V->load_stack[0].unget = -1; |
5945 | SCHEME_V->load_stack[0].unget = -1; |
5878 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5946 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5879 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5947 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5880 | #if USE_PORTS |
|
|
5881 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5948 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5882 | #endif |
|
|
5883 | SCHEME_V->retcode = 0; |
5949 | SCHEME_V->retcode = 0; |
5884 | |
5950 | |
5885 | #if USE_PORTS |
|
|
5886 | if (fin == STDIN_FILENO) |
5951 | if (fin == STDIN_FILENO) |
5887 | SCHEME_V->interactive_repl = 1; |
5952 | SCHEME_V->interactive_repl = 1; |
5888 | #endif |
|
|
5889 | |
5953 | |
5890 | #if USE_PORTS |
5954 | #if USE_PORTS |
5891 | #if SHOW_ERROR_LINE |
5955 | #if SHOW_ERROR_LINE |
5892 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5956 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5893 | |
5957 | |
… | |
… | |
5897 | #endif |
5961 | #endif |
5898 | |
5962 | |
5899 | SCHEME_V->inport = SCHEME_V->loadport; |
5963 | SCHEME_V->inport = SCHEME_V->loadport; |
5900 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5964 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5901 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5965 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
|
|
5966 | |
5902 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5967 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5903 | |
5968 | |
5904 | if (SCHEME_V->retcode == 0) |
5969 | if (SCHEME_V->retcode == 0) |
5905 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5970 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5906 | } |
5971 | } |
5907 | |
5972 | |
5908 | void |
5973 | ecb_cold void |
5909 | scheme_load_string (SCHEME_P_ const char *cmd) |
5974 | scheme_load_string (SCHEME_P_ const char *cmd) |
5910 | { |
5975 | { |
|
|
5976 | #if USE_PORTs |
5911 | dump_stack_reset (SCHEME_A); |
5977 | dump_stack_reset (SCHEME_A); |
5912 | SCHEME_V->envir = SCHEME_V->global_env; |
5978 | SCHEME_V->envir = SCHEME_V->global_env; |
5913 | SCHEME_V->file_i = 0; |
5979 | SCHEME_V->file_i = 0; |
5914 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5980 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5915 | 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 */ |
5916 | 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); |
5917 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5983 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5918 | #if USE_PORTS |
|
|
5919 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5984 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5920 | #endif |
|
|
5921 | SCHEME_V->retcode = 0; |
5985 | SCHEME_V->retcode = 0; |
5922 | SCHEME_V->interactive_repl = 0; |
5986 | SCHEME_V->interactive_repl = 0; |
5923 | SCHEME_V->inport = SCHEME_V->loadport; |
5987 | SCHEME_V->inport = SCHEME_V->loadport; |
5924 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5988 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5925 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5989 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5926 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5990 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5927 | |
5991 | |
5928 | if (SCHEME_V->retcode == 0) |
5992 | if (SCHEME_V->retcode == 0) |
5929 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5993 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
|
|
5994 | #else |
|
|
5995 | abort (); |
|
|
5996 | #endif |
5930 | } |
5997 | } |
5931 | |
5998 | |
5932 | void |
5999 | ecb_cold void |
5933 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
6000 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5934 | { |
6001 | { |
5935 | pointer x; |
6002 | pointer x; |
5936 | |
6003 | |
5937 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
6004 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
… | |
… | |
5942 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
6009 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5943 | } |
6010 | } |
5944 | |
6011 | |
5945 | #if !STANDALONE |
6012 | #if !STANDALONE |
5946 | |
6013 | |
5947 | void |
6014 | ecb_cold void |
5948 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
6015 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5949 | { |
6016 | { |
5950 | 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)); |
5951 | } |
6018 | } |
5952 | |
6019 | |
5953 | void |
6020 | ecb_cold void |
5954 | 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) |
5955 | { |
6022 | { |
5956 | int i; |
6023 | int i; |
5957 | |
6024 | |
5958 | for (i = 0; i < count; i++) |
6025 | for (i = 0; i < count; i++) |
5959 | scheme_register_foreign_func (SCHEME_A_ list + i); |
6026 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5960 | } |
6027 | } |
5961 | |
6028 | |
5962 | pointer |
6029 | ecb_cold pointer |
5963 | scheme_apply0 (SCHEME_P_ const char *procname) |
6030 | scheme_apply0 (SCHEME_P_ const char *procname) |
5964 | { |
6031 | { |
5965 | 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)); |
5966 | } |
6033 | } |
5967 | |
6034 | |
5968 | void |
6035 | ecb_cold void |
5969 | save_from_C_call (SCHEME_P) |
6036 | save_from_C_call (SCHEME_P) |
5970 | { |
6037 | { |
5971 | pointer saved_data = cons (car (S_SINK), |
6038 | pointer saved_data = cons (car (S_SINK), |
5972 | cons (SCHEME_V->envir, |
6039 | cons (SCHEME_V->envir, |
5973 | SCHEME_V->dump)); |
6040 | SCHEME_V->dump)); |
… | |
… | |
5977 | /* 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 |
5978 | directly resume pre-C-call operations. */ |
6045 | directly resume pre-C-call operations. */ |
5979 | dump_stack_reset (SCHEME_A); |
6046 | dump_stack_reset (SCHEME_A); |
5980 | } |
6047 | } |
5981 | |
6048 | |
5982 | void |
6049 | ecb_cold void |
5983 | restore_from_C_call (SCHEME_P) |
6050 | restore_from_C_call (SCHEME_P) |
5984 | { |
6051 | { |
5985 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
6052 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5986 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
6053 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5987 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
6054 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5988 | /* Pop */ |
6055 | /* Pop */ |
5989 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
6056 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5990 | } |
6057 | } |
5991 | |
6058 | |
5992 | /* "func" and "args" are assumed to be already eval'ed. */ |
6059 | /* "func" and "args" are assumed to be already eval'ed. */ |
5993 | pointer |
6060 | ecb_cold pointer |
5994 | scheme_call (SCHEME_P_ pointer func, pointer args) |
6061 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5995 | { |
6062 | { |
5996 | int old_repl = SCHEME_V->interactive_repl; |
6063 | int old_repl = SCHEME_V->interactive_repl; |
5997 | |
6064 | |
5998 | SCHEME_V->interactive_repl = 0; |
6065 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
6005 | SCHEME_V->interactive_repl = old_repl; |
6072 | SCHEME_V->interactive_repl = old_repl; |
6006 | restore_from_C_call (SCHEME_A); |
6073 | restore_from_C_call (SCHEME_A); |
6007 | return SCHEME_V->value; |
6074 | return SCHEME_V->value; |
6008 | } |
6075 | } |
6009 | |
6076 | |
6010 | pointer |
6077 | ecb_cold pointer |
6011 | scheme_eval (SCHEME_P_ pointer obj) |
6078 | scheme_eval (SCHEME_P_ pointer obj) |
6012 | { |
6079 | { |
6013 | int old_repl = SCHEME_V->interactive_repl; |
6080 | int old_repl = SCHEME_V->interactive_repl; |
6014 | |
6081 | |
6015 | SCHEME_V->interactive_repl = 0; |
6082 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
6027 | |
6094 | |
6028 | /* ========== Main ========== */ |
6095 | /* ========== Main ========== */ |
6029 | |
6096 | |
6030 | #if STANDALONE |
6097 | #if STANDALONE |
6031 | |
6098 | |
6032 | int |
6099 | ecb_cold int |
6033 | main (int argc, char **argv) |
6100 | main (int argc, char **argv) |
6034 | { |
6101 | { |
6035 | # if USE_MULTIPLICITY |
6102 | # if USE_MULTIPLICITY |
6036 | scheme ssc; |
6103 | scheme ssc; |
6037 | scheme *const SCHEME_V = &ssc; |
6104 | scheme *const SCHEME_V = &ssc; |
… | |
… | |
6043 | int isfile = 1; |
6110 | int isfile = 1; |
6044 | system ("ps v $PPID");//D |
6111 | system ("ps v $PPID");//D |
6045 | |
6112 | |
6046 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6113 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6047 | { |
6114 | { |
6048 | xwrstr ("Usage: tinyscheme -?\n"); |
6115 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
6049 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6116 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
6050 | xwrstr ("followed by\n"); |
6117 | putstr (SCHEME_A_ "followed by\n"); |
6051 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6118 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
6052 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6119 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6053 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6120 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
6054 | xwrstr ("Use - as filename for stdin.\n"); |
6121 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
6055 | return 1; |
6122 | return 1; |
6056 | } |
6123 | } |
6057 | |
6124 | |
6058 | if (!scheme_init (SCHEME_A)) |
6125 | if (!scheme_init (SCHEME_A)) |
6059 | { |
6126 | { |
6060 | xwrstr ("Could not initialize!\n"); |
6127 | putstr (SCHEME_A_ "Could not initialize!\n"); |
6061 | return 2; |
6128 | return 2; |
6062 | } |
6129 | } |
6063 | |
6130 | |
6064 | # if USE_PORTS |
6131 | # if USE_PORTS |
6065 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6132 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
6078 | } |
6145 | } |
6079 | #endif |
6146 | #endif |
6080 | |
6147 | |
6081 | do |
6148 | do |
6082 | { |
6149 | { |
6083 | #if USE_PORTS |
|
|
6084 | if (strcmp (file_name, "-") == 0) |
6150 | if (strcmp (file_name, "-") == 0) |
6085 | fin = STDIN_FILENO; |
6151 | fin = STDIN_FILENO; |
6086 | 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) |
6087 | { |
6153 | { |
6088 | pointer args = NIL; |
6154 | pointer args = NIL; |
… | |
… | |
6106 | 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); |
6107 | |
6173 | |
6108 | } |
6174 | } |
6109 | else |
6175 | else |
6110 | fin = open (file_name, O_RDONLY); |
6176 | fin = open (file_name, O_RDONLY); |
6111 | #endif |
|
|
6112 | |
6177 | |
6113 | if (isfile && fin < 0) |
6178 | if (isfile && fin < 0) |
6114 | { |
6179 | { |
6115 | 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'); |
6116 | } |
6183 | } |
6117 | else |
6184 | else |
6118 | { |
6185 | { |
6119 | if (isfile) |
6186 | if (isfile) |
6120 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6187 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6121 | else |
6188 | else |
6122 | scheme_load_string (SCHEME_A_ file_name); |
6189 | scheme_load_string (SCHEME_A_ file_name); |
6123 | |
6190 | |
6124 | #if USE_PORTS |
|
|
6125 | if (!isfile || fin != STDIN_FILENO) |
6191 | if (!isfile || fin != STDIN_FILENO) |
6126 | { |
6192 | { |
6127 | if (SCHEME_V->retcode != 0) |
6193 | if (SCHEME_V->retcode != 0) |
6128 | { |
6194 | { |
6129 | 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'); |
6130 | } |
6198 | } |
6131 | |
6199 | |
6132 | if (isfile) |
6200 | if (isfile) |
6133 | close (fin); |
6201 | close (fin); |
6134 | } |
6202 | } |
6135 | #endif |
|
|
6136 | } |
6203 | } |
6137 | |
6204 | |
6138 | file_name = *argv++; |
6205 | file_name = *argv++; |
6139 | } |
6206 | } |
6140 | while (file_name != 0); |
6207 | while (file_name != 0); |