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