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