… | |
… | |
16 | * (MINISCM) This is a revised and modified version by Akira KIDA. |
16 | * (MINISCM) This is a revised and modified version by Akira KIDA. |
17 | * (MINISCM) current version is 0.85k4 (15 May 1994) |
17 | * (MINISCM) current version is 0.85k4 (15 May 1994) |
18 | * |
18 | * |
19 | */ |
19 | */ |
20 | |
20 | |
21 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
21 | #define _POSIX_C_SOURCE 200201 |
22 | #include "malloc.c" |
22 | #define _XOPEN_SOURCE 600 |
|
|
23 | #define _GNU_SOURCE 1 /* for malloc mremap */ |
23 | |
24 | |
24 | #define SCHEME_SOURCE |
25 | #define SCHEME_SOURCE |
25 | #include "scheme-private.h" |
26 | #include "scheme-private.h" |
26 | #ifndef WIN32 |
27 | #ifndef WIN32 |
27 | # include <unistd.h> |
28 | # include <unistd.h> |
28 | #endif |
29 | #endif |
29 | #if USE_MATH |
30 | #if USE_MATH |
30 | # include <math.h> |
31 | # include <math.h> |
31 | #endif |
32 | #endif |
32 | |
33 | |
|
|
34 | #define ECB_NO_THREADS 1 |
|
|
35 | #include "ecb.h" |
|
|
36 | |
33 | #include <sys/types.h> |
37 | #include <sys/types.h> |
34 | #include <sys/stat.h> |
38 | #include <sys/stat.h> |
35 | #include <fcntl.h> |
39 | #include <fcntl.h> |
36 | |
40 | |
|
|
41 | #if !USE_ERROR_CHECKING |
|
|
42 | # define NDEBUG |
|
|
43 | #endif |
|
|
44 | |
|
|
45 | #include <assert.h> |
|
|
46 | #include <stdlib.h> |
37 | #include <string.h> |
47 | #include <string.h> |
38 | #include <stdlib.h> |
|
|
39 | |
48 | |
40 | #include <limits.h> |
49 | #include <limits.h> |
41 | #include <inttypes.h> |
50 | #include <inttypes.h> |
42 | #include <float.h> |
51 | #include <float.h> |
43 | //#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 |
|
|
60 | |
|
|
61 | #if '1' != '0' + 1 \ |
|
|
62 | || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ |
|
|
63 | || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ |
|
|
64 | || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ |
|
|
65 | || 'f' != 'a' + 5 |
|
|
66 | # error "execution character set digits not consecutive" |
|
|
67 | #endif |
44 | |
68 | |
45 | enum { |
69 | enum { |
46 | TOK_EOF, |
70 | TOK_EOF, |
47 | TOK_LPAREN, |
71 | TOK_LPAREN, |
48 | TOK_RPAREN, |
72 | TOK_RPAREN, |
49 | TOK_DOT, |
73 | TOK_DOT, |
50 | TOK_ATOM, |
74 | TOK_ATOM, |
|
|
75 | TOK_DOTATOM, /* atom name starting with '.' */ |
|
|
76 | TOK_STRATOM, /* atom name enclosed in | */ |
51 | TOK_QUOTE, |
77 | TOK_QUOTE, |
52 | TOK_DQUOTE, |
78 | TOK_DQUOTE, |
53 | TOK_BQUOTE, |
79 | TOK_BQUOTE, |
54 | TOK_COMMA, |
80 | TOK_COMMA, |
55 | TOK_ATMARK, |
81 | TOK_ATMARK, |
… | |
… | |
57 | TOK_SHARP_CONST, |
83 | TOK_SHARP_CONST, |
58 | TOK_VEC |
84 | TOK_VEC |
59 | }; |
85 | }; |
60 | |
86 | |
61 | #define BACKQUOTE '`' |
87 | #define BACKQUOTE '`' |
62 | #define DELIMITERS "()\";\f\t\v\n\r " |
88 | #define WHITESPACE " \t\r\n\v\f" |
|
|
89 | #define DELIMITERS "()\";" WHITESPACE |
63 | |
90 | |
64 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
91 | #define NIL POINTER (&SCHEME_V->xNIL) |
65 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
92 | #define S_T POINTER (&SCHEME_V->xT) |
66 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
93 | #define S_F POINTER (&SCHEME_V->xF) |
67 | #define S_SINK (&SCHEME_V->xsink) |
94 | #define S_SINK POINTER (&SCHEME_V->xsink) |
68 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
95 | #define S_EOF POINTER (&SCHEME_V->xEOF_OBJ) |
69 | |
|
|
70 | /* should use libecb */ |
|
|
71 | #if __GNUC__ >= 4 |
|
|
72 | # define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
73 | # define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
|
|
74 | # define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
|
|
75 | #else |
|
|
76 | # define ecb_expect_false(expr) !!(expr) |
|
|
77 | # define ecb_expect_true(expr) !!(expr) |
|
|
78 | #endif |
|
|
79 | |
96 | |
80 | #if !USE_MULTIPLICITY |
97 | #if !USE_MULTIPLICITY |
81 | static scheme sc; |
98 | static scheme sc; |
82 | #endif |
99 | #endif |
83 | |
100 | |
84 | static void |
101 | ecb_cold static void |
85 | xbase (char *s, long n, int base) |
102 | xbase (char *s, long n, int base) |
86 | { |
103 | { |
87 | if (n < 0) |
104 | if (n < 0) |
88 | { |
105 | { |
89 | *s++ = '-'; |
106 | *s++ = '-'; |
… | |
… | |
91 | } |
108 | } |
92 | |
109 | |
93 | char *p = s; |
110 | char *p = s; |
94 | |
111 | |
95 | do { |
112 | do { |
96 | *p++ = '0' + n % base; |
113 | *p++ = "0123456789abcdef"[n % base]; |
97 | n /= base; |
114 | n /= base; |
98 | } while (n); |
115 | } while (n); |
99 | |
116 | |
100 | *p-- = 0; |
117 | *p-- = 0; |
101 | |
118 | |
… | |
… | |
104 | char x = *s; *s = *p; *p = x; |
121 | char x = *s; *s = *p; *p = x; |
105 | --p; ++s; |
122 | --p; ++s; |
106 | } |
123 | } |
107 | } |
124 | } |
108 | |
125 | |
109 | static void |
126 | ecb_cold static void |
110 | xnum (char *s, long n) |
127 | xnum (char *s, long n) |
111 | { |
128 | { |
112 | xbase (s, n, 10); |
129 | xbase (s, n, 10); |
113 | } |
130 | } |
114 | |
131 | |
115 | static void |
132 | ecb_cold static void |
116 | xwrstr (const char *s) |
133 | putnum (SCHEME_P_ long n) |
117 | { |
|
|
118 | write (1, s, strlen (s)); |
|
|
119 | } |
|
|
120 | |
|
|
121 | static void |
|
|
122 | xwrnum (long n) |
|
|
123 | { |
134 | { |
124 | char buf[64]; |
135 | char buf[64]; |
125 | |
136 | |
126 | xnum (buf, n); |
137 | xnum (buf, n); |
127 | xwrstr (buf); |
138 | putstr (SCHEME_A_ buf); |
128 | } |
139 | } |
|
|
140 | |
|
|
141 | #if USE_CHAR_CLASSIFIERS |
|
|
142 | #include <ctype.h> |
|
|
143 | #else |
129 | |
144 | |
130 | static char |
145 | static char |
131 | xtoupper (char c) |
146 | xtoupper (char c) |
132 | { |
147 | { |
133 | if (c >= 'a' && c <= 'z') |
148 | if (c >= 'a' && c <= 'z') |
… | |
… | |
153 | |
168 | |
154 | #define toupper(c) xtoupper (c) |
169 | #define toupper(c) xtoupper (c) |
155 | #define tolower(c) xtolower (c) |
170 | #define tolower(c) xtolower (c) |
156 | #define isdigit(c) xisdigit (c) |
171 | #define isdigit(c) xisdigit (c) |
157 | |
172 | |
158 | #if USE_STRLWR |
173 | #endif |
|
|
174 | |
|
|
175 | #if USE_IGNORECASE |
159 | static const char * |
176 | ecb_cold static const char * |
160 | strlwr (char *s) |
177 | xstrlwr (char *s) |
161 | { |
178 | { |
162 | const char *p = s; |
179 | const char *p = s; |
163 | |
180 | |
164 | while (*s) |
181 | while (*s) |
165 | { |
182 | { |
… | |
… | |
167 | s++; |
184 | s++; |
168 | } |
185 | } |
169 | |
186 | |
170 | return p; |
187 | return p; |
171 | } |
188 | } |
172 | #endif |
|
|
173 | |
189 | |
|
|
190 | #define stricmp(a,b) strcasecmp (a, b) |
|
|
191 | #define strlwr(s) xstrlwr (s) |
|
|
192 | |
|
|
193 | #else |
174 | #define stricmp(a,b) strcmp (a, b) |
194 | # define stricmp(a,b) strcmp (a, b) |
175 | #define strlwr(s) (s) |
195 | # define strlwr(s) (s) |
|
|
196 | #endif |
176 | |
197 | |
177 | #ifndef prompt |
198 | #ifndef prompt |
178 | # define prompt "ts> " |
199 | # define prompt "ms> " |
179 | #endif |
200 | #endif |
180 | |
201 | |
181 | #ifndef InitFile |
202 | #ifndef InitFile |
182 | # define InitFile "init.scm" |
203 | # define InitFile "init.scm" |
183 | #endif |
204 | #endif |
184 | |
205 | |
185 | #ifndef FIRST_CELLSEGS |
|
|
186 | # define FIRST_CELLSEGS 3 |
|
|
187 | #endif |
|
|
188 | |
|
|
189 | enum scheme_types |
206 | enum scheme_types |
190 | { |
207 | { |
|
|
208 | T_INTEGER, |
|
|
209 | T_CHARACTER, |
191 | T_FREE, |
210 | T_REAL, |
192 | T_STRING, |
211 | T_STRING, |
193 | T_NUMBER, |
|
|
194 | T_SYMBOL, |
212 | T_SYMBOL, |
195 | T_PROC, |
213 | T_PROC, |
196 | T_PAIR, |
214 | T_PAIR, /* also used for free cells */ |
197 | T_CLOSURE, |
215 | T_CLOSURE, |
|
|
216 | T_BYTECODE, // temp |
|
|
217 | T_MACRO, |
198 | T_CONTINUATION, |
218 | T_CONTINUATION, |
199 | T_FOREIGN, |
219 | T_FOREIGN, |
200 | T_CHARACTER, |
|
|
201 | T_PORT, |
220 | T_PORT, |
202 | T_VECTOR, |
221 | T_VECTOR, |
203 | T_MACRO, |
|
|
204 | T_PROMISE, |
222 | T_PROMISE, |
205 | T_ENVIRONMENT, |
223 | T_ENVIRONMENT, |
206 | /* one more... */ |
224 | T_SPECIAL, // #t, #f, '(), eof-object |
|
|
225 | |
207 | T_NUM_SYSTEM_TYPES |
226 | T_NUM_SYSTEM_TYPES |
208 | }; |
227 | }; |
209 | |
228 | |
210 | #define T_MASKTYPE 0x000f |
229 | #define T_MASKTYPE 0x001f |
211 | #define T_SYNTAX 0x0010 |
230 | #define T_SYNTAX 0x0020 |
212 | #define T_IMMUTABLE 0x0020 |
231 | #define T_IMMUTABLE 0x0040 |
213 | #define T_ATOM 0x0040 /* only for gc */ |
232 | #define T_ATOM 0x0080 /* only for gc */ |
214 | #define T_MARK 0x0080 /* only for gc */ |
233 | //#define T_MARK 0x0080 /* only for gc */ |
|
|
234 | |
|
|
235 | /* num, for generic arithmetic */ |
|
|
236 | struct num |
|
|
237 | { |
|
|
238 | IVALUE ivalue; |
|
|
239 | #if USE_REAL |
|
|
240 | RVALUE rvalue; |
|
|
241 | char is_fixnum; |
|
|
242 | #endif |
|
|
243 | }; |
|
|
244 | |
|
|
245 | #if USE_REAL |
|
|
246 | # define num_is_fixnum(n) (n).is_fixnum |
|
|
247 | # define num_set_fixnum(n,f) (n).is_fixnum = (f) |
|
|
248 | # define num_ivalue(n) (n).ivalue |
|
|
249 | # define num_rvalue(n) (n).rvalue |
|
|
250 | # define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i) |
|
|
251 | # define num_set_rvalue(n,r) (n).rvalue = (r) |
|
|
252 | #else |
|
|
253 | # define num_is_fixnum(n) 1 |
|
|
254 | # define num_set_fixnum(n,f) 0 |
|
|
255 | # define num_ivalue(n) (n).ivalue |
|
|
256 | # define num_rvalue(n) (n).ivalue |
|
|
257 | # define num_set_ivalue(n,i) (n).ivalue = (i) |
|
|
258 | # define num_set_rvalue(n,r) (n).ivalue = (r) |
|
|
259 | #endif |
215 | |
260 | |
216 | enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; |
261 | enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; |
217 | |
262 | |
218 | 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); |
219 | static num num_intdiv (num a, num b); |
264 | static num num_intdiv (num a, num b); |
220 | static num num_rem (num a, num b); |
265 | static num num_rem (num a, num b); |
221 | static num num_mod (num a, num b); |
266 | static num num_mod (num a, num b); |
222 | |
267 | |
223 | #if USE_MATH |
|
|
224 | static double round_per_R5RS (double x); |
|
|
225 | #endif |
|
|
226 | static int is_zero_rvalue (RVALUE x); |
268 | static int is_zero_rvalue (RVALUE x); |
227 | |
|
|
228 | static INLINE int |
|
|
229 | num_is_integer (pointer p) |
|
|
230 | { |
|
|
231 | return num_is_fixnum (p->object.number); |
|
|
232 | } |
|
|
233 | |
269 | |
234 | static num num_zero; |
270 | static num num_zero; |
235 | static num num_one; |
271 | static num num_one; |
236 | |
272 | |
|
|
273 | /* convert "pointer" to cell* / cell* to pointer */ |
|
|
274 | #define CELL(p) ((struct cell *)(p) + 0) |
|
|
275 | #define POINTER(c) ((void *)((c) - 0)) |
|
|
276 | |
237 | /* macros for cell operations */ |
277 | /* macros for cell operations */ |
238 | #define typeflag(p) ((p)->flag + 0) |
278 | #define typeflag(p) (CELL(p)->flag + 0) |
239 | #define set_typeflag(p,v) ((p)->flag = (v)) |
279 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
240 | #define type(p) (typeflag (p) & T_MASKTYPE) |
280 | #define type(p) (typeflag (p) & T_MASKTYPE) |
241 | |
281 | |
242 | INTERFACE INLINE int |
282 | INTERFACE int |
243 | is_string (pointer p) |
283 | is_string (pointer p) |
244 | { |
284 | { |
245 | return type (p) == T_STRING; |
285 | return type (p) == T_STRING; |
246 | } |
286 | } |
247 | |
287 | |
248 | #define strvalue(p) ((p)->object.string.svalue) |
288 | #define strvalue(p) (CELL(p)->object.string.svalue) |
249 | #define strlength(p) ((p)->object.string.length) |
289 | #define strlength(p) (CELL(p)->object.string.length) |
250 | |
290 | |
251 | INTERFACE int is_list (SCHEME_P_ pointer p); |
|
|
252 | |
|
|
253 | INTERFACE INLINE int |
291 | INTERFACE int |
254 | is_vector (pointer p) |
292 | is_vector (pointer p) |
255 | { |
293 | { |
256 | return type (p) == T_VECTOR; |
294 | return type (p) == T_VECTOR; |
257 | } |
295 | } |
258 | |
296 | |
259 | #define vecvalue(p) ((p)->object.vector.vvalue) |
297 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
260 | #define veclength(p) ((p)->object.vector.length) |
298 | #define veclength(p) (CELL(p)->object.vector.length) |
261 | INTERFACE void fill_vector (pointer vec, pointer obj); |
299 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
262 | INTERFACE uint32_t vector_length (pointer vec); |
|
|
263 | INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); |
300 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
264 | INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); |
301 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
265 | |
302 | |
266 | INTERFACE uint32_t |
303 | INTERFACE int |
267 | vector_length (pointer vec) |
304 | is_integer (pointer p) |
268 | { |
305 | { |
269 | return vec->object.vector.length; |
306 | return type (p) == T_INTEGER; |
270 | } |
307 | } |
271 | |
308 | |
|
|
309 | /* not the same as in scheme, where integers are (correctly :) reals */ |
272 | INTERFACE INLINE int |
310 | INTERFACE int |
|
|
311 | is_real (pointer p) |
|
|
312 | { |
|
|
313 | return type (p) == T_REAL; |
|
|
314 | } |
|
|
315 | |
|
|
316 | INTERFACE int |
273 | is_number (pointer p) |
317 | is_number (pointer p) |
274 | { |
318 | { |
275 | return type (p) == T_NUMBER; |
319 | return is_integer (p) || is_real (p); |
276 | } |
320 | } |
277 | |
321 | |
278 | INTERFACE INLINE int |
322 | INTERFACE int |
279 | is_integer (pointer p) |
|
|
280 | { |
|
|
281 | if (!is_number (p)) |
|
|
282 | return 0; |
|
|
283 | |
|
|
284 | if (num_is_integer (p) || ivalue (p) == rvalue (p)) |
|
|
285 | return 1; |
|
|
286 | |
|
|
287 | return 0; |
|
|
288 | } |
|
|
289 | |
|
|
290 | INTERFACE INLINE int |
|
|
291 | is_real (pointer p) |
|
|
292 | { |
|
|
293 | return is_number (p) && !num_is_fixnum (p->object.number); |
|
|
294 | } |
|
|
295 | |
|
|
296 | INTERFACE INLINE int |
|
|
297 | is_character (pointer p) |
323 | is_character (pointer p) |
298 | { |
324 | { |
299 | return type (p) == T_CHARACTER; |
325 | return type (p) == T_CHARACTER; |
300 | } |
326 | } |
301 | |
327 | |
302 | INTERFACE INLINE char * |
328 | INTERFACE char * |
303 | string_value (pointer p) |
329 | string_value (pointer p) |
304 | { |
330 | { |
305 | return strvalue (p); |
331 | return strvalue (p); |
306 | } |
332 | } |
307 | |
333 | |
308 | INLINE num |
|
|
309 | nvalue (pointer p) |
|
|
310 | { |
|
|
311 | return (p)->object.number; |
|
|
312 | } |
|
|
313 | |
|
|
314 | static IVALUE |
|
|
315 | num_get_ivalue (const num n) |
|
|
316 | { |
|
|
317 | return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n); |
|
|
318 | } |
|
|
319 | |
|
|
320 | static RVALUE |
|
|
321 | num_get_rvalue (const num n) |
|
|
322 | { |
|
|
323 | return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n); |
|
|
324 | } |
|
|
325 | |
|
|
326 | INTERFACE IVALUE |
|
|
327 | ivalue (pointer p) |
|
|
328 | { |
|
|
329 | return num_get_ivalue (p->object.number); |
|
|
330 | } |
|
|
331 | |
|
|
332 | INTERFACE RVALUE |
|
|
333 | rvalue (pointer p) |
|
|
334 | { |
|
|
335 | return num_get_rvalue (p->object.number); |
|
|
336 | } |
|
|
337 | |
|
|
338 | #define ivalue_unchecked(p) ((p)->object.number.value.ivalue) |
334 | #define ivalue_unchecked(p) (CELL(p)->object.ivalue + 0) |
|
|
335 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
|
|
336 | |
339 | #if USE_REAL |
337 | #if USE_REAL |
340 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
338 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
341 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
339 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
342 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
|
|
343 | #else |
340 | #else |
344 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
341 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
345 | # define set_num_integer(p) 0 |
342 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
346 | # define set_num_real(p) 0 |
|
|
347 | #endif |
343 | #endif |
|
|
344 | |
348 | INTERFACE long |
345 | INTERFACE long |
349 | charvalue (pointer p) |
346 | charvalue (pointer p) |
350 | { |
347 | { |
351 | return ivalue_unchecked (p); |
348 | return ivalue_unchecked (p); |
352 | } |
349 | } |
353 | |
350 | |
|
|
351 | #define port(p) CELL(p)->object.port |
|
|
352 | #define set_port(p,v) port(p) = (v) |
354 | INTERFACE INLINE int |
353 | INTERFACE int |
355 | is_port (pointer p) |
354 | is_port (pointer p) |
356 | { |
355 | { |
357 | return type (p) == T_PORT; |
356 | return type (p) == T_PORT; |
358 | } |
357 | } |
359 | |
358 | |
360 | INTERFACE INLINE int |
359 | INTERFACE int |
361 | is_inport (pointer p) |
360 | is_inport (pointer p) |
362 | { |
361 | { |
363 | return is_port (p) && p->object.port->kind & port_input; |
362 | return is_port (p) && port (p)->kind & port_input; |
364 | } |
363 | } |
365 | |
364 | |
366 | INTERFACE INLINE int |
365 | INTERFACE int |
367 | is_outport (pointer p) |
366 | is_outport (pointer p) |
368 | { |
367 | { |
369 | return is_port (p) && p->object.port->kind & port_output; |
368 | return is_port (p) && port (p)->kind & port_output; |
370 | } |
369 | } |
371 | |
370 | |
372 | INTERFACE INLINE int |
371 | INTERFACE int |
373 | is_pair (pointer p) |
372 | is_pair (pointer p) |
374 | { |
373 | { |
375 | return type (p) == T_PAIR; |
374 | return type (p) == T_PAIR; |
376 | } |
375 | } |
377 | |
376 | |
378 | #define car(p) ((p)->object.cons.car + 0) |
377 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
379 | #define cdr(p) ((p)->object.cons.cdr + 0) |
378 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
380 | |
379 | |
381 | static pointer caar (pointer p) { return car (car (p)); } |
380 | static pointer caar (pointer p) { return car (car (p)); } |
382 | static pointer cadr (pointer p) { return car (cdr (p)); } |
381 | static pointer cadr (pointer p) { return car (cdr (p)); } |
383 | static pointer cdar (pointer p) { return cdr (car (p)); } |
382 | static pointer cdar (pointer p) { return cdr (car (p)); } |
384 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
383 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
385 | |
384 | |
386 | static pointer cadar (pointer p) { return car (cdr (car (p))); } |
385 | static pointer cadar (pointer p) { return car (cdr (car (p))); } |
387 | static pointer caddr (pointer p) { return car (cdr (cdr (p))); } |
386 | static pointer caddr (pointer p) { return car (cdr (cdr (p))); } |
388 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
387 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
389 | |
388 | |
|
|
389 | static pointer cadddr (pointer p) { return car (car (car (cdr (p)))); } |
|
|
390 | |
390 | INTERFACE void |
391 | INTERFACE void |
391 | set_car (pointer p, pointer q) |
392 | set_car (pointer p, pointer q) |
392 | { |
393 | { |
393 | p->object.cons.car = q; |
394 | CELL(p)->object.cons.car = CELL (q); |
394 | } |
395 | } |
395 | |
396 | |
396 | INTERFACE void |
397 | INTERFACE void |
397 | set_cdr (pointer p, pointer q) |
398 | set_cdr (pointer p, pointer q) |
398 | { |
399 | { |
399 | p->object.cons.cdr = q; |
400 | CELL(p)->object.cons.cdr = CELL (q); |
400 | } |
401 | } |
401 | |
402 | |
402 | INTERFACE pointer |
403 | INTERFACE pointer |
403 | pair_car (pointer p) |
404 | pair_car (pointer p) |
404 | { |
405 | { |
… | |
… | |
409 | pair_cdr (pointer p) |
410 | pair_cdr (pointer p) |
410 | { |
411 | { |
411 | return cdr (p); |
412 | return cdr (p); |
412 | } |
413 | } |
413 | |
414 | |
414 | INTERFACE INLINE int |
415 | INTERFACE int |
415 | is_symbol (pointer p) |
416 | is_symbol (pointer p) |
416 | { |
417 | { |
417 | return type (p) == T_SYMBOL; |
418 | return type (p) == T_SYMBOL; |
418 | } |
419 | } |
419 | |
420 | |
420 | INTERFACE INLINE char * |
421 | INTERFACE char * |
421 | symname (pointer p) |
422 | symname (pointer p) |
422 | { |
423 | { |
423 | return strvalue (car (p)); |
424 | return strvalue (p); |
424 | } |
425 | } |
425 | |
426 | |
426 | #if USE_PLIST |
427 | #if USE_PLIST |
|
|
428 | #error plists are broken because symbols are no longer pairs |
|
|
429 | #define symprop(p) cdr(p) |
427 | SCHEME_EXPORT INLINE int |
430 | SCHEME_EXPORT int |
428 | hasprop (pointer p) |
431 | hasprop (pointer p) |
429 | { |
432 | { |
430 | return typeflag (p) & T_SYMBOL; |
433 | return typeflag (p) & T_SYMBOL; |
431 | } |
434 | } |
432 | |
|
|
433 | # define symprop(p) cdr(p) |
|
|
434 | #endif |
435 | #endif |
435 | |
436 | |
436 | INTERFACE INLINE int |
437 | INTERFACE int |
437 | is_syntax (pointer p) |
438 | is_syntax (pointer p) |
438 | { |
439 | { |
439 | return typeflag (p) & T_SYNTAX; |
440 | return typeflag (p) & T_SYNTAX; |
440 | } |
441 | } |
441 | |
442 | |
442 | INTERFACE INLINE int |
443 | INTERFACE int |
443 | is_proc (pointer p) |
444 | is_proc (pointer p) |
444 | { |
445 | { |
445 | return type (p) == T_PROC; |
446 | return type (p) == T_PROC; |
446 | } |
447 | } |
447 | |
448 | |
448 | INTERFACE INLINE int |
449 | INTERFACE int |
449 | is_foreign (pointer p) |
450 | is_foreign (pointer p) |
450 | { |
451 | { |
451 | return type (p) == T_FOREIGN; |
452 | return type (p) == T_FOREIGN; |
452 | } |
453 | } |
453 | |
454 | |
454 | INTERFACE INLINE char * |
455 | INTERFACE char * |
455 | syntaxname (pointer p) |
456 | syntaxname (pointer p) |
456 | { |
457 | { |
457 | return strvalue (car (p)); |
458 | return strvalue (p); |
458 | } |
459 | } |
459 | |
460 | |
460 | #define procnum(p) ivalue (p) |
461 | #define procnum(p) ivalue_unchecked (p) |
461 | static const char *procname (pointer x); |
462 | static const char *procname (pointer x); |
462 | |
463 | |
463 | INTERFACE INLINE int |
464 | INTERFACE int |
464 | is_closure (pointer p) |
465 | is_closure (pointer p) |
465 | { |
466 | { |
466 | return type (p) == T_CLOSURE; |
467 | return type (p) == T_CLOSURE; |
467 | } |
468 | } |
468 | |
469 | |
469 | INTERFACE INLINE int |
470 | INTERFACE int |
470 | is_macro (pointer p) |
471 | is_macro (pointer p) |
471 | { |
472 | { |
472 | return type (p) == T_MACRO; |
473 | return type (p) == T_MACRO; |
473 | } |
474 | } |
474 | |
475 | |
475 | INTERFACE INLINE pointer |
476 | INTERFACE pointer |
476 | closure_code (pointer p) |
477 | closure_code (pointer p) |
477 | { |
478 | { |
478 | return car (p); |
479 | return car (p); |
479 | } |
480 | } |
480 | |
481 | |
481 | INTERFACE INLINE pointer |
482 | INTERFACE pointer |
482 | closure_env (pointer p) |
483 | closure_env (pointer p) |
483 | { |
484 | { |
484 | return cdr (p); |
485 | return cdr (p); |
485 | } |
486 | } |
486 | |
487 | |
487 | INTERFACE INLINE int |
488 | INTERFACE int |
488 | is_continuation (pointer p) |
489 | is_continuation (pointer p) |
489 | { |
490 | { |
490 | return type (p) == T_CONTINUATION; |
491 | return type (p) == T_CONTINUATION; |
491 | } |
492 | } |
492 | |
493 | |
493 | #define cont_dump(p) cdr (p) |
494 | #define cont_dump(p) cdr (p) |
494 | #define set_cont_dump(p,v) set_cdr ((p), (v)) |
495 | #define set_cont_dump(p,v) set_cdr ((p), (v)) |
495 | |
496 | |
496 | /* To do: promise should be forced ONCE only */ |
497 | /* To do: promise should be forced ONCE only */ |
497 | INTERFACE INLINE int |
498 | INTERFACE int |
498 | is_promise (pointer p) |
499 | is_promise (pointer p) |
499 | { |
500 | { |
500 | return type (p) == T_PROMISE; |
501 | return type (p) == T_PROMISE; |
501 | } |
502 | } |
502 | |
503 | |
503 | INTERFACE INLINE int |
504 | INTERFACE int |
504 | is_environment (pointer p) |
505 | is_environment (pointer p) |
505 | { |
506 | { |
506 | return type (p) == T_ENVIRONMENT; |
507 | return type (p) == T_ENVIRONMENT; |
507 | } |
508 | } |
508 | |
509 | |
… | |
… | |
510 | |
511 | |
511 | #define is_atom(p) (typeflag (p) & T_ATOM) |
512 | #define is_atom(p) (typeflag (p) & T_ATOM) |
512 | #define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) |
513 | #define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) |
513 | #define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM) |
514 | #define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM) |
514 | |
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 |
515 | #define is_mark(p) (typeflag (p) & T_MARK) |
521 | #define is_mark(p) (typeflag (p) & T_MARK) |
516 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
522 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
517 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
523 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
|
|
524 | #endif |
518 | |
525 | |
519 | INTERFACE INLINE int |
526 | INTERFACE int |
520 | is_immutable (pointer p) |
527 | is_immutable (pointer p) |
521 | { |
528 | { |
522 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
529 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
523 | } |
530 | } |
524 | |
531 | |
525 | INTERFACE INLINE void |
532 | INTERFACE void |
526 | setimmutable (pointer p) |
533 | setimmutable (pointer p) |
527 | { |
534 | { |
528 | #if USE_ERROR_CHECKING |
535 | #if USE_ERROR_CHECKING |
529 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
536 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
530 | #endif |
537 | #endif |
531 | } |
538 | } |
532 | |
539 | |
|
|
540 | /* Result is: |
|
|
541 | proper list: length |
|
|
542 | circular list: -1 |
|
|
543 | not even a pair: -2 |
|
|
544 | dotted list: -2 minus length before dot |
|
|
545 | */ |
|
|
546 | ecb_hot INTERFACE int |
|
|
547 | list_length (SCHEME_P_ pointer a) |
|
|
548 | { |
|
|
549 | int i = 0; |
|
|
550 | pointer slow, fast; |
|
|
551 | |
|
|
552 | slow = fast = a; |
|
|
553 | |
|
|
554 | while (1) |
|
|
555 | { |
|
|
556 | if (fast == NIL) |
|
|
557 | return i; |
|
|
558 | |
|
|
559 | if (!is_pair (fast)) |
|
|
560 | return -2 - i; |
|
|
561 | |
|
|
562 | fast = cdr (fast); |
|
|
563 | ++i; |
|
|
564 | |
|
|
565 | if (fast == NIL) |
|
|
566 | return i; |
|
|
567 | |
|
|
568 | if (!is_pair (fast)) |
|
|
569 | return -2 - i; |
|
|
570 | |
|
|
571 | ++i; |
|
|
572 | fast = cdr (fast); |
|
|
573 | |
|
|
574 | /* Safe because we would have already returned if `fast' |
|
|
575 | encountered a non-pair. */ |
|
|
576 | slow = cdr (slow); |
|
|
577 | |
|
|
578 | if (fast == slow) |
|
|
579 | { |
|
|
580 | /* the fast pointer has looped back around and caught up |
|
|
581 | with the slow pointer, hence the structure is circular, |
|
|
582 | not of finite length, and therefore not a list */ |
|
|
583 | return -1; |
|
|
584 | } |
|
|
585 | } |
|
|
586 | } |
|
|
587 | |
|
|
588 | INTERFACE int |
|
|
589 | is_list (SCHEME_P_ pointer a) |
|
|
590 | { |
|
|
591 | return list_length (SCHEME_A_ a) >= 0; |
|
|
592 | } |
|
|
593 | |
533 | #if USE_CHAR_CLASSIFIERS |
594 | #if USE_CHAR_CLASSIFIERS |
534 | static INLINE int |
595 | |
|
|
596 | ecb_inline int |
535 | Cisalpha (int c) |
597 | Cisalpha (int c) |
536 | { |
598 | { |
537 | return isascii (c) && isalpha (c); |
599 | return isascii (c) && isalpha (c); |
538 | } |
600 | } |
539 | |
601 | |
540 | static INLINE int |
602 | ecb_inline int |
541 | Cisdigit (int c) |
603 | Cisdigit (int c) |
542 | { |
604 | { |
543 | return isascii (c) && isdigit (c); |
605 | return isascii (c) && isdigit (c); |
544 | } |
606 | } |
545 | |
607 | |
546 | static INLINE int |
608 | ecb_inline int |
547 | Cisspace (int c) |
609 | Cisspace (int c) |
548 | { |
610 | { |
549 | return isascii (c) && isspace (c); |
611 | return isascii (c) && isspace (c); |
550 | } |
612 | } |
551 | |
613 | |
552 | static INLINE int |
614 | ecb_inline int |
553 | Cisupper (int c) |
615 | Cisupper (int c) |
554 | { |
616 | { |
555 | return isascii (c) && isupper (c); |
617 | return isascii (c) && isupper (c); |
556 | } |
618 | } |
557 | |
619 | |
558 | static INLINE int |
620 | ecb_inline int |
559 | Cislower (int c) |
621 | Cislower (int c) |
560 | { |
622 | { |
561 | return isascii (c) && islower (c); |
623 | return isascii (c) && islower (c); |
562 | } |
624 | } |
563 | #endif |
625 | #endif |
… | |
… | |
596 | "gs", |
658 | "gs", |
597 | "rs", |
659 | "rs", |
598 | "us" |
660 | "us" |
599 | }; |
661 | }; |
600 | |
662 | |
601 | static int |
663 | ecb_cold static int |
602 | is_ascii_name (const char *name, int *pc) |
664 | is_ascii_name (const char *name, int *pc) |
603 | { |
665 | { |
604 | int i; |
666 | int i; |
605 | |
667 | |
606 | for (i = 0; i < 32; i++) |
668 | for (i = 0; i < 32; i++) |
… | |
… | |
624 | #endif |
686 | #endif |
625 | |
687 | |
626 | static int file_push (SCHEME_P_ const char *fname); |
688 | static int file_push (SCHEME_P_ const char *fname); |
627 | static void file_pop (SCHEME_P); |
689 | static void file_pop (SCHEME_P); |
628 | static int file_interactive (SCHEME_P); |
690 | static int file_interactive (SCHEME_P); |
629 | static INLINE int is_one_of (char *s, int c); |
691 | ecb_inline int is_one_of (const char *s, int c); |
630 | static int alloc_cellseg (SCHEME_P_ int n); |
692 | static int alloc_cellseg (SCHEME_P); |
631 | static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); |
693 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
632 | static void finalize_cell (SCHEME_P_ pointer a); |
694 | static void finalize_cell (SCHEME_P_ pointer a); |
633 | static int count_consecutive_cells (pointer x, int needed); |
|
|
634 | 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); |
635 | static pointer mk_number (SCHEME_P_ const num n); |
696 | static pointer mk_number (SCHEME_P_ const num n); |
636 | 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); |
637 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
698 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
638 | static pointer mk_atom (SCHEME_P_ char *q); |
699 | static pointer mk_atom (SCHEME_P_ char *q); |
639 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
700 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
640 | |
701 | |
|
|
702 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
703 | |
641 | #if USE_PORTS |
704 | #if USE_PORTS |
642 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
643 | 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); |
644 | static pointer port_from_file (SCHEME_P_ int, int prop); |
706 | static pointer port_from_file (SCHEME_P_ int, int prop); |
645 | 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); |
646 | 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); |
647 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
709 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
648 | 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); |
649 | static void port_close (SCHEME_P_ pointer p, int flag); |
711 | static void port_close (SCHEME_P_ pointer p, int flag); |
650 | #endif |
712 | #endif |
|
|
713 | |
651 | static void mark (pointer a); |
714 | static void mark (pointer a); |
652 | static void gc (SCHEME_P_ pointer a, pointer b); |
715 | static void gc (SCHEME_P_ pointer a, pointer b); |
653 | static int basic_inchar (port *pt); |
716 | static int basic_inchar (port *pt); |
654 | static int inchar (SCHEME_P); |
717 | static int inchar (SCHEME_P); |
655 | static void backchar (SCHEME_P_ int c); |
718 | static void backchar (SCHEME_P_ int c); |
656 | static char *readstr_upto (SCHEME_P_ char *delim); |
719 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
657 | static pointer readstrexp (SCHEME_P); |
720 | static pointer readstrexp (SCHEME_P_ char delim); |
658 | static INLINE int skipspace (SCHEME_P); |
721 | static int skipspace (SCHEME_P); |
659 | static int token (SCHEME_P); |
722 | static int token (SCHEME_P); |
660 | static void printslashstring (SCHEME_P_ char *s, int len); |
723 | static void printslashstring (SCHEME_P_ char *s, int len); |
661 | 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); |
662 | static void printatom (SCHEME_P_ pointer l, int f); |
725 | static void printatom (SCHEME_P_ pointer l, int f); |
663 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
726 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
… | |
… | |
679 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
742 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
680 | static void assign_syntax (SCHEME_P_ const char *name); |
743 | static void assign_syntax (SCHEME_P_ const char *name); |
681 | static int syntaxnum (pointer p); |
744 | static int syntaxnum (pointer p); |
682 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
745 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
683 | |
746 | |
|
|
747 | static IVALUE |
|
|
748 | ivalue (pointer x) |
|
|
749 | { |
|
|
750 | return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x); |
|
|
751 | } |
|
|
752 | |
|
|
753 | static RVALUE |
|
|
754 | rvalue (pointer x) |
|
|
755 | { |
|
|
756 | return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x); |
|
|
757 | } |
|
|
758 | |
|
|
759 | INTERFACE num |
|
|
760 | nvalue (pointer x) |
|
|
761 | { |
|
|
762 | num n; |
|
|
763 | |
|
|
764 | num_set_fixnum (n, is_integer (x)); |
|
|
765 | |
|
|
766 | if (num_is_fixnum (n)) |
|
|
767 | num_set_ivalue (n, ivalue_unchecked (x)); |
|
|
768 | else |
|
|
769 | num_set_rvalue (n, rvalue_unchecked (x)); |
|
|
770 | |
|
|
771 | return n; |
|
|
772 | } |
|
|
773 | |
684 | static num |
774 | static num |
685 | num_op (enum num_op op, num a, num b) |
775 | num_op (enum num_op op, num a, num b) |
686 | { |
776 | { |
687 | num ret; |
777 | num ret; |
688 | |
778 | |
689 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
779 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
690 | |
780 | |
691 | if (num_is_fixnum (ret)) |
781 | if (num_is_fixnum (ret)) |
692 | { |
782 | { |
693 | IVALUE av = num_get_ivalue (a); |
|
|
694 | IVALUE bv = num_get_ivalue (b); |
|
|
695 | |
|
|
696 | switch (op) |
783 | switch (op) |
697 | { |
784 | { |
698 | case NUM_ADD: av += bv; break; |
785 | case NUM_ADD: a.ivalue += b.ivalue; break; |
699 | case NUM_SUB: av -= bv; break; |
786 | case NUM_SUB: a.ivalue -= b.ivalue; break; |
700 | case NUM_MUL: av *= bv; break; |
787 | case NUM_MUL: a.ivalue *= b.ivalue; break; |
701 | case NUM_INTDIV: av /= bv; break; |
788 | case NUM_INTDIV: a.ivalue /= b.ivalue; break; |
702 | } |
789 | } |
703 | |
790 | |
704 | num_set_ivalue (ret, av); |
791 | num_set_ivalue (ret, a.ivalue); |
705 | } |
792 | } |
|
|
793 | #if USE_REAL |
706 | else |
794 | else |
707 | { |
795 | { |
708 | RVALUE av = num_get_rvalue (a); |
|
|
709 | RVALUE bv = num_get_rvalue (b); |
|
|
710 | |
|
|
711 | switch (op) |
796 | switch (op) |
712 | { |
797 | { |
713 | case NUM_ADD: av += bv; break; |
798 | case NUM_ADD: a.rvalue += b.rvalue; break; |
714 | case NUM_SUB: av -= bv; break; |
799 | case NUM_SUB: a.rvalue -= b.rvalue; break; |
715 | case NUM_MUL: av *= bv; break; |
800 | case NUM_MUL: a.rvalue *= b.rvalue; break; |
716 | case NUM_INTDIV: av /= bv; break; |
801 | case NUM_INTDIV: a.rvalue /= b.rvalue; break; |
717 | } |
802 | } |
718 | |
803 | |
719 | num_set_rvalue (ret, av); |
804 | num_set_rvalue (ret, a.rvalue); |
720 | } |
805 | } |
|
|
806 | #endif |
721 | |
807 | |
722 | return ret; |
808 | return ret; |
723 | } |
809 | } |
724 | |
810 | |
725 | static num |
811 | static num |
726 | num_div (num a, num b) |
812 | num_div (num a, num b) |
727 | { |
813 | { |
728 | num ret; |
814 | num ret; |
729 | |
815 | |
730 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); |
816 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0); |
731 | |
817 | |
732 | if (num_is_fixnum (ret)) |
818 | if (num_is_fixnum (ret)) |
733 | num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); |
819 | num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b)); |
734 | else |
820 | else |
735 | num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); |
821 | num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); |
736 | |
822 | |
737 | return ret; |
823 | return ret; |
738 | } |
824 | } |
739 | |
825 | |
740 | static num |
826 | static num |
… | |
… | |
742 | { |
828 | { |
743 | num ret; |
829 | num ret; |
744 | long e1, e2, res; |
830 | long e1, e2, res; |
745 | |
831 | |
746 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
832 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
747 | e1 = num_get_ivalue (a); |
833 | e1 = num_ivalue (a); |
748 | e2 = num_get_ivalue (b); |
834 | e2 = num_ivalue (b); |
749 | res = e1 % e2; |
835 | res = e1 % e2; |
750 | |
836 | |
751 | /* remainder should have same sign as second operand */ |
837 | /* remainder should have same sign as second operand */ |
752 | if (res > 0) |
838 | if (res > 0) |
753 | { |
839 | { |
… | |
… | |
769 | { |
855 | { |
770 | num ret; |
856 | num ret; |
771 | long e1, e2, res; |
857 | long e1, e2, res; |
772 | |
858 | |
773 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
859 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
774 | e1 = num_get_ivalue (a); |
860 | e1 = num_ivalue (a); |
775 | e2 = num_get_ivalue (b); |
861 | e2 = num_ivalue (b); |
776 | res = e1 % e2; |
862 | res = e1 % e2; |
777 | |
863 | |
778 | /* modulo should have same sign as second operand */ |
864 | /* modulo should have same sign as second operand */ |
779 | if (res * e2 < 0) |
865 | if (res * e2 < 0) |
780 | res += e2; |
866 | res += e2; |
781 | |
867 | |
782 | num_set_ivalue (ret, res); |
868 | num_set_ivalue (ret, res); |
783 | return ret; |
869 | return ret; |
784 | } |
870 | } |
785 | |
871 | |
786 | /* this completely disrespects NaNs */ |
872 | /* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */ |
787 | static int |
873 | static int |
788 | num_cmp (num a, num b) |
874 | num_cmp (num a, num b) |
789 | { |
875 | { |
790 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
876 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
791 | int ret; |
877 | int ret; |
792 | |
878 | |
793 | if (is_fixnum) |
879 | if (is_fixnum) |
794 | { |
880 | { |
795 | IVALUE av = num_get_ivalue (a); |
881 | IVALUE av = num_ivalue (a); |
796 | IVALUE bv = num_get_ivalue (b); |
882 | IVALUE bv = num_ivalue (b); |
797 | |
883 | |
798 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
884 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
799 | } |
885 | } |
800 | else |
886 | else |
801 | { |
887 | { |
802 | RVALUE av = num_get_rvalue (a); |
888 | RVALUE av = num_rvalue (a); |
803 | RVALUE bv = num_get_rvalue (b); |
889 | RVALUE bv = num_rvalue (b); |
804 | |
890 | |
805 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
891 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
806 | } |
892 | } |
807 | |
893 | |
808 | return ret; |
894 | return ret; |
809 | } |
895 | } |
810 | |
|
|
811 | #if USE_MATH |
|
|
812 | |
|
|
813 | /* Round to nearest. Round to even if midway */ |
|
|
814 | static double |
|
|
815 | round_per_R5RS (double x) |
|
|
816 | { |
|
|
817 | double fl = floor (x); |
|
|
818 | double ce = ceil (x); |
|
|
819 | double dfl = x - fl; |
|
|
820 | double dce = ce - x; |
|
|
821 | |
|
|
822 | if (dfl > dce) |
|
|
823 | return ce; |
|
|
824 | else if (dfl < dce) |
|
|
825 | return fl; |
|
|
826 | else |
|
|
827 | { |
|
|
828 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
|
|
829 | return fl; |
|
|
830 | else |
|
|
831 | return ce; |
|
|
832 | } |
|
|
833 | } |
|
|
834 | #endif |
|
|
835 | |
896 | |
836 | static int |
897 | static int |
837 | is_zero_rvalue (RVALUE x) |
898 | is_zero_rvalue (RVALUE x) |
838 | { |
899 | { |
|
|
900 | return x == 0; |
|
|
901 | #if 0 |
839 | #if USE_REAL |
902 | #if USE_REAL |
840 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
903 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
841 | #else |
904 | #else |
842 | return x == 0; |
905 | return x == 0; |
843 | #endif |
906 | #endif |
|
|
907 | #endif |
844 | } |
908 | } |
845 | |
909 | |
846 | /* allocate new cell segment */ |
910 | /* allocate new cell segment */ |
847 | static int |
911 | ecb_cold static int |
848 | alloc_cellseg (SCHEME_P_ int n) |
912 | alloc_cellseg (SCHEME_P) |
849 | { |
913 | { |
850 | pointer newp; |
914 | struct cell *newp; |
851 | pointer last; |
915 | struct cell *last; |
852 | pointer p; |
916 | struct cell *p; |
853 | char *cp; |
917 | char *cp; |
854 | long i; |
918 | long i; |
855 | int k; |
919 | int k; |
856 | |
920 | |
857 | static int segsize = CELL_SEGSIZE >> 1; |
921 | static int segsize = CELL_SEGSIZE >> 1; |
858 | segsize <<= 1; |
922 | segsize <<= 1; |
859 | |
923 | |
860 | for (k = 0; k < n; k++) |
|
|
861 | { |
|
|
862 | if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1) |
|
|
863 | return k; |
|
|
864 | |
|
|
865 | cp = malloc (segsize * sizeof (struct cell)); |
924 | cp = malloc (segsize * sizeof (struct cell)); |
866 | |
925 | |
867 | if (!cp && USE_ERROR_CHECKING) |
926 | if (!cp && USE_ERROR_CHECKING) |
868 | return k; |
927 | return k; |
869 | |
928 | |
870 | i = ++SCHEME_V->last_cell_seg; |
929 | i = ++SCHEME_V->last_cell_seg; |
871 | SCHEME_V->alloc_seg[i] = cp; |
|
|
872 | |
930 | |
873 | /* insert new segment in address order */ |
931 | newp = (struct cell *)cp; |
874 | newp = (pointer)cp; |
|
|
875 | SCHEME_V->cell_seg[i] = newp; |
932 | SCHEME_V->cell_seg[i] = newp; |
876 | SCHEME_V->cell_segsize[i] = segsize; |
933 | SCHEME_V->cell_segsize[i] = segsize; |
877 | |
|
|
878 | //TODO: insert, not swap |
|
|
879 | while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i]) |
|
|
880 | { |
|
|
881 | p = SCHEME_V->cell_seg[i]; |
|
|
882 | SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1]; |
|
|
883 | SCHEME_V->cell_seg[i - 1] = p; |
|
|
884 | |
|
|
885 | k = SCHEME_V->cell_segsize[i]; |
|
|
886 | SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1]; |
|
|
887 | SCHEME_V->cell_segsize[i - 1] = k; |
|
|
888 | |
|
|
889 | --i; |
|
|
890 | } |
|
|
891 | |
|
|
892 | SCHEME_V->fcells += segsize; |
934 | SCHEME_V->fcells += segsize; |
893 | last = newp + segsize - 1; |
935 | last = newp + segsize - 1; |
894 | |
936 | |
895 | for (p = newp; p <= last; p++) |
937 | for (p = newp; p <= last; p++) |
896 | { |
938 | { |
|
|
939 | pointer cp = POINTER (p); |
|
|
940 | clrmark (cp); |
897 | set_typeflag (p, T_FREE); |
941 | set_typeflag (cp, T_PAIR); |
898 | set_car (p, NIL); |
942 | set_car (cp, NIL); |
899 | set_cdr (p, p + 1); |
943 | set_cdr (cp, POINTER (p + 1)); |
900 | } |
944 | } |
901 | |
945 | |
902 | /* insert new cells in address order on free list */ |
|
|
903 | if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) |
|
|
904 | { |
|
|
905 | set_cdr (last, SCHEME_V->free_cell); |
946 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
906 | SCHEME_V->free_cell = newp; |
947 | SCHEME_V->free_cell = POINTER (newp); |
907 | } |
|
|
908 | else |
|
|
909 | { |
|
|
910 | p = SCHEME_V->free_cell; |
|
|
911 | |
948 | |
912 | while (cdr (p) != NIL && newp > cdr (p)) |
|
|
913 | p = cdr (p); |
|
|
914 | |
|
|
915 | set_cdr (last, cdr (p)); |
|
|
916 | set_cdr (p, newp); |
|
|
917 | } |
|
|
918 | } |
|
|
919 | |
|
|
920 | return n; |
949 | return 1; |
921 | } |
950 | } |
922 | |
951 | |
923 | /* get new cell. parameter a, b is marked by gc. */ |
952 | /* get new cell. parameter a, b is marked by gc. */ |
924 | static INLINE pointer |
953 | ecb_inline pointer |
925 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
954 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
926 | { |
955 | { |
927 | if (ecb_expect_false (SCHEME_V->free_cell == NIL)) |
956 | if (ecb_expect_false (SCHEME_V->free_cell == NIL)) |
928 | { |
957 | { |
929 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
958 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
930 | return S_SINK; |
959 | return S_SINK; |
931 | |
960 | |
932 | if (SCHEME_V->free_cell == NIL) |
961 | if (SCHEME_V->free_cell == NIL) |
933 | { |
962 | { |
934 | const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; |
963 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 2; |
935 | |
964 | |
936 | gc (SCHEME_A_ a, b); |
965 | gc (SCHEME_A_ a, b); |
937 | |
966 | |
938 | 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) |
939 | { |
968 | { |
940 | /* if only a few recovered, get more to avoid fruitless gc's */ |
969 | /* if only a few recovered, get more to avoid fruitless gc's */ |
941 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
970 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
942 | { |
971 | { |
943 | #if USE_ERROR_CHECKING |
972 | #if USE_ERROR_CHECKING |
944 | SCHEME_V->no_memory = 1; |
973 | SCHEME_V->no_memory = 1; |
945 | return S_SINK; |
974 | return S_SINK; |
946 | #endif |
975 | #endif |
… | |
… | |
958 | } |
987 | } |
959 | } |
988 | } |
960 | |
989 | |
961 | /* To retain recent allocs before interpreter knows about them - |
990 | /* To retain recent allocs before interpreter knows about them - |
962 | Tehom */ |
991 | Tehom */ |
963 | |
992 | ecb_hot static void |
964 | static void |
|
|
965 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
993 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
966 | { |
994 | { |
967 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
995 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
968 | |
996 | |
969 | set_typeflag (holder, T_PAIR); |
997 | set_typeflag (holder, T_PAIR); |
… | |
… | |
971 | set_car (holder, recent); |
999 | set_car (holder, recent); |
972 | set_cdr (holder, car (S_SINK)); |
1000 | set_cdr (holder, car (S_SINK)); |
973 | set_car (S_SINK, holder); |
1001 | set_car (S_SINK, holder); |
974 | } |
1002 | } |
975 | |
1003 | |
976 | static pointer |
1004 | ecb_hot static pointer |
977 | get_cell (SCHEME_P_ pointer a, pointer b) |
1005 | get_cell (SCHEME_P_ pointer a, pointer b) |
978 | { |
1006 | { |
979 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
1007 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
980 | |
1008 | |
981 | /* 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 |
… | |
… | |
990 | } |
1018 | } |
991 | |
1019 | |
992 | static pointer |
1020 | static pointer |
993 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1021 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
994 | { |
1022 | { |
995 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1023 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
996 | pointer *e = malloc (len * sizeof (pointer)); |
1024 | pointer *e = malloc (len * sizeof (pointer)); |
997 | |
1025 | |
998 | if (!e && USE_ERROR_CHECKING) |
1026 | if (!e && USE_ERROR_CHECKING) |
999 | return S_SINK; |
1027 | return S_SINK; |
1000 | |
1028 | |
1001 | /* Record it as a vector so that gc understands it. */ |
1029 | /* Record it as a vector so that gc understands it. */ |
1002 | set_typeflag (v, T_VECTOR | T_ATOM); |
1030 | set_typeflag (v, T_VECTOR | T_ATOM); |
1003 | |
1031 | |
1004 | v->object.vector.vvalue = e; |
1032 | CELL(v)->object.vector.vvalue = e; |
1005 | v->object.vector.length = len; |
1033 | CELL(v)->object.vector.length = len; |
1006 | fill_vector (v, init); |
1034 | fill_vector (v, 0, init); |
1007 | push_recent_alloc (SCHEME_A_ v, NIL); |
1035 | push_recent_alloc (SCHEME_A_ v, NIL); |
1008 | |
1036 | |
1009 | return v; |
1037 | return v; |
1010 | } |
1038 | } |
1011 | |
1039 | |
1012 | static INLINE void |
1040 | ecb_inline void |
1013 | ok_to_freely_gc (SCHEME_P) |
1041 | ok_to_freely_gc (SCHEME_P) |
1014 | { |
1042 | { |
1015 | set_car (S_SINK, NIL); |
1043 | set_car (S_SINK, NIL); |
1016 | } |
1044 | } |
1017 | |
1045 | |
… | |
… | |
1019 | static void |
1047 | static void |
1020 | check_cell_alloced (pointer p, int expect_alloced) |
1048 | check_cell_alloced (pointer p, int expect_alloced) |
1021 | { |
1049 | { |
1022 | /* 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. */ |
1023 | if (typeflag (p) & !expect_alloced) |
1051 | if (typeflag (p) & !expect_alloced) |
1024 | xwrstr ("Cell is already allocated!\n"); |
1052 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1025 | |
1053 | |
1026 | if (!(typeflag (p)) & expect_alloced) |
1054 | if (!(typeflag (p)) & expect_alloced) |
1027 | xwrstr ("Cell is not allocated!\n"); |
1055 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1028 | } |
1056 | } |
1029 | |
1057 | |
1030 | static void |
1058 | static void |
1031 | check_range_alloced (pointer p, int n, int expect_alloced) |
1059 | check_range_alloced (pointer p, int n, int expect_alloced) |
1032 | { |
1060 | { |
… | |
… | |
1038 | #endif |
1066 | #endif |
1039 | |
1067 | |
1040 | /* Medium level cell allocation */ |
1068 | /* Medium level cell allocation */ |
1041 | |
1069 | |
1042 | /* get new cons cell */ |
1070 | /* get new cons cell */ |
1043 | pointer |
1071 | ecb_hot static pointer |
1044 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1072 | xcons (SCHEME_P_ pointer a, pointer b) |
1045 | { |
1073 | { |
1046 | pointer x = get_cell (SCHEME_A_ a, b); |
1074 | pointer x = get_cell (SCHEME_A_ a, b); |
1047 | |
1075 | |
1048 | set_typeflag (x, T_PAIR); |
1076 | set_typeflag (x, T_PAIR); |
1049 | |
|
|
1050 | if (immutable) |
|
|
1051 | setimmutable (x); |
|
|
1052 | |
1077 | |
1053 | set_car (x, a); |
1078 | set_car (x, a); |
1054 | set_cdr (x, b); |
1079 | set_cdr (x, b); |
1055 | |
1080 | |
1056 | return x; |
1081 | return x; |
1057 | } |
1082 | } |
1058 | |
1083 | |
|
|
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 |
|
|
1096 | generate_symbol (SCHEME_P_ const char *name) |
|
|
1097 | { |
|
|
1098 | pointer x = mk_string (SCHEME_A_ name); |
|
|
1099 | setimmutable (x); |
|
|
1100 | set_typeflag (x, T_SYMBOL | T_ATOM); |
|
|
1101 | return x; |
|
|
1102 | } |
|
|
1103 | |
1059 | /* ========== oblist implementation ========== */ |
1104 | /* ========== oblist implementation ========== */ |
1060 | |
1105 | |
1061 | #ifndef USE_OBJECT_LIST |
1106 | #ifndef USE_OBJECT_LIST |
1062 | |
1107 | |
|
|
1108 | static int |
1063 | static int hash_fn (const char *key, int table_size); |
1109 | hash_fn (const char *key, int table_size) |
|
|
1110 | { |
|
|
1111 | const unsigned char *p = (unsigned char *)key; |
|
|
1112 | uint32_t hash = 2166136261U; |
1064 | |
1113 | |
|
|
1114 | while (*p) |
|
|
1115 | hash = (hash ^ *p++) * 16777619; |
|
|
1116 | |
|
|
1117 | return hash % table_size; |
|
|
1118 | } |
|
|
1119 | |
1065 | static pointer |
1120 | ecb_cold static pointer |
1066 | oblist_initial_value (SCHEME_P) |
1121 | oblist_initial_value (SCHEME_P) |
1067 | { |
1122 | { |
1068 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1123 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1069 | } |
1124 | } |
1070 | |
1125 | |
1071 | /* returns the new symbol */ |
1126 | /* returns the new symbol */ |
1072 | static pointer |
1127 | ecb_cold static pointer |
1073 | oblist_add_by_name (SCHEME_P_ const char *name) |
1128 | oblist_add_by_name (SCHEME_P_ const char *name) |
1074 | { |
1129 | { |
1075 | int location; |
1130 | pointer x = generate_symbol (SCHEME_A_ name); |
1076 | |
|
|
1077 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1078 | set_typeflag (x, T_SYMBOL); |
|
|
1079 | setimmutable (car (x)); |
|
|
1080 | |
|
|
1081 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1131 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1082 | set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); |
1132 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1083 | return x; |
1133 | return x; |
1084 | } |
1134 | } |
1085 | |
1135 | |
1086 | static INLINE pointer |
1136 | ecb_cold static pointer |
1087 | oblist_find_by_name (SCHEME_P_ const char *name) |
1137 | oblist_find_by_name (SCHEME_P_ const char *name) |
1088 | { |
1138 | { |
1089 | int location; |
1139 | int location; |
1090 | pointer x; |
1140 | pointer x; |
1091 | char *s; |
1141 | char *s; |
1092 | |
1142 | |
1093 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1143 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1094 | |
1144 | |
1095 | for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1145 | for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1096 | { |
1146 | { |
1097 | s = symname (car (x)); |
1147 | s = symname (car (x)); |
1098 | |
1148 | |
1099 | /* case-insensitive, per R5RS section 2 */ |
1149 | /* case-insensitive, per R5RS section 2 */ |
1100 | if (stricmp (name, s) == 0) |
1150 | if (stricmp (name, s) == 0) |
… | |
… | |
1102 | } |
1152 | } |
1103 | |
1153 | |
1104 | return NIL; |
1154 | return NIL; |
1105 | } |
1155 | } |
1106 | |
1156 | |
1107 | static pointer |
1157 | ecb_cold static pointer |
1108 | oblist_all_symbols (SCHEME_P) |
1158 | oblist_all_symbols (SCHEME_P) |
1109 | { |
1159 | { |
1110 | int i; |
1160 | int i; |
1111 | pointer x; |
1161 | pointer x; |
1112 | pointer ob_list = NIL; |
1162 | pointer ob_list = NIL; |
1113 | |
1163 | |
1114 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1164 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1115 | for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1165 | for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1116 | ob_list = cons (x, ob_list); |
1166 | ob_list = cons (x, ob_list); |
1117 | |
1167 | |
1118 | return ob_list; |
1168 | return ob_list; |
1119 | } |
1169 | } |
1120 | |
1170 | |
1121 | #else |
1171 | #else |
1122 | |
1172 | |
1123 | static pointer |
1173 | ecb_cold static pointer |
1124 | oblist_initial_value (SCHEME_P) |
1174 | oblist_initial_value (SCHEME_P) |
1125 | { |
1175 | { |
1126 | return NIL; |
1176 | return NIL; |
1127 | } |
1177 | } |
1128 | |
1178 | |
1129 | static INLINE pointer |
1179 | ecb_cold static pointer |
1130 | oblist_find_by_name (SCHEME_P_ const char *name) |
1180 | oblist_find_by_name (SCHEME_P_ const char *name) |
1131 | { |
1181 | { |
1132 | pointer x; |
1182 | pointer x; |
1133 | char *s; |
1183 | char *s; |
1134 | |
1184 | |
… | |
… | |
1143 | |
1193 | |
1144 | return NIL; |
1194 | return NIL; |
1145 | } |
1195 | } |
1146 | |
1196 | |
1147 | /* returns the new symbol */ |
1197 | /* returns the new symbol */ |
1148 | static pointer |
1198 | ecb_cold static pointer |
1149 | oblist_add_by_name (SCHEME_P_ const char *name) |
1199 | oblist_add_by_name (SCHEME_P_ const char *name) |
1150 | { |
1200 | { |
1151 | pointer x; |
1201 | pointer x = generate_symbol (SCHEME_A_ name); |
1152 | |
|
|
1153 | x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1154 | set_typeflag (x, T_SYMBOL); |
|
|
1155 | setimmutable (car (x)); |
|
|
1156 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1202 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1157 | return x; |
1203 | return x; |
1158 | } |
1204 | } |
1159 | |
1205 | |
1160 | static pointer |
1206 | ecb_cold static pointer |
1161 | oblist_all_symbols (SCHEME_P) |
1207 | oblist_all_symbols (SCHEME_P) |
1162 | { |
1208 | { |
1163 | return SCHEME_V->oblist; |
1209 | return SCHEME_V->oblist; |
1164 | } |
1210 | } |
1165 | |
1211 | |
1166 | #endif |
1212 | #endif |
1167 | |
1213 | |
1168 | #if USE_PORTS |
|
|
1169 | static pointer |
1214 | ecb_cold static pointer |
1170 | mk_port (SCHEME_P_ port *p) |
1215 | mk_port (SCHEME_P_ port *p) |
1171 | { |
1216 | { |
1172 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1217 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1173 | |
1218 | |
1174 | set_typeflag (x, T_PORT | T_ATOM); |
1219 | set_typeflag (x, T_PORT | T_ATOM); |
1175 | x->object.port = p; |
1220 | set_port (x, p); |
1176 | |
1221 | |
1177 | return x; |
1222 | return x; |
1178 | } |
1223 | } |
1179 | #endif |
|
|
1180 | |
1224 | |
1181 | pointer |
1225 | ecb_cold pointer |
1182 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1226 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1183 | { |
1227 | { |
1184 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1228 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1185 | |
1229 | |
1186 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1230 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1187 | x->object.ff = f; |
1231 | CELL(x)->object.ff = f; |
1188 | |
1232 | |
1189 | return x; |
1233 | return x; |
1190 | } |
1234 | } |
1191 | |
1235 | |
1192 | INTERFACE pointer |
1236 | INTERFACE pointer |
1193 | mk_character (SCHEME_P_ int c) |
1237 | mk_character (SCHEME_P_ int c) |
1194 | { |
1238 | { |
1195 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1239 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1196 | |
1240 | |
1197 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1241 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1198 | ivalue_unchecked (x) = c & 0xff; |
1242 | set_ivalue (x, c & 0xff); |
1199 | set_num_integer (x); |
1243 | |
1200 | return x; |
1244 | return x; |
1201 | } |
1245 | } |
1202 | |
1246 | |
1203 | /* get number atom (integer) */ |
1247 | /* get number atom (integer) */ |
1204 | INTERFACE pointer |
1248 | INTERFACE pointer |
1205 | mk_integer (SCHEME_P_ long num) |
1249 | mk_integer (SCHEME_P_ long n) |
1206 | { |
1250 | { |
|
|
1251 | pointer p = 0; |
|
|
1252 | pointer *pp = &p; |
|
|
1253 | |
|
|
1254 | #if USE_INTCACHE |
|
|
1255 | if (n >= INTCACHE_MIN && n <= INTCACHE_MAX) |
|
|
1256 | pp = &SCHEME_V->intcache[n - INTCACHE_MIN]; |
|
|
1257 | #endif |
|
|
1258 | |
|
|
1259 | if (!*pp) |
|
|
1260 | { |
1207 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1261 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1208 | |
1262 | |
1209 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1263 | set_typeflag (x, T_INTEGER | T_ATOM); |
1210 | ivalue_unchecked (x) = num; |
1264 | setimmutable (x); /* shouldn't do anything, doesn't cost anything */ |
1211 | set_num_integer (x); |
1265 | set_ivalue (x, n); |
|
|
1266 | |
|
|
1267 | *pp = x; |
|
|
1268 | } |
|
|
1269 | |
1212 | return x; |
1270 | return *pp; |
1213 | } |
1271 | } |
1214 | |
1272 | |
1215 | INTERFACE pointer |
1273 | INTERFACE pointer |
1216 | mk_real (SCHEME_P_ RVALUE n) |
1274 | mk_real (SCHEME_P_ RVALUE n) |
1217 | { |
1275 | { |
|
|
1276 | #if USE_REAL |
1218 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1277 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1219 | |
1278 | |
1220 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1279 | set_typeflag (x, T_REAL | T_ATOM); |
1221 | rvalue_unchecked (x) = n; |
1280 | set_rvalue (x, n); |
1222 | set_num_real (x); |
1281 | |
1223 | return x; |
1282 | return x; |
|
|
1283 | #else |
|
|
1284 | return mk_integer (SCHEME_A_ n); |
|
|
1285 | #endif |
1224 | } |
1286 | } |
1225 | |
1287 | |
1226 | static pointer |
1288 | static pointer |
1227 | mk_number (SCHEME_P_ const num n) |
1289 | mk_number (SCHEME_P_ const num n) |
1228 | { |
1290 | { |
|
|
1291 | #if USE_REAL |
1229 | if (num_is_fixnum (n)) |
1292 | return num_is_fixnum (n) |
|
|
1293 | ? mk_integer (SCHEME_A_ num_ivalue (n)) |
|
|
1294 | : mk_real (SCHEME_A_ num_rvalue (n)); |
|
|
1295 | #else |
1230 | return mk_integer (SCHEME_A_ num_get_ivalue (n)); |
1296 | return mk_integer (SCHEME_A_ num_ivalue (n)); |
1231 | else |
1297 | #endif |
1232 | return mk_real (SCHEME_A_ num_get_rvalue (n)); |
|
|
1233 | } |
1298 | } |
1234 | |
1299 | |
1235 | /* allocate name to string area */ |
1300 | /* allocate name to string area */ |
1236 | static char * |
1301 | static char * |
1237 | store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) |
1302 | store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) |
… | |
… | |
1243 | SCHEME_V->no_memory = 1; |
1308 | SCHEME_V->no_memory = 1; |
1244 | return SCHEME_V->strbuff; |
1309 | return SCHEME_V->strbuff; |
1245 | } |
1310 | } |
1246 | |
1311 | |
1247 | if (str) |
1312 | if (str) |
1248 | { |
1313 | memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */ |
1249 | int l = strlen (str); |
|
|
1250 | |
|
|
1251 | if (l > len_str) |
|
|
1252 | l = len_str; |
|
|
1253 | |
|
|
1254 | memcpy (q, str, l); |
|
|
1255 | q[l] = 0; |
|
|
1256 | } |
|
|
1257 | else |
1314 | else |
1258 | { |
|
|
1259 | memset (q, fill, len_str); |
1315 | memset (q, fill, len_str); |
|
|
1316 | |
1260 | q[len_str] = 0; |
1317 | q[len_str] = 0; |
1261 | } |
|
|
1262 | |
1318 | |
1263 | return q; |
1319 | return q; |
1264 | } |
1320 | } |
1265 | |
1321 | |
1266 | INTERFACE pointer |
1322 | INTERFACE pointer |
… | |
… | |
1280 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1336 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1281 | |
1337 | |
1282 | set_typeflag (x, T_STRING | T_ATOM); |
1338 | set_typeflag (x, T_STRING | T_ATOM); |
1283 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1339 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1284 | strlength (x) = len; |
1340 | strlength (x) = len; |
|
|
1341 | |
1285 | return x; |
1342 | return x; |
1286 | } |
1343 | } |
1287 | |
1344 | |
1288 | INTERFACE pointer |
1345 | INTERFACE pointer |
1289 | mk_string (SCHEME_P_ const char *str) |
1346 | mk_string (SCHEME_P_ const char *str) |
… | |
… | |
1296 | { |
1353 | { |
1297 | return get_vector_object (SCHEME_A_ len, NIL); |
1354 | return get_vector_object (SCHEME_A_ len, NIL); |
1298 | } |
1355 | } |
1299 | |
1356 | |
1300 | INTERFACE void |
1357 | INTERFACE void |
1301 | fill_vector (pointer vec, pointer obj) |
1358 | fill_vector (pointer vec, uint32_t start, pointer obj) |
1302 | { |
1359 | { |
1303 | int i; |
1360 | int i; |
1304 | |
1361 | |
1305 | for (i = 0; i < vec->object.vector.length; i++) |
1362 | for (i = start; i < veclength (vec); i++) |
1306 | vecvalue (vec)[i] = obj; |
1363 | vecvalue (vec)[i] = obj; |
1307 | } |
1364 | } |
1308 | |
1365 | |
|
|
1366 | INTERFACE void |
|
|
1367 | vector_resize (pointer vec, uint32_t newsize, pointer fill) |
|
|
1368 | { |
|
|
1369 | uint32_t oldsize = veclength (vec); |
|
|
1370 | vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer)); |
|
|
1371 | veclength (vec) = newsize; |
|
|
1372 | fill_vector (vec, oldsize, fill); |
|
|
1373 | } |
|
|
1374 | |
1309 | INTERFACE pointer |
1375 | INTERFACE pointer |
1310 | vector_elem (pointer vec, uint32_t ielem) |
1376 | vector_get (pointer vec, uint32_t ielem) |
1311 | { |
1377 | { |
1312 | return vecvalue(vec)[ielem]; |
1378 | return vecvalue(vec)[ielem]; |
1313 | } |
1379 | } |
1314 | |
1380 | |
1315 | INTERFACE void |
1381 | INTERFACE void |
1316 | set_vector_elem (pointer vec, uint32_t ielem, pointer a) |
1382 | vector_set (pointer vec, uint32_t ielem, pointer a) |
1317 | { |
1383 | { |
1318 | vecvalue(vec)[ielem] = a; |
1384 | vecvalue(vec)[ielem] = a; |
1319 | } |
1385 | } |
1320 | |
1386 | |
1321 | /* get new symbol */ |
1387 | /* get new symbol */ |
… | |
… | |
1329 | x = oblist_add_by_name (SCHEME_A_ name); |
1395 | x = oblist_add_by_name (SCHEME_A_ name); |
1330 | |
1396 | |
1331 | return x; |
1397 | return x; |
1332 | } |
1398 | } |
1333 | |
1399 | |
1334 | INTERFACE pointer |
1400 | ecb_cold INTERFACE pointer |
1335 | gensym (SCHEME_P) |
1401 | gensym (SCHEME_P) |
1336 | { |
1402 | { |
1337 | pointer x; |
1403 | pointer x; |
1338 | |
|
|
1339 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
|
|
1340 | { |
|
|
1341 | char name[40] = "gensym-"; |
1404 | char name[40] = "gensym-"; |
1342 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1405 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1343 | |
1406 | |
1344 | /* first check oblist */ |
1407 | return generate_symbol (SCHEME_A_ name); |
1345 | x = oblist_find_by_name (SCHEME_A_ name); |
1408 | } |
1346 | |
1409 | |
1347 | if (x == NIL) |
1410 | static int |
1348 | { |
1411 | is_gensym (SCHEME_P_ pointer x) |
1349 | x = oblist_add_by_name (SCHEME_A_ name); |
1412 | { |
1350 | return x; |
1413 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1351 | } |
|
|
1352 | } |
|
|
1353 | |
|
|
1354 | return NIL; |
|
|
1355 | } |
1414 | } |
1356 | |
1415 | |
1357 | /* make symbol or number atom from string */ |
1416 | /* make symbol or number atom from string */ |
1358 | static pointer |
1417 | ecb_cold static pointer |
1359 | mk_atom (SCHEME_P_ char *q) |
1418 | mk_atom (SCHEME_P_ char *q) |
1360 | { |
1419 | { |
1361 | char c, *p; |
1420 | char c, *p; |
1362 | int has_dec_point = 0; |
1421 | int has_dec_point = 0; |
1363 | int has_fp_exp = 0; |
1422 | int has_fp_exp = 0; |
… | |
… | |
1413 | } |
1472 | } |
1414 | else if ((c == 'e') || (c == 'E')) |
1473 | else if ((c == 'e') || (c == 'E')) |
1415 | { |
1474 | { |
1416 | if (!has_fp_exp) |
1475 | if (!has_fp_exp) |
1417 | { |
1476 | { |
1418 | has_dec_point = 1; /* decimal point illegal |
1477 | has_dec_point = 1; /* decimal point illegal from now on */ |
1419 | from now on */ |
|
|
1420 | p++; |
1478 | p++; |
1421 | |
1479 | |
1422 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1480 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1423 | continue; |
1481 | continue; |
1424 | } |
1482 | } |
… | |
… | |
1435 | |
1493 | |
1436 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1494 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1437 | } |
1495 | } |
1438 | |
1496 | |
1439 | /* make constant */ |
1497 | /* make constant */ |
1440 | static pointer |
1498 | ecb_cold static pointer |
1441 | mk_sharp_const (SCHEME_P_ char *name) |
1499 | mk_sharp_const (SCHEME_P_ char *name) |
1442 | { |
1500 | { |
1443 | if (!strcmp (name, "t")) |
1501 | if (!strcmp (name, "t")) |
1444 | return S_T; |
1502 | return S_T; |
1445 | else if (!strcmp (name, "f")) |
1503 | else if (!strcmp (name, "f")) |
1446 | return S_F; |
1504 | return S_F; |
1447 | else if (*name == '\\') /* #\w (character) */ |
1505 | else if (*name == '\\') /* #\w (character) */ |
1448 | { |
1506 | { |
1449 | int c; |
1507 | int c; |
1450 | |
1508 | |
|
|
1509 | // TODO: optimise |
1451 | if (stricmp (name + 1, "space") == 0) |
1510 | if (stricmp (name + 1, "space") == 0) |
1452 | c = ' '; |
1511 | c = ' '; |
1453 | else if (stricmp (name + 1, "newline") == 0) |
1512 | else if (stricmp (name + 1, "newline") == 0) |
1454 | c = '\n'; |
1513 | c = '\n'; |
1455 | else if (stricmp (name + 1, "return") == 0) |
1514 | else if (stricmp (name + 1, "return") == 0) |
1456 | c = '\r'; |
1515 | c = '\r'; |
1457 | else if (stricmp (name + 1, "tab") == 0) |
1516 | else if (stricmp (name + 1, "tab") == 0) |
1458 | 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; |
1459 | else if (name[1] == 'x' && name[2] != 0) |
1528 | else if (name[1] == 'x' && name[2] != 0) |
1460 | { |
1529 | { |
1461 | long c1 = strtol (name + 2, 0, 16); |
1530 | long c1 = strtol (name + 2, 0, 16); |
1462 | |
1531 | |
1463 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1532 | if (0 <= c1 && c1 <= UCHAR_MAX) |
… | |
… | |
1477 | return mk_character (SCHEME_A_ c); |
1546 | return mk_character (SCHEME_A_ c); |
1478 | } |
1547 | } |
1479 | else |
1548 | else |
1480 | { |
1549 | { |
1481 | /* identify base by string index */ |
1550 | /* identify base by string index */ |
1482 | const char baseidx[17] = "ffbf" "ffff" "ofdf" "ffff" "x"; |
1551 | const char baseidx[18] = "ffbf" "ffff" "ofdf" "ffff" "x"; |
1483 | char *base = strchr (baseidx, *name); |
1552 | char *base = strchr (baseidx, *name); |
1484 | |
1553 | |
1485 | if (base) |
1554 | if (base && *base) |
1486 | return mk_integer (SCHEME_A_ strtol (name + 1, 0, base - baseidx)); |
1555 | return mk_integer (SCHEME_A_ strtol (name + 1, 0, base - baseidx)); |
1487 | |
1556 | |
1488 | return NIL; |
1557 | return NIL; |
1489 | } |
1558 | } |
1490 | } |
1559 | } |
1491 | |
1560 | |
1492 | /* ========== 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 | } |
1493 | |
1581 | |
1494 | /*-- |
1582 | /*-- |
1495 | * 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, |
1496 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1584 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1497 | * for marking. |
1585 | * for marking. |
1498 | * |
1586 | * |
1499 | * The exception is vectors - vectors are currently marked recursively, |
1587 | * The exception is vectors - vectors are currently marked recursively, |
1500 | * 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 |
1501 | * word of context in the vector |
1589 | * word of context in the vector |
1502 | */ |
1590 | */ |
1503 | static void |
1591 | ecb_hot static void |
1504 | mark (pointer a) |
1592 | mark (pointer a) |
1505 | { |
1593 | { |
1506 | pointer t, q, p; |
1594 | pointer t, q, p; |
1507 | |
1595 | |
1508 | t = 0; |
1596 | t = 0; |
… | |
… | |
1512 | |
1600 | |
1513 | if (ecb_expect_false (is_vector (p))) |
1601 | if (ecb_expect_false (is_vector (p))) |
1514 | { |
1602 | { |
1515 | int i; |
1603 | int i; |
1516 | |
1604 | |
1517 | for (i = 0; i < p->object.vector.length; i++) |
1605 | for (i = 0; i < veclength (p); i++) |
1518 | mark (vecvalue (p)[i]); |
1606 | mark (vecvalue (p)[i]); |
1519 | } |
1607 | } |
1520 | |
1608 | |
1521 | if (is_atom (p)) |
1609 | if (is_atom (p)) |
1522 | goto E6; |
1610 | goto E6; |
… | |
… | |
1565 | p = q; |
1653 | p = q; |
1566 | goto E6; |
1654 | goto E6; |
1567 | } |
1655 | } |
1568 | } |
1656 | } |
1569 | |
1657 | |
|
|
1658 | ecb_hot static void |
|
|
1659 | gc_free (SCHEME_P) |
|
|
1660 | { |
|
|
1661 | int i; |
|
|
1662 | uint32_t total = 0; |
|
|
1663 | |
|
|
1664 | /* Here we scan the cells to build the free-list. */ |
|
|
1665 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1666 | { |
|
|
1667 | struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1668 | struct cell *p; |
|
|
1669 | total += SCHEME_V->cell_segsize [i]; |
|
|
1670 | |
|
|
1671 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
|
|
1672 | { |
|
|
1673 | pointer c = POINTER (p); |
|
|
1674 | |
|
|
1675 | if (is_mark (c)) |
|
|
1676 | clrmark (c); |
|
|
1677 | else |
|
|
1678 | { |
|
|
1679 | /* reclaim cell */ |
|
|
1680 | if (typeflag (c) != T_PAIR) |
|
|
1681 | { |
|
|
1682 | finalize_cell (SCHEME_A_ c); |
|
|
1683 | set_typeflag (c, T_PAIR); |
|
|
1684 | set_car (c, NIL); |
|
|
1685 | } |
|
|
1686 | |
|
|
1687 | ++SCHEME_V->fcells; |
|
|
1688 | set_cdr (c, SCHEME_V->free_cell); |
|
|
1689 | SCHEME_V->free_cell = c; |
|
|
1690 | } |
|
|
1691 | } |
|
|
1692 | } |
|
|
1693 | |
|
|
1694 | if (SCHEME_V->gc_verbose) |
|
|
1695 | { |
|
|
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"); |
|
|
1697 | } |
|
|
1698 | } |
|
|
1699 | |
1570 | /* garbage collection. parameter a, b is marked. */ |
1700 | /* garbage collection. parameter a, b is marked. */ |
1571 | static void |
1701 | ecb_cold static void |
1572 | gc (SCHEME_P_ pointer a, pointer b) |
1702 | gc (SCHEME_P_ pointer a, pointer b) |
1573 | { |
1703 | { |
1574 | pointer p; |
|
|
1575 | int i; |
1704 | int i; |
1576 | |
1705 | |
1577 | if (SCHEME_V->gc_verbose) |
1706 | if (SCHEME_V->gc_verbose) |
1578 | putstr (SCHEME_A_ "gc..."); |
1707 | putstr (SCHEME_A_ "gc..."); |
1579 | |
1708 | |
… | |
… | |
1595 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1724 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1596 | mark (car (S_SINK)); |
1725 | mark (car (S_SINK)); |
1597 | /* Mark any older stuff above nested C calls */ |
1726 | /* Mark any older stuff above nested C calls */ |
1598 | mark (SCHEME_V->c_nest); |
1727 | mark (SCHEME_V->c_nest); |
1599 | |
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]); |
|
|
1734 | #endif |
|
|
1735 | |
1600 | /* mark variables a, b */ |
1736 | /* mark variables a, b */ |
1601 | mark (a); |
1737 | mark (a); |
1602 | mark (b); |
1738 | mark (b); |
1603 | |
1739 | |
1604 | /* garbage collect */ |
1740 | /* garbage collect */ |
1605 | clrmark (NIL); |
1741 | clrmark (NIL); |
1606 | SCHEME_V->fcells = 0; |
1742 | SCHEME_V->fcells = 0; |
1607 | SCHEME_V->free_cell = NIL; |
1743 | SCHEME_V->free_cell = NIL; |
1608 | |
1744 | |
1609 | /* free-list is kept sorted by address so as to maintain consecutive |
|
|
1610 | ranges, if possible, for use with vectors. Here we scan the cells |
|
|
1611 | (which are also kept sorted by address) downwards to build the |
|
|
1612 | free-list in sorted order. |
|
|
1613 | */ |
|
|
1614 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1615 | { |
|
|
1616 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1617 | |
|
|
1618 | while (--p >= SCHEME_V->cell_seg[i]) |
|
|
1619 | { |
|
|
1620 | if (is_mark (p)) |
|
|
1621 | clrmark (p); |
|
|
1622 | else |
|
|
1623 | { |
|
|
1624 | /* reclaim cell */ |
|
|
1625 | if (typeflag (p) != T_FREE) |
|
|
1626 | { |
|
|
1627 | finalize_cell (SCHEME_A_ p); |
|
|
1628 | set_typeflag (p, T_FREE); |
|
|
1629 | set_car (p, NIL); |
|
|
1630 | } |
|
|
1631 | |
|
|
1632 | ++SCHEME_V->fcells; |
|
|
1633 | set_cdr (p, SCHEME_V->free_cell); |
|
|
1634 | SCHEME_V->free_cell = p; |
|
|
1635 | } |
|
|
1636 | } |
|
|
1637 | } |
|
|
1638 | |
|
|
1639 | if (SCHEME_V->gc_verbose) |
1745 | if (SCHEME_V->gc_verbose) |
1640 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
1746 | putstr (SCHEME_A_ "freeing..."); |
1641 | } |
|
|
1642 | |
1747 | |
1643 | static void |
1748 | gc_free (SCHEME_A); |
1644 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1645 | { |
|
|
1646 | /* TODO, fast bitmap check? */ |
|
|
1647 | if (is_string (a)) |
|
|
1648 | free (strvalue (a)); |
|
|
1649 | else if (is_vector (a)) |
|
|
1650 | free (vecvalue (a)); |
|
|
1651 | #if USE_PORTS |
|
|
1652 | else if (is_port (a)) |
|
|
1653 | { |
|
|
1654 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
|
|
1655 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1656 | |
|
|
1657 | free (a->object.port); |
|
|
1658 | } |
|
|
1659 | #endif |
|
|
1660 | } |
1749 | } |
1661 | |
1750 | |
1662 | /* ========== Routines for Reading ========== */ |
1751 | /* ========== Routines for Reading ========== */ |
1663 | |
1752 | |
1664 | static int |
1753 | ecb_cold static int |
1665 | file_push (SCHEME_P_ const char *fname) |
1754 | file_push (SCHEME_P_ const char *fname) |
1666 | { |
1755 | { |
1667 | #if USE_PORTS |
|
|
1668 | int fin; |
1756 | int fin; |
1669 | |
1757 | |
1670 | if (SCHEME_V->file_i == MAXFIL - 1) |
1758 | if (SCHEME_V->file_i == MAXFIL - 1) |
1671 | return 0; |
1759 | return 0; |
1672 | |
1760 | |
… | |
… | |
1678 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1766 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1679 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1767 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1680 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1768 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1681 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1769 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1682 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1770 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1683 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1771 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1684 | |
1772 | |
1685 | #if SHOW_ERROR_LINE |
1773 | #if SHOW_ERROR_LINE |
1686 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1774 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1687 | |
1775 | |
1688 | if (fname) |
1776 | if (fname) |
1689 | 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); |
1690 | #endif |
1778 | #endif |
1691 | } |
1779 | } |
1692 | |
1780 | |
1693 | return fin >= 0; |
1781 | return fin >= 0; |
1694 | |
|
|
1695 | #else |
|
|
1696 | return 1; |
|
|
1697 | #endif |
|
|
1698 | } |
1782 | } |
1699 | |
1783 | |
1700 | static void |
1784 | ecb_cold static void |
1701 | file_pop (SCHEME_P) |
1785 | file_pop (SCHEME_P) |
1702 | { |
1786 | { |
1703 | if (SCHEME_V->file_i != 0) |
1787 | if (SCHEME_V->file_i != 0) |
1704 | { |
1788 | { |
1705 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1789 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1706 | #if USE_PORTS |
1790 | #if USE_PORTS |
1707 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1791 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1708 | #endif |
1792 | #endif |
1709 | SCHEME_V->file_i--; |
1793 | SCHEME_V->file_i--; |
1710 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1794 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1711 | } |
1795 | } |
1712 | } |
1796 | } |
1713 | |
1797 | |
1714 | static int |
1798 | ecb_cold static int |
1715 | file_interactive (SCHEME_P) |
1799 | file_interactive (SCHEME_P) |
1716 | { |
1800 | { |
1717 | #if USE_PORTS |
1801 | #if USE_PORTS |
1718 | return SCHEME_V->file_i == 0 |
1802 | return SCHEME_V->file_i == 0 |
1719 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1803 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1720 | && (SCHEME_V->inport->object.port->kind & port_file); |
1804 | && (port (SCHEME_V->inport)->kind & port_file); |
1721 | #else |
1805 | #else |
1722 | return 0; |
1806 | return 0; |
1723 | #endif |
1807 | #endif |
1724 | } |
1808 | } |
1725 | |
1809 | |
1726 | #if USE_PORTS |
1810 | #if USE_PORTS |
1727 | static port * |
1811 | ecb_cold static port * |
1728 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1812 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1729 | { |
1813 | { |
1730 | int fd; |
1814 | int fd; |
1731 | int flags; |
1815 | int flags; |
1732 | char *rw; |
1816 | char *rw; |
… | |
… | |
1755 | # endif |
1839 | # endif |
1756 | |
1840 | |
1757 | return pt; |
1841 | return pt; |
1758 | } |
1842 | } |
1759 | |
1843 | |
1760 | static pointer |
1844 | ecb_cold static pointer |
1761 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1845 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1762 | { |
1846 | { |
1763 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1847 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1764 | |
1848 | |
1765 | if (!pt && USE_ERROR_CHECKING) |
1849 | if (!pt && USE_ERROR_CHECKING) |
1766 | return NIL; |
1850 | return NIL; |
1767 | |
1851 | |
1768 | return mk_port (SCHEME_A_ pt); |
1852 | return mk_port (SCHEME_A_ pt); |
1769 | } |
1853 | } |
1770 | |
1854 | |
1771 | static port * |
1855 | ecb_cold static port * |
1772 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1856 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1773 | { |
1857 | { |
1774 | port *pt = malloc (sizeof *pt); |
1858 | port *pt = malloc (sizeof *pt); |
1775 | |
1859 | |
1776 | if (!pt && USE_ERROR_CHECKING) |
1860 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1781 | pt->rep.stdio.file = f; |
1865 | pt->rep.stdio.file = f; |
1782 | pt->rep.stdio.closeit = 0; |
1866 | pt->rep.stdio.closeit = 0; |
1783 | return pt; |
1867 | return pt; |
1784 | } |
1868 | } |
1785 | |
1869 | |
1786 | static pointer |
1870 | ecb_cold static pointer |
1787 | port_from_file (SCHEME_P_ int f, int prop) |
1871 | port_from_file (SCHEME_P_ int f, int prop) |
1788 | { |
1872 | { |
1789 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1873 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1790 | |
1874 | |
1791 | if (!pt && USE_ERROR_CHECKING) |
1875 | if (!pt && USE_ERROR_CHECKING) |
1792 | return NIL; |
1876 | return NIL; |
1793 | |
1877 | |
1794 | return mk_port (SCHEME_A_ pt); |
1878 | return mk_port (SCHEME_A_ pt); |
1795 | } |
1879 | } |
1796 | |
1880 | |
1797 | static port * |
1881 | ecb_cold static port * |
1798 | 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) |
1799 | { |
1883 | { |
1800 | port *pt = malloc (sizeof (port)); |
1884 | port *pt = malloc (sizeof (port)); |
1801 | |
1885 | |
1802 | if (!pt && USE_ERROR_CHECKING) |
1886 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1808 | pt->rep.string.curr = start; |
1892 | pt->rep.string.curr = start; |
1809 | pt->rep.string.past_the_end = past_the_end; |
1893 | pt->rep.string.past_the_end = past_the_end; |
1810 | return pt; |
1894 | return pt; |
1811 | } |
1895 | } |
1812 | |
1896 | |
1813 | static pointer |
1897 | ecb_cold static pointer |
1814 | 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) |
1815 | { |
1899 | { |
1816 | 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); |
1817 | |
1901 | |
1818 | if (!pt && USE_ERROR_CHECKING) |
1902 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1821 | return mk_port (SCHEME_A_ pt); |
1905 | return mk_port (SCHEME_A_ pt); |
1822 | } |
1906 | } |
1823 | |
1907 | |
1824 | # define BLOCK_SIZE 256 |
1908 | # define BLOCK_SIZE 256 |
1825 | |
1909 | |
1826 | static port * |
1910 | ecb_cold static port * |
1827 | port_rep_from_scratch (SCHEME_P) |
1911 | port_rep_from_scratch (SCHEME_P) |
1828 | { |
1912 | { |
1829 | char *start; |
1913 | char *start; |
1830 | port *pt = malloc (sizeof (port)); |
1914 | port *pt = malloc (sizeof (port)); |
1831 | |
1915 | |
… | |
… | |
1845 | pt->rep.string.curr = start; |
1929 | pt->rep.string.curr = start; |
1846 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1930 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1847 | return pt; |
1931 | return pt; |
1848 | } |
1932 | } |
1849 | |
1933 | |
1850 | static pointer |
1934 | ecb_cold static pointer |
1851 | port_from_scratch (SCHEME_P) |
1935 | port_from_scratch (SCHEME_P) |
1852 | { |
1936 | { |
1853 | port *pt = port_rep_from_scratch (SCHEME_A); |
1937 | port *pt = port_rep_from_scratch (SCHEME_A); |
1854 | |
1938 | |
1855 | if (!pt && USE_ERROR_CHECKING) |
1939 | if (!pt && USE_ERROR_CHECKING) |
1856 | return NIL; |
1940 | return NIL; |
1857 | |
1941 | |
1858 | return mk_port (SCHEME_A_ pt); |
1942 | return mk_port (SCHEME_A_ pt); |
1859 | } |
1943 | } |
1860 | |
1944 | |
1861 | static void |
1945 | ecb_cold static void |
1862 | port_close (SCHEME_P_ pointer p, int flag) |
1946 | port_close (SCHEME_P_ pointer p, int flag) |
1863 | { |
1947 | { |
1864 | port *pt = p->object.port; |
1948 | port *pt = port (p); |
1865 | |
1949 | |
1866 | pt->kind &= ~flag; |
1950 | pt->kind &= ~flag; |
1867 | |
1951 | |
1868 | if ((pt->kind & (port_input | port_output)) == 0) |
1952 | if ((pt->kind & (port_input | port_output)) == 0) |
1869 | { |
1953 | { |
… | |
… | |
1886 | } |
1970 | } |
1887 | } |
1971 | } |
1888 | #endif |
1972 | #endif |
1889 | |
1973 | |
1890 | /* get new character from input file */ |
1974 | /* get new character from input file */ |
1891 | static int |
1975 | ecb_cold static int |
1892 | inchar (SCHEME_P) |
1976 | inchar (SCHEME_P) |
1893 | { |
1977 | { |
1894 | int c; |
1978 | int c; |
1895 | port *pt; |
1979 | port *pt = port (SCHEME_V->inport); |
1896 | |
|
|
1897 | pt = SCHEME_V->inport->object.port; |
|
|
1898 | |
1980 | |
1899 | if (pt->kind & port_saw_EOF) |
1981 | if (pt->kind & port_saw_EOF) |
1900 | return EOF; |
1982 | return EOF; |
1901 | |
1983 | |
1902 | c = basic_inchar (pt); |
1984 | c = basic_inchar (pt); |
… | |
… | |
1912 | } |
1994 | } |
1913 | |
1995 | |
1914 | return c; |
1996 | return c; |
1915 | } |
1997 | } |
1916 | |
1998 | |
1917 | static int ungot = -1; |
1999 | ecb_cold static int |
1918 | |
|
|
1919 | static int |
|
|
1920 | basic_inchar (port *pt) |
2000 | basic_inchar (port *pt) |
1921 | { |
2001 | { |
1922 | #if USE_PORTS |
|
|
1923 | if (pt->unget != -1) |
2002 | if (pt->unget != -1) |
1924 | { |
2003 | { |
1925 | int r = pt->unget; |
2004 | int r = pt->unget; |
1926 | pt->unget = -1; |
2005 | pt->unget = -1; |
1927 | return r; |
2006 | return r; |
1928 | } |
2007 | } |
1929 | |
2008 | |
|
|
2009 | #if USE_PORTS |
1930 | if (pt->kind & port_file) |
2010 | if (pt->kind & port_file) |
1931 | { |
2011 | { |
1932 | char c; |
2012 | char c; |
1933 | |
2013 | |
1934 | if (!read (pt->rep.stdio.file, &c, 1)) |
2014 | if (!read (pt->rep.stdio.file, &c, 1)) |
… | |
… | |
1942 | return EOF; |
2022 | return EOF; |
1943 | else |
2023 | else |
1944 | return *pt->rep.string.curr++; |
2024 | return *pt->rep.string.curr++; |
1945 | } |
2025 | } |
1946 | #else |
2026 | #else |
1947 | if (ungot == -1) |
|
|
1948 | { |
|
|
1949 | char c; |
2027 | char c; |
1950 | if (!read (0, &c, 1)) |
2028 | |
|
|
2029 | if (!read (pt->rep.stdio.file, &c, 1)) |
1951 | return EOF; |
2030 | return EOF; |
1952 | |
2031 | |
1953 | ungot = c; |
|
|
1954 | } |
|
|
1955 | |
|
|
1956 | { |
|
|
1957 | int r = ungot; |
|
|
1958 | ungot = -1; |
|
|
1959 | return r; |
2032 | return c; |
1960 | } |
|
|
1961 | #endif |
2033 | #endif |
1962 | } |
2034 | } |
1963 | |
2035 | |
1964 | /* back character to input buffer */ |
2036 | /* back character to input buffer */ |
1965 | static void |
2037 | ecb_cold static void |
1966 | backchar (SCHEME_P_ int c) |
2038 | backchar (SCHEME_P_ int c) |
1967 | { |
2039 | { |
1968 | #if USE_PORTS |
2040 | port *pt = port (SCHEME_V->inport); |
1969 | port *pt; |
|
|
1970 | |
2041 | |
1971 | if (c == EOF) |
2042 | if (c == EOF) |
1972 | return; |
2043 | return; |
1973 | |
2044 | |
1974 | pt = SCHEME_V->inport->object.port; |
|
|
1975 | pt->unget = c; |
2045 | pt->unget = c; |
1976 | #else |
|
|
1977 | if (c == EOF) |
|
|
1978 | return; |
|
|
1979 | |
|
|
1980 | ungot = c; |
|
|
1981 | #endif |
|
|
1982 | } |
2046 | } |
1983 | |
2047 | |
1984 | #if USE_PORTS |
2048 | #if USE_PORTS |
1985 | static int |
2049 | ecb_cold static int |
1986 | realloc_port_string (SCHEME_P_ port *p) |
2050 | realloc_port_string (SCHEME_P_ port *p) |
1987 | { |
2051 | { |
1988 | char *start = p->rep.string.start; |
2052 | char *start = p->rep.string.start; |
1989 | 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; |
1990 | char *str = malloc (new_size); |
2054 | char *str = malloc (new_size); |
… | |
… | |
2003 | else |
2067 | else |
2004 | return 0; |
2068 | return 0; |
2005 | } |
2069 | } |
2006 | #endif |
2070 | #endif |
2007 | |
2071 | |
2008 | INTERFACE void |
2072 | ecb_cold static void |
2009 | putstr (SCHEME_P_ const char *s) |
2073 | putchars (SCHEME_P_ const char *s, int len) |
2010 | { |
2074 | { |
|
|
2075 | port *pt = port (SCHEME_V->outport); |
|
|
2076 | |
2011 | #if USE_PORTS |
2077 | #if USE_PORTS |
2012 | port *pt = SCHEME_V->outport->object.port; |
|
|
2013 | |
|
|
2014 | if (pt->kind & port_file) |
|
|
2015 | write (pt->rep.stdio.file, s, strlen (s)); |
|
|
2016 | else |
|
|
2017 | for (; *s; s++) |
|
|
2018 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2019 | *pt->rep.string.curr++ = *s; |
|
|
2020 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2021 | *pt->rep.string.curr++ = *s; |
|
|
2022 | |
|
|
2023 | #else |
|
|
2024 | xwrstr (s); |
|
|
2025 | #endif |
|
|
2026 | } |
|
|
2027 | |
|
|
2028 | static void |
|
|
2029 | putchars (SCHEME_P_ const char *s, int len) |
|
|
2030 | { |
|
|
2031 | #if USE_PORTS |
|
|
2032 | port *pt = SCHEME_V->outport->object.port; |
|
|
2033 | |
|
|
2034 | if (pt->kind & port_file) |
2078 | if (pt->kind & port_file) |
2035 | write (pt->rep.stdio.file, s, len); |
2079 | write (pt->rep.stdio.file, s, len); |
2036 | else |
2080 | else |
2037 | { |
2081 | { |
2038 | for (; len; len--) |
2082 | for (; len; len--) |
… | |
… | |
2043 | *pt->rep.string.curr++ = *s++; |
2087 | *pt->rep.string.curr++ = *s++; |
2044 | } |
2088 | } |
2045 | } |
2089 | } |
2046 | |
2090 | |
2047 | #else |
2091 | #else |
2048 | write (1, s, len); |
2092 | write (1, s, len); // output not initialised |
2049 | #endif |
2093 | #endif |
|
|
2094 | } |
|
|
2095 | |
|
|
2096 | INTERFACE void |
|
|
2097 | putstr (SCHEME_P_ const char *s) |
|
|
2098 | { |
|
|
2099 | putchars (SCHEME_A_ s, strlen (s)); |
2050 | } |
2100 | } |
2051 | |
2101 | |
2052 | INTERFACE void |
2102 | INTERFACE void |
2053 | putcharacter (SCHEME_P_ int c) |
2103 | putcharacter (SCHEME_P_ int c) |
2054 | { |
2104 | { |
2055 | #if USE_PORTS |
|
|
2056 | port *pt = SCHEME_V->outport->object.port; |
|
|
2057 | |
|
|
2058 | if (pt->kind & port_file) |
|
|
2059 | { |
|
|
2060 | char cc = c; |
|
|
2061 | write (pt->rep.stdio.file, &cc, 1); |
|
|
2062 | } |
|
|
2063 | else |
|
|
2064 | { |
|
|
2065 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2066 | *pt->rep.string.curr++ = c; |
|
|
2067 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2068 | *pt->rep.string.curr++ = c; |
|
|
2069 | } |
|
|
2070 | |
|
|
2071 | #else |
|
|
2072 | char cc = c; |
2105 | char cc = c; |
2073 | write (1, &c, 1); |
2106 | |
2074 | #endif |
2107 | putchars (SCHEME_A_ &cc, 1); |
2075 | } |
2108 | } |
2076 | |
2109 | |
2077 | /* read characters up to delimiter, but cater to character constants */ |
2110 | /* read characters up to delimiter, but cater to character constants */ |
2078 | static char * |
2111 | ecb_cold static char * |
2079 | readstr_upto (SCHEME_P_ char *delim) |
2112 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2080 | { |
2113 | { |
2081 | char *p = SCHEME_V->strbuff; |
2114 | char *p = SCHEME_V->strbuff + skip; |
2082 | |
2115 | |
2083 | 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)))); |
2084 | |
2117 | |
2085 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2118 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2086 | *p = 0; |
2119 | *p = 0; |
… | |
… | |
2092 | |
2125 | |
2093 | return SCHEME_V->strbuff; |
2126 | return SCHEME_V->strbuff; |
2094 | } |
2127 | } |
2095 | |
2128 | |
2096 | /* read string expression "xxx...xxx" */ |
2129 | /* read string expression "xxx...xxx" */ |
2097 | static pointer |
2130 | ecb_cold static pointer |
2098 | readstrexp (SCHEME_P) |
2131 | readstrexp (SCHEME_P_ char delim) |
2099 | { |
2132 | { |
2100 | char *p = SCHEME_V->strbuff; |
2133 | char *p = SCHEME_V->strbuff; |
2101 | int c; |
2134 | int c; |
2102 | int c1 = 0; |
2135 | int c1 = 0; |
2103 | enum |
|
|
2104 | { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2136 | enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2105 | |
2137 | |
2106 | for (;;) |
2138 | for (;;) |
2107 | { |
2139 | { |
2108 | c = inchar (SCHEME_A); |
2140 | c = inchar (SCHEME_A); |
2109 | |
2141 | |
… | |
… | |
2111 | return S_F; |
2143 | return S_F; |
2112 | |
2144 | |
2113 | switch (state) |
2145 | switch (state) |
2114 | { |
2146 | { |
2115 | case st_ok: |
2147 | case st_ok: |
2116 | switch (c) |
2148 | if (ecb_expect_false (c == delim)) |
2117 | { |
|
|
2118 | case '\\': |
|
|
2119 | state = st_bsl; |
|
|
2120 | break; |
|
|
2121 | |
|
|
2122 | case '"': |
|
|
2123 | *p = 0; |
|
|
2124 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2149 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2125 | |
2150 | |
2126 | default: |
2151 | if (ecb_expect_false (c == '\\')) |
|
|
2152 | state = st_bsl; |
|
|
2153 | else |
2127 | *p++ = c; |
2154 | *p++ = c; |
2128 | break; |
|
|
2129 | } |
|
|
2130 | |
2155 | |
2131 | break; |
2156 | break; |
2132 | |
2157 | |
2133 | case st_bsl: |
2158 | case st_bsl: |
2134 | switch (c) |
2159 | switch (c) |
… | |
… | |
2143 | case '7': |
2168 | case '7': |
2144 | state = st_oct1; |
2169 | state = st_oct1; |
2145 | c1 = c - '0'; |
2170 | c1 = c - '0'; |
2146 | break; |
2171 | break; |
2147 | |
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 |
2148 | case 'x': |
2188 | case 'x': |
2149 | case 'X': |
2189 | case 'X': |
2150 | state = st_x1; |
2190 | state = st_x1; |
2151 | c1 = 0; |
2191 | c1 = 0; |
2152 | break; |
2192 | break; |
2153 | |
2193 | |
2154 | case 'n': |
|
|
2155 | *p++ = '\n'; |
|
|
2156 | state = st_ok; |
|
|
2157 | break; |
|
|
2158 | |
|
|
2159 | case 't': |
|
|
2160 | *p++ = '\t'; |
|
|
2161 | state = st_ok; |
|
|
2162 | break; |
|
|
2163 | |
|
|
2164 | case 'r': |
|
|
2165 | *p++ = '\r'; |
|
|
2166 | state = st_ok; |
|
|
2167 | break; |
|
|
2168 | |
|
|
2169 | case '"': |
|
|
2170 | *p++ = '"'; |
|
|
2171 | state = st_ok; |
|
|
2172 | break; |
|
|
2173 | |
|
|
2174 | default: |
2194 | default: |
2175 | *p++ = c; |
2195 | *p++ = c; |
2176 | state = st_ok; |
2196 | state = st_ok; |
2177 | break; |
2197 | break; |
2178 | } |
2198 | } |
2179 | |
2199 | |
2180 | break; |
2200 | break; |
2181 | |
2201 | |
2182 | case st_x1: |
2202 | case st_x1: |
2183 | case st_x2: |
2203 | case st_x2: |
2184 | c = toupper (c); |
2204 | c = tolower (c); |
2185 | |
2205 | |
2186 | if (c >= '0' && c <= 'F') |
2206 | if (c >= '0' && c <= '9') |
2187 | { |
|
|
2188 | if (c <= '9') |
|
|
2189 | c1 = (c1 << 4) + c - '0'; |
2207 | c1 = (c1 << 4) + c - '0'; |
2190 | else |
2208 | else if (c >= 'a' && c <= 'f') |
2191 | c1 = (c1 << 4) + c - 'A' + 10; |
2209 | c1 = (c1 << 4) + c - 'a' + 10; |
2192 | |
|
|
2193 | if (state == st_x1) |
|
|
2194 | state = st_x2; |
|
|
2195 | else |
|
|
2196 | { |
|
|
2197 | *p++ = c1; |
|
|
2198 | state = st_ok; |
|
|
2199 | } |
|
|
2200 | } |
|
|
2201 | else |
2210 | else |
2202 | return S_F; |
2211 | return S_F; |
|
|
2212 | |
|
|
2213 | if (state == st_x1) |
|
|
2214 | state = st_x2; |
|
|
2215 | else |
|
|
2216 | { |
|
|
2217 | *p++ = c1; |
|
|
2218 | state = st_ok; |
|
|
2219 | } |
2203 | |
2220 | |
2204 | break; |
2221 | break; |
2205 | |
2222 | |
2206 | case st_oct1: |
2223 | case st_oct1: |
2207 | case st_oct2: |
2224 | case st_oct2: |
… | |
… | |
2211 | backchar (SCHEME_A_ c); |
2228 | backchar (SCHEME_A_ c); |
2212 | state = st_ok; |
2229 | state = st_ok; |
2213 | } |
2230 | } |
2214 | else |
2231 | else |
2215 | { |
2232 | { |
2216 | if (state == st_oct2 && c1 >= 32) |
2233 | if (state == st_oct2 && c1 >= ' ') |
2217 | return S_F; |
2234 | return S_F; |
2218 | |
2235 | |
2219 | c1 = (c1 << 3) + (c - '0'); |
2236 | c1 = (c1 << 3) + (c - '0'); |
2220 | |
2237 | |
2221 | if (state == st_oct1) |
2238 | if (state == st_oct1) |
… | |
… | |
2226 | state = st_ok; |
2243 | state = st_ok; |
2227 | } |
2244 | } |
2228 | } |
2245 | } |
2229 | |
2246 | |
2230 | break; |
2247 | break; |
2231 | |
|
|
2232 | } |
2248 | } |
2233 | } |
2249 | } |
2234 | } |
2250 | } |
2235 | |
2251 | |
2236 | /* check c is in chars */ |
2252 | /* check c is in chars */ |
2237 | static INLINE int |
2253 | ecb_cold int |
2238 | is_one_of (char *s, int c) |
2254 | is_one_of (const char *s, int c) |
2239 | { |
2255 | { |
2240 | if (c == EOF) |
|
|
2241 | return 1; |
|
|
2242 | |
|
|
2243 | return !!strchr (s, c); |
2256 | return c == EOF || !!strchr (s, c); |
2244 | } |
2257 | } |
2245 | |
2258 | |
2246 | /* skip white characters */ |
2259 | /* skip white characters */ |
2247 | static INLINE int |
2260 | ecb_cold int |
2248 | skipspace (SCHEME_P) |
2261 | skipspace (SCHEME_P) |
2249 | { |
2262 | { |
2250 | int c, curr_line = 0; |
2263 | int c, curr_line = 0; |
2251 | |
2264 | |
2252 | do |
2265 | do |
2253 | { |
2266 | { |
2254 | c = inchar (SCHEME_A); |
2267 | c = inchar (SCHEME_A); |
|
|
2268 | |
2255 | #if SHOW_ERROR_LINE |
2269 | #if SHOW_ERROR_LINE |
2256 | if (c == '\n') |
2270 | if (ecb_expect_false (c == '\n')) |
2257 | curr_line++; |
2271 | curr_line++; |
2258 | #endif |
2272 | #endif |
|
|
2273 | |
|
|
2274 | if (ecb_expect_false (c == EOF)) |
|
|
2275 | return c; |
2259 | } |
2276 | } |
2260 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2277 | while (is_one_of (WHITESPACE, c)); |
2261 | |
2278 | |
2262 | /* record it */ |
2279 | /* record it */ |
2263 | #if SHOW_ERROR_LINE |
2280 | #if SHOW_ERROR_LINE |
2264 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2281 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2265 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2282 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2266 | #endif |
2283 | #endif |
2267 | |
2284 | |
2268 | if (c != EOF) |
|
|
2269 | { |
|
|
2270 | backchar (SCHEME_A_ c); |
2285 | backchar (SCHEME_A_ c); |
2271 | return 1; |
2286 | return 1; |
2272 | } |
|
|
2273 | else |
|
|
2274 | return EOF; |
|
|
2275 | } |
2287 | } |
2276 | |
2288 | |
2277 | /* get token */ |
2289 | /* get token */ |
2278 | static int |
2290 | ecb_cold static int |
2279 | token (SCHEME_P) |
2291 | token (SCHEME_P) |
2280 | { |
2292 | { |
2281 | int c = skipspace (SCHEME_A); |
2293 | int c = skipspace (SCHEME_A); |
2282 | |
2294 | |
2283 | if (c == EOF) |
2295 | if (c == EOF) |
… | |
… | |
2295 | return TOK_RPAREN; |
2307 | return TOK_RPAREN; |
2296 | |
2308 | |
2297 | case '.': |
2309 | case '.': |
2298 | c = inchar (SCHEME_A); |
2310 | c = inchar (SCHEME_A); |
2299 | |
2311 | |
2300 | if (is_one_of (" \n\t", c)) |
2312 | if (is_one_of (WHITESPACE, c)) |
2301 | return TOK_DOT; |
2313 | return TOK_DOT; |
2302 | else |
2314 | else |
2303 | { |
2315 | { |
2304 | //TODO: ungetc twice in a row is not supported in C |
|
|
2305 | backchar (SCHEME_A_ c); |
2316 | backchar (SCHEME_A_ c); |
2306 | backchar (SCHEME_A_ '.'); |
|
|
2307 | return TOK_ATOM; |
2317 | return TOK_DOTATOM; |
2308 | } |
2318 | } |
|
|
2319 | |
|
|
2320 | case '|': |
|
|
2321 | return TOK_STRATOM; |
2309 | |
2322 | |
2310 | case '\'': |
2323 | case '\'': |
2311 | return TOK_QUOTE; |
2324 | return TOK_QUOTE; |
2312 | |
2325 | |
2313 | case ';': |
2326 | case ';': |
… | |
… | |
2380 | } |
2393 | } |
2381 | |
2394 | |
2382 | /* ========== Routines for Printing ========== */ |
2395 | /* ========== Routines for Printing ========== */ |
2383 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2396 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2384 | |
2397 | |
2385 | static void |
2398 | ecb_cold static void |
2386 | printslashstring (SCHEME_P_ char *p, int len) |
2399 | printslashstring (SCHEME_P_ char *p, int len) |
2387 | { |
2400 | { |
2388 | int i; |
2401 | int i; |
2389 | unsigned char *s = (unsigned char *) p; |
2402 | unsigned char *s = (unsigned char *) p; |
2390 | |
2403 | |
… | |
… | |
2445 | } |
2458 | } |
2446 | |
2459 | |
2447 | putcharacter (SCHEME_A_ '"'); |
2460 | putcharacter (SCHEME_A_ '"'); |
2448 | } |
2461 | } |
2449 | |
2462 | |
2450 | |
|
|
2451 | /* print atoms */ |
2463 | /* print atoms */ |
2452 | static void |
2464 | ecb_cold static void |
2453 | printatom (SCHEME_P_ pointer l, int f) |
2465 | printatom (SCHEME_P_ pointer l, int f) |
2454 | { |
2466 | { |
2455 | char *p; |
2467 | char *p; |
2456 | int len; |
2468 | int len; |
2457 | |
2469 | |
2458 | atom2str (SCHEME_A_ l, f, &p, &len); |
2470 | atom2str (SCHEME_A_ l, f, &p, &len); |
2459 | putchars (SCHEME_A_ p, len); |
2471 | putchars (SCHEME_A_ p, len); |
2460 | } |
2472 | } |
2461 | |
2473 | |
2462 | |
|
|
2463 | /* Uses internal buffer unless string pointer is already available */ |
2474 | /* Uses internal buffer unless string pointer is already available */ |
2464 | static void |
2475 | ecb_cold static void |
2465 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2476 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2466 | { |
2477 | { |
2467 | char *p; |
2478 | char *p; |
2468 | |
2479 | |
2469 | if (l == NIL) |
2480 | if (l == NIL) |
… | |
… | |
2480 | { |
2491 | { |
2481 | p = SCHEME_V->strbuff; |
2492 | p = SCHEME_V->strbuff; |
2482 | |
2493 | |
2483 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2494 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2484 | { |
2495 | { |
2485 | if (num_is_integer (l)) |
2496 | if (is_integer (l)) |
2486 | xnum (p, ivalue_unchecked (l)); |
2497 | xnum (p, ivalue_unchecked (l)); |
2487 | #if USE_REAL |
2498 | #if USE_REAL |
2488 | else |
2499 | else |
2489 | { |
2500 | { |
2490 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
2501 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
… | |
… | |
2602 | } |
2613 | } |
2603 | else if (is_symbol (l)) |
2614 | else if (is_symbol (l)) |
2604 | p = symname (l); |
2615 | p = symname (l); |
2605 | else if (is_proc (l)) |
2616 | else if (is_proc (l)) |
2606 | { |
2617 | { |
|
|
2618 | p = (char *)procname (l); // ok with r7rs display, but not r7rs write |
|
|
2619 | #if 0 |
2607 | #if USE_PRINTF |
2620 | #if USE_PRINTF |
2608 | p = SCHEME_V->strbuff; |
2621 | p = SCHEME_V->strbuff; |
2609 | snprintf (p, STRBUFFSIZE, "#<%s PROCEDURE %ld>", procname (l), procnum (l)); |
2622 | snprintf (p, STRBUFFSIZE, " PROCEDURE %ld>", procname (l), procnum (l)); |
2610 | #else |
2623 | #else |
2611 | p = "#<PROCEDURE>"; |
2624 | p = "#<PROCEDURE>"; |
|
|
2625 | #endif |
2612 | #endif |
2626 | #endif |
2613 | } |
2627 | } |
2614 | else if (is_macro (l)) |
2628 | else if (is_macro (l)) |
2615 | p = "#<MACRO>"; |
2629 | p = "#<MACRO>"; |
2616 | else if (is_closure (l)) |
2630 | else if (is_closure (l)) |
… | |
… | |
2627 | #endif |
2641 | #endif |
2628 | } |
2642 | } |
2629 | else if (is_continuation (l)) |
2643 | else if (is_continuation (l)) |
2630 | p = "#<CONTINUATION>"; |
2644 | p = "#<CONTINUATION>"; |
2631 | else |
2645 | else |
|
|
2646 | { |
|
|
2647 | #if USE_PRINTF |
|
|
2648 | p = SCHEME_V->strbuff; |
|
|
2649 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2650 | #else |
2632 | p = "#<ERROR>"; |
2651 | p = "#<ERROR>"; |
|
|
2652 | #endif |
|
|
2653 | } |
2633 | |
2654 | |
2634 | *pp = p; |
2655 | *pp = p; |
2635 | *plen = strlen (p); |
2656 | *plen = strlen (p); |
2636 | } |
2657 | } |
2637 | |
2658 | |
… | |
… | |
2669 | return car (d); |
2690 | return car (d); |
2670 | |
2691 | |
2671 | p = cons (car (d), cdr (d)); |
2692 | p = cons (car (d), cdr (d)); |
2672 | q = p; |
2693 | q = p; |
2673 | |
2694 | |
2674 | while (cdr (cdr (p)) != NIL) |
2695 | while (cddr (p) != NIL) |
2675 | { |
2696 | { |
2676 | d = cons (car (p), cdr (p)); |
2697 | d = cons (car (p), cdr (p)); |
2677 | |
2698 | |
2678 | if (cdr (cdr (p)) != NIL) |
2699 | if (cddr (p) != NIL) |
2679 | p = cdr (d); |
2700 | p = cdr (d); |
2680 | } |
2701 | } |
2681 | |
2702 | |
2682 | set_cdr (p, car (cdr (p))); |
2703 | set_cdr (p, cadr (p)); |
2683 | return q; |
2704 | return q; |
2684 | } |
2705 | } |
2685 | |
2706 | |
2686 | /* reverse list -- produce new list */ |
2707 | /* reverse list -- produce new list */ |
2687 | static pointer |
2708 | ecb_hot static pointer |
2688 | reverse (SCHEME_P_ pointer a) |
2709 | reverse (SCHEME_P_ pointer a) |
2689 | { |
2710 | { |
2690 | /* a must be checked by gc */ |
2711 | /* a must be checked by gc */ |
2691 | pointer p = NIL; |
2712 | pointer p = NIL; |
2692 | |
2713 | |
… | |
… | |
2695 | |
2716 | |
2696 | return p; |
2717 | return p; |
2697 | } |
2718 | } |
2698 | |
2719 | |
2699 | /* reverse list --- in-place */ |
2720 | /* reverse list --- in-place */ |
2700 | static pointer |
2721 | ecb_hot static pointer |
2701 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2722 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2702 | { |
2723 | { |
2703 | pointer result = term; |
2724 | pointer result = term; |
2704 | pointer p = list; |
2725 | pointer p = list; |
2705 | |
2726 | |
… | |
… | |
2713 | |
2734 | |
2714 | return result; |
2735 | return result; |
2715 | } |
2736 | } |
2716 | |
2737 | |
2717 | /* append list -- produce new list (in reverse order) */ |
2738 | /* append list -- produce new list (in reverse order) */ |
2718 | static pointer |
2739 | ecb_hot static pointer |
2719 | revappend (SCHEME_P_ pointer a, pointer b) |
2740 | revappend (SCHEME_P_ pointer a, pointer b) |
2720 | { |
2741 | { |
2721 | pointer result = a; |
2742 | pointer result = a; |
2722 | pointer p = b; |
2743 | pointer p = b; |
2723 | |
2744 | |
… | |
… | |
2732 | |
2753 | |
2733 | return S_F; /* signal an error */ |
2754 | return S_F; /* signal an error */ |
2734 | } |
2755 | } |
2735 | |
2756 | |
2736 | /* equivalence of atoms */ |
2757 | /* equivalence of atoms */ |
2737 | int |
2758 | ecb_hot int |
2738 | eqv (pointer a, pointer b) |
2759 | eqv (pointer a, pointer b) |
2739 | { |
2760 | { |
2740 | if (is_string (a)) |
2761 | if (is_string (a)) |
2741 | { |
2762 | { |
2742 | if (is_string (b)) |
2763 | if (is_string (b)) |
… | |
… | |
2745 | return 0; |
2766 | return 0; |
2746 | } |
2767 | } |
2747 | else if (is_number (a)) |
2768 | else if (is_number (a)) |
2748 | { |
2769 | { |
2749 | if (is_number (b)) |
2770 | if (is_number (b)) |
2750 | if (num_is_integer (a) == num_is_integer (b)) |
|
|
2751 | return num_cmp (nvalue (a), nvalue (b)) == 0; |
2771 | return num_cmp (nvalue (a), nvalue (b)) == 0; |
2752 | |
2772 | |
2753 | return 0; |
2773 | return 0; |
2754 | } |
2774 | } |
2755 | else if (is_character (a)) |
2775 | else if (is_character (a)) |
2756 | { |
2776 | { |
… | |
… | |
2782 | /* () is #t in R5RS */ |
2802 | /* () is #t in R5RS */ |
2783 | #define is_true(p) ((p) != S_F) |
2803 | #define is_true(p) ((p) != S_F) |
2784 | #define is_false(p) ((p) == S_F) |
2804 | #define is_false(p) ((p) == S_F) |
2785 | |
2805 | |
2786 | /* ========== Environment implementation ========== */ |
2806 | /* ========== Environment implementation ========== */ |
2787 | |
|
|
2788 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
|
|
2789 | |
|
|
2790 | static int |
|
|
2791 | hash_fn (const char *key, int table_size) |
|
|
2792 | { |
|
|
2793 | const unsigned char *p = key; |
|
|
2794 | uint32_t hash = 2166136261; |
|
|
2795 | |
|
|
2796 | while (*p) |
|
|
2797 | hash = (hash ^ *p++) * 16777619; |
|
|
2798 | |
|
|
2799 | return hash % table_size; |
|
|
2800 | } |
|
|
2801 | #endif |
|
|
2802 | |
2807 | |
2803 | #ifndef USE_ALIST_ENV |
2808 | #ifndef USE_ALIST_ENV |
2804 | |
2809 | |
2805 | /* |
2810 | /* |
2806 | * In this implementation, each frame of the environment may be |
2811 | * In this implementation, each frame of the environment may be |
… | |
… | |
2815 | { |
2820 | { |
2816 | pointer new_frame; |
2821 | pointer new_frame; |
2817 | |
2822 | |
2818 | /* The interaction-environment has about 300 variables in it. */ |
2823 | /* The interaction-environment has about 300 variables in it. */ |
2819 | if (old_env == NIL) |
2824 | if (old_env == NIL) |
2820 | new_frame = mk_vector (SCHEME_A_ 461); |
2825 | new_frame = mk_vector (SCHEME_A_ 29); // was 461 |
2821 | else |
2826 | else |
2822 | new_frame = NIL; |
2827 | new_frame = NIL; |
2823 | |
2828 | |
2824 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2829 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2825 | setenvironment (SCHEME_V->envir); |
2830 | setenvironment (SCHEME_V->envir); |
2826 | } |
2831 | } |
2827 | |
2832 | |
2828 | static INLINE void |
2833 | static uint32_t |
|
|
2834 | sym_hash (pointer sym, uint32_t size) |
|
|
2835 | { |
|
|
2836 | uintptr_t ptr = (uintptr_t)sym; |
|
|
2837 | |
|
|
2838 | #if 0 |
|
|
2839 | /* table size is prime, so why mix */ |
|
|
2840 | ptr += ptr >> 32; |
|
|
2841 | ptr += ptr >> 16; |
|
|
2842 | ptr += ptr >> 8; |
|
|
2843 | #endif |
|
|
2844 | |
|
|
2845 | return ptr % size; |
|
|
2846 | } |
|
|
2847 | |
|
|
2848 | ecb_inline void |
2829 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2849 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2830 | { |
2850 | { |
2831 | pointer slot = immutable_cons (variable, value); |
2851 | pointer slot = immutable_cons (variable, value); |
2832 | |
2852 | |
2833 | if (is_vector (car (env))) |
2853 | if (is_vector (car (env))) |
2834 | { |
2854 | { |
2835 | int location = hash_fn (symname (variable), veclength (car (env))); |
2855 | int location = sym_hash (variable, veclength (car (env))); |
2836 | |
|
|
2837 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
2856 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2838 | } |
2857 | } |
2839 | else |
2858 | else |
2840 | set_car (env, immutable_cons (slot, car (env))); |
2859 | set_car (env, immutable_cons (slot, car (env))); |
2841 | } |
2860 | } |
2842 | |
2861 | |
2843 | static pointer |
2862 | ecb_hot static pointer |
2844 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2863 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2845 | { |
2864 | { |
2846 | pointer x, y; |
2865 | pointer x, y; |
2847 | int location; |
|
|
2848 | |
2866 | |
2849 | for (x = env; x != NIL; x = cdr (x)) |
2867 | for (x = env; x != NIL; x = cdr (x)) |
2850 | { |
2868 | { |
2851 | if (is_vector (car (x))) |
2869 | if (is_vector (car (x))) |
2852 | { |
2870 | { |
2853 | location = hash_fn (symname (hdl), veclength (car (x))); |
2871 | int location = sym_hash (hdl, veclength (car (x))); |
2854 | y = vector_elem (car (x), location); |
2872 | y = vector_get (car (x), location); |
2855 | } |
2873 | } |
2856 | else |
2874 | else |
2857 | y = car (x); |
2875 | y = car (x); |
2858 | |
2876 | |
2859 | for (; y != NIL; y = cdr (y)) |
2877 | for (; y != NIL; y = cdr (y)) |
2860 | if (caar (y) == hdl) |
2878 | if (caar (y) == hdl) |
2861 | break; |
2879 | break; |
2862 | |
2880 | |
2863 | if (y != NIL) |
2881 | if (y != NIL) |
|
|
2882 | return car (y); |
|
|
2883 | |
|
|
2884 | if (!all) |
2864 | break; |
2885 | break; |
2865 | |
|
|
2866 | if (!all) |
|
|
2867 | return NIL; |
|
|
2868 | } |
2886 | } |
2869 | |
|
|
2870 | if (x != NIL) |
|
|
2871 | return car (y); |
|
|
2872 | |
2887 | |
2873 | return NIL; |
2888 | return NIL; |
2874 | } |
2889 | } |
2875 | |
2890 | |
2876 | #else /* USE_ALIST_ENV */ |
2891 | #else /* USE_ALIST_ENV */ |
2877 | |
2892 | |
2878 | static INLINE void |
2893 | static void |
2879 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2894 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2880 | { |
2895 | { |
2881 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2896 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2882 | setenvironment (SCHEME_V->envir); |
2897 | setenvironment (SCHEME_V->envir); |
2883 | } |
2898 | } |
2884 | |
2899 | |
2885 | static INLINE void |
2900 | static void |
2886 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2901 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2887 | { |
2902 | { |
2888 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2903 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2889 | } |
2904 | } |
2890 | |
2905 | |
2891 | static pointer |
2906 | ecb_hot static pointer |
2892 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2907 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2893 | { |
2908 | { |
2894 | pointer x, y; |
2909 | pointer x, y; |
2895 | |
2910 | |
2896 | for (x = env; x != NIL; x = cdr (x)) |
2911 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2898 | for (y = car (x); y != NIL; y = cdr (y)) |
2913 | for (y = car (x); y != NIL; y = cdr (y)) |
2899 | if (caar (y) == hdl) |
2914 | if (caar (y) == hdl) |
2900 | break; |
2915 | break; |
2901 | |
2916 | |
2902 | if (y != NIL) |
2917 | if (y != NIL) |
|
|
2918 | return car (y); |
2903 | break; |
2919 | break; |
2904 | |
2920 | |
2905 | if (!all) |
2921 | if (!all) |
2906 | return NIL; |
2922 | break; |
2907 | } |
2923 | } |
2908 | |
|
|
2909 | if (x != NIL) |
|
|
2910 | return car (y); |
|
|
2911 | |
2924 | |
2912 | return NIL; |
2925 | return NIL; |
2913 | } |
2926 | } |
2914 | |
2927 | |
2915 | #endif /* USE_ALIST_ENV else */ |
2928 | #endif /* USE_ALIST_ENV else */ |
2916 | |
2929 | |
2917 | static INLINE void |
2930 | static void |
2918 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2931 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2919 | { |
2932 | { |
|
|
2933 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2920 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2934 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2921 | } |
2935 | } |
2922 | |
2936 | |
2923 | static INLINE void |
2937 | static void |
2924 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2938 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2925 | { |
2939 | { |
2926 | set_cdr (slot, value); |
2940 | set_cdr (slot, value); |
2927 | } |
2941 | } |
2928 | |
2942 | |
2929 | static INLINE pointer |
2943 | static pointer |
2930 | slot_value_in_env (pointer slot) |
2944 | slot_value_in_env (pointer slot) |
2931 | { |
2945 | { |
2932 | return cdr (slot); |
2946 | return cdr (slot); |
2933 | } |
2947 | } |
2934 | |
2948 | |
2935 | /* ========== Evaluation Cycle ========== */ |
2949 | /* ========== Evaluation Cycle ========== */ |
2936 | |
2950 | |
2937 | static int |
2951 | ecb_cold static int |
2938 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2952 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2939 | { |
2953 | { |
2940 | #if USE_ERROR_HOOK |
|
|
2941 | pointer x; |
|
|
2942 | pointer hdl = SCHEME_V->ERROR_HOOK; |
|
|
2943 | #endif |
|
|
2944 | |
|
|
2945 | #if USE_PRINTF |
2954 | #if USE_PRINTF |
2946 | #if SHOW_ERROR_LINE |
2955 | #if SHOW_ERROR_LINE |
2947 | char sbuf[STRBUFFSIZE]; |
2956 | char sbuf[STRBUFFSIZE]; |
2948 | |
2957 | |
2949 | /* make sure error is not in REPL */ |
2958 | /* make sure error is not in REPL */ |
… | |
… | |
2964 | } |
2973 | } |
2965 | #endif |
2974 | #endif |
2966 | #endif |
2975 | #endif |
2967 | |
2976 | |
2968 | #if USE_ERROR_HOOK |
2977 | #if USE_ERROR_HOOK |
2969 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); |
2978 | pointer x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->ERROR_HOOK, 1); |
2970 | |
2979 | |
2971 | if (x != NIL) |
2980 | if (x != NIL) |
2972 | { |
2981 | { |
2973 | pointer code = a |
2982 | pointer code = a |
2974 | ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) |
2983 | ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) |
… | |
… | |
3018 | pointer code; |
3027 | pointer code; |
3019 | }; |
3028 | }; |
3020 | |
3029 | |
3021 | # define STACK_GROWTH 3 |
3030 | # define STACK_GROWTH 3 |
3022 | |
3031 | |
3023 | static void |
3032 | ecb_hot static void |
3024 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3033 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3025 | { |
3034 | { |
3026 | int nframes = (uintptr_t)SCHEME_V->dump; |
3035 | int nframes = (uintptr_t)SCHEME_V->dump; |
3027 | struct dump_stack_frame *next_frame; |
3036 | struct dump_stack_frame *next_frame; |
3028 | |
3037 | |
3029 | /* enough room for the next frame? */ |
3038 | /* enough room for the next frame? */ |
3030 | if (nframes >= SCHEME_V->dump_size) |
3039 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3031 | { |
3040 | { |
3032 | SCHEME_V->dump_size += STACK_GROWTH; |
3041 | SCHEME_V->dump_size += STACK_GROWTH; |
3033 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3042 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3034 | } |
3043 | } |
3035 | |
3044 | |
… | |
… | |
3041 | next_frame->code = code; |
3050 | next_frame->code = code; |
3042 | |
3051 | |
3043 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3052 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3044 | } |
3053 | } |
3045 | |
3054 | |
3046 | static int |
3055 | static ecb_hot int |
3047 | xs_return (SCHEME_P_ pointer a) |
3056 | xs_return (SCHEME_P_ pointer a) |
3048 | { |
3057 | { |
3049 | int nframes = (uintptr_t)SCHEME_V->dump; |
3058 | int nframes = (uintptr_t)SCHEME_V->dump; |
3050 | struct dump_stack_frame *frame; |
3059 | struct dump_stack_frame *frame; |
3051 | |
3060 | |
… | |
… | |
3062 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3071 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3063 | |
3072 | |
3064 | return 0; |
3073 | return 0; |
3065 | } |
3074 | } |
3066 | |
3075 | |
3067 | static INLINE void |
3076 | ecb_cold void |
3068 | dump_stack_reset (SCHEME_P) |
3077 | dump_stack_reset (SCHEME_P) |
3069 | { |
3078 | { |
3070 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3079 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3071 | SCHEME_V->dump = (pointer)+0; |
3080 | SCHEME_V->dump = (pointer)+0; |
3072 | } |
3081 | } |
3073 | |
3082 | |
3074 | static INLINE void |
3083 | ecb_cold void |
3075 | dump_stack_initialize (SCHEME_P) |
3084 | dump_stack_initialize (SCHEME_P) |
3076 | { |
3085 | { |
3077 | SCHEME_V->dump_size = 0; |
3086 | SCHEME_V->dump_size = 0; |
3078 | SCHEME_V->dump_base = 0; |
3087 | SCHEME_V->dump_base = 0; |
3079 | dump_stack_reset (SCHEME_A); |
3088 | dump_stack_reset (SCHEME_A); |
3080 | } |
3089 | } |
3081 | |
3090 | |
3082 | static void |
3091 | ecb_cold static void |
3083 | dump_stack_free (SCHEME_P) |
3092 | dump_stack_free (SCHEME_P) |
3084 | { |
3093 | { |
3085 | free (SCHEME_V->dump_base); |
3094 | free (SCHEME_V->dump_base); |
3086 | SCHEME_V->dump_base = 0; |
3095 | SCHEME_V->dump_base = 0; |
3087 | SCHEME_V->dump = (pointer)0; |
3096 | SCHEME_V->dump = (pointer)0; |
3088 | SCHEME_V->dump_size = 0; |
3097 | SCHEME_V->dump_size = 0; |
3089 | } |
3098 | } |
3090 | |
3099 | |
3091 | static void |
3100 | ecb_cold static void |
3092 | dump_stack_mark (SCHEME_P) |
3101 | dump_stack_mark (SCHEME_P) |
3093 | { |
3102 | { |
3094 | int nframes = (uintptr_t)SCHEME_V->dump; |
3103 | int nframes = (uintptr_t)SCHEME_V->dump; |
3095 | int i; |
3104 | int i; |
3096 | |
3105 | |
… | |
… | |
3102 | mark (frame->envir); |
3111 | mark (frame->envir); |
3103 | mark (frame->code); |
3112 | mark (frame->code); |
3104 | } |
3113 | } |
3105 | } |
3114 | } |
3106 | |
3115 | |
3107 | static pointer |
3116 | ecb_cold static pointer |
3108 | ss_get_cont (SCHEME_P) |
3117 | ss_get_cont (SCHEME_P) |
3109 | { |
3118 | { |
3110 | int nframes = (uintptr_t)SCHEME_V->dump; |
3119 | int nframes = (uintptr_t)SCHEME_V->dump; |
3111 | int i; |
3120 | int i; |
3112 | |
3121 | |
… | |
… | |
3124 | } |
3133 | } |
3125 | |
3134 | |
3126 | return cont; |
3135 | return cont; |
3127 | } |
3136 | } |
3128 | |
3137 | |
3129 | static void |
3138 | ecb_cold static void |
3130 | ss_set_cont (SCHEME_P_ pointer cont) |
3139 | ss_set_cont (SCHEME_P_ pointer cont) |
3131 | { |
3140 | { |
3132 | int i = 0; |
3141 | int i = 0; |
3133 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3142 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3134 | |
3143 | |
3135 | while (cont != NIL) |
3144 | while (cont != NIL) |
3136 | { |
3145 | { |
3137 | frame->op = ivalue (car (cont)); cont = cdr (cont); |
3146 | frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont); |
3138 | frame->args = car (cont) ; cont = cdr (cont); |
3147 | frame->args = car (cont) ; cont = cdr (cont); |
3139 | frame->envir = car (cont) ; cont = cdr (cont); |
3148 | frame->envir = car (cont) ; cont = cdr (cont); |
3140 | frame->code = car (cont) ; cont = cdr (cont); |
3149 | frame->code = car (cont) ; cont = cdr (cont); |
3141 | |
3150 | |
3142 | ++frame; |
3151 | ++frame; |
3143 | ++i; |
3152 | ++i; |
3144 | } |
3153 | } |
3145 | |
3154 | |
3146 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3155 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3147 | } |
3156 | } |
3148 | |
3157 | |
3149 | #else |
3158 | #else |
3150 | |
3159 | |
3151 | static INLINE void |
3160 | ecb_cold void |
3152 | dump_stack_reset (SCHEME_P) |
3161 | dump_stack_reset (SCHEME_P) |
3153 | { |
3162 | { |
3154 | SCHEME_V->dump = NIL; |
3163 | SCHEME_V->dump = NIL; |
3155 | } |
3164 | } |
3156 | |
3165 | |
3157 | static INLINE void |
3166 | ecb_cold void |
3158 | dump_stack_initialize (SCHEME_P) |
3167 | dump_stack_initialize (SCHEME_P) |
3159 | { |
3168 | { |
3160 | dump_stack_reset (SCHEME_A); |
3169 | dump_stack_reset (SCHEME_A); |
3161 | } |
3170 | } |
3162 | |
3171 | |
3163 | static void |
3172 | ecb_cold static void |
3164 | dump_stack_free (SCHEME_P) |
3173 | dump_stack_free (SCHEME_P) |
3165 | { |
3174 | { |
3166 | SCHEME_V->dump = NIL; |
3175 | SCHEME_V->dump = NIL; |
3167 | } |
3176 | } |
3168 | |
3177 | |
3169 | static int |
3178 | ecb_hot static int |
3170 | xs_return (SCHEME_P_ pointer a) |
3179 | xs_return (SCHEME_P_ pointer a) |
3171 | { |
3180 | { |
3172 | pointer dump = SCHEME_V->dump; |
3181 | pointer dump = SCHEME_V->dump; |
3173 | |
3182 | |
3174 | SCHEME_V->value = a; |
3183 | SCHEME_V->value = a; |
3175 | |
3184 | |
3176 | if (dump == NIL) |
3185 | if (dump == NIL) |
3177 | return -1; |
3186 | return -1; |
3178 | |
3187 | |
3179 | SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); |
3188 | SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump); |
3180 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3189 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3181 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3190 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3182 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3191 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3183 | |
3192 | |
3184 | SCHEME_V->dump = dump; |
3193 | SCHEME_V->dump = dump; |
3185 | |
3194 | |
3186 | return 0; |
3195 | return 0; |
3187 | } |
3196 | } |
3188 | |
3197 | |
3189 | static void |
3198 | ecb_hot static void |
3190 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3199 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3191 | { |
3200 | { |
3192 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3201 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3193 | cons (args, |
3202 | cons (args, |
3194 | cons (SCHEME_V->envir, |
3203 | cons (SCHEME_V->envir, |
3195 | cons (code, |
3204 | cons (code, |
3196 | SCHEME_V->dump)))); |
3205 | SCHEME_V->dump)))); |
3197 | } |
3206 | } |
3198 | |
3207 | |
|
|
3208 | ecb_cold static void |
|
|
3209 | dump_stack_mark (SCHEME_P) |
|
|
3210 | { |
|
|
3211 | mark (SCHEME_V->dump); |
|
|
3212 | } |
|
|
3213 | |
|
|
3214 | ecb_cold static pointer |
|
|
3215 | ss_get_cont (SCHEME_P) |
|
|
3216 | { |
|
|
3217 | return SCHEME_V->dump; |
|
|
3218 | } |
|
|
3219 | |
|
|
3220 | ecb_cold static void |
|
|
3221 | ss_set_cont (SCHEME_P_ pointer cont) |
|
|
3222 | { |
|
|
3223 | SCHEME_V->dump = cont; |
|
|
3224 | } |
|
|
3225 | |
|
|
3226 | #endif |
|
|
3227 | |
|
|
3228 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
|
|
3229 | |
|
|
3230 | #if EXPERIMENT |
|
|
3231 | |
|
|
3232 | static int |
|
|
3233 | dtree (SCHEME_P_ int indent, pointer x) |
|
|
3234 | { |
|
|
3235 | int c; |
|
|
3236 | |
|
|
3237 | if (is_syntax (x)) |
|
|
3238 | { |
|
|
3239 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3240 | return 8 + 8; |
|
|
3241 | } |
|
|
3242 | |
|
|
3243 | if (x == NIL) |
|
|
3244 | { |
|
|
3245 | printf ("%*sNIL\n", indent, ""); |
|
|
3246 | return 3; |
|
|
3247 | } |
|
|
3248 | |
|
|
3249 | switch (type (x)) |
|
|
3250 | { |
|
|
3251 | case T_INTEGER: |
|
|
3252 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3253 | return 32+8; |
|
|
3254 | |
|
|
3255 | case T_SYMBOL: |
|
|
3256 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3257 | return 24+8; |
|
|
3258 | |
|
|
3259 | case T_CLOSURE: |
|
|
3260 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3261 | dtree (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3262 | return 32 + dtree (SCHEME_A_ indent + 3, car (x)); |
|
|
3263 | |
|
|
3264 | case T_PAIR: |
|
|
3265 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3266 | c = dtree (SCHEME_A_ indent + 3, car (x)); |
|
|
3267 | c += dtree (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3268 | return c + 1; |
|
|
3269 | |
|
|
3270 | case T_PORT: |
|
|
3271 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3272 | return 24+8; |
|
|
3273 | |
|
|
3274 | case T_VECTOR: |
|
|
3275 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3276 | return 24+8; |
|
|
3277 | |
|
|
3278 | case T_ENVIRONMENT: |
|
|
3279 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3280 | return 0 + dtree (SCHEME_A_ indent + 3, car (x)); |
|
|
3281 | |
|
|
3282 | default: |
|
|
3283 | printf ("unhandled type %d\n", type (x)); |
|
|
3284 | break; |
|
|
3285 | } |
|
|
3286 | } |
|
|
3287 | |
|
|
3288 | #define DUMP(t) do { printf ("DUMP %s:%d\n", __FILE__, __LINE__); dtree (SCHEME_A_ 0, (t)); } while (0) |
|
|
3289 | |
|
|
3290 | typedef void *stream[1]; |
|
|
3291 | |
|
|
3292 | #define stream_init() { 0 } |
|
|
3293 | #define stream_data(s) ((char *)(s)[0] + sizeof (uint32_t) * 2) |
|
|
3294 | #define stream_size(s) (((uint32_t *)(s)[0])[0] - sizeof (uint32_t) * 2) |
|
|
3295 | #define stream_free(s) free (s[0]) |
|
|
3296 | |
|
|
3297 | ecb_cold static void |
|
|
3298 | stream_put (stream s, uint8_t byte) |
|
|
3299 | { |
|
|
3300 | uint32_t *sp = *s; |
|
|
3301 | uint32_t size = sizeof (uint32_t) * 2; |
|
|
3302 | uint32_t offs = size; |
|
|
3303 | |
|
|
3304 | if (ecb_expect_true (sp)) |
|
|
3305 | { |
|
|
3306 | offs = sp[0]; |
|
|
3307 | size = sp[1]; |
|
|
3308 | } |
|
|
3309 | |
|
|
3310 | if (ecb_expect_false (offs == size)) |
|
|
3311 | { |
|
|
3312 | size *= 2; |
|
|
3313 | sp = realloc (sp, size); |
|
|
3314 | *s = sp; |
|
|
3315 | sp[1] = size; |
|
|
3316 | |
|
|
3317 | } |
|
|
3318 | |
|
|
3319 | ((uint8_t *)sp)[offs++] = byte; |
|
|
3320 | sp[0] = offs; |
|
|
3321 | } |
|
|
3322 | |
|
|
3323 | ecb_cold static void |
|
|
3324 | stream_put_v (stream s, uint32_t v) |
|
|
3325 | { |
|
|
3326 | while (v > 0x7f) |
|
|
3327 | { |
|
|
3328 | stream_put (s, v | 0x80); |
|
|
3329 | v >>= 7; |
|
|
3330 | } |
|
|
3331 | |
|
|
3332 | stream_put (s, v); |
|
|
3333 | } |
|
|
3334 | |
|
|
3335 | ecb_cold static void |
|
|
3336 | stream_put_tv (stream s, int bop, uint32_t v) |
|
|
3337 | { |
|
|
3338 | printf ("put tv %d %d\n", bop, v);//D |
|
|
3339 | stream_put (s, bop); |
|
|
3340 | stream_put_v (s, v); |
|
|
3341 | } |
|
|
3342 | |
|
|
3343 | ecb_cold static void |
|
|
3344 | stream_put_stream (stream s, stream o) |
|
|
3345 | { |
|
|
3346 | uint32_t i; |
|
|
3347 | |
|
|
3348 | for (i = 0; i < stream_size (o); ++i) |
|
|
3349 | stream_put (s, stream_data (o)[i]); |
|
|
3350 | |
|
|
3351 | stream_free (o); |
|
|
3352 | } |
|
|
3353 | |
|
|
3354 | ecb_cold static uint32_t |
|
|
3355 | cell_id (SCHEME_P_ pointer x) |
|
|
3356 | { |
|
|
3357 | struct cell *p = CELL (x); |
|
|
3358 | int i; |
|
|
3359 | |
|
|
3360 | for (i = SCHEME_V->last_cell_seg; i >= 0; --i) |
|
|
3361 | if (SCHEME_V->cell_seg[i] <= p && p < SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize[i]) |
|
|
3362 | return i | ((p - SCHEME_V->cell_seg[i]) << CELL_NSEGMENT_LOG); |
|
|
3363 | |
|
|
3364 | abort (); |
|
|
3365 | } |
|
|
3366 | |
|
|
3367 | // calculates a (preferably small) integer that makes it possible to find |
|
|
3368 | // the symbol again. if pointers were offsets into a memory area... until |
|
|
3369 | // then, we return segment number in the low bits, and offset in the high |
|
|
3370 | // bits. |
|
|
3371 | // also, this function must never return 0. |
|
|
3372 | ecb_cold static uint32_t |
|
|
3373 | symbol_id (SCHEME_P_ pointer sym) |
|
|
3374 | { |
|
|
3375 | return cell_id (SCHEME_A_ sym); |
|
|
3376 | } |
|
|
3377 | |
|
|
3378 | enum byteop |
|
|
3379 | { |
|
|
3380 | BOP_NIL, |
|
|
3381 | BOP_INTEGER, |
|
|
3382 | BOP_SYMBOL, |
|
|
3383 | BOP_DATUM, |
|
|
3384 | BOP_LIST_BEG, |
|
|
3385 | BOP_LIST_END, |
|
|
3386 | BOP_IF, |
|
|
3387 | BOP_AND, |
|
|
3388 | BOP_OR, |
|
|
3389 | BOP_CASE, |
|
|
3390 | BOP_COND, |
|
|
3391 | BOP_LET, |
|
|
3392 | BOP_LETAST, |
|
|
3393 | BOP_LETREC, |
|
|
3394 | BOP_DEFINE, |
|
|
3395 | BOP_MACRO, |
|
|
3396 | BOP_SET, |
|
|
3397 | BOP_BEGIN, |
|
|
3398 | BOP_LAMBDA, |
|
|
3399 | BOP_DELAY, |
|
|
3400 | BOP_OP, |
|
|
3401 | }; |
|
|
3402 | |
|
|
3403 | ecb_cold static void compile_expr (SCHEME_P_ stream s, pointer x); |
|
|
3404 | |
|
|
3405 | ecb_cold static void |
|
|
3406 | compile_list (SCHEME_P_ stream s, pointer x) |
|
|
3407 | { |
|
|
3408 | // TODO: improper list |
|
|
3409 | |
|
|
3410 | for (; x != NIL; x = cdr (x)) |
|
|
3411 | { |
|
|
3412 | stream t = stream_init (); |
|
|
3413 | compile_expr (SCHEME_A_ t, car (x)); |
|
|
3414 | stream_put_v (s, stream_size (t)); |
|
|
3415 | stream_put_stream (s, t); |
|
|
3416 | } |
|
|
3417 | |
|
|
3418 | stream_put_v (s, 0); |
|
|
3419 | } |
|
|
3420 | |
3199 | static void |
3421 | static void |
3200 | dump_stack_mark (SCHEME_P) |
3422 | compile_if (SCHEME_P_ stream s, pointer cond, pointer ift, pointer iff) |
3201 | { |
3423 | { |
3202 | mark (SCHEME_V->dump); |
3424 | stream sift = stream_init (); compile_expr (SCHEME_A_ sift, ift); |
|
|
3425 | |
|
|
3426 | stream_put (s, BOP_IF); |
|
|
3427 | compile_expr (SCHEME_A_ s, cond); |
|
|
3428 | stream_put_v (s, stream_size (sift)); |
|
|
3429 | stream_put_stream (s, sift); |
|
|
3430 | compile_expr (SCHEME_A_ s, iff); |
|
|
3431 | } |
|
|
3432 | |
|
|
3433 | typedef uint32_t stream_fixup; |
|
|
3434 | |
|
|
3435 | static stream_fixup |
|
|
3436 | stream_put_fixup (stream s) |
|
|
3437 | { |
|
|
3438 | stream_put (s, 0); |
|
|
3439 | stream_put (s, 0); |
|
|
3440 | |
|
|
3441 | return stream_size (s); |
|
|
3442 | } |
|
|
3443 | |
|
|
3444 | static void |
|
|
3445 | stream_fix_fixup (stream s, stream_fixup fixup, uint32_t target) |
|
|
3446 | { |
|
|
3447 | target -= fixup; |
|
|
3448 | assert (target < (1 << 14)); |
|
|
3449 | stream_data (s)[fixup - 2] = target | 0x80; |
|
|
3450 | stream_data (s)[fixup - 1] = target >> 7; |
|
|
3451 | } |
|
|
3452 | |
|
|
3453 | static void |
|
|
3454 | compile_and_or (SCHEME_P_ stream s, int and, pointer x) |
|
|
3455 | { |
|
|
3456 | for (; cdr (x) != NIL; x = cdr (x)) |
|
|
3457 | { |
|
|
3458 | stream t = stream_init (); |
|
|
3459 | compile_expr (SCHEME_A_ t, car (x)); |
|
|
3460 | stream_put_v (s, stream_size (t)); |
|
|
3461 | stream_put_stream (s, t); |
|
|
3462 | } |
|
|
3463 | |
|
|
3464 | stream_put_v (s, 0); |
|
|
3465 | } |
|
|
3466 | |
|
|
3467 | static void |
|
|
3468 | compile_case (SCHEME_P_ stream s, pointer x) |
|
|
3469 | { |
|
|
3470 | compile_expr (SCHEME_A_ s, caar (x)); |
|
|
3471 | |
|
|
3472 | for (;;) |
|
|
3473 | { |
|
|
3474 | x = cdr (x); |
|
|
3475 | |
|
|
3476 | if (x == NIL) |
|
|
3477 | break; |
|
|
3478 | |
|
|
3479 | compile_expr (SCHEME_A_ s, caar (x)); |
|
|
3480 | stream t = stream_init (); compile_expr (SCHEME_A_ t, cdar (x)); |
|
|
3481 | stream_put_v (s, stream_size (t)); |
|
|
3482 | stream_put_stream (s, t); |
|
|
3483 | } |
|
|
3484 | |
|
|
3485 | stream_put_v (s, 0); |
|
|
3486 | } |
|
|
3487 | |
|
|
3488 | static void |
|
|
3489 | compile_cond (SCHEME_P_ stream s, pointer x) |
|
|
3490 | { |
|
|
3491 | for ( ; x != NIL; x = cdr (x)) |
|
|
3492 | { |
|
|
3493 | compile_expr (SCHEME_A_ s, caar (x)); |
|
|
3494 | stream t = stream_init (); compile_expr (SCHEME_A_ t, cdar (x)); |
|
|
3495 | stream_put_v (s, stream_size (t)); |
|
|
3496 | stream_put_stream (s, t); |
|
|
3497 | } |
|
|
3498 | |
|
|
3499 | stream_put_v (s, 0); |
3203 | } |
3500 | } |
3204 | |
3501 | |
3205 | static pointer |
3502 | static pointer |
3206 | ss_get_cont (SCHEME_P) |
3503 | lookup (SCHEME_P_ pointer x) |
3207 | { |
3504 | { |
3208 | return SCHEME_V->dump; |
3505 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, x, 1); |
3209 | } |
|
|
3210 | |
3506 | |
3211 | static void |
3507 | if (x != NIL) |
3212 | ss_set_cont (SCHEME_P_ pointer cont) |
3508 | x = slot_value_in_env (x); |
3213 | { |
|
|
3214 | SCHEME_V->dump = cont; |
|
|
3215 | } |
|
|
3216 | |
3509 | |
3217 | #endif |
3510 | return x; |
|
|
3511 | } |
3218 | |
3512 | |
3219 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3513 | ecb_cold static void |
|
|
3514 | compile_expr (SCHEME_P_ stream s, pointer x) |
|
|
3515 | { |
|
|
3516 | if (x == NIL) |
|
|
3517 | { |
|
|
3518 | stream_put (s, BOP_NIL); |
|
|
3519 | return; |
|
|
3520 | } |
3220 | |
3521 | |
3221 | static int |
3522 | if (is_pair (x)) |
|
|
3523 | { |
|
|
3524 | pointer head = car (x); |
|
|
3525 | |
|
|
3526 | if (is_syntax (head)) |
|
|
3527 | { |
|
|
3528 | int syn = syntaxnum (head); |
|
|
3529 | x = cdr (x); |
|
|
3530 | |
|
|
3531 | switch (syntaxnum (head)) |
|
|
3532 | { |
|
|
3533 | case OP_IF0: /* if */ |
|
|
3534 | stream_put_v (s, BOP_IF); |
|
|
3535 | compile_if (SCHEME_A_ s, car (x), cadr (x), caddr (x)); |
|
|
3536 | break; |
|
|
3537 | |
|
|
3538 | case OP_OR0: /* or */ |
|
|
3539 | stream_put_v (s, BOP_OR); |
|
|
3540 | compile_and_or (SCHEME_A_ s, 0, x); |
|
|
3541 | break; |
|
|
3542 | |
|
|
3543 | case OP_AND0: /* and */ |
|
|
3544 | stream_put_v (s, BOP_AND); |
|
|
3545 | compile_and_or (SCHEME_A_ s, 1, x); |
|
|
3546 | break; |
|
|
3547 | |
|
|
3548 | case OP_CASE0: /* case */ |
|
|
3549 | stream_put_v (s, BOP_CASE); |
|
|
3550 | compile_case (SCHEME_A_ s, x); |
|
|
3551 | break; |
|
|
3552 | |
|
|
3553 | case OP_COND0: /* cond */ |
|
|
3554 | stream_put_v (s, BOP_COND); |
|
|
3555 | compile_cond (SCHEME_A_ s, x); |
|
|
3556 | break; |
|
|
3557 | |
|
|
3558 | case OP_LET0: /* let */ |
|
|
3559 | case OP_LET0AST: /* let* */ |
|
|
3560 | case OP_LET0REC: /* letrec */ |
|
|
3561 | switch (syn) |
|
|
3562 | { |
|
|
3563 | case OP_LET0: stream_put (s, BOP_LET ); break; |
|
|
3564 | case OP_LET0AST: stream_put (s, BOP_LETAST); break; |
|
|
3565 | case OP_LET0REC: stream_put (s, BOP_LETREC); break; |
|
|
3566 | } |
|
|
3567 | |
|
|
3568 | { |
|
|
3569 | pointer bindings = car (x); |
|
|
3570 | pointer body = cadr (x); |
|
|
3571 | |
|
|
3572 | for (x = bindings; x != NIL; x = cdr (x)) |
|
|
3573 | { |
|
|
3574 | pointer init = NIL; |
|
|
3575 | pointer var = car (x); |
|
|
3576 | |
|
|
3577 | if (is_pair (var)) |
|
|
3578 | { |
|
|
3579 | init = cdr (var); |
|
|
3580 | var = car (var); |
|
|
3581 | } |
|
|
3582 | |
|
|
3583 | stream_put_v (s, symbol_id (SCHEME_A_ var)); |
|
|
3584 | compile_expr (SCHEME_A_ s, init); |
|
|
3585 | } |
|
|
3586 | |
|
|
3587 | stream_put_v (s, 0); |
|
|
3588 | compile_expr (SCHEME_A_ s, body); |
|
|
3589 | } |
|
|
3590 | break; |
|
|
3591 | |
|
|
3592 | case OP_DEF0: /* define */ |
|
|
3593 | case OP_MACRO0: /* macro */ |
|
|
3594 | stream_put (s, syntaxnum (head) == OP_DEF0 ? BOP_DEFINE : BOP_MACRO); |
|
|
3595 | stream_put_v (s, cell_id (SCHEME_A_ car (x))); |
|
|
3596 | compile_expr (SCHEME_A_ s, cadr (x)); |
|
|
3597 | break; |
|
|
3598 | |
|
|
3599 | case OP_SET0: /* set! */ |
|
|
3600 | stream_put (s, BOP_SET); |
|
|
3601 | stream_put_v (s, symbol_id (SCHEME_A_ car (x))); |
|
|
3602 | compile_expr (SCHEME_A_ s, cadr (x)); |
|
|
3603 | break; |
|
|
3604 | |
|
|
3605 | case OP_BEGIN: /* begin */ |
|
|
3606 | stream_put (s, BOP_BEGIN); |
|
|
3607 | compile_list (SCHEME_A_ s, x); |
|
|
3608 | return; |
|
|
3609 | |
|
|
3610 | case OP_QUOTE: /* quote */ |
|
|
3611 | stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x)); |
|
|
3612 | break; |
|
|
3613 | |
|
|
3614 | case OP_DELAY: /* delay */ |
|
|
3615 | stream_put (s, BOP_DELAY); |
|
|
3616 | compile_expr (SCHEME_A_ s, x); |
|
|
3617 | break; |
|
|
3618 | |
|
|
3619 | case OP_LAMBDA: /* lambda */ |
|
|
3620 | { |
|
|
3621 | pointer formals = car (x); |
|
|
3622 | pointer body = cadr (x); |
|
|
3623 | |
|
|
3624 | stream_put (s, BOP_LAMBDA); |
|
|
3625 | |
|
|
3626 | for (; is_pair (formals); formals = cdr (formals)) |
|
|
3627 | stream_put_v (s, symbol_id (SCHEME_A_ car (formals))); |
|
|
3628 | |
|
|
3629 | stream_put_v (s, 0); |
|
|
3630 | stream_put_v (s, formals == NIL ? 0 : symbol_id (SCHEME_A_ formals)); |
|
|
3631 | |
|
|
3632 | compile_expr (SCHEME_A_ s, body); |
|
|
3633 | } |
|
|
3634 | break; |
|
|
3635 | |
|
|
3636 | case OP_C0STREAM:/* cons-stream */ |
|
|
3637 | abort (); |
|
|
3638 | break; |
|
|
3639 | } |
|
|
3640 | |
|
|
3641 | return; |
|
|
3642 | } |
|
|
3643 | |
|
|
3644 | pointer m = lookup (SCHEME_A_ head); |
|
|
3645 | |
|
|
3646 | if (is_macro (m)) |
|
|
3647 | { |
|
|
3648 | s_save (SCHEME_A_ OP_DEBUG2, SCHEME_V->args, SCHEME_V->code); |
|
|
3649 | SCHEME_V->code = m; |
|
|
3650 | SCHEME_V->args = cons (x, NIL); |
|
|
3651 | Eval_Cycle (SCHEME_A_ OP_APPLY); |
|
|
3652 | x = SCHEME_V->value; |
|
|
3653 | compile_expr (SCHEME_A_ s, SCHEME_V->value); |
|
|
3654 | return; |
|
|
3655 | } |
|
|
3656 | |
|
|
3657 | stream_put (s, BOP_LIST_BEG); |
|
|
3658 | |
|
|
3659 | for (; x != NIL; x = cdr (x)) |
|
|
3660 | compile_expr (SCHEME_A_ s, car (x)); |
|
|
3661 | |
|
|
3662 | stream_put (s, BOP_LIST_END); |
|
|
3663 | return; |
|
|
3664 | } |
|
|
3665 | |
|
|
3666 | switch (type (x)) |
|
|
3667 | { |
|
|
3668 | case T_INTEGER: |
|
|
3669 | { |
|
|
3670 | IVALUE iv = ivalue_unchecked (x); |
|
|
3671 | iv = iv < 0 ? ((~(uint32_t)iv) << 1) | 1 : (uint32_t)iv << 1; |
|
|
3672 | stream_put_tv (s, BOP_INTEGER, iv); |
|
|
3673 | } |
|
|
3674 | return; |
|
|
3675 | |
|
|
3676 | case T_SYMBOL: |
|
|
3677 | if (0) |
|
|
3678 | { |
|
|
3679 | // no can do without more analysis |
|
|
3680 | pointer m = lookup (SCHEME_A_ x); |
|
|
3681 | |
|
|
3682 | if (is_proc (m)) |
|
|
3683 | { |
|
|
3684 | printf ("compile proc %s %d\n", procname(m), procnum(m)); |
|
|
3685 | stream_put_tv (s, BOP_SYMBOL, BOP_OP + procnum (m)); |
|
|
3686 | } |
|
|
3687 | else |
|
|
3688 | stream_put_tv (s, BOP_SYMBOL, symbol_id (SCHEME_A_ x)); |
|
|
3689 | } |
|
|
3690 | |
|
|
3691 | stream_put_tv (s, BOP_SYMBOL, symbol_id (SCHEME_A_ x)); |
|
|
3692 | return; |
|
|
3693 | |
|
|
3694 | default: |
|
|
3695 | stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x)); |
|
|
3696 | break; |
|
|
3697 | } |
|
|
3698 | } |
|
|
3699 | |
|
|
3700 | ecb_cold static int |
|
|
3701 | compile_closure (SCHEME_P_ pointer p) |
|
|
3702 | { |
|
|
3703 | stream s = stream_init (); |
|
|
3704 | |
|
|
3705 | compile_list (SCHEME_A_ s, cdar (p)); |
|
|
3706 | |
|
|
3707 | FILE *xxd = popen ("xxd", "we"); |
|
|
3708 | fwrite (stream_data (s), 1, stream_size (s), xxd); |
|
|
3709 | fclose (xxd); |
|
|
3710 | |
|
|
3711 | return stream_size (s); |
|
|
3712 | } |
|
|
3713 | |
|
|
3714 | #endif |
|
|
3715 | |
|
|
3716 | /* syntax, eval, core, ... */ |
|
|
3717 | ecb_hot static int |
3222 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3718 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3223 | { |
3719 | { |
3224 | pointer args = SCHEME_V->args; |
3720 | pointer args = SCHEME_V->args; |
3225 | pointer x, y; |
3721 | pointer x, y; |
3226 | |
3722 | |
3227 | switch (op) |
3723 | switch (op) |
3228 | { |
3724 | { |
|
|
3725 | #if EXPERIMENT //D |
|
|
3726 | case OP_DEBUG: |
|
|
3727 | { |
|
|
3728 | uint32_t len = compile_closure (SCHEME_A_ car (args)); |
|
|
3729 | printf ("len = %d\n", len); |
|
|
3730 | printf ("\n"); |
|
|
3731 | s_return (S_T); |
|
|
3732 | } |
|
|
3733 | |
|
|
3734 | case OP_DEBUG2: |
|
|
3735 | return -1; |
|
|
3736 | #endif |
|
|
3737 | |
3229 | case OP_LOAD: /* load */ |
3738 | case OP_LOAD: /* load */ |
3230 | if (file_interactive (SCHEME_A)) |
3739 | if (file_interactive (SCHEME_A)) |
3231 | { |
3740 | { |
3232 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3741 | putstr (SCHEME_A_ "Loading "); |
3233 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3742 | putstr (SCHEME_A_ strvalue (car (args))); |
|
|
3743 | putcharacter (SCHEME_A_ '\n'); |
3234 | } |
3744 | } |
3235 | |
3745 | |
3236 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3746 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3237 | Error_1 ("unable to open", car (args)); |
3747 | Error_1 ("unable to open", car (args)); |
3238 | else |
3748 | |
3239 | { |
|
|
3240 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3749 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3241 | s_goto (OP_T0LVL); |
3750 | s_goto (OP_T0LVL); |
3242 | } |
|
|
3243 | |
3751 | |
3244 | case OP_T0LVL: /* top level */ |
3752 | case OP_T0LVL: /* top level */ |
3245 | |
3753 | |
3246 | /* If we reached the end of file, this loop is done. */ |
3754 | /* If we reached the end of file, this loop is done. */ |
3247 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3755 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3248 | { |
3756 | { |
3249 | if (SCHEME_V->file_i == 0) |
3757 | if (SCHEME_V->file_i == 0) |
3250 | { |
3758 | { |
3251 | SCHEME_V->args = NIL; |
3759 | SCHEME_V->args = NIL; |
3252 | s_goto (OP_QUIT); |
3760 | s_goto (OP_QUIT); |
… | |
… | |
3263 | /* If interactive, be nice to user. */ |
3771 | /* If interactive, be nice to user. */ |
3264 | if (file_interactive (SCHEME_A)) |
3772 | if (file_interactive (SCHEME_A)) |
3265 | { |
3773 | { |
3266 | SCHEME_V->envir = SCHEME_V->global_env; |
3774 | SCHEME_V->envir = SCHEME_V->global_env; |
3267 | dump_stack_reset (SCHEME_A); |
3775 | dump_stack_reset (SCHEME_A); |
3268 | putstr (SCHEME_A_ "\n"); |
3776 | putcharacter (SCHEME_A_ '\n'); |
|
|
3777 | #if EXPERIMENT |
|
|
3778 | system ("ps v $PPID"); |
|
|
3779 | #endif |
3269 | putstr (SCHEME_A_ prompt); |
3780 | putstr (SCHEME_A_ prompt); |
3270 | } |
3781 | } |
3271 | |
3782 | |
3272 | /* Set up another iteration of REPL */ |
3783 | /* Set up another iteration of REPL */ |
3273 | SCHEME_V->nesting = 0; |
3784 | SCHEME_V->nesting = 0; |
… | |
… | |
3308 | { |
3819 | { |
3309 | SCHEME_V->print_flag = 1; |
3820 | SCHEME_V->print_flag = 1; |
3310 | SCHEME_V->args = SCHEME_V->value; |
3821 | SCHEME_V->args = SCHEME_V->value; |
3311 | s_goto (OP_P0LIST); |
3822 | s_goto (OP_P0LIST); |
3312 | } |
3823 | } |
3313 | else |
3824 | |
3314 | s_return (SCHEME_V->value); |
3825 | s_return (SCHEME_V->value); |
3315 | |
3826 | |
3316 | case OP_EVAL: /* main part of evaluation */ |
3827 | case OP_EVAL: /* main part of evaluation */ |
3317 | #if USE_TRACING |
3828 | #if USE_TRACING |
3318 | if (SCHEME_V->tracing) |
3829 | if (SCHEME_V->tracing) |
3319 | { |
3830 | { |
… | |
… | |
3330 | #endif |
3841 | #endif |
3331 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3842 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3332 | { |
3843 | { |
3333 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3844 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3334 | |
3845 | |
3335 | if (x != NIL) |
3846 | if (x == NIL) |
3336 | s_return (slot_value_in_env (x)); |
|
|
3337 | else |
|
|
3338 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3847 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3848 | |
|
|
3849 | s_return (slot_value_in_env (x)); |
3339 | } |
3850 | } |
3340 | else if (is_pair (SCHEME_V->code)) |
3851 | else if (is_pair (SCHEME_V->code)) |
3341 | { |
3852 | { |
3342 | x = car (SCHEME_V->code); |
3853 | x = car (SCHEME_V->code); |
3343 | |
3854 | |
… | |
… | |
3352 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3863 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3353 | SCHEME_V->code = x; |
3864 | SCHEME_V->code = x; |
3354 | s_goto (OP_EVAL); |
3865 | s_goto (OP_EVAL); |
3355 | } |
3866 | } |
3356 | } |
3867 | } |
3357 | else |
3868 | |
3358 | s_return (SCHEME_V->code); |
3869 | s_return (SCHEME_V->code); |
3359 | |
3870 | |
3360 | case OP_E0ARGS: /* eval arguments */ |
3871 | case OP_E0ARGS: /* eval arguments */ |
3361 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3872 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3362 | { |
3873 | { |
3363 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3874 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3364 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3875 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3365 | SCHEME_V->code = SCHEME_V->value; |
3876 | SCHEME_V->code = SCHEME_V->value; |
3366 | s_goto (OP_APPLY); |
3877 | s_goto (OP_APPLY); |
3367 | } |
3878 | } |
3368 | else |
3879 | |
3369 | { |
|
|
3370 | SCHEME_V->code = cdr (SCHEME_V->code); |
3880 | SCHEME_V->code = cdr (SCHEME_V->code); |
3371 | s_goto (OP_E1ARGS); |
3881 | s_goto (OP_E1ARGS); |
3372 | } |
|
|
3373 | |
3882 | |
3374 | case OP_E1ARGS: /* eval arguments */ |
3883 | case OP_E1ARGS: /* eval arguments */ |
3375 | args = cons (SCHEME_V->value, args); |
3884 | args = cons (SCHEME_V->value, args); |
3376 | |
3885 | |
3377 | if (is_pair (SCHEME_V->code)) /* continue */ |
3886 | if (is_pair (SCHEME_V->code)) /* continue */ |
… | |
… | |
3388 | SCHEME_V->args = cdr (args); |
3897 | SCHEME_V->args = cdr (args); |
3389 | s_goto (OP_APPLY); |
3898 | s_goto (OP_APPLY); |
3390 | } |
3899 | } |
3391 | |
3900 | |
3392 | #if USE_TRACING |
3901 | #if USE_TRACING |
3393 | |
|
|
3394 | case OP_TRACING: |
3902 | case OP_TRACING: |
3395 | { |
3903 | { |
3396 | int tr = SCHEME_V->tracing; |
3904 | int tr = SCHEME_V->tracing; |
3397 | |
3905 | |
3398 | SCHEME_V->tracing = ivalue (car (args)); |
3906 | SCHEME_V->tracing = ivalue_unchecked (car (args)); |
3399 | s_return (mk_integer (SCHEME_A_ tr)); |
3907 | s_return (mk_integer (SCHEME_A_ tr)); |
3400 | } |
3908 | } |
3401 | |
|
|
3402 | #endif |
3909 | #endif |
3403 | |
3910 | |
3404 | case OP_APPLY: /* apply 'code' to 'args' */ |
3911 | case OP_APPLY: /* apply 'code' to 'args' */ |
3405 | #if USE_TRACING |
3912 | #if USE_TRACING |
3406 | if (SCHEME_V->tracing) |
3913 | if (SCHEME_V->tracing) |
… | |
… | |
3420 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3927 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3421 | else if (is_foreign (SCHEME_V->code)) |
3928 | else if (is_foreign (SCHEME_V->code)) |
3422 | { |
3929 | { |
3423 | /* Keep nested calls from GC'ing the arglist */ |
3930 | /* Keep nested calls from GC'ing the arglist */ |
3424 | push_recent_alloc (SCHEME_A_ args, NIL); |
3931 | push_recent_alloc (SCHEME_A_ args, NIL); |
3425 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3932 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3426 | |
3933 | |
3427 | s_return (x); |
3934 | s_return (x); |
3428 | } |
3935 | } |
3429 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3936 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3430 | { |
3937 | { |
… | |
… | |
3460 | else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ |
3967 | else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ |
3461 | { |
3968 | { |
3462 | ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); |
3969 | ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); |
3463 | s_return (args != NIL ? car (args) : NIL); |
3970 | s_return (args != NIL ? car (args) : NIL); |
3464 | } |
3971 | } |
3465 | else |
3972 | |
3466 | Error_0 ("illegal function"); |
3973 | Error_0 ("illegal function"); |
3467 | |
3974 | |
3468 | case OP_DOMACRO: /* do macro */ |
3975 | case OP_DOMACRO: /* do macro */ |
3469 | SCHEME_V->code = SCHEME_V->value; |
3976 | SCHEME_V->code = SCHEME_V->value; |
3470 | s_goto (OP_EVAL); |
3977 | s_goto (OP_EVAL); |
3471 | |
|
|
3472 | #if 1 |
|
|
3473 | |
3978 | |
3474 | case OP_LAMBDA: /* lambda */ |
3979 | case OP_LAMBDA: /* lambda */ |
3475 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3980 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3476 | set SCHEME_V->value fall thru */ |
3981 | set SCHEME_V->value fall thru */ |
3477 | { |
3982 | { |
… | |
… | |
3484 | SCHEME_V->code = slot_value_in_env (f); |
3989 | SCHEME_V->code = slot_value_in_env (f); |
3485 | s_goto (OP_APPLY); |
3990 | s_goto (OP_APPLY); |
3486 | } |
3991 | } |
3487 | |
3992 | |
3488 | SCHEME_V->value = SCHEME_V->code; |
3993 | SCHEME_V->value = SCHEME_V->code; |
3489 | /* Fallthru */ |
|
|
3490 | } |
3994 | } |
|
|
3995 | /* Fallthru */ |
3491 | |
3996 | |
3492 | case OP_LAMBDA1: |
3997 | case OP_LAMBDA1: |
3493 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3998 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3494 | |
|
|
3495 | #else |
|
|
3496 | |
|
|
3497 | case OP_LAMBDA: /* lambda */ |
|
|
3498 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
|
|
3499 | |
|
|
3500 | #endif |
|
|
3501 | |
3999 | |
3502 | case OP_MKCLOSURE: /* make-closure */ |
4000 | case OP_MKCLOSURE: /* make-closure */ |
3503 | x = car (args); |
4001 | x = car (args); |
3504 | |
4002 | |
3505 | if (car (x) == SCHEME_V->LAMBDA) |
4003 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
3544 | else |
4042 | else |
3545 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
4043 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3546 | |
4044 | |
3547 | s_return (SCHEME_V->code); |
4045 | s_return (SCHEME_V->code); |
3548 | |
4046 | |
3549 | |
|
|
3550 | case OP_DEFP: /* defined? */ |
4047 | case OP_DEFP: /* defined? */ |
3551 | x = SCHEME_V->envir; |
4048 | x = SCHEME_V->envir; |
3552 | |
4049 | |
3553 | if (cdr (args) != NIL) |
4050 | if (cdr (args) != NIL) |
3554 | x = cadr (args); |
4051 | x = cadr (args); |
… | |
… | |
3572 | s_return (SCHEME_V->value); |
4069 | s_return (SCHEME_V->value); |
3573 | } |
4070 | } |
3574 | else |
4071 | else |
3575 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
4072 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3576 | |
4073 | |
3577 | |
|
|
3578 | case OP_BEGIN: /* begin */ |
4074 | case OP_BEGIN: /* begin */ |
3579 | if (!is_pair (SCHEME_V->code)) |
4075 | if (!is_pair (SCHEME_V->code)) |
3580 | s_return (SCHEME_V->code); |
4076 | s_return (SCHEME_V->code); |
3581 | |
4077 | |
3582 | if (cdr (SCHEME_V->code) != NIL) |
4078 | if (cdr (SCHEME_V->code) != NIL) |
… | |
… | |
3593 | case OP_IF1: /* if */ |
4089 | case OP_IF1: /* if */ |
3594 | if (is_true (SCHEME_V->value)) |
4090 | if (is_true (SCHEME_V->value)) |
3595 | SCHEME_V->code = car (SCHEME_V->code); |
4091 | SCHEME_V->code = car (SCHEME_V->code); |
3596 | else |
4092 | else |
3597 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
4093 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
|
|
4094 | |
3598 | s_goto (OP_EVAL); |
4095 | s_goto (OP_EVAL); |
3599 | |
4096 | |
3600 | case OP_LET0: /* let */ |
4097 | case OP_LET0: /* let */ |
3601 | SCHEME_V->args = NIL; |
4098 | SCHEME_V->args = NIL; |
3602 | SCHEME_V->value = SCHEME_V->code; |
4099 | SCHEME_V->value = SCHEME_V->code; |
3603 | SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); |
4100 | SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); |
3604 | s_goto (OP_LET1); |
4101 | s_goto (OP_LET1); |
3605 | |
4102 | |
3606 | case OP_LET1: /* let (calculate parameters) */ |
4103 | case OP_LET1: /* let (calculate parameters) */ |
|
|
4104 | case OP_LET1REC: /* letrec (calculate parameters) */ |
3607 | args = cons (SCHEME_V->value, args); |
4105 | args = cons (SCHEME_V->value, args); |
3608 | |
4106 | |
3609 | if (is_pair (SCHEME_V->code)) /* continue */ |
4107 | if (is_pair (SCHEME_V->code)) /* continue */ |
3610 | { |
4108 | { |
3611 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
4109 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3612 | Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); |
4110 | Error_1 ("Bad syntax of binding spec in let/letrec:", car (SCHEME_V->code)); |
3613 | |
4111 | |
3614 | s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code)); |
4112 | s_save (SCHEME_A_ op, args, cdr (SCHEME_V->code)); |
3615 | SCHEME_V->code = cadar (SCHEME_V->code); |
4113 | SCHEME_V->code = cadar (SCHEME_V->code); |
3616 | SCHEME_V->args = NIL; |
4114 | SCHEME_V->args = NIL; |
3617 | s_goto (OP_EVAL); |
4115 | s_goto (OP_EVAL); |
3618 | } |
4116 | } |
3619 | else /* end */ |
4117 | |
3620 | { |
4118 | /* end */ |
3621 | args = reverse_in_place (SCHEME_A_ NIL, args); |
4119 | args = reverse_in_place (SCHEME_A_ NIL, args); |
3622 | SCHEME_V->code = car (args); |
4120 | SCHEME_V->code = car (args); |
3623 | SCHEME_V->args = cdr (args); |
4121 | SCHEME_V->args = cdr (args); |
3624 | s_goto (OP_LET2); |
4122 | s_goto (op == OP_LET1 ? OP_LET2 : OP_LET2REC); |
3625 | } |
|
|
3626 | |
4123 | |
3627 | case OP_LET2: /* let */ |
4124 | case OP_LET2: /* let */ |
3628 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
4125 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3629 | |
4126 | |
3630 | for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args; |
4127 | for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args; |
… | |
… | |
3634 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
4131 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
3635 | { |
4132 | { |
3636 | for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x)) |
4133 | for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x)) |
3637 | { |
4134 | { |
3638 | if (!is_pair (x)) |
4135 | if (!is_pair (x)) |
3639 | Error_1 ("Bad syntax of binding in let :", x); |
4136 | Error_1 ("Bad syntax of binding in let:", x); |
3640 | |
4137 | |
3641 | if (!is_list (SCHEME_A_ car (x))) |
4138 | if (!is_list (SCHEME_A_ car (x))) |
3642 | Error_1 ("Bad syntax of binding in let :", car (x)); |
4139 | Error_1 ("Bad syntax of binding in let:", car (x)); |
3643 | |
4140 | |
3644 | args = cons (caar (x), args); |
4141 | args = cons (caar (x), args); |
3645 | } |
4142 | } |
3646 | |
4143 | |
3647 | x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)), |
4144 | x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)), |
… | |
… | |
3664 | SCHEME_V->code = cdr (SCHEME_V->code); |
4161 | SCHEME_V->code = cdr (SCHEME_V->code); |
3665 | s_goto (OP_BEGIN); |
4162 | s_goto (OP_BEGIN); |
3666 | } |
4163 | } |
3667 | |
4164 | |
3668 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code))) |
4165 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code))) |
3669 | Error_1 ("Bad syntax of binding spec in let* :", car (SCHEME_V->code)); |
4166 | Error_1 ("Bad syntax of binding spec in let*:", car (SCHEME_V->code)); |
3670 | |
4167 | |
3671 | s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code)); |
4168 | s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code)); |
3672 | SCHEME_V->code = car (cdaar (SCHEME_V->code)); |
4169 | SCHEME_V->code = car (cdaar (SCHEME_V->code)); |
3673 | s_goto (OP_EVAL); |
4170 | s_goto (OP_EVAL); |
3674 | |
4171 | |
… | |
… | |
3685 | s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); |
4182 | s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); |
3686 | SCHEME_V->code = cadar (SCHEME_V->code); |
4183 | SCHEME_V->code = cadar (SCHEME_V->code); |
3687 | SCHEME_V->args = NIL; |
4184 | SCHEME_V->args = NIL; |
3688 | s_goto (OP_EVAL); |
4185 | s_goto (OP_EVAL); |
3689 | } |
4186 | } |
3690 | else /* end */ |
4187 | |
|
|
4188 | /* end */ |
3691 | { |
4189 | |
3692 | SCHEME_V->code = args; |
4190 | SCHEME_V->code = args; |
3693 | SCHEME_V->args = NIL; |
4191 | SCHEME_V->args = NIL; |
3694 | s_goto (OP_BEGIN); |
4192 | s_goto (OP_BEGIN); |
3695 | } |
|
|
3696 | |
4193 | |
3697 | case OP_LET0REC: /* letrec */ |
4194 | case OP_LET0REC: /* letrec */ |
3698 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
4195 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3699 | SCHEME_V->args = NIL; |
4196 | SCHEME_V->args = NIL; |
3700 | SCHEME_V->value = SCHEME_V->code; |
4197 | SCHEME_V->value = SCHEME_V->code; |
3701 | SCHEME_V->code = car (SCHEME_V->code); |
4198 | SCHEME_V->code = car (SCHEME_V->code); |
3702 | s_goto (OP_LET1REC); |
4199 | s_goto (OP_LET1REC); |
3703 | |
4200 | |
3704 | case OP_LET1REC: /* letrec (calculate parameters) */ |
4201 | /* OP_LET1REC handled by OP_LET1 */ |
3705 | args = cons (SCHEME_V->value, args); |
|
|
3706 | |
|
|
3707 | if (is_pair (SCHEME_V->code)) /* continue */ |
|
|
3708 | { |
|
|
3709 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
|
|
3710 | Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); |
|
|
3711 | |
|
|
3712 | s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code)); |
|
|
3713 | SCHEME_V->code = cadar (SCHEME_V->code); |
|
|
3714 | SCHEME_V->args = NIL; |
|
|
3715 | s_goto (OP_EVAL); |
|
|
3716 | } |
|
|
3717 | else /* end */ |
|
|
3718 | { |
|
|
3719 | args = reverse_in_place (SCHEME_A_ NIL, args); |
|
|
3720 | SCHEME_V->code = car (args); |
|
|
3721 | SCHEME_V->args = cdr (args); |
|
|
3722 | s_goto (OP_LET2REC); |
|
|
3723 | } |
|
|
3724 | |
4202 | |
3725 | case OP_LET2REC: /* letrec */ |
4203 | case OP_LET2REC: /* letrec */ |
3726 | for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y)) |
4204 | for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y)) |
3727 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
4205 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3728 | |
4206 | |
… | |
… | |
3758 | } |
4236 | } |
3759 | else |
4237 | else |
3760 | { |
4238 | { |
3761 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
4239 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3762 | s_return (NIL); |
4240 | s_return (NIL); |
3763 | else |
4241 | |
3764 | { |
|
|
3765 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
4242 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3766 | SCHEME_V->code = caar (SCHEME_V->code); |
4243 | SCHEME_V->code = caar (SCHEME_V->code); |
3767 | s_goto (OP_EVAL); |
4244 | s_goto (OP_EVAL); |
3768 | } |
|
|
3769 | } |
4245 | } |
3770 | |
4246 | |
3771 | case OP_DELAY: /* delay */ |
4247 | case OP_DELAY: /* delay */ |
3772 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
4248 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3773 | set_typeflag (x, T_PROMISE); |
4249 | set_typeflag (x, T_PROMISE); |
… | |
… | |
3784 | case OP_AND1: /* and */ |
4260 | case OP_AND1: /* and */ |
3785 | if (is_false (SCHEME_V->value)) |
4261 | if (is_false (SCHEME_V->value)) |
3786 | s_return (SCHEME_V->value); |
4262 | s_return (SCHEME_V->value); |
3787 | else if (SCHEME_V->code == NIL) |
4263 | else if (SCHEME_V->code == NIL) |
3788 | s_return (SCHEME_V->value); |
4264 | s_return (SCHEME_V->value); |
3789 | else |
4265 | |
3790 | { |
|
|
3791 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
4266 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3792 | SCHEME_V->code = car (SCHEME_V->code); |
4267 | SCHEME_V->code = car (SCHEME_V->code); |
3793 | s_goto (OP_EVAL); |
4268 | s_goto (OP_EVAL); |
3794 | } |
|
|
3795 | |
4269 | |
3796 | case OP_OR0: /* or */ |
4270 | case OP_OR0: /* or */ |
3797 | if (SCHEME_V->code == NIL) |
4271 | if (SCHEME_V->code == NIL) |
3798 | s_return (S_F); |
4272 | s_return (S_F); |
3799 | |
4273 | |
… | |
… | |
3804 | case OP_OR1: /* or */ |
4278 | case OP_OR1: /* or */ |
3805 | if (is_true (SCHEME_V->value)) |
4279 | if (is_true (SCHEME_V->value)) |
3806 | s_return (SCHEME_V->value); |
4280 | s_return (SCHEME_V->value); |
3807 | else if (SCHEME_V->code == NIL) |
4281 | else if (SCHEME_V->code == NIL) |
3808 | s_return (SCHEME_V->value); |
4282 | s_return (SCHEME_V->value); |
3809 | else |
4283 | |
3810 | { |
|
|
3811 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
4284 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3812 | SCHEME_V->code = car (SCHEME_V->code); |
4285 | SCHEME_V->code = car (SCHEME_V->code); |
3813 | s_goto (OP_EVAL); |
4286 | s_goto (OP_EVAL); |
3814 | } |
|
|
3815 | |
4287 | |
3816 | case OP_C0STREAM: /* cons-stream */ |
4288 | case OP_C0STREAM: /* cons-stream */ |
3817 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
4289 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3818 | SCHEME_V->code = car (SCHEME_V->code); |
4290 | SCHEME_V->code = car (SCHEME_V->code); |
3819 | s_goto (OP_EVAL); |
4291 | s_goto (OP_EVAL); |
… | |
… | |
3884 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
4356 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
3885 | SCHEME_V->code = caar (x); |
4357 | SCHEME_V->code = caar (x); |
3886 | s_goto (OP_EVAL); |
4358 | s_goto (OP_EVAL); |
3887 | } |
4359 | } |
3888 | } |
4360 | } |
3889 | else |
4361 | |
3890 | s_return (NIL); |
4362 | s_return (NIL); |
3891 | |
4363 | |
3892 | case OP_CASE2: /* case */ |
4364 | case OP_CASE2: /* case */ |
3893 | if (is_true (SCHEME_V->value)) |
4365 | if (is_true (SCHEME_V->value)) |
3894 | s_goto (OP_BEGIN); |
4366 | s_goto (OP_BEGIN); |
3895 | else |
4367 | |
3896 | s_return (NIL); |
4368 | s_return (NIL); |
3897 | |
4369 | |
3898 | case OP_PAPPLY: /* apply */ |
4370 | case OP_PAPPLY: /* apply */ |
3899 | SCHEME_V->code = car (args); |
4371 | SCHEME_V->code = car (args); |
3900 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
4372 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
3901 | /*SCHEME_V->args = cadr(args); */ |
4373 | /*SCHEME_V->args = cadr(args); */ |
… | |
… | |
3912 | SCHEME_V->code = car (args); |
4384 | SCHEME_V->code = car (args); |
3913 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); |
4385 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); |
3914 | s_goto (OP_APPLY); |
4386 | s_goto (OP_APPLY); |
3915 | } |
4387 | } |
3916 | |
4388 | |
3917 | abort (); |
4389 | if (USE_ERROR_CHECKING) abort (); |
3918 | } |
4390 | } |
3919 | |
4391 | |
3920 | static int |
4392 | /* math, cxr */ |
|
|
4393 | ecb_hot static int |
3921 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4394 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
3922 | { |
4395 | { |
3923 | pointer args = SCHEME_V->args; |
4396 | pointer args = SCHEME_V->args; |
3924 | pointer x = car (args); |
4397 | pointer x = car (args); |
3925 | num v; |
4398 | num v; |
3926 | |
4399 | |
3927 | #if USE_MATH |
|
|
3928 | RVALUE dd; |
|
|
3929 | #endif |
|
|
3930 | |
|
|
3931 | switch (op) |
4400 | switch (op) |
3932 | { |
4401 | { |
3933 | #if USE_MATH |
4402 | #if USE_MATH |
3934 | case OP_INEX2EX: /* inexact->exact */ |
4403 | case OP_INEX2EX: /* inexact->exact */ |
3935 | if (num_is_integer (x)) |
4404 | if (!is_integer (x)) |
3936 | s_return (x); |
4405 | { |
3937 | else if (modf (rvalue_unchecked (x), &dd) == 0) |
4406 | RVALUE r = rvalue_unchecked (x); |
3938 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4407 | |
|
|
4408 | if (r == (RVALUE)(IVALUE)r) |
|
|
4409 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
3939 | else |
4410 | else |
3940 | Error_1 ("inexact->exact: not integral:", x); |
4411 | Error_1 ("inexact->exact: not integral:", x); |
|
|
4412 | } |
3941 | |
4413 | |
|
|
4414 | s_return (x); |
|
|
4415 | |
|
|
4416 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4417 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4418 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4419 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
|
|
4420 | |
|
|
4421 | case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
3942 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4422 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
3943 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
4423 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
|
|
4424 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
3944 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4425 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
3945 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4426 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
3946 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4427 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
3947 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4428 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
3948 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4429 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
3949 | |
4430 | |
3950 | case OP_ATAN: |
4431 | case OP_ATAN: |
|
|
4432 | s_return (mk_real (SCHEME_A_ |
3951 | if (cdr (args) == NIL) |
4433 | cdr (args) == NIL |
3952 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4434 | ? atan (rvalue (x)) |
3953 | else |
4435 | : atan2 (rvalue (x), rvalue (cadr (args))))); |
3954 | { |
|
|
3955 | pointer y = cadr (args); |
|
|
3956 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
|
|
3957 | } |
|
|
3958 | |
|
|
3959 | case OP_SQRT: |
|
|
3960 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
|
|
3961 | |
4436 | |
3962 | case OP_EXPT: |
4437 | case OP_EXPT: |
3963 | { |
4438 | { |
3964 | RVALUE result; |
4439 | RVALUE result; |
3965 | int real_result = 1; |
4440 | int real_result = 1; |
3966 | pointer y = cadr (args); |
4441 | pointer y = cadr (args); |
3967 | |
4442 | |
3968 | if (num_is_integer (x) && num_is_integer (y)) |
4443 | if (is_integer (x) && is_integer (y)) |
3969 | real_result = 0; |
4444 | real_result = 0; |
3970 | |
4445 | |
3971 | /* This 'if' is an R5RS compatibility fix. */ |
4446 | /* This 'if' is an R5RS compatibility fix. */ |
3972 | /* NOTE: Remove this 'if' fix for R6RS. */ |
4447 | /* NOTE: Remove this 'if' fix for R6RS. */ |
3973 | if (rvalue (x) == 0 && rvalue (y) < 0) |
4448 | if (rvalue (x) == 0 && rvalue (y) < 0) |
… | |
… | |
3979 | /* If the test fails, result is too big for integer. */ |
4454 | /* If the test fails, result is too big for integer. */ |
3980 | if (!real_result) |
4455 | if (!real_result) |
3981 | { |
4456 | { |
3982 | long result_as_long = result; |
4457 | long result_as_long = result; |
3983 | |
4458 | |
3984 | if (result != (RVALUE) result_as_long) |
4459 | if (result != result_as_long) |
3985 | real_result = 1; |
4460 | real_result = 1; |
3986 | } |
4461 | } |
3987 | |
4462 | |
3988 | if (real_result) |
4463 | if (real_result) |
3989 | s_return (mk_real (SCHEME_A_ result)); |
4464 | s_return (mk_real (SCHEME_A_ result)); |
3990 | else |
4465 | else |
3991 | s_return (mk_integer (SCHEME_A_ result)); |
4466 | s_return (mk_integer (SCHEME_A_ result)); |
3992 | } |
4467 | } |
3993 | |
|
|
3994 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
3995 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
3996 | |
|
|
3997 | case OP_TRUNCATE: |
|
|
3998 | { |
|
|
3999 | RVALUE rvalue_of_x; |
|
|
4000 | |
|
|
4001 | rvalue_of_x = rvalue (x); |
|
|
4002 | |
|
|
4003 | if (rvalue_of_x > 0) |
|
|
4004 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
|
|
4005 | else |
|
|
4006 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
|
|
4007 | } |
|
|
4008 | |
|
|
4009 | case OP_ROUND: |
|
|
4010 | if (num_is_integer (x)) |
|
|
4011 | s_return (x); |
|
|
4012 | |
|
|
4013 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
|
|
4014 | #endif |
4468 | #endif |
4015 | |
4469 | |
4016 | case OP_ADD: /* + */ |
4470 | case OP_ADD: /* + */ |
4017 | v = num_zero; |
4471 | v = num_zero; |
4018 | |
4472 | |
4019 | for (x = args; x != NIL; x = cdr (x)) |
4473 | for (x = args; x != NIL; x = cdr (x)) |
4020 | v = num_op ('+', v, nvalue (car (x))); |
4474 | v = num_op (NUM_ADD, v, nvalue (car (x))); |
4021 | |
4475 | |
4022 | s_return (mk_number (SCHEME_A_ v)); |
4476 | s_return (mk_number (SCHEME_A_ v)); |
4023 | |
4477 | |
4024 | case OP_MUL: /* * */ |
4478 | case OP_MUL: /* * */ |
4025 | v = num_one; |
4479 | v = num_one; |
4026 | |
4480 | |
4027 | for (x = args; x != NIL; x = cdr (x)) |
4481 | for (x = args; x != NIL; x = cdr (x)) |
4028 | v = num_op ('+', v, nvalue (car (x))); |
4482 | v = num_op (NUM_MUL, v, nvalue (car (x))); |
4029 | |
4483 | |
4030 | s_return (mk_number (SCHEME_A_ v)); |
4484 | s_return (mk_number (SCHEME_A_ v)); |
4031 | |
4485 | |
4032 | case OP_SUB: /* - */ |
4486 | case OP_SUB: /* - */ |
4033 | if (cdr (args) == NIL) |
4487 | if (cdr (args) == NIL) |
… | |
… | |
4040 | x = cdr (args); |
4494 | x = cdr (args); |
4041 | v = nvalue (car (args)); |
4495 | v = nvalue (car (args)); |
4042 | } |
4496 | } |
4043 | |
4497 | |
4044 | for (; x != NIL; x = cdr (x)) |
4498 | for (; x != NIL; x = cdr (x)) |
4045 | v = num_op ('+', v, nvalue (car (x))); |
4499 | v = num_op (NUM_SUB, v, nvalue (car (x))); |
4046 | |
4500 | |
4047 | s_return (mk_number (SCHEME_A_ v)); |
4501 | s_return (mk_number (SCHEME_A_ v)); |
4048 | |
4502 | |
4049 | case OP_DIV: /* / */ |
4503 | case OP_DIV: /* / */ |
4050 | if (cdr (args) == NIL) |
4504 | if (cdr (args) == NIL) |
… | |
… | |
4057 | x = cdr (args); |
4511 | x = cdr (args); |
4058 | v = nvalue (car (args)); |
4512 | v = nvalue (car (args)); |
4059 | } |
4513 | } |
4060 | |
4514 | |
4061 | for (; x != NIL; x = cdr (x)) |
4515 | for (; x != NIL; x = cdr (x)) |
4062 | { |
|
|
4063 | if (!is_zero_rvalue (rvalue (car (x)))) |
4516 | if (!is_zero_rvalue (rvalue (car (x)))) |
4064 | v = num_div (v, nvalue (car (x))); |
4517 | v = num_div (v, nvalue (car (x))); |
4065 | else |
4518 | else |
4066 | Error_0 ("/: division by zero"); |
4519 | Error_0 ("/: division by zero"); |
4067 | } |
|
|
4068 | |
4520 | |
4069 | s_return (mk_number (SCHEME_A_ v)); |
4521 | s_return (mk_number (SCHEME_A_ v)); |
4070 | |
4522 | |
4071 | case OP_INTDIV: /* quotient */ |
4523 | case OP_INTDIV: /* quotient */ |
4072 | if (cdr (args) == NIL) |
4524 | if (cdr (args) == NIL) |
… | |
… | |
4081 | } |
4533 | } |
4082 | |
4534 | |
4083 | for (; x != NIL; x = cdr (x)) |
4535 | for (; x != NIL; x = cdr (x)) |
4084 | { |
4536 | { |
4085 | if (ivalue (car (x)) != 0) |
4537 | if (ivalue (car (x)) != 0) |
4086 | v = num_op ('/', v, nvalue (car (x))); |
4538 | v = num_op (NUM_INTDIV, v, nvalue (car (x))); |
4087 | else |
4539 | else |
4088 | Error_0 ("quotient: division by zero"); |
4540 | Error_0 ("quotient: division by zero"); |
4089 | } |
4541 | } |
4090 | |
4542 | |
4091 | s_return (mk_number (SCHEME_A_ v)); |
4543 | s_return (mk_number (SCHEME_A_ v)); |
… | |
… | |
4108 | else |
4560 | else |
4109 | Error_0 ("modulo: division by zero"); |
4561 | Error_0 ("modulo: division by zero"); |
4110 | |
4562 | |
4111 | s_return (mk_number (SCHEME_A_ v)); |
4563 | s_return (mk_number (SCHEME_A_ v)); |
4112 | |
4564 | |
4113 | case OP_CAR: /* car */ |
4565 | /* the compiler will optimize this mess... */ |
4114 | s_return (caar (args)); |
4566 | case OP_CAR: op_car: s_return (car (x)); |
4115 | |
4567 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4116 | case OP_CDR: /* cdr */ |
4568 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4117 | s_return (cdar (args)); |
4569 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4570 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4571 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4572 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4573 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4574 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4575 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4576 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4577 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4578 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4579 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4580 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4581 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4582 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4583 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4584 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4585 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4586 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4587 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4588 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4589 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4590 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4591 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4592 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4593 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4594 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4595 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4118 | |
4596 | |
4119 | case OP_CONS: /* cons */ |
4597 | case OP_CONS: /* cons */ |
4120 | set_cdr (args, cadr (args)); |
4598 | set_cdr (args, cadr (args)); |
4121 | s_return (args); |
4599 | s_return (args); |
4122 | |
4600 | |
… | |
… | |
4137 | } |
4615 | } |
4138 | else |
4616 | else |
4139 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4617 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4140 | |
4618 | |
4141 | case OP_CHAR2INT: /* char->integer */ |
4619 | case OP_CHAR2INT: /* char->integer */ |
4142 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4620 | s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x))); |
4143 | |
4621 | |
4144 | case OP_INT2CHAR: /* integer->char */ |
4622 | case OP_INT2CHAR: /* integer->char */ |
4145 | s_return (mk_character (SCHEME_A_ ivalue (x))); |
4623 | s_return (mk_character (SCHEME_A_ ivalue_unchecked (x))); |
4146 | |
4624 | |
4147 | case OP_CHARUPCASE: |
4625 | case OP_CHARUPCASE: |
4148 | { |
4626 | { |
4149 | unsigned char c = ivalue (x); |
4627 | unsigned char c = ivalue_unchecked (x); |
4150 | c = toupper (c); |
4628 | c = toupper (c); |
4151 | s_return (mk_character (SCHEME_A_ c)); |
4629 | s_return (mk_character (SCHEME_A_ c)); |
4152 | } |
4630 | } |
4153 | |
4631 | |
4154 | case OP_CHARDNCASE: |
4632 | case OP_CHARDNCASE: |
4155 | { |
4633 | { |
4156 | unsigned char c = ivalue (x); |
4634 | unsigned char c = ivalue_unchecked (x); |
4157 | c = tolower (c); |
4635 | c = tolower (c); |
4158 | s_return (mk_character (SCHEME_A_ c)); |
4636 | s_return (mk_character (SCHEME_A_ c)); |
4159 | } |
4637 | } |
4160 | |
4638 | |
4161 | case OP_STR2SYM: /* string->symbol */ |
4639 | case OP_STR2SYM: /* string->symbol */ |
… | |
… | |
4238 | Error_1 ("atom->string: not an atom:", x); |
4716 | Error_1 ("atom->string: not an atom:", x); |
4239 | } |
4717 | } |
4240 | |
4718 | |
4241 | case OP_MKSTRING: /* make-string */ |
4719 | case OP_MKSTRING: /* make-string */ |
4242 | { |
4720 | { |
4243 | int fill = ' '; |
4721 | int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' '; |
4244 | int len; |
|
|
4245 | |
|
|
4246 | len = ivalue (x); |
4722 | int len = ivalue_unchecked (x); |
4247 | |
|
|
4248 | if (cdr (args) != NIL) |
|
|
4249 | fill = charvalue (cadr (args)); |
|
|
4250 | |
4723 | |
4251 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4724 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4252 | } |
4725 | } |
4253 | |
4726 | |
4254 | case OP_STRLEN: /* string-length */ |
4727 | case OP_STRLEN: /* string-length */ |
4255 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4728 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4256 | |
4729 | |
4257 | case OP_STRREF: /* string-ref */ |
4730 | case OP_STRREF: /* string-ref */ |
4258 | { |
4731 | { |
4259 | char *str; |
|
|
4260 | int index; |
|
|
4261 | |
|
|
4262 | str = strvalue (x); |
4732 | char *str = strvalue (x); |
4263 | |
|
|
4264 | index = ivalue (cadr (args)); |
4733 | int index = ivalue_unchecked (cadr (args)); |
4265 | |
4734 | |
4266 | if (index >= strlength (x)) |
4735 | if (index >= strlength (x)) |
4267 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4736 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4268 | |
4737 | |
4269 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4738 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4270 | } |
4739 | } |
4271 | |
4740 | |
4272 | case OP_STRSET: /* string-set! */ |
4741 | case OP_STRSET: /* string-set! */ |
4273 | { |
4742 | { |
4274 | char *str; |
4743 | char *str = strvalue (x); |
4275 | int index; |
4744 | int index = ivalue_unchecked (cadr (args)); |
4276 | int c; |
4745 | int c; |
4277 | |
4746 | |
4278 | if (is_immutable (x)) |
4747 | if (is_immutable (x)) |
4279 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4748 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4280 | |
|
|
4281 | str = strvalue (x); |
|
|
4282 | |
|
|
4283 | index = ivalue (cadr (args)); |
|
|
4284 | |
4749 | |
4285 | if (index >= strlength (x)) |
4750 | if (index >= strlength (x)) |
4286 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4751 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4287 | |
4752 | |
4288 | c = charvalue (caddr (args)); |
4753 | c = charvalue (caddr (args)); |
… | |
… | |
4309 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4774 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4310 | |
4775 | |
4311 | s_return (newstr); |
4776 | s_return (newstr); |
4312 | } |
4777 | } |
4313 | |
4778 | |
4314 | case OP_SUBSTR: /* substring */ |
4779 | case OP_STRING_COPY: /* substring/string-copy */ |
4315 | { |
4780 | { |
4316 | char *str; |
4781 | char *str = strvalue (x); |
4317 | int index0; |
4782 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4318 | int index1; |
4783 | int index1; |
4319 | int len; |
4784 | int len; |
4320 | |
4785 | |
4321 | str = strvalue (x); |
|
|
4322 | |
|
|
4323 | index0 = ivalue (cadr (args)); |
|
|
4324 | |
|
|
4325 | if (index0 > strlength (x)) |
4786 | if (index0 > strlength (x)) |
4326 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4787 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4327 | |
4788 | |
4328 | if (cddr (args) != NIL) |
4789 | if (cddr (args) != NIL) |
4329 | { |
4790 | { |
4330 | index1 = ivalue (caddr (args)); |
4791 | index1 = ivalue_unchecked (caddr (args)); |
4331 | |
4792 | |
4332 | if (index1 > strlength (x) || index1 < index0) |
4793 | if (index1 > strlength (x) || index1 < index0) |
4333 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4794 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4334 | } |
4795 | } |
4335 | else |
4796 | else |
4336 | index1 = strlength (x); |
4797 | index1 = strlength (x); |
4337 | |
4798 | |
4338 | len = index1 - index0; |
4799 | len = index1 - index0; |
4339 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4800 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4340 | memcpy (strvalue (x), str + index0, len); |
|
|
4341 | strvalue (x)[len] = 0; |
|
|
4342 | |
4801 | |
4343 | s_return (x); |
4802 | s_return (x); |
4344 | } |
4803 | } |
4345 | |
4804 | |
4346 | case OP_VECTOR: /* vector */ |
4805 | case OP_VECTOR: /* vector */ |
… | |
… | |
4358 | if (SCHEME_V->no_memory) |
4817 | if (SCHEME_V->no_memory) |
4359 | s_return (S_SINK); |
4818 | s_return (S_SINK); |
4360 | #endif |
4819 | #endif |
4361 | |
4820 | |
4362 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4821 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4363 | set_vector_elem (vec, i, car (x)); |
4822 | vector_set (vec, i, car (x)); |
4364 | |
4823 | |
4365 | s_return (vec); |
4824 | s_return (vec); |
4366 | } |
4825 | } |
4367 | |
4826 | |
4368 | case OP_MKVECTOR: /* make-vector */ |
4827 | case OP_MKVECTOR: /* make-vector */ |
4369 | { |
4828 | { |
4370 | pointer fill = NIL; |
4829 | pointer fill = NIL; |
4371 | int len; |
|
|
4372 | pointer vec; |
4830 | pointer vec; |
4373 | |
|
|
4374 | len = ivalue (x); |
4831 | int len = ivalue_unchecked (x); |
4375 | |
4832 | |
4376 | if (cdr (args) != NIL) |
4833 | if (cdr (args) != NIL) |
4377 | fill = cadr (args); |
4834 | fill = cadr (args); |
4378 | |
4835 | |
4379 | vec = mk_vector (SCHEME_A_ len); |
4836 | vec = mk_vector (SCHEME_A_ len); |
… | |
… | |
4382 | if (SCHEME_V->no_memory) |
4839 | if (SCHEME_V->no_memory) |
4383 | s_return (S_SINK); |
4840 | s_return (S_SINK); |
4384 | #endif |
4841 | #endif |
4385 | |
4842 | |
4386 | if (fill != NIL) |
4843 | if (fill != NIL) |
4387 | fill_vector (vec, fill); |
4844 | fill_vector (vec, 0, fill); |
4388 | |
4845 | |
4389 | s_return (vec); |
4846 | s_return (vec); |
4390 | } |
4847 | } |
4391 | |
4848 | |
4392 | case OP_VECLEN: /* vector-length */ |
4849 | case OP_VECLEN: /* vector-length */ |
4393 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4850 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4394 | |
4851 | |
|
|
4852 | case OP_VECRESIZE: |
|
|
4853 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4854 | s_return (x); |
|
|
4855 | |
4395 | case OP_VECREF: /* vector-ref */ |
4856 | case OP_VECREF: /* vector-ref */ |
4396 | { |
4857 | { |
4397 | int index; |
|
|
4398 | |
|
|
4399 | index = ivalue (cadr (args)); |
4858 | int index = ivalue_unchecked (cadr (args)); |
4400 | |
4859 | |
4401 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4860 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4402 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4861 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4403 | |
4862 | |
4404 | s_return (vector_elem (x, index)); |
4863 | s_return (vector_get (x, index)); |
4405 | } |
4864 | } |
4406 | |
4865 | |
4407 | case OP_VECSET: /* vector-set! */ |
4866 | case OP_VECSET: /* vector-set! */ |
4408 | { |
4867 | { |
4409 | int index; |
4868 | int index = ivalue_unchecked (cadr (args)); |
4410 | |
4869 | |
4411 | if (is_immutable (x)) |
4870 | if (is_immutable (x)) |
4412 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4871 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4413 | |
4872 | |
4414 | index = ivalue (cadr (args)); |
|
|
4415 | |
|
|
4416 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4873 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4417 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4874 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4418 | |
4875 | |
4419 | set_vector_elem (x, index, caddr (args)); |
4876 | vector_set (x, index, caddr (args)); |
4420 | s_return (x); |
4877 | s_return (x); |
4421 | } |
4878 | } |
4422 | } |
4879 | } |
4423 | |
4880 | |
4424 | abort (); |
4881 | if (USE_ERROR_CHECKING) abort (); |
4425 | } |
4882 | } |
4426 | |
4883 | |
4427 | INTERFACE int |
4884 | /* relational ops */ |
4428 | is_list (SCHEME_P_ pointer a) |
4885 | ecb_hot static int |
4429 | { |
|
|
4430 | return list_length (SCHEME_A_ a) >= 0; |
|
|
4431 | } |
|
|
4432 | |
|
|
4433 | /* Result is: |
|
|
4434 | proper list: length |
|
|
4435 | circular list: -1 |
|
|
4436 | not even a pair: -2 |
|
|
4437 | dotted list: -2 minus length before dot |
|
|
4438 | */ |
|
|
4439 | INTERFACE int |
|
|
4440 | list_length (SCHEME_P_ pointer a) |
|
|
4441 | { |
|
|
4442 | int i = 0; |
|
|
4443 | pointer slow, fast; |
|
|
4444 | |
|
|
4445 | slow = fast = a; |
|
|
4446 | |
|
|
4447 | while (1) |
|
|
4448 | { |
|
|
4449 | if (fast == NIL) |
|
|
4450 | return i; |
|
|
4451 | |
|
|
4452 | if (!is_pair (fast)) |
|
|
4453 | return -2 - i; |
|
|
4454 | |
|
|
4455 | fast = cdr (fast); |
|
|
4456 | ++i; |
|
|
4457 | |
|
|
4458 | if (fast == NIL) |
|
|
4459 | return i; |
|
|
4460 | |
|
|
4461 | if (!is_pair (fast)) |
|
|
4462 | return -2 - i; |
|
|
4463 | |
|
|
4464 | ++i; |
|
|
4465 | fast = cdr (fast); |
|
|
4466 | |
|
|
4467 | /* Safe because we would have already returned if `fast' |
|
|
4468 | encountered a non-pair. */ |
|
|
4469 | slow = cdr (slow); |
|
|
4470 | |
|
|
4471 | if (fast == slow) |
|
|
4472 | { |
|
|
4473 | /* the fast pointer has looped back around and caught up |
|
|
4474 | with the slow pointer, hence the structure is circular, |
|
|
4475 | not of finite length, and therefore not a list */ |
|
|
4476 | return -1; |
|
|
4477 | } |
|
|
4478 | } |
|
|
4479 | } |
|
|
4480 | |
|
|
4481 | static int |
|
|
4482 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4886 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4483 | { |
4887 | { |
4484 | pointer x = SCHEME_V->args; |
4888 | pointer x = SCHEME_V->args; |
4485 | |
4889 | |
4486 | for (;;) |
4890 | for (;;) |
… | |
… | |
4507 | } |
4911 | } |
4508 | |
4912 | |
4509 | s_return (S_T); |
4913 | s_return (S_T); |
4510 | } |
4914 | } |
4511 | |
4915 | |
4512 | static int |
4916 | /* predicates */ |
|
|
4917 | ecb_hot static int |
4513 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4918 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4514 | { |
4919 | { |
4515 | pointer args = SCHEME_V->args; |
4920 | pointer args = SCHEME_V->args; |
4516 | pointer a = car (args); |
4921 | pointer a = car (args); |
4517 | pointer d = cdr (args); |
4922 | pointer d = cdr (args); |
4518 | int r; |
4923 | int r; |
4519 | |
4924 | |
4520 | switch (op) |
4925 | switch (op) |
4521 | { |
4926 | { |
4522 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4927 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4523 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
4928 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break; |
4524 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4929 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4525 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4930 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4526 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
4931 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4932 | case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break; |
4527 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4933 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4528 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4934 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4529 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4935 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4530 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4936 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4531 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4937 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4532 | |
4938 | |
4533 | #if USE_CHAR_CLASSIFIERS |
4939 | #if USE_CHAR_CLASSIFIERS |
4534 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; |
4940 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4535 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; |
4941 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4536 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; |
4942 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4537 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; |
4943 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break; |
4538 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; |
4944 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break; |
4539 | #endif |
4945 | #endif |
4540 | |
4946 | |
4541 | #if USE_PORTS |
4947 | #if USE_PORTS |
4542 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4948 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4543 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
4949 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
… | |
… | |
4563 | } |
4969 | } |
4564 | |
4970 | |
4565 | s_retbool (r); |
4971 | s_retbool (r); |
4566 | } |
4972 | } |
4567 | |
4973 | |
4568 | static int |
4974 | /* promises, list ops, ports */ |
|
|
4975 | ecb_hot static int |
4569 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4976 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4570 | { |
4977 | { |
4571 | pointer args = SCHEME_V->args; |
4978 | pointer args = SCHEME_V->args; |
4572 | pointer a = car (args); |
4979 | pointer a = car (args); |
4573 | pointer x, y; |
4980 | pointer x, y; |
… | |
… | |
4586 | } |
4993 | } |
4587 | else |
4994 | else |
4588 | s_return (SCHEME_V->code); |
4995 | s_return (SCHEME_V->code); |
4589 | |
4996 | |
4590 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4997 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4591 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4998 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4592 | s_return (SCHEME_V->value); |
4999 | s_return (SCHEME_V->value); |
4593 | |
5000 | |
4594 | #if USE_PORTS |
5001 | #if USE_PORTS |
|
|
5002 | |
|
|
5003 | case OP_EOF_OBJECT: /* eof-object */ |
|
|
5004 | s_return (S_EOF); |
4595 | |
5005 | |
4596 | case OP_WRITE: /* write */ |
5006 | case OP_WRITE: /* write */ |
4597 | case OP_DISPLAY: /* display */ |
5007 | case OP_DISPLAY: /* display */ |
4598 | case OP_WRITE_CHAR: /* write-char */ |
5008 | case OP_WRITE_CHAR: /* write-char */ |
4599 | if (is_pair (cdr (SCHEME_V->args))) |
5009 | if (is_pair (cdr (SCHEME_V->args))) |
… | |
… | |
4613 | else |
5023 | else |
4614 | SCHEME_V->print_flag = 0; |
5024 | SCHEME_V->print_flag = 0; |
4615 | |
5025 | |
4616 | s_goto (OP_P0LIST); |
5026 | s_goto (OP_P0LIST); |
4617 | |
5027 | |
|
|
5028 | //TODO: move to scheme |
4618 | case OP_NEWLINE: /* newline */ |
5029 | case OP_NEWLINE: /* newline */ |
4619 | if (is_pair (args)) |
5030 | if (is_pair (args)) |
4620 | { |
5031 | { |
4621 | if (a != SCHEME_V->outport) |
5032 | if (a != SCHEME_V->outport) |
4622 | { |
5033 | { |
… | |
… | |
4624 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
5035 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4625 | SCHEME_V->outport = a; |
5036 | SCHEME_V->outport = a; |
4626 | } |
5037 | } |
4627 | } |
5038 | } |
4628 | |
5039 | |
4629 | putstr (SCHEME_A_ "\n"); |
5040 | putcharacter (SCHEME_A_ '\n'); |
4630 | s_return (S_T); |
5041 | s_return (S_T); |
4631 | #endif |
5042 | #endif |
4632 | |
5043 | |
4633 | case OP_ERR0: /* error */ |
5044 | case OP_ERR0: /* error */ |
4634 | SCHEME_V->retcode = -1; |
5045 | SCHEME_V->retcode = -1; |
… | |
… | |
4643 | putstr (SCHEME_A_ strvalue (car (args))); |
5054 | putstr (SCHEME_A_ strvalue (car (args))); |
4644 | SCHEME_V->args = cdr (args); |
5055 | SCHEME_V->args = cdr (args); |
4645 | s_goto (OP_ERR1); |
5056 | s_goto (OP_ERR1); |
4646 | |
5057 | |
4647 | case OP_ERR1: /* error */ |
5058 | case OP_ERR1: /* error */ |
4648 | putstr (SCHEME_A_ " "); |
5059 | putcharacter (SCHEME_A_ ' '); |
4649 | |
5060 | |
4650 | if (args != NIL) |
5061 | if (args != NIL) |
4651 | { |
5062 | { |
4652 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
5063 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4653 | SCHEME_V->args = a; |
5064 | SCHEME_V->args = a; |
4654 | SCHEME_V->print_flag = 1; |
5065 | SCHEME_V->print_flag = 1; |
4655 | s_goto (OP_P0LIST); |
5066 | s_goto (OP_P0LIST); |
4656 | } |
5067 | } |
4657 | else |
5068 | else |
4658 | { |
5069 | { |
4659 | putstr (SCHEME_A_ "\n"); |
5070 | putcharacter (SCHEME_A_ '\n'); |
4660 | |
5071 | |
4661 | if (SCHEME_V->interactive_repl) |
5072 | if (SCHEME_V->interactive_repl) |
4662 | s_goto (OP_T0LVL); |
5073 | s_goto (OP_T0LVL); |
4663 | else |
5074 | else |
4664 | return -1; |
5075 | return -1; |
… | |
… | |
4741 | SCHEME_V->gc_verbose = (a != S_F); |
5152 | SCHEME_V->gc_verbose = (a != S_F); |
4742 | s_retbool (was); |
5153 | s_retbool (was); |
4743 | } |
5154 | } |
4744 | |
5155 | |
4745 | case OP_NEWSEGMENT: /* new-segment */ |
5156 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
5157 | #if 0 |
4746 | if (!is_pair (args) || !is_number (a)) |
5158 | if (!is_pair (args) || !is_number (a)) |
4747 | Error_0 ("new-segment: argument must be a number"); |
5159 | Error_0 ("new-segment: argument must be a number"); |
4748 | |
5160 | #endif |
4749 | alloc_cellseg (SCHEME_A_ (int)ivalue (a)); |
5161 | s_retbool (alloc_cellseg (SCHEME_A)); |
4750 | |
|
|
4751 | s_return (S_T); |
|
|
4752 | |
5162 | |
4753 | case OP_OBLIST: /* oblist */ |
5163 | case OP_OBLIST: /* oblist */ |
4754 | s_return (oblist_all_symbols (SCHEME_A)); |
5164 | s_return (oblist_all_symbols (SCHEME_A)); |
4755 | |
5165 | |
4756 | #if USE_PORTS |
5166 | #if USE_PORTS |
… | |
… | |
4783 | break; |
5193 | break; |
4784 | } |
5194 | } |
4785 | |
5195 | |
4786 | p = port_from_filename (SCHEME_A_ strvalue (a), prop); |
5196 | p = port_from_filename (SCHEME_A_ strvalue (a), prop); |
4787 | |
5197 | |
4788 | if (p == NIL) |
5198 | s_return (p == NIL ? S_F : p); |
4789 | s_return (S_F); |
|
|
4790 | |
|
|
4791 | s_return (p); |
|
|
4792 | } |
5199 | } |
4793 | |
5200 | |
4794 | # if USE_STRING_PORTS |
5201 | # if USE_STRING_PORTS |
4795 | |
5202 | |
4796 | case OP_OPEN_INSTRING: /* open-input-string */ |
5203 | case OP_OPEN_INSTRING: /* open-input-string */ |
… | |
… | |
4811 | } |
5218 | } |
4812 | |
5219 | |
4813 | p = port_from_string (SCHEME_A_ strvalue (a), |
5220 | p = port_from_string (SCHEME_A_ strvalue (a), |
4814 | strvalue (a) + strlength (a), prop); |
5221 | strvalue (a) + strlength (a), prop); |
4815 | |
5222 | |
4816 | if (p == NIL) |
5223 | s_return (p == NIL ? S_F : p); |
4817 | s_return (S_F); |
|
|
4818 | |
|
|
4819 | s_return (p); |
|
|
4820 | } |
5224 | } |
4821 | |
5225 | |
4822 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
5226 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
4823 | { |
5227 | { |
4824 | pointer p; |
5228 | pointer p; |
4825 | |
5229 | |
4826 | if (a == NIL) |
5230 | if (a == NIL) |
4827 | { |
|
|
4828 | p = port_from_scratch (SCHEME_A); |
5231 | p = port_from_scratch (SCHEME_A); |
4829 | |
|
|
4830 | if (p == NIL) |
|
|
4831 | s_return (S_F); |
|
|
4832 | } |
|
|
4833 | else |
5232 | else |
4834 | { |
|
|
4835 | p = port_from_string (SCHEME_A_ strvalue (a), |
5233 | p = port_from_string (SCHEME_A_ strvalue (a), |
4836 | strvalue (a) + strlength (a), port_output); |
5234 | strvalue (a) + strlength (a), port_output); |
4837 | |
5235 | |
4838 | if (p == NIL) |
5236 | s_return (p == NIL ? S_F : p); |
4839 | s_return (S_F); |
|
|
4840 | } |
|
|
4841 | |
|
|
4842 | s_return (p); |
|
|
4843 | } |
5237 | } |
4844 | |
5238 | |
4845 | case OP_GET_OUTSTRING: /* get-output-string */ |
5239 | case OP_GET_OUTSTRING: /* get-output-string */ |
4846 | { |
5240 | { |
4847 | port *p; |
5241 | port *p = port (a); |
4848 | |
5242 | |
4849 | if ((p = a->object.port)->kind & port_string) |
5243 | if (p->kind & port_string) |
4850 | { |
5244 | { |
4851 | off_t size; |
5245 | off_t size; |
4852 | char *str; |
5246 | char *str; |
4853 | |
5247 | |
4854 | size = p->rep.string.curr - p->rep.string.start + 1; |
5248 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4886 | case OP_CURR_ENV: /* current-environment */ |
5280 | case OP_CURR_ENV: /* current-environment */ |
4887 | s_return (SCHEME_V->envir); |
5281 | s_return (SCHEME_V->envir); |
4888 | |
5282 | |
4889 | } |
5283 | } |
4890 | |
5284 | |
4891 | abort (); |
5285 | if (USE_ERROR_CHECKING) abort (); |
4892 | } |
5286 | } |
4893 | |
5287 | |
4894 | static int |
5288 | /* reading */ |
|
|
5289 | ecb_cold static int |
4895 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
5290 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4896 | { |
5291 | { |
4897 | pointer args = SCHEME_V->args; |
5292 | pointer args = SCHEME_V->args; |
4898 | pointer x; |
5293 | pointer x; |
4899 | |
5294 | |
… | |
… | |
4959 | int res; |
5354 | int res; |
4960 | |
5355 | |
4961 | if (is_pair (args)) |
5356 | if (is_pair (args)) |
4962 | p = car (args); |
5357 | p = car (args); |
4963 | |
5358 | |
4964 | res = p->object.port->kind & port_string; |
5359 | res = port (p)->kind & port_string; |
4965 | |
5360 | |
4966 | s_retbool (res); |
5361 | s_retbool (res); |
4967 | } |
5362 | } |
4968 | |
5363 | |
4969 | case OP_SET_INPORT: /* set-input-port */ |
5364 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
4978 | case OP_RDSEXPR: |
5373 | case OP_RDSEXPR: |
4979 | switch (SCHEME_V->tok) |
5374 | switch (SCHEME_V->tok) |
4980 | { |
5375 | { |
4981 | case TOK_EOF: |
5376 | case TOK_EOF: |
4982 | s_return (S_EOF); |
5377 | s_return (S_EOF); |
4983 | /* NOTREACHED */ |
|
|
4984 | |
5378 | |
4985 | case TOK_VEC: |
5379 | case TOK_VEC: |
4986 | s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); |
5380 | s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); |
4987 | /* fall through */ |
5381 | /* fall through */ |
4988 | |
5382 | |
… | |
… | |
4991 | |
5385 | |
4992 | if (SCHEME_V->tok == TOK_RPAREN) |
5386 | if (SCHEME_V->tok == TOK_RPAREN) |
4993 | s_return (NIL); |
5387 | s_return (NIL); |
4994 | else if (SCHEME_V->tok == TOK_DOT) |
5388 | else if (SCHEME_V->tok == TOK_DOT) |
4995 | Error_0 ("syntax error: illegal dot expression"); |
5389 | Error_0 ("syntax error: illegal dot expression"); |
4996 | else |
5390 | |
4997 | { |
|
|
4998 | SCHEME_V->nesting_stack[SCHEME_V->file_i]++; |
5391 | SCHEME_V->nesting_stack[SCHEME_V->file_i]++; |
4999 | s_save (SCHEME_A_ OP_RDLIST, NIL, NIL); |
5392 | s_save (SCHEME_A_ OP_RDLIST, NIL, NIL); |
5000 | s_goto (OP_RDSEXPR); |
5393 | s_goto (OP_RDSEXPR); |
5001 | } |
|
|
5002 | |
5394 | |
5003 | case TOK_QUOTE: |
5395 | case TOK_QUOTE: |
5004 | s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL); |
5396 | s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL); |
5005 | SCHEME_V->tok = token (SCHEME_A); |
5397 | SCHEME_V->tok = token (SCHEME_A); |
5006 | s_goto (OP_RDSEXPR); |
5398 | s_goto (OP_RDSEXPR); |
… | |
… | |
5012 | { |
5404 | { |
5013 | s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); |
5405 | s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); |
5014 | SCHEME_V->tok = TOK_LPAREN; |
5406 | SCHEME_V->tok = TOK_LPAREN; |
5015 | s_goto (OP_RDSEXPR); |
5407 | s_goto (OP_RDSEXPR); |
5016 | } |
5408 | } |
5017 | else |
5409 | |
5018 | s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); |
5410 | s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); |
5019 | |
|
|
5020 | s_goto (OP_RDSEXPR); |
5411 | s_goto (OP_RDSEXPR); |
5021 | |
5412 | |
5022 | case TOK_COMMA: |
5413 | case TOK_COMMA: |
5023 | s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); |
5414 | s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); |
5024 | SCHEME_V->tok = token (SCHEME_A); |
5415 | SCHEME_V->tok = token (SCHEME_A); |
… | |
… | |
5028 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5419 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5029 | SCHEME_V->tok = token (SCHEME_A); |
5420 | SCHEME_V->tok = token (SCHEME_A); |
5030 | s_goto (OP_RDSEXPR); |
5421 | s_goto (OP_RDSEXPR); |
5031 | |
5422 | |
5032 | case TOK_ATOM: |
5423 | case TOK_ATOM: |
5033 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); |
5424 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
|
|
5425 | |
|
|
5426 | case TOK_DOTATOM: |
|
|
5427 | SCHEME_V->strbuff[0] = '.'; |
|
|
5428 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
|
|
5429 | |
|
|
5430 | case TOK_STRATOM: |
|
|
5431 | //TODO: haven't checked whether the garbage collector could interfere and free x |
|
|
5432 | gc (SCHEME_A_ NIL, NIL); //TODO: superheavyhanded |
|
|
5433 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5434 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
5034 | |
5435 | |
5035 | case TOK_DQUOTE: |
5436 | case TOK_DQUOTE: |
5036 | x = readstrexp (SCHEME_A); |
5437 | x = readstrexp (SCHEME_A_ '"'); |
5037 | |
5438 | |
5038 | if (x == S_F) |
5439 | if (x == S_F) |
5039 | Error_0 ("Error reading string"); |
5440 | Error_0 ("Error reading string"); |
5040 | |
5441 | |
5041 | setimmutable (x); |
5442 | setimmutable (x); |
… | |
… | |
5045 | { |
5446 | { |
5046 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1); |
5447 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1); |
5047 | |
5448 | |
5048 | if (f == NIL) |
5449 | if (f == NIL) |
5049 | Error_0 ("undefined sharp expression"); |
5450 | Error_0 ("undefined sharp expression"); |
5050 | else |
5451 | |
5051 | { |
|
|
5052 | SCHEME_V->code = cons (slot_value_in_env (f), NIL); |
5452 | SCHEME_V->code = cons (slot_value_in_env (f), NIL); |
5053 | s_goto (OP_EVAL); |
5453 | s_goto (OP_EVAL); |
5054 | } |
|
|
5055 | } |
5454 | } |
5056 | |
5455 | |
5057 | case TOK_SHARP_CONST: |
5456 | case TOK_SHARP_CONST: |
5058 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) |
5457 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
5059 | Error_0 ("undefined sharp expression"); |
5458 | Error_0 ("undefined sharp expression"); |
5060 | else |
5459 | |
5061 | s_return (x); |
5460 | s_return (x); |
5062 | |
5461 | |
5063 | default: |
5462 | default: |
5064 | Error_0 ("syntax error: illegal token"); |
5463 | Error_0 ("syntax error: illegal token"); |
5065 | } |
5464 | } |
5066 | |
5465 | |
… | |
… | |
5159 | pointer b = cdr (args); |
5558 | pointer b = cdr (args); |
5160 | int ok_abbr = ok_abbrev (b); |
5559 | int ok_abbr = ok_abbrev (b); |
5161 | SCHEME_V->args = car (b); |
5560 | SCHEME_V->args = car (b); |
5162 | |
5561 | |
5163 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5562 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5164 | putstr (SCHEME_A_ "'"); |
5563 | putcharacter (SCHEME_A_ '\''); |
5165 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5564 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5166 | putstr (SCHEME_A_ "`"); |
5565 | putcharacter (SCHEME_A_ '`'); |
5167 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5566 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5168 | putstr (SCHEME_A_ ","); |
5567 | putcharacter (SCHEME_A_ ','); |
5169 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5568 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5170 | putstr (SCHEME_A_ ",@"); |
5569 | putstr (SCHEME_A_ ",@"); |
5171 | else |
5570 | else |
5172 | { |
5571 | { |
5173 | putstr (SCHEME_A_ "("); |
5572 | putcharacter (SCHEME_A_ '('); |
5174 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5573 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5175 | SCHEME_V->args = a; |
5574 | SCHEME_V->args = a; |
5176 | } |
5575 | } |
5177 | |
5576 | |
5178 | s_goto (OP_P0LIST); |
5577 | s_goto (OP_P0LIST); |
… | |
… | |
5180 | |
5579 | |
5181 | case OP_P1LIST: |
5580 | case OP_P1LIST: |
5182 | if (is_pair (args)) |
5581 | if (is_pair (args)) |
5183 | { |
5582 | { |
5184 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5583 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5185 | putstr (SCHEME_A_ " "); |
5584 | putcharacter (SCHEME_A_ ' '); |
5186 | SCHEME_V->args = car (args); |
5585 | SCHEME_V->args = car (args); |
5187 | s_goto (OP_P0LIST); |
5586 | s_goto (OP_P0LIST); |
5188 | } |
5587 | } |
5189 | else if (is_vector (args)) |
5588 | else if (is_vector (args)) |
5190 | { |
5589 | { |
… | |
… | |
5198 | { |
5597 | { |
5199 | putstr (SCHEME_A_ " . "); |
5598 | putstr (SCHEME_A_ " . "); |
5200 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5599 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5201 | } |
5600 | } |
5202 | |
5601 | |
5203 | putstr (SCHEME_A_ ")"); |
5602 | putcharacter (SCHEME_A_ ')'); |
5204 | s_return (S_T); |
5603 | s_return (S_T); |
5205 | } |
5604 | } |
5206 | |
5605 | |
5207 | case OP_PVECFROM: |
5606 | case OP_PVECFROM: |
5208 | { |
5607 | { |
5209 | int i = ivalue_unchecked (cdr (args)); |
5608 | IVALUE i = ivalue_unchecked (cdr (args)); |
5210 | pointer vec = car (args); |
5609 | pointer vec = car (args); |
5211 | int len = veclength (vec); |
5610 | uint32_t len = veclength (vec); |
5212 | |
5611 | |
5213 | if (i == len) |
5612 | if (i == len) |
5214 | { |
5613 | { |
5215 | putstr (SCHEME_A_ ")"); |
5614 | putcharacter (SCHEME_A_ ')'); |
5216 | s_return (S_T); |
5615 | s_return (S_T); |
5217 | } |
5616 | } |
5218 | else |
5617 | else |
5219 | { |
5618 | { |
5220 | pointer elem = vector_elem (vec, i); |
5619 | pointer elem = vector_get (vec, i); |
5221 | |
5620 | |
5222 | ivalue_unchecked (cdr (args)) = i + 1; |
5621 | set_cdr (args, mk_integer (SCHEME_A_ i + 1)); |
5223 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5622 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5224 | SCHEME_V->args = elem; |
5623 | SCHEME_V->args = elem; |
5225 | |
5624 | |
5226 | if (i > 0) |
5625 | if (i > 0) |
5227 | putstr (SCHEME_A_ " "); |
5626 | putcharacter (SCHEME_A_ ' '); |
5228 | |
5627 | |
5229 | s_goto (OP_P0LIST); |
5628 | s_goto (OP_P0LIST); |
5230 | } |
5629 | } |
5231 | } |
5630 | } |
5232 | } |
5631 | } |
5233 | |
5632 | |
5234 | abort (); |
5633 | if (USE_ERROR_CHECKING) abort (); |
5235 | } |
5634 | } |
5236 | |
5635 | |
5237 | static int |
5636 | /* list ops */ |
|
|
5637 | ecb_hot static int |
5238 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5638 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5239 | { |
5639 | { |
5240 | pointer args = SCHEME_V->args; |
5640 | pointer args = SCHEME_V->args; |
5241 | pointer a = car (args); |
5641 | pointer a = car (args); |
5242 | pointer x, y; |
5642 | pointer x, y; |
… | |
… | |
5265 | break; |
5665 | break; |
5266 | } |
5666 | } |
5267 | |
5667 | |
5268 | if (is_pair (y)) |
5668 | if (is_pair (y)) |
5269 | s_return (car (y)); |
5669 | s_return (car (y)); |
5270 | else |
5670 | |
5271 | s_return (S_F); |
5671 | s_return (S_F); |
5272 | |
|
|
5273 | |
5672 | |
5274 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5673 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5275 | SCHEME_V->args = a; |
5674 | SCHEME_V->args = a; |
5276 | |
5675 | |
5277 | if (SCHEME_V->args == NIL) |
5676 | if (SCHEME_V->args == NIL) |
5278 | s_return (S_F); |
5677 | s_return (S_F); |
5279 | else if (is_closure (SCHEME_V->args)) |
5678 | else if (is_closure (SCHEME_V->args) || is_macro (SCHEME_V->args)) |
5280 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
5679 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
5281 | else if (is_macro (SCHEME_V->args)) |
5680 | |
5282 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
|
|
5283 | else |
|
|
5284 | s_return (S_F); |
5681 | s_return (S_F); |
5285 | |
5682 | |
5286 | case OP_CLOSUREP: /* closure? */ |
5683 | case OP_CLOSUREP: /* closure? */ |
5287 | /* |
5684 | /* |
5288 | * Note, macro object is also a closure. |
5685 | * Note, macro object is also a closure. |
5289 | * Therefore, (closure? <#MACRO>) ==> #t |
5686 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5687 | * (schmorp) well, obviously not, fix? TODO |
5290 | */ |
5688 | */ |
5291 | s_retbool (is_closure (a)); |
5689 | s_retbool (is_closure (a)); |
5292 | |
5690 | |
5293 | case OP_MACROP: /* macro? */ |
5691 | case OP_MACROP: /* macro? */ |
5294 | s_retbool (is_macro (a)); |
5692 | s_retbool (is_macro (a)); |
5295 | } |
5693 | } |
5296 | |
5694 | |
5297 | abort (); |
5695 | if (USE_ERROR_CHECKING) abort (); |
5298 | } |
5696 | } |
5299 | |
5697 | |
5300 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5698 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5301 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5699 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5302 | |
5700 | |
5303 | typedef int (*test_predicate)(pointer); |
5701 | typedef int (*test_predicate)(pointer); |
5304 | static int |
5702 | |
|
|
5703 | ecb_hot static int |
5305 | is_any (pointer p) |
5704 | tst_any (pointer p) |
5306 | { |
5705 | { |
5307 | return 1; |
5706 | return 1; |
5308 | } |
5707 | } |
5309 | |
5708 | |
5310 | static int |
5709 | ecb_hot static int |
5311 | is_nonneg (pointer p) |
5710 | tst_inonneg (pointer p) |
5312 | { |
5711 | { |
5313 | return ivalue (p) >= 0 && is_integer (p); |
5712 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5314 | } |
5713 | } |
5315 | |
5714 | |
5316 | static int |
5715 | ecb_hot static int |
5317 | tst_is_list (pointer p) |
5716 | tst_is_list (SCHEME_P_ pointer p) |
5318 | { |
5717 | { |
5319 | return p == NIL || is_pair (p); |
5718 | return p == NIL || is_pair (p); |
5320 | } |
5719 | } |
5321 | |
5720 | |
5322 | /* Correspond carefully with following defines! */ |
5721 | /* Correspond carefully with following defines! */ |
5323 | static struct |
5722 | static struct |
5324 | { |
5723 | { |
5325 | test_predicate fct; |
5724 | test_predicate fct; |
5326 | const char *kind; |
5725 | const char *kind; |
5327 | } tests[] = |
5726 | } tests[] = { |
5328 | { |
|
|
5329 | { is_any, 0 }, |
5727 | { tst_any , 0 }, |
5330 | { is_string, "string" }, |
5728 | { is_string , "string" }, |
5331 | { is_symbol, "symbol" }, |
5729 | { is_symbol , "symbol" }, |
5332 | { is_port, "port" }, |
5730 | { is_port , "port" }, |
5333 | { is_inport, "input port" }, |
5731 | { is_inport , "input port" }, |
5334 | { is_outport, "output port" }, |
5732 | { is_outport , "output port" }, |
5335 | { is_environment, "environment" }, |
5733 | { is_environment, "environment" }, |
5336 | { is_pair, "pair" }, |
5734 | { is_pair , "pair" }, |
5337 | { tst_is_list, "pair or '()" }, |
5735 | { 0 , "pair or '()" }, |
5338 | { is_character, "character" }, |
5736 | { is_character , "character" }, |
5339 | { is_vector, "vector" }, |
5737 | { is_vector , "vector" }, |
5340 | { is_number, "number" }, |
5738 | { is_number , "number" }, |
5341 | { is_integer, "integer" }, |
5739 | { is_integer , "integer" }, |
5342 | { is_nonneg, "non-negative integer" } |
5740 | { tst_inonneg , "non-negative integer" } |
5343 | }; |
5741 | }; |
5344 | |
5742 | |
5345 | #define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ |
5743 | #define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ |
5346 | #define TST_ANY "\001" |
5744 | #define TST_ANY "\001" |
5347 | #define TST_STRING "\002" |
5745 | #define TST_STRING "\002" |
… | |
… | |
5365 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5763 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5366 | #include "opdefines.h" |
5764 | #include "opdefines.h" |
5367 | #undef OP_DEF |
5765 | #undef OP_DEF |
5368 | ; |
5766 | ; |
5369 | |
5767 | |
5370 | static const char * |
5768 | ecb_cold static const char * |
5371 | opname (int idx) |
5769 | opname (int idx) |
5372 | { |
5770 | { |
5373 | const char *name = opnames; |
5771 | const char *name = opnames; |
5374 | |
5772 | |
5375 | /* should do this at compile time, but would require external program, right? */ |
5773 | /* should do this at compile time, but would require external program, right? */ |
… | |
… | |
5377 | name += strlen (name) + 1; |
5775 | name += strlen (name) + 1; |
5378 | |
5776 | |
5379 | return *name ? name : "ILLEGAL"; |
5777 | return *name ? name : "ILLEGAL"; |
5380 | } |
5778 | } |
5381 | |
5779 | |
5382 | static const char * |
5780 | ecb_cold static const char * |
5383 | procname (pointer x) |
5781 | procname (pointer x) |
5384 | { |
5782 | { |
5385 | return opname (procnum (x)); |
5783 | return opname (procnum (x)); |
5386 | } |
5784 | } |
5387 | |
5785 | |
5388 | typedef struct |
5786 | typedef struct |
5389 | { |
5787 | { |
5390 | uint8_t func; |
5788 | uint8_t func; |
5391 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
5789 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
5392 | uint8_t builtin; |
5790 | uint8_t builtin; |
|
|
5791 | #if USE_ERROR_CHECKING |
5393 | uint8_t min_arity; |
5792 | uint8_t min_arity; |
5394 | uint8_t max_arity; |
5793 | uint8_t max_arity; |
5395 | char arg_tests_encoding[3]; |
5794 | char arg_tests_encoding[3]; |
|
|
5795 | #endif |
5396 | } op_code_info; |
5796 | } op_code_info; |
5397 | |
5797 | |
5398 | static const op_code_info dispatch_table[] = { |
5798 | static const op_code_info dispatch_table[] = { |
|
|
5799 | #if USE_ERROR_CHECKING |
5399 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, |
5800 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, |
|
|
5801 | #else |
|
|
5802 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 }, |
|
|
5803 | #endif |
5400 | #include "opdefines.h" |
5804 | #include "opdefines.h" |
5401 | #undef OP_DEF |
5805 | #undef OP_DEF |
5402 | {0} |
5806 | {0} |
5403 | }; |
5807 | }; |
5404 | |
5808 | |
5405 | /* kernel of this interpreter */ |
5809 | /* kernel of this interpreter */ |
5406 | static void |
5810 | ecb_hot static void |
5407 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5811 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5408 | { |
5812 | { |
5409 | SCHEME_V->op = op; |
5813 | SCHEME_V->op = op; |
5410 | |
5814 | |
5411 | for (;;) |
5815 | for (;;) |
… | |
… | |
5413 | const op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5817 | const op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5414 | |
5818 | |
5415 | #if USE_ERROR_CHECKING |
5819 | #if USE_ERROR_CHECKING |
5416 | if (pcd->builtin) /* if built-in function, check arguments */ |
5820 | if (pcd->builtin) /* if built-in function, check arguments */ |
5417 | { |
5821 | { |
5418 | int ok = 1; |
|
|
5419 | char msg[STRBUFFSIZE]; |
5822 | char msg[STRBUFFSIZE]; |
5420 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5823 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5421 | |
5824 | |
5422 | /* Check number of arguments */ |
5825 | /* Check number of arguments */ |
5423 | if (ecb_expect_false (n < pcd->min_arity)) |
5826 | if (ecb_expect_false (n < pcd->min_arity)) |
5424 | { |
5827 | { |
5425 | ok = 0; |
|
|
5426 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5828 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5427 | opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); |
5829 | opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); |
|
|
5830 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5831 | continue; |
5428 | } |
5832 | } |
5429 | else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG)) |
5833 | else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG)) |
5430 | { |
5834 | { |
5431 | ok = 0; |
|
|
5432 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5835 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5433 | opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
5836 | opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
|
|
5837 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5838 | continue; |
5434 | } |
5839 | } |
5435 | else |
5840 | else |
5436 | { |
5841 | { |
5437 | if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */ |
5842 | if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */ |
5438 | { |
5843 | { |
… | |
… | |
5445 | { |
5850 | { |
5446 | pointer arg = car (arglist); |
5851 | pointer arg = car (arglist); |
5447 | |
5852 | |
5448 | j = t[0]; |
5853 | j = t[0]; |
5449 | |
5854 | |
|
|
5855 | /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */ |
|
|
5856 | if (j == TST_LIST[0]) |
|
|
5857 | { |
|
|
5858 | if (!tst_is_list (SCHEME_A_ arg)) |
|
|
5859 | break; |
|
|
5860 | } |
|
|
5861 | else |
|
|
5862 | { |
5450 | if (!tests[j - 1].fct (arg)) |
5863 | if (!tests[j - 1].fct (arg)) |
5451 | break; |
5864 | break; |
|
|
5865 | } |
5452 | |
5866 | |
5453 | if (t[1]) /* last test is replicated as necessary */ |
5867 | if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */ |
5454 | t++; |
5868 | t++; |
5455 | |
5869 | |
5456 | arglist = cdr (arglist); |
5870 | arglist = cdr (arglist); |
5457 | i++; |
5871 | i++; |
5458 | } |
5872 | } |
5459 | while (i < n); |
5873 | while (i < n); |
5460 | |
5874 | |
5461 | if (i < n) |
5875 | if (i < n) |
5462 | { |
5876 | { |
5463 | ok = 0; |
|
|
5464 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind); |
5877 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind); |
|
|
5878 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5879 | continue; |
5465 | } |
5880 | } |
5466 | } |
5881 | } |
5467 | } |
|
|
5468 | |
|
|
5469 | if (!ok) |
|
|
5470 | { |
|
|
5471 | /* tinyscheme tested for returncode, but Error_1 always diverts? */ |
|
|
5472 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5473 | continue; |
|
|
5474 | } |
5882 | } |
5475 | } |
5883 | } |
5476 | #endif |
5884 | #endif |
5477 | |
5885 | |
5478 | ok_to_freely_gc (SCHEME_A); |
5886 | ok_to_freely_gc (SCHEME_A); |
… | |
… | |
5490 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5898 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5491 | return; |
5899 | return; |
5492 | |
5900 | |
5493 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5901 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5494 | { |
5902 | { |
5495 | xwrstr ("No memory!\n"); |
5903 | putstr (SCHEME_A_ "No memory!\n"); |
5496 | return; |
5904 | return; |
5497 | } |
5905 | } |
5498 | } |
5906 | } |
5499 | } |
5907 | } |
5500 | |
5908 | |
5501 | /* ========== Initialization of internal keywords ========== */ |
5909 | /* ========== Initialization of internal keywords ========== */ |
5502 | |
5910 | |
5503 | static void |
5911 | ecb_cold static void |
5504 | assign_syntax (SCHEME_P_ const char *name) |
5912 | assign_syntax (SCHEME_P_ const char *name) |
5505 | { |
5913 | { |
5506 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5914 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5507 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5915 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5508 | } |
5916 | } |
5509 | |
5917 | |
5510 | static void |
5918 | ecb_cold static void |
5511 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5919 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5512 | { |
5920 | { |
5513 | pointer x = mk_symbol (SCHEME_A_ name); |
5921 | pointer x = mk_symbol (SCHEME_A_ name); |
5514 | pointer y = mk_proc (SCHEME_A_ op); |
5922 | pointer y = mk_proc (SCHEME_A_ op); |
5515 | new_slot_in_env (SCHEME_A_ x, y); |
5923 | new_slot_in_env (SCHEME_A_ x, y); |
… | |
… | |
5518 | static pointer |
5926 | static pointer |
5519 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5927 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5520 | { |
5928 | { |
5521 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5929 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5522 | set_typeflag (y, (T_PROC | T_ATOM)); |
5930 | set_typeflag (y, (T_PROC | T_ATOM)); |
5523 | ivalue_unchecked (y) = op; |
5931 | set_ivalue (y, op); |
5524 | set_num_integer (y); |
|
|
5525 | return y; |
5932 | return y; |
5526 | } |
5933 | } |
5527 | |
5934 | |
5528 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5935 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5529 | static int |
5936 | ecb_hot static int |
5530 | syntaxnum (pointer p) |
5937 | syntaxnum (pointer p) |
5531 | { |
5938 | { |
5532 | const char *s = strvalue (car (p)); |
5939 | const char *s = strvalue (p); |
5533 | |
5940 | |
5534 | switch (strlength (car (p))) |
5941 | switch (strlength (p)) |
5535 | { |
5942 | { |
5536 | case 2: |
5943 | case 2: |
5537 | if (s[0] == 'i') |
5944 | if (s[0] == 'i') |
5538 | return OP_IF0; /* if */ |
5945 | return OP_IF0; /* if */ |
5539 | else |
5946 | else |
… | |
… | |
5594 | return OP_C0STREAM; /* cons-stream */ |
6001 | return OP_C0STREAM; /* cons-stream */ |
5595 | } |
6002 | } |
5596 | } |
6003 | } |
5597 | |
6004 | |
5598 | #if USE_MULTIPLICITY |
6005 | #if USE_MULTIPLICITY |
5599 | scheme * |
6006 | ecb_cold scheme * |
5600 | scheme_init_new () |
6007 | scheme_init_new () |
5601 | { |
6008 | { |
5602 | scheme *sc = malloc (sizeof (scheme)); |
6009 | scheme *sc = malloc (sizeof (scheme)); |
5603 | |
6010 | |
5604 | if (!scheme_init (SCHEME_A)) |
6011 | if (!scheme_init (SCHEME_A)) |
… | |
… | |
5609 | else |
6016 | else |
5610 | return sc; |
6017 | return sc; |
5611 | } |
6018 | } |
5612 | #endif |
6019 | #endif |
5613 | |
6020 | |
5614 | int |
6021 | ecb_cold int |
5615 | scheme_init (SCHEME_P) |
6022 | scheme_init (SCHEME_P) |
5616 | { |
6023 | { |
5617 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
6024 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5618 | pointer x; |
6025 | |
|
|
6026 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
6027 | * that memset 0 will also set pointers to 0, but memset does |
|
|
6028 | * of course not guarantee that. screw such systems. |
|
|
6029 | */ |
|
|
6030 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5619 | |
6031 | |
5620 | num_set_fixnum (num_zero, 1); |
6032 | num_set_fixnum (num_zero, 1); |
5621 | num_set_ivalue (num_zero, 0); |
6033 | num_set_ivalue (num_zero, 0); |
5622 | num_set_fixnum (num_one, 1); |
6034 | num_set_fixnum (num_one, 1); |
5623 | num_set_ivalue (num_one, 1); |
6035 | num_set_ivalue (num_one, 1); |
… | |
… | |
5635 | SCHEME_V->save_inport = NIL; |
6047 | SCHEME_V->save_inport = NIL; |
5636 | SCHEME_V->loadport = NIL; |
6048 | SCHEME_V->loadport = NIL; |
5637 | SCHEME_V->nesting = 0; |
6049 | SCHEME_V->nesting = 0; |
5638 | SCHEME_V->interactive_repl = 0; |
6050 | SCHEME_V->interactive_repl = 0; |
5639 | |
6051 | |
5640 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
6052 | if (!alloc_cellseg (SCHEME_A)) |
5641 | { |
6053 | { |
5642 | #if USE_ERROR_CHECKING |
6054 | #if USE_ERROR_CHECKING |
5643 | SCHEME_V->no_memory = 1; |
6055 | SCHEME_V->no_memory = 1; |
5644 | return 0; |
6056 | return 0; |
5645 | #endif |
6057 | #endif |
5646 | } |
6058 | } |
5647 | |
6059 | |
5648 | SCHEME_V->gc_verbose = 0; |
6060 | SCHEME_V->gc_verbose = 0; |
5649 | dump_stack_initialize (SCHEME_A); |
6061 | dump_stack_initialize (SCHEME_A); |
5650 | SCHEME_V->code = NIL; |
6062 | SCHEME_V->code = NIL; |
5651 | SCHEME_V->args = NIL; |
6063 | SCHEME_V->args = NIL; |
5652 | SCHEME_V->envir = NIL; |
6064 | SCHEME_V->envir = NIL; |
|
|
6065 | SCHEME_V->value = NIL; |
5653 | SCHEME_V->tracing = 0; |
6066 | SCHEME_V->tracing = 0; |
5654 | |
6067 | |
5655 | /* init NIL */ |
6068 | /* init NIL */ |
5656 | set_typeflag (NIL, T_ATOM | T_MARK); |
6069 | set_typeflag (NIL, T_SPECIAL | T_ATOM); |
5657 | set_car (NIL, NIL); |
6070 | set_car (NIL, NIL); |
5658 | set_cdr (NIL, NIL); |
6071 | set_cdr (NIL, NIL); |
5659 | /* init T */ |
6072 | /* init T */ |
5660 | set_typeflag (S_T, T_ATOM | T_MARK); |
6073 | set_typeflag (S_T, T_SPECIAL | T_ATOM); |
5661 | set_car (S_T, S_T); |
6074 | set_car (S_T, S_T); |
5662 | set_cdr (S_T, S_T); |
6075 | set_cdr (S_T, S_T); |
5663 | /* init F */ |
6076 | /* init F */ |
5664 | set_typeflag (S_F, T_ATOM | T_MARK); |
6077 | set_typeflag (S_F, T_SPECIAL | T_ATOM); |
5665 | set_car (S_F, S_F); |
6078 | set_car (S_F, S_F); |
5666 | set_cdr (S_F, S_F); |
6079 | set_cdr (S_F, S_F); |
5667 | /* init EOF_OBJ */ |
6080 | /* init EOF_OBJ */ |
5668 | set_typeflag (S_EOF, T_ATOM | T_MARK); |
6081 | set_typeflag (S_EOF, T_SPECIAL | T_ATOM); |
5669 | set_car (S_EOF, S_EOF); |
6082 | set_car (S_EOF, S_EOF); |
5670 | set_cdr (S_EOF, S_EOF); |
6083 | set_cdr (S_EOF, S_EOF); |
5671 | /* init sink */ |
6084 | /* init sink */ |
5672 | set_typeflag (S_SINK, T_PAIR | T_MARK); |
6085 | set_typeflag (S_SINK, T_PAIR); |
5673 | set_car (S_SINK, NIL); |
6086 | set_car (S_SINK, NIL); |
5674 | |
6087 | |
5675 | /* init c_nest */ |
6088 | /* init c_nest */ |
5676 | SCHEME_V->c_nest = NIL; |
6089 | SCHEME_V->c_nest = NIL; |
5677 | |
6090 | |
5678 | SCHEME_V->oblist = oblist_initial_value (SCHEME_A); |
6091 | SCHEME_V->oblist = oblist_initial_value (SCHEME_A); |
5679 | /* init global_env */ |
6092 | /* init global_env */ |
5680 | new_frame_in_env (SCHEME_A_ NIL); |
6093 | new_frame_in_env (SCHEME_A_ NIL); |
5681 | SCHEME_V->global_env = SCHEME_V->envir; |
6094 | SCHEME_V->global_env = SCHEME_V->envir; |
5682 | /* init else */ |
6095 | /* init else */ |
5683 | x = mk_symbol (SCHEME_A_ "else"); |
6096 | new_slot_in_env (SCHEME_A_ mk_symbol (SCHEME_A_ "else"), S_T); |
5684 | new_slot_in_env (SCHEME_A_ x, S_T); |
|
|
5685 | |
6097 | |
5686 | { |
6098 | { |
5687 | static const char *syntax_names[] = { |
6099 | static const char *syntax_names[] = { |
5688 | "lambda", "quote", "define", "if", "begin", "set!", |
6100 | "lambda", "quote", "define", "if", "begin", "set!", |
5689 | "let", "let*", "letrec", "cond", "delay", "and", |
6101 | "let", "let*", "letrec", "cond", "delay", "and", |
… | |
… | |
5713 | |
6125 | |
5714 | return !SCHEME_V->no_memory; |
6126 | return !SCHEME_V->no_memory; |
5715 | } |
6127 | } |
5716 | |
6128 | |
5717 | #if USE_PORTS |
6129 | #if USE_PORTS |
5718 | void |
6130 | ecb_cold void |
5719 | scheme_set_input_port_file (SCHEME_P_ int fin) |
6131 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5720 | { |
6132 | { |
5721 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
6133 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5722 | } |
6134 | } |
5723 | |
6135 | |
5724 | void |
6136 | ecb_cold void |
5725 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
6137 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5726 | { |
6138 | { |
5727 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
6139 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5728 | } |
6140 | } |
5729 | |
6141 | |
5730 | void |
6142 | ecb_cold void |
5731 | scheme_set_output_port_file (SCHEME_P_ int fout) |
6143 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5732 | { |
6144 | { |
5733 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
6145 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5734 | } |
6146 | } |
5735 | |
6147 | |
5736 | void |
6148 | ecb_cold void |
5737 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
6149 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5738 | { |
6150 | { |
5739 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
6151 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5740 | } |
6152 | } |
5741 | #endif |
6153 | #endif |
5742 | |
6154 | |
5743 | void |
6155 | ecb_cold void |
5744 | scheme_set_external_data (SCHEME_P_ void *p) |
6156 | scheme_set_external_data (SCHEME_P_ void *p) |
5745 | { |
6157 | { |
5746 | SCHEME_V->ext_data = p; |
6158 | SCHEME_V->ext_data = p; |
5747 | } |
6159 | } |
5748 | |
6160 | |
5749 | void |
6161 | ecb_cold void |
5750 | scheme_deinit (SCHEME_P) |
6162 | scheme_deinit (SCHEME_P) |
5751 | { |
6163 | { |
5752 | int i; |
6164 | int i; |
5753 | |
6165 | |
5754 | #if SHOW_ERROR_LINE |
6166 | #if SHOW_ERROR_LINE |
… | |
… | |
5780 | SCHEME_V->loadport = NIL; |
6192 | SCHEME_V->loadport = NIL; |
5781 | SCHEME_V->gc_verbose = 0; |
6193 | SCHEME_V->gc_verbose = 0; |
5782 | gc (SCHEME_A_ NIL, NIL); |
6194 | gc (SCHEME_A_ NIL, NIL); |
5783 | |
6195 | |
5784 | for (i = 0; i <= SCHEME_V->last_cell_seg; i++) |
6196 | for (i = 0; i <= SCHEME_V->last_cell_seg; i++) |
5785 | free (SCHEME_V->alloc_seg[i]); |
6197 | free (SCHEME_V->cell_seg[i]); |
5786 | |
6198 | |
5787 | #if SHOW_ERROR_LINE |
6199 | #if SHOW_ERROR_LINE |
5788 | for (i = 0; i <= SCHEME_V->file_i; i++) |
6200 | for (i = 0; i <= SCHEME_V->file_i; i++) |
5789 | { |
|
|
5790 | if (SCHEME_V->load_stack[i].kind & port_file) |
6201 | if (SCHEME_V->load_stack[i].kind & port_file) |
5791 | { |
6202 | { |
5792 | fname = SCHEME_V->load_stack[i].rep.stdio.filename; |
6203 | fname = SCHEME_V->load_stack[i].rep.stdio.filename; |
5793 | |
6204 | |
5794 | if (fname) |
6205 | if (fname) |
5795 | free (fname); |
6206 | free (fname); |
5796 | } |
6207 | } |
5797 | } |
|
|
5798 | #endif |
6208 | #endif |
5799 | } |
6209 | } |
5800 | |
6210 | |
5801 | void |
6211 | ecb_cold void |
5802 | scheme_load_file (SCHEME_P_ int fin) |
6212 | scheme_load_file (SCHEME_P_ int fin) |
5803 | { |
6213 | { |
5804 | scheme_load_named_file (SCHEME_A_ fin, 0); |
6214 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5805 | } |
6215 | } |
5806 | |
6216 | |
5807 | void |
6217 | ecb_cold void |
5808 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
6218 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5809 | { |
6219 | { |
5810 | dump_stack_reset (SCHEME_A); |
6220 | dump_stack_reset (SCHEME_A); |
5811 | SCHEME_V->envir = SCHEME_V->global_env; |
6221 | SCHEME_V->envir = SCHEME_V->global_env; |
5812 | SCHEME_V->file_i = 0; |
6222 | SCHEME_V->file_i = 0; |
5813 | SCHEME_V->load_stack[0].unget = -1; |
6223 | SCHEME_V->load_stack[0].unget = -1; |
5814 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
6224 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5815 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
6225 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5816 | #if USE_PORTS |
|
|
5817 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
6226 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5818 | #endif |
|
|
5819 | SCHEME_V->retcode = 0; |
6227 | SCHEME_V->retcode = 0; |
5820 | |
6228 | |
5821 | #if USE_PORTS |
|
|
5822 | if (fin == STDIN_FILENO) |
6229 | if (fin == STDIN_FILENO) |
5823 | SCHEME_V->interactive_repl = 1; |
6230 | SCHEME_V->interactive_repl = 1; |
5824 | #endif |
|
|
5825 | |
6231 | |
5826 | #if USE_PORTS |
6232 | #if USE_PORTS |
5827 | #if SHOW_ERROR_LINE |
6233 | #if SHOW_ERROR_LINE |
5828 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
6234 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5829 | |
6235 | |
… | |
… | |
5833 | #endif |
6239 | #endif |
5834 | |
6240 | |
5835 | SCHEME_V->inport = SCHEME_V->loadport; |
6241 | SCHEME_V->inport = SCHEME_V->loadport; |
5836 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
6242 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5837 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
6243 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
|
|
6244 | |
5838 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
6245 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5839 | |
6246 | |
5840 | if (SCHEME_V->retcode == 0) |
6247 | if (SCHEME_V->retcode == 0) |
5841 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
6248 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5842 | } |
6249 | } |
5843 | |
6250 | |
5844 | void |
6251 | ecb_cold void |
5845 | scheme_load_string (SCHEME_P_ const char *cmd) |
6252 | scheme_load_string (SCHEME_P_ const char *cmd) |
5846 | { |
6253 | { |
|
|
6254 | #if USE_PORTs |
5847 | dump_stack_reset (SCHEME_A); |
6255 | dump_stack_reset (SCHEME_A); |
5848 | SCHEME_V->envir = SCHEME_V->global_env; |
6256 | SCHEME_V->envir = SCHEME_V->global_env; |
5849 | SCHEME_V->file_i = 0; |
6257 | SCHEME_V->file_i = 0; |
5850 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
6258 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5851 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
6259 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
5852 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
6260 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
5853 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
6261 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5854 | #if USE_PORTS |
|
|
5855 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
6262 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5856 | #endif |
|
|
5857 | SCHEME_V->retcode = 0; |
6263 | SCHEME_V->retcode = 0; |
5858 | SCHEME_V->interactive_repl = 0; |
6264 | SCHEME_V->interactive_repl = 0; |
5859 | SCHEME_V->inport = SCHEME_V->loadport; |
6265 | SCHEME_V->inport = SCHEME_V->loadport; |
5860 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
6266 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5861 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
6267 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5862 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
6268 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5863 | |
6269 | |
5864 | if (SCHEME_V->retcode == 0) |
6270 | if (SCHEME_V->retcode == 0) |
5865 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
6271 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
|
|
6272 | #else |
|
|
6273 | abort (); |
|
|
6274 | #endif |
5866 | } |
6275 | } |
5867 | |
6276 | |
5868 | void |
6277 | ecb_cold void |
5869 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
6278 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5870 | { |
6279 | { |
5871 | pointer x; |
6280 | pointer x; |
5872 | |
6281 | |
5873 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
6282 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
… | |
… | |
5878 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
6287 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5879 | } |
6288 | } |
5880 | |
6289 | |
5881 | #if !STANDALONE |
6290 | #if !STANDALONE |
5882 | |
6291 | |
5883 | void |
6292 | ecb_cold void |
5884 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
6293 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5885 | { |
6294 | { |
5886 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
6295 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5887 | } |
6296 | } |
5888 | |
6297 | |
5889 | void |
6298 | ecb_cold void |
5890 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
6299 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5891 | { |
6300 | { |
5892 | int i; |
6301 | int i; |
5893 | |
6302 | |
5894 | for (i = 0; i < count; i++) |
6303 | for (i = 0; i < count; i++) |
5895 | scheme_register_foreign_func (SCHEME_A_ list + i); |
6304 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5896 | } |
6305 | } |
5897 | |
6306 | |
5898 | pointer |
6307 | ecb_cold pointer |
5899 | scheme_apply0 (SCHEME_P_ const char *procname) |
6308 | scheme_apply0 (SCHEME_P_ const char *procname) |
5900 | { |
6309 | { |
5901 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
6310 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
5902 | } |
6311 | } |
5903 | |
6312 | |
5904 | void |
6313 | ecb_cold void |
5905 | save_from_C_call (SCHEME_P) |
6314 | save_from_C_call (SCHEME_P) |
5906 | { |
6315 | { |
5907 | pointer saved_data = cons (car (S_SINK), |
6316 | pointer saved_data = cons (car (S_SINK), |
5908 | cons (SCHEME_V->envir, |
6317 | cons (SCHEME_V->envir, |
5909 | SCHEME_V->dump)); |
6318 | SCHEME_V->dump)); |
… | |
… | |
5913 | /* Truncate the dump stack so TS will return here when done, not |
6322 | /* Truncate the dump stack so TS will return here when done, not |
5914 | directly resume pre-C-call operations. */ |
6323 | directly resume pre-C-call operations. */ |
5915 | dump_stack_reset (SCHEME_A); |
6324 | dump_stack_reset (SCHEME_A); |
5916 | } |
6325 | } |
5917 | |
6326 | |
5918 | void |
6327 | ecb_cold void |
5919 | restore_from_C_call (SCHEME_P) |
6328 | restore_from_C_call (SCHEME_P) |
5920 | { |
6329 | { |
5921 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
6330 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5922 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
6331 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5923 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
6332 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5924 | /* Pop */ |
6333 | /* Pop */ |
5925 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
6334 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5926 | } |
6335 | } |
5927 | |
6336 | |
5928 | /* "func" and "args" are assumed to be already eval'ed. */ |
6337 | /* "func" and "args" are assumed to be already eval'ed. */ |
5929 | pointer |
6338 | ecb_cold pointer |
5930 | scheme_call (SCHEME_P_ pointer func, pointer args) |
6339 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5931 | { |
6340 | { |
5932 | int old_repl = SCHEME_V->interactive_repl; |
6341 | int old_repl = SCHEME_V->interactive_repl; |
5933 | |
6342 | |
5934 | SCHEME_V->interactive_repl = 0; |
6343 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5941 | SCHEME_V->interactive_repl = old_repl; |
6350 | SCHEME_V->interactive_repl = old_repl; |
5942 | restore_from_C_call (SCHEME_A); |
6351 | restore_from_C_call (SCHEME_A); |
5943 | return SCHEME_V->value; |
6352 | return SCHEME_V->value; |
5944 | } |
6353 | } |
5945 | |
6354 | |
5946 | pointer |
6355 | ecb_cold pointer |
5947 | scheme_eval (SCHEME_P_ pointer obj) |
6356 | scheme_eval (SCHEME_P_ pointer obj) |
5948 | { |
6357 | { |
5949 | int old_repl = SCHEME_V->interactive_repl; |
6358 | int old_repl = SCHEME_V->interactive_repl; |
5950 | |
6359 | |
5951 | SCHEME_V->interactive_repl = 0; |
6360 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5963 | |
6372 | |
5964 | /* ========== Main ========== */ |
6373 | /* ========== Main ========== */ |
5965 | |
6374 | |
5966 | #if STANDALONE |
6375 | #if STANDALONE |
5967 | |
6376 | |
5968 | int |
6377 | ecb_cold int |
5969 | main (int argc, char **argv) |
6378 | main (int argc, char **argv) |
5970 | { |
6379 | { |
5971 | # if USE_MULTIPLICITY |
6380 | # if USE_MULTIPLICITY |
5972 | scheme ssc; |
6381 | scheme ssc; |
5973 | scheme *const SCHEME_V = &ssc; |
6382 | scheme *const SCHEME_V = &ssc; |
… | |
… | |
5975 | # endif |
6384 | # endif |
5976 | int fin; |
6385 | int fin; |
5977 | char *file_name = InitFile; |
6386 | char *file_name = InitFile; |
5978 | int retcode; |
6387 | int retcode; |
5979 | int isfile = 1; |
6388 | int isfile = 1; |
|
|
6389 | #if EXPERIMENT |
|
|
6390 | system ("ps v $PPID"); |
|
|
6391 | #endif |
5980 | |
6392 | |
5981 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6393 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5982 | { |
6394 | { |
5983 | xwrstr ("Usage: tinyscheme -?\n"); |
6395 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
5984 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6396 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
5985 | xwrstr ("followed by\n"); |
6397 | putstr (SCHEME_A_ "followed by\n"); |
5986 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6398 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
5987 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6399 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
5988 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6400 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
5989 | xwrstr ("Use - as filename for stdin.\n"); |
6401 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
5990 | return 1; |
6402 | return 1; |
5991 | } |
6403 | } |
5992 | |
6404 | |
5993 | if (!scheme_init (SCHEME_A)) |
6405 | if (!scheme_init (SCHEME_A)) |
5994 | { |
6406 | { |
5995 | xwrstr ("Could not initialize!\n"); |
6407 | putstr (SCHEME_A_ "Could not initialize!\n"); |
5996 | return 2; |
6408 | return 2; |
5997 | } |
6409 | } |
5998 | |
6410 | |
5999 | # if USE_PORTS |
6411 | # if USE_PORTS |
6000 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6412 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
6013 | } |
6425 | } |
6014 | #endif |
6426 | #endif |
6015 | |
6427 | |
6016 | do |
6428 | do |
6017 | { |
6429 | { |
6018 | #if USE_PORTS |
|
|
6019 | if (strcmp (file_name, "-") == 0) |
6430 | if (strcmp (file_name, "-") == 0) |
6020 | fin = STDIN_FILENO; |
6431 | fin = STDIN_FILENO; |
6021 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
6432 | else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) |
6022 | { |
6433 | { |
6023 | pointer args = NIL; |
6434 | pointer args = NIL; |
… | |
… | |
6041 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
6452 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); |
6042 | |
6453 | |
6043 | } |
6454 | } |
6044 | else |
6455 | else |
6045 | fin = open (file_name, O_RDONLY); |
6456 | fin = open (file_name, O_RDONLY); |
6046 | #endif |
|
|
6047 | |
6457 | |
6048 | if (isfile && fin < 0) |
6458 | if (isfile && fin < 0) |
6049 | { |
6459 | { |
6050 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6460 | putstr (SCHEME_A_ "Could not open file "); |
|
|
6461 | putstr (SCHEME_A_ file_name); |
|
|
6462 | putcharacter (SCHEME_A_ '\n'); |
6051 | } |
6463 | } |
6052 | else |
6464 | else |
6053 | { |
6465 | { |
6054 | if (isfile) |
6466 | if (isfile) |
6055 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6467 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6056 | else |
6468 | else |
6057 | scheme_load_string (SCHEME_A_ file_name); |
6469 | scheme_load_string (SCHEME_A_ file_name); |
6058 | |
6470 | |
6059 | #if USE_PORTS |
|
|
6060 | if (!isfile || fin != STDIN_FILENO) |
6471 | if (!isfile || fin != STDIN_FILENO) |
6061 | { |
6472 | { |
6062 | if (SCHEME_V->retcode != 0) |
6473 | if (SCHEME_V->retcode != 0) |
6063 | { |
6474 | { |
6064 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6475 | putstr (SCHEME_A_ "Errors encountered reading "); |
|
|
6476 | putstr (SCHEME_A_ file_name); |
|
|
6477 | putcharacter (SCHEME_A_ '\n'); |
6065 | } |
6478 | } |
6066 | |
6479 | |
6067 | if (isfile) |
6480 | if (isfile) |
6068 | close (fin); |
6481 | close (fin); |
6069 | } |
6482 | } |
6070 | #endif |
|
|
6071 | } |
6483 | } |
6072 | |
6484 | |
6073 | file_name = *argv++; |
6485 | file_name = *argv++; |
6074 | } |
6486 | } |
6075 | while (file_name != 0); |
6487 | while (file_name != 0); |