… | |
… | |
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 _POSIX_C_SOURCE 200201 |
22 | |
22 | #define _XOPEN_SOURCE 600 |
23 | #if 1 |
23 | #define _GNU_SOURCE 1 /* for malloc mremap */ |
24 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
|
|
25 | #include "malloc.c" |
|
|
26 | #endif |
|
|
27 | |
24 | |
28 | #define SCHEME_SOURCE |
25 | #define SCHEME_SOURCE |
29 | #include "scheme-private.h" |
26 | #include "scheme-private.h" |
30 | #ifndef WIN32 |
27 | #ifndef WIN32 |
31 | # include <unistd.h> |
28 | # include <unistd.h> |
32 | #endif |
29 | #endif |
33 | #if USE_MATH |
30 | #if USE_MATH |
34 | # include <math.h> |
31 | # include <math.h> |
35 | #endif |
32 | #endif |
36 | |
33 | |
|
|
34 | #define ECB_NO_THREADS 1 |
37 | #include "ecb.h" |
35 | #include "ecb.h" |
38 | |
36 | |
39 | #include <sys/types.h> |
37 | #include <sys/types.h> |
40 | #include <sys/stat.h> |
38 | #include <sys/stat.h> |
41 | #include <fcntl.h> |
39 | #include <fcntl.h> |
… | |
… | |
49 | #include <string.h> |
47 | #include <string.h> |
50 | |
48 | |
51 | #include <limits.h> |
49 | #include <limits.h> |
52 | #include <inttypes.h> |
50 | #include <inttypes.h> |
53 | #include <float.h> |
51 | #include <float.h> |
54 | //#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 |
55 | |
60 | |
56 | #if '1' != '0' + 1 \ |
61 | #if '1' != '0' + 1 \ |
57 | || '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 \ |
58 | || '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 \ |
59 | || '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 \ |
… | |
… | |
91 | |
96 | |
92 | #if !USE_MULTIPLICITY |
97 | #if !USE_MULTIPLICITY |
93 | static scheme sc; |
98 | static scheme sc; |
94 | #endif |
99 | #endif |
95 | |
100 | |
96 | static void |
101 | ecb_cold static void |
97 | xbase (char *s, long n, int base) |
102 | xbase (char *s, long n, int base) |
98 | { |
103 | { |
99 | if (n < 0) |
104 | if (n < 0) |
100 | { |
105 | { |
101 | *s++ = '-'; |
106 | *s++ = '-'; |
… | |
… | |
103 | } |
108 | } |
104 | |
109 | |
105 | char *p = s; |
110 | char *p = s; |
106 | |
111 | |
107 | do { |
112 | do { |
108 | *p++ = '0' + n % base; |
113 | *p++ = "0123456789abcdef"[n % base]; |
109 | n /= base; |
114 | n /= base; |
110 | } while (n); |
115 | } while (n); |
111 | |
116 | |
112 | *p-- = 0; |
117 | *p-- = 0; |
113 | |
118 | |
… | |
… | |
116 | char x = *s; *s = *p; *p = x; |
121 | char x = *s; *s = *p; *p = x; |
117 | --p; ++s; |
122 | --p; ++s; |
118 | } |
123 | } |
119 | } |
124 | } |
120 | |
125 | |
121 | static void |
126 | ecb_cold static void |
122 | xnum (char *s, long n) |
127 | xnum (char *s, long n) |
123 | { |
128 | { |
124 | xbase (s, n, 10); |
129 | xbase (s, n, 10); |
125 | } |
130 | } |
126 | |
131 | |
127 | static void |
132 | ecb_cold static void |
128 | xwrstr (const char *s) |
133 | putnum (SCHEME_P_ long n) |
129 | { |
|
|
130 | write (1, s, strlen (s)); |
|
|
131 | } |
|
|
132 | |
|
|
133 | static void |
|
|
134 | xwrnum (long n) |
|
|
135 | { |
134 | { |
136 | char buf[64]; |
135 | char buf[64]; |
137 | |
136 | |
138 | xnum (buf, n); |
137 | xnum (buf, n); |
139 | xwrstr (buf); |
138 | putstr (SCHEME_A_ buf); |
140 | } |
139 | } |
|
|
140 | |
|
|
141 | #if USE_CHAR_CLASSIFIERS |
|
|
142 | #include <ctype.h> |
|
|
143 | #else |
141 | |
144 | |
142 | static char |
145 | static char |
143 | xtoupper (char c) |
146 | xtoupper (char c) |
144 | { |
147 | { |
145 | if (c >= 'a' && c <= 'z') |
148 | if (c >= 'a' && c <= 'z') |
… | |
… | |
165 | |
168 | |
166 | #define toupper(c) xtoupper (c) |
169 | #define toupper(c) xtoupper (c) |
167 | #define tolower(c) xtolower (c) |
170 | #define tolower(c) xtolower (c) |
168 | #define isdigit(c) xisdigit (c) |
171 | #define isdigit(c) xisdigit (c) |
169 | |
172 | |
|
|
173 | #endif |
|
|
174 | |
170 | #if USE_IGNORECASE |
175 | #if USE_IGNORECASE |
171 | static const char * |
176 | ecb_cold static const char * |
172 | xstrlwr (char *s) |
177 | xstrlwr (char *s) |
173 | { |
178 | { |
174 | const char *p = s; |
179 | const char *p = s; |
175 | |
180 | |
176 | while (*s) |
181 | while (*s) |
… | |
… | |
189 | # define stricmp(a,b) strcmp (a, b) |
194 | # define stricmp(a,b) strcmp (a, b) |
190 | # define strlwr(s) (s) |
195 | # define strlwr(s) (s) |
191 | #endif |
196 | #endif |
192 | |
197 | |
193 | #ifndef prompt |
198 | #ifndef prompt |
194 | # define prompt "ts> " |
199 | # define prompt "ms> " |
195 | #endif |
200 | #endif |
196 | |
201 | |
197 | #ifndef InitFile |
202 | #ifndef InitFile |
198 | # define InitFile "init.scm" |
203 | # define InitFile "init.scm" |
199 | #endif |
204 | #endif |
200 | |
205 | |
201 | enum scheme_types |
206 | enum scheme_types |
202 | { |
207 | { |
203 | T_INTEGER, |
208 | T_INTEGER, |
|
|
209 | T_CHARACTER, |
204 | T_REAL, |
210 | T_REAL, |
205 | T_STRING, |
211 | T_STRING, |
206 | T_SYMBOL, |
212 | T_SYMBOL, |
207 | T_PROC, |
213 | T_PROC, |
208 | T_PAIR, /* also used for free cells */ |
214 | T_PAIR, /* also used for free cells */ |
209 | T_CLOSURE, |
215 | T_CLOSURE, |
|
|
216 | T_BYTECODE, // temp |
|
|
217 | T_MACRO, |
210 | T_CONTINUATION, |
218 | T_CONTINUATION, |
211 | T_FOREIGN, |
219 | T_FOREIGN, |
212 | T_CHARACTER, |
|
|
213 | T_PORT, |
220 | T_PORT, |
214 | T_VECTOR, |
221 | T_VECTOR, |
215 | T_MACRO, |
|
|
216 | T_PROMISE, |
222 | T_PROMISE, |
217 | T_ENVIRONMENT, |
223 | T_ENVIRONMENT, |
218 | /* one more... */ |
224 | T_SPECIAL, // #t, #f, '(), eof-object |
|
|
225 | |
219 | T_NUM_SYSTEM_TYPES |
226 | T_NUM_SYSTEM_TYPES |
220 | }; |
227 | }; |
221 | |
228 | |
222 | #define T_MASKTYPE 0x000f |
229 | #define T_MASKTYPE 0x001f |
223 | #define T_SYNTAX 0x0010 |
230 | #define T_SYNTAX 0x0020 |
224 | #define T_IMMUTABLE 0x0020 |
231 | #define T_IMMUTABLE 0x0040 |
225 | #define T_ATOM 0x0040 /* only for gc */ |
232 | #define T_ATOM 0x0080 /* only for gc */ |
226 | #define T_MARK 0x0080 /* only for gc */ |
233 | //#define T_MARK 0x0080 /* only for gc */ |
227 | |
234 | |
228 | /* num, for generic arithmetic */ |
235 | /* num, for generic arithmetic */ |
229 | struct num |
236 | struct num |
230 | { |
237 | { |
231 | IVALUE ivalue; |
238 | IVALUE ivalue; |
… | |
… | |
256 | static num num_op (enum num_op op, num a, num b); |
263 | static num num_op (enum num_op op, num a, num b); |
257 | static num num_intdiv (num a, num b); |
264 | static num num_intdiv (num a, num b); |
258 | static num num_rem (num a, num b); |
265 | static num num_rem (num a, num b); |
259 | static num num_mod (num a, num b); |
266 | static num num_mod (num a, num b); |
260 | |
267 | |
261 | #if USE_MATH |
|
|
262 | static double round_per_R5RS (double x); |
|
|
263 | #endif |
|
|
264 | static int is_zero_rvalue (RVALUE x); |
268 | static int is_zero_rvalue (RVALUE x); |
265 | |
269 | |
266 | static num num_zero; |
270 | static num num_zero; |
267 | static num num_one; |
271 | static num num_one; |
268 | |
272 | |
… | |
… | |
380 | |
384 | |
381 | static pointer cadar (pointer p) { return car (cdr (car (p))); } |
385 | static pointer cadar (pointer p) { return car (cdr (car (p))); } |
382 | static pointer caddr (pointer p) { return car (cdr (cdr (p))); } |
386 | static pointer caddr (pointer p) { return car (cdr (cdr (p))); } |
383 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
387 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
384 | |
388 | |
|
|
389 | static pointer cadddr (pointer p) { return car (car (car (cdr (p)))); } |
|
|
390 | |
385 | INTERFACE void |
391 | INTERFACE void |
386 | set_car (pointer p, pointer q) |
392 | set_car (pointer p, pointer q) |
387 | { |
393 | { |
388 | CELL(p)->object.cons.car = CELL (q); |
394 | CELL(p)->object.cons.car = CELL (q); |
389 | } |
395 | } |
… | |
… | |
505 | |
511 | |
506 | #define is_atom(p) (typeflag (p) & T_ATOM) |
512 | #define is_atom(p) (typeflag (p) & T_ATOM) |
507 | #define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) |
513 | #define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) |
508 | #define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM) |
514 | #define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM) |
509 | |
515 | |
|
|
516 | #if 1 |
|
|
517 | #define is_mark(p) (CELL(p)->mark) |
|
|
518 | #define setmark(p) (CELL(p)->mark = 1) |
|
|
519 | #define clrmark(p) (CELL(p)->mark = 0) |
|
|
520 | #else |
510 | #define is_mark(p) (typeflag (p) & T_MARK) |
521 | #define is_mark(p) (typeflag (p) & T_MARK) |
511 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
522 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
512 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
523 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
|
|
524 | #endif |
513 | |
525 | |
514 | INTERFACE int |
526 | INTERFACE int |
515 | is_immutable (pointer p) |
527 | is_immutable (pointer p) |
516 | { |
528 | { |
517 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
529 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
… | |
… | |
529 | proper list: length |
541 | proper list: length |
530 | circular list: -1 |
542 | circular list: -1 |
531 | not even a pair: -2 |
543 | not even a pair: -2 |
532 | dotted list: -2 minus length before dot |
544 | dotted list: -2 minus length before dot |
533 | */ |
545 | */ |
534 | INTERFACE int |
546 | ecb_hot INTERFACE int |
535 | list_length (SCHEME_P_ pointer a) |
547 | list_length (SCHEME_P_ pointer a) |
536 | { |
548 | { |
537 | int i = 0; |
549 | int i = 0; |
538 | pointer slow, fast; |
550 | pointer slow, fast; |
539 | |
551 | |
… | |
… | |
578 | { |
590 | { |
579 | return list_length (SCHEME_A_ a) >= 0; |
591 | return list_length (SCHEME_A_ a) >= 0; |
580 | } |
592 | } |
581 | |
593 | |
582 | #if USE_CHAR_CLASSIFIERS |
594 | #if USE_CHAR_CLASSIFIERS |
|
|
595 | |
583 | ecb_inline int |
596 | ecb_inline int |
584 | Cisalpha (int c) |
597 | Cisalpha (int c) |
585 | { |
598 | { |
586 | return isascii (c) && isalpha (c); |
599 | return isascii (c) && isalpha (c); |
587 | } |
600 | } |
… | |
… | |
645 | "gs", |
658 | "gs", |
646 | "rs", |
659 | "rs", |
647 | "us" |
660 | "us" |
648 | }; |
661 | }; |
649 | |
662 | |
650 | static int |
663 | ecb_cold static int |
651 | is_ascii_name (const char *name, int *pc) |
664 | is_ascii_name (const char *name, int *pc) |
652 | { |
665 | { |
653 | int i; |
666 | int i; |
654 | |
667 | |
655 | for (i = 0; i < 32; i++) |
668 | for (i = 0; i < 32; i++) |
… | |
… | |
677 | static int file_interactive (SCHEME_P); |
690 | static int file_interactive (SCHEME_P); |
678 | ecb_inline int is_one_of (const char *s, int c); |
691 | ecb_inline int is_one_of (const char *s, int c); |
679 | static int alloc_cellseg (SCHEME_P); |
692 | static int alloc_cellseg (SCHEME_P); |
680 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
693 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
681 | static void finalize_cell (SCHEME_P_ pointer a); |
694 | static void finalize_cell (SCHEME_P_ pointer a); |
682 | static int count_consecutive_cells (pointer x, int needed); |
|
|
683 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
695 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
684 | static pointer mk_number (SCHEME_P_ const num n); |
696 | static pointer mk_number (SCHEME_P_ const num n); |
685 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
697 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
686 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
698 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
687 | static pointer mk_atom (SCHEME_P_ char *q); |
699 | static pointer mk_atom (SCHEME_P_ char *q); |
688 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
700 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
689 | |
701 | |
|
|
702 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
703 | |
690 | #if USE_PORTS |
704 | #if USE_PORTS |
691 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
692 | static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); |
705 | static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); |
693 | static pointer port_from_file (SCHEME_P_ int, int prop); |
706 | static pointer port_from_file (SCHEME_P_ int, int prop); |
694 | static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
707 | static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
695 | static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); |
708 | static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); |
696 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
709 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
697 | static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
710 | static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); |
698 | static void port_close (SCHEME_P_ pointer p, int flag); |
711 | static void port_close (SCHEME_P_ pointer p, int flag); |
699 | #endif |
712 | #endif |
|
|
713 | |
700 | static void mark (pointer a); |
714 | static void mark (pointer a); |
701 | static void gc (SCHEME_P_ pointer a, pointer b); |
715 | static void gc (SCHEME_P_ pointer a, pointer b); |
702 | static int basic_inchar (port *pt); |
716 | static int basic_inchar (port *pt); |
703 | static int inchar (SCHEME_P); |
717 | static int inchar (SCHEME_P); |
704 | static void backchar (SCHEME_P_ int c); |
718 | static void backchar (SCHEME_P_ int c); |
705 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
719 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
706 | static pointer readstrexp (SCHEME_P_ char delim); |
720 | static pointer readstrexp (SCHEME_P_ char delim); |
707 | ecb_inline int skipspace (SCHEME_P); |
721 | static int skipspace (SCHEME_P); |
708 | static int token (SCHEME_P); |
722 | static int token (SCHEME_P); |
709 | static void printslashstring (SCHEME_P_ char *s, int len); |
723 | static void printslashstring (SCHEME_P_ char *s, int len); |
710 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
724 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
711 | static void printatom (SCHEME_P_ pointer l, int f); |
725 | static void printatom (SCHEME_P_ pointer l, int f); |
712 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
726 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
… | |
… | |
878 | } |
892 | } |
879 | |
893 | |
880 | return ret; |
894 | return ret; |
881 | } |
895 | } |
882 | |
896 | |
883 | #if USE_MATH |
|
|
884 | |
|
|
885 | /* Round to nearest. Round to even if midway */ |
|
|
886 | static double |
|
|
887 | round_per_R5RS (double x) |
|
|
888 | { |
|
|
889 | double fl = floor (x); |
|
|
890 | double ce = ceil (x); |
|
|
891 | double dfl = x - fl; |
|
|
892 | double dce = ce - x; |
|
|
893 | |
|
|
894 | if (dfl > dce) |
|
|
895 | return ce; |
|
|
896 | else if (dfl < dce) |
|
|
897 | return fl; |
|
|
898 | else |
|
|
899 | { |
|
|
900 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
|
|
901 | return fl; |
|
|
902 | else |
|
|
903 | return ce; |
|
|
904 | } |
|
|
905 | } |
|
|
906 | #endif |
|
|
907 | |
|
|
908 | static int |
897 | static int |
909 | is_zero_rvalue (RVALUE x) |
898 | is_zero_rvalue (RVALUE x) |
910 | { |
899 | { |
911 | return x == 0; |
900 | return x == 0; |
912 | #if 0 |
901 | #if 0 |
… | |
… | |
917 | #endif |
906 | #endif |
918 | #endif |
907 | #endif |
919 | } |
908 | } |
920 | |
909 | |
921 | /* allocate new cell segment */ |
910 | /* allocate new cell segment */ |
922 | static int |
911 | ecb_cold static int |
923 | alloc_cellseg (SCHEME_P) |
912 | alloc_cellseg (SCHEME_P) |
924 | { |
913 | { |
925 | struct cell *newp; |
914 | struct cell *newp; |
926 | struct cell *last; |
915 | struct cell *last; |
927 | struct cell *p; |
916 | struct cell *p; |
… | |
… | |
936 | |
925 | |
937 | if (!cp && USE_ERROR_CHECKING) |
926 | if (!cp && USE_ERROR_CHECKING) |
938 | return k; |
927 | return k; |
939 | |
928 | |
940 | i = ++SCHEME_V->last_cell_seg; |
929 | i = ++SCHEME_V->last_cell_seg; |
941 | SCHEME_V->alloc_seg[i] = cp; |
|
|
942 | |
930 | |
943 | newp = (struct cell *)cp; |
931 | newp = (struct cell *)cp; |
944 | SCHEME_V->cell_seg[i] = newp; |
932 | SCHEME_V->cell_seg[i] = newp; |
945 | SCHEME_V->cell_segsize[i] = segsize; |
933 | SCHEME_V->cell_segsize[i] = segsize; |
946 | SCHEME_V->fcells += segsize; |
934 | SCHEME_V->fcells += segsize; |
947 | last = newp + segsize - 1; |
935 | last = newp + segsize - 1; |
948 | |
936 | |
949 | for (p = newp; p <= last; p++) |
937 | for (p = newp; p <= last; p++) |
950 | { |
938 | { |
951 | pointer cp = POINTER (p); |
939 | pointer cp = POINTER (p); |
|
|
940 | clrmark (cp); |
952 | set_typeflag (cp, T_PAIR); |
941 | set_typeflag (cp, T_PAIR); |
953 | set_car (cp, NIL); |
942 | set_car (cp, NIL); |
954 | set_cdr (cp, POINTER (p + 1)); |
943 | set_cdr (cp, POINTER (p + 1)); |
955 | } |
944 | } |
956 | |
945 | |
… | |
… | |
969 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
958 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
970 | return S_SINK; |
959 | return S_SINK; |
971 | |
960 | |
972 | if (SCHEME_V->free_cell == NIL) |
961 | if (SCHEME_V->free_cell == NIL) |
973 | { |
962 | { |
974 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1; |
963 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 2; |
975 | |
964 | |
976 | gc (SCHEME_A_ a, b); |
965 | gc (SCHEME_A_ a, b); |
977 | |
966 | |
978 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
967 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
979 | { |
968 | { |
… | |
… | |
998 | } |
987 | } |
999 | } |
988 | } |
1000 | |
989 | |
1001 | /* To retain recent allocs before interpreter knows about them - |
990 | /* To retain recent allocs before interpreter knows about them - |
1002 | Tehom */ |
991 | Tehom */ |
1003 | static void |
992 | ecb_hot static void |
1004 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
993 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
1005 | { |
994 | { |
1006 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
995 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1007 | |
996 | |
1008 | set_typeflag (holder, T_PAIR); |
997 | set_typeflag (holder, T_PAIR); |
… | |
… | |
1010 | set_car (holder, recent); |
999 | set_car (holder, recent); |
1011 | set_cdr (holder, car (S_SINK)); |
1000 | set_cdr (holder, car (S_SINK)); |
1012 | set_car (S_SINK, holder); |
1001 | set_car (S_SINK, holder); |
1013 | } |
1002 | } |
1014 | |
1003 | |
1015 | static pointer |
1004 | ecb_hot static pointer |
1016 | get_cell (SCHEME_P_ pointer a, pointer b) |
1005 | get_cell (SCHEME_P_ pointer a, pointer b) |
1017 | { |
1006 | { |
1018 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
1007 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
1019 | |
1008 | |
1020 | /* For right now, include "a" and "b" in "cell" so that gc doesn't |
1009 | /* For right now, include "a" and "b" in "cell" so that gc doesn't |
… | |
… | |
1058 | static void |
1047 | static void |
1059 | check_cell_alloced (pointer p, int expect_alloced) |
1048 | check_cell_alloced (pointer p, int expect_alloced) |
1060 | { |
1049 | { |
1061 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1050 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1062 | if (typeflag (p) & !expect_alloced) |
1051 | if (typeflag (p) & !expect_alloced) |
1063 | xwrstr ("Cell is already allocated!\n"); |
1052 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1064 | |
1053 | |
1065 | if (!(typeflag (p)) & expect_alloced) |
1054 | if (!(typeflag (p)) & expect_alloced) |
1066 | xwrstr ("Cell is not allocated!\n"); |
1055 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1067 | } |
1056 | } |
1068 | |
1057 | |
1069 | static void |
1058 | static void |
1070 | check_range_alloced (pointer p, int n, int expect_alloced) |
1059 | check_range_alloced (pointer p, int n, int expect_alloced) |
1071 | { |
1060 | { |
… | |
… | |
1077 | #endif |
1066 | #endif |
1078 | |
1067 | |
1079 | /* Medium level cell allocation */ |
1068 | /* Medium level cell allocation */ |
1080 | |
1069 | |
1081 | /* get new cons cell */ |
1070 | /* get new cons cell */ |
1082 | pointer |
1071 | ecb_hot static pointer |
1083 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1072 | xcons (SCHEME_P_ pointer a, pointer b) |
1084 | { |
1073 | { |
1085 | pointer x = get_cell (SCHEME_A_ a, b); |
1074 | pointer x = get_cell (SCHEME_A_ a, b); |
1086 | |
1075 | |
1087 | set_typeflag (x, T_PAIR); |
1076 | set_typeflag (x, T_PAIR); |
1088 | |
|
|
1089 | if (immutable) |
|
|
1090 | setimmutable (x); |
|
|
1091 | |
1077 | |
1092 | set_car (x, a); |
1078 | set_car (x, a); |
1093 | set_cdr (x, b); |
1079 | set_cdr (x, b); |
1094 | |
1080 | |
1095 | return x; |
1081 | return x; |
1096 | } |
1082 | } |
1097 | |
1083 | |
1098 | static pointer |
1084 | ecb_hot static pointer |
|
|
1085 | ximmutable_cons (SCHEME_P_ pointer a, pointer b) |
|
|
1086 | { |
|
|
1087 | pointer x = xcons (SCHEME_A_ a, b); |
|
|
1088 | setimmutable (x); |
|
|
1089 | return x; |
|
|
1090 | } |
|
|
1091 | |
|
|
1092 | #define cons(a,b) xcons (SCHEME_A_ a, b) |
|
|
1093 | #define immutable_cons(a,b) ximmutable_cons (SCHEME_A_ a, b) |
|
|
1094 | |
|
|
1095 | ecb_cold static pointer |
1099 | generate_symbol (SCHEME_P_ const char *name) |
1096 | generate_symbol (SCHEME_P_ const char *name) |
1100 | { |
1097 | { |
1101 | pointer x = mk_string (SCHEME_A_ name); |
1098 | pointer x = mk_string (SCHEME_A_ name); |
1102 | setimmutable (x); |
1099 | setimmutable (x); |
1103 | set_typeflag (x, T_SYMBOL | T_ATOM); |
1100 | set_typeflag (x, T_SYMBOL | T_ATOM); |
… | |
… | |
1109 | #ifndef USE_OBJECT_LIST |
1106 | #ifndef USE_OBJECT_LIST |
1110 | |
1107 | |
1111 | static int |
1108 | static int |
1112 | hash_fn (const char *key, int table_size) |
1109 | hash_fn (const char *key, int table_size) |
1113 | { |
1110 | { |
1114 | const unsigned char *p = key; |
1111 | const unsigned char *p = (unsigned char *)key; |
1115 | uint32_t hash = 2166136261; |
1112 | uint32_t hash = 2166136261U; |
1116 | |
1113 | |
1117 | while (*p) |
1114 | while (*p) |
1118 | hash = (hash ^ *p++) * 16777619; |
1115 | hash = (hash ^ *p++) * 16777619; |
1119 | |
1116 | |
1120 | return hash % table_size; |
1117 | return hash % table_size; |
1121 | } |
1118 | } |
1122 | |
1119 | |
1123 | static pointer |
1120 | ecb_cold static pointer |
1124 | oblist_initial_value (SCHEME_P) |
1121 | oblist_initial_value (SCHEME_P) |
1125 | { |
1122 | { |
1126 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1123 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1127 | } |
1124 | } |
1128 | |
1125 | |
1129 | /* returns the new symbol */ |
1126 | /* returns the new symbol */ |
1130 | static pointer |
1127 | ecb_cold static pointer |
1131 | oblist_add_by_name (SCHEME_P_ const char *name) |
1128 | oblist_add_by_name (SCHEME_P_ const char *name) |
1132 | { |
1129 | { |
1133 | pointer x = generate_symbol (SCHEME_A_ name); |
1130 | pointer x = generate_symbol (SCHEME_A_ name); |
1134 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1131 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1135 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1132 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1136 | return x; |
1133 | return x; |
1137 | } |
1134 | } |
1138 | |
1135 | |
1139 | ecb_inline pointer |
1136 | ecb_cold static pointer |
1140 | oblist_find_by_name (SCHEME_P_ const char *name) |
1137 | oblist_find_by_name (SCHEME_P_ const char *name) |
1141 | { |
1138 | { |
1142 | int location; |
1139 | int location; |
1143 | pointer x; |
1140 | pointer x; |
1144 | char *s; |
1141 | char *s; |
… | |
… | |
1155 | } |
1152 | } |
1156 | |
1153 | |
1157 | return NIL; |
1154 | return NIL; |
1158 | } |
1155 | } |
1159 | |
1156 | |
1160 | static pointer |
1157 | ecb_cold static pointer |
1161 | oblist_all_symbols (SCHEME_P) |
1158 | oblist_all_symbols (SCHEME_P) |
1162 | { |
1159 | { |
1163 | int i; |
1160 | int i; |
1164 | pointer x; |
1161 | pointer x; |
1165 | pointer ob_list = NIL; |
1162 | pointer ob_list = NIL; |
… | |
… | |
1171 | return ob_list; |
1168 | return ob_list; |
1172 | } |
1169 | } |
1173 | |
1170 | |
1174 | #else |
1171 | #else |
1175 | |
1172 | |
1176 | static pointer |
1173 | ecb_cold static pointer |
1177 | oblist_initial_value (SCHEME_P) |
1174 | oblist_initial_value (SCHEME_P) |
1178 | { |
1175 | { |
1179 | return NIL; |
1176 | return NIL; |
1180 | } |
1177 | } |
1181 | |
1178 | |
1182 | ecb_inline pointer |
1179 | ecb_cold static pointer |
1183 | oblist_find_by_name (SCHEME_P_ const char *name) |
1180 | oblist_find_by_name (SCHEME_P_ const char *name) |
1184 | { |
1181 | { |
1185 | pointer x; |
1182 | pointer x; |
1186 | char *s; |
1183 | char *s; |
1187 | |
1184 | |
… | |
… | |
1196 | |
1193 | |
1197 | return NIL; |
1194 | return NIL; |
1198 | } |
1195 | } |
1199 | |
1196 | |
1200 | /* returns the new symbol */ |
1197 | /* returns the new symbol */ |
1201 | static pointer |
1198 | ecb_cold static pointer |
1202 | oblist_add_by_name (SCHEME_P_ const char *name) |
1199 | oblist_add_by_name (SCHEME_P_ const char *name) |
1203 | { |
1200 | { |
1204 | pointer x = generate_symbol (SCHEME_A_ name); |
1201 | pointer x = generate_symbol (SCHEME_A_ name); |
1205 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1202 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1206 | return x; |
1203 | return x; |
1207 | } |
1204 | } |
1208 | |
1205 | |
1209 | static pointer |
1206 | ecb_cold static pointer |
1210 | oblist_all_symbols (SCHEME_P) |
1207 | oblist_all_symbols (SCHEME_P) |
1211 | { |
1208 | { |
1212 | return SCHEME_V->oblist; |
1209 | return SCHEME_V->oblist; |
1213 | } |
1210 | } |
1214 | |
1211 | |
1215 | #endif |
1212 | #endif |
1216 | |
1213 | |
1217 | #if USE_PORTS |
|
|
1218 | static pointer |
1214 | ecb_cold static pointer |
1219 | mk_port (SCHEME_P_ port *p) |
1215 | mk_port (SCHEME_P_ port *p) |
1220 | { |
1216 | { |
1221 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1217 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1222 | |
1218 | |
1223 | set_typeflag (x, T_PORT | T_ATOM); |
1219 | set_typeflag (x, T_PORT | T_ATOM); |
1224 | set_port (x, p); |
1220 | set_port (x, p); |
1225 | |
1221 | |
1226 | return x; |
1222 | return x; |
1227 | } |
1223 | } |
1228 | #endif |
|
|
1229 | |
1224 | |
1230 | pointer |
1225 | ecb_cold pointer |
1231 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1226 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1232 | { |
1227 | { |
1233 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1228 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1234 | |
1229 | |
1235 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1230 | set_typeflag (x, T_FOREIGN | T_ATOM); |
… | |
… | |
1400 | x = oblist_add_by_name (SCHEME_A_ name); |
1395 | x = oblist_add_by_name (SCHEME_A_ name); |
1401 | |
1396 | |
1402 | return x; |
1397 | return x; |
1403 | } |
1398 | } |
1404 | |
1399 | |
1405 | INTERFACE pointer |
1400 | ecb_cold INTERFACE pointer |
1406 | gensym (SCHEME_P) |
1401 | gensym (SCHEME_P) |
1407 | { |
1402 | { |
1408 | pointer x; |
1403 | pointer x; |
1409 | char name[40] = "gensym-"; |
1404 | char name[40] = "gensym-"; |
1410 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1405 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
… | |
… | |
1417 | { |
1412 | { |
1418 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1413 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1419 | } |
1414 | } |
1420 | |
1415 | |
1421 | /* make symbol or number atom from string */ |
1416 | /* make symbol or number atom from string */ |
1422 | static pointer |
1417 | ecb_cold static pointer |
1423 | mk_atom (SCHEME_P_ char *q) |
1418 | mk_atom (SCHEME_P_ char *q) |
1424 | { |
1419 | { |
1425 | char c, *p; |
1420 | char c, *p; |
1426 | int has_dec_point = 0; |
1421 | int has_dec_point = 0; |
1427 | int has_fp_exp = 0; |
1422 | int has_fp_exp = 0; |
… | |
… | |
1498 | |
1493 | |
1499 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1494 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1500 | } |
1495 | } |
1501 | |
1496 | |
1502 | /* make constant */ |
1497 | /* make constant */ |
1503 | static pointer |
1498 | ecb_cold static pointer |
1504 | mk_sharp_const (SCHEME_P_ char *name) |
1499 | mk_sharp_const (SCHEME_P_ char *name) |
1505 | { |
1500 | { |
1506 | if (!strcmp (name, "t")) |
1501 | if (!strcmp (name, "t")) |
1507 | return S_T; |
1502 | return S_T; |
1508 | else if (!strcmp (name, "f")) |
1503 | else if (!strcmp (name, "f")) |
1509 | return S_F; |
1504 | return S_F; |
1510 | else if (*name == '\\') /* #\w (character) */ |
1505 | else if (*name == '\\') /* #\w (character) */ |
1511 | { |
1506 | { |
1512 | int c; |
1507 | int c; |
1513 | |
1508 | |
|
|
1509 | // TODO: optimise |
1514 | if (stricmp (name + 1, "space") == 0) |
1510 | if (stricmp (name + 1, "space") == 0) |
1515 | c = ' '; |
1511 | c = ' '; |
1516 | else if (stricmp (name + 1, "newline") == 0) |
1512 | else if (stricmp (name + 1, "newline") == 0) |
1517 | c = '\n'; |
1513 | c = '\n'; |
1518 | else if (stricmp (name + 1, "return") == 0) |
1514 | else if (stricmp (name + 1, "return") == 0) |
1519 | c = '\r'; |
1515 | c = '\r'; |
1520 | else if (stricmp (name + 1, "tab") == 0) |
1516 | else if (stricmp (name + 1, "tab") == 0) |
1521 | c = '\t'; |
1517 | c = '\t'; |
|
|
1518 | else if (stricmp (name + 1, "alarm") == 0) |
|
|
1519 | c = 0x07; |
|
|
1520 | else if (stricmp (name + 1, "backspace") == 0) |
|
|
1521 | c = 0x08; |
|
|
1522 | else if (stricmp (name + 1, "escape") == 0) |
|
|
1523 | c = 0x1b; |
|
|
1524 | else if (stricmp (name + 1, "delete") == 0) |
|
|
1525 | c = 0x7f; |
|
|
1526 | else if (stricmp (name + 1, "null") == 0) |
|
|
1527 | c = 0; |
1522 | else if (name[1] == 'x' && name[2] != 0) |
1528 | else if (name[1] == 'x' && name[2] != 0) |
1523 | { |
1529 | { |
1524 | long c1 = strtol (name + 2, 0, 16); |
1530 | long c1 = strtol (name + 2, 0, 16); |
1525 | |
1531 | |
1526 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1532 | if (0 <= c1 && c1 <= UCHAR_MAX) |
… | |
… | |
1551 | return NIL; |
1557 | return NIL; |
1552 | } |
1558 | } |
1553 | } |
1559 | } |
1554 | |
1560 | |
1555 | /* ========== garbage collector ========== */ |
1561 | /* ========== garbage collector ========== */ |
|
|
1562 | |
|
|
1563 | static void |
|
|
1564 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1565 | { |
|
|
1566 | /* TODO, fast bitmap check? */ |
|
|
1567 | if (is_string (a) || is_symbol (a)) |
|
|
1568 | free (strvalue (a)); |
|
|
1569 | else if (is_vector (a)) |
|
|
1570 | free (vecvalue (a)); |
|
|
1571 | #if USE_PORTS |
|
|
1572 | else if (is_port (a)) |
|
|
1573 | { |
|
|
1574 | if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) |
|
|
1575 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1576 | |
|
|
1577 | free (port (a)); |
|
|
1578 | } |
|
|
1579 | #endif |
|
|
1580 | } |
1556 | |
1581 | |
1557 | /*-- |
1582 | /*-- |
1558 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1583 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1559 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1584 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1560 | * for marking. |
1585 | * for marking. |
1561 | * |
1586 | * |
1562 | * The exception is vectors - vectors are currently marked recursively, |
1587 | * The exception is vectors - vectors are currently marked recursively, |
1563 | * which is inherited form tinyscheme and could be fixed by having another |
1588 | * which is inherited form tinyscheme and could be fixed by having another |
1564 | * word of context in the vector |
1589 | * word of context in the vector |
1565 | */ |
1590 | */ |
1566 | static void |
1591 | ecb_hot static void |
1567 | mark (pointer a) |
1592 | mark (pointer a) |
1568 | { |
1593 | { |
1569 | pointer t, q, p; |
1594 | pointer t, q, p; |
1570 | |
1595 | |
1571 | t = 0; |
1596 | t = 0; |
… | |
… | |
1628 | p = q; |
1653 | p = q; |
1629 | goto E6; |
1654 | goto E6; |
1630 | } |
1655 | } |
1631 | } |
1656 | } |
1632 | |
1657 | |
1633 | /* garbage collection. parameter a, b is marked. */ |
1658 | ecb_hot static void |
1634 | static void |
1659 | gc_free (SCHEME_P) |
1635 | gc (SCHEME_P_ pointer a, pointer b) |
|
|
1636 | { |
1660 | { |
1637 | int i; |
1661 | int i; |
1638 | |
|
|
1639 | if (SCHEME_V->gc_verbose) |
|
|
1640 | putstr (SCHEME_A_ "gc..."); |
|
|
1641 | |
|
|
1642 | /* mark system globals */ |
|
|
1643 | mark (SCHEME_V->oblist); |
|
|
1644 | mark (SCHEME_V->global_env); |
|
|
1645 | |
|
|
1646 | /* mark current registers */ |
|
|
1647 | mark (SCHEME_V->args); |
|
|
1648 | mark (SCHEME_V->envir); |
|
|
1649 | mark (SCHEME_V->code); |
|
|
1650 | dump_stack_mark (SCHEME_A); |
|
|
1651 | mark (SCHEME_V->value); |
|
|
1652 | mark (SCHEME_V->inport); |
|
|
1653 | mark (SCHEME_V->save_inport); |
|
|
1654 | mark (SCHEME_V->outport); |
|
|
1655 | mark (SCHEME_V->loadport); |
|
|
1656 | |
|
|
1657 | /* Mark recent objects the interpreter doesn't know about yet. */ |
|
|
1658 | mark (car (S_SINK)); |
|
|
1659 | /* Mark any older stuff above nested C calls */ |
|
|
1660 | mark (SCHEME_V->c_nest); |
|
|
1661 | |
|
|
1662 | #if USE_INTCACHE |
|
|
1663 | /* mark intcache */ |
|
|
1664 | for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i) |
|
|
1665 | if (SCHEME_V->intcache[i - INTCACHE_MIN]) |
|
|
1666 | mark (SCHEME_V->intcache[i - INTCACHE_MIN]); |
|
|
1667 | #endif |
|
|
1668 | |
|
|
1669 | /* mark variables a, b */ |
|
|
1670 | mark (a); |
|
|
1671 | mark (b); |
|
|
1672 | |
|
|
1673 | /* garbage collect */ |
|
|
1674 | clrmark (NIL); |
|
|
1675 | SCHEME_V->fcells = 0; |
|
|
1676 | SCHEME_V->free_cell = NIL; |
|
|
1677 | |
|
|
1678 | if (SCHEME_V->gc_verbose) |
|
|
1679 | xwrstr ("freeing..."); |
|
|
1680 | |
|
|
1681 | uint32_t total = 0; |
1662 | uint32_t total = 0; |
1682 | |
1663 | |
1683 | /* Here we scan the cells to build the free-list. */ |
1664 | /* Here we scan the cells to build the free-list. */ |
1684 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1665 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1685 | { |
1666 | { |
… | |
… | |
1710 | } |
1691 | } |
1711 | } |
1692 | } |
1712 | |
1693 | |
1713 | if (SCHEME_V->gc_verbose) |
1694 | if (SCHEME_V->gc_verbose) |
1714 | { |
1695 | { |
1715 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" out of "); xwrnum (total); xwrstr (" cells were recovered.\n"); |
1696 | 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"); |
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 | { |
1697 | } |
1730 | if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) |
1698 | } |
1731 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1732 | |
1699 | |
1733 | free (port (a)); |
1700 | /* garbage collection. parameter a, b is marked. */ |
1734 | } |
1701 | ecb_cold static void |
|
|
1702 | gc (SCHEME_P_ pointer a, pointer b) |
|
|
1703 | { |
|
|
1704 | int i; |
|
|
1705 | |
|
|
1706 | if (SCHEME_V->gc_verbose) |
|
|
1707 | putstr (SCHEME_A_ "gc..."); |
|
|
1708 | |
|
|
1709 | /* mark system globals */ |
|
|
1710 | mark (SCHEME_V->oblist); |
|
|
1711 | mark (SCHEME_V->global_env); |
|
|
1712 | |
|
|
1713 | /* mark current registers */ |
|
|
1714 | mark (SCHEME_V->args); |
|
|
1715 | mark (SCHEME_V->envir); |
|
|
1716 | mark (SCHEME_V->code); |
|
|
1717 | dump_stack_mark (SCHEME_A); |
|
|
1718 | mark (SCHEME_V->value); |
|
|
1719 | mark (SCHEME_V->inport); |
|
|
1720 | mark (SCHEME_V->save_inport); |
|
|
1721 | mark (SCHEME_V->outport); |
|
|
1722 | mark (SCHEME_V->loadport); |
|
|
1723 | |
|
|
1724 | /* Mark recent objects the interpreter doesn't know about yet. */ |
|
|
1725 | mark (car (S_SINK)); |
|
|
1726 | /* Mark any older stuff above nested C calls */ |
|
|
1727 | mark (SCHEME_V->c_nest); |
|
|
1728 | |
|
|
1729 | #if USE_INTCACHE |
|
|
1730 | /* mark intcache */ |
|
|
1731 | for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i) |
|
|
1732 | if (SCHEME_V->intcache[i - INTCACHE_MIN]) |
|
|
1733 | mark (SCHEME_V->intcache[i - INTCACHE_MIN]); |
1735 | #endif |
1734 | #endif |
|
|
1735 | |
|
|
1736 | /* mark variables a, b */ |
|
|
1737 | mark (a); |
|
|
1738 | mark (b); |
|
|
1739 | |
|
|
1740 | /* garbage collect */ |
|
|
1741 | clrmark (NIL); |
|
|
1742 | SCHEME_V->fcells = 0; |
|
|
1743 | SCHEME_V->free_cell = NIL; |
|
|
1744 | |
|
|
1745 | if (SCHEME_V->gc_verbose) |
|
|
1746 | putstr (SCHEME_A_ "freeing..."); |
|
|
1747 | |
|
|
1748 | gc_free (SCHEME_A); |
1736 | } |
1749 | } |
1737 | |
1750 | |
1738 | /* ========== Routines for Reading ========== */ |
1751 | /* ========== Routines for Reading ========== */ |
1739 | |
1752 | |
1740 | static int |
1753 | ecb_cold static int |
1741 | file_push (SCHEME_P_ const char *fname) |
1754 | file_push (SCHEME_P_ const char *fname) |
1742 | { |
1755 | { |
1743 | #if USE_PORTS |
|
|
1744 | int fin; |
1756 | int fin; |
1745 | |
1757 | |
1746 | if (SCHEME_V->file_i == MAXFIL - 1) |
1758 | if (SCHEME_V->file_i == MAXFIL - 1) |
1747 | return 0; |
1759 | return 0; |
1748 | |
1760 | |
… | |
… | |
1765 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0); |
1777 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0); |
1766 | #endif |
1778 | #endif |
1767 | } |
1779 | } |
1768 | |
1780 | |
1769 | return fin >= 0; |
1781 | return fin >= 0; |
1770 | |
|
|
1771 | #else |
|
|
1772 | return 1; |
|
|
1773 | #endif |
|
|
1774 | } |
1782 | } |
1775 | |
1783 | |
1776 | static void |
1784 | ecb_cold static void |
1777 | file_pop (SCHEME_P) |
1785 | file_pop (SCHEME_P) |
1778 | { |
1786 | { |
1779 | if (SCHEME_V->file_i != 0) |
1787 | if (SCHEME_V->file_i != 0) |
1780 | { |
1788 | { |
1781 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1789 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
… | |
… | |
1785 | SCHEME_V->file_i--; |
1793 | SCHEME_V->file_i--; |
1786 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1794 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1787 | } |
1795 | } |
1788 | } |
1796 | } |
1789 | |
1797 | |
1790 | static int |
1798 | ecb_cold static int |
1791 | file_interactive (SCHEME_P) |
1799 | file_interactive (SCHEME_P) |
1792 | { |
1800 | { |
1793 | #if USE_PORTS |
1801 | #if USE_PORTS |
1794 | return SCHEME_V->file_i == 0 |
1802 | return SCHEME_V->file_i == 0 |
1795 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1803 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
… | |
… | |
1798 | return 0; |
1806 | return 0; |
1799 | #endif |
1807 | #endif |
1800 | } |
1808 | } |
1801 | |
1809 | |
1802 | #if USE_PORTS |
1810 | #if USE_PORTS |
1803 | static port * |
1811 | ecb_cold static port * |
1804 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1812 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1805 | { |
1813 | { |
1806 | int fd; |
1814 | int fd; |
1807 | int flags; |
1815 | int flags; |
1808 | char *rw; |
1816 | char *rw; |
… | |
… | |
1831 | # endif |
1839 | # endif |
1832 | |
1840 | |
1833 | return pt; |
1841 | return pt; |
1834 | } |
1842 | } |
1835 | |
1843 | |
1836 | static pointer |
1844 | ecb_cold static pointer |
1837 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1845 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1838 | { |
1846 | { |
1839 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1847 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1840 | |
1848 | |
1841 | if (!pt && USE_ERROR_CHECKING) |
1849 | if (!pt && USE_ERROR_CHECKING) |
1842 | return NIL; |
1850 | return NIL; |
1843 | |
1851 | |
1844 | return mk_port (SCHEME_A_ pt); |
1852 | return mk_port (SCHEME_A_ pt); |
1845 | } |
1853 | } |
1846 | |
1854 | |
1847 | static port * |
1855 | ecb_cold static port * |
1848 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1856 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1849 | { |
1857 | { |
1850 | port *pt = malloc (sizeof *pt); |
1858 | port *pt = malloc (sizeof *pt); |
1851 | |
1859 | |
1852 | if (!pt && USE_ERROR_CHECKING) |
1860 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1857 | pt->rep.stdio.file = f; |
1865 | pt->rep.stdio.file = f; |
1858 | pt->rep.stdio.closeit = 0; |
1866 | pt->rep.stdio.closeit = 0; |
1859 | return pt; |
1867 | return pt; |
1860 | } |
1868 | } |
1861 | |
1869 | |
1862 | static pointer |
1870 | ecb_cold static pointer |
1863 | port_from_file (SCHEME_P_ int f, int prop) |
1871 | port_from_file (SCHEME_P_ int f, int prop) |
1864 | { |
1872 | { |
1865 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1873 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1866 | |
1874 | |
1867 | if (!pt && USE_ERROR_CHECKING) |
1875 | if (!pt && USE_ERROR_CHECKING) |
1868 | return NIL; |
1876 | return NIL; |
1869 | |
1877 | |
1870 | return mk_port (SCHEME_A_ pt); |
1878 | return mk_port (SCHEME_A_ pt); |
1871 | } |
1879 | } |
1872 | |
1880 | |
1873 | static port * |
1881 | ecb_cold static port * |
1874 | port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1882 | port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1875 | { |
1883 | { |
1876 | port *pt = malloc (sizeof (port)); |
1884 | port *pt = malloc (sizeof (port)); |
1877 | |
1885 | |
1878 | if (!pt && USE_ERROR_CHECKING) |
1886 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1884 | pt->rep.string.curr = start; |
1892 | pt->rep.string.curr = start; |
1885 | pt->rep.string.past_the_end = past_the_end; |
1893 | pt->rep.string.past_the_end = past_the_end; |
1886 | return pt; |
1894 | return pt; |
1887 | } |
1895 | } |
1888 | |
1896 | |
1889 | static pointer |
1897 | ecb_cold static pointer |
1890 | port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1898 | port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1891 | { |
1899 | { |
1892 | port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); |
1900 | port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); |
1893 | |
1901 | |
1894 | if (!pt && USE_ERROR_CHECKING) |
1902 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1897 | return mk_port (SCHEME_A_ pt); |
1905 | return mk_port (SCHEME_A_ pt); |
1898 | } |
1906 | } |
1899 | |
1907 | |
1900 | # define BLOCK_SIZE 256 |
1908 | # define BLOCK_SIZE 256 |
1901 | |
1909 | |
1902 | static port * |
1910 | ecb_cold static port * |
1903 | port_rep_from_scratch (SCHEME_P) |
1911 | port_rep_from_scratch (SCHEME_P) |
1904 | { |
1912 | { |
1905 | char *start; |
1913 | char *start; |
1906 | port *pt = malloc (sizeof (port)); |
1914 | port *pt = malloc (sizeof (port)); |
1907 | |
1915 | |
… | |
… | |
1921 | pt->rep.string.curr = start; |
1929 | pt->rep.string.curr = start; |
1922 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1930 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1923 | return pt; |
1931 | return pt; |
1924 | } |
1932 | } |
1925 | |
1933 | |
1926 | static pointer |
1934 | ecb_cold static pointer |
1927 | port_from_scratch (SCHEME_P) |
1935 | port_from_scratch (SCHEME_P) |
1928 | { |
1936 | { |
1929 | port *pt = port_rep_from_scratch (SCHEME_A); |
1937 | port *pt = port_rep_from_scratch (SCHEME_A); |
1930 | |
1938 | |
1931 | if (!pt && USE_ERROR_CHECKING) |
1939 | if (!pt && USE_ERROR_CHECKING) |
1932 | return NIL; |
1940 | return NIL; |
1933 | |
1941 | |
1934 | return mk_port (SCHEME_A_ pt); |
1942 | return mk_port (SCHEME_A_ pt); |
1935 | } |
1943 | } |
1936 | |
1944 | |
1937 | static void |
1945 | ecb_cold static void |
1938 | port_close (SCHEME_P_ pointer p, int flag) |
1946 | port_close (SCHEME_P_ pointer p, int flag) |
1939 | { |
1947 | { |
1940 | port *pt = port (p); |
1948 | port *pt = port (p); |
1941 | |
1949 | |
1942 | pt->kind &= ~flag; |
1950 | pt->kind &= ~flag; |
… | |
… | |
1962 | } |
1970 | } |
1963 | } |
1971 | } |
1964 | #endif |
1972 | #endif |
1965 | |
1973 | |
1966 | /* get new character from input file */ |
1974 | /* get new character from input file */ |
1967 | static int |
1975 | ecb_cold static int |
1968 | inchar (SCHEME_P) |
1976 | inchar (SCHEME_P) |
1969 | { |
1977 | { |
1970 | int c; |
1978 | int c; |
1971 | port *pt = port (SCHEME_V->inport); |
1979 | port *pt = port (SCHEME_V->inport); |
1972 | |
1980 | |
… | |
… | |
1986 | } |
1994 | } |
1987 | |
1995 | |
1988 | return c; |
1996 | return c; |
1989 | } |
1997 | } |
1990 | |
1998 | |
1991 | static int ungot = -1; |
1999 | ecb_cold static int |
1992 | |
|
|
1993 | static int |
|
|
1994 | basic_inchar (port *pt) |
2000 | basic_inchar (port *pt) |
1995 | { |
2001 | { |
1996 | #if USE_PORTS |
|
|
1997 | if (pt->unget != -1) |
2002 | if (pt->unget != -1) |
1998 | { |
2003 | { |
1999 | int r = pt->unget; |
2004 | int r = pt->unget; |
2000 | pt->unget = -1; |
2005 | pt->unget = -1; |
2001 | return r; |
2006 | return r; |
2002 | } |
2007 | } |
2003 | |
2008 | |
|
|
2009 | #if USE_PORTS |
2004 | if (pt->kind & port_file) |
2010 | if (pt->kind & port_file) |
2005 | { |
2011 | { |
2006 | char c; |
2012 | char c; |
2007 | |
2013 | |
2008 | if (!read (pt->rep.stdio.file, &c, 1)) |
2014 | if (!read (pt->rep.stdio.file, &c, 1)) |
… | |
… | |
2016 | return EOF; |
2022 | return EOF; |
2017 | else |
2023 | else |
2018 | return *pt->rep.string.curr++; |
2024 | return *pt->rep.string.curr++; |
2019 | } |
2025 | } |
2020 | #else |
2026 | #else |
2021 | if (ungot == -1) |
|
|
2022 | { |
|
|
2023 | char c; |
2027 | char c; |
2024 | if (!read (0, &c, 1)) |
2028 | |
|
|
2029 | if (!read (pt->rep.stdio.file, &c, 1)) |
2025 | return EOF; |
2030 | return EOF; |
2026 | |
2031 | |
2027 | ungot = c; |
|
|
2028 | } |
|
|
2029 | |
|
|
2030 | { |
|
|
2031 | int r = ungot; |
|
|
2032 | ungot = -1; |
|
|
2033 | return r; |
2032 | return c; |
2034 | } |
|
|
2035 | #endif |
2033 | #endif |
2036 | } |
2034 | } |
2037 | |
2035 | |
2038 | /* back character to input buffer */ |
2036 | /* back character to input buffer */ |
2039 | static void |
2037 | ecb_cold static void |
2040 | backchar (SCHEME_P_ int c) |
2038 | backchar (SCHEME_P_ int c) |
2041 | { |
2039 | { |
2042 | #if USE_PORTS |
2040 | port *pt = port (SCHEME_V->inport); |
2043 | port *pt; |
|
|
2044 | |
2041 | |
2045 | if (c == EOF) |
2042 | if (c == EOF) |
2046 | return; |
2043 | return; |
2047 | |
2044 | |
2048 | pt = port (SCHEME_V->inport); |
|
|
2049 | pt->unget = c; |
2045 | pt->unget = c; |
2050 | #else |
|
|
2051 | if (c == EOF) |
|
|
2052 | return; |
|
|
2053 | |
|
|
2054 | ungot = c; |
|
|
2055 | #endif |
|
|
2056 | } |
2046 | } |
2057 | |
2047 | |
2058 | #if USE_PORTS |
2048 | #if USE_PORTS |
2059 | static int |
2049 | ecb_cold static int |
2060 | realloc_port_string (SCHEME_P_ port *p) |
2050 | realloc_port_string (SCHEME_P_ port *p) |
2061 | { |
2051 | { |
2062 | char *start = p->rep.string.start; |
2052 | char *start = p->rep.string.start; |
2063 | size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; |
2053 | size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; |
2064 | char *str = malloc (new_size); |
2054 | char *str = malloc (new_size); |
… | |
… | |
2077 | else |
2067 | else |
2078 | return 0; |
2068 | return 0; |
2079 | } |
2069 | } |
2080 | #endif |
2070 | #endif |
2081 | |
2071 | |
2082 | INTERFACE void |
2072 | ecb_cold static void |
2083 | putstr (SCHEME_P_ const char *s) |
2073 | putchars (SCHEME_P_ const char *s, int len) |
2084 | { |
2074 | { |
|
|
2075 | port *pt = port (SCHEME_V->outport); |
|
|
2076 | |
2085 | #if USE_PORTS |
2077 | #if USE_PORTS |
2086 | port *pt = port (SCHEME_V->outport); |
|
|
2087 | |
|
|
2088 | if (pt->kind & port_file) |
|
|
2089 | write (pt->rep.stdio.file, s, strlen (s)); |
|
|
2090 | else |
|
|
2091 | for (; *s; s++) |
|
|
2092 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2093 | *pt->rep.string.curr++ = *s; |
|
|
2094 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2095 | *pt->rep.string.curr++ = *s; |
|
|
2096 | |
|
|
2097 | #else |
|
|
2098 | xwrstr (s); |
|
|
2099 | #endif |
|
|
2100 | } |
|
|
2101 | |
|
|
2102 | static void |
|
|
2103 | putchars (SCHEME_P_ const char *s, int len) |
|
|
2104 | { |
|
|
2105 | #if USE_PORTS |
|
|
2106 | port *pt = port (SCHEME_V->outport); |
|
|
2107 | |
|
|
2108 | if (pt->kind & port_file) |
2078 | if (pt->kind & port_file) |
2109 | write (pt->rep.stdio.file, s, len); |
2079 | write (pt->rep.stdio.file, s, len); |
2110 | else |
2080 | else |
2111 | { |
2081 | { |
2112 | for (; len; len--) |
2082 | for (; len; len--) |
… | |
… | |
2117 | *pt->rep.string.curr++ = *s++; |
2087 | *pt->rep.string.curr++ = *s++; |
2118 | } |
2088 | } |
2119 | } |
2089 | } |
2120 | |
2090 | |
2121 | #else |
2091 | #else |
2122 | write (1, s, len); |
2092 | write (1, s, len); // output not initialised |
2123 | #endif |
2093 | #endif |
|
|
2094 | } |
|
|
2095 | |
|
|
2096 | INTERFACE void |
|
|
2097 | putstr (SCHEME_P_ const char *s) |
|
|
2098 | { |
|
|
2099 | putchars (SCHEME_A_ s, strlen (s)); |
2124 | } |
2100 | } |
2125 | |
2101 | |
2126 | INTERFACE void |
2102 | INTERFACE void |
2127 | putcharacter (SCHEME_P_ int c) |
2103 | putcharacter (SCHEME_P_ int c) |
2128 | { |
2104 | { |
2129 | #if USE_PORTS |
|
|
2130 | port *pt = port (SCHEME_V->outport); |
|
|
2131 | |
|
|
2132 | if (pt->kind & port_file) |
|
|
2133 | { |
|
|
2134 | char cc = c; |
|
|
2135 | write (pt->rep.stdio.file, &cc, 1); |
|
|
2136 | } |
|
|
2137 | else |
|
|
2138 | { |
|
|
2139 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2140 | *pt->rep.string.curr++ = c; |
|
|
2141 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2142 | *pt->rep.string.curr++ = c; |
|
|
2143 | } |
|
|
2144 | |
|
|
2145 | #else |
|
|
2146 | char cc = c; |
2105 | char cc = c; |
2147 | write (1, &c, 1); |
2106 | |
2148 | #endif |
2107 | putchars (SCHEME_A_ &cc, 1); |
2149 | } |
2108 | } |
2150 | |
2109 | |
2151 | /* read characters up to delimiter, but cater to character constants */ |
2110 | /* read characters up to delimiter, but cater to character constants */ |
2152 | static char * |
2111 | ecb_cold static char * |
2153 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2112 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2154 | { |
2113 | { |
2155 | char *p = SCHEME_V->strbuff + skip; |
2114 | char *p = SCHEME_V->strbuff + skip; |
2156 | |
2115 | |
2157 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2116 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
… | |
… | |
2166 | |
2125 | |
2167 | return SCHEME_V->strbuff; |
2126 | return SCHEME_V->strbuff; |
2168 | } |
2127 | } |
2169 | |
2128 | |
2170 | /* read string expression "xxx...xxx" */ |
2129 | /* read string expression "xxx...xxx" */ |
2171 | static pointer |
2130 | ecb_cold static pointer |
2172 | readstrexp (SCHEME_P_ char delim) |
2131 | readstrexp (SCHEME_P_ char delim) |
2173 | { |
2132 | { |
2174 | char *p = SCHEME_V->strbuff; |
2133 | char *p = SCHEME_V->strbuff; |
2175 | int c; |
2134 | int c; |
2176 | int c1 = 0; |
2135 | int c1 = 0; |
… | |
… | |
2209 | case '7': |
2168 | case '7': |
2210 | state = st_oct1; |
2169 | state = st_oct1; |
2211 | c1 = c - '0'; |
2170 | c1 = c - '0'; |
2212 | break; |
2171 | break; |
2213 | |
2172 | |
|
|
2173 | case 'a': *p++ = '\a'; state = st_ok; break; |
|
|
2174 | case 'n': *p++ = '\n'; state = st_ok; break; |
|
|
2175 | case 'r': *p++ = '\r'; state = st_ok; break; |
|
|
2176 | case 't': *p++ = '\t'; state = st_ok; break; |
|
|
2177 | |
|
|
2178 | // this overshoots the minimum requirements of r7rs |
|
|
2179 | case ' ': |
|
|
2180 | case '\t': |
|
|
2181 | case '\r': |
|
|
2182 | case '\n': |
|
|
2183 | skipspace (SCHEME_A); |
|
|
2184 | state = st_ok; |
|
|
2185 | break; |
|
|
2186 | |
|
|
2187 | //TODO: x should end in ;, not two-digit hex |
2214 | case 'x': |
2188 | case 'x': |
2215 | case 'X': |
2189 | case 'X': |
2216 | state = st_x1; |
2190 | state = st_x1; |
2217 | c1 = 0; |
2191 | c1 = 0; |
2218 | break; |
|
|
2219 | |
|
|
2220 | case 'n': |
|
|
2221 | *p++ = '\n'; |
|
|
2222 | state = st_ok; |
|
|
2223 | break; |
|
|
2224 | |
|
|
2225 | case 't': |
|
|
2226 | *p++ = '\t'; |
|
|
2227 | state = st_ok; |
|
|
2228 | break; |
|
|
2229 | |
|
|
2230 | case 'r': |
|
|
2231 | *p++ = '\r'; |
|
|
2232 | state = st_ok; |
|
|
2233 | break; |
2192 | break; |
2234 | |
2193 | |
2235 | default: |
2194 | default: |
2236 | *p++ = c; |
2195 | *p++ = c; |
2237 | state = st_ok; |
2196 | state = st_ok; |
… | |
… | |
2289 | } |
2248 | } |
2290 | } |
2249 | } |
2291 | } |
2250 | } |
2292 | |
2251 | |
2293 | /* check c is in chars */ |
2252 | /* check c is in chars */ |
2294 | ecb_inline int |
2253 | ecb_cold int |
2295 | is_one_of (const char *s, int c) |
2254 | is_one_of (const char *s, int c) |
2296 | { |
2255 | { |
2297 | return c == EOF || !!strchr (s, c); |
2256 | return c == EOF || !!strchr (s, c); |
2298 | } |
2257 | } |
2299 | |
2258 | |
2300 | /* skip white characters */ |
2259 | /* skip white characters */ |
2301 | ecb_inline int |
2260 | ecb_cold int |
2302 | skipspace (SCHEME_P) |
2261 | skipspace (SCHEME_P) |
2303 | { |
2262 | { |
2304 | int c, curr_line = 0; |
2263 | int c, curr_line = 0; |
2305 | |
2264 | |
2306 | do |
2265 | do |
… | |
… | |
2326 | backchar (SCHEME_A_ c); |
2285 | backchar (SCHEME_A_ c); |
2327 | return 1; |
2286 | return 1; |
2328 | } |
2287 | } |
2329 | |
2288 | |
2330 | /* get token */ |
2289 | /* get token */ |
2331 | static int |
2290 | ecb_cold static int |
2332 | token (SCHEME_P) |
2291 | token (SCHEME_P) |
2333 | { |
2292 | { |
2334 | int c = skipspace (SCHEME_A); |
2293 | int c = skipspace (SCHEME_A); |
2335 | |
2294 | |
2336 | if (c == EOF) |
2295 | if (c == EOF) |
… | |
… | |
2434 | } |
2393 | } |
2435 | |
2394 | |
2436 | /* ========== Routines for Printing ========== */ |
2395 | /* ========== Routines for Printing ========== */ |
2437 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2396 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2438 | |
2397 | |
2439 | static void |
2398 | ecb_cold static void |
2440 | printslashstring (SCHEME_P_ char *p, int len) |
2399 | printslashstring (SCHEME_P_ char *p, int len) |
2441 | { |
2400 | { |
2442 | int i; |
2401 | int i; |
2443 | unsigned char *s = (unsigned char *) p; |
2402 | unsigned char *s = (unsigned char *) p; |
2444 | |
2403 | |
… | |
… | |
2500 | |
2459 | |
2501 | putcharacter (SCHEME_A_ '"'); |
2460 | putcharacter (SCHEME_A_ '"'); |
2502 | } |
2461 | } |
2503 | |
2462 | |
2504 | /* print atoms */ |
2463 | /* print atoms */ |
2505 | static void |
2464 | ecb_cold static void |
2506 | printatom (SCHEME_P_ pointer l, int f) |
2465 | printatom (SCHEME_P_ pointer l, int f) |
2507 | { |
2466 | { |
2508 | char *p; |
2467 | char *p; |
2509 | int len; |
2468 | int len; |
2510 | |
2469 | |
2511 | atom2str (SCHEME_A_ l, f, &p, &len); |
2470 | atom2str (SCHEME_A_ l, f, &p, &len); |
2512 | putchars (SCHEME_A_ p, len); |
2471 | putchars (SCHEME_A_ p, len); |
2513 | } |
2472 | } |
2514 | |
2473 | |
2515 | /* Uses internal buffer unless string pointer is already available */ |
2474 | /* Uses internal buffer unless string pointer is already available */ |
2516 | static void |
2475 | ecb_cold static void |
2517 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2476 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2518 | { |
2477 | { |
2519 | char *p; |
2478 | char *p; |
2520 | |
2479 | |
2521 | if (l == NIL) |
2480 | if (l == NIL) |
… | |
… | |
2728 | return car (d); |
2687 | return car (d); |
2729 | |
2688 | |
2730 | p = cons (car (d), cdr (d)); |
2689 | p = cons (car (d), cdr (d)); |
2731 | q = p; |
2690 | q = p; |
2732 | |
2691 | |
2733 | while (cdr (cdr (p)) != NIL) |
2692 | while (cddr (p) != NIL) |
2734 | { |
2693 | { |
2735 | d = cons (car (p), cdr (p)); |
2694 | d = cons (car (p), cdr (p)); |
2736 | |
2695 | |
2737 | if (cdr (cdr (p)) != NIL) |
2696 | if (cddr (p) != NIL) |
2738 | p = cdr (d); |
2697 | p = cdr (d); |
2739 | } |
2698 | } |
2740 | |
2699 | |
2741 | set_cdr (p, car (cdr (p))); |
2700 | set_cdr (p, cadr (p)); |
2742 | return q; |
2701 | return q; |
2743 | } |
2702 | } |
2744 | |
2703 | |
2745 | /* reverse list -- produce new list */ |
2704 | /* reverse list -- produce new list */ |
2746 | static pointer |
2705 | ecb_hot static pointer |
2747 | reverse (SCHEME_P_ pointer a) |
2706 | reverse (SCHEME_P_ pointer a) |
2748 | { |
2707 | { |
2749 | /* a must be checked by gc */ |
2708 | /* a must be checked by gc */ |
2750 | pointer p = NIL; |
2709 | pointer p = NIL; |
2751 | |
2710 | |
… | |
… | |
2754 | |
2713 | |
2755 | return p; |
2714 | return p; |
2756 | } |
2715 | } |
2757 | |
2716 | |
2758 | /* reverse list --- in-place */ |
2717 | /* reverse list --- in-place */ |
2759 | static pointer |
2718 | ecb_hot static pointer |
2760 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2719 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2761 | { |
2720 | { |
2762 | pointer result = term; |
2721 | pointer result = term; |
2763 | pointer p = list; |
2722 | pointer p = list; |
2764 | |
2723 | |
… | |
… | |
2772 | |
2731 | |
2773 | return result; |
2732 | return result; |
2774 | } |
2733 | } |
2775 | |
2734 | |
2776 | /* append list -- produce new list (in reverse order) */ |
2735 | /* append list -- produce new list (in reverse order) */ |
2777 | static pointer |
2736 | ecb_hot static pointer |
2778 | revappend (SCHEME_P_ pointer a, pointer b) |
2737 | revappend (SCHEME_P_ pointer a, pointer b) |
2779 | { |
2738 | { |
2780 | pointer result = a; |
2739 | pointer result = a; |
2781 | pointer p = b; |
2740 | pointer p = b; |
2782 | |
2741 | |
… | |
… | |
2791 | |
2750 | |
2792 | return S_F; /* signal an error */ |
2751 | return S_F; /* signal an error */ |
2793 | } |
2752 | } |
2794 | |
2753 | |
2795 | /* equivalence of atoms */ |
2754 | /* equivalence of atoms */ |
2796 | int |
2755 | ecb_hot int |
2797 | eqv (pointer a, pointer b) |
2756 | eqv (pointer a, pointer b) |
2798 | { |
2757 | { |
2799 | if (is_string (a)) |
2758 | if (is_string (a)) |
2800 | { |
2759 | { |
2801 | if (is_string (b)) |
2760 | if (is_string (b)) |
… | |
… | |
2895 | } |
2854 | } |
2896 | else |
2855 | else |
2897 | set_car (env, immutable_cons (slot, car (env))); |
2856 | set_car (env, immutable_cons (slot, car (env))); |
2898 | } |
2857 | } |
2899 | |
2858 | |
2900 | static pointer |
2859 | ecb_hot static pointer |
2901 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2860 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2902 | { |
2861 | { |
2903 | pointer x, y; |
2862 | pointer x, y; |
2904 | |
2863 | |
2905 | for (x = env; x != NIL; x = cdr (x)) |
2864 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2926 | return NIL; |
2885 | return NIL; |
2927 | } |
2886 | } |
2928 | |
2887 | |
2929 | #else /* USE_ALIST_ENV */ |
2888 | #else /* USE_ALIST_ENV */ |
2930 | |
2889 | |
2931 | ecb_inline void |
2890 | static void |
2932 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2891 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2933 | { |
2892 | { |
2934 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2893 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2935 | setenvironment (SCHEME_V->envir); |
2894 | setenvironment (SCHEME_V->envir); |
2936 | } |
2895 | } |
2937 | |
2896 | |
2938 | ecb_inline void |
2897 | static void |
2939 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2898 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2940 | { |
2899 | { |
2941 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2900 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2942 | } |
2901 | } |
2943 | |
2902 | |
2944 | static pointer |
2903 | ecb_hot static pointer |
2945 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2904 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2946 | { |
2905 | { |
2947 | pointer x, y; |
2906 | pointer x, y; |
2948 | |
2907 | |
2949 | for (x = env; x != NIL; x = cdr (x)) |
2908 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2963 | return NIL; |
2922 | return NIL; |
2964 | } |
2923 | } |
2965 | |
2924 | |
2966 | #endif /* USE_ALIST_ENV else */ |
2925 | #endif /* USE_ALIST_ENV else */ |
2967 | |
2926 | |
2968 | ecb_inline void |
2927 | static void |
2969 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2928 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2970 | { |
2929 | { |
2971 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2930 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2972 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2931 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2973 | } |
2932 | } |
2974 | |
2933 | |
2975 | ecb_inline void |
2934 | static void |
2976 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2935 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2977 | { |
2936 | { |
2978 | set_cdr (slot, value); |
2937 | set_cdr (slot, value); |
2979 | } |
2938 | } |
2980 | |
2939 | |
2981 | ecb_inline pointer |
2940 | static pointer |
2982 | slot_value_in_env (pointer slot) |
2941 | slot_value_in_env (pointer slot) |
2983 | { |
2942 | { |
2984 | return cdr (slot); |
2943 | return cdr (slot); |
2985 | } |
2944 | } |
2986 | |
2945 | |
2987 | /* ========== Evaluation Cycle ========== */ |
2946 | /* ========== Evaluation Cycle ========== */ |
2988 | |
2947 | |
2989 | static int |
2948 | ecb_cold static int |
2990 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2949 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2991 | { |
2950 | { |
2992 | #if USE_ERROR_HOOK |
|
|
2993 | pointer x; |
|
|
2994 | pointer hdl = SCHEME_V->ERROR_HOOK; |
|
|
2995 | #endif |
|
|
2996 | |
|
|
2997 | #if USE_PRINTF |
2951 | #if USE_PRINTF |
2998 | #if SHOW_ERROR_LINE |
2952 | #if SHOW_ERROR_LINE |
2999 | char sbuf[STRBUFFSIZE]; |
2953 | char sbuf[STRBUFFSIZE]; |
3000 | |
2954 | |
3001 | /* make sure error is not in REPL */ |
2955 | /* make sure error is not in REPL */ |
… | |
… | |
3016 | } |
2970 | } |
3017 | #endif |
2971 | #endif |
3018 | #endif |
2972 | #endif |
3019 | |
2973 | |
3020 | #if USE_ERROR_HOOK |
2974 | #if USE_ERROR_HOOK |
3021 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); |
2975 | pointer x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->ERROR_HOOK, 1); |
3022 | |
2976 | |
3023 | if (x != NIL) |
2977 | if (x != NIL) |
3024 | { |
2978 | { |
3025 | pointer code = a |
2979 | pointer code = a |
3026 | ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) |
2980 | ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) |
… | |
… | |
3070 | pointer code; |
3024 | pointer code; |
3071 | }; |
3025 | }; |
3072 | |
3026 | |
3073 | # define STACK_GROWTH 3 |
3027 | # define STACK_GROWTH 3 |
3074 | |
3028 | |
3075 | static void |
3029 | ecb_hot static void |
3076 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3030 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3077 | { |
3031 | { |
3078 | int nframes = (uintptr_t)SCHEME_V->dump; |
3032 | int nframes = (uintptr_t)SCHEME_V->dump; |
3079 | struct dump_stack_frame *next_frame; |
3033 | struct dump_stack_frame *next_frame; |
3080 | |
3034 | |
… | |
… | |
3093 | next_frame->code = code; |
3047 | next_frame->code = code; |
3094 | |
3048 | |
3095 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3049 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3096 | } |
3050 | } |
3097 | |
3051 | |
3098 | static int |
3052 | static ecb_hot int |
3099 | xs_return (SCHEME_P_ pointer a) |
3053 | xs_return (SCHEME_P_ pointer a) |
3100 | { |
3054 | { |
3101 | int nframes = (uintptr_t)SCHEME_V->dump; |
3055 | int nframes = (uintptr_t)SCHEME_V->dump; |
3102 | struct dump_stack_frame *frame; |
3056 | struct dump_stack_frame *frame; |
3103 | |
3057 | |
… | |
… | |
3114 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3068 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3115 | |
3069 | |
3116 | return 0; |
3070 | return 0; |
3117 | } |
3071 | } |
3118 | |
3072 | |
3119 | ecb_inline void |
3073 | ecb_cold void |
3120 | dump_stack_reset (SCHEME_P) |
3074 | dump_stack_reset (SCHEME_P) |
3121 | { |
3075 | { |
3122 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3076 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3123 | SCHEME_V->dump = (pointer)+0; |
3077 | SCHEME_V->dump = (pointer)+0; |
3124 | } |
3078 | } |
3125 | |
3079 | |
3126 | ecb_inline void |
3080 | ecb_cold void |
3127 | dump_stack_initialize (SCHEME_P) |
3081 | dump_stack_initialize (SCHEME_P) |
3128 | { |
3082 | { |
3129 | SCHEME_V->dump_size = 0; |
3083 | SCHEME_V->dump_size = 0; |
3130 | SCHEME_V->dump_base = 0; |
3084 | SCHEME_V->dump_base = 0; |
3131 | dump_stack_reset (SCHEME_A); |
3085 | dump_stack_reset (SCHEME_A); |
3132 | } |
3086 | } |
3133 | |
3087 | |
3134 | static void |
3088 | ecb_cold static void |
3135 | dump_stack_free (SCHEME_P) |
3089 | dump_stack_free (SCHEME_P) |
3136 | { |
3090 | { |
3137 | free (SCHEME_V->dump_base); |
3091 | free (SCHEME_V->dump_base); |
3138 | SCHEME_V->dump_base = 0; |
3092 | SCHEME_V->dump_base = 0; |
3139 | SCHEME_V->dump = (pointer)0; |
3093 | SCHEME_V->dump = (pointer)0; |
3140 | SCHEME_V->dump_size = 0; |
3094 | SCHEME_V->dump_size = 0; |
3141 | } |
3095 | } |
3142 | |
3096 | |
3143 | static void |
3097 | ecb_cold static void |
3144 | dump_stack_mark (SCHEME_P) |
3098 | dump_stack_mark (SCHEME_P) |
3145 | { |
3099 | { |
3146 | int nframes = (uintptr_t)SCHEME_V->dump; |
3100 | int nframes = (uintptr_t)SCHEME_V->dump; |
3147 | int i; |
3101 | int i; |
3148 | |
3102 | |
… | |
… | |
3154 | mark (frame->envir); |
3108 | mark (frame->envir); |
3155 | mark (frame->code); |
3109 | mark (frame->code); |
3156 | } |
3110 | } |
3157 | } |
3111 | } |
3158 | |
3112 | |
3159 | static pointer |
3113 | ecb_cold static pointer |
3160 | ss_get_cont (SCHEME_P) |
3114 | ss_get_cont (SCHEME_P) |
3161 | { |
3115 | { |
3162 | int nframes = (uintptr_t)SCHEME_V->dump; |
3116 | int nframes = (uintptr_t)SCHEME_V->dump; |
3163 | int i; |
3117 | int i; |
3164 | |
3118 | |
… | |
… | |
3176 | } |
3130 | } |
3177 | |
3131 | |
3178 | return cont; |
3132 | return cont; |
3179 | } |
3133 | } |
3180 | |
3134 | |
3181 | static void |
3135 | ecb_cold static void |
3182 | ss_set_cont (SCHEME_P_ pointer cont) |
3136 | ss_set_cont (SCHEME_P_ pointer cont) |
3183 | { |
3137 | { |
3184 | int i = 0; |
3138 | int i = 0; |
3185 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3139 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3186 | |
3140 | |
… | |
… | |
3198 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3152 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3199 | } |
3153 | } |
3200 | |
3154 | |
3201 | #else |
3155 | #else |
3202 | |
3156 | |
3203 | ecb_inline void |
3157 | ecb_cold void |
3204 | dump_stack_reset (SCHEME_P) |
3158 | dump_stack_reset (SCHEME_P) |
3205 | { |
3159 | { |
3206 | SCHEME_V->dump = NIL; |
3160 | SCHEME_V->dump = NIL; |
3207 | } |
3161 | } |
3208 | |
3162 | |
3209 | ecb_inline void |
3163 | ecb_cold void |
3210 | dump_stack_initialize (SCHEME_P) |
3164 | dump_stack_initialize (SCHEME_P) |
3211 | { |
3165 | { |
3212 | dump_stack_reset (SCHEME_A); |
3166 | dump_stack_reset (SCHEME_A); |
3213 | } |
3167 | } |
3214 | |
3168 | |
3215 | static void |
3169 | ecb_cold static void |
3216 | dump_stack_free (SCHEME_P) |
3170 | dump_stack_free (SCHEME_P) |
3217 | { |
3171 | { |
3218 | SCHEME_V->dump = NIL; |
3172 | SCHEME_V->dump = NIL; |
3219 | } |
3173 | } |
3220 | |
3174 | |
3221 | static int |
3175 | ecb_hot static int |
3222 | xs_return (SCHEME_P_ pointer a) |
3176 | xs_return (SCHEME_P_ pointer a) |
3223 | { |
3177 | { |
3224 | pointer dump = SCHEME_V->dump; |
3178 | pointer dump = SCHEME_V->dump; |
3225 | |
3179 | |
3226 | SCHEME_V->value = a; |
3180 | SCHEME_V->value = a; |
… | |
… | |
3236 | SCHEME_V->dump = dump; |
3190 | SCHEME_V->dump = dump; |
3237 | |
3191 | |
3238 | return 0; |
3192 | return 0; |
3239 | } |
3193 | } |
3240 | |
3194 | |
3241 | static void |
3195 | ecb_hot static void |
3242 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3196 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3243 | { |
3197 | { |
3244 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3198 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3245 | cons (args, |
3199 | cons (args, |
3246 | cons (SCHEME_V->envir, |
3200 | cons (SCHEME_V->envir, |
3247 | cons (code, |
3201 | cons (code, |
3248 | SCHEME_V->dump)))); |
3202 | SCHEME_V->dump)))); |
3249 | } |
3203 | } |
3250 | |
3204 | |
3251 | static void |
3205 | ecb_cold static void |
3252 | dump_stack_mark (SCHEME_P) |
3206 | dump_stack_mark (SCHEME_P) |
3253 | { |
3207 | { |
3254 | mark (SCHEME_V->dump); |
3208 | mark (SCHEME_V->dump); |
3255 | } |
3209 | } |
3256 | |
3210 | |
3257 | static pointer |
3211 | ecb_cold static pointer |
3258 | ss_get_cont (SCHEME_P) |
3212 | ss_get_cont (SCHEME_P) |
3259 | { |
3213 | { |
3260 | return SCHEME_V->dump; |
3214 | return SCHEME_V->dump; |
3261 | } |
3215 | } |
3262 | |
3216 | |
3263 | static void |
3217 | ecb_cold static void |
3264 | ss_set_cont (SCHEME_P_ pointer cont) |
3218 | ss_set_cont (SCHEME_P_ pointer cont) |
3265 | { |
3219 | { |
3266 | SCHEME_V->dump = cont; |
3220 | SCHEME_V->dump = cont; |
3267 | } |
3221 | } |
3268 | |
3222 | |
3269 | #endif |
3223 | #endif |
3270 | |
3224 | |
3271 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3225 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3272 | |
3226 | |
3273 | #if EXPERIMENT |
3227 | #if EXPERIMENT |
|
|
3228 | |
3274 | static int |
3229 | static int |
3275 | debug (SCHEME_P_ int indent, pointer x) |
3230 | dtree (SCHEME_P_ int indent, pointer x) |
3276 | { |
3231 | { |
3277 | int c; |
3232 | int c; |
3278 | |
3233 | |
3279 | if (is_syntax (x)) |
3234 | if (is_syntax (x)) |
3280 | { |
3235 | { |
… | |
… | |
3298 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
3253 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
3299 | return 24+8; |
3254 | return 24+8; |
3300 | |
3255 | |
3301 | case T_CLOSURE: |
3256 | case T_CLOSURE: |
3302 | printf ("%*sS<%s>\n", indent, "", "closure"); |
3257 | printf ("%*sS<%s>\n", indent, "", "closure"); |
3303 | debug (SCHEME_A_ indent + 3, cdr(x)); |
3258 | dtree (SCHEME_A_ indent + 3, cdr(x)); |
3304 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
3259 | return 32 + dtree (SCHEME_A_ indent + 3, car (x)); |
3305 | |
3260 | |
3306 | case T_PAIR: |
3261 | case T_PAIR: |
3307 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
3262 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
3308 | c = debug (SCHEME_A_ indent + 3, car (x)); |
3263 | c = dtree (SCHEME_A_ indent + 3, car (x)); |
3309 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
3264 | c += dtree (SCHEME_A_ indent + 3, cdr (x)); |
3310 | return c + 1; |
3265 | return c + 1; |
3311 | |
3266 | |
3312 | case T_PORT: |
3267 | case T_PORT: |
3313 | printf ("%*sS<%s>\n", indent, "", "port"); |
3268 | printf ("%*sS<%s>\n", indent, "", "port"); |
3314 | return 24+8; |
3269 | return 24+8; |
… | |
… | |
3317 | printf ("%*sS<%s>\n", indent, "", "vector"); |
3272 | printf ("%*sS<%s>\n", indent, "", "vector"); |
3318 | return 24+8; |
3273 | return 24+8; |
3319 | |
3274 | |
3320 | case T_ENVIRONMENT: |
3275 | case T_ENVIRONMENT: |
3321 | printf ("%*sS<%s>\n", indent, "", "environment"); |
3276 | printf ("%*sS<%s>\n", indent, "", "environment"); |
3322 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
3277 | return 0 + dtree (SCHEME_A_ indent + 3, car (x)); |
3323 | |
3278 | |
3324 | default: |
3279 | default: |
3325 | printf ("unhandled type %d\n", type (x)); |
3280 | printf ("unhandled type %d\n", type (x)); |
3326 | break; |
3281 | break; |
3327 | } |
3282 | } |
3328 | } |
3283 | } |
3329 | #endif |
|
|
3330 | |
3284 | |
|
|
3285 | #define DUMP(t) do { printf ("DUMP %s:%d\n", __FILE__, __LINE__); dtree (SCHEME_A_ 0, (t)); } while (0) |
|
|
3286 | |
|
|
3287 | typedef void *stream[1]; |
|
|
3288 | |
|
|
3289 | #define stream_init() { 0 } |
|
|
3290 | #define stream_data(s) ((char *)(s)[0] + sizeof (uint32_t) * 2) |
|
|
3291 | #define stream_size(s) (((uint32_t *)(s)[0])[0] - sizeof (uint32_t) * 2) |
|
|
3292 | #define stream_free(s) free (s[0]) |
|
|
3293 | |
|
|
3294 | ecb_cold static void |
|
|
3295 | stream_put (stream s, uint8_t byte) |
|
|
3296 | { |
|
|
3297 | uint32_t *sp = *s; |
|
|
3298 | uint32_t size = sizeof (uint32_t) * 2; |
|
|
3299 | uint32_t offs = size; |
|
|
3300 | |
|
|
3301 | if (ecb_expect_true (sp)) |
|
|
3302 | { |
|
|
3303 | offs = sp[0]; |
|
|
3304 | size = sp[1]; |
|
|
3305 | } |
|
|
3306 | |
|
|
3307 | if (ecb_expect_false (offs == size)) |
|
|
3308 | { |
|
|
3309 | size *= 2; |
|
|
3310 | sp = realloc (sp, size); |
|
|
3311 | *s = sp; |
|
|
3312 | sp[1] = size; |
|
|
3313 | |
|
|
3314 | } |
|
|
3315 | |
|
|
3316 | ((uint8_t *)sp)[offs++] = byte; |
|
|
3317 | sp[0] = offs; |
|
|
3318 | } |
|
|
3319 | |
|
|
3320 | ecb_cold static void |
|
|
3321 | stream_put_v (stream s, uint32_t v) |
|
|
3322 | { |
|
|
3323 | while (v > 0x7f) |
|
|
3324 | { |
|
|
3325 | stream_put (s, v | 0x80); |
|
|
3326 | v >>= 7; |
|
|
3327 | } |
|
|
3328 | |
|
|
3329 | stream_put (s, v); |
|
|
3330 | } |
|
|
3331 | |
|
|
3332 | ecb_cold static void |
|
|
3333 | stream_put_tv (stream s, int bop, uint32_t v) |
|
|
3334 | { |
|
|
3335 | printf ("put tv %d %d\n", bop, v);//D |
|
|
3336 | stream_put (s, bop); |
|
|
3337 | stream_put_v (s, v); |
|
|
3338 | } |
|
|
3339 | |
|
|
3340 | ecb_cold static void |
|
|
3341 | stream_put_stream (stream s, stream o) |
|
|
3342 | { |
|
|
3343 | uint32_t i; |
|
|
3344 | |
|
|
3345 | for (i = 0; i < stream_size (o); ++i) |
|
|
3346 | stream_put (s, stream_data (o)[i]); |
|
|
3347 | |
|
|
3348 | stream_free (o); |
|
|
3349 | } |
|
|
3350 | |
|
|
3351 | ecb_cold static uint32_t |
|
|
3352 | cell_id (SCHEME_P_ pointer x) |
|
|
3353 | { |
|
|
3354 | struct cell *p = CELL (x); |
|
|
3355 | int i; |
|
|
3356 | |
|
|
3357 | for (i = SCHEME_V->last_cell_seg; i >= 0; --i) |
|
|
3358 | if (SCHEME_V->cell_seg[i] <= p && p < SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize[i]) |
|
|
3359 | return i | ((p - SCHEME_V->cell_seg[i]) << CELL_NSEGMENT_LOG); |
|
|
3360 | |
|
|
3361 | abort (); |
|
|
3362 | } |
|
|
3363 | |
|
|
3364 | // calculates a (preferably small) integer that makes it possible to find |
|
|
3365 | // the symbol again. if pointers were offsets into a memory area... until |
|
|
3366 | // then, we return segment number in the low bits, and offset in the high |
|
|
3367 | // bits. |
|
|
3368 | // also, this function must never return 0. |
|
|
3369 | ecb_cold static uint32_t |
|
|
3370 | symbol_id (SCHEME_P_ pointer sym) |
|
|
3371 | { |
|
|
3372 | return cell_id (SCHEME_A_ sym); |
|
|
3373 | } |
|
|
3374 | |
|
|
3375 | enum byteop |
|
|
3376 | { |
|
|
3377 | BOP_NIL, |
|
|
3378 | BOP_INTEGER, |
|
|
3379 | BOP_SYMBOL, |
|
|
3380 | BOP_DATUM, |
|
|
3381 | BOP_LIST_BEG, |
|
|
3382 | BOP_LIST_END, |
|
|
3383 | BOP_IF, |
|
|
3384 | BOP_AND, |
|
|
3385 | BOP_OR, |
|
|
3386 | BOP_CASE, |
|
|
3387 | BOP_COND, |
|
|
3388 | BOP_LET, |
|
|
3389 | BOP_LETAST, |
|
|
3390 | BOP_LETREC, |
|
|
3391 | BOP_DEFINE, |
|
|
3392 | BOP_MACRO, |
|
|
3393 | BOP_SET, |
|
|
3394 | BOP_BEGIN, |
|
|
3395 | BOP_LAMBDA, |
|
|
3396 | BOP_OP, |
|
|
3397 | }; |
|
|
3398 | |
|
|
3399 | ecb_cold static void compile_expr (SCHEME_P_ stream s, pointer x); |
|
|
3400 | |
|
|
3401 | ecb_cold static void |
|
|
3402 | compile_list (SCHEME_P_ stream s, pointer x) |
|
|
3403 | { |
|
|
3404 | // TODO: improper list |
|
|
3405 | |
|
|
3406 | for (; x != NIL; x = cdr (x)) |
|
|
3407 | { |
|
|
3408 | stream t = stream_init (); |
|
|
3409 | compile_expr (SCHEME_A_ t, car (x)); |
|
|
3410 | stream_put_v (s, stream_size (t)); |
|
|
3411 | stream_put_stream (s, t); |
|
|
3412 | } |
|
|
3413 | |
|
|
3414 | stream_put_v (s, 0); |
|
|
3415 | } |
|
|
3416 | |
|
|
3417 | static void |
|
|
3418 | compile_if (SCHEME_P_ stream s, pointer cond, pointer ift, pointer iff) |
|
|
3419 | { |
|
|
3420 | stream sift = stream_init (); compile_expr (SCHEME_A_ sift, ift); |
|
|
3421 | |
|
|
3422 | stream_put (s, BOP_IF); |
|
|
3423 | compile_expr (SCHEME_A_ s, cond); |
|
|
3424 | stream_put_v (s, stream_size (sift)); |
|
|
3425 | stream_put_stream (s, sift); |
|
|
3426 | compile_expr (SCHEME_A_ s, iff); |
|
|
3427 | } |
|
|
3428 | |
|
|
3429 | typedef uint32_t stream_fixup; |
|
|
3430 | |
|
|
3431 | static stream_fixup |
|
|
3432 | stream_put_fixup (stream s) |
|
|
3433 | { |
|
|
3434 | stream_put (s, 0); |
|
|
3435 | stream_put (s, 0); |
|
|
3436 | |
|
|
3437 | return stream_size (s); |
|
|
3438 | } |
|
|
3439 | |
|
|
3440 | static void |
|
|
3441 | stream_fix_fixup (stream s, stream_fixup fixup, uint32_t target) |
|
|
3442 | { |
|
|
3443 | target -= fixup; |
|
|
3444 | assert (target < (1 << 14)); |
|
|
3445 | stream_data (s)[fixup - 2] = target | 0x80; |
|
|
3446 | stream_data (s)[fixup - 1] = target >> 7; |
|
|
3447 | } |
|
|
3448 | |
|
|
3449 | static void |
|
|
3450 | compile_and_or (SCHEME_P_ stream s, int and, pointer x) |
|
|
3451 | { |
|
|
3452 | for (; cdr (x) != NIL; x = cdr (x)) |
|
|
3453 | { |
|
|
3454 | stream t = stream_init (); |
|
|
3455 | compile_expr (SCHEME_A_ t, car (x)); |
|
|
3456 | stream_put_v (s, stream_size (t)); |
|
|
3457 | stream_put_stream (s, t); |
|
|
3458 | } |
|
|
3459 | |
|
|
3460 | stream_put_v (s, 0); |
|
|
3461 | } |
|
|
3462 | |
|
|
3463 | static void |
|
|
3464 | compile_case (SCHEME_P_ stream s, pointer x) |
|
|
3465 | { |
|
|
3466 | compile_expr (SCHEME_A_ s, caar (x)); |
|
|
3467 | |
|
|
3468 | for (;;) |
|
|
3469 | { |
|
|
3470 | x = cdr (x); |
|
|
3471 | |
|
|
3472 | if (x == NIL) |
|
|
3473 | break; |
|
|
3474 | |
|
|
3475 | compile_expr (SCHEME_A_ s, caar (x)); |
|
|
3476 | stream t = stream_init (); compile_expr (SCHEME_A_ t, cdar (x)); |
|
|
3477 | stream_put_v (s, stream_size (t)); |
|
|
3478 | stream_put_stream (s, t); |
|
|
3479 | } |
|
|
3480 | |
|
|
3481 | stream_put_v (s, 0); |
|
|
3482 | } |
|
|
3483 | |
|
|
3484 | static void |
|
|
3485 | compile_cond (SCHEME_P_ stream s, pointer x) |
|
|
3486 | { |
|
|
3487 | for ( ; x != NIL; x = cdr (x)) |
|
|
3488 | { |
|
|
3489 | compile_expr (SCHEME_A_ s, caar (x)); |
|
|
3490 | stream t = stream_init (); compile_expr (SCHEME_A_ t, cdar (x)); |
|
|
3491 | stream_put_v (s, stream_size (t)); |
|
|
3492 | stream_put_stream (s, t); |
|
|
3493 | } |
|
|
3494 | |
|
|
3495 | stream_put_v (s, 0); |
|
|
3496 | } |
|
|
3497 | |
3331 | static int |
3498 | static pointer |
|
|
3499 | lookup (SCHEME_P_ pointer x) |
|
|
3500 | { |
|
|
3501 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, x, 1); |
|
|
3502 | |
|
|
3503 | if (x != NIL) |
|
|
3504 | x = slot_value_in_env (x); |
|
|
3505 | |
|
|
3506 | return x; |
|
|
3507 | } |
|
|
3508 | |
|
|
3509 | ecb_cold static void |
|
|
3510 | compile_expr (SCHEME_P_ stream s, pointer x) |
|
|
3511 | { |
|
|
3512 | if (x == NIL) |
|
|
3513 | { |
|
|
3514 | stream_put (s, BOP_NIL); |
|
|
3515 | return; |
|
|
3516 | } |
|
|
3517 | |
|
|
3518 | if (is_pair (x)) |
|
|
3519 | { |
|
|
3520 | pointer head = car (x); |
|
|
3521 | |
|
|
3522 | if (is_syntax (head)) |
|
|
3523 | { |
|
|
3524 | x = cdr (x); |
|
|
3525 | |
|
|
3526 | switch (syntaxnum (head)) |
|
|
3527 | { |
|
|
3528 | case OP_IF0: /* if */ |
|
|
3529 | stream_put_v (s, BOP_IF); |
|
|
3530 | compile_if (SCHEME_A_ s, car (x), cadr (x), caddr (x)); |
|
|
3531 | break; |
|
|
3532 | |
|
|
3533 | case OP_OR0: /* or */ |
|
|
3534 | stream_put_v (s, BOP_OR); |
|
|
3535 | compile_and_or (SCHEME_A_ s, 0, x); |
|
|
3536 | break; |
|
|
3537 | |
|
|
3538 | case OP_AND0: /* and */ |
|
|
3539 | stream_put_v (s, BOP_AND); |
|
|
3540 | compile_and_or (SCHEME_A_ s, 1, x); |
|
|
3541 | break; |
|
|
3542 | |
|
|
3543 | case OP_CASE0: /* case */ |
|
|
3544 | stream_put_v (s, BOP_CASE); |
|
|
3545 | compile_case (SCHEME_A_ s, x); |
|
|
3546 | break; |
|
|
3547 | |
|
|
3548 | case OP_COND0: /* cond */ |
|
|
3549 | stream_put_v (s, BOP_COND); |
|
|
3550 | compile_cond (SCHEME_A_ s, x); |
|
|
3551 | break; |
|
|
3552 | |
|
|
3553 | case OP_LET0: /* let */ |
|
|
3554 | case OP_LET0AST: /* let* */ |
|
|
3555 | case OP_LET0REC: /* letrec */ |
|
|
3556 | switch (syntaxnum (head)) |
|
|
3557 | { |
|
|
3558 | case OP_LET0: stream_put (s, BOP_LET ); break; |
|
|
3559 | case OP_LET0AST: stream_put (s, BOP_LETAST); break; |
|
|
3560 | case OP_LET0REC: stream_put (s, BOP_LETREC); break; |
|
|
3561 | } |
|
|
3562 | |
|
|
3563 | { |
|
|
3564 | pointer bindings = car (x); |
|
|
3565 | pointer body = cadr (x); |
|
|
3566 | |
|
|
3567 | for (x = bindings; x != NIL; x = cdr (x)) |
|
|
3568 | { |
|
|
3569 | pointer init = NIL; |
|
|
3570 | pointer var = car (x); |
|
|
3571 | |
|
|
3572 | if (is_pair (var)) |
|
|
3573 | { |
|
|
3574 | init = cdr (var); |
|
|
3575 | var = car (var); |
|
|
3576 | } |
|
|
3577 | |
|
|
3578 | stream_put_v (s, symbol_id (SCHEME_A_ var)); |
|
|
3579 | compile_expr (SCHEME_A_ s, init); |
|
|
3580 | } |
|
|
3581 | |
|
|
3582 | stream_put_v (s, 0); |
|
|
3583 | compile_expr (SCHEME_A_ s, body); |
|
|
3584 | } |
|
|
3585 | break; |
|
|
3586 | |
|
|
3587 | case OP_DEF0: /* define */ |
|
|
3588 | case OP_MACRO0: /* macro */ |
|
|
3589 | stream_put (s, syntaxnum (head) == OP_DEF0 ? BOP_DEFINE : BOP_MACRO); |
|
|
3590 | stream_put_v (s, cell_id (SCHEME_A_ car (x))); |
|
|
3591 | compile_expr (SCHEME_A_ s, cadr (x)); |
|
|
3592 | break; |
|
|
3593 | |
|
|
3594 | case OP_SET0: /* set! */ |
|
|
3595 | stream_put (s, BOP_SET); |
|
|
3596 | stream_put_v (s, symbol_id (SCHEME_A_ car (x))); |
|
|
3597 | compile_expr (SCHEME_A_ s, cadr (x)); |
|
|
3598 | break; |
|
|
3599 | |
|
|
3600 | case OP_BEGIN: /* begin */ |
|
|
3601 | stream_put (s, BOP_BEGIN); |
|
|
3602 | compile_list (SCHEME_A_ s, x); |
|
|
3603 | return; |
|
|
3604 | |
|
|
3605 | case OP_DELAY: /* delay */ |
|
|
3606 | abort (); |
|
|
3607 | break; |
|
|
3608 | |
|
|
3609 | case OP_QUOTE: /* quote */ |
|
|
3610 | stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x)); |
|
|
3611 | break; |
|
|
3612 | |
|
|
3613 | case OP_LAMBDA: /* lambda */ |
|
|
3614 | { |
|
|
3615 | pointer formals = car (x); |
|
|
3616 | pointer body = cadr (x); |
|
|
3617 | |
|
|
3618 | stream_put (s, BOP_LAMBDA); |
|
|
3619 | |
|
|
3620 | for (; is_pair (formals); formals = cdr (formals)) |
|
|
3621 | stream_put_v (s, symbol_id (SCHEME_A_ car (formals))); |
|
|
3622 | |
|
|
3623 | stream_put_v (s, 0); |
|
|
3624 | stream_put_v (s, formals == NIL ? 0 : symbol_id (SCHEME_A_ formals)); |
|
|
3625 | |
|
|
3626 | compile_expr (SCHEME_A_ s, body); |
|
|
3627 | } |
|
|
3628 | break; |
|
|
3629 | |
|
|
3630 | case OP_C0STREAM:/* cons-stream */ |
|
|
3631 | abort (); |
|
|
3632 | break; |
|
|
3633 | } |
|
|
3634 | |
|
|
3635 | return; |
|
|
3636 | } |
|
|
3637 | |
|
|
3638 | pointer m = lookup (SCHEME_A_ head); |
|
|
3639 | |
|
|
3640 | if (is_macro (m)) |
|
|
3641 | { |
|
|
3642 | s_save (SCHEME_A_ OP_DEBUG2, SCHEME_V->args, SCHEME_V->code); |
|
|
3643 | SCHEME_V->code = m; |
|
|
3644 | SCHEME_V->args = cons (x, NIL); |
|
|
3645 | Eval_Cycle (SCHEME_A_ OP_APPLY); |
|
|
3646 | x = SCHEME_V->value; |
|
|
3647 | compile_expr (SCHEME_A_ s, SCHEME_V->value); |
|
|
3648 | return; |
|
|
3649 | } |
|
|
3650 | |
|
|
3651 | stream_put (s, BOP_LIST_BEG); |
|
|
3652 | |
|
|
3653 | for (; x != NIL; x = cdr (x)) |
|
|
3654 | compile_expr (SCHEME_A_ s, car (x)); |
|
|
3655 | |
|
|
3656 | stream_put (s, BOP_LIST_END); |
|
|
3657 | return; |
|
|
3658 | } |
|
|
3659 | |
|
|
3660 | switch (type (x)) |
|
|
3661 | { |
|
|
3662 | case T_INTEGER: |
|
|
3663 | { |
|
|
3664 | IVALUE iv = ivalue_unchecked (x); |
|
|
3665 | iv = iv < 0 ? ((~(uint32_t)iv) << 1) | 1 : (uint32_t)iv << 1; |
|
|
3666 | stream_put_tv (s, BOP_INTEGER, iv); |
|
|
3667 | } |
|
|
3668 | return; |
|
|
3669 | |
|
|
3670 | case T_SYMBOL: |
|
|
3671 | if (0) |
|
|
3672 | { |
|
|
3673 | // no can do without more analysis |
|
|
3674 | pointer m = lookup (SCHEME_A_ x); |
|
|
3675 | |
|
|
3676 | if (is_proc (m)) |
|
|
3677 | { |
|
|
3678 | printf ("compile proc %s %d\n", procname(m), procnum(m)); |
|
|
3679 | stream_put_tv (s, BOP_SYMBOL, BOP_OP + procnum (m)); |
|
|
3680 | } |
|
|
3681 | else |
|
|
3682 | stream_put_tv (s, BOP_SYMBOL, symbol_id (SCHEME_A_ x)); |
|
|
3683 | } |
|
|
3684 | |
|
|
3685 | stream_put_tv (s, BOP_SYMBOL, symbol_id (SCHEME_A_ x)); |
|
|
3686 | return; |
|
|
3687 | |
|
|
3688 | default: |
|
|
3689 | stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x)); |
|
|
3690 | break; |
|
|
3691 | } |
|
|
3692 | } |
|
|
3693 | |
|
|
3694 | ecb_cold static int |
|
|
3695 | compile_closure (SCHEME_P_ pointer p) |
|
|
3696 | { |
|
|
3697 | stream s = stream_init (); |
|
|
3698 | |
|
|
3699 | compile_list (SCHEME_A_ s, cdar (p)); |
|
|
3700 | |
|
|
3701 | FILE *xxd = popen ("xxd", "we"); |
|
|
3702 | fwrite (stream_data (s), 1, stream_size (s), xxd); |
|
|
3703 | fclose (xxd); |
|
|
3704 | |
|
|
3705 | return stream_size (s); |
|
|
3706 | } |
|
|
3707 | |
|
|
3708 | #endif |
|
|
3709 | |
|
|
3710 | /* syntax, eval, core, ... */ |
|
|
3711 | ecb_hot static int |
3332 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3712 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3333 | { |
3713 | { |
3334 | pointer args = SCHEME_V->args; |
3714 | pointer args = SCHEME_V->args; |
3335 | pointer x, y; |
3715 | pointer x, y; |
3336 | |
3716 | |
3337 | switch (op) |
3717 | switch (op) |
3338 | { |
3718 | { |
3339 | #if EXPERIMENT //D |
3719 | #if EXPERIMENT //D |
3340 | case OP_DEBUG: |
3720 | case OP_DEBUG: |
3341 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
3721 | { |
|
|
3722 | uint32_t len = compile_closure (SCHEME_A_ car (args)); |
|
|
3723 | printf ("len = %d\n", len); |
3342 | printf ("\n"); |
3724 | printf ("\n"); |
3343 | s_return (S_T); |
3725 | s_return (S_T); |
|
|
3726 | } |
|
|
3727 | |
|
|
3728 | case OP_DEBUG2: |
|
|
3729 | return -1; |
3344 | #endif |
3730 | #endif |
|
|
3731 | |
3345 | case OP_LOAD: /* load */ |
3732 | case OP_LOAD: /* load */ |
3346 | if (file_interactive (SCHEME_A)) |
3733 | if (file_interactive (SCHEME_A)) |
3347 | { |
3734 | { |
3348 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3735 | putstr (SCHEME_A_ "Loading "); |
3349 | //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3736 | putstr (SCHEME_A_ strvalue (car (args))); |
|
|
3737 | putcharacter (SCHEME_A_ '\n'); |
3350 | } |
3738 | } |
3351 | |
3739 | |
3352 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3740 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3353 | Error_1 ("unable to open", car (args)); |
3741 | Error_1 ("unable to open", car (args)); |
3354 | else |
3742 | |
3355 | { |
|
|
3356 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3743 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3357 | s_goto (OP_T0LVL); |
3744 | s_goto (OP_T0LVL); |
3358 | } |
|
|
3359 | |
3745 | |
3360 | case OP_T0LVL: /* top level */ |
3746 | case OP_T0LVL: /* top level */ |
3361 | |
3747 | |
3362 | /* If we reached the end of file, this loop is done. */ |
3748 | /* If we reached the end of file, this loop is done. */ |
3363 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3749 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
… | |
… | |
3379 | /* If interactive, be nice to user. */ |
3765 | /* If interactive, be nice to user. */ |
3380 | if (file_interactive (SCHEME_A)) |
3766 | if (file_interactive (SCHEME_A)) |
3381 | { |
3767 | { |
3382 | SCHEME_V->envir = SCHEME_V->global_env; |
3768 | SCHEME_V->envir = SCHEME_V->global_env; |
3383 | dump_stack_reset (SCHEME_A); |
3769 | dump_stack_reset (SCHEME_A); |
3384 | putstr (SCHEME_A_ "\n"); |
3770 | putcharacter (SCHEME_A_ '\n'); |
|
|
3771 | #if EXPERIMENT |
|
|
3772 | system ("ps v $PPID"); |
|
|
3773 | #endif |
3385 | putstr (SCHEME_A_ prompt); |
3774 | putstr (SCHEME_A_ prompt); |
3386 | } |
3775 | } |
3387 | |
3776 | |
3388 | /* Set up another iteration of REPL */ |
3777 | /* Set up another iteration of REPL */ |
3389 | SCHEME_V->nesting = 0; |
3778 | SCHEME_V->nesting = 0; |
… | |
… | |
3424 | { |
3813 | { |
3425 | SCHEME_V->print_flag = 1; |
3814 | SCHEME_V->print_flag = 1; |
3426 | SCHEME_V->args = SCHEME_V->value; |
3815 | SCHEME_V->args = SCHEME_V->value; |
3427 | s_goto (OP_P0LIST); |
3816 | s_goto (OP_P0LIST); |
3428 | } |
3817 | } |
3429 | else |
3818 | |
3430 | s_return (SCHEME_V->value); |
3819 | s_return (SCHEME_V->value); |
3431 | |
3820 | |
3432 | case OP_EVAL: /* main part of evaluation */ |
3821 | case OP_EVAL: /* main part of evaluation */ |
3433 | #if USE_TRACING |
3822 | #if USE_TRACING |
3434 | if (SCHEME_V->tracing) |
3823 | if (SCHEME_V->tracing) |
3435 | { |
3824 | { |
… | |
… | |
3468 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3857 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3469 | SCHEME_V->code = x; |
3858 | SCHEME_V->code = x; |
3470 | s_goto (OP_EVAL); |
3859 | s_goto (OP_EVAL); |
3471 | } |
3860 | } |
3472 | } |
3861 | } |
3473 | else |
3862 | |
3474 | s_return (SCHEME_V->code); |
3863 | s_return (SCHEME_V->code); |
3475 | |
3864 | |
3476 | case OP_E0ARGS: /* eval arguments */ |
3865 | case OP_E0ARGS: /* eval arguments */ |
3477 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3866 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3478 | { |
3867 | { |
3479 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3868 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3480 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3869 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3481 | SCHEME_V->code = SCHEME_V->value; |
3870 | SCHEME_V->code = SCHEME_V->value; |
3482 | s_goto (OP_APPLY); |
3871 | s_goto (OP_APPLY); |
3483 | } |
3872 | } |
3484 | else |
3873 | |
3485 | { |
|
|
3486 | SCHEME_V->code = cdr (SCHEME_V->code); |
3874 | SCHEME_V->code = cdr (SCHEME_V->code); |
3487 | s_goto (OP_E1ARGS); |
3875 | s_goto (OP_E1ARGS); |
3488 | } |
|
|
3489 | |
3876 | |
3490 | case OP_E1ARGS: /* eval arguments */ |
3877 | case OP_E1ARGS: /* eval arguments */ |
3491 | args = cons (SCHEME_V->value, args); |
3878 | args = cons (SCHEME_V->value, args); |
3492 | |
3879 | |
3493 | if (is_pair (SCHEME_V->code)) /* continue */ |
3880 | if (is_pair (SCHEME_V->code)) /* continue */ |
… | |
… | |
3504 | SCHEME_V->args = cdr (args); |
3891 | SCHEME_V->args = cdr (args); |
3505 | s_goto (OP_APPLY); |
3892 | s_goto (OP_APPLY); |
3506 | } |
3893 | } |
3507 | |
3894 | |
3508 | #if USE_TRACING |
3895 | #if USE_TRACING |
3509 | |
|
|
3510 | case OP_TRACING: |
3896 | case OP_TRACING: |
3511 | { |
3897 | { |
3512 | int tr = SCHEME_V->tracing; |
3898 | int tr = SCHEME_V->tracing; |
3513 | |
3899 | |
3514 | SCHEME_V->tracing = ivalue_unchecked (car (args)); |
3900 | SCHEME_V->tracing = ivalue_unchecked (car (args)); |
3515 | s_return (mk_integer (SCHEME_A_ tr)); |
3901 | s_return (mk_integer (SCHEME_A_ tr)); |
3516 | } |
3902 | } |
3517 | |
|
|
3518 | #endif |
3903 | #endif |
3519 | |
3904 | |
3520 | case OP_APPLY: /* apply 'code' to 'args' */ |
3905 | case OP_APPLY: /* apply 'code' to 'args' */ |
3521 | #if USE_TRACING |
3906 | #if USE_TRACING |
3522 | if (SCHEME_V->tracing) |
3907 | if (SCHEME_V->tracing) |
… | |
… | |
3576 | else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ |
3961 | else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ |
3577 | { |
3962 | { |
3578 | ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); |
3963 | ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); |
3579 | s_return (args != NIL ? car (args) : NIL); |
3964 | s_return (args != NIL ? car (args) : NIL); |
3580 | } |
3965 | } |
3581 | else |
3966 | |
3582 | Error_0 ("illegal function"); |
3967 | Error_0 ("illegal function"); |
3583 | |
3968 | |
3584 | case OP_DOMACRO: /* do macro */ |
3969 | case OP_DOMACRO: /* do macro */ |
3585 | SCHEME_V->code = SCHEME_V->value; |
3970 | SCHEME_V->code = SCHEME_V->value; |
3586 | s_goto (OP_EVAL); |
3971 | s_goto (OP_EVAL); |
3587 | |
3972 | |
… | |
… | |
3651 | else |
4036 | else |
3652 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
4037 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3653 | |
4038 | |
3654 | s_return (SCHEME_V->code); |
4039 | s_return (SCHEME_V->code); |
3655 | |
4040 | |
3656 | |
|
|
3657 | case OP_DEFP: /* defined? */ |
4041 | case OP_DEFP: /* defined? */ |
3658 | x = SCHEME_V->envir; |
4042 | x = SCHEME_V->envir; |
3659 | |
4043 | |
3660 | if (cdr (args) != NIL) |
4044 | if (cdr (args) != NIL) |
3661 | x = cadr (args); |
4045 | x = cadr (args); |
… | |
… | |
3679 | s_return (SCHEME_V->value); |
4063 | s_return (SCHEME_V->value); |
3680 | } |
4064 | } |
3681 | else |
4065 | else |
3682 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
4066 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3683 | |
4067 | |
3684 | |
|
|
3685 | case OP_BEGIN: /* begin */ |
4068 | case OP_BEGIN: /* begin */ |
3686 | if (!is_pair (SCHEME_V->code)) |
4069 | if (!is_pair (SCHEME_V->code)) |
3687 | s_return (SCHEME_V->code); |
4070 | s_return (SCHEME_V->code); |
3688 | |
4071 | |
3689 | if (cdr (SCHEME_V->code) != NIL) |
4072 | if (cdr (SCHEME_V->code) != NIL) |
… | |
… | |
3700 | case OP_IF1: /* if */ |
4083 | case OP_IF1: /* if */ |
3701 | if (is_true (SCHEME_V->value)) |
4084 | if (is_true (SCHEME_V->value)) |
3702 | SCHEME_V->code = car (SCHEME_V->code); |
4085 | SCHEME_V->code = car (SCHEME_V->code); |
3703 | else |
4086 | else |
3704 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
4087 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
|
|
4088 | |
3705 | s_goto (OP_EVAL); |
4089 | s_goto (OP_EVAL); |
3706 | |
4090 | |
3707 | case OP_LET0: /* let */ |
4091 | case OP_LET0: /* let */ |
3708 | SCHEME_V->args = NIL; |
4092 | SCHEME_V->args = NIL; |
3709 | SCHEME_V->value = SCHEME_V->code; |
4093 | SCHEME_V->value = SCHEME_V->code; |
3710 | SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); |
4094 | SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); |
3711 | s_goto (OP_LET1); |
4095 | s_goto (OP_LET1); |
3712 | |
4096 | |
3713 | case OP_LET1: /* let (calculate parameters) */ |
4097 | case OP_LET1: /* let (calculate parameters) */ |
|
|
4098 | case OP_LET1REC: /* letrec (calculate parameters) */ |
3714 | args = cons (SCHEME_V->value, args); |
4099 | args = cons (SCHEME_V->value, args); |
3715 | |
4100 | |
3716 | if (is_pair (SCHEME_V->code)) /* continue */ |
4101 | if (is_pair (SCHEME_V->code)) /* continue */ |
3717 | { |
4102 | { |
3718 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
4103 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3719 | Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); |
4104 | Error_1 ("Bad syntax of binding spec in let/letrec:", car (SCHEME_V->code)); |
3720 | |
4105 | |
3721 | s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code)); |
4106 | s_save (SCHEME_A_ op, args, cdr (SCHEME_V->code)); |
3722 | SCHEME_V->code = cadar (SCHEME_V->code); |
4107 | SCHEME_V->code = cadar (SCHEME_V->code); |
3723 | SCHEME_V->args = NIL; |
4108 | SCHEME_V->args = NIL; |
3724 | s_goto (OP_EVAL); |
4109 | s_goto (OP_EVAL); |
3725 | } |
4110 | } |
3726 | else /* end */ |
4111 | |
3727 | { |
4112 | /* end */ |
3728 | args = reverse_in_place (SCHEME_A_ NIL, args); |
4113 | args = reverse_in_place (SCHEME_A_ NIL, args); |
3729 | SCHEME_V->code = car (args); |
4114 | SCHEME_V->code = car (args); |
3730 | SCHEME_V->args = cdr (args); |
4115 | SCHEME_V->args = cdr (args); |
3731 | s_goto (OP_LET2); |
4116 | s_goto (op == OP_LET1 ? OP_LET2 : OP_LET2REC); |
3732 | } |
|
|
3733 | |
4117 | |
3734 | case OP_LET2: /* let */ |
4118 | case OP_LET2: /* let */ |
3735 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
4119 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3736 | |
4120 | |
3737 | for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args; |
4121 | for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args; |
… | |
… | |
3741 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
4125 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
3742 | { |
4126 | { |
3743 | for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x)) |
4127 | for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x)) |
3744 | { |
4128 | { |
3745 | if (!is_pair (x)) |
4129 | if (!is_pair (x)) |
3746 | Error_1 ("Bad syntax of binding in let :", x); |
4130 | Error_1 ("Bad syntax of binding in let:", x); |
3747 | |
4131 | |
3748 | if (!is_list (SCHEME_A_ car (x))) |
4132 | if (!is_list (SCHEME_A_ car (x))) |
3749 | Error_1 ("Bad syntax of binding in let :", car (x)); |
4133 | Error_1 ("Bad syntax of binding in let:", car (x)); |
3750 | |
4134 | |
3751 | args = cons (caar (x), args); |
4135 | args = cons (caar (x), args); |
3752 | } |
4136 | } |
3753 | |
4137 | |
3754 | x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)), |
4138 | x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)), |
… | |
… | |
3771 | SCHEME_V->code = cdr (SCHEME_V->code); |
4155 | SCHEME_V->code = cdr (SCHEME_V->code); |
3772 | s_goto (OP_BEGIN); |
4156 | s_goto (OP_BEGIN); |
3773 | } |
4157 | } |
3774 | |
4158 | |
3775 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code))) |
4159 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code))) |
3776 | Error_1 ("Bad syntax of binding spec in let* :", car (SCHEME_V->code)); |
4160 | Error_1 ("Bad syntax of binding spec in let*:", car (SCHEME_V->code)); |
3777 | |
4161 | |
3778 | s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code)); |
4162 | s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code)); |
3779 | SCHEME_V->code = car (cdaar (SCHEME_V->code)); |
4163 | SCHEME_V->code = car (cdaar (SCHEME_V->code)); |
3780 | s_goto (OP_EVAL); |
4164 | s_goto (OP_EVAL); |
3781 | |
4165 | |
… | |
… | |
3792 | s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); |
4176 | s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); |
3793 | SCHEME_V->code = cadar (SCHEME_V->code); |
4177 | SCHEME_V->code = cadar (SCHEME_V->code); |
3794 | SCHEME_V->args = NIL; |
4178 | SCHEME_V->args = NIL; |
3795 | s_goto (OP_EVAL); |
4179 | s_goto (OP_EVAL); |
3796 | } |
4180 | } |
3797 | else /* end */ |
4181 | |
|
|
4182 | /* end */ |
3798 | { |
4183 | |
3799 | SCHEME_V->code = args; |
4184 | SCHEME_V->code = args; |
3800 | SCHEME_V->args = NIL; |
4185 | SCHEME_V->args = NIL; |
3801 | s_goto (OP_BEGIN); |
4186 | s_goto (OP_BEGIN); |
3802 | } |
|
|
3803 | |
4187 | |
3804 | case OP_LET0REC: /* letrec */ |
4188 | case OP_LET0REC: /* letrec */ |
3805 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
4189 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3806 | SCHEME_V->args = NIL; |
4190 | SCHEME_V->args = NIL; |
3807 | SCHEME_V->value = SCHEME_V->code; |
4191 | SCHEME_V->value = SCHEME_V->code; |
3808 | SCHEME_V->code = car (SCHEME_V->code); |
4192 | SCHEME_V->code = car (SCHEME_V->code); |
3809 | s_goto (OP_LET1REC); |
4193 | s_goto (OP_LET1REC); |
3810 | |
4194 | |
3811 | case OP_LET1REC: /* letrec (calculate parameters) */ |
4195 | /* OP_LET1REC handled by OP_LET1 */ |
3812 | args = cons (SCHEME_V->value, args); |
|
|
3813 | |
|
|
3814 | if (is_pair (SCHEME_V->code)) /* continue */ |
|
|
3815 | { |
|
|
3816 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
|
|
3817 | Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); |
|
|
3818 | |
|
|
3819 | s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code)); |
|
|
3820 | SCHEME_V->code = cadar (SCHEME_V->code); |
|
|
3821 | SCHEME_V->args = NIL; |
|
|
3822 | s_goto (OP_EVAL); |
|
|
3823 | } |
|
|
3824 | else /* end */ |
|
|
3825 | { |
|
|
3826 | args = reverse_in_place (SCHEME_A_ NIL, args); |
|
|
3827 | SCHEME_V->code = car (args); |
|
|
3828 | SCHEME_V->args = cdr (args); |
|
|
3829 | s_goto (OP_LET2REC); |
|
|
3830 | } |
|
|
3831 | |
4196 | |
3832 | case OP_LET2REC: /* letrec */ |
4197 | case OP_LET2REC: /* letrec */ |
3833 | for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y)) |
4198 | for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y)) |
3834 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
4199 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3835 | |
4200 | |
… | |
… | |
3865 | } |
4230 | } |
3866 | else |
4231 | else |
3867 | { |
4232 | { |
3868 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
4233 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3869 | s_return (NIL); |
4234 | s_return (NIL); |
3870 | else |
4235 | |
3871 | { |
|
|
3872 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
4236 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3873 | SCHEME_V->code = caar (SCHEME_V->code); |
4237 | SCHEME_V->code = caar (SCHEME_V->code); |
3874 | s_goto (OP_EVAL); |
4238 | s_goto (OP_EVAL); |
3875 | } |
|
|
3876 | } |
4239 | } |
3877 | |
4240 | |
3878 | case OP_DELAY: /* delay */ |
4241 | case OP_DELAY: /* delay */ |
3879 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
4242 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3880 | set_typeflag (x, T_PROMISE); |
4243 | set_typeflag (x, T_PROMISE); |
… | |
… | |
3891 | case OP_AND1: /* and */ |
4254 | case OP_AND1: /* and */ |
3892 | if (is_false (SCHEME_V->value)) |
4255 | if (is_false (SCHEME_V->value)) |
3893 | s_return (SCHEME_V->value); |
4256 | s_return (SCHEME_V->value); |
3894 | else if (SCHEME_V->code == NIL) |
4257 | else if (SCHEME_V->code == NIL) |
3895 | s_return (SCHEME_V->value); |
4258 | s_return (SCHEME_V->value); |
3896 | else |
4259 | |
3897 | { |
|
|
3898 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
4260 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3899 | SCHEME_V->code = car (SCHEME_V->code); |
4261 | SCHEME_V->code = car (SCHEME_V->code); |
3900 | s_goto (OP_EVAL); |
4262 | s_goto (OP_EVAL); |
3901 | } |
|
|
3902 | |
4263 | |
3903 | case OP_OR0: /* or */ |
4264 | case OP_OR0: /* or */ |
3904 | if (SCHEME_V->code == NIL) |
4265 | if (SCHEME_V->code == NIL) |
3905 | s_return (S_F); |
4266 | s_return (S_F); |
3906 | |
4267 | |
… | |
… | |
3911 | case OP_OR1: /* or */ |
4272 | case OP_OR1: /* or */ |
3912 | if (is_true (SCHEME_V->value)) |
4273 | if (is_true (SCHEME_V->value)) |
3913 | s_return (SCHEME_V->value); |
4274 | s_return (SCHEME_V->value); |
3914 | else if (SCHEME_V->code == NIL) |
4275 | else if (SCHEME_V->code == NIL) |
3915 | s_return (SCHEME_V->value); |
4276 | s_return (SCHEME_V->value); |
3916 | else |
4277 | |
3917 | { |
|
|
3918 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
4278 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3919 | SCHEME_V->code = car (SCHEME_V->code); |
4279 | SCHEME_V->code = car (SCHEME_V->code); |
3920 | s_goto (OP_EVAL); |
4280 | s_goto (OP_EVAL); |
3921 | } |
|
|
3922 | |
4281 | |
3923 | case OP_C0STREAM: /* cons-stream */ |
4282 | case OP_C0STREAM: /* cons-stream */ |
3924 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
4283 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3925 | SCHEME_V->code = car (SCHEME_V->code); |
4284 | SCHEME_V->code = car (SCHEME_V->code); |
3926 | s_goto (OP_EVAL); |
4285 | s_goto (OP_EVAL); |
… | |
… | |
3991 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
4350 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
3992 | SCHEME_V->code = caar (x); |
4351 | SCHEME_V->code = caar (x); |
3993 | s_goto (OP_EVAL); |
4352 | s_goto (OP_EVAL); |
3994 | } |
4353 | } |
3995 | } |
4354 | } |
3996 | else |
4355 | |
3997 | s_return (NIL); |
4356 | s_return (NIL); |
3998 | |
4357 | |
3999 | case OP_CASE2: /* case */ |
4358 | case OP_CASE2: /* case */ |
4000 | if (is_true (SCHEME_V->value)) |
4359 | if (is_true (SCHEME_V->value)) |
4001 | s_goto (OP_BEGIN); |
4360 | s_goto (OP_BEGIN); |
4002 | else |
4361 | |
4003 | s_return (NIL); |
4362 | s_return (NIL); |
4004 | |
4363 | |
4005 | case OP_PAPPLY: /* apply */ |
4364 | case OP_PAPPLY: /* apply */ |
4006 | SCHEME_V->code = car (args); |
4365 | SCHEME_V->code = car (args); |
4007 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
4366 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
4008 | /*SCHEME_V->args = cadr(args); */ |
4367 | /*SCHEME_V->args = cadr(args); */ |
… | |
… | |
4022 | } |
4381 | } |
4023 | |
4382 | |
4024 | if (USE_ERROR_CHECKING) abort (); |
4383 | if (USE_ERROR_CHECKING) abort (); |
4025 | } |
4384 | } |
4026 | |
4385 | |
4027 | static int |
4386 | /* math, cxr */ |
|
|
4387 | ecb_hot static int |
4028 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4388 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4029 | { |
4389 | { |
4030 | pointer args = SCHEME_V->args; |
4390 | pointer args = SCHEME_V->args; |
4031 | pointer x = car (args); |
4391 | pointer x = car (args); |
4032 | num v; |
4392 | num v; |
4033 | |
4393 | |
4034 | switch (op) |
4394 | switch (op) |
4035 | { |
4395 | { |
4036 | #if USE_MATH |
4396 | #if USE_MATH |
4037 | case OP_INEX2EX: /* inexact->exact */ |
4397 | case OP_INEX2EX: /* inexact->exact */ |
4038 | { |
|
|
4039 | if (is_integer (x)) |
4398 | if (!is_integer (x)) |
4040 | s_return (x); |
4399 | { |
4041 | |
|
|
4042 | RVALUE r = rvalue_unchecked (x); |
4400 | RVALUE r = rvalue_unchecked (x); |
4043 | |
4401 | |
4044 | if (r == (RVALUE)(IVALUE)r) |
4402 | if (r == (RVALUE)(IVALUE)r) |
4045 | s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); |
4403 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
4046 | else |
4404 | else |
4047 | Error_1 ("inexact->exact: not integral:", x); |
4405 | Error_1 ("inexact->exact: not integral:", x); |
4048 | } |
4406 | } |
4049 | |
4407 | |
|
|
4408 | s_return (x); |
|
|
4409 | |
|
|
4410 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4411 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4412 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4413 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
|
|
4414 | |
|
|
4415 | case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
4050 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4416 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4051 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
4417 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
|
|
4418 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
4052 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4419 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4053 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4420 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4054 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4421 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4055 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4422 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4056 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4423 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4057 | |
4424 | |
4058 | case OP_ATAN: |
4425 | case OP_ATAN: |
|
|
4426 | s_return (mk_real (SCHEME_A_ |
4059 | if (cdr (args) == NIL) |
4427 | cdr (args) == NIL |
4060 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4428 | ? atan (rvalue (x)) |
4061 | else |
4429 | : atan2 (rvalue (x), rvalue (cadr (args))))); |
4062 | { |
|
|
4063 | pointer y = cadr (args); |
|
|
4064 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
|
|
4065 | } |
|
|
4066 | |
|
|
4067 | case OP_SQRT: |
|
|
4068 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
|
|
4069 | |
4430 | |
4070 | case OP_EXPT: |
4431 | case OP_EXPT: |
4071 | { |
4432 | { |
4072 | RVALUE result; |
4433 | RVALUE result; |
4073 | int real_result = 1; |
4434 | int real_result = 1; |
… | |
… | |
4096 | if (real_result) |
4457 | if (real_result) |
4097 | s_return (mk_real (SCHEME_A_ result)); |
4458 | s_return (mk_real (SCHEME_A_ result)); |
4098 | else |
4459 | else |
4099 | s_return (mk_integer (SCHEME_A_ result)); |
4460 | s_return (mk_integer (SCHEME_A_ result)); |
4100 | } |
4461 | } |
4101 | |
|
|
4102 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4103 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4104 | |
|
|
4105 | case OP_TRUNCATE: |
|
|
4106 | { |
|
|
4107 | RVALUE n = rvalue (x); |
|
|
4108 | s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n))); |
|
|
4109 | } |
|
|
4110 | |
|
|
4111 | case OP_ROUND: |
|
|
4112 | if (is_integer (x)) |
|
|
4113 | s_return (x); |
|
|
4114 | |
|
|
4115 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
|
|
4116 | #endif |
4462 | #endif |
4117 | |
4463 | |
4118 | case OP_ADD: /* + */ |
4464 | case OP_ADD: /* + */ |
4119 | v = num_zero; |
4465 | v = num_zero; |
4120 | |
4466 | |
… | |
… | |
4422 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4768 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4423 | |
4769 | |
4424 | s_return (newstr); |
4770 | s_return (newstr); |
4425 | } |
4771 | } |
4426 | |
4772 | |
4427 | case OP_SUBSTR: /* substring */ |
4773 | case OP_STRING_COPY: /* substring/string-copy */ |
4428 | { |
4774 | { |
4429 | char *str = strvalue (x); |
4775 | char *str = strvalue (x); |
4430 | int index0 = ivalue_unchecked (cadr (args)); |
4776 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4431 | int index1; |
4777 | int index1; |
4432 | int len; |
4778 | int len; |
4433 | |
4779 | |
4434 | if (index0 > strlength (x)) |
4780 | if (index0 > strlength (x)) |
4435 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4781 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4436 | |
4782 | |
4437 | if (cddr (args) != NIL) |
4783 | if (cddr (args) != NIL) |
4438 | { |
4784 | { |
4439 | index1 = ivalue_unchecked (caddr (args)); |
4785 | index1 = ivalue_unchecked (caddr (args)); |
4440 | |
4786 | |
4441 | if (index1 > strlength (x) || index1 < index0) |
4787 | if (index1 > strlength (x) || index1 < index0) |
4442 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4788 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4443 | } |
4789 | } |
4444 | else |
4790 | else |
4445 | index1 = strlength (x); |
4791 | index1 = strlength (x); |
4446 | |
4792 | |
4447 | len = index1 - index0; |
4793 | len = index1 - index0; |
4448 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4794 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4449 | memcpy (strvalue (x), str + index0, len); |
|
|
4450 | strvalue (x)[len] = 0; |
|
|
4451 | |
4795 | |
4452 | s_return (x); |
4796 | s_return (x); |
4453 | } |
4797 | } |
4454 | |
4798 | |
4455 | case OP_VECTOR: /* vector */ |
4799 | case OP_VECTOR: /* vector */ |
… | |
… | |
4529 | } |
4873 | } |
4530 | |
4874 | |
4531 | if (USE_ERROR_CHECKING) abort (); |
4875 | if (USE_ERROR_CHECKING) abort (); |
4532 | } |
4876 | } |
4533 | |
4877 | |
4534 | static int |
4878 | /* relational ops */ |
|
|
4879 | ecb_hot static int |
4535 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4880 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4536 | { |
4881 | { |
4537 | pointer x = SCHEME_V->args; |
4882 | pointer x = SCHEME_V->args; |
4538 | |
4883 | |
4539 | for (;;) |
4884 | for (;;) |
… | |
… | |
4560 | } |
4905 | } |
4561 | |
4906 | |
4562 | s_return (S_T); |
4907 | s_return (S_T); |
4563 | } |
4908 | } |
4564 | |
4909 | |
4565 | static int |
4910 | /* predicates */ |
|
|
4911 | ecb_hot static int |
4566 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4912 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4567 | { |
4913 | { |
4568 | pointer args = SCHEME_V->args; |
4914 | pointer args = SCHEME_V->args; |
4569 | pointer a = car (args); |
4915 | pointer a = car (args); |
4570 | pointer d = cdr (args); |
4916 | pointer d = cdr (args); |
… | |
… | |
4617 | } |
4963 | } |
4618 | |
4964 | |
4619 | s_retbool (r); |
4965 | s_retbool (r); |
4620 | } |
4966 | } |
4621 | |
4967 | |
4622 | static int |
4968 | /* promises, list ops, ports */ |
|
|
4969 | ecb_hot static int |
4623 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4970 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4624 | { |
4971 | { |
4625 | pointer args = SCHEME_V->args; |
4972 | pointer args = SCHEME_V->args; |
4626 | pointer a = car (args); |
4973 | pointer a = car (args); |
4627 | pointer x, y; |
4974 | pointer x, y; |
… | |
… | |
4644 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4991 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4645 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4992 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4646 | s_return (SCHEME_V->value); |
4993 | s_return (SCHEME_V->value); |
4647 | |
4994 | |
4648 | #if USE_PORTS |
4995 | #if USE_PORTS |
|
|
4996 | |
|
|
4997 | case OP_EOF_OBJECT: /* eof-object */ |
|
|
4998 | s_return (S_EOF); |
4649 | |
4999 | |
4650 | case OP_WRITE: /* write */ |
5000 | case OP_WRITE: /* write */ |
4651 | case OP_DISPLAY: /* display */ |
5001 | case OP_DISPLAY: /* display */ |
4652 | case OP_WRITE_CHAR: /* write-char */ |
5002 | case OP_WRITE_CHAR: /* write-char */ |
4653 | if (is_pair (cdr (SCHEME_V->args))) |
5003 | if (is_pair (cdr (SCHEME_V->args))) |
… | |
… | |
4667 | else |
5017 | else |
4668 | SCHEME_V->print_flag = 0; |
5018 | SCHEME_V->print_flag = 0; |
4669 | |
5019 | |
4670 | s_goto (OP_P0LIST); |
5020 | s_goto (OP_P0LIST); |
4671 | |
5021 | |
|
|
5022 | //TODO: move to scheme |
4672 | case OP_NEWLINE: /* newline */ |
5023 | case OP_NEWLINE: /* newline */ |
4673 | if (is_pair (args)) |
5024 | if (is_pair (args)) |
4674 | { |
5025 | { |
4675 | if (a != SCHEME_V->outport) |
5026 | if (a != SCHEME_V->outport) |
4676 | { |
5027 | { |
… | |
… | |
4678 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
5029 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4679 | SCHEME_V->outport = a; |
5030 | SCHEME_V->outport = a; |
4680 | } |
5031 | } |
4681 | } |
5032 | } |
4682 | |
5033 | |
4683 | putstr (SCHEME_A_ "\n"); |
5034 | putcharacter (SCHEME_A_ '\n'); |
4684 | s_return (S_T); |
5035 | s_return (S_T); |
4685 | #endif |
5036 | #endif |
4686 | |
5037 | |
4687 | case OP_ERR0: /* error */ |
5038 | case OP_ERR0: /* error */ |
4688 | SCHEME_V->retcode = -1; |
5039 | SCHEME_V->retcode = -1; |
… | |
… | |
4697 | putstr (SCHEME_A_ strvalue (car (args))); |
5048 | putstr (SCHEME_A_ strvalue (car (args))); |
4698 | SCHEME_V->args = cdr (args); |
5049 | SCHEME_V->args = cdr (args); |
4699 | s_goto (OP_ERR1); |
5050 | s_goto (OP_ERR1); |
4700 | |
5051 | |
4701 | case OP_ERR1: /* error */ |
5052 | case OP_ERR1: /* error */ |
4702 | putstr (SCHEME_A_ " "); |
5053 | putcharacter (SCHEME_A_ ' '); |
4703 | |
5054 | |
4704 | if (args != NIL) |
5055 | if (args != NIL) |
4705 | { |
5056 | { |
4706 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
5057 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4707 | SCHEME_V->args = a; |
5058 | SCHEME_V->args = a; |
4708 | SCHEME_V->print_flag = 1; |
5059 | SCHEME_V->print_flag = 1; |
4709 | s_goto (OP_P0LIST); |
5060 | s_goto (OP_P0LIST); |
4710 | } |
5061 | } |
4711 | else |
5062 | else |
4712 | { |
5063 | { |
4713 | putstr (SCHEME_A_ "\n"); |
5064 | putcharacter (SCHEME_A_ '\n'); |
4714 | |
5065 | |
4715 | if (SCHEME_V->interactive_repl) |
5066 | if (SCHEME_V->interactive_repl) |
4716 | s_goto (OP_T0LVL); |
5067 | s_goto (OP_T0LVL); |
4717 | else |
5068 | else |
4718 | return -1; |
5069 | return -1; |
… | |
… | |
4926 | } |
5277 | } |
4927 | |
5278 | |
4928 | if (USE_ERROR_CHECKING) abort (); |
5279 | if (USE_ERROR_CHECKING) abort (); |
4929 | } |
5280 | } |
4930 | |
5281 | |
4931 | static int |
5282 | /* reading */ |
|
|
5283 | ecb_cold static int |
4932 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
5284 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4933 | { |
5285 | { |
4934 | pointer args = SCHEME_V->args; |
5286 | pointer args = SCHEME_V->args; |
4935 | pointer x; |
5287 | pointer x; |
4936 | |
5288 | |
… | |
… | |
5015 | case OP_RDSEXPR: |
5367 | case OP_RDSEXPR: |
5016 | switch (SCHEME_V->tok) |
5368 | switch (SCHEME_V->tok) |
5017 | { |
5369 | { |
5018 | case TOK_EOF: |
5370 | case TOK_EOF: |
5019 | s_return (S_EOF); |
5371 | s_return (S_EOF); |
5020 | /* NOTREACHED */ |
|
|
5021 | |
5372 | |
5022 | case TOK_VEC: |
5373 | case TOK_VEC: |
5023 | s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); |
5374 | s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); |
5024 | /* fall through */ |
5375 | /* fall through */ |
5025 | |
5376 | |
… | |
… | |
5028 | |
5379 | |
5029 | if (SCHEME_V->tok == TOK_RPAREN) |
5380 | if (SCHEME_V->tok == TOK_RPAREN) |
5030 | s_return (NIL); |
5381 | s_return (NIL); |
5031 | else if (SCHEME_V->tok == TOK_DOT) |
5382 | else if (SCHEME_V->tok == TOK_DOT) |
5032 | Error_0 ("syntax error: illegal dot expression"); |
5383 | Error_0 ("syntax error: illegal dot expression"); |
5033 | else |
5384 | |
5034 | { |
|
|
5035 | SCHEME_V->nesting_stack[SCHEME_V->file_i]++; |
5385 | SCHEME_V->nesting_stack[SCHEME_V->file_i]++; |
5036 | s_save (SCHEME_A_ OP_RDLIST, NIL, NIL); |
5386 | s_save (SCHEME_A_ OP_RDLIST, NIL, NIL); |
5037 | s_goto (OP_RDSEXPR); |
5387 | s_goto (OP_RDSEXPR); |
5038 | } |
|
|
5039 | |
5388 | |
5040 | case TOK_QUOTE: |
5389 | case TOK_QUOTE: |
5041 | s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL); |
5390 | s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL); |
5042 | SCHEME_V->tok = token (SCHEME_A); |
5391 | SCHEME_V->tok = token (SCHEME_A); |
5043 | s_goto (OP_RDSEXPR); |
5392 | s_goto (OP_RDSEXPR); |
… | |
… | |
5049 | { |
5398 | { |
5050 | s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); |
5399 | s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); |
5051 | SCHEME_V->tok = TOK_LPAREN; |
5400 | SCHEME_V->tok = TOK_LPAREN; |
5052 | s_goto (OP_RDSEXPR); |
5401 | s_goto (OP_RDSEXPR); |
5053 | } |
5402 | } |
5054 | else |
5403 | |
5055 | s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); |
5404 | s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); |
5056 | |
|
|
5057 | s_goto (OP_RDSEXPR); |
5405 | s_goto (OP_RDSEXPR); |
5058 | |
5406 | |
5059 | case TOK_COMMA: |
5407 | case TOK_COMMA: |
5060 | s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); |
5408 | s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); |
5061 | SCHEME_V->tok = token (SCHEME_A); |
5409 | SCHEME_V->tok = token (SCHEME_A); |
… | |
… | |
5072 | case TOK_DOTATOM: |
5420 | case TOK_DOTATOM: |
5073 | SCHEME_V->strbuff[0] = '.'; |
5421 | SCHEME_V->strbuff[0] = '.'; |
5074 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
5422 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
5075 | |
5423 | |
5076 | case TOK_STRATOM: |
5424 | case TOK_STRATOM: |
|
|
5425 | //TODO: haven't checked whether the garbage collector could interfere and free x |
|
|
5426 | gc (SCHEME_A_ NIL, NIL); //TODO: superheavyhanded |
5077 | x = readstrexp (SCHEME_A_ '|'); |
5427 | x = readstrexp (SCHEME_A_ '|'); |
5078 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5079 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
5428 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
5080 | |
5429 | |
5081 | case TOK_DQUOTE: |
5430 | case TOK_DQUOTE: |
5082 | x = readstrexp (SCHEME_A_ '"'); |
5431 | x = readstrexp (SCHEME_A_ '"'); |
5083 | |
5432 | |
… | |
… | |
5091 | { |
5440 | { |
5092 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1); |
5441 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1); |
5093 | |
5442 | |
5094 | if (f == NIL) |
5443 | if (f == NIL) |
5095 | Error_0 ("undefined sharp expression"); |
5444 | Error_0 ("undefined sharp expression"); |
5096 | else |
5445 | |
5097 | { |
|
|
5098 | SCHEME_V->code = cons (slot_value_in_env (f), NIL); |
5446 | SCHEME_V->code = cons (slot_value_in_env (f), NIL); |
5099 | s_goto (OP_EVAL); |
5447 | s_goto (OP_EVAL); |
5100 | } |
|
|
5101 | } |
5448 | } |
5102 | |
5449 | |
5103 | case TOK_SHARP_CONST: |
5450 | case TOK_SHARP_CONST: |
5104 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
5451 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
5105 | Error_0 ("undefined sharp expression"); |
5452 | Error_0 ("undefined sharp expression"); |
5106 | else |
5453 | |
5107 | s_return (x); |
5454 | s_return (x); |
5108 | |
5455 | |
5109 | default: |
5456 | default: |
5110 | Error_0 ("syntax error: illegal token"); |
5457 | Error_0 ("syntax error: illegal token"); |
5111 | } |
5458 | } |
5112 | |
5459 | |
… | |
… | |
5205 | pointer b = cdr (args); |
5552 | pointer b = cdr (args); |
5206 | int ok_abbr = ok_abbrev (b); |
5553 | int ok_abbr = ok_abbrev (b); |
5207 | SCHEME_V->args = car (b); |
5554 | SCHEME_V->args = car (b); |
5208 | |
5555 | |
5209 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5556 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5210 | putstr (SCHEME_A_ "'"); |
5557 | putcharacter (SCHEME_A_ '\''); |
5211 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5558 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5212 | putstr (SCHEME_A_ "`"); |
5559 | putcharacter (SCHEME_A_ '`'); |
5213 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5560 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5214 | putstr (SCHEME_A_ ","); |
5561 | putcharacter (SCHEME_A_ ','); |
5215 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5562 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5216 | putstr (SCHEME_A_ ",@"); |
5563 | putstr (SCHEME_A_ ",@"); |
5217 | else |
5564 | else |
5218 | { |
5565 | { |
5219 | putstr (SCHEME_A_ "("); |
5566 | putcharacter (SCHEME_A_ '('); |
5220 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5567 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5221 | SCHEME_V->args = a; |
5568 | SCHEME_V->args = a; |
5222 | } |
5569 | } |
5223 | |
5570 | |
5224 | s_goto (OP_P0LIST); |
5571 | s_goto (OP_P0LIST); |
… | |
… | |
5226 | |
5573 | |
5227 | case OP_P1LIST: |
5574 | case OP_P1LIST: |
5228 | if (is_pair (args)) |
5575 | if (is_pair (args)) |
5229 | { |
5576 | { |
5230 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5577 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5231 | putstr (SCHEME_A_ " "); |
5578 | putcharacter (SCHEME_A_ ' '); |
5232 | SCHEME_V->args = car (args); |
5579 | SCHEME_V->args = car (args); |
5233 | s_goto (OP_P0LIST); |
5580 | s_goto (OP_P0LIST); |
5234 | } |
5581 | } |
5235 | else if (is_vector (args)) |
5582 | else if (is_vector (args)) |
5236 | { |
5583 | { |
… | |
… | |
5244 | { |
5591 | { |
5245 | putstr (SCHEME_A_ " . "); |
5592 | putstr (SCHEME_A_ " . "); |
5246 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5593 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5247 | } |
5594 | } |
5248 | |
5595 | |
5249 | putstr (SCHEME_A_ ")"); |
5596 | putcharacter (SCHEME_A_ ')'); |
5250 | s_return (S_T); |
5597 | s_return (S_T); |
5251 | } |
5598 | } |
5252 | |
5599 | |
5253 | case OP_PVECFROM: |
5600 | case OP_PVECFROM: |
5254 | { |
5601 | { |
… | |
… | |
5256 | pointer vec = car (args); |
5603 | pointer vec = car (args); |
5257 | int len = veclength (vec); |
5604 | int len = veclength (vec); |
5258 | |
5605 | |
5259 | if (i == len) |
5606 | if (i == len) |
5260 | { |
5607 | { |
5261 | putstr (SCHEME_A_ ")"); |
5608 | putcharacter (SCHEME_A_ ')'); |
5262 | s_return (S_T); |
5609 | s_return (S_T); |
5263 | } |
5610 | } |
5264 | else |
5611 | else |
5265 | { |
5612 | { |
5266 | pointer elem = vector_get (vec, i); |
5613 | pointer elem = vector_get (vec, i); |
… | |
… | |
5268 | ivalue_unchecked (cdr (args)) = i + 1; |
5615 | ivalue_unchecked (cdr (args)) = i + 1; |
5269 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5616 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5270 | SCHEME_V->args = elem; |
5617 | SCHEME_V->args = elem; |
5271 | |
5618 | |
5272 | if (i > 0) |
5619 | if (i > 0) |
5273 | putstr (SCHEME_A_ " "); |
5620 | putcharacter (SCHEME_A_ ' '); |
5274 | |
5621 | |
5275 | s_goto (OP_P0LIST); |
5622 | s_goto (OP_P0LIST); |
5276 | } |
5623 | } |
5277 | } |
5624 | } |
5278 | } |
5625 | } |
5279 | |
5626 | |
5280 | if (USE_ERROR_CHECKING) abort (); |
5627 | if (USE_ERROR_CHECKING) abort (); |
5281 | } |
5628 | } |
5282 | |
5629 | |
5283 | static int |
5630 | /* list ops */ |
|
|
5631 | ecb_hot static int |
5284 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5632 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5285 | { |
5633 | { |
5286 | pointer args = SCHEME_V->args; |
5634 | pointer args = SCHEME_V->args; |
5287 | pointer a = car (args); |
5635 | pointer a = car (args); |
5288 | pointer x, y; |
5636 | pointer x, y; |
… | |
… | |
5311 | break; |
5659 | break; |
5312 | } |
5660 | } |
5313 | |
5661 | |
5314 | if (is_pair (y)) |
5662 | if (is_pair (y)) |
5315 | s_return (car (y)); |
5663 | s_return (car (y)); |
5316 | else |
5664 | |
5317 | s_return (S_F); |
5665 | s_return (S_F); |
5318 | |
|
|
5319 | |
5666 | |
5320 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5667 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5321 | SCHEME_V->args = a; |
5668 | SCHEME_V->args = a; |
5322 | |
5669 | |
5323 | if (SCHEME_V->args == NIL) |
5670 | if (SCHEME_V->args == NIL) |
5324 | s_return (S_F); |
5671 | s_return (S_F); |
5325 | else if (is_closure (SCHEME_V->args)) |
5672 | else if (is_closure (SCHEME_V->args) || is_macro (SCHEME_V->args)) |
5326 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
5673 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
5327 | else if (is_macro (SCHEME_V->args)) |
5674 | |
5328 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
|
|
5329 | else |
|
|
5330 | s_return (S_F); |
5675 | s_return (S_F); |
5331 | |
5676 | |
5332 | case OP_CLOSUREP: /* closure? */ |
5677 | case OP_CLOSUREP: /* closure? */ |
5333 | /* |
5678 | /* |
5334 | * Note, macro object is also a closure. |
5679 | * Note, macro object is also a closure. |
5335 | * Therefore, (closure? <#MACRO>) ==> #t |
5680 | * Therefore, (closure? <#MACRO>) ==> #t |
… | |
… | |
5346 | |
5691 | |
5347 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5692 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5348 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5693 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5349 | |
5694 | |
5350 | typedef int (*test_predicate)(pointer); |
5695 | typedef int (*test_predicate)(pointer); |
5351 | static int |
5696 | |
|
|
5697 | ecb_hot static int |
5352 | tst_any (pointer p) |
5698 | tst_any (pointer p) |
5353 | { |
5699 | { |
5354 | return 1; |
5700 | return 1; |
5355 | } |
5701 | } |
5356 | |
5702 | |
5357 | static int |
5703 | ecb_hot static int |
5358 | tst_inonneg (pointer p) |
5704 | tst_inonneg (pointer p) |
5359 | { |
5705 | { |
5360 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5706 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5361 | } |
5707 | } |
5362 | |
5708 | |
5363 | static int |
5709 | ecb_hot static int |
5364 | tst_is_list (SCHEME_P_ pointer p) |
5710 | tst_is_list (SCHEME_P_ pointer p) |
5365 | { |
5711 | { |
5366 | return p == NIL || is_pair (p); |
5712 | return p == NIL || is_pair (p); |
5367 | } |
5713 | } |
5368 | |
5714 | |
… | |
… | |
5411 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5757 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5412 | #include "opdefines.h" |
5758 | #include "opdefines.h" |
5413 | #undef OP_DEF |
5759 | #undef OP_DEF |
5414 | ; |
5760 | ; |
5415 | |
5761 | |
5416 | static const char * |
5762 | ecb_cold static const char * |
5417 | opname (int idx) |
5763 | opname (int idx) |
5418 | { |
5764 | { |
5419 | const char *name = opnames; |
5765 | const char *name = opnames; |
5420 | |
5766 | |
5421 | /* should do this at compile time, but would require external program, right? */ |
5767 | /* should do this at compile time, but would require external program, right? */ |
… | |
… | |
5423 | name += strlen (name) + 1; |
5769 | name += strlen (name) + 1; |
5424 | |
5770 | |
5425 | return *name ? name : "ILLEGAL"; |
5771 | return *name ? name : "ILLEGAL"; |
5426 | } |
5772 | } |
5427 | |
5773 | |
5428 | static const char * |
5774 | ecb_cold static const char * |
5429 | procname (pointer x) |
5775 | procname (pointer x) |
5430 | { |
5776 | { |
5431 | return opname (procnum (x)); |
5777 | return opname (procnum (x)); |
5432 | } |
5778 | } |
5433 | |
5779 | |
… | |
… | |
5453 | #undef OP_DEF |
5799 | #undef OP_DEF |
5454 | {0} |
5800 | {0} |
5455 | }; |
5801 | }; |
5456 | |
5802 | |
5457 | /* kernel of this interpreter */ |
5803 | /* kernel of this interpreter */ |
5458 | static void ecb_hot |
5804 | ecb_hot static void |
5459 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5805 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5460 | { |
5806 | { |
5461 | SCHEME_V->op = op; |
5807 | SCHEME_V->op = op; |
5462 | |
5808 | |
5463 | for (;;) |
5809 | for (;;) |
… | |
… | |
5546 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5892 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5547 | return; |
5893 | return; |
5548 | |
5894 | |
5549 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5895 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5550 | { |
5896 | { |
5551 | xwrstr ("No memory!\n"); |
5897 | putstr (SCHEME_A_ "No memory!\n"); |
5552 | return; |
5898 | return; |
5553 | } |
5899 | } |
5554 | } |
5900 | } |
5555 | } |
5901 | } |
5556 | |
5902 | |
5557 | /* ========== Initialization of internal keywords ========== */ |
5903 | /* ========== Initialization of internal keywords ========== */ |
5558 | |
5904 | |
5559 | static void |
5905 | ecb_cold static void |
5560 | assign_syntax (SCHEME_P_ const char *name) |
5906 | assign_syntax (SCHEME_P_ const char *name) |
5561 | { |
5907 | { |
5562 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5908 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5563 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5909 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5564 | } |
5910 | } |
5565 | |
5911 | |
5566 | static void |
5912 | ecb_cold static void |
5567 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5913 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5568 | { |
5914 | { |
5569 | pointer x = mk_symbol (SCHEME_A_ name); |
5915 | pointer x = mk_symbol (SCHEME_A_ name); |
5570 | pointer y = mk_proc (SCHEME_A_ op); |
5916 | pointer y = mk_proc (SCHEME_A_ op); |
5571 | new_slot_in_env (SCHEME_A_ x, y); |
5917 | new_slot_in_env (SCHEME_A_ x, y); |
… | |
… | |
5579 | ivalue_unchecked (y) = op; |
5925 | ivalue_unchecked (y) = op; |
5580 | return y; |
5926 | return y; |
5581 | } |
5927 | } |
5582 | |
5928 | |
5583 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5929 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5584 | static int |
5930 | ecb_hot static int |
5585 | syntaxnum (pointer p) |
5931 | syntaxnum (pointer p) |
5586 | { |
5932 | { |
5587 | const char *s = strvalue (p); |
5933 | const char *s = strvalue (p); |
5588 | |
5934 | |
5589 | switch (strlength (p)) |
5935 | switch (strlength (p)) |
… | |
… | |
5668 | |
6014 | |
5669 | ecb_cold int |
6015 | ecb_cold int |
5670 | scheme_init (SCHEME_P) |
6016 | scheme_init (SCHEME_P) |
5671 | { |
6017 | { |
5672 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
6018 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5673 | pointer x; |
|
|
5674 | |
6019 | |
5675 | /* this memset is not strictly correct, as we assume (intcache) |
6020 | /* this memset is not strictly correct, as we assume (intcache) |
5676 | * that memset 0 will also set pointers to 0, but memset does |
6021 | * that memset 0 will also set pointers to 0, but memset does |
5677 | * of course not guarantee that. screw such systems. |
6022 | * of course not guarantee that. screw such systems. |
5678 | */ |
6023 | */ |
… | |
… | |
5706 | #endif |
6051 | #endif |
5707 | } |
6052 | } |
5708 | |
6053 | |
5709 | SCHEME_V->gc_verbose = 0; |
6054 | SCHEME_V->gc_verbose = 0; |
5710 | dump_stack_initialize (SCHEME_A); |
6055 | dump_stack_initialize (SCHEME_A); |
5711 | SCHEME_V->code = NIL; |
6056 | SCHEME_V->code = NIL; |
5712 | SCHEME_V->args = NIL; |
6057 | SCHEME_V->args = NIL; |
5713 | SCHEME_V->envir = NIL; |
6058 | SCHEME_V->envir = NIL; |
|
|
6059 | SCHEME_V->value = NIL; |
5714 | SCHEME_V->tracing = 0; |
6060 | SCHEME_V->tracing = 0; |
5715 | |
6061 | |
5716 | /* init NIL */ |
6062 | /* init NIL */ |
5717 | set_typeflag (NIL, T_ATOM | T_MARK); |
6063 | set_typeflag (NIL, T_SPECIAL | T_ATOM); |
5718 | set_car (NIL, NIL); |
6064 | set_car (NIL, NIL); |
5719 | set_cdr (NIL, NIL); |
6065 | set_cdr (NIL, NIL); |
5720 | /* init T */ |
6066 | /* init T */ |
5721 | set_typeflag (S_T, T_ATOM | T_MARK); |
6067 | set_typeflag (S_T, T_SPECIAL | T_ATOM); |
5722 | set_car (S_T, S_T); |
6068 | set_car (S_T, S_T); |
5723 | set_cdr (S_T, S_T); |
6069 | set_cdr (S_T, S_T); |
5724 | /* init F */ |
6070 | /* init F */ |
5725 | set_typeflag (S_F, T_ATOM | T_MARK); |
6071 | set_typeflag (S_F, T_SPECIAL | T_ATOM); |
5726 | set_car (S_F, S_F); |
6072 | set_car (S_F, S_F); |
5727 | set_cdr (S_F, S_F); |
6073 | set_cdr (S_F, S_F); |
5728 | /* init EOF_OBJ */ |
6074 | /* init EOF_OBJ */ |
5729 | set_typeflag (S_EOF, T_ATOM | T_MARK); |
6075 | set_typeflag (S_EOF, T_SPECIAL | T_ATOM); |
5730 | set_car (S_EOF, S_EOF); |
6076 | set_car (S_EOF, S_EOF); |
5731 | set_cdr (S_EOF, S_EOF); |
6077 | set_cdr (S_EOF, S_EOF); |
5732 | /* init sink */ |
6078 | /* init sink */ |
5733 | set_typeflag (S_SINK, T_PAIR | T_MARK); |
6079 | set_typeflag (S_SINK, T_PAIR); |
5734 | set_car (S_SINK, NIL); |
6080 | set_car (S_SINK, NIL); |
5735 | |
6081 | |
5736 | /* init c_nest */ |
6082 | /* init c_nest */ |
5737 | SCHEME_V->c_nest = NIL; |
6083 | SCHEME_V->c_nest = NIL; |
5738 | |
6084 | |
5739 | SCHEME_V->oblist = oblist_initial_value (SCHEME_A); |
6085 | SCHEME_V->oblist = oblist_initial_value (SCHEME_A); |
5740 | /* init global_env */ |
6086 | /* init global_env */ |
5741 | new_frame_in_env (SCHEME_A_ NIL); |
6087 | new_frame_in_env (SCHEME_A_ NIL); |
5742 | SCHEME_V->global_env = SCHEME_V->envir; |
6088 | SCHEME_V->global_env = SCHEME_V->envir; |
5743 | /* init else */ |
6089 | /* init else */ |
5744 | x = mk_symbol (SCHEME_A_ "else"); |
6090 | new_slot_in_env (SCHEME_A_ mk_symbol (SCHEME_A_ "else"), S_T); |
5745 | new_slot_in_env (SCHEME_A_ x, S_T); |
|
|
5746 | |
6091 | |
5747 | { |
6092 | { |
5748 | static const char *syntax_names[] = { |
6093 | static const char *syntax_names[] = { |
5749 | "lambda", "quote", "define", "if", "begin", "set!", |
6094 | "lambda", "quote", "define", "if", "begin", "set!", |
5750 | "let", "let*", "letrec", "cond", "delay", "and", |
6095 | "let", "let*", "letrec", "cond", "delay", "and", |
… | |
… | |
5774 | |
6119 | |
5775 | return !SCHEME_V->no_memory; |
6120 | return !SCHEME_V->no_memory; |
5776 | } |
6121 | } |
5777 | |
6122 | |
5778 | #if USE_PORTS |
6123 | #if USE_PORTS |
5779 | void |
6124 | ecb_cold void |
5780 | scheme_set_input_port_file (SCHEME_P_ int fin) |
6125 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5781 | { |
6126 | { |
5782 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
6127 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5783 | } |
6128 | } |
5784 | |
6129 | |
5785 | void |
6130 | ecb_cold void |
5786 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
6131 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5787 | { |
6132 | { |
5788 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
6133 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5789 | } |
6134 | } |
5790 | |
6135 | |
5791 | void |
6136 | ecb_cold void |
5792 | scheme_set_output_port_file (SCHEME_P_ int fout) |
6137 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5793 | { |
6138 | { |
5794 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
6139 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5795 | } |
6140 | } |
5796 | |
6141 | |
5797 | void |
6142 | ecb_cold void |
5798 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
6143 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5799 | { |
6144 | { |
5800 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
6145 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5801 | } |
6146 | } |
5802 | #endif |
6147 | #endif |
5803 | |
6148 | |
5804 | void |
6149 | ecb_cold void |
5805 | scheme_set_external_data (SCHEME_P_ void *p) |
6150 | scheme_set_external_data (SCHEME_P_ void *p) |
5806 | { |
6151 | { |
5807 | SCHEME_V->ext_data = p; |
6152 | SCHEME_V->ext_data = p; |
5808 | } |
6153 | } |
5809 | |
6154 | |
… | |
… | |
5841 | SCHEME_V->loadport = NIL; |
6186 | SCHEME_V->loadport = NIL; |
5842 | SCHEME_V->gc_verbose = 0; |
6187 | SCHEME_V->gc_verbose = 0; |
5843 | gc (SCHEME_A_ NIL, NIL); |
6188 | gc (SCHEME_A_ NIL, NIL); |
5844 | |
6189 | |
5845 | for (i = 0; i <= SCHEME_V->last_cell_seg; i++) |
6190 | for (i = 0; i <= SCHEME_V->last_cell_seg; i++) |
5846 | free (SCHEME_V->alloc_seg[i]); |
6191 | free (SCHEME_V->cell_seg[i]); |
5847 | |
6192 | |
5848 | #if SHOW_ERROR_LINE |
6193 | #if SHOW_ERROR_LINE |
5849 | for (i = 0; i <= SCHEME_V->file_i; i++) |
6194 | for (i = 0; i <= SCHEME_V->file_i; i++) |
5850 | { |
|
|
5851 | if (SCHEME_V->load_stack[i].kind & port_file) |
6195 | if (SCHEME_V->load_stack[i].kind & port_file) |
5852 | { |
6196 | { |
5853 | fname = SCHEME_V->load_stack[i].rep.stdio.filename; |
6197 | fname = SCHEME_V->load_stack[i].rep.stdio.filename; |
5854 | |
6198 | |
5855 | if (fname) |
6199 | if (fname) |
5856 | free (fname); |
6200 | free (fname); |
5857 | } |
6201 | } |
5858 | } |
|
|
5859 | #endif |
6202 | #endif |
5860 | } |
6203 | } |
5861 | |
6204 | |
5862 | void |
6205 | ecb_cold void |
5863 | scheme_load_file (SCHEME_P_ int fin) |
6206 | scheme_load_file (SCHEME_P_ int fin) |
5864 | { |
6207 | { |
5865 | scheme_load_named_file (SCHEME_A_ fin, 0); |
6208 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5866 | } |
6209 | } |
5867 | |
6210 | |
5868 | void |
6211 | ecb_cold void |
5869 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
6212 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5870 | { |
6213 | { |
5871 | dump_stack_reset (SCHEME_A); |
6214 | dump_stack_reset (SCHEME_A); |
5872 | SCHEME_V->envir = SCHEME_V->global_env; |
6215 | SCHEME_V->envir = SCHEME_V->global_env; |
5873 | SCHEME_V->file_i = 0; |
6216 | SCHEME_V->file_i = 0; |
5874 | SCHEME_V->load_stack[0].unget = -1; |
6217 | SCHEME_V->load_stack[0].unget = -1; |
5875 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
6218 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5876 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
6219 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5877 | #if USE_PORTS |
|
|
5878 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
6220 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5879 | #endif |
|
|
5880 | SCHEME_V->retcode = 0; |
6221 | SCHEME_V->retcode = 0; |
5881 | |
6222 | |
5882 | #if USE_PORTS |
|
|
5883 | if (fin == STDIN_FILENO) |
6223 | if (fin == STDIN_FILENO) |
5884 | SCHEME_V->interactive_repl = 1; |
6224 | SCHEME_V->interactive_repl = 1; |
5885 | #endif |
|
|
5886 | |
6225 | |
5887 | #if USE_PORTS |
6226 | #if USE_PORTS |
5888 | #if SHOW_ERROR_LINE |
6227 | #if SHOW_ERROR_LINE |
5889 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
6228 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5890 | |
6229 | |
… | |
… | |
5894 | #endif |
6233 | #endif |
5895 | |
6234 | |
5896 | SCHEME_V->inport = SCHEME_V->loadport; |
6235 | SCHEME_V->inport = SCHEME_V->loadport; |
5897 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
6236 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5898 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
6237 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
|
|
6238 | |
5899 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
6239 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5900 | |
6240 | |
5901 | if (SCHEME_V->retcode == 0) |
6241 | if (SCHEME_V->retcode == 0) |
5902 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
6242 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5903 | } |
6243 | } |
5904 | |
6244 | |
5905 | void |
6245 | ecb_cold void |
5906 | scheme_load_string (SCHEME_P_ const char *cmd) |
6246 | scheme_load_string (SCHEME_P_ const char *cmd) |
5907 | { |
6247 | { |
|
|
6248 | #if USE_PORTs |
5908 | dump_stack_reset (SCHEME_A); |
6249 | dump_stack_reset (SCHEME_A); |
5909 | SCHEME_V->envir = SCHEME_V->global_env; |
6250 | SCHEME_V->envir = SCHEME_V->global_env; |
5910 | SCHEME_V->file_i = 0; |
6251 | SCHEME_V->file_i = 0; |
5911 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
6252 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5912 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
6253 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
5913 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
6254 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
5914 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
6255 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5915 | #if USE_PORTS |
|
|
5916 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
6256 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5917 | #endif |
|
|
5918 | SCHEME_V->retcode = 0; |
6257 | SCHEME_V->retcode = 0; |
5919 | SCHEME_V->interactive_repl = 0; |
6258 | SCHEME_V->interactive_repl = 0; |
5920 | SCHEME_V->inport = SCHEME_V->loadport; |
6259 | SCHEME_V->inport = SCHEME_V->loadport; |
5921 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
6260 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5922 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
6261 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5923 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
6262 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5924 | |
6263 | |
5925 | if (SCHEME_V->retcode == 0) |
6264 | if (SCHEME_V->retcode == 0) |
5926 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
6265 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
|
|
6266 | #else |
|
|
6267 | abort (); |
|
|
6268 | #endif |
5927 | } |
6269 | } |
5928 | |
6270 | |
5929 | void |
6271 | ecb_cold void |
5930 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
6272 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5931 | { |
6273 | { |
5932 | pointer x; |
6274 | pointer x; |
5933 | |
6275 | |
5934 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
6276 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
… | |
… | |
5939 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
6281 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5940 | } |
6282 | } |
5941 | |
6283 | |
5942 | #if !STANDALONE |
6284 | #if !STANDALONE |
5943 | |
6285 | |
5944 | void |
6286 | ecb_cold void |
5945 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
6287 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5946 | { |
6288 | { |
5947 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
6289 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5948 | } |
6290 | } |
5949 | |
6291 | |
5950 | void |
6292 | ecb_cold void |
5951 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
6293 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5952 | { |
6294 | { |
5953 | int i; |
6295 | int i; |
5954 | |
6296 | |
5955 | for (i = 0; i < count; i++) |
6297 | for (i = 0; i < count; i++) |
5956 | scheme_register_foreign_func (SCHEME_A_ list + i); |
6298 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5957 | } |
6299 | } |
5958 | |
6300 | |
5959 | pointer |
6301 | ecb_cold pointer |
5960 | scheme_apply0 (SCHEME_P_ const char *procname) |
6302 | scheme_apply0 (SCHEME_P_ const char *procname) |
5961 | { |
6303 | { |
5962 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
6304 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
5963 | } |
6305 | } |
5964 | |
6306 | |
5965 | void |
6307 | ecb_cold void |
5966 | save_from_C_call (SCHEME_P) |
6308 | save_from_C_call (SCHEME_P) |
5967 | { |
6309 | { |
5968 | pointer saved_data = cons (car (S_SINK), |
6310 | pointer saved_data = cons (car (S_SINK), |
5969 | cons (SCHEME_V->envir, |
6311 | cons (SCHEME_V->envir, |
5970 | SCHEME_V->dump)); |
6312 | SCHEME_V->dump)); |
… | |
… | |
5974 | /* Truncate the dump stack so TS will return here when done, not |
6316 | /* Truncate the dump stack so TS will return here when done, not |
5975 | directly resume pre-C-call operations. */ |
6317 | directly resume pre-C-call operations. */ |
5976 | dump_stack_reset (SCHEME_A); |
6318 | dump_stack_reset (SCHEME_A); |
5977 | } |
6319 | } |
5978 | |
6320 | |
5979 | void |
6321 | ecb_cold void |
5980 | restore_from_C_call (SCHEME_P) |
6322 | restore_from_C_call (SCHEME_P) |
5981 | { |
6323 | { |
5982 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
6324 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5983 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
6325 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5984 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
6326 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5985 | /* Pop */ |
6327 | /* Pop */ |
5986 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
6328 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5987 | } |
6329 | } |
5988 | |
6330 | |
5989 | /* "func" and "args" are assumed to be already eval'ed. */ |
6331 | /* "func" and "args" are assumed to be already eval'ed. */ |
5990 | pointer |
6332 | ecb_cold pointer |
5991 | scheme_call (SCHEME_P_ pointer func, pointer args) |
6333 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5992 | { |
6334 | { |
5993 | int old_repl = SCHEME_V->interactive_repl; |
6335 | int old_repl = SCHEME_V->interactive_repl; |
5994 | |
6336 | |
5995 | SCHEME_V->interactive_repl = 0; |
6337 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
6002 | SCHEME_V->interactive_repl = old_repl; |
6344 | SCHEME_V->interactive_repl = old_repl; |
6003 | restore_from_C_call (SCHEME_A); |
6345 | restore_from_C_call (SCHEME_A); |
6004 | return SCHEME_V->value; |
6346 | return SCHEME_V->value; |
6005 | } |
6347 | } |
6006 | |
6348 | |
6007 | pointer |
6349 | ecb_cold pointer |
6008 | scheme_eval (SCHEME_P_ pointer obj) |
6350 | scheme_eval (SCHEME_P_ pointer obj) |
6009 | { |
6351 | { |
6010 | int old_repl = SCHEME_V->interactive_repl; |
6352 | int old_repl = SCHEME_V->interactive_repl; |
6011 | |
6353 | |
6012 | SCHEME_V->interactive_repl = 0; |
6354 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
6024 | |
6366 | |
6025 | /* ========== Main ========== */ |
6367 | /* ========== Main ========== */ |
6026 | |
6368 | |
6027 | #if STANDALONE |
6369 | #if STANDALONE |
6028 | |
6370 | |
6029 | int |
6371 | ecb_cold int |
6030 | main (int argc, char **argv) |
6372 | main (int argc, char **argv) |
6031 | { |
6373 | { |
6032 | # if USE_MULTIPLICITY |
6374 | # if USE_MULTIPLICITY |
6033 | scheme ssc; |
6375 | scheme ssc; |
6034 | scheme *const SCHEME_V = &ssc; |
6376 | scheme *const SCHEME_V = &ssc; |
… | |
… | |
6036 | # endif |
6378 | # endif |
6037 | int fin; |
6379 | int fin; |
6038 | char *file_name = InitFile; |
6380 | char *file_name = InitFile; |
6039 | int retcode; |
6381 | int retcode; |
6040 | int isfile = 1; |
6382 | int isfile = 1; |
|
|
6383 | #if EXPERIMENT |
6041 | system ("ps v $PPID");//D |
6384 | system ("ps v $PPID"); |
|
|
6385 | #endif |
6042 | |
6386 | |
6043 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6387 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6044 | { |
6388 | { |
6045 | xwrstr ("Usage: tinyscheme -?\n"); |
6389 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
6046 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6390 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
6047 | xwrstr ("followed by\n"); |
6391 | putstr (SCHEME_A_ "followed by\n"); |
6048 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6392 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
6049 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6393 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6050 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6394 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
6051 | xwrstr ("Use - as filename for stdin.\n"); |
6395 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
6052 | return 1; |
6396 | return 1; |
6053 | } |
6397 | } |
6054 | |
6398 | |
6055 | if (!scheme_init (SCHEME_A)) |
6399 | if (!scheme_init (SCHEME_A)) |
6056 | { |
6400 | { |
6057 | xwrstr ("Could not initialize!\n"); |
6401 | putstr (SCHEME_A_ "Could not initialize!\n"); |
6058 | return 2; |
6402 | return 2; |
6059 | } |
6403 | } |
6060 | |
6404 | |
6061 | # if USE_PORTS |
6405 | # if USE_PORTS |
6062 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6406 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
6075 | } |
6419 | } |
6076 | #endif |
6420 | #endif |
6077 | |
6421 | |
6078 | do |
6422 | do |
6079 | { |
6423 | { |
6080 | #if USE_PORTS |
|
|
6081 | if (strcmp (file_name, "-") == 0) |
6424 | if (strcmp (file_name, "-") == 0) |
6082 | fin = STDIN_FILENO; |
6425 | fin = STDIN_FILENO; |
6083 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
6426 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
6084 | { |
6427 | { |
6085 | pointer args = NIL; |
6428 | pointer args = NIL; |
… | |
… | |
6103 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
6446 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
6104 | |
6447 | |
6105 | } |
6448 | } |
6106 | else |
6449 | else |
6107 | fin = open (file_name, O_RDONLY); |
6450 | fin = open (file_name, O_RDONLY); |
6108 | #endif |
|
|
6109 | |
6451 | |
6110 | if (isfile && fin < 0) |
6452 | if (isfile && fin < 0) |
6111 | { |
6453 | { |
6112 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6454 | putstr (SCHEME_A_ "Could not open file "); |
|
|
6455 | putstr (SCHEME_A_ file_name); |
|
|
6456 | putcharacter (SCHEME_A_ '\n'); |
6113 | } |
6457 | } |
6114 | else |
6458 | else |
6115 | { |
6459 | { |
6116 | if (isfile) |
6460 | if (isfile) |
6117 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6461 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6118 | else |
6462 | else |
6119 | scheme_load_string (SCHEME_A_ file_name); |
6463 | scheme_load_string (SCHEME_A_ file_name); |
6120 | |
6464 | |
6121 | #if USE_PORTS |
|
|
6122 | if (!isfile || fin != STDIN_FILENO) |
6465 | if (!isfile || fin != STDIN_FILENO) |
6123 | { |
6466 | { |
6124 | if (SCHEME_V->retcode != 0) |
6467 | if (SCHEME_V->retcode != 0) |
6125 | { |
6468 | { |
6126 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6469 | putstr (SCHEME_A_ "Errors encountered reading "); |
|
|
6470 | putstr (SCHEME_A_ file_name); |
|
|
6471 | putcharacter (SCHEME_A_ '\n'); |
6127 | } |
6472 | } |
6128 | |
6473 | |
6129 | if (isfile) |
6474 | if (isfile) |
6130 | close (fin); |
6475 | close (fin); |
6131 | } |
6476 | } |
6132 | #endif |
|
|
6133 | } |
6477 | } |
6134 | |
6478 | |
6135 | file_name = *argv++; |
6479 | file_name = *argv++; |
6136 | } |
6480 | } |
6137 | while (file_name != 0); |
6481 | while (file_name != 0); |