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