… | |
… | |
16 | * (MINISCM) This is a revised and modified version by Akira KIDA. |
16 | * (MINISCM) This is a revised and modified version by Akira KIDA. |
17 | * (MINISCM) current version is 0.85k4 (15 May 1994) |
17 | * (MINISCM) current version is 0.85k4 (15 May 1994) |
18 | * |
18 | * |
19 | */ |
19 | */ |
20 | |
20 | |
|
|
21 | #define EXPERIMENT 1 |
|
|
22 | |
|
|
23 | #if 1 |
21 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
24 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
22 | #include "malloc.c" |
25 | #include "malloc.c" |
|
|
26 | #endif |
23 | |
27 | |
24 | #define SCHEME_SOURCE |
28 | #define SCHEME_SOURCE |
25 | #include "scheme-private.h" |
29 | #include "scheme-private.h" |
26 | #ifndef WIN32 |
30 | #ifndef WIN32 |
27 | # include <unistd.h> |
31 | # include <unistd.h> |
… | |
… | |
34 | |
38 | |
35 | #include <sys/types.h> |
39 | #include <sys/types.h> |
36 | #include <sys/stat.h> |
40 | #include <sys/stat.h> |
37 | #include <fcntl.h> |
41 | #include <fcntl.h> |
38 | |
42 | |
|
|
43 | #if !USE_ERROR_CHECKING |
|
|
44 | # define NDEBUG |
|
|
45 | #endif |
|
|
46 | |
|
|
47 | #include <assert.h> |
|
|
48 | #include <stdlib.h> |
39 | #include <string.h> |
49 | #include <string.h> |
40 | #include <stdlib.h> |
|
|
41 | |
50 | |
42 | #include <limits.h> |
51 | #include <limits.h> |
43 | #include <inttypes.h> |
52 | #include <inttypes.h> |
44 | #include <float.h> |
53 | #include <float.h> |
45 | //#include <ctype.h> |
54 | //#include <ctype.h> |
|
|
55 | |
|
|
56 | #if '1' != '0' + 1 \ |
|
|
57 | || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ |
|
|
58 | || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ |
|
|
59 | || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ |
|
|
60 | || 'f' != 'a' + 5 |
|
|
61 | # error "execution character set digits not consecutive" |
|
|
62 | #endif |
46 | |
63 | |
47 | enum { |
64 | enum { |
48 | TOK_EOF, |
65 | TOK_EOF, |
49 | TOK_LPAREN, |
66 | TOK_LPAREN, |
50 | TOK_RPAREN, |
67 | TOK_RPAREN, |
51 | TOK_DOT, |
68 | TOK_DOT, |
52 | TOK_ATOM, |
69 | TOK_ATOM, |
|
|
70 | TOK_DOTATOM, /* atom name starting with '.' */ |
|
|
71 | TOK_STRATOM, /* atom name enclosed in | */ |
53 | TOK_QUOTE, |
72 | TOK_QUOTE, |
54 | TOK_DQUOTE, |
73 | TOK_DQUOTE, |
55 | TOK_BQUOTE, |
74 | TOK_BQUOTE, |
56 | TOK_COMMA, |
75 | TOK_COMMA, |
57 | TOK_ATMARK, |
76 | TOK_ATMARK, |
… | |
… | |
59 | TOK_SHARP_CONST, |
78 | TOK_SHARP_CONST, |
60 | TOK_VEC |
79 | TOK_VEC |
61 | }; |
80 | }; |
62 | |
81 | |
63 | #define BACKQUOTE '`' |
82 | #define BACKQUOTE '`' |
64 | #define DELIMITERS "()\";\f\t\v\n\r " |
83 | #define WHITESPACE " \t\r\n\v\f" |
|
|
84 | #define DELIMITERS "()\";" WHITESPACE |
65 | |
85 | |
66 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
86 | #define NIL POINTER (&SCHEME_V->xNIL) |
67 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
87 | #define S_T POINTER (&SCHEME_V->xT) |
68 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
88 | #define S_F POINTER (&SCHEME_V->xF) |
69 | #define S_SINK (&SCHEME_V->xsink) |
89 | #define S_SINK POINTER (&SCHEME_V->xsink) |
70 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
90 | #define S_EOF POINTER (&SCHEME_V->xEOF_OBJ) |
71 | |
91 | |
72 | #if !USE_MULTIPLICITY |
92 | #if !USE_MULTIPLICITY |
73 | static scheme sc; |
93 | static scheme sc; |
74 | #endif |
94 | #endif |
75 | |
95 | |
76 | static void |
96 | ecb_cold static void |
77 | xbase (char *s, long n, int base) |
97 | xbase (char *s, long n, int base) |
78 | { |
98 | { |
79 | if (n < 0) |
99 | if (n < 0) |
80 | { |
100 | { |
81 | *s++ = '-'; |
101 | *s++ = '-'; |
… | |
… | |
83 | } |
103 | } |
84 | |
104 | |
85 | char *p = s; |
105 | char *p = s; |
86 | |
106 | |
87 | do { |
107 | do { |
88 | *p++ = '0' + n % base; |
108 | *p++ = "0123456789abcdef"[n % base]; |
89 | n /= base; |
109 | n /= base; |
90 | } while (n); |
110 | } while (n); |
91 | |
111 | |
92 | *p-- = 0; |
112 | *p-- = 0; |
93 | |
113 | |
… | |
… | |
96 | char x = *s; *s = *p; *p = x; |
116 | char x = *s; *s = *p; *p = x; |
97 | --p; ++s; |
117 | --p; ++s; |
98 | } |
118 | } |
99 | } |
119 | } |
100 | |
120 | |
101 | static void |
121 | ecb_cold static void |
102 | xnum (char *s, long n) |
122 | xnum (char *s, long n) |
103 | { |
123 | { |
104 | xbase (s, n, 10); |
124 | xbase (s, n, 10); |
105 | } |
125 | } |
106 | |
126 | |
107 | static void |
127 | ecb_cold static void |
108 | xwrstr (const char *s) |
128 | putnum (SCHEME_P_ long n) |
109 | { |
|
|
110 | write (1, s, strlen (s)); |
|
|
111 | } |
|
|
112 | |
|
|
113 | static void |
|
|
114 | xwrnum (long n) |
|
|
115 | { |
129 | { |
116 | char buf[64]; |
130 | char buf[64]; |
117 | |
131 | |
118 | xnum (buf, n); |
132 | xnum (buf, n); |
119 | xwrstr (buf); |
133 | putstr (SCHEME_A_ buf); |
120 | } |
134 | } |
121 | |
135 | |
122 | static char |
136 | ecb_cold static char |
123 | xtoupper (char c) |
137 | xtoupper (char c) |
124 | { |
138 | { |
125 | if (c >= 'a' && c <= 'z') |
139 | if (c >= 'a' && c <= 'z') |
126 | c -= 'a' - 'A'; |
140 | c -= 'a' - 'A'; |
127 | |
141 | |
128 | return c; |
142 | return c; |
129 | } |
143 | } |
130 | |
144 | |
131 | static char |
145 | ecb_cold static char |
132 | xtolower (char c) |
146 | xtolower (char c) |
133 | { |
147 | { |
134 | if (c >= 'A' && c <= 'Z') |
148 | if (c >= 'A' && c <= 'Z') |
135 | c += 'a' - 'A'; |
149 | c += 'a' - 'A'; |
136 | |
150 | |
137 | return c; |
151 | return c; |
138 | } |
152 | } |
139 | |
153 | |
140 | static int |
154 | ecb_cold static int |
141 | xisdigit (char c) |
155 | xisdigit (char c) |
142 | { |
156 | { |
143 | return c >= '0' && c <= '9'; |
157 | return c >= '0' && c <= '9'; |
144 | } |
158 | } |
145 | |
159 | |
146 | #define toupper(c) xtoupper (c) |
160 | #define toupper(c) xtoupper (c) |
147 | #define tolower(c) xtolower (c) |
161 | #define tolower(c) xtolower (c) |
148 | #define isdigit(c) xisdigit (c) |
162 | #define isdigit(c) xisdigit (c) |
149 | |
163 | |
150 | #if USE_IGNORECASE |
164 | #if USE_IGNORECASE |
151 | static const char * |
165 | ecb_cold static const char * |
152 | xstrlwr (char *s) |
166 | xstrlwr (char *s) |
153 | { |
167 | { |
154 | const char *p = s; |
168 | const char *p = s; |
155 | |
169 | |
156 | while (*s) |
170 | while (*s) |
… | |
… | |
176 | |
190 | |
177 | #ifndef InitFile |
191 | #ifndef InitFile |
178 | # define InitFile "init.scm" |
192 | # define InitFile "init.scm" |
179 | #endif |
193 | #endif |
180 | |
194 | |
181 | #ifndef FIRST_CELLSEGS |
|
|
182 | # define FIRST_CELLSEGS 3 |
|
|
183 | #endif |
|
|
184 | |
|
|
185 | enum scheme_types |
195 | enum scheme_types |
186 | { |
196 | { |
|
|
197 | T_INTEGER, |
|
|
198 | T_CHARACTER, |
187 | T_FREE, |
199 | T_REAL, |
188 | T_STRING, |
200 | T_STRING, |
189 | T_NUMBER, |
|
|
190 | T_SYMBOL, |
201 | T_SYMBOL, |
191 | T_PROC, |
202 | T_PROC, |
192 | T_PAIR, |
203 | T_PAIR, /* also used for free cells */ |
193 | T_CLOSURE, |
204 | T_CLOSURE, |
|
|
205 | T_MACRO, |
194 | T_CONTINUATION, |
206 | T_CONTINUATION, |
195 | T_FOREIGN, |
207 | T_FOREIGN, |
196 | T_CHARACTER, |
|
|
197 | T_PORT, |
208 | T_PORT, |
198 | T_VECTOR, |
209 | T_VECTOR, |
199 | T_MACRO, |
|
|
200 | T_PROMISE, |
210 | T_PROMISE, |
201 | T_ENVIRONMENT, |
211 | T_ENVIRONMENT, |
202 | /* one more... */ |
212 | /* one more... */ |
203 | T_NUM_SYSTEM_TYPES |
213 | T_NUM_SYSTEM_TYPES |
204 | }; |
214 | }; |
… | |
… | |
207 | #define T_SYNTAX 0x0010 |
217 | #define T_SYNTAX 0x0010 |
208 | #define T_IMMUTABLE 0x0020 |
218 | #define T_IMMUTABLE 0x0020 |
209 | #define T_ATOM 0x0040 /* only for gc */ |
219 | #define T_ATOM 0x0040 /* only for gc */ |
210 | #define T_MARK 0x0080 /* only for gc */ |
220 | #define T_MARK 0x0080 /* only for gc */ |
211 | |
221 | |
|
|
222 | /* num, for generic arithmetic */ |
|
|
223 | struct num |
|
|
224 | { |
|
|
225 | IVALUE ivalue; |
|
|
226 | #if USE_REAL |
|
|
227 | RVALUE rvalue; |
|
|
228 | char is_fixnum; |
|
|
229 | #endif |
|
|
230 | }; |
|
|
231 | |
|
|
232 | #if USE_REAL |
|
|
233 | # define num_is_fixnum(n) (n).is_fixnum |
|
|
234 | # define num_set_fixnum(n,f) (n).is_fixnum = (f) |
|
|
235 | # define num_ivalue(n) (n).ivalue |
|
|
236 | # define num_rvalue(n) (n).rvalue |
|
|
237 | # define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i) |
|
|
238 | # define num_set_rvalue(n,r) (n).rvalue = (r) |
|
|
239 | #else |
|
|
240 | # define num_is_fixnum(n) 1 |
|
|
241 | # define num_set_fixnum(n,f) 0 |
|
|
242 | # define num_ivalue(n) (n).ivalue |
|
|
243 | # define num_rvalue(n) (n).ivalue |
|
|
244 | # define num_set_ivalue(n,i) (n).ivalue = (i) |
|
|
245 | # define num_set_rvalue(n,r) (n).ivalue = (r) |
|
|
246 | #endif |
|
|
247 | |
212 | enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; |
248 | enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; |
213 | |
249 | |
214 | static num num_op (enum num_op op, num a, num b); |
250 | static num num_op (enum num_op op, num a, num b); |
215 | static num num_intdiv (num a, num b); |
251 | static num num_intdiv (num a, num b); |
216 | static num num_rem (num a, num b); |
252 | static num num_rem (num a, num b); |
217 | static num num_mod (num a, num b); |
253 | static num num_mod (num a, num b); |
218 | |
254 | |
219 | #if USE_MATH |
|
|
220 | static double round_per_R5RS (double x); |
|
|
221 | #endif |
|
|
222 | static int is_zero_rvalue (RVALUE x); |
255 | static int is_zero_rvalue (RVALUE x); |
223 | |
256 | |
224 | static num num_zero; |
257 | static num num_zero; |
225 | static num num_one; |
258 | static num num_one; |
226 | |
259 | |
|
|
260 | /* convert "pointer" to cell* / cell* to pointer */ |
|
|
261 | #define CELL(p) ((struct cell *)(p) + 0) |
|
|
262 | #define POINTER(c) ((void *)((c) - 0)) |
|
|
263 | |
227 | /* macros for cell operations */ |
264 | /* macros for cell operations */ |
228 | #define typeflag(p) ((p)->flag + 0) |
265 | #define typeflag(p) (CELL(p)->flag + 0) |
229 | #define set_typeflag(p,v) ((p)->flag = (v)) |
266 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
230 | #define type(p) (typeflag (p) & T_MASKTYPE) |
267 | #define type(p) (typeflag (p) & T_MASKTYPE) |
231 | |
268 | |
232 | INTERFACE int |
269 | INTERFACE int |
233 | is_string (pointer p) |
270 | is_string (pointer p) |
234 | { |
271 | { |
235 | return type (p) == T_STRING; |
272 | return type (p) == T_STRING; |
236 | } |
273 | } |
237 | |
274 | |
238 | #define strvalue(p) ((p)->object.string.svalue) |
275 | #define strvalue(p) (CELL(p)->object.string.svalue) |
239 | #define strlength(p) ((p)->object.string.length) |
276 | #define strlength(p) (CELL(p)->object.string.length) |
240 | |
|
|
241 | INTERFACE int is_list (SCHEME_P_ pointer p); |
|
|
242 | |
277 | |
243 | INTERFACE int |
278 | INTERFACE int |
244 | is_vector (pointer p) |
279 | is_vector (pointer p) |
245 | { |
280 | { |
246 | return type (p) == T_VECTOR; |
281 | return type (p) == T_VECTOR; |
247 | } |
282 | } |
248 | |
283 | |
249 | #define vecvalue(p) ((p)->object.vector.vvalue) |
284 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
250 | #define veclength(p) ((p)->object.vector.length) |
285 | #define veclength(p) (CELL(p)->object.vector.length) |
251 | INTERFACE void fill_vector (pointer vec, pointer obj); |
286 | 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); |
287 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
254 | INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); |
288 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
255 | |
289 | |
256 | INTERFACE uint32_t |
290 | INTERFACE int |
257 | vector_length (pointer vec) |
291 | is_integer (pointer p) |
258 | { |
292 | { |
259 | return vec->object.vector.length; |
293 | return type (p) == T_INTEGER; |
|
|
294 | } |
|
|
295 | |
|
|
296 | /* not the same as in scheme, where integers are (correctly :) reals */ |
|
|
297 | INTERFACE int |
|
|
298 | is_real (pointer p) |
|
|
299 | { |
|
|
300 | return type (p) == T_REAL; |
260 | } |
301 | } |
261 | |
302 | |
262 | INTERFACE int |
303 | INTERFACE int |
263 | is_number (pointer p) |
304 | is_number (pointer p) |
264 | { |
305 | { |
265 | return type (p) == T_NUMBER; |
306 | 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 | } |
307 | } |
279 | |
308 | |
280 | INTERFACE int |
309 | INTERFACE int |
281 | is_character (pointer p) |
310 | is_character (pointer p) |
282 | { |
311 | { |
… | |
… | |
287 | string_value (pointer p) |
316 | string_value (pointer p) |
288 | { |
317 | { |
289 | return strvalue (p); |
318 | return strvalue (p); |
290 | } |
319 | } |
291 | |
320 | |
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) |
321 | #define ivalue_unchecked(p) CELL(p)->object.ivalue |
|
|
322 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
|
|
323 | |
323 | #if USE_REAL |
324 | #if USE_REAL |
324 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
325 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
325 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
326 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
326 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
|
|
327 | #else |
327 | #else |
328 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
328 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
329 | # define set_num_integer(p) 0 |
329 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
330 | # define set_num_real(p) 0 |
|
|
331 | #endif |
330 | #endif |
332 | |
331 | |
333 | INTERFACE long |
332 | INTERFACE long |
334 | charvalue (pointer p) |
333 | charvalue (pointer p) |
335 | { |
334 | { |
336 | return ivalue_unchecked (p); |
335 | return ivalue_unchecked (p); |
337 | } |
336 | } |
338 | |
337 | |
|
|
338 | #define port(p) CELL(p)->object.port |
|
|
339 | #define set_port(p,v) port(p) = (v) |
339 | INTERFACE int |
340 | INTERFACE int |
340 | is_port (pointer p) |
341 | is_port (pointer p) |
341 | { |
342 | { |
342 | return type (p) == T_PORT; |
343 | return type (p) == T_PORT; |
343 | } |
344 | } |
344 | |
345 | |
345 | INTERFACE int |
346 | INTERFACE int |
346 | is_inport (pointer p) |
347 | is_inport (pointer p) |
347 | { |
348 | { |
348 | return is_port (p) && p->object.port->kind & port_input; |
349 | return is_port (p) && port (p)->kind & port_input; |
349 | } |
350 | } |
350 | |
351 | |
351 | INTERFACE int |
352 | INTERFACE int |
352 | is_outport (pointer p) |
353 | is_outport (pointer p) |
353 | { |
354 | { |
354 | return is_port (p) && p->object.port->kind & port_output; |
355 | return is_port (p) && port (p)->kind & port_output; |
355 | } |
356 | } |
356 | |
357 | |
357 | INTERFACE int |
358 | INTERFACE int |
358 | is_pair (pointer p) |
359 | is_pair (pointer p) |
359 | { |
360 | { |
360 | return type (p) == T_PAIR; |
361 | return type (p) == T_PAIR; |
361 | } |
362 | } |
362 | |
363 | |
363 | #define car(p) ((p)->object.cons.car + 0) |
364 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
364 | #define cdr(p) ((p)->object.cons.cdr + 0) |
365 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
365 | |
366 | |
366 | static pointer caar (pointer p) { return car (car (p)); } |
367 | static pointer caar (pointer p) { return car (car (p)); } |
367 | static pointer cadr (pointer p) { return car (cdr (p)); } |
368 | static pointer cadr (pointer p) { return car (cdr (p)); } |
368 | static pointer cdar (pointer p) { return cdr (car (p)); } |
369 | static pointer cdar (pointer p) { return cdr (car (p)); } |
369 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
370 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
… | |
… | |
373 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
374 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
374 | |
375 | |
375 | INTERFACE void |
376 | INTERFACE void |
376 | set_car (pointer p, pointer q) |
377 | set_car (pointer p, pointer q) |
377 | { |
378 | { |
378 | p->object.cons.car = q; |
379 | CELL(p)->object.cons.car = CELL (q); |
379 | } |
380 | } |
380 | |
381 | |
381 | INTERFACE void |
382 | INTERFACE void |
382 | set_cdr (pointer p, pointer q) |
383 | set_cdr (pointer p, pointer q) |
383 | { |
384 | { |
384 | p->object.cons.cdr = q; |
385 | CELL(p)->object.cons.cdr = CELL (q); |
385 | } |
386 | } |
386 | |
387 | |
387 | INTERFACE pointer |
388 | INTERFACE pointer |
388 | pair_car (pointer p) |
389 | pair_car (pointer p) |
389 | { |
390 | { |
… | |
… | |
403 | } |
404 | } |
404 | |
405 | |
405 | INTERFACE char * |
406 | INTERFACE char * |
406 | symname (pointer p) |
407 | symname (pointer p) |
407 | { |
408 | { |
408 | return strvalue (car (p)); |
409 | return strvalue (p); |
409 | } |
410 | } |
410 | |
411 | |
411 | #if USE_PLIST |
412 | #if USE_PLIST |
|
|
413 | #error plists are broken because symbols are no longer pairs |
|
|
414 | #define symprop(p) cdr(p) |
412 | SCHEME_EXPORT int |
415 | SCHEME_EXPORT int |
413 | hasprop (pointer p) |
416 | hasprop (pointer p) |
414 | { |
417 | { |
415 | return typeflag (p) & T_SYMBOL; |
418 | return typeflag (p) & T_SYMBOL; |
416 | } |
419 | } |
417 | |
|
|
418 | # define symprop(p) cdr(p) |
|
|
419 | #endif |
420 | #endif |
420 | |
421 | |
421 | INTERFACE int |
422 | INTERFACE int |
422 | is_syntax (pointer p) |
423 | is_syntax (pointer p) |
423 | { |
424 | { |
… | |
… | |
437 | } |
438 | } |
438 | |
439 | |
439 | INTERFACE char * |
440 | INTERFACE char * |
440 | syntaxname (pointer p) |
441 | syntaxname (pointer p) |
441 | { |
442 | { |
442 | return strvalue (car (p)); |
443 | return strvalue (p); |
443 | } |
444 | } |
444 | |
445 | |
445 | #define procnum(p) ivalue (p) |
446 | #define procnum(p) ivalue_unchecked (p) |
446 | static const char *procname (pointer x); |
447 | static const char *procname (pointer x); |
447 | |
448 | |
448 | INTERFACE int |
449 | INTERFACE int |
449 | is_closure (pointer p) |
450 | is_closure (pointer p) |
450 | { |
451 | { |
… | |
… | |
511 | setimmutable (pointer p) |
512 | setimmutable (pointer p) |
512 | { |
513 | { |
513 | #if USE_ERROR_CHECKING |
514 | #if USE_ERROR_CHECKING |
514 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
515 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
515 | #endif |
516 | #endif |
|
|
517 | } |
|
|
518 | |
|
|
519 | /* Result is: |
|
|
520 | proper list: length |
|
|
521 | circular list: -1 |
|
|
522 | not even a pair: -2 |
|
|
523 | dotted list: -2 minus length before dot |
|
|
524 | */ |
|
|
525 | ecb_hot INTERFACE int |
|
|
526 | list_length (SCHEME_P_ pointer a) |
|
|
527 | { |
|
|
528 | int i = 0; |
|
|
529 | pointer slow, fast; |
|
|
530 | |
|
|
531 | slow = fast = a; |
|
|
532 | |
|
|
533 | while (1) |
|
|
534 | { |
|
|
535 | if (fast == NIL) |
|
|
536 | return i; |
|
|
537 | |
|
|
538 | if (!is_pair (fast)) |
|
|
539 | return -2 - i; |
|
|
540 | |
|
|
541 | fast = cdr (fast); |
|
|
542 | ++i; |
|
|
543 | |
|
|
544 | if (fast == NIL) |
|
|
545 | return i; |
|
|
546 | |
|
|
547 | if (!is_pair (fast)) |
|
|
548 | return -2 - i; |
|
|
549 | |
|
|
550 | ++i; |
|
|
551 | fast = cdr (fast); |
|
|
552 | |
|
|
553 | /* Safe because we would have already returned if `fast' |
|
|
554 | encountered a non-pair. */ |
|
|
555 | slow = cdr (slow); |
|
|
556 | |
|
|
557 | if (fast == slow) |
|
|
558 | { |
|
|
559 | /* the fast pointer has looped back around and caught up |
|
|
560 | with the slow pointer, hence the structure is circular, |
|
|
561 | not of finite length, and therefore not a list */ |
|
|
562 | return -1; |
|
|
563 | } |
|
|
564 | } |
|
|
565 | } |
|
|
566 | |
|
|
567 | INTERFACE int |
|
|
568 | is_list (SCHEME_P_ pointer a) |
|
|
569 | { |
|
|
570 | return list_length (SCHEME_A_ a) >= 0; |
516 | } |
571 | } |
517 | |
572 | |
518 | #if USE_CHAR_CLASSIFIERS |
573 | #if USE_CHAR_CLASSIFIERS |
519 | ecb_inline int |
574 | ecb_inline int |
520 | Cisalpha (int c) |
575 | Cisalpha (int c) |
… | |
… | |
581 | "gs", |
636 | "gs", |
582 | "rs", |
637 | "rs", |
583 | "us" |
638 | "us" |
584 | }; |
639 | }; |
585 | |
640 | |
586 | static int |
641 | ecb_cold static int |
587 | is_ascii_name (const char *name, int *pc) |
642 | is_ascii_name (const char *name, int *pc) |
588 | { |
643 | { |
589 | int i; |
644 | int i; |
590 | |
645 | |
591 | for (i = 0; i < 32; i++) |
646 | for (i = 0; i < 32; i++) |
… | |
… | |
609 | #endif |
664 | #endif |
610 | |
665 | |
611 | static int file_push (SCHEME_P_ const char *fname); |
666 | static int file_push (SCHEME_P_ const char *fname); |
612 | static void file_pop (SCHEME_P); |
667 | static void file_pop (SCHEME_P); |
613 | static int file_interactive (SCHEME_P); |
668 | static int file_interactive (SCHEME_P); |
614 | ecb_inline int is_one_of (char *s, int c); |
669 | ecb_inline int is_one_of (const char *s, int c); |
615 | static int alloc_cellseg (SCHEME_P_ int n); |
670 | static int alloc_cellseg (SCHEME_P); |
616 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
671 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
617 | static void finalize_cell (SCHEME_P_ pointer a); |
672 | static void finalize_cell (SCHEME_P_ pointer a); |
618 | static int count_consecutive_cells (pointer x, int needed); |
673 | static int count_consecutive_cells (pointer x, int needed); |
619 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
674 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
620 | static pointer mk_number (SCHEME_P_ const num n); |
675 | static pointer mk_number (SCHEME_P_ const num n); |
… | |
… | |
636 | static void mark (pointer a); |
691 | static void mark (pointer a); |
637 | static void gc (SCHEME_P_ pointer a, pointer b); |
692 | static void gc (SCHEME_P_ pointer a, pointer b); |
638 | static int basic_inchar (port *pt); |
693 | static int basic_inchar (port *pt); |
639 | static int inchar (SCHEME_P); |
694 | static int inchar (SCHEME_P); |
640 | static void backchar (SCHEME_P_ int c); |
695 | static void backchar (SCHEME_P_ int c); |
641 | static char *readstr_upto (SCHEME_P_ char *delim); |
696 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
642 | static pointer readstrexp (SCHEME_P); |
697 | static pointer readstrexp (SCHEME_P_ char delim); |
643 | ecb_inline int skipspace (SCHEME_P); |
698 | ecb_inline int skipspace (SCHEME_P); |
644 | static int token (SCHEME_P); |
699 | static int token (SCHEME_P); |
645 | static void printslashstring (SCHEME_P_ char *s, int len); |
700 | static void printslashstring (SCHEME_P_ char *s, int len); |
646 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
701 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
647 | static void printatom (SCHEME_P_ pointer l, int f); |
702 | static void printatom (SCHEME_P_ pointer l, int f); |
… | |
… | |
664 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
719 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
665 | static void assign_syntax (SCHEME_P_ const char *name); |
720 | static void assign_syntax (SCHEME_P_ const char *name); |
666 | static int syntaxnum (pointer p); |
721 | static int syntaxnum (pointer p); |
667 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
722 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
668 | |
723 | |
|
|
724 | static IVALUE |
|
|
725 | ivalue (pointer x) |
|
|
726 | { |
|
|
727 | return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x); |
|
|
728 | } |
|
|
729 | |
|
|
730 | static RVALUE |
|
|
731 | rvalue (pointer x) |
|
|
732 | { |
|
|
733 | return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x); |
|
|
734 | } |
|
|
735 | |
|
|
736 | INTERFACE num |
|
|
737 | nvalue (pointer x) |
|
|
738 | { |
|
|
739 | num n; |
|
|
740 | |
|
|
741 | num_set_fixnum (n, is_integer (x)); |
|
|
742 | |
|
|
743 | if (num_is_fixnum (n)) |
|
|
744 | num_set_ivalue (n, ivalue_unchecked (x)); |
|
|
745 | else |
|
|
746 | num_set_rvalue (n, rvalue_unchecked (x)); |
|
|
747 | |
|
|
748 | return n; |
|
|
749 | } |
|
|
750 | |
669 | static num |
751 | static num |
670 | num_op (enum num_op op, num a, num b) |
752 | num_op (enum num_op op, num a, num b) |
671 | { |
753 | { |
672 | num ret; |
754 | num ret; |
673 | |
755 | |
674 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
756 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
675 | |
757 | |
676 | if (num_is_fixnum (ret)) |
758 | if (num_is_fixnum (ret)) |
677 | { |
759 | { |
678 | IVALUE av = num_get_ivalue (a); |
|
|
679 | IVALUE bv = num_get_ivalue (b); |
|
|
680 | |
|
|
681 | switch (op) |
760 | switch (op) |
682 | { |
761 | { |
683 | case NUM_ADD: av += bv; break; |
762 | case NUM_ADD: a.ivalue += b.ivalue; break; |
684 | case NUM_SUB: av -= bv; break; |
763 | case NUM_SUB: a.ivalue -= b.ivalue; break; |
685 | case NUM_MUL: av *= bv; break; |
764 | case NUM_MUL: a.ivalue *= b.ivalue; break; |
686 | case NUM_INTDIV: av /= bv; break; |
765 | case NUM_INTDIV: a.ivalue /= b.ivalue; break; |
687 | } |
766 | } |
688 | |
767 | |
689 | num_set_ivalue (ret, av); |
768 | num_set_ivalue (ret, a.ivalue); |
690 | } |
769 | } |
|
|
770 | #if USE_REAL |
691 | else |
771 | else |
692 | { |
772 | { |
693 | RVALUE av = num_get_rvalue (a); |
|
|
694 | RVALUE bv = num_get_rvalue (b); |
|
|
695 | |
|
|
696 | switch (op) |
773 | switch (op) |
697 | { |
774 | { |
698 | case NUM_ADD: av += bv; break; |
775 | case NUM_ADD: a.rvalue += b.rvalue; break; |
699 | case NUM_SUB: av -= bv; break; |
776 | case NUM_SUB: a.rvalue -= b.rvalue; break; |
700 | case NUM_MUL: av *= bv; break; |
777 | case NUM_MUL: a.rvalue *= b.rvalue; break; |
701 | case NUM_INTDIV: av /= bv; break; |
778 | case NUM_INTDIV: a.rvalue /= b.rvalue; break; |
702 | } |
779 | } |
703 | |
780 | |
704 | num_set_rvalue (ret, av); |
781 | num_set_rvalue (ret, a.rvalue); |
705 | } |
782 | } |
|
|
783 | #endif |
706 | |
784 | |
707 | return ret; |
785 | return ret; |
708 | } |
786 | } |
709 | |
787 | |
710 | static num |
788 | static num |
711 | num_div (num a, num b) |
789 | num_div (num a, num b) |
712 | { |
790 | { |
713 | num ret; |
791 | num ret; |
714 | |
792 | |
715 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); |
793 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0); |
716 | |
794 | |
717 | if (num_is_fixnum (ret)) |
795 | if (num_is_fixnum (ret)) |
718 | num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); |
796 | num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b)); |
719 | else |
797 | else |
720 | num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); |
798 | num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); |
721 | |
799 | |
722 | return ret; |
800 | return ret; |
723 | } |
801 | } |
724 | |
802 | |
725 | static num |
803 | static num |
… | |
… | |
727 | { |
805 | { |
728 | num ret; |
806 | num ret; |
729 | long e1, e2, res; |
807 | long e1, e2, res; |
730 | |
808 | |
731 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
809 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
732 | e1 = num_get_ivalue (a); |
810 | e1 = num_ivalue (a); |
733 | e2 = num_get_ivalue (b); |
811 | e2 = num_ivalue (b); |
734 | res = e1 % e2; |
812 | res = e1 % e2; |
735 | |
813 | |
736 | /* remainder should have same sign as second operand */ |
814 | /* remainder should have same sign as second operand */ |
737 | if (res > 0) |
815 | if (res > 0) |
738 | { |
816 | { |
… | |
… | |
754 | { |
832 | { |
755 | num ret; |
833 | num ret; |
756 | long e1, e2, res; |
834 | long e1, e2, res; |
757 | |
835 | |
758 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
836 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
759 | e1 = num_get_ivalue (a); |
837 | e1 = num_ivalue (a); |
760 | e2 = num_get_ivalue (b); |
838 | e2 = num_ivalue (b); |
761 | res = e1 % e2; |
839 | res = e1 % e2; |
762 | |
840 | |
763 | /* modulo should have same sign as second operand */ |
841 | /* modulo should have same sign as second operand */ |
764 | if (res * e2 < 0) |
842 | if (res * e2 < 0) |
765 | res += e2; |
843 | res += e2; |
… | |
… | |
775 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
853 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
776 | int ret; |
854 | int ret; |
777 | |
855 | |
778 | if (is_fixnum) |
856 | if (is_fixnum) |
779 | { |
857 | { |
780 | IVALUE av = num_get_ivalue (a); |
858 | IVALUE av = num_ivalue (a); |
781 | IVALUE bv = num_get_ivalue (b); |
859 | IVALUE bv = num_ivalue (b); |
782 | |
860 | |
783 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
861 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
784 | } |
862 | } |
785 | else |
863 | else |
786 | { |
864 | { |
787 | RVALUE av = num_get_rvalue (a); |
865 | RVALUE av = num_rvalue (a); |
788 | RVALUE bv = num_get_rvalue (b); |
866 | RVALUE bv = num_rvalue (b); |
789 | |
867 | |
790 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
868 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
791 | } |
869 | } |
792 | |
870 | |
793 | return ret; |
871 | return ret; |
794 | } |
872 | } |
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 | |
873 | |
821 | static int |
874 | static int |
822 | is_zero_rvalue (RVALUE x) |
875 | is_zero_rvalue (RVALUE x) |
823 | { |
876 | { |
824 | return x == 0; |
877 | return x == 0; |
… | |
… | |
830 | #endif |
883 | #endif |
831 | #endif |
884 | #endif |
832 | } |
885 | } |
833 | |
886 | |
834 | /* allocate new cell segment */ |
887 | /* allocate new cell segment */ |
835 | static int |
888 | ecb_cold static int |
836 | alloc_cellseg (SCHEME_P_ int n) |
889 | alloc_cellseg (SCHEME_P) |
837 | { |
890 | { |
838 | pointer newp; |
891 | struct cell *newp; |
839 | pointer last; |
892 | struct cell *last; |
840 | pointer p; |
893 | struct cell *p; |
841 | char *cp; |
894 | char *cp; |
842 | long i; |
895 | long i; |
843 | int k; |
896 | int k; |
844 | |
897 | |
845 | static int segsize = CELL_SEGSIZE >> 1; |
898 | static int segsize = CELL_SEGSIZE >> 1; |
846 | segsize <<= 1; |
899 | segsize <<= 1; |
847 | |
900 | |
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)); |
901 | cp = malloc (segsize * sizeof (struct cell)); |
854 | |
902 | |
855 | if (!cp && USE_ERROR_CHECKING) |
903 | if (!cp && USE_ERROR_CHECKING) |
856 | return k; |
904 | return k; |
857 | |
905 | |
858 | i = ++SCHEME_V->last_cell_seg; |
906 | i = ++SCHEME_V->last_cell_seg; |
859 | SCHEME_V->alloc_seg[i] = cp; |
907 | SCHEME_V->alloc_seg[i] = cp; |
860 | |
908 | |
861 | /* insert new segment in address order */ |
909 | newp = (struct cell *)cp; |
862 | newp = (pointer)cp; |
|
|
863 | SCHEME_V->cell_seg[i] = newp; |
910 | SCHEME_V->cell_seg[i] = newp; |
864 | SCHEME_V->cell_segsize[i] = segsize; |
911 | 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; |
912 | SCHEME_V->fcells += segsize; |
881 | last = newp + segsize - 1; |
913 | last = newp + segsize - 1; |
882 | |
914 | |
883 | for (p = newp; p <= last; p++) |
915 | for (p = newp; p <= last; p++) |
884 | { |
916 | { |
|
|
917 | pointer cp = POINTER (p); |
885 | set_typeflag (p, T_FREE); |
918 | set_typeflag (cp, T_PAIR); |
886 | set_car (p, NIL); |
919 | set_car (cp, NIL); |
887 | set_cdr (p, p + 1); |
920 | set_cdr (cp, POINTER (p + 1)); |
888 | } |
921 | } |
889 | |
922 | |
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); |
923 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
894 | SCHEME_V->free_cell = newp; |
924 | SCHEME_V->free_cell = POINTER (newp); |
895 | } |
|
|
896 | else |
|
|
897 | { |
|
|
898 | p = SCHEME_V->free_cell; |
|
|
899 | |
925 | |
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; |
926 | return 1; |
909 | } |
927 | } |
910 | |
928 | |
911 | /* get new cell. parameter a, b is marked by gc. */ |
929 | /* get new cell. parameter a, b is marked by gc. */ |
912 | ecb_inline pointer |
930 | ecb_inline pointer |
913 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
931 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
… | |
… | |
917 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
935 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
918 | return S_SINK; |
936 | return S_SINK; |
919 | |
937 | |
920 | if (SCHEME_V->free_cell == NIL) |
938 | if (SCHEME_V->free_cell == NIL) |
921 | { |
939 | { |
922 | const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; |
940 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1; |
923 | |
941 | |
924 | gc (SCHEME_A_ a, b); |
942 | gc (SCHEME_A_ a, b); |
925 | |
943 | |
926 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
944 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
927 | { |
945 | { |
928 | /* if only a few recovered, get more to avoid fruitless gc's */ |
946 | /* if only a few recovered, get more to avoid fruitless gc's */ |
929 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
947 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
930 | { |
948 | { |
931 | #if USE_ERROR_CHECKING |
949 | #if USE_ERROR_CHECKING |
932 | SCHEME_V->no_memory = 1; |
950 | SCHEME_V->no_memory = 1; |
933 | return S_SINK; |
951 | return S_SINK; |
934 | #endif |
952 | #endif |
… | |
… | |
946 | } |
964 | } |
947 | } |
965 | } |
948 | |
966 | |
949 | /* To retain recent allocs before interpreter knows about them - |
967 | /* To retain recent allocs before interpreter knows about them - |
950 | Tehom */ |
968 | Tehom */ |
951 | |
969 | ecb_hot static void |
952 | static void |
|
|
953 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
970 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
954 | { |
971 | { |
955 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
972 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
956 | |
973 | |
957 | set_typeflag (holder, T_PAIR); |
974 | set_typeflag (holder, T_PAIR); |
… | |
… | |
959 | set_car (holder, recent); |
976 | set_car (holder, recent); |
960 | set_cdr (holder, car (S_SINK)); |
977 | set_cdr (holder, car (S_SINK)); |
961 | set_car (S_SINK, holder); |
978 | set_car (S_SINK, holder); |
962 | } |
979 | } |
963 | |
980 | |
964 | static pointer |
981 | ecb_hot static pointer |
965 | get_cell (SCHEME_P_ pointer a, pointer b) |
982 | get_cell (SCHEME_P_ pointer a, pointer b) |
966 | { |
983 | { |
967 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
984 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
968 | |
985 | |
969 | /* For right now, include "a" and "b" in "cell" so that gc doesn't |
986 | /* For right now, include "a" and "b" in "cell" so that gc doesn't |
… | |
… | |
978 | } |
995 | } |
979 | |
996 | |
980 | static pointer |
997 | static pointer |
981 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
998 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
982 | { |
999 | { |
983 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1000 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
984 | pointer *e = malloc (len * sizeof (pointer)); |
1001 | pointer *e = malloc (len * sizeof (pointer)); |
985 | |
1002 | |
986 | if (!e && USE_ERROR_CHECKING) |
1003 | if (!e && USE_ERROR_CHECKING) |
987 | return S_SINK; |
1004 | return S_SINK; |
988 | |
1005 | |
989 | /* Record it as a vector so that gc understands it. */ |
1006 | /* Record it as a vector so that gc understands it. */ |
990 | set_typeflag (v, T_VECTOR | T_ATOM); |
1007 | set_typeflag (v, T_VECTOR | T_ATOM); |
991 | |
1008 | |
992 | v->object.vector.vvalue = e; |
1009 | CELL(v)->object.vector.vvalue = e; |
993 | v->object.vector.length = len; |
1010 | CELL(v)->object.vector.length = len; |
994 | fill_vector (v, init); |
1011 | fill_vector (v, 0, init); |
995 | push_recent_alloc (SCHEME_A_ v, NIL); |
1012 | push_recent_alloc (SCHEME_A_ v, NIL); |
996 | |
1013 | |
997 | return v; |
1014 | return v; |
998 | } |
1015 | } |
999 | |
1016 | |
… | |
… | |
1007 | static void |
1024 | static void |
1008 | check_cell_alloced (pointer p, int expect_alloced) |
1025 | check_cell_alloced (pointer p, int expect_alloced) |
1009 | { |
1026 | { |
1010 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1027 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1011 | if (typeflag (p) & !expect_alloced) |
1028 | if (typeflag (p) & !expect_alloced) |
1012 | xwrstr ("Cell is already allocated!\n"); |
1029 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1013 | |
1030 | |
1014 | if (!(typeflag (p)) & expect_alloced) |
1031 | if (!(typeflag (p)) & expect_alloced) |
1015 | xwrstr ("Cell is not allocated!\n"); |
1032 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1016 | } |
1033 | } |
1017 | |
1034 | |
1018 | static void |
1035 | static void |
1019 | check_range_alloced (pointer p, int n, int expect_alloced) |
1036 | check_range_alloced (pointer p, int n, int expect_alloced) |
1020 | { |
1037 | { |
… | |
… | |
1026 | #endif |
1043 | #endif |
1027 | |
1044 | |
1028 | /* Medium level cell allocation */ |
1045 | /* Medium level cell allocation */ |
1029 | |
1046 | |
1030 | /* get new cons cell */ |
1047 | /* get new cons cell */ |
1031 | pointer |
1048 | ecb_hot pointer |
1032 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1049 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1033 | { |
1050 | { |
1034 | pointer x = get_cell (SCHEME_A_ a, b); |
1051 | pointer x = get_cell (SCHEME_A_ a, b); |
1035 | |
1052 | |
1036 | set_typeflag (x, T_PAIR); |
1053 | set_typeflag (x, T_PAIR); |
… | |
… | |
1042 | set_cdr (x, b); |
1059 | set_cdr (x, b); |
1043 | |
1060 | |
1044 | return x; |
1061 | return x; |
1045 | } |
1062 | } |
1046 | |
1063 | |
|
|
1064 | ecb_cold static pointer |
|
|
1065 | generate_symbol (SCHEME_P_ const char *name) |
|
|
1066 | { |
|
|
1067 | pointer x = mk_string (SCHEME_A_ name); |
|
|
1068 | setimmutable (x); |
|
|
1069 | set_typeflag (x, T_SYMBOL | T_ATOM); |
|
|
1070 | return x; |
|
|
1071 | } |
|
|
1072 | |
1047 | /* ========== oblist implementation ========== */ |
1073 | /* ========== oblist implementation ========== */ |
1048 | |
1074 | |
1049 | #ifndef USE_OBJECT_LIST |
1075 | #ifndef USE_OBJECT_LIST |
1050 | |
1076 | |
|
|
1077 | static int |
1051 | static int hash_fn (const char *key, int table_size); |
1078 | hash_fn (const char *key, int table_size) |
|
|
1079 | { |
|
|
1080 | const unsigned char *p = key; |
|
|
1081 | uint32_t hash = 2166136261; |
1052 | |
1082 | |
|
|
1083 | while (*p) |
|
|
1084 | hash = (hash ^ *p++) * 16777619; |
|
|
1085 | |
|
|
1086 | return hash % table_size; |
|
|
1087 | } |
|
|
1088 | |
1053 | static pointer |
1089 | ecb_cold static pointer |
1054 | oblist_initial_value (SCHEME_P) |
1090 | oblist_initial_value (SCHEME_P) |
1055 | { |
1091 | { |
1056 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1092 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1057 | } |
1093 | } |
1058 | |
1094 | |
1059 | /* returns the new symbol */ |
1095 | /* returns the new symbol */ |
1060 | static pointer |
1096 | ecb_cold static pointer |
1061 | oblist_add_by_name (SCHEME_P_ const char *name) |
1097 | oblist_add_by_name (SCHEME_P_ const char *name) |
1062 | { |
1098 | { |
1063 | int location; |
1099 | 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)); |
1100 | 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))); |
1101 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1071 | return x; |
1102 | return x; |
1072 | } |
1103 | } |
1073 | |
1104 | |
1074 | ecb_inline pointer |
1105 | ecb_cold static pointer |
1075 | oblist_find_by_name (SCHEME_P_ const char *name) |
1106 | oblist_find_by_name (SCHEME_P_ const char *name) |
1076 | { |
1107 | { |
1077 | int location; |
1108 | int location; |
1078 | pointer x; |
1109 | pointer x; |
1079 | char *s; |
1110 | char *s; |
1080 | |
1111 | |
1081 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1112 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1082 | |
1113 | |
1083 | for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1114 | for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1084 | { |
1115 | { |
1085 | s = symname (car (x)); |
1116 | s = symname (car (x)); |
1086 | |
1117 | |
1087 | /* case-insensitive, per R5RS section 2 */ |
1118 | /* case-insensitive, per R5RS section 2 */ |
1088 | if (stricmp (name, s) == 0) |
1119 | if (stricmp (name, s) == 0) |
… | |
… | |
1090 | } |
1121 | } |
1091 | |
1122 | |
1092 | return NIL; |
1123 | return NIL; |
1093 | } |
1124 | } |
1094 | |
1125 | |
1095 | static pointer |
1126 | ecb_cold static pointer |
1096 | oblist_all_symbols (SCHEME_P) |
1127 | oblist_all_symbols (SCHEME_P) |
1097 | { |
1128 | { |
1098 | int i; |
1129 | int i; |
1099 | pointer x; |
1130 | pointer x; |
1100 | pointer ob_list = NIL; |
1131 | pointer ob_list = NIL; |
1101 | |
1132 | |
1102 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1133 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1103 | for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1134 | for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1104 | ob_list = cons (x, ob_list); |
1135 | ob_list = cons (x, ob_list); |
1105 | |
1136 | |
1106 | return ob_list; |
1137 | return ob_list; |
1107 | } |
1138 | } |
1108 | |
1139 | |
1109 | #else |
1140 | #else |
1110 | |
1141 | |
1111 | static pointer |
1142 | ecb_cold static pointer |
1112 | oblist_initial_value (SCHEME_P) |
1143 | oblist_initial_value (SCHEME_P) |
1113 | { |
1144 | { |
1114 | return NIL; |
1145 | return NIL; |
1115 | } |
1146 | } |
1116 | |
1147 | |
1117 | ecb_inline pointer |
1148 | ecb_cold static pointer |
1118 | oblist_find_by_name (SCHEME_P_ const char *name) |
1149 | oblist_find_by_name (SCHEME_P_ const char *name) |
1119 | { |
1150 | { |
1120 | pointer x; |
1151 | pointer x; |
1121 | char *s; |
1152 | char *s; |
1122 | |
1153 | |
… | |
… | |
1131 | |
1162 | |
1132 | return NIL; |
1163 | return NIL; |
1133 | } |
1164 | } |
1134 | |
1165 | |
1135 | /* returns the new symbol */ |
1166 | /* returns the new symbol */ |
1136 | static pointer |
1167 | ecb_cold static pointer |
1137 | oblist_add_by_name (SCHEME_P_ const char *name) |
1168 | oblist_add_by_name (SCHEME_P_ const char *name) |
1138 | { |
1169 | { |
1139 | pointer x; |
1170 | 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); |
1171 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1145 | return x; |
1172 | return x; |
1146 | } |
1173 | } |
1147 | |
1174 | |
1148 | static pointer |
1175 | ecb_cold static pointer |
1149 | oblist_all_symbols (SCHEME_P) |
1176 | oblist_all_symbols (SCHEME_P) |
1150 | { |
1177 | { |
1151 | return SCHEME_V->oblist; |
1178 | return SCHEME_V->oblist; |
1152 | } |
1179 | } |
1153 | |
1180 | |
1154 | #endif |
1181 | #endif |
1155 | |
1182 | |
1156 | #if USE_PORTS |
1183 | #if USE_PORTS |
1157 | static pointer |
1184 | ecb_cold static pointer |
1158 | mk_port (SCHEME_P_ port *p) |
1185 | mk_port (SCHEME_P_ port *p) |
1159 | { |
1186 | { |
1160 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1187 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1161 | |
1188 | |
1162 | set_typeflag (x, T_PORT | T_ATOM); |
1189 | set_typeflag (x, T_PORT | T_ATOM); |
1163 | x->object.port = p; |
1190 | set_port (x, p); |
1164 | |
1191 | |
1165 | return x; |
1192 | return x; |
1166 | } |
1193 | } |
1167 | #endif |
1194 | #endif |
1168 | |
1195 | |
1169 | pointer |
1196 | ecb_cold pointer |
1170 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1197 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1171 | { |
1198 | { |
1172 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1199 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1173 | |
1200 | |
1174 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1201 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1175 | x->object.ff = f; |
1202 | CELL(x)->object.ff = f; |
1176 | |
1203 | |
1177 | return x; |
1204 | return x; |
1178 | } |
1205 | } |
1179 | |
1206 | |
1180 | INTERFACE pointer |
1207 | INTERFACE pointer |
1181 | mk_character (SCHEME_P_ int c) |
1208 | mk_character (SCHEME_P_ int c) |
1182 | { |
1209 | { |
1183 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1210 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1184 | |
1211 | |
1185 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1212 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1186 | ivalue_unchecked (x) = c & 0xff; |
1213 | set_ivalue (x, c & 0xff); |
1187 | set_num_integer (x); |
1214 | |
1188 | return x; |
1215 | return x; |
1189 | } |
1216 | } |
1190 | |
1217 | |
1191 | /* get number atom (integer) */ |
1218 | /* get number atom (integer) */ |
1192 | INTERFACE pointer |
1219 | INTERFACE pointer |
1193 | mk_integer (SCHEME_P_ long num) |
1220 | mk_integer (SCHEME_P_ long n) |
1194 | { |
1221 | { |
|
|
1222 | pointer p = 0; |
|
|
1223 | pointer *pp = &p; |
|
|
1224 | |
|
|
1225 | #if USE_INTCACHE |
|
|
1226 | if (n >= INTCACHE_MIN && n <= INTCACHE_MAX) |
|
|
1227 | pp = &SCHEME_V->intcache[n - INTCACHE_MIN]; |
|
|
1228 | #endif |
|
|
1229 | |
|
|
1230 | if (!*pp) |
|
|
1231 | { |
1195 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1232 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1196 | |
1233 | |
1197 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1234 | set_typeflag (x, T_INTEGER | T_ATOM); |
1198 | ivalue_unchecked (x) = num; |
1235 | setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */ |
1199 | set_num_integer (x); |
1236 | set_ivalue (x, n); |
|
|
1237 | |
|
|
1238 | *pp = x; |
|
|
1239 | } |
|
|
1240 | |
1200 | return x; |
1241 | return *pp; |
1201 | } |
1242 | } |
1202 | |
1243 | |
1203 | INTERFACE pointer |
1244 | INTERFACE pointer |
1204 | mk_real (SCHEME_P_ RVALUE n) |
1245 | mk_real (SCHEME_P_ RVALUE n) |
1205 | { |
1246 | { |
|
|
1247 | #if USE_REAL |
1206 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1248 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1207 | |
1249 | |
1208 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1250 | set_typeflag (x, T_REAL | T_ATOM); |
1209 | rvalue_unchecked (x) = n; |
1251 | set_rvalue (x, n); |
1210 | set_num_real (x); |
1252 | |
1211 | return x; |
1253 | return x; |
|
|
1254 | #else |
|
|
1255 | return mk_integer (SCHEME_A_ n); |
|
|
1256 | #endif |
1212 | } |
1257 | } |
1213 | |
1258 | |
1214 | static pointer |
1259 | static pointer |
1215 | mk_number (SCHEME_P_ const num n) |
1260 | mk_number (SCHEME_P_ const num n) |
1216 | { |
1261 | { |
|
|
1262 | #if USE_REAL |
1217 | if (num_is_fixnum (n)) |
1263 | return num_is_fixnum (n) |
|
|
1264 | ? mk_integer (SCHEME_A_ num_ivalue (n)) |
|
|
1265 | : mk_real (SCHEME_A_ num_rvalue (n)); |
|
|
1266 | #else |
1218 | return mk_integer (SCHEME_A_ num_get_ivalue (n)); |
1267 | return mk_integer (SCHEME_A_ num_ivalue (n)); |
1219 | else |
1268 | #endif |
1220 | return mk_real (SCHEME_A_ num_get_rvalue (n)); |
|
|
1221 | } |
1269 | } |
1222 | |
1270 | |
1223 | /* allocate name to string area */ |
1271 | /* allocate name to string area */ |
1224 | static char * |
1272 | static char * |
1225 | store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) |
1273 | store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) |
… | |
… | |
1231 | SCHEME_V->no_memory = 1; |
1279 | SCHEME_V->no_memory = 1; |
1232 | return SCHEME_V->strbuff; |
1280 | return SCHEME_V->strbuff; |
1233 | } |
1281 | } |
1234 | |
1282 | |
1235 | if (str) |
1283 | if (str) |
1236 | { |
1284 | 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 |
1285 | else |
1246 | { |
|
|
1247 | memset (q, fill, len_str); |
1286 | memset (q, fill, len_str); |
|
|
1287 | |
1248 | q[len_str] = 0; |
1288 | q[len_str] = 0; |
1249 | } |
|
|
1250 | |
1289 | |
1251 | return q; |
1290 | return q; |
1252 | } |
1291 | } |
1253 | |
1292 | |
1254 | INTERFACE pointer |
1293 | INTERFACE pointer |
… | |
… | |
1268 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1307 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1269 | |
1308 | |
1270 | set_typeflag (x, T_STRING | T_ATOM); |
1309 | set_typeflag (x, T_STRING | T_ATOM); |
1271 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1310 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1272 | strlength (x) = len; |
1311 | strlength (x) = len; |
|
|
1312 | |
1273 | return x; |
1313 | return x; |
1274 | } |
1314 | } |
1275 | |
1315 | |
1276 | INTERFACE pointer |
1316 | INTERFACE pointer |
1277 | mk_string (SCHEME_P_ const char *str) |
1317 | mk_string (SCHEME_P_ const char *str) |
… | |
… | |
1284 | { |
1324 | { |
1285 | return get_vector_object (SCHEME_A_ len, NIL); |
1325 | return get_vector_object (SCHEME_A_ len, NIL); |
1286 | } |
1326 | } |
1287 | |
1327 | |
1288 | INTERFACE void |
1328 | INTERFACE void |
1289 | fill_vector (pointer vec, pointer obj) |
1329 | fill_vector (pointer vec, uint32_t start, pointer obj) |
1290 | { |
1330 | { |
1291 | int i; |
1331 | int i; |
1292 | |
1332 | |
1293 | for (i = 0; i < vec->object.vector.length; i++) |
1333 | for (i = start; i < veclength (vec); i++) |
1294 | vecvalue (vec)[i] = obj; |
1334 | vecvalue (vec)[i] = obj; |
1295 | } |
1335 | } |
1296 | |
1336 | |
|
|
1337 | INTERFACE void |
|
|
1338 | vector_resize (pointer vec, uint32_t newsize, pointer fill) |
|
|
1339 | { |
|
|
1340 | uint32_t oldsize = veclength (vec); |
|
|
1341 | vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer)); |
|
|
1342 | veclength (vec) = newsize; |
|
|
1343 | fill_vector (vec, oldsize, fill); |
|
|
1344 | } |
|
|
1345 | |
1297 | INTERFACE pointer |
1346 | INTERFACE pointer |
1298 | vector_elem (pointer vec, uint32_t ielem) |
1347 | vector_get (pointer vec, uint32_t ielem) |
1299 | { |
1348 | { |
1300 | return vecvalue(vec)[ielem]; |
1349 | return vecvalue(vec)[ielem]; |
1301 | } |
1350 | } |
1302 | |
1351 | |
1303 | INTERFACE void |
1352 | INTERFACE void |
1304 | set_vector_elem (pointer vec, uint32_t ielem, pointer a) |
1353 | vector_set (pointer vec, uint32_t ielem, pointer a) |
1305 | { |
1354 | { |
1306 | vecvalue(vec)[ielem] = a; |
1355 | vecvalue(vec)[ielem] = a; |
1307 | } |
1356 | } |
1308 | |
1357 | |
1309 | /* get new symbol */ |
1358 | /* get new symbol */ |
… | |
… | |
1317 | x = oblist_add_by_name (SCHEME_A_ name); |
1366 | x = oblist_add_by_name (SCHEME_A_ name); |
1318 | |
1367 | |
1319 | return x; |
1368 | return x; |
1320 | } |
1369 | } |
1321 | |
1370 | |
1322 | INTERFACE pointer |
1371 | ecb_cold INTERFACE pointer |
1323 | gensym (SCHEME_P) |
1372 | gensym (SCHEME_P) |
1324 | { |
1373 | { |
1325 | pointer x; |
1374 | pointer x; |
1326 | |
|
|
1327 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
|
|
1328 | { |
|
|
1329 | char name[40] = "gensym-"; |
1375 | char name[40] = "gensym-"; |
1330 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1376 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1331 | |
1377 | |
1332 | /* first check oblist */ |
1378 | return generate_symbol (SCHEME_A_ name); |
1333 | x = oblist_find_by_name (SCHEME_A_ name); |
1379 | } |
1334 | |
1380 | |
1335 | if (x == NIL) |
1381 | static int |
1336 | { |
1382 | is_gensym (SCHEME_P_ pointer x) |
1337 | x = oblist_add_by_name (SCHEME_A_ name); |
1383 | { |
1338 | return x; |
1384 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1339 | } |
|
|
1340 | } |
|
|
1341 | |
|
|
1342 | return NIL; |
|
|
1343 | } |
1385 | } |
1344 | |
1386 | |
1345 | /* make symbol or number atom from string */ |
1387 | /* make symbol or number atom from string */ |
1346 | static pointer |
1388 | ecb_cold static pointer |
1347 | mk_atom (SCHEME_P_ char *q) |
1389 | mk_atom (SCHEME_P_ char *q) |
1348 | { |
1390 | { |
1349 | char c, *p; |
1391 | char c, *p; |
1350 | int has_dec_point = 0; |
1392 | int has_dec_point = 0; |
1351 | int has_fp_exp = 0; |
1393 | int has_fp_exp = 0; |
… | |
… | |
1401 | } |
1443 | } |
1402 | else if ((c == 'e') || (c == 'E')) |
1444 | else if ((c == 'e') || (c == 'E')) |
1403 | { |
1445 | { |
1404 | if (!has_fp_exp) |
1446 | if (!has_fp_exp) |
1405 | { |
1447 | { |
1406 | has_dec_point = 1; /* decimal point illegal |
1448 | has_dec_point = 1; /* decimal point illegal from now on */ |
1407 | from now on */ |
|
|
1408 | p++; |
1449 | p++; |
1409 | |
1450 | |
1410 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1451 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1411 | continue; |
1452 | continue; |
1412 | } |
1453 | } |
… | |
… | |
1423 | |
1464 | |
1424 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1465 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1425 | } |
1466 | } |
1426 | |
1467 | |
1427 | /* make constant */ |
1468 | /* make constant */ |
1428 | static pointer |
1469 | ecb_cold static pointer |
1429 | mk_sharp_const (SCHEME_P_ char *name) |
1470 | mk_sharp_const (SCHEME_P_ char *name) |
1430 | { |
1471 | { |
1431 | if (!strcmp (name, "t")) |
1472 | if (!strcmp (name, "t")) |
1432 | return S_T; |
1473 | return S_T; |
1433 | else if (!strcmp (name, "f")) |
1474 | else if (!strcmp (name, "f")) |
1434 | return S_F; |
1475 | return S_F; |
1435 | else if (*name == '\\') /* #\w (character) */ |
1476 | else if (*name == '\\') /* #\w (character) */ |
1436 | { |
1477 | { |
1437 | int c; |
1478 | int c; |
1438 | |
1479 | |
|
|
1480 | // TODO: optimise |
1439 | if (stricmp (name + 1, "space") == 0) |
1481 | if (stricmp (name + 1, "space") == 0) |
1440 | c = ' '; |
1482 | c = ' '; |
1441 | else if (stricmp (name + 1, "newline") == 0) |
1483 | else if (stricmp (name + 1, "newline") == 0) |
1442 | c = '\n'; |
1484 | c = '\n'; |
1443 | else if (stricmp (name + 1, "return") == 0) |
1485 | else if (stricmp (name + 1, "return") == 0) |
1444 | c = '\r'; |
1486 | c = '\r'; |
1445 | else if (stricmp (name + 1, "tab") == 0) |
1487 | else if (stricmp (name + 1, "tab") == 0) |
1446 | c = '\t'; |
1488 | c = '\t'; |
|
|
1489 | else if (stricmp (name + 1, "alarm") == 0) |
|
|
1490 | c = 0x07; |
|
|
1491 | else if (stricmp (name + 1, "backspace") == 0) |
|
|
1492 | c = 0x08; |
|
|
1493 | else if (stricmp (name + 1, "escape") == 0) |
|
|
1494 | c = 0x1b; |
|
|
1495 | else if (stricmp (name + 1, "delete") == 0) |
|
|
1496 | c = 0x7f; |
|
|
1497 | else if (stricmp (name + 1, "null") == 0) |
|
|
1498 | c = 0; |
1447 | else if (name[1] == 'x' && name[2] != 0) |
1499 | else if (name[1] == 'x' && name[2] != 0) |
1448 | { |
1500 | { |
1449 | long c1 = strtol (name + 2, 0, 16); |
1501 | long c1 = strtol (name + 2, 0, 16); |
1450 | |
1502 | |
1451 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1503 | if (0 <= c1 && c1 <= UCHAR_MAX) |
… | |
… | |
1476 | return NIL; |
1528 | return NIL; |
1477 | } |
1529 | } |
1478 | } |
1530 | } |
1479 | |
1531 | |
1480 | /* ========== garbage collector ========== */ |
1532 | /* ========== garbage collector ========== */ |
|
|
1533 | |
|
|
1534 | static void |
|
|
1535 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1536 | { |
|
|
1537 | /* TODO, fast bitmap check? */ |
|
|
1538 | if (is_string (a) || is_symbol (a)) |
|
|
1539 | free (strvalue (a)); |
|
|
1540 | else if (is_vector (a)) |
|
|
1541 | free (vecvalue (a)); |
|
|
1542 | #if USE_PORTS |
|
|
1543 | else if (is_port (a)) |
|
|
1544 | { |
|
|
1545 | if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) |
|
|
1546 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1547 | |
|
|
1548 | free (port (a)); |
|
|
1549 | } |
|
|
1550 | #endif |
|
|
1551 | } |
1481 | |
1552 | |
1482 | /*-- |
1553 | /*-- |
1483 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1554 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1484 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1555 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1485 | * for marking. |
1556 | * for marking. |
1486 | * |
1557 | * |
1487 | * The exception is vectors - vectors are currently marked recursively, |
1558 | * The exception is vectors - vectors are currently marked recursively, |
1488 | * which is inherited form tinyscheme and could be fixed by having another |
1559 | * which is inherited form tinyscheme and could be fixed by having another |
1489 | * word of context in the vector |
1560 | * word of context in the vector |
1490 | */ |
1561 | */ |
1491 | static void |
1562 | ecb_hot static void |
1492 | mark (pointer a) |
1563 | mark (pointer a) |
1493 | { |
1564 | { |
1494 | pointer t, q, p; |
1565 | pointer t, q, p; |
1495 | |
1566 | |
1496 | t = 0; |
1567 | t = 0; |
… | |
… | |
1500 | |
1571 | |
1501 | if (ecb_expect_false (is_vector (p))) |
1572 | if (ecb_expect_false (is_vector (p))) |
1502 | { |
1573 | { |
1503 | int i; |
1574 | int i; |
1504 | |
1575 | |
1505 | for (i = 0; i < p->object.vector.length; i++) |
1576 | for (i = 0; i < veclength (p); i++) |
1506 | mark (vecvalue (p)[i]); |
1577 | mark (vecvalue (p)[i]); |
1507 | } |
1578 | } |
1508 | |
1579 | |
1509 | if (is_atom (p)) |
1580 | if (is_atom (p)) |
1510 | goto E6; |
1581 | goto E6; |
… | |
… | |
1553 | p = q; |
1624 | p = q; |
1554 | goto E6; |
1625 | goto E6; |
1555 | } |
1626 | } |
1556 | } |
1627 | } |
1557 | |
1628 | |
|
|
1629 | ecb_hot static void |
|
|
1630 | gc_free (SCHEME_P) |
|
|
1631 | { |
|
|
1632 | int i; |
|
|
1633 | uint32_t total = 0; |
|
|
1634 | |
|
|
1635 | /* Here we scan the cells to build the free-list. */ |
|
|
1636 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1637 | { |
|
|
1638 | struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1639 | struct cell *p; |
|
|
1640 | total += SCHEME_V->cell_segsize [i]; |
|
|
1641 | |
|
|
1642 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
|
|
1643 | { |
|
|
1644 | pointer c = POINTER (p); |
|
|
1645 | |
|
|
1646 | if (is_mark (c)) |
|
|
1647 | clrmark (c); |
|
|
1648 | else |
|
|
1649 | { |
|
|
1650 | /* reclaim cell */ |
|
|
1651 | if (typeflag (c) != T_PAIR) |
|
|
1652 | { |
|
|
1653 | finalize_cell (SCHEME_A_ c); |
|
|
1654 | set_typeflag (c, T_PAIR); |
|
|
1655 | set_car (c, NIL); |
|
|
1656 | } |
|
|
1657 | |
|
|
1658 | ++SCHEME_V->fcells; |
|
|
1659 | set_cdr (c, SCHEME_V->free_cell); |
|
|
1660 | SCHEME_V->free_cell = c; |
|
|
1661 | } |
|
|
1662 | } |
|
|
1663 | } |
|
|
1664 | |
|
|
1665 | if (SCHEME_V->gc_verbose) |
|
|
1666 | { |
|
|
1667 | 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"); |
|
|
1668 | } |
|
|
1669 | } |
|
|
1670 | |
1558 | /* garbage collection. parameter a, b is marked. */ |
1671 | /* garbage collection. parameter a, b is marked. */ |
1559 | static void |
1672 | ecb_cold static void |
1560 | gc (SCHEME_P_ pointer a, pointer b) |
1673 | gc (SCHEME_P_ pointer a, pointer b) |
1561 | { |
1674 | { |
1562 | pointer p; |
|
|
1563 | int i; |
1675 | int i; |
1564 | |
1676 | |
1565 | if (SCHEME_V->gc_verbose) |
1677 | if (SCHEME_V->gc_verbose) |
1566 | putstr (SCHEME_A_ "gc..."); |
1678 | putstr (SCHEME_A_ "gc..."); |
1567 | |
1679 | |
… | |
… | |
1583 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1695 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1584 | mark (car (S_SINK)); |
1696 | mark (car (S_SINK)); |
1585 | /* Mark any older stuff above nested C calls */ |
1697 | /* Mark any older stuff above nested C calls */ |
1586 | mark (SCHEME_V->c_nest); |
1698 | mark (SCHEME_V->c_nest); |
1587 | |
1699 | |
|
|
1700 | #if USE_INTCACHE |
|
|
1701 | /* mark intcache */ |
|
|
1702 | for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i) |
|
|
1703 | if (SCHEME_V->intcache[i - INTCACHE_MIN]) |
|
|
1704 | mark (SCHEME_V->intcache[i - INTCACHE_MIN]); |
|
|
1705 | #endif |
|
|
1706 | |
1588 | /* mark variables a, b */ |
1707 | /* mark variables a, b */ |
1589 | mark (a); |
1708 | mark (a); |
1590 | mark (b); |
1709 | mark (b); |
1591 | |
1710 | |
1592 | /* garbage collect */ |
1711 | /* garbage collect */ |
1593 | clrmark (NIL); |
1712 | clrmark (NIL); |
1594 | SCHEME_V->fcells = 0; |
1713 | SCHEME_V->fcells = 0; |
1595 | SCHEME_V->free_cell = NIL; |
1714 | SCHEME_V->free_cell = NIL; |
1596 | |
1715 | |
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) |
1716 | if (SCHEME_V->gc_verbose) |
1628 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
1717 | putstr (SCHEME_A_ "freeing..."); |
1629 | } |
|
|
1630 | |
1718 | |
1631 | static void |
1719 | 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 | } |
1720 | } |
1649 | |
1721 | |
1650 | /* ========== Routines for Reading ========== */ |
1722 | /* ========== Routines for Reading ========== */ |
1651 | |
1723 | |
1652 | static int |
1724 | ecb_cold static int |
1653 | file_push (SCHEME_P_ const char *fname) |
1725 | file_push (SCHEME_P_ const char *fname) |
1654 | { |
1726 | { |
1655 | #if USE_PORTS |
1727 | #if USE_PORTS |
1656 | int fin; |
1728 | int fin; |
1657 | |
1729 | |
… | |
… | |
1666 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1738 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1667 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1739 | 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; |
1740 | 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; |
1741 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1670 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1742 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1671 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1743 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1672 | |
1744 | |
1673 | #if SHOW_ERROR_LINE |
1745 | #if SHOW_ERROR_LINE |
1674 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1746 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1675 | |
1747 | |
1676 | if (fname) |
1748 | if (fname) |
… | |
… | |
1683 | #else |
1755 | #else |
1684 | return 1; |
1756 | return 1; |
1685 | #endif |
1757 | #endif |
1686 | } |
1758 | } |
1687 | |
1759 | |
1688 | static void |
1760 | ecb_cold static void |
1689 | file_pop (SCHEME_P) |
1761 | file_pop (SCHEME_P) |
1690 | { |
1762 | { |
1691 | if (SCHEME_V->file_i != 0) |
1763 | if (SCHEME_V->file_i != 0) |
1692 | { |
1764 | { |
1693 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1765 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1694 | #if USE_PORTS |
1766 | #if USE_PORTS |
1695 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1767 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1696 | #endif |
1768 | #endif |
1697 | SCHEME_V->file_i--; |
1769 | SCHEME_V->file_i--; |
1698 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1770 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1699 | } |
1771 | } |
1700 | } |
1772 | } |
1701 | |
1773 | |
1702 | static int |
1774 | ecb_cold static int |
1703 | file_interactive (SCHEME_P) |
1775 | file_interactive (SCHEME_P) |
1704 | { |
1776 | { |
1705 | #if USE_PORTS |
1777 | #if USE_PORTS |
1706 | return SCHEME_V->file_i == 0 |
1778 | return SCHEME_V->file_i == 0 |
1707 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1779 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1708 | && (SCHEME_V->inport->object.port->kind & port_file); |
1780 | && (port (SCHEME_V->inport)->kind & port_file); |
1709 | #else |
1781 | #else |
1710 | return 0; |
1782 | return 0; |
1711 | #endif |
1783 | #endif |
1712 | } |
1784 | } |
1713 | |
1785 | |
1714 | #if USE_PORTS |
1786 | #if USE_PORTS |
1715 | static port * |
1787 | ecb_cold static port * |
1716 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1788 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1717 | { |
1789 | { |
1718 | int fd; |
1790 | int fd; |
1719 | int flags; |
1791 | int flags; |
1720 | char *rw; |
1792 | char *rw; |
… | |
… | |
1743 | # endif |
1815 | # endif |
1744 | |
1816 | |
1745 | return pt; |
1817 | return pt; |
1746 | } |
1818 | } |
1747 | |
1819 | |
1748 | static pointer |
1820 | ecb_cold static pointer |
1749 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1821 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1750 | { |
1822 | { |
1751 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1823 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1752 | |
1824 | |
1753 | if (!pt && USE_ERROR_CHECKING) |
1825 | if (!pt && USE_ERROR_CHECKING) |
1754 | return NIL; |
1826 | return NIL; |
1755 | |
1827 | |
1756 | return mk_port (SCHEME_A_ pt); |
1828 | return mk_port (SCHEME_A_ pt); |
1757 | } |
1829 | } |
1758 | |
1830 | |
1759 | static port * |
1831 | ecb_cold static port * |
1760 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1832 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1761 | { |
1833 | { |
1762 | port *pt = malloc (sizeof *pt); |
1834 | port *pt = malloc (sizeof *pt); |
1763 | |
1835 | |
1764 | if (!pt && USE_ERROR_CHECKING) |
1836 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1769 | pt->rep.stdio.file = f; |
1841 | pt->rep.stdio.file = f; |
1770 | pt->rep.stdio.closeit = 0; |
1842 | pt->rep.stdio.closeit = 0; |
1771 | return pt; |
1843 | return pt; |
1772 | } |
1844 | } |
1773 | |
1845 | |
1774 | static pointer |
1846 | ecb_cold static pointer |
1775 | port_from_file (SCHEME_P_ int f, int prop) |
1847 | port_from_file (SCHEME_P_ int f, int prop) |
1776 | { |
1848 | { |
1777 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1849 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1778 | |
1850 | |
1779 | if (!pt && USE_ERROR_CHECKING) |
1851 | if (!pt && USE_ERROR_CHECKING) |
1780 | return NIL; |
1852 | return NIL; |
1781 | |
1853 | |
1782 | return mk_port (SCHEME_A_ pt); |
1854 | return mk_port (SCHEME_A_ pt); |
1783 | } |
1855 | } |
1784 | |
1856 | |
1785 | static port * |
1857 | ecb_cold static port * |
1786 | port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1858 | port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1787 | { |
1859 | { |
1788 | port *pt = malloc (sizeof (port)); |
1860 | port *pt = malloc (sizeof (port)); |
1789 | |
1861 | |
1790 | if (!pt && USE_ERROR_CHECKING) |
1862 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1796 | pt->rep.string.curr = start; |
1868 | pt->rep.string.curr = start; |
1797 | pt->rep.string.past_the_end = past_the_end; |
1869 | pt->rep.string.past_the_end = past_the_end; |
1798 | return pt; |
1870 | return pt; |
1799 | } |
1871 | } |
1800 | |
1872 | |
1801 | static pointer |
1873 | ecb_cold static pointer |
1802 | port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1874 | port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1803 | { |
1875 | { |
1804 | port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); |
1876 | port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); |
1805 | |
1877 | |
1806 | if (!pt && USE_ERROR_CHECKING) |
1878 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1809 | return mk_port (SCHEME_A_ pt); |
1881 | return mk_port (SCHEME_A_ pt); |
1810 | } |
1882 | } |
1811 | |
1883 | |
1812 | # define BLOCK_SIZE 256 |
1884 | # define BLOCK_SIZE 256 |
1813 | |
1885 | |
1814 | static port * |
1886 | ecb_cold static port * |
1815 | port_rep_from_scratch (SCHEME_P) |
1887 | port_rep_from_scratch (SCHEME_P) |
1816 | { |
1888 | { |
1817 | char *start; |
1889 | char *start; |
1818 | port *pt = malloc (sizeof (port)); |
1890 | port *pt = malloc (sizeof (port)); |
1819 | |
1891 | |
… | |
… | |
1833 | pt->rep.string.curr = start; |
1905 | pt->rep.string.curr = start; |
1834 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1906 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1835 | return pt; |
1907 | return pt; |
1836 | } |
1908 | } |
1837 | |
1909 | |
1838 | static pointer |
1910 | ecb_cold static pointer |
1839 | port_from_scratch (SCHEME_P) |
1911 | port_from_scratch (SCHEME_P) |
1840 | { |
1912 | { |
1841 | port *pt = port_rep_from_scratch (SCHEME_A); |
1913 | port *pt = port_rep_from_scratch (SCHEME_A); |
1842 | |
1914 | |
1843 | if (!pt && USE_ERROR_CHECKING) |
1915 | if (!pt && USE_ERROR_CHECKING) |
1844 | return NIL; |
1916 | return NIL; |
1845 | |
1917 | |
1846 | return mk_port (SCHEME_A_ pt); |
1918 | return mk_port (SCHEME_A_ pt); |
1847 | } |
1919 | } |
1848 | |
1920 | |
1849 | static void |
1921 | ecb_cold static void |
1850 | port_close (SCHEME_P_ pointer p, int flag) |
1922 | port_close (SCHEME_P_ pointer p, int flag) |
1851 | { |
1923 | { |
1852 | port *pt = p->object.port; |
1924 | port *pt = port (p); |
1853 | |
1925 | |
1854 | pt->kind &= ~flag; |
1926 | pt->kind &= ~flag; |
1855 | |
1927 | |
1856 | if ((pt->kind & (port_input | port_output)) == 0) |
1928 | if ((pt->kind & (port_input | port_output)) == 0) |
1857 | { |
1929 | { |
… | |
… | |
1878 | /* get new character from input file */ |
1950 | /* get new character from input file */ |
1879 | static int |
1951 | static int |
1880 | inchar (SCHEME_P) |
1952 | inchar (SCHEME_P) |
1881 | { |
1953 | { |
1882 | int c; |
1954 | int c; |
1883 | port *pt; |
1955 | port *pt = port (SCHEME_V->inport); |
1884 | |
|
|
1885 | pt = SCHEME_V->inport->object.port; |
|
|
1886 | |
1956 | |
1887 | if (pt->kind & port_saw_EOF) |
1957 | if (pt->kind & port_saw_EOF) |
1888 | return EOF; |
1958 | return EOF; |
1889 | |
1959 | |
1890 | c = basic_inchar (pt); |
1960 | c = basic_inchar (pt); |
… | |
… | |
1957 | port *pt; |
2027 | port *pt; |
1958 | |
2028 | |
1959 | if (c == EOF) |
2029 | if (c == EOF) |
1960 | return; |
2030 | return; |
1961 | |
2031 | |
1962 | pt = SCHEME_V->inport->object.port; |
2032 | pt = port (SCHEME_V->inport); |
1963 | pt->unget = c; |
2033 | pt->unget = c; |
1964 | #else |
2034 | #else |
1965 | if (c == EOF) |
2035 | if (c == EOF) |
1966 | return; |
2036 | return; |
1967 | |
2037 | |
1968 | ungot = c; |
2038 | ungot = c; |
1969 | #endif |
2039 | #endif |
1970 | } |
2040 | } |
1971 | |
2041 | |
1972 | #if USE_PORTS |
2042 | #if USE_PORTS |
1973 | static int |
2043 | ecb_cold static int |
1974 | realloc_port_string (SCHEME_P_ port *p) |
2044 | realloc_port_string (SCHEME_P_ port *p) |
1975 | { |
2045 | { |
1976 | char *start = p->rep.string.start; |
2046 | char *start = p->rep.string.start; |
1977 | size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; |
2047 | size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; |
1978 | char *str = malloc (new_size); |
2048 | char *str = malloc (new_size); |
… | |
… | |
1991 | else |
2061 | else |
1992 | return 0; |
2062 | return 0; |
1993 | } |
2063 | } |
1994 | #endif |
2064 | #endif |
1995 | |
2065 | |
1996 | INTERFACE void |
2066 | ecb_cold INTERFACE void |
1997 | putstr (SCHEME_P_ const char *s) |
2067 | putstr (SCHEME_P_ const char *s) |
1998 | { |
2068 | { |
1999 | #if USE_PORTS |
2069 | #if USE_PORTS |
2000 | port *pt = SCHEME_V->outport->object.port; |
2070 | port *pt = port (SCHEME_V->outport); |
2001 | |
2071 | |
2002 | if (pt->kind & port_file) |
2072 | if (pt->kind & port_file) |
2003 | write (pt->rep.stdio.file, s, strlen (s)); |
2073 | write (pt->rep.stdio.file, s, strlen (s)); |
2004 | else |
2074 | else |
2005 | for (; *s; s++) |
2075 | for (; *s; s++) |
… | |
… | |
2007 | *pt->rep.string.curr++ = *s; |
2077 | *pt->rep.string.curr++ = *s; |
2008 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2078 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2009 | *pt->rep.string.curr++ = *s; |
2079 | *pt->rep.string.curr++ = *s; |
2010 | |
2080 | |
2011 | #else |
2081 | #else |
2012 | xwrstr (s); |
2082 | write (pt->rep.stdio.file, s, strlen (s)); |
2013 | #endif |
2083 | #endif |
2014 | } |
2084 | } |
2015 | |
2085 | |
2016 | static void |
2086 | ecb_cold static void |
2017 | putchars (SCHEME_P_ const char *s, int len) |
2087 | putchars (SCHEME_P_ const char *s, int len) |
2018 | { |
2088 | { |
2019 | #if USE_PORTS |
2089 | #if USE_PORTS |
2020 | port *pt = SCHEME_V->outport->object.port; |
2090 | port *pt = port (SCHEME_V->outport); |
2021 | |
2091 | |
2022 | if (pt->kind & port_file) |
2092 | if (pt->kind & port_file) |
2023 | write (pt->rep.stdio.file, s, len); |
2093 | write (pt->rep.stdio.file, s, len); |
2024 | else |
2094 | else |
2025 | { |
2095 | { |
… | |
… | |
2035 | #else |
2105 | #else |
2036 | write (1, s, len); |
2106 | write (1, s, len); |
2037 | #endif |
2107 | #endif |
2038 | } |
2108 | } |
2039 | |
2109 | |
2040 | INTERFACE void |
2110 | ecb_cold INTERFACE void |
2041 | putcharacter (SCHEME_P_ int c) |
2111 | putcharacter (SCHEME_P_ int c) |
2042 | { |
2112 | { |
2043 | #if USE_PORTS |
2113 | #if USE_PORTS |
2044 | port *pt = SCHEME_V->outport->object.port; |
2114 | port *pt = port (SCHEME_V->outport); |
2045 | |
2115 | |
2046 | if (pt->kind & port_file) |
2116 | if (pt->kind & port_file) |
2047 | { |
2117 | { |
2048 | char cc = c; |
2118 | char cc = c; |
2049 | write (pt->rep.stdio.file, &cc, 1); |
2119 | write (pt->rep.stdio.file, &cc, 1); |
… | |
… | |
2061 | write (1, &c, 1); |
2131 | write (1, &c, 1); |
2062 | #endif |
2132 | #endif |
2063 | } |
2133 | } |
2064 | |
2134 | |
2065 | /* read characters up to delimiter, but cater to character constants */ |
2135 | /* read characters up to delimiter, but cater to character constants */ |
2066 | static char * |
2136 | ecb_cold static char * |
2067 | readstr_upto (SCHEME_P_ char *delim) |
2137 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2068 | { |
2138 | { |
2069 | char *p = SCHEME_V->strbuff; |
2139 | char *p = SCHEME_V->strbuff + skip; |
2070 | |
2140 | |
2071 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2141 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2072 | |
2142 | |
2073 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2143 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2074 | *p = 0; |
2144 | *p = 0; |
… | |
… | |
2080 | |
2150 | |
2081 | return SCHEME_V->strbuff; |
2151 | return SCHEME_V->strbuff; |
2082 | } |
2152 | } |
2083 | |
2153 | |
2084 | /* read string expression "xxx...xxx" */ |
2154 | /* read string expression "xxx...xxx" */ |
2085 | static pointer |
2155 | ecb_cold static pointer |
2086 | readstrexp (SCHEME_P) |
2156 | readstrexp (SCHEME_P_ char delim) |
2087 | { |
2157 | { |
2088 | char *p = SCHEME_V->strbuff; |
2158 | char *p = SCHEME_V->strbuff; |
2089 | int c; |
2159 | int c; |
2090 | int c1 = 0; |
2160 | int c1 = 0; |
2091 | enum |
|
|
2092 | { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2161 | enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2093 | |
2162 | |
2094 | for (;;) |
2163 | for (;;) |
2095 | { |
2164 | { |
2096 | c = inchar (SCHEME_A); |
2165 | c = inchar (SCHEME_A); |
2097 | |
2166 | |
… | |
… | |
2099 | return S_F; |
2168 | return S_F; |
2100 | |
2169 | |
2101 | switch (state) |
2170 | switch (state) |
2102 | { |
2171 | { |
2103 | case st_ok: |
2172 | case st_ok: |
2104 | switch (c) |
2173 | 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); |
2174 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2113 | |
2175 | |
2114 | default: |
2176 | if (ecb_expect_false (c == '\\')) |
|
|
2177 | state = st_bsl; |
|
|
2178 | else |
2115 | *p++ = c; |
2179 | *p++ = c; |
2116 | break; |
|
|
2117 | } |
|
|
2118 | |
2180 | |
2119 | break; |
2181 | break; |
2120 | |
2182 | |
2121 | case st_bsl: |
2183 | case st_bsl: |
2122 | switch (c) |
2184 | switch (c) |
… | |
… | |
2131 | case '7': |
2193 | case '7': |
2132 | state = st_oct1; |
2194 | state = st_oct1; |
2133 | c1 = c - '0'; |
2195 | c1 = c - '0'; |
2134 | break; |
2196 | break; |
2135 | |
2197 | |
|
|
2198 | case 'a': *p++ = '\a'; state = st_ok; break; |
|
|
2199 | case 'n': *p++ = '\n'; state = st_ok; break; |
|
|
2200 | case 'r': *p++ = '\r'; state = st_ok; break; |
|
|
2201 | case 't': *p++ = '\t'; state = st_ok; break; |
|
|
2202 | |
|
|
2203 | //TODO: \whitespace eol whitespace |
|
|
2204 | |
|
|
2205 | //TODO: x should end in ;, not two-digit hex |
2136 | case 'x': |
2206 | case 'x': |
2137 | case 'X': |
2207 | case 'X': |
2138 | state = st_x1; |
2208 | state = st_x1; |
2139 | c1 = 0; |
2209 | c1 = 0; |
2140 | break; |
2210 | break; |
2141 | |
2211 | |
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: |
2212 | default: |
2163 | *p++ = c; |
2213 | *p++ = c; |
2164 | state = st_ok; |
2214 | state = st_ok; |
2165 | break; |
2215 | break; |
2166 | } |
2216 | } |
2167 | |
2217 | |
2168 | break; |
2218 | break; |
2169 | |
2219 | |
2170 | case st_x1: |
2220 | case st_x1: |
2171 | case st_x2: |
2221 | case st_x2: |
2172 | c = toupper (c); |
2222 | c = tolower (c); |
2173 | |
2223 | |
2174 | if (c >= '0' && c <= 'F') |
2224 | if (c >= '0' && c <= '9') |
2175 | { |
|
|
2176 | if (c <= '9') |
|
|
2177 | c1 = (c1 << 4) + c - '0'; |
2225 | c1 = (c1 << 4) + c - '0'; |
2178 | else |
2226 | else if (c >= 'a' && c <= 'f') |
2179 | c1 = (c1 << 4) + c - 'A' + 10; |
2227 | 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 |
2228 | else |
2190 | return S_F; |
2229 | return S_F; |
|
|
2230 | |
|
|
2231 | if (state == st_x1) |
|
|
2232 | state = st_x2; |
|
|
2233 | else |
|
|
2234 | { |
|
|
2235 | *p++ = c1; |
|
|
2236 | state = st_ok; |
|
|
2237 | } |
2191 | |
2238 | |
2192 | break; |
2239 | break; |
2193 | |
2240 | |
2194 | case st_oct1: |
2241 | case st_oct1: |
2195 | case st_oct2: |
2242 | case st_oct2: |
… | |
… | |
2199 | backchar (SCHEME_A_ c); |
2246 | backchar (SCHEME_A_ c); |
2200 | state = st_ok; |
2247 | state = st_ok; |
2201 | } |
2248 | } |
2202 | else |
2249 | else |
2203 | { |
2250 | { |
2204 | if (state == st_oct2 && c1 >= 32) |
2251 | if (state == st_oct2 && c1 >= ' ') |
2205 | return S_F; |
2252 | return S_F; |
2206 | |
2253 | |
2207 | c1 = (c1 << 3) + (c - '0'); |
2254 | c1 = (c1 << 3) + (c - '0'); |
2208 | |
2255 | |
2209 | if (state == st_oct1) |
2256 | if (state == st_oct1) |
… | |
… | |
2214 | state = st_ok; |
2261 | state = st_ok; |
2215 | } |
2262 | } |
2216 | } |
2263 | } |
2217 | |
2264 | |
2218 | break; |
2265 | break; |
2219 | |
|
|
2220 | } |
2266 | } |
2221 | } |
2267 | } |
2222 | } |
2268 | } |
2223 | |
2269 | |
2224 | /* check c is in chars */ |
2270 | /* check c is in chars */ |
2225 | ecb_inline int |
2271 | ecb_cold int |
2226 | is_one_of (char *s, int c) |
2272 | is_one_of (const char *s, int c) |
2227 | { |
2273 | { |
2228 | if (c == EOF) |
|
|
2229 | return 1; |
|
|
2230 | |
|
|
2231 | return !!strchr (s, c); |
2274 | return c == EOF || !!strchr (s, c); |
2232 | } |
2275 | } |
2233 | |
2276 | |
2234 | /* skip white characters */ |
2277 | /* skip white characters */ |
2235 | ecb_inline int |
2278 | ecb_cold int |
2236 | skipspace (SCHEME_P) |
2279 | skipspace (SCHEME_P) |
2237 | { |
2280 | { |
2238 | int c, curr_line = 0; |
2281 | int c, curr_line = 0; |
2239 | |
2282 | |
2240 | do |
2283 | do |
2241 | { |
2284 | { |
2242 | c = inchar (SCHEME_A); |
2285 | c = inchar (SCHEME_A); |
|
|
2286 | |
2243 | #if SHOW_ERROR_LINE |
2287 | #if SHOW_ERROR_LINE |
2244 | if (c == '\n') |
2288 | if (ecb_expect_false (c == '\n')) |
2245 | curr_line++; |
2289 | curr_line++; |
2246 | #endif |
2290 | #endif |
|
|
2291 | |
|
|
2292 | if (ecb_expect_false (c == EOF)) |
|
|
2293 | return c; |
2247 | } |
2294 | } |
2248 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2295 | while (is_one_of (WHITESPACE, c)); |
2249 | |
2296 | |
2250 | /* record it */ |
2297 | /* record it */ |
2251 | #if SHOW_ERROR_LINE |
2298 | #if SHOW_ERROR_LINE |
2252 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2299 | 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; |
2300 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2254 | #endif |
2301 | #endif |
2255 | |
2302 | |
2256 | if (c != EOF) |
|
|
2257 | { |
|
|
2258 | backchar (SCHEME_A_ c); |
2303 | backchar (SCHEME_A_ c); |
2259 | return 1; |
2304 | return 1; |
2260 | } |
|
|
2261 | else |
|
|
2262 | return EOF; |
|
|
2263 | } |
2305 | } |
2264 | |
2306 | |
2265 | /* get token */ |
2307 | /* get token */ |
2266 | static int |
2308 | ecb_cold static int |
2267 | token (SCHEME_P) |
2309 | token (SCHEME_P) |
2268 | { |
2310 | { |
2269 | int c = skipspace (SCHEME_A); |
2311 | int c = skipspace (SCHEME_A); |
2270 | |
2312 | |
2271 | if (c == EOF) |
2313 | if (c == EOF) |
… | |
… | |
2283 | return TOK_RPAREN; |
2325 | return TOK_RPAREN; |
2284 | |
2326 | |
2285 | case '.': |
2327 | case '.': |
2286 | c = inchar (SCHEME_A); |
2328 | c = inchar (SCHEME_A); |
2287 | |
2329 | |
2288 | if (is_one_of (" \n\t", c)) |
2330 | if (is_one_of (WHITESPACE, c)) |
2289 | return TOK_DOT; |
2331 | return TOK_DOT; |
2290 | else |
2332 | else |
2291 | { |
2333 | { |
2292 | //TODO: ungetc twice in a row is not supported in C |
|
|
2293 | backchar (SCHEME_A_ c); |
2334 | backchar (SCHEME_A_ c); |
2294 | backchar (SCHEME_A_ '.'); |
|
|
2295 | return TOK_ATOM; |
2335 | return TOK_DOTATOM; |
2296 | } |
2336 | } |
|
|
2337 | |
|
|
2338 | case '|': |
|
|
2339 | return TOK_STRATOM; |
2297 | |
2340 | |
2298 | case '\'': |
2341 | case '\'': |
2299 | return TOK_QUOTE; |
2342 | return TOK_QUOTE; |
2300 | |
2343 | |
2301 | case ';': |
2344 | case ';': |
… | |
… | |
2368 | } |
2411 | } |
2369 | |
2412 | |
2370 | /* ========== Routines for Printing ========== */ |
2413 | /* ========== Routines for Printing ========== */ |
2371 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2414 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2372 | |
2415 | |
2373 | static void |
2416 | ecb_cold static void |
2374 | printslashstring (SCHEME_P_ char *p, int len) |
2417 | printslashstring (SCHEME_P_ char *p, int len) |
2375 | { |
2418 | { |
2376 | int i; |
2419 | int i; |
2377 | unsigned char *s = (unsigned char *) p; |
2420 | unsigned char *s = (unsigned char *) p; |
2378 | |
2421 | |
… | |
… | |
2433 | } |
2476 | } |
2434 | |
2477 | |
2435 | putcharacter (SCHEME_A_ '"'); |
2478 | putcharacter (SCHEME_A_ '"'); |
2436 | } |
2479 | } |
2437 | |
2480 | |
2438 | |
|
|
2439 | /* print atoms */ |
2481 | /* print atoms */ |
2440 | static void |
2482 | ecb_cold static void |
2441 | printatom (SCHEME_P_ pointer l, int f) |
2483 | printatom (SCHEME_P_ pointer l, int f) |
2442 | { |
2484 | { |
2443 | char *p; |
2485 | char *p; |
2444 | int len; |
2486 | int len; |
2445 | |
2487 | |
2446 | atom2str (SCHEME_A_ l, f, &p, &len); |
2488 | atom2str (SCHEME_A_ l, f, &p, &len); |
2447 | putchars (SCHEME_A_ p, len); |
2489 | putchars (SCHEME_A_ p, len); |
2448 | } |
2490 | } |
2449 | |
2491 | |
2450 | |
|
|
2451 | /* Uses internal buffer unless string pointer is already available */ |
2492 | /* Uses internal buffer unless string pointer is already available */ |
2452 | static void |
2493 | ecb_cold static void |
2453 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2494 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2454 | { |
2495 | { |
2455 | char *p; |
2496 | char *p; |
2456 | |
2497 | |
2457 | if (l == NIL) |
2498 | if (l == NIL) |
… | |
… | |
2615 | #endif |
2656 | #endif |
2616 | } |
2657 | } |
2617 | else if (is_continuation (l)) |
2658 | else if (is_continuation (l)) |
2618 | p = "#<CONTINUATION>"; |
2659 | p = "#<CONTINUATION>"; |
2619 | else |
2660 | else |
|
|
2661 | { |
|
|
2662 | #if USE_PRINTF |
|
|
2663 | p = SCHEME_V->strbuff; |
|
|
2664 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2665 | #else |
2620 | p = "#<ERROR>"; |
2666 | p = "#<ERROR>"; |
|
|
2667 | #endif |
|
|
2668 | } |
2621 | |
2669 | |
2622 | *pp = p; |
2670 | *pp = p; |
2623 | *plen = strlen (p); |
2671 | *plen = strlen (p); |
2624 | } |
2672 | } |
2625 | |
2673 | |
… | |
… | |
2657 | return car (d); |
2705 | return car (d); |
2658 | |
2706 | |
2659 | p = cons (car (d), cdr (d)); |
2707 | p = cons (car (d), cdr (d)); |
2660 | q = p; |
2708 | q = p; |
2661 | |
2709 | |
2662 | while (cdr (cdr (p)) != NIL) |
2710 | while (cddr (p) != NIL) |
2663 | { |
2711 | { |
2664 | d = cons (car (p), cdr (p)); |
2712 | d = cons (car (p), cdr (p)); |
2665 | |
2713 | |
2666 | if (cdr (cdr (p)) != NIL) |
2714 | if (cddr (p) != NIL) |
2667 | p = cdr (d); |
2715 | p = cdr (d); |
2668 | } |
2716 | } |
2669 | |
2717 | |
2670 | set_cdr (p, car (cdr (p))); |
2718 | set_cdr (p, cadr (p)); |
2671 | return q; |
2719 | return q; |
2672 | } |
2720 | } |
2673 | |
2721 | |
2674 | /* reverse list -- produce new list */ |
2722 | /* reverse list -- produce new list */ |
2675 | static pointer |
2723 | ecb_hot static pointer |
2676 | reverse (SCHEME_P_ pointer a) |
2724 | reverse (SCHEME_P_ pointer a) |
2677 | { |
2725 | { |
2678 | /* a must be checked by gc */ |
2726 | /* a must be checked by gc */ |
2679 | pointer p = NIL; |
2727 | pointer p = NIL; |
2680 | |
2728 | |
… | |
… | |
2683 | |
2731 | |
2684 | return p; |
2732 | return p; |
2685 | } |
2733 | } |
2686 | |
2734 | |
2687 | /* reverse list --- in-place */ |
2735 | /* reverse list --- in-place */ |
2688 | static pointer |
2736 | ecb_hot static pointer |
2689 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2737 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2690 | { |
2738 | { |
2691 | pointer result = term; |
2739 | pointer result = term; |
2692 | pointer p = list; |
2740 | pointer p = list; |
2693 | |
2741 | |
… | |
… | |
2701 | |
2749 | |
2702 | return result; |
2750 | return result; |
2703 | } |
2751 | } |
2704 | |
2752 | |
2705 | /* append list -- produce new list (in reverse order) */ |
2753 | /* append list -- produce new list (in reverse order) */ |
2706 | static pointer |
2754 | ecb_hot static pointer |
2707 | revappend (SCHEME_P_ pointer a, pointer b) |
2755 | revappend (SCHEME_P_ pointer a, pointer b) |
2708 | { |
2756 | { |
2709 | pointer result = a; |
2757 | pointer result = a; |
2710 | pointer p = b; |
2758 | pointer p = b; |
2711 | |
2759 | |
… | |
… | |
2720 | |
2768 | |
2721 | return S_F; /* signal an error */ |
2769 | return S_F; /* signal an error */ |
2722 | } |
2770 | } |
2723 | |
2771 | |
2724 | /* equivalence of atoms */ |
2772 | /* equivalence of atoms */ |
2725 | int |
2773 | ecb_hot int |
2726 | eqv (pointer a, pointer b) |
2774 | eqv (pointer a, pointer b) |
2727 | { |
2775 | { |
2728 | if (is_string (a)) |
2776 | if (is_string (a)) |
2729 | { |
2777 | { |
2730 | if (is_string (b)) |
2778 | if (is_string (b)) |
… | |
… | |
2769 | /* () is #t in R5RS */ |
2817 | /* () is #t in R5RS */ |
2770 | #define is_true(p) ((p) != S_F) |
2818 | #define is_true(p) ((p) != S_F) |
2771 | #define is_false(p) ((p) == S_F) |
2819 | #define is_false(p) ((p) == S_F) |
2772 | |
2820 | |
2773 | /* ========== Environment implementation ========== */ |
2821 | /* ========== 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 | |
2822 | |
2790 | #ifndef USE_ALIST_ENV |
2823 | #ifndef USE_ALIST_ENV |
2791 | |
2824 | |
2792 | /* |
2825 | /* |
2793 | * In this implementation, each frame of the environment may be |
2826 | * In this implementation, each frame of the environment may be |
… | |
… | |
2810 | |
2843 | |
2811 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2844 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2812 | setenvironment (SCHEME_V->envir); |
2845 | setenvironment (SCHEME_V->envir); |
2813 | } |
2846 | } |
2814 | |
2847 | |
|
|
2848 | static uint32_t |
|
|
2849 | sym_hash (pointer sym, uint32_t size) |
|
|
2850 | { |
|
|
2851 | uintptr_t ptr = (uintptr_t)sym; |
|
|
2852 | |
|
|
2853 | #if 0 |
|
|
2854 | /* table size is prime, so why mix */ |
|
|
2855 | ptr += ptr >> 32; |
|
|
2856 | ptr += ptr >> 16; |
|
|
2857 | ptr += ptr >> 8; |
|
|
2858 | #endif |
|
|
2859 | |
|
|
2860 | return ptr % size; |
|
|
2861 | } |
|
|
2862 | |
2815 | ecb_inline void |
2863 | ecb_inline void |
2816 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2864 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2817 | { |
2865 | { |
2818 | pointer slot = immutable_cons (variable, value); |
2866 | pointer slot = immutable_cons (variable, value); |
2819 | |
2867 | |
2820 | if (is_vector (car (env))) |
2868 | if (is_vector (car (env))) |
2821 | { |
2869 | { |
2822 | int location = hash_fn (symname (variable), veclength (car (env))); |
2870 | int location = sym_hash (variable, veclength (car (env))); |
2823 | |
|
|
2824 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
2871 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2825 | } |
2872 | } |
2826 | else |
2873 | else |
2827 | set_car (env, immutable_cons (slot, car (env))); |
2874 | set_car (env, immutable_cons (slot, car (env))); |
2828 | } |
2875 | } |
2829 | |
2876 | |
2830 | static pointer |
2877 | ecb_hot static pointer |
2831 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2878 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2832 | { |
2879 | { |
2833 | pointer x, y; |
2880 | pointer x, y; |
2834 | int location; |
|
|
2835 | |
2881 | |
2836 | for (x = env; x != NIL; x = cdr (x)) |
2882 | for (x = env; x != NIL; x = cdr (x)) |
2837 | { |
2883 | { |
2838 | if (is_vector (car (x))) |
2884 | if (is_vector (car (x))) |
2839 | { |
2885 | { |
2840 | location = hash_fn (symname (hdl), veclength (car (x))); |
2886 | int location = sym_hash (hdl, veclength (car (x))); |
2841 | y = vector_elem (car (x), location); |
2887 | y = vector_get (car (x), location); |
2842 | } |
2888 | } |
2843 | else |
2889 | else |
2844 | y = car (x); |
2890 | y = car (x); |
2845 | |
2891 | |
2846 | for (; y != NIL; y = cdr (y)) |
2892 | for (; y != NIL; y = cdr (y)) |
2847 | if (caar (y) == hdl) |
2893 | if (caar (y) == hdl) |
2848 | break; |
2894 | break; |
2849 | |
2895 | |
2850 | if (y != NIL) |
2896 | if (y != NIL) |
|
|
2897 | return car (y); |
|
|
2898 | |
|
|
2899 | if (!all) |
2851 | break; |
2900 | break; |
2852 | |
|
|
2853 | if (!all) |
|
|
2854 | return NIL; |
|
|
2855 | } |
2901 | } |
2856 | |
|
|
2857 | if (x != NIL) |
|
|
2858 | return car (y); |
|
|
2859 | |
2902 | |
2860 | return NIL; |
2903 | return NIL; |
2861 | } |
2904 | } |
2862 | |
2905 | |
2863 | #else /* USE_ALIST_ENV */ |
2906 | #else /* USE_ALIST_ENV */ |
2864 | |
2907 | |
2865 | ecb_inline void |
2908 | static void |
2866 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2909 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2867 | { |
2910 | { |
2868 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2911 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2869 | setenvironment (SCHEME_V->envir); |
2912 | setenvironment (SCHEME_V->envir); |
2870 | } |
2913 | } |
2871 | |
2914 | |
2872 | ecb_inline void |
2915 | static void |
2873 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2916 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2874 | { |
2917 | { |
2875 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2918 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2876 | } |
2919 | } |
2877 | |
2920 | |
2878 | static pointer |
2921 | ecb_hot static pointer |
2879 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2922 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2880 | { |
2923 | { |
2881 | pointer x, y; |
2924 | pointer x, y; |
2882 | |
2925 | |
2883 | for (x = env; x != NIL; x = cdr (x)) |
2926 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2885 | for (y = car (x); y != NIL; y = cdr (y)) |
2928 | for (y = car (x); y != NIL; y = cdr (y)) |
2886 | if (caar (y) == hdl) |
2929 | if (caar (y) == hdl) |
2887 | break; |
2930 | break; |
2888 | |
2931 | |
2889 | if (y != NIL) |
2932 | if (y != NIL) |
|
|
2933 | return car (y); |
2890 | break; |
2934 | break; |
2891 | |
2935 | |
2892 | if (!all) |
2936 | if (!all) |
2893 | return NIL; |
2937 | break; |
2894 | } |
2938 | } |
2895 | |
|
|
2896 | if (x != NIL) |
|
|
2897 | return car (y); |
|
|
2898 | |
2939 | |
2899 | return NIL; |
2940 | return NIL; |
2900 | } |
2941 | } |
2901 | |
2942 | |
2902 | #endif /* USE_ALIST_ENV else */ |
2943 | #endif /* USE_ALIST_ENV else */ |
2903 | |
2944 | |
2904 | ecb_inline void |
2945 | static void |
2905 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2946 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2906 | { |
2947 | { |
|
|
2948 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2907 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2949 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2908 | } |
2950 | } |
2909 | |
2951 | |
2910 | ecb_inline void |
2952 | static void |
2911 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2953 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2912 | { |
2954 | { |
2913 | set_cdr (slot, value); |
2955 | set_cdr (slot, value); |
2914 | } |
2956 | } |
2915 | |
2957 | |
2916 | ecb_inline pointer |
2958 | static pointer |
2917 | slot_value_in_env (pointer slot) |
2959 | slot_value_in_env (pointer slot) |
2918 | { |
2960 | { |
2919 | return cdr (slot); |
2961 | return cdr (slot); |
2920 | } |
2962 | } |
2921 | |
2963 | |
2922 | /* ========== Evaluation Cycle ========== */ |
2964 | /* ========== Evaluation Cycle ========== */ |
2923 | |
2965 | |
2924 | static int |
2966 | ecb_cold static int |
2925 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2967 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2926 | { |
2968 | { |
2927 | #if USE_ERROR_HOOK |
2969 | #if USE_ERROR_HOOK |
2928 | pointer x; |
2970 | pointer x; |
2929 | pointer hdl = SCHEME_V->ERROR_HOOK; |
2971 | pointer hdl = SCHEME_V->ERROR_HOOK; |
… | |
… | |
3005 | pointer code; |
3047 | pointer code; |
3006 | }; |
3048 | }; |
3007 | |
3049 | |
3008 | # define STACK_GROWTH 3 |
3050 | # define STACK_GROWTH 3 |
3009 | |
3051 | |
3010 | static void |
3052 | ecb_hot static void |
3011 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3053 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3012 | { |
3054 | { |
3013 | int nframes = (uintptr_t)SCHEME_V->dump; |
3055 | int nframes = (uintptr_t)SCHEME_V->dump; |
3014 | struct dump_stack_frame *next_frame; |
3056 | struct dump_stack_frame *next_frame; |
3015 | |
3057 | |
3016 | /* enough room for the next frame? */ |
3058 | /* enough room for the next frame? */ |
3017 | if (nframes >= SCHEME_V->dump_size) |
3059 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3018 | { |
3060 | { |
3019 | SCHEME_V->dump_size += STACK_GROWTH; |
3061 | 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); |
3062 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3021 | } |
3063 | } |
3022 | |
3064 | |
… | |
… | |
3028 | next_frame->code = code; |
3070 | next_frame->code = code; |
3029 | |
3071 | |
3030 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3072 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3031 | } |
3073 | } |
3032 | |
3074 | |
3033 | static int |
3075 | static ecb_hot int |
3034 | xs_return (SCHEME_P_ pointer a) |
3076 | xs_return (SCHEME_P_ pointer a) |
3035 | { |
3077 | { |
3036 | int nframes = (uintptr_t)SCHEME_V->dump; |
3078 | int nframes = (uintptr_t)SCHEME_V->dump; |
3037 | struct dump_stack_frame *frame; |
3079 | struct dump_stack_frame *frame; |
3038 | |
3080 | |
… | |
… | |
3049 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3091 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3050 | |
3092 | |
3051 | return 0; |
3093 | return 0; |
3052 | } |
3094 | } |
3053 | |
3095 | |
3054 | ecb_inline void |
3096 | ecb_cold void |
3055 | dump_stack_reset (SCHEME_P) |
3097 | dump_stack_reset (SCHEME_P) |
3056 | { |
3098 | { |
3057 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3099 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3058 | SCHEME_V->dump = (pointer)+0; |
3100 | SCHEME_V->dump = (pointer)+0; |
3059 | } |
3101 | } |
3060 | |
3102 | |
3061 | ecb_inline void |
3103 | ecb_cold void |
3062 | dump_stack_initialize (SCHEME_P) |
3104 | dump_stack_initialize (SCHEME_P) |
3063 | { |
3105 | { |
3064 | SCHEME_V->dump_size = 0; |
3106 | SCHEME_V->dump_size = 0; |
3065 | SCHEME_V->dump_base = 0; |
3107 | SCHEME_V->dump_base = 0; |
3066 | dump_stack_reset (SCHEME_A); |
3108 | dump_stack_reset (SCHEME_A); |
3067 | } |
3109 | } |
3068 | |
3110 | |
3069 | static void |
3111 | ecb_cold static void |
3070 | dump_stack_free (SCHEME_P) |
3112 | dump_stack_free (SCHEME_P) |
3071 | { |
3113 | { |
3072 | free (SCHEME_V->dump_base); |
3114 | free (SCHEME_V->dump_base); |
3073 | SCHEME_V->dump_base = 0; |
3115 | SCHEME_V->dump_base = 0; |
3074 | SCHEME_V->dump = (pointer)0; |
3116 | SCHEME_V->dump = (pointer)0; |
3075 | SCHEME_V->dump_size = 0; |
3117 | SCHEME_V->dump_size = 0; |
3076 | } |
3118 | } |
3077 | |
3119 | |
3078 | static void |
3120 | ecb_cold static void |
3079 | dump_stack_mark (SCHEME_P) |
3121 | dump_stack_mark (SCHEME_P) |
3080 | { |
3122 | { |
3081 | int nframes = (uintptr_t)SCHEME_V->dump; |
3123 | int nframes = (uintptr_t)SCHEME_V->dump; |
3082 | int i; |
3124 | int i; |
3083 | |
3125 | |
… | |
… | |
3089 | mark (frame->envir); |
3131 | mark (frame->envir); |
3090 | mark (frame->code); |
3132 | mark (frame->code); |
3091 | } |
3133 | } |
3092 | } |
3134 | } |
3093 | |
3135 | |
3094 | static pointer |
3136 | ecb_cold static pointer |
3095 | ss_get_cont (SCHEME_P) |
3137 | ss_get_cont (SCHEME_P) |
3096 | { |
3138 | { |
3097 | int nframes = (uintptr_t)SCHEME_V->dump; |
3139 | int nframes = (uintptr_t)SCHEME_V->dump; |
3098 | int i; |
3140 | int i; |
3099 | |
3141 | |
… | |
… | |
3111 | } |
3153 | } |
3112 | |
3154 | |
3113 | return cont; |
3155 | return cont; |
3114 | } |
3156 | } |
3115 | |
3157 | |
3116 | static void |
3158 | ecb_cold static void |
3117 | ss_set_cont (SCHEME_P_ pointer cont) |
3159 | ss_set_cont (SCHEME_P_ pointer cont) |
3118 | { |
3160 | { |
3119 | int i = 0; |
3161 | int i = 0; |
3120 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3162 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3121 | |
3163 | |
3122 | while (cont != NIL) |
3164 | while (cont != NIL) |
3123 | { |
3165 | { |
3124 | frame->op = ivalue (car (cont)); cont = cdr (cont); |
3166 | frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont); |
3125 | frame->args = car (cont) ; cont = cdr (cont); |
3167 | frame->args = car (cont) ; cont = cdr (cont); |
3126 | frame->envir = car (cont) ; cont = cdr (cont); |
3168 | frame->envir = car (cont) ; cont = cdr (cont); |
3127 | frame->code = car (cont) ; cont = cdr (cont); |
3169 | frame->code = car (cont) ; cont = cdr (cont); |
3128 | |
3170 | |
3129 | ++frame; |
3171 | ++frame; |
3130 | ++i; |
3172 | ++i; |
3131 | } |
3173 | } |
3132 | |
3174 | |
3133 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3175 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3134 | } |
3176 | } |
3135 | |
3177 | |
3136 | #else |
3178 | #else |
3137 | |
3179 | |
3138 | ecb_inline void |
3180 | ecb_cold void |
3139 | dump_stack_reset (SCHEME_P) |
3181 | dump_stack_reset (SCHEME_P) |
3140 | { |
3182 | { |
3141 | SCHEME_V->dump = NIL; |
3183 | SCHEME_V->dump = NIL; |
3142 | } |
3184 | } |
3143 | |
3185 | |
3144 | ecb_inline void |
3186 | ecb_cold void |
3145 | dump_stack_initialize (SCHEME_P) |
3187 | dump_stack_initialize (SCHEME_P) |
3146 | { |
3188 | { |
3147 | dump_stack_reset (SCHEME_A); |
3189 | dump_stack_reset (SCHEME_A); |
3148 | } |
3190 | } |
3149 | |
3191 | |
3150 | static void |
3192 | ecb_cold static void |
3151 | dump_stack_free (SCHEME_P) |
3193 | dump_stack_free (SCHEME_P) |
3152 | { |
3194 | { |
3153 | SCHEME_V->dump = NIL; |
3195 | SCHEME_V->dump = NIL; |
3154 | } |
3196 | } |
3155 | |
3197 | |
3156 | static int |
3198 | ecb_hot static int |
3157 | xs_return (SCHEME_P_ pointer a) |
3199 | xs_return (SCHEME_P_ pointer a) |
3158 | { |
3200 | { |
3159 | pointer dump = SCHEME_V->dump; |
3201 | pointer dump = SCHEME_V->dump; |
3160 | |
3202 | |
3161 | SCHEME_V->value = a; |
3203 | SCHEME_V->value = a; |
3162 | |
3204 | |
3163 | if (dump == NIL) |
3205 | if (dump == NIL) |
3164 | return -1; |
3206 | return -1; |
3165 | |
3207 | |
3166 | SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); |
3208 | SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump); |
3167 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3209 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3168 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3210 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3169 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3211 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3170 | |
3212 | |
3171 | SCHEME_V->dump = dump; |
3213 | SCHEME_V->dump = dump; |
3172 | |
3214 | |
3173 | return 0; |
3215 | return 0; |
3174 | } |
3216 | } |
3175 | |
3217 | |
3176 | static void |
3218 | ecb_hot static void |
3177 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3219 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3178 | { |
3220 | { |
3179 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3221 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3180 | cons (args, |
3222 | cons (args, |
3181 | cons (SCHEME_V->envir, |
3223 | cons (SCHEME_V->envir, |
3182 | cons (code, |
3224 | cons (code, |
3183 | SCHEME_V->dump)))); |
3225 | SCHEME_V->dump)))); |
3184 | } |
3226 | } |
3185 | |
3227 | |
3186 | static void |
3228 | ecb_cold static void |
3187 | dump_stack_mark (SCHEME_P) |
3229 | dump_stack_mark (SCHEME_P) |
3188 | { |
3230 | { |
3189 | mark (SCHEME_V->dump); |
3231 | mark (SCHEME_V->dump); |
3190 | } |
3232 | } |
3191 | |
3233 | |
3192 | static pointer |
3234 | ecb_cold static pointer |
3193 | ss_get_cont (SCHEME_P) |
3235 | ss_get_cont (SCHEME_P) |
3194 | { |
3236 | { |
3195 | return SCHEME_V->dump; |
3237 | return SCHEME_V->dump; |
3196 | } |
3238 | } |
3197 | |
3239 | |
3198 | static void |
3240 | ecb_cold static void |
3199 | ss_set_cont (SCHEME_P_ pointer cont) |
3241 | ss_set_cont (SCHEME_P_ pointer cont) |
3200 | { |
3242 | { |
3201 | SCHEME_V->dump = cont; |
3243 | SCHEME_V->dump = cont; |
3202 | } |
3244 | } |
3203 | |
3245 | |
3204 | #endif |
3246 | #endif |
3205 | |
3247 | |
3206 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3248 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3207 | |
3249 | |
|
|
3250 | #if EXPERIMENT |
3208 | static int |
3251 | static int |
|
|
3252 | debug (SCHEME_P_ int indent, pointer x) |
|
|
3253 | { |
|
|
3254 | int c; |
|
|
3255 | |
|
|
3256 | if (is_syntax (x)) |
|
|
3257 | { |
|
|
3258 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3259 | return 8 + 8; |
|
|
3260 | } |
|
|
3261 | |
|
|
3262 | if (x == NIL) |
|
|
3263 | { |
|
|
3264 | printf ("%*sNIL\n", indent, ""); |
|
|
3265 | return 3; |
|
|
3266 | } |
|
|
3267 | |
|
|
3268 | switch (type (x)) |
|
|
3269 | { |
|
|
3270 | case T_INTEGER: |
|
|
3271 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3272 | return 32+8; |
|
|
3273 | |
|
|
3274 | case T_SYMBOL: |
|
|
3275 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3276 | return 24+8; |
|
|
3277 | |
|
|
3278 | case T_CLOSURE: |
|
|
3279 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3280 | debug (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3281 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3282 | |
|
|
3283 | case T_PAIR: |
|
|
3284 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3285 | c = debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3286 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3287 | return c + 1; |
|
|
3288 | |
|
|
3289 | case T_PORT: |
|
|
3290 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3291 | return 24+8; |
|
|
3292 | |
|
|
3293 | case T_VECTOR: |
|
|
3294 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3295 | return 24+8; |
|
|
3296 | |
|
|
3297 | case T_ENVIRONMENT: |
|
|
3298 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3299 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3300 | |
|
|
3301 | default: |
|
|
3302 | printf ("unhandled type %d\n", type (x)); |
|
|
3303 | break; |
|
|
3304 | } |
|
|
3305 | } |
|
|
3306 | #endif |
|
|
3307 | |
|
|
3308 | /* syntax, eval, core, ... */ |
|
|
3309 | ecb_hot static int |
3209 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3310 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3210 | { |
3311 | { |
3211 | pointer args = SCHEME_V->args; |
3312 | pointer args = SCHEME_V->args; |
3212 | pointer x, y; |
3313 | pointer x, y; |
3213 | |
3314 | |
3214 | switch (op) |
3315 | switch (op) |
3215 | { |
3316 | { |
|
|
3317 | #if EXPERIMENT //D |
|
|
3318 | case OP_DEBUG: |
|
|
3319 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
|
|
3320 | printf ("\n"); |
|
|
3321 | s_return (S_T); |
|
|
3322 | #endif |
3216 | case OP_LOAD: /* load */ |
3323 | case OP_LOAD: /* load */ |
3217 | if (file_interactive (SCHEME_A)) |
3324 | if (file_interactive (SCHEME_A)) |
3218 | { |
3325 | { |
3219 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3326 | putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n"); |
3220 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3327 | //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3221 | } |
3328 | } |
3222 | |
3329 | |
3223 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3330 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3224 | Error_1 ("unable to open", car (args)); |
3331 | Error_1 ("unable to open", car (args)); |
3225 | else |
3332 | else |
… | |
… | |
3229 | } |
3336 | } |
3230 | |
3337 | |
3231 | case OP_T0LVL: /* top level */ |
3338 | case OP_T0LVL: /* top level */ |
3232 | |
3339 | |
3233 | /* If we reached the end of file, this loop is done. */ |
3340 | /* If we reached the end of file, this loop is done. */ |
3234 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3341 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3235 | { |
3342 | { |
3236 | if (SCHEME_V->file_i == 0) |
3343 | if (SCHEME_V->file_i == 0) |
3237 | { |
3344 | { |
3238 | SCHEME_V->args = NIL; |
3345 | SCHEME_V->args = NIL; |
3239 | s_goto (OP_QUIT); |
3346 | s_goto (OP_QUIT); |
… | |
… | |
3317 | #endif |
3424 | #endif |
3318 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3425 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3319 | { |
3426 | { |
3320 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3427 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3321 | |
3428 | |
3322 | if (x != NIL) |
3429 | if (x == NIL) |
3323 | s_return (slot_value_in_env (x)); |
|
|
3324 | else |
|
|
3325 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3430 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3431 | |
|
|
3432 | s_return (slot_value_in_env (x)); |
3326 | } |
3433 | } |
3327 | else if (is_pair (SCHEME_V->code)) |
3434 | else if (is_pair (SCHEME_V->code)) |
3328 | { |
3435 | { |
3329 | x = car (SCHEME_V->code); |
3436 | x = car (SCHEME_V->code); |
3330 | |
3437 | |
… | |
… | |
3343 | } |
3450 | } |
3344 | else |
3451 | else |
3345 | s_return (SCHEME_V->code); |
3452 | s_return (SCHEME_V->code); |
3346 | |
3453 | |
3347 | case OP_E0ARGS: /* eval arguments */ |
3454 | case OP_E0ARGS: /* eval arguments */ |
3348 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3455 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3349 | { |
3456 | { |
3350 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3457 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3351 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3458 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3352 | SCHEME_V->code = SCHEME_V->value; |
3459 | SCHEME_V->code = SCHEME_V->value; |
3353 | s_goto (OP_APPLY); |
3460 | s_goto (OP_APPLY); |
… | |
… | |
3380 | |
3487 | |
3381 | case OP_TRACING: |
3488 | case OP_TRACING: |
3382 | { |
3489 | { |
3383 | int tr = SCHEME_V->tracing; |
3490 | int tr = SCHEME_V->tracing; |
3384 | |
3491 | |
3385 | SCHEME_V->tracing = ivalue (car (args)); |
3492 | SCHEME_V->tracing = ivalue_unchecked (car (args)); |
3386 | s_return (mk_integer (SCHEME_A_ tr)); |
3493 | s_return (mk_integer (SCHEME_A_ tr)); |
3387 | } |
3494 | } |
3388 | |
3495 | |
3389 | #endif |
3496 | #endif |
3390 | |
3497 | |
… | |
… | |
3407 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3514 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3408 | else if (is_foreign (SCHEME_V->code)) |
3515 | else if (is_foreign (SCHEME_V->code)) |
3409 | { |
3516 | { |
3410 | /* Keep nested calls from GC'ing the arglist */ |
3517 | /* Keep nested calls from GC'ing the arglist */ |
3411 | push_recent_alloc (SCHEME_A_ args, NIL); |
3518 | push_recent_alloc (SCHEME_A_ args, NIL); |
3412 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3519 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3413 | |
3520 | |
3414 | s_return (x); |
3521 | s_return (x); |
3415 | } |
3522 | } |
3416 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3523 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3417 | { |
3524 | { |
… | |
… | |
3454 | |
3561 | |
3455 | case OP_DOMACRO: /* do macro */ |
3562 | case OP_DOMACRO: /* do macro */ |
3456 | SCHEME_V->code = SCHEME_V->value; |
3563 | SCHEME_V->code = SCHEME_V->value; |
3457 | s_goto (OP_EVAL); |
3564 | s_goto (OP_EVAL); |
3458 | |
3565 | |
3459 | #if 1 |
|
|
3460 | |
|
|
3461 | case OP_LAMBDA: /* lambda */ |
3566 | case OP_LAMBDA: /* lambda */ |
3462 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3567 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3463 | set SCHEME_V->value fall thru */ |
3568 | set SCHEME_V->value fall thru */ |
3464 | { |
3569 | { |
3465 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3570 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
… | |
… | |
3471 | SCHEME_V->code = slot_value_in_env (f); |
3576 | SCHEME_V->code = slot_value_in_env (f); |
3472 | s_goto (OP_APPLY); |
3577 | s_goto (OP_APPLY); |
3473 | } |
3578 | } |
3474 | |
3579 | |
3475 | SCHEME_V->value = SCHEME_V->code; |
3580 | SCHEME_V->value = SCHEME_V->code; |
3476 | /* Fallthru */ |
|
|
3477 | } |
3581 | } |
|
|
3582 | /* Fallthru */ |
3478 | |
3583 | |
3479 | case OP_LAMBDA1: |
3584 | case OP_LAMBDA1: |
3480 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3585 | 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 | |
3586 | |
3489 | case OP_MKCLOSURE: /* make-closure */ |
3587 | case OP_MKCLOSURE: /* make-closure */ |
3490 | x = car (args); |
3588 | x = car (args); |
3491 | |
3589 | |
3492 | if (car (x) == SCHEME_V->LAMBDA) |
3590 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
3902 | } |
4000 | } |
3903 | |
4001 | |
3904 | if (USE_ERROR_CHECKING) abort (); |
4002 | if (USE_ERROR_CHECKING) abort (); |
3905 | } |
4003 | } |
3906 | |
4004 | |
3907 | static int |
4005 | /* math, cxr */ |
|
|
4006 | ecb_hot static int |
3908 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4007 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
3909 | { |
4008 | { |
3910 | pointer args = SCHEME_V->args; |
4009 | pointer args = SCHEME_V->args; |
3911 | pointer x = car (args); |
4010 | pointer x = car (args); |
3912 | num v; |
4011 | num v; |
3913 | |
4012 | |
3914 | #if USE_MATH |
|
|
3915 | RVALUE dd; |
|
|
3916 | #endif |
|
|
3917 | |
|
|
3918 | switch (op) |
4013 | switch (op) |
3919 | { |
4014 | { |
3920 | #if USE_MATH |
4015 | #if USE_MATH |
3921 | case OP_INEX2EX: /* inexact->exact */ |
4016 | case OP_INEX2EX: /* inexact->exact */ |
3922 | if (is_integer (x)) |
4017 | if (!is_integer (x)) |
3923 | s_return (x); |
4018 | { |
3924 | else if (modf (rvalue_unchecked (x), &dd) == 0) |
4019 | RVALUE r = rvalue_unchecked (x); |
3925 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4020 | |
|
|
4021 | if (r == (RVALUE)(IVALUE)r) |
|
|
4022 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
3926 | else |
4023 | else |
3927 | Error_1 ("inexact->exact: not integral:", x); |
4024 | Error_1 ("inexact->exact: not integral:", x); |
|
|
4025 | } |
3928 | |
4026 | |
|
|
4027 | s_return (x); |
|
|
4028 | |
|
|
4029 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4030 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4031 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4032 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
|
|
4033 | |
|
|
4034 | 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)))); |
4035 | 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)))); |
4036 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
|
|
4037 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
3931 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4038 | 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)))); |
4039 | 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)))); |
4040 | 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)))); |
4041 | 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)))); |
4042 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
3936 | |
4043 | |
3937 | case OP_ATAN: |
4044 | case OP_ATAN: |
|
|
4045 | s_return (mk_real (SCHEME_A_ |
3938 | if (cdr (args) == NIL) |
4046 | cdr (args) == NIL |
3939 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4047 | ? atan (rvalue (x)) |
3940 | else |
4048 | : 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 | |
4049 | |
3949 | case OP_EXPT: |
4050 | case OP_EXPT: |
3950 | { |
4051 | { |
3951 | RVALUE result; |
4052 | RVALUE result; |
3952 | int real_result = 1; |
4053 | int real_result = 1; |
… | |
… | |
3966 | /* If the test fails, result is too big for integer. */ |
4067 | /* If the test fails, result is too big for integer. */ |
3967 | if (!real_result) |
4068 | if (!real_result) |
3968 | { |
4069 | { |
3969 | long result_as_long = result; |
4070 | long result_as_long = result; |
3970 | |
4071 | |
3971 | if (result != (RVALUE) result_as_long) |
4072 | if (result != result_as_long) |
3972 | real_result = 1; |
4073 | real_result = 1; |
3973 | } |
4074 | } |
3974 | |
4075 | |
3975 | if (real_result) |
4076 | if (real_result) |
3976 | s_return (mk_real (SCHEME_A_ result)); |
4077 | s_return (mk_real (SCHEME_A_ result)); |
3977 | else |
4078 | else |
3978 | s_return (mk_integer (SCHEME_A_ result)); |
4079 | s_return (mk_integer (SCHEME_A_ result)); |
3979 | } |
4080 | } |
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 |
4081 | #endif |
4002 | |
4082 | |
4003 | case OP_ADD: /* + */ |
4083 | case OP_ADD: /* + */ |
4004 | v = num_zero; |
4084 | v = num_zero; |
4005 | |
4085 | |
… | |
… | |
4093 | else |
4173 | else |
4094 | Error_0 ("modulo: division by zero"); |
4174 | Error_0 ("modulo: division by zero"); |
4095 | |
4175 | |
4096 | s_return (mk_number (SCHEME_A_ v)); |
4176 | s_return (mk_number (SCHEME_A_ v)); |
4097 | |
4177 | |
4098 | case OP_CAR: /* car */ |
4178 | /* the compiler will optimize this mess... */ |
4099 | s_return (caar (args)); |
4179 | case OP_CAR: op_car: s_return (car (x)); |
4100 | |
4180 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4101 | case OP_CDR: /* cdr */ |
4181 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4102 | s_return (cdar (args)); |
4182 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4183 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4184 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4185 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4186 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4187 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4188 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4189 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4190 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4191 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4192 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4193 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4194 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4195 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4196 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4197 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4198 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4199 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4200 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4201 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4202 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4203 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4204 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4205 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4206 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4207 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4208 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4103 | |
4209 | |
4104 | case OP_CONS: /* cons */ |
4210 | case OP_CONS: /* cons */ |
4105 | set_cdr (args, cadr (args)); |
4211 | set_cdr (args, cadr (args)); |
4106 | s_return (args); |
4212 | s_return (args); |
4107 | |
4213 | |
… | |
… | |
4122 | } |
4228 | } |
4123 | else |
4229 | else |
4124 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4230 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4125 | |
4231 | |
4126 | case OP_CHAR2INT: /* char->integer */ |
4232 | case OP_CHAR2INT: /* char->integer */ |
4127 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4233 | s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x))); |
4128 | |
4234 | |
4129 | case OP_INT2CHAR: /* integer->char */ |
4235 | case OP_INT2CHAR: /* integer->char */ |
4130 | s_return (mk_character (SCHEME_A_ ivalue (x))); |
4236 | s_return (mk_character (SCHEME_A_ ivalue_unchecked (x))); |
4131 | |
4237 | |
4132 | case OP_CHARUPCASE: |
4238 | case OP_CHARUPCASE: |
4133 | { |
4239 | { |
4134 | unsigned char c = ivalue (x); |
4240 | unsigned char c = ivalue_unchecked (x); |
4135 | c = toupper (c); |
4241 | c = toupper (c); |
4136 | s_return (mk_character (SCHEME_A_ c)); |
4242 | s_return (mk_character (SCHEME_A_ c)); |
4137 | } |
4243 | } |
4138 | |
4244 | |
4139 | case OP_CHARDNCASE: |
4245 | case OP_CHARDNCASE: |
4140 | { |
4246 | { |
4141 | unsigned char c = ivalue (x); |
4247 | unsigned char c = ivalue_unchecked (x); |
4142 | c = tolower (c); |
4248 | c = tolower (c); |
4143 | s_return (mk_character (SCHEME_A_ c)); |
4249 | s_return (mk_character (SCHEME_A_ c)); |
4144 | } |
4250 | } |
4145 | |
4251 | |
4146 | case OP_STR2SYM: /* string->symbol */ |
4252 | case OP_STR2SYM: /* string->symbol */ |
… | |
… | |
4223 | Error_1 ("atom->string: not an atom:", x); |
4329 | Error_1 ("atom->string: not an atom:", x); |
4224 | } |
4330 | } |
4225 | |
4331 | |
4226 | case OP_MKSTRING: /* make-string */ |
4332 | case OP_MKSTRING: /* make-string */ |
4227 | { |
4333 | { |
4228 | int fill = ' '; |
4334 | int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' '; |
4229 | int len; |
|
|
4230 | |
|
|
4231 | len = ivalue (x); |
4335 | int len = ivalue_unchecked (x); |
4232 | |
|
|
4233 | if (cdr (args) != NIL) |
|
|
4234 | fill = charvalue (cadr (args)); |
|
|
4235 | |
4336 | |
4236 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4337 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4237 | } |
4338 | } |
4238 | |
4339 | |
4239 | case OP_STRLEN: /* string-length */ |
4340 | case OP_STRLEN: /* string-length */ |
4240 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4341 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4241 | |
4342 | |
4242 | case OP_STRREF: /* string-ref */ |
4343 | case OP_STRREF: /* string-ref */ |
4243 | { |
4344 | { |
4244 | char *str; |
|
|
4245 | int index; |
|
|
4246 | |
|
|
4247 | str = strvalue (x); |
4345 | char *str = strvalue (x); |
4248 | |
|
|
4249 | index = ivalue (cadr (args)); |
4346 | int index = ivalue_unchecked (cadr (args)); |
4250 | |
4347 | |
4251 | if (index >= strlength (x)) |
4348 | if (index >= strlength (x)) |
4252 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4349 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4253 | |
4350 | |
4254 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4351 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4255 | } |
4352 | } |
4256 | |
4353 | |
4257 | case OP_STRSET: /* string-set! */ |
4354 | case OP_STRSET: /* string-set! */ |
4258 | { |
4355 | { |
4259 | char *str; |
4356 | char *str = strvalue (x); |
4260 | int index; |
4357 | int index = ivalue_unchecked (cadr (args)); |
4261 | int c; |
4358 | int c; |
4262 | |
4359 | |
4263 | if (is_immutable (x)) |
4360 | if (is_immutable (x)) |
4264 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4361 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4265 | |
|
|
4266 | str = strvalue (x); |
|
|
4267 | |
|
|
4268 | index = ivalue (cadr (args)); |
|
|
4269 | |
4362 | |
4270 | if (index >= strlength (x)) |
4363 | if (index >= strlength (x)) |
4271 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4364 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4272 | |
4365 | |
4273 | c = charvalue (caddr (args)); |
4366 | c = charvalue (caddr (args)); |
… | |
… | |
4294 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4387 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4295 | |
4388 | |
4296 | s_return (newstr); |
4389 | s_return (newstr); |
4297 | } |
4390 | } |
4298 | |
4391 | |
4299 | case OP_SUBSTR: /* substring */ |
4392 | case OP_STRING_COPY: /* substring/string-copy */ |
4300 | { |
4393 | { |
4301 | char *str; |
4394 | char *str = strvalue (x); |
4302 | int index0; |
4395 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4303 | int index1; |
4396 | int index1; |
4304 | int len; |
4397 | int len; |
4305 | |
4398 | |
4306 | str = strvalue (x); |
|
|
4307 | |
|
|
4308 | index0 = ivalue (cadr (args)); |
|
|
4309 | |
|
|
4310 | if (index0 > strlength (x)) |
4399 | if (index0 > strlength (x)) |
4311 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4400 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4312 | |
4401 | |
4313 | if (cddr (args) != NIL) |
4402 | if (cddr (args) != NIL) |
4314 | { |
4403 | { |
4315 | index1 = ivalue (caddr (args)); |
4404 | index1 = ivalue_unchecked (caddr (args)); |
4316 | |
4405 | |
4317 | if (index1 > strlength (x) || index1 < index0) |
4406 | if (index1 > strlength (x) || index1 < index0) |
4318 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4407 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4319 | } |
4408 | } |
4320 | else |
4409 | else |
4321 | index1 = strlength (x); |
4410 | index1 = strlength (x); |
4322 | |
4411 | |
4323 | len = index1 - index0; |
4412 | len = index1 - index0; |
4324 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4413 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4325 | memcpy (strvalue (x), str + index0, len); |
|
|
4326 | strvalue (x)[len] = 0; |
|
|
4327 | |
4414 | |
4328 | s_return (x); |
4415 | s_return (x); |
4329 | } |
4416 | } |
4330 | |
4417 | |
4331 | case OP_VECTOR: /* vector */ |
4418 | case OP_VECTOR: /* vector */ |
… | |
… | |
4343 | if (SCHEME_V->no_memory) |
4430 | if (SCHEME_V->no_memory) |
4344 | s_return (S_SINK); |
4431 | s_return (S_SINK); |
4345 | #endif |
4432 | #endif |
4346 | |
4433 | |
4347 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4434 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4348 | set_vector_elem (vec, i, car (x)); |
4435 | vector_set (vec, i, car (x)); |
4349 | |
4436 | |
4350 | s_return (vec); |
4437 | s_return (vec); |
4351 | } |
4438 | } |
4352 | |
4439 | |
4353 | case OP_MKVECTOR: /* make-vector */ |
4440 | case OP_MKVECTOR: /* make-vector */ |
4354 | { |
4441 | { |
4355 | pointer fill = NIL; |
4442 | pointer fill = NIL; |
4356 | int len; |
|
|
4357 | pointer vec; |
4443 | pointer vec; |
4358 | |
|
|
4359 | len = ivalue (x); |
4444 | int len = ivalue_unchecked (x); |
4360 | |
4445 | |
4361 | if (cdr (args) != NIL) |
4446 | if (cdr (args) != NIL) |
4362 | fill = cadr (args); |
4447 | fill = cadr (args); |
4363 | |
4448 | |
4364 | vec = mk_vector (SCHEME_A_ len); |
4449 | vec = mk_vector (SCHEME_A_ len); |
… | |
… | |
4367 | if (SCHEME_V->no_memory) |
4452 | if (SCHEME_V->no_memory) |
4368 | s_return (S_SINK); |
4453 | s_return (S_SINK); |
4369 | #endif |
4454 | #endif |
4370 | |
4455 | |
4371 | if (fill != NIL) |
4456 | if (fill != NIL) |
4372 | fill_vector (vec, fill); |
4457 | fill_vector (vec, 0, fill); |
4373 | |
4458 | |
4374 | s_return (vec); |
4459 | s_return (vec); |
4375 | } |
4460 | } |
4376 | |
4461 | |
4377 | case OP_VECLEN: /* vector-length */ |
4462 | case OP_VECLEN: /* vector-length */ |
4378 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4463 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4379 | |
4464 | |
|
|
4465 | case OP_VECRESIZE: |
|
|
4466 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4467 | s_return (x); |
|
|
4468 | |
4380 | case OP_VECREF: /* vector-ref */ |
4469 | case OP_VECREF: /* vector-ref */ |
4381 | { |
4470 | { |
4382 | int index; |
|
|
4383 | |
|
|
4384 | index = ivalue (cadr (args)); |
4471 | int index = ivalue_unchecked (cadr (args)); |
4385 | |
4472 | |
4386 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4473 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4387 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4474 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4388 | |
4475 | |
4389 | s_return (vector_elem (x, index)); |
4476 | s_return (vector_get (x, index)); |
4390 | } |
4477 | } |
4391 | |
4478 | |
4392 | case OP_VECSET: /* vector-set! */ |
4479 | case OP_VECSET: /* vector-set! */ |
4393 | { |
4480 | { |
4394 | int index; |
4481 | int index = ivalue_unchecked (cadr (args)); |
4395 | |
4482 | |
4396 | if (is_immutable (x)) |
4483 | if (is_immutable (x)) |
4397 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4484 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4398 | |
4485 | |
4399 | index = ivalue (cadr (args)); |
|
|
4400 | |
|
|
4401 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4486 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4402 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4487 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4403 | |
4488 | |
4404 | set_vector_elem (x, index, caddr (args)); |
4489 | vector_set (x, index, caddr (args)); |
4405 | s_return (x); |
4490 | s_return (x); |
4406 | } |
4491 | } |
4407 | } |
4492 | } |
4408 | |
4493 | |
4409 | if (USE_ERROR_CHECKING) abort (); |
4494 | if (USE_ERROR_CHECKING) abort (); |
4410 | } |
4495 | } |
4411 | |
4496 | |
4412 | INTERFACE int |
4497 | /* relational ops */ |
4413 | is_list (SCHEME_P_ pointer a) |
4498 | 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) |
4499 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4468 | { |
4500 | { |
4469 | pointer x = SCHEME_V->args; |
4501 | pointer x = SCHEME_V->args; |
4470 | |
4502 | |
4471 | for (;;) |
4503 | for (;;) |
… | |
… | |
4492 | } |
4524 | } |
4493 | |
4525 | |
4494 | s_return (S_T); |
4526 | s_return (S_T); |
4495 | } |
4527 | } |
4496 | |
4528 | |
4497 | static int |
4529 | /* predicates */ |
|
|
4530 | ecb_hot static int |
4498 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4531 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4499 | { |
4532 | { |
4500 | pointer args = SCHEME_V->args; |
4533 | pointer args = SCHEME_V->args; |
4501 | pointer a = car (args); |
4534 | pointer a = car (args); |
4502 | pointer d = cdr (args); |
4535 | pointer d = cdr (args); |
4503 | int r; |
4536 | int r; |
4504 | |
4537 | |
4505 | switch (op) |
4538 | switch (op) |
4506 | { |
4539 | { |
4507 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4540 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4508 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
4541 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break; |
4509 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4542 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4510 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4543 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4511 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
4544 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4545 | case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break; |
4512 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4546 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4513 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4547 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4514 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4548 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4515 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4549 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4516 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4550 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4517 | |
4551 | |
4518 | #if USE_CHAR_CLASSIFIERS |
4552 | #if USE_CHAR_CLASSIFIERS |
4519 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; |
4553 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4520 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; |
4554 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4521 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; |
4555 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4522 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; |
4556 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break; |
4523 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; |
4557 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break; |
4524 | #endif |
4558 | #endif |
4525 | |
4559 | |
4526 | #if USE_PORTS |
4560 | #if USE_PORTS |
4527 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4561 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4528 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
4562 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
… | |
… | |
4548 | } |
4582 | } |
4549 | |
4583 | |
4550 | s_retbool (r); |
4584 | s_retbool (r); |
4551 | } |
4585 | } |
4552 | |
4586 | |
4553 | static int |
4587 | /* promises, list ops, ports */ |
|
|
4588 | ecb_hot static int |
4554 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4589 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4555 | { |
4590 | { |
4556 | pointer args = SCHEME_V->args; |
4591 | pointer args = SCHEME_V->args; |
4557 | pointer a = car (args); |
4592 | pointer a = car (args); |
4558 | pointer x, y; |
4593 | pointer x, y; |
… | |
… | |
4571 | } |
4606 | } |
4572 | else |
4607 | else |
4573 | s_return (SCHEME_V->code); |
4608 | s_return (SCHEME_V->code); |
4574 | |
4609 | |
4575 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4610 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4576 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4611 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4577 | s_return (SCHEME_V->value); |
4612 | s_return (SCHEME_V->value); |
4578 | |
4613 | |
4579 | #if USE_PORTS |
4614 | #if USE_PORTS |
|
|
4615 | |
|
|
4616 | case OP_EOF_OBJECT: /* eof-object */ |
|
|
4617 | s_return (S_EOF); |
4580 | |
4618 | |
4581 | case OP_WRITE: /* write */ |
4619 | case OP_WRITE: /* write */ |
4582 | case OP_DISPLAY: /* display */ |
4620 | case OP_DISPLAY: /* display */ |
4583 | case OP_WRITE_CHAR: /* write-char */ |
4621 | case OP_WRITE_CHAR: /* write-char */ |
4584 | if (is_pair (cdr (SCHEME_V->args))) |
4622 | if (is_pair (cdr (SCHEME_V->args))) |
… | |
… | |
4726 | SCHEME_V->gc_verbose = (a != S_F); |
4764 | SCHEME_V->gc_verbose = (a != S_F); |
4727 | s_retbool (was); |
4765 | s_retbool (was); |
4728 | } |
4766 | } |
4729 | |
4767 | |
4730 | case OP_NEWSEGMENT: /* new-segment */ |
4768 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4769 | #if 0 |
4731 | if (!is_pair (args) || !is_number (a)) |
4770 | if (!is_pair (args) || !is_number (a)) |
4732 | Error_0 ("new-segment: argument must be a number"); |
4771 | Error_0 ("new-segment: argument must be a number"); |
4733 | |
4772 | #endif |
4734 | alloc_cellseg (SCHEME_A_ (int)ivalue (a)); |
4773 | s_retbool (alloc_cellseg (SCHEME_A)); |
4735 | |
|
|
4736 | s_return (S_T); |
|
|
4737 | |
4774 | |
4738 | case OP_OBLIST: /* oblist */ |
4775 | case OP_OBLIST: /* oblist */ |
4739 | s_return (oblist_all_symbols (SCHEME_A)); |
4776 | s_return (oblist_all_symbols (SCHEME_A)); |
4740 | |
4777 | |
4741 | #if USE_PORTS |
4778 | #if USE_PORTS |
… | |
… | |
4811 | s_return (p == NIL ? S_F : p); |
4848 | s_return (p == NIL ? S_F : p); |
4812 | } |
4849 | } |
4813 | |
4850 | |
4814 | case OP_GET_OUTSTRING: /* get-output-string */ |
4851 | case OP_GET_OUTSTRING: /* get-output-string */ |
4815 | { |
4852 | { |
4816 | port *p; |
4853 | port *p = port (a); |
4817 | |
4854 | |
4818 | if ((p = a->object.port)->kind & port_string) |
4855 | if (p->kind & port_string) |
4819 | { |
4856 | { |
4820 | off_t size; |
4857 | off_t size; |
4821 | char *str; |
4858 | char *str; |
4822 | |
4859 | |
4823 | size = p->rep.string.curr - p->rep.string.start + 1; |
4860 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4858 | } |
4895 | } |
4859 | |
4896 | |
4860 | if (USE_ERROR_CHECKING) abort (); |
4897 | if (USE_ERROR_CHECKING) abort (); |
4861 | } |
4898 | } |
4862 | |
4899 | |
4863 | static int |
4900 | /* reading */ |
|
|
4901 | ecb_cold static int |
4864 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4902 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4865 | { |
4903 | { |
4866 | pointer args = SCHEME_V->args; |
4904 | pointer args = SCHEME_V->args; |
4867 | pointer x; |
4905 | pointer x; |
4868 | |
4906 | |
… | |
… | |
4928 | int res; |
4966 | int res; |
4929 | |
4967 | |
4930 | if (is_pair (args)) |
4968 | if (is_pair (args)) |
4931 | p = car (args); |
4969 | p = car (args); |
4932 | |
4970 | |
4933 | res = p->object.port->kind & port_string; |
4971 | res = port (p)->kind & port_string; |
4934 | |
4972 | |
4935 | s_retbool (res); |
4973 | s_retbool (res); |
4936 | } |
4974 | } |
4937 | |
4975 | |
4938 | case OP_SET_INPORT: /* set-input-port */ |
4976 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
4997 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5035 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
4998 | SCHEME_V->tok = token (SCHEME_A); |
5036 | SCHEME_V->tok = token (SCHEME_A); |
4999 | s_goto (OP_RDSEXPR); |
5037 | s_goto (OP_RDSEXPR); |
5000 | |
5038 | |
5001 | case TOK_ATOM: |
5039 | case TOK_ATOM: |
5002 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); |
5040 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
|
|
5041 | |
|
|
5042 | case TOK_DOTATOM: |
|
|
5043 | SCHEME_V->strbuff[0] = '.'; |
|
|
5044 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
|
|
5045 | |
|
|
5046 | case TOK_STRATOM: |
|
|
5047 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5048 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5049 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
5003 | |
5050 | |
5004 | case TOK_DQUOTE: |
5051 | case TOK_DQUOTE: |
5005 | x = readstrexp (SCHEME_A); |
5052 | x = readstrexp (SCHEME_A_ '"'); |
5006 | |
5053 | |
5007 | if (x == S_F) |
5054 | if (x == S_F) |
5008 | Error_0 ("Error reading string"); |
5055 | Error_0 ("Error reading string"); |
5009 | |
5056 | |
5010 | setimmutable (x); |
5057 | setimmutable (x); |
… | |
… | |
5022 | s_goto (OP_EVAL); |
5069 | s_goto (OP_EVAL); |
5023 | } |
5070 | } |
5024 | } |
5071 | } |
5025 | |
5072 | |
5026 | case TOK_SHARP_CONST: |
5073 | case TOK_SHARP_CONST: |
5027 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) |
5074 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
5028 | Error_0 ("undefined sharp expression"); |
5075 | Error_0 ("undefined sharp expression"); |
5029 | else |
5076 | else |
5030 | s_return (x); |
5077 | s_return (x); |
5031 | |
5078 | |
5032 | default: |
5079 | default: |
… | |
… | |
5184 | putstr (SCHEME_A_ ")"); |
5231 | putstr (SCHEME_A_ ")"); |
5185 | s_return (S_T); |
5232 | s_return (S_T); |
5186 | } |
5233 | } |
5187 | else |
5234 | else |
5188 | { |
5235 | { |
5189 | pointer elem = vector_elem (vec, i); |
5236 | pointer elem = vector_get (vec, i); |
5190 | |
5237 | |
5191 | ivalue_unchecked (cdr (args)) = i + 1; |
5238 | ivalue_unchecked (cdr (args)) = i + 1; |
5192 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5239 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5193 | SCHEME_V->args = elem; |
5240 | SCHEME_V->args = elem; |
5194 | |
5241 | |
… | |
… | |
5201 | } |
5248 | } |
5202 | |
5249 | |
5203 | if (USE_ERROR_CHECKING) abort (); |
5250 | if (USE_ERROR_CHECKING) abort (); |
5204 | } |
5251 | } |
5205 | |
5252 | |
5206 | static int |
5253 | /* list ops */ |
|
|
5254 | ecb_hot static int |
5207 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5255 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5208 | { |
5256 | { |
5209 | pointer args = SCHEME_V->args; |
5257 | pointer args = SCHEME_V->args; |
5210 | pointer a = car (args); |
5258 | pointer a = car (args); |
5211 | pointer x, y; |
5259 | pointer x, y; |
… | |
… | |
5254 | |
5302 | |
5255 | case OP_CLOSUREP: /* closure? */ |
5303 | case OP_CLOSUREP: /* closure? */ |
5256 | /* |
5304 | /* |
5257 | * Note, macro object is also a closure. |
5305 | * Note, macro object is also a closure. |
5258 | * Therefore, (closure? <#MACRO>) ==> #t |
5306 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5307 | * (schmorp) well, obviously not, fix? TODO |
5259 | */ |
5308 | */ |
5260 | s_retbool (is_closure (a)); |
5309 | s_retbool (is_closure (a)); |
5261 | |
5310 | |
5262 | case OP_MACROP: /* macro? */ |
5311 | case OP_MACROP: /* macro? */ |
5263 | s_retbool (is_macro (a)); |
5312 | s_retbool (is_macro (a)); |
… | |
… | |
5268 | |
5317 | |
5269 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5318 | /* 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); |
5319 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5271 | |
5320 | |
5272 | typedef int (*test_predicate)(pointer); |
5321 | typedef int (*test_predicate)(pointer); |
5273 | static int |
5322 | |
|
|
5323 | ecb_hot static int |
5274 | is_any (pointer p) |
5324 | tst_any (pointer p) |
5275 | { |
5325 | { |
5276 | return 1; |
5326 | return 1; |
5277 | } |
5327 | } |
5278 | |
5328 | |
5279 | static int |
5329 | ecb_hot static int |
5280 | is_nonneg (pointer p) |
5330 | tst_inonneg (pointer p) |
5281 | { |
5331 | { |
5282 | return ivalue (p) >= 0 && is_integer (p); |
5332 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5283 | } |
5333 | } |
5284 | |
5334 | |
5285 | static int |
5335 | ecb_hot static int |
5286 | tst_is_list (pointer p) |
5336 | tst_is_list (SCHEME_P_ pointer p) |
5287 | { |
5337 | { |
5288 | return p == NIL || is_pair (p); |
5338 | return p == NIL || is_pair (p); |
5289 | } |
5339 | } |
5290 | |
5340 | |
5291 | /* Correspond carefully with following defines! */ |
5341 | /* Correspond carefully with following defines! */ |
5292 | static struct |
5342 | static struct |
5293 | { |
5343 | { |
5294 | test_predicate fct; |
5344 | test_predicate fct; |
5295 | const char *kind; |
5345 | const char *kind; |
5296 | } tests[] = |
5346 | } tests[] = { |
5297 | { |
|
|
5298 | { is_any, 0 }, |
5347 | { tst_any , 0 }, |
5299 | { is_string, "string" }, |
5348 | { is_string , "string" }, |
5300 | { is_symbol, "symbol" }, |
5349 | { is_symbol , "symbol" }, |
5301 | { is_port, "port" }, |
5350 | { is_port , "port" }, |
5302 | { is_inport, "input port" }, |
5351 | { is_inport , "input port" }, |
5303 | { is_outport, "output port" }, |
5352 | { is_outport , "output port" }, |
5304 | { is_environment, "environment" }, |
5353 | { is_environment, "environment" }, |
5305 | { is_pair, "pair" }, |
5354 | { is_pair , "pair" }, |
5306 | { tst_is_list, "pair or '()" }, |
5355 | { 0 , "pair or '()" }, |
5307 | { is_character, "character" }, |
5356 | { is_character , "character" }, |
5308 | { is_vector, "vector" }, |
5357 | { is_vector , "vector" }, |
5309 | { is_number, "number" }, |
5358 | { is_number , "number" }, |
5310 | { is_integer, "integer" }, |
5359 | { is_integer , "integer" }, |
5311 | { is_nonneg, "non-negative integer" } |
5360 | { tst_inonneg , "non-negative integer" } |
5312 | }; |
5361 | }; |
5313 | |
5362 | |
5314 | #define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ |
5363 | #define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ |
5315 | #define TST_ANY "\001" |
5364 | #define TST_ANY "\001" |
5316 | #define TST_STRING "\002" |
5365 | #define TST_STRING "\002" |
… | |
… | |
5334 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5383 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5335 | #include "opdefines.h" |
5384 | #include "opdefines.h" |
5336 | #undef OP_DEF |
5385 | #undef OP_DEF |
5337 | ; |
5386 | ; |
5338 | |
5387 | |
5339 | static const char * |
5388 | ecb_cold static const char * |
5340 | opname (int idx) |
5389 | opname (int idx) |
5341 | { |
5390 | { |
5342 | const char *name = opnames; |
5391 | const char *name = opnames; |
5343 | |
5392 | |
5344 | /* should do this at compile time, but would require external program, right? */ |
5393 | /* should do this at compile time, but would require external program, right? */ |
… | |
… | |
5346 | name += strlen (name) + 1; |
5395 | name += strlen (name) + 1; |
5347 | |
5396 | |
5348 | return *name ? name : "ILLEGAL"; |
5397 | return *name ? name : "ILLEGAL"; |
5349 | } |
5398 | } |
5350 | |
5399 | |
5351 | static const char * |
5400 | ecb_cold static const char * |
5352 | procname (pointer x) |
5401 | procname (pointer x) |
5353 | { |
5402 | { |
5354 | return opname (procnum (x)); |
5403 | return opname (procnum (x)); |
5355 | } |
5404 | } |
5356 | |
5405 | |
5357 | typedef struct |
5406 | typedef struct |
5358 | { |
5407 | { |
5359 | uint8_t func; |
5408 | uint8_t func; |
5360 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
5409 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
5361 | uint8_t builtin; |
5410 | uint8_t builtin; |
|
|
5411 | #if USE_ERROR_CHECKING |
5362 | uint8_t min_arity; |
5412 | uint8_t min_arity; |
5363 | uint8_t max_arity; |
5413 | uint8_t max_arity; |
5364 | char arg_tests_encoding[3]; |
5414 | char arg_tests_encoding[3]; |
|
|
5415 | #endif |
5365 | } op_code_info; |
5416 | } op_code_info; |
5366 | |
5417 | |
5367 | static const op_code_info dispatch_table[] = { |
5418 | static const op_code_info dispatch_table[] = { |
|
|
5419 | #if USE_ERROR_CHECKING |
5368 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, |
5420 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, |
|
|
5421 | #else |
|
|
5422 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 }, |
|
|
5423 | #endif |
5369 | #include "opdefines.h" |
5424 | #include "opdefines.h" |
5370 | #undef OP_DEF |
5425 | #undef OP_DEF |
5371 | {0} |
5426 | {0} |
5372 | }; |
5427 | }; |
5373 | |
5428 | |
5374 | /* kernel of this interpreter */ |
5429 | /* kernel of this interpreter */ |
5375 | static void ecb_hot |
5430 | ecb_hot static void |
5376 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5431 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5377 | { |
5432 | { |
5378 | SCHEME_V->op = op; |
5433 | SCHEME_V->op = op; |
5379 | |
5434 | |
5380 | for (;;) |
5435 | for (;;) |
… | |
… | |
5415 | { |
5470 | { |
5416 | pointer arg = car (arglist); |
5471 | pointer arg = car (arglist); |
5417 | |
5472 | |
5418 | j = t[0]; |
5473 | j = t[0]; |
5419 | |
5474 | |
|
|
5475 | /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */ |
|
|
5476 | if (j == TST_LIST[0]) |
|
|
5477 | { |
|
|
5478 | if (!tst_is_list (SCHEME_A_ arg)) |
|
|
5479 | break; |
|
|
5480 | } |
|
|
5481 | else |
|
|
5482 | { |
5420 | if (!tests[j - 1].fct (arg)) |
5483 | if (!tests[j - 1].fct (arg)) |
5421 | break; |
5484 | break; |
|
|
5485 | } |
5422 | |
5486 | |
5423 | if (t[1]) /* last test is replicated as necessary */ |
5487 | if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */ |
5424 | t++; |
5488 | t++; |
5425 | |
5489 | |
5426 | arglist = cdr (arglist); |
5490 | arglist = cdr (arglist); |
5427 | i++; |
5491 | i++; |
5428 | } |
5492 | } |
… | |
… | |
5454 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5518 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5455 | return; |
5519 | return; |
5456 | |
5520 | |
5457 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5521 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5458 | { |
5522 | { |
5459 | xwrstr ("No memory!\n"); |
5523 | putstr (SCHEME_A_ "No memory!\n"); |
5460 | return; |
5524 | return; |
5461 | } |
5525 | } |
5462 | } |
5526 | } |
5463 | } |
5527 | } |
5464 | |
5528 | |
5465 | /* ========== Initialization of internal keywords ========== */ |
5529 | /* ========== Initialization of internal keywords ========== */ |
5466 | |
5530 | |
5467 | static void |
5531 | ecb_cold static void |
5468 | assign_syntax (SCHEME_P_ const char *name) |
5532 | assign_syntax (SCHEME_P_ const char *name) |
5469 | { |
5533 | { |
5470 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5534 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5471 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5535 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5472 | } |
5536 | } |
5473 | |
5537 | |
5474 | static void |
5538 | ecb_cold static void |
5475 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5539 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5476 | { |
5540 | { |
5477 | pointer x = mk_symbol (SCHEME_A_ name); |
5541 | pointer x = mk_symbol (SCHEME_A_ name); |
5478 | pointer y = mk_proc (SCHEME_A_ op); |
5542 | pointer y = mk_proc (SCHEME_A_ op); |
5479 | new_slot_in_env (SCHEME_A_ x, y); |
5543 | new_slot_in_env (SCHEME_A_ x, y); |
… | |
… | |
5483 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5547 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5484 | { |
5548 | { |
5485 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5549 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5486 | set_typeflag (y, (T_PROC | T_ATOM)); |
5550 | set_typeflag (y, (T_PROC | T_ATOM)); |
5487 | ivalue_unchecked (y) = op; |
5551 | ivalue_unchecked (y) = op; |
5488 | set_num_integer (y); |
|
|
5489 | return y; |
5552 | return y; |
5490 | } |
5553 | } |
5491 | |
5554 | |
5492 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5555 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5493 | static int |
5556 | ecb_hot static int |
5494 | syntaxnum (pointer p) |
5557 | syntaxnum (pointer p) |
5495 | { |
5558 | { |
5496 | const char *s = strvalue (car (p)); |
5559 | const char *s = strvalue (p); |
5497 | |
5560 | |
5498 | switch (strlength (car (p))) |
5561 | switch (strlength (p)) |
5499 | { |
5562 | { |
5500 | case 2: |
5563 | case 2: |
5501 | if (s[0] == 'i') |
5564 | if (s[0] == 'i') |
5502 | return OP_IF0; /* if */ |
5565 | return OP_IF0; /* if */ |
5503 | else |
5566 | else |
… | |
… | |
5578 | ecb_cold int |
5641 | ecb_cold int |
5579 | scheme_init (SCHEME_P) |
5642 | scheme_init (SCHEME_P) |
5580 | { |
5643 | { |
5581 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5644 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5582 | pointer x; |
5645 | pointer x; |
|
|
5646 | |
|
|
5647 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
5648 | * that memset 0 will also set pointers to 0, but memset does |
|
|
5649 | * of course not guarantee that. screw such systems. |
|
|
5650 | */ |
|
|
5651 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5583 | |
5652 | |
5584 | num_set_fixnum (num_zero, 1); |
5653 | num_set_fixnum (num_zero, 1); |
5585 | num_set_ivalue (num_zero, 0); |
5654 | num_set_ivalue (num_zero, 0); |
5586 | num_set_fixnum (num_one, 1); |
5655 | num_set_fixnum (num_one, 1); |
5587 | num_set_ivalue (num_one, 1); |
5656 | num_set_ivalue (num_one, 1); |
… | |
… | |
5599 | SCHEME_V->save_inport = NIL; |
5668 | SCHEME_V->save_inport = NIL; |
5600 | SCHEME_V->loadport = NIL; |
5669 | SCHEME_V->loadport = NIL; |
5601 | SCHEME_V->nesting = 0; |
5670 | SCHEME_V->nesting = 0; |
5602 | SCHEME_V->interactive_repl = 0; |
5671 | SCHEME_V->interactive_repl = 0; |
5603 | |
5672 | |
5604 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5673 | if (!alloc_cellseg (SCHEME_A)) |
5605 | { |
5674 | { |
5606 | #if USE_ERROR_CHECKING |
5675 | #if USE_ERROR_CHECKING |
5607 | SCHEME_V->no_memory = 1; |
5676 | SCHEME_V->no_memory = 1; |
5608 | return 0; |
5677 | return 0; |
5609 | #endif |
5678 | #endif |
… | |
… | |
5677 | |
5746 | |
5678 | return !SCHEME_V->no_memory; |
5747 | return !SCHEME_V->no_memory; |
5679 | } |
5748 | } |
5680 | |
5749 | |
5681 | #if USE_PORTS |
5750 | #if USE_PORTS |
5682 | void |
5751 | ecb_cold void |
5683 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5752 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5684 | { |
5753 | { |
5685 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5754 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5686 | } |
5755 | } |
5687 | |
5756 | |
5688 | void |
5757 | ecb_cold void |
5689 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5758 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5690 | { |
5759 | { |
5691 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5760 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5692 | } |
5761 | } |
5693 | |
5762 | |
5694 | void |
5763 | ecb_cold void |
5695 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5764 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5696 | { |
5765 | { |
5697 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5766 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5698 | } |
5767 | } |
5699 | |
5768 | |
5700 | void |
5769 | ecb_cold void |
5701 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5770 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5702 | { |
5771 | { |
5703 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5772 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5704 | } |
5773 | } |
5705 | #endif |
5774 | #endif |
5706 | |
5775 | |
5707 | void |
5776 | ecb_cold void |
5708 | scheme_set_external_data (SCHEME_P_ void *p) |
5777 | scheme_set_external_data (SCHEME_P_ void *p) |
5709 | { |
5778 | { |
5710 | SCHEME_V->ext_data = p; |
5779 | SCHEME_V->ext_data = p; |
5711 | } |
5780 | } |
5712 | |
5781 | |
… | |
… | |
5760 | } |
5829 | } |
5761 | } |
5830 | } |
5762 | #endif |
5831 | #endif |
5763 | } |
5832 | } |
5764 | |
5833 | |
5765 | void |
5834 | ecb_cold void |
5766 | scheme_load_file (SCHEME_P_ int fin) |
5835 | scheme_load_file (SCHEME_P_ int fin) |
5767 | { |
5836 | { |
5768 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5837 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5769 | } |
5838 | } |
5770 | |
5839 | |
5771 | void |
5840 | ecb_cold void |
5772 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5841 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5773 | { |
5842 | { |
5774 | dump_stack_reset (SCHEME_A); |
5843 | dump_stack_reset (SCHEME_A); |
5775 | SCHEME_V->envir = SCHEME_V->global_env; |
5844 | SCHEME_V->envir = SCHEME_V->global_env; |
5776 | SCHEME_V->file_i = 0; |
5845 | SCHEME_V->file_i = 0; |
… | |
… | |
5803 | |
5872 | |
5804 | if (SCHEME_V->retcode == 0) |
5873 | if (SCHEME_V->retcode == 0) |
5805 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5874 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5806 | } |
5875 | } |
5807 | |
5876 | |
5808 | void |
5877 | ecb_cold void |
5809 | scheme_load_string (SCHEME_P_ const char *cmd) |
5878 | scheme_load_string (SCHEME_P_ const char *cmd) |
5810 | { |
5879 | { |
5811 | dump_stack_reset (SCHEME_A); |
5880 | dump_stack_reset (SCHEME_A); |
5812 | SCHEME_V->envir = SCHEME_V->global_env; |
5881 | SCHEME_V->envir = SCHEME_V->global_env; |
5813 | SCHEME_V->file_i = 0; |
5882 | SCHEME_V->file_i = 0; |
… | |
… | |
5827 | |
5896 | |
5828 | if (SCHEME_V->retcode == 0) |
5897 | if (SCHEME_V->retcode == 0) |
5829 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5898 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5830 | } |
5899 | } |
5831 | |
5900 | |
5832 | void |
5901 | ecb_cold void |
5833 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5902 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5834 | { |
5903 | { |
5835 | pointer x; |
5904 | pointer x; |
5836 | |
5905 | |
5837 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
5906 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
… | |
… | |
5842 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5911 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5843 | } |
5912 | } |
5844 | |
5913 | |
5845 | #if !STANDALONE |
5914 | #if !STANDALONE |
5846 | |
5915 | |
5847 | void |
5916 | ecb_cold void |
5848 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5917 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5849 | { |
5918 | { |
5850 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5919 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5851 | } |
5920 | } |
5852 | |
5921 | |
5853 | void |
5922 | ecb_cold void |
5854 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5923 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5855 | { |
5924 | { |
5856 | int i; |
5925 | int i; |
5857 | |
5926 | |
5858 | for (i = 0; i < count; i++) |
5927 | for (i = 0; i < count; i++) |
5859 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5928 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5860 | } |
5929 | } |
5861 | |
5930 | |
5862 | pointer |
5931 | ecb_cold pointer |
5863 | scheme_apply0 (SCHEME_P_ const char *procname) |
5932 | scheme_apply0 (SCHEME_P_ const char *procname) |
5864 | { |
5933 | { |
5865 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
5934 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
5866 | } |
5935 | } |
5867 | |
5936 | |
5868 | void |
5937 | ecb_cold void |
5869 | save_from_C_call (SCHEME_P) |
5938 | save_from_C_call (SCHEME_P) |
5870 | { |
5939 | { |
5871 | pointer saved_data = cons (car (S_SINK), |
5940 | pointer saved_data = cons (car (S_SINK), |
5872 | cons (SCHEME_V->envir, |
5941 | cons (SCHEME_V->envir, |
5873 | SCHEME_V->dump)); |
5942 | SCHEME_V->dump)); |
… | |
… | |
5877 | /* Truncate the dump stack so TS will return here when done, not |
5946 | /* Truncate the dump stack so TS will return here when done, not |
5878 | directly resume pre-C-call operations. */ |
5947 | directly resume pre-C-call operations. */ |
5879 | dump_stack_reset (SCHEME_A); |
5948 | dump_stack_reset (SCHEME_A); |
5880 | } |
5949 | } |
5881 | |
5950 | |
5882 | void |
5951 | ecb_cold void |
5883 | restore_from_C_call (SCHEME_P) |
5952 | restore_from_C_call (SCHEME_P) |
5884 | { |
5953 | { |
5885 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5954 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5886 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5955 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5887 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5956 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5888 | /* Pop */ |
5957 | /* Pop */ |
5889 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5958 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5890 | } |
5959 | } |
5891 | |
5960 | |
5892 | /* "func" and "args" are assumed to be already eval'ed. */ |
5961 | /* "func" and "args" are assumed to be already eval'ed. */ |
5893 | pointer |
5962 | ecb_cold pointer |
5894 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5963 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5895 | { |
5964 | { |
5896 | int old_repl = SCHEME_V->interactive_repl; |
5965 | int old_repl = SCHEME_V->interactive_repl; |
5897 | |
5966 | |
5898 | SCHEME_V->interactive_repl = 0; |
5967 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5905 | SCHEME_V->interactive_repl = old_repl; |
5974 | SCHEME_V->interactive_repl = old_repl; |
5906 | restore_from_C_call (SCHEME_A); |
5975 | restore_from_C_call (SCHEME_A); |
5907 | return SCHEME_V->value; |
5976 | return SCHEME_V->value; |
5908 | } |
5977 | } |
5909 | |
5978 | |
5910 | pointer |
5979 | ecb_cold pointer |
5911 | scheme_eval (SCHEME_P_ pointer obj) |
5980 | scheme_eval (SCHEME_P_ pointer obj) |
5912 | { |
5981 | { |
5913 | int old_repl = SCHEME_V->interactive_repl; |
5982 | int old_repl = SCHEME_V->interactive_repl; |
5914 | |
5983 | |
5915 | SCHEME_V->interactive_repl = 0; |
5984 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5927 | |
5996 | |
5928 | /* ========== Main ========== */ |
5997 | /* ========== Main ========== */ |
5929 | |
5998 | |
5930 | #if STANDALONE |
5999 | #if STANDALONE |
5931 | |
6000 | |
5932 | int |
6001 | ecb_cold int |
5933 | main (int argc, char **argv) |
6002 | main (int argc, char **argv) |
5934 | { |
6003 | { |
5935 | # if USE_MULTIPLICITY |
6004 | # if USE_MULTIPLICITY |
5936 | scheme ssc; |
6005 | scheme ssc; |
5937 | scheme *const SCHEME_V = &ssc; |
6006 | scheme *const SCHEME_V = &ssc; |
… | |
… | |
5939 | # endif |
6008 | # endif |
5940 | int fin; |
6009 | int fin; |
5941 | char *file_name = InitFile; |
6010 | char *file_name = InitFile; |
5942 | int retcode; |
6011 | int retcode; |
5943 | int isfile = 1; |
6012 | int isfile = 1; |
|
|
6013 | system ("ps v $PPID");//D |
5944 | |
6014 | |
5945 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6015 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5946 | { |
6016 | { |
5947 | xwrstr ("Usage: tinyscheme -?\n"); |
6017 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
5948 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6018 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
5949 | xwrstr ("followed by\n"); |
6019 | putstr (SCHEME_A_ "followed by\n"); |
5950 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6020 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
5951 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6021 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
5952 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6022 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
5953 | xwrstr ("Use - as filename for stdin.\n"); |
6023 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
5954 | return 1; |
6024 | return 1; |
5955 | } |
6025 | } |
5956 | |
6026 | |
5957 | if (!scheme_init (SCHEME_A)) |
6027 | if (!scheme_init (SCHEME_A)) |
5958 | { |
6028 | { |
5959 | xwrstr ("Could not initialize!\n"); |
6029 | putstr (SCHEME_A_ "Could not initialize!\n"); |
5960 | return 2; |
6030 | return 2; |
5961 | } |
6031 | } |
5962 | |
6032 | |
5963 | # if USE_PORTS |
6033 | # if USE_PORTS |
5964 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6034 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
6009 | fin = open (file_name, O_RDONLY); |
6079 | fin = open (file_name, O_RDONLY); |
6010 | #endif |
6080 | #endif |
6011 | |
6081 | |
6012 | if (isfile && fin < 0) |
6082 | if (isfile && fin < 0) |
6013 | { |
6083 | { |
6014 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6084 | putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
6015 | } |
6085 | } |
6016 | else |
6086 | else |
6017 | { |
6087 | { |
6018 | if (isfile) |
6088 | if (isfile) |
6019 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6089 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
… | |
… | |
6023 | #if USE_PORTS |
6093 | #if USE_PORTS |
6024 | if (!isfile || fin != STDIN_FILENO) |
6094 | if (!isfile || fin != STDIN_FILENO) |
6025 | { |
6095 | { |
6026 | if (SCHEME_V->retcode != 0) |
6096 | if (SCHEME_V->retcode != 0) |
6027 | { |
6097 | { |
6028 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6098 | putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
6029 | } |
6099 | } |
6030 | |
6100 | |
6031 | if (isfile) |
6101 | if (isfile) |
6032 | close (fin); |
6102 | close (fin); |
6033 | } |
6103 | } |