… | |
… | |
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 | |
268 | |
224 | static num num_zero; |
269 | static num num_zero; |
225 | static num num_one; |
270 | static num num_one; |
226 | |
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 | |
227 | /* macros for cell operations */ |
276 | /* macros for cell operations */ |
228 | #define typeflag(p) ((p)->flag + 0) |
277 | #define typeflag(p) (CELL(p)->flag + 0) |
229 | #define set_typeflag(p,v) ((p)->flag = (v)) |
278 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
230 | #define type(p) (typeflag (p) & T_MASKTYPE) |
279 | #define type(p) (typeflag (p) & T_MASKTYPE) |
231 | |
280 | |
232 | INTERFACE int |
281 | INTERFACE int |
233 | is_string (pointer p) |
282 | is_string (pointer p) |
234 | { |
283 | { |
235 | return type (p) == T_STRING; |
284 | return type (p) == T_STRING; |
236 | } |
285 | } |
237 | |
286 | |
238 | #define strvalue(p) ((p)->object.string.svalue) |
287 | #define strvalue(p) (CELL(p)->object.string.svalue) |
239 | #define strlength(p) ((p)->object.string.length) |
288 | #define strlength(p) (CELL(p)->object.string.length) |
240 | |
|
|
241 | INTERFACE int is_list (SCHEME_P_ pointer p); |
|
|
242 | |
289 | |
243 | INTERFACE int |
290 | INTERFACE int |
244 | is_vector (pointer p) |
291 | is_vector (pointer p) |
245 | { |
292 | { |
246 | return type (p) == T_VECTOR; |
293 | return type (p) == T_VECTOR; |
247 | } |
294 | } |
248 | |
295 | |
249 | #define vecvalue(p) ((p)->object.vector.vvalue) |
296 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
250 | #define veclength(p) ((p)->object.vector.length) |
297 | #define veclength(p) (CELL(p)->object.vector.length) |
251 | INTERFACE void fill_vector (pointer vec, pointer obj); |
298 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
252 | INTERFACE uint32_t vector_length (pointer vec); |
|
|
253 | INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); |
299 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
254 | INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); |
300 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
255 | |
301 | |
256 | INTERFACE uint32_t |
302 | INTERFACE int |
257 | vector_length (pointer vec) |
303 | is_integer (pointer p) |
258 | { |
304 | { |
259 | 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; |
260 | } |
313 | } |
261 | |
314 | |
262 | INTERFACE int |
315 | INTERFACE int |
263 | is_number (pointer p) |
316 | is_number (pointer p) |
264 | { |
317 | { |
265 | return type (p) == T_NUMBER; |
318 | return is_integer (p) || is_real (p); |
266 | } |
|
|
267 | |
|
|
268 | INTERFACE int |
|
|
269 | is_integer (pointer p) |
|
|
270 | { |
|
|
271 | return is_number (p) && num_is_fixnum (p->object.number); |
|
|
272 | } |
|
|
273 | |
|
|
274 | INTERFACE int |
|
|
275 | is_real (pointer p) |
|
|
276 | { |
|
|
277 | return is_number (p) && !num_is_fixnum (p->object.number); |
|
|
278 | } |
319 | } |
279 | |
320 | |
280 | INTERFACE int |
321 | INTERFACE int |
281 | is_character (pointer p) |
322 | is_character (pointer p) |
282 | { |
323 | { |
… | |
… | |
287 | string_value (pointer p) |
328 | string_value (pointer p) |
288 | { |
329 | { |
289 | return strvalue (p); |
330 | return strvalue (p); |
290 | } |
331 | } |
291 | |
332 | |
292 | ecb_inline num |
|
|
293 | nvalue (pointer p) |
|
|
294 | { |
|
|
295 | return (p)->object.number; |
|
|
296 | } |
|
|
297 | |
|
|
298 | static IVALUE |
|
|
299 | num_get_ivalue (const num n) |
|
|
300 | { |
|
|
301 | return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n); |
|
|
302 | } |
|
|
303 | |
|
|
304 | static RVALUE |
|
|
305 | num_get_rvalue (const num n) |
|
|
306 | { |
|
|
307 | return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n); |
|
|
308 | } |
|
|
309 | |
|
|
310 | INTERFACE IVALUE |
|
|
311 | ivalue (pointer p) |
|
|
312 | { |
|
|
313 | return num_get_ivalue (p->object.number); |
|
|
314 | } |
|
|
315 | |
|
|
316 | INTERFACE RVALUE |
|
|
317 | rvalue (pointer p) |
|
|
318 | { |
|
|
319 | return num_get_rvalue (p->object.number); |
|
|
320 | } |
|
|
321 | |
|
|
322 | #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 | |
323 | #if USE_REAL |
336 | #if USE_REAL |
324 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
337 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
325 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
338 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
326 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
|
|
327 | #else |
339 | #else |
328 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
340 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
329 | # define set_num_integer(p) 0 |
341 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
330 | # define set_num_real(p) 0 |
|
|
331 | #endif |
342 | #endif |
332 | |
343 | |
333 | INTERFACE long |
344 | INTERFACE long |
334 | charvalue (pointer p) |
345 | charvalue (pointer p) |
335 | { |
346 | { |
336 | return ivalue_unchecked (p); |
347 | return ivalue_unchecked (p); |
337 | } |
348 | } |
338 | |
349 | |
|
|
350 | #define port(p) CELL(p)->object.port |
|
|
351 | #define set_port(p,v) port(p) = (v) |
339 | INTERFACE int |
352 | INTERFACE int |
340 | is_port (pointer p) |
353 | is_port (pointer p) |
341 | { |
354 | { |
342 | return type (p) == T_PORT; |
355 | return type (p) == T_PORT; |
343 | } |
356 | } |
344 | |
357 | |
345 | INTERFACE int |
358 | INTERFACE int |
346 | is_inport (pointer p) |
359 | is_inport (pointer p) |
347 | { |
360 | { |
348 | return is_port (p) && p->object.port->kind & port_input; |
361 | return is_port (p) && port (p)->kind & port_input; |
349 | } |
362 | } |
350 | |
363 | |
351 | INTERFACE int |
364 | INTERFACE int |
352 | is_outport (pointer p) |
365 | is_outport (pointer p) |
353 | { |
366 | { |
354 | return is_port (p) && p->object.port->kind & port_output; |
367 | return is_port (p) && port (p)->kind & port_output; |
355 | } |
368 | } |
356 | |
369 | |
357 | INTERFACE int |
370 | INTERFACE int |
358 | is_pair (pointer p) |
371 | is_pair (pointer p) |
359 | { |
372 | { |
360 | return type (p) == T_PAIR; |
373 | return type (p) == T_PAIR; |
361 | } |
374 | } |
362 | |
375 | |
363 | #define car(p) ((p)->object.cons.car + 0) |
376 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
364 | #define cdr(p) ((p)->object.cons.cdr + 0) |
377 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
365 | |
378 | |
366 | static pointer caar (pointer p) { return car (car (p)); } |
379 | static pointer caar (pointer p) { return car (car (p)); } |
367 | static pointer cadr (pointer p) { return car (cdr (p)); } |
380 | static pointer cadr (pointer p) { return car (cdr (p)); } |
368 | static pointer cdar (pointer p) { return cdr (car (p)); } |
381 | static pointer cdar (pointer p) { return cdr (car (p)); } |
369 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
382 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
370 | |
383 | |
371 | static pointer cadar (pointer p) { return car (cdr (car (p))); } |
384 | static pointer cadar (pointer p) { return car (cdr (car (p))); } |
372 | static pointer caddr (pointer p) { return car (cdr (cdr (p))); } |
385 | static pointer caddr (pointer p) { return car (cdr (cdr (p))); } |
373 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
386 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
374 | |
387 | |
|
|
388 | static pointer cadddr (pointer p) { return car (car (car (cdr (p)))); } |
|
|
389 | |
375 | INTERFACE void |
390 | INTERFACE void |
376 | set_car (pointer p, pointer q) |
391 | set_car (pointer p, pointer q) |
377 | { |
392 | { |
378 | p->object.cons.car = q; |
393 | CELL(p)->object.cons.car = CELL (q); |
379 | } |
394 | } |
380 | |
395 | |
381 | INTERFACE void |
396 | INTERFACE void |
382 | set_cdr (pointer p, pointer q) |
397 | set_cdr (pointer p, pointer q) |
383 | { |
398 | { |
384 | p->object.cons.cdr = q; |
399 | CELL(p)->object.cons.cdr = CELL (q); |
385 | } |
400 | } |
386 | |
401 | |
387 | INTERFACE pointer |
402 | INTERFACE pointer |
388 | pair_car (pointer p) |
403 | pair_car (pointer p) |
389 | { |
404 | { |
… | |
… | |
403 | } |
418 | } |
404 | |
419 | |
405 | INTERFACE char * |
420 | INTERFACE char * |
406 | symname (pointer p) |
421 | symname (pointer p) |
407 | { |
422 | { |
408 | return strvalue (car (p)); |
423 | return strvalue (p); |
409 | } |
424 | } |
410 | |
425 | |
411 | #if USE_PLIST |
426 | #if USE_PLIST |
|
|
427 | #error plists are broken because symbols are no longer pairs |
|
|
428 | #define symprop(p) cdr(p) |
412 | SCHEME_EXPORT int |
429 | SCHEME_EXPORT int |
413 | hasprop (pointer p) |
430 | hasprop (pointer p) |
414 | { |
431 | { |
415 | return typeflag (p) & T_SYMBOL; |
432 | return typeflag (p) & T_SYMBOL; |
416 | } |
433 | } |
417 | |
|
|
418 | # define symprop(p) cdr(p) |
|
|
419 | #endif |
434 | #endif |
420 | |
435 | |
421 | INTERFACE int |
436 | INTERFACE int |
422 | is_syntax (pointer p) |
437 | is_syntax (pointer p) |
423 | { |
438 | { |
… | |
… | |
437 | } |
452 | } |
438 | |
453 | |
439 | INTERFACE char * |
454 | INTERFACE char * |
440 | syntaxname (pointer p) |
455 | syntaxname (pointer p) |
441 | { |
456 | { |
442 | return strvalue (car (p)); |
457 | return strvalue (p); |
443 | } |
458 | } |
444 | |
459 | |
445 | #define procnum(p) ivalue (p) |
460 | #define procnum(p) ivalue_unchecked (p) |
446 | static const char *procname (pointer x); |
461 | static const char *procname (pointer x); |
447 | |
462 | |
448 | INTERFACE int |
463 | INTERFACE int |
449 | is_closure (pointer p) |
464 | is_closure (pointer p) |
450 | { |
465 | { |
… | |
… | |
513 | #if USE_ERROR_CHECKING |
528 | #if USE_ERROR_CHECKING |
514 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
529 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
515 | #endif |
530 | #endif |
516 | } |
531 | } |
517 | |
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 | |
518 | #if USE_CHAR_CLASSIFIERS |
587 | #if USE_CHAR_CLASSIFIERS |
|
|
588 | |
519 | ecb_inline int |
589 | ecb_inline int |
520 | Cisalpha (int c) |
590 | Cisalpha (int c) |
521 | { |
591 | { |
522 | return isascii (c) && isalpha (c); |
592 | return isascii (c) && isalpha (c); |
523 | } |
593 | } |
… | |
… | |
581 | "gs", |
651 | "gs", |
582 | "rs", |
652 | "rs", |
583 | "us" |
653 | "us" |
584 | }; |
654 | }; |
585 | |
655 | |
586 | static int |
656 | ecb_cold static int |
587 | is_ascii_name (const char *name, int *pc) |
657 | is_ascii_name (const char *name, int *pc) |
588 | { |
658 | { |
589 | int i; |
659 | int i; |
590 | |
660 | |
591 | for (i = 0; i < 32; i++) |
661 | for (i = 0; i < 32; i++) |
… | |
… | |
609 | #endif |
679 | #endif |
610 | |
680 | |
611 | static int file_push (SCHEME_P_ const char *fname); |
681 | static int file_push (SCHEME_P_ const char *fname); |
612 | static void file_pop (SCHEME_P); |
682 | static void file_pop (SCHEME_P); |
613 | static int file_interactive (SCHEME_P); |
683 | static int file_interactive (SCHEME_P); |
614 | ecb_inline int is_one_of (char *s, int c); |
684 | ecb_inline int is_one_of (const char *s, int c); |
615 | static int alloc_cellseg (SCHEME_P_ int n); |
685 | static int alloc_cellseg (SCHEME_P); |
616 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
686 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
617 | static void finalize_cell (SCHEME_P_ pointer a); |
687 | static void finalize_cell (SCHEME_P_ pointer a); |
618 | static int count_consecutive_cells (pointer x, int needed); |
|
|
619 | 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); |
620 | static pointer mk_number (SCHEME_P_ const num n); |
689 | static pointer mk_number (SCHEME_P_ const num n); |
621 | 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); |
622 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
691 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
623 | static pointer mk_atom (SCHEME_P_ char *q); |
692 | static pointer mk_atom (SCHEME_P_ char *q); |
624 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
693 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
625 | |
694 | |
|
|
695 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
696 | |
626 | #if USE_PORTS |
697 | #if USE_PORTS |
627 | static pointer mk_port (SCHEME_P_ port *p); |
|
|
628 | 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); |
629 | static pointer port_from_file (SCHEME_P_ int, int prop); |
699 | static pointer port_from_file (SCHEME_P_ int, int prop); |
630 | 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); |
631 | 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); |
632 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
702 | static port *port_rep_from_file (SCHEME_P_ int, int prop); |
633 | 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); |
634 | static void port_close (SCHEME_P_ pointer p, int flag); |
704 | static void port_close (SCHEME_P_ pointer p, int flag); |
635 | #endif |
705 | #endif |
|
|
706 | |
636 | static void mark (pointer a); |
707 | static void mark (pointer a); |
637 | static void gc (SCHEME_P_ pointer a, pointer b); |
708 | static void gc (SCHEME_P_ pointer a, pointer b); |
638 | static int basic_inchar (port *pt); |
709 | static int basic_inchar (port *pt); |
639 | static int inchar (SCHEME_P); |
710 | static int inchar (SCHEME_P); |
640 | static void backchar (SCHEME_P_ int c); |
711 | static void backchar (SCHEME_P_ int c); |
641 | static char *readstr_upto (SCHEME_P_ char *delim); |
712 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
642 | static pointer readstrexp (SCHEME_P); |
713 | static pointer readstrexp (SCHEME_P_ char delim); |
643 | ecb_inline int skipspace (SCHEME_P); |
714 | static int skipspace (SCHEME_P); |
644 | static int token (SCHEME_P); |
715 | static int token (SCHEME_P); |
645 | static void printslashstring (SCHEME_P_ char *s, int len); |
716 | static void printslashstring (SCHEME_P_ char *s, int len); |
646 | 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); |
647 | static void printatom (SCHEME_P_ pointer l, int f); |
718 | static void printatom (SCHEME_P_ pointer l, int f); |
648 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
719 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
… | |
… | |
664 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
735 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
665 | static void assign_syntax (SCHEME_P_ const char *name); |
736 | static void assign_syntax (SCHEME_P_ const char *name); |
666 | static int syntaxnum (pointer p); |
737 | static int syntaxnum (pointer p); |
667 | 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); |
668 | |
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 | |
669 | static num |
767 | static num |
670 | num_op (enum num_op op, num a, num b) |
768 | num_op (enum num_op op, num a, num b) |
671 | { |
769 | { |
672 | num ret; |
770 | num ret; |
673 | |
771 | |
674 | 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)); |
675 | |
773 | |
676 | if (num_is_fixnum (ret)) |
774 | if (num_is_fixnum (ret)) |
677 | { |
775 | { |
678 | IVALUE av = num_get_ivalue (a); |
|
|
679 | IVALUE bv = num_get_ivalue (b); |
|
|
680 | |
|
|
681 | switch (op) |
776 | switch (op) |
682 | { |
777 | { |
683 | case NUM_ADD: av += bv; break; |
778 | case NUM_ADD: a.ivalue += b.ivalue; break; |
684 | case NUM_SUB: av -= bv; break; |
779 | case NUM_SUB: a.ivalue -= b.ivalue; break; |
685 | case NUM_MUL: av *= bv; break; |
780 | case NUM_MUL: a.ivalue *= b.ivalue; break; |
686 | case NUM_INTDIV: av /= bv; break; |
781 | case NUM_INTDIV: a.ivalue /= b.ivalue; break; |
687 | } |
782 | } |
688 | |
783 | |
689 | num_set_ivalue (ret, av); |
784 | num_set_ivalue (ret, a.ivalue); |
690 | } |
785 | } |
|
|
786 | #if USE_REAL |
691 | else |
787 | else |
692 | { |
788 | { |
693 | RVALUE av = num_get_rvalue (a); |
|
|
694 | RVALUE bv = num_get_rvalue (b); |
|
|
695 | |
|
|
696 | switch (op) |
789 | switch (op) |
697 | { |
790 | { |
698 | case NUM_ADD: av += bv; break; |
791 | case NUM_ADD: a.rvalue += b.rvalue; break; |
699 | case NUM_SUB: av -= bv; break; |
792 | case NUM_SUB: a.rvalue -= b.rvalue; break; |
700 | case NUM_MUL: av *= bv; break; |
793 | case NUM_MUL: a.rvalue *= b.rvalue; break; |
701 | case NUM_INTDIV: av /= bv; break; |
794 | case NUM_INTDIV: a.rvalue /= b.rvalue; break; |
702 | } |
795 | } |
703 | |
796 | |
704 | num_set_rvalue (ret, av); |
797 | num_set_rvalue (ret, a.rvalue); |
705 | } |
798 | } |
|
|
799 | #endif |
706 | |
800 | |
707 | return ret; |
801 | return ret; |
708 | } |
802 | } |
709 | |
803 | |
710 | static num |
804 | static num |
711 | num_div (num a, num b) |
805 | num_div (num a, num b) |
712 | { |
806 | { |
713 | num ret; |
807 | num ret; |
714 | |
808 | |
715 | 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); |
716 | |
810 | |
717 | if (num_is_fixnum (ret)) |
811 | if (num_is_fixnum (ret)) |
718 | num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); |
812 | num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b)); |
719 | else |
813 | else |
720 | num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); |
814 | num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); |
721 | |
815 | |
722 | return ret; |
816 | return ret; |
723 | } |
817 | } |
724 | |
818 | |
725 | static num |
819 | static num |
… | |
… | |
727 | { |
821 | { |
728 | num ret; |
822 | num ret; |
729 | long e1, e2, res; |
823 | long e1, e2, res; |
730 | |
824 | |
731 | 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)); |
732 | e1 = num_get_ivalue (a); |
826 | e1 = num_ivalue (a); |
733 | e2 = num_get_ivalue (b); |
827 | e2 = num_ivalue (b); |
734 | res = e1 % e2; |
828 | res = e1 % e2; |
735 | |
829 | |
736 | /* remainder should have same sign as second operand */ |
830 | /* remainder should have same sign as second operand */ |
737 | if (res > 0) |
831 | if (res > 0) |
738 | { |
832 | { |
… | |
… | |
754 | { |
848 | { |
755 | num ret; |
849 | num ret; |
756 | long e1, e2, res; |
850 | long e1, e2, res; |
757 | |
851 | |
758 | 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)); |
759 | e1 = num_get_ivalue (a); |
853 | e1 = num_ivalue (a); |
760 | e2 = num_get_ivalue (b); |
854 | e2 = num_ivalue (b); |
761 | res = e1 % e2; |
855 | res = e1 % e2; |
762 | |
856 | |
763 | /* modulo should have same sign as second operand */ |
857 | /* modulo should have same sign as second operand */ |
764 | if (res * e2 < 0) |
858 | if (res * e2 < 0) |
765 | res += e2; |
859 | res += e2; |
… | |
… | |
775 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
869 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
776 | int ret; |
870 | int ret; |
777 | |
871 | |
778 | if (is_fixnum) |
872 | if (is_fixnum) |
779 | { |
873 | { |
780 | IVALUE av = num_get_ivalue (a); |
874 | IVALUE av = num_ivalue (a); |
781 | IVALUE bv = num_get_ivalue (b); |
875 | IVALUE bv = num_ivalue (b); |
782 | |
876 | |
783 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
877 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
784 | } |
878 | } |
785 | else |
879 | else |
786 | { |
880 | { |
787 | RVALUE av = num_get_rvalue (a); |
881 | RVALUE av = num_rvalue (a); |
788 | RVALUE bv = num_get_rvalue (b); |
882 | RVALUE bv = num_rvalue (b); |
789 | |
883 | |
790 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
884 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
791 | } |
885 | } |
792 | |
886 | |
793 | return ret; |
887 | return ret; |
794 | } |
888 | } |
795 | |
|
|
796 | #if USE_MATH |
|
|
797 | |
|
|
798 | /* Round to nearest. Round to even if midway */ |
|
|
799 | static double |
|
|
800 | round_per_R5RS (double x) |
|
|
801 | { |
|
|
802 | double fl = floor (x); |
|
|
803 | double ce = ceil (x); |
|
|
804 | double dfl = x - fl; |
|
|
805 | double dce = ce - x; |
|
|
806 | |
|
|
807 | if (dfl > dce) |
|
|
808 | return ce; |
|
|
809 | else if (dfl < dce) |
|
|
810 | return fl; |
|
|
811 | else |
|
|
812 | { |
|
|
813 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
|
|
814 | return fl; |
|
|
815 | else |
|
|
816 | return ce; |
|
|
817 | } |
|
|
818 | } |
|
|
819 | #endif |
|
|
820 | |
889 | |
821 | static int |
890 | static int |
822 | is_zero_rvalue (RVALUE x) |
891 | is_zero_rvalue (RVALUE x) |
823 | { |
892 | { |
824 | return x == 0; |
893 | return x == 0; |
… | |
… | |
830 | #endif |
899 | #endif |
831 | #endif |
900 | #endif |
832 | } |
901 | } |
833 | |
902 | |
834 | /* allocate new cell segment */ |
903 | /* allocate new cell segment */ |
835 | static int |
904 | ecb_cold static int |
836 | alloc_cellseg (SCHEME_P_ int n) |
905 | alloc_cellseg (SCHEME_P) |
837 | { |
906 | { |
838 | pointer newp; |
907 | struct cell *newp; |
839 | pointer last; |
908 | struct cell *last; |
840 | pointer p; |
909 | struct cell *p; |
841 | char *cp; |
910 | char *cp; |
842 | long i; |
911 | long i; |
843 | int k; |
912 | int k; |
844 | |
913 | |
845 | static int segsize = CELL_SEGSIZE >> 1; |
914 | static int segsize = CELL_SEGSIZE >> 1; |
846 | segsize <<= 1; |
915 | segsize <<= 1; |
847 | |
916 | |
848 | for (k = 0; k < n; k++) |
|
|
849 | { |
|
|
850 | if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1) |
|
|
851 | return k; |
|
|
852 | |
|
|
853 | cp = malloc (segsize * sizeof (struct cell)); |
917 | cp = malloc (segsize * sizeof (struct cell)); |
854 | |
918 | |
855 | if (!cp && USE_ERROR_CHECKING) |
919 | if (!cp && USE_ERROR_CHECKING) |
856 | return k; |
920 | return k; |
857 | |
921 | |
858 | i = ++SCHEME_V->last_cell_seg; |
922 | i = ++SCHEME_V->last_cell_seg; |
859 | SCHEME_V->alloc_seg[i] = cp; |
|
|
860 | |
923 | |
861 | /* insert new segment in address order */ |
924 | newp = (struct cell *)cp; |
862 | newp = (pointer)cp; |
|
|
863 | SCHEME_V->cell_seg[i] = newp; |
925 | SCHEME_V->cell_seg[i] = newp; |
864 | SCHEME_V->cell_segsize[i] = segsize; |
926 | SCHEME_V->cell_segsize[i] = segsize; |
865 | |
|
|
866 | //TODO: insert, not swap |
|
|
867 | while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i]) |
|
|
868 | { |
|
|
869 | p = SCHEME_V->cell_seg[i]; |
|
|
870 | SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1]; |
|
|
871 | SCHEME_V->cell_seg[i - 1] = p; |
|
|
872 | |
|
|
873 | k = SCHEME_V->cell_segsize[i]; |
|
|
874 | SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1]; |
|
|
875 | SCHEME_V->cell_segsize[i - 1] = k; |
|
|
876 | |
|
|
877 | --i; |
|
|
878 | } |
|
|
879 | |
|
|
880 | SCHEME_V->fcells += segsize; |
927 | SCHEME_V->fcells += segsize; |
881 | last = newp + segsize - 1; |
928 | last = newp + segsize - 1; |
882 | |
929 | |
883 | for (p = newp; p <= last; p++) |
930 | for (p = newp; p <= last; p++) |
884 | { |
931 | { |
|
|
932 | pointer cp = POINTER (p); |
885 | set_typeflag (p, T_FREE); |
933 | set_typeflag (cp, T_PAIR); |
886 | set_car (p, NIL); |
934 | set_car (cp, NIL); |
887 | set_cdr (p, p + 1); |
935 | set_cdr (cp, POINTER (p + 1)); |
888 | } |
936 | } |
889 | |
937 | |
890 | /* insert new cells in address order on free list */ |
|
|
891 | if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) |
|
|
892 | { |
|
|
893 | set_cdr (last, SCHEME_V->free_cell); |
938 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
894 | SCHEME_V->free_cell = newp; |
939 | SCHEME_V->free_cell = POINTER (newp); |
895 | } |
|
|
896 | else |
|
|
897 | { |
|
|
898 | p = SCHEME_V->free_cell; |
|
|
899 | |
940 | |
900 | while (cdr (p) != NIL && newp > cdr (p)) |
|
|
901 | p = cdr (p); |
|
|
902 | |
|
|
903 | set_cdr (last, cdr (p)); |
|
|
904 | set_cdr (p, newp); |
|
|
905 | } |
|
|
906 | } |
|
|
907 | |
|
|
908 | return n; |
941 | return 1; |
909 | } |
942 | } |
910 | |
943 | |
911 | /* get new cell. parameter a, b is marked by gc. */ |
944 | /* get new cell. parameter a, b is marked by gc. */ |
912 | ecb_inline pointer |
945 | ecb_inline pointer |
913 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
946 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
… | |
… | |
917 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
950 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
918 | return S_SINK; |
951 | return S_SINK; |
919 | |
952 | |
920 | if (SCHEME_V->free_cell == NIL) |
953 | if (SCHEME_V->free_cell == NIL) |
921 | { |
954 | { |
922 | 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; |
923 | |
956 | |
924 | gc (SCHEME_A_ a, b); |
957 | gc (SCHEME_A_ a, b); |
925 | |
958 | |
926 | 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) |
927 | { |
960 | { |
928 | /* 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 */ |
929 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
962 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
930 | { |
963 | { |
931 | #if USE_ERROR_CHECKING |
964 | #if USE_ERROR_CHECKING |
932 | SCHEME_V->no_memory = 1; |
965 | SCHEME_V->no_memory = 1; |
933 | return S_SINK; |
966 | return S_SINK; |
934 | #endif |
967 | #endif |
… | |
… | |
946 | } |
979 | } |
947 | } |
980 | } |
948 | |
981 | |
949 | /* To retain recent allocs before interpreter knows about them - |
982 | /* To retain recent allocs before interpreter knows about them - |
950 | Tehom */ |
983 | Tehom */ |
951 | |
984 | ecb_hot static void |
952 | static void |
|
|
953 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
985 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
954 | { |
986 | { |
955 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
987 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
956 | |
988 | |
957 | set_typeflag (holder, T_PAIR); |
989 | set_typeflag (holder, T_PAIR); |
… | |
… | |
959 | set_car (holder, recent); |
991 | set_car (holder, recent); |
960 | set_cdr (holder, car (S_SINK)); |
992 | set_cdr (holder, car (S_SINK)); |
961 | set_car (S_SINK, holder); |
993 | set_car (S_SINK, holder); |
962 | } |
994 | } |
963 | |
995 | |
964 | static pointer |
996 | ecb_hot static pointer |
965 | get_cell (SCHEME_P_ pointer a, pointer b) |
997 | get_cell (SCHEME_P_ pointer a, pointer b) |
966 | { |
998 | { |
967 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
999 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
968 | |
1000 | |
969 | /* 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 |
… | |
… | |
978 | } |
1010 | } |
979 | |
1011 | |
980 | static pointer |
1012 | static pointer |
981 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1013 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
982 | { |
1014 | { |
983 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1015 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
984 | pointer *e = malloc (len * sizeof (pointer)); |
1016 | pointer *e = malloc (len * sizeof (pointer)); |
985 | |
1017 | |
986 | if (!e && USE_ERROR_CHECKING) |
1018 | if (!e && USE_ERROR_CHECKING) |
987 | return S_SINK; |
1019 | return S_SINK; |
988 | |
1020 | |
989 | /* Record it as a vector so that gc understands it. */ |
1021 | /* Record it as a vector so that gc understands it. */ |
990 | set_typeflag (v, T_VECTOR | T_ATOM); |
1022 | set_typeflag (v, T_VECTOR | T_ATOM); |
991 | |
1023 | |
992 | v->object.vector.vvalue = e; |
1024 | CELL(v)->object.vector.vvalue = e; |
993 | v->object.vector.length = len; |
1025 | CELL(v)->object.vector.length = len; |
994 | fill_vector (v, init); |
1026 | fill_vector (v, 0, init); |
995 | push_recent_alloc (SCHEME_A_ v, NIL); |
1027 | push_recent_alloc (SCHEME_A_ v, NIL); |
996 | |
1028 | |
997 | return v; |
1029 | return v; |
998 | } |
1030 | } |
999 | |
1031 | |
… | |
… | |
1007 | static void |
1039 | static void |
1008 | check_cell_alloced (pointer p, int expect_alloced) |
1040 | check_cell_alloced (pointer p, int expect_alloced) |
1009 | { |
1041 | { |
1010 | /* 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. */ |
1011 | if (typeflag (p) & !expect_alloced) |
1043 | if (typeflag (p) & !expect_alloced) |
1012 | xwrstr ("Cell is already allocated!\n"); |
1044 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1013 | |
1045 | |
1014 | if (!(typeflag (p)) & expect_alloced) |
1046 | if (!(typeflag (p)) & expect_alloced) |
1015 | xwrstr ("Cell is not allocated!\n"); |
1047 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1016 | } |
1048 | } |
1017 | |
1049 | |
1018 | static void |
1050 | static void |
1019 | check_range_alloced (pointer p, int n, int expect_alloced) |
1051 | check_range_alloced (pointer p, int n, int expect_alloced) |
1020 | { |
1052 | { |
… | |
… | |
1026 | #endif |
1058 | #endif |
1027 | |
1059 | |
1028 | /* Medium level cell allocation */ |
1060 | /* Medium level cell allocation */ |
1029 | |
1061 | |
1030 | /* get new cons cell */ |
1062 | /* get new cons cell */ |
1031 | pointer |
1063 | ecb_hot static pointer |
1032 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1064 | xcons (SCHEME_P_ pointer a, pointer b) |
1033 | { |
1065 | { |
1034 | pointer x = get_cell (SCHEME_A_ a, b); |
1066 | pointer x = get_cell (SCHEME_A_ a, b); |
1035 | |
1067 | |
1036 | set_typeflag (x, T_PAIR); |
1068 | set_typeflag (x, T_PAIR); |
1037 | |
|
|
1038 | if (immutable) |
|
|
1039 | setimmutable (x); |
|
|
1040 | |
1069 | |
1041 | set_car (x, a); |
1070 | set_car (x, a); |
1042 | set_cdr (x, b); |
1071 | set_cdr (x, b); |
1043 | |
1072 | |
1044 | return x; |
1073 | return x; |
1045 | } |
1074 | } |
1046 | |
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 | |
1047 | /* ========== oblist implementation ========== */ |
1096 | /* ========== oblist implementation ========== */ |
1048 | |
1097 | |
1049 | #ifndef USE_OBJECT_LIST |
1098 | #ifndef USE_OBJECT_LIST |
1050 | |
1099 | |
|
|
1100 | static int |
1051 | 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; |
1052 | |
1105 | |
|
|
1106 | while (*p) |
|
|
1107 | hash = (hash ^ *p++) * 16777619; |
|
|
1108 | |
|
|
1109 | return hash % table_size; |
|
|
1110 | } |
|
|
1111 | |
1053 | static pointer |
1112 | ecb_cold static pointer |
1054 | oblist_initial_value (SCHEME_P) |
1113 | oblist_initial_value (SCHEME_P) |
1055 | { |
1114 | { |
1056 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1115 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1057 | } |
1116 | } |
1058 | |
1117 | |
1059 | /* returns the new symbol */ |
1118 | /* returns the new symbol */ |
1060 | static pointer |
1119 | ecb_cold static pointer |
1061 | oblist_add_by_name (SCHEME_P_ const char *name) |
1120 | oblist_add_by_name (SCHEME_P_ const char *name) |
1062 | { |
1121 | { |
1063 | int location; |
1122 | pointer x = generate_symbol (SCHEME_A_ name); |
1064 | |
|
|
1065 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1066 | set_typeflag (x, T_SYMBOL); |
|
|
1067 | setimmutable (car (x)); |
|
|
1068 | |
|
|
1069 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1123 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1070 | 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))); |
1071 | return x; |
1125 | return x; |
1072 | } |
1126 | } |
1073 | |
1127 | |
1074 | ecb_inline pointer |
1128 | ecb_cold static pointer |
1075 | oblist_find_by_name (SCHEME_P_ const char *name) |
1129 | oblist_find_by_name (SCHEME_P_ const char *name) |
1076 | { |
1130 | { |
1077 | int location; |
1131 | int location; |
1078 | pointer x; |
1132 | pointer x; |
1079 | char *s; |
1133 | char *s; |
1080 | |
1134 | |
1081 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1135 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1082 | |
1136 | |
1083 | 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)) |
1084 | { |
1138 | { |
1085 | s = symname (car (x)); |
1139 | s = symname (car (x)); |
1086 | |
1140 | |
1087 | /* case-insensitive, per R5RS section 2 */ |
1141 | /* case-insensitive, per R5RS section 2 */ |
1088 | if (stricmp (name, s) == 0) |
1142 | if (stricmp (name, s) == 0) |
… | |
… | |
1090 | } |
1144 | } |
1091 | |
1145 | |
1092 | return NIL; |
1146 | return NIL; |
1093 | } |
1147 | } |
1094 | |
1148 | |
1095 | static pointer |
1149 | ecb_cold static pointer |
1096 | oblist_all_symbols (SCHEME_P) |
1150 | oblist_all_symbols (SCHEME_P) |
1097 | { |
1151 | { |
1098 | int i; |
1152 | int i; |
1099 | pointer x; |
1153 | pointer x; |
1100 | pointer ob_list = NIL; |
1154 | pointer ob_list = NIL; |
1101 | |
1155 | |
1102 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1156 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1103 | 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)) |
1104 | ob_list = cons (x, ob_list); |
1158 | ob_list = cons (x, ob_list); |
1105 | |
1159 | |
1106 | return ob_list; |
1160 | return ob_list; |
1107 | } |
1161 | } |
1108 | |
1162 | |
1109 | #else |
1163 | #else |
1110 | |
1164 | |
1111 | static pointer |
1165 | ecb_cold static pointer |
1112 | oblist_initial_value (SCHEME_P) |
1166 | oblist_initial_value (SCHEME_P) |
1113 | { |
1167 | { |
1114 | return NIL; |
1168 | return NIL; |
1115 | } |
1169 | } |
1116 | |
1170 | |
1117 | ecb_inline pointer |
1171 | ecb_cold static pointer |
1118 | oblist_find_by_name (SCHEME_P_ const char *name) |
1172 | oblist_find_by_name (SCHEME_P_ const char *name) |
1119 | { |
1173 | { |
1120 | pointer x; |
1174 | pointer x; |
1121 | char *s; |
1175 | char *s; |
1122 | |
1176 | |
… | |
… | |
1131 | |
1185 | |
1132 | return NIL; |
1186 | return NIL; |
1133 | } |
1187 | } |
1134 | |
1188 | |
1135 | /* returns the new symbol */ |
1189 | /* returns the new symbol */ |
1136 | static pointer |
1190 | ecb_cold static pointer |
1137 | oblist_add_by_name (SCHEME_P_ const char *name) |
1191 | oblist_add_by_name (SCHEME_P_ const char *name) |
1138 | { |
1192 | { |
1139 | pointer x; |
1193 | pointer x = generate_symbol (SCHEME_A_ name); |
1140 | |
|
|
1141 | x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1142 | set_typeflag (x, T_SYMBOL); |
|
|
1143 | setimmutable (car (x)); |
|
|
1144 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1194 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1145 | return x; |
1195 | return x; |
1146 | } |
1196 | } |
1147 | |
1197 | |
1148 | static pointer |
1198 | ecb_cold static pointer |
1149 | oblist_all_symbols (SCHEME_P) |
1199 | oblist_all_symbols (SCHEME_P) |
1150 | { |
1200 | { |
1151 | return SCHEME_V->oblist; |
1201 | return SCHEME_V->oblist; |
1152 | } |
1202 | } |
1153 | |
1203 | |
1154 | #endif |
1204 | #endif |
1155 | |
1205 | |
1156 | #if USE_PORTS |
|
|
1157 | static pointer |
1206 | ecb_cold static pointer |
1158 | mk_port (SCHEME_P_ port *p) |
1207 | mk_port (SCHEME_P_ port *p) |
1159 | { |
1208 | { |
1160 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1209 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1161 | |
1210 | |
1162 | set_typeflag (x, T_PORT | T_ATOM); |
1211 | set_typeflag (x, T_PORT | T_ATOM); |
1163 | x->object.port = p; |
1212 | set_port (x, p); |
1164 | |
1213 | |
1165 | return x; |
1214 | return x; |
1166 | } |
1215 | } |
1167 | #endif |
|
|
1168 | |
1216 | |
1169 | pointer |
1217 | ecb_cold pointer |
1170 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1218 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1171 | { |
1219 | { |
1172 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1220 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1173 | |
1221 | |
1174 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1222 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1175 | x->object.ff = f; |
1223 | CELL(x)->object.ff = f; |
1176 | |
1224 | |
1177 | return x; |
1225 | return x; |
1178 | } |
1226 | } |
1179 | |
1227 | |
1180 | INTERFACE pointer |
1228 | INTERFACE pointer |
1181 | mk_character (SCHEME_P_ int c) |
1229 | mk_character (SCHEME_P_ int c) |
1182 | { |
1230 | { |
1183 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1231 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1184 | |
1232 | |
1185 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1233 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1186 | ivalue_unchecked (x) = c & 0xff; |
1234 | set_ivalue (x, c & 0xff); |
1187 | set_num_integer (x); |
1235 | |
1188 | return x; |
1236 | return x; |
1189 | } |
1237 | } |
1190 | |
1238 | |
1191 | /* get number atom (integer) */ |
1239 | /* get number atom (integer) */ |
1192 | INTERFACE pointer |
1240 | INTERFACE pointer |
1193 | mk_integer (SCHEME_P_ long num) |
1241 | mk_integer (SCHEME_P_ long n) |
1194 | { |
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 | { |
1195 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1253 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1196 | |
1254 | |
1197 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1255 | set_typeflag (x, T_INTEGER | T_ATOM); |
1198 | ivalue_unchecked (x) = num; |
1256 | setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */ |
1199 | set_num_integer (x); |
1257 | set_ivalue (x, n); |
|
|
1258 | |
|
|
1259 | *pp = x; |
|
|
1260 | } |
|
|
1261 | |
1200 | return x; |
1262 | return *pp; |
1201 | } |
1263 | } |
1202 | |
1264 | |
1203 | INTERFACE pointer |
1265 | INTERFACE pointer |
1204 | mk_real (SCHEME_P_ RVALUE n) |
1266 | mk_real (SCHEME_P_ RVALUE n) |
1205 | { |
1267 | { |
|
|
1268 | #if USE_REAL |
1206 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1269 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1207 | |
1270 | |
1208 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1271 | set_typeflag (x, T_REAL | T_ATOM); |
1209 | rvalue_unchecked (x) = n; |
1272 | set_rvalue (x, n); |
1210 | set_num_real (x); |
1273 | |
1211 | return x; |
1274 | return x; |
|
|
1275 | #else |
|
|
1276 | return mk_integer (SCHEME_A_ n); |
|
|
1277 | #endif |
1212 | } |
1278 | } |
1213 | |
1279 | |
1214 | static pointer |
1280 | static pointer |
1215 | mk_number (SCHEME_P_ const num n) |
1281 | mk_number (SCHEME_P_ const num n) |
1216 | { |
1282 | { |
|
|
1283 | #if USE_REAL |
1217 | 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 |
1218 | return mk_integer (SCHEME_A_ num_get_ivalue (n)); |
1288 | return mk_integer (SCHEME_A_ num_ivalue (n)); |
1219 | else |
1289 | #endif |
1220 | return mk_real (SCHEME_A_ num_get_rvalue (n)); |
|
|
1221 | } |
1290 | } |
1222 | |
1291 | |
1223 | /* allocate name to string area */ |
1292 | /* allocate name to string area */ |
1224 | static char * |
1293 | static char * |
1225 | 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) |
… | |
… | |
1231 | SCHEME_V->no_memory = 1; |
1300 | SCHEME_V->no_memory = 1; |
1232 | return SCHEME_V->strbuff; |
1301 | return SCHEME_V->strbuff; |
1233 | } |
1302 | } |
1234 | |
1303 | |
1235 | if (str) |
1304 | if (str) |
1236 | { |
1305 | memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */ |
1237 | int l = strlen (str); |
|
|
1238 | |
|
|
1239 | if (l > len_str) |
|
|
1240 | l = len_str; |
|
|
1241 | |
|
|
1242 | memcpy (q, str, l); |
|
|
1243 | q[l] = 0; |
|
|
1244 | } |
|
|
1245 | else |
1306 | else |
1246 | { |
|
|
1247 | memset (q, fill, len_str); |
1307 | memset (q, fill, len_str); |
|
|
1308 | |
1248 | q[len_str] = 0; |
1309 | q[len_str] = 0; |
1249 | } |
|
|
1250 | |
1310 | |
1251 | return q; |
1311 | return q; |
1252 | } |
1312 | } |
1253 | |
1313 | |
1254 | INTERFACE pointer |
1314 | INTERFACE pointer |
… | |
… | |
1268 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1328 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1269 | |
1329 | |
1270 | set_typeflag (x, T_STRING | T_ATOM); |
1330 | set_typeflag (x, T_STRING | T_ATOM); |
1271 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1331 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1272 | strlength (x) = len; |
1332 | strlength (x) = len; |
|
|
1333 | |
1273 | return x; |
1334 | return x; |
1274 | } |
1335 | } |
1275 | |
1336 | |
1276 | INTERFACE pointer |
1337 | INTERFACE pointer |
1277 | mk_string (SCHEME_P_ const char *str) |
1338 | mk_string (SCHEME_P_ const char *str) |
… | |
… | |
1284 | { |
1345 | { |
1285 | return get_vector_object (SCHEME_A_ len, NIL); |
1346 | return get_vector_object (SCHEME_A_ len, NIL); |
1286 | } |
1347 | } |
1287 | |
1348 | |
1288 | INTERFACE void |
1349 | INTERFACE void |
1289 | fill_vector (pointer vec, pointer obj) |
1350 | fill_vector (pointer vec, uint32_t start, pointer obj) |
1290 | { |
1351 | { |
1291 | int i; |
1352 | int i; |
1292 | |
1353 | |
1293 | for (i = 0; i < vec->object.vector.length; i++) |
1354 | for (i = start; i < veclength (vec); i++) |
1294 | vecvalue (vec)[i] = obj; |
1355 | vecvalue (vec)[i] = obj; |
1295 | } |
1356 | } |
1296 | |
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 | |
1297 | INTERFACE pointer |
1367 | INTERFACE pointer |
1298 | vector_elem (pointer vec, uint32_t ielem) |
1368 | vector_get (pointer vec, uint32_t ielem) |
1299 | { |
1369 | { |
1300 | return vecvalue(vec)[ielem]; |
1370 | return vecvalue(vec)[ielem]; |
1301 | } |
1371 | } |
1302 | |
1372 | |
1303 | INTERFACE void |
1373 | INTERFACE void |
1304 | set_vector_elem (pointer vec, uint32_t ielem, pointer a) |
1374 | vector_set (pointer vec, uint32_t ielem, pointer a) |
1305 | { |
1375 | { |
1306 | vecvalue(vec)[ielem] = a; |
1376 | vecvalue(vec)[ielem] = a; |
1307 | } |
1377 | } |
1308 | |
1378 | |
1309 | /* get new symbol */ |
1379 | /* get new symbol */ |
… | |
… | |
1317 | x = oblist_add_by_name (SCHEME_A_ name); |
1387 | x = oblist_add_by_name (SCHEME_A_ name); |
1318 | |
1388 | |
1319 | return x; |
1389 | return x; |
1320 | } |
1390 | } |
1321 | |
1391 | |
1322 | INTERFACE pointer |
1392 | ecb_cold INTERFACE pointer |
1323 | gensym (SCHEME_P) |
1393 | gensym (SCHEME_P) |
1324 | { |
1394 | { |
1325 | pointer x; |
1395 | pointer x; |
1326 | |
|
|
1327 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
|
|
1328 | { |
|
|
1329 | char name[40] = "gensym-"; |
1396 | char name[40] = "gensym-"; |
1330 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1397 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1331 | |
1398 | |
1332 | /* first check oblist */ |
1399 | return generate_symbol (SCHEME_A_ name); |
1333 | x = oblist_find_by_name (SCHEME_A_ name); |
1400 | } |
1334 | |
1401 | |
1335 | if (x == NIL) |
1402 | static int |
1336 | { |
1403 | is_gensym (SCHEME_P_ pointer x) |
1337 | x = oblist_add_by_name (SCHEME_A_ name); |
1404 | { |
1338 | return x; |
1405 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1339 | } |
|
|
1340 | } |
|
|
1341 | |
|
|
1342 | return NIL; |
|
|
1343 | } |
1406 | } |
1344 | |
1407 | |
1345 | /* make symbol or number atom from string */ |
1408 | /* make symbol or number atom from string */ |
1346 | static pointer |
1409 | ecb_cold static pointer |
1347 | mk_atom (SCHEME_P_ char *q) |
1410 | mk_atom (SCHEME_P_ char *q) |
1348 | { |
1411 | { |
1349 | char c, *p; |
1412 | char c, *p; |
1350 | int has_dec_point = 0; |
1413 | int has_dec_point = 0; |
1351 | int has_fp_exp = 0; |
1414 | int has_fp_exp = 0; |
… | |
… | |
1401 | } |
1464 | } |
1402 | else if ((c == 'e') || (c == 'E')) |
1465 | else if ((c == 'e') || (c == 'E')) |
1403 | { |
1466 | { |
1404 | if (!has_fp_exp) |
1467 | if (!has_fp_exp) |
1405 | { |
1468 | { |
1406 | has_dec_point = 1; /* decimal point illegal |
1469 | has_dec_point = 1; /* decimal point illegal from now on */ |
1407 | from now on */ |
|
|
1408 | p++; |
1470 | p++; |
1409 | |
1471 | |
1410 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1472 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1411 | continue; |
1473 | continue; |
1412 | } |
1474 | } |
… | |
… | |
1423 | |
1485 | |
1424 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1486 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1425 | } |
1487 | } |
1426 | |
1488 | |
1427 | /* make constant */ |
1489 | /* make constant */ |
1428 | static pointer |
1490 | ecb_cold static pointer |
1429 | mk_sharp_const (SCHEME_P_ char *name) |
1491 | mk_sharp_const (SCHEME_P_ char *name) |
1430 | { |
1492 | { |
1431 | if (!strcmp (name, "t")) |
1493 | if (!strcmp (name, "t")) |
1432 | return S_T; |
1494 | return S_T; |
1433 | else if (!strcmp (name, "f")) |
1495 | else if (!strcmp (name, "f")) |
1434 | return S_F; |
1496 | return S_F; |
1435 | else if (*name == '\\') /* #\w (character) */ |
1497 | else if (*name == '\\') /* #\w (character) */ |
1436 | { |
1498 | { |
1437 | int c; |
1499 | int c; |
1438 | |
1500 | |
|
|
1501 | // TODO: optimise |
1439 | if (stricmp (name + 1, "space") == 0) |
1502 | if (stricmp (name + 1, "space") == 0) |
1440 | c = ' '; |
1503 | c = ' '; |
1441 | else if (stricmp (name + 1, "newline") == 0) |
1504 | else if (stricmp (name + 1, "newline") == 0) |
1442 | c = '\n'; |
1505 | c = '\n'; |
1443 | else if (stricmp (name + 1, "return") == 0) |
1506 | else if (stricmp (name + 1, "return") == 0) |
1444 | c = '\r'; |
1507 | c = '\r'; |
1445 | else if (stricmp (name + 1, "tab") == 0) |
1508 | else if (stricmp (name + 1, "tab") == 0) |
1446 | 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; |
1447 | else if (name[1] == 'x' && name[2] != 0) |
1520 | else if (name[1] == 'x' && name[2] != 0) |
1448 | { |
1521 | { |
1449 | long c1 = strtol (name + 2, 0, 16); |
1522 | long c1 = strtol (name + 2, 0, 16); |
1450 | |
1523 | |
1451 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1524 | if (0 <= c1 && c1 <= UCHAR_MAX) |
… | |
… | |
1476 | return NIL; |
1549 | return NIL; |
1477 | } |
1550 | } |
1478 | } |
1551 | } |
1479 | |
1552 | |
1480 | /* ========== 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 | } |
1481 | |
1573 | |
1482 | /*-- |
1574 | /*-- |
1483 | * 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, |
1484 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1576 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1485 | * for marking. |
1577 | * for marking. |
1486 | * |
1578 | * |
1487 | * The exception is vectors - vectors are currently marked recursively, |
1579 | * The exception is vectors - vectors are currently marked recursively, |
1488 | * 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 |
1489 | * word of context in the vector |
1581 | * word of context in the vector |
1490 | */ |
1582 | */ |
1491 | static void |
1583 | ecb_hot static void |
1492 | mark (pointer a) |
1584 | mark (pointer a) |
1493 | { |
1585 | { |
1494 | pointer t, q, p; |
1586 | pointer t, q, p; |
1495 | |
1587 | |
1496 | t = 0; |
1588 | t = 0; |
… | |
… | |
1500 | |
1592 | |
1501 | if (ecb_expect_false (is_vector (p))) |
1593 | if (ecb_expect_false (is_vector (p))) |
1502 | { |
1594 | { |
1503 | int i; |
1595 | int i; |
1504 | |
1596 | |
1505 | for (i = 0; i < p->object.vector.length; i++) |
1597 | for (i = 0; i < veclength (p); i++) |
1506 | mark (vecvalue (p)[i]); |
1598 | mark (vecvalue (p)[i]); |
1507 | } |
1599 | } |
1508 | |
1600 | |
1509 | if (is_atom (p)) |
1601 | if (is_atom (p)) |
1510 | goto E6; |
1602 | goto E6; |
… | |
… | |
1553 | p = q; |
1645 | p = q; |
1554 | goto E6; |
1646 | goto E6; |
1555 | } |
1647 | } |
1556 | } |
1648 | } |
1557 | |
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 | |
1558 | /* garbage collection. parameter a, b is marked. */ |
1692 | /* garbage collection. parameter a, b is marked. */ |
1559 | static void |
1693 | ecb_cold static void |
1560 | gc (SCHEME_P_ pointer a, pointer b) |
1694 | gc (SCHEME_P_ pointer a, pointer b) |
1561 | { |
1695 | { |
1562 | pointer p; |
|
|
1563 | int i; |
1696 | int i; |
1564 | |
1697 | |
1565 | if (SCHEME_V->gc_verbose) |
1698 | if (SCHEME_V->gc_verbose) |
1566 | putstr (SCHEME_A_ "gc..."); |
1699 | putstr (SCHEME_A_ "gc..."); |
1567 | |
1700 | |
… | |
… | |
1583 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1716 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1584 | mark (car (S_SINK)); |
1717 | mark (car (S_SINK)); |
1585 | /* Mark any older stuff above nested C calls */ |
1718 | /* Mark any older stuff above nested C calls */ |
1586 | mark (SCHEME_V->c_nest); |
1719 | mark (SCHEME_V->c_nest); |
1587 | |
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 | |
1588 | /* mark variables a, b */ |
1728 | /* mark variables a, b */ |
1589 | mark (a); |
1729 | mark (a); |
1590 | mark (b); |
1730 | mark (b); |
1591 | |
1731 | |
1592 | /* garbage collect */ |
1732 | /* garbage collect */ |
1593 | clrmark (NIL); |
1733 | clrmark (NIL); |
1594 | SCHEME_V->fcells = 0; |
1734 | SCHEME_V->fcells = 0; |
1595 | SCHEME_V->free_cell = NIL; |
1735 | SCHEME_V->free_cell = NIL; |
1596 | |
1736 | |
1597 | /* free-list is kept sorted by address so as to maintain consecutive |
|
|
1598 | ranges, if possible, for use with vectors. Here we scan the cells |
|
|
1599 | (which are also kept sorted by address) downwards to build the |
|
|
1600 | free-list in sorted order. |
|
|
1601 | */ |
|
|
1602 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1603 | { |
|
|
1604 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1605 | |
|
|
1606 | while (--p >= SCHEME_V->cell_seg[i]) |
|
|
1607 | { |
|
|
1608 | if (is_mark (p)) |
|
|
1609 | clrmark (p); |
|
|
1610 | else |
|
|
1611 | { |
|
|
1612 | /* reclaim cell */ |
|
|
1613 | if (typeflag (p) != T_FREE) |
|
|
1614 | { |
|
|
1615 | finalize_cell (SCHEME_A_ p); |
|
|
1616 | set_typeflag (p, T_FREE); |
|
|
1617 | set_car (p, NIL); |
|
|
1618 | } |
|
|
1619 | |
|
|
1620 | ++SCHEME_V->fcells; |
|
|
1621 | set_cdr (p, SCHEME_V->free_cell); |
|
|
1622 | SCHEME_V->free_cell = p; |
|
|
1623 | } |
|
|
1624 | } |
|
|
1625 | } |
|
|
1626 | |
|
|
1627 | if (SCHEME_V->gc_verbose) |
1737 | if (SCHEME_V->gc_verbose) |
1628 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
1738 | putstr (SCHEME_A_ "freeing..."); |
1629 | } |
|
|
1630 | |
1739 | |
1631 | static void |
1740 | gc_free (SCHEME_A); |
1632 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1633 | { |
|
|
1634 | /* TODO, fast bitmap check? */ |
|
|
1635 | if (is_string (a)) |
|
|
1636 | free (strvalue (a)); |
|
|
1637 | else if (is_vector (a)) |
|
|
1638 | free (vecvalue (a)); |
|
|
1639 | #if USE_PORTS |
|
|
1640 | else if (is_port (a)) |
|
|
1641 | { |
|
|
1642 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
|
|
1643 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1644 | |
|
|
1645 | free (a->object.port); |
|
|
1646 | } |
|
|
1647 | #endif |
|
|
1648 | } |
1741 | } |
1649 | |
1742 | |
1650 | /* ========== Routines for Reading ========== */ |
1743 | /* ========== Routines for Reading ========== */ |
1651 | |
1744 | |
1652 | static int |
1745 | ecb_cold static int |
1653 | file_push (SCHEME_P_ const char *fname) |
1746 | file_push (SCHEME_P_ const char *fname) |
1654 | { |
1747 | { |
1655 | #if USE_PORTS |
|
|
1656 | int fin; |
1748 | int fin; |
1657 | |
1749 | |
1658 | if (SCHEME_V->file_i == MAXFIL - 1) |
1750 | if (SCHEME_V->file_i == MAXFIL - 1) |
1659 | return 0; |
1751 | return 0; |
1660 | |
1752 | |
… | |
… | |
1666 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1758 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1667 | 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; |
1668 | 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; |
1669 | 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; |
1670 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1762 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1671 | 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); |
1672 | |
1764 | |
1673 | #if SHOW_ERROR_LINE |
1765 | #if SHOW_ERROR_LINE |
1674 | 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; |
1675 | |
1767 | |
1676 | if (fname) |
1768 | if (fname) |
1677 | 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); |
1678 | #endif |
1770 | #endif |
1679 | } |
1771 | } |
1680 | |
1772 | |
1681 | return fin >= 0; |
1773 | return fin >= 0; |
1682 | |
|
|
1683 | #else |
|
|
1684 | return 1; |
|
|
1685 | #endif |
|
|
1686 | } |
1774 | } |
1687 | |
1775 | |
1688 | static void |
1776 | ecb_cold static void |
1689 | file_pop (SCHEME_P) |
1777 | file_pop (SCHEME_P) |
1690 | { |
1778 | { |
1691 | if (SCHEME_V->file_i != 0) |
1779 | if (SCHEME_V->file_i != 0) |
1692 | { |
1780 | { |
1693 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1781 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1694 | #if USE_PORTS |
1782 | #if USE_PORTS |
1695 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1783 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1696 | #endif |
1784 | #endif |
1697 | SCHEME_V->file_i--; |
1785 | SCHEME_V->file_i--; |
1698 | 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); |
1699 | } |
1787 | } |
1700 | } |
1788 | } |
1701 | |
1789 | |
1702 | static int |
1790 | ecb_cold static int |
1703 | file_interactive (SCHEME_P) |
1791 | file_interactive (SCHEME_P) |
1704 | { |
1792 | { |
1705 | #if USE_PORTS |
1793 | #if USE_PORTS |
1706 | return SCHEME_V->file_i == 0 |
1794 | return SCHEME_V->file_i == 0 |
1707 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1795 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1708 | && (SCHEME_V->inport->object.port->kind & port_file); |
1796 | && (port (SCHEME_V->inport)->kind & port_file); |
1709 | #else |
1797 | #else |
1710 | return 0; |
1798 | return 0; |
1711 | #endif |
1799 | #endif |
1712 | } |
1800 | } |
1713 | |
1801 | |
1714 | #if USE_PORTS |
1802 | #if USE_PORTS |
1715 | static port * |
1803 | ecb_cold static port * |
1716 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1804 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1717 | { |
1805 | { |
1718 | int fd; |
1806 | int fd; |
1719 | int flags; |
1807 | int flags; |
1720 | char *rw; |
1808 | char *rw; |
… | |
… | |
1743 | # endif |
1831 | # endif |
1744 | |
1832 | |
1745 | return pt; |
1833 | return pt; |
1746 | } |
1834 | } |
1747 | |
1835 | |
1748 | static pointer |
1836 | ecb_cold static pointer |
1749 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1837 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1750 | { |
1838 | { |
1751 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1839 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1752 | |
1840 | |
1753 | if (!pt && USE_ERROR_CHECKING) |
1841 | if (!pt && USE_ERROR_CHECKING) |
1754 | return NIL; |
1842 | return NIL; |
1755 | |
1843 | |
1756 | return mk_port (SCHEME_A_ pt); |
1844 | return mk_port (SCHEME_A_ pt); |
1757 | } |
1845 | } |
1758 | |
1846 | |
1759 | static port * |
1847 | ecb_cold static port * |
1760 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1848 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1761 | { |
1849 | { |
1762 | port *pt = malloc (sizeof *pt); |
1850 | port *pt = malloc (sizeof *pt); |
1763 | |
1851 | |
1764 | if (!pt && USE_ERROR_CHECKING) |
1852 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1769 | pt->rep.stdio.file = f; |
1857 | pt->rep.stdio.file = f; |
1770 | pt->rep.stdio.closeit = 0; |
1858 | pt->rep.stdio.closeit = 0; |
1771 | return pt; |
1859 | return pt; |
1772 | } |
1860 | } |
1773 | |
1861 | |
1774 | static pointer |
1862 | ecb_cold static pointer |
1775 | port_from_file (SCHEME_P_ int f, int prop) |
1863 | port_from_file (SCHEME_P_ int f, int prop) |
1776 | { |
1864 | { |
1777 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1865 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1778 | |
1866 | |
1779 | if (!pt && USE_ERROR_CHECKING) |
1867 | if (!pt && USE_ERROR_CHECKING) |
1780 | return NIL; |
1868 | return NIL; |
1781 | |
1869 | |
1782 | return mk_port (SCHEME_A_ pt); |
1870 | return mk_port (SCHEME_A_ pt); |
1783 | } |
1871 | } |
1784 | |
1872 | |
1785 | static port * |
1873 | ecb_cold static port * |
1786 | 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) |
1787 | { |
1875 | { |
1788 | port *pt = malloc (sizeof (port)); |
1876 | port *pt = malloc (sizeof (port)); |
1789 | |
1877 | |
1790 | if (!pt && USE_ERROR_CHECKING) |
1878 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1796 | pt->rep.string.curr = start; |
1884 | pt->rep.string.curr = start; |
1797 | pt->rep.string.past_the_end = past_the_end; |
1885 | pt->rep.string.past_the_end = past_the_end; |
1798 | return pt; |
1886 | return pt; |
1799 | } |
1887 | } |
1800 | |
1888 | |
1801 | static pointer |
1889 | ecb_cold static pointer |
1802 | 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) |
1803 | { |
1891 | { |
1804 | 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); |
1805 | |
1893 | |
1806 | if (!pt && USE_ERROR_CHECKING) |
1894 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1809 | return mk_port (SCHEME_A_ pt); |
1897 | return mk_port (SCHEME_A_ pt); |
1810 | } |
1898 | } |
1811 | |
1899 | |
1812 | # define BLOCK_SIZE 256 |
1900 | # define BLOCK_SIZE 256 |
1813 | |
1901 | |
1814 | static port * |
1902 | ecb_cold static port * |
1815 | port_rep_from_scratch (SCHEME_P) |
1903 | port_rep_from_scratch (SCHEME_P) |
1816 | { |
1904 | { |
1817 | char *start; |
1905 | char *start; |
1818 | port *pt = malloc (sizeof (port)); |
1906 | port *pt = malloc (sizeof (port)); |
1819 | |
1907 | |
… | |
… | |
1833 | pt->rep.string.curr = start; |
1921 | pt->rep.string.curr = start; |
1834 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1922 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1835 | return pt; |
1923 | return pt; |
1836 | } |
1924 | } |
1837 | |
1925 | |
1838 | static pointer |
1926 | ecb_cold static pointer |
1839 | port_from_scratch (SCHEME_P) |
1927 | port_from_scratch (SCHEME_P) |
1840 | { |
1928 | { |
1841 | port *pt = port_rep_from_scratch (SCHEME_A); |
1929 | port *pt = port_rep_from_scratch (SCHEME_A); |
1842 | |
1930 | |
1843 | if (!pt && USE_ERROR_CHECKING) |
1931 | if (!pt && USE_ERROR_CHECKING) |
1844 | return NIL; |
1932 | return NIL; |
1845 | |
1933 | |
1846 | return mk_port (SCHEME_A_ pt); |
1934 | return mk_port (SCHEME_A_ pt); |
1847 | } |
1935 | } |
1848 | |
1936 | |
1849 | static void |
1937 | ecb_cold static void |
1850 | port_close (SCHEME_P_ pointer p, int flag) |
1938 | port_close (SCHEME_P_ pointer p, int flag) |
1851 | { |
1939 | { |
1852 | port *pt = p->object.port; |
1940 | port *pt = port (p); |
1853 | |
1941 | |
1854 | pt->kind &= ~flag; |
1942 | pt->kind &= ~flag; |
1855 | |
1943 | |
1856 | if ((pt->kind & (port_input | port_output)) == 0) |
1944 | if ((pt->kind & (port_input | port_output)) == 0) |
1857 | { |
1945 | { |
… | |
… | |
1874 | } |
1962 | } |
1875 | } |
1963 | } |
1876 | #endif |
1964 | #endif |
1877 | |
1965 | |
1878 | /* get new character from input file */ |
1966 | /* get new character from input file */ |
1879 | static int |
1967 | ecb_cold static int |
1880 | inchar (SCHEME_P) |
1968 | inchar (SCHEME_P) |
1881 | { |
1969 | { |
1882 | int c; |
1970 | int c; |
1883 | port *pt; |
1971 | port *pt = port (SCHEME_V->inport); |
1884 | |
|
|
1885 | pt = SCHEME_V->inport->object.port; |
|
|
1886 | |
1972 | |
1887 | if (pt->kind & port_saw_EOF) |
1973 | if (pt->kind & port_saw_EOF) |
1888 | return EOF; |
1974 | return EOF; |
1889 | |
1975 | |
1890 | c = basic_inchar (pt); |
1976 | c = basic_inchar (pt); |
… | |
… | |
1900 | } |
1986 | } |
1901 | |
1987 | |
1902 | return c; |
1988 | return c; |
1903 | } |
1989 | } |
1904 | |
1990 | |
1905 | static int ungot = -1; |
1991 | ecb_cold static int |
1906 | |
|
|
1907 | static int |
|
|
1908 | basic_inchar (port *pt) |
1992 | basic_inchar (port *pt) |
1909 | { |
1993 | { |
1910 | #if USE_PORTS |
|
|
1911 | if (pt->unget != -1) |
1994 | if (pt->unget != -1) |
1912 | { |
1995 | { |
1913 | int r = pt->unget; |
1996 | int r = pt->unget; |
1914 | pt->unget = -1; |
1997 | pt->unget = -1; |
1915 | return r; |
1998 | return r; |
1916 | } |
1999 | } |
1917 | |
2000 | |
|
|
2001 | #if USE_PORTS |
1918 | if (pt->kind & port_file) |
2002 | if (pt->kind & port_file) |
1919 | { |
2003 | { |
1920 | char c; |
2004 | char c; |
1921 | |
2005 | |
1922 | if (!read (pt->rep.stdio.file, &c, 1)) |
2006 | if (!read (pt->rep.stdio.file, &c, 1)) |
… | |
… | |
1930 | return EOF; |
2014 | return EOF; |
1931 | else |
2015 | else |
1932 | return *pt->rep.string.curr++; |
2016 | return *pt->rep.string.curr++; |
1933 | } |
2017 | } |
1934 | #else |
2018 | #else |
1935 | if (ungot == -1) |
|
|
1936 | { |
|
|
1937 | char c; |
2019 | char c; |
1938 | if (!read (0, &c, 1)) |
2020 | |
|
|
2021 | if (!read (pt->rep.stdio.file, &c, 1)) |
1939 | return EOF; |
2022 | return EOF; |
1940 | |
2023 | |
1941 | ungot = c; |
|
|
1942 | } |
|
|
1943 | |
|
|
1944 | { |
|
|
1945 | int r = ungot; |
|
|
1946 | ungot = -1; |
|
|
1947 | return r; |
2024 | return c; |
1948 | } |
|
|
1949 | #endif |
2025 | #endif |
1950 | } |
2026 | } |
1951 | |
2027 | |
1952 | /* back character to input buffer */ |
2028 | /* back character to input buffer */ |
1953 | static void |
2029 | ecb_cold static void |
1954 | backchar (SCHEME_P_ int c) |
2030 | backchar (SCHEME_P_ int c) |
1955 | { |
2031 | { |
1956 | #if USE_PORTS |
2032 | port *pt = port (SCHEME_V->inport); |
1957 | port *pt; |
|
|
1958 | |
2033 | |
1959 | if (c == EOF) |
2034 | if (c == EOF) |
1960 | return; |
2035 | return; |
1961 | |
2036 | |
1962 | pt = SCHEME_V->inport->object.port; |
|
|
1963 | pt->unget = c; |
2037 | pt->unget = c; |
1964 | #else |
|
|
1965 | if (c == EOF) |
|
|
1966 | return; |
|
|
1967 | |
|
|
1968 | ungot = c; |
|
|
1969 | #endif |
|
|
1970 | } |
2038 | } |
1971 | |
2039 | |
1972 | #if USE_PORTS |
2040 | #if USE_PORTS |
1973 | static int |
2041 | ecb_cold static int |
1974 | realloc_port_string (SCHEME_P_ port *p) |
2042 | realloc_port_string (SCHEME_P_ port *p) |
1975 | { |
2043 | { |
1976 | char *start = p->rep.string.start; |
2044 | char *start = p->rep.string.start; |
1977 | 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; |
1978 | char *str = malloc (new_size); |
2046 | char *str = malloc (new_size); |
… | |
… | |
1991 | else |
2059 | else |
1992 | return 0; |
2060 | return 0; |
1993 | } |
2061 | } |
1994 | #endif |
2062 | #endif |
1995 | |
2063 | |
1996 | INTERFACE void |
2064 | ecb_cold static void |
1997 | putstr (SCHEME_P_ const char *s) |
2065 | putchars (SCHEME_P_ const char *s, int len) |
1998 | { |
2066 | { |
|
|
2067 | port *pt = port (SCHEME_V->outport); |
|
|
2068 | |
1999 | #if USE_PORTS |
2069 | #if USE_PORTS |
2000 | port *pt = SCHEME_V->outport->object.port; |
|
|
2001 | |
|
|
2002 | if (pt->kind & port_file) |
|
|
2003 | write (pt->rep.stdio.file, s, strlen (s)); |
|
|
2004 | else |
|
|
2005 | for (; *s; s++) |
|
|
2006 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2007 | *pt->rep.string.curr++ = *s; |
|
|
2008 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2009 | *pt->rep.string.curr++ = *s; |
|
|
2010 | |
|
|
2011 | #else |
|
|
2012 | xwrstr (s); |
|
|
2013 | #endif |
|
|
2014 | } |
|
|
2015 | |
|
|
2016 | static void |
|
|
2017 | putchars (SCHEME_P_ const char *s, int len) |
|
|
2018 | { |
|
|
2019 | #if USE_PORTS |
|
|
2020 | port *pt = SCHEME_V->outport->object.port; |
|
|
2021 | |
|
|
2022 | if (pt->kind & port_file) |
2070 | if (pt->kind & port_file) |
2023 | write (pt->rep.stdio.file, s, len); |
2071 | write (pt->rep.stdio.file, s, len); |
2024 | else |
2072 | else |
2025 | { |
2073 | { |
2026 | for (; len; len--) |
2074 | for (; len; len--) |
… | |
… | |
2031 | *pt->rep.string.curr++ = *s++; |
2079 | *pt->rep.string.curr++ = *s++; |
2032 | } |
2080 | } |
2033 | } |
2081 | } |
2034 | |
2082 | |
2035 | #else |
2083 | #else |
2036 | write (1, s, len); |
2084 | write (1, s, len); // output not initialised |
2037 | #endif |
2085 | #endif |
|
|
2086 | } |
|
|
2087 | |
|
|
2088 | INTERFACE void |
|
|
2089 | putstr (SCHEME_P_ const char *s) |
|
|
2090 | { |
|
|
2091 | putchars (SCHEME_A_ s, strlen (s)); |
2038 | } |
2092 | } |
2039 | |
2093 | |
2040 | INTERFACE void |
2094 | INTERFACE void |
2041 | putcharacter (SCHEME_P_ int c) |
2095 | putcharacter (SCHEME_P_ int c) |
2042 | { |
2096 | { |
2043 | #if USE_PORTS |
|
|
2044 | port *pt = SCHEME_V->outport->object.port; |
|
|
2045 | |
|
|
2046 | if (pt->kind & port_file) |
|
|
2047 | { |
|
|
2048 | char cc = c; |
|
|
2049 | write (pt->rep.stdio.file, &cc, 1); |
|
|
2050 | } |
|
|
2051 | else |
|
|
2052 | { |
|
|
2053 | if (pt->rep.string.curr != pt->rep.string.past_the_end) |
|
|
2054 | *pt->rep.string.curr++ = c; |
|
|
2055 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
|
|
2056 | *pt->rep.string.curr++ = c; |
|
|
2057 | } |
|
|
2058 | |
|
|
2059 | #else |
|
|
2060 | char cc = c; |
2097 | char cc = c; |
2061 | write (1, &c, 1); |
2098 | |
2062 | #endif |
2099 | putchars (SCHEME_A_ &cc, 1); |
2063 | } |
2100 | } |
2064 | |
2101 | |
2065 | /* read characters up to delimiter, but cater to character constants */ |
2102 | /* read characters up to delimiter, but cater to character constants */ |
2066 | static char * |
2103 | ecb_cold static char * |
2067 | readstr_upto (SCHEME_P_ char *delim) |
2104 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2068 | { |
2105 | { |
2069 | char *p = SCHEME_V->strbuff; |
2106 | char *p = SCHEME_V->strbuff + skip; |
2070 | |
2107 | |
2071 | 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)))); |
2072 | |
2109 | |
2073 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2110 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2074 | *p = 0; |
2111 | *p = 0; |
… | |
… | |
2080 | |
2117 | |
2081 | return SCHEME_V->strbuff; |
2118 | return SCHEME_V->strbuff; |
2082 | } |
2119 | } |
2083 | |
2120 | |
2084 | /* read string expression "xxx...xxx" */ |
2121 | /* read string expression "xxx...xxx" */ |
2085 | static pointer |
2122 | ecb_cold static pointer |
2086 | readstrexp (SCHEME_P) |
2123 | readstrexp (SCHEME_P_ char delim) |
2087 | { |
2124 | { |
2088 | char *p = SCHEME_V->strbuff; |
2125 | char *p = SCHEME_V->strbuff; |
2089 | int c; |
2126 | int c; |
2090 | int c1 = 0; |
2127 | int c1 = 0; |
2091 | enum |
|
|
2092 | { 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; |
2093 | |
2129 | |
2094 | for (;;) |
2130 | for (;;) |
2095 | { |
2131 | { |
2096 | c = inchar (SCHEME_A); |
2132 | c = inchar (SCHEME_A); |
2097 | |
2133 | |
… | |
… | |
2099 | return S_F; |
2135 | return S_F; |
2100 | |
2136 | |
2101 | switch (state) |
2137 | switch (state) |
2102 | { |
2138 | { |
2103 | case st_ok: |
2139 | case st_ok: |
2104 | switch (c) |
2140 | if (ecb_expect_false (c == delim)) |
2105 | { |
|
|
2106 | case '\\': |
|
|
2107 | state = st_bsl; |
|
|
2108 | break; |
|
|
2109 | |
|
|
2110 | case '"': |
|
|
2111 | *p = 0; |
|
|
2112 | 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); |
2113 | |
2142 | |
2114 | default: |
2143 | if (ecb_expect_false (c == '\\')) |
|
|
2144 | state = st_bsl; |
|
|
2145 | else |
2115 | *p++ = c; |
2146 | *p++ = c; |
2116 | break; |
|
|
2117 | } |
|
|
2118 | |
2147 | |
2119 | break; |
2148 | break; |
2120 | |
2149 | |
2121 | case st_bsl: |
2150 | case st_bsl: |
2122 | switch (c) |
2151 | switch (c) |
… | |
… | |
2131 | case '7': |
2160 | case '7': |
2132 | state = st_oct1; |
2161 | state = st_oct1; |
2133 | c1 = c - '0'; |
2162 | c1 = c - '0'; |
2134 | break; |
2163 | break; |
2135 | |
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 |
2136 | case 'x': |
2180 | case 'x': |
2137 | case 'X': |
2181 | case 'X': |
2138 | state = st_x1; |
2182 | state = st_x1; |
2139 | c1 = 0; |
2183 | c1 = 0; |
2140 | break; |
2184 | break; |
2141 | |
2185 | |
2142 | case 'n': |
|
|
2143 | *p++ = '\n'; |
|
|
2144 | state = st_ok; |
|
|
2145 | break; |
|
|
2146 | |
|
|
2147 | case 't': |
|
|
2148 | *p++ = '\t'; |
|
|
2149 | state = st_ok; |
|
|
2150 | break; |
|
|
2151 | |
|
|
2152 | case 'r': |
|
|
2153 | *p++ = '\r'; |
|
|
2154 | state = st_ok; |
|
|
2155 | break; |
|
|
2156 | |
|
|
2157 | case '"': |
|
|
2158 | *p++ = '"'; |
|
|
2159 | state = st_ok; |
|
|
2160 | break; |
|
|
2161 | |
|
|
2162 | default: |
2186 | default: |
2163 | *p++ = c; |
2187 | *p++ = c; |
2164 | state = st_ok; |
2188 | state = st_ok; |
2165 | break; |
2189 | break; |
2166 | } |
2190 | } |
2167 | |
2191 | |
2168 | break; |
2192 | break; |
2169 | |
2193 | |
2170 | case st_x1: |
2194 | case st_x1: |
2171 | case st_x2: |
2195 | case st_x2: |
2172 | c = toupper (c); |
2196 | c = tolower (c); |
2173 | |
2197 | |
2174 | if (c >= '0' && c <= 'F') |
2198 | if (c >= '0' && c <= '9') |
2175 | { |
|
|
2176 | if (c <= '9') |
|
|
2177 | c1 = (c1 << 4) + c - '0'; |
2199 | c1 = (c1 << 4) + c - '0'; |
2178 | else |
2200 | else if (c >= 'a' && c <= 'f') |
2179 | c1 = (c1 << 4) + c - 'A' + 10; |
2201 | c1 = (c1 << 4) + c - 'a' + 10; |
2180 | |
|
|
2181 | if (state == st_x1) |
|
|
2182 | state = st_x2; |
|
|
2183 | else |
|
|
2184 | { |
|
|
2185 | *p++ = c1; |
|
|
2186 | state = st_ok; |
|
|
2187 | } |
|
|
2188 | } |
|
|
2189 | else |
2202 | else |
2190 | 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 | } |
2191 | |
2212 | |
2192 | break; |
2213 | break; |
2193 | |
2214 | |
2194 | case st_oct1: |
2215 | case st_oct1: |
2195 | case st_oct2: |
2216 | case st_oct2: |
… | |
… | |
2199 | backchar (SCHEME_A_ c); |
2220 | backchar (SCHEME_A_ c); |
2200 | state = st_ok; |
2221 | state = st_ok; |
2201 | } |
2222 | } |
2202 | else |
2223 | else |
2203 | { |
2224 | { |
2204 | if (state == st_oct2 && c1 >= 32) |
2225 | if (state == st_oct2 && c1 >= ' ') |
2205 | return S_F; |
2226 | return S_F; |
2206 | |
2227 | |
2207 | c1 = (c1 << 3) + (c - '0'); |
2228 | c1 = (c1 << 3) + (c - '0'); |
2208 | |
2229 | |
2209 | if (state == st_oct1) |
2230 | if (state == st_oct1) |
… | |
… | |
2214 | state = st_ok; |
2235 | state = st_ok; |
2215 | } |
2236 | } |
2216 | } |
2237 | } |
2217 | |
2238 | |
2218 | break; |
2239 | break; |
2219 | |
|
|
2220 | } |
2240 | } |
2221 | } |
2241 | } |
2222 | } |
2242 | } |
2223 | |
2243 | |
2224 | /* check c is in chars */ |
2244 | /* check c is in chars */ |
2225 | ecb_inline int |
2245 | ecb_cold int |
2226 | is_one_of (char *s, int c) |
2246 | is_one_of (const char *s, int c) |
2227 | { |
2247 | { |
2228 | if (c == EOF) |
|
|
2229 | return 1; |
|
|
2230 | |
|
|
2231 | return !!strchr (s, c); |
2248 | return c == EOF || !!strchr (s, c); |
2232 | } |
2249 | } |
2233 | |
2250 | |
2234 | /* skip white characters */ |
2251 | /* skip white characters */ |
2235 | ecb_inline int |
2252 | ecb_cold int |
2236 | skipspace (SCHEME_P) |
2253 | skipspace (SCHEME_P) |
2237 | { |
2254 | { |
2238 | int c, curr_line = 0; |
2255 | int c, curr_line = 0; |
2239 | |
2256 | |
2240 | do |
2257 | do |
2241 | { |
2258 | { |
2242 | c = inchar (SCHEME_A); |
2259 | c = inchar (SCHEME_A); |
|
|
2260 | |
2243 | #if SHOW_ERROR_LINE |
2261 | #if SHOW_ERROR_LINE |
2244 | if (c == '\n') |
2262 | if (ecb_expect_false (c == '\n')) |
2245 | curr_line++; |
2263 | curr_line++; |
2246 | #endif |
2264 | #endif |
|
|
2265 | |
|
|
2266 | if (ecb_expect_false (c == EOF)) |
|
|
2267 | return c; |
2247 | } |
2268 | } |
2248 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2269 | while (is_one_of (WHITESPACE, c)); |
2249 | |
2270 | |
2250 | /* record it */ |
2271 | /* record it */ |
2251 | #if SHOW_ERROR_LINE |
2272 | #if SHOW_ERROR_LINE |
2252 | 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) |
2253 | 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; |
2254 | #endif |
2275 | #endif |
2255 | |
2276 | |
2256 | if (c != EOF) |
|
|
2257 | { |
|
|
2258 | backchar (SCHEME_A_ c); |
2277 | backchar (SCHEME_A_ c); |
2259 | return 1; |
2278 | return 1; |
2260 | } |
|
|
2261 | else |
|
|
2262 | return EOF; |
|
|
2263 | } |
2279 | } |
2264 | |
2280 | |
2265 | /* get token */ |
2281 | /* get token */ |
2266 | static int |
2282 | ecb_cold static int |
2267 | token (SCHEME_P) |
2283 | token (SCHEME_P) |
2268 | { |
2284 | { |
2269 | int c = skipspace (SCHEME_A); |
2285 | int c = skipspace (SCHEME_A); |
2270 | |
2286 | |
2271 | if (c == EOF) |
2287 | if (c == EOF) |
… | |
… | |
2283 | return TOK_RPAREN; |
2299 | return TOK_RPAREN; |
2284 | |
2300 | |
2285 | case '.': |
2301 | case '.': |
2286 | c = inchar (SCHEME_A); |
2302 | c = inchar (SCHEME_A); |
2287 | |
2303 | |
2288 | if (is_one_of (" \n\t", c)) |
2304 | if (is_one_of (WHITESPACE, c)) |
2289 | return TOK_DOT; |
2305 | return TOK_DOT; |
2290 | else |
2306 | else |
2291 | { |
2307 | { |
2292 | //TODO: ungetc twice in a row is not supported in C |
|
|
2293 | backchar (SCHEME_A_ c); |
2308 | backchar (SCHEME_A_ c); |
2294 | backchar (SCHEME_A_ '.'); |
|
|
2295 | return TOK_ATOM; |
2309 | return TOK_DOTATOM; |
2296 | } |
2310 | } |
|
|
2311 | |
|
|
2312 | case '|': |
|
|
2313 | return TOK_STRATOM; |
2297 | |
2314 | |
2298 | case '\'': |
2315 | case '\'': |
2299 | return TOK_QUOTE; |
2316 | return TOK_QUOTE; |
2300 | |
2317 | |
2301 | case ';': |
2318 | case ';': |
… | |
… | |
2368 | } |
2385 | } |
2369 | |
2386 | |
2370 | /* ========== Routines for Printing ========== */ |
2387 | /* ========== Routines for Printing ========== */ |
2371 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2388 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2372 | |
2389 | |
2373 | static void |
2390 | ecb_cold static void |
2374 | printslashstring (SCHEME_P_ char *p, int len) |
2391 | printslashstring (SCHEME_P_ char *p, int len) |
2375 | { |
2392 | { |
2376 | int i; |
2393 | int i; |
2377 | unsigned char *s = (unsigned char *) p; |
2394 | unsigned char *s = (unsigned char *) p; |
2378 | |
2395 | |
… | |
… | |
2433 | } |
2450 | } |
2434 | |
2451 | |
2435 | putcharacter (SCHEME_A_ '"'); |
2452 | putcharacter (SCHEME_A_ '"'); |
2436 | } |
2453 | } |
2437 | |
2454 | |
2438 | |
|
|
2439 | /* print atoms */ |
2455 | /* print atoms */ |
2440 | static void |
2456 | ecb_cold static void |
2441 | printatom (SCHEME_P_ pointer l, int f) |
2457 | printatom (SCHEME_P_ pointer l, int f) |
2442 | { |
2458 | { |
2443 | char *p; |
2459 | char *p; |
2444 | int len; |
2460 | int len; |
2445 | |
2461 | |
2446 | atom2str (SCHEME_A_ l, f, &p, &len); |
2462 | atom2str (SCHEME_A_ l, f, &p, &len); |
2447 | putchars (SCHEME_A_ p, len); |
2463 | putchars (SCHEME_A_ p, len); |
2448 | } |
2464 | } |
2449 | |
2465 | |
2450 | |
|
|
2451 | /* Uses internal buffer unless string pointer is already available */ |
2466 | /* Uses internal buffer unless string pointer is already available */ |
2452 | static void |
2467 | ecb_cold static void |
2453 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2468 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2454 | { |
2469 | { |
2455 | char *p; |
2470 | char *p; |
2456 | |
2471 | |
2457 | if (l == NIL) |
2472 | if (l == NIL) |
… | |
… | |
2615 | #endif |
2630 | #endif |
2616 | } |
2631 | } |
2617 | else if (is_continuation (l)) |
2632 | else if (is_continuation (l)) |
2618 | p = "#<CONTINUATION>"; |
2633 | p = "#<CONTINUATION>"; |
2619 | else |
2634 | else |
|
|
2635 | { |
|
|
2636 | #if USE_PRINTF |
|
|
2637 | p = SCHEME_V->strbuff; |
|
|
2638 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2639 | #else |
2620 | p = "#<ERROR>"; |
2640 | p = "#<ERROR>"; |
|
|
2641 | #endif |
|
|
2642 | } |
2621 | |
2643 | |
2622 | *pp = p; |
2644 | *pp = p; |
2623 | *plen = strlen (p); |
2645 | *plen = strlen (p); |
2624 | } |
2646 | } |
2625 | |
2647 | |
… | |
… | |
2657 | return car (d); |
2679 | return car (d); |
2658 | |
2680 | |
2659 | p = cons (car (d), cdr (d)); |
2681 | p = cons (car (d), cdr (d)); |
2660 | q = p; |
2682 | q = p; |
2661 | |
2683 | |
2662 | while (cdr (cdr (p)) != NIL) |
2684 | while (cddr (p) != NIL) |
2663 | { |
2685 | { |
2664 | d = cons (car (p), cdr (p)); |
2686 | d = cons (car (p), cdr (p)); |
2665 | |
2687 | |
2666 | if (cdr (cdr (p)) != NIL) |
2688 | if (cddr (p) != NIL) |
2667 | p = cdr (d); |
2689 | p = cdr (d); |
2668 | } |
2690 | } |
2669 | |
2691 | |
2670 | set_cdr (p, car (cdr (p))); |
2692 | set_cdr (p, cadr (p)); |
2671 | return q; |
2693 | return q; |
2672 | } |
2694 | } |
2673 | |
2695 | |
2674 | /* reverse list -- produce new list */ |
2696 | /* reverse list -- produce new list */ |
2675 | static pointer |
2697 | ecb_hot static pointer |
2676 | reverse (SCHEME_P_ pointer a) |
2698 | reverse (SCHEME_P_ pointer a) |
2677 | { |
2699 | { |
2678 | /* a must be checked by gc */ |
2700 | /* a must be checked by gc */ |
2679 | pointer p = NIL; |
2701 | pointer p = NIL; |
2680 | |
2702 | |
… | |
… | |
2683 | |
2705 | |
2684 | return p; |
2706 | return p; |
2685 | } |
2707 | } |
2686 | |
2708 | |
2687 | /* reverse list --- in-place */ |
2709 | /* reverse list --- in-place */ |
2688 | static pointer |
2710 | ecb_hot static pointer |
2689 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2711 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2690 | { |
2712 | { |
2691 | pointer result = term; |
2713 | pointer result = term; |
2692 | pointer p = list; |
2714 | pointer p = list; |
2693 | |
2715 | |
… | |
… | |
2701 | |
2723 | |
2702 | return result; |
2724 | return result; |
2703 | } |
2725 | } |
2704 | |
2726 | |
2705 | /* append list -- produce new list (in reverse order) */ |
2727 | /* append list -- produce new list (in reverse order) */ |
2706 | static pointer |
2728 | ecb_hot static pointer |
2707 | revappend (SCHEME_P_ pointer a, pointer b) |
2729 | revappend (SCHEME_P_ pointer a, pointer b) |
2708 | { |
2730 | { |
2709 | pointer result = a; |
2731 | pointer result = a; |
2710 | pointer p = b; |
2732 | pointer p = b; |
2711 | |
2733 | |
… | |
… | |
2720 | |
2742 | |
2721 | return S_F; /* signal an error */ |
2743 | return S_F; /* signal an error */ |
2722 | } |
2744 | } |
2723 | |
2745 | |
2724 | /* equivalence of atoms */ |
2746 | /* equivalence of atoms */ |
2725 | int |
2747 | ecb_hot int |
2726 | eqv (pointer a, pointer b) |
2748 | eqv (pointer a, pointer b) |
2727 | { |
2749 | { |
2728 | if (is_string (a)) |
2750 | if (is_string (a)) |
2729 | { |
2751 | { |
2730 | if (is_string (b)) |
2752 | if (is_string (b)) |
… | |
… | |
2769 | /* () is #t in R5RS */ |
2791 | /* () is #t in R5RS */ |
2770 | #define is_true(p) ((p) != S_F) |
2792 | #define is_true(p) ((p) != S_F) |
2771 | #define is_false(p) ((p) == S_F) |
2793 | #define is_false(p) ((p) == S_F) |
2772 | |
2794 | |
2773 | /* ========== Environment implementation ========== */ |
2795 | /* ========== Environment implementation ========== */ |
2774 | |
|
|
2775 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
|
|
2776 | |
|
|
2777 | static int |
|
|
2778 | hash_fn (const char *key, int table_size) |
|
|
2779 | { |
|
|
2780 | const unsigned char *p = key; |
|
|
2781 | uint32_t hash = 2166136261; |
|
|
2782 | |
|
|
2783 | while (*p) |
|
|
2784 | hash = (hash ^ *p++) * 16777619; |
|
|
2785 | |
|
|
2786 | return hash % table_size; |
|
|
2787 | } |
|
|
2788 | #endif |
|
|
2789 | |
2796 | |
2790 | #ifndef USE_ALIST_ENV |
2797 | #ifndef USE_ALIST_ENV |
2791 | |
2798 | |
2792 | /* |
2799 | /* |
2793 | * In this implementation, each frame of the environment may be |
2800 | * In this implementation, each frame of the environment may be |
… | |
… | |
2810 | |
2817 | |
2811 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2818 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2812 | setenvironment (SCHEME_V->envir); |
2819 | setenvironment (SCHEME_V->envir); |
2813 | } |
2820 | } |
2814 | |
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 | |
2815 | ecb_inline void |
2837 | ecb_inline void |
2816 | 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) |
2817 | { |
2839 | { |
2818 | pointer slot = immutable_cons (variable, value); |
2840 | pointer slot = immutable_cons (variable, value); |
2819 | |
2841 | |
2820 | if (is_vector (car (env))) |
2842 | if (is_vector (car (env))) |
2821 | { |
2843 | { |
2822 | int location = hash_fn (symname (variable), veclength (car (env))); |
2844 | int location = sym_hash (variable, veclength (car (env))); |
2823 | |
|
|
2824 | 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))); |
2825 | } |
2846 | } |
2826 | else |
2847 | else |
2827 | set_car (env, immutable_cons (slot, car (env))); |
2848 | set_car (env, immutable_cons (slot, car (env))); |
2828 | } |
2849 | } |
2829 | |
2850 | |
2830 | static pointer |
2851 | ecb_hot static pointer |
2831 | 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) |
2832 | { |
2853 | { |
2833 | pointer x, y; |
2854 | pointer x, y; |
2834 | int location; |
|
|
2835 | |
2855 | |
2836 | for (x = env; x != NIL; x = cdr (x)) |
2856 | for (x = env; x != NIL; x = cdr (x)) |
2837 | { |
2857 | { |
2838 | if (is_vector (car (x))) |
2858 | if (is_vector (car (x))) |
2839 | { |
2859 | { |
2840 | location = hash_fn (symname (hdl), veclength (car (x))); |
2860 | int location = sym_hash (hdl, veclength (car (x))); |
2841 | y = vector_elem (car (x), location); |
2861 | y = vector_get (car (x), location); |
2842 | } |
2862 | } |
2843 | else |
2863 | else |
2844 | y = car (x); |
2864 | y = car (x); |
2845 | |
2865 | |
2846 | for (; y != NIL; y = cdr (y)) |
2866 | for (; y != NIL; y = cdr (y)) |
2847 | if (caar (y) == hdl) |
2867 | if (caar (y) == hdl) |
2848 | break; |
2868 | break; |
2849 | |
2869 | |
2850 | if (y != NIL) |
2870 | if (y != NIL) |
|
|
2871 | return car (y); |
|
|
2872 | |
|
|
2873 | if (!all) |
2851 | break; |
2874 | break; |
2852 | |
|
|
2853 | if (!all) |
|
|
2854 | return NIL; |
|
|
2855 | } |
2875 | } |
2856 | |
|
|
2857 | if (x != NIL) |
|
|
2858 | return car (y); |
|
|
2859 | |
2876 | |
2860 | return NIL; |
2877 | return NIL; |
2861 | } |
2878 | } |
2862 | |
2879 | |
2863 | #else /* USE_ALIST_ENV */ |
2880 | #else /* USE_ALIST_ENV */ |
2864 | |
2881 | |
2865 | ecb_inline void |
2882 | static void |
2866 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2883 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2867 | { |
2884 | { |
2868 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2885 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2869 | setenvironment (SCHEME_V->envir); |
2886 | setenvironment (SCHEME_V->envir); |
2870 | } |
2887 | } |
2871 | |
2888 | |
2872 | ecb_inline void |
2889 | static void |
2873 | 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) |
2874 | { |
2891 | { |
2875 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2892 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2876 | } |
2893 | } |
2877 | |
2894 | |
2878 | static pointer |
2895 | ecb_hot static pointer |
2879 | 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) |
2880 | { |
2897 | { |
2881 | pointer x, y; |
2898 | pointer x, y; |
2882 | |
2899 | |
2883 | for (x = env; x != NIL; x = cdr (x)) |
2900 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2885 | for (y = car (x); y != NIL; y = cdr (y)) |
2902 | for (y = car (x); y != NIL; y = cdr (y)) |
2886 | if (caar (y) == hdl) |
2903 | if (caar (y) == hdl) |
2887 | break; |
2904 | break; |
2888 | |
2905 | |
2889 | if (y != NIL) |
2906 | if (y != NIL) |
|
|
2907 | return car (y); |
2890 | break; |
2908 | break; |
2891 | |
2909 | |
2892 | if (!all) |
2910 | if (!all) |
2893 | return NIL; |
2911 | break; |
2894 | } |
2912 | } |
2895 | |
|
|
2896 | if (x != NIL) |
|
|
2897 | return car (y); |
|
|
2898 | |
2913 | |
2899 | return NIL; |
2914 | return NIL; |
2900 | } |
2915 | } |
2901 | |
2916 | |
2902 | #endif /* USE_ALIST_ENV else */ |
2917 | #endif /* USE_ALIST_ENV else */ |
2903 | |
2918 | |
2904 | ecb_inline void |
2919 | static void |
2905 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2920 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2906 | { |
2921 | { |
|
|
2922 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2907 | 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); |
2908 | } |
2924 | } |
2909 | |
2925 | |
2910 | ecb_inline void |
2926 | static void |
2911 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2927 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2912 | { |
2928 | { |
2913 | set_cdr (slot, value); |
2929 | set_cdr (slot, value); |
2914 | } |
2930 | } |
2915 | |
2931 | |
2916 | ecb_inline pointer |
2932 | static pointer |
2917 | slot_value_in_env (pointer slot) |
2933 | slot_value_in_env (pointer slot) |
2918 | { |
2934 | { |
2919 | return cdr (slot); |
2935 | return cdr (slot); |
2920 | } |
2936 | } |
2921 | |
2937 | |
2922 | /* ========== Evaluation Cycle ========== */ |
2938 | /* ========== Evaluation Cycle ========== */ |
2923 | |
2939 | |
2924 | static int |
2940 | ecb_cold static int |
2925 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2941 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2926 | { |
2942 | { |
2927 | #if USE_ERROR_HOOK |
|
|
2928 | pointer x; |
|
|
2929 | pointer hdl = SCHEME_V->ERROR_HOOK; |
|
|
2930 | #endif |
|
|
2931 | |
|
|
2932 | #if USE_PRINTF |
2943 | #if USE_PRINTF |
2933 | #if SHOW_ERROR_LINE |
2944 | #if SHOW_ERROR_LINE |
2934 | char sbuf[STRBUFFSIZE]; |
2945 | char sbuf[STRBUFFSIZE]; |
2935 | |
2946 | |
2936 | /* make sure error is not in REPL */ |
2947 | /* make sure error is not in REPL */ |
… | |
… | |
2951 | } |
2962 | } |
2952 | #endif |
2963 | #endif |
2953 | #endif |
2964 | #endif |
2954 | |
2965 | |
2955 | #if USE_ERROR_HOOK |
2966 | #if USE_ERROR_HOOK |
2956 | 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); |
2957 | |
2968 | |
2958 | if (x != NIL) |
2969 | if (x != NIL) |
2959 | { |
2970 | { |
2960 | pointer code = a |
2971 | pointer code = a |
2961 | ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) |
2972 | ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) |
… | |
… | |
3005 | pointer code; |
3016 | pointer code; |
3006 | }; |
3017 | }; |
3007 | |
3018 | |
3008 | # define STACK_GROWTH 3 |
3019 | # define STACK_GROWTH 3 |
3009 | |
3020 | |
3010 | static void |
3021 | ecb_hot static void |
3011 | 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) |
3012 | { |
3023 | { |
3013 | int nframes = (uintptr_t)SCHEME_V->dump; |
3024 | int nframes = (uintptr_t)SCHEME_V->dump; |
3014 | struct dump_stack_frame *next_frame; |
3025 | struct dump_stack_frame *next_frame; |
3015 | |
3026 | |
3016 | /* enough room for the next frame? */ |
3027 | /* enough room for the next frame? */ |
3017 | if (nframes >= SCHEME_V->dump_size) |
3028 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3018 | { |
3029 | { |
3019 | SCHEME_V->dump_size += STACK_GROWTH; |
3030 | SCHEME_V->dump_size += STACK_GROWTH; |
3020 | 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); |
3021 | } |
3032 | } |
3022 | |
3033 | |
… | |
… | |
3028 | next_frame->code = code; |
3039 | next_frame->code = code; |
3029 | |
3040 | |
3030 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3041 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3031 | } |
3042 | } |
3032 | |
3043 | |
3033 | static int |
3044 | static ecb_hot int |
3034 | xs_return (SCHEME_P_ pointer a) |
3045 | xs_return (SCHEME_P_ pointer a) |
3035 | { |
3046 | { |
3036 | int nframes = (uintptr_t)SCHEME_V->dump; |
3047 | int nframes = (uintptr_t)SCHEME_V->dump; |
3037 | struct dump_stack_frame *frame; |
3048 | struct dump_stack_frame *frame; |
3038 | |
3049 | |
… | |
… | |
3049 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3060 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3050 | |
3061 | |
3051 | return 0; |
3062 | return 0; |
3052 | } |
3063 | } |
3053 | |
3064 | |
3054 | ecb_inline void |
3065 | ecb_cold void |
3055 | dump_stack_reset (SCHEME_P) |
3066 | dump_stack_reset (SCHEME_P) |
3056 | { |
3067 | { |
3057 | /* 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 */ |
3058 | SCHEME_V->dump = (pointer)+0; |
3069 | SCHEME_V->dump = (pointer)+0; |
3059 | } |
3070 | } |
3060 | |
3071 | |
3061 | ecb_inline void |
3072 | ecb_cold void |
3062 | dump_stack_initialize (SCHEME_P) |
3073 | dump_stack_initialize (SCHEME_P) |
3063 | { |
3074 | { |
3064 | SCHEME_V->dump_size = 0; |
3075 | SCHEME_V->dump_size = 0; |
3065 | SCHEME_V->dump_base = 0; |
3076 | SCHEME_V->dump_base = 0; |
3066 | dump_stack_reset (SCHEME_A); |
3077 | dump_stack_reset (SCHEME_A); |
3067 | } |
3078 | } |
3068 | |
3079 | |
3069 | static void |
3080 | ecb_cold static void |
3070 | dump_stack_free (SCHEME_P) |
3081 | dump_stack_free (SCHEME_P) |
3071 | { |
3082 | { |
3072 | free (SCHEME_V->dump_base); |
3083 | free (SCHEME_V->dump_base); |
3073 | SCHEME_V->dump_base = 0; |
3084 | SCHEME_V->dump_base = 0; |
3074 | SCHEME_V->dump = (pointer)0; |
3085 | SCHEME_V->dump = (pointer)0; |
3075 | SCHEME_V->dump_size = 0; |
3086 | SCHEME_V->dump_size = 0; |
3076 | } |
3087 | } |
3077 | |
3088 | |
3078 | static void |
3089 | ecb_cold static void |
3079 | dump_stack_mark (SCHEME_P) |
3090 | dump_stack_mark (SCHEME_P) |
3080 | { |
3091 | { |
3081 | int nframes = (uintptr_t)SCHEME_V->dump; |
3092 | int nframes = (uintptr_t)SCHEME_V->dump; |
3082 | int i; |
3093 | int i; |
3083 | |
3094 | |
… | |
… | |
3089 | mark (frame->envir); |
3100 | mark (frame->envir); |
3090 | mark (frame->code); |
3101 | mark (frame->code); |
3091 | } |
3102 | } |
3092 | } |
3103 | } |
3093 | |
3104 | |
3094 | static pointer |
3105 | ecb_cold static pointer |
3095 | ss_get_cont (SCHEME_P) |
3106 | ss_get_cont (SCHEME_P) |
3096 | { |
3107 | { |
3097 | int nframes = (uintptr_t)SCHEME_V->dump; |
3108 | int nframes = (uintptr_t)SCHEME_V->dump; |
3098 | int i; |
3109 | int i; |
3099 | |
3110 | |
… | |
… | |
3111 | } |
3122 | } |
3112 | |
3123 | |
3113 | return cont; |
3124 | return cont; |
3114 | } |
3125 | } |
3115 | |
3126 | |
3116 | static void |
3127 | ecb_cold static void |
3117 | ss_set_cont (SCHEME_P_ pointer cont) |
3128 | ss_set_cont (SCHEME_P_ pointer cont) |
3118 | { |
3129 | { |
3119 | int i = 0; |
3130 | int i = 0; |
3120 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3131 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3121 | |
3132 | |
3122 | while (cont != NIL) |
3133 | while (cont != NIL) |
3123 | { |
3134 | { |
3124 | frame->op = ivalue (car (cont)); cont = cdr (cont); |
3135 | frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont); |
3125 | frame->args = car (cont) ; cont = cdr (cont); |
3136 | frame->args = car (cont) ; cont = cdr (cont); |
3126 | frame->envir = car (cont) ; cont = cdr (cont); |
3137 | frame->envir = car (cont) ; cont = cdr (cont); |
3127 | frame->code = car (cont) ; cont = cdr (cont); |
3138 | frame->code = car (cont) ; cont = cdr (cont); |
3128 | |
3139 | |
3129 | ++frame; |
3140 | ++frame; |
3130 | ++i; |
3141 | ++i; |
3131 | } |
3142 | } |
3132 | |
3143 | |
3133 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3144 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3134 | } |
3145 | } |
3135 | |
3146 | |
3136 | #else |
3147 | #else |
3137 | |
3148 | |
3138 | ecb_inline void |
3149 | ecb_cold void |
3139 | dump_stack_reset (SCHEME_P) |
3150 | dump_stack_reset (SCHEME_P) |
3140 | { |
3151 | { |
3141 | SCHEME_V->dump = NIL; |
3152 | SCHEME_V->dump = NIL; |
3142 | } |
3153 | } |
3143 | |
3154 | |
3144 | ecb_inline void |
3155 | ecb_cold void |
3145 | dump_stack_initialize (SCHEME_P) |
3156 | dump_stack_initialize (SCHEME_P) |
3146 | { |
3157 | { |
3147 | dump_stack_reset (SCHEME_A); |
3158 | dump_stack_reset (SCHEME_A); |
3148 | } |
3159 | } |
3149 | |
3160 | |
3150 | static void |
3161 | ecb_cold static void |
3151 | dump_stack_free (SCHEME_P) |
3162 | dump_stack_free (SCHEME_P) |
3152 | { |
3163 | { |
3153 | SCHEME_V->dump = NIL; |
3164 | SCHEME_V->dump = NIL; |
3154 | } |
3165 | } |
3155 | |
3166 | |
3156 | static int |
3167 | ecb_hot static int |
3157 | xs_return (SCHEME_P_ pointer a) |
3168 | xs_return (SCHEME_P_ pointer a) |
3158 | { |
3169 | { |
3159 | pointer dump = SCHEME_V->dump; |
3170 | pointer dump = SCHEME_V->dump; |
3160 | |
3171 | |
3161 | SCHEME_V->value = a; |
3172 | SCHEME_V->value = a; |
3162 | |
3173 | |
3163 | if (dump == NIL) |
3174 | if (dump == NIL) |
3164 | return -1; |
3175 | return -1; |
3165 | |
3176 | |
3166 | SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); |
3177 | SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump); |
3167 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3178 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3168 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3179 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3169 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3180 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3170 | |
3181 | |
3171 | SCHEME_V->dump = dump; |
3182 | SCHEME_V->dump = dump; |
3172 | |
3183 | |
3173 | return 0; |
3184 | return 0; |
3174 | } |
3185 | } |
3175 | |
3186 | |
3176 | static void |
3187 | ecb_hot static void |
3177 | 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) |
3178 | { |
3189 | { |
3179 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3190 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3180 | cons (args, |
3191 | cons (args, |
3181 | cons (SCHEME_V->envir, |
3192 | cons (SCHEME_V->envir, |
3182 | cons (code, |
3193 | cons (code, |
3183 | SCHEME_V->dump)))); |
3194 | SCHEME_V->dump)))); |
3184 | } |
3195 | } |
3185 | |
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 | |
3186 | static void |
3400 | static void |
3187 | dump_stack_mark (SCHEME_P) |
3401 | compile_if (SCHEME_P_ stream s, pointer cond, pointer ift, pointer iff) |
3188 | { |
3402 | { |
3189 | mark (SCHEME_V->dump); |
3403 | //TODO: borked |
3190 | } |
3404 | stream sift = stream_init (); compile_expr (SCHEME_A_ sift, ift); |
3191 | |
3405 | |
3192 | static pointer |
3406 | stream_put (s, BOP_BIFF); |
3193 | 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) |
3194 | { |
3423 | { |
3195 | return SCHEME_V->dump; |
3424 | stream_put (s, 0); |
|
|
3425 | stream_put (s, 0); |
|
|
3426 | |
|
|
3427 | return stream_size (s); |
3196 | } |
3428 | } |
3197 | |
3429 | |
3198 | static void |
3430 | static void |
3199 | ss_set_cont (SCHEME_P_ pointer cont) |
3431 | stream_fix_fixup (stream s, stream_fixup fixup, uint32_t target) |
3200 | { |
3432 | { |
3201 | 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; |
3202 | } |
3437 | } |
3203 | |
3438 | |
3204 | #endif |
|
|
3205 | |
|
|
3206 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
|
|
3207 | |
|
|
3208 | 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 |
3209 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3645 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3210 | { |
3646 | { |
3211 | pointer args = SCHEME_V->args; |
3647 | pointer args = SCHEME_V->args; |
3212 | pointer x, y; |
3648 | pointer x, y; |
3213 | |
3649 | |
3214 | switch (op) |
3650 | switch (op) |
3215 | { |
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 | |
3216 | case OP_LOAD: /* load */ |
3665 | case OP_LOAD: /* load */ |
3217 | if (file_interactive (SCHEME_A)) |
3666 | if (file_interactive (SCHEME_A)) |
3218 | { |
3667 | { |
3219 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3668 | putstr (SCHEME_A_ "Loading "); |
3220 | //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'); |
3221 | } |
3671 | } |
3222 | |
3672 | |
3223 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3673 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3224 | Error_1 ("unable to open", car (args)); |
3674 | Error_1 ("unable to open", car (args)); |
3225 | else |
3675 | |
3226 | { |
|
|
3227 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3676 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3228 | s_goto (OP_T0LVL); |
3677 | s_goto (OP_T0LVL); |
3229 | } |
|
|
3230 | |
3678 | |
3231 | case OP_T0LVL: /* top level */ |
3679 | case OP_T0LVL: /* top level */ |
3232 | |
3680 | |
3233 | /* If we reached the end of file, this loop is done. */ |
3681 | /* If we reached the end of file, this loop is done. */ |
3234 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3682 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3235 | { |
3683 | { |
3236 | if (SCHEME_V->file_i == 0) |
3684 | if (SCHEME_V->file_i == 0) |
3237 | { |
3685 | { |
3238 | SCHEME_V->args = NIL; |
3686 | SCHEME_V->args = NIL; |
3239 | s_goto (OP_QUIT); |
3687 | s_goto (OP_QUIT); |
… | |
… | |
3250 | /* If interactive, be nice to user. */ |
3698 | /* If interactive, be nice to user. */ |
3251 | if (file_interactive (SCHEME_A)) |
3699 | if (file_interactive (SCHEME_A)) |
3252 | { |
3700 | { |
3253 | SCHEME_V->envir = SCHEME_V->global_env; |
3701 | SCHEME_V->envir = SCHEME_V->global_env; |
3254 | dump_stack_reset (SCHEME_A); |
3702 | dump_stack_reset (SCHEME_A); |
3255 | putstr (SCHEME_A_ "\n"); |
3703 | putcharacter (SCHEME_A_ '\n'); |
|
|
3704 | #if EXPERIMENT |
|
|
3705 | system ("ps v $PPID"); |
|
|
3706 | #endif |
3256 | putstr (SCHEME_A_ prompt); |
3707 | putstr (SCHEME_A_ prompt); |
3257 | } |
3708 | } |
3258 | |
3709 | |
3259 | /* Set up another iteration of REPL */ |
3710 | /* Set up another iteration of REPL */ |
3260 | SCHEME_V->nesting = 0; |
3711 | SCHEME_V->nesting = 0; |
… | |
… | |
3295 | { |
3746 | { |
3296 | SCHEME_V->print_flag = 1; |
3747 | SCHEME_V->print_flag = 1; |
3297 | SCHEME_V->args = SCHEME_V->value; |
3748 | SCHEME_V->args = SCHEME_V->value; |
3298 | s_goto (OP_P0LIST); |
3749 | s_goto (OP_P0LIST); |
3299 | } |
3750 | } |
3300 | else |
3751 | |
3301 | s_return (SCHEME_V->value); |
3752 | s_return (SCHEME_V->value); |
3302 | |
3753 | |
3303 | case OP_EVAL: /* main part of evaluation */ |
3754 | case OP_EVAL: /* main part of evaluation */ |
3304 | #if USE_TRACING |
3755 | #if USE_TRACING |
3305 | if (SCHEME_V->tracing) |
3756 | if (SCHEME_V->tracing) |
3306 | { |
3757 | { |
… | |
… | |
3317 | #endif |
3768 | #endif |
3318 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3769 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3319 | { |
3770 | { |
3320 | 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); |
3321 | |
3772 | |
3322 | if (x != NIL) |
3773 | if (x == NIL) |
3323 | s_return (slot_value_in_env (x)); |
|
|
3324 | else |
|
|
3325 | 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)); |
3326 | } |
3777 | } |
3327 | else if (is_pair (SCHEME_V->code)) |
3778 | else if (is_pair (SCHEME_V->code)) |
3328 | { |
3779 | { |
3329 | x = car (SCHEME_V->code); |
3780 | x = car (SCHEME_V->code); |
3330 | |
3781 | |
… | |
… | |
3339 | /* 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)); */ |
3340 | SCHEME_V->code = x; |
3791 | SCHEME_V->code = x; |
3341 | s_goto (OP_EVAL); |
3792 | s_goto (OP_EVAL); |
3342 | } |
3793 | } |
3343 | } |
3794 | } |
3344 | else |
3795 | |
3345 | s_return (SCHEME_V->code); |
3796 | s_return (SCHEME_V->code); |
3346 | |
3797 | |
3347 | case OP_E0ARGS: /* eval arguments */ |
3798 | case OP_E0ARGS: /* eval arguments */ |
3348 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3799 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3349 | { |
3800 | { |
3350 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3801 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3351 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3802 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3352 | SCHEME_V->code = SCHEME_V->value; |
3803 | SCHEME_V->code = SCHEME_V->value; |
3353 | s_goto (OP_APPLY); |
3804 | s_goto (OP_APPLY); |
3354 | } |
3805 | } |
3355 | else |
3806 | |
3356 | { |
|
|
3357 | SCHEME_V->code = cdr (SCHEME_V->code); |
3807 | SCHEME_V->code = cdr (SCHEME_V->code); |
3358 | s_goto (OP_E1ARGS); |
3808 | s_goto (OP_E1ARGS); |
3359 | } |
|
|
3360 | |
3809 | |
3361 | case OP_E1ARGS: /* eval arguments */ |
3810 | case OP_E1ARGS: /* eval arguments */ |
3362 | args = cons (SCHEME_V->value, args); |
3811 | args = cons (SCHEME_V->value, args); |
3363 | |
3812 | |
3364 | if (is_pair (SCHEME_V->code)) /* continue */ |
3813 | if (is_pair (SCHEME_V->code)) /* continue */ |
… | |
… | |
3375 | SCHEME_V->args = cdr (args); |
3824 | SCHEME_V->args = cdr (args); |
3376 | s_goto (OP_APPLY); |
3825 | s_goto (OP_APPLY); |
3377 | } |
3826 | } |
3378 | |
3827 | |
3379 | #if USE_TRACING |
3828 | #if USE_TRACING |
3380 | |
|
|
3381 | case OP_TRACING: |
3829 | case OP_TRACING: |
3382 | { |
3830 | { |
3383 | int tr = SCHEME_V->tracing; |
3831 | int tr = SCHEME_V->tracing; |
3384 | |
3832 | |
3385 | SCHEME_V->tracing = ivalue (car (args)); |
3833 | SCHEME_V->tracing = ivalue_unchecked (car (args)); |
3386 | s_return (mk_integer (SCHEME_A_ tr)); |
3834 | s_return (mk_integer (SCHEME_A_ tr)); |
3387 | } |
3835 | } |
3388 | |
|
|
3389 | #endif |
3836 | #endif |
3390 | |
3837 | |
3391 | case OP_APPLY: /* apply 'code' to 'args' */ |
3838 | case OP_APPLY: /* apply 'code' to 'args' */ |
3392 | #if USE_TRACING |
3839 | #if USE_TRACING |
3393 | if (SCHEME_V->tracing) |
3840 | if (SCHEME_V->tracing) |
… | |
… | |
3407 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3854 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3408 | else if (is_foreign (SCHEME_V->code)) |
3855 | else if (is_foreign (SCHEME_V->code)) |
3409 | { |
3856 | { |
3410 | /* Keep nested calls from GC'ing the arglist */ |
3857 | /* Keep nested calls from GC'ing the arglist */ |
3411 | push_recent_alloc (SCHEME_A_ args, NIL); |
3858 | push_recent_alloc (SCHEME_A_ args, NIL); |
3412 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3859 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3413 | |
3860 | |
3414 | s_return (x); |
3861 | s_return (x); |
3415 | } |
3862 | } |
3416 | 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 */ |
3417 | { |
3864 | { |
… | |
… | |
3447 | else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ |
3894 | else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ |
3448 | { |
3895 | { |
3449 | ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); |
3896 | ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); |
3450 | s_return (args != NIL ? car (args) : NIL); |
3897 | s_return (args != NIL ? car (args) : NIL); |
3451 | } |
3898 | } |
3452 | else |
3899 | |
3453 | Error_0 ("illegal function"); |
3900 | Error_0 ("illegal function"); |
3454 | |
3901 | |
3455 | case OP_DOMACRO: /* do macro */ |
3902 | case OP_DOMACRO: /* do macro */ |
3456 | SCHEME_V->code = SCHEME_V->value; |
3903 | SCHEME_V->code = SCHEME_V->value; |
3457 | s_goto (OP_EVAL); |
3904 | s_goto (OP_EVAL); |
3458 | |
|
|
3459 | #if 1 |
|
|
3460 | |
3905 | |
3461 | case OP_LAMBDA: /* lambda */ |
3906 | case OP_LAMBDA: /* lambda */ |
3462 | /* 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 |
3463 | set SCHEME_V->value fall thru */ |
3908 | set SCHEME_V->value fall thru */ |
3464 | { |
3909 | { |
… | |
… | |
3471 | SCHEME_V->code = slot_value_in_env (f); |
3916 | SCHEME_V->code = slot_value_in_env (f); |
3472 | s_goto (OP_APPLY); |
3917 | s_goto (OP_APPLY); |
3473 | } |
3918 | } |
3474 | |
3919 | |
3475 | SCHEME_V->value = SCHEME_V->code; |
3920 | SCHEME_V->value = SCHEME_V->code; |
3476 | /* Fallthru */ |
|
|
3477 | } |
3921 | } |
|
|
3922 | /* Fallthru */ |
3478 | |
3923 | |
3479 | case OP_LAMBDA1: |
3924 | case OP_LAMBDA1: |
3480 | 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)); |
3481 | |
|
|
3482 | #else |
|
|
3483 | |
|
|
3484 | case OP_LAMBDA: /* lambda */ |
|
|
3485 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
|
|
3486 | |
|
|
3487 | #endif |
|
|
3488 | |
3926 | |
3489 | case OP_MKCLOSURE: /* make-closure */ |
3927 | case OP_MKCLOSURE: /* make-closure */ |
3490 | x = car (args); |
3928 | x = car (args); |
3491 | |
3929 | |
3492 | if (car (x) == SCHEME_V->LAMBDA) |
3930 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
3531 | else |
3969 | else |
3532 | 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); |
3533 | |
3971 | |
3534 | s_return (SCHEME_V->code); |
3972 | s_return (SCHEME_V->code); |
3535 | |
3973 | |
3536 | |
|
|
3537 | case OP_DEFP: /* defined? */ |
3974 | case OP_DEFP: /* defined? */ |
3538 | x = SCHEME_V->envir; |
3975 | x = SCHEME_V->envir; |
3539 | |
3976 | |
3540 | if (cdr (args) != NIL) |
3977 | if (cdr (args) != NIL) |
3541 | x = cadr (args); |
3978 | x = cadr (args); |
… | |
… | |
3559 | s_return (SCHEME_V->value); |
3996 | s_return (SCHEME_V->value); |
3560 | } |
3997 | } |
3561 | else |
3998 | else |
3562 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3999 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3563 | |
4000 | |
3564 | |
|
|
3565 | case OP_BEGIN: /* begin */ |
4001 | case OP_BEGIN: /* begin */ |
3566 | if (!is_pair (SCHEME_V->code)) |
4002 | if (!is_pair (SCHEME_V->code)) |
3567 | s_return (SCHEME_V->code); |
4003 | s_return (SCHEME_V->code); |
3568 | |
4004 | |
3569 | if (cdr (SCHEME_V->code) != NIL) |
4005 | if (cdr (SCHEME_V->code) != NIL) |
… | |
… | |
3580 | case OP_IF1: /* if */ |
4016 | case OP_IF1: /* if */ |
3581 | if (is_true (SCHEME_V->value)) |
4017 | if (is_true (SCHEME_V->value)) |
3582 | SCHEME_V->code = car (SCHEME_V->code); |
4018 | SCHEME_V->code = car (SCHEME_V->code); |
3583 | else |
4019 | else |
3584 | 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 | |
3585 | s_goto (OP_EVAL); |
4022 | s_goto (OP_EVAL); |
3586 | |
4023 | |
3587 | case OP_LET0: /* let */ |
4024 | case OP_LET0: /* let */ |
3588 | SCHEME_V->args = NIL; |
4025 | SCHEME_V->args = NIL; |
3589 | SCHEME_V->value = SCHEME_V->code; |
4026 | SCHEME_V->value = SCHEME_V->code; |
3590 | 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); |
3591 | s_goto (OP_LET1); |
4028 | s_goto (OP_LET1); |
3592 | |
4029 | |
3593 | case OP_LET1: /* let (calculate parameters) */ |
4030 | case OP_LET1: /* let (calculate parameters) */ |
|
|
4031 | case OP_LET1REC: /* letrec (calculate parameters) */ |
3594 | args = cons (SCHEME_V->value, args); |
4032 | args = cons (SCHEME_V->value, args); |
3595 | |
4033 | |
3596 | if (is_pair (SCHEME_V->code)) /* continue */ |
4034 | if (is_pair (SCHEME_V->code)) /* continue */ |
3597 | { |
4035 | { |
3598 | 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))) |
3599 | 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)); |
3600 | |
4038 | |
3601 | s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code)); |
4039 | s_save (SCHEME_A_ op, args, cdr (SCHEME_V->code)); |
3602 | SCHEME_V->code = cadar (SCHEME_V->code); |
4040 | SCHEME_V->code = cadar (SCHEME_V->code); |
3603 | SCHEME_V->args = NIL; |
4041 | SCHEME_V->args = NIL; |
3604 | s_goto (OP_EVAL); |
4042 | s_goto (OP_EVAL); |
3605 | } |
4043 | } |
3606 | else /* end */ |
4044 | |
3607 | { |
4045 | /* end */ |
3608 | args = reverse_in_place (SCHEME_A_ NIL, args); |
4046 | args = reverse_in_place (SCHEME_A_ NIL, args); |
3609 | SCHEME_V->code = car (args); |
4047 | SCHEME_V->code = car (args); |
3610 | SCHEME_V->args = cdr (args); |
4048 | SCHEME_V->args = cdr (args); |
3611 | s_goto (OP_LET2); |
4049 | s_goto (op == OP_LET1 ? OP_LET2 : OP_LET2REC); |
3612 | } |
|
|
3613 | |
4050 | |
3614 | case OP_LET2: /* let */ |
4051 | case OP_LET2: /* let */ |
3615 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
4052 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3616 | |
4053 | |
3617 | 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; |
… | |
… | |
3621 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
4058 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
3622 | { |
4059 | { |
3623 | 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)) |
3624 | { |
4061 | { |
3625 | if (!is_pair (x)) |
4062 | if (!is_pair (x)) |
3626 | Error_1 ("Bad syntax of binding in let :", x); |
4063 | Error_1 ("Bad syntax of binding in let:", x); |
3627 | |
4064 | |
3628 | if (!is_list (SCHEME_A_ car (x))) |
4065 | if (!is_list (SCHEME_A_ car (x))) |
3629 | Error_1 ("Bad syntax of binding in let :", car (x)); |
4066 | Error_1 ("Bad syntax of binding in let:", car (x)); |
3630 | |
4067 | |
3631 | args = cons (caar (x), args); |
4068 | args = cons (caar (x), args); |
3632 | } |
4069 | } |
3633 | |
4070 | |
3634 | 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)), |
… | |
… | |
3651 | SCHEME_V->code = cdr (SCHEME_V->code); |
4088 | SCHEME_V->code = cdr (SCHEME_V->code); |
3652 | s_goto (OP_BEGIN); |
4089 | s_goto (OP_BEGIN); |
3653 | } |
4090 | } |
3654 | |
4091 | |
3655 | 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))) |
3656 | 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)); |
3657 | |
4094 | |
3658 | 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)); |
3659 | SCHEME_V->code = car (cdaar (SCHEME_V->code)); |
4096 | SCHEME_V->code = car (cdaar (SCHEME_V->code)); |
3660 | s_goto (OP_EVAL); |
4097 | s_goto (OP_EVAL); |
3661 | |
4098 | |
… | |
… | |
3672 | s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); |
4109 | s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); |
3673 | SCHEME_V->code = cadar (SCHEME_V->code); |
4110 | SCHEME_V->code = cadar (SCHEME_V->code); |
3674 | SCHEME_V->args = NIL; |
4111 | SCHEME_V->args = NIL; |
3675 | s_goto (OP_EVAL); |
4112 | s_goto (OP_EVAL); |
3676 | } |
4113 | } |
3677 | else /* end */ |
4114 | |
|
|
4115 | /* end */ |
3678 | { |
4116 | |
3679 | SCHEME_V->code = args; |
4117 | SCHEME_V->code = args; |
3680 | SCHEME_V->args = NIL; |
4118 | SCHEME_V->args = NIL; |
3681 | s_goto (OP_BEGIN); |
4119 | s_goto (OP_BEGIN); |
3682 | } |
|
|
3683 | |
4120 | |
3684 | case OP_LET0REC: /* letrec */ |
4121 | case OP_LET0REC: /* letrec */ |
3685 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
4122 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3686 | SCHEME_V->args = NIL; |
4123 | SCHEME_V->args = NIL; |
3687 | SCHEME_V->value = SCHEME_V->code; |
4124 | SCHEME_V->value = SCHEME_V->code; |
3688 | SCHEME_V->code = car (SCHEME_V->code); |
4125 | SCHEME_V->code = car (SCHEME_V->code); |
3689 | s_goto (OP_LET1REC); |
4126 | s_goto (OP_LET1REC); |
3690 | |
4127 | |
3691 | case OP_LET1REC: /* letrec (calculate parameters) */ |
4128 | /* OP_LET1REC handled by OP_LET1 */ |
3692 | args = cons (SCHEME_V->value, args); |
|
|
3693 | |
|
|
3694 | if (is_pair (SCHEME_V->code)) /* continue */ |
|
|
3695 | { |
|
|
3696 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
|
|
3697 | Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); |
|
|
3698 | |
|
|
3699 | s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code)); |
|
|
3700 | SCHEME_V->code = cadar (SCHEME_V->code); |
|
|
3701 | SCHEME_V->args = NIL; |
|
|
3702 | s_goto (OP_EVAL); |
|
|
3703 | } |
|
|
3704 | else /* end */ |
|
|
3705 | { |
|
|
3706 | args = reverse_in_place (SCHEME_A_ NIL, args); |
|
|
3707 | SCHEME_V->code = car (args); |
|
|
3708 | SCHEME_V->args = cdr (args); |
|
|
3709 | s_goto (OP_LET2REC); |
|
|
3710 | } |
|
|
3711 | |
4129 | |
3712 | case OP_LET2REC: /* letrec */ |
4130 | case OP_LET2REC: /* letrec */ |
3713 | 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)) |
3714 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
4132 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3715 | |
4133 | |
… | |
… | |
3745 | } |
4163 | } |
3746 | else |
4164 | else |
3747 | { |
4165 | { |
3748 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
4166 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3749 | s_return (NIL); |
4167 | s_return (NIL); |
3750 | else |
4168 | |
3751 | { |
|
|
3752 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
4169 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3753 | SCHEME_V->code = caar (SCHEME_V->code); |
4170 | SCHEME_V->code = caar (SCHEME_V->code); |
3754 | s_goto (OP_EVAL); |
4171 | s_goto (OP_EVAL); |
3755 | } |
|
|
3756 | } |
4172 | } |
3757 | |
4173 | |
3758 | case OP_DELAY: /* delay */ |
4174 | case OP_DELAY: /* delay */ |
3759 | 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); |
3760 | set_typeflag (x, T_PROMISE); |
4176 | set_typeflag (x, T_PROMISE); |
… | |
… | |
3771 | case OP_AND1: /* and */ |
4187 | case OP_AND1: /* and */ |
3772 | if (is_false (SCHEME_V->value)) |
4188 | if (is_false (SCHEME_V->value)) |
3773 | s_return (SCHEME_V->value); |
4189 | s_return (SCHEME_V->value); |
3774 | else if (SCHEME_V->code == NIL) |
4190 | else if (SCHEME_V->code == NIL) |
3775 | s_return (SCHEME_V->value); |
4191 | s_return (SCHEME_V->value); |
3776 | else |
4192 | |
3777 | { |
|
|
3778 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
4193 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3779 | SCHEME_V->code = car (SCHEME_V->code); |
4194 | SCHEME_V->code = car (SCHEME_V->code); |
3780 | s_goto (OP_EVAL); |
4195 | s_goto (OP_EVAL); |
3781 | } |
|
|
3782 | |
4196 | |
3783 | case OP_OR0: /* or */ |
4197 | case OP_OR0: /* or */ |
3784 | if (SCHEME_V->code == NIL) |
4198 | if (SCHEME_V->code == NIL) |
3785 | s_return (S_F); |
4199 | s_return (S_F); |
3786 | |
4200 | |
… | |
… | |
3791 | case OP_OR1: /* or */ |
4205 | case OP_OR1: /* or */ |
3792 | if (is_true (SCHEME_V->value)) |
4206 | if (is_true (SCHEME_V->value)) |
3793 | s_return (SCHEME_V->value); |
4207 | s_return (SCHEME_V->value); |
3794 | else if (SCHEME_V->code == NIL) |
4208 | else if (SCHEME_V->code == NIL) |
3795 | s_return (SCHEME_V->value); |
4209 | s_return (SCHEME_V->value); |
3796 | else |
4210 | |
3797 | { |
|
|
3798 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
4211 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3799 | SCHEME_V->code = car (SCHEME_V->code); |
4212 | SCHEME_V->code = car (SCHEME_V->code); |
3800 | s_goto (OP_EVAL); |
4213 | s_goto (OP_EVAL); |
3801 | } |
|
|
3802 | |
4214 | |
3803 | case OP_C0STREAM: /* cons-stream */ |
4215 | case OP_C0STREAM: /* cons-stream */ |
3804 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
4216 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3805 | SCHEME_V->code = car (SCHEME_V->code); |
4217 | SCHEME_V->code = car (SCHEME_V->code); |
3806 | s_goto (OP_EVAL); |
4218 | s_goto (OP_EVAL); |
… | |
… | |
3871 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
4283 | s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); |
3872 | SCHEME_V->code = caar (x); |
4284 | SCHEME_V->code = caar (x); |
3873 | s_goto (OP_EVAL); |
4285 | s_goto (OP_EVAL); |
3874 | } |
4286 | } |
3875 | } |
4287 | } |
3876 | else |
4288 | |
3877 | s_return (NIL); |
4289 | s_return (NIL); |
3878 | |
4290 | |
3879 | case OP_CASE2: /* case */ |
4291 | case OP_CASE2: /* case */ |
3880 | if (is_true (SCHEME_V->value)) |
4292 | if (is_true (SCHEME_V->value)) |
3881 | s_goto (OP_BEGIN); |
4293 | s_goto (OP_BEGIN); |
3882 | else |
4294 | |
3883 | s_return (NIL); |
4295 | s_return (NIL); |
3884 | |
4296 | |
3885 | case OP_PAPPLY: /* apply */ |
4297 | case OP_PAPPLY: /* apply */ |
3886 | SCHEME_V->code = car (args); |
4298 | SCHEME_V->code = car (args); |
3887 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
4299 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
3888 | /*SCHEME_V->args = cadr(args); */ |
4300 | /*SCHEME_V->args = cadr(args); */ |
… | |
… | |
3902 | } |
4314 | } |
3903 | |
4315 | |
3904 | if (USE_ERROR_CHECKING) abort (); |
4316 | if (USE_ERROR_CHECKING) abort (); |
3905 | } |
4317 | } |
3906 | |
4318 | |
3907 | static int |
4319 | /* math, cxr */ |
|
|
4320 | ecb_hot static int |
3908 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4321 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
3909 | { |
4322 | { |
3910 | pointer args = SCHEME_V->args; |
4323 | pointer args = SCHEME_V->args; |
3911 | pointer x = car (args); |
4324 | pointer x = car (args); |
3912 | num v; |
4325 | num v; |
3913 | |
4326 | |
3914 | #if USE_MATH |
|
|
3915 | RVALUE dd; |
|
|
3916 | #endif |
|
|
3917 | |
|
|
3918 | switch (op) |
4327 | switch (op) |
3919 | { |
4328 | { |
3920 | #if USE_MATH |
4329 | #if USE_MATH |
3921 | case OP_INEX2EX: /* inexact->exact */ |
4330 | case OP_INEX2EX: /* inexact->exact */ |
3922 | if (is_integer (x)) |
4331 | if (!is_integer (x)) |
3923 | s_return (x); |
4332 | { |
3924 | else if (modf (rvalue_unchecked (x), &dd) == 0) |
4333 | RVALUE r = rvalue_unchecked (x); |
3925 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4334 | |
|
|
4335 | if (r == (RVALUE)(IVALUE)r) |
|
|
4336 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
3926 | else |
4337 | else |
3927 | Error_1 ("inexact->exact: not integral:", x); |
4338 | Error_1 ("inexact->exact: not integral:", x); |
|
|
4339 | } |
3928 | |
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)))); |
3929 | 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)))); |
3930 | 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)))))); |
3931 | 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)))); |
3932 | 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)))); |
3933 | 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)))); |
3934 | 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)))); |
3935 | 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)))); |
3936 | |
4357 | |
3937 | case OP_ATAN: |
4358 | case OP_ATAN: |
|
|
4359 | s_return (mk_real (SCHEME_A_ |
3938 | if (cdr (args) == NIL) |
4360 | cdr (args) == NIL |
3939 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4361 | ? atan (rvalue (x)) |
3940 | else |
4362 | : atan2 (rvalue (x), rvalue (cadr (args))))); |
3941 | { |
|
|
3942 | pointer y = cadr (args); |
|
|
3943 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
|
|
3944 | } |
|
|
3945 | |
|
|
3946 | case OP_SQRT: |
|
|
3947 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
|
|
3948 | |
4363 | |
3949 | case OP_EXPT: |
4364 | case OP_EXPT: |
3950 | { |
4365 | { |
3951 | RVALUE result; |
4366 | RVALUE result; |
3952 | int real_result = 1; |
4367 | int real_result = 1; |
… | |
… | |
3966 | /* If the test fails, result is too big for integer. */ |
4381 | /* If the test fails, result is too big for integer. */ |
3967 | if (!real_result) |
4382 | if (!real_result) |
3968 | { |
4383 | { |
3969 | long result_as_long = result; |
4384 | long result_as_long = result; |
3970 | |
4385 | |
3971 | if (result != (RVALUE) result_as_long) |
4386 | if (result != result_as_long) |
3972 | real_result = 1; |
4387 | real_result = 1; |
3973 | } |
4388 | } |
3974 | |
4389 | |
3975 | if (real_result) |
4390 | if (real_result) |
3976 | s_return (mk_real (SCHEME_A_ result)); |
4391 | s_return (mk_real (SCHEME_A_ result)); |
3977 | else |
4392 | else |
3978 | s_return (mk_integer (SCHEME_A_ result)); |
4393 | s_return (mk_integer (SCHEME_A_ result)); |
3979 | } |
4394 | } |
3980 | |
|
|
3981 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
3982 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
3983 | |
|
|
3984 | case OP_TRUNCATE: |
|
|
3985 | { |
|
|
3986 | RVALUE rvalue_of_x; |
|
|
3987 | |
|
|
3988 | rvalue_of_x = rvalue (x); |
|
|
3989 | |
|
|
3990 | if (rvalue_of_x > 0) |
|
|
3991 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
|
|
3992 | else |
|
|
3993 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
|
|
3994 | } |
|
|
3995 | |
|
|
3996 | case OP_ROUND: |
|
|
3997 | if (num_is_integer (x)) |
|
|
3998 | s_return (x); |
|
|
3999 | |
|
|
4000 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
|
|
4001 | #endif |
4395 | #endif |
4002 | |
4396 | |
4003 | case OP_ADD: /* + */ |
4397 | case OP_ADD: /* + */ |
4004 | v = num_zero; |
4398 | v = num_zero; |
4005 | |
4399 | |
… | |
… | |
4093 | else |
4487 | else |
4094 | Error_0 ("modulo: division by zero"); |
4488 | Error_0 ("modulo: division by zero"); |
4095 | |
4489 | |
4096 | s_return (mk_number (SCHEME_A_ v)); |
4490 | s_return (mk_number (SCHEME_A_ v)); |
4097 | |
4491 | |
4098 | case OP_CAR: /* car */ |
4492 | /* the compiler will optimize this mess... */ |
4099 | s_return (caar (args)); |
4493 | case OP_CAR: op_car: s_return (car (x)); |
4100 | |
4494 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4101 | case OP_CDR: /* cdr */ |
4495 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4102 | 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; |
4103 | |
4523 | |
4104 | case OP_CONS: /* cons */ |
4524 | case OP_CONS: /* cons */ |
4105 | set_cdr (args, cadr (args)); |
4525 | set_cdr (args, cadr (args)); |
4106 | s_return (args); |
4526 | s_return (args); |
4107 | |
4527 | |
… | |
… | |
4122 | } |
4542 | } |
4123 | else |
4543 | else |
4124 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4544 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4125 | |
4545 | |
4126 | case OP_CHAR2INT: /* char->integer */ |
4546 | case OP_CHAR2INT: /* char->integer */ |
4127 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4547 | s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x))); |
4128 | |
4548 | |
4129 | case OP_INT2CHAR: /* integer->char */ |
4549 | case OP_INT2CHAR: /* integer->char */ |
4130 | s_return (mk_character (SCHEME_A_ ivalue (x))); |
4550 | s_return (mk_character (SCHEME_A_ ivalue_unchecked (x))); |
4131 | |
4551 | |
4132 | case OP_CHARUPCASE: |
4552 | case OP_CHARUPCASE: |
4133 | { |
4553 | { |
4134 | unsigned char c = ivalue (x); |
4554 | unsigned char c = ivalue_unchecked (x); |
4135 | c = toupper (c); |
4555 | c = toupper (c); |
4136 | s_return (mk_character (SCHEME_A_ c)); |
4556 | s_return (mk_character (SCHEME_A_ c)); |
4137 | } |
4557 | } |
4138 | |
4558 | |
4139 | case OP_CHARDNCASE: |
4559 | case OP_CHARDNCASE: |
4140 | { |
4560 | { |
4141 | unsigned char c = ivalue (x); |
4561 | unsigned char c = ivalue_unchecked (x); |
4142 | c = tolower (c); |
4562 | c = tolower (c); |
4143 | s_return (mk_character (SCHEME_A_ c)); |
4563 | s_return (mk_character (SCHEME_A_ c)); |
4144 | } |
4564 | } |
4145 | |
4565 | |
4146 | case OP_STR2SYM: /* string->symbol */ |
4566 | case OP_STR2SYM: /* string->symbol */ |
… | |
… | |
4223 | Error_1 ("atom->string: not an atom:", x); |
4643 | Error_1 ("atom->string: not an atom:", x); |
4224 | } |
4644 | } |
4225 | |
4645 | |
4226 | case OP_MKSTRING: /* make-string */ |
4646 | case OP_MKSTRING: /* make-string */ |
4227 | { |
4647 | { |
4228 | int fill = ' '; |
4648 | int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' '; |
4229 | int len; |
|
|
4230 | |
|
|
4231 | len = ivalue (x); |
4649 | int len = ivalue_unchecked (x); |
4232 | |
|
|
4233 | if (cdr (args) != NIL) |
|
|
4234 | fill = charvalue (cadr (args)); |
|
|
4235 | |
4650 | |
4236 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4651 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4237 | } |
4652 | } |
4238 | |
4653 | |
4239 | case OP_STRLEN: /* string-length */ |
4654 | case OP_STRLEN: /* string-length */ |
4240 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4655 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4241 | |
4656 | |
4242 | case OP_STRREF: /* string-ref */ |
4657 | case OP_STRREF: /* string-ref */ |
4243 | { |
4658 | { |
4244 | char *str; |
|
|
4245 | int index; |
|
|
4246 | |
|
|
4247 | str = strvalue (x); |
4659 | char *str = strvalue (x); |
4248 | |
|
|
4249 | index = ivalue (cadr (args)); |
4660 | int index = ivalue_unchecked (cadr (args)); |
4250 | |
4661 | |
4251 | if (index >= strlength (x)) |
4662 | if (index >= strlength (x)) |
4252 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4663 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4253 | |
4664 | |
4254 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4665 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4255 | } |
4666 | } |
4256 | |
4667 | |
4257 | case OP_STRSET: /* string-set! */ |
4668 | case OP_STRSET: /* string-set! */ |
4258 | { |
4669 | { |
4259 | char *str; |
4670 | char *str = strvalue (x); |
4260 | int index; |
4671 | int index = ivalue_unchecked (cadr (args)); |
4261 | int c; |
4672 | int c; |
4262 | |
4673 | |
4263 | if (is_immutable (x)) |
4674 | if (is_immutable (x)) |
4264 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4675 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4265 | |
|
|
4266 | str = strvalue (x); |
|
|
4267 | |
|
|
4268 | index = ivalue (cadr (args)); |
|
|
4269 | |
4676 | |
4270 | if (index >= strlength (x)) |
4677 | if (index >= strlength (x)) |
4271 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4678 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4272 | |
4679 | |
4273 | c = charvalue (caddr (args)); |
4680 | c = charvalue (caddr (args)); |
… | |
… | |
4294 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4701 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4295 | |
4702 | |
4296 | s_return (newstr); |
4703 | s_return (newstr); |
4297 | } |
4704 | } |
4298 | |
4705 | |
4299 | case OP_SUBSTR: /* substring */ |
4706 | case OP_STRING_COPY: /* substring/string-copy */ |
4300 | { |
4707 | { |
4301 | char *str; |
4708 | char *str = strvalue (x); |
4302 | int index0; |
4709 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4303 | int index1; |
4710 | int index1; |
4304 | int len; |
4711 | int len; |
4305 | |
4712 | |
4306 | str = strvalue (x); |
|
|
4307 | |
|
|
4308 | index0 = ivalue (cadr (args)); |
|
|
4309 | |
|
|
4310 | if (index0 > strlength (x)) |
4713 | if (index0 > strlength (x)) |
4311 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4714 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4312 | |
4715 | |
4313 | if (cddr (args) != NIL) |
4716 | if (cddr (args) != NIL) |
4314 | { |
4717 | { |
4315 | index1 = ivalue (caddr (args)); |
4718 | index1 = ivalue_unchecked (caddr (args)); |
4316 | |
4719 | |
4317 | if (index1 > strlength (x) || index1 < index0) |
4720 | if (index1 > strlength (x) || index1 < index0) |
4318 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4721 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4319 | } |
4722 | } |
4320 | else |
4723 | else |
4321 | index1 = strlength (x); |
4724 | index1 = strlength (x); |
4322 | |
4725 | |
4323 | len = index1 - index0; |
4726 | len = index1 - index0; |
4324 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4727 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4325 | memcpy (strvalue (x), str + index0, len); |
|
|
4326 | strvalue (x)[len] = 0; |
|
|
4327 | |
4728 | |
4328 | s_return (x); |
4729 | s_return (x); |
4329 | } |
4730 | } |
4330 | |
4731 | |
4331 | case OP_VECTOR: /* vector */ |
4732 | case OP_VECTOR: /* vector */ |
… | |
… | |
4343 | if (SCHEME_V->no_memory) |
4744 | if (SCHEME_V->no_memory) |
4344 | s_return (S_SINK); |
4745 | s_return (S_SINK); |
4345 | #endif |
4746 | #endif |
4346 | |
4747 | |
4347 | 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++) |
4348 | set_vector_elem (vec, i, car (x)); |
4749 | vector_set (vec, i, car (x)); |
4349 | |
4750 | |
4350 | s_return (vec); |
4751 | s_return (vec); |
4351 | } |
4752 | } |
4352 | |
4753 | |
4353 | case OP_MKVECTOR: /* make-vector */ |
4754 | case OP_MKVECTOR: /* make-vector */ |
4354 | { |
4755 | { |
4355 | pointer fill = NIL; |
4756 | pointer fill = NIL; |
4356 | int len; |
|
|
4357 | pointer vec; |
4757 | pointer vec; |
4358 | |
|
|
4359 | len = ivalue (x); |
4758 | int len = ivalue_unchecked (x); |
4360 | |
4759 | |
4361 | if (cdr (args) != NIL) |
4760 | if (cdr (args) != NIL) |
4362 | fill = cadr (args); |
4761 | fill = cadr (args); |
4363 | |
4762 | |
4364 | vec = mk_vector (SCHEME_A_ len); |
4763 | vec = mk_vector (SCHEME_A_ len); |
… | |
… | |
4367 | if (SCHEME_V->no_memory) |
4766 | if (SCHEME_V->no_memory) |
4368 | s_return (S_SINK); |
4767 | s_return (S_SINK); |
4369 | #endif |
4768 | #endif |
4370 | |
4769 | |
4371 | if (fill != NIL) |
4770 | if (fill != NIL) |
4372 | fill_vector (vec, fill); |
4771 | fill_vector (vec, 0, fill); |
4373 | |
4772 | |
4374 | s_return (vec); |
4773 | s_return (vec); |
4375 | } |
4774 | } |
4376 | |
4775 | |
4377 | case OP_VECLEN: /* vector-length */ |
4776 | case OP_VECLEN: /* vector-length */ |
4378 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4777 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4379 | |
4778 | |
|
|
4779 | case OP_VECRESIZE: |
|
|
4780 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4781 | s_return (x); |
|
|
4782 | |
4380 | case OP_VECREF: /* vector-ref */ |
4783 | case OP_VECREF: /* vector-ref */ |
4381 | { |
4784 | { |
4382 | int index; |
|
|
4383 | |
|
|
4384 | index = ivalue (cadr (args)); |
4785 | int index = ivalue_unchecked (cadr (args)); |
4385 | |
4786 | |
4386 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4787 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4387 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4788 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4388 | |
4789 | |
4389 | s_return (vector_elem (x, index)); |
4790 | s_return (vector_get (x, index)); |
4390 | } |
4791 | } |
4391 | |
4792 | |
4392 | case OP_VECSET: /* vector-set! */ |
4793 | case OP_VECSET: /* vector-set! */ |
4393 | { |
4794 | { |
4394 | int index; |
4795 | int index = ivalue_unchecked (cadr (args)); |
4395 | |
4796 | |
4396 | if (is_immutable (x)) |
4797 | if (is_immutable (x)) |
4397 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4798 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4398 | |
4799 | |
4399 | index = ivalue (cadr (args)); |
|
|
4400 | |
|
|
4401 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4800 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4402 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4801 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4403 | |
4802 | |
4404 | set_vector_elem (x, index, caddr (args)); |
4803 | vector_set (x, index, caddr (args)); |
4405 | s_return (x); |
4804 | s_return (x); |
4406 | } |
4805 | } |
4407 | } |
4806 | } |
4408 | |
4807 | |
4409 | if (USE_ERROR_CHECKING) abort (); |
4808 | if (USE_ERROR_CHECKING) abort (); |
4410 | } |
4809 | } |
4411 | |
4810 | |
4412 | INTERFACE int |
4811 | /* relational ops */ |
4413 | is_list (SCHEME_P_ pointer a) |
4812 | ecb_hot static int |
4414 | { |
|
|
4415 | return list_length (SCHEME_A_ a) >= 0; |
|
|
4416 | } |
|
|
4417 | |
|
|
4418 | /* Result is: |
|
|
4419 | proper list: length |
|
|
4420 | circular list: -1 |
|
|
4421 | not even a pair: -2 |
|
|
4422 | dotted list: -2 minus length before dot |
|
|
4423 | */ |
|
|
4424 | INTERFACE int |
|
|
4425 | list_length (SCHEME_P_ pointer a) |
|
|
4426 | { |
|
|
4427 | int i = 0; |
|
|
4428 | pointer slow, fast; |
|
|
4429 | |
|
|
4430 | slow = fast = a; |
|
|
4431 | |
|
|
4432 | while (1) |
|
|
4433 | { |
|
|
4434 | if (fast == NIL) |
|
|
4435 | return i; |
|
|
4436 | |
|
|
4437 | if (!is_pair (fast)) |
|
|
4438 | return -2 - i; |
|
|
4439 | |
|
|
4440 | fast = cdr (fast); |
|
|
4441 | ++i; |
|
|
4442 | |
|
|
4443 | if (fast == NIL) |
|
|
4444 | return i; |
|
|
4445 | |
|
|
4446 | if (!is_pair (fast)) |
|
|
4447 | return -2 - i; |
|
|
4448 | |
|
|
4449 | ++i; |
|
|
4450 | fast = cdr (fast); |
|
|
4451 | |
|
|
4452 | /* Safe because we would have already returned if `fast' |
|
|
4453 | encountered a non-pair. */ |
|
|
4454 | slow = cdr (slow); |
|
|
4455 | |
|
|
4456 | if (fast == slow) |
|
|
4457 | { |
|
|
4458 | /* the fast pointer has looped back around and caught up |
|
|
4459 | with the slow pointer, hence the structure is circular, |
|
|
4460 | not of finite length, and therefore not a list */ |
|
|
4461 | return -1; |
|
|
4462 | } |
|
|
4463 | } |
|
|
4464 | } |
|
|
4465 | |
|
|
4466 | static int |
|
|
4467 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4813 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4468 | { |
4814 | { |
4469 | pointer x = SCHEME_V->args; |
4815 | pointer x = SCHEME_V->args; |
4470 | |
4816 | |
4471 | for (;;) |
4817 | for (;;) |
… | |
… | |
4492 | } |
4838 | } |
4493 | |
4839 | |
4494 | s_return (S_T); |
4840 | s_return (S_T); |
4495 | } |
4841 | } |
4496 | |
4842 | |
4497 | static int |
4843 | /* predicates */ |
|
|
4844 | ecb_hot static int |
4498 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4845 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4499 | { |
4846 | { |
4500 | pointer args = SCHEME_V->args; |
4847 | pointer args = SCHEME_V->args; |
4501 | pointer a = car (args); |
4848 | pointer a = car (args); |
4502 | pointer d = cdr (args); |
4849 | pointer d = cdr (args); |
4503 | int r; |
4850 | int r; |
4504 | |
4851 | |
4505 | switch (op) |
4852 | switch (op) |
4506 | { |
4853 | { |
4507 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4854 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4508 | 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; |
4509 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4856 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4510 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4857 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4511 | 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; |
4512 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4860 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4513 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4861 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4514 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4862 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4515 | 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 */ |
4516 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4864 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4517 | |
4865 | |
4518 | #if USE_CHAR_CLASSIFIERS |
4866 | #if USE_CHAR_CLASSIFIERS |
4519 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; |
4867 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4520 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; |
4868 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4521 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; |
4869 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4522 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; |
4870 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break; |
4523 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; |
4871 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break; |
4524 | #endif |
4872 | #endif |
4525 | |
4873 | |
4526 | #if USE_PORTS |
4874 | #if USE_PORTS |
4527 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4875 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4528 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
4876 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
… | |
… | |
4548 | } |
4896 | } |
4549 | |
4897 | |
4550 | s_retbool (r); |
4898 | s_retbool (r); |
4551 | } |
4899 | } |
4552 | |
4900 | |
4553 | static int |
4901 | /* promises, list ops, ports */ |
|
|
4902 | ecb_hot static int |
4554 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4903 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4555 | { |
4904 | { |
4556 | pointer args = SCHEME_V->args; |
4905 | pointer args = SCHEME_V->args; |
4557 | pointer a = car (args); |
4906 | pointer a = car (args); |
4558 | pointer x, y; |
4907 | pointer x, y; |
… | |
… | |
4571 | } |
4920 | } |
4572 | else |
4921 | else |
4573 | s_return (SCHEME_V->code); |
4922 | s_return (SCHEME_V->code); |
4574 | |
4923 | |
4575 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4924 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4576 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4925 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4577 | s_return (SCHEME_V->value); |
4926 | s_return (SCHEME_V->value); |
4578 | |
4927 | |
4579 | #if USE_PORTS |
4928 | #if USE_PORTS |
|
|
4929 | |
|
|
4930 | case OP_EOF_OBJECT: /* eof-object */ |
|
|
4931 | s_return (S_EOF); |
4580 | |
4932 | |
4581 | case OP_WRITE: /* write */ |
4933 | case OP_WRITE: /* write */ |
4582 | case OP_DISPLAY: /* display */ |
4934 | case OP_DISPLAY: /* display */ |
4583 | case OP_WRITE_CHAR: /* write-char */ |
4935 | case OP_WRITE_CHAR: /* write-char */ |
4584 | if (is_pair (cdr (SCHEME_V->args))) |
4936 | if (is_pair (cdr (SCHEME_V->args))) |
… | |
… | |
4598 | else |
4950 | else |
4599 | SCHEME_V->print_flag = 0; |
4951 | SCHEME_V->print_flag = 0; |
4600 | |
4952 | |
4601 | s_goto (OP_P0LIST); |
4953 | s_goto (OP_P0LIST); |
4602 | |
4954 | |
|
|
4955 | //TODO: move to scheme |
4603 | case OP_NEWLINE: /* newline */ |
4956 | case OP_NEWLINE: /* newline */ |
4604 | if (is_pair (args)) |
4957 | if (is_pair (args)) |
4605 | { |
4958 | { |
4606 | if (a != SCHEME_V->outport) |
4959 | if (a != SCHEME_V->outport) |
4607 | { |
4960 | { |
… | |
… | |
4609 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4962 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4610 | SCHEME_V->outport = a; |
4963 | SCHEME_V->outport = a; |
4611 | } |
4964 | } |
4612 | } |
4965 | } |
4613 | |
4966 | |
4614 | putstr (SCHEME_A_ "\n"); |
4967 | putcharacter (SCHEME_A_ '\n'); |
4615 | s_return (S_T); |
4968 | s_return (S_T); |
4616 | #endif |
4969 | #endif |
4617 | |
4970 | |
4618 | case OP_ERR0: /* error */ |
4971 | case OP_ERR0: /* error */ |
4619 | SCHEME_V->retcode = -1; |
4972 | SCHEME_V->retcode = -1; |
… | |
… | |
4628 | putstr (SCHEME_A_ strvalue (car (args))); |
4981 | putstr (SCHEME_A_ strvalue (car (args))); |
4629 | SCHEME_V->args = cdr (args); |
4982 | SCHEME_V->args = cdr (args); |
4630 | s_goto (OP_ERR1); |
4983 | s_goto (OP_ERR1); |
4631 | |
4984 | |
4632 | case OP_ERR1: /* error */ |
4985 | case OP_ERR1: /* error */ |
4633 | putstr (SCHEME_A_ " "); |
4986 | putcharacter (SCHEME_A_ ' '); |
4634 | |
4987 | |
4635 | if (args != NIL) |
4988 | if (args != NIL) |
4636 | { |
4989 | { |
4637 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4990 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4638 | SCHEME_V->args = a; |
4991 | SCHEME_V->args = a; |
4639 | SCHEME_V->print_flag = 1; |
4992 | SCHEME_V->print_flag = 1; |
4640 | s_goto (OP_P0LIST); |
4993 | s_goto (OP_P0LIST); |
4641 | } |
4994 | } |
4642 | else |
4995 | else |
4643 | { |
4996 | { |
4644 | putstr (SCHEME_A_ "\n"); |
4997 | putcharacter (SCHEME_A_ '\n'); |
4645 | |
4998 | |
4646 | if (SCHEME_V->interactive_repl) |
4999 | if (SCHEME_V->interactive_repl) |
4647 | s_goto (OP_T0LVL); |
5000 | s_goto (OP_T0LVL); |
4648 | else |
5001 | else |
4649 | return -1; |
5002 | return -1; |
… | |
… | |
4726 | SCHEME_V->gc_verbose = (a != S_F); |
5079 | SCHEME_V->gc_verbose = (a != S_F); |
4727 | s_retbool (was); |
5080 | s_retbool (was); |
4728 | } |
5081 | } |
4729 | |
5082 | |
4730 | case OP_NEWSEGMENT: /* new-segment */ |
5083 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
5084 | #if 0 |
4731 | if (!is_pair (args) || !is_number (a)) |
5085 | if (!is_pair (args) || !is_number (a)) |
4732 | Error_0 ("new-segment: argument must be a number"); |
5086 | Error_0 ("new-segment: argument must be a number"); |
4733 | |
5087 | #endif |
4734 | alloc_cellseg (SCHEME_A_ (int)ivalue (a)); |
5088 | s_retbool (alloc_cellseg (SCHEME_A)); |
4735 | |
|
|
4736 | s_return (S_T); |
|
|
4737 | |
5089 | |
4738 | case OP_OBLIST: /* oblist */ |
5090 | case OP_OBLIST: /* oblist */ |
4739 | s_return (oblist_all_symbols (SCHEME_A)); |
5091 | s_return (oblist_all_symbols (SCHEME_A)); |
4740 | |
5092 | |
4741 | #if USE_PORTS |
5093 | #if USE_PORTS |
… | |
… | |
4811 | s_return (p == NIL ? S_F : p); |
5163 | s_return (p == NIL ? S_F : p); |
4812 | } |
5164 | } |
4813 | |
5165 | |
4814 | case OP_GET_OUTSTRING: /* get-output-string */ |
5166 | case OP_GET_OUTSTRING: /* get-output-string */ |
4815 | { |
5167 | { |
4816 | port *p; |
5168 | port *p = port (a); |
4817 | |
5169 | |
4818 | if ((p = a->object.port)->kind & port_string) |
5170 | if (p->kind & port_string) |
4819 | { |
5171 | { |
4820 | off_t size; |
5172 | off_t size; |
4821 | char *str; |
5173 | char *str; |
4822 | |
5174 | |
4823 | size = p->rep.string.curr - p->rep.string.start + 1; |
5175 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4858 | } |
5210 | } |
4859 | |
5211 | |
4860 | if (USE_ERROR_CHECKING) abort (); |
5212 | if (USE_ERROR_CHECKING) abort (); |
4861 | } |
5213 | } |
4862 | |
5214 | |
4863 | static int |
5215 | /* reading */ |
|
|
5216 | ecb_cold static int |
4864 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
5217 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4865 | { |
5218 | { |
4866 | pointer args = SCHEME_V->args; |
5219 | pointer args = SCHEME_V->args; |
4867 | pointer x; |
5220 | pointer x; |
4868 | |
5221 | |
… | |
… | |
4928 | int res; |
5281 | int res; |
4929 | |
5282 | |
4930 | if (is_pair (args)) |
5283 | if (is_pair (args)) |
4931 | p = car (args); |
5284 | p = car (args); |
4932 | |
5285 | |
4933 | res = p->object.port->kind & port_string; |
5286 | res = port (p)->kind & port_string; |
4934 | |
5287 | |
4935 | s_retbool (res); |
5288 | s_retbool (res); |
4936 | } |
5289 | } |
4937 | |
5290 | |
4938 | case OP_SET_INPORT: /* set-input-port */ |
5291 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
4947 | case OP_RDSEXPR: |
5300 | case OP_RDSEXPR: |
4948 | switch (SCHEME_V->tok) |
5301 | switch (SCHEME_V->tok) |
4949 | { |
5302 | { |
4950 | case TOK_EOF: |
5303 | case TOK_EOF: |
4951 | s_return (S_EOF); |
5304 | s_return (S_EOF); |
4952 | /* NOTREACHED */ |
|
|
4953 | |
5305 | |
4954 | case TOK_VEC: |
5306 | case TOK_VEC: |
4955 | s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); |
5307 | s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); |
4956 | /* fall through */ |
5308 | /* fall through */ |
4957 | |
5309 | |
… | |
… | |
4960 | |
5312 | |
4961 | if (SCHEME_V->tok == TOK_RPAREN) |
5313 | if (SCHEME_V->tok == TOK_RPAREN) |
4962 | s_return (NIL); |
5314 | s_return (NIL); |
4963 | else if (SCHEME_V->tok == TOK_DOT) |
5315 | else if (SCHEME_V->tok == TOK_DOT) |
4964 | Error_0 ("syntax error: illegal dot expression"); |
5316 | Error_0 ("syntax error: illegal dot expression"); |
4965 | else |
5317 | |
4966 | { |
|
|
4967 | SCHEME_V->nesting_stack[SCHEME_V->file_i]++; |
5318 | SCHEME_V->nesting_stack[SCHEME_V->file_i]++; |
4968 | s_save (SCHEME_A_ OP_RDLIST, NIL, NIL); |
5319 | s_save (SCHEME_A_ OP_RDLIST, NIL, NIL); |
4969 | s_goto (OP_RDSEXPR); |
5320 | s_goto (OP_RDSEXPR); |
4970 | } |
|
|
4971 | |
5321 | |
4972 | case TOK_QUOTE: |
5322 | case TOK_QUOTE: |
4973 | s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL); |
5323 | s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL); |
4974 | SCHEME_V->tok = token (SCHEME_A); |
5324 | SCHEME_V->tok = token (SCHEME_A); |
4975 | s_goto (OP_RDSEXPR); |
5325 | s_goto (OP_RDSEXPR); |
… | |
… | |
4981 | { |
5331 | { |
4982 | s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); |
5332 | s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); |
4983 | SCHEME_V->tok = TOK_LPAREN; |
5333 | SCHEME_V->tok = TOK_LPAREN; |
4984 | s_goto (OP_RDSEXPR); |
5334 | s_goto (OP_RDSEXPR); |
4985 | } |
5335 | } |
4986 | else |
5336 | |
4987 | s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); |
5337 | s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); |
4988 | |
|
|
4989 | s_goto (OP_RDSEXPR); |
5338 | s_goto (OP_RDSEXPR); |
4990 | |
5339 | |
4991 | case TOK_COMMA: |
5340 | case TOK_COMMA: |
4992 | s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); |
5341 | s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); |
4993 | SCHEME_V->tok = token (SCHEME_A); |
5342 | SCHEME_V->tok = token (SCHEME_A); |
… | |
… | |
4997 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5346 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
4998 | SCHEME_V->tok = token (SCHEME_A); |
5347 | SCHEME_V->tok = token (SCHEME_A); |
4999 | s_goto (OP_RDSEXPR); |
5348 | s_goto (OP_RDSEXPR); |
5000 | |
5349 | |
5001 | case TOK_ATOM: |
5350 | case TOK_ATOM: |
5002 | 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))); |
5003 | |
5362 | |
5004 | case TOK_DQUOTE: |
5363 | case TOK_DQUOTE: |
5005 | x = readstrexp (SCHEME_A); |
5364 | x = readstrexp (SCHEME_A_ '"'); |
5006 | |
5365 | |
5007 | if (x == S_F) |
5366 | if (x == S_F) |
5008 | Error_0 ("Error reading string"); |
5367 | Error_0 ("Error reading string"); |
5009 | |
5368 | |
5010 | setimmutable (x); |
5369 | setimmutable (x); |
… | |
… | |
5014 | { |
5373 | { |
5015 | 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); |
5016 | |
5375 | |
5017 | if (f == NIL) |
5376 | if (f == NIL) |
5018 | Error_0 ("undefined sharp expression"); |
5377 | Error_0 ("undefined sharp expression"); |
5019 | else |
5378 | |
5020 | { |
|
|
5021 | SCHEME_V->code = cons (slot_value_in_env (f), NIL); |
5379 | SCHEME_V->code = cons (slot_value_in_env (f), NIL); |
5022 | s_goto (OP_EVAL); |
5380 | s_goto (OP_EVAL); |
5023 | } |
|
|
5024 | } |
5381 | } |
5025 | |
5382 | |
5026 | case TOK_SHARP_CONST: |
5383 | case TOK_SHARP_CONST: |
5027 | 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) |
5028 | Error_0 ("undefined sharp expression"); |
5385 | Error_0 ("undefined sharp expression"); |
5029 | else |
5386 | |
5030 | s_return (x); |
5387 | s_return (x); |
5031 | |
5388 | |
5032 | default: |
5389 | default: |
5033 | Error_0 ("syntax error: illegal token"); |
5390 | Error_0 ("syntax error: illegal token"); |
5034 | } |
5391 | } |
5035 | |
5392 | |
… | |
… | |
5128 | pointer b = cdr (args); |
5485 | pointer b = cdr (args); |
5129 | int ok_abbr = ok_abbrev (b); |
5486 | int ok_abbr = ok_abbrev (b); |
5130 | SCHEME_V->args = car (b); |
5487 | SCHEME_V->args = car (b); |
5131 | |
5488 | |
5132 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5489 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5133 | putstr (SCHEME_A_ "'"); |
5490 | putcharacter (SCHEME_A_ '\''); |
5134 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5491 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
5135 | putstr (SCHEME_A_ "`"); |
5492 | putcharacter (SCHEME_A_ '`'); |
5136 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5493 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
5137 | putstr (SCHEME_A_ ","); |
5494 | putcharacter (SCHEME_A_ ','); |
5138 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5495 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
5139 | putstr (SCHEME_A_ ",@"); |
5496 | putstr (SCHEME_A_ ",@"); |
5140 | else |
5497 | else |
5141 | { |
5498 | { |
5142 | putstr (SCHEME_A_ "("); |
5499 | putcharacter (SCHEME_A_ '('); |
5143 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5500 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
5144 | SCHEME_V->args = a; |
5501 | SCHEME_V->args = a; |
5145 | } |
5502 | } |
5146 | |
5503 | |
5147 | s_goto (OP_P0LIST); |
5504 | s_goto (OP_P0LIST); |
… | |
… | |
5149 | |
5506 | |
5150 | case OP_P1LIST: |
5507 | case OP_P1LIST: |
5151 | if (is_pair (args)) |
5508 | if (is_pair (args)) |
5152 | { |
5509 | { |
5153 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5510 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5154 | putstr (SCHEME_A_ " "); |
5511 | putcharacter (SCHEME_A_ ' '); |
5155 | SCHEME_V->args = car (args); |
5512 | SCHEME_V->args = car (args); |
5156 | s_goto (OP_P0LIST); |
5513 | s_goto (OP_P0LIST); |
5157 | } |
5514 | } |
5158 | else if (is_vector (args)) |
5515 | else if (is_vector (args)) |
5159 | { |
5516 | { |
… | |
… | |
5167 | { |
5524 | { |
5168 | putstr (SCHEME_A_ " . "); |
5525 | putstr (SCHEME_A_ " . "); |
5169 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5526 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5170 | } |
5527 | } |
5171 | |
5528 | |
5172 | putstr (SCHEME_A_ ")"); |
5529 | putcharacter (SCHEME_A_ ')'); |
5173 | s_return (S_T); |
5530 | s_return (S_T); |
5174 | } |
5531 | } |
5175 | |
5532 | |
5176 | case OP_PVECFROM: |
5533 | case OP_PVECFROM: |
5177 | { |
5534 | { |
… | |
… | |
5179 | pointer vec = car (args); |
5536 | pointer vec = car (args); |
5180 | int len = veclength (vec); |
5537 | int len = veclength (vec); |
5181 | |
5538 | |
5182 | if (i == len) |
5539 | if (i == len) |
5183 | { |
5540 | { |
5184 | putstr (SCHEME_A_ ")"); |
5541 | putcharacter (SCHEME_A_ ')'); |
5185 | s_return (S_T); |
5542 | s_return (S_T); |
5186 | } |
5543 | } |
5187 | else |
5544 | else |
5188 | { |
5545 | { |
5189 | pointer elem = vector_elem (vec, i); |
5546 | pointer elem = vector_get (vec, i); |
5190 | |
5547 | |
5191 | ivalue_unchecked (cdr (args)) = i + 1; |
5548 | ivalue_unchecked (cdr (args)) = i + 1; |
5192 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5549 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5193 | SCHEME_V->args = elem; |
5550 | SCHEME_V->args = elem; |
5194 | |
5551 | |
5195 | if (i > 0) |
5552 | if (i > 0) |
5196 | putstr (SCHEME_A_ " "); |
5553 | putcharacter (SCHEME_A_ ' '); |
5197 | |
5554 | |
5198 | s_goto (OP_P0LIST); |
5555 | s_goto (OP_P0LIST); |
5199 | } |
5556 | } |
5200 | } |
5557 | } |
5201 | } |
5558 | } |
5202 | |
5559 | |
5203 | if (USE_ERROR_CHECKING) abort (); |
5560 | if (USE_ERROR_CHECKING) abort (); |
5204 | } |
5561 | } |
5205 | |
5562 | |
5206 | static int |
5563 | /* list ops */ |
|
|
5564 | ecb_hot static int |
5207 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5565 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5208 | { |
5566 | { |
5209 | pointer args = SCHEME_V->args; |
5567 | pointer args = SCHEME_V->args; |
5210 | pointer a = car (args); |
5568 | pointer a = car (args); |
5211 | pointer x, y; |
5569 | pointer x, y; |
… | |
… | |
5234 | break; |
5592 | break; |
5235 | } |
5593 | } |
5236 | |
5594 | |
5237 | if (is_pair (y)) |
5595 | if (is_pair (y)) |
5238 | s_return (car (y)); |
5596 | s_return (car (y)); |
5239 | else |
5597 | |
5240 | s_return (S_F); |
5598 | s_return (S_F); |
5241 | |
|
|
5242 | |
5599 | |
5243 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5600 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5244 | SCHEME_V->args = a; |
5601 | SCHEME_V->args = a; |
5245 | |
5602 | |
5246 | if (SCHEME_V->args == NIL) |
5603 | if (SCHEME_V->args == NIL) |
5247 | s_return (S_F); |
5604 | s_return (S_F); |
5248 | else if (is_closure (SCHEME_V->args)) |
5605 | else if (is_closure (SCHEME_V->args) || is_macro (SCHEME_V->args)) |
5249 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
5606 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
5250 | else if (is_macro (SCHEME_V->args)) |
5607 | |
5251 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
|
|
5252 | else |
|
|
5253 | s_return (S_F); |
5608 | s_return (S_F); |
5254 | |
5609 | |
5255 | case OP_CLOSUREP: /* closure? */ |
5610 | case OP_CLOSUREP: /* closure? */ |
5256 | /* |
5611 | /* |
5257 | * Note, macro object is also a closure. |
5612 | * Note, macro object is also a closure. |
5258 | * Therefore, (closure? <#MACRO>) ==> #t |
5613 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5614 | * (schmorp) well, obviously not, fix? TODO |
5259 | */ |
5615 | */ |
5260 | s_retbool (is_closure (a)); |
5616 | s_retbool (is_closure (a)); |
5261 | |
5617 | |
5262 | case OP_MACROP: /* macro? */ |
5618 | case OP_MACROP: /* macro? */ |
5263 | s_retbool (is_macro (a)); |
5619 | s_retbool (is_macro (a)); |
… | |
… | |
5268 | |
5624 | |
5269 | /* 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 */ |
5270 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5626 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5271 | |
5627 | |
5272 | typedef int (*test_predicate)(pointer); |
5628 | typedef int (*test_predicate)(pointer); |
5273 | static int |
5629 | |
|
|
5630 | ecb_hot static int |
5274 | is_any (pointer p) |
5631 | tst_any (pointer p) |
5275 | { |
5632 | { |
5276 | return 1; |
5633 | return 1; |
5277 | } |
5634 | } |
5278 | |
5635 | |
5279 | static int |
5636 | ecb_hot static int |
5280 | is_nonneg (pointer p) |
5637 | tst_inonneg (pointer p) |
5281 | { |
5638 | { |
5282 | return ivalue (p) >= 0 && is_integer (p); |
5639 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5283 | } |
5640 | } |
5284 | |
5641 | |
5285 | static int |
5642 | ecb_hot static int |
5286 | tst_is_list (pointer p) |
5643 | tst_is_list (SCHEME_P_ pointer p) |
5287 | { |
5644 | { |
5288 | return p == NIL || is_pair (p); |
5645 | return p == NIL || is_pair (p); |
5289 | } |
5646 | } |
5290 | |
5647 | |
5291 | /* Correspond carefully with following defines! */ |
5648 | /* Correspond carefully with following defines! */ |
5292 | static struct |
5649 | static struct |
5293 | { |
5650 | { |
5294 | test_predicate fct; |
5651 | test_predicate fct; |
5295 | const char *kind; |
5652 | const char *kind; |
5296 | } tests[] = |
5653 | } tests[] = { |
5297 | { |
|
|
5298 | { is_any, 0 }, |
5654 | { tst_any , 0 }, |
5299 | { is_string, "string" }, |
5655 | { is_string , "string" }, |
5300 | { is_symbol, "symbol" }, |
5656 | { is_symbol , "symbol" }, |
5301 | { is_port, "port" }, |
5657 | { is_port , "port" }, |
5302 | { is_inport, "input port" }, |
5658 | { is_inport , "input port" }, |
5303 | { is_outport, "output port" }, |
5659 | { is_outport , "output port" }, |
5304 | { is_environment, "environment" }, |
5660 | { is_environment, "environment" }, |
5305 | { is_pair, "pair" }, |
5661 | { is_pair , "pair" }, |
5306 | { tst_is_list, "pair or '()" }, |
5662 | { 0 , "pair or '()" }, |
5307 | { is_character, "character" }, |
5663 | { is_character , "character" }, |
5308 | { is_vector, "vector" }, |
5664 | { is_vector , "vector" }, |
5309 | { is_number, "number" }, |
5665 | { is_number , "number" }, |
5310 | { is_integer, "integer" }, |
5666 | { is_integer , "integer" }, |
5311 | { is_nonneg, "non-negative integer" } |
5667 | { tst_inonneg , "non-negative integer" } |
5312 | }; |
5668 | }; |
5313 | |
5669 | |
5314 | #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 */ |
5315 | #define TST_ANY "\001" |
5671 | #define TST_ANY "\001" |
5316 | #define TST_STRING "\002" |
5672 | #define TST_STRING "\002" |
… | |
… | |
5334 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5690 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5335 | #include "opdefines.h" |
5691 | #include "opdefines.h" |
5336 | #undef OP_DEF |
5692 | #undef OP_DEF |
5337 | ; |
5693 | ; |
5338 | |
5694 | |
5339 | static const char * |
5695 | ecb_cold static const char * |
5340 | opname (int idx) |
5696 | opname (int idx) |
5341 | { |
5697 | { |
5342 | const char *name = opnames; |
5698 | const char *name = opnames; |
5343 | |
5699 | |
5344 | /* 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? */ |
… | |
… | |
5346 | name += strlen (name) + 1; |
5702 | name += strlen (name) + 1; |
5347 | |
5703 | |
5348 | return *name ? name : "ILLEGAL"; |
5704 | return *name ? name : "ILLEGAL"; |
5349 | } |
5705 | } |
5350 | |
5706 | |
5351 | static const char * |
5707 | ecb_cold static const char * |
5352 | procname (pointer x) |
5708 | procname (pointer x) |
5353 | { |
5709 | { |
5354 | return opname (procnum (x)); |
5710 | return opname (procnum (x)); |
5355 | } |
5711 | } |
5356 | |
5712 | |
5357 | typedef struct |
5713 | typedef struct |
5358 | { |
5714 | { |
5359 | uint8_t func; |
5715 | uint8_t func; |
5360 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
5716 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
5361 | uint8_t builtin; |
5717 | uint8_t builtin; |
|
|
5718 | #if USE_ERROR_CHECKING |
5362 | uint8_t min_arity; |
5719 | uint8_t min_arity; |
5363 | uint8_t max_arity; |
5720 | uint8_t max_arity; |
5364 | char arg_tests_encoding[3]; |
5721 | char arg_tests_encoding[3]; |
|
|
5722 | #endif |
5365 | } op_code_info; |
5723 | } op_code_info; |
5366 | |
5724 | |
5367 | static const op_code_info dispatch_table[] = { |
5725 | static const op_code_info dispatch_table[] = { |
|
|
5726 | #if USE_ERROR_CHECKING |
5368 | #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 |
5369 | #include "opdefines.h" |
5731 | #include "opdefines.h" |
5370 | #undef OP_DEF |
5732 | #undef OP_DEF |
5371 | {0} |
5733 | {0} |
5372 | }; |
5734 | }; |
5373 | |
5735 | |
5374 | /* kernel of this interpreter */ |
5736 | /* kernel of this interpreter */ |
5375 | static void ecb_hot |
5737 | ecb_hot static void |
5376 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5738 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5377 | { |
5739 | { |
5378 | SCHEME_V->op = op; |
5740 | SCHEME_V->op = op; |
5379 | |
5741 | |
5380 | for (;;) |
5742 | for (;;) |
… | |
… | |
5415 | { |
5777 | { |
5416 | pointer arg = car (arglist); |
5778 | pointer arg = car (arglist); |
5417 | |
5779 | |
5418 | j = t[0]; |
5780 | j = t[0]; |
5419 | |
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 | { |
5420 | if (!tests[j - 1].fct (arg)) |
5790 | if (!tests[j - 1].fct (arg)) |
5421 | break; |
5791 | break; |
|
|
5792 | } |
5422 | |
5793 | |
5423 | 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 */ |
5424 | t++; |
5795 | t++; |
5425 | |
5796 | |
5426 | arglist = cdr (arglist); |
5797 | arglist = cdr (arglist); |
5427 | i++; |
5798 | i++; |
5428 | } |
5799 | } |
… | |
… | |
5454 | 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)) |
5455 | return; |
5826 | return; |
5456 | |
5827 | |
5457 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5828 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5458 | { |
5829 | { |
5459 | xwrstr ("No memory!\n"); |
5830 | putstr (SCHEME_A_ "No memory!\n"); |
5460 | return; |
5831 | return; |
5461 | } |
5832 | } |
5462 | } |
5833 | } |
5463 | } |
5834 | } |
5464 | |
5835 | |
5465 | /* ========== Initialization of internal keywords ========== */ |
5836 | /* ========== Initialization of internal keywords ========== */ |
5466 | |
5837 | |
5467 | static void |
5838 | ecb_cold static void |
5468 | assign_syntax (SCHEME_P_ const char *name) |
5839 | assign_syntax (SCHEME_P_ const char *name) |
5469 | { |
5840 | { |
5470 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5841 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5471 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5842 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5472 | } |
5843 | } |
5473 | |
5844 | |
5474 | static void |
5845 | ecb_cold static void |
5475 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5846 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5476 | { |
5847 | { |
5477 | pointer x = mk_symbol (SCHEME_A_ name); |
5848 | pointer x = mk_symbol (SCHEME_A_ name); |
5478 | pointer y = mk_proc (SCHEME_A_ op); |
5849 | pointer y = mk_proc (SCHEME_A_ op); |
5479 | new_slot_in_env (SCHEME_A_ x, y); |
5850 | new_slot_in_env (SCHEME_A_ x, y); |
… | |
… | |
5483 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5854 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5484 | { |
5855 | { |
5485 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5856 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5486 | set_typeflag (y, (T_PROC | T_ATOM)); |
5857 | set_typeflag (y, (T_PROC | T_ATOM)); |
5487 | ivalue_unchecked (y) = op; |
5858 | ivalue_unchecked (y) = op; |
5488 | set_num_integer (y); |
|
|
5489 | return y; |
5859 | return y; |
5490 | } |
5860 | } |
5491 | |
5861 | |
5492 | /* 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! */ |
5493 | static int |
5863 | ecb_hot static int |
5494 | syntaxnum (pointer p) |
5864 | syntaxnum (pointer p) |
5495 | { |
5865 | { |
5496 | const char *s = strvalue (car (p)); |
5866 | const char *s = strvalue (p); |
5497 | |
5867 | |
5498 | switch (strlength (car (p))) |
5868 | switch (strlength (p)) |
5499 | { |
5869 | { |
5500 | case 2: |
5870 | case 2: |
5501 | if (s[0] == 'i') |
5871 | if (s[0] == 'i') |
5502 | return OP_IF0; /* if */ |
5872 | return OP_IF0; /* if */ |
5503 | else |
5873 | else |
… | |
… | |
5578 | ecb_cold int |
5948 | ecb_cold int |
5579 | scheme_init (SCHEME_P) |
5949 | scheme_init (SCHEME_P) |
5580 | { |
5950 | { |
5581 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5951 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5582 | 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)); |
5583 | |
5959 | |
5584 | num_set_fixnum (num_zero, 1); |
5960 | num_set_fixnum (num_zero, 1); |
5585 | num_set_ivalue (num_zero, 0); |
5961 | num_set_ivalue (num_zero, 0); |
5586 | num_set_fixnum (num_one, 1); |
5962 | num_set_fixnum (num_one, 1); |
5587 | num_set_ivalue (num_one, 1); |
5963 | num_set_ivalue (num_one, 1); |
… | |
… | |
5599 | SCHEME_V->save_inport = NIL; |
5975 | SCHEME_V->save_inport = NIL; |
5600 | SCHEME_V->loadport = NIL; |
5976 | SCHEME_V->loadport = NIL; |
5601 | SCHEME_V->nesting = 0; |
5977 | SCHEME_V->nesting = 0; |
5602 | SCHEME_V->interactive_repl = 0; |
5978 | SCHEME_V->interactive_repl = 0; |
5603 | |
5979 | |
5604 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5980 | if (!alloc_cellseg (SCHEME_A)) |
5605 | { |
5981 | { |
5606 | #if USE_ERROR_CHECKING |
5982 | #if USE_ERROR_CHECKING |
5607 | SCHEME_V->no_memory = 1; |
5983 | SCHEME_V->no_memory = 1; |
5608 | return 0; |
5984 | return 0; |
5609 | #endif |
5985 | #endif |
5610 | } |
5986 | } |
5611 | |
5987 | |
5612 | SCHEME_V->gc_verbose = 0; |
5988 | SCHEME_V->gc_verbose = 0; |
5613 | dump_stack_initialize (SCHEME_A); |
5989 | dump_stack_initialize (SCHEME_A); |
5614 | SCHEME_V->code = NIL; |
5990 | SCHEME_V->code = NIL; |
5615 | SCHEME_V->args = NIL; |
5991 | SCHEME_V->args = NIL; |
5616 | SCHEME_V->envir = NIL; |
5992 | SCHEME_V->envir = NIL; |
|
|
5993 | SCHEME_V->value = NIL; |
5617 | SCHEME_V->tracing = 0; |
5994 | SCHEME_V->tracing = 0; |
5618 | |
5995 | |
5619 | /* init NIL */ |
5996 | /* init NIL */ |
5620 | set_typeflag (NIL, T_ATOM | T_MARK); |
5997 | set_typeflag (NIL, T_ATOM | T_MARK); |
5621 | set_car (NIL, NIL); |
5998 | set_car (NIL, NIL); |
… | |
… | |
5677 | |
6054 | |
5678 | return !SCHEME_V->no_memory; |
6055 | return !SCHEME_V->no_memory; |
5679 | } |
6056 | } |
5680 | |
6057 | |
5681 | #if USE_PORTS |
6058 | #if USE_PORTS |
5682 | void |
6059 | ecb_cold void |
5683 | scheme_set_input_port_file (SCHEME_P_ int fin) |
6060 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5684 | { |
6061 | { |
5685 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
6062 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5686 | } |
6063 | } |
5687 | |
6064 | |
5688 | void |
6065 | ecb_cold void |
5689 | 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) |
5690 | { |
6067 | { |
5691 | 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); |
5692 | } |
6069 | } |
5693 | |
6070 | |
5694 | void |
6071 | ecb_cold void |
5695 | scheme_set_output_port_file (SCHEME_P_ int fout) |
6072 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5696 | { |
6073 | { |
5697 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
6074 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5698 | } |
6075 | } |
5699 | |
6076 | |
5700 | void |
6077 | ecb_cold void |
5701 | 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) |
5702 | { |
6079 | { |
5703 | 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); |
5704 | } |
6081 | } |
5705 | #endif |
6082 | #endif |
5706 | |
6083 | |
5707 | void |
6084 | ecb_cold void |
5708 | scheme_set_external_data (SCHEME_P_ void *p) |
6085 | scheme_set_external_data (SCHEME_P_ void *p) |
5709 | { |
6086 | { |
5710 | SCHEME_V->ext_data = p; |
6087 | SCHEME_V->ext_data = p; |
5711 | } |
6088 | } |
5712 | |
6089 | |
… | |
… | |
5744 | SCHEME_V->loadport = NIL; |
6121 | SCHEME_V->loadport = NIL; |
5745 | SCHEME_V->gc_verbose = 0; |
6122 | SCHEME_V->gc_verbose = 0; |
5746 | gc (SCHEME_A_ NIL, NIL); |
6123 | gc (SCHEME_A_ NIL, NIL); |
5747 | |
6124 | |
5748 | for (i = 0; i <= SCHEME_V->last_cell_seg; i++) |
6125 | for (i = 0; i <= SCHEME_V->last_cell_seg; i++) |
5749 | free (SCHEME_V->alloc_seg[i]); |
6126 | free (SCHEME_V->cell_seg[i]); |
5750 | |
6127 | |
5751 | #if SHOW_ERROR_LINE |
6128 | #if SHOW_ERROR_LINE |
5752 | for (i = 0; i <= SCHEME_V->file_i; i++) |
6129 | for (i = 0; i <= SCHEME_V->file_i; i++) |
5753 | { |
|
|
5754 | if (SCHEME_V->load_stack[i].kind & port_file) |
6130 | if (SCHEME_V->load_stack[i].kind & port_file) |
5755 | { |
6131 | { |
5756 | fname = SCHEME_V->load_stack[i].rep.stdio.filename; |
6132 | fname = SCHEME_V->load_stack[i].rep.stdio.filename; |
5757 | |
6133 | |
5758 | if (fname) |
6134 | if (fname) |
5759 | free (fname); |
6135 | free (fname); |
5760 | } |
6136 | } |
5761 | } |
|
|
5762 | #endif |
6137 | #endif |
5763 | } |
6138 | } |
5764 | |
6139 | |
5765 | void |
6140 | ecb_cold void |
5766 | scheme_load_file (SCHEME_P_ int fin) |
6141 | scheme_load_file (SCHEME_P_ int fin) |
5767 | { |
6142 | { |
5768 | scheme_load_named_file (SCHEME_A_ fin, 0); |
6143 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5769 | } |
6144 | } |
5770 | |
6145 | |
5771 | void |
6146 | ecb_cold void |
5772 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
6147 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5773 | { |
6148 | { |
5774 | dump_stack_reset (SCHEME_A); |
6149 | dump_stack_reset (SCHEME_A); |
5775 | SCHEME_V->envir = SCHEME_V->global_env; |
6150 | SCHEME_V->envir = SCHEME_V->global_env; |
5776 | SCHEME_V->file_i = 0; |
6151 | SCHEME_V->file_i = 0; |
5777 | SCHEME_V->load_stack[0].unget = -1; |
6152 | SCHEME_V->load_stack[0].unget = -1; |
5778 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
6153 | SCHEME_V->load_stack[0].kind = port_input | port_file; |
5779 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
6154 | SCHEME_V->load_stack[0].rep.stdio.file = fin; |
5780 | #if USE_PORTS |
|
|
5781 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
6155 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5782 | #endif |
|
|
5783 | SCHEME_V->retcode = 0; |
6156 | SCHEME_V->retcode = 0; |
5784 | |
6157 | |
5785 | #if USE_PORTS |
|
|
5786 | if (fin == STDIN_FILENO) |
6158 | if (fin == STDIN_FILENO) |
5787 | SCHEME_V->interactive_repl = 1; |
6159 | SCHEME_V->interactive_repl = 1; |
5788 | #endif |
|
|
5789 | |
6160 | |
5790 | #if USE_PORTS |
6161 | #if USE_PORTS |
5791 | #if SHOW_ERROR_LINE |
6162 | #if SHOW_ERROR_LINE |
5792 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
6163 | SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; |
5793 | |
6164 | |
… | |
… | |
5797 | #endif |
6168 | #endif |
5798 | |
6169 | |
5799 | SCHEME_V->inport = SCHEME_V->loadport; |
6170 | SCHEME_V->inport = SCHEME_V->loadport; |
5800 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
6171 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5801 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
6172 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
|
|
6173 | |
5802 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
6174 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5803 | |
6175 | |
5804 | if (SCHEME_V->retcode == 0) |
6176 | if (SCHEME_V->retcode == 0) |
5805 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
6177 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5806 | } |
6178 | } |
5807 | |
6179 | |
5808 | void |
6180 | ecb_cold void |
5809 | scheme_load_string (SCHEME_P_ const char *cmd) |
6181 | scheme_load_string (SCHEME_P_ const char *cmd) |
5810 | { |
6182 | { |
|
|
6183 | #if USE_PORTs |
5811 | dump_stack_reset (SCHEME_A); |
6184 | dump_stack_reset (SCHEME_A); |
5812 | SCHEME_V->envir = SCHEME_V->global_env; |
6185 | SCHEME_V->envir = SCHEME_V->global_env; |
5813 | SCHEME_V->file_i = 0; |
6186 | SCHEME_V->file_i = 0; |
5814 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
6187 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5815 | 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 */ |
5816 | 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); |
5817 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
6190 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5818 | #if USE_PORTS |
|
|
5819 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
6191 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5820 | #endif |
|
|
5821 | SCHEME_V->retcode = 0; |
6192 | SCHEME_V->retcode = 0; |
5822 | SCHEME_V->interactive_repl = 0; |
6193 | SCHEME_V->interactive_repl = 0; |
5823 | SCHEME_V->inport = SCHEME_V->loadport; |
6194 | SCHEME_V->inport = SCHEME_V->loadport; |
5824 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
6195 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
5825 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
6196 | Eval_Cycle (SCHEME_A_ OP_T0LVL); |
5826 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
6197 | set_typeflag (SCHEME_V->loadport, T_ATOM); |
5827 | |
6198 | |
5828 | if (SCHEME_V->retcode == 0) |
6199 | if (SCHEME_V->retcode == 0) |
5829 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
6200 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
|
|
6201 | #else |
|
|
6202 | abort (); |
|
|
6203 | #endif |
5830 | } |
6204 | } |
5831 | |
6205 | |
5832 | void |
6206 | ecb_cold void |
5833 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
6207 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5834 | { |
6208 | { |
5835 | pointer x; |
6209 | pointer x; |
5836 | |
6210 | |
5837 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
6211 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
… | |
… | |
5842 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
6216 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5843 | } |
6217 | } |
5844 | |
6218 | |
5845 | #if !STANDALONE |
6219 | #if !STANDALONE |
5846 | |
6220 | |
5847 | void |
6221 | ecb_cold void |
5848 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
6222 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5849 | { |
6223 | { |
5850 | 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)); |
5851 | } |
6225 | } |
5852 | |
6226 | |
5853 | void |
6227 | ecb_cold void |
5854 | 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) |
5855 | { |
6229 | { |
5856 | int i; |
6230 | int i; |
5857 | |
6231 | |
5858 | for (i = 0; i < count; i++) |
6232 | for (i = 0; i < count; i++) |
5859 | scheme_register_foreign_func (SCHEME_A_ list + i); |
6233 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5860 | } |
6234 | } |
5861 | |
6235 | |
5862 | pointer |
6236 | ecb_cold pointer |
5863 | scheme_apply0 (SCHEME_P_ const char *procname) |
6237 | scheme_apply0 (SCHEME_P_ const char *procname) |
5864 | { |
6238 | { |
5865 | 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)); |
5866 | } |
6240 | } |
5867 | |
6241 | |
5868 | void |
6242 | ecb_cold void |
5869 | save_from_C_call (SCHEME_P) |
6243 | save_from_C_call (SCHEME_P) |
5870 | { |
6244 | { |
5871 | pointer saved_data = cons (car (S_SINK), |
6245 | pointer saved_data = cons (car (S_SINK), |
5872 | cons (SCHEME_V->envir, |
6246 | cons (SCHEME_V->envir, |
5873 | SCHEME_V->dump)); |
6247 | SCHEME_V->dump)); |
… | |
… | |
5877 | /* 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 |
5878 | directly resume pre-C-call operations. */ |
6252 | directly resume pre-C-call operations. */ |
5879 | dump_stack_reset (SCHEME_A); |
6253 | dump_stack_reset (SCHEME_A); |
5880 | } |
6254 | } |
5881 | |
6255 | |
5882 | void |
6256 | ecb_cold void |
5883 | restore_from_C_call (SCHEME_P) |
6257 | restore_from_C_call (SCHEME_P) |
5884 | { |
6258 | { |
5885 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
6259 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5886 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
6260 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5887 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
6261 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5888 | /* Pop */ |
6262 | /* Pop */ |
5889 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
6263 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5890 | } |
6264 | } |
5891 | |
6265 | |
5892 | /* "func" and "args" are assumed to be already eval'ed. */ |
6266 | /* "func" and "args" are assumed to be already eval'ed. */ |
5893 | pointer |
6267 | ecb_cold pointer |
5894 | scheme_call (SCHEME_P_ pointer func, pointer args) |
6268 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5895 | { |
6269 | { |
5896 | int old_repl = SCHEME_V->interactive_repl; |
6270 | int old_repl = SCHEME_V->interactive_repl; |
5897 | |
6271 | |
5898 | SCHEME_V->interactive_repl = 0; |
6272 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5905 | SCHEME_V->interactive_repl = old_repl; |
6279 | SCHEME_V->interactive_repl = old_repl; |
5906 | restore_from_C_call (SCHEME_A); |
6280 | restore_from_C_call (SCHEME_A); |
5907 | return SCHEME_V->value; |
6281 | return SCHEME_V->value; |
5908 | } |
6282 | } |
5909 | |
6283 | |
5910 | pointer |
6284 | ecb_cold pointer |
5911 | scheme_eval (SCHEME_P_ pointer obj) |
6285 | scheme_eval (SCHEME_P_ pointer obj) |
5912 | { |
6286 | { |
5913 | int old_repl = SCHEME_V->interactive_repl; |
6287 | int old_repl = SCHEME_V->interactive_repl; |
5914 | |
6288 | |
5915 | SCHEME_V->interactive_repl = 0; |
6289 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5927 | |
6301 | |
5928 | /* ========== Main ========== */ |
6302 | /* ========== Main ========== */ |
5929 | |
6303 | |
5930 | #if STANDALONE |
6304 | #if STANDALONE |
5931 | |
6305 | |
5932 | int |
6306 | ecb_cold int |
5933 | main (int argc, char **argv) |
6307 | main (int argc, char **argv) |
5934 | { |
6308 | { |
5935 | # if USE_MULTIPLICITY |
6309 | # if USE_MULTIPLICITY |
5936 | scheme ssc; |
6310 | scheme ssc; |
5937 | scheme *const SCHEME_V = &ssc; |
6311 | scheme *const SCHEME_V = &ssc; |
… | |
… | |
5939 | # endif |
6313 | # endif |
5940 | int fin; |
6314 | int fin; |
5941 | char *file_name = InitFile; |
6315 | char *file_name = InitFile; |
5942 | int retcode; |
6316 | int retcode; |
5943 | int isfile = 1; |
6317 | int isfile = 1; |
|
|
6318 | #if EXPERIMENT |
|
|
6319 | system ("ps v $PPID"); |
|
|
6320 | #endif |
5944 | |
6321 | |
5945 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6322 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5946 | { |
6323 | { |
5947 | xwrstr ("Usage: tinyscheme -?\n"); |
6324 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
5948 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6325 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
5949 | xwrstr ("followed by\n"); |
6326 | putstr (SCHEME_A_ "followed by\n"); |
5950 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6327 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
5951 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6328 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
5952 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6329 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
5953 | xwrstr ("Use - as filename for stdin.\n"); |
6330 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
5954 | return 1; |
6331 | return 1; |
5955 | } |
6332 | } |
5956 | |
6333 | |
5957 | if (!scheme_init (SCHEME_A)) |
6334 | if (!scheme_init (SCHEME_A)) |
5958 | { |
6335 | { |
5959 | xwrstr ("Could not initialize!\n"); |
6336 | putstr (SCHEME_A_ "Could not initialize!\n"); |
5960 | return 2; |
6337 | return 2; |
5961 | } |
6338 | } |
5962 | |
6339 | |
5963 | # if USE_PORTS |
6340 | # if USE_PORTS |
5964 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6341 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
5977 | } |
6354 | } |
5978 | #endif |
6355 | #endif |
5979 | |
6356 | |
5980 | do |
6357 | do |
5981 | { |
6358 | { |
5982 | #if USE_PORTS |
|
|
5983 | if (strcmp (file_name, "-") == 0) |
6359 | if (strcmp (file_name, "-") == 0) |
5984 | fin = STDIN_FILENO; |
6360 | fin = STDIN_FILENO; |
5985 | 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) |
5986 | { |
6362 | { |
5987 | pointer args = NIL; |
6363 | pointer args = NIL; |
… | |
… | |
6005 | 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); |
6006 | |
6382 | |
6007 | } |
6383 | } |
6008 | else |
6384 | else |
6009 | fin = open (file_name, O_RDONLY); |
6385 | fin = open (file_name, O_RDONLY); |
6010 | #endif |
|
|
6011 | |
6386 | |
6012 | if (isfile && fin < 0) |
6387 | if (isfile && fin < 0) |
6013 | { |
6388 | { |
6014 | 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'); |
6015 | } |
6392 | } |
6016 | else |
6393 | else |
6017 | { |
6394 | { |
6018 | if (isfile) |
6395 | if (isfile) |
6019 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6396 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6020 | else |
6397 | else |
6021 | scheme_load_string (SCHEME_A_ file_name); |
6398 | scheme_load_string (SCHEME_A_ file_name); |
6022 | |
6399 | |
6023 | #if USE_PORTS |
|
|
6024 | if (!isfile || fin != STDIN_FILENO) |
6400 | if (!isfile || fin != STDIN_FILENO) |
6025 | { |
6401 | { |
6026 | if (SCHEME_V->retcode != 0) |
6402 | if (SCHEME_V->retcode != 0) |
6027 | { |
6403 | { |
6028 | 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'); |
6029 | } |
6407 | } |
6030 | |
6408 | |
6031 | if (isfile) |
6409 | if (isfile) |
6032 | close (fin); |
6410 | close (fin); |
6033 | } |
6411 | } |
6034 | #endif |
|
|
6035 | } |
6412 | } |
6036 | |
6413 | |
6037 | file_name = *argv++; |
6414 | file_name = *argv++; |
6038 | } |
6415 | } |
6039 | while (file_name != 0); |
6416 | while (file_name != 0); |