… | |
… | |
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) |
91 | #define NIL POINTER (&SCHEME_V->xNIL) |
85 | #define S_T (&SCHEME_V->xT) |
92 | #define S_T POINTER (&SCHEME_V->xT) |
86 | #define S_F (&SCHEME_V->xF) |
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); |
701 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
710 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
702 | static pointer readstrexp (SCHEME_P_ char delim); |
711 | static pointer readstrexp (SCHEME_P_ char delim); |
703 | ecb_inline int skipspace (SCHEME_P); |
712 | static int skipspace (SCHEME_P); |
704 | static int token (SCHEME_P); |
713 | static int token (SCHEME_P); |
705 | static void printslashstring (SCHEME_P_ char *s, int len); |
714 | static void printslashstring (SCHEME_P_ char *s, int len); |
706 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
715 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
707 | static void printatom (SCHEME_P_ pointer l, int f); |
716 | static void printatom (SCHEME_P_ pointer l, int f); |
708 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
717 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
… | |
… | |
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; |
|
|
943 | |
921 | |
944 | newp = (pointer)cp; |
922 | newp = (struct cell *)cp; |
945 | SCHEME_V->cell_seg[i] = newp; |
923 | SCHEME_V->cell_seg[i] = newp; |
946 | SCHEME_V->cell_segsize[i] = segsize; |
924 | SCHEME_V->cell_segsize[i] = segsize; |
947 | SCHEME_V->fcells += segsize; |
925 | SCHEME_V->fcells += segsize; |
948 | last = newp + segsize - 1; |
926 | last = newp + segsize - 1; |
949 | |
927 | |
950 | for (p = newp; p <= last; p++) |
928 | for (p = newp; p <= last; p++) |
951 | { |
929 | { |
|
|
930 | pointer cp = POINTER (p); |
952 | set_typeflag (p, T_PAIR); |
931 | set_typeflag (cp, T_PAIR); |
953 | set_car (p, NIL); |
932 | set_car (cp, NIL); |
954 | set_cdr (p, p + 1); |
933 | set_cdr (cp, POINTER (p + 1)); |
955 | } |
934 | } |
956 | |
935 | |
957 | set_cdr (last, SCHEME_V->free_cell); |
936 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
958 | SCHEME_V->free_cell = newp; |
937 | SCHEME_V->free_cell = POINTER (newp); |
959 | } |
|
|
960 | |
938 | |
961 | return n; |
939 | return 1; |
962 | } |
940 | } |
963 | |
941 | |
964 | /* get new cell. parameter a, b is marked by gc. */ |
942 | /* get new cell. parameter a, b is marked by gc. */ |
965 | ecb_inline pointer |
943 | ecb_inline pointer |
966 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
944 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
… | |
… | |
970 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
948 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
971 | return S_SINK; |
949 | return S_SINK; |
972 | |
950 | |
973 | if (SCHEME_V->free_cell == NIL) |
951 | if (SCHEME_V->free_cell == NIL) |
974 | { |
952 | { |
975 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1; |
953 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 2; |
976 | |
954 | |
977 | gc (SCHEME_A_ a, b); |
955 | gc (SCHEME_A_ a, b); |
978 | |
956 | |
979 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
957 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
980 | { |
958 | { |
981 | /* if only a few recovered, get more to avoid fruitless gc's */ |
959 | /* if only a few recovered, get more to avoid fruitless gc's */ |
982 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
960 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
983 | { |
961 | { |
984 | #if USE_ERROR_CHECKING |
962 | #if USE_ERROR_CHECKING |
985 | SCHEME_V->no_memory = 1; |
963 | SCHEME_V->no_memory = 1; |
986 | return S_SINK; |
964 | return S_SINK; |
987 | #endif |
965 | #endif |
… | |
… | |
999 | } |
977 | } |
1000 | } |
978 | } |
1001 | |
979 | |
1002 | /* To retain recent allocs before interpreter knows about them - |
980 | /* To retain recent allocs before interpreter knows about them - |
1003 | Tehom */ |
981 | Tehom */ |
1004 | |
982 | ecb_hot static void |
1005 | static void |
|
|
1006 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
983 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
1007 | { |
984 | { |
1008 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
985 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1009 | |
986 | |
1010 | set_typeflag (holder, T_PAIR); |
987 | set_typeflag (holder, T_PAIR); |
… | |
… | |
1012 | set_car (holder, recent); |
989 | set_car (holder, recent); |
1013 | set_cdr (holder, car (S_SINK)); |
990 | set_cdr (holder, car (S_SINK)); |
1014 | set_car (S_SINK, holder); |
991 | set_car (S_SINK, holder); |
1015 | } |
992 | } |
1016 | |
993 | |
1017 | static pointer |
994 | ecb_hot static pointer |
1018 | get_cell (SCHEME_P_ pointer a, pointer b) |
995 | get_cell (SCHEME_P_ pointer a, pointer b) |
1019 | { |
996 | { |
1020 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
997 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
1021 | |
998 | |
1022 | /* For right now, include "a" and "b" in "cell" so that gc doesn't |
999 | /* For right now, include "a" and "b" in "cell" so that gc doesn't |
… | |
… | |
1040 | return S_SINK; |
1017 | return S_SINK; |
1041 | |
1018 | |
1042 | /* Record it as a vector so that gc understands it. */ |
1019 | /* Record it as a vector so that gc understands it. */ |
1043 | set_typeflag (v, T_VECTOR | T_ATOM); |
1020 | set_typeflag (v, T_VECTOR | T_ATOM); |
1044 | |
1021 | |
1045 | v->object.vector.vvalue = e; |
1022 | CELL(v)->object.vector.vvalue = e; |
1046 | v->object.vector.length = len; |
1023 | CELL(v)->object.vector.length = len; |
1047 | fill_vector (v, 0, init); |
1024 | fill_vector (v, 0, init); |
1048 | push_recent_alloc (SCHEME_A_ v, NIL); |
1025 | push_recent_alloc (SCHEME_A_ v, NIL); |
1049 | |
1026 | |
1050 | return v; |
1027 | return v; |
1051 | } |
1028 | } |
… | |
… | |
1060 | static void |
1037 | static void |
1061 | check_cell_alloced (pointer p, int expect_alloced) |
1038 | check_cell_alloced (pointer p, int expect_alloced) |
1062 | { |
1039 | { |
1063 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1040 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1064 | if (typeflag (p) & !expect_alloced) |
1041 | if (typeflag (p) & !expect_alloced) |
1065 | xwrstr ("Cell is already allocated!\n"); |
1042 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1066 | |
1043 | |
1067 | if (!(typeflag (p)) & expect_alloced) |
1044 | if (!(typeflag (p)) & expect_alloced) |
1068 | xwrstr ("Cell is not allocated!\n"); |
1045 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1069 | } |
1046 | } |
1070 | |
1047 | |
1071 | static void |
1048 | static void |
1072 | check_range_alloced (pointer p, int n, int expect_alloced) |
1049 | check_range_alloced (pointer p, int n, int expect_alloced) |
1073 | { |
1050 | { |
… | |
… | |
1079 | #endif |
1056 | #endif |
1080 | |
1057 | |
1081 | /* Medium level cell allocation */ |
1058 | /* Medium level cell allocation */ |
1082 | |
1059 | |
1083 | /* get new cons cell */ |
1060 | /* get new cons cell */ |
1084 | pointer |
1061 | ecb_hot static pointer |
1085 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1062 | xcons (SCHEME_P_ pointer a, pointer b) |
1086 | { |
1063 | { |
1087 | pointer x = get_cell (SCHEME_A_ a, b); |
1064 | pointer x = get_cell (SCHEME_A_ a, b); |
1088 | |
1065 | |
1089 | set_typeflag (x, T_PAIR); |
1066 | set_typeflag (x, T_PAIR); |
1090 | |
|
|
1091 | if (immutable) |
|
|
1092 | setimmutable (x); |
|
|
1093 | |
1067 | |
1094 | set_car (x, a); |
1068 | set_car (x, a); |
1095 | set_cdr (x, b); |
1069 | set_cdr (x, b); |
1096 | |
1070 | |
1097 | return x; |
1071 | return x; |
1098 | } |
1072 | } |
1099 | |
1073 | |
1100 | static pointer |
1074 | ecb_hot static pointer |
|
|
1075 | ximmutable_cons (SCHEME_P_ pointer a, pointer b) |
|
|
1076 | { |
|
|
1077 | pointer x = xcons (SCHEME_A_ a, b); |
|
|
1078 | setimmutable (x); |
|
|
1079 | return x; |
|
|
1080 | } |
|
|
1081 | |
|
|
1082 | #define cons(a,b) xcons (SCHEME_A_ a, b) |
|
|
1083 | #define immutable_cons(a,b) ximmutable_cons (SCHEME_A_ a, b) |
|
|
1084 | |
|
|
1085 | ecb_cold static pointer |
1101 | generate_symbol (SCHEME_P_ const char *name) |
1086 | generate_symbol (SCHEME_P_ const char *name) |
1102 | { |
1087 | { |
1103 | pointer x = mk_string (SCHEME_A_ name); |
1088 | pointer x = mk_string (SCHEME_A_ name); |
1104 | setimmutable (x); |
1089 | setimmutable (x); |
1105 | set_typeflag (x, T_SYMBOL | T_ATOM); |
1090 | set_typeflag (x, T_SYMBOL | T_ATOM); |
… | |
… | |
1111 | #ifndef USE_OBJECT_LIST |
1096 | #ifndef USE_OBJECT_LIST |
1112 | |
1097 | |
1113 | static int |
1098 | static int |
1114 | hash_fn (const char *key, int table_size) |
1099 | hash_fn (const char *key, int table_size) |
1115 | { |
1100 | { |
1116 | const unsigned char *p = key; |
1101 | const unsigned char *p = (unsigned char *)key; |
1117 | uint32_t hash = 2166136261; |
1102 | uint32_t hash = 2166136261; |
1118 | |
1103 | |
1119 | while (*p) |
1104 | while (*p) |
1120 | hash = (hash ^ *p++) * 16777619; |
1105 | hash = (hash ^ *p++) * 16777619; |
1121 | |
1106 | |
1122 | return hash % table_size; |
1107 | return hash % table_size; |
1123 | } |
1108 | } |
1124 | |
1109 | |
1125 | static pointer |
1110 | ecb_cold static pointer |
1126 | oblist_initial_value (SCHEME_P) |
1111 | oblist_initial_value (SCHEME_P) |
1127 | { |
1112 | { |
1128 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1113 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1129 | } |
1114 | } |
1130 | |
1115 | |
1131 | /* returns the new symbol */ |
1116 | /* returns the new symbol */ |
1132 | static pointer |
1117 | ecb_cold static pointer |
1133 | oblist_add_by_name (SCHEME_P_ const char *name) |
1118 | oblist_add_by_name (SCHEME_P_ const char *name) |
1134 | { |
1119 | { |
1135 | pointer x = generate_symbol (SCHEME_A_ name); |
1120 | pointer x = generate_symbol (SCHEME_A_ name); |
1136 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1121 | 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))); |
1122 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1138 | return x; |
1123 | return x; |
1139 | } |
1124 | } |
1140 | |
1125 | |
1141 | ecb_inline pointer |
1126 | ecb_cold static pointer |
1142 | oblist_find_by_name (SCHEME_P_ const char *name) |
1127 | oblist_find_by_name (SCHEME_P_ const char *name) |
1143 | { |
1128 | { |
1144 | int location; |
1129 | int location; |
1145 | pointer x; |
1130 | pointer x; |
1146 | char *s; |
1131 | char *s; |
… | |
… | |
1157 | } |
1142 | } |
1158 | |
1143 | |
1159 | return NIL; |
1144 | return NIL; |
1160 | } |
1145 | } |
1161 | |
1146 | |
1162 | static pointer |
1147 | ecb_cold static pointer |
1163 | oblist_all_symbols (SCHEME_P) |
1148 | oblist_all_symbols (SCHEME_P) |
1164 | { |
1149 | { |
1165 | int i; |
1150 | int i; |
1166 | pointer x; |
1151 | pointer x; |
1167 | pointer ob_list = NIL; |
1152 | pointer ob_list = NIL; |
… | |
… | |
1173 | return ob_list; |
1158 | return ob_list; |
1174 | } |
1159 | } |
1175 | |
1160 | |
1176 | #else |
1161 | #else |
1177 | |
1162 | |
1178 | static pointer |
1163 | ecb_cold static pointer |
1179 | oblist_initial_value (SCHEME_P) |
1164 | oblist_initial_value (SCHEME_P) |
1180 | { |
1165 | { |
1181 | return NIL; |
1166 | return NIL; |
1182 | } |
1167 | } |
1183 | |
1168 | |
1184 | ecb_inline pointer |
1169 | ecb_cold static pointer |
1185 | oblist_find_by_name (SCHEME_P_ const char *name) |
1170 | oblist_find_by_name (SCHEME_P_ const char *name) |
1186 | { |
1171 | { |
1187 | pointer x; |
1172 | pointer x; |
1188 | char *s; |
1173 | char *s; |
1189 | |
1174 | |
… | |
… | |
1198 | |
1183 | |
1199 | return NIL; |
1184 | return NIL; |
1200 | } |
1185 | } |
1201 | |
1186 | |
1202 | /* returns the new symbol */ |
1187 | /* returns the new symbol */ |
1203 | static pointer |
1188 | ecb_cold static pointer |
1204 | oblist_add_by_name (SCHEME_P_ const char *name) |
1189 | oblist_add_by_name (SCHEME_P_ const char *name) |
1205 | { |
1190 | { |
1206 | pointer x = generate_symbol (SCHEME_A_ name); |
1191 | pointer x = generate_symbol (SCHEME_A_ name); |
1207 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1192 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1208 | return x; |
1193 | return x; |
1209 | } |
1194 | } |
1210 | |
1195 | |
1211 | static pointer |
1196 | ecb_cold static pointer |
1212 | oblist_all_symbols (SCHEME_P) |
1197 | oblist_all_symbols (SCHEME_P) |
1213 | { |
1198 | { |
1214 | return SCHEME_V->oblist; |
1199 | return SCHEME_V->oblist; |
1215 | } |
1200 | } |
1216 | |
1201 | |
1217 | #endif |
1202 | #endif |
1218 | |
1203 | |
1219 | #if USE_PORTS |
|
|
1220 | static pointer |
1204 | ecb_cold static pointer |
1221 | mk_port (SCHEME_P_ port *p) |
1205 | mk_port (SCHEME_P_ port *p) |
1222 | { |
1206 | { |
1223 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1207 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1224 | |
1208 | |
1225 | set_typeflag (x, T_PORT | T_ATOM); |
1209 | set_typeflag (x, T_PORT | T_ATOM); |
1226 | x->object.port = p; |
1210 | set_port (x, p); |
1227 | |
1211 | |
1228 | return x; |
1212 | return x; |
1229 | } |
1213 | } |
1230 | #endif |
|
|
1231 | |
1214 | |
1232 | pointer |
1215 | ecb_cold pointer |
1233 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1216 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1234 | { |
1217 | { |
1235 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1218 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1236 | |
1219 | |
1237 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1220 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1238 | x->object.ff = f; |
1221 | CELL(x)->object.ff = f; |
1239 | |
1222 | |
1240 | return x; |
1223 | return x; |
1241 | } |
1224 | } |
1242 | |
1225 | |
1243 | INTERFACE pointer |
1226 | INTERFACE pointer |
… | |
… | |
1402 | x = oblist_add_by_name (SCHEME_A_ name); |
1385 | x = oblist_add_by_name (SCHEME_A_ name); |
1403 | |
1386 | |
1404 | return x; |
1387 | return x; |
1405 | } |
1388 | } |
1406 | |
1389 | |
1407 | INTERFACE pointer |
1390 | ecb_cold INTERFACE pointer |
1408 | gensym (SCHEME_P) |
1391 | gensym (SCHEME_P) |
1409 | { |
1392 | { |
1410 | pointer x; |
1393 | pointer x; |
1411 | char name[40] = "gensym-"; |
1394 | char name[40] = "gensym-"; |
1412 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1395 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
… | |
… | |
1419 | { |
1402 | { |
1420 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1403 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1421 | } |
1404 | } |
1422 | |
1405 | |
1423 | /* make symbol or number atom from string */ |
1406 | /* make symbol or number atom from string */ |
1424 | static pointer |
1407 | ecb_cold static pointer |
1425 | mk_atom (SCHEME_P_ char *q) |
1408 | mk_atom (SCHEME_P_ char *q) |
1426 | { |
1409 | { |
1427 | char c, *p; |
1410 | char c, *p; |
1428 | int has_dec_point = 0; |
1411 | int has_dec_point = 0; |
1429 | int has_fp_exp = 0; |
1412 | int has_fp_exp = 0; |
… | |
… | |
1500 | |
1483 | |
1501 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1484 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1502 | } |
1485 | } |
1503 | |
1486 | |
1504 | /* make constant */ |
1487 | /* make constant */ |
1505 | static pointer |
1488 | ecb_cold static pointer |
1506 | mk_sharp_const (SCHEME_P_ char *name) |
1489 | mk_sharp_const (SCHEME_P_ char *name) |
1507 | { |
1490 | { |
1508 | if (!strcmp (name, "t")) |
1491 | if (!strcmp (name, "t")) |
1509 | return S_T; |
1492 | return S_T; |
1510 | else if (!strcmp (name, "f")) |
1493 | else if (!strcmp (name, "f")) |
1511 | return S_F; |
1494 | return S_F; |
1512 | else if (*name == '\\') /* #\w (character) */ |
1495 | else if (*name == '\\') /* #\w (character) */ |
1513 | { |
1496 | { |
1514 | int c; |
1497 | int c; |
1515 | |
1498 | |
|
|
1499 | // TODO: optimise |
1516 | if (stricmp (name + 1, "space") == 0) |
1500 | if (stricmp (name + 1, "space") == 0) |
1517 | c = ' '; |
1501 | c = ' '; |
1518 | else if (stricmp (name + 1, "newline") == 0) |
1502 | else if (stricmp (name + 1, "newline") == 0) |
1519 | c = '\n'; |
1503 | c = '\n'; |
1520 | else if (stricmp (name + 1, "return") == 0) |
1504 | else if (stricmp (name + 1, "return") == 0) |
1521 | c = '\r'; |
1505 | c = '\r'; |
1522 | else if (stricmp (name + 1, "tab") == 0) |
1506 | else if (stricmp (name + 1, "tab") == 0) |
1523 | c = '\t'; |
1507 | c = '\t'; |
|
|
1508 | else if (stricmp (name + 1, "alarm") == 0) |
|
|
1509 | c = 0x07; |
|
|
1510 | else if (stricmp (name + 1, "backspace") == 0) |
|
|
1511 | c = 0x08; |
|
|
1512 | else if (stricmp (name + 1, "escape") == 0) |
|
|
1513 | c = 0x1b; |
|
|
1514 | else if (stricmp (name + 1, "delete") == 0) |
|
|
1515 | c = 0x7f; |
|
|
1516 | else if (stricmp (name + 1, "null") == 0) |
|
|
1517 | c = 0; |
1524 | else if (name[1] == 'x' && name[2] != 0) |
1518 | else if (name[1] == 'x' && name[2] != 0) |
1525 | { |
1519 | { |
1526 | long c1 = strtol (name + 2, 0, 16); |
1520 | long c1 = strtol (name + 2, 0, 16); |
1527 | |
1521 | |
1528 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1522 | if (0 <= c1 && c1 <= UCHAR_MAX) |
… | |
… | |
1553 | return NIL; |
1547 | return NIL; |
1554 | } |
1548 | } |
1555 | } |
1549 | } |
1556 | |
1550 | |
1557 | /* ========== garbage collector ========== */ |
1551 | /* ========== garbage collector ========== */ |
|
|
1552 | |
|
|
1553 | static void |
|
|
1554 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1555 | { |
|
|
1556 | /* TODO, fast bitmap check? */ |
|
|
1557 | if (is_string (a) || is_symbol (a)) |
|
|
1558 | free (strvalue (a)); |
|
|
1559 | else if (is_vector (a)) |
|
|
1560 | free (vecvalue (a)); |
|
|
1561 | #if USE_PORTS |
|
|
1562 | else if (is_port (a)) |
|
|
1563 | { |
|
|
1564 | if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) |
|
|
1565 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1566 | |
|
|
1567 | free (port (a)); |
|
|
1568 | } |
|
|
1569 | #endif |
|
|
1570 | } |
1558 | |
1571 | |
1559 | /*-- |
1572 | /*-- |
1560 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1573 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1561 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1574 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1562 | * for marking. |
1575 | * for marking. |
1563 | * |
1576 | * |
1564 | * The exception is vectors - vectors are currently marked recursively, |
1577 | * The exception is vectors - vectors are currently marked recursively, |
1565 | * which is inherited form tinyscheme and could be fixed by having another |
1578 | * which is inherited form tinyscheme and could be fixed by having another |
1566 | * word of context in the vector |
1579 | * word of context in the vector |
1567 | */ |
1580 | */ |
1568 | static void |
1581 | ecb_hot static void |
1569 | mark (pointer a) |
1582 | mark (pointer a) |
1570 | { |
1583 | { |
1571 | pointer t, q, p; |
1584 | pointer t, q, p; |
1572 | |
1585 | |
1573 | t = 0; |
1586 | t = 0; |
… | |
… | |
1630 | p = q; |
1643 | p = q; |
1631 | goto E6; |
1644 | goto E6; |
1632 | } |
1645 | } |
1633 | } |
1646 | } |
1634 | |
1647 | |
|
|
1648 | ecb_hot static void |
|
|
1649 | gc_free (SCHEME_P) |
|
|
1650 | { |
|
|
1651 | int i; |
|
|
1652 | uint32_t total = 0; |
|
|
1653 | |
|
|
1654 | /* Here we scan the cells to build the free-list. */ |
|
|
1655 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1656 | { |
|
|
1657 | struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1658 | struct cell *p; |
|
|
1659 | total += SCHEME_V->cell_segsize [i]; |
|
|
1660 | |
|
|
1661 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
|
|
1662 | { |
|
|
1663 | pointer c = POINTER (p); |
|
|
1664 | |
|
|
1665 | if (is_mark (c)) |
|
|
1666 | clrmark (c); |
|
|
1667 | else |
|
|
1668 | { |
|
|
1669 | /* reclaim cell */ |
|
|
1670 | if (typeflag (c) != T_PAIR) |
|
|
1671 | { |
|
|
1672 | finalize_cell (SCHEME_A_ c); |
|
|
1673 | set_typeflag (c, T_PAIR); |
|
|
1674 | set_car (c, NIL); |
|
|
1675 | } |
|
|
1676 | |
|
|
1677 | ++SCHEME_V->fcells; |
|
|
1678 | set_cdr (c, SCHEME_V->free_cell); |
|
|
1679 | SCHEME_V->free_cell = c; |
|
|
1680 | } |
|
|
1681 | } |
|
|
1682 | } |
|
|
1683 | |
|
|
1684 | if (SCHEME_V->gc_verbose) |
|
|
1685 | { |
|
|
1686 | 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"); |
|
|
1687 | } |
|
|
1688 | } |
|
|
1689 | |
1635 | /* garbage collection. parameter a, b is marked. */ |
1690 | /* garbage collection. parameter a, b is marked. */ |
1636 | static void |
1691 | ecb_cold static void |
1637 | gc (SCHEME_P_ pointer a, pointer b) |
1692 | gc (SCHEME_P_ pointer a, pointer b) |
1638 | { |
1693 | { |
1639 | pointer p; |
|
|
1640 | int i; |
1694 | int i; |
1641 | |
1695 | |
1642 | if (SCHEME_V->gc_verbose) |
1696 | if (SCHEME_V->gc_verbose) |
1643 | putstr (SCHEME_A_ "gc..."); |
1697 | putstr (SCHEME_A_ "gc..."); |
1644 | |
1698 | |
… | |
… | |
1677 | clrmark (NIL); |
1731 | clrmark (NIL); |
1678 | SCHEME_V->fcells = 0; |
1732 | SCHEME_V->fcells = 0; |
1679 | SCHEME_V->free_cell = NIL; |
1733 | SCHEME_V->free_cell = NIL; |
1680 | |
1734 | |
1681 | if (SCHEME_V->gc_verbose) |
1735 | if (SCHEME_V->gc_verbose) |
1682 | xwrstr ("freeing..."); |
1736 | putstr (SCHEME_A_ "freeing..."); |
1683 | |
1737 | |
1684 | uint32_t total = 0; |
1738 | 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 | } |
1739 | } |
1737 | |
1740 | |
1738 | /* ========== Routines for Reading ========== */ |
1741 | /* ========== Routines for Reading ========== */ |
1739 | |
1742 | |
1740 | static int |
1743 | ecb_cold static int |
1741 | file_push (SCHEME_P_ const char *fname) |
1744 | file_push (SCHEME_P_ const char *fname) |
1742 | { |
1745 | { |
1743 | #if USE_PORTS |
|
|
1744 | int fin; |
1746 | int fin; |
1745 | |
1747 | |
1746 | if (SCHEME_V->file_i == MAXFIL - 1) |
1748 | if (SCHEME_V->file_i == MAXFIL - 1) |
1747 | return 0; |
1749 | return 0; |
1748 | |
1750 | |
… | |
… | |
1754 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1756 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1755 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1757 | 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; |
1758 | 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; |
1759 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1758 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1760 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1759 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1761 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1760 | |
1762 | |
1761 | #if SHOW_ERROR_LINE |
1763 | #if SHOW_ERROR_LINE |
1762 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1764 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1763 | |
1765 | |
1764 | if (fname) |
1766 | if (fname) |
1765 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0); |
1767 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0); |
1766 | #endif |
1768 | #endif |
1767 | } |
1769 | } |
1768 | |
1770 | |
1769 | return fin >= 0; |
1771 | return fin >= 0; |
1770 | |
|
|
1771 | #else |
|
|
1772 | return 1; |
|
|
1773 | #endif |
|
|
1774 | } |
1772 | } |
1775 | |
1773 | |
1776 | static void |
1774 | ecb_cold static void |
1777 | file_pop (SCHEME_P) |
1775 | file_pop (SCHEME_P) |
1778 | { |
1776 | { |
1779 | if (SCHEME_V->file_i != 0) |
1777 | if (SCHEME_V->file_i != 0) |
1780 | { |
1778 | { |
1781 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1779 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1782 | #if USE_PORTS |
1780 | #if USE_PORTS |
1783 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1781 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1784 | #endif |
1782 | #endif |
1785 | SCHEME_V->file_i--; |
1783 | SCHEME_V->file_i--; |
1786 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1784 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1787 | } |
1785 | } |
1788 | } |
1786 | } |
1789 | |
1787 | |
1790 | static int |
1788 | ecb_cold static int |
1791 | file_interactive (SCHEME_P) |
1789 | file_interactive (SCHEME_P) |
1792 | { |
1790 | { |
1793 | #if USE_PORTS |
1791 | #if USE_PORTS |
1794 | return SCHEME_V->file_i == 0 |
1792 | return SCHEME_V->file_i == 0 |
1795 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1793 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1796 | && (SCHEME_V->inport->object.port->kind & port_file); |
1794 | && (port (SCHEME_V->inport)->kind & port_file); |
1797 | #else |
1795 | #else |
1798 | return 0; |
1796 | return 0; |
1799 | #endif |
1797 | #endif |
1800 | } |
1798 | } |
1801 | |
1799 | |
1802 | #if USE_PORTS |
1800 | #if USE_PORTS |
1803 | static port * |
1801 | ecb_cold static port * |
1804 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1802 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1805 | { |
1803 | { |
1806 | int fd; |
1804 | int fd; |
1807 | int flags; |
1805 | int flags; |
1808 | char *rw; |
1806 | char *rw; |
… | |
… | |
1831 | # endif |
1829 | # endif |
1832 | |
1830 | |
1833 | return pt; |
1831 | return pt; |
1834 | } |
1832 | } |
1835 | |
1833 | |
1836 | static pointer |
1834 | ecb_cold static pointer |
1837 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1835 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1838 | { |
1836 | { |
1839 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1837 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1840 | |
1838 | |
1841 | if (!pt && USE_ERROR_CHECKING) |
1839 | if (!pt && USE_ERROR_CHECKING) |
1842 | return NIL; |
1840 | return NIL; |
1843 | |
1841 | |
1844 | return mk_port (SCHEME_A_ pt); |
1842 | return mk_port (SCHEME_A_ pt); |
1845 | } |
1843 | } |
1846 | |
1844 | |
1847 | static port * |
1845 | ecb_cold static port * |
1848 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1846 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1849 | { |
1847 | { |
1850 | port *pt = malloc (sizeof *pt); |
1848 | port *pt = malloc (sizeof *pt); |
1851 | |
1849 | |
1852 | if (!pt && USE_ERROR_CHECKING) |
1850 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1857 | pt->rep.stdio.file = f; |
1855 | pt->rep.stdio.file = f; |
1858 | pt->rep.stdio.closeit = 0; |
1856 | pt->rep.stdio.closeit = 0; |
1859 | return pt; |
1857 | return pt; |
1860 | } |
1858 | } |
1861 | |
1859 | |
1862 | static pointer |
1860 | ecb_cold static pointer |
1863 | port_from_file (SCHEME_P_ int f, int prop) |
1861 | port_from_file (SCHEME_P_ int f, int prop) |
1864 | { |
1862 | { |
1865 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1863 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1866 | |
1864 | |
1867 | if (!pt && USE_ERROR_CHECKING) |
1865 | if (!pt && USE_ERROR_CHECKING) |
1868 | return NIL; |
1866 | return NIL; |
1869 | |
1867 | |
1870 | return mk_port (SCHEME_A_ pt); |
1868 | return mk_port (SCHEME_A_ pt); |
1871 | } |
1869 | } |
1872 | |
1870 | |
1873 | static port * |
1871 | ecb_cold static port * |
1874 | port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1872 | port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1875 | { |
1873 | { |
1876 | port *pt = malloc (sizeof (port)); |
1874 | port *pt = malloc (sizeof (port)); |
1877 | |
1875 | |
1878 | if (!pt && USE_ERROR_CHECKING) |
1876 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1884 | pt->rep.string.curr = start; |
1882 | pt->rep.string.curr = start; |
1885 | pt->rep.string.past_the_end = past_the_end; |
1883 | pt->rep.string.past_the_end = past_the_end; |
1886 | return pt; |
1884 | return pt; |
1887 | } |
1885 | } |
1888 | |
1886 | |
1889 | static pointer |
1887 | ecb_cold static pointer |
1890 | port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1888 | port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1891 | { |
1889 | { |
1892 | port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); |
1890 | port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); |
1893 | |
1891 | |
1894 | if (!pt && USE_ERROR_CHECKING) |
1892 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1897 | return mk_port (SCHEME_A_ pt); |
1895 | return mk_port (SCHEME_A_ pt); |
1898 | } |
1896 | } |
1899 | |
1897 | |
1900 | # define BLOCK_SIZE 256 |
1898 | # define BLOCK_SIZE 256 |
1901 | |
1899 | |
1902 | static port * |
1900 | ecb_cold static port * |
1903 | port_rep_from_scratch (SCHEME_P) |
1901 | port_rep_from_scratch (SCHEME_P) |
1904 | { |
1902 | { |
1905 | char *start; |
1903 | char *start; |
1906 | port *pt = malloc (sizeof (port)); |
1904 | port *pt = malloc (sizeof (port)); |
1907 | |
1905 | |
… | |
… | |
1921 | pt->rep.string.curr = start; |
1919 | pt->rep.string.curr = start; |
1922 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1920 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1923 | return pt; |
1921 | return pt; |
1924 | } |
1922 | } |
1925 | |
1923 | |
1926 | static pointer |
1924 | ecb_cold static pointer |
1927 | port_from_scratch (SCHEME_P) |
1925 | port_from_scratch (SCHEME_P) |
1928 | { |
1926 | { |
1929 | port *pt = port_rep_from_scratch (SCHEME_A); |
1927 | port *pt = port_rep_from_scratch (SCHEME_A); |
1930 | |
1928 | |
1931 | if (!pt && USE_ERROR_CHECKING) |
1929 | if (!pt && USE_ERROR_CHECKING) |
1932 | return NIL; |
1930 | return NIL; |
1933 | |
1931 | |
1934 | return mk_port (SCHEME_A_ pt); |
1932 | return mk_port (SCHEME_A_ pt); |
1935 | } |
1933 | } |
1936 | |
1934 | |
1937 | static void |
1935 | ecb_cold static void |
1938 | port_close (SCHEME_P_ pointer p, int flag) |
1936 | port_close (SCHEME_P_ pointer p, int flag) |
1939 | { |
1937 | { |
1940 | port *pt = p->object.port; |
1938 | port *pt = port (p); |
1941 | |
1939 | |
1942 | pt->kind &= ~flag; |
1940 | pt->kind &= ~flag; |
1943 | |
1941 | |
1944 | if ((pt->kind & (port_input | port_output)) == 0) |
1942 | if ((pt->kind & (port_input | port_output)) == 0) |
1945 | { |
1943 | { |
… | |
… | |
1962 | } |
1960 | } |
1963 | } |
1961 | } |
1964 | #endif |
1962 | #endif |
1965 | |
1963 | |
1966 | /* get new character from input file */ |
1964 | /* get new character from input file */ |
1967 | static int |
1965 | ecb_cold static int |
1968 | inchar (SCHEME_P) |
1966 | inchar (SCHEME_P) |
1969 | { |
1967 | { |
1970 | int c; |
1968 | int c; |
1971 | port *pt; |
1969 | port *pt = port (SCHEME_V->inport); |
1972 | |
|
|
1973 | pt = SCHEME_V->inport->object.port; |
|
|
1974 | |
1970 | |
1975 | if (pt->kind & port_saw_EOF) |
1971 | if (pt->kind & port_saw_EOF) |
1976 | return EOF; |
1972 | return EOF; |
1977 | |
1973 | |
1978 | c = basic_inchar (pt); |
1974 | c = basic_inchar (pt); |
… | |
… | |
1988 | } |
1984 | } |
1989 | |
1985 | |
1990 | return c; |
1986 | return c; |
1991 | } |
1987 | } |
1992 | |
1988 | |
1993 | static int ungot = -1; |
1989 | ecb_cold static int |
1994 | |
|
|
1995 | static int |
|
|
1996 | basic_inchar (port *pt) |
1990 | basic_inchar (port *pt) |
1997 | { |
1991 | { |
1998 | #if USE_PORTS |
|
|
1999 | if (pt->unget != -1) |
1992 | if (pt->unget != -1) |
2000 | { |
1993 | { |
2001 | int r = pt->unget; |
1994 | int r = pt->unget; |
2002 | pt->unget = -1; |
1995 | pt->unget = -1; |
2003 | return r; |
1996 | return r; |
2004 | } |
1997 | } |
2005 | |
1998 | |
|
|
1999 | #if USE_PORTS |
2006 | if (pt->kind & port_file) |
2000 | if (pt->kind & port_file) |
2007 | { |
2001 | { |
2008 | char c; |
2002 | char c; |
2009 | |
2003 | |
2010 | if (!read (pt->rep.stdio.file, &c, 1)) |
2004 | if (!read (pt->rep.stdio.file, &c, 1)) |
… | |
… | |
2018 | return EOF; |
2012 | return EOF; |
2019 | else |
2013 | else |
2020 | return *pt->rep.string.curr++; |
2014 | return *pt->rep.string.curr++; |
2021 | } |
2015 | } |
2022 | #else |
2016 | #else |
2023 | if (ungot == -1) |
|
|
2024 | { |
|
|
2025 | char c; |
2017 | char c; |
2026 | if (!read (0, &c, 1)) |
2018 | |
|
|
2019 | if (!read (pt->rep.stdio.file, &c, 1)) |
2027 | return EOF; |
2020 | return EOF; |
2028 | |
2021 | |
2029 | ungot = c; |
|
|
2030 | } |
|
|
2031 | |
|
|
2032 | { |
|
|
2033 | int r = ungot; |
|
|
2034 | ungot = -1; |
|
|
2035 | return r; |
2022 | return c; |
2036 | } |
|
|
2037 | #endif |
2023 | #endif |
2038 | } |
2024 | } |
2039 | |
2025 | |
2040 | /* back character to input buffer */ |
2026 | /* back character to input buffer */ |
2041 | static void |
2027 | ecb_cold static void |
2042 | backchar (SCHEME_P_ int c) |
2028 | backchar (SCHEME_P_ int c) |
2043 | { |
2029 | { |
2044 | #if USE_PORTS |
2030 | port *pt = port (SCHEME_V->inport); |
2045 | port *pt; |
|
|
2046 | |
2031 | |
2047 | if (c == EOF) |
2032 | if (c == EOF) |
2048 | return; |
2033 | return; |
2049 | |
2034 | |
2050 | pt = SCHEME_V->inport->object.port; |
|
|
2051 | pt->unget = c; |
2035 | pt->unget = c; |
2052 | #else |
|
|
2053 | if (c == EOF) |
|
|
2054 | return; |
|
|
2055 | |
|
|
2056 | ungot = c; |
|
|
2057 | #endif |
|
|
2058 | } |
2036 | } |
2059 | |
2037 | |
2060 | #if USE_PORTS |
2038 | #if USE_PORTS |
2061 | static int |
2039 | ecb_cold static int |
2062 | realloc_port_string (SCHEME_P_ port *p) |
2040 | realloc_port_string (SCHEME_P_ port *p) |
2063 | { |
2041 | { |
2064 | char *start = p->rep.string.start; |
2042 | char *start = p->rep.string.start; |
2065 | size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; |
2043 | size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; |
2066 | char *str = malloc (new_size); |
2044 | char *str = malloc (new_size); |
… | |
… | |
2079 | else |
2057 | else |
2080 | return 0; |
2058 | return 0; |
2081 | } |
2059 | } |
2082 | #endif |
2060 | #endif |
2083 | |
2061 | |
2084 | INTERFACE void |
2062 | ecb_cold static void |
2085 | putstr (SCHEME_P_ const char *s) |
2063 | putchars (SCHEME_P_ const char *s, int len) |
2086 | { |
2064 | { |
|
|
2065 | port *pt = port (SCHEME_V->outport); |
|
|
2066 | |
2087 | #if USE_PORTS |
2067 | #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) |
2068 | if (pt->kind & port_file) |
2111 | write (pt->rep.stdio.file, s, len); |
2069 | write (pt->rep.stdio.file, s, len); |
2112 | else |
2070 | else |
2113 | { |
2071 | { |
2114 | for (; len; len--) |
2072 | for (; len; len--) |
… | |
… | |
2119 | *pt->rep.string.curr++ = *s++; |
2077 | *pt->rep.string.curr++ = *s++; |
2120 | } |
2078 | } |
2121 | } |
2079 | } |
2122 | |
2080 | |
2123 | #else |
2081 | #else |
2124 | write (1, s, len); |
2082 | write (1, s, len); // output not initialised |
2125 | #endif |
2083 | #endif |
|
|
2084 | } |
|
|
2085 | |
|
|
2086 | INTERFACE void |
|
|
2087 | putstr (SCHEME_P_ const char *s) |
|
|
2088 | { |
|
|
2089 | putchars (SCHEME_A_ s, strlen (s)); |
2126 | } |
2090 | } |
2127 | |
2091 | |
2128 | INTERFACE void |
2092 | INTERFACE void |
2129 | putcharacter (SCHEME_P_ int c) |
2093 | putcharacter (SCHEME_P_ int c) |
2130 | { |
2094 | { |
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; |
2095 | char cc = c; |
2149 | write (1, &c, 1); |
2096 | |
2150 | #endif |
2097 | putchars (SCHEME_A_ &cc, 1); |
2151 | } |
2098 | } |
2152 | |
2099 | |
2153 | /* read characters up to delimiter, but cater to character constants */ |
2100 | /* read characters up to delimiter, but cater to character constants */ |
2154 | static char * |
2101 | ecb_cold static char * |
2155 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2102 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2156 | { |
2103 | { |
2157 | char *p = SCHEME_V->strbuff + skip; |
2104 | char *p = SCHEME_V->strbuff + skip; |
2158 | |
2105 | |
2159 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2106 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
… | |
… | |
2168 | |
2115 | |
2169 | return SCHEME_V->strbuff; |
2116 | return SCHEME_V->strbuff; |
2170 | } |
2117 | } |
2171 | |
2118 | |
2172 | /* read string expression "xxx...xxx" */ |
2119 | /* read string expression "xxx...xxx" */ |
2173 | static pointer |
2120 | ecb_cold static pointer |
2174 | readstrexp (SCHEME_P_ char delim) |
2121 | readstrexp (SCHEME_P_ char delim) |
2175 | { |
2122 | { |
2176 | char *p = SCHEME_V->strbuff; |
2123 | char *p = SCHEME_V->strbuff; |
2177 | int c; |
2124 | int c; |
2178 | int c1 = 0; |
2125 | int c1 = 0; |
… | |
… | |
2211 | case '7': |
2158 | case '7': |
2212 | state = st_oct1; |
2159 | state = st_oct1; |
2213 | c1 = c - '0'; |
2160 | c1 = c - '0'; |
2214 | break; |
2161 | break; |
2215 | |
2162 | |
|
|
2163 | case 'a': *p++ = '\a'; state = st_ok; break; |
|
|
2164 | case 'n': *p++ = '\n'; state = st_ok; break; |
|
|
2165 | case 'r': *p++ = '\r'; state = st_ok; break; |
|
|
2166 | case 't': *p++ = '\t'; state = st_ok; break; |
|
|
2167 | |
|
|
2168 | // this overshoots the minimum requirements of r7rs |
|
|
2169 | case ' ': |
|
|
2170 | case '\t': |
|
|
2171 | case '\r': |
|
|
2172 | case '\n': |
|
|
2173 | skipspace (SCHEME_A); |
|
|
2174 | state = st_ok; |
|
|
2175 | break; |
|
|
2176 | |
|
|
2177 | //TODO: x should end in ;, not two-digit hex |
2216 | case 'x': |
2178 | case 'x': |
2217 | case 'X': |
2179 | case 'X': |
2218 | state = st_x1; |
2180 | state = st_x1; |
2219 | c1 = 0; |
2181 | 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; |
2182 | break; |
2236 | |
2183 | |
2237 | default: |
2184 | default: |
2238 | *p++ = c; |
2185 | *p++ = c; |
2239 | state = st_ok; |
2186 | state = st_ok; |
… | |
… | |
2291 | } |
2238 | } |
2292 | } |
2239 | } |
2293 | } |
2240 | } |
2294 | |
2241 | |
2295 | /* check c is in chars */ |
2242 | /* check c is in chars */ |
2296 | ecb_inline int |
2243 | ecb_cold int |
2297 | is_one_of (const char *s, int c) |
2244 | is_one_of (const char *s, int c) |
2298 | { |
2245 | { |
2299 | return c == EOF || !!strchr (s, c); |
2246 | return c == EOF || !!strchr (s, c); |
2300 | } |
2247 | } |
2301 | |
2248 | |
2302 | /* skip white characters */ |
2249 | /* skip white characters */ |
2303 | ecb_inline int |
2250 | ecb_cold int |
2304 | skipspace (SCHEME_P) |
2251 | skipspace (SCHEME_P) |
2305 | { |
2252 | { |
2306 | int c, curr_line = 0; |
2253 | int c, curr_line = 0; |
2307 | |
2254 | |
2308 | do |
2255 | do |
… | |
… | |
2328 | backchar (SCHEME_A_ c); |
2275 | backchar (SCHEME_A_ c); |
2329 | return 1; |
2276 | return 1; |
2330 | } |
2277 | } |
2331 | |
2278 | |
2332 | /* get token */ |
2279 | /* get token */ |
2333 | static int |
2280 | ecb_cold static int |
2334 | token (SCHEME_P) |
2281 | token (SCHEME_P) |
2335 | { |
2282 | { |
2336 | int c = skipspace (SCHEME_A); |
2283 | int c = skipspace (SCHEME_A); |
2337 | |
2284 | |
2338 | if (c == EOF) |
2285 | if (c == EOF) |
… | |
… | |
2436 | } |
2383 | } |
2437 | |
2384 | |
2438 | /* ========== Routines for Printing ========== */ |
2385 | /* ========== Routines for Printing ========== */ |
2439 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2386 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2440 | |
2387 | |
2441 | static void |
2388 | ecb_cold static void |
2442 | printslashstring (SCHEME_P_ char *p, int len) |
2389 | printslashstring (SCHEME_P_ char *p, int len) |
2443 | { |
2390 | { |
2444 | int i; |
2391 | int i; |
2445 | unsigned char *s = (unsigned char *) p; |
2392 | unsigned char *s = (unsigned char *) p; |
2446 | |
2393 | |
… | |
… | |
2502 | |
2449 | |
2503 | putcharacter (SCHEME_A_ '"'); |
2450 | putcharacter (SCHEME_A_ '"'); |
2504 | } |
2451 | } |
2505 | |
2452 | |
2506 | /* print atoms */ |
2453 | /* print atoms */ |
2507 | static void |
2454 | ecb_cold static void |
2508 | printatom (SCHEME_P_ pointer l, int f) |
2455 | printatom (SCHEME_P_ pointer l, int f) |
2509 | { |
2456 | { |
2510 | char *p; |
2457 | char *p; |
2511 | int len; |
2458 | int len; |
2512 | |
2459 | |
2513 | atom2str (SCHEME_A_ l, f, &p, &len); |
2460 | atom2str (SCHEME_A_ l, f, &p, &len); |
2514 | putchars (SCHEME_A_ p, len); |
2461 | putchars (SCHEME_A_ p, len); |
2515 | } |
2462 | } |
2516 | |
2463 | |
2517 | /* Uses internal buffer unless string pointer is already available */ |
2464 | /* Uses internal buffer unless string pointer is already available */ |
2518 | static void |
2465 | ecb_cold static void |
2519 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2466 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2520 | { |
2467 | { |
2521 | char *p; |
2468 | char *p; |
2522 | |
2469 | |
2523 | if (l == NIL) |
2470 | if (l == NIL) |
… | |
… | |
2730 | return car (d); |
2677 | return car (d); |
2731 | |
2678 | |
2732 | p = cons (car (d), cdr (d)); |
2679 | p = cons (car (d), cdr (d)); |
2733 | q = p; |
2680 | q = p; |
2734 | |
2681 | |
2735 | while (cdr (cdr (p)) != NIL) |
2682 | while (cddr (p) != NIL) |
2736 | { |
2683 | { |
2737 | d = cons (car (p), cdr (p)); |
2684 | d = cons (car (p), cdr (p)); |
2738 | |
2685 | |
2739 | if (cdr (cdr (p)) != NIL) |
2686 | if (cddr (p) != NIL) |
2740 | p = cdr (d); |
2687 | p = cdr (d); |
2741 | } |
2688 | } |
2742 | |
2689 | |
2743 | set_cdr (p, car (cdr (p))); |
2690 | set_cdr (p, cadr (p)); |
2744 | return q; |
2691 | return q; |
2745 | } |
2692 | } |
2746 | |
2693 | |
2747 | /* reverse list -- produce new list */ |
2694 | /* reverse list -- produce new list */ |
2748 | static pointer |
2695 | ecb_hot static pointer |
2749 | reverse (SCHEME_P_ pointer a) |
2696 | reverse (SCHEME_P_ pointer a) |
2750 | { |
2697 | { |
2751 | /* a must be checked by gc */ |
2698 | /* a must be checked by gc */ |
2752 | pointer p = NIL; |
2699 | pointer p = NIL; |
2753 | |
2700 | |
… | |
… | |
2756 | |
2703 | |
2757 | return p; |
2704 | return p; |
2758 | } |
2705 | } |
2759 | |
2706 | |
2760 | /* reverse list --- in-place */ |
2707 | /* reverse list --- in-place */ |
2761 | static pointer |
2708 | ecb_hot static pointer |
2762 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2709 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2763 | { |
2710 | { |
2764 | pointer result = term; |
2711 | pointer result = term; |
2765 | pointer p = list; |
2712 | pointer p = list; |
2766 | |
2713 | |
… | |
… | |
2774 | |
2721 | |
2775 | return result; |
2722 | return result; |
2776 | } |
2723 | } |
2777 | |
2724 | |
2778 | /* append list -- produce new list (in reverse order) */ |
2725 | /* append list -- produce new list (in reverse order) */ |
2779 | static pointer |
2726 | ecb_hot static pointer |
2780 | revappend (SCHEME_P_ pointer a, pointer b) |
2727 | revappend (SCHEME_P_ pointer a, pointer b) |
2781 | { |
2728 | { |
2782 | pointer result = a; |
2729 | pointer result = a; |
2783 | pointer p = b; |
2730 | pointer p = b; |
2784 | |
2731 | |
… | |
… | |
2793 | |
2740 | |
2794 | return S_F; /* signal an error */ |
2741 | return S_F; /* signal an error */ |
2795 | } |
2742 | } |
2796 | |
2743 | |
2797 | /* equivalence of atoms */ |
2744 | /* equivalence of atoms */ |
2798 | int |
2745 | ecb_hot int |
2799 | eqv (pointer a, pointer b) |
2746 | eqv (pointer a, pointer b) |
2800 | { |
2747 | { |
2801 | if (is_string (a)) |
2748 | if (is_string (a)) |
2802 | { |
2749 | { |
2803 | if (is_string (b)) |
2750 | if (is_string (b)) |
… | |
… | |
2897 | } |
2844 | } |
2898 | else |
2845 | else |
2899 | set_car (env, immutable_cons (slot, car (env))); |
2846 | set_car (env, immutable_cons (slot, car (env))); |
2900 | } |
2847 | } |
2901 | |
2848 | |
2902 | static pointer |
2849 | ecb_hot static pointer |
2903 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2850 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2904 | { |
2851 | { |
2905 | pointer x, y; |
2852 | pointer x, y; |
2906 | |
2853 | |
2907 | for (x = env; x != NIL; x = cdr (x)) |
2854 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2928 | return NIL; |
2875 | return NIL; |
2929 | } |
2876 | } |
2930 | |
2877 | |
2931 | #else /* USE_ALIST_ENV */ |
2878 | #else /* USE_ALIST_ENV */ |
2932 | |
2879 | |
2933 | ecb_inline void |
2880 | static void |
2934 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2881 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2935 | { |
2882 | { |
2936 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2883 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2937 | setenvironment (SCHEME_V->envir); |
2884 | setenvironment (SCHEME_V->envir); |
2938 | } |
2885 | } |
2939 | |
2886 | |
2940 | ecb_inline void |
2887 | static void |
2941 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2888 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2942 | { |
2889 | { |
2943 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2890 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2944 | } |
2891 | } |
2945 | |
2892 | |
2946 | static pointer |
2893 | ecb_hot static pointer |
2947 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2894 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2948 | { |
2895 | { |
2949 | pointer x, y; |
2896 | pointer x, y; |
2950 | |
2897 | |
2951 | for (x = env; x != NIL; x = cdr (x)) |
2898 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2965 | return NIL; |
2912 | return NIL; |
2966 | } |
2913 | } |
2967 | |
2914 | |
2968 | #endif /* USE_ALIST_ENV else */ |
2915 | #endif /* USE_ALIST_ENV else */ |
2969 | |
2916 | |
2970 | ecb_inline void |
2917 | static void |
2971 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2918 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2972 | { |
2919 | { |
2973 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2920 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2974 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2921 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2975 | } |
2922 | } |
2976 | |
2923 | |
2977 | ecb_inline void |
2924 | static void |
2978 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2925 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2979 | { |
2926 | { |
2980 | set_cdr (slot, value); |
2927 | set_cdr (slot, value); |
2981 | } |
2928 | } |
2982 | |
2929 | |
2983 | ecb_inline pointer |
2930 | static pointer |
2984 | slot_value_in_env (pointer slot) |
2931 | slot_value_in_env (pointer slot) |
2985 | { |
2932 | { |
2986 | return cdr (slot); |
2933 | return cdr (slot); |
2987 | } |
2934 | } |
2988 | |
2935 | |
2989 | /* ========== Evaluation Cycle ========== */ |
2936 | /* ========== Evaluation Cycle ========== */ |
2990 | |
2937 | |
2991 | static int |
2938 | ecb_cold static int |
2992 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2939 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2993 | { |
2940 | { |
2994 | #if USE_ERROR_HOOK |
2941 | #if USE_ERROR_HOOK |
2995 | pointer x; |
2942 | pointer x; |
2996 | pointer hdl = SCHEME_V->ERROR_HOOK; |
2943 | pointer hdl = SCHEME_V->ERROR_HOOK; |
… | |
… | |
3072 | pointer code; |
3019 | pointer code; |
3073 | }; |
3020 | }; |
3074 | |
3021 | |
3075 | # define STACK_GROWTH 3 |
3022 | # define STACK_GROWTH 3 |
3076 | |
3023 | |
3077 | static void |
3024 | ecb_hot static void |
3078 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3025 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3079 | { |
3026 | { |
3080 | int nframes = (uintptr_t)SCHEME_V->dump; |
3027 | int nframes = (uintptr_t)SCHEME_V->dump; |
3081 | struct dump_stack_frame *next_frame; |
3028 | struct dump_stack_frame *next_frame; |
3082 | |
3029 | |
3083 | /* enough room for the next frame? */ |
3030 | /* enough room for the next frame? */ |
3084 | if (nframes >= SCHEME_V->dump_size) |
3031 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3085 | { |
3032 | { |
3086 | SCHEME_V->dump_size += STACK_GROWTH; |
3033 | 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); |
3034 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3088 | } |
3035 | } |
3089 | |
3036 | |
… | |
… | |
3095 | next_frame->code = code; |
3042 | next_frame->code = code; |
3096 | |
3043 | |
3097 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3044 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3098 | } |
3045 | } |
3099 | |
3046 | |
3100 | static int |
3047 | static ecb_hot int |
3101 | xs_return (SCHEME_P_ pointer a) |
3048 | xs_return (SCHEME_P_ pointer a) |
3102 | { |
3049 | { |
3103 | int nframes = (uintptr_t)SCHEME_V->dump; |
3050 | int nframes = (uintptr_t)SCHEME_V->dump; |
3104 | struct dump_stack_frame *frame; |
3051 | struct dump_stack_frame *frame; |
3105 | |
3052 | |
… | |
… | |
3116 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3063 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3117 | |
3064 | |
3118 | return 0; |
3065 | return 0; |
3119 | } |
3066 | } |
3120 | |
3067 | |
3121 | ecb_inline void |
3068 | ecb_cold void |
3122 | dump_stack_reset (SCHEME_P) |
3069 | dump_stack_reset (SCHEME_P) |
3123 | { |
3070 | { |
3124 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3071 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3125 | SCHEME_V->dump = (pointer)+0; |
3072 | SCHEME_V->dump = (pointer)+0; |
3126 | } |
3073 | } |
3127 | |
3074 | |
3128 | ecb_inline void |
3075 | ecb_cold void |
3129 | dump_stack_initialize (SCHEME_P) |
3076 | dump_stack_initialize (SCHEME_P) |
3130 | { |
3077 | { |
3131 | SCHEME_V->dump_size = 0; |
3078 | SCHEME_V->dump_size = 0; |
3132 | SCHEME_V->dump_base = 0; |
3079 | SCHEME_V->dump_base = 0; |
3133 | dump_stack_reset (SCHEME_A); |
3080 | dump_stack_reset (SCHEME_A); |
3134 | } |
3081 | } |
3135 | |
3082 | |
3136 | static void |
3083 | ecb_cold static void |
3137 | dump_stack_free (SCHEME_P) |
3084 | dump_stack_free (SCHEME_P) |
3138 | { |
3085 | { |
3139 | free (SCHEME_V->dump_base); |
3086 | free (SCHEME_V->dump_base); |
3140 | SCHEME_V->dump_base = 0; |
3087 | SCHEME_V->dump_base = 0; |
3141 | SCHEME_V->dump = (pointer)0; |
3088 | SCHEME_V->dump = (pointer)0; |
3142 | SCHEME_V->dump_size = 0; |
3089 | SCHEME_V->dump_size = 0; |
3143 | } |
3090 | } |
3144 | |
3091 | |
3145 | static void |
3092 | ecb_cold static void |
3146 | dump_stack_mark (SCHEME_P) |
3093 | dump_stack_mark (SCHEME_P) |
3147 | { |
3094 | { |
3148 | int nframes = (uintptr_t)SCHEME_V->dump; |
3095 | int nframes = (uintptr_t)SCHEME_V->dump; |
3149 | int i; |
3096 | int i; |
3150 | |
3097 | |
… | |
… | |
3156 | mark (frame->envir); |
3103 | mark (frame->envir); |
3157 | mark (frame->code); |
3104 | mark (frame->code); |
3158 | } |
3105 | } |
3159 | } |
3106 | } |
3160 | |
3107 | |
3161 | static pointer |
3108 | ecb_cold static pointer |
3162 | ss_get_cont (SCHEME_P) |
3109 | ss_get_cont (SCHEME_P) |
3163 | { |
3110 | { |
3164 | int nframes = (uintptr_t)SCHEME_V->dump; |
3111 | int nframes = (uintptr_t)SCHEME_V->dump; |
3165 | int i; |
3112 | int i; |
3166 | |
3113 | |
… | |
… | |
3178 | } |
3125 | } |
3179 | |
3126 | |
3180 | return cont; |
3127 | return cont; |
3181 | } |
3128 | } |
3182 | |
3129 | |
3183 | static void |
3130 | ecb_cold static void |
3184 | ss_set_cont (SCHEME_P_ pointer cont) |
3131 | ss_set_cont (SCHEME_P_ pointer cont) |
3185 | { |
3132 | { |
3186 | int i = 0; |
3133 | int i = 0; |
3187 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3134 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3188 | |
3135 | |
… | |
… | |
3200 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3147 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3201 | } |
3148 | } |
3202 | |
3149 | |
3203 | #else |
3150 | #else |
3204 | |
3151 | |
3205 | ecb_inline void |
3152 | ecb_cold void |
3206 | dump_stack_reset (SCHEME_P) |
3153 | dump_stack_reset (SCHEME_P) |
3207 | { |
3154 | { |
3208 | SCHEME_V->dump = NIL; |
3155 | SCHEME_V->dump = NIL; |
3209 | } |
3156 | } |
3210 | |
3157 | |
3211 | ecb_inline void |
3158 | ecb_cold void |
3212 | dump_stack_initialize (SCHEME_P) |
3159 | dump_stack_initialize (SCHEME_P) |
3213 | { |
3160 | { |
3214 | dump_stack_reset (SCHEME_A); |
3161 | dump_stack_reset (SCHEME_A); |
3215 | } |
3162 | } |
3216 | |
3163 | |
3217 | static void |
3164 | ecb_cold static void |
3218 | dump_stack_free (SCHEME_P) |
3165 | dump_stack_free (SCHEME_P) |
3219 | { |
3166 | { |
3220 | SCHEME_V->dump = NIL; |
3167 | SCHEME_V->dump = NIL; |
3221 | } |
3168 | } |
3222 | |
3169 | |
3223 | static int |
3170 | ecb_hot static int |
3224 | xs_return (SCHEME_P_ pointer a) |
3171 | xs_return (SCHEME_P_ pointer a) |
3225 | { |
3172 | { |
3226 | pointer dump = SCHEME_V->dump; |
3173 | pointer dump = SCHEME_V->dump; |
3227 | |
3174 | |
3228 | SCHEME_V->value = a; |
3175 | SCHEME_V->value = a; |
… | |
… | |
3238 | SCHEME_V->dump = dump; |
3185 | SCHEME_V->dump = dump; |
3239 | |
3186 | |
3240 | return 0; |
3187 | return 0; |
3241 | } |
3188 | } |
3242 | |
3189 | |
3243 | static void |
3190 | ecb_hot static void |
3244 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3191 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3245 | { |
3192 | { |
3246 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3193 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3247 | cons (args, |
3194 | cons (args, |
3248 | cons (SCHEME_V->envir, |
3195 | cons (SCHEME_V->envir, |
3249 | cons (code, |
3196 | cons (code, |
3250 | SCHEME_V->dump)))); |
3197 | SCHEME_V->dump)))); |
3251 | } |
3198 | } |
3252 | |
3199 | |
|
|
3200 | ecb_cold static void |
|
|
3201 | dump_stack_mark (SCHEME_P) |
|
|
3202 | { |
|
|
3203 | mark (SCHEME_V->dump); |
|
|
3204 | } |
|
|
3205 | |
|
|
3206 | ecb_cold static pointer |
|
|
3207 | ss_get_cont (SCHEME_P) |
|
|
3208 | { |
|
|
3209 | return SCHEME_V->dump; |
|
|
3210 | } |
|
|
3211 | |
|
|
3212 | ecb_cold static void |
|
|
3213 | ss_set_cont (SCHEME_P_ pointer cont) |
|
|
3214 | { |
|
|
3215 | SCHEME_V->dump = cont; |
|
|
3216 | } |
|
|
3217 | |
|
|
3218 | #endif |
|
|
3219 | |
|
|
3220 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
|
|
3221 | |
|
|
3222 | #if EXPERIMENT |
|
|
3223 | |
|
|
3224 | typedef void *stream[1]; |
|
|
3225 | |
|
|
3226 | #define stream_init() { 0 } |
|
|
3227 | |
|
|
3228 | ecb_cold static void |
|
|
3229 | stream_put (void **s, uint8_t byte) |
|
|
3230 | { |
|
|
3231 | uint32_t *sp = *s; |
|
|
3232 | uint32_t size = sizeof (uint32_t) * 2; |
|
|
3233 | uint32_t offs = size; |
|
|
3234 | |
|
|
3235 | if (ecb_expect_true (sp)) |
|
|
3236 | { |
|
|
3237 | offs = sp[0]; |
|
|
3238 | size = sp[1]; |
|
|
3239 | } |
|
|
3240 | |
|
|
3241 | if (ecb_expect_false (offs == size)) |
|
|
3242 | { |
|
|
3243 | size *= 2; |
|
|
3244 | sp = realloc (sp, size); |
|
|
3245 | *s = sp; |
|
|
3246 | sp[1] = size; |
|
|
3247 | |
|
|
3248 | } |
|
|
3249 | |
|
|
3250 | ((uint8_t *)sp)[offs++] = byte; |
|
|
3251 | sp[0] = offs; |
|
|
3252 | } |
|
|
3253 | |
|
|
3254 | #define stream_data(s) ((char *)(s)[0] + sizeof (uint32_t) * 2) |
|
|
3255 | #define stream_size(s) (((uint32_t *)(s)[0])[0] - sizeof (uint32_t) * 2) |
|
|
3256 | #define stream_free(s) free (s[0]) |
|
|
3257 | |
|
|
3258 | // calculates a (preferably small) integer that makes it possible to find |
|
|
3259 | // the symbol again. if pointers were offsets into a memory area... until |
|
|
3260 | // then, we return segment number in the low bits, and offset in the high |
|
|
3261 | // bits |
|
|
3262 | static uint32_t |
|
|
3263 | symbol_id (SCHEME_P_ pointer sym) |
|
|
3264 | { |
|
|
3265 | struct cell *p = CELL (sym); |
|
|
3266 | int i; |
|
|
3267 | |
|
|
3268 | for (i = SCHEME_V->last_cell_seg; i >= 0; --i) |
|
|
3269 | if (SCHEME_V->cell_seg[i] <= p && p < SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize[i]) |
|
|
3270 | { |
|
|
3271 | printf ("seg %d ofs %d/%d\n",i,(p - SCHEME_V->cell_seg[i]),SCHEME_V->cell_segsize[i]);//D |
|
|
3272 | return i | ((p - SCHEME_V->cell_seg[i]) << CELL_NSEGMENT_LOG); |
|
|
3273 | } |
|
|
3274 | |
|
|
3275 | abort (); |
|
|
3276 | } |
|
|
3277 | |
3253 | static void |
3278 | static void |
3254 | dump_stack_mark (SCHEME_P) |
3279 | compile (SCHEME_P_ stream s, pointer x) |
3255 | { |
3280 | { |
3256 | mark (SCHEME_V->dump); |
3281 | if (x == NIL) |
3257 | } |
3282 | { |
|
|
3283 | stream_put (s, 0); |
|
|
3284 | return; |
|
|
3285 | } |
3258 | |
3286 | |
3259 | static pointer |
3287 | if (is_syntax (x)) |
3260 | ss_get_cont (SCHEME_P) |
3288 | { |
3261 | { |
3289 | stream_put (s, 1); |
3262 | return SCHEME_V->dump; |
3290 | stream_put (s, syntaxnum (x)); |
3263 | } |
3291 | return; |
|
|
3292 | } |
3264 | |
3293 | |
3265 | static void |
3294 | switch (type (x)) |
3266 | ss_set_cont (SCHEME_P_ pointer cont) |
3295 | { |
3267 | { |
3296 | case T_INTEGER: |
3268 | SCHEME_V->dump = cont; |
3297 | stream_put (s, 2); |
3269 | } |
3298 | stream_put (s, 0); |
|
|
3299 | stream_put (s, 0); |
|
|
3300 | stream_put (s, 0); |
|
|
3301 | stream_put (s, 0); |
|
|
3302 | return; |
3270 | |
3303 | |
3271 | #endif |
3304 | case T_SYMBOL: |
|
|
3305 | { |
|
|
3306 | uint32_t sym = symbol_id (SCHEME_A_ x); |
|
|
3307 | printf ("sym %x\n", sym);//D |
3272 | |
3308 | |
3273 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3309 | stream_put (s, 3); |
3274 | |
3310 | |
3275 | #if EXPERIMENT |
3311 | while (sym > 0x7f) |
|
|
3312 | { |
|
|
3313 | stream_put (s, sym | 0x80); |
|
|
3314 | sym >>= 8; |
|
|
3315 | } |
|
|
3316 | |
|
|
3317 | stream_put (s, sym); |
|
|
3318 | } |
|
|
3319 | return; |
|
|
3320 | |
|
|
3321 | case T_PAIR: |
|
|
3322 | stream_put (s, 4); |
|
|
3323 | while (x != NIL) |
|
|
3324 | { |
|
|
3325 | compile (SCHEME_A_ s, car (x)); |
|
|
3326 | x = cdr (x); |
|
|
3327 | } |
|
|
3328 | stream_put (s, 0xff); |
|
|
3329 | return; |
|
|
3330 | |
|
|
3331 | default: |
|
|
3332 | stream_put (s, 5); |
|
|
3333 | stream_put (s, type (x)); |
|
|
3334 | stream_put (s, 0); |
|
|
3335 | stream_put (s, 0); |
|
|
3336 | stream_put (s, 0); |
|
|
3337 | stream_put (s, 0); |
|
|
3338 | break; |
|
|
3339 | } |
|
|
3340 | } |
|
|
3341 | |
3276 | static int |
3342 | static int |
|
|
3343 | compile_closure (SCHEME_P_ pointer p) |
|
|
3344 | { |
|
|
3345 | stream s = stream_init (); |
|
|
3346 | |
|
|
3347 | printatom (SCHEME_A_ p, 1);//D |
|
|
3348 | compile (SCHEME_A_ s, car (p)); |
|
|
3349 | |
|
|
3350 | FILE *xxd = popen ("xxd", "we"); |
|
|
3351 | fwrite (stream_data (s), 1, stream_size (s), xxd); |
|
|
3352 | fclose (xxd); |
|
|
3353 | |
|
|
3354 | return stream_size (s); |
|
|
3355 | } |
|
|
3356 | |
|
|
3357 | static int |
3277 | debug (SCHEME_P_ int indent, pointer x) |
3358 | dtree (SCHEME_P_ int indent, pointer x) |
3278 | { |
3359 | { |
3279 | int c; |
3360 | int c; |
3280 | |
3361 | |
3281 | if (is_syntax (x)) |
3362 | if (is_syntax (x)) |
3282 | { |
3363 | { |
… | |
… | |
3300 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
3381 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
3301 | return 24+8; |
3382 | return 24+8; |
3302 | |
3383 | |
3303 | case T_CLOSURE: |
3384 | case T_CLOSURE: |
3304 | printf ("%*sS<%s>\n", indent, "", "closure"); |
3385 | printf ("%*sS<%s>\n", indent, "", "closure"); |
3305 | debug (SCHEME_A_ indent + 3, cdr(x)); |
3386 | dtree (SCHEME_A_ indent + 3, cdr(x)); |
3306 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
3387 | return 32 + dtree (SCHEME_A_ indent + 3, car (x)); |
3307 | |
3388 | |
3308 | case T_PAIR: |
3389 | case T_PAIR: |
3309 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
3390 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
3310 | c = debug (SCHEME_A_ indent + 3, car (x)); |
3391 | c = dtree (SCHEME_A_ indent + 3, car (x)); |
3311 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
3392 | c += dtree (SCHEME_A_ indent + 3, cdr (x)); |
3312 | return c + 1; |
3393 | return c + 1; |
3313 | |
3394 | |
3314 | case T_PORT: |
3395 | case T_PORT: |
3315 | printf ("%*sS<%s>\n", indent, "", "port"); |
3396 | printf ("%*sS<%s>\n", indent, "", "port"); |
3316 | return 24+8; |
3397 | return 24+8; |
… | |
… | |
3319 | printf ("%*sS<%s>\n", indent, "", "vector"); |
3400 | printf ("%*sS<%s>\n", indent, "", "vector"); |
3320 | return 24+8; |
3401 | return 24+8; |
3321 | |
3402 | |
3322 | case T_ENVIRONMENT: |
3403 | case T_ENVIRONMENT: |
3323 | printf ("%*sS<%s>\n", indent, "", "environment"); |
3404 | printf ("%*sS<%s>\n", indent, "", "environment"); |
3324 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
3405 | return 0 + dtree (SCHEME_A_ indent + 3, car (x)); |
3325 | |
3406 | |
3326 | default: |
3407 | default: |
3327 | printf ("unhandled type %d\n", type (x)); |
3408 | printf ("unhandled type %d\n", type (x)); |
3328 | break; |
3409 | break; |
3329 | } |
3410 | } |
3330 | } |
3411 | } |
3331 | #endif |
3412 | #endif |
3332 | |
3413 | |
3333 | static int |
3414 | /* syntax, eval, core, ... */ |
|
|
3415 | ecb_hot static int |
3334 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3416 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3335 | { |
3417 | { |
3336 | pointer args = SCHEME_V->args; |
3418 | pointer args = SCHEME_V->args; |
3337 | pointer x, y; |
3419 | pointer x, y; |
3338 | |
3420 | |
3339 | switch (op) |
3421 | switch (op) |
3340 | { |
3422 | { |
3341 | #if EXPERIMENT //D |
3423 | #if EXPERIMENT //D |
3342 | case OP_DEBUG: |
3424 | case OP_DEBUG: |
3343 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
3425 | { |
|
|
3426 | uint32_t len = compile_closure (SCHEME_A_ car (args)); |
|
|
3427 | printf ("len = %d\n", len); |
3344 | printf ("\n"); |
3428 | printf ("\n"); |
3345 | s_return (S_T); |
3429 | s_return (S_T); |
|
|
3430 | } |
3346 | #endif |
3431 | #endif |
3347 | case OP_LOAD: /* load */ |
3432 | case OP_LOAD: /* load */ |
3348 | if (file_interactive (SCHEME_A)) |
3433 | if (file_interactive (SCHEME_A)) |
3349 | { |
3434 | { |
3350 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3435 | putstr (SCHEME_A_ "Loading "); |
3351 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3436 | putstr (SCHEME_A_ strvalue (car (args))); |
|
|
3437 | putcharacter (SCHEME_A_ '\n'); |
3352 | } |
3438 | } |
3353 | |
3439 | |
3354 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3440 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3355 | Error_1 ("unable to open", car (args)); |
3441 | Error_1 ("unable to open", car (args)); |
3356 | else |
3442 | |
3357 | { |
|
|
3358 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3443 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3359 | s_goto (OP_T0LVL); |
3444 | s_goto (OP_T0LVL); |
3360 | } |
|
|
3361 | |
3445 | |
3362 | case OP_T0LVL: /* top level */ |
3446 | case OP_T0LVL: /* top level */ |
3363 | |
3447 | |
3364 | /* If we reached the end of file, this loop is done. */ |
3448 | /* If we reached the end of file, this loop is done. */ |
3365 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3449 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3366 | { |
3450 | { |
3367 | if (SCHEME_V->file_i == 0) |
3451 | if (SCHEME_V->file_i == 0) |
3368 | { |
3452 | { |
3369 | SCHEME_V->args = NIL; |
3453 | SCHEME_V->args = NIL; |
3370 | s_goto (OP_QUIT); |
3454 | s_goto (OP_QUIT); |
… | |
… | |
3381 | /* If interactive, be nice to user. */ |
3465 | /* If interactive, be nice to user. */ |
3382 | if (file_interactive (SCHEME_A)) |
3466 | if (file_interactive (SCHEME_A)) |
3383 | { |
3467 | { |
3384 | SCHEME_V->envir = SCHEME_V->global_env; |
3468 | SCHEME_V->envir = SCHEME_V->global_env; |
3385 | dump_stack_reset (SCHEME_A); |
3469 | dump_stack_reset (SCHEME_A); |
3386 | putstr (SCHEME_A_ "\n"); |
3470 | putcharacter (SCHEME_A_ '\n'); |
3387 | putstr (SCHEME_A_ prompt); |
3471 | putstr (SCHEME_A_ prompt); |
3388 | } |
3472 | } |
3389 | |
3473 | |
3390 | /* Set up another iteration of REPL */ |
3474 | /* Set up another iteration of REPL */ |
3391 | SCHEME_V->nesting = 0; |
3475 | SCHEME_V->nesting = 0; |
… | |
… | |
3426 | { |
3510 | { |
3427 | SCHEME_V->print_flag = 1; |
3511 | SCHEME_V->print_flag = 1; |
3428 | SCHEME_V->args = SCHEME_V->value; |
3512 | SCHEME_V->args = SCHEME_V->value; |
3429 | s_goto (OP_P0LIST); |
3513 | s_goto (OP_P0LIST); |
3430 | } |
3514 | } |
3431 | else |
3515 | |
3432 | s_return (SCHEME_V->value); |
3516 | s_return (SCHEME_V->value); |
3433 | |
3517 | |
3434 | case OP_EVAL: /* main part of evaluation */ |
3518 | case OP_EVAL: /* main part of evaluation */ |
3435 | #if USE_TRACING |
3519 | #if USE_TRACING |
3436 | if (SCHEME_V->tracing) |
3520 | if (SCHEME_V->tracing) |
3437 | { |
3521 | { |
… | |
… | |
3448 | #endif |
3532 | #endif |
3449 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3533 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3450 | { |
3534 | { |
3451 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3535 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3452 | |
3536 | |
3453 | if (x != NIL) |
3537 | if (x == NIL) |
3454 | s_return (slot_value_in_env (x)); |
|
|
3455 | else |
|
|
3456 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3538 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3539 | |
|
|
3540 | s_return (slot_value_in_env (x)); |
3457 | } |
3541 | } |
3458 | else if (is_pair (SCHEME_V->code)) |
3542 | else if (is_pair (SCHEME_V->code)) |
3459 | { |
3543 | { |
3460 | x = car (SCHEME_V->code); |
3544 | x = car (SCHEME_V->code); |
3461 | |
3545 | |
… | |
… | |
3470 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3554 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3471 | SCHEME_V->code = x; |
3555 | SCHEME_V->code = x; |
3472 | s_goto (OP_EVAL); |
3556 | s_goto (OP_EVAL); |
3473 | } |
3557 | } |
3474 | } |
3558 | } |
3475 | else |
3559 | |
3476 | s_return (SCHEME_V->code); |
3560 | s_return (SCHEME_V->code); |
3477 | |
3561 | |
3478 | case OP_E0ARGS: /* eval arguments */ |
3562 | case OP_E0ARGS: /* eval arguments */ |
3479 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3563 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3480 | { |
3564 | { |
3481 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3565 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
… | |
… | |
3538 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3622 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3539 | else if (is_foreign (SCHEME_V->code)) |
3623 | else if (is_foreign (SCHEME_V->code)) |
3540 | { |
3624 | { |
3541 | /* Keep nested calls from GC'ing the arglist */ |
3625 | /* Keep nested calls from GC'ing the arglist */ |
3542 | push_recent_alloc (SCHEME_A_ args, NIL); |
3626 | push_recent_alloc (SCHEME_A_ args, NIL); |
3543 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3627 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3544 | |
3628 | |
3545 | s_return (x); |
3629 | s_return (x); |
3546 | } |
3630 | } |
3547 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3631 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3548 | { |
3632 | { |
… | |
… | |
3653 | else |
3737 | else |
3654 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3738 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3655 | |
3739 | |
3656 | s_return (SCHEME_V->code); |
3740 | s_return (SCHEME_V->code); |
3657 | |
3741 | |
3658 | |
|
|
3659 | case OP_DEFP: /* defined? */ |
3742 | case OP_DEFP: /* defined? */ |
3660 | x = SCHEME_V->envir; |
3743 | x = SCHEME_V->envir; |
3661 | |
3744 | |
3662 | if (cdr (args) != NIL) |
3745 | if (cdr (args) != NIL) |
3663 | x = cadr (args); |
3746 | x = cadr (args); |
… | |
… | |
3680 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3763 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3681 | s_return (SCHEME_V->value); |
3764 | s_return (SCHEME_V->value); |
3682 | } |
3765 | } |
3683 | else |
3766 | else |
3684 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3767 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3685 | |
|
|
3686 | |
3768 | |
3687 | case OP_BEGIN: /* begin */ |
3769 | case OP_BEGIN: /* begin */ |
3688 | if (!is_pair (SCHEME_V->code)) |
3770 | if (!is_pair (SCHEME_V->code)) |
3689 | s_return (SCHEME_V->code); |
3771 | s_return (SCHEME_V->code); |
3690 | |
3772 | |
… | |
… | |
3702 | case OP_IF1: /* if */ |
3784 | case OP_IF1: /* if */ |
3703 | if (is_true (SCHEME_V->value)) |
3785 | if (is_true (SCHEME_V->value)) |
3704 | SCHEME_V->code = car (SCHEME_V->code); |
3786 | SCHEME_V->code = car (SCHEME_V->code); |
3705 | else |
3787 | else |
3706 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
3788 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
|
|
3789 | |
3707 | s_goto (OP_EVAL); |
3790 | s_goto (OP_EVAL); |
3708 | |
3791 | |
3709 | case OP_LET0: /* let */ |
3792 | case OP_LET0: /* let */ |
3710 | SCHEME_V->args = NIL; |
3793 | SCHEME_V->args = NIL; |
3711 | SCHEME_V->value = SCHEME_V->code; |
3794 | SCHEME_V->value = SCHEME_V->code; |
… | |
… | |
3867 | } |
3950 | } |
3868 | else |
3951 | else |
3869 | { |
3952 | { |
3870 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3953 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3871 | s_return (NIL); |
3954 | s_return (NIL); |
3872 | else |
3955 | |
3873 | { |
|
|
3874 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3956 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3875 | SCHEME_V->code = caar (SCHEME_V->code); |
3957 | SCHEME_V->code = caar (SCHEME_V->code); |
3876 | s_goto (OP_EVAL); |
3958 | s_goto (OP_EVAL); |
3877 | } |
|
|
3878 | } |
3959 | } |
3879 | |
3960 | |
3880 | case OP_DELAY: /* delay */ |
3961 | case OP_DELAY: /* delay */ |
3881 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3962 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3882 | set_typeflag (x, T_PROMISE); |
3963 | set_typeflag (x, T_PROMISE); |
… | |
… | |
3893 | case OP_AND1: /* and */ |
3974 | case OP_AND1: /* and */ |
3894 | if (is_false (SCHEME_V->value)) |
3975 | if (is_false (SCHEME_V->value)) |
3895 | s_return (SCHEME_V->value); |
3976 | s_return (SCHEME_V->value); |
3896 | else if (SCHEME_V->code == NIL) |
3977 | else if (SCHEME_V->code == NIL) |
3897 | s_return (SCHEME_V->value); |
3978 | s_return (SCHEME_V->value); |
3898 | else |
3979 | |
3899 | { |
|
|
3900 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3980 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3901 | SCHEME_V->code = car (SCHEME_V->code); |
3981 | SCHEME_V->code = car (SCHEME_V->code); |
3902 | s_goto (OP_EVAL); |
3982 | s_goto (OP_EVAL); |
3903 | } |
|
|
3904 | |
3983 | |
3905 | case OP_OR0: /* or */ |
3984 | case OP_OR0: /* or */ |
3906 | if (SCHEME_V->code == NIL) |
3985 | if (SCHEME_V->code == NIL) |
3907 | s_return (S_F); |
3986 | s_return (S_F); |
3908 | |
3987 | |
… | |
… | |
3913 | case OP_OR1: /* or */ |
3992 | case OP_OR1: /* or */ |
3914 | if (is_true (SCHEME_V->value)) |
3993 | if (is_true (SCHEME_V->value)) |
3915 | s_return (SCHEME_V->value); |
3994 | s_return (SCHEME_V->value); |
3916 | else if (SCHEME_V->code == NIL) |
3995 | else if (SCHEME_V->code == NIL) |
3917 | s_return (SCHEME_V->value); |
3996 | s_return (SCHEME_V->value); |
3918 | else |
3997 | |
3919 | { |
|
|
3920 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3998 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3921 | SCHEME_V->code = car (SCHEME_V->code); |
3999 | SCHEME_V->code = car (SCHEME_V->code); |
3922 | s_goto (OP_EVAL); |
4000 | s_goto (OP_EVAL); |
3923 | } |
|
|
3924 | |
4001 | |
3925 | case OP_C0STREAM: /* cons-stream */ |
4002 | case OP_C0STREAM: /* cons-stream */ |
3926 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
4003 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3927 | SCHEME_V->code = car (SCHEME_V->code); |
4004 | SCHEME_V->code = car (SCHEME_V->code); |
3928 | s_goto (OP_EVAL); |
4005 | s_goto (OP_EVAL); |
… | |
… | |
3993 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
4070 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
3994 | SCHEME_V->code = caar (x); |
4071 | SCHEME_V->code = caar (x); |
3995 | s_goto (OP_EVAL); |
4072 | s_goto (OP_EVAL); |
3996 | } |
4073 | } |
3997 | } |
4074 | } |
3998 | else |
4075 | |
3999 | s_return (NIL); |
4076 | s_return (NIL); |
4000 | |
4077 | |
4001 | case OP_CASE2: /* case */ |
4078 | case OP_CASE2: /* case */ |
4002 | if (is_true (SCHEME_V->value)) |
4079 | if (is_true (SCHEME_V->value)) |
4003 | s_goto (OP_BEGIN); |
4080 | s_goto (OP_BEGIN); |
4004 | else |
4081 | |
4005 | s_return (NIL); |
4082 | s_return (NIL); |
4006 | |
4083 | |
4007 | case OP_PAPPLY: /* apply */ |
4084 | case OP_PAPPLY: /* apply */ |
4008 | SCHEME_V->code = car (args); |
4085 | SCHEME_V->code = car (args); |
4009 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
4086 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
4010 | /*SCHEME_V->args = cadr(args); */ |
4087 | /*SCHEME_V->args = cadr(args); */ |
… | |
… | |
4024 | } |
4101 | } |
4025 | |
4102 | |
4026 | if (USE_ERROR_CHECKING) abort (); |
4103 | if (USE_ERROR_CHECKING) abort (); |
4027 | } |
4104 | } |
4028 | |
4105 | |
4029 | static int |
4106 | /* math, cxr */ |
|
|
4107 | ecb_hot static int |
4030 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4108 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4031 | { |
4109 | { |
4032 | pointer args = SCHEME_V->args; |
4110 | pointer args = SCHEME_V->args; |
4033 | pointer x = car (args); |
4111 | pointer x = car (args); |
4034 | num v; |
4112 | num v; |
4035 | |
4113 | |
4036 | switch (op) |
4114 | switch (op) |
4037 | { |
4115 | { |
4038 | #if USE_MATH |
4116 | #if USE_MATH |
4039 | case OP_INEX2EX: /* inexact->exact */ |
4117 | case OP_INEX2EX: /* inexact->exact */ |
4040 | { |
|
|
4041 | if (is_integer (x)) |
4118 | if (!is_integer (x)) |
4042 | s_return (x); |
4119 | { |
4043 | |
|
|
4044 | RVALUE r = rvalue_unchecked (x); |
4120 | RVALUE r = rvalue_unchecked (x); |
4045 | |
4121 | |
4046 | if (r == (RVALUE)(IVALUE)r) |
4122 | if (r == (RVALUE)(IVALUE)r) |
4047 | s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); |
4123 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
4048 | else |
4124 | else |
4049 | Error_1 ("inexact->exact: not integral:", x); |
4125 | Error_1 ("inexact->exact: not integral:", x); |
4050 | } |
4126 | } |
4051 | |
4127 | |
|
|
4128 | s_return (x); |
|
|
4129 | |
|
|
4130 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4131 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4132 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4133 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
|
|
4134 | |
|
|
4135 | 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)))); |
4136 | 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)))); |
4137 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
|
|
4138 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
4054 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4139 | 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)))); |
4140 | 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)))); |
4141 | 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)))); |
4142 | 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)))); |
4143 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4059 | |
4144 | |
4060 | case OP_ATAN: |
4145 | case OP_ATAN: |
|
|
4146 | s_return (mk_real (SCHEME_A_ |
4061 | if (cdr (args) == NIL) |
4147 | cdr (args) == NIL |
4062 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4148 | ? atan (rvalue (x)) |
4063 | else |
4149 | : 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 | |
4150 | |
4072 | case OP_EXPT: |
4151 | case OP_EXPT: |
4073 | { |
4152 | { |
4074 | RVALUE result; |
4153 | RVALUE result; |
4075 | int real_result = 1; |
4154 | int real_result = 1; |
… | |
… | |
4098 | if (real_result) |
4177 | if (real_result) |
4099 | s_return (mk_real (SCHEME_A_ result)); |
4178 | s_return (mk_real (SCHEME_A_ result)); |
4100 | else |
4179 | else |
4101 | s_return (mk_integer (SCHEME_A_ result)); |
4180 | s_return (mk_integer (SCHEME_A_ result)); |
4102 | } |
4181 | } |
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 |
4182 | #endif |
4119 | |
4183 | |
4120 | case OP_ADD: /* + */ |
4184 | case OP_ADD: /* + */ |
4121 | v = num_zero; |
4185 | v = num_zero; |
4122 | |
4186 | |
… | |
… | |
4424 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4488 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4425 | |
4489 | |
4426 | s_return (newstr); |
4490 | s_return (newstr); |
4427 | } |
4491 | } |
4428 | |
4492 | |
4429 | case OP_SUBSTR: /* substring */ |
4493 | case OP_STRING_COPY: /* substring/string-copy */ |
4430 | { |
4494 | { |
4431 | char *str = strvalue (x); |
4495 | char *str = strvalue (x); |
4432 | int index0 = ivalue_unchecked (cadr (args)); |
4496 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4433 | int index1; |
4497 | int index1; |
4434 | int len; |
4498 | int len; |
4435 | |
4499 | |
4436 | if (index0 > strlength (x)) |
4500 | if (index0 > strlength (x)) |
4437 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4501 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4438 | |
4502 | |
4439 | if (cddr (args) != NIL) |
4503 | if (cddr (args) != NIL) |
4440 | { |
4504 | { |
4441 | index1 = ivalue_unchecked (caddr (args)); |
4505 | index1 = ivalue_unchecked (caddr (args)); |
4442 | |
4506 | |
4443 | if (index1 > strlength (x) || index1 < index0) |
4507 | if (index1 > strlength (x) || index1 < index0) |
4444 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4508 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4445 | } |
4509 | } |
4446 | else |
4510 | else |
4447 | index1 = strlength (x); |
4511 | index1 = strlength (x); |
4448 | |
4512 | |
4449 | len = index1 - index0; |
4513 | len = index1 - index0; |
4450 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4514 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4451 | memcpy (strvalue (x), str + index0, len); |
|
|
4452 | strvalue (x)[len] = 0; |
|
|
4453 | |
4515 | |
4454 | s_return (x); |
4516 | s_return (x); |
4455 | } |
4517 | } |
4456 | |
4518 | |
4457 | case OP_VECTOR: /* vector */ |
4519 | case OP_VECTOR: /* vector */ |
… | |
… | |
4531 | } |
4593 | } |
4532 | |
4594 | |
4533 | if (USE_ERROR_CHECKING) abort (); |
4595 | if (USE_ERROR_CHECKING) abort (); |
4534 | } |
4596 | } |
4535 | |
4597 | |
4536 | static int |
4598 | /* relational ops */ |
|
|
4599 | ecb_hot static int |
4537 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4600 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4538 | { |
4601 | { |
4539 | pointer x = SCHEME_V->args; |
4602 | pointer x = SCHEME_V->args; |
4540 | |
4603 | |
4541 | for (;;) |
4604 | for (;;) |
… | |
… | |
4562 | } |
4625 | } |
4563 | |
4626 | |
4564 | s_return (S_T); |
4627 | s_return (S_T); |
4565 | } |
4628 | } |
4566 | |
4629 | |
4567 | static int |
4630 | /* predicates */ |
|
|
4631 | ecb_hot static int |
4568 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4632 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4569 | { |
4633 | { |
4570 | pointer args = SCHEME_V->args; |
4634 | pointer args = SCHEME_V->args; |
4571 | pointer a = car (args); |
4635 | pointer a = car (args); |
4572 | pointer d = cdr (args); |
4636 | pointer d = cdr (args); |
… | |
… | |
4619 | } |
4683 | } |
4620 | |
4684 | |
4621 | s_retbool (r); |
4685 | s_retbool (r); |
4622 | } |
4686 | } |
4623 | |
4687 | |
4624 | static int |
4688 | /* promises, list ops, ports */ |
|
|
4689 | ecb_hot static int |
4625 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4690 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4626 | { |
4691 | { |
4627 | pointer args = SCHEME_V->args; |
4692 | pointer args = SCHEME_V->args; |
4628 | pointer a = car (args); |
4693 | pointer a = car (args); |
4629 | pointer x, y; |
4694 | pointer x, y; |
… | |
… | |
4642 | } |
4707 | } |
4643 | else |
4708 | else |
4644 | s_return (SCHEME_V->code); |
4709 | s_return (SCHEME_V->code); |
4645 | |
4710 | |
4646 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4711 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4647 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4712 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4648 | s_return (SCHEME_V->value); |
4713 | s_return (SCHEME_V->value); |
4649 | |
4714 | |
4650 | #if USE_PORTS |
4715 | #if USE_PORTS |
|
|
4716 | |
|
|
4717 | case OP_EOF_OBJECT: /* eof-object */ |
|
|
4718 | s_return (S_EOF); |
4651 | |
4719 | |
4652 | case OP_WRITE: /* write */ |
4720 | case OP_WRITE: /* write */ |
4653 | case OP_DISPLAY: /* display */ |
4721 | case OP_DISPLAY: /* display */ |
4654 | case OP_WRITE_CHAR: /* write-char */ |
4722 | case OP_WRITE_CHAR: /* write-char */ |
4655 | if (is_pair (cdr (SCHEME_V->args))) |
4723 | if (is_pair (cdr (SCHEME_V->args))) |
… | |
… | |
4669 | else |
4737 | else |
4670 | SCHEME_V->print_flag = 0; |
4738 | SCHEME_V->print_flag = 0; |
4671 | |
4739 | |
4672 | s_goto (OP_P0LIST); |
4740 | s_goto (OP_P0LIST); |
4673 | |
4741 | |
|
|
4742 | //TODO: move to scheme |
4674 | case OP_NEWLINE: /* newline */ |
4743 | case OP_NEWLINE: /* newline */ |
4675 | if (is_pair (args)) |
4744 | if (is_pair (args)) |
4676 | { |
4745 | { |
4677 | if (a != SCHEME_V->outport) |
4746 | if (a != SCHEME_V->outport) |
4678 | { |
4747 | { |
… | |
… | |
4680 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4749 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4681 | SCHEME_V->outport = a; |
4750 | SCHEME_V->outport = a; |
4682 | } |
4751 | } |
4683 | } |
4752 | } |
4684 | |
4753 | |
4685 | putstr (SCHEME_A_ "\n"); |
4754 | putcharacter (SCHEME_A_ '\n'); |
4686 | s_return (S_T); |
4755 | s_return (S_T); |
4687 | #endif |
4756 | #endif |
4688 | |
4757 | |
4689 | case OP_ERR0: /* error */ |
4758 | case OP_ERR0: /* error */ |
4690 | SCHEME_V->retcode = -1; |
4759 | SCHEME_V->retcode = -1; |
… | |
… | |
4699 | putstr (SCHEME_A_ strvalue (car (args))); |
4768 | putstr (SCHEME_A_ strvalue (car (args))); |
4700 | SCHEME_V->args = cdr (args); |
4769 | SCHEME_V->args = cdr (args); |
4701 | s_goto (OP_ERR1); |
4770 | s_goto (OP_ERR1); |
4702 | |
4771 | |
4703 | case OP_ERR1: /* error */ |
4772 | case OP_ERR1: /* error */ |
4704 | putstr (SCHEME_A_ " "); |
4773 | putcharacter (SCHEME_A_ ' '); |
4705 | |
4774 | |
4706 | if (args != NIL) |
4775 | if (args != NIL) |
4707 | { |
4776 | { |
4708 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4777 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4709 | SCHEME_V->args = a; |
4778 | SCHEME_V->args = a; |
4710 | SCHEME_V->print_flag = 1; |
4779 | SCHEME_V->print_flag = 1; |
4711 | s_goto (OP_P0LIST); |
4780 | s_goto (OP_P0LIST); |
4712 | } |
4781 | } |
4713 | else |
4782 | else |
4714 | { |
4783 | { |
4715 | putstr (SCHEME_A_ "\n"); |
4784 | putcharacter (SCHEME_A_ '\n'); |
4716 | |
4785 | |
4717 | if (SCHEME_V->interactive_repl) |
4786 | if (SCHEME_V->interactive_repl) |
4718 | s_goto (OP_T0LVL); |
4787 | s_goto (OP_T0LVL); |
4719 | else |
4788 | else |
4720 | return -1; |
4789 | return -1; |
… | |
… | |
4797 | SCHEME_V->gc_verbose = (a != S_F); |
4866 | SCHEME_V->gc_verbose = (a != S_F); |
4798 | s_retbool (was); |
4867 | s_retbool (was); |
4799 | } |
4868 | } |
4800 | |
4869 | |
4801 | case OP_NEWSEGMENT: /* new-segment */ |
4870 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4871 | #if 0 |
4802 | if (!is_pair (args) || !is_number (a)) |
4872 | if (!is_pair (args) || !is_number (a)) |
4803 | Error_0 ("new-segment: argument must be a number"); |
4873 | Error_0 ("new-segment: argument must be a number"); |
4804 | |
4874 | #endif |
4805 | alloc_cellseg (SCHEME_A_ ivalue (a)); |
4875 | s_retbool (alloc_cellseg (SCHEME_A)); |
4806 | |
|
|
4807 | s_return (S_T); |
|
|
4808 | |
4876 | |
4809 | case OP_OBLIST: /* oblist */ |
4877 | case OP_OBLIST: /* oblist */ |
4810 | s_return (oblist_all_symbols (SCHEME_A)); |
4878 | s_return (oblist_all_symbols (SCHEME_A)); |
4811 | |
4879 | |
4812 | #if USE_PORTS |
4880 | #if USE_PORTS |
… | |
… | |
4882 | s_return (p == NIL ? S_F : p); |
4950 | s_return (p == NIL ? S_F : p); |
4883 | } |
4951 | } |
4884 | |
4952 | |
4885 | case OP_GET_OUTSTRING: /* get-output-string */ |
4953 | case OP_GET_OUTSTRING: /* get-output-string */ |
4886 | { |
4954 | { |
4887 | port *p; |
4955 | port *p = port (a); |
4888 | |
4956 | |
4889 | if ((p = a->object.port)->kind & port_string) |
4957 | if (p->kind & port_string) |
4890 | { |
4958 | { |
4891 | off_t size; |
4959 | off_t size; |
4892 | char *str; |
4960 | char *str; |
4893 | |
4961 | |
4894 | size = p->rep.string.curr - p->rep.string.start + 1; |
4962 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4929 | } |
4997 | } |
4930 | |
4998 | |
4931 | if (USE_ERROR_CHECKING) abort (); |
4999 | if (USE_ERROR_CHECKING) abort (); |
4932 | } |
5000 | } |
4933 | |
5001 | |
4934 | static int |
5002 | /* reading */ |
|
|
5003 | ecb_cold static int |
4935 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
5004 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4936 | { |
5005 | { |
4937 | pointer args = SCHEME_V->args; |
5006 | pointer args = SCHEME_V->args; |
4938 | pointer x; |
5007 | pointer x; |
4939 | |
5008 | |
… | |
… | |
4999 | int res; |
5068 | int res; |
5000 | |
5069 | |
5001 | if (is_pair (args)) |
5070 | if (is_pair (args)) |
5002 | p = car (args); |
5071 | p = car (args); |
5003 | |
5072 | |
5004 | res = p->object.port->kind & port_string; |
5073 | res = port (p)->kind & port_string; |
5005 | |
5074 | |
5006 | s_retbool (res); |
5075 | s_retbool (res); |
5007 | } |
5076 | } |
5008 | |
5077 | |
5009 | case OP_SET_INPORT: /* set-input-port */ |
5078 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
5208 | pointer b = cdr (args); |
5277 | pointer b = cdr (args); |
5209 | int ok_abbr = ok_abbrev (b); |
5278 | int ok_abbr = ok_abbrev (b); |
5210 | SCHEME_V->args = car (b); |
5279 | SCHEME_V->args = car (b); |
5211 | |
5280 | |
5212 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5281 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5213 | putstr (SCHEME_A_ "'"); |
5282 | putcharacter (SCHEME_A_ '\''); |
5214 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5283 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5215 | putstr (SCHEME_A_ "`"); |
5284 | putcharacter (SCHEME_A_ '`'); |
5216 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5285 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5217 | putstr (SCHEME_A_ ","); |
5286 | putcharacter (SCHEME_A_ ','); |
5218 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5287 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5219 | putstr (SCHEME_A_ ",@"); |
5288 | putstr (SCHEME_A_ ",@"); |
5220 | else |
5289 | else |
5221 | { |
5290 | { |
5222 | putstr (SCHEME_A_ "("); |
5291 | putcharacter (SCHEME_A_ '('); |
5223 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5292 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5224 | SCHEME_V->args = a; |
5293 | SCHEME_V->args = a; |
5225 | } |
5294 | } |
5226 | |
5295 | |
5227 | s_goto (OP_P0LIST); |
5296 | s_goto (OP_P0LIST); |
… | |
… | |
5229 | |
5298 | |
5230 | case OP_P1LIST: |
5299 | case OP_P1LIST: |
5231 | if (is_pair (args)) |
5300 | if (is_pair (args)) |
5232 | { |
5301 | { |
5233 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5302 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5234 | putstr (SCHEME_A_ " "); |
5303 | putcharacter (SCHEME_A_ ' '); |
5235 | SCHEME_V->args = car (args); |
5304 | SCHEME_V->args = car (args); |
5236 | s_goto (OP_P0LIST); |
5305 | s_goto (OP_P0LIST); |
5237 | } |
5306 | } |
5238 | else if (is_vector (args)) |
5307 | else if (is_vector (args)) |
5239 | { |
5308 | { |
… | |
… | |
5247 | { |
5316 | { |
5248 | putstr (SCHEME_A_ " . "); |
5317 | putstr (SCHEME_A_ " . "); |
5249 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5318 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5250 | } |
5319 | } |
5251 | |
5320 | |
5252 | putstr (SCHEME_A_ ")"); |
5321 | putcharacter (SCHEME_A_ ')'); |
5253 | s_return (S_T); |
5322 | s_return (S_T); |
5254 | } |
5323 | } |
5255 | |
5324 | |
5256 | case OP_PVECFROM: |
5325 | case OP_PVECFROM: |
5257 | { |
5326 | { |
… | |
… | |
5259 | pointer vec = car (args); |
5328 | pointer vec = car (args); |
5260 | int len = veclength (vec); |
5329 | int len = veclength (vec); |
5261 | |
5330 | |
5262 | if (i == len) |
5331 | if (i == len) |
5263 | { |
5332 | { |
5264 | putstr (SCHEME_A_ ")"); |
5333 | putcharacter (SCHEME_A_ ')'); |
5265 | s_return (S_T); |
5334 | s_return (S_T); |
5266 | } |
5335 | } |
5267 | else |
5336 | else |
5268 | { |
5337 | { |
5269 | pointer elem = vector_get (vec, i); |
5338 | pointer elem = vector_get (vec, i); |
… | |
… | |
5271 | ivalue_unchecked (cdr (args)) = i + 1; |
5340 | ivalue_unchecked (cdr (args)) = i + 1; |
5272 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5341 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5273 | SCHEME_V->args = elem; |
5342 | SCHEME_V->args = elem; |
5274 | |
5343 | |
5275 | if (i > 0) |
5344 | if (i > 0) |
5276 | putstr (SCHEME_A_ " "); |
5345 | putcharacter (SCHEME_A_ ' '); |
5277 | |
5346 | |
5278 | s_goto (OP_P0LIST); |
5347 | s_goto (OP_P0LIST); |
5279 | } |
5348 | } |
5280 | } |
5349 | } |
5281 | } |
5350 | } |
5282 | |
5351 | |
5283 | if (USE_ERROR_CHECKING) abort (); |
5352 | if (USE_ERROR_CHECKING) abort (); |
5284 | } |
5353 | } |
5285 | |
5354 | |
5286 | static int |
5355 | /* list ops */ |
|
|
5356 | ecb_hot static int |
5287 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5357 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5288 | { |
5358 | { |
5289 | pointer args = SCHEME_V->args; |
5359 | pointer args = SCHEME_V->args; |
5290 | pointer a = car (args); |
5360 | pointer a = car (args); |
5291 | pointer x, y; |
5361 | pointer x, y; |
… | |
… | |
5349 | |
5419 | |
5350 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5420 | /* 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); |
5421 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5352 | |
5422 | |
5353 | typedef int (*test_predicate)(pointer); |
5423 | typedef int (*test_predicate)(pointer); |
5354 | static int |
5424 | |
|
|
5425 | ecb_hot static int |
5355 | tst_any (pointer p) |
5426 | tst_any (pointer p) |
5356 | { |
5427 | { |
5357 | return 1; |
5428 | return 1; |
5358 | } |
5429 | } |
5359 | |
5430 | |
5360 | static int |
5431 | ecb_hot static int |
5361 | tst_inonneg (pointer p) |
5432 | tst_inonneg (pointer p) |
5362 | { |
5433 | { |
5363 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5434 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5364 | } |
5435 | } |
5365 | |
5436 | |
5366 | static int |
5437 | ecb_hot static int |
5367 | tst_is_list (SCHEME_P_ pointer p) |
5438 | tst_is_list (SCHEME_P_ pointer p) |
5368 | { |
5439 | { |
5369 | return p == NIL || is_pair (p); |
5440 | return p == NIL || is_pair (p); |
5370 | } |
5441 | } |
5371 | |
5442 | |
… | |
… | |
5414 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5485 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5415 | #include "opdefines.h" |
5486 | #include "opdefines.h" |
5416 | #undef OP_DEF |
5487 | #undef OP_DEF |
5417 | ; |
5488 | ; |
5418 | |
5489 | |
5419 | static const char * |
5490 | ecb_cold static const char * |
5420 | opname (int idx) |
5491 | opname (int idx) |
5421 | { |
5492 | { |
5422 | const char *name = opnames; |
5493 | const char *name = opnames; |
5423 | |
5494 | |
5424 | /* should do this at compile time, but would require external program, right? */ |
5495 | /* should do this at compile time, but would require external program, right? */ |
… | |
… | |
5426 | name += strlen (name) + 1; |
5497 | name += strlen (name) + 1; |
5427 | |
5498 | |
5428 | return *name ? name : "ILLEGAL"; |
5499 | return *name ? name : "ILLEGAL"; |
5429 | } |
5500 | } |
5430 | |
5501 | |
5431 | static const char * |
5502 | ecb_cold static const char * |
5432 | procname (pointer x) |
5503 | procname (pointer x) |
5433 | { |
5504 | { |
5434 | return opname (procnum (x)); |
5505 | return opname (procnum (x)); |
5435 | } |
5506 | } |
5436 | |
5507 | |
… | |
… | |
5456 | #undef OP_DEF |
5527 | #undef OP_DEF |
5457 | {0} |
5528 | {0} |
5458 | }; |
5529 | }; |
5459 | |
5530 | |
5460 | /* kernel of this interpreter */ |
5531 | /* kernel of this interpreter */ |
5461 | static void ecb_hot |
5532 | ecb_hot static void |
5462 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5533 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5463 | { |
5534 | { |
5464 | SCHEME_V->op = op; |
5535 | SCHEME_V->op = op; |
5465 | |
5536 | |
5466 | for (;;) |
5537 | for (;;) |
… | |
… | |
5549 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5620 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5550 | return; |
5621 | return; |
5551 | |
5622 | |
5552 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5623 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5553 | { |
5624 | { |
5554 | xwrstr ("No memory!\n"); |
5625 | putstr (SCHEME_A_ "No memory!\n"); |
5555 | return; |
5626 | return; |
5556 | } |
5627 | } |
5557 | } |
5628 | } |
5558 | } |
5629 | } |
5559 | |
5630 | |
5560 | /* ========== Initialization of internal keywords ========== */ |
5631 | /* ========== Initialization of internal keywords ========== */ |
5561 | |
5632 | |
5562 | static void |
5633 | ecb_cold static void |
5563 | assign_syntax (SCHEME_P_ const char *name) |
5634 | assign_syntax (SCHEME_P_ const char *name) |
5564 | { |
5635 | { |
5565 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5636 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5566 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5637 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5567 | } |
5638 | } |
5568 | |
5639 | |
5569 | static void |
5640 | ecb_cold static void |
5570 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5641 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5571 | { |
5642 | { |
5572 | pointer x = mk_symbol (SCHEME_A_ name); |
5643 | pointer x = mk_symbol (SCHEME_A_ name); |
5573 | pointer y = mk_proc (SCHEME_A_ op); |
5644 | pointer y = mk_proc (SCHEME_A_ op); |
5574 | new_slot_in_env (SCHEME_A_ x, y); |
5645 | new_slot_in_env (SCHEME_A_ x, y); |
… | |
… | |
5582 | ivalue_unchecked (y) = op; |
5653 | ivalue_unchecked (y) = op; |
5583 | return y; |
5654 | return y; |
5584 | } |
5655 | } |
5585 | |
5656 | |
5586 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5657 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5587 | static int |
5658 | ecb_hot static int |
5588 | syntaxnum (pointer p) |
5659 | syntaxnum (pointer p) |
5589 | { |
5660 | { |
5590 | const char *s = strvalue (p); |
5661 | const char *s = strvalue (p); |
5591 | |
5662 | |
5592 | switch (strlength (p)) |
5663 | switch (strlength (p)) |
… | |
… | |
5699 | SCHEME_V->save_inport = NIL; |
5770 | SCHEME_V->save_inport = NIL; |
5700 | SCHEME_V->loadport = NIL; |
5771 | SCHEME_V->loadport = NIL; |
5701 | SCHEME_V->nesting = 0; |
5772 | SCHEME_V->nesting = 0; |
5702 | SCHEME_V->interactive_repl = 0; |
5773 | SCHEME_V->interactive_repl = 0; |
5703 | |
5774 | |
5704 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5775 | if (!alloc_cellseg (SCHEME_A)) |
5705 | { |
5776 | { |
5706 | #if USE_ERROR_CHECKING |
5777 | #if USE_ERROR_CHECKING |
5707 | SCHEME_V->no_memory = 1; |
5778 | SCHEME_V->no_memory = 1; |
5708 | return 0; |
5779 | return 0; |
5709 | #endif |
5780 | #endif |
5710 | } |
5781 | } |
5711 | |
5782 | |
5712 | SCHEME_V->gc_verbose = 0; |
5783 | SCHEME_V->gc_verbose = 0; |
5713 | dump_stack_initialize (SCHEME_A); |
5784 | dump_stack_initialize (SCHEME_A); |
5714 | SCHEME_V->code = NIL; |
5785 | SCHEME_V->code = NIL; |
5715 | SCHEME_V->args = NIL; |
5786 | SCHEME_V->args = NIL; |
5716 | SCHEME_V->envir = NIL; |
5787 | SCHEME_V->envir = NIL; |
|
|
5788 | SCHEME_V->value = NIL; |
5717 | SCHEME_V->tracing = 0; |
5789 | SCHEME_V->tracing = 0; |
5718 | |
5790 | |
5719 | /* init NIL */ |
5791 | /* init NIL */ |
5720 | set_typeflag (NIL, T_ATOM | T_MARK); |
5792 | set_typeflag (NIL, T_ATOM | T_MARK); |
5721 | set_car (NIL, NIL); |
5793 | set_car (NIL, NIL); |
… | |
… | |
5777 | |
5849 | |
5778 | return !SCHEME_V->no_memory; |
5850 | return !SCHEME_V->no_memory; |
5779 | } |
5851 | } |
5780 | |
5852 | |
5781 | #if USE_PORTS |
5853 | #if USE_PORTS |
5782 | void |
5854 | ecb_cold void |
5783 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5855 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5784 | { |
5856 | { |
5785 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5857 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5786 | } |
5858 | } |
5787 | |
5859 | |
5788 | void |
5860 | ecb_cold void |
5789 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5861 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5790 | { |
5862 | { |
5791 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5863 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5792 | } |
5864 | } |
5793 | |
5865 | |
5794 | void |
5866 | ecb_cold void |
5795 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5867 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5796 | { |
5868 | { |
5797 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5869 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5798 | } |
5870 | } |
5799 | |
5871 | |
5800 | void |
5872 | ecb_cold void |
5801 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5873 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5802 | { |
5874 | { |
5803 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5875 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5804 | } |
5876 | } |
5805 | #endif |
5877 | #endif |
5806 | |
5878 | |
5807 | void |
5879 | ecb_cold void |
5808 | scheme_set_external_data (SCHEME_P_ void *p) |
5880 | scheme_set_external_data (SCHEME_P_ void *p) |
5809 | { |
5881 | { |
5810 | SCHEME_V->ext_data = p; |
5882 | SCHEME_V->ext_data = p; |
5811 | } |
5883 | } |
5812 | |
5884 | |
… | |
… | |
5844 | SCHEME_V->loadport = NIL; |
5916 | SCHEME_V->loadport = NIL; |
5845 | SCHEME_V->gc_verbose = 0; |
5917 | SCHEME_V->gc_verbose = 0; |
5846 | gc (SCHEME_A_ NIL, NIL); |
5918 | gc (SCHEME_A_ NIL, NIL); |
5847 | |
5919 | |
5848 | for (i = 0; i <= SCHEME_V->last_cell_seg; i++) |
5920 | for (i = 0; i <= SCHEME_V->last_cell_seg; i++) |
5849 | free (SCHEME_V->alloc_seg[i]); |
5921 | free (SCHEME_V->cell_seg[i]); |
5850 | |
5922 | |
5851 | #if SHOW_ERROR_LINE |
5923 | #if SHOW_ERROR_LINE |
5852 | for (i = 0; i <= SCHEME_V->file_i; i++) |
5924 | for (i = 0; i <= SCHEME_V->file_i; i++) |
5853 | { |
5925 | { |
5854 | if (SCHEME_V->load_stack[i].kind & port_file) |
5926 | if (SCHEME_V->load_stack[i].kind & port_file) |
… | |
… | |
5860 | } |
5932 | } |
5861 | } |
5933 | } |
5862 | #endif |
5934 | #endif |
5863 | } |
5935 | } |
5864 | |
5936 | |
5865 | void |
5937 | ecb_cold void |
5866 | scheme_load_file (SCHEME_P_ int fin) |
5938 | scheme_load_file (SCHEME_P_ int fin) |
5867 | { |
5939 | { |
5868 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5940 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5869 | } |
5941 | } |
5870 | |
5942 | |
5871 | void |
5943 | ecb_cold void |
5872 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5944 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5873 | { |
5945 | { |
5874 | dump_stack_reset (SCHEME_A); |
5946 | dump_stack_reset (SCHEME_A); |
5875 | SCHEME_V->envir = SCHEME_V->global_env; |
5947 | SCHEME_V->envir = SCHEME_V->global_env; |
5876 | SCHEME_V->file_i = 0; |
5948 | SCHEME_V->file_i = 0; |
5877 | SCHEME_V->load_stack[0].unget = -1; |
5949 | SCHEME_V->load_stack[0].unget = -1; |
5878 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5950 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5879 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5951 | 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); |
5952 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5882 | #endif |
|
|
5883 | SCHEME_V->retcode = 0; |
5953 | SCHEME_V->retcode = 0; |
5884 | |
5954 | |
5885 | #if USE_PORTS |
|
|
5886 | if (fin == STDIN_FILENO) |
5955 | if (fin == STDIN_FILENO) |
5887 | SCHEME_V->interactive_repl = 1; |
5956 | SCHEME_V->interactive_repl = 1; |
5888 | #endif |
|
|
5889 | |
5957 | |
5890 | #if USE_PORTS |
5958 | #if USE_PORTS |
5891 | #if SHOW_ERROR_LINE |
5959 | #if SHOW_ERROR_LINE |
5892 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5960 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5893 | |
5961 | |
… | |
… | |
5897 | #endif |
5965 | #endif |
5898 | |
5966 | |
5899 | SCHEME_V->inport = SCHEME_V->loadport; |
5967 | SCHEME_V->inport = SCHEME_V->loadport; |
5900 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5968 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5901 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5969 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
|
|
5970 | |
5902 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5971 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5903 | |
5972 | |
5904 | if (SCHEME_V->retcode == 0) |
5973 | if (SCHEME_V->retcode == 0) |
5905 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5974 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5906 | } |
5975 | } |
5907 | |
5976 | |
5908 | void |
5977 | ecb_cold void |
5909 | scheme_load_string (SCHEME_P_ const char *cmd) |
5978 | scheme_load_string (SCHEME_P_ const char *cmd) |
5910 | { |
5979 | { |
|
|
5980 | #if USE_PORTs |
5911 | dump_stack_reset (SCHEME_A); |
5981 | dump_stack_reset (SCHEME_A); |
5912 | SCHEME_V->envir = SCHEME_V->global_env; |
5982 | SCHEME_V->envir = SCHEME_V->global_env; |
5913 | SCHEME_V->file_i = 0; |
5983 | SCHEME_V->file_i = 0; |
5914 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5984 | 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 */ |
5985 | 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); |
5986 | 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; |
5987 | 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); |
5988 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5920 | #endif |
|
|
5921 | SCHEME_V->retcode = 0; |
5989 | SCHEME_V->retcode = 0; |
5922 | SCHEME_V->interactive_repl = 0; |
5990 | SCHEME_V->interactive_repl = 0; |
5923 | SCHEME_V->inport = SCHEME_V->loadport; |
5991 | SCHEME_V->inport = SCHEME_V->loadport; |
5924 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5992 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5925 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5993 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5926 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5994 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5927 | |
5995 | |
5928 | if (SCHEME_V->retcode == 0) |
5996 | if (SCHEME_V->retcode == 0) |
5929 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5997 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
|
|
5998 | #else |
|
|
5999 | abort (); |
|
|
6000 | #endif |
5930 | } |
6001 | } |
5931 | |
6002 | |
5932 | void |
6003 | ecb_cold void |
5933 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
6004 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5934 | { |
6005 | { |
5935 | pointer x; |
6006 | pointer x; |
5936 | |
6007 | |
5937 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
6008 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
… | |
… | |
5942 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
6013 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5943 | } |
6014 | } |
5944 | |
6015 | |
5945 | #if !STANDALONE |
6016 | #if !STANDALONE |
5946 | |
6017 | |
5947 | void |
6018 | ecb_cold void |
5948 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
6019 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5949 | { |
6020 | { |
5950 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
6021 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5951 | } |
6022 | } |
5952 | |
6023 | |
5953 | void |
6024 | ecb_cold void |
5954 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
6025 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5955 | { |
6026 | { |
5956 | int i; |
6027 | int i; |
5957 | |
6028 | |
5958 | for (i = 0; i < count; i++) |
6029 | for (i = 0; i < count; i++) |
5959 | scheme_register_foreign_func (SCHEME_A_ list + i); |
6030 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5960 | } |
6031 | } |
5961 | |
6032 | |
5962 | pointer |
6033 | ecb_cold pointer |
5963 | scheme_apply0 (SCHEME_P_ const char *procname) |
6034 | scheme_apply0 (SCHEME_P_ const char *procname) |
5964 | { |
6035 | { |
5965 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
6036 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
5966 | } |
6037 | } |
5967 | |
6038 | |
5968 | void |
6039 | ecb_cold void |
5969 | save_from_C_call (SCHEME_P) |
6040 | save_from_C_call (SCHEME_P) |
5970 | { |
6041 | { |
5971 | pointer saved_data = cons (car (S_SINK), |
6042 | pointer saved_data = cons (car (S_SINK), |
5972 | cons (SCHEME_V->envir, |
6043 | cons (SCHEME_V->envir, |
5973 | SCHEME_V->dump)); |
6044 | SCHEME_V->dump)); |
… | |
… | |
5977 | /* Truncate the dump stack so TS will return here when done, not |
6048 | /* Truncate the dump stack so TS will return here when done, not |
5978 | directly resume pre-C-call operations. */ |
6049 | directly resume pre-C-call operations. */ |
5979 | dump_stack_reset (SCHEME_A); |
6050 | dump_stack_reset (SCHEME_A); |
5980 | } |
6051 | } |
5981 | |
6052 | |
5982 | void |
6053 | ecb_cold void |
5983 | restore_from_C_call (SCHEME_P) |
6054 | restore_from_C_call (SCHEME_P) |
5984 | { |
6055 | { |
5985 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
6056 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5986 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
6057 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5987 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
6058 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5988 | /* Pop */ |
6059 | /* Pop */ |
5989 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
6060 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5990 | } |
6061 | } |
5991 | |
6062 | |
5992 | /* "func" and "args" are assumed to be already eval'ed. */ |
6063 | /* "func" and "args" are assumed to be already eval'ed. */ |
5993 | pointer |
6064 | ecb_cold pointer |
5994 | scheme_call (SCHEME_P_ pointer func, pointer args) |
6065 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5995 | { |
6066 | { |
5996 | int old_repl = SCHEME_V->interactive_repl; |
6067 | int old_repl = SCHEME_V->interactive_repl; |
5997 | |
6068 | |
5998 | SCHEME_V->interactive_repl = 0; |
6069 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
6005 | SCHEME_V->interactive_repl = old_repl; |
6076 | SCHEME_V->interactive_repl = old_repl; |
6006 | restore_from_C_call (SCHEME_A); |
6077 | restore_from_C_call (SCHEME_A); |
6007 | return SCHEME_V->value; |
6078 | return SCHEME_V->value; |
6008 | } |
6079 | } |
6009 | |
6080 | |
6010 | pointer |
6081 | ecb_cold pointer |
6011 | scheme_eval (SCHEME_P_ pointer obj) |
6082 | scheme_eval (SCHEME_P_ pointer obj) |
6012 | { |
6083 | { |
6013 | int old_repl = SCHEME_V->interactive_repl; |
6084 | int old_repl = SCHEME_V->interactive_repl; |
6014 | |
6085 | |
6015 | SCHEME_V->interactive_repl = 0; |
6086 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
6027 | |
6098 | |
6028 | /* ========== Main ========== */ |
6099 | /* ========== Main ========== */ |
6029 | |
6100 | |
6030 | #if STANDALONE |
6101 | #if STANDALONE |
6031 | |
6102 | |
6032 | int |
6103 | ecb_cold int |
6033 | main (int argc, char **argv) |
6104 | main (int argc, char **argv) |
6034 | { |
6105 | { |
6035 | # if USE_MULTIPLICITY |
6106 | # if USE_MULTIPLICITY |
6036 | scheme ssc; |
6107 | scheme ssc; |
6037 | scheme *const SCHEME_V = &ssc; |
6108 | scheme *const SCHEME_V = &ssc; |
… | |
… | |
6043 | int isfile = 1; |
6114 | int isfile = 1; |
6044 | system ("ps v $PPID");//D |
6115 | system ("ps v $PPID");//D |
6045 | |
6116 | |
6046 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6117 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6047 | { |
6118 | { |
6048 | xwrstr ("Usage: tinyscheme -?\n"); |
6119 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
6049 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6120 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
6050 | xwrstr ("followed by\n"); |
6121 | putstr (SCHEME_A_ "followed by\n"); |
6051 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6122 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
6052 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6123 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6053 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6124 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
6054 | xwrstr ("Use - as filename for stdin.\n"); |
6125 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
6055 | return 1; |
6126 | return 1; |
6056 | } |
6127 | } |
6057 | |
6128 | |
6058 | if (!scheme_init (SCHEME_A)) |
6129 | if (!scheme_init (SCHEME_A)) |
6059 | { |
6130 | { |
6060 | xwrstr ("Could not initialize!\n"); |
6131 | putstr (SCHEME_A_ "Could not initialize!\n"); |
6061 | return 2; |
6132 | return 2; |
6062 | } |
6133 | } |
6063 | |
6134 | |
6064 | # if USE_PORTS |
6135 | # if USE_PORTS |
6065 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6136 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
6078 | } |
6149 | } |
6079 | #endif |
6150 | #endif |
6080 | |
6151 | |
6081 | do |
6152 | do |
6082 | { |
6153 | { |
6083 | #if USE_PORTS |
|
|
6084 | if (strcmp (file_name, "-") == 0) |
6154 | if (strcmp (file_name, "-") == 0) |
6085 | fin = STDIN_FILENO; |
6155 | fin = STDIN_FILENO; |
6086 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
6156 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
6087 | { |
6157 | { |
6088 | pointer args = NIL; |
6158 | pointer args = NIL; |
… | |
… | |
6106 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
6176 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
6107 | |
6177 | |
6108 | } |
6178 | } |
6109 | else |
6179 | else |
6110 | fin = open (file_name, O_RDONLY); |
6180 | fin = open (file_name, O_RDONLY); |
6111 | #endif |
|
|
6112 | |
6181 | |
6113 | if (isfile && fin < 0) |
6182 | if (isfile && fin < 0) |
6114 | { |
6183 | { |
6115 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6184 | putstr (SCHEME_A_ "Could not open file "); |
|
|
6185 | putstr (SCHEME_A_ file_name); |
|
|
6186 | putcharacter (SCHEME_A_ '\n'); |
6116 | } |
6187 | } |
6117 | else |
6188 | else |
6118 | { |
6189 | { |
6119 | if (isfile) |
6190 | if (isfile) |
6120 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6191 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6121 | else |
6192 | else |
6122 | scheme_load_string (SCHEME_A_ file_name); |
6193 | scheme_load_string (SCHEME_A_ file_name); |
6123 | |
6194 | |
6124 | #if USE_PORTS |
|
|
6125 | if (!isfile || fin != STDIN_FILENO) |
6195 | if (!isfile || fin != STDIN_FILENO) |
6126 | { |
6196 | { |
6127 | if (SCHEME_V->retcode != 0) |
6197 | if (SCHEME_V->retcode != 0) |
6128 | { |
6198 | { |
6129 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6199 | putstr (SCHEME_A_ "Errors encountered reading "); |
|
|
6200 | putstr (SCHEME_A_ file_name); |
|
|
6201 | putcharacter (SCHEME_A_ '\n'); |
6130 | } |
6202 | } |
6131 | |
6203 | |
6132 | if (isfile) |
6204 | if (isfile) |
6133 | close (fin); |
6205 | close (fin); |
6134 | } |
6206 | } |
6135 | #endif |
|
|
6136 | } |
6207 | } |
6137 | |
6208 | |
6138 | file_name = *argv++; |
6209 | file_name = *argv++; |
6139 | } |
6210 | } |
6140 | while (file_name != 0); |
6211 | while (file_name != 0); |